+void MapState::OnTileLock() {
+ // moveTimer is running when the pc is walking against a wall
+ if (moveTimer.Running() && !moveTimer.JustHit()) return;
+
+ Vector<int> nowLock(ToInt(controlled->Position()));
+ bool event(false);
+ if (nowLock != lastLock) {
+ event = OnGridLock();
+ afterLock = true;
+ moveTimer.Clear();
+ } else if (moveTimer.JustHit()) {
+ event = OnGridLock();
+ afterLock = true;
+ }
+
+ if (event) {
+ return;
+ }
+
+ const Tile *tile(map->TileAt(nowLock));
+
+ if (nextDirection >= 0) {
+ if (afterLock) {
+ bool blocked(CheckBlocking());
+ OnMove(!blocked);
+ controlled->SetDirection(Entity::Orientation(nextDirection));
+ if (!blocked) {
+ afterLock = false;
+ controlled->SetSpeed(walkingSpeed);
+ moveTimer.Clear();
+ if (pushed) {
+ pushed->SetDirection(Entity::Orientation(nextDirection));
+ pushed->SetSpeed(walkingSpeed);
+ controlled->SetPushing();
+ } else {
+ controlled->SetHandsFree();
+ }
+ } else {
+ controlled->SetSpeed(0);
+ StopFollowers(*controlled);
+ if (!moveTimer.Running()) {
+ int tileSize((controlled->GetDirection() % 2) ? map->Tileset()->Width() : map->Tileset()->Height());
+ Fixed<8> walkingInterval(tileSize);
+ walkingInterval /= walkingSpeed;
+ moveTimer = PhysicsTimers().StartInterval(walkingInterval.Int());
+ }
+ pushed = 0;
+ }
+ if (!controlled->AnimationRunning()) {
+ controlled->StartAnimation(*this);
+ }
+ }
+ } else {
+ controlled->SetSpeed(0);
+ StopFollowers(*controlled);
+ controlled->StopAnimation();
+ moveTimer.Clear();
+ if (pushed) {
+ pushed->SetSpeed(0);
+ pushed = 0;
+ }
+ }
+
+ if (controlled->GetDirection() == Entity::ORIENTATION_SOUTH
+ && tile && tile->IsLadder()) {
+ controlled->SetOrientation(Entity::ORIENTATION_NORTH);
+ }
+
+ lastLock = nowLock;
+}
+
+bool MapState::CheckBlocking() {
+ if (pushed) {
+ pushed->SetSpeed(0);
+ pushed = 0;
+ }
+ const Tile *tile(map->TileAt(ToInt(controlled->Position())));
+ Vector<int> direction;
+ switch (nextDirection) {
+ case Entity::ORIENTATION_NORTH:
+ if (tile && tile->BlocksNorth()) {
+ return true;
+ } else {
+ direction = Vector<int>(0, -map->Tileset()->Height());
+ }
+ break;
+ case Entity::ORIENTATION_EAST:
+ if (tile && tile->BlocksEast()) {
+ return true;
+ } else {
+ direction = Vector<int>(map->Tileset()->Width(), 0);
+ }
+ break;
+ case Entity::ORIENTATION_SOUTH:
+ if (tile && tile->BlocksSouth()) {
+ return true;
+ } else {
+ direction = Vector<int>(0, map->Tileset()->Height());
+ }
+ break;
+ case Entity::ORIENTATION_WEST:
+ if (tile && tile->BlocksWest()) {
+ return true;
+ } else {
+ direction = Vector<int>(-map->Tileset()->Width(), 0);
+ }
+ break;
+ default:
+ return false;
+ }
+ Vector<int> nextTilePosition(direction + ToInt(controlled->Position()));
+ Vector<int> nextTileCoords(map->TileCoordinates(nextTilePosition));
+ for (std::vector<Entity *>::const_iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
+ const Entity &e(**i);
+ if (map->TileCoordinates(ToInt(e.Position())) != nextTileCoords) continue;
+ if (!e.Blocking()) continue;
+ if (!pushing || !e.Pushable()) return true;
+ if (CheckBlocking(nextTilePosition, Entity::Orientation(nextDirection))) return true;
+ pushed = *i;
+ }
+ return false;
+}
+
+bool MapState::CheckBlocking(const Vector<int> &position, Entity::Orientation direction) const {
+ const Tile *tile(map->TileAt(position));
+ Vector<int> directionVector;
+ switch (direction) {
+ case Entity::ORIENTATION_NORTH:
+ if (tile && tile->BlocksNorth()) {
+ return true;
+ } else {
+ directionVector = Vector<int>(0, -map->Tileset()->Height());
+ }
+ break;
+ case Entity::ORIENTATION_EAST:
+ if (tile && tile->BlocksEast()) {
+ return true;
+ } else {
+ directionVector = Vector<int>(map->Tileset()->Width(), 0);
+ }
+ break;
+ case Entity::ORIENTATION_SOUTH:
+ if (tile && tile->BlocksSouth()) {
+ return true;
+ } else {
+ directionVector = Vector<int>(0, map->Tileset()->Height());
+ }
+ break;
+ case Entity::ORIENTATION_WEST:
+ if (tile && tile->BlocksWest()) {
+ return true;
+ } else {
+ directionVector = Vector<int>(-map->Tileset()->Width(), 0);
+ }
+ break;
+ default:
+ return false;
+ }
+ Vector<int> nextTileCoords(map->TileCoordinates(directionVector + position));
+ for (std::vector<Entity *>::const_iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
+ const Entity &e(**i);
+ if (map->TileCoordinates(ToInt(e.Position())) == nextTileCoords && e.Blocking()) {
+ return true;
+ }
+ }
+ return false;
+}
+
+bool MapState::OnGridLock() {
+ if (skipLock) {
+ skipLock = false;
+ return false;
+ } else {
+ LockEntities();
+ return CheckMonster() || CheckLockTrigger();
+ }
+}
+
+void MapState::LockEntities() {
+ for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
+ if (*i == controlled) {
+ // don't lock player
+ continue;
+ }
+ (*i)->Position().Lock(map->Tileset()->Size());
+ }
+}
+
+bool MapState::CheckMonster() {
+ Vector<int> coords(map->TileCoordinates(ToInt(controlled->Position())));
+ Vector<int> neighbor[4];
+ neighbor[0] = Vector<int>(coords.X(), coords.Y() - 1); // N
+ neighbor[1] = Vector<int>(coords.X() + 1, coords.Y()); // E
+ neighbor[2] = Vector<int>(coords.X(), coords.Y() + 1); // S
+ neighbor[3] = Vector<int>(coords.X() - 1, coords.Y()); // W
+
+ for (int i(0); i < 4; ++i) {
+ for (std::vector<Entity *>::iterator e(entities.begin()), end(entities.end()); e != end; ++e) {
+ if ((*e)->Hostile() && map->TileCoordinates(ToInt((*e)->Position())) == neighbor[i]) {
+ // TODO: move entity erase to happen after the transition or battle
+ LoadBattle(*controlled, **e);
+ entities.erase(e);
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
+void MapState::LoadBattle(Entity &hero, Entity &monster) {
+ SDL_Surface *bg = map->BattleBackgroundAt(ToInt(monster.Position()));
+ BattleState *battleState(new BattleState(game, bg, monster.PartyLayout()));
+ for (int i(0); i < 4; ++i) {
+ if (game->state->party[i]) {
+ battleState->AddHero(*game->state->party[i]);
+ }
+ }
+ if (game->state->capsule) {
+ battleState->SetCapsule(&game->state->GetCapsule());
+ }
+ for (battle::Monster **m(monster.MonstersBegin()); m != monster.MonstersEnd(); ++m) {
+ battleState->AddMonster(**m);
+ }
+
+ // TODO: pass turn advantage to battle, see #26
+ Entity::Orientation faceDirection;
+ if (monster.Position().Y() < hero.Position().Y()) {
+ faceDirection = Entity::ORIENTATION_NORTH;
+ } else if (monster.Position().X() > hero.Position().X()) {
+ faceDirection = Entity::ORIENTATION_EAST;
+ } else if (monster.Position().Y() > hero.Position().Y()) {
+ faceDirection = Entity::ORIENTATION_SOUTH;
+ } else {
+ faceDirection = Entity::ORIENTATION_WEST;
+ }
+ if (hero.GetOrientation() == monster.GetOrientation()
+ && hero.GetOrientation() == faceDirection) {
+ // advantage hero
+ } else if (hero.GetOrientation() == monster.GetOrientation()
+ && hero.GetOrientation() == ((faceDirection + 2) % 4)) {
+ // advantage monster
+ } else if (((monster.GetOrientation() == faceDirection && (hero.GetOrientation() % 2) != (faceDirection % 2))
+ || (hero.GetOrientation() == faceDirection && (monster.GetOrientation() % 2) != (faceDirection % 2)))
+ && rand() % 2) {
+ // 50% advantage chance hero
+ } else if ((monster.GetOrientation() == (faceDirection + 2) % 4)
+ && ((hero.GetOrientation() % 2) != (faceDirection % 2))
+ && rand() % 2) {
+ // 50% advantage chance monster
+ }
+
+ // TODO: other transition
+ ColorFade *fadeIn(new ColorFade(this, 0, 500, true));
+ fadeIn->SetLeadInTime(500);
+ ColorFade *fadeOut(new ColorFade(this, 0, 500));
+ fadeOut->SetLeadOutTime(500);
+
+ Ctrl().PushState(fadeIn);
+ Ctrl().PushState(battleState);
+ Ctrl().PushState(fadeOut);
+}
+
+bool MapState::CheckLockTrigger() {
+ Trigger *trigger(map->TriggerAt(ToInt(controlled->Position())));
+ if (!trigger || trigger->GetType() != Trigger::TYPE_CONTACT) return false;
+ RunTrigger(*trigger);
+ return true;
+}
+
+void MapState::OnMove(bool realMove) {
+ if (CheckMoveTrigger()) {
+ return;
+ }
+ // TODO: evaluate monster movements
+ if (realMove) {
+ UpdateFollower(*controlled);
+ } else {
+ StopFollowers(*controlled);
+ }
+}
+
+bool MapState::CheckMoveTrigger() {
+ Trigger *trigger(map->TriggerAt(ToInt(controlled->Position())));
+ if (!trigger || int(trigger->GetType()) != nextDirection) return false;
+ RunTrigger(*trigger);
+ return true;
+}
+
+void MapState::RunTrigger(Trigger &trigger) {
+ if (!trigger.HasScript()) return;
+ runner.Run(*this, trigger.GetScript());
+}
+
+void MapState::UpdateFollower(Entity &e) {
+ if (!e.Follower()) return;
+
+ Entity &f(*e.Follower());
+ UpdateFollower(f);
+
+ Vector<int> coords(map->TileCoordinates(ToInt(e.Position())));
+ Vector<int> fCoords(map->TileCoordinates(ToInt(f.Position())));
+ Vector<int> direction(coords - fCoords);
+
+ if (direction.Y() < 0) {
+ f.SetDirection(Entity::ORIENTATION_NORTH);
+ f.SetSpeed(walkingSpeed);
+ f.StartAnimation(*this);
+ } else if (direction.X() > 0) {
+ f.SetDirection(Entity::ORIENTATION_EAST);
+ f.SetSpeed(walkingSpeed);
+ f.StartAnimation(*this);
+ } else if (direction.Y() > 0) {
+ f.SetDirection(Entity::ORIENTATION_SOUTH);
+ f.SetSpeed(walkingSpeed);
+ f.StartAnimation(*this);
+ } else if (direction.X() < 0) {
+ f.SetDirection(Entity::ORIENTATION_WEST);
+ f.SetSpeed(walkingSpeed);
+ f.StartAnimation(*this);
+ } else {
+ f.SetSpeed(0);
+ f.StopAnimation();
+ }
+}
+
+void MapState::StopFollowers(Entity &e) {
+ for (Entity *f(e.Follower()); f; f = f->Follower()) {
+ f->SetSpeed(0);
+ f->StopAnimation();
+ }
+}
+
+
+void MapState::Transition(Map *newMap, const Vector<int> &coordinates) {
+ UnloadMap();
+ Vector<int> position(coordinates * map->Tileset()->Size());
+ for (Entity *e(controlled); e; e = e->Follower()) {
+ e->Position() = position;
+ e->SetDirection(controlled->GetDirection());
+ }
+ LoadMap(newMap);
+ skipLock = true;
+}
+
+void MapState::UnloadMap() {
+ entities.clear();
+}
+
+void MapState::LoadMap(Map *m) {
+ map = m;
+ for (Entity *e(m->EntitiesBegin()), *end(m->EntitiesEnd()); e != end; ++e) {
+ entities.push_back(e);
+ e->ResetPosition(map->Tileset()->Size());
+ }
+ for (Entity *e(controlled); e; e = e->Follower()) {
+ entities.push_back(e);
+ }
+}
+
+