Add armor mod

master
BlockMen 2015-10-12 15:57:35 +02:00
parent c8fe4d9bb6
commit b9998b45f3
46 changed files with 318 additions and 0 deletions

27
mods/armor/README.txt Normal file
View File

@ -0,0 +1,27 @@
This mod is part of Minetest NeXt
==================================
License of source code:
-----------------------
Copyright (C) 2015 BlockMen <blockmen2015@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
License of media (textures, sounds, meshes):
--------------------------------------------
(by Authors)
Ryan Jones (CC-BY-SA):
armor_*.png

29
mods/armor/api.lua Normal file
View File

@ -0,0 +1,29 @@
function armor.register_armor(name, def)
if not name or not def then
return false
end
armor.registered_armor[name] = def
for k,v in pairs(def.elements) do
local elem_def = v
local element = tostring(k)
elem_def.groups = v.groups or {}
elem_def.groups["armor"] = 1
elem_def.groups["armor_" .. element] = v.protection_lvl or def.protection_lvl
elem_def.groups["armor_heal"] = v.uses or def.healing or 0
elem_def.groups["armor_uses"] = v.uses or def.uses or 1
elem_def.wear = 0
minetest.register_tool("armor:" .. name .. "_" .. element, elem_def)
if v.recipe then
minetest.register_craft({
output = "armor:" .. name .. "_" .. element,
recipe = v.recipe,
})
end
end
return true
end

81
mods/armor/armor.lua Normal file
View File

@ -0,0 +1,81 @@
armor.register_armor("wood", {
protection_lvl = 5, -- how much (%) it reduces the damage (for each element)
uses = 30, --number how often it protects (for each element)
elements = {
head = {
description = "Wooden Helmet",
inventory_image = "armor_wood_head_inv.png",
recipe = {
{"group:wood", "group:wood", "group:wood"},
{"group:wood", "", "group:wood"},
}
},
chest = {
description = "Wooden Chestplate",
inventory_image = "armor_wood_chest_inv.png",
recipe = {
{"group:wood", "", "group:wood"},
{"group:wood", "group:wood", "group:wood"},
{"group:wood", "group:wood", "group:wood"}
}
},
legs = {
description = "Wooden Leggins",
inventory_image = "armor_wood_legs_inv.png",
recipe = {
{"group:wood", "group:wood", "group:wood"},
{"group:wood", "", "group:wood"},
{"group:wood", "", "group:wood"}
}
},
feet = {
description = "Wooden Shoes",
inventory_image = "armor_wood_feet_inv.png",
recipe = {
{"group:wood", "", "group:wood"},
{"group:wood", "", "group:wood"}
}
},
}
})
armor.register_armor("steel", {
protection_lvl = 5, -- how much (%) it reduces the damage (for each element)
uses = 30, --number how often it protects (for each element)
elements = {
head = {
description = "Steel Helmet",
inventory_image = "armor_steel_head_inv.png",
recipe = {
{"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"},
{"default:steel_ingot", "", "default:steel_ingot"},
}
},
chest = {
description = "Steel Chestplate",
inventory_image = "armor_steel_chest_inv.png",
recipe = {
{"default:steel_ingot", "", "default:steel_ingot"},
{"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"},
{"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"}
}
},
legs = {
description = "Steel Leggins",
inventory_image = "armor_steel_legs_inv.png",
recipe = {
{"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"},
{"default:steel_ingot", "", "default:steel_ingot"},
{"default:steel_ingot", "", "default:steel_ingot"}
}
},
feet = {
description = "Steel Shoes",
inventory_image = "armor_steel_feet_inv.png",
recipe = {
{"default:steel_ingot", "", "default:steel_ingot"},
{"default:steel_ingot", "", "default:steel_ingot"}
}
},
}
})

2
mods/armor/depends.txt Normal file
View File

@ -0,0 +1,2 @@
default

151
mods/armor/functions.lua Normal file
View File

@ -0,0 +1,151 @@
local elements = armor.elements
local function calc_protection(player)
local name = player:get_player_name()
local armor_inv = core.get_inventory({type = "detached", name = name .. "_armor"})
local protection = 0
for _,v in ipairs(elements) do
local stack = armor_inv:get_stack("armor_" .. v, 1)
if stack:get_count() > 0 then
local def = stack:get_definition()
if def then
local prot_val = def.groups["armor_" .. v] or 0
protection = protection + prot_val
end
end
end
--default.player_armor[name] = protection
return protection
end
function armor.add_wearout(player)
local name = player:get_player_name()
local armor_inv = core.get_inventory({type = "detached", name = name .. "_armor"})
local player_inv = player:get_inventory()
for _,v in ipairs(elements) do
local stack = armor_inv:get_stack("armor_" .. v, 1)
if stack:get_count() > 0 then
local def = stack:get_definition()
if def then
local wearout = 65535 / (def.groups["armor_uses"] or 1)
stack:add_wear(wearout)
local stack2 = stack
armor_inv:set_stack("armor_" .. v, 1, stack)
player_inv:set_stack("armor_" .. v, 1, stack2)
end
end
end
end
local function update_textures(player)
local name = player:get_player_name()
local armor_inv = core.get_inventory({type = "detached", name = name .. "_armor"})
local textures = ""
for _,v in ipairs(elements) do
local element = armor_inv:get_stack("armor_" .. v, 1):get_name()
if element ~= nil and element ~= "" then
textures = textures .. "^" .. element:gsub("%:", "_") .. ".png"
end
end
if textures == "" then
textures = "^default_armor_blank.png"
end
default.player_set_armor_texture(player, textures:sub(2))
end
function armor.on_damage(player, hp_change)
if not hp_change or hp_change >= 0 then
return hp_change or 0
end
local fleshy = player:get_armor_groups().fleshy or 100
local new_hp = math.floor(hp_change * (fleshy / 100)) or 0
if new_hp ~= hp_change then
armor.add_wearout(player)
armor.update_armor(player)
end
return new_hp
end
function armor.update_armor(player)
-- calc protection value depending on current armor (0 - 100)
local lvl = calc_protection(player)
-- set armor_groups depending on protection lvl
if lvl then
player:set_armor_groups({fleshy = 100 - lvl})
end
-- update armor textures
update_textures(player)
--update hud
end
local function is_armor(stack)
if not stack then
return false
end
local name = stack:get_name()
local def = core.registered_items[name]
if def and def.groups then
if def.groups.armor ~= nil then
return true
end
end
return false
end
function armor.init_player(player)
local name = player:get_player_name()
local player_inv = player:get_inventory()
local armor_inv = core.create_detached_inventory(name .. "_armor", {
on_put = function(inv, listname, index, stack, player)
player:get_inventory():set_stack(listname, index, stack)
armor.update_armor(player)
end,
on_take = function(inv, listname, index, stack, player)
player:get_inventory():set_stack(listname, index, nil)
armor.update_armor(player)
end,
on_move = function(inv, from_list, from_index, to_list, to_index, count, player)
local plaver_inv = player:get_inventory()
local stack = inv:get_stack(to_list, to_index)
player_inv:set_stack(to_list, to_index, stack)
player_inv:set_stack(from_list, from_index, nil)
armor.update_armor(player)
end,
allow_put = function(inv, listname, index, stack, player)
local field = core.registered_items[stack:get_name()]
if (field and field.groups[listname] and field.groups[listname] ~= 0) and inv:is_empty(listname) then
return 1
end
return 0
end,
allow_take = function(inv, listname, index, stack, player)
return stack:get_count()
end,
allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
return count
end,
})
for _,v in ipairs(elements) do
local list = "armor_" .. v
player_inv:set_size(list, 1)
armor_inv:set_size(list, 1)
armor_inv:set_stack(list, 1, player_inv:get_stack(list, 1))
end
armor.update_armor(player)
end

28
mods/armor/init.lua Normal file
View File

@ -0,0 +1,28 @@
armor = {}
armor.registered_armor = {}
armor.elements = {"head", "chest", "legs", "feet"}
local mod_path = minetest.get_modpath("armor")
dofile(mod_path .. "/api.lua")
dofile(mod_path .. "/functions.lua")
dofile(mod_path .. "/armor.lua")
minetest.register_on_joinplayer(function(player)
armor.init_player(player)
local player_name = player:get_player_name()
local form_add = "list[detached:" .. player_name .. "_armor;armor_head;0,0;1,1;]" ..
"list[detached:" .. player_name .. "_armor;armor_chest;0,1;1,1;]" ..
"list[detached:" .. player_name .. "_armor;armor_legs;0,2;1,1;]" ..
"list[detached:" .. player_name .. "_armor;armor_feet;0,3;1,1;]"
if not minetest.setting_getbool("creative_mode") then
player:set_inventory_formspec(player:get_inventory_formspec() .. form_add)
end
end)
minetest.register_on_player_hpchange(function(player, hp_change)
return armor.on_damage(player, hp_change)
end, true)

Binary file not shown.

After

Width:  |  Height:  |  Size: 556 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 142 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 321 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 129 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 602 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 140 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 335 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 155 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 475 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 142 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 295 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 129 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 524 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 140 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 306 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 155 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 430 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 142 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 287 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 129 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 489 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 140 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 298 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 155 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 537 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 142 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 316 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 129 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 556 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 140 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 331 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 155 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 453 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 142 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 274 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 129 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 485 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 140 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 285 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 155 B