-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroom.experimental.js
119 lines (105 loc) · 4.9 KB
/
room.experimental.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
var lodash = require('lodash');
Room.prototype.associateLinks = function() {
let sources = this.memory.energySources;
if(sources) {
for(let i = 0; i < sources.length; i++) {
if(sources[i].link) {
console.log('Source', sources[i].id, 'has link', sources[i].link);
}
else {
let source = Game.getObjectById(sources[i].id);
let closestLink = source.pos.findClosestByRange(FIND_MY_STRUCTURES, {filter: (structure) => structure.structureType == STRUCTURE_LINK});
console.log(source, source.pos, 'has closest link', closestLink, closestLink.pos, source.pos.getRangeTo(closestLink));
if(source.pos.getRangeTo(closestLink) < 3) {
sources[i].link = closestLink.id;
}
}
}
}
else {
console.log(this, 'has not saved its sources:', sources);
}
if(!this.memory.upgradeLink) {
let upgradeLink = this.controller.pos.findClosestByRange(FIND_MY_STRUCTURES, {filter: (structure) => structure.structureType == STRUCTURE_LINK});
console.log(this.controller, this.controller.pos, 'has closest link', upgradeLink, upgradeLink.pos, this.controller.pos.getRangeTo(upgradeLink));
this.memory.upgradeLink = upgradeLink.id;
}
else {
console.log('Controller has link', this.memory.upgradeLink);
}
}
//experimental automatic link placement - not currently used
var isWalkable = function(squareTerrain) {
return squareTerrain.terrain != 'wall';
}
var adjacentInOtherList = function(item, list) {
let x = item.x;
let y = item.y;
for(let i = 0; i < list.length; i++) {
if(list[i].x >= x-1 && list[i].x <= x+1 && list[i].y >= y-1 && list[i].y <= y+1) {
return true;
}
}
return false;
}
function scoreTiles(tileArray) {
let counts = lodash.countBy(tileArray, 'terrain');
let plains = counts['plain'] || 0;
let swamp = counts['swamp'] || 0;
return plains + swamp;
}
var scoreLocationAccess = function(best, current) {
if(!best) return current;
let bestAdjacent = this.lookForAtArea(LOOK_TERRAIN, best.y - 1, best.x - 1, best.y + 1, best.x + 1, true);
let newAdjacent = this.lookForAtArea(LOOK_TERRAIN, current.y - 1, current.x - 1, current.y + 1, current.x + 1, true);
if(scoreTiles(bestAdjacent) < scoreTiles(newAdjacent)) {
return current;
}
else {
return best;
}
}
Room.prototype.flagUpgrades = function() {
let controller = this.controller;
let withinThree = lodash.filter(this.lookForAtArea(LOOK_TERRAIN, controller.pos.y - 3, controller.pos.x - 3, controller.pos.y + 3, controller.pos.x + 3, true),
isWalkable);
for(let j = 0; j < withinThree.length; j++) {
this.createFlag(withinThree[j].x, withinThree[j].y, null, COLOR_YELLOW);
}
let topScoreLocation = lodash.reduce(withinThree, scoreLocationAccess, null, this);
this.createFlag(topScoreLocation.x, topScoreLocation.y, null, COLOR_GREEN);
}
Room.prototype.flagSources = function() {
let sources = this.find(FIND_SOURCES);
for(let i = 0; i < sources.length; i++) {
let source = sources[i];
let oneAwayWalkable = lodash.filter(this.lookForAtArea(LOOK_TERRAIN, source.pos.y - 1, source.pos.x - 1, source.pos.y + 1, source.pos.x + 1, true),
isWalkable);
let twoAway = [];
//top
twoAway.push(this.lookForAtArea(LOOK_TERRAIN, source.pos.y - 2, source.pos.x + 2, source.pos.y + 2, source.pos.x + 2, true));
//left
twoAway.push(this.lookForAtArea(LOOK_TERRAIN, source.pos.y - 2, source.pos.x - 1, source.pos.y - 2, source.pos.x + 1, true));
//right
twoAway.push(this.lookForAtArea(LOOK_TERRAIN, source.pos.y + 2, source.pos.x - 1, source.pos.y + 2, source.pos.x + 1, true));
//bottom
twoAway.push(this.lookForAtArea(LOOK_TERRAIN, source.pos.y - 2, source.pos.x - 2, source.pos.y + 2, source.pos.x - 2, true));
twoAway = lodash.flatten(twoAway);
let twoAwayWalkable = lodash.filter(twoAway, (item) => isWalkable(item) && adjacentInOtherList(item, oneAwayWalkable));
for(let j = 0; j < twoAwayWalkable.length; j++) {
this.createFlag(twoAwayWalkable[j].x, twoAwayWalkable[j].y, null, COLOR_BLUE);
}
for(let j = 0; j < oneAwayWalkable.length; j++) {
this.createFlag(oneAwayWalkable[j].x, oneAwayWalkable[j].y);
}
this.createFlag(source.pos.x, source.pos.y, null, COLOR_RED);
let topScoreLocation = lodash.reduce(twoAwayWalkable, scoreLocationAccess, null, this);
this.createFlag(topScoreLocation.x, topScoreLocation.y, null, COLOR_GREEN);
}
}
Room.prototype.deleteAllFlags = function() {
let flags = this.find(FIND_FLAGS);
for(let i = 0; i < flags.length; i++) {
flags[i].remove();
}
}