1 /* src/server/thing_actions.c
3 * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
4 * or any later version. For details on its copyright, license, and warranties,
5 * see the file NOTICE in the root directory of the PlomRogue source package.
8 #include "thing_actions.h"
9 #include <stddef.h> /* NULL */
10 #include <stdint.h> /* uint8_t */
11 #include <stdio.h> /* sprintf() */
12 #include <stdlib.h> /* free() */
13 #include <string.h> /* strlen() */
14 #include "../common/rexit.h" /* exit_trouble() */
15 #include "../common/try_malloc.h" /* try_malloc() */
16 #include "../common/yx_uint8.h" /* yx_uint8 */
17 #include "field_of_view.h" /* build_fov_map() */
18 #include "hardcoded_strings.h" /* s */
19 #include "things.h" /* Thing, ThingType, get_player(), free_things_in_memory(),
20 * own_thing(), set_thing_position(), get_thing_type(),
22 #include "map.h" /* mv_yx_in_dir_legal() */
23 #include "run.h" /* send_to_outfile() */
24 #include "world.h" /* global world */
28 /* Send "text" as log message to server out file. */
29 static void update_log(char * text);
31 /* One actor "wounds" another actor, decrementing his lifepoints and, if they
32 * reach zero in the process, killing it. Generates appropriate log message.
34 static void actor_hits_actor(struct Thing * hitter, struct Thing * hitted);
36 /* Bonus stuff to actor_*() to happen if actor==player. Mostly writing of log
37 * messages; _pick and _drop also decrement world.inventory_sel by 1 if >0.
38 * (match_dir() is just a little helper to playerbonus_move().)
40 static void playerbonus_wait();
41 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match);
42 static void playerbonus_move(char d, uint8_t passable);
43 static void playerbonus_drop(uint8_t owns_none);
44 static void playerbonus_pick(uint8_t picked);
45 static void playerbonus_use(uint8_t no_thing, uint8_t wrong_thing);
49 static void update_log(char * text)
51 send_to_outfile("LOG ", 0);
52 send_to_outfile(text, 0);
53 send_to_outfile("\n", 1);
58 static void actor_hits_actor(struct Thing * hitter, struct Thing * hitted)
60 struct ThingType * tt_hitter = get_thing_type(hitter->type);
61 struct ThingType * tt_hitted = get_thing_type(hitted->type);
62 struct Thing * player = get_player();
64 char * msg2 = "wound";
68 msg1 = tt_hitter->name;
73 msg3 = tt_hitted->name;
75 uint8_t len = strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2;
76 char * msg = try_malloc(len, __func__);
77 int test = sprintf(msg, "%s %s %s.", msg1, msg2, msg3);
78 exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]);
82 if (0 == hitted->lifepoints)
84 hitted->type = tt_hitted->corpse_id;
87 update_log("You die.");
88 memset(hitted->fov_map, ' ', world.map.length * world.map.length);
93 free(hitted->fov_map);
94 hitted->fov_map = NULL;
95 free(hitted->mem_map);
96 hitted->mem_map = NULL;
97 free_things_in_memory(hitted->t_mem);
100 update_log("It dies.");
106 static void playerbonus_wait()
108 update_log("You wait.");
113 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match)
125 static void playerbonus_move(char d, uint8_t passable)
127 char * dsc_dir = "north-east";
128 if ( match_dir(d, &dsc_dir, 'd', "east")
129 || match_dir(d, &dsc_dir, 'c', "south-east")
130 || match_dir(d, &dsc_dir, 'x', "south-west")
131 || match_dir(d, &dsc_dir, 's', "west")
132 || match_dir(d, &dsc_dir, 'w', "north-west"))
136 char * dsc_move = "You move ";
139 dsc_move = "You fail to move ";
141 char * msg = try_malloc(strlen(dsc_move) + strlen (dsc_dir) + 2, __func__);
142 int test = sprintf(msg, "%s%s.", dsc_move, dsc_dir);
143 exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]);
150 static void playerbonus_drop(uint8_t owns_none)
154 update_log("You try to drop an object, but you own none.");
157 update_log("You drop an object.");
162 static void playerbonus_pick(uint8_t picked)
166 update_log("You pick up an object.");
169 update_log("You try to pick up an object, but there is none.");
174 static void playerbonus_use(uint8_t no_thing, uint8_t wrong_thing)
178 update_log("You try to use an object, but you own none.");
181 else if (wrong_thing)
183 update_log("You try to use this object, but fail.");
186 update_log("You consume MAGIC MEAT.");
191 extern void actor_wait(struct Thing * t)
193 if (t == get_player())
201 extern void actor_move(struct Thing * t)
204 struct Thing * other_t;
205 struct yx_uint8 target = t->pos;
206 uint8_t legal_move = mv_yx_in_dir_legal(d, &target);
207 mv_yx_in_dir_legal(0, NULL);
208 uint8_t passable = 0;
211 passable = '.' == world.map.cells[target.y*world.map.length + target.x];
212 for (other_t = world.things; other_t != 0; other_t = other_t->next)
214 if (0 == other_t->lifepoints || other_t == t)
218 if (target.y == other_t->pos.y && target.x == other_t->pos.x)
220 actor_hits_actor(t, other_t);
227 set_thing_position(t, target);
230 if (t == get_player())
232 playerbonus_move(d, passable);
238 extern void actor_drop(struct Thing * t)
240 uint8_t owns_none = (!t->owns);
243 uint8_t select = t->arg;
244 struct Thing * owned = t->owns;
246 for (; i != select; i++, owned = owned->next);
247 own_thing(&world.things, &t->owns, owned->id);
249 if (t == get_player())
251 playerbonus_drop(owns_none);
257 extern void actor_pick(struct Thing * t)
259 struct Thing * picked = NULL;
261 for (t_i = world.things; t_i; t_i = t_i->next)
263 if (t_i != t && t_i->pos.y == t->pos.y && t_i->pos.x == t->pos.x)
270 own_thing(&t->owns, &world.things, picked->id);
271 set_thing_position(picked, t->pos);
273 if (t == get_player())
275 playerbonus_pick(!(!picked));
281 extern void actor_use(struct Thing * t)
283 uint8_t wrong_thing = 1;
284 uint8_t no_thing = (!t->owns);
287 uint8_t select = t->arg;
289 struct Thing * selected = t->owns;
290 for (; i != select; i++, selected = selected->next);
291 struct ThingType * tt = get_thing_type(selected->type);
295 struct Thing * next = selected->next;
301 for (i = 0; i != select; i++, selected = selected->next);
302 selected->next = next;
308 t->lifepoints = t->lifepoints + tt->consumable;
311 if (t == get_player())
313 playerbonus_use(no_thing, wrong_thing);