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 /* 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 MapObj * hitter, struct MapObj * 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_object, uint8_t wrong_object);
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 MapObj * hitter, struct MapObj * hitted)
87 struct MapObjDef * mod_hitter = get_map_object_def(hitter->type);
88 struct MapObjDef * mod_hitted = get_map_object_def(hitted->type);
89 struct MapObj * player = get_player();
91 char * msg2 = "wound";
95 msg1 = mod_hitter->name;
100 msg3 = mod_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 = mod_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";
143 if ( match_dir(d, &dsc_dir, '6', "east")
144 || match_dir(d, &dsc_dir, '2', "south")
145 || match_dir(d, &dsc_dir, '4', "west")
146 || match_dir(d, &dsc_dir, '7', "north-west")
147 || match_dir(d, &dsc_dir, '9', "north-east")
148 || match_dir(d, &dsc_dir, '1', "south-west")
149 || match_dir(d, &dsc_dir, '3', "south-east"))
153 char * dsc_move = "You move ";
156 dsc_move = "You fail to move ";
158 char msg[strlen(dsc_move) + strlen (dsc_dir) + 3];
159 sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
165 static void playerbonus_drop(uint8_t owns_none)
169 update_log("\nYou try to drop an object, but you own none.");
172 update_log("\nYou drop an object.");
177 static void playerbonus_pick(uint8_t picked)
181 update_log("\nYou pick up an object.");
184 update_log("\nYou try to pick up an object, but there is none.");
189 static void playerbonus_use(uint8_t no_object, uint8_t wrong_object)
193 update_log("\nYou try to use an object, but you own none.");
196 else if (wrong_object)
198 update_log("\nYou try to use this object, but fail.");
201 update_log("\nYou consume MAGIC MEAT.");
206 extern void free_map_object_actions(struct MapObjAct * moa)
213 free_map_object_actions(moa->next);
219 extern uint8_t get_moa_id_by_name(char * name)
221 struct MapObjAct * moa = world.map_obj_acts;
224 if (0 == strcmp(moa->name, name))
230 exit_err(NULL==moa, "get_moa_id_by_name() did not find map object action.");
236 extern void actor_wait(struct MapObj * mo)
238 if (mo == get_player())
246 extern void actor_move(struct MapObj * mo)
249 struct yx_uint8 target = mv_yx_in_dir(d, mo->pos);
250 struct MapObj * other_mo;
251 for (other_mo = world.map_objs; other_mo != 0; other_mo = other_mo->next)
253 if (0 == other_mo->lifepoints || other_mo == mo)
257 if (yx_uint8_cmp(&target, &other_mo->pos))
259 actor_hits_actor(mo, other_mo);
263 uint8_t passable = is_passable(target);
266 set_object_position(mo, target);
268 if (mo == get_player())
270 playerbonus_move(d, passable);
276 extern void actor_drop(struct MapObj * mo)
278 uint8_t owns_none = (NULL == mo->owns);
281 uint8_t select = mo->arg;
282 struct MapObj * owned = mo->owns;
284 for (; i != select; i++, owned = owned->next);
285 own_map_object(&world.map_objs, &mo->owns, owned->id);
287 if (mo == get_player())
289 playerbonus_drop(owns_none);
295 extern void actor_pick(struct MapObj * mo)
297 struct MapObj * picked = NULL;
298 struct MapObj * mo_i;
299 for (mo_i = world.map_objs; NULL != mo_i; mo_i = mo_i->next)
301 if (mo_i != mo && yx_uint8_cmp(&mo_i->pos, &mo->pos))
308 own_map_object(&mo->owns, &world.map_objs, picked->id);
309 set_object_position(picked, mo->pos);
311 if (mo == get_player())
313 playerbonus_pick(NULL != picked);
319 extern void actor_use(struct MapObj * mo)
321 uint8_t wrong_object = 1;
322 uint8_t no_object = (NULL == mo->owns);
325 uint8_t select = mo->arg;
327 struct MapObj * selected = mo->owns;
328 for (; i != select; i++, selected = selected->next);
329 struct MapObjDef * mod = get_map_object_def(selected->type);
333 struct MapObj * next = selected->next;
339 for (i = 0; i != select; i++, selected = selected->next);
340 selected->next = next;
346 mo->lifepoints = mo->lifepoints + mod->consumable;
349 if (mo == get_player())
351 playerbonus_use(no_object, wrong_object);