Skip to content

Commit de3c823

Browse files
committed
Implement cache for active structures in rooms
1 parent c7818a7 commit de3c823

File tree

11 files changed

+33
-125
lines changed

11 files changed

+33
-125
lines changed

src/game/game.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@
218218
};
219219

220220
var c = {};
221-
222221
for(var i in runtimeData.roomObjects) {
223222
var object = runtimeData.roomObjects[i];
224223

@@ -270,7 +269,7 @@
270269
addObjectToFindCache(register, C.FIND_MY_SPAWNS, register.spawns[i], object);
271270
addObjectToFindCache(register, C.FIND_HOSTILE_SPAWNS, register.spawns[i], object);
272271
}
273-
272+
274273
}
275274
if (!object.off && (object.type == 'extension' || object.type == 'spawn') && (object.user == runtimeData.user._id)) {
276275
register.rooms[object.room].energyAvailable += object.energy;
@@ -329,7 +328,7 @@
329328
}
330329

331330
}
332-
331+
333332
runtimeData.flags.forEach(flagRoomData => {
334333

335334
var data = flagRoomData.data.split("|");

src/game/structures.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
194194
if(!this.room.controller) {
195195
return false;
196196
}
197-
return utils.checkStructureAgainstController(data(this.id), register.objectsByRoom[data(this.id).room], data(this.room.controller.id));
197+
198+
return !data(this.id).off
198199
});
199200

200201
Object.defineProperty(globals, 'Structure', {enumerable: true, value: Structure});
@@ -434,7 +435,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
434435
if(this.cooldown > 0) {
435436
return C.ERR_TIRED;
436437
}
437-
if(!utils.checkStructureAgainstController(data(this.id), register.objectsByRoom[data(this.id).room], data(this.room.controller.id))) {
438+
if(data(this.id).off) {
438439
return C.ERR_RCL_NOT_ENOUGH;
439440
}
440441
if(!lab1 || !lab1.id || !register.structures[lab1.id] ||
@@ -469,7 +470,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
469470
if(!this.my) {
470471
return C.ERR_NOT_OWNER;
471472
}
472-
if(!utils.checkStructureAgainstController(data(this.id), register.objectsByRoom[data(this.id).room], data(this.room.controller.id))) {
473+
if(data(this.id).off) {
473474
return C.ERR_RCL_NOT_ENOUGH;
474475
}
475476
if(!target || !target.id || !register.creeps[target.id] || !(target instanceof globals.Creep)) {
@@ -504,7 +505,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
504505
if(!this.my || !target.my) {
505506
return C.ERR_NOT_OWNER;
506507
}
507-
if(!utils.checkStructureAgainstController(data(this.id), register.objectsByRoom[data(this.id).room], data(this.room.controller.id))) {
508+
if(data(this.id).off) {
508509
return C.ERR_RCL_NOT_ENOUGH;
509510
}
510511
if(this.cooldown > 0) {
@@ -568,7 +569,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
568569
if(!this.room.controller) {
569570
return C.ERR_RCL_NOT_ENOUGH;
570571
}
571-
if(!utils.checkStructureAgainstController(data(this.id), register.objectsByRoom[data(this.id).room], data(this.room.controller.id))) {
572+
if(data(this.id).off) {
572573
return C.ERR_RCL_NOT_ENOUGH;
573574
}
574575
}
@@ -628,7 +629,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
628629
if(!_.isString(roomName) || !/^(W|E)\d+(S|N)\d+$/.test(roomName)) {
629630
return C.ERR_INVALID_ARGS;
630631
}
631-
if(!utils.checkStructureAgainstController(data(this.id), register.objectsByRoom[data(this.id).room], data(this.room.controller.id))) {
632+
if(data(this.id).off) {
632633
return C.ERR_RCL_NOT_ENOUGH;
633634
}
634635

@@ -689,7 +690,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
689690
if(!this.my) {
690691
return C.ERR_NOT_OWNER;
691692
}
692-
if(!utils.checkStructureAgainstController(data(this.id), register.objectsByRoom[data(this.id).room], data(this.room.controller.id))) {
693+
if(data(this.id).off) {
693694
return C.ERR_RCL_NOT_ENOUGH;
694695
}
695696
if(!this.power || this.energy < C.POWER_SPAWN_ENERGY_RATIO) {
@@ -789,7 +790,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
789790
if(!this.my) {
790791
return C.ERR_NOT_OWNER;
791792
}
792-
if(!utils.checkStructureAgainstController(data(this.id), register.objectsByRoom[data(this.id).room], data(this.room.controller.id))) {
793+
if(data(this.id).off) {
793794
return C.ERR_RCL_NOT_ENOUGH;
794795
}
795796
if(!/^(W|E)\d+(N|S)\d+$/.test(targetRoomName)) {
@@ -853,7 +854,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
853854
if(data(this.id).energy < C.TOWER_ENERGY_COST) {
854855
return C.ERR_NOT_ENOUGH_ENERGY;
855856
}
856-
if(!utils.checkStructureAgainstController(data(this.id), register.objectsByRoom[data(this.id).room], data(this.room.controller.id))) {
857+
if(data(this.id).off) {
857858
return C.ERR_RCL_NOT_ENOUGH;
858859
}
859860

@@ -872,7 +873,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
872873
if(data(this.id).energy < C.TOWER_ENERGY_COST) {
873874
return C.ERR_NOT_ENOUGH_ENERGY;
874875
}
875-
if(!utils.checkStructureAgainstController(data(this.id), register.objectsByRoom[data(this.id).room], data(this.room.controller.id))) {
876+
if(data(this.id).off) {
876877
return C.ERR_RCL_NOT_ENOUGH;
877878
}
878879

@@ -892,7 +893,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
892893
if(data(this.id).energy < C.TOWER_ENERGY_COST) {
893894
return C.ERR_NOT_ENOUGH_ENERGY;
894895
}
895-
if(!utils.checkStructureAgainstController(data(this.id), register.objectsByRoom[data(this.id).room], data(this.room.controller.id))) {
896+
if(data(this.id).off) {
896897
return C.ERR_RCL_NOT_ENOUGH;
897898
}
898899

@@ -1461,7 +1462,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
14611462
if(this.cooldown > 0) {
14621463
return C.ERR_TIRED;
14631464
}
1464-
if(!utils.checkStructureAgainstController(data(this.id), register.objectsByRoom[data(this.id).room], data(this.room.controller.id))) {
1465+
if(data(this.id).off) {
14651466
return C.ERR_RCL_NOT_ENOUGH;
14661467
}
14671468
var [tx,ty] = utils.roomNameToXY(pos.roomName);

src/processor.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ function processRoom(roomId, {intents, roomObjects, users, roomTerrain, gameTime
2020
hasNewbieWalls = false,
2121
stats = driver.getRoomStatsUpdater(roomId),
2222
objectsToHistory = {},
23-
roomSpawns = [], roomExtensions = [],
2423
oldRoomInfo = _.clone(roomInfo);
2524

2625
roomInfo.active = false;
@@ -34,6 +33,7 @@ function processRoom(roomId, {intents, roomObjects, users, roomTerrain, gameTime
3433

3534
let scope = {roomObjects, roomTerrain, bulk, bulkUsers, stats, flags, bulkFlags, gameTime, roomInfo, users, eventLog};
3635

36+
3737
_.forEach(roomObjects, (object) => {
3838
if(!object) {
3939
return;
@@ -106,21 +106,10 @@ function processRoom(roomId, {intents, roomObjects, users, roomTerrain, gameTime
106106
hasNewbieWalls = true;
107107
}
108108

109-
if(object.type == 'extension') {
110-
roomExtensions.push(object);
111-
}
112-
if(object.type == 'spawn') {
113-
roomSpawns.push(object);
114-
}
115-
116109
driver.config.emit('processObject',object, roomObjects, roomTerrain, gameTime, roomInfo, bulk, bulkUsers);
117110

118111
});
119112

120-
if(roomSpawns.length || roomExtensions.length) {
121-
require('./processor/intents/_calc_spawns')(roomSpawns, roomExtensions, scope);
122-
}
123-
124113
movement.init(roomObjects, roomTerrain);
125114

126115
if (intents) {
@@ -230,6 +219,8 @@ function processRoom(roomId, {intents, roomObjects, users, roomTerrain, gameTime
230219
var resultPromises = [];
231220
var userVisibility = {};
232221

222+
let ownedTypes = ['extension','spawn','link','storage','tower','observer','powerSpawn','extractor','terminal','lab','nuker']
223+
let ownedObjects = {}
233224
_.forEach(roomObjects, (object) => {
234225

235226
if(!object || object._skip) {
@@ -316,6 +307,10 @@ function processRoom(roomId, {intents, roomObjects, users, roomTerrain, gameTime
316307
}
317308
}
318309

310+
if(ownedTypes.includes(object.type)) {
311+
(ownedObjects[object.type] = ownedObjects[object.type] || []).push(object)
312+
}
313+
319314
if (object.user) {
320315
//userVisibility[object.user] = true;
321316

@@ -354,6 +349,11 @@ function processRoom(roomId, {intents, roomObjects, users, roomTerrain, gameTime
354349
}
355350
});
356351

352+
353+
if(scope.roomController) {
354+
require('./processor/intents/_check_active_structures')(ownedObjects, scope);
355+
}
356+
357357
/*for(var user in userVisibility) {
358358
resultPromises.push(core.setUserRoomVisibility(user, roomId));
359359
}*/

src/processor/intents/_calc_spawns.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/processor/intents/creeps/harvest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ module.exports = function(object, intent, scope) {
7777
if(extractor.user && extractor.user != object.user) {
7878
return;
7979
}
80-
if(!utils.checkStructureAgainstController(extractor, roomObjects, roomController)) {
80+
if(extractor.off) {
8181
return;
8282
}
8383
if(extractor.cooldown) {

src/processor/intents/labs/unboost-creep.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = function(object, intent, scope) {
1212
if(!target || target.type != 'creep' || target.user != object.user) {
1313
return;
1414
}
15-
if(!utils.checkStructureAgainstController(object, roomObjects, roomController)) {
15+
if(object.off) {
1616
return;
1717
}
1818
if(Math.abs(target.x - object.x) > 1 || Math.abs(target.y - object.y) > 1) {

src/processor/intents/links/transfer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module.exports = function(object, intent, {roomObjects, bulk, roomController, ev
3232
if(object.cooldown > 0) {
3333
return;
3434
}
35-
if(!utils.checkStructureAgainstController(object, roomObjects, roomController)) {
35+
if(object.off) {
3636
return;
3737
}
3838
targetTotal = target.energy;

src/processor/intents/nukers/launch-nuke.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var _ = require('lodash'),
77

88
module.exports = function(object, intent, {roomObjects, bulk, roomController, gameTime, roomInfo}) {
99

10-
if(!utils.checkStructureAgainstController(object, roomObjects, roomController)) {
10+
if(object.off) {
1111
return;
1212
}
1313
if(object.G < object.GCapacity || object.energy < object.energyCapacity) {

src/processor/intents/power-spawns/process-power.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = function(object, intent, {roomObjects, bulk, bulkUsers, roomCon
99
if(object.type != 'powerSpawn')
1010
return;
1111

12-
if(!utils.checkStructureAgainstController(object, roomObjects, roomController)) {
12+
if(object.off) {
1313
return;
1414
}
1515

src/processor/intents/spawns/create-creep.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = function(spawn, intent, scope) {
1414
if(spawn.type != 'spawn')
1515
return;
1616

17-
if(!utils.checkStructureAgainstController(spawn, roomObjects, roomController)) {
17+
if(spawn.off) {
1818
return;
1919
}
2020

0 commit comments

Comments
 (0)