-
Notifications
You must be signed in to change notification settings - Fork 249
Expand file tree
/
Copy pathstart_boxes.lua
More file actions
600 lines (516 loc) · 17.7 KB
/
Copy pathstart_boxes.lua
File metadata and controls
600 lines (516 loc) · 17.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
if not gadgetHandler:IsSyncedCode() or VFS.FileExists("mission.lua") then return end
function gadget:GetInfo() return {
name = "Startbox handler",
desc = "Handles startboxes",
author = "Sprung",
date = "2015-05-19",
license = "PD",
layer = -math.huge, -- in particular, before mex_spot_finder (so mexes can depend on occupied boxes)
enabled = true,
} end
local gaiaAllyTeamID = select(6, Spring.GetTeamInfo(Spring.GetGaiaTeamID(), false))
local shuffleMode = Spring.GetModOptions().shuffle or "auto"
local startboxConfig
local isCampaignBattle = Spring.GetModOptions().singleplayercampaignbattleid and true
local ParseBoxes = VFS.Include ("LuaRules/Gadgets/Include/startbox_utilities.lua")
local function GetAverageStartpoint(boxID)
local box = startboxConfig[boxID]
local startpoints = box.startpoints
local x, z = 0, 0
for i = 1, #startpoints do
x = x + startpoints[i][1]
z = z + startpoints[i][2]
end
x = x / #startpoints
z = z / #startpoints
return x, z
end
local function RectangularizeTrapezoid(edgeA, edgeB)
local vector = Spring.Utilities.Vector
local origin = edgeA[1]
local unit = vector.Unit(vector.Subtract(edgeA[2], edgeA[1]))
if (edgeA[1][1] < edgeA[1][2]) ~= (edgeB[1][1] < edgeB[1][2]) then
-- Swap points if lines are passed backwards
edgeB[1], edgeB[2] = edgeB[2], edgeB[1]
end
local distANear, distAFar = 0, vector.AbsVal(vector.Subtract(edgeA[2], edgeA[1]))
local distBNear, distBFar = vector.Dot(vector.Subtract(edgeB[1], edgeA[1]), unit), vector.Dot(vector.Subtract(edgeB[2], edgeA[1]), unit)
local nearDist, farDist = math.max(distANear, distBNear), math.min(distAFar, distBFar)
edgeA[1] = vector.Add(origin, vector.Mult(nearDist, unit))
edgeA[2] = vector.Add(origin, vector.Mult(farDist, unit))
local normal = vector.Normal(vector.Subtract(edgeB[1], edgeA[1]), unit)
return {edgeA[1], vector.Subtract(edgeA[2], edgeA[1]), normal}
end
local function GetBoxID(allyTeamID)
local teamID = Spring.GetTeamList(allyTeamID)[1]
if not teamID then
return
end
local boxID = Spring.GetTeamRulesParam(teamID, "start_box_id") + 1 -- default to 1-indexed
return boxID
end
local function OrientRectangleToPoint(rect, point)
-- Return three vectors.
-- rect[1] is a point on the edge closest to startPos.
-- rect[2] is such that rect[1]+rect[2] defines the other point on the edge closest to startPos.
-- rect[3] is such that rect[1]+rect[3] and rect[1]+rect[2]+rect[3] are the other two points of the rectangle.
local v = Spring.Utilities.Vector
local shortIndex = 1
local shortDistSq = v.AbsValSq(v.Subtract(point, v.Add(rect[1], v.Mult(0.5, rect[2]))))
local challengeDistSq = v.AbsValSq(v.Subtract(point, v.Add(rect[1], v.Mult(0.5, rect[3]))))
if challengeDistSq < shortDistSq then
shortIndex = 2
shortDistSq = challengeDistSq
end
challengeDistSq = v.AbsValSq(v.Subtract(point, v.Add(rect[1], v.Add(rect[3], v.Mult(0.5, rect[2])))))
if challengeDistSq < shortDistSq then
shortIndex = 3
shortDistSq = challengeDistSq
end
challengeDistSq = v.AbsValSq(v.Subtract(point, v.Add(rect[1], v.Add(rect[2], v.Mult(0.5, rect[3])))))
if challengeDistSq < shortDistSq then
shortIndex = 4
shortDistSq = challengeDistSq
end
if shortIndex == 2 then
rect[2], rect[3] = rect[3], rect[2]
end
if shortIndex == 3 or shortIndex == 4 then
rect[1] = v.Add(rect[1], v.Add(rect[2], rect[3]))
rect[2] = v.Mult(-1, rect[2])
rect[3] = v.Mult(-1, rect[3])
if shortIndex == 4 then
rect[2], rect[3] = rect[3], rect[2]
end
end
return rect
end
local function GetPlanetwarsBoxes(teamDistance, teamWidth, neutralWidth, edgeDist)
local attackerBoxID = GetBoxID(0)
local defenderBoxID = GetBoxID(1)
if not attackerBoxID or not defenderBoxID then
return
end
local attackerX, attackerZ = GetAverageStartpoint(attackerBoxID)
local defenderX, defenderZ = GetAverageStartpoint(defenderBoxID)
local function GetPointOnLine(distance)
return {
defenderX + distance * (attackerX - defenderX),
defenderZ + distance * (attackerZ - defenderZ),
}
end
local defenderBoxStart = GetPointOnLine(teamDistance)
local attackerBoxStart = GetPointOnLine(1 - teamDistance)
local middleBoxStart = GetPointOnLine(0.5 - (neutralWidth / 2))
local defenderBoxEnd = GetPointOnLine(teamDistance + teamWidth)
local attackerBoxEnd = GetPointOnLine(1 - (teamDistance + teamWidth))
local middleBoxEnd = GetPointOnLine(0.5 + (neutralWidth / 2))
local function GetBasicRectangle(pointA, pointB, isX, startPos)
local box
if isX then
box = {
{edgeDist, pointA[2]},
{0, pointB[2] - pointA[2]},
{Game.mapSizeX - 2*edgeDist, 0},
}
else
box = {
{pointA[1], edgeDist},
{0, Game.mapSizeZ - 2*edgeDist},
{pointB[1] - pointA[1], 0},
}
end
return OrientRectangleToPoint(box, startPos)
end
if math.abs(defenderX - attackerX) < 10 or math.abs(defenderZ - attackerZ) < 10 then
local isX = math.abs(defenderX - attackerX) < 10
return {
attacker = GetBasicRectangle(attackerBoxStart, attackerBoxEnd, isX, {attackerX, attackerZ}),
defender = GetBasicRectangle(defenderBoxStart, defenderBoxEnd, isX, {defenderX, defenderZ}),
neutral = GetBasicRectangle(middleBoxStart, middleBoxEnd, isX, {attackerX, attackerZ}),
}
end
-- Note that the gradient is perpendicular to the gradient Attacker-Defender line.
local gradient = (attackerX - defenderX) / (defenderZ - attackerZ)
local function GetEdgePoints(point)
local offset = point[2] - (gradient * point[1])
local left = {
edgeDist,
(gradient * edgeDist) + offset,
}
if left[2] < edgeDist then
left[1] = (edgeDist - offset) / gradient
left[2] = edgeDist
elseif left[2] > Game.mapSizeZ - edgeDist then
left[1] = ((Game.mapSizeZ - edgeDist) - offset) / gradient
left[2] = Game.mapSizeZ - edgeDist
end
local right = {
Game.mapSizeX - edgeDist,
(gradient * (Game.mapSizeX - edgeDist)) + offset,
}
if right[2] < edgeDist then
right[1] = (edgeDist - offset) / gradient
right[2] = edgeDist
elseif right[2] > Game.mapSizeZ - edgeDist then
right[1] = ((Game.mapSizeZ - edgeDist) - offset) / gradient
right[2] = Game.mapSizeZ - edgeDist
end
return {left, right}
end
local function GetRectangle(pointA, pointB, startPos)
local edgesA = GetEdgePoints(pointA)
local edgesB = GetEdgePoints(pointB)
local box = RectangularizeTrapezoid(edgesA, edgesB)
return OrientRectangleToPoint(box, startPos)
end
return {
attacker = GetRectangle(attackerBoxStart, attackerBoxEnd, {attackerX, attackerZ}),
defender = GetRectangle(defenderBoxStart, defenderBoxEnd, {defenderX, defenderZ}),
neutral = GetRectangle(middleBoxStart, middleBoxEnd, {attackerX, attackerZ}),
}
end
local function GetAllyAverageStartpoint(allyTeamID)
local allyBoxID = GetBoxID(allyTeamID)
local boxX, boxZ = GetAverageStartpoint(allyBoxID)
return boxX, boxZ
end
local function CheckStartbox (boxID, x, z, ignoreDisable)
if isCampaignBattle then
return true
end
if (shuffleMode == "disable") and not ignoreDisable then
return true
end
if not boxID then
return true
end
local box = startboxConfig[boxID] and startboxConfig[boxID].boxes
if not box then
return true
end
for i = 1, #box do
local x1, z1, x2, z2, x3, z3 = unpack(box[i])
if (math.cross_product(x, z, x1, z1, x2, z2) <= 0
and math.cross_product(x, z, x2, z2, x3, z3) <= 0
and math.cross_product(x, z, x3, z3, x1, z1) <= 0
) then
return true
end
end
return false
end
-- name, elo, clanShort, clanLong, isAI
local function GetPlayerInfo (teamID)
local _,playerID,_,isAI = Spring.GetTeamInfo(teamID, false)
if isAI then
return select(2, Spring.GetAIInfo(teamID)), -1000, "", "", true
end
local name = Spring.GetPlayerInfo(playerID, false) or "?"
local customKeys = select(10, Spring.GetPlayerInfo(playerID)) or {}
local clanShort = customKeys.clan or ""
local clanLong = customKeys.clanfull or ""
local elo = customKeys.elo or "0"
return name, tonumber(elo), clanShort, clanLong, false
end
-- returns full name, short name, clan full name, clan short name
local function GetTeamNames (allyTeamID)
if allyTeamID == gaiaAllyTeamID then
return "Neutral", "Neutral" -- more descriptive than "Gaia"
end
local pwPlanet = Spring.GetModOptions().planet
if pwPlanet then
if allyTeamID == 0 then -- attacker is always 0, and always present
local shortName = Spring.GetModOptions().attackingfaction
local longName = Spring.GetModOptions().attackingfactionname
return longName, shortName
else
local defenderShortName = Spring.GetModOptions().defendingfaction
if not defenderShortName then -- attacking a neutral planet
local longName = pwPlanet .. " Militia"
return longName, "Militia"
else
local longName = Spring.GetModOptions().defendingfactionname
return longName, defenderShortName
end
end
end
local teamList = Spring.GetTeamList(allyTeamID) or {}
if #teamList == 0 then
return "Empty", "Empty"
end
local clanShortName, clanLongName
local clanFirst = true
local leaderName = ""
local leaderElo = -2000
local bots = 0
local humans = 0
for i = 1, #teamList do
local name, elo, clanShort, clanLong, isAI = GetPlayerInfo(teamList[i])
if not isAI then
if clanFirst then
clanShortName = clanShort
clanLongName = clanLong
clanFirst = false
else
if clanShort ~= clanShortName then
clanShortName = ""
clanLongName = ""
end
end
end
if elo > leaderElo then
leaderName = name
leaderElo = elo
end
if isAI then
bots = bots + 1
else
humans = humans + 1
end
end
if humans == 1 then
return leaderName, leaderName
end
if humans == 0 then
--[[ AI is treated like a clan, so it generally takes
priority unless there are multiple AI allyteams ]]
clanShortName = "AI"
clanLongName = "AI"
end
local boxCount = 0
for _ in pairs(startboxConfig) do
boxCount = boxCount + 1
end
if ((shuffleMode == "off")
or (Spring.Utilities.GetTeamCount() == 2 and shuffleMode == "shuffle")
or (boxCount == 2 and shuffleMode == "allshuffle"))
then
local boxID = Spring.GetTeamRulesParam(teamList[1], "start_box_id")
if boxID then
boxID = boxID + 1 -- default to 1-indexed
local box = startboxConfig[boxID]
if box.nameLong and box.nameShort then
return box.nameLong, box.nameShort, clanLongName, clanShortName
end
end
end
if humans == 0 then
local aiStr = "AI " .. allyTeamID
return aiStr, aiStr, clanLongName, clanShortName
end
return ("Team " .. leaderName), leaderName, clanLongName, clanShortName
end
function gadget:Initialize()
if GG.MOD_MISSION then
gadgetHandler:RemoveGadget()
return
end
if shuffleMode == "auto" then
if Spring.Utilities.GetTeamCount() > 2 then
shuffleMode = "shuffle"
elseif Spring.Utilities.GetTeamCount() == 2 then
shuffleMode = "allshuffle"
else
-- 1 team: chickens
shuffleMode = "off"
end
end
Spring.SetGameRulesParam("shuffleMode", shuffleMode)
startboxConfig = ParseBoxes()
GG.startBoxConfig = startboxConfig
GG.GetPlanetwarsBoxes = GetPlanetwarsBoxes
GG.CheckStartbox = CheckStartbox
GG.GetAllyAverageStartpoint = GetAllyAverageStartpoint
Spring.SetGameRulesParam("startbox_max_n", #startboxConfig)
Spring.SetGameRulesParam("startbox_recommended_startpos", 1)
for box_id, rawbox in pairs(startboxConfig) do
local polygons = rawbox.boxes
Spring.SetGameRulesParam("startbox_n_" .. box_id, #polygons)
for i = 1, #polygons do
local polygon = polygons[i]
Spring.SetGameRulesParam("startbox_polygon_" .. box_id .. "_" .. i, #polygons[i])
for j = 1, #polygons[i] do
Spring.SetGameRulesParam("startbox_polygon_x_" .. box_id .. "_" .. i .. "_" .. j, polygons[i][j][1])
Spring.SetGameRulesParam("startbox_polygon_z_" .. box_id .. "_" .. i .. "_" .. j, polygons[i][j][2])
end
end
local startposes = rawbox.startpoints
Spring.SetGameRulesParam("startpos_n_" .. box_id, #startposes)
for i = 1, #startposes do
Spring.SetGameRulesParam("startpos_x_" .. box_id .. "_" .. i, startposes[i][1])
Spring.SetGameRulesParam("startpos_z_" .. box_id .. "_" .. i, startposes[i][2])
end
end
for id, rawbox in pairs(startboxConfig) do
rawbox.boxes = math.triangulate(rawbox.boxes)
end
-- filter out fake teams (empty or Gaia)
local allyTeamList = Spring.GetAllyTeamList()
local actualAllyTeamList = {}
for i = 1, #allyTeamList do
local teamList = Spring.GetTeamList(allyTeamList[i]) or {}
if ((#teamList > 0) and (allyTeamList[i] ~= gaiaAllyTeamID)) then
actualAllyTeamList[#actualAllyTeamList+1] = {allyTeamList[i], math.random()}
end
end
if (shuffleMode == "off") or (shuffleMode == "disable") then
for i = 1, #allyTeamList do
local allyTeamID = allyTeamList[i]
local boxID = allyTeamList[i] + 1 -- default to 1-indexed
if startboxConfig[boxID] then
local teamList = Spring.GetTeamList(allyTeamID) or {}
for j = 1, #teamList do
Spring.SetTeamRulesParam(teamList[j], "start_box_id", boxID)
end
end
end
elseif (shuffleMode == "shuffle") then
local randomizedSequence = {}
for i = 1, #actualAllyTeamList do
randomizedSequence[#randomizedSequence + 1] = {actualAllyTeamList[i][1], math.random()}
end
table.sort(randomizedSequence, function(a, b) return (a[2] < b[2]) end)
for i = 1, #actualAllyTeamList do
local allyTeamID = actualAllyTeamList[i][1]
local boxID = randomizedSequence[i][1]
if startboxConfig[boxID] then
local teamList = Spring.GetTeamList(allyTeamID) or {}
for j = 1, #teamList do
Spring.SetTeamRulesParam(teamList[j], "start_box_id", boxID)
end
end
end
elseif (shuffleMode == "allshuffle") then
local randomizedSequence = {}
for id in pairs(startboxConfig) do
randomizedSequence[#randomizedSequence + 1] = {id, math.random()}
end
table.sort(randomizedSequence, function(a, b) return (a[2] < b[2]) end)
table.sort(actualAllyTeamList, function(a, b) return (a[2] < b[2]) end)
for i = 1, #actualAllyTeamList do
local allyTeamID = actualAllyTeamList[i][1]
local boxID = randomizedSequence[i] and randomizedSequence[i][1]
if boxID and startboxConfig[boxID] then
local teamList = Spring.GetTeamList(allyTeamID) or {}
for j = 1, #teamList do
Spring.SetTeamRulesParam(teamList[j], "start_box_id", boxID)
end
end
end
end
local clans = {}
for i = 1, #allyTeamList do
local allyTeamID = allyTeamList[i]
local longName, shortName, clanLong, clanShort = GetTeamNames(allyTeamID)
Spring.SetGameRulesParam("allyteam_short_name_" .. allyTeamID, shortName)
Spring.SetGameRulesParam("allyteam_long_name_" .. allyTeamID, longName)
if clanLong and clanLong ~= "" then
clans[clanLong] = clans[clanLong] or {0, clanShort, allyTeamID}
clans[clanLong][1] = clans[clanLong][1] + 1
end
end
for clanName, clan in pairs(clans) do
if (clan[1] == 1) and (clanName ~= "") then
Spring.SetGameRulesParam("allyteam_short_name_" .. clan[3], clan[2])
Spring.SetGameRulesParam("allyteam_long_name_" .. clan[3], clanName)
end
end
-- AllyTeam 'origin'
for i = 1, #allyTeamList do
local allyTeamID = allyTeamList[i]
local boxID = GetBoxID(allyTeamID)
if boxID then
local boxX, boxZ = GetAverageStartpoint(boxID)
if boxX and boxZ then
local teamID = Spring.GetTeamList(allyTeamID)[1]
Spring.SetTeamRulesParam(teamID, "allyteam_origin_x", boxX)
Spring.SetTeamRulesParam(teamID, "allyteam_origin_z", boxZ)
--Spring.MarkerAddPoint(boxX, 0, boxZ, "ally origin " .. allyTeamID)
end
end
end
end
function gadget:AllowStartPosition(playerID, teamID, readyState, x, y, z, rx, ry, rz)
if (x == 0 and z == 0) then
-- engine default startpos
return false
end
if (playerID == 255) then
return true -- custom AI, can't know which team it is on so allow it to place anywhere for now and filter invalid positions later
end
local teamID = select(4, Spring.GetPlayerInfo(playerID, false))
if (shuffleMode == "disable") then
-- note this is after the AI check; toasters still have to obey
Spring.SetTeamRulesParam (teamID, "valid_startpos", 1)
return true
end
local boxID = Spring.GetTeamRulesParam(teamID, "start_box_id")
if boxID then
boxID = boxID + 1 -- default to 1-indexed
end
if (not boxID) or CheckStartbox(boxID, x, z) then
Spring.SetTeamRulesParam (teamID, "valid_startpos", 1)
return true
else
return false
end
end
function gadget:RecvSkirmishAIMessage(teamID, dataStr)
local command = "ai_is_valid_startpos:"
local command2 = "ai_is_valid_enemy_startpos:"
if not dataStr:find(command,1,true) and not dataStr:find(command2,1,true) then return end
if dataStr:find(command2,1,true) then
command = command2
end
local boxID = Spring.GetTeamRulesParam(teamID, "start_box_id")
if boxID then
boxID = boxID + 1 -- default to 1-indexed
end
local xz = dataStr:sub(command:len()+1)
local slash = xz:find("/",1,true)
if not slash then return end
local x = tonumber(xz:sub(1, slash-1))
local z = tonumber(xz:sub(slash+1))
if not x or not z then return end
if not dataStr:find(command2,1,true) then
-- for checking own startpos
if (not boxID) or CheckStartbox(boxID, x, z) then
return "1"
else
return "0"
end
else
-- for checking enemy startpos
local enemyboxes = {}
local _,_,_,_,_,allyteamid = Spring.GetTeamInfo(teamID, false)
local allyteams = Spring.GetAllyTeamList()
if shuffleMode == "allshuffle" then
for id,_ in pairs(startboxConfig) do
if id ~= boxID then
enemyboxes[id] = true
end
end
else
for _,value in pairs(allyteams) do
if value ~= allyteamid then
local enemyteams = Spring.GetTeamList(value)
local _,value1 = next(enemyteams)
if value1 then
local enemybox = Spring.GetTeamRulesParam(value1, "start_box_id")
if (enemybox) then
enemybox = enemybox + 1 -- default to 1-indexed
enemyboxes[enemybox] = true
end
end
end
end
end
for bid,_ in pairs(enemyboxes) do
if CheckStartbox(bid, x, z) then
return "1"
end
end
return "0"
end
end