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 for new thing ("struct_id"==0), thing type
35 * ("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 (; NULL != * 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)");
107 extern struct Thing * add_thing(int16_t id, uint8_t type, uint8_t y, uint8_t x)
110 t = (struct Thing *) add_to_struct_list(sizeof(struct Thing), 0, id, 0,
111 (struct NextAndId **)&world.things);
112 struct ThingType * tt = get_thing_type(type);
113 set_cleanup_flag(CLEANUP_THINGS);
115 t->lifepoints = tt->lifepoints;
123 extern void free_thing_actions(struct ThingAction * ta)
129 free_thing_actions(ta->next);
136 extern void free_thing_types(struct ThingType * tt)
142 free_thing_types(tt->next);
149 extern void free_things(struct Thing * t)
155 free_things(t->owns);
156 free_things(t->next);
159 if (t == world.things) /* So add_things()' NULL-delimited thing */
160 { /* iteration loop does not iterate over */
161 world.things = NULL; /* freed memory when called the first time */
162 } /* after world re-seeding. */
167 extern struct ThingAction * get_thing_action(uint8_t id)
169 struct ThingAction * ta = world.thing_actions;
170 for (; NULL != ta && id != ta->id; ta = ta->next);
176 extern struct ThingType * get_thing_type(uint8_t id)
178 struct ThingType * tt = world.thing_types;
179 for (; NULL != tt && id != tt->id; tt = tt->next);
185 extern uint8_t get_thing_action_id_by_name(char * name)
187 struct ThingAction * ta = world.thing_actions;
190 if (0 == strcmp(ta->name, name))
205 extern struct Thing * get_thing(struct Thing * ptr, uint8_t id, uint8_t deep)
209 if (NULL == ptr || id == ptr->id)
215 struct Thing * owned_thing = get_thing(ptr->owns, id, 1);
216 if (NULL != owned_thing)
227 extern struct Thing * get_player()
229 return get_thing(world.things, 0, 0);
234 extern void add_things(uint8_t type, uint8_t n)
237 for (i = 0; i < n; i++)
242 char * err = "Space to put thing on too hard to find."
245 for (pos.y = pos.x = 0; 0 == is_passable(pos); i_pos++)
247 exit_err(UINT16_MAX == i_pos, err);
248 pos.y = rrand() % world.map.length;
249 pos.x = rrand() % world.map.length;
253 for (t = world.things; t; t = t->next)
255 if (yx_uint8_cmp(&pos, &t->pos) && 0 != t->lifepoints)
266 add_thing(-1, type, pos.y, pos.x);
272 extern void own_thing(struct Thing ** target, struct Thing ** source,
276 if (id == (*source)->id)
283 struct Thing * penult = * source;
286 if (id == penult->next->id)
290 penult = penult->next;
293 penult->next = t->next;
295 struct Thing ** t_ptr_ptr = target;
296 for (; NULL != * t_ptr_ptr; t_ptr_ptr = &(*t_ptr_ptr)->next);
303 extern void set_thing_position(struct Thing * t, struct yx_uint8 pos)
306 struct Thing * owned = t->owns;
307 for (; owned != NULL; set_thing_position(owned, pos), owned = owned->next);