Crossfire Server, Trunk
lamp.c
Go to the documentation of this file.
1 /*
2  CrossFire, A Multiplayer game for X-windows
3 
4  Copyright (C) 2007 Crossfire Development Team
5  Copyright (C) 1992 Frank Tore Johansen
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21  The authors can be reached via e-mail at crossfire-devel@real-time.com
22 */
23 
28 #include <global.h>
29 #include <ob_methods.h>
30 #include <ob_types.h>
31 #include <sounds.h>
32 #include <sproto.h>
33 
34 static method_ret lamp_type_apply(object *lighter, object *applier, int aflags);
35 static void lamp_type_describe(const object *op, const object *observer, int use_media_tags, char *buf, size_t size);
36 
40 void init_type_lamp(void) {
43 }
44 
56 static void do_turn(object *op, object *who, int aflags, const char *onoff) {
57  object *tmp2;
58 
59  if (!(aflags&AP_NOPRINT))
61  "You turn %s your %s.",
62  onoff, op->name);
63 
64  tmp2 = arch_to_object(op->other_arch);
65  tmp2->stats.food = op->stats.food;
67  CLEAR_FLAG(tmp2, FLAG_APPLIED);
68  else
69  SET_FLAG(tmp2, FLAG_APPLIED);
72  object_insert_in_ob(tmp2, who);
73 
76 
77  fix_object(who);
78 
80  if (who->type == PLAYER) {
81  if (!(aflags&AP_NOPRINT))
83  "Oops, it feels deadly cold!");
85  }
86  }
87 
88  if (who->map) {
89  SET_MAP_FLAGS(who->map, who->x, who->y, P_NEED_UPDATE);
90  update_position(who->map, who->x, who->y);
91  update_all_los(who->map, who->x, who->y);
92  }
93 }
94 
107 static method_ret lamp_type_apply(object *lamp, object *applier, int aflags) {
108  object *tmp;
109 
110  if (object_get_player_container(lamp) != applier) {
112  "You must get it first!\n");
113  return METHOD_ERROR;
114  }
115 
116  if (lamp->nrof > 1)
117  tmp = object_split(lamp, lamp->nrof-1, NULL, 0);
118  else
119  tmp = NULL;
120 
121  if (lamp->stats.food > lamp->stats.maxsp) {
122  // Lamps created before food value was adjusted
123  lamp->stats.food = lamp->stats.maxsp;
124  }
125 
126  if (QUERY_FLAG(lamp, FLAG_APPLIED))
127  do_turn(lamp, applier, aflags, "off");
128  else {
129  if (lamp->stats.food < 1) {
130  if (!(aflags&AP_NOPRINT))
132  "Your %s is out of fuel!",
133  lamp->name);
134  return METHOD_OK;
135  }
136  do_turn(lamp, applier, aflags, "on");
137  }
138 
139  /* insert the portion that was split off. */
140  if (tmp != NULL) {
141  object_insert_in_ob(tmp, applier);
142  }
143 
144  return METHOD_OK;
145 }
146 
147 static void lamp_type_describe(const object *op, const object *observer, int use_media_tags, char *buf, size_t size) {
149  char *final;
150  (void)observer;
151  (void)use_media_tags;
152 
153  buf[0] = '\0';
154  query_name(op, buf, size-1);
155  buf[size-1] = 0;
156 
157  if (!QUERY_FLAG(observer, FLAG_BLIND)) {
158  stringbuffer_append_printf(sb, ". It is %s. ", QUERY_FLAG(op, FLAG_APPLIED) ? "on" : "off");
159  if (op->stats.maxsp) {
160  float fill = (float)op->stats.food / op->stats.maxsp;
161  stringbuffer_append_printf(sb, "It is ");
162  if (fill == 1) {
163  stringbuffer_append_printf(sb, "full.");
164  } else if (fill == 0) {
165  stringbuffer_append_printf(sb, "empty.");
166  } else if (fill > 1) {
167  // Lamps created before food value was adjusted
168  stringbuffer_append_printf(sb, "very full.");
169  } else if (fill > 0.9) {
170  stringbuffer_append_printf(sb, "nearly full.");
171  } else if (fill > 0.6) {
172  stringbuffer_append_printf(sb, "more than half full.");
173  } else if (fill > 0.4) {
174  stringbuffer_append_printf(sb, "about half full.");
175  } else if (fill > 0.2) {
176  stringbuffer_append_printf(sb, "less than half full.");
177  } else if (fill > 0.1) {
178  stringbuffer_append_printf(sb, "almost empty.");
179  }
180  }
181  }
182 
183  final = stringbuffer_finish(sb);
184  strncat(buf, final, size);
185  free(final);
186 }
PLAYER
@ PLAYER
Definition: object.h:107
global.h
object_remove
void object_remove(object *op)
Definition: object.c:1819
stringbuffer_new
StringBuffer * stringbuffer_new(void)
Definition: stringbuffer.c:57
SET_FLAG
#define SET_FLAG(xyz, p)
Definition: define.h:224
QUERY_FLAG
#define QUERY_FLAG(xyz, p)
Definition: define.h:226
METHOD_OK
#define METHOD_OK
Definition: ob_methods.h:15
MSG_TYPE_APPLY_CURSED
#define MSG_TYPE_APPLY_CURSED
Definition: newclient.h:605
Ice.tmp
int tmp
Definition: Ice.py:207
do_turn
static void do_turn(object *op, object *who, int aflags, const char *onoff)
Definition: lamp.c:56
FLAG_INV_LOCKED
#define FLAG_INV_LOCKED
Definition: define.h:329
FLAG_APPLIED
#define FLAG_APPLIED
Definition: define.h:235
FLAG_BLIND
#define FLAG_BLIND
Definition: define.h:336
obj::nrof
uint32_t nrof
Definition: object.h:337
liv::maxsp
int16_t maxsp
Definition: living.h:43
autojail.who
who
Definition: autojail.py:3
obj::name
sstring name
Definition: object.h:314
MSG_TYPE_APPLY_SUCCESS
#define MSG_TYPE_APPLY_SUCCESS
Definition: newclient.h:603
query_name
void query_name(const object *op, char *buf, size_t size)
Definition: item.c:585
FLAG_KNOWN_CURSED
#define FLAG_KNOWN_CURSED
Definition: define.h:320
FLAG_DAMNED
#define FLAG_DAMNED
Definition: define.h:317
SET_MAP_FLAGS
#define SET_MAP_FLAGS(M, X, Y, C)
Definition: map.h:164
fix_object
void fix_object(object *op)
Definition: living.c:1126
stringbuffer_finish
char * stringbuffer_finish(StringBuffer *sb)
Definition: stringbuffer.c:76
LAMP
@ LAMP
Definition: object.h:201
lamp_type_describe
static void lamp_type_describe(const object *op, const object *observer, int use_media_tags, char *buf, size_t size)
Definition: lamp.c:147
sproto.h
liv::food
int32_t food
Definition: living.h:48
nlohmann::detail::void
j template void())
Definition: json.hpp:4099
register_describe
void register_describe(int ob_type, describe_func method)
Definition: ob_types.c:80
StringBuffer
Definition: stringbuffer.c:25
register_apply
void register_apply(int ob_type, apply_func method)
Definition: ob_types.c:62
method_ret
char method_ret
Definition: ob_methods.h:14
ob_types.h
sounds.h
NDI_UNIQUE
#define NDI_UNIQUE
Definition: newclient.h:262
obj::stats
living stats
Definition: object.h:373
P_NEED_UPDATE
#define P_NEED_UPDATE
Definition: map.h:240
give.op
op
Definition: give.py:33
CLEAR_FLAG
#define CLEAR_FLAG(xyz, p)
Definition: define.h:225
buf
StringBuffer * buf
Definition: readable.c:1610
AP_NOPRINT
#define AP_NOPRINT
Definition: define.h:585
stringbuffer_append_printf
void stringbuffer_append_printf(StringBuffer *sb, const char *format,...)
Definition: stringbuffer.c:138
object_insert_in_ob
object * object_insert_in_ob(object *op, object *where)
Definition: object.c:2833
update_all_los
void update_all_los(const mapstruct *map, int x, int y)
Definition: los.c:536
arch_to_object
object * arch_to_object(archetype *at)
Definition: arch.cpp:232
lamp_type_apply
static method_ret lamp_type_apply(object *lighter, object *applier, int aflags)
Definition: lamp.c:107
MSG_TYPE_APPLY_FAILURE
#define MSG_TYPE_APPLY_FAILURE
Definition: newclient.h:604
draw_ext_info
void draw_ext_info(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *message)
Definition: main.c:309
object_free_drop_inventory
void object_free_drop_inventory(object *ob)
Definition: object.c:1546
update_position
void update_position(mapstruct *m, int x, int y)
Definition: map.c:2133
METHOD_ERROR
#define METHOD_ERROR
Definition: ob_methods.h:17
ob_methods.h
object_get_player_container
object * object_get_player_container(object *op)
Definition: object.c:611
MSG_TYPE_APPLY
#define MSG_TYPE_APPLY
Definition: newclient.h:408
FLAG_CURSED
#define FLAG_CURSED
Definition: define.h:316
MSG_TYPE_APPLY_ERROR
#define MSG_TYPE_APPLY_ERROR
Definition: newclient.h:601
init_type_lamp
void init_type_lamp(void)
Definition: lamp.c:40
object_split
object * object_split(object *orig_ob, uint32_t nr, char *err, size_t size)
Definition: object.c:2613
draw_ext_info_format
void draw_ext_info_format(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *format,...)
Definition: main.c:319