1 /* src/server/thing_actions.c */
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()
17 #include "map.h" /* is_passable() */
18 #include "yx_uint8.h" /* mv_yx_in_dir(), yx_uint8_cmp() */
19 #include "world.h" /* global world */
23 /* How many previous characters of the game log to keep on adding new text */
24 #define MAX_BACKLOG_CHARS 3000
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);
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.
35 static void update_log(char * text);
37 /* One actor "wounds" another actor, decrementing his lifepoints and, if they
38 * reach zero in the process, killing it. Generates appropriate log message.
40 static void actor_hits_actor(struct Thing * hitter, struct Thing * hitted);
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().)
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);
55 static uint8_t text_equals_log_end(char * log, char * text)
57 uint16_t len_old = strlen(log);
58 uint16_t last_nl = len_old - 1;
61 if ('\n' == log[last_nl])
67 uint16_t last_stop = len_old - 1;
68 while (last_stop != 0)
70 if ('.' == log[last_stop] && '.' != log[last_stop - 1])
76 if ( (last_stop + 1) - last_nl == (uint16_t) strlen(text)
77 && 0 == strncmp(log + last_nl, text, strlen(text)))
86 static void update_log(char * text)
88 uint16_t len_new = strlen(text);
93 len_old = strlen(world.log);
94 if (len_old > MAX_BACKLOG_CHARS)
96 offset = len_old - MAX_BACKLOG_CHARS;
97 len_old = MAX_BACKLOG_CHARS;
99 if (text_equals_log_end(world.log + offset, text))
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]);
110 world.log = new_text;
115 static void actor_hits_actor(struct Thing * hitter, struct Thing * hitted)
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();
121 char * msg2 = "wound";
123 if (player != hitter)
125 msg1 = tt_hitter->name;
128 if (player != hitted)
130 msg3 = tt_hitted->name;
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]);
138 hitted->lifepoints--;
139 if (0 == hitted->lifepoints)
141 hitted->type = tt_hitted->corpse_id;
142 if (player == hitted)
144 update_log(" You die.");
147 update_log(" It dies.");
153 static void playerbonus_wait()
155 update_log("\nYou wait.");
160 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match)
172 static void playerbonus_move(char d, uint8_t passable)
174 char * dsc_dir = "north-east";
175 if ( match_dir(d, &dsc_dir, 'd', "east")
176 || match_dir(d, &dsc_dir, 'c', "south-east")
177 || match_dir(d, &dsc_dir, 'x', "south-west")
178 || match_dir(d, &dsc_dir, 's', "west")
179 || match_dir(d, &dsc_dir, 'w', "north-west"))
183 char * dsc_move = "You move ";
186 dsc_move = "You fail to move ";
188 char * msg = try_malloc(strlen(dsc_move) + strlen (dsc_dir) + 3, __func__);
189 int test = sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
190 exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]);
197 static void playerbonus_drop(uint8_t owns_none)
201 update_log("\nYou try to drop an object, but you own none.");
204 update_log("\nYou drop an object.");
209 static void playerbonus_pick(uint8_t picked)
213 update_log("\nYou pick up an object.");
216 update_log("\nYou try to pick up an object, but there is none.");
221 static void playerbonus_use(uint8_t no_thing, uint8_t wrong_thing)
225 update_log("\nYou try to use an object, but you own none.");
228 else if (wrong_thing)
230 update_log("\nYou try to use this object, but fail.");
233 update_log("\nYou consume MAGIC MEAT.");
238 extern void actor_wait(struct Thing * t)
240 if (t == get_player())
248 extern void actor_move(struct Thing * t)
251 struct yx_uint8 target = mv_yx_in_dir(d, t->pos);
252 struct Thing * other_t;
253 for (other_t = world.things; other_t != 0; other_t = other_t->next)
255 if (0 == other_t->lifepoints || other_t == t)
259 if (yx_uint8_cmp(&target, &other_t->pos))
261 actor_hits_actor(t, other_t);
265 uint8_t passable = is_passable(target);
268 set_thing_position(t, target);
270 t->fov_map = build_fov_map(t);
272 if (t == get_player())
274 playerbonus_move(d, passable);
280 extern void actor_drop(struct Thing * t)
282 uint8_t owns_none = (NULL == t->owns);
285 uint8_t select = t->arg;
286 struct Thing * owned = t->owns;
288 for (; i != select; i++, owned = owned->next);
289 own_thing(&world.things, &t->owns, owned->id);
291 if (t == get_player())
293 playerbonus_drop(owns_none);
299 extern void actor_pick(struct Thing * t)
301 struct Thing * picked = NULL;
303 for (t_i = world.things; NULL != t_i; t_i = t_i->next)
305 if (t_i != t && yx_uint8_cmp(&t_i->pos, &t->pos))
312 own_thing(&t->owns, &world.things, picked->id);
313 set_thing_position(picked, t->pos);
315 if (t == get_player())
317 playerbonus_pick(NULL != picked);
323 extern void actor_use(struct Thing * t)
325 uint8_t wrong_thing = 1;
326 uint8_t no_thing = (NULL == t->owns);
329 uint8_t select = t->arg;
331 struct Thing * selected = t->owns;
332 for (; i != select; i++, selected = selected->next);
333 struct ThingType * tt = get_thing_type(selected->type);
337 struct Thing * next = selected->next;
343 for (i = 0; i != select; i++, selected = selected->next);
344 selected->next = next;
350 t->lifepoints = t->lifepoints + tt->consumable;
353 if (t == get_player())
355 playerbonus_use(no_thing, wrong_thing);