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