diff --git a/init.lua b/init.lua index 5c3c72e..e8edeaf 100644 --- a/init.lua +++ b/init.lua @@ -64,6 +64,9 @@ local cardnames = {} -- Prefix of all individual card node names. local cardprefix="deck:card_" 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 -- verbose card name, for example deck:card_ah -> Ace of hearts @@ -524,7 +527,7 @@ local function cardtable_try_add_card(pos,cardname) end -- 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} if cardtable_try_add_card(above,cardname) then return true end local positions=cardtable_get_positions(pos) @@ -586,9 +589,8 @@ local function inventory_to_cardtable(list,pos,sender,which) end local function is_cardtable(pos) - local prefix="deck:cardtable_" 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 end return false @@ -638,7 +640,7 @@ local function deal_card_to_table(pos,formname,fields,sender) log("out of cards") return false 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 else log("Card did not fit on table.") @@ -887,6 +889,23 @@ local function after_dig_cardtable(pos, oldnode, oldmeta, digger) 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. minetest.register_node("deck:chestpile", { 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) pile_inv_setup(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]".. "button[0,0;2,1;draw;Draw]".. "button[2,0;2,1;drawall;Draw all]".. "button[4,0;2,1;alltopile;Add all to pile]".. "button[6,0;2,1;shuffle;Shuffle 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;]" ) end, @@ -947,7 +978,7 @@ minetest.register_node("deck:stockpile", { log(table.getn(dye.dyes).." cardtable colors") for _, row in ipairs(dye.dyes) do local color = row[1] - local name = "deck:cardtable_"..color + local name = tableprefix..color local colorize = tablecolorize[color] if colorize == nil then colorize = color end 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 minetest.register_craft({ - output = "deck:cardtable_white 99", + output = tableprefix.."white 99", recipe = {{ "group:wood", "group:wood"},{"group:wood","group:wood"}} }) for _, row in ipairs(dye.dyes) do local color = row[1] - local name = "deck:cardtable_"..color + local name = tableprefix..color local outputstring = name.." 99" local dyetype = "group:dye,color_white" log("registering craft for '"..outputstring.."' and dyetype "..dyetype)