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() */
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 "rrand.h" /* rrand() */
20 #include "thing_actions.h" /* actor_wait */
21 #include "world.h" /* world */
25 /* Used to treat structs Thing, ThingType and ThingAction the same. */
28 struct NextAndId * next;
34 /* To linked list of NextAndId structs (or rather structs whose start region is
35 * compatible to it) starting at "start", add newly allocated element of
36 * "n_size" and an ID that is either "id" or, if "id" is <= UINT8_MAX and >=
37 * "id_start", get lowest ID >= "start_id" and <= UINT8_MAX for new thing
38 * ("struct_id"=0), thing type ("struct_id"=1) or thing action ("struct_id"=2).
40 static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id,
41 int16_t id, uint8_t struct_id,
42 struct NextAndId ** start);
46 static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id,
47 int16_t id, uint8_t struct_id,
48 struct NextAndId ** start)
50 struct NextAndId * nai = try_malloc(n_size, __func__);
51 memset(nai, 0, n_size);
52 if (start_id <= id && id <= UINT8_MAX)
60 if ( (0 == struct_id && !get_thing(world.things, start_id, 1))
61 || (1 == struct_id && !get_thing_type(start_id))
62 || (2 == struct_id && !get_thing_action(start_id)))
67 char * err = "No unused ID available to add to ID list.";
68 exit_err(start_id == UINT8_MAX, err);
72 struct NextAndId ** nai_ptr_ptr = start;
73 for (; * nai_ptr_ptr; nai_ptr_ptr = &(*nai_ptr_ptr)->next);
80 extern struct ThingAction * add_thing_action(uint8_t id)
82 struct ThingAction * ta;
83 ta = (struct ThingAction *) add_to_struct_list(sizeof(struct ThingAction),
86 &world.thing_actions);
87 set_cleanup_flag(CLEANUP_THING_ACTIONS);
88 ta->name = strdup(s[S_CMD_WAIT]);
90 ta->func = actor_wait;
96 extern struct ThingType * add_thing_type(int16_t id)
98 struct ThingType * tt;
99 tt = (struct ThingType *) add_to_struct_list(sizeof(struct ThingType),
101 (struct NextAndId **)
103 set_cleanup_flag(CLEANUP_THING_TYPES);
104 tt->name = strdup("(none)");
105 tt->corpse_id = tt->id;
111 extern struct Thing * add_thing(int16_t id, uint8_t type, uint8_t y, uint8_t x)
114 t = (struct Thing *) add_to_struct_list(sizeof(struct Thing), 0, id, 0,
115 (struct NextAndId **)&world.things);
116 struct ThingType * tt = get_thing_type(type);
117 set_cleanup_flag(CLEANUP_THINGS);
119 t->lifepoints = tt->lifepoints;
127 extern void add_thing_to_memory_map(struct Thing * t, uint8_t type,
128 uint8_t y, uint8_t x)
130 struct ThingInMemory * tm=try_malloc(sizeof(struct ThingInMemory),__func__);
140 extern void free_thing_actions(struct ThingAction * ta)
146 free_thing_actions(ta->next);
153 extern void free_thing_types(struct ThingType * tt)
159 free_thing_types(tt->next);
166 extern void free_things(struct Thing * t)
172 free_things(t->owns);
173 free_things(t->next);
176 free_things_in_memory(t->t_mem);
178 if (t == world.things) /* So add_things()' NULL-delimited thing */
179 { /* iteration loop does not iterate over */
180 world.things = NULL; /* freed memory when called the first time */
181 } /* after world re-seeding. */
186 extern void free_things_in_memory(struct ThingInMemory * tm)
192 free_things_in_memory(tm->next);
198 extern struct ThingAction * get_thing_action(uint8_t id)
200 struct ThingAction * ta = world.thing_actions;
201 for (; ta && id != ta->id; ta = ta->next);
207 extern struct ThingType * get_thing_type(uint8_t id)
209 struct ThingType * tt = world.thing_types;
210 for (; tt && id != tt->id; tt = tt->next);
216 extern uint8_t get_thing_action_id_by_name(char * name)
218 struct ThingAction * ta = world.thing_actions;
221 if (0 == strcmp(ta->name, name))
236 extern struct Thing * get_thing(struct Thing * ptr, uint8_t id, uint8_t deep)
240 if (!ptr || id == ptr->id)
246 struct Thing * owned_thing = get_thing(ptr->owns, id, 1);
258 extern struct Thing * get_player()
260 return get_thing(world.things, 0, 0);
265 extern void add_things(uint8_t type, uint8_t n)
268 for (i = 0; i < n; i++)
273 char * err="Space to put thing on too hard to find. Map too small?";
275 for (pos.y = pos.x = 0;
276 '.' != world.map.cells[pos.y * world.map.length + pos.x];
279 exit_err(UINT16_MAX == i_pos, err);
280 pos.y = rrand() % world.map.length;
281 pos.x = rrand() % world.map.length;
285 for (t = world.things; t; t = t->next)
287 if (0 != t->lifepoints && pos.y==t->pos.y && pos.x==t->pos.x)
298 add_thing(-1, type, pos.y, pos.x);
304 extern void own_thing(struct Thing ** target, struct Thing ** source,
308 if (id == (*source)->id)
315 struct Thing * penult = * source;
318 if (id == penult->next->id)
322 penult = penult->next;
325 penult->next = t->next;
327 struct Thing ** t_ptr_ptr = target;
328 for (; * t_ptr_ptr; t_ptr_ptr = &(*t_ptr_ptr)->next);
335 extern void set_thing_position(struct Thing * t, struct yx_uint8 pos)
338 struct Thing * owned = t->owns;
339 for (; owned; set_thing_position(owned, pos), owned = owned->next);