Use tool capabilities and add more levels (#12)

This commit is contained in:
OgelGames 2020-06-18 21:37:21 +10:00 committed by GitHub
parent 9ca558718a
commit 2ac0b96b47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 124 additions and 102 deletions

@ -1,14 +1,27 @@
# minetest-toolranks # minetest-toolranks [toolranks]
Minetest tool ranks mod Minetest tool ranks mod
Tool gains levels for digging nodes. Higher level tools take longer to Tools gain levels for digging nodes. Higher level tools dig faster and take longer to wear out.
wear out.
## Are you a mod developer? ## Are you a mod developer?
Does one of your mods add new tools? Does one of your mods add new tools?
If so, to support this mod, check if it is loaded with If so, to support this mod, add this code to your mod, after your tool's code:
```minetest.get_modpath("toolranks")```
and then replace all after_use definitions with toolranks.new_afteruse. ```lua
Optionaly, you can also replace tools description with if minetest.get_modpath("toolranks") then
```toolranks.create_description("Tool Name", 0, 1)``` minetest.override_item("mymod:mytool", {
and then set original_description to your tools name. original_description = "My Tool",
description = toolranks.create_description("My Tool", 0, 1),
after_use = toolranks.new_afteruse
})
end
end
```
Or alternatively, you can use the helper function:
```lua
toolranks.add_tool("mymod:mytool")
```

147
init.lua

@ -10,6 +10,12 @@ toolranks.colors = {
white = minetest.get_color_escape_sequence("#ffffff") white = minetest.get_color_escape_sequence("#ffffff")
} }
local max_speed = tonumber(minetest.settings:get("toolranks_speed_multiplier")) or 2.0
local max_use = tonumber(minetest.settings:get("toolranks_use_multiplier")) or 2.0
local max_level = tonumber(minetest.settings:get("toolranks_levels")) or 10
local level_digs = tonumber(minetest.settings:get("toolranks_level_digs")) or 500
local level_multiplier = 1 / max_level
function toolranks.get_tool_type(description) function toolranks.get_tool_type(description)
if not description then if not description then
return "tool" return "tool"
@ -34,103 +40,98 @@ end
function toolranks.create_description(name, uses, level) function toolranks.create_description(name, uses, level)
local description = name local description = name
local tooltype = toolranks.get_tool_type(description) local tooltype = toolranks.get_tool_type(description)
local newdesc = S(
local newdesc = S("@1@2\n@3Level @4 @5\n@6@Node dug: @7", "@1@2\n@3Level @4 @5\n@6@Node dug: @7",
toolranks.colors.green, toolranks.colors.green,
description, description,
toolranks.colors.gold, toolranks.colors.gold,
(level or 1), (level or 1),
S(tooltype), S(tooltype),
toolranks.colors.grey, toolranks.colors.grey,
(uses or 0)) (uses or 0)
)
return newdesc return newdesc
end end
function toolranks.get_level(uses) function toolranks.get_level(uses)
if uses <= 200 then return math.min(max_level, math.floor(uses / level_digs))
return 1
elseif uses < 400 then
return 2
elseif uses < 1000 then
return 3
elseif uses < 2000 then
return 4
elseif uses < 3200 then
return 5
else
return 6
end
end end
function toolranks.new_afteruse(itemstack, user, node, digparams) function toolranks.new_afteruse(itemstack, user, node, digparams)
local itemmeta = itemstack:get_meta() -- Metadata local itemmeta = itemstack:get_meta()
local itemdef = itemstack:get_definition() -- Item Definition local itemdef = itemstack:get_definition()
local itemdesc = itemdef.original_description -- Original Description local itemdesc = itemdef.original_description
local dugnodes = tonumber(itemmeta:get_string("dug")) or 0 -- Number of nodes dug local dugnodes = tonumber(itemmeta:get_string("dug")) or 0
local lastlevel = tonumber(itemmeta:get_string("lastlevel")) or 1 -- Level the tool had local lastlevel = tonumber(itemmeta:get_string("lastlevel")) or 0
-- on the last dig
local most_digs = mod_storage:get_int("most_digs") or 0 local most_digs = mod_storage:get_int("most_digs") or 0
local most_digs_user = mod_storage:get_string("most_digs_user") or 0 local most_digs_user = mod_storage:get_string("most_digs_user") or 0
-- Only count nodes that spend the tool if digparams.wear > 0 then -- Only count nodes that spend the tool
if(digparams.wear > 0) then
dugnodes = dugnodes + 1 dugnodes = dugnodes + 1
itemmeta:set_string("dug", dugnodes) itemmeta:set_string("dug", dugnodes)
end end
if(dugnodes > most_digs) then
most_digs = dugnodes if dugnodes > most_digs then
if(most_digs_user ~= user:get_player_name()) then -- Avoid spam. if most_digs_user ~= user:get_player_name() then -- Avoid spam.
most_digs_user = user:get_player_name() minetest.chat_send_all(S(
minetest.chat_send_all(S("Most used tool is now a @1@2@3 owned by @4 with @5 uses.", "Most used tool is now a @1@2@3 owned by @4 with @5 uses.",
toolranks.colors.green, toolranks.colors.green,
itemdesc, itemdesc,
toolranks.colors.white, toolranks.colors.white,
user:get_player_name(), user:get_player_name(),
dugnodes)) dugnodes
))
end end
mod_storage:set_int("most_digs", dugnodes) mod_storage:set_int("most_digs", dugnodes)
mod_storage:set_string("most_digs_user", user:get_player_name()) mod_storage:set_string("most_digs_user", user:get_player_name())
end end
if(itemstack:get_wear() > 60135) then
if itemstack:get_wear() > 60135 then
minetest.chat_send_player(user:get_player_name(), S("Your tool is about to break!")) minetest.chat_send_player(user:get_player_name(), S("Your tool is about to break!"))
minetest.sound_play("default_tool_breaks", { minetest.sound_play("default_tool_breaks", {
to_player = user:get_player_name(), to_player = user:get_player_name(),
gain = 2.0, gain = 2.0,
}) })
end end
local level = toolranks.get_level(dugnodes)
local level = toolranks.get_level(dugnodes)
if lastlevel < level then if lastlevel < level then
local levelup_text = S("Your @1@2@3 just leveled up!", local levelup_text = S(
"Your @1@2@3 just leveled up!",
toolranks.colors.green, toolranks.colors.green,
itemdesc, itemdesc,
toolranks.colors.white) toolranks.colors.white
)
minetest.chat_send_player(user:get_player_name(), levelup_text)
minetest.sound_play("toolranks_levelup", { minetest.sound_play("toolranks_levelup", {
to_player = user:get_player_name(), to_player = user:get_player_name(),
gain = 2.0, gain = 2.0,
}) })
minetest.chat_send_player(user:get_player_name(), levelup_text)
itemmeta:set_string("lastlevel", level) itemmeta:set_string("lastlevel", level)
local speed_multiplier = 1 + (level * level_multiplier * (max_speed - 1))
local use_multiplier = 1 + (level * level_multiplier * (max_use - 1))
local caps = table.copy(itemdef.tool_capabilities)
caps.full_punch_interval = caps.full_punch_interval and (caps.full_punch_interval / speed_multiplier)
caps.punch_attack_uses = caps.punch_attack_uses and (caps.punch_attack_uses * use_multiplier)
for _,c in pairs(caps.groupcaps) do
c.uses = c.uses * use_multiplier
for i,t in ipairs(c.times) do
c.times[i] = t / speed_multiplier
end
end
itemmeta:set_tool_capabilities(caps)
end end
local newdesc = toolranks.create_description(itemdesc, dugnodes, level) itemmeta:set_string("description", toolranks.create_description(itemdesc, dugnodes, level))
itemstack:add_wear(digparams.wear)
itemmeta:set_string("description", newdesc)
local wear = digparams.wear
if level > 1 then
wear = digparams.wear / (1 + level / 4)
end
--minetest.chat_send_all("wear="..wear.."Original wear: "..digparams.wear.." 1+level/4="..1+level/4)
-- Uncomment for testing ^
itemstack:add_wear(wear)
return itemstack return itemstack
end end
-- Helper function -- Helper function
local function add_tool(name) function toolranks.add_tool(name)
local desc = ItemStack(name):get_definition().description local desc = ItemStack(name):get_definition().description
minetest.override_item(name, { minetest.override_item(name, {
original_description = desc, original_description = desc,
@ -140,33 +141,33 @@ local function add_tool(name)
end end
-- Sword -- Sword
add_tool("default:sword_wood") toolranks.add_tool("default:sword_wood")
add_tool("default:sword_stone") toolranks.add_tool("default:sword_stone")
add_tool("default:sword_steel") toolranks.add_tool("default:sword_steel")
add_tool("default:sword_bronze") toolranks.add_tool("default:sword_bronze")
add_tool("default:sword_mese") toolranks.add_tool("default:sword_mese")
add_tool("default:sword_diamond") toolranks.add_tool("default:sword_diamond")
-- Pickaxe -- Pickaxe
add_tool("default:pick_wood") toolranks.add_tool("default:pick_wood")
add_tool("default:pick_stone") toolranks.add_tool("default:pick_stone")
add_tool("default:pick_steel") toolranks.add_tool("default:pick_steel")
add_tool("default:pick_bronze") toolranks.add_tool("default:pick_bronze")
add_tool("default:pick_mese") toolranks.add_tool("default:pick_mese")
add_tool("default:pick_diamond") toolranks.add_tool("default:pick_diamond")
-- Axe -- Axe
add_tool("default:axe_wood") toolranks.add_tool("default:axe_wood")
add_tool("default:axe_stone") toolranks.add_tool("default:axe_stone")
add_tool("default:axe_steel") toolranks.add_tool("default:axe_steel")
add_tool("default:axe_bronze") toolranks.add_tool("default:axe_bronze")
add_tool("default:axe_mese") toolranks.add_tool("default:axe_mese")
add_tool("default:axe_diamond") toolranks.add_tool("default:axe_diamond")
-- Shovel -- Shovel
add_tool("default:shovel_wood") toolranks.add_tool("default:shovel_wood")
add_tool("default:shovel_stone") toolranks.add_tool("default:shovel_stone")
add_tool("default:shovel_steel") toolranks.add_tool("default:shovel_steel")
add_tool("default:shovel_bronze") toolranks.add_tool("default:shovel_bronze")
add_tool("default:shovel_mese") toolranks.add_tool("default:shovel_mese")
add_tool("default:shovel_diamond") toolranks.add_tool("default:shovel_diamond")

8
settingtypes.txt Normal file

@ -0,0 +1,8 @@
# Number of tool levels
toolranks_levels (Levels) int 10
# Number of nodes that need to be dug to reach the next tool level
toolranks_level_digs (Digs per level) int 500
# Dig speed multiplier at maximum tool level (1.0 to disable)
toolranks_speed_multiplier (Dig speed multiplier) float 2.0 1.0 10.0
# Durability multiplier at maximum tool level (1.0 to disable)
toolranks_use_multiplier (Durability multiplier) float 2.0 1.0 10.0