summaryrefslogtreecommitdiff
path: root/utils/chunk.js
blob: e84bbd6264cd76e59dd82b25754cd42fca581747 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279

function ChunkUtils() {
    this.chunkList = [];
}

var chunkUtils = new ChunkUtils();

module.exports = chunkUtils;

var fs = require("fs");
var pathUtils = require("path");

var tempResource = require("models/pos");
var Pos = tempResource.Pos;
var createPosFromJson = tempResource.createPosFromJson;

var tempResource = require("models/chunk");
var Chunk = tempResource.Chunk;
var chunkSize = tempResource.chunkSize;
var chunkTileLength = tempResource.chunkTileLength;

var chunksDirectoryPath = "./chunks";
var chunkTileCount = chunkSize * chunkSize
var chunkDataLength = chunkTileCount * chunkTileLength;
var chunkMetadataLength = 1000;
var chunkEntryLength = chunkMetadataLength + chunkDataLength;
var fileChunkSize = 8;
var fileSize = fileChunkSize * chunkSize;

if (!fs.existsSync(chunksDirectoryPath)) {
    fs.mkdirSync(chunksDirectoryPath);
}

ChunkUtils.prototype.convertPosToChunkPos = function(pos) {
    return new Pos(
        Math.floor(pos.x / chunkSize) * chunkSize,
        Math.floor(pos.y / chunkSize) * chunkSize
    );
}

ChunkUtils.prototype.convertPosToFilePos = function(pos) {
    return new Pos(
        Math.floor(pos.x / fileSize) * fileSize,
        Math.floor(pos.y / fileSize) * fileSize
    );
}

ChunkUtils.prototype.getChunkWithoutGenerating = function(pos) {
    var index = 0;
    while (index < this.chunkList.length) {
        var tempChunk = this.chunkList[index];
        if (tempChunk.pos.equals(pos)) {
            return tempChunk;
        }
        index += 1;
    }
    return null;
}

ChunkUtils.prototype.getChunk = function(pos) {
    var output = this.getChunkWithoutGenerating(pos);
    if (output !== null) {
        return output;
    }
    output = this.loadChunk(pos);
    this.chunkList.push(output);
    return output;
}

ChunkUtils.prototype.getChunkCountInFile = function(file) {
    var tempStats = fs.fstatSync(file);
    return tempStats.size / chunkEntryLength;
}

// pos must be aligned to chunk boundaries.
// Output:
// {
//   index: (number)
//   metadata: (object)
// }
ChunkUtils.prototype.findChunkInFile = function(file, pos) {
    var tempCount = this.getChunkCountInFile(file);
    var index = 0;
    while (index < tempCount) {
        var tempBuffer = Buffer.alloc(chunkMetadataLength);
        fs.readSync(file, tempBuffer, 0, chunkMetadataLength, index * chunkEntryLength);
        var tempJson = JSON.parse(tempBuffer.toString("utf8"));
        var tempPos = createPosFromJson(tempJson.pos);
        if (tempPos.equals(pos)) {
            return {
                index: index,
                metadata: tempJson
            }
        }
        index += 1;
    }
    return {
        index: -1,
        metadata: null
    }
}

ChunkUtils.prototype.createEmptyChunk = function(pos) {
    var tempBuffer = Buffer.alloc(chunkDataLength);
    return new Chunk(pos.copy(), tempBuffer);
}

ChunkUtils.prototype.getFile = function(pos) {
    var tempPos = this.convertPosToFilePos(pos);
    var tempName = tempPos.x + "_" + tempPos.y + ".dat";
    var tempPath = pathUtils.join(chunksDirectoryPath, tempName);
    if (!fs.existsSync(tempPath)) {
        var tempFile = fs.openSync(tempPath, "w");
        fs.closeSync(tempFile);
    }
    return fs.openSync(tempPath, "r+");
}

// pos must be aligned to chunk boundaries.
ChunkUtils.prototype.loadChunk = function(pos) {
    var tempFile = this.getFile(pos);
    var tempResult = this.findChunkInFile(tempFile, pos);
    var output;
    if (tempResult.index >= 0) {
        var tempBuffer = Buffer.alloc(chunkDataLength);
        fs.readSync(tempFile, tempBuffer, 0, chunkDataLength, tempResult.index * chunkEntryLength + chunkMetadataLength);
        output = new Chunk(pos.copy(), tempBuffer);
    } else {
        output = this.createEmptyChunk(pos);
    }
    fs.closeSync(tempFile);
    return output;
}

ChunkUtils.prototype.getTiles = function(pos, size) {
    var output = [];
    var tempPos = new Pos(0, 0);
    var tempOffset = new Pos(0, 0);
    while (tempOffset.y < size) {
        tempPos.set(pos);
        tempPos.add(tempOffset);
        var tempTile = this.getTile(tempPos);
        output.push(tempTile);
        tempOffset.x += 1;
        if (tempOffset.x >= size) {
            tempOffset.x = 0;
            tempOffset.y += 1;
        }
    }
    return output;
}

ChunkUtils.prototype.getTileWithoutGenerating = function(pos) {
    var tempPos = this.convertPosToChunkPos(pos);
    var tempChunk = this.getChunkWithoutGenerating(tempPos);
    if (tempChunk === null) {
        return 0;
    }
    return tempChunk.getTileWithoutGenerating(pos);
}

ChunkUtils.prototype.getTile = function(pos) {
    var tempPos = this.convertPosToChunkPos(pos);
    var tempChunk = this.getChunk(tempPos);
    return tempChunk.getTile(pos);
}

ChunkUtils.prototype.setTile = function(pos, value) {
    var tempPos = this.convertPosToChunkPos(pos);
    var tempChunk = this.getChunk(tempPos);
    tempChunk.setTile(pos, value);
}

ChunkUtils.prototype.getHeightMapValue = function(pos, offset) {
    var tempPos = this.convertPosToChunkPos(pos);
    var tempChunk = this.getChunk(tempPos);
    return tempChunk.getHeightMapValue(pos, offset);
}

ChunkUtils.prototype.setHeightMapValue = function(pos, offset, value) {
    var tempPos = this.convertPosToChunkPos(pos);
    var tempChunk = this.getChunk(tempPos);
    tempChunk.setHeightMapValue(pos, offset, value);
}


ChunkUtils.prototype.createEntryMetadataBuffer = function(metadata) {
    var tempText = JSON.stringify(metadata);
    var tempBuffer = Buffer.from(tempText, "utf8");
    var output = Buffer.alloc(chunkMetadataLength, 32);
    tempBuffer.copy(output);
    return output;
}

ChunkUtils.prototype.persistChunk = function(chunk) {
    var tempFile = this.getFile(chunk.pos);
    var tempResult = this.findChunkInFile(tempFile, chunk.pos);
    var index;
    if (tempResult.index >= 0) {
        index = tempResult.index;
    } else {
        index = this.getChunkCountInFile(tempFile);
    }
    var tempMetadata = {
        pos: chunk.pos.toJson()
    };
    var tempBuffer = this.createEntryMetadataBuffer(tempMetadata);
    var tempPosition = index * chunkEntryLength;
    fs.writeSync(tempFile, tempBuffer, 0, chunkMetadataLength, tempPosition);
    fs.writeSync(tempFile, chunk.data, 0, chunkDataLength, tempPosition + chunkMetadataLength);
    fs.closeSync(tempFile);
    chunk.isDirty = false;
}

ChunkUtils.prototype.persistAllChunks = function() {
    var index = 0;
    while (index < this.chunkList.length) {
        var tempChunk = this.chunkList[index];
        if (tempChunk.isDirty) {
            this.persistChunk(tempChunk);
        }
        index += 1;
    }
}

ChunkUtils.prototype.removeChunk = function(chunk) {
    chunk.removeEvent();
    var index = this.chunkList.indexOf(chunk);
    if (index >= 0) {
        this.chunkList.splice(index, 1);
    }
    this.persistChunk(chunk);
}

ChunkUtils.prototype.removeFarChunks = function(posList, maximumDistance) {
    var tempChunkListToRemove = this.chunkList.slice();
    var index = 0;
    while (index < posList.length) {
        var tempPos = posList[index];
        var tempIndex = tempChunkListToRemove.length - 1;
        while (tempIndex >= 0) {
            var tempChunk = tempChunkListToRemove[tempIndex];
            var tempDistance = tempChunk.getOrthogonalDistance(tempPos);
            if (tempDistance <= maximumDistance) {
                tempChunkListToRemove.splice(tempIndex, 1);
            }
            tempIndex -= 1;
        }
        index += 1;
    }
    var index = 0;
    while (index < tempChunkListToRemove.length) {
        var tempChunk = tempChunkListToRemove[index];
        this.removeChunk(tempChunk);
        index += 1;
    }
}

ChunkUtils.prototype.getGeneratedChunkList = function() {
    var output = [];
    var index = 0;
    while (index < this.chunkList.length) {
        var tempChunk = this.chunkList[index];
        if (tempChunk.hasGeneratedTiles()) {
            output.push(tempChunk);
        }
        index += 1;
    }
    return output;
}

ChunkUtils.prototype.posIsInRestZone = function(pos) {
    var tempPos = this.convertPosToChunkPos(pos);
    var tempChunk = this.getChunkWithoutGenerating(tempPos);
    if (tempChunk === null) {
        return false;
    }
    return tempChunk.posIsInRestZone(pos);
}