-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.js
More file actions
85 lines (79 loc) · 2.12 KB
/
utilities.js
File metadata and controls
85 lines (79 loc) · 2.12 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
82
83
84
85
/**
* Summary. Returns the distance between pos1 and pos2
*
* @param pos1
* @param pos2
* @returns {number}
*/
const distance=function(pos1,pos2){
let xdif=pos1.x-pos2.x;
let ydif=pos1.y-pos2.y;
return Math.max(Math.abs(xdif),Math.abs(ydif));
};
module.exports = {
distance: distance,
/**
* Summary. Returns true if pos1 and pos2 are adjacent, otherwise false
*
* @param pos1
* @param pos2
* @returns {boolean}
*/
areAdjacent: function(pos1,pos2){
return distance(pos1,pos2)<=1;
},
/**
* Summary. Returns true if pos1 and pos2 are within 3 spaces of each other, otherwise false
*
* @param pos1
* @param pos2
* @returns {boolean}
*/
isWithinRange: function(pos1,pos2){
return distance(pos1,pos2)<=3;
},
/**
* Summary. Generates a new creep name by adding to prefix
*
* @param {String} prefix Prefix to use for name generation, generally is the creep's role
* @returns {String} Generated Name
*/
selectCreepName: function(prefix){
const letter="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
let name;
while(true){
name=prefix;
for(let i=0;i<4;i++){
name+=letter.charAt(Math.floor(Math.random()*letter.length));
}
if(!Game.creeps[name])
return name;
}
},
/**
* Summary. Generates an array of ids from an array of objects
*
* @param {*[]} objs Array of objects to get IDs from
* @returns {String[]} Array of IDs derive from objs
*/
objsToIds: function(objs){
let ids=[];
for(let i in objs){
ids.push(objs[i].id);
}
return ids;
},
/**
* Summary. Generates an array of objects from an array of IDs
*
* @param {*[]} ids Array of IDs to get objects from
* @returns {Structure[]} Array of objects derive from ids
*/
idsToObjs: function(ids){
let objs = [];
for(let i in ids){
objs.push(Game.getObjectById(ids[i]));
}
return objs;
}
};