Use true distance for block priority.
[oweals/minetest.git] / src / clientiface.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2014 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser 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.
18 */
19
20 #include <sstream>
21
22 #include "clientiface.h"
23 #include "util/numeric.h"
24 #include "remoteplayer.h"
25 #include "settings.h"
26 #include "mapblock.h"
27 #include "network/connection.h"
28 #include "serverenvironment.h"
29 #include "map.h"
30 #include "emerge.h"
31 #include "content_sao.h"              // TODO this is used for cleanup of only
32 #include "log.h"
33 #include "util/srp.h"
34
35 const char *ClientInterface::statenames[] = {
36         "Invalid",
37         "Disconnecting",
38         "Denied",
39         "Created",
40         "AwaitingInit2",
41         "HelloSent",
42         "InitDone",
43         "DefinitionsSent",
44         "Active",
45         "SudoMode",
46 };
47
48
49
50 std::string ClientInterface::state2Name(ClientState state)
51 {
52         return statenames[state];
53 }
54
55 void RemoteClient::ResendBlockIfOnWire(v3s16 p)
56 {
57         // if this block is on wire, mark it for sending again as soon as possible
58         if (m_blocks_sending.find(p) != m_blocks_sending.end()) {
59                 SetBlockNotSent(p);
60         }
61 }
62
63 void RemoteClient::GetNextBlocks (
64                 ServerEnvironment *env,
65                 EmergeManager * emerge,
66                 float dtime,
67                 std::vector<PrioritySortedBlockTransfer> &dest)
68 {
69         DSTACK(FUNCTION_NAME);
70
71
72         // Increment timers
73         m_nothing_to_send_pause_timer -= dtime;
74         m_nearest_unsent_reset_timer += dtime;
75
76         if(m_nothing_to_send_pause_timer >= 0)
77                 return;
78
79         RemotePlayer *player = env->getPlayer(peer_id);
80         // This can happen sometimes; clients and players are not in perfect sync.
81         if (player == NULL)
82                 return;
83
84         PlayerSAO *sao = player->getPlayerSAO();
85         if (sao == NULL)
86                 return;
87
88         // Won't send anything if already sending
89         if(m_blocks_sending.size() >= g_settings->getU16
90                         ("max_simultaneous_block_sends_per_client"))
91         {
92                 //infostream<<"Not sending any blocks, Queue full."<<std::endl;
93                 return;
94         }
95
96         v3f playerpos = sao->getBasePosition();
97         v3f playerspeed = player->getSpeed();
98         v3f playerspeeddir(0,0,0);
99         if(playerspeed.getLength() > 1.0*BS)
100                 playerspeeddir = playerspeed / playerspeed.getLength();
101         // Predict to next block
102         v3f playerpos_predicted = playerpos + playerspeeddir*MAP_BLOCKSIZE*BS;
103
104         v3s16 center_nodepos = floatToInt(playerpos_predicted, BS);
105
106         v3s16 center = getNodeBlockPos(center_nodepos);
107
108         // Camera position and direction
109         v3f camera_pos = sao->getEyePosition();
110         v3f camera_dir = v3f(0,0,1);
111         camera_dir.rotateYZBy(sao->getPitch());
112         camera_dir.rotateXZBy(sao->getYaw());
113
114         /*infostream<<"camera_dir=("<<camera_dir.X<<","<<camera_dir.Y<<","
115                         <<camera_dir.Z<<")"<<std::endl;*/
116
117         /*
118                 Get the starting value of the block finder radius.
119         */
120
121         if(m_last_center != center)
122         {
123                 m_nearest_unsent_d = 0;
124                 m_last_center = center;
125         }
126
127         /*infostream<<"m_nearest_unsent_reset_timer="
128                         <<m_nearest_unsent_reset_timer<<std::endl;*/
129
130         // Reset periodically to workaround for some bugs or stuff
131         if(m_nearest_unsent_reset_timer > 20.0)
132         {
133                 m_nearest_unsent_reset_timer = 0;
134                 m_nearest_unsent_d = 0;
135                 //infostream<<"Resetting m_nearest_unsent_d for "
136                 //              <<server->getPlayerName(peer_id)<<std::endl;
137         }
138
139         //s16 last_nearest_unsent_d = m_nearest_unsent_d;
140         s16 d_start = m_nearest_unsent_d;
141
142         //infostream<<"d_start="<<d_start<<std::endl;
143
144         u16 max_simul_sends_setting = g_settings->getU16
145                         ("max_simultaneous_block_sends_per_client");
146         u16 max_simul_sends_usually = max_simul_sends_setting;
147
148         /*
149                 Check the time from last addNode/removeNode.
150
151                 Decrease send rate if player is building stuff.
152         */
153         m_time_from_building += dtime;
154         if(m_time_from_building < g_settings->getFloat(
155                                 "full_block_send_enable_min_time_from_building"))
156         {
157                 max_simul_sends_usually
158                         = LIMITED_MAX_SIMULTANEOUS_BLOCK_SENDS;
159         }
160
161         /*
162                 Number of blocks sending + number of blocks selected for sending
163         */
164         u32 num_blocks_selected = m_blocks_sending.size();
165
166         /*
167                 next time d will be continued from the d from which the nearest
168                 unsent block was found this time.
169
170                 This is because not necessarily any of the blocks found this
171                 time are actually sent.
172         */
173         s32 new_nearest_unsent_d = -1;
174
175         // get view range and camera fov from the client
176         s16 wanted_range = sao->getWantedRange();
177         float camera_fov = sao->getFov();
178         // if FOV, wanted_range are not available (old client), fall back to old default
179         if (wanted_range <= 0) wanted_range = 1000;
180         if (camera_fov <= 0) camera_fov = (72.0*M_PI/180) * 4./3.;
181
182         const s16 full_d_max = MYMIN(g_settings->getS16("max_block_send_distance"), wanted_range);
183         const s16 d_opt = MYMIN(g_settings->getS16("block_send_optimize_distance"), wanted_range);
184         const s16 d_blocks_in_sight = full_d_max * BS * MAP_BLOCKSIZE;
185         //infostream << "Fov from client " << camera_fov << " full_d_max " << full_d_max << std::endl;
186
187         s16 d_max = full_d_max;
188         s16 d_max_gen = MYMIN(g_settings->getS16("max_block_generate_distance"), wanted_range);
189
190         // Don't loop very much at a time
191         s16 max_d_increment_at_time = 2;
192         if(d_max > d_start + max_d_increment_at_time)
193                 d_max = d_start + max_d_increment_at_time;
194
195         s32 nearest_emerged_d = -1;
196         s32 nearest_emergefull_d = -1;
197         s32 nearest_sent_d = -1;
198         //bool queue_is_full = false;
199
200         const v3s16 cam_pos_nodes = floatToInt(camera_pos, BS);
201         const bool occ_cull = g_settings->getBool("server_side_occlusion_culling");
202
203         s16 d;
204         for(d = d_start; d <= d_max; d++) {
205                 /*
206                         Get the border/face dot coordinates of a "d-radiused"
207                         box
208                 */
209                 std::vector<v3s16> list = FacePositionCache::getFacePositions(d);
210
211                 std::vector<v3s16>::iterator li;
212                 for(li = list.begin(); li != list.end(); ++li) {
213                         v3s16 p = *li + center;
214
215                         /*
216                                 Send throttling
217                                 - Don't allow too many simultaneous transfers
218                                 - EXCEPT when the blocks are very close
219
220                                 Also, don't send blocks that are already flying.
221                         */
222
223                         // Start with the usual maximum
224                         u16 max_simul_dynamic = max_simul_sends_usually;
225
226                         // If block is very close, allow full maximum
227                         if(d <= BLOCK_SEND_DISABLE_LIMITS_MAX_D)
228                                 max_simul_dynamic = max_simul_sends_setting;
229
230                         // Don't select too many blocks for sending
231                         if (num_blocks_selected >= max_simul_dynamic) {
232                                 //queue_is_full = true;
233                                 goto queue_full_break;
234                         }
235
236                         // Don't send blocks that are currently being transferred
237                         if (m_blocks_sending.find(p) != m_blocks_sending.end())
238                                 continue;
239
240                         /*
241                                 Do not go over-limit
242                         */
243                         if (blockpos_over_limit(p))
244                                 continue;
245
246                         // If this is true, inexistent block will be made from scratch
247                         bool generate = d <= d_max_gen;
248
249                         /*
250                                 Don't generate or send if not in sight
251                                 FIXME This only works if the client uses a small enough
252                                 FOV setting. The default of 72 degrees is fine.
253                         */
254
255                         f32 dist;
256                         if (!isBlockInSight(p, camera_pos, camera_dir, camera_fov, d_blocks_in_sight, &dist)) {
257                                 continue;
258                         }
259
260                         /*
261                                 Don't send already sent blocks
262                         */
263                         {
264                                 if(m_blocks_sent.find(p) != m_blocks_sent.end())
265                                 {
266                                         continue;
267                                 }
268                         }
269
270                         /*
271                                 Check if map has this block
272                         */
273                         MapBlock *block = env->getMap().getBlockNoCreateNoEx(p);
274
275                         bool surely_not_found_on_disk = false;
276                         bool block_is_invalid = false;
277                         if(block != NULL)
278                         {
279                                 // Reset usage timer, this block will be of use in the future.
280                                 block->resetUsageTimer();
281
282                                 // Block is dummy if data doesn't exist.
283                                 // It means it has been not found from disk and not generated
284                                 if(block->isDummy())
285                                 {
286                                         surely_not_found_on_disk = true;
287                                 }
288
289                                 if(block->isGenerated() == false)
290                                         block_is_invalid = true;
291
292                                 /*
293                                         If block is not close, don't send it unless it is near
294                                         ground level.
295
296                                         Block is near ground level if night-time mesh
297                                         differs from day-time mesh.
298                                 */
299                                 if(d >= d_opt)
300                                 {
301                                         if(block->getDayNightDiff() == false)
302                                                 continue;
303                                 }
304
305                                 if (occ_cull && !block_is_invalid &&
306                                                 env->getMap().isBlockOccluded(block, cam_pos_nodes)) {
307                                         continue;
308                                 }
309                         }
310
311                         /*
312                                 If block has been marked to not exist on disk (dummy)
313                                 and generating new ones is not wanted, skip block.
314                         */
315                         if(generate == false && surely_not_found_on_disk == true)
316                         {
317                                 // get next one.
318                                 continue;
319                         }
320
321                         /*
322                                 Add inexistent block to emerge queue.
323                         */
324                         if(block == NULL || surely_not_found_on_disk || block_is_invalid)
325                         {
326                                 if (emerge->enqueueBlockEmerge(peer_id, p, generate)) {
327                                         if (nearest_emerged_d == -1)
328                                                 nearest_emerged_d = d;
329                                 } else {
330                                         if (nearest_emergefull_d == -1)
331                                                 nearest_emergefull_d = d;
332                                         goto queue_full_break;
333                                 }
334
335                                 // get next one.
336                                 continue;
337                         }
338
339                         if(nearest_sent_d == -1)
340                                 nearest_sent_d = d;
341
342                         /*
343                                 Add block to send queue
344                         */
345                         PrioritySortedBlockTransfer q((float)dist, p, peer_id);
346
347                         dest.push_back(q);
348
349                         num_blocks_selected += 1;
350                 }
351         }
352 queue_full_break:
353
354         // If nothing was found for sending and nothing was queued for
355         // emerging, continue next time browsing from here
356         if(nearest_emerged_d != -1){
357                 new_nearest_unsent_d = nearest_emerged_d;
358         } else if(nearest_emergefull_d != -1){
359                 new_nearest_unsent_d = nearest_emergefull_d;
360         } else {
361                 if(d > full_d_max){
362                         new_nearest_unsent_d = 0;
363                         m_nothing_to_send_pause_timer = 2.0;
364                 } else {
365                         if(nearest_sent_d != -1)
366                                 new_nearest_unsent_d = nearest_sent_d;
367                         else
368                                 new_nearest_unsent_d = d;
369                 }
370         }
371
372         if(new_nearest_unsent_d != -1)
373                 m_nearest_unsent_d = new_nearest_unsent_d;
374 }
375
376 void RemoteClient::GotBlock(v3s16 p)
377 {
378         if (m_blocks_modified.find(p) == m_blocks_modified.end()) {
379                 if (m_blocks_sending.find(p) != m_blocks_sending.end())
380                         m_blocks_sending.erase(p);
381                 else
382                         m_excess_gotblocks++;
383
384                 m_blocks_sent.insert(p);
385         }
386 }
387
388 void RemoteClient::SentBlock(v3s16 p)
389 {
390         if (m_blocks_modified.find(p) != m_blocks_modified.end())
391                 m_blocks_modified.erase(p);
392
393         if(m_blocks_sending.find(p) == m_blocks_sending.end())
394                 m_blocks_sending[p] = 0.0;
395         else
396                 infostream<<"RemoteClient::SentBlock(): Sent block"
397                                 " already in m_blocks_sending"<<std::endl;
398 }
399
400 void RemoteClient::SetBlockNotSent(v3s16 p)
401 {
402         m_nearest_unsent_d = 0;
403         m_nothing_to_send_pause_timer = 0;
404
405         if(m_blocks_sending.find(p) != m_blocks_sending.end())
406                 m_blocks_sending.erase(p);
407         if(m_blocks_sent.find(p) != m_blocks_sent.end())
408                 m_blocks_sent.erase(p);
409         m_blocks_modified.insert(p);
410 }
411
412 void RemoteClient::SetBlocksNotSent(std::map<v3s16, MapBlock*> &blocks)
413 {
414         m_nearest_unsent_d = 0;
415         m_nothing_to_send_pause_timer = 0;
416
417         for(std::map<v3s16, MapBlock*>::iterator
418                         i = blocks.begin();
419                         i != blocks.end(); ++i)
420         {
421                 v3s16 p = i->first;
422                 m_blocks_modified.insert(p);
423
424                 if(m_blocks_sending.find(p) != m_blocks_sending.end())
425                         m_blocks_sending.erase(p);
426                 if(m_blocks_sent.find(p) != m_blocks_sent.end())
427                         m_blocks_sent.erase(p);
428         }
429 }
430
431 void RemoteClient::notifyEvent(ClientStateEvent event)
432 {
433         std::ostringstream myerror;
434         switch (m_state)
435         {
436         case CS_Invalid:
437                 //intentionally do nothing
438                 break;
439         case CS_Created:
440                 switch (event) {
441                 case CSE_Hello:
442                         m_state = CS_HelloSent;
443                         break;
444                 case CSE_InitLegacy:
445                         m_state = CS_AwaitingInit2;
446                         break;
447                 case CSE_Disconnect:
448                         m_state = CS_Disconnecting;
449                         break;
450                 case CSE_SetDenied:
451                         m_state = CS_Denied;
452                         break;
453                 /* GotInit2 SetDefinitionsSent SetMediaSent */
454                 default:
455                         myerror << "Created: Invalid client state transition! " << event;
456                         throw ClientStateError(myerror.str());
457                 }
458                 break;
459         case CS_Denied:
460                 /* don't do anything if in denied state */
461                 break;
462         case CS_HelloSent:
463                 switch(event)
464                 {
465                 case CSE_AuthAccept:
466                         m_state = CS_AwaitingInit2;
467                         if ((chosen_mech == AUTH_MECHANISM_SRP)
468                                         || (chosen_mech == AUTH_MECHANISM_LEGACY_PASSWORD))
469                                 srp_verifier_delete((SRPVerifier *) auth_data);
470                         chosen_mech = AUTH_MECHANISM_NONE;
471                         break;
472                 case CSE_Disconnect:
473                         m_state = CS_Disconnecting;
474                         break;
475                 case CSE_SetDenied:
476                         m_state = CS_Denied;
477                         if ((chosen_mech == AUTH_MECHANISM_SRP)
478                                         || (chosen_mech == AUTH_MECHANISM_LEGACY_PASSWORD))
479                                 srp_verifier_delete((SRPVerifier *) auth_data);
480                         chosen_mech = AUTH_MECHANISM_NONE;
481                         break;
482                 default:
483                         myerror << "HelloSent: Invalid client state transition! " << event;
484                         throw ClientStateError(myerror.str());
485                 }
486                 break;
487         case CS_AwaitingInit2:
488                 switch(event)
489                 {
490                 case CSE_GotInit2:
491                         confirmSerializationVersion();
492                         m_state = CS_InitDone;
493                         break;
494                 case CSE_Disconnect:
495                         m_state = CS_Disconnecting;
496                         break;
497                 case CSE_SetDenied:
498                         m_state = CS_Denied;
499                         break;
500
501                 /* Init SetDefinitionsSent SetMediaSent */
502                 default:
503                         myerror << "InitSent: Invalid client state transition! " << event;
504                         throw ClientStateError(myerror.str());
505                 }
506                 break;
507
508         case CS_InitDone:
509                 switch(event)
510                 {
511                 case CSE_SetDefinitionsSent:
512                         m_state = CS_DefinitionsSent;
513                         break;
514                 case CSE_Disconnect:
515                         m_state = CS_Disconnecting;
516                         break;
517                 case CSE_SetDenied:
518                         m_state = CS_Denied;
519                         break;
520
521                 /* Init GotInit2 SetMediaSent */
522                 default:
523                         myerror << "InitDone: Invalid client state transition! " << event;
524                         throw ClientStateError(myerror.str());
525                 }
526                 break;
527         case CS_DefinitionsSent:
528                 switch(event)
529                 {
530                 case CSE_SetClientReady:
531                         m_state = CS_Active;
532                         break;
533                 case CSE_Disconnect:
534                         m_state = CS_Disconnecting;
535                         break;
536                 case CSE_SetDenied:
537                         m_state = CS_Denied;
538                         break;
539                 /* Init GotInit2 SetDefinitionsSent */
540                 default:
541                         myerror << "DefinitionsSent: Invalid client state transition! " << event;
542                         throw ClientStateError(myerror.str());
543                 }
544                 break;
545         case CS_Active:
546                 switch(event)
547                 {
548                 case CSE_SetDenied:
549                         m_state = CS_Denied;
550                         break;
551                 case CSE_Disconnect:
552                         m_state = CS_Disconnecting;
553                         break;
554                 case CSE_SudoSuccess:
555                         m_state = CS_SudoMode;
556                         if ((chosen_mech == AUTH_MECHANISM_SRP)
557                                         || (chosen_mech == AUTH_MECHANISM_LEGACY_PASSWORD))
558                                 srp_verifier_delete((SRPVerifier *) auth_data);
559                         chosen_mech = AUTH_MECHANISM_NONE;
560                         break;
561                 /* Init GotInit2 SetDefinitionsSent SetMediaSent SetDenied */
562                 default:
563                         myerror << "Active: Invalid client state transition! " << event;
564                         throw ClientStateError(myerror.str());
565                         break;
566                 }
567                 break;
568         case CS_SudoMode:
569                 switch(event)
570                 {
571                 case CSE_SetDenied:
572                         m_state = CS_Denied;
573                         break;
574                 case CSE_Disconnect:
575                         m_state = CS_Disconnecting;
576                         break;
577                 case CSE_SudoLeave:
578                         m_state = CS_Active;
579                         break;
580                 default:
581                         myerror << "Active: Invalid client state transition! " << event;
582                         throw ClientStateError(myerror.str());
583                         break;
584                 }
585                 break;
586         case CS_Disconnecting:
587                 /* we are already disconnecting */
588                 break;
589         }
590 }
591
592 u32 RemoteClient::uptime()
593 {
594         return getTime(PRECISION_SECONDS) - m_connection_time;
595 }
596
597 ClientInterface::ClientInterface(con::Connection* con)
598 :
599         m_con(con),
600         m_env(NULL),
601         m_print_info_timer(0.0)
602 {
603
604 }
605 ClientInterface::~ClientInterface()
606 {
607         /*
608                 Delete clients
609         */
610         {
611                 MutexAutoLock clientslock(m_clients_mutex);
612
613                 for (UNORDERED_MAP<u16, RemoteClient*>::iterator i = m_clients.begin();
614                         i != m_clients.end(); ++i) {
615                         // Delete client
616                         delete i->second;
617                 }
618         }
619 }
620
621 std::vector<u16> ClientInterface::getClientIDs(ClientState min_state)
622 {
623         std::vector<u16> reply;
624         MutexAutoLock clientslock(m_clients_mutex);
625
626         for(UNORDERED_MAP<u16, RemoteClient*>::iterator i = m_clients.begin();
627                 i != m_clients.end(); ++i) {
628                 if (i->second->getState() >= min_state)
629                         reply.push_back(i->second->peer_id);
630         }
631
632         return reply;
633 }
634
635 void ClientInterface::step(float dtime)
636 {
637         m_print_info_timer += dtime;
638         if(m_print_info_timer >= 30.0)
639         {
640                 m_print_info_timer = 0.0;
641                 UpdatePlayerList();
642         }
643 }
644
645 void ClientInterface::UpdatePlayerList()
646 {
647         if (m_env != NULL) {
648                 std::vector<u16> clients = getClientIDs();
649                 m_clients_names.clear();
650
651
652                 if(!clients.empty())
653                         infostream<<"Players:"<<std::endl;
654
655                 for (std::vector<u16>::iterator i = clients.begin(); i != clients.end(); ++i) {
656                         RemotePlayer *player = m_env->getPlayer(*i);
657
658                         if (player == NULL)
659                                 continue;
660
661                         infostream << "* " << player->getName() << "\t";
662
663                         {
664                                 MutexAutoLock clientslock(m_clients_mutex);
665                                 RemoteClient* client = lockedGetClientNoEx(*i);
666                                 if(client != NULL)
667                                         client->PrintInfo(infostream);
668                         }
669
670                         m_clients_names.push_back(player->getName());
671                 }
672         }
673 }
674
675 void ClientInterface::send(u16 peer_id, u8 channelnum,
676                 NetworkPacket* pkt, bool reliable)
677 {
678         m_con->Send(peer_id, channelnum, pkt, reliable);
679 }
680
681 void ClientInterface::sendToAll(u16 channelnum,
682                 NetworkPacket* pkt, bool reliable)
683 {
684         MutexAutoLock clientslock(m_clients_mutex);
685         for(UNORDERED_MAP<u16, RemoteClient*>::iterator i = m_clients.begin();
686                 i != m_clients.end(); ++i) {
687                 RemoteClient *client = i->second;
688
689                 if (client->net_proto_version != 0) {
690                         m_con->Send(client->peer_id, channelnum, pkt, reliable);
691                 }
692         }
693 }
694
695 RemoteClient* ClientInterface::getClientNoEx(u16 peer_id, ClientState state_min)
696 {
697         MutexAutoLock clientslock(m_clients_mutex);
698         UNORDERED_MAP<u16, RemoteClient*>::iterator n = m_clients.find(peer_id);
699         // The client may not exist; clients are immediately removed if their
700         // access is denied, and this event occurs later then.
701         if (n == m_clients.end())
702                 return NULL;
703
704         if (n->second->getState() >= state_min)
705                 return n->second;
706         else
707                 return NULL;
708 }
709
710 RemoteClient* ClientInterface::lockedGetClientNoEx(u16 peer_id, ClientState state_min)
711 {
712         UNORDERED_MAP<u16, RemoteClient*>::iterator n = m_clients.find(peer_id);
713         // The client may not exist; clients are immediately removed if their
714         // access is denied, and this event occurs later then.
715         if (n == m_clients.end())
716                 return NULL;
717
718         if (n->second->getState() >= state_min)
719                 return n->second;
720         else
721                 return NULL;
722 }
723
724 ClientState ClientInterface::getClientState(u16 peer_id)
725 {
726         MutexAutoLock clientslock(m_clients_mutex);
727         UNORDERED_MAP<u16, RemoteClient*>::iterator n = m_clients.find(peer_id);
728         // The client may not exist; clients are immediately removed if their
729         // access is denied, and this event occurs later then.
730         if (n == m_clients.end())
731                 return CS_Invalid;
732
733         return n->second->getState();
734 }
735
736 void ClientInterface::setPlayerName(u16 peer_id,std::string name)
737 {
738         MutexAutoLock clientslock(m_clients_mutex);
739         UNORDERED_MAP<u16, RemoteClient*>::iterator n = m_clients.find(peer_id);
740         // The client may not exist; clients are immediately removed if their
741         // access is denied, and this event occurs later then.
742         if (n != m_clients.end())
743                 n->second->setName(name);
744 }
745
746 void ClientInterface::DeleteClient(u16 peer_id)
747 {
748         MutexAutoLock conlock(m_clients_mutex);
749
750         // Error check
751         UNORDERED_MAP<u16, RemoteClient*>::iterator n = m_clients.find(peer_id);
752         // The client may not exist; clients are immediately removed if their
753         // access is denied, and this event occurs later then.
754         if (n == m_clients.end())
755                 return;
756
757         /*
758                 Mark objects to be not known by the client
759         */
760         //TODO this should be done by client destructor!!!
761         RemoteClient *client = n->second;
762         // Handle objects
763         for(std::set<u16>::iterator
764                         i = client->m_known_objects.begin();
765                         i != client->m_known_objects.end(); ++i)
766         {
767                 // Get object
768                 u16 id = *i;
769                 ServerActiveObject* obj = m_env->getActiveObject(id);
770
771                 if(obj && obj->m_known_by_count > 0)
772                         obj->m_known_by_count--;
773         }
774
775         // Delete client
776         delete m_clients[peer_id];
777         m_clients.erase(peer_id);
778 }
779
780 void ClientInterface::CreateClient(u16 peer_id)
781 {
782         MutexAutoLock conlock(m_clients_mutex);
783
784         // Error check
785         UNORDERED_MAP<u16, RemoteClient*>::iterator n = m_clients.find(peer_id);
786         // The client shouldn't already exist
787         if (n != m_clients.end()) return;
788
789         // Create client
790         RemoteClient *client = new RemoteClient();
791         client->peer_id = peer_id;
792         m_clients[client->peer_id] = client;
793 }
794
795 void ClientInterface::event(u16 peer_id, ClientStateEvent event)
796 {
797         {
798                 MutexAutoLock clientlock(m_clients_mutex);
799
800                 // Error check
801                 UNORDERED_MAP<u16, RemoteClient*>::iterator n = m_clients.find(peer_id);
802
803                 // No client to deliver event
804                 if (n == m_clients.end())
805                         return;
806                 n->second->notifyEvent(event);
807         }
808
809         if ((event == CSE_SetClientReady) ||
810                 (event == CSE_Disconnect)     ||
811                 (event == CSE_SetDenied))
812         {
813                 UpdatePlayerList();
814         }
815 }
816
817 u16 ClientInterface::getProtocolVersion(u16 peer_id)
818 {
819         MutexAutoLock conlock(m_clients_mutex);
820
821         // Error check
822         UNORDERED_MAP<u16, RemoteClient*>::iterator n = m_clients.find(peer_id);
823
824         // No client to get version
825         if (n == m_clients.end())
826                 return 0;
827
828         return n->second->net_proto_version;
829 }
830
831 void ClientInterface::setClientVersion(u16 peer_id, u8 major, u8 minor, u8 patch, std::string full)
832 {
833         MutexAutoLock conlock(m_clients_mutex);
834
835         // Error check
836         UNORDERED_MAP<u16, RemoteClient*>::iterator n = m_clients.find(peer_id);
837
838         // No client to set versions
839         if (n == m_clients.end())
840                 return;
841
842         n->second->setVersionInfo(major,minor,patch,full);
843 }