Skip to content
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

Merged
merged 6 commits into from Jun 3, 2022

Conversation

Wuzzy2
Copy link
Contributor

@Wuzzy2 Wuzzy2 commented Feb 3, 2022

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 and get_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 usesis by adding wear withtool:add_wear(some_fixed_value)in whichsome_fixed_valueis chosen in a way that the tool breaks after exactlyntimes. This works for small numbers ofn(<273) but asnincreases you might actually not be able to hitn` 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

  • Activate luacmd
  • Give yourself any tool with 0 wear and wield it
  • /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)
  • This is a loop that calls add_wear_by_uses(n) n times, so the tool must break after exactly n uses. At the end, it prints out how many iterations this look actually had
  • Since n is 60000 in this case, it should print out 60000
  • Replace the value of n with different values, including 0 (=infinite uses), 65535 or 65536
  • The other function can be tested in a similar fashion
  • Also try manually calling these functions without a loop to see how the wear bar reacts

@Wuzzy2 Wuzzy2 changed the title Get tool wear after use Add helper functions to make tool usable n times Feb 3, 2022
@rubenwardy rubenwardy added @ Script API Feature ✨ PRs that add or enhance a feature Concept approved Approved by a core dev: PRs welcomed! labels Feb 23, 2022
@rubenwardy rubenwardy self-requested a review February 23, 2022 19:01
@ghost
Copy link

ghost commented Mar 3, 2022

This would be cool.

@ghost
Copy link

ghost commented Mar 14, 2022

Tested with all integers between [0 .. 65537]

  • 0 and 65537 never broke
  • All other values broke in the expected number of uses

At a glance, behavior seems correct. Will test more and finish review tomorrow.

Copy link

@ghost ghost left a 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.

src/script/lua_api/l_item.cpp Outdated Show resolved Hide resolved
src/script/lua_api/l_util.cpp Outdated Show resolved Hide resolved
// 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.
Copy link
Member

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

@sfan5
Copy link
Member

sfan5 commented Mar 20, 2022

If you have time it'd also be nice to see unittests in devtest for this API function.

doc/lua_api.txt Outdated Show resolved Hide resolved
doc/lua_api.txt Outdated Show resolved Hide resolved
@Wuzzy2
Copy link
Contributor Author

Wuzzy2 commented May 2, 2022

I fixed the broken bullet points and I renamed the arg.

Copy link
Member

@sfan5 sfan5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

@sfan5 sfan5 merged commit 6a6b579 into minetest:master Jun 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Concept approved Approved by a core dev: PRs welcomed! Feature ✨ PRs that add or enhance a feature @ Script API >= Two approvals ✅ ✅
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants