summaryrefslogtreecommitdiff
path: root/src/world.cpp
diff options
context:
space:
mode:
authorPaweł Redman <pawel.redman@gmail.com>2017-10-07 08:03:44 +0200
committerPaweł Redman <pawel.redman@gmail.com>2017-10-07 08:03:44 +0200
commit9e7d64d052eabcf40d85c3e903aaba44903a380a (patch)
tree0a3a2c7f53884bd035b54812a77d493be058f43c /src/world.cpp
Initial commit.
Diffstat (limited to 'src/world.cpp')
-rw-r--r--src/world.cpp86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/world.cpp b/src/world.cpp
new file mode 100644
index 0000000..b5aeea2
--- /dev/null
+++ b/src/world.cpp
@@ -0,0 +1,86 @@
+#include "common.hpp"
+
+namespace world {
+
+sector_index_t::sector_index_t ()
+{
+}
+
+sector_index_t::sector_index_t (int64_t x_, int64_t y_)
+{
+ x = x_;
+ y = y_;
+}
+
+bool sector_index_t::operator<(sector_index_t B) const
+{
+ if (x < B.x)
+ return true;
+ if (x > B.x)
+ return false;
+ if (y < B.y)
+ return true;
+ return false;
+}
+
+void sector_t::generate(sector_index_t index)
+{
+ std::cout << "generating (" << index.x << ", " << index.y << ")\n";
+
+ for (ssize_t ly = 0; ly < SECTOR_SIZE; ly++)
+ for (ssize_t lx = 0; lx < SECTOR_SIZE; lx++) {
+ ssize_t x, y;
+
+ x = index.x * SECTOR_SIZE + lx;
+ y = index.y * SECTOR_SIZE + ly;
+
+ tiles[ly * SECTOR_SIZE + lx].type = (x ^ y) * 151;
+ }
+
+ empty = false;
+}
+
+sector_t *world_t::get_sector(sector_index_t index)
+{
+ sector_t *sector;
+
+ sector = &sectors[index];
+
+ if (sector->empty)
+ sector->generate(index);
+
+ return sector;
+}
+
+// divide and round to minus infinity
+static ssize_t divide_rmi(ssize_t x, ssize_t y, size_t *rem)
+{
+ ssize_t rv;
+
+ if (x >= 0) {
+ *rem = x % y;
+ return x / y;
+ }
+
+ rv = (x + 1) / y - 1;
+ *rem = x - rv * y;
+ return rv;
+}
+
+tile_t *world_t::get_tile(ssize_t x, ssize_t y)
+{
+ sector_index_t index;
+ sector_t *sector;
+ size_t tx, ty;
+
+ index.x = divide_rmi(x, SECTOR_SIZE, &tx);
+ index.y = divide_rmi(y, SECTOR_SIZE, &ty);
+ printf("get_tile(%zd, %zd)\n", x, y);
+ printf("\tsector is (%zd, %zd)\n", index.x, index.y);
+ sector = get_sector(index);
+
+ printf("\ttile is (%zd, %zd)\n", tx, ty);
+ return sector->tiles + ty * SECTOR_SIZE + tx;
+}
+
+}