commit c9015954fcd8f9318c69bed3a114f3bdc70b9e4d Author: SmallJoker Date: Tue May 15 19:32:23 2018 +0200 Upload :boom: diff --git a/README.md b/README.md new file mode 100644 index 0000000..fa6f224 --- /dev/null +++ b/README.md @@ -0,0 +1,12 @@ +# Upgrade Packs + +## Description + +[Screenshot to be added] + +This server mod provides upgradeable packs in form of items to players which give them additional health or breath. However, the better the health packs are and the more of them you use, the faster they will wear out. + +License: MIT + +## Craft recipes +1. Download and install a craft guide diff --git a/api.lua b/api.lua new file mode 100644 index 0000000..72bcfe7 --- /dev/null +++ b/api.lua @@ -0,0 +1,55 @@ +function upgrade_packs.add_wear(player, pack, amount) + local lookup = upgrade_packs[pack .. "_items"] + + local inv = player:get_inventory() + local list = inv:get_list("ugpacks") + for i, stack in pairs(list) do + if lookup[stack:get_name()] then + assert(stack:add_wear(amount), "Wear out impossible: " + .. stack:get_name()) + end + end + inv:set_list("ugpacks", list) +end + +function upgrade_packs.register_pack(name, pack, pack_def) + assert(pack == "breath" or pack == "health") + assert(pack_def.description) + assert(pack_def.image) + assert(pack_def.strength > 0) + + local def = { + description = pack_def.description, + inventory_image = pack_def.image, + groups = pack_def.groups or {} + } + def.groups["upgrade_" .. pack] = pack_def.strength + + minetest.register_tool(name, def) +end + +function upgrade_packs.update_player(player) + local inv = player:get_inventory() + local health = minetest.PLAYER_MAX_HP_DEFAULT + local breath = minetest.PLAYER_MAX_BREATH_DEFAULT + + local health_items = upgrade_packs.health_items + local breath_items = upgrade_packs.breath_items + + local list = inv:get_list("ugpacks") + for i, stack in pairs(list) do + local name = stack:get_name() + if health_items[name] then + health = health + health_items[name] + elseif breath_items[name] then + breath = breath + breath_items[name] + else + -- How did we reach this? + end + end + + player:set_properties({ + hp_max = health, + breath_max = breath + }) +end \ No newline at end of file diff --git a/gui_sfinv.lua b/gui_sfinv.lua new file mode 100644 index 0000000..09f50ce --- /dev/null +++ b/gui_sfinv.lua @@ -0,0 +1,2 @@ +-- TODO +return \ No newline at end of file diff --git a/gui_unified_inventory.lua b/gui_unified_inventory.lua new file mode 100644 index 0000000..3ba9636 --- /dev/null +++ b/gui_unified_inventory.lua @@ -0,0 +1,22 @@ +if unified_inventory.sfinv_compat_layer then + return +end + +unified_inventory.register_button("ugpacks", { + type = "image", + image = "heart.png", + tooltip = "Upgrade Packs" +}) + +unified_inventory.register_page("ugpacks", { + get_formspec = function(player, perplayer_formspec) + local y = perplayer_formspec.formspec_y + + return { formspec = ( + "label[1,0;Upgrade Packs]".. + "list[current_player;ugpacks;1," ..(y + 1) .. ";2,2;]".. + "listring[current_player;main]".. + "listring[current_player;ugpacks]" + )} + end +}) \ No newline at end of file diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..24c8c6b --- /dev/null +++ b/init.lua @@ -0,0 +1,94 @@ +upgrade_packs = {} +upgrade_packs.health_items = {} +upgrade_packs.breath_items = {} + +local modpath = minetest.get_modpath("upgrade_packs") + +dofile(modpath .. "/api.lua") +if minetest.get_modpath("unified_inventory") then + dofile(modpath .. "/gui_unified_inventory.lua") +elseif minetest.get_modpath("sfinv") then + dofile(modpath .. "/gui_sfinv.lua") +end + +upgrade_packs.register_pack("upgrade_packs:hp_10", "health", { + description = "+10 HP", + strength = 10, + image = "heart.png" +}) + +upgrade_packs.register_pack("upgrade_packs:breath_5", "breath", { + description = "+5 Breath", + strength = 5, + image = "bubble.png" +}) + +-- Cache items which are interesting for this mod +minetest.after(0, function() + local items = minetest.registered_items + local health_items = {} + local breath_items = {} + + for name, def in pairs(items) do + local groups = def.groups or {} + if groups.upgrade_health + and groups.upgrade_health ~= 0 then + health_items[name] = groups.upgrade_health + end + if groups.upgrade_breath + and groups.upgrade_breath ~= 0 then + breath_items[name] = groups.upgrade_breath + end + end + upgrade_packs.health_items = health_items + upgrade_packs.breath_items = breath_items +end) + +minetest.register_on_joinplayer(function(player) + local inv = player:get_inventory() + inv:set_size("ugpacks", 4) + upgrade_packs.update_player(player) +end) + +minetest.register_on_item_eat(function(hp_change, replace_with_item, itemstack, user) + print(dump(hp_change)) + if hp_change == 0 then + return + end + -- Undo some of the wear when eating instead of dying + upgrade_packs.add_wear(user, "health", hp_change * -10) +end) + +minetest.register_on_player_hpchange(function(player, hp_change, reason) + if hp_change >= 0 then + return + end + if reason == "drown" then + upgrade_packs.add_wear(player, "breath", 200) + else + upgrade_packs.add_wear(player, "health", hp_change * -10) + end +end, false) + +minetest.register_allow_player_inventory_action(function(player, action, inv, data) + print(dump(data)) + if data.to_list ~= "ugpacks" then + return -- Not interesting for this mod + end + local stack = inv:get_stack(data.from_list, data.from_index) + + if upgrade_packs.health_items[stack:get_name()] then + return 1 + end + if upgrade_packs.breath_items[stack:get_name()] then + return 1 + end + + return 0 +end) + +minetest.register_on_player_inventory_action(function(player, action, inv, data) + if data.to_list == "ugpacks" or data.from_list == "ugpacks" then + upgrade_packs.update_player(player) + end +end) diff --git a/mod.conf b/mod.conf new file mode 100644 index 0000000..345020d --- /dev/null +++ b/mod.conf @@ -0,0 +1,4 @@ +name = upgrade_packs +description = Provides craftable packs to players to increase their health and breath +depends = default +optional_depends = unified_inventory, sfinv \ No newline at end of file