Debian Codegraph @

common/living.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 living.c
16 * Functions related to attributes, weight, experience, which concern only living things.
17 *
18 * @todo
19 * make "stat"/"attr" coherent.
20 */
21
22 #include "global.h"
23
24 #include <assert.h>
25 #include <ctype.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "sproto.h"
30 #include "living.h"
31
32 static float get_con_bonus(int stat);
33 static float get_sp_bonus(int stat);
34 static float get_grace_bonus(int stat);
35 static size_t get_index(int stat, size_t max_index);
36
37 /**
38 * Handy little macro that adds exp and keeps it within bounds. Since
39 * we are now using 64 bit values, I'm not all concerned about overflow issues
40 * with exptotal wrapping. exptotal is typically op->exp, or op->total_exp
41 */
42 #define ADD_EXP(exptotal, exp) do { exptotal += exp; if (exptotal > MAX_EXPERIENCE) exptotal = MAX_EXPERIENCE; } while (0)
43 #define ADD_TOTALEXP(exptotal, exp) do { exptotal += exp; if (exptotal > MAX_TOTAL_EXPERIENCE) exptotal = MAX_TOTAL_EXPERIENCE; } while(0)
44
45 /**
46 * The definitions below are indexes into the bonuses[] array.
47 * Rather than have a bunch of different variables, it is easier
48 * to just have a single array - all access to these is done
49 * through the get_cha_...() function in any case -
50 * making it an array makes processing simpler.
51 *
52 * The INT_ prefix is to note the type of array the bonus
53 * goes into (vs FLOAT) - it is unfortunately that it is also the
54 * name of one of the stats.
55 *
56 * The NUM_BONUSES is how many bonuses they are (size of the array),
57 * so is equal to last value +1.
58 */
59 #define INT_FEAR_BONUS 0
60 #define INT_TURN_BONUS 1
61 #define INT_CLERIC_CHANCE 2
62 #define INT_LEARN_SPELL 3
63 #define INT_CHA_BONUS 4
64 #define INT_DEX_BONUS 5
65 #define INT_DAM_BONUS 6
66 #define INT_THAC0_BONUS 7
67 #define INT_WEIGHT_LIMIT 8
68 #define NUM_INT_BONUSES 9
69
70 static int *int_bonuses[NUM_INT_BONUSES];
71
72 /**
73 * Following array corresponds to the defines above, but are the text
74 * names as found in the file. In this way, processing of file is simpler.
75 */
76 static const char *int_bonus_names[NUM_INT_BONUSES] = {
77 "cha_fear_bonus", "wis_turn_bonus", "wis_cleric_chance", "int_wis_learn_spell",
78 "cha_shop_bonus", "dex_bonus", "str_damage_bonus", "str_hit_bonus",
79 "str_weight_limit",
80 };
81
82 /**
83 * This is basically same as above, but for bonuses in which we store
84 * the value as a float.
85 */
86 #define FLOAT_CON_BONUS 0
87 #define FLOAT_DEX_BONUS 1
88 #define FLOAT_SP_BONUS 2
89 #define FLOAT_GRACE_BONUS 3
90 #define NUM_FLOAT_BONUSES 4
91 static float *float_bonuses[NUM_FLOAT_BONUSES];
92 static const char *float_bonus_names[NUM_FLOAT_BONUSES] = {
93 "con_hp_bonus", "dex_speed_bonus", "pow_int_sp_bonus", "wis_pow_grace_bonus"
94 };
95
96 /*
97 * Since this is nowhere defined ...
98 * Both come in handy at least in function add_exp()
99 */
100 #define MAX_EXPERIENCE levels[settings.max_level]
101
102 extern int64_t *levels;
103
104 #define MAX_SAVE_LEVEL 110
105 /**
106 * Probability to avoid something.
107 *
108 * This no longer needs to be changed anytime the number of
109 * levels is increased - rather, did_make_save() will do the
110 * right thing and always use range within this table.
111 * for safety, savethrow should not be accessed directly anymore,
112 * and instead did_make_save() should be used instead.
113 */
114 static const int savethrow[MAX_SAVE_LEVEL+1] = {
115 18,
116 18, 17, 16, 15, 14, 14, 13, 13, 12, 12,
117 12, 11, 11, 11, 11, 10, 10, 10, 10, 9,
118 9, 9, 9, 9, 8, 8, 8, 8, 8, 8,
119 7, 7, 7, 7, 7, 7, 7, 6, 6, 6,
120 6, 6, 6, 6, 6, 5, 5, 5, 5, 5,
121 5, 5, 5, 5, 4, 4, 4, 4, 4, 4,
122 4, 4, 4, 4, 3, 3, 3, 3, 3, 3,
123 3, 3, 3, 3, 3, 2, 2, 2, 2, 2,
124 2, 2, 2, 2, 2, 2, 2, 2, 2, 1,
125 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
126 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
127 };
128
129 /** Attack type names. */
130 const char *const attacks[NROFATTACKS] = {
131 "physical", "magical", "fire", "electricity", "cold",
132 "confusion", "acid", "drain", "weaponmagic", "ghosthit",
133 "poison", "slow", "paralyze", "turn undead", "fear",
134 "cancellation", "depletion", "death", "chaos", "counterspell",
135 "god power", "holy power", "blinding", "", "life stealing",
136 "disease"
137 };
138
139 /** Message when a player is drained of a stat. */
140 const char *const drain_msg[NUM_STATS] = {
141 "You feel drained of strength.",
142 "You feel drained of agility.",
143 "You feel drained of health.",
144 "You feel drained of wisdom.",
145 "You feel drained of charisma.",
146 "You feel drained of intelligence.",
147 "You feel drained of power."
148 };
149
150 /** Message when a player has a stat restored. */
151 const char *const restore_msg[NUM_STATS] = {
152 "You feel your strength return.",
153 "You feel your agility return.",
154 "You feel your health return.",
155 "You feel your wisdom return.",
156 "You feel your charisma return.",
157 "You feel your intelligence return.",
158 "You feel your power return."
159 };
160
161 /** Message when a player increases permanently a stat. */
162 const char *const gain_msg[NUM_STATS] = {
163 "You feel stronger.",
164 "You feel more agile.",
165 "You feel healthy.",
166 "You feel wiser.",
167 "You seem to look better.",
168 "You feel smarter.",
169 "You feel more potent."
170 };
171
172 /** Message when a player decreases permanently a stat. */
173 const char *const lose_msg[NUM_STATS] = {
174 "You feel weaker!",
175 "You feel clumsy!",
176 "You feel less healthy!",
177 "You feel foolish!",
178 "You look ugly!",
179 "You feel stupid!",
180 "You feel less potent!"
181 };
182
183 /** Name of stats. */
184 const char *const statname[NUM_STATS] = {
185 "strength",
186 "dexterity",
187 "constitution",
188 "wisdom",
189 "charisma",
190 "intelligence",
191 "power"
192 };
193
194 /** Short name of stats. */
195 const char *const short_stat_name[NUM_STATS] = {
196 "Str",
197 "Dex",
198 "Con",
199 "Wis",
200 "Cha",
201 "Int",
202 "Pow"
203 };
204
205 /**
206 * Sets Str/Dex/con/Wis/Cha/Int/Pow in stats to value, depending on
207 * what attr is (STR to POW).
208 *
209 * @param stats
210 * item to modify. Must not be NULL.
211 * @param attr
212 * attribute to change.
213 * @param value
214 * new value.
215 *
216 * @todo
217 * check if attr is valid? Check whether value is valid or not.
218 */
219 void set_attr_value(living *stats, int attr, int8_t value) {
220 switch (attr) {
221 case STRENGTH:
222 stats->Str = value;
223 break;
224
225 case DEXTERITY:
226 stats->Dex = value;
227 break;
228
229 case CONSTITUTION:
230 stats->Con = value;
231 break;
232
233 case WISDOM:
234 stats->Wis = value;
235 break;
236
237 case POWER:
238 stats->Pow = value;
239 break;
240
241 case CHARISMA:
242 stats->Cha = value;
243 break;
244
245 case INTELLIGENCE:
246 stats->Int = value;
247 break;
248 }
249 }
250
251 /**
252 * Like set_attr_value(), but instead the value (which can be negative)
253 * is added to the specified stat.
254 *
255 * @param stats
256 * item to modify. Must not be NULL.
257 * @param attr
258 * attribute to change.
259 * @param value
260 * delta (can be positive).
261 *
262 * @todo
263 * check if attr is valid? Checks result valus is valid?
264 */
265 void change_attr_value(living *stats, int attr, int8_t value) {
266 if (value == 0)
267 return;
268 switch (attr) {
269 case STRENGTH:
270 stats->Str += value;
271 break;
272
273 case DEXTERITY:
274 stats->Dex += value;
275 break;
276
277 case CONSTITUTION:
278 stats->Con += value;
279 break;
280
281 case WISDOM:
282 stats->Wis += value;
283 break;
284
285 case POWER:
286 stats->Pow += value;
287 break;
288
289 case CHARISMA:
290 stats->Cha += value;
291 break;
292
293 case INTELLIGENCE:
294 stats->Int += value;
295 break;
296
297 default:
298 LOG(llevError, "Invalid attribute in change_attr_value: %d\n", attr);
299 }
300 }
301
302 /**
303 * Gets the value of a stat.
304 *
305 * @param stats
306 * item from which to get stat.
307 * @param attr
308 * attribute to get.
309 * @return
310 * specified attribute, 0 if not found.
311 *
312 * @see set_attr_value().
313 */
314 int8_t get_attr_value(const living *stats, int attr) {
315 switch (attr) {
316 case STRENGTH:
317 return(stats->Str);
318
319 case DEXTERITY:
320 return(stats->Dex);
321
322 case CONSTITUTION:
323 return(stats->Con);
324
325 case WISDOM:
326 return(stats->Wis);
327
328 case CHARISMA:
329 return(stats->Cha);
330
331 case INTELLIGENCE:
332 return(stats->Int);
333
334 case POWER:
335 return(stats->Pow);
336 }
337 return 0;
338 }
339
340 /**
341 * Ensures that all stats (str/dex/con/wis/cha/int) are within the
342 * passed in range of min_stat and max_stat. Often, the caller
343 * will pass in MIN_STAT and MAX_STAT, but in case of force objects
344 * or temporary calculations, we want things outside the range
345 * (force objects may have negative stats, but we don't want them
346 * too negative)
347 *
348 * @param stats
349 * attributes to check.
350 * @param min_stat
351 * lowest the stat can be
352 * @param max_stat
353 * highest the stat can be
354 */
355 void check_stat_bounds(living *stats, int8_t min_stat, int8_t max_stat) {
356 int i, v;
357 for (i = 0; i < NUM_STATS; i++)
358 if ((v = get_attr_value(stats, i)) > max_stat)
359 set_attr_value(stats, i, max_stat);
360 else if (v < min_stat)
361 set_attr_value(stats, i, min_stat);
362 }
363
364
365 /**
366 * Rather than having a whole bunch of if (flag) draw.. else _draw,
367 * make this macro to clean those up. Not usuable outside change_abil
368 * function since some of the values passed to draw_ext_info are hardcoded.
369 */
370 #define DIFF_MSG(flag, subtype1, subtype2, msg1, msg2) \
371 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ATTRIBUTE, (flag > 0) ? subtype1 : subtype2, (flag > 0) ? msg1 : msg2);
372
373 /**
374 * Permanently alters an object's stats/flags based on another object.
375 * @return
376 * 1 if we sucessfully changed a stat, 0 if nothing was changed.
377 *
378 * @note
379 * flag is set to 1 if we are applying the object, -1 if we are removing
380 * the object.
381 *
382 * @note
383 * It is the calling functions responsibilty to check to see if the object
384 * can be applied or not.
385 * The main purpose of calling this function is the messages that are
386 * displayed - fix_object should really always be called after this when
387 * removing an object - that is because it is impossible to know if some object
388 * is the only source of an attacktype or spell attunement, so this function
389 * will clear the bits, but the player may still have some other object
390 * that gives them that ability.
391 *
392 * @todo
393 * check logic, and things like that. Is the call to fix_object always required?
394 */
395 int change_abil(object *op, object *tmp) {
396 int flag = QUERY_FLAG(tmp, FLAG_APPLIED) ? 1 : -1, i, j, success = 0;
397 object refop;
398 int potion_max = 0;
399
400 /* remember what object was like before it was changed. note that
401 * refop is a local copy of op only to be used for detecting changes
402 * found by fix_object. refop is not a real object
403 */
404 memcpy(&refop, op, sizeof(object));
405
406 if (op->type == PLAYER) {
407 if (tmp->type == POTION) {
408 potion_max = 1;
409 for (j = 0; j < NUM_STATS; j++) {
410 int nstat, ostat;
411
412 ostat = get_attr_value(&(op->contr->orig_stats), j);
413 i = get_attr_value(&(tmp->stats), j);
414
415 /* nstat is what the stat will be after use of the potion */
416 nstat = flag*i+ostat;
417
418 /* Do some bounds checking. While I don't think any
419 * potions do so right now, there is the potential for potions
420 * that adjust that stat by more than one point, so we need
421 * to allow for that.
422 */
423 if (nstat < 1 && i*flag < 0)
424 nstat = 1;
425 else if (nstat > 20+get_attr_value(&(op->arch->clone.stats), j)) {
426 nstat = 20+get_attr_value(&(op->arch->clone.stats), j);
427 }
428 if (nstat != ostat) {
429 set_attr_value(&(op->contr->orig_stats), j, nstat);
430 potion_max = 0;
431 } else if (i) {
432 /* potion is useless - player has already hit the natural maximum */
433 potion_max = 1;
434 }
435 }
436 /* This section of code ups the characters normal stats also. I am not
437 * sure if this is strictly necessary, being that fix_object probably
438 * recalculates this anyway.
439 */
440 for (j = 0; j < NUM_STATS; j++)
441 change_attr_value(&(op->stats), j, flag*get_attr_value(&(tmp->stats), j));
442 check_stat_bounds(&(op->stats), MIN_STAT, settings.max_stat);
443 } /* end of potion handling code */
444 }
445
446 /* reset attributes that fix_object doesn't reset since it doesn't search
447 * everything to set
448 */
449 if (flag == -1) {
450 op->attacktype &= ~tmp->attacktype;
451 op->path_attuned &= ~tmp->path_attuned;
452 op->path_repelled &= ~tmp->path_repelled;
453 op->path_denied &= ~tmp->path_denied;
454 /* Presuming here that creatures only have move_type,
455 * and not the other move_ fields.
456 */
457 op->move_type &= ~tmp->move_type;
458 }
459
460 /* call fix_object since op object could have whatever attribute due
461 * to multiple items. if fix_object always has to be called after
462 * change_ability then might as well call it from here
463 */
464 fix_object(op);
465
466 /* Fix player won't add the bows ability to the player, so don't
467 * print out message if this is a bow.
468 */
469 if (tmp->attacktype&AT_CONFUSION && tmp->type != BOW) {
470 success = 1;
471 DIFF_MSG(flag, MSG_TYPE_ATTRIBUTE_ATTACKTYPE_GAIN, MSG_TYPE_ATTRIBUTE_ATTACKTYPE_LOSS,
472 "Your hands begin to glow red.",
473 "Your hands stop glowing red.");
474 }
475 if (QUERY_FLAG(op, FLAG_LIFESAVE) != QUERY_FLAG(&refop, FLAG_LIFESAVE)) {
476 success = 1;
477 DIFF_MSG(flag, MSG_TYPE_ATTRIBUTE_PROTECTION_GAIN, MSG_TYPE_ATTRIBUTE_PROTECTION_LOSS,
478 "You feel very protected.",
479 "You don't feel protected anymore.");
480 }
481 if (QUERY_FLAG(op, FLAG_REFL_MISSILE) != QUERY_FLAG(&refop, FLAG_REFL_MISSILE)) {
482 success = 1;
483 DIFF_MSG(flag, MSG_TYPE_ATTRIBUTE_PROTECTION_GAIN, MSG_TYPE_ATTRIBUTE_PROTECTION_LOSS,
484 "A magic force shimmers around you.",
485 "The magic force fades away.");
486 }
487 if (QUERY_FLAG(op, FLAG_REFL_SPELL) != QUERY_FLAG(&refop, FLAG_REFL_SPELL)) {
488 success = 1;
489 DIFF_MSG(flag, MSG_TYPE_ATTRIBUTE_PROTECTION_GAIN, MSG_TYPE_ATTRIBUTE_PROTECTION_LOSS,
490 "You feel more safe now, somehow.",
491 "Suddenly you feel less safe, somehow.");
492 }
493 /* movement type has changed. We don't care about cases where
494 * user has multiple items giving the same type appled like we
495 * used to - that is more work than what we gain, plus messages
496 * can be misleading (a little higher could be miscontrued from
497 * from fly high)
498 */
499 if (tmp->move_type && op->move_type != refop.move_type) {
500 success = 1;
501
502 /* MOVE_FLY_HIGH trumps MOVE_FLY_LOW - changing your move_fly_low
503 * status doesn't make a difference if you are flying high
504 */
505 if (tmp->move_type&MOVE_FLY_LOW && !(op->move_type&MOVE_FLY_HIGH)) {
506 DIFF_MSG(flag, MSG_TYPE_ATTRIBUTE_MOVE, MSG_TYPE_ATTRIBUTE_MOVE,
507 "You start to float in the air!",
508 "You float down to the ground.");
509 }
510
511 if (tmp->move_type&MOVE_FLY_HIGH) {
512 /* double conditional - second case covers if you have move_fly_low -
513 * in that case, you don't actually land
514 */
515 DIFF_MSG(flag, MSG_TYPE_ATTRIBUTE_MOVE, MSG_TYPE_ATTRIBUTE_MOVE,
516 "You soar into the air!.",
517 (op->move_type&MOVE_FLY_LOW ? "You glide closer to the ground.":
518 "You float down to the ground."));
519 }
520 if (tmp->move_type&MOVE_SWIM)
521 DIFF_MSG(flag, MSG_TYPE_ATTRIBUTE_MOVE, MSG_TYPE_ATTRIBUTE_MOVE,
522 "You feel ready for a swim",
523 "You no longer feel like swimming");
524
525 /* Changing move status may mean you are affected by things you weren't before */
526 object_check_move_on(op, op);
527 }
528
529 /* becoming UNDEAD... a special treatment for this flag. Only those not
530 * originally undead may change their status
531 */
532 if (!QUERY_FLAG(&op->arch->clone, FLAG_UNDEAD))
533 if (QUERY_FLAG(op, FLAG_UNDEAD) != QUERY_FLAG(&refop, FLAG_UNDEAD)) {
534 success = 1;
535 if (flag > 0) {
536 if (op->race)
537 free_string(op->race);
538 op->race = add_string("undead");
539 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ATTRIBUTE, MSG_TYPE_ATTRIBUTE_RACE, "Your lifeforce drains away!");
540 } else {
541 if (op->race)
542 free_string(op->race);
543 if (op->arch->clone.race)
544 op->race = add_string(op->arch->clone.race);
545 else
546 op->race = NULL;
547 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ATTRIBUTE, MSG_TYPE_ATTRIBUTE_RACE, "Your lifeforce returns!");
548 }
549 }
550
551 if (QUERY_FLAG(op, FLAG_STEALTH) != QUERY_FLAG(&refop, FLAG_STEALTH)) {
552 success = 1;
553 DIFF_MSG(flag, MSG_TYPE_ATTRIBUTE_GOOD_EFFECT_START, MSG_TYPE_ATTRIBUTE_GOOD_EFFECT_END,
554 "You walk more quietly.",
555 "You walk more noisily.");
556 }
557 if (QUERY_FLAG(op, FLAG_MAKE_INVIS) != QUERY_FLAG(&refop, FLAG_MAKE_INVIS)) {
558 success = 1;
559 DIFF_MSG(flag, MSG_TYPE_ATTRIBUTE_GOOD_EFFECT_START, MSG_TYPE_ATTRIBUTE_GOOD_EFFECT_END,
560 "You become transparent.",
561 "You can see yourself.");
562 }
563 /* blinded you can tell if more blinded since blinded player has minimal
564 * vision
565 */
566 if (QUERY_FLAG(tmp, FLAG_BLIND)) {
567 success = 1;
568 if (flag > 0) {
569 if (QUERY_FLAG(op, FLAG_WIZ))
570 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ATTRIBUTE, MSG_TYPE_ATTRIBUTE_BAD_EFFECT_START, "Your mortal self is blinded.");
571 else {
572 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ATTRIBUTE, MSG_TYPE_ATTRIBUTE_BAD_EFFECT_START, "You are blinded.");
573 SET_FLAG(op, FLAG_BLIND);
574 if (op->type == PLAYER)
575 op->contr->do_los = 1;
576 }
577 } else {
578 if (QUERY_FLAG(op, FLAG_WIZ))
579 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ATTRIBUTE, MSG_TYPE_ATTRIBUTE_BAD_EFFECT_END, "Your mortal self can now see again.");
580 else {
581 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ATTRIBUTE, MSG_TYPE_ATTRIBUTE_BAD_EFFECT_END, "Your vision returns.");
582 CLEAR_FLAG(op, FLAG_BLIND);
583 if (op->type == PLAYER)
584 op->contr->do_los = 1;
585 }
586 }
587 }
588
589 if (QUERY_FLAG(op, FLAG_SEE_IN_DARK) != QUERY_FLAG(&refop, FLAG_SEE_IN_DARK)) {
590 success = 1;
591 if (op->type == PLAYER)
592 op->contr->do_los = 1;
593 DIFF_MSG(flag, MSG_TYPE_ATTRIBUTE_GOOD_EFFECT_START, MSG_TYPE_ATTRIBUTE_GOOD_EFFECT_END,
594 "Your vision is better in the dark.",
595 "You see less well in the dark.");
596 }
597
598 if (QUERY_FLAG(op, FLAG_XRAYS) != QUERY_FLAG(&refop, FLAG_XRAYS)) {
599 success = 1;
600 if (flag > 0) {
601 if (QUERY_FLAG(op, FLAG_WIZ))
602 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ATTRIBUTE, MSG_TYPE_ATTRIBUTE_GOOD_EFFECT_START, "Your vision becomes a little clearer.");
603 else {
604 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ATTRIBUTE, MSG_TYPE_ATTRIBUTE_GOOD_EFFECT_START, "Everything becomes transparent.");
605 if (op->type == PLAYER)
606 op->contr->do_los = 1;
607 }
608 } else {
609 if (QUERY_FLAG(op, FLAG_WIZ))
610 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ATTRIBUTE, MSG_TYPE_ATTRIBUTE_GOOD_EFFECT_END, "Your vision becomes a bit out of focus.");
611 else {
612 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ATTRIBUTE, MSG_TYPE_ATTRIBUTE_GOOD_EFFECT_END, "Everything suddenly looks very solid.");
613 if (op->type == PLAYER)
614 op->contr->do_los = 1;
615 }
616 }
617 }
618
619 if (tmp->stats.luck) {
620 success = 1;
621 DIFF_MSG(flag*tmp->stats.luck, MSG_TYPE_ATTRIBUTE_STAT_GAIN, MSG_TYPE_ATTRIBUTE_STAT_LOSS,
622 "You feel more lucky.",
623 "You feel less lucky.");
624 }
625
626 if (tmp->stats.hp && op->type == PLAYER) {
627 success = 1;
628 DIFF_MSG(flag*tmp->stats.hp, MSG_TYPE_ATTRIBUTE_STAT_GAIN, MSG_TYPE_ATTRIBUTE_STAT_LOSS,
629 "You feel much more healthy!",
630 "You feel much less healthy!");
631 }
632
633 if (tmp->stats.sp && op->type == PLAYER && tmp->type != SKILL) {
634 success = 1;
635 DIFF_MSG(flag*tmp->stats.sp, MSG_TYPE_ATTRIBUTE_STAT_GAIN, MSG_TYPE_ATTRIBUTE_STAT_LOSS,
636 "You feel one with the powers of magic!",
637 "You suddenly feel very mundane.");
638 }
639
640 /* for the future when artifacts set this -b.t. */
641 if (tmp->stats.grace && op->type == PLAYER) {
642 success = 1;
643 DIFF_MSG(flag*tmp->stats.grace, MSG_TYPE_ATTRIBUTE_STAT_GAIN, MSG_TYPE_ATTRIBUTE_STAT_LOSS,
644 "You feel closer to your god!",
645 "You suddenly feel less holy.");
646 }
647
648 if (tmp->stats.wc && op->type == PLAYER) {
649 success = 1;
650 DIFF_MSG(flag*tmp->stats.wc, MSG_TYPE_ATTRIBUTE_STAT_GAIN, MSG_TYPE_ATTRIBUTE_STAT_LOSS,
651 "You feel more confident in combat.",
652 "You feel less confident in combat.");
653 }
654
655 if (tmp->stats.ac && op->type == PLAYER) {
656 success = 1;
657 DIFF_MSG(flag*tmp->stats.ac, MSG_TYPE_ATTRIBUTE_STAT_GAIN, MSG_TYPE_ATTRIBUTE_STAT_LOSS,
658 "You feel more confident in your dodging skills.",
659 "You feel less confident in your dodging skills.");
660 }
661
662 if (tmp->stats.exp && tmp->type != SKILL && op->type == PLAYER) {
663 success = 1;
664 DIFF_MSG(flag*tmp->stats.exp, MSG_TYPE_ATTRIBUTE_STAT_GAIN, MSG_TYPE_ATTRIBUTE_STAT_LOSS,
665 "You feel like you're moving faster.",
666 "You feel like you're moving more slowly.");
667 }
668
669 if (tmp->stats.food && op->type == PLAYER) {
670 success = 1;
671 DIFF_MSG(flag*tmp->stats.food, MSG_TYPE_ATTRIBUTE_STAT_GAIN, MSG_TYPE_ATTRIBUTE_STAT_LOSS,
672 "You feel your digestion slowing down.",
673 "You feel your digestion speeding up.");
674 }
675
676 /* Messages for changed resistance */
677 for (i = 0; i < NROFATTACKS; i++) {
678 if (i == ATNR_PHYSICAL)
679 continue; /* Don't display about armour */
680
681 if (op->resist[i] != refop.resist[i]) {
682 success = 1;
683 if (op->resist[i] > refop.resist[i])
684 draw_ext_info_format(NDI_UNIQUE|NDI_BLUE, 0, op, MSG_TYPE_ATTRIBUTE, MSG_TYPE_ATTRIBUTE_PROTECTION_GAIN,
685 "Your resistance to %s rises to %d%%.",
686 change_resist_msg[i], op->resist[i]);
687 else
688 draw_ext_info_format(NDI_UNIQUE|NDI_BLUE, 0, op, MSG_TYPE_ATTRIBUTE, MSG_TYPE_ATTRIBUTE_PROTECTION_LOSS,
689 "Your resistance to %s drops to %d%%.",
690 change_resist_msg[i], op->resist[i]);
691 }
692 }
693
694 if (!potion_max) {
695 for (j = 0; j < NUM_STATS; j++) {
696 if ((i = get_attr_value(&(tmp->stats), j)) != 0) {
697 success = 1;
698 DIFF_MSG(i*flag, MSG_TYPE_ATTRIBUTE_STAT_GAIN, MSG_TYPE_ATTRIBUTE_STAT_LOSS, gain_msg[j], lose_msg[j]);
699 }
700 }
701 }
702 return success;
703 }
704
705 /**
706 * Drains a random stat from op.
707 * Stat draining by Vick 930307
708 * (Feeling evil, I made it work as well now. -Frank 8)
709 *
710 * @param op
711 * object to drain.
712 */
713 void drain_stat(object *op) {
714 drain_specific_stat(op, RANDOM()%NUM_STATS);
715 }
716
717 /**
718 * Drain a specified stat from op.
719 *
720 * @param op
721 * victim to drain.
722 * @param deplete_stats
723 * statistic to drain.
724 */
725 void drain_specific_stat(object *op, int deplete_stats) {
726 object *tmp;
727 archetype *at;
728
729 at = find_archetype(ARCH_DEPLETION);
730 if (!at) {
731 return;
732 } else {
733 tmp = arch_present_in_ob(at, op);
734 if (!tmp) {
735 tmp = arch_to_object(at);
736 tmp = object_insert_in_ob(tmp, op);
737 SET_FLAG(tmp, FLAG_APPLIED);
738 }
739 }
740
741 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ATTRIBUTE, MSG_TYPE_ATTRIBUTE_STAT_LOSS, drain_msg[deplete_stats]);
742 change_attr_value(&tmp->stats, deplete_stats, -1);
743 fix_object(op);
744 }
745
746 /**
747 * Remove depletion from op, if present, and warn player of such restorations.
748 * @param op who to remove depletion from.
749 * @param level maximum depletion level to remove, if -1 no maximum.
750 * @return 0 if no depletion (or with no drained statistics) was present or level was insufficient, 1 if something was removed.
751 */
752 int remove_depletion(object *op, int level) {
753 object *depl;
754 archetype *at;
755 int i, count = 0;
756
757 if ((at = find_archetype(ARCH_DEPLETION)) == NULL) {
758 return 0;
759 }
760
761 depl = arch_present_in_ob(at, op);
762
763 if (depl == NULL)
764 return 0;
765
766 if (level != -1 && level < op->level)
767 return 0;
768
769 for (i = 0; i < NUM_STATS; i++) {
770 if (get_attr_value(&depl->stats, i)) {
771 count++;
772 draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ATTRIBUTE,
773 MSG_TYPE_ATTRIBUTE_BAD_EFFECT_END, restore_msg[i]);
774 }
775 }
776
777 object_remove(depl);
778 object_free_drop_inventory(depl);
779 fix_object(op);
780
781 return (count == 0) ? 0 : 1;
782 }
783
784 /**
785 * Alter the object's luck.
786 *
787 * @param op
788 * object to altar.
789 * @param value
790 * delta to apply. 0 indicates timeout (moves luck towards 0), otherwise change the luck of the object
791 * via an applied bad_luck object in inventory.
792 */
793 void change_luck(object *op, int value) {
794 object *tmp;
795 archetype *at;
796 int new_luck;
797
798 at = find_archetype("luck");
799 if (!at)
800 ;
801 else {
802 tmp = arch_present_in_ob(at, op);
803 if (!tmp) {
804 if (!value)
805 return;
806 tmp = arch_to_object(at);
807 tmp = object_insert_in_ob(tmp, op);
808 SET_FLAG(tmp, FLAG_APPLIED);
809 }
810 if (value) {
811 /* Limit the luck value of the bad luck object to +/-100. This
812 * (arbitrary) value prevents overflows (both in the bad luck object and
813 * in op itself).
814 */
815 new_luck = tmp->stats.luck+value;
816 if (new_luck >= -100 && new_luck <= 100) {
817 op->stats.luck += value;
818 tmp->stats.luck = new_luck;
819 }
820 } else {
821 if (!tmp->stats.luck) {
822 return;
823 }
824 /* Randomly change the players luck. Basically, we move it
825 * back neutral (if greater>0, subtract, otherwise add)
826 */
827 if (RANDOM()%(FABS(tmp->stats.luck)) >= RANDOM()%30) {
828 int diff = tmp->stats.luck > 0 ? -1 : 1;
829 op->stats.luck += diff;
830 tmp->stats.luck += diff;
831 }
832 }
833 }
834 }
835
836 /**
837 * Subtracts stat-bonuses given by the class which the player has chosen.
838 *
839 * @param op
840 * object which must be a player with contr not NULL.
841 */
842 void remove_statbonus(object *op) {
843 op->stats.Str -= op->arch->clone.stats.Str;
844 op->stats.Dex -= op->arch->clone.stats.Dex;
845 op->stats.Con -= op->arch->clone.stats.Con;
846 op->stats.Wis -= op->arch->clone.stats.Wis;
847 op->stats.Pow -= op->arch->clone.stats.Pow;
848 op->stats.Cha -= op->arch->clone.stats.Cha;
849 op->stats.Int -= op->arch->clone.stats.Int;
850 op->contr->orig_stats.Str -= op->arch->clone.stats.Str;
851 op->contr->orig_stats.Dex -= op->arch->clone.stats.Dex;
852 op->contr->orig_stats.Con -= op->arch->clone.stats.Con;
853 op->contr->orig_stats.Wis -= op->arch->clone.stats.Wis;
854 op->contr->orig_stats.Pow -= op->arch->clone.stats.Pow;
855 op->contr->orig_stats.Cha -= op->arch->clone.stats.Cha;
856 op->contr->orig_stats.Int -= op->arch->clone.stats.Int;
857 }
858
859 /**
860 * Adds stat-bonuses given by the class which the player has chosen.
861 *
862 * @param op
863 * object which must be a player with contr not NULL.
864 */
865 void add_statbonus(object *op) {
866 op->stats.Str += op->arch->clone.stats.Str;
867 op->stats.Dex += op->arch->clone.stats.Dex;
868 op->stats.Con += op->arch->clone.stats.Con;
869 op->stats.Wis += op->arch->clone.stats.Wis;
870 op->stats.Pow += op->arch->clone.stats.Pow;
871 op->stats.Cha += op->arch->clone.stats.Cha;
872 op->stats.Int += op->arch->clone.stats.Int;
873 op->contr->orig_stats.Str += op->arch->clone.stats.Str;
874 op->contr->orig_stats.Dex += op->arch->clone.stats.Dex;
875 op->contr->orig_stats.Con += op->arch->clone.stats.Con;
876 op->contr->orig_stats.Wis += op->arch->clone.stats.Wis;
877 op->contr->orig_stats.Pow += op->arch->clone.stats.Pow;
878 op->contr->orig_stats.Cha += op->arch->clone.stats.Cha;
879 op->contr->orig_stats.Int += op->arch->clone.stats.Int;
880 }
881
882 /**
883 * Complement to fix_object() for player.
884 * Figure out the players sp/mana/hp totals. Also do encumberance and various speeds.
885 * @param op object being fixed, must not be NULL.
886 * @param ac current ac, may be adjusted.
887 * @param wc current wc, may be adjusted.
888 * @param grace_obj praying skill, can be NULL.
889 * @param mana_obj spell-casting skill, can be NULL.
890 * @param wc_obj applied combat skill, can be NULL.
891 * @param weapon_speed current weapon's speed.
892 * @param added_speed speed bonus from items.
893 */
894 static void fix_player(object *op, int *ac, int *wc, const object *grace_obj, const object *mana_obj, const object *wc_obj, int weapon_speed, float added_speed)
895 {
896 int pl_level, i;
897 float character_load = 0.0, maxhp, tmpf;
898
899 if (op->type != PLAYER)
900 return;
901
902 check_stat_bounds(&(op->stats), MIN_STAT, settings.max_stat);
903 pl_level = op->level;
904
905 if (pl_level < 1)
906 pl_level = 1; /* safety, we should always get 1 levels worth of hp! */
907
908 /*
909 * We store maxhp as a float to hold any fractional hp bonuses,
910 * (eg, 2.5 con bonus). While it may seem simpler to just
911 * do a get_con_bonus() * min(level,10), there is also a 1 hp/level
912 * minimum (including bonus), se we have to do the logic on a
913 * level basis.
914 */
915 maxhp = 0.0;
916 for (i = 1, op->stats.maxhp = 0; i <= pl_level && i <= 10; i++) {
917
918 tmpf = op->contr->levhp[i]+get_con_bonus(op->stats.Con);
919
920 /* always get at least 1 hp/level */
921 if (tmpf < 1.0) tmpf = 1.0;
922
923 maxhp += tmpf;
924 }
925
926 /* Add 0.5 so that this rounds normally - the cast just drops the
927 * fraction, so 1.5 becomes 1.
928 */
929 op->stats.maxhp = (int)(maxhp + 0.5);
930 if (op->level > 10)
931 op->stats.maxhp += 2 * (op->level - 10);
932
933 op->stats.maxhp += op->arch->clone.stats.maxhp;
934
935 if (op->stats.hp > op->stats.maxhp)
936 op->stats.hp = op->stats.maxhp;
937
938 /* Sp gain is controlled by the level of the player's
939 * relevant experience object (mana_obj, see above) */
940
941 /* set maxsp */
942 if (!mana_obj || !mana_obj->level) {
943 op->stats.maxsp = 1;
944 } else {
945 float sp_tmp = 0.0, mana_bonus;
946 int mana_lvl_max;
947
948 mana_lvl_max = (mana_obj->level >10 ? 10: mana_obj->level);
949 mana_bonus = (2.0*get_sp_bonus(op->stats.Pow)+get_sp_bonus(op->stats.Int)) / 3.0;
950
951 for (i = 1; i <= mana_lvl_max; i++) {
952 float stmp;
953
954 stmp = op->contr->levsp[i] + mana_bonus;
955
956 /* Got some extra bonus at first level */
957 if (i == 1) stmp += mana_bonus;
958
959 if (stmp < 1.0)
960 stmp = 1.0;
961
962 sp_tmp += stmp;
963 }
964
965 op->stats.maxsp = (int)sp_tmp+op->arch->clone.stats.maxsp;
966
967 if (mana_obj->level > 10)
968 op->stats.maxsp += 2 * (mana_obj->level - 10);
969 }
970
971 /* Characters can get their sp supercharged via rune of transferrance */
972 if (op->stats.sp > op->stats.maxsp*2)
973 op->stats.sp = op->stats.maxsp*2;
974
975 /* set maxgrace, notice 3-4 lines below it depends on both Wis and Pow */
976 if (!grace_obj || !grace_obj->level) {
977 op->stats.maxgrace = 1;
978 } else {
979 /* store grace in a float - this way, the divisions below don't create
980 * big jumps when you go from level to level - with int's, it then
981 * becomes big jumps when the sums of the bonuses jump to the next
982 * step of 8 - with floats, even fractional ones are useful.
983 */
984 float sp_tmp = 0.0, grace_bonus;
985
986 grace_bonus = (get_grace_bonus(op->stats.Pow)+2.0*get_grace_bonus(op->stats.Wis)) / 3.0;
987
988 for (i = 1; i <= grace_obj->level && i <= 10; i++) {
989 float grace_tmp = op->contr->levgrace[i] + grace_bonus;
990
991 /* Got some extra bonus at first level */
992 if (i == 1)
993 grace_tmp += grace_bonus;
994
995 if (grace_tmp < 1.0)
996 grace_tmp = 1.0;
997 sp_tmp += grace_tmp;
998 }
999 op->stats.maxgrace = (int)sp_tmp+op->arch->clone.stats.maxgrace;
1000
1001 /* two grace points per level after 11 */
1002 if (grace_obj->level > 10)
1003 op->stats.maxgrace += 2 * (grace_obj->level - 10);
1004 }
1005 /* No limit on grace vs maxgrace */
1006
1007 if (op->contr->braced) {
1008 (*ac) += 2;
1009 (*wc) += 4;
1010 } else
1011 (*ac) -= get_dex_bonus(op->stats.Dex);
1012
1013 /* In new exp/skills system, wc bonuses are related to
1014 * the players level in a relevant exp object (wc_obj)
1015 * not the general player level -b.t.
1016 * I changed this slightly so that wc bonuses are better
1017 * than before. This is to balance out the fact that
1018 * the player no longer gets a personal weapon w/ 1
1019 * improvement every level, now its fighterlevel/5. So
1020 * we give the player a bonus here in wc and dam
1021 * to make up for the change. Note that I left the
1022 * monster bonus the same as before. -b.t.
1023 */
1024
1025 if (wc_obj && wc_obj->level >= 1) {
1026 const char *wc_in = object_get_value(wc_obj, "wc_increase_rate");
1027 int wc_increase_rate;
1028
1029 wc_increase_rate = wc_in?atoi(wc_in):5;
1030 assert(wc_increase_rate != 0);
1031
1032 (*wc) -= get_thaco_bonus(op->stats.Str);
1033 (*wc) -= (wc_obj->level-1)/wc_increase_rate;
1034 op->stats.dam += (wc_obj->level-1)/4;
1035 } else {
1036 (*wc) -= (((op->level-1)/5)+get_thaco_bonus(op->stats.Str));
1037 }
1038 op->stats.dam += get_dam_bonus(op->stats.Str);
1039
1040 if (op->stats.dam < 1)
1041 op->stats.dam = 1;
1042
1043 op->speed = MAX_PLAYER_SPEED+get_speed_bonus(op->stats.Dex);
1044
1045 if (settings.search_items && op->contr->search_str[0])
1046 op->speed -= 0.25;
1047
1048 if (op->attacktype == 0)
1049 op->attacktype = op->arch->clone.attacktype;
1050
1051
1052 /* First block is for encumbrance of the player */
1053
1054 /* The check for FREE_PLAYER_LOAD_PERCENT < 1.0 is really a safety. One would
1055 * think that it should never be the case if that is set to 1.0, that carrying
1056 * would be above the limit. But it could be if character is weakened and
1057 * was otherwise at limit. Without that safety, could get divide by zeros.
1058 */
1059 if (op->carrying > (get_weight_limit(op->stats.Str)*FREE_PLAYER_LOAD_PERCENT)
1060 && (FREE_PLAYER_LOAD_PERCENT < 1.0)) {
1061 int extra_weight = op->carrying-get_weight_limit(op->stats.Str)*FREE_PLAYER_LOAD_PERCENT;
1062
1063 character_load = (float)extra_weight/(float)(get_weight_limit(op->stats.Str)*(1.0-FREE_PLAYER_LOAD_PERCENT));
1064
1065 /* character_load is used for weapon speed below, so sanitize value */
1066 if (character_load >= 1.0)
1067 character_load = 1.0;
1068
1069 /* If a character is fully loaded, they will always get cut down to min
1070 * speed no matter what their dex. Note that magic is below, so
1071 * still helps out.
1072 */
1073 if (op->speed > MAX_PLAYER_SPEED)
1074 op->speed -= character_load*(op->speed-MIN_PLAYER_SPEED);
1075 else
1076 op->speed -= character_load*(MAX_PLAYER_SPEED-MIN_PLAYER_SPEED);
1077 }
1078
1079 /* This block is for weapon speed */
1080 op->weapon_speed = BASE_WEAPON_SPEED+get_speed_bonus(op->stats.Dex)-weapon_speed/20.0+added_speed/10.0;
1081 if (wc_obj) {
1082 op->weapon_speed += 0.005*wc_obj->level;
1083 } else
1084 op->weapon_speed += 0.005*op->level;
1085
1086 /* character_load=1.0 means character is fully loaded, 0.0 is unloaded. Multiplying
1087 * by 0.2 for penalty is purely arbitrary, but slows things down without completely
1088 * stopping them.
1089 */
1090 op->weapon_speed -= character_load*0.2;
1091
1092 if (op->weapon_speed < 0.05)
1093 op->weapon_speed = 0.05;
1094
1095 /* It is quite possible that a player's spell costing might have changed,
1096 * so we will check that now.
1097 */
1098 esrv_update_spells(op->contr);
1099 }
1100 /**
1101 * Updates all abilities given by applied objects in the inventory
1102 * of the given object.
1103 *
1104 * This functions starts from base values (archetype or player object)
1105 * and then adjusts them according to what the player/monster has equipped.
1106 *
1107 * Note that a player always has stats reset to their initial value.
1108 *
1109 * July 95 - inserted stuff to handle new skills/exp system - b.t.
1110 * spell system split, grace points now added to system --peterm
1111 *
1112 * November 2006: max armor speed is always taken into account, no exception.
1113 *
1114 * @param op
1115 * object to reset.
1116 *
1117 * @todo
1118 * this function is too long, and should be cleaned / split.
1119 */
1120 void fix_object(object *op) {
1121 int i;
1122 float max = 9, added_speed = 0, speed_reduce_from_disease = 1;
1123 int weapon_speed = 0;
1124 int best_wc = 0, best_ac = 0, wc = 0, ac = 0;
1125 int prot[NROFATTACKS], vuln[NROFATTACKS], potion_resist[NROFATTACKS];
1126 const object *grace_obj = NULL, *mana_obj = NULL, *wc_obj = NULL;
1127
1128 /* First task is to clear all the values back to their original values */
1129 if (op->type == PLAYER) {
1130 for (i = 0; i < NUM_STATS; i++) {
1131 set_attr_value(&(op->stats), i, get_attr_value(&(op->contr->orig_stats), i));
1132 set_attr_value(&(op->contr->applied_stats), i, 0);
1133 }
1134 if (settings.spell_encumbrance == TRUE)
1135 op->contr->encumbrance = 0;
1136
1137 op->attacktype = 0;
1138 op->contr->digestion = 0;
1139 op->contr->gen_hp = 0;
1140 op->contr->gen_sp = 0;
1141 op->contr->gen_grace = 0;
1142 op->contr->gen_sp_armour = 10;
1143 op->contr->item_power = 0;
1144
1145 /* Don't clobber all the range_ values. range_golem otherwise
1146 * gets reset for no good reason, and we don't want to reset
1147 * range_magic (what spell is readied). These three below
1148 * well get filled in based on what the player has equipped.
1149 */
1150 op->contr->ranges[range_bow] = NULL;
1151 op->contr->ranges[range_misc] = NULL;
1152 op->contr->ranges[range_skill] = NULL;
1153 } /* If player */
1154 memcpy(op->body_used, op->body_info, sizeof(op->body_info));
1155
1156 if (op->slaying != NULL) {
1157 free_string(op->slaying);
1158 op->slaying = NULL;
1159 }
1160 if (!QUERY_FLAG(op, FLAG_WIZ)) {
1161 CLEAR_FLAG(op, FLAG_XRAYS);
1162 CLEAR_FLAG(op, FLAG_MAKE_INVIS);
1163 }
1164
1165 CLEAR_FLAG(op, FLAG_LIFESAVE);
1166 CLEAR_FLAG(op, FLAG_STEALTH);
1167 CLEAR_FLAG(op, FLAG_BLIND);
1168 CLEAR_FLAG(op, FLAG_CONFUSED);
1169 if (!QUERY_FLAG(&op->arch->clone, FLAG_REFL_SPELL))
1170 CLEAR_FLAG(op, FLAG_REFL_SPELL);
1171 if (!QUERY_FLAG(&op->arch->clone, FLAG_REFL_MISSILE))
1172 CLEAR_FLAG(op, FLAG_REFL_MISSILE);
1173 if (!QUERY_FLAG(&op->arch->clone, FLAG_UNDEAD))
1174 CLEAR_FLAG(op, FLAG_UNDEAD);
1175 if (!QUERY_FLAG(&op->arch->clone, FLAG_SEE_IN_DARK))
1176 CLEAR_FLAG(op, FLAG_SEE_IN_DARK);
1177 CLEAR_FLAG(op, FLAG_PROBE);
1178
1179 op->path_attuned = op->arch->clone.path_attuned;
1180 op->path_repelled = op->arch->clone.path_repelled;
1181 op->path_denied = op->arch->clone.path_denied;
1182 op->glow_radius = op->arch->clone.glow_radius;
1183 op->move_type = op->arch->clone.move_type;
1184 op->chosen_skill = NULL;
1185
1186 /* initializing resistances from the values in player/monster's
1187 * archetype clone
1188 */
1189 memcpy(&op->resist, &op->arch->clone.resist, sizeof(op->resist));
1190
1191 for (i = 0; i < NROFATTACKS; i++) {
1192 if (op->resist[i] > 0)
1193 prot[i] = op->resist[i], vuln[i] = 0;
1194 else
1195 vuln[i] = -(op->resist[i]), prot[i] = 0;
1196 potion_resist[i] = 0;
1197 }
1198
1199 wc = op->arch->clone.stats.wc;
1200 op->stats.dam = op->arch->clone.stats.dam;
1201
1202 /* for players which cannot use armour, they gain AC -1 per 3 levels,
1203 * plus a small amount of physical resist, those poor suckers. ;)
1204 * the fact that maxlevel is factored in could be considered sort of bogus -
1205 * we should probably give them some bonus and cap it off - otherwise,
1206 * basically, if a server updates its max level, these playes may find
1207 * that their protection from physical goes down
1208 */
1209 if (!QUERY_FLAG(op, FLAG_USE_ARMOUR) && op->type == PLAYER) {
1210 ac = MAX(-10, op->arch->clone.stats.ac-op->level/3);
1211 prot[ATNR_PHYSICAL] += ((100-prot[AT_PHYSICAL])*(80*op->level/settings.max_level))/100;
1212 } else
1213 ac = op->arch->clone.stats.ac;
1214
1215 op->stats.luck = op->arch->clone.stats.luck;
1216 op->speed = op->arch->clone.speed;
1217
1218 /* OK - we've reset most all the objects attributes to sane values.
1219 * now go through and make adjustments for what the player has equipped.
1220 */
1221
1222 FOR_INV_PREPARE(op, tmp) {
1223 /* See note in map.c:update_position about making this additive
1224 * since light sources are never applied, need to put check here.
1225 */
1226 if (tmp->glow_radius > op->glow_radius)
1227 op->glow_radius = tmp->glow_radius;
1228
1229 /* This happens because apply_potion calls change_abil with the potion
1230 * applied so we can tell the player what chagned. But change_abil
1231 * then calls this function.
1232 */
1233 if (QUERY_FLAG(tmp, FLAG_APPLIED) && tmp->type == POTION)
1234 continue;
1235
1236 /* For some things, we don't care what is equipped */
1237 if (tmp->type == SKILL) {
1238 /* Want to take the highest skill here. */
1239 if (IS_MANA_SKILL(tmp->subtype)) {
1240 if (!mana_obj)
1241 mana_obj = tmp;
1242 else if (tmp->level > mana_obj->level)
1243 mana_obj = tmp;
1244 }
1245 if (IS_GRACE_SKILL(tmp->subtype)) {
1246 if (!grace_obj)
1247 grace_obj = tmp;
1248 else if (tmp->level > grace_obj->level)
1249 grace_obj = tmp;
1250 }
1251 }
1252
1253 /* Container objects are not meant to adjust a players, but other applied
1254 * objects need to make adjustments.
1255 * This block should handle all player specific changes
1256 * The check for Praying is a bit of a hack - god given bonuses are put
1257 * in the praying skill, and the player should always get those.
1258 * It also means we need to put in additional checks for applied below,
1259 * because the skill shouldn't count against body positions being used
1260 * up, etc.
1261 */
1262 if ((QUERY_FLAG(tmp, FLAG_APPLIED) && tmp->type != CONTAINER && tmp->type != CLOSE_CON)
1263 || (tmp->type == SKILL && tmp->subtype == SK_PRAYING)) {
1264 if (op->type == PLAYER) {
1265 if (tmp->type == BOW)
1266 op->contr->ranges[range_bow] = tmp;
1267
1268 if (tmp->type == WAND || tmp->type == ROD)
1269 op->contr->ranges[range_misc] = tmp;
1270
1271 for (i = 0; i < NUM_STATS; i++) {
1272 int8_t value;
1273
1274 value = get_attr_value(&(tmp->stats), i);
1275 change_attr_value(&(op->stats), i, value);
1276 if (strcmp(tmp->arch->clone.name, ARCH_DEPLETION) != 0)
1277 change_attr_value(&(op->contr->applied_stats), i, value);
1278 }
1279
1280 /* For this temporary calculation, allow wider range of stat - if we have
1281 * having that gives +5 and different object that gives -5 and stat
1282 * is maxed, we don't want different results based on order of
1283 * inventory.
1284 */
1285 check_stat_bounds(&(tmp->stats), -settings.max_stat, 2*settings.max_stat);
1286
1287 /* these are the items that currently can change digestion, regeneration,
1288 * spell point recovery and mana point recovery. Seems sort of an arbitary
1289 * list, but other items store other info into stats array.
1290 */
1291 switch (tmp->type)
1292 {
1293 case WEAPON:
1294 case ARMOUR:
1295 case HELMET:
1296 case SHIELD:
1297 case RING:
1298 case BOOTS:
1299 case GLOVES:
1300 case AMULET:
1301 case GIRDLE:
1302 case BRACERS:
1303 case CLOAK:
1304 case DISEASE:
1305 case FORCE:
1306 case SKILL:
1307 op->contr->digestion += tmp->stats.food;
1308 op->contr->gen_hp += tmp->stats.hp;
1309 op->contr->gen_sp += tmp->stats.sp;
1310 op->contr->gen_grace += tmp->stats.grace;
1311 op->contr->gen_sp_armour += tmp->gen_sp_armour;
1312 /*FALLTHROUGH*/
1313
1314 /* Bow and skill utils need to update item_power specifically.
1315 * This should fix bug #648
1316 * Daniel Hawkins 2017-08-09
1317 */
1318 case BOW:
1319 case SKILL_TOOL:
1320 op->contr->item_power += tmp->item_power;
1321 break;
1322 }
1323 } /* if this is a player */
1324
1325 /* Update slots used for items */
1326 if (QUERY_FLAG(tmp, FLAG_APPLIED)) {
1327 for (i = 0; i < NUM_BODY_LOCATIONS; i++)
1328 op->body_used[i] += tmp->body_info[i];
1329 }
1330
1331 if (tmp->type == SYMPTOM && tmp->last_sp) {
1332 /* Should take the worst disease of the bunch */
1333 if (((float)tmp->last_sp/100.0) < speed_reduce_from_disease)
1334 speed_reduce_from_disease = (float)tmp->last_sp/100.0;
1335 }
1336
1337 /* Pos. and neg. protections are counted seperate (-> pro/vuln).
1338 * (Negative protections are calculated extactly like positive.)
1339 * Resistance from potions are treated special as well. If there's
1340 * more than one potion-effect, the bigger prot.-value is taken.
1341 */
1342 if (tmp->type != POTION) {
1343 for (i = 0; i < NROFATTACKS; i++) {
1344 /* Potential for cursed potions, in which case we just can use
1345 * a straight MAX, as potion_resist is initialized to zero.
1346 */
1347 if (tmp->type == POTION_RESIST_EFFECT) {
1348 if (potion_resist[i])
1349 potion_resist[i] = MAX(potion_resist[i], tmp->resist[i]);
1350 else
1351 potion_resist[i] = tmp->resist[i];
1352 } else if (tmp->resist[i] > 0)
1353 prot[i] += ((100-prot[i])*tmp->resist[i])/100;
1354 else if (tmp->resist[i] < 0)
1355 vuln[i] += ((100-vuln[i])*(-tmp->resist[i]))/100;
1356 }
1357 }
1358
1359 /* There may be other things that should not adjust the attacktype */
1360 if (tmp->type != BOW && tmp->type != SYMPTOM)
1361 op->attacktype |= tmp->attacktype;
1362
1363 op->path_attuned |= tmp->path_attuned;
1364 op->path_repelled |= tmp->path_repelled;
1365 op->path_denied |= tmp->path_denied;
1366 op->stats.luck += tmp->stats.luck;
1367 op->move_type |= tmp->move_type;
1368
1369 if (QUERY_FLAG(tmp, FLAG_LIFESAVE))
1370 SET_FLAG(op, FLAG_LIFESAVE);
1371 if (QUERY_FLAG(tmp, FLAG_REFL_SPELL))
1372 SET_FLAG(op, FLAG_REFL_SPELL);
1373 if (QUERY_FLAG(tmp, FLAG_REFL_MISSILE))
1374 SET_FLAG(op, FLAG_REFL_MISSILE);
1375 if (QUERY_FLAG(tmp, FLAG_STEALTH))
1376 SET_FLAG(op, FLAG_STEALTH);
1377 if (QUERY_FLAG(tmp, FLAG_XRAYS))
1378 SET_FLAG(op, FLAG_XRAYS);
1379 if (QUERY_FLAG(tmp, FLAG_BLIND))
1380 SET_FLAG(op, FLAG_BLIND);
1381 if (QUERY_FLAG(tmp, FLAG_SEE_IN_DARK))
1382 SET_FLAG(op, FLAG_SEE_IN_DARK);
1383 if (QUERY_FLAG(tmp, FLAG_PROBE))
1384 SET_FLAG(op, FLAG_PROBE);
1385
1386 // Items can make the wielder confused.
1387 if (QUERY_FLAG(tmp, FLAG_CONFUSED))
1388 SET_FLAG(op, FLAG_CONFUSED);
1389
1390 if (QUERY_FLAG(tmp, FLAG_UNDEAD) && !QUERY_FLAG(&op->arch->clone, FLAG_UNDEAD))
1391 SET_FLAG(op, FLAG_UNDEAD);
1392
1393 if (QUERY_FLAG(tmp, FLAG_MAKE_INVIS)) {
1394 SET_FLAG(op, FLAG_MAKE_INVIS);
1395 op->invisible = 1;
1396 }
1397
1398 if (tmp->stats.exp && tmp->type != SKILL) {
1399 added_speed += (float)tmp->stats.exp/3.0;
1400 }
1401
1402 switch (tmp->type) {
1403 /* skills modifying the character -b.t. */
1404 /* for all skills and skill granting objects */
1405 case SKILL:
1406 if (!QUERY_FLAG(tmp, FLAG_APPLIED))
1407 break;
1408
1409 if (IS_COMBAT_SKILL(tmp->subtype))
1410 wc_obj = tmp;
1411
1412 if (op->chosen_skill) {
1413 LOG(llevDebug, "fix_object, op %s has multiple skills applied\n", op->name);
1414 }
1415 op->chosen_skill = tmp;
1416 if (tmp->stats.dam > 0) { /* skill is a 'weapon' */
1417 if (!QUERY_FLAG(op, FLAG_READY_WEAPON))
1418 weapon_speed = (int)WEAPON_SPEED(tmp);
1419 if (weapon_speed < 0)
1420 weapon_speed = 0;
1421 op->stats.dam += tmp->stats.dam*(1+(op->chosen_skill->level/9));
1422 op->stats.dam += tmp->magic;
1423 }
1424 if (tmp->stats.wc)
1425 wc -= (tmp->stats.wc+tmp->magic);
1426
1427 if (tmp->slaying != NULL) {
1428 if (op->slaying != NULL)
1429 free_string(op->slaying);
1430 add_refcount(op->slaying = tmp->slaying);
1431 }
1432
1433 if (tmp->stats.ac)
1434 ac -= (tmp->stats.ac+tmp->magic);
1435 if (settings.spell_encumbrance == TRUE && op->type == PLAYER)
1436 op->contr->encumbrance += (int)3*tmp->weight/1000;
1437 if (op->type == PLAYER)
1438 op->contr->ranges[range_skill] = op;
1439 break;
1440
1441 case SKILL_TOOL:
1442 if (op->chosen_skill) {
1443 LOG(llevDebug, "fix_object, op %s has multiple skills applied\n", op->name);
1444 }
1445 op->chosen_skill = tmp;
1446 if (op->type == PLAYER)
1447 op->contr->ranges[range_skill] = op;
1448 break;
1449
1450 case SHIELD:
1451 if (settings.spell_encumbrance == TRUE && op->type == PLAYER)
1452 op->contr->encumbrance += (int)tmp->weight/2000;
1453 /* fall through */
1454 case RING:
1455 case AMULET:
1456 case GIRDLE:
1457 case HELMET:
1458 case BOOTS:
1459 case GLOVES:
1460 case CLOAK:
1461 if (tmp->stats.wc)
1462 wc -= tmp->stats.wc;
1463 if (tmp->stats.dam)
1464 op->stats.dam += (tmp->stats.dam+tmp->magic);
1465 if (tmp->stats.ac)
1466 ac -= (tmp->stats.ac+tmp->magic);
1467 break;
1468
1469 case WEAPON:
1470 wc -= tmp->stats.wc;
1471 if (tmp->stats.ac && tmp->stats.ac+tmp->magic > 0)
1472 ac -= tmp->stats.ac+tmp->magic;
1473 op->stats.dam += (tmp->stats.dam+tmp->magic);
1474 weapon_speed = ((int)WEAPON_SPEED(tmp)*2-tmp->magic)/2;
1475 if (weapon_speed < 0)
1476 weapon_speed = 0;
1477 if (tmp->slaying != NULL) {
1478 if (op->slaying != NULL)
1479 free_string(op->slaying);
1480 add_refcount(op->slaying = tmp->slaying);
1481 }
1482 /* If there is desire that two handed weapons should do
1483 * extra strength damage, this is where the code should
1484 * go.
1485 */
1486 op->current_weapon = tmp;
1487 if (settings.spell_encumbrance == TRUE && op->type == PLAYER)
1488 op->contr->encumbrance += (int)3*tmp->weight/1000;
1489 break;
1490
1491 case ARMOUR: /* Only the best of these three are used: */
1492 if (settings.spell_encumbrance == TRUE && op->type == PLAYER)
1493 op->contr->encumbrance += (int)tmp->weight/1000;
1494
1495 /* ARMOUR falls through to here */
1496 /* fall through */
1497 case BRACERS:
1498 case FORCE:
1499 // Code simplification to reduce branching -- we don't need to sub/add ac and wc all the time.
1500 // Daniel Hawkins 2018-05-28
1501 if (tmp->stats.wc) {
1502 // Since we are alreay here, make sure wc stacking also occurs for serpentman players.
1503 if (tmp->type == BRACERS && op->type == PLAYER && op->arch->name && strcmp(op->arch->name, "serpentman_player") == 0)
1504 {
1505 // Apply ac from bracers directly.
1506 // This also grants the side effect that armor and bracer ac are separate for serpentmen,
1507 // But that should be better than the extra bracers being mostly useless.
1508 wc -= tmp->stats.wc;
1509 }
1510 else if (best_wc < tmp->stats.wc) {
1511 wc += best_wc;
1512 best_wc = tmp->stats.wc;
1513 wc -= tmp->stats.wc;
1514 }
1515 }
1516 if (tmp->stats.ac) {
1517 /*
1518 * If we have a serpentman player, then we do bracers differently
1519 * to allow for both bracers they equip to apply to ac, instead of only the best.
1520 *
1521 * Daniel Hawkins 2018-05-28
1522 */
1523 if (tmp->type == BRACERS && op->type == PLAYER && op->arch->name && strcmp(op->arch->name, "serpentman_player") == 0)
1524 {
1525 // Apply ac from bracers directly.
1526 // This also grants the side effect that armor and bracer ac are separate for serpentmen,
1527 // But that should be better than the extra bracers being mostly useless.
1528 ac -= tmp->stats.ac+tmp->magic;
1529 }
1530 else if (best_ac < tmp->stats.ac+tmp->magic) {
1531 ac += best_ac; /* Remove last bonus */
1532 best_ac = tmp->stats.ac+tmp->magic;
1533 ac -= (tmp->stats.ac+tmp->magic);
1534 }
1535 }
1536 if (tmp->stats.dam && tmp->type == BRACERS)
1537 op->stats.dam += (tmp->stats.dam+tmp->magic);
1538 if (ARMOUR_SPEED(tmp) && ARMOUR_SPEED(tmp)/10.0 < max)
1539 max = ARMOUR_SPEED(tmp)/10.0;
1540 break;
1541 } /* switch tmp->type */
1542 } /* item is equipped */
1543 } FOR_INV_FINISH(); /* for loop of items */
1544
1545 /* We've gone through all the objects the player has equipped. For many things, we
1546 * have generated intermediate values which we now need to assign.
1547 */
1548
1549 /* 'total resistance = total protections - total vulnerabilities'.
1550 * If there is an uncursed potion in effect, granting more protection
1551 * than that, we take: 'total resistance = resistance from potion'.
1552 * If there is a cursed (and no uncursed) potion in effect, we take
1553 * 'total resistance = vulnerability from cursed potion'.
1554 */
1555 for (i = 0; i < NROFATTACKS; i++) {
1556 op->resist[i] = prot[i]-vuln[i];
1557 if (potion_resist[i]
1558 && ((potion_resist[i] > op->resist[i]) || (potion_resist[i] < 0)))
1559 op->resist[i] = potion_resist[i];
1560 }
1561
1562 fix_player(op, &ac, &wc, grace_obj, mana_obj, wc_obj, weapon_speed, added_speed);
1563
1564 op->speed = op->speed*speed_reduce_from_disease;
1565
1566 /* Max is determined by armour */
1567 if (op->speed > max)
1568 op->speed = max;
1569
1570 op->speed += added_speed/10.0;
1571
1572
1573 /* Put a lower limit on speed. Note with this speed, you move once every
1574 * 20 ticks or so. This amounts to once every 3 seconds of realtime.
1575 */
1576 if (op->speed < 0.05 && op->type == PLAYER)
1577 op->speed = 0.05;
1578 /*
1579 * A lower limit should also apply to monsters. By ensuring speeds are
1580 * loaded in as positive, we can properly do this. Otherwise, slow effects
1581 * either push the monster's speed to be more negative (if we leave the input
1582 * speed as negative) or can reduce the speed by more than the monster's speed,
1583 * making it turn negative (if we load speeds as positive always).
1584 *
1585 * For whatever reason, op->type is 0 for some monsters, making FLAG_MONSTER
1586 * the useful information that indicates whether it is a monster or not.
1587 *
1588 * MIN_ACTIVE_SPEED is super low, so make our speed threshold be about .005 instead
1589 */
1590 if (op->speed < MIN_ACTIVE_SPEED*500 && QUERY_FLAG(op, FLAG_MONSTER)) {
1591 // If added_speed is less than zero, we're probably working with a slow effect.
1592 if (added_speed >= 0)
1593 LOG(llevInfo, "fix_object: Monster %s has negative speed of %f.\n",
1594 op->name ? op->name : "(null)", op->speed);
1595 op->speed = MIN_ACTIVE_SPEED*500;
1596 }
1597
1598 /* I want to limit the power of small monsters with big weapons: */
1599 if (op->type != PLAYER
1600 && op->arch != NULL
1601 && op->stats.dam > op->arch->clone.stats.dam*3)
1602 op->stats.dam = op->arch->clone.stats.dam*3;
1603
1604 /* Prevent overflows of wc - best you can get is ABS(120) - this
1605 * should be more than enough - remember, AC is also in 8 bits,
1606 * so its value is the same.
1607 */
1608 if (wc > 120)
1609 wc = 120;
1610 else if (wc < -120)
1611 wc = -120;
1612 op->stats.wc = wc;
1613
1614 if (ac > 120)
1615 ac = 120;
1616 else if (ac < -120)
1617 ac = -120;
1618 op->stats.ac = ac;
1619
1620 /* if for some reason the creature doesn't have any move type,
1621 * give them walking as a default.
1622 * The second case is a special case - to more closely mimic the
1623 * old behaviour - if your flying, your not walking - just
1624 * one or the other.
1625 */
1626 if (op->move_type == 0)
1627 op->move_type = MOVE_WALK;
1628 else if (op->move_type&(MOVE_FLY_LOW|MOVE_FLY_HIGH))
1629 op->move_type &= ~MOVE_WALK;
1630
1631 object_update_speed(op);
1632 }
1633
1634 /**
1635 * Returns true if the given player is a legal class.
1636 * The function to add and remove class-bonuses to the stats doesn't
1637 * check if the stat becomes negative, thus this function
1638 * merely checks that all stats are 1 or more, and returns
1639 * false otherwise.
1640 *
1641 * @param op
1642 * object to check.
1643 * @return
1644 * 1 if allowed, 0 else.
1645 */
1646 int allowed_class(const object *op) {
1647 return op->stats.Dex > 0
1648 && op->stats.Str > 0
1649 && op->stats.Con > 0
1650 && op->stats.Int > 0
1651 && op->stats.Wis > 0
1652 && op->stats.Pow > 0
1653 && op->stats.Cha > 0;
1654 }
1655
1656 /**
1657 * Set the new dragon name after gaining levels or
1658 * changing ability focus (later this can be extended to
1659 * eventually change the player's face and animation)
1660 *
1661 * Note that the title is written to 'own_title' in the
1662 * player struct. This should be changed to 'ext_title'
1663 * as soon as clients support this!
1664 * Please, anyone, write support for 'ext_title'.
1665 *
1666 * @param pl
1667 * player's object to change.
1668 * @param abil
1669 * dragon's innate abilities.
1670 * @param skin
1671 * dragon's skin.
1672 */
1673 void set_dragon_name(object *pl, const object *abil, const object *skin) {
1674 int atnr = -1; /* attacknumber of highest level */
1675 int level = 0; /* highest level */
1676 int i;
1677
1678 /* Perhaps do something more clever? */
1679 if (!abil || !skin)
1680 return;
1681
1682 /* first, look for the highest level */
1683 for (i = 0; i < NROFATTACKS; i++) {
1684 if (atnr_is_dragon_enabled(i)
1685 && (atnr == -1 || abil->resist[i] > abil->resist[atnr])) {
1686 level = abil->resist[i];
1687 atnr = i;
1688 }
1689 }
1690
1691 /* now if there are equals at highest level, pick the one with focus,
1692 or else at random */
1693 if (atnr_is_dragon_enabled(abil->stats.exp)
1694 && abil->resist[abil->stats.exp] >= level)
1695 atnr = abil->stats.exp;
1696
1697 level = (int)(level/5.);
1698
1699 /* now set the new title */
1700 if (pl->contr != NULL) {
1701 player_set_dragon_title(pl->contr, level, attacks[atnr], skin->resist[atnr]);
1702 }
1703 }
1704
1705 /**
1706 * This function is called when a dragon-player gains
1707 * an overall level. Here, the dragon might gain new abilities
1708 * or change the ability-focus.
1709 *
1710 * @param who
1711 * dragon's object.
1712 */
1713 static void dragon_level_gain(object *who) {
1714 object *abil = NULL; /* pointer to dragon ability force*/
1715 object *skin = NULL; /* pointer to dragon skin force*/
1716
1717 /* now grab the 'dragon_ability'-forces from the player's inventory */
1718 abil = object_find_by_type_and_arch_name(who, FORCE, "dragon_ability_force");
1719 skin = object_find_by_type_and_arch_name(who, FORCE, "dragon_skin_force");
1720 /* if the force is missing -> bail out */
1721 if (abil == NULL)
1722 return;
1723
1724 /* The ability_force keeps track of maximum level ever achieved.
1725 * New abilties can only be gained by surpassing this max level
1726 */
1727 if (who->level > abil->level) {
1728 /* increase our focused ability */
1729 int lev = ++(abil->resist[abil->stats.exp]);
1730
1731 if (lev > 0) {
1732 /* try to hand out a new ability-gift
1733 * if not the right level, this will handout nothing.
1734 */
1735 dragon_ability_gain(who, (int)abil->stats.exp, lev);
1736 }
1737
1738 if (abil->last_eat > 0 && atnr_is_dragon_enabled(abil->last_eat)) {
1739 /* apply new ability focus */
1740 draw_ext_info_format(NDI_UNIQUE|NDI_BLUE, 0, who, MSG_TYPE_ATTRIBUTE, MSG_TYPE_ATTRIBUTE_RACE,
1741 "Your metabolism now focuses on %s!",
1742 change_resist_msg[abil->last_eat]);
1743
1744 abil->stats.exp = abil->last_eat;
1745 abil->last_eat = 0;
1746 }
1747
1748 abil->level = who->level;
1749 }
1750
1751 /* last but not least, set the new title for the dragon */
1752 set_dragon_name(who, abil, skin);
1753 }
1754
1755 /**
1756 * Given the skill name skill_name, we find the skill
1757 * archetype/object, set appropriate values, and insert it into
1758 * the object (op) that is passed.
1759 *
1760 * We return the skill - this makes it easier for calling functions that
1761 * want to do something with it immediately.
1762 *
1763 * @param op
1764 * object to which to give the skill. Must not be NULL.
1765 * @param skill_name
1766 * skill to give. Must not be NULL.
1767 * @return
1768 * newly created skill, or NULL if failure (logger with error level).
1769 *
1770 * @note
1771 * this doesn't check whether the object already has the skill or not.
1772 */
1773 object *give_skill_by_name(object *op, const char *skill_name) {
1774 object *skill_obj;
1775 archetype *skill_arch;
1776
1777 skill_arch = get_archetype_by_skill_name(skill_name, SKILL);
1778 if (!skill_arch) {
1779 LOG(llevError, "add_player_exp: couldn't find skill %s\n", skill_name);
1780 return NULL;
1781 }
1782 skill_obj = arch_to_object(skill_arch); /* never returns NULL. */
1783
1784 /* clear the flag - exp goes into this bucket, but player
1785 * still doesn't know it.
1786 */
1787 CLEAR_FLAG(skill_obj, FLAG_CAN_USE_SKILL);
1788 skill_obj->stats.exp = 0;
1789 skill_obj->level = 1;
1790 object_insert_in_ob(skill_obj, op);
1791 if (op->contr) {
1792 link_player_skills(op);
1793 }
1794 return skill_obj;
1795 }
1796
1797 /**
1798 * For the new exp system. we are concerned with
1799 * whether the player gets more hp, sp and new levels.
1800 * Note this this function should only be called for players. Monsters
1801 * don't really gain levels
1802 *
1803 * Will tell the player about changed levels.
1804 * A single call will handle mutiple level changes to one object from one effect.
1805 * The only difference between this and the recursive version is this cannot handle
1806 * a level that both increases and decreases simultaneously.
1807 *
1808 * @param who
1809 * player, must not be NULL.
1810 * @param op
1811 * what we are checking to gain the level (eg, skill), can be NULL.
1812 */
1813 void player_lvl_adj(object *who, object *op) {
1814 char buf[MAX_BUF];
1815
1816 assert(who);
1817
1818 if (!op) /* when rolling stats */
1819 op = who;
1820
1821 if (op->level < settings.max_level && op->stats.exp >= level_exp(op->level+1, who->expmul)) {
1822 do{
1823 op->level++;
1824
1825 if (op != NULL && op == who && op->stats.exp > 1 && is_dragon_pl(who))
1826 dragon_level_gain(who);
1827
1828 /* Only roll these if it is the player (who) that gained the level */
1829 if (op == who && (who->level < 11) && who->type == PLAYER) {
1830 who->contr->levhp[who->level] = die_roll(2, 4, who, PREFER_HIGH)+1;
1831 who->contr->levsp[who->level] = die_roll(2, 3, who, PREFER_HIGH);
1832 who->contr->levgrace[who->level] = die_roll(2, 2, who, PREFER_HIGH)-1;
1833 }
1834
1835 fix_object(who); // TODO: Call this only once per function call?
1836 if (op->level > 1) {
1837 if (op->type != PLAYER)
1838 snprintf(buf, sizeof(buf), "You are now level %d in the %s skill.", op->level, op->name);
1839 else
1840 snprintf(buf, sizeof(buf), "You are now level %d.", op->level);
1841
1842 draw_ext_info(NDI_UNIQUE|NDI_RED, 0, who, MSG_TYPE_ATTRIBUTE, MSG_TYPE_ATTRIBUTE_LEVEL_GAIN, buf);
1843 }
1844 } while (op->level < settings.max_level && op->stats.exp >= level_exp(op->level+1, who->expmul)); /* To increase more levels */
1845 } else if (op->level > 1 && op->stats.exp < level_exp(op->level, who->expmul)) {
1846 do{
1847 op->level--;
1848 fix_object(who); // TODO: Call this only once per function call?
1849 if (op->type != PLAYER)
1850 snprintf(buf, sizeof(buf), "You are now level %d in the %s skill.", op->level, op->name);
1851 else
1852 snprintf(buf, sizeof(buf), "You are now level %d.", op->level);
1853
1854 draw_ext_info(NDI_UNIQUE|NDI_RED, 0, who, MSG_TYPE_ATTRIBUTE, MSG_TYPE_ATTRIBUTE_LEVEL_LOSS, buf);
1855 } while (op->level > 1 && op->stats.exp < level_exp(op->level, who->expmul)); /* To decrease more levels */
1856 }
1857 }
1858
1859 /**
1860 * Returns how much experience is needed for a player to become
1861 * the given level. level should really never exceed max_level
1862 *
1863 * @param level
1864 * level player wants to reach
1865 * @param expmul
1866 * penality/bonus for experience.
1867 */
1868 int64_t level_exp(int level, double expmul) {
1869 if (level > settings.max_level)
1870 return expmul*levels[settings.max_level];
1871 return expmul*levels[level];
1872 }
1873
1874 /**
1875 * Returns the level for a given exp.
1876 *
1877 * @param exp
1878 * experince points to look up in the level table
1879 */
1880 int exp_level(int64_t exp) {
1881 int level = 1;
1882 while ( level < settings.max_level && levels[level+1] <= exp )
1883 ++level;
1884 return level;
1885 }
1886
1887 /**
1888 * Ensure that the permanent experience requirements in an exp object are met.
1889 * This really just checks 'op to make sure the perm_exp value is within
1890 * proper range. Note that the checking of what is passed through
1891 * has been reduced. Since there is now a proper field for perm_exp,
1892 * this can now work on a much larger set of objects.
1893 *
1894 * @param op
1895 * object to check.
1896 */
1897 void calc_perm_exp(object *op) {
1898 if (op->total_exp < op->stats.exp)
1899 op->total_exp = op->stats.exp;
1900
1901 /* Cap permanent experience. */
1902 if (op->total_exp < 0)
1903 op->total_exp = 0;
1904 else if (op->total_exp > MAX_EXPERIENCE * 100 / settings.permanent_exp_ratio)
1905 op->total_exp = MAX_EXPERIENCE * 100 / settings.permanent_exp_ratio;
1906 }
1907
1908 /**
1909 * Find a skill by name using the last_skill_ob list. Assume last_skill_ob
1910 * was correctly updated before.
1911 *
1912 * This is defined here rather than server/skill_util.c because things
1913 * defined in libserver.a are not available when libcommon.a is linked.
1914 */
1915 object* find_applied_skill_by_name(const object* op, const char* name) {
1916 for (int i = 0; i < MAX_SKILLS; i++) {
1917 if (op->contr->last_skill_ob[i] != NULL) {
1918 // Skill objects can be removed without updating last_skill_ob. Clean
1919 // them up here if that's the case.
1920 if (QUERY_FLAG(op->contr->last_skill_ob[i], FLAG_REMOVED)) {
1921 LOG(llevDebug, "pruning removed object from last_skill_ob\n");
1922 op->contr->last_skill_ob[i] = NULL;
1923 continue;
1924 }
1925
1926 if (op->contr->last_skill_ob[i]->skill != NULL) {
1927 if (!strcmp(op->contr->last_skill_ob[i]->skill, name)) {
1928 return op->contr->last_skill_ob[i];
1929 }
1930 } else {
1931 LOG(llevError,
1932 "%s's skill object %s does not have a skill name\n",
1933 op->name, op->contr->last_skill_ob[i]->name);
1934 }
1935 }
1936 }
1937 return NULL;
1938 }
1939
1940 /**
1941 * Add experience to a player - exp should only be positive.
1942 * Updates permanent exp for the skill we are adding to.
1943 *
1944 * @param op
1945 * object we add exp to.
1946 * @param exp
1947 * experience to gain. Can be 0, but not that useful to call in that case...
1948 * @param skill_name
1949 * skill to add exp to. Can be NULL, in which case exp increases the players general
1950 * total, but not any particular skill.
1951 * @param flag
1952 * what to do if the player doesn't have the skill. Combination of @ref SK_EXP_xxx "SK_EXP_xxx" flags.
1953 */
1954 static void add_player_exp(object *op, int64_t exp, const char *skill_name, int flag) {
1955 object *skill_obj = NULL;
1956 int64_t limit, exp_to_add;
1957 int64_t added_skill_exp, added_skill_total_exp;
1958
1959 /* prevents some forms of abuse. */
1960 if (op->contr->braced)
1961 exp = exp/5;
1962
1963 /* Try to find the matching skill.
1964 * We do a shortcut/time saving mechanism first - see if it matches
1965 * chosen_skill. This means we don't need to search through
1966 * the players inventory.
1967 */
1968 if (skill_name) {
1969 if (op->chosen_skill
1970 && op->chosen_skill->type == SKILL
1971 && !strcmp(skill_name, op->chosen_skill->skill))
1972 skill_obj = op->chosen_skill;
1973 else {
1974 skill_obj = find_applied_skill_by_name(op, skill_name);
1975
1976 /* Player doesn't have the skill. Check to see what to do, and give
1977 * it to the player if necessary
1978 */
1979 if (!skill_obj) {
1980 if (flag == SK_EXP_NONE)
1981 return;
1982 else if (flag == SK_EXP_ADD_SKILL)
1983 skill_obj = give_skill_by_name(op, skill_name);
1984 }
1985 }
1986 }
1987
1988 /* Basically, you can never gain more experience in one shot
1989 * than half what you need to gain for next level.
1990 */
1991 exp_to_add = exp;
1992 /*
1993 * Make sure we aren't trying to go backwards when we hit maximum level,
1994 * but make sure we can still add to our permanent experience.
1995 *
1996 * -- Daniel Hawkins 2015-05-24
1997 */
1998 if (op->level == settings.max_level)
1999 limit = levels[op->level] / 2;
2000 else
2001 limit = (levels[op->level+1]-levels[op->level])/2;
2002
2003 if (exp_to_add > limit)
2004 exp_to_add = limit;
2005
2006 if (skill_obj) {
2007 exp_to_add = exp;
2008 /*
2009 * Make sure we aren't trying to go backwards when we hit maximum level,
2010 * but make sure we can still add to our permanent experience.
2011 *
2012 * -- Daniel Hawkins 2015-05-24
2013 */
2014 if (skill_obj->level == settings.max_level)
2015 limit = levels[skill_obj->level] / 2;
2016 else
2017 limit = (levels[skill_obj->level+1]-levels[skill_obj->level])/2;
2018
2019 if (exp_to_add > limit)
2020 exp_to_add = limit;
2021 added_skill_exp = skill_obj->stats.exp;
2022 ADD_EXP(skill_obj->stats.exp, exp_to_add);
2023 added_skill_exp = skill_obj->stats.exp - added_skill_exp;
2024 if (settings.permanent_exp_ratio) {
2025 added_skill_total_exp = skill_obj->total_exp;
2026 ADD_TOTALEXP(skill_obj->total_exp, exp_to_add);
2027 added_skill_total_exp = skill_obj->total_exp - added_skill_total_exp;
2028 calc_perm_exp(skill_obj);
2029 }
2030 player_lvl_adj(op, skill_obj);
2031 }
2032 else {
2033 added_skill_exp = added_skill_total_exp = exp_to_add;
2034 }
2035
2036 ADD_EXP(op->stats.exp, (float)added_skill_exp*(skill_obj ? skill_obj->expmul : 1));
2037 if (settings.permanent_exp_ratio) {
2038 ADD_TOTALEXP(op->total_exp, (float)added_skill_total_exp*(skill_obj ? skill_obj->expmul : 1));
2039 calc_perm_exp(op);
2040 }
2041
2042 player_lvl_adj(op, NULL);
2043 }
2044
2045 /**
2046 * This function checks to make sure that object 'op' can
2047 * lose 'exp' experience. It basically makes
2048 * adjustments based on permanent exp and the like.
2049 * This function should always be used for losing experience -
2050 * the 'exp' value passed should be positive - this is the
2051 * amount that should get subtract from the player.
2052 *
2053 * @param op
2054 * object from which to subtract.
2055 * @param exp
2056 * experience to lose.
2057 * @return
2058 * the amount of exp object 'op' can in fact lose -
2059 */
2060 int64_t check_exp_loss(const object *op, int64_t exp) {
2061 int64_t del_exp;
2062
2063 if (exp > op->stats.exp)
2064 exp = op->stats.exp;
2065 if (settings.permanent_exp_ratio) {
2066 del_exp = (op->stats.exp-PERM_EXP(op->total_exp))*PERM_EXP_MAX_LOSS_RATIO;
2067 if (del_exp < 0)
2068 del_exp = 0;
2069 if (exp > del_exp)
2070 exp = del_exp;
2071 }
2072 return exp;
2073 }
2074
2075 /**
2076 * Returns the maximum experience the object can gain or lose.
2077 *
2078 * @param op
2079 * object which will be the victim.
2080 * @param exp
2081 * experience to gain or lose.
2082 * @return
2083 * maximum value op can gain or lose (can be positive or negative).
2084 */
2085 int64_t check_exp_adjust(const object *op, int64_t exp) {
2086 if (exp < 0)
2087 return check_exp_loss(op, exp);
2088 else
2089 return MIN(exp, MAX_EXPERIENCE-op->stats.exp);
2090 }
2091
2092 /**
2093 * Subtracts experience from player.
2094 *
2095 * Eg, if we figure the player is losing 10%
2096 * of his total exp, what happens is he loses 10% from all his skills.
2097 * Note that if permanent exp is used, player may not in fact lose
2098 * as much as listed. Eg, if player has gotten reduced to the point
2099 * where everything is at the minimum perm exp, he would lose nothing.
2100 *
2101 * @param op
2102 * victim we want to substract from
2103 * @param exp
2104 * value to substract. Should be positive.
2105 * @param skill
2106 * skill to substract exp from. Can be NULL.
2107 * @param flag
2108 * if ::SK_SUBTRACT_SKILL_EXP and skill is set, only subtract from the matching skill. Otherwise,
2109 * this subtracts a portion from all skills the player has.
2110 *
2111 * @todo
2112 * check whether flag is necessary, can't it be only based on skill==null?
2113 */
2114 static void subtract_player_exp(object *op, int64_t exp, const char *skill, int flag) {
2115 float fraction = (float)exp/(float)op->stats.exp;
2116 int64_t del_exp;
2117
2118 FOR_INV_PREPARE(op, tmp)
2119 if (tmp->type == SKILL && tmp->stats.exp) {
2120 if (flag == SK_SUBTRACT_SKILL_EXP && skill && !strcmp(tmp->skill, skill)) {
2121 del_exp = check_exp_loss(tmp, exp);
2122 tmp->stats.exp -= del_exp;
2123 player_lvl_adj(op, tmp);
2124 } else if (flag != SK_SUBTRACT_SKILL_EXP) {
2125 /* only want to process other skills if we are not trying
2126 * to match a specific skill.
2127 */
2128 del_exp = check_exp_loss(tmp, tmp->stats.exp*fraction);
2129 tmp->stats.exp -= del_exp;
2130 player_lvl_adj(op, tmp);
2131 }
2132 }
2133 FOR_INV_FINISH();
2134 if (flag != SK_SUBTRACT_SKILL_EXP) {
2135 del_exp = check_exp_loss(op, exp);
2136 op->stats.exp -= del_exp;
2137 player_lvl_adj(op, NULL);
2138 }
2139 }
2140
2141 /**
2142 * Changes experience to a player/monster. This
2143 * does bounds checking to make sure we don't overflow the max exp.
2144 *
2145 * The exp passed is typically not modified much by this function -
2146 * it is assumed the caller has modified the exp as needed.
2147 * skill_name is the skill that should get the exp added.
2148 * flag is what to do if player doesn't have the skill.
2149 * these last two values are only used for players.
2150 *
2151 * @param op
2152 * victim to alter.
2153 * @param exp
2154 * experience to gain (positive) or lose (negative).
2155 * @param skill_name
2156 * skill to change. Can be NULL.
2157 * @param flag
2158 * @li if experience gain, what to do if player doesn't have the skill
2159 * @li if experience loss, whether to remove from all skills or only specified skill
2160 * @see share_exp() for a party-aware version.
2161 */
2162 void change_exp(object *op, int64_t exp, const char *skill_name, int flag) {
2163 /* safety */
2164 if (!op) {
2165 LOG(llevError, "change_exp() called for null object!\n");
2166 return;
2167 }
2168
2169 /* if no change in exp, just return - most of the below code
2170 * won't do anything if the value is 0 anyways.
2171 */
2172 if (exp == 0)
2173 return;
2174
2175 /* Monsters are easy - we just adjust their exp - we
2176 * don't adjust level, since in most cases it is unrelated to
2177 * the exp they have - the monsters exp represents what its
2178 * worth.
2179 */
2180 if (op->type != PLAYER && op->type != WEAPON) {
2181 /* Sanity check */
2182 if (!QUERY_FLAG(op, FLAG_ALIVE))
2183 return;
2184
2185 /* reset exp to max allowed value. We subtract from
2186 * MAX_EXPERIENCE to prevent overflows. If the player somehow has
2187 * more than max exp, just return.
2188 */
2189 if (exp > 0 && (op->stats.exp > (MAX_EXPERIENCE-exp))) {
2190 exp = MAX_EXPERIENCE-op->stats.exp;
2191 if (exp < 0)
2192 return;
2193 }
2194
2195 op->stats.exp += exp;
2196 } else if (op->type == WEAPON) { /* Weapons -- this allows us to make magic weapons that get stronger the more they are used. */
2197 // Handle adding exp -- Don't use level because other stuff already uses that.
2198 // The caller should determine what percentage of base exp this is.
2199 ADD_TOTALEXP(op->total_exp, exp);
2200 if (exp > 0) {
2201 // Check for a level-up
2202 while (level_exp(op->item_power+1, 1) < op->total_exp) {
2203 ++(op->item_power);
2204 }
2205 } else { /* Removing exp allows for the weapon's power to be reset if needed. */
2206 // Recalculate level
2207 while (level_exp(op->item_power, 1) > op->total_exp) {
2208 --(op->item_power);
2209 }
2210 }
2211 } else { /* Players only */
2212 if (exp > 0)
2213 add_player_exp(op, exp, skill_name, flag);
2214 else
2215 subtract_player_exp(op, FABS(exp), skill_name, flag);
2216 }
2217 }
2218
2219 /**
2220 * Applies a death penalty experience, the size of this is defined by the
2221 * settings death_penalty_percentage and death_penalty_levels, and by the
2222 * amount of permenent experience, whichever gives the lowest loss.
2223 *
2224 * @param op
2225 * victim of the penalty. Must not be NULL.
2226 */
2227 void apply_death_exp_penalty(object *op) {
2228 int64_t loss;
2229 int64_t percentage_loss; /* defined by the setting 'death_penalty_percent' */
2230 int64_t level_loss; /* defined by the setting 'death_penalty_levels */
2231
2232 FOR_INV_PREPARE(op, tmp)
2233 if (tmp->type == SKILL && tmp->stats.exp) {
2234 percentage_loss = tmp->stats.exp*settings.death_penalty_ratio/100;
2235 level_loss = tmp->stats.exp-levels[MAX(0, tmp->level-settings.death_penalty_level)];
2236
2237 /* With the revised exp system, you can get cases where
2238 * losing several levels would still require that you have more
2239 * exp than you currently have - this is true if the levels
2240 * tables is a lot harder.
2241 */
2242 if (level_loss < 0)
2243 level_loss = 0;
2244
2245 loss = check_exp_loss(tmp, MIN(level_loss, percentage_loss));
2246
2247 tmp->stats.exp -= loss;
2248 player_lvl_adj(op, tmp);
2249 }
2250 FOR_INV_FINISH();
2251
2252 percentage_loss = op->stats.exp*settings.death_penalty_ratio/100;
2253 level_loss = op->stats.exp-levels[MAX(0, op->level-settings.death_penalty_level)];
2254 if (level_loss < 0)
2255 level_loss = 0;
2256 loss = check_exp_loss(op, MIN(level_loss, percentage_loss));
2257
2258 op->stats.exp -= loss;
2259 player_lvl_adj(op, NULL);
2260 }
2261
2262 /**
2263 * This function takes an object (monster/player, op), and
2264 * determines if it makes a basic save throw by looking at the
2265 * save_throw table.
2266 *
2267 * @param op
2268 * potential victim.
2269 * @param level
2270 * the effective level to make the save at
2271 * @param bonus
2272 * any plus/bonus (typically based on resistance to particular attacktype).
2273 * @return
2274 * 1 if op makes his save, 0 if he failed
2275 */
2276 int did_make_save(const object *op, int level, int bonus) {
2277 if (level > MAX_SAVE_LEVEL)
2278 level = MAX_SAVE_LEVEL;
2279
2280 if ((random_roll(1, 20, op, PREFER_HIGH)+bonus) < savethrow[level])
2281 return 0;
2282 return 1;
2283 }
2284
2285 /**
2286 * Gives experience to a player/monster, sharing it with party if applicable.
2287 * This does bounds checking to make sure we don't overflow the max exp.
2288 *
2289 * The exp passed is typically not modified much by this function -
2290 * it is assumed the caller has modified the exp as needed.
2291 *
2292 * This is merely a wrapper for change_exp().
2293 *
2294 * @param op
2295 * victim to alter.
2296 * @param exp
2297 * experience to gain (positive) or lose (negative).
2298 * @param skill
2299 * skill to change. Can be NULL.
2300 * @param flag
2301 * @li if experience gain, what to do if player doesn't have the skill
2302 * @li if experience loss, whether to remove from all skills or only specified skill
2303 * @note
2304 * flag only applies to op, not other players in same party.
2305 */
2306 void share_exp(object *op, int64_t exp, const char *skill, int flag) {
2307 int shares = 0, count = 0;
2308 player *pl;
2309 partylist *party;
2310
2311 if (op->type != PLAYER || op->contr->party == NULL) {
2312 change_exp(op, exp, skill, 0);
2313 return;
2314 }
2315
2316 party = op->contr->party;
2317
2318 for (pl = first_player; pl != NULL; pl = pl->next) {
2319 if (party && pl->ob->contr->party == party && on_same_map(pl->ob, op)) {
2320 count++;
2321 shares += (pl->ob->level+4);
2322 }
2323 }
2324
2325 assert(shares > 0);
2326
2327 if (count == 1 || shares > exp)
2328 change_exp(op, exp, skill, flag);
2329 else {
2330 int64_t share = exp/shares, given = 0, nexp;
2331 for (pl = first_player; pl != NULL; pl = pl->next) {
2332 if (party && pl->ob->contr->party == party && on_same_map(pl->ob, op)) {
2333 nexp = (pl->ob->level+4)*share;
2334 change_exp(pl->ob, nexp, skill, SK_EXP_TOTAL);
2335 given += nexp;
2336 }
2337 }
2338 exp -= given;
2339 /* give any remainder to the player */
2340 change_exp(op, exp, skill, flag);
2341 }
2342 }
2343
2344 int get_cha_bonus(int stat) {
2345 return int_bonuses[INT_CHA_BONUS][get_index(stat, settings.max_stat)];
2346 }
2347
2348 int get_dex_bonus(int stat) {
2349 return int_bonuses[INT_DEX_BONUS][get_index(stat, settings.max_stat)];
2350 }
2351
2352 int get_thaco_bonus(int stat) {
2353 return int_bonuses[INT_THAC0_BONUS][get_index(stat, settings.max_stat)];
2354 }
2355
2356 uint32_t get_weight_limit(int stat) {
2357 return int_bonuses[INT_WEIGHT_LIMIT][get_index(stat, settings.max_stat)];
2358 }
2359
2360 int get_learn_spell(int stat) {
2361 return int_bonuses[INT_LEARN_SPELL][get_index(stat, settings.max_stat)];
2362 }
2363
2364 int get_cleric_chance(int stat) {
2365 return int_bonuses[INT_CLERIC_CHANCE][get_index(stat, settings.max_stat)];
2366 }
2367
2368 int get_turn_bonus(int stat) {
2369 return int_bonuses[INT_TURN_BONUS][get_index(stat, settings.max_stat)];
2370 }
2371
2372 int get_dam_bonus(int stat) {
2373 return int_bonuses[INT_DAM_BONUS][get_index(stat, settings.max_stat)];
2374 }
2375
2376 float get_speed_bonus(int stat) {
2377 return float_bonuses[FLOAT_DEX_BONUS][get_index(stat, settings.max_stat)];
2378 }
2379
2380 int get_fear_bonus(int stat) {
2381 return int_bonuses[INT_FEAR_BONUS][get_index(stat, settings.max_stat)];
2382 }
2383
2384 static float get_con_bonus(int stat) {
2385 return float_bonuses[FLOAT_CON_BONUS][get_index(stat, settings.max_stat)];
2386 }
2387
2388 static float get_sp_bonus(int stat) {
2389 return float_bonuses[FLOAT_SP_BONUS][get_index(stat, settings.max_stat)];
2390 }
2391
2392 static float get_grace_bonus(int stat) {
2393 return float_bonuses[FLOAT_GRACE_BONUS][get_index(stat, settings.max_stat)];
2394 }
2395
2396 /**
2397 * Limits a stat value to [0..<code>max_index</code>].
2398 * @param stat
2399 * index of the stat to get.
2400 * @param max_index
2401 * the maximum index (inclusive).
2402 * @return the limited index.
2403 */
2404 static size_t get_index(int stat, size_t max_index) {
2405 size_t index;
2406
2407 if (stat < 0) {
2408 return 0;
2409 }
2410
2411 index = (size_t)stat;
2412 return MIN(index, max_index);
2413 }
2414
2415 /**
2416 * This loads up a stat table from the file - basically,
2417 * it keeps processing until it gets the closing brace.
2418 *
2419 * @param bonuses
2420 * an array will be allocated and the bonus put into this allocated
2421 * array.
2422 * @param fp
2423 * File to load the data from.
2424 * @param bonus_name
2425 * Used purely for error messages to make it easier to identify
2426 * where in the file an error is.
2427 *
2428 * @return
2429 * 0 on success, 1 on error. In general, the error
2430 * will be too many or too few bonus values.
2431 */
2432 static int load_table_int(int **bonuses, FILE *fp, char *bonus_name)
2433 {
2434 char buf[MAX_BUF], *cp;
2435 int on_stat = 0, tmp_bonus;
2436
2437 *bonuses = calloc(settings.max_stat+1, sizeof(int));
2438
2439 while (fgets(buf, MAX_BUF-1, fp) != NULL) {
2440 if (buf[0] == '#')
2441 continue;
2442
2443 /* Skip over empty lines */
2444 if (buf[0] == '\n')
2445 continue;
2446
2447 /* Do not care about opening brace */
2448 if (buf[0] == '{')
2449 continue;
2450
2451 if (buf[0] == '}') {
2452 if ((on_stat-1) != settings.max_stat) {
2453 LOG(llevError,"Number of bonus does not match max stat (%d!=%d, bonus=%s)\n",
2454 on_stat, settings.max_stat, bonus_name);
2455 return 1;
2456 }
2457 else return 0;
2458 }
2459
2460 /* If not any of the above values, must be the stat table,
2461 * or so we hope.
2462 */
2463 cp = buf;
2464 while (*cp != 0) {
2465 /* Skip over any non numbers */
2466 while (!isdigit(*cp) && *cp!='.' && *cp!='-' && *cp!='+' && *cp != 0)
2467 cp++;
2468
2469 if (*cp == 0) break;
2470
2471 tmp_bonus = atoi(cp);
2472
2473 if (on_stat > settings.max_stat) {
2474 LOG(llevError,"Number of bonus entries exceed max stat (line=%s, bonus=%s)\n",
2475 buf, bonus_name);
2476 return 1;
2477 }
2478 (*bonuses)[on_stat] = tmp_bonus;
2479 on_stat++;
2480
2481 /* Skip over any digits, as that is the number we just processed */
2482 while ((isdigit(*cp) || *cp=='-' || *cp=='+') && *cp != 0)
2483 cp++;
2484 }
2485 }
2486 /* This should never happen - we should always get the closing brace */
2487 LOG(llevError,"Reached end of file without getting close brace? bonus=%s\n", bonus_name);
2488 return 1;
2489 }
2490
2491 /**
2492 * This loads up a stat table from the file - basically,
2493 * it keeps processing until it gets the closing brace.
2494 *
2495 * @param bonuses
2496 * an array will be allocated and the bonus put into this allocated
2497 * array.
2498 * @param fp
2499 * File to load the data from.
2500 * @param bonus_name
2501 * Used purely for error messages to make it easier to identify
2502 * where in the file an error is.
2503 *
2504 * @return
2505 * 0 on success, 1 on error. In general, the error
2506 * will be too many or too few bonus values.
2507 */
2508 static int load_table_float(float **bonuses, FILE *fp, char *bonus_name)
2509 {
2510 char buf[MAX_BUF], *cp;
2511 int on_stat = 0;
2512 float tmp_bonus;
2513
2514 *bonuses = calloc(settings.max_stat+1, sizeof(float));
2515
2516 while (fgets(buf, MAX_BUF-1, fp) != NULL) {
2517 if (buf[0] == '#')
2518 continue;
2519
2520 /* Skip over empty lines */
2521 if (buf[0] == '\n')
2522 continue;
2523
2524 /* Do not care about opening brace */
2525 if (buf[0] == '{')
2526 continue;
2527
2528 if (buf[0] == '}') {
2529 if ((on_stat-1) != settings.max_stat) {
2530 LOG(llevError,"Number of bonus does not match max stat (%d!=%d, bonus=%s)\n",
2531 on_stat, settings.max_stat, bonus_name);
2532 return 1;
2533 }
2534 else return 0;
2535 }
2536
2537 /* If not any of the above values, must be the stat table,
2538 * or so we hope.
2539 */
2540 cp = buf;
2541 while (*cp != 0) {
2542 /* Skip over any non numbers */
2543 while (!isdigit(*cp) && *cp!='.' && *cp!='-' && *cp!='+' && *cp != 0)
2544 cp++;
2545
2546 if (*cp == 0) break;
2547
2548 tmp_bonus = atof(cp);
2549
2550 if (on_stat > settings.max_stat) {
2551 LOG(llevError,"Number of bonus entries exceed max stat (line=%s, bonus=%s)\n",
2552 buf, bonus_name);
2553 return 1;
2554 }
2555 (*bonuses)[on_stat] = tmp_bonus;
2556 on_stat++;
2557
2558 /* Skip over any digits, as that is the number we just processed
2559 * since this is floats, also skip over any dots.
2560 */
2561 while ((isdigit(*cp) || *cp=='-' || *cp=='+' || *cp=='.') && *cp != 0)
2562 cp++;
2563 }
2564 }
2565 /* This should never happen - we should always get the closing brace */
2566 LOG(llevError,"Reached end of file without getting close brace? bonus=%s\n", bonus_name);
2567 return 1;
2568 }
2569
2570
2571 /**
2572 * This loads statistic bonus/penalties from the stat_bonus file.
2573 * If reload is false (eg, this is initial start time load),
2574 * then any error becomes fatal, as system needs working
2575 * bonuses.
2576 *
2577 * @param reload
2578 * If set, this is reloading new values - this can be useful for
2579 * real time adjustment of stat bonuses - however, it also has some
2580 * more restraints - in particular, max_stat can not decrease (that could
2581 * be made to work but is more work) and also errors are not fatal - it
2582 * will just use the old stat bonus tables in the case of any errors.
2583 * TODO: add code that uses reload
2584 *
2585 * @note
2586 * will call exit() if file is invalid or not found.
2587 */
2588 void init_stats(int reload) {
2589 char buf[MAX_BUF], *cp;
2590 int error=0, i, oldmax = settings.max_stat;
2591 FILE *fp;
2592 float *new_float_bonuses[NUM_FLOAT_BONUSES];
2593 int *new_int_bonuses[NUM_INT_BONUSES];
2594
2595 snprintf(buf, sizeof(buf), "%s/stat_bonus", settings.confdir);
2596
2597 memset(new_int_bonuses, 0, NUM_INT_BONUSES * sizeof(int));
2598 memset(new_float_bonuses, 0, NUM_FLOAT_BONUSES * sizeof(float));
2599
2600 if ((fp = fopen(buf, "r")) == NULL) {
2601 LOG(llevError, "Fatal error: could not open experience table (%s)\n", buf);
2602 if (reload) return;
2603 else exit(1);
2604 }
2605 while (fgets(buf, MAX_BUF-1, fp) != NULL) {
2606 if (buf[0] == '#')
2607 continue;
2608
2609 /* eliminate newline */
2610 if ((cp = strrchr(buf, '\n')) != NULL)
2611 *cp = '\0';
2612
2613 /* Skip over empty lines */
2614 if (buf[0] == 0)
2615 continue;
2616
2617 /* Skip over leading spaces */
2618 cp = buf;
2619 while (isspace(*cp) && *cp != 0)
2620 cp++;
2621
2622 if (!strncasecmp(cp, "max_stat", 8)) {
2623 int newmax = atoi(cp+8);
2624
2625 /* newmax must be at least MIN_STAT and we do not currently
2626 * cupport decrease max stat on the fly - why this might be possible,
2627 * bounds checking for all objects would be needed, potentionally resetting
2628 * them.
2629 * If this is a reload, then on error, we just return without doing work.
2630 * If this is initial load, having an invalid stat range is an error, so
2631 * exit the program.
2632 */
2633 if (newmax < MIN_STAT || newmax < settings.max_stat) {
2634 LOG(llevError, "Got invalid max_stat (%d) from stat_bonus file\n", newmax);
2635 fclose(fp);
2636 if (reload) return;
2637 else exit(1);
2638 }
2639 settings.max_stat = newmax;
2640 continue;
2641 }
2642 /* max_stat needs to be set before any of the bonus values - we
2643 * need to know how large to make the array.
2644 */
2645 if (settings.max_stat == 0) {
2646 LOG(llevError, "Got bonus line or otherwise unknown value before max stat! (%s)\n",
2647 buf);
2648 fclose(fp);
2649
2650 if (reload) {
2651 return;
2652 } else {
2653 exit(1);
2654 }
2655 }
2656
2657 for (i=0; i<NUM_INT_BONUSES; i++) {
2658 if (!strncasecmp(cp, int_bonus_names[i], strlen(int_bonus_names[i]))) {
2659 error = load_table_int(&new_int_bonuses[i], fp, cp);
2660 break;
2661 }
2662 }
2663 /* If we did not find a match in the int bonuses, check the
2664 * float bonuses now.
2665 */
2666 if (i == NUM_INT_BONUSES) {
2667 for (i=0; i<NUM_FLOAT_BONUSES; i++) {
2668 if (!strncasecmp(cp, float_bonus_names[i], strlen(float_bonus_names[i]))) {
2669 error = load_table_float(&new_float_bonuses[i], fp, cp);
2670 break;
2671 }
2672 }
2673 /* This may not actually be a critical error */
2674 if (i == NUM_FLOAT_BONUSES) {
2675 LOG(llevError,"Unknown line in stat_bonus file: %s\n", buf);
2676 }
2677 }
2678 if (error) break;
2679 }
2680 fclose(fp);
2681
2682 /* Make sure that we have load tables for all the bonuses.
2683 * This is critical on initial load, but on reloads, it enusres that
2684 * all the bonus data matches.
2685 */
2686 for (i=0; i<NUM_INT_BONUSES; i++) {
2687 if (!new_int_bonuses[i]) {
2688 LOG(llevError,"No bonus loaded for %s\n", int_bonus_names[i]);
2689 error=2;
2690 }
2691 }
2692
2693 for (i=0; i<NUM_FLOAT_BONUSES; i++) {
2694 if (!new_float_bonuses[i]) {
2695 LOG(llevError,"No bonus loaded for %s\n", float_bonus_names[i]);
2696 error=2;
2697 }
2698 }
2699
2700 /* If we got an error, we just free up the data we read in and return/exit.
2701 * if no error, we make the tables we just read in into the default
2702 * tables.
2703 */
2704 if (error) {
2705 if (error==1)
2706 LOG(llevError,"Got error reading stat_bonus: %s\n", buf);
2707
2708 if (reload) {
2709 for (i=0; i<NUM_INT_BONUSES; i++)
2710 if (new_int_bonuses[i]) FREE_AND_CLEAR(new_int_bonuses[i]);
2711 for (i=0; i<NUM_FLOAT_BONUSES; i++)
2712 if (new_float_bonuses[i]) FREE_AND_CLEAR(new_float_bonuses[i]);
2713 settings.max_stat = oldmax;
2714 } else {
2715 exit(1);
2716 }
2717 } else {
2718 /* Everything check out - now copy the data into
2719 * the live arrays.
2720 */
2721 for (i=0; i<NUM_INT_BONUSES; i++) {
2722 free(int_bonuses[i]);
2723 int_bonuses[i] = new_int_bonuses[i];
2724 new_int_bonuses[i] = NULL;
2725 }
2726
2727 for (i=0; i<NUM_FLOAT_BONUSES; i++) {
2728 free(float_bonuses[i]);
2729 float_bonuses[i] = new_float_bonuses[i];
2730 new_float_bonuses[i] = NULL;
2731 }
2732 }
2733 }