home · contact · privacy
8cb1fca18b73f41926db759dc17cf7deae66a2b6
[plomrogue] / src / map_objects.c
1 /* map_objects.c */
2
3 #include "map_objects.h"
4 #include <stdlib.h> /* for malloc(), calloc(), free(), atoi() */
5 #include <stdint.h> /* for uint8_t */
6 #include <stdio.h> /* for FILE typedef */
7 #include <string.h> /* for strchr(), strlen(), memcpy()  */
8 #include "readwrite.h" /* for [read/write]_uint[8/16/23][_bigendian]() */
9 #include "misc.h" /* for textfile_sizes(), find_passable_pos() */
10 #include "main.h" /* for World struct */
11
12
13
14 /* Return pointer to newly allocated map object struct of size "size". If first
15  * in map object chain ("first" pointing to !0), point "start" to it.
16  */
17 static struct MapObj * get_next_map_obj(void * start, char * first,
18                                         size_t size, struct MapObj * map_obj);
19
20
21
22 /* Map-object-type-specific helpers to (build|write|read)_map_objects(). */
23 static void build_map_objects_itemdata(struct MapObjDef * map_obj_def,
24                                        void * start);
25 static void build_map_objects_monsterdata(struct MapObjDef * map_obj_def,
26                                           void * start);
27 static uint8_t write_map_objects_monsterdata(void * start, FILE * file);
28 static uint8_t read_map_objects_monsterdata( void * start, FILE * file);
29
30
31
32 static struct MapObj * get_next_map_obj(void * start, char * first,
33                                         size_t size, struct MapObj * map_obj)
34 {
35     if (* first)
36     {
37         struct MapObj * * z = start;
38         map_obj = malloc(size);
39         * z = map_obj;
40         * first = 0;
41     }
42     else
43     {
44         map_obj->next = malloc(size);
45         map_obj = map_obj->next;
46     }
47     return map_obj;
48 }
49
50
51
52 static void build_map_objects_itemdata(struct MapObjDef * map_obj_def,
53                                        void * start)
54 {
55   struct Item * i = (struct Item *) start;
56   i->map_obj.type = map_obj_def->id;
57 }
58
59
60
61 static void build_map_objects_monsterdata(struct MapObjDef * map_obj_def,
62                                           void * start)
63 {
64     struct Monster * m = (struct Monster *) start;
65     m->map_obj.type = map_obj_def->id;
66     struct MonsterDef * md = (struct MonsterDef *) map_obj_def;
67     m->hitpoints = md->hitpoints_start;
68 }
69
70
71
72 static uint8_t write_map_objects_monsterdata(void * start, FILE * file)
73 {
74     struct Monster * m = (struct Monster *) start;
75     return write_uint8(m->hitpoints, file);
76 }
77
78
79
80 static uint8_t read_map_objects_monsterdata (void * start, FILE * file)
81 {
82     struct Monster * m = (struct Monster *) start;
83     return read_uint8(file, &m->hitpoints);
84 }
85
86
87
88 extern void init_map_object_defs(struct World * world, char * filename)
89 {
90     world->item_def    = 0;
91     world->monster_def = 0;
92     FILE * file = fopen(filename, "r");
93     uint16_t linemax;
94     textfile_sizes (file, &linemax, NULL);
95     struct MapObjDef  mod;
96     struct ItemDef    id;
97     struct MonsterDef md;
98     struct ItemDef    * * p_p_id  = &world->item_def;
99     struct MonsterDef * * p_p_md  = &world->monster_def;
100     char * defline = malloc(linemax);
101     char * line_p;
102     while (fgets(defline, linemax, file))
103     {
104         mod.next    = 0;
105         mod.id      = atoi(defline);
106         line_p      = strchr(defline, ' ') + 1;
107         mod.m_or_i  = * line_p;
108         mod.mapchar = * (line_p + 2);
109         if ('i' == mod.m_or_i)
110         {
111             line_p = line_p + 5;
112         }
113         else
114         {
115             md.corpse_id       = atoi   (line_p + 4);
116             md.hitpoints_start = atoi   (line_p + 6);
117             line_p             = strchr (line_p + 6, ' ') + 1;
118         }
119         mod.desc = calloc (strlen (line_p), sizeof(char));
120         memcpy (mod.desc, line_p, strlen(line_p) - 1);
121         if ('i' == mod.m_or_i)
122         {
123             id.map_obj_def = mod;
124             * p_p_id       = malloc(sizeof(struct ItemDef));
125             * * p_p_id     = id;
126             p_p_id         = (struct ItemDef    * *) * p_p_id;
127         }
128         else
129         {
130             md.map_obj_def = mod;
131             * p_p_md       = malloc(sizeof(struct MonsterDef));
132             * * p_p_md     = md;
133             p_p_md         = (struct MonsterDef * *) * p_p_md;
134         }
135     }
136     free(defline);
137     fclose(file);
138 };
139
140
141
142 extern uint8_t write_map_objects(struct World * world, void * start,
143                                  FILE * file)
144 {
145     struct MapObj * map_obj;
146     struct MapObjDef * mod;
147     for (map_obj = start; map_obj != 0; map_obj = map_obj->next)
148     {
149         if (   write_uint8(map_obj->type, file)
150             || write_uint16_bigendian(map_obj->pos.y + 1, file)
151             || write_uint16_bigendian(map_obj->pos.x + 1, file))
152         {
153             return 1;
154         }
155         mod = get_map_obj_def(world, map_obj->type);
156         if ('m' == mod->m_or_i)
157         {
158             if (write_map_objects_monsterdata(map_obj, file))
159             {
160                 return 1;
161             }
162         }
163     }
164     return write_uint16_bigendian(0, file);
165 }
166
167
168
169 extern uint8_t read_map_objects(struct World * world, void * start, FILE * file)
170 {
171     struct MapObj * map_obj;
172     struct MapObjDef * mod;
173     size_t size;
174     uint8_t type;
175     char first = 1;
176     long pos;
177     uint16_t read_uint16 = 0;
178     while (1)
179     {
180         pos = ftell(file);
181         if (read_uint16_bigendian(file, &read_uint16))
182         {
183             return 1;
184         }
185         if (0 == read_uint16)
186         {
187             break;
188         }
189         fseek(file, pos, SEEK_SET);
190         if (read_uint8(file, &type))
191         {
192             return 1;
193         }
194         mod = get_map_obj_def(world, type);
195         if ('m' == mod->m_or_i)
196         {
197             size = sizeof(struct Monster);
198         }
199         else
200         {
201             size = sizeof(struct Item);
202         }
203         map_obj = get_next_map_obj(start, &first, size, map_obj);
204         map_obj->type = type;
205         if (   read_uint16_bigendian(file, &map_obj->pos.y)
206             || read_uint16_bigendian(file, &map_obj->pos.x))
207         {
208             return 1;
209         }
210         map_obj->pos.y--;
211         map_obj->pos.x--;
212         if ('m' == mod->m_or_i)
213         {
214             if (read_map_objects_monsterdata(map_obj, file))
215             {
216                 return 1;
217             }
218         }
219     }
220     if (!first)
221     {
222         map_obj->next = 0;
223     }
224     return 0;
225 }
226
227
228
229 extern void * build_map_objects(struct World * world, void * start, char def_id,
230                                 uint8_t n)
231 {
232     uint8_t i;
233     struct MapObj * mo;
234     char first = 1;
235     struct MapObjDef * mod = get_map_obj_def(world, def_id);
236     size_t size = 0;
237     if ('i' == mod->m_or_i)
238     {
239         size = sizeof(struct Item);
240     }
241     else
242     {
243         size = sizeof(struct Monster);
244     }
245     for (i = 0; i < n; i++)
246     {
247         mo = get_next_map_obj(start, &first, size, mo);
248         mo->pos = find_passable_pos(world->map);
249         if ('i' == mod->m_or_i)
250         {
251             build_map_objects_itemdata(mod, mo);
252         }
253         else
254         {
255             build_map_objects_monsterdata(mod, mo);
256         }
257
258     }
259     if (!first)
260     {
261         mo->next = 0;
262     }
263     return &mo->next;
264 }
265
266
267
268 extern struct MapObjDef * get_map_obj_def (struct World * world, char def_id)
269 {
270     struct MapObjDef * d = NULL;
271     for (d = (struct MapObjDef *) world->monster_def;
272          d->id != def_id && 0 != d->next;
273          d = d->next);
274     if (d->id != def_id)
275     {
276         for (d = (struct MapObjDef *) world->item_def;
277              d->id != def_id && 0 != d->next;
278              d = d->next);
279     }
280     return d;
281 }