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