Name the tiles to make everything better because I'm a bloody idiot
[SmugglerRL.git] / src / map.d
blobc8ec320df6ef86bbc0eed549a38a6977f94a442d
1 import constants;
2 import colour;
3 import glyph;
4 import logging;
5 import tile;
7 // (n)ormalized
8 struct Vector2n {
9 private immutable maxx = map_x, maxy = map_y;
12 private struct Normalint(uint max) {
13 private int _i;
15 // No reason you would want to negate it, deref it, or invert it; unary + literally does nothing,
16 uint opUnary(string op)() if (op == "++" || op = "--") {
17 mixin(op ~ "_i");
18 return _i = normalized(_i, max);
20 uint opBinary(string op)(int other) {
21 return normalized(mixin("_i" ~ op ~ "other"), max);
24 @property uint i() {
25 // we don't have to normalize i. Because assignment and inc/dec already normalize it
26 return _i;
28 @property uint i(int newi) {
29 return _i = normalized(newi, max);
31 alias i this;
33 Normalint!maxx x;
34 Normalint!maxy y;
36 this(uint newy, uint newx) {
37 y.i = newy;
38 x.i = newx;
41 pragma(inline, true) uint normalized(int i, int max) {
42 i %= max;
43 if (i < 0) {
44 i += max;
47 return i;
48 // If this function is a perf problem, use the following to trigger a cmov (compiler should be smart enough to do it that way already though)
50 * int oth = i + max;
51 * i = (i < 0) ? i : oth;
55 class Map {
56 private Tile[][] tiles;
58 this(Tile[][] tiles) {
59 this.tiles = tiles;
61 this() {
62 tiles = new Tile[][](map_x, map_y);
65 Tile[][] get_tiles() {
66 return tiles;
69 // lambda of the form, e.g., (Tile x) => x.visible = false will return bool, not void
70 // so if we take a void function, all is lost. So take a function returning T
71 void for_all(T)(T delegate(ref Tile x) fun) {
72 foreach (ref row; tiles) {
73 foreach (ref tile; row) {
74 fun(tile);
79 //ref Tile opIndexAssign(Tile value, ulong y, ulong x) { return tiles[y][x] = value; }
80 ref Tile opIndex(uint y, uint x) {
81 return tiles[y % map_y][x % map_x];
83 ref Tile opIndex(Vector2n vec) {
84 return tiles[vec.y][vec.x];
86 ref Tile opIndex(int y, int x) {
87 return this[Vector2n(y, x)];
90 Map dup() {
91 Map ret = new Map();
92 ret.tiles = tiles.dup;
93 return ret;