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

fix entiy add and remove updates were not send in the correct order

Kolja Strohm пре 9 месеци
родитељ
комит
aba30da978

+ 1 - 4
FactoryCraft/AddEntityUpdate.cpp

@@ -6,10 +6,7 @@
 #include "StaticRegistry.h"
 
 AddEntityUpdate::AddEntityUpdate(Entity* entity, int dimension)
-    : WorldUpdate(WorldUpdateTypeEnum::ADD_ENTITY,
-        dimension,
-        entity->getPosition(),
-        entity->getPosition()),
+    : WorldUpdate(WorldUpdateTypeEnum::ADD_ENTITY, dimension, entity->getLocation()),
       entity(entity)
 {}
 

+ 6 - 3
FactoryCraft/Entity.cpp

@@ -229,9 +229,12 @@ Entity::Entity(
 
 void Entity::onDeath()
 {
-    removed = 1;
-    Game::INSTANCE->requestWorldUpdate(
-        new EntityRemovedUpdate(id, dimensionId, location));
+    if (!removed)
+    {
+        Game::INSTANCE->requestWorldUpdate(
+            new EntityRemovedUpdate(id, dimensionId, location));
+        removed = 1;
+    }
 }
 
 bool Entity::useItem(int typeId, ItemStack* zStack, bool left)

+ 3 - 2
FactoryCraft/EntityRemovedUpdate.cpp

@@ -2,8 +2,9 @@
 #include "Dimension.h"
 
 
-EntityRemovedUpdate::EntityRemovedUpdate(int entityId, int dimensionId, Framework::Vec3<int> pos)
-    : WorldUpdate(WorldUpdateTypeEnum::REMOVE_ENTITY, dimensionId, pos, pos),
+EntityRemovedUpdate::EntityRemovedUpdate(
+    int entityId, int dimensionId, Framework::Vec3<float> pos)
+    : WorldUpdate(WorldUpdateTypeEnum::REMOVE_ENTITY, dimensionId, pos),
 	entityId(entityId)
 {}
 

+ 1 - 1
FactoryCraft/EntityRemovedUpdate.h

@@ -12,7 +12,7 @@ protected:
 
 public:
     EntityRemovedUpdate(
-        int entityId, int dimensionId, Framework::Vec3<int> pos);
+        int entityId, int dimensionId, Framework::Vec3<float> pos);
     ~EntityRemovedUpdate();
 
     void onUpdate(Dimension* zDimension) override;

+ 1 - 23
FactoryCraft/Game.cpp

@@ -128,14 +128,7 @@ void GameClient::sendWorldUpdate(WorldUpdate* update)
         if (dist < viewDistance * CHUNK_SIZE)
         {
             other.lock();
-            int index = 0;
-            for (auto update2 : updateQueue)
-            {
-                int dist2 = update2->distanceTo(pos.x, pos.y);
-                if (dist2 > dist) break;
-                index++;
-            }
-            updateQueue.add(update, index);
+            updateQueue.add(update);
             other.unlock();
             updateSync.notify();
             add = 1;
@@ -684,21 +677,6 @@ void Game::sendMessage(NetworkMessage* response, Entity* zTargetPlayer)
 bool Game::requestWorldUpdate(WorldUpdate* update)
 {
     cs.lock();
-    for (WorldUpdate* u : *updates)
-    {
-        if (u->getMaxAffectedPoint().x >= update->getMinAffectedPoint().x
-            && u->getMinAffectedPoint().x <= update->getMaxAffectedPoint().x
-            && u->getMaxAffectedPoint().y >= update->getMinAffectedPoint().y
-            && u->getMinAffectedPoint().y <= update->getMaxAffectedPoint().y
-            && u->getMaxAffectedPoint().z >= update->getMinAffectedPoint().z
-            && u->getMinAffectedPoint().z <= update->getMaxAffectedPoint().z
-            && u->getType() == update->getType())
-        {
-            cs.unlock();
-            update->release();
-            return 0;
-        }
-    }
     updates->add(update);
     cs.unlock();
     return 1;

+ 5 - 23
FactoryCraft/WorldUpdate.cpp

@@ -1,14 +1,10 @@
 #include "WorldUpdate.h"
 
-WorldUpdate::WorldUpdate(int type,
-    int dimensionId,
-    Framework::Vec3<int> minAffected,
-    Framework::Vec3<int> maxAffected)
+WorldUpdate::WorldUpdate(int type, int dimensionId, Framework::Vec3<float> pos)
     : ReferenceCounter(),
       affectedDimensionId(dimensionId),
-      minAffected(minAffected),
-      maxAffected(maxAffected),
-      type(type)
+      type(type),
+      pos(pos)
 {}
 
 void WorldUpdate::writeAndCheck(Framework::StreamWriter* zWriter)
@@ -23,16 +19,6 @@ int WorldUpdate::getAffectedDimension() const
     return affectedDimensionId;
 }
 
-const Framework::Vec3<int>& WorldUpdate::getMinAffectedPoint() const
-{
-    return minAffected;
-}
-
-const Framework::Vec3<int>& WorldUpdate::getMaxAffectedPoint() const
-{
-    return maxAffected;
-}
-
 int WorldUpdate::getType() const
 {
     return type;
@@ -40,9 +26,5 @@ int WorldUpdate::getType() const
 
 int WorldUpdate::distanceTo(int x, int y) const
 {
-    int xDist = MIN(abs(x - minAffected.x), abs(x - maxAffected.x));
-    int yDist = MIN(abs(y - minAffected.y), abs(y - maxAffected.y));
-    if (x >= minAffected.x && x <= maxAffected.x) xDist = 0;
-    if (y >= minAffected.y && y <= maxAffected.y) yDist = 0;
-    return MIN(xDist, yDist);
-}
+    return pos.abstand(Framework::Vec3<float>((float)x, (float)y, pos.z));
+}

+ 2 - 8
FactoryCraft/WorldUpdate.h

@@ -19,25 +19,19 @@ class WorldUpdate : public Framework::ReferenceCounter
 {
 private:
     int affectedDimensionId;
-    Framework::Vec3<int> minAffected;
-    Framework::Vec3<int> maxAffected;
     int type;
+    Framework::Vec3<float> pos;
 
 protected:
     virtual void write(Framework::StreamWriter* zWriter) = 0;
 
 public:
-    WorldUpdate(int type,
-        int dimensionId,
-        Framework::Vec3<int> minAffected,
-        Framework::Vec3<int> maxAffected);
+    WorldUpdate(int type, int dimensionId, Framework::Vec3<float> pos);
 
     virtual void onUpdate(Dimension* zDimension) = 0;
     void writeAndCheck(Framework::StreamWriter* zWriter);
 
     int getAffectedDimension() const;
-    const Framework::Vec3<int>& getMinAffectedPoint() const;
-    const Framework::Vec3<int>& getMaxAffectedPoint() const;
     int getType() const;
     int distanceTo(int x, int y) const;
 };