1 /* map_object_actions.c */
3 #include "map_object_actions.h"
4 #include <stdlib.h> /* for malloc(), calloc(), free() */
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 map object (definition) structs */
10 #include "rrand.h" /* for rrand() */
14 extern void move_monster(struct World * world, struct Monster * monster)
17 struct yx_uint16 t = mv_yx_in_dir(d, monster->map_obj.pos);
18 char * msg = malloc(100);
19 struct MapObjDef * mod = get_map_obj_def(world, monster->map_obj.type);
20 char * desc = mod->desc;
22 if (yx_uint16_cmp(t, world->player->pos))
24 sprintf(msg, "\nThe %s hits you.", desc);
25 update_log(world, msg);
26 world->player->hitpoints--;
27 if (0 == world->player->hitpoints)
29 update_log(world, "\nYou are dead.");
33 struct Monster * other_monster;
34 for (other_monster = world->monster;
36 other_monster = other_monster->map_obj.next)
38 if (other_monster == monster)
42 if (yx_uint16_cmp(t, other_monster->map_obj.pos))
44 mod = get_map_obj_def(world, other_monster->map_obj.type);
45 desc_other = mod->desc;
46 sprintf(msg, "\n%s bumps into %s.", desc, desc_other);
47 update_log(world, msg);
52 if (is_passable(world->map, t))
54 monster->map_obj.pos = t;
60 extern void move_player (struct World * world, enum dir d)
62 struct yx_uint16 t = mv_yx_in_dir(d, world->player->pos);
63 struct Monster * monster;
64 struct MapObjDef * mod;
65 char * msg = calloc(100, sizeof(char));
67 for (monster = world->monster;
69 monster = monster->map_obj.next)
71 if (yx_uint16_cmp(t, monster->map_obj.pos))
73 mod = get_map_obj_def(world, monster->map_obj.type);
75 sprintf(msg, "\nYou hit the %s.", desc);
76 update_log(world, msg);
78 if (0 == monster->hitpoints)
80 sprintf(msg, "\nYou kill the %s.", desc);
81 update_log(world, msg);
82 if (world->monster == monster)
84 world->monster = world->monster->map_obj.next;
88 struct Monster * m_prev;
89 for (m_prev = world->monster;
90 m_prev->map_obj.next != monster;
91 m_prev = m_prev->map_obj.next);
92 m_prev->map_obj.next = monster->map_obj.next;
100 char * msg_content = "You fail to move";
118 if (is_passable(world->map, t))
120 msg_content = "You move";
121 world->player->pos = t;
123 sprintf(msg, "\n%s %s.", msg_content, dir);
124 update_log(world, msg);
131 extern void player_wait (struct World * world)
133 update_log(world, "\nYou wait.");
139 extern char is_passable (struct Map * map, struct yx_uint16 pos)
142 if (0 <= pos.x && pos.x < map->size.x && 0 <= pos.y && pos.y < map->size.y)
144 if ('.' == map->cells[pos.y * map->size.x + pos.x])