New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add helper functions to make tool usable n times #12047
Conversation
This would be cool. |
Tested with all integers between [0 .. 65537]
At a glance, behavior seems correct. Will test more and finish review tomorrow. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looked over the code, also did some further manual testing mixing uses
values and behavior seemed sensible. Looks good to me.
// only this function is called to add wear, the item | ||
// will be destroyed exactly after `uses` times of calling it. | ||
// No-op if `uses` is 0 or item is not a tool. | ||
// Returns true if the item is (or was) a tool. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just as information: I don't think it's necessary to put detailed documentation here, btw
If you have time it'd also be nice to see unittests in devtest for this API function. |
I fixed the broken bullet points and I renamed the arg. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good.
This adds two functions:
item:add_wear_by_uses
minetest.get_tool_wear_after_use
These are two helper functions for tools that have a specified number of uses before breaking. Two examples for such tools are MTG's Screwdriver and Flint and Steel. This makes sure the tool will break after the exact specified number of uses.
The first function actually adds the wear to the tool and (if neccessary) breaks it.
The second function only "simulates" it, analog to
get_dig_params
andget_hit_params
.It also fixes a minor mistake in the function comments.
add_wear
claims to support only a range of [0,65535] but this is false, it also supports 65536 (which leads to insta-break). I checked this.Context
The common (and naive) way that mods usually implement "tool break after n uses
is by adding wear with
tool:add_wear(some_fixed_value)in which
some_fixed_valueis chosen in a way that the tool breaks after exactly
ntimes. This works for small numbers of
n(<273) but as
nincreases you might actually not be able to hit
n` exactly due to rounding. As an extreme example, with this method you can't have a tool with exactly 50000 uses, but must decide 32768 or 65536 uses (adding 2 or 1 wear at every use, respectively).That's where the helper function comes into play.
This PR builds on the work done in #11110 and is basically an extension of the logic used there for mods. It uses
calculateResultWear
for the calcuation. Read this PR for more context.Note the actual math is NOT altered by this PR in any way. It basically just exposes the "tool uses before it breaks" calculation the engine already uses to Lua.
To do
This PR is ready for review.
How to test
luacmd
/lua n=60000; p=0; x=me:get_wielded_item(); for i=1,65536 do x:add_wear_by_uses(n); p=p+1; if x:get_count()==0 then break end end print(p); me:set_wielded_item(x)
add_wear_by_uses(n)
n
times, so the tool must break after exactlyn
uses. At the end, it prints out how many iterations this look actually hadn
is 60000 in this case, it should print out 60000n
with different values, including 0 (=infinite uses), 65535 or 65536