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 /* Return lowest unused id for new thing ("sel"==0), thing type ("sel"==1) or
32 * thing action ("sel"==2).
34 static uint8_t get_unused_id(uint8_t sel);
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 ID from get_unused_id("struct_id").
41 static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id,
42 int16_t id, uint8_t struct_id,
43 struct NextAndId ** start);
47 static uint8_t get_unused_id(uint8_t sel)
52 if ( (0 == sel && !get_thing(world.things, i, 1))
53 || (1 == sel && !get_thing_type(i))
54 || (2 == sel && !get_thing_action(i)))
58 exit_err(i == UINT8_MAX, "No unused ID available to add to ID list.");
65 static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id,
66 int16_t id, uint8_t struct_id,
67 struct NextAndId ** start)
69 struct NextAndId * nai = try_malloc(n_size, __func__);
70 memset(nai, 0, n_size);
71 nai->id = (start_id<=id && id<=UINT8_MAX) ? id : get_unused_id(struct_id);
72 struct NextAndId ** nai_ptr_ptr = start;
73 for (; NULL != * nai_ptr_ptr; nai_ptr_ptr = &(*nai_ptr_ptr)->next);
80 extern struct ThingAction * add_thing_action(int16_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)");
110 extern struct Thing * add_thing(int16_t id, uint8_t type, uint8_t y, uint8_t x)
113 t = (struct Thing *) add_to_struct_list(sizeof(struct Thing), 0, id, 0,
114 (struct NextAndId **)&world.things);
115 struct ThingType * tt = get_thing_type(type);
116 set_cleanup_flag(CLEANUP_THINGS);
118 t->lifepoints = tt->lifepoints;
126 extern void free_thing_actions(struct ThingAction * ta)
132 free_thing_actions(ta->next);
139 extern void free_thing_types(struct ThingType * tt)
145 free_thing_types(tt->next);
152 extern void free_things(struct Thing * t)
158 free_things(t->owns);
159 free_things(t->next);
162 if (t == world.things) /* So add_things()' NULL-delimited thing */
163 { /* iteration loop does not iterate over */
164 world.things = NULL; /* freed memory when called the first time */
165 } /* after world re-seeding. */
170 extern struct ThingAction * get_thing_action(uint8_t id)
172 struct ThingAction * ta = world.thing_actions;
173 for (; NULL != ta && id != ta->id; ta = ta->next);
179 extern struct ThingType * get_thing_type(uint8_t id)
181 struct ThingType * tt = world.thing_types;
182 for (; NULL != tt && id != tt->id; tt = tt->next);
188 extern uint8_t get_thing_action_id_by_name(char * name)
190 struct ThingAction * ta = world.thing_actions;
193 if (0 == strcmp(ta->name, name))
208 extern struct Thing * get_thing(struct Thing * ptr, uint8_t id, uint8_t deep)
212 if (NULL == ptr || id == ptr->id)
218 struct Thing * owned_thing = get_thing(ptr->owns, id, 1);
219 if (NULL != owned_thing)
230 extern struct Thing * get_player()
232 return get_thing(world.things, 0, 0);
237 extern void add_things(uint8_t type, uint8_t n)
240 for (i = 0; i < n; i++)
245 char * err = "Space to put thing on too hard to find."
248 for (pos.y = pos.x = 0; 0 == is_passable(pos); i_pos++)
250 exit_err(UINT16_MAX == i_pos, err);
251 pos.y = rrand() % world.map.length;
252 pos.x = rrand() % world.map.length;
256 for (t = world.things; t; t = t->next)
258 if (yx_uint8_cmp(&pos, &t->pos) && 0 != t->lifepoints)
269 add_thing(-1, type, pos.y, pos.x);
275 extern void own_thing(struct Thing ** target, struct Thing ** source,
279 if (id == (*source)->id)
286 struct Thing * penult = * source;
289 if (id == penult->next->id)
293 penult = penult->next;
296 penult->next = t->next;
298 struct Thing ** t_ptr_ptr = target;
299 for (; NULL != * t_ptr_ptr; t_ptr_ptr = &(*t_ptr_ptr)->next);
306 extern void set_thing_position(struct Thing * t, struct yx_uint8 pos)
309 struct Thing * owned = t->owns;
310 for (; owned != NULL; set_thing_position(owned, pos), owned = owned->next);