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
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions doc/lua_api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3512,6 +3512,12 @@ Helper functions
* `minetest.pointed_thing_to_face_pos(placer, pointed_thing)`: returns a
position.
* returns the exact position on the surface of a pointed node
* `minetest.get_tool_wear_after_use(uses [, initial_wear])`
* Simulates a tool being used once and returns the added wear,
* such that, if only this function is used to calculate wear,
Wuzzy2 marked this conversation as resolved.
Show resolved Hide resolved
* the tool will break exactly after `uses` times of uses
* `uses`: Number of times the tool can be used
* `initial_wear`: The initial wear the tool starts with (default: 0)
* `minetest.get_dig_params(groups, tool_capabilities [, wear])`:
Simulates an item that digs a node.
Returns a table with the following fields:
Expand Down Expand Up @@ -6375,7 +6381,13 @@ an itemstring, a table or `nil`.
or those of the hand if none are defined for this item type
* `add_wear(amount)`
* Increases wear by `amount` if the item is a tool, otherwise does nothing
* Valid `amount` range is [0,65536]
* `amount`: number, integer
* `add_wear_by_uses(uses)`
Wuzzy2 marked this conversation as resolved.
Show resolved Hide resolved
* Increases wear in such a way that, if only this function is called,
the item breaks after `uses` times
* Valid `uses` range is [0,65536]
* Does nothing if item is not a tool or if `uses` is 0
* `add_item(item)`: returns leftover `ItemStack`
* Put some item or stack onto this stack
* `item_fits(item)`: returns `true` if item or stack can be fully added to
Expand Down
23 changes: 22 additions & 1 deletion src/script/lua_api/l_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ int LuaItemStack::l_get_tool_capabilities(lua_State *L)
}

// add_wear(self, amount) -> true/false
// The range for "amount" is [0,65535]. Wear is only added if the item
// The range for "amount" is [0,65536]. Wear is only added if the item
// is a tool. Adding wear might destroy the item.
// Returns true if the item is (or was) a tool.
int LuaItemStack::l_add_wear(lua_State *L)
Expand All @@ -356,6 +356,26 @@ int LuaItemStack::l_add_wear(lua_State *L)
return 1;
}

// add_wear_by_uses(self, uses) -> true/false
// The range for "uses" is [0,65536].
// Adds wear to the item in such a way that, if
// 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.
int LuaItemStack::l_add_wear_by_uses(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1);
ItemStack &item = o->m_stack;
u32 uses = readParam<int>(L, 2);
u16 initial_wear = item.wear;
u32 add_wear = calculateResultWear(uses, initial_wear);
bool result = item.addWear(add_wear, getGameDef(L)->idef());
lua_pushboolean(L, result);
sfan5 marked this conversation as resolved.
Show resolved Hide resolved
return 1;
}

// add_item(self, itemstack or itemstring or table or nil) -> itemstack
// Returns leftover item stack
int LuaItemStack::l_add_item(lua_State *L)
Expand Down Expand Up @@ -514,6 +534,7 @@ const luaL_Reg LuaItemStack::methods[] = {
luamethod(LuaItemStack, get_definition),
luamethod(LuaItemStack, get_tool_capabilities),
luamethod(LuaItemStack, add_wear),
luamethod(LuaItemStack, add_wear_by_uses),
luamethod(LuaItemStack, add_item),
luamethod(LuaItemStack, item_fits),
luamethod(LuaItemStack, take_item),
Expand Down
11 changes: 10 additions & 1 deletion src/script/lua_api/l_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,20 @@ class LuaItemStack : public ModApiBase {
static int l_get_tool_capabilities(lua_State *L);

// add_wear(self, amount) -> true/false
// The range for "amount" is [0,65535]. Wear is only added if the item
// The range for "amount" is [0,65536]. Wear is only added if the item
// is a tool. Adding wear might destroy the item.
// Returns true if the item is (or was) a tool.
static int l_add_wear(lua_State *L);

// add_wear_by_uses(self, uses) -> true/false
// The range for "uses" is [0,65536].
// Adds wear to the item in such a way that, if
// 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

static int l_add_wear_by_uses(lua_State *L);

// add_item(self, itemstack or itemstring or table or nil) -> itemstack
// Returns leftover item stack
static int l_add_item(lua_State *L);
Expand Down
14 changes: 14 additions & 0 deletions src/script/lua_api/l_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,19 @@ int ModApiUtil::l_write_json(lua_State *L)
return 1;
}

// get_tool_wear_after_use(uses[, initial_wear])
int ModApiUtil::l_get_tool_wear_after_use(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
u32 uses = readParam<int>(L, 1);
u16 initial_wear = 0;
if (!lua_isnoneornil(L, 2))
initial_wear = readParam<int>(L, 2);
sfan5 marked this conversation as resolved.
Show resolved Hide resolved
u16 wear = calculateResultWear(uses, initial_wear);
lua_pushnumber(L, wear);
return 1;
}

// get_dig_params(groups, tool_capabilities[, wear])
int ModApiUtil::l_get_dig_params(lua_State *L)
{
Expand Down Expand Up @@ -586,6 +599,7 @@ void ModApiUtil::Initialize(lua_State *L, int top)
API_FCT(parse_json);
API_FCT(write_json);

API_FCT(get_tool_wear_after_use);
API_FCT(get_dig_params);
API_FCT(get_hit_params);

Expand Down
3 changes: 3 additions & 0 deletions src/script/lua_api/l_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class ModApiUtil : public ModApiBase
// write_json(data[, styled])
static int l_write_json(lua_State *L);

// get_tool_wear_after_use(uses[, initial_wear])
static int l_get_tool_wear_after_use(lua_State *L);

// get_dig_params(groups, tool_capabilities[, wear])
static int l_get_dig_params(lua_State *L);

Expand Down
2 changes: 1 addition & 1 deletion src/tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ void ToolCapabilities::deserializeJson(std::istream &is)
}
}

static u32 calculateResultWear(const u32 uses, const u16 initial_wear)
u32 calculateResultWear(const u32 uses, const u16 initial_wear)
{
if (uses == 0) {
// Trivial case: Infinite uses
Expand Down
1 change: 1 addition & 0 deletions src/tool.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,5 @@ PunchDamageResult getPunchDamage(
u16 initial_wear = 0
);

u32 calculateResultWear(const u32 uses, const u16 initial_wear);
f32 getToolRange(const ItemDefinition &def_selected, const ItemDefinition &def_hand);