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 char * f_name = "actor_hits_actor()";
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, f_name);
134 sprintf(msg, "\n%s %s %s.", msg1, msg2, msg3);
137 hitted->lifepoints--;
138 if (0 == hitted->lifepoints)
140 hitted->type = tt_hitted->corpse_id;
141 if (player == hitted)
143 update_log(" You die.");
146 update_log(" It dies.");
152 static void playerbonus_wait()
154 update_log("\nYou wait.");
159 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match)
171 static void playerbonus_move(char d, uint8_t passable)
173 char * f_name = "playerbonus_move()";
174 char * dsc_dir = "north-east";
175 if ( match_dir(d, &dsc_dir, 'd', "east")
176 || match_dir(d, &dsc_dir, 'c', "south-east")
177 || match_dir(d, &dsc_dir, 'x', "south-west")
178 || match_dir(d, &dsc_dir, 's', "west")
179 || match_dir(d, &dsc_dir, 'w', "north-west"))
183 char * dsc_move = "You move ";
186 dsc_move = "You fail to move ";
188 char * msg = try_malloc(strlen(dsc_move) + strlen (dsc_dir) + 3, f_name);
189 sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
196 static void playerbonus_drop(uint8_t owns_none)
200 update_log("\nYou try to drop an object, but you own none.");
203 update_log("\nYou drop an object.");
208 static void playerbonus_pick(uint8_t picked)
212 update_log("\nYou pick up an object.");
215 update_log("\nYou try to pick up an object, but there is none.");
220 static void playerbonus_use(uint8_t no_thing, uint8_t wrong_thing)
224 update_log("\nYou try to use an object, but you own none.");
227 else if (wrong_thing)
229 update_log("\nYou try to use this object, but fail.");
232 update_log("\nYou consume MAGIC MEAT.");
237 extern void free_thing_actions(struct ThingAction * ta)
244 free_thing_actions(ta->next);
250 extern uint8_t get_thing_action_id_by_name(char * name)
252 struct ThingAction * ta = world.thing_actions;
255 if (0 == strcmp(ta->name, name))
261 exit_err(NULL == ta, "get_thing_action_id_by_name() did not find action.");
267 extern void actor_wait(struct Thing * t)
269 if (t == get_player())
277 extern void actor_move(struct Thing * t)
280 struct yx_uint8 target = mv_yx_in_dir(d, t->pos);
281 struct Thing * other_t;
282 for (other_t = world.things; other_t != 0; other_t = other_t->next)
284 if (0 == other_t->lifepoints || other_t == t)
288 if (yx_uint8_cmp(&target, &other_t->pos))
290 actor_hits_actor(t, other_t);
294 uint8_t passable = is_passable(target);
297 set_thing_position(t, target);
299 t->fov_map = build_fov_map(t);
301 if (t == get_player())
303 playerbonus_move(d, passable);
309 extern void actor_drop(struct Thing * t)
311 uint8_t owns_none = (NULL == t->owns);
314 uint8_t select = t->arg;
315 struct Thing * owned = t->owns;
317 for (; i != select; i++, owned = owned->next);
318 own_thing(&world.things, &t->owns, owned->id);
320 if (t == get_player())
322 playerbonus_drop(owns_none);
328 extern void actor_pick(struct Thing * t)
330 struct Thing * picked = NULL;
332 for (t_i = world.things; NULL != t_i; t_i = t_i->next)
334 if (t_i != t && yx_uint8_cmp(&t_i->pos, &t->pos))
341 own_thing(&t->owns, &world.things, picked->id);
342 set_thing_position(picked, t->pos);
344 if (t == get_player())
346 playerbonus_pick(NULL != picked);
352 extern void actor_use(struct Thing * t)
354 uint8_t wrong_thing = 1;
355 uint8_t no_thing = (NULL == t->owns);
358 uint8_t select = t->arg;
360 struct Thing * selected = t->owns;
361 for (; i != select; i++, selected = selected->next);
362 struct ThingType * tt = get_thing_type(selected->type);
366 struct Thing * next = selected->next;
372 for (i = 0; i != select; i++, selected = selected->next);
373 selected->next = next;
379 t->lifepoints = t->lifepoints + tt->consumable;
382 if (t == get_player())
384 playerbonus_use(no_thing, wrong_thing);