-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbronco-paratroopers.lua
363 lines (288 loc) · 11.8 KB
/
bronco-paratroopers.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
-- SPDX-License-Identifier: MIT
--[[
MIT License
Copyright (c) 2023 gcask <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
--]]
--[[
Creates groups from the Bronco paratroopers.
When the weapons first reach the ground, stand-ins (static) are created as placeholder
until all the paratroopers finish up.
Then, all remaining paratroopers who reached the ground safely are gathered into one group.
The systems handles multiple active drops from the same aircraft,
as well as multiple active drops from multiple aircrafts.
The script has no dependencies.
Inspired from: https://github.com/icuham/bronco-para1/blob/main/para-spawner.lua
USAGE
=====
* Add the script to your miz via a MISSION START trigger.
* You're done!
CUSTOMIZING
===========
* Look for customize: tags throughout the code for interesting points.
* It only spawns 'Soldier M4'.
If you need a more complex behavior, look at Airdrop:generateGroupData() and Airdrop:paratrooperLanded()
* AirborneParatrooper:isLandingSiteValid() if you want to tune what makes up a valid location for landing.
Right now it will spawn everywher it can but WATER.
* Airdrops:onEvent if you want more complex behavior
* Airdrop:generateUniqueGroupName if you want to tweak how 'unique' drops are.
Right now it's as unique as can be,
but if you want only one live group per airframe you could fix the generation field.
CHANGELOG
=========
2023.07.22
----------
Initial release.
--]]
-- The usual vector maths.
local vec3 = {
scale = function(vec, scale)
return { x = vec.x * scale, y = vec.y * scale, z = vec.z * scale }
end,
length = function(vec)
return (vec.x^2 + vec.y^2 + vec.z^2)^0.5
end,
add = function (left, right)
return { x = left.x + right.x, y = left.y + right.y, z = left.z + right.z }
end,
tostring = function(vec)
return string.format("{x = %.2f, y = %.2f, z = %.2f}", vec.x, vec.y, vec.z)
end,
toTerrain2D = function(vec)
-- World positions have y and z flipped around compared to the terrain2D.
return { x = vec.x, y = vec.z }
end
}
function vec3.distance(from, to)
return vec3.length({x = to.x - from.x, y = to.y - from.y, z = to.z - from.z})
end
function vec3.normalize(vec)
local length = vec3.length(vec)
return vec3.scale(vec, 1/length)
end
--[[
Track the state of the airborne paratrooper during a drop.
Owned by an Airdrop, will notify it with its potential impact point (if one is found).
--]]
local AirborneParatrooper = {
new = function(self, paratrooper, airdrop)
local instance = {
paratrooper = paratrooper,
airdrop = airdrop,
position = paratrooper:getPoint(),
velocity = paratrooper:getVelocity(),
impactPoint = nil,
lastUpdateTimeSeconds = 0
}
setmetatable(instance, self)
self.__index = self
timer.scheduleFunction(self.update, instance, timer.getTime() + 1)
return instance
end,
update = function(self, currentTimeSeconds)
local status, exists = pcall(Weapon.isExist, self.paratrooper)
local eta = nil
if status and exists then
local nextCallDelaySeconds = 1
-- For as long as we can track, update our current position, and velocity.
self.position = self.paratrooper:getPoint()
self.velocity = self.paratrooper:getVelocity()
self.lastUpdateTimeSeconds = currentTimeSeconds
-- Based on current heading and velocity, check if we're about to hit the ground
-- at 2x our default sampling rate.
local speed = math.max(vec3.length(self.velocity), 1)
local lookaheadSeconds = 2 * nextCallDelaySeconds
self.impactPoint = land.getIP(self.position, vec3.normalize(self.velocity), lookaheadSeconds * speed)
if self.impactPoint then
-- Impact point found! See if we get a chance for another update.
local distanceToImpact = vec3.distance(self.position, self.impactPoint)
eta = distanceToImpact / speed
nextCallDelaySeconds = eta / 2
end
-- If we're hitting the ground within the next second,
-- don't reschedule and handle impact.
if not eta or eta > 1 then
return currentTimeSeconds + nextCallDelaySeconds
end
end
-- Paratrooper no longer in air/just about to hit the ground.
if not self.impactPoint then
-- We might have missed the mark, do a little linear interpolation.
local delta = currentTimeSeconds - self.lastUpdateTimeSeconds
local distance = vec3.scale(self.velocity, delta)
self.impactPoint = vec3.add(self.position, distance)
end
local impact2D = vec3.toTerrain2D(self.impactPoint)
-- Did it land somewhere viable?
if not self:isLandingSiteValid(impact2D) then
impact2D = nil
end
if eta and impact2D then
timer.scheduleFunction(function(params)
params.airdrop:paratrooperLanded(params.impact)
end, {airdrop = self.airdrop, impact = impact2D}, currentTimeSeconds + eta)
else
self.airdrop:paratrooperLanded(impact2D)
end
-- Done, don't reschedule.
end,
isLandingSiteValid = function(self, impact2D)
-- customize: you can run different surface checks (could validate slope angle, check for other objects, etc).
return land.getSurfaceType(impact2D) ~= land.SurfaceType.WATER
end
}
--[[
A group of paratroopers.
Assumes paratroopers are shot in bursts.
Will create a group once all registered paratroopers for the sequence have landed.
--]]
local Airdrop = {
-- statics
prefix = "Paratroopers",
-- customize: different types/types per coalition/country etc.
unitType = "Soldier M4",
new = function(self, airdrops, initiatorID, countryId)
local instance = {
initiatorID = initiatorID,
countryId = countryId,
airdrops = airdrops,
airborne = 0,
troopers = {},
placeholders = {},
}
setmetatable(instance, self)
self.__index = self
return instance
end,
addParatrooper = function(self, parachutist)
self.airborne = self.airborne + 1
AirborneParatrooper:new(parachutist, self)
end,
paratrooperLanded = function(self, landingPos)
if landingPos then
if #self.troopers == 0 then
-- Lazily acquire a groupID.
self:acquireUniqueGroupName()
end
-- customize: Could leverage MIST here, have more complex unit setup.
self.troopers[#self.troopers + 1] = {
name = table.concat({self:getGroupName(), #self.troopers + 1}, ":"),
type = self.unitType,
x = landingPos.x,
y = landingPos.y
}
end
self.airborne = self.airborne - 1
if self.airborne == 0 then
self:destroyPlaceholders()
if #self.troopers > 0 then
coalition.addGroup(self.countryId, Group.Category.GROUND, self:generateGroupData())
else
-- No troopers made it in, release the reserved group.
local reservedGroup = Group.getByName(self:getGroupName())
if reservedGroup then reservedGroup:destroy() end
end
-- We've handed off the group,
-- you will need to call acquireUniqueGroupName() before being able to use it again.
self.getGroupName = nil
self.airdrops:completeAirdrop(self)
elseif landingPos then
-- Standins until the group is complete.
self.placeholders[#self.troopers] = coalition.addStaticObject(self.countryId, self.troopers[#self.troopers])
end
end,
destroyPlaceholders = function(self)
for _, placeholder in ipairs(self.placeholders) do
placeholder:destroy()
end
self.placeholders = {}
end,
generateGroupData = function(self)
-- customize: Could leverage MIST here.
-- Add group tasking, etc.
local group = {
task = "Ground Nothing",
name = self:getGroupName(),
units = self.troopers
}
self.troopers = {}
return group
end,
acquireUniqueGroupName = function(self)
-- Find our generation ID, in case the same aircraft did multiple drops.
self.getGroupName = function(self)
return table.concat({self.prefix, self.initiatorID, self.generation}, ":")
end
self.generation = 1
-- customize: remove this loop will only allow one group per airframe life.
while Group.getByName(self:getGroupName()) do
self.generation = self.generation + 1
end
-- lock in the group.
-- Create a placeholder group to ensure the name is reserved.
coalition.addGroup(self.countryId, Group.Category.GROUND, {
task = "Ground Nothing",
name = self:getGroupName(),
lateActivation = true,
visible = false
})
end
}
--[[
Keep track of active airdrops, manages world events.
--]]
local Airdrops = {
new = function(self)
local instance = {}
setmetatable(instance, self)
self.__index = self
return instance
end,
completeAirdrop = function(self, airdrop)
if self[airdrop.initiatorID] then
self[airdrop.initiatorID] = nil
end
airdrop.airdrops = nil
end,
getAirdrop = function(self, initiatorID, countryID)
if not self[initiatorID] then
self[initiatorID] = Airdrop:new(self, initiatorID, countryID)
end
return self[initiatorID]
end,
onEvent = function(self, event)
if event.id ~= world.event.S_EVENT_SHOT then
return
end
if event.weapon == nil then
return
end
if event.weapon:getDesc().category ~= Weapon.Category.BOMB then
return
end
if event.weapon:getTypeName() ~= "Paratrooperx5" then
return
end
-- customize: extra accept/reject logic (could limit to player airframes or other).
local launcher = event.weapon:getLauncher()
-- customize: objectID is unique to the airframe's life.
-- Could use getID()/getCallsign() to have groups unique per slot.
local initiatorID = launcher:getObjectID()
self:getAirdrop(initiatorID, event.weapon:getCountry()):addParatrooper(event.weapon)
end
}
world.addEventHandler(Airdrops:new())