1 /* map_object_actions.c */
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() */
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);
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.
22 static void monster_hits_player(struct World * world, char * dsc_monster);
24 /* Decrement HP of "monster" hit by player, kill it if its HP hit zero; log the
27 static void player_hits_monster(struct World * world, struct Monster * monster);
29 /* Try moving the player in direction "d" towards coordinate "target"; log
30 * success or failure of the whole action.
32 static void try_player_move(struct World * world,
33 enum dir d, struct yx_uint16 target);
37 static void monster_bumps_monster(struct World * world, char * dsc_monster1,
38 struct Monster * monster2)
40 char * bump_dsc = " bumps into ";
41 struct MapObjDef * mod = get_map_obj_def(world, monster2->map_obj.type);
42 char * msg = malloc(strlen(dsc_monster1) + strlen(bump_dsc)
43 + strlen(mod->desc) + 3);
44 sprintf(msg, "\n%s%s%s.", dsc_monster1, bump_dsc, mod->desc);
45 update_log(world, msg);
51 static void monster_hits_player(struct World * world, char * dsc_monster)
53 char * hit_dsc = " hits you";
54 char * msg = malloc(strlen(dsc_monster) + strlen(hit_dsc) + 3);
55 sprintf(msg, "\n%s%s.", dsc_monster, hit_dsc);
56 update_log(world, msg);
58 world->player->hitpoints--;
59 if (0 == world->player->hitpoints)
61 update_log(world, "\nYou are dead.");
67 static void player_hits_monster(struct World * world, struct Monster * 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 * msg = malloc(strlen(hit_dsc) + strlen(monster_dsc) + 3);
73 sprintf(msg, "\n%s%s.", hit_dsc, monster_dsc);
74 update_log(world, msg);
77 if (0 == monster->hitpoints)
79 hit_dsc = "You kill the ";
80 msg = malloc(strlen(hit_dsc) + strlen(monster_dsc) + 3);
81 sprintf(msg, "\n%s%s.", hit_dsc, monster_dsc);
82 update_log(world, msg);
84 struct MonsterDef * md = (struct MonsterDef * ) mod;
85 struct Item * corpse = malloc(sizeof(struct Item));
86 corpse->map_obj.type = md->corpse_id;
87 corpse->map_obj.pos = monster->map_obj.pos;
88 corpse->map_obj.next = world->item;
90 if (world->monster == monster)
92 world->monster = world->monster->map_obj.next;
96 struct Monster * m_prev;
97 for (m_prev = world->monster;
98 m_prev->map_obj.next != monster;
99 m_prev = m_prev->map_obj.next);
101 m_prev->map_obj.next = monster->map_obj.next;
104 uint8_t score = md->hitpoints_start;
105 world->score = world->score + score;
112 static void try_player_move(struct World * world,
113 enum dir d, struct yx_uint16 target)
132 char * dsc_move = "You fail to move ";
133 if (is_passable(world->map, target))
135 dsc_move = "You move ";
136 world->player->pos = target;
138 char * msg = malloc(strlen(dsc_move) + strlen (dsc_dir) + 3);
139 sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
140 update_log(world, msg);
146 extern void move_monster(struct World * world, struct Monster * monster)
148 char d = rrand() % 5;
149 struct yx_uint16 t = mv_yx_in_dir(d, monster->map_obj.pos);
150 struct MapObjDef * mod = get_map_obj_def(world, monster->map_obj.type);
151 char * dsc = mod->desc;
152 if (yx_uint16_cmp(&t, &world->player->pos))
154 monster_hits_player(world, dsc);
157 struct Monster * other_monster;
158 for (other_monster = world->monster;
160 other_monster = other_monster->map_obj.next)
162 if (other_monster == monster)
166 if (yx_uint16_cmp(&t, &other_monster->map_obj.pos))
168 monster_bumps_monster(world, dsc, other_monster);
172 if (is_passable(world->map, t))
174 monster->map_obj.pos = t;
180 extern void move_player(struct World * world, enum dir d)
182 struct yx_uint16 t = mv_yx_in_dir(d, world->player->pos);
183 struct Monster * monster;
184 for (monster = world->monster;
186 monster = monster->map_obj.next)
188 if (yx_uint16_cmp(&t, &monster->map_obj.pos))
190 player_hits_monster(world, monster);
195 try_player_move(world, d, t);
201 extern void player_wait (struct World * world)
203 update_log(world, "\nYou wait.");
209 extern char is_passable (struct Map * map, struct yx_uint16 pos)
212 if (0 <= pos.x && pos.x < map->size.x && 0 <= pos.y && pos.y < map->size.y)
214 if ('.' == map->cells[pos.y * map->size.x + pos.x])