home · contact · privacy
Make resize_active_win use yx_uint16 for coordinates instead of two separate ints.
[plomrogue] / src / actors.c
1 #include "actors.h"
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <stdint.h>
5 #include "yx_uint16.h"
6 #include "roguelike.h"
7
8 extern char is_passable (struct Map * map, uint16_t y, uint16_t x) {
9 // Check if coordinate on (or beyond) map is accessible to actor movement.
10   char passable = 0;
11   if (0 <= x && x < map->size.x && 0 <= y && y < map->size.y)
12     if ('.' == map->cells[y * map->size.x + x])
13       passable = 1;
14   return passable; }
15
16 extern void move_monster (struct World * world, struct Monster * monster) {
17 // Move monster in random direction, trigger fighting when hindered by player/monster.
18   char d = rrand(0, 0) % 5;
19   struct yx_uint16 t = mv_yx_in_dir (d, monster->pos);
20   if (yx_uint16_cmp (t, world->player->pos)) {
21     update_log (world, "\nThe monster hits you.");
22     return; }
23   struct Monster * other_monster;
24   for (other_monster = world->monster; other_monster != 0; other_monster = other_monster->next) {
25     if (other_monster == monster)
26       continue;
27     if (yx_uint16_cmp (t, other_monster->pos)) {
28       update_log (world, "\nMonster hits monster.");
29       return; } }
30   if (is_passable(world->map, t.y, t.x))
31     monster->pos = t; }
32
33 extern void move_player (struct World * world, char d) {
34 // Move player in direction d, increment turn counter and update log.
35   struct yx_uint16 t = mv_yx_in_dir (d, world->player->pos);
36   struct Monster * monster;
37   for (monster = world->monster; monster != 0; monster = monster->next)
38     if (yx_uint16_cmp (t, monster->pos)) {
39       update_log (world, "\nYou hit the monster.");
40       turn_over (world, d);
41       return; }
42   char * msg = calloc(25, sizeof(char));
43   char * msg_content = "You fail to move";
44   char * dir;
45   if      (NORTH == d) dir = "north";
46   else if (EAST  == d) dir = "east" ;
47   else if (SOUTH == d) dir = "south";
48   else if (WEST  == d) dir = "west" ;
49   if (is_passable(world->map, t.y, t.x)) {
50     msg_content = "You move";
51     world->player->pos = t; }
52   sprintf(msg, "\n%s %s.", msg_content, dir);
53   update_log (world, msg);
54   free(msg);
55   turn_over (world, d); }
56
57 extern void player_wait (struct World * world) {
58 // Make player wait one turn.
59   update_log (world, "\nYou wait.");
60   turn_over (world, 0); }