Skip to content

Commit

Permalink
Allow place_param2 = 0 node placement predictions
Browse files Browse the repository at this point in the history
The placement prediction value 0 was accidentally ignored
and made the clients fall back to automatic rotation based
on the node paramtype2 value.

This now changes the internal representation to s16 to properly
indicate the disabled state (e.g. 'nil' in Lua).
  • Loading branch information
SmallJoker committed Sep 9, 2023
1 parent 48ab183 commit 852a103
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/client/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3627,9 +3627,9 @@ bool Game::nodePlacement(const ItemDefinition &selected_def,
// Compare core.item_place_node() for what the server does with param2
MapNode predicted_node(id, 0, 0);

const u8 place_param2 = selected_def.place_param2;
const s16 place_param2 = selected_def.place_param2;

if (place_param2) {
if (place_param2 >= 0) {
predicted_node.setParam2(place_param2);
} else if (predicted_f.param_type_2 == CPT2_WALLMOUNTED ||
predicted_f.param_type_2 == CPT2_COLORED_WALLMOUNTED) {
Expand Down Expand Up @@ -3689,7 +3689,7 @@ bool Game::nodePlacement(const ItemDefinition &selected_def,
}

// Apply color
if (!place_param2 && (predicted_f.param_type_2 == CPT2_COLOR
if (place_param2 < 0 && (predicted_f.param_type_2 == CPT2_COLOR
|| predicted_f.param_type_2 == CPT2_COLORED_FACEDIR
|| predicted_f.param_type_2 == CPT2_COLORED_4DIR
|| predicted_f.param_type_2 == CPT2_COLORED_WALLMOUNTED)) {
Expand Down
6 changes: 4 additions & 2 deletions src/client/wieldmesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,8 @@ void WieldMeshSceneNode::setItem(const ItemStack &item, Client *client, bool che
default: {
// Render non-trivial drawtypes like the actual node
MapNode n(id);
n.setParam2(def.place_param2);
if (def.place_param2 >= 0)
n.setParam2(def.place_param2);

mesh = createSpecialNodeMesh(client, n, &m_colors, f);
changeToMesh(mesh);
Expand Down Expand Up @@ -638,7 +639,8 @@ void getItemMesh(Client *client, const ItemStack &item, ItemMesh *result)
default: {
// Render non-trivial drawtypes like the actual node
MapNode n(id);
n.setParam2(def.place_param2);
if (def.place_param2 >= 0)
n.setParam2(def.place_param2);

mesh = createSpecialNodeMesh(client, n, &result->buffer_colors, f);
scaleMesh(mesh, v3f(0.12, 0.12, 0.12));
Expand Down
22 changes: 19 additions & 3 deletions src/itemdef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ void ItemDefinition::reset()
sound_use_air = SoundSpec();
range = -1;
node_placement_prediction.clear();
place_param2 = 0;
place_param2 = -1;
}

void ItemDefinition::serialize(std::ostream &os, u16 protocol_version) const
Expand Down Expand Up @@ -169,10 +169,16 @@ void ItemDefinition::serialize(std::ostream &os, u16 protocol_version) const

os << serializeString16(short_description);

os << place_param2;
if (protocol_version <= 43) {
// Uncertainity whether 0 is the specified prediction or means disabled
const u8 place_param2_legacy = rangelim(place_param2, 0, U8_MAX);
os << place_param2_legacy;
}

sound_use.serializeSimple(os, protocol_version);
sound_use_air.serializeSimple(os, protocol_version);

os << place_param2; // protocol_version >= 43
}

void ItemDefinition::deSerialize(std::istream &is, u16 protocol_version)
Expand Down Expand Up @@ -226,10 +232,20 @@ void ItemDefinition::deSerialize(std::istream &is, u16 protocol_version)
try {
short_description = deSerializeString16(is);

place_param2 = readU8(is); // 0 if missing
if (protocol_version <= 43) {
place_param2 = readU8(is);
// assume disabled prediction
if (place_param2 == 0)
place_param2 = -1;
}

sound_use.deSerializeSimple(is, protocol_version);
sound_use_air.deSerializeSimple(is, protocol_version);

if (is.eof())
throw SerializationError("");

place_param2 = readS16(is); // protocol_version >= 43
} catch(SerializationError &e) {};
}

Expand Down
2 changes: 1 addition & 1 deletion src/itemdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ struct ItemDefinition
// Server will update the precise end result a moment later.
// "" = no prediction
std::string node_placement_prediction;
u8 place_param2;
s16 place_param2; // -1: not defined, >= 0: hardcoded prediction

/*
Some helpful methods
Expand Down
1 change: 1 addition & 0 deletions src/network/networkprotocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
[scheduled bump for 5.7.0]
PROTOCOL VERSION 43:
"start_time" added to TOCLIENT_PLAY_SOUND
place_param2 type change u8 -> s16
[scheduled bump for 5.8.0]
*/

Expand Down
3 changes: 2 additions & 1 deletion src/script/common/c_content.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ void read_item_definition(lua_State* L, int index,
getstringfield(L, index, "node_placement_prediction",
def.node_placement_prediction);

getintfield(L, index, "place_param2", def.place_param2);
int place_param2 = getintfield_default(L, index, "place_param2", -1);
def.place_param2 = rangelim(place_param2, -1, U8_MAX);
}

/******************************************************************************/
Expand Down

0 comments on commit 852a103

Please sign in to comment.