Trigger on_place in many situations even if prediction failed

This commit is contained in:
DS-Minetest 2019-06-29 17:06:58 +02:00 committed by sfan5
parent c413eeb026
commit 0b5c5499ec

@ -801,8 +801,8 @@ private:
void updateChat(f32 dtime, const v2u32 &screensize); void updateChat(f32 dtime, const v2u32 &screensize);
bool nodePlacementPrediction(const ItemDefinition &selected_def, bool nodePlacement(const ItemDefinition &selected_def, const ItemStack &selected_item,
const ItemStack &selected_item, const v3s16 &nodepos, const v3s16 &neighbourpos); const v3s16 &nodepos, const v3s16 &neighbourpos, const PointedThing &pointed);
static const ClientEventHandler clientEventHandler[CLIENTEVENT_MAX]; static const ClientEventHandler clientEventHandler[CLIENTEVENT_MAX];
InputHandler *input = nullptr; InputHandler *input = nullptr;
@ -3225,39 +3225,24 @@ void Game::handlePointingAtNode(const PointedThing &pointed,
camera->setDigging(1); // right click animation (always shown for feedback) camera->setDigging(1); // right click animation (always shown for feedback)
soundmaker->m_player_rightpunch_sound = SimpleSoundSpec();
// If the wielded item has node placement prediction, // If the wielded item has node placement prediction,
// make that happen // make that happen
// And also set the sound and send the interact
auto &def = selected_item.getDefinition(itemdef_manager); auto &def = selected_item.getDefinition(itemdef_manager);
bool placed = nodePlacementPrediction(def, selected_item, nodepos, bool placed = nodePlacement(def, selected_item, nodepos, neighbourpos,
neighbourpos); pointed);
if (placed) { if (placed && client->modsLoaded())
// Report to server
client->interact(INTERACT_PLACE, pointed);
// Read the sound
soundmaker->m_player_rightpunch_sound =
def.sound_place;
if (client->modsLoaded())
client->getScript()->on_placenode(pointed, def); client->getScript()->on_placenode(pointed, def);
} else {
soundmaker->m_player_rightpunch_sound =
SimpleSoundSpec();
if (def.node_placement_prediction.empty() ||
nodedef_manager->get(map.getNode(nodepos)).rightclickable) {
client->interact(INTERACT_PLACE, pointed); // Report to server
} else {
soundmaker->m_player_rightpunch_sound =
def.sound_place_failed;
}
}
} }
} }
} }
bool Game::nodePlacementPrediction(const ItemDefinition &selected_def, bool Game::nodePlacement(const ItemDefinition &selected_def,
const ItemStack &selected_item, const v3s16 &nodepos, const v3s16 &neighbourpos) const ItemStack &selected_item, const v3s16 &nodepos, const v3s16 &neighbourpos,
const PointedThing &pointed)
{ {
std::string prediction = selected_def.node_placement_prediction; std::string prediction = selected_def.node_placement_prediction;
const NodeDefManager *nodedef = client->ndef(); const NodeDefManager *nodedef = client->ndef();
@ -3266,28 +3251,37 @@ bool Game::nodePlacementPrediction(const ItemDefinition &selected_def,
bool is_valid_position; bool is_valid_position;
node = map.getNode(nodepos, &is_valid_position); node = map.getNode(nodepos, &is_valid_position);
if (!is_valid_position) if (!is_valid_position) {
soundmaker->m_player_rightpunch_sound = selected_def.sound_place_failed;
return false; return false;
}
if (!prediction.empty() && !(nodedef->get(node).rightclickable && if (prediction.empty() || (nodedef->get(node).rightclickable &&
!isKeyDown(KeyType::SNEAK))) { !isKeyDown(KeyType::SNEAK))) {
// Report to server
client->interact(INTERACT_PLACE, pointed);
return false;
}
verbosestream << "Node placement prediction for " verbosestream << "Node placement prediction for "
<< selected_item.name << " is " << selected_def.name << " is "
<< prediction << std::endl; << prediction << std::endl;
v3s16 p = neighbourpos; v3s16 p = neighbourpos;
// Place inside node itself if buildable_to // Place inside node itself if buildable_to
MapNode n_under = map.getNode(nodepos, &is_valid_position); MapNode n_under = map.getNode(nodepos, &is_valid_position);
if (is_valid_position) if (is_valid_position) {
{ if (nodedef->get(n_under).buildable_to) {
if (nodedef->get(n_under).buildable_to)
p = nodepos; p = nodepos;
else { } else {
node = map.getNode(p, &is_valid_position); node = map.getNode(p, &is_valid_position);
if (is_valid_position &&!nodedef->get(node).buildable_to) if (is_valid_position && !nodedef->get(node).buildable_to) {
// Report to server
client->interact(INTERACT_PLACE, pointed);
return false; return false;
} }
} }
}
// Find id of predicted node // Find id of predicted node
content_t id; content_t id;
@ -3295,9 +3289,12 @@ bool Game::nodePlacementPrediction(const ItemDefinition &selected_def,
if (!found) { if (!found) {
errorstream << "Node placement prediction failed for " errorstream << "Node placement prediction failed for "
<< selected_item.name << " (places " << selected_def.name << " (places "
<< prediction << prediction
<< ") - Name not known" << std::endl; << ") - Name not known" << std::endl;
// Handle this as if prediction was empty
// Report to server
client->interact(INTERACT_PLACE, pointed);
return false; return false;
} }
@ -3350,9 +3347,12 @@ bool Game::nodePlacementPrediction(const ItemDefinition &selected_def,
else else
pp = p + v3s16(0, -1, 0); pp = p + v3s16(0, -1, 0);
if (!nodedef->get(map.getNode(pp)).walkable) if (!nodedef->get(map.getNode(pp)).walkable) {
// Report to server
client->interact(INTERACT_PLACE, pointed);
return false; return false;
} }
}
// Apply color // Apply color
if ((predicted_f.param_type_2 == CPT2_COLOR if ((predicted_f.param_type_2 == CPT2_COLOR
@ -3364,12 +3364,10 @@ bool Game::nodePlacementPrediction(const ItemDefinition &selected_def,
s32 index = mystoi(indexstr); s32 index = mystoi(indexstr);
if (predicted_f.param_type_2 == CPT2_COLOR) { if (predicted_f.param_type_2 == CPT2_COLOR) {
param2 = index; param2 = index;
} else if (predicted_f.param_type_2 } else if (predicted_f.param_type_2 == CPT2_COLORED_WALLMOUNTED) {
== CPT2_COLORED_WALLMOUNTED) {
// param2 = pure palette index + other // param2 = pure palette index + other
param2 = (index & 0xf8) | (param2 & 0x07); param2 = (index & 0xf8) | (param2 & 0x07);
} else if (predicted_f.param_type_2 } else if (predicted_f.param_type_2 == CPT2_COLORED_FACEDIR) {
== CPT2_COLORED_FACEDIR) {
// param2 = pure palette index + other // param2 = pure palette index + other
param2 = (index & 0xe0) | (param2 & 0x1f); param2 = (index & 0xe0) | (param2 & 0x1f);
} }
@ -3390,21 +3388,26 @@ bool Game::nodePlacementPrediction(const ItemDefinition &selected_def,
(nodedef->get(n).walkable && (nodedef->get(n).walkable &&
neighbourpos != player->getStandingNodePos() + v3s16(0, 1, 0) && neighbourpos != player->getStandingNodePos() + v3s16(0, 1, 0) &&
neighbourpos != player->getStandingNodePos() + v3s16(0, 2, 0))) { neighbourpos != player->getStandingNodePos() + v3s16(0, 2, 0))) {
// This triggers the required mesh update too // This triggers the required mesh update too
client->addNode(p, n); client->addNode(p, n);
// Report to server
client->interact(INTERACT_PLACE, pointed);
// A node is predicted, also play a sound
soundmaker->m_player_rightpunch_sound = selected_def.sound_place;
return true; return true;
} else {
soundmaker->m_player_rightpunch_sound = selected_def.sound_place_failed;
return false;
} }
} catch (InvalidPositionException &e) { } catch (InvalidPositionException &e) {
errorstream << "Node placement prediction failed for " errorstream << "Node placement prediction failed for "
<< selected_item.name << " (places " << selected_def.name << " (places "
<< prediction << prediction
<< ") - Position not loaded" << std::endl; << ") - Position not loaded" << std::endl;
} soundmaker->m_player_rightpunch_sound = selected_def.sound_place_failed;
}
return false; return false;
} }
}
void Game::handlePointingAtObject(const PointedThing &pointed, void Game::handlePointingAtObject(const PointedThing &pointed,
const ItemStack &tool_item, const v3f &player_position, bool show_debug) const ItemStack &tool_item, const v3f &player_position, bool show_debug)