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