<commit_before>#include "game/ui/general/difficultymenu.h"
#include "forms/form.h"
#include "forms/ui.h"
#include "framework/event.h"
#include "framework/framework.h"
#include "framework/keycodes.h"
#include "game/state/city/city.h"
#include "game/state/gamestate.h"
#include "game/ui/city/cityview.h"
#include "game/ui/general/loadingscreen.h"

namespace OpenApoc
{

DifficultyMenu::DifficultyMenu() : Stage(), difficultymenuform(ui().getForm("difficultymenu"))
{
	LogAssert(difficultymenuform);
}

DifficultyMenu::~DifficultyMenu() = default;

void DifficultyMenu::begin() {}

void DifficultyMenu::pause() {}

void DifficultyMenu::resume() {}

void DifficultyMenu::finish() {}

std::future<void> loadGame(const UString &path, sp<GameState> state)
{
	auto loadTask = fw().threadPoolEnqueue([path, state]() -> void {
		if (!state->loadGame("data/gamestate_common"))
		{
			LogError("Failed to load common gamestate");
			return;
		}
		if (!state->loadGame(path))
		{
			LogError("Failed to load '%s'", path.cStr());
			return;
		}
		state->startGame();
		state->initState();
		state->fillPlayerStartingProperty();
		return;
	});

	return loadTask;
}

void DifficultyMenu::eventOccurred(Event *e)
{
	difficultymenuform->eventOccured(e);

	if (e->type() == EVENT_KEY_DOWN)
	{
		if (e->keyboard().KeyCode == SDLK_ESCAPE)
		{
			fw().stageQueueCommand({StageCmd::Command::POP});
			return;
		}
	}

	if (e->type() == EVENT_FORM_INTERACTION && e->forms().EventFlag == FormEventType::ButtonClick)
	{
		UString initialStatePath;
		if (e->forms().RaisedBy->Name.compare("BUTTON_DIFFICULTY1") == 0)
		{
			initialStatePath = "data/difficulty1_patched";
		}
		else if (e->forms().RaisedBy->Name.compare("BUTTON_DIFFICULTY2") == 0)
		{
			initialStatePath = "data/difficulty2_patched";
		}
		else if (e->forms().RaisedBy->Name.compare("BUTTON_DIFFICULTY3") == 0)
		{
			initialStatePath = "data/difficulty3_patched";
		}
		else if (e->forms().RaisedBy->Name.compare("BUTTON_DIFFICULTY4") == 0)
		{
			initialStatePath = "data/difficulty4_patched";
		}
		else if (e->forms().RaisedBy->Name.compare("BUTTON_DIFFICULTY5") == 0)
		{
			initialStatePath = "data/difficulty5_patched";
		}
		else
		{
			LogWarning("Unknown button pressed: %s", e->forms().RaisedBy->Name.cStr());
			return;
		}

		auto loadedState = mksp<GameState>();

		fw().stageQueueCommand(
		    {StageCmd::Command::PUSH,
		     mksp<LoadingScreen>(loadGame(initialStatePath, loadedState),
		                         [loadedState]() { return mksp<CityView>(loadedState); })});
		return;
	}
}

void DifficultyMenu::update() { difficultymenuform->update(); }

void DifficultyMenu::render() { difficultymenuform->render(); }

bool DifficultyMenu::isTransition() { return false; }
}; // namespace OpenApoc<commit_msg>Load initial gamestate relative to the supplied data dir<commit_after>#include "game/ui/general/difficultymenu.h"
#include "forms/form.h"
#include "forms/ui.h"
#include "framework/event.h"
#include "framework/framework.h"
#include "framework/keycodes.h"
#include "game/state/city/city.h"
#include "game/state/gamestate.h"
#include "game/ui/city/cityview.h"
#include "game/ui/general/loadingscreen.h"

namespace OpenApoc
{

DifficultyMenu::DifficultyMenu() : Stage(), difficultymenuform(ui().getForm("difficultymenu"))
{
	LogAssert(difficultymenuform);
}

DifficultyMenu::~DifficultyMenu() = default;

void DifficultyMenu::begin() {}

void DifficultyMenu::pause() {}

void DifficultyMenu::resume() {}

void DifficultyMenu::finish() {}

std::future<void> loadGame(const UString &path, sp<GameState> state)
{
	auto loadTask = fw().threadPoolEnqueue([path, state]() -> void {
		if (!state->loadGame(fw().getDataDir() + "/gamestate_common"))
		{
			LogError("Failed to load common gamestate");
			return;
		}
		if (!state->loadGame(fw().getDataDir() + "/" + path))
		{
			LogError("Failed to load '%s'", path.cStr());
			return;
		}
		state->startGame();
		state->initState();
		state->fillPlayerStartingProperty();
		return;
	});

	return loadTask;
}

void DifficultyMenu::eventOccurred(Event *e)
{
	difficultymenuform->eventOccured(e);

	if (e->type() == EVENT_KEY_DOWN)
	{
		if (e->keyboard().KeyCode == SDLK_ESCAPE)
		{
			fw().stageQueueCommand({StageCmd::Command::POP});
			return;
		}
	}

	if (e->type() == EVENT_FORM_INTERACTION && e->forms().EventFlag == FormEventType::ButtonClick)
	{
		UString initialStatePath;
		if (e->forms().RaisedBy->Name.compare("BUTTON_DIFFICULTY1") == 0)
		{
			initialStatePath = "difficulty1_patched";
		}
		else if (e->forms().RaisedBy->Name.compare("BUTTON_DIFFICULTY2") == 0)
		{
			initialStatePath = "difficulty2_patched";
		}
		else if (e->forms().RaisedBy->Name.compare("BUTTON_DIFFICULTY3") == 0)
		{
			initialStatePath = "difficulty3_patched";
		}
		else if (e->forms().RaisedBy->Name.compare("BUTTON_DIFFICULTY4") == 0)
		{
			initialStatePath = "difficulty4_patched";
		}
		else if (e->forms().RaisedBy->Name.compare("BUTTON_DIFFICULTY5") == 0)
		{
			initialStatePath = "difficulty5_patched";
		}
		else
		{
			LogWarning("Unknown button pressed: %s", e->forms().RaisedBy->Name.cStr());
			return;
		}

		auto loadedState = mksp<GameState>();

		fw().stageQueueCommand(
		    {StageCmd::Command::PUSH,
		     mksp<LoadingScreen>(loadGame(initialStatePath, loadedState),
		                         [loadedState]() { return mksp<CityView>(loadedState); })});
		return;
	}
}

void DifficultyMenu::update() { difficultymenuform->update(); }

void DifficultyMenu::render() { difficultymenuform->render(); }

bool DifficultyMenu::isTransition() { return false; }
}; // namespace OpenApoc
{'fraction_non_alphanumeric': 0.172506256703611, 'fraction_numerical': 0.005362888809438684, 'mean_word_length': 4.883280757097792, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6520471', 'n_tokens_mistral': 2163, 'n_tokens_neox': 1994, 'n_words': 303}