Browse Source

removed experimental highmap, world cleanup

Kajetan Johannes Hammerle 2 years ago
parent
commit
c13f675c2b

+ 2 - 2
client/packets/WorldPackets.cpp

@@ -13,14 +13,14 @@ void WorldPackets::receiveChunk(World& w, InPacket& in) {
     }
     uint16 counter = 0;
     Block::Id b = 0;
-    for(int y = 0; y < w.getHeight(); y++) {
+    for(int y = 0; y < w.blocks.getHeight(); y++) {
         for(int x = 0; x < BlockStorage::SEGMENT; x++) {
             for(int z = 0; z < BlockStorage::SEGMENT; z++) {
                 if(counter == 0 && (in.readU16(b) || in.readU16(counter))) {
                     LOG_WARNING("missing block data in packet");
                     return;
                 }
-                w.setBlock(ox + x, y, oz + z, b);
+                w.blocks.set(ox + x, y, oz + z, b);
                 counter--;
             }
         }

+ 7 - 6
client/rendering/renderer/WorldRenderer.cpp

@@ -17,10 +17,10 @@ bool WorldRenderer::init() {
 void WorldRenderer::render() {
     if(world.dirty) {
         TypedBuffer<Triangle> buffer(100);
-        for(int x = 0; x < world.getSize(); x++) {
-            for(int y = 0; y < world.getHeight(); y++) {
-                for(int z = 0; z < world.getSize(); z++) {
-                    if(world.getBlock(x, y, z) != 0) {
+        for(int x = 0; x < world.blocks.getSize(); x++) {
+            for(int y = 0; y < world.blocks.getHeight(); y++) {
+                for(int z = 0; z < world.blocks.getSize(); z++) {
+                    if(world.blocks.get(x, y, z) != 0) {
                         addCube(buffer, x, y, z);
                     }
                 }
@@ -34,7 +34,8 @@ void WorldRenderer::render() {
     for(int x = -1; x <= 1; x++) {
         for(int z = -1; z <= 1; z++) {
             Engine::matrix
-                .translateTo(world.getSize() * x, 0.0f, world.getSize() * z)
+                .translateTo(world.blocks.getSize() * x, 0.0f,
+                             world.blocks.getSize() * z)
                 .update();
             mesh.draw();
         }
@@ -42,7 +43,7 @@ void WorldRenderer::render() {
 }
 
 bool WorldRenderer::isAir(int x, int y, int z) const {
-    return world.getBlock(x, y, z) == 0;
+    return world.blocks.get(x, y, z) == 0;
 }
 
 void WorldRenderer::addCube(TypedBuffer<Triangle>& buffer, float x, float y,

+ 0 - 84
common/world/HighMap.cpp

@@ -1,84 +0,0 @@
-#include "common/world/HighMap.h"
-#include "utils/Random.h"
-
-static constexpr int permutation[] = {
-    151, 160, 137, 91,  90,  15,  131, 13,  201, 95,  96,  53,  194, 233, 7,
-    225, 140, 36,  103, 30,  69,  142, 8,   99,  37,  240, 21,  10,  23,  190,
-    6,   148, 247, 120, 234, 75,  0,   26,  197, 62,  94,  252, 219, 203, 117,
-    35,  11,  32,  57,  177, 33,  88,  237, 149, 56,  87,  174, 20,  125, 136,
-    171, 168, 68,  175, 74,  165, 71,  134, 139, 48,  27,  166, 77,  146, 158,
-    231, 83,  111, 229, 122, 60,  211, 133, 230, 220, 105, 92,  41,  55,  46,
-    245, 40,  244, 102, 143, 54,  65,  25,  63,  161, 1,   216, 80,  73,  209,
-    76,  132, 187, 208, 89,  18,  169, 200, 196, 135, 130, 116, 188, 159, 86,
-    164, 100, 109, 198, 173, 186, 3,   64,  52,  217, 226, 250, 124, 123, 5,
-    202, 38,  147, 118, 126, 255, 82,  85,  212, 207, 206, 59,  227, 47,  16,
-    58,  17,  182, 189, 28,  42,  223, 183, 170, 213, 119, 248, 152, 2,   44,
-    154, 163, 70,  221, 153, 101, 155, 167, 43,  172, 9,   129, 22,  39,  253,
-    19,  98,  108, 110, 79,  113, 224, 232, 178, 185, 112, 104, 218, 246, 97,
-    228, 251, 34,  242, 193, 238, 210, 144, 12,  191, 179, 162, 241, 81,  51,
-    145, 235, 249, 14,  239, 107, 49,  192, 214, 31,  181, 199, 106, 157, 184,
-    84,  204, 176, 115, 121, 50,  45,  127, 4,   150, 254, 138, 236, 205, 93,
-    222, 114, 67,  29,  24,  72,  243, 141, 128, 195, 78,  66,  215, 61,  156,
-    180};
-
-static int p[512];
-
-static double fade(double t) {
-    return t * t * t * (t * (t * 6 - 15) + 10);
-}
-
-static double lerp(double t, double a, double b) {
-    return a + t * (b - a);
-}
-
-static double grad(int hash, double x, double y) {
-    int h = hash & 15;
-    double u = h < 8 ? x : y;
-    double v = h < 4 ? y : (h == 12 || h == 14 ? x : 0);
-    return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v);
-}
-
-HighMap::HighMap(int size, int height) : size(size), height(height) {
-    for(int i = 0; i < 256; i++)
-        p[256 + i] = p[i] = permutation[i];
-}
-
-int HighMap::getHeight(int x2, int y2) const {
-    x2 &= size - 1;
-    y2 &= size - 1;
-    float x = x2 / 64.0f;
-    float y = y2 / 64.0f;
-    int X = ((int)x) & 255;
-    int Y = ((int)y) & 255;
-    x -= (int)(x);
-    y -= (int)(y);
-    double u = fade(x);
-    double v = fade(y);
-    int A = p[X] + Y;
-    int AA = p[A];
-    int AB = p[A + 1];
-    int B = p[X + 1] + Y;
-    int BA = p[B];
-    int BB = p[B + 1];
-
-    double d = lerp(v, lerp(u, grad(p[AA], x, y), grad(p[BA], x - 1, y)),
-                    lerp(u, grad(p[AB], x, y - 1), grad(p[BB], x - 1, y - 1)));
-    d *= d;
-    return static_cast<unsigned int>(d * height + 1.0) % height;
-}
-
-unsigned int pot(unsigned int a, unsigned int n) {
-    unsigned int sum = 0;
-    while(n > 0) {
-        if(n & 1) {
-            sum += a;
-        }
-        a *= a;
-        n >>= 1;
-    }
-    return sum;
-}
-
-int HighMap::random(int x, int y) const {
-    return (pot(328249, x) + pot(894503, y)) % height;
-}

+ 0 - 19
common/world/HighMap.h

@@ -1,19 +0,0 @@
-#ifndef HIGHMAP_H
-#define HIGHMAP_H
-
-#include "math/Vector.h"
-
-class HighMap {
-    int size;
-    int height;
-
-public:
-    HighMap(int size, int height);
-
-    int getHeight(int x, int y) const;
-
-private:
-    int random(int x, int y) const;
-};
-
-#endif

+ 1 - 18
common/world/World.cpp

@@ -1,6 +1,5 @@
 #include <cmath>
 
-#include "common/world/HighMap.h"
 #include "common/world/World.h"
 #include "utils/Logger.h"
 #include "utils/Random.h"
@@ -8,22 +7,6 @@
 World::World() : blocks(7, 7), dirty(true) {
 }
 
-void World::setBlock(int x, int y, int z, Block::Id block) {
-    blocks.set(x, y, z, block);
-}
-
-Block::Id World::getBlock(int x, int y, int z) const {
-    return blocks.get(x, y, z);
-}
-
-int World::getSize() const {
-    return blocks.getSize();
-}
-
-int World::getHeight() const {
-    return blocks.getHeight();
-}
-
 void World::addEntity(Entity* e) {
     entities.add(e);
 }
@@ -49,7 +32,7 @@ List<Box> World::getBoxes(const Box& box) const {
     for(int x = minX; x <= maxX; x++) {
         for(int y = minY; y <= maxY; y++) {
             for(int z = minZ; z <= maxZ; z++) {
-                Block::addBoxes(getBlock(x, y, z), boxes,
+                Block::addBoxes(blocks.get(x, y, z), boxes,
                                 Vector3(static_cast<float>(x),
                                         static_cast<float>(y),
                                         static_cast<float>(z)));

+ 1 - 7
common/world/World.h

@@ -6,20 +6,14 @@
 #include "utils/List.h"
 
 class World final {
-    BlockStorage blocks;
     List<Entity*> entities;
 
 public:
+    BlockStorage blocks;
     mutable bool dirty;
 
     World();
 
-    void setBlock(int x, int y, int z, Block::Id block);
-    Block::Id getBlock(int x, int y, int z) const;
-
-    int getSize() const;
-    int getHeight() const;
-
     void addEntity(Entity* e);
     void removeEntity(Entity* e);
 

+ 0 - 1
meson.build

@@ -4,7 +4,6 @@ src_common = [
     'common/Block.cpp', 
     'common/world/BlockStorage.cpp',
     'common/world/World.cpp', 
-    'common/world/HighMap.cpp',
     'common/Box.cpp',
     'common/entities/Entity.cpp',
     'common/network/toserver/PlayerUpdatePacket.cpp',

+ 1 - 1
server/Game.cpp

@@ -53,7 +53,7 @@ void Game::addPlayer(ServerPlayer& p) {
             WorldPackets::sendChunk(p, world, x, z);
         }
     }
-    p.setPosition(Vector3(0.0f, 1.0f, 0.0f));
+    p.setPosition(Vector3(0.0f, 32.0f, 0.0f));
     world.addEntity(&p);
 }
 

+ 4 - 4
server/packets/WorldPackets.cpp

@@ -6,20 +6,20 @@ void WorldPackets::sendChunk(ServerPlayer& p, World& w, int cx, int cz) {
     cx *= size;
     cz *= size;
     OutPacket out = OutPacket::reliable(
-        w.getHeight() * size * size * sizeof(Block::Id) * 2 + 10);
+        w.blocks.getHeight() * size * size * sizeof(Block::Id) * 2 + 10);
     out.writeU16(Packets::S_WORLD_SEGMENT);
     out.writeS32(cx);
     out.writeS32(cz);
     int endX = cx + size;
     int endZ = cz + size;
 
-    Block::Id current = w.getBlock(cx, 0, cz);
+    Block::Id current = w.blocks.get(cx, 0, cz);
     uint16 counter = 0;
 
-    for(int y = 0; y < w.getHeight(); y++) {
+    for(int y = 0; y < w.blocks.getHeight(); y++) {
         for(int x = cx; x < endX; x++) {
             for(int z = cz; z < endZ; z++) {
-                Block::Id id = w.getBlock(x, y, z);
+                Block::Id id = w.blocks.get(x, y, z);
                 if(current == id && counter < 65535) {
                     counter++;
                 } else {

+ 6 - 6
server/world/WorldGenerator.cpp

@@ -1,13 +1,13 @@
+#include <cmath>
+
 #include "server/world/WorldGenerator.h"
-#include "common/world/HighMap.h"
 
 void WorldGenerator::generate(World& w) {
-    HighMap map(w.getSize(), w.getHeight());
-    for(int x = 0; x < w.getSize(); x++) {
-        for(int z = 0; z < w.getSize(); z++) {
-            int height = map.getHeight(x, z);
+    for(int x = 0; x < w.blocks.getSize(); x++) {
+        for(int z = 0; z < w.blocks.getSize(); z++) {
+            int height = sinf(x * 0.25f) * 2.0f + cosf(z * 0.25f) * 2.0f + 6.0f;
             for(int y = 0; y < height; y++) {
-                w.setBlock(x, y, z, 1);
+                w.blocks.set(x, y, z, 1);
             }
         }
     }