-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroom.queues.js
182 lines (155 loc) · 5.9 KB
/
room.queues.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const ui = require('room.ui');
const roomQueue = {
energySites: {},
mineralSites: {},
constructionSites: [],
hostileCreeps: [],
};
let roomQueues = {};
let creepHeatmaps = {};
const canMoveToPos = (room, x, y) => {
let roomPos = new RoomPosition(x, y, room.name);
//console.log(roomPos);
//const road = roomPos.lookFor()
const terrain = new Room.Terrain(room.name);
switch(terrain.get(x, y)) {
case TERRAIN_MASK_WALL:
const structures = roomPos.lookFor(LOOK_STRUCTURES);
if (structures.length && _.filter(structures, (s) => s.structureType === STRUCTURE_ROAD)) {
room.visual.circle(roomPos,
{fill: 'yellow', radius: 0.15, stroke: 'orange'});
return true;
}
room.visual.circle(roomPos,
{fill: 'red', radius: 0.15, stroke: 'red'});
return false;
case TERRAIN_MASK_LAVA:
room.visual.circle(roomPos,
{fill: 'red', radius: 0.15, stroke: 'darkred'});
return false;
case TERRAIN_MASK_SWAMP:
room.visual.circle(roomPos,
{fill: 'green', radius: 0.15, stroke: 'darkgreen'});
return true;
case 0:
room.visual.circle(roomPos,
{fill: 'green', radius: 0.15, stroke: 'green'});
return true;
}
};
const getSurroundingCoordinates = (x, y) => {
return [
{ x: x, y: y - 1 },
{ x: x - 1, y: y - 1 },
{ x: x - 1, y: y },
{ x: x, y: y + 1 },
{ x: x + 1, y: y + 1 },
{ x: x + 1, y: y },
{ x: x - 1, y: y + 1 },
{ x: x + 1, y: y - 1 },
];
};
const findEnergySites = (room) => {
let sources = room.find(FIND_SOURCES);
let claimedSources = getClaimedSites(room, 'sourceId');
//console.log(claimedSources);
let energySites = { total: 0};
sources.map(source => {
room.visual.circle(source.pos,
{fill: 'blue', radius: 0.55, stroke: 'lightblue'});
/*
canMoveToPos(room, source.pos.x, source.pos.y - 1);
canMoveToPos(room, source.pos.x - 1, source.pos.y - 1);
canMoveToPos(room, source.pos.x - 1, source.pos.y);
canMoveToPos(room, source.pos.x + 1, source.pos.y);
canMoveToPos(room, source.pos.x, source.pos.y + 1);
canMoveToPos(room, source.pos.x + 1, source.pos.y + 1);
canMoveToPos(room, source.pos.x - 1, source.pos.y + 1);
canMoveToPos(room, source.pos.x + 1, source.pos.y - 1);*/
if (!energySites[source.id]) {
energySites[source.id] = [];
}
getSurroundingCoordinates(source.pos.x, source.pos.y).map(pos => {
//console.log('energy_left', source.energy > 0);
if (canMoveToPos(room, pos.x, pos.y)) {
energySites['total'] = energySites['total'] + 1;
}
if (source.energy > 0) {
//console.log('claimed', claimedSources.indexOf(source.id) === -1);
if (canMoveToPos(room, pos.x, pos.y) && claimedSources.indexOf(source.id) === -1) {
//if (source.energy > 0) {
energySites[source.id].push(new RoomPosition(pos.x, pos.y, room.name));
//}
} else {
let newClaimedSources = [];
let gotOne = false;
claimedSources.map(claimedSource => {
//console.log(claimedSource, '===', source.id, gotOne);
if (claimedSource !== source.id || gotOne) {
//console.log('push');
newClaimedSources.push(claimedSource);
} else {
//console.log('ignore');
gotOne = true;
}
});
claimedSources = newClaimedSources;
}
//console.log(claimedSources);
}
});
//console.log(energySites[source.id].length);
if (energySites[source.id].length <= 0) {
//console.log('delete');
delete(energySites[source.id]);
}
});
//console.log(JSON.stringify(energySites));
return energySites;
};
const getClaimedSites = (room, jobType) => {
let assignedSites = [];
//console.log(builders);
for (let name in Game.creeps) {
if (/*creep.memory.role === 'janitor' && */Game.creeps[name].memory[jobType]) {
assignedSites.push(Game.creeps[name].memory[jobType]);
}
//console.log(name);
}
return assignedSites;
};
const findConstructionSites = (room) => {
return room.find(FIND_CONSTRUCTION_SITES) || [];
};
const findHostileCreeps = (room) => {
return room.find(FIND_HOSTILE_CREEPS) || [];
};
const queues = {
run: (roomName) => {
const room = Game.rooms[roomName];
let tempRoomQueue = {...roomQueue};
tempRoomQueue['energySites'] = findEnergySites(room);
tempRoomQueue['constructionSites'] = findConstructionSites(room);
tempRoomQueue['hostileCreeps'] = findHostileCreeps(room);
//console.log('hostiles', tempRoomQueue['hostileCreeps'].length);
roomQueues = {...roomQueues, [room.name]: tempRoomQueue};
//console.log(JSON.stringify(roomQueues));
},
getRoomQueue(room) {
return roomQueues[room] || roomQueue;
},
getCreepHeatmap(roomName) {
return creepHeatmaps[roomName] || {};
},
feedCreepHeatmap: (roomName, x, y) => {
let tempCreepHeatmap = {...creepHeatmaps[roomName]};
let cordKey = x + '_' + y;
if (tempCreepHeatmap[cordKey]) {
tempCreepHeatmap[cordKey] = tempCreepHeatmap[cordKey] + 1;
} else {
tempCreepHeatmap[cordKey] = 1;
}
creepHeatmaps = {...creepHeatmaps, [roomName]: tempCreepHeatmap};
}
};
module.exports = queues;