-
Notifications
You must be signed in to change notification settings - Fork 9
/
factiondraft.ttslua
818 lines (720 loc) · 19.5 KB
/
factiondraft.ttslua
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
-- this script should be placed on a standard cube object with a color tint with alpha = 0
local testPlayers = false
local testPlayerCount = 6
local ignoreReach = false
local allDice = {}
local playmats = {}
local factions = {
{ name = "Marquise de Cat", color = "Orange", reachVal = 10, setupName = "Marquise"},
{ name = "Underground Duchy", color = "Brown", reachVal = 8, setupName = "Duchy"},
{ name = "Eyrie Dynasties", color = "Blue", reachVal = 7, setupName = "Eyrie"},
{ name = "Vagabond", color = "White", reachVal = 5, setupName = "Vagabond"},
{ name = "Riverfolk Company", color = "Teal", reachVal = 5, setupName = "Riverfolk"},
{ name = "Woodland Alliance", color = "Green", reachVal = 3, setupName = "Woodland"},
{ name = "Corvid Conspiracy", color = "Purple", reachVal = 3, setupName = "Corvid"},
{ name = "Vagabond (Second)", color = "Red", reachVal = 2, setupName = "Vagabond"},
{ name = "Lizard Cult", color = "Yellow", reachVal = 2, setupName = "Lizard"},
}
local reachMins = {
[1] = 0, -- for testing
[2] = 17,
[3] = 18,
[4] = 21,
[5] = 25,
[6] = 28,
}
local selections = {}
local selectionOrder = {}
local turnOrder = {}
local currentReach = 0
local xmlDefaults = {
tag = "Defaults",
children = {
{
tag = "Button",
attributes = {
textColor = "#dcdccc",
color = "#1c1c1c",
fontSize = "90",
}
},
{
tag = "Text",
attributes = {
color = "#dcdccc",
fontSize = "90",
}
}
}
}
local xmlPanelStart = {
tag = "Panel",
attributes = {
id = "start",
width = "900",
height = "1000",
showAnimation = "Grow",
hideAnimation = "Shrink",
},
children = {
{
tag = "VerticalLayout",
attributes = {
spacing = "7"
},
children = {
{ tag = "Button", attributes = {text = "Start Draft", onClick="start"}},
{ tag = "Button", attributes = {text = "Start Draft\n(Ignore Reach)", onClick="start(noreach)"}},
{ tag = "Button", attributes = {text = "Start Draft\n(Mock Draft)", onClick="start(mock)"}},
{ tag = "Button", attributes = {text = "No Draft", onClick="destruct()"}}
}
}
}
}
function onLoad()
self.setColorTint(Color(0, 0, 0, 0))
self.setLock(true)
self.UI.setXmlTable({xmlDefaults, xmlPanelStart})
playmats = find_objects_by_name("Faction Selection")
end
function getPlayers()
local allPlayers = Player.getPlayers()
local players = {}
for _, p in pairs(allPlayers) do
if p.color ~= "Black" then
table.insert(players, p)
end
end
if not testPlayers then
return players
end
local colors = Player.getAvailableColors()
while #players < testPlayerCount do
table.insert(players, {
steam_name = "player" .. #players,
steam_id = #players,
color = colors[#players]
})
end
return players
end
function start(player, value, element)
if value == "noreach" then
ignoreReach = true
elseif value == "mock" then
testPlayers = true
end
log("starting draft")
local players = getPlayers()
for _, player in ipairs(players) do
if player.changeColor then
player.changeColor("Grey")
end
end
selections = {}
local dice = {}
for i, p in ipairs(players) do
local pname = p.steam_name or p.color
dice[i] = {
name = pname,
description = p.steam_id or ""
}
end
self.UI.setXmlTable({{}})
allDice = spawnDice(dice)
Wait.time(function() rollForOrder(allDice) end, 1)
end
function rollForOrder(dice)
if #dice < 1 then
return
end
callAttention(Vector(0, 0, 0), 20)
for _, die in ipairs(dice) do
-- die.interactable = false
die.setLock(false)
die.randomize()
end
waitForRest(
dice,
function()
displayOrder(dice, Vector(0, 0, 4))
end
)
end
function waitForRest(dice, callback)
Wait.condition(
function()
Wait.time(callback, 1)
end,
function()
for _, die in ipairs(dice) do
if not die.resting then
return false
end
end
return true
end
)
end
function startDraft()
currentReach = 0
local turns = {}
local i = #allDice
for _, d in spairs(allDice, function(t, a, b) return t[a].getPosition().z < t[b].getPosition().z end) do
selectionOrder[i] = {name = d.getName(), id = d.getDescription()}
selections[d.getDescription()] = nil
i = i-1
end
local factionButtons = {
{
tag = "Text",
attributes = {
id = "reach-counter",
color = "#dcdccc",
fontSize = 30,
text = "Reach: 0/" .. reachMins[#selectionOrder]
}
},
{
tag = "Text",
attributes = {
id = "turn-display",
color = "#dcdccc",
fontSize = 30,
text = selectionOrder[1].name .. "'s Pick"
}
}
}
for i, f in ipairs(factions) do
factionButtons[i+2] = {
tag = "Button",
attributes = {
id = "btn-faction-"..i,
text = f.name .. " (" .. f.reachVal .. ")",
color = f.color,
fontSize = 30,
textColor = "#1c1c1c",
onClick = "selectFaction(".. i .. ")",
showAnimation = "Grow",
hideAnimation = "Shrink",
}
}
if i == 8 then
factionButtons[i+2].attributes.active = false
end
end
local pickerHeight = #factions * 100
local factionPicker = {
tag = "Panel",
attributes = {
position = "0 " .. (-1*(pickerHeight/2)) .. " -50",
width = "400",
color = "#1c1c1c",
padding = 7,
height = pickerHeight,
id = "btns-draft-selection",
showAnimation = "Grow",
hideAnimation = "Shrink",
active = false,
},
children = {
{
tag = "VerticalLayout",
attributes = {
spacing = 7,
},
children = factionButtons
}
}
}
self.UI.setXmlTable({factionPicker})
callAttention(Vector(0, 0, -4))
Wait.frames(
function()
self.UI.show("btns-draft-selection")
hideUnavailable()
end,
7
)
end
function callAttention(pos, distance)
for _, p in ipairs(Player.getPlayers()) do
p.lookAt({
position = self.positionToWorld(pos),
pitch = 80,
yaw = 0,
distance = distance or 10,
})
end
end
function hideUnavailable(choices)
if ignoreReach then
return
end
local playerCount = #selectionOrder
local minReach = reachMins[playerCount]
local available = {}
for i, f in ipairs(factions) do
self.UI.setAttributes(
"btn-faction-"..i,
{
interactable = false,
color = "#3f3f3f"
}
)
local taken = false
for _, s in pairs(selections) do
if s ~= nil then
if s.color == f.color then
taken = true
end
end
end
if not taken then
table.insert(available, {
id = i,
faction = f,
})
end
end
local selectedCount = 0
for _, s in pairs(selections) do
if s ~= nil then
selectedCount = selectedCount + 1
end
end
local pickable = {}
for i, v in pairs(available) do
if(v ~= nil) then
local tTemp = sCopy(available)
tTemp[i] = nil
if( validateSetup(tTemp, currentReach+v.faction.reachVal, playerCount - selectedCount-1) ) then
table.insert(pickable, v)
end
end
end
for i, v in pairs(pickable) do
if v ~= nil then
self.UI.setAttributes(
"btn-faction-"..v.id,
{
color = v.faction.color,
interactable = true,
}
)
end
end
end
function validateSetup(possible, reach, playersLeft)
local playerCount = #selectionOrder
local minReach = reachMins[playerCount]
if reach >= minReach then
return true
end
for i, v in pairs(possible) do
if(v ~= nil) then
local tTemp = sCopy(possible)
if(playersLeft <= 0) then
return false
end
local reachTmp = reach + v.faction.reachVal
if(reachTmp >= minReach) then
return true
end
tTemp[i] = nil
if( validateSetup(tTemp, reachTmp, playersLeft-1) ) then
return true
else
possible[i] = nil
end
end
end
end
function selectFaction(player, value, elem)
local nextID = ""
local nextName = ""
local currentOrderIndex = -1
for i, c in ipairs(selectionOrder) do
if selections[c.id] == nil then
nextID = c.id
nextName = c.name
currentOrderIndex = i
break
end
end
if player.steam_id ~= nextID and #(tostring(nextID)) > 1 then
broadcastToColor("Not your turn yet", player.color)
return
end
local faction = factions[tonumber(value)]
if testPlayers then
selections[nextID] = faction
else
selections[player.steam_id] = faction
end
currentReach = currentReach + faction.reachVal
self.UI.setAttributes(
"reach-counter",
{
text = "Reach: " .. currentReach .. "/" .. reachMins[#selectionOrder],
color = "#dcdccc",
}
)
self.UI.hide("btn-faction-"..value)
local bgColor = self.UI.getAttribute("btn-faction-"..value, "color")
local fgColor = self.UI.getAttribute("btn-faction-"..value, "textColor")
local text = self.UI.getAttribute("btn-faction-"..value, "text")
self.UI.setAttributes(
"btn-faction-"..value,
{
color = fgColor,
text = text .. "\n" .. nextName .. " (" .. currentOrderIndex .. ")",
textColor = bgColor,
interactable = false,
}
)
self.UI.show("btn-faction-"..value)
table.insert(turnOrder, 1, faction.color)
if value == "4" then
self.UI.show("btn-faction-8")
end
local playmat = getNextPlaymat()
Wait.frames(
function()
if playmat == nil then
return
end
playmat.call("spawnRemote", {
player = Player[faction.color],
faction = faction.setupName,
})
Wait.frames(
function()
if value == "8" then
playmat.call("setColorRemote", faction.color)
end
if value == "4" or value == "8" then
playmat.UI.setAttribute("subpanel", "active", false)
end
end,
3
)
player.changeColor(faction.color)
end,
30
)
if selectionOrder[currentOrderIndex+1] ~= nil then
self.UI.setAttribute("turn-display", "text", selectionOrder[currentOrderIndex+1].name .. "'s Pick")
hideUnavailable()
else
self.UI.setAttribute("turn-display", "text", "Draft Finished! GLHF!")
Wait.frames(endDraft, 7)
end
end
function getNextPlaymat()
for i, playmat in ipairs(playmats) do
if playmat ~= "" then
playmats[i] = ""
return playmat
end
end
return nil
end
function playerName(playerID)
for i, p in ipairs(Player.getPlayers()) do
if tostring(p.steam_id) == tostring(playerID) then
return p.steam_name or player.color
end
end
return tostring(playerID)
end
function endDraft()
for _, d in ipairs(allDice) do
d.destruct()
end
Turns.enable = true
Turns.type = 2
Turns.order = turnOrder
Turns.turn_color = turnOrder[1]
for i, f in ipairs(factions) do
self.UI.setAttribute("btn-faction-"..i, "interactable", false)
end
self.UI.hide("btns-draft-selection")
local factionButtons = {
{
tag = "Text",
attributes = {
id = "turn-display",
color = "#dcdccc",
fontSize = 30,
text = "Draft Finished! GLHF!"
}
},
{
tag = "Text",
attributes = {
id = "turn-display",
color = "#dcdccc",
fontSize = 30,
text = "Turn Order:"
}
}
}
for i, c in ipairs(turnOrder) do
local id = (Player[c].steam_id) or c
local faction = nil
for i, f in ipairs(factions) do
if c == f.color then
faction = f
break
end
end
table.insert(
factionButtons,
{
tag = "Button",
attributes = {
id = "btn-faction-"..i,
text = faction.name .. "\n" .. playerName(id),
textColor = faction.color,
interactable = false,
fontSize = 30,
color = "#1c1c1c",
}
}
)
end
table.insert(
factionButtons,
{
tag = "Button",
attributes = {
text = "Remove",
textColor = "#dcdccc",
fontSize = 30,
color = "#1c1c1c",
onClick = "destruct()"
}
}
)
local pickerHeight = #factions * 100
local factionPicker = {
tag = "Panel",
attributes = {
position = "0 " .. (-1*(pickerHeight/2)) .. " -50",
width = "400",
color = "#1c1c1c",
id = "btns-draft-selection",
padding = 7,
height = pickerHeight,
showAnimation = "Grow",
hideAnimation = "Shrink",
active = false,
},
children = {
{
tag = "VerticalLayout",
attributes = {
spacing = 7,
},
children = factionButtons
}
}
}
Wait.frames(
function()
self.UI.setXmlTable({factionPicker})
Wait.frames(
function()
self.UI.show("btns-draft-selection")
end, 3)
end
, 3)
end
function destruct()
self.destruct()
end
function displayOrder(dice, pos)
local i = 0
local groups = {}
for _, d in spairs(dice, function(t, a, b) return t[a].getRotationValue() < t[b].getRotationValue() end) do
local val = d.getRotationValue()
d.setPositionSmooth(self.positionToWorld(pos + Vector(0, 0.5, i*1.5)))
i = i+1
if groups[val] == nil then
groups[val] = {d}
else
table.insert(groups[val], d)
end
broadcastToAll(d.getName() .. " rolled " .. val)
end
callAttention(self.positionToLocal(dice[1].getPosition()), 20)
waitForRest(
dice,
function()
local rollingOff = false
for _, g in pairs(groups) do
if #g > 1 then
local roPlayers = {}
for _, d in ipairs(g) do
table.insert(roPlayers, d.getName())
end
broadcastToAll("Rolling off for tie: " .. table.concat(roPlayers, ", "))
rollingOff = true
Wait.time(function() doRolloff(g) end, 1)
end
end
if not rollingOff then
Wait.time(
function()
waitForRest(allDice, startDraft)
end,
1
)
end
end
)
end
function doRolloff(dice)
if #dice < 2 then
return
end
local origin = self.positionToLocal(dice[1].getPosition())
callAttention(origin, 20)
for _, die in ipairs(dice) do
die.setLock(true)
die.setPositionSmooth(die.getPosition()+Vector(0, 2, 0))
end
waitForRest(
dice,
function()
for _, die in ipairs(dice) do
die.setLock(false)
die.randomize()
end
waitForRest(
dice,
function()
displayOrder(dice, origin)
end
)
end
)
end
function spairs(t, order)
-- collect the keys
local keys = {}
for k in pairs(t) do keys[#keys+1] = k end
-- if order function given, sort by it by passing the table and keys a, b,
-- otherwise just sort the keys
if order then
table.sort(keys, function(a,b) return order(t, a, b) end)
else
table.sort(keys)
end
-- return the iterator function
local i = 0
return function()
i = i + 1
if keys[i] then
return keys[i], t[keys[i]]
end
end
end
function spawnDice(toSpawn)
local dice = {}
for i, dieConf in ipairs(toSpawn) do
local name = dieConf.name or ""
local angleStep = 360 / (#dice+1)
for i, die in ipairs(dice) do
local pos = findGlobalPosWithLocalDirection(angleStep*(i-1))
die.setPositionSmooth(pos, false, true)
end
local rollLocation = findGlobalPosWithLocalDirection(360-angleStep)
local spawnedDie = spawnObject({
type = "Die_20",
rotation = randomRotation(),
position = rollLocation,
sound = false,
callback_function = function(die)
die.setLock(true)
die.setColorTint(Color.Black)
die.setName(name)
die.setDescription(dieConf.description or "")
end
})
table.insert(dice, spawnedDie)
end
return dice
end
local radialOffset = 2.2
local heightOffset = 2.2
function randomRotation()
--Credit for this function goes to Revinor (forums)
--Get 3 random numbers
local u1 = math.random()
local u2 = math.random()
local u3 = math.random()
--Convert them into quats to avoid gimbal lock
local u1sqrt = math.sqrt(u1)
local u1m1sqrt = math.sqrt(1-u1)
local qx = u1m1sqrt *math.sin(2*math.pi*u2)
local qy = u1m1sqrt *math.cos(2*math.pi*u2)
local qz = u1sqrt *math.sin(2*math.pi*u3)
local qw = u1sqrt *math.cos(2*math.pi*u3)
--Apply rotation
local ysqr = qy * qy
local t0 = -2.0 * (ysqr + qz * qz) + 1.0
local t1 = 2.0 * (qx * qy - qw * qz)
local t2 = -2.0 * (qx * qz + qw * qy)
local t3 = 2.0 * (qy * qz - qw * qx)
local t4 = -2.0 * (qx * qx + ysqr) + 1.0
--Correct
if t2 > 1.0 then t2 = 1.0 end
if t2 < -1.0 then ts = -1.0 end
--Convert back to X/Y/Z
local xr = math.asin(t2)
local yr = math.atan2(t3, t4)
local zr = math.atan2(t1, t0)
--Return result
return {math.deg(xr),math.deg(yr),math.deg(zr)}
end
function findGlobalPosWithLocalDirection(angle)
local object, distance = self, radialOffset * self.getScale().x
local oPos, oRot = object.getPosition(), object.getRotation()
local posX = oPos.x + math.sin( math.rad(angle+oRot.y) ) * distance
local posY = oPos.y + heightOffset
local posZ = oPos.z + math.cos( math.rad(angle+oRot.y) ) * distance
return {x=posX, y=posY, z=posZ}
end
local function reversedipairsiter(t, i)
i = i - 1
if i ~= 0 then
return i, t[i]
end
end
function reversedipairs(t)
return reversedipairsiter, t, #t + 1
end
function sCopy(orig)
local orig_type = type(orig)
local copy
if(orig_type == 'table') then
copy = {}
for orig_key, orig_value in pairs(orig) do
copy[orig_key] = orig_value
end
else -- number, string, boolean, etc
copy = orig
end
return copy
end
function find_objects_by_name(name)
local objects = {}
for _, obj in ipairs(getAllObjects()) do
if obj.getName() == name then
table.insert(objects, obj)
end
end
return objects
end