Browse Source

removed tick lags when world updates are transmitted to clients

Kolja Strohm 3 years ago
parent
commit
3d0a6628fc

+ 2 - 2
FactoryCraft/AddChunkUpdate.cpp

@@ -5,7 +5,7 @@
 
 
 AddChunkUpdate::AddChunkUpdate( Chunk* chunk )
-    : WorldUpdate( AddChunkUpdateType::ID, chunk->getDimensionId(), Framework::Vec3<int>( chunk->getCenter().x - CHUNK_SIZE / 2, chunk->getCenter().y - CHUNK_SIZE / 2, 0 ), Framework::Vec3<int>( chunk->getCenter().x + CHUNK_SIZE / 2, chunk->getCenter().y + CHUNK_SIZE / 2, WORLD_HEIGHT - 1 ) ),
+    : WorldUpdate( AddChunkUpdateType::ID, chunk->getDimensionId(), Framework::Vec3<int>( chunk->getClock().x - CHUNK_SIZE / 2, chunk->getClock().y - CHUNK_SIZE / 2, 0 ), Framework::Vec3<int>( chunk->getClock().x + CHUNK_SIZE / 2, chunk->getClock().y + CHUNK_SIZE / 2, WORLD_HEIGHT - 1 ) ),
     chunk( chunk )
 {}
 
@@ -24,7 +24,7 @@ void AddChunkUpdate::write( Framework::StreamWriter* zWriter )
     zWriter->schreibe( (char*)&AddChunkUpdateType::ID, 4 );
     int dimensionID = chunk->getDimensionId();
     zWriter->schreibe( (char*)&dimensionID, 4 );
-    Framework::Punkt center = chunk->getCenter();
+    Framework::Punkt center = chunk->getClock();
     zWriter->schreibe( (char*)&center.x, 4 );
     zWriter->schreibe( (char*)&center.y, 4 );
     chunk->save( zWriter );

+ 1 - 1
FactoryCraft/Chunk.cpp

@@ -296,7 +296,7 @@ int Chunk::getDimensionId() const
     return dimensionId;
 }
 
-Framework::Punkt Chunk::getCenter() const
+Framework::Punkt Chunk::getClock() const
 {
     return location;
 }

+ 1 - 1
FactoryCraft/Chunk.h

@@ -39,7 +39,7 @@ public:
     void save( Framework::StreamWriter* zWriter );
     void removeUnusedBlocks();
     int getDimensionId() const;
-    Framework::Punkt getCenter() const;
+    Framework::Punkt getClock() const;
     Framework::Vec3<int> getMin() const;
     Framework::Vec3<int> getMax() const;
     Game* zGameObj() const;

+ 8 - 8
FactoryCraft/Dimension.cpp

@@ -86,32 +86,32 @@ void Dimension::addEntity( Entity* entity )
 void Dimension::addChunk( Chunk* chunk )
 {
     char addr[ 8 ];
-    getAddrOfWorld( chunk->getCenter(), addr );
+    getAddrOfWorld( chunk->getClock(), addr );
     if( !chunks->z( addr, 8 ) )
     {
         chunks->set( addr, 8, chunk );
-        getAddrOfWorld( chunk->getCenter() + Punkt( CHUNK_SIZE, 0 ), addr );
+        getAddrOfWorld( chunk->getClock() + Punkt( CHUNK_SIZE, 0 ), addr );
         Chunk* zChunk = chunks->z( addr, 8 );
         if( zChunk )
         {
             zChunk->setNeighbor( WEST, chunk );
             chunk->setNeighbor( EAST, chunk );
         }
-        getAddrOfWorld( chunk->getCenter() + Punkt( -CHUNK_SIZE, 0 ), addr );
+        getAddrOfWorld( chunk->getClock() + Punkt( -CHUNK_SIZE, 0 ), addr );
         zChunk = chunks->z( addr, 8 );
         if( zChunk )
         {
             zChunk->setNeighbor( EAST, chunk );
             chunk->setNeighbor( WEST, chunk );
         }
-        getAddrOfWorld( chunk->getCenter() + Punkt( 0, CHUNK_SIZE ), addr );
+        getAddrOfWorld( chunk->getClock() + Punkt( 0, CHUNK_SIZE ), addr );
         zChunk = chunks->z( addr, 8 );
         if( zChunk )
         {
             zChunk->setNeighbor( NORTH, chunk );
             chunk->setNeighbor( SOUTH, chunk );
         }
-        getAddrOfWorld( chunk->getCenter() + Punkt( 0, -CHUNK_SIZE ), addr );
+        getAddrOfWorld( chunk->getClock() + Punkt( 0, -CHUNK_SIZE ), addr );
         zChunk = chunks->z( addr, 8 );
         if( zChunk )
         {
@@ -121,7 +121,7 @@ void Dimension::addChunk( Chunk* chunk )
     }
     else
     {
-        std::cout << "WARNING: chunk duplication at dimension " << dimensionId << " at chunk " << chunk->getCenter().x << ", " << chunk->getCenter().y << "\n";
+        std::cout << "WARNING: chunk duplication at dimension " << dimensionId << " at chunk " << chunk->getClock().x << ", " << chunk->getClock().y << "\n";
         assert( false );
         chunk->release();
     }
@@ -135,9 +135,9 @@ void Dimension::save( Text worldDir ) const
             continue;
         Datei* file = new Datei();
         Text filePath = worldDir + "/dim/" + dimensionId + "/";
-        filePath.appendHex( chunk->getCenter().x );
+        filePath.appendHex( chunk->getClock().x );
         filePath += "_";
-        filePath.appendHex( chunk->getCenter().y );
+        filePath.appendHex( chunk->getClock().y );
         filePath += ".chunk";
         file->setDatei( filePath );
         if( file->open( Datei::Style::schreiben ) )

+ 61 - 40
FactoryCraft/Game.cpp

@@ -4,6 +4,7 @@
 #include "OverworldDimension.h"
 #include "AddChunkUpdate.h"
 #include "NoBlock.h"
+#include "AsynchronCall.h"
 
 using namespace Framework;
 
@@ -13,11 +14,40 @@ GameClient::GameClient( Player* zPlayer, FCKlient* client )
     client( client ),
     viewDistance( DEFAULT_VIEW_DISTANCE ),
     first( 1 ),
-    online( 1 )
-{}
+    online( 1 ),
+    finished( 0 )
+{
+    new AsynchronCall( [this]() {
+        while( online )
+        {
+            other.lock();
+            if( updateQueue.hat( 0 ) )
+            {
+                WorldUpdate* update = updateQueue.get( 0 );
+                updateQueue.remove( 0 );
+                other.unlock();
+                background.lock();
+                this->client->zBackgroundWriter()->schreibe( (char*)&Message::WORLD_UPDATE, 1 );
+                update->write( this->client->zBackgroundWriter() );
+                background.unlock();
+                update->release();
+            }
+            else
+            {
+                other.unlock();
+                updateSync.wait();
+            }
+        }
+        finished = 1;
+    } );
+}
 
 GameClient::~GameClient()
 {
+    online = 0;
+    updateSync.notify();
+    while( !finished )
+        Sleep( 100 );
     client->release();
 }
 
@@ -30,17 +60,18 @@ void GameClient::sendWorldUpdate( WorldUpdate* update )
         int dist = update->distanceTo( pos.x, pos.y );
         if( dist < viewDistance * CHUNK_SIZE )
         {
-            other.Enter();
+            other.lock();
             int index = 0;
             for( auto update2 : updateQueue )
             {
-                int dist2 = update2->distanceTo( pos.x, pos.y );
+                int dist2 = update2->distanceTo( pohen world is s.x, pos.y );
                 if( dist2 >= dist )
                     break;
                 index++;
             }
             updateQueue.add( update, index );
-            other.Leave();
+            other.unlock();
+            updateSync.notify();
             add = 1;
         }
     }
@@ -50,15 +81,15 @@ void GameClient::sendWorldUpdate( WorldUpdate* update )
 
 void GameClient::reply( Game* zGame )
 {
-    other.Enter();
+    other.lock();
     for( auto req : requests )
         zGame->api( req, this );
     requests.leeren();
-    other.Leave();
+    other.unlock();
     int x = (int)zPlayer->getPosition().x;
     int y = (int)zPlayer->getPosition().y;
     int d = zPlayer->getCurrentDimensionId();
-    foreground.Enter();
+    foreground.lock();
     client->zForegroundWriter()->schreibe( (char*)&Message::POSITION_UPDATE, 1 );
     float f = zPlayer->getPosition().x;
     client->zForegroundWriter()->schreibe( (char*)&f, 4 );
@@ -70,17 +101,7 @@ void GameClient::reply( Game* zGame )
     client->zForegroundWriter()->schreibe( (char*)&f, 4 );
     f = zPlayer->getFaceDir().y;
     client->zForegroundWriter()->schreibe( (char*)&f, 4 );
-    foreground.Leave();
-    other.Enter();
-    if( updateQueue.hat( 0 ) )
-    {
-        background.Enter();
-        client->zBackgroundWriter()->schreibe( (char*)&Message::WORLD_UPDATE, 1 );
-        updateQueue.z( 0 )->write( client->zBackgroundWriter() );
-        updateQueue.remove( 0 );
-        background.Leave();
-    }
-    other.Leave();
+    foreground.unlock();
     // send world to client
     if( first )
     {
@@ -134,9 +155,9 @@ void GameClient::addMessage( StreamReader* reader )
     reader->lese( tmp, len );
     buffer->schreibe( tmp, len );
     delete[]tmp;
-    other.Enter();
+    other.lock();
     requests.add( buffer );
-    other.Leave();
+    other.unlock();
 }
 
 bool GameClient::isOnline() const
@@ -150,17 +171,17 @@ void GameClient::sendResponse( NetworkResponse* zResponse )
     {
         if( zResponse->isUseBackground() )
         {
-            background.Leave();
+            background.unlock();
             client->zBackgroundWriter()->schreibe( (char*)&Message::API_MESSAGE, 1 );
             zResponse->writeTo( client->zBackgroundWriter() );
-            background.Leave();
+            background.unlock();
         }
         else
         {
-            foreground.Leave();
+            foreground.unlock();
             client->zForegroundWriter()->schreibe( (char*)&Message::API_MESSAGE, 1 );
             zResponse->writeTo( client->zForegroundWriter() );
-            foreground.Leave();
+            foreground.unlock();
         }
     }
 }
@@ -221,7 +242,7 @@ void Game::thread()
         m.messungStart();
         ticker->nextTick();
         Array<int> removed;
-        cs.Enter();
+        cs.lock();
         int index = 0;
         for( auto player : *clients )
         {
@@ -237,10 +258,10 @@ void Game::thread()
         }
         for( auto i : removed )
             clients->remove( i );
-        cs.Leave();
+        cs.unlock();
         for( auto dim : *dimensions )
             dim->tickEntities( this );
-        cs.Enter();
+        cs.lock();
         while( updates->hat( 0 ) )
         {
             WorldUpdate* update = updates->z( 0 );
@@ -251,7 +272,7 @@ void Game::thread()
             update->onUpdate( zDimension( update->getAffectedDimension() ) );
             updates->remove( 0 );
         }
-        cs.Leave();
+        cs.unlock();
         for( auto client : *clients )
             client->reply( this );
         m.messungEnde();
@@ -305,14 +326,14 @@ void Game::distributeResponse( NetworkResponse* zResponse )
 
 void Game::requestWorldUpdate( WorldUpdate* update )
 {
-    cs.Enter();
+    cs.lock();
     updates->add( update );
-    cs.Leave();
+    cs.unlock();
 }
 
 GameClient* Game::addPlayer( FCKlient* client, Framework::Text name )
 {
-    cs.Enter();
+    cs.lock();
     Datei pFile;
     pFile.setDatei( path + "/player/" + name );
     Player* player;
@@ -331,9 +352,9 @@ GameClient* Game::addPlayer( FCKlient* client, Framework::Text name )
     requestArea( { (int)player->getPosition().x - DEFAULT_VIEW_DISTANCE * CHUNK_SIZE, (int)player->getPosition().y - DEFAULT_VIEW_DISTANCE * CHUNK_SIZE, (int)player->getPosition().x + DEFAULT_VIEW_DISTANCE * CHUNK_SIZE, (int)player->getPosition().y + DEFAULT_VIEW_DISTANCE * CHUNK_SIZE, player->getCurrentDimensionId() } );
     while( !zDimension( OverworldDimension::ID ) || (isNew && !zDimension( OverworldDimension::ID )->zChunk( getChunkCenter( (int)player->getPosition().x, (int)player->getPosition().y ) )) )
     {
-        cs.Leave();
+        cs.unlock();
         Sleep( 1000 );
-        cs.Enter();
+        cs.lock();
     }
     if( isNew )
     {
@@ -346,7 +367,7 @@ GameClient* Game::addPlayer( FCKlient* client, Framework::Text name )
     zDimension( OverworldDimension::ID )->addEntity( player );
     GameClient* gameClient = new GameClient( player, client );
     clients->add( gameClient );
-    cs.Leave();
+    cs.unlock();
     return dynamic_cast<GameClient*>(gameClient->getThis());
 }
 
@@ -358,17 +379,17 @@ bool Game::isChunkLoaded( int x, int y, int dimension ) const
 
 bool Game::doesChunkExist( int x, int y, int dimension )
 {
-    cs.Enter();
+    cs.lock();
     bool result = isChunkLoaded( x, y, dimension ) || loader->existsChunk( x, y, dimension );
     if( !result )
     {
         for( WorldUpdate* update : *updates )
         {
             if( update->getType() == AddChunkUpdateType::ID )
-                result |= ((AddChunkUpdate*)update)->zChunk()->getCenter() == Framework::Punkt( x, y );
+                result |= ((AddChunkUpdate*)update)->zChunk()->getClock() == Framework::Punkt( x, y );
         }
     }
-    cs.Leave();
+    cs.unlock();
     return result;
 }
 
@@ -435,9 +456,9 @@ void Game::addDimension( Dimension* d )
 
 int Game::getNextEntityId()
 {
-    cs.Enter();
+    cs.lock();
     int result = nextEntityId++;
-    cs.Leave();
+    cs.unlock();
     return result;
 }
 

+ 7 - 4
FactoryCraft/Game.h

@@ -4,6 +4,7 @@
 #include <Punkt.h>
 #include <Thread.h>
 #include <Network.h>
+#include <Critical.h>
 
 #include "Dimension.h"
 #include "Player.h"
@@ -22,15 +23,17 @@ class GameClient : public virtual Framework::ReferenceCounter
 private:
     Player* zPlayer;
     FCKlient* client;
-    CriticalSection background;
-    CriticalSection foreground;
-    CriticalSection other;
+    Critical background;
+    Critical foreground;
+    Critical other;
+    Framework::Synchronizer updateSync;
     RCArray<InMemoryBuffer> requests;
     Vec3<float> lastPos;
     RCArray<WorldUpdate> updateQueue;
     int viewDistance;
     bool first;
     bool online;
+    bool finished;
 
 public:
     GameClient( Player* zPlayer, FCKlient* client );
@@ -66,7 +69,7 @@ private:
     Framework::Text path;
     bool stop;
     __int64 tickId;
-    CriticalSection cs;
+    Critical cs;
     int nextEntityId;
     WorldGenerator* generator;
     WorldLoader* loader;

+ 25 - 25
FactoryCraft/Inventory.cpp

@@ -27,30 +27,30 @@ void InventoryInteraction::lock()
     if( !current || !other ) return;
     if( current->location.x < other->location.x )
     {
-        current->cs.Enter();
-        other->cs.Enter();
+        current->cs.lock();
+        other->cs.lock();
         return;
     }
     else if( current->location.x == other->location.x )
     {
         if( current->location.y < other->location.y )
         {
-            current->cs.Enter();
-            other->cs.Enter();
+            current->cs.lock();
+            other->cs.lock();
             return;
         }
         else if( current->location.y == other->location.y )
         {
             if( current->location.z < other->location.z )
             {
-                current->cs.Enter();
-                other->cs.Enter();
+                current->cs.lock();
+                other->cs.lock();
                 return;
             }
         }
     }
-    other->cs.Enter();
-    current->cs.Enter();
+    other->cs.lock();
+    current->cs.lock();
 }
 
 void InventoryInteraction::unlock()
@@ -58,30 +58,30 @@ void InventoryInteraction::unlock()
     if( !current || !other ) return;
     if( current->location.x < other->location.x )
     {
-        current->cs.Leave();
-        other->cs.Leave();
+        current->cs.unlock();
+        other->cs.unlock();
         return;
     }
     else if( current->location.x == other->location.x )
     {
         if( current->location.y < other->location.y )
         {
-            current->cs.Leave();
-            other->cs.Leave();
+            current->cs.unlock();
+            other->cs.unlock();
             return;
         }
         else if( current->location.y == other->location.y )
         {
             if( current->location.z < other->location.z )
             {
-                current->cs.Leave();
-                other->cs.Leave();
+                current->cs.unlock();
+                other->cs.unlock();
                 return;
             }
         }
     }
-    other->cs.Leave();
-    current->cs.Leave();
+    other->cs.unlock();
+    current->cs.unlock();
 }
 
 void InventoryInteraction::transaction( Inventory* zSource, Inventory* zTarget, ItemFilter* zFilter, Direction sourceView, Direction targetView, int count )
@@ -252,7 +252,7 @@ void Inventory::updateCache( ItemSlot* zSlot, int beforeKey )
 
 void Inventory::addSlot( ItemSlot* slot )
 {
-    cs.Enter();
+    cs.lock();
     int pullPrio = slot->getPullPriority();
     int pushPrio = slot->getPushPriority();
     int index = 0;
@@ -272,7 +272,7 @@ void Inventory::addSlot( ItemSlot* slot )
     }
     pullSlotsOrder->add( slot, index );
     updateCache( slot, -1 );
-    cs.Leave();
+    cs.unlock();
 }
 
 bool Inventory::allowPullStack( ItemSlot* zSlot, Direction dir )
@@ -338,7 +338,7 @@ void Inventory::localTransaction( Array< ItemSlot* >* zSourceSlots, Array< ItemS
 {
     if( itemCache )
     {
-        cs.Enter();
+        cs.lock();
         auto sourceSlot = zSourceSlots->begin();
         for( auto targetSlot = zTargetSlots->begin(); targetSlot; )
         {
@@ -369,7 +369,7 @@ void Inventory::localTransaction( Array< ItemSlot* >* zSourceSlots, Array< ItemS
                                     targetSlot->addItems( stack, NO_DIRECTION );
                                     if( stack->getSize() )
                                     {
-                                        cs.Leave();
+                                        cs.unlock();
                                         throw stack;
                                     }
                                     stack->release();
@@ -383,7 +383,7 @@ void Inventory::localTransaction( Array< ItemSlot* >* zSourceSlots, Array< ItemS
                             zSurceListe->remove( indexToDelete );
                         if( count == 0 )
                         {
-                            cs.Leave();
+                            cs.unlock();
                             return;
                         }
                     }
@@ -394,7 +394,7 @@ void Inventory::localTransaction( Array< ItemSlot* >* zSourceSlots, Array< ItemS
                         sourceSlot++;
                     if( !sourceSlot )
                     {
-                        cs.Leave();
+                        cs.unlock();
                         return;
                     }
                     int number = MIN( targetSlot->numberOfAddableItems( sourceSlot->zStack(), NO_DIRECTION ), count );
@@ -408,14 +408,14 @@ void Inventory::localTransaction( Array< ItemSlot* >* zSourceSlots, Array< ItemS
                             updateCache( targetSlot, targetSlot->zStack()->zItem()->zItemType()->getId() );
                             if( stack->getSize() )
                             {
-                                cs.Leave();
+                                cs.unlock();
                                 throw stack;
                             }
                             stack->release();
                             count -= number;
                             if( count == 0 )
                             {
-                                cs.Leave();
+                                cs.unlock();
                                 return;
                             }
                         }
@@ -425,7 +425,7 @@ void Inventory::localTransaction( Array< ItemSlot* >* zSourceSlots, Array< ItemS
             if( amount == count || targetSlot->isFull() )
                 targetSlot++;
         }
-        cs.Leave();
+        cs.unlock();
     }
 }
 

+ 2 - 1
FactoryCraft/Inventory.h

@@ -3,6 +3,7 @@
 #include <Vec3.h>
 #include <ReferenceCounter.h>
 #include <HashMap.h>
+#include <Critical.h>
 
 #include "ItemSlot.h"
 #include "Area.h"
@@ -36,7 +37,7 @@ private:
     Framework::RCArray<ItemSlot>* pullSlotsOrder;
     Framework::RCArray<ItemSlot>* pushSlotsOrder;
     Framework::HashMap<int, Framework::Array<ItemSlot*>*>* itemCache;
-    CriticalSection cs;
+    Framework::Critical cs;
     void updateCache( ItemSlot* zSlot, int beforeKey );
 
 protected:

+ 1 - 1
FactoryCraft/NetworkResponse.cpp

@@ -28,7 +28,7 @@ void NetworkResponse::adressChunck( Chunk* zChunk )
     adress[ 0 ] = 1; // world response
     *(int*)(adress + 1) = zChunk->getDimensionId();
     adress[ 5 ] = 1; // chunck
-    Framework::Punkt center = zChunk->getCenter();
+    Framework::Punkt center = zChunk->getClock();
     *(int*)(adress + 6) = center.x;
     *(int*)(adress + 10) = center.y;
     minPosition = zChunk->getMin();

+ 4 - 4
FactoryCraft/WorldGenerator.cpp

@@ -24,7 +24,7 @@ void WorldGenerator::thread()
 {
     while( !exit )
     {
-        cs.Enter();
+        cs.lock();
         Area next;
         bool hasNext = 0;
         if( requestQueue.getEintragAnzahl() > 0 )
@@ -33,7 +33,7 @@ void WorldGenerator::thread()
             requestQueue.remove( 0 );
             hasNext = 1;
         }
-        cs.Leave();
+        cs.unlock();
         if( !hasNext )
         {
             sleep( 1 );
@@ -60,9 +60,9 @@ void WorldGenerator::thread()
 
 void WorldGenerator::requestGeneration( Area request )
 {
-    cs.Enter();
+    cs.lock();
     requestQueue.add( request );
-    cs.Leave();
+    cs.unlock();
 }
 
 void WorldGenerator::exitAndWait()

+ 2 - 1
FactoryCraft/WorldGenerator.h

@@ -1,6 +1,7 @@
 #pragma once
 
 #include <Thread.h>
+#include <Critical.h>
 
 #include "DimensionGenerator.h"
 #include "Area.h"
@@ -10,7 +11,7 @@ class Game;
 class WorldGenerator : public Framework::Thread
 {
 private:
-    CriticalSection cs;
+    Framework::Critical cs;
     Framework::Array<Area> requestQueue;
     Game* zGame;
     Noise* noise;

+ 4 - 4
FactoryCraft/WorldLoader.cpp

@@ -45,7 +45,7 @@ void WorldLoader::thread()
 {
     while( !exit )
     {
-        cs.Enter();
+        cs.lock();
         Area next;
         bool hasNext = 0;
         if( requestQueue.getEintragAnzahl() > 0 )
@@ -54,7 +54,7 @@ void WorldLoader::thread()
             requestQueue.remove( 0 );
             hasNext = 1;
         }
-        cs.Leave();
+        cs.unlock();
         if( !hasNext )
         {
             Sleep( 1000 );
@@ -92,9 +92,9 @@ void WorldLoader::thread()
 
 void WorldLoader::requestLoading( Area request )
 {
-    cs.Enter();
+    cs.lock();
     requestQueue.add( request );
-    cs.Leave();
+    cs.unlock();
 }
 
 void WorldLoader::exitAndWait()

+ 2 - 1
FactoryCraft/WorldLoader.h

@@ -2,6 +2,7 @@
 
 #include <Thread.h>
 #include <HashMap.h>
+#include <Critical.h>
 
 #include "Area.h"
 
@@ -10,7 +11,7 @@ class Game;
 class WorldLoader : public Framework::Thread
 {
 private:
-    CriticalSection cs;
+    Framework::Critical cs;
     Framework::Array<Area> requestQueue;
     Game* zGame;
     bool exit;

+ 7 - 8
FactoryCraft/WorldUpdate.cpp

@@ -30,14 +30,13 @@ int WorldUpdate::getType() const
 
 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;
+    int xDist = MIN( abs( x - minAffected.x ), abs( x - maxAffected.x ) );
+    int yDist = MIN( abs( y - minAffected.y ), abs( y - maxAffected.y ) );
+    if( x >= minAffected.x && x <= maxAffected.x )
+        xDist = 0;
+    if( y >= minAffected.y && y <= maxAffected.y )
+        yDist = 0;
+    return xDist * xDist + yDist * yDist;
 }