Not random source choice

This commit is contained in:
Anton Vakhrushev 2020-07-22 14:00:34 +03:00
parent c03b0ccea6
commit cc7ac0e06e
6 changed files with 37 additions and 28 deletions

View File

@ -2,7 +2,6 @@ import { selectSource } from './common';
interface BuilderMemory extends CreepMemory { interface BuilderMemory extends CreepMemory {
building: boolean | undefined; building: boolean | undefined;
sourceId: Id<Source> | undefined;
} }
export function runAsBuilder(creep: Creep) { export function runAsBuilder(creep: Creep) {
@ -16,8 +15,8 @@ export function runAsBuilder(creep: Creep) {
memory.building = true; memory.building = true;
} }
if (memory.sourceId === undefined) { if (!memory.sourceId) {
memory.sourceId = selectSource(creep); memory.sourceId = selectSource(creep.room);
} }
if (memory.building) { if (memory.building) {

View File

@ -1,21 +1,29 @@
import { randomValueFromArray } from '../utils/Random'; export function selectSource(room: Room): Id<Source> {
console.log('Select source for room', room.name);
export function selectSource(creep: Creep): Id<Source> { const creepsInRoom = room.find(FIND_MY_CREEPS);
const sources = creep.room.find(FIND_SOURCES); const sourcesInRoom = room.find(FIND_SOURCES);
const choices: Array<String> = []; const rates: Weights = {};
for (let source of sources) { for (let source of sourcesInRoom) {
const creepCount = creepsInRoom.filter((c) => c.memory.sourceId === source.id).length;
const weight = getSourceWeight(source); const weight = getSourceWeight(source);
for (let i = 0; i < weight; ++i) { rates[source.id] = creepCount / weight;
choices.push(source.id);
} }
} console.log('Sources', JSON.stringify(rates));
console.log('Sources', choices); return getKeyWithMinWeight(rates) as Id<Source>;
return randomValueFromArray(choices) as Id<Source>;
} }
function getSourceWeight(source: Source) { function getSourceWeight(source: Source): number {
const pos = source.pos; const pos = source.pos;
const terrains = source.room.lookForAtArea(LOOK_TERRAIN, pos.y - 1, pos.x - 1, pos.y + 1, pos.x + 1, true); const terrains = source.room.lookForAtArea(LOOK_TERRAIN, pos.y - 1, pos.x - 1, pos.y + 1, pos.x + 1, true);
const walked = terrains.filter((t) => t.terrain !== 'wall'); const walked = terrains.filter((t) => t.terrain !== 'wall');
return Math.max(walked.length - 1, 1); return Math.max(walked.length - 1, 1);
} }
interface Weights {
[key: string]: number;
}
function getKeyWithMinWeight(weights: Weights): string {
let keys = Object.keys(weights);
return keys.reduce((key, v) => (weights[v] < weights[key] ? v : key));
}

View File

@ -1,10 +1,6 @@
import { selectSource } from './common'; import { selectSource } from './common';
const TARGET_TYPES = [STRUCTURE_EXTENSION, STRUCTURE_SPAWN, STRUCTURE_TOWER]; const TARGET_TYPES = [STRUCTURE_EXTENSION, STRUCTURE_TOWER, STRUCTURE_SPAWN];
interface HarvesterMemory extends CreepMemory {
sourceId: Id<Source> | undefined;
}
enum Action { enum Action {
Harvest = 'harvest', Harvest = 'harvest',
@ -12,14 +8,14 @@ enum Action {
} }
export function runAsHarvester(creep: Creep) { export function runAsHarvester(creep: Creep) {
const memory = creep.memory as HarvesterMemory; const memory = creep.memory as CreepMemory;
if (memory.action === undefined) { if (memory.action === undefined) {
memory.action = Action.Harvest; memory.action = Action.Harvest;
} }
if (memory.sourceId === undefined) { if (!memory.sourceId) {
memory.sourceId = selectSource(creep); memory.sourceId = selectSource(creep.room);
} }
switch (memory.action) { switch (memory.action) {

View File

@ -2,7 +2,6 @@ import { selectSource } from './common';
interface UpgraderMemory extends CreepMemory { interface UpgraderMemory extends CreepMemory {
upgrading: boolean | undefined; upgrading: boolean | undefined;
sourceId: Id<Source> | undefined;
} }
export function runAsUpgrader(creep: Creep) { export function runAsUpgrader(creep: Creep) {
@ -16,8 +15,8 @@ export function runAsUpgrader(creep: Creep) {
memory.upgrading = true; memory.upgrading = true;
} }
if (memory.sourceId === undefined) { if (!memory.sourceId) {
memory.sourceId = selectSource(creep); memory.sourceId = selectSource(creep.room);
} }
if (memory.upgrading) { if (memory.upgrading) {

View File

@ -20,8 +20,11 @@ function makeCreep(role: CreepRole, count: number) {
if (creeps.length < count) { if (creeps.length < count) {
const firstSpawn = _.first(Object.values(Game.spawns)); const firstSpawn = _.first(Object.values(Game.spawns));
const name = makeName(); const name = makeName();
const memory = { role: role } as CreepMemory; const memory = { role: role, sourceId: '' } as CreepMemory;
const err = firstSpawn.spawnCreep([WORK, WORK, CARRY, CARRY, MOVE, MOVE], name, { memory }); const err = firstSpawn.spawnCreep([WORK, WORK, CARRY, CARRY, MOVE, MOVE], name, {
memory,
directions: [BOTTOM, BOTTOM_RIGHT, RIGHT]
});
console.log(`Make creep "${role}"`, 'err', err); console.log(`Make creep "${role}"`, 'err', err);
} }
} }

4
src/types.d.ts vendored
View File

@ -11,6 +11,10 @@ interface CreepMemory {
* Действие, которое крип будет выполнять. * Действие, которое крип будет выполнять.
*/ */
action: string | undefined; action: string | undefined;
/**
* Идентификатор источника энергии для крипа.
*/
sourceId: Id<Source> | '';
room: string; room: string;
working: boolean; working: boolean;
} }