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