Skip to content

Commit 70f9e1a

Browse files
authoredSep 22, 2019
Punchwear (improved) (#8959)
1 parent fec30e3 commit 70f9e1a

File tree

10 files changed

+73
-31
lines changed

10 files changed

+73
-31
lines changed
 

‎builtin/game/register.lua

+12
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,18 @@ function core.register_tool(name, tooldef)
256256
end
257257
-- END Legacy stuff
258258

259+
-- This isn't just legacy, but more of a convenience feature
260+
local toolcaps = tooldef.tool_capabilities
261+
if toolcaps and toolcaps.punch_attack_uses == nil then
262+
for _, cap in pairs(toolcaps.groupcaps or {}) do
263+
local level = (cap.maxlevel or 0) - 1
264+
if (cap.uses or 0) ~= 0 and level >= 0 then
265+
toolcaps.punch_attack_uses = cap.uses * (3 ^ level)
266+
break
267+
end
268+
end
269+
end
270+
259271
core.register_item(name, tooldef)
260272
end
261273

‎doc/lua_api.txt

+9-1
Original file line numberDiff line numberDiff line change
@@ -6269,7 +6269,7 @@ Used by `minetest.register_node`, `minetest.register_craftitem`, and
62696269

62706270
liquids_pointable = false,
62716271

6272-
-- See "Tools" section
6272+
-- See "Tools" section for an example including explanation
62736273
tool_capabilities = {
62746274
full_punch_interval = 1.0,
62756275
max_drop_level = 0,
@@ -6279,6 +6279,14 @@ Used by `minetest.register_node`, `minetest.register_craftitem`, and
62796279
uses = 20, maxlevel = 2},
62806280
},
62816281
damage_groups = {groupname = damage},
6282+
6283+
punch_attack_uses = nil,
6284+
-- Amount of uses this tool has for attacking players and entities
6285+
-- by punching them (0 = infinite uses).
6286+
-- For compatibility, this is automatically set from the first
6287+
-- suitable groupcap using the forumla "uses * 3^(maxlevel - 1)".
6288+
-- It is recommend to set this explicitly instead of relying on the
6289+
-- fallback behavior.
62826290
},
62836291

62846292
node_placement_prediction = nil,

‎src/content_sao.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ void LuaEntitySAO::getStaticData(std::string *result) const
624624
*result = os.str();
625625
}
626626

627-
int LuaEntitySAO::punch(v3f dir,
627+
u16 LuaEntitySAO::punch(v3f dir,
628628
const ToolCapabilities *toolcap,
629629
ServerActiveObject *puncher,
630630
float time_from_last_punch)
@@ -1273,7 +1273,7 @@ void PlayerSAO::setLookPitchAndSend(const float pitch)
12731273
m_env->getGameDef()->SendMovePlayer(m_peer_id);
12741274
}
12751275

1276-
int PlayerSAO::punch(v3f dir,
1276+
u16 PlayerSAO::punch(v3f dir,
12771277
const ToolCapabilities *toolcap,
12781278
ServerActiveObject *puncher,
12791279
float time_from_last_punch)

‎src/content_sao.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ class LuaEntitySAO : public UnitSAO
122122
bool isStaticAllowed() const
123123
{ return m_prop.static_save; }
124124
void getStaticData(std::string *result) const;
125-
int punch(v3f dir,
125+
u16 punch(v3f dir,
126126
const ToolCapabilities *toolcap = nullptr,
127127
ServerActiveObject *puncher = nullptr,
128-
float time_from_last_punch = 1000000);
128+
float time_from_last_punch = 1000000.0f);
129129
void rightClick(ServerActiveObject *clicker);
130130
void setPos(const v3f &pos);
131131
void moveTo(v3f pos, bool continuous);
@@ -258,7 +258,7 @@ class PlayerSAO : public UnitSAO
258258
Interaction interface
259259
*/
260260

261-
int punch(v3f dir,
261+
u16 punch(v3f dir,
262262
const ToolCapabilities *toolcap,
263263
ServerActiveObject *puncher,
264264
float time_from_last_punch);

‎src/network/serverpackethandler.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -1163,9 +1163,13 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
11631163
u16 src_original_hp = pointed_object->getHP();
11641164
u16 dst_origin_hp = playersao->getHP();
11651165

1166-
pointed_object->punch(dir, &toolcap, playersao,
1166+
u16 wear = pointed_object->punch(dir, &toolcap, playersao,
11671167
time_from_last_punch);
11681168

1169+
bool changed = punchitem.addWear(wear, m_itemdef);
1170+
if (changed)
1171+
playersao->setWieldedItem(punchitem);
1172+
11691173
// If the object is a player and its HP changed
11701174
if (src_original_hp != pointed_object->getHP() &&
11711175
pointed_object->getType() == ACTIVEOBJECT_TYPE_PLAYER) {

‎src/script/common/c_content.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,7 @@ void push_item_definition_full(lua_State *L, const ItemDefinition &i)
164164
lua_pushboolean(L, i.liquids_pointable);
165165
lua_setfield(L, -2, "liquids_pointable");
166166
if (i.type == ITEM_TOOL) {
167-
push_tool_capabilities(L, ToolCapabilities(
168-
i.tool_capabilities->full_punch_interval,
169-
i.tool_capabilities->max_drop_level,
170-
i.tool_capabilities->groupcaps,
171-
i.tool_capabilities->damageGroups));
167+
push_tool_capabilities(L, *i.tool_capabilities);
172168
lua_setfield(L, -2, "tool_capabilities");
173169
}
174170
push_groups(L, i.groups);
@@ -1253,7 +1249,8 @@ void push_tool_capabilities(lua_State *L,
12531249
{
12541250
lua_newtable(L);
12551251
setfloatfield(L, -1, "full_punch_interval", toolcap.full_punch_interval);
1256-
setintfield(L, -1, "max_drop_level", toolcap.max_drop_level);
1252+
setintfield(L, -1, "max_drop_level", toolcap.max_drop_level);
1253+
setintfield(L, -1, "punch_attack_uses", toolcap.punch_attack_uses);
12571254
// Create groupcaps table
12581255
lua_newtable(L);
12591256
// For each groupcap
@@ -1375,6 +1372,7 @@ ToolCapabilities read_tool_capabilities(
13751372
ToolCapabilities toolcap;
13761373
getfloatfield(L, table, "full_punch_interval", toolcap.full_punch_interval);
13771374
getintfield(L, table, "max_drop_level", toolcap.max_drop_level);
1375+
getintfield(L, table, "punch_attack_uses", toolcap.punch_attack_uses);
13781376
lua_getfield(L, table, "groupcaps");
13791377
if(lua_istable(L, -1)){
13801378
int table_groupcaps = lua_gettop(L);

‎src/script/lua_api/l_object.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ int ObjectRef::l_punch(lua_State *L)
187187
u16 dst_origin_hp = puncher->getHP();
188188

189189
// Do it
190-
co->punch(dir, &toolcap, puncher, time_from_last_punch);
190+
u16 wear = co->punch(dir, &toolcap, puncher, time_from_last_punch);
191+
lua_pushnumber(L, wear);
191192

192193
// If the punched is a player, and its HP changed
193194
if (src_original_hp != co->getHP() &&
@@ -202,7 +203,7 @@ int ObjectRef::l_punch(lua_State *L)
202203
getServer(L)->SendPlayerHPOrDie((PlayerSAO *)puncher,
203204
PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, co));
204205
}
205-
return 0;
206+
return 1;
206207
}
207208

208209
// right_click(self, clicker); clicker = an another ObjectRef

‎src/serverobject.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,10 @@ class ServerActiveObject : public ActiveObject
133133
{return true;}
134134

135135
// Returns tool wear
136-
virtual int punch(v3f dir,
137-
const ToolCapabilities *toolcap=NULL,
138-
ServerActiveObject *puncher=NULL,
139-
float time_from_last_punch=1000000)
136+
virtual u16 punch(v3f dir,
137+
const ToolCapabilities *toolcap = nullptr,
138+
ServerActiveObject *puncher = nullptr,
139+
float time_from_last_punch = 1000000.0f)
140140
{ return 0; }
141141
virtual void rightClick(ServerActiveObject *clicker)
142142
{}

‎src/tool.cpp

+22-6
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ void ToolGroupCap::fromJson(const Json::Value &json)
5656

5757
void ToolCapabilities::serialize(std::ostream &os, u16 protocol_version) const
5858
{
59-
writeU8(os, 4); // protocol_version >= 37
59+
if (protocol_version >= 38)
60+
writeU8(os, 5);
61+
else
62+
writeU8(os, 4); // proto == 37
6063
writeF32(os, full_punch_interval);
6164
writeS16(os, max_drop_level);
6265
writeU32(os, groupcaps.size());
@@ -79,6 +82,9 @@ void ToolCapabilities::serialize(std::ostream &os, u16 protocol_version) const
7982
os << serializeString(damageGroup.first);
8083
writeS16(os, damageGroup.second);
8184
}
85+
86+
if (protocol_version >= 38)
87+
writeU16(os, rangelim(punch_attack_uses, 0, U16_MAX));
8288
}
8389

8490
void ToolCapabilities::deSerialize(std::istream &is)
@@ -111,13 +117,17 @@ void ToolCapabilities::deSerialize(std::istream &is)
111117
s16 rating = readS16(is);
112118
damageGroups[name] = rating;
113119
}
120+
121+
if (version >= 5)
122+
punch_attack_uses = readU16(is);
114123
}
115124

116125
void ToolCapabilities::serializeJson(std::ostream &os) const
117126
{
118127
Json::Value root;
119128
root["full_punch_interval"] = full_punch_interval;
120129
root["max_drop_level"] = max_drop_level;
130+
root["punch_attack_uses"] = punch_attack_uses;
121131

122132
Json::Value groupcaps_object;
123133
for (auto groupcap : groupcaps) {
@@ -144,6 +154,8 @@ void ToolCapabilities::deserializeJson(std::istream &is)
144154
full_punch_interval = root["full_punch_interval"].asFloat();
145155
if (root["max_drop_level"].isInt())
146156
max_drop_level = root["max_drop_level"].asInt();
157+
if (root["punch_attack_uses"].isInt())
158+
punch_attack_uses = root["punch_attack_uses"].asInt();
147159

148160
Json::Value &groupcaps_object = root["groupcaps"];
149161
if (groupcaps_object.isObject()) {
@@ -227,16 +239,20 @@ HitParams getHitParams(const ItemGroupList &armor_groups,
227239
const ToolCapabilities *tp, float time_from_last_punch)
228240
{
229241
s16 damage = 0;
230-
float full_punch_interval = tp->full_punch_interval;
242+
float result_wear = 0.0f;
243+
float punch_interval_multiplier =
244+
rangelim(time_from_last_punch / tp->full_punch_interval, 0.0f, 1.0f);
231245

232246
for (const auto &damageGroup : tp->damageGroups) {
233247
s16 armor = itemgroup_get(armor_groups, damageGroup.first);
234-
damage += damageGroup.second
235-
* rangelim(time_from_last_punch / full_punch_interval, 0.0, 1.0)
236-
* armor / 100.0;
248+
damage += damageGroup.second * punch_interval_multiplier * armor / 100.0;
237249
}
238250

239-
return {damage, 0};
251+
if (tp->punch_attack_uses > 0)
252+
result_wear = 1.0f / tp->punch_attack_uses * punch_interval_multiplier;
253+
254+
u16 wear_i = U16_MAX * result_wear;
255+
return {damage, wear_i};
240256
}
241257

242258
HitParams getHitParams(const ItemGroupList &armor_groups,

‎src/tool.h

+9-6
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,20 @@ struct ToolCapabilities
6060
int max_drop_level;
6161
ToolGCMap groupcaps;
6262
DamageGroup damageGroups;
63+
int punch_attack_uses;
6364

6465
ToolCapabilities(
65-
float full_punch_interval_=1.4,
66-
int max_drop_level_=1,
66+
float full_punch_interval_ = 1.4f,
67+
int max_drop_level_ = 1,
6768
const ToolGCMap &groupcaps_ = ToolGCMap(),
68-
const DamageGroup &damageGroups_ = DamageGroup()
69+
const DamageGroup &damageGroups_ = DamageGroup(),
70+
int punch_attack_uses_ = 0
6971
):
7072
full_punch_interval(full_punch_interval_),
7173
max_drop_level(max_drop_level_),
7274
groupcaps(groupcaps_),
73-
damageGroups(damageGroups_)
75+
damageGroups(damageGroups_),
76+
punch_attack_uses(punch_attack_uses_)
7477
{}
7578

7679
void serialize(std::ostream &os, u16 version) const;
@@ -103,9 +106,9 @@ DigParams getDigParams(const ItemGroupList &groups,
103106
struct HitParams
104107
{
105108
s16 hp;
106-
s16 wear;
109+
u16 wear;
107110

108-
HitParams(s16 hp_=0, s16 wear_=0):
111+
HitParams(s16 hp_ = 0, u16 wear_ = 0):
109112
hp(hp_),
110113
wear(wear_)
111114
{}

0 commit comments

Comments
 (0)
Please sign in to comment.