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