Skip to content

Commit

Permalink
Merge pull request #10 from suiramdev/features/multiRoles
Browse files Browse the repository at this point in the history
Features/multi roles
  • Loading branch information
suiramdev authored Nov 28, 2021
2 parents 64f685c + f7f9547 commit a72d83d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 30 deletions.
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
14 changes: 4 additions & 10 deletions src/managers/roomManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,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 @@ -69,25 +65,23 @@ export function run(): void {
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

0 comments on commit a72d83d

Please sign in to comment.