1 /* map_object_actions.c */
3 #include "map_object_actions.h"
4 #include <string.h> /* for strlen() */
5 #include "yx_uint16.h" /* for yx_uint16 struct, mv_yx_in_dir(), yx_uint16_cmp */
6 #include "map_objects.h" /* for MapObj, MapObjDef structs, get_player() */
7 #include "misc.h" /* for update_log(), turn_over() */
8 #include "map.h" /* for Map struct */
9 #include "main.h" /* for World struct */
10 #include "command_db.h" /* for get_command_id() */
14 /* One actor "wounds" another actor, decrementing his lifepoints and, if they
15 * reach zero in the process, killing it. Generates appropriate log message.
17 static void actor_hits_actor(struct World * world, struct MapObj * hitter,
18 struct MapObj * hitted)
22 static void actor_hits_actor(struct World * world, struct MapObj * hitter,
23 struct MapObj * hitted)
25 struct MapObjDef * mod_hitter = get_map_object_def(world, hitter->type);
26 struct MapObjDef * mod_hitted = get_map_object_def(world, hitted->type);
27 struct MapObj * player = get_player(world);
29 char * msg2 = "wound";
33 msg1 = mod_hitter->name;
38 msg3 = mod_hitted->name;
40 uint8_t len = 1 + strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2;
42 sprintf(msg, "\n%s %s %s.", msg1, msg2, msg3);
43 update_log(world, msg);
45 if (0 == hitted->lifepoints)
47 hitted->type = mod_hitted->corpse_id;
50 update_log(world, " You die.");
54 update_log(world, " It dies.");
57 world->score = world->score + mod_hitted->lifepoints;
65 extern uint8_t move_actor(struct World * world, struct MapObj * actor,
68 struct yx_uint16 target = mv_yx_in_dir(d, actor->pos);
69 struct MapObj * other_actor;
70 for (other_actor = world->map_objs;
72 other_actor = other_actor->next)
74 if (0 == other_actor->lifepoints || other_actor == actor)
78 if (yx_uint16_cmp(&target, &other_actor->pos))
80 actor_hits_actor(world, actor, other_actor);
84 if (is_passable(world->map, target))
94 extern void move_player(struct World * world, enum dir d)
97 char * action_dsc_prototype = "player_";
98 uint8_t len_action_dsc_prototype = strlen(action_dsc_prototype);
99 char action_dsc[len_action_dsc_prototype + 2];
100 memcpy(action_dsc, action_dsc_prototype, len_action_dsc_prototype);
104 action_dsc[len_action_dsc_prototype] = 'u';
109 action_dsc[len_action_dsc_prototype] = 'r';
114 action_dsc[len_action_dsc_prototype] = 'd';
119 action_dsc[len_action_dsc_prototype] = 'l';
121 action_dsc[len_action_dsc_prototype + 1] = '\0';
122 uint8_t res = move_actor(world, get_player(world), d);
125 char * dsc_move = "You fail to move ";
128 dsc_move = "You move ";
130 char msg[strlen(dsc_move) + strlen (dsc_dir) + 3];
131 sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
132 update_log(world, msg);
134 turn_over(world, get_command_id(world, action_dsc));
139 extern void player_wait(struct World * world)
141 update_log(world, "\nYou wait.");
142 turn_over(world, get_command_id(world, "wait"));
147 extern char is_passable(struct Map * map, struct yx_uint16 pos)
150 if (0 <= pos.x && pos.x < map->size.x && 0 <= pos.y && pos.y < map->size.y)
152 if ('.' == map->cells[pos.y * map->size.x + pos.x])