1 /* src/server/map_objects.c */
3 #include "map_objects.h"
4 #include <stdio.h> /* FILE typedef */
5 #include <stdint.h> /* uint8_t, uint16_t */
6 #include <stdlib.h> /* free(), atoi() */
7 #include <string.h> /* strlen(), memcpy(), strtok() */
8 #include "../common/readwrite.h" /* try_fopen(), try_fclose(), try_fgets(),
11 #include "../common/try_malloc.h" /* try_malloc() */
12 #include "../common/yx_uint16.h" /* yx_uint16 struct */
13 #include "cleanup.h" /* set_cleanup_flag() */
14 #include "map.h" /* is_passable() */
15 #include "rrand.h" /* rrand() */
16 #include "world.h" /* global world */
17 #include "yx_uint16.h" /* yx_uint16_cmp() */
21 /* Return pointer to map object of "id" in chain starting at "ptr". */
22 static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id);
24 /* Return random passable (as by is_passable()) position on world.map. */
25 static struct yx_uint16 find_passable_pos();
27 /* Add object of "type" to map on random position. Don't place actor on actor.*/
28 static void add_map_object(uint8_t type);
32 static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id)
36 if (NULL == ptr || id == ptr->id)
40 struct MapObj * owned_object = get_map_object(ptr->owns, id);
41 if (NULL != owned_object)
51 static struct yx_uint16 find_passable_pos()
54 for (pos.y = pos.x = 0; 0 == is_passable(pos);)
56 pos.y = rrand() % world.map.size.y;
57 pos.x = rrand() % world.map.size.x;
64 static void add_map_object(uint8_t type)
66 char * f_name = "add_map_object()";
67 struct MapObjDef * mod = get_map_object_def(type);
68 struct MapObj * mo = try_malloc(sizeof(struct MapObj), f_name);
69 mo->id = world.map_obj_count++;
71 mo->lifepoints = mod->lifepoints;
74 struct yx_uint16 pos = find_passable_pos(world.map);
75 struct MapObj * mo_ptr;
77 for (mo_ptr = world.map_objs; mo_ptr != NULL; mo_ptr = mo_ptr->next)
79 if (yx_uint16_cmp(&pos, &mo_ptr->pos) && 0 != mo_ptr->lifepoints)
96 struct MapObj ** mo_ptr_ptr = &world.map_objs;
97 for (; NULL != * mo_ptr_ptr; mo_ptr_ptr = &(*mo_ptr_ptr)->next);
103 extern void init_map_object_defs(char * filename)
105 char * f_name = "init_map_object_defs()";
106 FILE * file = try_fopen(filename, "r", f_name);
107 uint16_t linemax = textfile_sizes(file, NULL);
108 struct MapObjDef ** last_mod_ptr_ptr = &world.map_obj_defs;
110 char line[linemax + 1];
111 while (try_fgets(line, linemax + 1, file, f_name))
113 struct MapObjDef * mod = try_malloc(sizeof(struct MapObjDef), f_name);
115 mod->id = atoi(strtok(line, delim));
116 mod->corpse_id = atoi(strtok(NULL, delim));
117 mod->char_on_map = * strtok(NULL, delim);
118 mod->lifepoints = atoi(strtok(NULL, delim));
119 char * name = strtok(NULL, "\n");
120 mod->name = try_malloc(strlen(name) + 1, f_name);
121 memcpy(mod->name, name, strlen(name) + 1);
122 * last_mod_ptr_ptr = mod;
123 last_mod_ptr_ptr = &mod->next;
125 try_fclose(file, f_name);
126 set_cleanup_flag(CLEANUP_MAP_OBJECT_DEFS);
131 extern void free_map_object_defs(struct MapObjDef * mod_start)
133 if (NULL == mod_start)
137 free_map_object_defs(mod_start->next);
138 free(mod_start->name);
145 extern void add_map_objects(uint8_t type, uint8_t n)
148 for (i = 0; i < n; i++)
150 add_map_object(type);
156 extern void free_map_objects(struct MapObj * mo_start)
158 if (NULL == mo_start)
162 free_map_objects(mo_start->owns);
163 free_map_objects(mo_start->next);
165 if (mo_start == world.map_objs)
167 world.map_objs = NULL;
173 extern void own_map_object(struct MapObj ** target, struct MapObj ** source,
177 if (id == (*source)->id)
184 struct MapObj * penult = * source;
187 if (id == penult->next->id)
191 penult = penult->next;
194 penult->next = mo->next;
196 struct MapObj ** mo_ptr_ptr = target;
197 for (; NULL != * mo_ptr_ptr; mo_ptr_ptr = &(*mo_ptr_ptr)->next);
204 extern struct MapObj * get_player()
206 return get_map_object(world.map_objs, 0);
211 extern struct MapObjDef * get_map_object_def(uint8_t id)
213 struct MapObjDef * mod = world.map_obj_defs;
214 for (; id != mod->id; mod = mod->next);
220 extern void set_object_position(struct MapObj * mo, struct yx_uint16 pos)
223 struct MapObj * owned = mo->owns;
224 for (; owned != NULL; set_object_position(owned, pos), owned = owned->next);