home · contact · privacy
e91f576aa3ebfd5ed84fbb296c2ba100f0f4d574
[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     char * f_name = "update_log()";
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, f_name);
107     memcpy(new_text, world.log + offset, len_old);
108     int test = sprintf(new_text + len_old, "%s", text);
109     exit_trouble(test < 0, f_name, 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     char * f_name = "actor_hits_actor()";
119     struct ThingType * tt_hitter = get_thing_type(hitter->type);
120     struct ThingType * tt_hitted = get_thing_type(hitted->type);
121     struct Thing * player = get_player();
122     char * msg1 = "You";
123     char * msg2 = "wound";
124     char * msg3 = "you";
125     if      (player != hitter)
126     {
127         msg1 = tt_hitter->name;
128         msg2 = "wounds";
129     }
130     if (player != hitted)
131     {
132         msg3 = tt_hitted->name;
133     }
134     uint8_t len = 1 + strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2;
135     char * msg = try_malloc(len, f_name);
136     int test = sprintf(msg, "\n%s %s %s.", msg1, msg2, msg3);
137     exit_trouble(test < 0, f_name, s[S_FCN_SPRINTF]);
138     update_log(msg);
139     free(msg);
140     hitted->lifepoints--;
141     if (0 == hitted->lifepoints)
142     {
143         hitted->type = tt_hitted->corpse_id;
144         if (player == hitted)
145         {
146             update_log(" You die.");
147             return;
148         }
149         update_log(" It dies.");
150     }
151 }
152
153
154
155 static void playerbonus_wait()
156 {
157         update_log("\nYou wait.");
158 }
159
160
161
162 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match)
163 {
164     if (d == match)
165     {
166         * dsc_d = dsc_match;
167         return 1;
168     }
169     return 0;
170 }
171
172
173
174 static void playerbonus_move(char d, uint8_t passable)
175 {
176     char * f_name = "playerbonus_move()";
177     char * dsc_dir = "north-east";
178     if (   match_dir(d, &dsc_dir, 'd', "east")
179         || match_dir(d, &dsc_dir, 'c', "south-east")
180         || match_dir(d, &dsc_dir, 'x', "south-west")
181         || match_dir(d, &dsc_dir, 's', "west")
182         || match_dir(d, &dsc_dir, 'w', "north-west"))
183     {
184         ;
185     }
186     char * dsc_move = "You move ";
187     if (0 == passable)
188     {
189         dsc_move = "You fail to move ";
190     }
191     char * msg = try_malloc(strlen(dsc_move) + strlen (dsc_dir) + 3, f_name);
192     int test = sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
193     exit_trouble(test < 0, f_name, s[S_FCN_SPRINTF]);
194     update_log(msg);
195     free(msg);
196 }
197
198
199
200 static void playerbonus_drop(uint8_t owns_none)
201 {
202     if (0 != owns_none)
203     {
204         update_log("\nYou try to drop an object, but you own none.");
205         return;
206     }
207     update_log("\nYou drop an object.");
208 }
209
210
211
212 static void playerbonus_pick(uint8_t picked)
213 {
214     if (picked)
215     {
216         update_log("\nYou pick up an object.");
217         return;
218     }
219     update_log("\nYou try to pick up an object, but there is none.");
220 }
221
222
223
224 static void playerbonus_use(uint8_t no_thing, uint8_t wrong_thing)
225 {
226     if      (no_thing)
227     {
228         update_log("\nYou try to use an object, but you own none.");
229         return;
230     }
231     else if (wrong_thing)
232     {
233         update_log("\nYou try to use this object, but fail.");
234         return;
235     }
236     update_log("\nYou consume MAGIC MEAT.");
237 }
238
239
240
241 extern void actor_wait(struct Thing * t)
242 {
243     if (t == get_player())
244     {
245         playerbonus_wait();
246     }
247 }
248
249
250
251 extern void actor_move(struct Thing * t)
252 {
253     char d = t->arg;
254     struct yx_uint8 target = mv_yx_in_dir(d, t->pos);
255     struct Thing * other_t;
256     for (other_t = world.things; other_t != 0; other_t = other_t->next)
257     {
258         if (0 == other_t->lifepoints || other_t == t)
259         {
260             continue;
261         }
262         if (yx_uint8_cmp(&target, &other_t->pos))
263         {
264             actor_hits_actor(t, other_t);
265             return;
266         }
267     }
268     uint8_t passable = is_passable(target);
269     if (passable)
270     {
271         set_thing_position(t, target);
272         free(t->fov_map);
273         t->fov_map = build_fov_map(t);
274     }
275     if (t == get_player())
276     {
277         playerbonus_move(d, passable);
278     }
279 }
280
281
282
283 extern void actor_drop(struct Thing * t)
284 {
285     uint8_t owns_none = (NULL == t->owns);
286     if (!owns_none)
287     {
288         uint8_t select = t->arg;
289         struct Thing * owned = t->owns;
290         uint8_t i = 0;
291         for (; i != select; i++, owned = owned->next);
292         own_thing(&world.things, &t->owns, owned->id);
293     }
294     if (t == get_player())
295     {
296         playerbonus_drop(owns_none);
297     }
298 }
299
300
301
302 extern void actor_pick(struct Thing * t)
303 {
304     struct Thing * picked = NULL;
305     struct Thing * t_i;
306     for (t_i = world.things; NULL != t_i; t_i = t_i->next)
307     {
308         if (t_i != t && yx_uint8_cmp(&t_i->pos, &t->pos))
309         {
310             picked = t_i;
311         }
312     }
313     if (NULL != picked)
314     {
315         own_thing(&t->owns, &world.things, picked->id);
316         set_thing_position(picked, t->pos);
317     }
318     if (t == get_player())
319     {
320         playerbonus_pick(NULL != picked);
321     }
322 }
323
324
325
326 extern void actor_use(struct Thing * t)
327 {
328     uint8_t wrong_thing = 1;
329     uint8_t no_thing = (NULL == t->owns);
330     if (!no_thing)
331     {
332         uint8_t select = t->arg;
333         uint8_t i = 0;
334         struct Thing * selected = t->owns;
335         for (; i != select; i++, selected = selected->next);
336         struct ThingType * tt = get_thing_type(selected->type);
337         if (tt->consumable)
338         {
339             wrong_thing = 0;
340             struct Thing * next = selected->next;
341             free(selected);
342             if (0 < select)
343             {
344                 select--;
345                 selected = t->owns;
346                 for (i = 0; i != select; i++, selected = selected->next);
347                 selected->next = next;
348             }
349             else
350             {
351                 t->owns = next;
352             }
353             t->lifepoints = t->lifepoints + tt->consumable;
354         }
355     }
356     if (t == get_player())
357     {
358         playerbonus_use(no_thing, wrong_thing);
359     }
360 }