home · contact · privacy
Improved comments.
[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 <string.h> /* for strlen() */
6 #include "yx_uint16.h" /* for yx_uint16 struct, mv_yx_in_dir(), yx_uint16_cmp */
7 #include "misc.h" /* for update_log(), turn_over()*/
8 #include "map.h" /* for Map struct */
9 #include "main.h" /* for World struct */
10 #include "map_objects.h" /* for map object (definition) structs */
11 #include "rrand.h" /* for rrand() */
12
13
14
15 /* Log monster (described by "dsc_monster1") bumping into "monster2". */
16 static void monster_bumps_monster(struct World * world, char * dsc_monster1,
17                                   struct Monster * monster2);
18
19 /* Decrement player HPs due to attack of monster described by "dsc_monster",
20  * kill player if his HP hit zero; log the whole action.
21  */
22 static void monster_hits_player(struct World * world, char * dsc_monster);
23
24 /* Decrement HP of "monster" hit by player, kill it if its HP hit zero, create a
25  * corpse and increment player's score by the amount of hitpoints the monster
26  * started with; log the whole action.
27  */
28 static void player_hits_monster(struct World * world, struct Monster * monster);
29
30 /* Try moving the player in direction "d" towards coordinate "target"; log
31  * success or failure of the whole action.
32  */
33 static void try_player_move(struct World * world,
34                             enum dir d, struct yx_uint16 target);
35
36
37
38 static void monster_bumps_monster(struct World * world, char * dsc_monster1,
39                                   struct Monster * monster2)
40 {
41     char * bump_dsc = " bumps into ";
42     struct MapObjDef * mod = get_map_obj_def(world, monster2->map_obj.type);
43     char * msg = malloc(strlen(dsc_monster1) + strlen(bump_dsc)
44                         + strlen(mod->desc) + 3);
45     sprintf(msg, "\n%s%s%s.", dsc_monster1, bump_dsc, mod->desc);
46     update_log(world, msg);
47     free(msg);
48 }
49
50
51
52 static void monster_hits_player(struct World * world, char * dsc_monster)
53 {
54     char * hit_dsc = " hits you";
55     char * msg = malloc(strlen(dsc_monster) + strlen(hit_dsc) + 3);
56     sprintf(msg, "\n%s%s.", dsc_monster, hit_dsc);
57     update_log(world, msg);
58     free(msg);
59     world->player->hitpoints--;
60     if (0 == world->player->hitpoints)
61     {
62         update_log(world, "\nYou are dead.");
63     }
64 }
65
66
67
68 static void player_hits_monster(struct World * world, struct Monster * monster)
69 {
70     struct MapObjDef * mod = get_map_obj_def(world, monster->map_obj.type);
71     char * hit_dsc = "You hit the ";
72     char * monster_dsc = mod->desc;
73     char * msg = malloc(strlen(hit_dsc) + strlen(monster_dsc) + 3);
74     sprintf(msg, "\n%s%s.", hit_dsc, monster_dsc);
75     update_log(world, msg);
76     free(msg);
77     monster->hitpoints--;
78     if (0 == monster->hitpoints)
79     {
80         hit_dsc = "You kill the ";
81         msg = malloc(strlen(hit_dsc) + strlen(monster_dsc) + 3);
82         sprintf(msg, "\n%s%s.", hit_dsc, monster_dsc);
83         update_log(world, msg);
84         free(msg);
85         struct MonsterDef * md = (struct MonsterDef * ) mod;
86         struct Item * corpse = malloc(sizeof(struct Item));
87         corpse->map_obj.type = md->corpse_id;
88         corpse->map_obj.pos = monster->map_obj.pos;
89         corpse->map_obj.next = world->item;
90         world->item = corpse;
91         if (world->monster == monster)
92         {
93             world->monster = world->monster->map_obj.next;
94         }
95         else
96         {
97             struct Monster * m_prev;
98             for (m_prev = world->monster;
99                  m_prev->map_obj.next != monster;
100                  m_prev = m_prev->map_obj.next);
101             {
102                 m_prev->map_obj.next = monster->map_obj.next;
103             }
104         }
105         uint8_t score = md->hitpoints_start;
106         world->score = world->score + score;
107         free(monster);
108     }
109 }
110
111
112
113 static void try_player_move(struct World * world,
114                             enum dir d, struct yx_uint16 target)
115 {
116     char * dsc_dir;
117     if      (NORTH == d)
118     {
119         dsc_dir = "north";
120     }
121     else if (EAST  == d)
122     {
123         dsc_dir = "east" ;
124     }
125     else if (SOUTH == d)
126     {
127         dsc_dir = "south";
128     }
129     else if (WEST  == d)
130     {
131         dsc_dir = "west" ;
132     }
133     char * dsc_move = "You fail to move ";
134     if (is_passable(world->map, target))
135     {
136         dsc_move = "You move ";
137         world->player->pos = target;
138     }
139     char * msg = malloc(strlen(dsc_move) + strlen (dsc_dir) + 3);
140     sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
141     update_log(world, msg);
142     free(msg);
143 }
144
145
146
147 extern void move_monster(struct World * world, struct Monster * monster)
148 {
149     char d = rrand() % 5;
150     struct yx_uint16 t = mv_yx_in_dir(d, monster->map_obj.pos);
151     struct MapObjDef * mod = get_map_obj_def(world, monster->map_obj.type);
152     char * dsc = mod->desc;
153     if (yx_uint16_cmp(&t, &world->player->pos))
154     {
155         monster_hits_player(world, dsc);
156         return;
157     }
158     struct Monster * other_monster;
159     for (other_monster = world->monster;
160          other_monster != 0;
161          other_monster = other_monster->map_obj.next)
162     {
163         if (other_monster == monster)
164         {
165             continue;
166         }
167         if (yx_uint16_cmp(&t, &other_monster->map_obj.pos))
168         {
169             monster_bumps_monster(world, dsc, other_monster);
170             return;
171         }
172     }
173     if (is_passable(world->map, t))
174     {
175         monster->map_obj.pos = t;
176     }
177 }
178
179
180
181 extern void move_player(struct World * world, enum dir d)
182 {
183     struct yx_uint16 t = mv_yx_in_dir(d, world->player->pos);
184     struct Monster * monster;
185     for (monster = world->monster;
186          monster != 0;
187          monster = monster->map_obj.next)
188     {
189         if (yx_uint16_cmp(&t, &monster->map_obj.pos))
190         {
191             player_hits_monster(world, monster);
192             turn_over(world, d);
193             return;
194           }
195     }
196     try_player_move(world, d, t);
197     turn_over(world, d);
198 }
199
200
201
202 extern void player_wait (struct World * world)
203 {
204     update_log(world, "\nYou wait.");
205     turn_over(world, 0);
206 }
207
208
209
210 extern char is_passable (struct Map * map, struct yx_uint16 pos)
211 {
212     char passable = 0;
213     if (0 <= pos.x && pos.x < map->size.x && 0 <= pos.y && pos.y < map->size.y)
214     {
215         if ('.' == map->cells[pos.y * map->size.x + pos.x])
216         {
217             passable = 1;
218         }
219     }
220     return passable;
221 }