Browse Source

fix missing chunks

Kolja Strohm 3 years ago
parent
commit
d3ccc4551a

+ 11 - 6
FactoryCraft/Game.cpp

@@ -27,10 +27,19 @@ void GameClient::sendWorldUpdate( WorldUpdate* update )
     if( zPlayer->getCurrentDimensionId() == update->getAffectedDimension() )
     {
         auto pos = (Vec3<int>)zPlayer->getPosition();
-        if( abs( pos.x - update->getMinAffectedPoint().x ) < viewDistance * CHUNK_SIZE || (abs( pos.x - update->getMaxAffectedPoint().x ) < viewDistance * CHUNK_SIZE) || abs( pos.y - update->getMinAffectedPoint().y ) < viewDistance * CHUNK_SIZE || (abs( pos.y - update->getMaxAffectedPoint().y ) < viewDistance * CHUNK_SIZE) )
+        int dist = update->distanceTo( pos.x, pos.y );
+        if( dist < viewDistance * CHUNK_SIZE )
         {
             other.Enter();
-            updateQueue.add( update );
+            int index = 0;
+            for( auto update2 : updateQueue )
+            {
+                int dist2 = update2->distanceTo( pos.x, pos.y );
+                if( dist2 >= dist )
+                    break;
+                index++;
+            }
+            updateQueue.add( update, index );
             other.Leave();
             add = 1;
         }
@@ -50,7 +59,6 @@ void GameClient::reply( Game* zGame )
     int y = (int)zPlayer->getPosition().y;
     int d = zPlayer->getCurrentDimensionId();
     foreground.Enter();
-    client->zForegroundWriter()->schreibe( (char*)&Message::INGAME_MESSAGE, 1 );
     client->zForegroundWriter()->schreibe( (char*)&Message::POSITION_UPDATE, 1 );
     float f = zPlayer->getPosition().x;
     client->zForegroundWriter()->schreibe( (char*)&f, 4 );
@@ -67,7 +75,6 @@ void GameClient::reply( Game* zGame )
     if( updateQueue.hat( 0 ) )
     {
         background.Enter();
-        client->zBackgroundWriter()->schreibe( (char*)&Message::INGAME_MESSAGE, 1 );
         client->zBackgroundWriter()->schreibe( (char*)&Message::WORLD_UPDATE, 1 );
         updateQueue.z( 0 )->write( client->zBackgroundWriter() );
         updateQueue.remove( 0 );
@@ -144,7 +151,6 @@ void GameClient::sendResponse( NetworkResponse* zResponse )
         if( zResponse->isUseBackground() )
         {
             background.Leave();
-            client->zBackgroundWriter()->schreibe( (char*)&Message::INGAME_MESSAGE, 1 );
             client->zBackgroundWriter()->schreibe( (char*)&Message::API_MESSAGE, 1 );
             zResponse->writeTo( client->zBackgroundWriter() );
             background.Leave();
@@ -152,7 +158,6 @@ void GameClient::sendResponse( NetworkResponse* zResponse )
         else
         {
             foreground.Leave();
-            client->zForegroundWriter()->schreibe( (char*)&Message::INGAME_MESSAGE, 1 );
             client->zForegroundWriter()->schreibe( (char*)&Message::API_MESSAGE, 1 );
             zResponse->writeTo( client->zForegroundWriter() );
             foreground.Leave();

+ 0 - 1
FactoryCraft/Game.h

@@ -48,7 +48,6 @@ private:
     class Message
     {
     public:
-        inline const static unsigned char INGAME_MESSAGE = 0;
         inline const static unsigned char TERMINATE = 1;
         inline const static unsigned char WORLD_UPDATE = 2;
         inline const static unsigned char API_MESSAGE = 3;

+ 6 - 2
FactoryCraft/Server.cpp

@@ -44,10 +44,10 @@ FactoryCraftServer::FactoryCraftServer( InitDatei* zIni )
             if( !klient )
                 continue;
             unsigned short len;
-            klient->setEmpfangTimeout( 2000 );
+            klient->setEmpfangTimeout( 5000 );
             klient->getNachricht( (char*)&len, 2 );
             char* key = new char[ len ];
-            klient->getNachricht( (char*)&key, len );
+            klient->getNachricht( (char*)key, len );
             bool bg;
             klient->getNachricht( (char*)&bg, 1 );
             klient->setEmpfangTimeout( 0 );
@@ -223,6 +223,8 @@ FCKlient::~FCKlient()
 void FCKlient::setForegroundClient( SKlient* foreground )
 {
     this->foreground = foreground;
+    foregroundReader = new NetworkReader( foreground );
+    foregroundWriter = new NetworkWriter( foreground );
     if( foreground && background )
         zGameClient = ls->zGame()->addPlayer( dynamic_cast<FCKlient*>(getThis()), Framework::Text( (int)accountId ) );
     new AsynchronCall( [this]() {
@@ -241,6 +243,8 @@ void FCKlient::setForegroundClient( SKlient* foreground )
 void FCKlient::setBackgroundClient( SKlient* background )
 {
     this->background = background;
+    backgroundReader = new NetworkReader( background );
+    backgroundWriter = new NetworkWriter( background );
     if( foreground && background )
         zGameClient = ls->zGame()->addPlayer( dynamic_cast<FCKlient*>(getThis()), Framework::Text( (int)accountId ) );
     new AsynchronCall( [this]() {

+ 4 - 4
FactoryCraft/WorldGenerator.cpp

@@ -41,11 +41,11 @@ void WorldGenerator::thread()
         }
         Punkt start = zGame->getChunkCenter( next.startX, next.startY );
         Punkt end = zGame->getChunkCenter( next.endX, next.endY );
-        int xDir = start.x > end.x ? -1 : (start.x < end.x ? 1 : 0);
-        int yDir = start.y > end.y ? -1 : (start.y < end.y ? 1 : 0);
-        for( int x = start.x; x != end.x; x += CHUNK_SIZE * xDir )
+        int xDir = start.x > end.x ? -1 : 1;
+        int yDir = start.y > end.y ? -1 : 1;
+        for( int x = start.x; xDir < 0 ? x >= end.x : x <= end.x; x += CHUNK_SIZE * xDir )
         {
-            for( int y = start.y; y != end.y; y += CHUNK_SIZE * yDir )
+            for( int y = start.y; yDir < 0 ? y >= end.y : y <= end.y; y += CHUNK_SIZE * yDir )
             {
                 if( !zGame->doesChunkExist( x, y, next.dimensionId ) )
                 {

+ 4 - 4
FactoryCraft/WorldLoader.cpp

@@ -62,11 +62,11 @@ void WorldLoader::thread()
         }
         Punkt start = zGame->getChunkCenter( next.startX, next.startY );
         Punkt end = zGame->getChunkCenter( next.endX, next.endY );
-        int xDir = start.x > end.x ? -1 : (start.x < end.x ? 1 : 0);
-        int yDir = start.y > end.y ? -1 : (start.y < end.y ? 1 : 0);
-        for( int x = start.x; x != end.x; x += CHUNK_SIZE * xDir )
+        int xDir = start.x > end.x ? -1 : 1;
+        int yDir = start.y > end.y ? -1 : 1;
+        for( int x = start.x; xDir < 0 ? x >= end.x : x <= end.x; x += CHUNK_SIZE * xDir )
         {
-            for( int y = start.y; y != end.y; y += CHUNK_SIZE * yDir )
+            for( int y = start.y; yDir < 0 ? y >= end.y : y <= end.y; y += CHUNK_SIZE * yDir )
             {
                 if( !zGame->isChunkLoaded( x, y, next.dimensionId ) )
                 {

+ 12 - 0
FactoryCraft/WorldUpdate.cpp

@@ -28,6 +28,18 @@ int WorldUpdate::getType() const
     return type;
 }
 
+int WorldUpdate::distanceTo( int x, int y ) const
+{
+    int dist = abs( x - minAffected.x );
+    if( dist > abs( x - maxAffected.x ) )
+        dist = abs( x - maxAffected.x );
+    if( dist > abs( y - minAffected.y ) )
+        dist = abs( y - minAffected.y );
+    if( dist > abs( y - maxAffected.y ) )
+        dist = abs( y - maxAffected.y );
+    return dist;
+}
+
 
 WorldUpdateType::WorldUpdateType( int id )
     : ReferenceCounter()

+ 1 - 0
FactoryCraft/WorldUpdate.h

@@ -26,6 +26,7 @@ public:
     const Framework::Vec3<int>& getMinAffectedPoint() const;
     const Framework::Vec3<int>& getMaxAffectedPoint() const;
     int getType() const;
+    int distanceTo( int x, int y ) const;
 };
 
 class WorldUpdateType : public Framework::ReferenceCounter