-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoom.js
More file actions
164 lines (136 loc) · 3.85 KB
/
Room.js
File metadata and controls
164 lines (136 loc) · 3.85 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
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
'use strict';
const Task = require('Task');
const TaskNetwork = require('TaskNetwork');
const ReactionManager = require('economy.reactionManager');
Room.prototype.myOwned = function () {
return this.controller && this.controller.my;
};
Room.prototype.hasInvaderAndNoTower = function(){
let towers = this.find(FIND_MY_STRUCTURES, {
filter: (s) => s.structureType === STRUCTURE_TOWER
});
let hostiles = this.find(FIND_HOSTILE_CREEPS, {
filter: c => c.owner.username === 'Invader'
});
return towers.length === 0 && hostiles.length > 0;
};
Room.prototype.run = function () {
this.cpuUsed = 0;
let cpu = Game.cpu.getUsed();
if(!this.myOwned()){
return;
}
if(this.hasInvaderAndNoTower()){
//suicide if invader and no tower
_.forEach(_.filter(Game.creeps, (c) => c.memory.home === this.name),
(c) => c.suicide());
}
let cManager = this.getColonyManager();
cManager.run();
let hManager = this.getHarvestManager();
hManager.operateLink();
let rManager = this.getResourceManager();
try{
rManager.run();
}catch (e) {
console.log(e.stack);
}
TaskNetwork.runTask(this);
this.cpuUsed += (Game.cpu.getUsed() - cpu);
};
const HarvestManager = require('economy.harvestManager');
const ResourceManager = require('economy.resourceManager');
/**
*
* @return {HarvestManager}
*/
Room.prototype.getHarvestManager = function () {
if (!this.harvestManager) {
this.harvestManager = new HarvestManager(this);
}
return this.harvestManager;
};
/**
*
* @return {ColonyManager}
*/
Room.prototype.getColonyManager = function () {
let ColonyManager = require('economy.colonyManager');
if (!this.colonyManager) {
this.colonyManager = new ColonyManager(this);
}
return this.colonyManager;
};
Room.prototype.countRole = function (role) {
if (this.roles === undefined) {
this.roles = {starter: 0};
for (let name in Game.creeps) {
if (!Game.creeps.hasOwnProperty(name) || Game.creeps[name].memory.home !== this.name) continue;
if (this.roles[Game.creeps[name].memory.role] === undefined) {
this.roles[Game.creeps[name].memory.role] = 1;
} else {
this.roles[Game.creeps[name].memory.role]++;
}
}
}
if(role === undefined){return _.sum(this.roles);}
return this.roles[role] || 0;
};
Room.prototype.incrementRole = function(role){
let c = this.countRole(role);
this.roles[role] = c + 1;
};
/**
*
* @return {RoomBuilder}
*/
Room.prototype.getRoomBuilder = function () {
let RoomBuilder = require('economy.roomBuilder');
if (!this.roomBuilder) {
this.roomBuilder = new RoomBuilder(this);
}
return this.roomBuilder;
};
/**
*
* @return {ResourceManager}
*/
Room.prototype.getResourceManager = function () {
if (!this.resourceManager) {
this.resourceManager = new ResourceManager(this);
}
return this.resourceManager;
};
/**
*
* @return {ReactionManager}
*/
Room.prototype.getReactionManager = function () {
if (!this.reactionManager) {
this.reactionManager = new ReactionManager(this);
}
return this.reactionManager;
};
Room.parseRoomName = function (roomName) {
let pattern = /([NSEW]|[0-9]+)/g;
let parts = roomName.match(pattern);
if(parts.length !== 4){
return;
}
return {
xSect: parts[0],
x: parseInt(parts[1]),
ySect: parts[2],
y: parseInt(parts[3])
}
};
Room.prototype.getTasks = function () {
return this.memory.tasks || [];
};
Room.prototype.addTask = function (task) {
if(!this.memory.tasks) this.memory.tasks = [];
this.memory.tasks.push(task);
};
Room.prototype.getCreeps = function () {
return _.filter(Game.creeps, (c) => c.memory.home === this.name);
};