home · contact · privacy
Server: Make objects definable as "consumable" to gain n hitpoints.
[plomrogue] / src / server / map_objects.c
1 /* src/server/map_objects.c */
2
3 #include "map_objects.h"
4 #include <stddef.h> /* NULL */
5 #include <stdio.h> /* FILE typedef */
6 #include <stdint.h> /* uint8_t, uint16_t, UINT8_MAX */
7 #include <stdlib.h> /* free(), atoi() */
8 #include <string.h> /* strlen(), memcpy(), memset() */
9 #include "../common/err_try_fgets.h" /* err_try_fgets(), err_line(),
10                                       * reset_err_try_fgets_counter()
11                                       */
12 #include "../common/readwrite.h" /* try_fopen(), try_fclose(), try_fgetc(),
13                                   * textfile_width()
14                                   */
15 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
16 #include "../common/try_malloc.h" /* try_malloc() */
17 #include "../common/yx_uint8.h" /* yx_uint8 struct */
18 #include "cleanup.h" /* set_cleanup_flag() */
19 #include "map.h" /* is_passable() */
20 #include "rrand.h" /* rrand() */
21 #include "world.h" /* global world */
22 #include "yx_uint8.h" /* yx_uint8_cmp() */
23
24
25
26 /* Return pointer to map object of "id" in chain starting at "ptr". */
27 static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id);
28
29 /* Return random passable (as by is_passable()) position on world.map. */
30 static struct yx_uint8 find_passable_pos();
31
32 /* Add object of "type" to map on random position. Don't place actor on actor.*/
33 static void add_map_object(uint8_t type);
34
35
36
37 static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id)
38 {
39     while (1)
40     {
41         if (NULL == ptr || id == ptr->id)
42         {
43             return ptr;
44         }
45         struct MapObj * owned_object = get_map_object(ptr->owns, id);
46         if (NULL != owned_object)
47         {
48             return ptr;
49         }
50         ptr = ptr->next;
51     }
52 }
53
54
55
56 static struct yx_uint8 find_passable_pos()
57 {
58     struct yx_uint8 pos;
59     for (pos.y = pos.x = 0; 0 == is_passable(pos);)
60     {
61         pos.y = rrand() % world.map.size.y;
62         pos.x = rrand() % world.map.size.x;
63     }
64     return pos;
65 }
66
67
68
69 static void add_map_object(uint8_t type)
70 {
71     char * f_name = "add_map_object()";
72     struct MapObjDef * mod = get_map_object_def(type);
73     struct MapObj *    mo  = try_malloc(sizeof(struct MapObj), f_name);
74     memset(mo, 0, sizeof(struct MapObj));
75     mo->id         = world.map_obj_count++;
76     mo->type       = mod->id;
77     mo->lifepoints = mod->lifepoints;
78     while (1)
79     {
80         struct yx_uint8 pos = find_passable_pos(world.map);
81         struct MapObj * mo_ptr;
82         uint8_t clear = 1;
83         for (mo_ptr = world.map_objs; mo_ptr != NULL; mo_ptr = mo_ptr->next)
84         {
85             if (yx_uint8_cmp(&pos, &mo_ptr->pos) && 0 != mo_ptr->lifepoints)
86             {
87                 clear = 0;
88                 break;
89             }
90         }
91         if (1 == clear)
92         {
93             mo->pos = pos;
94             break;
95         }
96     }
97     struct MapObj ** mo_ptr_ptr = &world.map_objs;
98     for (; NULL != * mo_ptr_ptr; mo_ptr_ptr = &(*mo_ptr_ptr)->next);
99     * mo_ptr_ptr = mo;
100 }
101
102
103
104 extern void init_map_object_defs()
105 {
106     char * f_name = "init_map_object_defs()";
107     char * context = "Failed reading map object definitions file. ";
108     char * err_toolarge = "Value is too large.";
109     char * err_uniq     = "Declaration of ID already used.";
110     FILE * file = try_fopen(world.path_map_obj_defs, "r", f_name);
111     uint32_t linemax = textfile_width(file);
112     struct MapObjDef ** last_mod_ptr_ptr = &world.map_obj_defs;
113     char line[linemax + 1];
114     reset_err_try_fgets_counter();
115     while (1)
116     {
117         int test_for_end = try_fgetc(file, f_name);
118         if (EOF == test_for_end || '\n' == test_for_end)
119         {
120             break;
121         }
122         exit_trouble(EOF == ungetc(test_for_end, file), f_name, "ungetc()");
123         struct MapObjDef * mod = try_malloc(sizeof(struct MapObjDef), f_name);
124         mod->next = NULL;
125         err_try_fgets(line, linemax, file, context, "nfi");
126         err_line(atoi(line) > UINT8_MAX, line, context, err_toolarge);
127         mod->id = atoi(line);
128         struct MapObjDef * mod_test = world.map_obj_defs;
129         for (; NULL != mod_test; mod_test = mod_test->next)
130         {
131             err_line(mod->id == mod_test->id, line, context, err_uniq);
132         }
133         err_try_fgets(line, linemax, file, context, "0nfi");
134         err_line(atoi(line) > UINT8_MAX, line, context, err_toolarge);
135         mod->corpse_id = atoi(line);
136         err_try_fgets(line, linemax, file, context, "0nfs");
137         mod->char_on_map = line[0];
138         err_try_fgets(line, linemax, file, context, "0nfi");
139         err_line(atoi(line) > UINT8_MAX, line, context, err_toolarge);
140         mod->lifepoints = atoi(line);
141         err_try_fgets(line, linemax, file, context, "0nf");
142         line[strlen(line) - 1] = '\0';
143         mod->name = try_malloc(strlen(line) + 1, f_name);
144         memcpy(mod->name, line, strlen(line) + 1);
145         err_try_fgets(line, linemax, file, context, "0nfi");
146         err_line(atoi(line) > UINT8_MAX, line, context, err_toolarge);
147         mod->consumable = atoi(line);
148         * last_mod_ptr_ptr = mod;
149         last_mod_ptr_ptr = &mod->next;
150         err_try_fgets(line, linemax, file, context, "d");
151     }
152     try_fclose(file, f_name);
153     set_cleanup_flag(CLEANUP_MAP_OBJECT_DEFS);
154 }
155
156
157
158 extern void free_map_object_defs(struct MapObjDef * mod_start)
159 {
160     if (NULL == mod_start)
161     {
162         return;
163     }
164     free_map_object_defs(mod_start->next);
165     free(mod_start->name);
166     free(mod_start);
167 }
168
169
170
171 extern void add_map_objects(uint8_t type, uint8_t n)
172 {
173     uint8_t i;
174     for (i = 0; i < n; i++)
175     {
176         add_map_object(type);
177     }
178 }
179
180
181
182 extern void free_map_objects(struct MapObj * mo_start)
183 {
184     if (NULL == mo_start)
185     {
186         return;
187     }
188     free_map_objects(mo_start->owns);
189     free_map_objects(mo_start->next);
190     free(mo_start);
191     if (mo_start == world.map_objs)  /* So add_map_objects()' NULL-delimited  */
192     {                                /* map object iteration loop does not    */
193         world.map_objs = NULL;       /* iterate over freed memory when called */
194     }                                /* the 1st time after world re-seeding.  */
195 }
196
197
198
199 extern void own_map_object(struct MapObj ** target, struct MapObj ** source,
200                            uint8_t id)
201 {
202     struct MapObj * mo;
203     if (id == (*source)->id)
204     {
205         mo = * source;
206         * source = mo->next;
207     }
208     else
209     {
210         struct MapObj * penult = * source;
211         while (1)
212         {
213             if (id == penult->next->id)
214             {
215                 break;
216             }
217             penult = penult->next;
218         }
219         mo = penult->next;
220         penult->next = mo->next;
221     }
222     struct MapObj ** mo_ptr_ptr = target;
223     for (; NULL != * mo_ptr_ptr; mo_ptr_ptr = &(*mo_ptr_ptr)->next);
224     * mo_ptr_ptr = mo;
225     mo->next = NULL;
226 }
227
228
229
230 extern struct MapObj * get_player()
231 {
232     return get_map_object(world.map_objs, 0);
233 }
234
235
236
237 extern struct MapObjDef * get_map_object_def(uint8_t id)
238 {
239     struct MapObjDef * mod = world.map_obj_defs;
240     for (; NULL != mod && id != mod->id; mod = mod->next);
241     char * err_intro = "Requested map object definition of unused ID ";
242     char err[strlen(err_intro) + 3 + 1 + 1];
243     sprintf(err, "%s%d.", err_intro, id);
244     exit_err(NULL == mod, err);
245     return mod;
246 }
247
248
249
250 extern void set_object_position(struct MapObj * mo, struct yx_uint8 pos)
251 {
252     mo->pos = pos;
253     struct MapObj * owned = mo->owns;
254     for (; owned != NULL; set_object_position(owned, pos), owned = owned->next);
255 }