Skip to content

Commit

Permalink
DynRpg: Refactor to use a Game_DynRpg instance.
Browse files Browse the repository at this point in the history
This makes cleanup of state easier when switching between games.
  • Loading branch information
Ghabry committed Oct 30, 2023
1 parent c25e3a8 commit b3c5e4c
Show file tree
Hide file tree
Showing 16 changed files with 198 additions and 187 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ add_library(${PROJECT_NAME} OBJECT
src/drawable_list.h
src/drawable_mgr.cpp
src/drawable_mgr.h
src/dynrpg.cpp
src/dynrpg.h
src/dynrpg_easyrpg.cpp
src/dynrpg_easyrpg.h
src/dynrpg_textplugin.cpp
Expand Down Expand Up @@ -158,6 +156,8 @@ add_library(${PROJECT_NAME} OBJECT
src/game_config.h
src/game_config_game.cpp
src/game_config_game.h
src/game_dynrpg.cpp
src/game_dynrpg.h
src/game_enemy.cpp
src/game_enemy.h
src/game_enemyparty.cpp
Expand Down
4 changes: 2 additions & 2 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ libeasyrpg_player_a_SOURCES = \
src/drawable_list.h \
src/drawable_mgr.cpp \
src/drawable_mgr.h \
src/dynrpg.cpp \
src/dynrpg.h \
src/dynrpg_easyrpg.cpp \
src/dynrpg_easyrpg.h \
src/dynrpg_textplugin.h \
Expand Down Expand Up @@ -139,6 +137,8 @@ libeasyrpg_player_a_SOURCES = \
src/game_config.h \
src/game_config_game.cpp \
src/game_config_game.h \
src/game_dynrpg.cpp \
src/game_dynrpg.h \
src/game_enemy.cpp \
src/game_enemy.h \
src/game_enemyparty.cpp \
Expand Down
95 changes: 52 additions & 43 deletions src/dynrpg_easyrpg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,53 +48,23 @@ static bool EasyOput(dyn_arg_list args) {
return true;
}

static bool EasyRaw(dyn_arg_list args) {
auto func = "raw";
bool okay = false;

lcf::rpg::EventCommand outputCommand;
std::vector<int32_t> outputParams = {};

for (std::size_t i = 0; i < args.size(); ++i) {
std::string currValue = DynRpg::ParseVarArg(func, args, i, okay);
Output::Warning("{}", currValue);

if (!okay) return true;

if (i == 0) outputCommand.code = stoi(currValue);
if (i == 1) outputCommand.string = lcf::DBString(currValue);
else outputParams.push_back(stoi(currValue));
}

outputCommand.parameters = lcf::DBArray<int32_t>(outputParams.begin(), outputParams.end());

//FIXME: this will crash when you two interpreters run a raw command in parallel.
// The lack to access the current interpreter frame is a lack in the dynrpg API design.
// Have to fix this. The current frame should be easy to access
std::vector<lcf::rpg::EventCommand> cmdList = { outputCommand };
if (Game_Battle::IsBattleRunning()) Game_Battle::GetInterpreter().Push(cmdList, 0, false);
else Game_Map::GetInterpreter().Push(cmdList, 0, false);
bool DynRpg::EasyRpgPlugin::EasyCall(dyn_arg_list args, bool& do_yield, Game_Interpreter* interpreter) {
auto func_name = std::get<0>(DynRpg::ParseArgs<std::string>("call", args));

return true;
}

static bool EasyCall(dyn_arg_list args) {
auto token = std::get<0>(DynRpg::ParseArgs<std::string>("call", args));

if (token.empty()) {
if (func_name.empty()) {
// empty function name
Output::Warning("call: Empty RPGSS function name");

return true;
}

if (!DynRpg::HasFunction(token)) {
// Not a supported function
Output::Warning("Unsupported RPGSS function: {}", token);
return true;
for (auto& plugin: Main_Data::game_dynrpg->plugins) {
if (plugin->Invoke(func_name, args.subspan(1), do_yield, interpreter)) {
return true;
}
}

return DynRpg::Invoke(token, args.subspan(1));
return false;
}

static bool EasyAdd(dyn_arg_list args) {
Expand All @@ -118,11 +88,50 @@ static bool EasyAdd(dyn_arg_list args) {
return true;
}

void DynRpg::EasyRpgPlugin::RegisterFunctions() {
DynRpg::RegisterFunction("call", EasyCall);
DynRpg::RegisterFunction("easyrpg_output", EasyOput);
DynRpg::RegisterFunction("easyrpg_add", EasyAdd);
DynRpg::RegisterFunction("easyrpg_raw", EasyRaw);
bool DynRpg::EasyRpgPlugin::EasyRaw(dyn_arg_list args, Game_Interpreter* interpreter) {
if (!interpreter) {
return true;
}

auto func = "raw";
bool okay = false;

lcf::rpg::EventCommand outputCommand;
std::vector<int32_t> outputParams = {};

for (std::size_t i = 0; i < args.size(); ++i) {
std::string currValue = DynRpg::ParseVarArg(func, args, i, okay);
Output::Warning("{}", currValue);

if (!okay) return true;

if (i == 0) outputCommand.code = stoi(currValue);
if (i == 1) outputCommand.string = lcf::DBString(currValue);
else outputParams.push_back(stoi(currValue));
}

outputCommand.parameters = lcf::DBArray<int32_t>(outputParams.begin(), outputParams.end());

//FIXME: this will crash when you two interpreters run a raw command in parallel.
// The lack to access the current interpreter frame is a lack in the dynrpg API design.
// Have to fix this. The current frame should be easy to access
std::vector<lcf::rpg::EventCommand> cmdList = { outputCommand };
interpreter->Push(cmdList, 0, false);

return true;
}

bool DynRpg::EasyRpgPlugin::Invoke(StringView func, dyn_arg_list args, bool& do_yield, Game_Interpreter* interpreter) {
if (func == "call") {
return EasyCall(args, do_yield, interpreter);
} else if (func == "easyrpg_output") {
return EasyOput(args);
} else if (func == "easyrpg_add") {
return EasyAdd(args);
} else if (func == "easyrpg_raw") {
return EasyRaw(args, interpreter);
}
return false;
}

void DynRpg::EasyRpgPlugin::Load(const std::vector<uint8_t>& buffer) {
Expand Down
8 changes: 6 additions & 2 deletions src/dynrpg_easyrpg.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#ifndef EP_DYNRPG_EASYRPG_H
#define EP_DYNRPG_EASYRPG_H

#include "dynrpg.h"
#include "game_dynrpg.h"
#include "game_battle.h"
#include "game_map.h"

Expand All @@ -31,9 +31,13 @@ namespace DynRpg {
public:
EasyRpgPlugin() : DynRpgPlugin("EasyRpgPlugin") {}

void RegisterFunctions() override;
bool Invoke(StringView func, dyn_arg_list args, bool& do_yield, Game_Interpreter* interpreter) override;
void Load(const std::vector<uint8_t>& buffer) override;
std::vector<uint8_t> Save() override;

private:
bool EasyCall(dyn_arg_list args, bool& do_yield, Game_Interpreter* interpreter);
bool EasyRaw(dyn_arg_list args, Game_Interpreter* interpreter);
};
}

Expand Down
25 changes: 17 additions & 8 deletions src/dynrpg_textplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,14 +399,23 @@ static bool RemoveAll(dyn_arg_list) {
return true;
}

void DynRpg::TextPlugin::RegisterFunctions() {
DynRpg::RegisterFunction("write_text", WriteText);
DynRpg::RegisterFunction("append_line", AppendLine);
DynRpg::RegisterFunction("append_text", AppendText);
DynRpg::RegisterFunction("change_text", ChangeText);
DynRpg::RegisterFunction("change_position", ChangePosition);
DynRpg::RegisterFunction("remove_text", RemoveText);
DynRpg::RegisterFunction("remove_all", RemoveAll);
bool DynRpg::TextPlugin::Invoke(StringView func, dyn_arg_list args, bool& do_yield, Game_Interpreter* interpreter) {
if (func == "write_text") {
return WriteText(args);
} else if (func == "append_line") {
return AppendLine(args);
} else if (func == "append_text") {
return AppendText(args);
} else if (func == "change_text") {
return ChangeText(args);
} else if (func == "change_position") {
return ChangePosition(args);
} else if (func == "remove_text") {
return RemoveText(args);
} else if (func == "remove_all") {
return RemoveAll(args);
}
return false;
}

void DynRpg::TextPlugin::Update() {
Expand Down
6 changes: 3 additions & 3 deletions src/dynrpg_textplugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
#ifndef EP_DYNRPG_TEXTPLUGIN_H
#define EP_DYNRPG_TEXTPLUGIN_H

#include "dynrpg.h"
#include "game_dynrpg.h"

namespace DynRpg {
class TextPlugin : public DynRpgPlugin {
public:
TextPlugin() : DynRpgPlugin("DynTextPlugin") {}
~TextPlugin();
~TextPlugin() override;

void RegisterFunctions() override;
bool Invoke(StringView func, dyn_arg_list args, bool& do_yield, Game_Interpreter* interpreter) override;
void Update() override;
void Load(const std::vector<uint8_t>&) override;
std::vector<uint8_t> Save() override;
Expand Down
Loading

0 comments on commit b3c5e4c

Please sign in to comment.