forked from RivalAUT/FS19_SprayerSectionControl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSprayerSectionControl.lua
666 lines (616 loc) · 27.9 KB
/
SprayerSectionControl.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
--
-- SprayerSectionControl
--
-- author: Rival
SprayerSectionControl = {}
SprayerSectionControl.modDirectory = g_currentModDirectory
SprayerSectionControl.HUDUVs = {
BACKGROUND = { 992, 100, 32, 32 },
SECTION_MID = { 710, 0, 166, 165 },
SECTION_LEFT_1 = { 520, 105, 180, 60 },
SECTION_LEFT_2 = { 362, 105, 153, 60 },
SECTION_LEFT_3 = { 200, 105, 160, 60 },
SECTION_LEFT_4 = { 12, 105, 185, 60 },
SECTION_RIGHT_1 = { 520, 34, 180, 60 },
SECTION_RIGHT_2 = { 362, 34, 153, 60 },
SECTION_RIGHT_3 = { 200, 34, 160, 60 },
SECTION_RIGHT_4 = { 12, 34, 185, 60 },
SECTION_LEFT_SPRAY_1 = { 0, 165, 218, 90 },
SECTION_LEFT_SPRAY_2 = { 218, 165, 190, 90 },
SECTION_MID_SPRAY = { 410, 165, 155, 90 },
SECTION_RIGHT_SPRAY_2 = { 566, 165, 190, 90 },
SECTION_RIGHT_SPRAY_1 = { 756, 165, 218, 90 },
SECTION_ONOFF = { 936, 0, 88, 88 }
}
SprayerSectionControl.COLOR = {
RED = { 0.7, 0, 0, 1 },
YELLOW = { 0.8, 0.5, 0, 1 },
GREEN = { 0, 0.7, 0, 1 },
}
function SprayerSectionControl.prerequisitesPresent(specializations)
return true
end
function SprayerSectionControl.getPrecisionFarmingAutoSpray(self)
if self.spec_ssc.isSSCReady and self.PrecisionFarming.loaded then
if self:getIsTurnedOn() then
local spec_es = self.spec_extendedSprayer
if spec_es ~= nil then
if spec_es.isLiming or spec_es.isFertilizing then
if spec_es.sprayAmountAutoMode then
return true
else
return false
end
else
return false
end
end
end
end
return false
end
function SprayerSectionControl.registerEventListeners(vehicleType)
SpecializationUtil.registerEventListener(vehicleType, "onRegisterActionEvents", SprayerSectionControl)
SpecializationUtil.registerEventListener(vehicleType, "onLoad", SprayerSectionControl)
SpecializationUtil.registerEventListener(vehicleType, "onUpdateTick", SprayerSectionControl)
--SpecializationUtil.registerEventListener(vehicleType, "onUpdate", SprayerSectionControl) -- onDraw
SpecializationUtil.registerEventListener(vehicleType, "onPostAttach", SprayerSectionControl)
SpecializationUtil.registerEventListener(vehicleType, "onPreDetach", SprayerSectionControl)
SpecializationUtil.registerEventListener(vehicleType, "onLeaveRootVehicle", SprayerSectionControl)
SpecializationUtil.registerEventListener(vehicleType, "onTurnedOn", SprayerSectionControl)
SpecializationUtil.registerEventListener(vehicleType, "onTurnedOff", SprayerSectionControl)
end
function SprayerSectionControl.registerOverwrittenFunctions(vehicleType)
SpecializationUtil.registerOverwrittenFunction(vehicleType, "processSprayerArea", SprayerSectionControl.processSprayerArea)
SpecializationUtil.registerOverwrittenFunction(vehicleType, "getSprayerUsage", SprayerSectionControl.getSprayerUsage)
SpecializationUtil.registerOverwrittenFunction(vehicleType, "doCheckSpeedLimit", SprayerSectionControl.doCheckSpeedLimit)
end
function SprayerSectionControl.registerFunctions(vehicleType)
SpecializationUtil.registerFunction(vehicleType, "getSprayerFullWidth", SprayerSectionControl.getSprayerFullWidth)
SpecializationUtil.registerFunction(vehicleType, "getActiveSprayerSectionsWidth", SprayerSectionControl.getActiveSprayerSectionsWidth)
SpecializationUtil.registerFunction(vehicleType, "changeSectionState", SprayerSectionControl.changeSectionState)
SpecializationUtil.registerFunction(vehicleType, "changeSectionGroupState", SprayerSectionControl.changeSectionGroupState)
SpecializationUtil.registerFunction(vehicleType, "toggleAutomaticMode", SprayerSectionControl.toggleAutomaticMode)
SpecializationUtil.registerFunction(vehicleType, "createSSCHUDElement", SprayerSectionControl.createSSCHUDElement)
SpecializationUtil.registerFunction(vehicleType, "createTestAreas", SprayerSectionControl.createTestAreas)
end
function SprayerSectionControl:onRegisterActionEvents(isActiveForInput, isActiveForInputIgnoreSelection)
if self.isClient then
if self.spec_ssc == nil then
self.spec_ssc = {}
else
if not self.spec_ssc.isSSCReady then
return
end
end
local spec = self.spec_ssc
spec.actionEvents = {}
self:clearActionEventsTable(spec.actionEvents)
if self:getIsActiveForInput(true, true) then -- ..., triggerUp, triggerDown, triggerAlways, startActive
local _, actionEventId = self:addActionEvent(spec.actionEvents, InputAction.SHOW_SSC_HUD, self, SprayerSectionControl.processActionEvent, false, true, false, true)
local _, actionEventId2 = self:addActionEvent(spec.actionEvents, InputAction.SHOW_SSC_MOUSE, self, SprayerSectionControl.processActionEvent, false, true, false, true)
end
end
end
function SprayerSectionControl:onLoad(savegame)
self.spec_ssc = {}
local spec = self.spec_ssc
self.PrecisionFarming = {}
self.PrecisionFarming.loaded = g_modIsLoaded["FS19_precisionFarming"]
self.PrecisionFarming.autoSprayActive = {}
spec.sections = {}
spec.groups = {}
if hasXMLProperty(self.xmlFile, "vehicle.sprayerSectionControl") then
spec.isSSCReady = true
else
spec.isSSCReady = false
end
if spec.isSSCReady then
local i = 0
while true do
local key = string.format("vehicle.sprayerSectionControl.sections.section(%d)", i)
if not hasXMLProperty(self.xmlFile, key) then
break
end
i = i + 1
local workAreaId = getXMLInt(self.xmlFile, key.."#workAreaId")
local testStart, testWidth, testHeight, coordinates = self:createTestAreas(workAreaId)
local effectNodes = StringUtil.getVectorNFromString(getXMLString(self.xmlFile, key.."#effectNodeId"))
local workingWidth = Utils.getNoNil(getXMLFloat(self.xmlFile, key.."#workingWidth"), 3)
if testStart ~= nil and testWidth ~= nil and testHeight ~= nil and effectNodes ~= nil then
spec.sections[i] = {workAreaId=workAreaId, effectNodes=effectNodes, testAreaStart=testStart, testAreaWidth=testWidth, testAreaHeight=testHeight, active=true, workingWidth = workingWidth, direction = 1, coordinates = coordinates}
self:getWorkAreaByIndex(workAreaId).sscId = i
spec.sections[i].sprayType = self:getWorkAreaByIndex(workAreaId).sprayType
else
print("Warning: Invalid sprayer section setup '"..key.."' in '" .. self.configFileName.."'!")
end
end
if hasXMLProperty(self.xmlFile, "vehicle.sprayerSectionControl.groups") then
local j=0
while true do
local key = string.format("vehicle.sprayerSectionControl.groups.group(%d)", j)
if not hasXMLProperty(self.xmlFile, key) then
break
end
j = j + 1
local sectionIds = StringUtil.splitString(" ", StringUtil.trim(getXMLString(self.xmlFile, key.."#sectionIds")))
if sectionIds ~= nil then
spec.groups[j] = sectionIds
end
end
else
for j=1, #spec.sections do
spec.groups[j] = {j}
end
end
for k,group in ipairs(spec.groups) do
for _,sectionId in ipairs(group) do
spec.sections[tonumber(sectionId)].group = k
end
end
spec.herbicideQuery = FieldCropsQuery:new(g_currentMission.terrainDetailId)
local desc = g_fruitTypeManager:getWeedFruitType()
spec.herbicideQuery:addRequiredCropType(g_currentMission.fruits[desc.index].id, 1, 3, desc.startStateChannel, desc.numStateChannels, g_currentMission.terrainDetailTypeFirstChannel, g_currentMission.terrainDetailTypeNumChannels)
spec.fertilizerQuery = FieldCropsQuery:new(g_currentMission.terrainDetailId)
spec.fertilizerQuery:addRequiredGroundValue(g_currentMission.cultivatorValue, g_currentMission.plowValue, g_currentMission.terrainDetailTypeFirstChannel, g_currentMission.terrainDetailTypeNumChannels)
spec.fertilizerQuery:addRequiredGroundValue(g_currentMission.sowingValue, g_currentMission.sowingWidthValue, g_currentMission.terrainDetailTypeFirstChannel, g_currentMission.terrainDetailTypeNumChannels)
spec.fertilizerQuery:addRequiredGroundValue(g_currentMission.grassValue, g_currentMission.grassValue, g_currentMission.terrainDetailTypeFirstChannel, g_currentMission.terrainDetailTypeNumChannels)
local fillType = self:getFillUnitLastValidFillType(self:getSprayerFillUnitIndex())
if fillType == FillType.UNKNOWN then
fillType = self:getFillUnitFirstSupportedFillType(self:getSprayerFillUnitIndex())
end
local sprayTypeDesc = g_sprayTypeManager:getSprayTypeByFillTypeIndex(fillType)
spec.fertilizerQuery:addProhibitedGroundValue(sprayTypeDesc.groundType, sprayTypeDesc.groundType, g_currentMission.sprayFirstChannel, g_currentMission.sprayNumChannels)
spec.fertilizerQuery:addProhibitedGroundValue(g_currentMission.sprayLevelMaxValue, g_currentMission.sprayLevelMaxValue, g_currentMission.sprayLevelFirstChannel, g_currentMission.sprayLevelNumChannels)
spec.isAutomaticMode = true
spec.hudActive = true
spec.hud = {}
local sections = math.min(#spec.groups, 13)
local midSection = math.ceil(sections/2)
local even = false
if midSection == sections/2 then -- section count even --> no middle part in HUD
midSection = midSection+1
even = true
end
local image = SprayerSectionControl.modDirectory .. "sschud.dds"
local uiScale = g_gameSettings.uiScale
local hudScale = 0.33
local wpx = SprayerSectionControl.HUDUVs.SECTION_LEFT_4[3]
if midSection > 2 then
wpx = wpx + SprayerSectionControl.HUDUVs.SECTION_LEFT_3[3]
end
if midSection > 3 then
wpx = wpx + SprayerSectionControl.HUDUVs.SECTION_LEFT_2[3]
end
if midSection > 4 then
wpx = wpx + ((midSection-4) * SprayerSectionControl.HUDUVs.SECTION_LEFT_1[3])
end
if even then
wpx = 2 * wpx
midSection = midSection-0.5
else
wpx = SprayerSectionControl.HUDUVs.SECTION_MID[3] + 2 * wpx
end
local w,h = getNormalizedScreenValues(wpx * uiScale * hudScale, 350 * uiScale * hudScale)
local w2,h2 = getNormalizedScreenValues((wpx+80) * uiScale * hudScale, 350 * uiScale * hudScale)
local baseX = 1-g_safeFrameOffsetX-w
local baseY = 0.75
spec.hud.sections = {}
spec.hud.buttons = {}
local lastW, lastH
local offsetX = 0
for j=1, sections do
if j < midSection then
local jj = math.max(5-j,1)
spec.hud.sections[j], lastW, lastH = self:createSSCHUDElement(image, baseX+offsetX, baseY, hudScale*uiScale, SprayerSectionControl.HUDUVs["SECTION_LEFT_"..tostring(jj)], Overlay.ALIGN_VERTICAL_BOTTOM)
spec.hud.sections[j]:setPosition(baseX+offsetX+lastW/2)
spec.hud.buttons[j],_,_ = self:createSSCHUDElement(image, baseX+offsetX+lastW/2, baseY, hudScale*uiScale, SprayerSectionControl.HUDUVs[(jj == 1 or jj == 4) and "SECTION_LEFT_SPRAY_1" or "SECTION_LEFT_SPRAY_2"], Overlay.ALIGN_VERTICAL_TOP, self.changeSectionGroupState, j)
offsetX = offsetX + lastW
end
if j == midSection then
spec.hud.sections[j], lastW, lastH = self:createSSCHUDElement(image, baseX+offsetX, baseY, hudScale*uiScale, SprayerSectionControl.HUDUVs.SECTION_MID, Overlay.ALIGN_VERTICAL_BOTTOM)
spec.hud.sections[j]:setPosition(baseX+offsetX+lastW/2)
spec.hud.buttons[j],_,_ = self:createSSCHUDElement(image, baseX+offsetX+lastW/2, baseY, hudScale*uiScale, SprayerSectionControl.HUDUVs.SECTION_MID_SPRAY, Overlay.ALIGN_VERTICAL_TOP, self.changeSectionGroupState, j)
offsetX = offsetX + lastW
end
if j > midSection then
local jj = math.max(j-sections+4,1)
spec.hud.sections[j], lastW, lastH = self:createSSCHUDElement(image, baseX+offsetX, baseY, hudScale*uiScale, SprayerSectionControl.HUDUVs["SECTION_RIGHT_"..tostring(jj)], Overlay.ALIGN_VERTICAL_BOTTOM)
spec.hud.sections[j]:setPosition(baseX+offsetX+lastW/2)
spec.hud.buttons[j],_,_ = self:createSSCHUDElement(image, baseX+offsetX+lastW/2, baseY, hudScale*uiScale, SprayerSectionControl.HUDUVs[(jj == 1 or jj == 4) and "SECTION_RIGHT_SPRAY_1" or "SECTION_RIGHT_SPRAY_2"], Overlay.ALIGN_VERTICAL_TOP, self.changeSectionGroupState, j)
offsetX = offsetX + lastW
end
end
spec.hud.midSection = math.ceil(midSection)
spec.hud.autoModeButton,_,_ = self:createSSCHUDElement(image, baseX+lastW/2, baseY+lastH, hudScale*uiScale*1.75, SprayerSectionControl.HUDUVs.SECTION_ONOFF, Overlay.ALIGN_VERTICAL_BOTTOM, self.toggleAutomaticMode)
spec.hud.bg = Overlay:new(image, spec.hud.sections[spec.hud.midSection].x+(even and spec.hud.sections[spec.hud.midSection].offsetX or 0), baseY+h/5, w2, h)
spec.hud.bg:setUVs(getNormalizedUVs(SprayerSectionControl.HUDUVs.BACKGROUND, { 1024, 256 }))
spec.hud.bg:setAlignment(Overlay.ALIGN_VERTICAL_MIDDLE, Overlay.ALIGN_HORIZONTAL_CENTER)
spec.hud.bg:setColor(0.015, 0.015, 0.015, 0.9)
end
if self.loadDashboardsFromXML ~= nil then
local key = "vehicle.sprayerSectionControl.dashboards"
self:loadDashboardsFromXML(self.xmlFile, key, {
valueFunc = "getActiveSprayerSectionsWidth",
valueTypeToLoad = "section",
valueObject = self,
additionalAttributesFunc = SprayerSectionControl.dashboardSectionAttributes,
stateFunc = SprayerSectionControl.dashboardSectionState
})
end
end
function SprayerSectionControl:createTestAreas(workAreaId)
local workArea = self:getWorkAreaByIndex(workAreaId)
if workArea == nil then
print(string.format("Error: workArea %d not found! Could not create testArea!", workareaId))
return
end
local parentNode = getParent(workArea.start)
local x1,y1,z1 = worldToLocal(parentNode, getWorldTranslation(workArea.start))
local x2,y2,z2 = worldToLocal(workArea.start, getWorldTranslation(workArea.width))
local x3,y3,z3 = worldToLocal(workArea.start, getWorldTranslation(workArea.height))
local max1 = math.max(z2,z3)
local adjust = g_client.serverStreamId ~= 0 and 1.8 or 1
if max1 == 0 then
z1 = z1 + adjust
end
if (z2 == max1 and max1 ~= 0) then
z2 = z2 + adjust
elseif (max1 == 0 and z2 < max1) then
z2 = z2 - adjust
end
if (z3 == max1 and max1 ~= 0) then
z3 = z3 + adjust
elseif (max1 == 0 and z3 < max1) then
z3 = z3 - adjust
end
local testAreaStartNode = createTransformGroup(string.format("testAreaStart%d", workAreaId))
setTranslation(testAreaStartNode, x1,y1,z1)
link(parentNode, testAreaStartNode)
local testAreaWidthNode = createTransformGroup(string.format("testAreaWidth%d", workAreaId))
setTranslation(testAreaWidthNode, x2,y2,z2)
link(testAreaStartNode, testAreaWidthNode)
local testAreaHeightNode = createTransformGroup(string.format("testAreaHeight%d", workAreaId))
setTranslation(testAreaHeightNode, x3,y3,z3)
link(testAreaStartNode, testAreaHeightNode)
return testAreaStartNode, testAreaWidthNode, testAreaHeightNode, {x1,y1,z1,adjust}
end
function SprayerSectionControl:createSSCHUDElement(image, x, y, hudScale, uvs, alignment, onClickCallback, sectionId)
local w,h = getNormalizedScreenValues(uvs[3]*hudScale, uvs[4]*hudScale)
local overlay = Overlay:new(image, x, y, w, h)
overlay:setUVs(getNormalizedUVs(uvs, { 1024, 256 }))
overlay:setAlignment(alignment, Overlay.ALIGN_HORIZONTAL_CENTER)
overlay:setIsVisible(true)
if onClickCallback ~= nil then
overlay.onClickCallback = onClickCallback
if sectionId ~= nil then
overlay.sectionId = sectionId
if self.spec_ssc.sections[sectionId].active then
overlay:setColor(unpack(SprayerSectionControl.COLOR.YELLOW))
else
overlay:setColor(unpack(SprayerSectionControl.COLOR.RED))
end
else
overlay:setColor(unpack(SprayerSectionControl.COLOR.GREEN))
end
end
return overlay, w, h
end
function SprayerSectionControl:onUpdateTick(dt)
if self.spec_ssc.isSSCReady then
local PFautoSpray = SprayerSectionControl.getPrecisionFarmingAutoSpray(self)
if self.PrecisionFarming.autoSprayActive == nil or self.PrecisionFarming.autoSprayActive ~= PFautoSpray then
self.PrecisionFarming.autoSprayActive = PFautoSpray
if PFautoSpray then
printf('SSC:onUpdateTick-- PF autoSpray ON')
else
printf('SSC:onUpdateTick-- PF autoSpray OFF')
end
end
local spec = self.spec_ssc
if spec.isAutomaticMode and self:getIsTurnedOn() then
if #spec.sections > 0 then
for k,section in pairs(spec.sections) do
local sActive = true
if section.sprayType ~= nil then
local sprayType = self:getActiveSprayType()
if sprayType ~= nil and section.sprayType ~= sprayType.index then
sActive = false
end
end
if sActive then
if self.movingDirection == 1 and section.direction ~= 1 then -- if driving reverse, adjust testAreas to cover area behind sprayer
setTranslation(section.testAreaStart, section.coordinates[1], section.coordinates[2], section.coordinates[3])
section.direction = 1
elseif self.movingDirection == 0 and section.direction ~= 0 then
setTranslation(section.testAreaStart, section.coordinates[1], section.coordinates[2], section.coordinates[3]-section.coordinates[4]/2)
section.direction = 0
elseif self.movingDirection == -1 and section.direction ~= -1 then
setTranslation(section.testAreaStart, section.coordinates[1], section.coordinates[2], section.coordinates[3]-section.coordinates[4])
section.direction = -1
end
local fillType = self:getFillUnitLastValidFillType(self:getSprayerFillUnitIndex())
if fillType == FillType.UNKNOWN then
fillType = self:getFillUnitFirstSupportedFillType(self:getSprayerFillUnitIndex())
end
local sprayTypeDesc = g_sprayTypeManager:getSprayTypeByFillTypeIndex(fillType)
local query = spec.fertilizerQuery
if sprayTypeDesc ~= nil and sprayTypeDesc.isHerbicide then
query = spec.herbicideQuery
end
local sx,_,sz = getWorldTranslation(section.testAreaStart)
local wx,_,wz = getWorldTranslation(section.testAreaWidth)
local hx,_,hz = getWorldTranslation(section.testAreaHeight)
local area, totalArea = AIVehicleUtil.getAIFruitArea(sx, sz, wx, wz, hx, hz, query)
local isAreaOwned = SprayerSectionControl.getIsAreaOwned(self:getActiveFarm(), sx, sz, wx, wz, hx, hz) -- check if is owned area
local newState = area > 0 and self:getLastSpeed() > 1 and isAreaOwned
if section.active ~= newState then
self:changeSectionState(section, newState)
end
end
end
end
end
end
end
function SprayerSectionControl:onMouseEvent(posX, posY, isDown, isUp, mouseButton)
if mouseButton == 1 then
local eventUsed = false
local controlledVehicle = g_currentMission.controlledVehicle
if controlledVehicle ~= nil then
local vehicle = controlledVehicle:getSelectedVehicle() -- workaround for non-existing self
if vehicle ~= nil and vehicle.spec_ssc ~= nil and vehicle.spec_ssc.isSSCReady then
if not vehicle.spec_ssc.isAutomaticMode then
for i,button in ipairs(vehicle.spec_ssc.hud.buttons) do
local x, y = button:getPosition()
local cursorInElement = GuiUtils.checkOverlayOverlap(posX, posY, x+button.offsetX, y+button.offsetY, button.width, button.height)
if not eventUsed then
if cursorInElement then
button.onClickCallback(vehicle, button.sectionId)
eventUsed = true
break
end
end
end
end
if not eventUsed then
local autoModeButton = vehicle.spec_ssc.hud.autoModeButton
local cursorInElement = GuiUtils.checkOverlayOverlap(posX, posY, autoModeButton.x+autoModeButton.offsetX, autoModeButton.y+autoModeButton.offsetY, autoModeButton.width, autoModeButton.height)
if cursorInElement then
vehicle.spec_ssc.hud.autoModeButton.onClickCallback(vehicle)
eventUsed = true
end
end
end
end
end
end
function SprayerSectionControl:onDrawFixed(vehicle)
if vehicle:getIsActiveForInput(false, true) and vehicle.spec_ssc.hudActive then
vehicle.spec_ssc.hud.bg:render()
for k,hud in pairs(vehicle.spec_ssc.hud.sections) do
hud:render()
end
for k,hud in pairs(vehicle.spec_ssc.hud.buttons) do
hud:render()
end
vehicle.spec_ssc.hud.autoModeButton:render()
setTextAlignment(RenderText.ALIGN_CENTER)
renderText(vehicle.spec_ssc.hud.sections[vehicle.spec_ssc.hud.midSection].x, vehicle.spec_ssc.hud.sections[vehicle.spec_ssc.hud.midSection].y+vehicle.spec_ssc.hud.sections[vehicle.spec_ssc.hud.midSection].offsetY+vehicle.spec_ssc.hud.sections[vehicle.spec_ssc.hud.midSection].height*1.1,
0.013*g_gameSettings.uiScale, vehicle.spec_ssc.isAutomaticMode and g_i18n:getText("SSC_AUTOMATIC_MODE") or g_i18n:getText("SSC_MANUAL_MODE"))
end
end
function SprayerSectionControl:toggleAutomaticMode(active)
if active ~= nil then
self.spec_ssc.isAutomaticMode = active
else
self.spec_ssc.isAutomaticMode = not self.spec_ssc.isAutomaticMode
end
if self.spec_ssc.isAutomaticMode then
self.spec_ssc.hud.autoModeButton:setColor(unpack(SprayerSectionControl.COLOR.GREEN))
else
self.spec_ssc.hud.autoModeButton:setColor(unpack(SprayerSectionControl.COLOR.RED))
end
end
function SprayerSectionControl:changeSectionGroupState(groupId)
for _,sectionId in ipairs(self.spec_ssc.groups[groupId]) do
self:changeSectionState(self.spec_ssc.sections[tonumber(sectionId)])
end
end
function SprayerSectionControl:changeSectionState(section, newState)
if newState == nil then
newState = not section.active
end
section.active = newState
if newState then
if self:getIsTurnedOn() then
if self:getAreEffectsVisible() then
for k2,effectNodeId in pairs(section.effectNodes) do
local sprayType = self:getActiveSprayType()
if sprayType ~= nil then
g_effectManager:startEffect(sprayType.effects[tonumber(effectNodeId)])
else
g_effectManager:startEffect(self.spec_sprayer.effects[tonumber(effectNodeId)])
end
end
end
self.spec_ssc.hud.buttons[section.group]:setColor(unpack(SprayerSectionControl.COLOR.GREEN))
else
self.spec_ssc.hud.buttons[section.group]:setColor(unpack(SprayerSectionControl.COLOR.YELLOW))
end
else
for k2,effectNodeId in pairs(section.effectNodes) do
--g_effectManager:stopEffect(self.spec_sprayer.effects[tonumber(effectNodeId)])
local sprayType = self:getActiveSprayType()
if sprayType ~= nil then
g_effectManager:stopEffect(sprayType.effects[tonumber(effectNodeId)])
else
g_effectManager:stopEffect(self.spec_sprayer.effects[tonumber(effectNodeId)])
end
end
self.spec_ssc.hud.buttons[section.group]:setColor(unpack(SprayerSectionControl.COLOR.RED))
end
end
function SprayerSectionControl:onTurnedOn()
if self.spec_ssc.isSSCReady then
local PFautoSpray = SprayerSectionControl.getPrecisionFarmingAutoSpray(self)
if self.PrecisionFarming.autoSprayActive == nil or self.PrecisionFarming.autoSprayActive ~= PFautoSpray then
self.PrecisionFarming.autoSprayActive = PFautoSpray
if PFautoSpray then
printf('SSC:onTurnedOn-- PF autoSpray ON')
else
printf('SSC:onTurnedOn-- PF autoSpray OFF')
end
end
for k,section in pairs(self.spec_ssc.sections) do
if not section.active then
for k2,effectNodeId in pairs(section.effectNodes) do
--[[g_effectManager:stopEffect(self.spec_sprayer.effects[tonumber(effectNodeId)])
for _, sprayType in ipairs(self.spec_sprayer.sprayTypes) do
g_effectManager:stopEffect(sprayType.effects[tonumber(effectNodeId)])
end]]
local sprayType = self:getActiveSprayType()
if sprayType ~= nil then
g_effectManager:stopEffect(sprayType.effects[tonumber(effectNodeId)])
else
g_effectManager:stopEffect(self.spec_sprayer.effects[tonumber(effectNodeId)])
end
end
else
self.spec_ssc.hud.buttons[section.group]:setColor(unpack(SprayerSectionControl.COLOR.GREEN))
end
end
end
end
function SprayerSectionControl:onTurnedOff()
if self.spec_ssc.isSSCReady then
for k,section in pairs(self.spec_ssc.sections) do
if section.active then
self.spec_ssc.hud.buttons[section.group]:setColor(unpack(SprayerSectionControl.COLOR.YELLOW))
end
end
end
end
function SprayerSectionControl:doCheckSpeedLimit(superFunc)
if self.spec_ssc ~= nil and self.spec_ssc.isSSCReady then
if self:getActiveSprayerSectionsWidth() == 0 then
return false
end
end
return superFunc(self)
end
function SprayerSectionControl:getSprayerUsage(superFunc, fillType, dt)
local origUsage = superFunc(self, fillType, dt)
if self.spec_ssc.isSSCReady then
return origUsage * (self:getLastSpeed() / self.speedLimit) * (self:getActiveSprayerSectionsWidth() / self:getSprayerFullWidth())
else
return origUsage
end
end
function SprayerSectionControl:processSprayerArea(superFunc, workArea, dt)
if self.spec_ssc.isSSCReady then
if workArea.sscId ~= nil and not self.spec_ssc.sections[workArea.sscId].active then
return 0,0
end
end
local changedArea, totalArea = superFunc(self, workArea, dt)
return changedArea, totalArea
end
function SprayerSectionControl:onPostAttach(attacherVehicle, inputJointDescIndex, jointDescIndex)
if attacherVehicle.spec_enterable ~= nil and attacherVehicle.spec_enterable.cameras ~= nil then
for _, camera in pairs(attacherVehicle.spec_enterable.cameras) do
camera.isRotatableBk = camera.isRotatable
end
end
end
function SprayerSectionControl:onPreDetach(attacherVehicle, implement)
if attacherVehicle.spec_enterable ~= nil and attacherVehicle.spec_enterable.cameras ~= nil then
for _, camera in pairs(attacherVehicle.spec_enterable.cameras) do
camera.allowTranslation = camera.rotateNode ~= nil and camera.rotateNode ~= camera.cameraNode
camera.isRotatable = Utils.getNoNil(camera.isRotatableBk, true)
end
end
end
function SprayerSectionControl:onLeaveRootVehicle()
local attacherVehicle = self:getRootVehicle()
if attacherVehicle.spec_enterable ~= nil and attacherVehicle.spec_enterable.cameras ~= nil then
for _, camera in pairs(attacherVehicle.spec_enterable.cameras) do
camera.allowTranslation = camera.rotateNode ~= nil and camera.rotateNode ~= camera.cameraNode
camera.isRotatable = Utils.getNoNil(camera.isRotatableBk, true)
end
end
end
function SprayerSectionControl:getSprayerFullWidth()
local width = 0
for k,section in pairs(self.spec_ssc.sections) do
width = width + section.workingWidth
end
return width
end
function SprayerSectionControl:getActiveSprayerSectionsWidth()
local width = 0
for k,section in pairs(self.spec_ssc.sections) do
if section.active then
width = width + section.workingWidth
end
end
return width
end
function SprayerSectionControl.processActionEvent(self, actionName, inputValue, callbackState, isAnalog)
if actionName == "SHOW_SSC_HUD" then
self.spec_ssc.hudActive = not self.spec_ssc.hudActive
if not self.spec_ssc.hudActive and g_inputBinding:getShowMouseCursor() then
g_inputBinding:setShowMouseCursor(false)
local rootVehicle = self:getRootVehicle()
if rootVehicle.spec_enterable ~= nil and rootVehicle.spec_enterable.cameras ~= nil then
for _, camera in pairs(rootVehicle.spec_enterable.cameras) do
camera.allowTranslation = camera.rotateNode ~= nil and camera.rotateNode ~= camera.cameraNode
camera.isRotatable = Utils.getNoNil(camera.isRotatableBk, true)
end
end
end
end
if actionName == "SHOW_SSC_MOUSE" then
if self.spec_ssc.hudActive then
local show = not g_inputBinding:getShowMouseCursor()
g_inputBinding:setShowMouseCursor(show)
local rootVehicle = self:getRootVehicle()
if rootVehicle.spec_enterable ~= nil and rootVehicle.spec_enterable.cameras ~= nil then
for _, camera in pairs(self:getRootVehicle().spec_enterable.cameras) do
camera.isRotatable = Utils.getNoNil(camera.isRotatableBk, true) and not show
camera.allowTranslation = (camera.rotateNode ~= nil and camera.rotateNode ~= camera.cameraNode) and not show
end
end
end
end
end
function SprayerSectionControl.getIsAreaOwned(farmId, sX, sZ, wX, wZ, hX, hZ)
local centerX, centerZ = (sX + wX)*0.5, (sZ + wZ)*0.5
if g_farmlandManager:getIsOwnedByFarmAtWorldPosition(farmId, centerX, centerZ) then
return true
end
if g_missionManager:getIsMissionWorkAllowed(farmId, centerX, centerZ, nil) then
return true
end
return false
end
function SprayerSectionControl:dashboardSectionAttributes(xmlFile, key, dashboard, isActive)
dashboard.sectionId = {
StringUtil.getVectorFromString(getXMLString(xmlFile, key .. "#sectionId"))
}
return true
end
function SprayerSectionControl:dashboardSectionState(dashboard, newValue, minValue, maxValue, isActive)
local isSectionActive = false
if dashboard.sectionId ~= nil then
for _, sectionID in pairs(dashboard.sectionId) do
for id, section in pairs(self.spec_ssc.sections) do
if sectionID == id then
isSectionActive = section.active
break
end
end
end
end
Dashboard.defaultDashboardStateFunc(self, dashboard, isSectionActive, minValue, maxValue, isActive)
end