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(),
16 * free_things_in_memory()
18 #include "map.h" /* mv_yx_in_dir_legal(), is_passable() */
19 #include "yx_uint8.h" /* mv_yx_in_dir_wrap(), yx_uint8_cmp() */
20 #include "world.h" /* global world */
24 /* How many previous characters of the game log to keep on adding new text */
25 #define MAX_BACKLOG_CHARS 3000
29 /* If "text" is equal "log"'s last line, return 1, else 0. */
30 static uint8_t text_equals_log_end(char * log, char * text);
32 /* Append "text" to game log shortened to MAX_BACKLOG_CHARS characters, or
33 * continuation period if "text" is the same as the (shortened) log's last line
34 * minus continuation periods.
36 static void update_log(char * text);
38 /* One actor "wounds" another actor, decrementing his lifepoints and, if they
39 * reach zero in the process, killing it. Generates appropriate log message.
41 static void actor_hits_actor(struct Thing * hitter, struct Thing * hitted);
43 /* Bonus stuff to actor_*() to happen if actor==player. Mostly writing of log
44 * messages; _pick and _drop also decrement world.inventory_sel by 1 if >0.
45 * (match_dir() is just a little helper to playerbonus_move().)
47 static void playerbonus_wait();
48 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match);
49 static void playerbonus_move(char d, uint8_t passable);
50 static void playerbonus_drop(uint8_t owns_none);
51 static void playerbonus_pick(uint8_t picked);
52 static void playerbonus_use(uint8_t no_thing, uint8_t wrong_thing);
56 static uint8_t text_equals_log_end(char * log, char * text)
58 uint16_t len_old = strlen(log);
59 uint16_t last_nl = len_old - 1;
62 if ('\n' == log[last_nl])
68 uint16_t last_stop = len_old - 1;
69 while (last_stop != 0)
71 if ('.' == log[last_stop] && '.' != log[last_stop - 1])
77 if ( (last_stop + 1) - last_nl == (uint16_t) strlen(text)
78 && 0 == strncmp(log + last_nl, text, strlen(text)))
87 static void update_log(char * text)
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, __func__);
107 memcpy(new_text, world.log + offset, len_old);
108 int test = sprintf(new_text + len_old, "%s", text);
109 exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]);
111 world.log = new_text;
116 static void actor_hits_actor(struct Thing * hitter, struct Thing * hitted)
118 struct ThingType * tt_hitter = get_thing_type(hitter->type);
119 struct ThingType * tt_hitted = get_thing_type(hitted->type);
120 struct Thing * player = get_player();
122 char * msg2 = "wound";
124 if (player != hitter)
126 msg1 = tt_hitter->name;
129 if (player != hitted)
131 msg3 = tt_hitted->name;
133 uint8_t len = 1 + strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2;
134 char * msg = try_malloc(len, __func__);
135 int test = sprintf(msg, "\n%s %s %s.", msg1, msg2, msg3);
136 exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]);
139 hitted->lifepoints--;
140 if (0 == hitted->lifepoints)
142 hitted->type = tt_hitted->corpse_id;
143 if (player == hitted)
145 update_log(" You die.");
146 memset(hitted->fov_map, ' ', world.map.length * world.map.length);
151 free(hitted->fov_map);
152 hitted->fov_map = NULL;
153 free(hitted->mem_map);
154 hitted->mem_map = NULL;
155 free_things_in_memory(hitted->t_mem);
156 hitted->t_mem = NULL;
158 update_log(" It dies.");
164 static void playerbonus_wait()
166 update_log("\nYou wait.");
171 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match)
183 static void playerbonus_move(char d, uint8_t passable)
185 char * dsc_dir = "north-east";
186 if ( match_dir(d, &dsc_dir, 'd', "east")
187 || match_dir(d, &dsc_dir, 'c', "south-east")
188 || match_dir(d, &dsc_dir, 'x', "south-west")
189 || match_dir(d, &dsc_dir, 's', "west")
190 || match_dir(d, &dsc_dir, 'w', "north-west"))
194 char * dsc_move = "You move ";
197 dsc_move = "You fail to move ";
199 char * msg = try_malloc(strlen(dsc_move) + strlen (dsc_dir) + 3, __func__);
200 int test = sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
201 exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]);
208 static void playerbonus_drop(uint8_t owns_none)
212 update_log("\nYou try to drop an object, but you own none.");
215 update_log("\nYou drop an object.");
220 static void playerbonus_pick(uint8_t picked)
224 update_log("\nYou pick up an object.");
227 update_log("\nYou try to pick up an object, but there is none.");
232 static void playerbonus_use(uint8_t no_thing, uint8_t wrong_thing)
236 update_log("\nYou try to use an object, but you own none.");
239 else if (wrong_thing)
241 update_log("\nYou try to use this object, but fail.");
244 update_log("\nYou consume MAGIC MEAT.");
249 extern void actor_wait(struct Thing * t)
251 if (t == get_player())
259 extern void actor_move(struct Thing * t)
262 struct Thing * other_t;
263 struct yx_uint8 target = t->pos;
264 uint8_t legal_move = mv_yx_in_dir_legal(d, &target);
265 mv_yx_in_dir_wrap(0, NULL, 1);
268 for (other_t = world.things; other_t != 0; other_t = other_t->next)
270 if (0 == other_t->lifepoints || other_t == t)
274 if (yx_uint8_cmp(&target, &other_t->pos))
276 actor_hits_actor(t, other_t);
281 uint8_t passable = legal_move && is_passable(target);
284 set_thing_position(t, target);
287 if (t == get_player())
289 playerbonus_move(d, passable);
295 extern void actor_drop(struct Thing * t)
297 uint8_t owns_none = (!t->owns);
300 uint8_t select = t->arg;
301 struct Thing * owned = t->owns;
303 for (; i != select; i++, owned = owned->next);
304 own_thing(&world.things, &t->owns, owned->id);
306 if (t == get_player())
308 playerbonus_drop(owns_none);
314 extern void actor_pick(struct Thing * t)
316 struct Thing * picked = NULL;
318 for (t_i = world.things; t_i; t_i = t_i->next)
320 if (t_i != t && yx_uint8_cmp(&t_i->pos, &t->pos))
327 own_thing(&t->owns, &world.things, picked->id);
328 set_thing_position(picked, t->pos);
330 if (t == get_player())
332 playerbonus_pick(!(!picked));
338 extern void actor_use(struct Thing * t)
340 uint8_t wrong_thing = 1;
341 uint8_t no_thing = (!t->owns);
344 uint8_t select = t->arg;
346 struct Thing * selected = t->owns;
347 for (; i != select; i++, selected = selected->next);
348 struct ThingType * tt = get_thing_type(selected->type);
352 struct Thing * next = selected->next;
358 for (i = 0; i != select; i++, selected = selected->next);
359 selected->next = next;
365 t->lifepoints = t->lifepoints + tt->consumable;
368 if (t == get_player())
370 playerbonus_use(no_thing, wrong_thing);