3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "clientserver.h"
25 #include "jmutexautolock.h"
27 #include "constants.h"
29 #include "materials.h"
32 #define BLOCK_EMERGE_FLAG_FROMDISK (1<<0)
34 void * ServerThread::Thread()
38 DSTACK(__FUNCTION_NAME);
40 BEGIN_DEBUG_EXCEPTION_HANDLER
45 //TimeTaker timer("AsyncRunStep() + Receive()");
48 //TimeTaker timer("AsyncRunStep()");
49 m_server->AsyncRunStep();
52 //dout_server<<"Running m_server->Receive()"<<std::endl;
55 catch(con::NoIncomingDataException &e)
58 catch(con::PeerNotFoundException &e)
60 dout_server<<"Server: PeerNotFoundException"<<std::endl;
64 END_DEBUG_EXCEPTION_HANDLER
69 void * EmergeThread::Thread()
73 DSTACK(__FUNCTION_NAME);
77 BEGIN_DEBUG_EXCEPTION_HANDLER
80 Get block info from queue, emerge them and send them
83 After queue is empty, exit.
87 QueuedBlockEmerge *qptr = m_server->m_emerge_queue.pop();
91 SharedPtr<QueuedBlockEmerge> q(qptr);
95 //derr_server<<"EmergeThread::Thread(): running"<<std::endl;
97 //TimeTaker timer("block emerge");
100 Try to emerge it from somewhere.
102 If it is only wanted as optional, only loading from disk
107 Check if any peer wants it as non-optional. In that case it
110 Also decrement the emerge queue count in clients.
113 bool optional = true;
116 core::map<u16, u8>::Iterator i;
117 for(i=q->peer_ids.getIterator(); i.atEnd()==false; i++)
119 //u16 peer_id = i.getNode()->getKey();
122 u8 flags = i.getNode()->getValue();
123 if((flags & BLOCK_EMERGE_FLAG_FROMDISK) == false)
129 /*dstream<<"EmergeThread: p="
130 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<") "
131 <<"optional="<<optional<<std::endl;*/
133 ServerMap &map = ((ServerMap&)m_server->m_env.getMap());
135 core::map<v3s16, MapBlock*> changed_blocks;
136 core::map<v3s16, MapBlock*> lighting_invalidated_blocks;
138 MapBlock *block = NULL;
139 bool got_block = true;
140 core::map<v3s16, MapBlock*> modified_blocks;
144 //TimeTaker envlockwaittimer("block emerge envlock wait time");
147 JMutexAutoLock envlock(m_server->m_env_mutex);
149 //envlockwaittimer.stop();
151 //TimeTaker timer("block emerge (while env locked)");
154 bool only_from_disk = false;
157 only_from_disk = true;
159 // First check if the block already exists
160 //block = map.getBlockNoCreate(p);
164 //dstream<<"Calling emergeBlock"<<std::endl;
165 block = map.emergeBlock(
169 lighting_invalidated_blocks);
173 While we're at it, generate some other blocks too
181 lighting_invalidated_blocks);
186 lighting_invalidated_blocks);
188 catch(InvalidPositionException &e)
194 // If it is a dummy, block was not found on disk
197 //dstream<<"EmergeThread: Got a dummy block"<<std::endl;
200 if(only_from_disk == false)
202 dstream<<"EmergeThread: wanted to generate a block but got a dummy"<<std::endl;
207 catch(InvalidPositionException &e)
210 // This happens when position is over limit.
216 if(debug && changed_blocks.size() > 0)
218 dout_server<<DTIME<<"Got changed_blocks: ";
219 for(core::map<v3s16, MapBlock*>::Iterator i = changed_blocks.getIterator();
220 i.atEnd() == false; i++)
222 MapBlock *block = i.getNode()->getValue();
223 v3s16 p = block->getPos();
224 dout_server<<"("<<p.X<<","<<p.Y<<","<<p.Z<<") ";
226 dout_server<<std::endl;
230 Collect a list of blocks that have been modified in
231 addition to the fetched one.
234 if(lighting_invalidated_blocks.size() > 0)
236 /*dstream<<"lighting "<<lighting_invalidated_blocks.size()
237 <<" blocks"<<std::endl;*/
239 // 50-100ms for single block generation
240 //TimeTaker timer("** EmergeThread updateLighting");
242 // Update lighting without locking the environment mutex,
243 // add modified blocks to changed blocks
244 map.updateLighting(lighting_invalidated_blocks, modified_blocks);
247 // Add all from changed_blocks to modified_blocks
248 for(core::map<v3s16, MapBlock*>::Iterator i = changed_blocks.getIterator();
249 i.atEnd() == false; i++)
251 MapBlock *block = i.getNode()->getValue();
252 modified_blocks.insert(block->getPos(), block);
255 // If we got no block, there should be no invalidated blocks
258 assert(lighting_invalidated_blocks.size() == 0);
264 Set sent status of modified blocks on clients
267 // NOTE: Server's clients are also behind the connection mutex
268 JMutexAutoLock lock(m_server->m_con_mutex);
271 Add the originally fetched block to the modified list
275 modified_blocks.insert(p, block);
279 Set the modified blocks unsent for all the clients
282 for(core::map<u16, RemoteClient*>::Iterator
283 i = m_server->m_clients.getIterator();
284 i.atEnd() == false; i++)
286 RemoteClient *client = i.getNode()->getValue();
288 if(modified_blocks.size() > 0)
290 // Remove block from sent history
291 client->SetBlocksNotSent(modified_blocks);
297 END_DEBUG_EXCEPTION_HANDLER
302 void RemoteClient::GetNextBlocks(Server *server, float dtime,
303 core::array<PrioritySortedBlockTransfer> &dest)
305 DSTACK(__FUNCTION_NAME);
308 m_nearest_unsent_reset_timer += dtime;
310 // Won't send anything if already sending
311 if(m_blocks_sending.size() >= g_settings.getU16
312 ("max_simultaneous_block_sends_per_client"))
314 //dstream<<"Not sending any blocks, Queue full."<<std::endl;
318 Player *player = server->m_env.getPlayer(peer_id);
320 assert(player != NULL);
322 v3f playerpos = player->getPosition();
323 v3f playerspeed = player->getSpeed();
325 v3s16 center_nodepos = floatToInt(playerpos, BS);
327 v3s16 center = getNodeBlockPos(center_nodepos);
329 // Camera position and direction
331 playerpos + v3f(0, BS+BS/2, 0);
332 v3f camera_dir = v3f(0,0,1);
333 camera_dir.rotateYZBy(player->getPitch());
334 camera_dir.rotateXZBy(player->getYaw());
337 Get the starting value of the block finder radius.
339 s16 last_nearest_unsent_d;
342 if(m_last_center != center)
344 m_nearest_unsent_d = 0;
345 m_last_center = center;
348 /*dstream<<"m_nearest_unsent_reset_timer="
349 <<m_nearest_unsent_reset_timer<<std::endl;*/
350 if(m_nearest_unsent_reset_timer > 5.0)
352 m_nearest_unsent_reset_timer = 0;
353 m_nearest_unsent_d = 0;
354 //dstream<<"Resetting m_nearest_unsent_d"<<std::endl;
357 last_nearest_unsent_d = m_nearest_unsent_d;
359 d_start = m_nearest_unsent_d;
361 u16 maximum_simultaneous_block_sends_setting = g_settings.getU16
362 ("max_simultaneous_block_sends_per_client");
363 u16 maximum_simultaneous_block_sends =
364 maximum_simultaneous_block_sends_setting;
367 Check the time from last addNode/removeNode.
369 Decrease send rate if player is building stuff.
371 m_time_from_building += dtime;
372 if(m_time_from_building < g_settings.getFloat(
373 "full_block_send_enable_min_time_from_building"))
375 maximum_simultaneous_block_sends
376 = LIMITED_MAX_SIMULTANEOUS_BLOCK_SENDS;
379 u32 num_blocks_selected = m_blocks_sending.size();
382 next time d will be continued from the d from which the nearest
383 unsent block was found this time.
385 This is because not necessarily any of the blocks found this
386 time are actually sent.
388 s32 new_nearest_unsent_d = -1;
390 s16 d_max = g_settings.getS16("max_block_send_distance");
391 s16 d_max_gen = g_settings.getS16("max_block_generate_distance");
393 //dstream<<"Starting from "<<d_start<<std::endl;
395 for(s16 d = d_start; d <= d_max; d++)
397 //dstream<<"RemoteClient::SendBlocks(): d="<<d<<std::endl;
400 If m_nearest_unsent_d was changed by the EmergeThread
401 (it can change it to 0 through SetBlockNotSent),
403 Else update m_nearest_unsent_d
405 if(m_nearest_unsent_d != last_nearest_unsent_d)
407 d = m_nearest_unsent_d;
408 last_nearest_unsent_d = m_nearest_unsent_d;
412 Get the border/face dot coordinates of a "d-radiused"
415 core::list<v3s16> list;
416 getFacePositions(list, d);
418 core::list<v3s16>::Iterator li;
419 for(li=list.begin(); li!=list.end(); li++)
421 v3s16 p = *li + center;
425 - Don't allow too many simultaneous transfers
426 - EXCEPT when the blocks are very close
428 Also, don't send blocks that are already flying.
431 u16 maximum_simultaneous_block_sends_now =
432 maximum_simultaneous_block_sends;
434 if(d <= BLOCK_SEND_DISABLE_LIMITS_MAX_D)
436 maximum_simultaneous_block_sends_now =
437 maximum_simultaneous_block_sends_setting;
440 // Limit is dynamically lowered when building
441 if(num_blocks_selected
442 >= maximum_simultaneous_block_sends_now)
444 /*dstream<<"Not sending more blocks. Queue full. "
445 <<m_blocks_sending.size()
450 if(m_blocks_sending.find(p) != NULL)
456 if(p.X < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
457 || p.X > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
458 || p.Y < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
459 || p.Y > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
460 || p.Z < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
461 || p.Z > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE)
464 // If this is true, inexistent block will be made from scratch
465 bool generate = d <= d_max_gen;
468 /*// Limit the generating area vertically to 2/3
469 if(abs(p.Y - center.Y) > d_max_gen - d_max_gen / 3)
472 // Limit the send area vertically to 2/3
473 if(abs(p.Y - center.Y) > d_max_gen - d_max_gen / 3)
479 If block is far away, don't generate it unless it is
482 NOTE: We can't know the ground level this way with the
488 MapSector *sector = NULL;
491 sector = server->m_env.getMap().getSectorNoGenerate(p2d);
493 catch(InvalidPositionException &e)
499 // Get center ground height in nodes
500 f32 gh = sector->getGroundHeight(
501 v2s16(MAP_BLOCKSIZE/2, MAP_BLOCKSIZE/2));
502 // Block center y in nodes
503 f32 y = (f32)(p.Y * MAP_BLOCKSIZE + MAP_BLOCKSIZE/2);
504 // If differs a lot, don't generate
505 if(fabs(gh - y) > MAP_BLOCKSIZE*2)
512 Don't generate or send if not in sight
515 if(isBlockInSight(p, camera_pos, camera_dir, 10000*BS) == false)
521 Don't send already sent blocks
524 if(m_blocks_sent.find(p) != NULL)
529 Check if map has this block
531 MapBlock *block = server->m_env.getMap().getBlockNoCreateNoEx(p);
533 bool surely_not_found_on_disk = false;
534 bool block_is_invalid = false;
539 surely_not_found_on_disk = true;
542 if(block->isValid() == false)
544 block_is_invalid = true;
547 /*if(block->isFullyGenerated() == false)
549 block_is_invalid = true;
553 ServerMap *map = (ServerMap*)(&server->m_env.getMap());
554 v2s16 chunkpos = map->sector_to_chunk(p2d);
555 if(map->chunkNonVolatile(chunkpos) == false)
556 block_is_invalid = true;
560 If block has been marked to not exist on disk (dummy)
561 and generating new ones is not wanted, skip block.
563 if(generate == false && surely_not_found_on_disk == true)
570 Record the lowest d from which a a block has been
571 found being not sent and possibly to exist
573 if(new_nearest_unsent_d == -1 || d < new_nearest_unsent_d)
575 new_nearest_unsent_d = d;
579 Add inexistent block to emerge queue.
581 if(block == NULL || surely_not_found_on_disk || block_is_invalid)
583 //TODO: Get value from somewhere
584 // Allow only one block in emerge queue
585 //if(server->m_emerge_queue.peerItemCount(peer_id) < 1)
586 if(server->m_emerge_queue.peerItemCount(peer_id) < 2)
588 //dstream<<"Adding block to emerge queue"<<std::endl;
590 // Add it to the emerge queue and trigger the thread
593 if(generate == false)
594 flags |= BLOCK_EMERGE_FLAG_FROMDISK;
596 server->m_emerge_queue.addBlock(peer_id, p, flags);
597 server->m_emergethread.trigger();
605 Add block to send queue
608 PrioritySortedBlockTransfer q((float)d, p, peer_id);
612 num_blocks_selected += 1;
617 if(new_nearest_unsent_d != -1)
619 m_nearest_unsent_d = new_nearest_unsent_d;
623 void RemoteClient::SendObjectData(
626 core::map<v3s16, bool> &stepped_blocks
629 DSTACK(__FUNCTION_NAME);
631 // Can't send anything without knowing version
632 if(serialization_version == SER_FMT_VER_INVALID)
634 dstream<<"RemoteClient::SendObjectData(): Not sending, no version."
640 Send a TOCLIENT_OBJECTDATA packet.
644 u16 number of player positions
655 std::ostringstream os(std::ios_base::binary);
659 writeU16(buf, TOCLIENT_OBJECTDATA);
660 os.write((char*)buf, 2);
663 Get and write player data
666 // Get connected players
667 core::list<Player*> players = server->m_env.getPlayers(true);
669 // Write player count
670 u16 playercount = players.size();
671 writeU16(buf, playercount);
672 os.write((char*)buf, 2);
674 core::list<Player*>::Iterator i;
675 for(i = players.begin();
676 i != players.end(); i++)
680 v3f pf = player->getPosition();
681 v3f sf = player->getSpeed();
683 v3s32 position_i(pf.X*100, pf.Y*100, pf.Z*100);
684 v3s32 speed_i (sf.X*100, sf.Y*100, sf.Z*100);
685 s32 pitch_i (player->getPitch() * 100);
686 s32 yaw_i (player->getYaw() * 100);
688 writeU16(buf, player->peer_id);
689 os.write((char*)buf, 2);
690 writeV3S32(buf, position_i);
691 os.write((char*)buf, 12);
692 writeV3S32(buf, speed_i);
693 os.write((char*)buf, 12);
694 writeS32(buf, pitch_i);
695 os.write((char*)buf, 4);
696 writeS32(buf, yaw_i);
697 os.write((char*)buf, 4);
701 Get and write object data
707 For making players to be able to build to their nearby
708 environment (building is not possible on blocks that are not
711 - Add blocks to emerge queue if they are not found
713 SUGGESTION: These could be ignored from the backside of the player
716 Player *player = server->m_env.getPlayer(peer_id);
720 v3f playerpos = player->getPosition();
721 v3f playerspeed = player->getSpeed();
723 v3s16 center_nodepos = floatToInt(playerpos, BS);
724 v3s16 center = getNodeBlockPos(center_nodepos);
726 s16 d_max = g_settings.getS16("active_object_range");
728 // Number of blocks whose objects were written to bos
731 std::ostringstream bos(std::ios_base::binary);
733 for(s16 d = 0; d <= d_max; d++)
735 core::list<v3s16> list;
736 getFacePositions(list, d);
738 core::list<v3s16>::Iterator li;
739 for(li=list.begin(); li!=list.end(); li++)
741 v3s16 p = *li + center;
744 Ignore blocks that haven't been sent to the client
747 if(m_blocks_sent.find(p) == NULL)
751 // Try stepping block and add it to a send queue
756 MapBlock *block = server->m_env.getMap().getBlockNoCreate(p);
759 Step block if not in stepped_blocks and add to stepped_blocks.
761 if(stepped_blocks.find(p) == NULL)
763 block->stepObjects(dtime, true, server->getDayNightRatio());
764 stepped_blocks.insert(p, true);
765 block->setChangedFlag();
768 // Skip block if there are no objects
769 if(block->getObjectCount() == 0)
778 bos.write((char*)buf, 6);
781 block->serializeObjects(bos, serialization_version);
786 Stop collecting objects if data is already too big
788 // Sum of player and object data sizes
789 s32 sum = (s32)os.tellp() + 2 + (s32)bos.tellp();
790 // break out if data too big
791 if(sum > MAX_OBJECTDATA_SIZE)
793 goto skip_subsequent;
797 catch(InvalidPositionException &e)
800 // Add it to the emerge queue and trigger the thread.
801 // Fetch the block only if it is on disk.
803 // Grab and increment counter
804 /*SharedPtr<JMutexAutoLock> lock
805 (m_num_blocks_in_emerge_queue.getLock());
806 m_num_blocks_in_emerge_queue.m_value++;*/
808 // Add to queue as an anonymous fetch from disk
809 u8 flags = BLOCK_EMERGE_FLAG_FROMDISK;
810 server->m_emerge_queue.addBlock(0, p, flags);
811 server->m_emergethread.trigger();
819 writeU16(buf, blockcount);
820 os.write((char*)buf, 2);
822 // Write block objects
829 //dstream<<"Server: Sending object data to "<<peer_id<<std::endl;
832 std::string s = os.str();
833 SharedBuffer<u8> data((u8*)s.c_str(), s.size());
834 // Send as unreliable
835 server->m_con.Send(peer_id, 0, data, false);
838 void RemoteClient::GotBlock(v3s16 p)
840 if(m_blocks_sending.find(p) != NULL)
841 m_blocks_sending.remove(p);
844 /*dstream<<"RemoteClient::GotBlock(): Didn't find in"
845 " m_blocks_sending"<<std::endl;*/
846 m_excess_gotblocks++;
848 m_blocks_sent.insert(p, true);
851 void RemoteClient::SentBlock(v3s16 p)
853 if(m_blocks_sending.find(p) == NULL)
854 m_blocks_sending.insert(p, 0.0);
856 dstream<<"RemoteClient::SentBlock(): Sent block"
857 " already in m_blocks_sending"<<std::endl;
860 void RemoteClient::SetBlockNotSent(v3s16 p)
862 m_nearest_unsent_d = 0;
864 if(m_blocks_sending.find(p) != NULL)
865 m_blocks_sending.remove(p);
866 if(m_blocks_sent.find(p) != NULL)
867 m_blocks_sent.remove(p);
870 void RemoteClient::SetBlocksNotSent(core::map<v3s16, MapBlock*> &blocks)
872 m_nearest_unsent_d = 0;
874 for(core::map<v3s16, MapBlock*>::Iterator
875 i = blocks.getIterator();
876 i.atEnd()==false; i++)
878 v3s16 p = i.getNode()->getKey();
880 if(m_blocks_sending.find(p) != NULL)
881 m_blocks_sending.remove(p);
882 if(m_blocks_sent.find(p) != NULL)
883 m_blocks_sent.remove(p);
891 PlayerInfo::PlayerInfo()
896 void PlayerInfo::PrintLine(std::ostream *s)
899 (*s)<<"\""<<name<<"\" ("
900 <<(position.X/10)<<","<<(position.Y/10)
901 <<","<<(position.Z/10)<<") ";
903 (*s)<<" avg_rtt="<<avg_rtt;
907 u32 PIChecksum(core::list<PlayerInfo> &l)
909 core::list<PlayerInfo>::Iterator i;
912 for(i=l.begin(); i!=l.end(); i++)
914 checksum += a * (i->id+1);
915 checksum ^= 0x435aafcd;
926 std::string mapsavedir
928 m_env(new ServerMap(mapsavedir), this),
929 m_con(PROTOCOL_ID, 512, CONNECTION_TIMEOUT, this),
931 m_emergethread(this),
934 m_time_of_day_send_timer(0),
936 m_mapsavedir(mapsavedir),
937 m_shutdown_requested(false),
938 m_ignore_map_edit_events(false),
939 m_ignore_map_edit_events_peer_id(0)
941 m_liquid_transform_timer = 0.0;
942 m_print_info_timer = 0.0;
943 m_objectdata_timer = 0.0;
944 m_emergethread_trigger_timer = 0.0;
945 m_savemap_timer = 0.0;
949 m_step_dtime_mutex.Init();
952 m_env.getMap().addEventReceiver(this);
955 m_env.deSerializePlayers(m_mapsavedir);
961 Send shutdown message
964 JMutexAutoLock conlock(m_con_mutex);
966 std::wstring line = L"*** Server shutting down";
969 Send the message to clients
971 for(core::map<u16, RemoteClient*>::Iterator
972 i = m_clients.getIterator();
973 i.atEnd() == false; i++)
975 // Get client and check that it is valid
976 RemoteClient *client = i.getNode()->getValue();
977 assert(client->peer_id == i.getNode()->getKey());
978 if(client->serialization_version == SER_FMT_VER_INVALID)
981 SendChatMessage(client->peer_id, line);
988 m_env.serializePlayers(m_mapsavedir);
999 JMutexAutoLock clientslock(m_con_mutex);
1001 for(core::map<u16, RemoteClient*>::Iterator
1002 i = m_clients.getIterator();
1003 i.atEnd() == false; i++)
1006 // NOTE: These are removed by env destructor
1008 u16 peer_id = i.getNode()->getKey();
1009 JMutexAutoLock envlock(m_env_mutex);
1010 m_env.removePlayer(peer_id);
1014 delete i.getNode()->getValue();
1019 void Server::start(unsigned short port)
1021 DSTACK(__FUNCTION_NAME);
1022 // Stop thread if already running
1025 // Initialize connection
1026 m_con.setTimeoutMs(30);
1030 m_thread.setRun(true);
1033 dout_server<<"Server: Started on port "<<port<<std::endl;
1038 DSTACK(__FUNCTION_NAME);
1040 // Stop threads (set run=false first so both start stopping)
1041 m_thread.setRun(false);
1042 m_emergethread.setRun(false);
1044 m_emergethread.stop();
1046 dout_server<<"Server: Threads stopped"<<std::endl;
1048 dout_server<<"Server: Saving players"<<std::endl;
1050 // FIXME: Apparently this does not do anything here
1051 //m_env.serializePlayers(m_mapsavedir);
1054 void Server::step(float dtime)
1056 DSTACK(__FUNCTION_NAME);
1061 JMutexAutoLock lock(m_step_dtime_mutex);
1062 m_step_dtime += dtime;
1066 void Server::AsyncRunStep()
1068 DSTACK(__FUNCTION_NAME);
1072 JMutexAutoLock lock1(m_step_dtime_mutex);
1073 dtime = m_step_dtime;
1076 // Send blocks to clients
1082 //dstream<<"Server steps "<<dtime<<std::endl;
1083 //dstream<<"Server::AsyncRunStep(): dtime="<<dtime<<std::endl;
1086 JMutexAutoLock lock1(m_step_dtime_mutex);
1087 m_step_dtime -= dtime;
1094 m_uptime.set(m_uptime.get() + dtime);
1098 Update m_time_of_day
1101 m_time_counter += dtime;
1102 f32 speed = g_settings.getFloat("time_speed") * 24000./(24.*3600);
1103 u32 units = (u32)(m_time_counter*speed);
1104 m_time_counter -= (f32)units / speed;
1105 m_time_of_day.set((m_time_of_day.get() + units) % 24000);
1107 //dstream<<"Server: m_time_of_day = "<<m_time_of_day.get()<<std::endl;
1110 Send to clients at constant intervals
1113 m_time_of_day_send_timer -= dtime;
1114 if(m_time_of_day_send_timer < 0.0)
1116 m_time_of_day_send_timer = g_settings.getFloat("time_send_interval");
1118 //JMutexAutoLock envlock(m_env_mutex);
1119 JMutexAutoLock conlock(m_con_mutex);
1121 for(core::map<u16, RemoteClient*>::Iterator
1122 i = m_clients.getIterator();
1123 i.atEnd() == false; i++)
1125 RemoteClient *client = i.getNode()->getValue();
1126 //Player *player = m_env.getPlayer(client->peer_id);
1128 SharedBuffer<u8> data = makePacket_TOCLIENT_TIME_OF_DAY(
1129 m_time_of_day.get());
1131 m_con.Send(client->peer_id, 0, data, true);
1137 // Process connection's timeouts
1138 JMutexAutoLock lock2(m_con_mutex);
1139 m_con.RunTimeouts(dtime);
1143 // This has to be called so that the client list gets synced
1144 // with the peer list of the connection
1145 handlePeerChanges();
1150 // This also runs Map's timers
1151 JMutexAutoLock lock(m_env_mutex);
1162 m_liquid_transform_timer += dtime;
1163 if(m_liquid_transform_timer >= 1.00)
1165 m_liquid_transform_timer -= 1.00;
1167 JMutexAutoLock lock(m_env_mutex);
1169 core::map<v3s16, MapBlock*> modified_blocks;
1170 m_env.getMap().transformLiquids(modified_blocks);
1175 core::map<v3s16, MapBlock*> lighting_modified_blocks;
1176 ServerMap &map = ((ServerMap&)m_env.getMap());
1177 map.updateLighting(modified_blocks, lighting_modified_blocks);
1179 // Add blocks modified by lighting to modified_blocks
1180 for(core::map<v3s16, MapBlock*>::Iterator
1181 i = lighting_modified_blocks.getIterator();
1182 i.atEnd() == false; i++)
1184 MapBlock *block = i.getNode()->getValue();
1185 modified_blocks.insert(block->getPos(), block);
1189 Set the modified blocks unsent for all the clients
1192 JMutexAutoLock lock2(m_con_mutex);
1194 for(core::map<u16, RemoteClient*>::Iterator
1195 i = m_clients.getIterator();
1196 i.atEnd() == false; i++)
1198 RemoteClient *client = i.getNode()->getValue();
1200 if(modified_blocks.size() > 0)
1202 // Remove block from sent history
1203 client->SetBlocksNotSent(modified_blocks);
1208 // Periodically print some info
1210 float &counter = m_print_info_timer;
1216 JMutexAutoLock lock2(m_con_mutex);
1218 for(core::map<u16, RemoteClient*>::Iterator
1219 i = m_clients.getIterator();
1220 i.atEnd() == false; i++)
1222 //u16 peer_id = i.getNode()->getKey();
1223 RemoteClient *client = i.getNode()->getValue();
1224 Player *player = m_env.getPlayer(client->peer_id);
1225 std::cout<<player->getName()<<" ";
1226 client->PrintInfo(std::cout);
1231 if(g_settings.getBool("enable_experimental"))
1235 Check added and deleted active objects
1238 JMutexAutoLock envlock(m_env_mutex);
1239 JMutexAutoLock conlock(m_con_mutex);
1241 // Radius inside which objects are active
1244 for(core::map<u16, RemoteClient*>::Iterator
1245 i = m_clients.getIterator();
1246 i.atEnd() == false; i++)
1248 RemoteClient *client = i.getNode()->getValue();
1249 Player *player = m_env.getPlayer(client->peer_id);
1252 v3s16 pos = floatToInt(player->getPosition(), BS);
1254 core::map<u16, bool> removed_objects;
1255 core::map<u16, bool> added_objects;
1256 m_env.getRemovedActiveObjects(pos, radius,
1257 client->m_known_objects, removed_objects);
1258 m_env.getAddedActiveObjects(pos, radius,
1259 client->m_known_objects, added_objects);
1261 // Ignore if nothing happened
1262 if(removed_objects.size() == 0 && added_objects.size() == 0)
1265 std::string data_buffer;
1269 // Handle removed objects
1270 writeU16((u8*)buf, removed_objects.size());
1271 data_buffer.append(buf, 2);
1272 for(core::map<u16, bool>::Iterator
1273 i = removed_objects.getIterator();
1274 i.atEnd()==false; i++)
1277 u16 id = i.getNode()->getKey();
1278 ServerActiveObject* obj = m_env.getActiveObject(id);
1280 // Add to data buffer for sending
1281 writeU16((u8*)buf, i.getNode()->getKey());
1282 data_buffer.append(buf, 2);
1284 // Remove from known objects
1285 client->m_known_objects.remove(i.getNode()->getKey());
1287 if(obj && obj->m_known_by_count > 0)
1288 obj->m_known_by_count--;
1291 // Handle added objects
1292 writeU16((u8*)buf, added_objects.size());
1293 data_buffer.append(buf, 2);
1294 for(core::map<u16, bool>::Iterator
1295 i = added_objects.getIterator();
1296 i.atEnd()==false; i++)
1299 u16 id = i.getNode()->getKey();
1300 ServerActiveObject* obj = m_env.getActiveObject(id);
1303 u8 type = ACTIVEOBJECT_TYPE_INVALID;
1305 dstream<<"WARNING: "<<__FUNCTION_NAME
1306 <<": NULL object"<<std::endl;
1308 type = obj->getType();
1310 // Add to data buffer for sending
1311 writeU16((u8*)buf, id);
1312 data_buffer.append(buf, 2);
1313 writeU8((u8*)buf, type);
1314 data_buffer.append(buf, 1);
1316 data_buffer.append(serializeLongString(
1317 obj->getClientInitializationData()));
1319 // Add to known objects
1320 client->m_known_objects.insert(i.getNode()->getKey(), false);
1323 obj->m_known_by_count++;
1327 SharedBuffer<u8> reply(2 + data_buffer.size());
1328 writeU16(&reply[0], TOCLIENT_ACTIVE_OBJECT_REMOVE_ADD);
1329 memcpy((char*)&reply[2], data_buffer.c_str(),
1330 data_buffer.size());
1332 m_con.Send(client->peer_id, 0, reply, true);
1334 dstream<<"INFO: Server: Sent object remove/add: "
1335 <<removed_objects.size()<<" removed, "
1336 <<added_objects.size()<<" added, "
1337 <<"packet size is "<<reply.getSize()<<std::endl;
1342 Send object messages
1345 JMutexAutoLock envlock(m_env_mutex);
1346 JMutexAutoLock conlock(m_con_mutex);
1349 // Value = data sent by object
1350 core::map<u16, core::list<ActiveObjectMessage>* > buffered_messages;
1352 // Get active object messages from environment
1355 ActiveObjectMessage aom = m_env.getActiveObjectMessage();
1359 core::list<ActiveObjectMessage>* message_list = NULL;
1360 core::map<u16, core::list<ActiveObjectMessage>* >::Node *n;
1361 n = buffered_messages.find(aom.id);
1364 message_list = new core::list<ActiveObjectMessage>;
1365 buffered_messages.insert(aom.id, message_list);
1369 message_list = n->getValue();
1371 message_list->push_back(aom);
1374 // Route data to every client
1375 for(core::map<u16, RemoteClient*>::Iterator
1376 i = m_clients.getIterator();
1377 i.atEnd()==false; i++)
1379 RemoteClient *client = i.getNode()->getValue();
1380 std::string reliable_data;
1381 std::string unreliable_data;
1382 // Go through all objects in message buffer
1383 for(core::map<u16, core::list<ActiveObjectMessage>* >::Iterator
1384 j = buffered_messages.getIterator();
1385 j.atEnd()==false; j++)
1387 // If object is not known by client, skip it
1388 u16 id = j.getNode()->getKey();
1389 if(client->m_known_objects.find(id) == NULL)
1391 // Get message list of object
1392 core::list<ActiveObjectMessage>* list = j.getNode()->getValue();
1393 // Go through every message
1394 for(core::list<ActiveObjectMessage>::Iterator
1395 k = list->begin(); k != list->end(); k++)
1397 // Compose the full new data with header
1398 ActiveObjectMessage aom = *k;
1399 std::string new_data;
1402 writeU16((u8*)&buf[0], aom.id);
1403 new_data.append(buf, 2);
1405 new_data += serializeString(aom.datastring);
1406 // Add data to buffer
1408 reliable_data += new_data;
1410 unreliable_data += new_data;
1414 reliable_data and unreliable_data are now ready.
1417 if(reliable_data.size() > 0)
1419 SharedBuffer<u8> reply(2 + reliable_data.size());
1420 writeU16(&reply[0], TOCLIENT_ACTIVE_OBJECT_MESSAGES);
1421 memcpy((char*)&reply[2], reliable_data.c_str(),
1422 reliable_data.size());
1424 m_con.Send(client->peer_id, 0, reply, true);
1426 if(unreliable_data.size() > 0)
1428 SharedBuffer<u8> reply(2 + unreliable_data.size());
1429 writeU16(&reply[0], TOCLIENT_ACTIVE_OBJECT_MESSAGES);
1430 memcpy((char*)&reply[2], unreliable_data.c_str(),
1431 unreliable_data.size());
1432 // Send as unreliable
1433 m_con.Send(client->peer_id, 0, reply, false);
1436 /*if(reliable_data.size() > 0 || unreliable_data.size() > 0)
1438 dstream<<"INFO: Server: Size of object message data: "
1439 <<"reliable: "<<reliable_data.size()
1440 <<", unreliable: "<<unreliable_data.size()
1445 // Clear buffered_messages
1446 for(core::map<u16, core::list<ActiveObjectMessage>* >::Iterator
1447 i = buffered_messages.getIterator();
1448 i.atEnd()==false; i++)
1450 delete i.getNode()->getValue();
1454 } // enable_experimental
1457 Send queued-for-sending map edit events.
1460 while(m_unsent_map_edit_queue.size() != 0)
1462 MapEditEvent* event = m_unsent_map_edit_queue.pop_front();
1464 if(event->type == MEET_ADDNODE)
1466 dstream<<"Server: MEET_ADDNODE"<<std::endl;
1467 sendAddNode(event->p, event->n, event->already_known_by_peer);
1469 else if(event->type == MEET_REMOVENODE)
1471 dstream<<"Server: MEET_REMOVENODE"<<std::endl;
1472 sendRemoveNode(event->p, event->already_known_by_peer);
1474 else if(event->type == MEET_OTHER)
1476 dstream<<"WARNING: Server: MEET_OTHER not implemented"
1481 dstream<<"WARNING: Server: Unknown MapEditEvent "
1482 <<((u32)event->type)<<std::endl;
1490 Send object positions
1491 TODO: Get rid of MapBlockObjects
1494 float &counter = m_objectdata_timer;
1496 if(counter >= g_settings.getFloat("objectdata_interval"))
1498 JMutexAutoLock lock1(m_env_mutex);
1499 JMutexAutoLock lock2(m_con_mutex);
1500 SendObjectData(counter);
1507 Trigger emergethread (it somehow gets to a non-triggered but
1508 bysy state sometimes)
1511 float &counter = m_emergethread_trigger_timer;
1517 m_emergethread.trigger();
1523 float &counter = m_savemap_timer;
1525 if(counter >= g_settings.getFloat("server_map_save_interval"))
1529 JMutexAutoLock lock(m_env_mutex);
1531 if(((ServerMap*)(&m_env.getMap()))->isSavingEnabled() == true)
1533 // Save only changed parts
1534 m_env.getMap().save(true);
1536 // Delete unused sectors
1537 u32 deleted_count = m_env.getMap().deleteUnusedSectors(
1538 g_settings.getFloat("server_unload_unused_sectors_timeout"));
1539 if(deleted_count > 0)
1541 dout_server<<"Server: Unloaded "<<deleted_count
1542 <<" sectors from memory"<<std::endl;
1546 m_env.serializePlayers(m_mapsavedir);
1552 void Server::Receive()
1554 DSTACK(__FUNCTION_NAME);
1555 u32 data_maxsize = 10000;
1556 Buffer<u8> data(data_maxsize);
1561 JMutexAutoLock conlock(m_con_mutex);
1562 datasize = m_con.Receive(peer_id, *data, data_maxsize);
1565 // This has to be called so that the client list gets synced
1566 // with the peer list of the connection
1567 handlePeerChanges();
1569 ProcessData(*data, datasize, peer_id);
1571 catch(con::InvalidIncomingDataException &e)
1573 derr_server<<"Server::Receive(): "
1574 "InvalidIncomingDataException: what()="
1575 <<e.what()<<std::endl;
1577 catch(con::PeerNotFoundException &e)
1579 //NOTE: This is not needed anymore
1581 // The peer has been disconnected.
1582 // Find the associated player and remove it.
1584 /*JMutexAutoLock envlock(m_env_mutex);
1586 dout_server<<"ServerThread: peer_id="<<peer_id
1587 <<" has apparently closed connection. "
1588 <<"Removing player."<<std::endl;
1590 m_env.removePlayer(peer_id);*/
1594 void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
1596 DSTACK(__FUNCTION_NAME);
1597 // Environment is locked first.
1598 JMutexAutoLock envlock(m_env_mutex);
1599 JMutexAutoLock conlock(m_con_mutex);
1603 peer = m_con.GetPeer(peer_id);
1605 catch(con::PeerNotFoundException &e)
1607 derr_server<<DTIME<<"Server::ProcessData(): Cancelling: peer "
1608 <<peer_id<<" not found"<<std::endl;
1612 u8 peer_ser_ver = getClient(peer->id)->serialization_version;
1620 ToServerCommand command = (ToServerCommand)readU16(&data[0]);
1622 if(command == TOSERVER_INIT)
1624 // [0] u16 TOSERVER_INIT
1625 // [2] u8 SER_FMT_VER_HIGHEST
1626 // [3] u8[20] player_name
1631 derr_server<<DTIME<<"Server: Got TOSERVER_INIT from "
1632 <<peer->id<<std::endl;
1634 // First byte after command is maximum supported
1635 // serialization version
1636 u8 client_max = data[2];
1637 u8 our_max = SER_FMT_VER_HIGHEST;
1638 // Use the highest version supported by both
1639 u8 deployed = core::min_(client_max, our_max);
1640 // If it's lower than the lowest supported, give up.
1641 if(deployed < SER_FMT_VER_LOWEST)
1642 deployed = SER_FMT_VER_INVALID;
1644 //peer->serialization_version = deployed;
1645 getClient(peer->id)->pending_serialization_version = deployed;
1647 if(deployed == SER_FMT_VER_INVALID)
1649 derr_server<<DTIME<<"Server: Cannot negotiate "
1650 "serialization version with peer "
1651 <<peer_id<<std::endl;
1660 const u32 playername_size = 20;
1661 char playername[playername_size];
1662 for(u32 i=0; i<playername_size-1; i++)
1664 playername[i] = data[3+i];
1666 playername[playername_size-1] = 0;
1669 Player *player = emergePlayer(playername, "", peer_id);
1670 //Player *player = m_env.getPlayer(peer_id);
1673 // DEBUG: Test serialization
1674 std::ostringstream test_os;
1675 player->serialize(test_os);
1676 dstream<<"Player serialization test: \""<<test_os.str()
1678 std::istringstream test_is(test_os.str());
1679 player->deSerialize(test_is);
1682 // If failed, cancel
1685 derr_server<<DTIME<<"Server: peer_id="<<peer_id
1686 <<": failed to emerge player"<<std::endl;
1691 // If a client is already connected to the player, cancel
1692 if(player->peer_id != 0)
1694 derr_server<<DTIME<<"Server: peer_id="<<peer_id
1695 <<" tried to connect to "
1696 "an already connected player (peer_id="
1697 <<player->peer_id<<")"<<std::endl;
1700 // Set client of player
1701 player->peer_id = peer_id;
1704 // Check if player doesn't exist
1706 throw con::InvalidIncomingDataException
1707 ("Server::ProcessData(): INIT: Player doesn't exist");
1709 /*// update name if it was supplied
1710 if(datasize >= 20+3)
1713 player->updateName((const char*)&data[3]);
1716 // Now answer with a TOCLIENT_INIT
1718 SharedBuffer<u8> reply(2+1+6+8);
1719 writeU16(&reply[0], TOCLIENT_INIT);
1720 writeU8(&reply[2], deployed);
1721 writeV3S16(&reply[2+1], floatToInt(player->getPosition()+v3f(0,BS/2,0), BS));
1722 //writeU64(&reply[2+1+6], m_env.getServerMap().getSeed());
1725 m_con.Send(peer_id, 0, reply, true);
1729 if(command == TOSERVER_INIT2)
1731 derr_server<<DTIME<<"Server: Got TOSERVER_INIT2 from "
1732 <<peer->id<<std::endl;
1735 getClient(peer->id)->serialization_version
1736 = getClient(peer->id)->pending_serialization_version;
1739 Send some initialization data
1742 // Send player info to all players
1745 // Send inventory to player
1746 SendInventory(peer->id);
1750 SharedBuffer<u8> data = makePacket_TOCLIENT_TIME_OF_DAY(
1751 m_time_of_day.get());
1752 m_con.Send(peer->id, 0, data, true);
1755 // Send information about server to player in chat
1756 SendChatMessage(peer_id, getStatusString());
1758 // Send information about joining in chat
1760 std::wstring name = L"unknown";
1761 Player *player = m_env.getPlayer(peer_id);
1763 name = narrow_to_wide(player->getName());
1765 std::wstring message;
1768 message += L" joined game";
1769 BroadcastChatMessage(message);
1775 if(peer_ser_ver == SER_FMT_VER_INVALID)
1777 derr_server<<DTIME<<"Server::ProcessData(): Cancelling: Peer"
1778 " serialization format invalid or not initialized."
1779 " Skipping incoming command="<<command<<std::endl;
1783 Player *player = m_env.getPlayer(peer_id);
1786 derr_server<<"Server::ProcessData(): Cancelling: "
1787 "No player for peer_id="<<peer_id
1791 if(command == TOSERVER_PLAYERPOS)
1793 if(datasize < 2+12+12+4+4)
1797 v3s32 ps = readV3S32(&data[start+2]);
1798 v3s32 ss = readV3S32(&data[start+2+12]);
1799 f32 pitch = (f32)readS32(&data[2+12+12]) / 100.0;
1800 f32 yaw = (f32)readS32(&data[2+12+12+4]) / 100.0;
1801 v3f position((f32)ps.X/100., (f32)ps.Y/100., (f32)ps.Z/100.);
1802 v3f speed((f32)ss.X/100., (f32)ss.Y/100., (f32)ss.Z/100.);
1803 pitch = wrapDegrees(pitch);
1804 yaw = wrapDegrees(yaw);
1805 player->setPosition(position);
1806 player->setSpeed(speed);
1807 player->setPitch(pitch);
1808 player->setYaw(yaw);
1810 /*dout_server<<"Server::ProcessData(): Moved player "<<peer_id<<" to "
1811 <<"("<<position.X<<","<<position.Y<<","<<position.Z<<")"
1812 <<" pitch="<<pitch<<" yaw="<<yaw<<std::endl;*/
1814 else if(command == TOSERVER_GOTBLOCKS)
1827 u16 count = data[2];
1828 for(u16 i=0; i<count; i++)
1830 if((s16)datasize < 2+1+(i+1)*6)
1831 throw con::InvalidIncomingDataException
1832 ("GOTBLOCKS length is too short");
1833 v3s16 p = readV3S16(&data[2+1+i*6]);
1834 /*dstream<<"Server: GOTBLOCKS ("
1835 <<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;*/
1836 RemoteClient *client = getClient(peer_id);
1837 client->GotBlock(p);
1840 else if(command == TOSERVER_DELETEDBLOCKS)
1853 u16 count = data[2];
1854 for(u16 i=0; i<count; i++)
1856 if((s16)datasize < 2+1+(i+1)*6)
1857 throw con::InvalidIncomingDataException
1858 ("DELETEDBLOCKS length is too short");
1859 v3s16 p = readV3S16(&data[2+1+i*6]);
1860 /*dstream<<"Server: DELETEDBLOCKS ("
1861 <<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;*/
1862 RemoteClient *client = getClient(peer_id);
1863 client->SetBlockNotSent(p);
1866 else if(command == TOSERVER_CLICK_OBJECT)
1873 [2] u8 button (0=left, 1=right)
1878 u8 button = readU8(&data[2]);
1880 p.X = readS16(&data[3]);
1881 p.Y = readS16(&data[5]);
1882 p.Z = readS16(&data[7]);
1883 s16 id = readS16(&data[9]);
1884 //u16 item_i = readU16(&data[11]);
1886 MapBlock *block = NULL;
1889 block = m_env.getMap().getBlockNoCreate(p);
1891 catch(InvalidPositionException &e)
1893 derr_server<<"CLICK_OBJECT block not found"<<std::endl;
1897 MapBlockObject *obj = block->getObject(id);
1901 derr_server<<"CLICK_OBJECT object not found"<<std::endl;
1905 //TODO: Check that object is reasonably close
1910 InventoryList *ilist = player->inventory.getList("main");
1911 if(g_settings.getBool("creative_mode") == false && ilist != NULL)
1914 // Skip if inventory has no free space
1915 if(ilist->getUsedSlots() == ilist->getSize())
1917 dout_server<<"Player inventory has no free space"<<std::endl;
1922 Create the inventory item
1924 InventoryItem *item = NULL;
1925 // If it is an item-object, take the item from it
1926 if(obj->getTypeId() == MAPBLOCKOBJECT_TYPE_ITEM)
1928 item = ((ItemObject*)obj)->createInventoryItem();
1930 // Else create an item of the object
1933 item = new MapBlockObjectItem
1934 (obj->getInventoryString());
1937 // Add to inventory and send inventory
1938 ilist->addItem(item);
1939 SendInventory(player->peer_id);
1942 // Remove from block
1943 block->removeObject(id);
1946 else if(command == TOSERVER_GROUND_ACTION)
1954 [3] v3s16 nodepos_undersurface
1955 [9] v3s16 nodepos_abovesurface
1960 2: stop digging (all parameters ignored)
1961 3: digging completed
1963 u8 action = readU8(&data[2]);
1965 p_under.X = readS16(&data[3]);
1966 p_under.Y = readS16(&data[5]);
1967 p_under.Z = readS16(&data[7]);
1969 p_over.X = readS16(&data[9]);
1970 p_over.Y = readS16(&data[11]);
1971 p_over.Z = readS16(&data[13]);
1972 u16 item_i = readU16(&data[15]);
1974 //TODO: Check that target is reasonably close
1982 NOTE: This can be used in the future to check if
1983 somebody is cheating, by checking the timing.
1990 else if(action == 2)
1993 RemoteClient *client = getClient(peer->id);
1994 JMutexAutoLock digmutex(client->m_dig_mutex);
1995 client->m_dig_tool_item = -1;
2000 3: Digging completed
2002 else if(action == 3)
2004 // Mandatory parameter; actually used for nothing
2005 core::map<v3s16, MapBlock*> modified_blocks;
2008 u8 mineral = MINERAL_NONE;
2012 MapNode n = m_env.getMap().getNode(p_under);
2013 // Get material at position
2015 // If it's not diggable, do nothing
2016 if(content_diggable(material) == false)
2018 derr_server<<"Server: Not finishing digging: Node not diggable"
2021 // Client probably has wrong data.
2022 // Set block not sent, so that client will get
2024 dstream<<"Client "<<peer_id<<" tried to dig "
2025 <<"node from invalid position; setting"
2026 <<" MapBlock not sent."<<std::endl;
2027 RemoteClient *client = getClient(peer_id);
2028 v3s16 blockpos = getNodeBlockPos(p_under);
2029 client->SetBlockNotSent(blockpos);
2034 mineral = n.getMineral();
2036 catch(InvalidPositionException &e)
2038 derr_server<<"Server: Not finishing digging: Node not found."
2039 <<" Adding block to emerge queue."
2041 m_emerge_queue.addBlock(peer_id,
2042 getNodeBlockPos(p_over), BLOCK_EMERGE_FLAG_FROMDISK);
2047 Send the removal to all other clients
2049 sendRemoveNode(p_under, peer_id);
2052 Update and send inventory
2055 if(g_settings.getBool("creative_mode") == false)
2060 InventoryList *mlist = player->inventory.getList("main");
2063 InventoryItem *item = mlist->getItem(item_i);
2064 if(item && (std::string)item->getName() == "ToolItem")
2066 ToolItem *titem = (ToolItem*)item;
2067 std::string toolname = titem->getToolName();
2069 // Get digging properties for material and tool
2070 DiggingProperties prop =
2071 getDiggingProperties(material, toolname);
2073 if(prop.diggable == false)
2075 derr_server<<"Server: WARNING: Player digged"
2076 <<" with impossible material + tool"
2077 <<" combination"<<std::endl;
2080 bool weared_out = titem->addWear(prop.wear);
2084 mlist->deleteItem(item_i);
2090 Add dug item to inventory
2093 InventoryItem *item = NULL;
2095 if(mineral != MINERAL_NONE)
2096 item = getDiggedMineralItem(mineral);
2101 std::string &dug_s = content_features(material).dug_item;
2104 std::istringstream is(dug_s, std::ios::binary);
2105 item = InventoryItem::deSerialize(is);
2111 // Add a item to inventory
2112 player->inventory.addItem("main", item);
2115 SendInventory(player->peer_id);
2121 (this takes some time so it is done after the quick stuff)
2123 m_ignore_map_edit_events = true;
2124 m_env.getMap().removeNodeAndUpdate(p_under, modified_blocks);
2125 m_ignore_map_edit_events = false;
2131 else if(action == 1)
2134 InventoryList *ilist = player->inventory.getList("main");
2139 InventoryItem *item = ilist->getItem(item_i);
2141 // If there is no item, it is not possible to add it anywhere
2146 Handle material items
2148 if(std::string("MaterialItem") == item->getName())
2151 // Don't add a node if this is not a free space
2152 MapNode n2 = m_env.getMap().getNode(p_over);
2153 if(content_buildable_to(n2.d) == false)
2155 // Client probably has wrong data.
2156 // Set block not sent, so that client will get
2158 dstream<<"Client "<<peer_id<<" tried to place"
2159 <<" node in invalid position; setting"
2160 <<" MapBlock not sent."<<std::endl;
2161 RemoteClient *client = getClient(peer_id);
2162 v3s16 blockpos = getNodeBlockPos(p_over);
2163 client->SetBlockNotSent(blockpos);
2167 catch(InvalidPositionException &e)
2169 derr_server<<"Server: Ignoring ADDNODE: Node not found"
2170 <<" Adding block to emerge queue."
2172 m_emerge_queue.addBlock(peer_id,
2173 getNodeBlockPos(p_over), BLOCK_EMERGE_FLAG_FROMDISK);
2177 // Reset build time counter
2178 getClient(peer->id)->m_time_from_building = 0.0;
2181 MaterialItem *mitem = (MaterialItem*)item;
2183 n.d = mitem->getMaterial();
2184 if(content_features(n.d).wall_mounted)
2185 n.dir = packDir(p_under - p_over);
2190 sendAddNode(p_over, n, 0);
2195 InventoryList *ilist = player->inventory.getList("main");
2196 if(g_settings.getBool("creative_mode") == false && ilist)
2198 // Remove from inventory and send inventory
2199 if(mitem->getCount() == 1)
2200 ilist->deleteItem(item_i);
2204 SendInventory(peer_id);
2210 This takes some time so it is done after the quick stuff
2212 core::map<v3s16, MapBlock*> modified_blocks;
2213 m_ignore_map_edit_events = true;
2214 m_env.getMap().addNodeAndUpdate(p_over, n, modified_blocks);
2215 m_ignore_map_edit_events = false;
2218 Calculate special events
2221 /*if(n.d == CONTENT_MESE)
2224 for(s16 z=-1; z<=1; z++)
2225 for(s16 y=-1; y<=1; y++)
2226 for(s16 x=-1; x<=1; x++)
2237 v3s16 blockpos = getNodeBlockPos(p_over);
2239 MapBlock *block = NULL;
2242 block = m_env.getMap().getBlockNoCreate(blockpos);
2244 catch(InvalidPositionException &e)
2246 derr_server<<"Error while placing object: "
2247 "block not found"<<std::endl;
2251 v3s16 block_pos_i_on_map = block->getPosRelative();
2252 v3f block_pos_f_on_map = intToFloat(block_pos_i_on_map, BS);
2254 v3f pos = intToFloat(p_over, BS);
2255 pos -= block_pos_f_on_map;
2257 /*dout_server<<"pos="
2258 <<"("<<pos.X<<","<<pos.Y<<","<<pos.Z<<")"
2261 MapBlockObject *obj = NULL;
2264 Handle block object items
2266 if(std::string("MBOItem") == item->getName())
2268 MapBlockObjectItem *oitem = (MapBlockObjectItem*)item;
2270 /*dout_server<<"Trying to place a MapBlockObjectItem: "
2271 "inventorystring=\""
2272 <<oitem->getInventoryString()
2273 <<"\""<<std::endl;*/
2275 obj = oitem->createObject
2276 (pos, player->getYaw(), player->getPitch());
2283 dout_server<<"Placing a miscellaneous item on map"
2286 Create an ItemObject that contains the item.
2288 ItemObject *iobj = new ItemObject(NULL, -1, pos);
2289 std::ostringstream os(std::ios_base::binary);
2290 item->serialize(os);
2291 dout_server<<"Item string is \""<<os.str()<<"\""<<std::endl;
2292 iobj->setItemString(os.str());
2298 derr_server<<"WARNING: item resulted in NULL object, "
2299 <<"not placing onto map"
2304 block->addObject(obj);
2306 dout_server<<"Placed object"<<std::endl;
2308 InventoryList *ilist = player->inventory.getList("main");
2309 if(g_settings.getBool("creative_mode") == false && ilist)
2311 // Remove from inventory and send inventory
2312 ilist->deleteItem(item_i);
2314 SendInventory(peer_id);
2322 Catch invalid actions
2326 derr_server<<"WARNING: Server: Invalid action "
2327 <<action<<std::endl;
2331 else if(command == TOSERVER_RELEASE)
2340 dstream<<"TOSERVER_RELEASE ignored"<<std::endl;
2343 else if(command == TOSERVER_SIGNTEXT)
2352 std::string datastring((char*)&data[2], datasize-2);
2353 std::istringstream is(datastring, std::ios_base::binary);
2356 is.read((char*)buf, 6);
2357 v3s16 blockpos = readV3S16(buf);
2358 is.read((char*)buf, 2);
2359 s16 id = readS16(buf);
2360 is.read((char*)buf, 2);
2361 u16 textlen = readU16(buf);
2363 for(u16 i=0; i<textlen; i++)
2365 is.read((char*)buf, 1);
2366 text += (char)buf[0];
2369 MapBlock *block = NULL;
2372 block = m_env.getMap().getBlockNoCreate(blockpos);
2374 catch(InvalidPositionException &e)
2376 derr_server<<"Error while setting sign text: "
2377 "block not found"<<std::endl;
2381 MapBlockObject *obj = block->getObject(id);
2384 derr_server<<"Error while setting sign text: "
2385 "object not found"<<std::endl;
2389 if(obj->getTypeId() != MAPBLOCKOBJECT_TYPE_SIGN)
2391 derr_server<<"Error while setting sign text: "
2392 "object is not a sign"<<std::endl;
2396 ((SignObject*)obj)->setText(text);
2398 obj->getBlock()->setChangedFlag();
2400 else if(command == TOSERVER_SIGNNODETEXT)
2408 std::string datastring((char*)&data[2], datasize-2);
2409 std::istringstream is(datastring, std::ios_base::binary);
2412 is.read((char*)buf, 6);
2413 v3s16 p = readV3S16(buf);
2414 is.read((char*)buf, 2);
2415 u16 textlen = readU16(buf);
2417 for(u16 i=0; i<textlen; i++)
2419 is.read((char*)buf, 1);
2420 text += (char)buf[0];
2423 NodeMetadata *meta = m_env.getMap().getNodeMetadata(p);
2426 if(meta->typeId() != CONTENT_SIGN_WALL)
2428 SignNodeMetadata *signmeta = (SignNodeMetadata*)meta;
2429 signmeta->setText(text);
2431 v3s16 blockpos = getNodeBlockPos(p);
2432 MapBlock *block = m_env.getMap().getBlockNoCreateNoEx(blockpos);
2435 block->setChangedFlag();
2438 for(core::map<u16, RemoteClient*>::Iterator
2439 i = m_clients.getIterator();
2440 i.atEnd()==false; i++)
2442 RemoteClient *client = i.getNode()->getValue();
2443 client->SetBlockNotSent(blockpos);
2446 else if(command == TOSERVER_INVENTORY_ACTION)
2448 /*// Ignore inventory changes if in creative mode
2449 if(g_settings.getBool("creative_mode") == true)
2451 dstream<<"TOSERVER_INVENTORY_ACTION: ignoring in creative mode"
2455 // Strip command and create a stream
2456 std::string datastring((char*)&data[2], datasize-2);
2457 dstream<<"TOSERVER_INVENTORY_ACTION: data="<<datastring<<std::endl;
2458 std::istringstream is(datastring, std::ios_base::binary);
2460 InventoryAction *a = InventoryAction::deSerialize(is);
2464 Handle craftresult specially if not in creative mode
2466 bool disable_action = false;
2467 if(a->getType() == IACTION_MOVE
2468 && g_settings.getBool("creative_mode") == false)
2470 IMoveAction *ma = (IMoveAction*)a;
2471 // Don't allow moving anything to craftresult
2472 if(ma->to_name == "craftresult")
2475 disable_action = true;
2477 // When something is removed from craftresult
2478 if(ma->from_name == "craftresult")
2480 disable_action = true;
2481 // Remove stuff from craft
2482 InventoryList *clist = player->inventory.getList("craft");
2485 u16 count = ma->count;
2488 clist->decrementMaterials(count);
2491 // Feed action to player inventory
2492 a->apply(&player->inventory);
2495 // If something appeared in craftresult, throw it
2497 InventoryList *rlist = player->inventory.getList("craftresult");
2498 InventoryList *mlist = player->inventory.getList("main");
2499 if(rlist && mlist && rlist->getUsedSlots() == 1)
2501 InventoryItem *item1 = rlist->changeItem(0, NULL);
2502 mlist->addItem(item1);
2506 if(disable_action == false)
2508 // Feed action to player inventory
2509 a->apply(&player->inventory);
2514 SendInventory(player->peer_id);
2518 dstream<<"TOSERVER_INVENTORY_ACTION: "
2519 <<"InventoryAction::deSerialize() returned NULL"
2523 else if(command == TOSERVER_CHAT_MESSAGE)
2531 std::string datastring((char*)&data[2], datasize-2);
2532 std::istringstream is(datastring, std::ios_base::binary);
2535 is.read((char*)buf, 2);
2536 u16 len = readU16(buf);
2538 std::wstring message;
2539 for(u16 i=0; i<len; i++)
2541 is.read((char*)buf, 2);
2542 message += (wchar_t)readU16(buf);
2545 // Get player name of this client
2546 std::wstring name = narrow_to_wide(player->getName());
2548 // Line to send to players
2550 // Whether to send to the player that sent the line
2551 bool send_to_sender = false;
2552 // Whether to send to other players
2553 bool send_to_others = false;
2556 std::wstring commandprefix = L"/#";
2557 if(message.substr(0, commandprefix.size()) == commandprefix)
2559 line += L"Server: ";
2561 message = message.substr(commandprefix.size());
2562 // Get player name as narrow string
2563 std::string name_s = player->getName();
2564 // Convert message to narrow string
2565 std::string message_s = wide_to_narrow(message);
2566 // Operator is the single name defined in config.
2567 std::string operator_name = g_settings.get("name");
2568 bool is_operator = (operator_name != "" &&
2569 wide_to_narrow(name) == operator_name);
2570 bool valid_command = false;
2571 if(message_s == "help")
2573 line += L"-!- Available commands: ";
2577 line += L"shutdown setting ";
2582 send_to_sender = true;
2583 valid_command = true;
2585 else if(message_s == "status")
2587 line = getStatusString();
2588 send_to_sender = true;
2589 valid_command = true;
2591 else if(is_operator)
2593 if(message_s == "shutdown")
2595 dstream<<DTIME<<" Server: Operator requested shutdown."
2597 m_shutdown_requested.set(true);
2599 line += L"*** Server shutting down (operator request)";
2600 send_to_sender = true;
2601 valid_command = true;
2603 else if(message_s.substr(0,8) == "setting ")
2605 std::string confline = message_s.substr(8);
2606 g_settings.parseConfigLine(confline);
2607 line += L"-!- Setting changed.";
2608 send_to_sender = true;
2609 valid_command = true;
2613 if(valid_command == false)
2615 line += L"-!- Invalid command: " + message;
2616 send_to_sender = true;
2627 send_to_others = true;
2632 dstream<<"CHAT: "<<wide_to_narrow(line)<<std::endl;
2635 Send the message to clients
2637 for(core::map<u16, RemoteClient*>::Iterator
2638 i = m_clients.getIterator();
2639 i.atEnd() == false; i++)
2641 // Get client and check that it is valid
2642 RemoteClient *client = i.getNode()->getValue();
2643 assert(client->peer_id == i.getNode()->getKey());
2644 if(client->serialization_version == SER_FMT_VER_INVALID)
2648 bool sender_selected = (peer_id == client->peer_id);
2649 if(sender_selected == true && send_to_sender == false)
2651 if(sender_selected == false && send_to_others == false)
2654 SendChatMessage(client->peer_id, line);
2660 derr_server<<"WARNING: Server::ProcessData(): Ignoring "
2661 "unknown command "<<command<<std::endl;
2665 catch(SendFailedException &e)
2667 derr_server<<"Server::ProcessData(): SendFailedException: "
2673 void Server::onMapEditEvent(MapEditEvent *event)
2675 dstream<<"Server::onMapEditEvent()"<<std::endl;
2676 if(m_ignore_map_edit_events)
2678 MapEditEvent *e = event->clone();
2679 m_unsent_map_edit_queue.push_back(e);
2682 core::list<PlayerInfo> Server::getPlayerInfo()
2684 DSTACK(__FUNCTION_NAME);
2685 JMutexAutoLock envlock(m_env_mutex);
2686 JMutexAutoLock conlock(m_con_mutex);
2688 core::list<PlayerInfo> list;
2690 core::list<Player*> players = m_env.getPlayers();
2692 core::list<Player*>::Iterator i;
2693 for(i = players.begin();
2694 i != players.end(); i++)
2698 Player *player = *i;
2701 con::Peer *peer = m_con.GetPeer(player->peer_id);
2702 // Copy info from peer to info struct
2704 info.address = peer->address;
2705 info.avg_rtt = peer->avg_rtt;
2707 catch(con::PeerNotFoundException &e)
2709 // Set dummy peer info
2711 info.address = Address(0,0,0,0,0);
2715 snprintf(info.name, PLAYERNAME_SIZE, "%s", player->getName());
2716 info.position = player->getPosition();
2718 list.push_back(info);
2725 void Server::peerAdded(con::Peer *peer)
2727 DSTACK(__FUNCTION_NAME);
2728 dout_server<<"Server::peerAdded(): peer->id="
2729 <<peer->id<<std::endl;
2732 c.type = PEER_ADDED;
2733 c.peer_id = peer->id;
2735 m_peer_change_queue.push_back(c);
2738 void Server::deletingPeer(con::Peer *peer, bool timeout)
2740 DSTACK(__FUNCTION_NAME);
2741 dout_server<<"Server::deletingPeer(): peer->id="
2742 <<peer->id<<", timeout="<<timeout<<std::endl;
2745 c.type = PEER_REMOVED;
2746 c.peer_id = peer->id;
2747 c.timeout = timeout;
2748 m_peer_change_queue.push_back(c);
2751 void Server::SendObjectData(float dtime)
2753 DSTACK(__FUNCTION_NAME);
2755 core::map<v3s16, bool> stepped_blocks;
2757 for(core::map<u16, RemoteClient*>::Iterator
2758 i = m_clients.getIterator();
2759 i.atEnd() == false; i++)
2761 u16 peer_id = i.getNode()->getKey();
2762 RemoteClient *client = i.getNode()->getValue();
2763 assert(client->peer_id == peer_id);
2765 if(client->serialization_version == SER_FMT_VER_INVALID)
2768 client->SendObjectData(this, dtime, stepped_blocks);
2772 void Server::SendPlayerInfos()
2774 DSTACK(__FUNCTION_NAME);
2776 //JMutexAutoLock envlock(m_env_mutex);
2778 // Get connected players
2779 core::list<Player*> players = m_env.getPlayers(true);
2781 u32 player_count = players.getSize();
2782 u32 datasize = 2+(2+PLAYERNAME_SIZE)*player_count;
2784 SharedBuffer<u8> data(datasize);
2785 writeU16(&data[0], TOCLIENT_PLAYERINFO);
2788 core::list<Player*>::Iterator i;
2789 for(i = players.begin();
2790 i != players.end(); i++)
2792 Player *player = *i;
2794 /*dstream<<"Server sending player info for player with "
2795 "peer_id="<<player->peer_id<<std::endl;*/
2797 writeU16(&data[start], player->peer_id);
2798 memset((char*)&data[start+2], 0, PLAYERNAME_SIZE);
2799 snprintf((char*)&data[start+2], PLAYERNAME_SIZE, "%s", player->getName());
2800 start += 2+PLAYERNAME_SIZE;
2803 //JMutexAutoLock conlock(m_con_mutex);
2806 m_con.SendToAll(0, data, true);
2810 Craft checking system
2828 ItemSpec(enum ItemSpecType a_type, std::string a_name):
2834 ItemSpec(enum ItemSpecType a_type, u16 a_num):
2840 enum ItemSpecType type;
2841 // Only other one of these is used
2847 items: a pointer to an array of 9 pointers to items
2848 specs: a pointer to an array of 9 ItemSpecs
2850 bool checkItemCombination(InventoryItem **items, ItemSpec *specs)
2852 u16 items_min_x = 100;
2853 u16 items_max_x = 100;
2854 u16 items_min_y = 100;
2855 u16 items_max_y = 100;
2856 for(u16 y=0; y<3; y++)
2857 for(u16 x=0; x<3; x++)
2859 if(items[y*3 + x] == NULL)
2861 if(items_min_x == 100 || x < items_min_x)
2863 if(items_min_y == 100 || y < items_min_y)
2865 if(items_max_x == 100 || x > items_max_x)
2867 if(items_max_y == 100 || y > items_max_y)
2870 // No items at all, just return false
2871 if(items_min_x == 100)
2874 u16 items_w = items_max_x - items_min_x + 1;
2875 u16 items_h = items_max_y - items_min_y + 1;
2877 u16 specs_min_x = 100;
2878 u16 specs_max_x = 100;
2879 u16 specs_min_y = 100;
2880 u16 specs_max_y = 100;
2881 for(u16 y=0; y<3; y++)
2882 for(u16 x=0; x<3; x++)
2884 if(specs[y*3 + x].type == ITEM_NONE)
2886 if(specs_min_x == 100 || x < specs_min_x)
2888 if(specs_min_y == 100 || y < specs_min_y)
2890 if(specs_max_x == 100 || x > specs_max_x)
2892 if(specs_max_y == 100 || y > specs_max_y)
2895 // No specs at all, just return false
2896 if(specs_min_x == 100)
2899 u16 specs_w = specs_max_x - specs_min_x + 1;
2900 u16 specs_h = specs_max_y - specs_min_y + 1;
2903 if(items_w != specs_w || items_h != specs_h)
2906 for(u16 y=0; y<specs_h; y++)
2907 for(u16 x=0; x<specs_w; x++)
2909 u16 items_x = items_min_x + x;
2910 u16 items_y = items_min_y + y;
2911 u16 specs_x = specs_min_x + x;
2912 u16 specs_y = specs_min_y + y;
2913 InventoryItem *item = items[items_y * 3 + items_x];
2914 ItemSpec &spec = specs[specs_y * 3 + specs_x];
2916 if(spec.type == ITEM_NONE)
2918 // Has to be no item
2924 // There should be an item
2928 std::string itemname = item->getName();
2930 if(spec.type == ITEM_MATERIAL)
2932 if(itemname != "MaterialItem")
2934 MaterialItem *mitem = (MaterialItem*)item;
2935 if(mitem->getMaterial() != spec.num)
2938 else if(spec.type == ITEM_CRAFT)
2940 if(itemname != "CraftItem")
2942 CraftItem *mitem = (CraftItem*)item;
2943 if(mitem->getSubName() != spec.name)
2946 else if(spec.type == ITEM_TOOL)
2948 // Not supported yet
2951 else if(spec.type == ITEM_MBO)
2953 // Not supported yet
2958 // Not supported yet
2966 void Server::SendInventory(u16 peer_id)
2968 DSTACK(__FUNCTION_NAME);
2970 Player* player = m_env.getPlayer(peer_id);
2973 Calculate crafting stuff
2975 if(g_settings.getBool("creative_mode") == false)
2977 InventoryList *clist = player->inventory.getList("craft");
2978 InventoryList *rlist = player->inventory.getList("craftresult");
2981 rlist->clearItems();
2985 InventoryItem *items[9];
2986 for(u16 i=0; i<9; i++)
2988 items[i] = clist->getItem(i);
2997 specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_TREE);
2998 if(checkItemCombination(items, specs))
3000 rlist->addItem(new MaterialItem(CONTENT_WOOD, 4));
3009 specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
3010 if(checkItemCombination(items, specs))
3012 rlist->addItem(new CraftItem("Stick", 4));
3021 specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
3022 specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
3023 specs[2] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
3024 specs[3] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
3025 specs[4] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
3026 specs[5] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
3027 specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
3028 if(checkItemCombination(items, specs))
3030 rlist->addItem(new MapBlockObjectItem("Sign"));
3039 specs[0] = ItemSpec(ITEM_CRAFT, "lump_of_coal");
3040 specs[3] = ItemSpec(ITEM_CRAFT, "Stick");
3041 if(checkItemCombination(items, specs))
3043 rlist->addItem(new MaterialItem(CONTENT_TORCH, 4));
3052 specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
3053 specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
3054 specs[2] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
3055 specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
3056 specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
3057 if(checkItemCombination(items, specs))
3059 rlist->addItem(new ToolItem("WPick", 0));
3068 specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_STONE);
3069 specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_STONE);
3070 specs[2] = ItemSpec(ITEM_MATERIAL, CONTENT_STONE);
3071 specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
3072 specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
3073 if(checkItemCombination(items, specs))
3075 rlist->addItem(new ToolItem("STPick", 0));
3084 specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_MESE);
3085 specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_MESE);
3086 specs[2] = ItemSpec(ITEM_MATERIAL, CONTENT_MESE);
3087 specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
3088 specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
3089 if(checkItemCombination(items, specs))
3091 rlist->addItem(new ToolItem("MesePick", 0));
3096 } // if creative_mode == false
3102 std::ostringstream os;
3103 //os.imbue(std::locale("C"));
3105 player->inventory.serialize(os);
3107 std::string s = os.str();
3109 SharedBuffer<u8> data(s.size()+2);
3110 writeU16(&data[0], TOCLIENT_INVENTORY);
3111 memcpy(&data[2], s.c_str(), s.size());
3114 m_con.Send(peer_id, 0, data, true);
3117 void Server::SendChatMessage(u16 peer_id, const std::wstring &message)
3119 DSTACK(__FUNCTION_NAME);
3121 std::ostringstream os(std::ios_base::binary);
3125 writeU16(buf, TOCLIENT_CHAT_MESSAGE);
3126 os.write((char*)buf, 2);
3129 writeU16(buf, message.size());
3130 os.write((char*)buf, 2);
3133 for(u32 i=0; i<message.size(); i++)
3137 os.write((char*)buf, 2);
3141 std::string s = os.str();
3142 SharedBuffer<u8> data((u8*)s.c_str(), s.size());
3144 m_con.Send(peer_id, 0, data, true);
3147 void Server::BroadcastChatMessage(const std::wstring &message)
3149 for(core::map<u16, RemoteClient*>::Iterator
3150 i = m_clients.getIterator();
3151 i.atEnd() == false; i++)
3153 // Get client and check that it is valid
3154 RemoteClient *client = i.getNode()->getValue();
3155 assert(client->peer_id == i.getNode()->getKey());
3156 if(client->serialization_version == SER_FMT_VER_INVALID)
3159 SendChatMessage(client->peer_id, message);
3163 void Server::sendRemoveNode(v3s16 p, u16 ignore_id)
3167 SharedBuffer<u8> reply(replysize);
3168 writeU16(&reply[0], TOCLIENT_REMOVENODE);
3169 writeS16(&reply[2], p.X);
3170 writeS16(&reply[4], p.Y);
3171 writeS16(&reply[6], p.Z);
3173 for(core::map<u16, RemoteClient*>::Iterator
3174 i = m_clients.getIterator();
3175 i.atEnd() == false; i++)
3177 // Get client and check that it is valid
3178 RemoteClient *client = i.getNode()->getValue();
3179 assert(client->peer_id == i.getNode()->getKey());
3180 if(client->serialization_version == SER_FMT_VER_INVALID)
3183 // Don't send if it's the same one
3184 if(client->peer_id == ignore_id)
3188 m_con.Send(client->peer_id, 0, reply, true);
3192 void Server::sendAddNode(v3s16 p, MapNode n, u16 ignore_id)
3194 for(core::map<u16, RemoteClient*>::Iterator
3195 i = m_clients.getIterator();
3196 i.atEnd() == false; i++)
3198 // Get client and check that it is valid
3199 RemoteClient *client = i.getNode()->getValue();
3200 assert(client->peer_id == i.getNode()->getKey());
3201 if(client->serialization_version == SER_FMT_VER_INVALID)
3204 // Don't send if it's the same one
3205 if(client->peer_id == ignore_id)
3209 u32 replysize = 8 + MapNode::serializedLength(client->serialization_version);
3210 SharedBuffer<u8> reply(replysize);
3211 writeU16(&reply[0], TOCLIENT_ADDNODE);
3212 writeS16(&reply[2], p.X);
3213 writeS16(&reply[4], p.Y);
3214 writeS16(&reply[6], p.Z);
3215 n.serialize(&reply[8], client->serialization_version);
3218 m_con.Send(client->peer_id, 0, reply, true);
3222 void Server::SendBlockNoLock(u16 peer_id, MapBlock *block, u8 ver)
3224 DSTACK(__FUNCTION_NAME);
3226 Create a packet with the block in the right format
3229 std::ostringstream os(std::ios_base::binary);
3230 block->serialize(os, ver);
3231 std::string s = os.str();
3232 SharedBuffer<u8> blockdata((u8*)s.c_str(), s.size());
3234 u32 replysize = 8 + blockdata.getSize();
3235 SharedBuffer<u8> reply(replysize);
3236 v3s16 p = block->getPos();
3237 writeU16(&reply[0], TOCLIENT_BLOCKDATA);
3238 writeS16(&reply[2], p.X);
3239 writeS16(&reply[4], p.Y);
3240 writeS16(&reply[6], p.Z);
3241 memcpy(&reply[8], *blockdata, blockdata.getSize());
3243 /*dstream<<"Sending block ("<<p.X<<","<<p.Y<<","<<p.Z<<")"
3244 <<": \tpacket size: "<<replysize<<std::endl;*/
3249 m_con.Send(peer_id, 1, reply, true);
3252 void Server::SendBlocks(float dtime)
3254 DSTACK(__FUNCTION_NAME);
3256 JMutexAutoLock envlock(m_env_mutex);
3257 JMutexAutoLock conlock(m_con_mutex);
3259 //TimeTaker timer("Server::SendBlocks");
3261 core::array<PrioritySortedBlockTransfer> queue;
3263 s32 total_sending = 0;
3265 for(core::map<u16, RemoteClient*>::Iterator
3266 i = m_clients.getIterator();
3267 i.atEnd() == false; i++)
3269 RemoteClient *client = i.getNode()->getValue();
3270 assert(client->peer_id == i.getNode()->getKey());
3272 total_sending += client->SendingCount();
3274 if(client->serialization_version == SER_FMT_VER_INVALID)
3277 client->GetNextBlocks(this, dtime, queue);
3281 // Lowest priority number comes first.
3282 // Lowest is most important.
3285 for(u32 i=0; i<queue.size(); i++)
3287 //TODO: Calculate limit dynamically
3288 if(total_sending >= g_settings.getS32
3289 ("max_simultaneous_block_sends_server_total"))
3292 PrioritySortedBlockTransfer q = queue[i];
3294 MapBlock *block = NULL;
3297 block = m_env.getMap().getBlockNoCreate(q.pos);
3299 catch(InvalidPositionException &e)
3304 RemoteClient *client = getClient(q.peer_id);
3306 SendBlockNoLock(q.peer_id, block, client->serialization_version);
3308 client->SentBlock(q.pos);
3315 RemoteClient* Server::getClient(u16 peer_id)
3317 DSTACK(__FUNCTION_NAME);
3318 //JMutexAutoLock lock(m_con_mutex);
3319 core::map<u16, RemoteClient*>::Node *n;
3320 n = m_clients.find(peer_id);
3321 // A client should exist for all peers
3323 return n->getValue();
3326 std::wstring Server::getStatusString()
3328 std::wostringstream os(std::ios_base::binary);
3331 os<<L"uptime="<<m_uptime.get();
3332 // Information about clients
3334 for(core::map<u16, RemoteClient*>::Iterator
3335 i = m_clients.getIterator();
3336 i.atEnd() == false; i++)
3338 // Get client and check that it is valid
3339 RemoteClient *client = i.getNode()->getValue();
3340 assert(client->peer_id == i.getNode()->getKey());
3341 if(client->serialization_version == SER_FMT_VER_INVALID)
3344 Player *player = m_env.getPlayer(client->peer_id);
3345 // Get name of player
3346 std::wstring name = L"unknown";
3348 name = narrow_to_wide(player->getName());
3349 // Add name to information string
3353 if(((ServerMap*)(&m_env.getMap()))->isSavingEnabled() == false)
3354 os<<" WARNING: Map saving is disabled."<<std::endl;
3359 void setCreativeInventory(Player *player)
3361 player->resetInventory();
3363 // Give some good picks
3365 InventoryItem *item = new ToolItem("STPick", 0);
3366 void* r = player->inventory.addItem("main", item);
3370 InventoryItem *item = new ToolItem("MesePick", 0);
3371 void* r = player->inventory.addItem("main", item);
3379 // CONTENT_IGNORE-terminated list
3380 u8 material_items[] = {
3388 CONTENT_WATERSOURCE,
3395 u8 *mip = material_items;
3396 for(u16 i=0; i<PLAYER_INVENTORY_SIZE; i++)
3398 if(*mip == CONTENT_IGNORE)
3401 InventoryItem *item = new MaterialItem(*mip, 1);
3402 player->inventory.addItem("main", item);
3408 assert(USEFUL_CONTENT_COUNT <= PLAYER_INVENTORY_SIZE);
3411 InventoryItem *item = new MaterialItem(CONTENT_TORCH, 1);
3412 player->inventory.addItem("main", item);
3415 for(u16 i=0; i<USEFUL_CONTENT_COUNT; i++)
3417 // Skip some materials
3418 if(i == CONTENT_WATER || i == CONTENT_TORCH
3419 || i == CONTENT_COALSTONE)
3422 InventoryItem *item = new MaterialItem(i, 1);
3423 player->inventory.addItem("main", item);
3429 InventoryItem *item = new MapBlockObjectItem("Sign Example text");
3430 void* r = player->inventory.addItem("main", item);
3435 Player *Server::emergePlayer(const char *name, const char *password,
3439 Try to get an existing player
3441 Player *player = m_env.getPlayer(name);
3444 // If player is already connected, cancel
3445 if(player->peer_id != 0)
3447 dstream<<"emergePlayer(): Player already connected"<<std::endl;
3452 player->peer_id = peer_id;
3454 // Reset inventory to creative if in creative mode
3455 if(g_settings.getBool("creative_mode"))
3457 setCreativeInventory(player);
3464 If player with the wanted peer_id already exists, cancel.
3466 if(m_env.getPlayer(peer_id) != NULL)
3468 dstream<<"emergePlayer(): Player with wrong name but same"
3469 " peer_id already exists"<<std::endl;
3477 player = new ServerRemotePlayer();
3478 //player->peer_id = c.peer_id;
3479 //player->peer_id = PEER_ID_INEXISTENT;
3480 player->peer_id = peer_id;
3481 player->updateName(name);
3487 dstream<<"Server: Finding spawn place for player \""
3488 <<player->getName()<<"\""<<std::endl;
3492 player->setPosition(intToFloat(v3s16(
3499 s16 groundheight = 0;
3501 // Try to find a good place a few times
3502 for(s32 i=0; i<1000; i++)
3505 // We're going to try to throw the player to this position
3506 nodepos = v2s16(-range + (myrand()%(range*2)),
3507 -range + (myrand()%(range*2)));
3508 v2s16 sectorpos = getNodeSectorPos(nodepos);
3509 // Get sector (NOTE: Don't get because it's slow)
3510 //m_env.getMap().emergeSector(sectorpos);
3511 // Get ground height at point (fallbacks to heightmap function)
3512 groundheight = m_env.getServerMap().findGroundLevel(nodepos);
3513 // Don't go underwater
3514 if(groundheight < WATER_LEVEL)
3516 //dstream<<"-> Underwater"<<std::endl;
3519 // Don't go to high places
3520 if(groundheight > WATER_LEVEL + 4)
3522 //dstream<<"-> Underwater"<<std::endl;
3527 // Doesn't work, generating blocks is a bit too complicated for doing here
3528 // Get block at point
3530 nodepos3d = v3s16(nodepos.X, groundheight+1, nodepos.Y);
3531 v3s16 blockpos = getNodeBlockPos(nodepos3d);
3532 ((ServerMap*)(&m_env.getMap()))->emergeBlock(blockpos);
3533 // Don't go inside ground
3535 /*v3s16 footpos(nodepos.X, groundheight+1, nodepos.Y);
3536 v3s16 headpos(nodepos.X, groundheight+2, nodepos.Y);*/
3537 v3s16 footpos = nodepos3d + v3s16(0,0,0);
3538 v3s16 headpos = nodepos3d + v3s16(0,1,0);
3539 if(m_env.getMap().getNode(footpos).d != CONTENT_AIR
3540 || m_env.getMap().getNode(headpos).d != CONTENT_AIR)
3542 dstream<<"-> Inside ground"<<std::endl;
3546 }catch(InvalidPositionException &e)
3548 dstream<<"-> Invalid position"<<std::endl;
3549 // Ignore invalid position
3554 // Found a good place
3555 dstream<<"Searched through "<<i<<" places."<<std::endl;
3560 // If no suitable place was not found, go above water at least.
3561 if(groundheight < WATER_LEVEL)
3562 groundheight = WATER_LEVEL;
3564 player->setPosition(intToFloat(v3s16(
3566 groundheight + 5, // Accomodate mud
3572 Add player to environment
3575 m_env.addPlayer(player);
3578 Add stuff to inventory
3581 if(g_settings.getBool("creative_mode"))
3583 setCreativeInventory(player);
3588 InventoryItem *item = new ToolItem("WPick", 32000);
3589 void* r = player->inventory.addItem("main", item);
3593 InventoryItem *item = new MaterialItem(CONTENT_MESE, 6);
3594 void* r = player->inventory.addItem("main", item);
3598 InventoryItem *item = new MaterialItem(CONTENT_COALSTONE, 6);
3599 void* r = player->inventory.addItem("main", item);
3603 InventoryItem *item = new MaterialItem(CONTENT_WOOD, 6);
3604 void* r = player->inventory.addItem("main", item);
3608 InventoryItem *item = new CraftItem("Stick", 4);
3609 void* r = player->inventory.addItem("main", item);
3613 InventoryItem *item = new ToolItem("WPick", 32000);
3614 void* r = player->inventory.addItem("main", item);
3618 InventoryItem *item = new ToolItem("STPick", 32000);
3619 void* r = player->inventory.addItem("main", item);
3622 /*// Give some lights
3624 InventoryItem *item = new MaterialItem(CONTENT_TORCH, 999);
3625 bool r = player->inventory.addItem("main", item);
3629 for(u16 i=0; i<4; i++)
3631 InventoryItem *item = new MapBlockObjectItem("Sign Example text");
3632 bool r = player->inventory.addItem("main", item);
3635 /*// Give some other stuff
3637 InventoryItem *item = new MaterialItem(CONTENT_TREE, 999);
3638 bool r = player->inventory.addItem("main", item);
3645 } // create new player
3649 void Server::UpdateBlockWaterPressure(MapBlock *block,
3650 core::map<v3s16, MapBlock*> &modified_blocks)
3652 MapVoxelManipulator v(&m_env.getMap());
3653 v.m_disable_water_climb =
3654 g_settings.getBool("disable_water_climb");
3656 VoxelArea area(block->getPosRelative(),
3657 block->getPosRelative() + v3s16(1,1,1)*(MAP_BLOCKSIZE-1));
3661 v.updateAreaWaterPressure(area, m_flow_active_nodes);
3663 catch(ProcessingLimitException &e)
3665 dstream<<"Processing limit reached (1)"<<std::endl;
3668 v.blitBack(modified_blocks);
3672 void Server::handlePeerChange(PeerChange &c)
3674 JMutexAutoLock envlock(m_env_mutex);
3675 JMutexAutoLock conlock(m_con_mutex);
3677 if(c.type == PEER_ADDED)
3684 core::map<u16, RemoteClient*>::Node *n;
3685 n = m_clients.find(c.peer_id);
3686 // The client shouldn't already exist
3690 RemoteClient *client = new RemoteClient();
3691 client->peer_id = c.peer_id;
3692 m_clients.insert(client->peer_id, client);
3695 else if(c.type == PEER_REMOVED)
3702 core::map<u16, RemoteClient*>::Node *n;
3703 n = m_clients.find(c.peer_id);
3704 // The client should exist
3707 // Collect information about leaving in chat
3708 std::wstring message;
3710 std::wstring name = L"unknown";
3711 Player *player = m_env.getPlayer(c.peer_id);
3713 name = narrow_to_wide(player->getName());
3717 message += L" left game";
3719 message += L" (timed out)";
3724 m_env.removePlayer(c.peer_id);
3727 // Set player client disconnected
3729 Player *player = m_env.getPlayer(c.peer_id);
3731 player->peer_id = 0;
3735 delete m_clients[c.peer_id];
3736 m_clients.remove(c.peer_id);
3738 // Send player info to all remaining clients
3741 // Send leave chat message to all remaining clients
3742 BroadcastChatMessage(message);
3751 void Server::handlePeerChanges()
3753 while(m_peer_change_queue.size() > 0)
3755 PeerChange c = m_peer_change_queue.pop_front();
3757 dout_server<<"Server: Handling peer change: "
3758 <<"id="<<c.peer_id<<", timeout="<<c.timeout
3761 handlePeerChange(c);
3765 void dedicated_server_loop(Server &server, bool &kill)
3767 DSTACK(__FUNCTION_NAME);
3769 std::cout<<DTIME<<std::endl;
3770 std::cout<<"========================"<<std::endl;
3771 std::cout<<"Running dedicated server"<<std::endl;
3772 std::cout<<"========================"<<std::endl;
3773 std::cout<<std::endl;
3777 // This is kind of a hack but can be done like this
3778 // because server.step() is very light
3782 if(server.getShutdownRequested() || kill)
3784 std::cout<<DTIME<<" dedicated_server_loop(): Quitting."<<std::endl;
3788 static int counter = 0;
3794 core::list<PlayerInfo> list = server.getPlayerInfo();
3795 core::list<PlayerInfo>::Iterator i;
3796 static u32 sum_old = 0;
3797 u32 sum = PIChecksum(list);
3800 std::cout<<DTIME<<"Player info:"<<std::endl;
3801 for(i=list.begin(); i!=list.end(); i++)
3803 i->PrintLine(&std::cout);