Dealing cards on selected table color

Turn "Deal to table" button to drop down menu of colors if
table has multiple colors.
This commit is contained in:
Mikko Tuumanen 2023-10-29 00:07:29 +03:00
parent 5295e23e74
commit 537a198ef4

View File

@ -64,6 +64,9 @@ local cardnames = {}
-- Prefix of all individual card node names. -- Prefix of all individual card node names.
local cardprefix="deck:card_" local cardprefix="deck:card_"
local cardprefixlen=string.len(cardprefix) local cardprefixlen=string.len(cardprefix)
-- Prefix of cardtable types
local tableprefix="deck:cardtable_"
local tableprefixlen=string.len(tableprefix)
-- Table of card descriptions. Key is minetest node name, value is -- Table of card descriptions. Key is minetest node name, value is
-- verbose card name, for example deck:card_ah -> Ace of hearts -- verbose card name, for example deck:card_ah -> Ace of hearts
@ -524,7 +527,7 @@ local function cardtable_try_add_card(pos,cardname)
end end
-- Adds card to cardtable. Return true if card was added or false if cardtable was full. -- Adds card to cardtable. Return true if card was added or false if cardtable was full.
local function cardtable_add_card(pos,cardname) local function cardtable_add_card(pos,cardname,color)
local above={x=pos.x,y=pos.y+1,z=pos.z} local above={x=pos.x,y=pos.y+1,z=pos.z}
if cardtable_try_add_card(above,cardname) then return true end if cardtable_try_add_card(above,cardname) then return true end
local positions=cardtable_get_positions(pos) local positions=cardtable_get_positions(pos)
@ -586,9 +589,8 @@ local function inventory_to_cardtable(list,pos,sender,which)
end end
local function is_cardtable(pos) local function is_cardtable(pos)
local prefix="deck:cardtable_"
local n=minetest.get_node(pos) local n=minetest.get_node(pos)
if string.sub(n.name,1,string.len(prefix)) == prefix then if string.sub(n.name,1,string.len(tableprefix)) == tableprefix then
return true return true
end end
return false return false
@ -638,7 +640,7 @@ local function deal_card_to_table(pos,formname,fields,sender)
log("out of cards") log("out of cards")
return false return false
end end
if cardtable_add_card(below,stack:get_name()) then if cardtable_add_card(below,stack:get_name(),fields.deal_card_to_table) then
return true return true
else else
log("Card did not fit on table.") log("Card did not fit on table.")
@ -887,6 +889,23 @@ local function after_dig_cardtable(pos, oldnode, oldmeta, digger)
end end
end end
-- return array of strings that contain colors of cardtable at pos. Each color is returned only once.
local function cardtable_get_colors(pos)
local tablepositions=cardtable_get_positions(pos)
local colors={}
local colortable={}
local offset=tableprefixlen+1
for i,p in ipairs(tablepositions) do
local n=minetest.get_node(p)
local color=string.sub(n.name,offset)
if colortable[color] == nil then
table.insert(colors,color)
colortable[color]=1
end
end
return colors
end
-- Chestpile is a pile of cards that player can use like a chest. -- Chestpile is a pile of cards that player can use like a chest.
minetest.register_node("deck:chestpile", { minetest.register_node("deck:chestpile", {
description = "Card pile that user has full access to. Works also like a minetest chest.", description = "Card pile that user has full access to. Works also like a minetest chest.",
@ -926,13 +945,25 @@ minetest.register_node("deck:stockpile", {
on_construct = function(pos) on_construct = function(pos)
pile_inv_setup(pos) pile_inv_setup(pos)
local meta=minetest.get_meta(pos) local meta=minetest.get_meta(pos)
local below={x=pos.x,y=pos.y-1,z=pos.z}
local colors=cardtable_get_colors(below)
local dealmenu
if table.getn(colors) == 1 then
dealmenu="button[10,0;2,1;deal_to_table;Deal to table]"
else
dealmenu="dropdown[10,0;2;deal_to_table;"
for key,value in ipairs(colors) do
dealmenu=dealmenu..value..","
end
dealmenu=dealmenu..";1]"
end
meta:set_string("formspec","size[13,6]".. meta:set_string("formspec","size[13,6]"..
"button[0,0;2,1;draw;Draw]".. "button[0,0;2,1;draw;Draw]"..
"button[2,0;2,1;drawall;Draw all]".. "button[2,0;2,1;drawall;Draw all]"..
"button[4,0;2,1;alltopile;Add all to pile]".. "button[4,0;2,1;alltopile;Add all to pile]"..
"button[6,0;2,1;shuffle;Shuffle cards]".. "button[6,0;2,1;shuffle;Shuffle cards]"..
"button[8,0;2,1;deal;Deal cards]".. "button[8,0;2,1;deal;Deal cards]"..
"button[10,0;2,1;deal_to_table;Deal to table]".. dealmenu..
"list[current_player;main;0,1;13,5;]" "list[current_player;main;0,1;13,5;]"
) )
end, end,
@ -947,7 +978,7 @@ minetest.register_node("deck:stockpile", {
log(table.getn(dye.dyes).." cardtable colors") log(table.getn(dye.dyes).." cardtable colors")
for _, row in ipairs(dye.dyes) do for _, row in ipairs(dye.dyes) do
local color = row[1] local color = row[1]
local name = "deck:cardtable_"..color local name = tableprefix..color
local colorize = tablecolorize[color] local colorize = tablecolorize[color]
if colorize == nil then colorize = color end if colorize == nil then colorize = color end
local tile = "deck_table_g.png^[colorize:"..colorize..":127" local tile = "deck_table_g.png^[colorize:"..colorize..":127"
@ -1014,13 +1045,13 @@ minetest.register_craft({
-- card table pieces can be crafted from 2x2 grid of wood -- card table pieces can be crafted from 2x2 grid of wood
minetest.register_craft({ minetest.register_craft({
output = "deck:cardtable_white 99", output = tableprefix.."white 99",
recipe = {{ "group:wood", "group:wood"},{"group:wood","group:wood"}} recipe = {{ "group:wood", "group:wood"},{"group:wood","group:wood"}}
}) })
for _, row in ipairs(dye.dyes) do for _, row in ipairs(dye.dyes) do
local color = row[1] local color = row[1]
local name = "deck:cardtable_"..color local name = tableprefix..color
local outputstring = name.." 99" local outputstring = name.." 99"
local dyetype = "group:dye,color_white" local dyetype = "group:dye,color_white"
log("registering craft for '"..outputstring.."' and dyetype "..dyetype) log("registering craft for '"..outputstring.."' and dyetype "..dyetype)