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

Features/multi roles #10

Merged
merged 3 commits into from
Nov 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 26 additions & 19 deletions src/managers/rolesManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,31 @@ export enum Role {
CARRIER = "CARRIER",
UPGRADER = "UPGRADER",
REPAIRER = "REPAIRER",
BUILDER = "BUILDER",
SAFER = "SAFER"
BUILDER = "BUILDER"
}

export const RoleTasks: Record<string, string[]> = {
[Role.HARVESTER]: [Task.HARVEST],
[Role.CARRIER]: [Task.CARRY, Task.SPREAD_ENERGY],
[Role.UPGRADER]: [Task.UPGRADE],
[Role.REPAIRER]: [Task.REPAIR],
[Role.BUILDER]: [Task.BUILD],
[Role.SAFER]: [Task.STORE]
export const RoleTasks: Record<string, string[][]> = {
[Role.HARVESTER]: [[Task.HARVEST]],
[Role.CARRIER]: [[Task.CARRY], [Task.STORE, Task.SPREAD_ENERGY], [Task.SPREAD_ENERGY]],
[Role.UPGRADER]: [[Task.UPGRADE]],
[Role.REPAIRER]: [[Task.REPAIR, Task.BUILD]],
[Role.BUILDER]: [[Task.BUILD, Task.REPAIR]]
}

export const RoleBodyParts: Record<Role, Record<string, number>> = {
[Role.HARVESTER]: {[MOVE]: 0.1, [CARRY]: 0.1, [WORK]: 0.8},
[Role.CARRIER]: {[MOVE]: 0.4, [CARRY]: 0.5, [WORK]: 0.1},
[Role.UPGRADER]: {[MOVE]: 0.1, [CARRY]: 0.4, [WORK]: 0.5},
[Role.REPAIRER]: {[MOVE]: 0.3, [CARRY]: 0.3, [WORK]: 0.4},
[Role.BUILDER]: {[MOVE]: 0.3, [CARRY]: 0.3, [WORK]: 0.4},
[Role.SAFER]: {[MOVE]: 0.4, [CARRY]: 0.5, [WORK]: 0.1}
[Role.BUILDER]: {[MOVE]: 0.3, [CARRY]: 0.3, [WORK]: 0.4}
}

export function run(creep: Creep): void {
if (creep.memory.pauseRole) {
return;
}

// Self-harvest
// Self-harvest energy
if (creep.memory.needEnergy && creep.store.getFreeCapacity() > 0 &&
_.filter(Game.creeps, c => c.memory.role === Role.CARRIER).length <= 0) {
const target: Source | null = creep.pos.findClosestByPath(FIND_SOURCES_ACTIVE);
Expand All @@ -51,13 +48,23 @@ export function run(creep: Creep): void {
}
}

if (!creep.memory.task) creep.memory.task = RoleTasks[creep.memory.role][0];

const taskStatus = creep.runTask(creep.memory.task);
if (taskStatus === TaskStatus.COMPLETED) {
const currentTaskIndex = RoleTasks[creep.memory.role].indexOf(creep.memory.task);
const nextTaskIndex = currentTaskIndex + 1 > RoleTasks[creep.memory.role].length ? 0 : currentTaskIndex + 1
creep.memory.task = RoleTasks[creep.memory.role][nextTaskIndex];
// Assign creep task and run it
const creepTasks = RoleTasks[creep.memory.role];
if (!creep.memory.task) {
creep.memory.taskIndex = 0;
creep.memory.task = creepTasks[0][0];
}
switch (creep.runTask(creep.memory.task)) {
case TaskStatus.COMPLETED:
creep.memory.taskIndex += 1;
if (!creepTasks[creep.memory.taskIndex]) creep.memory.taskIndex = 0;
creep.memory.task = creepTasks[creep.memory.taskIndex][0];
break;
case TaskStatus.FAILED:
let nextSubTaskIndex = Object.values(creepTasks[creep.memory.taskIndex]).indexOf(creep.memory.task)+1;
if (!creepTasks[creep.memory.taskIndex][nextSubTaskIndex]) nextSubTaskIndex = 0;
creep.memory.task = creepTasks[creep.memory.taskIndex][nextSubTaskIndex]
break;
}

if (Memory.debug.drawRoles) {
Expand Down
16 changes: 4 additions & 12 deletions src/managers/roomManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ Room.prototype.configuration = function () {
{
role: Role.BUILDER,
needed: this.find(FIND_MY_CONSTRUCTION_SITES).length > 0 ? 1 : 0
},
{
role: Role.SAFER,
needed: this.sourceStorages().length
}
];
}
Expand Down Expand Up @@ -54,7 +50,6 @@ export function run(): void {
if (Game.time % 100 === 0 || !configurations[roomHash]) {
configurations[roomHash] = room.configuration();
configurations[roomHash].forEach(conf => subpopulations[roomHash] += conf.needed);
console.log(subpopulations[roomHash]);
}
}

Expand All @@ -64,31 +59,28 @@ export function run(): void {

const neededRole = room.neededRoles()[0];
if (neededRole) {
console.log(neededRole);
// TODO: Look to unused creeps
const spawn: StructureSpawn = _.filter(room.closestSpawns(), s => !s.spawning)[0];
if (spawn) {
const creepName: string = neededRole + "_" + Math.random().toString(36).substr(2, 5);

const saferCount = _.filter(Game.creeps, c => c.memory.role === Role.SAFER && c.memory.room === spawn.room.name).length;
let energyRemaining: number = saferCount > 0 ? spawn.room.energyCapacityAvailable : spawn.store.getCapacity(RESOURCE_ENERGY);
const creepParts: BodyPartConstant[] = [];

const keys = Object.keys(RoleBodyParts[neededRole as Role]);
for (const bodyPart in RoleBodyParts[neededRole as Role]) {
const percentage = RoleBodyParts[neededRole as Role][bodyPart];
const isLast = keys[keys.length] === bodyPart;

for (let i = 0; i < (isLast ? energyRemaining : energyRemaining * percentage) / BODYPART_COST[bodyPart as BodyPartConstant]; i++) {
for (let i = 0; i < (isLast ? spawn.room.energyCapacityAvailable : spawn.room.energyCapacityAvailable * percentage) / BODYPART_COST[bodyPart as BodyPartConstant]; i++) {
creepParts.push(bodyPart as BodyPartConstant);
energyRemaining -= BODYPART_COST[bodyPart as BodyPartConstant];
spawn.room.energyCapacityAvailable -= BODYPART_COST[bodyPart as BodyPartConstant];
}
}

const spawnStatus = spawn.spawnCreep(creepParts, creepName, {
memory: {
role: neededRole,
task: RoleTasks[neededRole][0],
taskIndex: 0,
task: RoleTasks[neededRole][0][0],
room: room.name
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function run(creep: Creep): TaskStatus {
return TaskStatus.COMPLETED;
} else if (creep.store.getFreeCapacity() <= 0) creep.memory.needEnergy = false;

const storages = creep.room.find(FIND_STRUCTURES, {
const storages = creep.room.find(FIND_MY_STRUCTURES, {
filter: (structure) => (structure.structureType === STRUCTURE_SPAWN ||
structure.structureType === STRUCTURE_EXTENSION ||
structure.structureType === STRUCTURE_STORAGE ||
Expand Down
1 change: 1 addition & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ interface Memory {

interface CreepMemory {
role: string;
taskIndex: number;
task: string;
room: string;
needEnergy?: boolean;
Expand Down