1 /* src/server/map_object_actions.c */
3 #include "map_object_actions.h"
4 #include <stdint.h> /* uint8_t, uint16_t */
5 #include <stdio.h> /* sprintf() */
6 #include <stdlib.h> /* free(), atoi() */
7 #include <string.h> /* strlen(), strcmp(), memcpy(), strtok(), strncmp() */
8 #include "../common/readwrite.h" /* textfile_sizes(), try_fopen(), try_fclose(),
11 #include "../common/rexit.h" /* exit_err() */
12 #include "../common/try_malloc.h" /* try_malloc() */
13 #include "../common/yx_uint16.h" /* yx_uint16 struct */
14 #include "cleanup.h" /* set_cleanup_flag() */
15 #include "map_objects.h" /* structs MapObj, MapObjDef, get_player(),
16 * set_object_position(), own_map_object(),
17 * get_map_object_def()
19 #include "map.h" /* is_passable() */
20 #include "yx_uint16.h" /* mv_yx_in_dir(), yx_uint16_cmp() */
21 #include "world.h" /* global world */
25 /* Append "text" to game log, or a "." if "text" is the same as the last one. */
26 static void update_log(char * text);
28 /* If "name" fits "moa"->name, set "moa"->func to "func". */
29 static uint8_t try_func_name(struct MapObjAct * moa,
30 char * name, void (* func) (struct MapObj *));
32 /* One actor "wounds" another actor, decrementing his lifepoints and, if they
33 * reach zero in the process, killing it. Generates appropriate log message.
35 static void actor_hits_actor(struct MapObj * hitter, struct MapObj * hitted);
37 /* Bonus stuff to actor_*() to happen if actor==player. Mostly writing of log
38 * messages; _pick and _drop also decrement world.inventory_sel by 1 if >0.
40 static void playerbonus_wait();
41 static void playerbonus_move(char d, uint8_t passable);
42 static void playerbonus_drop(uint8_t owns_none);
43 static void playerbonus_pick(uint8_t picked);
44 static void playerbonus_use(uint8_t no_object, uint8_t wrong_object);
48 static void update_log(char * text)
50 char * f_name = "update_log()";
51 uint16_t len_new = strlen(text);
55 len_old = strlen(world.log);
56 uint16_t last_nl = len_old - 1;
59 if ('\n' == world.log[last_nl])
65 uint16_t last_stop = len_old - 1;
66 while (last_stop != 0)
68 if ('.' == world.log[last_stop] && '.' != world.log[last_stop - 1])
74 if ( (last_stop + 1) - last_nl == strlen(text)
75 && 0 == strncmp(world.log + last_nl, text, strlen(text)))
80 uint16_t len_whole = len_old + len_new + 1;
81 char * new_text = try_malloc(len_whole, f_name);
82 memcpy(new_text, world.log, len_old);
83 sprintf(new_text + len_old, "%s", text);
90 static uint8_t try_func_name(struct MapObjAct * moa,
91 char * name, void (* func) (struct MapObj *))
93 if (0 == strcmp(moa->name, name))
103 static void actor_hits_actor(struct MapObj * hitter, struct MapObj * hitted)
105 struct MapObjDef * mod_hitter = get_map_object_def(hitter->type);
106 struct MapObjDef * mod_hitted = get_map_object_def(hitted->type);
107 struct MapObj * player = get_player();
109 char * msg2 = "wound";
111 if (player != hitter)
113 msg1 = mod_hitter->name;
116 if (player != hitted)
118 msg3 = mod_hitted->name;
120 uint8_t len = 1 + strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2;
122 sprintf(msg, "\n%s %s %s.", msg1, msg2, msg3);
124 hitted->lifepoints--;
125 if (0 == hitted->lifepoints)
127 hitted->type = mod_hitted->corpse_id;
128 if (player == hitted)
130 update_log(" You die.");
133 update_log(" It dies.");
134 if (player == hitter)
136 world.score = world.score + mod_hitted->lifepoints;
143 static void playerbonus_wait()
145 update_log("\nYou wait.");
150 static void playerbonus_move(char d, uint8_t passable)
152 char * dsc_dir = "north";
165 char * dsc_move = "You move ";
168 dsc_move = "You fail to move ";
170 char msg[strlen(dsc_move) + strlen (dsc_dir) + 3];
171 sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
177 static void playerbonus_drop(uint8_t owns_none)
181 update_log("\nYou try to drop an object, but you own none.");
184 update_log("\nYou drop an object.");
189 static void playerbonus_pick(uint8_t picked)
193 update_log("\nYou pick up an object.");
196 update_log("\nYou try to pick up an object, but there is none.");
201 static void playerbonus_use(uint8_t no_object, uint8_t wrong_object)
205 update_log("\nYou try to use an object, but you own none.");
208 else if (wrong_object)
210 update_log("\nYou try to use this object, but fail.");
213 update_log("\nYou consume MAGIC MEAT.");
218 extern void init_map_object_actions(char * path)
220 char * f_name = "init_map_object_actions()";
221 FILE * file = try_fopen(path, "r", f_name);
222 uint16_t linemax = textfile_sizes(file, NULL);
223 char line[linemax + 1];
224 struct MapObjAct ** moa_ptr_ptr = &world.map_obj_acts;
226 while (try_fgets(line, linemax + 1, file, f_name))
228 if ('\n' == line[0] || 0 == line[0])
232 struct MapObjAct * moa = try_malloc(sizeof(struct MapObjAct), f_name);
233 moa->id = atoi(strtok(line, delim));
234 moa->effort = atoi(strtok(NULL, delim));
235 char * funcname = strtok(NULL, "\n");
236 uint8_t len_name = strlen(funcname) + 1;
237 moa->name = try_malloc(len_name, f_name);
238 memcpy(moa->name, funcname, len_name);
239 if (!( try_func_name(moa, "move", actor_move)
240 || try_func_name(moa, "pick_up", actor_pick)
241 || try_func_name(moa, "drop", actor_drop)
242 || try_func_name(moa, "use", actor_use)))
244 moa->func = actor_wait;
248 moa_ptr_ptr = &moa->next;
250 try_fclose(file, f_name);
251 set_cleanup_flag(CLEANUP_MAP_OBJECT_ACTS);
256 extern void free_map_object_actions(struct MapObjAct * moa)
263 free_map_object_actions(moa->next);
269 extern uint8_t get_moa_id_by_name(char * name)
271 struct MapObjAct * moa = world.map_obj_acts;
274 if (0 == strcmp(moa->name, name))
280 exit_err(NULL == moa, "get_moa_id_name() did not find map object action.");
286 extern void actor_wait(struct MapObj * mo)
288 if (mo == get_player())
296 extern void actor_move(struct MapObj * mo)
299 struct yx_uint16 target = mv_yx_in_dir(d, mo->pos);
300 struct MapObj * other_mo;
301 for (other_mo = world.map_objs; other_mo != 0; other_mo = other_mo->next)
303 if (0 == other_mo->lifepoints || other_mo == mo)
307 if (yx_uint16_cmp(&target, &other_mo->pos))
309 actor_hits_actor(mo, other_mo);
313 uint8_t passable = is_passable(target);
316 set_object_position(mo, target);
318 if (mo == get_player())
320 playerbonus_move(d, passable);
326 extern void actor_drop(struct MapObj * mo)
328 uint8_t owns_none = (NULL == mo->owns);
331 uint8_t select = mo->arg;
332 struct MapObj * owned = mo->owns;
334 for (; i != select; i++, owned = owned->next);
335 own_map_object(&world.map_objs, &mo->owns, owned->id);
337 if (mo == get_player())
339 playerbonus_drop(owns_none);
345 extern void actor_pick(struct MapObj * mo)
347 struct MapObj * picked;
348 for (picked = world.map_objs; NULL != picked; picked = picked->next)
350 if (picked != mo && yx_uint16_cmp(&picked->pos, &mo->pos))
357 own_map_object(&mo->owns, &world.map_objs, picked->id);
358 set_object_position(picked, mo->pos);
360 if (mo == get_player())
362 playerbonus_pick(NULL != picked);
368 extern void actor_use(struct MapObj * mo)
370 uint8_t wrong_object = 1;
371 uint8_t no_object = (NULL == mo->owns);
374 uint8_t select = mo->arg;
376 struct MapObj * selected = mo->owns;
377 for (; i != select; i++, selected = selected->next);
378 struct MapObjDef * mod = get_map_object_def(selected->type);
379 if (!strcmp("MAGIC MEAT", mod->name))
382 struct MapObj * next = selected->next;
388 for (i = 0; i != select; i++, selected = selected->next);
389 selected->next = next;
398 if (mo == get_player())
400 playerbonus_use(no_object, wrong_object);