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 333 334 335 336 337 338 339 340 341 342 343 344 345 | 1x 1x 1x 1x 1x 7x 6x 6x 5x 5x 5x 5x 5x 5x 5x 5x 7x 7x 5x 5x 5x 6x 6x 6x 6x 3x 6x 6x 2x 6x 2x 20x 6x 5x 6x 6x 6x 4x 4x 4x 4x 4x 4x 6x 6x 6x 6x 6x 5x 5x 5x 5x 9x 9x 5x 5x 5x 9x 8x 5x 5x 2x 1x 1x 1x 1x 1x 1x 5x 1x 1x 1x 1x 1x 3x 3x 3x 3x 1x | /**
* src/managers/spawnManager.js
* クリープスポーン管理モジュール
*
* ルームのRCL・エネルギー状況・現在のクリープ数に応じて
* 最適なクリープのスポーンを管理する。
* スポーンキューを使い、優先度の高いロールから順にスポーンする。
*/
'use strict';
const cache = require('../utils/cache');
const logger = require('../utils/logger');
const { ROLES, BODY_PRESETS, SPAWN_PRIORITY, TARGET_CREEPS_BY_RCL } = require('../constants');
// ============================================================
// スポーンキュー
// ============================================================
/**
* スポーンキューを取得する(global.cache に格納)
* @returns {Array}
*/
function _getQueue() {
if (!global.cache) global.cache = {};
if (!global.cache.spawnQueue) global.cache.spawnQueue = [];
return global.cache.spawnQueue;
}
/**
* スポーンキューをクリアする
*/
function clearQueue() {
Eif (global.cache) {
global.cache.spawnQueue = [];
}
}
// ============================================================
// メイン制御
// ============================================================
/**
* スポーン管理のメインロジックを実行する
* @param {StructureSpawn} spawn
*/
function run(spawn) {
if (!spawn || spawn.spawning) return;
const room = spawn.room;
if (!room.controller || !room.controller.my) return;
try {
// スポーンキューを構築
const queue = _buildSpawnQueue(room);
if (queue.length === 0) return;
// キューの先頭からスポーンを試みる
for (const request of queue) {
const result = _trySpawn(spawn, request);
if (result === OK) {
logger.info(`[SpawnManager] ${request.role} をスポーン: ${request.name}`);
break;
} else if (result === ERR_NOT_ENOUGH_ENERGY) {
// エネルギー不足は一時的なのでスキップしない
break;
}
}
} catch (e) {
logger.error('[SpawnManager] スポーンエラー', e);
}
}
// ============================================================
// スポーンキュー構築
// ============================================================
/**
* 現在のルーム状態からスポーンキューを構築する
* @param {Room} room
* @returns {Array<{ role: string, body: string[], name: string, priority: number }>}
*/
function _buildSpawnQueue(room) {
const rcl = room.controller.level;
const targets = _getTargetCounts(room, rcl);
const current = _getCurrentCounts(room);
const queue = [];
for (const role in targets) {
const needed = targets[role] - (current[role] || 0);
if (needed <= 0) continue;
// エネルギーに応じた最適ボディを取得
const body = _selectBody(role, room.energyAvailable, room.energyCapacityAvailable);
Iif (!body || body.length === 0) continue;
const cost = _calcBodyCost(body);
if (cost > room.energyCapacityAvailable) continue;
queue.push({
role,
body,
name: `${role}_${Game.time}_${Math.floor(Math.random() * 100)}`,
priority: SPAWN_PRIORITY[role] || 99,
cost,
});
}
// 優先度でソート
return queue.sort((a, b) => a.priority - b.priority);
}
/**
* RCLとルーム状況に応じたターゲットクリープ数を返す
* @param {Room} room
* @param {number} rcl
* @returns {Object.<string, number>}
*/
function _getTargetCounts(room, rcl) {
const baseTargets = TARGET_CREEPS_BY_RCL[rcl] || TARGET_CREEPS_BY_RCL[1];
const result = Object.assign({}, baseTargets);
// 建設サイトがある場合はビルダーを追加
const sites = cache.getConstructionSites(room);
if (sites.length > 0) {
result[ROLES.BUILDER] = Math.max(result[ROLES.BUILDER] || 0, Math.min(3, Math.ceil(sites.length / 5)));
}
// 侵入者がいる場合はディフェンダーを追加
const enemies = cache.getEnemies(room);
const attackers = enemies.filter(
(e) =>
e.getActiveBodyparts(ATTACK) > 0 ||
e.getActiveBodyparts(RANGED_ATTACK) > 0
);
if (attackers.length > 0) {
result[ROLES.DEFENDER] = Math.max(result[ROLES.DEFENDER] || 0, attackers.length);
}
// 緊急モード: クリープが0の場合は最低限のハーベスターを確保
const totalCreeps = Object.values(result).reduce((s, v) => s + v, 0);
if (Object.keys(Game.creeps).length === 0) {
result[ROLES.HARVESTER] = Math.max(result[ROLES.HARVESTER] || 0, 1);
}
return result;
}
/**
* 現在のルーム内クリープ数をロール別に集計する
* @param {Room} room
* @returns {Object.<string, number>}
*/
function _getCurrentCounts(room) {
const counts = {};
for (const name in Game.creeps) {
const creep = Game.creeps[name];
Iif (creep.room.name !== room.name) continue;
Iif (creep.spawning) continue; // スポーン中のクリープはスポーンAPIから別途カウント
const role = creep.memory.role;
Eif (role) {
counts[role] = (counts[role] || 0) + 1;
}
}
// スポーン中のクリープも含める
for (const spawnName in Game.spawns) {
const spawn = Game.spawns[spawnName];
Iif (spawn.room.name !== room.name) continue;
Eif (!spawn.spawning) continue;
const spawningCreep = Game.creeps[spawn.spawning.name];
if (spawningCreep && spawningCreep.memory.role) {
const role = spawningCreep.memory.role;
counts[role] = (counts[role] || 0) + 1;
}
}
return counts;
}
// ============================================================
// ボディ選択
// ============================================================
/**
* ロールと利用可能エネルギーに応じた最適ボディを選択する
* @param {string} role
* @param {number} available - 現在利用可能なエネルギー
* @param {number} capacity - スポーン最大エネルギー容量
* @returns {string[]|null}
*/
function _selectBody(role, available, capacity) {
const presets = BODY_PRESETS[role];
Iif (!presets) return null;
// 最大容量以下で最もコストが高いボディを選択(最強のボディ)
let bestBody = null;
for (const preset of presets) {
Eif (preset.cost <= capacity) {
bestBody = preset.body;
}
}
Iif (!bestBody) {
// 容量内に収まるプリセットがなければ最小構成を選択
bestBody = presets[0].body;
}
// 現在のエネルギーでスポーンできる最大のボディを選択
let spawnableBody = null;
for (const preset of presets) {
if (preset.cost <= available) {
spawnableBody = preset.body;
}
}
// 緊急時は小さいボディでもスポーン
Iif (!spawnableBody) {
const emergencyBodies = {
[ROLES.HARVESTER]: [WORK, CARRY, MOVE],
[ROLES.UPGRADER]: [WORK, CARRY, MOVE],
[ROLES.BUILDER]: [WORK, CARRY, MOVE],
[ROLES.REPAIRER]: [WORK, CARRY, MOVE],
[ROLES.MINER]: [WORK, MOVE],
[ROLES.TRANSPORTER]: [CARRY, CARRY, MOVE],
[ROLES.DEFENDER]: [ATTACK, MOVE],
[ROLES.SCOUT]: [MOVE],
};
const emergency = emergencyBodies[role];
if (emergency && _calcBodyCost(emergency) <= available) {
return emergency;
}
return null;
}
return spawnableBody;
}
// ============================================================
// スポーン実行
// ============================================================
/**
* スポーンリクエストを実行する
* @param {StructureSpawn} spawn
* @param {{ role: string, body: string[], name: string }} request
* @returns {number} スポーン結果コード
*/
function _trySpawn(spawn, request) {
return spawn.spawnCreep(request.body, request.name, {
memory: {
role: request.role,
homeRoom: spawn.room.name,
spawnedAt: Game.time,
},
});
}
// ============================================================
// スポーン表示
// ============================================================
/**
* スポーン中のビジュアルを表示する
* @param {StructureSpawn} spawn
*/
function showSpawnVisual(spawn) {
if (!spawn.spawning) return;
const spawningCreep = Game.creeps[spawn.spawning.name];
Iif (!spawningCreep) return;
const role = spawningCreep.memory.role;
const progress =
(spawn.spawning.needTime - spawn.spawning.remainingTime) /
spawn.spawning.needTime;
// 役割と進捗を表示
spawn.room.visual.text(
`🛠️ ${role} (${Math.floor(progress * 100)}%)`,
spawn.pos.x + 1,
spawn.pos.y,
{ align: 'left', opacity: 0.8, font: 0.5 }
);
// プログレスバー
spawn.room.visual.rect(
spawn.pos.x - 0.5,
spawn.pos.y + 0.6,
progress,
0.2,
{ fill: '#00bfff', opacity: 0.8 }
);
}
// ============================================================
// ユーティリティ
// ============================================================
/**
* ボディのエネルギーコストを計算する
* @param {string[]} body
* @returns {number}
*/
function _calcBodyCost(body) {
const COSTS = {
[MOVE]: 50,
[WORK]: 100,
[CARRY]: 50,
[ATTACK]: 80,
[RANGED_ATTACK]: 150,
[HEAL]: 250,
[CLAIM]: 600,
[TOUGH]: 10,
};
return body.reduce((total, part) => total + (COSTS[part] || 0), 0);
}
/**
* スポーン統計をコンソールに表示する
* @param {Room} room
*/
function showStats(room) {
const rcl = room.controller ? room.controller.level : 0;
const targets = _getTargetCounts(room, rcl);
const current = _getCurrentCounts(room);
logger.info(`[SpawnManager] ルーム ${room.name} のクリープ状況 (RCL ${rcl}):`);
for (const role in targets) {
const t = targets[role];
const c = current[role] || 0;
const status = c >= t ? '✓' : '⚠️';
logger.info(` ${status} ${role}: ${c}/${t}`);
}
}
module.exports = {
run,
showSpawnVisual,
showStats,
clearQueue,
};
|