Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on May 23, 2023. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 510
Expand file tree
/
Copy pathdebug.lua
More file actions
Latest commit
783 lines (691 loc) · 28 KB
/
Copy pathdebug.lua
File metadata and controls
783 lines (691 loc) · 28 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
courseplay=courseplayor {}
-- GENERAL DEBUG
functioncourseplay:debug(str, channel)
ifcourseplay.debugChannelsandchannel~=nilandcourseplay.debugChannels[channel] ~=nilandcourseplay.debugChannels[channel] ==truethen
localtimestamp=getDate( ":%S")
print(timestamp..' [dbg' ..tostring(channel) ..' lp' ..g_updateLoopIndex..'] ' ..str);
end;
end;
-- convenience debug function that expects string.format() arguments,
-- courseplay.debugVehicle( courseplay.DBG_TURN, "fill level is %.1f, mode = %d", fillLevel, mode )
---@paramchannelnumber
functioncourseplay.debugFormat(channel, ...)
ifcourseplay.debugChannelsandchannel~=nilandcourseplay.debugChannels[channel] ~=nilandcourseplay.debugChannels[channel] ==truethen
localupdateLoopIndex=g_updateLoopIndexandg_updateLoopIndexor0
localtimestamp=getDate( ":%S")
print(string.format('%s [dbg%d lp%d] %s', timestamp, channel, updateLoopIndex, string.format( ... )))
end
end
-- convenience debug function to show the vehicle name and expects string.format() arguments,
-- courseplay.debugVehicle( courseplay.DBG_TURN, vehicle, "fill level is %.1f, mode = %d", fillLevel, mode )
---@paramchannelnumber
functioncourseplay.debugVehicle(channel, vehicle, ...)
ifcourseplay.debugChannelsandchannel~=nilandcourseplay.debugChannels[channel] ~=nilandcourseplay.debugChannels[channel] ==truethen
localvehicleName=vehicleandnameNum(vehicle) or"Unknown vehicle"
localupdateLoopIndex=g_updateLoopIndexandg_updateLoopIndexor0
localtimestamp=getDate( ":%S")
print(string.format('%s [dbg%d lp%d] %s: %s', timestamp, channel, updateLoopIndex, vehicleName, string.format( ... )))
end
end
functioncourseplay.info(...)
localupdateLoopIndex=g_updateLoopIndexandg_updateLoopIndexor0
localtimestamp=getDate( ":%S")
print(string.format('%s [info lp%d] %s', timestamp, updateLoopIndex, string.format( ... )))
end
functioncourseplay.infoVehicle(vehicle, ...)
localvehicleName=vehicleandnameNum(vehicle) or"Unknown vehicle"
localupdateLoopIndex=g_updateLoopIndexandg_updateLoopIndexor0
localtimestamp=getDate( ":%S")
print(string.format('%s [info lp%d] %s: %s', timestamp, updateLoopIndex, vehicleName, string.format( ... )))
end
locallines= {
('-'):rep(50),
('_'):rep(50),
('#'):rep(50)
};
functioncourseplay.debugLine(debugChannel, line)
ifdebugChannel==nilorcourseplay.debugChannels[debugChannel] then
line=lineor1;
print(lines[line]);
end;
end;
functiontableShow(t, name, channel, indent, maxDepth)
-- important performance backup: the channel is checked first before proceeding with the compilation of the table
ifchannel~=nilthen--Tommi and courseplay.debugChannels[channel] ~= nil and courseplay.debugChannels[channel] == false then
return;
end;
localcart; -- a container
localautoref; -- for self references
maxDepth=maxDepthor50;
localdepth=0;
-- (RiciLake) returns true if the table is empty
localfunctionisemptytable(t)
returnnext(t) ==nil;
end;
localfunctionbasicSerialize(o)
localso=tostring(o);
localoType=type(o);
ifoType=='function' then
return ("function...")
--[[local info = debug.getinfo(o, 'S')
-- info.name is nil because o is not a calling level
if info.what == 'C' then
return ('"%s, C function"'):format(so);
else
-- the information is defined in a script
return ('"%s, defined in %s (lines %d-%d)"'):format(so, info.source, info.linedefined, info.lastlinedefined);
end]]
elseifoType=='number' then
returnso;
elseifoType=='boolean' then
return ('%s'):format(so);
else
return ('%q'):format(so);
end;
end;
localfunctionaddToCart(value, name, indent, saved, field, curDepth)
indent=indentor''
saved=savedor {}
field=fieldorname
-- cart = cart .. indent .. field
cart=indent..field
-- print(('addToCart(value=%q, name=%q, indent, saved, field=%q, curDepth=%d)'):format(tostring(value), tostring(name), tostring(field), tostring(curDepth)));
iftype(value) ~='table' then
cart=cart..' = ' ..basicSerialize(value) ..';';
print(cart);
else
ifsaved[value] then
cart=cart..' = {}; -- ' ..saved[value] ..' (self reference)';
print(cart);
autoref=autoref..name..' = ' ..saved[value] ..';\n';
else
saved[value] =name;
ifisemptytable(value) then
cart=cart..' = {};';
print(cart);
else
ifcurDepth<=maxDepththen
cart=cart..' = {';
print(cart);
fork, vinpairs(value) do
k=basicSerialize(k);
localfname=string.format('%s[%s]', name, k);
field=string.format('[%s]', k);
-- three spaces between levels
addToCart(v, fname, indent..'\t', saved, field, curDepth+1);
end;
cart=indent..'};';
print(cart);
else
cart=cart..' = { ... };';
print(cart);
end;
end;
end;
end;
end;
name=nameor'__unnamed__';
iftype(t) ~='table' then
returnname..' = ' ..basicSerialize(t);
end;
cart, autoref='', '';
addToCart(t, name, indent, nil, nil, depth+1)
-- return cart .. autoref
print(autoref);
return ('-- %s %s -END- %s --'):format(('#'):rep(40), name, ('#'):rep(40));
end;
functioneval(str)
returnassert(loadstring(str))()
end
--------------------------------------------------
-- MULTIPLAYER DEBUG
courseplay.streamDebugCounter=0;
courseplay.streamWriteFunctions= {
Bool=streamWriteBool;
Float=streamWriteFloat32;
Int=streamWriteInt32;
String=streamWriteString;
};
courseplay.streamReadFunctions= {
Bool=streamReadBool;
Float=streamReadFloat32;
Int=streamReadInt32;
String=streamReadString;
};
functioncourseplay.streamDebugWrite(streamId, varType, value, name)
courseplay.streamDebugCounter=courseplay.streamDebugCounter+1;
stream_debug_counter=stream_debug_counter+1
ifvarType=='Bool' then
value=Utils.getNoNil(value, false);
ifvalue==1then
value=true;
elseifvalue==0then
value=false;
end;
courseplay:debug(('%d: writing %s (bool): %s'):format(stream_debug_counter,nameor"XX", tostring(value)), courseplay.DBG_MULTIPLAYER);
elseifvarType=='Float' then
value=valueor0.0;
courseplay:debug(('%d: writing %s (float): %f'):format(stream_debug_counter,nameor"XX", value), courseplay.DBG_MULTIPLAYER);
elseifvarType=='Int' then
value=valueor0.0;
courseplay:debug(('%d: writing %s (int): %d'):format(stream_debug_counter,nameor"XX", value), courseplay.DBG_MULTIPLAYER);
elseifvarType=='String' then
value=valueor'nil';
courseplay:debug(('%d: writing %s (string): %q'):format(stream_debug_counter,nameor"XX", value), courseplay.DBG_MULTIPLAYER);
end;
courseplay.streamWriteFunctions[varType](streamId, value);
end;
functioncourseplay.streamDebugRead(streamId, varType)
courseplay.streamDebugCounter=courseplay.streamDebugCounter+1;
stream_debug_counter=stream_debug_counter+1
localvalue=courseplay.streamReadFunctions[varType](streamId);
ifvarType=='Bool' then
courseplay:debug(('%d: reading bool: %s'):format(stream_debug_counter, tostring(value)), courseplay.DBG_MULTIPLAYER);
elseifvarType=='Float' then
courseplay:debug(('%d: reading float: %s'):format(stream_debug_counter, tostring(value)), courseplay.DBG_MULTIPLAYER);
elseifvarType=='Int' then
courseplay:debug(('%d: reading int: %s'):format(stream_debug_counter, tostring(value)), courseplay.DBG_MULTIPLAYER);
elseifvarType=='String' then
courseplay:debug(('%d: reading string: %s'):format(stream_debug_counter, tostring(value)), courseplay.DBG_MULTIPLAYER);
end;
returnvalue;
end;
stream_debug_counter=0;
functionstreamDebugWriteFloat32(streamId, value)
value=Utils.getNoNil(value, 0.0)
stream_debug_counter=stream_debug_counter+1
--- courseplay:debug(string.format("%d: writing float: %f",stream_debug_counter, value ),courseplay.DBG_MULTIPLAYER)
streamWriteFloat32(streamId, value)
end
functionstreamDebugWriteBool(streamId, value)
value=Utils.getNoNil(value, false)
ifvalue==1then
value=true
elseifvalue==0then
value=false
end
stream_debug_counter=stream_debug_counter+1
--- courseplay:debug(string.format("%d: writing bool: %s",stream_debug_counter, tostring(value) ),courseplay.DBG_MULTIPLAYER)
streamWriteBool(streamId, value)
end
functionstreamDebugWriteInt32(streamId, value)
value=Utils.getNoNil(value, 0)
stream_debug_counter=stream_debug_counter+1
--- courseplay:debug(string.format("%d: writing int: %d",stream_debug_counter, value ),courseplay.DBG_MULTIPLAYER)
streamWriteInt32(streamId, value)
end
functionstreamDebugWriteString(streamId, value)
value=Utils.getNoNil(value, "")
stream_debug_counter=stream_debug_counter+1
--- courseplay:debug(string.format("%d: writing string: %s",stream_debug_counter, value ),courseplay.DBG_MULTIPLAYER)
streamWriteString(streamId, value)
end
functionstreamDebugReadFloat32(streamId)
stream_debug_counter=stream_debug_counter+1
localvalue=streamReadFloat32(streamId)
--- courseplay:debug(string.format("%d: reading float: %f",stream_debug_counter, value ),courseplay.DBG_MULTIPLAYER)
returnvalue
end
functionstreamDebugReadInt32(streamId)
stream_debug_counter=stream_debug_counter+1
localvalue=streamReadInt32(streamId)
--- courseplay:debug(string.format("%d: reading int: %d",stream_debug_counter, value ),courseplay.DBG_MULTIPLAYER)
returnvalue
end
functionstreamDebugReadBool(streamId)
stream_debug_counter=stream_debug_counter+1
localvalue=streamReadBool(streamId)
--- courseplay:debug(string.format("%d: reading bool: %s",stream_debug_counter, tostring(value)),courseplay.DBG_MULTIPLAYER)
returnvalue
end
functionstreamDebugReadString(streamId)
stream_debug_counter=stream_debug_counter+1
localvalue=streamReadString(streamId)
--- courseplay:debug(string.format("%d: reading string: %s",stream_debug_counter, value ),courseplay.DBG_MULTIPLAYER)
returnvalue
end
--e.g. courseplay:findInTables(g_currentMission ,"g_currentMission", otherId)
functioncourseplay:findInTables(tableToSearchIn , tableToSearchString, valueToSearch)
ifcourseplay.lastSearchedValue==nilthen
courseplay.lastSearchedValue="empty"
end
ifcourseplay.lastSearchedValue==valueToSearchthen--prevent loops in searching
return
else
print("courseplay:findInTables -> searching "..type(valueToSearch)..""..tostring(valueToSearch).." in "..tableToSearchString)
--courseplay.lastSearchedValue = valueToSearch
end
iftype(tableToSearchIn) =="table" then
--Level 0
forindex, valueinpairs(tableToSearchIn) do
ifcourseplay:findInMatchingValues(index,value,valueToSearch) then
print(string.format("courseplay:findInTables -> %s.%s = %s",tableToSearchString,tostring(index),tostring(value)))
elseiftype(value) =="table" then
localtable1=tableToSearchIn[index]
forindex1, value1inpairs(table1) do
ifcourseplay:findInMatchingValues(index1,value1,valueToSearch) then
print(string.format("courseplay:findInTables -> %s.%s.%s = %s",tableToSearchString,tostring(index),tostring(index1),tostring(value1)))
elseiftype(value1) =="table" then
localtable2=table1[index1]
forindex2, value2inpairs(table2) do
ifcourseplay:findInMatchingValues(index2,value2,valueToSearch) then
print(string.format("courseplay:findInTables -> %s.%s.%s.%s = %s",tableToSearchString,tostring(index),tostring(index1),tostring(index2),tostring(value2)))
elseiftype(value2) =="table" then
localtable3=table2[index2]
forindex3, value3inpairs(table3) do
ifcourseplay:findInMatchingValues(index3,value3 ,valueToSearch) then
print(string.format("courseplay:findInTables -> %s.%s.%s.%s.%s = %s",tableToSearchString,tostring(index),tostring(index1),tostring(index2),tostring(index3),tostring(value3)))
elseiftype(value3) =="table" then
localtable4=table3[index3]
forindex4, value4inpairs(table3) do
ifcourseplay:findInMatchingValues(index4,value4,valueToSearch) then
print(string.format("courseplay:findInTables -> %s.%s.%s.%s.%s.%s = %s",tableToSearchString,tostring(index),tostring(index1),tostring(index2),tostring(index3),tostring(index4),tostring(value4)))
elseiftype(value4) =="table" then
localtable5=table4[index4]
forindex5, value5inpairs(table4) do
ifcourseplay:findInMatchingValues(index5,value5,valueToSearch) then
print(string.format("courseplay:findInTables -> %s.%s.%s.%s.%s.%s.%s = %s",tableToSearchString,tostring(index),tostring(index1),tostring(index2),tostring(index3),tostring(index4),tostring(index5),tostring(value5)))
elseiftype(value4) =="table" then
end
end
end
end
end
end
end
end
end
end
end
end
else
print("courseplay:findInTables -> "..tableToSearchString.." is not a table")
return
end
print("courseplay:findInTables -> searching finished")
end
functioncourseplay:findInMatchingValues(index, value1, value2)
localtype1=type(value1)
localtype2=type(value2)
--print("checking "..type1..tostring(value1).."vs "..type2.." "..tostring(value2))
iftype1==type2andvalue1==value2then
returntrue
end
iftype2=="string" then
iftostring(index) ==value2then
returntrue
end
end
returnfalse
end
--- Ugly hack until we figure out why there's no global debug available in FS19
debug= {}
functiondebug.traceback()
return'debug.traceback() not implemented'
end
functiondebug.getinfo()
localresult= {}
result.name='debug.getinfo() not implemented'
result.currentline=0
returnresult
end
-- TODO: there could be a drawTemporaryLine in cpDebug that already has a buffer for all draw data, there's no need to
-- create a separate one
functioncourseplay:showTemporaryMarkers(vehicle)
ifnotcourseplay.debugChannels[courseplay.DBG_AI_DRIVER] thenreturnend
ifvehicle.cp.showMarkersthen
ifvehicle.cp.showMarkers.timer<vehicle.timerthen
-- time is up, remove markers
vehicle.cp.showMarkers=nil
else
cpDebug:drawLine(vehicle.cp.showMarkers.x1, vehicle.cp.showMarkers.y+1, vehicle.cp.showMarkers.z1, 0.5, 0, 0.5,
vehicle.cp.showMarkers.x2, vehicle.cp.showMarkers.y+1, vehicle.cp.showMarkers.z2);
end
end
end
--- start showing a temporary marker line
functioncourseplay:addTemporaryMarker(vehicle, node)
vehicle.cp.showMarkers= {}
vehicle.cp.showMarkers.timer=vehicle.timer+25000
vehicle.cp.showMarkers.x1, _, vehicle.cp.showMarkers.z1=localToWorld(node, -1, 0, 0)
vehicle.cp.showMarkers.x2, _, vehicle.cp.showMarkers.z2=localToWorld(node, 1, 0, 0)
vehicle.cp.showMarkers.y=getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, vehicle.cp.showMarkers.x1, 0, vehicle.cp.showMarkers.z1 );
end
functioncourseplay:showAIMarkers(vehicle)
end
--------------------------------------------------
--- Courseplay Debug Class
--------------------------------------------------
cpDebug= {};
localcpDebug_mt=Class(cpDebug);
addModEventListener(cpDebug);
localmodDirectory=g_currentModDirectory;
localcolorDelta=1/255; -- Used to convert RGB color code into 0-1 float color code
--- Define debug draw types
localdrawTypes= {
Point="Sphere", -- Reference to img/debug/Sphere.i3d
Line="Line" -- Reference to img/debug/Line.i3d
}
--- setup debug items
functioncpDebug:setup()
--- Get the main rootNode
localglobalRootNode=getRootNode();
--- Preset variables
self.activeDrawData= {}; -- Holds the active visible drawItems
self.drawBuffer= {}; -- Holds drawItems for later use
self.drawBufferMax=50; -- Max size of the buffer for each draw type
self.drawPrototypes= {}; -- Holds a object prototype of each draw type
self.itemsToDraw= {}; -- Holds a list of items to draw in the next draw update
self.nextUpdateLoopIndex=0; -- This is when last update was done
self.oldDrawData= {}; -- Holds the left over drawItems that needs to be put in the buffer.
--- Setup drawTypes as prototypes to be cloned from when needed
fordrawType, i3dFileinpairs(drawTypes) do
self.activeDrawData[drawType] = {}; -- Define the draw type in the activeDrawData table
self.drawBuffer[drawType] = {}; -- Define the draw type in the drawBuffer table
self.oldDrawData[drawType] = {}; -- Define the draw type in the oldDrawData table
-- Load the i3d file for the draw type and set it's default settings.
locali3dNode=g_i3DManager:loadSharedI3DFile( 'img/debug/' ..i3dFile..'.i3d' , modDirectory);
localitemNode=getChildAt(i3dNode, 0);
link(globalRootNode, itemNode);
setRigidBodyType(itemNode, 'NoRigidBody');
setTranslation(itemNode, 0, 0, 0);
setVisibility(itemNode, false);
delete(i3dNode);
-- Store the draw type node as a prototype for later use
self.drawPrototypes[drawType] =itemNode;
end;
end;
--- setup debug items on map load
functioncpDebug:loadMap(name)
self:setup();
end
--- Cleanup on exit map
functioncpDebug:deleteMap()
--- Delete activeDrawData itemNodes
ifself.activeDrawDatathen
fordrawType,drawDatasinpairs(self.activeDrawData) do
for_,drawDatainpairs(drawDatas) do
self:deleteDrawItem(drawData.itemNode);
end;
self.drawBuffer[drawType] = {};
end;
end
--- Delete drawBuffer itemNodes
ifself.drawBufferthen
fordrawType,drawDatasinpairs(self.drawBuffer) do
for_,drawDatainpairs(drawDatas) do
self:deleteDrawItem(drawData.itemNode);
end;
self.drawBuffer[drawType] = {};
end;
end
--- Deleting oldDrawData itemNodes is not needed since it's set and reset on each draw
--- Delete draw prototypes itemNodes
ifself.drawPrototypesthen
for_,itemNodeinpairs(self.drawPrototypes) do
self:deleteDrawItem(itemNode);
end;
end
end;
functioncpDebug:update(dt) end; -- TODO: might not be used at all, so we can delete it when sure
--- Draw debug items if there is any to show
functioncpDebug:draw()
ifg_currentMission.pausedthen
return;
end;
--- Clean active drawData
fordrawType,_inpairs(drawTypes) do
if#self.activeDrawData[drawType] >0then
self.oldDrawData[drawType] =self.activeDrawData[drawType];
self.activeDrawData[drawType] = {};
end
end
--- Draw requested items
for_, drawInfoinipairs(self.itemsToDraw) do
-- Get draw data from draw info
localdrawData=self:getDrawData(drawInfo);
-- Continue if we have drawData
ifdrawDatathen
ifdrawData.drawType=="Point" then
-- Update position and color of point
self:updatePointDrawData(drawData);
elseifdrawData.drawType=="Line" then
-- Update position, direction, length and color of line
self:updateLineDrawData(drawData);
end;
-- Store the drawData so we can access them later
table.insert(self.activeDrawData[drawData.drawType], drawData);
end
end
--- Clear items to draw so it's ready for next update
self.itemsToDraw= {};
--- Cleanup leftover drawData
self:storeInBuffer();
--- set next loop to run so we don't double draw
self.nextUpdateLoopIndex=g_updateLoopIndex+1;
--for drawType,_ in pairs(drawTypes) do
-- print("numDrawBuffer[\""..drawType.."\"] = " .. tostring(#self.drawBuffer[drawType]));
--end
end;
--- Get draw data based on info
-- @param drawInfo (table) Contains info to generate drawData
-- @return nil if drawType do not excist in prototypes
-- @return active drawData table
functioncpDebug:getDrawData(drawInfo)
--- Make sure the draw type excist and if not then return nothing.
ifnotself.drawPrototypes[drawInfo.drawType] then
return;
end
localdrawData= {};
--- Pull drawData from an excisting active drawdata if there is one to use
if#self.oldDrawData[drawInfo.drawType] >0then
-- Pull drawData from active list. No need to set visibility here since they are already visible!
drawData=table.remove(self.oldDrawData[drawInfo.drawType]);
--- Pull drawData from drawBuffer if there is one to use
elseif#self.drawBuffer[drawInfo.drawType] >0then
-- Pull buffer drawData
drawData=table.remove(self.drawBuffer[drawInfo.drawType]);
-- Show the object
setVisibility(drawData.itemNode, true);
--- Create a new itemNode from the prototype if none of the above was valid
else
-- Clone prototype to get new object
drawData.itemNode=clone(self.drawPrototypes[drawInfo.drawType], true);
-- Set the draw type
drawData.drawType=drawInfo.drawType;
-- Show the object
setVisibility(drawData.itemNode, true);
end
--- Define new color
drawData.r=drawInfo.r;
drawData.g=drawInfo.g;
drawData.b=drawInfo.b;
--- Set new position based on drawType
ifdrawInfo.drawType=="Point" then
-- Set draw data for a point
drawData.posX=drawInfo.posX
drawData.posY=drawInfo.posY
drawData.posZ=drawInfo.posZ
elseifdrawInfo.drawType=="Line" then
-- Set draw data for a line
drawData.posX1=drawInfo.posX1
drawData.posY1=drawInfo.posY1
drawData.posZ1=drawInfo.posZ1
drawData.posX2=drawInfo.posX2
drawData.posY2=drawInfo.posY2
drawData.posZ2=drawInfo.posZ2
end
returndrawData;
end
--- Update color of object
functioncpDebug:updateObjectColor(drawData)
setShaderParameter(drawData.itemNode, 'shapeColor', drawData.r, drawData.g, drawData.b, 1, false);
end
--- Update position and color of point
functioncpDebug:updatePointDrawData(drawData)
--- Update point position
setTranslation(drawData.itemNode, drawData.posX, drawData.posY, drawData.posZ);
--- Update scale
ifcourseEditor.enabledthen
setScale(drawData.itemNode, courseEditor.pointScale.x, courseEditor.pointScale.y, courseEditor.pointScale.z)
else
setScale(drawData.itemNode, 2, 2, 2)
end
--- Update point color
self:updateObjectColor(drawData);
end
--- Update position, direction, length and color of line
functioncpDebug:updateLineDrawData(drawData)
--- Update line start position
setTranslation(drawData.itemNode, drawData.posX1, drawData.posY1, drawData.posZ1);
--- Get the direction to the end point
localdirX, _, dirZ, distToNextPoint=courseplay:getWorldDirection(drawData.posX1, drawData.posY1, drawData.posZ1, drawData.posX2, drawData.posY2, drawData.posZ2);
--- Get Y rotation
localrotY=MathUtil.getYRotationFromDirection(dirX, dirZ);
--- Get X rotation
localdy=drawData.posY2-drawData.posY1;
localdist2D=MathUtil.vector2Length(drawData.posX2-drawData.posX1, drawData.posZ2-drawData.posZ1);
localrotX=-MathUtil.getYRotationFromDirection(dy, dist2D);
--- Set the direction of the line
setRotation(drawData.itemNode, rotX, rotY, 0);
--- Set the length if the line
setScale(drawData.itemNode, 1, 1, distToNextPoint);
--- Update line color
self:updateObjectColor(drawData);
end
--- Draw a line from point a to b with 0-1 float color
-- @param posXa (float) From point x (world location)
-- @param posYa (float) From point y (world location)
-- @param posZa (float) From point z (world location)
-- @param r (float) Line color Red
-- @param g (float) Line color Green
-- @param b (float) Line color Blue
-- @param posXb (float) To point x (world location)
-- @param posYb (float) To point y (world location)
-- @param posZb (float) To point z (world location)
functioncpDebug:drawLine(posXa, posYa, posZa, r, g, b, posXb, posYb, posZb)
self:addDrawItem("Line", posXa, posYa, posZa, r, g, b, posXb, posYb, posZb);
end
--- Draw a line from point a to b with 0-255 RGB color code
-- @param posXa (float) From point x (world location)
-- @param posYa (float) From point y (world location)
-- @param posZa (float) From point z (world location)
-- @param r (RGB color) Line color Red
-- @param g (RGB color) Line color Green
-- @param b (RGB color) Line color Blue
-- @param posXb (float) To point x (world location)
-- @param posYb (float) To point y (world location)
-- @param posZb (float) To point z (world location)
functioncpDebug:drawLineRGB(posXa, posYa, posZa, r, g, b, posXb, posYb, posZb)
self:addDrawItem("Line", posXa, posYa, posZa, colorDelta*r, colorDelta*g, colorDelta*b, posXb, posYb, posZb);
end
--- Draw a circle around a point with radius, color with 0-1 float color
--- @paramposXnumber From point x (world location)
--- @paramposYnumber From point y (world location)
--- @paramposZnumber From point z (world location)
--- @paramradiusnumber Radius
--- @paramstepsnumber Steps
--- @paramrnumber Line color Red (0..1)
--- @paramgnumber Line color Green (0..1)
--- @parambnumber Line color Blue (0..1)
functioncpDebug:drawCircle(posX, posY, posZ,radius,steps, r, g, b)
steps=stepsor3
fori=1,stepsdo
locala1= ((i-1)/steps)*2*math.pi
locala2= ((i)/steps)*2*math.pi
localc=math.cos(a1) *radius
locals=math.sin(a1) *radius
localx1, y1, z1=posX+c, posY, posZ+s
localc=math.cos(a2) *radius
locals=math.sin(a2) *radius
localx2, y2, z2=posX+c, posY, posZ+s
self:addDrawItem("Line",x1,y1,z1, r, g, b, x2,y2,z2);
end
end
--- Draw a point with 0-1 float color
-- @param posX (float) From point x (world location)
-- @param posY (float) From point y (world location)
-- @param posZ (float) From point z (world location)
-- @param r (float) Point color Red
-- @param g (float) Point color Green
-- @param b (float) Point color Blue
functioncpDebug:drawPoint(posX, posY, posZ, r, g, b)
self:addDrawItem("Point", posX, posY, posZ, r, g, b);
end
--- Draw a point with 0-255 RGB color code
-- @param posX (float) From point x (world location)
-- @param posY (float) From point y (world location)
-- @param posZ (float) From point z (world location)
-- @param r (RGB color) Point color Red
-- @param g (RGB color) Point color Green
-- @param b (RGB color) Point color Blue
functioncpDebug:drawPointRGB(posX, posY, posZ, r, g, b)
self:addDrawItem("Point", posX, posY, posZ, colorDelta*r, colorDelta*g, colorDelta*b);
end
--- Add draw item to the next draw update
-- @param drawType (string) From point x (world location)
-- @param posX1 (float) From point x (world location)
-- @param posY1 (float) From point y (world location)
-- @param posZ1 (float) From point z (world location)
-- @param r (float) Object color Red
-- @param g (float) Object color Green
-- @param b (float) Object color Blue
-- @param posX2 (float) To point x (world location)
-- @param posY2 (float) To point y (world location)
-- @param posZ2 (float) To point z (world location)
functioncpDebug:addDrawItem(drawType, posX1, posY1, posZ1, r, g, b, posX2, posY2, posZ2)
--- If we are in a new update loop and we haven't drawn the previous once, clear the previous draws since we don't want to double draw
ifself.nextUpdateLoopIndex<g_updateLoopIndexthen
self.nextUpdateLoopIndex=g_updateLoopIndex;
self.itemsToDraw= {};
end
localdrawInfo= {};
ifdrawType=="Point" then
-- Set draw data for a point
drawInfo.posX=posX1
drawInfo.posY=posY1
drawInfo.posZ=posZ1
elseifdrawType=="Line" then
-- Set draw data for a line
drawInfo.posX1=posX1
drawInfo.posY1=posY1
drawInfo.posZ1=posZ1
drawInfo.posX2=posX2
drawInfo.posY2=posY2
drawInfo.posZ2=posZ2
else
-- Skip adding an items if they are not defined
return
end
drawInfo.drawType=drawType
drawInfo.r=r
drawInfo.g=g
drawInfo.b=b
--- Add drawData to itemsToDraw table to be drawn in the next draw update
table.insert(self.itemsToDraw, drawInfo);
end
--- Store leftover draw data table into the buffer for later use
functioncpDebug:storeInBuffer()
--- Store leftovers in the buffer by drawType
fordrawType, drawDatasinpairs(self.oldDrawData) do
for_, drawDatainipairs(drawDatas) do
if#self.drawBuffer[drawType] <self.drawBufferMaxthen
-- Hide the object
setVisibility(drawData.itemNode, false);
-- Store leftover items in the buffer
table.insert(self.drawBuffer[drawType], drawData);
else
-- Delete leftover items since the buffer is full
self:deleteDrawItem(drawData.itemNode);
end
end
-- Clean the oldDrawData table of the drawType
self.oldDrawData[drawType] = {};
end
end;
--- Delete draw item
functioncpDebug:deleteDrawItem(itemNode)
setVisibility(itemNode, false);
unlink(itemNode);
delete(itemNode);
end