Explorar o código

fix negative block address calculation

Kolja Strohm %!s(int64=3) %!d(string=hai) anos
pai
achega
b1a6a262a0
Modificáronse 3 ficheiros con 94 adicións e 78 borrados
  1. 11 2
      FactoryCraft/Dimension.cpp
  2. 82 76
      FactoryCraft/World.cpp
  3. 1 0
      FactoryCraft/World.h

+ 11 - 2
FactoryCraft/Dimension.cpp

@@ -2,6 +2,7 @@
 #include "Constants.h"
 #include "Datei.h"
 #include "Game.h"
+#include "Globals.h"
 
 using namespace Framework;
 
@@ -55,9 +56,17 @@ Chunk *Dimension::zChunk( Punkt wPos ) const
 
 Block *Dimension::zBlock( Vec3<int> location )
 {
-    Chunk *c = zChunk( Punkt( location.x, location.y ) );
+    Chunk *c = zChunk( currentGame->getChunkCenter( location.x, location.y ) );
     if( c )
-        return c->zBlockAt( Vec3<int>( location.x % CHUNK_SIZE, location.y % CHUNK_SIZE, location.z ) );
+    {
+        int x = location.x % CHUNK_SIZE;
+        int y = location.y % CHUNK_SIZE;
+        if( x < 0 )
+            x += CHUNK_SIZE;
+        if( y < 0 )
+            y += CHUNK_SIZE;
+        return c->zBlockAt( Vec3<int>( x, y, location.z ) );
+    }
     return 0;
 }
 

+ 82 - 76
FactoryCraft/World.cpp

@@ -3,118 +3,124 @@
 #include "World.h"
 #include "Globals.h"
 #include "WorldUpdate.h"
+#include "Constants.h"
 
 using namespace Network;
 using namespace Framework;
 
 World::World( Bildschirm3D *zScreen )
-	: Thread()
+    : Thread()
 {
-	renderedWorld = new Welt3D();
-	dimensions = new RCArray<Dimension>();
-	currentPlayer = new CurrentPlayer();
-	zScreenPtr = zScreen;
-	kam = new Kam3D();
-	kam->setWelt( renderedWorld );
-	zScreen->addKamera( kam );
-	start();
+    renderedWorld = new Welt3D();
+    dimensions = new RCArray<Dimension>();
+    currentPlayer = new CurrentPlayer();
+    zScreenPtr = zScreen;
+    kam = new Kam3D();
+    kam->setWelt( renderedWorld );
+    zScreen->addKamera( kam );
+    start();
 }
 
 World::~World()
 {
-	zScreenPtr->removeKamera( kam );
-	dimensions->release();
-	currentPlayer->release();
+    zScreenPtr->removeKamera( kam );
+    dimensions->release();
+    currentPlayer->release();
 }
 
 void World::update()
 {
-	NetworkReader *serverMessageReader = 0;
-	const char type = 0;
-	while( serverMessageReader = network->zFactoryClient()->getNextMessage() )
-	{
-		serverMessageReader->lese( (char *)&type, 1 );
-		if( type == 2 ) // WORLD UPDATE
-		{
-			int id = 0;
-			serverMessageReader->lese( (char *)&id, 4 );
-			StaticRegistry<WorldUpdateType>::INSTANCE.zElement( id )->applyUpdate( serverMessageReader );
-		}
-		if( type == 3 ) // API MESSAGE
-		{
-			// TODO: process messages
-		}
-		if( type == 4 ) // POSITION UPDATE
-		{
-			Vec3<float> pos, dir;
-			serverMessageReader->lese( (char *)&pos.x, 4 );
-			serverMessageReader->lese( (char *)&pos.y, 4 );
-			serverMessageReader->lese( (char *)&pos.z, 4 );
-			kam->setPosition( pos );
-			serverMessageReader->lese( (char *)&dir.x, 4 );
-			serverMessageReader->lese( (char *)&dir.y, 4 );
-			dir.z = 0;
-			if( dir.getLengthSq() > 0 )
-				kam->setAusrichtung( pos + dir * 10 );
-		}
-	}
+    NetworkReader *serverMessageReader = 0;
+    const char type = 0;
+    while( serverMessageReader = network->zFactoryClient()->getNextMessage() )
+    {
+        serverMessageReader->lese( (char *)&type, 1 );
+        if( type == 2 ) // WORLD UPDATE
+        {
+            int id = 0;
+            serverMessageReader->lese( (char *)&id, 4 );
+            StaticRegistry<WorldUpdateType>::INSTANCE.zElement( id )->applyUpdate( serverMessageReader );
+        }
+        if( type == 3 ) // API MESSAGE
+        {
+            // TODO: process messages
+        }
+        if( type == 4 ) // POSITION UPDATE
+        {
+            Vec3<float> pos, dir;
+            serverMessageReader->lese( (char *)&pos.x, 4 );
+            serverMessageReader->lese( (char *)&pos.y, 4 );
+            serverMessageReader->lese( (char *)&pos.z, 4 );
+            kam->setPosition( pos );
+            serverMessageReader->lese( (char *)&dir.x, 4 );
+            serverMessageReader->lese( (char *)&dir.y, 4 );
+            dir.z = 0;
+            if( dir.getLengthSq() > 0 )
+                kam->setAusrichtung( pos + dir * 10 );
+        }
+    }
 }
 
 void World::setChunk( Chunk *chunk, int dimensionId )
 {
-	Dimension *zDim = zDimension( dimensionId );
-	if( !zDim )
-	{
-		zDim = new Dimension( dimensionId );
-		dimensions->add( zDim );
-	}
-	zDim->addChunk( chunk );
-	zDim->updateVisibility();
+    Dimension *zDim = zDimension( dimensionId );
+    if( !zDim )
+    {
+        zDim = new Dimension( dimensionId );
+        dimensions->add( zDim );
+    }
+    zDim->addChunk( chunk );
+    zDim->updateVisibility();
 }
 
 void World::thread()
 {
-	while( true )
-	{
-		zScreenPtr->lock();
-		if( currentGame != this )
-		{
-			zScreenPtr->unlock();
-			return;
-		}
-		update();
-		zScreenPtr->unlock();
-		Sleep( 10 );
-	}
+    while( true )
+    {
+        zScreenPtr->lock();
+        if( currentGame != this )
+        {
+            zScreenPtr->unlock();
+            return;
+        }
+        update();
+        zScreenPtr->unlock();
+        Sleep( 10 );
+    }
 }
 
 Block *World::zBlockAt( Framework::Vec3<int> location, int dimension ) const
 {
-	Dimension *dim = zDimension( dimension );
-	if( dim )
-		return dim->zBlock( location );
-	return 0;
+    Dimension *dim = zDimension( dimension );
+    if( dim )
+        return dim->zBlock( location );
+    return 0;
 }
 
 Dimension *World::zDimension( int id ) const
 {
-	for( auto dim = dimensions->getIterator(); dim; dim++ )
-	{
-		if( dim->getDimensionId() == id )
-			return dim;
-	}
-	return 0;
+    for( auto dim = dimensions->getIterator(); dim; dim++ )
+    {
+        if( dim->getDimensionId() == id )
+            return dim;
+    }
+    return 0;
 }
 
 void World::setVisibility( Framework::Model3D *zModel, bool visible )
 {
-	if( visible )
-		renderedWorld->addZeichnung( dynamic_cast<Framework::Model3D *>(zModel->getThis()) );
-	else
-		renderedWorld->removeZeichnung( zModel );
+    if( visible )
+        renderedWorld->addZeichnung( dynamic_cast<Framework::Model3D *>(zModel->getThis()) );
+    else
+        renderedWorld->removeZeichnung( zModel );
 }
 
 Framework::Bildschirm3D *World::zScreen() const
 {
-	return zScreenPtr;
+    return zScreenPtr;
+}
+
+Framework::Punkt World::getChunkCenter( int x, int y ) const
+{
+    return Punkt( (x / CHUNK_SIZE) * CHUNK_SIZE + (x < 0 ? -CHUNK_SIZE : CHUNK_SIZE) / 2, (y / CHUNK_SIZE) * CHUNK_SIZE + (y < 0 ? -CHUNK_SIZE : CHUNK_SIZE) / 2 );
 }

+ 1 - 0
FactoryCraft/World.h

@@ -28,4 +28,5 @@ public:
     Dimension *zDimension( int id ) const;
     void setVisibility( Framework::Model3D *zModel, bool visible );
     Framework::Bildschirm3D *zScreen() const;
+    Framework::Punkt getChunkCenter( int x, int y ) const;
 };