-
Notifications
You must be signed in to change notification settings - Fork 10
/
gf.js
176 lines (132 loc) · 4.69 KB
/
gf.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
/**
* Created by Piers on 19/07/2016.
*/
"use strict";
var gc = require("gc");
var gf = {
isEnterable: function (pos) {
var atPos = pos.look();
var SWAMP = "swamp";
var PLAIN = "plain";
for ( var i = 0 ; i < atPos.length ; i++ )
{
switch (atPos[i].type) {
case LOOK_TERRAIN:
if (atPos[i].terrain != PLAIN && atPos[i].terrain != SWAMP)
return false;
break;
case LOOK_STRUCTURES:
if (OBSTACLE_OBJECT_TYPES.includes(atPos[i].structure.structureType))
return false;
break;
case LOOK_CREEPS:
case LOOK_SOURCES:
case LOOK_MINERALS:
case LOOK_NUKES:
case LOOK_ENERGY:
case LOOK_RESOURCES:
case LOOK_FLAGS:
case LOOK_CONSTRUCTION_SITES:
default:
}
}
return true;
},
unique: function(array) {
var unique = [];
for (var i = 0; i < array.length; i++) {
var current = array[i];
if (unique.indexOf(current) < 0) unique.push(current);
}
return unique;
},
structureTypeInRange: function (pos, structureType, range) {
if (!range) range = 1;
//console.log("structureTypeInRange",pos, structureType,range);
var structures = Game.rooms[pos.roomName].find(FIND_STRUCTURES, {
filter: { structureType: structureType }
});
//console.log(pos,"structureTypeInRange",structureType,"obj",structures);
var inRange = [];
for ( var i = 0 ; i < structures.length ; i++ ) {
//console.log(pos,"pos",structures[i].pos,"is it in range",pos.inRangeTo(structures[i], range))
if ( pos.inRangeTo(structures[i], range) )
inRange.push(structures[i]);
}
return inRange;
},
isStructureTypeAtPos: function (pos, structureType) {
if (!pos || !pos.roomName) return undefined;
if (!Game.rooms[pos.roomName]) return undefined;
var atPos = pos.look();
if (!atPos) return false;
//console.log(pos,"isStructureTypeAtPos",structureType,atPos);
for ( var i = 0 ; i < atPos.length ; i++ ) {
if (atPos[i].type == LOOK_STRUCTURES
&& atPos[i].structure.structureType == structureType)
return true;
}
return false;
},
joinPointsBetween: function (pos1, pos2) {
var deltaX = pos1.x - pos2.x;
var deltaY = pos1.y - pos2.y;
var offsets = gc.ADJACENCIES[deltaX][deltaY];
var joinPos = [];
// console.log("joinPointsBetween", pos1,pos2);
for (var i = 0 ; i < offsets.length ; i++ ) {
var pos = new RoomPosition( pos2.x + offsets[i].dx,
pos2.y + offsets[i].dy,
pos2.roomName );
if (gf.isEnterable(pos)) joinPos.push(pos);
}
return joinPos;
},
isFull: function (dump) {
switch (dump.structureType) {
case STRUCTURE_CONTAINER:
case STRUCTURE_STORAGE:
case STRUCTURE_TERMINAL:
return dump.storeCapacity == _.sum(dump.store);
case STRUCTURE_EXTENSION:
case STRUCTURE_SPAWN:
case STRUCTURE_LINK:
return dump.energyCapacity == dump.energy;
default:
return undefined;
}
},
getPerimeter: function (N) {
var perimeter = [];
for ( var i = -N ; i <= N ; i++ ) {
if ( i != -N )
perimeter.push( { x : N, y : i } );
if ( i != N )
perimeter.push( { x : i, y : N } );
if ( i != N )
perimeter.push( { x : -N, y : i } );
if ( i != -N )
perimeter.push( { x : i, y : -N } );
}
return perimeter;
},
countValues(array, value) {
var count = 0;
for( var i = 0 ; i < array.length ; i++ ) {
if (array[i] == value) count++;
}
return count;
},
mostAbundantNonEnergyStore: function (store, exclude) {
var exclude = exclude ? exclude : RESOURCE_ENERGY;
var maxSoFar = 0, mostAbundantResource;
for ( var resource in store ) {
if ( store[resource] > maxSoFar && resource != exclude ) {
maxSoFar = store[resource];
mostAbundantResource = resource;
}
}
return mostAbundantResource;
}
};
module.exports = gf;