Browse Source

add block_info command for debugging

Kolja Strohm 1 year ago
parent
commit
6398635f03

+ 147 - 0
FactoryCraft/BlockInfoCommand.cpp

@@ -0,0 +1,147 @@
+#include "BlockInfoCommand.h"
+
+#include "Game.h"
+
+BlockInfoCommand::BlockInfoCommand()
+    : ChatCommand("block_info", "Outputs information of a specified block", 1)
+{
+    addParam(
+        new IntegerParameter("x", "The x position of the block", false, 0));
+    addParam(
+        new IntegerParameter("y", "The y position of the block", false, 0));
+    addParam(
+        new IntegerParameter("z", "The z position of the block", false, 0));
+    addParam(new IntegerParameter(
+        "dimensionId", "The dimensionId of the block", true, [](Entity* e) {
+            if (e) return e->getDimensionId();
+            return 0;
+        }));
+}
+
+void BlockInfoCommand::execute(
+    Framework::RCArray<Framework::Text> params, Entity* zActor) const
+{
+    int x = (int)*params.z(0);
+    int y = (int)*params.z(1);
+    int z = (int)*params.z(2);
+    int dimension = (int)*params.z(3);
+    Vec3<int> location = {x, y, z};
+    Text result = "";
+    if (Game::INSTANCE->zDimension(dimension))
+    {
+        auto block = Game::INSTANCE->zBlockAt(location, dimension);
+
+        if (block.isB())
+        {
+            result += "No block instance was created yet.\n";
+            result += "  Block Type: ";
+            if (block.getB() == 0)
+            {
+                result += "[not loaded]";
+            }
+            else
+            {
+                result += StaticRegistry<BlockType>::INSTANCE
+                              .zElement(block.getB())
+                              ->getName();
+            }
+            result += " (";
+            result += block.getB();
+            result += ")\n";
+        }
+        else
+        {
+            Block* zBlock = block.getA();
+            result.append()
+                << "Block instance found.\n  "
+                << "  Block Type: " << zBlock->zBlockType()->getName() << " ("
+                << zBlock->zBlockType()->getId() << ")\n"
+                << "  HP: " << zBlock->getHP() << " / " << zBlock->getMaxHP()
+                << "\n"
+                << "  Transparent: " << zBlock->isTransparent() << "\n"
+                << "  Passable: " << zBlock->isPassable() << "\n"
+                << "  Hardness: " << zBlock->getHardness() << "\n"
+                << "  Tick source: " << zBlock->isTickSource() << "\n"
+                << "  Interactable by hand: " << zBlock->isInteractable(0)
+                << "\n"
+                << "  Light emission: ("
+                << (int)zBlock->getLightEmisionColor()[0] << ", "
+                << (int)zBlock->getLightEmisionColor()[1] << ", "
+                << (int)zBlock->getLightEmisionColor()[2] << ")\n";
+        }
+        result += "  Light data: ";
+        Chunk* zChunk = Game::INSTANCE->zDimension(dimension)->zChunk(
+            Game::getChunkCenter(x, y));
+        if (zChunk)
+        {
+            unsigned char* light
+                = zChunk->getLightData(Dimension::chunkCoordinates(location));
+            result.append()
+                << "(" << (int)light[0] << ", " << (int)light[1] << ", "
+                << (int)light[2] << ", " << (int)light[3] << ", "
+                << (int)light[4] << ", " << (int)light[5] << ")";
+        }
+        else
+        {
+            result += "[not loaded]";
+        }
+        result += "\n";
+        for (int i = 0; i < 6; i++)
+        {
+            Vec3<int> pos = location + getDirection(getDirectionFromIndex(i));
+            if (pos.z >= 0 && pos.z < WORLD_HEIGHT)
+            {
+                switch (i)
+                {
+                case 0:
+                    result += "    North: ";
+                    break;
+                case 1:
+                    result += "    East: ";
+                    break;
+                case 2:
+                    result += "    South: ";
+                    break;
+                case 3:
+                    result += "    West: ";
+                    break;
+                case 4:
+                    result += "    Top: ";
+                    break;
+                case 5:
+                    result += "    Bottom: ";
+                    break;
+                }
+                zChunk = Game::INSTANCE->zDimension(dimension)->zChunk(
+                    Game::getChunkCenter(x, y));
+                if (zChunk)
+                {
+                    unsigned char* light = zChunk->getLightData(
+                        Dimension::chunkCoordinates(pos));
+                    result.append()
+                        << "(" << (int)light[0] << ", " << (int)light[1] << ", "
+                        << (int)light[2] << ", " << (int)light[3] << ", "
+                        << (int)light[4] << ", " << (int)light[5] << ")";
+                }
+                else
+                {
+                    result += "[not loaded]";
+                }
+                result += "\n";
+            }
+        }
+    }
+    else
+    {
+        result += "Given dimension is not loaded";
+    }
+    if (zActor)
+    {
+        Game::INSTANCE->zChat()->sendMessageTo(
+            result, zActor, Chat::CHANNEL_INFO);
+    }
+    else
+    {
+        std::cout << result;
+    }
+}

+ 12 - 0
FactoryCraft/BlockInfoCommand.h

@@ -0,0 +1,12 @@
+#pragma once
+
+#include "ChatCommand.h"
+
+class BlockInfoCommand : public ChatCommand
+{
+public:
+    BlockInfoCommand();
+
+    void execute(Framework::RCArray<Framework::Text> params,
+        Entity* zActor) const override;
+};

+ 2 - 0
FactoryCraft/ChatCommandExecutor.cpp

@@ -3,12 +3,14 @@
 #include "Game.h"
 #include "GrantCommand.h"
 #include "SaveCommand.h"
+#include "BlockInfoCommand.h"
 
 ChatCommandExecutor::ChatCommandExecutor()
     : ReferenceCounter()
 {
     knownCommands.add(new SaveCommand());
     knownCommands.add(new CrantCommand());
+    knownCommands.add(new BlockInfoCommand());
 }
 
 bool ChatCommandExecutor::execute(Framework::Text line, Entity* zActor)

+ 2 - 0
Windows Version/Windows Version.vcxproj

@@ -165,6 +165,7 @@ copy ..\..\..\..\..\Allgemein\Framework\x64\release\Framework.dll Framework.dll<
     <ClCompile Include="..\FactoryCraft\BasicTool.cpp" />
     <ClCompile Include="..\FactoryCraft\BiomGenerator.cpp" />
     <ClCompile Include="..\FactoryCraft\Block.cpp" />
+    <ClCompile Include="..\FactoryCraft\BlockInfoCommand.cpp" />
     <ClCompile Include="..\FactoryCraft\BlockType.cpp" />
     <ClCompile Include="..\FactoryCraft\BlockTypeGeneratorRule.cpp" />
     <ClCompile Include="..\FactoryCraft\CaveGenerator.cpp" />
@@ -246,6 +247,7 @@ copy ..\..\..\..\..\Allgemein\Framework\x64\release\Framework.dll Framework.dll<
     <ClCompile Include="..\FactoryCraft\WormCaveGenerator.cpp" />
   </ItemGroup>
   <ItemGroup>
+    <ClInclude Include="..\FactoryCraft\BlockInfoCommand.h" />
     <ClInclude Include="..\FactoryCraft\FactorizeNoise.h" />
     <ClInclude Include="..\FactoryCraft\FlattenNoise.h" />
     <ClInclude Include="..\FactoryCraft\GeneratorRule.h" />

+ 6 - 0
Windows Version/Windows Version.vcxproj.filters

@@ -357,6 +357,9 @@
     <ClCompile Include="..\FactoryCraft\InformationObserver.cpp">
       <Filter>server\response</Filter>
     </ClCompile>
+    <ClCompile Include="..\FactoryCraft\BlockInfoCommand.cpp">
+      <Filter>chat\commands</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="..\FactoryCraft\Chunk.h">
@@ -632,5 +635,8 @@
     <ClInclude Include="..\FactoryCraft\InformationObserver.h">
       <Filter>server\response</Filter>
     </ClInclude>
+    <ClInclude Include="..\FactoryCraft\BlockInfoCommand.h">
+      <Filter>chat\commands</Filter>
+    </ClInclude>
   </ItemGroup>
 </Project>