Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/game_battle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <lcf/data.h>
#include "player.h"
#include "game_actors.h"
#include "game_commonevent.h"
#include "game_enemyparty.h"
#include "game_message.h"
#include "game_party.h"
Expand All @@ -43,6 +44,8 @@ namespace Game_Battle {
std::string background_name;

std::unique_ptr<Game_Interpreter_Battle> interpreter;
std::vector<Game_CommonEvent> common_events;

/** Contains battle related sprites */
std::unique_ptr<Spriteset_Battle> spriteset;

Expand Down Expand Up @@ -76,18 +79,28 @@ void Game_Battle::Init(int troop_id) {
animation_actors.reset();
animation_enemies.reset();

InitCommonEvents();

for (auto* actor: Main_Data::game_party->GetActors()) {
actor->ResetEquipmentStates(true);
}
}

void Game_Battle::InitCommonEvents() {
common_events.clear();
common_events.reserve(lcf::Data::commonevents.size());
for (const lcf::rpg::CommonEvent& ev : lcf::Data::commonevents) {
common_events.emplace_back(ev.ID, false);
}
}

void Game_Battle::Quit() {
if (!IsBattleRunning()) {
return;
}

interpreter.reset();
common_events.clear();
spriteset.reset();
animation_actors.reset();
animation_enemies.reset();
Expand Down Expand Up @@ -276,6 +289,10 @@ Game_Interpreter_Battle& Game_Battle::GetInterpreterBattle() {
return *interpreter;
}

std::vector<Game_CommonEvent>& Game_Battle::GetCommonEvents() {
return common_events;
}

void Game_Battle::SetTerrainId(int id) {
terrain_id = id;
}
Expand Down
12 changes: 12 additions & 0 deletions src/game_battle.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ namespace Game_Battle {
*/
void Init(int troop_id);

/**
* Initializes Common Events.
*/
void InitCommonEvents();

/** @return true if a battle is currently running */
bool IsBattleRunning();

Expand Down Expand Up @@ -141,6 +146,13 @@ namespace Game_Battle {
*/
Game_Interpreter_Battle& GetInterpreterBattle();

/**
* Gets common events list.
*
* @return common events list.
*/
std::vector<Game_CommonEvent>& GetCommonEvents();

void SetTerrainId(int id);
int GetTerrainId();

Expand Down
36 changes: 29 additions & 7 deletions src/game_commonevent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,37 @@

// Headers
#include "game_commonevent.h"
#include "game_interpreter_battle.h"
#include "game_map.h"
#include "game_switches.h"
#include "game_interpreter_map.h"
#include "main_data.h"
#include <lcf/reader_util.h>
#include <cassert>
#include <lcf/rpg/commonevent.h>
#include <lcf/rpg/eventcommand.h>

Game_CommonEvent::Game_CommonEvent(int common_event_id) :
common_event_id(common_event_id)
Game_CommonEvent::Game_CommonEvent(int common_event_id, bool on_map) :
common_event_id(common_event_id), on_map(on_map)
{
auto* ce = lcf::ReaderUtil::GetElement(lcf::Data::commonevents, common_event_id);

if (ce->trigger == lcf::rpg::EventPage::Trigger_parallel
if (on_map && ce->trigger == lcf::rpg::CommonEvent::Trigger_parallel
&& !ce->event_commands.empty()) {
interpreter.reset(new Game_Interpreter_Map());
interpreter->Push<InterpreterExecutionType::Parallel>(this);
}


if (!on_map && ce->trigger == lcf::rpg::CommonEvent::Trigger_maniac_battle_parallel
&& !ce->event_commands.empty()) {
interpreter.reset(new Game_Interpreter_Battle(false));
interpreter->Push<InterpreterExecutionType::BattleParallel>(this);
}
}

void Game_CommonEvent::SetSaveData(const lcf::rpg::SaveEventExecState& data) {
assert(on_map);

// RPG_RT Savegames have empty stacks for parallel events.
// We are LSD compatible but don't load these into interpreter.
if (!data.stack.empty() && !data.stack.front().commands.empty()) {
Expand Down Expand Up @@ -94,11 +103,13 @@ std::vector<lcf::rpg::EventCommand>& Game_CommonEvent::GetList() {
}

lcf::rpg::SaveEventExecState Game_CommonEvent::GetSaveData() {
assert(on_map);

lcf::rpg::SaveEventExecState state;
if (interpreter) {
state = interpreter->GetSaveState();
}
if (GetTrigger() == lcf::rpg::EventPage::Trigger_parallel && state.stack.empty()) {
if (GetTrigger() == lcf::rpg::CommonEvent::Trigger_parallel && state.stack.empty()) {
// RPG_RT always stores an empty stack frame for parallel events.
state.stack.push_back({});
}
Expand All @@ -107,13 +118,24 @@ lcf::rpg::SaveEventExecState Game_CommonEvent::GetSaveData() {

bool Game_CommonEvent::IsWaitingForegroundExecution() const {
auto* ce = lcf::ReaderUtil::GetElement(lcf::Data::commonevents, common_event_id);
return ce->trigger == lcf::rpg::EventPage::Trigger_auto_start &&
return ce->trigger == lcf::rpg::CommonEvent::Trigger_automatic &&
(!ce->switch_flag || Main_Data::game_switches->Get(ce->switch_id))
&& !ce->event_commands.empty();
}

bool Game_CommonEvent::IsWaitingBattleStartExecution() const {
auto* ce = lcf::ReaderUtil::GetElement(lcf::Data::commonevents, common_event_id);
return ce->trigger == lcf::rpg::CommonEvent::Trigger_maniac_battle_start &&
(!ce->switch_flag || Main_Data::game_switches->Get(ce->switch_id))
&& !ce->event_commands.empty();
}

bool Game_CommonEvent::IsWaitingBackgroundExecution(bool force_run) const {
auto* ce = lcf::ReaderUtil::GetElement(lcf::Data::commonevents, common_event_id);
return ce->trigger == lcf::rpg::EventPage::Trigger_parallel &&
const auto trigger = on_map ?
lcf::rpg::CommonEvent::Trigger_parallel :
lcf::rpg::CommonEvent::Trigger_maniac_battle_parallel;

return ce->trigger == trigger &&
(force_run || !ce->switch_flag || Main_Data::game_switches->Get(ce->switch_id));
}
16 changes: 14 additions & 2 deletions src/game_commonevent.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Game_CommonEvent {
*
* @param common_event_id database common event ID.
*/
explicit Game_CommonEvent(int common_event_id);
explicit Game_CommonEvent(int common_event_id, bool on_map = true);

/**
* Set savegame data.
Expand Down Expand Up @@ -109,19 +109,31 @@ class Game_CommonEvent {
/** @return true if waiting for foreground execution */
bool IsWaitingForegroundExecution() const;

/** @return true if waiting for execution when battle starts */
bool IsWaitingBattleStartExecution() const;

/**
* @param force_run force the event to execute even if conditions not met.
* @return true if waiting for background execution
*/
bool IsWaitingBackgroundExecution(bool force_run) const;

const Game_Interpreter* GetInterpreter() const;

private:
int common_event_id;

/** Indicates whether these common events run on the map (false = on battle) */
bool on_map;

/** Interpreter for parallel common events. */
std::unique_ptr<Game_Interpreter_Map> interpreter;
std::unique_ptr<Game_Interpreter> interpreter;

friend class Game_Interpreter_Inspector;
};

inline const Game_Interpreter* Game_CommonEvent::GetInterpreter() const {
return interpreter.get();
}

#endif
5 changes: 5 additions & 0 deletions src/game_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ void Game_Interpreter::PushInternal(
_state.stack.push_back(std::move(frame));
}

void Game_Interpreter::SetState(const lcf::rpg::SaveEventExecState& save) {
Clear();
_state = save;
_keyinput.fromSave(save);
}

void Game_Interpreter::KeyInputState::fromSave(const lcf::rpg::SaveEventExecState& save) {
*this = {};
Expand Down
14 changes: 11 additions & 3 deletions src/game_interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,20 @@ class Game_Interpreter : public Game_BaseInterpreterContext
bool ExecuteCommand();
virtual bool ExecuteCommand(lcf::rpg::EventCommand const& com);


/**
* Returns the interpreters current state information.
* For saving state into a save file, use GetSaveState instead.
*/
const lcf::rpg::SaveEventExecState& GetState() const override;

/**
* Sets up the interpreter with given state.
*
* @param save event to load.
*
*/
void SetState(const lcf::rpg::SaveEventExecState& save);

/**
* Returns a SaveEventExecState needed for the savefile.
*
Expand All @@ -122,7 +129,7 @@ class Game_Interpreter : public Game_BaseInterpreterContext
void ClearOriginalEventId();

/** Return true if the interpreter is waiting for an async operation and needs to be resumed */
bool IsAsyncPending();
bool IsAsyncPending() const;

/** Return true if the interpreter is waiting for an async operation and needs to be resumed */
AsyncOp GetAsyncOp() const;
Expand Down Expand Up @@ -412,6 +419,7 @@ inline void Game_Interpreter::Push(Game_Event* ev, const lcf::rpg::EventPage* pa
template<InterpreterExecutionType type_ex>
inline void Game_Interpreter::Push(Game_CommonEvent* ev) {
static_assert(type_ex == InterpreterExecutionType::AutoStart || type_ex == InterpreterExecutionType::Parallel
|| type_ex == InterpreterExecutionType::BattleStart || type_ex == InterpreterExecutionType::BattleParallel
|| type_ex == InterpreterExecutionType::Call || type_ex == InterpreterExecutionType::DeathHandler
|| type_ex == InterpreterExecutionType::DebugCall || type_ex == InterpreterExecutionType::ManiacHook, "Unexpected ExecutionType for CommonEvent"
);
Expand Down Expand Up @@ -457,7 +465,7 @@ inline int Game_Interpreter::GetLoopCount() const {
return loop_count;
}

inline bool Game_Interpreter::IsAsyncPending() {
inline bool Game_Interpreter::IsAsyncPending() const {
return GetAsyncOp().IsActive();
}

Expand Down
4 changes: 2 additions & 2 deletions src/game_interpreter_battle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ Game_Interpreter_Battle::Game_Interpreter_Battle(Span<const lcf::rpg::TroopPage>
maniac_interpreter.reset(new Game_Interpreter_Battle());
}

Game_Interpreter_Battle::Game_Interpreter_Battle()
: Game_Interpreter(true)
Game_Interpreter_Battle::Game_Interpreter_Battle(bool main_flag)
: Game_Interpreter(main_flag)
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/game_interpreter_battle.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Game_Interpreter_Battle : public Game_Interpreter
{
public:
explicit Game_Interpreter_Battle(Span<const lcf::rpg::TroopPage> pages);
explicit Game_Interpreter_Battle();
explicit Game_Interpreter_Battle(bool main_flag = true);

int GetNumPages() const;

Expand Down
2 changes: 1 addition & 1 deletion src/game_interpreter_control_variables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ int ControlVariables::Actor(int op, int actor_id) {
case 16:
// ATB
if (Player::IsPatchManiac()) {
return actor->GetAtbGauge();
return actor->GetAtbGauge() * 100 / actor->GetMaxAtbGauge();
}
break;
}
Expand Down
6 changes: 0 additions & 6 deletions src/game_interpreter_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,6 @@ enum InnSubcommand {

using namespace Game_Interpreter_Shared;

void Game_Interpreter_Map::SetState(const lcf::rpg::SaveEventExecState& save) {
Clear();
_state = save;
_keyinput.fromSave(save);
}

void Game_Interpreter_Map::OnMapChange() {
// When we change the map, we reset all event id's to 0.
for (auto& frame: _state.stack) {
Expand Down
14 changes: 0 additions & 14 deletions src/game_interpreter_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,6 @@ class Game_Interpreter_Map : public Game_Interpreter
public:
using Game_Interpreter::Game_Interpreter;

/**
* Sets up the interpreter with given state.
*
* @param save event to load.
*
*/
void SetState(const lcf::rpg::SaveEventExecState& save);

/**
* Called when we change maps.
*/
Expand Down Expand Up @@ -88,12 +80,6 @@ class Game_Interpreter_Map : public Game_Interpreter
bool CommandEasyRpgWaitForSingleMovement(lcf::rpg::EventCommand const& com);
AsyncOp ContinuationShowInnStart(int indent, int choice_result, int price);

bool CommandSmartMoveRoute(
lcf::rpg::EventCommand const& com,
int maxRouteStepsDefault, int maxSearchStepsDefault,
int abortIfAlreadyMovingDefault
); // Internal generic path finder function.

static std::vector<Game_Character*> pending;
};

Expand Down
Loading
Loading