Skip to content

Commit

Permalink
Merge pull request #402 from TooAngel/attack
Browse files Browse the repository at this point in the history
Improve fighting
  • Loading branch information
democratic-collaboration authored Sep 15, 2017
2 parents ba9a494 + 49d243d commit 2dddbb4
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 144 deletions.
5 changes: 4 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ global.config = {
healMyCreeps: false,
repairStructures: false,
},

autoattack: {
disabled: false,
notify: false,
timeBetweenAttacks: 2000,
},

revive: {
Expand All @@ -79,7 +81,7 @@ global.config = {
nextRoom: {
boostToControllerLevel: 4,
scoutMinControllerLevel: 4,
ttlPerRoomForScout: 500,
ttlPerRoomForScout: 1500,
numberOfNextroomers: 10,
nextroomerInterval: 500,
maxRooms: 20,
Expand Down Expand Up @@ -128,6 +130,7 @@ global.config = {

external: {
distance: 3,
defendDistance: 1,
},

carry: {
Expand Down
25 changes: 14 additions & 11 deletions src/prototype_creep_fight.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Creep.prototype.fleeFromHostile = function(hostile) {
for (let offset = 0; offset < 8; offset++) {
const dir = (direction + offset) % 8 + 1;
const pos = this.pos.getAdjacentPosition(dir);
if (pos.lookFor(LOOK_TERRAIN)[0] !== STRUCTURE_WALL && pos.lookFor(LOOK_CREEPS).length === 0) {
direction = direction + offset;
if (!pos.checkForWall() && pos.lookFor(LOOK_CREEPS).length === 0) {
direction = dir;
break;
}
}
Expand Down Expand Up @@ -169,6 +169,16 @@ Creep.prototype.fightRampart = function(target) {
if (range > 3) {
return false;
}

const targets = this.pos.findInRange(FIND_HOSTILE_CREEPS, 3, {
filter: this.room.findAttackCreeps,
});
if (targets.length > 1) {
this.rangedMassAttack();
} else {
this.rangedAttack(target);
}

const returnCode = this.moveToMy(rampart.pos, 0);
if (returnCode === OK) {
return true;
Expand All @@ -179,14 +189,6 @@ Creep.prototype.fightRampart = function(target) {

this.log('creep_fight.fightRampart returnCode: ' + returnCode);

const targets = this.pos.findInRange(FIND_HOSTILE_CREEPS, 3, {
filter: this.room.findAttackCreeps,
});
if (targets.length > 1) {
this.rangedMassAttack();
} else {
this.rangedAttack(target);
}
return true;
};

Expand Down Expand Up @@ -233,7 +235,8 @@ Creep.prototype.siege = function() {
this.memory.hitsLost = this.memory.hitsLast - this.hits;
this.memory.hitsLast = this.hits;

if (this.hits - this.memory.hitsLost < this.hits / 2) {
// if (this.hits - this.memory.hitsLost < this.hits / 2) {
if (this.hits < 0.7 * this.hitsMax) {
const exitNext = this.pos.findClosestByRange(FIND_EXIT);
this.moveTo(exitNext);
return true;
Expand Down
17 changes: 17 additions & 0 deletions src/prototype_creep_squad.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

Creep.prototype.squadMove = function(squad, maxRange, moveRandom, role) {
if (this.room.name === squad.moveTarget) {
const nextExits = this.room.find(this.memory.routing.route[this.memory.routing.routePos].exit);
const nextExit = nextExits[Math.floor(nextExits.length / 2)];
const range = this.pos.getRangeTo(nextExit.x, nextExit.y);
if (range < maxRange) {
Memory.squads[this.memory.squad][role][this.id].waiting = true;
if (moveRandom) {
this.moveRandom();
}
return true;
}
}
return false;
};
142 changes: 88 additions & 54 deletions src/prototype_room_attack.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
'use strict';

/**
* attack42 is called so because 42 is the only true answer :-)
* Attack42 is called so because 42 is the only true answer :-).
*
* USAGE: get a Room Object then .attack42('targetRoomName')
* e.g. Game.rooms[Memory.myRooms[0]].attack42('targetRoomName');
* TODO add an attack42 event if invader is seen by reserver, sourcer or carry
* USAGE: get a Room Object then `.attack42('targetRoomName')`
* e.g. `Game.rooms[Memory.myRooms[0]].attack42('targetRoomName')`;.
* TODO add an attack42 event if invader is seen by reserver, sourcer or carry.
*
* @example room.attack42('E5S3')
*
* @param {string} roomName - Should be your targetRoomName.
* @param {Array} [spawn] - YourCustomSpawn Array of {creeps: creepsToAdd, role: 'rolesToAdd'}.
*
* @param {String} roomName should be your targetRoomName
* @param {Array} [spawn] yourCustomSpawn Array of {creeps: creepsToAdd, role: 'rolesToAdd'}
*/
Room.prototype.attack42 = function(roomName, spawn) {
spawn = spawn || [{
Expand All @@ -21,7 +24,6 @@ Room.prototype.attack42 = function(roomName, spawn) {
creep: 1,
role: 'squadheal',
},

{
creep: 2,
role: 'autoattackmelee',
Expand All @@ -35,35 +37,60 @@ Room.prototype.attack42 = function(roomName, spawn) {
];

const closestSpawn = this.closestSpawn(roomName);
// this.log('closestSpawn ' + JSON.stringify(closestSpawn, null, 2));
if (closestSpawn && closestSpawn.id) {
brain.startMeleeSquad(closestSpawn.room, roomName
// , spawn
);
brain.startMeleeSquad(closestSpawn.room, roomName);
}
};

Room.prototype.attackRoom = function() {
function attack0(room) {
room.log('Queuing level 0 attack');
if (config.autoattack.notify) {
Game.notify(Game.time + ' ' + room.name + ' Queuing autoattacker');
}
const getClosestRoom = function(roomName) {
const sortByDistance = function(object) {
return Game.map.getRoomLinearDistance(roomName, object);
};

const sortByDistance = function(object) {
return Game.map.getRoomLinearDistance(room.name, object);
};
const roomsMy = _.sortBy(Memory.myRooms, sortByDistance);
return Game.rooms[roomsMy[0]];
};

const roomsMy = _.sortBy(Memory.myRooms, sortByDistance);
const attacks = {
attack0: function(room) {
const origin = getClosestRoom(room.name);
origin.checkRoleToSpawn('autoattackmelee', 1, undefined, room.name);
return true;
},

Game.rooms[roomsMy[0]].memory.queue.push({
role: 'autoattackmelee',
routing: {
targetRoom: room.name,
},
});
attack1: function(room) {
const origin = getClosestRoom(room.name);
brain.startSquad(origin.name, room.name);
return true;
},

attack2: function(room) {
const origin = getClosestRoom(room.name);
origin.attack42(room.name);
return true;
},
};

const addRoom = function(player, room) {
if (!player.rooms) {
player.rooms = {};
}
if (!player.rooms[room.name]) {
player.rooms[room.name] = {
visited: Game.time,
};
Memory.players[player.name] = player;
}
};

const getPlayer = function(name) {
brain.increaseIdiot(name, 0);
return Memory.players[name];
};

Room.prototype.getOwnerName = function() {
if (this.controller.owner) {
return this.controller.owner.username;
}

if (config.autoattack.disabled) {
Expand All @@ -75,45 +102,52 @@ Room.prototype.attackRoom = function() {
} else {
if (this.controller.reservation) {
name = this.controller.reservation.username;
} else {
return;
}
}
return name;
};

Room.prototype.launchAutoAttack = function(player) {
if (!player.lastAttacked) {
player.lastAttacked = Game.time;
Memory.players[player.name] = player;
}
if (Game.time < player.lastAttacked + config.autoattack.timeBetweenAttacks) {
return false;
}
this.log(`Queuing level ${player.level} attack`);
if (config.autoattack.notify) {
Game.notify(Game.time + ' ' + this.name + ' Queuing autoattacker');
}
attacks[`attack${player.level}`](this);
player.counter++;
if (player.counter > 10) {
player.level += 1;
player.counter = 0;
}
Memory.players[player.name] = player;
};

Room.prototype.attackRoom = function() {
if (config.autoattack.disabled) {
return true;
}
const name = this.getOwnerName();
if (!name) {
return;
}

// We only exclude players in the friends.js
if (friends.indexOf(name) > -1) {
return true;
}

const getPlayer = function(name) {
brain.increaseIdiot(name, 0);
return Memory.players[name];
};

const player = getPlayer(name);

const addRoom = function(player, room) {
if (!player.rooms) {
player.rooms = {};
}
if (!player.rooms[room.name]) {
player.rooms[room.name] = {
visited: Game.time,
};
Memory.players[player.name] = player;
}
};

addRoom(player, this);

if (player.level === 0) {
attack0(this);
player.counter++;
if (player.counter > 5) {
player.level = 1;
player.counter = 0;
}
Memory.players[name] = player;
if (player.level < attacks.length) {
this.launchAutoAttack(player);
}
return true;
};
1 change: 1 addition & 0 deletions src/require.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,4 @@ require('role_structurer');
require('role_towerdrainer');
require('role_towerfiller');
require('role_upgrader');
require('prototype_creep_squad');
2 changes: 1 addition & 1 deletion src/role_autoattackmelee.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ roles.autoattackmelee = {};

roles.autoattackmelee.settings = {
layoutString: 'MA',
amount: [5, 5],
amount: [1, 1],
fillTough: true,
};

Expand Down
24 changes: 24 additions & 0 deletions src/role_scout.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,29 @@ function haveNotSeen(creep, room) {
creep.memory.skip.indexOf(room) === -1;
}

function checkForDefender(creep) {
if (!creep.room.controller) {
return false;
}
if (!creep.room.controller.reservation) {
return false;
}
if (creep.room.controller.reservation.username === Memory.username) {
return false;
}
if (!config.external.defendDistance) {
return false;
}

const distance = Game.map.getRoomLinearDistance(creep.room.name, creep.memory.base);
if (distance > config.external.defendDistance) {
return false;
}

creep.log('Spawning defender for external room');
Game.rooms[creep.memory.base].checkRoleToSpawn('defender', 1, undefined, creep.room.name);
}

roles.scout.execute = function(creep) {
if (creep.memory.skip === undefined) {
creep.memory.skip = [];
Expand Down Expand Up @@ -57,6 +80,7 @@ roles.scout.execute = function(creep) {
creep.memory.skip.push(creep.memory.search.target);
delete creep.memory.scoutSkip;
} else {
checkForDefender(creep);
creep.memory.search.seen.push(creep.room.name);
}
if (!setNewTarget(creep)) {
Expand Down
Loading

0 comments on commit 2dddbb4

Please sign in to comment.