LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

PlaceableChargingStation

Description
Specialization for placeables
Functions

getChargeState

Description
Definition
getChargeState()
Code
101function PlaceableChargingStation:getChargeState()
102 local spec = self.spec_chargingStation
103 if spec.loadTrigger ~= nil then
104 local index = next(spec.loadTrigger.fillableObjects)
105 if index ~= nil then
106 local vehicle = spec.loadTrigger.fillableObjects[index].object
107 if vehicle.getConsumerFillUnitIndex ~= nil then
108 local fillUnitIndex = vehicle:getConsumerFillUnitIndex(FillType.ELECTRICCHARGE)
109 if fillUnitIndex ~= nil then
110 return vehicle:getFillUnitFillLevel(fillUnitIndex), vehicle:getFillUnitCapacity(fillUnitIndex)
111 end
112 end
113 end
114 end
115
116 return 0, 1
117end

onLoad

Description
Called on loading
Definition
onLoad(table savegame)
Arguments
tablesavegamesavegame
Code
57function PlaceableChargingStation:onLoad(savegame)
58 local spec = self.spec_chargingStation
59
60 spec.chargeIndicatorIntensity = self.xmlFile:getValue("placeable.chargingStation.chargeIndicator#intensity", 20)
61 spec.chargeIndicatorBlinkSpeed = self.xmlFile:getValue("placeable.chargingStation.chargeIndicator#blinkSpeed", 5)
62
63 spec.chargeIndicatorNode = self.xmlFile:getValue("placeable.chargingStation.chargeIndicator#node", nil, self.components, self.i3dMappings)
64 if spec.chargeIndicatorNode ~= nil then
65 setShaderParameter(spec.chargeIndicatorNode, "lightControl", spec.chargeIndicatorIntensity, 0, 0, 0, false)
66 setShaderParameter(spec.chargeIndicatorNode, "emitColor", 1, 1, 0, 0, false)
67 end
68
69 spec.chargeIndicatorLight = self.xmlFile:getValue("placeable.chargingStation.chargeIndicator#light", nil, self.components, self.i3dMappings)
70 if spec.chargeIndicatorLight then
71 setLightColor(spec.chargeIndicatorLight, 0, 0, 0)
72 end
73
74 spec.chargeIndicatorColorFull = self.xmlFile:getValue("placeable.chargingStation.chargeIndicator#colorFull", "0 1 0 1", true)
75 spec.chargeIndicatorColorEmpty = self.xmlFile:getValue("placeable.chargingStation.chargeIndicator#colorEmpty", "1 1 0 1", true)
76 spec.chargeIndicatorLightColor = spec.chargeIndicatorColorFull
77
78 spec.interactionRadius = self.xmlFile:getValue("placeable.chargingStation#interactionRadius", 5)
79
80 spec.loadTrigger = nil
81
82 spec.buyingStation = self:getBuyingStation()
83 if spec.buyingStation ~= nil then
84 for j=1, #spec.buyingStation.loadTriggers do
85 local loadTrigger = spec.buyingStation.loadTriggers[j]
86
87 spec.loadTrigger = loadTrigger
88
89 spec.fillSample = g_soundManager:loadSampleFromXML(self.xmlFile, "placeable.chargingStation.sounds", "fill", self.baseDirectory, self.components, 0, AudioGroup.ENVIRONMENT, self.i3dMappings, nil)
90 if spec.fillSample ~= nil then
91 if loadTrigger.samples.load == nil then
92 loadTrigger.samples.load = spec.fillSample
93 end
94 end
95 end
96 end
97end

onUpdate

Description
Update
Definition
onUpdate(float dt)
Arguments
floatdttime since last call in ms
Code
122function PlaceableChargingStation:onUpdate(dt)
123 local spec = self.spec_chargingStation
124
125 if spec.loadTrigger ~= nil then
126 local isActive = next(spec.loadTrigger.fillableObjects) ~= nil
127
128 if spec.chargeIndicatorNode ~= nil then
129 if isActive then
130 local color = spec.chargeIndicatorColorEmpty
131 local fillLevel, capacity = self:getChargeState()
132 if fillLevel / capacity > 0.95 then
133 color = spec.chargeIndicatorColorFull
134 end
135
136 setShaderParameter(spec.chargeIndicatorNode, "colorScale", color[1], color[2], color[3], color[4], false)
137 spec.chargeIndicatorLightColor = color
138 end
139
140 local blinkSpeed = spec.loadTrigger.isLoading and spec.chargeIndicatorBlinkSpeed or 0
141 setShaderParameter(spec.chargeIndicatorNode, "blinkSpeed", blinkSpeed, 0, 0, 0, false)
142 setShaderParameter(spec.chargeIndicatorNode, "lightControl", isActive and spec.chargeIndicatorIntensity or 0, 0, 0, 0, false)
143
144 if spec.chargeIndicatorLight ~= nil then
145 local alpha = 0
146 if isActive then
147 local x, y, z, _ = getShaderParameter(spec.chargeIndicatorNode, "blinkOffset")
148 alpha = MathUtil.clamp(math.cos(blinkSpeed * z * (getShaderTimeSec() - y) + 2 * math.pi * x) + 0.2, 0, 1)
149 end
150
151 setLightColor(spec.chargeIndicatorLight, spec.chargeIndicatorLightColor[1]*alpha, spec.chargeIndicatorLightColor[2]*alpha, spec.chargeIndicatorLightColor[3]*alpha)
152 end
153 end
154
155 if spec.loadTrigger.isLoading then
156 local allowDisplay = false
157 if g_currentMission.controlPlayer and g_currentMission.player ~= nil then
158 local distance = calcDistanceFrom(g_currentMission.player.rootNode, self.rootNode)
159 if distance < spec.interactionRadius then
160 allowDisplay = true
161 end
162 else
163 if g_currentMission.controlledVehicle ~= nil then
164 for _, object in pairs(spec.loadTrigger.fillableObjects) do
165 if object.object == g_currentMission.controlledVehicle then
166 allowDisplay = true
167 end
168 end
169 end
170 end
171
172 if allowDisplay then
173 local fillLevel, capacity = self:getChargeState()
174 local fillLevelToFill = capacity - fillLevel
175 local literPerSecond = spec.loadTrigger.fillLitersPerMS * 1000
176 local seconds = fillLevelToFill / literPerSecond
177 if seconds >= 1 then
178 local minutes = math.floor(seconds / 60)
179 seconds = seconds - (minutes * 60)
180
181 g_currentMission:addExtraPrintText(string.format(g_i18n:getText("info_chargeTime"), minutes, seconds, fillLevel / capacity * 100))
182 end
183 end
184 end
185 end
186
187 self:raiseActive()
188end

prerequisitesPresent

Description
Checks if all prerequisite specializations are loaded
Definition
prerequisitesPresent(table specializations)
Arguments
tablespecializationsspecializations
Return Values
booleanhasPrerequisitetrue if all prerequisite specializations are loaded
Code
18function PlaceableChargingStation.prerequisitesPresent(specializations)
19 return SpecializationUtil.hasSpecialization(PlaceableBuyingStation, specializations)
20end

registerFunctions

Description
Definition
registerFunctions()
Code
24function PlaceableChargingStation.registerFunctions(placeableType)
25 SpecializationUtil.registerFunction(placeableType, "getChargeState", PlaceableChargingStation.getChargeState)
26end