Skip to content

Commit

Permalink
patches
Browse files Browse the repository at this point in the history
  • Loading branch information
PHCCorso committed Dec 10, 2019
1 parent 023d592 commit b17f495
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 125 deletions.
11 changes: 8 additions & 3 deletions src/creep/role/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
createCreep,
changeCreepActivity,
upgradeCommonCreep,
upgradeCollectorCreep,
} from '../../utils/creep';
import { Role, COMMON_CREEP, Activity } from '../../constants/creep';
import roleRepairer from './repairer';
Expand Down Expand Up @@ -31,8 +32,8 @@ const roleBuilder = {
if (creep.build(target) == ERR_NOT_IN_RANGE) {
creep.moveTo(target, {
visualizePathStyle: { stroke: '#ffffff' },
maxOps: 5000, reusePath: 15

maxOps: 5000,
reusePath: 15,
});
}
return Activity.BUILD;
Expand All @@ -42,6 +43,10 @@ const roleBuilder = {
} else if (creep.room.memory['wallsAndRamparts'].length > 0) {
roleWallRepairer.run(creep);
} else {
if (creep.room.memory['storages'].length) {
const storage = creep.room.memory['storages'][0];
creep.memory['collectTarget'] = storage;
}
return collectOrHarvest(creep);
}
},
Expand All @@ -64,7 +69,7 @@ const roleBuilder = {
) {
createCreep(spawn, Role.BUILDER, [
...COMMON_CREEP,
...upgradeCommonCreep(spawn),
...upgradeCollectorCreep(spawn),
]);
}
},
Expand Down
18 changes: 10 additions & 8 deletions src/creep/role/collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,15 @@ const roleCollector = {

if (
target &&
creep.withdraw(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE
creep.withdraw(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE &&
creep.memory['storeTarget'] != target.id
) {
creep.memory['storeTarget'] = undefined;
creep.memory['collectTarget'] = target.id;
creep.moveTo(target, {
visualizePathStyle: { stroke: '#ffaa00' },
maxOps: 5000, reusePath: 15

maxOps: 5000,
reusePath: 15,
});
return Activity.COLLECT;
}
Expand Down Expand Up @@ -124,7 +125,8 @@ const roleCollector = {
) as any;
if (
tower.store.getUsedCapacity(RESOURCE_ENERGY) <=
tower.store.getCapacity(RESOURCE_ENERGY) / 2
tower.store.getCapacity(RESOURCE_ENERGY) / 2 ||
hostiles.length
) {
target = tower;
targetHasFreeCapacity =
Expand All @@ -143,7 +145,7 @@ const roleCollector = {
}

if (
target.id != creep.memory['collectTarget'] &&
creep.memory['storeTarget'] != creep.memory['collectTarget'] &&
targetHasFreeCapacity
) {
creep.memory['storeTarget'] = target.id;
Expand All @@ -152,14 +154,14 @@ const roleCollector = {
) {
creep.moveTo(target, {
visualizePathStyle: { stroke: '#ffffff' },
maxOps: 5000, reusePath: 15

maxOps: 5000,
reusePath: 15,
});
}
creep.memory['collectTarget'] = undefined;
return Activity.STORE;
}
return Activity.NONE;
return Activity.COLLECT;
}

return Activity.NONE;
Expand Down
13 changes: 9 additions & 4 deletions src/creep/role/repairer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import roleHarvester from './harvester';
import roleBuilder from './builder';
import roleWallRepairer from './wallRepairer';
import roleCollector from './collector';

const roleRepairer = {
run: function(creep: Creep) {
Expand Down Expand Up @@ -39,8 +40,8 @@ const roleRepairer = {
if (creep.repair(closestDamagedStructure) == ERR_NOT_IN_RANGE) {
creep.moveTo(closestDamagedStructure, {
visualizePathStyle: { stroke: '#ffffff' },
maxOps: 5000, reusePath: 15

maxOps: 5000,
reusePath: 15,
});
}
return Activity.REPAIR;
Expand All @@ -49,17 +50,21 @@ const roleRepairer = {
} else if (creep.room.memory['wallsAndRamparts'].length > 0) {
roleWallRepairer.run(creep);
} else {
return roleHarvester.run(creep);
return roleCollector.run(creep);
}
} else if (
creep.room.memory['damagedStructures'].length == 0 &&
creep.room.memory['constructionSites'].length > 0
) {
return roleBuilder.run(creep);
} else if (creep.room.memory['damagedStructures'].length > 0) {
if (creep.room.memory['storages'].length) {
const storage = creep.room.memory['storages'][0];
creep.memory['collectTarget'] = storage;
}
return collectOrHarvest(creep);
} else {
return roleHarvester.run(creep);
return roleCollector.run(creep);
}
},
generate: function(spawn: StructureSpawn) {
Expand Down
6 changes: 5 additions & 1 deletion src/creep/role/wallRepairer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,13 @@ const roleWallRepairer = {
}
return Activity.REPAIR;
} else {
return roleHarvester.run(creep);
return collectOrHarvest(creep);
}
} else if (creep.room.memory['wallsAndRamparts'].length > 0) {
if (creep.room.memory['storages'].length) {
const storage = creep.room.memory['storages'][0];
creep.memory['collectTarget'] = storage;
}
return collectOrHarvest(creep);
} else {
return roleCollector.run(creep);
Expand Down
228 changes: 119 additions & 109 deletions src/room/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,120 +5,130 @@ function handleRooms() {
for (const roomName in Game.rooms) {
const room = Game.rooms[roomName];

// # MEMORY

// Resources
// ---------------------------------------------
const containers = room.find(FIND_STRUCTURES, {
filter: structure => structure.structureType == STRUCTURE_CONTAINER,
});

room.memory['containers'] = containers.map(c => c.id);

room.memory['filledContainers'] = containers
// @ts-ignore
.filter(c => c.store.getUsedCapacity(RESOURCE_ENERGY) > 0)
.sort(
(a: any, b: any) =>
b.store.getUsedCapacity(RESOURCE_ENERGY) -
a.store.getUsedCapacity(RESOURCE_ENERGY)
)
.map(c => c.id);

const storages = room.find(FIND_STRUCTURES, {
filter: structure => structure.structureType == STRUCTURE_STORAGE,
});

room.memory['storages'] = storages.map(s => s.id);

const tombstones = room
.find(FIND_TOMBSTONES)
.filter(t => t.store[RESOURCE_ENERGY] > 0);
room.memory['tombstones'] = tombstones.map(t => t.id);

const ruinsWithEnergy = room
// @ts-ignore
.find(FIND_RUINS)
.filter((r: any) => r.store && r.store[RESOURCE_ENERGY] > 0);
room.memory['ruinsWithEnergy'] = ruinsWithEnergy.map((r: any) => r.id);

// STRUCTURES
// --------------------------------------------

room.memory['towers'] = room
.find(FIND_STRUCTURES)
.filter(s => s.structureType == STRUCTURE_TOWER)
.sort(
(a: any, b: any) =>
a.store.getUsedCapacity(RESOURCE_ENERGY) -
b.store.getUsedCapacity(RESOURCE_ENERGY)
)
.map(s => s.id);

room.memory['constructionSites'] = room
.find(FIND_CONSTRUCTION_SITES)
.map(c => c.id);

room.memory['damagedStructures'] = room
.find(FIND_STRUCTURES)
.filter(
s =>
s.hits < s.hitsMax &&
s.structureType != STRUCTURE_RAMPART &&
s.structureType != STRUCTURE_WALL
)
.sort((a, b) => a.hits - b.hits)
.map(s => s.id);

room.memory['wallsAndRamparts'] = room
.find(FIND_STRUCTURES)
.filter(
s =>
s.hits < MAX_WALL_HITS &&
(s.structureType == STRUCTURE_RAMPART ||
s.structureType == STRUCTURE_WALL)
)
.sort((a, b) => {
if (a.structureType == STRUCTURE_RAMPART) {
return a.hits - 1000 - b.hits;
}
return a.hits - b.hits;
})
.map(s => s.id);

room.memory['extensions'] = room
const isOwnedRoom = room
.find(FIND_MY_STRUCTURES)
.filter(s => s.structureType === STRUCTURE_EXTENSION)
.sort(
(a: any, b: any) =>
a.store.getUsedCapacity(RESOURCE_ENERGY) -
b.store.getUsedCapacity(RESOURCE_ENERGY)
)
.map(e => e.id);
.some(s => s.structureType == STRUCTURE_CONTROLLER);

if (isOwnedRoom) {
// # MEMORY

// Resources
// ---------------------------------------------
const containers = room.find(FIND_STRUCTURES, {
filter: structure =>
structure.structureType == STRUCTURE_CONTAINER,
});

room.memory['containers'] = containers.map(c => c.id);

room.memory['filledContainers'] = containers
// @ts-ignore
.filter(c => c.store.getUsedCapacity(RESOURCE_ENERGY) > 0)
.sort(
(a: any, b: any) =>
b.store.getUsedCapacity(RESOURCE_ENERGY) -
a.store.getUsedCapacity(RESOURCE_ENERGY)
)
.map(c => c.id);

const storages = room.find(FIND_STRUCTURES, {
filter: structure =>
structure.structureType == STRUCTURE_STORAGE,
});

room.memory['storages'] = storages.map(s => s.id);

const tombstones = room
.find(FIND_TOMBSTONES)
.filter(t => t.store[RESOURCE_ENERGY] > 0);
room.memory['tombstones'] = tombstones.map(t => t.id);

const ruinsWithEnergy = room
// @ts-ignore
.find(FIND_RUINS)
.filter((r: any) => r.store && r.store[RESOURCE_ENERGY] > 0);
room.memory['ruinsWithEnergy'] = ruinsWithEnergy.map(
(r: any) => r.id
);

// STRUCTURES
// --------------------------------------------

room.memory['towers'] = room
.find(FIND_STRUCTURES)
.filter(s => s.structureType == STRUCTURE_TOWER)
.sort(
(a: any, b: any) =>
a.store.getUsedCapacity(RESOURCE_ENERGY) -
b.store.getUsedCapacity(RESOURCE_ENERGY)
)
.map(s => s.id);

room.memory['constructionSites'] = room
.find(FIND_CONSTRUCTION_SITES)
.map(c => c.id);

room.memory['damagedStructures'] = room
.find(FIND_STRUCTURES)
.filter(
s =>
s.hits < s.hitsMax &&
s.structureType != STRUCTURE_RAMPART &&
s.structureType != STRUCTURE_WALL
)
.sort((a, b) => a.hits - b.hits)
.map(s => s.id);

room.memory['wallsAndRamparts'] = room
.find(FIND_STRUCTURES)
.filter(
s =>
s.hits < MAX_WALL_HITS &&
(s.structureType == STRUCTURE_RAMPART ||
s.structureType == STRUCTURE_WALL)
)
.sort((a, b) => {
if (a.structureType == STRUCTURE_RAMPART) {
return a.hits - 1000 - b.hits;
}
return a.hits - b.hits;
})
.map(s => s.id);

room.memory['extensions'] = room
.find(FIND_MY_STRUCTURES)
.filter(s => s.structureType === STRUCTURE_EXTENSION)
.sort(
(a: any, b: any) =>
a.store.getUsedCapacity(RESOURCE_ENERGY) -
b.store.getUsedCapacity(RESOURCE_ENERGY)
)
.map(e => e.id);

// CREEPS
// ---------------------------------------------

room.memory['creeps'] = {};

for (const role in Role) {
room.memory['creeps'][Role[role]] = [];
}

// CREEPS
// ---------------------------------------------
room.memory['woundedCreeps'] = [];

room.memory['creeps'] = {};
room.find(FIND_MY_CREEPS).forEach(creep => {
const role = creep.memory['role'];
if (role) {
room.memory['creeps'][role].push(creep.id);
}
if (creep.hits < creep.hitsMax) {
room.memory['woundedCreeps'].push(creep.id);
}
});

for (const role in Role) {
room.memory['creeps'][Role[role]] = [];
const hostiles = room.find(FIND_HOSTILE_CREEPS);
room.memory['hostiles'] = hostiles.map(h => h.id);
}

room.memory['woundedCreeps'] = [];

room.find(FIND_MY_CREEPS).forEach(creep => {
const role = creep.memory['role'];
if (role) {
room.memory['creeps'][role].push(creep.id);
}
if (creep.hits < creep.hitsMax) {
room.memory['woundedCreeps'].push(creep.id);
}
});

const hostiles = room.find(FIND_HOSTILE_CREEPS);
room.memory['hostiles'] = hostiles.map(h => h.id);
}
}

Expand Down

0 comments on commit b17f495

Please sign in to comment.