-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.js
175 lines (141 loc) · 5.09 KB
/
main.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
const roles = [
require("role.harvester"),
require("role.miner"),
require("role.linkCollector"),
require("role.upgrader"),
require("role.builder"),
require("role.claimer"),
require("role.conqueror"),
require("role.reserver"),
require("role.carrier"),
require("role.guard"),
require("role.defender"),
require("role.ranger"),
require("role.reloader"),
require("role.flagHunter"),
require("role.attacker"),
require("role.dismantler"),
require("role.healer"),
require("role.hopper"),
require("role.observer"),
require("role.discoverer"),
require("role.trader"),
require("role.scooper"),
require("role.powerFarmer"),
require("role.powerRefiner"),
require("role.mason"),
require("role.scientist"),
require("role.factoryWorker"),
require("role.nukeOperator"),
require("role.downgrader"),
];
const powerRoles = [
require("powerRole.factoryOperator")
];
const ConstructionSitesCleaner = require("cleaner.constructionSites");
const logistic = require("helper.logistic");
const globalStatistics = require("global.statistics");
const profitVisual = require("visual.roomProfit");
const PixelTrader = require("global.pixelTrader");
const SegmentExport = require("global.segmentExport");
const ShardTravel = require("global.shardTravel");
const TradeLogger = require("global.tradeLogger");
global.AbsolutePosition = require("global.absolutePosition");
global.ExpansionPlanner = require("global.expansionPlanner");
global.FriendList = require("global.friendList");
global.MapKnowledge = require("global.mapKnowledge");
global.RoomUI = require("global.roomui");
global.SegmentScanner = require("global.segmentScanner");
require("global.operation");
require("patch.controller");
require("patch.creep");
require("patch.powerCreep");
require("patch.room");
const profiler = require('screeps-profiler');
profiler.enable();
function suppressErrors(callback) {
try {
callback();
} catch(error) {
console.log('<span style="color: #faa">' + error.stack + '</span>');
}
}
function runCreeps() {
for(let role of roles) {
let creeps = _.filter(Game.creeps, (creep) => creep.memory.role == role.name && creep.ticksToLive !== undefined);
for(let creep of creeps) {
suppressErrors(() => role.run(creep));
}
}
for(let roleClass of powerRoles) {
let creeps = _.filter(Game.powerCreeps, (creep) => creep.memory.role == roleClass.name);
for(let creep of creeps) {
let role = new roleClass(creep)
if(creep.ticksToLive) {
suppressErrors(() => role.run());
} else {
suppressErrors(() => role.runUnspawned());
}
}
}
}
runCreeps = profiler.registerFN(runCreeps, 'Creep Actions');
module.exports.loop = function() {
profiler.wrap(function() {
globalStatistics.initialize();
if(Memory.lastCompletedTick < Game.time - 1) {
Memory.stats.skippedTicks += 1;
}
if(Game.time % 10 === 0 && Game.cpu.bucket < 3000) {
console.log("Bucket at " + Game.cpu.bucket);
}
if(Game.time % 100 == 50) {
for(let name in Memory.creeps) {
if(!Game.creeps[name]) {
delete Memory.creeps[name];
}
}
}
if(Game.time % 10000 === 0) {
logistic.cleanupCaches();
}
suppressErrors(() => ShardTravel.loadArrivals());
runCreeps();
suppressErrors(() => ShardTravel.announceDepartures());
suppressErrors(() => MapKnowledge.updateKnowledge());
if(Game.cpu.bucket < 1000 && Game.time % 2 === 0) return;
suppressErrors(() => new SegmentScanner().run());
for(let operation of Operation.operations) {
suppressErrors(() => operation.run());
}
for(let roomName in Game.rooms) {
let room = Game.rooms[roomName];
if(room.ai()) {
suppressErrors(() => room.ai().run());
}
}
new ConstructionSitesCleaner().run();
suppressErrors(() => new ExpansionPlanner().run());
suppressErrors(() => new TradeLogger().logTrades());
suppressErrors(() => new PixelTrader().run());
for(let operation of Operation.operations) {
suppressErrors(() => operation.drawVisuals());
}
suppressErrors(() => new SegmentExport().run());
for(let ui of RoomUI.all) {
suppressErrors(() => ui.render());
}
suppressErrors(() => MapKnowledge.drawMapVisuals());
suppressErrors(() => Memory.debugRoomScores && new ExpansionPlanner().drawRoomScores());
if(Memory.generatePixels && Game.cpu.bucket >= 9999) {
Game.cpu.generatePixel();
}
ExpansionPlanner.sampleCpuUsage();
globalStatistics.run();
profitVisual.run();
RawMemory.setActiveSegments([98, 99]);
RawMemory.setPublicSegments([98]);
RawMemory.setDefaultPublicSegment(98);
Memory.lastCompletedTick = Game.time;
});
}