Sprache Deutsch Language English

Script Dokumentation LS 2015 - ManureBarrel (Patch 1.3)

Script Dokumentation Übersicht

scripts/vehicles/specializations/ManureBarrel.lua

Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de
1--
2-- ManureBarrel
3--
4-- @author Stefan Geiger
5-- @date 10/06/11
6--
7-- Copyright (C) GIANTS Software GmbH, Confidential, All Rights Reserved.
8
9ManureBarrel = {};
10
11function ManureBarrel.prerequisitesPresent(specializations)
12 return SpecializationUtil.hasSpecialization(Sprayer, specializations);
13end;
14
15function ManureBarrel:load(xmlFile)
16
17 self.groundAdjustRaycastCallback = ManureBarrel.groundAdjustRaycastCallback;
18 self.getIsInWorkPosition = Utils.overwrittenFunction(self.getIsInWorkPosition, ManureBarrel.getIsInWorkPosition);
19 self.getCanBeTurnedOn = Utils.overwrittenFunction(self.getCanBeTurnedOn, ManureBarrel.getCanBeTurnedOn);
20 self.getIsTurnedOnAllowed = Utils.overwrittenFunction(self.getIsTurnedOnAllowed, ManureBarrel.getIsTurnedOnAllowed);
21 self.setIsTurnedOn = Utils.overwrittenFunction(self.setIsTurnedOn, ManureBarrel.setIsTurnedOn);
22 self.getIsReadyToSpray = Utils.overwrittenFunction(self.getIsReadyToSpray, ManureBarrel.getIsReadyToSpray);
23 self.getAreEffectsVisible = Utils.overwrittenFunction(self.getAreEffectsVisible, ManureBarrel.getAreEffectsVisible);
24 self.getIsWorkAreaActive = Utils.overwrittenFunction(self.getIsWorkAreaActive, ManureBarrel.getIsWorkAreaActive);
25
26 self.manureBarrelFoldMinLimit = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.manureBarrel#foldMinLimit"), 0);
27 self.manureBarrelFoldMaxLimit = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.manureBarrel#foldMaxLimit"), 1);
28
29
30 self.emptySprayValves = {};
31 self.lastSprayValveUpdateFoldTime = self.foldAnimTime;
32
33 self.attachToolAnimation = getXMLString(xmlFile, "vehicle.manureBarrel#toolAttachAnimName");
34
35 self.groundAdjustedNodes = {};
36 local i = 0;
37 while true do
38 local key = string.format("vehicle.groundAdjustedNodes.groundAdjustedNode(%d)", i);
39 if not hasXMLProperty(xmlFile, key) then
40 break;
41 end;
42 local node = Utils.indexToObject(self.components, getXMLString(xmlFile, key.."#index"));
43 if node ~= nil then
44
45 local x,y,z = getTranslation(node);
46 local raycastNodes = {};
47
48 local j = 0;
49 while true do
50 local raycastKey = key..string.format(".raycastNode(%d)", j);
51 if not hasXMLProperty(xmlFile, raycastKey) then
52 break;
53 end;
54 local raycastNode = Utils.indexToObject(self.components, getXMLString(xmlFile, raycastKey.."#index"));
55 if raycastNode ~= nil then
56 assert(getParent(node) == getParent(raycastNode));
57
58 local x1,y1,z1 = getTranslation(raycastNode);
59 table.insert(raycastNodes, {node=raycastNode, yDiff=y1-y});
60 end;
61 j = j+1;
62 end;
63 if table.getn(raycastNodes) > 0 then
64
65
66 local minY = Utils.getNoNil(getXMLFloat(xmlFile, key.."#minY"), y-1);
67 local maxY = Utils.getNoNil(getXMLFloat(xmlFile, key.."#maxY"), minY+1);
68 local yOffset = Utils.getNoNil(getXMLFloat(xmlFile, key.."#yOffset"), 0);
69 local moveSpeed = Utils.getNoNil(getXMLFloat(xmlFile, key.."#moveSpeed"), 1)/1000;
70
71 local adjustedNode = {node=node, raycastNodes=raycastNodes, x=x, y=y, z=z, curY=y, targetY=y, minY=minY, maxY=maxY, yOffset=yOffset, moveSpeed=moveSpeed}
72
73 adjustedNode.foldMinLimit = Utils.getNoNil(getXMLFloat(xmlFile, key.."#foldMinLimit"), 0);
74 adjustedNode.foldMaxLimit = Utils.getNoNil(getXMLFloat(xmlFile, key.."#foldMaxLimit"), 1);
75
76 local translationNode = Utils.indexToObject(self.components, getXMLString(xmlFile, key.."#translationNode"));
77 if translationNode ~= nil then
78 local tx, ty, tz = getWorldTranslation(translationNode);
79 local tx, ty, tz = worldToLocal(node, tx, ty, tz);
80
81 adjustedNode.translationNode=translationNode;
82 adjustedNode.tx=tx;
83 adjustedNode.ty=ty;
84 adjustedNode.tz=tz;
85 end;
86 table.insert(self.groundAdjustedNodes, adjustedNode);
87 end;
88 end;
89 i = i + 1;
90 end;
91
92end;
93
94function ManureBarrel:delete()
95end;
96
97function ManureBarrel:mouseEvent(posX, posY, isDown, isUp, button)
98end;
99
100function ManureBarrel:keyEvent(unicode, sym, modifier, isDown)
101end;
102
103function ManureBarrel:update(dt)
104 if self:getIsActive() then
105 if self.isClient then
106 for _, node in pairs(self.groundAdjustedNodes) do
107 if node.targetY ~= node.curY then
108 if node.targetY > node.curY then
109 node.curY = math.min(node.curY + node.moveSpeed*dt, node.targetY);
110 else
111 node.curY = math.max(node.curY - node.moveSpeed*dt, node.targetY);
112 end;
113 setTranslation(node.node, node.x, node.curY, node.z);
114
115
116 if node.translationNode ~= nil then
117 local tx, ty, tz = localToWorld(node.node, node.tx, node.ty, node.tz);
118 local tx, ty, tz = worldToLocal(getParent(node.translationNode), tx, ty, tz);
119 setTranslation(node.translationNode, tx, ty, tz);
120 end;
121 end;
122 end;
123 end;
124 end;
125end;
126
127function ManureBarrel:updateTick(dt)
128 if self:getIsActive() then
129
130 if self.isClient then
131
132 local foldAnimTime = self.foldAnimTime;
133
134 if self.isTurnedOn then
135 if foldAnimTime ~= nil and foldAnimTime ~= self.lastSprayValveUpdateFoldTime then
136 self.lastSprayValveUpdateFoldTime = foldAnimTime;
137 for k,sprayValve in pairs(self.sprayValves) do
138 Utils.setEmittingState(sprayValve.particleSystems, foldAnimTime <= sprayValve.foldMaxLimit and foldAnimTime >= sprayValve.foldMinLimit);
139 end;
140 end;
141 end;
142
143 for _, node in pairs(self.groundAdjustedNodes) do
144 if foldAnimTime == nil or (foldAnimTime <= node.foldMaxLimit and foldAnimTime >= node.foldMinLimit) then
145
146 local newY = node.minY;
147
148 for _, raycastNode in pairs(node.raycastNodes) do
149 local x,y,z = getWorldTranslation(raycastNode.node);
150 local dx,dy,dz = localDirectionToWorld(raycastNode.node, 0, -1, 0);
151
152 self.lastRaycastDistance = 0;
153 raycastClosest(x, y, z, dx, dy, dz, "groundAdjustRaycastCallback", 4, self);
154 if self.lastRaycastDistance ~= 0 then
155 newY = math.max(newY, node.y - self.lastRaycastDistance + raycastNode.yDiff - node.yOffset);
156 end;
157 end;
158 newY = Utils.clamp(newY, node.minY, node.maxY);
159 node.targetY = newY;
160 _, node.curY, _ = getTranslation(node.node);
161 end;
162 end;
163 end;
164 end;
165end;
166
167function ManureBarrel:draw()
168end;
169
170function ManureBarrel:attachImplement(implement)
171 local object = implement.object;
172 if object.attacherJoint.jointType == Vehicle.JOINTTYPE_MANUREBARREL then
173 self.attachedTool = object;
174 if self.attachToolAnimation ~= nil and self.playAnimation ~= nil then
175 self:playAnimation(self.attachToolAnimation, 1, self:getAnimationTime(self.attachToolAnimation), true);
176 end;
177 if self.attachedTool.aiLeftMarker ~= nil and self.attachedTool.aiRightMarker ~= nil and self.attachedTool.aiBackMarker ~= nil then
178 self.aiLeftMarkerBackup, self.aiRightMarkerBackup, self.aiBackMarkerBackup = self.aiLeftMarker, self.aiRightMarker, self.aiBackMarker;
179 self.aiLeftMarker, self.aiRightMarker, self.aiBackMarker = self.attachedTool.aiLeftMarker, self.attachedTool.aiRightMarker, self.attachedTool.aiBackMarker;
180 end;
181 end;
182end;
183
184function ManureBarrel:detachImplement(implementIndex)
185 local object = self.attachedImplements[implementIndex].object;
186 if object ~= nil then
187 if object.attacherJoint.jointType == Vehicle.JOINTTYPE_MANUREBARREL then
188 if self.attachToolAnimation ~= nil and self.playAnimation ~= nil then
189 self:playAnimation(self.attachToolAnimation, -1, self:getAnimationTime(self.attachToolAnimation), true);
190 end;
191 if self.attachedTool.aiLeftMarker ~= nil and self.attachedTool.aiRightMarker ~= nil and self.attachedTool.aiBackMarker ~= nil then
192 self.aiLeftMarker, self.aiRightMarker, self.aiBackMarker = self.aiLeftMarkerBackup, self.aiRightMarkerBackup, self.aiBackMarkerBackup;
193 end;
194 self.attachedTool = nil;
195 end;
196 end;
197end;
198
199function ManureBarrel:getIsReadyToSpray(superFunc)
200 if self.attachedTool ~= nil then
201 return false;
202 end;
203
204 if superFunc ~= nil then
205 superFunc(self);
206 end;
207 return true;
208end;
209
210function ManureBarrel:getAreEffectsVisible(superFunc)
211 if self.attachedTool ~= nil then
212 return false;
213 end;
214
215 if superFunc ~= nil then
216 return superFunc(self);
217 end;
218 return true;
219end;
220
221function ManureBarrel:getIsWorkAreaActive(superFunc, workArea)
222 if self.attachedTool ~= nil then
223 return false;
224 end;
225
226 if superFunc ~= nil then
227 return superFunc(self, workArea);
228 end;
229 return true;
230end;
231
232function ManureBarrel:getLitersPerSecond(superFunc, fillType)
233 if self.attachedTool ~= nil and self.attachedTool.getLitersPerSecond ~= nil then
234 return self.attachedTool:getLitersPerSecond(fillType);
235 end;
236
237 if superFunc ~= nil then
238 return superFunc(self, fillType);
239 end;
240 return self.defaultSprayLitersPerSecond;
241end;
242
243function ManureBarrel:getIsInWorkPosition()
244 if self.attachedTool == nil then
245 if foldAnimTime ~= nil and (foldAnimTime > self.manureBarrelFoldMaxLimit or foldAnimTime < self.manureBarrelFoldMinLimit) then
246 return false;
247 end;
248 else
249 --return self.attachedTool:getIsInWorkPosition();
250 if self.attachedTool:getIsInWorkPosition() then
251 local implement = self:getImplementByObject(self.attachedTool);
252 if implement ~= nil then
253 local jointDesc = self.attacherJoints[implement.jointDescIndex];
254 if jointDesc.allowsLowering and self.attachedTool.aiNeedsLowering then
255 if jointDesc.moveAlpha == jointDesc.lowerAlpha then
256 return true;
257 else
258 self:setJointMoveDown(implement.jointDescIndex, true, true);
259 return false;
260 end
261 end;
262 end;
263 else
264 return false;
265 end;
266 end;
267
268 if superFunc ~= nil then
269 return superFunc(self, isTurnedOn);
270 end
271
272 return true;
273end;
274
275function ManureBarrel:getCanBeTurnedOn(superFunc)
276 if self.attachedTool ~= nil then
277 if self.attachedTool.isAlwaysTurnedOn and self.attachedTool.needsTankActivation and self.attachedTool.activateTankOnLowering then
278 return false;
279 end;
280 end;
281
282 if superFunc ~= nil then
283 return superFunc(self);
284 end
285 return true;
286end;
287
288function ManureBarrel:getIsTurnedOnAllowed(superFunc, isTurnedOn)
289 if isTurnedOn then
290 if self.attachedTool == nil then
291 local foldAnimTime = self.foldAnimTime;
292 if foldAnimTime ~= nil and (foldAnimTime > self.manureBarrelFoldMaxLimit or foldAnimTime < self.manureBarrelFoldMinLimit) then
293 return false;
294 end;
295 else
296 if self.attachedTool:getIsInWorkPosition() then
297 local implement = self:getImplementByObject(self.attachedTool);
298 if implement ~= nil then
299 local jointDesc = self.attacherJoints[implement.jointDescIndex];
300 if jointDesc.allowsLowering and self.attachedTool.aiNeedsLowering then
301 return jointDesc.moveDown;
302 end;
303 end;
304 else
305 return false;
306 end;
307 end;
308 end;
309
310 if superFunc ~= nil then
311 return superFunc(self, isTurnedOn);
312 end
313
314 return true;
315end;
316
317function ManureBarrel:setIsTurnedOn(superFunc, isTurnedOn, noEventSend)
318 local sprayValves = self.sprayValves;
319 if isTurnedOn and self.foldAnimTime ~= nil then
320 -- do not turn on valves by setIsTurnedOn
321 self.sprayValves = self.emptySprayValves;
322 self.lastSprayValveUpdateFoldTime = -100;
323 end;
324 superFunc(self, isTurnedOn, noEventSend);
325
326 self.sprayValves = sprayValves;
327end;
328
329function ManureBarrel:aiTurnOn()
330 if self.attachedTool ~= nil then
331 self.attachedTool:aiTurnOn();
332 end;
333end;
334
335function ManureBarrel:aiTurnOff()
336 if self.attachedTool ~= nil then
337 self.attachedTool:aiTurnOff();
338 end;
339end;
340
341function ManureBarrel:aiLower()
342 if self.attachedTool ~= nil then
343 local implement = self:getImplementByObject(self.attachedTool);
344 if implement ~= nil then
345 local jointDesc = self.attacherJoints[implement.jointDescIndex];
346 if jointDesc.allowsLowering and self.attachedTool.aiNeedsLowering then
347 if jointDesc.moveAlpha ~= jointDesc.lowerAlpha then
348 self:setJointMoveDown(implement.jointDescIndex, true, true);
349 end;
350 end;
351 end;
352 end;
353end;
354
355function ManureBarrel:aiRaise()
356 if self.attachedTool ~= nil then
357 local implement = self:getImplementByObject(self.attachedTool);
358 if implement ~= nil then
359 local jointDesc = self.attacherJoints[implement.jointDescIndex];
360 if jointDesc.allowsLowering and self.attachedTool.aiNeedsLowering then
361 self:setJointMoveDown(implement.jointDescIndex, false, true);
362 end;
363 end;
364 end;
365end;
366
367function ManureBarrel:groundAdjustRaycastCallback(transformId, x, y, z, distance)
368 self.lastRaycastDistance = distance;
369end;
Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de
Script Dokumentation Übersicht