void GameStatePlay::checkTeleport() {

	// both map events and player powers can cause teleportation
	if (mapr->teleportation || pc->stats.teleportation) {

		mapr->collider.unblock(pc->stats.pos.x, pc->stats.pos.y);

		if (mapr->teleportation) {
			mapr->cam.x = pc->stats.pos.x = mapr->teleport_destination.x;
			mapr->cam.y = pc->stats.pos.y = mapr->teleport_destination.y;
		}
		else {
			mapr->cam.x = pc->stats.pos.x = pc->stats.teleport_destination.x;
			mapr->cam.y = pc->stats.pos.y = pc->stats.teleport_destination.y;
		}

		// if we're not changing map, move allies to a the player's new position
		// when changing maps, enemies->handleNewMap() does something similar to this
		if (mapr->teleport_mapname == "") {
			FPoint spawn_pos = mapr->collider.get_random_neighbor(floor(pc->stats.pos), 1, false);
			for (unsigned int i=0; i < enemies->enemies.size(); i++) {
				if(enemies->enemies[i]->stats.hero_ally && enemies->enemies[i]->stats.alive) {
					mapr->collider.unblock(enemies->enemies[i]->stats.pos.x, enemies->enemies[i]->stats.pos.y);
					enemies->enemies[i]->stats.pos = spawn_pos;
					mapr->collider.block(enemies->enemies[i]->stats.pos.x, enemies->enemies[i]->stats.pos.y, true);
				}
			}
		}

		// process intermap teleport
		if (mapr->teleportation && mapr->teleport_mapname != "") {
			std::string teleport_mapname = mapr->teleport_mapname;
			mapr->teleport_mapname = "";
			inpt->lock_all = (teleport_mapname == "maps/spawn.txt");
			mapr->executeOnMapExitEvents();
			showLoading();
			mapr->load(teleport_mapname);
			setLoadingFrame();
			enemies->handleNewMap();
			hazards->handleNewMap();
			loot->handleNewMap();
			powers->handleNewMap(&mapr->collider);
			menu->enemy->handleNewMap();
			npcs->handleNewMap();
			menu->npc->setNPC(NULL);
			menu->vendor->setNPC(NULL);
			menu->talker->setNPC(NULL);
			menu->stash->visible = false;
			menu->mini->prerender(&mapr->collider, mapr->w, mapr->h);
			npc_id = nearest_npc = -1;

			// store this as the new respawn point (provided the tile is open)
			if (mapr->collider.is_valid_position(pc->stats.pos.x, pc->stats.pos.y, MOVEMENT_NORMAL, true)) {
				mapr->respawn_map = teleport_mapname;
				mapr->respawn_point = pc->stats.pos;
			}
			else {
				logError("GameStatePlay: Spawn position (%d, %d) is blocked.", static_cast<int>(pc->stats.pos.x), static_cast<int>(pc->stats.pos.y));
			}

			// return to title (permadeath) OR auto-save
			if (pc->stats.permadeath && pc->stats.corpse) {
				removeSaveDir(save_load->getGameSlot());

				snd->stopMusic();
				delete requestedGameState;
				requestedGameState = new GameStateTitle();
			}
			else if (SAVE_ONLOAD) {
				save_load->saveGame();
			}
		}

		mapr->collider.block(pc->stats.pos.x, pc->stats.pos.y, false);

		pc->stats.teleportation = false; // teleport spell

	}

	if (mapr->teleport_mapname == "") mapr->teleportation = false;
}
void GameStateLoad::logic() {

	if (inpt->window_resized)
		refreshWidgets();

	for (size_t i = 0; i < game_slots.size(); ++i) {
		if (static_cast<int>(i) == selected_slot) {
			if (game_slots[i]->preview_turn_ticks > 0)
				game_slots[i]->preview_turn_ticks--;

			if (game_slots[i]->preview_turn_ticks == 0) {
				game_slots[i]->preview_turn_ticks = GAMESLOT_PREVIEW_TURN_DURATION;

				game_slots[i]->stats.direction++;
				if (game_slots[i]->stats.direction > 7)
					game_slots[i]->stats.direction = 0;
			}
		}
		game_slots[i]->preview.logic();
	}

	if (!confirm->visible) {
		tablist.logic(true);
		if (button_exit->checkClick() || (inpt->pressing[CANCEL] && !inpt->lock[CANCEL])) {
			inpt->lock[CANCEL] = true;
			showLoading();
			setRequestedGameState(new GameStateTitle());
		}

		if (loading_requested) {
			loading = true;
			loading_requested = false;
			logicLoading();
		}

		bool outside_scrollbar = true;

		if (button_new->checkClick()) {
			// create a new game
			showLoading();
			GameStateNew* newgame = new GameStateNew();
			newgame->game_slot = (game_slots.empty() ? 1 : game_slots.back()->id+1);
			delete_items = false;
			setRequestedGameState(newgame);
		}
		else if (button_load->checkClick()) {
			loading_requested = true;
		}
		else if (button_delete->checkClick()) {
			// Display pop-up to make sure save should be deleted
			confirm->visible = true;
			confirm->render();
		}
		else if (game_slots.size() > 0) {
			Rect scroll_area = slot_pos[0];
			scroll_area.h = slot_pos[0].h * game_slot_max;

			if (isWithinRect(scroll_area, inpt->mouse)) {
				if (inpt->pressing[MAIN1] && !inpt->lock[MAIN1]) {
					for (int i=0; i<visible_slots; ++i) {
						if (isWithinRect(slot_pos[i], inpt->mouse)) {
							inpt->lock[MAIN1] = true;
							setSelectedSlot(i + scroll_offset);
							updateButtons();
							break;
						}
					}
				}
				else if (inpt->scroll_up) {
					scrollUp();
				}
				else if (inpt->scroll_down) {
					scrollDown();
				}
			}
			else if (has_scroll_bar) {
				switch (scrollbar->checkClick(inpt->mouse.x, inpt->mouse.y)) {
					case 1:
						scrollUp();
						outside_scrollbar = false;
						break;
					case 2:
						scrollDown();
						outside_scrollbar = false;
						break;
					case 3:
						scroll_offset = scrollbar->getValue();
						if (scroll_offset >= static_cast<int>(game_slots.size()) - visible_slots) {
							scroll_offset = static_cast<int>(game_slots.size()) - visible_slots;
						}
						outside_scrollbar = false;
						break;
					default:
						break;
				}
			}

			if (outside_scrollbar && inpt->pressing[MAIN1] && !inpt->lock[MAIN1]) {
				inpt->lock[MAIN1] = true;
				setSelectedSlot(-1);
				updateButtons();
			}

			// Allow characters to be navigateable via up/down keys
			if (inpt->pressing[UP] && !inpt->lock[UP]) {
				inpt->lock[UP] = true;
				setSelectedSlot((selected_slot - 1 < 0) ? static_cast<int>(game_slots.size()) - 1 : selected_slot - 1);
				scroll_offset = std::min(static_cast<int>(game_slots.size()) - visible_slots, selected_slot);
				updateButtons();
			}
			else if (inpt->pressing[DOWN] && !inpt->lock[DOWN]) {
				inpt->lock[DOWN] = true;
				setSelectedSlot((selected_slot + 1 == static_cast<int>(game_slots.size())) ? 0 : selected_slot + 1);
				scroll_offset = std::max(0, selected_slot-visible_slots+1);
				updateButtons();
			}
		}
	}
	else if (confirm->visible) {
		confirm->logic();
		if (confirm->confirmClicked) {
			removeSaveDir(game_slots[selected_slot]->id);

			delete game_slots[selected_slot];
			game_slots[selected_slot] = NULL;
			game_slots.erase(game_slots.begin()+selected_slot);

			visible_slots = (game_slot_max > static_cast<int>(game_slots.size()) ? static_cast<int>(game_slots.size()) : game_slot_max);
			setSelectedSlot(-1);

			while (scroll_offset + visible_slots > static_cast<int>(game_slots.size())) {
				scroll_offset--;
			}

			updateButtons();

			confirm->visible = false;
			confirm->confirmClicked = false;
		}
	}
}