Skip to content

Commit

Permalink
Improve MapEventCache data structure.
Browse files Browse the repository at this point in the history
Get rid of the unnecessary pointer.
  • Loading branch information
Ghabry committed Mar 12, 2024
1 parent 2ddbaca commit 1ed9339
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 49 deletions.
5 changes: 2 additions & 3 deletions src/game_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ void Game_Interpreter::Update(bool reset_loop_count) {
// 10 per second
Main_Data::game_variables->Set(_keyinput.time_variable,
(_keyinput.wait_frames * 10) / Game_Clock::GetTargetGameFps());
Game_Map::SetNeedRefreshForVarChange(_keyinput.time_variable);
}
_keyinput.wait = false;
}
Expand Down Expand Up @@ -2287,9 +2288,7 @@ bool Game_Interpreter::CommandMemorizeLocation(lcf::rpg::EventCommand const& com
Main_Data::game_variables->Set(var_map_id, Game_Map::GetMapId());
Main_Data::game_variables->Set(var_x, player->GetX());
Main_Data::game_variables->Set(var_y, player->GetY());
Game_Map::SetNeedRefreshForVarChange(var_map_id);
Game_Map::SetNeedRefreshForVarChange(var_x);
Game_Map::SetNeedRefreshForVarChange(var_y);
Game_Map::SetNeedRefreshForVarChange({var_map_id, var_x, var_y});
return true;
}

Expand Down
41 changes: 23 additions & 18 deletions src/game_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

// Headers
#include <cassert>
#include <initializer_list>
#include <iomanip>
#include <sstream>
#include <algorithm>
Expand Down Expand Up @@ -72,8 +73,8 @@ namespace {
std::vector<unsigned char> passages_up;
std::vector<Game_Event> events;
std::vector<Game_CommonEvent> common_events;
std::unordered_map<int, std::unique_ptr<MapEventCache>> events_cache_by_switch;
std::unordered_map<int, std::unique_ptr<MapEventCache>> events_cache_by_variable;
std::unordered_map<int, MapEventCache> events_cache_by_switch;
std::unordered_map<int, MapEventCache> events_cache_by_variable;

std::unique_ptr<lcf::rpg::Map> map;

Expand Down Expand Up @@ -378,19 +379,11 @@ void Game_Map::SetupCommon() {
}

void Game_Map::AddEventToSwitchCache(lcf::rpg::Event& ev, int switch_id) {
if (events_cache_by_switch.find(switch_id) == events_cache_by_switch.end()) {
std::unique_ptr<MapEventCache> cache = std::make_unique<MapEventCache>();
events_cache_by_switch[switch_id] = std::move(cache);
}
events_cache_by_switch[switch_id]->AddEvent(ev);
events_cache_by_switch[switch_id].AddEvent(ev);
}

void Game_Map::AddEventToVariableCache(lcf::rpg::Event& ev, int var_id) {
if (events_cache_by_variable.find(var_id) == events_cache_by_variable.end()) {
std::unique_ptr<MapEventCache> cache = std::make_unique<MapEventCache>();
events_cache_by_variable[var_id] = std::move(cache);
}
events_cache_by_variable[var_id]->AddEvent(ev);
events_cache_by_variable[var_id].AddEvent(ev);
}

void Game_Map::PrepareSave(lcf::rpg::Save& save) {
Expand Down Expand Up @@ -1544,7 +1537,8 @@ void Game_Map::SetPositionY(int y, bool reset_panorama) {
}

bool Game_Map::GetNeedRefresh() {
if (Player::game_config.patch_anti_lag_switch.Get() && Main_Data::game_switches->Get(Player::game_config.patch_anti_lag_switch.Get())) {
int anti_lag_switch = Player::game_config.patch_anti_lag_switch.Get();
if (anti_lag_switch > 0 && Main_Data::game_switches->Get(anti_lag_switch)) {
return false;
}

Expand All @@ -1555,13 +1549,12 @@ void Game_Map::SetNeedRefresh(bool refresh) {
need_refresh = refresh;
}


void MapEventCache::AddEvent(lcf::rpg::Event& ev) {
for (const auto& ev2 : events) {
if (ev.ID == ev2.ID)
return;
auto id = ev.ID;

if (std::find(event_ids.begin(), event_ids.end(), id) == event_ids.end()) {
event_ids.emplace_back(id);
}
events.emplace_back(ev);
}

void Game_Map::SetNeedRefreshForSwitchChange(int switch_id) {
Expand All @@ -1578,6 +1571,18 @@ void Game_Map::SetNeedRefreshForVarChange(int var_id) {
SetNeedRefresh(true);
}

void Game_Map::SetNeedRefreshForSwitchChange(std::initializer_list<int> switch_ids) {
for (auto switch_id: switch_ids) {
SetNeedRefreshForSwitchChange(switch_id);
}
}

void Game_Map::SetNeedRefreshForVarChange(std::initializer_list<int> var_ids) {
for (auto var_id: var_ids) {
SetNeedRefreshForVarChange(var_id);
}
}

std::vector<unsigned char>& Game_Map::GetPassagesDown() {
return passages_down;
}
Expand Down
59 changes: 31 additions & 28 deletions src/game_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

// Headers
#include <cstdint>
#include <initializer_list>
#include <vector>
#include <string>
#include <unordered_set>
Expand Down Expand Up @@ -47,38 +48,38 @@ struct BattleArgs;
constexpr int SCREEN_TILE_SIZE = 256;

class MapUpdateAsyncContext {
public:
MapUpdateAsyncContext() = default;

static MapUpdateAsyncContext FromCommonEvent(int ce, AsyncOp aop);
static MapUpdateAsyncContext FromMapEvent(int ce, AsyncOp aop);
static MapUpdateAsyncContext FromForegroundEvent(AsyncOp aop);
static MapUpdateAsyncContext FromMessage(AsyncOp aop);

AsyncOp GetAsyncOp() const;

int GetParallelCommonEvent() const;
int GetParallelMapEvent() const;

bool IsForegroundEvent() const;
bool IsParallelCommonEvent() const;
bool IsParallelMapEvent() const;
bool IsMessage() const;
bool IsActive() const;
private:
AsyncOp async_op = {};
int common_event = 0;
int map_event = 0;
bool foreground_event = false;
bool message = false;
public:
MapUpdateAsyncContext() = default;

static MapUpdateAsyncContext FromCommonEvent(int ce, AsyncOp aop);
static MapUpdateAsyncContext FromMapEvent(int ce, AsyncOp aop);
static MapUpdateAsyncContext FromForegroundEvent(AsyncOp aop);
static MapUpdateAsyncContext FromMessage(AsyncOp aop);

AsyncOp GetAsyncOp() const;

int GetParallelCommonEvent() const;
int GetParallelMapEvent() const;

bool IsForegroundEvent() const;
bool IsParallelCommonEvent() const;
bool IsParallelMapEvent() const;
bool IsMessage() const;
bool IsActive() const;
private:
AsyncOp async_op = {};
int common_event = 0;
int map_event = 0;
bool foreground_event = false;
bool message = false;
};

class MapEventCache {
public:
void AddEvent(lcf::rpg::Event& ev);
public:
void AddEvent(lcf::rpg::Event& ev);

private:
std::vector<lcf::rpg::Event> events;
private:
std::vector<int> event_ids;
};

/**
Expand Down Expand Up @@ -689,6 +690,8 @@ namespace Game_Map {

void SetNeedRefreshForSwitchChange(int switch_id);
void SetNeedRefreshForVarChange(int var_id);
void SetNeedRefreshForSwitchChange(std::initializer_list<int> switch_ids);
void SetNeedRefreshForVarChange(std::initializer_list<int> var_ids);

void AddEventToSwitchCache(lcf::rpg::Event& ev, int switch_id);
void AddEventToVariableCache(lcf::rpg::Event& ev, int var_id);
Expand Down

0 comments on commit 1ed9339

Please sign in to comment.