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);
46 /* Return 1 if cell at "test_pos" is proliferable by "t", i.e. it is passable,
47 * it is not inhabited by another thing of "t"'s type, and, if "t" is animate,
48 * neither by any other animate thing; else return 0.
50 static uint8_t cell_is_proliferable(struct yx_uint8 test_pos, struct Thing * t);
53 static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id,
54 int16_t id, uint8_t struct_id,
55 struct NextAndId ** start)
57 struct NextAndId * nai = try_malloc(n_size, __func__);
58 memset(nai, 0, n_size);
59 if (start_id <= id && id <= UINT8_MAX)
67 if ( (0 == struct_id && !get_thing(world.things, start_id, 1))
68 || (1 == struct_id && !get_thing_type(start_id))
69 || (2 == struct_id && !get_thing_action(start_id)))
74 char * err = "No unused ID available to add to ID list.";
75 exit_err(start_id == UINT8_MAX, err);
79 struct NextAndId ** nai_ptr_ptr = start;
80 for (; * nai_ptr_ptr; nai_ptr_ptr = &(*nai_ptr_ptr)->next);
87 static uint8_t cell_is_proliferable(struct yx_uint8 test_pos, struct Thing * t)
89 if ('.' == world.map.cells[test_pos.y * world.map.length + test_pos.x])
91 struct Thing * t_test;
92 for (t_test = world.things; t_test; t_test = t_test->next)
94 if (t_test->pos.y == test_pos.y && t_test->pos.x == test_pos.x)
96 if (t_test->type == t->type)
100 if (t_test->lifepoints && t->lifepoints)
113 extern struct ThingAction * add_thing_action(uint8_t id)
115 struct ThingAction * ta;
116 ta = (struct ThingAction *) add_to_struct_list(sizeof(struct ThingAction),
118 (struct NextAndId **)
119 &world.thing_actions);
120 set_cleanup_flag(CLEANUP_THING_ACTIONS);
121 ta->name = strdup(s[S_CMD_WAIT]);
123 ta->func = actor_wait;
129 extern struct ThingType * add_thing_type(int16_t id)
131 struct ThingType * tt;
132 tt = (struct ThingType *) add_to_struct_list(sizeof(struct ThingType),
134 (struct NextAndId **)
136 set_cleanup_flag(CLEANUP_THING_TYPES);
137 tt->name = strdup("(none)");
138 tt->corpse_id = tt->id;
144 extern struct Thing * add_thing(int16_t id, uint8_t type, uint8_t y, uint8_t x)
147 t = (struct Thing *) add_to_struct_list(sizeof(struct Thing), 0, id, 0,
148 (struct NextAndId **)&world.things);
149 struct ThingType * tt = get_thing_type(type);
150 set_cleanup_flag(CLEANUP_THINGS);
152 t->lifepoints = tt->lifepoints;
155 if (t->lifepoints && world.exists)
164 extern void add_thing_to_memory_map(struct Thing * t, uint8_t type,
165 uint8_t y, uint8_t x)
167 struct ThingInMemory * tm=try_malloc(sizeof(struct ThingInMemory),__func__);
177 extern void free_thing_actions(struct ThingAction * ta)
183 free_thing_actions(ta->next);
190 extern void free_thing_types(struct ThingType * tt)
196 free_thing_types(tt->next);
203 extern void free_things(struct Thing * t)
209 free_things(t->owns);
210 free_things(t->next);
213 free(t->mem_depth_map);
214 free_things_in_memory(t->t_mem);
216 if (t == world.things) /* So add_things()' NULL-delimited thing */
217 { /* iteration loop does not iterate over */
218 world.things = NULL; /* freed memory when called the first time */
219 } /* after world re-seeding. */
224 extern void free_things_in_memory(struct ThingInMemory * tm)
230 free_things_in_memory(tm->next);
236 extern struct ThingAction * get_thing_action(uint8_t id)
238 struct ThingAction * ta = world.thing_actions;
239 for (; ta && id != ta->id; ta = ta->next);
245 extern struct ThingType * get_thing_type(uint8_t id)
247 struct ThingType * tt = world.thing_types;
248 for (; tt && id != tt->id; tt = tt->next);
254 extern uint8_t get_thing_action_id_by_name(char * name)
256 struct ThingAction * ta = world.thing_actions;
259 if (0 == strcmp(ta->name, name))
274 extern struct Thing * get_thing(struct Thing * ptr, uint8_t id, uint8_t deep)
278 if (!ptr || id == ptr->id)
284 struct Thing * owned_thing = get_thing(ptr->owns, id, 1);
296 extern struct Thing * get_player()
298 return get_thing(world.things, 0, 0);
303 extern void try_thing_proliferation(struct Thing * t)
305 struct ThingType * tt = get_thing_type(t->type);
308 if (1 == tt->proliferate || 1 == (rrand() % tt->proliferate))
310 struct yx_uint8 candidates[6];
311 uint8_t n_candidates = 0;
312 char dirs[7] = "cxswed";
313 struct yx_uint8 test = t->pos;
315 for (i = 0; i < strlen(dirs); i++)
317 if ( mv_yx_in_dir_legal(dirs[i], &test)
318 && cell_is_proliferable(test, t))
320 candidates[n_candidates] = test;
328 i = rrand() % n_candidates;
329 add_thing(-1, tt->id, candidates[i].y, candidates[i].x);
336 extern void add_things(uint8_t type, uint8_t n)
339 for (i = 0; i < n; i++)
344 char * err="Space to put thing on too hard to find. Map too small?";
346 for (pos.y = pos.x = 0;
347 '.' != world.map.cells[pos.y * world.map.length + pos.x];
350 exit_err(UINT16_MAX == i_pos, err);
351 pos.y = rrand() % world.map.length;
352 pos.x = rrand() % world.map.length;
356 for (t = world.things; t; t = t->next)
358 if (0 != t->lifepoints && pos.y==t->pos.y && pos.x==t->pos.x)
369 add_thing(-1, type, pos.y, pos.x);
375 extern void own_thing(struct Thing ** target, struct Thing ** source,
379 if (id == (*source)->id)
386 struct Thing * penult = * source;
389 if (id == penult->next->id)
393 penult = penult->next;
396 penult->next = t->next;
398 struct Thing ** t_ptr_ptr = target;
399 for (; * t_ptr_ptr; t_ptr_ptr = &(*t_ptr_ptr)->next);
406 extern void set_thing_position(struct Thing * t, struct yx_uint8 pos)
409 struct Thing * owned = t->owns;
410 for (; owned; set_thing_position(owned, pos), owned = owned->next);