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);
102 while (fgets(defline, linemax, file))
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)
115 md.hitpoints_start = atoi (line_p + 4);
116 line_p = strchr (line_p + 4, ' ') + 1;
118 mod.desc = calloc (strlen (line_p), sizeof(char));
119 memcpy (mod.desc, line_p, strlen(line_p) - 1);
120 if ('i' == mod.m_or_i)
122 id.map_obj_def = mod;
123 * p_p_id = malloc(sizeof(struct ItemDef));
125 p_p_id = (struct ItemDef * *) * p_p_id;
129 md.map_obj_def = mod;
130 * p_p_md = malloc(sizeof(struct MonsterDef));
132 p_p_md = (struct MonsterDef * *) * p_p_md;
141 extern uint8_t write_map_objects(struct World * world, void * start,
144 struct MapObj * map_obj;
145 struct MapObjDef * mod;
146 for (map_obj = start; map_obj != 0; map_obj = map_obj->next)
148 if ( write_uint8(map_obj->type, file)
149 || write_uint16_bigendian(map_obj->pos.y + 1, file)
150 || write_uint16_bigendian(map_obj->pos.x + 1, file))
154 mod = get_map_obj_def(world, map_obj->type);
155 if ('m' == mod->m_or_i)
157 if (write_map_objects_monsterdata(map_obj, file))
163 return write_uint16_bigendian(0, file);
168 extern uint8_t read_map_objects(struct World * world, void * start, FILE * file)
170 struct MapObj * map_obj;
171 struct MapObjDef * mod;
176 uint16_t read_uint16 = 0;
180 if (read_uint16_bigendian(file, &read_uint16))
184 if (0 == read_uint16)
188 fseek(file, pos, SEEK_SET);
189 if (read_uint8(file, &type))
193 mod = get_map_obj_def(world, type);
194 if ('m' == mod->m_or_i)
196 size = sizeof(struct Monster);
200 size = sizeof(struct Item);
202 map_obj = get_next_map_obj(start, &first, size, map_obj);
203 map_obj->type = type;
204 if ( read_uint16_bigendian(file, &map_obj->pos.y)
205 || read_uint16_bigendian(file, &map_obj->pos.x))
211 if ('m' == mod->m_or_i)
213 if (read_map_objects_monsterdata(map_obj, file))
228 extern void * build_map_objects(struct World * world, void * start, char def_id,
234 struct MapObjDef * mod = get_map_obj_def(world, def_id);
236 if ('i' == mod->m_or_i)
238 size = sizeof(struct Item);
242 size = sizeof(struct Monster);
244 for (i = 0; i < n; i++)
246 mo = get_next_map_obj(start, &first, size, mo);
247 mo->pos = find_passable_pos(world->map);
248 if ('i' == mod->m_or_i)
250 build_map_objects_itemdata(mod, mo);
254 build_map_objects_monsterdata(mod, mo);
267 extern struct MapObjDef * get_map_obj_def (struct World * world, char def_id)
269 struct MapObjDef * d = NULL;
270 for (d = (struct MapObjDef *) world->monster_def;
271 d->id != def_id && 0 != d->next;
275 for (d = (struct MapObjDef *) world->item_def;
276 d->id != def_id && 0 != d->next;