Skip to content

Commit bf8fb26

Browse files
authoredMar 8, 2021
Use place_param2 client-side for item appearance & prediction (#11024)
1 parent a21402b commit bf8fb26

File tree

5 files changed

+51
-35
lines changed

5 files changed

+51
-35
lines changed
 

Diff for: ‎src/client/game.cpp

+18-18
Original file line numberDiff line numberDiff line change
@@ -3287,7 +3287,8 @@ bool Game::nodePlacement(const ItemDefinition &selected_def,
32873287
const ItemStack &selected_item, const v3s16 &nodepos, const v3s16 &neighbourpos,
32883288
const PointedThing &pointed, const NodeMetadata *meta)
32893289
{
3290-
std::string prediction = selected_def.node_placement_prediction;
3290+
const auto &prediction = selected_def.node_placement_prediction;
3291+
32913292
const NodeDefManager *nodedef = client->ndef();
32923293
ClientMap &map = client->getEnv().getClientMap();
32933294
MapNode node;
@@ -3357,8 +3358,7 @@ bool Game::nodePlacement(const ItemDefinition &selected_def,
33573358

33583359
if (!found) {
33593360
errorstream << "Node placement prediction failed for "
3360-
<< selected_def.name << " (places "
3361-
<< prediction
3361+
<< selected_def.name << " (places " << prediction
33623362
<< ") - Name not known" << std::endl;
33633363
// Handle this as if prediction was empty
33643364
// Report to server
@@ -3369,9 +3369,14 @@ bool Game::nodePlacement(const ItemDefinition &selected_def,
33693369
const ContentFeatures &predicted_f = nodedef->get(id);
33703370

33713371
// Predict param2 for facedir and wallmounted nodes
3372+
// Compare core.item_place_node() for what the server does
33723373
u8 param2 = 0;
33733374

3374-
if (predicted_f.param_type_2 == CPT2_WALLMOUNTED ||
3375+
const u8 place_param2 = selected_def.place_param2;
3376+
3377+
if (place_param2) {
3378+
param2 = place_param2;
3379+
} else if (predicted_f.param_type_2 == CPT2_WALLMOUNTED ||
33753380
predicted_f.param_type_2 == CPT2_COLORED_WALLMOUNTED) {
33763381
v3s16 dir = nodepos - neighbourpos;
33773382

@@ -3382,9 +3387,7 @@ bool Game::nodePlacement(const ItemDefinition &selected_def,
33823387
} else {
33833388
param2 = dir.Z < 0 ? 5 : 4;
33843389
}
3385-
}
3386-
3387-
if (predicted_f.param_type_2 == CPT2_FACEDIR ||
3390+
} else if (predicted_f.param_type_2 == CPT2_FACEDIR ||
33883391
predicted_f.param_type_2 == CPT2_COLORED_FACEDIR) {
33893392
v3s16 dir = nodepos - floatToInt(client->getEnv().getLocalPlayer()->getPosition(), BS);
33903393

@@ -3395,11 +3398,9 @@ bool Game::nodePlacement(const ItemDefinition &selected_def,
33953398
}
33963399
}
33973400

3398-
assert(param2 <= 5);
3399-
3400-
//Check attachment if node is in group attached_node
3401-
if (((ItemGroupList) predicted_f.groups)["attached_node"] != 0) {
3402-
static v3s16 wallmounted_dirs[8] = {
3401+
// Check attachment if node is in group attached_node
3402+
if (itemgroup_get(predicted_f.groups, "attached_node") != 0) {
3403+
const static v3s16 wallmounted_dirs[8] = {
34033404
v3s16(0, 1, 0),
34043405
v3s16(0, -1, 0),
34053406
v3s16(1, 0, 0),
@@ -3424,11 +3425,11 @@ bool Game::nodePlacement(const ItemDefinition &selected_def,
34243425
}
34253426

34263427
// Apply color
3427-
if ((predicted_f.param_type_2 == CPT2_COLOR
3428+
if (!place_param2 && (predicted_f.param_type_2 == CPT2_COLOR
34283429
|| predicted_f.param_type_2 == CPT2_COLORED_FACEDIR
34293430
|| predicted_f.param_type_2 == CPT2_COLORED_WALLMOUNTED)) {
3430-
const std::string &indexstr = selected_item.metadata.getString(
3431-
"palette_index", 0);
3431+
const auto &indexstr = selected_item.metadata.
3432+
getString("palette_index", 0);
34323433
if (!indexstr.empty()) {
34333434
s32 index = mystoi(indexstr);
34343435
if (predicted_f.param_type_2 == CPT2_COLOR) {
@@ -3468,11 +3469,10 @@ bool Game::nodePlacement(const ItemDefinition &selected_def,
34683469
soundmaker->m_player_rightpunch_sound = selected_def.sound_place_failed;
34693470
return false;
34703471
}
3471-
} catch (InvalidPositionException &e) {
3472+
} catch (const InvalidPositionException &e) {
34723473
errorstream << "Node placement prediction failed for "
34733474
<< selected_def.name << " (places "
3474-
<< prediction
3475-
<< ") - Position not loaded" << std::endl;
3475+
<< prediction << ") - Position not loaded" << std::endl;
34763476
soundmaker->m_player_rightpunch_sound = selected_def.sound_place_failed;
34773477
return false;
34783478
}

Diff for: ‎src/client/wieldmesh.cpp

+22-11
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ void WieldMeshSceneNode::setExtruded(const std::string &imagename,
294294
}
295295
material.setFlag(video::EMF_ANISOTROPIC_FILTER, m_anisotropic_filter);
296296
// mipmaps cause "thin black line" artifacts
297-
#if (IRRLICHT_VERSION_MAJOR >= 1 && IRRLICHT_VERSION_MINOR >= 8) || IRRLICHT_VERSION_MAJOR >= 2
297+
#if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR >= 8) || IRRLICHT_VERSION_MAJOR >= 2
298298
material.setFlag(video::EMF_USE_MIP_MAPS, false);
299299
#endif
300300
if (m_enable_shaders) {
@@ -303,23 +303,26 @@ void WieldMeshSceneNode::setExtruded(const std::string &imagename,
303303
}
304304
}
305305

306-
scene::SMesh *createSpecialNodeMesh(Client *client, content_t id, std::vector<ItemPartColor> *colors, const ContentFeatures &f)
306+
static scene::SMesh *createSpecialNodeMesh(Client *client, MapNode n,
307+
std::vector<ItemPartColor> *colors, const ContentFeatures &f)
307308
{
308309
MeshMakeData mesh_make_data(client, false);
309310
MeshCollector collector;
310311
mesh_make_data.setSmoothLighting(false);
311312
MapblockMeshGenerator gen(&mesh_make_data, &collector);
312-
u8 param2 = 0;
313-
if (f.param_type_2 == CPT2_WALLMOUNTED ||
313+
314+
if (n.getParam2()) {
315+
// keep it
316+
} else if (f.param_type_2 == CPT2_WALLMOUNTED ||
314317
f.param_type_2 == CPT2_COLORED_WALLMOUNTED) {
315318
if (f.drawtype == NDT_TORCHLIKE)
316-
param2 = 1;
319+
n.setParam2(1);
317320
else if (f.drawtype == NDT_SIGNLIKE ||
318321
f.drawtype == NDT_NODEBOX ||
319322
f.drawtype == NDT_MESH)
320-
param2 = 4;
323+
n.setParam2(4);
321324
}
322-
gen.renderSingle(id, param2);
325+
gen.renderSingle(n.getContent(), n.getParam2());
323326

324327
colors->clear();
325328
scene::SMesh *mesh = new scene::SMesh();
@@ -413,16 +416,20 @@ void WieldMeshSceneNode::setItem(const ItemStack &item, Client *client, bool che
413416
case NDT_LIQUID:
414417
setCube(f, def.wield_scale);
415418
break;
416-
default:
419+
default: {
417420
// Render non-trivial drawtypes like the actual node
418-
mesh = createSpecialNodeMesh(client, id, &m_colors, f);
421+
MapNode n(id);
422+
n.setParam2(def.place_param2);
423+
424+
mesh = createSpecialNodeMesh(client, n, &m_colors, f);
419425
changeToMesh(mesh);
420426
mesh->drop();
421427
m_meshnode->setScale(
422428
def.wield_scale * WIELD_SCALE_FACTOR
423429
/ (BS * f.visual_scale));
424430
break;
425431
}
432+
}
426433

427434
u32 material_count = m_meshnode->getMaterialCount();
428435
for (u32 i = 0; i < material_count; ++i) {
@@ -585,12 +592,16 @@ void getItemMesh(Client *client, const ItemStack &item, ItemMesh *result)
585592
result->buffer_colors.emplace_back(l0.has_color, l0.color);
586593
break;
587594
}
588-
default:
595+
default: {
589596
// Render non-trivial drawtypes like the actual node
590-
mesh = createSpecialNodeMesh(client, id, &result->buffer_colors, f);
597+
MapNode n(id);
598+
n.setParam2(def.place_param2);
599+
600+
mesh = createSpecialNodeMesh(client, n, &result->buffer_colors, f);
591601
scaleMesh(mesh, v3f(0.12, 0.12, 0.12));
592602
break;
593603
}
604+
}
594605

595606
u32 mc = mesh->getMeshBufferCount();
596607
for (u32 i = 0; i < mc; ++i) {

Diff for: ‎src/itemdef.cpp

+8-6
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,11 @@ ItemDefinition& ItemDefinition::operator=(const ItemDefinition &def)
7171
stack_max = def.stack_max;
7272
usable = def.usable;
7373
liquids_pointable = def.liquids_pointable;
74-
if(def.tool_capabilities)
75-
{
76-
tool_capabilities = new ToolCapabilities(
77-
*def.tool_capabilities);
78-
}
74+
if (def.tool_capabilities)
75+
tool_capabilities = new ToolCapabilities(*def.tool_capabilities);
7976
groups = def.groups;
8077
node_placement_prediction = def.node_placement_prediction;
78+
place_param2 = def.place_param2;
8179
sound_place = def.sound_place;
8280
sound_place_failed = def.sound_place_failed;
8381
range = def.range;
@@ -120,8 +118,8 @@ void ItemDefinition::reset()
120118
sound_place = SimpleSoundSpec();
121119
sound_place_failed = SimpleSoundSpec();
122120
range = -1;
123-
124121
node_placement_prediction = "";
122+
place_param2 = 0;
125123
}
126124

127125
void ItemDefinition::serialize(std::ostream &os, u16 protocol_version) const
@@ -166,6 +164,8 @@ void ItemDefinition::serialize(std::ostream &os, u16 protocol_version) const
166164
os << serializeString16(wield_overlay);
167165

168166
os << serializeString16(short_description);
167+
168+
os << place_param2;
169169
}
170170

171171
void ItemDefinition::deSerialize(std::istream &is)
@@ -219,6 +219,8 @@ void ItemDefinition::deSerialize(std::istream &is)
219219
// block to not need to increase the version.
220220
try {
221221
short_description = deSerializeString16(is);
222+
223+
place_param2 = readU8(is); // 0 if missing
222224
} catch(SerializationError &e) {};
223225
}
224226

Diff for: ‎src/itemdef.h

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ struct ItemDefinition
8686
// Server will update the precise end result a moment later.
8787
// "" = no prediction
8888
std::string node_placement_prediction;
89+
u8 place_param2;
8990

9091
/*
9192
Some helpful methods

Diff for: ‎src/script/common/c_content.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ void read_item_definition(lua_State* L, int index,
119119
// "" = no prediction
120120
getstringfield(L, index, "node_placement_prediction",
121121
def.node_placement_prediction);
122+
123+
getintfield(L, index, "place_param2", def.place_param2);
122124
}
123125

124126
/******************************************************************************/

0 commit comments

Comments
 (0)
Please sign in to comment.