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(), strcmp(), memcpy(), strncmp() */
9 #include "../common/rexit.h" /* exit_err(), 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" /* structs 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 char * f_name = "update_log()";
89 uint16_t len_new = strlen(text);
94 len_old = strlen(world.log);
95 if (len_old > MAX_BACKLOG_CHARS)
97 offset = len_old - MAX_BACKLOG_CHARS;
98 len_old = MAX_BACKLOG_CHARS;
100 if (text_equals_log_end(world.log + offset, text))
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]);
111 world.log = new_text;
116 static void actor_hits_actor(struct Thing * hitter, struct Thing * hitted)
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();
123 char * msg2 = "wound";
125 if (player != hitter)
127 msg1 = tt_hitter->name;
130 if (player != hitted)
132 msg3 = tt_hitted->name;
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]);
140 hitted->lifepoints--;
141 if (0 == hitted->lifepoints)
143 hitted->type = tt_hitted->corpse_id;
144 if (player == hitted)
146 update_log(" You die.");
149 update_log(" It dies.");
155 static void playerbonus_wait()
157 update_log("\nYou wait.");
162 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match)
174 static void playerbonus_move(char d, uint8_t passable)
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"))
186 char * dsc_move = "You move ";
189 dsc_move = "You fail to move ";
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]);
200 static void playerbonus_drop(uint8_t owns_none)
204 update_log("\nYou try to drop an object, but you own none.");
207 update_log("\nYou drop an object.");
212 static void playerbonus_pick(uint8_t picked)
216 update_log("\nYou pick up an object.");
219 update_log("\nYou try to pick up an object, but there is none.");
224 static void playerbonus_use(uint8_t no_thing, uint8_t wrong_thing)
228 update_log("\nYou try to use an object, but you own none.");
231 else if (wrong_thing)
233 update_log("\nYou try to use this object, but fail.");
236 update_log("\nYou consume MAGIC MEAT.");
241 extern void free_thing_actions(struct ThingAction * ta)
248 free_thing_actions(ta->next);
254 extern uint8_t get_thing_action_id_by_name(char * name)
256 struct ThingAction * ta = world.thing_actions;
259 if (0 == strcmp(ta->name, name))
265 exit_err(NULL == ta, "get_thing_action_id_by_name() did not find action.");
271 extern void actor_wait(struct Thing * t)
273 if (t == get_player())
281 extern void actor_move(struct Thing * t)
284 struct yx_uint8 target = mv_yx_in_dir(d, t->pos);
285 struct Thing * other_t;
286 for (other_t = world.things; other_t != 0; other_t = other_t->next)
288 if (0 == other_t->lifepoints || other_t == t)
292 if (yx_uint8_cmp(&target, &other_t->pos))
294 actor_hits_actor(t, other_t);
298 uint8_t passable = is_passable(target);
301 set_thing_position(t, target);
303 t->fov_map = build_fov_map(t);
305 if (t == get_player())
307 playerbonus_move(d, passable);
313 extern void actor_drop(struct Thing * t)
315 uint8_t owns_none = (NULL == t->owns);
318 uint8_t select = t->arg;
319 struct Thing * owned = t->owns;
321 for (; i != select; i++, owned = owned->next);
322 own_thing(&world.things, &t->owns, owned->id);
324 if (t == get_player())
326 playerbonus_drop(owns_none);
332 extern void actor_pick(struct Thing * t)
334 struct Thing * picked = NULL;
336 for (t_i = world.things; NULL != t_i; t_i = t_i->next)
338 if (t_i != t && yx_uint8_cmp(&t_i->pos, &t->pos))
345 own_thing(&t->owns, &world.things, picked->id);
346 set_thing_position(picked, t->pos);
348 if (t == get_player())
350 playerbonus_pick(NULL != picked);
356 extern void actor_use(struct Thing * t)
358 uint8_t wrong_thing = 1;
359 uint8_t no_thing = (NULL == t->owns);
362 uint8_t select = t->arg;
364 struct Thing * selected = t->owns;
365 for (; i != select; i++, selected = selected->next);
366 struct ThingType * tt = get_thing_type(selected->type);
370 struct Thing * next = selected->next;
376 for (i = 0; i != select; i++, selected = selected->next);
377 selected->next = next;
383 t->lifepoints = t->lifepoints + tt->consumable;
386 if (t == get_player())
388 playerbonus_use(no_thing, wrong_thing);