1 /* src/server/map_object_actions.c */
3 #include "map_object_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 "map_objects.h" /* structs MapObj, MapObjDef, get_player(),
14 * set_object_position(), own_map_object(),
15 * get_map_object_def()
17 #include "map.h" /* is_passable() */
18 #include "yx_uint8.h" /* mv_yx_in_dir(), yx_uint8_cmp() */
19 #include "world.h" /* global world */
23 /* Append "text" to game log, or a "." if "text" is the same as the last one. */
24 static void update_log(char * text);
26 /* One actor "wounds" another actor, decrementing his lifepoints and, if they
27 * reach zero in the process, killing it. Generates appropriate log message.
29 static void actor_hits_actor(struct MapObj * hitter, struct MapObj * hitted);
31 /* Bonus stuff to actor_*() to happen if actor==player. Mostly writing of log
32 * messages; _pick and _drop also decrement world.inventory_sel by 1 if >0.
33 * (match_dir() is just a little helper to playerbonus_move().)
35 static void playerbonus_wait();
36 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match);
37 static void playerbonus_move(char d, uint8_t passable);
38 static void playerbonus_drop(uint8_t owns_none);
39 static void playerbonus_pick(uint8_t picked);
40 static void playerbonus_use(uint8_t no_object, uint8_t wrong_object);
44 static void update_log(char * text)
46 char * f_name = "update_log()";
47 uint16_t len_new = strlen(text);
51 len_old = strlen(world.log);
52 uint16_t last_nl = len_old - 1;
55 if ('\n' == world.log[last_nl])
61 uint16_t last_stop = len_old - 1;
62 while (last_stop != 0)
64 if ('.' == world.log[last_stop] && '.' != world.log[last_stop - 1])
70 if ( (last_stop + 1) - last_nl == (uint16_t) strlen(text)
71 && 0 == strncmp(world.log + last_nl, text, strlen(text)))
76 uint16_t len_whole = len_old + len_new + 1;
77 char * new_text = try_malloc(len_whole, f_name);
78 memcpy(new_text, world.log, len_old);
79 sprintf(new_text + len_old, "%s", text);
86 static void actor_hits_actor(struct MapObj * hitter, struct MapObj * hitted)
88 struct MapObjDef * mod_hitter = get_map_object_def(hitter->type);
89 struct MapObjDef * mod_hitted = get_map_object_def(hitted->type);
90 struct MapObj * player = get_player();
92 char * msg2 = "wound";
96 msg1 = mod_hitter->name;
101 msg3 = mod_hitted->name;
103 uint8_t len = 1 + strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2;
105 sprintf(msg, "\n%s %s %s.", msg1, msg2, msg3);
107 hitted->lifepoints--;
108 if (0 == hitted->lifepoints)
110 hitted->type = mod_hitted->corpse_id;
111 if (player == hitted)
113 update_log(" You die.");
116 update_log(" It dies.");
122 static void playerbonus_wait()
124 update_log("\nYou wait.");
129 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match)
141 static void playerbonus_move(char d, uint8_t passable)
143 char * dsc_dir = "north-east";
144 if ( match_dir(d, &dsc_dir, 'd', "east")
145 || match_dir(d, &dsc_dir, 'c', "south-east")
146 || match_dir(d, &dsc_dir, 'x', "south-west")
147 || match_dir(d, &dsc_dir, 's', "west")
148 || match_dir(d, &dsc_dir, 'w', "north-west"))
152 char * dsc_move = "You move ";
155 dsc_move = "You fail to move ";
157 char msg[strlen(dsc_move) + strlen (dsc_dir) + 3];
158 sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
164 static void playerbonus_drop(uint8_t owns_none)
168 update_log("\nYou try to drop an object, but you own none.");
171 update_log("\nYou drop an object.");
176 static void playerbonus_pick(uint8_t picked)
180 update_log("\nYou pick up an object.");
183 update_log("\nYou try to pick up an object, but there is none.");
188 static void playerbonus_use(uint8_t no_object, uint8_t wrong_object)
192 update_log("\nYou try to use an object, but you own none.");
195 else if (wrong_object)
197 update_log("\nYou try to use this object, but fail.");
200 update_log("\nYou consume MAGIC MEAT.");
205 extern void free_map_object_actions(struct MapObjAct * moa)
212 free_map_object_actions(moa->next);
218 extern uint8_t get_moa_id_by_name(char * name)
220 struct MapObjAct * moa = world.map_obj_acts;
223 if (0 == strcmp(moa->name, name))
229 exit_err(NULL==moa, "get_moa_id_by_name() did not find map object action.");
235 extern void actor_wait(struct MapObj * mo)
237 if (mo == get_player())
245 extern void actor_move(struct MapObj * mo)
248 struct yx_uint8 target = mv_yx_in_dir(d, mo->pos);
249 struct MapObj * other_mo;
250 for (other_mo = world.map_objs; other_mo != 0; other_mo = other_mo->next)
252 if (0 == other_mo->lifepoints || other_mo == mo)
256 if (yx_uint8_cmp(&target, &other_mo->pos))
258 actor_hits_actor(mo, other_mo);
262 uint8_t passable = is_passable(target);
265 set_object_position(mo, target);
267 mo->fov_map = build_fov_map(mo);
269 if (mo == get_player())
271 playerbonus_move(d, passable);
277 extern void actor_drop(struct MapObj * mo)
279 uint8_t owns_none = (NULL == mo->owns);
282 uint8_t select = mo->arg;
283 struct MapObj * owned = mo->owns;
285 for (; i != select; i++, owned = owned->next);
286 own_map_object(&world.map_objs, &mo->owns, owned->id);
288 if (mo == get_player())
290 playerbonus_drop(owns_none);
296 extern void actor_pick(struct MapObj * mo)
298 struct MapObj * picked = NULL;
299 struct MapObj * mo_i;
300 for (mo_i = world.map_objs; NULL != mo_i; mo_i = mo_i->next)
302 if (mo_i != mo && yx_uint8_cmp(&mo_i->pos, &mo->pos))
309 own_map_object(&mo->owns, &world.map_objs, picked->id);
310 set_object_position(picked, mo->pos);
312 if (mo == get_player())
314 playerbonus_pick(NULL != picked);
320 extern void actor_use(struct MapObj * mo)
322 uint8_t wrong_object = 1;
323 uint8_t no_object = (NULL == mo->owns);
326 uint8_t select = mo->arg;
328 struct MapObj * selected = mo->owns;
329 for (; i != select; i++, selected = selected->next);
330 struct MapObjDef * mod = get_map_object_def(selected->type);
334 struct MapObj * next = selected->next;
340 for (i = 0; i != select; i++, selected = selected->next);
341 selected->next = next;
347 mo->lifepoints = mo->lifepoints + mod->consumable;
350 if (mo == get_player())
352 playerbonus_use(no_object, wrong_object);