1 /* map_object_actions.c */
3 #include "map_object_actions.h"
4 #include <stdint.h> /* for uint8_t */
5 #include <string.h> /* for strlen(), strcmp() */
6 #include "yx_uint16.h" /* for yx_uint16 struct, mv_yx_in_dir(),
9 #include "map_objects.h" /* for MapObj, MapObjDef structs, get_player(),
10 * set_object_position(), own_map_object()
12 #include "misc.h" /* for update_log(), try_malloc() */
13 #include "map.h" /* for is_passable() */
14 #include "main.h" /* for world global */
15 #include "readwrite.h" /* for try_fopen(), try_fclose(), textfile_sizes() */
16 #include "rexit.h" /* for exit_err() */
20 /* If "name" fits "moa"->name, set "moa"->func to "func". */
21 static uint8_t try_func_name(struct MapObjAct * moa,
22 char * name, void (* func) (struct MapObj *));
24 /* One actor "wounds" another actor, decrementing his lifepoints and, if they
25 * reach zero in the process, killing it. Generates appropriate log message.
27 static void actor_hits_actor(struct MapObj * hitter, struct MapObj * hitted);
29 /* Bonus stuff to actor_*() to happen if actor==player. Mostly writing of log
30 * messages; _pick and _drop also decrement world.inventory_sel by 1 if >0.
32 static void playerbonus_wait();
33 static void playerbonus_move(char d, uint8_t passable);
34 static void playerbonus_drop(uint8_t owns_none);
35 static void playerbonus_pick(uint8_t picked);
36 static void playerbonus_use(uint8_t no_object, uint8_t wrong_object);
40 static uint8_t try_func_name(struct MapObjAct * moa,
41 char * name, void (* func) (struct MapObj *))
43 if (0 == strcmp(moa->name, name))
53 static void actor_hits_actor(struct MapObj * hitter, struct MapObj * hitted)
55 struct MapObjDef * mod_hitter = get_map_object_def(hitter->type);
56 struct MapObjDef * mod_hitted = get_map_object_def(hitted->type);
57 struct MapObj * player = get_player();
59 char * msg2 = "wound";
63 msg1 = mod_hitter->name;
68 msg3 = mod_hitted->name;
70 uint8_t len = 1 + strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2;
72 sprintf(msg, "\n%s %s %s.", msg1, msg2, msg3);
75 if (0 == hitted->lifepoints)
77 hitted->type = mod_hitted->corpse_id;
80 update_log(" You die.");
84 update_log(" It dies.");
87 world.score = world.score + mod_hitted->lifepoints;
95 static void playerbonus_wait()
97 update_log("\nYou wait.");
102 static void playerbonus_move(char d, uint8_t passable)
104 char * dsc_dir = "north";
117 char * dsc_move = "You move ";
120 dsc_move = "You fail to move ";
122 char msg[strlen(dsc_move) + strlen (dsc_dir) + 3];
123 sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
129 static void playerbonus_drop(uint8_t owns_none)
133 update_log("\nYou try to drop an object, but you own none.");
137 update_log("\nYou drop an object.");
138 if (0 < world.inventory_sel)
140 world.inventory_sel--;
147 static void playerbonus_pick(uint8_t picked)
151 update_log("\nYou pick up an object.");
155 update_log("\nYou try to pick up an object, but there is none.");
161 static void playerbonus_use(uint8_t no_object, uint8_t wrong_object)
165 update_log("\nYou try to use an object, but you own none.");
167 else if (wrong_object)
169 update_log("\nYou try to use this object, but fail.");
173 update_log("\nYou consume MAGIC MEAT.");
174 if (0 < world.inventory_sel)
176 world.inventory_sel--;
183 extern void init_map_object_actions()
185 char * f_name = "init_map_object_actions()";
187 char * path = "config/map_object_actions";
188 FILE * file = try_fopen(path, "r", f_name);
189 uint16_t linemax = textfile_sizes(file, NULL);
190 char line[linemax + 1];
192 struct MapObjAct ** moa_ptr_ptr = &world.map_obj_acts;
194 while (fgets(line, linemax + 1, file))
196 if ('\n' == line[0] || 0 == line[0])
200 struct MapObjAct * moa = try_malloc(sizeof(struct MapObjAct), f_name);
201 moa->id = atoi(strtok(line, delim));
202 moa->effort = atoi(strtok(NULL, delim));
203 char * funcname = strtok(NULL, "\n");
204 uint8_t len_name = strlen(funcname) + 1;
205 moa->name = try_malloc(len_name, f_name);
206 memcpy(moa->name, funcname, len_name);
207 if (!( try_func_name(moa, "move", actor_move)
208 || try_func_name(moa, "pick_up", actor_pick)
209 || try_func_name(moa, "drop", actor_drop)
210 || try_func_name(moa, "use", actor_use)))
212 moa->func = actor_wait;
216 moa_ptr_ptr = &moa->next;
218 try_fclose(file, f_name);
223 extern void free_map_object_actions(struct MapObjAct * moa)
230 free_map_object_actions(moa->next);
236 extern uint8_t get_moa_id_by_name(char * name)
238 struct MapObjAct * moa = world.map_obj_acts;
241 if (0 == strcmp(moa->name, name))
247 exit_err(NULL == moa, "get_moa_id_name() did not find map object action.");
253 extern void actor_wait(struct MapObj * mo)
255 if (mo == get_player())
263 extern void actor_move(struct MapObj * mo)
266 struct yx_uint16 target = mv_yx_in_dir(d, mo->pos);
267 struct MapObj * other_mo;
268 for (other_mo = world.map_objs; other_mo != 0; other_mo = other_mo->next)
270 if (0 == other_mo->lifepoints || other_mo == mo)
274 if (yx_uint16_cmp(&target, &other_mo->pos))
276 actor_hits_actor(mo, other_mo);
280 uint8_t passable = is_passable(world.map, target);
283 set_object_position(mo, target);
285 if (mo == get_player())
287 playerbonus_move(d, passable);
293 extern void actor_drop(struct MapObj * mo)
295 uint8_t owns_none = (NULL == mo->owns);
298 uint8_t select = mo->arg;
299 struct MapObj * owned = mo->owns;
301 for (; i != select; i++, owned = owned->next);
302 own_map_object(&world.map_objs, &mo->owns, owned->id);
304 if (mo == get_player())
306 playerbonus_drop(owns_none);
312 extern void actor_pick(struct MapObj * mo)
314 struct MapObj * picked;
315 for (picked = world.map_objs; NULL != picked; picked = picked->next)
317 if (picked != mo && yx_uint16_cmp(&picked->pos, &mo->pos))
324 own_map_object(&mo->owns, &world.map_objs, picked->id);
325 set_object_position(picked, mo->pos);
327 if (mo == get_player())
329 playerbonus_pick(NULL != picked);
335 extern void actor_use(struct MapObj * mo)
337 uint8_t wrong_object = 1;
338 uint8_t no_object = (NULL == mo->owns);
341 uint8_t select = mo->arg;
343 struct MapObj * selected = mo->owns;
344 for (; i != select; i++, selected = selected->next);
345 struct MapObjDef * mod = get_map_object_def(selected->type);
346 if (!strcmp("MAGIC MEAT", mod->name))
349 struct MapObj * next = selected->next;
354 for (i = 0, selected = mo->owns;
356 i++, selected = selected->next);
357 selected->next = next;
366 if (mo == get_player())
368 playerbonus_use(no_object, wrong_object);