Added comments/refractored #2

Merged
jasonl8446 merged 1 commits from jason-add-cards-to-drop into main 2023-02-19 19:06:21 -05:00

182
init.lua
View File

@ -1,3 +1,94 @@
-- This function generates cards
-- It uses a table that has 3 values:
-- name
-- description
-- on_use_items
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)
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
end,
})
end
end
-- This function generates a random card in a table
-- It uses a table that has 3 values:
-- name
-- description
function get_random_card(card_type)
local card = card_type[ math.random( #card_type )]
return "tradingcards:" .. card["name"]
end
-- Get a random card based on rarity
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
-- Add card pack item, gives 3 random cards
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,
})
-- Cards separated by rarity
common_cards = { common_cards = {
{name = "card_default_tree", description = "Apple Tree Card\nMTG Card #1", on_use_item = "default:tree 5"}, {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_acacia_tree", description = "Acacia Tree Card\nMTG Card #2", on_use_item = "default:acacia_tree 5"},
@ -53,97 +144,6 @@ super_rare_cards = {
{name = "card_default_diamond", description = "Diamond Card\nMTG Card #37", on_use_item = "default:diamond 1"}, {name = "card_default_diamond", description = "Diamond Card\nMTG Card #37", on_use_item = "default:diamond 1"},
} }
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)
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
end,
})
end
end
function get_random_card(card_type)
local card = card_type[ math.random( #card_type )]
return "tradingcards:" .. card["name"]
end
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
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
--[[
itemstack:take_item()
card = get_random_card(common_cards)
user:get_inventory():add_item("main", card)
card = get_random_card(common_cards)
user:get_inventory():add_item("main", card)
card = get_random_card(common_cards)
print(user:get_inventory():room_for_item("main", card))
print(user:get_inventory():add_item("main", card))
print(user:get_inventory():get_size("main"))
print(dump(user:get_inventory():get_list("main")))
]]--
return itemstack
end,
})
generate_cards(common_cards) generate_cards(common_cards)
generate_cards(uncommon_cards) generate_cards(uncommon_cards)