home · contact · privacy
Load last world state from save file, not from re-stepping record file.
[plomrogue] / src / server / things.c
1 /* src/server/things.c */
2
3 #include "things.h"
4 #include <stddef.h> /* NULL */
5 #include <stdint.h> /* uint8_t, uint16_t, UINT8_MAX, UINT16_MAX */
6 #include <stdlib.h> /* free() */
7 #include <string.h> /* memset(), strlen() */
8 #include "../common/rexit.h" /* exit_err() */
9 #include "../common/try_malloc.h" /* try_malloc() */
10 #include "../common/yx_uint8.h" /* yx_uint8 */
11 #include "map.h" /* is_passable() */
12 #include "rrand.h" /* rrand() */
13 #include "world.h" /* global world */
14 #include "yx_uint8.h" /* yx_uint8_cmp() */
15
16
17
18
19 /* Return lowest unused id for new thing. */
20 static uint8_t get_lowest_unused_id();
21
22
23
24 static uint8_t get_lowest_unused_id()
25 {
26     uint8_t i = 0;
27     while (1)
28     {
29         if (!get_thing(world.things, i, 1))
30         {
31             return i;
32         }
33         exit_err(i == UINT8_MAX, "No unused ID available to add new thing.");
34         i++;
35     }
36 }
37
38
39
40 extern struct Thing * get_thing(struct Thing * ptr, uint8_t id, uint8_t deep)
41 {
42     while (1)
43     {
44         if (NULL == ptr || id == ptr->id)
45         {
46             return ptr;
47         }
48         if (deep)
49         {
50             struct Thing * owned_thing = get_thing(ptr->owns, id, 1);
51             if (NULL != owned_thing)
52             {
53                 return ptr;
54             }
55         }
56         ptr = ptr->next;
57     }
58 }
59
60
61
62 extern void free_thing_types(struct ThingType * tt_start)
63 {
64     if (NULL == tt_start)
65     {
66         return;
67     }
68     free_thing_types(tt_start->next);
69     free(tt_start->name);
70     free(tt_start);
71 }
72
73
74
75 extern struct Thing * add_thing(int16_t id, uint8_t type, uint8_t find_pos)
76 {
77     char * f_name = "add_thing()";
78     struct ThingType * tt = get_thing_type(type);
79     struct Thing *     t  = try_malloc(sizeof(struct Thing), f_name);
80     memset(t, 0, sizeof(struct Thing));
81     t->id = (0 <= id && id <= UINT8_MAX) ? id : get_lowest_unused_id();
82     t->type       = tt->id;
83     t->lifepoints = tt->lifepoints;
84     char * err = "Space to put thing on too hard to find. Map too small?";
85     uint16_t i = 0;
86     memset(&(t->pos), 0, sizeof(struct yx_uint8));
87     while (find_pos)
88     {
89         struct yx_uint8 pos;
90         for (pos.y = pos.x = 0; 0 == is_passable(pos); i++)
91         {
92             exit_err(UINT16_MAX == i, err);
93             pos.y = rrand() % world.map.length;
94             pos.x = rrand() % world.map.length;
95         }
96         struct Thing * t_ptr;
97         uint8_t clear = 1;
98         for (t_ptr = world.things; t_ptr != NULL; t_ptr = t_ptr->next)
99         {
100             if (yx_uint8_cmp(&pos, &t_ptr->pos) && 0 != t_ptr->lifepoints)
101             {
102                 clear = 0;
103                 break;
104             }
105         }
106         if (1 == clear)
107         {
108             t->pos = pos;
109             break;
110         }
111     }
112     struct Thing ** t_ptr_ptr = &world.things;
113     for (; NULL != * t_ptr_ptr; t_ptr_ptr = &(*t_ptr_ptr)->next);
114     * t_ptr_ptr = t;
115     return t;
116 }
117
118
119
120 extern void add_things(uint8_t type, uint8_t n)
121 {
122     uint8_t i;
123     for (i = 0; i < n; i++)
124     {
125         add_thing(-1, type, 1);
126     }
127 }
128
129
130
131 extern void free_things(struct Thing * t_start)
132 {
133     if (NULL == t_start)
134     {
135         return;
136     }
137     free_things(t_start->owns);
138     free_things(t_start->next);
139     free(t_start->fov_map);
140     free(t_start);
141     if (t_start == world.things)   /* So add_things()' NULL-delimited thing   */
142     {                              /* iteration loop does not iterate over    */
143         world.things = NULL;       /* freed memory when called the first time */
144     }                              /* after world re-seeding.                 */
145 }
146
147
148
149 extern void own_thing(struct Thing ** target, struct Thing ** source,
150                       uint8_t id)
151 {
152     struct Thing * t;
153     if (id == (*source)->id)
154     {
155         t = * source;
156         * source = t->next;
157     }
158     else
159     {
160         struct Thing * penult = * source;
161         while (1)
162         {
163             if (id == penult->next->id)
164             {
165                 break;
166             }
167             penult = penult->next;
168         }
169         t = penult->next;
170         penult->next = t->next;
171     }
172     struct Thing ** t_ptr_ptr = target;
173     for (; NULL != * t_ptr_ptr; t_ptr_ptr = &(*t_ptr_ptr)->next);
174     * t_ptr_ptr = t;
175     t->next = NULL;
176 }
177
178
179
180 extern struct Thing * get_player()
181 {
182     return get_thing(world.things, 0, 1);
183 }
184
185
186
187 extern struct ThingType * get_thing_type(uint8_t id)
188 {
189     struct ThingType * tt = world.thing_types;
190     for (; NULL != tt && id != tt->id; tt = tt->next);
191     char * err_intro = "Requested thing type of unused ID ";
192     char err[strlen(err_intro) + 3 + 1 + 1];
193     sprintf(err, "%s%d.", err_intro, id);
194     exit_err(NULL == tt, err);
195     return tt;
196 }
197
198
199
200 extern void set_thing_position(struct Thing * t, struct yx_uint8 pos)
201 {
202     t->pos = pos;
203     struct Thing * owned = t->owns;
204     for (; owned != NULL; set_thing_position(owned, pos), owned = owned->next);
205 }