Refactor BodyLocations.lua for improved management
Refactored BodyLocations.lua to streamline body location management and improve functionality.
This commit is contained in:
@@ -1,110 +1,74 @@
|
|||||||
--Based on RabenRabo's bodylocation solution from their mod "Fantasy Bodyparts" and "Fantasy Legs" sub-mod.
|
require("TOC/Debug")
|
||||||
--Modified by GanydeBielovzki with permission for batch use for the Frockin' Splendor franchise and spin-offs.
|
require("NPCs/BodyLocations")
|
||||||
--To copy, further modify or otherwise use this code the original creator and the modifier must be credited.
|
local TOC = require("TOC/Registries")
|
||||||
|
local StaticData = require("TOC/StaticData")
|
||||||
|
|
||||||
local function copyBodyLocationProperties(oldGroup, oldLocID, newGroup)
|
local BodyLocationsAPI = {}
|
||||||
for k = 0, oldGroup:size()-1 do
|
local function customGetVal(obj, int) return getClassFieldVal(obj, getClassField(obj, int)) end
|
||||||
local otherLocID = oldGroup:getLocationByIndex(k):getId()
|
local group = BodyLocations.getGroup("Human")
|
||||||
if oldGroup:isExclusive(oldLocID, otherLocID)
|
|
||||||
then
|
---@type ArrayList
|
||||||
newGroup:setExclusive(oldLocID, otherLocID)
|
local list = customGetVal(group, 1)
|
||||||
end
|
|
||||||
if oldGroup:isHideModel(oldLocID, otherLocID)
|
---@param bodyLoc string
|
||||||
then
|
function BodyLocationsAPI.New(bodyLoc)
|
||||||
newGroup:setHideModel(oldLocID, otherLocID)
|
curItem = group:getOrCreateLocation(bodyLoc) -- get current item - or create
|
||||||
end
|
return curItem
|
||||||
if oldGroup:isAltModel(oldLocID, otherLocID)
|
|
||||||
then
|
|
||||||
newGroup:setAltModel(oldLocID, otherLocID)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function addBodyLocationsAt(groupName, locationList)
|
-- TODO Not sure if this method actually works as intende with b42, but for our use case it's fine...
|
||||||
local results = {}
|
---@param toRelocateOrCreate string
|
||||||
|
---@param locationElement string
|
||||||
-- get list (!!actually a view!!) of all groups and copy to array (BodyLocations.reset() will also clear the view)
|
---@param afterBoolean boolean
|
||||||
local allGroupsList = BodyLocations.getAllGroups()
|
---@return BodyLocation
|
||||||
local allGroups = {}
|
function BodyLocationsAPI.MoveOrCreateBeforeOrAfter(toRelocateOrCreate, locationElement, afterBoolean)
|
||||||
for i = 0, allGroupsList:size()-1 do
|
-- Check type of arg 2 == string - if not error out.
|
||||||
allGroups[i + 1] = allGroupsList:get(i)
|
if type(locationElement) ~= "string" then error("Argument 2 is not of type string. Please re-check!", 2) end
|
||||||
end
|
local itemToMoveTo = group:getLocation(locationElement) -- get location to move to
|
||||||
|
if itemToMoveTo ~= nil then
|
||||||
BodyLocations.reset()
|
-- Check type of arg 1 == string - if not, error out.
|
||||||
|
if type(toRelocateOrCreate) ~= "string" then error("Argument 1 is not of type string. Please re-check!", 2) end
|
||||||
-- recreate all groups/bodylocations and insert new bodylocations
|
|
||||||
for i = 1, #allGroups do
|
local curItem = BodyLocationsAPI.New(toRelocateOrCreate)
|
||||||
local oldGroup = allGroups[i]
|
list:remove(curItem) -- remove from the list
|
||||||
local newGroup = BodyLocations.getGroup(oldGroup:getId())
|
local index = group:indexOf(locationElement) -- get current index after removal of the location to move to
|
||||||
|
if afterBoolean then index = index + 1 end -- if we want it after it, we increase the index to move to by one
|
||||||
-- FIRST: Process all original locations AND insert new ones at correct positions
|
list:add(index, curItem) -- we add the item again
|
||||||
for j = 0, oldGroup:size()-1 do
|
|
||||||
local oldLoc = oldGroup:getLocationByIndex(j)
|
|
||||||
local oldLocID = oldLoc:getId()
|
return curItem
|
||||||
|
else -- we did not find the location to move to, so we throw an error.
|
||||||
-- For each location definition, check if it should be inserted here
|
error("Could not find the BodyLocation [".. tostring(locationElement) .."] - please check the passed arguments!", 2)
|
||||||
for _, locDef in ipairs(locationList) do
|
end
|
||||||
if oldGroup:getId() == groupName then
|
|
||||||
local newLocID = type(locDef.name) ~= "string" and locDef.name or
|
|
||||||
ItemBodyLocation.get(ResourceLocation.of(locDef.name))
|
|
||||||
|
|
||||||
local refLocID = type(locDef.reference) ~= "string" and locDef.reference or
|
|
||||||
ResourceLocation.of(locDef.reference)
|
|
||||||
|
|
||||||
local isTargetGroupAndLoc = refLocID == oldLocID
|
|
||||||
|
|
||||||
if isTargetGroupAndLoc and locDef.before then
|
|
||||||
results[locDef.name] = newGroup:getOrCreateLocation(newLocID)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Add the original location
|
|
||||||
newGroup:getOrCreateLocation(oldLocID)
|
|
||||||
|
|
||||||
-- Check for "after" insertions
|
|
||||||
for _, locDef in ipairs(locationList) do
|
|
||||||
if oldGroup:getId() == groupName then
|
|
||||||
local newLocID = type(locDef.name) ~= "string" and locDef.name or
|
|
||||||
ItemBodyLocation.get(ResourceLocation.of(locDef.name))
|
|
||||||
|
|
||||||
local refLocID = type(locDef.reference) ~= "string" and locDef.reference or
|
|
||||||
ResourceLocation.of(locDef.reference)
|
|
||||||
|
|
||||||
local isTargetGroupAndLoc = refLocID == oldLocID
|
|
||||||
|
|
||||||
if isTargetGroupAndLoc and not locDef.before then
|
|
||||||
results[locDef.name] = newGroup:getOrCreateLocation(newLocID)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- SECOND: copy bodylocation properties from old groups to new groups
|
|
||||||
for j = 0, oldGroup:size()-1 do
|
|
||||||
local oldLocID = oldGroup:getLocationByIndex(j):getId()
|
|
||||||
newGroup:setMultiItem(oldLocID, oldGroup:isMultiItem(oldLocID))
|
|
||||||
copyBodyLocationProperties(oldGroup, oldLocID, newGroup)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return results
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local results = addBodyLocationsAt("Human", {
|
|
||||||
{name = "toc:TOC_Arm_L", reference = ItemBodyLocation.FULL_TOP, before = false},
|
-- function TestBodyLocations()
|
||||||
{name = "toc:TOC_Arm_R", reference = ItemBodyLocation.FULL_TOP, before = false},
|
-- local group = BodyLocations.getGroup("Human")
|
||||||
{name = "toc:TOC_ArmProst_L", reference = ItemBodyLocation.FULL_TOP, before = false},
|
-- local x = group:getAllLocations()
|
||||||
{name = "toc:TOC_ArmProst_R", reference = ItemBodyLocation.FULL_TOP, before = false},
|
|
||||||
{name = "toc:TOC_ArmAccessory_L", reference = ItemBodyLocation.FULL_TOP, before = false},
|
-- for i=0, x:size() -1 do
|
||||||
{name = "toc:TOC_ArmAccessory_R", reference = ItemBodyLocation.FULL_TOP, before = false},
|
|
||||||
})
|
-- ---@type BodyLocation
|
||||||
|
-- local bl = x:get(i)
|
||||||
|
|
||||||
|
-- print(bl:getId())
|
||||||
|
-- end
|
||||||
|
-- end
|
||||||
|
|
||||||
|
-- MultiItem causes a ton of issues... fucking hell
|
||||||
|
|
||||||
|
-- local curItem = BodyLocation.new(group, "TOC_Arm_L")
|
||||||
|
-- group:getAllLocations():add(curItem)
|
||||||
|
|
||||||
|
-- local curItem = BodyLocation.new(group, "TOC_Arm_R")
|
||||||
|
-- group:getAllLocations():add(curItem)
|
||||||
|
|
||||||
|
|
||||||
results['toc:TOC_Arm_L']:setMultiItem(true)
|
BodyLocationsAPI.New(TOC.bodylocations.TOC_Arm_L)
|
||||||
results['toc:TOC_Arm_R']:setMultiItem(true)
|
BodyLocationsAPI.New(TOC.bodylocations.TOC_Arm_R)
|
||||||
results['toc:TOC_ArmProst_L']:setMultiItem(true)
|
BodyLocationsAPI.New(TOC.bodylocations.TOC_ArmProst_L)
|
||||||
results['toc:TOC_ArmProst_R']:setMultiItem(true)
|
BodyLocationsAPI.New(TOC.bodylocations.TOC_ArmProst_R)
|
||||||
results['toc:TOC_ArmAccessory_L']:setMultiItem(true)
|
BodyLocationsAPI.New(TOC.bodylocations.TOC_ArmAccessory_L)
|
||||||
results['toc:TOC_ArmAccessory_R']:setMultiItem(true)
|
BodyLocationsAPI.New(TOC.bodylocations.TOC_ArmAccessory_R)
|
||||||
|
|||||||
Reference in New Issue
Block a user