Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class c_role_upgrader extends c_creep_base {
- fsm(creep) {
- var start_state = creep.memory.state;
- var new_state = 0;
- switch (start_state) {
- case 0: // Gathering. When we're at capacity, start moving.
- if(creep.carry.energy == creep.carryCapacity) {
- new_state = 1;
- }
- break;
- case 1: // Distributing energy. When we hit 0, go back to gathering.
- if (creep.carry.energy == 0) {
- new_state = 0;
- }
- break;
- }
- creep.memory.state = new_state;
- return new_state;
- }
- /** @param {Creep} creep **/
- run(creep, chained) {
- if (!chained) {
- this.reincarnation_check(creep);
- }
- var new_state = this.fsm(creep);
- // Take actions based on the new state
- switch (new_state) {
- case 0: // Take resources, either from a container or node
- var source = Game.getObjectById(creep.memory.upgrade_source);
- if (source instanceof StructureContainer) {
- // Only take from containers that are >50% full. Basic static-priority scheduling :(. Fixme.
- if (source.store[RESOURCE_ENERGY] < source.storeCapacity >> 1) {
- return;
- }
- if(source.transfer(creep, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
- creep.moveTo(source);
- }
- } else {
- // Harvest from sources
- if (creep.harvest(source) == ERR_NOT_IN_RANGE) {
- creep.moveTo(source);
- }
- }
- break;
- case 1: // Upgraede the node controller
- if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
- creep.moveTo(creep.room.controller);
- }
- break;
- }
- }
- };
- var roleUpgrader = new c_role_upgrader();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement