Skip to content

Commit 5b3fbf9

Browse files
sofarsfan5
authored andcommittedSep 11, 2017
Implement client node dig prediction
Dig prediction allows clients to remove dug nodes without waiting for server acknowledgement. This patch allows mods to override dig prediction, it can either be turned off or a different "prediction node" can be selected.
1 parent d10ccce commit 5b3fbf9

File tree

5 files changed

+33
-1
lines changed

5 files changed

+33
-1
lines changed
 

‎doc/lua_api.txt

+7
Original file line numberDiff line numberDiff line change
@@ -4240,6 +4240,13 @@ Definition tables
42404240
on ground when the player places the item. Server will always update
42414241
actual result to client in a short moment.
42424242
]]
4243+
node_dig_prediction = "air",
4244+
--[[
4245+
^ if "", no prediction is made
4246+
^ if "air", node is removed
4247+
^ Otherwise should be name of node which the client immediately places
4248+
upon digging. Server will always update actual result shortly.
4249+
]]
42434250
sound = {
42444251
breaks = "default_tool_break", -- tools only
42454252
place = --[[<SimpleSoundSpec>]],

‎src/game.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -4104,7 +4104,17 @@ void Game::handleDigging(const PointedThing &pointed, const v3s16 &nodepos,
41044104
client->getScript()->on_dignode(nodepos, wasnode)) {
41054105
return;
41064106
}
4107-
client->removeNode(nodepos);
4107+
4108+
const ContentFeatures &f = client->ndef()->get(wasnode);
4109+
if (f.node_dig_prediction == "air") {
4110+
client->removeNode(nodepos);
4111+
} else if (!f.node_dig_prediction.empty()) {
4112+
content_t id;
4113+
bool found = client->ndef()->getId(f.node_dig_prediction, id);
4114+
if (found)
4115+
client->addNode(nodepos, id, true);
4116+
}
4117+
// implicit else: no prediction
41084118
}
41094119

41104120
client->interact(2, pointed);

‎src/nodedef.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ void ContentFeatures::reset()
335335
color = video::SColor(0xFFFFFFFF);
336336
palette_name = "";
337337
palette = NULL;
338+
node_dig_prediction = "air";
338339
}
339340

340341
void ContentFeatures::serialize(std::ostream &os, u16 protocol_version) const
@@ -422,6 +423,8 @@ void ContentFeatures::serialize(std::ostream &os, u16 protocol_version) const
422423
// legacy
423424
writeU8(os, legacy_facedir_simple);
424425
writeU8(os, legacy_wallmounted);
426+
427+
os << serializeString(node_dig_prediction);
425428
}
426429

427430
void ContentFeatures::correctAlpha(TileDef *tiles, int length)
@@ -530,6 +533,10 @@ void ContentFeatures::deSerialize(std::istream &is)
530533
// read legacy properties
531534
legacy_facedir_simple = readU8(is);
532535
legacy_wallmounted = readU8(is);
536+
537+
try {
538+
node_dig_prediction = deSerializeString(is);
539+
} catch(SerializationError &e) {};
533540
}
534541

535542
#ifndef SERVER

‎src/nodedef.h

+2
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ struct ContentFeatures
324324
// Player cannot build to these (placement prediction disabled)
325325
bool rightclickable;
326326
u32 damage_per_second;
327+
// client dig prediction
328+
std::string node_dig_prediction;
327329

328330
// --- LIQUID PROPERTIES ---
329331

‎src/script/common/c_content.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,10 @@ ContentFeatures read_content_features(lua_State *L, int index)
737737
}
738738
lua_pop(L, 1);
739739

740+
// Node immediately placed by client when node is dug
741+
getstringfield(L, index, "node_dig_prediction",
742+
f.node_dig_prediction);
743+
740744
return f;
741745
}
742746

@@ -861,6 +865,8 @@ void push_content_features(lua_State *L, const ContentFeatures &c)
861865
lua_setfield(L, -2, "legacy_facedir_simple");
862866
lua_pushboolean(L, c.legacy_wallmounted);
863867
lua_setfield(L, -2, "legacy_wallmounted");
868+
lua_pushstring(L, c.node_dig_prediction.c_str());
869+
lua_setfield(L, -2, "node_dig_prediction");
864870
}
865871

866872
/******************************************************************************/

0 commit comments

Comments
 (0)
Please sign in to comment.