Browse Source

Record brawler cost for remote harvesting as usual.

David Franke 5 months ago
parent
commit
6e28772b78
3 changed files with 44 additions and 5 deletions
  1. 3
    0
      src/spawn-manager.js
  2. 14
    5
      src/spawn-role.brawler.js
  3. 27
    0
      src/spawn-role.js

+ 3
- 0
src/spawn-manager.js View File

@@ -152,6 +152,9 @@ module.exports = class SpawnManager {
152 152
 			room.boostManager.markForBoosting(creepName, boosts);
153 153
 		}
154 154
 
155
+		// Notify the role that spawning was successful.
156
+		role.onSpawn(room, option, body, creepName);
157
+
155 158
 		return true;
156 159
 	}
157 160
 

+ 14
- 5
src/spawn-role.brawler.js View File

@@ -108,13 +108,22 @@ module.exports = class ExploitSpawnRole extends SpawnRole {
108 108
 	}
109 109
 
110 110
 	/**
111
+	 * Act when a creep belonging to this spawn role is successfully spawning.
111 112
 	 *
113
+	 * @param {Room} room
114
+	 *   The room the creep is spawned in.
115
+	 * @param {Object} option
116
+	 *   The spawn option which caused the spawning.
117
+	 * @param {string[]} body
118
+	 *   The body generated for this creep.
119
+	 * @param {string} name
120
+	 *   The name of the new creep.
112 121
 	 */
113
-	onSpawn(room, option, body) {
114
-		const position = utilities.encodePosition(harvestPosition);
115
-		console.log('Spawning new brawler to defend', position, ':', result);
122
+	onSpawn(room, option, body, name) {
123
+		const position = option.targetPos;
124
+		if (!position) return;
116 125
 
117
-		const cost = this.calculateCreepBodyCost(Memory.creeps[result].body);
118
-		stats.addRemoteHarvestDefenseCost(this.room.name, position, cost);
126
+		hivemind.log('creeps', room.name).info('Spawning new brawler', name, 'to defend', position);
127
+		stats.addRemoteHarvestDefenseCost(room.name, position, this.calculateBodyCost(body));
119 128
 	}
120 129
 };

+ 27
- 0
src/spawn-role.js View File

@@ -60,6 +60,20 @@ module.exports = class SpawnRole {
60 60
 		return {};
61 61
 	}
62 62
 
63
+	/**
64
+	 * Act when a creep belonging to this spawn role is successfully spawning.
65
+	 *
66
+	 * @param {Room} room
67
+	 *   The room the creep is spawned in.
68
+	 * @param {Object} option
69
+	 *   The spawn option which caused the spawning.
70
+	 * @param {string[]} body
71
+	 *   The body generated for this creep.
72
+	 * @param {string} name
73
+	 *   The name of the new creep.
74
+	 */
75
+	onSpawn() {}
76
+
63 77
 	/**
64 78
 	 * Dynamically generates a creep body using body part weights and limits.
65 79
 	 *
@@ -190,4 +204,17 @@ module.exports = class SpawnRole {
190 204
 			[partType]: bestBoost,
191 205
 		};
192 206
 	}
207
+
208
+	/**
209
+	 * Calculates the cost of a creep body array.
210
+	 *
211
+	 * @param {String[]} body
212
+	 *   A creep body.
213
+	 *
214
+	 * @return {number}
215
+	 *   The energy cost of the provided body.
216
+	 */
217
+	calculateBodyCost(body) {
218
+		return _.reduce(body, (sum, part) => sum + BODYPART_COST[part], 0);
219
+	}
193 220
 };