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);
141 extern void add_map_objects(uint8_t type, uint8_t n)
144 for (i = 0; i < n; i++)
146 add_map_object(type);
152 extern void free_map_objects(struct MapObj * mo_start)
154 if (NULL == mo_start)
158 free_map_objects(mo_start->owns);
159 free_map_objects(mo_start->next);
161 if (mo_start == world.map_objs) /* So add_map_objects()' NULL-delimited */
162 { /* map object iteration loop does not */
163 world.map_objs = NULL; /* iterate over freed memory when called */
164 } /* the 1st time after world re-seeding. */
169 extern void own_map_object(struct MapObj ** target, struct MapObj ** source,
173 if (id == (*source)->id)
180 struct MapObj * penult = * source;
183 if (id == penult->next->id)
187 penult = penult->next;
190 penult->next = mo->next;
192 struct MapObj ** mo_ptr_ptr = target;
193 for (; NULL != * mo_ptr_ptr; mo_ptr_ptr = &(*mo_ptr_ptr)->next);
200 extern struct MapObj * get_player()
202 return get_map_object(world.map_objs, 0);
207 extern struct MapObjDef * get_map_object_def(uint8_t id)
209 struct MapObjDef * mod = world.map_obj_defs;
210 for (; id != mod->id; mod = mod->next);
216 extern void set_object_position(struct MapObj * mo, struct yx_uint16 pos)
219 struct MapObj * owned = mo->owns;
220 for (; owned != NULL; set_object_position(owned, pos), owned = owned->next);