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

change animation messages to be handled by chunk api

Kolja Strohm пре 2 година
родитељ
комит
6548d5b031

+ 2 - 1
FactoryCraft/Block.cpp

@@ -425,7 +425,8 @@ BasicBlockItemType::BasicBlockItemType(int id,
       hardness(1.f),
       toolId(0),
       speedModifier(1.f),
-      blockTypeId(blockTypeId)
+      blockTypeId(blockTypeId),
+      placableProofState(0)
 {}
 
 void BasicBlockItemType::loadSuperItem(

+ 1 - 1
FactoryCraft/BlockType.h

@@ -44,7 +44,7 @@ public:
     static const int CRAFTING_TABLE = 27;
     static const int CHEST = 28;
 
-    static const int MAX_VALUE = 28;
+    static const int MAX_VALUE = 100000; // leve enough space for other blocks
 };
 
 class BlockType : public virtual Framework::ReferenceCounter

+ 10 - 5
FactoryCraft/Chest.cpp

@@ -30,7 +30,8 @@ void Chest::onDialogClosed(Framework::Text dialogId)
         userEntityId = 0;
         NetworkMessage* msg = new NetworkMessage();
         msg->animateBlockBone(getDimensionId(),
-            getPos(),
+            Game::getChunkCenter(getPos().x, getPos().y),
+            Chunk::index(Dimension::chunkCoordinates(getPos())),
             1,
             1.0,
             Vec3<float>(1.f, 1.f, 0.45f),
@@ -113,11 +114,13 @@ void Chest::interact(Item* zItem, Entity* zActor)
         Game::INSTANCE->sendMessage(msg, zActor);
         msg = new NetworkMessage();
         msg->animateBlockBone(getDimensionId(),
-            getPos(),
+            Game::getChunkCenter(getPos().x, getPos().y),
+            Chunk::index(Dimension::chunkCoordinates(getPos())),
             1,
             1.0,
             Vec3<float>(1.f, 1.f, 0.45f),
-            Vec3<float>(0.0f, (float)(PI / 2.0), 0.f)); // open the chest over 1 second
+            Vec3<float>(
+                0.0f, (float)(PI / 2.0), 0.f)); // open the chest over 1 second
         Game::INSTANCE->broadcastMessage(msg);
     }
     unlock();
@@ -128,10 +131,12 @@ void Chest::sendModelInfo(NetworkMessage* zMessage)
     if (open)
     {
         zMessage->animateBlockBone(getDimensionId(),
-            getPos(),
+            Game::getChunkCenter(getPos().x, getPos().y),
+            Chunk::index(Dimension::chunkCoordinates(getPos())),
             1,
             0.0,
             Vec3<float>(1.f, 1.f, 0.45f),
-            Vec3<float>(0.0f, (float)(PI / 2.0), 0.f)); // open the chest instantly
+            Vec3<float>(
+                0.0f, (float)(PI / 2.0), 0.f)); // open the chest instantly
     }
 }

+ 14 - 23
FactoryCraft/Chunk.cpp

@@ -1,7 +1,8 @@
+#include "Chunk.h"
+
 #include <AsynchronCall.h>
 #include <InMemoryBuffer.h>
 
-#include "Chunk.h"
 #include "Constants.h"
 #include "Game.h"
 #include "NoBlock.h"
@@ -181,7 +182,8 @@ void Chunk::sendLightToClient(Framework::StreamWriter* zWriter)
                             zWriter->schreibe((char*)&y, 4);
                             zWriter->schreibe((char*)&z, 4);
                             zWriter->schreibe(
-                                (char*)(zNeighbours[dir]->lightData + index * 6),
+                                (char*)(zNeighbours[dir]->lightData
+                                        + index * 6),
                                 6);
                         }
                     }
@@ -396,8 +398,7 @@ void Chunk::initializeLightning()
 Framework::Either<Block*, int> Chunk::zBlockAt(
     Framework::Vec3<int> location) const
 {
-    int index
-        = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
+    int index = Chunk::index(location);
     assert(index < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT);
     if (blocks[index])
         return blocks[index];
@@ -409,8 +410,7 @@ const Block* Chunk::zBlockConst(Framework::Vec3<int> location) const
 {
     auto b = zBlockAt(location);
     if (b.isA()) return b;
-    return StaticRegistry<BlockType>::INSTANCE.zElement(b.getB())
-        ->zDefault();
+    return StaticRegistry<BlockType>::INSTANCE.zElement(b.getB())->zDefault();
 }
 
 void Chunk::instantiateBlock(Framework::Vec3<int> location)
@@ -431,8 +431,7 @@ void Chunk::instantiateBlock(Framework::Vec3<int> location)
 
 void Chunk::generateBlock(Framework::Vec3<int> location)
 {
-    int index
-        = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
+    int index = Chunk::index(location);
     if (blockIds[index]) return;
     auto generated = Game::INSTANCE->zGenerator()->generateSingleBlock(
         {location.x + this->location.x - CHUNK_SIZE / 2,
@@ -447,8 +446,7 @@ void Chunk::generateBlock(Framework::Vec3<int> location)
 
 void Chunk::putBlockAt(Framework::Vec3<int> location, Block* block)
 {
-    int index
-        = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
+    int index = Chunk::index(location);
     assert(index < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT && index >= 0);
     Block* old = blocks[index];
     if (old && old->isTickSource())
@@ -541,8 +539,7 @@ void Chunk::putBlockAt(Framework::Vec3<int> location, Block* block)
 
 void Chunk::putBlockTypeAt(Framework::Vec3<int> location, int type)
 {
-    int index
-        = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
+    int index = Chunk::index(location);
     assert(index < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT);
     bool wasLightSource
         = StaticRegistry<BlockType>::INSTANCE.zElement(blockIds[index])
@@ -585,8 +582,7 @@ void Chunk::putBlockTypeAt(Framework::Vec3<int> location, int type)
 
 void Chunk::sendBlockInfo(Framework::Vec3<int> location)
 {
-    int index
-        = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
+    int index = Chunk::index(location);
     char* msg = new char[9];
     msg[0] = 0; // set block
     *(int*)(msg + 1) = index;
@@ -602,9 +598,7 @@ void Chunk::sendBlockInfo(Framework::Vec3<int> location)
         if (loc.x >= 0 && loc.x < CHUNK_SIZE && loc.y >= 0 && loc.y < CHUNK_SIZE
             && loc.z >= 0 && loc.z < WORLD_HEIGHT)
         {
-            int index
-                = (loc.x * CHUNK_SIZE + loc.y) * WORLD_HEIGHT + loc.z;
-            broadcastLightData(index, true);
+            broadcastLightData(Chunk::index(loc), true);
         }
         else if (loc.z >= 0 && loc.z < WORLD_HEIGHT && i < 4 && zNeighbours[i])
         {
@@ -755,7 +749,7 @@ void Chunk::sendToClient(Framework::StreamWriter* zWriter)
         {
             for (int z = 0; z < WORLD_HEIGHT; z++)
             {
-                int index = (x * CHUNK_SIZE + y) * WORLD_HEIGHT + z;
+                int index = Chunk::index({x, y, z});
                 unsigned short blockType
                     = blocks[index]
                         ? (unsigned short)blocks[index]->zBlockType()->getId()
@@ -924,9 +918,7 @@ bool Chunk::hasObservers() const
 
 unsigned char* Chunk::getLightData(Framework::Vec3<int> location) const
 {
-    int index
-        = ((location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z)
-        * 6;
+    int index = Chunk::index(location) * 6;
     assert(index < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * 6);
     return lightData + index;
 }
@@ -934,8 +926,7 @@ unsigned char* Chunk::getLightData(Framework::Vec3<int> location) const
 void Chunk::setLightData(
     Framework::Vec3<int> location, unsigned char* data, bool foreground)
 {
-    int index
-        = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
+    int index = Chunk::index(location);
     memcpy(lightData + index * 6, data, 6);
     // check if neighbor is a visible block and send update to clients
     bool needSend = 0;

+ 7 - 0
FactoryCraft/Chunk.h

@@ -9,6 +9,7 @@
 #include "Block.h"
 #include "DoLaterHandler.h"
 #include "Tickable.h"
+#include "Constants.h"
 
 class ChunkMap;
 
@@ -83,5 +84,11 @@ public:
     void setLightData(
         Framework::Vec3<int> location, unsigned char* data, bool foreground);
 
+    inline static int index(Framework::Vec3<int> localLocation)
+    {
+        return (localLocation.x * CHUNK_SIZE + localLocation.y) * WORLD_HEIGHT
+             + localLocation.z;
+    }
+
     friend ChunkMap;
 };

+ 26 - 49
FactoryCraft/Dimension.cpp

@@ -189,12 +189,8 @@ void Dimension::thread()
         {
             if (chunk)
             {
-                int x = position.x % CHUNK_SIZE;
-                int y = position.y % CHUNK_SIZE;
-                if (x < 0) x += CHUNK_SIZE;
-                if (y < 0) y += CHUNK_SIZE;
-                unsigned char* light
-                    = chunk->getLightData(Vec3<int>(x, y, position.z));
+                Vec3<int> chunkPos = chunkCoordinates(position);
+                unsigned char* light = chunk->getLightData(chunkPos);
                 unsigned char dayLight[6] = {255, 255, 255, 0, 0, 0};
                 unsigned char noLight[6] = {0, 0, 0, 0, 0, 0};
                 unsigned char newLight[6] = {0, 0, 0, 0, 0, 0};
@@ -217,13 +213,9 @@ void Dimension::thread()
                         Chunk* neighborChunk
                             = zChunk(Game::INSTANCE->getChunkCenter(
                                 neighborPos.x, neighborPos.y));
-                        int x = neighborPos.x % CHUNK_SIZE;
-                        int y = neighborPos.y % CHUNK_SIZE;
-                        if (x < 0) x += CHUNK_SIZE;
-                        if (y < 0) y += CHUNK_SIZE;
                         if (neighborChunk)
                             neighborLeight = neighborChunk->getLightData(
-                                Vec3<int>(x, y, neighborPos.z));
+                                chunkCoordinates(neighborPos));
                         else
                             neighborLeight = noLight;
                     }
@@ -248,7 +240,7 @@ void Dimension::thread()
                 {
                     if (newLight[i] != light[i])
                     {
-                        chunk->setLightData(Vec3<int>(x, y, position.z),
+                        chunk->setLightData(chunkPos,
                             newLight,
                             isForeground);
                         for (int j = 0; j < 6; j++)
@@ -329,11 +321,8 @@ Framework::Either<Block*, int> Dimension::zBlock(Vec3<int> location)
     Chunk* c = zChunk(Game::INSTANCE->getChunkCenter(location.x, location.y));
     if (c)
     {
-        int x = location.x % CHUNK_SIZE;
-        int y = location.y % CHUNK_SIZE;
-        if (x < 0) x += CHUNK_SIZE;
-        if (y < 0) y += CHUNK_SIZE;
-        return c->zBlockAt(Vec3<int>(x, y, location.z));
+        location = chunkCoordinates(location);
+        return c->zBlockAt(location);
     }
     return 0;
 }
@@ -343,12 +332,9 @@ Block* Dimension::zRealBlockInstance(Framework::Vec3<int> location)
     Chunk* c = zChunk(Game::INSTANCE->getChunkCenter(location.x, location.y));
     if (c)
     {
-        int x = location.x % CHUNK_SIZE;
-        int y = location.y % CHUNK_SIZE;
-        if (x < 0) x += CHUNK_SIZE;
-        if (y < 0) y += CHUNK_SIZE;
-        c->instantiateBlock(Vec3<int>(x, y, location.z));
-        auto result = c->zBlockAt(Vec3<int>(x, y, location.z));
+        location = chunkCoordinates(location);
+        c->instantiateBlock(location);
+        auto result = c->zBlockAt(location);
         return result.isA() ? result.getA() : 0;
     }
     return 0;
@@ -359,11 +345,8 @@ const Block* Dimension::zBlockOrDefault(Framework::Vec3<int> location)
     Chunk* c = zChunk(Game::INSTANCE->getChunkCenter(location.x, location.y));
     if (c)
     {
-        int x = location.x % CHUNK_SIZE;
-        int y = location.y % CHUNK_SIZE;
-        if (x < 0) x += CHUNK_SIZE;
-        if (y < 0) y += CHUNK_SIZE;
-        return c->zBlockConst(Vec3<int>(x, y, location.z));
+        location = chunkCoordinates(location);
+        return c->zBlockConst(location);
     }
     return &NoBlock::INSTANCE;
 }
@@ -374,16 +357,13 @@ void Dimension::placeBlock(
     Chunk* c = zChunk(Game::getChunkCenter(location.x, location.y));
     if (c)
     {
-        int x = location.x % CHUNK_SIZE;
-        int y = location.y % CHUNK_SIZE;
-        if (x < 0) x += CHUNK_SIZE;
-        if (y < 0) y += CHUNK_SIZE;
+        location = chunkCoordinates(location);
         if (block.isA())
-            c->putBlockAt(Vec3<int>(x, y, location.z), block);
+            c->putBlockAt(location, block);
         else
         {
-            c->putBlockAt(Vec3<int>(x, y, location.z), 0);
-            c->putBlockTypeAt(Vec3<int>(x, y, location.z), block);
+            c->putBlockAt(location, 0);
+            c->putBlockTypeAt(location, block);
         }
     }
     else if (block.isA())
@@ -395,11 +375,8 @@ void Dimension::sendBlockInfo(Framework::Vec3<int> location)
     Chunk* c = zChunk(Game::getChunkCenter(location.x, location.y));
     if (c)
     {
-        int x = location.x % CHUNK_SIZE;
-        int y = location.y % CHUNK_SIZE;
-        if (x < 0) x += CHUNK_SIZE;
-        if (y < 0) y += CHUNK_SIZE;
-        c->sendBlockInfo(Vec3<int>(x, y, location.z));
+        location = chunkCoordinates(location);
+        c->sendBlockInfo(location);
     }
 }
 
@@ -902,16 +879,16 @@ void Dimension::updateMap(int x, int y, int height)
     ChunkMap* cMap = map->getMap(addr, 8, center);
     if (cMap)
     {
-        x = x % CHUNK_SIZE;
-        y = y % CHUNK_SIZE;
-        if (x < 0) x += CHUNK_SIZE;
-        if (y < 0) y += CHUNK_SIZE;
-        if (cMap->update(
-                (char)x, (char)y, (unsigned char)(height / 2), color1, color2)
+        Framework::Vec3<int> chunkLocation = chunkCoordinates({x, y, height});
+        if (cMap->update((char)chunkLocation.x,
+                (char)chunkLocation.y,
+                (unsigned char)(chunkLocation.z / 2),
+                color1,
+                color2)
             || (h1 > 0
-                && cMap->update((char)x,
-                    (char)y,
-                    (unsigned char)(height / 2) - 1,
+                && cMap->update((char)chunkLocation.x,
+                    (char)chunkLocation.y,
+                    (unsigned char)(chunkLocation.z / 2) - 1,
                     color1m,
                     color2m)))
         {

+ 11 - 0
FactoryCraft/Dimension.h

@@ -86,5 +86,16 @@ public:
     void updateMap(int x, int y, int height);
     int getChunkCount();
 
+    inline static Framework::Vec3<int> chunkCoordinates(
+        Framework::Vec3<int> worldLocation)
+    {
+        Framework::Vec3<int> result = worldLocation;
+        result.x = result.x % CHUNK_SIZE;
+        result.y = result.y % CHUNK_SIZE;
+        if (result.x < 0) result.x += CHUNK_SIZE;
+        if (result.y < 0) result.y += CHUNK_SIZE;
+        return result;
+    }
+
     friend DimensionMap;
 };

+ 14 - 3
FactoryCraft/Game.cpp

@@ -375,8 +375,8 @@ void Game::thread()
                         + " left the game.",
                     Chat::CHANNEL_INFO);
                 Datei pFile;
-                pFile.setDatei(
-                    path + "/player/" + getPlayerId(player->zEntity()->getName()));
+                pFile.setDatei(path + "/player/"
+                               + getPlayerId(player->zEntity()->getName()));
                 pFile.erstellen();
                 if (pFile.open(Datei::Style::schreiben))
                     StaticRegistry<EntityType>::INSTANCE
@@ -561,6 +561,17 @@ void Game::api(Framework::InMemoryBuffer* zRequest, GameClient* zOrigin)
             chat->chatApi(zRequest, zOrigin->zEntity(), response);
             break;
         }
+    case 7: // other dimension
+        {
+            int dimensionId;
+            zRequest->lese((char*)&dimensionId, 4);
+            Dimension* dim = zDimension(dimensionId);
+            if (dim)
+            {
+                dim->api(zRequest, response, zOrigin->zEntity());
+            }
+            break;
+        }
     default:
         std::cout << "received unknown api request in game with type "
                   << (int)type << "\n";
@@ -640,7 +651,7 @@ bool Game::checkPlayer(Framework::Text name, Framework::Text secret)
     else
     {
         std::cout << "player " << name.getText()
-                    << " tryed to connect with an invalid secret.\n";
+                  << " tryed to connect with an invalid secret.\n";
         return 0;
     }
 }

+ 18 - 17
FactoryCraft/NetworkMessage.cpp

@@ -155,32 +155,33 @@ void NetworkMessage::sendToAll()
 }
 
 void NetworkMessage::animateBlockBone(int dimensionId,
-    Framework::Vec3<int> position,
+    Framework::Punkt chunkCenter,
+    int index,
     int boneId,
     double time,
     Framework::Vec3<float> pos,
     Framework::Vec3<float> rotation)
 {
     delete[] address;
-    addressLength = 19;
+    addressLength = 15;
     address = new char[addressLength];
     address[0] = 1; // dimension api
     *(int*)(address + 1) = dimensionId;
-    address[5] = 3; // block api
-    *(int*)(address + 6) = position.x;
-    *(int*)(address + 10) = position.y;
-    *(int*)(address + 14) = position.z;
-    address[18] = 3; // animate bone
-    char* msg = new char[36];
-    *(int*)msg = boneId;
-    *(double*)(msg + 4) = boneId;
-    *(float*)(msg + 12) = pos.x;
-    *(float*)(msg + 16) = pos.y;
-    *(float*)(msg + 20) = pos.z;
-    *(float*)(msg + 24) = rotation.x;
-    *(float*)(msg + 28) = rotation.y;
-    *(float*)(msg + 32) = rotation.z;
-    setMessage(msg, 36);
+    address[5] = 1; // chunk api
+    *(int*)(address + 6) = chunkCenter.x;
+    *(int*)(address + 10) = chunkCenter.y;
+    address[14] = 1; // animate block
+    char* msg = new char[40];
+    *(int*)(msg) = index;
+    *(int*)(msg + 4) = boneId;
+    *(double*)(msg + 8) = time;
+    *(float*)(msg + 16) = pos.x;
+    *(float*)(msg + 20) = pos.y;
+    *(float*)(msg + 24) = pos.z;
+    *(float*)(msg + 28) = rotation.x;
+    *(float*)(msg + 32) = rotation.y;
+    *(float*)(msg + 36) = rotation.z;
+    setMessage(msg, 40);
 }
 
 void NetworkMessage::writeTo(Framework::StreamWriter* zWriter) const

+ 3 - 1
FactoryCraft/NetworkMessage.h

@@ -3,6 +3,7 @@
 #include <ReferenceCounter.h>
 #include <Vec3.h>
 #include <Writer.h>
+#include <Punkt.h>
 
 class Chunk;
 class Block;
@@ -41,7 +42,8 @@ public:
     void setUseBackground();
     void sendToAll();
     void animateBlockBone(int dimensionId,
-        Framework::Vec3<int> position,
+        Framework::Punkt chunkCenter,
+        int index,
         int boneId,
         double time,
         Framework::Vec3<float> pos,

+ 3 - 0
FactoryCraft/RecipieLoader.cpp

@@ -37,12 +37,15 @@ void RecipieLoader::loadRecipies(const char* path)
         JSONValidator* validator = zRecipieValidator();
         JSONValue* valid = validator->getValidParts(json);
         json->release();
+        int count = 0; 
         if (valid)
         {
             for (JSONValue* recipie : *valid->asArray())
                 loadRecipie(recipie->asObject());
+            count = valid->asArray()->getLength();
             valid->release();
         }
+        std::cout << count << " recipies were loaded.\n";
     }
 }
 

+ 2 - 2
FactoryCraft/StaticInitializerOrder.cpp

@@ -299,7 +299,7 @@ void initializeBlockTypes()
     (new BasicBlockType(BlockTypeEnum::CHEST,
          ItemTypeEnum::CHEST,
          ModelInfo(
-             "chest", {"blocks.ltdb/chest.png", "blocks.ltdb/chestcover.png"}),
+             "blocks.m3/chest", {"blocks.ltdb/chest.png", "blocks.ltdb/chestcover.png"}),
          [](Framework::Vec3<int> pos) {
              return new Chest(BlockTypeEnum::CHEST, 0, pos);
          },
@@ -621,7 +621,7 @@ void initializeItemTypes()
         0,
         0,
         ModelInfo(
-            "chest", {"blocks.ltdb/chest.png", "blocks.ltdb/chestcover.png"}),
+            "blocks.m3/chest", {"blocks.ltdb/chest.png", "blocks.ltdb/chestcover.png"}),
         BlockTypeEnum::CHEST));
 }
 

+ 76 - 0
Windows Version/data/recipies/blocks.json

@@ -0,0 +1,76 @@
+[
+	{
+		"type": "shaped",
+		"group": "inventory",
+		"width": 3,
+		"height": 3,
+		"inputs": [
+			{
+				"x": 1,
+				"y": 1,
+				"filter": {
+					"itemType": "Flint"
+				}
+			},
+			{
+				"x": 0,
+				"y": 0,
+				"filter": {
+					"itemType": "WoodenStick"
+				}
+			},
+			{
+				"x": 0,
+				"y": 1,
+				"filter": {
+					"itemType": "WoodenStick"
+				}
+			},
+			{
+				"x": 0,
+				"y": 2,
+				"filter": {
+					"itemType": "WoodenStick"
+				}
+			},
+			{
+				"x": 1,
+				"y": 0,
+				"filter": {
+					"itemType": "WoodenStick"
+				}
+			},
+			{
+				"x": 1,
+				"y": 2,
+				"filter": {
+					"itemType": "WoodenStick"
+				}
+			},
+			{
+				"x": 2,
+				"y": 0,
+				"filter": {
+					"itemType": "WoodenStick"
+				}
+			},
+			{
+				"x": 2,
+				"y": 1,
+				"filter": {
+					"itemType": "WoodenStick"
+				}
+			},
+			{
+				"x": 2,
+				"y": 2,
+				"filter": {
+					"itemType": "WoodenStick"
+				}
+			}
+		],
+		"output": {
+			"itemType": "Chest"
+		}
+	}
+]

+ 1 - 1
Windows Version/data/recipies/tools.json

@@ -11,7 +11,7 @@
 				"filter": {
 					"itemType": "Flint"
 				}
-			}
+			},
 			{
 				"x": 1,
 				"y": 0,