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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | 1x 1x 1x 1x 5x 5x 5x 1x | /**
* src/managers/towerManager.js
* タワー自動管理モジュール
*
* ルーム内のすべてのタワーを制御し、以下の優先順位で動作する:
* 1. 敵クリープへの攻撃
* 2. 負傷した味方クリープの回復
* 3. 損傷した構造物の修復
* タワーのエネルギー管理も行い、エネルギー補充の優先度を制御する。
*/
'use strict';
const cache = require('../utils/cache');
const pathfinder = require('../utils/pathfinder');
const logger = require('../utils/logger');
const {
TOWER_ATTACK_PRIORITY_HP,
TOWER_REPAIR_THRESHOLD,
TOWER_REPAIR_STOP_THRESHOLD,
TOWER_HEAL_THRESHOLD,
TOWER_ENERGY_PRIORITY,
REPAIR_THRESHOLD,
WALL_HP_TARGET,
} = require('../constants');
// ============================================================
// メイン制御
// ============================================================
/**
* ルーム内の全タワーを制御する
* @param {Room} room
*/
function run(room) {
try {
const towers = cache.getMyStructures(room, STRUCTURE_TOWER);
if (towers.length === 0) return;
const enemies = cache.getEnemies(room);
const myCreeps = room.find(FIND_MY_CREEPS);
const injuredCreeps = myCreeps.filter((c) => c.hits < c.hitsMax * TOWER_HEAL_THRESHOLD);
for (const tower of towers) {
if (tower.store[RESOURCE_ENERGY] < 10) continue;
_runTower(tower, enemies, injuredCreeps, room);
}
} catch (e) {
logger.error('[TowerManager] タワーエラー', e);
}
}
// ============================================================
// タワー個別制御
// ============================================================
/**
* タワー1基の動作を決定・実行する
* @param {StructureTower} tower
* @param {Creep[]} enemies
* @param {Creep[]} injuredCreeps
* @param {Room} room
*/
function _runTower(tower, enemies, injuredCreeps, room) {
// 1. 敵への攻撃(最優先)
if (enemies.length > 0) {
const target = _selectAttackTarget(tower, enemies);
if (target) {
tower.attack(target);
_showAttackVisual(tower, target);
return;
}
}
// 2. 負傷クリープの回復
if (injuredCreeps.length > 0) {
const target = _selectHealTarget(tower, injuredCreeps);
if (target) {
tower.heal(target);
_showHealVisual(tower, target);
return;
}
}
// 3. 構造物の修復(エネルギーに余裕がある場合のみ)
const energyRatio = tower.store[RESOURCE_ENERGY] / tower.store.getCapacity(RESOURCE_ENERGY);
if (energyRatio > TOWER_ENERGY_PRIORITY) {
const repairTarget = _selectRepairTarget(tower, room);
if (repairTarget) {
tower.repair(repairTarget);
_showRepairVisual(tower, repairTarget);
}
}
}
// ============================================================
// 攻撃対象選択
// ============================================================
/**
* 攻撃対象を選択する
* 優先度: HPが低い敵 → コントローラーに近い敵 → タワーに近い敵
* @param {StructureTower} tower
* @param {Creep[]} enemies
* @returns {Creep|null}
*/
function _selectAttackTarget(tower, enemies) {
if (enemies.length === 0) return null;
// HPが閾値以下の敵を最優先
const criticalEnemies = enemies.filter((e) => e.hits <= TOWER_ATTACK_PRIORITY_HP);
if (criticalEnemies.length > 0) {
return pathfinder.closest(tower.pos, criticalEnemies);
}
// コントローラーに最も近い敵(占領脅威)を優先
const controller = tower.room.controller;
if (controller) {
const claimers = enemies.filter((e) => e.getActiveBodyparts(CLAIM) > 0);
if (claimers.length > 0) {
return pathfinder.closest(controller.pos, claimers);
}
}
// 攻撃系パーツを持つ敵を優先
const attackers = enemies.filter(
(e) =>
e.getActiveBodyparts(ATTACK) > 0 ||
e.getActiveBodyparts(RANGED_ATTACK) > 0
);
if (attackers.length > 0) {
// HPが最も低い攻撃者
return attackers.reduce((a, b) => (a.hits < b.hits ? a : b));
}
// 一般的な敵はHPが最も低いものを選択
return enemies.reduce((a, b) => (a.hits < b.hits ? a : b));
}
// ============================================================
// 回復対象選択
// ============================================================
/**
* 回復対象を選択する
* HPが最も低いクリープを優先
* @param {StructureTower} tower
* @param {Creep[]} injured
* @returns {Creep|null}
*/
function _selectHealTarget(tower, injured) {
if (injured.length === 0) return null;
return injured.reduce((a, b) => (a.hits / a.hitsMax < b.hits / b.hitsMax ? a : b));
}
// ============================================================
// 修復対象選択
// ============================================================
/**
* 修復対象を選択する
* 損傷率が高い構造物を優先(ウォールは除く)
* @param {StructureTower} tower
* @param {Room} room
* @returns {Structure|null}
*/
function _selectRepairTarget(tower, room) {
// ランパートの緊急修復
const rcl = room.controller ? room.controller.level : 1;
const wallTarget = WALL_HP_TARGET[rcl] || WALL_HP_TARGET[1];
const urgentRamparts = room.find(FIND_MY_STRUCTURES, {
filter: (s) =>
s.structureType === STRUCTURE_RAMPART &&
s.hits < Math.min(wallTarget * 0.1, 5000),
});
if (urgentRamparts.length > 0) {
return urgentRamparts.reduce((a, b) => (a.hits < b.hits ? a : b));
}
// 道路・コンテナの修復
const damaged = room.find(FIND_STRUCTURES, {
filter: (s) => {
if (s.structureType === STRUCTURE_WALL) return false;
if (s.structureType === STRUCTURE_RAMPART) return false;
const threshold = REPAIR_THRESHOLD[s.structureType] || REPAIR_THRESHOLD.OTHER;
return s.hits < s.hitsMax * threshold;
},
});
if (damaged.length === 0) return null;
// 最も損傷率が高い構造物
return damaged.reduce((a, b) => {
const ra = a.hits / a.hitsMax;
const rb = b.hits / b.hitsMax;
return ra < rb ? a : b;
});
}
// ============================================================
// ビジュアル表示
// ============================================================
/**
* 攻撃のビジュアルを表示する
* @param {StructureTower} tower
* @param {Creep} target
*/
function _showAttackVisual(tower, target) {
tower.room.visual.line(tower.pos, target.pos, {
color: '#ff4444',
width: 0.3,
opacity: 0.7,
});
tower.room.visual.circle(target.pos, {
radius: 0.5,
fill: 'transparent',
stroke: '#ff4444',
strokeWidth: 0.2,
opacity: 0.8,
});
}
/**
* 回復のビジュアルを表示する
* @param {StructureTower} tower
* @param {Creep} target
*/
function _showHealVisual(tower, target) {
tower.room.visual.line(tower.pos, target.pos, {
color: '#00ff88',
width: 0.2,
opacity: 0.5,
});
tower.room.visual.circle(target.pos, {
radius: 0.4,
fill: '#00ff88',
opacity: 0.2,
});
}
/**
* 修復のビジュアルを表示する
* @param {StructureTower} tower
* @param {Structure} target
*/
function _showRepairVisual(tower, target) {
tower.room.visual.line(tower.pos, target.pos, {
color: '#ffaa00',
width: 0.15,
opacity: 0.4,
lineStyle: 'dotted',
});
}
// ============================================================
// エネルギー管理
// ============================================================
/**
* タワーのエネルギー補充が必要かチェックし、
* 必要なら補充フラグをメモリに設定する
* @param {Room} room
* @returns {StructureTower[]} エネルギー補充が必要なタワー一覧
*/
function getTowersNeedingEnergy(room) {
const towers = cache.getMyStructures(room, STRUCTURE_TOWER);
return towers.filter(
(t) =>
t.store[RESOURCE_ENERGY] / t.store.getCapacity(RESOURCE_ENERGY) <
TOWER_ENERGY_PRIORITY
);
}
// ============================================================
// 統計・ダッシュボード
// ============================================================
/**
* タワーの稼働状況を返す
* @param {Room} room
* @returns {Object}
*/
function getStats(room) {
const towers = cache.getMyStructures(room, STRUCTURE_TOWER);
const stats = {
total: towers.length,
active: 0,
avgEnergy: 0,
lowEnergy: 0,
};
if (towers.length === 0) return stats;
let totalEnergy = 0;
for (const tower of towers) {
const ratio = tower.store[RESOURCE_ENERGY] / tower.store.getCapacity(RESOURCE_ENERGY);
totalEnergy += ratio;
if (ratio < TOWER_ENERGY_PRIORITY) stats.lowEnergy++;
if (tower.store[RESOURCE_ENERGY] > 0) stats.active++;
}
stats.avgEnergy = totalEnergy / towers.length;
return stats;
}
/**
* タワー統計をルームビジュアルに表示する
* @param {Room} room
*/
function showDashboard(room) {
const towers = cache.getMyStructures(room, STRUCTURE_TOWER);
for (const tower of towers) {
const ratio = tower.store[RESOURCE_ENERGY] / tower.store.getCapacity(RESOURCE_ENERGY);
const color = ratio > 0.7 ? '#00ff88' : ratio > 0.4 ? '#ffaa00' : '#ff4444';
tower.room.visual.text(
`🏰 ${Math.floor(ratio * 100)}%`,
tower.pos.x,
tower.pos.y - 1,
{ color, font: 0.4, align: 'center' }
);
}
}
module.exports = {
run,
getTowersNeedingEnergy,
getStats,
showDashboard,
};
|