This commit is contained in:
Philipp Horstenkamp 2022-04-09 17:47:40 +02:00
commit 6024aac943
Signed by: Philipp
GPG Key ID: DD53EAC36AFB61B4
1 changed files with 53 additions and 0 deletions

53
main.mjs Normal file
View File

@ -0,0 +1,53 @@
import { getObjectsByPrototype } from '/game/utils';
import { Creep, StructureSpawn, Source } from '/game/prototypes';
import { MOVE, WORK, CARRY, ATTACK, RANGED_ATTACK, HEAL, TOUGH, ERR_NOT_IN_RANGE, RESOURCE_ENERGY } from '/game/constants';
import { } from '/arena';
var harvester = [];
var defender = [];
function harvest(creep) {
var mySpawn = getObjectsByPrototype(StructureSpawn)[0];
var source = getObjectsByPrototype(Source)[0];
if (creep.store.getFreeCapacity()) {
if (creep.harvest(source) == ERR_NOT_IN_RANGE) {
creep.moveTo(source);
}
} else {
if(creep.transfer(mySpawn, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(mySpawn);
}
}
}
function defend(defending_creep) {
var enemyCreep = getObjectsByPrototype(Creep).find(creep => !creep.my);
if(defending_creep.attack(enemyCreep) == ERR_NOT_IN_RANGE) {
defending_creep.moveTo(enemyCreep);
}
}
export function loop() {
var mySpawn = getObjectsByPrototype(StructureSpawn)[0];
harvester.forEach(creep => {
harvest(creep);
});
defender.forEach(creep => {
defend(creep);
});
if (harvester.length < 4) {
var creep = mySpawn.spawnCreep([MOVE, CARRY, WORK, WORK, WORK]).object;
if (creep != undefined && creep != null) {
harvester.push(creep);
}
} else {
var creep = mySpawn.spawnCreep([MOVE, ATTACK, MOVE, ATTACK, MOVE, ATTACK]).object;
if (creep != undefined && creep != null) {
defender.push(creep);
}
}
}