Bladeren bron

fixed some bugs

Kolja Strohm 3 jaren geleden
bovenliggende
commit
392a86ef01

+ 1 - 0
FactoryCraft/Block.cpp

@@ -14,6 +14,7 @@ Block::Block( ItemType *zTool, Framework::Vec3<int> pos )
     hardness = 1;
     this->zTool = zTool;
     speedModifier = 1;
+    memset( zNeighbours, 0, sizeof( Block * ) * 6 );
 }
 
 Block::~Block()

+ 1 - 1
FactoryCraft/Block.h

@@ -11,7 +11,7 @@ using namespace Framework;
 class BasicBlockItemType;
 
 #define AIR_BLOCK -1
-#define IS_BLOCK(b) b > 0
+#define IS_BLOCK(b) (((__int64)b) > 0)
 
 class Block : public Model3D, public Inventory
 {

+ 22 - 25
FactoryCraft/Chunk.cpp

@@ -10,7 +10,8 @@ Chunk::Chunk( Framework::Punkt location, int dimensionId )
     location( location )
 {
     blocks = new Block * [ CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT ];
-    memset( blocks, AIR_BLOCK, sizeof( Block * ) * CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT );
+    Block *val = (Block *)AIR_BLOCK;
+    std::uninitialized_fill_n( blocks, CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT, val );
     zNeighbours[ 0 ] = 0;
     zNeighbours[ 1 ] = 0;
     zNeighbours[ 2 ] = 0;
@@ -46,24 +47,20 @@ bool Chunk::updateVisibility()
 
 Block *Chunk::getBlockAt( Framework::Vec3<int> location ) const
 {
-    location.x += CHUNK_SIZE / 2;
-    location.y += CHUNK_SIZE / 2;
-    Block *result = dynamic_cast<Block *>( blocks[ ( location.x * CHUNK_SIZE + location.y ) * CHUNK_SIZE + location.z ]->getThis() );
-    return result;
+    Block *result = zBlockAt( location );
+    if( result )
+        return dynamic_cast<Block *>(result->getThis());
+    return 0;
 }
 
 Block *Chunk::zBlockAt( Framework::Vec3<int> location ) const
 {
-    location.x += CHUNK_SIZE / 2;
-    location.y += CHUNK_SIZE / 2;
-    return blocks[ ( location.x * CHUNK_SIZE + location.y ) * CHUNK_SIZE + location.z ];
+    return blocks[ (location.x * CHUNK_SIZE + location.y) * CHUNK_SIZE + location.z ];
 }
 
 void Chunk::putBlockAt( Framework::Vec3<int> location, Block *block )
 {
-    location.x -= this->location.x - CHUNK_SIZE / 2;
-    location.y -= this->location.x - CHUNK_SIZE / 2;
-    int index = ( location.x * CHUNK_SIZE + location.y ) * CHUNK_SIZE + location.z;
+    int index = (location.x * CHUNK_SIZE + location.y) * CHUNK_SIZE + location.z;
     Block *old = blocks[ index ];
     blocks[ index ] = block;
     Block *neighbor = currentGame->zBlockAt( location + getDirection( NORTH ), dimensionId );
@@ -75,8 +72,8 @@ void Chunk::putBlockAt( Framework::Vec3<int> location, Block *block )
         neighbor->setNeighbour( WEST, block );
     block->setNeighbour( EAST, neighbor );
     neighbor = currentGame->zBlockAt( location + getDirection( SOUTH ), dimensionId );
-    if( IS_BLOCK( neighbor ) ) {}
-    neighbor->setNeighbour( NORTH, block );
+    if( IS_BLOCK( neighbor ) )
+        neighbor->setNeighbour( NORTH, block );
     block->setNeighbour( SOUTH, neighbor );
     neighbor = currentGame->zBlockAt( location + getDirection( WEST ), dimensionId );
     if( IS_BLOCK( neighbor ) )
@@ -103,27 +100,27 @@ void Chunk::setNeighbor( Direction dir, Chunk *zChunk )
         {
             if( dir == NORTH )
             {
-                int index = i * CHUNK_SIZE * CHUNK_SIZE + z;
-                if( blocks[ index ] )
-                    blocks[ index ]->setNeighbour( NORTH, zChunk->blocks[ ( i * CHUNK_SIZE + CHUNK_SIZE - 1 ) * CHUNK_SIZE + z ] );
+                int index = i * CHUNK_SIZE * WORLD_HEIGHT + z;
+                if( IS_BLOCK( blocks[ index ] ) )
+                    blocks[ index ]->setNeighbour( NORTH, zChunk->blocks[ (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z ] );
             }
             else if( dir == EAST )
             {
-                int index = ( ( CHUNK_SIZE - 1 ) * CHUNK_SIZE + i ) * CHUNK_SIZE + z;
-                if( blocks[ index ] )
-                    blocks[ index ]->setNeighbour( EAST, zChunk->blocks[ i * CHUNK_SIZE + z ] );
+                int index = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
+                if( IS_BLOCK( blocks[ index ] ) )
+                    blocks[ index ]->setNeighbour( EAST, zChunk->blocks[ i * WORLD_HEIGHT + z ] );
             }
             else if( dir == SOUTH )
             {
-                int index = ( i * CHUNK_SIZE + CHUNK_SIZE - 1 ) * CHUNK_SIZE + z;
-                if( blocks[ index ] )
-                    blocks[ index ]->setNeighbour( SOUTH, zChunk->blocks[ i * CHUNK_SIZE * CHUNK_SIZE + z ] );
+                int index = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
+                if( IS_BLOCK( blocks[ index ] ) )
+                    blocks[ index ]->setNeighbour( SOUTH, zChunk->blocks[ i * CHUNK_SIZE * WORLD_HEIGHT + z ] );
             }
             else if( dir == WEST )
             {
-                int index = i * CHUNK_SIZE + z;
-                if( blocks[ index ] )
-                    blocks[ index ]->setNeighbour( WEST, zChunk->blocks[ ( ( CHUNK_SIZE - 1 ) * CHUNK_SIZE + i ) * CHUNK_SIZE + z ] );
+                int index = i * WORLD_HEIGHT + z;
+                if( IS_BLOCK( blocks[ index ] ) )
+                    blocks[ index ]->setNeighbour( WEST, zChunk->blocks[ ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z ] );
             }
         }
     }

+ 5 - 3
FactoryCraft/Dimension.cpp

@@ -38,8 +38,7 @@ void Dimension::getAddrOf( Punkt cPos, char *addr ) const
 
 void Dimension::getAddrOfWorld( Punkt wPos, char *addr ) const
 {
-    wPos.x = (int)floor( ( (float)wPos.x + CHUNK_SIZE / 2 ) / CHUNK_SIZE );
-    wPos.y = (int)floor( ( (float)wPos.y + CHUNK_SIZE / 2 ) / CHUNK_SIZE );
+    wPos /= CHUNK_SIZE;
     getAddrOf( wPos, addr );
 }
 
@@ -52,7 +51,10 @@ Chunk *Dimension::zChunk( Punkt wPos ) const
 
 Block *Dimension::zBlock( Vec3<int> location )
 {
-    return zChunk( Punkt( location.x, location.y ) )->zBlockAt( Vec3<int>( ( location.x + CHUNK_SIZE / 2 ) % CHUNK_SIZE, ( location.y + CHUNK_SIZE / 2 ) % CHUNK_SIZE, location.z ) );
+    Chunk *c = zChunk( Punkt( location.x, location.y ) );
+    if( c )
+        return c->zBlockAt( Vec3<int>( location.x % CHUNK_SIZE, location.y % CHUNK_SIZE, location.z ) );
+    return 0;
 }
 
 void Dimension::addEntity( Entity *entity )

+ 0 - 1
FactoryCraft/Game.cpp

@@ -6,7 +6,6 @@ Game::Game( Bildschirm *zScreen )
     : Menu( zScreen )
 {
     logout = initKnopf( 10, 10, 200, 20, Knopf::Style::Normal, "Verlassen" );
-    elements.add( logout );
     logout->setMausEreignis( [this]( void *p, void *o, MausEreignis me )
     {
         if( me.id == ME_RLinks )

+ 2 - 0
FactoryCraft/Inventoty.cpp

@@ -5,6 +5,8 @@ using namespace Framework;
 
 
 Inventory::Inventory( const Framework::Vec3<float> location )
+    : ReferenceCounter(),
+    location( location )
 {
     pullSlotsOrder = new Framework::RCArray<ItemSlot>();
     pushSlotsOrder = new Framework::RCArray<ItemSlot>();

+ 1 - 2
FactoryCraft/Main.cpp

@@ -14,7 +14,7 @@
 
 int KSGStart Framework::Start( Framework::Startparam p )
 {
-    Network::Start( 10 );
+    Network::Start( 20 );
     initVariables();
 
     Datei d;
@@ -58,7 +58,6 @@ int KSGStart Framework::Start( Framework::Startparam p )
     rTh.beginn();
     StartNachrichtenSchleife();
     rTh.beenden();
-
     releaseVariables();
     Network::Exit();
     return 0;

+ 6 - 6
FactoryCraft/Menu.cpp

@@ -1,5 +1,5 @@
 #include "Menu.h"
-
+#include <AsynchronCall.h>
 
 Menu::Menu( Bildschirm *zScreen )
     : ReferenceCounter(),
@@ -10,14 +10,14 @@ void Menu::show()
 {
     for( auto i = elements.getIterator(); i; i++ )
     {
-        zScreen->addMember( dynamic_cast<Zeichnung *>( i->getThis() ) );
+        zScreen->addMember( dynamic_cast<Zeichnung *>(i->getThis()) );
     }
 }
 
 void Menu::hide()
 {
-    for( auto i = elements.getIterator(); i; i++ )
-    {
-        zScreen->removeMember( dynamic_cast<Zeichnung *>( i->getThis() ) );
-    }
+    new AsynchronCall( [this]() {
+        for( auto i = elements.getIterator(); i; i++ )
+            zScreen->removeMember( dynamic_cast<Zeichnung *>(i->getThis()) );
+    } );
 }

+ 2 - 0
FactoryCraft/NetworkHandler.cpp

@@ -93,6 +93,8 @@ bool NetworkHandler::connect( Text ip, short port )
 {
     if( !gsc )
         gsc = msc->createMinigameServerClient();
+    if (!gsc)
+        return 0;
     Text *secret = gsc->getSecret();
     if( !secret )
         return 0;

+ 80 - 46
FactoryCraft/World.cpp

@@ -8,79 +8,113 @@ using namespace Network;
 using namespace Framework;
 
 World::World( Bildschirm3D *zScreen )
+	: Thread()
 {
-    renderedWorld = new Welt3D();
-    dimensions = new RCArray<Dimension>();
-    currentPlayer = new CurrentPlayer();
-    zScreenPtr = zScreen;
-    kam = new Kam3D();
-    kam->setWelt( renderedWorld );
-    zScreen->addKamera( kam );
+	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();
-    kam->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 );
-        }
-    }
+	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 );
+	}
 }
 
 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;
 }

+ 3 - 1
FactoryCraft/World.h

@@ -3,11 +3,12 @@
 #include <Welt3D.h>
 #include <Bildschirm.h>
 #include <Kam3D.h>
+#include <Thread.h>
 
 #include "Dimension.h"
 #include "CurrentPlayer.h"
 
-class World : public Framework::ReferenceCounter
+class World : public Framework::Thread
 {
 private:
     Framework::RCArray<Dimension> *dimensions;
@@ -21,6 +22,7 @@ public:
     ~World();
     void update();
     void setChunk( Chunk *chunk, int dimensionId );
+    void thread() override;
 
     Block *zBlockAt( Framework::Vec3<int> location, int dimension ) const;
     Dimension *zDimension( int id ) const;