home · contact · privacy
Server: Add regeneration of lifepoints on positive satiation values.
[plomrogue] / src / server / thing_actions.c
1 /* src/server/thing_actions.c
2  *
3  * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
4  * or any later version. For details on its copyright, license, and warranties,
5  * see the file NOTICE in the root directory of the PlomRogue source package.
6  */
7
8 #include "thing_actions.h"
9 #include <stddef.h> /* NULL */
10 #include <stdint.h> /* uint8_t, INT16_MIN, INT16_MAX */
11 #include <stdio.h> /* sprintf() */
12 #include <stdlib.h> /* free() */
13 #include <string.h> /* strlen() */
14 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
15 #include "../common/try_malloc.h" /* try_malloc() */
16 #include "../common/yx_uint8.h" /* yx_uint8 */
17 #include "field_of_view.h" /* build_fov_map() */
18 #include "hardcoded_strings.h" /* s */
19 #include "things.h" /* Thing, ThingType, get_player(), free_things_in_memory(),
20                      * own_thing(), set_thing_position(), get_thing_type(),
21                      */
22 #include "map.h" /* mv_yx_in_dir_legal() */
23 #include "rrand.h" /* rrand() */
24 #include "run.h" /* send_to_outfile() */
25 #include "world.h" /* global world */
26
27
28
29 /* Send "text" as log message to server out file. */
30 static void update_log(char * text);
31
32 /* Decrement "t"'s lifepoints, and if to zero, kill it with log update. */
33 static void decrement_lifepoints(struct Thing * t);
34
35 /* One actor "wounds" another actor, decrementing his lifepoints. */
36 static void actor_hits_actor(struct Thing * hitter, struct Thing * hitted);
37
38 /* Bonus stuff to actor_*() to happen if actor==player. Mostly writing of log
39  * messages; _pick and _drop also decrement world.inventory_sel by 1 if >0.
40  * (match_dir() is just a little helper to playerbonus_move().)
41  */
42 static void playerbonus_wait();
43 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match);
44 static void playerbonus_move(char d, uint8_t passable);
45 static void playerbonus_drop(uint8_t owns_none);
46 static void playerbonus_pick(uint8_t picked);
47 static void playerbonus_use(uint8_t no_thing, uint8_t wrong_thing);
48
49
50
51 static void update_log(char * text)
52 {
53     send_to_outfile("LOG ", 0);
54     send_to_outfile(text, 0);
55     send_to_outfile("\n", 1);
56 }
57
58
59
60 static void decrement_lifepoints(struct Thing * t)
61 {
62     struct Thing * player = get_player();
63     t->lifepoints--;
64     if (0 == t->lifepoints)
65     {
66         t->type = get_thing_type(t->type)->corpse_id;
67         if (player == t)
68         {
69             update_log("You die.");
70             memset(t->fov_map, ' ', world.map.length * world.map.length);
71             return;
72         }
73         else
74         {
75             free(t->fov_map);
76             t->fov_map = NULL;
77             free(t->mem_map);
78             t->mem_map = NULL;
79             free(t->mem_depth_map);
80             t->mem_depth_map = NULL;
81             free_things_in_memory(t->t_mem);
82             t->t_mem = NULL;
83         }
84         update_log("It dies.");
85     }
86 }
87
88
89
90 static void actor_hits_actor(struct Thing * hitter, struct Thing * hitted)
91 {
92     struct ThingType * tt_hitter = get_thing_type(hitter->type);
93     struct ThingType * tt_hitted = get_thing_type(hitted->type);
94     struct Thing * player = get_player();
95     char * msg1 = "You";
96     char * msg2 = "wound";
97     char * msg3 = "you";
98     if      (player != hitter)
99     {
100         msg1 = tt_hitter->name;
101         msg2 = "wounds";
102     }
103     if (player != hitted)
104     {
105         msg3 = tt_hitted->name;
106     }
107     uint8_t len = strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2;
108     char * msg = try_malloc(len, __func__);
109     int test = sprintf(msg, "%s %s %s.", msg1, msg2, msg3);
110     exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]);
111     update_log(msg);
112     free(msg);
113     decrement_lifepoints(hitted);
114 }
115
116
117
118 static void playerbonus_wait()
119 {
120         update_log("You wait.");
121 }
122
123
124
125 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match)
126 {
127     if (d == match)
128     {
129         * dsc_d = dsc_match;
130         return 1;
131     }
132     return 0;
133 }
134
135
136
137 static void playerbonus_move(char d, uint8_t passable)
138 {
139     char * dsc_dir = "north-east";
140     if (   match_dir(d, &dsc_dir, 'd', "east")
141         || match_dir(d, &dsc_dir, 'c', "south-east")
142         || match_dir(d, &dsc_dir, 'x', "south-west")
143         || match_dir(d, &dsc_dir, 's', "west")
144         || match_dir(d, &dsc_dir, 'w', "north-west"))
145     {
146         ;
147     }
148     char * dsc_move = "You move ";
149     if (0 == passable)
150     {
151         dsc_move = "You fail to move ";
152     }
153     char * msg = try_malloc(strlen(dsc_move) + strlen (dsc_dir) + 2, __func__);
154     int test = sprintf(msg, "%s%s.", dsc_move, dsc_dir);
155     exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]);
156     update_log(msg);
157     free(msg);
158 }
159
160
161
162 static void playerbonus_drop(uint8_t owns_none)
163 {
164     if (0 != owns_none)
165     {
166         update_log("You try to drop an object, but you own none.");
167         return;
168     }
169     update_log("You drop an object.");
170 }
171
172
173
174 static void playerbonus_pick(uint8_t picked)
175 {
176     if (picked)
177     {
178         update_log("You pick up an object.");
179         return;
180     }
181     update_log("You try to pick up an object, but there is none.");
182 }
183
184
185
186 static void playerbonus_use(uint8_t no_thing, uint8_t wrong_thing)
187 {
188     if      (no_thing)
189     {
190         update_log("You try to use an object, but you own none.");
191         return;
192     }
193     else if (wrong_thing)
194     {
195         update_log("You try to use this object, but fail.");
196         return;
197     }
198     update_log("You consume this object.");
199 }
200
201
202
203 extern void actor_wait(struct Thing * t)
204 {
205     if (t == get_player())
206     {
207         playerbonus_wait();
208     }
209 }
210
211
212
213 extern void actor_move(struct Thing * t)
214 {
215     char d = t->arg;
216     struct Thing * other_t;
217     struct yx_uint8 target = t->pos;
218     uint8_t legal_move = mv_yx_in_dir_legal(d, &target);
219     mv_yx_in_dir_legal(0, NULL);
220     uint8_t passable = 0;
221     if (legal_move)
222     {
223         passable = '.' == world.map.cells[target.y*world.map.length + target.x];
224         for (other_t = world.things; other_t != 0; other_t = other_t->next)
225         {
226             if (0 == other_t->lifepoints || other_t == t)
227             {
228                 continue;
229             }
230             if (target.y == other_t->pos.y && target.x == other_t->pos.x)
231             {
232                actor_hits_actor(t, other_t);
233                return;
234             }
235         }
236     }
237     if (passable)
238     {
239         set_thing_position(t, target);
240         build_fov_map(t);
241     }
242     if (t == get_player())
243     {
244         playerbonus_move(d, passable);
245     }
246 }
247
248
249
250 extern void actor_drop(struct Thing * t)
251 {
252     uint8_t owns_none = (!t->owns);
253     if (!owns_none)
254     {
255         uint8_t select = t->arg;
256         struct Thing * owned = t->owns;
257         uint8_t i = 0;
258         for (; i != select; i++, owned = owned->next);
259         own_thing(&world.things, &t->owns, owned->id);
260     }
261     if (t == get_player())
262     {
263         playerbonus_drop(owns_none);
264     }
265 }
266
267
268
269 extern void actor_pick(struct Thing * t)
270 {
271     struct Thing * picked = NULL;
272     struct Thing * t_i;
273     for (t_i = world.things; t_i; t_i = t_i->next)
274     {
275         if (t_i != t && t_i->pos.y == t->pos.y && t_i->pos.x == t->pos.x)
276         {
277             picked = t_i;
278         }
279     }
280     if (picked)
281     {
282         own_thing(&t->owns, &world.things, picked->id);
283         set_thing_position(picked, t->pos);
284     }
285     if (t == get_player())
286     {
287         playerbonus_pick(!(!picked));
288     }
289 }
290
291
292
293 extern void actor_use(struct Thing * t)
294 {
295     uint8_t wrong_thing = 1;
296     uint8_t no_thing = (!t->owns);
297     if (!no_thing)
298     {
299         uint8_t select = t->arg;
300         uint8_t i = 0;
301         struct Thing * selected = t->owns;
302         for (; i != select; i++, selected = selected->next);
303         struct ThingType * tt = get_thing_type(selected->type);
304         if (tt->consumable)
305         {
306             wrong_thing = 0;
307             struct Thing * next = selected->next;
308             free(selected);
309             if (0 < select)
310             {
311                 select--;
312                 selected = t->owns;
313                 for (i = 0; i != select; i++, selected = selected->next);
314                 selected->next = next;
315             }
316             else
317             {
318                 t->owns = next;
319             }
320             t->satiation = t->satiation + tt->consumable > INT16_MAX ?
321                            INT16_MAX : t->satiation + tt->consumable;
322             t->lifepoints = t->lifepoints + tt->consumable;
323         }
324     }
325     if (t == get_player())
326     {
327         playerbonus_use(no_thing, wrong_thing);
328     }
329 }
330
331
332
333 extern void try_healing(struct Thing * t)
334 {
335     struct ThingType * tt = get_thing_type(t->type);
336     if (   t->satiation > 0 && t->lifepoints < tt->lifepoints
337         && 0 == (rrand() % 31)
338         && get_thing_action_id_by_name(s[S_CMD_WAIT]) == t->command)
339     {
340         t->lifepoints++;
341         t->satiation = t->satiation - 32;
342         if (get_player() == t)
343         {
344             update_log("You heal.");
345         }
346         else
347         {
348             char * msg_part = " heals.";
349             uint8_t len = strlen(tt->name) + strlen(msg_part) + 1;
350             char * msg = try_malloc(len, __func__);
351             int test = sprintf(msg, "%s%s", tt->name, msg_part);
352             exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]);
353             update_log(msg);
354             free(msg);
355         }
356     }
357 }
358
359
360
361 extern void hunger(struct Thing * t)
362 {
363     if (t->satiation > INT16_MIN)
364     {
365         t->satiation--;
366     }
367     struct ThingType * tt = get_thing_type(t->type);
368     uint16_t testbase = t->satiation < 0 ? -(t->satiation) : t->satiation;
369     exit_err(!(tt->lifepoints), "A thing that should not hunger is hungering.");
370     uint16_t endurance = INT16_MAX / tt->lifepoints;
371     if ((testbase / endurance) / ((rrand() % endurance) + 1))
372     {
373         if (get_player() == t)
374         {
375             update_log("You suffer from hunger.");
376         }
377         else
378         {
379             char * msg_part = " suffers from hunger.";
380             uint8_t len = strlen(tt->name) + strlen(msg_part) + 1;
381             char * msg = try_malloc(len, __func__);
382             int test = sprintf(msg, "%s%s", tt->name, msg_part);
383             exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]);
384             update_log(msg);
385             free(msg);
386         }
387         decrement_lifepoints(t);
388     }
389 }