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 */
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.
17 static struct MapObj * get_next_map_obj(void * start, char * first,
18 size_t size, struct MapObj * map_obj);
22 /* Map-object-type-specific helpers to (build|write|read)_map_objects(). */
23 static void build_map_objects_itemdata(struct MapObjDef * map_obj_def,
25 static void build_map_objects_monsterdata(struct MapObjDef * map_obj_def,
27 static uint8_t write_map_objects_monsterdata(void * start, FILE * file);
28 static uint8_t read_map_objects_monsterdata( void * start, FILE * file);
32 static struct MapObj * get_next_map_obj(void * start, char * first,
33 size_t size, struct MapObj * map_obj)
37 struct MapObj * * z = start;
38 map_obj = malloc(size);
44 map_obj->next = malloc(size);
45 map_obj = map_obj->next;
52 static void build_map_objects_itemdata(struct MapObjDef * map_obj_def,
55 struct Item * i = (struct Item *) start;
56 i->map_obj.type = map_obj_def->id;
61 static void build_map_objects_monsterdata(struct MapObjDef * map_obj_def,
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;
72 static uint8_t write_map_objects_monsterdata(void * start, FILE * file)
74 struct Monster * m = (struct Monster *) start;
75 return write_uint8(m->hitpoints, file);
80 static uint8_t read_map_objects_monsterdata (void * start, FILE * file)
82 struct Monster * m = (struct Monster *) start;
83 return read_uint8(file, &m->hitpoints);
88 extern void init_map_object_defs(struct World * world, char * filename)
91 world->monster_def = 0;
92 FILE * file = fopen(filename, "r");
94 textfile_sizes (file, &linemax, NULL);
98 struct ItemDef * * p_p_id = &world->item_def;
99 struct MonsterDef * * p_p_md = &world->monster_def;
100 char * defline = malloc(linemax);
103 while (fgets(defline, linemax, file))
106 mod.id = atoi(strtok(defline, delim));
107 mod.m_or_i = * strtok(NULL, delim);
108 mod.mapchar = * strtok(NULL, delim);
109 if ('i' == mod.m_or_i)
111 line_p = strtok(NULL, delim);
115 md.corpse_id = atoi(strtok(NULL, delim));
116 md.hitpoints_start = atoi(strtok(NULL, delim));
117 line_p = strtok(NULL, delim);
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)
123 id.map_obj_def = mod;
124 * p_p_id = malloc(sizeof(struct ItemDef));
126 p_p_id = (struct ItemDef * *) * p_p_id;
130 md.map_obj_def = mod;
131 * p_p_md = malloc(sizeof(struct MonsterDef));
133 p_p_md = (struct MonsterDef * *) * p_p_md;
142 extern uint8_t write_map_objects(struct World * world, void * start,
145 struct MapObj * map_obj;
146 struct MapObjDef * mod;
147 for (map_obj = start; map_obj != 0; map_obj = map_obj->next)
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))
155 mod = get_map_obj_def(world, map_obj->type);
156 if ('m' == mod->m_or_i)
158 if (write_map_objects_monsterdata(map_obj, file))
164 return write_uint16_bigendian(0, file);
169 extern uint8_t read_map_objects(struct World * world, void * start, FILE * file)
171 struct MapObj * map_obj;
172 struct MapObjDef * mod;
177 uint16_t read_uint16 = 0;
181 if (read_uint16_bigendian(file, &read_uint16))
185 if (0 == read_uint16)
189 fseek(file, pos, SEEK_SET);
190 if (read_uint8(file, &type))
194 mod = get_map_obj_def(world, type);
195 if ('m' == mod->m_or_i)
197 size = sizeof(struct Monster);
201 size = sizeof(struct Item);
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))
212 if ('m' == mod->m_or_i)
214 if (read_map_objects_monsterdata(map_obj, file))
229 extern void * build_map_objects(struct World * world, void * start, char def_id,
235 struct MapObjDef * mod = get_map_obj_def(world, def_id);
237 if ('i' == mod->m_or_i)
239 size = sizeof(struct Item);
243 size = sizeof(struct Monster);
245 for (i = 0; i < n; i++)
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)
251 build_map_objects_itemdata(mod, mo);
255 build_map_objects_monsterdata(mod, mo);
268 extern struct MapObjDef * get_map_obj_def (struct World * world, char def_id)
270 struct MapObjDef * d = NULL;
271 for (d = (struct MapObjDef *) world->monster_def;
272 d->id != def_id && 0 != d->next;
276 for (d = (struct MapObjDef *) world->item_def;
277 d->id != def_id && 0 != d->next;