-
Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathBlueprints.lua
1122 lines (973 loc) · 41.3 KB
/
Blueprints.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
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
---@declare-global
--
-- Blueprint loading
--
-- During preloading of the map, we run loadBlueprints() from this file. It scans
-- the game directories and runs all .bp files it finds.
--
-- The .bp files call UnitBlueprint(), PropBlueprint(), etc. to define a blueprint.
-- All those functions do is fill in a couple of default fields and store off the
-- table in 'original_blueprints'.
--
-- Once that scan is complete, ModBlueprints() is called. It can arbitrarily mess
-- with the data in original_blueprints.
--
-- Finally, the engine registers all blueprints in original_blueprints to define the
-- "real" blueprints used by the game. A separate copy of these blueprints is made
-- available to the sim-side and user-side scripts.
--
-- How mods can affect blueprints
--
-- First, a mod can simply add a new blueprint file that defines a new blueprint.
--
-- Second, a mod can contain a blueprint with the same ID as an existing blueprint.
-- In this case it will completely override the original blueprint. Note that in
-- order to replace an original non-unit blueprint, the mod must set the "BlueprintId"
-- field to name the blueprint to be replaced. Otherwise the BlueprintId is defaulted
-- off the source file name. (Units don't have this problem because the BlueprintId is
-- shortened and doesn't include the original path).
--
-- Third, a mod can contain a blueprint with the same ID as an existing blueprint,
-- and with the special field "Merge = true". This causes the mod to be merged with,
-- rather than replace, the original blueprint.
--
-- Finally, a mod can hook the ModBlueprints() function which manipulates the
-- blueprints table in arbitrary ways.
-- 1. create a file /mod/s.../hook/system/Blueprints.lua
-- 2. override ModBlueprints(all_bps) in that file to manipulate the blueprints
--
-- Reloading of changed blueprints
--
-- When the disk watcher notices that a .bp file has changed, it calls
-- ReloadBlueprint() on it. ReloadBlueprint() repeats the above steps, but with
-- original_blueprints containing just the one blueprint.
--
-- Changing an existing blueprint is not 100% reliable; some changes will be picked
-- up by existing units, some not until a new unit of that type is created, and some
-- not at all. Also, if you remove a field from a blueprint and then reload, it will
-- default to its old value, not to 0 or its normal default.
--
local sub = string.sub
local gsub = string.gsub
local lower = string.lower
local getinfo = debug.getinfo
local here = getinfo(1).source
---@type BlueprintsTable
local original_blueprints
local current_mod
-- upvalue for performance
pcall = pcall
doscript = doscript
DiskFindFiles = DiskFindFiles
doscript("/lua/system/blueprints-ai.lua")
doscript("/lua/system/blueprints-lod.lua")
doscript("/lua/system/blueprints-projectiles.lua")
doscript("/lua/system/blueprints-units.lua")
doscript("/lua/system/blueprints-props.lua")
doscript("/lua/system/blueprints-weapons.lua")
---@class PreGameData
---@field CurrentMapDir string ## is obsolete, set for removal
---@field IconReplacements ModInfo[]
--- Load in the pre game data that is defined in the lobby through the preference file.
---@return PreGameData?
local function LoadPreGameData()
-- load in the prefs file
local file = DiskFindFiles("/preferences", "Game.prefs")[1]
if not file then
WARN('Blueprints.lua - Preferences file is not found. Skipping pre game data.')
return nil
end
-- try and load the pre game data of prefs file
---@type PreGameData
local preGameData
local ok, msg = pcall(
function()
local data = {}
doscript(file, data)
preGameData = data.PreGameData
end
)
-- tell us if something went wrong
if not ok then
WARN("Blueprints.lua - Preferences file is locked or corrupt. Skipping pre game data.")
WARN(msg)
end
return preGameData
end
---@class BlueprintIconAssignment
---@field BlueprintId BlueprintId
---@field IconSet? string
---@field TypeId? string
--- Attempts to assign icons to units if they exist
---@param units UnitBlueprint[] all unit blueprints
---@param assignments BlueprintIconAssignment[] A list of assignments
---@param identifier string The identifier of the UI mod that ensures compatibility when turned off (/textures/ui/game/common/strategicicons/identifier)
local function AssignIcons(units, assignments, identifier)
local StringLower = string.lower
---@param units UnitBlueprint[]
---@param id string
---@param icon string
local function AssignBlueprintId(units, id, icon)
-- do not punish people for capitalization of unit database
id = StringLower(id)
-- check whether unit exists
local unit = units[id]
if unit then
local path = identifier .. "/" .. icon
unit.StrategicIconName = path
end
end
local function AssignTypeId(units, id, icon)
-- todo :)
end
if assignments then
for _, info in assignments do
if info.BlueprintId and info.IconSet then
AssignBlueprintId(units, info.BlueprintId, info.IconSet)
elseif info.TypeId and info.Iconset then
AssignTypeId(units, info.TypeId, info.IconSet)
end
end
end
end
--- Creates a new basic environment that's safe for the UI to interact with
---@return table
local function NewSafeEnv()
local env = {
-- libraries
table = table.copy(table),
math = table.copy(math),
string = table.copy(string),
-- standard functions
ipairs = ipairs,
next = next,
pairs = pairs,
rawequal = rawequal,
rawget = rawget,
rawset = rawset,
tonumber = tonumber,
tostring = tostring,
type = type,
unpack = unpack,
pcall = pcall,
_VERSION = _VERSION,
-- __pow = __pow,
-- moholog-interacting functions
_ALERT = _ALERT,
-- _TRACEBACK = _TRACEBACK,
LOG = LOG,
SPEW = SPEW,
WARN = WARN,
assert = assert,
error = error,
-- core moho functions
Basename = Basename,
Dirname = Dirname,
EnumColorNames = EnumColorNames,
EulerToQuaternion = EulerToQuaternion,
exists = exists,
FileCollapsePath = FileCollapsePath,
GetVersion = GetVersion,
MATH_IRound = MATH_IRound,
MATH_Lerp = MATH_Lerp,
MinLerp = MinLerp,
MinSlerp = MinSlerp,
OrientFromDir = OrientFromDir,
Rect = Rect,
STR_GetTokens = STR_GetTokens,
STR_Utf8Len = STR_Utf8Len,
STR_Utf8SubString = STR_Utf8SubString,
STR_itox = STR_itox,
STR_xtoi = STR_xtoi,
VAdd = VAdd,
VDiff = VDiff,
VDist2 = VDist2,
VDist2Sq = VDist2Sq,
VDist3 = VDist3,
VDist3Sq = VDist3Sq,
VDot = VDot,
VMult = VMult,
VPerpDot = VPerpDot,
Vector = Vector,
Vector2 = Vector2,
-- representation functions
repr = repr,
reprs = reprs,
reprsl = reprsl,
repru = repru,
-- utility functions
iscallable = iscallable,
printField = printField,
safecall = safecall,
Sort = Sort,
sort_by = sort_by,
sort_down_by = sort_down_by,
sortedpairs = sortedpairs,
StringCapitalize = StringCapitalize,
StringComma = StringComma,
StringEnds = StringEnds,
StringExtract = StringExtract,
StringJoin = StringJoin,
StringPrepend = StringPrepend,
StringReverse = StringReverse,
StringSplit = StringSplit,
StringSplitCamel = StringSplitCamel,
StringStarts = StringStarts,
StringStartsWith = StringStartsWith,
}
-- these can cause desyncs
env.table.shuffle = nil
env.table.random = nil
env.math.random = nil
-- add the same behavior for non-existent globals as usual
setmetatable(env, getmetatable(_G))
return env
end
--- Finds and applies custom strategic icons defined by UI mods
--- @param all_bps BlueprintsTable the table with all blueprint values
local function FindCustomStrategicIcons(all_bps)
-- STRATEGIC ICON REPLACEMENT --
-- try and load in pre game data
local preGameData = LoadPreGameData()
if preGameData and preGameData.IconReplacements then
for _, info in preGameData.IconReplacements do
-- data that is set in the lobby
-- info.Name = mod.name
-- info.Author = mod.author
-- info.Location = mod.location
-- info.Identifier = string.lower(utils.StringSplit(mod.location, '/')[2])
-- info.UID = uid
-- all the functionality that is available in the _icons.lua
local state = NewSafeEnv()
-- try to get the icons file
local ok, msg = pcall(
function()
doscript(info.Location .. "/mod_icons.lua", state)
-- syntax errors are caught internally and instead it just returns the table untouched
if not (state.UnitIconAssignments or state.ScriptedIconAssignments) then
error("Blueprints.lua - can not import the icon configuration file at '" .. info.Location .. "'. This could be due to missing functionality functionality or a parsing error.")
end
end
)
-- if we can't, report it
if not ok then
WARN("Blueprints.lua - Unable to load icons from mod '" .. info.Name .. "' with uuid '" .. info.UID .. "'. Please inform the author: " .. info.Author)
WARN(msg)
end
ok, msg = pcall (
function()
-- scripted approach
if state.ScriptedIconAssignments then
-- retrieve data, make sure it is a deepcopy to prevent ui mods messing with the original
local units = table.deepcopy(all_bps.Unit)
local projectiles = table.deepcopy(all_bps.Projectile)
local icons = DiskFindFiles(info.Location .. "/custom-strategic-icons", "*.dds")
-- find scripted icons and assign them
local scriptedIcons = state.ScriptedIconAssignments(units, projectiles, icons)
AssignIcons(all_bps.Unit, scriptedIcons, info.Identifier)
-- inform the dev
local n = table.getsize(scriptedIcons)
if n > 0 then
SPEW("Blueprints.lua - Found (" .. n .. ") scripted icon assignments in " .. info.Name .. " by " .. info.Author .. ".")
end
end
-- manual approach
if state.UnitIconAssignments then
AssignIcons(all_bps.Unit, state.UnitIconAssignments, info.Identifier)
-- inform the dev
local n = table.getsize(state.UnitIconAssignments)
if n > 0 then
SPEW("Blueprints.lua - Found (" .. n .. ") manual icon assignments in " .. info.Name .. " by " .. info.Author .. ".")
end
end
end
)
-- if we can't, report it
if not ok then
WARN("Blueprints.lua - Unable to load icons from mod '" .. info.Name .. "' with uuid '" .. info.UID .. "'. Please inform the author: " .. info.Author)
WARN(msg)
end
end
end
end
local function InitOriginalBlueprints()
current_mod = nil
---@class BlueprintsTable
original_blueprints = {
---@type MeshBlueprint[]
Mesh = {},
---@type UnitBlueprint[]
Unit = {},
---@type PropBlueprint[]
Prop = {},
---@type ProjectileBlueprint[]
Projectile = {},
---@type TrailBlueprint[]
TrailEmitter = {},
---@type EmitterBlueprint[]
Emitter = {},
---@type BeamBlueprint[]
Beam = {},
}
end
local function GetSource()
-- Find the first calling function not in this source file
local n = 2
local there
while true do
there = getinfo(n).source
if there ~= here then break end
n = n + 1
end
if sub(there, 1, 1) == "@" then
there = sub(there, 2)
end
return DiskToLocal(there)
end
---@param group string
---@param bp Blueprint
local function StoreBlueprint(group, bp)
local id = bp.BlueprintId
local t = original_blueprints[group]
if t[id] and bp.Merge then
bp.Merge = nil
bp.Source = nil
t[id] = table.merged(t[id], bp)
else
t[id] = bp
end
end
--- Figure out what to name this blueprint based on the name of the file it came from.
--- Returns the entire filename. Either this or SetLongId() should really be got rid of.
---@param bp Blueprint
local function SetBackwardsCompatId(bp)
bp.Source = bp.Source or GetSource()
bp.BlueprintId = lower(bp.Source)
end
--- Figure out what to name this blueprint based on the name of the file it came from.
--- Returns the full resource name except with ".bp" stripped off
---@param bp Blueprint
local function SetLongId(bp)
bp.Source = bp.Source or GetSource()
if not bp.BlueprintId then
local id = lower(bp.Source)
id = gsub(id, "%.bp$", "") -- strip trailing .bp
--id = gsub(id, "/([^/]+)/%1_([a-z]+)$", "/%1_%2") -- strip redundant directory name
bp.BlueprintId = id
end
end
--- Figure out what to name this blueprint based on the name of the file it came from.
--- Returns just the base filename, without any blueprint type info or extension. Used
--- for units only.
---@param bp Blueprint
local function SetShortId(bp)
bp.Source = bp.Source or GetSource()
bp.BlueprintId = bp.BlueprintId or
gsub(lower(bp.Source), "^.*/([^/]+)_[a-z]+%.bp$", "%1")
end
--- If the bp contains a 'Mesh' section, move that over to a separate Mesh blueprint, and
--- point bp.MeshBlueprint at it.
---
--- Also fill in a default value for bp.MeshBlueprint if one was not given at all.
---@param bp Blueprint
function ExtractMeshBlueprint(bp)
local disp = bp.Display or {}
bp.Display = disp
if disp.MeshBlueprint == '' then
LOG('Warning: ', bp.Source, ': MeshBlueprint should not be an empty string')
disp.MeshBlueprint = nil
end
if type(disp.MeshBlueprint) == 'string' then
if disp.MeshBlueprint ~= lower(disp.MeshBlueprint) then
--Should we allow mixed-case blueprint names?
--LOG('Warning: ', bp.Source, ' (MeshBlueprint): ', 'Blueprint IDs must be all lowercase')
disp.MeshBlueprint = lower(disp.MeshBlueprint)
end
-- strip trailing .bp
disp.MeshBlueprint = gsub(disp.MeshBlueprint, "%.bp$", "")
if disp.Mesh then
LOG('Warning: ', bp.Source, ' has mesh defined both inline and by reference')
end
end
if disp.MeshBlueprint == nil then
-- For a blueprint file "/units/uel0001/uel0001_unit.bp", the default
-- mesh blueprint is "/units/uel0001/uel0001_mesh"
local meshname,subcount = gsub(bp.Source, "_[a-z]+%.bp$", "_mesh")
if subcount == 1 then
disp.MeshBlueprint = meshname
end
if type(disp.Mesh) == 'table' then
local meshbp = disp.Mesh
meshbp.Source = meshbp.Source or bp.Source
meshbp.BlueprintId = disp.MeshBlueprint
-- roates: Commented out so the info would stay in the unit BP and I could use it to precache by unit.
-- disp.Mesh = nil
MeshBlueprint(meshbp)
end
end
end
function ExtractWreckageBlueprint(bp)
local meshid = bp.Display.MeshBlueprint
if not meshid then return end
local meshbp = original_blueprints.Mesh[meshid]
if not meshbp then return end
local wreckbp = table.deepcopy(meshbp)
if wreckbp.LODs then
for _, lod in wreckbp.LODs do
if lod.ShaderName == 'TMeshAlpha' or
lod.ShaderName == 'NormalMappedAlpha' or
lod.ShaderName == 'UndulatingNormalMappedAlpha'
then
lod.ShaderName = 'BlackenedNormalMappedAlpha'
else
lod.ShaderName = 'Wreckage'
lod.SpecularName = '/env/common/props/wreckage_noise.dds'
end
end
end
wreckbp.BlueprintId = meshid .. '_wreck'
bp.Display.MeshBlueprintWrecked = wreckbp.BlueprintId
MeshBlueprint(wreckbp)
end
local aeonBuildNoAnimation = "AeonBuildNoAnimation"
--- Allows us to specify build shaders on a per-unit basis
local uniqueBuildAnimations = {}
uniqueBuildAnimations["ual0401"] = aeonBuildNoAnimation
uniqueBuildAnimations["uas0401"] = aeonBuildNoAnimation
--- Extracts the build mesh blueprint information. Adds the Display.BuildMeshBlueprint field to the blueprint and registers the build mesh.
---@param bp UnitBlueprint The blueprint to generate the build mesh for.
function ExtractBuildMeshBlueprint(bp)
local FactionName = bp.General.FactionName
local isSubCommander = false
if bp.Categories then
for _, v in bp.Categories do
if v == 'SUBCOMMANDER' then
isSubCommander = true
break
end
end
end
-- determine build mesh blueprint and add it to the game
if FactionName == 'Aeon' or FactionName == 'UEF' or FactionName == 'Cybran' or FactionName == 'Seraphim' then
-- no reason to have a build mesh if you have no mesh at all
local meshid = bp.Display.MeshBlueprint
if not meshid then return end
-- no reason to have a build mesh if regular mesh is not defined properly
local meshbp = original_blueprints.Mesh[meshid]
if not meshbp then return end
-- default shader name
local shadername = FactionName .. 'Build'
-- shader for specific unit BPs
shadername = uniqueBuildAnimations[string.lower(bp.BlueprintId)] or shadername
-- shader for specific SACUs
if isSubCommander and FactionName == 'Aeon' then
shadername = aeonBuildNoAnimation
end
-- build effects
local secondaryname = '/textures/effects/' .. FactionName .. 'BuildSpecular.dds'
-- copy over original content and switch up elements
local buildmeshbp = table.deepcopy(meshbp)
if buildmeshbp.LODs then
for _, lod in buildmeshbp.LODs do
lod.ShaderName = shadername
lod.SecondaryName = secondaryname
if FactionName == 'Seraphim' then
lod.LookupName = '/textures/environment/Falloff_seraphim_lookup.dds'
end
end
end
-- set its blueprint id and register it
buildmeshbp.BlueprintId = meshid .. '_build'
bp.Display.BuildMeshBlueprint = buildmeshbp.BlueprintId
MeshBlueprint(buildmeshbp)
end
end
---@param bp MeshBlueprint
function MeshBlueprint(bp)
-- fill in default values
SetLongId(bp)
StoreBlueprint('Mesh', bp)
end
---@param bp UnitBlueprint
function UnitBlueprint(bp)
-- save info about mods that changed this blueprint
bp.Mod = current_mod
SetShortId(bp)
StoreBlueprint('Unit', bp)
end
---@param bp PropBlueprint
function PropBlueprint(bp)
SetBackwardsCompatId(bp)
StoreBlueprint('Prop', bp)
end
---@param bp ProjectileBlueprint
function ProjectileBlueprint(bp)
-- save info about mods that changed this blueprint
bp.Mod = current_mod
SetBackwardsCompatId(bp)
StoreBlueprint('Projectile', bp)
end
---@param bp TrailBlueprint
function TrailEmitterBlueprint(bp)
SetBackwardsCompatId(bp)
StoreBlueprint('TrailEmitter', bp)
end
---@param bp EmitterBlueprint
function EmitterBlueprint(bp)
SetBackwardsCompatId(bp)
StoreBlueprint('Emitter', bp)
end
---@param bp BeamBlueprint
function BeamBlueprint(bp)
SetBackwardsCompatId(bp)
StoreBlueprint('Beam', bp)
end
function ExtractAllMeshBlueprints()
for _, bp in original_blueprints.Unit do
ExtractMeshBlueprint(bp)
ExtractWreckageBlueprint(bp)
ExtractBuildMeshBlueprint(bp)
end
for _, bp in original_blueprints.Prop do
ExtractMeshBlueprint(bp)
ExtractWreckageBlueprint(bp)
end
for _, bp in original_blueprints.Projectile do
ExtractMeshBlueprint(bp)
end
end
---@param blueprints BlueprintsTable
function RegisterAllBlueprints(blueprints)
local function RegisterGroup(g, fun)
for id in sortedpairs(g) do
fun(g[id])
end
end
RegisterGroup(blueprints.Mesh, RegisterMeshBlueprint)
RegisterGroup(blueprints.Unit, RegisterUnitBlueprint)
RegisterGroup(blueprints.Prop, RegisterPropBlueprint)
RegisterGroup(blueprints.Projectile, RegisterProjectileBlueprint)
RegisterGroup(blueprints.TrailEmitter, RegisterTrailEmitterBlueprint)
RegisterGroup(blueprints.Emitter, RegisterEmitterBlueprint)
RegisterGroup(blueprints.Beam, RegisterBeamBlueprint)
end
-- Brute51 - Adding support for SCU presets: allows building units that get enhancements at the factory, so no need to enhance
-- after building SCU.
---@param bps UnitBlueprint[]
---@param all_bps BlueprintsTable
function HandleUnitWithBuildPresets(bps, all_bps)
-- hashing sort categories for quick lookup
local sortCategories = { ['SORTOTHER'] = true, ['SORTINTEL'] = true, ['SORTSTRATEGIC'] = true, ['SORTDEFENSE'] = true, ['SORTECONOMY'] = true, ['SORTCONSTRUCTION'] = true, }
local tempBp = {}
for _, bp in bps do
for name, preset in bp.EnhancementPresets do
-- start with clean copy of the original unit BP
tempBp = table.deepcopy(bp)
-- create BP table for the assigned preset with required info
tempBp.EnhancementPresetAssigned = {
Enhancements = table.deepcopy(preset.Enhancements),
Name = name,
BaseBlueprintId = bp.BlueprintId,
}
-- change cost of the new unit to match unit base cost + preset enhancement costs. An override is provided for cases where this is not desired.
local e, m, t = 0, 0, 0
if not preset.BuildCostEnergyOverride or not preset.BuildCostMassOverride or not preset.BuildTimeOverride then
for _, enh in preset.Enhancements do
-- replaced continue by reversing if statement
if tempBp.Enhancements[enh] then
e = e + (tempBp.Enhancements[enh].BuildCostEnergy or 0)
m = m + (tempBp.Enhancements[enh].BuildCostMass or 0)
t = t + (tempBp.Enhancements[enh].BuildTime or 0)
-- HUSSAR added name of the enhancement so that preset units cannot be built
-- if they have restricted enhancement(s)
tempBp.CategoriesHash[enh] = true -- hashing without changing case of enhancements
else
WARN('*DEBUG: Enhancement ' .. repr(enh) .. ' used in preset ' .. repr(name) .. ' for unit ' .. repr(tempBp.BlueprintId) .. ' does not exist')
end
end
end
tempBp.Economy.BuildCostEnergy = preset.BuildCostEnergyOverride or (tempBp.Economy.BuildCostEnergy + e)
tempBp.Economy.BuildCostMass = preset.BuildCostMassOverride or (tempBp.Economy.BuildCostMass + m)
tempBp.Economy.BuildTime = preset.BuildTimeOverride or (tempBp.Economy.BuildTime + t)
-- adjust veterancy so that it is as easy to vet with preset SCUs as upgraded SCUs
if not bp.VeteranMass then
-- blueprints-units.lua gives a default multiplier of 2 for SUBCOMMANDER category units.
tempBp.VeteranMassMult = (tempBp.VeteranMassMult or 2) * bp.Economy.BuildCostMass / tempBp.Economy.BuildCostMass
end
-- teleport cost adjustments. Manually enhanced SCU with teleport is cheaper than a prebuild SCU because the latter has its cost
-- adjusted (up). This code sets bp values used in the code to calculate with different base values than the unit cost.
if preset.TeleportNoCostAdjustment ~= false then
-- set teleport cost overrides to cost of base unit
tempBp.Economy.TeleportEnergyCost = bp.Economy.BuildCostEnergy or 0
tempBp.Economy.TeleportMassCost = bp.Economy.BuildMassEnergy or 0
end
-- Add a sorting category so similar SCUs are grouped together in the build menu
if preset.SortCategory then
if sortCategories[preset.SortCategory] or preset.SortCategory == 'None' then
for k in sortCategories do
tempBp.CategoriesHash[k] = false
end
if preset.SortCategory ~= 'None' then
tempBp.CategoriesHash[preset.SortCategory] = true
end
end
end
-- change other things relevant things as well
tempBp.BaseBlueprintId = tempBp.BlueprintId
tempBp.BlueprintId = string.lower(tempBp.BlueprintId .. '_' .. name)
tempBp.BuildIconSortPriority = preset.BuildIconSortPriority or tempBp.BuildIconSortPriority or 0
tempBp.General.SelectionPriority = preset.SelectionPriority or tempBp.General.SelectionPriority or 1
tempBp.General.UnitName = preset.UnitName or tempBp.General.UnitName
tempBp.Interface = tempBp.Interface or { }
tempBp.Interface.HelpText = preset.HelpText or tempBp.Interface.HelpText
tempBp.Description = preset.Description or tempBp.Description
tempBp.CategoriesHash['ISPREENHANCEDUNIT'] = true
tempBp.CategoriesHash[string.upper(name .. 'PRESET')] = true
-- clean up some data that's not needed anymore
tempBp.CategoriesHash['USEBUILDPRESETS'] = false
tempBp.EnhancementPresets = nil
-- synchronizing Categories with CategoriesHash for compatibility
tempBp.Categories = table.unhash(tempBp.CategoriesHash)
all_bps.Unit[tempBp.BlueprintId] = tempBp
BlueprintLoaderUpdateProgress()
end
end
end
function HandleUnitsWithExternalFactories(bps, all_bps)
end
-- Assign shader and mesh for visual Cloaking FX
---@param bp UnitBlueprint
function ExtractCloakMeshBlueprint(bp)
local meshid = bp.Display.MeshBlueprint
if not meshid then return end
local meshbp = original_blueprints.Mesh[meshid]
if not meshbp then return end
local shadernameE = 'ShieldCybran'
local shadernameA = 'ShieldAeon'
local shadernameC = 'ShieldCybran'
local shadernameS = 'ShieldAeon'
local cloakmeshbp = table.deepcopy(meshbp)
if cloakmeshbp.LODs then
for _, cat in bp.Categories do
if cat == 'UEF' or cat == 'CYBRAN' then
for _, lod in cloakmeshbp.LODs do
lod.ShaderName = shadernameE
end
elseif cat == 'AEON' or cat == 'SERAPHIM' then
for _, lod in cloakmeshbp.LODs do
lod.ShaderName = shadernameA
end
end
end
end
cloakmeshbp.BlueprintId = meshid .. '_cloak'
bp.Display.CloakMeshBlueprint = cloakmeshbp.BlueprintId
MeshBlueprint(cloakmeshbp)
end
--- Adapts all unit blueprints before they're passed onto mods.
---@param all_bps BlueprintsTable All the blueprints of the game.
function PreModBlueprints(all_bps)
for _, bp in all_bps.Unit do
-- Units with no categories are skipped
if bp.Categories then
ExtractCloakMeshBlueprint(bp)
-- Construct hash-based categories
bp.CategoriesHash = table.hash(bp.Categories)
-- Allow to add or delete categories for mods
if bp.DelCategories then
for _, v in bp.DelCategories do
bp.CategoriesHash[v] = false
end
bp.DelCategories = nil
end
if bp.AddCategories then
for _, v in bp.AddCategories do
bp.CategoriesHash[v] = true
end
bp.AddCategories = nil
end
-- Add common category values for easier lookup
-- Add tech category
for _, category in {'EXPERIMENTAL', 'SUBCOMMANDER', 'COMMAND', 'TECH1', 'TECH2', 'TECH3'} do
if bp.CategoriesHash[category] then
bp.TechCategory = category
break
end
end
-- Add layer category
for _, category in {'LAND', 'AIR', 'NAVAL'} do
if bp.CategoriesHash[category] then
bp.LayerCategory = category
break
end
end
-- Add faction category
bp.FactionCategory = string.upper(bp.General.FactionName or 'Unknown')
-- Adjust weapon blueprints
for i, w in bp.Weapon or {} do
-- add in weapon blueprint id
local label = w.Label or "Unlabelled"
w.BlueprintId = bp.BlueprintId .. "-" .. i .. "-" .. label
end
-- Hotfix for naval wrecks
if bp.CategoriesHash.NAVAL and not bp.Wreckage then
bp.Wreckage = {
Blueprint = '/props/DefaultWreckage/DefaultWreckage_prop.bp',
EnergyMult = 0,
HealthMult = 0.9,
MassMult = 0.9,
ReclaimTimeMultiplier = 1,
WreckageLayers = {
Air = false,
Land = false,
Seabed = true,
Sub = true,
Water = true,
},
}
end
-- Synchronize hashed categories with actual categories
bp.Categories = table.unhash(bp.CategoriesHash)
end
BlueprintLoaderUpdateProgress()
end
end
-- Hook for mods to manipulate the entire blueprint table
---@param all_bps BlueprintsTable
function ModBlueprints(all_bps)
end
local NewDummies = {}
local function GetFoot(bp, axe) return math.ceil(bp.Footprint and bp.Footprint[axe] or bp[axe] or 1) end
local function GetSkirt(bp, axe) return math.max((bp.Physics and bp.Physics['Skirt'..axe] or 1), GetFoot(bp, axe)) end
local function GetOffset(bp, axe) return (bp.Physics and bp.Physics['SkirtOffset'..axe] or 0) end
local function ReduceFoot(val)
local modded = math.mod(val, 2)
return modded == 0 and 2 or modded
end
local function NewOffset(offset, foot, reduced)
return offset-(foot-reduced)/2
end
local function SpawnMenuDummyChanges(all_bps)
for id, bp in pairs(all_bps) do
-- allow all mobile units to be dragged while being built
if bp.Categories and table.find(bp.Categories, 'MOBILE') then
table.insert(bp.Categories, 'DRAGBUILD')
end
if bp.Physics and bp.Physics.MotionType ~= 'RULEUMT_Air' then
local FootX, FootZ = GetFoot(bp, 'SizeX'), GetFoot(bp, 'SizeZ')
local SkirtX, SkirtZ = GetSkirt(bp, 'SizeX'), GetSkirt(bp, 'SizeZ')
local ReducedFootX, ReducedFootZ = ReduceFoot(FootX), ReduceFoot(FootZ)
local SOffsetX, SOffsetZ = NewOffset(GetOffset(bp, 'X'), FootX, ReducedFootX), NewOffset(GetOffset(bp, 'Z'), FootZ, ReducedFootZ)
local DummyID = 'spawn_dummy_'..SkirtX..SkirtZ..'_'..SOffsetX..SOffsetZ..'_'..ReducedFootX..ReducedFootZ
if not NewDummies[DummyID] then
NewDummies[DummyID] = {
BlueprintId = DummyID,
Categories = {
'DRAGBUILD',
'UNSPAWNABLE',
},
Display = {
BuildMeshBlueprint = '/meshes/game/nil_mesh',
MeshBlueprint = '/meshes/game/nil_mesh',
UniformScale = 0,
HideLifebars = true,
},
Intel = {
WaterVisionRadius = 0,
},
Physics = {
SkirtOffsetX = SOffsetX,
SkirtOffsetZ = SOffsetZ,
SkirtSizeX = SkirtX,
SkirtSizeZ = SkirtZ,
MotionType = 'RULEUMT_Air',
MaxSpeed = 0.5,
},
--ScriptClass = 'BrewLANFootprintDummyUnit',
--ScriptModule = '/lua/defaultunits.lua',
SizeX = ReducedFootX,
SizeY = 1,
SizeZ = ReducedFootZ,
Source = bp.Source,
}
end
bp.SpawnDummyId = DummyID
else
bp.SpawnDummyId = id -- Aircraft can be themselves.
end
end
for id, bp in NewDummies do
all_bps[id] = bp
end
end
---@param all_bps BlueprintsTable
function PostModBlueprints(all_bps)
-- Brute51: Modified code for ship wrecks and added code for SCU presets.
-- removed the pairs() function call in the for loops for better efficiency and because it is not necessary.
local preset_bps = {}
SpawnMenuDummyChanges(all_bps.Unit)
for _, bp in all_bps.Unit do
-- skip units without categories
if bp.Categories then
-- check if blueprint was changed in ModBlueprints(all_bps)
if bp.Mod or table.getsize(bp.CategoriesHash) ~= table.getsize(bp.Categories) then
bp.CategoriesHash = table.hash(bp.Categories)
end
if bp.CategoriesHash.USEBUILDPRESETS then
-- HUSSAR adding logic for finding issues in enhancements table
local issues = {}
if not bp.Enhancements then table.insert(issues, 'no Enhancements value') end
if type(bp.Enhancements) ~= 'table' then table.insert(issues, 'no Enhancements table') end
if not bp.EnhancementPresets then table.insert(issues, 'no EnhancementPresets value') end
if type(bp.EnhancementPresets) ~= 'table' then table.insert(issues, 'no EnhancementPresets table') end
-- check blueprint, if correct info for presets then put this unit on the list to handle later
if table.empty(issues) then
table.insert(preset_bps, table.deepcopy(bp))
else
issues = table.concat(issues, ', ')
WARN('UnitBlueprint ' .. repr(bp.BlueprintId) .. ' has a category USEBUILDPRESETS but ' .. issues)
end
end
end
BlueprintLoaderUpdateProgress()
end
HandleUnitWithBuildPresets(preset_bps, all_bps)
-- find custom strategic icons defined by ui mods, this should be the very last thing
-- we do before releasing the blueprint values to the game as we want to catch all
-- units, even those included by mods
FindCustomStrategicIcons(all_bps)
BlueprintLoaderUpdateProgress()
-- dynamically compute the unit threat values that are used by the AI to make sense
-- of a units capabilities
SetThreatValuesOfUnits(all_bps.Unit)
BlueprintLoaderUpdateProgress()
ProcessWeapons(all_bps, all_bps.Unit)
BlueprintLoaderUpdateProgress()
-- re-computes all the LODs of various entities to match the LOD with the size of the entity
CalculateLODs(all_bps)
BlueprintLoaderUpdateProgress()
-- post process units and projectiles for easier access to information and sanitizing some fields
PostProcessProjectiles(all_bps.Projectile)
PostProcessUnits(all_bps, all_bps.Unit)
PostProcessProps(all_bps.Prop)
end
--- Loads all blueprints with optional parameters
--- NOTE now it supports loading blueprints on UI-side in addition to loading on Sim-side
--- `Sim -> LoadBlueprints() - no arguments, no changes!`
--- `UI -> LoadBlueprints('*_unit.bp', {'/units'}, mods, true, true, true, taskNotifier)` used in ModsManager.lua
--- `UI -> LoadBlueprints('*_unit.bp', {'/units'}, mods, false, true, true, taskNotifier)` used in UnitsAnalyzer.lua
---@param pattern? string pattern of files to load, defaults to `'*.bp'`
---@param directories? string[] paths to load blueprints from, defaults to all directories
---@param mods? ModInfo[] mods to load blueprints from, defaults to active mods
---@param skipGameFiles? boolean skip loading original game files
---@param skipExtraction? boolean skip extraction of meshes
---@param skipRegistration? boolean skip registration of blueprints
---@param taskNotifier? TaskNotifier notifier that is updating UI when loading blueprints
function LoadBlueprints(pattern, directories, mods, skipGameFiles, skipExtraction, skipRegistration, taskNotifier)
local task = 'Blueprints Loading... '
local progress = nil