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 "desc_monster1") bumping into "monster2". */
16 static void monster_bumps_monster(struct World * world, char * desc_monster1,
17 struct Monster * monster2);
19 /* Decrement player HPs due to attack of monster described by "desc_monster",
20 * kill player if his HP hit zero; log the whole action.
22 static void monster_hits_player(struct World * world, char * desc_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 * desc_monster1,
38 struct Monster * monster2)
40 char * bumpdesc = " bumps into ";
41 struct MapObjDef * mod = get_map_obj_def(world, monster2->map_obj.type);
42 char * msg = malloc(strlen(desc_monster1) + strlen(bumpdesc)
43 + strlen(mod->desc) + 3);
44 sprintf(msg, "\n%s%s%s.", desc_monster1, bumpdesc, mod->desc);
45 update_log(world, msg);
51 static void monster_hits_player(struct World * world, char * desc_monster)
53 char * hitdesc = " hits you";
54 char * msg = malloc(strlen(desc_monster) + strlen(hitdesc) + 3);
55 sprintf(msg, "\n%s%s.", desc_monster, hitdesc);
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_desc = "You hit the ";
71 char * monster_desc = mod->desc;
72 char * msg = malloc(strlen(hit_desc) + strlen(monster_desc) + 3);
73 sprintf(msg, "\n%s%s.", hit_desc, monster_desc);
74 update_log(world, msg);
78 if (0 == monster->hitpoints)
80 hit_desc = "You kill the ";
81 msg = malloc(strlen(hit_desc) + strlen(monster_desc) + 3);
82 sprintf(msg, "\n%s%s.", hit_desc, monster_desc);
83 update_log(world, msg);
85 if (world->monster == monster)
87 world->monster = world->monster->map_obj.next;
91 struct Monster * m_prev;
92 for (m_prev = world->monster;
93 m_prev->map_obj.next != monster;
94 m_prev = m_prev->map_obj.next);
96 m_prev->map_obj.next = monster->map_obj.next;
105 static void try_player_move(struct World * world,
106 enum dir d, struct yx_uint16 target)
125 char * dsc_move = "You fail to move ";
126 if (is_passable(world->map, target))
128 dsc_move = "You move ";
129 world->player->pos = target;
131 char * msg = malloc(strlen(dsc_move) + strlen (dsc_dir) + 3);
132 sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
133 update_log(world, msg);
139 extern void move_monster(struct World * world, struct Monster * monster)
141 char d = rrand() % 5;
142 struct yx_uint16 t = mv_yx_in_dir(d, monster->map_obj.pos);
143 struct MapObjDef * mod = get_map_obj_def(world, monster->map_obj.type);
144 char * desc = mod->desc;
145 if (yx_uint16_cmp(&t, &world->player->pos))
147 monster_hits_player(world, desc);
150 struct Monster * other_monster;
151 for (other_monster = world->monster;
153 other_monster = other_monster->map_obj.next)
155 if (other_monster == monster)
159 if (yx_uint16_cmp(&t, &other_monster->map_obj.pos))
161 monster_bumps_monster(world, desc, other_monster);
165 if (is_passable(world->map, t))
167 monster->map_obj.pos = t;
173 extern void move_player (struct World * world, enum dir d)
175 struct yx_uint16 t = mv_yx_in_dir(d, world->player->pos);
176 struct Monster * monster;
177 for (monster = world->monster;
179 monster = monster->map_obj.next)
181 if (yx_uint16_cmp(&t, &monster->map_obj.pos))
183 player_hits_monster(world, monster);
188 try_player_move(world, d, t);
194 extern void player_wait (struct World * world)
196 update_log(world, "\nYou wait.");
202 extern char is_passable (struct Map * map, struct yx_uint16 pos)
205 if (0 <= pos.x && pos.x < map->size.x && 0 <= pos.y && pos.y < map->size.y)
207 if ('.' == map->cells[pos.y * map->size.x + pos.x])