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->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->next) {
32 if (other_monster == monster)
34 if (yx_uint16_cmp (t, other_monster->pos)) {
35 update_log (world, "\nMonster hits monster.");
37 if (is_passable(world->map, 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->next)
45 if (yx_uint16_cmp (t, monster->pos)) {
46 update_log (world, "\nYou hit the monster.");
49 char * msg = calloc(25, sizeof(char));
50 char * msg_content = "You fail to move";
52 if (NORTH == d) dir = "north";
53 else if (EAST == d) dir = "east" ;
54 else if (SOUTH == d) dir = "south";
55 else if (WEST == d) dir = "west" ;
56 if (is_passable(world->map, t)) {
57 msg_content = "You move";
58 world->player->pos = t; }
59 sprintf(msg, "\n%s %s.", msg_content, dir);
60 update_log (world, msg);
62 turn_over (world, d); }
64 extern void player_wait (struct World * world) {
65 // Make player wait one turn.
66 update_log (world, "\nYou wait.");
67 turn_over (world, 0); }