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(), 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 "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 int test = sprintf(new_text + len_old, "%s", text);
108 exit_trouble(test < 0, f_name, "sprintf()");
110 world.log = new_text;
115 static void actor_hits_actor(struct Thing * hitter, struct Thing * hitted)
117 char * f_name = "actor_hits_actor()";
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, f_name);
135 int test = sprintf(msg, "\n%s %s %s.", msg1, msg2, msg3);
136 exit_trouble(test < 0, f_name, "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.");
148 update_log(" It dies.");
154 static void playerbonus_wait()
156 update_log("\nYou wait.");
161 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match)
173 static void playerbonus_move(char d, uint8_t passable)
175 char * f_name = "playerbonus_move()";
176 char * dsc_dir = "north-east";
177 if ( match_dir(d, &dsc_dir, 'd', "east")
178 || match_dir(d, &dsc_dir, 'c', "south-east")
179 || match_dir(d, &dsc_dir, 'x', "south-west")
180 || match_dir(d, &dsc_dir, 's', "west")
181 || match_dir(d, &dsc_dir, 'w', "north-west"))
185 char * dsc_move = "You move ";
188 dsc_move = "You fail to move ";
190 char * msg = try_malloc(strlen(dsc_move) + strlen (dsc_dir) + 3, f_name);
191 int test = sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
192 exit_trouble(test < 0, f_name, "sprintf()");
199 static void playerbonus_drop(uint8_t owns_none)
203 update_log("\nYou try to drop an object, but you own none.");
206 update_log("\nYou drop an object.");
211 static void playerbonus_pick(uint8_t picked)
215 update_log("\nYou pick up an object.");
218 update_log("\nYou try to pick up an object, but there is none.");
223 static void playerbonus_use(uint8_t no_thing, uint8_t wrong_thing)
227 update_log("\nYou try to use an object, but you own none.");
230 else if (wrong_thing)
232 update_log("\nYou try to use this object, but fail.");
235 update_log("\nYou consume MAGIC MEAT.");
240 extern void free_thing_actions(struct ThingAction * ta)
247 free_thing_actions(ta->next);
253 extern uint8_t get_thing_action_id_by_name(char * name)
255 struct ThingAction * ta = world.thing_actions;
258 if (0 == strcmp(ta->name, name))
264 exit_err(NULL == ta, "get_thing_action_id_by_name() did not find action.");
270 extern void actor_wait(struct Thing * t)
272 if (t == get_player())
280 extern void actor_move(struct Thing * t)
283 struct yx_uint8 target = mv_yx_in_dir(d, t->pos);
284 struct Thing * other_t;
285 for (other_t = world.things; other_t != 0; other_t = other_t->next)
287 if (0 == other_t->lifepoints || other_t == t)
291 if (yx_uint8_cmp(&target, &other_t->pos))
293 actor_hits_actor(t, other_t);
297 uint8_t passable = is_passable(target);
300 set_thing_position(t, target);
302 t->fov_map = build_fov_map(t);
304 if (t == get_player())
306 playerbonus_move(d, passable);
312 extern void actor_drop(struct Thing * t)
314 uint8_t owns_none = (NULL == t->owns);
317 uint8_t select = t->arg;
318 struct Thing * owned = t->owns;
320 for (; i != select; i++, owned = owned->next);
321 own_thing(&world.things, &t->owns, owned->id);
323 if (t == get_player())
325 playerbonus_drop(owns_none);
331 extern void actor_pick(struct Thing * t)
333 struct Thing * picked = NULL;
335 for (t_i = world.things; NULL != t_i; t_i = t_i->next)
337 if (t_i != t && yx_uint8_cmp(&t_i->pos, &t->pos))
344 own_thing(&t->owns, &world.things, picked->id);
345 set_thing_position(picked, t->pos);
347 if (t == get_player())
349 playerbonus_pick(NULL != picked);
355 extern void actor_use(struct Thing * t)
357 uint8_t wrong_thing = 1;
358 uint8_t no_thing = (NULL == t->owns);
361 uint8_t select = t->arg;
363 struct Thing * selected = t->owns;
364 for (; i != select; i++, selected = selected->next);
365 struct ThingType * tt = get_thing_type(selected->type);
369 struct Thing * next = selected->next;
375 for (i = 0; i != select; i++, selected = selected->next);
376 selected->next = next;
382 t->lifepoints = t->lifepoints + tt->consumable;
385 if (t == get_player())
387 playerbonus_use(no_thing, wrong_thing);