'use strict';

/*
 * reserver is used to reserve controller in external harvesting rooms
 *
 * Moves to the controller and reserves
 * Currently checks if there are enough sourcer and maybe trigger a defender.
 */

roles.reserver = {};
roles.reserver.killPrevious = true;
// TODO should be true, but flee must be fixed  (2016-10-13)
roles.reserver.flee = false;

roles.reserver.settings = {
  layoutString: 'MK',
  maxLayoutAmout: 1,
};
roles.reserver.updateSettings = function(room, creep) {
  let level = creep.level ? creep.level : 1;
  if (level === 2) {
    return {
      amount: [2, 2]
    };
  }
  if (level === 5) {
    room.log('Build super reserver');
    return {
      amount: [5, 5]
    };
  }
};

roles.reserver.action = function(creep) {
  creep.mySignController();
  if (!creep.memory.routing.targetId) {
    // TODO check when this happens and fix it
    creep.log('creep_reserver.action No targetId !!!!!!!!!!!' + JSON.stringify(creep.memory));
    if (creep.room.name === creep.memory.routing.targetRoom) {
      creep.memory.routing.targetId = creep.room.controller.id;
    }
  }

  // TODO this should be enabled, because the reserver should flee without being attacked
  creep.notifyWhenAttacked(false);

  creep.handleReserver();
  return true;
};

roles.reserver.execute = function(creep) {
  creep.log('Execute!!!');
};

@@ -496,138 +496,169 @@
 let 
-level = creep.level ? creep.level : 1;%0A  if (level === 2) %7B%0A    return %7B%0A      amount: %5B2, 2%5D%0A    %7D;%0A  %7D%0A  if (level === 5
+partsData = room.getPartsStringDatas(roles.reserver.settings.layoutString);%0A  let level = creep.level ? creep.level : 1;%0A  for (var l = level; l %3E 0; l--
 ) %7B%0A    
 room
@@ -657,46 +657,71 @@
     
+if (
 room.
-log('Build super reserver');%0A
+energyCapacityAvailable %3E partsData.cost * l) %7B%0A 
     
+ 
 retu
@@ -725,16 +725,18 @@
 eturn %7B%0A
+  
       am
@@ -746,21 +746,29 @@
 t: %5B
-5, 5
+l, l
 %5D%0A    
+  
 %7D;%0A
+    %7D%0A
   %7D%0A

Improve reserver