- world->monster = world->monster->map_obj.next;
- else {
- struct Monster * m_prev;
- for (m_prev = world->monster; m_prev->map_obj.next != monster; m_prev = m_prev->map_obj.next);
- m_prev->map_obj.next = monster->map_obj.next; }
- free(monster); }
- turn_over (world, d);
- return; }
- char * msg_content = "You fail to move";
- char * dir;
- if (NORTH == d) dir = "north";
- else if (EAST == d) dir = "east" ;
- else if (SOUTH == d) dir = "south";
- else if (WEST == d) dir = "west" ;
- if (is_passable(world->map, t)) {
- msg_content = "You move";
- world->player->pos = t; }
- sprintf(msg, "\n%s %s.", msg_content, dir);
- update_log (world, msg);
- free(msg);
- turn_over (world, d); }
-
-extern void player_wait (struct World * world) {
-// Make player wait one turn.
- update_log (world, "\nYou wait.");
- turn_over (world, 0); }
+ {
+ world->monster = world->monster->map_obj.next;
+ }
+ else
+ {
+ struct Monster * m_prev;
+ for (m_prev = world->monster;
+ m_prev->map_obj.next != monster;
+ m_prev = m_prev->map_obj.next);
+ {
+ m_prev->map_obj.next = monster->map_obj.next;
+ }
+ }
+ free(monster);
+ }
+}
+
+
+
+static void try_player_move(struct World * world,
+ enum dir d, struct yx_uint16 target)
+{
+ char * dsc_dir;
+ if (NORTH == d)
+ {
+ dsc_dir = "north";
+ }
+ else if (EAST == d)
+ {
+ dsc_dir = "east" ;
+ }
+ else if (SOUTH == d)
+ {
+ dsc_dir = "south";
+ }
+ else if (WEST == d)
+ {
+ dsc_dir = "west" ;
+ }
+ char * dsc_move = "You fail to move ";
+ if (is_passable(world->map, target))
+ {
+ dsc_move = "You move ";
+ world->player->pos = target;
+ }
+ char * msg = malloc(strlen(dsc_move) + strlen (dsc_dir) + 3);
+ sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
+ update_log(world, msg);
+ free(msg);
+}
+
+
+
+extern void move_monster(struct World * world, struct Monster * monster)
+{
+ char d = rrand() % 5;
+ struct yx_uint16 t = mv_yx_in_dir(d, monster->map_obj.pos);
+ struct MapObjDef * mod = get_map_obj_def(world, monster->map_obj.type);
+ char * desc = mod->desc;
+ if (yx_uint16_cmp(&t, &world->player->pos))
+ {
+ monster_hits_player(world, desc);
+ return;
+ }
+ struct Monster * other_monster;
+ for (other_monster = world->monster;
+ other_monster != 0;
+ other_monster = other_monster->map_obj.next)
+ {
+ if (other_monster == monster)
+ {
+ continue;
+ }
+ if (yx_uint16_cmp(&t, &other_monster->map_obj.pos))
+ {
+ monster_bumps_monster(world, desc, other_monster);
+ return;
+ }
+ }
+ if (is_passable(world->map, t))
+ {
+ monster->map_obj.pos = t;
+ }
+}
+
+
+
+extern void move_player (struct World * world, enum dir d)
+{
+ struct yx_uint16 t = mv_yx_in_dir(d, world->player->pos);
+ struct Monster * monster;
+ for (monster = world->monster;
+ monster != 0;
+ monster = monster->map_obj.next)
+ {
+ if (yx_uint16_cmp(&t, &monster->map_obj.pos))
+ {
+ player_hits_monster(world, monster);
+ turn_over(world, d);
+ return;
+ }
+ }
+ try_player_move(world, d, t);
+ turn_over(world, d);
+}
+
+
+
+extern void player_wait (struct World * world)
+{
+ update_log(world, "\nYou wait.");
+ turn_over(world, 0);
+}
+
+
+
+extern char is_passable (struct Map * map, struct yx_uint16 pos)
+{
+ char passable = 0;
+ if (0 <= pos.x && pos.x < map->size.x && 0 <= pos.y && pos.y < map->size.y)
+ {
+ if ('.' == map->cells[pos.y * map->size.x + pos.x])
+ {
+ passable = 1;
+ }
+ }
+ return passable;
+}
+