changed git root
This commit is contained in:
7
default/.vscode/settings.json
vendored
Normal file
7
default/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"files.exclude": {
|
||||
".*": true,
|
||||
"LICENSE": true,
|
||||
"*.md": true
|
||||
}
|
||||
}
|
||||
166
default/ClassCreep.js
Normal file
166
default/ClassCreep.js
Normal file
@@ -0,0 +1,166 @@
|
||||
module.exports = {
|
||||
setup: function () { Creep.prototype = _Creep.prototype; }
|
||||
}
|
||||
|
||||
class _Creep extends Creep {
|
||||
init() {
|
||||
if (this.memory.role == undefined) this.memory.role = ROLE_HARVESTER;
|
||||
if (this.memory.recharge == undefined) this.memory.recharge = true;
|
||||
}
|
||||
|
||||
tick(){
|
||||
switch (this.memory.role) {
|
||||
case ROLE_HARVESTER:
|
||||
if (this.memory.recharge) this.harvestSource();
|
||||
else this.harvesterJobs();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
toggleChargeMode() {
|
||||
if (this.store.getFreeCapacity(RESOURCE_ENERGY) == 0) {
|
||||
this.memory.recharge = false;
|
||||
} else if (this.store.getUsedCapacity(RESOURCE_ENERGY) == 0) {
|
||||
this.memory.recharge = true;
|
||||
} else return;
|
||||
this.memory.target = undefined;
|
||||
this.memory.path = undefined;
|
||||
}
|
||||
|
||||
findTarget(TARGET) {
|
||||
if (this.memory.target == undefined) {
|
||||
const target = this.pos.findClosestByPath(TARGET);
|
||||
if (target == null) return ERR_NOT_FOUND;
|
||||
else this.memory.target = target.id;
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
findTarget(TARGET, filter) {
|
||||
if (this.memory.target == undefined) {
|
||||
const target = this.pos.findClosestByPath(TARGET, filter);
|
||||
if (target == null) return ERR_NOT_FOUND;
|
||||
else this.memory.target = target.id;
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
harvestSource() {
|
||||
if (this.findTarget(FIND_SOURCES, {
|
||||
filter: (s) => {
|
||||
const area = s.room.lookAtArea(s.pos.y - 1, s.pos.x - 1, s.pos.y + 1, s.pos.y + 1, true);
|
||||
var free_space = 0;
|
||||
area.forEach(entry => {
|
||||
if (entry.type == "creep") free_space--;
|
||||
else if (entry.type == "terrain" && entry.terrain == "plain") free_space++;
|
||||
else if (entry.type == "terrain" && entry.terrain == "swamp") free_space++;
|
||||
});
|
||||
return free_space > 0;
|
||||
}
|
||||
}) == ERR_NOT_FOUND) return;
|
||||
const target = Game.getObjectById(this.memory.target);
|
||||
if (this.moveToTarget() == OK) {
|
||||
this.harvest(target);
|
||||
this.toggleChargeMode();
|
||||
}
|
||||
}
|
||||
|
||||
harvesterJobs() {
|
||||
if (this.memory.counter == undefined) this.memory.counter = 0;
|
||||
switch (this.memory.counter % 4) {
|
||||
case 0: this.supplyStructures();
|
||||
break;
|
||||
case 1: this.upgradeRoom();
|
||||
break;
|
||||
case 2: this.repairStructures();
|
||||
break;
|
||||
case 3: this.buildConstructionSites();
|
||||
break;
|
||||
default:
|
||||
this.memory.counter++;
|
||||
break;
|
||||
}
|
||||
if (this.memory.target == undefined) this.memory.counter++;
|
||||
}
|
||||
|
||||
supplyStructures() {
|
||||
if (this.findTarget(FIND_MY_STRUCTURES, {
|
||||
filter: (s) => {
|
||||
return (s.structureType == STRUCTURE_SPAWN ||
|
||||
s.structureType == STRUCTURE_EXTENSION ||
|
||||
s.structureType == STRUCTURE_TOWER ||
|
||||
s.structureType == STRUCTURE_STORAGE)
|
||||
&& 0 < s.store.getFreeCapacity();
|
||||
}
|
||||
}) == ERR_NOT_FOUND) return;
|
||||
const target = Game.getObjectById(this.memory.target);
|
||||
if (this.moveToTarget() == OK) {
|
||||
this.transfer(target, RESOURCE_ENERGY);
|
||||
this.toggleChargeMode();
|
||||
}
|
||||
}
|
||||
|
||||
upgradeRoom() {
|
||||
this.memory.target = this.room.controller.id;
|
||||
if (this.moveToTarget() == OK) {
|
||||
if (this.upgradeController(this.room.controller) == OK) return;
|
||||
this.toggleChargeMode();
|
||||
}
|
||||
}
|
||||
|
||||
repairStructures() {
|
||||
if (this.findTarget(FIND_MY_STRUCTURES, {
|
||||
filter: (s) => {
|
||||
return s.hits < s.hitsMax &&
|
||||
s.structureType != STRUCTURE_WALL &&
|
||||
s.structureType != STRUCTURE_RAMPART;
|
||||
}
|
||||
}) == ERR_NOT_FOUND);
|
||||
if (this.findTarget(FIND_MY_STRUCTURES, {
|
||||
filter: (s) => {
|
||||
return s.hits < s.hitsMax;
|
||||
}
|
||||
}) == ERR_NOT_FOUND) return;
|
||||
|
||||
const target = Game.getObjectById(this.memory.target.id);
|
||||
if (this.moveToTarget() == OK) {
|
||||
this.repair(target);
|
||||
this.toggleChargeMode();
|
||||
}
|
||||
}
|
||||
|
||||
buildConstructionSites() {
|
||||
if (this.findTarget(FIND_MY_CONSTRUCTION_SITES) == ERR_NOT_FOUND) return;
|
||||
const target = Game.getObjectById(this.memory.target);
|
||||
if (this.moveToTarget() == OK) {
|
||||
this.build(target);
|
||||
this.toggleChargeMode();
|
||||
}
|
||||
}
|
||||
|
||||
moveToTarget() {
|
||||
const target = Game.getObjectById(this.memory.target)
|
||||
if (this.pos.inRangeTo(target.pos.x, target.pos.y, 1)) {
|
||||
this.memory.path = undefined;
|
||||
return OK;
|
||||
}
|
||||
if (this.pos.inRangeTo(target.pos.x, target.pos.y, 5)) {
|
||||
this.moveTo(target)
|
||||
return ERR_BUSY;
|
||||
}
|
||||
if (this.memory.path == undefined) {
|
||||
this.memory.path = this.pos.findPathTo(target.pos, {
|
||||
ignoreCreeps: true, range: 1
|
||||
});
|
||||
}
|
||||
const response = this.moveByPath(this.memory.path);
|
||||
if (response == OK) return ERR_BUSY;
|
||||
if (response == ERR_INVALID_ARGS || response == ERR_NOT_FOUND) {
|
||||
this.moveTo(target);
|
||||
this.memory.path = undefined;
|
||||
return ERR_BUSY;
|
||||
}
|
||||
}
|
||||
}
|
||||
15
default/ClassRoom.js
Normal file
15
default/ClassRoom.js
Normal file
@@ -0,0 +1,15 @@
|
||||
module.exports = {
|
||||
setup: function () { Room.prototype = _Room.prototype; }
|
||||
}
|
||||
|
||||
|
||||
class _Room extends Room {
|
||||
// Initialize the room object
|
||||
init() {
|
||||
// Initialize memory
|
||||
if (this.memory.state == undefined) this.memory.state = ROOMSTATE_NEUTRAL;
|
||||
|
||||
// Start game when spawn is in neutral room make it the capital.
|
||||
if (this.controller.my && this.memory.state == ROOMSTATE_NEUTRAL) this.memory.state = ROOMSTATE_CAPITAL;
|
||||
}
|
||||
}
|
||||
37
default/ClassSpawn.js
Normal file
37
default/ClassSpawn.js
Normal file
@@ -0,0 +1,37 @@
|
||||
module.exports = {
|
||||
setup: function () { StructureSpawn.prototype = _Spawn.prototype; }
|
||||
}
|
||||
|
||||
|
||||
class _Spawn extends StructureSpawn {
|
||||
init() {
|
||||
}
|
||||
|
||||
requireNewCreep(role) {
|
||||
const roleCount = this.room.find(FIND_MY_CREEPS, {
|
||||
filter: (c) => { return c.memory.role == role; }
|
||||
}).length;
|
||||
|
||||
switch (role) {
|
||||
case ROLE_HARVESTER:
|
||||
return roleCount < 10;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
createHarvester() {
|
||||
var body = [WORK, CARRY, MOVE];
|
||||
this.createCreep(body, "harvester", ROLE_HARVESTER);
|
||||
}
|
||||
|
||||
createCreep(body, name, role) {
|
||||
if (this.memory.creepCounter == undefined) this.memory.creepCounter = 0;
|
||||
const response = this.spawnCreep(body, name + ": " + this.memory.creepCounter, {
|
||||
memory: { role: role }
|
||||
});
|
||||
if (response == OK) this.memory.creepCounter++;
|
||||
}
|
||||
}
|
||||
11
default/_commandHandler.js
Normal file
11
default/_commandHandler.js
Normal file
@@ -0,0 +1,11 @@
|
||||
module.exports = {
|
||||
// Gets called when recompiling and when Restart command is called.
|
||||
start: function () {
|
||||
global.ClearConstructions = clearConstructions;
|
||||
}
|
||||
}
|
||||
|
||||
function clearConstructions(){
|
||||
|
||||
}
|
||||
|
||||
21
default/_creepsHandler.js
Normal file
21
default/_creepsHandler.js
Normal file
@@ -0,0 +1,21 @@
|
||||
module.exports = {
|
||||
// Gets called when recompiling and when Restart command is called.
|
||||
start: function () {
|
||||
for (const creep_name in Game.creeps) {
|
||||
const creep = Game.creeps[creep_name];
|
||||
creep.init();
|
||||
}
|
||||
},
|
||||
|
||||
// Gets called every tick.
|
||||
update: function () {
|
||||
for (const creep_name in Memory.creeps) {
|
||||
const creep = Game.creeps[creep_name];
|
||||
if (creep == undefined) {
|
||||
delete Memory.creeps[creep_name];
|
||||
return;
|
||||
}
|
||||
creep.tick();
|
||||
}
|
||||
}
|
||||
}
|
||||
14
default/_roomsHandler.js
Normal file
14
default/_roomsHandler.js
Normal file
@@ -0,0 +1,14 @@
|
||||
module.exports = {
|
||||
start: function () {
|
||||
for (const room_name in Game.rooms) {
|
||||
const room = Game.rooms[room_name];
|
||||
room.init();
|
||||
}
|
||||
},
|
||||
|
||||
update: function () {
|
||||
for (const room_name in Game.rooms) {
|
||||
const room = Game.rooms[room_name];
|
||||
}
|
||||
}
|
||||
}
|
||||
15
default/_spawnsHandler.js
Normal file
15
default/_spawnsHandler.js
Normal file
@@ -0,0 +1,15 @@
|
||||
module.exports = {
|
||||
start: function () {
|
||||
for (const spawn_name in Game.spawns) {
|
||||
const spawn = Game.spawns[spawn_name];
|
||||
spawn.init();
|
||||
}
|
||||
},
|
||||
|
||||
update: function () {
|
||||
for (const spawn_name in Game.spawns) {
|
||||
const spawn = Game.spawns[spawn_name];
|
||||
if (spawn.requireNewCreep(ROLE_HARVESTER)) spawn.createHarvester();
|
||||
}
|
||||
}
|
||||
}
|
||||
47
default/main.js
Normal file
47
default/main.js
Normal file
@@ -0,0 +1,47 @@
|
||||
|
||||
/* ######################
|
||||
# Screeps 2022 #
|
||||
######################*/
|
||||
|
||||
const system = require("system");
|
||||
|
||||
module.exports.loop = function () {
|
||||
if (global.compiled == undefined) {
|
||||
// Configure classes and constants
|
||||
setupClasses();
|
||||
setConstants();
|
||||
console.log("Script recompiled...");
|
||||
global.compiled = true;
|
||||
} else {
|
||||
if (global.started == undefined) {
|
||||
// After one tick of configuration, restart the system.
|
||||
system.start();
|
||||
console.log("System started...");
|
||||
global.started = true;
|
||||
}
|
||||
system.update();
|
||||
}
|
||||
}
|
||||
|
||||
const creepClass = require("ClassCreep");
|
||||
const roomClass = require("ClassRoom");
|
||||
const spawnClass = require("ClassSpawn");
|
||||
|
||||
// Overwrite all game defined classes by inherited ones.
|
||||
function setupClasses() {
|
||||
creepClass.setup();
|
||||
roomClass.setup();
|
||||
spawnClass.setup();
|
||||
}
|
||||
|
||||
// Adds additional constants to global scope
|
||||
function setConstants() {
|
||||
// The states of a room.
|
||||
global.ROOMSTATE_NEUTRAL = 0;
|
||||
global.ROOMSTATE_CAPITAL = 1;
|
||||
global.ROOMSTATE_COLONY = 2;
|
||||
global.ROOMSTATE_OUTPOST = 3;
|
||||
|
||||
// The roles of creeps.
|
||||
global.ROLE_HARVESTER = 0;
|
||||
}
|
||||
24
default/system.js
Normal file
24
default/system.js
Normal file
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Basis of the entire system. Everything will be managed from here on out.
|
||||
*/
|
||||
const rooms = require("_roomsHandler")
|
||||
const spawns = require("_spawnsHandler")
|
||||
const creeps = require("_creepsHandler")
|
||||
const commands = require("_commandHandler")
|
||||
|
||||
module.exports = {
|
||||
// Gets called when recompiling and when Restart command is called.
|
||||
start: function () {
|
||||
rooms.start();
|
||||
spawns.start();
|
||||
creeps.start();
|
||||
commands.start();
|
||||
},
|
||||
|
||||
// Gets called every tick.
|
||||
update: function () {
|
||||
rooms.update();
|
||||
spawns.update();
|
||||
creeps.update();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user