From fa736e138c2eb04f50bcc0431fc5a0435bf34bc6 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Mon, 4 Apr 2011 05:12:33 +0300 Subject: [PATCH] fully implemented the sign with the new framework --- src/client.cpp | 34 +++++++++++++++++++++++++++++ src/client.h | 1 + src/clientserver.h | 8 +++++++ src/main.cpp | 47 +++++++++++++++++++++++++++++++++++++++- src/mapblock.cpp | 19 ++++++++++++++--- src/mapnode.cpp | 2 +- src/serialization.h | 3 ++- src/server.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++-- 8 files changed, 158 insertions(+), 8 deletions(-) diff --git a/src/client.cpp b/src/client.cpp index 009591c4..2fb14cb6 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -1630,6 +1630,40 @@ void Client::sendSignText(v3s16 blockpos, s16 id, std::string text) Send(0, data, true); } +void Client::sendSignNodeText(v3s16 p, std::string text) +{ + /* + u16 command + v3s16 p + u16 textlen + textdata + */ + std::ostringstream os(std::ios_base::binary); + u8 buf[12]; + + // Write command + writeU16(buf, TOSERVER_SIGNNODETEXT); + os.write((char*)buf, 2); + + // Write p + writeV3S16(buf, p); + os.write((char*)buf, 6); + + u16 textlen = text.size(); + // Write text length + writeS16(buf, textlen); + os.write((char*)buf, 2); + + // Write text + os.write((char*)text.c_str(), textlen); + + // Make data buffer + std::string s = os.str(); + SharedBuffer data((u8*)s.c_str(), s.size()); + // Send as reliable + Send(0, data, true); +} + void Client::sendInventoryAction(InventoryAction *a) { std::ostringstream os(std::ios_base::binary); diff --git a/src/client.h b/src/client.h index 39152828..fdb98d28 100644 --- a/src/client.h +++ b/src/client.h @@ -277,6 +277,7 @@ public: void clickObject(u8 button, v3s16 blockpos, s16 id, u16 item); void sendSignText(v3s16 blockpos, s16 id, std::string text); + void sendSignNodeText(v3s16 p, std::string text); void sendInventoryAction(InventoryAction *a); void sendChatMessage(const std::wstring &message); diff --git a/src/clientserver.h b/src/clientserver.h index 52b4e520..7baa79fa 100644 --- a/src/clientserver.h +++ b/src/clientserver.h @@ -241,6 +241,14 @@ enum ToServerCommand wstring message */ + TOSERVER_SIGNNODETEXT = 0x33, + /* + u16 command + v3s16 p + u16 textlen + textdata + */ + }; inline SharedBuffer makePacket_TOCLIENT_TIME_OF_DAY(u16 time) diff --git a/src/main.cpp b/src/main.cpp index 4b968865..c9099f69 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -578,6 +578,25 @@ struct TextDestChat : public TextDest Client *m_client; }; +struct TextDestSignNode : public TextDest +{ + TextDestSignNode(v3s16 p, Client *client) + { + m_p = p; + m_client = client; + } + void gotText(std::wstring text) + { + std::string ntext = wide_to_narrow(text); + dstream<<"Changing text of a sign node: " + <sendSignNodeText(m_p, ntext); + } + + v3s16 m_p; + Client *m_client; +}; + class MyEventReceiver : public IEventReceiver { public: @@ -2829,6 +2848,8 @@ int main(int argc, char *argv[]) { infotext = narrow_to_wide(meta->infoText()); } + + //MapNode node = client.getNode(nodepos); /* Handle digging @@ -2956,7 +2977,31 @@ int main(int argc, char *argv[]) if(g_input->getRightClicked()) { std::cout<typeId() == CONTENT_SIGN_WALL) + { + dstream<<"Sign node right-clicked"<getText()); + + (new GUITextInputMenu(guienv, guiroot, -1, + &g_menumgr, dest, + wtext))->drop(); + } + } + else + { + client.groundAction(1, nodepos, neighbourpos, g_selected_item); + } } nodepos_old = nodepos; diff --git a/src/mapblock.cpp b/src/mapblock.cpp index e895f774..c14fbd04 100644 --- a/src/mapblock.cpp +++ b/src/mapblock.cpp @@ -1878,8 +1878,6 @@ void MapBlock::serialize(std::ostream &os, u8 version) flags |= 0x02; if(m_lighting_expired) flags |= 0x04; - /*if(m_not_fully_generated) - flags |= 0x08;*/ os.write((char*)&flags, 1); u32 nodecount = MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE; @@ -1913,6 +1911,14 @@ void MapBlock::serialize(std::ostream &os, u8 version) */ compress(databuf, os, version); + + /* + NodeMetadata + */ + if(version >= 14) + { + m_node_metadata.serialize(os); + } } } @@ -2002,7 +2008,6 @@ void MapBlock::deSerialize(std::istream &is, u8 version) is_underground = (flags & 0x01) ? true : false; m_day_night_differs = (flags & 0x02) ? true : false; m_lighting_expired = (flags & 0x04) ? true : false; - //m_not_fully_generated = (flags & 0x08) ? true : false; // Uncompress data std::ostringstream os(std::ios_base::binary); @@ -2027,6 +2032,14 @@ void MapBlock::deSerialize(std::istream &is, u8 version) { data[i].param2 = s[i+nodecount*2]; } + + /* + NodeMetadata + */ + if(version >= 14) + { + m_node_metadata.deSerialize(is); + } } /* diff --git a/src/mapnode.cpp b/src/mapnode.cpp index c8a7e504..cb8bf7c4 100644 --- a/src/mapnode.cpp +++ b/src/mapnode.cpp @@ -307,7 +307,7 @@ void init_mapnode() f->wall_mounted = true; f->dug_item = std::string("MaterialItem ")+itos(i)+" 1"; if(f->initial_metadata == NULL) - f->initial_metadata = new SignNodeMetadata(); + f->initial_metadata = new SignNodeMetadata("Some sign"); } diff --git a/src/serialization.h b/src/serialization.h index e84ceee3..fed5bb52 100644 --- a/src/serialization.h +++ b/src/serialization.h @@ -46,11 +46,12 @@ with this program; if not, write to the Free Software Foundation, Inc., 11: (dev) zlib'd blocks, block flags 12: (dev) UnlimitedHeightmap now uses interpolated areas 13: (dev) Mapgen v2 + 14: (dev) NodeMetadata */ // This represents an uninitialized or invalid format #define SER_FMT_VER_INVALID 255 // Highest supported serialization version -#define SER_FMT_VER_HIGHEST 13 +#define SER_FMT_VER_HIGHEST 14 // Lowest supported serialization version #define SER_FMT_VER_LOWEST 2 diff --git a/src/server.cpp b/src/server.cpp index b3ce9c13..44c26dbc 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1221,6 +1221,8 @@ void Server::AsyncRunStep() { //u16 peer_id = i.getNode()->getKey(); RemoteClient *client = i.getNode()->getValue(); + Player *player = m_env.getPlayer(client->peer_id); + std::cout<getName()<<" "; client->PrintInfo(std::cout); } } @@ -2395,6 +2397,52 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) obj->getBlock()->setChangedFlag(); } + else if(command == TOSERVER_SIGNNODETEXT) + { + /* + u16 command + v3s16 p + u16 textlen + textdata + */ + std::string datastring((char*)&data[2], datasize-2); + std::istringstream is(datastring, std::ios_base::binary); + u8 buf[6]; + // Read stuff + is.read((char*)buf, 6); + v3s16 p = readV3S16(buf); + is.read((char*)buf, 2); + u16 textlen = readU16(buf); + std::string text; + for(u16 i=0; itypeId() != CONTENT_SIGN_WALL) + return; + SignNodeMetadata *signmeta = (SignNodeMetadata*)meta; + signmeta->setText(text); + + v3s16 blockpos = getNodeBlockPos(p); + MapBlock *block = m_env.getMap().getBlockNoCreateNoEx(blockpos); + if(block) + { + block->setChangedFlag(); + } + + for(core::map::Iterator + i = m_clients.getIterator(); + i.atEnd()==false; i++) + { + RemoteClient *client = i.getNode()->getValue(); + client->SetBlockNotSent(blockpos); + } + } else if(command == TOSERVER_INVENTORY_ACTION) { /*// Ignore inventory changes if in creative mode @@ -3376,12 +3424,12 @@ void setCreativeInventory(Player *player) } #endif - // Sign + /*// Sign { InventoryItem *item = new MapBlockObjectItem("Sign Example text"); void* r = player->inventory.addItem("main", item); assert(r == NULL); - } + }*/ } Player *Server::emergePlayer(const char *name, const char *password,