home · contact · privacy
Server: Fix buggy evaluation of THING_ACTION id argument.
[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 /* 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).
36  */
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);
40
41
42
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)
46 {
47     struct NextAndId * nai  = try_malloc(n_size, __func__);
48     memset(nai, 0, n_size);
49     if (start_id <= id && id <= UINT8_MAX)
50     {
51         nai->id = id;
52     }
53     else
54     {
55         while (1)
56         {
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)))
60             {
61                 nai->id = start_id;
62                 break;
63             }
64             char * err =  "No unused ID available to add to ID list.";
65             exit_err(start_id == UINT8_MAX, err);
66             start_id++;
67         }
68     }
69     struct NextAndId ** nai_ptr_ptr = start;
70     for (; NULL != * nai_ptr_ptr; nai_ptr_ptr = &(*nai_ptr_ptr)->next);
71     *nai_ptr_ptr = nai;
72     return nai;
73 }
74
75
76
77 extern struct ThingAction * add_thing_action(uint8_t id)
78 {
79     struct ThingAction * ta;
80     ta = (struct ThingAction *) add_to_struct_list(sizeof(struct ThingAction),
81                                                    1, (int16_t) id, 2,
82                                                    (struct NextAndId **)
83                                                    &world.thing_actions);
84     set_cleanup_flag(CLEANUP_THING_ACTIONS);
85     ta->name = strdup(s[S_CMD_WAIT]);
86     ta->effort = 1;
87     ta->func = actor_wait;
88     return ta;
89 }
90
91
92
93 extern struct ThingType * add_thing_type(int16_t id)
94 {
95     struct ThingType * tt;
96     tt = (struct ThingType *) add_to_struct_list(sizeof(struct ThingType),
97                                                  0, id, 1,
98                                                  (struct NextAndId **)
99                                                  &world.thing_types);
100     set_cleanup_flag(CLEANUP_THING_TYPES);
101     tt->name = strdup("(none)");
102     return tt;
103 }
104
105
106
107 extern struct Thing * add_thing(int16_t id, uint8_t type, uint8_t y, uint8_t x)
108 {
109     struct Thing * t;
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);
114     t->type       = tt->id;
115     t->lifepoints = tt->lifepoints;
116     t->pos.y      = y;
117     t->pos.x      = x;
118     return t;
119 }
120
121
122
123 extern void free_thing_actions(struct ThingAction * ta)
124 {
125     if (NULL == ta)
126     {
127         return;
128     }
129     free_thing_actions(ta->next);
130     free(ta->name);
131     free(ta);
132 }
133
134
135
136 extern void free_thing_types(struct ThingType * tt)
137 {
138     if (NULL == tt)
139     {
140         return;
141     }
142     free_thing_types(tt->next);
143     free(tt->name);
144     free(tt);
145 }
146
147
148
149 extern void free_things(struct Thing * t)
150 {
151     if (NULL == t)
152     {
153         return;
154     }
155     free_things(t->owns);
156     free_things(t->next);
157     free(t->fov_map);
158     free(t);
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.                 */
163 }
164
165
166
167 extern struct ThingAction * get_thing_action(uint8_t id)
168 {
169     struct ThingAction * ta = world.thing_actions;
170     for (; NULL != ta && id != ta->id; ta = ta->next);
171     return ta;
172 }
173
174
175
176 extern struct ThingType * get_thing_type(uint8_t id)
177 {
178     struct ThingType * tt = world.thing_types;
179     for (; NULL != tt && id != tt->id; tt = tt->next);
180     return tt;
181 }
182
183
184
185 extern uint8_t get_thing_action_id_by_name(char * name)
186 {
187     struct ThingAction * ta = world.thing_actions;
188     while (NULL != ta)
189     {
190         if (0 == strcmp(ta->name, name))
191         {
192             break;
193         }
194         ta = ta->next;
195     }
196     if (!ta)
197     {
198         return 0;
199     }
200     return ta->id;
201 }
202
203
204
205 extern struct Thing * get_thing(struct Thing * ptr, uint8_t id, uint8_t deep)
206 {
207     while (1)
208     {
209         if (NULL == ptr || id == ptr->id)
210         {
211             return ptr;
212         }
213         if (deep)
214         {
215             struct Thing * owned_thing = get_thing(ptr->owns, id, 1);
216             if (NULL != owned_thing)
217             {
218                 return ptr;
219             }
220         }
221         ptr = ptr->next;
222     }
223 }
224
225
226
227 extern struct Thing * get_player()
228 {
229     return get_thing(world.things, 0, 0);
230 }
231
232
233
234 extern void add_things(uint8_t type, uint8_t n)
235 {
236     uint8_t i;
237     for (i = 0; i < n; i++)
238     {
239         struct yx_uint8 pos;
240         while (1)
241         {
242             char * err = "Space to put thing on too hard to find."
243                          "Map too small?";
244             uint16_t i_pos = 0;
245             for (pos.y = pos.x = 0; 0 == is_passable(pos); i_pos++)
246             {
247                 exit_err(UINT16_MAX == i_pos, err);
248                 pos.y = rrand() % world.map.length;
249                 pos.x = rrand() % world.map.length;
250             }
251             struct Thing * t;
252             uint8_t clear = 1;
253             for (t = world.things; t; t = t->next)
254             {
255                 if (yx_uint8_cmp(&pos, &t->pos) && 0 != t->lifepoints)
256                 {
257                     clear = 0;
258                     break;
259                 }
260             }
261             if (1 == clear)
262             {
263                 break;
264             }
265         }
266         add_thing(-1, type, pos.y, pos.x);
267     }
268 }
269
270
271
272 extern void own_thing(struct Thing ** target, struct Thing ** source,
273                       uint8_t id)
274 {
275     struct Thing * t;
276     if (id == (*source)->id)
277     {
278         t = * source;
279         * source = t->next;
280     }
281     else
282     {
283         struct Thing * penult = * source;
284         while (1)
285         {
286             if (id == penult->next->id)
287             {
288                 break;
289             }
290             penult = penult->next;
291         }
292         t = penult->next;
293         penult->next = t->next;
294     }
295     struct Thing ** t_ptr_ptr = target;
296     for (; NULL != * t_ptr_ptr; t_ptr_ptr = &(*t_ptr_ptr)->next);
297     * t_ptr_ptr = t;
298     t->next = NULL;
299 }
300
301
302
303 extern void set_thing_position(struct Thing * t, struct yx_uint8 pos)
304 {
305     t->pos = pos;
306     struct Thing * owned = t->owns;
307     for (; owned != NULL; set_thing_position(owned, pos), owned = owned->next);
308 }