-
Notifications
You must be signed in to change notification settings - Fork 228
/
EffectUtilitiesUEF.lua
411 lines (349 loc) · 16.8 KB
/
EffectUtilitiesUEF.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
local AttachBeamEntityToEntity = AttachBeamEntityToEntity
local CreateAttachedEmitter = CreateAttachedEmitter
local CreateEmitterOnEntity = CreateEmitterOnEntity
local Random = Random
local WaitSeconds = WaitSeconds
local WaitTicks = WaitTicks
local Warp = Warp
local MathMin = math.min
local EntityCreateProjectile = moho.entity_methods.CreateProjectile
local EntityGetPositionXYZ = moho.entity_methods.GetPositionXYZ
local EntitySetScale = moho.entity_methods.SetScale
local EntityBeenDestroyed = moho.entity_methods.BeenDestroyed
local ProjectileSetVelocity = moho.projectile_methods.SetVelocity
local TrashBagAdd = TrashBag.Add
local UnitGetFractionComplete = moho.unit_methods.GetFractionComplete
local UnitShowBone = moho.unit_methods.ShowBone
local VectorCached = Vector(0, 0, 0)
-- various delays in ticks + 1
local BuildCubeGlowDuration = 2
local BuildCubeDelay = 8
local BuildCubeFirstSliceDelay = 3
local BuildCubeSlicePeriod = 12
local BuilderSlicePeriod = 6
--- Creates the default build beams that, among others, UEF engineers use to build non-UEF units
---@param builder Unit The builder
---@param unitBeingBuilt Unit The unit being build
---@param buildEffectBones string[] The effect bones of the builder
---@param buildEffectsBag TrashBag The effects bag of the builder
function CreateDefaultBuildBeams(builder, unitBeingBuilt, buildEffectBones, buildEffectsBag)
WaitTicks(1)
-- always confirm after waiting!
if IsDestroyed(unitBeingBuilt) then
return
end
local ProjectileSetVelocity = ProjectileSetVelocity
local WaitTicks = WaitTicks
local vc = VectorCached
local army = builder.Army
local ox, oy, oz = EntityGetPositionXYZ(unitBeingBuilt)
-- allocation of entity
local beamEndBuilder = builder.UEFBuildProjectile
if not beamEndBuilder then
beamEndBuilder = EntityCreateProjectile(unitBeingBuilt, '/effects/entities/UEFBuild/UEFBuild01_proj.bp', 0, 0, 0, nil, nil, nil)
builder.UEFBuildProjectile = beamEndBuilder
TrashBagAdd(builder.Trash, beamEndBuilder)
end
-- reset the state of the projectile
ProjectileSetVelocity(beamEndBuilder, 0)
TrashBagAdd(buildEffectsBag, CreateEmitterOnEntity(beamEndBuilder, army, '/effects/emitters/build_terran_glow_01_emit.bp'))
TrashBagAdd(buildEffectsBag, CreateEmitterOnEntity(beamEndBuilder, army, '/effects/emitters/sparks_08_emit.bp'))
vc[1], vc[2], vc[3] = ox, oy, oz
Warp(beamEndBuilder, vc)
-- create build beams
if buildEffectBones ~= nil then
for _, buildBone in buildEffectBones do
TrashBagAdd(buildEffectsBag, AttachBeamEntityToEntity(builder, buildBone, beamEndBuilder, -1, army, '/effects/emitters/build_beam_01_emit.bp'))
end
end
local waitTime = Random(8, 15)
local waitTimeInv = 10 / waitTime
while not (EntityBeenDestroyed(builder) or EntityBeenDestroyed(unitBeingBuilt)) do
local x, y, z = builder.GetRandomOffset(unitBeingBuilt, 1)
local px, py, pz = EntityGetPositionXYZ(beamEndBuilder)
local dx, dy, dz = waitTimeInv * (ox + x - px), waitTimeInv * (oy + y - py), waitTimeInv * (oz + z - pz)
ProjectileSetVelocity(beamEndBuilder, dx, dy, dz)
WaitTicks(waitTime)
end
end
--- Creates the slice beams that UEF engineers use to build UEF units
---@param builder Unit The builder
---@param unitBeingBuilt Unit The unit being build
---@param buildEffectBones string[] The effect bones of the builder
---@param buildEffectsBag TrashBag The effects bag of the builder
function CreateUEFBuildSliceBeams(builder, unitBeingBuilt, buildEffectBones, buildEffectsBag)
WaitTicks(1)
-- always confirm after waiting!
if IsDestroyed(unitBeingBuilt) then
return
end
-- store as locals for performance
local UnitGetFractionComplete = UnitGetFractionComplete
local ProjectileSetVelocity = ProjectileSetVelocity
local Warp = Warp
local vc = VectorCached
local army = builder.Army
-- create a projectile for the end of build effect and warp it to the unit
local beamEndBuilder = builder.UEFBuildProjectile
if not beamEndBuilder then
beamEndBuilder = EntityCreateProjectile(unitBeingBuilt, '/effects/entities/UEFBuild/UEFBuild01_proj.bp', 0, 0, 0, nil, nil, nil)
builder.UEFBuildProjectile = beamEndBuilder
TrashBagAdd(builder.Trash, beamEndBuilder)
end
-- reset the state of the projectile
ProjectileSetVelocity(beamEndBuilder, 0)
TrashBagAdd(buildEffectsBag, CreateEmitterOnEntity(beamEndBuilder, army, '/effects/emitters/build_terran_glow_01_emit.bp'))
TrashBagAdd(buildEffectsBag, CreateEmitterOnEntity(beamEndBuilder, army, '/effects/emitters/build_sparks_blue_01_emit.bp'))
-- add the build beam between the build bones and the projectile
if buildEffectBones ~= nil then
for _, BuildBone in buildEffectBones do
TrashBagAdd(buildEffectsBag, AttachBeamEntityToEntity(builder, BuildBone, beamEndBuilder, -1, army, '/effects/emitters/build_beam_01_emit.bp'))
TrashBagAdd(buildEffectsBag, CreateAttachedEmitter(builder, BuildBone, army, '/effects/emitters/flashing_blue_glow_01_emit.bp'))
end
end
local x1, z1, x2, z2, y, height = CalculateUEFBuildPoints(builder, unitBeingBuilt)
-- Determine a the velocity of our projectile, used for the scanning effect
local velX = 2 * (x2 - x1)
local velZ = 2 * (z2 - z1)
local fraction = UnitGetFractionComplete(unitBeingBuilt)
if fraction == 0 then
vc[1], vc[2], vc[3] = (x1 + x2) * 0.5, y - height, (z1 + z2) * 0.5
Warp(beamEndBuilder, vc)
WaitTicks(BuildCubeDelay)
end
-- Warp our projectile back to the initial corner and lower based on build completeness
local flipDirection = true
while not (EntityBeenDestroyed(builder) or EntityBeenDestroyed(unitBeingBuilt)) do
fraction = UnitGetFractionComplete(unitBeingBuilt)
if flipDirection then
vc[1], vc[2], vc[3] = x1, y - height * fraction, z1
Warp(beamEndBuilder, vc)
ProjectileSetVelocity(beamEndBuilder, velX, 0, velZ)
flipDirection = false
else
vc[1], vc[2], vc[3] = x2, y - height * fraction, z2
Warp(beamEndBuilder, vc)
ProjectileSetVelocity(beamEndBuilder, -velX, 0, -velZ)
flipDirection = true
end
WaitTicks(BuilderSlicePeriod)
end
end
--- Creates the build cube used by UEF structures
---@param unitBeingBuilt Unit The unit being build
---@param builder Unit The builder (unused)
---@param onBeingBuiltEffectsBag TrashBag The Effects bag of the unit being built
function CreateBuildCubeThread(unitBeingBuilt, builder, onBeingBuiltEffectsBag)
-- used to differentiate between using a slice beam and a default build beam
unitBeingBuilt.BuildingCube = true
local buildbp = unitBeingBuilt.Blueprint
local Physics = buildbp.Physics
-- cache information used throughout the function
local vc = VectorCached
local px, py, pz = EntityGetPositionXYZ(unitBeingBuilt)
py = py + (Physics.MeshExtentsOffsetY or 0)
local bx = Physics.MeshExtentsX or buildbp.Footprint.SizeX
local bz = Physics.MeshExtentsZ or buildbp.Footprint.SizeZ
local by = Physics.MeshExtentsY or buildbp.Footprint.SizeY or MathMin(bx, bz)
-- create a quick glow effect
local proj = EntityCreateProjectile(unitBeingBuilt, '/effects/Entities/UEFBuildEffect/UEFBuildEffect02_proj.bp', 0, 0, 0)
EntitySetScale(proj, bx * 1.05, by * 0.2, bz * 1.05)
WaitTicks(BuildCubeGlowDuration)
-- always check if we're still there after waiting
if EntityBeenDestroyed(unitBeingBuilt) then
return
end
-- create the build cube
local buildBaseEffect = EntityCreateProjectile(unitBeingBuilt, '/effects/Entities/UEFBuildEffect/UEFBuildEffect03_proj.bp', 0, 0, 0)
TrashBagAdd(onBeingBuiltEffectsBag, buildBaseEffect)
TrashBagAdd(unitBeingBuilt.Trash, buildBaseEffect)
-- warp it to the build site
vc[1], vc[2], vc[3] = px, py - by, pz
Warp(buildBaseEffect, vc)
-- scale it accordingly
EntitySetScale(buildBaseEffect, bx, by, bz)
-- it warps at y = 0 (relative to the unit), move up until the build cube completely covered the unit
ProjectileSetVelocity(buildBaseEffect, 0, 1.4 * by, 0)
WaitTicks(BuildCubeDelay)
-- always check if we're still there after waiting
if EntityBeenDestroyed(unitBeingBuilt) or EntityBeenDestroyed(buildBaseEffect) then
return
end
-- build cube is where it should be
ProjectileSetVelocity(buildBaseEffect, 0)
-- update internal state
UnitShowBone(unitBeingBuilt, 0, true)
if unitBeingBuilt.HideLandBones then
-- non-structure units that use this setup do not have the land bones function
-- we can't upvalue this, it is a Lua function
unitBeingBuilt:HideLandBones()
end
unitBeingBuilt.BeingBuiltShowBoneTriggered = true
local lComplete = UnitGetFractionComplete(unitBeingBuilt)
WaitSeconds(BuildCubeFirstSliceDelay)
-- always check if we're still there after waiting
if EntityBeenDestroyed(unitBeingBuilt) or EntityBeenDestroyed(buildBaseEffect) then
return
end
-- create glow slice cuts and resize base cube
local slice = nil
local cComplete = UnitGetFractionComplete(unitBeingBuilt)
while not EntityBeenDestroyed(unitBeingBuilt) and cComplete < 1.0 do
if lComplete < cComplete and not EntityBeenDestroyed(buildBaseEffect) then
-- create a glow effect
local effectY = by * (1 - cComplete)
proj = EntityCreateProjectile(
buildBaseEffect,
'/effects/Entities/UEFBuildEffect/UEFBuildEffect02_proj.bp',
0, effectY, 0
)
TrashBagAdd(onBeingBuiltEffectsBag, proj)
-- size the build cube and the glow effect
slice = cComplete - lComplete
EntitySetScale(proj, bx, by * slice, bz)
EntitySetScale(buildBaseEffect, bx, effectY, bz)
end
WaitTicks(BuildCubeSlicePeriod)
-- always check if we're still there after waiting
if not EntityBeenDestroyed(unitBeingBuilt) then
lComplete = cComplete
cComplete = UnitGetFractionComplete(unitBeingBuilt)
end
end
-- keep track of state on the unit itself
unitBeingBuilt.BuildingCube = nil
end
--- Creates the UEF unit being built effects
---@param builder Unit The builder
---@param unitBeingBuilt Unit The unit being build (unused)
---@param buildEffectsBag TrashBag The effects bag of the unit being built
function CreateUEFUnitBeingBuiltEffects(builder, unitBeingBuilt, buildEffectsBag)
local buildAttachBone = builder.Blueprint.Display.BuildAttachBone
TrashBagAdd(buildEffectsBag, CreateAttachedEmitter(builder, buildAttachBone, builder.Army, '/effects/emitters/uef_mobile_unit_build_01_emit.bp'))
end
--- Creates the commander-like slice beams where two beams originate from the build effect bones instead of one.
--- @param builder Unit an (S)ACU
--- @param unitBeingBuilt Unit The unit being build
--- @param buildEffectBones string[] The effect bones of the builder
--- @param buildEffectsBag TrashBag The effects bag of the builder
function CreateUEFCommanderBuildSliceBeams(builder, unitBeingBuilt, buildEffectBones, buildEffectsBag)
WaitTicks(1)
-- localize for optimal access
local Warp = Warp
local WaitTicks = WaitTicks
local ProjectileSetVelocity = ProjectileSetVelocity
local UnitGetFractionComplete = UnitGetFractionComplete
local vc = VectorCached
local army = builder.Army
-- create a projectiles for the end of build effect and warp it to the unit
local beamEndBuilder = builder.UEFBuildProjectile
if not beamEndBuilder then
beamEndBuilder = EntityCreateProjectile(unitBeingBuilt, '/effects/entities/UEFBuild/UEFBuild01_proj.bp', 0, 0, 0, nil, nil, nil)
builder.UEFBuildProjectile = beamEndBuilder
TrashBagAdd(builder.Trash, beamEndBuilder)
end
-- second beam for the commander
local beamEndBuilder2 = builder.UEFBuildProjectile2
if not beamEndBuilder2 then
beamEndBuilder2 = EntityCreateProjectile(unitBeingBuilt, '/effects/entities/UEFBuild/UEFBuild01_proj.bp', 0, 0, 0, nil, nil, nil)
builder.UEFBuildProjectile2 = beamEndBuilder2
TrashBagAdd(builder.Trash, beamEndBuilder2)
end
-- reset the state of the projectiles
ProjectileSetVelocity(beamEndBuilder, 0)
ProjectileSetVelocity(beamEndBuilder2, 0)
TrashBagAdd(buildEffectsBag, CreateEmitterOnEntity(beamEndBuilder, army, '/effects/emitters/build_terran_glow_01_emit.bp'))
TrashBagAdd(buildEffectsBag, CreateEmitterOnEntity(beamEndBuilder, army, '/effects/emitters/build_sparks_blue_01_emit.bp'))
TrashBagAdd(buildEffectsBag, CreateEmitterOnEntity(beamEndBuilder2, army, '/effects/emitters/build_terran_glow_01_emit.bp'))
TrashBagAdd(buildEffectsBag, CreateEmitterOnEntity(beamEndBuilder2, army, '/effects/emitters/build_sparks_blue_01_emit.bp'))
-- add the build beams between the build bones and the projectiles
if buildEffectBones ~= nil then
for _, buildBone in buildEffectBones do
TrashBagAdd(buildEffectsBag, AttachBeamEntityToEntity(builder, buildBone, beamEndBuilder, -1, army, '/effects/emitters/build_beam_01_emit.bp'))
TrashBagAdd(buildEffectsBag, AttachBeamEntityToEntity(builder, buildBone, beamEndBuilder2, -1, army, '/effects/emitters/build_beam_01_emit.bp'))
TrashBagAdd(buildEffectsBag, CreateAttachedEmitter(builder, buildBone, army, '/effects/emitters/flashing_blue_glow_01_emit.bp'))
end
end
local x1, z1, x2, z2, y, height = CalculateUEFBuildPoints(builder, unitBeingBuilt)
-- determine a the velocity of our projectile, used for the scanning effect
local velX = 2 * (x2 - x1)
local velZ = 2 * (z2 - z1)
local fraction = UnitGetFractionComplete(unitBeingBuilt)
if fraction == 0 then
vc[1], vc[2], vc[3] = (x1 + x2) * 0.5, y - height, (z1 + z2) * 0.5
Warp(beamEndBuilder, vc)
Warp(beamEndBuilder2, vc)
WaitTicks(BuildCubeDelay)
end
-- Warp our projectile back to the initial corner and lower based on build completeness
-- CrossWire cheat, but its fair game
local flipDirection = true
while not (builder.Dead or unitBeingBuilt.Dead) do
fraction = UnitGetFractionComplete(unitBeingBuilt)
if flipDirection then
vc[1], vc[2], vc[3] = x1, y - height * fraction, z1
Warp(beamEndBuilder, vc)
ProjectileSetVelocity(beamEndBuilder, velX, 0, velZ)
vc[1], vc[3] = x2, z2
Warp(beamEndBuilder2, vc)
ProjectileSetVelocity(beamEndBuilder2, -velX, 0, -velZ)
flipDirection = false
else
vc[1], vc[2], vc[3] = x2, y - height * fraction, z2
Warp(beamEndBuilder, vc)
ProjectileSetVelocity(beamEndBuilder, -velX, 0, -velZ)
vc[1], vc[3] = x1, z1
Warp(beamEndBuilder2, vc)
ProjectileSetVelocity(beamEndBuilder2, velX, 0, velZ)
flipDirection = true
end
WaitTicks(BuilderSlicePeriod)
end
end
--- Calculates the build points for the UEF slicing-style build lasers
---@param builder Unit
---@param unitBeingBuilt Unit
---@return number x1, number z1 the first point
---@return number x2, number z2 the second point
---@return number y the y coord of both points
---@return number height the height of the build size
function CalculateUEFBuildPoints(builder, unitBeingBuilt)
local buildbp = unitBeingBuilt.Blueprint
local Physics = buildbp.Physics
-- determine beam positioning on build cube, this should match sizes of CreateBuildCubeThread
local sx = Physics.MeshExtentsX or buildbp.Footprint.SizeX
local sz = Physics.MeshExtentsZ or buildbp.Footprint.SizeZ
sx = sx * 0.5
sz = sz * 0.5
local sy = Physics.MeshExtentsY or buildbp.Footprint.SizeY or MathMin(sx, sz)
local x, y, z = EntityGetPositionXYZ(unitBeingBuilt)
y = y + (Physics.MeshExtentsOffsetY or 0)
local x1, z1, x2, z2
-- determine direction to builder
local px, _, pz = EntityGetPositionXYZ(builder)
local dcpx = px - x
local dcpzsz = (pz - z) * sz
-- south west / north east comparison
local dot = dcpzsz - sx * dcpx
-- if this dot product is positive, then the engineer is closer to a than it is to b
if dot > 0 then
x1 = x - sx
z1 = z + sz
else
x1 = x + sx
z1 = z - sz
end
-- north west / south east comparison
-- if this dot product is positive, then the engineer is closer to a than it is to b
if dot - 2 * dcpzsz > 0 then
x2 = x - sx
z2 = z - sz
else
x2 = x + sx
z2 = z + sz
end
-- the two vectors that represent the points
-- (x1, y+sy, z1)
-- (x2, y+sy, z2)
return x1, z1, x2, z2, y + sy, sy
end