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 write_map_objects_monsterdata (void * start, FILE * file) {
53 // Write to file data specific to map objects of type monster.
54 struct Monster * m = (struct Monster *) start;
55 fputc(m->hitpoints, file); }
57 extern void write_map_objects (void * start, FILE * file, void (* w_typedata) (void *, FILE *) ) {
58 // Write into file the map object chain starting at start, use write_type() for object-type specific data.
59 struct MapObj * map_obj;
60 for (map_obj = start; map_obj != 0; map_obj = map_obj->next) {
61 write_uint16_bigendian(map_obj->pos.y + 1, file);
62 write_uint16_bigendian(map_obj->pos.x + 1, file);
63 fputc(map_obj->type, file);
65 w_typedata (map_obj, file); }
66 write_uint16_bigendian(0, file); }
68 extern void read_map_objects_monsterdata (void * start, FILE * file) {
69 // Read from file data speciifc to map objects of type monster.
70 struct Monster * m = (struct Monster *) start;
71 m->hitpoints = fgetc(file); }
73 static struct MapObj * get_next_map_obj (void * start, char * first, size_t size, struct MapObj * map_obj) {
74 // Return pointer to map object of "size". If first in chain ("first" pointing to !0), point "start" to it.
76 struct MapObj * * z = start;
77 map_obj = malloc(size);
81 map_obj->next = malloc(size);
82 map_obj = map_obj->next; }
85 extern void read_map_objects (void * start, FILE * file, size_t size, void (* r_typedata) (void *, FILE *) ) {
86 // Read from file chain of map objects starting at start, use r_typedata() for object-type specific data.
87 struct MapObj * map_obj;
91 test = read_uint16_bigendian(file);
94 map_obj = get_next_map_obj(start, &first, size, map_obj);
95 map_obj->pos.y = test - 1;
96 map_obj->pos.x = read_uint16_bigendian(file) - 1;
97 map_obj->type = fgetc(file);
99 r_typedata (map_obj, file); }
103 extern void build_map_objects_monsterdata (struct MapObjDef * map_obj_def, void * start) {
104 // Build data specific to map objects of type monster.
105 struct Monster * m = (struct Monster *) start;
106 m->map_obj.type = map_obj_def->id;
107 struct MonsterDef * md = (struct MonsterDef *) map_obj_def;
108 m->hitpoints = md->hitpoints_start; }
110 extern void build_map_objects_itemdata (struct MapObjDef * map_obj_def, void * start) {
111 // Build data speciifc to map objects of type data.
112 struct Item * i = (struct Item *) start;
113 i->map_obj.type = map_obj_def->id; }
115 extern void * build_map_objects (struct World * world, void * start, char def_id, unsigned char n,
116 size_t size, void (* b_typedata) (struct MapObjDef *, void *)) {
117 // Build chain of n map objects starting at start, use f() for object-specific data.
121 struct MapObjDef * mod = get_map_obj_def (world, def_id);
122 for (i = 0; i < n; i++) {
123 mo = get_next_map_obj(start, &first, size, mo);
124 mo->pos = find_passable_pos(world->map);
125 b_typedata (mod, mo); }
130 extern struct MapObjDef * get_map_obj_def (struct World * world, char def_id) {
131 // Get pointer to the map object definition with id "def_id".
132 struct MapObjDef * d = NULL;
133 for (d = (struct MapObjDef *) world->monster_def; d->id != def_id && 0 != d->next; d = d->next);
135 for (d = (struct MapObjDef *) world->item_def; d->id != def_id && 0 != d->next; d = d->next);