aboutsummaryrefslogtreecommitdiff
path: root/source/spots.d
blob: 8ee035fa7c1e18807aef5e6763907a26e73b3e90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import std.bigint;
import std.digest.crc;
import std.random : unpredictableSeed;
import std.range;
import std.stdio;
import lru : LRUCache;
import std.traits : isSomeString;

private BigInt positive(BigInt n) {
    if (n >= 0)
        return n * 2;
    else
        return -n * 2 - 1;
}

private BigInt pair(BigInt n, BigInt m) {
    // https://stackoverflow.com/a/919661
    return (positive(n) + positive(m)) * (positive(n) + positive(m) + 1) / 2 + positive(m);
}

struct Spot {
    private char _contents;
    private int _id;
    this(char c) {
        _contents = c;
    }

    this(char c, int id) {
        this(c);
        _id = id;
    }

    import std.format : format, formattedRead;

    string toString() {

        return format!`'%c' %d`(_contents, _id);
    }

    void fromString(Range)(auto ref Range s) {
        s.formattedRead!`'%c' %d`(_contents, _id);
    }

    @property contents() {
        return _contents;
    }

    @property id() {
        return _id;
    }

    bool opEqual(const Spot s) {
        return s._contents == _contents;
    }

    bool isSpace() {
        return _contents == ' ';
    }

    bool isRock() {
        return _contents == '*';
    }

    bool isPortal() {
        return _contents == '/';
    }
}

interface SpotSource {
    Spot opIndex(BigInt y, BigInt x);
    void save(File);
    void load(File);
}

class RandomSource : SpotSource {
    uint seed;
    double density = .00025;
    private LRUCache!(BigInt[2], Spot) cache;

    this() {
        seed = unpredictableSeed;
        cache = new LRUCache!(BigInt[2], Spot)(50000);
    }

    this(uint seed) {
        this.seed = seed;
    }

    this(uint seed, double density) {
        this.density = density;
        this(seed);
    }

    private uint hash(BigInt n) {
        CRC32 crc;
        crc.start();
        crc.put((cast(ubyte*)&seed)[0 .. seed.sizeof]);
        foreach (i; iota(n.ulongLength)) {
            auto digit = n.getDigit(i);
            crc.put((cast(ubyte*)&digit)[0 .. digit.sizeof]);
        }
        auto result = crc.finish();
        auto hash = *(cast(uint*)&result);
        return hash;
    }

    public Spot opIndex(BigInt y, BigInt x) {
        if ([y, x] in cache)
            return cache[[y, x]];
        auto roll = cast(double) hash(pair(y, x)) / uint.max;
        Spot ret;
        if (roll < density)
            ret = Spot('*');
        else
            ret = Spot(' ');
        cache[[y, x]] = ret;
        return ret;
    }

    public void save(File f) {
        f.writefln!"%d %f"(seed, density);
    }

    public void load(File f) {
        f.readf!"%d %f\n"(seed, density);
        cache = new LRUCache!(BigInt[2], Spot)(50000);
    }
}

class OverlaySource : SpotSource {
    private SpotSource _base;
    private Spot[BigInt[2]] _overlay;
    immutable private string terminator = "END OVERLAY\n";

    this(SpotSource base) {
        _base = base;
    }

    void opIndexAssign(Spot spot, BigInt y, BigInt x) {
        Spot* p = [y, x] in _overlay;
        if (p !is null && spot == _base[y, x]) {
            _overlay.remove([y, x]);
        } else {
            _overlay[[y, x]] = spot;
        }
    }

    Spot opIndex(BigInt y, BigInt x) {
        Spot* p = [y, x] in _overlay;
        if (p !is null)
            return *p;
        else
            return _base[y, x];
    }

    void save(File f) {
        _base.save(f);
        foreach (coord; _overlay.keys()) {
            f.writefln!"%s %s: %s"(coord[0], coord[1], _overlay[coord]);
        }
        f.write(terminator);
    }

    void load(File f) {
        import std.conv : to;
        import std.algorithm;

        //_overlay = _overlay.init;
        _base.load(f);
        foreach (char[] line; f.lines) {
            if (line == terminator)
                break;

            auto ints = line.until(':').array.splitter(' ').map!(to!BigInt);
            BigInt[2] coord;
            coord[0] = ints.front;
            ints.popFront;
            coord[1] = ints.front;

            auto s = Spot();
            s.fromString(line.find(':').find('\''));
            _overlay[coord] = s;
        }
    }

    unittest {
        auto base = new RandomSource(0, 0.0);
        auto src = new OverlaySource(base);
        src[BigInt(0), BigInt(0)] = Spot('*');
        src[BigInt(0), BigInt(1)] = Spot('*');
        assert(src[BigInt(0), BigInt(0)] == Spot('*'));
        assert(src[BigInt(0), BigInt(1)] == Spot('*'));
    }
}