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 = {
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;

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() {
var body = [WORK, CARRY, MOVE];
this.createCreep(body, "harvester", ROLE_HARVESTER);
this.createCreep(body, "harvester", Role.HARVESTER);
}
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 () {
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();
}

View File

@@ -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());
}
}
}