summaryrefslogtreecommitdiff
path: root/src/world.cpp
diff options
context:
space:
mode:
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;
+}
+
+}