trading_cards/init.lua

151 lines
7.6 KiB
Lua
Raw Normal View History

2023-02-19 19:05:23 -05:00
-- This function generates cards
-- It uses a table that has 3 values:
-- name
-- description
-- on_use_items
2023-02-19 15:17:04 -05:00
function generate_cards(cards)
for key,value in pairs(cards) do
local card_id = "tradingcards:" .. cards[key]["name"]
local card_image = cards[key]["name"] .. ".png"
minetest.register_craftitem(card_id, {
description = cards[key]["description"],
inventory_image = card_image,
on_use = function(itemstack, user, pointed_thing)
2023-02-19 18:43:15 -05:00
if user:get_inventory():room_for_item("main",cards[key]["on_use_item"])
then
itemstack:take_item()
user:get_inventory():add_item("main", cards[key]["on_use_item"])
else
minetest.chat_send_player(user:get_player_name(), "No Room in inventory")
end
return itemstack
2023-02-19 15:17:04 -05:00
end,
})
end
end
2023-02-19 19:05:23 -05:00
-- This function generates a random card in a table
-- It uses a table that has 3 values:
-- name
-- description
2023-02-19 15:17:04 -05:00
function get_random_card(card_type)
local card = card_type[ math.random( #card_type )]
return "tradingcards:" .. card["name"]
end
2023-02-19 19:05:23 -05:00
-- Get a random card based on rarity
2023-02-19 15:17:04 -05:00
function get_card_for_pack()
-- 60% common
-- 25% uncommon
-- 10% rare
-- 5% super rare
local random_number = math.random(1, 100)
local card = ""
if random_number <= 60
then
card = get_random_card(common_cards)
elseif random_number <= 85
then
card = get_random_card(uncommon_cards)
elseif random_number <= 95
then
card = get_random_card(rare_cards)
else
card = get_random_card(super_rare_cards)
end
print(card)
return card
end
2023-02-19 19:05:23 -05:00
-- Add card pack item, gives 3 random cards
2023-02-19 15:17:04 -05:00
minetest.register_craftitem("tradingcards:card_pack", {
description = "MTG Card Pack!",
inventory_image = 'card_pack.png',
on_use = function(itemstack, user, pointed_thing)
local empty_count = 0
for k, v in pairs(user:get_inventory():get_list("main")) do
if tostring(v) == 'ItemStack("")'
then
empty_count = empty_count + 1
end
end
print(empty_count)
if empty_count < 3
then
minetest.chat_send_player(user:get_player_name(),"You can only use this item if you have 3 open inventory slots")
else
itemstack:take_item()
local card = get_card_for_pack()
user:get_inventory():add_item("main", card)
local card = get_card_for_pack()
user:get_inventory():add_item("main", card)
local card = get_card_for_pack()
user:get_inventory():add_item("main", card)
end
return itemstack
end,
})
2023-02-19 19:05:23 -05:00
-- Cards separated by rarity
common_cards = {
{name = "card_default_tree", description = "Apple Tree Card\nMTG Card #1", on_use_item = "default:tree 5"},
{name = "card_default_acacia_tree", description = "Acacia Tree Card\nMTG Card #2", on_use_item = "default:acacia_tree 5"},
{name = "card_default_aspen_tree", description = "Aspen Tree Card\nMTG Card #3", on_use_item = "default:aspen_tree 5"},
{name = "card_default_jungle_tree", description = "Jungle Tree Card\nMTG Card #4", on_use_item = "default:jungletree 5"},
{name = "card_default_pine_tree", description = "Pine Tree Card\nMTG Card #5", on_use_item = "default:pine_tree 5"},
{name = "card_default_tool_stone_pickaxe", description = "Stone Pickaxe Card\nMTG Card #6", on_use_item = "default:pick_stone 1"},
{name = "card_default_tool_stone_axe", description = "Stone Axe Card\nMTG Card #7", on_use_item = "default:axe_stone 1"},
{name = "card_default_tool_stone_sword", description = "Stone Sword Card\nMTG Card #8", on_use_item = "default:sword_stone 1"},
{name = "card_default_tool_stone_shovel", description = "Stone Shovel Card\nMTG Card #9", on_use_item = "default:shovel_stone 1"},
{name = "card_default_tool_wood_pickaxe", description = "Wood Pickaxe Card\nMTG Card #10", on_use_item = "default:pick_wood 1"},
{name = "card_default_tool_wood_axe", description = "Wood Axe Card\nMTG Card #11", on_use_item = "default:axe_wood 1"},
{name = "card_default_tool_wood_sword", description = "Wood Sword Card\nMTG Card #12", on_use_item = "default:sword_wood 1"},
{name = "card_default_tool_wood_shovel", description = "Wood Shovel Card\nMTG Card #13", on_use_item = "default:shovel_stone 1"},
{name = "card_default_coal_lump", description = "Coal Card\nMTG Card #14", on_use_item = "default:coal_lump 5"},
{name = "card_default_copper_lump", description = "Copper Card\nMTG Card #15", on_use_item = "default:copper_lump 5"},
{name = "card_default_iron_lump", description = "Iron Card\nMTG Card #16", on_use_item = "default:iron_lump 5"},
{name = "card_default_tin_lump", description = "Tin Card\nMTG Card #17", on_use_item = "default:tin_lump 5"},
}
uncommon_cards = {
{name = "card_default_tool_bronze_pickaxe", description = "Bronze Pickaxe Card\nMTG Card #18", on_use_item = "default:pick_bronze 1"},
{name = "card_default_tool_bronze_axe", description = "Bronze Axe Card\nMTG Card #19", on_use_item = "default:axe_bronze 1"},
{name = "card_default_tool_bronze_sword", description = "Bronze Sword Card\nMTG Card #20", on_use_item = "default:sword_bronze 1"},
{name = "card_default_tool_bronze_shovel", description = "Bronze Shovel Tree Card\nMTG Card #21", on_use_item = "default:shovel_bronze 1"},
{name = "card_default_gold_lump", description = "Gold Card\nMTG Card #22", on_use_item = "default:gold_lump 1",},
}
rare_cards = {
{name = "card_default_mese_crystal", description = "Mese Crystal Card\nMTG Card #23", on_use_item = "default:mese_crystal 1"},
{name = "card_default_tool_steel_pickaxe", description = "Steel Pickaxe Card\nMTG Card #24", on_use_item = "default:pick_steel 1"},
{name = "card_default_tool_steel_axe", description = "Steel Axe Card\nMTG Card #25", on_use_item = "default:axe_steel 1"},
{name = "card_default_tool_steel_sword", description = "Steel Sword Card\nMTG Card #26", on_use_item = "default:sword_steel 1"},
{name = "card_default_tool_steel_shovel", description = "Steel Shovel Tree Card\nMTG Card #27", on_use_item = "default:shovel_steel 1"},
}
super_rare_cards = {
{name = "card_default_tool_diamond_pickaxe", description = "Diamond Pickaxe Card\nMTG Card #28", on_use_item = "default:pick_diamond 1"},
{name = "card_default_tool_diamond_axe", description = "Diamond Axe Card\nMTG Card #29", on_use_item = "default:axe_diamond 1"},
{name = "card_default_tool_diamond_sword", description = "Diamond Sword Card\nMTG Card #30", on_use_item = "default:sword_diamond 1"},
{name = "card_default_tool_diamond_shovel", description = "Diamond Shovel Tree Card\nMTG Card #31", on_use_item = "default:shovel_diamond 1"},
{name = "card_default_tool_mese_pickaxe", description = "Mese Pickaxe Card\nMTG Card #32", on_use_item = "default:pick_mese 1"},
{name = "card_default_tool_mese_axe", description = "Mese Axe Card\nMTG Card #33", on_use_item = "default:axe_mese 1"},
{name = "card_default_tool_mese_sword", description = "Mese Sword Card\nMTG Card #34", on_use_item = "default:sword_mese 1"},
{name = "card_default_tool_mese_shovel", description = "Mese Shovel Tree Card\nMTG Card #35", on_use_item = "default:shovel_mese 1"},
{name = "card_default_diamond", description = "Diamond Card\nMTG Card #37", on_use_item = "default:diamond 1"},
}
2023-02-19 15:17:04 -05:00
generate_cards(common_cards)
generate_cards(uncommon_cards)
generate_cards(rare_cards)
generate_cards(super_rare_cards)