1 /* src/server/map_objects.c */
3 #include "map_objects.h"
4 #include <stddef.h> /* NULL */
5 #include <stdio.h> /* FILE typedef */
6 #include <stdint.h> /* uint8_t, uint16_t */
7 #include <stdlib.h> /* free(), atoi() */
8 #include <string.h> /* strlen(), memcpy(), strtok() */
9 #include "../common/readwrite.h" /* try_fopen(), try_fclose(), try_fgets(),
12 #include "../common/try_malloc.h" /* try_malloc() */
13 #include "../common/yx_uint16.h" /* yx_uint16 struct */
14 #include "cleanup.h" /* set_cleanup_flag() */
15 #include "map.h" /* is_passable() */
16 #include "rrand.h" /* rrand() */
17 #include "world.h" /* global world */
18 #include "yx_uint16.h" /* yx_uint16_cmp() */
22 /* Return pointer to map object of "id" in chain starting at "ptr". */
23 static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id);
25 /* Return random passable (as by is_passable()) position on world.map. */
26 static struct yx_uint16 find_passable_pos();
28 /* Add object of "type" to map on random position. Don't place actor on actor.*/
29 static void add_map_object(uint8_t type);
33 static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id)
37 if (NULL == ptr || id == ptr->id)
41 struct MapObj * owned_object = get_map_object(ptr->owns, id);
42 if (NULL != owned_object)
52 static struct yx_uint16 find_passable_pos()
55 for (pos.y = pos.x = 0; 0 == is_passable(pos);)
57 pos.y = rrand() % world.map.size.y;
58 pos.x = rrand() % world.map.size.x;
65 static void add_map_object(uint8_t type)
67 char * f_name = "add_map_object()";
68 struct MapObjDef * mod = get_map_object_def(type);
69 struct MapObj * mo = try_malloc(sizeof(struct MapObj), f_name);
70 mo->id = world.map_obj_count++;
72 mo->lifepoints = mod->lifepoints;
75 struct yx_uint16 pos = find_passable_pos(world.map);
76 struct MapObj * mo_ptr;
78 for (mo_ptr = world.map_objs; mo_ptr != NULL; mo_ptr = mo_ptr->next)
80 if (yx_uint16_cmp(&pos, &mo_ptr->pos) && 0 != mo_ptr->lifepoints)
97 struct MapObj ** mo_ptr_ptr = &world.map_objs;
98 for (; NULL != * mo_ptr_ptr; mo_ptr_ptr = &(*mo_ptr_ptr)->next);
104 extern void init_map_object_defs(char * filename)
106 char * f_name = "init_map_object_defs()";
107 FILE * file = try_fopen(filename, "r", f_name);
108 uint16_t linemax = textfile_sizes(file, NULL);
109 struct MapObjDef ** last_mod_ptr_ptr = &world.map_obj_defs;
111 char line[linemax + 1];
112 while (try_fgets(line, linemax + 1, file, f_name))
114 struct MapObjDef * mod = try_malloc(sizeof(struct MapObjDef), f_name);
116 mod->id = atoi(strtok(line, delim));
117 mod->corpse_id = atoi(strtok(NULL, delim));
118 mod->char_on_map = * strtok(NULL, delim);
119 mod->lifepoints = atoi(strtok(NULL, delim));
120 char * name = strtok(NULL, "\n");
121 mod->name = try_malloc(strlen(name) + 1, f_name);
122 memcpy(mod->name, name, strlen(name) + 1);
123 * last_mod_ptr_ptr = mod;
124 last_mod_ptr_ptr = &mod->next;
126 try_fclose(file, f_name);
127 set_cleanup_flag(CLEANUP_MAP_OBJECT_DEFS);
132 extern void free_map_object_defs(struct MapObjDef * mod_start)
134 if (NULL == mod_start)
138 free_map_object_defs(mod_start->next);
139 free(mod_start->name);
146 extern void add_map_objects(uint8_t type, uint8_t n)
149 for (i = 0; i < n; i++)
151 add_map_object(type);
157 extern void free_map_objects(struct MapObj * mo_start)
159 if (NULL == mo_start)
163 free_map_objects(mo_start->owns);
164 free_map_objects(mo_start->next);
166 if (mo_start == world.map_objs)
168 world.map_objs = NULL;
174 extern void own_map_object(struct MapObj ** target, struct MapObj ** source,
178 if (id == (*source)->id)
185 struct MapObj * penult = * source;
188 if (id == penult->next->id)
192 penult = penult->next;
195 penult->next = mo->next;
197 struct MapObj ** mo_ptr_ptr = target;
198 for (; NULL != * mo_ptr_ptr; mo_ptr_ptr = &(*mo_ptr_ptr)->next);
205 extern struct MapObj * get_player()
207 return get_map_object(world.map_objs, 0);
212 extern struct MapObjDef * get_map_object_def(uint8_t id)
214 struct MapObjDef * mod = world.map_obj_defs;
215 for (; id != mod->id; mod = mod->next);
221 extern void set_object_position(struct MapObj * mo, struct yx_uint16 pos)
224 struct MapObj * owned = mo->owns;
225 for (; owned != NULL; set_object_position(owned, pos), owned = owned->next);