deck-cd2025/init.lua

376 lines
11 KiB
Lua
Raw Normal View History

function describe(prefix,stuff)
local t=type(stuff)
if(t == "table") then
for key,value in pairs(stuff) do
minetest.log(prefix.." "..key .. "=".. type(value))
describe(prefix.." ",value)
end
elseif(t=="nil" or t=="userdata") then
minetest.log(prefix.." "..t)
elseif(t=="number" or t=="string" or t=="boolean") then
minetest.log(prefix.." "..value)
else
minetest.log(t)
end
end
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
2020-06-01 21:47:46 +03:00
local suits = { "clubs","diamonds","hearts","spades" }
local numbers = { "Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten",
"Jack","Queen","King"}
local texture_file_name_prefix = { "a","1","2","3","4","5","6","7","8","9","t","j","q","k" }
2020-05-03 00:30:37 +03:00
2020-06-01 21:47:46 +03:00
local textures = {}
--{"2d","3d","4d","5d","6d","7d","8d","ad" }
2020-05-03 00:30:37 +03:00
--, "ac", "ah", "as" }
2020-06-01 21:47:46 +03:00
local descriptions = {}
2020-05-03 00:30:37 +03:00
local cardnames = {}
local cardprefix="deck:card_"
local cardprefixlen=string.len(cardprefix)
2020-05-15 20:26:42 +03:00
local carddescriptions = {}
2020-05-03 00:30:37 +03:00
2020-06-01 21:47:46 +03:00
local function generate_descriptions()
local i=1
for suit=1,4,1 do
for number=1,13,1 do
descriptions[i]=numbers[number].." of "..suits[suit]
textures[i]=texture_file_name_prefix[number]..string.sub(suits[suit],1,1)
i=i+1
end
end
--descriptions = {"Two of diamonds", "Three of diamonds",
-- "Four of diamonds",
-- "Five of diamonds","Seven of diamonds","Ace of diamonds" }
--, "Ace of clubs","Ace of hearts","Ace of spades" }
end
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-06-01 21:47:46 +03:00
generate_descriptions()
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)
2020-05-15 20:26:42 +03:00
carddescriptions[name]=descriptions[i]
minetest.register_node(name, {
description = descriptions[i],
groups = cardgroups,
on_construct = function(pos)
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,
tiles = { textures[i]..".png" }
})
2020-05-03 00:30:37 +03:00
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
2020-05-15 20:40:11 +03:00
local function draw_one_card(pos,formname,fields,sender)
2020-05-03 00:30:37 +03:00
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")
2020-05-15 20:40:11 +03:00
return false
2020-05-03 00:30:37 +03:00
else
player_inv:add_item("main",stack)
2020-05-15 20:40:11 +03:00
return true
end
end
local function draw_card(pos,formname,fields,sender)
if fields.draw then
return draw_one_card(pos,formname,fields,sender)
elseif fields.drawall then
while draw_one_card(pos,formname,fields,sender) do
2020-05-03 00:30:37 +03:00
end
end
end
2020-05-09 21:41:18 +03:00
2020-05-15 20:26:42 +03:00
local function on_dig_pile_getcards(pos,node,digger)
2020-05-09 21:41:18 +03:00
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-15 20:26:42 +03:00
local function on_dig_pile_getpile(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 new_chestpile=ItemStack("deck:chestpile")
local stackmeta=new_chestpile:get_meta()
local i=1
local cardstack=pile_pop(pile_inv)
if cardstack == nil then
stackmeta:set_string("description","Empty pile")
return
end
local descr=""
while cardstack ~= nil do
local cardname=cardstack:get_name()
stackmeta:set_string(i,cardname)
cardstack=pile_pop(pile_inv)
descr=descr..", "..carddescriptions[cardname]
end
stackmeta:set_string("description",string.sub(descr,3))
player_inv:add_item("main",new_chestpile)
minetest.remove_node(pos)
end
local function after_place_pile(pos,placer,itemstack,pointed_thing)
local stackmeta=itemstack:get_meta()
local i=1
local card=stackmeta:get_string(i)
if card == nil then
stackmeta:set_string("description","Empty pile")
return
end
local pilemeta=minetest.get_meta(pos)
local pile_inv=pilemeta:get_inventory()
local descr=""
while card ~= "" do
minetest.log("after_place_pile: i="..i.." card="..card)
pile_add_card(pile_inv,card)
descr=descr..", "..carddescriptions[card]
i=i+1
card=stackmeta:get_string(i)
end
stackmeta:set_string("description",string.sub(descr,3))
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,
2020-05-15 20:26:42 +03:00
on_construct = function(pos)
pile_inv_setup(pos)
2020-05-03 00:30:37 +03:00
local meta=minetest.get_meta(pos)
2020-05-15 20:40:11 +03:00
meta:set_string("formspec", "size[13,10]"..
"list[context;main;0,0;13,4;]"..
"button[0,4;2,1;draw;Draw]"..
"button[2,4;2,1;drawall;Draw all]"..
"list[current_player;main;0,6;8,4;]")
2020-05-03 00:30:37 +03:00
end,
2020-05-15 20:26:42 +03:00
on_dig = on_dig_pile_getpile,
stack_max = 1,
after_place_node=after_place_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" },
2020-05-15 20:26:42 +03:00
on_construct = function(pos)
pile_inv_setup(pos)
2020-05-03 00:30:37 +03:00
local meta=minetest.get_meta(pos)
2020-05-15 20:40:11 +03:00
meta:set_string("formspec","size[8,5]"..
"button[0,0;2,1;draw;Draw]"..
"button[2,0;2,1;drawall;Draw all]"..
"list[current_player;main;0,1;8,4;]"
2020-05-03 00:30:37 +03:00
)
end,
2020-05-15 20:26:42 +03:00
on_dig = on_dig_pile_getpile,
stack_max = 1,
after_place_node=after_place_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 string.sub(abovenode.name,1,cardprefixlen) ~= cardprefix then
-- minetest.log("not card, but "..abovenode.name)
return end
2020-05-03 00:30:37 +03:00
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"}}
})
local function craft_pile(itemstack,player,old_craft_grid,craft_inv,really)
--describe("craft",old_craft_grid)
-- local s=craft_inv:get_size("craft")
local place, cardname
for key,value in pairs(old_craft_grid) do
--local stack=craft_inv:get_stack("craft",i)
local stack=old_craft_grid[key]
if stack:is_empty() then
minetest.log("empty "..key)
else
place=key
cardname=stack:get_name()
minetest.log("craft:"..key.."="..stack:get_name())
end
end
if cardname == nil then
return ItemStack()
end
local pilename
if place < 5 then
pilename="deck:chestpile"
else
pilename="deck:stockpile"
end
minetest.log("place="..place.." pilename="..pilename)
local pilestack=ItemStack(pilename)
local stackmeta=pilestack:get_meta()
stackmeta:set_string(1,cardname)
if really then
stackmeta:set_string("description",carddescriptions[cardname])
end
return pilestack
end
minetest.register_on_craft(function(itemstack,player,old_craft_grid,craft_inv)
return craft_pile(itemstack,player,old_craft_grid,craft_inv,true)
end)
minetest.register_craft_predict(function(itemstack,player,old_craft_grid,craft_inv)
return craft_pile(itemstack,player,old_craft_grid,craft_inv,false)
end)