home · contact · privacy
More documentation and re-styling of code.
[plomrogue] / src / map_object_actions.c
1 /* map_object_actions.c */
2
3 #include "map_object_actions.h"
4 #include <stdlib.h> /* for malloc(), calloc(), free() */
5 #include "yx_uint16.h" /* for yx_uint16 struct, mv_yx_in_dir(), yx_uint16_cmp */
6 #include "misc.h" /* for rrand(), update_log(), turn_over()*/
7 #include "map.h" /* for Map struct */
8 #include "main.h" /* for World struct */
9 #include "map_objects.h" /* for map object (definition) structs */
10
11
12
13 extern void move_monster(struct World * world, struct Monster * monster)
14 {
15     char d = rrand(0, 0) % 5;
16     struct yx_uint16 t = mv_yx_in_dir(d, monster->map_obj.pos);
17     char * msg = malloc(100);
18     struct MapObjDef * mod = get_map_obj_def(world, monster->map_obj.type);
19     char * desc = mod->desc;
20     char * desc_other;
21     if (yx_uint16_cmp(t, world->player->pos))
22     {
23         sprintf(msg, "\nThe %s hits you.", desc);
24         update_log(world, msg);
25         world->player->hitpoints--;
26         if (0 == world->player->hitpoints)
27         {
28             update_log(world, "\nYou are dead.");
29         }
30         return;
31     }
32     struct Monster * other_monster;
33     for (other_monster = world->monster;
34          other_monster != 0;
35          other_monster = other_monster->map_obj.next)
36     {
37         if (other_monster == monster)
38         {
39             continue;
40         }
41         if (yx_uint16_cmp(t, other_monster->map_obj.pos))
42         {
43             mod = get_map_obj_def(world, monster->map_obj.type);
44             desc_other = mod->desc;
45             sprintf(msg, "\n%s bumps into %s.", desc, desc_other);
46             update_log(world, msg);
47             return;
48         }
49     }
50     free(msg);
51     if (is_passable(world->map, t))
52     {
53         monster->map_obj.pos = t;
54     }
55 }
56
57
58
59 extern void move_player (struct World * world, enum dir d)
60 {
61     struct yx_uint16 t = mv_yx_in_dir(d, world->player->pos);
62     struct Monster * monster;
63     struct MapObjDef * mod;
64     char * msg = calloc(100, sizeof(char));
65     char * desc;
66     for (monster = world->monster;
67          monster != 0;
68          monster = monster->map_obj.next)
69     {
70         if (yx_uint16_cmp(t, monster->map_obj.pos))
71         {
72             mod = get_map_obj_def(world, monster->map_obj.type);
73             desc = mod->desc;
74             sprintf(msg, "\nYou hit the %s.", desc);
75             update_log(world, msg);
76             monster->hitpoints--;
77             if (0 == monster->hitpoints)
78             {
79                 sprintf(msg, "\nYou kill the %s.", desc);
80                 update_log(world, msg);
81                 if (world->monster == monster)
82                 {
83                     world->monster = world->monster->map_obj.next;
84                 }
85                 else
86                 {
87                     struct Monster * m_prev;
88                     for (m_prev = world->monster;
89                          m_prev->map_obj.next != monster;
90                          m_prev = m_prev->map_obj.next);
91                     m_prev->map_obj.next = monster->map_obj.next;
92                 }
93                 free(monster);
94             }
95             turn_over(world, d);
96             return;
97           }
98       }
99       char * msg_content = "You fail to move";
100       char * dir;
101       if      (NORTH == d)
102       {
103           dir = "north";
104       }
105       else if (EAST  == d)
106       {
107           dir = "east" ;
108       }
109       else if (SOUTH == d)
110       {
111           dir = "south";
112       }
113       else if (WEST  == d)
114       {
115           dir = "west" ;
116       }
117       if (is_passable(world->map, t))
118       {
119           msg_content = "You move";
120           world->player->pos = t;
121       }
122       sprintf(msg, "\n%s %s.", msg_content, dir);
123       update_log(world, msg);
124       free(msg);
125       turn_over(world, d);
126 }
127
128
129
130 extern void player_wait (struct World * world)
131 {
132     update_log(world, "\nYou wait.");
133     turn_over(world, 0);
134 }
135
136
137
138 extern char is_passable (struct Map * map, struct yx_uint16 pos)
139 {
140     char passable = 0;
141     if (0 <= pos.x && pos.x < map->size.x && 0 <= pos.y && pos.y < map->size.y)
142     {
143         if ('.' == map->cells[pos.y * map->size.x + pos.x])
144         {
145             passable = 1;
146         }
147     }
148     return passable;
149 }
150