-
Notifications
You must be signed in to change notification settings - Fork 2
/
Global.-1.ttslua
4335 lines (4014 loc) · 178 KB
/
Global.-1.ttslua
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
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- ~~~~~~
-- Script by dzikakulka
-- Issues, history at: http://github.com/tjakubo2/TTS_xwing
--
-- Based on a work of: Flolania, Hera Vertigo
-- ~~~~~~
-- ~~~~~~
-- Code contributions
-- - Characted width data: Indimeco
-- - http://github.com/Indimeco/Tabletop-Simulator-Misc
-- ~~~~~~
-- Should the code execute print functions or skip them?
-- This should be set to false on every release
print_debug = true
TTS_print = print
function print(...)
if print_debug == true then
TTS_print(table.unpack({...}))
end
end
-- Vector manipulation
#include TTS_lib/Vector/Vector
-- Standard libraries extentions
#include TTS_lib/Util/Util
-- Standard event handling
#include TTS_lib/EventSub/EventSub
-- Object type abstraction
#include TTS_lib/ObjType/ObjType
-- Save/load management
#include TTS_lib/SaveManager/SaveManager
-- Component sizes, unit conversion methods
#include TTS_xwing/src/Dimensions
-- Data on ship types, sizes etc.
#include TTS_xwing/src/ModelDB
-- Modules API, must be loaded last
#include TTS_xwing/src/API
--------
-- MISC FUNCTIONS
-- Dumbest TTS issue ever workaround
function TTS_Serialize(pos)
return {pos[1], pos[2], pos[3]}
end
ObjType.AddType('ship', function(obj)
return (obj.tag == 'Figurine')
end)
ObjType.AddType('token', function(obj)
return (obj.tag == 'Chip' or (obj.getVar('__XW_Token') and obj.getVar('__XW_TokenIdle')))
end)
ObjType.AddType('dial', function(obj)
return (obj.tag == 'Card' and XW_cmd.CheckCommand(obj.getDescription()) == 'move')
end)
-- Dud function for info buttons and not yet written sections where Lua complains about no code
function dummy() return end
-- END MISC FUNCTIONS
--------
--------
-- COMMAND HANDLING MODULE
-- Sanitizes input (more like ignores anything not explicitly allowed)
-- Allows other modules to add available commands and passes their execution where they belong
XW_cmd = {}
XW_cmd.commandLUT = {}
-- Table of valid commands: their patterns and general types
XW_cmd.ValidCommands = {}
-- Add given regen expression as a valid command for processing
XW_cmd.AddCommand = function(cmdRegex, type)
-- When adding available commands, assert beggining and end of string automatically
if cmdRegex:sub(1,1) ~= '^' then cmdRegex = '^' .. cmdRegex end
if cmdRegex:sub(-1,-1) ~= '$' then cmdRegex = cmdRegex .. '$' end
table.insert(XW_cmd.ValidCommands, {string.lower(cmdRegex), type})
end
-- Check if command is registered as valid
-- If it is return its type identifier, if not return nil
XW_cmd.CheckCommand = function(cmd)
-- Trim whitespaces
cmd = string.lower(cmd:match( "^%s*(.-)%s*$" ))
local type = nil
if XW_cmd.commandLUT[cmd] then
return XW_cmd.commandLUT[cmd]
end
-- Resolve command type
for k,pat in pairs(XW_cmd.ValidCommands) do
if cmd:match(pat[1]) ~= nil then
type = pat[2]
break
end
end
XW_cmd.commandLUT[cmd] = type
return type
end
-- (special function)
-- Purge all save data (everything that goes to onSave)
XW_cmd.AddCommand('purgeSave', 'special')
XW_cmd.PurgeSave = function()
MoveModule.moveHistory = {}
while DialModule.ActiveSets[1] ~= nil do
DialModule.RemoveSet(DialModule.ActiveSets[1].ship)
end
end
-- (special function)
-- Print ship hitory
XW_cmd.AddCommand('hist', 'special')
XW_cmd.ShowHist = function(ship)
MoveModule.PrintHistory(ship)
end
-- (special function)
-- Check for typical issues with a ship
XW_cmd.AddCommand('diag', 'special')
XW_cmd.Diagnose = function(ship)
-- Check and unlock XW_cmd lock if it's on
local issueFound = false
if ObjType.IsOfType(ship, 'ship') ~= true then return end
if XW_cmd.isReady(ship) ~= true then
XW_cmd.SetReady(ship)
printToAll(ship.getName() .. '\'s deadlock resolved', {0.1, 0.1, 1})
issueFound = true
end
-- Delete lingering buttons
if ship.getButtons() ~= nil then
ship.clearButtons()
printToAll(ship.getName() .. '\'s lingering buttons deleted', {0.1, 0.1, 1})
issueFound = true
end
-- If ship is tagged as sliding now, reset
if ship.getVar('slideOngoing') == true then
ship.setVar('slideOngoing', false)
printToAll(ship.getName() .. '\'s stuck slide resolved', {0.1, 0.1, 1})
issueFound = true
end
-- If ship has unrecognized model and said that before, remind
if ship.getVar('missingModelWarned') == true then
printToAll('I hope you do remember that I told you about ' .. ship.getName() .. '\'s model being unrecognized when it was first moved/used', {0.1, 0.1, 1})
issueFound = true
-- If its model is unrecognized and haven't been used yet, notify
elseif shipType == 'Unknown' then
local shipType = ModelDB.GetData(ship).type
printToAll(ship.getName() .. '\'s ship type not reconized. If this model was taken from Squad Builder or Collection, notify author of the issue.', {1, 0.1, 0.1})
issueFound = true
end
-- CHECK SHIP DIAL SET
local set = DialModule.GetSet(ship)
local dialError = false
local dialErrorCode = ''
-- Restore active dial in case it's been lost somewhere
DialModule.RestoreActive(ship)
if set ~= nil and set.dialSet ~= nil then
for k, dInfo in pairs(set.dialSet) do
-- If any dial ref in set is nil, critical error
if dInfo.dial == nil then
dialError = true
dialErrorCode = 'nilDial'
-- If any dial is not set to this ship, critical error
elseif dial.getVar('assignedShip') ~= ship then
dialError = true
dialErrorCode = 'wrongShip'
-- If dial is stuck to slide mode, reset
elseif dial.getVar('slideOngoing') == true then
printToAll(ship.getName() .. '\'s dial stuck slide resolved', {0.1, 0.1, 1})
issueFound = true
end
end
end
-- Critical error notify
if dialError == true then
printToAll( ship.getName() .. '\'s dial data corrupted - it\'s bad, delete model and dials and reassign new set (may need table reload, notify author of this)' ..
' [' .. dialErrorCode .. ']', {1, 0.1, 0.1})
issueFound = true
end
-- No issues found
if issueFound ~= true then
printToAll(ship.getName() .. ' looks OK', {0.1, 1, 0.1})
end
end
-- Process provided command on a provided object
-- Return true if command has been executed/started
-- Return false if object cannot process commands right now or command was invalid
XW_cmd.Process = function(obj, cmd)
-- Trim whitespaces
cmd = cmd:match( "^%s*(.-)%s*$" )
-- Resolve command type
local type = XW_cmd.CheckCommand(cmd)
-- Process special commands without taking lock into consideration
if type == nil then
return false
elseif type == 'special' then
if cmd == 'diag' then
XW_cmd.Diagnose(obj)
elseif cmd == 'purgeSave' then
XW_cmd.PurgeSave()
elseif cmd == 'hist' then
XW_cmd.ShowHist(obj)
end
end
-- Return if not ready, else process
if XW_cmd.isReady(obj) ~= true then
return false
end
if type == 'demoMove' then
MoveModule.DemoMove(cmd:sub(3, -1), obj)
elseif type == 'move' or type == 'actionMove' then
local info = MoveData.DecodeInfo(cmd, obj)
MoveModule.PerformMove(cmd, obj)
elseif type == 'historyHandle' then
if cmd == 'q' or cmd == 'undo' then
MoveModule.UndoMove(obj)
elseif cmd == 'z' or cmd == 'redo' then
MoveModule.RedoMove(obj)
elseif cmd == 'keep' then
MoveModule.SaveStateToHistory(obj, false)
elseif cmd:sub(1,8) == 'restore#' then
local keyNum = tonumber(cmd:sub(9, -1))
MoveModule.Restore(obj, keyNum)
end
elseif type == 'dialHandle' then
if cmd == 'sd' then
DialModule.SaveNearby(obj)
elseif cmd == 'rd' then
DialModule.RemoveSet(obj)
end
elseif type == 'rulerHandle' then
RulerModule.ToggleRuler(obj, string.upper(cmd))
elseif type == 'action' then
DialModule.PerformAction(obj, cmd)
elseif type == 'bombDrop' then
BombModule.ToggleDrop(obj, cmd)
end
obj.setDescription('')
return true
end
-- Is object not processing some commands right now?
XW_cmd.isReady = function(obj)
return (obj.getVar('cmdBusy') ~= true)
end
-- Flag the object as processing commands to ignore any in the meantime
XW_cmd.SetBusy = function(obj)
if XW_cmd.isReady(obj) ~= true then
print('Nested process on ' .. obj.getName())
end
obj.setVar('cmdBusy', true)
end
-- Flag the object as ready to process next command
XW_cmd.SetReady = function(obj)
if XW_cmd.isReady(obj) == true then
print('Double ready on ' .. obj.getName())
end
obj.setVar('cmdBusy', false)
end
--------
-- MOVEMENT DATA MODULE
-- Stores and processes data about moves
-- NOT aware of any ship position, operation solely on relative movements
-- Used for feeding data about a move to a higher level movement module
-- Exclusively uses milimeters and degrees for values, needs external conversion
-- Possible commands supported by this module
XW_cmd.AddCommand('[sk][012345][r]?', 'move') -- Straights/Koiograns + stationary moves
XW_cmd.AddCommand('b[rle][123][str]?', 'move') -- Banks + segnor and reverse versions
XW_cmd.AddCommand('t[rle][123][str]?', 'move') -- Turns + segnor, talon and reverse versions
XW_cmd.AddCommand('x[rle][fb]?', 'actionMove') -- Barrel rolls
XW_cmd.AddCommand('s[12345]b', 'actionMove') -- Boost straights
XW_cmd.AddCommand('b[rle][123]b', 'actionMove') -- Boost banks
XW_cmd.AddCommand('t[rle][123]b', 'actionMove') -- Boost turns
XW_cmd.AddCommand('c[srle]', 'actionMove') -- Decloaks side middle + straight
XW_cmd.AddCommand('c[rle][fb]', 'actionMove') -- Decloaks side forward + backward
XW_cmd.AddCommand('ch[rle][fb]', 'actionMove') -- Echo's bullshit
XW_cmd.AddCommand('chs[rle]', 'actionMove') -- Echo's bullshit, part 2
XW_cmd.AddCommand('vr[rle][fb]', 'actionMove') -- StarViper Mk.II rolls
MoveData = {}
-- Lookup table for most of the moves
-- Generated using Matlab, source: https://github.com/tjakubo2/xwing_traj
-- Stored on another object to reduce clutter, passsed on load
MoveData.LUT = {}
MoveData.onLoad = function()
for k,obj in pairs(getAllObjects()) do
if obj.getName() == 'MoveLUT' then
MoveData.LUT.Parse(obj)
end
end
end
EventSub.Register('onLoad', MoveData.onLoad)
MoveData.LUT.Parse = function(object)
MoveData.LUT.Data = object.call('ParseLUT', {})
end
-- Max part value for partial moves
-- Part equal to this is a full move
-- Value is largely irrelevant since part can be a fraction (any kind of number really)
MoveData.partMax = 1000
-- Construct data from a lookup table entry
-- Move info provided from MoveData.DecodeInfo
-- Return format: {xPos_offset, yPos_offset, zPos_offset, yRot_offset}
-- Linear interpolation between points in lookup table
--
-- Only returns data for RIGHT direction move (if applies)
-- Doesn't take any segnor, talon versions etc into considerations
-- Above things are considered MODIFIERS with functions to apply them defined futher
MoveData.LUT.ConstructData = function(moveInfo, part)
if part == nil then
part = MoveData.partMax
end
if moveInfo.speed == 0 then
return {0, 0, 0, 0}
end
part = math.clamp(part, 0, MoveData.partMax)
local LUTtable = MoveData.LUT.Data[moveInfo.size .. 'Base'][moveInfo.type][moveInfo.speed]
local LUTindex = (part/MoveData.partMax)*LUTtable.dataNum
if LUTindex < 1 then LUTindex = 1 end
-- Interpolation between two nearest indexes
local aProp = LUTindex - math.floor(LUTindex)
local bProp = 1 - aProp
local outPos = Vect.Sum(Vect.Scale(LUTtable.posXZ[math.floor(LUTindex)], bProp), Vect.Scale(LUTtable.posXZ[math.ceil(LUTindex)], aProp))
local outRot = (LUTtable.rotY[math.floor(LUTindex)] * bProp) + (LUTtable.rotY[math.ceil(LUTindex)] * aProp)
local outData = {outPos[1], 0, outPos[2], outRot}
return outData
end
-- Get true move length from LUT data *IN MILIMETERS*
-- True as in trajectory length, not distance between start and end
-- (stored in LUT to reduce load here)
MoveData.MoveLength = function(moveInfo)
if moveInfo.traits.part == false then
return nil
elseif moveInfo.speed == 0 then
return 0
else
return MoveData.LUT.Data[moveInfo.size .. 'Base'][moveInfo.type][moveInfo.speed].length
end
end
-- Regex match for moves that support sliding base after execution
MoveData.slideMatchTable = { 'x[rle]',
'x[rle][fb]?',
'c[rle][fb]?',
't[rle][123]t',
'b[rle][123]t',
'ch[rle][fb]',
'chadj',
'vr[rle][fb]',
'vradj' }
-- Check if move allows sliding based on above table
-- Argumant can be either move code or move info as per MoveData.DecodeInfo
MoveData.IsSlideMove = function(moveInfoCode)
local code = nil
if type(moveInfoCode) == 'table' and type(moveInfoCode.code) == 'string' then
code = moveInfoCode.code
elseif type(moveInfoCode) == 'string' then
code = moveInfoCode
else
print('MoveData.IsSlideMove: arg of invalid type')
end
local matched = false
for k,pat in pairs(MoveData.slideMatchTable) do
if code:match(pat) ~= nil then
matched = true
break
end
end
return matched
end
-- Get slide length (if move supports sliding) IN MILIMETERS
MoveData.SlideLength = function(moveInfo)
if type(moveInfo) ~= 'table' then
print('MoveData.SlideLength: arg of invalid type')
end
if moveInfo.traits.slide ~= true then
return nil
else
baseSize = mm_baseSize[moveInfo.size]
if moveInfo.type == 'roll' then
return baseSize
elseif moveInfo.extra == 'talon' or moveInfo.type == 'echo' or moveInfo.type == 'viper' then
return baseSize/2
end
end
return nil
end
-- Get the position at which slide after move should start
-- This is the position when ship is slid as far BACK as possible (part=0 in later processing)
MoveData.SlideMoveOrigin = function(moveInfo)
local code = moveInfo.code
local baseSize = mm_baseSize[moveInfo.size]
local data = nil
if moveInfo.type == 'roll' then
-- Rolls are straights rotated 90deg sideways
local ang = -90
if moveInfo.dir == 'right' then ang = 90 end
if moveInfo.size == 'small' then
data = MoveData.LUT.ConstructData({type='straight', speed=moveInfo.speed, size=moveInfo.size, code='s'..moveInfo.speed})
else
data = {0, 0, baseSize + 20, 0}
end
data = MoveData.RotateEntry(data, ang)
data[4] = data[4] - ang
data[3] = data[3] - MoveData.SlideLength(moveInfo)/2
elseif moveInfo.type == 'echo' or moveInfo.type == 'viper' then
-- FUCKING ECHO is appropriate bank 2 rotated 90deg sideways
-- Viper is same but bank 1
local ang = -90
if moveInfo.dir == 'right' then
ang = 90
end
local bankSpeed = 2
if moveInfo.type == 'viper' then bankSpeed = 1 end
data = MoveData.LUT.ConstructData({type='bank', speed=bankSpeed, size='small', code='br' .. tostring(bankSpeed)})
if (moveInfo.dir == 'left' and moveInfo.extra == 'backward') or (moveInfo.dir == 'right' and moveInfo.extra == 'forward') then
data = MoveData.LeftVariant(data)
end
data = MoveData.RotateEntry(data, ang)
if moveInfo.dir == 'right' then
data[4] = data[4] - 90
else
data[4] = data[4] + 90
end
data[3] = data[3] - MoveData.SlideLength(moveInfo)/2
elseif moveInfo.type == 'turn' and moveInfo.extra == 'talon' then
-- Talon roll are simply talons rolls slid forward
-- (forward cause this is initial position relative and talons do a 180)
data = MoveData.LUT.ConstructData(moveInfo)
data[3] = data[3] + MoveData.SlideLength(moveInfo)/2
if moveInfo.dir == 'left' then
data = MoveData.LeftVariant(data)
end
data = MoveData.TurnInwardVariant(data)
elseif moveInfo.type == 'bank' and moveInfo.extra == 'talon' then
-- Bank talon rolls go half slide length at 45 deg
data = MoveData.LUT.ConstructData(moveInfo)
local off = Vect.RotateDeg({0, 0, MoveData.SlideLength(moveInfo)/2}, -45)
data = Vect.Sum(data, {off[1], off[2], off[3], 0})
if moveInfo.dir == 'left' then
data = MoveData.LeftVariant(data)
end
data = MoveData.TurnInwardVariant(data)
end
return data
end
-- Get the offset sliding by part/maxPart applies to ship position
-- MoveData.SlideMoveOrigin and this provide total offset for some part of a slide
-- THIS IS ZERO-SLIDE-POSITION RELATIVE
MoveData.SlidePartOffset = function(moveInfo, part)
if moveInfo.type == 'echo' or moveInfo.type == 'viper' then
if moveInfo.extra == 'adjust' then
-- Echo 2nd adjust is similair to a barrel roll
-- This is when ship is slid against the template
return {0, 0, MoveData.SlideLength(moveInfo)*(part/MoveData.partMax), 0}
else
-- Echo 1st adjust is a diagonal 45deg line through base
-- This is when template is slid against ship before it is moved
local dirVec = {-1, 0, 1, 0}
if (moveInfo.dir == 'left' and moveInfo.extra == 'backward') or (moveInfo.dir == 'right' and moveInfo.extra == 'forward') then
dirVec[1] = -1*dirVec[1]
end
dirVec = Vect.SetLength(dirVec, MoveData.SlideLength(moveInfo))
return Vect.Scale(dirVec, part/MoveData.partMax)
end
else
-- Normal rolls/talons are simply slid forward
return {0, 0, MoveData.SlideLength(moveInfo)*(part/MoveData.partMax), 0}
end
end
-- Convert an entry from milimeters to in-game units
MoveData.ConvertDataToIGU = function(entry)
return {Convert_mm_igu(entry[1]), Convert_mm_igu(entry[2]), Convert_mm_igu(entry[3]), entry[4]}
end
-- Change an entry to a left-heading version
MoveData.LeftVariant = function(entry)
return {-1*entry[1], entry[2], entry[3], -1*entry[4]}
end
-- Change an entry to be k-turn like (+180deg rot at the end)
MoveData.TurnAroundVariant = function(entry)
return {entry[1], entry[2], entry[3], entry[4]+180}
end
-- Change an entry to be talon-roll like (+90deg INWARD rot at the end)
MoveData.TurnInwardVariant = function(entry)
local dir = 0
if entry[1] > 0 then dir = 90
elseif entry[1] < 0 then dir = -90 end
return {entry[1], entry[2], entry[3], entry[4]+dir}
end
-- Change an entry to be reverse type
MoveData.ReverseVariant = function(entry)
return {entry[1], entry[2], -1*entry[3], -1*entry[4]}
end
-- Rotate an entry by given degrees
-- Helps define rolls as straights rotated 90deg sideways etc
MoveData.RotateEntry = function(entry, angDeg)
local rotEntry = Vect.RotateDeg(entry, angDeg)
return {rotEntry[1], rotEntry[2], rotEntry[3], entry[4]+angDeg}
end
-- Apply move modifiers that happen even if move is partial
MoveData.ApplyBasicModifiers = function(entry, info)
local out = table.shallowcopy(entry)
if info.dir == 'left' then
out = MoveData.LeftVariant(out)
end
if info.extra == 'reverse' then
out = MoveData.ReverseVariant(out)
end
return out
end
-- Apply move modifiers that only happen if move is performed fully
MoveData.ApplyFinalModifiers = function(entry, info)
local out = table.shallowcopy(entry)
if info.extra == 'talon' then
out = MoveData.TurnInwardVariant(out)
elseif info.extra == 'koiogran' or info.extra == 'segnor' then
out = MoveData.TurnAroundVariant(out)
end
return out
end
-- Decode a move command into table with type, direction, speed etc info
-- TODO make a lookup table?
MoveData.DecodeInfo = function (move_code, ship)
local info = {
-- [option1] [option2] ... [optionN] // [errorOption]
type='invalid', -- [straight] [bank] [turn] [roll] [echo] // [invalid]
speed=nil, -- [1] [2] [3] [4] [5] // [nil]
dir=nil, -- [left] [right] [nil]
extra=nil, -- [koiogran] [segnor] [talon] [reverse] [straight] [forward] [backward] [nil]
traits =
{ slide=false, -- [true] [false] if this move can be slid afterwards
full=false, -- [true] [false] if this move can be attempted as a full move
part=false -- [true] [false] if this move can be attempted as a partial move
},
size=nil, -- [small] [large] // [nil]
note=nil, -- [string] eg. 'banked xxx' // [nil]
collNote=nil, -- [string] eg. 'tried to do xxx' // [nil]
code=move_code -- [string] eg. 'be2' // [nil]
}
-- Set to slide-able if it is
if MoveData.IsSlideMove(move_code) then
info.traits.slide = true
end
info.size = ModelDB.GetData(ship).baseSize or 'small'
-- Straights and koiograns, regular stuff
if move_code:sub(1,1) == 's' or move_code:sub(1,1) == 'k' then
info.type = 'straight'
info.speed = tonumber(move_code:sub(2,2))
info.traits.full = true
info.traits.part = true
if move_code:sub(1,1) == 'k' then
info.extra = 'koiogran'
info.note = 'koiogran turned ' .. info.speed
info.collNote = 'tried to koiogran turn ' .. info.speed
elseif move_code:sub(-1,-1) == 'r' then
info.extra = 'reverse'
info.note = 'flew reverse ' .. info.speed
info.collNote = 'tried to fly reverse ' .. info.speed
elseif move_code:sub(-1,-1) == 'b' then
info.traits.part = false
local boostSpd = ''
if info.speed > 1 then
boostSpd = ' ' .. info.speed
end
info.note = 'boosted straight' .. boostSpd
info.collNote = 'tried to boost straight' .. boostSpd
else
info.note = 'flew straight ' .. info.speed
info.collNote = 'tried to fly straight ' .. info.speed
end
if info.speed == 0 then
info.traits.part = false
if info.extra == 'koiogran' then
info.note = 'turned around'
else
info.note = 'is stationary'
end
end
-- Banks, regular stuff
elseif move_code:sub(1,1) == 'b' then
info.type = 'bank'
info.dir = 'right'
info.speed = tonumber(move_code:sub(3,3))
info.traits.full = true
info.traits.part = true
if move_code:sub(2,2) == 'l' or move_code:sub(2,2) == 'e' then
info.dir = 'left'
end
if move_code:sub(-1,-1) == 's' then
info.extra = 'segnor'
info.note = 'segnor looped ' .. info.dir .. ' ' .. info.speed
info.collNote = 'tried to segnor loop ' .. info.dir .. ' ' .. info.speed
elseif move_code:sub(-1,-1) == 't' then
info.extra = 'talon'
info.note = 'talon rolled (bank template) ' .. info.dir .. ' ' .. info.speed
info.collNote = 'tried to talon roll (bank template) ' .. info.dir .. ' ' .. info.speed
elseif move_code:sub(-1,-1) == 'r' then
info.extra = 'reverse'
info.note = 'flew reverse bank ' .. info.dir .. ' ' .. info.speed
info.collNote = 'tried to fly reverse bank ' .. info.dir .. ' ' .. info.speed
elseif move_code:sub(-1,-1) == 'b' then
info.traits.part = false
local boostSpd = ''
if info.speed > 1 then
boostSpd = ' ' .. info.speed
end
info.note = 'boosted ' .. info.dir .. boostSpd
info.collNote = 'tried to boost ' .. info.dir .. boostSpd
else
info.note = 'banked ' .. info.dir .. ' ' .. info.speed
info.collNote = 'tried to bank ' .. info.dir .. ' ' .. info.speed
end
-- Turns, regular stuff
elseif move_code:sub(1,1) == 't' then
info.type = 'turn'
info.dir = 'right'
info.speed = tonumber(move_code:sub(3,3))
info.traits.full = true
info.traits.part = true
if move_code:sub(2,2) == 'l' or move_code:sub(2,2) == 'e' then
info.dir = 'left'
end
if move_code:sub(-1,-1) == 't' then
info.extra = 'talon'
info.note = 'talon rolled ' .. info.dir .. ' ' .. info.speed
info.collNote = 'tried to talon roll ' .. info.dir .. ' ' .. info.speed
elseif move_code:sub(-1,-1) == 's' then
info.extra = 'segnor'
info.note = 'segnor looped (turn template) ' .. info.dir .. ' ' .. info.speed
info.collNote = 'tried to segnor loop (turn template) ' .. info.dir .. ' ' .. info.speed
elseif move_code:sub(-1,-1) == 'r' then
info.extra = 'reverse'
info.note = 'flew reverse turn ' .. info.dir .. ' ' .. info.speed
info.collNote = 'tried to fly reverse turn ' .. info.dir .. ' ' .. info.speed
elseif move_code:sub(-1,-1) == 'b' then
info.traits.part = false
local boostSpd = ''
if info.speed > 1 then
boostSpd = ' ' .. info.speed
end
info.note = 'boosted (turn template) ' .. info.dir .. boostSpd
info.collNote = 'tried to boost (turn template) ' .. info.dir .. boostSpd
else
info.note = 'turned ' .. info.dir .. ' ' .. info.speed
info.collNote = 'tried to turn ' .. info.dir .. ' ' .. info.speed
end
-- Barrel rolls and decloaks, spaghetti
elseif move_code:sub(1,2) == 'ch' or move_code:sub(1,2) == 'vr' then
-- Echo's fucking bullshit which goes against ALL the standards
-- StarViper handled the same
info.type = 'echo'
if move_code:sub(1,2) == 'vr' then
info.type = 'viper'
end
info.dir = 'right'
info.extra = 'forward'
if move_code:sub(4,4) == 'b' then
info.extra = 'backward'
end
if move_code:sub(3,3) == 'l' or move_code:sub(3,3) == 'e' then
-- Ones going right/left
info.dir = 'left'
elseif move_code:sub(3,3) == 's' then
-- Ones going forward
info = MoveData.DecodeInfo('b' .. move_code:sub(4,4) .. '2', ship)
info.traits.part = false
info.code = move_code
end
if move_code:sub(1,2) == 'ch' then
-- Echo dedscriptions
if info.type == 'echo' then
info.note = 'dechocloaked ' .. info.dir .. ' ' .. info.extra
info.collNote = 'tried to dechocloak ' .. info.dir .. ' ' .. info.extra
else
info.note = 'dechocloaked forward ' .. info.dir
info.collNote = 'tried to dechocloak forward ' .. info.dir
end
else
-- SV descriptions
info.note = 'bank rolled ' .. info.dir .. ' ' .. info.extra
info.collNote = 'tried to bank roll ' .. info.dir .. ' ' .. info.extra
end
-- Special 2nd adjust move
if move_code == 'chadj' or move_code == 'vradj' then
info.extra = 'adjust'
end
elseif move_code:sub(1,1) == 'x' or move_code:sub(1,1) == 'c' then
-- Rolls
info.type = 'roll'
info.dir = 'right'
info.speed = 1
if move_code:sub(2,2) == 'l' or move_code:sub(2,2) == 'e' then
info.dir = 'left'
end
info.note = 'barrel rolled'
info.collNote = 'tried to barrel roll'
-- Decloaks
-- Straigh decloak is treated as a roll before, now just return straight 2 data
if move_code:sub(2,2) == 's' then
info.type = 'straight'
info.speed = 2
info.traits.full = true
info.note = 'decloaked forward'
info.collNote = 'tried to decloak forward'
info.dir = nil
-- Side decloak is a barrel roll, but with 2 speed
elseif move_code:sub(1,1) == 'c' then
info.note = 'decloaked'
info.collNote = 'tried to decloak'
info.speed = 2
end
-- Forward/backward modifiers
if info.type ~= 'straight' then
if move_code:sub(-1,-1) == 'f' then
info.extra = 'forward'
info.note = info.note .. ' forward ' .. info.dir
info.collNote = info.collNote .. ' forward ' .. info.dir
elseif move_code:sub(-1,-1) == 'b' then
info.extra = 'backward'
info.note = info.note .. ' backward ' .. info.dir
info.collNote = info.collNote .. ' forward ' .. info.dir
else
info.note = info.note .. ' ' .. info.dir
info.collNote = info.collNote .. ' ' .. info.dir
end
end
end
return info
end
-- Get the offset data for a full move
-- Return format: {xPos_offset, yPos_offset, zPos_offset, yRot_offset}
MoveData.DecodeFullMove = function(move_code, ship)
local data = {}
local info = MoveData.DecodeInfo(move_code, ship)
if info.type == 'invalid' then
print('MoveData.DecodeFullMove: invalid move type')
return {0, 0, 0, 0}
else
data = MoveData.DecodePartMove(move_code, ship, MoveData.partMax)
end
data = MoveData.ApplyFinalModifiers(data, info)
return data
end
-- Get the offset data for a partial move
-- Return format: {xPos_offset, yPos_offset, zPos_offset, yRot_offset}
MoveData.DecodePartMove = function(move_code, ship, part)
local data = {}
local info = MoveData.DecodeInfo(move_code, ship)
part = math.clamp(part, 0, MoveData.partMax)
if info.type == 'invalid' then
print('MoveData.DecodePartMove: invalid move type')
return {0, 0, 0, 0}
end
data = MoveData.LUT.ConstructData(info, part)
data = MoveData.ApplyBasicModifiers(data, info)
return data
end
-- Get the offset data for a partial slide
-- Return format: {xPos_offset, yPos_offset, zPos_offset, yRot_offset}
MoveData.DecodePartSlide = function(move_code, ship, part)
local info = MoveData.DecodeInfo(move_code, ship)
local slideOrigin = MoveData.SlideMoveOrigin(info)
local offset = Vect.RotateDeg(MoveData.SlidePartOffset(info, part), slideOrigin[4])
return Vect.Sum(slideOrigin, offset)
end
-- END MOVEMENT DATA MODULE
--------
--------
-- MAIN MOVEMENT MODULE
-- Lets us move ships around and handles what comes with moving
MoveModule = {}
-- Convert a typical entry from MoveData functions
-- (this: {xPos_offset, yPos_offset, zPos_offset, yRot_offset} )
-- to a real ship position in world
MoveModule.EntryToPos = function(entry, shipPos)
local basePos = nil
local baseRot = nil
if type(shipPos) == 'userdata' then
basePos = shipPos.getPosition()
baseRot = shipPos.getRotation()
elseif type(shipPos) == 'table' then
basePos = shipPos.pos
baseRot = shipPos.rot
end
local finalPos = MoveData.ConvertDataToIGU(entry)
local finalRot = entry[4] + baseRot[2]
finalPos = Vect.RotateDeg(finalPos, baseRot[2]+180)
finalPos = Vect.Sum(basePos, finalPos)
return {pos=finalPos, rot={0, finalRot, 0}}
end
-- Get the position for a ship if it did a full move
-- Returned position and rotation are ready to feed TTS functions with
MoveModule.GetFullMove = function(move, ship)
local entry = MoveData.DecodeFullMove(move, ship)
return MoveModule.EntryToPos(entry, ship)
end
-- Get the position for a ship if it did a part of a move
-- Returned position and rotation are ready to feed TTS functions with
MoveModule.GetPartMove = function(move, ship, part)
local entry = MoveData.DecodePartMove(move, ship, part)
return MoveModule.EntryToPos(entry, ship)
end
-- Get the position for a ship if it did a part of a slide
-- Returned position and rotation are ready to feed TTS functions with
MoveModule.GetPartSlide = function(move, ship, part)
local entry = MoveData.DecodePartSlide(move, ship, part)
return MoveModule.EntryToPos(entry, ship)
end
-- HISTORY HANDLING:
-- Lets us undo, redo and save positions a ship was seen at
-- History table: {ship=shipRef, actKey=keyOfHistoryEntryShipWasLastSeenAt (._.), history=entryList}
-- Entry list: {entry1, entry2, entry3, ...}
-- Entry: {pos=position, rot=rotation, move=moveThatGotShipHere, part=partOfMovePerformed}
MoveModule.moveHistory = {}
-- Hostory-related commads
XW_cmd.AddCommand('[qz]', 'historyHandle')
XW_cmd.AddCommand('undo', 'historyHandle')
XW_cmd.AddCommand('redo', 'historyHandle')
XW_cmd.AddCommand('keep', 'historyHandle')
-- Return history of a ship
MoveModule.GetHistory = function(ship)
for k,hist in pairs(MoveModule.moveHistory) do
if hist.ship == ship then
return hist
end
end
table.insert(MoveModule.moveHistory, {ship=ship, actKey=0, history={}})
return MoveModule.GetHistory(ship)
end
-- Erase all history "forward" from the current state
-- Happens when you undo and then do a move - all positions you undid are lost
MoveModule.ErasePastCurrent = function(ship)
local histData = MoveModule.GetHistory(ship)
local k=1
while histData.history[histData.actKey + k] ~= nil do
histData.history[histData.actKey + k] = nil
k = k+1
end
end
-- Print history, just for debug
MoveModule.PrintHistory = function(ship)
local histData = MoveModule.GetHistory(ship)
if histData.actKey == 0 then
print(ship.getName() .. ': NO HISTORY')
else
print(ship.getName() .. '\'s HISTORY:')
local k=1
while histData.history[k] ~= nil do
local entry = histData.history[k]
local typeStr = ' (' .. entry.finType
if entry.part ~= nil then
typeStr = typeStr .. ':' .. entry.part .. ')'
else
typeStr = typeStr .. ')'
end
if k == histData.actKey then
print(' >> ' .. entry.move .. typeStr)
else
print(' -- ' .. entry.move .. typeStr)
end
k = k+1
end
print(' -- -- -- -- ')
end
end
-- Save <some> ship position to the history
-- Saves on the position after current and deletes any past that
MoveModule.AddHistoryEntry = function(ship, entry)
local histData = MoveModule.GetHistory(ship)
histData.actKey = histData.actKey+1
histData.history[histData.actKey] = entry
MoveModule.ErasePastCurrent(ship)
end
-- How much position can be offset to be considered 'same'
MoveModule.undoPosCutoff = Convert_mm_igu(1)
-- How much rotation can be offset to be considered 'same'
MoveModule.undoRotCutoffDeg = 1
-- Check if the ship is on the curent history position (tolerance above)
MoveModule.IsAtSavedState = function(ship)
local histData = MoveModule.GetHistory(ship)
if histData.actKey > 0 then
local currEntry = histData.history[histData.actKey]
local dist = Vect.Distance(ship.getPosition(), currEntry.pos)
local angDiff = math.abs(ship.getRotation()[2] - currEntry.rot[2])
if math.abs(angDiff) > 180 then
angDiff = math.abs(angDiff - math.sgn(angDiff)*360)
end
return (dist < MoveModule.undoPosCutoff and angDiff < MoveModule.undoRotCutoffDeg)
end
return false
end
-- Save curent ship position to the history
-- Can be quiet when not explicitly called by the user
MoveModule.SaveStateToHistory = function(ship, beQuiet)
local histData = MoveModule.GetHistory(ship)
-- Don't add an entry if it's current position/rotation
if MoveModule.IsAtSavedState(ship) then
if beQuiet ~= true then
AnnModule.Announce({type='historyHandle', note='already has current position saved'}, 'all', ship)
end
else
local entry = {pos=ship.getPosition(), rot=ship.getRotation(), move='position save', part=nil, finType='special'}
MoveModule.AddHistoryEntry(ship, entry)
if beQuiet ~= true then
AnnModule.Announce({type='historyHandle', note='stored current position'}, 'all', ship)
end
end
end
-- Move a ship to a previous state from the history
-- Return true if action was taken
-- Return false if there is no more data
MoveModule.UndoMove = function(ship)
local histData = MoveModule.GetHistory(ship)
local announceInfo = {type='historyHandle'}
-- No history
if histData.actKey == 0 then
announceInfo.note = 'has no more moves to undo'
else
-- There is history
local currEntry = histData.history[histData.actKey]
-- current position not matching history
if not MoveModule.IsAtSavedState(ship) then
MoveModule.MoveShip(ship, {finPos={pos=currEntry.pos, rot=currEntry.rot}, noSave=true})
announceInfo.note = 'moved to the last saved position'
else
-- current position matching current histor
if histData.actKey > 1 then
local undidMove = currEntry.move
histData.actKey = histData.actKey - 1