1 /*
2  * monsters.h
3  * Copyright (C) 2009-2020 Joachim de Groot <jdegroot@web.de>
4  *
5  * NLarn is free software: you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * NLarn is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef __MONSTERS_H_
20 #define __MONSTERS_H_
21 
22 #include <glib.h>
23 #include <time.h>
24 
25 #include "cJSON.h"
26 #include "effects.h"
27 #include "enumFactory.h"
28 #include "inventory.h"
29 #include "items.h"
30 #include "position.h"
31 #include "utils.h"
32 
33 /* forward declarations */
34 
35 struct _monster;
36 typedef struct _monster monster;
37 
38 struct game;
39 struct player;
40 struct map;
41 
42 #define MONSTER_TYPE_ENUM(MT) \
43     /* D1 - D4 */ \
44     MT(MT_GIANT_BAT,) \
45     MT(MT_GNOME,) \
46     MT(MT_HOBGOBLIN,) \
47     MT(MT_JACKAL,) \
48     /* D2 - D4 */ \
49     MT(MT_KOBOLD,)          /*  5 */ \
50     MT(MT_ORC,) \
51     MT(MT_SNAKE,) \
52     MT(MT_CENTIPEDE,) \
53     MT(MT_JACULUS,) \
54     MT(MT_TROGLODYTE,)      /* 10 */ \
55     /* D3 - D5 */ \
56     MT(MT_GIANT_ANT,) \
57     MT(MT_FLOATING_EYE,) \
58     MT(MT_LEPRECHAUN,) \
59     MT(MT_NYMPH,) \
60     MT(MT_QUASIT,)          /* 15 */ \
61     MT(MT_RUST_MONSTER,) \
62     /* D4 - D6 */ \
63     MT(MT_ZOMBIE,) \
64     MT(MT_ASSASSIN_BUG,) \
65     MT(MT_BUGBEAR,) \
66     MT(MT_HELLHOUND,)       /* 20 */ \
67     MT(MT_ICE_LIZARD,) \
68     /* D5 - D7 */ \
69     MT(MT_CENTAUR,) \
70     MT(MT_TROLL,) \
71     MT(MT_YETI,) \
72     MT(MT_ELF,)             /* 25 */ \
73     MT(MT_GELATINOUSCUBE,) \
74     /* D6 - D8 */ \
75     MT(MT_WHITE_DRAGON,) \
76     MT(MT_METAMORPH,) \
77     MT(MT_VORTEX,) \
78     MT(MT_ZILLER,)          /* 30 */ \
79     MT(MT_VIOLET_FUNGUS,) \
80     MT(MT_WRAITH,) \
81     /* D7 - D9 */ \
82     MT(MT_FORVALAKA,) \
83     MT(MT_LAMA_NOBE,) \
84     MT(MT_OSQUIP,)          /* 35 */ \
85     MT(MT_ROTHE,) \
86     MT(MT_XORN,) \
87     MT(MT_VAMPIRE,) \
88     /* D8 - D10 */ \
89     MT(MT_STALKER,) \
90     MT(MT_POLTERGEIST,)     /* 40 */ \
91     MT(MT_DISENCHANTRESS,) \
92     /* D9 - V1 */ \
93     MT(MT_SHAMBLINGMOUND,) \
94     MT(MT_YELLOW_MOLD,) \
95     MT(MT_UMBER_HULK,) \
96     MT(MT_GNOME_KING,)      /* 45 */ \
97     /* D10 - V2 */ \
98     MT(MT_MIMIC,) \
99     MT(MT_WATER_LORD,) \
100     MT(MT_PURPLE_WORM,) \
101     MT(MT_XVART,) \
102     /* V1 - V3 */ \
103     MT(MT_BRONZE_DRAGON,)   /* 50 */ \
104     MT(MT_GREEN_DRAGON,) \
105     MT(MT_SILVER_DRAGON,) \
106     /* V2 - V3 */ \
107     MT(MT_PLATINUM_DRAGON,) \
108     MT(MT_RED_DRAGON,) \
109     MT(MT_SPIRIT_NAGA,)     /* 55 */ \
110     /* V3 */ \
111     MT(MT_GREEN_URCHIN,) \
112     MT(MT_DEMONLORD_I,) \
113     MT(MT_DEMONLORD_II,) \
114     MT(MT_DEMONLORD_III,) \
115     MT(MT_DEMONLORD_IV,)    /* 60 */ \
116     MT(MT_DEMONLORD_V,) \
117     MT(MT_DEMONLORD_VI,) \
118     MT(MT_DEMONLORD_VII,) \
119     /* not actually generated randomly */ \
120     MT(MT_DEMON_PRINCE,) \
121     MT(MT_TOWN_PERSON,) \
122     MT(MT_MAX,)                /* maximum # monsters in the dungeon */
123 
124 DECLARE_ENUM(monster_t, MONSTER_TYPE_ENUM)
125 
126 typedef enum monster_action_type
127 {
128     MA_NONE,
129     MA_FLEE,
130     MA_REMAIN,
131     MA_WANDER,
132     MA_ATTACK,
133     MA_CONFUSION,
134     MA_SERVE,
135     MA_CIVILIAN,
136 } monster_action_t;
137 
138 #define MONSTER_FLAG_ENUM(MF) \
139     MF(HEAD         , = 1)       /* has a head */ \
140     MF(NOBEHEAD     , = 1 << 1)  /* cannot be beheaded */ \
141     MF(HANDS        , = 1 << 2)  /* has hands => can open doors */ \
142     MF(FLY          , = 1 << 3)  /* can fly (not affected by pits and trapdoors) */ \
143     MF(SPIRIT       , = 1 << 4)  /* is a spirit */ \
144     MF(UNDEAD       , = 1 << 5)  /* is undead */ \
145     MF(INVISIBLE    , = 1 << 6)  /* is invisible */ \
146     MF(INFRAVISION  , = 1 << 7)  /* can see invisible */ \
147     MF(REGENERATE   , = 1 << 8)  /* does regenerate */ \
148     MF(METALLIVORE  , = 1 << 9)  /* eats metal */ \
149     MF(DEMON        , = 1 << 10) /* is a demon */ \
150     MF(DRAGON       , = 1 << 11) /* is a dragon */ \
151     MF(MIMIC        , = 1 << 12) /* is a mimic */ \
152     MF(RES_FIRE     , = 1 << 13) /* resistant to fire (half damage)*/ \
153     MF(RES_COLD     , = 1 << 14) /* resistant to cold */ \
154     MF(RES_ELEC     , = 1 << 15) /* resistant to electricity */ \
155     MF(RES_SLEEP    , = 1 << 16) /* resistant to sleep */ \
156     MF(RES_POISON   , = 1 << 17) /* resistant to poison */ \
157     MF(RES_CONF     , = 1 << 18) /* resistant to confusion */ \
158     MF(RES_MAGIC    , = 1 << 19) /* resistant to magic */ \
159     MF(SWIM         , = 1 << 20) /* can swim through water */ \
160     MF(PACK         , = 1 << 21) /* creature appears in packs */ \
161 
162 #define MONSTER_FLAG_COUNT 20
163 
164 DECLARE_ENUM(monster_flag, MONSTER_FLAG_ENUM)
165 
166 /* function definitions */
167 
168 monster *monster_new(monster_t type, position pos, gpointer leader);
169 monster *monster_new_by_level(position pos);
170 void monster_destroy(monster *m);
171 
172 void monster_serialize(gpointer oid, monster *m, cJSON *root);
173 void monster_deserialize(cJSON *mser, struct game *g);
174 
175 /* getters / setters */
176 
177 int monster_hp_max(monster *m);
178 int monster_hp(monster *m);
179 void monster_hp_inc(monster *m, int amount);
180 gpointer monster_oid(monster *m);
181 position monster_pos(monster *m);
182 int monster_pos_set(monster *m, struct map *mp, position target);
183 int monster_valid_dest(struct map *m, position pos, int map_elem);
184 monster_t monster_type(monster *m);
185 gboolean monster_unknown(monster *m);
186 void monster_unknown_set(monster *m, gboolean what);
187 
188 /**
189  * @brief Return the monster's inventory.
190  *
191  * @param A monster.
192  * @return The given monster's inventory.
193  */
194 inventory **monster_inv(monster *m);
195 
196 gboolean monster_in_sight(monster *m);
197 
198 /** @brief Get the currently set AI action for a given monster.
199   *
200   * @param A monster.
201   * @return The currently set AI action for the given monster.
202   */
203 monster_action_t monster_action(monster *m);
204 
205 /* other functions */
206 const char *monster_get_name(monster *m);
207 const char* monster_type_plural_name(monster_t mt, const int count);
208 void monster_die(monster *m, struct player *p);
209 
210 void monster_level_enter(monster *m, struct map *l);
211 void monster_move(gpointer *oid, monster *m, struct game *g);
212 
213 void monster_polymorph(monster *m);
214 
215 /**
216  * check stash at monster's position for something desired
217  *
218  * @param a monster
219  * @return TRUE if something has been picked up, FALSE if not
220  */
221 int monster_items_pickup(monster *m);
222 
223 /**
224  * Returns the number of attack type a monster can choose from
225  *
226  * @param a monster
227  * @return the number of attacks
228  */
229 guint monster_attack_count(monster *m);
230 
231 /**
232  * Returns the chosen attack type for the monster
233  *
234  * @param a monster
235  * @param the number of an attack
236  * @return an attack
237  */
238 attack monster_attack(monster *m, guint num);
239 
240 void monster_player_attack(monster *m, struct player *p);
241 int monster_player_ranged_attack(monster *m, struct player *p);
242 
243 /**
244  * Deal damage to a monster
245  *
246  * @param monster
247  * @param pointer to the damage to be dealt (will be free'd)
248  * @return the monster if it has survived, othewise NULL
249  */
250 monster *monster_damage_take(monster *m, damage *dam);
251 
252 /**
253  * Determine a monster's action.
254  *
255  * @param the monster
256  * @param manually set action for a monster
257  * @return TRUE if the action has changed
258  */
259 gboolean monster_update_action(monster *m, monster_action_t override);
260 
261 void monster_update_player_pos(monster *m, position ppos);
262 gboolean monster_regenerate(monster *m, time_t gtime, int difficulty);
263 
264 item *get_mimic_item(monster *m);
265 char *monster_desc(monster *m);
266 char monster_glyph(monster *m);
267 int monster_color(monster *m);
268 
269 /* dealing with temporary effects */
270 effect *monster_effect_add(monster *m, effect *e);
271 int monster_effect_del(monster *m, effect *e);
272 effect *monster_effect_get(monster *m , effect_t type);
273 int monster_effect(monster *m, effect_t type);
274 void monster_effects_expire(monster *m);
275 int monster_is_carrying_item(monster *m, item_t type);
276 
277 /* query monster data */
278 const char *monster_name(monster *m);
279 int monster_level(monster *m);
280 int monster_ac(monster *m);
281 guint monster_int(monster *m);
282 int monster_gold_chance(monster *m);
283 int monster_gold_amount(monster *m);
284 int monster_exp(monster *m);
285 int monster_size(monster *m);
286 int monster_speed(monster *m);
287 int monster_flags(monster *m, monster_flag f);
288 const char *monster_sound(monster *m);
289 
290 #define monster_map(M)      game_map(nlarn, Z(monster_pos(M)))
291 
292 /* query monster type data */
293 int monster_type_hp_max(monster_t type);
294 char monster_type_glyph(monster_t type);
295 const char *monster_type_name(monster_t type);
296 int monster_type_reroll_chance(monster_t type);
297 
298 void monster_genocide(monster_t monster_id);
299 int monster_is_genocided(monster_t monster_id);
300 
301 #endif
302