Use random source got building
This commit is contained in:
parent
1fa5ab1e4e
commit
574204a837
@ -1,33 +1,49 @@
|
|||||||
|
interface BuilderMemory extends CreepMemory {
|
||||||
|
building: boolean | undefined;
|
||||||
|
sourceNum: number | undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
function randomIntInRange(min: number, max: number): number {
|
||||||
|
const delta = max - min;
|
||||||
|
return Math.floor(Math.random() * delta) + min;
|
||||||
|
}
|
||||||
|
|
||||||
export function runAsBuilder(creep: Creep) {
|
export function runAsBuilder(creep: Creep) {
|
||||||
const memory = creep.memory as CreepMemory & { building: boolean | undefined };
|
const memory = creep.memory as BuilderMemory;
|
||||||
|
|
||||||
if (memory.building && creep.store[RESOURCE_ENERGY] === 0) {
|
if (memory.building && creep.store[RESOURCE_ENERGY] === 0) {
|
||||||
memory.building = false;
|
memory.building = false;
|
||||||
creep.say('🔄 harvest');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!memory.building && creep.store.getFreeCapacity() === 0) {
|
if (!memory.building && creep.store.getFreeCapacity() === 0) {
|
||||||
memory.building = true;
|
memory.building = true;
|
||||||
creep.say('🚧 build');
|
}
|
||||||
|
|
||||||
|
if (memory.sourceNum === undefined) {
|
||||||
|
const sources = creep.room.find(FIND_SOURCES);
|
||||||
|
memory.sourceNum = randomIntInRange(0, sources.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (memory.building) {
|
if (memory.building) {
|
||||||
const targets = creep.room.find(FIND_CONSTRUCTION_SITES);
|
const targets = creep.room.find(FIND_CONSTRUCTION_SITES);
|
||||||
if (targets.length > 0) {
|
if (targets.length > 0) {
|
||||||
if (creep.build(targets[0]) == ERR_NOT_IN_RANGE) {
|
if (creep.build(targets[0]) == ERR_NOT_IN_RANGE) {
|
||||||
|
creep.say('🚧 build');
|
||||||
creep.moveTo(targets[0], { visualizePathStyle: { stroke: '#ffffff' } });
|
creep.moveTo(targets[0], { visualizePathStyle: { stroke: '#ffffff' } });
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const spawns = creep.room.find(FIND_MY_SPAWNS);
|
const spawns = creep.room.find(FIND_MY_SPAWNS);
|
||||||
if (spawns.length > 0) {
|
if (spawns.length > 0) {
|
||||||
creep.say('to spawn');
|
creep.say('To spawn');
|
||||||
creep.moveTo(spawns[0]);
|
creep.moveTo(spawns[0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const sources = creep.room.find(FIND_SOURCES);
|
const sources = creep.room.find(FIND_SOURCES);
|
||||||
if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
|
const sourceNum = memory.sourceNum;
|
||||||
creep.moveTo(sources[0], { visualizePathStyle: { stroke: '#ffaa00' } });
|
if (creep.harvest(sources[sourceNum]) == ERR_NOT_IN_RANGE) {
|
||||||
|
creep.say('🔄 harvest ' + sourceNum);
|
||||||
|
creep.moveTo(sources[sourceNum], { visualizePathStyle: { stroke: '#ffaa00' } });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ export const loop = ErrorMapper.wrapLoop(() => {
|
|||||||
|
|
||||||
makeCreep(CreepRole.HARVESTER, 1);
|
makeCreep(CreepRole.HARVESTER, 1);
|
||||||
makeCreep(CreepRole.UPGRADER, 3);
|
makeCreep(CreepRole.UPGRADER, 3);
|
||||||
makeCreep(CreepRole.BUILDER, 2);
|
makeCreep(CreepRole.BUILDER, 4);
|
||||||
|
|
||||||
// Process current creeps
|
// Process current creeps
|
||||||
for (let name in Game.creeps) {
|
for (let name in Game.creeps) {
|
||||||
|
Loading…
Reference in New Issue
Block a user