3 #include "map_objects.h"
4 #include <stdlib.h> /* for free(), atoi() */
5 #include <stdint.h> /* for uint8_t */
6 #include <stdio.h> /* for FILE typedef */
7 #include <string.h> /* for strchr(), strlen(), memcpy(), strtok() */
8 #include "readwrite.h" /* for get_linemax(), try_fopen(), try_fclose()
9 * [read/write]_uint[8/16/23][_bigendian]()
11 #include "misc.h" /* for try_malloc(), try_calloc(), find_passable_pos() */
12 #include "main.h" /* for World struct */
13 #include "rexit.h" /* for err_exit() */
17 extern void init_map_object_defs(struct World * world, char * filename)
19 char * f_name = "init_map_object_defs()";
20 FILE * file = try_fopen(filename, "r", world, f_name);
21 uint16_t linemax = get_linemax(file, world, f_name);
22 struct MapObjDef ** last_mod_ptr_ptr = &world->map_obj_defs;
24 char line[linemax + 1];
25 while (try_fgets(line, linemax + 1, file, world, f_name))
27 struct MapObjDef * mod;
28 mod = try_malloc(sizeof(struct MapObjDef), world, f_name);
30 mod->id = atoi(strtok(line, delim));
31 mod->corpse_id = atoi(strtok(NULL, delim));
32 mod->char_on_map = * strtok(NULL, delim);
33 mod->lifepoints = atoi(strtok(NULL, delim));
34 char * name = strtok(NULL, "\n");
35 mod->name = try_malloc(strlen(name) + 1, world, f_name);
36 memcpy(mod->name, name, strlen(name) + 1);
37 * last_mod_ptr_ptr = mod;
38 last_mod_ptr_ptr = &mod->next;
40 try_fclose(file, world, f_name);
45 extern void free_map_object_defs(struct MapObjDef * mod_start)
47 if (NULL == mod_start)
51 free_map_object_defs(mod_start->next);
52 free(mod_start->name);
58 extern void write_map_objects(struct World * world, FILE * file)
60 char * f_name = "write_map_objects()";
61 struct MapObj * mo = world->map_objs;
62 uint8_t size = 3 + 1 + 3 + 1 + 3 + 1 + 5 + 1 + 5 + 1;
66 sprintf(line, "%d %d %d %d %d\n",
67 mo->id, mo->type, mo->lifepoints, mo->pos.y, mo->pos.x);
68 try_fwrite(line, strlen(line), 1, file, world, f_name);
75 extern void read_map_objects(struct World * world, FILE * file, char * line,
78 char * f_name = "read_map_objects()";
79 struct MapObj ** mo_ptr_ptr = &world->map_objs;
81 while (try_fgets(line, linemax + 1, file, world, f_name))
83 struct MapObj * mo = malloc(sizeof(struct MapObj));
85 mo->id = atoi(strtok(line, delim));
86 if (mo->id > world->map_obj_count)
88 world->map_obj_count = mo->id;
90 mo->type = atoi(strtok(NULL, delim));
91 mo->lifepoints = atoi(strtok(NULL, delim));
92 mo->pos.y = atoi(strtok(NULL, delim));
93 mo->pos.x = atoi(strtok(NULL, delim));
95 mo_ptr_ptr = &mo->next;
101 extern void add_map_object(struct World * world, uint8_t type)
103 char * f_name = "add_map_object";
104 struct MapObjDef * mod = get_map_object_def(world, type);
105 struct MapObj * mo = try_malloc(sizeof(struct MapObj), world, f_name);
106 mo->id = world->map_obj_count;
107 world->map_obj_count++;
109 mo->lifepoints = mod->lifepoints;
110 mo->pos = find_passable_pos(world->map);
111 mo->next = world->map_objs;
112 world->map_objs = mo;
117 extern void add_map_objects(struct World * world, uint8_t type, uint8_t n)
120 for (i = 0; i < n; i++)
122 add_map_object(world, type);
128 extern void free_map_objects(struct MapObj * mo_start)
130 if (NULL == mo_start)
134 free_map_objects(mo_start->next);
140 extern struct MapObjDef * get_map_object_def(struct World * w, uint8_t id)
142 struct MapObjDef * mod = w->map_obj_defs;
143 while (id != mod->id)