screeps-clockwork 0.1.1 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +97 -4
- package/dist/index.js +270 -91
- package/dist/index.js.map +1 -1
- package/dist/screeps_clockwork.wasm +0 -0
- package/dist/src/index.d.ts +11 -1
- package/dist/src/wasm/screeps_clockwork.d.ts +380 -0
- package/dist/src/wrappers/bfsDistanceMap.d.ts +20 -2
- package/dist/src/wrappers/bfsFlowField.d.ts +36 -3
- package/dist/src/wrappers/dijkstraDistanceMap.d.ts +18 -0
- package/dist/src/wrappers/dijkstraFlowField.d.ts +32 -0
- package/dist/src/wrappers/getRange.d.ts +9 -0
- package/package.json +62 -60
package/README.md
CHANGED
@@ -1,7 +1,100 @@
|
|
1
1
|
# Screeps Clockwork
|
2
2
|
|
3
|
-
|
3
|
+
<div style="text-align: center;"><a href="https://glitchassassin.github.io/screeps-clockwork/">Understand the Theory</a> | <a href="https://glitchassassin.github.io/screeps-clockwork/api/">API Docs</a></div>
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
This guide will assume you're using Rollup along the lines of the Screeps Typescript starter kit.
|
8
|
+
|
9
|
+
Set up dependencies:
|
10
|
+
|
11
|
+
```
|
12
|
+
npm install screeps-clockwork
|
13
|
+
npm install -D @rollup/plugin-wasm rollup-plugin-copy
|
14
|
+
```
|
15
|
+
|
16
|
+
Update your Rollup build script to copy the WASM binary to your dist folder:
|
17
|
+
|
18
|
+
```js
|
19
|
+
// add to existing dependencies
|
20
|
+
import wasm from '@rollup/plugin-wasm';
|
21
|
+
import copy from 'rollup-plugin-copy';
|
22
|
+
|
23
|
+
// ...
|
24
|
+
|
25
|
+
export default {
|
26
|
+
// ...
|
27
|
+
external: ['screeps_clockwork.wasm'],
|
28
|
+
plugins: [
|
29
|
+
clear({ targets: ['dist'] }),
|
30
|
+
wasm(),
|
31
|
+
copy({
|
32
|
+
targets: [
|
33
|
+
{
|
34
|
+
src: 'node_modules/screeps-clockwork/dist/screeps_clockwork.wasm',
|
35
|
+
dest: 'dist'
|
36
|
+
}
|
37
|
+
]
|
38
|
+
}),
|
39
|
+
// ...
|
40
|
+
screeps({ config: cfg, dryRun: cfg == null })
|
41
|
+
]
|
42
|
+
};
|
43
|
+
```
|
44
|
+
|
45
|
+
`rollup-plugin-screeps` will automatically push the wasm file out when it deploys.
|
46
|
+
|
47
|
+
## Dev Setup
|
48
|
+
|
49
|
+
Dependencies to build and run the project:
|
50
|
+
|
51
|
+
```bash
|
52
|
+
# Install Rust + Cargo via rustup
|
53
|
+
curl https://sh.rustup.rs -sSf | sh
|
54
|
+
|
55
|
+
# Install wasm-pack
|
56
|
+
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
|
57
|
+
|
58
|
+
# Add rust-src component
|
59
|
+
rustup component add rust-src --toolchain nightly-x86_64-unknown-linux-gnu
|
60
|
+
|
61
|
+
# Install nvm
|
62
|
+
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
|
63
|
+
|
64
|
+
# Install node
|
65
|
+
nvm install --lts
|
66
|
+
nvm use --lts
|
67
|
+
nvm alias default --lts # optional
|
68
|
+
```
|
69
|
+
|
70
|
+
To build the project:
|
71
|
+
|
72
|
+
```bash
|
73
|
+
npm run build
|
74
|
+
```
|
75
|
+
|
76
|
+
To set up the local Screeps server, you'll need to have docker installed:
|
77
|
+
|
78
|
+
```bash
|
79
|
+
cp .env.sample .env # fill this out with the path to your Screeps .nw package and Steam key
|
80
|
+
./reset-docker.sh
|
81
|
+
```
|
82
|
+
|
83
|
+
Once running, you can log in and spawn into the server with the [local screeps-steamless-client](http://localhost:8080/).
|
84
|
+
|
85
|
+
- **Username:** clockwork
|
86
|
+
- **Password:** passw0rd
|
87
|
+
|
88
|
+
To deploy the test codebase, once the server is running:
|
89
|
+
|
90
|
+
```bash
|
91
|
+
npm run watch
|
92
|
+
```
|
93
|
+
|
94
|
+
Set up commitizen and pre-commit for conventional commits:
|
95
|
+
|
96
|
+
```bash
|
97
|
+
# requires Python 3.8+
|
98
|
+
pip install --user -U commitizen pre-commit
|
99
|
+
pre-commit install
|
100
|
+
```
|
package/dist/index.js
CHANGED
@@ -4,13 +4,25 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
5
5
|
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
6
6
|
|
7
|
+
var EncoderDecoderTogether_min = {};
|
8
|
+
|
9
|
+
var hasRequiredEncoderDecoderTogether_min;
|
10
|
+
|
11
|
+
function requireEncoderDecoderTogether_min () {
|
12
|
+
if (hasRequiredEncoderDecoderTogether_min) return EncoderDecoderTogether_min;
|
13
|
+
hasRequiredEncoderDecoderTogether_min = 1;
|
7
14
|
(function(q){function y(){}function C(b){var c=b.charCodeAt(0)|0;if(55296<=c)if(56319>=c)if(b=b.charCodeAt(1)|0,56320<=b&&57343>=b){if(c=(c<<10)+b-56613888|0,65535<c)return v(240|c>>18,128|c>>12&63,128|c>>6&63,128|c&63)}else c=65533;else 57343>=c&&(c=65533);return 2047>=c?v(192|c>>6,128|c&63):v(224|c>>12,128|c>>6&63,128|c&63)}function z(){}function A(b,c){var g=void 0===b?"":(""+b).replace(D,C),d=g.length|0,a=0,k=0,f=c.length|0,h=b.length|0;f<d&&(d=f);a:for(;a<d;a=a+1|0){b=g.charCodeAt(a)|
|
8
|
-
0;switch(b>>4){case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:k=k+1|0;case 8:case 9:case 10:case 11:break;case 12:case 13:if((a+1|0)<f){k=k+1|0;break}case 14:if((a+2|0)<f){k=k+1|0;break}case 15:if((a+3|0)<f){k=k+1|0;break}default:break a}c[a]=b;}return {written:a,read:h<k?h:k}}var v=String.fromCharCode,x={}.toString,E=x.call(q.SharedArrayBuffer),F=x(),t=q.Uint8Array,w=t||Array,u=t?ArrayBuffer:w,G=u.isView||function(b){return b&&"length"in b},H=x.call(u.prototype),B=z.prototype;u=q.TextEncoder;
|
9
|
-
var D=/[\x80-\uD7ff\uDC00-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]?/g,e=new (t?Uint16Array:w)(32);y.prototype.decode=function(b){if(!G(b)){var c=x.call(b);if(c!==H&&c!==E&&c!==F)throw TypeError("Failed to execute 'decode' on 'TextDecoder': The provided value is not of type '(ArrayBuffer or ArrayBufferView)'");b=t?new w(b):b||[];}for(var g=c="",d=0,a=b.length|0,k=a-32|0,f,h,l=0,r=0,n,m=0,p=-1;d<a;){for(f=d<=k?32:a-d|0;m<f;d=d+1|0,m=m+1|0){h=b[d]&255;switch(h>>4){case 15:n=b[d=d+1|0]&255;if(2!==n>>6||
|
10
|
-
247<h){d=d-1|0;break}l=(h&7)<<6|n&63;r=5;h=256;case 14:n=b[d=d+1|0]&255,l<<=6,l|=(h&15)<<6|n&63,r=2===n>>6?r+4|0:24,h=h+256&768;case 13:case 12:n=b[d=d+1|0]&255,l<<=6,l|=(h&31)<<6|n&63,r=r+7|0,d<a&&2===n>>6&&l>>r&&1114112>l?(h=l,l=l-65536|0,0<=l&&(p=(l>>10)+55296|0,h=(l&1023)+56320|0,31>m?(e[m]=p,m=m+1|0,p=-1):(n=p,p=h,h=n))):(h>>=8,d=d-h-1|0,h=65533),l=r=0,f=d<=k?32:a-d|0;default:e[m]=h;continue;case 11:case 10:case 9:case 8:}e[m]=65533;}g+=v(e[0],e[1],e[2],e[3],e[4],e[5],e[6],e[7],e[8],e[9],e[10],
|
11
|
-
e[11],e[12],e[13],e[14],e[15],e[16],e[17],e[18],e[19],e[20],e[21],e[22],e[23],e[24],e[25],e[26],e[27],e[28],e[29],e[30],e[31]);32>m&&(g=g.slice(0,m-32|0));if(d<a){if(e[0]=p,m=~p>>>31,p=-1,g.length<c.length)continue}else -1!==p&&(g+=v(p));c+=g;g="";}return c};B.encode=function(b){b=void 0===b?"":""+b;var c=b.length|0,g=new w((c<<1)+8|0),d,a=0,k=!t;for(d=0;d<c;d=d+1|0,a=a+1|0){var f=b.charCodeAt(d)|0;if(127>=f)g[a]=f;else {if(2047>=f)g[a]=192|f>>6;else {a:{if(55296<=f)if(56319>=f){var h=b.charCodeAt(d=
|
12
|
-
d+1|0)|0;if(56320<=h&&57343>=h){f=(f<<10)+h-56613888|0;if(65535<f){g[a]=240|f>>18;g[a=a+1|0]=128|f>>12&63;g[a=a+1|0]=128|f>>6&63;g[a=a+1|0]=128|f&63;continue}break a}f=65533;}else 57343>=f&&(f=65533);!k&&d<<1<a&&d<<1<(a-7|0)&&(k=!0,h=new w(3*c),h.set(g),g=h);}g[a]=224|f>>12;g[a=a+1|0]=128|f>>6&63;}g[a=a+1|0]=128|f&63;}}return t?g.subarray(0,a):g.slice(0,a)};B.encodeInto=A;if(!u)q.TextDecoder=y,q.TextEncoder=z;else if(!(q=u.prototype).encodeInto){var I=new u;q.encodeInto=function(b,c){var g=b.length|0,
|
13
|
-
d=c.length|0;if(g<d>>1){var a=I.encode(b);if((a.length|0)<d)return c.set(a),{read:g,written:a.length|0}}return A(b,c)};}})("undefined"==typeof commonjsGlobal?"undefined"==typeof self?
|
15
|
+
0;switch(b>>4){case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:k=k+1|0;case 8:case 9:case 10:case 11:break;case 12:case 13:if((a+1|0)<f){k=k+1|0;break}case 14:if((a+2|0)<f){k=k+1|0;break}case 15:if((a+3|0)<f){k=k+1|0;break}default:break a}c[a]=b;}return {written:a,read:h<k?h:k}}var v=String.fromCharCode,x={}.toString,E=x.call(q.SharedArrayBuffer),F=x(),t=q.Uint8Array,w=t||Array,u=t?ArrayBuffer:w,G=u.isView||function(b){return b&&"length"in b},H=x.call(u.prototype),B=z.prototype;u=q.TextEncoder;
|
16
|
+
var D=/[\x80-\uD7ff\uDC00-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]?/g,e=new (t?Uint16Array:w)(32);y.prototype.decode=function(b){if(!G(b)){var c=x.call(b);if(c!==H&&c!==E&&c!==F)throw TypeError("Failed to execute 'decode' on 'TextDecoder': The provided value is not of type '(ArrayBuffer or ArrayBufferView)'");b=t?new w(b):b||[];}for(var g=c="",d=0,a=b.length|0,k=a-32|0,f,h,l=0,r=0,n,m=0,p=-1;d<a;){for(f=d<=k?32:a-d|0;m<f;d=d+1|0,m=m+1|0){h=b[d]&255;switch(h>>4){case 15:n=b[d=d+1|0]&255;if(2!==n>>6||
|
17
|
+
247<h){d=d-1|0;break}l=(h&7)<<6|n&63;r=5;h=256;case 14:n=b[d=d+1|0]&255,l<<=6,l|=(h&15)<<6|n&63,r=2===n>>6?r+4|0:24,h=h+256&768;case 13:case 12:n=b[d=d+1|0]&255,l<<=6,l|=(h&31)<<6|n&63,r=r+7|0,d<a&&2===n>>6&&l>>r&&1114112>l?(h=l,l=l-65536|0,0<=l&&(p=(l>>10)+55296|0,h=(l&1023)+56320|0,31>m?(e[m]=p,m=m+1|0,p=-1):(n=p,p=h,h=n))):(h>>=8,d=d-h-1|0,h=65533),l=r=0,f=d<=k?32:a-d|0;default:e[m]=h;continue;case 11:case 10:case 9:case 8:}e[m]=65533;}g+=v(e[0],e[1],e[2],e[3],e[4],e[5],e[6],e[7],e[8],e[9],e[10],
|
18
|
+
e[11],e[12],e[13],e[14],e[15],e[16],e[17],e[18],e[19],e[20],e[21],e[22],e[23],e[24],e[25],e[26],e[27],e[28],e[29],e[30],e[31]);32>m&&(g=g.slice(0,m-32|0));if(d<a){if(e[0]=p,m=~p>>>31,p=-1,g.length<c.length)continue}else -1!==p&&(g+=v(p));c+=g;g="";}return c};B.encode=function(b){b=void 0===b?"":""+b;var c=b.length|0,g=new w((c<<1)+8|0),d,a=0,k=!t;for(d=0;d<c;d=d+1|0,a=a+1|0){var f=b.charCodeAt(d)|0;if(127>=f)g[a]=f;else {if(2047>=f)g[a]=192|f>>6;else {a:{if(55296<=f)if(56319>=f){var h=b.charCodeAt(d=
|
19
|
+
d+1|0)|0;if(56320<=h&&57343>=h){f=(f<<10)+h-56613888|0;if(65535<f){g[a]=240|f>>18;g[a=a+1|0]=128|f>>12&63;g[a=a+1|0]=128|f>>6&63;g[a=a+1|0]=128|f&63;continue}break a}f=65533;}else 57343>=f&&(f=65533);!k&&d<<1<a&&d<<1<(a-7|0)&&(k=!0,h=new w(3*c),h.set(g),g=h);}g[a]=224|f>>12;g[a=a+1|0]=128|f>>6&63;}g[a=a+1|0]=128|f&63;}}return t?g.subarray(0,a):g.slice(0,a)};B.encodeInto=A;if(!u)q.TextDecoder=y,q.TextEncoder=z;else if(!(q=u.prototype).encodeInto){var I=new u;q.encodeInto=function(b,c){var g=b.length|0,
|
20
|
+
d=c.length|0;if(g<d>>1){var a=I.encode(b);if((a.length|0)<d)return c.set(a),{read:g,written:a.length|0}}return A(b,c)};}})("undefined"==typeof commonjsGlobal?"undefined"==typeof self?EncoderDecoderTogether_min:self:commonjsGlobal);//AnonyCo
|
21
|
+
|
22
|
+
return EncoderDecoderTogether_min;
|
23
|
+
}
|
24
|
+
|
25
|
+
requireEncoderDecoderTogether_min();
|
14
26
|
|
15
27
|
let wasm;
|
16
28
|
|
@@ -72,6 +84,36 @@ function getStringFromWasm0(ptr, len) {
|
|
72
84
|
ptr = ptr >>> 0;
|
73
85
|
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
74
86
|
}
|
87
|
+
/**
|
88
|
+
* @returns {string}
|
89
|
+
*/
|
90
|
+
function version() {
|
91
|
+
let deferred1_0;
|
92
|
+
let deferred1_1;
|
93
|
+
try {
|
94
|
+
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
95
|
+
wasm.version(retptr);
|
96
|
+
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
97
|
+
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
98
|
+
deferred1_0 = r0;
|
99
|
+
deferred1_1 = r1;
|
100
|
+
return getStringFromWasm0(r0, r1);
|
101
|
+
} finally {
|
102
|
+
wasm.__wbindgen_add_to_stack_pointer(16);
|
103
|
+
wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
|
104
|
+
}
|
105
|
+
}
|
106
|
+
|
107
|
+
/**
|
108
|
+
* Exports the global range calculation between two positions.
|
109
|
+
* @param {number} packed_pos_1
|
110
|
+
* @param {number} packed_pos_2
|
111
|
+
* @returns {number}
|
112
|
+
*/
|
113
|
+
function get_range(packed_pos_1, packed_pos_2) {
|
114
|
+
const ret = wasm.get_range(packed_pos_1, packed_pos_2);
|
115
|
+
return ret >>> 0;
|
116
|
+
}
|
75
117
|
|
76
118
|
let cachedUint32ArrayMemory0 = null;
|
77
119
|
|
@@ -98,87 +140,64 @@ function _assertClass(instance, klass) {
|
|
98
140
|
return instance.ptr;
|
99
141
|
}
|
100
142
|
/**
|
101
|
-
*
|
102
|
-
* * WASM wrapper for the BFS distance map function.
|
103
|
-
*
|
143
|
+
* WASM wrapper for the Dijkstra flow field function.
|
104
144
|
* @param {Uint32Array} start_packed
|
105
145
|
* @param {ClockworkCostMatrix} cost_matrix
|
106
|
-
* @returns {
|
146
|
+
* @returns {FlowField}
|
107
147
|
*/
|
108
|
-
function
|
148
|
+
function js_dijkstra_flow_field(start_packed, cost_matrix) {
|
109
149
|
const ptr0 = passArray32ToWasm0(start_packed, wasm.__wbindgen_malloc);
|
110
150
|
const len0 = WASM_VECTOR_LEN;
|
111
151
|
_assertClass(cost_matrix, ClockworkCostMatrix);
|
112
|
-
const ret = wasm.
|
113
|
-
return
|
152
|
+
const ret = wasm.js_dijkstra_flow_field(ptr0, len0, cost_matrix.__wbg_ptr);
|
153
|
+
return FlowField.__wrap(ret);
|
114
154
|
}
|
115
155
|
|
116
156
|
/**
|
117
|
-
*
|
118
|
-
* * WASM wrapper for the BFS flow field function.
|
119
|
-
*
|
157
|
+
* WASM wrapper for the Dijkstra mono flow field function.
|
120
158
|
* @param {Uint32Array} start_packed
|
121
159
|
* @param {ClockworkCostMatrix} cost_matrix
|
122
|
-
* @returns {
|
160
|
+
* @returns {MonoFlowField}
|
123
161
|
*/
|
124
|
-
function
|
162
|
+
function js_dijkstra_mono_flow_field(start_packed, cost_matrix) {
|
125
163
|
const ptr0 = passArray32ToWasm0(start_packed, wasm.__wbindgen_malloc);
|
126
164
|
const len0 = WASM_VECTOR_LEN;
|
127
165
|
_assertClass(cost_matrix, ClockworkCostMatrix);
|
128
|
-
const ret = wasm.
|
129
|
-
return
|
166
|
+
const ret = wasm.js_dijkstra_mono_flow_field(ptr0, len0, cost_matrix.__wbg_ptr);
|
167
|
+
return MonoFlowField.__wrap(ret);
|
130
168
|
}
|
131
169
|
|
132
170
|
/**
|
133
|
-
*
|
134
|
-
* * WASM wrapper for the BFS mono flow field function.
|
135
|
-
*
|
171
|
+
* WASM wrapper for the BFS flow field function.
|
136
172
|
* @param {Uint32Array} start_packed
|
137
173
|
* @param {ClockworkCostMatrix} cost_matrix
|
138
|
-
* @returns {
|
174
|
+
* @returns {FlowField}
|
139
175
|
*/
|
140
|
-
function
|
176
|
+
function js_bfs_flow_field(start_packed, cost_matrix) {
|
141
177
|
const ptr0 = passArray32ToWasm0(start_packed, wasm.__wbindgen_malloc);
|
142
178
|
const len0 = WASM_VECTOR_LEN;
|
143
179
|
_assertClass(cost_matrix, ClockworkCostMatrix);
|
144
|
-
const ret = wasm.
|
145
|
-
return
|
146
|
-
}
|
147
|
-
|
148
|
-
/**
|
149
|
-
* @returns {string}
|
150
|
-
*/
|
151
|
-
function version() {
|
152
|
-
let deferred1_0;
|
153
|
-
let deferred1_1;
|
154
|
-
try {
|
155
|
-
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
156
|
-
wasm.version(retptr);
|
157
|
-
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
158
|
-
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
159
|
-
deferred1_0 = r0;
|
160
|
-
deferred1_1 = r1;
|
161
|
-
return getStringFromWasm0(r0, r1);
|
162
|
-
} finally {
|
163
|
-
wasm.__wbindgen_add_to_stack_pointer(16);
|
164
|
-
wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
|
165
|
-
}
|
166
|
-
}
|
167
|
-
|
168
|
-
/**
|
169
|
-
* @param {number} packed_pos_1
|
170
|
-
* @param {number} packed_pos_2
|
171
|
-
* @returns {number}
|
172
|
-
*/
|
173
|
-
function get_range(packed_pos_1, packed_pos_2) {
|
174
|
-
const ret = wasm.get_range(packed_pos_1, packed_pos_2);
|
175
|
-
return ret >>> 0;
|
180
|
+
const ret = wasm.js_bfs_flow_field(ptr0, len0, cost_matrix.__wbg_ptr);
|
181
|
+
return FlowField.__wrap(ret);
|
176
182
|
}
|
177
183
|
|
178
184
|
function getArrayU32FromWasm0(ptr, len) {
|
179
185
|
ptr = ptr >>> 0;
|
180
186
|
return getUint32ArrayMemory0().subarray(ptr / 4, ptr / 4 + len);
|
181
187
|
}
|
188
|
+
/**
|
189
|
+
* WASM wrapper for the Dijkstra distance map function.
|
190
|
+
* @param {Uint32Array} start_packed
|
191
|
+
* @param {ClockworkCostMatrix} cost_matrix
|
192
|
+
* @returns {DistanceMap}
|
193
|
+
*/
|
194
|
+
function js_dijkstra_distance_map(start_packed, cost_matrix) {
|
195
|
+
const ptr0 = passArray32ToWasm0(start_packed, wasm.__wbindgen_malloc);
|
196
|
+
const len0 = WASM_VECTOR_LEN;
|
197
|
+
_assertClass(cost_matrix, ClockworkCostMatrix);
|
198
|
+
const ret = wasm.js_dijkstra_distance_map(ptr0, len0, cost_matrix.__wbg_ptr);
|
199
|
+
return DistanceMap.__wrap(ret);
|
200
|
+
}
|
182
201
|
|
183
202
|
function getArrayJsValueFromWasm0(ptr, len) {
|
184
203
|
ptr = ptr >>> 0;
|
@@ -199,6 +218,34 @@ function passArrayJsValueToWasm0(array, malloc) {
|
|
199
218
|
WASM_VECTOR_LEN = array.length;
|
200
219
|
return ptr;
|
201
220
|
}
|
221
|
+
/**
|
222
|
+
* WASM wrapper for the BFS distance map function.
|
223
|
+
* @param {Uint32Array} start_packed
|
224
|
+
* @param {ClockworkCostMatrix} cost_matrix
|
225
|
+
* @returns {DistanceMap}
|
226
|
+
*/
|
227
|
+
function js_bfs_distance_map(start_packed, cost_matrix) {
|
228
|
+
const ptr0 = passArray32ToWasm0(start_packed, wasm.__wbindgen_malloc);
|
229
|
+
const len0 = WASM_VECTOR_LEN;
|
230
|
+
_assertClass(cost_matrix, ClockworkCostMatrix);
|
231
|
+
const ret = wasm.js_bfs_distance_map(ptr0, len0, cost_matrix.__wbg_ptr);
|
232
|
+
return DistanceMap.__wrap(ret);
|
233
|
+
}
|
234
|
+
|
235
|
+
/**
|
236
|
+
* WASM wrapper for the BFS mono flow field function.
|
237
|
+
* @param {Uint32Array} start_packed
|
238
|
+
* @param {ClockworkCostMatrix} cost_matrix
|
239
|
+
* @returns {MonoFlowField}
|
240
|
+
*/
|
241
|
+
function js_bfs_mono_flow_field(start_packed, cost_matrix) {
|
242
|
+
const ptr0 = passArray32ToWasm0(start_packed, wasm.__wbindgen_malloc);
|
243
|
+
const len0 = WASM_VECTOR_LEN;
|
244
|
+
_assertClass(cost_matrix, ClockworkCostMatrix);
|
245
|
+
const ret = wasm.js_bfs_mono_flow_field(ptr0, len0, cost_matrix.__wbg_ptr);
|
246
|
+
return MonoFlowField.__wrap(ret);
|
247
|
+
}
|
248
|
+
|
202
249
|
/**
|
203
250
|
* Translates `COLOR_*` and `COLORS_ALL` constants.
|
204
251
|
*/
|
@@ -256,7 +303,11 @@ Object.freeze({ Plain:0,"0":"Plain",Wall:1,"1":"Wall",Swamp:2,"2":"Swamp", });
|
|
256
303
|
const ClockworkCostMatrixFinalization = (typeof FinalizationRegistry === 'undefined')
|
257
304
|
? { register: () => {}, unregister: () => {} }
|
258
305
|
: new FinalizationRegistry(ptr => wasm.__wbg_clockworkcostmatrix_free(ptr >>> 0, 1));
|
259
|
-
|
306
|
+
/**
|
307
|
+
* A wrapper around the `LocalCostMatrix` type from the Screeps API.
|
308
|
+
* Instances can be passed between WASM and JS as a pointer, using the
|
309
|
+
* methods to get and set values, rather than copying the entire matrix.
|
310
|
+
*/
|
260
311
|
class ClockworkCostMatrix {
|
261
312
|
|
262
313
|
__destroy_into_raw() {
|
@@ -270,13 +321,19 @@ class ClockworkCostMatrix {
|
|
270
321
|
const ptr = this.__destroy_into_raw();
|
271
322
|
wasm.__wbg_clockworkcostmatrix_free(ptr, 0);
|
272
323
|
}
|
273
|
-
|
274
|
-
|
324
|
+
/**
|
325
|
+
* Creates a new cost matrix within the WASM module. Optionally, a default value
|
326
|
+
* can be provided to initialize all cells in the matrix to that value.
|
327
|
+
* @param {number | undefined} [_default]
|
328
|
+
*/
|
329
|
+
constructor(_default) {
|
330
|
+
const ret = wasm.clockworkcostmatrix_new(isLikeNone(_default) ? 0xFFFFFF : _default);
|
275
331
|
this.__wbg_ptr = ret >>> 0;
|
276
332
|
ClockworkCostMatrixFinalization.register(this, this.__wbg_ptr, this);
|
277
333
|
return this;
|
278
334
|
}
|
279
335
|
/**
|
336
|
+
* Gets the cost of a given position in the cost matrix.
|
280
337
|
* @param {number} x
|
281
338
|
* @param {number} y
|
282
339
|
* @returns {number}
|
@@ -286,6 +343,7 @@ class ClockworkCostMatrix {
|
|
286
343
|
return ret;
|
287
344
|
}
|
288
345
|
/**
|
346
|
+
* Sets the cost of a given position in the cost matrix.
|
289
347
|
* @param {number} x
|
290
348
|
* @param {number} y
|
291
349
|
* @param {number} value
|
@@ -299,7 +357,7 @@ const DistanceMapFinalization = (typeof FinalizationRegistry === 'undefined')
|
|
299
357
|
? { register: () => {}, unregister: () => {} }
|
300
358
|
: new FinalizationRegistry(ptr => wasm.__wbg_distancemap_free(ptr >>> 0, 1));
|
301
359
|
/**
|
302
|
-
* Maps
|
360
|
+
* Maps a distance value onto individual room tile positions.
|
303
361
|
*/
|
304
362
|
class DistanceMap {
|
305
363
|
|
@@ -323,6 +381,7 @@ class DistanceMap {
|
|
323
381
|
wasm.__wbg_distancemap_free(ptr, 0);
|
324
382
|
}
|
325
383
|
/**
|
384
|
+
* Converts the distance map into a flat array of distances.
|
326
385
|
* @returns {Uint32Array}
|
327
386
|
*/
|
328
387
|
toArray() {
|
@@ -339,6 +398,7 @@ class DistanceMap {
|
|
339
398
|
}
|
340
399
|
}
|
341
400
|
/**
|
401
|
+
* Gets the distance value at a given position.
|
342
402
|
* @param {number} x
|
343
403
|
* @param {number} y
|
344
404
|
* @returns {number}
|
@@ -348,6 +408,7 @@ class DistanceMap {
|
|
348
408
|
return ret >>> 0;
|
349
409
|
}
|
350
410
|
/**
|
411
|
+
* Sets the distance value at a given position.
|
351
412
|
* @param {number} x
|
352
413
|
* @param {number} y
|
353
414
|
* @param {number} value
|
@@ -361,12 +422,10 @@ const FlowFieldFinalization = (typeof FinalizationRegistry === 'undefined')
|
|
361
422
|
? { register: () => {}, unregister: () => {} }
|
362
423
|
: new FinalizationRegistry(ptr => wasm.__wbg_flowfield_free(ptr >>> 0, 1));
|
363
424
|
/**
|
364
|
-
*
|
365
|
-
*
|
366
|
-
*
|
367
|
-
*
|
368
|
-
* * 8-bit unsigned integer represents a direction that is either viable or not).
|
369
|
-
*
|
425
|
+
* A flow field is a 50x50 grid (representing a room), representing viable directions
|
426
|
+
* to travel to reach a particular target (or targets). A given tile may have multiple
|
427
|
+
* equally valid directions, so we represent this as a bitfield (where each bit in an
|
428
|
+
* 8-bit unsigned integer represents a direction that is either viable or not).
|
370
429
|
*/
|
371
430
|
class FlowField {
|
372
431
|
|
@@ -390,9 +449,7 @@ class FlowField {
|
|
390
449
|
wasm.__wbg_flowfield_free(ptr, 0);
|
391
450
|
}
|
392
451
|
/**
|
393
|
-
*
|
394
|
-
* * Get the internal value for a given coordinate.
|
395
|
-
*
|
452
|
+
* Get the internal value for a given coordinate.
|
396
453
|
* @param {number} x
|
397
454
|
* @param {number} y
|
398
455
|
* @returns {number}
|
@@ -402,9 +459,7 @@ class FlowField {
|
|
402
459
|
return ret;
|
403
460
|
}
|
404
461
|
/**
|
405
|
-
*
|
406
|
-
* * Set the internal value for a given coordinate.
|
407
|
-
*
|
462
|
+
* Set the internal value for a given coordinate.
|
408
463
|
* @param {number} x
|
409
464
|
* @param {number} y
|
410
465
|
* @param {number} value
|
@@ -413,9 +468,7 @@ class FlowField {
|
|
413
468
|
wasm.flowfield_set(this.__wbg_ptr, x, y, value);
|
414
469
|
}
|
415
470
|
/**
|
416
|
-
*
|
417
|
-
* * Get the list of valid directions for a given coordinate.
|
418
|
-
*
|
471
|
+
* Get the list of valid directions for a given coordinate.
|
419
472
|
* @param {number} x
|
420
473
|
* @param {number} y
|
421
474
|
* @returns {any[]}
|
@@ -434,9 +487,7 @@ class FlowField {
|
|
434
487
|
}
|
435
488
|
}
|
436
489
|
/**
|
437
|
-
*
|
438
|
-
* * Set the list of valid directions for a given coordinate.
|
439
|
-
*
|
490
|
+
* Set the list of valid directions for a given coordinate.
|
440
491
|
* @param {number} x
|
441
492
|
* @param {number} y
|
442
493
|
* @param {any[]} directions
|
@@ -447,6 +498,7 @@ class FlowField {
|
|
447
498
|
wasm.flowfield_setDirections(this.__wbg_ptr, x, y, ptr0, len0);
|
448
499
|
}
|
449
500
|
/**
|
501
|
+
* Add a direction to the list of valid directions for a given coordinate.
|
450
502
|
* @param {number} x
|
451
503
|
* @param {number} y
|
452
504
|
* @param {Direction} direction
|
@@ -460,12 +512,10 @@ const MonoFlowFieldFinalization = (typeof FinalizationRegistry === 'undefined')
|
|
460
512
|
? { register: () => {}, unregister: () => {} }
|
461
513
|
: new FinalizationRegistry(ptr => wasm.__wbg_monoflowfield_free(ptr >>> 0, 1));
|
462
514
|
/**
|
463
|
-
*
|
464
|
-
*
|
465
|
-
*
|
466
|
-
*
|
467
|
-
* * integer (0 for no direction, 1 for TOP, etc.).
|
468
|
-
*
|
515
|
+
* A flow field is a 50x50 grid (representing a room), representing viable directions
|
516
|
+
* to travel to reach a particular target (or targets). A mono flow field only stores
|
517
|
+
* a single direction for each tile, so we represent this as 4 bits of an unsigned
|
518
|
+
* integer (0 for no direction, 1 for TOP, etc.).
|
469
519
|
*/
|
470
520
|
class MonoFlowField {
|
471
521
|
|
@@ -489,9 +539,7 @@ class MonoFlowField {
|
|
489
539
|
wasm.__wbg_monoflowfield_free(ptr, 0);
|
490
540
|
}
|
491
541
|
/**
|
492
|
-
*
|
493
|
-
* * Get the direction for a given coordinate.
|
494
|
-
*
|
542
|
+
* Get the direction for a given coordinate.
|
495
543
|
* @param {number} x
|
496
544
|
* @param {number} y
|
497
545
|
* @returns {Direction | undefined}
|
@@ -501,9 +549,7 @@ class MonoFlowField {
|
|
501
549
|
return ret === 0 ? undefined : ret;
|
502
550
|
}
|
503
551
|
/**
|
504
|
-
*
|
505
|
-
* * Set the direction for a given coordinate.
|
506
|
-
*
|
552
|
+
* Set the direction for a given coordinate.
|
507
553
|
* @param {number} x
|
508
554
|
* @param {number} y
|
509
555
|
* @param {Direction | undefined} [value]
|
@@ -591,23 +637,145 @@ function initSync(module) {
|
|
591
637
|
return __wbg_finalize_init(instance);
|
592
638
|
}
|
593
639
|
|
640
|
+
/**
|
641
|
+
* Generate a [distance map](https://glitchassassin.github.io/screeps-clockwork/primitives/flowfield.html) for a set of positions
|
642
|
+
* using a breadth-first search algorithm.
|
643
|
+
*
|
644
|
+
* The BFS algorithm doesn't include variable costs, and only considers
|
645
|
+
* values of 255 (impassible) in the provided cost matrix. Any other
|
646
|
+
* values are ignored.
|
647
|
+
*
|
648
|
+
* This might be useful for creeps with only MOVE parts and/or empty
|
649
|
+
* CARRY parts, which don't generate fatigue.
|
650
|
+
*
|
651
|
+
* Note that the `roomName` on start positions is ignored - all positions
|
652
|
+
* are assumed to be in the same room as the cost matrix.
|
653
|
+
*
|
654
|
+
* @param start - The starting positions.
|
655
|
+
* @param costMatrix - The cost matrix to use for the flow field.
|
656
|
+
* @returns The flow field.
|
657
|
+
*/
|
594
658
|
function bfsDistanceMap(start, costMatrix) {
|
595
659
|
const startPacked = new Uint32Array(start.map(pos => pos.__packedPos));
|
596
660
|
const result = js_bfs_distance_map(startPacked, costMatrix);
|
597
661
|
return result;
|
598
662
|
}
|
599
663
|
|
664
|
+
/**
|
665
|
+
* Generate a [flow field](https://glitchassassin.github.io/screeps-clockwork/primitives/flowfield.html) for a set of positions
|
666
|
+
* using a breadth-first search algorithm.
|
667
|
+
*
|
668
|
+
* The BFS algorithm doesn't include variable costs, and only considers
|
669
|
+
* values of 255 (impassible) in the provided cost matrix. Any other
|
670
|
+
* values are ignored.
|
671
|
+
*
|
672
|
+
* This might be useful for creeps with only MOVE parts and/or empty
|
673
|
+
* CARRY parts, which don't generate fatigue.
|
674
|
+
*
|
675
|
+
* @param start - The starting positions.
|
676
|
+
* @param costMatrix - The cost matrix to use for the flow field.
|
677
|
+
* @returns The flow field.
|
678
|
+
*/
|
600
679
|
function bfsFlowField(start, costMatrix) {
|
601
680
|
const startPacked = new Uint32Array(start.map(pos => pos.__packedPos));
|
602
681
|
const result = js_bfs_flow_field(startPacked, costMatrix);
|
603
682
|
return result;
|
604
683
|
}
|
684
|
+
/**
|
685
|
+
* Generate a [mono-directional flow field](https://glitchassassin.github.io/screeps-clockwork/primitives/flowfield.html)
|
686
|
+
* for a set of positions using a breadth-first search algorithm.
|
687
|
+
*
|
688
|
+
* The BFS algorithm doesn't include variable costs, and only considers
|
689
|
+
* values of 255 (impassible) in the provided cost matrix. Any other
|
690
|
+
* values are ignored.
|
691
|
+
*
|
692
|
+
* This might be useful for creeps with only MOVE parts and/or empty
|
693
|
+
* CARRY parts, which don't generate fatigue.
|
694
|
+
*
|
695
|
+
* Note that the `roomName` on start positions is ignored - all positions
|
696
|
+
* are assumed to be in the same room as the cost matrix.
|
697
|
+
*
|
698
|
+
* @param start - The starting positions.
|
699
|
+
* @param costMatrix - The cost matrix to use for the flow field.
|
700
|
+
* @returns The flow field.
|
701
|
+
*/
|
605
702
|
function bfsMonoFlowField(start, costMatrix) {
|
606
703
|
const startPacked = new Uint32Array(start.map(pos => pos.__packedPos));
|
607
704
|
const result = js_bfs_mono_flow_field(startPacked, costMatrix);
|
608
705
|
return result;
|
609
706
|
}
|
610
707
|
|
708
|
+
/**
|
709
|
+
* Generate a [distance map](https://glitchassassin.github.io/screeps-clockwork/primitives/flowfield.html) for a set of positions
|
710
|
+
* using Dijkstra's algorithm.
|
711
|
+
*
|
712
|
+
* Dijkstra's algorithm includes variable costs to account for terrain or other cost functions.
|
713
|
+
*
|
714
|
+
* Note that values of 0 in the cost matrix may have unexpected behavior. You probably want
|
715
|
+
* a cost matrix with a default value of at least 1.
|
716
|
+
*
|
717
|
+
* Note that the `roomName` on start positions is ignored - all positions
|
718
|
+
* are assumed to be in the same room as the cost matrix.
|
719
|
+
*
|
720
|
+
* @param start - The starting positions.
|
721
|
+
* @param costMatrix - The cost matrix to use for the flow field.
|
722
|
+
* @returns The flow field.
|
723
|
+
*/
|
724
|
+
function dijkstraDistanceMap(start, costMatrix) {
|
725
|
+
const startPacked = new Uint32Array(start.map(pos => pos.__packedPos));
|
726
|
+
const result = js_dijkstra_distance_map(startPacked, costMatrix);
|
727
|
+
return result;
|
728
|
+
}
|
729
|
+
|
730
|
+
/**
|
731
|
+
* Generate a [flow field](https://glitchassassin.github.io/screeps-clockwork/primitives/flowfield.html) for a set of positions
|
732
|
+
* using Dijkstra's algorithm.
|
733
|
+
*
|
734
|
+
* Dijkstra's algorithm includes variable costs to account for terrain or other cost functions.
|
735
|
+
*
|
736
|
+
* Note that values of 0 in the cost matrix may have unexpected behavior. You probably want
|
737
|
+
* a cost matrix with a default value of at least 1.
|
738
|
+
*
|
739
|
+
* @param start - The starting positions.
|
740
|
+
* @param costMatrix - The cost matrix to use for the flow field.
|
741
|
+
* @returns The flow field.
|
742
|
+
*/
|
743
|
+
function dijkstraFlowField(start, costMatrix) {
|
744
|
+
const startPacked = new Uint32Array(start.map(pos => pos.__packedPos));
|
745
|
+
const result = js_dijkstra_flow_field(startPacked, costMatrix);
|
746
|
+
return result;
|
747
|
+
}
|
748
|
+
/**
|
749
|
+
* Generate a [mono-directional flow field](https://glitchassassin.github.io/screeps-clockwork/primitives/flowfield.html)
|
750
|
+
* for a set of positions using Dijkstra's algorithm.
|
751
|
+
*
|
752
|
+
* Dijkstra's algorithm includes variable costs to account for terrain or other cost functions.
|
753
|
+
*
|
754
|
+
* Note that values of 0 in the cost matrix may have unexpected behavior. You probably want
|
755
|
+
* a cost matrix with a default value of at least 1.
|
756
|
+
*
|
757
|
+
* Note that the `roomName` on start positions is ignored - all positions
|
758
|
+
* are assumed to be in the same room as the cost matrix.
|
759
|
+
*
|
760
|
+
* @param start - The starting positions.
|
761
|
+
* @param costMatrix - The cost matrix to use for the flow field.
|
762
|
+
* @returns The flow field.
|
763
|
+
*/
|
764
|
+
function dijkstraMonoFlowField(start, costMatrix) {
|
765
|
+
const startPacked = new Uint32Array(start.map(pos => pos.__packedPos));
|
766
|
+
const result = js_dijkstra_mono_flow_field(startPacked, costMatrix);
|
767
|
+
return result;
|
768
|
+
}
|
769
|
+
|
770
|
+
/**
|
771
|
+
* Get the global range between two positions. This is different
|
772
|
+
* from the `getRange` method on `RoomPosition`, which gets the
|
773
|
+
* range only within the same room.
|
774
|
+
*
|
775
|
+
* @param pos1 - The first position.
|
776
|
+
* @param pos2 - The second position.
|
777
|
+
* @returns The range between the two positions.
|
778
|
+
*/
|
611
779
|
function getRange(pos1, pos2) {
|
612
780
|
return get_range(pos1.__packedPos, pos2.__packedPos);
|
613
781
|
}
|
@@ -616,6 +784,14 @@ let wasm_bytes;
|
|
616
784
|
let wasm_module;
|
617
785
|
let wasm_instance;
|
618
786
|
let initialized = false;
|
787
|
+
/**
|
788
|
+
* The `initialize` function should be called in your main loop before
|
789
|
+
* using any other screeps-clockwork functions. Depending on available
|
790
|
+
* CPU, it may not load the WASM module completely in the first tick,
|
791
|
+
* but it will pick back up where it left off if the script times out.
|
792
|
+
*
|
793
|
+
* @param verbose - If true, will log the state of the WASM module as it loads.
|
794
|
+
*/
|
619
795
|
function initialize(verbose = false) {
|
620
796
|
if (!wasm_bytes)
|
621
797
|
wasm_bytes = require('screeps_clockwork.wasm');
|
@@ -641,6 +817,9 @@ exports.MonoFlowField = MonoFlowField;
|
|
641
817
|
exports.bfsDistanceMap = bfsDistanceMap;
|
642
818
|
exports.bfsFlowField = bfsFlowField;
|
643
819
|
exports.bfsMonoFlowField = bfsMonoFlowField;
|
820
|
+
exports.dijkstraDistanceMap = dijkstraDistanceMap;
|
821
|
+
exports.dijkstraFlowField = dijkstraFlowField;
|
822
|
+
exports.dijkstraMonoFlowField = dijkstraMonoFlowField;
|
644
823
|
exports.getRange = getRange;
|
645
824
|
exports.initialize = initialize;
|
646
825
|
//# sourceMappingURL=index.js.map
|