Debian Codegraph @

types/lighter/lighter.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /**
15 * @file
16 * Implementation of the Lighter class of objects.
17 */
18 #include "global.h"
19
20 #include <string.h>
21
22 #include "ob_methods.h"
23 #include "ob_types.h"
24 #include "sounds.h"
25 #include "sproto.h"
26
27 static method_ret lighter_type_apply(ob_methods *context, object *lighter, object *applier, int aflags);
28
29 /**
30 * Initializer for the LIGHTER object type.
31 */
32 void init_type_lighter(void) {
33 register_apply(LIGHTER, lighter_type_apply);
34 }
35
36 /**
37 * Attempts to apply a lighter.
38 *
39 * Designed primarily to light torches/lanterns/etc.
40 * Also burns up burnable material too. First object in the inventory is
41 * the selected object to "burn". -b.t.
42 *
43 * @param context
44 * method context.
45 * @param lighter
46 * lighter to apply.
47 * @param applier
48 * object attempting to apply the lighter.
49 * @param aflags
50 * special flags (always apply/unapply).
51 * @return
52 * METHOD_UNHANDLED if the applier is not a player, METHOD_OK else..
53 */
54 static method_ret lighter_type_apply(ob_methods *context, object *lighter, object *applier, int aflags) {
55 object *item;
56 int is_player_env = 0;
57 uint32_t nrof;
58 tag_t count;
59 char item_name[MAX_BUF];
60
61 if (applier->type != PLAYER)
62 return METHOD_UNHANDLED;
63
64 item = find_marked_object(applier);
65 if (!item) {
66 draw_ext_info(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR,
67 "You need to mark a lightable object.");
68 return METHOD_OK;
69 }
70
71 if (lighter->last_eat && lighter->stats.food) {
72 /* Split multiple lighters if they're being used up. Otherwise
73 * one charge from each would be used up. --DAMN
74 */
75 if (lighter->nrof > 1) {
76 object *oneLighter = object_split(lighter, 1, NULL, 0);
77
78 oneLighter->stats.food--;
79 oneLighter = object_insert_in_ob(oneLighter, applier);
80 } else {
81 lighter->stats.food--;
82 }
83 } else if (lighter->last_eat) { /* no charges left in lighter */
84 draw_ext_info_format(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_FAILURE,
85 "You fail to light the %s with a used up %s.",
86 item->name, lighter->name);
87 return METHOD_OK;
88 }
89
90 /* Perhaps we should split what we are trying to light on fire?
91 * I can't see many times when you would want to light multiple
92 * objects at once. */
93 nrof = item->nrof;
94 count = item->count;
95 /* If the item is destroyed, we don't have a valid pointer to the
96 * name object, so make a copy so the message we print out makes
97 * some sense. */
98 safe_strncpy(item_name, item->name, sizeof(item_name));
99 if (applier == object_get_player_container(item))
100 is_player_env = 1;
101
102 save_throw_object(item, AT_FIRE, applier);
103
104 /* Change to check count and not freed, since the object pointer
105 * may have gotten recycled */
106 if ((nrof != item->nrof) || (count != item->count)) {
107 draw_ext_info_format(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS,
108 "You light the %s with the %s.",
109 item_name, lighter->name);
110
111 /* Need to update the player so that the players glow radius
112 * gets changed. */
113 if (is_player_env)
114 fix_object(applier);
115 } else {
116 draw_ext_info_format(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_FAILURE,
117 "You attempt to light the %s with the %s and fail.",
118 item->name, lighter->name);
119 }
120
121 return METHOD_OK;
122 }