-
Notifications
You must be signed in to change notification settings - Fork 38
/
flagDir.js
77 lines (73 loc) · 2.58 KB
/
flagDir.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
var mod = {
list:[],
findName: function(flagColor, pos, local, rangeMod){
let self = this;
if( flagColor == null || this.list.length == 0)
return null;
let filter = flagColor.filter;
if( local && pos && pos.roomName )
_.assign(filter, {roomName: pos.roomName});
let flags = _.filter(this.list, filter);
if( flags.length == 0 )
return null;
if( flags.length == 1 )
return flags[0].name;
// some flags found - find nearest by roughly estimated range
if( pos && pos.roomName ){
var range = flag => {
var r = 0;
let roomDist = self.roomDistance(flag.roomName, pos.roomName);
if( roomDist == 0 )
r = _.max([Math.abs(flag.x-pos.x), Math.abs(flag.y-pos.y)]);
else r = roomDist * 100;
if( rangeMod ){
r = rangeMod(r, flag);
}
return r;
};
return _.sortBy(flags, range)[0].name;
} else return flags[0];
},
find: function(flagColor, pos, local, rangeMod){
let id = this.findName(flagColor, pos, local, rangeMod);
if( id === null )
return null;
return Game.flags[id];
},
loop: function(){
this.list = [];
var register = flag => {
flag.creeps = {};
this.list.push({
name: flag.name,
color: flag.color,
secondaryColor: flag.secondaryColor,
roomName: flag.pos.roomName,
x: flag.pos.x,
y: flag.pos.y
})};
_.forEach(Game.flags, register);
},
count: function(flagColor, pos, local){
let self = this;
if( flagColor == null || this.list.length == 0)
return 0;
let filter = flagColor.filter;
if( local && pos && pos.roomName )
_.assign(filter, {roomName: pos.roomName});
return _.countBy(this.list, filter).true;
},
roomDistance: function(a, b){
if( a == b ) return 0;
let posA = a.split(/([N,E,S,W])/);
let posB = b.split(/([N,E,S,W])/);
let xDif = posA[1] == posB[1] ? Math.abs(posA[2]-posB[2]) : posA[2]+posB[2]+1;
let yDif = posA[3] == posB[3] ? Math.abs(posA[4]-posB[4]) : posA[4]+posB[4]+1;
return xDif + yDif;
},
rangeModPrivateer: function(range, flagItem){
var flag = Game.flags[flagItem.name];
return range + ((flag.creeps.sum || 0) * 10);
}
}
module.exports = mod;