home · contact · privacy
Use not f_name variable but __func__, standardize function name writing.
[plomrogue] / src / server / things.c
1 /* src/server/things.c */
2
3 #define _POSIX_C_SOURCE 200809L /* strdup() */
4 #include "things.h"
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() */
19
20
21
22 /* Used to treat structs Thing, ThingType and ThingAction the same. */
23 struct NextAndId
24 {
25     struct NextAndId * next;
26     uint8_t id;
27 };
28
29
30
31 /* Return lowest unused id for new thing ("sel"==0), thing type ("sel"==1) or
32  * thing action ("sel"==2).
33  */
34 static uint8_t get_unused_id(uint8_t sel);
35
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").
40  */
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);
44
45
46
47 static uint8_t get_unused_id(uint8_t sel)
48 {
49     uint8_t i = 0;
50     while (1)
51     {
52         if (   (0 == sel && !get_thing(world.things, i, 1))
53             || (1 == sel && !get_thing_type(i))
54             || (2 == sel && !get_thing_action(i)))
55         {
56             return i;
57         }
58         exit_err(i == UINT8_MAX, "No unused ID available to add to ID list.");
59         i++;
60     }
61 }
62
63
64
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)
68 {
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);
74     *nai_ptr_ptr = nai;
75     return nai;
76 }
77
78
79
80 extern struct ThingAction * add_thing_action(int16_t id)
81 {
82     struct ThingAction * ta;
83     ta = (struct ThingAction *) add_to_struct_list(sizeof(struct ThingAction),
84                                                    1, id, 2,
85                                                    (struct NextAndId **)
86                                                    &world.thing_actions);
87     set_cleanup_flag(CLEANUP_THING_ACTIONS);
88     ta->name = strdup(s[S_CMD_WAIT]);
89     ta->effort = 1;
90     ta->func = actor_wait;
91     return ta;
92 }
93
94
95
96 extern struct ThingType * add_thing_type(int16_t id)
97 {
98     struct ThingType * tt;
99     tt = (struct ThingType *) add_to_struct_list(sizeof(struct ThingType),
100                                                  0, id, 1,
101                                                  (struct NextAndId **)
102                                                  &world.thing_types);
103     set_cleanup_flag(CLEANUP_THING_TYPES);
104     tt->name = strdup("(none)");
105     return tt;
106 }
107
108
109
110 extern struct Thing * add_thing(int16_t id, uint8_t type, uint8_t y, uint8_t x)
111 {
112     struct Thing * t;
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);
117     t->type       = tt->id;
118     t->lifepoints = tt->lifepoints;
119     t->pos.y      = y;
120     t->pos.x      = x;
121     return t;
122 }
123
124
125
126 extern void free_thing_actions(struct ThingAction * ta)
127 {
128     if (NULL == ta)
129     {
130         return;
131     }
132     free_thing_actions(ta->next);
133     free(ta->name);
134     free(ta);
135 }
136
137
138
139 extern void free_thing_types(struct ThingType * tt)
140 {
141     if (NULL == tt)
142     {
143         return;
144     }
145     free_thing_types(tt->next);
146     free(tt->name);
147     free(tt);
148 }
149
150
151
152 extern void free_things(struct Thing * t)
153 {
154     if (NULL == t)
155     {
156         return;
157     }
158     free_things(t->owns);
159     free_things(t->next);
160     free(t->fov_map);
161     free(t);
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.                 */
166 }
167
168
169
170 extern struct ThingAction * get_thing_action(uint8_t id)
171 {
172     struct ThingAction * ta = world.thing_actions;
173     for (; NULL != ta && id != ta->id; ta = ta->next);
174     return ta;
175 }
176
177
178
179 extern struct ThingType * get_thing_type(uint8_t id)
180 {
181     struct ThingType * tt = world.thing_types;
182     for (; NULL != tt && id != tt->id; tt = tt->next);
183     return tt;
184 }
185
186
187
188 extern uint8_t get_thing_action_id_by_name(char * name)
189 {
190     struct ThingAction * ta = world.thing_actions;
191     while (NULL != ta)
192     {
193         if (0 == strcmp(ta->name, name))
194         {
195             break;
196         }
197         ta = ta->next;
198     }
199     if (!ta)
200     {
201         return 0;
202     }
203     return ta->id;
204 }
205
206
207
208 extern struct Thing * get_thing(struct Thing * ptr, uint8_t id, uint8_t deep)
209 {
210     while (1)
211     {
212         if (NULL == ptr || id == ptr->id)
213         {
214             return ptr;
215         }
216         if (deep)
217         {
218             struct Thing * owned_thing = get_thing(ptr->owns, id, 1);
219             if (NULL != owned_thing)
220             {
221                 return ptr;
222             }
223         }
224         ptr = ptr->next;
225     }
226 }
227
228
229
230 extern struct Thing * get_player()
231 {
232     return get_thing(world.things, 0, 0);
233 }
234
235
236
237 extern void add_things(uint8_t type, uint8_t n)
238 {
239     uint8_t i;
240     for (i = 0; i < n; i++)
241     {
242         struct yx_uint8 pos;
243         while (1)
244         {
245             char * err = "Space to put thing on too hard to find."
246                          "Map too small?";
247             uint16_t i_pos = 0;
248             for (pos.y = pos.x = 0; 0 == is_passable(pos); i_pos++)
249             {
250                 exit_err(UINT16_MAX == i_pos, err);
251                 pos.y = rrand() % world.map.length;
252                 pos.x = rrand() % world.map.length;
253             }
254             struct Thing * t;
255             uint8_t clear = 1;
256             for (t = world.things; t; t = t->next)
257             {
258                 if (yx_uint8_cmp(&pos, &t->pos) && 0 != t->lifepoints)
259                 {
260                     clear = 0;
261                     break;
262                 }
263             }
264             if (1 == clear)
265             {
266                 break;
267             }
268         }
269         add_thing(-1, type, pos.y, pos.x);
270     }
271 }
272
273
274
275 extern void own_thing(struct Thing ** target, struct Thing ** source,
276                       uint8_t id)
277 {
278     struct Thing * t;
279     if (id == (*source)->id)
280     {
281         t = * source;
282         * source = t->next;
283     }
284     else
285     {
286         struct Thing * penult = * source;
287         while (1)
288         {
289             if (id == penult->next->id)
290             {
291                 break;
292             }
293             penult = penult->next;
294         }
295         t = penult->next;
296         penult->next = t->next;
297     }
298     struct Thing ** t_ptr_ptr = target;
299     for (; NULL != * t_ptr_ptr; t_ptr_ptr = &(*t_ptr_ptr)->next);
300     * t_ptr_ptr = t;
301     t->next = NULL;
302 }
303
304
305
306 extern void set_thing_position(struct Thing * t, struct yx_uint8 pos)
307 {
308     t->pos = pos;
309     struct Thing * owned = t->owns;
310     for (; owned != NULL; set_thing_position(owned, pos), owned = owned->next);
311 }