Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de
1 | -- |
2 | -- Tedder |
3 | -- Class for all Tedders |
4 | -- Note: the rotor animation is not perfectly synchronized, thus do not attach any gameplay relevant stuff to the rotor animation |
5 | -- |
6 | -- @author Stefan Geiger |
7 | -- @date 11/05/08 |
8 | -- |
9 | -- Copyright (C) GIANTS Software GmbH, Confidential, All Rights Reserved. |
10 | |
11 | Tedder = {}; |
12 | source("dataS/scripts/vehicles/specializations/SetTurnedOnEvent.lua"); |
13 | source("dataS/scripts/vehicles/specializations/TedderAreaEvent.lua"); |
14 | |
15 | function Tedder.initSpecialization() |
16 | WorkArea.registerAreaType("tedder"); |
17 | WorkArea.registerAreaType("tedderDrop"); |
18 | end; |
19 | |
20 | function Tedder.prerequisitesPresent(specializations) |
21 | return SpecializationUtil.hasSpecialization(WorkArea, specializations) and SpecializationUtil.hasSpecialization(TurnOnVehicle, specializations); |
22 | end; |
23 | |
24 | function Tedder:preLoad(xmlFile) |
25 | self.loadWorkAreaFromXML = Utils.overwrittenFunction(self.loadWorkAreaFromXML, Tedder.loadWorkAreaFromXML); |
26 | end |
27 | |
28 | function Tedder:load(xmlFile) |
29 | |
30 | self.getDirtMultiplier = Utils.overwrittenFunction(self.getDirtMultiplier, Tedder.getDirtMultiplier); |
31 | self.doCheckSpeedLimit = Utils.overwrittenFunction(self.doCheckSpeedLimit, Tedder.doCheckSpeedLimit); |
32 | |
33 | self.tedderTurnedOnRotationNodes = Utils.loadRotationNodes(xmlFile, {}, "vehicle.turnedOnRotationNodes.turnedOnRotationNode", "tedder", self.components); |
34 | |
35 | if hasXMLProperty(xmlFile, "vehicle.rotors") then |
36 | print("Warning: vehicle.rotors are not supported anymore, use vehicle.turnedOnRotationNodes.turnedOnRotationNode (type: tedder) instead"); |
37 | end; |
38 | |
39 | local numTedderDropAreas = table.getn(self:getTypedWorkAreas(WorkArea.AREATYPE_TEDDERDROP)); |
40 | if numTedderDropAreas == 0 then |
41 | print("Warning: No drop work areas specified in '"..self.configFileName.."'"); |
42 | else |
43 | if numTedderDropAreas ~= 1 and numTedderDropAreas ~= table.getn(self:getTypedWorkAreas(WorkArea.AREATYPE_TEDDER)) then |
44 | print("Warning: Number of work areas and drop work areas should be equal in '"..self.configFileName.."'"); |
45 | end; |
46 | end; |
47 | |
48 | self.grassParticleSystems = {}; |
49 | local i=0; |
50 | while true do |
51 | local baseName = string.format("vehicle.grassParticleSystems.grassParticleSystem(%d)", i); |
52 | |
53 | local particleSystem = {}; |
54 | particleSystem.ps = {}; |
55 | local ps = Utils.loadParticleSystem(xmlFile, particleSystem.ps, baseName, self.components, false, nil, self.baseDirectory) |
56 | if ps == nil then |
57 | break; |
58 | end; |
59 | particleSystem.disableTime = 0; |
60 | particleSystem.isEnabled = false; |
61 | table.insert(self.grassParticleSystems, particleSystem); |
62 | i = i+1; |
63 | end; |
64 | |
65 | if self.isClient then |
66 | self.sampleTedder = Utils.loadSample(xmlFile, {}, "vehicle.tedderSound", nil, self.baseDirectory); |
67 | end; |
68 | |
69 | self.isTedderSpeedLimitActive = false; |
70 | |
71 | self.tedderParticleSystemFlag = self:getNextDirtyFlag(); |
72 | end; |
73 | |
74 | function Tedder:delete() |
75 | |
76 | for k,v in pairs(self.grassParticleSystems) do |
77 | Utils.deleteParticleSystem(v.ps); |
78 | end; |
79 | |
80 | if self.isClient then |
81 | Utils.deleteSample(self.sampleTedder); |
82 | end; |
83 | end; |
84 | |
85 | function Tedder:readStream(streamId, connection) |
86 | for k,v in ipairs(self.grassParticleSystems) do |
87 | local enabled = streamReadBool(streamId); |
88 | Utils.setEmittingState(v.ps, enabled); |
89 | end; |
90 | end; |
91 | |
92 | function Tedder:writeStream(streamId, connection) |
93 | for k,v in ipairs(self.grassParticleSystems) do |
94 | streamWriteBool(streamId, v.isEnabled); |
95 | end; |
96 | end; |
97 | |
98 | function Tedder:readUpdateStream(streamId, timestamp, connection) |
99 | if connection:getIsServer() then |
100 | local hasUpdate = streamReadBool(streamId); |
101 | if hasUpdate then |
102 | for k,v in ipairs(self.grassParticleSystems) do |
103 | local enabled = streamReadBool(streamId); |
104 | Utils.setEmittingState(v.ps, enabled); |
105 | end; |
106 | end; |
107 | end; |
108 | end; |
109 | |
110 | function Tedder:writeUpdateStream(streamId, connection, dirtyMask) |
111 | if not connection:getIsServer() then |
112 | if bitAND(dirtyMask, self.tedderParticleSystemFlag) ~= 0 then |
113 | streamWriteBool(streamId, true); |
114 | for k,v in ipairs(self.grassParticleSystems) do |
115 | streamWriteBool(streamId, v.isEnabled); |
116 | end; |
117 | else |
118 | streamWriteBool(streamId, false); |
119 | end; |
120 | end; |
121 | end; |
122 | |
123 | function Tedder:mouseEvent(posX, posY, isDown, isUp, button) |
124 | end; |
125 | |
126 | function Tedder:keyEvent(unicode, sym, modifier, isDown) |
127 | end; |
128 | |
129 | function Tedder:update(dt) |
130 | if self.isClient then |
131 | Utils.updateRotationNodes(self, self.tedderTurnedOnRotationNodes, dt, self:getIsActive() and self:getIsTurnedOn()); |
132 | end; |
133 | end; |
134 | |
135 | function Tedder:updateTick(dt) |
136 | self.isTedderSpeedLimitActive = false; |
137 | self.isWorking = false; |
138 | if self:getIsActive() then |
139 | local hasGroundContact, typedWorkAreas = self:getIsTypedWorkAreaActive(WorkArea.AREATYPE_TEDDER); |
140 | self.hasGroundContact = hasGroundContact; |
141 | |
142 | if self.hasGroundContact then |
143 | local foldAnimTime = self.foldAnimTime; |
144 | if self:getIsTurnedOn() then |
145 | self.isTedderSpeedLimitActive = true; |
146 | if self.isServer then |
147 | local workAreasSend = {}; |
148 | local typedWorkAreasDrop = self:getTypedWorkAreas(WorkArea.AREATYPE_TEDDERDROP); |
149 | for k, workArea in pairs(typedWorkAreas) do |
150 | if self:getIsWorkAreaActive(workArea) then |
151 | local x,y,z = getWorldTranslation(workArea.start); |
152 | local x1,y1,z1 = getWorldTranslation(workArea.width); |
153 | local x2,y2,z2 = getWorldTranslation(workArea.height); |
154 | |
155 | local dropArea = typedWorkAreasDrop[workArea.dropAreaIndex]; |
156 | local dx,dy,dz = getWorldTranslation(dropArea.start); |
157 | local dx1,dy1,dz1 = getWorldTranslation(dropArea.width); |
158 | local dx2,dy2,dz2 = getWorldTranslation(dropArea.height); |
159 | |
160 | table.insert(workAreasSend, {x,z, x1,z1, x2,z2, dx,dz, dx1,dz1, dx2,dz2, 0, k}); |
161 | end; |
162 | end; |
163 | if (table.getn(workAreasSend) > 0) then |
164 | local workAreasSend, bitType = TedderAreaEvent.runLocally(workAreasSend, self.accumulatedWorkAreaValues); |
165 | if (table.getn(workAreasSend) > 0) then |
166 | self.isWorking = true; |
167 | if self:getLastSpeed(true) > 0.5 then |
168 | for i=1, table.getn(workAreasSend) do |
169 | local workArea = typedWorkAreas[workAreasSend[i][14]]; |
170 | if workArea.grassParticleSystemIndex ~= nil then |
171 | local ps = self.grassParticleSystems[workArea.grassParticleSystemIndex+1]; |
172 | if ps ~= nil then |
173 | ps.disableTime = g_currentMission.time + 300; |
174 | if not ps.isEnabled then |
175 | ps.isEnabled = true; |
176 | self:raiseDirtyFlags(self.tedderParticleSystemFlag); |
177 | if self.isClient then |
178 | Utils.setEmittingState(ps.ps, true); |
179 | end; |
180 | end; |
181 | end; |
182 | end; |
183 | end; |
184 | end; |
185 | g_server:broadcastEvent(TedderAreaEvent:new(workAreasSend, bitType)); |
186 | end; |
187 | end; |
188 | end; |
189 | end; |
190 | end; |
191 | if self.isServer then |
192 | for k,v in pairs(self.grassParticleSystems) do |
193 | if g_currentMission.time > v.disableTime then |
194 | if v.isEnabled then |
195 | v.isEnabled = false; |
196 | self:raiseDirtyFlags(self.tedderParticleSystemFlag); |
197 | if self.isClient then |
198 | Utils.setEmittingState(v.ps, false); |
199 | end; |
200 | end; |
201 | end; |
202 | end; |
203 | end; |
204 | |
205 | if self:getIsTurnedOn() then |
206 | if self.isClient and self:getIsActiveForSound() then |
207 | Utils.playSample(self.sampleTedder, 0, 0, nil); |
208 | end; |
209 | end; |
210 | |
211 | end; |
212 | end; |
213 | |
214 | function Tedder:draw() |
215 | end; |
216 | |
217 | function Tedder:onDeactivate() |
218 | if self.isClient then |
219 | for k,v in pairs(self.grassParticleSystems) do |
220 | v.isEnabled = false; |
221 | Utils.setEmittingState(v.ps, false); |
222 | end; |
223 | end; |
224 | end; |
225 | |
226 | function Tedder:onDeactivateSounds() |
227 | if self.isClient then |
228 | Utils.stopSample(self.sampleTedder, true); |
229 | end; |
230 | end; |
231 | |
232 | function Tedder:onTurnedOff(noEventSend) |
233 | Tedder.onDeactivateSounds(self) |
234 | end; |
235 | |
236 | function Tedder:getDirtMultiplier(superFunc) |
237 | local multiplier = 0; |
238 | if superFunc ~= nil then |
239 | multiplier = multiplier + superFunc(self); |
240 | end; |
241 | |
242 | if self.isWorking then -- ToDo: sync self.isWorking for MP or dismiss ? |
243 | multiplier = multiplier + self.workMultiplier * self:getLastSpeed() / self.speedLimit; |
244 | end; |
245 | |
246 | return multiplier; |
247 | end; |
248 | |
249 | function Tedder:loadWorkAreaFromXML(superFunc, workArea, xmlFile, key) |
250 | local retValue = true; |
251 | if superFunc ~= nil then |
252 | retValue = superFunc(self, workArea, xmlFile, key) |
253 | end |
254 | |
255 | if workArea.type == WorkArea.AREATYPE_DEFAULT then |
256 | workArea.type = WorkArea.AREATYPE_TEDDER; |
257 | end; |
258 | |
259 | if workArea.type == WorkArea.AREATYPE_TEDDER then |
260 | workArea.grassParticleSystemIndex = getXMLInt(xmlFile, key .. "#particleSystemIndex"); |
261 | workArea.dropAreaIndex = Utils.getNoNil(getXMLInt(xmlFile, key .. "#dropAreaIndex"), 0) + 1; |
262 | |
263 | if self.accumulatedWorkAreaValues == nil then |
264 | self.accumulatedWorkAreaValues = {}; |
265 | end; |
266 | self.accumulatedWorkAreaValues[table.getn(self.accumulatedWorkAreaValues)+1] = 0; |
267 | end; |
268 | |
269 | return retValue; |
270 | end; |
271 | |
272 | function Tedder:doCheckSpeedLimit(superFunc) |
273 | local parent = true; |
274 | if superFunc ~= nil then |
275 | parent = superFunc(self); |
276 | end |
277 | |
278 | return parent and self.isTedderSpeedLimitActive; |
279 | end; |
280 | |
281 | function Tedder.getDefaultSpeedLimit() |
282 | return 15; |
283 | end;
|
Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de