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(), ungetc() */
7 #include <stdlib.h> /* free(), atoi() */
8 #include <string.h> /* strlen(), strcmp(), memcpy(), strncmp() */
9 #include "../common/err_try_fgets.h" /* err_try_fgets() */
10 #include "../common/rexit.h" /* exit_err() */
11 #include "../common/try_malloc.h" /* try_malloc() */
12 #include "../common/yx_uint8.h" /* struct yx_uint8 */
13 #include "io.h" /* struct EntrySkeleton */
14 #include "map_objects.h" /* structs MapObj, MapObjDef, get_player(),
15 * set_object_position(), own_map_object(),
16 * get_map_object_def()
18 #include "map.h" /* is_passable() */
19 #include "yx_uint8.h" /* mv_yx_in_dir(), yx_uint8_cmp() */
20 #include "world.h" /* global world */
24 /* Append "text" to game log, or a "." if "text" is the same as the last one. */
25 static void update_log(char * text);
27 /* If "name" fits "moa"->name, set "moa"->func to "func". */
28 static uint8_t try_func_name(struct MapObjAct * moa,
29 char * name, void (* func) (struct MapObj *));
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 MapObj * hitter, struct MapObj * 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_object, uint8_t wrong_object);
49 static void update_log(char * text)
51 char * f_name = "update_log()";
52 uint16_t len_new = strlen(text);
56 len_old = strlen(world.log);
57 uint16_t last_nl = len_old - 1;
60 if ('\n' == world.log[last_nl])
66 uint16_t last_stop = len_old - 1;
67 while (last_stop != 0)
69 if ('.' == world.log[last_stop] && '.' != world.log[last_stop - 1])
75 if ( (last_stop + 1) - last_nl == (uint16_t) strlen(text)
76 && 0 == strncmp(world.log + last_nl, text, strlen(text)))
81 uint16_t len_whole = len_old + len_new + 1;
82 char * new_text = try_malloc(len_whole, f_name);
83 memcpy(new_text, world.log, len_old);
84 sprintf(new_text + len_old, "%s", text);
91 static uint8_t try_func_name(struct MapObjAct * moa,
92 char * name, void (* func) (struct MapObj *))
94 if (0 == strcmp(moa->name, name))
104 static void actor_hits_actor(struct MapObj * hitter, struct MapObj * hitted)
106 struct MapObjDef * mod_hitter = get_map_object_def(hitter->type);
107 struct MapObjDef * mod_hitted = get_map_object_def(hitted->type);
108 struct MapObj * player = get_player();
110 char * msg2 = "wound";
112 if (player != hitter)
114 msg1 = mod_hitter->name;
117 if (player != hitted)
119 msg3 = mod_hitted->name;
121 uint8_t len = 1 + strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2;
123 sprintf(msg, "\n%s %s %s.", msg1, msg2, msg3);
125 hitted->lifepoints--;
126 if (0 == hitted->lifepoints)
128 hitted->type = mod_hitted->corpse_id;
129 if (player == hitted)
131 update_log(" You die.");
134 update_log(" It dies.");
140 static void playerbonus_wait()
142 update_log("\nYou wait.");
147 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match)
159 static void playerbonus_move(char d, uint8_t passable)
161 char * dsc_dir = "north";
162 if ( match_dir(d, &dsc_dir, '6', "east")
163 || match_dir(d, &dsc_dir, '2', "south")
164 || match_dir(d, &dsc_dir, '4', "west")
165 || match_dir(d, &dsc_dir, '7', "north-west")
166 || match_dir(d, &dsc_dir, '9', "north-east")
167 || match_dir(d, &dsc_dir, '1', "south-west")
168 || match_dir(d, &dsc_dir, '3', "south-east"))
172 char * dsc_move = "You move ";
175 dsc_move = "You fail to move ";
177 char msg[strlen(dsc_move) + strlen (dsc_dir) + 3];
178 sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
184 static void playerbonus_drop(uint8_t owns_none)
188 update_log("\nYou try to drop an object, but you own none.");
191 update_log("\nYou drop an object.");
196 static void playerbonus_pick(uint8_t picked)
200 update_log("\nYou pick up an object.");
203 update_log("\nYou try to pick up an object, but there is none.");
208 static void playerbonus_use(uint8_t no_object, uint8_t wrong_object)
212 update_log("\nYou try to use an object, but you own none.");
215 else if (wrong_object)
217 update_log("\nYou try to use this object, but fail.");
220 update_log("\nYou consume MAGIC MEAT.");
225 extern void read_map_object_action(char * line, uint32_t linemax,char * context,
226 struct EntrySkeleton * entry, FILE * file)
228 char * f_name = "init_map_object_actions()";
229 struct MapObjAct * moa = (struct MapObjAct *) entry;
230 err_try_fgets(line, linemax, file, context, "0nfi8");
231 moa->effort = atoi(line);
232 err_try_fgets(line, linemax, file, context, "0nf");
233 line[strlen(line) - 1] = '\0';
234 uint8_t len_name = strlen(line) + 1;
235 moa->name = try_malloc(len_name, f_name);
236 memcpy(moa->name, line, len_name);
237 if (!( try_func_name(moa, "move", actor_move)
238 || try_func_name(moa, "pick_up", actor_pick)
239 || try_func_name(moa, "drop", actor_drop)
240 || try_func_name(moa, "use", actor_use)))
242 moa->func = actor_wait;
248 extern void free_map_object_actions(struct MapObjAct * moa)
255 free_map_object_actions(moa->next);
261 extern uint8_t get_moa_id_by_name(char * name)
263 struct MapObjAct * moa = world.map_obj_acts;
266 if (0 == strcmp(moa->name, name))
272 exit_err(NULL==moa, "get_moa_id_by_name() did not find map object action.");
278 extern void actor_wait(struct MapObj * mo)
280 if (mo == get_player())
288 extern void actor_move(struct MapObj * mo)
291 struct yx_uint8 target = mv_yx_in_dir(d, mo->pos);
292 struct MapObj * other_mo;
293 for (other_mo = world.map_objs; other_mo != 0; other_mo = other_mo->next)
295 if (0 == other_mo->lifepoints || other_mo == mo)
299 if (yx_uint8_cmp(&target, &other_mo->pos))
301 actor_hits_actor(mo, other_mo);
305 uint8_t passable = is_passable(target);
308 set_object_position(mo, target);
310 if (mo == get_player())
312 playerbonus_move(d, passable);
318 extern void actor_drop(struct MapObj * mo)
320 uint8_t owns_none = (NULL == mo->owns);
323 uint8_t select = mo->arg;
324 struct MapObj * owned = mo->owns;
326 for (; i != select; i++, owned = owned->next);
327 own_map_object(&world.map_objs, &mo->owns, owned->id);
329 if (mo == get_player())
331 playerbonus_drop(owns_none);
337 extern void actor_pick(struct MapObj * mo)
339 struct MapObj * picked = NULL;
340 struct MapObj * mo_i;
341 for (mo_i = world.map_objs; NULL != mo_i; mo_i = mo_i->next)
343 if (mo_i != mo && yx_uint8_cmp(&mo_i->pos, &mo->pos))
350 own_map_object(&mo->owns, &world.map_objs, picked->id);
351 set_object_position(picked, mo->pos);
353 if (mo == get_player())
355 playerbonus_pick(NULL != picked);
361 extern void actor_use(struct MapObj * mo)
363 uint8_t wrong_object = 1;
364 uint8_t no_object = (NULL == mo->owns);
367 uint8_t select = mo->arg;
369 struct MapObj * selected = mo->owns;
370 for (; i != select; i++, selected = selected->next);
371 struct MapObjDef * mod = get_map_object_def(selected->type);
375 struct MapObj * next = selected->next;
381 for (i = 0; i != select; i++, selected = selected->next);
382 selected->next = next;
388 mo->lifepoints = mo->lifepoints + mod->consumable;
391 if (mo == get_player())
393 playerbonus_use(no_object, wrong_object);