home · contact · privacy
Rewrote update_log () for a comeback of the message repition compression feature...
[plomrogue] / src / actors.c
1 #include "actors.h"
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include "yx_uint16.h"
5 #include "roguelike.h"
6
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.
9   char passable = 0;
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])
12       passable = 1;
13   return passable; }
14
15 extern void move_monster (struct World * world, struct Monster * monster) {
16 // Move monster in random direction, trigger fighting when hindered by player/monster.
17   char d = rrand(0, 0) % 5;
18   struct yx_uint16 t = mv_yx_in_dir (d, monster->pos);
19   if (yx_uint16_cmp (t, world->player->pos)) {
20     update_log (world, "\nThe monster hits you.");
21     return; }
22   struct Monster * other_monster;
23   for (other_monster = world->monster; other_monster != 0; other_monster = other_monster->next) {
24     if (other_monster == monster)
25       continue;
26     if (yx_uint16_cmp (t, other_monster->pos)) {
27       update_log (world, "\nMonster hits monster.");
28       return; } }
29   if (is_passable(world->map, t))
30     monster->pos = t; }
31
32 extern void move_player (struct World * world, char d) {
33 // Move player in direction d, update log and turn over to the enemy.
34   struct yx_uint16 t = mv_yx_in_dir (d, world->player->pos);
35   struct Monster * monster;
36   for (monster = world->monster; monster != 0; monster = monster->next)
37     if (yx_uint16_cmp (t, monster->pos)) {
38       update_log (world, "\nYou hit the monster.");
39       turn_over (world, d);
40       return; }
41   char * msg = calloc(25, sizeof(char));
42   char * msg_content = "You fail to move";
43   char * dir;
44   if      (NORTH == d) dir = "north";
45   else if (EAST  == d) dir = "east" ;
46   else if (SOUTH == d) dir = "south";
47   else if (WEST  == d) dir = "west" ;
48   if (is_passable(world->map, t)) {
49     msg_content = "You move";
50     world->player->pos = t; }
51   sprintf(msg, "\n%s %s.", msg_content, dir);
52   update_log (world, msg);
53   free(msg);
54   turn_over (world, d); }
55
56 extern void player_wait (struct World * world) {
57 // Make player wait one turn.
58   update_log (world, "\nYou wait.");
59   turn_over (world, 0); }