LUADOC - Farming Simulator 17

Printable Version

Script v1.4.4.0

Engine v7.0.0.2

Foundation Reference

TreeSaw

Description
This is the specialization for all tree saws
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
17function TreeSaw.prerequisitesPresent(specializations)
18 return SpecializationUtil.hasSpecialization(TurnOnVehicle, specializations)
19end

load

Description
Called on loading
Definition
load(table savegame)
Arguments
tablesavegamesavegame
Code
24function TreeSaw:load(savegame)
25 self.treeSaw = {}
26
27 if self.isClient then
28 self.treeSaw.treeSawTurnedOnRotationNodes = Utils.loadRotationNodes(self.xmlFile, {}, "vehicle.turnedOnRotationNodes.turnedOnRotationNode", "treeSaw", self.components)
29
30 self.treeSaw.cutParticleSystems = {}
31
32 local i = 0
33 while true do
34 local keyPS = string.format("vehicle.treeSaw.cutParticleSystems.emitterShape(%d)", i)
35 if not hasXMLProperty(self.xmlFile, keyPS) then
36 break
37 end
38 local emitterShape = Utils.indexToObject(self.components, getXMLString(self.xmlFile, keyPS.."#node"))
39 local particleType = getXMLString(self.xmlFile, keyPS.."#particleType")
40 if emitterShape ~= nil then
41 local fillType = FillUtil.FILLTYPE_WOODCHIPS
42 local particleSystem = MaterialUtil.getParticleSystem(fillType, particleType)
43 if particleSystem ~= nil then
44 table.insert(self.treeSaw.cutParticleSystems, ParticleUtil.copyParticleSystem(self.xmlFile, keyPS, particleSystem, emitterShape))
45 end
46 end
47 i = i + 1
48 end
49
50 if #self.treeSaw.cutParticleSystems == 0 then
51 self.treeSaw.cutParticleSystems = nil
52 end
53
54 self.treeSaw.sampleCut = SoundUtil.loadSample(self.xmlFile, {}, "vehicle.treeSaw.cutSound", nil, self.baseDirectory)
55 self.treeSaw.sampleSaw = SoundUtil.loadSample(self.xmlFile, {}, "vehicle.treeSaw.sawSound", nil, self.baseDirectory)
56 end
57
58 self.treeSaw.cutNode = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.treeSaw.cutNode#node"))
59 self.treeSaw.cutSizeY = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.treeSaw.cutNode#sizeY"), 1)
60 self.treeSaw.cutSizeZ = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.treeSaw.cutNode#sizeZ"), 1)
61 self.treeSaw.lengthAboveThreshold = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.treeSaw.cutNode#lengthAboveThreshold"), 0.3)
62 self.treeSaw.lengthBelowThreshold = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.treeSaw.cutNode#lengthBelowThreshold"), 0.3)
63 self.treeSaw.cutTimerDuration = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.treeSaw.cutNode#timer"), 1)*1000
64 self.treeSaw.curSplitShape = nil
65 self.treeSaw.cutTimer = -1
66 self.treeSaw.isCutting = false
67end

delete

Description
Called on deleting
Definition
delete()
Code
71function TreeSaw:delete()
72 if self.isClient then
73 ParticleUtil.deleteParticleSystems(self.treeSaw.cutParticleSystems)
74 SoundUtil.deleteSample(self.treeSaw.sampleCut)
75 SoundUtil.deleteSample(self.treeSaw.sampleSaw)
76 end
77end

readUpdateStream

Description
Called on on update
Definition
readUpdateStream(integer streamId, integer timestamp, table connection)
Arguments
integerstreamIdstream ID
integertimestamptimestamp
tableconnectionconnection
Code
90function TreeSaw:readUpdateStream(streamId, timestamp, connection)
91 if connection:getIsServer() then
92 self.treeSaw.isCutting = streamReadBool(streamId)
93 end
94end

writeUpdateStream

Description
Called on on update
Definition
writeUpdateStream(integer streamId, table connection, integer dirtyMask)
Arguments
integerstreamIdstream ID
tableconnectionconnection
integerdirtyMaskdirty mask
Code
101function TreeSaw:writeUpdateStream(streamId, connection, dirtyMask)
102 if not connection:getIsServer() then
103 streamWriteBool(streamId, self.treeSaw.isCutting)
104 end
105end

update

Description
Called on update
Definition
update(float dt)
Arguments
floatdttime since last call in ms
Code
122function TreeSaw:update(dt)
123 if self:getIsActive() then
124
125 -- Verify that the split shapes still exist (possible that someone has cut them)
126 if self.isServer then
127 if self.treeSaw.curSplitShape ~= nil then
128 if not entityExists(self.treeSaw.curSplitShape) then
129 self.treeSaw.curSplitShape = nil
130 if g_server ~= nil then
131 self.treeSaw.cutTimer = -1
132 end
133 end
134 end
135 end
136
137 if self.isServer then
138 self.treeSaw.isCutting = self.treeSaw.curSplitShape ~= nil
139 if self.treeSaw.curSplitShape ~= nil then
140 if self.treeSaw.cutTimer > 0 then
141 self.treeSaw.cutTimer = math.max(self.treeSaw.cutTimer - dt, 0)
142 end
143
144 -- cut
145 if self.treeSaw.cutTimer == 0 then
146 self.treeSaw.cutTimer = -1
147
148 local x,y,z = getWorldTranslation(self.treeSaw.cutNode)
149 local nx,ny,nz = localDirectionToWorld(self.treeSaw.cutNode, 1,0,0)
150 local yx,yy,yz = localDirectionToWorld(self.treeSaw.cutNode, 0,1,0)
151
152 ChainsawUtil.cutSplitShape(self.treeSaw.curSplitShape, x,y,z, nx,ny,nz, yx,yy,yz, self.treeSaw.cutSizeY, self.treeSaw.cutSizeZ)
153 self.treeSaw.curSplitShape = nil
154 end
155 end
156 end
157 end
158
159 -- PS and sound for cut
160 if self.isClient then
161 if self:getIsActiveForSound() then
162 if self:getIsTurnedOn() then
163 SoundUtil.playSample(self.treeSaw.sampleSaw, 0, 0, nil)
164 end
165 if self.treeSaw.cutTimer > 0 then
166 SoundUtil.playSample(self.treeSaw.sampleCut, 0, 0, nil)
167 else
168 SoundUtil.stopSample(self.treeSaw.sampleCut)
169 end
170 else
171 SoundUtil.stopSample(self.treeSaw.sampleCut)
172 end
173
174 if self.treeSaw.cutParticleSystems ~= nil then
175 if self.treeSaw.cutTimer > 0 and not self.treeSaw.cutParticleSystemsActive then
176 self.treeSaw.cutParticleSystemsActive = true
177 for _,ps in pairs(self.treeSaw.cutParticleSystems) do
178 ParticleUtil.setEmittingState(ps, true)
179 end
180 elseif self.treeSaw.cutTimer <= 0 and self.treeSaw.cutParticleSystemsActive then
181 self.treeSaw.cutParticleSystemsActive = false
182 for _,ps in pairs(self.treeSaw.cutParticleSystems) do
183 ParticleUtil.setEmittingState(ps, false)
184 end
185 end
186 end
187 end
188
189 if self.isClient then
190 Utils.updateRotationNodes(self, self.treeSaw.treeSawTurnedOnRotationNodes, dt, self:getIsActive() and self:getIsTurnedOn())
191 end
192end

updateTick

Description
Called on update tick
Definition
updateTick(float dt)
Arguments
floatdttime since last call in ms
Code
197function TreeSaw:updateTick(dt)
198 if self:getIsActive() then
199
200 if self:getIsTurnedOn() then
201 if self.treeSaw.cutNode ~= nil then
202 local x,y,z = getWorldTranslation(self.treeSaw.cutNode)
203 local nx,ny,nz = localDirectionToWorld(self.treeSaw.cutNode, 1,0,0)
204 local yx,yy,yz = localDirectionToWorld(self.treeSaw.cutNode, 0,1,0)
205
206 if self.treeSaw.curSplitShape == nil then
207 local shape, _, _, _, _ = findSplitShape(x,y,z, nx,ny,nz, yx,yy,yz, self.treeSaw.cutSizeY, self.treeSaw.cutSizeZ)
208 if shape ~= 0 then
209 self.treeSaw.curSplitShape = shape
210 self.treeSaw.cutTimer = self.treeSaw.cutTimerDuration
211 end
212 end
213
214 if self.treeSaw.curSplitShape ~= nil then
215 local minY,maxY, minZ,maxZ = testSplitShape(self.treeSaw.curSplitShape, x,y,z, nx,ny,nz, yx,yy,yz, self.treeSaw.cutSizeY, self.treeSaw.cutSizeZ)
216 if minY == nil then
217 self.treeSaw.curSplitShape = nil
218 else
219 -- check if cut would be below y=0 (tree CoSy)
220 local cutTooLow = false
221 local _,y,_ = localToLocal(self.treeSaw.cutNode, self.treeSaw.curSplitShape, 0,minY,minZ)
222 cutTooLow = cutTooLow or y < 0.01
223 local _,y,_ = localToLocal(self.treeSaw.cutNode, self.treeSaw.curSplitShape, 0,minY,maxZ)
224 cutTooLow = cutTooLow or y < 0.01
225 local _,y,_ = localToLocal(self.treeSaw.cutNode, self.treeSaw.curSplitShape, 0,maxY,minZ)
226 cutTooLow = cutTooLow or y < 0.01
227 local _,y,_ = localToLocal(self.treeSaw.cutNode, self.treeSaw.curSplitShape, 0,maxY,maxZ)
228 cutTooLow = cutTooLow or y < 0.01
229 if cutTooLow then
230 self.treeSaw.curSplitShape = nil
231 end
232 end
233 end
234
235 if self.treeSaw.curSplitShape ~= nil then
236 local lenBelow, lenAbove = getSplitShapePlaneExtents(self.treeSaw.curSplitShape, x,y,z, nx,ny,nz)
237 if lenAbove < self.treeSaw.lengthAboveThreshold or lenBelow < self.treeSaw.lengthBelowThreshold then
238 self.treeSaw.curSplitShape = nil
239 end
240 end
241
242 if self.treeSaw.curSplitShape == nil and self.treeSaw.cutTimer > -1 then
243 self.treeSaw.cutTimer = -1
244 end
245 end
246 end
247 end
248end

draw

Description
Called on draw
Definition
draw()
Code
252function TreeSaw:draw()
253 if self:getIsActive() and self:getIsTurnedOn() then
254 if self.treeSaw.isCutting then
255 g_currentMission:addExtraPrintText(g_i18n:getText("info_cutting"))
256 end
257 end
258end

onDeactivate

Description
Called on deactivate
Definition
onDeactivate()
Code
262function TreeSaw:onDeactivate()
263 self.treeSaw.curSplitShape = nil
264 self.treeSaw.cutTimer = -1
265end

onDeactivateSounds

Description
Called on deactivating sounds
Definition
onDeactivateSounds()
Code
269function TreeSaw:onDeactivateSounds()
270 if self.isClient then
271 self.treeSaw.cutParticleSystemsActive = false
272 if self.treeSaw.cutParticleSystems ~= nil then
273 for _,ps in pairs(self.treeSaw.cutParticleSystems) do
274 ParticleUtil.setEmittingState(ps, false)
275 end
276 end
277
278 SoundUtil.stopSample(self.treeSaw.sampleCut)
279 SoundUtil.stopSample(self.treeSaw.sampleSaw)
280 end
281end

onTurnedOff

Description
Called on turn off
Definition
onTurnedOff(boolean noEventSend)
Arguments
booleannoEventSendno event send
Code
286function TreeSaw:onTurnedOff(noEventSend)
287 self.treeSaw.curSplitShape = nil
288 self.treeSaw.cutTimer = -1
289 SoundUtil.stopSample(self.treeSaw.sampleSaw)
290end