Simplyfied the code. Ready to rebuild on top of.

This commit is contained in:
unknown
2023-08-20 16:31:21 +02:00
parent 15c86d3c14
commit ae3812aa83
10 changed files with 71 additions and 137 deletions

View File

@@ -1,15 +0,0 @@
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;
}
}

View File

@@ -1,16 +1,27 @@
module.exports = { module.exports = {
setup: function () { Creep.prototype = _Creep.prototype; } setup: function () {
Creep.prototype = _Creep.prototype;
global.Role = Role;
}
}
const Role = {
HARVESTER: 0
}
const State = {
} }
class _Creep extends Creep { class _Creep extends Creep {
init() { begin() {
if (this.memory.role == undefined) this.memory.role = ROLE_HARVESTER; if (this.memory.role == undefined) this.memory.role = Role.HARVESTER;
if (this.memory.recharge == undefined) this.memory.recharge = true; if (this.memory.recharge == undefined) this.memory.recharge = true;
} }
tick(){ tick(){
switch (this.memory.role) { switch (this.memory.role) {
case ROLE_HARVESTER: case Role.HARVESTER:
if (this.memory.recharge) this.harvestSource(); if (this.memory.recharge) this.harvestSource();
else this.harvesterJobs(); else this.harvesterJobs();
break; break;

17
Room.js Normal file
View File

@@ -0,0 +1,17 @@
module.exports = {
setup: function () { Room.prototype = _Room.prototype; }
}
class _Room extends Room {
begin(){}
tick(){
const creepsCount = this.find(FIND_MY_CREEPS).length;
console.log(creepsCount);
if(creepsCount < 5){
const spawn = this.find(FIND_MY_STRUCTURES, {filter: {structureType: STRUCTURE_SPAWN}})[0];
spawn.createHarvester();
}
}
}

View File

@@ -24,7 +24,7 @@ class _Spawn extends StructureSpawn {
createHarvester() { createHarvester() {
var body = [WORK, CARRY, MOVE]; var body = [WORK, CARRY, MOVE];
this.createCreep(body, "harvester", ROLE_HARVESTER); this.createCreep(body, "harvester", Role.HARVESTER);
} }
createCreep(body, name, role) { createCreep(body, name, role) {

View File

@@ -1,11 +0,0 @@
module.exports = {
// Gets called when recompiling and when Restart command is called.
start: function () {
global.ClearConstructions = clearConstructions;
}
}
function clearConstructions(){
}

View File

@@ -1,21 +0,0 @@
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();
}
}
}

View File

@@ -1,14 +0,0 @@
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];
}
}
}

View File

@@ -1,15 +0,0 @@
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();
}
}
}

54
main.js
View File

@@ -1,47 +1,27 @@
// ########################################
// ########## MAIN ##########
// ########################################
/* ######################
# Screeps 2022 #
######################*/
const system = require("system"); const system = require("System");
module.exports.loop = function () { module.exports.loop = function () {
if (global.compiled == undefined) { if(!global.compiled) onRecompile();
// Configure classes and constants else if (!global.started) onRestart();
setupClasses(); else onTick();
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"); function onRecompile(){
const roomClass = require("ClassRoom"); system.setup();
const spawnClass = require("ClassSpawn"); console.log("Script recompiled...");
global.compiled = true;
// Overwrite all game defined classes by inherited ones.
function setupClasses() {
creepClass.setup();
roomClass.setup();
spawnClass.setup();
} }
// Adds additional constants to global scope function onRestart(){
function setConstants() { system.begin();
// The states of a room. global.started = true;
global.ROOMSTATE_NEUTRAL = 0; }
global.ROOMSTATE_CAPITAL = 1;
global.ROOMSTATE_COLONY = 2;
global.ROOMSTATE_OUTPOST = 3;
// The roles of creeps. function onTick(){
global.ROLE_HARVESTER = 0; system.tick();
} }

View File

@@ -1,24 +1,26 @@
/** const creepClass = require("Creep");
* Basis of the entire system. Everything will be managed from here on out. const roomClass = require("Room");
*/ const spawnClass = require("Spawn");
const rooms = require("_roomsHandler")
const spawns = require("_spawnsHandler") // ########################################
const creeps = require("_creepsHandler") // ########## SYSTEM ##########
const commands = require("_commandHandler") // ########################################
module.exports = { module.exports = {
// Gets called when recompiling and when Restart command is called.
start: function () { setup: function () {
rooms.start(); creepClass.setup();
spawns.start(); roomClass.setup();
creeps.start(); spawnClass.setup();
commands.start();
}, },
// Gets called every tick. begin: function () {
update: function () { Object.values(Game.rooms).forEach(room => room.begin());
rooms.update(); Object.values(Game.creeps).forEach(creep => creep.begin());
spawns.update(); },
creeps.update();
tick: function () {
Object.values(Game.rooms).forEach(room => room.tick());
Object.values(Game.creeps).forEach(creep => creep.tick());
} }
} }