Browse Source

add frontDirection to blocks

Kolja Strohm 3 weeks ago
parent
commit
a3829c854d

+ 115 - 1
FactoryCraft/Area.cpp

@@ -57,4 +57,118 @@ Directions getDirectionsFromVector(Framework::Vec3<float> dir)
     else if (dir.z > 0)
         dirs |= TOP;
     return dirs;
-}
+}
+
+Framework::Vec3<float> getRotation(Direction source, Direction target)
+{
+    switch (source)
+    {
+    case NORTH:
+        switch (target)
+        {
+        case EAST:
+            return Framework::Vec3<float>(0.f, 0.f, (float)PI / 2.f);
+        case SOUTH:
+            return Framework::Vec3<float>(0.f, 0.f, (float)PI);
+        case WEST:
+            return Framework::Vec3<float>(0.f, 0.f, -(float)PI / 2.f);
+        case TOP:
+            return Framework::Vec3<float>(-(float)PI / 2.f, 0.f, 0.f);
+        case BOTTOM:
+            return Framework::Vec3<float>((float)PI / 2.f, 0.f, 0.f);
+        default:
+            break;
+        }
+        break;
+    case EAST:
+        switch (target)
+        {
+        case SOUTH:
+            return Framework::Vec3<float>(0.f, 0.f, (float)PI / 2.f);
+        case WEST:
+            return Framework::Vec3<float>(0.f, 0.f, (float)PI);
+        case NORTH:
+            return Framework::Vec3<float>(0.f, 0.f, -(float)PI / 2.f);
+        case TOP:
+            return Framework::Vec3<float>(
+                -(float)PI / 2.f, 0.f, (float)PI / 2.f);
+        case BOTTOM:
+            return Framework::Vec3<float>(
+                (float)PI / 2.f, 0.f, (float)PI / 2.f);
+        default:
+            break;
+        }
+        break;
+    case SOUTH:
+        switch (target)
+        {
+        case WEST:
+            return Framework::Vec3<float>(0.f, 0.f, (float)PI / 2.f);
+        case NORTH:
+            return Framework::Vec3<float>(0.f, 0.f, (float)PI);
+        case EAST:
+            return Framework::Vec3<float>(0.f, 0.f, -(float)PI / 2.f);
+        case TOP:
+            return Framework::Vec3<float>(-(float)PI / 2.f, 0.f, (float)PI);
+        case BOTTOM:
+            return Framework::Vec3<float>((float)PI / 2.f, 0.f, (float)PI);
+        default:
+            break;
+        }
+        break;
+    case WEST:
+        switch (target)
+        {
+        case NORTH:
+            return Framework::Vec3<float>(0.f, 0.f, (float)PI / 2.f);
+        case EAST:
+            return Framework::Vec3<float>(0.f, 0.f, (float)PI);
+        case SOUTH:
+            return Framework::Vec3<float>(0.f, 0.f, -(float)PI / 2.f);
+        case TOP:
+            return Framework::Vec3<float>(
+                -(float)PI / 2.f, 0.f, -(float)PI / 2.f);
+        case BOTTOM:
+            return Framework::Vec3<float>(
+                (float)PI / 2.f, 0.f, -(float)PI / 2.f);
+        default:
+            break;
+        }
+        break;
+    case TOP:
+        switch (target)
+        {
+        case NORTH:
+            return Framework::Vec3<float>(-(float)PI / 2.f, 0.f, 0.f);
+        case EAST:
+            return Framework::Vec3<float>(
+                -(float)PI / 2.f, 0.f, (float)PI / 2.f);
+        case SOUTH:
+            return Framework::Vec3<float>(-(float)PI / 2.f, 0.f, (float)PI);
+        case WEST:
+            return Framework::Vec3<float>(
+                -(float)PI / 2.f, 0.f, -(float)PI / 2.f);
+        default:
+            break;
+        }
+        break;
+    case BOTTOM:
+        switch (target)
+        {
+        case NORTH:
+            return Framework::Vec3<float>((float)PI / 2.f, 0.f, 0.f);
+        case EAST:
+            return Framework::Vec3<float>(
+                (float)PI / 2.f, 0.f, (float)PI / 2.f);
+        case SOUTH:
+            return Framework::Vec3<float>((float)PI / 2.f, 0.f, (float)PI);
+        case WEST:
+            return Framework::Vec3<float>(
+                (float)PI / 2.f, 0.f, -(float)PI / 2.f);
+        default:
+            break;
+        }
+        break;
+    }
+    return Framework::Vec3<float>(0.f, 0.f, 0.f);
+}

+ 4 - 3
FactoryCraft/Area.h

@@ -11,7 +11,7 @@ struct Area
     int dimensionId;
 };
 
-enum Direction
+enum Direction : char
 {
     NO_DIRECTION = 0,
     NORTH = 1,
@@ -23,7 +23,7 @@ enum Direction
     INSIDE = 64
 };
 
-typedef int Directions;
+typedef char Directions;
 
 #define ANY_DIRECTION NORTH | EAST | SOUTH | WEST | TOP | BOTTOM
 
@@ -31,4 +31,5 @@ Framework::Vec3<int> getDirection(Directions dir);
 int getDirectionIndex(Direction dir);
 Direction getOppositeDirection(Direction dir);
 Direction getDirectionFromIndex(int index);
-Directions getDirectionsFromVector(Framework::Vec3<float> dir);
+Directions getDirectionsFromVector(Framework::Vec3<float> dir);
+Framework::Vec3<float> getRotation(Direction source, Direction target);

+ 54 - 79
FactoryCraft/Block.cpp

@@ -15,7 +15,8 @@ Block::Block(const BlockType* zType,
     bool needRequestModelInfo,
     float size,
     bool passable,
-    float speedModifier)
+    float speedModifier,
+    Direction frontDirection)
     : FactoryCraftModel(),
       zType(zType),
       location(pos),
@@ -26,7 +27,8 @@ Block::Block(const BlockType* zType,
       distanceToSource(0),
       passable(passable),
       speedModifier(speedModifier),
-      currentModelInfo(0)
+      currentModelInfo(0),
+      frontDirection(frontDirection)
 {
     hp = (float)maxHP;
     memset(sideVisible, 0, 6);
@@ -37,6 +39,7 @@ Block::Block(const BlockType* zType,
     setModelTextur(texture);
     memset(lightData, 0, 6 * 6);
     setSize(size);
+    Model3D::setRotation(::getRotation(NORTH, frontDirection));
 }
 
 Block::~Block()
@@ -191,6 +194,11 @@ bool Block::isVisible() const
     //     || sideVisible[4] || sideVisible[5];
 }
 
+Direction Block::getFrontDirection() const
+{
+    return frontDirection;
+}
+
 Vec3<int> Block::getLocation() const
 {
     return location;
@@ -209,83 +217,50 @@ Skeleton* Block::zSkeleton() const
 Text Block::printLightInfo()
 {
     Text result = "NORTH[0;-1;0](";
-    result += (int)lightData[0];
-    result += ",";
-    result += (int)lightData[1];
-    result += ",";
-    result += (int)lightData[2];
-    result += ";";
-    result += (int)lightData[3];
-    result += ",";
-    result += (int)lightData[4];
-    result += ",";
-    result += (int)lightData[5];
-    result += ")\n";
-    result += "EAST[1;0;0](";
-    result += (int)lightData[6];
-    result += ",";
-    result += (int)lightData[7];
-    result += ",";
-    result += (int)lightData[8];
-    result += ";";
-    result += (int)lightData[9];
-    result += ",";
-    result += (int)lightData[10];
-    result += ",";
-    result += (int)lightData[11];
-    result += ")\n";
-    result += "SOUTH[0;1;0](";
-    result += (int)lightData[12];
-    result += ",";
-    result += (int)lightData[13];
-    result += ",";
-    result += (int)lightData[14];
-    result += ";";
-    result += (int)lightData[15];
-    result += ",";
-    result += (int)lightData[16];
-    result += ",";
-    result += (int)lightData[17];
-    result += ")\n";
-    result += "WEST[-1;0;0](";
-    result += (int)lightData[18];
-    result += ",";
-    result += (int)lightData[19];
-    result += ",";
-    result += (int)lightData[20];
-    result += ";";
-    result += (int)lightData[21];
-    result += ",";
-    result += (int)lightData[22];
-    result += ",";
-    result += (int)lightData[23];
-    result += ")\n";
-    result += "TOP[0;0;1](";
-    result += (int)lightData[24];
-    result += ",";
-    result += (int)lightData[25];
-    result += ",";
-    result += (int)lightData[26];
-    result += ";";
-    result += (int)lightData[27];
-    result += ",";
-    result += (int)lightData[28];
-    result += ",";
-    result += (int)lightData[29];
-    result += ")\n";
-    result += "BOTTOM[0;0;-1](";
-    result += (int)lightData[30];
-    result += ",";
-    result += (int)lightData[31];
-    result += ",";
-    result += (int)lightData[32];
-    result += ";";
-    result += (int)lightData[33];
-    result += ",";
-    result += (int)lightData[34];
-    result += ",";
-    result += (int)lightData[35];
-    result += ")\n";
+    result.append() << (int)lightData[0] << "," << (int)lightData[1] << ","
+                    << (int)lightData[2] << ";" << (int)lightData[3] << ","
+                    << (int)lightData[4] << "," << (int)lightData[5] << ")\n"
+                    << "EAST[1;0;0](" << (int)lightData[6] << ","
+                    << (int)lightData[7] << "," << (int)lightData[8] << ";"
+                    << (int)lightData[9] << "," << (int)lightData[10] << ","
+                    << (int)lightData[11] << ")\n"
+                    << "SOUTH[0;1;0](" << (int)lightData[12] << ","
+                    << (int)lightData[13] << "," << (int)lightData[14] << ";"
+                    << (int)lightData[15] << "," << (int)lightData[16] << ","
+                    << (int)lightData[17] << ")\n"
+                    << "WEST[-1;0;0](" << (int)lightData[18] << ","
+                    << (int)lightData[19] << "," << (int)lightData[20] << ";"
+                    << (int)lightData[21] << "," << (int)lightData[22] << ","
+                    << (int)lightData[23] << ")\n"
+                    << "TOP[0;0;1](" << (int)lightData[24] << ","
+                    << (int)lightData[25] << "," << (int)lightData[26] << ";"
+                    << (int)lightData[27] << "," << (int)lightData[28] << ","
+                    << (int)lightData[29] << ")\n"
+                    << "BOTTOM[0;0;-1](" << (int)lightData[30] << ","
+                    << (int)lightData[31] << "," << (int)lightData[32] << ";"
+                    << (int)lightData[33] << "," << (int)lightData[34] << ","
+                    << (int)lightData[35] << ")\n";
+    switch (frontDirection)
+    {
+    case NORTH:
+        result += "Front: NORTH\n";
+        break;
+    case EAST:
+        result += "Front: EAST\n";
+        break;
+    case SOUTH:
+        result += "Front: SOUTH\n";
+        break;
+    case WEST:
+        result += "Front: WEST\n";
+        break;
+    case TOP:
+        result += "Front: TOP\n";
+        break;
+    case BOTTOM:
+        result += "Front: BOTTOM\n";
+        break;
+    }
     return result;
 }
 

+ 4 - 1
FactoryCraft/Block.h

@@ -27,6 +27,7 @@ protected:
     char distanceToSource;
     bool passable;
     float speedModifier;
+    Direction frontDirection;
     ModelInfo* currentModelInfo;
 
     void beforeRender(
@@ -42,7 +43,8 @@ public:
         bool needRequestModelInfo,
         float size,
         bool passable,
-        float speedModifier);
+        float speedModifier,
+        Direction frontDirection);
     virtual ~Block();
 
     void setFlow(char flowOptions, char distanceToSource);
@@ -53,6 +55,7 @@ public:
     __int64 getMaxLight() const;
     const unsigned char* getLightData(Direction dir) const;
     bool isVisible() const;
+    Direction getFrontDirection() const;
 
     Vec3<int> getLocation() const;
     const BlockType* zBlockType() const;

+ 15 - 5
FactoryCraft/BlockType.cpp

@@ -10,7 +10,8 @@ BlockType::BlockType(int id,
     int initialMaxHP,
     bool needModelSubscription,
     bool fluid,
-    char maxFlowDistance)
+    char maxFlowDistance,
+    Direction defaultFrontDirection)
     : ReferenceCounter(),
       id(id),
       needsInstance(needsInstance),
@@ -18,13 +19,16 @@ BlockType::BlockType(int id,
       initialMaxHP(initialMaxHP),
       needModelSubscription(needModelSubscription),
       fluid(fluid),
-      maxFlowDistance(maxFlowDistance)
+      maxFlowDistance(maxFlowDistance),
+      defaultFrontDirection(defaultFrontDirection)
 {}
 
 BlockType::~BlockType() {}
 
-Block* BlockType::createBlock(
-    Framework::Vec3<int> position, bool passable, float speedModifier)
+Block* BlockType::createBlock(Framework::Vec3<int> position,
+    bool passable,
+    float speedModifier,
+    Direction dir)
 {
     return new Block(this,
         position,
@@ -35,7 +39,8 @@ Block* BlockType::createBlock(
         needModelSubscription,
         model.getSize(),
         passable,
-        speedModifier);
+        speedModifier,
+        dir);
 }
 
 int BlockType::getId() const
@@ -63,6 +68,11 @@ char BlockType::getMaxFlowDistance() const
     return maxFlowDistance;
 }
 
+Direction BlockType::getDefaultFrontDirection() const
+{
+    return defaultFrontDirection;
+}
+
 const ModelInfo& BlockType::getModelInfo() const
 {
     return model;

+ 9 - 3
FactoryCraft/BlockType.h

@@ -4,6 +4,7 @@
 #include <ReferenceCounter.h>
 #include <Vec3.h>
 
+#include "Area.h"
 #include "ModelInfo.h"
 
 class Block;
@@ -18,6 +19,7 @@ private:
     bool fluid;
     char maxFlowDistance;
     ModelInfo model;
+    Direction defaultFrontDirection;
 
 public:
     BlockType(int id,
@@ -26,14 +28,18 @@ public:
         int initialMaxHP,
         bool needModelSubscription,
         bool fluid,
-        char maxFlowDistance);
+        char maxFlowDistance,
+        Direction defaultFrontDirection);
     ~BlockType();
-    Block* createBlock(
-        Framework::Vec3<int> position, bool passable, float speedModifier);
+    Block* createBlock(Framework::Vec3<int> position,
+        bool passable,
+        float speedModifier,
+        Direction frontDirection);
     bool doesNeedInstance() const;
     bool doesNeedModelSubscription() const;
     bool isFluid() const;
     char getMaxFlowDistance() const;
+    Direction getDefaultFrontDirection() const;
 
     const ModelInfo& getModelInfo() const;
 

+ 27 - 14
FactoryCraft/Chunk.cpp

@@ -155,14 +155,14 @@ void Chunk::load(Framework::StreamReader* zReader)
         zReader->read(&state, 1);
         char flowOptions = 0;
         char distanceToSource = 0;
-        if ((state | 1) == state)
+        if (state & 1)
         {
             zReader->read(&flowOptions, 1);
             zReader->read(&distanceToSource, 1);
         }
         bool passable = 0;
         float speedModifier = 1.f;
-        if ((state | 2) == state)
+        if (state & 2)
         {
             passable = 1;
             zReader->read((char*)&speedModifier, 4);
@@ -170,6 +170,11 @@ void Chunk::load(Framework::StreamReader* zReader)
         pos = Vec3<int>((index / WORLD_HEIGHT) / CHUNK_SIZE,
             (index / WORLD_HEIGHT) % CHUNK_SIZE,
             index % WORLD_HEIGHT);
+        Direction dir = blockTypes[id]->getDefaultFrontDirection();
+        if (state & 4)
+        {
+            zReader->read((char*)&dir, 1);
+        }
         if (blockTypes[id]->doesNeedInstance())
         {
             cs.lock();
@@ -178,7 +183,8 @@ void Chunk::load(Framework::StreamReader* zReader)
                     pos.y + location.y - CHUNK_SIZE / 2,
                     pos.z},
                 passable,
-                speedModifier);
+                speedModifier,
+                dir);
             b->setFlow(flowOptions, distanceToSource);
             blocks[index] = b;
             cs.unlock();
@@ -442,17 +448,24 @@ void Chunk::api(char* message)
             char state = message[7];
             char flowOptions = 0;
             char distanceToSource = 0;
-            if ((state | 1) == state)
+            int i = 8;
+            if (state & 1)
             {
-                flowOptions = message[8];
-                distanceToSource = message[9];
+                flowOptions = message[i++];
+                distanceToSource = message[i++];
             }
             bool passable = 0;
             float speedModifier = 1.f;
-            if ((state | 2) == state)
+            if (state & 2)
             {
                 passable = 1;
-                speedModifier = *(float*)(message + 10);
+                speedModifier = *(float*)(message + i);
+                i += 4;
+            }
+            Direction dir = blockTypes[id]->getDefaultFrontDirection();
+            if (state & 4)
+            {
+                dir = (Direction)message[i++];
             }
             Framework::Vec3<int> location((index / WORLD_HEIGHT) / CHUNK_SIZE,
                 (index / WORLD_HEIGHT) % CHUNK_SIZE,
@@ -462,7 +475,7 @@ void Chunk::api(char* message)
             if (blockTypes[id]->doesNeedInstance())
             {
                 Block* zB = blockTypes[id]->createBlock(
-                    location, passable, speedModifier);
+                    location, passable, speedModifier, dir);
                 zB->setFlow(flowOptions, distanceToSource);
                 setBlock(zB);
             }
@@ -536,7 +549,7 @@ void Chunk::setBlock(Block* block)
     {
         vcs.lock();
         for (Framework::ArrayIterator<Block*> vi = visibleBlocks.begin(); vi;
-             vi++)
+            vi++)
         {
             if (blocks[index] == (Block*)vi)
             {
@@ -576,8 +589,8 @@ void Chunk::removeBlock(Block* zBlock)
     cs.lock();
     vcs.lock();
     for (Framework::ArrayIterator<Block*> iterator = visibleBlocks.begin();
-         iterator;
-         iterator++)
+        iterator;
+        iterator++)
     {
         if (zBlock == (Block*)iterator)
         {
@@ -612,8 +625,8 @@ void Chunk::blockVisibilityChanged(Block* zB)
     else
     {
         for (Framework::ArrayIterator<Block*> iterator = visibleBlocks.begin();
-             iterator;
-             iterator++)
+            iterator;
+            iterator++)
         {
             if (zB == (Block*)iterator)
             {

+ 4 - 1
FactoryCraft/FactoryClient.cpp

@@ -661,6 +661,8 @@ void FactoryClient::loadServerInfo()
         foregroundReader->read((char*)&fluid, 1);
         char maxFlowDistance = 0;
         if (fluid) foregroundReader->read((char*)&maxFlowDistance, 1);
+        Direction flowDirection;
+        foregroundReader->read((char*)&flowDirection, 1);
         int maxHp;
         foregroundReader->read((char*)&maxHp, 4);
         blockTypes[i] = new BlockType(id,
@@ -669,7 +671,8 @@ void FactoryClient::loadServerInfo()
             maxHp,
             needsSubscription,
             fluid,
-            maxFlowDistance);
+            maxFlowDistance,
+            flowDirection);
     }
     loadMenu->allProgress(1);
     foregroundReader->read((char*)&itemTypeCount, 4);

+ 8 - 11
FactoryCraft/InventoryView.cpp

@@ -14,7 +14,7 @@ InventoryElement::InventoryElement()
     : UIMLElement()
 {}
 
-//! prüft, ob dieses UIML Element für ein bestimmtes xml Element zuständig ist
+//! prüft, ob dieses UIML Element für ein bestimmtes xml Element zuständig ist
 bool InventoryElement::isApplicableFor(Framework::XML::Element& element)
 {
     return element.getName().isEqual("inventory");
@@ -31,16 +31,14 @@ Framework::Drawable* InventoryElement::parseElement(
     {
         Text* first
             = targetValue.getTeilText(0, targetValue.positionOf(",", 0) + 1);
-        Text* second
-            = targetValue.getTeilText(targetValue.positionOf(",", 0) + 1,
-                targetValue.positionOf(",", 1));
-        Text* third
-            = targetValue.getTeilText(targetValue.positionOf(",", 1) + 1,
-                targetValue.positionOf(",", 2));
+        Text* second = targetValue.getTeilText(
+            targetValue.positionOf(",", 0) + 1, targetValue.positionOf(",", 1));
+        Text* third = targetValue.getTeilText(
+            targetValue.positionOf(",", 1) + 1, targetValue.positionOf(",", 2));
         Text* forth
             = targetValue.getTeilText(targetValue.positionOf(",", 2) + 1);
-        target = Framework::Either<int, VecN<int, 4>>(Framework::VecN<int, 4>(
-            {(int)*first, (int)*second, (int)*third, (int)*forth}));
+        target = Framework::VecN<int, 4>(
+            {(int)*first, (int)*second, (int)*third, (int)*forth});
         first->release();
         second->release();
         third->release();
@@ -75,8 +73,7 @@ void SlotInfo::render(
     const Text* filter
         = dynamic_cast<Game*>((Menu*)menuRegister->get("game"))->zFilterText();
     TextRenderer tr;
-    tr.setFontZ(
-        dynamic_cast<Font*>(uiFactory.initParam.font->getThis()));
+    tr.setFontZ(dynamic_cast<Font*>(uiFactory.initParam.font->getThis()));
     tr.setFontSize(12);
     bool filterMatch
         = filter->getLength() > 0 && this->name.has(filter->getText());

+ 3 - 3
FactoryCraft/World.cpp

@@ -132,10 +132,10 @@ void World::update(bool background)
                             setTarget(zBlockAt(Vec3<int>(*(int*)(data + 2),
                                 *(int*)(data + 6),
                                 *(int*)(data + 10))));
-                            int side = *(int*)(data + 14);
-                            short len = *(short*)(data + 18);
+                            Direction side = *(Direction*)(data + 14);
+                            short len = *(short*)(data + 15);
                             char* uiml = new char[len + 1];
-                            memcpy(uiml, data + 20, len);
+                            memcpy(uiml, data + 17, len);
                             uiml[len] = 0;
                             ((Game*)(Menu*)menuRegister->get("game"))
                                 ->setTargetUIML(uiml);