|
| 1 | +'use strict' |
| 2 | + |
| 3 | + |
| 4 | +class CityDismantle extends kernel.process { |
| 5 | + constructor (...args) { |
| 6 | + super(...args) |
| 7 | + this.priority = PRIORITIES_DEFAULT |
| 8 | + } |
| 9 | + getDescriptor () { |
| 10 | + return `Dismantling ${this.structure} in ${this.data.room}` |
| 11 | + } |
| 12 | + |
| 13 | + main () { |
| 14 | + if (!Game.rooms[this.data.room]) { |
| 15 | + return this.suicide() |
| 16 | + } |
| 17 | + if(this.room.find(FIND_FLAGS, { filter: { name: "dismantle" } }).length === 0) { |
| 18 | + return this.suicide |
| 19 | + } |
| 20 | + |
| 21 | + this.room = Game.rooms[this.data.room] |
| 22 | + this.flag = this.room.find(FIND_FLAGS, { filter: { name: "dismantle" } })[0] |
| 23 | + |
| 24 | + const target = this.flag.pos.findClosestByRange(FIND_STRUCTURES); |
| 25 | + if (target.pos != this.flag.pos) { |
| 26 | + return this.suicide |
| 27 | + } |
| 28 | + |
| 29 | + const dismantler = this.getCluster(`dismantler`, this.room) |
| 30 | + |
| 31 | + dismantler.sizeCluster('dismantler', 2) |
| 32 | + let storage |
| 33 | + if (this.room.storage && target != this.room.storage) { |
| 34 | + storage = this.room.storage |
| 35 | + } else if (this.room.terminal && target != this.room.terminal) { |
| 36 | + storage = this.room.terminal |
| 37 | + } else { |
| 38 | + const containers = this.room.structures[STRUCTURE_CONTAINER] |
| 39 | + if (containers && containers.length > 0) { |
| 40 | + if (containers.length > 1) { |
| 41 | + containers.sort((a, b) => a.store[RESOURCE_ENERGY] - b.store[RESOURCE_ENERGY]) |
| 42 | + } |
| 43 | + storage = containers[0] |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + dismantler.forEach(function (creep) { |
| 49 | + if (creep.ticksToLive < 10) { |
| 50 | + creep.recycle() |
| 51 | + return |
| 52 | + } |
| 53 | + if (creep.getCarryPercentage() > 1) { |
| 54 | + if (!creep.pos.isNearTo(storage)) { |
| 55 | + creep.travelTo(storage) |
| 56 | + } else { |
| 57 | + creep.store.forEach( r => { |
| 58 | + creep.transferAll(storage, r) |
| 59 | + }) |
| 60 | + } |
| 61 | + return |
| 62 | + } |
| 63 | + if (!creep.pos.isNearTo(target)) { |
| 64 | + creep.travelTo(target) |
| 65 | + return |
| 66 | + } |
| 67 | + if (creep.pos.isNearTo(target)) { |
| 68 | + if(target.store && target.store.getUsedCapacity() > 0) { |
| 69 | + target.store.forEach( resource => { |
| 70 | + creep.withdraw(resource) |
| 71 | + if (creep.store.getFreeCapacity() > 0) { |
| 72 | + return |
| 73 | + } |
| 74 | + }) |
| 75 | + } else { |
| 76 | + creep.dismantle(target) |
| 77 | + } |
| 78 | + } |
| 79 | + }) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +export default CityDismantle |
0 commit comments