From ae3812aa830c88bb9ea3564dd43438adefdc8558 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 20 Aug 2023 16:31:21 +0200 Subject: [PATCH] Simplyfied the code. Ready to rebuild on top of. --- ClassRoom.js | 15 ----------- ClassCreep.js => Creep.js | 19 +++++++++++--- Room.js | 17 ++++++++++++ ClassSpawn.js => Spawn.js | 2 +- _commandHandler.js | 11 -------- _creepsHandler.js | 21 --------------- _roomsHandler.js | 14 ---------- _spawnsHandler.js | 15 ----------- main.js | 54 ++++++++++++--------------------------- system.js | 40 +++++++++++++++-------------- 10 files changed, 71 insertions(+), 137 deletions(-) delete mode 100644 ClassRoom.js rename ClassCreep.js => Creep.js (94%) create mode 100644 Room.js rename ClassSpawn.js => Spawn.js (93%) delete mode 100644 _commandHandler.js delete mode 100644 _creepsHandler.js delete mode 100644 _roomsHandler.js delete mode 100644 _spawnsHandler.js diff --git a/ClassRoom.js b/ClassRoom.js deleted file mode 100644 index fa17062..0000000 --- a/ClassRoom.js +++ /dev/null @@ -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; - } -} diff --git a/ClassCreep.js b/Creep.js similarity index 94% rename from ClassCreep.js rename to Creep.js index c9fd469..b4707a0 100644 --- a/ClassCreep.js +++ b/Creep.js @@ -1,16 +1,27 @@ 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 { - init() { - if (this.memory.role == undefined) this.memory.role = ROLE_HARVESTER; + begin() { + 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: + case Role.HARVESTER: if (this.memory.recharge) this.harvestSource(); else this.harvesterJobs(); break; diff --git a/Room.js b/Room.js new file mode 100644 index 0000000..845c9bc --- /dev/null +++ b/Room.js @@ -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(); + } + } +} + diff --git a/ClassSpawn.js b/Spawn.js similarity index 93% rename from ClassSpawn.js rename to Spawn.js index b116582..91458b7 100644 --- a/ClassSpawn.js +++ b/Spawn.js @@ -24,7 +24,7 @@ class _Spawn extends StructureSpawn { createHarvester() { var body = [WORK, CARRY, MOVE]; - this.createCreep(body, "harvester", ROLE_HARVESTER); + this.createCreep(body, "harvester", Role.HARVESTER); } createCreep(body, name, role) { diff --git a/_commandHandler.js b/_commandHandler.js deleted file mode 100644 index 8fb64c3..0000000 --- a/_commandHandler.js +++ /dev/null @@ -1,11 +0,0 @@ -module.exports = { - // Gets called when recompiling and when Restart command is called. - start: function () { - global.ClearConstructions = clearConstructions; - } -} - -function clearConstructions(){ - -} - diff --git a/_creepsHandler.js b/_creepsHandler.js deleted file mode 100644 index fadc7b7..0000000 --- a/_creepsHandler.js +++ /dev/null @@ -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(); - } - } -} \ No newline at end of file diff --git a/_roomsHandler.js b/_roomsHandler.js deleted file mode 100644 index 4325d8b..0000000 --- a/_roomsHandler.js +++ /dev/null @@ -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]; - } - } -} \ No newline at end of file diff --git a/_spawnsHandler.js b/_spawnsHandler.js deleted file mode 100644 index 4fda0fd..0000000 --- a/_spawnsHandler.js +++ /dev/null @@ -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(); - } - } -} \ No newline at end of file diff --git a/main.js b/main.js index 9fd9c3f..f548032 100644 --- a/main.js +++ b/main.js @@ -1,47 +1,27 @@ +// ######################################## +// ########## MAIN ########## +// ######################################## -/* ###################### - # Screeps 2022 # - ######################*/ -const system = require("system"); +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(); - } + if(!global.compiled) onRecompile(); + else if (!global.started) onRestart(); + else onTick(); } -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(); +function onRecompile(){ + system.setup(); + console.log("Script recompiled..."); + global.compiled = true; } -// 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; +function onRestart(){ + system.begin(); + global.started = true; +} - // The roles of creeps. - global.ROLE_HARVESTER = 0; +function onTick(){ + system.tick(); } \ No newline at end of file diff --git a/system.js b/system.js index 83b81a6..015bc1a 100644 --- a/system.js +++ b/system.js @@ -1,24 +1,26 @@ -/** - * 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") +const creepClass = require("Creep"); +const roomClass = require("Room"); +const spawnClass = require("Spawn"); + +// ######################################## +// ########## SYSTEM ########## +// ######################################## module.exports = { - // Gets called when recompiling and when Restart command is called. - start: function () { - rooms.start(); - spawns.start(); - creeps.start(); - commands.start(); + + setup: function () { + creepClass.setup(); + roomClass.setup(); + spawnClass.setup(); }, - // Gets called every tick. - update: function () { - rooms.update(); - spawns.update(); - creeps.update(); + begin: function () { + Object.values(Game.rooms).forEach(room => room.begin()); + Object.values(Game.creeps).forEach(creep => creep.begin()); + }, + + tick: function () { + Object.values(Game.rooms).forEach(room => room.tick()); + Object.values(Game.creeps).forEach(creep => creep.tick()); } -} +} \ No newline at end of file