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 rrand(), 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 */
13 extern void move_monster(struct World * world, struct Monster * monster)
15 char d = rrand(0, 0) % 5;
16 struct yx_uint16 t = mv_yx_in_dir(d, monster->map_obj.pos);
17 char * msg = malloc(100);
18 struct MapObjDef * mod = get_map_obj_def(world, monster->map_obj.type);
19 char * desc = mod->desc;
21 if (yx_uint16_cmp(t, world->player->pos))
23 sprintf(msg, "\nThe %s hits you.", desc);
24 update_log(world, msg);
25 world->player->hitpoints--;
26 if (0 == world->player->hitpoints)
28 update_log(world, "\nYou are dead.");
32 struct Monster * other_monster;
33 for (other_monster = world->monster;
35 other_monster = other_monster->map_obj.next)
37 if (other_monster == monster)
41 if (yx_uint16_cmp(t, other_monster->map_obj.pos))
43 mod = get_map_obj_def(world, other_monster->map_obj.type);
44 desc_other = mod->desc;
45 sprintf(msg, "\n%s bumps into %s.", desc, desc_other);
46 update_log(world, msg);
51 if (is_passable(world->map, t))
53 monster->map_obj.pos = t;
59 extern void move_player (struct World * world, enum dir d)
61 struct yx_uint16 t = mv_yx_in_dir(d, world->player->pos);
62 struct Monster * monster;
63 struct MapObjDef * mod;
64 char * msg = calloc(100, sizeof(char));
66 for (monster = world->monster;
68 monster = monster->map_obj.next)
70 if (yx_uint16_cmp(t, monster->map_obj.pos))
72 mod = get_map_obj_def(world, monster->map_obj.type);
74 sprintf(msg, "\nYou hit the %s.", desc);
75 update_log(world, msg);
77 if (0 == monster->hitpoints)
79 sprintf(msg, "\nYou kill the %s.", desc);
80 update_log(world, msg);
81 if (world->monster == monster)
83 world->monster = world->monster->map_obj.next;
87 struct Monster * m_prev;
88 for (m_prev = world->monster;
89 m_prev->map_obj.next != monster;
90 m_prev = m_prev->map_obj.next);
91 m_prev->map_obj.next = monster->map_obj.next;
99 char * msg_content = "You fail to move";
117 if (is_passable(world->map, t))
119 msg_content = "You move";
120 world->player->pos = t;
122 sprintf(msg, "\n%s %s.", msg_content, dir);
123 update_log(world, msg);
130 extern void player_wait (struct World * world)
132 update_log(world, "\nYou wait.");
138 extern char is_passable (struct Map * map, struct yx_uint16 pos)
141 if (0 <= pos.x && pos.x < map->size.x && 0 <= pos.y && pos.y < map->size.y)
143 if ('.' == map->cells[pos.y * map->size.x + pos.x])