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