Guest User

Untitled

a guest
Feb 3rd, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 29.16 KB | None | 0 0
  1. #include "ClientMapManagement.h"
  2.  
  3. ClientMapManagement::ClientMapManagement()
  4. {
  5.     current_map=NULL;
  6.     stopIt=false;
  7.     basePath=QCoreApplication::applicationDirPath()+"/datapack/map/tmx/";
  8.     #ifdef POKECRAFT_SERVER_MAP_MANAGEMENT_LIST_TYPE_C_MALLOC
  9.     to_send_map_management_move_size=0;
  10.     #endif
  11. }
  12.  
  13. ClientMapManagement::~ClientMapManagement()
  14. {
  15.     stopIt=true;
  16.     QMutexLocker lock(&mutex);
  17.     wait_the_end.acquire();
  18. }
  19.  
  20. void ClientMapManagement::setBasePath(const QString &basePath)
  21. {
  22.     this->basePath=basePath;
  23. }
  24.  
  25. void ClientMapManagement::stop()
  26. {
  27.     stopIt=true;
  28. }
  29.  
  30. Map_player_info ClientMapManagement::getMapPlayerInfo()
  31. {
  32.     Map_player_info temp;
  33.     if(current_map==NULL)
  34.         temp.loaded=false;
  35.     else
  36.     {
  37.         temp.map        = current_map->map_final;
  38.         temp.map_file_path  = current_map->mapName();
  39.         temp.x          = x;
  40.         temp.y          = y;
  41.         temp.loaded     = true;
  42.     }
  43.     return temp;
  44. }
  45.  
  46. void ClientMapManagement::setVariable(QList<Map_custom *> *map_list,EventThreader* map_loader_thread)
  47. {
  48.     this->map_loader_thread=map_loader_thread;
  49.     this->map_list=map_list;
  50. }
  51.  
  52. void ClientMapManagement::askIfIsReadyToStop()
  53. {
  54.     stopIt=true;
  55.     QMutexLocker lock(&mutex);
  56.     wait_the_end.release();
  57.     if(current_map==NULL)
  58.     {
  59.         emit isReadyToStop();
  60.         return;
  61.     }
  62.     unloadFromCurrentMap();
  63.     to_send_map_management_insert.clear();
  64. #if defined(POKECRAFT_SERVER_MAP_MANAGEMENT_LIST_TYPE_QT) || defined(POKECRAFT_SERVER_MAP_MANAGEMENT_LIST_TYPE_QT_MUTABLELISTITERATOR) || defined(POKECRAFT_SERVER_MAP_MANAGEMENT_LIST_TYPE_QT_QHASH)
  65. to_send_map_management_move.clear();
  66. #endif
  67. #ifdef POKECRAFT_SERVER_MAP_MANAGEMENT_LIST_TYPE_STL_LIST
  68. to_send_map_management_move.clear();
  69. #endif
  70. #ifdef POKECRAFT_SERVER_MAP_MANAGEMENT_LIST_TYPE_C_MALLOC
  71. if(to_send_map_management_move_size>0)
  72. {
  73.     free((void*)to_send_map_management_move);
  74.     to_send_map_management_move_size=0;
  75. }
  76. #endif
  77.     to_send_map_management_remove.clear();
  78.     //virtual stop the player
  79.     if(last_direction>4)
  80.         last_direction=Direction((quint8)last_direction-4);
  81.     Orientation orientation=(Orientation)last_direction;
  82.     if(current_map->mapName()!=at_start_map_name || x!=at_start_x || y!=at_start_y || orientation!=at_start_orientation)
  83.     {
  84.         emit updatePlayerPosition(current_map->mapName(),x,y,orientation);
  85.         #ifdef DEBUG_MESSAGE_CLIENT_MOVE
  86.         DebugClass::debugConsole(
  87.                     QString("current_map->mapName(): %1,x: %2,y: %3, orientation: %4")
  88.                     .arg(current_map->mapName())
  89.                     .arg(x)
  90.                     .arg(y)
  91.                     .arg(orientation)
  92.                     );
  93.         #endif
  94.     }
  95.     unloadMapIfNeeded(current_map);
  96.     current_map=NULL;
  97.     emit isReadyToStop();
  98. }
  99.  
  100. void ClientMapManagement::unloadFromCurrentMap()
  101. {
  102.     //remove player to current map
  103.     if(current_map->clients.removeAll(this)!=1)
  104.     //if(!current_map->clients.removeOne(this))
  105.     {
  106.         emit message("unable to remove the player from the map");
  107.         return;
  108.     }
  109.     //tell at all other client the disconnect of this
  110.     if(current_map->map_loaded)
  111.     {
  112.         int index_maps_clients=0;
  113.         QList<ClientMapManagement *> client_near=current_map->clients;
  114.         int list_size_maps_clients=client_near.size();
  115.         while(index_maps_clients<list_size_maps_clients)
  116.         {
  117.             client_near.at(index_maps_clients)->removeClient(player_id);
  118.             index_maps_clients++;
  119.         }
  120.     }
  121. }
  122.  
  123. void ClientMapManagement::unloadMapIfNeeded(Map_custom * map)
  124. {
  125.     //not more client, can unload the map
  126.     if(map->clients.size()==0)
  127.     {
  128.         map_list->removeOne(map);
  129.         /// \note auto unregistred on other map by destructor
  130.         delete map;
  131.     }
  132. }
  133.  
  134. void ClientMapManagement::put_on_the_map(const quint32 &player_id,const QString & map,const quint16 &x,const quint16 &y,const Orientation &orientation)
  135. {
  136.     have_diff=false;
  137.     at_start_x=x;
  138.     at_start_y=y;
  139.     at_start_orientation=orientation;
  140.     last_direction=(Direction)orientation;
  141.     at_start_map_name=map;
  142.     this->player_id=player_id;
  143.     this->x=x;
  144.     this->y=y;
  145.     current_map=getMap(map);
  146.     current_map->clients << this;
  147.     propagate();
  148. }
  149.  
  150. void ClientMapManagement::put_on_the_map_by_diff(const QString & map,const quint16 &x,const quint16 &y)
  151. {
  152.     DebugClass::debugConsole(QString("try load map by diff for for player: %1").arg(player_id));
  153.     have_diff=true;
  154.     this->x=x;
  155.     this->y=y;
  156.     current_map=getMap(map);
  157.     current_map->clients << this;
  158.     propagate();
  159.     //set to 0 or map max size
  160.     //moveThePlayer() to check colision and do real move
  161. }
  162.  
  163. void ClientMapManagement::propagate()
  164. {
  165.     if(current_map==NULL)
  166.     {
  167.         DebugClass::debugConsole(QString("current_map==NULL and should not: %1").arg(player_id));
  168.         return;
  169.     }
  170.     if(current_map->map_loaded)
  171.     {
  172.         if(have_diff)
  173.         {
  174.             have_diff=false;
  175.             switch(last_direction)
  176.             {
  177.                 case 0x05:
  178.                     y+=0;//useless but it's y=0 + offset stored into y
  179.                 break;
  180.                 case 0x06:
  181.                     x+=0;//useless but it's x=0 + offset stored into x
  182.                 break;
  183.                 case 0x07:
  184.                     y+=current_map->height;//it's y=height + offset stored into y, then y=y+height
  185.                 break;
  186.                 case 0x08:
  187.                     x+=current_map->height;//it's x=height + offset stored into x, then x=x+height
  188.                 break;
  189.                 default:
  190.                 break;
  191.             }
  192.         }
  193.         else
  194.         {
  195.             current_map->check_client_position(current_map->clients.size()-1);
  196.             int index_maps_clients=0;
  197.             int list_size_map_custom=current_map->clients.size();
  198.             while(index_maps_clients<list_size_map_custom)
  199.             {
  200.                 ClientMapManagement *other_client=current_map->clients.at(index_maps_clients);
  201.                 if(other_client!=this)
  202.                     other_client->insertClient(player_id,current_map->mapName(),x,y,last_direction);
  203.                 insertClient(other_client->player_id,
  204.                          other_client->current_map->mapName(),
  205.                          other_client->x,
  206.                          other_client->y,
  207.                          other_client->last_direction);
  208.                 index_maps_clients++;
  209.             }
  210.         }
  211.     }
  212. }
  213.  
  214. QList<ClientMapManagement *> ClientMapManagement::preMapMove()
  215. {
  216.     QList<ClientMapManagement *> previousClient;
  217.     if(stopIt)
  218.         return previousClient;
  219.     getNearClient(previousClient);
  220.     unloadFromCurrentMap();
  221.     old_map=current_map;
  222.     return previousClient;
  223. }
  224.  
  225. void ClientMapManagement::postMapMove(const QList<ClientMapManagement *> &previousClient,const quint8 &previousMovedUnit,const Direction &direction)
  226. {
  227.     if(stopIt)
  228.         return;
  229.     postMapMove_nextClient.clear();
  230.     getNearClient(postMapMove_nextClient);
  231.     #ifdef DEBUG_MESSAGE_CLIENT_COMPLEXITY_LINEARE
  232.     emit message(QString("postMapMove start: %1, getNearClient: %2, previousClient: %3").arg(player_id).arg(postMapMove_nextClient.size()).arg(previousClient.size()));
  233.     #endif // DEBUG_MESSAGE_CLIENT_COMPLEXITY_LINEARE
  234.     index=0;
  235.     list_size=previousClient.size();
  236.     while(index<list_size)
  237.     {
  238.         if(postMapMove_nextClient.contains(previousClient.at(index)))
  239.         {
  240.             previousClient.at(index)->moveClient(player_id,previousMovedUnit,direction);
  241.             postMapMove_nextClient.removeOne(previousClient.at(index));
  242.         }
  243.         else
  244.             previousClient.at(index)->removeClient(player_id);
  245.         index++;
  246.     }
  247.     postMapMove_size=postMapMove_nextClient.size();
  248.     while(postMapMove_size>0)
  249.     {
  250.         postMapMove_nextClient.first()->insertClient(player_id,current_map->mapName(),x,y,(Direction)direction);
  251.         postMapMove_nextClient.removeFirst();
  252.         postMapMove_size--;
  253.     }
  254.     unloadMapIfNeeded(old_map);
  255.     return;
  256. }
  257.  
  258. /// \note The second heavy function
  259. void ClientMapManagement::moveThePlayer(const quint8 &previousMovedUnit,const Direction &direction)
  260. {
  261.     if(stopIt)
  262.         return;
  263.     if(current_map==NULL)
  264.     {
  265.         emit message(QString("internal error, map pointer == NULL, for player: %1").arg(player_id));
  266.         return;
  267.     }
  268.     if(current_map->map_loaded)
  269.     {
  270.         #ifdef DEBUG_MESSAGE_CLIENT_COMPLEXITY_SQUARE
  271.         emit message(QString("for player (%1,%2): %3, previousMovedUnit: %4, direction: %5").arg(x).arg(y).arg(player_id).arg(previousMovedUnit).arg(Map_custom::directionToString((Direction)direction)));
  272.         #endif
  273.         moveThePlayer_index_move=0;
  274.         switch(last_direction)
  275.         {
  276.             case Direction_move_at_top:
  277.                 if(previousMovedUnit==0)
  278.                     emit error(QString("Previous action is moving: %1").arg(last_direction));
  279.                 while(moveThePlayer_index_move<previousMovedUnit)
  280.                 {
  281.                     if(0==y)
  282.                     {
  283.                         if(current_map->map_border_top.fileName.isEmpty())
  284.                             emit error(QString("moveThePlayer(), out of map: %1, file name: \"%2\"").arg(current_map->mapName()).arg(current_map->map_border_top.fileName));
  285.                         else
  286.                         {
  287.                             QList<ClientMapManagement *> previousClient=preMapMove();
  288.                             put_on_the_map_by_diff(current_map->map_border_top.fileName,current_map->map_border_top.x_offset+x,-(previousMovedUnit-moveThePlayer_index_move));
  289.                             postMapMove(previousClient,previousMovedUnit,direction);
  290.                         }
  291.                         return;
  292.                     }
  293.                     y--;
  294.                     if(!current_map->is_walkalble(x,y))
  295.                     {
  296.                         emit error(QString("move at top, can wall at: %1,%2 on map: %3").arg(x).arg(y).arg(current_map->mapName()));
  297.                         return;
  298.                     }
  299.                     moveThePlayer_index_move++;
  300.                 }
  301.             break;
  302.             case Direction_look_at_top:
  303.                 if(previousMovedUnit>0)
  304.                     emit error(QString("Previous action is not moving: %1").arg(last_direction));
  305.             break;
  306.             case Direction_move_at_right:
  307.                 if(previousMovedUnit==0)
  308.                     emit error(QString("Previous action is moving: %1").arg(last_direction));
  309.                 while(moveThePlayer_index_move<previousMovedUnit)
  310.                 {
  311.                     if(current_map->width==x)
  312.                     {
  313.                         if(current_map->map_border_right.fileName.isEmpty())
  314.                             emit error(QString("moveThePlayer(): move at right, out of map: %1, file name: \"%2\"").arg(current_map->mapName()).arg(current_map->map_border_right.fileName));
  315.                         else
  316.                         {
  317.                             QList<ClientMapManagement *> previousClient=preMapMove();
  318.                             put_on_the_map_by_diff(current_map->map_border_right.fileName,current_map->map_border_right.y_offset+y,(previousMovedUnit-moveThePlayer_index_move));
  319.                             postMapMove(previousClient,previousMovedUnit,direction);
  320.                         }
  321.                         return;
  322.                     }
  323.                     x++;
  324.                     if(!current_map->is_walkalble(x,y))
  325.                     {
  326.                         emit error(QString("move at right, can wall at: %1,%2 on map: %3").arg(x).arg(y).arg(current_map->mapName()));
  327.                         return;
  328.                     }
  329.                     moveThePlayer_index_move++;
  330.                 }
  331.             break;
  332.             case Direction_look_at_right:
  333.                 if(previousMovedUnit>0)
  334.                     emit error(QString("Previous action is not moving: %1").arg(last_direction));
  335.             break;
  336.             case Direction_move_at_bottom:
  337.                 if(previousMovedUnit==0)
  338.                     emit error(QString("Previous action is moving: %1").arg(last_direction));
  339.                 while(moveThePlayer_index_move<previousMovedUnit)
  340.                 {
  341.                     if(current_map->height==y)
  342.                     {
  343.                         if(current_map->map_border_bottom.fileName.isEmpty())
  344.                             emit error(QString("moveThePlayer(): move at bottom, out of map: %1, file name: \"%2\"").arg(current_map->mapName()).arg(current_map->map_border_bottom.fileName));
  345.                         else
  346.                         {
  347.                             QList<ClientMapManagement *> previousClient=preMapMove();
  348.                             put_on_the_map_by_diff(current_map->map_border_bottom.fileName,current_map->map_border_bottom.x_offset+x,(previousMovedUnit-moveThePlayer_index_move));
  349.                             postMapMove(previousClient,previousMovedUnit,direction);
  350.                         }
  351.                         return;
  352.                     }
  353.                     y++;
  354.                     if(!current_map->is_walkalble(x,y))
  355.                     {
  356.                         emit error(QString("move at bottom, can wall at: %1,%2 on map: %3").arg(x).arg(y).arg(current_map->mapName()));
  357.                         return;
  358.                     }
  359.                     moveThePlayer_index_move++;
  360.                 }
  361.             break;
  362.             case Direction_look_at_bottom:
  363.                 if(previousMovedUnit>0)
  364.                     emit error(QString("Previous action is not moving: %1").arg(last_direction));
  365.             break;
  366.             case Direction_move_at_left:
  367.                 if(previousMovedUnit==0)
  368.                     emit error(QString("Previous action is moving: %1").arg(last_direction));
  369.                 while(moveThePlayer_index_move<previousMovedUnit)
  370.                 {
  371.                     if(0==x)
  372.                     {
  373.                         if(current_map->map_border_left.fileName.isEmpty())
  374.                             emit error(QString("moveThePlayer(): move at left, out of map: %1, file name: \"%2\"").arg(current_map->mapName()).arg(current_map->map_border_left.fileName));
  375.                         else
  376.                         {
  377.                             QList<ClientMapManagement *> previousClient=preMapMove();
  378.                             put_on_the_map_by_diff(current_map->map_border_left.fileName,current_map->map_border_left.y_offset+y,-(previousMovedUnit-moveThePlayer_index_move));
  379.                             postMapMove(previousClient,previousMovedUnit,direction);
  380.                         }
  381.                         return;
  382.                     }
  383.                     x--;
  384.                     if(!current_map->is_walkalble(x,y))
  385.                     {
  386.                         emit error(QString("can wall at: %1,%2 on map: %3").arg(x).arg(y).arg(current_map->mapName()));
  387.                         return;
  388.                     }
  389.                     moveThePlayer_index_move++;
  390.                 }
  391.             break;
  392.             case Direction_look_at_left:
  393.                 if(previousMovedUnit>0)
  394.                     emit error(QString("Previous action is not moving: %1").arg(last_direction));
  395.             break;
  396.             default:
  397.             break;
  398.         }
  399.         moveThePlayer_returnList.clear();
  400.         getNearClient(moveThePlayer_returnList);
  401.         moveThePlayer_index=0;
  402.         moveThePlayer_list_size=moveThePlayer_returnList.size();
  403.         while(moveThePlayer_index<moveThePlayer_list_size)
  404.         {
  405.             moveThePlayer_returnList[moveThePlayer_index]->moveClient(player_id,previousMovedUnit,(Direction)direction);
  406.             moveThePlayer_index++;
  407.         }
  408.         last_direction=(Direction)direction;
  409.         #ifdef DEBUG_MESSAGE_CLIENT_MOVE
  410.         emit message(QString("after %4: (%1,%2): %3, send at %5 player(s)").arg(x).arg(y).arg(player_id).arg(Map_custom::directionToString((Direction)direction)).arg(moveThePlayer_list_size));
  411.         #endif
  412.     }
  413.     else
  414.     {
  415. /*      map_management_movement temp;
  416.         temp.movedUnit=previousMovedUnit;
  417.         temp.direction=(Direction)direction;
  418.         delayed_map_management_move << temp;*/
  419.     }
  420. }
  421.  
  422. //send client only on near map loaded and not the current player
  423. void ClientMapManagement::getNearClient(QList<ClientMapManagement *> & returnList)
  424. {
  425.     if(!current_map->map_loaded)
  426.         return;
  427.     returnList << current_map->clients;
  428.     /// \todo uncomment and fix this code
  429.     /*
  430.     getNearClient_index=0;
  431.     getNearClient_list_size=current_map->clients.size();
  432.     while(getNearClient_index<getNearClient_list_size)
  433.     {
  434.         if(current_map->clients.at(getNearClient_index)!=this)
  435.             returnList << current_map->clients.at(getNearClient_index);
  436.         getNearClient_index++;
  437.     }*/
  438. }
  439.  
  440. void ClientMapManagement::insertClient(const quint32 &player_id,const QString &map,const quint16 &x,const quint16 &y,const Direction &direction)
  441. {
  442.     if(current_map==NULL)
  443.     {
  444.         emit message("internal error, map pointer == NULL at insertClient()");
  445.         return;
  446.     }
  447.     insertClient_temp.fileName=map;
  448.     insertClient_temp.id=player_id;
  449.     insertClient_temp.direction=direction;
  450.     insertClient_temp.x=x;
  451.     insertClient_temp.y=y;
  452.     to_send_map_management_insert << insertClient_temp;
  453. }
  454.  
  455. /// \note The first heavy function
  456. void ClientMapManagement::moveClient(const quint32 &player_id,const quint8 &movedUnit,const Direction &direction)
  457. {
  458.     #ifdef DEBUG_MESSAGE_CLIENT_COMPLEXITY_SQUARE
  459.     emit message(QString("moveClient(%1,%2,%3)").arg(player_id).arg(movedUnit).arg(Map_custom::directionToString((Direction)direction)));
  460.     #endif
  461.     moveClient_tempMov.movedUnit=movedUnit;
  462.     moveClient_tempMov.direction=direction;
  463.  
  464.  
  465.  
  466.     #ifdef POKECRAFT_SERVER_MAP_MANAGEMENT_LIST_TYPE_QT
  467.     moveClient_list_size=to_send_map_management_move.size();
  468.     moveClient_index=0;
  469.     while(moveClient_index<moveClient_list_size)
  470.     {
  471.         if(to_send_map_management_move[moveClient_index].id==player_id)
  472.         {
  473.             to_send_map_management_move[moveClient_index].movement_list << moveClient_tempMov;
  474.             return;
  475.         }
  476.         moveClient_index++;
  477.     }
  478.     moveClient_temp.id=player_id;
  479.     moveClient_temp.movement_list.clear();
  480.     moveClient_temp.movement_list << moveClient_tempMov;
  481.     to_send_map_management_move << moveClient_temp;
  482.     #endif
  483.  
  484.  
  485.  
  486.     #ifdef POKECRAFT_SERVER_MAP_MANAGEMENT_LIST_TYPE_QT_MUTABLELISTITERATOR
  487.     QMutableListIterator<map_management_move> i(to_send_map_management_move);
  488.     while(i.hasNext())
  489.     {
  490.         if(i.next().id==player_id)
  491.         {
  492.             i.next().movement_list << moveClient_tempMov;
  493.             return;
  494.         }
  495.     }
  496.     moveClient_temp.id=player_id;
  497.     moveClient_temp.movement_list.clear();
  498.     moveClient_temp.movement_list << moveClient_tempMov;
  499.     to_send_map_management_move << moveClient_temp;
  500.     #endif
  501.  
  502.  
  503.  
  504.  
  505.     #ifdef POKECRAFT_SERVER_MAP_MANAGEMENT_LIST_TYPE_QT_QHASH
  506.     if(to_send_map_management_move.contains(player_id))
  507.     {
  508.         to_send_map_management_move[player_id] << moveClient_tempMov;
  509.         return;
  510.     }
  511.     to_send_map_management_move[player_id] << moveClient_tempMov;
  512.     #endif
  513.  
  514.  
  515.  
  516.     #ifdef POKECRAFT_SERVER_MAP_MANAGEMENT_LIST_TYPE_STL_LIST
  517.     moveClient_list_size=to_send_map_management_move.size();
  518.     moveClient_index=0;
  519.     while(moveClient_index<moveClient_list_size)
  520.     {
  521.         if(to_send_map_management_move[moveClient_index].id==player_id)
  522.         {
  523.             to_send_map_management_move[moveClient_index].movement_list << moveClient_tempMov;
  524.             return;
  525.         }
  526.         moveClient_index++;
  527.     }
  528.     moveClient_temp.id=player_id;
  529.     moveClient_temp.movement_list.clear();
  530.     moveClient_temp.movement_list << moveClient_tempMov;
  531.     to_send_map_management_move << moveClient_temp;
  532.     #endif
  533.  
  534.  
  535.  
  536.     #ifdef POKECRAFT_SERVER_MAP_MANAGEMENT_LIST_TYPE_C_MALLOC
  537.     moveClient_index=0;
  538.     while(moveClient_index<to_send_map_management_move_size)
  539.     {
  540.         if(to_send_map_management_move[moveClient_index].id==player_id)
  541.         {
  542.             to_send_map_management_move[moveClient_index].movement_list << moveClient_tempMov;
  543.             return;
  544.         }
  545.         moveClient_index++;
  546.     }
  547.     moveClient_temp.id=player_id;
  548.     moveClient_temp.movement_list.clear();
  549.     moveClient_temp.movement_list << moveClient_tempMov;
  550.     if(to_send_map_management_move_size==0)
  551.         to_send_map_management_move=(map_management_move*)malloc(sizeof(map_management_move));
  552.     else
  553.         to_send_map_management_move=(map_management_move*)realloc(to_send_map_management_move,sizeof(map_management_move)*(to_send_map_management_move_size+1));
  554.     memcpy(
  555.                 (void*)(to_send_map_management_move+to_send_map_management_move_size*sizeof(map_management_move)),
  556.                 (void*)&moveClient_temp,
  557.                 sizeof(map_management_move)
  558.                 );
  559.     to_send_map_management_move_size++;
  560.     #endif
  561. }
  562.  
  563. void ClientMapManagement::removeClient(const quint32 &player_id)
  564. {
  565.     if(current_map==NULL)
  566.     {
  567.         emit message("internal error, map pointer == NULL at removeClient()");
  568.         return;
  569.     }
  570.     if(stopIt)
  571.         return;
  572.     if(to_send_map_management_remove.contains(player_id))
  573.         emit message("try dual remove");
  574.     else
  575.     {
  576.         #ifdef POKECRAFT_SERVER_MAP_MANAGEMENT_LIST_TYPE_QT
  577.         list_size=to_send_map_management_move.size();
  578.         index=0;
  579.         while(index<list_size)
  580.         {
  581.             if(to_send_map_management_move.at(index).id==player_id)
  582.             {
  583.                 to_send_map_management_move.removeAt(index);
  584.                 return;
  585.             }
  586.             index++;
  587.         }
  588.         #endif
  589.  
  590.  
  591.  
  592.         #ifdef POKECRAFT_SERVER_MAP_MANAGEMENT_LIST_TYPE_QT_MUTABLELISTITERATOR
  593.         list_size=to_send_map_management_move.size();
  594.         index=0;
  595.         while(index<list_size)
  596.         {
  597.             if(to_send_map_management_move.at(index).id==player_id)
  598.             {
  599.                 to_send_map_management_move.removeAt(index);
  600.                 return;
  601.             }
  602.             index++;
  603.         }
  604.         #endif
  605.  
  606.  
  607.  
  608.         #ifdef POKECRAFT_SERVER_MAP_MANAGEMENT_LIST_TYPE_QT_QHASH
  609.         to_send_map_management_move.remove(player_id);
  610.         #endif
  611.  
  612.  
  613.  
  614.         #ifdef POKECRAFT_SERVER_MAP_MANAGEMENT_LIST_TYPE_STL_LIST
  615.         list_size=to_send_map_management_move.size();
  616.         index=0;
  617.         while(index<list_size)
  618.         {
  619.             if(to_send_map_management_move.at(index).id==player_id)
  620.             {
  621.                 to_send_map_management_move.removeAt(index);
  622.                 return;
  623.             }
  624.             index++;
  625.         }
  626.         #endif
  627.  
  628.  
  629.  
  630.         #ifdef POKECRAFT_SERVER_MAP_MANAGEMENT_LIST_TYPE_C_MALLOC
  631.         index=0;
  632.         while(index<to_send_map_management_move_size)
  633.         {
  634.             if(to_send_map_management_move[index].id==player_id)
  635.             {
  636.                 memmove(
  637.                             (void*)(to_send_map_management_move+index*sizeof(map_management_move)),
  638.                             (void*)(to_send_map_management_move+(index+1)*sizeof(map_management_move)),
  639.                             sizeof(map_management_move)*(index-to_send_map_management_move_size)
  640.                             );
  641.                 to_send_map_management_move=(map_management_move*)realloc(to_send_map_management_move,sizeof(map_management_move)*(to_send_map_management_move_size-1));
  642.                 to_send_map_management_move_size--;
  643.                 return;
  644.             }
  645.             index++;
  646.         }
  647.         #endif
  648.  
  649.  
  650.  
  651.  
  652.         to_send_map_management_remove << player_id;
  653.     }
  654. }
  655.  
  656. void ClientMapManagement::purgeBuffer()
  657. {
  658.     if(to_send_map_management_insert.size()==0 &&
  659.         #if defined(POKECRAFT_SERVER_MAP_MANAGEMENT_LIST_TYPE_QT) || defined(POKECRAFT_SERVER_MAP_MANAGEMENT_LIST_TYPE_QT_MUTABLELISTITERATOR) || defined(POKECRAFT_SERVER_MAP_MANAGEMENT_LIST_TYPE_QT_QHASH)
  660.         to_send_map_management_move.size()==0
  661.         #endif
  662.         #ifdef POKECRAFT_SERVER_MAP_MANAGEMENT_LIST_TYPE_STL_LIST
  663.         to_send_map_management_move.size()==0
  664.         #endif
  665.         #ifdef POKECRAFT_SERVER_MAP_MANAGEMENT_LIST_TYPE_C_MALLOC
  666.         to_send_map_management_move_size==0
  667.         #endif
  668.         && to_send_map_management_remove.size()==0)
  669.         return;
  670.     if(stopIt)
  671.         return;
  672.     QMutexLocker lock(&mutex);
  673.  
  674.     #ifdef DEBUG_MESSAGE_CLIENT_COMPLEXITY_LINEARE
  675.     emit message("purgeBuffer() runing....");
  676.     #endif
  677.  
  678.     purgeBuffer_player_affected=0;
  679.  
  680.     purgeBuffer_outputData.clear();
  681.     QDataStream out(&purgeBuffer_outputData, QIODevice::WriteOnly);
  682.     out.setVersion(QDataStream::Qt_4_4);
  683.     out << (quint8)0xC0;
  684.     purgeBuffer_outputDataLoop.clear();
  685.     QDataStream outLoop(&purgeBuffer_outputDataLoop, QIODevice::WriteOnly);
  686.     outLoop.setVersion(QDataStream::Qt_4_4);
  687.  
  688.     purgeBuffer_index=0;
  689.     purgeBuffer_list_size=to_send_map_management_remove.size();
  690.     while(purgeBuffer_index<purgeBuffer_list_size)
  691.     {
  692.         if(stopIt)
  693.             return;
  694.         #ifdef DEBUG_MESSAGE_CLIENT_COMPLEXITY_SQUARE
  695.         emit message(
  696.                     QString("player_id to remove: %1, for player: %2")
  697.                     .arg(to_send_map_management_remove.at(purgeBuffer_index))
  698.                     .arg(player_id)
  699.                      );
  700.         #endif
  701.         outLoop << to_send_map_management_remove.at(purgeBuffer_index);
  702.         outLoop << (quint8)0x03;
  703.         purgeBuffer_index++;
  704.         purgeBuffer_player_affected++;
  705.     }
  706.     to_send_map_management_remove.clear();
  707.  
  708.     purgeBuffer_index=0;
  709.     purgeBuffer_list_size=to_send_map_management_insert.size();
  710.     while(purgeBuffer_index<purgeBuffer_list_size)
  711.     {
  712.         if(stopIt)
  713.             return;
  714.         #ifdef DEBUG_MESSAGE_CLIENT_COMPLEXITY_SQUARE
  715.         emit message(
  716.                     QString("insert player_id: %1,mapName %2,x: %3,y: %4,direction: %5, for player: %6")
  717.                     .arg(to_send_map_management_insert.at(purgeBuffer_index).id)
  718.                     .arg(to_send_map_management_insert.at(purgeBuffer_index).fileName)
  719.                     .arg(to_send_map_management_insert.at(purgeBuffer_index).x)
  720.                     .arg(to_send_map_management_insert.at(purgeBuffer_index).y)
  721.                     .arg(Map_custom::directionToString(to_send_map_management_insert.at(purgeBuffer_index).direction))
  722.                     .arg(player_id)
  723.                      );
  724.         #endif
  725.         outLoop << to_send_map_management_insert.at(purgeBuffer_index).id;
  726.         outLoop << (quint8)0x01;
  727.         outLoop << to_send_map_management_insert.at(purgeBuffer_index).fileName;
  728.         outLoop << to_send_map_management_insert.at(purgeBuffer_index).x;
  729.         outLoop << to_send_map_management_insert.at(purgeBuffer_index).y;
  730.         outLoop << (quint8)to_send_map_management_insert.at(purgeBuffer_index).direction;
  731.         purgeBuffer_index++;
  732.         purgeBuffer_player_affected++;
  733.     }
  734.     to_send_map_management_insert.clear();
  735.  
  736. #if defined(POKECRAFT_SERVER_MAP_MANAGEMENT_LIST_TYPE_QT) || defined(POKECRAFT_SERVER_MAP_MANAGEMENT_LIST_TYPE_QT_MUTABLELISTITERATOR)
  737.     purgeBuffer_index=0;
  738.     purgeBuffer_list_size=to_send_map_management_move.size();
  739.     purgeBuffer_list_size_internal=0;
  740.     purgeBuffer_indexMovement=0;
  741.     while(purgeBuffer_index<purgeBuffer_list_size)
  742.     {
  743.         if(stopIt)
  744.             return;
  745.         purgeBuffer_move=to_send_map_management_move.at(purgeBuffer_index);
  746.         #ifdef DEBUG_MESSAGE_CLIENT_COMPLEXITY_SQUARE
  747.         emit message(
  748.                     QString("move player_id: %1, for player: %2")
  749.                     .arg(purgeBuffer_move.id)
  750.                     .arg(player_id)
  751.                      );
  752.         #endif
  753.         outLoop << purgeBuffer_move.id;
  754.         outLoop << (quint8)0x02;
  755.         purgeBuffer_list_size_internal=purgeBuffer_move.movement_list.size();
  756.         if(purgeBuffer_list_size_internal==0)
  757.             DebugClass::debugConsole(QString("for player: %1, list_size_internal==0").arg(this->player_id));
  758.         outLoop << (quint8)purgeBuffer_list_size_internal;
  759.         purgeBuffer_indexMovement=0;
  760.         while(purgeBuffer_indexMovement<purgeBuffer_list_size_internal)
  761.         {
  762.             if(stopIt)
  763.                 return;
  764.             outLoop << purgeBuffer_move.movement_list.at(purgeBuffer_indexMovement).movedUnit;
  765.             outLoop << (quint8)purgeBuffer_move.movement_list.at(purgeBuffer_indexMovement).direction;
  766.             purgeBuffer_indexMovement++;
  767.         }
  768.         purgeBuffer_player_affected++;
  769.         purgeBuffer_index++;
  770.         if(stopIt)
  771.             return;
  772.     }
  773.     if(stopIt)
  774.         return;
  775.     to_send_map_management_move.clear();
  776. #endif
  777.  
  778.  
  779.  
  780. #ifdef POKECRAFT_SERVER_MAP_MANAGEMENT_LIST_TYPE_QT_QHASH
  781.     purgeBuffer_list_size_internal=0;
  782.     purgeBuffer_indexMovement=0;
  783.     QHash<quint32, QList<map_management_movement> >::const_iterator i = to_send_map_management_move.constBegin();
  784.     while (i != to_send_map_management_move.constEnd())
  785.     {
  786.         if(stopIt)
  787.             return;
  788.         #ifdef DEBUG_MESSAGE_CLIENT_COMPLEXITY_SQUARE
  789.         emit message(
  790.                     QString("move player_id: %1, for player: %2")
  791.                     .arg(i.key())
  792.                     .arg(player_id)
  793.                      );
  794.         #endif
  795.         outLoop << i.key();
  796.         outLoop << (quint8)0x02;
  797.         purgeBuffer_list_size_internal=i.value().size();
  798.         if(purgeBuffer_list_size_internal==0)
  799.             DebugClass::debugConsole(QString("for player: %1, list_size_internal==0").arg(this->player_id));
  800.         outLoop << (quint8)purgeBuffer_list_size_internal;
  801.         purgeBuffer_indexMovement=0;
  802.         while(purgeBuffer_indexMovement<purgeBuffer_list_size_internal)
  803.         {
  804.             if(stopIt)
  805.                 return;
  806.             outLoop << i.value().at(purgeBuffer_indexMovement).movedUnit;
  807.             outLoop << (quint8)i.value().at(purgeBuffer_indexMovement).direction;
  808.             purgeBuffer_indexMovement++;
  809.         }
  810.         purgeBuffer_player_affected++;
  811.         if(stopIt)
  812.             return;
  813.         ++i;
  814.     }
  815.     if(stopIt)
  816.         return;
  817.     to_send_map_management_move.clear();
  818. #endif
  819.  
  820.  
  821. #ifdef POKECRAFT_SERVER_MAP_MANAGEMENT_LIST_TYPE_STL_LIST
  822.     purgeBuffer_index=0;
  823.     purgeBuffer_list_size=to_send_map_management_move.size();
  824.     purgeBuffer_list_size_internal=0;
  825.     purgeBuffer_indexMovement=0;
  826.     while(purgeBuffer_index<purgeBuffer_list_size)
  827.     {
  828.         if(stopIt)
  829.             return;
  830.         purgeBuffer_move=to_send_map_management_move.at(purgeBuffer_index);
  831.         #ifdef DEBUG_MESSAGE_CLIENT_COMPLEXITY_SQUARE
  832.         emit message(
  833.                     QString("move player_id: %1, for player: %2")
  834.                     .arg(purgeBuffer_move.id)
  835.                     .arg(player_id)
  836.                      );
  837.         #endif
  838.         outLoop << purgeBuffer_move.id;
  839.         outLoop << (quint8)0x02;
  840.         purgeBuffer_list_size_internal=purgeBuffer_move.movement_list.size();
  841.         if(purgeBuffer_list_size_internal==0)
  842.             DebugClass::debugConsole(QString("for player: %1, list_size_internal==0").arg(this->player_id));
  843.         outLoop << (quint8)purgeBuffer_list_size_internal;
  844.         purgeBuffer_indexMovement=0;
  845.         while(purgeBuffer_indexMovement<purgeBuffer_list_size_internal)
  846.         {
  847.             if(stopIt)
  848.                 return;
  849.             outLoop << purgeBuffer_move.movement_list.at(purgeBuffer_indexMovement).movedUnit;
  850.             outLoop << (quint8)purgeBuffer_move.movement_list.at(purgeBuffer_indexMovement).direction;
  851.             purgeBuffer_indexMovement++;
  852.         }
  853.         purgeBuffer_player_affected++;
  854.         purgeBuffer_index++;
  855.         if(stopIt)
  856.             return;
  857.     }
  858.     if(stopIt)
  859.         return;
  860.     to_send_map_management_move.clear();
  861. #endif
  862.  
  863.  
  864.  
  865. #ifdef POKECRAFT_SERVER_MAP_MANAGEMENT_LIST_TYPE_C_MALLOC
  866.     purgeBuffer_index=0;
  867.     purgeBuffer_list_size_internal=0;
  868.     purgeBuffer_indexMovement=0;
  869.     while(purgeBuffer_index<to_send_map_management_move_size)
  870.     {
  871.         if(stopIt)
  872.             return;
  873.         purgeBuffer_move=to_send_map_management_move[purgeBuffer_index];
  874.         #ifdef DEBUG_MESSAGE_CLIENT_COMPLEXITY_SQUARE
  875.         emit message(
  876.                     QString("move player_id: %1, for player: %2")
  877.                     .arg(purgeBuffer_move.id)
  878.                     .arg(player_id)
  879.                      );
  880.         #endif
  881.         outLoop << purgeBuffer_move.id;
  882.         outLoop << (quint8)0x02;
  883.         purgeBuffer_list_size_internal=purgeBuffer_move.movement_list.size();
  884.         if(purgeBuffer_list_size_internal==0)
  885.             DebugClass::debugConsole(QString("for player: %1, list_size_internal==0").arg(this->player_id));
  886.         outLoop << (quint8)purgeBuffer_list_size_internal;
  887.         purgeBuffer_indexMovement=0;
  888.         while(purgeBuffer_indexMovement<purgeBuffer_list_size_internal)
  889.         {
  890.             if(stopIt)
  891.                 return;
  892.             outLoop << purgeBuffer_move.movement_list.at(purgeBuffer_indexMovement).movedUnit;
  893.             outLoop << (quint8)purgeBuffer_move.movement_list.at(purgeBuffer_indexMovement).direction;
  894.             purgeBuffer_indexMovement++;
  895.         }
  896.         purgeBuffer_player_affected++;
  897.         purgeBuffer_index++;
  898.         if(stopIt)
  899.             return;
  900.     }
  901.     if(stopIt)
  902.         return;
  903.     if(to_send_map_management_move_size>0)
  904.     {
  905.         free((void*)to_send_map_management_move);
  906.         to_send_map_management_move_size=0;
  907.     }
  908. #endif
  909.  
  910.  
  911.  
  912.     if(stopIt)
  913.         return;
  914.  
  915.     #ifdef DEBUG_MESSAGE_CLIENT_COMPLEXITY_SQUARE
  916.     emit message(QString("player_affected: %1").arg(purgeBuffer_player_affected));
  917.     #endif
  918.     out << purgeBuffer_player_affected;
  919.     purgeBuffer_outputData+=purgeBuffer_outputDataLoop;
  920.     emit sendPacket(purgeBuffer_outputData);
  921. }
  922.  
  923. Map_custom * ClientMapManagement::getMap(const QString & mapName)
  924. {
  925.     int index=0;
  926.     int map_list_size=map_list->size();
  927.     while(index<map_list_size)
  928.     {
  929.         //current map found in already loaded
  930.         if(map_list->at(index)->mapName()==mapName)
  931.             return map_list->at(index);
  932.         index++;
  933.     }
  934.     *map_list << new Map_custom(map_loader_thread,map_list);
  935.     map_list->last()->moveToThread(this->thread());
  936.     map_list->last()->loadMap(mapName,basePath);
  937.     return map_list->last();
  938. }
  939.  
  940. void ClientMapManagement::mapError(QString errorString)
  941. {
  942.     QByteArray outputData;
  943.     QDataStream out(&outputData, QIODevice::WriteOnly);
  944.     out.setVersion(QDataStream::Qt_4_4);
  945.     out << (quint8)0xC2;
  946.     out << (quint32)0x00000008;
  947.     out << (quint8)0x03;
  948.     out << "Unable to load the current map";
  949.     emit sendPacket(outputData);
  950.     emit error(errorString);
  951. }
Advertisement
Add Comment
Please, Sign In to add comment