Files

197 lines
5.6 KiB
Lua

-- Variablen
Produktion = {}
Produktion.debug = true
Produktion.debugPrefix = "[LS25-Mine] "
Produktion.fillType = "PAYDIRT"
Produktion.amountPerHour = 40000
Produktion.animationAcceleration = 0.25
Produktion.animationSpeedMax = 1.0
-- Registrierung
function Produktion.prerequisitesPresent(specializations)
return true
end
function Produktion.registerFunctions(placeableType)
SpecializationUtil.registerFunction(placeableType, "produce", Produktion.produce)
SpecializationUtil.registerFunction(placeableType, "updateAnimationState", Produktion.updateAnimationState)
end
function Produktion.registerEventListeners(placeableType)
SpecializationUtil.registerEventListener(placeableType, "onLoad", Produktion)
SpecializationUtil.registerEventListener(placeableType, "onUpdate", Produktion)
SpecializationUtil.registerEventListener(placeableType, "onFinalizePlacement", Produktion)
end
-- Initialisierung
function Produktion:onLoad(savegame)
self.produktionFirstUpdatePrinted = false
self.produktionAnimationSpeed = 0
self.produktionAnimationTargetSpeed = 0
self.produktionWasFull = false
self.produktionBuffer = 0
if self.raiseActive ~= nil then
self:raiseActive()
end
if Produktion.debug then
print(Produktion.debugPrefix .. "Produktion.lua wurde geladen")
end
end
function Produktion:onFinalizePlacement()
if self.raiseActive ~= nil then
self:raiseActive()
end
if Produktion.debug then
print(Produktion.debugPrefix .. "Mine platziert, Produktion aktiv")
end
end
-- Update
function Produktion:onUpdate(dt)
if self.produktionFirstUpdatePrinted ~= true then
self.produktionFirstUpdatePrinted = true
if Produktion.debug then
print(Produktion.debugPrefix .. "onUpdate läuft")
end
end
local timeScale = 1
if g_currentMission ~= nil and g_currentMission.missionInfo ~= nil and g_currentMission.missionInfo.timeScale ~= nil then
timeScale = g_currentMission.missionInfo.timeScale
end
local gameTimeHours = (dt * timeScale) / 3600000
local amountToProduce = Produktion.amountPerHour * gameTimeHours
self.produktionBuffer = self.produktionBuffer + amountToProduce
local producedAmount = 0
if self.produktionBuffer >= 1 then
producedAmount = self:produce(self.produktionBuffer)
self.produktionBuffer = self.produktionBuffer - producedAmount
if self.produktionBuffer > 1000 then
self.produktionBuffer = 1000
end
end
if producedAmount > 0 then
self:updateAnimationState(true, dt)
else
self:updateAnimationState(false, dt)
end
if self.raiseActive ~= nil then
self:raiseActive()
end
end
-- Produktion
function Produktion:produce(amount)
if self.isServer == false then
return 0
end
local spec = self.spec_silo
if spec == nil or spec.storages == nil then
if Produktion.debug then
print(Produktion.debugPrefix .. "Kein Silo-Speicher gefunden")
end
return 0
end
local fillTypeIndex = g_fillTypeManager:getFillTypeIndexByName(Produktion.fillType)
if fillTypeIndex == nil then
if Produktion.debug then
print(Produktion.debugPrefix .. "FillType nicht gefunden: " .. Produktion.fillType)
end
return 0
end
local remainingAmount = amount
local producedAmount = 0
local hasFreeCapacity = false
for _, storage in ipairs(spec.storages) do
local freeCapacity = storage:getFreeCapacity(fillTypeIndex)
if freeCapacity > 0 then
hasFreeCapacity = true
local movedAmount = math.min(remainingAmount, freeCapacity)
local oldFillLevel = storage:getFillLevel(fillTypeIndex) or 0
storage:setFillLevel(oldFillLevel + movedAmount, fillTypeIndex)
remainingAmount = remainingAmount - movedAmount
producedAmount = producedAmount + movedAmount
end
if remainingAmount <= 0.001 then
break
end
end
if Produktion.debug then
if producedAmount > 0 then
self.produktionWasFull = false
if producedAmount >= 1 then
print(Produktion.debugPrefix .. "Produziert: " .. tostring(math.floor(producedAmount)) .. " L " .. Produktion.fillType)
end
elseif hasFreeCapacity == false and self.produktionWasFull ~= true then
self.produktionWasFull = true
print(Produktion.debugPrefix .. "Speicher voll, Produktion gestoppt")
end
end
return producedAmount
end
-- Animation vorbereitet
function Produktion:updateAnimationState(isProducing, dt)
if isProducing then
self.produktionAnimationTargetSpeed = Produktion.animationSpeedMax
else
self.produktionAnimationTargetSpeed = 0
end
local step = Produktion.animationAcceleration * (dt / 1000)
if self.produktionAnimationSpeed < self.produktionAnimationTargetSpeed then
self.produktionAnimationSpeed = math.min(self.produktionAnimationSpeed + step, self.produktionAnimationTargetSpeed)
elseif self.produktionAnimationSpeed > self.produktionAnimationTargetSpeed then
self.produktionAnimationSpeed = math.max(self.produktionAnimationSpeed - step, self.produktionAnimationTargetSpeed)
end
-- Hier kommt später die echte Animation rein.
-- Beispiel später:
-- self:setAnimationTime("mineWorkAnimation", self.produktionAnimationSpeed, true)
-- oder:
-- setRotation(self.animationNode, 0, self.produktionAnimationSpeed, 0)
end