Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.lua
More file actions
Latest commit
817 lines (770 loc) · 31.3 KB
/
Copy pathmain.lua
File metadata and controls
817 lines (770 loc) · 31.3 KB
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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
--
-- Graphical test tool for the Fieldwork Course Generator
--
-- Usage:
-- love.exe . <selectedField definitions file> <selectedField number>
--
-- where 'selectedField definitions file' is an XML file that can be created from the
-- Farming Simulator game console command cpSaveAllFields. The file will contain
-- the boundaries of all owned fields of the current map, including selectedField islands.
--
--
dofile('include.lua')
locallogger=Logger('main', Logger.level.debug)
localparameters= {}
-- working width of the equipment
localworkingWidth=AdjustableParameter(2.8, 'width', 'W', 'w', 0.1, 0, 100)
table.insert(parameters, workingWidth)
localturningRadius=AdjustableParameter(5, 'radius', 'T', 't', 0.1, 0, 20)
table.insert(parameters, turningRadius)
localfieldMargin=AdjustableParameter(0, 'margin', 'N', 'n', 0.1, -5, 5)
table.insert(parameters, fieldMargin)
-- number of headland passes around the field boundary
localnHeadlandPasses=AdjustableParameter(3, 'headlands', 'P', 'p', 1, 0, 100)
table.insert(parameters, nHeadlandPasses)
localnHeadlandsWithRoundCorners=AdjustableParameter(2, 'headlands with round corners', 'R', 'r', 1, 0, 100)
table.insert(parameters, nHeadlandsWithRoundCorners)
localheadlandClockwise=ToggleParameter('headlands clockwise', true, 'c')
table.insert(parameters, headlandClockwise)
localheadlandFirst=ToggleParameter('headlands first', true, 'h')
table.insert(parameters, headlandFirst)
localheadlandOverlap=AdjustableParameter(5, 'headland overlap', 'Q', 'q', 1, 0, 100)
table.insert(parameters, headlandOverlap)
localfieldCornerRadius=AdjustableParameter(7, 'field corner radius', 'F', 'f', 1, 0, 30)
table.insert(parameters, fieldCornerRadius)
localsharpenCorners=ToggleParameter('sharpen corners', true, 's')
table.insert(parameters, sharpenCorners)
localbypassIslands=ToggleParameter('island bypass', true, 'b')
table.insert(parameters, bypassIslands)
localnIslandHeadlandPasses=AdjustableParameter(2, 'island headlands', 'I', 'i', 1, 1, 10)
table.insert(parameters, nIslandHeadlandPasses)
localislandHeadlandClockwise=ToggleParameter('island headlands clockwise', false, 'C')
table.insert(parameters, islandHeadlandClockwise)
localautoRowAngle=ToggleParameter('auto row angle', true, '6')
table.insert(parameters, autoRowAngle)
localrowAngleDeg=AdjustableParameter(0, 'row angle', 'A', 'a', 10, -90, 90)
table.insert(parameters, rowAngleDeg)
localrowPattern=ListParameter(CourseGenerator.RowPattern.LANDS, 'row pattern', 'O', 'o',
{ CourseGenerator.RowPattern.ALTERNATING,
CourseGenerator.RowPattern.SKIP,
CourseGenerator.RowPattern.SPIRAL,
CourseGenerator.RowPattern.LANDS,
CourseGenerator.RowPattern.RACETRACK
},
{
'alternating',
'skip',
'spiral',
'lands',
'racetrack'
})
table.insert(parameters, rowPattern)
localnRows=AdjustableParameter(3, 'rows to skip/rows per land', 'K', 'k', 1, 0, 10)
table.insert(parameters, nRows)
localleaveSkippedRowsUnworked=ToggleParameter('skipped rows unworked', false, 'u')
table.insert(parameters, leaveSkippedRowsUnworked)
localcenterClockwise=ToggleParameter('spiral/lands clockwise', true, 'l')
table.insert(parameters, centerClockwise)
localspiralFromInside=ToggleParameter('spiral from inside', true, 'L')
table.insert(parameters, spiralFromInside)
localevenRowDistribution=ToggleParameter('even row width', true, 'e')
table.insert(parameters, evenRowDistribution)
localuseBaselineEdge=ToggleParameter('use base line edge', false, 'g')
table.insert(parameters, useBaselineEdge)
localshowDebugInfo=ToggleParameter('show debug info', false, 'd', true)
table.insert(parameters, showDebugInfo)
localtwoSided=ToggleParameter('two sided', false, '2')
table.insert(parameters, twoSided)
localshowSwath=ToggleParameter('show swath', false, '1', true)
table.insert(parameters, showSwath)
localreverseCourse=ToggleParameter('reverse', false, 'v', true)
table.insert(parameters, reverseCourse)
localsmallOverlaps=ToggleParameter('small overlaps', false, 'm', true)
table.insert(parameters, smallOverlaps)
localnVehicles=AdjustableParameter(1, 'number of vehicles', 'Y', 'y', 1, 1, 5)
table.insert(parameters, nVehicles)
localuseSameTurnWidth=ToggleParameter('use same turn width', false, 'u')
table.insert(parameters, useSameTurnWidth)
localprofilerEnabled=false
localfileName=''
localdragging=false
localpointSize=1
locallineWidth=0.1
localscale=1.0
localwindowWidth=1400
localwindowHeight=800
localxOffset, yOffset=0, 0
-- starting position
localstartX, startY, baselineX, baselineY=1000, 0, 1000, 0
localgraphicsTransform, textTransform, statusTransform, mouseTransform, contextTransform, errorTransform
localstartSign, stopSign
localparameterNameColor= { 1, 1, 1 }
localparameterKeyColor= { 0, 1, 1 }
localparameterValueColor= { 1, 1, 0 }
localstartLocationColor= { 0.9, 0.9, 0.9 }
localfieldBoundaryColor= { 0.5, 0.5, 0.5 }
localcourseColor= { 0, 0.7, 1 }
localturnColor= { 1, 1, 0, 0.5 }
localusePathfinderColor= { 0, 1, 0 }
localislandHeadlandColor= { 1, 1, 1, 0.2 }
localwaypointColor= { 0.7, 0.5, 0.2 }
localcornerColor= { 1, 1, 0.0, 0.8 }
localislandBypassColor= { 0, 0.2, 1.0 }
localdebugColor= { 1, 0.5, 0.5, 0.7 }
localdebugTextColor= { 0.8, 0, 0, 1 }
localwarningColor= { 1, 0.5, 0 }
localhighlightedWaypointColor= { 0.7, 0.7, 0.7, 1 }
localhighlightedWaypointColorForward= { 0, 0.7, 0, 1 }
localhighlightedWaypointColorBackward= { 0.7, 0, 0, 1 }
localcenterColor= { 0, 0.7, 1, 0.8 }
localcenterFontColor= { 0, 0.7, 1 }
localblockColor= { 1, 0.5, 0, 0.2 }
localblockFontColor= { 1, 0.5, 0, 1 }
localrowEndColor= { 0, 1, 0, 1 }
localrowStartColor= { 1, 0, 0, 1 }
localconnectingPathColor= { 0.3, 0.3, 0.3, 1 }
localconnectingPathFontColor= { 0.8, 0.8, 0.8, 1 }
localswathColor= { 0, 0.7, 0, 0.25 }
localislandColor= { 0.7, 0, 0.7, 1 }
localislandPointColor= { 0.7, 0, 0.7, 0.4 }
localislandPerimeterPointColor= { 1, 0.4, 1 }
localmultiVehicleColorForPosition= {}
multiVehicleColorForPosition[-2] = { 1, 0, 0 }
multiVehicleColorForPosition[-1] = { 1, 0.3, 0.3 }
multiVehicleColorForPosition[0] =courseColor
multiVehicleColorForPosition[1] = { 0.3, 1, 0.3 }
multiVehicleColorForPosition[2] = { 0, 1, 0 }
-- the selectedField to generate the course for
---@typeCourseGenerator.Field
localselectedField
-- the generated fieldwork course
---@typeCourseGenerator.FieldworkCourse
localcourse
localsavedFields
localcurrentVertices
localerrors= {}
localcontext
------------------------------------------------------------------------------------------------------------------------
--- Generate the fieldwork course
---------------------------------------------------------------------------------------------------------------------------
localfunctiongenerate()
Logger.setLogfile(string.format('log/%s.log', selectedField:getId()))
CourseGenerator.clearDebugObjects()
context=CourseGenerator.FieldworkContext(selectedField, workingWidth:get(), turningRadius:get(), nHeadlandPasses:get())
:setHeadlandsWithRoundCorners(nHeadlandsWithRoundCorners:get())
:setHeadlandClockwise(headlandClockwise:get())
:setIslandHeadlandClockwise(islandHeadlandClockwise:get())
:setHeadlandFirst(headlandFirst:get())
:setIslandHeadlands(nIslandHeadlandPasses:get())
:setFieldCornerRadius(fieldCornerRadius:get())
:setBypassIslands(bypassIslands:get())
:setSharpenCorners(sharpenCorners:get())
:setAutoRowAngle(autoRowAngle:get())
:setRowAngle(math.rad(rowAngleDeg:get()))
:setEvenRowDistribution(evenRowDistribution:get())
:setUseBaselineEdge(useBaselineEdge:get())
:setStartLocation(startX, startY)
:setBaselineEdge(baselineX, baselineY)
:setEnableSmallOverlapsWithHeadland(smallOverlaps:get())
:setFieldMargin(fieldMargin:get())
:setHeadlandOverlap(headlandOverlap:get())
ifcontext.setNumberOfVehiclesthen
context:setNumberOfVehicles(nVehicles:get())
context:setUseSameTurnWidth(useSameTurnWidth:get())
end
ifprofilerEnabledthen
love.profiler.start()
end
ifrowPattern:get() ==CourseGenerator.RowPattern.SKIPthen
context:setRowPattern(CourseGenerator.RowPattern.create(rowPattern:get(), nRows:get(), leaveSkippedRowsUnworked:get()))
elseifrowPattern:get() ==CourseGenerator.RowPattern.SPIRALthen
context:setRowPattern(CourseGenerator.RowPattern.create(rowPattern:get(), centerClockwise:get(), spiralFromInside:get()))
elseifrowPattern:get() ==CourseGenerator.RowPattern.LANDSthen
context:setRowPattern(CourseGenerator.RowPattern.create(rowPattern:get(), centerClockwise:get(), nRows:get()))
elseifrowPattern:get() ==CourseGenerator.RowPattern.RACETRACKthen
context:setRowPattern(CourseGenerator.RowPattern.create(rowPattern:get(), nRows:get()))
else
context:setRowPattern(CourseGenerator.RowPattern.create(rowPattern:get()))
end
localgeneratorFunc
iftwoSided:get() then
-- baseline edge is where the headlands are (there, and the opposite field edge)
context:setBaselineEdge(startX, startY)
generatorFunc=function()
returnCourseGenerator.FieldworkCourseTwoSided(context)
end
elseifnVehicles:get() >1andCourseGenerator.FieldworkCourseMultiVehiclethen
generatorFunc=function()
returnCourseGenerator.FieldworkCourseMultiVehicle(context)
end
else
generatorFunc=function()
returnCourseGenerator.FieldworkCourse(context)
end
end
localsuccess
success, course=xpcall(
generatorFunc,
function(err)
context:addError(logger, debug.traceback(err))
error(nil)
end)
ifnotsuccessthen
io.stdout:flush()
errors=context:getErrors()
return
end
ifreverseCourse:get() then
course:reverse()
end
ifprofilerEnabledthen
print(love.profiler.report(40))
love.profiler.reset()
love.profiler.stop()
end
-- export the first headland as CSV
localexporter=Exporter(course)
exporter:exportHeadlandAsCsv(1, 'headland-1.csv')
-- make sure all logs are now visible
io.stdout:flush()
errors=context:getErrors()
end
localfunctionupdateTransform()
graphicsTransform=love.math.newTransform(xOffset, yOffset, 0, 1, 1, 0, 0, 0, 0):scale(scale, -scale)
textTransform=love.math.newTransform(xOffset, yOffset, 0, 1, 1, 0, 0, 0, 0):scale(scale, scale)
end
--- Set offset so with the current scale, the world coordinates x, y are in the middle of the screen
localfunctionsetOffset(x, y)
xOffset=-(scale*x-windowWidth/2)
yOffset= (scale*y-windowHeight/2) +windowHeight
end
functionlove.load(arg)
ifprofilerEnabledthen
love.profiler=require('profile')
end
fileName=arg[1]
logger:debug('Reading %s...', fileName)
savedFields=CourseGenerator.Field.loadSavedFields(fileName)
selectedField=savedFields[tonumber(arg[2])]
localx1, y1, x2, y2=selectedField:getBoundingBox()
localfieldWidth, fieldHeight=x2-x1, y2-y1
localxScale=windowWidth/fieldWidth
localyScale=windowHeight/fieldHeight
ifxScale>yScalethen
scale=0.9*yScale
pointSize=0.9*yScale
else
scale=0.9*xScale
pointSize=0.9*xScale
end
-- window is 80% of the screen size
windowWidth, windowHeight=love.window.getDesktopDimensions()
windowWidth, windowHeight=0.8*windowWidth, 0.8*windowHeight
-- initially, start in the lower left corner
startX, startY=x1+10, y1+10
localfieldCenter=selectedField:getCenter()
-- world offset
--scale = 1
setOffset(fieldCenter.x, fieldCenter.y)
updateTransform()
statusTransform=love.math.newTransform(0, 0, 0, 1, 1, -windowWidth+200, -windowHeight+30)
mouseTransform=love.math.newTransform()
contextTransform=love.math.newTransform(10, 10, 0, 1, 1, 0, 0)
errorTransform=love.math.newTransform(300, 0, 0, 1, 1, 0, 0)
love.graphics.setPointSize(pointSize)
love.graphics.setLineWidth(lineWidth)
love.window.setMode(windowWidth, windowHeight, { highdpi=true })
love.window.setTitle(string.format('Course Generator - %s', selectedField:getId()))
startSign=love.graphics.newImage('FS25_Courseplay/img/signs/start.dds')
stopSign=love.graphics.newImage('FS25_Courseplay/img/signs/stop.dds')
generate()
end
localfunctionscreenToWorld(sx, sy)
returngraphicsTransform:inverseTransformPoint(sx, sy)
end
localfunctionfloatToString(f)
iffthen
returnstring.format('%.1f', f)
else
return'nil'
end
end
localfunctionintToString(d)
ifdthen
returnstring.format('%d', d)
else
return'nil'
end
end
localfunctionfindCurrentVertices(sx, sy)
localx, y=screenToWorld(sx, sy)
ifcourseandnotcontext:hasErrors() then
localvertices= {}
for_, pos, pathincourse:pathIterator() do
for_, vinpath:vertices() do
ifmath.abs(v.x-x) <1andmath.abs(v.y-y) <1then
ifvertices[pos] ==nilthen
vertices[pos] = {}
end
table.insert(vertices[pos], v)
end
end
end
returnvertices
end
end
localfunctionselectFieldUnderCursor()
localx, y=love.mouse.getPosition()
startX, startY=screenToWorld(x, y)
for_, finpairs(savedFields) do
iff:getBoundary():isInside(startX, startY) then
print(string.format('Field %s selected', f:getId()))
selectedField=f
love.window.setTitle(string.format('Course Generator - %s', selectedField:getId()))
generate()
end
end
end
localfunctiondrawVertexAsArrow(v)
localleft, right, back=-0.8, 0.8, -0.5
localtriangle= { left, 0, right, 0, 0, 1.6 }
love.graphics.push()
love.graphics.translate(v.x, v.y)
love.graphics.rotate((v:getExitEdge() orv:getEntryEdge()):getHeading() -math.pi/2)
love.graphics.polygon('fill', triangle)
ifv:getAttributes():isLeftSideNotWorked() then
love.graphics.line({1.5*left, back, 0, back})
end
ifv:getAttributes():isRightSideNotWorked() then
love.graphics.line({1.5*right, back, 0, back})
end
love.graphics.pop()
end
localfunctiondrawWaypoint(v)
ifv.colorthen
love.graphics.setColor(v.color)
else
love.graphics.setColor(waypointColor)
end
ifv:getAttributes():isHeadlandTurn() then
love.graphics.setColor(cornerColor)
end
ifv:getAttributes():isIslandBypass() then
love.graphics.setColor(islandBypassColor)
end
ifv:getAttributes():isRowStart() then
love.graphics.setColor(rowStartColor)
elseifv:getAttributes():isRowEnd() then
love.graphics.setColor(rowEndColor)
end
drawVertexAsArrow(v)
end
localfunctiondrawPath(p, color)
if#p>1then
fori, vinp:vertices() do
ifv:getExitEdge() then
ifv:getAttributes():shouldUsePathfinderToNextWaypoint() then
love.graphics.setLineWidth(5*lineWidth)
love.graphics.setColor(usePathfinderColor)
elseifv:getAttributes():isRowEnd() then
love.graphics.setLineWidth(lineWidth)
love.graphics.setColor(turnColor)
else
love.graphics.setLineWidth(lineWidth)
love.graphics.setColor(colororcourseColor)
end
love.graphics.line(v.x, v.y, v:getExitEdge():getEnd().x, v:getExitEdge():getEnd().y)
end
ifv:getEntryEdge() andv:getAttributes():shouldUsePathfinderToThisWaypoint() then
love.graphics.setLineWidth(5*lineWidth)
love.graphics.setColor(usePathfinderColor)
love.graphics.line(v.x, v.y, v:getEntryEdge():getBase().x, v:getEntryEdge():getBase().y)
end
ifp[i+1] andv:almostEquals(p[i+1]) then
-- two subsequent vertices have the same position
love.graphics.setColor(warningColor)
love.graphics.circle('line', v.x, v.y, 2)
end
drawWaypoint(v)
end
love.graphics.push()
love.graphics.scale(1, -1)
localsignScale=0.03
love.graphics.draw(startSign, p[1].x-2, -p[1].y-2, 0, signScale, signScale)
love.graphics.draw(stopSign, p[#p].x-2, -p[#p].y-2, 0, signScale, signScale)
love.graphics.pop()
end
end
localfunctiondrawSwath(p)
ifshowSwath:get() then
if#p>1then
love.graphics.setLineWidth(workingWidth:get())
fori, vinp:vertices() do
-- when hovering over a vertex, show the swath up
ifcurrentVerticesandcurrentVertices[1] andcurrentVertices[1].ix<=ithen
break
end
ifv:getExitEdge() then
ifnotv:getAttributes():isRowEnd() then
love.graphics.setColor(swathColor)
love.graphics.line(v.x, v.y, v:getExitEdge():getEnd().x, v:getExitEdge():getEnd().y)
end
end
end
end
end
end
localfunctiondrawIslandHeadland(h, color)
love.graphics.setLineWidth(10*lineWidth)
love.graphics.setColor(color)
ifh:isValid() then
love.graphics.polygon('line', h:getUnpackedVertices())
end
end
localfunctiondrawText(x, y, color, textScale, ...)
love.graphics.push()
love.graphics.setColor(color)
love.graphics.scale(1, -1)
love.graphics.print(string.format(...), x, -y, 0, (textScaleor1) /scale)
love.graphics.pop()
end
---@paramblockCourseGenerator.Block
localfunctiondrawRows(block)
fori, rinipairs(block:getRows()) do
love.graphics.push()
love.graphics.setColor(centerColor)
love.graphics.setPointSize(pointSize*1.5)
localm=r:getMiddle()
love.graphics.setColor(centerFontColor)
love.graphics.scale(1, -1)
love.graphics.print(i, m.x, -m.y, 0, 1/scale)
love.graphics.pop()
end
end
---@paramblockCourseGenerator.Center
localfunctiondrawConnectingPaths(center)
ifcenter==nilorcenter:getConnectingPaths() ==nilthen
return
end
fori, pinipairs(center:getConnectingPaths()) do
love.graphics.setLineWidth(10*lineWidth)
if#p>0then
love.graphics.setColor(connectingPathColor)
if#p>1then
love.graphics.line(p:getUnpackedVertices())
end
love.graphics.setPointSize(pointSize*6)
love.graphics.setColor(0, 1, 0, 0.3)
love.graphics.points(p[1].x, p[1].y)
love.graphics.setColor(1, 0, 0, 0.3)
love.graphics.points(p[#p].x, p[#p].y)
drawText(p[1].x, p[1].y, connectingPathFontColor, 1, '%d - start', i)
drawText(p[#p].x, p[#p].y, connectingPathFontColor, 1, '%d - end', i)
end
end
end
localfunctiondrawBlocks(center)
forib, binipairs(center:getBlocks()) do
love.graphics.setColor(blockColor)
love.graphics.polygon('fill', b:getPolygon():getUnpackedVertices())
love.graphics.setLineWidth(2*lineWidth)
localblockCenter=b:getPolygon():getCenter()
love.graphics.push()
love.graphics.setColor(blockFontColor)
love.graphics.scale(1, -1)
love.graphics.print(string.format('%d (%d)', ib, b.id), blockCenter.x, -blockCenter.y, 0, 1/scale)
love.graphics.pop()
drawRows(b)
end
end
localfunctiondrawCenter(center)
ifcenter:getDebugRows() then
ifshowDebugInfo:get() then
fori, rinipairs(course:getCenter():getDebugRows()) do
love.graphics.setColor(debugColor)
love.graphics.setLineWidth(3*lineWidth)
love.graphics.line(r:getUnpackedVertices())
for_, vinr:vertices() do
love.graphics.setColor(debugColor)
love.graphics.points(v.x, v.y)
end
drawText(r:getMiddle().x, r:getMiddle().y, debugTextColor, 1, '%d', i)
end
end
end
drawBlocks(center)
end
localfunctiondrawFields()
for_, finpairs(savedFields) do
love.graphics.setLineWidth(lineWidth)
love.graphics.setColor(fieldBoundaryColor)
localunpackedVertices=f:getUnpackedVertices()
if#unpackedVertices>2then
love.graphics.polygon('line', unpackedVertices)
fori, vinipairs(f:getBoundary()) do
ifv:getRadius() <10then
love.graphics.setColor(debugColor)
else
love.graphics.setColor(fieldBoundaryColor)
end
love.graphics.points(v.x, v.y)
end
for_, iinipairs(f:getIslands()) do
love.graphics.setColor(islandColor)
love.graphics.setLineWidth(lineWidth)
if#i:getBoundary() >2then
love.graphics.polygon('line', i:getBoundary():getUnpackedVertices())
end
localislandHeadlands=i:getHeadlands();
for_, hinipairs(islandHeadlands) do
drawIslandHeadland(h, islandHeadlandColor)
end
love.graphics.setColor(islandColor)
localc=i:getBoundary():getCenter()
love.graphics.push()
love.graphics.scale(1, -1)
love.graphics.print(i:getId(), c.x, -c.y)
love.graphics.pop()
end
end
love.graphics.setColor(fieldBoundaryColor)
localc=f:getCenter()
love.graphics.push()
love.graphics.scale(1, -1)
love.graphics.print(f:getNum(), c.x, -c.y)
love.graphics.pop()
end
end
localfunctiondrawHeadlands()
--drawHeadland(course:getHeadlandPath(), courseColor)
drawConnectingPaths(course:getCenter())
end
-- Draw a tooltip with the vertex' details
localfunctiondrawVertexInfo()
love.graphics.replaceTransform(mouseTransform)
localtext=''
forpos, verticesinpairs(currentVertices) do
fori, vinipairs(vertices) do
ifi>1then
text=text..'---\n'
end
text=text..string.format('v: %s ix: %s\n', intToString(pos), intToString(v.ix))
text=text..string.format('r: %s ur: %s xte: %s\n', floatToString(v:getSignedRadius()),
floatToString(v.unitRadius), floatToString(v:getXte(turningRadius:get())))
text=text..string.format('corner: %s\n', v.isCorner)
text=text..string.format('x: %s y: %s\n', floatToString(v.x), floatToString(v.y))
text=text..tostring(v:getAttributes()) ..'\n'
end
end
iftext~='' then
localwidth, margin=300, 10
local_, wrappedText=love.graphics.getFont():getWrap(text, width-2*margin)
localh=love.graphics.getFont():getHeight()
love.graphics.setColor(0.2, 0.2, 0.2)
love.graphics.rectangle('fill', 0, 0, width, (#wrappedText+1) *h)
love.graphics.setColor(0.9, 0.9, 0.9)
love.graphics.printf(text, margin, h, width-2*margin)
end
end
-- Highlight a few vertices around the selected one
localfunctionhighlightPathAroundVertex(polygon, v)
love.graphics.replaceTransform(graphicsTransform)
love.graphics.setLineWidth(lineWidth)
fori=v.ix-20, v.ix+30do
love.graphics.setColor(i==v.ixandhighlightedWaypointColoror
(i<v.ixandhighlightedWaypointColorBackwardorhighlightedWaypointColorForward))
localp=polygon:at(i)
ifpthen
love.graphics.circle('line', p.x, p.y, 1.5)
end
end
end
localfunctionhighlightPathToNextRow()
forpos, verticesinpairs(currentVertices) do
fori=1, #vertices-1do
localv=vertices[i]
ifv:getAttributes():isRowEnd() then
localinnermostHeadland=course:findPathToNextRow(v:getAttributes():getAtBoundaryId(),
v, course:getPath(pos)[v.ix+1], turningRadius:get())
if#innermostHeadland>1then
love.graphics.replaceTransform(graphicsTransform)
love.graphics.setLineWidth(10*lineWidth)
love.graphics.setColor({1, 1, 0, 0.3})
love.graphics.line(innermostHeadland:getUnpackedVertices())
end
end
end
end
end
localfunctiondrawStartLocation()
love.graphics.replaceTransform(graphicsTransform)
love.graphics.setColor(startLocationColor)
love.graphics.setLineWidth(lineWidth)
love.graphics.circle('line', startX, startY, 2)
love.graphics.push()
love.graphics.scale(1, -1)
love.graphics.print('Start location', startX+2, -startY, 0, 1/scale)
love.graphics.pop()
end
localfunctiondrawGraphics()
love.graphics.replaceTransform(graphicsTransform)
love.graphics.setPointSize(pointSize)
drawFields()
ifcourseandnotcontext:hasErrors() then
drawHeadlands()
ifcourse:getCenter() then
drawCenter(course:getCenter())
end
ifnVehicles:get() >1then
for_, pos, pathincourse:pathIterator() do
ifp~=0ornVehicles:get() %2~=0then
-- 0 position makes sense only with odd number of vehicles
drawPath(path, multiVehicleColorForPosition[pos])
end
end
else
drawPath(course:getPath())
end
drawSwath(course:getPath())
end
drawStartLocation()
end
localfunctiondrawContext()
love.graphics.replaceTransform(contextTransform)
love.graphics.setColor(0.2, 0.2, 0.2, 0.6)
localfontsize=12
localy=0
love.graphics.rectangle('fill', 0, 0, 300, (3+#parameters) *fontsize)
love.graphics.setColor(1, 1, 1) -- base color for the coloredText is white (love2D can sometimes be strange)
love.graphics.print({ parameterNameColor, 'To generate, hit ', parameterKeyColor, 'SPACE or right click' }, 0, y)
y=y+fontsize
love.graphics.print({ parameterNameColor, 'To mark baseline edge ', parameterKeyColor, 'hold SHIFT + right click' }, 0, y)
y=y+2*fontsize
for_, pinipairs(parameters) do
love.graphics.print(p:toColoredText(parameterNameColor, parameterKeyColor, parameterValueColor), 0, y)
y=y+fontsize
end
end
localfunctiondrawErrors()
if#errors>0then
love.graphics.replaceTransform(errorTransform)
love.graphics.setColor(1, 0, 0)
localfontsize=12
love.graphics.rectangle('fill', 0, 0, 600, (3+#errors) *fontsize)
love.graphics.setColor(1, 1, 1) -- base color for the coloredText is white (love2D can sometimes be strange)
localy=2*fontsize
for_, einipairs(errors) do
love.graphics.print(e, 0, y)
y=y+fontsize
end
end
end
localfunctiondrawStatus()
love.graphics.setColor(1, 1, 0)
love.graphics.replaceTransform(statusTransform)
localmx, my=love.mouse.getPosition()
localx, y=screenToWorld(mx, my)
love.graphics.print(string.format('%.1f %.1f (%.1f %.1f / %.1f)', x, y, xOffset, yOffset, scale), 0, 0)
ifcurrentVerticesthen
for_, pos, pathincourse:pathIterator() do
ifcurrentVertices[pos] then
highlightPathAroundVertex(path, currentVertices[pos][1])
end
end
drawVertexInfo(currentVertices)
highlightPathToNextRow(currentVertices)
end
end
localfunctiondrawDebugPoints()
ifCourseGenerator.debugPointsthen
for_, pinipairs(CourseGenerator.debugPoints) do
love.graphics.setColor(p.debugColorordebugColor)
love.graphics.replaceTransform(graphicsTransform)
love.graphics.setPointSize(p.smallandpointSize*0.3orpointSize*3)
love.graphics.points(p.x, p.y)
ifp.debugTextthen
drawText(p.x+1, p.y+1, debugTextColor, 1, p.debugText)
end
end
end
end
localfunctiondrawDebugPolylines()
ifCourseGenerator.debugPolylinesthen
love.graphics.push()
love.graphics.replaceTransform(graphicsTransform)
love.graphics.setPointSize(pointSize)
love.graphics.setLineWidth(pointSize)
for_, pinipairs(CourseGenerator.debugPolylines) do
if#p>1then
localcolor=p.debugColorordebugColor
love.graphics.setColor(color)
love.graphics.line(p:getUnpackedVertices())
fori, vinipairs(p) do
drawVertexAsArrow(v)
drawText(v.x+1, v.y+1, color, 1, i)
end
end
end
love.graphics.pop()
end
end
functionlove.draw()
drawGraphics()
drawStatus()
drawContext()
drawErrors()
ifshowDebugInfo:get() then
drawDebugPoints()
drawDebugPolylines()
end
end
------------------------------------------------------------------------------------------------------------------------
--- Input
---------------------------------------------------------------------------------------------------------------------------
functionlove.textinput(key)
for_, pinpairs(parameters) do
p:onKey(key, function()
returntrue
end)
end
ifkey=='' orkey=='y' orkey=='Y' then
-- regenerate when the number of vehicles changes
generate()
end
end
------------------------------------------------------------------------------------------------------------------------
--- Pan/Zoom
---------------------------------------------------------------------------------------------------------------------------
functionlove.wheelmoved(dx, dy)
-- when zooming, keep the window center in place
localwindowCenterX, windowCenterY=screenToWorld(windowWidth/2, windowHeight/2)
scale=scale* (1+dy*0.03)
setOffset(windowCenterX, windowCenterY)
updateTransform()
pointSize=pointSize+pointSize*dy*0.02
end
functionlove.mousepressed(x, y, button, istouch)
ifbutton==1then
dragging=true
end
end
functionlove.mousereleased(x, y, button, istouch)
ifbutton==1then
dragging=false
elseifbutton==2andnotlove.keyboard.isDown('lshift') then
selectFieldUnderCursor()
elseifbutton==3or (button==2andlove.keyboard.isDown('lshift')) then
x, y=love.mouse.getPosition()
baselineX, baselineY=screenToWorld(x, y)
generate()
end
end
functionlove.mousemoved(x, y, dx, dy)
ifdraggingthen
xOffset=xOffset+dx
yOffset=yOffset+dy
updateTransform()
end
mouseTransform:setTransformation(x+20, y+20)
currentVertices=findCurrentVertices(x, y)
end