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, uint16_t */
11 #include <stdio.h> /* sprintf() */
12 #include <stdlib.h> /* free() */
13 #include <string.h> /* strlen(), memcpy(), strncmp() */
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(), own_thing(),
20 * set_thing_position(), get_thing_type(),
21 * free_things_in_memory()
23 #include "map.h" /* mv_yx_in_dir_legal() */
24 #include "world.h" /* global world */
28 /* How many previous characters of the game log to keep on adding new text */
29 #define MAX_BACKLOG_CHARS 3000
33 /* If "text" is equal "log"'s last line, return 1, else 0. */
34 static uint8_t text_equals_log_end(char * log, char * text);
36 /* Append "text" to game log shortened to MAX_BACKLOG_CHARS characters, or
37 * continuation period if "text" is the same as the (shortened) log's last line
38 * minus continuation periods.
40 static void update_log(char * text);
42 /* One actor "wounds" another actor, decrementing his lifepoints and, if they
43 * reach zero in the process, killing it. Generates appropriate log message.
45 static void actor_hits_actor(struct Thing * hitter, struct Thing * hitted);
47 /* Bonus stuff to actor_*() to happen if actor==player. Mostly writing of log
48 * messages; _pick and _drop also decrement world.inventory_sel by 1 if >0.
49 * (match_dir() is just a little helper to playerbonus_move().)
51 static void playerbonus_wait();
52 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match);
53 static void playerbonus_move(char d, uint8_t passable);
54 static void playerbonus_drop(uint8_t owns_none);
55 static void playerbonus_pick(uint8_t picked);
56 static void playerbonus_use(uint8_t no_thing, uint8_t wrong_thing);
60 static uint8_t text_equals_log_end(char * log, char * text)
62 uint16_t len_old = strlen(log);
63 uint16_t last_nl = len_old - 1;
66 if ('\n' == log[last_nl])
72 uint16_t last_stop = len_old - 1;
73 while (last_stop != 0)
75 if ('.' == log[last_stop] && '.' != log[last_stop - 1])
81 if ( (last_stop + 1) - last_nl == (uint16_t) strlen(text)
82 && 0 == strncmp(log + last_nl, text, strlen(text)))
91 static void update_log(char * text)
93 uint16_t len_new = strlen(text);
98 len_old = strlen(world.log);
99 if (len_old > MAX_BACKLOG_CHARS)
101 offset = len_old - MAX_BACKLOG_CHARS;
102 len_old = MAX_BACKLOG_CHARS;
104 if (text_equals_log_end(world.log + offset, text))
109 uint16_t len_whole = len_old + len_new + 1;
110 char * new_text = try_malloc(len_whole, __func__);
111 memcpy(new_text, world.log + offset, len_old);
112 int test = sprintf(new_text + len_old, "%s", text);
113 exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]);
115 world.log = new_text;
120 static void actor_hits_actor(struct Thing * hitter, struct Thing * hitted)
122 struct ThingType * tt_hitter = get_thing_type(hitter->type);
123 struct ThingType * tt_hitted = get_thing_type(hitted->type);
124 struct Thing * player = get_player();
126 char * msg2 = "wound";
128 if (player != hitter)
130 msg1 = tt_hitter->name;
133 if (player != hitted)
135 msg3 = tt_hitted->name;
137 uint8_t len = 1 + strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2;
138 char * msg = try_malloc(len, __func__);
139 int test = sprintf(msg, "\n%s %s %s.", msg1, msg2, msg3);
140 exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]);
143 hitted->lifepoints--;
144 if (0 == hitted->lifepoints)
146 hitted->type = tt_hitted->corpse_id;
147 if (player == hitted)
149 update_log(" You die.");
150 memset(hitted->fov_map, ' ', world.map.length * world.map.length);
155 free(hitted->fov_map);
156 hitted->fov_map = NULL;
157 free(hitted->mem_map);
158 hitted->mem_map = NULL;
159 free_things_in_memory(hitted->t_mem);
160 hitted->t_mem = NULL;
162 update_log(" It dies.");
168 static void playerbonus_wait()
170 update_log("\nYou wait.");
175 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match)
187 static void playerbonus_move(char d, uint8_t passable)
189 char * dsc_dir = "north-east";
190 if ( match_dir(d, &dsc_dir, 'd', "east")
191 || match_dir(d, &dsc_dir, 'c', "south-east")
192 || match_dir(d, &dsc_dir, 'x', "south-west")
193 || match_dir(d, &dsc_dir, 's', "west")
194 || match_dir(d, &dsc_dir, 'w', "north-west"))
198 char * dsc_move = "You move ";
201 dsc_move = "You fail to move ";
203 char * msg = try_malloc(strlen(dsc_move) + strlen (dsc_dir) + 3, __func__);
204 int test = sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
205 exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]);
212 static void playerbonus_drop(uint8_t owns_none)
216 update_log("\nYou try to drop an object, but you own none.");
219 update_log("\nYou drop an object.");
224 static void playerbonus_pick(uint8_t picked)
228 update_log("\nYou pick up an object.");
231 update_log("\nYou try to pick up an object, but there is none.");
236 static void playerbonus_use(uint8_t no_thing, uint8_t wrong_thing)
240 update_log("\nYou try to use an object, but you own none.");
243 else if (wrong_thing)
245 update_log("\nYou try to use this object, but fail.");
248 update_log("\nYou consume MAGIC MEAT.");
253 extern void actor_wait(struct Thing * t)
255 if (t == get_player())
263 extern void actor_move(struct Thing * t)
266 struct Thing * other_t;
267 struct yx_uint8 target = t->pos;
268 uint8_t legal_move = mv_yx_in_dir_legal(d, &target);
269 mv_yx_in_dir_legal(0, NULL);
270 uint8_t passable = 0;
273 passable = '.' == world.map.cells[target.y*world.map.length + target.x];
274 for (other_t = world.things; other_t != 0; other_t = other_t->next)
276 if (0 == other_t->lifepoints || other_t == t)
280 if (target.y == other_t->pos.y && target.x == other_t->pos.x)
282 actor_hits_actor(t, other_t);
289 set_thing_position(t, target);
292 if (t == get_player())
294 playerbonus_move(d, passable);
300 extern void actor_drop(struct Thing * t)
302 uint8_t owns_none = (!t->owns);
305 uint8_t select = t->arg;
306 struct Thing * owned = t->owns;
308 for (; i != select; i++, owned = owned->next);
309 own_thing(&world.things, &t->owns, owned->id);
311 if (t == get_player())
313 playerbonus_drop(owns_none);
319 extern void actor_pick(struct Thing * t)
321 struct Thing * picked = NULL;
323 for (t_i = world.things; t_i; t_i = t_i->next)
325 if (t_i != t && t_i->pos.y == t->pos.y && t_i->pos.x == t->pos.x)
332 own_thing(&t->owns, &world.things, picked->id);
333 set_thing_position(picked, t->pos);
335 if (t == get_player())
337 playerbonus_pick(!(!picked));
343 extern void actor_use(struct Thing * t)
345 uint8_t wrong_thing = 1;
346 uint8_t no_thing = (!t->owns);
349 uint8_t select = t->arg;
351 struct Thing * selected = t->owns;
352 for (; i != select; i++, selected = selected->next);
353 struct ThingType * tt = get_thing_type(selected->type);
357 struct Thing * next = selected->next;
363 for (i = 0; i != select; i++, selected = selected->next);
364 selected->next = next;
370 t->lifepoints = t->lifepoints + tt->consumable;
373 if (t == get_player())
375 playerbonus_use(no_thing, wrong_thing);