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, create a
25 * corpse and increment player's score by the amount of hitpoints the monster
26 * started with; log the whole action.
28 static void player_hits_monster(struct World * world, struct Monster * monster);
30 /* Try moving the player in direction "d" towards coordinate "target"; log
31 * success or failure of the whole action.
33 static void try_player_move(struct World * world,
34 enum dir d, struct yx_uint16 target);
38 static void monster_bumps_monster(struct World * world, char * dsc_monster1,
39 struct Monster * monster2)
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);
52 static void monster_hits_player(struct World * world, char * dsc_monster)
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);
59 world->player->hitpoints--;
60 if (0 == world->player->hitpoints)
62 update_log(world, "\nYou are dead.");
68 static void player_hits_monster(struct World * world, struct Monster * monster)
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);
78 if (0 == monster->hitpoints)
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);
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;
91 if (world->monster == monster)
93 world->monster = world->monster->map_obj.next;
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);
102 m_prev->map_obj.next = monster->map_obj.next;
105 uint8_t score = md->hitpoints_start;
106 world->score = world->score + score;
113 static void try_player_move(struct World * world,
114 enum dir d, struct yx_uint16 target)
133 char * dsc_move = "You fail to move ";
134 if (is_passable(world->map, target))
136 dsc_move = "You move ";
137 world->player->pos = target;
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);
147 extern void move_monster(struct World * world, struct Monster * monster)
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))
155 monster_hits_player(world, dsc);
158 struct Monster * other_monster;
159 for (other_monster = world->monster;
161 other_monster = other_monster->map_obj.next)
163 if (other_monster == monster)
167 if (yx_uint16_cmp(&t, &other_monster->map_obj.pos))
169 monster_bumps_monster(world, dsc, other_monster);
173 if (is_passable(world->map, t))
175 monster->map_obj.pos = t;
181 extern void move_player(struct World * world, enum dir d)
183 struct yx_uint16 t = mv_yx_in_dir(d, world->player->pos);
184 struct Monster * monster;
185 for (monster = world->monster;
187 monster = monster->map_obj.next)
189 if (yx_uint16_cmp(&t, &monster->map_obj.pos))
191 player_hits_monster(world, monster);
196 try_player_move(world, d, t);
202 extern void player_wait (struct World * world)
204 update_log(world, "\nYou wait.");
210 extern char is_passable (struct Map * map, struct yx_uint16 pos)
213 if (0 <= pos.x && pos.x < map->size.x && 0 <= pos.y && pos.y < map->size.y)
215 if ('.' == map->cells[pos.y * map->size.x + pos.x])