1 #include "map_object_actions.h"
7 #include "map_objects.h"
9 extern char is_passable (struct Map * map, struct yx_uint16 pos) {
10 // Check if coordinate on (or beyond) map is accessible to actor movement.
12 if (0 <= pos.x && pos.x < map->size.x && 0 <= pos.y && pos.y < map->size.y)
13 if ('.' == map->cells[pos.y * map->size.x + pos.x])
17 extern void move_monster (struct World * world, struct Monster * monster) {
18 // Move monster in random direction, trigger fighting when hindered by player/monster.
19 char d = rrand(0, 0) % 5;
20 struct yx_uint16 t = mv_yx_in_dir (d, monster->map_obj.pos);
21 char * msg = malloc(100);
22 struct MapObjDef * mod = get_map_obj_def (world, monster->map_obj.type);
23 char * desc = mod->desc;
25 if (yx_uint16_cmp (t, world->player->pos)) {
26 sprintf(msg, "\nThe %s hits you.", desc);
27 update_log (world, msg);
28 world->player->hitpoints--;
29 if (0 == world->player->hitpoints)
30 update_log (world, "\nYou are dead.");
32 struct Monster * other_monster;
33 for (other_monster = world->monster; other_monster != 0; other_monster = other_monster->map_obj.next) {
34 if (other_monster == monster)
36 if (yx_uint16_cmp (t, other_monster->map_obj.pos)) {
37 mod = get_map_obj_def (world, monster->map_obj.type);
38 desc_other = mod->desc;
39 sprintf(msg, "\n%s bumps into %s.", desc, desc_other);
40 update_log (world, msg);
43 if (is_passable(world->map, t))
44 monster->map_obj.pos = t; }
46 extern void move_player (struct World * world, enum dir d) {
47 // Move player in direction d, update log and turn over to the enemy.
48 struct yx_uint16 t = mv_yx_in_dir (d, world->player->pos);
49 struct Monster * monster;
50 struct MapObjDef * mod;
51 char * msg = calloc(100, sizeof(char));
53 for (monster = world->monster; monster != 0; monster = monster->map_obj.next)
54 if (yx_uint16_cmp (t, monster->map_obj.pos)) {
55 mod = get_map_obj_def (world, monster->map_obj.type);
57 sprintf (msg, "\nYou hit the %s.", desc);
58 update_log (world, msg);
60 if (0 == monster->hitpoints) {
61 sprintf (msg, "\nYou kill the %s.", desc);
62 update_log (world, msg);
63 if (world->monster == monster)
64 world->monster = world->monster->map_obj.next;
66 struct Monster * m_prev;
67 for (m_prev = world->monster; m_prev->map_obj.next != monster; m_prev = m_prev->map_obj.next);
68 m_prev->map_obj.next = monster->map_obj.next; }
72 char * msg_content = "You fail to move";
74 if (NORTH == d) dir = "north";
75 else if (EAST == d) dir = "east" ;
76 else if (SOUTH == d) dir = "south";
77 else if (WEST == d) dir = "west" ;
78 if (is_passable(world->map, t)) {
79 msg_content = "You move";
80 world->player->pos = t; }
81 sprintf(msg, "\n%s %s.", msg_content, dir);
82 update_log (world, msg);
84 turn_over (world, d); }
86 extern void player_wait (struct World * world) {
87 // Make player wait one turn.
88 update_log (world, "\nYou wait.");
89 turn_over (world, 0); }