deck-cd2025/init.lua

241 lines
6.7 KiB
Lua
Raw Normal View History

local cardgroups = { crumbly=1, falling_node=1, dig_immediate=3, card=1 }
2020-05-09 21:41:18 +03:00
local pilegroups = { crumbly=1 }
2020-05-03 00:30:37 +03:00
local descriptions = {"Three of diamonds","Seven of diamonds","Ace of diamonds" }
--, "Ace of clubs","Ace of hearts","Ace of spades" }
local textures = { "3d","7d","ad" }
--, "ac", "ah", "as" }
local cardnames = {}
local cardprefix="deck:card_"
local cardprefixlen=string.len(cardprefix)
local function pile_add_card(inv,cardname)
local s=inv:get_size("main")
minetest.log("inv size="..s)
local place=s
for i=1,s,1 do
local stack=inv:get_stack("main",i)
if stack:get_count() > 0 then
place=i-1
break
end
end
if place > 0 then
inv:set_stack("main",place,cardname)
minetest.log("deck_add_card done")
return true
end
return false
end
local function pile_add_card_from_pos(inv,pos)
local cardname=ItemStack(minetest.get_node(pos).name)
return pile_add_card(inv,cardname)
end
local function flip_card(pos,formname,fields,sender)
local n=minetest.get_node(pos)
local pile
if fields.to_stockpile then
pile="deck:stockpile"
elseif fields.to_chestpile then
pile="deck:chestpile"
else
minetest.log("not flipped")
return
end
minetest.log(n.name.." to "..pile)
minetest.remove_node(pos)
minetest.place_node(pos,{name=pile})
local meta=minetest.get_meta(pos)
local inv=meta:get_inventory()
pile_add_card(inv,n.name)
minetest.close_formspec(sender:get_player_name(),"")
end
2020-05-03 00:30:37 +03:00
for i = 1,table.getn(textures),1 do
local name=cardprefix..textures[i]
table.insert(cardnames,name)
minetest.register_node(name, {
description = descriptions[i],
groups = cardgroups,
on_construct = function(pos, placer, itemstack, pointed_thing)
local meta=minetest.get_meta(pos)
meta:set_string("formspec", "size[3,3]"..
"label[0,0;Flip card to make a]"..
"button[0,1;2,1;to_stockpile;Stockpile]"..
"button[0,2;2,1;to_chestpile;Chestpile]")
end,
on_receive_fields = flip_card,
2020-05-03 00:30:37 +03:00
tiles = { textures[i]..".png" }
})
end
local function pile_inv_setup(pos, placer, itemstack, pointed_thing)
local meta=minetest.get_meta(pos)
local inv=meta:get_inventory()
local invsize=13*4
if inv:set_size("main",invsize) then
inv:set_width("main",13)
minetest.log("deck_setup: size set to "..invsize)
else
minetest.log("deck_setup: failed to set size")
end
--for i=1,invsize,1 do
-- local stack=inv:get_stack("main",i)
--end
end
local function pile_pop(inv)
local s=inv:get_size("main")
for i=1,s,1 do
local stack=inv:get_stack("main",i)
if stack:get_count() > 0 then
local item=stack:take_item(1)
inv:set_stack("main",i,stack)
return item
end
end
end
local function pile_peek_first(inv)
local s=inv:get_size("main")
for i=1,s,1 do
local stack=inv:get_stack("main",i)
if stack:get_count() > 0 then
return stack:get_name()
end
end
end
local function add_to_inv(inv, cardname)
local s=inv:get_size("main")
for i=1,s,1 do
local stack=inv:get_stack("main",i)
if stack:get_count() == 0 then
minetest.log(cardname)
inv:set_stack("main",i,ItemStack(cardname))
return true
end
end
return false
end
local function draw_card(pos,formname,fields,sender)
if fields.draw then
local player_inv=minetest.get_inventory({type="player", name=sender:get_player_name()})
local meta=minetest.get_meta(pos)
local pile_inv=meta:get_inventory()
if pile_inv == nil then minetest.log("pile inv=nil") return end
local stack=pile_pop(pile_inv)
if stack == nil then
minetest.log("empty pile")
else
player_inv:add_item("main",stack)
end
end
end
2020-05-09 21:41:18 +03:00
local function on_dig_pile(pos,node,digger)
local meta=minetest.get_meta(pos)
local pile_inv=meta:get_inventory()
local player_inv=minetest.get_inventory({type="player", name=digger:get_player_name()})
local stack=pile_pop(pile_inv)
while stack ~= nil do
player_inv:add_item("main",stack)
stack=pile_pop(pile_inv)
end
minetest.remove_node(pos)
end
2020-05-03 00:30:37 +03:00
minetest.register_node("deck:chestpile", {
description = "Card pile that user has full access to. Works also like a minetest chest.",
tiles = { "back.png","side.png" },
groups = pilegroups,
on_construct = function(pos, placer, itemstack, pointed_thing)
pile_inv_setup(pos, placer, itemstack, pointed_thing)
local meta=minetest.get_meta(pos)
meta:set_string("formspec","size[13,10]list[context;main;0,0;13,4;]button[0,4;2,1;draw;Draw]list[current_player;main;0,6;8,4;]")
end,
2020-05-09 21:41:18 +03:00
on_dig = on_dig_pile,
2020-05-03 00:30:37 +03:00
on_receive_fields = draw_card
})
minetest.register_node("deck:stockpile", {
description = "Stock pile of cards. Can't be peeked.",
groups = pilegroups,
tiles = { "back.png" },
on_construct = function(pos, placer, itemstack, pointed_thing)
pile_inv_setup(pos, placer, itemstack, pointed_thing)
local meta=minetest.get_meta(pos)
meta:set_string("formspec","size[8,5]button[0,0;2,1;draw;Draw]list[current_player;main;0,1;8,4;]"
)
end,
2020-05-09 21:41:18 +03:00
on_dig = on_dig_pile,
2020-05-03 00:30:37 +03:00
on_receive_fields = draw_card
})
-- minetest.register_on_player_receive_fields(function(player, formname, fields)
-- minetest.log("xformname="..formname)
-- if fields.draw then
-- minetest.chat_send_all("player "..player.name.."draw card")
-- end
-- end)
minetest.register_abm({
label="Drop a card to deck",
nodenames={"deck:chestpile","deck:stockpile"},
neighbors=cardnames,
interval=1.0,
chance = 1,
action=function(pos,node,active_object_count,active_object_count_wider)
local above = {x=pos.x,y=pos.y+1,z=pos.z}
local abovenode = minetest.get_node(above)
--minetest.log("abm1"..minetest.pos_to_string(pos).." "..node.name..
--""..abovenode.name)
if not string.sub(abovenode.name,1,cardprefixlen) == cardprefix then return end
local meta=minetest.get_meta(pos)
local inv=meta:get_inventory()
if pile_add_card_from_pos(inv,above) then
2020-05-03 00:30:37 +03:00
minetest.remove_node(above)
while true do
above={x=above.x,y=above.y+1,z=above.z}
abovenode = minetest.get_node(above)
if string.sub(abovenode.name,1,cardprefixlen) == cardprefix then
minetest.spawn_falling_node(above)
else
return
end
end
end
end
})
-- minetest.register_craft({
-- output = "deck:stockpile",
-- recipe = {{"group:card"}}
-- })
-- minetest.register_craft({
-- output = "deck:chestpile",
-- recipe = {{"group:card"}}
-- })
-- minetest.register_on_craft(function(itemstack,player,old_craft_grid,craft_inv)
-- local s=craft_inv:get_size("craft")
-- local i
-- for i=1,s,1 do
-- local stack=craft_inv:get_stack("craft",1)
-- if stack:is_empty() then
-- minetest.log("empty")
-- else
-- minetest.log(stack:get_name())
-- end
-- end
-- end)