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