Skip to content

Commit

Permalink
Increase priority of drop event slightly to drop things before they g…
Browse files Browse the repository at this point in the history
…et replaced in other mods
  • Loading branch information
jhaakma committed Mar 26, 2023
1 parent 14f9f61 commit 91544eb
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 23 deletions.
9 changes: 9 additions & 0 deletions Data Files/MWSE/mods/mer/justDropIt/common.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
local common = {}
local config = require("mer.justDropIt.config")

common.logger = require("logging.logger").new{
name = "Just Drop It",
logLevel = config.mcmConfig.logLevel
}

return common
2 changes: 2 additions & 0 deletions Data Files/MWSE/mods/mer/justDropIt/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ When you drop an item, it will actually touch the ground! It also orients the it
}
config.mcmConfig = mwse.loadConfig(config.modName, mcmDefaultValues)

config.registeredItems = {}

return config
18 changes: 18 additions & 0 deletions Data Files/MWSE/mods/mer/justDropIt/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
local common = require("mer.justDropIt.common")
local config = require("mer.justDropIt.config")
local JustDropIt = {}

---@class JustDropIt.registeredItem
---@field id string The ID of the object
---@field maxSteepness number The maximum angle the object will be placed at

---@param e JustDropIt.registeredItem
function JustDropIt.registerItem(e)
common.logger:assert(type(e.id) == "string", "Item ID must be a string")
common.logger:assert(type(e.maxSteepness) == "number", "Item maxSteepness must be a number")

common.logger:debug("Registering item %s", e.id)
config.registeredItems[e.id:lower()] = e
end

return JustDropIt
42 changes: 22 additions & 20 deletions Data Files/MWSE/mods/mer/justDropIt/main.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
local common = require("mer.justDropIt.common")
local config = require('mer.justDropIt.config')
local orient = require("mer.justDropIt.orient")
local logger = require("logging.logger").new{
name = "Just Drop It",
logLevel = config.mcmConfig.logLevel
}

local modName = config.modName

local deathAnimations = {
Expand All @@ -23,19 +21,19 @@ local validObjectTypes = {
--Initialisation
local function onItemDrop(e)
if config.mcmConfig.enabled then
logger:debug("Orienting %s on itemDropped", e.reference)
orient.orientRefToGround{ ref = e.reference }
common.logger:debug("Orienting %s on itemDropped", e.reference)
orient.orientRefToGround{ ref = e.reference, offset = 0}
end
end
event.register("itemDropped", onItemDrop)
event.register("itemDropped", onItemDrop, { priority = 10 })

---@param e playGroupEventData
local function onNPCDying(e)
if config.mcmConfig.enabled and config.mcmConfig.orientOnDeath then
if deathAnimations[e.group] then
if not e.reference.data.justDropItOrientedOnDeath then
logger:debug("Orienting %s on death", e.reference)
local result = orient.getGroundBelowRef({ref = e.reference})
common.logger:debug("Orienting %s on death", e.reference)
local result = orient.getGroundBelowRef({ref = e.reference, offset = 0})
if result then
orient.orientRef(e.reference, result)
e.reference.data.justDropItOrientedOnDeath = true
Expand All @@ -50,7 +48,7 @@ event.register("playGroup", onNPCDying)
---@param e mobileActivatedEventData
local function onRefResurrected(e)
if validObjectTypes[e.reference.baseObject.objectType] then
logger:debug("Restoring vertical orientation of %s on referenceActivated", e.reference)
common.logger:debug("Restoring vertical orientation of %s on referenceActivated", e.reference)
orient.resetXYOrientation(e.reference)
e.reference.data.justDropItOrientedOnDeath = nil
end
Expand Down Expand Up @@ -93,11 +91,13 @@ local function getMaxWidth(reference)
return width
end



---@param reference tes3reference
local function dropNearbyObjects(reference, processedRefs)
processedRefs = processedRefs or {}
processedRefs[reference] = true
logger:debug("Dropping nearby objects for %s", reference)
common.logger:debug("Dropping nearby objects for %s", reference)
local nearbyRefs = {}
for _, cell in pairs( tes3.getActiveCells() ) do
for nearbyRef in cell:iterateReferences() do
Expand All @@ -121,22 +121,24 @@ local function dropNearbyObjects(reference, processedRefs)
return a.position.z < b.position.z
end)
for _, nearbyRef in pairs(nearbyRefs) do
logger:debug("Dropping %s near %s", nearbyRef, reference)
local result = orient.getGroundBelowRef({ref = nearbyRef})
common.logger:debug("Dropping %s near %s", nearbyRef.id, reference.id)
local result = orient.getGroundBelowRef({ref = nearbyRef, offset = 0})
if result and result.reference == reference then
local safeParent = tes3.makeSafeObjectHandle(reference)
local parentZ = reference.position.z
local safeRef = tes3.makeSafeObjectHandle(nearbyRef)
timer.delayOneFrame(function()timer.delayOneFrame(function()
if safeParent:valid() and math.isclose(parentZ, reference.position.z) then
logger:debug("Parent %s still exists and wasn't moved, don't bother dropping children", reference)
timer.delayOneFrame(function()timer.delayOneFrame(function()timer.delayOneFrame(function()timer.delayOneFrame(function()timer.delayOneFrame(function()timer.delayOneFrame(function()
if safeParent and safeParent:valid() and math.isclose(parentZ, reference.position.z) then
common.logger:debug("Parent %s still exists and wasn't moved, don't bother dropping children", reference.id)
return
end
if safeRef:valid() then
if safeRef and safeRef:valid() then
dropNearbyObjects(nearbyRef, processedRefs)
orient.orientRefToGround{ref = nearbyRef, ignoreBlackList = true}
orient.orientRefToGround{ref = nearbyRef, ignoreBlackList = true, offset = 0}
end
end)end)
end)end)end)end)end)end)
else
common.logger:debug("Raytest from %s didn't return original reference %s, hit %s instead", nearbyRef, reference.id, result and result.reference)
end
end
end
Expand Down Expand Up @@ -176,7 +178,7 @@ local function registerModConfig()
},
variable = mwse.mcm.createTableVariable{ id = "logLevel", table = config.mcmConfig },
callback = function(self)
logger:setLogLevel(self.variable.value)
common.logger:setLogLevel(self.variable.value)
end
}

Expand Down
14 changes: 11 additions & 3 deletions Data Files/MWSE/mods/mer/justDropIt/orient.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,15 @@ end
local function getMaxSteepness(ref)
local objType = ref.baseObject.objectType
if objType == tes3.objectType.npc or objType == tes3.objectType.creature then return 60 end
return isTall(ref) and config.mcmConfig.maxSteepnessTall or config.mcmConfig.maxSteepnessFlat

local registeredItem = config.registeredItems[ref.baseObject.id:lower()]
if registeredItem and registeredItem.maxSteepness then
return registeredItem.maxSteepness
else
return isTall(ref)
and config.mcmConfig.maxSteepnessTall
or config.mcmConfig.maxSteepnessFlat
end
end

local function doOrient(result)
Expand Down Expand Up @@ -91,7 +99,7 @@ end

function this.getGroundBelowRef(e)
local ref = e.ref
local offset = 0
local offset = e.offset or 0 --ref.object.boundingBox.max.z-ref.object.boundingBox.min.z
if not ref then
return
end
Expand Down Expand Up @@ -125,7 +133,7 @@ function this.orientRefToGround(params)
return
end

local result = this.getGroundBelowRef({ref = ref})
local result = this.getGroundBelowRef({ref = ref, offset = params.offset})
if result then
this.positionRef(ref, result)
if doOrient(result) then
Expand Down

0 comments on commit 91544eb

Please sign in to comment.
  翻译: