-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialization.js
More file actions
81 lines (76 loc) · 2.14 KB
/
serialization.js
File metadata and controls
81 lines (76 loc) · 2.14 KB
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
'use strict';
/**
* serializes an game object into a string.
* Unsupported objects: Global objects, RoomVisual, Room.Terrain, Store, StructureSpawn.spawning
* @param obj {Object}
* @return {string | undefined}
*/
global.serialize = function(obj){
if(obj === undefined) return;
function f2(num){
if(num < 10){
return "0".concat(num.toString());
}else{
return num.toString();
}
}
if(obj instanceof Room || obj instanceof Flag){
return obj.name;
}else if(obj instanceof RoomPosition){
return `${f2(obj.x)}${f2(obj.y)}${obj.roomName}`;
}else if(obj instanceof PathFinder.CostMatrix) {
return serializeMatrix(obj);
}else{
return obj.id;
}
};
/**
* @param s {string} the serialized string
* @param type {function} type of the original object
* @return {object} the deserialized object
*/
global.deserialize = function(s, type){
if(s == null){
return undefined;
}
switch (type) {
case StructureSpawn: return Game.spawns[s];
case Room: return Game.rooms[s];
case RoomPosition: return new RoomPosition(
parseInt(s.substr(0,2)),
parseInt(s.substr(2,2)), s.substr(4));
default: return Game.getObjectById(s);
}
};
global.getPosObjPair = function (obj) {
if(obj == null) return;
return [serialize(obj.pos), serialize(obj)];
};
const minChar = 32;
const maxChar = 126;
const chars = maxChar - minChar + 1;
function serializeMatrix(costMatrix){
//matrix of 50 * 50 uint8
let str = '';
function convertToStr(n) {
let s = [];
for(let i = 0; i < 5; i++){
s[4 - i] = String.fromCharCode(n % chars);
n = Math.floor(n / chars);
}
return s.join('');
}
for(let index = 0; index < 2500; index += 4){
let n = 0;
for(let i = 0; i < 4; i++){
let x = (index + i) % 50;
let y = Math.floor((index + i) / 50);
n += costMatrix.get(x, y);
if(i !== 3){
n = n << 8 >>> 0;
}
}
str += convertToStr(n);
}
return str;
}