Skip to content

Commit

Permalink
Make Inn Sequence asynchronous
Browse files Browse the repository at this point in the history
  • Loading branch information
mateofio committed Sep 3, 2019
1 parent 9f66fdd commit 36ede65
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 60 deletions.
8 changes: 8 additions & 0 deletions src/async_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class AsyncOp {
eNone,
eShowScreen,
eEraseScreen,
eCallInn,
eToTitle,
eExitGame
};
Expand All @@ -46,6 +47,9 @@ class AsyncOp {
/** @return an EraseScreen async operation */
static AsyncOp MakeEraseScreen(int transition_type);

/** @return an CallInn async operation */
static AsyncOp MakeCallInn();

/** @return a ToTitle async operation */
static AsyncOp MakeToTitle();

Expand Down Expand Up @@ -99,6 +103,10 @@ inline AsyncOp AsyncOp::MakeEraseScreen(int transition_type) {
return AsyncOp(eEraseScreen, transition_type);
}

inline AsyncOp AsyncOp::MakeCallInn() {
return AsyncOp(eCallInn);
}

inline AsyncOp AsyncOp::MakeToTitle() {
return AsyncOp(eToTitle);
}
Expand Down
6 changes: 5 additions & 1 deletion src/game_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,11 @@ void Game_Interpreter::Update(bool reset_loop_count) {
}
}

// continuation triggered an async operation.
if (IsAsyncPending()) {
break;
}

if (Game_Map::GetNeedRefresh()) {
Game_Map::Refresh();
}
Expand Down Expand Up @@ -3333,6 +3338,5 @@ bool Game_Interpreter::DefaultContinuation(RPG::EventCommand const& /* com */) {

bool Game_Interpreter::ContinuationOpenShop(RPG::EventCommand const& /* com */) { return true; }
bool Game_Interpreter::ContinuationShowInnStart(RPG::EventCommand const& /* com */) { return true; }
bool Game_Interpreter::ContinuationShowInnFinish(RPG::EventCommand const& /* com */) { return true; }
bool Game_Interpreter::ContinuationEnemyEncounter(RPG::EventCommand const& /* com */) { return true; }

1 change: 0 additions & 1 deletion src/game_interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ class Game_Interpreter
virtual bool ContinuationChoices(RPG::EventCommand const& com);
virtual bool ContinuationOpenShop(RPG::EventCommand const& com);
virtual bool ContinuationShowInnStart(RPG::EventCommand const& com);
virtual bool ContinuationShowInnFinish(RPG::EventCommand const& com);
virtual bool ContinuationEnemyEncounter(RPG::EventCommand const& com);

int DecodeInt(std::vector<int32_t>::const_iterator& it);
Expand Down
60 changes: 4 additions & 56 deletions src/game_interpreter_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,7 @@ bool Game_Interpreter_Map::CommandShowInn(RPG::EventCommand const& com) { // cod
if (Game_Temp::inn_price == 0) {
// Skip prompt.
Game_Message::choice_result = 0;
SetContinuation(static_cast<ContinuationFunction>(&Game_Interpreter_Map::ContinuationShowInnStart));
return false;
return ContinuationShowInnStart(com);
}

Game_Message::message_waiting = true;
Expand Down Expand Up @@ -462,7 +461,7 @@ bool Game_Interpreter_Map::CommandShowInn(RPG::EventCommand const& com) { // cod
// save game compatibility with RPG_RT
ReserveSubcommandIndex(com.indent);

return false;
return true;
}

bool Game_Interpreter_Map::ContinuationShowInnStart(RPG::EventCommand const& com) {
Expand All @@ -484,64 +483,13 @@ bool Game_Interpreter_Map::ContinuationShowInnStart(RPG::EventCommand const& com
if (inn_stay) {
Main_Data::game_party->GainGold(-Game_Temp::inn_price);

// Full heal
std::vector<Game_Actor*> actors = Main_Data::game_party->GetActors();
for (Game_Actor* actor : actors) {
actor->FullHeal();
}
Graphics::GetTransition().Init(Transition::TransitionFadeOut, Scene::instance.get(), 36, true);
Game_System::BgmFade(800);
SetContinuation(static_cast<ContinuationFunction>(&Game_Interpreter_Map::ContinuationShowInnContinue));
return false;
_async_op = AsyncOp::MakeCallInn();
return true;
}

++index;
return true;
}

bool Game_Interpreter_Map::ContinuationShowInnContinue(RPG::EventCommand const& /* com */) {
if (Graphics::IsTransitionPending())
return false;

const RPG::Music& bgm_inn = Game_System::GetSystemBGM(Game_System::BGM_Inn);
// FIXME: Abusing before_battle_music (Which is unused when calling an Inn)
// Is there also before_inn_music in the savegame?
Main_Data::game_data.system.before_battle_music = Game_System::GetCurrentBGM();

Game_System::BgmPlay(bgm_inn);

SetContinuation(static_cast<ContinuationFunction>(&Game_Interpreter_Map::ContinuationShowInnFinish));

return false;
}

bool Game_Interpreter_Map::ContinuationShowInnFinish(RPG::EventCommand const& com) {
auto* frame = GetFrame();
assert(frame);
auto& index = frame->current_command;

if (Graphics::IsTransitionPending())
return false;

const RPG::Music& bgm_inn = Game_System::GetSystemBGM(Game_System::BGM_Inn);
if (bgm_inn.name.empty() ||
bgm_inn.name == "(OFF)" ||
bgm_inn.name == "(Brak)" ||
!Audio().BGM_IsPlaying() ||
Audio().BGM_PlayedOnce()) {

Game_System::BgmStop();
continuation = NULL;
Graphics::GetTransition().Init(Transition::TransitionFadeIn, Scene::instance.get(), 36, false);
Game_System::BgmPlay(Main_Data::game_data.system.before_battle_music);

++index;
return false;
}

return false;
}

bool Game_Interpreter_Map::CommandStay(RPG::EventCommand const& com) { // code 20730
return CommandOptionGeneric(com, eOptionInnStay, {Cmd::NoStay, Cmd::EndInn});
}
Expand Down
2 changes: 0 additions & 2 deletions src/game_interpreter_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ class Game_Interpreter_Map : public Game_Interpreter

bool ContinuationOpenShop(RPG::EventCommand const& com) override;
bool ContinuationShowInnStart(RPG::EventCommand const& com) override;
bool ContinuationShowInnContinue(RPG::EventCommand const& com);
bool ContinuationShowInnFinish(RPG::EventCommand const& com) override;
bool ContinuationEnemyEncounter(RPG::EventCommand const& com) override;

static std::vector<Game_Character*> pending;
Expand Down
53 changes: 53 additions & 0 deletions src/scene_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ void Scene_Map::PreUpdate(MapUpdateAsyncContext& actx) {
}

void Scene_Map::Update() {
if (activate_inn) {
UpdateInn();
return;
}
MapUpdateAsyncContext actx;
UpdateStage1(actx);
}
Expand Down Expand Up @@ -391,6 +395,55 @@ void Scene_Map::OnAsyncSuspend(F&& f, AsyncOp aop, bool is_preupdate) {
Graphics::GetTransition().Init(tt, this, 32, false);
}

if (aop.GetType() == AsyncOp::eCallInn) {
activate_inn = true;
music_before_inn = Game_System::GetCurrentBGM();
inn_continuation = std::forward<F>(f);

Game_System::BgmFade(800);

// FIXME: Is 36 correct here?
Graphics::GetTransition().Init(Transition::TransitionFadeOut, Scene::instance.get(), 36, true);

AsyncNext([=]() { StartInn(); });
return;
}

AsyncNext(std::forward<F>(f));
}

void Scene_Map::StartInn() {
const RPG::Music& bgm_inn = Game_System::GetSystemBGM(Game_System::BGM_Inn);
if (bgm_inn.name.empty() ||
bgm_inn.name == "(OFF)" ||
bgm_inn.name == "(Brak)") {
FinishInn();
return;
}

Game_System::BgmStop();
Game_System::BgmPlay(bgm_inn);
}

void Scene_Map::FinishInn() {
// FIXME: Is 36 correct here?
Graphics::GetTransition().Init(Transition::TransitionFadeIn, Scene::instance.get(), 36, false);
// FIXME: Does this play before or after transition?
Game_System::BgmPlay(music_before_inn);

// Full heal
std::vector<Game_Actor*> actors = Main_Data::game_party->GetActors();
for (Game_Actor* actor : actors) {
actor->FullHeal();
}

activate_inn = false;
AsyncNext(std::move(inn_continuation));
}

void Scene_Map::UpdateInn() {
if (!Audio().BGM_IsPlaying() || Audio().BGM_PlayedOnce()) {
Game_System::BgmStop();
FinishInn();
}
}
8 changes: 8 additions & 0 deletions src/scene_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ class Scene_Map: public Scene {

void UpdateSceneCalling();

void StartInn();
void UpdateInn();
void FinishInn();

template <typename F> void AsyncNext(F&& f);
template <typename F> void OnAsyncSuspend(F&& f, AsyncOp aop, bool is_preupdate);

Expand All @@ -86,6 +90,10 @@ class Scene_Map: public Scene {
int debug_menuoverwrite_counter = 0;
bool from_save;
bool screen_erased_by_event = false;

RPG::Music music_before_inn = {};
AsyncContinuation inn_continuation = {};
bool activate_inn = false;
};

#endif

0 comments on commit 36ede65

Please sign in to comment.