Skip to content

Commit

Permalink
feat: strongholds deploy
Browse files Browse the repository at this point in the history
DEV-153
  • Loading branch information
o4kapuk committed Sep 19, 2019
1 parent 162e7f9 commit ec8bc68
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 16 deletions.
4 changes: 1 addition & 3 deletions src/game/structures.js
Expand Up @@ -1396,10 +1396,8 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
StructureInvaderCore.prototype.constructor = StructureInvaderCore;

utils.defineGameObjectProperties(StructureInvaderCore.prototype, data, {
my: () => false,
level: o => o.level,
ticksToUpgrade: o => o.nextUpgradeTime ? o.nextUpgradeTime - runtimeData.time : undefined,
ticksToDecay: o => o.nextDecayTime ? o.nextDecayTime - runtimeData.time : undefined,
ticksToDeploy: o => o.deployTime ? o.deployTime - runtimeData.time : undefined
});

StructureInvaderCore.prototype.toString = register.wrapFn(function() {
Expand Down
5 changes: 4 additions & 1 deletion src/processor.js
Expand Up @@ -110,6 +110,9 @@ function processRoom(roomId, {intents, roomObjects, users, roomTerrain, gameTime
if(object.type == 'powerBank' && gameTime > object.decayTime - 500) {
roomInfo.active = true;
}
if(object.type == 'deposit' && gameTime > object.decayTime - 500) {
roomInfo.active = true;
}
if(object.type == 'energy') {
roomInfo.active = true;
}
Expand Down Expand Up @@ -390,7 +393,7 @@ function processRoom(roomId, {intents, roomObjects, users, roomTerrain, gameTime
require('./processor/intents/storages/tick')(object, scope);
}

if(object.type == 'powerBank') {
if(object.type == 'powerBank' || object.type == 'deposit') {
if(gameTime >= object.decayTime-1) {
bulk.remove(object._id);
delete roomObjects[object._id];
Expand Down
2 changes: 1 addition & 1 deletion src/processor/intents/invader-core/create-creep.js
Expand Up @@ -51,7 +51,7 @@ module.exports = function(object, intent, scope) {
spawning: true,
fatigue: 0,
notifyWhenAttacked: false,
ageTime: object.nextDecayTime
ageTime: object.decayTime
};

bulk.insert(creep);
Expand Down
6 changes: 1 addition & 5 deletions src/processor/intents/invader-core/pretick.js
Expand Up @@ -15,11 +15,7 @@ module.exports = function(object, scope) {
}
};

if(!object.hits || (object.hits < object.hitsMax)) {
return;
}

const behavior = object.strongholdBehavior || 'default';
const behavior = object.deployTime ? 'deploy' : object.strongholdBehavior || 'default';
if(!stronghold.behaviors[behavior]) {
return;
}
Expand Down
76 changes: 70 additions & 6 deletions src/processor/intents/invader-core/stronghold/stronghold.js
Expand Up @@ -2,6 +2,7 @@ const _ = require('lodash'),
utils = require('../../../../utils'),
driver = utils.getDriver(),
C = driver.constants,
strongholds = driver.strongholds,
creeps = require('./creeps'),
fortifier = require('./fortifier'),
simpleMelee = require('./simple-melee');
Expand All @@ -17,15 +18,72 @@ const range = function(a, b) {
return Math.max(Math.abs(a.x-b.x), Math.abs(a.y-b.y));
};

const reserveController = function(context) {
const deployStronghold = function deployStronghold(context) {
const { scope, core, ramparts, bulk, gameTime } = context;
const { roomObjects } = scope;

if(core.deployTime && (core.deployTime <= gameTime)) {
const decayTime = gameTime + C.STRONGHOLD_DECAY_TICKS;

bulk.update(core, {
deployTime: null,
decayTime,
hits: C.INVADER_CORE_HITS,
hitsMax: C.INVADER_CORE_HITS
});

_.forEach(ramparts, rampart => {bulk.remove(rampart._id); delete roomObjects[rampart._id]});

const template = strongholds.templates[core.templateName];

const objectOptions = {};
objectOptions[C.STRUCTURE_RAMPART] = {
hits: C.STRONGHOLD_RAMPART_HITS[template.rewardLevel],
hitsMax: C.STRONGHOLD_RAMPART_HITS[template.rewardLevel],
nextDecayTime: decayTime
};
objectOptions[C.STRUCTURE_TOWER] = {
hits: C.TOWER_HITS,
hitsMax: C.TOWER_HITS,
store:{ energy: C.TOWER_CAPACITY },
storeCapacityResource: { energy: C.TOWER_CAPACITY },
actionLog: {attack: null, heal: null, repair: null}
};
objectOptions[C.STRUCTURE_CONTAINER] = {
notifyWhenAttacked: false,
hits: C.CONTAINER_HITS,
hitsMax: C.CONTAINER_HITS,
nextDecayTime: decayTime,
store: { energy: 0 },
storeCapacity: 0
};
objectOptions[C.STRUCTURE_ROAD] = {
notifyWhenAttacked: false,
hits: C.ROAD_HITS,
hitsMax: C.ROAD_HITS,
nextDecayTime: decayTime
};

const structures = _.map(template.structures, i => {
const s = _.merge(i, { x: 0+core.x+i.dx, y: 0+core.y+i.dy, room: core.room, user: core.user, strongholdId: core.strongholdId, decayTime }, objectOptions[i.type]||{});
delete s.dx;
delete s.dy;
return s;
});

_.forEach(structures, s => { if(s.type != C.STRUCTURE_INVADER_CORE) bulk.insert(s) });
}
};

const reserveController = function reserveController (context) {
const { core, intents, roomController } = context;

if(roomController) {
intents.set(core._id, 'reserveController', {id: roomController._id});
}
};

const refillTowers = function(context) {
const refillTowers = function refillTowers(context) {
const {core, intents, towers} = context;
const underchargedTowers = _.filter(towers, t => 2*t.store.energy <= t.storeCapacityResource.energy);
if(_.some(underchargedTowers)) {
Expand All @@ -39,7 +97,7 @@ const refillTowers = function(context) {
return false;
};

const refillCreeps = function(context) {
const refillCreeps = function refillCreeps(context) {
const {core, intents, defenders} = context;

const underchargedCreeps = _.filter(defenders, c => (c.storeCapacity > 0) && (2*c.store.energy <= c.storeCapacity));
Expand All @@ -54,7 +112,7 @@ const refillCreeps = function(context) {
return false;
};

const focusClosest = function(context) {
const focusClosest = function focusClosest(context) {
const {core, intents, defenders, hostiles, towers} = context;

if(!_.some(hostiles)) {
Expand Down Expand Up @@ -86,7 +144,7 @@ const focusClosest = function(context) {
return true;
};

const maintainCreep = function(name, setup, context, behavior) {
const maintainCreep = function maintainCreep(name, setup, context, behavior) {
const {core, intents, defenders} = context;
const creep = _.find(defenders, {name});
if(creep && behavior) {
Expand All @@ -101,7 +159,7 @@ const maintainCreep = function(name, setup, context, behavior) {
})
};

const antinuke = function(context) {
const antinuke = function antinuke(context) {
const { core, ramparts, roomObjects, bulk, gameTime } = context;
if(!!(gameTime % 10)) {
return;
Expand Down Expand Up @@ -132,6 +190,10 @@ const antinuke = function(context) {

module.exports = {
behaviors: {
'deploy': function(context) {
reserveController(context);
deployStronghold(context);
},
'default': function(context){
reserveController(context);
refillTowers(context);
Expand All @@ -144,6 +206,8 @@ module.exports = {
antinuke(context);
maintainCreep('fortifier', creeps['fortifier'], context, fortifier);
maintainCreep('defender1', creeps['weakDefender'], context, simpleMelee);

focusClosest(context);
}
}
};

0 comments on commit ec8bc68

Please sign in to comment.