home · contact · privacy
Fixed bug that led to endless loop in nearest_enemy_dir().
[plomrogue] / src / map_objects.c
1 /* map_objects.c */
2
3 #include "map_objects.h"
4 #include <stdlib.h> /* for free(), atoi() */
5 #include <stdint.h> /* for uint8_t, uint16_t */
6 #include <stdio.h> /* for FILE typedef */
7 #include <string.h> /* for strlen(), memcpy(), strtok() */
8 #include "readwrite.h" /* for textfile_sizes(), try_fopen(), try_fclose(),
9                         * try_fgets()
10                         */
11 #include "misc.h" /* for try_malloc(), find_passable_pos() */
12 #include "main.h" /* for world global */
13 #include "rexit.h" /* for exit_err() */
14 #include "yx_uint16.h" /* for yx_uint16 struct, yx_uint16_cmp() */
15
16
17
18 /* Write representation of "mo" and all of the map objects it owns to "file". */
19 static void write_map_object(FILE * file, struct MapObj * mo);
20
21 /* Return pointer to map object of "id" in chain starting at "ptr". */
22 static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id);
23
24
25
26 static void write_map_object(FILE * file, struct MapObj * mo)
27 {
28     char * f_name = "write_map_object()";
29     struct MapObj * mo_ptr = mo->owns;
30     uint8_t i = 0;
31     for (; NULL != mo_ptr; mo_ptr = mo_ptr->next, i++);
32     uint8_t size = 3+1 + 3+1 + 3+1 + 5+1 + 5 + ((1+3)*i) + 1 + 1;
33     char line[size];
34     sprintf(line, "%d %d %d %d %d %d %d %d",
35                   mo->id, mo->type, mo->lifepoints, mo->pos.y, mo->pos.x,
36                   mo->command, mo->arg, mo->progress);
37     for (mo_ptr = mo->owns; NULL != mo_ptr; mo_ptr = mo_ptr->next)
38     {
39         sprintf(line + strlen(line), " %d", mo_ptr->id);
40     }
41     line[strlen(line) + 1] = '\0';
42     line[strlen(line)] = '\n';
43     try_fwrite(line, strlen(line), 1, file, f_name);
44     for (mo_ptr = mo->owns; NULL != mo_ptr; mo_ptr = mo_ptr->next)
45     {
46         write_map_object(file, mo_ptr);
47     }
48 }
49
50
51
52 static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id)
53 {
54     while (1)
55     {
56         if (NULL == ptr || id == ptr->id)
57         {
58             return ptr;
59         }
60         struct MapObj * owned_object = get_map_object(ptr->owns, id);
61         if (NULL != owned_object)
62         {
63             return ptr;
64         }
65         ptr = ptr->next;
66     }
67 }
68
69
70
71 extern void init_map_object_defs(char * filename)
72 {
73     char * f_name = "init_map_object_defs()";
74     FILE * file = try_fopen(filename, "r", f_name);
75     uint16_t linemax = textfile_sizes(file, NULL);
76     struct MapObjDef ** last_mod_ptr_ptr = &world.map_obj_defs;
77     char * delim = " ";
78     char line[linemax + 1];
79     while (try_fgets(line, linemax + 1, file, f_name))
80     {
81         struct MapObjDef * mod = try_malloc(sizeof(struct MapObjDef), f_name);
82         mod->next = NULL;
83         mod->id = atoi(strtok(line, delim));
84         mod->corpse_id = atoi(strtok(NULL, delim));
85         mod->char_on_map = * strtok(NULL, delim);
86         mod->lifepoints = atoi(strtok(NULL, delim));
87         char * name = strtok(NULL, "\n");
88         mod->name = try_malloc(strlen(name) + 1, f_name);
89         memcpy(mod->name, name, strlen(name) + 1);
90         * last_mod_ptr_ptr = mod;
91         last_mod_ptr_ptr = &mod->next;
92     }
93     try_fclose(file, f_name);
94 }
95
96
97
98 extern void free_map_object_defs(struct MapObjDef * mod_start)
99 {
100     if (NULL == mod_start)
101     {
102         return;
103     }
104     free_map_object_defs(mod_start->next);
105     free(mod_start->name);
106     free(mod_start);
107 }
108
109
110
111 extern void write_map_objects(FILE * file)
112 {
113     struct MapObj * mo = world.map_objs;
114     while (NULL != mo)
115     {
116         write_map_object(file, mo);
117         mo = mo->next;
118     }
119 }
120
121
122
123 extern void read_map_objects(FILE * file, char * line, int linemax)
124 {
125     char * f_name = "read_map_objects()";
126     struct MapObj ** mo_ptr_ptr = &world.map_objs;
127     char * delim = " ";
128     fpos_t pos;
129     exit_err(-1 == fgetpos(file, &pos), f_name);
130     while (try_fgets(line, linemax + 1, file, f_name))
131     {
132         struct MapObj * mo = try_malloc(sizeof(struct MapObj), f_name);
133         mo->next       = NULL;
134         mo->id         = atoi(strtok(line, delim));
135         mo->type       = atoi(strtok(NULL, delim));
136         mo->lifepoints = atoi(strtok(NULL, delim));
137         mo->pos.y      = atoi(strtok(NULL, delim));
138         mo->pos.x      = atoi(strtok(NULL, delim));
139         mo->command    = atoi(strtok(NULL, delim));;
140         mo->arg        = atoi(strtok(NULL, delim));;
141         mo->progress   = atoi(strtok(NULL, delim));;
142         mo->owns       = NULL;
143         * mo_ptr_ptr = mo;
144         mo_ptr_ptr = &mo->next;
145     }
146     exit_err(-1 == fsetpos(file, &pos), f_name);
147     while (try_fgets(line, linemax + 1, file, f_name))
148     {
149         uint8_t id = atoi(strtok(line, delim));
150         uint8_t i;
151         for (i = 0; i < 7; i++, strtok(NULL, delim));
152         char * owned = strtok(NULL, "\n");
153         if (NULL != owned)
154         {
155             struct MapObj * mo = get_map_object(world.map_objs, id);
156             char * owned_id = "";
157             owned_id = strtok(owned, delim);
158             while (NULL != owned_id)
159             {
160                 own_map_object(&mo->owns, &world.map_objs, atoi(owned_id));
161                 owned_id = strtok(NULL, delim);
162             }
163         }
164     }
165 }
166
167
168
169 extern void add_map_object(uint8_t type)
170 {
171     char * f_name = "add_map_object()";
172     struct MapObjDef * mod = get_map_object_def(type);
173     struct MapObj *    mo  = try_malloc(sizeof(struct MapObj), f_name);
174     mo->id         = world.map_obj_count++;
175     mo->type       = mod->id;
176     mo->lifepoints = mod->lifepoints;
177     while (1)
178     {
179         struct yx_uint16 pos = find_passable_pos(world.map);
180         struct MapObj * mo_ptr;
181         uint8_t clear = 1;
182         for (mo_ptr = world.map_objs; mo_ptr != NULL; mo_ptr = mo_ptr->next)
183         {
184             if (yx_uint16_cmp(&pos, &mo_ptr->pos) && 0 != mo_ptr->lifepoints)
185             {
186                 clear = 0;
187                 break;
188             }
189         }
190         if (1 == clear)
191         {
192             mo->pos = pos;
193             break;
194         }
195     }
196     mo->progress = 0;
197     mo->command  = 0;
198     mo->arg      = 0;
199     mo->owns     = NULL;
200     mo->next     = NULL;
201     struct MapObj ** mo_ptr_ptr = &world.map_objs;
202     for (; NULL != * mo_ptr_ptr; mo_ptr_ptr = &(*mo_ptr_ptr)->next);
203     * mo_ptr_ptr = mo;
204 }
205
206
207
208 extern void add_map_objects(uint8_t type, uint8_t n)
209 {
210     uint8_t i;
211     for (i = 0; i < n; i++)
212     {
213         add_map_object(type);
214     }
215 }
216
217
218
219 extern void free_map_objects(struct MapObj * mo_start)
220 {
221     if (NULL == mo_start)
222     {
223         return;
224     }
225     free_map_objects(mo_start->owns);
226     free_map_objects(mo_start->next);
227     free(mo_start);
228 }
229
230
231
232 extern void own_map_object(struct MapObj ** target, struct MapObj ** source,
233                            uint8_t id)
234 {
235     struct MapObj * mo;
236     if (id == (*source)->id)
237     {
238         mo = * source;
239         * source = mo->next;
240     }
241     else
242     {
243         struct MapObj * penult = * source;
244         while (1)
245         {
246             if (id == penult->next->id)
247             {
248                 break;
249             }
250             penult = penult->next;
251         }
252         mo = penult->next;
253         penult->next = mo->next;
254     }
255     struct MapObj ** mo_ptr_ptr = target;
256     for (; NULL != * mo_ptr_ptr; mo_ptr_ptr = &(*mo_ptr_ptr)->next);
257     * mo_ptr_ptr = mo;
258     mo->next = NULL;
259 }
260
261
262
263 extern struct MapObj * get_player()
264 {
265     return get_map_object(world.map_objs, 0);
266 }
267
268
269
270 extern struct MapObjDef * get_map_object_def(uint8_t id)
271 {
272     struct MapObjDef * mod = world.map_obj_defs;
273     for (; id != mod->id; mod = mod->next);
274     return mod;
275 }
276
277
278
279 extern void set_object_position(struct MapObj * mo, struct yx_uint16 pos)
280 {
281     mo->pos = pos;
282     struct MapObj * owned = mo->owns;
283     for (; owned != NULL; set_object_position(owned, pos), owned = owned->next);
284 }