LUADOC - Farming Simulator 17

Printable Version

Script v1.4.4.0

Engine v7.0.0.2

Foundation Reference

StrawBlower

Description
Class for all sprayers
Functions

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
19function StrawBlower.prerequisitesPresent(specializations)
20 return SpecializationUtil.hasSpecialization(Fillable, specializations) and SpecializationUtil.hasSpecialization(Trailer, specializations);
21end

load

Description
Called on loading
Definition
load(table savegame)
Arguments
tablesavegamesavegame
Code
26function StrawBlower:load(savegame)
27
28 self.setUnitFillLevel = Utils.overwrittenFunction(self.setUnitFillLevel, StrawBlower.setUnitFillLevel);
29 self.strawBlowerBaleTriggerCallback = StrawBlower.strawBlowerBaleTriggerCallback;
30 self.setIsStrawBlowerDoorOpen = StrawBlower.setIsStrawBlowerDoorOpen;
31
32
33 self.strawBlowerDoorAnimation = getXMLString(self.xmlFile, "vehicle.strawBlowerDoor#animationName");
34 self.strawBlowerDoorAnimationOpenSpeed = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.strawBlowerDoor#openSpeed"), 1);
35 self.strawBlowerDoorAnimationCloseSpeed = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.strawBlowerDoor#closeSpeed"), -self.strawBlowerDoorAnimationOpenSpeed);
36 self.isStrawBlowerDoorOpen = false;
37
38 self.triggeredBales = {};
39
40 if self.isServer then
41 self.strawBlowerTriggerId = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.strawBlowerBaleTrigger#index"));
42 if self.strawBlowerTriggerId ~= nil then
43 addTrigger(self.strawBlowerTriggerId, "strawBlowerBaleTriggerCallback", self);
44 end
45 end
46
47 self.samples = {};
48 local linkNode = Utils.indexToObject(self.components, Utils.getNoNil( getXMLString(self.xmlFile, "vehicle.strawBlowerSounds#linkNode"), "0>" ) );
49 self.samples.workStart = SoundUtil.loadSample(self.xmlFile, {}, "vehicle.strawBlowerSounds.startWork", nil, self.baseDirectory, linkNode);
50 self.samples.work = SoundUtil.loadSample(self.xmlFile, {}, "vehicle.strawBlowerSounds.work", nil, self.baseDirectory, linkNode);
51 self.samples.workStop = SoundUtil.loadSample(self.xmlFile, {}, "vehicle.strawBlowerSounds.stopWork", nil, self.baseDirectory, linkNode);
52
53 self.samples.work.startTime = -1;
54 self.samples.workStop.stopTime = -1;
55
56 if self.samples.workStart.sample ~= nil then
57 self.strawBlowerDoorAnimationOpenSpeed = self:getAnimationDuration(self.strawBlowerDoorAnimation) / self.samples.workStart.duration;
58 end
59
60 self.strawBlower = {};
61 self.strawBlower.fillUnitIndex = Utils.getNoNil(getXMLInt(self.xmlFile, "vehicle.strawBlower#fillUnitIndex"), 1);
62 self.strawBlower.unloadInfoIndex = Utils.getNoNil(getXMLInt(self.xmlFile, "vehicle.strawBlower#unloadInfoIndex"), 1);
63 self.strawBlower.loadInfoIndex = Utils.getNoNil(getXMLInt(self.xmlFile, "vehicle.strawBlower#loadInfoIndex"), 1);
64 self.strawBlower.dischargeInfoIndex = Utils.getNoNil(getXMLInt(self.xmlFile, "vehicle.strawBlower#dischargeInfoIndex"), 1);
65
66
67 -- we need to send the full fill level rather than percentages since we change the capacity
68 self.synchronizeFullFillLevel = true;
69
70 if savegame ~= nil and not savegame.resetVehicles then
71 if self.strawBlowerDoorAnimation ~= nil then
72 local isDoorOpen = getXMLBool(savegame.xmlFile, savegame.key.."#isStrawBlowerDoorOpen");
73 if isDoorOpen ~= nil then
74 self:setIsStrawBlowerDoorOpen(isDoorOpen, true);
75 if self.playAnimation ~= nil then
76 AnimatedVehicle.updateAnimationByName(self, self.strawBlowerDoorAnimation, 99999999);
77 if self.updateCylinderedInitial ~= nil then
78 self:updateCylinderedInitial(false);
79 end
80 end
81 end
82 end
83 self:setUnitFillLevel(self.strawBlower.fillUnitIndex, 0, FillUtil.FILLTYPE_UNKNOWN);
84 end
85end

delete

Description
Called on deleting
Definition
delete()
Code
89function StrawBlower:delete()
90 if self.isServer then
91 if self.strawBlowerTriggerId ~= nil then
92 removeTrigger(self.strawBlowerTriggerId);
93 end
94
95 for bale, _ in pairs(self.triggeredBales) do
96 if entityExists(bale.nodeId) then
97 Utils.wakeUpObject(bale.nodeId)
98 end
99 end
100
101
102 end
103 SoundUtil.deleteSample(self.samples.workStart);
104 SoundUtil.deleteSample(self.samples.work);
105 SoundUtil.deleteSample(self.samples.workStop);
106end

getSaveAttributesAndNodes

Description
Returns attributes and nodes to save
Definition
getSaveAttributesAndNodes(table nodeIdent)
Arguments
tablenodeIdentnode ident
Return Values
stringattributesattributes
stringnodesnodes
Code
113function StrawBlower:getSaveAttributesAndNodes(nodeIdent)
114 local attributes = "";
115 if self.strawBlowerDoorAnimation ~= nil then
116 attributes = attributes.. 'isStrawBlowerDoorOpen="'.. tostring(self.isStrawBlowerDoorOpen)..'"';
117 end
118 local nodes = "";
119 return attributes, nodes;
120end

readStream

Description
Called on client side on join
Definition
readStream(integer streamId, integer connection)
Arguments
integerstreamIdstreamId
integerconnectionconnection
Code
126function StrawBlower:readStream(streamId, connection)
127 if self.strawBlowerDoorAnimation ~= nil then
128 local isStrawBlowerDoorOpen = streamReadBool(streamId);
129 self:setIsStrawBlowerDoorOpen(isStrawBlowerDoorOpen, true);
130 end
131end

writeStream

Description
Called on server side on join
Definition
writeStream(integer streamId, integer connection)
Arguments
integerstreamIdstreamId
integerconnectionconnection
Code
137function StrawBlower:writeStream(streamId, connection)
138 if self.strawBlowerDoorAnimation ~= nil then
139 streamWriteBool(streamId, self.isStrawBlowerDoorOpen);
140 end
141end

update

Description
Called on update
Definition
update(float dt)
Arguments
floatdttime since last call in ms
Code
158function StrawBlower:update(dt)
159 if self.isActive then
160 if self:getIsActiveForInput() then
161 if self.strawBlowerDoorAnimation ~= nil then
162 if InputBinding.hasEvent(InputBinding.IMPLEMENT_EXTRA2) then
163 self:setIsStrawBlowerDoorOpen(not self.isStrawBlowerDoorOpen);
164 end
165 end
166 end
167 end
168 if self.isClient then
169 if self.samples.workStart.sound3D ~= nil and self.samples.work.sound3D ~= nil and self.samples.workStop.sound3D ~= nil then
170 if self.samples.work.startTime ~= -1 and self.samples.work.startTime <= g_currentMission.time then
171 SoundUtil.stop3DSample(self.samples.workStart);
172 SoundUtil.play3DSample(self.samples.work);
173 SoundUtil.stop3DSample(self.samples.workStop);
174 self.samples.work.startTime = -1;
175 end
176 if self.samples.workStop.stopTime ~= -1 and self.samples.workStop.stopTime < g_currentMission.time then
177 self.samples.workStop.stopTime = -1;
178 SoundUtil.stop3DSample(self.samples.workStop);
179 end
180 end
181 end
182end

updateTick

Description
Called on update tick
Definition
updateTick(float dt)
Arguments
floatdttime since last call in ms
Code
187function StrawBlower:updateTick(dt)
188 if self:getIsActive() then
189 if self.isServer then
190 if self.currentStrawBlowerBale == nil then
191 local bale = next(self.triggeredBales);
192 if bale ~= nil then
193 self:setUnitFillLevel(self.strawBlower.fillUnitIndex, bale:getFillLevel(), bale:getFillType());
194
195 self.currentStrawBlowerBale = bale;
196 end
197 end
198 end
199 end
200end

draw

Description
Called on draw
Definition
draw()
Code
204function StrawBlower:draw()
205 if self.strawBlowerDoorAnimation ~= nil then
206 if self:getIsActiveForInput(true) then
207 if self.isStrawBlowerDoorOpen then
208 g_currentMission:addHelpButtonText(g_i18n:getText("action_closeDoor"), InputBinding.IMPLEMENT_EXTRA2, nil, GS_PRIO_NORMAL);
209 else
210 g_currentMission:addHelpButtonText(g_i18n:getText("action_openDoor"), InputBinding.IMPLEMENT_EXTRA2, nil, GS_PRIO_NORMAL);
211 end
212 if self:getUnitFillLevel(self.strawBlower.fillUnitIndex) <= 0 and self:getUnitCapacity(self.strawBlower.fillUnitIndex) ~= 0 then
213 g_currentMission:addExtraPrintText(g_i18n:getText("info_firstFillTheTool"));
214 end
215 end
216 end
217end

setUnitFillLevel

Description
Set unit fill level
Definition
setUnitFillLevel(integer fillUnitIndex, float fillLevel, integer fillType, boolean force, table fillInfo)
Arguments
integerfillUnitIndexindex of fill unit
floatfillLevelnew fill level
integerfillTypefill type
booleanforceforce action
tablefillInfofill info for fill volume
Code
229function StrawBlower:setUnitFillLevel(superFunc, fillUnitIndex, fillLevel, fillType, force)
230 if fillUnitIndex == self.strawBlower.fillUnitIndex then
231 -- the capacity is always the fill level we have
232 self:setUnitCapacity(self.strawBlower.fillUnitIndex, math.max(self:getUnitCapacity(self.strawBlower.fillUnitIndex), fillLevel));
233
234 if self.isServer then
235 local bale = self.currentStrawBlowerBale;
236 if bale ~= nil then
237 if fillLevel <= 0 then
238 if self.pendingDynamicMountObjects[bale] ~= nil then
239 self.pendingDynamicMountObjects[bale] = nil;
240 end
241 if self.dynamicMountedObjects[bale] ~= nil then
242 self.dynamicMountedObjects[bale] = nil;
243 end
244 bale:delete();
245 self.currentStrawBlowerBale = nil;
246 self:setUnitCapacity(self.strawBlower.fillUnitIndex, 1);
247 self.triggeredBales[bale] = nil;
248 elseif fillLevel < bale:getFillLevel() and fillType == bale:getFillType() then
249 bale:setFillLevel(fillLevel);
250 end
251 end
252 else
253 if fillLevel <= 0 then
254 self:setUnitCapacity(self.strawBlower.fillUnitIndex, 1);
255 end
256 end
257 end
258 superFunc(self, fillUnitIndex, fillLevel, fillType, force);
259end

strawBlowerBaleTriggerCallback

Description
Trigger callback
Definition
strawBlowerBaleTriggerCallback(integer triggerId, integer otherActorId, boolean onEnter, boolean onLeave, boolean onStay, integer otherShapeId)
Arguments
integertriggerIdid of trigger
integerotherActorIdid of other actor
booleanonEnteron enter
booleanonLeaveon leave
booleanonStayon stay
integerotherShapeIdid of other shape
Code
269function StrawBlower:strawBlowerBaleTriggerCallback(triggerId, otherActorId, onEnter, onLeave, onStay, otherShapeId)
270 if onEnter then
271 -- this happens if a compound child of a deleted compound is entering
272 if otherActorId ~= 0 then
273 local object = g_currentMission:getNodeObject(otherActorId);
274 if object ~= nil then
275 if object:isa(Bale) then
276 if self:getUnitFillTypes(self.strawBlower.fillUnitIndex)[object:getFillType()] then
277 self.triggeredBales[object] = Utils.getNoNil(self.triggeredBales[object], 0) + 1;
278 end
279 end
280 end
281 end
282 elseif onLeave then
283 if otherActorId ~= 0 then
284 local object = g_currentMission:getNodeObject(otherActorId);
285 if object ~= nil then
286 local triggerCount = self.triggeredBales[object];
287 if triggerCount ~= nil then
288 if triggerCount == 1 then
289 self.triggeredBales[object] = nil;
290 if object == self.currentStrawBlowerBale then
291 self.currentStrawBlowerBale = nil;
292 self:setUnitFillLevel(self.strawBlower.fillUnitIndex, 0, FillUtil.FILLTYPE_UNKNOWN, true);
293 end
294 else
295 self.triggeredBales[object] = triggerCount-1;
296 end
297 end
298 end
299 end
300 end
301end

setIsStrawBlowerDoorOpen

Description
Set straw blower door open state
Definition
setIsStrawBlowerDoorOpen(boolean isStrawBlowerDoorOpen, boolean noEventSend)
Arguments
booleanisStrawBlowerDoorOpenis straw blower door open
booleannoEventSendno event send
Code
307function StrawBlower:setIsStrawBlowerDoorOpen(isStrawBlowerDoorOpen, noEventSend)
308 StrawBlowerDoorOpenEvent.sendEvent(self, isStrawBlowerDoorOpen, noEventSend)
309 self.isStrawBlowerDoorOpen = isStrawBlowerDoorOpen;
310
311 if self.strawBlowerDoorAnimation ~= nil and self.playAnimation ~= nil then
312 local curTime = self:getAnimationTime(self.strawBlowerDoorAnimation);
313 if self.isStrawBlowerDoorOpen then
314 self:playAnimation(self.strawBlowerDoorAnimation, self.strawBlowerDoorAnimationOpenSpeed, curTime, true);
315 else
316 self:playAnimation(self.strawBlowerDoorAnimation, self.strawBlowerDoorAnimationCloseSpeed, curTime, true);
317 end
318 end
319end

onStartTip

Description
Called on start tipping
Definition
onStartTip(table tipTrigger, integer tipReferencePointIndex, boolean noEventSend)
Arguments
tabletipTriggertip trigger
integertipReferencePointIndexindex of tip reference point
booleannoEventSendno event send
Code
326function StrawBlower:onStartTip(tipTrigger, tipReferencePointIndex, noEventSend)
327 if self.samples.workStart.sound3D ~= nil and self.samples.work.sound3D ~= nil and self.samples.workStop.sound3D ~= nil then
328 SoundUtil.play3DSample(self.samples.workStart);
329 SoundUtil.stop3DSample(self.samples.work);
330 SoundUtil.stop3DSample(self.samples.workStop);
331 self.samples.work.startTime = g_currentMission.time + self.samples.workStart.duration;
332 end
333end

onEndTip

Description
Called at the end of tipping
Definition
onEndTip(boolean noEventSend)
Arguments
booleannoEventSendno event send
Code
338function StrawBlower:onEndTip(noEventSend)
339 if self.samples.workStart.sound3D ~= nil and self.samples.work.sound3D ~= nil and self.samples.workStop.sound3D ~= nil then
340 SoundUtil.stop3DSample(self.samples.workStart);
341 SoundUtil.stop3DSample(self.samples.work);
342 SoundUtil.play3DSample(self.samples.workStop);
343 self.samples.work.startTime = -1;
344 self.samples.workStop.stopTime = g_currentMission.time + self.samples.workStop.duration;
345 end
346end