3 #include "map_objects.h"
4 #include <stdlib.h> /* for malloc(), calloc(), free(), atoi() */
5 #include <stdio.h> /* for FILE typedef */
6 #include <string.h> /* for strchr(), strlen(), memcpy() */
7 #include "readwrite.h" /* for all the map object (def) loading/saving */
8 #include "misc.h" /* for textfile_sizes(), find_passable_pos() */
9 #include "main.h" /* for World struct */
13 /* Return pointer to newly allocated map object struct of size "size". If first
14 * in map object chain ("first" pointing to !0), point "start" to it.
16 static struct MapObj * get_next_map_obj(void * start, char * first,
17 size_t size, struct MapObj * map_obj);
21 static struct MapObj * get_next_map_obj(void * start, char * first,
22 size_t size, struct MapObj * map_obj)
26 struct MapObj * * z = start;
27 map_obj = malloc(size);
33 map_obj->next = malloc(size);
34 map_obj = map_obj->next;
41 extern void init_map_object_defs(struct World * world, char * filename)
44 world->monster_def = 0;
45 FILE * file = fopen(filename, "r");
47 textfile_sizes (file, &linemax, NULL);
51 struct ItemDef * * p_p_id = &world->item_def;
52 struct MonsterDef * * p_p_md = &world->monster_def;
53 char * defline = malloc(linemax);
56 while (fgets(defline, linemax, file))
59 mod.id = atoi(defline);
60 line_p = strchr(defline, ' ') + 1;
62 mod.mapchar = * (line_p + 2);
69 md.hitpoints_start = atoi (line_p + 4);
70 line_p = strchr (line_p + 4, ' ') + 1;
72 mod.desc = calloc (strlen (line_p), sizeof(char));
73 memcpy (mod.desc, line_p, strlen(line_p) - 1);
77 * p_p_id = malloc(sizeof(struct ItemDef));
79 p_p_id = (struct ItemDef * *) * p_p_id;
84 * p_p_md = malloc(sizeof(struct MonsterDef));
86 p_p_md = (struct MonsterDef * *) * p_p_md;
95 extern void write_map_objects(void * start, FILE * file,
96 void (* w_typedata) (void *, FILE *) )
98 struct MapObj * map_obj;
99 for (map_obj = start; map_obj != 0; map_obj = map_obj->next)
101 write_uint16_bigendian(map_obj->pos.y + 1, file);
102 write_uint16_bigendian(map_obj->pos.x + 1, file);
103 fputc(map_obj->type, file);
106 w_typedata (map_obj, file);
109 write_uint16_bigendian(0, file);
114 extern void read_map_objects(void * start, FILE * file, size_t size,
115 void (* r_typedata) (void *, FILE *) )
117 struct MapObj * map_obj;
122 test = read_uint16_bigendian(file);
127 map_obj = get_next_map_obj(start, &first, size, map_obj);
128 map_obj->pos.y = test - 1;
129 map_obj->pos.x = read_uint16_bigendian(file) - 1;
130 map_obj->type = fgetc(file);
133 r_typedata(map_obj, file);
144 extern void write_map_objects_monsterdata(void * start, FILE * file)
146 struct Monster * m = (struct Monster *) start;
147 fputc(m->hitpoints, file);
152 extern void read_map_objects_monsterdata (void * start, FILE * file)
154 struct Monster * m = (struct Monster *) start;
155 m->hitpoints = fgetc(file);
160 extern void * build_map_objects(struct World * world, void * start, char def_id,
161 unsigned char n, size_t size,
162 void (* b_typedata) (struct MapObjDef *,
168 struct MapObjDef * mod = get_map_obj_def(world, def_id);
169 for (i = 0; i < n; i++)
171 mo = get_next_map_obj(start, &first, size, mo);
172 mo->pos = find_passable_pos(world->map);
184 extern void build_map_objects_itemdata(struct MapObjDef * map_obj_def,
187 struct Item * i = (struct Item *) start;
188 i->map_obj.type = map_obj_def->id;
193 extern void build_map_objects_monsterdata(struct MapObjDef * map_obj_def,
196 struct Monster * m = (struct Monster *) start;
197 m->map_obj.type = map_obj_def->id;
198 struct MonsterDef * md = (struct MonsterDef *) map_obj_def;
199 m->hitpoints = md->hitpoints_start;
204 extern struct MapObjDef * get_map_obj_def (struct World * world, char def_id)
206 struct MapObjDef * d = NULL;
207 for (d = (struct MapObjDef *) world->monster_def;
208 d->id != def_id && 0 != d->next;
212 for (d = (struct MapObjDef *) world->item_def;
213 d->id != def_id && 0 != d->next;