-- author: DD ModPassion
-- date: 14.04.2021
-- copyright (C): DD ModPassion, All Rights Reserved
-- version: farming simulator 19
SprayerAddon = {} local modName = g_currentModName function SprayerAddon.prerequisitesPresent(specializations) return true end function SprayerAddon.registerEventListeners(vehicleType) SpecializationUtil.registerEventListener(vehicleType, "onLoad", SprayerAddon) SpecializationUtil.registerEventListener(vehicleType, "onUpdate", SprayerAddon) SpecializationUtil.registerEventListener(vehicleType, "onPostAttach", SprayerAddon) SpecializationUtil.registerEventListener(vehicleType, "onPreDetach", SprayerAddon) end function SprayerAddon.registerOverwrittenFunctions(vehicleType) SpecializationUtil.registerOverwrittenFunction(vehicleType, "getSprayerUsage", SprayerAddon.getSprayerUsage) end function SprayerAddon:onLoad(savegame) self.spec_sprayerAddon = self[string.format("spec_%s.sprayerAddon", modName)] local spec = self.spec_sprayerAddon spec.attachedSPL = nil end function SprayerAddon:onUpdate(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected) local spec = self.spec_sprayerAddon if spec.attachedSPL ~= nil then self:setFillUnitLastValidFillType(1, spec.attachedSPL:getFillUnitFillType(1), true) local rootVehicle = self:getRootVehicle() if rootVehicle.getAIIsTurning ~= nil and self:getIsAIActive() then if not rootVehicle:getAIIsTurning() then if not spec.attachedSPL:getIsTurnedOn() then spec.attachedSPL:setIsTurnedOn(true) end else if spec.attachedSPL:getIsTurnedOn() then spec.attachedSPL:setIsTurnedOn(false) end end end end end function SprayerAddon:onPostAttach(attacherVehicle, inputJointDescIndex, jointDescIndex) local spec = self.spec_sprayerAddon spec.attachedSPL = self:getAttacherVehicle() end function SprayerAddon:onPreDetach(attacherVehicle, implement) local spec = self.spec_sprayerAddon if spec.attachedSPL ~= nil then spec.attachedSPL = nil end end function SprayerAddon:getSprayerUsage(superFunc, fillType, dt) local usage = superFunc(self, fillType, dt) if usage == 0 then if fillType == FillType.UNKNOWN then return 0 end local spec = self.spec_sprayer local scale = Utils.getNoNil(spec.usageScale.fillTypeScales[fillType], spec.usageScale.default) local litersPerSecond = 1 local sprayType = g_sprayTypeManager:getSprayTypeByFillTypeIndex(fillType) if sprayType ~= nil then litersPerSecond = sprayType.litersPerSecond end local activeSprayType = self:getActiveSprayType() local workWidth = spec.usageScale.workingWidth if activeSprayType ~= nil then workWidth = activeSprayType.usageScale.workingWidth or workWidth end return scale * litersPerSecond * self.speedLimit * workWidth * dt * 0.001 end return usage end