Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | /** * src/roles/builder.js * 建設クリープ(Builder)の制御モジュール * * ビルダーはエネルギーを取得して建設サイトに建設作業を行う。 * 建設サイトがない場合は修復や、コントローラーアップグレードを補助する。 * 建設の優先度: コンテナ → エクステンション → スポーン → ランパート → ロード → その他 */ 'use strict'; const cache = require('../utils/cache'); const pathfinder = require('../utils/pathfinder'); const logger = require('../utils/logger'); const { MEMORY_KEYS } = require('../constants'); // ============================================================ // 建設優先度(低い数値 = 高優先) // ============================================================ const BUILD_PRIORITY = { [STRUCTURE_CONTAINER]: 1, [STRUCTURE_EXTENSION]: 2, [STRUCTURE_SPAWN]: 3, [STRUCTURE_TOWER]: 4, [STRUCTURE_STORAGE]: 5, [STRUCTURE_LINK]: 6, [STRUCTURE_ROAD]: 7, [STRUCTURE_RAMPART]: 8, [STRUCTURE_WALL]: 9, }; // ============================================================ // メイン制御 // ============================================================ /** * ビルダークリープのメインロジックを実行する * @param {Creep} creep */ function run(creep) { _updateWorkingState(creep); if (creep.memory[MEMORY_KEYS.WORKING]) { _build(creep); } else { _getEnergy(creep); } } // ============================================================ // 状態遷移 // ============================================================ /** * クリープのworking状態を更新する * @param {Creep} creep */ function _updateWorkingState(creep) { const energy = creep.store[RESOURCE_ENERGY]; const capacity = creep.store.getCapacity(RESOURCE_ENERGY); if (creep.memory[MEMORY_KEYS.WORKING] && energy === 0) { creep.memory[MEMORY_KEYS.WORKING] = false; creep.say('🔄 採掘'); delete creep.memory[MEMORY_KEYS.TARGET_ID]; } if (!creep.memory[MEMORY_KEYS.WORKING] && energy === capacity) { creep.memory[MEMORY_KEYS.WORKING] = true; creep.say('🔨 建設'); } } // ============================================================ // 建設ロジック // ============================================================ /** * 建設サイトに建設作業を行う * @param {Creep} creep */ function _build(creep) { const site = _getTargetSite(creep); if (!site) { // 建設サイトがなければ修復を行う _repairAsBackup(creep); return; } const result = creep.build(site); if (result === ERR_NOT_IN_RANGE) { pathfinder.moveTo(creep, site, { range: 3 }); // 建設進捗をビジュアル表示 const pct = site.progress / site.progressTotal; creep.room.visual.text( `🔨 ${(pct * 100).toFixed(0)}%`, site.pos.x, site.pos.y - 1, { color: '#ffaa00', font: 0.5, align: 'center' } ); } else if (result === ERR_INVALID_TARGET) { // ターゲットが無効になった(完成済み等) delete creep.memory[MEMORY_KEYS.TARGET_ID]; cache.invalidate(`construction_sites_${creep.room.name}`); } } /** * 優先度の高い建設サイトを返す * @param {Creep} creep * @returns {ConstructionSite|null} */ function _getTargetSite(creep) { // メモリに保存されたターゲットを優先的に使用 if (creep.memory[MEMORY_KEYS.TARGET_ID]) { const saved = Game.getObjectById(creep.memory[MEMORY_KEYS.TARGET_ID]); if (saved) return saved; delete creep.memory[MEMORY_KEYS.TARGET_ID]; } const sites = cache.getConstructionSites(creep.room); if (sites.length === 0) return null; // 優先度でソートし、同優先度なら近い方を優先 const sorted = sites.slice().sort((a, b) => { const pa = BUILD_PRIORITY[a.structureType] || 10; const pb = BUILD_PRIORITY[b.structureType] || 10; if (pa !== pb) return pa - pb; return creep.pos.getRangeTo(a) - creep.pos.getRangeTo(b); }); const target = sorted[0]; creep.memory[MEMORY_KEYS.TARGET_ID] = target.id; return target; } /** * 建設サイトがない場合に修復を行う(補助動作) * @param {Creep} creep */ function _repairAsBackup(creep) { const damaged = creep.room.find(FIND_STRUCTURES, { filter: (s) => s.hits < s.hitsMax * 0.8 && s.structureType !== STRUCTURE_WALL && s.structureType !== STRUCTURE_RAMPART, }); if (damaged.length > 0) { const target = pathfinder.closest(creep.pos, damaged); const result = creep.repair(target); if (result === ERR_NOT_IN_RANGE) { pathfinder.moveTo(creep, target, { range: 3 }); } creep.say('🔧 修復'); return; } // 修復対象もない場合はコントローラーをアップグレード const controller = creep.room.controller; if (controller) { if (creep.upgradeController(controller) === ERR_NOT_IN_RANGE) { pathfinder.moveTo(creep, controller, { range: 3 }); } creep.say('⬆️ 強化'); } } // ============================================================ // エネルギー取得ロジック // ============================================================ /** * エネルギーを取得する * @param {Creep} creep */ function _getEnergy(creep) { const room = creep.room; // 落下リソースを優先回収 const dropped = cache.getDroppedResources(room); const energyDrops = dropped.filter( (r) => r.resourceType === RESOURCE_ENERGY && r.amount >= 50 ); if (energyDrops.length > 0) { const res = pathfinder.closest(creep.pos, energyDrops); if (creep.pickup(res) === ERR_NOT_IN_RANGE) { pathfinder.moveTo(creep, res, { range: 1 }); } return; } // コンテナから取得 const containers = cache.getContainers(room); const available = containers.filter((c) => c.store[RESOURCE_ENERGY] >= 100); if (available.length > 0) { const container = pathfinder.closest(creep.pos, available); if (creep.withdraw(container, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) { pathfinder.moveTo(creep, container, { range: 1 }); } return; } // ストレージから取得 const storage = cache.getStorage(room); if (storage && storage.store[RESOURCE_ENERGY] >= 500) { if (creep.withdraw(storage, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) { pathfinder.moveTo(creep, storage, { range: 1 }); } return; } // ソースから直接採掘 const source = cache.assignSource(creep, room); if (source) { if (creep.harvest(source) === ERR_NOT_IN_RANGE) { pathfinder.moveTo(creep, source, { range: 1 }); } } } // ============================================================ // ボディ生成 // ============================================================ /** * 利用可能エネルギーに応じた最適なボディを返す * @param {number} energy * @returns {string[]} */ function getBody(energy) { if (energy >= 800) { return [WORK, WORK, WORK, CARRY, CARRY, CARRY, MOVE, MOVE, MOVE, MOVE]; } if (energy >= 500) { return [WORK, WORK, CARRY, CARRY, MOVE, MOVE]; } if (energy >= 350) { return [WORK, CARRY, CARRY, MOVE, MOVE]; } return [WORK, CARRY, MOVE]; } // ============================================================ // ユーティリティ // ============================================================ /** * ルーム内に建設サイトが存在するか確認する * @param {Room} room * @returns {boolean} */ function hasBuildSites(room) { return cache.getConstructionSites(room).length > 0; } /** * ルーム内のすべての建設サイトの合計建設量を返す * @param {Room} room * @returns {number} */ function getTotalBuildProgress(room) { const sites = cache.getConstructionSites(room); return sites.reduce((acc, s) => acc + (s.progressTotal - s.progress), 0); } module.exports = { run, getBody, hasBuildSites, getTotalBuildProgress, BUILD_PRIORITY }; |