1 #include "objects_on_map.h"
7 extern char is_passable (struct Map * map, struct yx_uint16 pos) {
8 // Check if coordinate on (or beyond) map is accessible to actor movement.
10 if (0 <= pos.x && pos.x < map->size.x && 0 <= pos.y && pos.y < map->size.y)
11 if ('.' == map->cells[pos.y * map->size.x + pos.x])
15 extern struct yx_uint16 find_passable_pos (struct Map * map) {
16 // Return a random passable position on map.
18 for (pos.y = pos.x = 0; 0 == is_passable(map, pos);) {
19 pos.y = rrand(0, 0) % map->size.y;
20 pos.x = rrand(0, 0) % map->size.x; }
23 extern void move_monster (struct World * world, struct Monster * monster) {
24 // Move monster in random direction, trigger fighting when hindered by player/monster.
25 char d = rrand(0, 0) % 5;
26 struct yx_uint16 t = mv_yx_in_dir (d, monster->cmo.pos);
27 if (yx_uint16_cmp (t, world->player->pos)) {
28 update_log (world, "\nThe monster hits you.");
30 struct Monster * other_monster;
31 for (other_monster = world->monster; other_monster != 0; other_monster = other_monster->cmo.next) {
32 if (other_monster == monster)
34 if (yx_uint16_cmp (t, other_monster->cmo.pos)) {
35 update_log (world, "\nMonster bumps into monster.");
37 if (is_passable(world->map, t))
38 monster->cmo.pos = t; }
40 extern void move_player (struct World * world, char d) {
41 // Move player in direction d, update log and turn over to the enemy.
42 struct yx_uint16 t = mv_yx_in_dir (d, world->player->pos);
43 struct Monster * monster;
44 for (monster = world->monster; monster != 0; monster = monster->cmo.next)
45 if (yx_uint16_cmp (t, monster->cmo.pos)) {
46 update_log (world, "\nYou hit the monster.");
48 if (0 == monster->hitpoints) {
49 update_log (world, "\nYou kill the monster.");
50 if (world->monster == monster)
51 world->monster = world->monster->cmo.next;
53 struct Monster * m_prev;
54 for (m_prev = world->monster; m_prev->cmo.next != monster; m_prev = m_prev->cmo.next);
55 m_prev->cmo.next = monster->cmo.next; }
59 char * msg = calloc(25, sizeof(char));
60 char * msg_content = "You fail to move";
62 if (NORTH == d) dir = "north";
63 else if (EAST == d) dir = "east" ;
64 else if (SOUTH == d) dir = "south";
65 else if (WEST == d) dir = "west" ;
66 if (is_passable(world->map, t)) {
67 msg_content = "You move";
68 world->player->pos = t; }
69 sprintf(msg, "\n%s %s.", msg_content, dir);
70 update_log (world, msg);
72 turn_over (world, d); }
74 extern void player_wait (struct World * world) {
75 // Make player wait one turn.
76 update_log (world, "\nYou wait.");
77 turn_over (world, 0); }