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(), err_line(),
10 * reset_err_try_fgets_counter()
12 #include "../common/readwrite.h" /* textfile_width(), try_fopen(), try_fclose(),
15 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
16 #include "../common/try_malloc.h" /* try_malloc() */
17 #include "../common/yx_uint8.h" /* struct yx_uint8 */
18 #include "cleanup.h" /* set_cleanup_flag() */
19 #include "map_objects.h" /* structs MapObj, MapObjDef, get_player(),
20 * set_object_position(), own_map_object(),
21 * get_map_object_def()
23 #include "map.h" /* is_passable() */
24 #include "yx_uint8.h" /* mv_yx_in_dir(), yx_uint8_cmp() */
25 #include "world.h" /* global world */
29 /* Append "text" to game log, or a "." if "text" is the same as the last one. */
30 static void update_log(char * text);
32 /* If "name" fits "moa"->name, set "moa"->func to "func". */
33 static uint8_t try_func_name(struct MapObjAct * moa,
34 char * name, void (* func) (struct MapObj *));
36 /* One actor "wounds" another actor, decrementing his lifepoints and, if they
37 * reach zero in the process, killing it. Generates appropriate log message.
39 static void actor_hits_actor(struct MapObj * hitter, struct MapObj * hitted);
41 /* Bonus stuff to actor_*() to happen if actor==player. Mostly writing of log
42 * messages; _pick and _drop also decrement world.inventory_sel by 1 if >0.
43 * (match_dir() is just a little helper to playerbonus_move().)
45 static void playerbonus_wait();
46 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match);
47 static void playerbonus_move(char d, uint8_t passable);
48 static void playerbonus_drop(uint8_t owns_none);
49 static void playerbonus_pick(uint8_t picked);
50 static void playerbonus_use(uint8_t no_object, uint8_t wrong_object);
54 static void update_log(char * text)
56 char * f_name = "update_log()";
57 uint16_t len_new = strlen(text);
61 len_old = strlen(world.log);
62 uint16_t last_nl = len_old - 1;
65 if ('\n' == world.log[last_nl])
71 uint16_t last_stop = len_old - 1;
72 while (last_stop != 0)
74 if ('.' == world.log[last_stop] && '.' != world.log[last_stop - 1])
80 if ( (last_stop + 1) - last_nl == (uint16_t) strlen(text)
81 && 0 == strncmp(world.log + last_nl, text, strlen(text)))
86 uint16_t len_whole = len_old + len_new + 1;
87 char * new_text = try_malloc(len_whole, f_name);
88 memcpy(new_text, world.log, len_old);
89 sprintf(new_text + len_old, "%s", text);
96 static uint8_t try_func_name(struct MapObjAct * moa,
97 char * name, void (* func) (struct MapObj *))
99 if (0 == strcmp(moa->name, name))
109 static void actor_hits_actor(struct MapObj * hitter, struct MapObj * hitted)
111 struct MapObjDef * mod_hitter = get_map_object_def(hitter->type);
112 struct MapObjDef * mod_hitted = get_map_object_def(hitted->type);
113 struct MapObj * player = get_player();
115 char * msg2 = "wound";
117 if (player != hitter)
119 msg1 = mod_hitter->name;
122 if (player != hitted)
124 msg3 = mod_hitted->name;
126 uint8_t len = 1 + strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2;
128 sprintf(msg, "\n%s %s %s.", msg1, msg2, msg3);
130 hitted->lifepoints--;
131 if (0 == hitted->lifepoints)
133 hitted->type = mod_hitted->corpse_id;
134 if (player == hitted)
136 update_log(" You die.");
139 update_log(" It dies.");
145 static void playerbonus_wait()
147 update_log("\nYou wait.");
152 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match)
164 static void playerbonus_move(char d, uint8_t passable)
166 char * dsc_dir = "north";
167 if ( match_dir(d, &dsc_dir, '6', "east")
168 || match_dir(d, &dsc_dir, '2', "south")
169 || match_dir(d, &dsc_dir, '4', "west")
170 || match_dir(d, &dsc_dir, '7', "north-west")
171 || match_dir(d, &dsc_dir, '9', "north-east")
172 || match_dir(d, &dsc_dir, '1', "south-west")
173 || match_dir(d, &dsc_dir, '3', "south-east"))
177 char * dsc_move = "You move ";
180 dsc_move = "You fail to move ";
182 char msg[strlen(dsc_move) + strlen (dsc_dir) + 3];
183 sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
189 static void playerbonus_drop(uint8_t owns_none)
193 update_log("\nYou try to drop an object, but you own none.");
196 update_log("\nYou drop an object.");
201 static void playerbonus_pick(uint8_t picked)
205 update_log("\nYou pick up an object.");
208 update_log("\nYou try to pick up an object, but there is none.");
213 static void playerbonus_use(uint8_t no_object, uint8_t wrong_object)
217 update_log("\nYou try to use an object, but you own none.");
220 else if (wrong_object)
222 update_log("\nYou try to use this object, but fail.");
225 update_log("\nYou consume MAGIC MEAT.");
230 extern void init_map_object_actions()
232 char * f_name = "init_map_object_actions()";
233 FILE * file = try_fopen(world.path_map_obj_acts, "r", f_name);
234 uint32_t linemax = textfile_width(file);
235 char line[linemax + 1];
236 struct MapObjAct ** moa_ptr_ptr = &world.map_obj_acts;
237 char * context = "Failed reading map object actions config file. ";
238 char * err_uniq = "Declaration of ID already used.";
239 reset_err_try_fgets_counter();
242 int test_for_end = try_fgetc(file, f_name);
243 if (EOF == test_for_end || '\n' == test_for_end)
247 exit_trouble(EOF == ungetc(test_for_end, file), f_name, "ungetc()");
248 struct MapObjAct * moa = try_malloc(sizeof(struct MapObjAct), f_name);
249 err_try_fgets(line, linemax, file, context, "nfi8");
250 moa->id = atoi(line);
251 struct MapObjAct * moa_test = world.map_obj_acts;
252 for (; NULL != moa_test; moa_test = moa_test->next)
254 err_line(moa->id == moa_test->id, line, context, err_uniq);
256 err_try_fgets(line, linemax, file, context, "0nfi8");
257 moa->effort = atoi(line);
258 err_try_fgets(line, linemax, file, context, "0nf");
259 line[strlen(line) - 1] = '\0';
260 uint8_t len_name = strlen(line) + 1;
261 moa->name = try_malloc(len_name, f_name);
262 memcpy(moa->name, line, len_name);
263 if (!( try_func_name(moa, "move", actor_move)
264 || try_func_name(moa, "pick_up", actor_pick)
265 || try_func_name(moa, "drop", actor_drop)
266 || try_func_name(moa, "use", actor_use)))
268 moa->func = actor_wait;
272 moa_ptr_ptr = &moa->next;
273 err_try_fgets(line, linemax, file, context, "d");
275 try_fclose(file, f_name);
276 set_cleanup_flag(CLEANUP_MAP_OBJECT_ACTS);
281 extern void free_map_object_actions(struct MapObjAct * moa)
288 free_map_object_actions(moa->next);
294 extern uint8_t get_moa_id_by_name(char * name)
296 struct MapObjAct * moa = world.map_obj_acts;
299 if (0 == strcmp(moa->name, name))
305 exit_err(NULL==moa, "get_moa_id_by_name() did not find map object action.");
311 extern void actor_wait(struct MapObj * mo)
313 if (mo == get_player())
321 extern void actor_move(struct MapObj * mo)
324 struct yx_uint8 target = mv_yx_in_dir(d, mo->pos);
325 struct MapObj * other_mo;
326 for (other_mo = world.map_objs; other_mo != 0; other_mo = other_mo->next)
328 if (0 == other_mo->lifepoints || other_mo == mo)
332 if (yx_uint8_cmp(&target, &other_mo->pos))
334 actor_hits_actor(mo, other_mo);
338 uint8_t passable = is_passable(target);
341 set_object_position(mo, target);
343 if (mo == get_player())
345 playerbonus_move(d, passable);
351 extern void actor_drop(struct MapObj * mo)
353 uint8_t owns_none = (NULL == mo->owns);
356 uint8_t select = mo->arg;
357 struct MapObj * owned = mo->owns;
359 for (; i != select; i++, owned = owned->next);
360 own_map_object(&world.map_objs, &mo->owns, owned->id);
362 if (mo == get_player())
364 playerbonus_drop(owns_none);
370 extern void actor_pick(struct MapObj * mo)
372 struct MapObj * picked = NULL;
373 struct MapObj * mo_i;
374 for (mo_i = world.map_objs; NULL != mo_i; mo_i = mo_i->next)
376 if (mo_i != mo && yx_uint8_cmp(&mo_i->pos, &mo->pos))
383 own_map_object(&mo->owns, &world.map_objs, picked->id);
384 set_object_position(picked, mo->pos);
386 if (mo == get_player())
388 playerbonus_pick(NULL != picked);
394 extern void actor_use(struct MapObj * mo)
396 uint8_t wrong_object = 1;
397 uint8_t no_object = (NULL == mo->owns);
400 uint8_t select = mo->arg;
402 struct MapObj * selected = mo->owns;
403 for (; i != select; i++, selected = selected->next);
404 struct MapObjDef * mod = get_map_object_def(selected->type);
408 struct MapObj * next = selected->next;
414 for (i = 0; i != select; i++, selected = selected->next);
415 selected->next = next;
421 mo->lifepoints = mo->lifepoints + mod->consumable;
424 if (mo == get_player())
426 playerbonus_use(no_object, wrong_object);