end
-- END Legacy stuff
+ -- This isn't just legacy, but more of a convenience feature
+ local toolcaps = tooldef.tool_capabilities
+ if toolcaps and toolcaps.punch_attack_uses == nil then
+ for _, cap in pairs(toolcaps.groupcaps or {}) do
+ local level = (cap.maxlevel or 0) - 1
+ if (cap.uses or 0) ~= 0 and level >= 0 then
+ toolcaps.punch_attack_uses = cap.uses * (3 ^ level)
+ break
+ end
+ end
+ end
+
core.register_item(name, tooldef)
end
liquids_pointable = false,
- -- See "Tools" section
+ -- See "Tools" section for an example including explanation
tool_capabilities = {
full_punch_interval = 1.0,
max_drop_level = 0,
uses = 20, maxlevel = 2},
},
damage_groups = {groupname = damage},
+
+ punch_attack_uses = nil,
+ -- Amount of uses this tool has for attacking players and entities
+ -- by punching them (0 = infinite uses).
+ -- For compatibility, this is automatically set from the first
+ -- suitable groupcap using the forumla "uses * 3^(maxlevel - 1)".
+ -- It is recommend to set this explicitly instead of relying on the
+ -- fallback behavior.
},
node_placement_prediction = nil,
*result = os.str();
}
-int LuaEntitySAO::punch(v3f dir,
+u16 LuaEntitySAO::punch(v3f dir,
const ToolCapabilities *toolcap,
ServerActiveObject *puncher,
float time_from_last_punch)
m_env->getGameDef()->SendMovePlayer(m_peer_id);
}
-int PlayerSAO::punch(v3f dir,
+u16 PlayerSAO::punch(v3f dir,
const ToolCapabilities *toolcap,
ServerActiveObject *puncher,
float time_from_last_punch)
bool isStaticAllowed() const
{ return m_prop.static_save; }
void getStaticData(std::string *result) const;
- int punch(v3f dir,
+ u16 punch(v3f dir,
const ToolCapabilities *toolcap = nullptr,
ServerActiveObject *puncher = nullptr,
- float time_from_last_punch = 1000000);
+ float time_from_last_punch = 1000000.0f);
void rightClick(ServerActiveObject *clicker);
void setPos(const v3f &pos);
void moveTo(v3f pos, bool continuous);
Interaction interface
*/
- int punch(v3f dir,
+ u16 punch(v3f dir,
const ToolCapabilities *toolcap,
ServerActiveObject *puncher,
float time_from_last_punch);
u16 src_original_hp = pointed_object->getHP();
u16 dst_origin_hp = playersao->getHP();
- pointed_object->punch(dir, &toolcap, playersao,
+ u16 wear = pointed_object->punch(dir, &toolcap, playersao,
time_from_last_punch);
+ bool changed = punchitem.addWear(wear, m_itemdef);
+ if (changed)
+ playersao->setWieldedItem(punchitem);
+
// If the object is a player and its HP changed
if (src_original_hp != pointed_object->getHP() &&
pointed_object->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
lua_pushboolean(L, i.liquids_pointable);
lua_setfield(L, -2, "liquids_pointable");
if (i.type == ITEM_TOOL) {
- push_tool_capabilities(L, ToolCapabilities(
- i.tool_capabilities->full_punch_interval,
- i.tool_capabilities->max_drop_level,
- i.tool_capabilities->groupcaps,
- i.tool_capabilities->damageGroups));
+ push_tool_capabilities(L, *i.tool_capabilities);
lua_setfield(L, -2, "tool_capabilities");
}
push_groups(L, i.groups);
{
lua_newtable(L);
setfloatfield(L, -1, "full_punch_interval", toolcap.full_punch_interval);
- setintfield(L, -1, "max_drop_level", toolcap.max_drop_level);
+ setintfield(L, -1, "max_drop_level", toolcap.max_drop_level);
+ setintfield(L, -1, "punch_attack_uses", toolcap.punch_attack_uses);
// Create groupcaps table
lua_newtable(L);
// For each groupcap
ToolCapabilities toolcap;
getfloatfield(L, table, "full_punch_interval", toolcap.full_punch_interval);
getintfield(L, table, "max_drop_level", toolcap.max_drop_level);
+ getintfield(L, table, "punch_attack_uses", toolcap.punch_attack_uses);
lua_getfield(L, table, "groupcaps");
if(lua_istable(L, -1)){
int table_groupcaps = lua_gettop(L);
u16 dst_origin_hp = puncher->getHP();
// Do it
- co->punch(dir, &toolcap, puncher, time_from_last_punch);
+ u16 wear = co->punch(dir, &toolcap, puncher, time_from_last_punch);
+ lua_pushnumber(L, wear);
// If the punched is a player, and its HP changed
if (src_original_hp != co->getHP() &&
getServer(L)->SendPlayerHPOrDie((PlayerSAO *)puncher,
PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, co));
}
- return 0;
+ return 1;
}
// right_click(self, clicker); clicker = an another ObjectRef
{return true;}
// Returns tool wear
- virtual int punch(v3f dir,
- const ToolCapabilities *toolcap=NULL,
- ServerActiveObject *puncher=NULL,
- float time_from_last_punch=1000000)
+ virtual u16 punch(v3f dir,
+ const ToolCapabilities *toolcap = nullptr,
+ ServerActiveObject *puncher = nullptr,
+ float time_from_last_punch = 1000000.0f)
{ return 0; }
virtual void rightClick(ServerActiveObject *clicker)
{}
void ToolCapabilities::serialize(std::ostream &os, u16 protocol_version) const
{
- writeU8(os, 4); // protocol_version >= 37
+ if (protocol_version >= 38)
+ writeU8(os, 5);
+ else
+ writeU8(os, 4); // proto == 37
writeF32(os, full_punch_interval);
writeS16(os, max_drop_level);
writeU32(os, groupcaps.size());
os << serializeString(damageGroup.first);
writeS16(os, damageGroup.second);
}
+
+ if (protocol_version >= 38)
+ writeU16(os, rangelim(punch_attack_uses, 0, U16_MAX));
}
void ToolCapabilities::deSerialize(std::istream &is)
s16 rating = readS16(is);
damageGroups[name] = rating;
}
+
+ if (version >= 5)
+ punch_attack_uses = readU16(is);
}
void ToolCapabilities::serializeJson(std::ostream &os) const
Json::Value root;
root["full_punch_interval"] = full_punch_interval;
root["max_drop_level"] = max_drop_level;
+ root["punch_attack_uses"] = punch_attack_uses;
Json::Value groupcaps_object;
for (auto groupcap : groupcaps) {
full_punch_interval = root["full_punch_interval"].asFloat();
if (root["max_drop_level"].isInt())
max_drop_level = root["max_drop_level"].asInt();
+ if (root["punch_attack_uses"].isInt())
+ punch_attack_uses = root["punch_attack_uses"].asInt();
Json::Value &groupcaps_object = root["groupcaps"];
if (groupcaps_object.isObject()) {
const ToolCapabilities *tp, float time_from_last_punch)
{
s16 damage = 0;
- float full_punch_interval = tp->full_punch_interval;
+ float result_wear = 0.0f;
+ float punch_interval_multiplier =
+ rangelim(time_from_last_punch / tp->full_punch_interval, 0.0f, 1.0f);
for (const auto &damageGroup : tp->damageGroups) {
s16 armor = itemgroup_get(armor_groups, damageGroup.first);
- damage += damageGroup.second
- * rangelim(time_from_last_punch / full_punch_interval, 0.0, 1.0)
- * armor / 100.0;
+ damage += damageGroup.second * punch_interval_multiplier * armor / 100.0;
}
- return {damage, 0};
+ if (tp->punch_attack_uses > 0)
+ result_wear = 1.0f / tp->punch_attack_uses * punch_interval_multiplier;
+
+ u16 wear_i = U16_MAX * result_wear;
+ return {damage, wear_i};
}
HitParams getHitParams(const ItemGroupList &armor_groups,
int max_drop_level;
ToolGCMap groupcaps;
DamageGroup damageGroups;
+ int punch_attack_uses;
ToolCapabilities(
- float full_punch_interval_=1.4,
- int max_drop_level_=1,
+ float full_punch_interval_ = 1.4f,
+ int max_drop_level_ = 1,
const ToolGCMap &groupcaps_ = ToolGCMap(),
- const DamageGroup &damageGroups_ = DamageGroup()
+ const DamageGroup &damageGroups_ = DamageGroup(),
+ int punch_attack_uses_ = 0
):
full_punch_interval(full_punch_interval_),
max_drop_level(max_drop_level_),
groupcaps(groupcaps_),
- damageGroups(damageGroups_)
+ damageGroups(damageGroups_),
+ punch_attack_uses(punch_attack_uses_)
{}
void serialize(std::ostream &os, u16 version) const;
struct HitParams
{
s16 hp;
- s16 wear;
+ u16 wear;
- HitParams(s16 hp_=0, s16 wear_=0):
+ HitParams(s16 hp_ = 0, u16 wear_ = 0):
hp(hp_),
wear(wear_)
{}