Преглед изворни кода

add foreground and background connection

Kolja Strohm пре 3 година
родитељ
комит
9dc6078ac5

+ 1 - 0
FactoryCraft/AddChunkUpdate.cpp

@@ -16,6 +16,7 @@ void AddChunkUpdateType::applyUpdate( Framework::StreamReader* zReader )
     Framework::Punkt center;
     zReader->lese( (char*)&center.x, 4 );
     zReader->lese( (char*)&center.y, 4 );
+    std::cout << "downloading chunk " << center.x << ", " << center.y << "\n";
     Chunk* chunk = new Chunk( center, dimensionID );
     chunk->load( zReader );
     currentGame->setChunk( chunk, dimensionID );

+ 31 - 37
FactoryCraft/Dimension.cpp

@@ -37,7 +37,6 @@ void Dimension::getAddrOf( Punkt cPos, char* addr ) const
 {
     *(int*)addr = cPos.x;
     *((int*)addr + 1) = cPos.y;
-    addr[ 8 ] = 0;
 }
 
 void Dimension::getAddrOfWorld( Punkt wPos, char* addr ) const
@@ -52,9 +51,9 @@ void Dimension::getAddrOfWorld( Punkt wPos, char* addr ) const
 
 Chunk* Dimension::zChunk( Punkt wPos ) const
 {
-    char addr[ 9 ];
+    char addr[ 8 ];
     getAddrOfWorld( wPos, addr );
-    return chunks->z( addr, 9 );
+    return chunks->z( addr, 8 );
 }
 
 Framework::Either<Block*, int> Dimension::zBlock( Vec3<int> location )
@@ -80,43 +79,38 @@ void Dimension::addEntity( Entity* entity )
 
 void Dimension::addChunk( Chunk* chunk )
 {
-    char addr[ 9 ];
+    char addr[ 8 ];
     getAddrOfWorld( chunk->getCenter(), addr );
-    if( !chunks->z( addr, 9 ) )
+    chunks->set( addr, 8, chunk );
+    chunkList.add( chunk );
+    getAddrOfWorld( chunk->getCenter() + Punkt( CHUNK_SIZE, 0 ), addr );
+    Chunk* zChunk = chunks->z( addr, 8 );
+    if( zChunk )
     {
-        chunks->set( addr, 9, chunk );
-        chunkList.add( chunk );
-        getAddrOfWorld( chunk->getCenter() + Punkt( CHUNK_SIZE, 0 ), addr );
-        Chunk* zChunk = chunks->z( addr, 9 );
-        if( zChunk )
-        {
-            zChunk->setNeighbor( WEST, chunk );
-            chunk->setNeighbor( EAST, chunk );
-        }
-        getAddrOfWorld( chunk->getCenter() + Punkt( -CHUNK_SIZE, 0 ), addr );
-        zChunk = chunks->z( addr, 9 );
-        if( zChunk )
-        {
-            zChunk->setNeighbor( EAST, chunk );
-            chunk->setNeighbor( WEST, chunk );
-        }
-        getAddrOfWorld( chunk->getCenter() + Punkt( 0, CHUNK_SIZE ), addr );
-        zChunk = chunks->z( addr, 9 );
-        if( zChunk )
-        {
-            zChunk->setNeighbor( NORTH, chunk );
-            chunk->setNeighbor( SOUTH, chunk );
-        }
-        getAddrOfWorld( chunk->getCenter() + Punkt( 0, -CHUNK_SIZE ), addr );
-        zChunk = chunks->z( addr, 9 );
-        if( zChunk )
-        {
-            zChunk->setNeighbor( SOUTH, chunk );
-            chunk->setNeighbor( NORTH, chunk );
-        }
+        zChunk->setNeighbor( WEST, chunk );
+        chunk->setNeighbor( EAST, chunk );
+    }
+    getAddrOfWorld( chunk->getCenter() + 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 );
+    zChunk = chunks->z( addr, 8 );
+    if( zChunk )
+    {
+        zChunk->setNeighbor( NORTH, chunk );
+        chunk->setNeighbor( SOUTH, chunk );
+    }
+    getAddrOfWorld( chunk->getCenter() + Punkt( 0, -CHUNK_SIZE ), addr );
+    zChunk = chunks->z( addr, 8 );
+    if( zChunk )
+    {
+        zChunk->setNeighbor( SOUTH, chunk );
+        chunk->setNeighbor( NORTH, chunk );
     }
-    else
-        chunk->release();
 }
 
 int Dimension::getDimensionId() const

+ 63 - 27
FactoryCraft/FactoryClient.cpp

@@ -5,8 +5,10 @@ using namespace Network;
 FactoryClient::FactoryClient()
 {
     client = 0;
-    connected = 0;
-    reader = 0;
+    background = 0;
+    foreground = 0;
+    backgroundReader = 0;
+    foregroundReader = 0;
 }
 
 FactoryClient::~FactoryClient()
@@ -22,7 +24,6 @@ bool FactoryClient::connect( Text ip, short port, int accountId, Text secret )
     client = new SSLKlient();
     if( !client->verbinde( port, ip ) )
         return false;
-    connected = 1;
     if( !client->sende( "\1", 1 ) )
         return false;
     if( !client->sende( (char*)&accountId, 4 ) )
@@ -35,7 +36,39 @@ bool FactoryClient::connect( Text ip, short port, int accountId, Text secret )
     char result = 0;
     if( !client->getNachricht( &result, 1 ) || result != 1 )
         return false;
-    reader = new NetworkReader( client );
+    int keyLen;
+    if( !client->getNachricht( (char*)&keyLen, 4 ) )
+        return false;
+    char* key = new char[ keyLen ];
+    if( !client->getNachricht( key, keyLen ) )
+        return false;
+    int p;
+    if( !client->getNachricht( (char*)&p, 4 ) )
+        return false;
+    foreground = new Klient();
+    if( !foreground->verbinde( p, ip ) )
+        return false;
+    unsigned short l = (unsigned short)keyLen;
+    if( !foreground->sende( (char*)&l, 2 ) )
+        return false;
+    if( !foreground->sende( key, keyLen ) )
+        return false;
+    bool bg = 0;
+    if( !foreground->sende( (char*)&bg, 1 ) )
+        return false;
+    foregroundReader = new NetworkReader( foreground );
+    background = new Klient();
+    if( !background->verbinde( p, ip ) )
+        return false;
+    if( !background->sende( (char*)&l, 2 ) )
+        return false;
+    if( !background->sende( key, keyLen ) )
+        return false;
+    bg = 1;
+    if( !background->sende( (char*)&bg, 1 ) )
+        return false;
+    backgroundReader = new NetworkReader( background );
+    client->trenne();
     return true;
 }
 
@@ -43,45 +76,48 @@ void FactoryClient::disconnect()
 {
     if( client )
     {
-        if( connected )
-            client->sende( "\2", 1 );
-        delete reader;
-        client->trenne();
+        delete foregroundReader;
+        foregroundReader = 0;
+        delete backgroundReader;
+        backgroundReader = 0;
         client->release();
         client = 0;
+        if( foreground )
+            foreground->release();
+        foreground = 0;
+        if( background )
+            background->release();
+        background = 0;
     }
 }
 
-NetworkReader* FactoryClient::getNextMessage()
+NetworkReader* FactoryClient::getNextForegroundMessage()
 {
-    if( !client )
+    if( !foreground )
         return 0;
-    if( !client->hatNachricht( 0 ) )
+    if( !foreground->hatNachricht( 0 ) )
         return 0;
-    char type = 0;
-    if( !client->getNachricht( &type, 1 ) )
+    return foregroundReader;
+}
+
+NetworkReader* FactoryClient::getNextBackgroundMessage()
+{
+    if( !background )
         return 0;
-    if( type == 1 )
-    {
-        client->trenne();
-        client->release();
-        client = 0;
+    if( !background->hatNachricht( 0 ) )
         return 0;
-    }
-    return reader;
+    return backgroundReader;
 }
 
 void FactoryClient::sendPlayerAction( void* data, unsigned short length )
 {
-    if( !client )
+    if( !foreground )
         return;
     cs.lock();
-    char msgId = 3;
-    client->sende( &msgId, 1 );
     length += 1;
-    client->sende( (char*)&length, 2 );
-    msgId = 2;
-    client->sende( &msgId, 1 );
-    client->sende( (char*)data, length - 1 );
+    foreground->sende( (char*)&length, 2 );
+    char msgId = 2;
+    foreground->sende( &msgId, 1 );
+    foreground->sende( (char*)data, length - 1 );
     cs.unlock();
 }

+ 7 - 4
FactoryCraft/FactoryClient.h

@@ -10,16 +10,19 @@ class FactoryClient : public Framework::ReferenceCounter
 {
 private:
     Network::SSLKlient* client;
-    bool connected;
-    void disconnect();
-    Network::NetworkReader* reader;
+    Network::Klient* foreground;
+    Network::Klient* background;
+    Network::NetworkReader* foregroundReader;
+    Network::NetworkReader* backgroundReader;
     Framework::Critical cs;
+    void disconnect();
 
 public:
     FactoryClient();
     ~FactoryClient();
 
     bool connect( Text ip, short port, int accountId, Text secret );
-    Network::NetworkReader* getNextMessage();
+    Network::NetworkReader* getNextForegroundMessage();
+    Network::NetworkReader* getNextBackgroundMessage();
     void sendPlayerAction( void* data, unsigned short length );
 };

+ 19 - 0
FactoryCraft/Game.cpp

@@ -22,4 +22,23 @@ Game::Game( Bildschirm* zScreen )
         return 1;
     } );
     elements.add( logout );
+    debug = initTextFeld( 10, 40, 500, 20, TextFeld::Style::Text, "" );
+    elements.add( debug );
+}
+
+void Game::updatePositionAndFace( Vec3<float> position, Vec2<float> face )
+{
+    Text txt = "Position: (";
+    txt.setPrecision( 2 );
+    txt += position.x;
+    txt += ", ";
+    txt += position.y;
+    txt += ", ";
+    txt += position.z;
+    txt += ") Face: (";
+    txt += face.x;
+    txt += ", ";
+    txt += face.y;
+    txt += ")";
+    debug->setText( txt );
 }

+ 3 - 0
FactoryCraft/Game.h

@@ -8,8 +8,11 @@ class Game : public Menu
 {
 private:
     Knopf* logout;
+    TextFeld* debug;
 
 public:
     // Konstruktor
     Game( Bildschirm* zScreen );
+
+    void updatePositionAndFace( Vec3<float> position, Vec2<float> face );
 };

+ 1 - 1
FactoryCraft/Main.cpp

@@ -35,7 +35,7 @@ int KSGStart Framework::Start( Framework::Startparam p )
     WNDCLASS wc = Framework::F_Normal( p.hinst );
     wc.lpszClassName = "Factory Craft";
     window.erstellen( WS_POPUPWINDOW, wc );
-    Monitor m = Framework::getMonitor( 2 );
+    Monitor m = Framework::getMonitor( 0 );
     window.setBounds( Punkt( m.x, m.y ), Punkt( m.breite, m.height ) );
     window.setAnzeigeModus( SW_SHOWNORMAL );
     window.setVSchließAktion( [&window]( void* p, void* f ) {

+ 1 - 0
FactoryCraft/NoBlock.cpp

@@ -23,6 +23,7 @@ NoBlockBlockType::NoBlockBlockType( int id )
 
 Block* NoBlockBlockType::createBlock( Framework::Vec3<int> position )
 {
+    assert( false );
     return 0;
 }
 

+ 1 - 1
FactoryCraft/PlayerKam.cpp

@@ -14,7 +14,7 @@ void PlayerKam::setDirection( Framework::Vec2<float> direction )
 {
     if( direction.getLengthSq() > 0 )
     {
-        float rotZ = lowPrecisionASin( abs( direction.x ) / Vec3< float >( direction.x, direction.y, 0.f ).getLength() );
+        float rotZ = std::atan2( direction.y, direction.x ) + (float)PI / 2;
         setRotation( { (float)PI / 2.f, getRotation().y, rotZ } );
     }
 }

+ 27 - 7
FactoryCraft/World.cpp

@@ -8,6 +8,8 @@
 #include "Constants.h"
 #include "Registries.h"
 #include "BasicBlocks.h"
+#include "Game.h"
+#include <AsynchronCall.h>
 
 using namespace Network;
 using namespace Framework;
@@ -33,11 +35,11 @@ World::~World()
     currentPlayer->release();
 }
 
-void World::update()
+void World::update( bool background )
 {
     NetworkReader* serverMessageReader = 0;
     unsigned char type = 0;
-    while( serverMessageReader = network->zFactoryClient()->getNextMessage() )
+    while( background ? serverMessageReader = network->zFactoryClient()->getNextBackgroundMessage() : serverMessageReader = network->zFactoryClient()->getNextForegroundMessage() )
     {
         serverMessageReader->lese( (char*)&type, 1 );
         if( type == 2 ) // WORLD UPDATE
@@ -52,15 +54,15 @@ void World::update()
         }
         if( type == 4 ) // POSITION UPDATE
         {
-            Vec3<float> pos, dir;
+            Vec3<float> pos;
+            Vec2<float> dir;
             serverMessageReader->lese( (char*)&pos.x, 4 );
             serverMessageReader->lese( (char*)&pos.y, 4 );
             serverMessageReader->lese( (char*)&pos.z, 4 );
-            std::cout << "position: " << pos.x << ", " << pos.y << ", " << pos.z << " face: ";
             kam->setPosition( pos + Vec3<float>( 0.f, 0.f, 2.f ) );
             serverMessageReader->lese( (char*)&dir.x, 4 );
             serverMessageReader->lese( (char*)&dir.y, 4 );
-            std::cout << pos.x << ", " << pos.y << "\n";
+            ((Game*)(Menu*)menuRegister->get( "game" ))->updatePositionAndFace( pos, dir );
             kam->setDirection( { dir.x, dir.y } );
         }
     }
@@ -68,6 +70,7 @@ void World::update()
 
 void World::setChunk( Chunk* chunk, int dimensionId )
 {
+    zScreenPtr->lock();
     Dimension* zDim = zDimension( dimensionId );
     if( !zDim )
     {
@@ -75,11 +78,26 @@ void World::setChunk( Chunk* chunk, int dimensionId )
         dimensions->add( zDim );
     }
     zDim->addChunk( chunk );
+    zScreenPtr->unlock();
     zDim->updateVisibility();
 }
 
 void World::thread()
 {
+    new AsynchronCall( [this]() {
+        while( true )
+        {
+            zScreenPtr->lock();
+            if( currentGame != this )
+            {
+                zScreenPtr->unlock();
+                return;
+            }
+            zScreenPtr->unlock();
+            update( 0 );
+            Sleep( 10 );
+        }
+    } );
     while( true )
     {
         zScreenPtr->lock();
@@ -88,8 +106,8 @@ void World::thread()
             zScreenPtr->unlock();
             return;
         }
-        update();
         zScreenPtr->unlock();
+        update( 1 );
         Sleep( 10 );
     }
 }
@@ -114,10 +132,12 @@ Dimension* World::zDimension( int id ) const
 
 void World::setVisibility( Framework::Model3D* zModel, bool visible )
 {
+    renderedWorld->lock();
     if( visible )
         renderedWorld->addZeichnung( dynamic_cast<Framework::Model3D*>(zModel->getThis()) );
     else
         renderedWorld->removeZeichnung( zModel );
+    renderedWorld->unlock();
 }
 
 Framework::Bildschirm3D* World::zScreen() const
@@ -127,5 +147,5 @@ Framework::Bildschirm3D* World::zScreen() const
 
 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 );
+    return Punkt( ((x < 0 ? x + 1 : x) / CHUNK_SIZE) * CHUNK_SIZE + (x < 0 ? -CHUNK_SIZE : CHUNK_SIZE) / 2, ((y < 0 ? y + 1 : y) / CHUNK_SIZE) * CHUNK_SIZE + (y < 0 ? -CHUNK_SIZE : CHUNK_SIZE) / 2 );
 }

+ 1 - 1
FactoryCraft/World.h

@@ -21,7 +21,7 @@ private:
 public:
     World( Framework::Bildschirm3D* zScreen );
     ~World();
-    void update();
+    void update( bool background );
     void setChunk( Chunk* chunk, int dimensionId );
     void thread() override;