-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsaveload2-sct.tbm
More file actions
1176 lines (886 loc) · 34.7 KB
/
saveload2-sct.tbm
File metadata and controls
1176 lines (886 loc) · 34.7 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
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
#Conditional Hooks
$Application: FS2_Open
$On Game Init:
[
SaveState = {}
function SaveState:Init()
ba.print("SAVELOAD: Initialization begun...!\n")
self.WorksOutOfCampaign = true
if self.WorksOutOfCampaign or mn.isInCampaign() then
self.Enabled = true
end
if not self.Enabled then
return
end
self.CurrentIndex = nil
self.WingList = {}
self.List = self:GetAllCurrentShips()
self.CuedList = {}
local playerData = ba.getCurrentPlayer()
self.SaveFilename = playerData:getName() .. "_" .. mn.getMissionFilename() .. ".sav"
if cf.fileExists(self.SaveFilename, "data/config") then
self.LoadedData = axemParse:ReadJSON(self.SaveFilename, "data/config")
else
self.LoadedData = {}
end
self.AssistList = {}
self.AssistHP = nil
self.AssistAmmo = nil
self.ShipFlags = {"invulnerable", "protect-ship", "beam-protect-ship", "flak-protect-ship", "laser-protect-ship", "missile-protect-ship", "cargo-known", "hidden-from-sensors", "stealth", "friendly-stealth-invisible", "afterburners-locked", "primaries-locked", "secondaries-locked", "free-afterburner-use"}
end
function SaveState:AddWing(wingname)
for i,v in ipairs(self.WingList) do
if v == wingname then return end
end
ba.print("SAVELOAD: New wing found, " .. wingname ..", adding to Wing List\n")
self.WingList[#self.WingList+1] = wingname
ba.print("SAVELOAD: Number of wings: " .. #self.WingList .."\n")
end
--This just gets called at mission start so we have the beginnings of our master list.
function SaveState:GetAllCurrentShips()
ba.print("SAVELOAD: Getting ships present at start...\n")
local t = {}
local numShips = #mn.Ships
for i=1, numShips do
local thisShip = mn.Ships[i]
if thisShip and thisShip:isValid() then
t[#t+1] = thisShip.Name
ba.print("SAVELOAD: Found " .. thisShip.Name .. ", adding to Ship List\n")
if thisShip:getWing() and thisShip:getWing():isValid() then
self:AddWing(thisShip:getWing().Name)
end
end
end
ba.print("SAVELOAD: Found " .. #t .. " items.\n")
return t
end
--When a ship arrives, we add it to THE LIST, which we'll check when we're saving. We want to catch every ship that arrives, even ones that might have died/left already, so that's why we're not just checking mn.Ships[].
function SaveState:AddShip(ship)
ba.print("SAVELOAD: " .. ship.Name .. " arrived, adding to Ship List\n")
if self.Enabled then
self.List[#self.List+1] = ship.Name
if ship:getWing() and ship:getWing():isValid() then
self:AddWing(ship:getWing().Name)
end
end
if self.CurrentIndex then
self:CheckLateData(ship)
end
end
function SaveState:CheckLateData(ship)
ba.print("SAVELOAD: Checking " .. ship.Name .. " for late data\n")
local thisShipName = ship.Name
for i=1, #self.CuedList do
if self.CuedList[i] and self.CuedList[i] == thisShipName then
local thisloadeddata = self.LoadedData[self.CurrentIndex]
local data = thisloadeddata[thisShipName]
if data then self:ApplyLateData(ship,data) end
return
end
end
end
--Collecting data from a ship
function SaveState:GetShipData(shipname)
ba.print("SAVELOAD: Getting ship data from " .. shipname .. "\n")
local t = {}
t.Name = shipname
ba.print("Name: " .. tostring(t.Name) .. "\n")
t.IsInMission = mn.evaluateSEXP("(is-in-mission !" .. t.Name .. "!)")
ba.print(" Is In Mission: " .. tostring(t.IsInMission) .. "\n")
t.HasArrived = mn.evaluateSEXP("(has-arrived-delay 0 !" .. t.Name .. "!)")
ba.print(" Has Arrived: " .. tostring(t.HasArrived) .. "\n")
t.HasBeenDestroyed = mn.evaluateSEXP("(is-destroyed-delay 0 !" .. t.Name .."!)")
ba.print(" Has Been Destroyed: " .. tostring(t.HasBeenDestroyed) .. "\n")
t.HasDeparted = mn.evaluateSEXP("(has-departed-delay 0 !" .. t.Name .."!)")
ba.print(" Has Departed: " .. tostring(t.HasDeparted) .. "\n")
if t.IsInMission then
local ship = mn.Ships[shipname]
if ship and ship:isValid() then
--Get Class
t.Class = ship.Class.Name
ba.print(" Class: " .. tostring(t.Class) .. "\n")
--Is this part of a wing?
if ship:getWing() and ship:getWing():isValid() then
t.Wing = ship:getWing().Name
ba.print(" Wing: " .. tostring(t.Wing) .. "\n")
end
--Get HP
t.HP = math.floor((ship.HitpointsLeft / ship.HitpointsMax) * 100)
ba.print(" HP: " .. tostring(t.HP) .. "\n")
--Get Shields
if ship.Shields and ship.Shields:isValid() then
t.Shields = math.floor((ship.Shields.CombinedLeft / ship.Shields.CombinedMax) * 100)
ba.print(" Shields: " .. tostring(t.Shields) .. "\n")
end
--Get Armor (LateData)
t.HullArmor = ship.ArmorClass
ba.print(" Hull Armor: " .. tostring(t.HullArmor) .. "\n")
t.ShieldArmor = ship.ShieldArmorClass
ba.print(" Shield Armor: " .. tostring(t.ShieldArmor) .. "\n")
--Get Team (LateData)
t.Team = ship.Team.Name
ba.print(" Team: " .. tostring(t.Team) .. "\n")
--Get Countermeasures (LateData)
t.Countermeasures = ship.CountermeasuresLeft
ba.print(" Countermeasures: " .. tostring(t.Countermeasures) .. "\n")
--Get Orders (LateData)
ba.print(" Orders:\n")
t.Orders = {}
for i=1, #ship.Orders do
local order = ship.Orders[i]
ba.print(" Number: " .. i .. "\n")
tt = {}
tt.Priority = order.Priority
ba.print(" Priority: " .. tostring(tt.Priority) .. "\n")
tt.Type = self:GetAIOrderFromEnum(order:getType())
ba.print(" Type: " .. tostring(tt.Type) .. "\n")
if (order:getType() == ORDER_WAYPOINTS) or (order:getType() == ORDER_WAYPOINTS_ONCE) then
--tt.Target = order.Target:getList().Name
else
if order.Target and order.Target:isValid() then
tt.Target = order.Target.Name
ba.print(" Target: " .. tostring(tt.Target) .. "\n")
end
end
tt.Subsystem = order.TargetSubsystem.Name
ba.print(" Subsystem: " .. tostring(tt.Subsystem) .. "\n")
t.Orders[i] = tt
end
--Get Flags (LateData)
t.Flags = {}
for i=1, #self.ShipFlags do
if mn.evaluateSEXP("(are-ship-flags-set !" .. shipname .. "! !" .. self.ShipFlags[i] .. "!)") then
t.Flags[i] = true
else
t.Flags[i] = false
end
end
ba.print(" Flags:\n")
for i=1, #self.ShipFlags do
if t.Flags[i] then
ba.print(" Index " .. i .. ": " .. tostring(self.ShipFlags[i]) .. "\n")
end
end
ba.print(" Subsystems:\n")
--Subsystem craziness
local numSubsys = #ship
if numSubsys > 0 then
t.Subsystems = {}
for i=1, numSubsys do
ba.print(" Subsystem " .. i .. "\n")
local thisSubsys = ship[i]
local ss = {}
ba.print(" Subsystem Name: " .. tostring(thisSubsys:getModelName()) .. "\n")
ss.HP = math.floor((thisSubsys.HitpointsLeft / thisSubsys.HitpointsMax) * 100)
ba.print(" HP: " .. tostring(ss.HP) .. "\n")
ss.Armor = thisSubsys.ArmorClass
ba.print(" Armor: " .. tostring(ss.Armor) .. "\n")
if thisSubsys:isTurret() then
ba.print(" Is a turret!\n")
ss.TurretLocked = thisSubsys.TurretLocked -- (LateData)
ba.print(" Locked: " .. tostring(ss.TurretLocked) .. "\n")
if thisSubsys.PrimaryBanks and thisSubsys.PrimaryBanks:isValid() then
ba.print(" Primary Banks:\n")
ss.PBanks = {}
for j=1, #thisSubsys.PrimaryBanks do
if thisSubsys.PrimaryBanks[j] and thisSubsys.PrimaryBanks[j]:isValid() then
ba.print(" Bank: " .. j .. "\n")
ss.PBanks[j] = {}
ss.PBanks[j].Class = thisSubsys.PrimaryBanks[j].WeaponClass.Name
ba.print(" Class: " .. tostring(ss.PBanks[j].Class) .. "\n")
ss.PBanks[j].AmmoLeft = math.floor((thisSubsys.PrimaryBanks[j].AmmoLeft / thisSubsys.PrimaryBanks[j].AmmoMax) * 100)
ba.print(" AmmoLeft: " .. tostring(ss.PBanks[j].AmmoLeft) .. "\n")
end
end
end
if thisSubsys.SecondaryBanks and thisSubsys.SecondaryBanks:isValid() then
ba.print(" Secondary Banks:\n")
ss.SBanks = {}
for j=1, #thisSubsys.SecondaryBanks do
if thisSubsys.SecondaryBanks[j] and thisSubsys.SecondaryBanks[j]:isValid() then
ba.print(" Bank: " .. j .. "\n")
ss.SBanks[j] = {}
ss.SBanks[j].Class = thisSubsys.SecondaryBanks[j].WeaponClass.Name
ba.print(" Class: " .. tostring(ss.SBanks[j].Class) .. "\n")
ss.SBanks[j].AmmoLeft = math.floor((thisSubsys.SecondaryBanks[j].AmmoLeft / thisSubsys.SecondaryBanks[j].AmmoMax) * 100)
ba.print(" AmmoLeft: " .. tostring(ss.SBanks[j].AmmoLeft) .. "\n")
end
end
end
end
t.Subsystems[i] = ss
end
end
if ship.PrimaryBanks and ship.PrimaryBanks:isValid() then
ba.print(" Ship Primary Banks:\n")
t.PBanks = {}
for i=1, #ship.PrimaryBanks do
ba.print(" Bank: " .. i .. "\n")
t.PBanks[i] = {}
t.PBanks[i].Class = ship.PrimaryBanks[i].WeaponClass.Name
ba.print(" Class: " .. tostring(t.PBanks[i].Class) .. "\n")
t.PBanks[i].AmmoLeft = math.floor((ship.PrimaryBanks[i].AmmoLeft / ship.PrimaryBanks[i].AmmoMax) * 100)
ba.print(" AmmoLeft: " .. tostring(t.PBanks[i].AmmoLeft) .. "\n")
end
end
if ship.SecondaryBanks and ship.SecondaryBanks:isValid() then
ba.print(" Ship Secondary Banks:\n")
t.SBanks = {}
for i=1, #ship.SecondaryBanks do
ba.print(" Bank: " .. i .. "\n")
t.SBanks[i] = {}
t.SBanks[i].Class = ship.SecondaryBanks[i].WeaponClass.Name
ba.print(" Class: " .. tostring(t.SBanks[i].Class) .. "\n")
t.SBanks[i].AmmoLeft = math.floor((ship.SecondaryBanks[i].AmmoLeft / ship.SecondaryBanks[i].AmmoMax) * 100)
ba.print(" AmmoLeft: " .. tostring(t.SBanks[i].AmmoLeft) .. "\n")
end
end
if ship == hv.Player then
t.AfterburnerFuelLeft = ship.AfterburnerFuelLeft
ba.print(" Afterburner: " .. tostring(t.AfterburnerFuelLeft) .. "\n")
t.WeaponEnergyLeft = ship.WeaponEnergyLeft
ba.print(" Afterburner: " .. tostring(t.WeaponEnergyLeft) .. "\n")
end
t.Position = {}
t.Position.x = ship.Position.x
t.Position.y = ship.Position.y
t.Position.z = ship.Position.z
ba.print(" Position: " .. tostring(ship.Position) .. "\n")
t.Orientation = {}
t.Orientation.p = ship.Orientation.p
t.Orientation.b = ship.Orientation.b
t.Orientation.h = ship.Orientation.h
ba.print(" Orientation: " .. tostring(ship.Orientation) .. "\n")
t.Velocity = {}
t.Velocity.x = ship.Physics.Velocity.x
t.Velocity.y = ship.Physics.Velocity.y
t.Velocity.z = ship.Physics.Velocity.z
ba.print(" Velocity: " .. tostring(ship.Physics.Velocity) .. "\n")
end
end
return t, t.Name
end
function SaveState:GetWingData(wingname)
ba.print("SAVELOAD: Getting wing data from " .. wingname .. "\n")
local t = {}
t.Name = wingname
t.HasArrived = mn.evaluateSEXP("(has-arrived-delay 0 !" .. wingname .. "!)")
t.HasBeenDestroyed = mn.evaluateSEXP("(is-destroyed-delay 0 !" .. wingname .."!)")
t.HasDeparted = mn.evaluateSEXP("(has-departed-delay 0 !" .. wingname .."!)")
return t, t.Name
end
function SaveState:CheckList(t, data)
for i,v in ipairs(t) do
if v == data then return true end
end
return false
end
--This is what lua-savestate-save calls
--We go through THE LIST and nab all of the ship data which will go into self.LoadedData which is what will get saved.
--We use the ship name as a key for the data table for ease of retrival and checking later. This might take a bit more CPU time, but we can probably spare it.
function SaveState:SaveAll(index, ...)
ba.print("SAVELOAD: ***** BEGINNING SAVE STATE! *****\n")
if not self.Enabled then return false end
local data = {}
local exclusionList = {}
for i, v in ipairs(arg) do
ba.print("SAVELOAD: Adding " .. v[1] .. " to exclusion list\n")
exclusionList[#exclusionList+1] = v[1]
end
for i, entry in ipairs(self.List) do
if (not self:CheckList(exclusionList,entry)) and (not self:IsSupportShip(entry)) then
shipdata,key = self:GetShipData(entry)
data[key] = shipdata
end
end
data.Wings = {}
for i, entry in ipairs(self.WingList) do
if not self:CheckList(exclusionList,entry) then
wingdata,key = self:GetWingData(entry)
data.Wings[key] = wingdata
end
end
self.LoadedData[index or #self.LoadedData+1] = data
ba.print("SAVELOAD: Writing save data to file " .. self.SaveFilename .. " with save index " .. index .. "\n")
axemParse:WriteJSON(self.LoadedData, self.SaveFilename, "data/config")
end
--This is what lua-savestate-load calls
--Determinations are used to figure out how to apply the data. If the ship is present, we can apply it now. If not, we'll need to put it in the parsed object data. We also check if the ship is blown up or departed, where we can instantly and forcefully apply these things.
function SaveState:LoadAll(index)
if not self.Enabled then return false end
ba.print("SAVELOAD: ***** BEGINNING LOAD STATE! *****\n")
if not index then
ba.print("SAVE STATE: no index specified, aborting load data.\n")
return
else
self.CurrentIndex = index
end
local thisloadeddata = self.LoadedData[self.CurrentIndex]
if not thisloadeddata then
ba.print("SAVE STATE: index " .. self.CurrentIndex .. " not found, aborting load data.\n")
return
end
--Apply saved data to ships that are present
local numShips = #mn.Ships
for i=1, numShips do
local thisShip = mn.Ships[i]
if thisShip and thisShip:isValid() then
local thisShipName = thisShip.Name
local data = thisloadeddata[thisShipName]
if data then
if data.IsInMission then --Present from the start and was alive during save...
self:ApplyData(thisShip,data)
else
if data.HasBeenDestroyed then -- or was destroyed
self:InstantKill(thisShip)
elseif data.HasDeparted then -- or had left
self:InstantDepart(thisShip)
end
end
end
end
end
--Apply saved data to ships that have yet to arrive
for thisShip in mn.getArrivalList() do
local data = thisloadeddata[thisShip.Name]
if data then
if data.IsInMission then self:ApplyParseData(thisShip,data) end
self.CuedList[#self.CuedList+1] = thisShip.Name
end
end
end
function SaveState:GiveOrders(ship,data)
mn.runSEXP("(clear-goals !" .. ship.Name .. "!)")
for i=1, #data.Orders do
ba.print(" Order " .. i .. ":")
local thisOrder = data.Orders[i]
local order = self:GetAIOrderFromString(thisOrder.Type)
local targetShip
if thisOrder.Target then
targetShip = mn.Ships[thisOrder.Target]
end
local targetSubsystem = thisOrder.Subsystem
ba.print(tostring(order) .. " Target: " .. tostring(targetShip) .. " Target Subsystem: " .. tostring(targetSubsystem) .. "\n")
if order and targetShip and targetShip:isValid() then
if targetSubsystem then
ship:giveOrder(order, targetShip, targetShip[targetSubsystem], thisOrder.Priority)
else
ship:giveOrder(order, targetShip, nil, thisOrder.Priority)
end
end
end
end
--Applying data to ships already present at time of load
function SaveState:ApplyData(ship,data,external)
ba.print("SAVELOAD: Applying data to " .. ship.Name .. "\n")
if ship.Class.Name ~= data.Class then
ba.print(" Applying Class: " .. tostring(data.Class) .. "\n")
ship.Class = tb.ShipClasses[data.Class]
end
ship.HitpointsLeft = (self:GetHP(data.HP,data.Name) / 100) * ship.HitpointsMax
ba.print(" Applying HP Left: " .. tostring(ship.HitpointsLeft) .. "\n")
ba.print(" Applying Shields: ")
if data.Shields and ship.Shields and ship.Shields:isValid() then
for i=1, #ship.Shields do
ship.Shields[i] = (data.Shields / 100 / #ship.Shields) * ship.Shields.CombinedMax
ba.print("Quad" .. i .. ": " .. tostring(ship.Shields[i]))
end
end
ba.print("\n")
ba.print(" Applying Armor Class: " .. tostring(data.HullArmor) .. "\n")
ship.ArmorClass = data.HullArmor
ba.print(" Applying Shield Armor Class: " .. tostring(data.ShieldArmor) .. "\n")
ship.ShieldArmorClass = data.ShieldArmor
ba.print(" Applying Team: " .. tostring(data.Team) .. "\n")
ship.Team = mn.Teams[data.Team]
if not external then
ba.print(" Applying Orders:\n")
self:GiveOrders(ship, data)
end
ba.print(" Applying Ship Flags...\n")
for i=1, #self.ShipFlags do
if data.Flags[i] then
local sexpstring = "(alter-ship-flag !" .. self.ShipFlags[i] .. "! (true) (true) !" .. ship.Name .. "! )"
ba.print(sexpstring .. "\n")
mn.runSEXP(sexpstring)
end
end
ba.print(" Applying Countermeasures: " .. tostring(data.Countermeasures) .. "\n")
ship.CountermeasuresLeft = data.Countermeasures
ba.print(" Applying Subsystem data...\n")
local numSubsys = #ship
if numSubsys > 0 then
for i=1, numSubsys do
local thisSubsys = ship[i]
ba.print(" Subsystem " .. i .. " - " .. thisSubsys:getModelName() .. "\n")
local ssdata = data.Subsystems[i]
if ssdata.HP then
ba.print(" HP Left: " .. tostring((ssdata.HP / 100) * thisSubsys.HitpointsMax) .. "\n")
thisSubsys.HitpointsLeft = (ssdata.HP / 100) * thisSubsys.HitpointsMax
end
if ssdata.Armor then
ba.print(" Armor Class: " .. tostring(ssdata.Armor) .. "\n")
thisSubsys.ArmorClass = ssdata.Armor
end
if thisSubsys:isTurret() then
ba.print(" This is a turret!\n")
ba.print(" Locked: " .. tostring(ssdata.TurretLocked) .. "\n")
thisSubsys.TurretLocked = ssdata.TurretLocked
if thisSubsys.PrimaryBanks and thisSubsys.PrimaryBanks:isValid() then
ba.print(" Has primary banks...\n")
for j=1, #thisSubsys.PrimaryBanks do
if thisSubsys.PrimaryBanks[j] and thisSubsys.PrimaryBanks[j]:isValid() then
ba.print(" Bank " .. j .. ": " .. tostring(ssdata.PBanks[j].Class) .. "\n")
thisSubsys.PrimaryBanks[j].WeaponClass = tb.WeaponClasses[ssdata.PBanks[j].Class]
thisSubsys.PrimaryBanks[j].AmmoLeft = (self:GetAmmo(ssdata.PBanks[j].AmmoLeft,data.Name)/100) * thisSubsys.PrimaryBanks[j].AmmoMax
end
end
end
if thisSubsys.SecondaryBanks and thisSubsys.SecondaryBanks:isValid() then
ba.print(" Has secondary banks...\n")
for j=1, #thisSubsys.SecondaryBanks do
if thisSubsys.SecondaryBanks[j] and thisSubsys.SecondaryBanks[j]:isValid() then
ba.print(" Bank " .. j .. ": " .. tostring(ssdata.SBanks[j].Class) .. "\n")
thisSubsys.SecondaryBanks[j].WeaponClass = tb.WeaponClasses[ssdata.SBanks[j].Class]
thisSubsys.SecondaryBanks[j].AmmoLeft = (self:GetAmmo(ssdata.SBanks[j].AmmoLeft,data.Name)/100) * thisSubsys.SecondaryBanks[j].AmmoMax
end
end
end
end
end
end
ba.print(" Applying Weapon Bank data...\n")
if ship.PrimaryBanks and ship.PrimaryBanks:isValid() then
ba.print(" Has primary banks...\n")
for i=1, #ship.PrimaryBanks do
ba.print(" Bank .. " .. i .. " - Class: " .. tostring(data.PBanks[i].Class) .. "\n")
ship.PrimaryBanks[i].WeaponClass = tb.WeaponClasses[data.PBanks[i].Class]
ba.print(" Ammo: " .. tostring(self:GetAmmo(data.PBanks[i].AmmoLeft,data.Name)/100) * ship.PrimaryBanks[i].AmmoMax .. "\n")
ship.PrimaryBanks[i].AmmoLeft = (self:GetAmmo(data.PBanks[i].AmmoLeft,data.Name)/100) * ship.PrimaryBanks[i].AmmoMax
end
end
if ship.SecondaryBanks and ship.SecondaryBanks:isValid() then
ba.print(" Has secondary banks...\n")
for i=1, #ship.SecondaryBanks do
ba.print(" Bank .. " .. i .. " - Class: " .. tostring(data.SBanks[i].Class) .. "\n")
ship.SecondaryBanks[i].WeaponClass = tb.WeaponClasses[data.SBanks[i].Class]
ba.print(" Ammo: " .. tostring(self:GetAmmo(data.SBanks[i].AmmoLeft,data.Name)/100) * ship.SecondaryBanks[i].AmmoMax .. "\n")
ship.SecondaryBanks[i].AmmoLeft = (self:GetAmmo(data.SBanks[i].AmmoLeft,data.Name)/100) * ship.SecondaryBanks[i].AmmoMax
end
end
if ship == hv.Player then
ba.print(" Applying Afterburner Fuel: " .. tostring(data.AfterburnerFuelLeft) .. "\n")
ship.AfterburnerFuelLeft = data.AfterburnerFuelLeft
ba.print(" Applying Weapon Energy: " .. tostring(data.WeaponEnergyLeft) .. "\n")
ship.WeaponEnergyLeft = data.WeaponEnergyLeft
end
if not external then
local newPosition = ba.createVector(data.Position.x, data.Position.y, data.Position.z)
ba.print(" Applying Position: " .. tostring(newPosition) .. "\n")
ship.Position = newPosition
local newOrientation = ba.createOrientation(data.Orientation.p, data.Orientation.b, data.Orientation.h)
ba.print(" Applying Orientation: " .. tostring(newOrientation) .. "\n")
ship.Orientation = newOrientation
local newVelocity = ba.createVector(data.Velocity.x, data.Velocity.y, data.Velocity.z)
ba.print(" Applying Velocity: " .. tostring(newVelocity) .. "\n")
ship.Physics.Velocity = newVelocity
end
end
--Bang
function SaveState:InstantKill(ship)
mn.runSEXP("(destroy-instantly !" .. ship.Name .. "! )")
end
--Bon voyage!
function SaveState:InstantDepart(ship)
mn.runSEXP("(set-departure-info !" .. ship.Name .. "! !Hyperspace! !<no anchor>! 0 0 false)")
ship:warpOut()
end
--Applying data to parse objects, or the objects that have yet to actually arrive in game yet.
function SaveState:ApplyParseData(ship,data)
ba.print("SAVELOAD: Applying parse data to " .. ship.Name .. "\n")
--Set the ship to arrive by hyperspace without warp-in to save the FREDder from having to do that
mn.runSEXP("(set-arrival-info !" .. ship.Name .. "! !Hyperspace! !<any friendly>! 0 0 0 false)")
ba.print(" set-arrival-info set!\n")
if data.Wing then
mn.runSEXP("(set-arrival-info !" .. data.Wing .. "! !Hyperspace! !<any friendly>! 0 0 0 false)")
ba.print(" set-arrival-info (wing) set!\n")
end
if ship.ShipClass.Name ~= data.Class then
ship.ShipClass = tb.ShipClasses[data.Class]
ba.print(" Ship Class set: " .. tostring(ship.ShipClass.Name) .. "\n")
end
ship.InitialHull = self:GetHP(data.HP,data.Name)
ba.print(" Initial Hull set: " .. tostring(ship.InitialHull) .. "\n")
if data.Shields then
ship.InitialShields = data.Shields
ba.print(" Initial Shields set: " .. tostring(ship.InitialShields) .. "\n")
end
if ship.MainStatus.PrimaryBanks then
ba.print(" Beginning Primary Bank Setup...\n (number of primary banks in save data: " .. #data.PBanks .. ")\n (number of actual primary banks: " .. #ship.MainStatus.PrimaryBanks .." )\n")
for i, pbank in ipairs(data.PBanks) do
ba.print(" Bank : " .. i .. ":")
if ship.MainStatus.PrimaryBanks[i] then
ba.print(tostring(pbank.Class) .. "\n")
ship.MainStatus.PrimaryBanks[i] = tb.WeaponClasses[pbank.Class]
if pbank.AmmoLeft then
ship.MainStatus.PrimaryAmmo[i] = self:GetAmmo(pbank.AmmoLeft,data.Name)
ba.print(" Ammo Left: " .. tostring(ship.MainStatus.PrimaryAmmo[i]) .. "\n")
end
else
ba.print("Something went very wrong!\n")
end
end
end
if ship.MainStatus.SecondaryBanks then
ba.print(" Beginning Secondary Bank Setup...\n (number of Secondary banks in save data: " .. #data.SBanks .. ")\n (number of actual Secondary banks: " .. #ship.MainStatus.SecondaryBanks .." )\n")
for i, sbank in ipairs(data.SBanks) do
ba.print(" Bank : " .. i .. ":")
if ship.MainStatus.SecondaryBanks[i] then
ba.print(tostring(sbank.Class) .. "\n")
ship.MainStatus.SecondaryBanks[i] = tb.WeaponClasses[sbank.Class]
if sbank.AmmoLeft then
ship.MainStatus.SecondaryAmmo[i] = self:GetAmmo(sbank.AmmoLeft,data.Name)
ba.print(" Ammo Left: " .. tostring(ship.MainStatus.SecondaryAmmo[i]) .. "\n")
end
else
ba.print("Something went very wrong!\n")
end
end
end
ba.print(" Done with weapons...\n Beginning Subsystem Damage...\n")
for i,subsys in ipairs(ship.Subsystems) do
ba.print(" Subsystem Index: " .. i .. " - " .. subsys.Name .. "\n")
if data.Subsystems[i].HP then
subsys.Damage = 100 - data.Subsystems[i].HP --We record the health in the save data, but the subsystem uses damage (silly Volition)
ba.print(" Subsystem Damage: " .. subsys.Damage .. "\n")
end
ba.print(" Turret Bank Info:\n")
if subsys.PrimaryBanks and data.Subsystems[i].PBanks then
ba.print(" Primary Banks:\n")
for i, pbank in ipairs(data.Subsystems[i].PBanks) do
ba.print(" Bank: " .. i .. "\n")
if subsys.PrimaryBanks[i] then
subsys.PrimaryBanks[i] = tb.WeaponClasses[pbank.Class]
ba.print(" Class: " .. pbank.Class .. "\n")
if pbank.AmmoLeft then subsys.PrimaryAmmo[i] = self:GetAmmo(pbank.AmmoLeft,data.Name) end
end
end
end
if subsys.SecondaryBanks and data.Subsystems[i].SBanks then
ba.print(" Secondary Banks:\n")
for i, sbank in ipairs(data.Subsystems[i].SBanks) do
ba.print(" Bank: " .. i .. "\n")
if subsys.SecondaryBanks[i] then
subsys.SecondaryBanks[i] = tb.WeaponClasses[sbank.Class]
ba.print(" Class: " .. sbank.Class .. "\n")
if sbank.AmmoLeft then subsys.SecondaryAmmo[i] = self:GetAmmo(sbank.AmmoLeft,data.Name) end
end
end
end
end
local newPosition = ba.createVector(data.Position.x, data.Position.y, data.Position.z)
ship.Position = newPosition
ba.print(" Position: " .. tostring(newPosition) .. "\n")
local newOrientation = ba.createOrientation(data.Orientation.p, data.Orientation.b, data.Orientation.h)
ship.Orientation = newOrientation
ba.print(" Orientation: " .. tostring(newOrientation) .. "\n")
end
--Apply data to ships that aren't present at time of load and don't have the right parse data parameters
function SaveState:ApplyLateData(ship,data)
ba.print("SAVELOAD: Applying late data to " .. ship.Name .. "\n")
if data.HasBeenDestroyed then
ba.print(" Killing ship...\n")
self:InstantKill(ship)
return
elseif data.HasDeparted then
ba.print(" Instantly departing ship...\n")
self:InstantDepart(ship)
return
end
--Apply Armor
ba.print(" Applying armor: " .. tostring(data.HullArmor) .."\n")
ship.ArmorClass = data.HullArmor
ba.print(" Applying shield armor: " .. tostring(data.ShieldArmor) .."\n")
ship.ShieldArmorClass = data.ShieldArmor
--Apply Team
ba.print(" Applying team: " .. tostring(data.Team) .."\n")
ship.Team = mn.Teams[data.Team]
--Apply Countermeasures
ba.print(" Applying countermeasure count: " .. tostring(data.Countermeasures) .."\n")
ship.CountermeasuresLeft = data.Countermeasures
--Apply AI Orders
ba.print(" Applying AI orders (Note waypoint orders will show up as nil!)\n")
self:GiveOrders(ship, data)
--Apply Ship Flags
ba.print(" Running SEXPs for ship flags...\n")
for i=1, #self.ShipFlags do
if data.Flags[i] then
local sexpstring = "(alter-ship-flag !" .. self.ShipFlags[i] .. "! (true) (true) !" .. ship.Name .. "! )"
ba.print(sexpstring .. "\n")
mn.runSEXP(sexpstring)
end
end
--Apply Turret Lock Status
ba.print(" Applying Turret Locks (true = locked, false = free)\n")
local numSubsys = #ship
if numSubsys > 0 then
for i=1, numSubsys do
local thisSubsys = ship[i]
if thisSubsys:isTurret() then
ba.print(" " .. thisSubsys:getModelName() .. ": " .. tostring(data.Subsystems[i].TurretLocked) .. "\n")
ship[i].TurretLocked = data.Subsystems[i].TurretLocked
end
end
end
end
--Why...
function SaveState:GetAIOrderFromEnum(orderEnum)
local order
if orderEnum == ORDER_ATTACK then
order = "Attack"
elseif orderEnum == ORDER_ATTACK then
order = "Attack Any"
elseif orderEnum == ORDER_DEPART then
order = "Depart"
elseif orderEnum == ORDER_DISABLE then
order = "Disable"
elseif orderEnum == ORDER_DISARM then
order = "Disarm"
elseif orderEnum == ORDER_DOCK then
order = "Dock"
elseif orderEnum == ORDER_EVADE then
order = "Evade"
elseif orderEnum == ORDER_FLY_TO then
order = "Fly to"
elseif orderEnum == ORDER_FORM_ON_WING then
order = "Form on Wing"
elseif orderEnum == ORDER_GUARD then
order = "Guard"
elseif orderEnum == ORDER_IGNORE_SHIP then
order = "Ignore"
elseif orderEnum == ORDER_KEEP_SAFE_DISTANCE then
order = "Keep Safe Distance"
elseif orderEnum == ORDER_PLAY_DEAD then
order = "Play Dead"
elseif orderEnum == ORDER_REARM then
order = "Rearm"
elseif orderEnum == ORDER_STAY_NEAR then
order = "Stay Near"
elseif orderEnum == ORDER_STAY_STILL then
order = "Stay Still"
elseif orderEnum == ORDER_UNDOCK then
order = "Undock"
elseif orderEnum == ORDER_WAYPOINTS then
order = "Waypoints"
elseif orderEnum == ORDER_WAYPOINTS_ONCE then
order = "Waypoints Once"
elseif orderEnum == ORDER_ATTACK_WING then
order = "Attack Wing"
elseif orderEnum == ORDER_GUARD_WING then
order = "Guard Wing"
else
order = "None"
end
return order
end
--Why????
function SaveState:GetAIOrderFromString(order)
--Waypoints orders will return nil because we can't start a waypoint mid-way through
local orderEnum
if order == "Attack" then
orderEnum = ORDER_ATTACK
elseif order == "Attack Any" then
orderEnum = ORDER_ATTACK
elseif order == "Depart" then
orderEnum = ORDER_DEPART
elseif order == "Disable" then
orderEnum = ORDER_DISABLE
elseif order == "Disarm" then
orderEnum = ORDER_DISARM
elseif order == "Dock" then
orderEnum = ORDER_DOCK
elseif order == "Evade" then
orderEnum = ORDER_EVADE
elseif order == "Fly to" then
orderEnum = ORDER_FLY_TO
elseif order == "Form on Wing" then
orderEnum = ORDER_FORM_ON_WING
elseif order == "Guard" then
orderEnum = ORDER_GUARD
elseif order == "Ignore" then
orderEnum = ORDER_IGNORE_SHIP
elseif order == "Keep Safe Distance" then
orderEnum = ORDER_KEEP_SAFE_DISTANCE
elseif order == "Play Dead" then
orderEnum = ORDER_PLAY_DEAD
elseif order == "Rearm" then
orderEnum = nil
--orderEnum = ORDER_REARM
elseif order == "Stay Near" then
orderEnum = ORDER_STAY_NEAR
elseif order == "Stay Still" then
orderEnum = ORDER_STAY_STILL
elseif order == "Undock" then
orderEnum = ORDER_UNDOCK
elseif order == "Waypoints" then
orderEnum = nil
--orderEnum = ORDER_WAYPOINTS
elseif order == "Waypoints Once" then
orderEnum = nil
--orderEnum = ORDER_WAYPOINTS_ONCE
elseif order == "Attack Wing" then
orderEnum = ORDER_ATTACK_WING
elseif order == "Guard Wing" then
orderEnum = ORDER_GUARD_WING
else
orderEnum = nil
end
return orderEnum
end
--Solves world hunger
function SaveState:GetCurrentSaveIndex()
return self.CurrentIndex or 0
end
--Returns false if a ship is destroyed or departed in the current save slot or another specified one.
function SaveState:QueryStatus(object, index)
if not self.Enabled then return false end
if (not index) and (not self.CurrentIndex) then
return true
end
local saveData
--Wait is this actually a wing?
if mn.Wings[object] and mn.Wings[object]:isValid() then
saveData = self.LoadedData[index or self.CurrentIndex].Wings
else
saveData = self.LoadedData[index or self.CurrentIndex]
end
if saveData[object] then
if saveData[object].HasBeenDestroyed then
return false
elseif saveData[object].HasDeparted then
return false
else
return true
end
end
return false