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