1 /* src/server/things.c */
3 #define _POSIX_C_SOURCE 200809L /* strdup() */
5 #include <stddef.h> /* NULL, size_t */
6 #include <stdint.h> /* uint8_t, uint16_t, int16_t, UINT8_MAX, UINT16_MAX */
7 #include <stdlib.h> /* free() */
8 #include <string.h> /* memset(), strcmp(), strdup() */
9 #include "../common/rexit.h" /* exit_err() */
10 #include "../common/try_malloc.h" /* try_malloc() */
11 #include "../common/yx_uint8.h" /* yx_uint8 */
12 #include "cleanup.h" /* set_cleanup_flag() */
13 #include "hardcoded_strings.h" /* s */
14 #include "map.h" /* is_passable() */
15 #include "rrand.h" /* rrand() */
16 #include "thing_actions.h" /* actor_wait */
17 #include "world.h" /* world */
18 #include "yx_uint8.h" /* yx_uint8_cmp() */
22 /* Used to treat structs Thing, ThingType and ThingAction the same. */
25 struct NextAndId * next;
31 /* To linked list of NextAndId structs (or rather structs whose start region is
32 * compatible to it) starting at "start", add newly allocated element of
33 * "n_size" and an ID that is either "id" or, if "id" is <= UINT8_MAX and >=
34 * "id_start", get lowest ID >= "start_id" and <= UINT8_MAX for new thing
35 * ("struct_id"=0), thing type ("struct_id"=1) or thing action ("struct_id"=2).
37 static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id,
38 int16_t id, uint8_t struct_id,
39 struct NextAndId ** start);
43 static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id,
44 int16_t id, uint8_t struct_id,
45 struct NextAndId ** start)
47 struct NextAndId * nai = try_malloc(n_size, __func__);
48 memset(nai, 0, n_size);
49 if (start_id <= id && id <= UINT8_MAX)
57 if ( (0 == struct_id && !get_thing(world.things, start_id, 1))
58 || (1 == struct_id && !get_thing_type(start_id))
59 || (2 == struct_id && !get_thing_action(start_id)))
64 char * err = "No unused ID available to add to ID list.";
65 exit_err(start_id == UINT8_MAX, err);
69 struct NextAndId ** nai_ptr_ptr = start;
70 for (; * nai_ptr_ptr; nai_ptr_ptr = &(*nai_ptr_ptr)->next);
77 extern struct ThingAction * add_thing_action(uint8_t id)
79 struct ThingAction * ta;
80 ta = (struct ThingAction *) add_to_struct_list(sizeof(struct ThingAction),
83 &world.thing_actions);
84 set_cleanup_flag(CLEANUP_THING_ACTIONS);
85 ta->name = strdup(s[S_CMD_WAIT]);
87 ta->func = actor_wait;
93 extern struct ThingType * add_thing_type(int16_t id)
95 struct ThingType * tt;
96 tt = (struct ThingType *) add_to_struct_list(sizeof(struct ThingType),
100 set_cleanup_flag(CLEANUP_THING_TYPES);
101 tt->name = strdup("(none)");
102 tt->corpse_id = tt->id;
108 extern struct Thing * add_thing(int16_t id, uint8_t type, uint8_t y, uint8_t x)
111 t = (struct Thing *) add_to_struct_list(sizeof(struct Thing), 0, id, 0,
112 (struct NextAndId **)&world.things);
113 struct ThingType * tt = get_thing_type(type);
114 set_cleanup_flag(CLEANUP_THINGS);
116 t->lifepoints = tt->lifepoints;
124 extern void add_thing_to_memory_map(struct Thing * t, uint8_t type,
125 uint8_t y, uint8_t x)
127 struct ThingInMemory * tm=try_malloc(sizeof(struct ThingInMemory),__func__);
137 extern void free_thing_actions(struct ThingAction * ta)
143 free_thing_actions(ta->next);
150 extern void free_thing_types(struct ThingType * tt)
156 free_thing_types(tt->next);
163 extern void free_things(struct Thing * t)
169 free_things(t->owns);
170 free_things(t->next);
173 free_things_in_memory(t->t_mem);
175 if (t == world.things) /* So add_things()' NULL-delimited thing */
176 { /* iteration loop does not iterate over */
177 world.things = NULL; /* freed memory when called the first time */
178 } /* after world re-seeding. */
183 extern void free_things_in_memory(struct ThingInMemory * tm)
189 free_things_in_memory(tm->next);
195 extern struct ThingAction * get_thing_action(uint8_t id)
197 struct ThingAction * ta = world.thing_actions;
198 for (; ta && id != ta->id; ta = ta->next);
204 extern struct ThingType * get_thing_type(uint8_t id)
206 struct ThingType * tt = world.thing_types;
207 for (; tt && id != tt->id; tt = tt->next);
213 extern uint8_t get_thing_action_id_by_name(char * name)
215 struct ThingAction * ta = world.thing_actions;
218 if (0 == strcmp(ta->name, name))
233 extern struct Thing * get_thing(struct Thing * ptr, uint8_t id, uint8_t deep)
237 if (!ptr || id == ptr->id)
243 struct Thing * owned_thing = get_thing(ptr->owns, id, 1);
255 extern struct Thing * get_player()
257 return get_thing(world.things, 0, 0);
262 extern void add_things(uint8_t type, uint8_t n)
265 for (i = 0; i < n; i++)
270 char * err = "Space to put thing on too hard to find."
273 for (pos.y = pos.x = 0; 0 == is_passable(pos); i_pos++)
275 exit_err(UINT16_MAX == i_pos, err);
276 pos.y = rrand() % world.map.length;
277 pos.x = rrand() % world.map.length;
281 for (t = world.things; t; t = t->next)
283 if (yx_uint8_cmp(&pos, &t->pos) && 0 != t->lifepoints)
294 add_thing(-1, type, pos.y, pos.x);
300 extern void own_thing(struct Thing ** target, struct Thing ** source,
304 if (id == (*source)->id)
311 struct Thing * penult = * source;
314 if (id == penult->next->id)
318 penult = penult->next;
321 penult->next = t->next;
323 struct Thing ** t_ptr_ptr = target;
324 for (; * t_ptr_ptr; t_ptr_ptr = &(*t_ptr_ptr)->next);
331 extern void set_thing_position(struct Thing * t, struct yx_uint8 pos)
334 struct Thing * owned = t->owns;
335 for (; owned; set_thing_position(owned, pos), owned = owned->next);