Browse Source

fix some issues with the action target calculation

Kolja Strohm 3 years ago
parent
commit
81fe9f3f21

+ 4 - 2
FactoryCraft/BasicBlock.cpp

@@ -15,8 +15,10 @@ void BasicBlock::onPostTick()
 
 
 DirtBlockType::DirtBlockType()
-    : BlockType( ID, createBlock( { 0, 0, 0 }, 0 ) )
-{}
+    : BlockType( ID, 0 )
+{
+    defaultBlock = createBlockAt( { 0, 0, 0 }, 0, 0 );
+}
 
 void DirtBlockType::loadSuperBlock( Block* zBlock, Framework::StreamReader* zReader ) const
 {

+ 1 - 1
FactoryCraft/BlockType.h

@@ -15,9 +15,9 @@ class BlockType : public virtual Framework::ReferenceCounter
 {
 private:
     const int id;
-    Block* defaultBlock;
 
 protected:
+    Block* defaultBlock;
     BlockType( int id, Block* defaultBlock );
     virtual ~BlockType();
 

+ 29 - 69
FactoryCraft/Entity.cpp

@@ -119,32 +119,24 @@ void Entity::prepareTick( const Dimension* zDimension, Game* zGame )
     int py = (int)floor( headPosition.y );
     int pz = (int)floor( headPosition.z );
     faceDir.normalize();
-    bool needCollisionCheck = 1;
-    while( needCollisionCheck )
+    Direction dir = BOTTOM;
+    while( true )
     {
         if( getDefaultBlock( zGame->zBlockAt( Vec3<int>{ px, py, pz }, zDimension->getDimensionId() ) )->isInteractable() )
         {
             delete target;
-            target = new ActionTarget( { px, py, pz }, BOTTOM );
+            target = new ActionTarget( { px, py, pz }, dir );
             break;
         }
-        needCollisionCheck = 0;
-        // collision to neighbor of current block current block
+        // collision to neighbor of current block
         if( faceDir.x > 0 )
         {
             float xt = ((float)px + 1.f - headPosition.x) / faceDir.x;
             Vec3<float> tmp = headPosition + faceDir * xt;
             if( xt <= targetDistanceLimit && tmp.y >= (float)py && tmp.y < (float)py + 1.f && tmp.z >= (float)pz && tmp.z < (float)pz + 1.f )
             {
-                if( !getDefaultBlock( zGame->zBlockAt( Vec3<int>{ px + 1, py, pz }, zDimension->getDimensionId() ) )->isInteractable() )
-                {
-                    delete target;
-                    target = new ActionTarget( { px, py, pz }, WEST );
-                    break;
-                }
-                else
-                    px++;
-                needCollisionCheck = 1;
+                dir = WEST;
+                px++;
                 continue;
             }
         }
@@ -152,92 +144,60 @@ void Entity::prepareTick( const Dimension* zDimension, Game* zGame )
         {
             float xt = ((float)px - headPosition.x) / faceDir.x;
             Vec3<float> tmp = headPosition + faceDir * xt;
-            if( xt <= 1.f && tmp.y >= (float)py && tmp.y < (float)py + 1.f && tmp.z >= (float)pz && tmp.z < (float)pz + 1.f )
+            if( xt <= targetDistanceLimit && tmp.y >= (float)py && tmp.y < (float)py + 1.f && tmp.z >= (float)pz && tmp.z < (float)pz + 1.f )
             {
-                if( !getDefaultBlock( zGame->zBlockAt( Vec3<int>{ px - 1, py, pz }, zDimension->getDimensionId() ) )->isPassable() )
-                {
-                    delete target;
-                    target = new ActionTarget( { px, py, pz }, EAST );
-                    break;
-                }
-                else
-                    px--;
-                needCollisionCheck = 1;
+                dir = EAST;
+                px--;
                 continue;
             }
         }
-        if( speed.y > 0 )
+        if( faceDir.y > 0 )
         {
             float yt = ((float)py + 1.f - headPosition.y) / faceDir.y;
             Vec3<float> tmp = headPosition + faceDir * yt;
-            if( yt <= 1.f && tmp.x >= (float)px && tmp.x < (float)px + 1.f && tmp.z >= (float)pz && tmp.z < (float)pz + 1.f )
+            if( yt <= targetDistanceLimit && tmp.x >= (float)px && tmp.x < (float)px + 1.f && tmp.z >= (float)pz && tmp.z < (float)pz + 1.f )
             {
-                if( !getDefaultBlock( zGame->zBlockAt( Vec3<int>{ px, py + 1, pz }, zDimension->getDimensionId() ) )->isPassable() )
-                {
-                    delete target;
-                    target = new ActionTarget( { px, py, pz }, NORTH );
-                    break;
-                }
-                else
-                    py++;
-                needCollisionCheck = 1;
+                dir = NORTH;
+                py++;
                 continue;
             }
         }
-        if( speed.y < 0 )
+        if( faceDir.y < 0 )
         {
             float yt = ((float)py - headPosition.y) / faceDir.y;
             Vec3<float> tmp = headPosition + faceDir * yt;
-            if( yt <= 1.f && tmp.x >= (float)px && tmp.x < (float)px + 1.f && tmp.z >= (float)pz && tmp.z < (float)pz + 1.f )
+            if( yt <= targetDistanceLimit && tmp.x >= (float)px && tmp.x < (float)px + 1.f && tmp.z >= (float)pz && tmp.z < (float)pz + 1.f )
             {
-                if( !getDefaultBlock( zGame->zBlockAt( Vec3<int>{ px, py - 1, pz }, zDimension->getDimensionId() ) )->isPassable() )
-                {
-                    delete target;
-                    target = new ActionTarget( { px, py, pz }, SOUTH );
-                    break;
-                }
-                else
-                    py--;
-                needCollisionCheck = 1;
+                dir = SOUTH;
+                py--;
                 continue;
             }
         }
-        if( speed.z > 0 )
+        if( faceDir.z > 0 )
         {
             float zt = ((float)pz + 1.f - headPosition.z) / faceDir.z;
             Vec3<float> tmp = headPosition + faceDir * zt;
-            if( zt <= 1.f && tmp.x >= (float)px && tmp.x < (float)px + 1.f && tmp.y >= (float)py && tmp.y < (float)py + 1.f )
+            if( zt <= targetDistanceLimit && tmp.x >= (float)px && tmp.x < (float)px + 1.f && tmp.y >= (float)py && tmp.y < (float)py + 1.f )
             {
-                if( !getDefaultBlock( zGame->zBlockAt( Vec3<int>{ px, py, pz + 1 }, zDimension->getDimensionId() ) )->isPassable() )
-                {
-                    delete target;
-                    target = new ActionTarget( { px, py, pz }, BOTTOM );
-                    break;
-                }
-                else
-                    pz++;
-                needCollisionCheck = 1;
+                dir = BOTTOM;
+                pz++;
                 continue;
             }
         }
-        if( speed.z < 0 )
+        if( faceDir.z < 0 )
         {
             float zt = ((float)pz - headPosition.z) / faceDir.z;
             Vec3<float> tmp = headPosition + faceDir * zt;
-            if( zt <= 1.f && tmp.x >= (float)px && tmp.x < (float)px + 1.f && tmp.y >= (float)py && tmp.y < (float)py + 1 )
+            if( zt <= targetDistanceLimit && tmp.x >= (float)px && tmp.x < (float)px + 1.f && tmp.y >= (float)py && tmp.y < (float)py + 1 )
             {
-                if( !getDefaultBlock( zGame->zBlockAt( Vec3<int>{ px, py, pz - 1 }, zDimension->getDimensionId() ) )->isPassable() )
-                {
-                    delete target;
-                    target = new ActionTarget( { px, py, pz }, TOP );
-                    break;
-                }
-                else
-                    pz--;
-                needCollisionCheck = 1;
+                dir = TOP;
+                pz--;
                 continue;
             }
         }
+        delete target;
+        target = 0;
+        break;
     }
 }
 
@@ -257,7 +217,7 @@ void Entity::tick( const Dimension* zDimension, Game* zGame )
     while( needCollisionCheck )
     {
         needCollisionCheck = 0;
-        // collision to neighbor of current block current block
+        // collision to neighbor of current block
         if( speed.x > 0 )
         {
             float xt = ((float)px + 1.f - oldPos.x) / frameSpeed.x;

+ 1 - 1
FactoryCraft/Inventory.cpp

@@ -229,7 +229,7 @@ void Inventory::updateCache( ItemSlot* zSlot, int beforeKey )
 {
     if( !itemCache )
         return;
-    int key = zSlot->zStack()->zItem()->zItemType()->getId();
+    int key = zSlot->zStack() ? zSlot->zStack()->zItem()->zItemType()->getId() : -1;
     if( key == beforeKey )
         return;
     if( beforeKey >= 0 )

+ 1 - 0
FactoryCraft/Player.cpp

@@ -24,6 +24,7 @@ Player::Player( Framework::Vec3<float> location, int dimensionId, int entityId )
     keyState = 0;
     jumping = 0;
     faceOffset = { 0.f, 0.f, 1.5f };
+    targetDistanceLimit = 4;
 }
 
 void Player::useItemSlot( ItemSlot* zSlot, Game* zGame )