Created
June 14, 2018 07:12
-
-
Save jorgen99/33fb189875f89bb3e090af800665a3ad to your computer and use it in GitHub Desktop.
Lua script for use with Agricola in Tabletop Simulator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | |
-- This script assumes that you're playing with three players | |
-- Blue, Yellow and Green. | |
-- | |
-- All the guids are from the save-file that I used | |
-- to the develop this scipt. Some of the guids, like the bags | |
-- are probably the same, but the decks will surely be different | |
-- | |
-- Use this file as inspiration for your own hacking. | |
-- | |
-- Also, this is my first time ever coding in Lua. If you're | |
-- an experienced lua hacker I appologize in advance... :) | |
-- | |
cards_dealt = false | |
round = 0 | |
deal_button_guid = "2d4eb8" | |
next_button_guid = "7eac2c" | |
-- Piles | |
wood_1_guid = "a0c02b" | |
wood_1_position = {} | |
clay_1_guid = "35666b" | |
clay_1_position = {} | |
reed_guid = "4352a8" | |
reed_position = {} | |
fishing_guid = "b967b3" | |
fishing_position = {} | |
wood_2_guid = "4c6fa3" | |
wood_2_position = {} | |
clay_2_guid = "2d0554" | |
clay_2_position = {} | |
-- Bags | |
wood_bag = {} | |
wood_bag_guid = "57458b" | |
clay_bag = {} | |
clay_bag_guid = "8a147c" | |
stone_bag = {} | |
stone_bag_guid = "c64ad7" | |
reed_bag = {} | |
reed_bag_guid = "141313" | |
food_bag = {} | |
food_bag_guid = "f249ab" | |
sheep_bag = {} | |
sheep_bag_guid = "f851b2" | |
boar_bag = {} | |
boar_bag_guid = "e7b33c" | |
cattle_bag = {} | |
cattle_bag_guid = "f0c5e7" | |
grain_bag = {} | |
grain_bag_guid = "61fc77" | |
vegetable_bag = {} | |
vegetable_bag_guid = "e7cb62" | |
-- Occupations & Minor | |
occupation_deck = {} | |
occupation_deck_guid = "1df0b4" | |
minor_deck = {} | |
minor_deck_guid = "33981c" | |
-- Refillable stage cards | |
sheep_card_guid = "291a40" | |
stone_card_1_guid = "31145b" | |
vegetable_card_guid = "771079" | |
boar_card_guid = "9abcb1" | |
stone_card_2_guid = "872124" | |
cattle_card_guid = "d4fb01" | |
-- Stage decks | |
stage_1_deck = {} | |
stage_1_guid = "fced40" | |
stage_2_deck = {} | |
stage_2_guid = "233abd" | |
stage_3_deck = {} | |
stage_3_guid = "0939fc" | |
stage_4_deck = {} | |
stage_4_guid = "c47df5" | |
stage_5_deck = {} | |
stage_5_guid = "a444c2" | |
stage_6_deck = {} | |
stage_6_guid = renvation_2_guid | |
-- Round card positions | |
round_1_position = {7.62, 1.52, 3.79} | |
round_2_position = {10.44, 1.52, 3.79} | |
round_3_position = {10.44, 1.52, 0.03} | |
round_4_position = {10.44, 1.52, -3.61} | |
round_5_position = {12.89, 1.52, 3.79} | |
round_6_position = {12.89, 1.52, 0.03} | |
round_7_position = {12.89, 1.52, -3.61} | |
round_8_position = {17.11, 1.52, 3.22} | |
round_9_position = {17.11, 1.52, -0.67} | |
round_10_position = {19.70, 1.52, 3.22} | |
round_11_position = {19.70, 1.52, -0.67} | |
round_12_position = {22.35, 1.52, 3.22} | |
round_13_position = {22.35, 1.52, -0.67} | |
round_positions = { | |
round_1_position, round_2_position, round_3_position, | |
round_4_position, round_5_position, round_6_position, | |
round_7_position, round_8_position, round_9_position, | |
round_10_position, round_11_position, round_12_position, | |
round_13_position | |
} | |
-- called by TTS when the board is loaded | |
function onload() | |
setup_bags() | |
setup_pile_positions() | |
setup_stages() | |
setup_occupations_and_minor() | |
button_setup("Next Round", next_button_guid, "next_round") | |
button_setup("Deal Cards", deal_button_guid, "deal_cards") | |
end | |
function setup_bags() | |
wood_bag = getObjectFromGUID(wood_bag_guid) | |
clay_bag = getObjectFromGUID(clay_bag_guid) | |
stone_bag = getObjectFromGUID(stone_bag_guid) | |
reed_bag = getObjectFromGUID(reed_bag_guid) | |
food_bag = getObjectFromGUID(food_bag_guid) | |
sheep_bag = getObjectFromGUID(sheep_bag_guid) | |
boar_bag = getObjectFromGUID(boar_bag_guid) | |
cattle_bag = getObjectFromGUID(cattle_bag_guid) | |
grain_bag = getObjectFromGUID(grain_bag_guid) | |
vegetable_bag = getObjectFromGUID(vegetable_bag_guid) | |
end | |
-- We need the positions of the initial resource piles | |
-- so that we can refill them even if someone has taken | |
-- the resources in the previous round | |
function setup_pile_positions() | |
local wood_1 = getObjectFromGUID(wood_1_guid) | |
wood_1_position = wood_1.getPosition() | |
local clay_1 = getObjectFromGUID(clay_1_guid) | |
clay_1_position = clay_1.getPosition() | |
local reed = getObjectFromGUID(reed_guid) | |
reed_position = reed.getPosition() | |
local fishing = getObjectFromGUID(fishing_guid) | |
fishing_position = fishing.getPosition() | |
local wood_2 = getObjectFromGUID(wood_2_guid) | |
wood_2_position = wood_2.getPosition() | |
local clay_2 = getObjectFromGUID(clay_2_guid) | |
clay_2_position = clay_2.getPosition() | |
end | |
function setup_stages() | |
stage_1_deck = getObjectFromGUID(stage_1_guid) | |
stage_1_deck.shuffle() | |
stage_2_deck = getObjectFromGUID(stage_2_guid) | |
stage_2_deck.shuffle() | |
stage_3_deck = getObjectFromGUID(stage_3_guid) | |
stage_3_deck.shuffle() | |
stage_4_deck = getObjectFromGUID(stage_4_guid) | |
stage_4_deck.shuffle() | |
stage_5_deck = getObjectFromGUID(stage_5_guid) | |
stage_5_deck.shuffle() | |
stage_6_deck = getObjectFromGUID(renovation_2_guid) | |
-- A list of all round cards. This is used when | |
-- we deal the last round card in every stage. | |
-- Again, since a deck is not a deck anymore when | |
-- there is only one card left. See deal_last_card_in_stage | |
round_cards = {} | |
join_tables(round_cards, stage_1_deck.getObjects()) | |
join_tables(round_cards, stage_2_deck.getObjects()) | |
join_tables(round_cards, stage_3_deck.getObjects()) | |
join_tables(round_cards, stage_4_deck.getObjects()) | |
join_tables(round_cards, stage_5_deck.getObjects()) | |
end | |
function setup_occupations_and_minor() | |
occupation_deck = getObjectFromGUID(occupation_deck_guid) | |
minor_deck = getObjectFromGUID(minor_deck_guid) | |
end | |
function button_setup(label, guid, function_name) | |
local button = getObjectFromGUID(guid) | |
local button_parameters = {} | |
button_parameters.click_function = function_name | |
button_parameters.label = label | |
button_parameters.function_owner = nil | |
button_parameters.position = { 0, 0.2, 0 } | |
button_parameters.rotation = { 0, 0, 0 } | |
button_parameters.width = 2000 | |
button_parameters.height = 500 | |
button_parameters.font_size = 200 | |
button.createButton(button_parameters) | |
return button | |
end | |
-- This function is called when the "Next Round" | |
-- button is clicked | |
function next_round() | |
round = round + 1 | |
print("Starting round ", round) | |
startLuaCoroutine(Global, "next_round_coroutine") | |
end | |
function next_round_coroutine() | |
flip_round_card() | |
waitFrames(50) | |
refill_on_card(sheep_card_guid, sheep_bag) | |
refill_on_card(stone_card_1_guid, stone_bag) | |
refill_on_card(vegetable_card_guid, vegetable_bag) | |
refill_on_card(boar_card_guid, boar_bag) | |
refill_on_card(stone_card_2_guid, stone_bag) | |
refill_on_card(cattle_card_guid, cattle_bag) | |
if round == 1 then | |
-- We already have resources for round 1 on | |
-- the table, so no refill is needed. | |
return 1 | |
end | |
refill(wood_1_position, wood_bag, 3) | |
refill(clay_1_position, clay_bag, 1) | |
refill(reed_position, reed_bag, 1) | |
refill(fishing_position, food_bag, 1) | |
refill(wood_2_position, wood_bag, 2) | |
refill(clay_2_position, clay_bag, 1) | |
return 1 | |
end | |
function flip_round_card() | |
local params = {} | |
params.position = round_positions[round] | |
params.flip = true | |
if round < 4 then | |
stage_1_deck.takeObject(params) | |
elseif round == 4 then | |
deal_last_card_in_stage() | |
elseif round < 7 then | |
stage_2_deck.takeObject(params) | |
elseif round == 7 then | |
deal_last_card_in_stage() | |
elseif round < 9 then | |
stage_3_deck.takeObject(params) | |
elseif round == 9 then | |
deal_last_card_in_stage() | |
elseif round < 11 then | |
stage_4_deck.takeObject(params) | |
elseif round == 11 then | |
deal_last_card_in_stage() | |
elseif round < 13 then | |
stage_5_deck.takeObject(params) | |
elseif round == 13 then | |
deal_last_card_in_stage() | |
else | |
stage_6_deck.flip() | |
end | |
end | |
-- Since we can't use takeObject on a single | |
-- card, only a deck, we need special treatement | |
-- of the last card in the stage decks | |
function deal_last_card_in_stage() | |
local position = round_positions[round] | |
local round_card = round_cards[round] | |
local card = getObjectFromGUID(round_card.guid) | |
card.flip() | |
card.setPositionSmooth(position) | |
end | |
function refill_on_card(card_guid, bag) | |
local card = getObjectFromGUID(card_guid) | |
if card and card_is_on_the_board(card) then | |
local pos = card.getPosition() | |
refill({pos.x, pos.y + 6, pos.z}, bag, 1) | |
end | |
end | |
function card_is_on_the_board(card) | |
-- If a round card is below this z-value | |
-- then it has not been dealt. This is | |
-- used in refill_on_card where we check | |
-- this value so that we don't put resources | |
-- on round cards that has not been played yet. | |
-- This also can only happen if the card is | |
-- the last one in a stage since it's only | |
-- then it's not part of a deck | |
local z_below_first_stage_board = -3.62 | |
return card.getPosition().z > z_below_first_stage_board | |
end | |
-- This function is called when the "Deal Cards" | |
-- button is clicked | |
function deal_cards() | |
startLuaCoroutine(Global, "deal_coroutine") | |
end | |
function deal_coroutine() | |
print "Dealing Cards to players" | |
if cards_dealt then | |
rotate_cards_between_players() | |
return 1 | |
end | |
waitFrames(50) | |
dealToPlayers(occupation_deck) | |
waitFrames(50) | |
dealToPlayers(minor_deck) | |
cards_dealt = true | |
return 1 | |
end | |
function rotate_cards_between_players() | |
local temp_zone_guid = "43e5c9" | |
local temp_zone = getObjectFromGUID(temp_zone_guid) | |
local green_temp_zone_guid = "539864" | |
local green_temp_zone = getObjectFromGUID(green_temp_zone_guid) | |
print "Move Green cards to temp area" | |
local hand = Player.Green.getHandObjects(1) | |
for i, obj in ipairs(hand) do | |
-- first move to green_temp_zone below the green hand | |
-- to get around bug that makes it impossible to do | |
-- setPositionSmooth from hand to temp_zone directly | |
obj.setPosition(green_temp_zone.getPosition()) | |
-- now we can move to the temp_zone with the card animation | |
obj.setPositionSmooth(temp_zone.getPosition()) | |
waitFrames(5) | |
end | |
waitFrames(100) | |
print "Yellow cards to Green" | |
from_hand_to_hand(Player.Yellow, "Green") | |
print "Move Red cards to Yellow" | |
from_hand_to_hand(Player.Red, "Yellow") | |
print "Move temp cards to Red" | |
local temp_zone_cards = temp_zone.getObjects() | |
for i, temp_zone_card in ipairs(temp_zone_cards) do | |
for j=1,13 do | |
temp_zone_card.deal(1, "Red") | |
waitFrames(5) | |
end | |
end | |
-- last card is not a deck, so it needs special treatement | |
waitFrames(15) | |
temp_zone_cards = temp_zone.getObjects() | |
local final_temp_zone_card = temp_zone_cards[1] | |
final_temp_zone_card.deal(1, "Red") | |
print "Done shifting cards around" | |
end | |
function from_hand_to_hand(fromPlayer, toColor) | |
for i, card in ipairs(fromPlayer.getHandObjects(1)) do | |
card.deal(1, toColor) | |
waitFrames(5) | |
end | |
waitFrames(50) | |
end | |
function dealToPlayers(deck) | |
deck.shuffle() | |
for i = 1,7 do | |
deck.deal(1) | |
waitFrames(15) | |
end | |
end | |
function refill(position, from, quantity) | |
local params = {} | |
params.position = position | |
for i = 1, quantity do | |
from.takeObject(params) | |
waitFrames(15) | |
end | |
end | |
function waitFrames(frames) | |
while frames > 0 do | |
coroutine.yield(0) | |
frames = frames - 1 | |
end | |
end | |
function join_tables(t1, t2) | |
for i=1,#t2 do | |
t1[#t1+1] = t2[i] | |
end | |
return t1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment