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