@@ -5,28 +5,29 @@ export interface HarvesterInterface {
getHarvesters(): HarvesterInterface[];
tryHarvest(target: Source): number;
}
import {Role} from "./role"
/**
* Wraps a creep with harvesting functionality
*/
export class Harvester implements HarvesterInterface {
public creep: Creep = null;
get memory(): HarvesterCreepMemory {
return this.creep._memory.harvester;
return this.creep._memory().harvester;
}
get source(): Source {
return Game.getObjectById<Source>(this.creep._memory.harvester.source)
return Game.getObjectById<Source>(this.creep._memory().harvester.source)
}
set source(newSource) {
if (newSource == null) return;
this.creep._memory.harvester.source = newSource.id;
this.creep._memory().harvester.source = newSource.id;
}
public constructor(creep: Creep) {
this.creep = creep;
if (this.creep._memory.role != Role.Harvester) {
this.creep._memory.role = Role.Harvester;
if (this.creep._memory().role != Role.Harvester) {
this.creep._memory().role = Role.Harvester;
}
if (this.creep._memory.harvester == null) {
this.creep._memory.harvester = { source: null };
if (this.creep._memory().harvester == null) {
this.creep._memory().harvester = { source: null };
}
}
public setCreep(creep: Creep) {
@@ -1,14 +1,15 @@
/// <reference path="prospector.d.ts" />
import {Role} from "./role"
export class Prospector {
public creep: Creep;
public get memory(): ProspectorCreepMemory {
return this.creep._memory.prospector;
return this.creep._memory().prospector;
}
public constructor(creep: Creep) {
this.creep = creep;
this.creep._memory.role = Role.Prospector;
this.creep._memory().role = Role.Prospector;
if (this.memory == null) {
this.creep._memory.prospector = {
this.creep._memory().prospector = {
assistants: [],
pos: null,
site: null,
@@ -20,7 +21,7 @@ export class Prospector {
let path = this.getPathToSource(Game.getObjectById<Source>(this.memory.source));
if (path.path.length > 0) {
this.memory.pos = path.path[path.path.length - 1];
this.creep._memory.path = path.path;
this.creep._memory().path = path;
} else {
console.log(`${this.creep.name} cannot find anywhere to setup a container near ${this.memory.source}`);
}
@@ -29,12 +30,17 @@ export class Prospector {
public setupSource() {
if (this.creep.pos.isEqualTo(this.memory.pos)) {
if (this.memory.site == null) {
return this.memory.pos.createConstructionSite(STRUCTURE_CONTAINER);
let sitePos = new RoomPosition(this.memory.pos.x, this.memory.pos.y, this.memory.pos.roomName);
return sitePos.createConstructionSite(STRUCTURE_CONTAINER);
} else {
return this.creep.harvest(Game.getObjectById<Source>(this.memory.source))
}
} else {
if (this.creep._memory.path == null || this.creep._memory.path.length < 1) {
console.log('FART')
if (this.creep._memory().path == null || this.creep._memory().path.path.length < 1) {

} else {
console.log(this.creep.moveByPath(this.creep._memory().path))
}
}
}
@@ -44,11 +50,11 @@ export class Prospector {
public getPathToSource(source: Source) {
let takenSourcePos = source.room.find<Creep>(FIND_MY_CREEPS, {
filter: (creep: Creep) => {
return creep._memory.role == Role.Prospector &&
creep._memory.prospector.source == source.id &&
creep._memory.prospector.pos != null
return creep._memory().role == Role.Prospector &&
creep._memory().prospector.source == source.id &&
creep._memory().prospector.pos != null
}
}).map(creep => creep._memory.prospector.pos);
}).map(creep => creep._memory().prospector.pos);
//Add nearby containers to list of unpathable
//because we would not be able to build there
takenSourcePos.concat(source.room.find<Structure>(FIND_CONSTRUCTION_SITES, { filter: { structureType: STRUCTURE_CONTAINER } })
@@ -0,0 +1,9 @@
export enum Role {
Prospector,
Harvester
}
declare global {
interface CreepMemory {
role: Role
}
}
@@ -1,5 +1,5 @@
interface Room {
_memory: RoomMemory;
_memory(): RoomMemory;
/**
* Gets a cost matrix that includes buildings and construction sites
*/
@@ -2,6 +2,7 @@ import {GameState} from "../game-state"
import {Strategy} from "./strategy"
import {Prospector} from "./../role/prospector"
import {EnergyMonitor} from "./../monitor/energy-monitor"
import {Role} from "./../role/role"
export type SpawnOrExtension = StructureSpawn | StructureExtension
export class StartStrategy implements Strategy {
public state = GameState.Start;
@@ -20,6 +21,7 @@ export class StartStrategy implements Strategy {
});
}
public execute() {
console.log(`we're inside startstrategy`);
this.manageProspectors();
}
public recommend() {
@@ -43,10 +45,12 @@ export class StartStrategy implements Strategy {
}
manageProspectors() {
let availableSource = this.availableSources()[0];
if (availableSource == null) {
console.log(`available source: ${availableSource}`)
if (availableSource != null) {
let spawn = _.find(Game.spawns, spawn => spawn.room.name == this.room.name);
console.log(`selected spawn: ${spawn}`)
if (spawn) {
spawn.createCreep([WORK, CARRY, MOVE], undefined, {prospector: {source: availableSource.id, pos: null, assistants: [], site: null}});
spawn.createCreep([WORK, CARRY, MOVE], undefined, {role: Role.Prospector, prospector: {source: availableSource.id, pos: null, assistants: [], site: null}});
}
}
for (let prospector of this.prospectors) {
@@ -3,13 +3,8 @@
"module": "commonjs",
"target": "es5",
"sourceMap": false,
"declaration": true,
"outDir": "./dist",
"moduleResolution": "node"
"declaration": false,
"outDir": ""
},
"exclude": ["node_modules", "dist"],
"filesGlob": [
"./src/**/*.ts"
]

"exclude": ["node_modules", "dist"]
}
@@ -0,0 +1,17 @@
module.exports = {
entry: './src/main.ts',
output: {
filename: 'screeps.js',
library: 'screeps',
libraryTarget: 'commonjs2'
},
resolve: {
extensions: ['', '.js', '.ts', '.d.ts', '.tsx']
},
devtool: 'source-map', // if we want a source map
module: {
loaders: [
{ test: /\.tsx?$/, loader: 'ts-loader' }
]
}
}