Fixing minor bugs in scripts

This commit is contained in:
Vladislav Khorev committed 2026-06-09 23:57:27 +03:00
1 parent 80133aa8c8
commit 87fb6d4655
14 files changed
+490 -234

No files matched your search

+95 -95
View File
@@ -33,7 +33,7 @@ namespace ZL {
ScriptEngine::ScriptEngine() = default;
ScriptEngine::~ScriptEngine() = default;
void ScriptEngine::init(Location* game, Inventory* inventory, const std::string& scriptPath) {
void ScriptEngine::init(Location* loc, Inventory* inventory, const std::string& scriptPath) {
impl = std::make_unique<Impl>();
sol::state& lua = impl->lua;
@@ -45,8 +45,8 @@ namespace ZL {
// on_arrived is an optional Lua function called when the NPC reaches the target.
// It can call npc_walk_to again (or anything else) to chain behaviour.
api.set_function("npc_walk_to",
[game](int index, float x, float y, float z, sol::object on_arrived) {
auto& npcs = game->npcs;
[loc](int index, float x, float y, float z, sol::object on_arrived) {
auto& npcs = loc->npcs;
if (index < 0 || index >= static_cast<int>(npcs.size())) {
std::cerr << "[script] npc_walk_to: index " << index
<< " out of range (0.." << npcs.size() - 1 << ")\n";
@@ -68,7 +68,7 @@ namespace ZL {
});
api.set_function("player_walk_to",
[game](float x, float y, float z, sol::object on_arrived) {
[loc](float x, float y, float z, sol::object on_arrived) {
std::function<void()> cb;
if (on_arrived.is<sol::protected_function>()) {
@@ -81,8 +81,8 @@ namespace ZL {
}
};
}
game->player->homePosition = Eigen::Vector3f(x, 0.f, z);
game->player->setTarget(Eigen::Vector3f(x, y, z), std::move(cb));
loc->player->homePosition = Eigen::Vector3f(x, 0.f, z);
loc->player->setTarget(Eigen::Vector3f(x, y, z), std::move(cb));
});
@@ -117,14 +117,14 @@ namespace ZL {
});
// remove_item(item_id)
api.set_function("remove_item", [game, inventory](const std::string& id) {
api.set_function("remove_item", [loc, inventory](const std::string& id) {
std::cout << "[script] remove_item: " << id << std::endl;
inventory->removeItem(id);
});
// deactivate_interactive_object(object_name)
api.set_function("deactivate_interactive_object", [game](const std::string& objectName) {
for (auto& intObj : game->interactiveObjects) {
api.set_function("deactivate_interactive_object", [loc](const std::string& objectName) {
for (auto& intObj : loc->interactiveObjects) {
if (intObj.loadedObject.name == objectName) {
intObj.isActive = false;
std::cout << "[script] deactivate_interactive_object: " << objectName << std::endl;
@@ -135,8 +135,8 @@ namespace ZL {
});
// activate_interactive_object(object_name)
api.set_function("activate_interactive_object", [game](const std::string& objectName) {
for (auto& intObj : game->interactiveObjects) {
api.set_function("activate_interactive_object", [loc](const std::string& objectName) {
for (auto& intObj : loc->interactiveObjects) {
if (intObj.loadedObject.name == objectName) {
intObj.isActive = true;
std::cout << "[script] activate_interactive_object: " << objectName << std::endl;
@@ -146,8 +146,8 @@ namespace ZL {
std::cerr << "[script] activate_interactive_object: not found: " << objectName << std::endl;
});
api.set_function("set_object_rotation", [game](const std::string& objectName, float value) {
for (auto& intObj : game->interactiveObjects) {
api.set_function("set_object_rotation", [loc](const std::string& objectName, float value) {
for (auto& intObj : loc->interactiveObjects) {
if (intObj.loadedObject.name == objectName) {
intObj.rotationY = value * static_cast<float>(M_PI) / 180.f;
std::cout << "[script] set_object_rotation: " << objectName << " " << value << std::endl;
@@ -157,8 +157,8 @@ namespace ZL {
std::cerr << "[script] set_object_rotation: not found: " << objectName << std::endl;
});
api.set_function("set_object_alpha", [game](const std::string& objectName, float value) {
for (auto& intObj : game->interactiveObjects) {
api.set_function("set_object_alpha", [loc](const std::string& objectName, float value) {
for (auto& intObj : loc->interactiveObjects) {
if (intObj.loadedObject.name == objectName) {
intObj.alpha = value;
std::cout << "[script] set_object_alpha: " <<objectName << " " << value << std::endl;
@@ -179,16 +179,16 @@ namespace ZL {
});
api.set_function("start_dialogue",
[game](const std::string& dialogueId) {
if (!game->requestDialogueStart(dialogueId)) {
[loc](const std::string& dialogueId) {
if (!loc->requestDialogueStart(dialogueId)) {
std::cerr << "[script] start_dialogue failed for id: " << dialogueId << "\n";
}
});
// start_cutscene(cutscene_id)
api.set_function("start_cutscene",
[game](const std::string& cutsceneId) {
if (!game->requestCutsceneStart(cutsceneId))
[loc](const std::string& cutsceneId) {
if (!loc->requestCutsceneStart(cutsceneId))
std::cerr << "[script] start_cutscene failed for id: " << cutsceneId << "\n";
});
@@ -201,26 +201,26 @@ namespace ZL {
});
api.set_function("set_dialogue_flag",
[game](const std::string& flag, int value) {
game->setDialogueFlag(flag, value);
[loc](const std::string& flag, int value) {
loc->setDialogueFlag(flag, value);
});
api.set_function("get_dialogue_flag",
[game](const std::string& flag) {
return game->getDialogueFlag(flag);
[loc](const std::string& flag) {
return loc->getDialogueFlag(flag);
});
api.set_function("set_navigation_area_available",
[game](const std::string& areaName, bool available) {
if (!game->setNavigationAreaAvailable(areaName, available)) {
[loc](const std::string& areaName, bool available) {
if (!loc->setNavigationAreaAvailable(areaName, available)) {
std::cerr << "[script] set_navigation_area_available: area not found: "
<< areaName << "\n";
}
});
api.set_function("switch_navigation",
[game](int index) {
if (!game->switchNavigation(index)) {
[loc](int index) {
if (!loc->switchNavigation(index)) {
std::cerr << "[script] switch_navigation: index " << index << " out of range\n";
}
});
@@ -240,47 +240,47 @@ namespace ZL {
// Triggers the white-flash transition to toggle darklands mode.
// Does nothing if a transition is already in progress.
api.set_function("start_darklands_transition",
[game]() {
if (game->requestDarklandsTransition)
game->requestDarklandsTransition();
[loc]() {
if (loc->requestDarklandsTransition)
loc->requestDarklandsTransition();
});
api.set_function("set_day",
[game]() {
game->requestNightDayTransition(false, false);
[loc]() {
loc->requestNightDayTransition(false, false);
});
api.set_function("set_night",
[game]() {
game->requestNightDayTransition(true, false);
[loc]() {
loc->requestNightDayTransition(true, false);
std::cout << "Set night called" << std::endl;
});
api.set_function("set_dawn",
[game]() {
game->requestNightDayTransition(true, true);
[loc]() {
loc->requestNightDayTransition(true, true);
std::cout << "Set dawn called" << std::endl;
});
api.set_function("is_night",
[game]() {
return game->isNight;
[loc]() {
return loc->isNight;
});
api.set_function("is_dawn",
[game]() {
return game->isDawn;
[loc]() {
return loc->isDawn;
});
// advance_darklands_hud()
// Advances the uni_interior darklands HUD from step12 to step13.
// Call when the player enters the ghost trigger zone in darklands.
api.set_function("advance_darklands_hud",
[game]() {
if (game->requestAdvanceDarklandsHud)
game->requestAdvanceDarklandsHud();
[loc]() {
if (loc->requestAdvanceDarklandsHud)
loc->requestAdvanceDarklandsHud();
});
// set_location_callbacks(on_enter, on_exit)
@@ -296,7 +296,7 @@ namespace ZL {
// is_darklands() → bool
api.set_function("is_darklands",
[game]() { return game->isDarklands; });
[loc]() { return loc->isDarklands; });
api.set_function("setFloatValue",
[this_impl = impl.get()](const std::string& key, float value) {
@@ -311,31 +311,31 @@ namespace ZL {
});
api.set_function("get_player_x",
[game]() -> float {
return game->player ? game->player->position.x() : 0.0f;
[loc]() -> float {
return loc->player ? loc->player->position.x() : 0.0f;
});
api.set_function("get_player_z",
[game]() -> float {
return game->player ? game->player->position.z() : 0.0f;
[loc]() -> float {
return loc->player ? loc->player->position.z() : 0.0f;
});
// set_player_hp(value) — sets the player's current HP directly.
api.set_function("set_player_hp",
[game](float value) {
if (game->player)
[loc](float value) {
if (loc->player)
{
game->player->hp = value;
if (game->player->currentState == AnimationState::ACTION_TO_DEATH || game->player->currentState == AnimationState::DEATH_IDLE)
loc->player->hp = value;
if (loc->player->currentState == AnimationState::ACTION_TO_DEATH || loc->player->currentState == AnimationState::DEATH_IDLE)
{
game->player->currentState = AnimationState::STAND;
game->player->resetAnim = true;
loc->player->currentState = AnimationState::STAND;
loc->player->resetAnim = true;
}
}
});
// get_player_hp() → float (0 if no player)
api.set_function("get_player_hp",
[game]() -> float {
return game->player ? game->player->hp : 0.0f;
[loc]() -> float {
return loc->player ? loc->player->hp : 0.0f;
});
// Global integer store — shared across all location scripts.
@@ -365,15 +365,15 @@ namespace ZL {
});
api.set_function("close_phone",
[game]() {
if (game->requestClosePhone)
game->requestClosePhone();
[loc]() {
if (loc->requestClosePhone)
loc->requestClosePhone();
});
api.set_function("return_to_main_menu",
[game]() {
if (game->requestReturnToMainMenu)
game->requestReturnToMainMenu();
[loc]() {
if (loc->requestReturnToMainMenu)
loc->requestReturnToMainMenu();
});
api.set_function("set_chat_callbacks",
@@ -396,8 +396,8 @@ namespace ZL {
});
api.set_function("set_trigger_zone_enabled",
[game](int index, bool value) {
auto& triggerZones = game->triggerZones;
[loc](int index, bool value) {
auto& triggerZones = loc->triggerZones;
if (index < 0 || index >= static_cast<int>(triggerZones.size())) {
std::cerr << "[script] set_trigger_zone_enabled: index " << index << " out of range\n";
return;
@@ -408,8 +408,8 @@ namespace ZL {
// set_teleport_active(index, active) — activates or deactivates a teleport zone by index.
// Swaps the particle effect texture to match the new state.
api.set_function("set_teleport_active",
[game](int index, bool value) {
auto& zones = game->teleportZones;
[loc](int index, bool value) {
auto& zones = loc->teleportZones;
if (index < 0 || index >= static_cast<int>(zones.size())) {
std::cerr << "[script] set_teleport_active: index " << index << " out of range\n";
return;
@@ -419,8 +419,8 @@ namespace ZL {
// is_teleport_active(index) → bool
api.set_function("is_teleport_active",
[game](int index) -> bool {
auto& zones = game->teleportZones;
[loc](int index) -> bool {
auto& zones = loc->teleportZones;
if (index < 0 || index >= static_cast<int>(zones.size())) {
std::cerr << "[script] is_teleport_active: index " << index << " out of range\n";
return false;
@@ -431,8 +431,8 @@ namespace ZL {
// npc_set_hp(index, value) — sets an NPC's current HP directly.
api.set_function("npc_set_hp",
[game](int index, float value) {
auto& npcs = game->npcs;
[loc](int index, float value) {
auto& npcs = loc->npcs;
if (index < 0 || index >= static_cast<int>(npcs.size())) {
std::cerr << "[script] npc_set_hp: index " << index << " out of range\n";
return;
@@ -442,8 +442,8 @@ namespace ZL {
// npc_set_position(index, x, y, z) — teleports an NPC instantly, no walking.
api.set_function("npc_set_position",
[game](int index, float x, float y, float z) {
auto& npcs = game->npcs;
[loc](int index, float x, float y, float z) {
auto& npcs = loc->npcs;
if (index < 0 || index >= static_cast<int>(npcs.size())) {
std::cerr << "[script] npc_set_position: index " << index << " out of range\n";
return;
@@ -456,8 +456,8 @@ namespace ZL {
// npc_set_rotation(index, angle) — sets NPC facing angle around Y axis (degrees).
api.set_function("npc_set_rotation",
[game](int index, float angle) {
auto& npcs = game->npcs;
[loc](int index, float angle) {
auto& npcs = loc->npcs;
if (index < 0 || index >= static_cast<int>(npcs.size())) {
std::cerr << "[script] npc_set_rotation: index " << index << " out of range\n";
return;
@@ -469,8 +469,8 @@ namespace ZL {
// set_npc_enabled(index, enabled)
api.set_function("set_npc_enabled",
[game](int index, bool value) {
auto& npcs = game->npcs;
[loc](int index, bool value) {
auto& npcs = loc->npcs;
if (index < 0 || index >= static_cast<int>(npcs.size())) {
std::cerr << "[script] set_npc_enabled: index " << index << " out of range\n";
return;
@@ -481,16 +481,16 @@ namespace ZL {
npcs[index]->currentState = AnimationState::STAND;
if (npcs[index]->canAttack)
{
npcs[index]->attackTarget = game->player.get();
npcs[index]->attackTarget = loc->player.get();
}
npcs[index]->battle_state = 0;
});
// move_object(name, x, y, z, duration_sec [, on_complete])
api.set_function("move_object",
[game](const std::string& name, float x, float y, float z,
[loc](const std::string& name, float x, float y, float z,
float durationSec, sol::object onComplete) {
for (auto& intObj : game->interactiveObjects) {
for (auto& intObj : loc->interactiveObjects) {
if (intObj.loadedObject.name != name) continue;
if (intObj.isAnimating) {
std::cerr << "[script] move_object: '" << name << "' is already animating\n";
@@ -515,9 +515,9 @@ namespace ZL {
// rotate_object(name, angle_deg, duration_sec [, on_complete])
api.set_function("rotate_object",
[game](const std::string& name, float angleDeg,
[loc](const std::string& name, float angleDeg,
float durationSec, sol::object onComplete) {
for (auto& intObj : game->interactiveObjects) {
for (auto& intObj : loc->interactiveObjects) {
if (intObj.loadedObject.name != name) continue;
if (intObj.isAnimating) {
std::cerr << "[script] rotate_object: '" << name << "' is already animating\n";
@@ -543,9 +543,9 @@ namespace ZL {
// fade_object(name, target_alpha, duration_sec [, on_complete])
api.set_function("fade_object",
[game](const std::string& name, float targetAlpha,
[loc](const std::string& name, float targetAlpha,
float durationSec, sol::object onComplete) {
for (auto& intObj : game->interactiveObjects) {
for (auto& intObj : loc->interactiveObjects) {
if (intObj.loadedObject.name != name) continue;
if (intObj.isAnimating) {
std::cerr << "[script] fade_object: '" << name << "' is already animating\n";
@@ -570,9 +570,9 @@ namespace ZL {
// scale_object(name, target_scale, duration_sec [, on_complete])
api.set_function("scale_object",
[game](const std::string& name, float targetScale,
[loc](const std::string& name, float targetScale,
float durationSec, sol::object onComplete) {
for (auto& intObj : game->interactiveObjects) {
for (auto& intObj : loc->interactiveObjects) {
if (intObj.loadedObject.name != name) continue;
if (intObj.isAnimating) {
std::cerr << "[script] scale_object: '" << name << "' is already animating\n";
@@ -596,20 +596,20 @@ namespace ZL {
});
api.set_function("resetPlayerAfterDeath",
[game]() {
if (game->player) game->player->resetPlayerAfterDeath();
[loc]() {
if (loc->player) loc->player->resetPlayerAfterDeath();
});
// player_stop() — cancels the player's current walk target and stops them immediately.
api.set_function("player_stop",
[game]() {
if (game->player) game->player->stopInPlace();
[loc]() {
if (loc->player) loc->player->stopInPlace();
});
// npc_stop(index) — cancels an NPC's current walk target and stops them immediately.
api.set_function("npc_stop",
[game](int index) {
auto& npcs = game->npcs;
[loc](int index) {
auto& npcs = loc->npcs;
if (index < 0 || index >= static_cast<int>(npcs.size())) {
std::cerr << "[script] npc_stop: index " << index
<< " out of range (0.." << npcs.size() - 1 << ")\n";
@@ -694,11 +694,11 @@ namespace ZL {
});
api.set_function("call_tutorial_taxi_required",
[game]() -> void {
if (game->onPlayerTaxiRequired)
[loc]() -> void {
if (loc->onPlayerTaxiRequired)
{
game->onPlayerTaxiRequired();
game->onPlayerTaxiRequired = nullptr;
loc->onPlayerTaxiRequired();
loc->onPlayerTaxiRequired = nullptr;
}
else {
std::cerr << "[script] call_tutorial_taxi_required: function is empty\n";