@ -1,5 +1,7 @@
# include "luabindings.hpp"
# include <components/esm3/loadfact.hpp>
# include <components/esm3/loadnpc.hpp>
# include <components/lua/luastate.hpp>
# include "../mwworld/cellstore.hpp"
@ -142,6 +144,62 @@ namespace MWLua
context . mLuaEvents - > addLocalEvent (
{ dest . id ( ) , std : : move ( eventName ) , LuaUtil : : serialize ( eventData , context . mSerializer ) } ) ;
} ;
auto getOwnerRecordId = [ ] ( const ObjectT & o ) - > sol : : optional < std : : string > {
ESM : : RefId owner = o . ptr ( ) . getCellRef ( ) . getOwner ( ) ;
if ( owner . empty ( ) )
return sol : : nullopt ;
else
return owner . serializeText ( ) ;
} ;
auto setOwnerRecordId = [ ] ( const ObjectT & obj , sol : : optional < std : : string_view > ownerId ) {
if ( std : : is_same_v < ObjectT , LObject > & & ! dynamic_cast < const SelfObject * > ( & obj ) )
throw std : : runtime_error ( " Local scripts can set an owner only on self " ) ;
const MWWorld : : Ptr & ptr = obj . ptr ( ) ;
if ( ! ownerId )
{
ptr . getCellRef ( ) . setOwner ( ESM : : RefId ( ) ) ;
return ;
}
ESM : : RefId owner = ESM : : RefId : : deserializeText ( * ownerId ) ;
const MWWorld : : ESMStore & store = * MWBase : : Environment : : get ( ) . getESMStore ( ) ;
if ( ! store . get < ESM : : NPC > ( ) . search ( owner ) )
throw std : : runtime_error ( " Invalid owner record id " ) ;
ptr . getCellRef ( ) . setOwner ( owner ) ;
} ;
objectT [ " ownerRecordId " ] = sol : : property ( getOwnerRecordId , setOwnerRecordId ) ;
auto getOwnerFactionId = [ ] ( const ObjectT & o ) - > sol : : optional < std : : string > {
ESM : : RefId owner = o . ptr ( ) . getCellRef ( ) . getFaction ( ) ;
if ( owner . empty ( ) )
return sol : : nullopt ;
else
return owner . serializeText ( ) ;
} ;
auto setOwnerFactionId = [ ] ( const ObjectT & object , sol : : optional < std : : string > ownerId ) {
ESM : : RefId ownerFac ;
if ( std : : is_same_v < ObjectT , LObject > & & ! dynamic_cast < const SelfObject * > ( & object ) )
throw std : : runtime_error ( " Local scripts can set an owner faction only on self " ) ;
if ( ! ownerId )
{
object . ptr ( ) . getCellRef ( ) . setFaction ( ESM : : RefId ( ) ) ;
return ;
}
ownerFac = ESM : : RefId : : deserializeText ( * ownerId ) ;
const MWWorld : : ESMStore & store = * MWBase : : Environment : : get ( ) . getESMStore ( ) ;
if ( ! store . get < ESM : : Faction > ( ) . search ( ownerFac ) )
throw std : : runtime_error ( " Invalid owner faction id " ) ;
object . ptr ( ) . getCellRef ( ) . setFaction ( ownerFac ) ;
} ;
objectT [ " ownerFactionId " ] = sol : : property ( getOwnerFactionId , setOwnerFactionId ) ;
auto getOwnerFactionRank = [ ] ( const ObjectT & o ) - > int { return o . ptr ( ) . getCellRef ( ) . getFactionRank ( ) ; } ;
auto setOwnerFactionRank = [ ] ( const ObjectT & object , int factionRank ) {
if ( std : : is_same_v < ObjectT , LObject > & & ! dynamic_cast < const SelfObject * > ( & object ) )
throw std : : runtime_error ( " Local scripts can set an owner faction rank only on self " ) ;
object . ptr ( ) . getCellRef ( ) . setFactionRank ( factionRank ) ;
} ;
objectT [ " ownerFactionRank " ] = sol : : property ( getOwnerFactionRank , setOwnerFactionRank ) ;
objectT [ " activateBy " ] = [ ] ( const ObjectT & object , const ObjectT & actor ) {
const MWWorld : : Ptr & objPtr = object . ptr ( ) ;