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(), memset() */
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 memset(mo, 0, sizeof(struct MapObj));
71 mo->id = world.map_obj_count++;
73 mo->lifepoints = mod->lifepoints;
76 struct yx_uint16 pos = find_passable_pos(world.map);
77 struct MapObj * mo_ptr;
79 for (mo_ptr = world.map_objs; mo_ptr != NULL; mo_ptr = mo_ptr->next)
81 if (yx_uint16_cmp(&pos, &mo_ptr->pos) && 0 != mo_ptr->lifepoints)
93 struct MapObj ** mo_ptr_ptr = &world.map_objs;
94 for (; NULL != * mo_ptr_ptr; mo_ptr_ptr = &(*mo_ptr_ptr)->next);
100 extern void init_map_object_defs(char * filename)
102 char * f_name = "init_map_object_defs()";
103 FILE * file = try_fopen(filename, "r", f_name);
104 uint16_t linemax = textfile_sizes(file, NULL);
105 struct MapObjDef ** last_mod_ptr_ptr = &world.map_obj_defs;
107 char line[linemax + 1];
108 while (try_fgets(line, linemax + 1, file, f_name))
110 struct MapObjDef * mod = try_malloc(sizeof(struct MapObjDef), f_name);
112 mod->id = atoi(strtok(line, delim));
113 mod->corpse_id = atoi(strtok(NULL, delim));
114 mod->char_on_map = * strtok(NULL, delim);
115 mod->lifepoints = atoi(strtok(NULL, delim));
116 char * name = strtok(NULL, "\n");
117 mod->name = try_malloc(strlen(name) + 1, f_name);
118 memcpy(mod->name, name, strlen(name) + 1);
119 * last_mod_ptr_ptr = mod;
120 last_mod_ptr_ptr = &mod->next;
122 try_fclose(file, f_name);
123 set_cleanup_flag(CLEANUP_MAP_OBJECT_DEFS);
128 extern void free_map_object_defs(struct MapObjDef * mod_start)
130 if (NULL == mod_start)
134 free_map_object_defs(mod_start->next);
135 free(mod_start->name);
142 extern void add_map_objects(uint8_t type, uint8_t n)
145 for (i = 0; i < n; i++)
147 add_map_object(type);
153 extern void free_map_objects(struct MapObj * mo_start)
155 if (NULL == mo_start)
159 free_map_objects(mo_start->owns);
160 free_map_objects(mo_start->next);
162 if (mo_start == world.map_objs)
164 world.map_objs = NULL;
170 extern void own_map_object(struct MapObj ** target, struct MapObj ** source,
174 if (id == (*source)->id)
181 struct MapObj * penult = * source;
184 if (id == penult->next->id)
188 penult = penult->next;
191 penult->next = mo->next;
193 struct MapObj ** mo_ptr_ptr = target;
194 for (; NULL != * mo_ptr_ptr; mo_ptr_ptr = &(*mo_ptr_ptr)->next);
201 extern struct MapObj * get_player()
203 return get_map_object(world.map_objs, 0);
208 extern struct MapObjDef * get_map_object_def(uint8_t id)
210 struct MapObjDef * mod = world.map_obj_defs;
211 for (; id != mod->id; mod = mod->next);
217 extern void set_object_position(struct MapObj * mo, struct yx_uint16 pos)
220 struct MapObj * owned = mo->owns;
221 for (; owned != NULL; set_object_position(owned, pos), owned = owned->next);