From 032cad4e24ed83688125d0944dbc6bfd295aa71f Mon Sep 17 00:00:00 2001 From: ObeseTermite Date: Thu, 5 Jun 2025 11:37:55 -0700 Subject: [PATCH] Removed classes, as nodejs 10 doesn't support them properly (mega bruh) --- role_base.js | 26 +++++++++++--------- role_builder.js | 46 ++++++++++++++++------------------ role_dispatcher.js | 8 ++++-- role_guard.js | 27 +++++++++----------- role_harvester.js | 35 ++++++++++++-------------- role_repairer.js | 61 ++++++++++++++++++++++------------------------ role_upgrader.js | 48 +++++++++++++++++------------------- 7 files changed, 119 insertions(+), 132 deletions(-) diff --git a/role_base.js b/role_base.js index aef7de5..843d491 100644 --- a/role_base.js +++ b/role_base.js @@ -1,13 +1,15 @@ -class roleBase{ - static body = [ MOVE, WORK, CARRY ]; - static name = 'Creep'; - /** @param {Creep} creep **/ - run(creep) { } - static spawn(spawnPoint){ - spawnPoint.spawnCreep(this.body, this.name + Game.time, - {memory : {role : this.name} - }); - } -}; +var body = [ MOVE, WORK, CARRY ]; +var name = 'Creep'; -module.exports = roleBase; +function run(creep) { } + +function spawn(spawnPoint, newBody, newName){ + if(newBody === 'undefined') newBody = body; + if(newName === 'undefined') newName = name; + + spawnPoint.spawnCreep(newBody, newName + Game.time, + {memory : {role : newName} + }); +} + +module.exports = {body,name,run,spawn}; diff --git a/role_builder.js b/role_builder.js index 961a045..30f48c0 100644 --- a/role_builder.js +++ b/role_builder.js @@ -1,30 +1,26 @@ var energyUtils = require('util_energy'); -var roleBase = require('role_base'); -class roleBuilder extends roleBase{ - /** @param {Creep} creep **/ - static name = 'builder'; +var name = 'builder'; - static run(creep) { - if(creep.memory.building && creep.store[RESOURCE_ENERGY] == 0) { - creep.memory.building = false; - } - if(!creep.memory.building && creep.store.getFreeCapacity() == 0) { - creep.memory.building = true; - } +function run(creep) { + if(creep.memory.building && creep.store[RESOURCE_ENERGY] == 0) { + creep.memory.building = false; + } + if(!creep.memory.building && creep.store.getFreeCapacity() == 0) { + creep.memory.building = true; + } - if(creep.memory.building) { - var targets = creep.room.find(FIND_CONSTRUCTION_SITES); - if(targets.length) { - if(creep.build(targets[0]) == ERR_NOT_IN_RANGE) { - creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}}); - } - } - } - else { - energyUtils.gatherEnergy(creep); - } - } -}; + if(creep.memory.building) { + var targets = creep.room.find(FIND_CONSTRUCTION_SITES); + if(targets.length) { + if(creep.build(targets[0]) == ERR_NOT_IN_RANGE) { + creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}}); + } + } + } + else { + energyUtils.gatherEnergy(creep); + } +} -module.exports = roleBuilder; +module.exports = {name,run}; diff --git a/role_dispatcher.js b/role_dispatcher.js index bb56376..fc3dc31 100644 --- a/role_dispatcher.js +++ b/role_dispatcher.js @@ -3,6 +3,7 @@ var roleUpgrader = require('role_upgrader'); var roleBuilder = require('role_builder'); var roleRepairer = require('role_repairer'); var roleGuard = require('role_guard'); +var roleBase = require('role_base'); const roleMap = { harvester: roleHarvester, @@ -23,7 +24,7 @@ var creepCounts = { function runRole(creep){ const role = roleMap[creep.memory.role]; - if(role !== 'undefined'){ + if(typeof role.run === 'function'){ role.run(creep); } else { console.log('Unknown or undefined role: ' + creep.memory.role); @@ -35,7 +36,10 @@ function spawnCreeps(spawn){ var roleCreeps = _.filter(Game.creeps, (creep) => creep.memory.role == role); if(roleCreeps.length < count && !spawn.spawning) { - roleMap[role].spawn(spawn); + if(typeof roleMap[role].spawn === 'function') + roleMap[role].spawn(spawn); + else + roleBase.spawn(spawn, roleMap[role].body, roleMap[role].name); break; } } diff --git a/role_guard.js b/role_guard.js index f8ee11f..044ec17 100644 --- a/role_guard.js +++ b/role_guard.js @@ -1,10 +1,7 @@ -var roleBase = require('role_base'); +var body = [ MOVE, MOVE, RANGED_ATTACK, RANGED_ATTACK ]; +var name = 'streltsy' -class roleGuard extends roleBase{ - static body = [ MOVE, MOVE, RANGED_ATTACK, RANGED_ATTACK ]; - static name = 'streltsy' - /** @param {Creep} creep **/ - static run(creep) { +function run(creep) { const target = creep.pos.findClosestByRange(FIND_HOSTILE_CREEPS); if(target) { if(creep.rangedAttack(target) == ERR_NOT_IN_RANGE) { @@ -24,14 +21,14 @@ class roleGuard extends roleBase{ } } } - static spawn(spawnPoint, newWaypoints){ - if(typeof newWaypoints === 'undefined') - newWaypoints = ['A','B']; - spawnPoint.spawnCreep(this.body, this.name + Game.time, - {memory : {role : this.name, waypoints : newWaypoints, currentWaypoint : 0} - }); - } -}; +function spawn(spawnPoint, newWaypoints){ + if(typeof newWaypoints === 'undefined') + newWaypoints = ['A','B']; -module.exports = roleGuard; + spawnPoint.spawnCreep(this.body, this.name + Game.time, + {memory : {role : this.name, waypoints : newWaypoints, currentWaypoint : 0} + }); +} + +module.exports = {body,name,run,spawn}; diff --git a/role_harvester.js b/role_harvester.js index ba6b30a..4393b2d 100644 --- a/role_harvester.js +++ b/role_harvester.js @@ -1,24 +1,19 @@ var energyUtils = require('util_energy'); -var roleBase = require('role_base'); -class roleHarvester extends roleBase{ - /** @param {Creep} creep **/ +var name = 'harvester'; - static name = 'harvester'; +function run(creep) { + if(creep.store.getFreeCapacity() > 0) { + var sources = creep.room.find(FIND_SOURCES); + if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { + creep.moveTo(sources[0], {visualizePathStyle: {stroke: '#ffaa00'}}); + } + } + else { + if(!energyUtils.depositEnergy(creep)){ + creep.moveTo(Game.flags.IdleArea); + } + } +} - static run(creep) { - if(creep.store.getFreeCapacity() > 0) { - var sources = creep.room.find(FIND_SOURCES); - if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { - creep.moveTo(sources[0], {visualizePathStyle: {stroke: '#ffaa00'}}); - } - } - else { - if(!energyUtils.depositEnergy(creep)){ - creep.moveTo(Game.flags.IdleArea); - } - } - } -}; - -module.exports = roleHarvester; +module.exports = {name,run}; diff --git a/role_repairer.js b/role_repairer.js index d257501..b9f4332 100644 --- a/role_repairer.js +++ b/role_repairer.js @@ -1,36 +1,33 @@ var energyUtils = require('util_energy'); -var roleBase = require('role_base'); -class roleRepairer extends roleBase { - static name = 'repairer'; - /** @param {Creep} creep **/ - static run(creep) { - if(creep.memory.building && creep.store[RESOURCE_ENERGY] == 0) { - creep.memory.building = false; - } - if(!creep.memory.building && creep.store.getFreeCapacity() == 0) { - creep.memory.building = true; - } +var name = 'repairer'; - if(creep.memory.building) { - var targets = creep.room.find(FIND_STRUCTURES, { - filter: (structure) => { - return structure.hitsMax - structure.hits > 0; - } - }); - if(targets.length > 0) { - if(creep.repair(targets[0]) == ERR_NOT_IN_RANGE) { - creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}}); - } - } - else{ - creep.say("IDLE") - } - } - else { - energyUtils.gatherEnergy(creep); - } - } -}; +function run(creep) { + if(creep.memory.building && creep.store[RESOURCE_ENERGY] == 0) { + creep.memory.building = false; + } + if(!creep.memory.building && creep.store.getFreeCapacity() == 0) { + creep.memory.building = true; + } -module.exports = roleRepairer; + if(creep.memory.building) { + var targets = creep.room.find(FIND_STRUCTURES, { + filter: (structure) => { + return structure.hitsMax - structure.hits > 0; + } + }); + if(targets.length > 0) { + if(creep.repair(targets[0]) == ERR_NOT_IN_RANGE) { + creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}}); + } + } + else{ + creep.say("IDLE") + } + } + else { + energyUtils.gatherEnergy(creep); + } +} + +module.exports = {name,run}; diff --git a/role_upgrader.js b/role_upgrader.js index efb53d7..84e4038 100644 --- a/role_upgrader.js +++ b/role_upgrader.js @@ -1,30 +1,26 @@ -var roleBase = require('role_base'); +var name = 'upgrader'; -class roleUpgrader extends roleBase { - static name = 'upgrader'; +/** @param {Creep} creep **/ +function run(creep) { - /** @param {Creep} creep **/ - static run(creep) { + if(creep.memory.upgrading && creep.store[RESOURCE_ENERGY] == 0) { + creep.memory.upgrading = false; + } + if(!creep.memory.upgrading && creep.store.getFreeCapacity() == 0) { + creep.memory.upgrading = true; + } - if(creep.memory.upgrading && creep.store[RESOURCE_ENERGY] == 0) { - creep.memory.upgrading = false; - } - if(!creep.memory.upgrading && creep.store.getFreeCapacity() == 0) { - creep.memory.upgrading = true; - } + if(creep.memory.upgrading) { + if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) { + creep.moveTo(creep.room.controller, {visualizePathStyle: {stroke: '#ffffff'}}); + } + } + else { + var sources = creep.room.find(FIND_SOURCES); + if(creep.harvest(sources[1]) == ERR_NOT_IN_RANGE) { + creep.moveTo(sources[1], {visualizePathStyle: {stroke: '#ffaa00'}}); + } + } +} - if(creep.memory.upgrading) { - if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) { - creep.moveTo(creep.room.controller, {visualizePathStyle: {stroke: '#ffffff'}}); - } - } - else { - var sources = creep.room.find(FIND_SOURCES); - if(creep.harvest(sources[1]) == ERR_NOT_IN_RANGE) { - creep.moveTo(sources[1], {visualizePathStyle: {stroke: '#ffaa00'}}); - } - } - } -}; - -module.exports = roleUpgrader; +module.exports = {name,run};