From 6a0388bb4bedc1b1c6318d7bfebaf1ec67ccf94e Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Sun, 10 Jun 2012 12:46:48 +0300 Subject: Node placement client-side prediction --- src/game.cpp | 33 +++++++++++++++++++++++++++++++++ src/itemdef.cpp | 9 +++++++++ src/itemdef.h | 5 +++++ src/nodedef.cpp | 1 + src/scriptapi.cpp | 30 +++++++++++++++++++++++++----- src/server.cpp | 21 ++++++++++++++++++--- 6 files changed, 91 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/game.cpp b/src/game.cpp index 485bcbb50..6ccf02677 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -2366,8 +2366,41 @@ void the_game( // Otherwise report right click to server else { + // Report to server client.interact(3, pointed); camera.setDigging(1); // right click animation + + // If the wielded item has node placement prediction, + // make that happen + const ItemDefinition &def = + playeritem.getDefinition(itemdef); + if(def.node_placement_prediction != "") + do{ // breakable + verbosestream<<"Node placement prediction for " + <getId(def.node_placement_prediction, id); + if(!found){ + errorstream<<"Node placement prediction failed for " + <first); writeS16(os, i->second); } + os<deSerialize(wrapper_is); + verbosestream<<"deserialized "<name<name != "") addNameIdMapping(i, f->name); } diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp index 3766496a0..afd8546d9 100644 --- a/src/scriptapi.cpp +++ b/src/scriptapi.cpp @@ -930,13 +930,14 @@ static void read_object_properties(lua_State *L, int index, ItemDefinition */ -static ItemDefinition read_item_definition(lua_State *L, int index) +static ItemDefinition read_item_definition(lua_State *L, int index, + ItemDefinition default_def = ItemDefinition()) { if(index < 0) index = lua_gettop(L) + 1 + index; // Read the item definition - ItemDefinition def; + ItemDefinition def = default_def; def.type = (ItemType)getenumfield(L, index, "type", es_ItemType, ITEM_NONE); @@ -981,6 +982,12 @@ static ItemDefinition read_item_definition(lua_State *L, int index) read_groups(L, -1, def.groups); lua_pop(L, 1); + // Client shall immediately place this node when player places the item. + // Server will update the precise end result a moment later. + // "" = no prediction + getstringfield(L, index, "node_placement_prediction", + def.node_placement_prediction); + return def; } @@ -3891,9 +3898,10 @@ static int l_register_item_raw(lua_State *L) get_server(L)->getWritableNodeDefManager(); // Check if name is defined + std::string name; lua_getfield(L, table, "name"); if(lua_isstring(L, -1)){ - std::string name = lua_tostring(L, -1); + name = lua_tostring(L, -1); verbosestream<<"register_item_raw: "<registerItem(def); // Read the node definition (content features) and register it diff --git a/src/server.cpp b/src/server.cpp index a7bd5e953..98072e854 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -2971,8 +2971,16 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) <SetBlockNotSent(blockpos); + // Digging completed -> under + if(action == 2){ + v3s16 blockpos = getNodeBlockPos(floatToInt(pointed_pos_under, BS)); + client->SetBlockNotSent(blockpos); + } + // Placement -> above + if(action == 3){ + v3s16 blockpos = getNodeBlockPos(floatToInt(pointed_pos_above, BS)); + client->SetBlockNotSent(blockpos); + } return; } @@ -3104,7 +3112,14 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) if(g_settings->getBool("creative_mode") == false) playersao->setWieldedItem(item); } - + + // If item has node placement prediction, always send the above + // node to make sure the client knows what exactly happened + if(item.getDefinition(m_itemdef).node_placement_prediction != ""){ + RemoteClient *client = getClient(peer_id); + v3s16 blockpos = getNodeBlockPos(floatToInt(pointed_pos_above, BS)); + client->SetBlockNotSent(blockpos); + } } // action == 3 /* -- cgit v1.2.3