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() */
14 #include "yx_uint16.h" /* for yx_uint16 struct, yx_uint16_cmp() */
18 extern void init_map_object_defs(struct World * world, char * filename)
20 char * f_name = "init_map_object_defs()";
21 FILE * file = try_fopen(filename, "r", world, f_name);
22 uint16_t linemax = get_linemax(file, world, f_name);
23 struct MapObjDef ** last_mod_ptr_ptr = &world->map_obj_defs;
25 char line[linemax + 1];
26 while (try_fgets(line, linemax + 1, file, world, f_name))
28 struct MapObjDef * mod;
29 mod = try_malloc(sizeof(struct MapObjDef), world, f_name);
31 mod->id = atoi(strtok(line, delim));
32 mod->corpse_id = atoi(strtok(NULL, delim));
33 mod->char_on_map = * strtok(NULL, delim);
34 mod->lifepoints = atoi(strtok(NULL, delim));
35 char * name = strtok(NULL, "\n");
36 mod->name = try_malloc(strlen(name) + 1, world, f_name);
37 memcpy(mod->name, name, strlen(name) + 1);
38 * last_mod_ptr_ptr = mod;
39 last_mod_ptr_ptr = &mod->next;
41 try_fclose(file, world, f_name);
46 extern void free_map_object_defs(struct MapObjDef * mod_start)
48 if (NULL == mod_start)
52 free_map_object_defs(mod_start->next);
53 free(mod_start->name);
59 extern void write_map_objects(struct World * world, FILE * file)
61 char * f_name = "write_map_objects()";
62 struct MapObj * mo = world->map_objs;
63 uint8_t size = 3 + 1 + 3 + 1 + 3 + 1 + 5 + 1 + 5 + 1;
67 sprintf(line, "%d %d %d %d %d\n",
68 mo->id, mo->type, mo->lifepoints, mo->pos.y, mo->pos.x);
69 try_fwrite(line, strlen(line), 1, file, world, f_name);
76 extern void read_map_objects(struct World * world, FILE * file, char * line,
79 char * f_name = "read_map_objects()";
80 struct MapObj ** mo_ptr_ptr = &world->map_objs;
83 while (try_fgets(line, linemax + 1, file, world, f_name))
85 mo = malloc(sizeof(struct MapObj));
87 mo->id = atoi(strtok(line, delim));
88 if (mo->id > world->map_obj_count)
90 world->map_obj_count = mo->id;
92 mo->type = atoi(strtok(NULL, delim));
93 mo->lifepoints = atoi(strtok(NULL, delim));
94 mo->pos.y = atoi(strtok(NULL, delim));
95 mo->pos.x = atoi(strtok(NULL, delim));
97 mo_ptr_ptr = &mo->next;
103 extern void add_map_object(struct World * world, uint8_t type)
105 char * f_name = "add_map_object";
106 struct MapObjDef * mod = get_map_object_def(world, type);
107 struct MapObj * mo = try_malloc(sizeof(struct MapObj), world, f_name);
108 mo->id = world->map_obj_count;
109 world->map_obj_count++;
111 mo->lifepoints = mod->lifepoints;
114 struct yx_uint16 pos = find_passable_pos(world->map);
115 struct MapObj * mo_ptr;
117 for (mo_ptr = world->map_objs;
119 mo_ptr = mo_ptr->next)
121 if (yx_uint16_cmp(&pos, &mo_ptr->pos) && 0 != mo_ptr->lifepoints)
134 struct MapObj ** last_ptr_ptr = &world->map_objs;
135 struct MapObj * mo_ptr;
136 while (NULL != * last_ptr_ptr)
138 mo_ptr = * last_ptr_ptr;
139 last_ptr_ptr = & mo_ptr->next;
146 extern void add_map_objects(struct World * world, uint8_t type, uint8_t n)
149 for (i = 0; i < n; i++)
151 add_map_object(world, type);
157 extern void free_map_objects(struct MapObj * mo_start)
159 if (NULL == mo_start)
163 free_map_objects(mo_start->next);
169 extern struct MapObj * get_player(struct World * world)
171 struct MapObj * ptr = world->map_objs;
188 extern struct MapObjDef * get_map_object_def(struct World * w, uint8_t id)
190 struct MapObjDef * mod = w->map_obj_defs;
191 while (id != mod->id)