3 * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
4 * or any later version. For details on its copyright, license, and warranties,
5 * see the file NOTICE in the root directory of the PlomRogue source package.
8 #define _POSIX_C_SOURCE 200809L /* strdup() */
10 #include <stddef.h> /* NULL, size_t */
11 #include <stdint.h> /* uint8_t, uint16_t, int16_t, UINT8_MAX, UINT16_MAX */
12 #include <stdlib.h> /* free() */
13 #include <string.h> /* memset(), strcmp(), strdup(), strlen() */
14 #include "../common/rexit.h" /* exit_err() */
15 #include "../common/try_malloc.h" /* try_malloc() */
16 #include "../common/yx_uint8.h" /* yx_uint8 */
17 #include "cleanup.h" /* set_cleanup_flag() */
18 #include "hardcoded_strings.h" /* s */
19 #include "field_of_view.h" /* build_fov_map() */
20 #include "map.h" /* mv_yx_in_dir_legal() */
21 #include "rrand.h" /* rrand() */
22 #include "thing_actions.h" /* actor_wait */
23 #include "world.h" /* world */
27 /* Used to treat structs Thing, ThingType and ThingAction the same. */
30 struct NextAndId * next;
36 /* To linked list of NextAndId structs (or rather structs whose start region is
37 * compatible to it) starting at "start", add newly allocated element of
38 * "n_size" and an ID that is either "id" or, if "id" is <= UINT8_MAX and >=
39 * "id_start", get lowest ID >= "start_id" and <= UINT8_MAX for new thing
40 * ("struct_id"=0), thing type ("struct_id"=1) or thing action ("struct_id"=2).
42 static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id,
43 int16_t id, uint8_t struct_id,
44 struct NextAndId ** start);
48 static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id,
49 int16_t id, uint8_t struct_id,
50 struct NextAndId ** start)
52 struct NextAndId * nai = try_malloc(n_size, __func__);
53 memset(nai, 0, n_size);
54 if (start_id <= id && id <= UINT8_MAX)
62 if ( (0 == struct_id && !get_thing(world.things, start_id, 1))
63 || (1 == struct_id && !get_thing_type(start_id))
64 || (2 == struct_id && !get_thing_action(start_id)))
69 char * err = "No unused ID available to add to ID list.";
70 exit_err(start_id == UINT8_MAX, err);
74 struct NextAndId ** nai_ptr_ptr = start;
75 for (; * nai_ptr_ptr; nai_ptr_ptr = &(*nai_ptr_ptr)->next);
82 extern struct ThingAction * add_thing_action(uint8_t id)
84 struct ThingAction * ta;
85 ta = (struct ThingAction *) add_to_struct_list(sizeof(struct ThingAction),
88 &world.thing_actions);
89 set_cleanup_flag(CLEANUP_THING_ACTIONS);
90 ta->name = strdup(s[S_CMD_WAIT]);
92 ta->func = actor_wait;
98 extern struct ThingType * add_thing_type(int16_t id)
100 struct ThingType * tt;
101 tt = (struct ThingType *) add_to_struct_list(sizeof(struct ThingType),
103 (struct NextAndId **)
105 set_cleanup_flag(CLEANUP_THING_TYPES);
106 tt->name = strdup("(none)");
107 tt->corpse_id = tt->id;
113 extern struct Thing * add_thing(int16_t id, uint8_t type, uint8_t y, uint8_t x)
116 t = (struct Thing *) add_to_struct_list(sizeof(struct Thing), 0, id, 0,
117 (struct NextAndId **)&world.things);
118 struct ThingType * tt = get_thing_type(type);
119 set_cleanup_flag(CLEANUP_THINGS);
121 t->lifepoints = tt->lifepoints;
124 if (t->lifepoints && world.exists)
133 extern void add_thing_to_memory_map(struct Thing * t, uint8_t type,
134 uint8_t y, uint8_t x)
136 struct ThingInMemory * tm=try_malloc(sizeof(struct ThingInMemory),__func__);
146 extern void free_thing_actions(struct ThingAction * ta)
152 free_thing_actions(ta->next);
159 extern void free_thing_types(struct ThingType * tt)
165 free_thing_types(tt->next);
172 extern void free_things(struct Thing * t)
178 free_things(t->owns);
179 free_things(t->next);
182 free_things_in_memory(t->t_mem);
184 if (t == world.things) /* So add_things()' NULL-delimited thing */
185 { /* iteration loop does not iterate over */
186 world.things = NULL; /* freed memory when called the first time */
187 } /* after world re-seeding. */
192 extern void free_things_in_memory(struct ThingInMemory * tm)
198 free_things_in_memory(tm->next);
204 extern struct ThingAction * get_thing_action(uint8_t id)
206 struct ThingAction * ta = world.thing_actions;
207 for (; ta && id != ta->id; ta = ta->next);
213 extern struct ThingType * get_thing_type(uint8_t id)
215 struct ThingType * tt = world.thing_types;
216 for (; tt && id != tt->id; tt = tt->next);
222 extern uint8_t get_thing_action_id_by_name(char * name)
224 struct ThingAction * ta = world.thing_actions;
227 if (0 == strcmp(ta->name, name))
242 extern struct Thing * get_thing(struct Thing * ptr, uint8_t id, uint8_t deep)
246 if (!ptr || id == ptr->id)
252 struct Thing * owned_thing = get_thing(ptr->owns, id, 1);
264 extern struct Thing * get_player()
266 return get_thing(world.things, 0, 0);
271 extern void try_thing_proliferation(struct Thing * t)
273 struct ThingType * tt = get_thing_type(t->type);
276 if (1 == tt->proliferate || 1 == (rrand() % tt->proliferate))
278 struct yx_uint8 candidates[6];
279 uint8_t n_candidates = 0;
280 char dirs[7] = "dxswed";
281 struct yx_uint8 start = t->pos;
283 for (i = 0; i < strlen(dirs); i++)
285 if ( mv_yx_in_dir_legal(dirs[i], &start)
286 && '.' == world.map.cells[start.y*world.map.length+start.x])
291 for (t = world.things; t; t = t->next)
294 && start.y == t->pos.y && start.x == t->pos.x)
302 candidates[n_candidates] = start;
311 i = rrand() % n_candidates;
312 add_thing(-1, tt->id, candidates[i].y, candidates[i].x);
319 extern void add_things(uint8_t type, uint8_t n)
322 for (i = 0; i < n; i++)
327 char * err="Space to put thing on too hard to find. Map too small?";
329 for (pos.y = pos.x = 0;
330 '.' != world.map.cells[pos.y * world.map.length + pos.x];
333 exit_err(UINT16_MAX == i_pos, err);
334 pos.y = rrand() % world.map.length;
335 pos.x = rrand() % world.map.length;
339 for (t = world.things; t; t = t->next)
341 if (0 != t->lifepoints && pos.y==t->pos.y && pos.x==t->pos.x)
352 add_thing(-1, type, pos.y, pos.x);
358 extern void own_thing(struct Thing ** target, struct Thing ** source,
362 if (id == (*source)->id)
369 struct Thing * penult = * source;
372 if (id == penult->next->id)
376 penult = penult->next;
379 penult->next = t->next;
381 struct Thing ** t_ptr_ptr = target;
382 for (; * t_ptr_ptr; t_ptr_ptr = &(*t_ptr_ptr)->next);
389 extern void set_thing_position(struct Thing * t, struct yx_uint8 pos)
392 struct Thing * owned = t->owns;
393 for (; owned; set_thing_position(owned, pos), owned = owned->next);