home · contact · privacy
5a866bb2f35a7f6395b4b7d005b8dcece45507da
[plomrogue] / src / server / things.c
1 /* src/server/things.c
2  *
3  * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
4  * or any later version. For details on its copyright, license, and warranties,
5  * see the file NOTICE in the root directory of the PlomRogue source package.
6  */
7
8 #define _POSIX_C_SOURCE 200809L /* strdup() */
9 #include "things.h"
10 #include <stddef.h> /* NULL, size_t */
11 #include <stdint.h> /* uint8_t, uint16_t, int16_t, UINT8_MAX, UINT16_MAX */
12 #include <stdlib.h> /* free() */
13 #include <string.h> /* memset(), strcmp(), strdup() */
14 #include "../common/rexit.h" /* exit_err() */
15 #include "../common/try_malloc.h" /* try_malloc() */
16 #include "../common/yx_uint8.h" /* yx_uint8 */
17 #include "cleanup.h" /* set_cleanup_flag() */
18 #include "hardcoded_strings.h" /* s */
19 #include "rrand.h" /* rrand() */
20 #include "thing_actions.h" /* actor_wait */
21 #include "world.h" /* world */
22
23
24
25 /* Used to treat structs Thing, ThingType and ThingAction the same. */
26 struct NextAndId
27 {
28     struct NextAndId * next;
29     uint8_t id;
30 };
31
32
33
34 /* To linked list of NextAndId structs (or rather structs whose start region is
35  * compatible to it) starting at "start", add newly allocated element of
36  * "n_size" and an ID that is either "id" or, if "id" is <= UINT8_MAX and >=
37  * "id_start", get lowest ID >= "start_id" and <= UINT8_MAX for new thing
38  * ("struct_id"=0), thing type ("struct_id"=1) or thing action ("struct_id"=2).
39  */
40 static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id,
41                                              int16_t id, uint8_t struct_id,
42                                              struct NextAndId ** start);
43
44
45
46 static struct NextAndId * add_to_struct_list(size_t n_size, uint8_t start_id,
47                                              int16_t id, uint8_t struct_id,
48                                              struct NextAndId ** start)
49 {
50     struct NextAndId * nai  = try_malloc(n_size, __func__);
51     memset(nai, 0, n_size);
52     if (start_id <= id && id <= UINT8_MAX)
53     {
54         nai->id = id;
55     }
56     else
57     {
58         while (1)
59         {
60             if (   (0 == struct_id && !get_thing(world.things, start_id, 1))
61                 || (1 == struct_id && !get_thing_type(start_id))
62                 || (2 == struct_id && !get_thing_action(start_id)))
63             {
64                 nai->id = start_id;
65                 break;
66             }
67             char * err =  "No unused ID available to add to ID list.";
68             exit_err(start_id == UINT8_MAX, err);
69             start_id++;
70         }
71     }
72     struct NextAndId ** nai_ptr_ptr = start;
73     for (; * 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(uint8_t id)
81 {
82     struct ThingAction * ta;
83     ta = (struct ThingAction *) add_to_struct_list(sizeof(struct ThingAction),
84                                                    1, (int16_t) 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     tt->corpse_id = tt->id;
106     return tt;
107 }
108
109
110
111 extern struct Thing * add_thing(int16_t id, uint8_t type, uint8_t y, uint8_t x)
112 {
113     struct Thing * t;
114     t = (struct Thing *) add_to_struct_list(sizeof(struct Thing), 0, id, 0,
115                                             (struct NextAndId **)&world.things);
116     struct ThingType * tt = get_thing_type(type);
117     set_cleanup_flag(CLEANUP_THINGS);
118     t->type       = tt->id;
119     t->lifepoints = tt->lifepoints;
120     t->pos.y      = y;
121     t->pos.x      = x;
122     return t;
123 }
124
125
126
127 extern void add_thing_to_memory_map(struct Thing * t, uint8_t type,
128                                     uint8_t y, uint8_t x)
129 {
130     struct ThingInMemory * tm=try_malloc(sizeof(struct ThingInMemory),__func__);
131     tm->type = type;
132     tm->pos.y = y;
133     tm->pos.x = x;
134     tm->next = t->t_mem;
135     t->t_mem = tm;
136 }
137
138
139
140 extern void free_thing_actions(struct ThingAction * ta)
141 {
142     if (!ta)
143     {
144         return;
145     }
146     free_thing_actions(ta->next);
147     free(ta->name);
148     free(ta);
149 }
150
151
152
153 extern void free_thing_types(struct ThingType * tt)
154 {
155     if (!tt)
156     {
157         return;
158     }
159     free_thing_types(tt->next);
160     free(tt->name);
161     free(tt);
162 }
163
164
165
166 extern void free_things(struct Thing * t)
167 {
168     if (!t)
169     {
170         return;
171     }
172     free_things(t->owns);
173     free_things(t->next);
174     free(t->fov_map);
175     free(t->mem_map);
176     free_things_in_memory(t->t_mem);
177     free(t);
178     if (t == world.things)         /* So add_things()' NULL-delimited thing   */
179     {                              /* iteration loop does not iterate over    */
180         world.things = NULL;       /* freed memory when called the first time */
181     }                              /* after world re-seeding.                 */
182 }
183
184
185
186 extern void free_things_in_memory(struct ThingInMemory * tm)
187 {
188     if (!tm)
189     {
190         return;
191     }
192     free_things_in_memory(tm->next);
193     free(tm);
194 }
195
196
197
198 extern struct ThingAction * get_thing_action(uint8_t id)
199 {
200     struct ThingAction * ta = world.thing_actions;
201     for (; ta && id != ta->id; ta = ta->next);
202     return ta;
203 }
204
205
206
207 extern struct ThingType * get_thing_type(uint8_t id)
208 {
209     struct ThingType * tt = world.thing_types;
210     for (; tt && id != tt->id; tt = tt->next);
211     return tt;
212 }
213
214
215
216 extern uint8_t get_thing_action_id_by_name(char * name)
217 {
218     struct ThingAction * ta = world.thing_actions;
219     while (ta)
220     {
221         if (0 == strcmp(ta->name, name))
222         {
223             break;
224         }
225         ta = ta->next;
226     }
227     if (!ta)
228     {
229         return 0;
230     }
231     return ta->id;
232 }
233
234
235
236 extern struct Thing * get_thing(struct Thing * ptr, uint8_t id, uint8_t deep)
237 {
238     while (1)
239     {
240         if (!ptr || id == ptr->id)
241         {
242             return ptr;
243         }
244         if (deep)
245         {
246             struct Thing * owned_thing = get_thing(ptr->owns, id, 1);
247             if (owned_thing)
248             {
249                 return ptr;
250             }
251         }
252         ptr = ptr->next;
253     }
254 }
255
256
257
258 extern struct Thing * get_player()
259 {
260     return get_thing(world.things, 0, 0);
261 }
262
263
264
265 extern void add_things(uint8_t type, uint8_t n)
266 {
267     uint8_t i;
268     for (i = 0; i < n; i++)
269     {
270         struct yx_uint8 pos;
271         while (1)
272         {
273             char * err="Space to put thing on too hard to find. Map too small?";
274             uint16_t i_pos = 0;
275             for (pos.y = pos.x = 0;
276                  '.' != world.map.cells[pos.y * world.map.length + pos.x];
277                  i_pos++)
278             {
279                 exit_err(UINT16_MAX == i_pos, err);
280                 pos.y = rrand() % world.map.length;
281                 pos.x = rrand() % world.map.length;
282             }
283             struct Thing * t;
284             uint8_t clear = 1;
285             for (t = world.things; t; t = t->next)
286             {
287                 if (0 != t->lifepoints && pos.y==t->pos.y && pos.x==t->pos.x)
288                 {
289                     clear = 0;
290                     break;
291                 }
292             }
293             if (1 == clear)
294             {
295                 break;
296             }
297         }
298         add_thing(-1, type, pos.y, pos.x);
299     }
300 }
301
302
303
304 extern void own_thing(struct Thing ** target, struct Thing ** source,
305                       uint8_t id)
306 {
307     struct Thing * t;
308     if (id == (*source)->id)
309     {
310         t = * source;
311         * source = t->next;
312     }
313     else
314     {
315         struct Thing * penult = * source;
316         while (1)
317         {
318             if (id == penult->next->id)
319             {
320                 break;
321             }
322             penult = penult->next;
323         }
324         t = penult->next;
325         penult->next = t->next;
326     }
327     struct Thing ** t_ptr_ptr = target;
328     for (; * t_ptr_ptr; t_ptr_ptr = &(*t_ptr_ptr)->next);
329     * t_ptr_ptr = t;
330     t->next = NULL;
331 }
332
333
334
335 extern void set_thing_position(struct Thing * t, struct yx_uint8 pos)
336 {
337     t->pos = pos;
338     struct Thing * owned = t->owns;
339     for (; owned; set_thing_position(owned, pos), owned = owned->next);
340 }