Debian Codegraph @

types/food/food.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 * The implementation of the Food class of objects.
17 */
18
19 #include "global.h"
20
21 #include <math.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "ob_methods.h"
26 #include "ob_types.h"
27 #include "sounds.h"
28 #include "sproto.h"
29
30 static method_ret food_type_apply(ob_methods *context, object *food, object *applier, int aflags);
31 static void eat_special_food(object *who, object *food);
32 static int dragon_eat_flesh(object *op, object *meal);
33
34 /**
35 * Initializer for the food object type.
36 */
37 void init_type_food(void) {
38 register_apply(FOOD, food_type_apply);
39 register_apply(DRINK, food_type_apply);
40 register_apply(FLESH, food_type_apply);
41 }
42
43 static void cursed_food_effects(object *who, object *food) {
44 /* check for hp, sp change */
45 if (food->stats.hp != 0 && !is_wraith_pl(who)) {
46 if (QUERY_FLAG(food, FLAG_CURSED)) {
47 safe_strncpy(who->contr->killer, food->name,
48 sizeof(who->contr->killer));
49 hit_player(who, food->stats.hp, food, AT_POISON, 1);
50 draw_ext_info(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_CURSED,
51 "Eck!...that was poisonous!");
52 } else {
53 if (food->stats.hp > 0)
54 draw_ext_info(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS,
55 "You begin to feel better.");
56 else
57 draw_ext_info(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_CURSED,
58 "Eck!...that was poisonous!");
59 who->stats.hp += food->stats.hp;
60 }
61 }
62 if (food->stats.sp != 0) {
63 if (QUERY_FLAG(food, FLAG_CURSED)) {
64 draw_ext_info(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_CURSED,
65 "You are drained of mana!");
66 who->stats.sp -= food->stats.sp;
67 if (who->stats.sp < 0)
68 who->stats.sp = 0;
69 } else {
70 draw_ext_info(NDI_UNIQUE, 0, who, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS,
71 "You feel a rush of magical energy!");
72 who->stats.sp += food->stats.sp;
73 /* place limit on max sp from food? */
74 }
75 }
76 }
77
78 static void eat_common(object* applier, object* food) {
79 char buf[MAX_BUF];
80 int capacity_remaining = MAX_FOOD - applier->stats.food;
81 applier->stats.food += food->stats.food;
82 if (capacity_remaining < food->stats.food)
83 applier->stats.hp += capacity_remaining / 50;
84 else
85 applier->stats.hp += food->stats.food / 50;
86 if (applier->stats.hp > applier->stats.maxhp)
87 applier->stats.hp = applier->stats.maxhp;
88 if (applier->stats.food > MAX_FOOD)
89 applier->stats.food = MAX_FOOD;
90 const int wasted_food = food->stats.food - capacity_remaining;
91 if (wasted_food > 0) {
92 const int thresh = MAX_FOOD/4;
93 int speed_penalty;
94 if (wasted_food > thresh) {
95 speed_penalty = 10;
96 } else {
97 speed_penalty = 5;
98 }
99
100 if (slow_living_by(applier, speed_penalty)) {
101 snprintf(buf, sizeof(buf),
102 "%s the %s makes you %s full. You feel yourself moving %s "
103 "slower.",
104 food->type == DRINK ? "Drinking" : "Eating", food->name,
105 wasted_food > thresh ? "very" : "rather",
106 wasted_food > thresh ? "much" : "somewhat");
107 draw_ext_info(NDI_UNIQUE, 0, applier, MSG_TYPE_ATTRIBUTE,
108 MSG_TYPE_ATTRIBUTE_BAD_EFFECT_START, buf);
109 }
110 }
111 }
112
113 /**
114 * Handles applying food.
115 * If player is applying, takes care of messages and dragon special food.
116 * @param context The method context
117 * @param food The food to apply
118 * @param applier The object attempting to apply the food
119 * @param aflags Special flags (always apply/unapply)
120 * @return METHOD_OK unless failure for some reason.
121 */
122 static method_ret food_type_apply(ob_methods *context, object *food, object *applier, int aflags) {
123 if (QUERY_FLAG(food, FLAG_NO_PICK)) {
124 draw_ext_info_format(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY,
125 MSG_TYPE_APPLY_FAILURE, "You can't %s that!",
126 food->type == DRINK ? "drink" : "eat");
127 return METHOD_OK;
128 }
129
130 if (applier->type != PLAYER) {
131 applier->stats.hp = applier->stats.maxhp;
132 } else {
133 char buf[MAX_BUF];
134 if (food->type == FLESH && is_dragon_pl(applier)) {
135 /* check if this is a dragon (player), eating some flesh */
136 if (!QUERY_FLAG(food, FLAG_CURSED)) {
137 dragon_eat_flesh(applier, food);
138 eat_common(applier, food);
139 } else {
140 cursed_food_effects(applier, food);
141 }
142 } else if (is_old_wraith_pl(applier)) {
143 /* Check for old wraith player, give them the feeding skill */
144 object *skill = give_skill_by_name(applier, "wraith feed");
145 if (skill) {
146 SET_FLAG(skill, FLAG_CAN_USE_SKILL);
147 link_player_skills(applier);
148
149 snprintf(buf, sizeof(buf), "You have been dead for too long to taste %s, ", food->name);
150 draw_ext_info(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_FAILURE,
151 buf);
152 draw_ext_info(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_FAILURE,
153 "and seem to have obtained a taste for living flesh.");
154 } else
155 LOG(llevError, "wraith feed skill not found\n");
156 } else if (is_wraith_pl(applier)) {
157 /* Wraith player gets no food from eating. */
158 snprintf(buf, sizeof(buf), "You can no longer taste %s, and do not feel less hungry after %s it.", food->name, food->type == DRINK ? "drinking" : "eating");
159 draw_ext_info(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_FAILURE,
160 buf);
161 } else {
162 /* usual case - not a wraith or a dragon eating non-flesh */
163 if (!QUERY_FLAG(food, FLAG_CURSED)) {
164 if (!is_dragon_pl(applier)) {
165 /* eating message for normal players*/
166 if (food->type == DRINK)
167 snprintf(buf, sizeof(buf), "Ahhh...that %s tasted good.", food->name);
168 else
169 snprintf(buf, sizeof(buf), "The %s tasted %s", food->name, food->type == FLESH ? "terrible!" : "good.");
170 } else {
171 /* eating message for dragon players*/
172 snprintf(buf, sizeof(buf), "The %s tasted terrible!", food->name);
173 }
174 draw_ext_info(0, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS, buf);
175 eat_common(applier, food);
176 } else {
177 cursed_food_effects(applier, food);
178 }
179
180 /* special food hack -b.t. */
181 if (food->title)
182 eat_special_food(applier, food);
183 }
184 }
185 if (food->type == DRINK) {
186 play_sound_map(SOUND_TYPE_ITEM, applier, 0, "drink");
187 }
188 apply_handle_yield(food);
189 object_decrease_nrof_by_one(food);
190 return METHOD_OK;
191 }
192
193 /**
194 * Handles player eating food that temporarily changes status
195 * (resistances, stats).
196 * This used to call cast_change_attr(), but
197 * that doesn't work with the new spell code. Since we know what
198 * the food changes, just grab a force and use that instead.
199 *
200 * @param who
201 * living eating food.
202 * @param food
203 * eaten food.
204 */
205 static void eat_special_food(object *who, object *food) {
206 object *force;
207 int i, did_one = 0;
208 int8_t k;
209
210 force = create_archetype(FORCE_NAME);
211
212 for (i = 0; i < NUM_STATS; i++) {
213 k = get_attr_value(&food->stats, i);
214 if (k) {
215 set_attr_value(&force->stats, i, k);
216 did_one = 1;
217 }
218 }
219
220 /* check if we can protect the eater */
221 for (i = 0; i < NROFATTACKS; i++) {
222 if (food->resist[i] > 0) {
223 force->resist[i] = food->resist[i]/2;
224 did_one = 1;
225 }
226 }
227 if (did_one) {
228 force->speed = MOVE_PER_SECOND;
229 object_update_speed(force);
230 /* bigger morsel of food = longer effect time */
231 force->duration = food->stats.food/4;
232 SET_FLAG(force, FLAG_IS_USED_UP);
233 SET_FLAG(force, FLAG_APPLIED);
234 change_abil(who, force);
235 object_insert_in_ob(force, who);
236 } else {
237 object_free_drop_inventory(force);
238 }
239
240 fix_object(who);
241 }
242
243 /**
244 * A dragon is eating some flesh. If the flesh contains resistances,
245 * there is a chance for the dragon's skin to get improved.
246 *
247 * @param op
248 * object (dragon player) eating the flesh.
249 * @param meal
250 * flesh item, getting chewed in dragon's mouth.
251 * @return
252 * 1 if meal was eaten, 0 else.
253 * @note
254 * meal's nrof isn't decreased, caller is responsible for that.
255 */
256 static int dragon_eat_flesh(object *op, object *meal) {
257 object *skin = NULL; /* pointer to dragon skin force*/
258 object *abil = NULL; /* pointer to dragon ability force*/
259
260 char buf[MAX_BUF]; /* tmp. string buffer */
261 double chance; /* improvement-chance of one resist type */
262 double totalchance = 1; /* total chance of gaining one resistance */
263 double bonus = 0; /* level bonus (improvement is easier at lowlevel) */
264 double mbonus = 0; /* monster bonus */
265 int atnr_winner[NROFATTACKS]; /* winning candidates for resistance improvement */
266 int winners = 0; /* number of winners */
267 int i; /* index */
268
269 /* let's make sure and doublecheck the parameters */
270 if (meal->type != FLESH || !is_dragon_pl(op))
271 return 0;
272
273 /* now grab the 'dragon_skin'- and 'dragon_ability'-forces
274 * from the player's inventory
275 */
276 skin = object_find_by_type_and_arch_name(op, FORCE, "dragon_skin_force");
277 abil = object_find_by_type_and_arch_name(op, FORCE, "dragon_ability_force");
278
279 /* if either skin or ability are missing, this is an old player
280 * which is not to be considered a dragon -> bail out
281 */
282 if (skin == NULL || abil == NULL)
283 return 0;
284
285 /*LOG(llevDebug, "-> player: %d, flesh: %d\n", op->level, meal->level);*/
286
287 /* on to the interesting part: chances for adding resistance */
288 for (i = 0; i < NROFATTACKS; i++) {
289 if (meal->resist[i] > 0 && atnr_is_dragon_enabled(i)) {
290 /* got positive resistance, now calculate improvement chance (0-100) */
291
292 /* this bonus makes resistance increase easier at lower levels */
293 bonus = (settings.max_level-op->level)*30./((double)settings.max_level);
294 if (i == abil->stats.exp)
295 bonus += 5; /* additional bonus for resistance of ability-focus */
296
297 /* monster bonus increases with level, because high-level
298 * flesh is too rare
299 */
300 mbonus = op->level*20./((double)settings.max_level);
301
302 chance = (((double)MIN(op->level+bonus, meal->level+bonus+mbonus))*100./((double)settings.max_level))-skin->resist[i];
303
304 if (chance >= 0.)
305 chance += 1.;
306 else
307 chance = (chance < -12) ? 0. : 1./pow(2., -chance);
308
309 /* chance is proportional to amount of resistance (max. 50) */
310 chance *= ((double)(MIN(meal->resist[i], 50)))/50.;
311
312 /* doubled chance for resistance of ability-focus */
313 if (i == abil->stats.exp)
314 chance = MIN(100., chance*2.);
315
316 /* now make the throw and save all winners (Don't insert luck bonus here!) */
317 if (RANDOM()%10000 < (unsigned int)(chance*100)) {
318 atnr_winner[winners] = i;
319 winners++;
320 }
321
322 if (chance >= 0.01)
323 totalchance *= 1-chance/100;
324
325 /*LOG(llevDebug, " %s: bonus %.1f, chance %.1f\n", attacks[i], bonus, chance);*/
326 }
327 }
328
329 /* inverse totalchance as until now we have the failure-chance */
330 totalchance = 100-totalchance*100;
331 /* print message according to totalchance */
332 if (totalchance > 50.)
333 snprintf(buf, sizeof(buf), "Hmm! The %s tasted delicious!", meal->name);
334 else if (totalchance > 10.)
335 snprintf(buf, sizeof(buf), "The %s tasted very good.", meal->name);
336 else if (totalchance > 1.)
337 snprintf(buf, sizeof(buf), "The %s tasted good.", meal->name);
338 else if (totalchance > 0.1)
339 snprintf(buf, sizeof(buf), "The %s tasted bland.", meal->name);
340 else if (totalchance >= 0.01)
341 snprintf(buf, sizeof(buf), "The %s had a boring taste.", meal->name);
342 else if (meal->last_eat > 0 && atnr_is_dragon_enabled(meal->last_eat))
343 snprintf(buf, sizeof(buf), "The %s tasted strange.", meal->name);
344 else
345 snprintf(buf, sizeof(buf), "The %s had no taste.", meal->name);
346 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS,
347 buf);
348
349 /* now choose a winner if we have any */
350 i = -1;
351 if (winners > 0)
352 i = atnr_winner[RANDOM()%winners];
353
354 if (i >= 0 && i < NROFATTACKS && skin->resist[i] < 95) {
355 /* resistance increased! */
356 skin->resist[i]++;
357 fix_object(op);
358
359 draw_ext_info_format(NDI_UNIQUE|NDI_RED, 0, op, MSG_TYPE_ATTRIBUTE, MSG_TYPE_ATTRIBUTE_PROTECTION_GAIN,
360 "Your skin is now more resistant to %s!",
361 change_resist_msg[i]);
362 }
363
364 /* if this flesh contains a new ability focus, we mark it
365 * into the ability_force and it will take effect on next level
366 */
367 if (meal->last_eat > 0
368 && atnr_is_dragon_enabled(meal->last_eat)
369 && meal->last_eat != abil->last_eat) {
370 abil->last_eat = meal->last_eat; /* write:last_eat <new attnr focus> */
371
372 if (meal->last_eat != abil->stats.exp) {
373 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_ATTRIBUTE, MSG_TYPE_ATTRIBUTE_RACE,
374 "Your metabolism prepares to focus on %s!",
375 change_resist_msg[meal->last_eat]);
376 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_ATTRIBUTE, MSG_TYPE_ATTRIBUTE_RACE,
377 "The change will happen at level %d",
378 abil->level+1);
379 } else {
380 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_ATTRIBUTE, MSG_TYPE_ATTRIBUTE_RACE,
381 "Your metabolism will continue to focus on %s.",
382 change_resist_msg[meal->last_eat]);
383 abil->last_eat = 0;
384 }
385 }
386 return 1;
387 }