Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable spawning direction #61

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 47 additions & 2 deletions src/game/structures.js
Expand Up @@ -910,7 +910,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
name: (o) => o.user == runtimeData.user._id ? o.name : undefined,
energy: (o) => o.energy,
energyCapacity: (o) => o.energyCapacity,
spawning: (o) => o.spawning || null
spawning: (o) => o.spawning ? new StructureSpawn.Spawning(o.spawning, o.user == runtimeData.user._id) : null,
});

Object.defineProperty(StructureSpawn.prototype, 'memory', {
Expand Down Expand Up @@ -1111,6 +1111,21 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {

let energyStructures = options.energyStructures && _.uniq(_.map(options.energyStructures, 'id'));

let directions = options.directions;
if(directions !== undefined) {
if(!_.isArray(directions)) {
return C.ERR_INVALID_ARGS;
}
// convert directions to numbers, eliminate duplicates
directions = _.uniq(_.map(directions, d => +d));
if(directions.length > 0) {
// bail if any numbers are out of bounds or non-integers
if(!_.all(directions, (direction) => direction >= 1 && direction <= 8 && direction === (direction | 0))) {
return C.ERR_INVALID_ARGS;
}
}
}

if(!this.my) {
return C.ERR_NOT_OWNER;
}
Expand Down Expand Up @@ -1228,7 +1243,7 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
}
});

intents.set(this.id, 'createCreep', {name, body, energyStructures});
intents.set(this.id, 'createCreep', {name, body, energyStructures, directions});

return C.OK;
});
Expand Down Expand Up @@ -1355,6 +1370,36 @@ exports.make = function(_runtimeData, _intents, _register, _globals) {
globals.StructureSpawn = StructureSpawn;
globals.Spawn = StructureSpawn;

/**
* SpawnSpawning
* @param {Number} spawnId
* @param {Object} properties
* @constructor
*/
StructureSpawn.Spawning = register.wrapFn(function(spawningObject, exposePrivate = false) {
this.spawnId = spawningObject.spawnId;
this.name = exposePrivate ? spawningObject.name : null;
this.needTime = spawningObject.needtime;
this.remainingTime = spawningObject.remainingTime;
this.directions = exposePrivate ? spawningObject.directions : null;
});

StructureSpawn.Spawning.prototype.setDirections = register.wrapFn(function(directions) {
if(!(new StructureSpawn(this.spawnId).my)) {
return C.ERR_NOT_OWNER;
}
if(_.isArray(directions) && directions.length > 0) {
// convert directions to numbers, eliminate duplicates
directions = _.uniq(_.map(directions, e => +e));
// bail if any numbers are out of bounds or non-integers
if(!_.any(directions, (direction)=>direction < 1 || direction > 8 || direction !== (direction | 0))) {
intents.set(this.spawnId, 'setSpawnDirections', {directions});
return C.OK;
}
}
return C.ERR_INVALID_ARGS;
});

/**
* StructureNuker
* @param id
Expand Down
26 changes: 24 additions & 2 deletions src/processor/intents/spawns/_born-creep.js
Expand Up @@ -9,7 +9,10 @@ module.exports = function(spawn, creep, roomObjects, roomTerrain, bulk, stats) {
var newX, newY, isOccupied, hostileOccupied;
var checkObstacleFn = (i) => _.contains(C.OBSTACLE_OBJECT_TYPES, i.type) && i.x == newX && i.y == newY;

for (var direction = 1; direction <= 8; direction++) {
const directions = spawn.spawning.directions || [1,2,3,4,5,6,7,8];
const otherDirections = _.difference([1,2,3,4,5,6,7,8], directions);
// find the first direction where the creep can spawn
for (var direction of directions) {
var [dx,dy] = utils.getOffsetsByDirection(direction);

newX = spawn.x + dx;
Expand All @@ -22,11 +25,13 @@ module.exports = function(spawn, creep, roomObjects, roomTerrain, bulk, stats) {
break;
}

// remember the first direction where we found a hostile creep
if(!hostileOccupied) {
hostileOccupied = _.find(roomObjects, i => i.x == newX && i.y == newY && i.type == 'creep' && i.user != spawn.user);
}
}

// if we found a place to spawn, do so
if(!isOccupied) {
bulk.update(creep, {
x: newX,
Expand All @@ -35,7 +40,24 @@ module.exports = function(spawn, creep, roomObjects, roomTerrain, bulk, stats) {
});
return true;
}
else if(hostileOccupied) {

// spawn is surrounded, spawnstomp the first hostile we found above, unless...
if(hostileOccupied) {
// bail if there's an opening we could spawn to but chose not to
for (var direction of otherDirections) {
var [dx,dy] = utils.getOffsetsByDirection(direction);

newX = spawn.x + dx;
newY = spawn.y + dy;
isOccupied = _.any(roomObjects, checkObstacleFn) ||
utils.checkTerrain(roomTerrain, newX, newY, C.TERRAIN_MASK_WALL) ||
movement.isTileBusy(newX, newY);

if (!isOccupied) {
return false;
}
}

require('../creeps/_die')(hostileOccupied, roomObjects, bulk, stats);
bulk.update(creep, {
x: hostileOccupied.x,
Expand Down
19 changes: 18 additions & 1 deletion src/processor/intents/spawns/create-creep.js
Expand Up @@ -16,6 +16,21 @@ module.exports = function(spawn, intent, roomObjects, roomTerrain, bulk, bulkUse
return;
}

let directions = intent.directions;
if(directions !== undefined) {
if(!_.isArray(directions)) {
return;
}
// convert directions to numbers, eliminate duplicates
directions = _.uniq(_.map(directions, e => +e));
if(directions.length > 0) {
// bail if any numbers are out of bounds or non-integers
if(!_.all(directions, direction => direction >= 1 && direction <= 8 && direction === (direction | 0))) {
return;
}
}
}

intent.body = intent.body.slice(0, C.MAX_CREEP_SIZE);

var cost = utils.calcCreepCost(intent.body);
Expand All @@ -31,9 +46,11 @@ module.exports = function(spawn, intent, roomObjects, roomTerrain, bulk, bulkUse

bulk.update(spawn, {
spawning: {
spawnId: spawn._id,
name: intent.name,
needTime: C.CREEP_SPAWN_TIME * intent.body.length,
remainingTime: C.CREEP_SPAWN_TIME * intent.body.length
remainingTime: C.CREEP_SPAWN_TIME * intent.body.length,
directions
}
});

Expand Down
3 changes: 3 additions & 0 deletions src/processor/intents/spawns/intents.js
Expand Up @@ -12,4 +12,7 @@ module.exports = function(object, objectIntents, roomObjects, roomTerrain, bulk,
if(objectIntents.recycleCreep)
require('./recycle-creep')(object, objectIntents.recycleCreep, roomObjects, roomTerrain, bulk, bulkUsers, roomController, stats, gameTime);

if(objectIntents.setSpawnDirections)
require('./set-spawn-directions')(object, objectIntents.setSpawnDirections, roomObjects, roomTerrain, bulk);

};
18 changes: 18 additions & 0 deletions src/processor/intents/spawns/set-spawn-directions.js
@@ -0,0 +1,18 @@
var _ = require('lodash'),
utils = require('../../../utils'),
driver = utils.getDriver(),
C = driver.constants;

module.exports = function(spawn, intent, roomObjects, roomTerrain, bulk) {
if(spawn.type != 'spawn')
return;
var directions = intent.directions;
if(_.isArray(directions) && directions.length > 0) {
// convert directions to numbers, eliminate duplicates
directions = _.uniq(_.map(directions, e => +e));
// bail if any numbers are out of bounds or non-integers
if(!_.any(directions, (direction)=>direction < 1 || direction > 8 || direction !== (direction | 0))) {
bulk.update(spawn, {spawning:{directions}});
}
}
};
8 changes: 7 additions & 1 deletion src/utils.js
Expand Up @@ -665,7 +665,8 @@ exports.storeIntents = function(userId, userIntents, userRuntimeData) {
objectIntents.createCreep = {
name: ""+objectIntentsResult.createCreep.name,
body: _.filter(objectIntentsResult.createCreep.body, (i) => _.contains(C.BODYPARTS_ALL, i)),
energyStructures: objectIntentsResult.createCreep.energyStructures
energyStructures: objectIntentsResult.createCreep.energyStructures,
directions: objectIntentsResult.createCreep.directions
};
}
if(objectIntentsResult.renewCreep) {
Expand Down Expand Up @@ -805,6 +806,11 @@ exports.storeIntents = function(userId, userIntents, userRuntimeData) {
sign: (""+objectIntentsResult.signController.sign).substring(0,100)
};
}
if(objectIntentsResult.setSpawnDirections) {
objectIntents.setSpawnDirections = {
directions: objectIntentsResult.setSpawnDirections.directions
};
}


for(var iCustomType in driver.config.customIntentTypes) {
Expand Down