Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable Substitution: Options for enabling substution in command strings & for applying {fmt}-style number formatting #3220

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
63 changes: 40 additions & 23 deletions src/game_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ bool Game_Interpreter::CommandChangeFaceGraphic(lcf::rpg::EventCommand const& co
return false;
}

Main_Data::game_system->SetMessageFaceName(ToString(CommandStringOrVariableBitfield(com, 3, 0, 4)));
Main_Data::game_system->SetMessageFaceName(CommandStringOrVariableBitfield(com, 3, 0, 4));
Main_Data::game_system->SetMessageFaceIndex(ValueOrVariableBitfield(com, 3, 1, 0));
Main_Data::game_system->SetMessageFaceRightPosition(com.parameters[1] != 0);
Main_Data::game_system->SetMessageFaceFlipped(com.parameters[2] != 0);
Expand Down Expand Up @@ -1785,33 +1785,47 @@ int Game_Interpreter::ValueOrVariableBitfield(lcf::rpg::EventCommand const& com,
return com.parameters[val_idx];
}

StringView Game_Interpreter::CommandStringOrVariable(lcf::rpg::EventCommand const& com, int mode_idx, int val_idx) {
std::string Game_Interpreter::CommandString(lcf::rpg::EventCommand const& com) {
#ifdef LIBLCF_STUB_COMSTRING_VARSUBSTITUTION
if (!Player::HasEasyRpgExtensions()) {
#else
if (!Player::HasEasyRpgExtensions() || !lcf::Data::system.easyrpg_var_substitution_in_commands) {
#endif
return ToString(com.string);
}
std::string command_string = ToString(com.string);
PendingMessage::ApplyTextInsertingCommands(command_string, Player::escape_char, Game_Message::CommandCodeInserter);
return command_string;
}


std::string Game_Interpreter::CommandStringOrVariable(lcf::rpg::EventCommand const& com, int mode_idx, int val_idx) {
if (!Player::IsPatchManiac()) {
return com.string;
return CommandString(com);
}

assert(mode_idx != val_idx);

if (com.parameters.size() > std::max(mode_idx, val_idx)) {
return Main_Data::game_strings->GetWithMode(ToString(com.string), com.parameters[mode_idx], com.parameters[val_idx], *Main_Data::game_variables);
return ToString(Main_Data::game_strings->GetWithMode(ToString(com.string), com.parameters[mode_idx], com.parameters[val_idx], *Main_Data::game_variables));
}

return com.string;
return CommandString(com);
}

StringView Game_Interpreter::CommandStringOrVariableBitfield(lcf::rpg::EventCommand const& com, int mode_idx, int shift, int val_idx) {
std::string Game_Interpreter::CommandStringOrVariableBitfield(lcf::rpg::EventCommand const& com, int mode_idx, int shift, int val_idx) {
if (!Player::IsPatchManiac()) {
return com.string;
return CommandString(com);
}

assert(mode_idx != val_idx);

if (com.parameters.size() >= std::max(mode_idx, val_idx) + 1) {
int mode = com.parameters[mode_idx];
return Main_Data::game_strings->GetWithMode(ToString(com.string), (mode & (0xF << shift * 4)) >> shift * 4, com.parameters[val_idx], *Main_Data::game_variables);
return ToString(Main_Data::game_strings->GetWithMode(ToString(com.string), (mode & (0xF << shift * 4)) >> shift * 4, com.parameters[val_idx], *Main_Data::game_variables));
}

return com.string;
return CommandString(com);
}

bool Game_Interpreter::CommandChangeParameters(lcf::rpg::EventCommand const& com) { // Code 10430
Expand Down Expand Up @@ -2088,7 +2102,7 @@ bool Game_Interpreter::CommandWait(lcf::rpg::EventCommand const& com) { // code

bool Game_Interpreter::CommandPlayBGM(lcf::rpg::EventCommand const& com) { // code 11510
lcf::rpg::Music music;
music.name = ToString(CommandStringOrVariableBitfield(com, 4, 0, 5));
music.name = CommandStringOrVariableBitfield(com, 4, 0, 5);

music.fadein = ValueOrVariableBitfield(com, 4, 1, 0);
music.volume = ValueOrVariableBitfield(com, 4, 2, 1);
Expand All @@ -2107,7 +2121,7 @@ bool Game_Interpreter::CommandFadeOutBGM(lcf::rpg::EventCommand const& com) { //

bool Game_Interpreter::CommandPlaySound(lcf::rpg::EventCommand const& com) { // code 11550
lcf::rpg::Sound sound;
sound.name = ToString(CommandStringOrVariableBitfield(com, 3, 0, 4));
sound.name = CommandStringOrVariableBitfield(com, 3, 0, 4);

sound.volume = ValueOrVariableBitfield(com, 3, 1, 0);
sound.tempo = ValueOrVariableBitfield(com, 3, 2, 1);
Expand Down Expand Up @@ -2179,7 +2193,7 @@ bool Game_Interpreter::CommandChangeHeroName(lcf::rpg::EventCommand const& com)
return true;
}

actor->SetName(ToString(CommandStringOrVariableBitfield(com, 1, 1, 2)));
actor->SetName(CommandStringOrVariableBitfield(com, 1, 1, 2));
return true;
}

Expand All @@ -2192,7 +2206,7 @@ bool Game_Interpreter::CommandChangeHeroTitle(lcf::rpg::EventCommand const& com)
return true;
}

actor->SetTitle(ToString(CommandStringOrVariableBitfield(com, 1, 1, 2)));
actor->SetTitle(CommandStringOrVariableBitfield(com, 1, 1, 2));
return true;
}

Expand All @@ -2205,7 +2219,7 @@ bool Game_Interpreter::CommandChangeSpriteAssociation(lcf::rpg::EventCommand con
return true;
}

auto file = ToString(CommandStringOrVariableBitfield(com, 3, 1, 4));
auto file = CommandStringOrVariableBitfield(com, 3, 1, 4);
int idx = ValueOrVariableBitfield(com, 3, 2, 1);
bool transparent = com.parameters[2] != 0;
actor->SetSprite(file, idx, transparent);
Expand All @@ -2223,7 +2237,7 @@ bool Game_Interpreter::CommandChangeActorFace(lcf::rpg::EventCommand const& com)
}

actor->SetFace(
ToString(CommandStringOrVariableBitfield(com, 2, 1, 3)),
CommandStringOrVariableBitfield(com, 2, 1, 3),
ValueOrVariableBitfield(com, 2, 2, 1));
return true;
}
Expand All @@ -2237,7 +2251,7 @@ bool Game_Interpreter::CommandChangeVehicleGraphic(lcf::rpg::EventCommand const&
return true;
}

const std::string& name = ToString(com.string);
std::string name = CommandString(com);
int vehicle_index = com.parameters[1];

vehicle->SetSpriteGraphic(name, vehicle_index);
Expand All @@ -2249,7 +2263,7 @@ bool Game_Interpreter::CommandChangeVehicleGraphic(lcf::rpg::EventCommand const&
bool Game_Interpreter::CommandChangeSystemBGM(lcf::rpg::EventCommand const& com) { //code 10660
lcf::rpg::Music music;
int context = com.parameters[0];
music.name = ToString(com.string);
music.name = CommandString(com);
music.fadein = com.parameters[1];
music.volume = com.parameters[2];
music.tempo = com.parameters[3];
Expand All @@ -2261,7 +2275,7 @@ bool Game_Interpreter::CommandChangeSystemBGM(lcf::rpg::EventCommand const& com)
bool Game_Interpreter::CommandChangeSystemSFX(lcf::rpg::EventCommand const& com) { //code 10670
lcf::rpg::Sound sound;
int context = com.parameters[0];
sound.name = ToString(com.string);
sound.name = CommandString(com);
sound.volume = com.parameters[1];
sound.tempo = com.parameters[2];
sound.balance = com.parameters[3];
Expand All @@ -2270,7 +2284,7 @@ bool Game_Interpreter::CommandChangeSystemSFX(lcf::rpg::EventCommand const& com)
}

bool Game_Interpreter::CommandChangeSystemGraphics(lcf::rpg::EventCommand const& com) { // code 10680
Main_Data::game_system->SetSystemGraphic(ToString(CommandStringOrVariable(com, 2, 3)),
Main_Data::game_system->SetSystemGraphic(CommandStringOrVariable(com, 2, 3),
static_cast<lcf::rpg::System::Stretch>(com.parameters[0]),
static_cast<lcf::rpg::System::Font>(com.parameters[1]));

Expand Down Expand Up @@ -2796,7 +2810,7 @@ bool Game_Interpreter::CommandShowPicture(lcf::rpg::EventCommand const& com) { /
int pic_id = com.parameters[0];

Game_Pictures::ShowParams params = {};
params.name = ToString(com.string);
params.name = CommandString(com);
// Maniac Patch uses the upper bits for X/Y origin, mask it away
int pos_mode = ManiacBitmask(com.parameters[1], 0xFF);
params.position_x = ValueOrVariable(pos_mode, com.parameters[2]);
Expand Down Expand Up @@ -2830,7 +2844,7 @@ bool Game_Interpreter::CommandShowPicture(lcf::rpg::EventCommand const& com) { /
// Handling of RPG2k3 1.12 chunks
if (Player::IsPatchManiac()) {
pic_id = ValueOrVariableBitfield(com.parameters[17], 0, pic_id);
params.name = ToString(CommandStringOrVariableBitfield(com, 17, 2, 30));
params.name = CommandStringOrVariableBitfield(com, 17, 2, 30);
} else {
pic_id = ValueOrVariable(com.parameters[17], pic_id);
}
Expand Down Expand Up @@ -3390,7 +3404,7 @@ bool Game_Interpreter::CommandChangeMapTileset(lcf::rpg::EventCommand const& com

bool Game_Interpreter::CommandChangePBG(lcf::rpg::EventCommand const& com) { // code 11720
Game_Map::Parallax::Params params;
params.name = ToString(com.string);
params.name = CommandString(com);
params.scroll_horz = com.parameters[0] != 0;
params.scroll_vert = com.parameters[1] != 0;
params.scroll_horz_auto = com.parameters[2] != 0;
Expand Down Expand Up @@ -3552,9 +3566,12 @@ bool Game_Interpreter::CommandConditionalBranch(lcf::rpg::EventCommand const& co
result = Main_Data::game_party->IsActorInParty(actor_id);
break;
case 1:
{
// Name
result = (actor->GetName() == com.string);
std::string name = CommandString(com);
result = (actor->GetName() == name);
break;
}
case 2:
// Higher or equal level
result = (actor->GetLevel() >= com.parameters[3]);
Expand Down
7 changes: 5 additions & 2 deletions src/game_interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,11 @@ class Game_Interpreter
static int ValueOrVariableBitfield(int mode, int shift, int val);
// Range checked, conditional version (slower) of ValueOrVariableBitfield
static int ValueOrVariableBitfield(lcf::rpg::EventCommand const& com, int mode_idx, int shift, int val_idx);
static StringView CommandStringOrVariable(lcf::rpg::EventCommand const& com, int mode_idx, int val_idx);
static StringView CommandStringOrVariableBitfield(lcf::rpg::EventCommand const& com, int mode_idx, int shift, int val_idx);

static std::string CommandString(lcf::rpg::EventCommand const& com);
static std::string CommandStringOrVariable(lcf::rpg::EventCommand const& com, int mode_idx, int val_idx);
static std::string CommandStringOrVariableBitfield(lcf::rpg::EventCommand const& com, int mode_idx, int shift, int val_idx);


/**
* When current frame finishes executing we pop the stack
Expand Down
2 changes: 1 addition & 1 deletion src/game_interpreter_battle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ bool Game_Interpreter_Battle::CommandShowHiddenMonster(lcf::rpg::EventCommand co
}

bool Game_Interpreter_Battle::CommandChangeBattleBG(lcf::rpg::EventCommand const& com) {
Game_Battle::ChangeBackground(ToString(com.string));
Game_Battle::ChangeBackground(CommandString(com));
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions src/game_interpreter_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ bool Game_Interpreter_Map::CommandEnemyEncounter(lcf::rpg::EventCommand const& c
Game_Map::SetupBattle(args);
break;
case 1:
args.background = ToString(com.string);
args.background = CommandString(com);

if (Player::IsRPG2k3()) {
args.formation = static_cast<lcf::rpg::System::BattleFormation>(com.parameters[7]);
Expand Down Expand Up @@ -699,7 +699,7 @@ bool Game_Interpreter_Map::CommandPlayMovie(lcf::rpg::EventCommand const& com) {
return false;
}

auto filename = ToString(com.string);
auto filename = CommandString(com);
int pos_x = ValueOrVariable(com.parameters[0], com.parameters[1]);
int pos_y = ValueOrVariable(com.parameters[0], com.parameters[2]);
int res_x = com.parameters[3];
Expand Down
Loading