X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;ds=inline;f=src%2Fclient.cpp;h=75168c6855ad1c16879d5cc45a62e77224c90eda;hb=b65a5aceb0d6552d8ba4878bed5326c614dd3046;hp=449b0c2f26ccaedf024a85158120fd100633b10d;hpb=3fccc67eb7c530c280e9b496e22288ffa772152d;p=dragonfireclient.git diff --git a/src/client.cpp b/src/client.cpp index 449b0c2f2..75168c685 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -5,7 +5,7 @@ Copyright (C) 2010 celeron55, Perttu Ahola This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or -MeshUpdateQueue::(at your option) any later version. +(at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -28,6 +28,9 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "mapsector.h" #include "mapblock_mesh.h" #include "mapblock.h" +#include "settings.h" +#include "profiler.h" +#include "log.h" /* QueuedMeshUpdate @@ -129,6 +132,8 @@ void * MeshUpdateThread::Thread() { ThreadStarted(); + log_register_thread("MeshUpdateThread"); + DSTACK(__FUNCTION_NAME); BEGIN_DEBUG_EXCEPTION_HANDLER @@ -151,7 +156,7 @@ void * MeshUpdateThread::Thread() continue; } - ScopeProfiler sp(&g_profiler, "mesh make"); + ScopeProfiler sp(g_profiler, "mesh make"); scene::SMesh *mesh_new = NULL; mesh_new = makeMapBlockMesh(q->data); @@ -161,7 +166,7 @@ void * MeshUpdateThread::Thread() r.mesh = mesh_new; r.ack_block_to_server = q->ack_block_to_server; - /*dstream<<"MeshUpdateThread: Processed " + /*infostream<<"MeshUpdateThread: Processed " <<"("<p.X<<","<p.Y<<","<p.Z<<")" < deleted_blocks; float delete_unused_sectors_timeout = - g_settings.getFloat("client_delete_unused_sectors_timeout"); + g_settings->getFloat("client_delete_unused_sectors_timeout"); // Delete sector blocks /*u32 num = m_env.getMap().unloadUnusedData @@ -339,9 +346,9 @@ void Client::step(float dtime) if(deleted_blocks.size() > 0) { - /*dstream< data(2+1+PLAYERNAME_SIZE+PASSWORD_SIZE); + // [23] u8[28] password (new in some version) + // [51] u16 client network protocol version (new in some version) + SharedBuffer data(2+1+PLAYERNAME_SIZE+PASSWORD_SIZE+2); writeU16(&data[0], TOSERVER_INIT); writeU8(&data[2], SER_FMT_VER_HIGHEST); memset((char*)&data[3], 0, PLAYERNAME_SIZE); snprintf((char*)&data[3], PLAYERNAME_SIZE, "%s", myplayer->getName()); - /*dstream<<"Client: password hash is \""< deleted_blocks; + m_env.getMap().timerUpdate(map_timer_and_unload_dtime, + g_settings->getFloat("client_unload_unused_data_timeout"), + &deleted_blocks); + + /*if(deleted_blocks.size() > 0) + infostream<<"Client: Unloaded "<::Iterator i = deleted_blocks.begin(); + core::list sendlist; + for(;;) + { + if(sendlist.size() == 255 || i == deleted_blocks.end()) + { + if(sendlist.size() == 0) + break; + /* + [0] u16 command + [2] u8 count + [3] v3s16 pos_0 + [3+6] v3s16 pos_1 + ... + */ + u32 replysize = 2+1+6*sendlist.size(); + SharedBuffer reply(replysize); + writeU16(&reply[0], TOSERVER_DELETEDBLOCKS); + reply[2] = sendlist.size(); + u32 k = 0; + for(core::list::Iterator + j = sendlist.begin(); + j != sendlist.end(); j++) + { + writeV3S16(&reply[2+1+6*k], *j); + k++; + } + m_con.Send(PEER_ID_SERVER, 1, reply, true); + + if(i == deleted_blocks.end()) + break; + + sendlist.clear(); + } + + sendlist.push_back(*i); + i++; + } + } + /* Handle environment */ @@ -453,25 +524,7 @@ void Client::step(float dtime) //TimeTaker envtimer("env step", m_device); // Step environment m_env.step(dtime); - - // Step active blocks - for(core::map::Iterator - i = m_active_blocks.getIterator(); - i.atEnd() == false; i++) - { - v3s16 p = i.getNode()->getKey(); - - MapBlock *block = NULL; - try - { - block = m_env.getMap().getBlockNoCreate(p); - block->stepObjects(dtime, false, m_env.getDayNightRatio()); - } - catch(InvalidPositionException &e) - { - } - } - + /* Get events */ @@ -511,7 +564,7 @@ void Client::step(float dtime) //JMutexAutoLock lock(m_con_mutex); //bulk comment-out // connectedAndInitialized() is true, peer exists. con::Peer *peer = m_con.GetPeer(PEER_ID_SERVER); - dstream<id=" + infostream<<"Client::peerAdded(): peer->id=" <id<deSerialize(istr, ser_version); } else @@ -825,7 +878,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id) /* Create a new block */ - //dstream<<"Creating new"<deSerialize(istr, ser_version); sector->insertBlock(block); @@ -868,16 +921,15 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id) */ //m_env.getClientMap().updateMeshes(block->getPos(), getDayNightRatio()); - /* Add it to mesh update queue and set it to be acknowledged after update. */ - //std::cerr<<"Adding mesh update task for received block"< players = m_env.getPlayers(); core::list::Iterator ip; for(ip=players.begin(); ip!=players.end(); ip++) @@ -1023,7 +1075,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id) // Warn about a special case if((*ip)->peer_id == 0) { - dstream<peer_id + infostream<<"Removing dead player "<<(*ip)->peer_id <peer_id); } @@ -1049,7 +1101,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id) } else if(command == TOCLIENT_SECTORMETA) { - dstream<<"Client received DEPRECATED TOCLIENT_SECTORMETA"<inventory.print(dstream); + //infostream<<"Client got player inventory:"<inventory.print(infostream); } } //DEBUG else if(command == TOCLIENT_OBJECTDATA) - //else if(0) { // Strip command word and create a stringstream std::string datastring((char*)&data[2], datasize-2); std::istringstream is(datastring, std::ios_base::binary); - { //envlock - - //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out - u8 buf[12]; /* @@ -1183,105 +1230,16 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id) /* Read block objects + NOTE: Deprecated stuff */ // Read active block count - is.read((char*)buf, 2); - u16 blockcount = readU16(buf); - - // Initialize delete queue with all active blocks - core::map abs_to_delete; - for(core::map::Iterator - i = m_active_blocks.getIterator(); - i.atEnd() == false; i++) - { - v3s16 p = i.getNode()->getKey(); - /*dstream<<"adding " - <<"("<updateObjects(is, m_server_ser_ver, - m_device->getSceneManager(), m_env.getDayNightRatio()); - } - - /*dstream<<"Final delete queue size: "<::Iterator - i = abs_to_delete.getIterator(); - i.atEnd() == false; i++) - { - v3s16 p = i.getNode()->getKey(); - try - { - MapBlock *block = m_env.getMap().getBlockNoCreate(p); - - // Clear objects - block->clearObjects(); - // Remove from active blocks list - m_active_blocks.remove(p); - } - catch(InvalidPositionException &e) - { - dstream<<"WARNAING: Client: " - <<"Couldn't clear objects of active->inactive" - <<" block " - <<"("<getBool("enable_experimental")) { /* u16 command @@ -1349,7 +1307,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id) for all added objects { u16 id u8 type - u16 initialization data length + u32 initialization data length string initialization data } */ @@ -1396,7 +1354,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id) } else if(command == TOCLIENT_ACTIVE_OBJECT_MESSAGES) { - //if(g_settings.getBool("enable_experimental")) + //if(g_settings->getBool("enable_experimental")) { /* u16 command @@ -1459,7 +1417,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id) /*player->setPitch(pitch); player->setYaw(yaw);*/ - dstream<<"Client got TOCLIENT_MOVE_PLAYER" + infostream<<"Client got TOCLIENT_MOVE_PLAYER" <<" pos=("<isLocal()) { + infostream<<"Client: ignoring player item " + << deSerializeString(is) + << " for local player" << std::endl; + continue; + } else { + InventoryList *inv = player->inventory.getList("main"); + std::string itemstring(deSerializeString(is)); + if (itemstring.empty()) { + inv->deleteItem(0); + infostream + <<"Client: empty player item for peer " + << peer_id << std::endl; + } else { + std::istringstream iss(itemstring); + delete inv->changeItem(0, InventoryItem::deSerialize(iss)); + infostream<<"Client: player item for peer " << peer_id << ": "; + player->getWieldItem()->serialize(infostream); + infostream< data(datasize); - writeU16(&data[0], TOSERVER_CLICK_OBJECT); - writeU8(&data[2], button); - writeV3S16(&data[3], blockpos); - writeS16(&data[9], id); - writeU16(&data[11], item); - Send(0, data, true); -} -void Client::clickActiveObject(u8 button, u16 id, u16 item) -{ - if(connectedAndInitialized() == false){ - dout_client<inventory.getList("main"); + if(mlist != NULL) + { + InventoryItem *item = mlist->getItem(item_i); + if(item && (std::string)item->getName() == "ToolItem") + { + titem = (ToolItem*)item; + toolname = titem->getToolName(); + } + } + + v3f playerpos = player->getPosition(); + v3f objpos = obj->getPosition(); + v3f dir = (objpos - playerpos).normalize(); + + bool disable_send = obj->directReportPunch(toolname, dir); + + if(disable_send) + return; + } } /* @@ -1574,49 +1595,10 @@ void Client::clickActiveObject(u8 button, u16 id, u16 item) writeU16(&data[0], TOSERVER_CLICK_ACTIVEOBJECT); writeU8(&data[2], button); writeU16(&data[3], id); - writeU16(&data[5], item); + writeU16(&data[5], item_i); Send(0, data, true); } -void Client::sendSignText(v3s16 blockpos, s16 id, std::string text) -{ - /* - u16 command - v3s16 blockpos - s16 id - u16 textlen - textdata - */ - std::ostringstream os(std::ios_base::binary); - u8 buf[12]; - - // Write command - writeU16(buf, TOSERVER_SIGNTEXT); - os.write((char*)buf, 2); - - // Write blockpos - writeV3S16(buf, blockpos); - os.write((char*)buf, 6); - - // Write id - writeS16(buf, id); - os.write((char*)buf, 2); - - 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::sendSignNodeText(v3s16 p, std::string text) { /* @@ -1749,6 +1731,20 @@ void Client::sendDamage(u8 damage) Send(0, data, true); } +void Client::sendRespawn() +{ + DSTACK(__FUNCTION_NAME); + std::ostringstream os(std::ios_base::binary); + + writeU16(os, TOSERVER_RESPAWN); + + // 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::sendPlayerPos() { //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out @@ -1796,6 +1792,28 @@ void Client::sendPlayerPos() Send(0, data, false); } +void Client::sendPlayerItem(u16 item) +{ + Player *myplayer = m_env.getLocalPlayer(); + if(myplayer == NULL) + return; + + u16 our_peer_id = m_con.GetPeerID(); + + // Set peer id if not set already + if(myplayer->peer_id == PEER_ID_INEXISTENT) + myplayer->peer_id = our_peer_id; + // Check that an existing peer_id is the same as the connection's + assert(myplayer->peer_id == our_peer_id); + + SharedBuffer data(2+2); + writeU16(&data[0], TOSERVER_PLAYERITEM); + writeU16(&data[2], item); + + // Send as reliable + Send(0, data, true); +} + void Client::removeNode(v3s16 p) { //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out @@ -1832,7 +1850,8 @@ void Client::addNode(v3s16 p, MapNode n) try { //TimeTaker timer3("Client::addNode(): addNodeAndUpdate"); - m_env.getMap().addNodeAndUpdate(p, n, modified_blocks); + std::string st = std::string(""); + m_env.getMap().addNodeAndUpdate(p, n, modified_blocks, st); } catch(InvalidPositionException &e) {} @@ -1849,11 +1868,14 @@ void Client::addNode(v3s16 p, MapNode n) } } -void Client::updateCamera(v3f pos, v3f dir) +void Client::updateCamera(v3f pos, v3f dir, f32 fov) +{ + m_env.getClientMap().updateCamera(pos, dir, fov); +} + +void Client::renderPostFx() { - m_env.getClientMap().updateCamera(pos, dir); - camera_position = pos; - camera_direction = dir; + m_env.getClientMap().renderPostFx(); } MapNode Client::getNode(v3s16 p) @@ -1867,12 +1889,9 @@ NodeMetadata* Client::getNodeMetadata(v3s16 p) return m_env.getMap().getNodeMetadata(p); } -v3f Client::getPlayerPosition() +LocalPlayer* Client::getLocalPlayer() { - //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out - LocalPlayer *player = m_env.getLocalPlayer(); - assert(player != NULL); - return player->getPosition(); + return m_env.getLocalPlayer(); } void Client::setPlayerControl(PlayerControl &control) @@ -1883,6 +1902,16 @@ void Client::setPlayerControl(PlayerControl &control) player->control = control; } +void Client::selectPlayerItem(u16 item) +{ + LocalPlayer *player = m_env.getLocalPlayer(); + assert(player != NULL); + + player->wieldItem(item); + + sendPlayerItem(item); +} + // Returns true if the inventory of the local player has been // updated from the server. If it is true, it is set to false. bool Client::getLocalInventoryUpdated() @@ -1928,12 +1957,12 @@ Inventory* Client::getInventory(InventoryContext *c, std::string id) NodeMetadata* meta = getNodeMetadata(p); if(meta) return meta->getInventory(); - dstream<<"nodemeta at ("< shootline_on_map - ) -{ - //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out - - core::array objects; - - for(core::map::Iterator - i = m_active_blocks.getIterator(); - i.atEnd() == false; i++) - { - v3s16 p = i.getNode()->getKey(); - - MapBlock *block = NULL; - try - { - block = m_env.getMap().getBlockNoCreate(p); - } - catch(InvalidPositionException &e) - { - continue; - } - - // Calculate from_pos relative to block - v3s16 block_pos_i_on_map = block->getPosRelative(); - v3f block_pos_f_on_map = intToFloat(block_pos_i_on_map, BS); - v3f from_pos_f_on_block = from_pos_f_on_map - block_pos_f_on_map; - - block->getObjects(from_pos_f_on_block, max_d, objects); - //block->getPseudoObjects(from_pos_f_on_block, max_d, objects); - } - - //dstream<<"Collected "<getBlock(); - - // Calculate shootline relative to block - v3s16 block_pos_i_on_map = block->getPosRelative(); - v3f block_pos_f_on_map = intToFloat(block_pos_i_on_map, BS); - core::line3d shootline_on_block( - shootline_on_map.start - block_pos_f_on_map, - shootline_on_map.end - block_pos_f_on_map - ); - - if(obj->isSelected(shootline_on_block)) - { - //dstream<<"Returning selected object"<