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