Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de
1 | -- |
2 | -- Cultivator |
3 | -- Class for all cultivators |
4 | -- |
5 | -- @author Stefan Geiger |
6 | -- @date 16/01/14 |
7 | -- |
8 | -- Copyright (C) GIANTS Software GmbH, Confidential, All Rights Reserved. |
9 | |
10 | Cultivator = {}; |
11 | source("dataS/scripts/vehicles/specializations/CultivatorAreaEvent.lua"); |
12 | |
13 | function Cultivator.initSpecialization() |
14 | WorkArea.registerAreaType("cultivator"); |
15 | end; |
16 | |
17 | function Cultivator.prerequisitesPresent(specializations) |
18 | return SpecializationUtil.hasSpecialization(WorkArea, specializations); |
19 | end; |
20 | |
21 | function Cultivator:preLoad(xmlFile) |
22 | self.loadWorkAreaFromXML = Utils.overwrittenFunction(self.loadWorkAreaFromXML, Cultivator.loadWorkAreaFromXML); |
23 | end |
24 | |
25 | function Cultivator:load(xmlFile) |
26 | |
27 | self.getDoGroundManipulation = Utils.overwrittenFunction(self.getDoGroundManipulation, Cultivator.getDoGroundManipulation); |
28 | self.doCheckSpeedLimit = Utils.overwrittenFunction(self.doCheckSpeedLimit, Cultivator.doCheckSpeedLimit); |
29 | self.getDirtMultiplier = Utils.overwrittenFunction(self.getDirtMultiplier, Cultivator.getDirtMultiplier); |
30 | self.getIsReadyToSpray = Utils.overwrittenFunction(self.getIsReadyToSpray, Cultivator.getIsReadyToSpray); |
31 | |
32 | if hasXMLProperty(xmlFile, "vehicle.drum#index") then |
33 | print("Warning: vehicle.drum is no longer used, use vehicle.speedRotatingParts.speedRotatingPart\n"); |
34 | end; |
35 | |
36 | if next(self.groundReferenceNodes) == nil then |
37 | print("Warning: No ground reference nodes in "..self.configFileName); |
38 | end; |
39 | |
40 | if self.isClient then |
41 | self.sampleCultivator = Utils.loadSample(xmlFile, {}, "vehicle.cultivatorSound", nil, self.baseDirectory); |
42 | end; |
43 | |
44 | self.cultivatorDirectionNode = Utils.getNoNil(Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.cultivatorDirectionNode#index")), self.components[1].node); |
45 | |
46 | self.onlyActiveWhenLowered = Utils.getNoNil(getXMLBool(xmlFile, "vehicle.onlyActiveWhenLowered#value"), true); |
47 | |
48 | self.aiTerrainDetailChannel1 = g_currentMission.ploughChannel; |
49 | self.aiTerrainDetailChannel2 = g_currentMission.sowingChannel; |
50 | self.aiTerrainDetailChannel3 = g_currentMission.sowingWidthChannel; |
51 | |
52 | self.startActivationTimeout = 2000; |
53 | self.startActivationTime = 0; |
54 | |
55 | self.cultivatorHasGroundContact = false; |
56 | self.doGroundManipulation = false; |
57 | self.cultivatorLimitToField = false; |
58 | self.cultivatorForceLimitToField = true; |
59 | self.lastCultivatorArea = 0; |
60 | |
61 | self.isCultivatorSpeedLimitActive = false; |
62 | |
63 | self.showFieldNotOwnedWarning = false; |
64 | |
65 | self.cultivatorGroundContactFlag = self:getNextDirtyFlag(); |
66 | end; |
67 | |
68 | function Cultivator:delete() |
69 | if self.isClient then |
70 | Utils.deleteSample(self.sampleCultivator); |
71 | end; |
72 | end; |
73 | |
74 | function Cultivator:readUpdateStream(streamId, timestamp, connection) |
75 | if connection:getIsServer() then |
76 | self.cultivatorHasGroundContact = streamReadBool(streamId); |
77 | self.showFieldNotOwnedWarning = streamReadBool(streamId); |
78 | end; |
79 | end; |
80 | |
81 | function Cultivator:writeUpdateStream(streamId, connection, dirtyMask) |
82 | if not connection:getIsServer() then |
83 | streamWriteBool(streamId, self.cultivatorHasGroundContact); |
84 | streamWriteBool(streamId, self.showFieldNotOwnedWarning); |
85 | end; |
86 | end; |
87 | |
88 | function Cultivator:mouseEvent(posX, posY, isDown, isUp, button) |
89 | end; |
90 | |
91 | function Cultivator:keyEvent(unicode, sym, modifier, isDown) |
92 | end; |
93 | |
94 | function Cultivator:update(dt) |
95 | end; |
96 | |
97 | function Cultivator:updateTick(dt) |
98 | self.isCultivatorSpeedLimitActive = false; |
99 | if self:getIsActive() then |
100 | self.lastCultivatorArea = 0; |
101 | local showFieldNotOwnedWarning = false; |
102 | |
103 | if self.isServer then |
104 | local hasGroundContact = self:getIsTypedWorkAreaActive(WorkArea.AREATYPE_CULTIVATOR); |
105 | if self.cultivatorHasGroundContact ~= hasGroundContact then |
106 | self:raiseDirtyFlags(self.cultivatorGroundContactFlag); |
107 | self.cultivatorHasGroundContact = hasGroundContact; |
108 | end; |
109 | end; |
110 | local hasGroundContact = self.cultivatorHasGroundContact; |
111 | |
112 | self.doGroundManipulation = (hasGroundContact and (not self.onlyActiveWhenLowered or self:isLowered(false)) and self.startActivationTime <= g_currentMission.time); |
113 | |
114 | local foldAnimTime = self.foldAnimTime; |
115 | if self.doGroundManipulation then |
116 | self.isCultivatorSpeedLimitActive = true; |
117 | if self.isServer then |
118 | local workAreasSend, showWarning, _ = self:getTypedNetworkAreas(WorkArea.AREATYPE_CULTIVATOR, true); |
119 | showFieldNotOwnedWarning = showWarning; |
120 | |
121 | if table.getn(workAreasSend) > 0 then |
122 | local limitToField = self.cultivatorLimitToField or self.cultivatorForceLimitToField; |
123 | local limitGrassDestructionToField = self.cultivatorLimitToField or self.cultivatorForceLimitToField; |
124 | if not g_currentMission:getHasPermission("createFields", self:getOwner()) then |
125 | limitToField = true; |
126 | limitGrassDestructionToField = true; |
127 | end; |
128 | |
129 | local dx,dy,dz = localDirectionToWorld(self.cultivatorDirectionNode, 0, 0, 1); |
130 | local angle = Utils.convertToDensityMapAngle(Utils.getYRotationFromDirection(dx, dz), g_currentMission.terrainDetailAngleMaxValue); |
131 | |
132 | local realArea = CultivatorAreaEvent.runLocally(workAreasSend, limitToField, limitGrassDestructionToField, angle); |
133 | g_server:broadcastEvent(CultivatorAreaEvent:new(workAreasSend, limitToField, limitGrassDestructionToField, angle)); |
134 | |
135 | self.lastCultivatorArea = Utils.areaToHa(realArea, g_currentMission:getFruitPixelsToSqm()); -- 4096px are mapped to 2048m |
136 | g_currentMission.missionStats:updateStats("hectaresWorked", self.lastCultivatorArea); |
137 | end; |
138 | end; |
139 | g_currentMission.missionStats:updateStats("workingDuration", dt/(1000*60)); |
140 | end |
141 | |
142 | if self.isClient then |
143 | if self.doGroundManipulation and self:getLastSpeed() > 3 then |
144 | if self:getIsActiveForSound() then |
145 | Utils.playSample(self.sampleCultivator, 0, 0, nil); |
146 | end; |
147 | else |
148 | Utils.stopSample(self.sampleCultivator); |
149 | end; |
150 | end; |
151 | |
152 | if self.isServer then |
153 | if showFieldNotOwnedWarning ~= self.showFieldNotOwnedWarning then |
154 | self.showFieldNotOwnedWarning = showFieldNotOwnedWarning; |
155 | self:raiseDirtyFlags(self.cultivatorGroundContactFlag); |
156 | end |
157 | end |
158 | end; |
159 | |
160 | end; |
161 | |
162 | function Cultivator:draw() |
163 | if self.showFieldNotOwnedWarning then |
164 | g_currentMission:showBlinkingWarning(g_i18n:getText("You_dont_own_this_field")); |
165 | end; |
166 | end; |
167 | |
168 | function Cultivator:onAttach(attacherVehicle) |
169 | Cultivator.onActivate(self); |
170 | self.startActivationTime = g_currentMission.time + self.startActivationTimeout; |
171 | end; |
172 | |
173 | function Cultivator:onDetach() |
174 | self.cultivatorLimitToField = false; |
175 | end; |
176 | |
177 | function Cultivator:onEnter(isControlling) |
178 | if isControlling then |
179 | Cultivator.onActivate(self); |
180 | end; |
181 | end; |
182 | |
183 | function Cultivator:onActivate() |
184 | end; |
185 | |
186 | function Cultivator:onDeactivate() |
187 | self.showFieldNotOwnedWarning = false; |
188 | end; |
189 | |
190 | function Cultivator:onDeactivateSounds() |
191 | if self.isClient then |
192 | Utils.stopSample(self.sampleCultivator, true); |
193 | end; |
194 | end; |
195 | |
196 | function Cultivator:aiTurnOn() |
197 | self.cultivatorLimitToField = true; |
198 | end; |
199 | |
200 | function Cultivator:aiTurnOff() |
201 | self.cultivatorLimitToField = false; |
202 | end; |
203 | |
204 | function Cultivator:doCheckSpeedLimit(superFunc) |
205 | local parent = true; |
206 | if superFunc ~= nil then |
207 | parent = superFunc(self); |
208 | end |
209 | |
210 | return parent and self.isCultivatorSpeedLimitActive; |
211 | end; |
212 | |
213 | function Cultivator:getDoGroundManipulation(superFunc) |
214 | if not self.doGroundManipulation then |
215 | return false; |
216 | end; |
217 | |
218 | if superFunc ~= nil then |
219 | return superFunc(self, speedRotatingPart); |
220 | end |
221 | return true; |
222 | end; |
223 | |
224 | function Cultivator:getIsReadyToSpray(superFunc) |
225 | local isReadyToSpray = true; |
226 | if superFunc ~= nil then |
227 | isReadyToSpray = isReadyToSpray and superFunc(self); |
228 | end; |
229 | |
230 | isReadyToSpray = isReadyToSpray and self.doGroundManipulation; |
231 | |
232 | return isReadyToSpray; |
233 | end; |
234 | |
235 | function Cultivator:getDirtMultiplier(superFunc) |
236 | local multiplier = 0; |
237 | if superFunc ~= nil then |
238 | multiplier = multiplier + superFunc(self); |
239 | end; |
240 | |
241 | if self.doGroundManipulation then |
242 | multiplier = multiplier + self.workMultiplier * self:getLastSpeed() / self.speedLimit; |
243 | end; |
244 | |
245 | return multiplier; |
246 | end; |
247 | |
248 | function Cultivator:loadWorkAreaFromXML(superFunc, workArea, xmlFile, key) |
249 | local retValue = true; |
250 | if superFunc ~= nil then |
251 | retValue = superFunc(self, workArea, xmlFile, key) |
252 | end |
253 | |
254 | if workArea.type == WorkArea.AREATYPE_DEFAULT then |
255 | workArea.type = WorkArea.AREATYPE_CULTIVATOR; |
256 | end; |
257 | |
258 | return retValue; |
259 | end; |
260 | |
261 | function Cultivator.getDefaultSpeedLimit() |
262 | return 15; |
263 | end;
|
Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de