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 /* Append "text" to game log, or a "." if "text" is the same as the last one. */
23 static void update_log(char * text);
25 /* One actor "wounds" another actor, decrementing his lifepoints and, if they
26 * reach zero in the process, killing it. Generates appropriate log message.
28 static void actor_hits_actor(struct Thing * hitter, struct Thing * hitted);
30 /* Bonus stuff to actor_*() to happen if actor==player. Mostly writing of log
31 * messages; _pick and _drop also decrement world.inventory_sel by 1 if >0.
32 * (match_dir() is just a little helper to playerbonus_move().)
34 static void playerbonus_wait();
35 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match);
36 static void playerbonus_move(char d, uint8_t passable);
37 static void playerbonus_drop(uint8_t owns_none);
38 static void playerbonus_pick(uint8_t picked);
39 static void playerbonus_use(uint8_t no_thing, uint8_t wrong_thing);
43 static void update_log(char * text)
45 char * f_name = "update_log()";
46 uint16_t len_new = strlen(text);
50 len_old = strlen(world.log);
51 uint16_t last_nl = len_old - 1;
54 if ('\n' == world.log[last_nl])
60 uint16_t last_stop = len_old - 1;
61 while (last_stop != 0)
63 if ('.' == world.log[last_stop] && '.' != world.log[last_stop - 1])
69 if ( (last_stop + 1) - last_nl == (uint16_t) strlen(text)
70 && 0 == strncmp(world.log + last_nl, text, strlen(text)))
75 uint16_t len_whole = len_old + len_new + 1;
76 char * new_text = try_malloc(len_whole, f_name);
77 memcpy(new_text, world.log, len_old);
78 sprintf(new_text + len_old, "%s", text);
85 static void actor_hits_actor(struct Thing * hitter, struct Thing * hitted)
87 struct ThingType * tt_hitter = get_thing_type(hitter->type);
88 struct ThingType * tt_hitted = get_thing_type(hitted->type);
89 struct Thing * player = get_player();
91 char * msg2 = "wound";
95 msg1 = tt_hitter->name;
100 msg3 = tt_hitted->name;
102 uint8_t len = 1 + strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2;
104 sprintf(msg, "\n%s %s %s.", msg1, msg2, msg3);
106 hitted->lifepoints--;
107 if (0 == hitted->lifepoints)
109 hitted->type = tt_hitted->corpse_id;
110 if (player == hitted)
112 update_log(" You die.");
115 update_log(" It dies.");
121 static void playerbonus_wait()
123 update_log("\nYou wait.");
128 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match)
140 static void playerbonus_move(char d, uint8_t passable)
142 char * dsc_dir = "north-east";
143 if ( match_dir(d, &dsc_dir, 'd', "east")
144 || match_dir(d, &dsc_dir, 'c', "south-east")
145 || match_dir(d, &dsc_dir, 'x', "south-west")
146 || match_dir(d, &dsc_dir, 's', "west")
147 || match_dir(d, &dsc_dir, 'w', "north-west"))
151 char * dsc_move = "You move ";
154 dsc_move = "You fail to move ";
156 char msg[strlen(dsc_move) + strlen (dsc_dir) + 3];
157 sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
163 static void playerbonus_drop(uint8_t owns_none)
167 update_log("\nYou try to drop an object, but you own none.");
170 update_log("\nYou drop an object.");
175 static void playerbonus_pick(uint8_t picked)
179 update_log("\nYou pick up an object.");
182 update_log("\nYou try to pick up an object, but there is none.");
187 static void playerbonus_use(uint8_t no_thing, uint8_t wrong_thing)
191 update_log("\nYou try to use an object, but you own none.");
194 else if (wrong_thing)
196 update_log("\nYou try to use this object, but fail.");
199 update_log("\nYou consume MAGIC MEAT.");
204 extern void free_thing_actions(struct ThingAction * ta)
211 free_thing_actions(ta->next);
217 extern uint8_t get_thing_action_id_by_name(char * name)
219 struct ThingAction * ta = world.thing_actions;
222 if (0 == strcmp(ta->name, name))
228 exit_err(NULL == ta, "get_thing_action_id_by_name() did not find action.");
234 extern void actor_wait(struct Thing * t)
236 if (t == get_player())
244 extern void actor_move(struct Thing * t)
247 struct yx_uint8 target = mv_yx_in_dir(d, t->pos);
248 struct Thing * other_t;
249 for (other_t = world.things; other_t != 0; other_t = other_t->next)
251 if (0 == other_t->lifepoints || other_t == t)
255 if (yx_uint8_cmp(&target, &other_t->pos))
257 actor_hits_actor(t, other_t);
261 uint8_t passable = is_passable(target);
264 set_thing_position(t, target);
266 t->fov_map = build_fov_map(t);
268 if (t == get_player())
270 playerbonus_move(d, passable);
276 extern void actor_drop(struct Thing * t)
278 uint8_t owns_none = (NULL == t->owns);
281 uint8_t select = t->arg;
282 struct Thing * owned = t->owns;
284 for (; i != select; i++, owned = owned->next);
285 own_thing(&world.things, &t->owns, owned->id);
287 if (t == get_player())
289 playerbonus_drop(owns_none);
295 extern void actor_pick(struct Thing * t)
297 struct Thing * picked = NULL;
299 for (t_i = world.things; NULL != t_i; t_i = t_i->next)
301 if (t_i != t && yx_uint8_cmp(&t_i->pos, &t->pos))
308 own_thing(&t->owns, &world.things, picked->id);
309 set_thing_position(picked, t->pos);
311 if (t == get_player())
313 playerbonus_pick(NULL != picked);
319 extern void actor_use(struct Thing * t)
321 uint8_t wrong_thing = 1;
322 uint8_t no_thing = (NULL == t->owns);
325 uint8_t select = t->arg;
327 struct Thing * selected = t->owns;
328 for (; i != select; i++, selected = selected->next);
329 struct ThingType * tt = get_thing_type(selected->type);
333 struct Thing * next = selected->next;
339 for (i = 0; i != select; i++, selected = selected->next);
340 selected->next = next;
346 t->lifepoints = t->lifepoints + tt->consumable;
349 if (t == get_player())
351 playerbonus_use(no_thing, wrong_thing);