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() */
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 "things.h" /* structs Thing, ThingType, get_player(), own_thing(),
14 * set_thing_position(), get_thing_type()
16 #include "map.h" /* is_passable() */
17 #include "yx_uint8.h" /* mv_yx_in_dir(), yx_uint8_cmp() */
18 #include "world.h" /* global world */
22 /* How many previous characters of the game log to keep on adding new text */
23 #define MAX_BACKLOG_CHARS 3000
27 /* If "text" is equal "log"'s last line, return 1, else 0. */
28 static uint8_t text_equals_log_end(char * log, char * text);
30 /* Append "text" to game log shortened to MAX_BACKLOG_CHARS characters, or
31 * continuation period if "text" is the same as the (shortened) log's last line
32 * minus continuation periods.
34 static void update_log(char * text);
36 /* One actor "wounds" another actor, decrementing his lifepoints and, if they
37 * reach zero in the process, killing it. Generates appropriate log message.
39 static void actor_hits_actor(struct Thing * hitter, struct Thing * hitted);
41 /* Bonus stuff to actor_*() to happen if actor==player. Mostly writing of log
42 * messages; _pick and _drop also decrement world.inventory_sel by 1 if >0.
43 * (match_dir() is just a little helper to playerbonus_move().)
45 static void playerbonus_wait();
46 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match);
47 static void playerbonus_move(char d, uint8_t passable);
48 static void playerbonus_drop(uint8_t owns_none);
49 static void playerbonus_pick(uint8_t picked);
50 static void playerbonus_use(uint8_t no_thing, uint8_t wrong_thing);
54 static uint8_t text_equals_log_end(char * log, char * text)
56 uint16_t len_old = strlen(log);
57 uint16_t last_nl = len_old - 1;
60 if ('\n' == log[last_nl])
66 uint16_t last_stop = len_old - 1;
67 while (last_stop != 0)
69 if ('.' == log[last_stop] && '.' != log[last_stop - 1])
75 if ( (last_stop + 1) - last_nl == (uint16_t) strlen(text)
76 && 0 == strncmp(log + last_nl, text, strlen(text)))
85 static void update_log(char * text)
87 char * f_name = "update_log()";
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, f_name);
106 memcpy(new_text, world.log + offset, len_old);
107 sprintf(new_text + len_old, "%s", text);
109 world.log = new_text;
114 static void actor_hits_actor(struct Thing * hitter, struct Thing * hitted)
116 struct ThingType * tt_hitter = get_thing_type(hitter->type);
117 struct ThingType * tt_hitted = get_thing_type(hitted->type);
118 struct Thing * player = get_player();
120 char * msg2 = "wound";
122 if (player != hitter)
124 msg1 = tt_hitter->name;
127 if (player != hitted)
129 msg3 = tt_hitted->name;
131 uint8_t len = 1 + strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2;
133 sprintf(msg, "\n%s %s %s.", msg1, msg2, msg3);
135 hitted->lifepoints--;
136 if (0 == hitted->lifepoints)
138 hitted->type = tt_hitted->corpse_id;
139 if (player == hitted)
141 update_log(" You die.");
144 update_log(" It dies.");
150 static void playerbonus_wait()
152 update_log("\nYou wait.");
157 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match)
169 static void playerbonus_move(char d, uint8_t passable)
171 char * dsc_dir = "north-east";
172 if ( match_dir(d, &dsc_dir, 'd', "east")
173 || match_dir(d, &dsc_dir, 'c', "south-east")
174 || match_dir(d, &dsc_dir, 'x', "south-west")
175 || match_dir(d, &dsc_dir, 's', "west")
176 || match_dir(d, &dsc_dir, 'w', "north-west"))
180 char * dsc_move = "You move ";
183 dsc_move = "You fail to move ";
185 char msg[strlen(dsc_move) + strlen (dsc_dir) + 3];
186 sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
192 static void playerbonus_drop(uint8_t owns_none)
196 update_log("\nYou try to drop an object, but you own none.");
199 update_log("\nYou drop an object.");
204 static void playerbonus_pick(uint8_t picked)
208 update_log("\nYou pick up an object.");
211 update_log("\nYou try to pick up an object, but there is none.");
216 static void playerbonus_use(uint8_t no_thing, uint8_t wrong_thing)
220 update_log("\nYou try to use an object, but you own none.");
223 else if (wrong_thing)
225 update_log("\nYou try to use this object, but fail.");
228 update_log("\nYou consume MAGIC MEAT.");
233 extern void free_thing_actions(struct ThingAction * ta)
240 free_thing_actions(ta->next);
246 extern uint8_t get_thing_action_id_by_name(char * name)
248 struct ThingAction * ta = world.thing_actions;
251 if (0 == strcmp(ta->name, name))
257 exit_err(NULL == ta, "get_thing_action_id_by_name() did not find action.");
263 extern void actor_wait(struct Thing * t)
265 if (t == get_player())
273 extern void actor_move(struct Thing * t)
276 struct yx_uint8 target = mv_yx_in_dir(d, t->pos);
277 struct Thing * other_t;
278 for (other_t = world.things; other_t != 0; other_t = other_t->next)
280 if (0 == other_t->lifepoints || other_t == t)
284 if (yx_uint8_cmp(&target, &other_t->pos))
286 actor_hits_actor(t, other_t);
290 uint8_t passable = is_passable(target);
293 set_thing_position(t, target);
295 t->fov_map = build_fov_map(t);
297 if (t == get_player())
299 playerbonus_move(d, passable);
305 extern void actor_drop(struct Thing * t)
307 uint8_t owns_none = (NULL == t->owns);
310 uint8_t select = t->arg;
311 struct Thing * owned = t->owns;
313 for (; i != select; i++, owned = owned->next);
314 own_thing(&world.things, &t->owns, owned->id);
316 if (t == get_player())
318 playerbonus_drop(owns_none);
324 extern void actor_pick(struct Thing * t)
326 struct Thing * picked = NULL;
328 for (t_i = world.things; NULL != t_i; t_i = t_i->next)
330 if (t_i != t && yx_uint8_cmp(&t_i->pos, &t->pos))
337 own_thing(&t->owns, &world.things, picked->id);
338 set_thing_position(picked, t->pos);
340 if (t == get_player())
342 playerbonus_pick(NULL != picked);
348 extern void actor_use(struct Thing * t)
350 uint8_t wrong_thing = 1;
351 uint8_t no_thing = (NULL == t->owns);
354 uint8_t select = t->arg;
356 struct Thing * selected = t->owns;
357 for (; i != select; i++, selected = selected->next);
358 struct ThingType * tt = get_thing_type(selected->type);
362 struct Thing * next = selected->next;
368 for (i = 0; i != select; i++, selected = selected->next);
369 selected->next = next;
375 t->lifepoints = t->lifepoints + tt->consumable;
378 if (t == get_player())
380 playerbonus_use(no_thing, wrong_thing);