Skip to content

Commit

Permalink
Implement standalone pump. Requires >= 2 references. (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnwop committed Apr 7, 2018
1 parent b8ec323 commit a5c5cf2
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 5 deletions.
25 changes: 21 additions & 4 deletions src/vehicles/HoseSystemPumpMotor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,25 @@ function HoseSystemPumpMotor:load(savegame)
self.warningMessage.messages[HoseSystemPumpMotor.OBJECT_FULL] = g_i18n:getText('pumpMotor_warningObjectFull')
self.warningMessage.messages[HoseSystemPumpMotor.INVALID_FILLTYPE] = g_i18n:getText('pumpMotor_warningInvalidFilltype')

local isStandalone = Utils.getNoNil(getXMLBool(self.xmlFile, "vehicle.pumpMotor#isStandalone"), false)

self.pumpMotor = {
isStandalone = isStandalone,
unloadInfoIndex = Utils.getNoNil(getXMLInt(self.xmlFile, "vehicle.pumpMotor#unloadInfoIndex"), 1),
dischargeInfoIndex = Utils.getNoNil(getXMLInt(self.xmlFile, "vehicle.pumpMotor#dischargeInfoIndex"), 1),
ptoRpm = self.powerConsumer.ptoRpm
dischargeInfoIndex = Utils.getNoNil(getXMLInt(self.xmlFile, "vehicle.pumpMotor#dischargeInfoIndex"), 1)
}

-- Todo: lookup what we actually need on the current fillObject. (can we fill to multiple targets!?)
-- self.fillableObjects = {}
self.pumpStrategies = {}

if isStandalone then
local strategy = HoseSystemPumpMotorFactory.getPumpStrategy(HoseSystemPumpMotorFactory.TYPE_STANDALONE, self)

if strategy:prerequisitesPresent(self) then
self.pumpStrategies = HoseSystemUtil.insertStrategy(HoseSystemPumpMotorFactory.getPumpStrategy(HoseSystemPumpMotorFactory.TYPE_STANDALONE, self), self.pumpStrategies)
end
end

HoseSystemUtil.callStrategyFunction(self.pumpStrategies, 'load', { self.xmlFile, "vehicle.pumpMotor" })

self.sourceObject = nil
self.fillObject = nil
Expand Down Expand Up @@ -274,6 +285,8 @@ function HoseSystemPumpMotor:update(dt)
self.updateNetworkSourceObject = false
end

HoseSystemUtil.callStrategyFunction(self.pumpStrategies, 'update', { dt })

if self:getIsActive() then
if self:getIsActiveForInput() and not self:hasInputConflictWithSelection() then
if InputBinding.hasEvent(InputBinding.ACTIVATE_OBJECT) then
Expand Down Expand Up @@ -303,6 +316,8 @@ end
-- @param dt
--
function HoseSystemPumpMotor:updateTick(dt)
HoseSystemUtil.callStrategyFunction(self.pumpStrategies, 'updateTick', { dt })

if self.isClient then
if self.pumpIsStarted then
if self.pumpEfficiency.currentScale ~= 0 then
Expand Down Expand Up @@ -788,6 +803,8 @@ function HoseSystemPumpMotor:addFillObject(object, fillMode, rayCasted)
local rootVehicle = self:getRootAttacherVehicle()

sourceObject = HoseSystemPumpMotor.findAttachedTransferTank(rootVehicle)
elseif self.pumpMotor.isStandalone then
sourceObject = self.standAloneSourceObject
end

if sourceObject ~= nil then
Expand Down
29 changes: 28 additions & 1 deletion src/vehicles/HoseSystemPumpMotorFactory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,29 @@

HoseSystemPumpMotorFactory = {}

HoseSystemPumpMotorFactory.TYPE_STANDALONE = "standalone"

HoseSystemPumpMotorFactory.numFillModes = 0
HoseSystemPumpMotorFactory.fillModes = {}

---
--
function HoseSystemPumpMotorFactory:preLoadHoseSystem()
local srcDirectory = g_hoseSystem.baseDirectory .. 'src/vehicles/strategies'

local files = {
('%s/%s'):format(srcDirectory, 'HoseSystemStandalonePumpStrategy.lua')
}

for _, path in pairs(files) do
source(path)
end

-- Register the fill mode for the hose system
HoseSystemPumpMotorFactory.registerFillMode(HoseSystemConnectorFactory.TYPE_HOSE_COUPLING)
HoseSystemPumpMotorFactory.registerFillMode(HoseSystemFillArmFactory.TYPE_DOCK)
HoseSystemPumpMotorFactory.registerFillMode(HoseSystemFillArmFactory.TYPE_ARM)
HoseSystemPumpMotorFactory.registerFillMode(HoseSystemPumpMotorFactory.TYPE_STANDALONE)
end

---
Expand Down Expand Up @@ -67,4 +80,18 @@ function HoseSystemPumpMotorFactory.getInitialFillMode(name)
end

return nil
end
end

---
-- @param type
-- @param object
--
function HoseSystemPumpMotorFactory.getPumpStrategy(type, object)
local strategy

if type == HoseSystemPumpMotorFactory.TYPE_STANDALONE then
strategy = HoseSystemStandalonePumpStrategy:new(object)
end

return strategy
end
80 changes: 80 additions & 0 deletions src/vehicles/strategies/HoseSystemStandalonePumpStrategy.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
HoseSystemStandalonePumpStrategy = {}

local HoseSystemStandalonePumpStrategy_mt = Class(HoseSystemStandalonePumpStrategy)

---
-- @param object
--
function HoseSystemStandalonePumpStrategy:prerequisitesPresent(object)
if not SpecializationUtil.hasSpecialization(HoseSystemConnector, object.specializations) then
HoseSystemUtil:log(HoseSystemUtil.ERROR, "Strategy HoseSystemStandalonePumpStrategy needs the specialization HoseSystemConnector")

return false
end

return true
end

---
-- @param object
-- @param mt
--
function HoseSystemStandalonePumpStrategy:new(object, mt)
local pumpStrategy = {
object = object
}

object.standAloneSourceObject = nil

setmetatable(pumpStrategy, mt == nil and HoseSystemStandalonePumpStrategy_mt or mt)

return pumpStrategy
end

---
-- @param xmlFile
-- @param key
--
function HoseSystemStandalonePumpStrategy:load(xmlFile, key)
end

function HoseSystemStandalonePumpStrategy:update(dt)
end

---
-- @param ref1
-- @param ref2
--
local sortReferencesByActiveState = function(ref1, ref2)
return ref1.isActive and not ref2.isActive
end

function HoseSystemStandalonePumpStrategy:updateTick(dt)
if not self.object.isServer or next(self.object.attachedHoseSystemReferences) == nil then
return
end

-- use custom since we need the number of elements which are not nil, the # or maxn operator won't do in this case..
if HoseSystemUtil.getNoNilAmount(self.object.attachedHoseSystemReferences) >= 2 then -- we only need two sources.
table.sort(self.object.attachedHoseSystemReferences, sortReferencesByActiveState)

for _, entry in pairs({ unpack(self.object.attachedHoseSystemReferences, 1, 2) }) do
if entry ~= nil and entry.isActive and entry.fillObject ~= nil then
if entry.fillObject ~= self.object.fillObject and entry.fillObject ~= self.object.standAloneSourceObject then
self.object.standAloneSourceObject = entry.fillObject
self.object:addFillObject(self.object.fillObject, self.object:getFillMode())
end
end
end

if g_hoseSystem.debugRendering then
-- local debugTable = {}
--
-- for _, n in pairs(self.object.attachedHoseSystemReferences) do
-- table.insert(debugTable, { isActive = n.isActive })
-- end
--
-- HoseSystemUtil:log(HoseSystemUtil.DEBUG, debugTable)
end
end
end

0 comments on commit a5c5cf2

Please sign in to comment.