Tutorials

A couple of tutorials to get you started. Further information about functions and data-structures can be found on the API page.

Tutorial 1: Markus

The first tutorial is about making a vary basic card game called "Markus": Each turn the player draws a card. If the card is Jack of spades, the player is Markus. That means he has lost. I think.

The first thing you need to know about is actions. Actions control what the player can do. In the current version of Tones of Tales, adding an action to a game means there will be a menu, either on a card or a deck (or stack, or overlay, but more about that later). The specific action we need is "pick_card". There is also "play_card", as we will see in tutorial 2.

Actions are added to the game with command add_action. They can be removed with remove_action.

Some other common commands is shuffle, set_values, game_over and end_turn.

The full code listing of the game is below. Also feel free to check out the video.

init_script:
action1 = {
	action_id = 1,
	action_name = "pick_card"
	players = players,	-- Global variable "players" includes all players
	target = "deck",
	target_ids = {1},	-- Deck with id 1
	menu_text = "Pick card"
}

function init()
	shuffle(deck1)		-- Shuffle deck 1
	set_values(deck1)	-- Set values and suits to cards in standard deck

	table.insert(table_slots, deck1)	-- Place deck at first table_slot
	update_table()				-- Show changes

	add_action(action1)	-- Add "pick_card" action
end
onpickup_all:
function onpickup_all(player, deck)
	local card = table.remove(deck.cards, 1)	
	card.facing = "up"		-- Change facing to "up"
	player.slots[1] = card		-- Place card on players first slot
	update_player_slots(player)	-- Show changes

	if (card.value == 11 and card.suit == "spades") then
		chat(player.name .. " is Markus!")		-- Chat message
		game_over()					-- End the game
	end

	end_turn()
end
	

Video of tutorial 1

Tutorial 2: Casino

In this tutorial I will demonstrate the new action "play_card" in a prototype version of Casino. Casino is a fairly simple game where you pick up cards laying on the table with cards on your hand. E.g., if you play a king, you will pick up kings on the table and cards summing up to 13 (8 and 5, 7 and 6, etc). You then calculate points depending how many cards you picked up, how many aces, spades, and so on.

Let's begin with the init_script Lua chunk.

First thing to do is the action definition. The target of "play_card" will in our case be "hand", since the player will play cards from his/hers hand (it's also possible to play cards from your player slots).

action1 = {
	action_id = 1,
	action_name = "play_card",
	target = "hand",
	target_ids = {1},
	menu_text = "Play card",
	players = players
}

We add the action to the game with function add_action, as seen in the last tutorial.

As in the last tutorial, we shuffle and set values for the deck, then place it on the table.

shuffle(deck1)
set_values(deck1)

table_slots[1] = deck1
update_table()

Next thing we want to do is deal cards. Each player will have four cards on their hand at start, and also there will be four cards on the table. We create a small help function to draw a card from the deck:

function draw_card()
	return table.remove(deck1.cards, 1)
end

We make a deal function where we iterate four times, each time draw a card and adding it to a players hand.

function deal(p)
	for i = 1, 4 do
		table.insert(p.hand, draw_card())
	end

	update_hand(p)
end

Then, in our init function we simply iterate for all players online and deal for them.

for _, p in ipairs(players) do
	deal(p)
end

In Casino there will lay four cards on the table at start. We iterate again four times, and don't forget to change the card facing to "up" (default is "down") so value and suit will be shown.

for i = 1, 4 do
	local card = draw_card()
	card.facing = "up"
	table.insert(table_slots, card)
end

That's it for initialization. Let's move on to the onplay_all chunk.

I want to show a small "animation", rotating the card 45 degrees when it's played on the table. This is done using the position field of the card, like below.

remove_card_from_hand(player, card)
card.facing = "up"
card.position.rotate = 45
table.insert(table_slots, card)
update_table()
sleep()
card.position.rotate = 0

Since this just a prototype, I will make a very simple algorithm for picking up cards from table: you pick up all cards that equals to the value of the card you played. To iterate through all the cards on the table, we first need to remove the card we just laid there.

table.remove(table_slots, #table_slots)

Since we might want to put this card on table again (if we didn't pick up any cards), we create a boolean to check later.

local picked_card = false

Almost done! We loop through table_slots and pick up a card if the table slot contains a card and the value is equal to the one we played. After, we update the table, the players hand and end our turn.

for k, c in ipairs(table_slots) do
	if (c.__type == "card" and c.value == card.value) then
		table.remove(table_slots, k)
		picked_card = true
		break;
	end 
end

if not picked_card then
	table.insert(table_slots, card)
end

update_table()
update_hand(player)

end_turn()

Video of tutorial 2