소스 검색

add cheat mode to quickly get any items

Kolja Strohm 1 개월 전
부모
커밋
0798e7bb8b

+ 25 - 2
FactoryCraft/ChatCommand.cpp

@@ -140,6 +140,11 @@ PlayerNameParameter::PlayerNameParameter()
           "player", "The name of the player (has to be online)", 0)
 {}
 
+PlayerNameParameter::PlayerNameParameter(bool optional)
+    : ChatCommandParameter(
+          "player", "The name of the player (has to be online)", optional)
+{}
+
 bool PlayerNameParameter::isLegalValue(Framework::Text value) const
 {
     return Game::INSTANCE->zPlayerByName(value) != 0;
@@ -174,12 +179,30 @@ IntegerParameter::IntegerParameter(Framework::Text name,
     bool optional,
     std::function<int(Entity* zEntity)> calculateDefault)
     : ChatCommandParameter(name, description, optional),
-      calculateDefault(calculateDefault)
+      calculateDefault(calculateDefault),
+      hasMin(0),
+      minValue(0),
+      hasMax(0),
+      maxValue(0)
 {}
 
+IntegerParameter* IntegerParameter::setMin(int minValue)
+{
+    hasMin = 1;
+    this->minValue = minValue;
+    return this;
+}
+
+IntegerParameter* IntegerParameter::setMax(int maxValue)
+{
+    hasMax = 1;
+    this->maxValue = maxValue;
+    return this;
+}
+
 bool IntegerParameter::isLegalValue(Framework::Text value) const
 {
-    return Framework::Text((int)value).isEqual(value);
+    return Framework::Text((int)value).isEqual(value) && (!hasMin || minValue <= (int)value) && (!hasMax || maxValue >= (int)value);
 }
 
 Framework::Text IntegerParameter::getDefaultValue(Entity* zActor) const

+ 7 - 1
FactoryCraft/ChatCommand.h

@@ -61,6 +61,7 @@ class PlayerNameParameter : public ChatCommandParameter
 {
 public:
     PlayerNameParameter();
+    PlayerNameParameter(bool optional);
 
     bool isLegalValue(Framework::Text value) const override;
     Framework::Text getDefaultValue(Entity* zActor) const override;
@@ -72,13 +73,18 @@ class IntegerParameter : public ChatCommandParameter
 {
 private:
     std::function<int(Entity* zEntity)> calculateDefault;
+    bool hasMin;
+    int minValue;
+    bool hasMax;
+    int maxValue;
 
 public:
     IntegerParameter(Framework::Text name,
         Framework::Text description,
         bool optional,
         std::function<int(Entity* zEntity)> calculateDefault);
-
+    IntegerParameter* setMin(int minValue);
+    IntegerParameter* setMax(int maxValue);
     bool isLegalValue(Framework::Text value) const override;
     Framework::Text getDefaultValue(Entity* zActor) const override;
 };

+ 4 - 2
FactoryCraft/ChatCommandExecutor.cpp

@@ -4,6 +4,7 @@
 #include "Chat.h"
 #include "Game.h"
 #include "GrantCommand.h"
+#include "ModeCommand.h"
 #include "SaveCommand.h"
 
 ChatCommandExecutor::ChatCommandExecutor()
@@ -12,6 +13,7 @@ ChatCommandExecutor::ChatCommandExecutor()
     knownCommands.add(new SaveCommand());
     knownCommands.add(new CrantCommand());
     knownCommands.add(new BlockInfoCommand());
+    knownCommands.add(new ModeCommand());
     // Add more commands here
     for (ChatCommand* command : knownCommands)
     {
@@ -37,8 +39,8 @@ bool ChatCommandExecutor::execute(Framework::Text line, Entity* zActor)
                 int start = command->getName().getLength() + 2;
                 bool escaped = 0;
                 for (int i = command->getName().getLength() + 2;
-                     i < line.getLength();
-                     i++)
+                    i < line.getLength();
+                    i++)
                 {
                     if (line.hasAt(i, " ") && !escaped)
                     {

+ 17 - 1
FactoryCraft/Dimension.cpp

@@ -332,13 +332,16 @@ void Dimension::entityTickLoop()
     double ausgleich = 0.0;
     zm.measureStart();
     Sleep(16);
+    int etps = 0;
+    double secondSum = 0.0;
     while (!stop)
     {
         zm.measureEnd();
         double seconds = zm.getSekunden();
         zm.measureStart();
         {
-            Framework::CriticalLock lock({&entityCs, &chunkCs});
+            Framework::StackLock* lock
+                = new Framework::StackLock({&entityCs, &chunkCs});
             auto iterator = entities->begin();
             auto end = entities->end();
             while (iterator != end)
@@ -369,8 +372,21 @@ void Dimension::entityTickLoop()
                 }
                 index++;
             }
+            delete lock;
         } // end of LOCK entityCs, chunkCs
         ausgleich += 1.0 / 30 - seconds;
+        etps++;
+        secondSum += seconds;
+        if (secondSum > 1.0)
+        {
+            if (etps < 29)
+            {
+                Framework::Logging::warning()
+                    << "Only " << etps << " entity ticks per second!";
+            }
+            etps = 0;
+            secondSum -= 1.0;
+        }
         if (ausgleich > 0) Sleep((int)(ausgleich * 1000));
     }
 }

+ 2 - 0
FactoryCraft/FactoryCraft.vcxproj

@@ -172,6 +172,7 @@
     <ClInclude Include="JsonUtils.h" />
     <ClInclude Include="LightSources.h" />
     <ClInclude Include="DimensionMap.h" />
+    <ClInclude Include="ModeCommand.h" />
     <ClInclude Include="ModelInfo.h" />
     <ClInclude Include="MultiblockStructure.h" />
     <ClInclude Include="MultiblockStructureManager.h" />
@@ -302,6 +303,7 @@
     <ClCompile Include="JsonExpression.cpp" />
     <ClCompile Include="JsonUtils.cpp" />
     <ClCompile Include="LightSources.cpp" />
+    <ClCompile Include="ModeCommand.cpp" />
     <ClCompile Include="ModelInfo.cpp" />
     <ClCompile Include="MultiblockStructure.cpp" />
     <ClCompile Include="MultiblockStructureManager.cpp" />

+ 4 - 0
FactoryCraft/FireBasedProcessingBlockComponent.cpp

@@ -86,6 +86,8 @@ bool FireBasedProcessingBlockComponent::consumeFuel()
                 if (fuelBuffer == 0)
                 {
                     burning = false;
+                    Game::INSTANCE->updateLightningWithoutWait(
+                        zBlock->getDimensionId(), zBlock->getPos());
                 }
             }
         }
@@ -94,6 +96,8 @@ bool FireBasedProcessingBlockComponent::consumeFuel()
             if (fuelBuffer == 0)
             {
                 burning = false;
+                Game::INSTANCE->updateLightningWithoutWait(
+                    zBlock->getDimensionId(), zBlock->getPos());
             }
         }
     }

+ 83 - 0
FactoryCraft/ModeCommand.cpp

@@ -0,0 +1,83 @@
+#include "ModeCommand.h"
+
+#include "Chat.h"
+#include "Game.h"
+#include "Player.h"
+
+ModeCommand::ModeCommand()
+    : ChatCommand(
+          "gamemode", "sets the gamemode to 0 (normal) or 1 (cheats)", 10)
+{
+    addParam(new PlayerNameParameter(true));
+    addParam(new IntegerParameter("mode",
+        "0 (normal) or 1 (cheats)",
+        false,
+        [](Entity* actor) {
+            if (actor) return dynamic_cast<Player*>(actor)->getGameMode();
+            return 0;
+        }));
+}
+
+bool ModeCommand::execute(
+    Framework::RCArray<Framework::Text>& params, Entity* zActor) const
+{
+    if (zActor && params.getEntryCount() == 1)
+    {
+        Player* p = dynamic_cast<Player*>(zActor);
+        int mode = (int)*params.z(0);
+        if (mode < 0 || mode > 1)
+        {
+            Framework::Text message = "Usage: gamemode [<playerName>] <0/1>";
+            Game::INSTANCE->zChat()->sendMessageTo(
+                message, zActor, Chat::CHANNEL_INFO);
+            message = "gamemode must be 0 or 1";
+            Game::INSTANCE->zChat()->sendMessageTo(
+                message, zActor, Chat::CHANNEL_INFO);
+            return false;
+        }
+        p->setGameMode(mode);
+        Framework::Text message = "gamemode for player ";
+        message.append() << p->getName() << " is now set to " << mode;
+        Game::INSTANCE->zChat()->sendMessageTo(
+            message, zActor, Chat::CHANNEL_INFO);
+    }
+    else if (params.getEntryCount() == 2)
+    {
+        Player* p = Game::INSTANCE->zPlayerByName(*params.z(0));
+        if (!p)
+        {
+            Framework::Text message = "Usage: gamemode [<playerName>] <0/1>";
+            Game::INSTANCE->zChat()->sendMessageTo(
+                message, zActor, Chat::CHANNEL_INFO);
+            message = "";
+            message.append() << "Player " << *params.z(0) << " not found";
+            Game::INSTANCE->zChat()->sendMessageTo(
+                message, zActor, Chat::CHANNEL_INFO);
+            return false;
+        }
+        int mode = (int)*params.z(1);
+        if (mode < 0 || mode > 1)
+        {
+            Framework::Text message = "Usage: gamemode [<playerName>] <0/1>";
+            Game::INSTANCE->zChat()->sendMessageTo(
+                message, zActor, Chat::CHANNEL_INFO);
+            message = "gamemode must be 0 or 1";
+            Game::INSTANCE->zChat()->sendMessageTo(
+                message, zActor, Chat::CHANNEL_INFO);
+            return false;
+        }
+        p->setGameMode(mode);
+        Framework::Text message = "gamemode for player ";
+        message.append() << p->getName() << " is now set to " << mode;
+        Game::INSTANCE->zChat()->sendMessageTo(
+            message, zActor, Chat::CHANNEL_INFO);
+    }
+    else
+    {
+        Framework::Text message = "Usage: gamemode [<playerName>] <0/1>";
+        Game::INSTANCE->zChat()->sendMessageTo(
+            message, zActor, Chat::CHANNEL_INFO);
+        return false;
+    }
+    return true;
+}

+ 11 - 0
FactoryCraft/ModeCommand.h

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

+ 43 - 0
FactoryCraft/Player.cpp

@@ -409,6 +409,39 @@ void Player::playerApi(
             movementFlags = flags;
             break;
         }
+    case 11: // give Items
+    {
+        if (gameMode)
+        {
+            int type;
+            zRequest->read((char*)&type, 4);
+            bool fullStack = 0;
+            zRequest->read((char*)&fullStack, 1);
+            Item *item = Game::INSTANCE->zItemType(type)->createItem();
+            if (item)
+            {
+                ItemStack* stack = new ItemStack(
+                    item, fullStack ? item->getMaxStackSize() : 1);
+                unsaveAddItem(stack, NO_DIRECTION, 0);
+                if (stack->getSize() > 0)
+                {
+                    Game::INSTANCE->zChat()->sendMessageTo(
+                        "Der Stack konnte nicht vollständig hinzugefügt werden. Eventuell ist das Inventar voll.",
+                        this,
+                        Chat::CHANNEL_ERROR);
+                }
+                stack->release();
+            }
+            else
+            {
+                Game::INSTANCE->zChat()->sendMessageTo(
+                    "Ein Item dieses Typs kann nicht erstellt werden.",
+                    this,
+                    Chat::CHANNEL_ERROR);
+            }
+        }
+        break;
+    }
     }
 }
 
@@ -426,6 +459,16 @@ void Player::onDeath(Entity* zActor, Item* zUsedItem, ItemSkill* zUsedSkill)
     // TODO: respown
 }
 
+void Player::setGameMode(int gameMode)
+{
+    this->gameMode = gameMode;
+}
+
+int Player::getGameMode() const
+{
+    return gameMode;
+}
+
 PlayerEntityType::PlayerEntityType()
     : EntityType()
 {

+ 3 - 0
FactoryCraft/Player.h

@@ -26,6 +26,7 @@ private:
     int leftHandPosition;
     bool jumping;
     __int64 keyState;
+    int gameMode;
 
     void useItemSlot(ItemSlot* zSlot, bool left);
     Player(Framework::Vec3<float> location, int dimensionId, int entityId);
@@ -44,6 +45,8 @@ public:
     void onFall(float collisionSpeed) override;
     void onDeath(
         Entity* zActor, Item* zUsedItem, ItemSkill* zUsedSkill) override;
+    void setGameMode(int gameMode);
+    int getGameMode() const;
 
     friend PlayerEntityType;
 };

+ 2 - 0
Windows Version/Windows Version.vcxproj

@@ -229,6 +229,7 @@ copy ..\..\..\..\..\Allgemein\Framework\x64\release\Framework.dll Framework.dll<
     <ClCompile Include="..\FactoryCraft\JsonExpression.cpp" />
     <ClCompile Include="..\FactoryCraft\JsonUtils.cpp" />
     <ClCompile Include="..\FactoryCraft\LightSources.cpp" />
+    <ClCompile Include="..\FactoryCraft\ModeCommand.cpp" />
     <ClCompile Include="..\FactoryCraft\ModelInfo.cpp" />
     <ClCompile Include="..\FactoryCraft\MultiblockStructure.cpp" />
     <ClCompile Include="..\FactoryCraft\MultiblockStructureManager.cpp" />
@@ -361,6 +362,7 @@ copy ..\..\..\..\..\Allgemein\Framework\x64\release\Framework.dll Framework.dll<
     <ClInclude Include="..\FactoryCraft\JsonUtils.h" />
     <ClInclude Include="..\FactoryCraft\LightSources.h" />
     <ClInclude Include="..\FactoryCraft\DimensionMap.h" />
+    <ClInclude Include="..\FactoryCraft\ModeCommand.h" />
     <ClInclude Include="..\FactoryCraft\ModelInfo.h" />
     <ClInclude Include="..\FactoryCraft\MultiblockStructure.h" />
     <ClInclude Include="..\FactoryCraft\MultiblockStructureManager.h" />

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

@@ -516,6 +516,9 @@
     <ClCompile Include="..\FactoryCraft\FloatDistribution.cpp">
       <Filter>world\generator\distribution</Filter>
     </ClCompile>
+    <ClCompile Include="..\FactoryCraft\ModeCommand.cpp">
+      <Filter>chat\commands</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="..\FactoryCraft\Chunk.h">
@@ -911,5 +914,8 @@
     <ClInclude Include="..\FactoryCraft\FloatDistribution.h">
       <Filter>world\generator\distribution</Filter>
     </ClInclude>
+    <ClInclude Include="..\FactoryCraft\ModeCommand.h">
+      <Filter>chat\commands</Filter>
+    </ClInclude>
   </ItemGroup>
 </Project>