-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDraft.ttslua
651 lines (578 loc) · 19.1 KB
/
Draft.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
--[[
SH Expansion Draft Tool
Made by Sionar, with code borrowed from Lost Savage and Markimus
--]]
------------------Constants
VERSION = '1.2.6'
COLOR_ORDER = {'White', 'Brown', 'Red', 'Orange', 'Yellow', 'Green', 'Teal', 'Blue', 'Purple', 'Pink'}
COLOR_RGB = {White = '[FFFFFF]', Brown = '[703A16]', Red = '[DA1917]', Orange = '[F3631C]', Yellow = '[E6E42B]', Green = '[30B22A]', Teal = '[20B09A]', Blue = '[1E87FF]', Purple = '[9F1FEF]', Pink = '[F46FCD]'}
ABILITY_ZONE_GUID = 'eea120'
POS = {
{-11, 5, 15},
{-6, 5, 15},
{-1, 5, 15},
{4, 5, 15}
}
NO_ROT = {x = 0, y = 0, z = 0}
FACE_UP_ROT = {x = 0, y = 180, z = 0}
FACE_DOWN_ROT = {x = 0, y = 180, z = 180}
SETUP_BOARD_GUID = '39d283'
------------------Variables
deck = {nil, nil, nil, nil}
deckNum = {nil, nil, nil, nil}
deckDir = {'cw', 'ccw', 'cw', 'ccw'}
deckGUID = {nil, nil, nil, nil}
players = {nil, nil, nil, nil}
numPlayers = 10
round = 1
shufToggle = true
seatedTable = {}
draftStarted = false
syncDraft = true
doneDecks = 0
------------------Load Save
function onLoad(saveString)
if not (saveString == '') then
local save = JSON.decode(saveString)
players = save['p'] or {nil, nil, nil, nil}
numPlayers = save['n'] or 10
seatedTable = save['st'] or {}
round = save['r'] or 1
draftStarted = save['ds'] or false
shufToggle = save['ts'] or true
deckGUID = save['g'] or {nil, nil, nil, nil}
syncDraft = save['sd'] or false
doneDecks = save['dd'] or 0
end
for i = 1,4 do
deck[i] = getObjectFromGUID(deckGUID[i])
end
refreshButtons()
local global_name = Global.getVar('MOD_NAME')
if global_name == 'Secret Hitler: CE' then
self.setLock(true)
end
Wait.time(checkGameStarted, 2, -1)
checkDraftStarted()
end
function onSave()
local save = {}
save['p'] = players
save['n'] = numPlayers
save['st'] = seatedTable
save['r'] = round
save['ds'] = draftStarted
save['ts'] = shufToggle
save['g'] = deckGUID
save['sd'] = syncDraft
save['dd'] = doneDecks
local saveString = JSON.encode(save)
return saveString
end
function saveDeckGUID()
for i = 1,4 do
deckGUID[i] = deck[i].getGUID()
end
end
------------------Event
function onObjectLeaveContainer(container, leave_object)
local pos
local fwd, rgt
local deckIndex, otherIndex
local started = Global.getVar('started')
if not string.match(container.getName(), 'Draft Deck') then
return
end
if leave_object.getName() == 'Draft' and draftStarted then
container.setLock(false)
pos = container.getPosition()
pos['y'] = pos['y'] + 0.1
Wait.time(function() leave_object.setPosition(pos) end, 3)
Wait.time(function() container.setLock(true) end, 4)
Wait.time(resetDeckPos, 4.01)
return
end
if not syncDraft and draftStarted and container.getQuantity() ~= 2 then
deckIndex = getDeckByGUID(container.getGUID())
otherIndex = (deckIndex + 1)%4 + 1 --deck that's going the same direction
players[deckIndex] = getNextPlayer(players[deckIndex], deckDir[deckIndex])
if players[deckIndex] == players[otherIndex] then
if deckDir[deckIndex] == 'cw' then
rgt = 5
else
rgt = -5
end
else
rgt = 0
end
if deckIndex%2 == 1 then
fwd = 11.5
else
fwd = 16.5
end
giveObjectToPlayer(container, players[deckIndex], {forward = fwd, right = rgt, up = 0, forceHeight = 2.2}, FACE_DOWN_ROT)
if container.getQuantity() == 3 then
local deckTable = container.getObjects()
for j = 1,3 do
if deckTable[j].name ~= 'Draft' then
local card = container.takeObject({index = j-1})
giveObjectToPlayer(card, players[deckIndex], {forward = 0, right = 0, up = 0, forceHeight = 8}, FACE_DOWN_ROT)
break
end
end
container.destruct()
doneDecks = doneDecks + 1
if doneDecks == 4 then
self.destruct()
end
end
end
end
function onPlayerChangeColor(player_color)
if draftStarted then
Wait.time(function() Global.call('deleteCustomBoardCards') end, 2)
end
end
function resetDeckPos()
for i = 1, 4 do
if i%2 == 1 then
fwd = 11.5
else
fwd = 16.5
end
giveObjectToPlayer(deck[i], players[i], {forward = fwd, right = 0, up = 0, forceHeight = 2.2}, FACE_DOWN_ROT)
end
end
------------------Main Functions
function setupStart(clickedObject, playerColor)
if Player[playerColor].admin then
if #getSeatedPlayers() < 2 then
Player[playerColor].broadcast('Error: You need more than one player seated to start drafting.', {1,0,0})
return
end
Global.call('deleteCustomBoardCards')
local options = Global.getTable('options')
options.shufflePlayers = false
options.expansionAmount = 0
Global.setTable('options', options)
Global.call('settingsPannelMakeButtons')
seatedTable = getSeatedPlayers()
if shufToggle then
startLuaCoroutine(self, 'shufflePlayersCoroutine')
end
local setupBoard = getObjectFromGUID(SETUP_BOARD_GUID)
setupBoard.scale(0.25)
dealAbilityDecks()
end
end
function shufflePlayersCoroutine()
shufflePlayers()
return 1
end
function findUnusedColor()
local checkList = {'Brown', 'Teal', 'Black', 'White', 'Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Purple', 'Pink'}
for _, playerColor in ipairs(checkList) do
if not Player[playerColor].seated then
return playerColor
end
end
end
function shufflePlayers()
local blackSteamId
local options = Global.getTable('options')
if #getSeatedPlayers() == 10 and Player['Black'].seated then
blackSteamId = Player['Black'].steam_id
Player['Black']:changeColor('Grey')
while Player['Black'].seated do
coroutine.yield()
end
end
swapColor = findUnusedColor()
local ranColors = {}
for _, v in pairs(getSeatedPlayers()) do
if (not Player[v].host) then
table.insert(ranColors, 1, v)
end
end
shuffleTable(ranColors)
seatedPlayers = {}
local j = 1
for _, v in pairs(getSeatedPlayers()) do
if (not Player[v].host) or options.shuffleHost then
local playerInfo = {}
playerInfo.target = ranColors[j]
playerInfo.myColor = v
table.insert(seatedPlayers, 1, playerInfo)
j = j + 1
end
end
local doneCount = 0
local tryCount = #seatedPlayers
while doneCount ~= #seatedPlayers and tryCount > 0 do
doneCount = 0
for i, v in pairs(seatedPlayers) do
if v.target ~= v.myColor then
if Player[v.target].seated == false then
local myC = v.myColor
if Player[myC].seated == true then
Player[myC]:changeColor(v.target)
while Player[myC].seated and not Player[v.target].seated do
coroutine.yield()
end
v.myColor = v.target
doneCount = doneCount + 1
end
elseif Player[swapColor].seated == false then
local myC = v.myColor
if Player[myC].seated == true then
Player[myC]:changeColor(swapColor)
while Player[myC].seated and not Player[swapColor].seated do
coroutine.yield()
end
v.myColor = swapColor
end
end
else
doneCount = doneCount + 1
end
end
tryCount = tryCount - 1
coroutine.yield()
end
if blackSteamId then
for _, p in pairs(Player.getSpectators()) do
if p.steam_id == blackSteamId then
p:changeColor('Black')
end
end
end
end
function getNextPlayer(currPlayer, order)
local index
local found = false
for i = 1, #COLOR_ORDER do
if currPlayer == COLOR_ORDER[i] then
index = i
break
end
end
while not found do
if order == 'cw' then
index = index + 1
if index > 10 then
index = 1
end
else
index = index - 1
if index < 1 then
index = 10
end
end
if inTable(COLOR_ORDER[index], seatedTable) then
found = true
end
end
return COLOR_ORDER[index]
end
function dealAbilityDecks()
startLuaCoroutine(self, 'dealCoroutine')
end
function dealCoroutine()
local params = {}
params[1] = {position = POS[1], rotation = FACE_DOWN_ROT}
params[2] = {position = POS[2], rotation = FACE_DOWN_ROT}
params[3] = {position = POS[3], rotation = FACE_DOWN_ROT}
params[4] = {position = POS[4], rotation = FACE_DOWN_ROT}
local objParam = {
type = "DeckCustom",
position = {0,0,0},
rotation = {0, 0, 0},
scale = {1.5, 1, 1.5},
sound = false
}
local customParam = {
type = "DeckCustom",
face = "http://cloud-3.steamusercontent.com/ugc/966483288120157589/563FBEE10DC4852BA9E51826FF254049E2320542/",
back = "http://cloud-3.steamusercontent.com/ugc/966483288120157589/563FBEE10DC4852BA9E51826FF254049E2320542/",
unique_back = true,
width = 2,
height = 3,
number = 6,
back_is_hidden = true
}
sleep(2)
local draftDeck = spawnObject(objParam)
local card
draftDeck.setCustomObject(customParam)
for i = 1,4 do
card = draftDeck.takeObject(params[i])
card.setName('Draft')
end
draftDeck.destruct()
sleep(2)
local abilityDeck = getDeckFromZoneByGUID(ABILITY_ZONE_GUID)
abilityDeck.shuffle()
numPlayers = #seatedTable
for i = 1, numPlayers do
for j = 1,4 do
card = abilityDeck.takeObject(params[j])
end
sleep(0.2)
end
sleep(0.1)
draftDeck = spawnObject(objParam)
draftDeck.setCustomObject(customParam)
for i = 1,4 do
card = draftDeck.takeObject(params[i])
card.setName('Draft')
end
draftDeck.destruct()
sleep(3)
local allObjects = getAllObjects()
local objPosition
for k,v in pairs(allObjects) do
objPosition = v.getPosition()
if objPosition.x > POS[1][1] - 0.01 and objPosition.x < POS[1][1] + 0.01 and objPosition.z > POS[1][3] - 0.01 and objPosition.z < POS[1][3] + 0.01 then
deck[1] = v
deck[1].setLock(true)
deck[1].setName('Draft Deck 1')
elseif objPosition.x > POS[2][1] - 0.01 and objPosition.x < POS[2][1] + 0.01 and objPosition.z > POS[2][3] - 0.01 and objPosition.z < POS[2][3] + 0.01 then
deck[2] = v
deck[2].setLock(true)
deck[2].setName('Draft Deck 2')
elseif objPosition.x > POS[3][1] - 0.01 and objPosition.x < POS[3][1] + 0.01 and objPosition.z > POS[3][3] - 0.01 and objPosition.z < POS[3][3] + 0.01 then
deck[3] = v
deck[3].setLock(true)
deck[3].setName('Draft Deck 3')
elseif objPosition.x > POS[4][1] - 0.01 and objPosition.x < POS[4][1] + 0.01 and objPosition.z > POS[4][3] - 0.01 and objPosition.z < POS[4][3] + 0.01 then
deck[4] = v
deck[4].setLock(true)
deck[4].setName('Draft Deck 4')
end
end
sleep(0.01)
players[1] = getRandomPlayer()
players[3] = players[1]
for i = 1, numPlayers/2 do
players[3] = getNextPlayer(players[3], 'cw')
end
players[2] = getNextPlayer(players[1], 'ccw')
players[4] = getNextPlayer(players[3], 'ccw')
local fwd
for i = 1, 4 do
if i%2 == 1 then
fwd = 11.5
else
fwd = 16.5
end
giveObjectToPlayer(deck[i], players[i], {forward = fwd, right = 0, up = 0, forceHeight = 2.2}, FACE_DOWN_ROT)
end
saveDeckGUID()
draftStarted = true
local broadcastString = 'First round picks go to: '
for i = 1,4 do
broadcastString = broadcastString .. COLOR_RGB[players[i]] .. players[i] .. ' '
end
broadcastToAll(broadcastString, {1,1,1})
if syncDraft then
broadcastToAll('Round ' .. round .. ' has started.', {1,1,1})
Wait.time(checkSync, 2, -1)
end
return 1
end
function checkSync()
local deckTable
local card
for i = 1, 4 do
deckNum[i] = deck[i].getQuantity()
end
if deckNum[1] == numPlayers - round + 2 and deckNum[2] == deckNum[1] and deckNum[3] == deckNum[1] and deckNum[4] == deckNum[1] then
round = round + 1
broadcastToAll('Round ' .. round .. ' has started.', {1,1,1})
players[1] = getNextPlayer(players[1], 'cw')
players[2] = getNextPlayer(players[2], 'ccw')
players[3] = getNextPlayer(players[3], 'cw')
players[4] = getNextPlayer(players[4], 'ccw')
for i = 1, 4 do
if i%2 == 1 then
fwd = 11.5
else
fwd = 16.5
end
giveObjectToPlayer(deck[i], players[i], {forward = fwd, right = 0, up = 0, forceHeight = 2.2}, FACE_DOWN_ROT)
end
if round == numPlayers then
for i = 1,4 do
deckTable = deck[i].getObjects()
for j = 1,3 do
if deckTable[j].name ~= 'Draft' then
card = deck[i].takeObject({index = j-1})
giveObjectToPlayer(card, players[i], {forward = 0, right = 0, up = 0, forceHeight = 2.2}, FACE_DOWN_ROT)
break
end
end
end
for i = 1,4 do
deck[i].destruct()
end
broadcastToAll('Drafting finished.', {1,1,1})
self.destruct()
end
end
end
function checkDraftStarted()
if draftStarted then
Wait.time(function() Global.call('deleteCustomBoardCards') end, 1)
Wait.time(checkSync, 2, -1)
end
end
function getDeckByGUID(guid)
for i = 1,4 do
if guid == deckGUID[i] then
return i
end
end
return nil
end
------------------Utility Functions
function shuffleTable(objects)
for i = 1, #objects * 5 do
local a = math.random(#objects)
local b = math.random(#objects)
objects[a], objects[b] = objects[b], objects[a]
end
end
function getDeckFromZoneByGUID(guidIn)
local deck = nil
local deck_ct = 0
local zone = getObjectFromGUID(guidIn)
local object
if zone then
local inZone = zone.getObjects()
for _, object in ipairs(inZone) do
if object.name == 'Card' then
deck_ct = 2
elseif object.name == 'Deck' then
deck = object
deck_ct = deck_ct + 1
elseif object.name == 'DeckCustom' then
deck = object
deck_ct = deck_ct + 1
end
end
end
if deck_ct == 1 then
return deck
end
return nil
end
function sleep(numSeconds)
local t0 = os.clock()
while os.clock() - t0 <= numSeconds do
coroutine.yield(0)
end
end
function getPlayerPosRotVectors(playerColor)
local returnVal = {}
local ph = Player[playerColor].getPlayerHand()
if ph then
returnVal.pos = {x = ph["pos_x"], y = ph["pos_y"], z = ph["pos_z"]}
returnVal.rot = {x = ph["rot_x"], y = ph["rot_y"], z = ph["rot_z"]}
returnVal.vForward = {x = ph["trigger_forward_x"], y = ph["trigger_forward_y"], z = ph["trigger_forward_z"]}
returnVal.vRight = {x = ph["trigger_right_x"], y = ph["trigger_right_y"], z = ph["trigger_right_z"]}
returnVal.vUp = {x = ph["trigger_up_x"], y = ph["trigger_up_y"], z = ph["trigger_up_z"]}
end
return returnVal
end
function giveObjectToPlayer(object, playerColor, posAdd, rotAdd, ...)
local info = getPlayerPosRotVectors(playerColor)
if info then
if rotAdd["exactRot"] then
object.setRotationSmooth({rotAdd["x"], rotAdd["y"], rotAdd["z"]}, ...)
else
object.setRotationSmooth({info.rot["x"] + rotAdd["x"], info.rot["y"] + rotAdd["y"], info.rot["z"] + rotAdd["z"]}, ...)
end
if posAdd["forceHeight"] then
object.setPositionSmooth({info.pos["x"] + info.vForward["x"] * posAdd["forward"] + info.vRight["x"] * posAdd["right"] + info.vUp["x"] * posAdd["up"],
posAdd["forceHeight"],
info.pos["z"] + info.vForward["z"] * posAdd["forward"] + info.vRight["z"] * posAdd["right"] + info.vUp["z"] * posAdd["up"]}, ...)
else
object.setPositionSmooth({info.pos["x"] + info.vForward["x"] * posAdd["forward"] + info.vRight["x"] * posAdd["right"] + info.vUp["x"] * posAdd["up"],
info.pos["y"] + info.vForward["y"] * posAdd["forward"] + info.vRight["y"] * posAdd["right"] + info.vUp["y"] * posAdd["up"],
info.pos["z"] + info.vForward["z"] * posAdd["forward"] + info.vRight["z"] * posAdd["right"] + info.vUp["z"] * posAdd["up"]}, ...)
end
end
end
function getRandomPlayer()
local targs = {}
local randNum, temp, colorString
for i = 1, 10 do
if inTable(COLOR_ORDER[i], seatedTable) then
table.insert(targs, COLOR_ORDER[i])
end
end
for i = #targs, 1, -1 do
randNum = math.random(1, i)
temp = targs[randNum]
targs[randNum] = targs[i]
targs[i] = temp
end
return targs[1]
end
function toggleShuffle(clickedButton, playerColor)
if Player[playerColor].admin then
shufToggle = not shufToggle
refreshButtons()
end
end
function toggleSync(clickedButton, playerColor)
if Player[playerColor].admin then
syncDraft = not syncDraft
refreshButtons()
end
end
function checkGameStarted()
local started = Global.getVar('started')
if started then
self.destruct()
end
end
function inTable(str, table)
local i
for i = 1, #table do
if str == table[i] then
return true
end
end
return false
end
------------------User Interface
function refreshButtons()
self.clearButtons()
local buttonParam = {click_function = 'nullFunc', label = 'Shuffle Players', color = {1,1,1}, function_owner = self,
position = {-0.3,0.2,-0.9}, rotation = {0,0,0}, width = 0, height = 0, font_size = 100, font_color = {1,1,1}}
self.createButton(buttonParam)
buttonParam = {click_function = 'toggleShuffle', label = 'X', color = {1,1,1}, function_owner = self,
position = {0.8,0.2,-0.9}, rotation = {0,0,0}, width = 200, height = 200, font_size = 100, tooltip = 'Shuffle ON'}
if not shufToggle then
buttonParam.tooltip = 'Shuffle OFF'
buttonParam.label = ''
end
self.createButton(buttonParam)
local buttonParam = {click_function = 'nullFunc', label = 'Synchronous Draft', color = {1,1,1}, function_owner = self,
position = {-0.3,0.2,-0.5}, rotation = {0,0,0}, width = 0, height = 0, font_size = 100, font_color = {1,1,1}}
self.createButton(buttonParam)
buttonParam = {click_function = 'toggleSync', label = 'X', color = {1,1,1}, function_owner = self,
position = {0.8,0.2,-0.5}, rotation = {0,0,0}, width = 200, height = 200, font_size = 100, tooltip = 'Synchronous Draft'}
if not syncDraft then
buttonParam.tooltip = 'Asynchronous Draft'
buttonParam.label = ''
end
self.createButton(buttonParam)
buttonParam = {click_function = 'setupStart', label = 'Start\nDraft', color = {1,1,1}, function_owner = self,
position = {0,0.2,0.4}, rotation = {0,0,0}, width = 1000, height = 500, font_size = 100}
self.createButton(buttonParam)
self.setDescription('v ' .. VERSION .. '\nMade by Sionar')
end
function nullFunc()
end