Skip to content

Commit

Permalink
Restructure event and common event update
Browse files Browse the repository at this point in the history
  • Loading branch information
Zegeri committed Feb 6, 2016
1 parent ae1e5aa commit 341b4c4
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 62 deletions.
34 changes: 17 additions & 17 deletions src/game_commonevent.cpp
Expand Up @@ -41,8 +41,6 @@ Game_CommonEvent::Game_CommonEvent(int common_event_id, bool battle, const RPG::
}

void Game_CommonEvent::Refresh() {
CheckEventTriggerAuto();

if (GetTrigger() == RPG::EventPage::Trigger_parallel) {
if (GetSwitchFlag() ? Game_Switches[GetSwitchId()] : true) {
if (!interpreter) {
Expand All @@ -58,14 +56,27 @@ void Game_CommonEvent::Refresh() {
}

void Game_CommonEvent::Update() {
CheckEventTriggerAuto();
if (GetTrigger() != RPG::EventPage::Trigger_auto_start)
return;

for (int i = 0; i < 500; ++i) {

This comment has been minimized.

Copy link
@Ghabry

Ghabry Feb 8, 2016

What is the purpose of that loop?
It loops up to 500 times when the event is finishing in one update tick but why the 500?

This comment has been minimized.

Copy link
@Zegeri

Zegeri Feb 8, 2016

Author Owner

It's the actual behaviour of autostarting common event, they loop and run multiple times in each frame while their conditions are met. In RPG_RT, the number of loops depends on how many event commands it has. The more event commands it has, the less it loops. I think 500 loops is good enough.

if (GetSwitchFlag() ? Game_Switches[GetSwitchId()] : true) {
if (!Game_Map::GetInterpreter().IsRunning()) {
Game_Map::GetInterpreter().SetupStartingEvent(this);
Game_Map::GetInterpreter().Update();
continue;
}
}
return;
}
}

void Game_CommonEvent::UpdateParallel() {
if (interpreter && parallel_running) {
if (!interpreter->IsRunning()) {
interpreter->Setup(GetList(), 0, false, -common_event_id, -2);
} else {
interpreter->Update();
interpreter->Setup(GetList(), 0, -common_event_id, -2);
}
interpreter->Update();
}
}

Expand Down Expand Up @@ -93,17 +104,6 @@ std::vector<RPG::EventCommand>& Game_CommonEvent::GetList() {
return Data::commonevents[common_event_id - 1].event_commands;
}

void Game_CommonEvent::CheckEventTriggerAuto() {
if (GetTrigger() == RPG::EventPage::Trigger_auto_start) {
if (GetSwitchFlag() ? Game_Switches[GetSwitchId()] : true) {
//printf("%d %d\n", GetSwitchId(), (int)Game_Switches[GetSwitchId()]);
if (!Game_Map::GetInterpreter().IsRunning()) {
Game_Map::GetInterpreter().SetupStartingEvent(this);
}
}
}
}

RPG::SaveEventData Game_CommonEvent::GetSaveData() {
RPG::SaveEventData event_data;

Expand Down
8 changes: 6 additions & 2 deletions src/game_commonevent.h
Expand Up @@ -55,10 +55,15 @@ class Game_CommonEvent {
void Refresh();

/**
* Updates common event interpreter.
* Updates common event.
*/
void Update();

/**
* Updates common event parallel interpreter.
*/
void UpdateParallel();

/**
* Gets common event index.
*
Expand Down Expand Up @@ -100,7 +105,6 @@ class Game_CommonEvent {
* @return event commands list.
*/
std::vector<RPG::EventCommand>& GetList();
void CheckEventTriggerAuto();

RPG::SaveEventData GetSaveData();

Expand Down
40 changes: 23 additions & 17 deletions src/game_event.cpp
Expand Up @@ -292,7 +292,6 @@ void Game_Event::Setup(RPG::EventPage* new_page) {
if (trigger == RPG::EventPage::Trigger_parallel) {
interpreter.reset(new Game_Interpreter_Map());
}
CheckEventTriggerAuto();
}

void Game_Event::SetupFromSave(RPG::EventPage* new_page) {
Expand Down Expand Up @@ -328,7 +327,6 @@ void Game_Event::SetupFromSave(RPG::EventPage* new_page) {
if (!interpreter && trigger == RPG::EventPage::Trigger_parallel) {
interpreter.reset(new Game_Interpreter_Map());
}
CheckEventTriggerAuto();
}

void Game_Event::Refresh() {
Expand Down Expand Up @@ -475,12 +473,6 @@ void Game_Event::Start(bool by_decision_key) {
started_by_decision_key = by_decision_key;
}

void Game_Event::CheckEventTriggerAuto() {
if (trigger == RPG::EventPage::Trigger_auto_start && Game_Map::GetReady()) {
Start();
}
}

std::vector<RPG::EventCommand>& Game_Event::GetList() {
return list;
}
Expand All @@ -501,6 +493,14 @@ void Game_Event::StopTalkToHero() {
halting = true;
}

void Game_Event::CheckEventTriggers() {
if (trigger == RPG::EventPage::Trigger_auto_start) {
Start();
} else if (trigger == RPG::EventPage::Trigger_collision) {
CheckEventTriggerTouch(GetX(),GetY());
}
}

bool Game_Event::CheckEventTriggerTouch(int x, int y) {
if (Game_Map::GetInterpreter().IsRunning())
return false;
Expand Down Expand Up @@ -549,16 +549,9 @@ void Game_Event::Update() {
return;
}

CheckEventTriggerAuto();

if (interpreter) {
if (!interpreter->IsRunning()) {
interpreter->Setup(list, event.ID, started_by_decision_key, -event.x, event.y);
} else {
interpreter->Update();
}
} else if (starting && !Game_Map::GetInterpreter().IsRunning()) {
if (starting && !Game_Map::GetInterpreter().IsRunning()) {
Game_Map::GetInterpreter().SetupStartingEvent(this);
Game_Map::GetInterpreter().Update();
running = true;
}

Expand All @@ -570,6 +563,19 @@ void Game_Event::Update() {
}
}

void Game_Event::UpdateParallel() {
if (!data.active || page == NULL) {
return;
}

if (interpreter) {
if (!interpreter->IsRunning()) {
interpreter->Setup(list, event.ID, -event.x, event.y);
}
interpreter->Update();
}
}

RPG::Event& Game_Event::GetEvent() {
return event;
}
Expand Down
3 changes: 2 additions & 1 deletion src/game_event.h
Expand Up @@ -152,10 +152,11 @@ class Game_Event : public Game_Character {
*/
void StopTalkToHero();

void CheckEventTriggerAuto();
void CheckEventTriggers();
bool CheckEventTriggerTouch(int x, int y);
void Start(bool triggered_by_decision_key = false);
void Update();
void UpdateParallel();
bool AreConditionsMet(const RPG::EventPage& page);

/**
Expand Down
3 changes: 0 additions & 3 deletions src/game_interpreter.cpp
Expand Up @@ -105,9 +105,6 @@ void Game_Interpreter::Setup(

if (main_flag && depth == 0)
Game_Message::SetFaceName("");

if (!updating && depth == 0)
Update();
}

void Game_Interpreter::CancelMenuCall() {
Expand Down
29 changes: 19 additions & 10 deletions src/game_map.cpp
Expand Up @@ -78,7 +78,6 @@ namespace {
bool pan_locked;
bool pan_wait;
int pan_speed;
bool ready;
}

void Game_Map::Init() {
Expand Down Expand Up @@ -112,7 +111,6 @@ void Game_Map::Init() {
location.pan_finish_y = 0;
location.pan_current_x = 0;
location.pan_current_y = 0;
ready = false;
}

void Game_Map::Dispose() {
Expand Down Expand Up @@ -146,7 +144,6 @@ void Game_Map::Setup(int _id) {
location.pan_finish_y = 0;
location.pan_current_x = 0;
location.pan_current_y = 0;
ready = true;
}

void Game_Map::SetupFromSave() {
Expand Down Expand Up @@ -190,11 +187,9 @@ void Game_Map::SetupFromSave() {
location.pan_current_y = 0;
location.pan_finish_x = 0;
location.pan_finish_y = 0;
ready = true;
}

void Game_Map::SetupCommon(int _id) {
ready = false;
Dispose();

location.map_id = _id;
Expand Down Expand Up @@ -701,7 +696,7 @@ void Game_Map::UpdateScroll() {
}
}

void Game_Map::Update() {
void Game_Map::Update(bool only_parallel) {
if (GetNeedRefresh()) Refresh();
UpdateScroll();
UpdatePan();
Expand All @@ -713,6 +708,24 @@ void Game_Map::Update() {
}
}

for (Game_CommonEvent& ev : common_events) {
ev.UpdateParallel();
}

for (Game_Event& ev : events) {
ev.UpdateParallel();
}

if (only_parallel)
return;

for (Game_Event& ev : events) {
ev.CheckEventTriggers();
}

Main_Data::game_player->Update();
GetInterpreter().Update();

for (Game_Event& ev : events) {
ev.Update();
}
Expand Down Expand Up @@ -913,10 +926,6 @@ void Game_Map::SetNeedRefresh(bool new_need_refresh) {
need_refresh = new_need_refresh;
}

bool Game_Map::GetReady() {
return ready;
}

std::vector<unsigned char>& Game_Map::GetPassagesDown() {
return passages_down;
}
Expand Down
11 changes: 3 additions & 8 deletions src/game_map.h
Expand Up @@ -219,8 +219,10 @@ namespace Game_Map {

/**
* Updates the map state.
*
* @param only_parallel Update only parallel interpreters
*/
void Update();
void Update(bool only_parallel = false);

/**
* Updates the scroll state.
Expand Down Expand Up @@ -401,13 +403,6 @@ namespace Game_Map {
*/
bool GetNeedRefresh();

/**
* Gets ready flag.
*
* @return ready flag.
*/
bool GetReady();

/**
* Gets the game interpreter.
*
Expand Down
6 changes: 2 additions & 4 deletions src/scene_map.cpp
Expand Up @@ -66,6 +66,7 @@ void Scene_Map::Start() {
}

Player::FrameReset();
Game_Map::Update(true);
}

Scene_Map::~Scene_Map() {
Expand Down Expand Up @@ -129,12 +130,9 @@ void Scene_Map::Update() {
FinishTeleportPlayer();
}

Game_Map::GetInterpreter().Update();

Main_Data::game_party->UpdateTimers();

Game_Map::Update();
Main_Data::game_player->Update();
Main_Data::game_screen->Update();
spriteset->Update();
message_window->Update();
Expand Down Expand Up @@ -222,7 +220,7 @@ void Scene_Map::FinishTeleportPlayer() {

spriteset.reset(new Spriteset_Map());

Game_Map::Update();
Game_Map::Update(true);

if (autotransition) {
auto_transition = true;
Expand Down

0 comments on commit 341b4c4

Please sign in to comment.