1 #include "map_objects.h"
9 static struct MapObj * get_next_map_obj (void *, char *, size_t, struct MapObj *);
11 extern void init_map_object_defs (struct World * world, char * filename) {
12 // Initialize map object definitions from file at path "filename".
14 world->monster_def = 0;
15 FILE * file = fopen(filename, "r");
17 textfile_sizes (file, &linemax, NULL);
21 struct ItemDef * * p_p_id = &world->item_def;
22 struct MonsterDef * * p_p_md = &world->monster_def;
23 char * defline = malloc(linemax);
26 while (fgets (defline, linemax, file)) {
28 mod.id = atoi(defline);
29 line_p = strchr(defline, ' ') + 1;
31 mod.mapchar = * (line_p + 2);
35 md.hitpoints_start = atoi (line_p + 4);
36 line_p = strchr (line_p + 4, ' ') + 1; }
37 mod.desc = calloc (strlen (line_p), sizeof(char));
38 memcpy (mod.desc, line_p, strlen(line_p) - 1);
41 * p_p_id = malloc (sizeof (struct ItemDef));
43 p_p_id = (struct ItemDef * *) * p_p_id; }
46 * p_p_md = malloc (sizeof (struct MonsterDef));
48 p_p_md = (struct MonsterDef * *) * p_p_md; } }
52 extern void readwrite_map_objects_dummy (void * dummy, FILE * file) {
53 // Dummy function for calls of (write|read)_map_objects on map objects without specific attributes.
56 extern void write_map_objects_monsterdata (void * start, FILE * file) {
57 // Write to file data specific to map objects of type monster.
58 struct Monster * m = (struct Monster *) start;
59 fputc(m->hitpoints, file); }
61 extern void write_map_objects (void * start, FILE * file, void (* w_typedata) (void *, FILE *) ) {
62 // Write into file the map object chain starting at start, use write_type() for object-type specific data.
63 struct MapObj * map_obj;
64 for (map_obj = start; map_obj != 0; map_obj = map_obj->next) {
65 write_uint16_bigendian(map_obj->pos.y + 1, file);
66 write_uint16_bigendian(map_obj->pos.x + 1, file);
67 fputc(map_obj->type, file);
68 w_typedata (map_obj, file); }
69 write_uint16_bigendian(0, file); }
71 extern void read_map_objects_monsterdata (void * start, FILE * file) {
72 // Read from file data speciifc to map objects of type monster.
73 struct Monster * m = (struct Monster *) start;
74 m->hitpoints = fgetc(file); }
76 static struct MapObj * get_next_map_obj (void * start, char * first, size_t size, struct MapObj * map_obj) {
77 // Return pointer to map object of "size". If first in chain ("first" pointing to !0), point "start" to it.
79 struct MapObj * * z = start;
80 map_obj = malloc(size);
84 map_obj->next = malloc(size);
85 map_obj = map_obj->next; }
88 extern void read_map_objects (void * start, FILE * file, size_t size, void (* r_typedata) (void *, FILE *) ) {
89 // Read from file chain of map objects starting at start, use r_typedata() for object-type specific data.
90 struct MapObj * map_obj;
94 test = read_uint16_bigendian(file);
97 map_obj = get_next_map_obj(start, &first, size, map_obj);
98 map_obj->pos.y = test - 1;
99 map_obj->pos.x = read_uint16_bigendian(file) - 1;
100 map_obj->type = fgetc(file);
101 r_typedata (map_obj, file); }
105 extern void build_map_objects_monsterdata (struct MapObjDef * map_obj_def, void * start) {
106 // Build data specific to map objects of type monster.
107 struct Monster * m = (struct Monster *) start;
108 m->map_obj.type = map_obj_def->id;
109 struct MonsterDef * md = (struct MonsterDef *) map_obj_def;
110 m->hitpoints = md->hitpoints_start; }
112 extern void build_map_objects_itemdata (struct MapObjDef * map_obj_def, void * start) {
113 // Build data speciifc to map objects of type data.
114 struct Item * i = (struct Item *) start;
115 i->map_obj.type = map_obj_def->id; }
117 extern void * build_map_objects (struct World * world, void * start, char def_id, unsigned char n,
118 size_t size, void (* b_typedata) (struct MapObjDef *, void *)) {
119 // Build chain of n map objects starting at start, use f() for object-specific data.
123 struct MapObjDef * mod = get_map_obj_def (world, def_id);
124 for (i = 0; i < n; i++) {
125 mo = get_next_map_obj(start, &first, size, mo);
126 mo->pos = find_passable_pos(world->map);
127 b_typedata (mod, mo); }
132 extern struct MapObjDef * get_map_obj_def (struct World * world, char def_id) {
133 // Get pointer to the map object definition with id "def_id".
134 struct MapObjDef * d = NULL;
135 for (d = (struct MapObjDef *) world->monster_def; d->id != def_id && 0 != d->next; d = d->next);
137 for (d = (struct MapObjDef *) world->item_def; d->id != def_id && 0 != d->next; d = d->next);