代码ts化, 修复编译上传脚本
This commit is contained in:
parent
916150eea2
commit
57c28c29ef
@ -1,4 +1,4 @@
|
|||||||
{
|
let config = {
|
||||||
"main": {
|
"main": {
|
||||||
"token": "",
|
"token": "",
|
||||||
"protocol": "http",
|
"protocol": "http",
|
||||||
@ -10,4 +10,7 @@
|
|||||||
"local": {
|
"local": {
|
||||||
"copyPath": "C:\\Users\\jie\\AppData\\Local\\Screeps\\scripts\\127_0_0_1___21025\\default"
|
"copyPath": "C:\\Users\\jie\\AppData\\Local\\Screeps\\scripts\\127_0_0_1___21025\\default"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export { config }
|
179
dist/main.js
vendored
179
dist/main.js
vendored
@ -3333,8 +3333,183 @@ const errorMapper = function (next) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var loop = errorMapper(function () {
|
function FindCapacity(creep) {
|
||||||
console.log("Game Start");
|
return creep.room.find(FIND_STRUCTURES, {
|
||||||
|
filter: (structure) => {
|
||||||
|
let type = structure.structureType;
|
||||||
|
return ((type == STRUCTURE_EXTENSION ||
|
||||||
|
type == STRUCTURE_SPAWN) &&
|
||||||
|
structure.store.getFreeCapacity() != null && structure.store.getFreeCapacity() > 0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function Harvest(creep) {
|
||||||
|
if (creep.store.getFreeCapacity() > 0) {
|
||||||
|
let sources = creep.room.find(FIND_SOURCES);
|
||||||
|
if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
|
||||||
|
creep.moveTo(sources[0], { visualizePathStyle: { stroke: "#ffaa00" } });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
let targets = FindCapacity(creep);
|
||||||
|
if (targets.length > 0) {
|
||||||
|
if (creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
|
||||||
|
creep.moveTo(targets[0], { visualizePathStyle: { stroke: "#ffffff" } });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function Build(creep) {
|
||||||
|
if (creep.memory.working && creep.store[RESOURCE_ENERGY] == 0) {
|
||||||
|
creep.memory.working = false;
|
||||||
|
creep.say("harvest");
|
||||||
|
}
|
||||||
|
if (!creep.memory.working && creep.store.getFreeCapacity(RESOURCE_ENERGY) == 0) {
|
||||||
|
creep.memory.working = true;
|
||||||
|
creep.say("builder");
|
||||||
|
}
|
||||||
|
if (creep.memory.working) {
|
||||||
|
let target = creep.room.find(FIND_CONSTRUCTION_SITES);
|
||||||
|
if (target.length > 0) {
|
||||||
|
if (creep.build(target[0]) == ERR_NOT_IN_RANGE) {
|
||||||
|
creep.moveTo(target[0], { visualizePathStyle: { stroke: "#ffaa00" } });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
let targets = FindCapacity(creep);
|
||||||
|
if (targets.length > 0) {
|
||||||
|
target.sort();
|
||||||
|
if (creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
|
||||||
|
creep.moveTo(targets[0], { visualizePathStyle: { stroke: "#ffaa00" } });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
creep.memory.working = false;
|
||||||
|
let sources = creep.room.find(FIND_SOURCES);
|
||||||
|
if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
|
||||||
|
creep.moveTo(sources[0], { visualizePathStyle: { stroke: "#ffaa00" } });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**@param {Creep} creep */
|
||||||
|
function Upgrade(creep) {
|
||||||
|
if (creep.memory.working && creep.store[RESOURCE_ENERGY] == 0) {
|
||||||
|
creep.memory.working = false;
|
||||||
|
creep.say('🔄 harvest');
|
||||||
|
}
|
||||||
|
if (!creep.memory.working && creep.store.getFreeCapacity() == 0) {
|
||||||
|
creep.memory.working = true;
|
||||||
|
creep.say('⚡ upgrade');
|
||||||
|
}
|
||||||
|
if (creep.memory.working) {
|
||||||
|
if (creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
|
||||||
|
creep.moveTo(creep.room.controller, { visualizePathStyle: { stroke: '#ffffff' } });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var sources = creep.room.find(FIND_SOURCES);
|
||||||
|
if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
|
||||||
|
creep.moveTo(sources[0], { visualizePathStyle: { stroke: '#ffaa00' } });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let HARVESTER_COUNT = 2;
|
||||||
|
let BUILDER_COUNT = 2;
|
||||||
|
let UPGRADER_COUNT = 6;
|
||||||
|
|
||||||
|
var createDefaultHarvester = (spawn) => {
|
||||||
|
let name = "Harvester" + Game.time;
|
||||||
|
return spawn.spawnCreep([WORK, CARRY, MOVE], name, {
|
||||||
|
memory: {
|
||||||
|
role: "Harvester",
|
||||||
|
working: false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
var createDefaultBuilder = (spawn) => {
|
||||||
|
let name = "Builder" + Game.time;
|
||||||
|
return spawn.spawnCreep([WORK, CARRY, MOVE], name, {
|
||||||
|
memory: {
|
||||||
|
role: "Builder",
|
||||||
|
working: false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
var createDefaultUpgrader = (spawn) => {
|
||||||
|
let name = "Upgrader" + Game.time;
|
||||||
|
return spawn.spawnCreep([WORK, CARRY, MOVE], name, {
|
||||||
|
memory: {
|
||||||
|
role: "Upgrader",
|
||||||
|
working: false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
function SpawnCreep(spawn) {
|
||||||
|
if (spawn.spawning)
|
||||||
|
return;
|
||||||
|
let harvesters = _.filter(Game.creeps,
|
||||||
|
/**@param {Creep} creep*/
|
||||||
|
(creep) => {
|
||||||
|
return creep.memory.role == "Harvester";
|
||||||
|
});
|
||||||
|
let builders = _.filter(Game.creeps,
|
||||||
|
/**@param {Creep} creep*/
|
||||||
|
(creep) => {
|
||||||
|
return creep.memory.role == "Builder";
|
||||||
|
});
|
||||||
|
let upgraders = _.filter(Game.creeps,
|
||||||
|
/**@param {Creep} creep*/
|
||||||
|
(creep) => {
|
||||||
|
return creep.memory.role == "Upgrader";
|
||||||
|
});
|
||||||
|
if (harvesters.length < HARVESTER_COUNT) {
|
||||||
|
if (createDefaultHarvester(spawn) == 0) {
|
||||||
|
console.log("Spawn Harvester");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (upgraders.length < UPGRADER_COUNT) {
|
||||||
|
if (createDefaultUpgrader(spawn) == 0) {
|
||||||
|
console.log("Spawn Upgrader");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (builders.length < BUILDER_COUNT) {
|
||||||
|
if (createDefaultBuilder(spawn) == 0) {
|
||||||
|
console.log("Spawn Builder");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (spawn.spawning) {
|
||||||
|
var spawningCreep = Game.creeps[spawn.spawning.name];
|
||||||
|
spawn.room.visual.text('🛠️' + spawningCreep.memory.role, spawn.pos.x + 1, spawn.pos.y, { align: 'left', opacity: 0.8 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const loop = errorMapper(() => {
|
||||||
|
let mainSpawn = Game.spawns["Spawn1"];
|
||||||
|
SpawnCreep(mainSpawn);
|
||||||
|
for (var name in Memory.creeps) {
|
||||||
|
if (!Game.creeps[name]) {
|
||||||
|
delete Memory.creeps[name];
|
||||||
|
console.log("clearing non-existing creep memory:", name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (var name in Game.creeps) {
|
||||||
|
let creep = Game.creeps[name];
|
||||||
|
if (creep.memory.role == "Harvester") {
|
||||||
|
Harvest(creep);
|
||||||
|
}
|
||||||
|
else if (creep.memory.role == "Upgrader") {
|
||||||
|
Upgrade(creep);
|
||||||
|
}
|
||||||
|
else if (creep.memory.role == "Builder") {
|
||||||
|
Build(creep);
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
exports.loop = loop;
|
exports.loop = loop;
|
||||||
|
1
dist/main.js.map
vendored
Normal file
1
dist/main.js.map
vendored
Normal file
File diff suppressed because one or more lines are too long
1
dist/main.js.map.js
vendored
1
dist/main.js.map.js
vendored
File diff suppressed because one or more lines are too long
@ -3,6 +3,7 @@
|
|||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1",
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
"build": "rollup -cw",
|
"build": "rollup -cw",
|
||||||
|
@ -4,27 +4,28 @@ import copy from 'rollup-plugin-copy'
|
|||||||
import resolve from '@rollup/plugin-node-resolve'
|
import resolve from '@rollup/plugin-node-resolve'
|
||||||
import commonjs from '@rollup/plugin-commonjs'
|
import commonjs from '@rollup/plugin-commonjs'
|
||||||
import typescript from 'rollup-plugin-typescript2' // <== 新增这一行
|
import typescript from 'rollup-plugin-typescript2' // <== 新增这一行
|
||||||
|
import { config } from './.secret.js'
|
||||||
|
|
||||||
|
|
||||||
let config
|
console.log(config[process.env.DEST])
|
||||||
// 根据指定的目标获取对应的配置项
|
// 根据指定的目标获取对应的配置项
|
||||||
if (!process.env.DEST) console.log("未指定目标, 代码将被编译但不会上传")
|
if (!process.env.DEST) console.log("未指定目标, 代码将被编译但不会上传")
|
||||||
else if (!(config = require("./.secret.json")[process.env.DEST])) {
|
else if (config.main == null && config.local == null) {
|
||||||
throw new Error("无效目标,请检查 secret.json 中是否包含对应配置")
|
throw new Error("无效目标,请检查 secret.json 中是否包含对应配置")
|
||||||
}
|
}
|
||||||
|
let runConfig = config[process.env.DEST]
|
||||||
// 根据指定的配置决定是上传还是复制到文件夹
|
// 根据指定的配置决定是上传还是复制到文件夹
|
||||||
const pluginDeploy = config && config.copyPath ?
|
const pluginDeploy = runConfig && runConfig.copyPath ?
|
||||||
// 复制到指定路径
|
// 复制到指定路径
|
||||||
copy({
|
copy({
|
||||||
targets: [
|
targets: [
|
||||||
{
|
{
|
||||||
src: 'dist/main.js',
|
src: 'dist/main.js',
|
||||||
dest: config.copyPath
|
dest: runConfig.copyPath
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
src: 'dist/main.js.map',
|
src: 'dist/main.js.map',
|
||||||
dest: config.copyPath,
|
dest: runConfig.copyPath,
|
||||||
rename: name => name + '.map.js',
|
rename: name => name + '.map.js',
|
||||||
transform: contents => `module.exports = ${contents.toString()};`
|
transform: contents => `module.exports = ${contents.toString()};`
|
||||||
}
|
}
|
||||||
@ -33,7 +34,7 @@ const pluginDeploy = config && config.copyPath ?
|
|||||||
verbose: true
|
verbose: true
|
||||||
}) :
|
}) :
|
||||||
// 更新 .map 到 .map.js 并上传
|
// 更新 .map 到 .map.js 并上传
|
||||||
screeps({ config, dryRun: !config })
|
screeps({ runConfig, dryRun: !config })
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
input: 'src/main.ts',
|
input: 'src/main.ts',
|
||||||
|
5
src/index.d.ts
vendored
5
src/index.d.ts
vendored
@ -1,3 +1,4 @@
|
|||||||
interface CreepMemory{
|
interface CreepMemory{
|
||||||
role: string
|
role: string,
|
||||||
}
|
working: boolean | null
|
||||||
|
}
|
||||||
|
24
src/main.ts
24
src/main.ts
@ -1,4 +1,26 @@
|
|||||||
import { errorMapper } from "./modules/errorMapping";
|
import { errorMapper } from "./modules/errorMapping";
|
||||||
|
import { Harvest } from "./modules/harvester";
|
||||||
|
import { Build } from "./modules/builder";
|
||||||
|
import { Upgrade } from "./modules/upgrader";
|
||||||
|
import { SpawnCreep } from "./modules/spawnCreep";
|
||||||
export const loop = errorMapper(() => {
|
export const loop = errorMapper(() => {
|
||||||
console.log("Game Start");
|
let mainSpawn = Game.spawns["Spawn1"];
|
||||||
|
SpawnCreep(mainSpawn);
|
||||||
|
for (var name in Memory.creeps) {
|
||||||
|
if (!Game.creeps[name]) {
|
||||||
|
delete Memory.creeps[name];
|
||||||
|
console.log("clearing non-existing creep memory:", name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var name in Game.creeps) {
|
||||||
|
let creep = Game.creeps[name];
|
||||||
|
if (creep.memory.role == "Harvester") {
|
||||||
|
Harvest(creep);
|
||||||
|
} else if (creep.memory.role == "Upgrader") {
|
||||||
|
Upgrade(creep);
|
||||||
|
} else if (creep.memory.role == "Builder") {
|
||||||
|
Build(creep);
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
34
src/modules/builder.ts
Normal file
34
src/modules/builder.ts
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import { FindCapacity } from "./util";
|
||||||
|
|
||||||
|
export function Build(creep: Creep) {
|
||||||
|
if (creep.memory.working && creep.store[RESOURCE_ENERGY] == 0) {
|
||||||
|
creep.memory.working = false;
|
||||||
|
creep.say("harvest")
|
||||||
|
}
|
||||||
|
if (!creep.memory.working && creep.store.getFreeCapacity(RESOURCE_ENERGY) == 0) {
|
||||||
|
creep.memory.working = true
|
||||||
|
creep.say("builder")
|
||||||
|
}
|
||||||
|
if (creep.memory.working) {
|
||||||
|
let target = creep.room.find(FIND_CONSTRUCTION_SITES);
|
||||||
|
if (target.length > 0) {
|
||||||
|
if (creep.build(target[0]) == ERR_NOT_IN_RANGE) {
|
||||||
|
creep.moveTo(target[0], { visualizePathStyle: { stroke: "#ffaa00" } });
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let targets = FindCapacity(creep);
|
||||||
|
if (targets.length > 0) {
|
||||||
|
target.sort();
|
||||||
|
if (creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
|
||||||
|
creep.moveTo(targets[0], { visualizePathStyle: { stroke: "#ffaa00" } });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
creep.memory.working = false
|
||||||
|
let sources = creep.room.find(FIND_SOURCES);
|
||||||
|
if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
|
||||||
|
creep.moveTo(sources[0], { visualizePathStyle: { stroke: "#ffaa00" } });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
17
src/modules/harvester.ts
Normal file
17
src/modules/harvester.ts
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import { FindCapacity } from "./util"
|
||||||
|
|
||||||
|
export function Harvest(creep: Creep) {
|
||||||
|
if (creep.store.getFreeCapacity() > 0) {
|
||||||
|
let sources: Source[] = creep.room.find(FIND_SOURCES);
|
||||||
|
if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
|
||||||
|
creep.moveTo(sources[0], { visualizePathStyle: { stroke: "#ffaa00" } });
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let targets = FindCapacity(creep);
|
||||||
|
if (targets.length > 0) {
|
||||||
|
if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE){
|
||||||
|
creep.moveTo(targets[0], { visualizePathStyle: { stroke: "#ffffff" } });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
9
src/modules/setting.ts
Normal file
9
src/modules/setting.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
let HARVESTER_COUNT: number = 2;
|
||||||
|
let BUILDER_COUNT: number = 2;
|
||||||
|
let UPGRADER_COUNT: number = 6;
|
||||||
|
export {
|
||||||
|
HARVESTER_COUNT,
|
||||||
|
BUILDER_COUNT,
|
||||||
|
UPGRADER_COUNT
|
||||||
|
}
|
||||||
|
|
76
src/modules/spawnCreep.ts
Normal file
76
src/modules/spawnCreep.ts
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
import "./setting";
|
||||||
|
import { BUILDER_COUNT, HARVESTER_COUNT, UPGRADER_COUNT } from "./setting";
|
||||||
|
|
||||||
|
var createDefaultHarvester = (spawn: StructureSpawn) => {
|
||||||
|
let name = "Harvester" + Game.time;
|
||||||
|
return spawn.spawnCreep([WORK, CARRY, MOVE], name, {
|
||||||
|
memory: {
|
||||||
|
role: "Harvester",
|
||||||
|
working: false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
var createDefaultBuilder = (spawn: StructureSpawn) => {
|
||||||
|
let name = "Builder" + Game.time;
|
||||||
|
return spawn.spawnCreep([WORK, CARRY, MOVE], name, {
|
||||||
|
memory: {
|
||||||
|
role: "Builder",
|
||||||
|
working: false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
var createDefaultUpgrader = (spawn: StructureSpawn) => {
|
||||||
|
let name = "Upgrader" + Game.time;
|
||||||
|
return spawn.spawnCreep([WORK, CARRY, MOVE], name, {
|
||||||
|
memory: {
|
||||||
|
role: "Upgrader",
|
||||||
|
working: false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function SpawnCreep(spawn: StructureSpawn) {
|
||||||
|
if (spawn.spawning) return;
|
||||||
|
|
||||||
|
|
||||||
|
let harvesters = _.filter(Game.creeps,
|
||||||
|
/**@param {Creep} creep*/
|
||||||
|
(creep) => {
|
||||||
|
return creep.memory.role == "Harvester";
|
||||||
|
});
|
||||||
|
let builders = _.filter(Game.creeps,
|
||||||
|
/**@param {Creep} creep*/
|
||||||
|
(creep) => {
|
||||||
|
return creep.memory.role == "Builder";
|
||||||
|
});
|
||||||
|
let upgraders = _.filter(Game.creeps,
|
||||||
|
/**@param {Creep} creep*/
|
||||||
|
(creep) => {
|
||||||
|
return creep.memory.role == "Upgrader";
|
||||||
|
});
|
||||||
|
if (harvesters.length < HARVESTER_COUNT) {
|
||||||
|
if (createDefaultHarvester(spawn) == 0) {
|
||||||
|
console.log("Spawn Harvester")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (upgraders.length < UPGRADER_COUNT) {
|
||||||
|
if (createDefaultUpgrader(spawn) == 0) {
|
||||||
|
console.log("Spawn Upgrader")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (builders.length < BUILDER_COUNT) {
|
||||||
|
if (createDefaultBuilder(spawn) == 0) {
|
||||||
|
console.log("Spawn Builder")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (spawn.spawning) {
|
||||||
|
var spawningCreep = Game.creeps[spawn.spawning.name];
|
||||||
|
spawn.room.visual.text(
|
||||||
|
'🛠️' + spawningCreep.memory.role,
|
||||||
|
spawn.pos.x + 1,
|
||||||
|
spawn.pos.y,
|
||||||
|
{ align: 'left', opacity: 0.8 });
|
||||||
|
}
|
||||||
|
}
|
23
src/modules/upgrader.ts
Normal file
23
src/modules/upgrader.ts
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
/**@param {Creep} creep */
|
||||||
|
export function Upgrade(creep: Creep) {
|
||||||
|
if (creep.memory.working && creep.store[RESOURCE_ENERGY] == 0) {
|
||||||
|
creep.memory.working = false;
|
||||||
|
creep.say('🔄 harvest');
|
||||||
|
}
|
||||||
|
if (!creep.memory.working && creep.store.getFreeCapacity() == 0) {
|
||||||
|
creep.memory.working = true;
|
||||||
|
creep.say('⚡ upgrade');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (creep.memory.working) {
|
||||||
|
if (creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
|
||||||
|
creep.moveTo(creep.room.controller, { visualizePathStyle: { stroke: '#ffffff' } });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var sources = creep.room.find(FIND_SOURCES);
|
||||||
|
if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
|
||||||
|
creep.moveTo(sources[0], { visualizePathStyle: { stroke: '#ffaa00' } });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
12
src/modules/util.ts
Normal file
12
src/modules/util.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
export function FindCapacity(creep: Creep) {
|
||||||
|
return creep.room.find(FIND_STRUCTURES, {
|
||||||
|
filter: (structure: StructureSpawn | StructureExtension) => {
|
||||||
|
let type = structure.structureType;
|
||||||
|
return (
|
||||||
|
(type == STRUCTURE_EXTENSION ||
|
||||||
|
type == STRUCTURE_SPAWN) &&
|
||||||
|
structure.store.getFreeCapacity() != null && structure.store.getFreeCapacity() as number > 0
|
||||||
|
)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
@ -1,37 +0,0 @@
|
|||||||
let util = require("util");
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
/**@param {Creep} creep */
|
|
||||||
run: function (creep) {
|
|
||||||
if (creep.memory.working && creep.store[RESOURCE_ENERGY] == 0) {
|
|
||||||
creep.memory.working = false;
|
|
||||||
creep.say("harvest")
|
|
||||||
}
|
|
||||||
if (!creep.memory.working && creep.store.getFreeCapacity(RESOURCE_ENERGY) == 0) {
|
|
||||||
creep.memory.working = true
|
|
||||||
creep.say("builder")
|
|
||||||
}
|
|
||||||
if (creep.memory.working) {
|
|
||||||
let target = creep.room.find(FIND_CONSTRUCTION_SITES);
|
|
||||||
if (target.length > 0) {
|
|
||||||
if (creep.build(target[0]) == ERR_NOT_IN_RANGE) {
|
|
||||||
creep.moveTo(target[0], {visualizePathStyle: {stroke: "#ffaa00"}});
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
let targets = util.FindCapacity(creep);
|
|
||||||
if (targets.length > 0) {
|
|
||||||
target.sort();
|
|
||||||
if (creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
|
|
||||||
creep.moveTo(targets[0], {visualizePathStyle: {stroke: "#ffaa00"}});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
creep.memory.working = false
|
|
||||||
let sources = creep.room.find(FIND_SOURCES);
|
|
||||||
if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
|
|
||||||
creep.moveTo(sources[0], {visualizePathStyle: {stroke: "#ffaa00"}});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
let util = require("util")
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
/**@param {Creep} creep */
|
|
||||||
run: function(creep){
|
|
||||||
if(creep.store.getFreeCapacity() > 0){
|
|
||||||
let sources = creep.room.find(FIND_SOURCES);
|
|
||||||
sources.sort();
|
|
||||||
if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE){
|
|
||||||
creep.moveTo(sources[0], {visualizePathStyle: {stroke: "#ffaa00"}});
|
|
||||||
}
|
|
||||||
}else{
|
|
||||||
let target = util.FindCapacity(creep);
|
|
||||||
|
|
||||||
if(target.length > 0){
|
|
||||||
if(creep.transfer(target[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE){
|
|
||||||
creep.moveTo(target[0], {visualizePathStyle: {stroke: "#ffaa00"}});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,27 +0,0 @@
|
|||||||
let spawnCreeps = require("spawnCreeps");
|
|
||||||
let harvester = require("harvester")
|
|
||||||
let upgrader = require("upgrader")
|
|
||||||
let builder = require("builder")
|
|
||||||
|
|
||||||
module.exports.loop = function(){
|
|
||||||
let mainSpawn = Game.spawns["Spawn1"];
|
|
||||||
spawnCreeps.run(mainSpawn);
|
|
||||||
|
|
||||||
for(var name in Memory.creeps) {
|
|
||||||
if(!Game.creeps[name]) {
|
|
||||||
delete Memory.creeps[name];
|
|
||||||
console.log('Clearing non-existing creep memory:', name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var name in Game.creeps){
|
|
||||||
let creep = Game.creeps[name];
|
|
||||||
if(creep.memory.role == "Harvester"){
|
|
||||||
harvester.run(creep);
|
|
||||||
}else if(creep.memory.role == "Upgrader"){
|
|
||||||
upgrader.run(creep)
|
|
||||||
}else if(creep.memory.role == "Builder"){
|
|
||||||
builder.run(creep)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,72 +0,0 @@
|
|||||||
|
|
||||||
let harvesterCount = 2;
|
|
||||||
let builderCount = 3;
|
|
||||||
let upgraderCount = 6;
|
|
||||||
|
|
||||||
|
|
||||||
/**@param {StructureSpawn} spawn */
|
|
||||||
var createDefaultHarvester = (spawn) => {
|
|
||||||
let name = "Harvester" + Game.time;
|
|
||||||
return spawn.spawnCreep([WORK, CARRY, MOVE], name, { memory: { role: "Harvester" } });
|
|
||||||
}
|
|
||||||
/**@param {StructureSpawn} spawn */
|
|
||||||
var createDefaultBuilder = (spawn) => {
|
|
||||||
let name = "Builder" + Game.time;
|
|
||||||
return spawn.spawnCreep([WORK, CARRY, MOVE], name, { memory: { role: "Builder" } });
|
|
||||||
}
|
|
||||||
|
|
||||||
/**@param {StructureSpawn} spawn */
|
|
||||||
var createDefaultUpgrader = (spawn) => {
|
|
||||||
let name = "Upgrader" + Game.time;
|
|
||||||
return spawn.spawnCreep([WORK, CARRY, MOVE], name, { memory: { role: "Upgrader" } });
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
/**@param {StructureSpawn} spawn*/
|
|
||||||
run: function (spawn) {
|
|
||||||
|
|
||||||
if (spawn.spawning) return;
|
|
||||||
|
|
||||||
|
|
||||||
let harvesters = _.filter(Game.creeps,
|
|
||||||
/**@param {Creep} creep*/
|
|
||||||
(creep) => {
|
|
||||||
return creep.memory.role == "Harvester";
|
|
||||||
});
|
|
||||||
let builders = _.filter(Game.creeps,
|
|
||||||
/**@param {Creep} creep*/
|
|
||||||
(creep) => {
|
|
||||||
return creep.memory.role == "Builder";
|
|
||||||
});
|
|
||||||
let upgraders = _.filter(Game.creeps,
|
|
||||||
/**@param {Creep} creep*/
|
|
||||||
(creep) => {
|
|
||||||
return creep.memory.role == "Upgrader";
|
|
||||||
});
|
|
||||||
if (harvesters.length < harvesterCount) {
|
|
||||||
if (createDefaultHarvester(spawn) == 0) {
|
|
||||||
console.log("Spawn Harvester")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (upgraders.length < upgraderCount) {
|
|
||||||
if(createDefaultUpgrader(spawn) == 0){
|
|
||||||
console.log("Spawn Upgrader")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (builders.length < builderCount) {
|
|
||||||
if(createDefaultBuilder(spawn) == 0){
|
|
||||||
console.log("Spawn Builder")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (spawn.spawning) {
|
|
||||||
var spawningCreep = Game.creeps[spawn.spawning.name];
|
|
||||||
spawn.room.visual.text(
|
|
||||||
'🛠️' + spawningCreep.memory.role,
|
|
||||||
spawn.pos.x + 1,
|
|
||||||
spawn.pos.y,
|
|
||||||
{ align: 'left', opacity: 0.8 });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,25 +0,0 @@
|
|||||||
module.exports = {
|
|
||||||
/**@param {Creep} creep */
|
|
||||||
run: function (creep) {
|
|
||||||
if (creep.memory.upgrading && creep.store[RESOURCE_ENERGY] == 0) {
|
|
||||||
creep.memory.upgrading = false;
|
|
||||||
creep.say('🔄 harvest');
|
|
||||||
}
|
|
||||||
if (!creep.memory.upgrading && creep.store.getFreeCapacity() == 0) {
|
|
||||||
creep.memory.upgrading = true;
|
|
||||||
creep.say('⚡ upgrade');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (creep.memory.upgrading) {
|
|
||||||
if (creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
|
|
||||||
creep.moveTo(creep.room.controller, { visualizePathStyle: { stroke: '#ffffff' } });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var sources = creep.room.find(FIND_SOURCES);
|
|
||||||
if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
|
|
||||||
creep.moveTo(sources[0], { visualizePathStyle: { stroke: '#ffaa00' } });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,18 +0,0 @@
|
|||||||
module.exports = {
|
|
||||||
/**@param {Creep} creep */
|
|
||||||
FindCapacity: function(creep){
|
|
||||||
return creep.room.find(FIND_STRUCTURES, {
|
|
||||||
/**@param {Structure} structure */
|
|
||||||
filter: (structure)=>{
|
|
||||||
let type = structure.structureType;
|
|
||||||
return ((type == STRUCTURE_EXTENSION) ||
|
|
||||||
(type == STRUCTURE_SPAWN )) &&
|
|
||||||
structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
/** @param {Creep} creep */
|
|
||||||
Log: function(creep, msg){
|
|
||||||
console.log(creep.name + " " + msg)
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,21 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "es2017",
|
||||||
|
"moduleResolution": "Node",
|
||||||
|
"outDir": "dist/",
|
||||||
|
"baseUrl": "./",
|
||||||
|
"sourceMap": true,
|
||||||
|
"allowSyntheticDefaultImports": true,
|
||||||
|
"resolveJsonModule": true,
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"paths": {
|
||||||
|
"@/*": ["./src/*"]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"exclude": [
|
||||||
|
"node_modules"
|
||||||
|
],
|
||||||
|
"include": [
|
||||||
|
"src/**/*.ts"
|
||||||
|
]
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user