Skip to content
74 changes: 34 additions & 40 deletions internal/gm-unit/editor_body.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,37 @@ function Editor_Body_Modifier:beautifyString(text)
return out
end

function Editor_Body_Modifier:setPartModifier(indexList, value)
for _, index in ipairs(indexList) do
self.target_unit.appearance.bp_modifiers[index] = tonumber(value)
function Editor_Body_Modifier:recalculateBodySize()
-- Manually recalculate body size
local new_size = self.target_unit.body.size_info.size_base
local caste = df.creature_raw.find(self.target_unit.race).caste[self.target_unit.caste]
for idx, mod_entry in ipairs(caste.body_appearance_modifiers) do
local t = mod_entry.modifier.type
if t >= 0 and t <= 2 then -- 0=HEIGHT, 1=BROADNESS, 2=LENGTH
local mod_val = self.target_unit.appearance.body_modifiers[idx]
new_size = math.floor((new_size * mod_val) / 100)
end
end
self.target_unit.body.size_info.size_cur = new_size
end

-- Update the unit's portrait
self.target_unit.flags4.portrait_must_be_refreshed = true
-- Update the world texture
self.target_unit.flags4.any_texture_must_be_refreshed = true

self:updateChoices()
function Editor_Body_Modifier:getModifier(modifier)
if self.partChoice.type == "part" then
return self.target_unit.appearance.bp_modifiers[modifier.idx[1]]
else
return self.target_unit.appearance.body_modifiers[modifier.index]
end
end

function Editor_Body_Modifier:setBodyModifier(modifierIndex, value)
self.target_unit.appearance.body_modifiers[modifierIndex] = tonumber(value)
function Editor_Body_Modifier:setModifier(modifier, value)
if self.partChoice.type == "part" then
for _, idx in ipairs(modifier.idx) do
self.target_unit.appearance.bp_modifiers[idx] = tonumber(value)
end
else
self.target_unit.appearance.body_modifiers[modifier.index] = tonumber(value)
self:recalculateBodySize()
end

-- Update the unit's portrait
self.target_unit.flags4.portrait_must_be_refreshed = true
Expand All @@ -55,12 +71,7 @@ function Editor_Body_Modifier:selected(index, selected)
nil,
tostring(selected.value),
function(newValue)
local value = tonumber(newValue)
if self.partChoice.type == "part" then
self:setPartModifier(selected.modifier.idx, value)
else -- Body
self:setBodyModifier(selected.modifier.index, value)
end
self:setModifier(selected.modifier, tonumber(newValue))
end,
nil,nil
)
Expand All @@ -87,11 +98,7 @@ function Editor_Body_Modifier:random()
local value = min + roll

-- Set the modifier to the new value
if self.partChoice.type == "part" then
self:setPartModifier(selected.modifier.idx, value)
else
self:setBodyModifier(selected.modifier.index, value)
end
self:setModifier(selected.modifier, value)
end

function Editor_Body_Modifier:step(amount)
Expand All @@ -108,40 +115,27 @@ function Editor_Body_Modifier:step(amount)

-- Now determine what range the modifier currently falls into
local currentValue = selected.value
local rangeIndex
local rangeIndex = #ranges

for index, value in ipairs(ranges) do
if ranges[index+1] then -- There's still a next entry
if currentValue < ranges[index+1] then -- The current value is less than the next entry
rangeIndex = index
break
end
else -- This is the last entry
if ranges[index+1] and currentValue < ranges[index+1] then
rangeIndex = index
break
end
end

-- Finally, move the modifier's value up / down in range tiers based on given amount
local newTier = math.min(#ranges, math.max(1, rangeIndex + amount)) -- Clamp values to not go beyond bounds of ranges
local newValue = ranges[newTier]

if self.partChoice.type == "part" then
self:setPartModifier(selected.modifier.idx, newValue)
else
self:setBodyModifier(selected.modifier.index, newValue)
end
self:setModifier(selected.modifier, newValue)
end

function Editor_Body_Modifier:updateChoices()
local choices = {}

for index, modifier in ipairs(self.partChoice.modifiers) do
local currentValue
if self.partChoice.type == "part" then
currentValue = self.target_unit.appearance.bp_modifiers[modifier.idx[1]]
else -- Body
currentValue = self.target_unit.appearance.body_modifiers[modifier.index]
end
local currentValue = self:getModifier(modifier)
table.insert(choices, {text = self:beautifyString(df.appearance_modifier_type[modifier.entry.modifier.type]) .. ": " .. currentValue, value = currentValue, modifier = modifier})
end

Expand Down