home · contact · privacy
Included <stddef.h> were NULL was used.
[plomrogue] / src / server / map_objects.c
1 /* src/server/map_objects.c */
2
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(),
10                                   * textfile_sizes()
11                                   */
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() */
19
20
21
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);
24
25 /* Return random passable (as by is_passable()) position on world.map. */
26 static struct yx_uint16 find_passable_pos();
27
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);
30
31
32
33 static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id)
34 {
35     while (1)
36     {
37         if (NULL == ptr || id == ptr->id)
38         {
39             return ptr;
40         }
41         struct MapObj * owned_object = get_map_object(ptr->owns, id);
42         if (NULL != owned_object)
43         {
44             return ptr;
45         }
46         ptr = ptr->next;
47     }
48 }
49
50
51
52 static struct yx_uint16 find_passable_pos()
53 {
54     struct yx_uint16 pos;
55     for (pos.y = pos.x = 0; 0 == is_passable(pos);)
56     {
57         pos.y = rrand() % world.map.size.y;
58         pos.x = rrand() % world.map.size.x;
59     }
60     return pos;
61 }
62
63
64
65 static void add_map_object(uint8_t type)
66 {
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++;
71     mo->type       = mod->id;
72     mo->lifepoints = mod->lifepoints;
73     while (1)
74     {
75         struct yx_uint16 pos = find_passable_pos(world.map);
76         struct MapObj * mo_ptr;
77         uint8_t clear = 1;
78         for (mo_ptr = world.map_objs; mo_ptr != NULL; mo_ptr = mo_ptr->next)
79         {
80             if (yx_uint16_cmp(&pos, &mo_ptr->pos) && 0 != mo_ptr->lifepoints)
81             {
82                 clear = 0;
83                 break;
84             }
85         }
86         if (1 == clear)
87         {
88             mo->pos = pos;
89             break;
90         }
91     }
92     mo->progress = 0;
93     mo->command  = 0;
94     mo->arg      = 0;
95     mo->owns     = NULL;
96     mo->next     = NULL;
97     struct MapObj ** mo_ptr_ptr = &world.map_objs;
98     for (; NULL != * mo_ptr_ptr; mo_ptr_ptr = &(*mo_ptr_ptr)->next);
99     * mo_ptr_ptr = mo;
100 }
101
102
103
104 extern void init_map_object_defs(char * filename)
105 {
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;
110     char * delim = " ";
111     char line[linemax + 1];
112     while (try_fgets(line, linemax + 1, file, f_name))
113     {
114         struct MapObjDef * mod = try_malloc(sizeof(struct MapObjDef), f_name);
115         mod->next = NULL;
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;
125     }
126     try_fclose(file, f_name);
127     set_cleanup_flag(CLEANUP_MAP_OBJECT_DEFS);
128 }
129
130
131
132 extern void free_map_object_defs(struct MapObjDef * mod_start)
133 {
134     if (NULL == mod_start)
135     {
136         return;
137     }
138     free_map_object_defs(mod_start->next);
139     free(mod_start->name);
140     free(mod_start);
141     mod_start = NULL;
142 }
143
144
145
146 extern void add_map_objects(uint8_t type, uint8_t n)
147 {
148     uint8_t i;
149     for (i = 0; i < n; i++)
150     {
151         add_map_object(type);
152     }
153 }
154
155
156
157 extern void free_map_objects(struct MapObj * mo_start)
158 {
159     if (NULL == mo_start)
160     {
161         return;
162     }
163     free_map_objects(mo_start->owns);
164     free_map_objects(mo_start->next);
165     free(mo_start);
166     if (mo_start == world.map_objs)
167     {
168         world.map_objs = NULL;
169     }
170 }
171
172
173
174 extern void own_map_object(struct MapObj ** target, struct MapObj ** source,
175                            uint8_t id)
176 {
177     struct MapObj * mo;
178     if (id == (*source)->id)
179     {
180         mo = * source;
181         * source = mo->next;
182     }
183     else
184     {
185         struct MapObj * penult = * source;
186         while (1)
187         {
188             if (id == penult->next->id)
189             {
190                 break;
191             }
192             penult = penult->next;
193         }
194         mo = penult->next;
195         penult->next = mo->next;
196     }
197     struct MapObj ** mo_ptr_ptr = target;
198     for (; NULL != * mo_ptr_ptr; mo_ptr_ptr = &(*mo_ptr_ptr)->next);
199     * mo_ptr_ptr = mo;
200     mo->next = NULL;
201 }
202
203
204
205 extern struct MapObj * get_player()
206 {
207     return get_map_object(world.map_objs, 0);
208 }
209
210
211
212 extern struct MapObjDef * get_map_object_def(uint8_t id)
213 {
214     struct MapObjDef * mod = world.map_obj_defs;
215     for (; id != mod->id; mod = mod->next);
216     return mod;
217 }
218
219
220
221 extern void set_object_position(struct MapObj * mo, struct yx_uint16 pos)
222 {
223     mo->pos = pos;
224     struct MapObj * owned = mo->owns;
225     for (; owned != NULL; set_object_position(owned, pos), owned = owned->next);
226 }