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(), atoi() */
8 #include <string.h> /* strlen(), strcmp(), memcpy(), strtok(), strncmp() */
9 #include "../common/readwrite.h" /* textfile_sizes(), try_fopen(), try_fclose(),
12 #include "../common/rexit.h" /* exit_err() */
13 #include "../common/try_malloc.h" /* try_malloc() */
14 #include "../common/yx_uint16.h" /* yx_uint16 struct */
15 #include "cleanup.h" /* set_cleanup_flag() */
16 #include "map_objects.h" /* structs MapObj, MapObjDef, get_player(),
17 * set_object_position(), own_map_object(),
18 * get_map_object_def()
20 #include "map.h" /* is_passable() */
21 #include "yx_uint16.h" /* mv_yx_in_dir(), yx_uint16_cmp() */
22 #include "world.h" /* global world */
26 /* Append "text" to game log, or a "." if "text" is the same as the last one. */
27 static void update_log(char * text);
29 /* If "name" fits "moa"->name, set "moa"->func to "func". */
30 static uint8_t try_func_name(struct MapObjAct * moa,
31 char * name, void (* func) (struct MapObj *));
33 /* One actor "wounds" another actor, decrementing his lifepoints and, if they
34 * reach zero in the process, killing it. Generates appropriate log message.
36 static void actor_hits_actor(struct MapObj * hitter, struct MapObj * hitted);
38 /* Bonus stuff to actor_*() to happen if actor==player. Mostly writing of log
39 * messages; _pick and _drop also decrement world.inventory_sel by 1 if >0.
41 static void playerbonus_wait();
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 == 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.");
135 if (player == hitter)
137 world.score = world.score + mod_hitted->lifepoints;
144 static void playerbonus_wait()
146 update_log("\nYou wait.");
151 static void playerbonus_move(char d, uint8_t passable)
153 char * dsc_dir = "north";
166 char * dsc_move = "You move ";
169 dsc_move = "You fail to move ";
171 char msg[strlen(dsc_move) + strlen (dsc_dir) + 3];
172 sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
178 static void playerbonus_drop(uint8_t owns_none)
182 update_log("\nYou try to drop an object, but you own none.");
185 update_log("\nYou drop an object.");
190 static void playerbonus_pick(uint8_t picked)
194 update_log("\nYou pick up an object.");
197 update_log("\nYou try to pick up an object, but there is none.");
202 static void playerbonus_use(uint8_t no_object, uint8_t wrong_object)
206 update_log("\nYou try to use an object, but you own none.");
209 else if (wrong_object)
211 update_log("\nYou try to use this object, but fail.");
214 update_log("\nYou consume MAGIC MEAT.");
219 extern void init_map_object_actions(char * path)
221 char * f_name = "init_map_object_actions()";
222 FILE * file = try_fopen(path, "r", f_name);
223 uint16_t linemax = textfile_sizes(file, NULL);
224 char line[linemax + 1];
225 struct MapObjAct ** moa_ptr_ptr = &world.map_obj_acts;
227 while (try_fgets(line, linemax + 1, file, f_name))
229 if ('\n' == line[0] || 0 == line[0])
233 struct MapObjAct * moa = try_malloc(sizeof(struct MapObjAct), f_name);
234 moa->id = atoi(strtok(line, delim));
235 moa->effort = atoi(strtok(NULL, delim));
236 char * funcname = strtok(NULL, "\n");
237 uint8_t len_name = strlen(funcname) + 1;
238 moa->name = try_malloc(len_name, f_name);
239 memcpy(moa->name, funcname, len_name);
240 if (!( try_func_name(moa, "move", actor_move)
241 || try_func_name(moa, "pick_up", actor_pick)
242 || try_func_name(moa, "drop", actor_drop)
243 || try_func_name(moa, "use", actor_use)))
245 moa->func = actor_wait;
249 moa_ptr_ptr = &moa->next;
251 try_fclose(file, f_name);
252 set_cleanup_flag(CLEANUP_MAP_OBJECT_ACTS);
257 extern void free_map_object_actions(struct MapObjAct * moa)
264 free_map_object_actions(moa->next);
270 extern uint8_t get_moa_id_by_name(char * name)
272 struct MapObjAct * moa = world.map_obj_acts;
275 if (0 == strcmp(moa->name, name))
281 exit_err(NULL==moa, "get_moa_id_by_name() did not find map object action.");
287 extern void actor_wait(struct MapObj * mo)
289 if (mo == get_player())
297 extern void actor_move(struct MapObj * mo)
300 struct yx_uint16 target = mv_yx_in_dir(d, mo->pos);
301 struct MapObj * other_mo;
302 for (other_mo = world.map_objs; other_mo != 0; other_mo = other_mo->next)
304 if (0 == other_mo->lifepoints || other_mo == mo)
308 if (yx_uint16_cmp(&target, &other_mo->pos))
310 actor_hits_actor(mo, other_mo);
314 uint8_t passable = is_passable(target);
317 set_object_position(mo, target);
319 if (mo == get_player())
321 playerbonus_move(d, passable);
327 extern void actor_drop(struct MapObj * mo)
329 uint8_t owns_none = (NULL == mo->owns);
332 uint8_t select = mo->arg;
333 struct MapObj * owned = mo->owns;
335 for (; i != select; i++, owned = owned->next);
336 own_map_object(&world.map_objs, &mo->owns, owned->id);
338 if (mo == get_player())
340 playerbonus_drop(owns_none);
346 extern void actor_pick(struct MapObj * mo)
348 struct MapObj * picked;
349 for (picked = world.map_objs; NULL != picked; picked = picked->next)
351 if (picked != mo && yx_uint16_cmp(&picked->pos, &mo->pos))
358 own_map_object(&mo->owns, &world.map_objs, picked->id);
359 set_object_position(picked, mo->pos);
361 if (mo == get_player())
363 playerbonus_pick(NULL != picked);
369 extern void actor_use(struct MapObj * mo)
371 uint8_t wrong_object = 1;
372 uint8_t no_object = (NULL == mo->owns);
375 uint8_t select = mo->arg;
377 struct MapObj * selected = mo->owns;
378 for (; i != select; i++, selected = selected->next);
379 struct MapObjDef * mod = get_map_object_def(selected->type);
380 if (!strcmp("MAGIC MEAT", mod->name))
383 struct MapObj * next = selected->next;
389 for (i = 0; i != select; i++, selected = selected->next);
390 selected->next = next;
399 if (mo == get_player())
401 playerbonus_use(no_object, wrong_object);