Skip to content

Commit

Permalink
Add support for discharge to objects
Browse files Browse the repository at this point in the history
  • Loading branch information
schliesser committed Jul 7, 2024
1 parent 09ed0e3 commit 2ec4d26
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 46 deletions.
2 changes: 2 additions & 0 deletions modDesc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Please report bug on <a href="https://github.com/VertexDezign/TargetFillLevel/is
Changelog 2.0.1.0
- Show notification when target fill level is at 80% or completely full
- Add support for discharge to objects (unloading triggers, shovels) ...
Changelog 2.0.0.0
- Use custom fill type for target vehicle
Expand All @@ -29,6 +30,7 @@ Fehler bitte auf <a href="https://github.com/VertexDezign/TargetFillLevel/issues
Changelog 2.0.1.0
- Benachrichtigung anzeigen, wenn der Zielfüllstand bei 80% oder vollständig gefüllt ist
- Unterstützung für Ablade Trigger, Schaufeln, ... hinzugefügt
Changelog 2.0.0.0
- Eigener Fülltyp für die Anzeige des Zielfahrzeugs
Expand Down
172 changes: 126 additions & 46 deletions scripts/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,52 +11,113 @@ FS22_TargetFillLevel © 2023 by André Buchmann & VertexDezign is licensed under
]]

TargetFillLevel = {}
TargetFillLevel.debug = false
TargetFillLevel.debug = true

function TargetFillLevel:getFillLevelInformation(superFunc, display)
-- Variable self is the vehicle in this method
superFunc(self, display)

-- Variable self is the vehicle in this method
if SpecializationUtil.hasSpecialization(Pipe, self.specializations) then
local pipe = self.spec_pipe
tflPrint('I\'m a vehicle with a pipe!')

-- Check if vehicle has spec_foldable and exit if not unfolded
if SpecializationUtil.hasSpecialization(Foldable, self.specializations) then
if not self.spec_foldable:getIsUnfolded() then
tflPrint('unfold first!!')
return
end
end
local tlfDisplayed = TargetFillLevel:hasPipeTarget(self, display)
if not tlfDisplayed then
TargetFillLevel:hasDischargeTarget(self, display)
end
end

-- All basegame vehicles have <states num="2" unloading="2" /> except Ropa Maus6 and Grimme Ventor4150
-- they are foldable, but they have no movable pipes, so we skip them in this check
if (pipe.hasMovablePipe and pipe.targetState ~= pipe.numStates) then
tflPrint('Pipe is not ready!!')
return
end
function TargetFillLevel:hasPipeTarget(vehicle, display)
if not SpecializationUtil.hasSpecialization(Pipe, vehicle.specializations) then
return false
end

-- Check for valid discharge location
if pipe.nearestObjectInTriggerIgnoreFillLevel then
tflPrint('Invalid trailer in trigger!')
return
end
local pipe = vehicle.spec_pipe
tflPrint('I\'m a vehicle with a pipe!')

-- Get the trailer to find it's current fill level
local trailer = NetworkUtil.getObject(pipe.nearestObjectInTriggers.objectId)
if trailer == nil then
tflPrint('No trailer in trigger!')
return
-- Check if vehicle has spec_foldable and exit if not unfolded
if SpecializationUtil.hasSpecialization(Foldable, vehicle.specializations) then
if not vehicle.spec_foldable:getIsUnfolded() then
tflPrint('unfold first!!')
return false
end
end

-- All basegame vehicles have <states num="2" unloading="2" /> except Ropa Maus6 and Grimme Ventor4150
-- they are foldable, but they have no movable pipes, so we skip them in this check
if (pipe.hasMovablePipe and pipe.targetState ~= pipe.numStates) then
tflPrint('Pipe is not ready!!')
return false
end

-- Check for valid discharge location
if pipe.nearestObjectInTriggerIgnoreFillLevel then
tflPrint('Invalid trailer in trigger!')
return false
end

-- Get the trailer to find it's current fill level
local trailer = NetworkUtil.getObject(pipe.nearestObjectInTriggers.objectId)
if trailer == nil then
tflPrint('No trailer in trigger!')
return false
end

-- Display fill level of target vehicle
TargetFillLevel:addFillLevelDisplay(vehicle, trailer, display)
return true
end

function TargetFillLevel:hasDischargeTarget(vehicle, display)
if not SpecializationUtil.hasSpecialization(Dischargeable, vehicle.specializations) then
return false
end

-- Show target fill level for shovels, unload triggers,...
local dischargeable = vehicle.spec_dischargeable
local dischargeNode = dischargeable.currentDischargeNode
tflPrint('I\'m a vehicle with a Dischargeable!')

-- Show only discharges to objects. There is no level when dumping on the ground
if dischargeable:getDischargeState() ~= Dischargeable.DISCHARGE_STATE_OBJECT then
tflPrint('Wrong discharge state!')
return false
end

-- Return early when access check fails
if not dischargeable:getCanDischargeToObject(dischargeNode) then
tflPrint('Can\'t discharge to object!')
return false
end

TargetFillLevel:addFillLevelDisplay(self, trailer, display)
-- Get discharge target object
local target = dischargeNode.dischargeObject
if target == nil then
tflPrint('No discharge object in trigger!')
return false
end

if target.getFillUnitFillLevelTFL ~= nil and target.getFillUnitCapacityTFL then
-- Calculate fill level for storages / productions
local fillLevel = target:getFillUnitFillLevelTFL(dischargeNode.dischargeFillUnitIndex, dischargeable:getDischargeFillType(dischargeNode), dischargeable:getActiveFarm())
local capacity = target:getFillUnitCapacityTFL(dischargeNode.dischargeFillUnitIndex, dischargeable:getDischargeFillType(dischargeNode), dischargeable:getActiveFarm())
if fillLevel ~= nil and capacity ~= nil then
tflPrint('Show FillLevel of storage/production')
display:addFillLevel(TargetFillLevel:getFillType(), fillLevel, capacity)
return true
end
else
-- Propably a vehicle at this point
-- an unsupported object which is catched in the early exit method of addFillLevelDisplay()
TargetFillLevel:addFillLevelDisplay(vehicle, target, display)
return true
end
return false
end


function TargetFillLevel:addFillLevelDisplay(vehicle, targetVehicle, display)
-- skip unsupported target types
if targetVehicle.getFillLevelInformation == nil then
return
end
tflPrint('Show FillLevel of vehicle')

-- here we have the pipes target vehicle (e.g. trailer under combine pipe)
local spec = targetVehicle.spec_fillUnit
Expand Down Expand Up @@ -127,25 +188,44 @@ function initTargetFillLevel(name)
Vehicle.getFillLevelInformation = Utils.overwrittenFunction(Vehicle.getFillLevelInformation, TargetFillLevel.getFillLevelInformation)
end

-- Helper function to print in debug mode
initTargetFillLevel(g_currentModName)

-- ################
-- Helper functions
-- ################

-- Print when debug mode is active
function tflPrint(value)
if TargetFillLevel.debug then
print('TFL: ' .. tflDump(value))
print('TFL: ' .. tostring(value))
end
end

-- Helper function to debug tables
function tflDump(o)
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. tflDump(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end
-- Add helper functions to UnloadTrigger objects
function UnloadTrigger:getFillUnitFillLevelTFL(fillUnitIndex, fillTypeIndex, farmId)
if self.target.getFreeCapacity ~= nil then
local conversion = self.fillTypeConversions[fillTypeIndex]

initTargetFillLevel(g_currentModName)
if conversion ~= nil then
return self.target:getFillLevel(conversion.outgoingFillType, farmId, self.extraAttributes) / conversion.ratio
end

return self.target:getFillLevel(fillTypeIndex, farmId, self.extraAttributes)
end

return nil
end

function UnloadTrigger:getFillUnitCapacityTFL(fillUnitIndex, fillTypeIndex, farmId)
if self.target.getFreeCapacity ~= nil then
local conversion = self.fillTypeConversions[fillTypeIndex]

if conversion ~= nil then
return self.target:getCapacity(conversion.outgoingFillType, farmId, self.extraAttributes) / conversion.ratio
end

return self.target:getCapacity(fillTypeIndex, farmId, self.extraAttributes)
end

return nil
end

0 comments on commit 2ec4d26

Please sign in to comment.