home · contact · privacy
Re-wrote map object system to use same structs for items and monsters, and switched...
[plomrogue] / src / map_object_actions.c
1 /* map_object_actions.c */
2
3 #include "map_object_actions.h"
4 #include <string.h> /* for strlen() */
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 structs MapObj, MapObjDef,
10                           * get_map_object_def()
11                           */
12 #include "rrand.h" /* for rrand() */
13 #include "command_db.h" /* for get_command_id() */
14
15
16
17 /* Log monster (described by "dsc_monster1") bumping into "monster2". */
18 static void monster_bumps_monster(struct World * world, char * dsc_monster1,
19                                   struct MapObj * monster2);
20
21 /* Decrement player HPs due to attack of monster described by "dsc_monster",
22  * kill player if his HP hit zero; log the whole action.
23  */
24 static void monster_hits_player(struct World * world, char * dsc_monster);
25
26 /* Decrement HP of "monster" hit by player, kill it if its HP hit zero, create a
27  * corpse and increment player's score by the amount of hitpoints the monster
28  * started with; log the whole action.
29  */
30 static void player_hits_monster(struct World * world, struct MapObj * monster);
31
32 /* Try moving the player in direction "d" towards coordinate "target"; log
33  * success or failure of the whole action.
34  */
35 static void try_player_move(struct World * world,
36                             enum dir d, struct yx_uint16 target);
37
38
39
40 static void monster_bumps_monster(struct World * world, char * dsc_monster1,
41                                   struct MapObj * monster2)
42 {
43     char * bump_dsc = " bumps into ";
44     struct MapObjDef * mod = get_map_object_def(world, monster2->type);
45     char msg[strlen(dsc_monster1) + strlen(bump_dsc) + strlen(mod->name) + 3];
46     sprintf(msg, "\n%s%s%s.", dsc_monster1, bump_dsc, mod->name);
47     update_log(world, 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[strlen(dsc_monster) + strlen(hit_dsc) + 3];
56     sprintf(msg, "\n%s%s.", dsc_monster, hit_dsc);
57     update_log(world, msg);
58     world->player->hitpoints--;
59
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 MapObj * monster)
69 {
70     struct MapObjDef * mod = get_map_object_def(world, monster->type);
71     char * hit_dsc = "You hit the ";
72     char * monster_dsc = mod->name;
73     char hitmsg[strlen(hit_dsc) + strlen(monster_dsc) + 3];
74     sprintf(hitmsg, "\n%s%s.", hit_dsc, monster_dsc);
75     update_log(world, hitmsg);
76     monster->lifepoints--;
77     if (0 == monster->lifepoints)
78     {
79         hit_dsc = "You kill the ";
80         char kill_msg[strlen(hit_dsc) + strlen(monster_dsc) + 3];
81         sprintf(kill_msg, "\n%s%s.", hit_dsc, monster_dsc);
82         update_log(world, kill_msg);
83         struct MapObjDef * md = mod;
84         monster->type = md->corpse_id;
85         uint8_t score = md->lifepoints;
86         world->score = world->score + score;
87     }
88 }
89
90
91
92 static void try_player_move(struct World * world,
93                             enum dir d, struct yx_uint16 target)
94 {
95     char * dsc_dir;
96     if      (NORTH == d)
97     {
98         dsc_dir = "north";
99     }
100     else if (EAST  == d)
101     {
102         dsc_dir = "east" ;
103     }
104     else if (SOUTH == d)
105     {
106         dsc_dir = "south";
107     }
108     else if (WEST  == d)
109     {
110         dsc_dir = "west" ;
111     }
112     char * dsc_move = "You fail to move ";
113     if (is_passable(world->map, target))
114     {
115         dsc_move = "You move ";
116         world->player->pos = target;
117     }
118     char msg[strlen(dsc_move) + strlen (dsc_dir) + 3];
119     sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
120     update_log(world, msg);
121 }
122
123
124
125 extern void move_monster(struct World * world, struct MapObj * monster)
126 {
127     char d = rrand() % 5;
128     struct yx_uint16 t = mv_yx_in_dir(d, monster->pos);
129     struct MapObjDef * mod = get_map_object_def(world, monster->type);
130     char * dsc = mod->name;
131     if (yx_uint16_cmp(&t, &world->player->pos))
132     {
133         monster_hits_player(world, dsc);
134         return;
135     }
136     struct MapObj * other_monster;
137     for (other_monster = world->map_objs;
138          other_monster != 0;
139          other_monster = other_monster->next)
140     {
141         if (0 == other_monster->lifepoints || other_monster == monster)
142         {
143             continue;
144         }
145         if (yx_uint16_cmp(&t, &other_monster->pos))
146         {
147             monster_bumps_monster(world, dsc, other_monster);
148             return;
149         }
150     }
151     if (is_passable(world->map, t))
152     {
153         monster->pos = t;
154     }
155 }
156
157
158
159 extern void move_player(struct World * world, enum dir d)
160 {
161     char * action_dsc_prototype = "player_";
162     uint8_t len = strlen(action_dsc_prototype);
163     char action_dsc[len + 2];
164     memcpy(action_dsc, action_dsc_prototype, len);
165     if      (NORTH == d)
166     {
167         action_dsc[len] = 'u';
168     }
169     else if (SOUTH == d)
170     {
171         action_dsc[len] = 'd';
172     }
173     else if (WEST  == d)
174     {
175         action_dsc[len] = 'l';
176     }
177     else if (EAST  == d)
178     {
179         action_dsc[len] = 'r';
180     }
181     action_dsc[len + 1] = '\0';
182     uint8_t action_id = get_command_id(world, action_dsc);
183     struct yx_uint16 t = mv_yx_in_dir(d, world->player->pos);
184     struct MapObj * monster;
185     for (monster = world->map_objs;
186          monster != 0;
187          monster = monster->next)
188     {
189         if (0 < monster->lifepoints && yx_uint16_cmp(&t, &monster->pos))
190         {
191             player_hits_monster(world, monster);
192             turn_over(world, action_id);
193             return;
194           }
195     }
196     try_player_move(world, d, t);
197     turn_over(world, action_id);
198 }
199
200
201
202 extern void player_wait (struct World * world)
203 {
204     update_log(world, "\nYou wait.");
205     turn_over(world, get_command_id(world, "wait"));
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 }