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" /* 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() */
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.");
145 memset(hitted->fov_map, ' ', world.map.length * world.map.length);
150 free(hitted->fov_map);
151 hitted->fov_map = NULL;
152 free(hitted->mem_map);
153 hitted->mem_map = NULL;
154 free_things_in_memory(hitted->t_mem);
155 hitted->t_mem = NULL;
157 update_log(" It dies.");
163 static void playerbonus_wait()
165 update_log("\nYou wait.");
170 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match)
182 static void playerbonus_move(char d, uint8_t passable)
184 char * dsc_dir = "north-east";
185 if ( match_dir(d, &dsc_dir, 'd', "east")
186 || match_dir(d, &dsc_dir, 'c', "south-east")
187 || match_dir(d, &dsc_dir, 'x', "south-west")
188 || match_dir(d, &dsc_dir, 's', "west")
189 || match_dir(d, &dsc_dir, 'w', "north-west"))
193 char * dsc_move = "You move ";
196 dsc_move = "You fail to move ";
198 char * msg = try_malloc(strlen(dsc_move) + strlen (dsc_dir) + 3, __func__);
199 int test = sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
200 exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]);
207 static void playerbonus_drop(uint8_t owns_none)
211 update_log("\nYou try to drop an object, but you own none.");
214 update_log("\nYou drop an object.");
219 static void playerbonus_pick(uint8_t picked)
223 update_log("\nYou pick up an object.");
226 update_log("\nYou try to pick up an object, but there is none.");
231 static void playerbonus_use(uint8_t no_thing, uint8_t wrong_thing)
235 update_log("\nYou try to use an object, but you own none.");
238 else if (wrong_thing)
240 update_log("\nYou try to use this object, but fail.");
243 update_log("\nYou consume MAGIC MEAT.");
248 extern void actor_wait(struct Thing * t)
250 if (t == get_player())
258 extern void actor_move(struct Thing * t)
261 struct Thing * other_t;
262 struct yx_uint8 target = t->pos;
263 uint8_t legal_move = mv_yx_in_dir_legal(d, &target);
264 mv_yx_in_dir_legal(0, NULL);
265 uint8_t passable = 0;
268 passable = '.' == world.map.cells[target.y*world.map.length + target.x];
269 for (other_t = world.things; other_t != 0; other_t = other_t->next)
271 if (0 == other_t->lifepoints || other_t == t)
275 if (target.y == other_t->pos.y && target.x == other_t->pos.x)
277 actor_hits_actor(t, other_t);
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 && t_i->pos.y == t->pos.y && t_i->pos.x == t->pos.x)
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);