Crossfire Server  1.75.0
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;
66  if (QUERY_FLAG(op, FLAG_APPLIED))
67  CLEAR_FLAG(tmp2, FLAG_APPLIED);
68  else
69  SET_FLAG(tmp2, FLAG_APPLIED);
70  if (QUERY_FLAG(op, FLAG_INV_LOCKED))
72  object_insert_in_ob(tmp2, who);
73 
74  object_remove(op);
76 
77  fix_object(who);
78 
79  if (QUERY_FLAG(op, FLAG_CURSED) || QUERY_FLAG(op, FLAG_DAMNED)) {
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 }
Sound-related defines.
#define FLAG_DAMNED
The object is very cursed.
Definition: define.h:305
void update_position(mapstruct *m, int x, int y)
This function updates various attributes about a specific space on the map (what it looks like...
Definition: map.cpp:2147
char * stringbuffer_finish(StringBuffer *sb)
Deallocate the string buffer instance and return the string.
#define P_NEED_UPDATE
This space is out of date.
Definition: map.h:242
object * object_split(object *orig_ob, uint32_t nr, char *err, size_t size)
object_split(ob,nr) splits up ob into two parts.
Definition: object.cpp:2622
#define MSG_TYPE_APPLY_FAILURE
Apply OK, but no/bad result.
Definition: newclient.h:640
void register_describe(int ob_type, describe_func method)
Registers the describe method for the given type.
Definition: ob_types.cpp:80
void draw_ext_info_format(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *format,...) PRINTF_ARGS(6
#define METHOD_ERROR
Definition: ob_methods.h:17
void fix_object(object *op)
Updates all abilities given by applied objects in the inventory of the given object.
Definition: living.cpp:1132
void object_remove(object *op)
This function removes the object op from the linked list of objects which it is currently tied to...
Definition: object.cpp:1818
#define FLAG_BLIND
If set, object cannot see (visually)
Definition: define.h:324
int16_t y
Position in the map for this object.
Definition: object.h:335
StringBuffer * stringbuffer_new(void)
Create a new string buffer.
int16_t x
Definition: object.h:335
Global type definitions and header inclusions.
void draw_ext_info(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *message)
Sends message to player(s).
Definition: main.cpp:308
#define MSG_TYPE_APPLY
Applying objects.
Definition: newclient.h:425
char method_ret
Define some standard return values for callbacks which don&#39;t need to return any other results...
Definition: ob_methods.h:14
void stringbuffer_append_printf(StringBuffer *sb, const char *format,...)
Append a formatted string to a string buffer instance.
object * arch_to_object(archetype *at)
Creates and returns a new object which is a copy of the given archetype.
Definition: arch.cpp:227
#define METHOD_OK
Definition: ob_methods.h:15
#define SET_FLAG(xyz, p)
Definition: define.h:384
#define QUERY_FLAG(xyz, p)
Definition: define.h:386
#define MSG_TYPE_APPLY_ERROR
Definition: newclient.h:637
void query_name(const object *op, char *buf, size_t size)
Describes an item.
Definition: item.cpp:594
void object_free_drop_inventory(object *ob)
Frees everything allocated by an object, removes it from the list of used objects, and puts it on the list of free objects.
Definition: object.cpp:1545
void update_all_los(const mapstruct *map, int x, int y)
This function makes sure that update_los() will be called for all players on the given map within the...
Definition: los.cpp:595
static method_ret lamp_type_apply(object *lighter, object *applier, int aflags)
Applies a lamp.
Definition: lamp.cpp:107
void init_type_lamp(void)
Initializer for the LAMP object type.
Definition: lamp.cpp:40
#define AP_NOPRINT
Don&#39;t print messages - caller will do that may be some that still print.
Definition: define.h:607
living stats
Str, Con, Dex, etc.
Definition: object.h:378
struct mapstruct * map
Pointer to the map in which this object is present.
Definition: object.h:305
Lamp.
Definition: object.h:206
Object type variables.
#define FLAG_KNOWN_CURSED
The object is known to be cursed.
Definition: define.h:308
#define FLAG_CURSED
The object is cursed.
Definition: define.h:304
See Player.
Definition: object.h:112
struct archetype * other_arch
Pointer used for various things - mostly used for what this objects turns into or what this object cr...
Definition: object.h:425
#define SET_MAP_FLAGS(M, X, Y, C)
Sets map flags.
Definition: map.h:162
sstring name
The name of the object, obviously...
Definition: object.h:319
uint32_t nrof
Number of objects.
Definition: object.h:342
#define CLEAR_FLAG(xyz, p)
Definition: define.h:385
uint8_t type
PLAYER, BULLET, etc.
Definition: object.h:348
#define FLAG_APPLIED
Object is ready for use by living.
Definition: define.h:222
static void do_turn(object *op, object *who, int aflags, const char *onoff)
Turn on/off the lamp, based on op&#39;s APPLIED status.
Definition: lamp.cpp:56
#define MSG_TYPE_APPLY_SUCCESS
Was able to apply object.
Definition: newclient.h:639
#define MSG_TYPE_APPLY_CURSED
Applied a cursed object (BAD)
Definition: newclient.h:641
int16_t maxsp
Max spell points.
Definition: living.h:43
#define NDI_UNIQUE
Print immediately, don&#39;t buffer.
Definition: newclient.h:270
int32_t food
How much food in stomach.
Definition: living.h:48
StringBuffer * buf
Definition: readable.cpp:1563
A buffer that will be expanded as content is added to it.
object * object_get_player_container(object *op)
Finds the player carrying an object.
Definition: object.cpp:592
object * object_insert_in_ob(object *op, object *where)
This function inserts the object op in the linked list inside the object environment.
Definition: object.cpp:2842
Object type functions and variables.
#define FLAG_INV_LOCKED
Item will not be dropped from inventory.
Definition: define.h:317
static void lamp_type_describe(const object *op, const object *observer, int use_media_tags, char *buf, size_t size)
Definition: lamp.cpp:147
void register_apply(int ob_type, apply_func method)
Registers the apply method for the given type.
Definition: ob_types.cpp:62