More robust harvester handling
This commit is contained in:
parent
c8fba64a2f
commit
c03b0ccea6
@ -6,26 +6,51 @@ interface HarvesterMemory extends CreepMemory {
|
||||
sourceId: Id<Source> | undefined;
|
||||
}
|
||||
|
||||
enum Action {
|
||||
Harvest = 'harvest',
|
||||
Charge = 'charge'
|
||||
}
|
||||
|
||||
export function runAsHarvester(creep: Creep) {
|
||||
const memory = creep.memory as HarvesterMemory;
|
||||
|
||||
if (memory.action === undefined) {
|
||||
memory.action = Action.Harvest;
|
||||
}
|
||||
|
||||
if (memory.sourceId === undefined) {
|
||||
memory.sourceId = selectSource(creep);
|
||||
}
|
||||
|
||||
if (creep.store.getFreeCapacity() > 0) {
|
||||
switch (memory.action) {
|
||||
case Action.Charge:
|
||||
if (creep.store[RESOURCE_ENERGY] === 0) {
|
||||
memory.action = Action.Harvest;
|
||||
}
|
||||
break;
|
||||
case Action.Harvest:
|
||||
if (creep.store.getFreeCapacity() === 0) {
|
||||
memory.action = Action.Charge;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
switch (memory.action) {
|
||||
case Action.Harvest:
|
||||
const source = Game.getObjectById(memory.sourceId);
|
||||
if (source && creep.harvest(source) === ERR_NOT_IN_RANGE) {
|
||||
creep.say('🔄 harvest');
|
||||
creep.moveTo(source, { visualizePathStyle: { stroke: '#ffaa00' } });
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
case Action.Charge:
|
||||
const target = findTarget(creep);
|
||||
if (target) {
|
||||
moveEnergyToTarget(creep, target);
|
||||
} else {
|
||||
moveToSpawn(creep);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,7 @@ function makeCreep(role: CreepRole, count: number) {
|
||||
const firstSpawn = _.first(Object.values(Game.spawns));
|
||||
const name = makeName();
|
||||
const memory = { role: role } as CreepMemory;
|
||||
const err = firstSpawn.spawnCreep([WORK, WORK, CARRY, CARRY, MOVE], name, { memory });
|
||||
const err = firstSpawn.spawnCreep([WORK, WORK, CARRY, CARRY, MOVE, MOVE], name, { memory });
|
||||
console.log(`Make creep "${role}"`, 'err', err);
|
||||
}
|
||||
}
|
||||
@ -58,7 +58,7 @@ export const loop = ErrorMapper.wrapLoop(() => {
|
||||
|
||||
callHarvestersFromOthers(2);
|
||||
|
||||
makeCreep(CreepRole.HARVESTER, 2);
|
||||
makeCreep(CreepRole.HARVESTER, 4);
|
||||
makeCreep(CreepRole.UPGRADER, 4);
|
||||
makeCreep(CreepRole.BUILDER, 4);
|
||||
|
||||
|
8
src/types.d.ts
vendored
8
src/types.d.ts
vendored
@ -2,7 +2,15 @@
|
||||
|
||||
// memory extension samples
|
||||
interface CreepMemory {
|
||||
/**
|
||||
* Строка, которая определяет роль крипа,
|
||||
* то есть действия, которые крип совершает.
|
||||
*/
|
||||
role: string;
|
||||
/**
|
||||
* Действие, которое крип будет выполнять.
|
||||
*/
|
||||
action: string | undefined;
|
||||
room: string;
|
||||
working: boolean;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user