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