home · contact · privacy
8d6a802417ea6df95068892fe3b89b77cad7057f
[plomrogue] / src / server / thing_actions.c
1 /* src/server/thing_actions.c */
2
3 #include "thing_actions.h"
4 #include <stddef.h> /* NULL */
5 #include <stdint.h> /* uint8_t, uint16_t */
6 #include <stdio.h> /* sprintf() */
7 #include <stdlib.h> /* free() */
8 #include <string.h> /* strlen(), memcpy(), strncmp() */
9 #include "../common/rexit.h" /* exit_trouble() */
10 #include "../common/try_malloc.h" /* try_malloc() */
11 #include "../common/yx_uint8.h" /* struct yx_uint8 */
12 #include "field_of_view.h" /* build_fov_map() */
13 #include "hardcoded_strings.h" /* s */
14 #include "things.h" /* Thing, ThingType, get_player(), own_thing(),
15                      * set_thing_position(), get_thing_type()
16                      */
17 #include "map.h" /* is_passable() */
18 #include "yx_uint8.h" /* mv_yx_in_dir(), yx_uint8_cmp() */
19 #include "world.h" /* global world */
20
21
22
23 /* How many previous characters of the game log to keep on adding new text */
24 #define MAX_BACKLOG_CHARS 3000
25
26
27
28 /* If "text" is equal "log"'s last line, return 1, else 0. */
29 static uint8_t text_equals_log_end(char * log, char * text);
30
31 /* Append "text" to game log shortened to MAX_BACKLOG_CHARS characters, or
32  * continuation period if "text" is the same as the (shortened) log's last line
33  * minus continuation periods.
34  */
35 static void update_log(char * text);
36
37 /* One actor "wounds" another actor, decrementing his lifepoints and, if they
38  * reach zero in the process, killing it. Generates appropriate log message.
39  */
40 static void actor_hits_actor(struct Thing * hitter, struct Thing * hitted);
41
42 /* Bonus stuff to actor_*() to happen if actor==player. Mostly writing of log
43  * messages; _pick and _drop also decrement world.inventory_sel by 1 if >0.
44  * (match_dir() is just a little helper to playerbonus_move().)
45  */
46 static void playerbonus_wait();
47 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match);
48 static void playerbonus_move(char d, uint8_t passable);
49 static void playerbonus_drop(uint8_t owns_none);
50 static void playerbonus_pick(uint8_t picked);
51 static void playerbonus_use(uint8_t no_thing, uint8_t wrong_thing);
52
53
54
55 static uint8_t text_equals_log_end(char * log, char * text)
56 {
57     uint16_t len_old = strlen(log);
58     uint16_t last_nl = len_old - 1;
59     while (last_nl != 0)
60     {
61         if ('\n' == log[last_nl])
62         {
63             break;
64         }
65         last_nl--;
66     }
67     uint16_t last_stop = len_old - 1;
68     while (last_stop != 0)
69     {
70         if ('.' == log[last_stop] && '.' != log[last_stop - 1])
71         {
72             break;
73         }
74         last_stop--;
75     }
76     if (   (last_stop + 1) - last_nl == (uint16_t) strlen(text)
77         && 0 == strncmp(log + last_nl, text, strlen(text)))
78     {
79         return 1;
80     }
81     return 0;
82 }
83
84
85
86 static void update_log(char * text)
87 {
88     uint16_t len_new = strlen(text);
89     uint16_t len_old = 0;
90     uint16_t offset = 0;
91     if (world.log)
92     {
93         len_old = strlen(world.log);
94         if (len_old > MAX_BACKLOG_CHARS)
95         {
96             offset = len_old - MAX_BACKLOG_CHARS;
97             len_old = MAX_BACKLOG_CHARS;
98         }
99         if (text_equals_log_end(world.log + offset, text))
100         {
101             text = ".";
102         }
103     }
104     uint16_t len_whole = len_old + len_new + 1;
105     char * new_text = try_malloc(len_whole, __func__);
106     memcpy(new_text, world.log + offset, len_old);
107     int test = sprintf(new_text + len_old, "%s", text);
108     exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]);
109     free(world.log);
110     world.log = new_text;
111 }
112
113
114
115 static void actor_hits_actor(struct Thing * hitter, struct Thing * hitted)
116 {
117     struct ThingType * tt_hitter = get_thing_type(hitter->type);
118     struct ThingType * tt_hitted = get_thing_type(hitted->type);
119     struct Thing * player = get_player();
120     char * msg1 = "You";
121     char * msg2 = "wound";
122     char * msg3 = "you";
123     if      (player != hitter)
124     {
125         msg1 = tt_hitter->name;
126         msg2 = "wounds";
127     }
128     if (player != hitted)
129     {
130         msg3 = tt_hitted->name;
131     }
132     uint8_t len = 1 + strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2;
133     char * msg = try_malloc(len, __func__);
134     int test = sprintf(msg, "\n%s %s %s.", msg1, msg2, msg3);
135     exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]);
136     update_log(msg);
137     free(msg);
138     hitted->lifepoints--;
139     if (0 == hitted->lifepoints)
140     {
141         hitted->type = tt_hitted->corpse_id;
142         if (player == hitted)
143         {
144             update_log(" You die.");
145             return;
146         }
147         update_log(" It dies.");
148     }
149 }
150
151
152
153 static void playerbonus_wait()
154 {
155         update_log("\nYou wait.");
156 }
157
158
159
160 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match)
161 {
162     if (d == match)
163     {
164         * dsc_d = dsc_match;
165         return 1;
166     }
167     return 0;
168 }
169
170
171
172 static void playerbonus_move(char d, uint8_t passable)
173 {
174     char * dsc_dir = "north-east";
175     if (   match_dir(d, &dsc_dir, 'd', "east")
176         || match_dir(d, &dsc_dir, 'c', "south-east")
177         || match_dir(d, &dsc_dir, 'x', "south-west")
178         || match_dir(d, &dsc_dir, 's', "west")
179         || match_dir(d, &dsc_dir, 'w', "north-west"))
180     {
181         ;
182     }
183     char * dsc_move = "You move ";
184     if (0 == passable)
185     {
186         dsc_move = "You fail to move ";
187     }
188     char * msg = try_malloc(strlen(dsc_move) + strlen (dsc_dir) + 3, __func__);
189     int test = sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
190     exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]);
191     update_log(msg);
192     free(msg);
193 }
194
195
196
197 static void playerbonus_drop(uint8_t owns_none)
198 {
199     if (0 != owns_none)
200     {
201         update_log("\nYou try to drop an object, but you own none.");
202         return;
203     }
204     update_log("\nYou drop an object.");
205 }
206
207
208
209 static void playerbonus_pick(uint8_t picked)
210 {
211     if (picked)
212     {
213         update_log("\nYou pick up an object.");
214         return;
215     }
216     update_log("\nYou try to pick up an object, but there is none.");
217 }
218
219
220
221 static void playerbonus_use(uint8_t no_thing, uint8_t wrong_thing)
222 {
223     if      (no_thing)
224     {
225         update_log("\nYou try to use an object, but you own none.");
226         return;
227     }
228     else if (wrong_thing)
229     {
230         update_log("\nYou try to use this object, but fail.");
231         return;
232     }
233     update_log("\nYou consume MAGIC MEAT.");
234 }
235
236
237
238 extern void actor_wait(struct Thing * t)
239 {
240     if (t == get_player())
241     {
242         playerbonus_wait();
243     }
244 }
245
246
247
248 extern void actor_move(struct Thing * t)
249 {
250     char d = t->arg;
251     struct yx_uint8 target = mv_yx_in_dir(d, t->pos);
252     struct Thing * other_t;
253     for (other_t = world.things; other_t != 0; other_t = other_t->next)
254     {
255         if (0 == other_t->lifepoints || other_t == t)
256         {
257             continue;
258         }
259         if (yx_uint8_cmp(&target, &other_t->pos))
260         {
261             actor_hits_actor(t, other_t);
262             return;
263         }
264     }
265     uint8_t passable = is_passable(target);
266     if (passable)
267     {
268         set_thing_position(t, target);
269         build_fov_map(t);
270     }
271     if (t == get_player())
272     {
273         playerbonus_move(d, passable);
274     }
275 }
276
277
278
279 extern void actor_drop(struct Thing * t)
280 {
281     uint8_t owns_none = (NULL == t->owns);
282     if (!owns_none)
283     {
284         uint8_t select = t->arg;
285         struct Thing * owned = t->owns;
286         uint8_t i = 0;
287         for (; i != select; i++, owned = owned->next);
288         own_thing(&world.things, &t->owns, owned->id);
289     }
290     if (t == get_player())
291     {
292         playerbonus_drop(owns_none);
293     }
294 }
295
296
297
298 extern void actor_pick(struct Thing * t)
299 {
300     struct Thing * picked = NULL;
301     struct Thing * t_i;
302     for (t_i = world.things; NULL != t_i; t_i = t_i->next)
303     {
304         if (t_i != t && yx_uint8_cmp(&t_i->pos, &t->pos))
305         {
306             picked = t_i;
307         }
308     }
309     if (NULL != picked)
310     {
311         own_thing(&t->owns, &world.things, picked->id);
312         set_thing_position(picked, t->pos);
313     }
314     if (t == get_player())
315     {
316         playerbonus_pick(NULL != picked);
317     }
318 }
319
320
321
322 extern void actor_use(struct Thing * t)
323 {
324     uint8_t wrong_thing = 1;
325     uint8_t no_thing = (NULL == t->owns);
326     if (!no_thing)
327     {
328         uint8_t select = t->arg;
329         uint8_t i = 0;
330         struct Thing * selected = t->owns;
331         for (; i != select; i++, selected = selected->next);
332         struct ThingType * tt = get_thing_type(selected->type);
333         if (tt->consumable)
334         {
335             wrong_thing = 0;
336             struct Thing * next = selected->next;
337             free(selected);
338             if (0 < select)
339             {
340                 select--;
341                 selected = t->owns;
342                 for (i = 0; i != select; i++, selected = selected->next);
343                 selected->next = next;
344             }
345             else
346             {
347                 t->owns = next;
348             }
349             t->lifepoints = t->lifepoints + tt->consumable;
350         }
351     }
352     if (t == get_player())
353     {
354         playerbonus_use(no_thing, wrong_thing);
355     }
356 }