forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proptree.lua
319 lines (263 loc) · 9.57 KB
/
proptree.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
--****************************************************************************
--**
--** File : /lua/proptree.lua
--**
--** Summary : Class for tree props that can burn and fall down and such
--**
--** Copyright 2006 Gas Powered Games, Inc. All rights reserved.
--****************************************************************************
local Prop = import("/lua/sim/prop.lua").Prop
local FireEffects = import("/lua/effecttemplates.lua").TreeBurning01
local ApplyWindDirection = import("/lua/effectutilities.lua").ApplyWindDirection
local CreateScorchMarkSplat = import("/lua/defaultexplosions.lua").CreateScorchMarkSplat
local GetRandomFloat = import("/lua/utilities.lua").GetRandomFloat
local BurningTrees = 0
local MaximumBurningTrees = 150
-- upvalue for performance
local Random = Random
local ForkThread = ForkThread
local DamageArea = DamageArea
local WaitTicks = coroutine.yield
local CreateEmitterAtEntity = CreateEmitterAtEntity
local CreateLightParticleIntel = CreateLightParticleIntel
local TrashBag = TrashBag
local TrashAdd = TrashBag.Add
local TrashDestroy = TrashBag.Destroy
local EntityMethods = moho.entity_methods
local EntityDestroy = EntityMethods.Destroy
local EntitySetMesh = EntityMethods.SetMesh
local EntityBeenDestroyed = EntityMethods.BeenDestroyed
local EffectMethods = moho.IEffect
local EffectScaleEmitter = EffectMethods.ScaleEmitter
local EffectOffsetEmitter = EffectMethods.OffsetEmitter
local EffectSetEmitterCurveParam = EffectMethods.SetEmitterCurveParam
---@class Tree : Prop
---@field Fallen? boolean
---@field Burning? boolean
---@field NoBurn? boolean
Tree = Class(Prop) {
---@param self Tree
OnDestroy = function(self)
Prop.OnDestroy(self)
-- reduce burning tree count
if self.Burning then
BurningTrees = BurningTrees - 1
end
end,
--- Collision check with projectiles
---@param self Tree
---@param other Projectile
---@return boolean
OnCollisionCheck = function(self, other)
return not self.Fallen
end,
--- Collision check with units
---@param self Tree
---@param other Unit
---@param nx number
---@param ny number
---@param nz number
---@param depth number
OnCollision = function(self, other, nx, ny, nz, depth)
if self.Fallen then
return
end
if self:BeenDestroyed() then
return
end
-- change internal state
self.Fallen = true
TrashAdd(self.Trash, ForkThread(self.FallThread, self, nx, ny, nz, depth))
self:PlayUprootingEffect(other)
end,
--- When damaged in some fashion - note that the tree can only be destroyed by disintegrating
--- damage and that the base class is not called accordingly.
---@param self Tree
---@param instigator Unit
---@param amount number
---@param direction number
---@param type DamageType
OnDamage = function(self, instigator, amount, direction, type)
if self:BeenDestroyed() then
return
end
local canFall = not self.Fallen
local canBurn = (not self.Burning) and (not self.NoBurn)
if type == 'Disintegrate' or type == "Reclaimed" then
-- we just got obliterated
EntityDestroy(self)
elseif type == 'Force' or type == "TreeForce" then
if canFall then
-- change internal state
self.NoBurn = true
self.Fallen = true
TrashAdd(self.Trash, ForkThread(self.FallThread, self, direction[1], direction[2], direction[3], 0.5))
-- change the mesh
EntitySetMesh(self, self.Blueprint.Display.MeshBlueprintWrecked)
end
elseif type == 'Nuke' and canBurn then
-- slight chance we catch fire
if Random(1, 250) < 5 then
self:Burn()
end
elseif (type == 'Fire' or type == 'TreeFire') and canBurn then
-- fire type damage, slightly higher odds to catch fire
if Random(1, 35) <= 2 then
self:Burn()
end
end
if (type ~= 'Force') and (type ~= 'Fire') and canBurn and canFall then
-- any damage type but force can cause a burn
if Random(1, 20) <= 1 then
self:Burn()
end
end
end,
--- Uprooting effect when the tree falls over
---@param self Tree
---@param instigator Unit
PlayUprootingEffect = function(self, instigator)
CreateEmitterAtEntity( self, -1, '/effects/emitters/tree_uproot_01_emit.bp' )
self:PlayPropSound('TreeFall')
end,
--- Contains all the falling logic
---@param self Tree
---@param dx number
---@param dy number
---@param dz number
---@param depth number
FallThread = function(self, dx, dy, dz, depth)
-- make it fall down
local motor = self:FallDown()
motor:Whack(dx, dy, dz, depth, true)
-- no longer be able to catch fire after a while
WaitTicks(150 + Random(0, 50))
self.NoBurn = true
-- make it sink after a while
WaitTicks(150 + Random(0, 50))
self:SinkAway(-.1)
-- get rid of it when it is completely below the terrain
WaitTicks(100)
EntityDestroy(self)
end,
---@param self Tree
Burn = function(self)
-- limit maximum number of burning trees on the map
if Random(1, MaximumBurningTrees) > BurningTrees then
BurningTrees = BurningTrees + 1
self.Burning = true
TrashAdd(self.Trash, ForkThread(self.BurnThread, self))
end
end,
--- Contains all the burning logic
---@param self Tree
BurnThread = function(self)
-- used throughout this function
local trash = self.Trash
local position = self.CachePosition
local effect
local effects = { }
local effectsHead = 1
local fireSize = 0.75 * Random() + 0.25
-- fire effect
for k, v in FireEffects do
effect = CreateEmitterAtEntity(self, -1, v )
EffectOffsetEmitter(effect, 0, 0.15, 0)
EffectScaleEmitter(effect, 3)
-- keep track
effects[effectsHead] = effect
effectsHead = effectsHead + 1
-- add it to trash bag
TrashAdd(trash, effect)
end
-- add randomness to direction of smoke
ApplyWindDirection(effects[3], 1.0)
-- light splash
effect = CreateLightParticleIntel( self, -1, -1, 1.5, 10, 'glow_03', 'ramp_flare_02' )
-- sounds
self:PlayPropSound('BurnStart')
self:PlayPropAmbientSound('BurnLoop')
-- wait a bit before we change to a scorched tree
WaitTicks(50 + Random(0, 10))
EntitySetMesh(self, self.Blueprint.Display.MeshBlueprintWrecked)
-- more fire effects
for i = 5, 1, -1 do
-- do not change the distort effect
effects[1]:ScaleEmitter(3 + fireSize * (5 - i))
effects[3]:ScaleEmitter(3 + fireSize * (5 - i))
-- hold up a bit
WaitTicks(20 + Random(10, 50))
-- try and spread out the fire
if i == 3 then
DamageArea(self, position, 1, 1, 'TreeFire', true)
end
end
-- wait a bit before we make a scorch mark
WaitTicks(50 + Random(0, 10))
CreateScorchMarkSplat( self, 0.5, -1 )
-- try and spread the fire
DamageArea(self, position, 1, 1, 'TreeFire', true)
-- stop all sound
self:PlayPropAmbientSound(nil)
-- destroy all effects
for k = 1, effectsHead - 1 do
effects[k]:Destroy()
end
-- add smoke effect removed when the tree is destroyed
effect = CreateEmitterAtEntity(self, -1, FireEffects[3] )
EffectScaleEmitter(effect, 2 + Random())
ApplyWindDirection(effect, 0.75)
TrashAdd(trash, effect)
-- fall down in a random direction if we didn't before
if not self.Fallen then
self.Fallen = true
self:FallThread(Random() * 2 - 1, 0, Random() * 2 - 1, 0.25)
end
end,
}
---@class TreeGroup : Prop
TreeGroup = Class(Prop) {
--- Break when colliding with a projectile of some sort
---@param self TreeGroup
---@param other string
---@return boolean
OnCollisionCheck = function(self, other)
return true
end,
--- Break when colliding with something / someone
---@param self TreeGroup
---@param other Projectile
---@param vec Vector
OnCollision = function(self, other, vec)
self:Breakup()
end,
--- Break when receiving damage
---@param self TreeGroup
---@param instigator Unit
---@param amount number
---@param direction number
---@param type DamageType
OnDamage = function(self, instigator, amount, direction, type)
self:Breakup()
end,
--- Breaks up the tree group into smaller trees
---@param self TreeGroup
---@param instigator Unit
---@param amount number
---@param direction Vector
---@param type DamageType
---@return (Tree[])?
Breakup = function(self)
-- can't do much when we're destroyed
if EntityBeenDestroyed(self) then
return
end
-- a group with a single prop type in it
if self.Blueprint.SingleTreeBlueprint then
return SplitProp(self, self.Blueprint.SingleTreeBlueprint)
-- a group with multiple prop types in it
else
return self:SplitOnBonesByName(self.Blueprint.SingleTreeDir)
end
end,
}