Lighting: Update lighting at block loading
[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         s16 d;
201         for(d = d_start; d <= d_max; d++) {
202                 /*
203                         Get the border/face dot coordinates of a "d-radiused"
204                         box
205                 */
206                 std::vector<v3s16> list = FacePositionCache::getFacePositions(d);
207
208                 std::vector<v3s16>::iterator li;
209                 for(li = list.begin(); li != list.end(); ++li) {
210                         v3s16 p = *li + center;
211
212                         /*
213                                 Send throttling
214                                 - Don't allow too many simultaneous transfers
215                                 - EXCEPT when the blocks are very close
216
217                                 Also, don't send blocks that are already flying.
218                         */
219
220                         // Start with the usual maximum
221                         u16 max_simul_dynamic = max_simul_sends_usually;
222
223                         // If block is very close, allow full maximum
224                         if(d <= BLOCK_SEND_DISABLE_LIMITS_MAX_D)
225                                 max_simul_dynamic = max_simul_sends_setting;
226
227                         // Don't select too many blocks for sending
228                         if (num_blocks_selected >= max_simul_dynamic) {
229                                 //queue_is_full = true;
230                                 goto queue_full_break;
231                         }
232
233                         // Don't send blocks that are currently being transferred
234                         if (m_blocks_sending.find(p) != m_blocks_sending.end())
235                                 continue;
236
237                         /*
238                                 Do not go over-limit
239                         */
240                         if (blockpos_over_limit(p))
241                                 continue;
242
243                         // If this is true, inexistent block will be made from scratch
244                         bool generate = d <= d_max_gen;
245
246                         /*
247                                 Don't generate or send if not in sight
248                                 FIXME This only works if the client uses a small enough
249                                 FOV setting. The default of 72 degrees is fine.
250                         */
251
252                         if(isBlockInSight(p, camera_pos, camera_dir, camera_fov, d_blocks_in_sight) == false)
253                         {
254                                 continue;
255                         }
256
257                         /*
258                                 Don't send already sent blocks
259                         */
260                         {
261                                 if(m_blocks_sent.find(p) != m_blocks_sent.end())
262                                 {
263                                         continue;
264                                 }
265                         }
266
267                         /*
268                                 Check if map has this block
269                         */
270                         MapBlock *block = env->getMap().getBlockNoCreateNoEx(p);
271
272                         bool surely_not_found_on_disk = false;
273                         bool block_is_invalid = false;
274                         if(block != NULL)
275                         {
276                                 // Reset usage timer, this block will be of use in the future.
277                                 block->resetUsageTimer();
278
279                                 // Block is dummy if data doesn't exist.
280                                 // It means it has been not found from disk and not generated
281                                 if(block->isDummy())
282                                 {
283                                         surely_not_found_on_disk = true;
284                                 }
285
286                                 if(block->isGenerated() == false)
287                                         block_is_invalid = true;
288
289                                 /*
290                                         If block is not close, don't send it unless it is near
291                                         ground level.
292
293                                         Block is near ground level if night-time mesh
294                                         differs from day-time mesh.
295                                 */
296                                 if(d >= d_opt)
297                                 {
298                                         if(block->getDayNightDiff() == false)
299                                                 continue;
300                                 }
301                         }
302
303                         /*
304                                 If block has been marked to not exist on disk (dummy)
305                                 and generating new ones is not wanted, skip block.
306                         */
307                         if(generate == false && surely_not_found_on_disk == true)
308                         {
309                                 // get next one.
310                                 continue;
311                         }
312
313                         /*
314                                 Add inexistent block to emerge queue.
315                         */
316                         if(block == NULL || surely_not_found_on_disk || block_is_invalid)
317                         {
318                                 if (emerge->enqueueBlockEmerge(peer_id, p, generate)) {
319                                         if (nearest_emerged_d == -1)
320                                                 nearest_emerged_d = d;
321                                 } else {
322                                         if (nearest_emergefull_d == -1)
323                                                 nearest_emergefull_d = d;
324                                         goto queue_full_break;
325                                 }
326
327                                 // get next one.
328                                 continue;
329                         }
330
331                         if(nearest_sent_d == -1)
332                                 nearest_sent_d = d;
333
334                         /*
335                                 Add block to send queue
336                         */
337                         PrioritySortedBlockTransfer q((float)d, p, peer_id);
338
339                         dest.push_back(q);
340
341                         num_blocks_selected += 1;
342                 }
343         }
344 queue_full_break:
345
346         // If nothing was found for sending and nothing was queued for
347         // emerging, continue next time browsing from here
348         if(nearest_emerged_d != -1){
349                 new_nearest_unsent_d = nearest_emerged_d;
350         } else if(nearest_emergefull_d != -1){
351                 new_nearest_unsent_d = nearest_emergefull_d;
352         } else {
353                 if(d > full_d_max){
354                         new_nearest_unsent_d = 0;
355                         m_nothing_to_send_pause_timer = 2.0;
356                 } else {
357                         if(nearest_sent_d != -1)
358                                 new_nearest_unsent_d = nearest_sent_d;
359                         else
360                                 new_nearest_unsent_d = d;
361                 }
362         }
363
364         if(new_nearest_unsent_d != -1)
365                 m_nearest_unsent_d = new_nearest_unsent_d;
366 }
367
368 void RemoteClient::GotBlock(v3s16 p)
369 {
370         if (m_blocks_modified.find(p) == m_blocks_modified.end()) {
371                 if (m_blocks_sending.find(p) != m_blocks_sending.end())
372                         m_blocks_sending.erase(p);
373                 else
374                         m_excess_gotblocks++;
375
376                 m_blocks_sent.insert(p);
377         }
378 }
379
380 void RemoteClient::SentBlock(v3s16 p)
381 {
382         if (m_blocks_modified.find(p) != m_blocks_modified.end())
383                 m_blocks_modified.erase(p);
384
385         if(m_blocks_sending.find(p) == m_blocks_sending.end())
386                 m_blocks_sending[p] = 0.0;
387         else
388                 infostream<<"RemoteClient::SentBlock(): Sent block"
389                                 " already in m_blocks_sending"<<std::endl;
390 }
391
392 void RemoteClient::SetBlockNotSent(v3s16 p)
393 {
394         m_nearest_unsent_d = 0;
395         m_nothing_to_send_pause_timer = 0;
396
397         if(m_blocks_sending.find(p) != m_blocks_sending.end())
398                 m_blocks_sending.erase(p);
399         if(m_blocks_sent.find(p) != m_blocks_sent.end())
400                 m_blocks_sent.erase(p);
401         m_blocks_modified.insert(p);
402 }
403
404 void RemoteClient::SetBlocksNotSent(std::map<v3s16, MapBlock*> &blocks)
405 {
406         m_nearest_unsent_d = 0;
407         m_nothing_to_send_pause_timer = 0;
408
409         for(std::map<v3s16, MapBlock*>::iterator
410                         i = blocks.begin();
411                         i != blocks.end(); ++i)
412         {
413                 v3s16 p = i->first;
414                 m_blocks_modified.insert(p);
415
416                 if(m_blocks_sending.find(p) != m_blocks_sending.end())
417                         m_blocks_sending.erase(p);
418                 if(m_blocks_sent.find(p) != m_blocks_sent.end())
419                         m_blocks_sent.erase(p);
420         }
421 }
422
423 void RemoteClient::notifyEvent(ClientStateEvent event)
424 {
425         std::ostringstream myerror;
426         switch (m_state)
427         {
428         case CS_Invalid:
429                 //intentionally do nothing
430                 break;
431         case CS_Created:
432                 switch (event) {
433                 case CSE_Hello:
434                         m_state = CS_HelloSent;
435                         break;
436                 case CSE_InitLegacy:
437                         m_state = CS_AwaitingInit2;
438                         break;
439                 case CSE_Disconnect:
440                         m_state = CS_Disconnecting;
441                         break;
442                 case CSE_SetDenied:
443                         m_state = CS_Denied;
444                         break;
445                 /* GotInit2 SetDefinitionsSent SetMediaSent */
446                 default:
447                         myerror << "Created: Invalid client state transition! " << event;
448                         throw ClientStateError(myerror.str());
449                 }
450                 break;
451         case CS_Denied:
452                 /* don't do anything if in denied state */
453                 break;
454         case CS_HelloSent:
455                 switch(event)
456                 {
457                 case CSE_AuthAccept:
458                         m_state = CS_AwaitingInit2;
459                         if ((chosen_mech == AUTH_MECHANISM_SRP)
460                                         || (chosen_mech == AUTH_MECHANISM_LEGACY_PASSWORD))
461                                 srp_verifier_delete((SRPVerifier *) auth_data);
462                         chosen_mech = AUTH_MECHANISM_NONE;
463                         break;
464                 case CSE_Disconnect:
465                         m_state = CS_Disconnecting;
466                         break;
467                 case CSE_SetDenied:
468                         m_state = CS_Denied;
469                         if ((chosen_mech == AUTH_MECHANISM_SRP)
470                                         || (chosen_mech == AUTH_MECHANISM_LEGACY_PASSWORD))
471                                 srp_verifier_delete((SRPVerifier *) auth_data);
472                         chosen_mech = AUTH_MECHANISM_NONE;
473                         break;
474                 default:
475                         myerror << "HelloSent: Invalid client state transition! " << event;
476                         throw ClientStateError(myerror.str());
477                 }
478                 break;
479         case CS_AwaitingInit2:
480                 switch(event)
481                 {
482                 case CSE_GotInit2:
483                         confirmSerializationVersion();
484                         m_state = CS_InitDone;
485                         break;
486                 case CSE_Disconnect:
487                         m_state = CS_Disconnecting;
488                         break;
489                 case CSE_SetDenied:
490                         m_state = CS_Denied;
491                         break;
492
493                 /* Init SetDefinitionsSent SetMediaSent */
494                 default:
495                         myerror << "InitSent: Invalid client state transition! " << event;
496                         throw ClientStateError(myerror.str());
497                 }
498                 break;
499
500         case CS_InitDone:
501                 switch(event)
502                 {
503                 case CSE_SetDefinitionsSent:
504                         m_state = CS_DefinitionsSent;
505                         break;
506                 case CSE_Disconnect:
507                         m_state = CS_Disconnecting;
508                         break;
509                 case CSE_SetDenied:
510                         m_state = CS_Denied;
511                         break;
512
513                 /* Init GotInit2 SetMediaSent */
514                 default:
515                         myerror << "InitDone: Invalid client state transition! " << event;
516                         throw ClientStateError(myerror.str());
517                 }
518                 break;
519         case CS_DefinitionsSent:
520                 switch(event)
521                 {
522                 case CSE_SetClientReady:
523                         m_state = CS_Active;
524                         break;
525                 case CSE_Disconnect:
526                         m_state = CS_Disconnecting;
527                         break;
528                 case CSE_SetDenied:
529                         m_state = CS_Denied;
530                         break;
531                 /* Init GotInit2 SetDefinitionsSent */
532                 default:
533                         myerror << "DefinitionsSent: Invalid client state transition! " << event;
534                         throw ClientStateError(myerror.str());
535                 }
536                 break;
537         case CS_Active:
538                 switch(event)
539                 {
540                 case CSE_SetDenied:
541                         m_state = CS_Denied;
542                         break;
543                 case CSE_Disconnect:
544                         m_state = CS_Disconnecting;
545                         break;
546                 case CSE_SudoSuccess:
547                         m_state = CS_SudoMode;
548                         if ((chosen_mech == AUTH_MECHANISM_SRP)
549                                         || (chosen_mech == AUTH_MECHANISM_LEGACY_PASSWORD))
550                                 srp_verifier_delete((SRPVerifier *) auth_data);
551                         chosen_mech = AUTH_MECHANISM_NONE;
552                         break;
553                 /* Init GotInit2 SetDefinitionsSent SetMediaSent SetDenied */
554                 default:
555                         myerror << "Active: Invalid client state transition! " << event;
556                         throw ClientStateError(myerror.str());
557                         break;
558                 }
559                 break;
560         case CS_SudoMode:
561                 switch(event)
562                 {
563                 case CSE_SetDenied:
564                         m_state = CS_Denied;
565                         break;
566                 case CSE_Disconnect:
567                         m_state = CS_Disconnecting;
568                         break;
569                 case CSE_SudoLeave:
570                         m_state = CS_Active;
571                         break;
572                 default:
573                         myerror << "Active: Invalid client state transition! " << event;
574                         throw ClientStateError(myerror.str());
575                         break;
576                 }
577                 break;
578         case CS_Disconnecting:
579                 /* we are already disconnecting */
580                 break;
581         }
582 }
583
584 u32 RemoteClient::uptime()
585 {
586         return getTime(PRECISION_SECONDS) - m_connection_time;
587 }
588
589 ClientInterface::ClientInterface(con::Connection* con)
590 :
591         m_con(con),
592         m_env(NULL),
593         m_print_info_timer(0.0)
594 {
595
596 }
597 ClientInterface::~ClientInterface()
598 {
599         /*
600                 Delete clients
601         */
602         {
603                 MutexAutoLock clientslock(m_clients_mutex);
604
605                 for (UNORDERED_MAP<u16, RemoteClient*>::iterator i = m_clients.begin();
606                         i != m_clients.end(); ++i) {
607                         // Delete client
608                         delete i->second;
609                 }
610         }
611 }
612
613 std::vector<u16> ClientInterface::getClientIDs(ClientState min_state)
614 {
615         std::vector<u16> reply;
616         MutexAutoLock clientslock(m_clients_mutex);
617
618         for(UNORDERED_MAP<u16, RemoteClient*>::iterator i = m_clients.begin();
619                 i != m_clients.end(); ++i) {
620                 if (i->second->getState() >= min_state)
621                         reply.push_back(i->second->peer_id);
622         }
623
624         return reply;
625 }
626
627 void ClientInterface::step(float dtime)
628 {
629         m_print_info_timer += dtime;
630         if(m_print_info_timer >= 30.0)
631         {
632                 m_print_info_timer = 0.0;
633                 UpdatePlayerList();
634         }
635 }
636
637 void ClientInterface::UpdatePlayerList()
638 {
639         if (m_env != NULL) {
640                 std::vector<u16> clients = getClientIDs();
641                 m_clients_names.clear();
642
643
644                 if(!clients.empty())
645                         infostream<<"Players:"<<std::endl;
646
647                 for (std::vector<u16>::iterator i = clients.begin(); i != clients.end(); ++i) {
648                         RemotePlayer *player = m_env->getPlayer(*i);
649
650                         if (player == NULL)
651                                 continue;
652
653                         infostream << "* " << player->getName() << "\t";
654
655                         {
656                                 MutexAutoLock clientslock(m_clients_mutex);
657                                 RemoteClient* client = lockedGetClientNoEx(*i);
658                                 if(client != NULL)
659                                         client->PrintInfo(infostream);
660                         }
661
662                         m_clients_names.push_back(player->getName());
663                 }
664         }
665 }
666
667 void ClientInterface::send(u16 peer_id, u8 channelnum,
668                 NetworkPacket* pkt, bool reliable)
669 {
670         m_con->Send(peer_id, channelnum, pkt, reliable);
671 }
672
673 void ClientInterface::sendToAll(u16 channelnum,
674                 NetworkPacket* pkt, bool reliable)
675 {
676         MutexAutoLock clientslock(m_clients_mutex);
677         for(UNORDERED_MAP<u16, RemoteClient*>::iterator i = m_clients.begin();
678                 i != m_clients.end(); ++i) {
679                 RemoteClient *client = i->second;
680
681                 if (client->net_proto_version != 0) {
682                         m_con->Send(client->peer_id, channelnum, pkt, reliable);
683                 }
684         }
685 }
686
687 RemoteClient* ClientInterface::getClientNoEx(u16 peer_id, ClientState state_min)
688 {
689         MutexAutoLock clientslock(m_clients_mutex);
690         UNORDERED_MAP<u16, RemoteClient*>::iterator n = m_clients.find(peer_id);
691         // The client may not exist; clients are immediately removed if their
692         // access is denied, and this event occurs later then.
693         if (n == m_clients.end())
694                 return NULL;
695
696         if (n->second->getState() >= state_min)
697                 return n->second;
698         else
699                 return NULL;
700 }
701
702 RemoteClient* ClientInterface::lockedGetClientNoEx(u16 peer_id, ClientState state_min)
703 {
704         UNORDERED_MAP<u16, RemoteClient*>::iterator n = m_clients.find(peer_id);
705         // The client may not exist; clients are immediately removed if their
706         // access is denied, and this event occurs later then.
707         if (n == m_clients.end())
708                 return NULL;
709
710         if (n->second->getState() >= state_min)
711                 return n->second;
712         else
713                 return NULL;
714 }
715
716 ClientState ClientInterface::getClientState(u16 peer_id)
717 {
718         MutexAutoLock clientslock(m_clients_mutex);
719         UNORDERED_MAP<u16, RemoteClient*>::iterator n = m_clients.find(peer_id);
720         // The client may not exist; clients are immediately removed if their
721         // access is denied, and this event occurs later then.
722         if (n == m_clients.end())
723                 return CS_Invalid;
724
725         return n->second->getState();
726 }
727
728 void ClientInterface::setPlayerName(u16 peer_id,std::string name)
729 {
730         MutexAutoLock clientslock(m_clients_mutex);
731         UNORDERED_MAP<u16, RemoteClient*>::iterator n = m_clients.find(peer_id);
732         // The client may not exist; clients are immediately removed if their
733         // access is denied, and this event occurs later then.
734         if (n != m_clients.end())
735                 n->second->setName(name);
736 }
737
738 void ClientInterface::DeleteClient(u16 peer_id)
739 {
740         MutexAutoLock conlock(m_clients_mutex);
741
742         // Error check
743         UNORDERED_MAP<u16, RemoteClient*>::iterator n = m_clients.find(peer_id);
744         // The client may not exist; clients are immediately removed if their
745         // access is denied, and this event occurs later then.
746         if (n == m_clients.end())
747                 return;
748
749         /*
750                 Mark objects to be not known by the client
751         */
752         //TODO this should be done by client destructor!!!
753         RemoteClient *client = n->second;
754         // Handle objects
755         for(std::set<u16>::iterator
756                         i = client->m_known_objects.begin();
757                         i != client->m_known_objects.end(); ++i)
758         {
759                 // Get object
760                 u16 id = *i;
761                 ServerActiveObject* obj = m_env->getActiveObject(id);
762
763                 if(obj && obj->m_known_by_count > 0)
764                         obj->m_known_by_count--;
765         }
766
767         // Delete client
768         delete m_clients[peer_id];
769         m_clients.erase(peer_id);
770 }
771
772 void ClientInterface::CreateClient(u16 peer_id)
773 {
774         MutexAutoLock conlock(m_clients_mutex);
775
776         // Error check
777         UNORDERED_MAP<u16, RemoteClient*>::iterator n = m_clients.find(peer_id);
778         // The client shouldn't already exist
779         if (n != m_clients.end()) return;
780
781         // Create client
782         RemoteClient *client = new RemoteClient();
783         client->peer_id = peer_id;
784         m_clients[client->peer_id] = client;
785 }
786
787 void ClientInterface::event(u16 peer_id, ClientStateEvent event)
788 {
789         {
790                 MutexAutoLock clientlock(m_clients_mutex);
791
792                 // Error check
793                 UNORDERED_MAP<u16, RemoteClient*>::iterator n = m_clients.find(peer_id);
794
795                 // No client to deliver event
796                 if (n == m_clients.end())
797                         return;
798                 n->second->notifyEvent(event);
799         }
800
801         if ((event == CSE_SetClientReady) ||
802                 (event == CSE_Disconnect)     ||
803                 (event == CSE_SetDenied))
804         {
805                 UpdatePlayerList();
806         }
807 }
808
809 u16 ClientInterface::getProtocolVersion(u16 peer_id)
810 {
811         MutexAutoLock conlock(m_clients_mutex);
812
813         // Error check
814         UNORDERED_MAP<u16, RemoteClient*>::iterator n = m_clients.find(peer_id);
815
816         // No client to get version
817         if (n == m_clients.end())
818                 return 0;
819
820         return n->second->net_proto_version;
821 }
822
823 void ClientInterface::setClientVersion(u16 peer_id, u8 major, u8 minor, u8 patch, std::string full)
824 {
825         MutexAutoLock conlock(m_clients_mutex);
826
827         // Error check
828         UNORDERED_MAP<u16, RemoteClient*>::iterator n = m_clients.find(peer_id);
829
830         // No client to set versions
831         if (n == m_clients.end())
832                 return;
833
834         n->second->setVersionInfo(major,minor,patch,full);
835 }