Files
ashlar/scripts/cell/base.lua
T
David Cernat 5c39b71bc4 [Cell] Rename methods starting with Send so they start with Load instead
This makes them consistent with methods from the Player, Worldstate and Recordstore classes.
2018-08-25 11:48:35 +03:00

1746 lines
60 KiB
Lua

require("enumerations")
require("patterns")
require("utils")
contentFixer = require("contentFixer")
tableHelper = require("tableHelper")
inventoryHelper = require("inventoryHelper")
packetBuilder = require("packetBuilder")
local BaseCell = class("BaseCell")
function BaseCell:__init(cellDescription)
self.data =
{
entry = {
description = cellDescription
},
lastVisit = {},
objectData = {},
packets = {
delete = {},
place = {},
spawn = {},
lock = {},
trap = {},
scale = {},
state = {},
doorState = {},
container = {},
equipment = {},
ai = {},
death = {},
actorList = {},
position = {},
statsDynamic = {},
cellChangeTo = {},
cellChangeFrom = {}
}
}
self.description = cellDescription
self.visitors = {}
self.authority = nil
self.isRequestingContainers = false
self.containerRequestPid = nil
self.isRequestingActorList = false
self.actorListRequestPid = nil
self.unusableContainerUniqueIndexes = {}
self.isExterior = false
if string.match(cellDescription, patterns.exteriorCell) then
self.isExterior = true
local _, _, gridX, gridY = string.find(cellDescription, patterns.exteriorCell)
self.gridX = tonumber(gridX)
self.gridY = tonumber(gridY)
end
end
function BaseCell:ContainsPosition(posX, posY)
local cellSize = 8192
if self.isExterior then
local correctGridX = math.floor(posX / cellSize)
local correctGridY = math.floor(posY / cellSize)
if self.gridX ~= correctGridX or self.gridY ~= correctGridY then
return false
end
end
return true
end
function BaseCell:HasEntry()
return self.hasEntry
end
function BaseCell:IsExterior()
if string.match(self.description, patterns.exteriorCell) then
return true
end
return false
end
function BaseCell:GetVisitorCount()
return tableHelper.getCount(self.visitors)
end
function BaseCell:AddVisitor(pid)
-- Only add new visitor if we don't already have them
if tableHelper.containsValue(self.visitors, pid) == false then
table.insert(self.visitors, pid)
-- Also add a record to the player's list of loaded cells
Players[pid]:AddCellLoaded(self.description)
local shouldSendInfo = false
local lastVisitTimestamp = self.data.lastVisit[Players[pid].accountName]
-- If this player has never been in this cell, they should be
-- sent its cell data
if lastVisitTimestamp == nil then
shouldSendInfo = true
-- Otherwise, send them the cell data only if they haven't
-- visited since last connecting to the server
elseif Players[pid].initTimestamp > lastVisitTimestamp then
shouldSendInfo = true
end
if shouldSendInfo == true then
-- First, fix whatever quest problems exist in this cell
contentFixer.FixCell(pid, self.description)
self:LoadInitialCellData(pid)
end
self:LoadMomentaryCellData(pid)
end
end
function BaseCell:RemoveVisitor(pid)
-- Only remove visitor if they are actually recorded as one
if tableHelper.containsValue(self.visitors, pid) then
tableHelper.removeValue(self.visitors, pid)
-- Also remove the record from the player's list of loaded cells
Players[pid]:RemoveCellLoaded(self.description)
-- Remember when this visitor left
self:SaveLastVisit(Players[pid].accountName)
-- Were we waiting on a container request from this pid?
if self.isRequestingContainers == true and self.containerRequestPid == pid then
self.isRequestingContainers = false
self.containerRequestPid = nil
end
-- Were we waiting on an actorList request from this pid?
if self.isRequestingActorList == true and self.actorListRequestPid == pid then
self.isRequestingActorList = false
self.actorListRequestPid = nil
end
end
end
function BaseCell:GetAuthority()
return self.authority
end
function BaseCell:SetAuthority(pid)
self.authority = pid
tes3mp.LogMessage(1, "Authority of cell " .. self.data.entry.description ..
" is now " .. logicHandler.GetChatName(pid))
self:LoadActorAuthority(pid)
end
-- Iterate through saved packets and ensure the object uniqueIndexes they refer to
-- actually exist
function BaseCell:EnsurePacketValidity()
for packetType, packetArray in pairs(self.data.packets) do
for arrayIndex, uniqueIndex in pairs(self.data.packets[packetType]) do
if self.data.objectData[uniqueIndex] == nil then
tableHelper.removeValue(self.data.packets[packetType], uniqueIndex)
end
end
end
end
-- Check whether an object is in this cell
function BaseCell:ContainsObject(uniqueIndex)
if self.data.objectData[uniqueIndex] ~= nil and self.data.objectData[uniqueIndex].refId ~= nil then
return true
end
return false
end
function BaseCell:HasContainerData()
if tableHelper.isEmpty(self.data.packets.container) == true then
return false
end
return true
end
function BaseCell:HasActorData()
if tableHelper.isEmpty(self.data.packets.actorList) == true then
return false
end
return true
end
function BaseCell:InitializeObjectData(uniqueIndex, refId)
if uniqueIndex ~= nil and refId ~= nil and self.data.objectData[uniqueIndex] == nil then
self.data.objectData[uniqueIndex] = {}
self.data.objectData[uniqueIndex].refId = refId
end
end
function BaseCell:DeleteObjectData(uniqueIndex)
if self.data.objectData[uniqueIndex] == nil then
return
end
-- Is this a player's summon? If so, remove it from the summons tracked
-- for the player
local summon = self.data.objectData[uniqueIndex].summon
if summon ~= nil then
if summon.summonerPlayer ~= nil and logicHandler.IsPlayerNameLoggedIn(summon.summonerPlayer) then
logicHandler.GetPlayerByName(summon.summonerPlayer).summons[uniqueIndex] = nil
end
end
-- Delete all packets associated with an object
for packetIndex, packetType in pairs(self.data.packets) do
tableHelper.removeValue(self.data.packets[packetIndex], uniqueIndex)
end
-- Delete all object data
self.data.objectData[uniqueIndex] = nil
end
function BaseCell:MoveObjectData(uniqueIndex, newCell)
-- Move all packets about this uniqueIndex from the old cell to the new cell
for packetIndex, packetType in pairs(self.data.packets) do
if tableHelper.containsValue(self.data.packets[packetIndex], uniqueIndex) then
table.insert(newCell.data.packets[packetIndex], uniqueIndex)
tableHelper.removeValue(self.data.packets[packetIndex], uniqueIndex)
end
end
newCell.data.objectData[uniqueIndex] = self.data.objectData[uniqueIndex]
self.data.objectData[uniqueIndex] = nil
end
function BaseCell:SaveLastVisit(playerName)
self.data.lastVisit[playerName] = os.time()
end
function BaseCell:SaveObjectsDeleted(pid)
local temporaryLoadedCells = {}
tes3mp.ReadReceivedObjectList()
tes3mp.LogMessage(1, "Saving ObjectDelete from " .. logicHandler.GetChatName(pid) ..
" about " .. self.description)
for objectIndex = 0, tes3mp.GetObjectListSize() - 1 do
local uniqueIndex = tes3mp.GetObjectRefNum(objectIndex) .. "-" .. tes3mp.GetObjectMpNum(objectIndex)
local refId = tes3mp.GetObjectRefId(objectIndex)
tes3mp.LogAppend(1, "- " .. uniqueIndex .. ", refId: " .. refId)
-- Check whether this object was moved to this cell from another one
local wasMovedHere = tableHelper.containsValue(self.data.packets.cellChangeFrom, uniqueIndex)
if wasMovedHere == true then
local originalCellDescription = self.data.objectData[uniqueIndex].cellChangeFrom
-- If the new cell is not loaded, load it temporarily
if LoadedCells[originalCellDescription] == nil then
logicHandler.LoadCell(originalCellDescription)
table.insert(temporaryLoadedCells, originalCellDescription)
end
local originalCell = LoadedCells[originalCellDescription]
originalCell:DeleteObjectData(uniqueIndex)
table.insert(originalCell.data.packets.delete, uniqueIndex)
originalCell:InitializeObjectData(uniqueIndex, refId)
self:DeleteObjectData(uniqueIndex)
else
-- Check whether this is a placed or spawned object
local wasPlacedHere = tableHelper.containsValue(self.data.packets.place, uniqueIndex) or
tableHelper.containsValue(self.data.packets.spawn, uniqueIndex)
self:DeleteObjectData(uniqueIndex)
if wasPlacedHere == false then
table.insert(self.data.packets.delete, uniqueIndex)
self:InitializeObjectData(uniqueIndex, refId)
end
end
end
-- Go through every temporary loaded cell and unload it
for arrayIndex, originalCellDescription in pairs(temporaryLoadedCells) do
logicHandler.UnloadCell(originalCellDescription)
end
end
function BaseCell:SaveObjectsPlaced(pid)
local containerUniqueIndexesRequested = {}
tes3mp.ReadReceivedObjectList()
tes3mp.LogMessage(1, "Saving ObjectPlace from " .. logicHandler.GetChatName(pid) ..
" about " .. self.description)
for objectIndex = 0, tes3mp.GetObjectListSize() - 1 do
local uniqueIndex = tes3mp.GetObjectRefNum(objectIndex) .. "-" .. tes3mp.GetObjectMpNum(objectIndex)
local location = {
posX = tes3mp.GetObjectPosX(objectIndex),
posY = tes3mp.GetObjectPosY(objectIndex),
posZ = tes3mp.GetObjectPosZ(objectIndex),
rotX = tes3mp.GetObjectRotX(objectIndex),
rotY = tes3mp.GetObjectRotY(objectIndex),
rotZ = tes3mp.GetObjectRotZ(objectIndex)
}
-- Ensure data integrity before proceeeding
if tableHelper.getCount(location) == 6 and tableHelper.usesNumericalValues(location) and
self:ContainsPosition(location.posX, location.posY) then
local refId = tes3mp.GetObjectRefId(objectIndex)
self:InitializeObjectData(uniqueIndex, refId)
local count = tes3mp.GetObjectCount(objectIndex)
local charge = tes3mp.GetObjectCharge(objectIndex)
local enchantmentCharge = tes3mp.GetObjectEnchantmentCharge(objectIndex)
local soul = tes3mp.GetObjectSoul(objectIndex)
local goldValue = tes3mp.GetObjectGoldValue(objectIndex)
-- Only save count if it isn't the default value of 1
if count ~= 1 then
self.data.objectData[uniqueIndex].count = count
end
-- Only save charge if it isn't the default value of -1
if charge ~= -1 then
self.data.objectData[uniqueIndex].charge = charge
end
-- Only save enchantment charge if it isn't the default value of -1
if enchantmentCharge ~= -1 then
self.data.objectData[uniqueIndex].enchantmentCharge = enchantmentCharge
end
if soul ~= "" then
self.data.objectData[uniqueIndex].soul = soul
end
-- Only save goldValue if it isn't the default value of 1
if goldValue ~=1 then
self.data.objectData[uniqueIndex].goldValue = goldValue
end
self.data.objectData[uniqueIndex].location = location
tes3mp.LogAppend(1, "- " .. uniqueIndex .. ", refId: " .. refId .. ", count: " .. count ..
", charge: " .. charge .. ", enchantmentCharge: " .. enchantmentCharge ..
", soul: " .. soul .. ", goldValue: " .. goldValue)
table.insert(self.data.packets.place, uniqueIndex)
-- Track objects which have containers so we can request their contents
if tes3mp.DoesObjectHaveContainer(objectIndex) then
table.insert(containerUniqueIndexesRequested, uniqueIndex)
end
end
end
if tableHelper.isEmpty(containerUniqueIndexesRequested) == false then
self:RequestContainers(pid, containerUniqueIndexesRequested)
end
end
function BaseCell:SaveObjectsSpawned(pid)
local containerUniqueIndexesRequested = {}
tes3mp.ReadReceivedObjectList()
tes3mp.LogMessage(1, "Saving ObjectSpawn from " .. logicHandler.GetChatName(pid) ..
" about " .. self.description)
for objectIndex = 0, tes3mp.GetObjectListSize() - 1 do
local uniqueIndex = tes3mp.GetObjectRefNum(objectIndex) .. "-" .. tes3mp.GetObjectMpNum(objectIndex)
local location = {
posX = tes3mp.GetObjectPosX(objectIndex),
posY = tes3mp.GetObjectPosY(objectIndex),
posZ = tes3mp.GetObjectPosZ(objectIndex),
rotX = tes3mp.GetObjectRotX(objectIndex),
rotY = tes3mp.GetObjectRotY(objectIndex),
rotZ = tes3mp.GetObjectRotZ(objectIndex)
}
-- Ensure data integrity before proceeeding
if tableHelper.getCount(location) == 6 and tableHelper.usesNumericalValues(location) and
self:ContainsPosition(location.posX, location.posY) then
local refId = tes3mp.GetObjectRefId(objectIndex)
self:InitializeObjectData(uniqueIndex, refId)
tes3mp.LogAppend(1, "- " .. uniqueIndex .. ", refId: " .. refId)
self.data.objectData[uniqueIndex].location = location
if tes3mp.GetObjectSummonState(objectIndex) then
local summonDuration = tes3mp.GetObjectSummonDuration(objectIndex)
if summonDuration > 0 then
local summon = {}
summon.duration = summonDuration
summon.startTime = os.time()
local isPlayer = tes3mp.DoesObjectHavePlayerSummoner(objectIndex)
if isPlayer then
local summonerPid = tes3mp.GetObjectSummonerPid(objectIndex)
tes3mp.LogAppend(1, "- summoned by pid " .. summonerPid)
-- Track the player and the summon for each other
summon.summonerPlayer = Players[summonerPid].accountName
Players[summonerPid].summons[uniqueIndex] = refId
else
local summonerUniqueIndex = tes3mp.GetObjectSummonerRefNum(objectIndex) ..
"-" .. tes3mp.GetObjectSummonerMpNum(objectIndex)
local summonerRefId = tes3mp.GetObjectSummonerRefId(objectIndex)
tes3mp.LogAppend(1, "- summoned by actor " .. summonerUniqueIndex ..
", refId: " .. summonerRefId)
end
self.data.objectData[uniqueIndex].summon = summon
end
end
table.insert(self.data.packets.spawn, uniqueIndex)
table.insert(self.data.packets.actorList, uniqueIndex)
table.insert(containerUniqueIndexesRequested, uniqueIndex)
end
end
if tableHelper.isEmpty(containerUniqueIndexesRequested) == false then
self:RequestContainers(pid, containerUniqueIndexesRequested)
end
end
function BaseCell:SaveObjectsLocked(pid)
tes3mp.ReadReceivedObjectList()
tes3mp.LogMessage(1, "Saving ObjectLock from " .. logicHandler.GetChatName(pid) ..
" about " .. self.description)
for objectIndex = 0, tes3mp.GetObjectListSize() - 1 do
local uniqueIndex = tes3mp.GetObjectRefNum(objectIndex) .. "-" .. tes3mp.GetObjectMpNum(objectIndex)
local refId = tes3mp.GetObjectRefId(objectIndex)
local lockLevel = tes3mp.GetObjectLockLevel(objectIndex)
self:InitializeObjectData(uniqueIndex, refId)
self.data.objectData[uniqueIndex].lockLevel = lockLevel
tes3mp.LogAppend(1, "- " .. uniqueIndex .. ", refId: " .. refId .. ", lockLevel: " .. lockLevel)
tableHelper.insertValueIfMissing(self.data.packets.lock, uniqueIndex)
end
end
function BaseCell:SaveObjectTrapsTriggered(pid)
tes3mp.ReadReceivedObjectList()
tes3mp.LogMessage(1, "Saving ObjectTrap from " .. logicHandler.GetChatName(pid) ..
" about " .. self.description)
for objectIndex = 0, tes3mp.GetObjectListSize() - 1 do
local uniqueIndex = tes3mp.GetObjectRefNum(objectIndex) .. "-" .. tes3mp.GetObjectMpNum(objectIndex)
local refId = tes3mp.GetObjectRefId(objectIndex)
self:InitializeObjectData(uniqueIndex, refId)
tes3mp.LogAppend(1, "- " .. uniqueIndex .. ", refId: " .. refId)
tableHelper.insertValueIfMissing(self.data.packets.trap, uniqueIndex)
end
end
function BaseCell:SaveObjectsScaled(pid)
tes3mp.ReadReceivedObjectList()
tes3mp.LogMessage(1, "Saving ObjectScale from " .. logicHandler.GetChatName(pid) ..
" about " .. self.description)
for objectIndex = 0, tes3mp.GetObjectListSize() - 1 do
local uniqueIndex = tes3mp.GetObjectRefNum(objectIndex) .. "-" .. tes3mp.GetObjectMpNum(objectIndex)
local refId = tes3mp.GetObjectRefId(objectIndex)
local scale = tes3mp.GetObjectScale(objectIndex)
self:InitializeObjectData(uniqueIndex, refId)
self.data.objectData[uniqueIndex].scale = scale
tes3mp.LogAppend(1, "- " .. uniqueIndex .. ", refId: " .. refId .. ", scale: " .. scale)
tableHelper.insertValueIfMissing(self.data.packets.scale, uniqueIndex)
end
end
function BaseCell:SaveObjectStates(pid)
if self.data.packets.state == nil then
self.data.packets.state = {}
end
tes3mp.ReadReceivedObjectList()
tes3mp.LogMessage(1, "Saving ObjectState from " .. logicHandler.GetChatName(pid) ..
" about " .. self.description)
for objectIndex = 0, tes3mp.GetObjectListSize() - 1 do
local refNum = tes3mp.GetObjectRefNum(objectIndex)
local mpNum = tes3mp.GetObjectMpNum(objectIndex)
local uniqueIndex = refNum .. "-" .. mpNum
local refId = tes3mp.GetObjectRefId(objectIndex)
local state = tes3mp.GetObjectState(objectIndex)
self:InitializeObjectData(uniqueIndex, refId)
self.data.objectData[uniqueIndex].state = state
tes3mp.LogAppend(1, "- " .. uniqueIndex .. ", refId: " .. refId .. ", state: " .. tostring(state))
tableHelper.insertValueIfMissing(self.data.packets.state, uniqueIndex)
if state == false then
if Players[pid].stateSpam == nil then
Players[pid].stateSpam = {}
end
if Players[pid].stateSpam[refId] == nil then
Players[pid].stateSpam[refId] = 0
else
Players[pid].stateSpam[refId] = Players[pid].stateSpam[refId] + 1
-- If the player gets 5 false object states for the same refid in that cell, delete it
if Players[pid].stateSpam[refId] >= 5 then
logicHandler.DeleteObjectForPlayer(pid, refId, refNum, mpNum)
tes3mp.LogMessage(1, "- " .. uniqueIndex .. " with refId: " .. refId ..
" was causing spam and has been deleted")
end
end
end
end
end
function BaseCell:SaveDoorStates(pid)
tes3mp.ReadReceivedObjectList()
for objectIndex = 0, tes3mp.GetObjectListSize() - 1 do
local uniqueIndex = tes3mp.GetObjectRefNum(objectIndex) .. "-" .. tes3mp.GetObjectMpNum(objectIndex)
local refId = tes3mp.GetObjectRefId(objectIndex)
local doorState = tes3mp.GetObjectDoorState(objectIndex)
self:InitializeObjectData(uniqueIndex, refId)
self.data.objectData[uniqueIndex].doorState = doorState
tableHelper.insertValueIfMissing(self.data.packets.doorState, uniqueIndex)
end
end
function BaseCell:SaveContainers(pid)
tes3mp.ReadReceivedObjectList()
tes3mp.CopyReceivedObjectListToStore()
tes3mp.LogMessage(1, "Saving Container from " .. logicHandler.GetChatName(pid) ..
" about " .. self.description)
local action = tes3mp.GetObjectListAction()
local subAction = tes3mp.GetObjectListContainerSubAction()
for objectIndex = 0, tes3mp.GetObjectListSize() - 1 do
local uniqueIndex = tes3mp.GetObjectRefNum(objectIndex) .. "-" .. tes3mp.GetObjectMpNum(objectIndex)
local refId = tes3mp.GetObjectRefId(objectIndex)
tes3mp.LogAppend(1, "- " .. uniqueIndex .. ", refId: " .. refId)
self:InitializeObjectData(uniqueIndex, refId)
tableHelper.insertValueIfMissing(self.data.packets.container, uniqueIndex)
local inventory = self.data.objectData[uniqueIndex].inventory
-- If this object's inventory is nil, or if the action is SET,
-- change the inventory to an empty table
if inventory == nil or action == enumerations.container.SET then
inventory = {}
end
for itemIndex = 0, tes3mp.GetContainerChangesSize(objectIndex) - 1 do
local itemRefId = tes3mp.GetContainerItemRefId(objectIndex, itemIndex)
local itemCount = tes3mp.GetContainerItemCount(objectIndex, itemIndex)
local itemCharge = tes3mp.GetContainerItemCharge(objectIndex, itemIndex)
local itemEnchantmentCharge = tes3mp.GetContainerItemEnchantmentCharge(objectIndex, itemIndex)
local itemSoul = tes3mp.GetContainerItemSoul(objectIndex, itemIndex)
-- Check if the object's stored inventory contains this item already
if inventoryHelper.containsItem(inventory, itemRefId, itemCharge, itemEnchantmentCharge, itemSoul) then
local foundIndex = inventoryHelper.getItemIndex(inventory, itemRefId, itemCharge,
itemEnchantmentCharge, itemSoul)
local item = inventory[foundIndex]
if action == enumerations.container.ADD then
item.count = item.count + itemCount
elseif action == enumerations.container.REMOVE then
local actionCount = tes3mp.GetContainerItemActionCount(objectIndex, itemIndex)
local newCount = item.count - actionCount
-- The item will still exist in the container with a lower count
if newCount > 0 then
item.count = newCount
-- The item is to be completely removed
elseif newCount == 0 then
inventory[foundIndex] = nil
else
actionCount = item.count
tes3mp.LogAppend(2, "- Attempt to remove more than possible from item")
tes3mp.LogAppend(2, "- Removed just " .. actionCount .. " instead")
tes3mp.SetContainerItemActionCountByIndex(objectIndex, itemIndex, actionCount)
inventory[foundIndex] = nil
end
end
else
if action == enumerations.container.REMOVE then
tes3mp.LogAppend(2, "- Attempt to remove non-existent item")
tes3mp.SetContainerItemActionCountByIndex(objectIndex, itemIndex, 0)
else
inventoryHelper.addItem(inventory, itemRefId, itemCount,
itemCharge, itemEnchantmentCharge, itemSoul)
end
end
end
tableHelper.cleanNils(inventory)
self.data.objectData[uniqueIndex].inventory = inventory
end
-- Is this a player replying to our request for container contents?
-- If so, only send the reply to other visitors
-- i.e. sendToOtherPlayers is true and skipAttachedPlayer is true
if subAction == enumerations.containerSub.REPLY_TO_REQUEST then
tes3mp.SendContainer(true, true)
-- Otherwise, send the received packet to everyone, including the
-- player who sent it (because no clientside changes will be made
-- to the container they're in otherwise)
-- i.e. sendToOtherPlayers is true and skipAttachedPlayer is false
else
tes3mp.SendContainer(true, false)
end
self:Save()
if action == enumerations.container.SET then
self.isRequestingContainers = false
end
end
function BaseCell:SaveActorList(pid)
tes3mp.ReadReceivedActorList()
tes3mp.LogMessage(1, "Saving ActorList from " .. logicHandler.GetChatName(pid) ..
" about " .. self.description)
for actorIndex = 0, tes3mp.GetActorListSize() - 1 do
local uniqueIndex = tes3mp.GetActorRefNum(actorIndex) .. "-" .. tes3mp.GetActorMpNum(actorIndex)
local refId = tes3mp.GetActorRefId(actorIndex)
self:InitializeObjectData(uniqueIndex, refId)
tes3mp.LogAppend(1, "- " .. uniqueIndex .. ", refId: " .. refId)
tableHelper.insertValueIfMissing(self.data.packets.actorList, uniqueIndex)
end
self:Save()
self.isRequestingActorList = false
end
function BaseCell:SaveActorPositions()
tes3mp.ReadCellActorList(self.description)
local actorListSize = tes3mp.GetActorListSize()
if actorListSize == 0 then
return
end
for objectIndex = 0, actorListSize - 1 do
local uniqueIndex = tes3mp.GetActorRefNum(objectIndex) .. "-" .. tes3mp.GetActorMpNum(objectIndex)
if tes3mp.DoesActorHavePosition(objectIndex) == true and self:ContainsObject(uniqueIndex) then
self.data.objectData[uniqueIndex].location = {
posX = tes3mp.GetActorPosX(objectIndex),
posY = tes3mp.GetActorPosY(objectIndex),
posZ = tes3mp.GetActorPosZ(objectIndex),
rotX = tes3mp.GetActorRotX(objectIndex),
rotY = tes3mp.GetActorRotY(objectIndex),
rotZ = tes3mp.GetActorRotZ(objectIndex)
}
tableHelper.insertValueIfMissing(self.data.packets.position, uniqueIndex)
end
end
end
function BaseCell:SaveActorStatsDynamic()
tes3mp.ReadCellActorList(self.description)
local actorListSize = tes3mp.GetActorListSize()
if actorListSize == 0 then
return
end
for objectIndex = 0, actorListSize - 1 do
local uniqueIndex = tes3mp.GetActorRefNum(objectIndex) .. "-" .. tes3mp.GetActorMpNum(objectIndex)
if tes3mp.DoesActorHaveStatsDynamic(objectIndex) == true and self:ContainsObject(uniqueIndex) then
self.data.objectData[uniqueIndex].stats = {
healthBase = tes3mp.GetActorHealthBase(objectIndex),
healthCurrent = tes3mp.GetActorHealthCurrent(objectIndex),
healthModified = tes3mp.GetActorHealthModified(objectIndex),
magickaBase = tes3mp.GetActorMagickaBase(objectIndex),
magickaCurrent = tes3mp.GetActorMagickaCurrent(objectIndex),
magickaModified = tes3mp.GetActorMagickaModified(objectIndex),
fatigueBase = tes3mp.GetActorFatigueBase(objectIndex),
fatigueCurrent = tes3mp.GetActorFatigueCurrent(objectIndex),
fatigueModified = tes3mp.GetActorFatigueModified(objectIndex)
}
tableHelper.insertValueIfMissing(self.data.packets.statsDynamic, uniqueIndex)
end
end
end
function BaseCell:SaveActorEquipment(pid)
tes3mp.ReadReceivedActorList()
tes3mp.LogMessage(1, "Saving ActorEquipment from " .. logicHandler.GetChatName(pid) ..
" about " .. self.description)
local actorListSize = tes3mp.GetActorListSize()
if actorListSize == 0 then
return
end
for actorIndex = 0, actorListSize - 1 do
local uniqueIndex = tes3mp.GetActorRefNum(actorIndex) .. "-" .. tes3mp.GetActorMpNum(actorIndex)
tes3mp.LogAppend(1, "- " .. uniqueIndex)
if self:ContainsObject(uniqueIndex) then
self.data.objectData[uniqueIndex].equipment = {}
for itemIndex = 0, tes3mp.GetEquipmentSize() - 1 do
local itemRefId = tes3mp.GetActorEquipmentItemRefId(actorIndex, itemIndex)
if itemRefId ~= "" then
self.data.objectData[uniqueIndex].equipment[itemIndex] = {
refId = itemRefId,
count = tes3mp.GetActorEquipmentItemCount(actorIndex, itemIndex),
charge = tes3mp.GetActorEquipmentItemCharge(actorIndex, itemIndex),
enchantmentCharge = tes3mp.GetActorEquipmentItemEnchantmentCharge(actorIndex, itemIndex)
}
end
end
tableHelper.insertValueIfMissing(self.data.packets.equipment, uniqueIndex)
end
end
self:Save()
end
function BaseCell:SaveActorDeath(pid)
if self.data.packets.death == nil then
self.data.packets.death = {}
end
local containerUniqueIndexesRequested = {}
tes3mp.ReadReceivedActorList()
tes3mp.LogMessage(1, "Saving ActorDeath from " .. logicHandler.GetChatName(pid) ..
" about " .. self.description)
local actorListSize = tes3mp.GetActorListSize()
if actorListSize == 0 then
return
end
for actorIndex = 0, actorListSize - 1 do
local uniqueIndex = tes3mp.GetActorRefNum(actorIndex) .. "-" .. tes3mp.GetActorMpNum(actorIndex)
if self:ContainsObject(uniqueIndex) then
local deathReason = "committed suicide"
if tes3mp.DoesActorHavePlayerKiller(actorIndex) then
local killerPid = tes3mp.GetActorKillerPid(actorIndex)
deathReason = "was killed by player " .. logicHandler.GetChatName(killerPid)
self.data.objectData[uniqueIndex].killer = {
player = Players[killerPid].accountName
}
else
local killerName = tes3mp.GetActorKillerName(actorIndex)
local killerUniqueIndex = tes3mp.GetActorKillerRefNum(actorIndex) ..
"-" .. tes3mp.GetActorKillerMpNum(actorIndex)
if killerName ~= "" and uniqueIndex ~= killerUniqueIndex then
deathReason = "was killed by " .. killerName
self.data.objectData[uniqueIndex].killer = {
refId = tes3mp.GetActorKillerRefId(actorIndex),
uniqueIndex = killerUniqueIndex
}
end
end
tes3mp.LogAppend(1, "- " .. uniqueIndex .. ", deathReason: " .. deathReason)
tableHelper.insertValueIfMissing(self.data.packets.death, uniqueIndex)
-- Prevent this actor's container from being used until we update
-- its contents based on a request to a player
--
-- This is an unfortunate workaround that needs to be used until
-- some changes are made on the C++ side
table.insert(self.unusableContainerUniqueIndexes, uniqueIndex)
table.insert(containerUniqueIndexesRequested, uniqueIndex)
end
end
if tableHelper.isEmpty(containerUniqueIndexesRequested) == false then
self:RequestContainers(pid, containerUniqueIndexesRequested)
end
self:Save()
end
function BaseCell:SaveActorCellChanges(pid)
local temporaryLoadedCells = {}
tes3mp.ReadReceivedActorList()
tes3mp.LogMessage(1, "Saving ActorCellChange from " .. logicHandler.GetChatName(pid) ..
" about " .. self.description)
for actorIndex = 0, tes3mp.GetActorListSize() - 1 do
local uniqueIndex = tes3mp.GetActorRefNum(actorIndex) .. "-" .. tes3mp.GetActorMpNum(actorIndex)
local newCellDescription = tes3mp.GetActorCell(actorIndex)
tes3mp.LogAppend(1, "- " .. uniqueIndex .. " moved to " .. newCellDescription)
-- If the new cell is not loaded, load it temporarily
if LoadedCells[newCellDescription] == nil then
logicHandler.LoadCell(newCellDescription)
table.insert(temporaryLoadedCells, newCellDescription)
end
local newCell = LoadedCells[newCellDescription]
-- Only proceed if this Actor is actually supposed to exist in this cell
if self.data.objectData[uniqueIndex] ~= nil then
-- Was this actor spawned in the old cell, instead of being a pre-existing actor?
-- If so, delete it entirely from the old cell and make it get spawned in the new cell
if tableHelper.containsValue(self.data.packets.spawn, uniqueIndex) == true then
tes3mp.LogAppend(1, "-- As a server-only object, it was moved entirely")
self:MoveObjectData(uniqueIndex, newCell)
-- Was this actor moved to the old cell from another cell?
elseif tableHelper.containsValue(self.data.packets.cellChangeFrom, uniqueIndex) == true then
local originalCellDescription = self.data.objectData[uniqueIndex].cellChangeFrom
-- Is the new cell actually this actor's original cell?
-- If so, move its data back and remove all of its cell change data
if originalCellDescription == newCellDescription then
tes3mp.LogAppend(1, "-- It is now back in its original cell " .. originalCellDescription)
self:MoveObjectData(uniqueIndex, newCell)
tableHelper.removeValue(newCell.data.packets.cellChangeTo, uniqueIndex)
tableHelper.removeValue(newCell.data.packets.cellChangeFrom, uniqueIndex)
newCell.data.objectData[uniqueIndex].cellChangeTo = nil
newCell.data.objectData[uniqueIndex].cellChangeFrom = nil
-- Otherwise, move its data to the new cell, delete it from the old cell, and update its
-- information in its original cell
else
self:MoveObjectData(uniqueIndex, newCell)
-- If the original cell is not loaded, load it temporarily
if LoadedCells[originalCellDescription] == nil then
logicHandler.LoadCell(originalCellDescription)
table.insert(temporaryLoadedCells, originalCellDescription)
end
local originalCell = LoadedCells[originalCellDescription]
if originalCell.data.objectData[uniqueIndex] ~= nil then
tes3mp.LogAppend(1, "-- This is now referenced in its original cell " .. originalCellDescription)
originalCell.data.objectData[uniqueIndex].cellChangeTo = newCellDescription
else
tes3mp.LogAppend(3, "-- It does not exist in its original cell " .. originalCellDescription ..
"! Please report this to a developer")
end
end
-- Otherwise, simply move this actor's data to the new cell and mark it as being moved there
-- in its old cell, as long as it's not supposed to already be in the new cell
elseif self.data.objectData[uniqueIndex].cellChangeTo ~= newCellDescription then
tes3mp.LogAppend(1, "-- This was its first move away from its original cell")
self:MoveObjectData(uniqueIndex, newCell)
table.insert(self.data.packets.cellChangeTo, uniqueIndex)
if self.data.objectData[uniqueIndex] == nil then
self.data.objectData[uniqueIndex] = {}
end
self.data.objectData[uniqueIndex].cellChangeTo = newCellDescription
table.insert(newCell.data.packets.cellChangeFrom, uniqueIndex)
newCell.data.objectData[uniqueIndex].cellChangeFrom = self.description
end
if newCell.data.objectData[uniqueIndex] ~= nil then
newCell.data.objectData[uniqueIndex].location = {
posX = tes3mp.GetActorPosX(actorIndex),
posY = tes3mp.GetActorPosY(actorIndex),
posZ = tes3mp.GetActorPosZ(actorIndex),
rotX = tes3mp.GetActorRotX(actorIndex),
rotY = tes3mp.GetActorRotY(actorIndex),
rotZ = tes3mp.GetActorRotZ(actorIndex)
}
end
else
tes3mp.LogAppend(3, "-- Invalid or repeated cell change was attempted! Please report this to a developer")
end
end
-- Go through every temporary loaded cell and unload it
for arrayIndex, newCellDescription in pairs(temporaryLoadedCells) do
logicHandler.UnloadCell(newCellDescription)
end
self:Save()
end
function BaseCell:LoadObjectsDeleted(pid)
local objectCount = 0
tes3mp.ClearObjectList()
tes3mp.SetObjectListPid(pid)
tes3mp.SetObjectListCell(self.description)
for arrayIndex, uniqueIndex in pairs(self.data.packets.delete) do
packetBuilder.AddObjectDelete(uniqueIndex, self.data.objectData[uniqueIndex])
objectCount = objectCount + 1
end
if objectCount > 0 then
tes3mp.SendObjectDelete()
end
end
function BaseCell:LoadObjectsPlaced(pid)
local objectCount = 0
tes3mp.ClearObjectList()
tes3mp.SetObjectListPid(pid)
tes3mp.SetObjectListCell(self.description)
for arrayIndex, uniqueIndex in pairs(self.data.packets.place) do
local location = self.data.objectData[uniqueIndex].location
-- Ensure data integrity before proceeeding
if tableHelper.getCount(location) == 6 and tableHelper.usesNumericalValues(location) and
self:ContainsPosition(location.posX, location.posY) then
packetBuilder.AddObjectPlace(uniqueIndex, self.data.objectData[uniqueIndex])
objectCount = objectCount + 1
else
self.data.objectData[uniqueIndex] = nil
tableHelper.removeValue(self.data.packets.place, uniqueIndex)
end
end
if objectCount > 0 then
tes3mp.SendObjectPlace()
end
end
function BaseCell:LoadObjectsSpawned(pid)
-- Keep this around for backwards compatibility
if self.data.packets.spawn == nil then
self.data.packets.spawn = {}
end
local objectCount = 0
tes3mp.ClearObjectList()
tes3mp.SetObjectListPid(pid)
tes3mp.SetObjectListCell(self.description)
for arrayIndex, uniqueIndex in pairs(self.data.packets.spawn) do
local location = self.data.objectData[uniqueIndex].location
-- Ensure data integrity before proceeeding
if tableHelper.getCount(location) == 6 and tableHelper.usesNumericalValues(location) and
self:ContainsPosition(location.posX, location.posY) then
local shouldSkip = false
local summon = self.data.objectData[uniqueIndex].summon
if summon ~= nil then
local currentTime = os.time()
local finishTime = summon.startTime + summon.duration
if currentTime >= finishTime then
self:DeleteObjectData(uniqueIndex)
shouldSkip = true
else
local remainingTime = finishTime - currentTime
tes3mp.SetObjectSummonDuration(remainingTime)
end
end
if shouldSkip == false then
packetBuilder.AddObjectSpawn(uniqueIndex, self.data.objectData[uniqueIndex])
objectCount = objectCount + 1
end
else
self.data.objectData[uniqueIndex] = nil
tableHelper.removeValue(self.data.packets.spawn, uniqueIndex)
end
end
if objectCount > 0 then
tes3mp.SendObjectSpawn()
end
end
function BaseCell:LoadObjectsLocked(pid)
local objectCount = 0
tes3mp.ClearObjectList()
tes3mp.SetObjectListPid(pid)
tes3mp.SetObjectListCell(self.description)
for arrayIndex, uniqueIndex in pairs(self.data.packets.lock) do
local refId = self.data.objectData[uniqueIndex].refId
local lockLevel = self.data.objectData[uniqueIndex].lockLevel
if refId ~= nil and lockLevel ~= nil then
packetBuilder.AddObjectLock(uniqueIndex, self.data.objectData[uniqueIndex])
objectCount = objectCount + 1
else
tableHelper.removeValue(self.data.packets.lock, uniqueIndex)
end
end
if objectCount > 0 then
tes3mp.SendObjectLock()
end
end
function BaseCell:LoadObjectTrapsTriggered(pid)
local objectCount = 0
tes3mp.ClearObjectList()
tes3mp.SetObjectListPid(pid)
tes3mp.SetObjectListCell(self.description)
for arrayIndex, uniqueIndex in pairs(self.data.packets.trap) do
packetBuilder.AddObjectTrap(uniqueIndex, self.data.objectData[uniqueIndex])
objectCount = objectCount + 1
end
if objectCount > 0 then
tes3mp.SendObjectTrap()
end
end
function BaseCell:LoadObjectsScaled(pid)
local objectCount = 0
tes3mp.ClearObjectList()
tes3mp.SetObjectListPid(pid)
tes3mp.SetObjectListCell(self.description)
for arrayIndex, uniqueIndex in pairs(self.data.packets.scale) do
local refId = self.data.objectData[uniqueIndex].refId
local scale = self.data.objectData[uniqueIndex].scale
if refId ~= nil and scale ~= nil then
packetBuilder.AddObjectScale(uniqueIndex, self.data.objectData[uniqueIndex])
objectCount = objectCount + 1
end
end
if objectCount > 0 then
tes3mp.SendObjectScale()
end
end
function BaseCell:LoadObjectStates(pid)
if self.data.packets.state == nil then
self.data.packets.state = {}
end
local objectCount = 0
tes3mp.ClearObjectList()
tes3mp.SetObjectListPid(pid)
tes3mp.SetObjectListCell(self.description)
for arrayIndex, uniqueIndex in pairs(self.data.packets.state) do
local refId = self.data.objectData[uniqueIndex].refId
local state = self.data.objectData[uniqueIndex].state
if refId ~= nil and state ~= nil then
packetBuilder.AddObjectState(uniqueIndex, self.data.objectData[uniqueIndex])
objectCount = objectCount + 1
end
end
if objectCount > 0 then
tes3mp.SendObjectState()
end
end
function BaseCell:LoadDoorStates(pid)
local objectCount = 0
tes3mp.ClearObjectList()
tes3mp.SetObjectListPid(pid)
tes3mp.SetObjectListCell(self.description)
for arrayIndex, uniqueIndex in pairs(self.data.packets.doorState) do
packetBuilder.AddDoorState(uniqueIndex, self.data.objectData[uniqueIndex])
objectCount = objectCount + 1
end
if objectCount > 0 then
tes3mp.SendDoorState()
end
end
function BaseCell:LoadContainers(pid)
local objectCount = 0
tes3mp.ClearObjectList()
tes3mp.SetObjectListPid(pid)
tes3mp.SetObjectListCell(self.description)
for arrayIndex, uniqueIndex in pairs(self.data.packets.container) do
local splitIndex = uniqueIndex:split("-")
tes3mp.SetObjectRefNum(splitIndex[1])
tes3mp.SetObjectMpNum(splitIndex[2])
if self:ContainsObject(uniqueIndex) and self.data.objectData[uniqueIndex].inventory ~= nil then
tes3mp.SetObjectRefId(self.data.objectData[uniqueIndex].refId)
for itemIndex, item in pairs(self.data.objectData[uniqueIndex].inventory) do
if item.enchantmentCharge == nil then
item.enchantmentCharge = -1
end
if item.soul == nil then
item.soul = ""
end
tes3mp.SetContainerItemRefId(item.refId)
tes3mp.SetContainerItemCount(item.count)
tes3mp.SetContainerItemCharge(item.charge)
tes3mp.SetContainerItemEnchantmentCharge(item.enchantmentCharge)
tes3mp.SetContainerItemSoul(item.soul)
tes3mp.AddContainerItem()
end
tes3mp.AddObject()
objectCount = objectCount + 1
else
tes3mp.LogAppend(3, "- Had container packet recorded for " .. uniqueIndex ..
", but no matching object data! Please report this to a developer")
tableHelper.removeValue(self.data.packets.container, uniqueIndex)
end
end
if objectCount > 0 then
-- Set the action to SET
tes3mp.SetObjectListAction(0)
tes3mp.SendContainer()
end
end
function BaseCell:LoadActorList(pid)
local actorCount = 0
tes3mp.ClearActorList()
tes3mp.SetActorListPid(pid)
tes3mp.SetActorListCell(self.description)
for arrayIndex, uniqueIndex in pairs(self.data.packets.actorList) do
local splitIndex = uniqueIndex:split("-")
tes3mp.SetActorRefNum(splitIndex[1])
tes3mp.SetActorMpNum(splitIndex[2])
if self:ContainsObject(uniqueIndex) then
tes3mp.SetActorRefId(self.data.objectData[uniqueIndex].refId)
actorCount = actorCount + 1
else
tes3mp.LogAppend(3, "- Had actorList packet recorded for " .. uniqueIndex ..
", but no matching object data! Please report this to a developer")
tableHelper.removeValue(self.data.packets.actorList, uniqueIndex)
end
end
if actorCount > 0 then
-- Set the action to SET
tes3mp.SetActorListAction(0)
tes3mp.SendActorList()
end
end
function BaseCell:LoadActorAuthority(pid)
tes3mp.ClearActorList()
tes3mp.SetActorListPid(pid)
tes3mp.SetActorListCell(self.description)
tes3mp.SendActorAuthority()
end
function BaseCell:LoadActorPositions(pid)
local actorCount = 0
tes3mp.ClearActorList()
tes3mp.SetActorListPid(pid)
tes3mp.SetActorListCell(self.description)
for arrayIndex, uniqueIndex in pairs(self.data.packets.position) do
local splitIndex = uniqueIndex:split("-")
tes3mp.SetActorRefNum(splitIndex[1])
tes3mp.SetActorMpNum(splitIndex[2])
if self:ContainsObject(uniqueIndex) then
local location = self.data.objectData[uniqueIndex].location
-- Ensure data integrity before proceeeding
if tableHelper.getCount(location) == 6 and tableHelper.usesNumericalValues(location) and
self:ContainsPosition(location.posX, location.posY) then
tes3mp.SetActorPosition(location.posX, location.posY, location.posZ)
tes3mp.SetActorRotation(location.rotX, location.rotY, location.rotZ)
tes3mp.AddActor()
actorCount = actorCount + 1
end
else
tes3mp.LogAppend(3, "- Had position packet recorded for " .. uniqueIndex ..
", but no matching object data! Please report this to a developer")
tableHelper.removeValue(self.data.packets.position, uniqueIndex)
end
end
if actorCount > 0 then
tes3mp.SendActorPosition()
end
end
function BaseCell:LoadActorStatsDynamic(pid)
local actorCount = 0
tes3mp.ClearActorList()
tes3mp.SetActorListPid(pid)
tes3mp.SetActorListCell(self.description)
for arrayIndex, uniqueIndex in pairs(self.data.packets.statsDynamic) do
local splitIndex = uniqueIndex:split("-")
tes3mp.SetActorRefNum(splitIndex[1])
tes3mp.SetActorMpNum(splitIndex[2])
if self:ContainsObject(uniqueIndex) and self.data.objectData[uniqueIndex].stats ~= nil then
local stats = self.data.objectData[uniqueIndex].stats
tes3mp.SetActorHealthBase(stats.healthBase)
tes3mp.SetActorHealthCurrent(stats.healthCurrent)
tes3mp.SetActorHealthModified(stats.healthModified)
tes3mp.SetActorMagickaBase(stats.magickaBase)
tes3mp.SetActorMagickaCurrent(stats.magickaCurrent)
tes3mp.SetActorMagickaModified(stats.magickaModified)
tes3mp.SetActorFatigueBase(stats.fatigueBase)
tes3mp.SetActorFatigueCurrent(stats.fatigueCurrent)
tes3mp.SetActorFatigueModified(stats.fatigueModified)
tes3mp.AddActor()
actorCount = actorCount + 1
else
tes3mp.LogAppend(3, "- Had statsDynamic packet recorded for " .. uniqueIndex ..
", but no matching object data! Please report this to a developer")
tableHelper.removeValue(self.data.packets.statsDynamic, uniqueIndex)
end
end
if actorCount > 0 then
tes3mp.SendActorStatsDynamic()
end
end
function BaseCell:LoadActorEquipment(pid)
local actorCount = 0
tes3mp.ClearActorList()
tes3mp.SetActorListPid(pid)
tes3mp.SetActorListCell(self.description)
for arrayIndex, uniqueIndex in pairs(self.data.packets.equipment) do
local splitIndex = uniqueIndex:split("-")
tes3mp.SetActorRefNum(splitIndex[1])
tes3mp.SetActorMpNum(splitIndex[2])
if self:ContainsObject(uniqueIndex) and self.data.objectData[uniqueIndex].equipment ~= nil then
local equipment = self.data.objectData[uniqueIndex].equipment
for itemIndex = 0, tes3mp.GetEquipmentSize() - 1 do
local currentItem = equipment[itemIndex]
if currentItem ~= nil then
if currentItem.enchantmentCharge == nil then
currentItem.enchantmentCharge = -1
end
tes3mp.EquipActorItem(itemIndex, currentItem.refId, currentItem.count,
currentItem.charge, currentItem.enchantmentCharge)
else
tes3mp.UnequipActorItem(itemIndex)
end
end
tes3mp.AddActor()
actorCount = actorCount + 1
else
tes3mp.LogAppend(3, "- Had equipment packet recorded for " .. uniqueIndex ..
", but no matching object data! Please report this to a developer")
tableHelper.removeValue(self.data.packets.equipment, uniqueIndex)
end
end
if actorCount > 0 then
tes3mp.SendActorEquipment()
end
end
function BaseCell:LoadActorAI(pid)
if self.data.packets.ai == nil then
self.data.packets.ai = {}
end
local actorCount = 0
-- These packets only need to be sent to the new visitor, unless the
-- new visitor is the target of some of them, in which case those
-- need to be tracked and sent separately to all the cell's visitors
local sharedPacketUniqueIndexes = {}
tes3mp.ClearActorList()
tes3mp.SetActorListPid(pid)
tes3mp.SetActorListCell(self.description)
for arrayIndex, uniqueIndex in pairs(self.data.packets.ai) do
local splitIndex = uniqueIndex:split("-")
tes3mp.SetActorRefNum(splitIndex[1])
tes3mp.SetActorMpNum(splitIndex[2])
if self:ContainsObject(uniqueIndex) and self.data.objectData[uniqueIndex].ai ~= nil then
local ai = self.data.objectData[uniqueIndex].ai
local targetPid
if ai.targetPlayer ~= nil then
if logicHandler.IsPlayerNameLoggedIn(ai.targetPlayer) then
targetPid = logicHandler.GetPlayerByName(ai.targetPlayer).pid
end
end
local isValid = true
-- Don't allow untargeted packets that require targets
if targetPid == nil and ai.targetUniqueIndex == nil then
if ai.action == enumerations.ai.ACTIVATE or ai.action == enumerations.ai.COMBAT or
ai.action == enumerations.ai.ESCORT or ai.action == enumerations.ai.FOLLOW then
isValid = false
tes3mp.LogAppend(2, "- Could not find valid AI target for actor " .. uniqueIndex)
end
end
if isValid then
-- Is this new visitor the target of one of the actors? If so, we'll
-- send a separate packet to every cell visitor with just that at
-- the end
if pid == targetPid then
table.insert(sharedPacketUniqueIndexes, uniqueIndex)
else
packetBuilder.AddAIActor(uniqueIndex, targetPid, ai)
actorCount = actorCount + 1
end
end
else
tes3mp.LogAppend(3, "- Had AI packet recorded for " .. uniqueIndex ..
", but no matching object data! Please report this to a developer")
tableHelper.removeValue(self.data.packets.equipment, uniqueIndex)
end
end
-- Send the packets meant for just this new visitor
if actorCount > 0 then
tes3mp.SendActorAI(false)
end
-- Send the packets targeting this visitor that all the visitors
-- need to have
if tableHelper.getCount(sharedPacketUniqueIndexes) > 0 then
tes3mp.ClearActorList()
tes3mp.SetActorListPid(pid)
tes3mp.SetActorListCell(self.description)
for arrayIndex, uniqueIndex in pairs(sharedPacketUniqueIndexes) do
local splitIndex = uniqueIndex:split("-")
tes3mp.SetActorRefNum(splitIndex[1])
tes3mp.SetActorMpNum(splitIndex[2])
local ai = self.data.objectData[uniqueIndex].ai
packetBuilder.AddAIActor(uniqueIndex, pid, ai)
end
tes3mp.SendActorAI(true)
end
end
function BaseCell:LoadActorCellChanges(pid)
local temporaryLoadedCells = {}
local actorCount = 0
-- Move actors originally from this cell to other cells
tes3mp.ClearActorList()
tes3mp.SetActorListPid(pid)
tes3mp.SetActorListCell(self.description)
for arrayIndex, uniqueIndex in pairs(self.data.packets.cellChangeTo) do
local splitIndex = uniqueIndex:split("-")
tes3mp.SetActorRefNum(splitIndex[1])
tes3mp.SetActorMpNum(splitIndex[2])
local newCellDescription = self.data.objectData[uniqueIndex].cellChangeTo
if newCellDescription ~= nil then
tes3mp.SetActorCell(newCellDescription)
-- If the new cell is not loaded, load it temporarily
if LoadedCells[newCellDescription] == nil then
logicHandler.LoadCell(newCellDescription)
table.insert(temporaryLoadedCells, newCellDescription)
end
if LoadedCells[newCellDescription].data.objectData[uniqueIndex] ~= nil then
local location = LoadedCells[newCellDescription].data.objectData[uniqueIndex].location
-- Ensure data integrity before proceeeding
if tableHelper.getCount(location) == 6 and tableHelper.usesNumericalValues(location) and
self:ContainsPosition(location.posX, location.posY) then
tes3mp.SetActorPosition(location.posX, location.posY, location.posZ)
tes3mp.SetActorRotation(location.rotX, location.rotY, location.rotZ)
tes3mp.AddActor()
actorCount = actorCount + 1
end
else
tes3mp.LogAppend(3, "- Tried to move " .. uniqueIndex .. " from " .. self.description ..
" to " .. newCellDescription .. " with no position data!")
self.data.objectData[uniqueIndex] = nil
tableHelper.removeValue(self.data.packets.cellChangeTo, uniqueIndex)
end
else
tes3mp.LogAppend(3, "- Had cellChangeTo packet recorded for " .. uniqueIndex ..
", but no matching cell description! Please report this to a developer")
tableHelper.removeValue(self.data.packets.cellChangeTo, uniqueIndex)
end
end
if actorCount > 0 then
tes3mp.SendActorCellChange()
end
-- Go through every temporary loaded cell and unload it
for arrayIndex, newCellDescription in pairs(temporaryLoadedCells) do
logicHandler.UnloadCell(newCellDescription)
end
-- Make a table of every cell that has sent actors to this cell
local cellChangesFrom = {}
for arrayIndex, uniqueIndex in pairs(self.data.packets.cellChangeFrom) do
if self.data.objectData[uniqueIndex] ~= nil and self.data.objectData[uniqueIndex].cellChangeFrom ~= nil then
local originalCellDescription = self.data.objectData[uniqueIndex].cellChangeFrom
if cellChangesFrom[originalCellDescription] == nil then
cellChangesFrom[originalCellDescription] = {}
end
table.insert(cellChangesFrom[originalCellDescription], uniqueIndex)
else
tes3mp.LogAppend(3, "- Had cellChangeFrom packet recorded for " .. uniqueIndex ..
", but no matching cell description! Please report this to a developer")
tableHelper.removeValue(self.data.packets.cellChangeFrom, uniqueIndex)
end
end
local actorCount = 0
-- Send a cell change packet for every cell that has sent actors to this cell
for originalCellDescription, actorArray in pairs(cellChangesFrom) do
tes3mp.ClearActorList()
tes3mp.SetActorListPid(pid)
tes3mp.SetActorListCell(originalCellDescription)
for arrayIndex, uniqueIndex in pairs(actorArray) do
local splitIndex = uniqueIndex:split("-")
tes3mp.SetActorRefNum(splitIndex[1])
tes3mp.SetActorMpNum(splitIndex[2])
tes3mp.SetActorCell(self.description)
local location = self.data.objectData[uniqueIndex].location
-- Ensure data integrity before proceeeding
if tableHelper.getCount(location) == 6 and tableHelper.usesNumericalValues(location) and
self:ContainsPosition(location.posX, location.posY) then
tes3mp.SetActorPosition(location.posX, location.posY, location.posZ)
tes3mp.SetActorRotation(location.rotX, location.rotY, location.rotZ)
tes3mp.AddActor()
actorCount = actorCount + 1
end
end
if actorCount > 0 then
tes3mp.SendActorCellChange()
end
end
end
function BaseCell:RequestContainers(pid, requestUniqueIndexes)
self.isRequestingContainers = true
self.containerRequestPid = pid
tes3mp.ClearObjectList()
tes3mp.SetObjectListPid(pid)
tes3mp.SetObjectListCell(self.description)
-- Set the action to REQUEST
tes3mp.SetObjectListAction(3)
-- If certain uniqueIndexes are specified, iterate through them and
-- add them as world objects
--
-- Otherwise, the client will simply reply with the contents of all
-- the containers in this cell
if requestUniqueIndexes ~= nil and type(requestUniqueIndexes) == "table" then
for arrayIndex, uniqueIndex in pairs(requestUniqueIndexes) do
local splitIndex = uniqueIndex:split("-")
tes3mp.SetObjectRefNum(splitIndex[1])
tes3mp.SetObjectMpNum(splitIndex[2])
tes3mp.SetObjectRefId(self.data.objectData[uniqueIndex].refId)
tes3mp.AddObject()
end
end
tes3mp.SendContainer()
end
function BaseCell:RequestActorList(pid)
self.isRequestingActorList = true
self.actorListRequestPid = pid
tes3mp.ClearActorList()
tes3mp.SetActorListPid(pid)
tes3mp.SetActorListCell(self.description)
-- Set the action to REQUEST
tes3mp.SetActorListAction(3)
tes3mp.SendActorList()
end
function BaseCell:LoadInitialCellData(pid)
self:EnsurePacketValidity()
tes3mp.LogMessage(1, "Sending data of cell " .. self.description .. " to pid " .. pid)
self:LoadObjectsDeleted(pid)
self:LoadObjectsPlaced(pid)
self:LoadObjectsSpawned(pid)
self:LoadObjectsLocked(pid)
self:LoadObjectTrapsTriggered(pid)
self:LoadObjectsScaled(pid)
self:LoadObjectStates(pid)
self:LoadDoorStates(pid)
if self:HasContainerData() == true then
tes3mp.LogAppend(1, "- Had container data")
self:LoadContainers(pid)
elseif self.isRequestingContainers == false then
tes3mp.LogAppend(1, "- Requesting containers")
self:RequestContainers(pid)
end
if self:HasActorData() == true then
tes3mp.LogAppend(1, "- Had actor data")
self:LoadActorCellChanges(pid)
self:LoadActorEquipment(pid)
self:LoadActorAI(pid)
elseif self.isRequestingActorList == false then
tes3mp.LogAppend(1, "- Requesting actor list")
self:RequestActorList(pid)
end
end
function BaseCell:LoadMomentaryCellData(pid)
if self:HasActorData() == true then
self:LoadActorPositions(pid)
self:LoadActorStatsDynamic(pid)
end
end
return BaseCell