Parcourir la source

block updates will now be processed

Kolja Strohm il y a 3 ans
Parent
commit
e2844e8ab5

+ 1 - 1
FactoryCraft/BasicBlocks.cpp

@@ -182,7 +182,7 @@ BasicBlock::~BasicBlock()
 
 bool BasicBlock::needRenderPolygon( int index )
 {
-    return 1;
+    return sideVisible[ index ];
 }
 
 Textur* BasicBlock::zEffectTextur()

+ 11 - 0
FactoryCraft/Block.cpp

@@ -15,6 +15,7 @@ Block::Block( const BlockType* zType, ItemType* zTool, Framework::Vec3<int> pos,
     hardness = 1;
     this->zTool = zTool;
     speedModifier = 1;
+    memset( sideVisible, 1, 6 );
     Model3D::setPosition( (Framework::Vec3<float>)pos + Framework::Vec3<float>{0.5f, 0.5f, 0.5f} );
 }
 
@@ -26,6 +27,16 @@ bool Block::isTransparent() const
     return transparent;
 }
 
+bool Block::isPassable() const
+{
+    return passable;
+}
+
+void Block::setSideVisible( Direction dir, bool visible )
+{
+    sideVisible[ getDirectionIndex( dir ) ] = visible;
+}
+
 const BlockType* Block::zBlockType() const
 {
     return zType;

+ 3 - 0
FactoryCraft/Block.h

@@ -25,12 +25,15 @@ protected:
     float speedModifier;
     const BlockType* zType;
     ItemType* zTool;
+    bool sideVisible[ 6 ];
 
 public:
     Block( const BlockType* zType, ItemType* zTool, Vec3<int> position, bool hasInventory );
     virtual ~Block();
 
     bool isTransparent() const;
+    bool isPassable() const;
+    void setSideVisible( Direction dir, bool visible );
 
     const BlockType* zBlockType() const;
     friend Chunk;

+ 47 - 0
FactoryCraft/BlockChangedUpdate.cpp

@@ -0,0 +1,47 @@
+#include "BlockChangedUpdate.h"
+#include <Vec3.h>
+#include "Globals.h"
+#include "Registries.h"
+
+BlockChangedUpdateType::BlockChangedUpdateType()
+    : WorldUpdateType( ID )
+{}
+
+void BlockChangedUpdateType::applyUpdate( Framework::StreamReader* zReader )
+{
+    int dimension = 0;
+    zReader->lese( (char*)&dimension, 4 );
+    Framework::Vec3<int> pos;
+    zReader->lese( (char*)&pos.x, 4 );
+    zReader->lese( (char*)&pos.y, 4 );
+    zReader->lese( (char*)&pos.z, 4 );
+    unsigned short id;
+    zReader->lese( (char*)&id, 2 );
+    bool d = 1;
+    zReader->lese( (char*)&d, 1 );
+    Block* b = currentGame->zBlockAt( pos, dimension );
+    if( !d )
+    {
+        if( !b || b->zBlockType()->getId() != id )
+        {
+            if( STATIC_REGISTRY( BlockType ).zElement( id )->needsInstance() )
+            {
+                b = STATIC_REGISTRY( BlockType ).zElement( id )->createBlock( pos );
+                currentGame->zDimension( dimension )->setBlock( b );
+            }
+            else if( b )
+                currentGame->zDimension( dimension )->removeBlock( b );
+        }
+    }
+    else
+    {
+        if( b )
+            STATIC_REGISTRY( BlockType ).zElement( id )->updateBlock( b, zReader );
+        else
+        {
+            b = STATIC_REGISTRY( BlockType ).zElement( id )->createBlock( pos );
+            STATIC_REGISTRY( BlockType ).zElement( id )->updateBlock( b, zReader );
+            currentGame->zDimension( dimension )->setBlock( b );
+        }
+    }
+}

+ 16 - 0
FactoryCraft/BlockChangedUpdate.h

@@ -0,0 +1,16 @@
+#pragma once
+
+#include "WorldUpdate.h"
+
+class BlockChangedUpdateType : WorldUpdateType
+{
+    REGISTRABLE( BlockChangedUpdateType )
+
+protected:
+    BlockChangedUpdateType();
+
+public:
+    void applyUpdate( Framework::StreamReader* zReader ) override;
+};
+
+REGISTER( BlockChangedUpdateType, WorldUpdateType )

+ 5 - 0
FactoryCraft/BlockType.cpp

@@ -43,6 +43,11 @@ Block* BlockType::loadBlock( Framework::Vec3<int> position, Framework::StreamRea
     return result;
 }
 
+void BlockType::updateBlock( Block* zBlock, Framework::StreamReader* zReader )
+{
+    loadSuperBlock( zBlock, zReader );
+}
+
 int BlockType::getId() const
 {
     return id;

+ 1 - 0
FactoryCraft/BlockType.h

@@ -24,6 +24,7 @@ protected:
 public:
     virtual Block* createBlock( Framework::Vec3<int> position ) = 0;
     virtual Block* loadBlock( Framework::Vec3<int> position, Framework::StreamReader* zReader );
+    virtual void updateBlock( Block* zBlock, Framework::StreamReader* zReader );
     virtual const Block* zDefault();
     virtual bool needsInstance() const = 0;
 

+ 51 - 2
FactoryCraft/Chunk.cpp

@@ -8,7 +8,8 @@
 Chunk::Chunk( Framework::Punkt location, int dimensionId )
     : ReferenceCounter(),
     dimensionId( dimensionId ),
-    location( location )
+    location( location ),
+    isLoading( 0 )
 {}
 
 Chunk::Chunk( Framework::Punkt location, int dimensionId, Framework::StreamReader* zReader )
@@ -20,18 +21,24 @@ Chunk::Chunk( Framework::Punkt location, int dimensionId, Framework::StreamReade
 Chunk::~Chunk()
 {}
 
-Block* Chunk::zBlockAt( Framework::Vec3<int> location ) const
+Block* Chunk::zBlockAt( Framework::Vec3<int> location )
 {
+    cs.lock();
     for( Block* b : blocks )
     {
         if( (Framework::Vec3<int>)b->getPos() == location )
+        {
+            cs.unlock();
             return b;
+        }
     }
+    cs.unlock();
     return 0;
 }
 
 void Chunk::setBlock( Block* block )
 {
+    cs.lock();
     Framework::Vec3<int> pos = (Framework::Vec3<int>)block->getPos();
     for( Framework::Iterator<Block*> iterator = blocks.begin(); iterator; iterator++ )
     {
@@ -39,14 +46,37 @@ void Chunk::setBlock( Block* block )
         {
             iterator->release();
             iterator.set( block );
+            cs.unlock();
             return;
         }
     }
     blocks.add( block );
+    cs.unlock();
+    if( !isLoading )
+        updateVisibility();
+}
+
+void Chunk::removeBlock( Block* zBlock )
+{
+    cs.lock();
+    int index = 0;
+    for( Framework::Iterator<Block*> iterator = blocks.begin(); iterator; iterator++, index++ )
+    {
+        if( zBlock == (Block*)iterator )
+        {
+            blocks.remove( index );
+            cs.unlock();
+            if( !isLoading )
+                updateVisibility();
+            return;
+        }
+    }
+    cs.unlock();
 }
 
 void Chunk::load( Framework::StreamReader* zReader )
 {
+    isLoading = 1;
     Framework::Vec3<int> pos = { 0, 0, 0 };
     unsigned short id;
     zReader->lese( (char*)&id, 2 );
@@ -67,6 +97,8 @@ void Chunk::load( Framework::StreamReader* zReader )
             setBlock( STATIC_REGISTRY( BlockType ).zElement( id )->createBlock( { pos.x + location.x - CHUNK_SIZE / 2, pos.y + location.y - CHUNK_SIZE / 2, pos.z } ) );
         zReader->lese( (char*)&id, 2 );
     }
+    isLoading = 0;
+    updateVisibility();
 }
 
 int Chunk::getDimensionId() const
@@ -91,6 +123,23 @@ Framework::Vec3<int> Chunk::getMax() const
 
 void Chunk::forAll( std::function<void( Model3D* )> f )
 {
+    cs.lock();
     for( Block* b : blocks )
         f( b );
+    cs.unlock();
+}
+
+void Chunk::updateVisibility()
+{
+    cs.lock();
+    for( Block* b : blocks )
+    {
+        Framework::Vec3<int> pos = (Framework::Vec3<int>)b->getPos();
+        for( int i = 0; i < 6; i++ )
+        {
+            Block* c = zBlockAt( pos + getDirection( getDirectionFromIndex( i ) ) );
+            b->setSideVisible( getDirectionFromIndex( i ), !c || c->isTransparent() || c->isPassable() );
+        }
+    }
+    cs.unlock();
 }

+ 6 - 1
FactoryCraft/Chunk.h

@@ -6,6 +6,7 @@
 #include <Either.h>
 #include <Model3D.h>
 #include <Array.h>
+#include <Critical.h>
 
 #include "Block.h"
 #include "Area.h"
@@ -16,17 +17,21 @@ private:
     int dimensionId;
     Framework::Punkt location;
     Framework::RCArray<Block> blocks;
+    bool isLoading;
+    Framework::Critical cs;
 
 public:
     Chunk( Framework::Punkt location, int dimensionId );
     Chunk( Framework::Punkt location, int dimensionId, Framework::StreamReader* zReader );
     ~Chunk();
-    Block* zBlockAt( Framework::Vec3<int> cLocation ) const;
+    Block* zBlockAt( Framework::Vec3<int> cLocation );
     void setBlock( Block* block );
+    void removeBlock( Block* zBlock );
     void load( Framework::StreamReader* zReader );
     int getDimensionId() const;
     Framework::Punkt getCenter() const;
     Framework::Vec3<int> getMin() const;
     Framework::Vec3<int> getMax() const;
     void forAll( std::function<void( Model3D* )> f ) override;
+    void updateVisibility();
 };

+ 21 - 9
FactoryCraft/Dimension.cpp

@@ -48,15 +48,7 @@ Framework::Either<Block*, int> Dimension::zBlock( Vec3<int> location )
 {
     Chunk* c = zChunk( currentGame->getChunkCenter( location.x, location.y ) );
     if( c )
-    {
-        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 c->zBlockAt( location );
     return 0;
 }
 
@@ -122,4 +114,24 @@ void Dimension::removeDistantChunks( Punkt wPos, World* zWorld )
         zWorld->setVisibility( chunk, 0 );
         setChunk( 0, chunk->getCenter(), zWorld );
     }
+}
+
+void Dimension::setBlock( Block* block )
+{
+    cs.lock();
+    Chunk* c = zChunk( currentGame->getChunkCenter( (int)floor( block->getPos().x ), (int)floor( block->getPos().y ) ) );
+    if( c )
+        c->setBlock( block );
+    else
+        block->release();
+    cs.unlock();
+}
+
+void Dimension::removeBlock( Block* zBlock )
+{
+    cs.lock();
+    Chunk* c = zChunk( currentGame->getChunkCenter( (int)floor( zBlock->getPos().x ), (int)floor( zBlock->getPos().y ) ) );
+    if( c )
+        c->removeBlock( zBlock );
+    cs.unlock();
 }

+ 2 - 0
FactoryCraft/Dimension.h

@@ -31,4 +31,6 @@ public:
     bool hasChunck( int x, int y ) const;
     Chunk* zChunk( Framework::Punkt wPos ) const;
     void removeDistantChunks( Framework::Punkt wPos, World* zWorld );
+    void setBlock( Block* block );
+    void removeBlock( Block* zBlock );
 };

+ 4 - 0
FactoryCraft/FactoryCraft.vcxproj

@@ -174,6 +174,7 @@ copy "..\..\..\..\..\Allgemein\Network\x64\Release\Network.dll" "network.dll"</C
     <ClCompile Include="Area.cpp" />
     <ClCompile Include="BasicBlocks.cpp" />
     <ClCompile Include="Block.cpp" />
+    <ClCompile Include="BlockChangedUpdate.cpp" />
     <ClCompile Include="BlockType.cpp" />
     <ClCompile Include="Chunk.cpp" />
     <ClCompile Include="Dimension.cpp" />
@@ -193,6 +194,7 @@ copy "..\..\..\..\..\Allgemein\Network\x64\Release\Network.dll" "network.dll"</C
     <ClCompile Include="Menu.cpp" />
     <ClCompile Include="NetworkHandler.cpp" />
     <ClCompile Include="NoBlock.cpp" />
+    <ClCompile Include="PlaceBlockUpdate.cpp" />
     <ClCompile Include="PlayerKam.cpp" />
     <ClCompile Include="StaticInitializerOrder.cpp" />
     <ClCompile Include="World.cpp" />
@@ -202,6 +204,7 @@ copy "..\..\..\..\..\Allgemein\Network\x64\Release\Network.dll" "network.dll"</C
     <ClInclude Include="Area.h" />
     <ClInclude Include="BasicBlocks.h" />
     <ClInclude Include="Block.h" />
+    <ClInclude Include="BlockChangedUpdate.h" />
     <ClInclude Include="BlockType.h" />
     <ClInclude Include="Chunk.h" />
     <ClInclude Include="Constants.h" />
@@ -224,6 +227,7 @@ copy "..\..\..\..\..\Allgemein\Network\x64\Release\Network.dll" "network.dll"</C
     <ClInclude Include="Menu.h" />
     <ClInclude Include="NetworkHandler.h" />
     <ClInclude Include="NoBlock.h" />
+    <ClInclude Include="PlaceBlockUpdate.h" />
     <ClInclude Include="PlayerKam.h" />
     <ClInclude Include="Registries.h" />
     <ClInclude Include="StaticRegistry.h" />

+ 12 - 0
FactoryCraft/FactoryCraft.vcxproj.filters

@@ -120,6 +120,12 @@
     <ClCompile Include="Load.cpp">
       <Filter>Menu</Filter>
     </ClCompile>
+    <ClCompile Include="BlockChangedUpdate.cpp">
+      <Filter>world\update</Filter>
+    </ClCompile>
+    <ClCompile Include="PlaceBlockUpdate.cpp">
+      <Filter>world\update</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="Entity.h">
@@ -212,5 +218,11 @@
     <ClInclude Include="FactoryClient.h">
       <Filter>Network</Filter>
     </ClInclude>
+    <ClInclude Include="BlockChangedUpdate.h">
+      <Filter>world\update</Filter>
+    </ClInclude>
+    <ClInclude Include="PlaceBlockUpdate.h">
+      <Filter>world\update</Filter>
+    </ClInclude>
   </ItemGroup>
 </Project>

+ 23 - 0
FactoryCraft/PlaceBlockUpdate.cpp

@@ -0,0 +1,23 @@
+#include "PlaceBlockUpdate.h"
+#include <Vec3.h>
+#include "BlockType.h"
+#include "StaticRegistry.h"
+#include "Registries.h"
+#include "Globals.h"
+
+PlaceBlockUpdateType::PlaceBlockUpdateType()
+    : WorldUpdateType( ID )
+{}
+
+void PlaceBlockUpdateType::applyUpdate( Framework::StreamReader* zReader )
+{
+    int dimension = 0;
+    zReader->lese( (char*)&dimension, 4 );
+    Framework::Vec3<int> pos;
+    zReader->lese( (char*)&pos.x, 4 );
+    zReader->lese( (char*)&pos.y, 4 );
+    zReader->lese( (char*)&pos.z, 4 );
+    unsigned short id;
+    zReader->lese( (char*)&id, 2 );
+    currentGame->zDimension( dimension )->setBlock( STATIC_REGISTRY( BlockType ).zElement( id )->loadBlock( pos, zReader ) );
+}

+ 15 - 0
FactoryCraft/PlaceBlockUpdate.h

@@ -0,0 +1,15 @@
+#pragma once
+#include "WorldUpdate.h"
+
+class PlaceBlockUpdateType : WorldUpdateType
+{
+    REGISTRABLE( PlaceBlockUpdateType )
+
+protected:
+    PlaceBlockUpdateType();
+
+public:
+    void applyUpdate( Framework::StreamReader* zReader ) override;
+};
+
+REGISTER( PlaceBlockUpdateType, WorldUpdateType )

+ 42 - 17
FactoryCraft/PlayerKam.cpp

@@ -137,25 +137,50 @@ void PlayerKam::doMausEreignis( Framework::MausEreignis& me )
         }
         else
         {
-            if( me.id == ME_Bewegung && kameraControll )
+            if( kameraControll )
             {
-                int yDir = lastMousePos.y - me.originalY;
-                int xDir = lastMousePos.x - me.originalX;
-                setRotation( { min( max( getRotation().x - yDir * 0.005f, 0.1f ), 2.5f ), getRotation().y, getRotation().z - xDir * 0.005f } );
-                if( getRotation().z > 2 * PI )
-                    setRotation( { getRotation().x, getRotation().y, getRotation().z - 2.f * (float)PI } );
-                if( getRotation().z < -2 * PI )
-                    setRotation( { getRotation().x, getRotation().y, getRotation().z + 2.f * (float)PI } );
-                Vec3<float> direction = getWorldDirection( getScreenPos() + getScreenSize() / 2 );
-                char action[ 13 ];
-                action[ 0 ] = 2;
-                *(float*)(action + 1) = direction.x;
-                *(float*)(action + 5) = direction.y;
-                *(float*)(action + 9) = direction.z;
-                network->zFactoryClient()->sendPlayerAction( action, 13 );
-                SetCursorPos( lastMousePos.x + window->getPosition().x, lastMousePos.y + window->getPosition().y );
-                ShowCursor( false );
+                if( me.id == ME_Bewegung )
+                {
+                    int yDir = lastMousePos.y - me.originalY;
+                    int xDir = lastMousePos.x - me.originalX;
+                    setRotation( { min( max( getRotation().x - yDir * 0.005f, 0.1f ), 2.5f ), getRotation().y, getRotation().z - xDir * 0.005f } );
+                    if( getRotation().z > 2 * PI )
+                        setRotation( { getRotation().x, getRotation().y, getRotation().z - 2.f * (float)PI } );
+                    if( getRotation().z < -2 * PI )
+                        setRotation( { getRotation().x, getRotation().y, getRotation().z + 2.f * (float)PI } );
+                    Vec3<float> direction = getWorldDirection( getScreenPos() + getScreenSize() / 2 );
+                    char action[ 13 ];
+                    action[ 0 ] = 2;
+                    *(float*)(action + 1) = direction.x;
+                    *(float*)(action + 5) = direction.y;
+                    *(float*)(action + 9) = direction.z;
+                    network->zFactoryClient()->sendPlayerAction( action, 13 );
+                    SetCursorPos( lastMousePos.x + window->getPosition().x, lastMousePos.y + window->getPosition().y );
+                    ShowCursor( false );
+                }
+                if( me.id == ME_PLinks )
+                {
+                    char action[ 2 ] = { 1, 8 };
+                    network->zFactoryClient()->sendPlayerAction( action, 2 );
+
+                }
+                if( me.id == ME_RLinks )
+                {
+                    char action[ 2 ] = { 0, 8 };
+                    network->zFactoryClient()->sendPlayerAction( action, 2 );
+                }
+                if( me.id == ME_PRechts )
+                {
+                    char action[ 2 ] = { 1, 9 };
+                    network->zFactoryClient()->sendPlayerAction( action, 2 );
+                }
+                if( me.id == ME_RRechts )
+                {
+                    char action[ 2 ] = { 0, 9 };
+                    network->zFactoryClient()->sendPlayerAction( action, 2 );
+                }
             }
+            
         }
         me.verarbeitet = 1;
     }

+ 5 - 1
FactoryCraft/StaticInitializerOrder.cpp

@@ -12,8 +12,12 @@ int count_EntityType = 0;
 const int c::ID = count_##typ++;       \
 const c *c::INSTANCE = new c(); 
 
+// order of includes determines the ids
+
 #include "NoBlock.h" // must be first
 #include "BasicBlocks.h"
 //#include "OverworldDimension.h"
 #include "AddChunkUpdate.h"
-//#include "Player.h"
+//#include "Player.h"
+#include "PlaceBlockUpdate.h"
+#include "BlockChangedUpdate.h"