@@ -1132,6 +1132,121 @@ function RmAdjustStorageCapacity:applyHusbandryFoodCapacity(spec, newCapacity)
11321132 oldCapacity , newCapacity , totalFill , spec .FILLLEVEL_NUM_BITS )
11331133end
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
0 commit comments