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" /* is_passable() */
19 #include "yx_uint8.h" /* mv_yx_in_dir(), 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 yx_uint8 target = mv_yx_in_dir(d, t->pos);
263 struct Thing * other_t;
264 for (other_t = world.things; other_t != 0; other_t = other_t->next)
266 if (0 == other_t->lifepoints || other_t == t)
270 if (yx_uint8_cmp(&target, &other_t->pos))
272 actor_hits_actor(t, other_t);
276 uint8_t passable = is_passable(target);
279 set_thing_position(t, target);
282 if (t == get_player())
284 playerbonus_move(d, passable);
290 extern void actor_drop(struct Thing * t)
292 uint8_t owns_none = (!t->owns);
295 uint8_t select = t->arg;
296 struct Thing * owned = t->owns;
298 for (; i != select; i++, owned = owned->next);
299 own_thing(&world.things, &t->owns, owned->id);
301 if (t == get_player())
303 playerbonus_drop(owns_none);
309 extern void actor_pick(struct Thing * t)
311 struct Thing * picked = NULL;
313 for (t_i = world.things; t_i; t_i = t_i->next)
315 if (t_i != t && yx_uint8_cmp(&t_i->pos, &t->pos))
322 own_thing(&t->owns, &world.things, picked->id);
323 set_thing_position(picked, t->pos);
325 if (t == get_player())
327 playerbonus_pick(!(!picked));
333 extern void actor_use(struct Thing * t)
335 uint8_t wrong_thing = 1;
336 uint8_t no_thing = (!t->owns);
339 uint8_t select = t->arg;
341 struct Thing * selected = t->owns;
342 for (; i != select; i++, selected = selected->next);
343 struct ThingType * tt = get_thing_type(selected->type);
347 struct Thing * next = selected->next;
353 for (i = 0; i != select; i++, selected = selected->next);
354 selected->next = next;
360 t->lifepoints = t->lifepoints + tt->consumable;
363 if (t == get_player())
365 playerbonus_use(no_thing, wrong_thing);