Remove 2 job & refactor tower spec
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -79,6 +79,7 @@ GitHub.sublime-settings
|
|||||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
|
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
|
||||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||||
|
|
||||||
|
.idea
|
||||||
# User-specific stuff:
|
# User-specific stuff:
|
||||||
.idea/**/workspace.xml
|
.idea/**/workspace.xml
|
||||||
.idea/**/tasks.xml
|
.idea/**/tasks.xml
|
||||||
|
|||||||
14
src/main.js
14
src/main.js
@@ -4,32 +4,26 @@ require('prototype.tower');
|
|||||||
require('prototype.spawn');
|
require('prototype.spawn');
|
||||||
|
|
||||||
module.exports.loop = function() {
|
module.exports.loop = function() {
|
||||||
// check for memory entries of died creeps by iterating over Memory.creeps
|
// Memory cleanup
|
||||||
for (let name in Memory.creeps) {
|
for (let name in Memory.creeps) {
|
||||||
// and checking if the creep is still alive
|
|
||||||
if (Game.creeps[name] == undefined) {
|
if (Game.creeps[name] == undefined) {
|
||||||
// if not, delete the memory entry
|
|
||||||
delete Memory.creeps[name];
|
delete Memory.creeps[name];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// for each creeps
|
// Creeps tasks
|
||||||
for (let name in Game.creeps) {
|
for (let name in Game.creeps) {
|
||||||
// run creep logic
|
|
||||||
Game.creeps[name].runRole();
|
Game.creeps[name].runRole();
|
||||||
}
|
}
|
||||||
|
|
||||||
// find all towers
|
// Tower Action
|
||||||
var towers = _.filter(Game.structures, s => s.structureType == STRUCTURE_TOWER);
|
var towers = _.filter(Game.structures, s => s.structureType == STRUCTURE_TOWER);
|
||||||
// for each tower
|
|
||||||
for (let tower of towers) {
|
for (let tower of towers) {
|
||||||
// run tower logic
|
|
||||||
tower.defend();
|
tower.defend();
|
||||||
}
|
}
|
||||||
|
|
||||||
// for each spawn
|
// Colony Action
|
||||||
for (let spawnName in Game.spawns) {
|
for (let spawnName in Game.spawns) {
|
||||||
// run spawn logic
|
|
||||||
Game.spawns[spawnName].spawnCreepsIfNecessary();
|
Game.spawns[spawnName].spawnCreepsIfNecessary();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
2
src/prototype.creep.js
vendored
2
src/prototype.creep.js
vendored
@@ -3,11 +3,9 @@ var roles = {
|
|||||||
upgrader: require('role.upgrader'),
|
upgrader: require('role.upgrader'),
|
||||||
builder: require('role.builder'),
|
builder: require('role.builder'),
|
||||||
repairer: require('role.repairer'),
|
repairer: require('role.repairer'),
|
||||||
wallRepairer: require('role.wallRepairer'),
|
|
||||||
longDistanceHarvester: require('role.longDistanceHarvester'),
|
longDistanceHarvester: require('role.longDistanceHarvester'),
|
||||||
claimer: require('role.claimer'),
|
claimer: require('role.claimer'),
|
||||||
miner: require('role.miner'),
|
miner: require('role.miner'),
|
||||||
lorry: require('role.lorry')
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Creep.prototype.runRole =
|
Creep.prototype.runRole =
|
||||||
|
|||||||
38
src/prototype.spawn.js
vendored
38
src/prototype.spawn.js
vendored
@@ -1,4 +1,4 @@
|
|||||||
var listOfRoles = ['harvester', 'lorry', 'claimer', 'upgrader', 'repairer', 'builder', 'wallRepairer'];
|
var listOfRoles = ['harvester', 'claimer', 'upgrader', 'repairer', 'builder'];
|
||||||
|
|
||||||
// create a new function for StructureSpawn
|
// create a new function for StructureSpawn
|
||||||
StructureSpawn.prototype.spawnCreepsIfNecessary =
|
StructureSpawn.prototype.spawnCreepsIfNecessary =
|
||||||
@@ -22,19 +22,9 @@ StructureSpawn.prototype.spawnCreepsIfNecessary =
|
|||||||
|
|
||||||
// if no harvesters are left AND either no miners or no lorries are left
|
// if no harvesters are left AND either no miners or no lorries are left
|
||||||
// create a backup creep
|
// create a backup creep
|
||||||
if (numberOfCreeps['harvester'] == 0 && numberOfCreeps['lorry'] == 0) {
|
if (numberOfCreeps['harvester'] == 0) {
|
||||||
// if there are still miners or enough energy in Storage left
|
|
||||||
if (numberOfCreeps['miner'] > 0 ||
|
|
||||||
(room.storage != undefined && room.storage.store[RESOURCE_ENERGY] >= 150 + 550)) {
|
|
||||||
// create a lorry
|
|
||||||
name = this.createLorry(150);
|
|
||||||
}
|
|
||||||
// if there is no miner and not enough energy in Storage left
|
|
||||||
else {
|
|
||||||
// create a harvester because it can work on its own
|
|
||||||
name = this.createCustomCreep(room.energyAvailable, 'harvester');
|
name = this.createCustomCreep(room.energyAvailable, 'harvester');
|
||||||
}
|
}
|
||||||
}
|
|
||||||
// if no backup creep is required
|
// if no backup creep is required
|
||||||
else {
|
else {
|
||||||
// check if all sources have miners
|
// check if all sources have miners
|
||||||
@@ -76,12 +66,7 @@ StructureSpawn.prototype.spawnCreepsIfNecessary =
|
|||||||
}
|
}
|
||||||
// if no claim order was found, check other roles
|
// if no claim order was found, check other roles
|
||||||
else if (numberOfCreeps[role] < this.memory.minCreeps[role]) {
|
else if (numberOfCreeps[role] < this.memory.minCreeps[role]) {
|
||||||
if (role == 'lorry') {
|
|
||||||
name = this.createLorry(150);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
name = this.createCustomCreep(maxEnergy, role);
|
name = this.createCustomCreep(maxEnergy, role);
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -180,22 +165,3 @@ StructureSpawn.prototype.createMiner =
|
|||||||
return this.createCreep([WORK, WORK, WORK, WORK, WORK, MOVE], undefined,
|
return this.createCreep([WORK, WORK, WORK, WORK, WORK, MOVE], undefined,
|
||||||
{ role: 'miner', sourceId: sourceId });
|
{ role: 'miner', sourceId: sourceId });
|
||||||
};
|
};
|
||||||
|
|
||||||
// create a new function for StructureSpawn
|
|
||||||
StructureSpawn.prototype.createLorry =
|
|
||||||
function (energy) {
|
|
||||||
// create a body with twice as many CARRY as MOVE parts
|
|
||||||
var numberOfParts = Math.floor(energy / 150);
|
|
||||||
// make sure the creep is not too big (more than 50 parts)
|
|
||||||
numberOfParts = Math.min(numberOfParts, Math.floor(50 / 3));
|
|
||||||
var body = [];
|
|
||||||
for (let i = 0; i < numberOfParts * 2; i++) {
|
|
||||||
body.push(CARRY);
|
|
||||||
}
|
|
||||||
for (let i = 0; i < numberOfParts; i++) {
|
|
||||||
body.push(MOVE);
|
|
||||||
}
|
|
||||||
|
|
||||||
// create creep with the created body and the role 'lorry'
|
|
||||||
return this.createCreep(body, undefined, { role: 'lorry', working: false });
|
|
||||||
};
|
|
||||||
8
src/prototype.tower.js
vendored
8
src/prototype.tower.js
vendored
@@ -1,15 +1,15 @@
|
|||||||
StructureTower.prototype.defend =
|
StructureTower.prototype.defend =
|
||||||
function () {
|
function () {
|
||||||
// find closes hostile creep
|
|
||||||
|
// Defend Base
|
||||||
var target = this.pos.findClosestByRange(FIND_HOSTILE_CREEPS);
|
var target = this.pos.findClosestByRange(FIND_HOSTILE_CREEPS);
|
||||||
// if one is found...
|
|
||||||
if (target != undefined) {
|
if (target != undefined) {
|
||||||
// ...FIRE!
|
|
||||||
this.attack(target);
|
this.attack(target);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Repair structure & only first 100k of wall
|
||||||
var closestDamagedStructure = this.pos.findClosestByRange(FIND_STRUCTURES, {
|
var closestDamagedStructure = this.pos.findClosestByRange(FIND_STRUCTURES, {
|
||||||
filter: (structure) => structure.hits < structure.hitsMax
|
filter: (structure) => structure.hits < structure.hitsMax && (structure.structureType != STRUCTURE_WALL || structure.hits < 100000)
|
||||||
});
|
});
|
||||||
|
|
||||||
if(closestDamagedStructure) {
|
if(closestDamagedStructure) {
|
||||||
|
|||||||
@@ -1,63 +0,0 @@
|
|||||||
module.exports = {
|
|
||||||
// a function to run the logic for this role
|
|
||||||
/** @param {Creep} creep */
|
|
||||||
run: function(creep) {
|
|
||||||
// if creep is bringing energy to a structure but has no energy left
|
|
||||||
if (creep.memory.working == true && creep.carry.energy == 0) {
|
|
||||||
// switch state
|
|
||||||
creep.memory.working = false;
|
|
||||||
}
|
|
||||||
// if creep is harvesting energy but is full
|
|
||||||
else if (creep.memory.working == false && creep.carry.energy == creep.carryCapacity) {
|
|
||||||
// switch state
|
|
||||||
creep.memory.working = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// if creep is supposed to transfer energy to a structure
|
|
||||||
if (creep.memory.working == true) {
|
|
||||||
// find closest spawn, extension or tower which is not full
|
|
||||||
var structure = creep.pos.findClosestByPath(FIND_MY_STRUCTURES, {
|
|
||||||
// the second argument for findClosestByPath is an object which takes
|
|
||||||
// a property called filter which can be a function
|
|
||||||
// we use the arrow operator to define it
|
|
||||||
filter: (s) => (s.structureType == STRUCTURE_SPAWN
|
|
||||||
|| s.structureType == STRUCTURE_EXTENSION
|
|
||||||
|| s.structureType == STRUCTURE_TOWER)
|
|
||||||
&& s.energy < s.energyCapacity
|
|
||||||
});
|
|
||||||
|
|
||||||
if (structure == undefined) {
|
|
||||||
structure = creep.room.storage;
|
|
||||||
}
|
|
||||||
|
|
||||||
// if we found one
|
|
||||||
if (structure != undefined) {
|
|
||||||
// try to transfer energy, if it is not in range
|
|
||||||
if (creep.transfer(structure, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
|
|
||||||
// move towards it
|
|
||||||
creep.moveTo(structure);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// if creep is supposed to get energy
|
|
||||||
else {
|
|
||||||
// find closest container
|
|
||||||
let container = creep.pos.findClosestByPath(FIND_STRUCTURES, {
|
|
||||||
filter: s => s.structureType == STRUCTURE_CONTAINER && s.store[RESOURCE_ENERGY] > 0
|
|
||||||
});
|
|
||||||
|
|
||||||
if (container == undefined) {
|
|
||||||
container = creep.room.storage;
|
|
||||||
}
|
|
||||||
|
|
||||||
// if one was found
|
|
||||||
if (container != undefined) {
|
|
||||||
// try to withdraw energy, if the container is not in range
|
|
||||||
if (creep.withdraw(container, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
|
|
||||||
// move towards it
|
|
||||||
creep.moveTo(container);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@@ -6,7 +6,7 @@ module.exports = {
|
|||||||
// find container next to source
|
// find container next to source
|
||||||
let container = source.pos.findInRange(FIND_STRUCTURES, 1, {
|
let container = source.pos.findInRange(FIND_STRUCTURES, 1, {
|
||||||
filter: s => s.structureType == STRUCTURE_CONTAINER
|
filter: s => s.structureType == STRUCTURE_CONTAINER
|
||||||
})[0];
|
});
|
||||||
|
|
||||||
// if creep is on top of the container
|
// if creep is on top of the container
|
||||||
if (creep.pos.isEqualTo(container.pos)) {
|
if (creep.pos.isEqualTo(container.pos)) {
|
||||||
|
|||||||
@@ -1,72 +0,0 @@
|
|||||||
var roleBuilder = require('role.builder');
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
// a function to run the logic for this role
|
|
||||||
/** @param {Creep} creep */
|
|
||||||
run: function(creep) {
|
|
||||||
// if creep is trying to repair something but has no energy left
|
|
||||||
if (creep.memory.working == true && creep.carry.energy == 0) {
|
|
||||||
var time = creep.memory.ttl - creep.ticksToLive;
|
|
||||||
console.log(creep.name + " : " + creep.memory.role + " > " + time);
|
|
||||||
|
|
||||||
if(creep.ticksToLive < 100) {
|
|
||||||
console.log(creep.name + " suicide for ttl : " + creep.ticksToLive);
|
|
||||||
creep.suicide();
|
|
||||||
}
|
|
||||||
// switch state
|
|
||||||
creep.memory.ttl = creep.ticksToLive;
|
|
||||||
// switch state
|
|
||||||
creep.memory.working = false;
|
|
||||||
}
|
|
||||||
// if creep is harvesting energy but is full
|
|
||||||
else if (creep.memory.working == false && creep.carry.energy == creep.carryCapacity) {
|
|
||||||
// switch state
|
|
||||||
creep.memory.working = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// if creep is supposed to repair something
|
|
||||||
if (creep.memory.working == true) {
|
|
||||||
// find all walls in the room
|
|
||||||
var walls = creep.room.find(FIND_STRUCTURES, {
|
|
||||||
filter: (s) => s.structureType == STRUCTURE_WALL
|
|
||||||
});
|
|
||||||
|
|
||||||
var target = undefined;
|
|
||||||
|
|
||||||
// loop with increasing percentages
|
|
||||||
for (let percentage = 0.0001; percentage <= 1; percentage = percentage + 0.0001){
|
|
||||||
// find a wall with less than percentage hits
|
|
||||||
for (let wall of walls) {
|
|
||||||
if (wall.hits / wall.hitsMax < percentage) {
|
|
||||||
target = wall;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// if there is one
|
|
||||||
if (target != undefined) {
|
|
||||||
// break the loop
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// if we find a wall that has to be repaired
|
|
||||||
if (target != undefined) {
|
|
||||||
// try to repair it, if not in range
|
|
||||||
if (creep.repair(target) == ERR_NOT_IN_RANGE) {
|
|
||||||
// move towards it
|
|
||||||
creep.moveTo(target);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// if we can't fine one
|
|
||||||
else {
|
|
||||||
// look for construction sites
|
|
||||||
roleBuilder.run(creep);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// if creep is supposed to get energy
|
|
||||||
else {
|
|
||||||
creep.getEnergy(true, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Reference in New Issue
Block a user