Skip to content

Instantly share code, notes, and snippets.

@KyleJamesWalker
Last active May 10, 2020 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KyleJamesWalker/c50a89a1e5ae51f38346da107413fb7a to your computer and use it in GitHub Desktop.
Save KyleJamesWalker/c50a89a1e5ae51f38346da107413fb7a to your computer and use it in GitHub Desktop.
The Crew Tabletop Simulator Script
--[[ Lua code. See documentation: https://api.tabletopsimulator.com/ --]]
#include Console/console++
console.hide_globals('guids')
function onLoad()
startingPiecesBox = getObjectFromGUID("51ae69")
collectionZone = getObjectFromGUID("ffb6e4")
reminderZone = getObjectFromGUID("a07fbf")
-- Player Colors
gameColors = Player.getAvailableColors()
fixedColor = {"White","Red","Yellow","Green","Blue"}
allColor = {"White","Red","Yellow","Green","Blue","Grey"}
console.load()
end
function onUpdate()
console.update()
end
function sortHandClicked(player, value, id)
sortCards(player.color)
end
function randHandClicked(player, value, id)
--Gets cards from owner's hand
local handObjects = player.getHandObjects()
--Establishes the position of each card
local cardPositions = {}
for i, v in pairs(handObjects) do
cardPositions[i] = v.getPosition()
end
--Activates shuffle of the table of cards
local handObjectsShuffled = shuffleTable(handObjects)
--Activates shuffle of the table of card positions
local cardPositionsShuffled = shuffleTable(cardPositions)
--Puts the shuffled cards into the shuffled card positions
for i, v in pairs(handObjectsShuffled) do
v.setPosition(cardPositionsShuffled[i])
end
end
function resetGameClicked(player, value, id)
local player_hand = Player[player.color].getHandTransform()
local spawn_pos = player_hand.position
clearCards()
cloneStartingCards()
moveStartingPieces()
end
function dealGameClicked(player, value, id)
dealPlayerCards()
Wait.time(sortCardsAll, 1)
end
function clearCards()
-- Destroy all cards on play area
local allObjects = getAllObjects()
for _,v in pairs(allObjects) do
if v.tag == "Deck" then
v.destroy()
elseif v.tag == "Card" and v.getLock() == false then
v.destroy()
end
end
return pieces
end
function cloneStartingCards()
-- Place copies of new cards
local playingCardPos = {17.10, 1.84, 0.15}
local playingCardRot = {0.00, 180.00, 180.00}
local reminderCardPos = {17.09, 1.67, -4.39}
local reminderCardRot = {359.92, 270.00, 180.02}
local signalMarkerRot = {0,180,0}
local signalMarkerPos = {32.12, 0.95, -14.59}
for _,piece in pairs(startingPiecesBox.getObjects()) do
if piece.name == "PlayerDeck" then
cloneBoxItem(startingPiecesBox, piece, playingCardPos, playingCardRot)
elseif piece.name == "ReminderDeck" then
cloneBoxItem(startingPiecesBox, piece, reminderCardPos, reminderCardRot)
end
end
end
function cloneBoxItem(box, piece, pos, rot)
local orgItem = box.takeObject(piece)
local newItem = orgItem.clone({
position=pos,
rotation=rot
})
box.putObject(orgItem)
end
function moveStartingPieces()
local allObjects = getAllObjects()
local pieces = {
-- Task Markers
task_p1 = {guid="8fbf4c", pos={38.10, 1.08, -7.85}, rot={0.00, 270.00, 0.00}},
task_p2 = {guid="540e6f", pos={38.10, 1.08, -9.85}, rot={0.00, 270.00, 0.00}},
task_p3 = {guid="325c19", pos={38.10, 1.08, -11.85}, rot={0.00, 270.00, 0.00}},
task_p4 = {guid="8c7619", pos={38.10, 1.08, -13.85}, rot={0.00, 270.00, 0.00}},
task_p5 = {guid="85a50e", pos={38.10, 1.08, -15.85}, rot={0.00, 270.00, 0.00}},
-- Chevron Markers
task_c1 = {guid="cd687b", pos={36.10, 1.08, -7.85}, rot={0.00, 270.00, 0.00}},
task_c2 = {guid="b4849d", pos={36.10, 1.08, -9.85}, rot={0.00, 270.00, 0.00}},
task_c3 = {guid="92cf22", pos={36.10, 1.08, -11.85}, rot={0.00, 270.00, 0.00}},
task_c4 = {guid="7c1c71", pos={36.10, 1.08, -13.85}, rot={0.00, 270.00, 0.00}},
-- Omega Marker
task_o1 = {guid="3926e8", pos={36.10, 1.08, -15.85}, rot={0.00, 270.00, 0.00}},
dstress = {guid="fe43e7", pos={34.10, 1.08, -11.85}, rot={0.00, 270.00, 0.00}},
-- Additional Pieces
command = {guid="c5ccb8", pos={-0.75, 1.54, -0.30}, rot={0.00, 0.00, 0.00}},
shuttle = {guid="1dfc34", pos={1.48, 1.83, -0.76}, rot={0.00, 180.00, 0.00}},
}
for i,v in pairs(pieces) do
local piece = getObjectFromGUID(v.guid)
piece.setPositionSmooth(v.pos)
piece.setRotationSmooth(v.rot)
end
end
function dealPlayerCards()
-- Deal deck evenly to players
local playerDeck = findDeckInZone(collectionZone)
local reminderDeck = findDeckInZone(reminderZone)
local seatedColors = getSeatedPlayers()
local playerCount = #seatedColors
if playerCount < 3 then
broadcastToAll("Error not enough players 3-5 seats required")
return
elseif playerDeck.getQuantity() ~= 40 then
broadcastToAll("Error deck should have 40 for dealing to players")
return
elseif reminderDeck.getQuantity() ~= 36 then
broadcastToAll("Warning reminder deck should have 36 cards")
end
playerDeck.shuffle()
reminderDeck.shuffle()
while(playerDeck.getQuantity() > 0) do
for i,v in pairs(seatedColors) do
playerDeck.deal(1, v)
end
end
-- Deal last remaining card if uneven deal
local playerCard = findDeckInZone(collectionZone)
if playerCard ~= nil then
playerCard.deal(1, seatedColors[math.random(playerCount)])
end
end
function findDeckInZone(zone)
-- Find the first day in a given zone
local objectsInZone = zone.getObjects()
for i, object in ipairs(objectsInZone) do
if object.tag == "Deck" then
return object
end
end
-- In case there is not deck, find the last card
for i, object in ipairs(objectsInZone) do
if object.tag == "Card" then
return object
end
end
return nil
end
function sortCardsAll()
local seatedColors = getSeatedPlayers()
for i,v in pairs(seatedColors) do
sortCards(v)
end
end
function sortCards(playerColor)
-- Sort cards by random suit order
local cards = {}
local handPos = {}
local t = {"A","G","P","Y","Z"}
local o = {1,2}
-- Get player"s hand
local handObjects = Player[playerColor].getHandObjects()
local randSuit = randomize(t)
local randOrder = randomize(o)[1]
if handObjects == nil then
broadcastToColor("Error your hand is empty", playerColor)
return
end
-- One table stores card names, the other position
for j, k in pairs(handObjects) do
table.insert(handPos, k.getPosition())
for x, y in pairs(randSuit) do
if y == k.getName():sub(1, 1) then
table.insert(cards, {card=k, ind=x, dir=randOrder,name=k.getName()})
end
end
end
-- Reorder names while retaining original positions
table.sort(cards, function(a, b)
if a.ind ~= b.ind then
return a.ind > b.ind
end
if a.dir == 1 then
return a.name > b.name
else
return a.name < b.name
end
end)
-- Reset position of re-ordered cards
for i, j in pairs(cards) do
j.card.setPosition(handPos[i])
if j.card.getDescription() ~= "Reminder" then
j.card.setDescription(playerColor)
end
end
end
function randomize(t)
for i = 1, #t*2 do
local a = math.random(#t)
local b = math.random(#t)
t[a],t[b] = t[b],t[a]
end
return t
end
function shuffleTable(t)
for i = #t, 2, -1 do
local n = math.random(i)
t[i], t[n] = t[n], t[i]
end
return t
end
<!-- Xml UI. See documentation: https://api.tabletopsimulator.com/ui/introUI/ -->
<VerticalLayout id="gameButtons" padding="0 0 0 20" rectAlignment="UpperRight" offsetXY="-300 0" height="80" width="100">
<Button id="resetGame" onClick="resetGameClicked" text="Game Reset"></Button>
<Button id="dealGame" onClick="dealGameClicked" text="Deal"></Button>
</VerticalLayout>
<VerticalLayout id="playerButtons" padding="0 0 0 20" rectAlignment="LowerRight" offsetXY="-100 0" height="80" width="120">
<Button id="sortHand" onClick="sortHandClicked" text="Sort Hand"></Button>
<Button id="randHand" onClick="randHandClicked" text="Shuffle Hand"></Button>
</VerticalLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment