Crossfire Server, Trunk
lamp.cpp
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:112
global.h
SET_FLAG
#define SET_FLAG(xyz, p)
Definition: define.h:224
QUERY_FLAG
#define QUERY_FLAG(xyz, p)
Definition: define.h:226
stringbuffer_append_printf
void stringbuffer_append_printf(StringBuffer *sb, const char *format,...)
Definition: stringbuffer.cpp:138
update_position
void update_position(mapstruct *m, int x, int y)
Definition: map.cpp:2110
stringbuffer_new
StringBuffer * stringbuffer_new(void)
Definition: stringbuffer.cpp:57
register_apply
void register_apply(int ob_type, apply_func method)
Definition: ob_types.cpp:62
METHOD_OK
#define METHOD_OK
Definition: ob_methods.h:15
MSG_TYPE_APPLY_CURSED
#define MSG_TYPE_APPLY_CURSED
Definition: newclient.h:594
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,...) PRINTF_ARGS(6
fix_object
void fix_object(object *op)
Definition: living.cpp:1125
Ice.tmp
int tmp
Definition: Ice.py:207
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
buf
StringBuffer * buf
Definition: readable.cpp:1565
object_insert_in_ob
object * object_insert_in_ob(object *op, object *where)
Definition: object.cpp:2853
autojail.who
who
Definition: autojail.py:3
stringbuffer_finish
char * stringbuffer_finish(StringBuffer *sb)
Definition: stringbuffer.cpp:76
object_free_drop_inventory
void object_free_drop_inventory(object *ob)
Definition: object.cpp:1560
MSG_TYPE_APPLY_SUCCESS
#define MSG_TYPE_APPLY_SUCCESS
Definition: newclient.h:592
query_name
void query_name(const object *op, char *buf, size_t size)
Definition: item.cpp:588
FLAG_KNOWN_CURSED
#define FLAG_KNOWN_CURSED
Definition: define.h:320
lamp_type_apply
static method_ret lamp_type_apply(object *lighter, object *applier, int aflags)
Definition: lamp.cpp:107
init_type_lamp
void init_type_lamp(void)
Definition: lamp.cpp:40
FLAG_DAMNED
#define FLAG_DAMNED
Definition: define.h:317
SET_MAP_FLAGS
#define SET_MAP_FLAGS(M, X, Y, C)
Definition: map.h:159
LAMP
@ LAMP
Definition: object.h:206
living::food
int32_t food
Definition: living.h:48
sproto.h
nlohmann::detail::void
j template void())
Definition: json.hpp:4099
StringBuffer
Definition: stringbuffer.cpp:25
method_ret
char method_ret
Definition: ob_methods.h:14
ob_types.h
sounds.h
NDI_UNIQUE
#define NDI_UNIQUE
Definition: newclient.h:251
object::name
sstring name
Definition: object.h:319
living::maxsp
int16_t maxsp
Definition: living.h:43
P_NEED_UPDATE
#define P_NEED_UPDATE
Definition: map.h:237
give.op
op
Definition: give.py:33
object_split
object * object_split(object *orig_ob, uint32_t nr, char *err, size_t size)
Definition: object.cpp:2633
CLEAR_FLAG
#define CLEAR_FLAG(xyz, p)
Definition: define.h:225
AP_NOPRINT
#define AP_NOPRINT
Definition: define.h:585
arch_to_object
object * arch_to_object(archetype *at)
Definition: arch.cpp:229
MSG_TYPE_APPLY_FAILURE
#define MSG_TYPE_APPLY_FAILURE
Definition: newclient.h:593
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.cpp:308
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.cpp:147
object_remove
void object_remove(object *op)
Definition: object.cpp:1833
METHOD_ERROR
#define METHOD_ERROR
Definition: ob_methods.h:17
update_all_los
void update_all_los(const mapstruct *map, int x, int y)
Definition: los.cpp:595
object::nrof
uint32_t nrof
Definition: object.h:342
ob_methods.h
object::stats
living stats
Definition: object.h:378
MSG_TYPE_APPLY
#define MSG_TYPE_APPLY
Definition: newclient.h:397
FLAG_CURSED
#define FLAG_CURSED
Definition: define.h:316
MSG_TYPE_APPLY_ERROR
#define MSG_TYPE_APPLY_ERROR
Definition: newclient.h:590
object_get_player_container
object * object_get_player_container(object *op)
Definition: object.cpp:607
register_describe
void register_describe(int ob_type, describe_func method)
Definition: ob_types.cpp:80
do_turn
static void do_turn(object *op, object *who, int aflags, const char *onoff)
Definition: lamp.cpp:56