home · contact · privacy
31b70af8ebc5f327cd4c4820d075f9dbf1bcd995
[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 */
11 #include <stdio.h> /* sprintf() */
12 #include <stdlib.h> /* free() */
13 #include <string.h> /* strlen() */
14 #include "../common/rexit.h" /* 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 "run.h" /* send_to_outfile() */
24 #include "world.h" /* global world */
25
26
27
28 /* Send "text" as log message to server out file. */
29 static void update_log(char * text);
30
31 /* One actor "wounds" another actor, decrementing his lifepoints and, if they
32  * reach zero in the process, killing it. Generates appropriate log message.
33  */
34 static void actor_hits_actor(struct Thing * hitter, struct Thing * hitted);
35
36 /* Bonus stuff to actor_*() to happen if actor==player. Mostly writing of log
37  * messages; _pick and _drop also decrement world.inventory_sel by 1 if >0.
38  * (match_dir() is just a little helper to playerbonus_move().)
39  */
40 static void playerbonus_wait();
41 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match);
42 static void playerbonus_move(char d, uint8_t passable);
43 static void playerbonus_drop(uint8_t owns_none);
44 static void playerbonus_pick(uint8_t picked);
45 static void playerbonus_use(uint8_t no_thing, uint8_t wrong_thing);
46
47
48
49 static void update_log(char * text)
50 {
51     send_to_outfile("LOG ", 0);
52     send_to_outfile(text, 0);
53     send_to_outfile("\n", 1);
54 }
55
56
57
58 static void actor_hits_actor(struct Thing * hitter, struct Thing * hitted)
59 {
60     struct ThingType * tt_hitter = get_thing_type(hitter->type);
61     struct ThingType * tt_hitted = get_thing_type(hitted->type);
62     struct Thing * player = get_player();
63     char * msg1 = "You";
64     char * msg2 = "wound";
65     char * msg3 = "you";
66     if      (player != hitter)
67     {
68         msg1 = tt_hitter->name;
69         msg2 = "wounds";
70     }
71     if (player != hitted)
72     {
73         msg3 = tt_hitted->name;
74     }
75     uint8_t len = strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2;
76     char * msg = try_malloc(len, __func__);
77     int test = sprintf(msg, "%s %s %s.", msg1, msg2, msg3);
78     exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]);
79     update_log(msg);
80     free(msg);
81     hitted->lifepoints--;
82     if (0 == hitted->lifepoints)
83     {
84         hitted->type = tt_hitted->corpse_id;
85         if (player == hitted)
86         {
87             update_log("You die.");
88             memset(hitted->fov_map, ' ', world.map.length * world.map.length);
89             return;
90         }
91         else
92         {
93             free(hitted->fov_map);
94             hitted->fov_map = NULL;
95             free(hitted->mem_map);
96             hitted->mem_map = NULL;
97             free(hitted->mem_depth_map);
98             hitted->mem_depth_map = NULL;
99             free_things_in_memory(hitted->t_mem);
100             hitted->t_mem = NULL;
101         }
102         update_log("It dies.");
103     }
104 }
105
106
107
108 static void playerbonus_wait()
109 {
110         update_log("You wait.");
111 }
112
113
114
115 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match)
116 {
117     if (d == match)
118     {
119         * dsc_d = dsc_match;
120         return 1;
121     }
122     return 0;
123 }
124
125
126
127 static void playerbonus_move(char d, uint8_t passable)
128 {
129     char * dsc_dir = "north-east";
130     if (   match_dir(d, &dsc_dir, 'd', "east")
131         || match_dir(d, &dsc_dir, 'c', "south-east")
132         || match_dir(d, &dsc_dir, 'x', "south-west")
133         || match_dir(d, &dsc_dir, 's', "west")
134         || match_dir(d, &dsc_dir, 'w', "north-west"))
135     {
136         ;
137     }
138     char * dsc_move = "You move ";
139     if (0 == passable)
140     {
141         dsc_move = "You fail to move ";
142     }
143     char * msg = try_malloc(strlen(dsc_move) + strlen (dsc_dir) + 2, __func__);
144     int test = sprintf(msg, "%s%s.", dsc_move, dsc_dir);
145     exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]);
146     update_log(msg);
147     free(msg);
148 }
149
150
151
152 static void playerbonus_drop(uint8_t owns_none)
153 {
154     if (0 != owns_none)
155     {
156         update_log("You try to drop an object, but you own none.");
157         return;
158     }
159     update_log("You drop an object.");
160 }
161
162
163
164 static void playerbonus_pick(uint8_t picked)
165 {
166     if (picked)
167     {
168         update_log("You pick up an object.");
169         return;
170     }
171     update_log("You try to pick up an object, but there is none.");
172 }
173
174
175
176 static void playerbonus_use(uint8_t no_thing, uint8_t wrong_thing)
177 {
178     if      (no_thing)
179     {
180         update_log("You try to use an object, but you own none.");
181         return;
182     }
183     else if (wrong_thing)
184     {
185         update_log("You try to use this object, but fail.");
186         return;
187     }
188     update_log("You consume this object.");
189 }
190
191
192
193 extern void actor_wait(struct Thing * t)
194 {
195     if (t == get_player())
196     {
197         playerbonus_wait();
198     }
199 }
200
201
202
203 extern void actor_move(struct Thing * t)
204 {
205     char d = t->arg;
206     struct Thing * other_t;
207     struct yx_uint8 target = t->pos;
208     uint8_t legal_move = mv_yx_in_dir_legal(d, &target);
209     mv_yx_in_dir_legal(0, NULL);
210     uint8_t passable = 0;
211     if (legal_move)
212     {
213         passable = '.' == world.map.cells[target.y*world.map.length + target.x];
214         for (other_t = world.things; other_t != 0; other_t = other_t->next)
215         {
216             if (0 == other_t->lifepoints || other_t == t)
217             {
218                 continue;
219             }
220             if (target.y == other_t->pos.y && target.x == other_t->pos.x)
221             {
222                actor_hits_actor(t, other_t);
223                return;
224             }
225         }
226     }
227     if (passable)
228     {
229         set_thing_position(t, target);
230         build_fov_map(t);
231     }
232     if (t == get_player())
233     {
234         playerbonus_move(d, passable);
235     }
236 }
237
238
239
240 extern void actor_drop(struct Thing * t)
241 {
242     uint8_t owns_none = (!t->owns);
243     if (!owns_none)
244     {
245         uint8_t select = t->arg;
246         struct Thing * owned = t->owns;
247         uint8_t i = 0;
248         for (; i != select; i++, owned = owned->next);
249         own_thing(&world.things, &t->owns, owned->id);
250     }
251     if (t == get_player())
252     {
253         playerbonus_drop(owns_none);
254     }
255 }
256
257
258
259 extern void actor_pick(struct Thing * t)
260 {
261     struct Thing * picked = NULL;
262     struct Thing * t_i;
263     for (t_i = world.things; t_i; t_i = t_i->next)
264     {
265         if (t_i != t && t_i->pos.y == t->pos.y && t_i->pos.x == t->pos.x)
266         {
267             picked = t_i;
268         }
269     }
270     if (picked)
271     {
272         own_thing(&t->owns, &world.things, picked->id);
273         set_thing_position(picked, t->pos);
274     }
275     if (t == get_player())
276     {
277         playerbonus_pick(!(!picked));
278     }
279 }
280
281
282
283 extern void actor_use(struct Thing * t)
284 {
285     uint8_t wrong_thing = 1;
286     uint8_t no_thing = (!t->owns);
287     if (!no_thing)
288     {
289         uint8_t select = t->arg;
290         uint8_t i = 0;
291         struct Thing * selected = t->owns;
292         for (; i != select; i++, selected = selected->next);
293         struct ThingType * tt = get_thing_type(selected->type);
294         if (tt->consumable)
295         {
296             wrong_thing = 0;
297             struct Thing * next = selected->next;
298             free(selected);
299             if (0 < select)
300             {
301                 select--;
302                 selected = t->owns;
303                 for (i = 0; i != select; i++, selected = selected->next);
304                 selected->next = next;
305             }
306             else
307             {
308                 t->owns = next;
309             }
310             t->lifepoints = t->lifepoints + tt->consumable;
311         }
312     }
313     if (t == get_player())
314     {
315         playerbonus_use(no_thing, wrong_thing);
316     }
317 }