diff --git a/init.lua b/init.lua index 7ae55cd..c4070e5 100644 --- a/init.lua +++ b/init.lua @@ -505,6 +505,81 @@ local function inventory_to_cardtable(list,pos,sender,which) return false end +-- return true if pos is deal position +local function is_deal_position(pos) + local around={ + {x=pos.x-1, y=pos.y, z=pos.z-1},{x=pos.x,y=pos.y,z=pos.z-1},{x=pos.x+1,y=pos.y,z=pos.z-1}, + {x=pos.x-1, y=pos.y, z=pos.z}, {x=pos.x+1,y=pos.y,z=pos.z}, + {x=pos.x-1, y=pos.y, z=pos.z+1},{x=pos.x,y=pos.y,z=pos.z+1},{x=pos.x+1,y=pos.y,z=pos.z+1} + } + local tablenodes=0 + for key,p in ipairs(around) do + local n=minetest.get_node(p) + if n.name == "deck:cardtable" then + tablenodes=tablenodes+1 + end + end + return tablenodes==1 +end + +-- return list of positions where cards should be dealt +local function cardtable_get_deal_positions(pos) + local positions=cardtable_get_positions(pos) + local dealpositions={} + for i,p in ipairs(positions) do + if is_deal_position(p) then + table.insert(dealpositions,p) + else + minetest.log(minetest.pos_to_string(p).." is not deal position") + end + end + return dealpositions +end + +-- Deal cards from pile to some other piles on a table +local function deal_cards(pos,formname,fields,sender) + minetest.log("deal cards at "..minetest.pos_to_string(pos)) + local below={x=pos.x,y=pos.y-1,z=pos.z} + local belownode=minetest.get_node(below) + local dp + if belownode.name == "deck:cardtable" then + minetest.log("below is cardtable") + dp=cardtable_get_deal_positions(below) + else + minetest.log("below is not cardtable -> not implemented") + dp=cardtable_get_deal_positions(pos) + end + for i,p in ipairs(dp) do + minetest.log("deal to "..minetest.pos_to_string(p)) + local from_pile_meta=minetest.get_meta(pos) + local from_pile_inv=from_pile_meta:get_inventory() + if from_pile_inv == nil then minetest.log("pile inv=nil") return false end + local stack=pile_pop(from_pile_inv) + if stack == nil then + minetest.log("out of cards") + return false + end + local target_pos={x=p.x,y=p.y+1,z=p.z} + local target_node=minetest.get_node(target_pos) + if target_node.name == "air" then + minetest.place_node(target_pos,{name="deck:chestpile"}) + target_node=minetest.get_node(target_pos) + end + if target_node.name == "deck:chestpile" or target_node.name == "deck:stockpile" then + local target_pile_meta=minetest.get_meta(target_pos) + local target_pile_inv=target_pile_meta:get_inventory() + if pile_peek_first(target_pile_inv) == nil then + lighten_pile(target_pos) + target_pile_meta=minetest.get_meta(target_pos) + target_pile_inv=target_pile_meta:get_inventory() + end + pile_add_card(target_pile_inv,stack:get_name()) + else + minetest.log("not pile or air but "..target_node.name.." at "..minetest.pos_to_string(target_pos)) + end + end +end + -- Process form button presses of cardtable local function fields_cardtable(pos,formname,fields,sender) if fields.cardtabletomain then @@ -541,6 +616,8 @@ local function draw_card(pos,formname,fields,sender) convert_pile(pos,"deck:stockpile") elseif fields.alltopile then put_all_to_pile(pos,formname,fields,sender) + elseif fields.deal then + deal_cards(pos,formname,fields,sender) end end @@ -736,6 +813,7 @@ minetest.register_node("deck:stockpile", { "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]".. "list[current_player;main;0,1;13,5;]" ) end,