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 "map_objects.h" /* structs MapObj, MapObjDef, get_player(),
13 * set_object_position(), own_map_object(),
14 * get_map_object_def()
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 /* If "name" fits "moa"->name, set "moa"->func to "func". */
26 //static uint8_t try_func_name(struct MapObjAct * moa,
27 // char * name, void (* func) (struct MapObj *));
29 /* One actor "wounds" another actor, decrementing his lifepoints and, if they
30 * reach zero in the process, killing it. Generates appropriate log message.
32 static void actor_hits_actor(struct MapObj * hitter, struct MapObj * hitted);
34 /* Bonus stuff to actor_*() to happen if actor==player. Mostly writing of log
35 * messages; _pick and _drop also decrement world.inventory_sel by 1 if >0.
36 * (match_dir() is just a little helper to playerbonus_move().)
38 static void playerbonus_wait();
39 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match);
40 static void playerbonus_move(char d, uint8_t passable);
41 static void playerbonus_drop(uint8_t owns_none);
42 static void playerbonus_pick(uint8_t picked);
43 static void playerbonus_use(uint8_t no_object, uint8_t wrong_object);
47 static void update_log(char * text)
49 char * f_name = "update_log()";
50 uint16_t len_new = strlen(text);
54 len_old = strlen(world.log);
55 uint16_t last_nl = len_old - 1;
58 if ('\n' == world.log[last_nl])
64 uint16_t last_stop = len_old - 1;
65 while (last_stop != 0)
67 if ('.' == world.log[last_stop] && '.' != world.log[last_stop - 1])
73 if ( (last_stop + 1) - last_nl == (uint16_t) strlen(text)
74 && 0 == strncmp(world.log + last_nl, text, strlen(text)))
79 uint16_t len_whole = len_old + len_new + 1;
80 char * new_text = try_malloc(len_whole, f_name);
81 memcpy(new_text, world.log, len_old);
82 sprintf(new_text + len_old, "%s", text);
89 static void actor_hits_actor(struct MapObj * hitter, struct MapObj * hitted)
91 struct MapObjDef * mod_hitter = get_map_object_def(hitter->type);
92 struct MapObjDef * mod_hitted = get_map_object_def(hitted->type);
93 struct MapObj * player = get_player();
95 char * msg2 = "wound";
99 msg1 = mod_hitter->name;
102 if (player != hitted)
104 msg3 = mod_hitted->name;
106 uint8_t len = 1 + strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2;
108 sprintf(msg, "\n%s %s %s.", msg1, msg2, msg3);
110 hitted->lifepoints--;
111 if (0 == hitted->lifepoints)
113 hitted->type = mod_hitted->corpse_id;
114 if (player == hitted)
116 update_log(" You die.");
119 update_log(" It dies.");
125 static void playerbonus_wait()
127 update_log("\nYou wait.");
132 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match)
144 static void playerbonus_move(char d, uint8_t passable)
146 char * dsc_dir = "north";
147 if ( match_dir(d, &dsc_dir, '6', "east")
148 || match_dir(d, &dsc_dir, '2', "south")
149 || match_dir(d, &dsc_dir, '4', "west")
150 || match_dir(d, &dsc_dir, '7', "north-west")
151 || match_dir(d, &dsc_dir, '9', "north-east")
152 || match_dir(d, &dsc_dir, '1', "south-west")
153 || match_dir(d, &dsc_dir, '3', "south-east"))
157 char * dsc_move = "You move ";
160 dsc_move = "You fail to move ";
162 char msg[strlen(dsc_move) + strlen (dsc_dir) + 3];
163 sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
169 static void playerbonus_drop(uint8_t owns_none)
173 update_log("\nYou try to drop an object, but you own none.");
176 update_log("\nYou drop an object.");
181 static void playerbonus_pick(uint8_t picked)
185 update_log("\nYou pick up an object.");
188 update_log("\nYou try to pick up an object, but there is none.");
193 static void playerbonus_use(uint8_t no_object, uint8_t wrong_object)
197 update_log("\nYou try to use an object, but you own none.");
200 else if (wrong_object)
202 update_log("\nYou try to use this object, but fail.");
205 update_log("\nYou consume MAGIC MEAT.");
210 extern void free_map_object_actions(struct MapObjAct * moa)
217 free_map_object_actions(moa->next);
223 extern uint8_t get_moa_id_by_name(char * name)
225 struct MapObjAct * moa = world.map_obj_acts;
228 if (0 == strcmp(moa->name, name))
234 exit_err(NULL==moa, "get_moa_id_by_name() did not find map object action.");
240 extern void actor_wait(struct MapObj * mo)
242 if (mo == get_player())
250 extern void actor_move(struct MapObj * mo)
253 struct yx_uint8 target = mv_yx_in_dir(d, mo->pos);
254 struct MapObj * other_mo;
255 for (other_mo = world.map_objs; other_mo != 0; other_mo = other_mo->next)
257 if (0 == other_mo->lifepoints || other_mo == mo)
261 if (yx_uint8_cmp(&target, &other_mo->pos))
263 actor_hits_actor(mo, other_mo);
267 uint8_t passable = is_passable(target);
270 set_object_position(mo, target);
272 if (mo == get_player())
274 playerbonus_move(d, passable);
280 extern void actor_drop(struct MapObj * mo)
282 uint8_t owns_none = (NULL == mo->owns);
285 uint8_t select = mo->arg;
286 struct MapObj * owned = mo->owns;
288 for (; i != select; i++, owned = owned->next);
289 own_map_object(&world.map_objs, &mo->owns, owned->id);
291 if (mo == get_player())
293 playerbonus_drop(owns_none);
299 extern void actor_pick(struct MapObj * mo)
301 struct MapObj * picked = NULL;
302 struct MapObj * mo_i;
303 for (mo_i = world.map_objs; NULL != mo_i; mo_i = mo_i->next)
305 if (mo_i != mo && yx_uint8_cmp(&mo_i->pos, &mo->pos))
312 own_map_object(&mo->owns, &world.map_objs, picked->id);
313 set_object_position(picked, mo->pos);
315 if (mo == get_player())
317 playerbonus_pick(NULL != picked);
323 extern void actor_use(struct MapObj * mo)
325 uint8_t wrong_object = 1;
326 uint8_t no_object = (NULL == mo->owns);
329 uint8_t select = mo->arg;
331 struct MapObj * selected = mo->owns;
332 for (; i != select; i++, selected = selected->next);
333 struct MapObjDef * mod = get_map_object_def(selected->type);
337 struct MapObj * next = selected->next;
343 for (i = 0; i != select; i++, selected = selected->next);
344 selected->next = next;
350 mo->lifepoints = mo->lifepoints + mod->consumable;
353 if (mo == get_player())
355 playerbonus_use(no_object, wrong_object);