forked from screepers/screeps-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorldPosition uniform global coordinate system.js
203 lines (174 loc) · 4.97 KB
/
WorldPosition uniform global coordinate system.js
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
// warinternal 24 November 2016 at 04:18
/**
* Uniform screep's world position with E0S0 as origin.
*/
'use strict';
class WorldPosition
{
/** @property int x */
/** @property int y */
/**
* @params {Object} point
* @params {number} point.x - world position x (-3025 to 3025)
* @params {number} point.y - world position y (-3025 to 3025)
*/
constructor(x,y) {
this.x = x;
this.y = y;
Object.seal(this);
}
/**
* @params {Object} point
* @params {number} point.x
* @params {number} point.y
*/
getRangeTo(point) {
return this.getRangeToXY(point.x, point.y);
}
/**
* @params {number} x
* @params {number} y
*/
getRangeToXY(x,y) {
return this.getChebyshevDist(x,y);
}
inRangeTo(point, range) {
return this.inRangeToXY(point.x, point.y, range);
}
inRangeToXY(x,y,range) {
return (this.getRangeToXY(x,y) <= range);
}
getDirectionTo(point) {
return this.getDirectionToXY(point.x, point.y);
}
/**
* @params {number} x - world coordinate x
* @params {number} y - world coordinate y
* ..don't question it. don't even think about it.
*/
getDirectionToXY(x,y) {
let [dx,dy] = [x - this.x, y - this.y];
let arc = Math.atan2(dy, dx) * (180 / Math.PI);
let dir = Math.round((arc / 45) + 3);
return (dir == 0)?8:dir;
}
findRouteToWorldPosition(pos, opts) {
return Game.map.findRoute(this.getRoomName(), pos.getRoomName(), opts);
}
findPathToWorldPosition(pos, opts) {
let src = this.toRoomPosition();
let dst = pos.toRoomPosition();
return PathFinder.search(src, dst, opts);
}
/**
* @params [WorldPosition] - array of other world positions to compare
*/
findClosestByRange(arr) {
return _.min(arr, p => this.getRangeTo(p.wpos));
}
/** @returns String - name of the room this point belongs to */
getRoomName() {
let [x,y] = [Math.floor(this.x / 50), Math.floor(this.y / 50)]
var result = "";
result += (x < 0 ? "W" + String(~x) : "E" + String(x));
result += (y < 0 ? "N" + String(~y) : "S" + String(y));
return result;
}
/** @returns boolean - do we have visibility in the room this point belongs to? */
isVisible() {
let name = this.getRoomName();
return (Game.rooms[name] !== undefined);
}
/** @returns boolean - is this room part of the highways between sectors? */
isHighway() {
let roomName = this.getRoomName();
let parsed = roomName.match(/^[WE]([0-9]+)[NS]([0-9]+)$/);
return (parsed[1] % 10 === 0) || (parsed[2] % 10 === 0);
}
/** @returns boolean - do I own this point in space? */
isMine() {
let roomName = this.getRoomName();
return _.get(Game.rooms, roomName + '.controller.my', false);
}
/** Distance functions */
/**
* @params {Object} point
* @params {number} point.x
* @params {number} point.y
*/
getEuclidDist(pos) {
return Math.hypot( pos.x - this.x, pos.y - this.y );
}
/**
* @params {Object} point
* @params {number} point.x
* @params {number} point.y
*/
getManhattanDist(pos) {
return Math.abs(pos.x - this.x) + Math.abs(pos.y - this.y);
}
// yeah. and with that, it'll give you the correct distance of diagonals, whereas manhattan won't consider that.
/**
* @params {Object} point
* @params {number} point.x
* @params {number} point.y
*/
getChebyshevDist(x,y) {
return Math.max( Math.abs((x-this.x)), Math.abs((y-this.y)) );
}
/** serialization */
serialize() {
return this.x + "_" + this.y;
}
static deserialize(str) {
let [x,y] = str.split('_');
return new WorldPosition(x,y);
}
/** [object WorldPosition] */
get [Symbol.toStringTag]() {
return 'WorldPosition';
}
/**
* @params {RoomPosition} roomPos
* @params {number} roomPos.x
* @params {number} roomPos.y
* @params {String} roomPos.roomName
* @returns {WorldPosition}
*/
static fromRoomPosition(roomPos) {
let {x,y,roomName} = roomPos;
if(!_.inRange(x, 0, 50)) throw new RangeError('x value ' + x + ' not in range');
if(!_.inRange(y, 0, 50)) throw new RangeError('y value ' + y + ' not in range');
if(roomName == 'sim') throw new RangeError('Sim room does not have world position');
let [name,h,wx,v,wy] = roomName.match(/^([WE])([0-9]+)([NS])([0-9]+)$/);
if(h == 'W') wx = ~wx;
if(v == 'N') wy = ~wy;
return new WorldPosition( (50*wx)+x, (50*wy)+y );
}
toRoomPosition() {
let [rx,x] = [Math.floor(this.x / 50), this.x % 50];
let [ry,y] = [Math.floor(this.y / 50), this.y % 50];
if( rx < 0 && x < 0 ) x = (49 - ~x);
if( ry < 0 && y < 0 ) y = (49 - ~y);
return new RoomPosition(x,y,this.getRoomName());
}
/** [world pos 1275,1275] */
toString() {
return "[world pos " + this.x + "," + this.y + "]";
}
}
Object.defineProperty(RoomObject.prototype, "wpos", {
get: function () {
if(!this._wpos)
this._wpos = WorldPosition.fromRoomPosition(this.pos);
return this._wpos;
},
configurable: true,
enumerable: false
});
RoomPosition.prototype.toWorldPosition = function() {
if(!this._wpos)
this._wpos = WorldPosition.fromRoomPosition(this);
return this._wpos;
}
module.exports = WorldPosition;