Advertisement
Guest User

Upgrader FSM

a guest
Jun 25th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class c_role_upgrader extends c_creep_base {
  2.  
  3.     fsm(creep) {
  4.     var start_state = creep.memory.state;
  5.     var new_state = 0;
  6.    
  7.     switch (start_state) {
  8.     case 0: // Gathering.  When we're at capacity, start moving.
  9.         if(creep.carry.energy == creep.carryCapacity) {
  10.         new_state = 1;
  11.             }
  12.         break;
  13.     case 1: // Distributing energy.  When we hit 0, go back to gathering.
  14.         if (creep.carry.energy == 0) {
  15.         new_state = 0;
  16.         }
  17.         break;
  18.     }
  19.     creep.memory.state = new_state;
  20.     return new_state;
  21.     }
  22.    
  23.     /** @param {Creep} creep **/
  24.     run(creep, chained) {
  25.     if (!chained) {
  26.         this.reincarnation_check(creep);
  27.     }
  28.  
  29.     var new_state = this.fsm(creep);
  30.  
  31.     // Take actions based on the new state
  32.     switch (new_state) {
  33.     case 0: // Take resources, either from a container or node
  34.         var source = Game.getObjectById(creep.memory.upgrade_source);
  35.         if (source instanceof StructureContainer) {
  36.         // Only take from containers that are >50% full.  Basic static-priority scheduling :(.  Fixme.
  37.         if (source.store[RESOURCE_ENERGY] < source.storeCapacity >> 1) {
  38.             return;
  39.         }
  40.         if(source.transfer(creep, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
  41.                     creep.moveTo(source);
  42.         }
  43.         } else {
  44.         // Harvest from sources
  45.         if (creep.harvest(source) == ERR_NOT_IN_RANGE) {
  46.             creep.moveTo(source);
  47.         }
  48.         }
  49.         break;
  50.     case 1: // Upgraede the node controller
  51.             if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
  52.                 creep.moveTo(creep.room.controller);
  53.             }
  54.         break;
  55.         }
  56.    
  57.     }
  58. };
  59.  
  60. var roleUpgrader = new c_role_upgrader();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement