Skip to content

Commit bd2ed00

Browse files
committed
fix: remove excess fill when resetting capacity to original
Clamp fill levels on reset for all storage types: - Per-filltype storage: clamp each fill to its capacity - Shared capacity: proportionally reduce all fill types - HusbandryFood: proportionally reduce via removeFood() - Vehicles: clamp via addFillUnitFillLevel with ToolType.UNDEFINED Also fix original capacity capture for newly placed buildings by adding onFinalizePlacement hook (uniqueId is nil during onLoad for new placements).
1 parent 8d0a67c commit bd2ed00

4 files changed

Lines changed: 165 additions & 1 deletion

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ The mod supports multiplayer with a permission system:
147147

148148
## Changelog
149149

150+
### 0.6.1.0 (Beta):
151+
- Fixed excess fill not being removed when resetting capacity to original (shared capacity storages reduce proportionally)
152+
- Fixed reset failing on newly placed buildings with "No original capacities recorded" error
153+
150154
### 0.6.0.0 (Beta)
151155

152156
- Added in-vehicle capacity adjustment: press K while driving to adjust capacity of your vehicle and attached implements

modDesc.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8" standalone="no"?>
22
<modDesc descVersion="106">
33
<author>Ritter</author>
4-
<version>0.6.0.0</version>
4+
<version>0.6.1.0</version>
55
<title>
66
<en>Adjust Storage Capacity</en>
77
</title>
@@ -42,6 +42,10 @@ Not Supported:
4242
4343
Changelog:
4444
45+
0.6.1.0 (Beta):
46+
- Fixed excess fill not being removed when resetting capacity to original (shared capacity storages reduce proportionally)
47+
- Fixed reset failing on newly placed buildings with "No original capacities recorded" error
48+
4549
0.6.0.0 (Beta):
4650
- Added in-vehicle capacity adjustment: press K while driving to adjust capacity of your vehicle and attached implements
4751
- Added auto-scale vehicle mass setting: keeps expanded vehicles drivable by scaling weight to original capacity

scripts/RmAdjustStorageCapacity.lua

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,121 @@ function RmAdjustStorageCapacity:applyHusbandryFoodCapacity(spec, newCapacity)
11321132
oldCapacity, newCapacity, totalFill, spec.FILLLEVEL_NUM_BITS)
11331133
end
11341134

1135+
--- Clamp excess fill levels on a Storage object after capacity reduction.
1136+
--- Per-filltype: clamps each fillType to its capacity.
1137+
--- Shared capacity: proportionally reduces all fill types so total fits new capacity.
1138+
---@param storage table The Storage object
1139+
function RmAdjustStorageCapacity:clampExcessFill(storage)
1140+
if storage == nil or storage.fillLevels == nil then
1141+
return
1142+
end
1143+
1144+
-- Per-filltype capacity
1145+
if storage.capacities ~= nil and next(storage.capacities) ~= nil then
1146+
for fillTypeIndex, level in pairs(storage.fillLevels) do
1147+
local capacity = storage.capacities[fillTypeIndex]
1148+
if capacity ~= nil and level > capacity then
1149+
local fillType = g_fillTypeManager:getFillTypeByIndex(fillTypeIndex)
1150+
local fillTypeName = fillType and fillType.name or "UNKNOWN"
1151+
Log:info("Clamping overfill for %s: %d -> %d", fillTypeName, level, capacity)
1152+
storage:setFillLevel(capacity, fillTypeIndex)
1153+
end
1154+
end
1155+
return
1156+
end
1157+
1158+
-- Shared capacity: proportionally reduce all fill types
1159+
if storage.capacity ~= nil then
1160+
local totalFill = 0
1161+
for _, level in pairs(storage.fillLevels) do
1162+
totalFill = totalFill + level
1163+
end
1164+
if totalFill > storage.capacity and totalFill > 0 then
1165+
local ratio = storage.capacity / totalFill
1166+
Log:info("Proportionally reducing shared storage fill from %d to fit capacity %d",
1167+
totalFill, storage.capacity)
1168+
for ft, level in pairs(storage.fillLevels) do
1169+
if level > 0 then
1170+
local newLevel = math.floor(level * ratio)
1171+
storage:setFillLevel(newLevel, ft)
1172+
end
1173+
end
1174+
end
1175+
end
1176+
end
1177+
1178+
--- Clamp excess fill levels on HusbandryFood after capacity reduction.
1179+
--- Proportionally reduces all food fill types so total fits new capacity.
1180+
---@param placeable table The husbandry placeable
1181+
function RmAdjustStorageCapacity:clampExcessHusbandryFood(placeable)
1182+
if placeable == nil then
1183+
return
1184+
end
1185+
1186+
local spec = placeable.spec_husbandryFood
1187+
if spec == nil or spec.fillLevels == nil then
1188+
return
1189+
end
1190+
1191+
local totalFill = 0
1192+
for _, level in pairs(spec.fillLevels) do
1193+
totalFill = totalFill + level
1194+
end
1195+
1196+
if totalFill > spec.capacity and totalFill > 0 then
1197+
local ratio = spec.capacity / totalFill
1198+
Log:info("Proportionally reducing husbandry food fill from %d to fit capacity %d",
1199+
totalFill, spec.capacity)
1200+
for ft, level in pairs(spec.fillLevels) do
1201+
if level > 0 then
1202+
local targetLevel = math.floor(level * ratio)
1203+
local excess = level - targetLevel
1204+
if excess > 0 then
1205+
placeable:removeFood(excess, ft)
1206+
end
1207+
end
1208+
end
1209+
end
1210+
end
1211+
1212+
--- Clamp excess fill level on a vehicle fill unit after capacity reduction.
1213+
--- If fillUnitIndex is nil, clamps all fill units.
1214+
---@param vehicle table The vehicle
1215+
---@param fillUnitIndex number|nil The fill unit index (nil = all)
1216+
function RmAdjustStorageCapacity:clampExcessVehicleFill(vehicle, fillUnitIndex)
1217+
if vehicle == nil then
1218+
return
1219+
end
1220+
1221+
local fillUnitSpec = vehicle.spec_fillUnit
1222+
if fillUnitSpec == nil or fillUnitSpec.fillUnits == nil then
1223+
return
1224+
end
1225+
1226+
local farmId = vehicle:getOwnerFarmId()
1227+
1228+
if fillUnitIndex ~= nil then
1229+
local fillUnit = fillUnitSpec.fillUnits[fillUnitIndex]
1230+
if fillUnit ~= nil and fillUnit.fillLevel > fillUnit.capacity then
1231+
local excess = fillUnit.fillLevel - fillUnit.capacity
1232+
local fillType = vehicle:getFillUnitFillType(fillUnitIndex) or FillType.UNKNOWN
1233+
Log:info("Clamping vehicle %s fillUnit[%d] overfill: %d -> %d",
1234+
vehicle:getName(), fillUnitIndex, fillUnit.fillLevel, fillUnit.capacity)
1235+
vehicle:addFillUnitFillLevel(farmId, fillUnitIndex, -excess, fillType, ToolType.UNDEFINED)
1236+
end
1237+
else
1238+
for i, fillUnit in ipairs(fillUnitSpec.fillUnits) do
1239+
if fillUnit.fillLevel > fillUnit.capacity then
1240+
local excess = fillUnit.fillLevel - fillUnit.capacity
1241+
local fillType = vehicle:getFillUnitFillType(i) or FillType.UNKNOWN
1242+
Log:info("Clamping vehicle %s fillUnit[%d] overfill: %d -> %d",
1243+
vehicle:getName(), i, fillUnit.fillLevel, fillUnit.capacity)
1244+
vehicle:addFillUnitFillLevel(farmId, i, -excess, fillType, ToolType.UNDEFINED)
1245+
end
1246+
end
1247+
end
1248+
end
1249+
11351250
-- ============================================================================
11361251
-- Capacity Modification API
11371252
-- ============================================================================
@@ -1321,6 +1436,7 @@ function RmAdjustStorageCapacity:resetCapacity(placeable, fillTypeIndex)
13211436
local storageInfo = self:getStorageInfo(placeable)
13221437
if storageInfo.husbandryFood ~= nil then
13231438
self:applyHusbandryFoodCapacity(storageInfo.husbandryFood, originals.husbandryFood)
1439+
self:clampExcessHusbandryFood(placeable)
13241440
-- Update visual fill planes
13251441
self:updatePlaceableFillPlanes(placeable)
13261442
end
@@ -1351,6 +1467,11 @@ function RmAdjustStorageCapacity:resetCapacity(placeable, fillTypeIndex)
13511467
self:applyCapacitiesToPlaceable(placeable, customCapacity)
13521468
end
13531469

1470+
-- Clamp excess fill after shared capacity reduction
1471+
for _, info in ipairs(storageInfo.storages) do
1472+
self:clampExcessFill(info.storage)
1473+
end
1474+
13541475
Log:info("Reset shared capacity for %s to original", placeable:getName())
13551476
else
13561477
-- Reset per-filltype capacity
@@ -1364,6 +1485,11 @@ function RmAdjustStorageCapacity:resetCapacity(placeable, fillTypeIndex)
13641485
self:applyCapacitiesToPlaceable(placeable, customCapacity)
13651486
end
13661487

1488+
-- Clamp excess fill after per-filltype capacity reduction
1489+
for _, info in ipairs(storageInfo.storages) do
1490+
self:clampExcessFill(info.storage)
1491+
end
1492+
13671493
Log:info("Reset capacity for %s fillType=%d to original", placeable:getName(), fillTypeIndex)
13681494
end
13691495
else
@@ -1382,11 +1508,18 @@ function RmAdjustStorageCapacity:resetCapacity(placeable, fillTypeIndex)
13821508
local storageInfo = self:getStorageInfo(placeable)
13831509
if storageInfo.husbandryFood ~= nil then
13841510
self:applyHusbandryFoodCapacity(storageInfo.husbandryFood, originals.husbandryFood)
1511+
self:clampExcessHusbandryFood(placeable)
13851512
-- Update visual fill planes
13861513
self:updatePlaceableFillPlanes(placeable)
13871514
end
13881515
end
13891516

1517+
-- Clamp excess fill after all capacity reductions
1518+
local storageInfo = self:getStorageInfo(placeable)
1519+
for _, info in ipairs(storageInfo.storages) do
1520+
self:clampExcessFill(info.storage)
1521+
end
1522+
13901523
-- Reset load speed to original
13911524
self:resetLoadSpeed(placeable)
13921525

@@ -1490,6 +1623,9 @@ function RmAdjustStorageCapacity:resetVehicleCapacity(vehicle, fillUnitIndex)
14901623
self:applyVehicleCapacity(vehicle, fillUnitIndex, originalCapacity)
14911624
end
14921625

1626+
-- Clamp excess fill after capacity reduction
1627+
self:clampExcessVehicleFill(vehicle, fillUnitIndex)
1628+
14931629
-- Explicitly reset discharge speed for this fill unit
14941630
RmVehicleStorageCapacity.resetDischargeSpeed(vehicle, fillUnitIndex)
14951631

@@ -1503,6 +1639,9 @@ function RmAdjustStorageCapacity:resetVehicleCapacity(vehicle, fillUnitIndex)
15031639
self:applyVehicleCapacity(vehicle, fuIndex, originalCapacity)
15041640
end
15051641

1642+
-- Clamp excess fill after all capacity reductions
1643+
self:clampExcessVehicleFill(vehicle, nil)
1644+
15061645
-- Explicitly reset all discharge speeds
15071646
RmVehicleStorageCapacity.resetDischargeSpeed(vehicle, nil)
15081647

scripts/placeables/RmPlaceableStorageCapacity.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ end
6262
function RmPlaceableStorageCapacity.registerEventListeners(placeableType)
6363
SpecializationUtil.registerEventListener(placeableType, "onLoad", RmPlaceableStorageCapacity)
6464
SpecializationUtil.registerEventListener(placeableType, "onPostLoad", RmPlaceableStorageCapacity)
65+
SpecializationUtil.registerEventListener(placeableType, "onFinalizePlacement", RmPlaceableStorageCapacity)
6566
SpecializationUtil.registerEventListener(placeableType, "onDelete", RmPlaceableStorageCapacity)
6667
SpecializationUtil.registerEventListener(placeableType, "onReadStream", RmPlaceableStorageCapacity)
6768
SpecializationUtil.registerEventListener(placeableType, "onWriteStream", RmPlaceableStorageCapacity)
@@ -191,6 +192,22 @@ function RmPlaceableStorageCapacity:onPostLoad(savegame)
191192
table.concat(spec.storageTypes, ", "))
192193
end
193194

195+
--- Called when placeable finalization completes (after uniqueId is assigned)
196+
--- For newly placed buildings, uniqueId is nil during onLoad but available here.
197+
--- Retries original capacity capture if it was missed in onLoad.
198+
function RmPlaceableStorageCapacity:onFinalizePlacement()
199+
local uniqueId = self.uniqueId
200+
if uniqueId == nil then
201+
return
202+
end
203+
204+
-- Capture original capacities if not already captured (e.g., new placements where
205+
-- uniqueId was nil during onLoad)
206+
if RmAdjustStorageCapacity.originalCapacities[uniqueId] == nil then
207+
RmAdjustStorageCapacity:captureOriginalCapacities(self)
208+
end
209+
end
210+
194211
--- Detect which storage types are present on this placeable
195212
---@return table Array of storage type strings
196213
function RmPlaceableStorageCapacity:detectStorageTypes()

0 commit comments

Comments
 (0)