home · contact · privacy
Server: Read in former "config" data as normal server god commands.
[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     char * f_name = "add_to_struct_list()";
70     struct NextAndId * nai  = try_malloc(n_size, f_name);
71     memset(nai, 0, n_size);
72     nai->id = (start_id<=id && id<=UINT8_MAX) ? id : get_unused_id(struct_id);
73     struct NextAndId ** nai_ptr_ptr = start;
74     for (; NULL != * nai_ptr_ptr; nai_ptr_ptr = &(*nai_ptr_ptr)->next);
75     *nai_ptr_ptr = nai;
76     return nai;
77 }
78
79
80
81 extern struct ThingAction * add_thing_action(int16_t id)
82 {
83     struct ThingAction * ta;
84     ta = (struct ThingAction *) add_to_struct_list(sizeof(struct ThingAction),
85                                                    1, id, 2,
86                                                    (struct NextAndId **)
87                                                    &world.thing_actions);
88     set_cleanup_flag(CLEANUP_THING_ACTIONS);
89     ta->name = strdup(s[S_CMD_WAIT]);
90     ta->effort = 1;
91     ta->func = actor_wait;
92     return ta;
93 }
94
95
96
97 extern struct ThingType * add_thing_type(int16_t id)
98 {
99     struct ThingType * tt;
100     tt = (struct ThingType *) add_to_struct_list(sizeof(struct ThingType),
101                                                  0, id, 1,
102                                                  (struct NextAndId **)
103                                                  &world.thing_types);
104     set_cleanup_flag(CLEANUP_THING_TYPES);
105     tt->name = strdup("(none)");
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 free_thing_actions(struct ThingAction * ta)
128 {
129     if (NULL == ta)
130     {
131         return;
132     }
133     free_thing_actions(ta->next);
134     free(ta->name);
135     free(ta);
136 }
137
138
139
140 extern void free_thing_types(struct ThingType * tt)
141 {
142     if (NULL == tt)
143     {
144         return;
145     }
146     free_thing_types(tt->next);
147     free(tt->name);
148     free(tt);
149 }
150
151
152
153 extern void free_things(struct Thing * t)
154 {
155     if (NULL == t)
156     {
157         return;
158     }
159     free_things(t->owns);
160     free_things(t->next);
161     free(t->fov_map);
162     free(t);
163     if (t == world.things)         /* So add_things()' NULL-delimited thing   */
164     {                              /* iteration loop does not iterate over    */
165         world.things = NULL;       /* freed memory when called the first time */
166     }                              /* after world re-seeding.                 */
167 }
168
169
170
171 extern struct ThingAction * get_thing_action(uint8_t id)
172 {
173     struct ThingAction * ta = world.thing_actions;
174     for (; NULL != ta && id != ta->id; ta = ta->next);
175     return ta;
176 }
177
178
179
180 extern struct ThingType * get_thing_type(uint8_t id)
181 {
182     struct ThingType * tt = world.thing_types;
183     for (; NULL != tt && id != tt->id; tt = tt->next);
184     return tt;
185 }
186
187
188
189 extern uint8_t get_thing_action_id_by_name(char * name)
190 {
191     struct ThingAction * ta = world.thing_actions;
192     while (NULL != ta)
193     {
194         if (0 == strcmp(ta->name, name))
195         {
196             break;
197         }
198         ta = ta->next;
199     }
200     if (!ta)
201     {
202         return 0;
203     }
204     return ta->id;
205 }
206
207
208
209 extern struct Thing * get_thing(struct Thing * ptr, uint8_t id, uint8_t deep)
210 {
211     while (1)
212     {
213         if (NULL == ptr || id == ptr->id)
214         {
215             return ptr;
216         }
217         if (deep)
218         {
219             struct Thing * owned_thing = get_thing(ptr->owns, id, 1);
220             if (NULL != owned_thing)
221             {
222                 return ptr;
223             }
224         }
225         ptr = ptr->next;
226     }
227 }
228
229
230
231 extern struct Thing * get_player()
232 {
233     return get_thing(world.things, 0, 0);
234 }
235
236
237
238 extern void add_things(uint8_t type, uint8_t n)
239 {
240     uint8_t i;
241     for (i = 0; i < n; i++)
242     {
243         struct yx_uint8 pos;
244         while (1)
245         {
246             char * err = "Space to put thing on too hard to find."
247                          "Map too small?";
248             uint16_t i_pos = 0;
249             for (pos.y = pos.x = 0; 0 == is_passable(pos); i_pos++)
250             {
251                 exit_err(UINT16_MAX == i_pos, err);
252                 pos.y = rrand() % world.map.length;
253                 pos.x = rrand() % world.map.length;
254             }
255             struct Thing * t;
256             uint8_t clear = 1;
257             for (t = world.things; t; t = t->next)
258             {
259                 if (yx_uint8_cmp(&pos, &t->pos) && 0 != t->lifepoints)
260                 {
261                     clear = 0;
262                     break;
263                 }
264             }
265             if (1 == clear)
266             {
267                 break;
268             }
269         }
270         add_thing(-1, type, pos.y, pos.x);
271     }
272 }
273
274
275
276 extern void own_thing(struct Thing ** target, struct Thing ** source,
277                       uint8_t id)
278 {
279     struct Thing * t;
280     if (id == (*source)->id)
281     {
282         t = * source;
283         * source = t->next;
284     }
285     else
286     {
287         struct Thing * penult = * source;
288         while (1)
289         {
290             if (id == penult->next->id)
291             {
292                 break;
293             }
294             penult = penult->next;
295         }
296         t = penult->next;
297         penult->next = t->next;
298     }
299     struct Thing ** t_ptr_ptr = target;
300     for (; NULL != * t_ptr_ptr; t_ptr_ptr = &(*t_ptr_ptr)->next);
301     * t_ptr_ptr = t;
302     t->next = NULL;
303 }
304
305
306
307 extern void set_thing_position(struct Thing * t, struct yx_uint8 pos)
308 {
309     t->pos = pos;
310     struct Thing * owned = t->owns;
311     for (; owned != NULL; set_thing_position(owned, pos), owned = owned->next);
312 }