1 /**
2  * \file player.h
3  * \brief Player implementation
4  *
5  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
6  * Copyright (c) 2011 elly+angband@leptoquark.net. See COPYING.
7  * Copyright (c) 2015 Nick McConnell
8  *
9  * This work is free software; you can redistribute it and/or modify it
10  * under the terms of either:
11  *
12  * a) the GNU General Public License as published by the Free Software
13  *    Foundation, version 2, or
14  *
15  * b) the "Angband licence":
16  *    This software may be copied and distributed for educational, research,
17  *    and not for profit purposes provided that this copyright and statement
18  *    are included in all such copies.  Other copyrights may also apply.
19  */
20 
21 #ifndef PLAYER_H
22 #define PLAYER_H
23 
24 #include "guid.h"
25 #include "obj-properties.h"
26 #include "object.h"
27 #include "option.h"
28 
29 /**
30  * Indexes of the player stats (hard-coded by savefiles).
31  */
32 enum {
33 	#define STAT(a) STAT_##a,
34 	#include "list-stats.h"
35 	#undef STAT
36 
37 	STAT_MAX
38 };
39 
40 /**
41  * Player race and class flags
42  */
43 enum
44 {
45 	#define PF(a) PF_##a,
46 	#include "list-player-flags.h"
47 	#undef PF
48 	PF_MAX
49 };
50 
51 #define PF_SIZE                FLAG_SIZE(PF_MAX)
52 
53 #define pf_has(f, flag)        flag_has_dbg(f, PF_SIZE, flag, #f, #flag)
54 #define pf_next(f, flag)       flag_next(f, PF_SIZE, flag)
55 #define pf_is_empty(f)         flag_is_empty(f, PF_SIZE)
56 #define pf_is_full(f)          flag_is_full(f, PF_SIZE)
57 #define pf_is_inter(f1, f2)    flag_is_inter(f1, f2, PF_SIZE)
58 #define pf_is_subset(f1, f2)   flag_is_subset(f1, f2, PF_SIZE)
59 #define pf_is_equal(f1, f2)    flag_is_equal(f1, f2, PF_SIZE)
60 #define pf_on(f, flag)         flag_on_dbg(f, PF_SIZE, flag, #f, #flag)
61 #define pf_off(f, flag)        flag_off(f, PF_SIZE, flag)
62 #define pf_wipe(f)             flag_wipe(f, PF_SIZE)
63 #define pf_setall(f)           flag_setall(f, PF_SIZE)
64 #define pf_negate(f)           flag_negate(f, PF_SIZE)
65 #define pf_copy(f1, f2)        flag_copy(f1, f2, PF_SIZE)
66 #define pf_union(f1, f2)       flag_union(f1, f2, PF_SIZE)
67 #define pf_inter(f1, f2)       flag_inter(f1, f2, PF_SIZE)
68 #define pf_diff(f1, f2)        flag_diff(f1, f2, PF_SIZE)
69 
70 /**
71  * The range of possible indexes into tables based upon stats.
72  * Currently things range from 3 to 18/220 = 40.
73  */
74 #define STAT_RANGE 38
75 
76 /**
77  * Player constants
78  */
79 #define PY_MAX_EXP		99999999L	/* Maximum exp */
80 #define PY_KNOW_LEVEL	30			/* Level to know all runes */
81 #define PY_MAX_LEVEL	50			/* Maximum level */
82 
83 /**
84  * Flags for player.spell_flags[]
85  */
86 #define PY_SPELL_LEARNED    0x01 	/* Spell has been learned */
87 #define PY_SPELL_WORKED     0x02 	/* Spell has been successfully tried */
88 #define PY_SPELL_FORGOTTEN  0x04 	/* Spell has been forgotten */
89 
90 #define BTH_PLUS_ADJ    	3 		/* Adjust BTH per plus-to-hit */
91 
92 /**
93  * Ways in which players can be marked as cheaters
94  */
95 #define NOSCORE_WIZARD		0x0002
96 #define NOSCORE_DEBUG		0x0008
97 #define NOSCORE_JUMPING     0x0010
98 
99 /**
100  * Terrain that the player has a chance of digging through
101  */
102 enum {
103 	DIGGING_RUBBLE = 0,
104 	DIGGING_MAGMA,
105 	DIGGING_QUARTZ,
106 	DIGGING_GRANITE,
107 	DIGGING_DOORS,
108 
109 	DIGGING_MAX
110 };
111 
112 /**
113  * Skill indexes
114  */
115 enum {
116 	SKILL_DISARM_PHYS,		/* Disarming - physical */
117 	SKILL_DISARM_MAGIC,		/* Disarming - magical */
118 	SKILL_DEVICE,			/* Magic Devices */
119 	SKILL_SAVE,				/* Saving throw */
120 	SKILL_SEARCH,			/* Searching ability */
121 	SKILL_STEALTH,			/* Stealth factor */
122 	SKILL_TO_HIT_MELEE,		/* To hit (normal) */
123 	SKILL_TO_HIT_BOW,		/* To hit (shooting) */
124 	SKILL_TO_HIT_THROW,		/* To hit (throwing) */
125 	SKILL_DIGGING,			/* Digging */
126 
127 	SKILL_MAX
128 };
129 
130 /**
131  * Structure for the "quests"
132  */
133 struct quest
134 {
135 	struct quest *next;
136 	byte index;
137 	char *name;
138 	byte level;					/* Dungeon level */
139 	struct monster_race *race;	/* Monster race */
140 	int cur_num;				/* Number killed (unused) */
141 	int max_num;				/* Number required (unused) */
142 };
143 
144 /**
145  * A single equipment slot
146  */
147 struct equip_slot {
148 	struct equip_slot *next;
149 
150 	u16b type;
151 	char *name;
152 	struct object *obj;
153 };
154 
155 /**
156  * A player 'body'
157  */
158 struct player_body {
159 	struct player_body *next;
160 
161 	char *name;
162 	u16b count;
163 	struct equip_slot *slots;
164 };
165 
166 /**
167  * Player race info
168  */
169 struct player_race {
170 	struct player_race *next;
171 	const char *name;
172 
173 	unsigned int ridx;
174 
175 	int r_mhp;		/**< Hit-dice modifier */
176 	int r_exp;		/**< Experience factor */
177 
178 	int b_age;		/**< Base age */
179 	int m_age;		/**< Mod age */
180 
181 	int base_hgt;	/**< Base height */
182 	int mod_hgt;	/**< Mod height */
183 	int base_wgt;	/**< Base weight */
184 	int mod_wgt;	/**< Mod weight */
185 
186 	int infra;		/**< Infra-vision range */
187 
188 	int body;		/**< Race body */
189 
190 	int r_adj[STAT_MAX];		/**< Stat bonuses */
191 
192 	int r_skills[SKILL_MAX];	/**< Skills */
193 
194 	bitflag flags[OF_SIZE];		/**< Racial (object) flags */
195 	bitflag pflags[PF_SIZE];	/**< Racial (player) flags */
196 
197 	struct history_chart *history;
198 
199 	struct element_info el_info[ELEM_MAX]; /**< Resists */
200 };
201 
202 /**
203  * Blow names for shapechanged players
204  */
205 struct player_blow {
206 	struct player_blow *next;
207 	char *name;
208 };
209 
210 /**
211  * Player shapechange shape info
212  */
213 struct player_shape {
214 	struct player_shape *next;
215 	const char *name;
216 
217 	int sidx;
218 
219 	int to_a;				/**< Plusses to AC */
220 	int to_h;				/**< Plusses to hit */
221 	int to_d;				/**< Plusses to damage */
222 
223 	int skills[SKILL_MAX];  /**< Skills */
224 	bitflag flags[OF_SIZE];		/**< Shape (object) flags */
225 	bitflag pflags[PF_SIZE];	/**< Shape (player) flags */
226 	int modifiers[OBJ_MOD_MAX];	/**< Stat and other modifiers*/
227 	struct element_info el_info[ELEM_MAX]; /**< Resists */
228 
229 	struct effect *effect;	/**< Effect on taking this shape (effects.c) */
230 
231 	struct player_blow *blows;
232 	int num_blows;
233 };
234 
235 /**
236  * Items the player starts with.  Used in player_class and specified in
237  * class.txt.
238  */
239 struct start_item {
240 	int tval;	/**< General object type (see TV_ macros) */
241 	int sval;	/**< Object sub-type  */
242 	int min;	/**< Minimum starting amount */
243 	int max;	/**< Maximum starting amount */
244 
245 	struct start_item *next;
246 };
247 
248 /**
249  * Structure for magic realms
250  */
251 struct magic_realm {
252 	struct magic_realm *next;
253 	char *code;
254 	char *name;
255 	int stat;
256 	char *verb;
257 	char *spell_noun;
258 	char *book_noun;
259 };
260 
261 /**
262  * A structure to hold class-dependent information on spells.
263  */
264 struct class_spell {
265 	char *name;
266 	char *text;
267 
268 	struct effect *effect;	/**< The spell's effect */
269 	const struct magic_realm *realm;	/**< The magic realm of this spell */
270 
271 	int sidx;				/**< The index of this spell for this class */
272 	int bidx;				/**< The index into the player's books array */
273 	int slevel;				/**< Required level (to learn) */
274 	int smana;				/**< Required mana (to cast) */
275 	int sfail;				/**< Base chance of failure */
276 	int sexp;				/**< Encoded experience bonus */
277 };
278 
279 /**
280  * A structure to hold class-dependent information on spell books.
281  */
282 struct class_book {
283 	int tval;							/**< Item type of the book */
284 	int sval;							/**< Item sub-type for book */
285 	bool dungeon;						/**< Whether this is a dungeon book */
286 	int num_spells;						/**< Number of spells in this book */
287 	const struct magic_realm *realm;	/**< The magic realm of this book */
288 	struct class_spell *spells;			/**< Spells in the book*/
289 };
290 
291 /**
292  * Information about class magic knowledge
293  */
294 struct class_magic {
295 	int spell_first;			/**< Level of first spell */
296 	int spell_weight;			/**< Max armor weight to avoid mana penalties */
297 	int num_books;				/**< Number of spellbooks */
298 	struct class_book *books;	/**< Details of spellbooks */
299 	int total_spells;			/**< Number of spells for this class */
300 };
301 
302 /**
303  * Player class info
304  */
305 struct player_class {
306 	struct player_class *next;
307 	const char *name;
308 	unsigned int cidx;
309 
310 	const char *title[10];		/**< Titles */
311 
312 	int c_adj[STAT_MAX];		/**< Stat modifier */
313 
314 	int c_skills[SKILL_MAX];	/**< Class skills */
315 	int x_skills[SKILL_MAX];	/**< Extra skills */
316 
317 	int c_mhp;					/**< Hit-dice adjustment */
318 	int c_exp;					/**< Experience factor */
319 
320 	bitflag flags[OF_SIZE];		/**< (Object) flags */
321 	bitflag pflags[PF_SIZE];	/**< (Player) flags */
322 
323 	int max_attacks;			/**< Maximum possible attacks */
324 	int min_weight;				/**< Minimum weapon weight for calculations */
325 	int att_multiply;			/**< Multiplier for attack calculations */
326 
327 	struct start_item *start_items; /**< Starting inventory */
328 
329 	struct class_magic magic;	/**< Magic spells */
330 };
331 
332 /**
333  * Info for player abilities
334  */
335 struct player_ability {
336 	struct player_ability *next;
337 	u16b index;			/* PF_*, OF_* or element index */
338 	char *type;			/* Ability type */
339 	char *name;			/* Ability name */
340 	char *desc;			/* Ability description */
341 	int group;			/* Ability group (set locally when viewing) */
342 	int value;			/* Resistance value for elements */
343 };
344 
345 /**
346  * Histories are a graph of charts; each chart contains a set of individual
347  * entries for that chart, and each entry contains a text description and a
348  * successor chart to move history generation to.
349  * For example:
350  * 	chart 1 {
351  * 		entry {
352  * 			desc "You are the illegitimate and unacknowledged child";
353  * 			next 2;
354  * 		};
355  * 		entry {
356  * 			desc "You are the illegitimate but acknowledged child";
357  * 			next 2;
358  * 		};
359  * 		entry {
360  * 			desc "You are one of several children";
361  * 			next 3;
362  * 		};
363  * 	};
364  *
365  * History generation works by walking the graph from the starting chart for
366  * each race, picking a random entry (with weighted probability) each time.
367  */
368 struct history_entry {
369 	struct history_entry *next;
370 	struct history_chart *succ;
371 	int isucc;
372 	int roll;
373 	char *text;
374 };
375 
376 struct history_chart {
377 	struct history_chart *next;
378 	struct history_entry *entries;
379 	unsigned int idx;
380 };
381 
382 /**
383  * Player history information
384  *
385  * See player-history.c/.h
386  */
387 struct player_history {
388 	struct history_info *entries;	/**< List of entries */
389 	size_t next;					/**< First unused entry */
390 	size_t length;					/**< Current length */
391 };
392 
393 /**
394  * All the variable state that changes when you put on/take off equipment.
395  * Player flags are not currently variable, but useful here so monsters can
396  * learn them.
397  */
398 struct player_state {
399 	int stat_add[STAT_MAX];	/**< Equipment stat bonuses */
400 	int stat_ind[STAT_MAX];	/**< Indexes into stat tables */
401 	int stat_use[STAT_MAX];	/**< Current modified stats */
402 	int stat_top[STAT_MAX];	/**< Maximal modified stats */
403 
404 	int skills[SKILL_MAX];		/**< Skills */
405 
406 	int speed;			/**< Current speed */
407 
408 	int num_blows;		/**< Number of blows x100 */
409 	int num_shots;		/**< Number of shots x10 */
410 	int num_moves;		/**< Number of extra movement actions */
411 
412 	int ammo_mult;		/**< Ammo multiplier */
413 	int ammo_tval;		/**< Ammo variety */
414 
415 	int ac;				/**< Base ac */
416 	int dam_red;		/**< Damage reduction */
417 	int perc_dam_red;	/**< Percentage damage reduction */
418 	int to_a;			/**< Bonus to ac */
419 	int to_h;			/**< Bonus to hit */
420 	int to_d;			/**< Bonus to dam */
421 
422 	int see_infra;		/**< Infravision range */
423 
424 	int cur_light;		/**< Radius of light (if any) */
425 
426 	bool heavy_wield;	/**< Heavy weapon */
427 	bool heavy_shoot;	/**< Heavy shooter */
428 	bool bless_wield;	/**< Blessed (or blunt) weapon */
429 
430 	bool cumber_armor;	/**< Mana draining armor */
431 
432 	bitflag flags[OF_SIZE];					/**< Status flags from race and items */
433 	bitflag pflags[PF_SIZE];				/**< Player intrinsic flags */
434 	struct element_info el_info[ELEM_MAX];	/**< Resists from race and items */
435 };
436 
437 #define player_has(p, flag)       (pf_has(p->state.pflags, (flag)))
438 
439 /**
440  * Temporary, derived, player-related variables used during play but not saved
441  *
442  * XXX Some of these probably should go to the UI
443  */
444 struct player_upkeep {
445 	bool playing;			/* True if player is playing */
446 	bool autosave;			/* True if autosave is pending */
447 	bool generate_level;	/* True if level needs regenerating */
448 	bool only_partial;		/* True if only partial updates are needed */
449 	bool dropping;			/* True if auto-drop is in progress */
450 
451 	int energy_use;			/* Energy use this turn */
452 	int new_spells;			/* Number of spells available */
453 
454 	struct monster *health_who;			/* Health bar trackee */
455 	struct monster_race *monster_race;	/* Monster race trackee */
456 	struct object *object;				/* Object trackee */
457 	struct object_kind *object_kind;	/* Object kind trackee */
458 
459 	u32b notice;			/* Bit flags for pending actions such as
460 							 * reordering inventory, ignoring, etc. */
461 	u32b update;			/* Bit flags for recalculations needed
462 							 * such as HP, or visible area */
463 	u32b redraw;			/* Bit flags for things that /have/ changed,
464 							 * and just need to be redrawn by the UI,
465 							 * such as HP, Speed, etc.*/
466 
467 	int command_wrk;		/* Used by the UI to decide whether
468 							 * to start off showing equipment or
469 							 * inventory listings when offering
470 							 * a choice.  See obj-ui.c */
471 
472 	bool create_up_stair;	/* Create up stair on next level */
473 	bool create_down_stair;	/* Create down stair on next level */
474 	bool light_level;		/* Level is to be lit on creation */
475 	bool arena_level;		/* Current level is an arena */
476 
477 	int resting;			/* Resting counter */
478 
479 	int running;				/* Running counter */
480 	bool running_withpathfind;	/* Are we using the pathfinder ? */
481 	bool running_firststep;		/* Is this our first step running? */
482 
483 	struct object **quiver;	/* Quiver objects */
484 	struct object **inven;	/* Inventory objects */
485 	int total_weight;		/* Total weight being carried */
486 	int inven_cnt;			/* Number of items in inventory */
487 	int equip_cnt;			/* Number of items in equipment */
488 	int quiver_cnt;			/* Number of items in the quiver */
489 	int recharge_pow;		/* Power of recharge effect */
490 };
491 
492 /**
493  * Most of the "player" information goes here.
494  *
495  * This stucture gives us a large collection of player variables.
496  *
497  * This entire structure is wiped when a new character is born.
498  *
499  * This structure is more or less laid out so that the information
500  * which must be saved in the savefile precedes all the information
501  * which can be recomputed as needed.
502  */
503 struct player {
504 	const struct player_race *race;
505 	const struct player_class *class;
506 
507 	struct loc grid;/* Player location */
508 
509 	byte hitdie;	/* Hit dice (sides) */
510 	byte expfact;	/* Experience factor */
511 
512 	s16b age;		/* Characters age */
513 	s16b ht;		/* Height */
514 	s16b wt;		/* Weight */
515 
516 	s32b au;		/* Current Gold */
517 
518 	s16b max_depth;	/* Max depth */
519 	s16b recall_depth;	/* Recall depth */
520 	s16b depth;		/* Cur depth */
521 
522 	s16b max_lev;	/* Max level */
523 	s16b lev;		/* Cur level */
524 
525 	s32b max_exp;	/* Max experience */
526 	s32b exp;		/* Cur experience */
527 	u16b exp_frac;	/* Cur exp frac (times 2^16) */
528 
529 	s16b mhp;		/* Max hit pts */
530 	s16b chp;		/* Cur hit pts */
531 	u16b chp_frac;	/* Cur hit frac (times 2^16) */
532 
533 	s16b msp;		/* Max mana pts */
534 	s16b csp;		/* Cur mana pts */
535 	u16b csp_frac;	/* Cur mana frac (times 2^16) */
536 
537 	s16b stat_max[STAT_MAX];	/* Current "maximal" stat values */
538 	s16b stat_cur[STAT_MAX];	/* Current "natural" stat values */
539 	s16b stat_map[STAT_MAX];	/* Tracks remapped stats from temp stat swap */
540 
541 	s16b *timed;				/* Timed effects */
542 
543 	s16b word_recall;			/* Word of recall counter */
544 	s16b deep_descent;			/* Deep Descent counter */
545 
546 	s16b energy;				/* Current energy */
547 	u32b total_energy;			/* Total energy used (including resting) */
548 	u32b resting_turn;			/* Number of player turns spent resting */
549 
550 	s16b food;					/* Current nutrition */
551 
552 	byte unignoring;			/* Unignoring */
553 
554 	byte *spell_flags;			/* Spell flags */
555 	byte *spell_order;			/* Spell order */
556 
557 	char full_name[PLAYER_NAME_LEN];	/* Full name */
558 	char died_from[80];					/* Cause of death */
559 	char *history;						/* Player history */
560 	struct quest *quests;				/* Quest history */
561 	u16b total_winner;					/* Total winner */
562 
563 	u16b noscore;				/* Cheating flags */
564 
565 	bool is_dead;				/* Player is dead */
566 
567 	bool wizard;				/* Player is in wizard mode */
568 
569 	s16b player_hp[PY_MAX_LEVEL];		/* HP gained per level */
570 
571 	/* Saved values for quickstart */
572 	s32b au_birth;						/* Birth gold when option birth_money is false */
573 	s16b stat_birth[STAT_MAX];			/* Birth "natural" stat values */
574 	s16b ht_birth;						/* Birth Height */
575 	s16b wt_birth;						/* Birth Weight */
576 
577 	struct player_options opts;			/* Player options */
578 	struct player_history hist;			/* Player history (see player-history.c) */
579 
580 	struct player_body body;			/* Equipment slots available */
581 	struct player_shape *shape;			/* Current player shape */
582 
583 	struct object *gear;				/* Real gear */
584 	struct object *gear_k;				/* Known gear */
585 
586 	struct object *obj_k;				/* Object knowledge ("runes") */
587 	struct chunk *cave;					/* Known version of current level */
588 
589 	struct player_state state;			/* Calculatable state */
590 	struct player_state known_state;	/* What the player can know of the above */
591 	struct player_upkeep *upkeep;		/* Temporary player-related values */
592 };
593 
594 
595 /**
596  * ------------------------------------------------------------------------
597  * Externs
598  * ------------------------------------------------------------------------ */
599 
600 extern struct player_body *bodies;
601 extern struct player_race *races;
602 extern struct player_shape *shapes;
603 extern struct player_class *classes;
604 extern struct player_ability *player_abilities;
605 extern struct magic_realm *realms;
606 
607 extern const s32b player_exp[PY_MAX_LEVEL];
608 extern struct player *player;
609 
610 /* player-class.c */
611 struct player_class *player_id2class(guid id);
612 
613 /* player.c */
614 int stat_name_to_idx(const char *name);
615 const char *stat_idx_to_name(int type);
616 const struct magic_realm *lookup_realm(const char *code);
617 bool player_stat_inc(struct player *p, int stat);
618 bool player_stat_dec(struct player *p, int stat, bool permanent);
619 void player_exp_gain(struct player *p, s32b amount);
620 void player_exp_lose(struct player *p, s32b amount, bool permanent);
621 void player_flags(struct player *p, bitflag f[OF_SIZE]);
622 void player_flags_timed(struct player *p, bitflag f[OF_SIZE]);
623 byte player_hp_attr(struct player *p);
624 byte player_sp_attr(struct player *p);
625 bool player_restore_mana(struct player *p, int amt);
626 void player_safe_name(char *safe, size_t safelen, const char *name, bool strip_suffix);
627 void player_cleanup_members(struct player *p);
628 
629 /* player-race.c */
630 struct player_race *player_id2race(guid id);
631 
632 #endif /* !PLAYER_H */
633