2020-12-18 23:21:10 +01:00
|
|
|
#include "localscripts.hpp"
|
|
|
|
|
2022-01-23 20:49:42 +01:00
|
|
|
#include <components/esm3/loadcell.hpp>
|
2024-06-21 23:18:13 +02:00
|
|
|
#include <components/esm3/loadweap.hpp>
|
2023-01-07 20:52:36 +01:00
|
|
|
#include <components/misc/strings/lower.hpp>
|
2022-01-23 20:49:42 +01:00
|
|
|
|
2024-01-26 21:39:33 +00:00
|
|
|
#include "../mwbase/environment.hpp"
|
|
|
|
#include "../mwbase/mechanicsmanager.hpp"
|
2021-01-29 01:54:54 +01:00
|
|
|
#include "../mwmechanics/aicombat.hpp"
|
2022-01-23 20:49:42 +01:00
|
|
|
#include "../mwmechanics/aiescort.hpp"
|
|
|
|
#include "../mwmechanics/aifollow.hpp"
|
|
|
|
#include "../mwmechanics/aipackage.hpp"
|
|
|
|
#include "../mwmechanics/aipursue.hpp"
|
2021-01-29 01:54:54 +01:00
|
|
|
#include "../mwmechanics/aisequence.hpp"
|
2022-01-23 20:49:42 +01:00
|
|
|
#include "../mwmechanics/aitravel.hpp"
|
|
|
|
#include "../mwmechanics/aiwander.hpp"
|
2024-06-21 23:18:13 +02:00
|
|
|
#include "../mwmechanics/attacktype.hpp"
|
2022-07-16 16:37:31 +02:00
|
|
|
#include "../mwmechanics/creaturestats.hpp"
|
2021-01-29 01:54:54 +01:00
|
|
|
#include "../mwworld/class.hpp"
|
|
|
|
#include "../mwworld/ptr.hpp"
|
|
|
|
|
2022-08-17 19:09:38 +02:00
|
|
|
#include "context.hpp"
|
2021-03-28 17:44:08 +02:00
|
|
|
|
2020-12-18 23:21:10 +01:00
|
|
|
namespace sol
|
|
|
|
{
|
|
|
|
template <>
|
|
|
|
struct is_automagical<MWBase::LuaManager::ActorControls> : std::false_type
|
|
|
|
{
|
|
|
|
};
|
|
|
|
template <>
|
2023-03-06 02:31:58 +01:00
|
|
|
struct is_automagical<MWLua::SelfObject> : std::false_type
|
2020-12-18 23:21:10 +01:00
|
|
|
{
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace MWLua
|
|
|
|
{
|
|
|
|
|
|
|
|
void LocalScripts::initializeSelfPackage(const Context& context)
|
|
|
|
{
|
2024-08-22 22:22:28 +02:00
|
|
|
auto lua = context.sol();
|
2020-12-18 23:21:10 +01:00
|
|
|
using ActorControls = MWBase::LuaManager::ActorControls;
|
2024-08-22 22:22:28 +02:00
|
|
|
sol::usertype<ActorControls> controls = lua.new_usertype<ActorControls>("ActorControls");
|
2021-08-03 02:40:44 +03:00
|
|
|
|
|
|
|
#define CONTROL(TYPE, FIELD) \
|
|
|
|
sol::property([](const ActorControls& c) { return c.FIELD; }, \
|
|
|
|
[](ActorControls& c, const TYPE& v) { \
|
|
|
|
c.FIELD = v; \
|
|
|
|
c.mChanged = true; \
|
|
|
|
})
|
|
|
|
controls["movement"] = CONTROL(float, mMovement);
|
|
|
|
controls["sideMovement"] = CONTROL(float, mSideMovement);
|
2022-02-01 18:47:20 +00:00
|
|
|
controls["pitchChange"] = CONTROL(float, mPitchChange);
|
|
|
|
controls["yawChange"] = CONTROL(float, mYawChange);
|
2021-08-03 02:40:44 +03:00
|
|
|
controls["run"] = CONTROL(bool, mRun);
|
2022-12-05 23:31:23 +01:00
|
|
|
controls["sneak"] = CONTROL(bool, mSneak);
|
2021-08-03 02:40:44 +03:00
|
|
|
controls["jump"] = CONTROL(bool, mJump);
|
2022-02-01 18:47:20 +00:00
|
|
|
controls["use"] = CONTROL(int, mUse);
|
2021-08-03 02:40:44 +03:00
|
|
|
#undef CONTROL
|
2020-12-18 23:21:10 +01:00
|
|
|
|
2024-08-22 22:22:28 +02:00
|
|
|
sol::usertype<SelfObject> selfAPI
|
|
|
|
= lua.new_usertype<SelfObject>("SelfObject", sol::base_classes, sol::bases<LObject, Object>());
|
2020-12-18 23:21:10 +01:00
|
|
|
selfAPI[sol::meta_function::to_string]
|
|
|
|
= [](SelfObject& self) { return "openmw.self[" + self.toString() + "]"; };
|
|
|
|
selfAPI["object"] = sol::readonly_property([](SelfObject& self) -> LObject { return LObject(self); });
|
|
|
|
selfAPI["controls"] = sol::readonly_property([](SelfObject& self) { return &self.mControls; });
|
2021-04-14 00:00:51 +02:00
|
|
|
selfAPI["isActive"] = [](SelfObject& self) { return &self.mIsActive; };
|
2021-04-23 02:49:12 +02:00
|
|
|
selfAPI["enableAI"] = [](SelfObject& self, bool v) { self.mControls.mDisableAI = !v; };
|
2024-06-24 01:15:43 +02:00
|
|
|
selfAPI["ATTACK_TYPE"]
|
2024-08-22 22:22:28 +02:00
|
|
|
= LuaUtil::makeStrictReadOnly(LuaUtil::tableFromPairs<std::string_view, MWMechanics::AttackType>(lua,
|
2024-06-21 23:18:13 +02:00
|
|
|
{ { "NoAttack", MWMechanics::AttackType::NoAttack }, { "Any", MWMechanics::AttackType::Any },
|
|
|
|
{ "Chop", MWMechanics::AttackType::Chop }, { "Slash", MWMechanics::AttackType::Slash },
|
|
|
|
{ "Thrust", MWMechanics::AttackType::Thrust } }));
|
2022-01-23 20:49:42 +01:00
|
|
|
|
|
|
|
using AiPackage = MWMechanics::AiPackage;
|
2024-08-22 22:22:28 +02:00
|
|
|
sol::usertype<AiPackage> aiPackage = lua.new_usertype<AiPackage>("AiPackage");
|
2022-01-23 20:49:42 +01:00
|
|
|
aiPackage["type"] = sol::readonly_property([](const AiPackage& p) -> std::string_view {
|
|
|
|
switch (p.getTypeId())
|
|
|
|
{
|
|
|
|
case MWMechanics::AiPackageTypeId::Wander:
|
|
|
|
return "Wander";
|
|
|
|
case MWMechanics::AiPackageTypeId::Travel:
|
|
|
|
return "Travel";
|
|
|
|
case MWMechanics::AiPackageTypeId::Escort:
|
|
|
|
return "Escort";
|
|
|
|
case MWMechanics::AiPackageTypeId::Follow:
|
|
|
|
return "Follow";
|
|
|
|
case MWMechanics::AiPackageTypeId::Activate:
|
|
|
|
return "Activate";
|
|
|
|
case MWMechanics::AiPackageTypeId::Combat:
|
|
|
|
return "Combat";
|
|
|
|
case MWMechanics::AiPackageTypeId::Pursue:
|
|
|
|
return "Pursue";
|
|
|
|
case MWMechanics::AiPackageTypeId::AvoidDoor:
|
|
|
|
return "AvoidDoor";
|
|
|
|
case MWMechanics::AiPackageTypeId::Face:
|
|
|
|
return "Face";
|
|
|
|
case MWMechanics::AiPackageTypeId::Breathe:
|
|
|
|
return "Breathe";
|
|
|
|
case MWMechanics::AiPackageTypeId::Cast:
|
|
|
|
return "Cast";
|
|
|
|
default:
|
|
|
|
return "Unknown";
|
|
|
|
}
|
|
|
|
});
|
2022-12-23 19:57:12 +01:00
|
|
|
aiPackage["target"] = sol::readonly_property([](const AiPackage& p) -> sol::optional<LObject> {
|
|
|
|
MWWorld::Ptr target = p.getTarget();
|
|
|
|
if (target.isEmpty())
|
|
|
|
return sol::nullopt;
|
|
|
|
else
|
|
|
|
return LObject(getId(target));
|
|
|
|
});
|
2022-01-23 20:49:42 +01:00
|
|
|
aiPackage["sideWithTarget"] = sol::readonly_property([](const AiPackage& p) { return p.sideWithTarget(); });
|
2022-06-21 21:06:37 +02:00
|
|
|
aiPackage["destPosition"] = sol::readonly_property([](const AiPackage& p) { return p.getDestination(); });
|
2024-05-03 09:40:30 +00:00
|
|
|
aiPackage["distance"] = sol::readonly_property([](const AiPackage& p) { return p.getDistance(); });
|
|
|
|
aiPackage["duration"] = sol::readonly_property([](const AiPackage& p) { return p.getDuration(); });
|
2024-08-22 22:22:28 +02:00
|
|
|
aiPackage["idle"]
|
2024-09-06 16:57:36 +02:00
|
|
|
= sol::readonly_property([lua = lua.lua_state()](const AiPackage& p) -> sol::optional<sol::table> {
|
2024-08-22 22:22:28 +02:00
|
|
|
if (p.getTypeId() == MWMechanics::AiPackageTypeId::Wander)
|
|
|
|
{
|
|
|
|
sol::table idles(lua, sol::create);
|
|
|
|
const std::vector<unsigned char>& idle = static_cast<const MWMechanics::AiWander&>(p).getIdle();
|
|
|
|
if (!idle.empty())
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < idle.size(); ++i)
|
|
|
|
{
|
|
|
|
std::string_view groupName = MWMechanics::AiWander::getIdleGroupName(i);
|
|
|
|
idles[groupName] = idle[i];
|
|
|
|
}
|
|
|
|
return idles;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sol::nullopt;
|
|
|
|
});
|
2022-01-23 20:49:42 +01:00
|
|
|
|
2024-05-03 09:40:30 +00:00
|
|
|
aiPackage["isRepeat"] = sol::readonly_property([](const AiPackage& p) { return p.getRepeat(); });
|
2024-07-15 17:26:35 +02:00
|
|
|
|
|
|
|
selfAPI["_isFleeing"] = [](SelfObject& self) -> bool {
|
|
|
|
const MWWorld::Ptr& ptr = self.ptr();
|
|
|
|
MWMechanics::AiSequence& ai = ptr.getClass().getCreatureStats(ptr).getAiSequence();
|
|
|
|
if (ai.isEmpty())
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return ai.isFleeing();
|
|
|
|
};
|
2022-01-23 20:49:42 +01:00
|
|
|
selfAPI["_getActiveAiPackage"] = [](SelfObject& self) -> sol::optional<std::shared_ptr<AiPackage>> {
|
2021-01-29 01:54:54 +01:00
|
|
|
const MWWorld::Ptr& ptr = self.ptr();
|
|
|
|
MWMechanics::AiSequence& ai = ptr.getClass().getCreatureStats(ptr).getAiSequence();
|
2022-01-23 20:49:42 +01:00
|
|
|
if (ai.isEmpty())
|
|
|
|
return sol::nullopt;
|
2021-01-29 01:54:54 +01:00
|
|
|
else
|
2022-01-23 20:49:42 +01:00
|
|
|
return *ai.begin();
|
2021-01-29 01:54:54 +01:00
|
|
|
};
|
2022-01-23 20:49:42 +01:00
|
|
|
selfAPI["_iterateAndFilterAiSequence"] = [](SelfObject& self, sol::function callback) {
|
2021-01-29 01:54:54 +01:00
|
|
|
const MWWorld::Ptr& ptr = self.ptr();
|
|
|
|
MWMechanics::AiSequence& ai = ptr.getClass().getCreatureStats(ptr).getAiSequence();
|
2022-02-12 23:50:41 +00:00
|
|
|
|
|
|
|
ai.erasePackagesIf([&](auto& entry) {
|
|
|
|
bool keep = LuaUtil::call(callback, entry).template get<bool>();
|
|
|
|
return !keep;
|
|
|
|
});
|
2021-01-29 01:54:54 +01:00
|
|
|
};
|
2023-03-27 02:28:45 +02:00
|
|
|
selfAPI["_startAiCombat"] = [](SelfObject& self, const LObject& target, bool cancelOther) {
|
2021-01-29 01:54:54 +01:00
|
|
|
const MWWorld::Ptr& ptr = self.ptr();
|
|
|
|
MWMechanics::AiSequence& ai = ptr.getClass().getCreatureStats(ptr).getAiSequence();
|
2023-03-27 02:28:45 +02:00
|
|
|
ai.stack(MWMechanics::AiCombat(target.ptr()), ptr, cancelOther);
|
2021-01-29 01:54:54 +01:00
|
|
|
};
|
2023-03-27 02:28:45 +02:00
|
|
|
selfAPI["_startAiPursue"] = [](SelfObject& self, const LObject& target, bool cancelOther) {
|
2022-01-23 20:49:42 +01:00
|
|
|
const MWWorld::Ptr& ptr = self.ptr();
|
|
|
|
MWMechanics::AiSequence& ai = ptr.getClass().getCreatureStats(ptr).getAiSequence();
|
2023-03-27 02:28:45 +02:00
|
|
|
ai.stack(MWMechanics::AiPursue(target.ptr()), ptr, cancelOther);
|
2022-01-23 20:49:42 +01:00
|
|
|
};
|
2024-05-03 09:40:30 +00:00
|
|
|
selfAPI["_startAiFollow"] = [](SelfObject& self, const LObject& target, sol::optional<LCell> cell,
|
|
|
|
float duration, const osg::Vec3f& dest, bool repeat, bool cancelOther) {
|
2022-01-23 20:49:42 +01:00
|
|
|
const MWWorld::Ptr& ptr = self.ptr();
|
|
|
|
MWMechanics::AiSequence& ai = ptr.getClass().getCreatureStats(ptr).getAiSequence();
|
2024-05-03 09:40:30 +00:00
|
|
|
if (cell)
|
|
|
|
{
|
|
|
|
ai.stack(MWMechanics::AiFollow(target.ptr().getCellRef().getRefId(),
|
|
|
|
cell->mStore->getCell()->getNameId(), duration, dest.x(), dest.y(), dest.z(), repeat),
|
|
|
|
ptr, cancelOther);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ai.stack(MWMechanics::AiFollow(
|
|
|
|
target.ptr().getCellRef().getRefId(), duration, dest.x(), dest.y(), dest.z(), repeat),
|
|
|
|
ptr, cancelOther);
|
|
|
|
}
|
2022-01-23 20:49:42 +01:00
|
|
|
};
|
|
|
|
selfAPI["_startAiEscort"] = [](SelfObject& self, const LObject& target, LCell cell, float duration,
|
2024-05-03 09:40:30 +00:00
|
|
|
const osg::Vec3f& dest, bool repeat, bool cancelOther) {
|
2022-01-23 20:49:42 +01:00
|
|
|
const MWWorld::Ptr& ptr = self.ptr();
|
|
|
|
MWMechanics::AiSequence& ai = ptr.getClass().getCreatureStats(ptr).getAiSequence();
|
|
|
|
// TODO: change AiEscort implementation to accept ptr instead of a non-unique refId.
|
Initial commit: In ESM structures, replace the string members that are RefIds to other records, to a new strong type
The strong type is actually just a string underneath, but this will help in the future to have a distinction so it's easier to search and replace when we use an integer ID
Slowly going through all the changes to make, still hundreds of errors
a lot of functions/structures use std::string or stringview to designate an ID. So it takes time
Continues slowly replacing ids. There are technically more and more compilation errors
I have good hope that there is a point where the amount of errors will dramatically go down as all the main functions use the ESM::RefId type
Continue moving forward, changes to the stores
slowly moving along
Starting to see the fruit of those changes.
still many many error, but more and more Irun into a situation where a function is sandwiched between two functions that use the RefId type.
More replacements. Things are starting to get easier
I can see more and more often the issue is that the function is awaiting a RefId, but is given a string
there is less need to go down functions and to fix a long list of them.
Still moving forward, and for the first time error count is going down!
Good pace, not sure about topics though, mId and mName are actually the same thing and are used interchangeably
Cells are back to using string for the name, haven't fixed everything yet. Many other changes
Under the bar of 400 compilation errors.
more good progress <100 compile errors!
More progress
Game settings store can use string for find, it was a bit absurd how every use of it required to create refId from string
some more progress on other fronts
Mostly game settings clean
one error opened a lot of other errors. Down to 18, but more will prbably appear
only link errors left??
Fixed link errors
OpenMW compiles, and launches, with some issues, but still!
2022-09-25 13:17:09 +02:00
|
|
|
const ESM::RefId& refId = target.ptr().getCellRef().getRefId();
|
2022-01-23 20:49:42 +01:00
|
|
|
int gameHoursDuration = static_cast<int>(std::ceil(duration / 3600.0));
|
2023-01-27 01:13:17 +01:00
|
|
|
auto* esmCell = cell.mStore->getCell();
|
2022-01-23 20:49:42 +01:00
|
|
|
if (esmCell->isExterior())
|
2024-05-03 09:40:30 +00:00
|
|
|
ai.stack(MWMechanics::AiEscort(refId, gameHoursDuration, dest.x(), dest.y(), dest.z(), repeat), ptr,
|
2023-03-27 02:28:45 +02:00
|
|
|
cancelOther);
|
2022-01-23 20:49:42 +01:00
|
|
|
else
|
2022-12-01 20:02:39 +01:00
|
|
|
ai.stack(MWMechanics::AiEscort(
|
2024-05-03 09:40:30 +00:00
|
|
|
refId, esmCell->getNameId(), gameHoursDuration, dest.x(), dest.y(), dest.z(), repeat),
|
2023-03-27 02:28:45 +02:00
|
|
|
ptr, cancelOther);
|
2022-01-23 20:49:42 +01:00
|
|
|
};
|
2024-05-03 09:40:30 +00:00
|
|
|
selfAPI["_startAiWander"]
|
|
|
|
= [](SelfObject& self, int distance, int duration, sol::table luaIdle, bool repeat, bool cancelOther) {
|
|
|
|
const MWWorld::Ptr& ptr = self.ptr();
|
|
|
|
MWMechanics::AiSequence& ai = ptr.getClass().getCreatureStats(ptr).getAiSequence();
|
|
|
|
std::vector<unsigned char> idle;
|
|
|
|
// Lua index starts at 1
|
|
|
|
for (size_t i = 1; i <= luaIdle.size(); i++)
|
|
|
|
idle.emplace_back(luaIdle.get<unsigned char>(i));
|
|
|
|
ai.stack(MWMechanics::AiWander(distance, duration, 0, idle, repeat), ptr, cancelOther);
|
|
|
|
};
|
|
|
|
selfAPI["_startAiTravel"] = [](SelfObject& self, const osg::Vec3f& target, bool repeat, bool cancelOther) {
|
2022-01-23 20:49:42 +01:00
|
|
|
const MWWorld::Ptr& ptr = self.ptr();
|
|
|
|
MWMechanics::AiSequence& ai = ptr.getClass().getCreatureStats(ptr).getAiSequence();
|
2024-05-03 09:40:30 +00:00
|
|
|
ai.stack(MWMechanics::AiTravel(target.x(), target.y(), target.z(), repeat), ptr, cancelOther);
|
2022-01-23 20:49:42 +01:00
|
|
|
};
|
2024-01-26 21:39:33 +00:00
|
|
|
selfAPI["_enableLuaAnimations"] = [](SelfObject& self, bool enable) {
|
|
|
|
const MWWorld::Ptr& ptr = self.ptr();
|
|
|
|
MWBase::Environment::get().getMechanicsManager()->enableLuaAnimations(ptr, enable);
|
|
|
|
};
|
2020-12-18 23:21:10 +01:00
|
|
|
}
|
|
|
|
|
2024-10-15 21:18:13 +00:00
|
|
|
LocalScripts::LocalScripts(LuaUtil::LuaState* lua, const LObject& obj, LuaUtil::ScriptTracker* tracker)
|
|
|
|
: LuaUtil::ScriptsContainer(lua, "L" + obj.id().toString(), tracker, false)
|
2022-05-20 21:47:13 +02:00
|
|
|
, mData(obj)
|
2020-12-18 23:21:10 +01:00
|
|
|
{
|
2024-08-22 22:22:28 +02:00
|
|
|
lua->protectedCall(
|
|
|
|
[&](LuaUtil::LuaView& view) { addPackage("openmw.self", sol::make_object(view.sol(), &mData)); });
|
2023-06-18 21:19:34 +02:00
|
|
|
registerEngineHandlers({ &mOnActiveHandlers, &mOnInactiveHandlers, &mOnConsumeHandlers, &mOnActivatedHandlers,
|
2024-01-30 00:05:57 +01:00
|
|
|
&mOnTeleportedHandlers, &mOnAnimationTextKeyHandlers, &mOnPlayAnimationHandlers, &mOnSkillUse,
|
|
|
|
&mOnSkillLevelUp });
|
2021-04-14 00:00:51 +02:00
|
|
|
}
|
|
|
|
|
2023-03-22 01:27:22 +01:00
|
|
|
void LocalScripts::setActive(bool active)
|
2021-04-14 00:00:51 +02:00
|
|
|
{
|
2023-03-22 01:27:22 +01:00
|
|
|
mData.mIsActive = active;
|
|
|
|
if (active)
|
|
|
|
callEngineHandlers(mOnActiveHandlers);
|
|
|
|
else
|
|
|
|
callEngineHandlers(mOnInactiveHandlers);
|
2020-12-18 23:21:10 +01:00
|
|
|
}
|
|
|
|
|
2022-03-25 20:03:13 +00:00
|
|
|
void LocalScripts::applyStatsCache()
|
|
|
|
{
|
|
|
|
const auto& ptr = mData.ptr();
|
|
|
|
for (auto& [stat, value] : mData.mStatsCache)
|
|
|
|
stat(ptr, value);
|
|
|
|
mData.mStatsCache.clear();
|
|
|
|
}
|
2020-12-18 23:21:10 +01:00
|
|
|
}
|