1 Star 0 Fork 0

MaHe666/myScreepsCode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
roles.js 19.80 KB
一键复制 编辑 原始数据 按行查看 历史
MaHe666 提交于 2022-04-30 21:30 . 2022.4.30 更新
const find_Resource = require('find.resource')
const find_Structure = require('find.structure')
const variable_homeSignal = require("variable.homeSignal");
const roles = {
//creeps工作
run: function () {
for (let name in Game.creeps) {
let creep = Game.creeps[name]
//出生地是自己的home
if (creep.memory.homeRoom == undefined) {
creep.memory.homeRoom = creep.room;
}
if (creep.memory.role == 'harvester') {
this.harvester(creep);
}
if (creep.memory.role == 'upgrader') {
this.upgrader(creep);
}
if (creep.memory.role == 'builder') {
this.builder(creep);
}
if (creep.memory.role == 'energyCarrier') {
this.energyCarrier(creep)
}
if (creep.memory.role == 'attacker') {
this.attacker(creep);
}
if (creep.memory.role == 'claimer') {
this.claimer(creep)
}
if (creep.memory.role == 'miner') {
this.miner(creep)
}
if (creep.memory.role == 'crossRoomBuilder') {
this.crossRoomBuilder(creep)
}
if (creep.memory.role == 'terminalCarrier') {
this.terminalCarrier(creep)
}
if (creep.memory.role == 'cleaner') {
this.cleaner(creep)
}
}
},
harvester: function (creep) {
if (!creep.memory.working && creep.store[RESOURCE_ENERGY] == 0) {
creep.memory.working = true;
}
if (creep.memory.working && creep.store.getFreeCapacity() == 0) {
creep.memory.working = false;
}
let source;
let container
for (let data of creep.room.memory.energyAndEnergyContainer) {
let s = Game.getObjectById(data.sourceId)
if (s.energy > 0) {
source = s
//container
let c = Game.getObjectById(data.containerId)
let target = find_Structure.find_NeedEnergy(creep);
if (data.containerId != undefined && c.store[RESOURCE_ENERGY] != 2000) {
container = c
//storage
} else if (creep.room.storage != undefined) {
container = creep.room.storage
//
} else if (target != undefined && target.length != 0) {
container = target
}
break;
}
}
//采矿 true
if (creep.memory.working) {
if (creep.harvest(source) == ERR_NOT_IN_RANGE) {
creep.moveTo(source);
}
//搬运 false
} else {
if (creep.transfer(container, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(container)
}
}
},
upgrader: function (creep) {
if (creep.memory.working && creep.store[RESOURCE_ENERGY] == 0) {
creep.memory.working = false;
}
if (!creep.memory.working && creep.store.getFreeCapacity() == 0) {
creep.memory.working = true;
}
//升级controller true
if (creep.memory.working) {
if (creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
creep.moveTo(creep.room.controller);
}
//拿energy false
} else {
find_Resource.fromStorage(creep, RESOURCE_ENERGY);
}
},
builder: function (creep) {
if (creep.memory.working && creep.store[RESOURCE_ENERGY] == 0) {
creep.memory.working = false;
}
if (!creep.memory.working && creep.store.getFreeCapacity() == 0) {
creep.memory.working = true;
}
//建造 或 其他工作 true
if (creep.memory.working) {
let target = creep.pos.findClosestByPath(FIND_CONSTRUCTION_SITES);
//如果有需要建造的目标
if (target != undefined) {
if (creep.build(target) == ERR_NOT_IN_RANGE) {
creep.moveTo(target);
}
} else {
//如果没有目标,则执行其他工作(upgrade)
if (creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
creep.moveTo(creep.room.controller);
}
}
// 拿energy false
} else {
find_Resource.fromStorage(creep, RESOURCE_ENERGY);
}
},
energyCarrier: function (creep) {
if (creep.memory.working && creep.store[RESOURCE_ENERGY] == 0) {
creep.memory.working = false;
}
if (!creep.memory.working && creep.store.getFreeCapacity() == 0) {
creep.memory.working = true;
}
//获取所有需要充能的建筑
let targets = find_Structure.find_NeedEnergy(creep);
//判断是否存在充能的建筑
let flag = targets != undefined
if (flag) {
//搬运能量至storage信号
creep.memory.transferStorage = false;
}
// 搬运工作 working == true
if (creep.memory.working) {
//当充能目标需要充能时
if (flag) {
if (creep.transfer(targets, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(targets);
}
//不存在则开始搬运能量至storage
} else {
creep.memory.transferStorage = true;
let storage = creep.room.storage;
if (storage != undefined) {
if (creep.transfer(storage, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(storage);
}
}
}
// 找能量 working == false
} else {
//如果有装满energy的container,则从这里拿
let container = find_Structure.findContainer_Full(creep);
if (container != undefined) {
if (creep.withdraw(container, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(container);
}
//如果没有,则按照下面的逻辑拿
} else {
//如果正在执行向storage中存energy transferStorage == true
if (creep.memory.transferStorage) {
//如果有满了的container,则满的container去拿
find_Resource.fromContainer(creep, RESOURCE_ENERGY)
//如果需要拿能量去充能
} else {
//需要充能的话则从stroage拿能量
find_Resource.fromStorage(creep, RESOURCE_ENERGY);
}
}
}
},
attacker: function (creep) {
//如果自己的room受到攻击
if (creep.room.memory.attackWarning) {
let hostile_creeps = creep.pos.findClosestByPath(FIND_HOSTILE_CREEPS, {
// filter: c => c.getActiveBodyparts(ATTACK) > 0 || c.getActiveBodyparts(RANGED_ATTACK) > 0
})
if (hostile_creeps != undefined) {
if (creep.attack(hostile_creeps) == ERR_NOT_IN_RANGE) {
creep.moveTo(hostile_creeps)
}
}
} else {
//如果不需要攻击,则回到自己的room中待命
if (!variable_homeSignal.attackFlag) {
creep.moveTo(new RoomPosition(creep.memory.homeRoom.x, creep.memory.homeRoom.y, creep.memory.homeRoom.name));
// 如果需要攻击
} else {
if (creep.room.name == variable_homeSignal.attackTarget.roomName) {
let hostile_creep = creep.pos.findClosestByPath(FIND_HOSTILE_CREEPS);
let wall = creep.pos.findClosestByPath(FIND_STRUCTURES, {
filter: w => w.hits < 10 && w.structureType == STRUCTURE_WALL
//先攻击敌对creeps
})
if (hostile_creep != undefined) {
if (creep.attack(hostile_creep) == ERR_NOT_IN_RANGE) {
creep.moveTo(hostile_creep)
}
} else if (wall != undefined) {
if (creep.attack(wall) == ERR_NOT_IN_RANGE) {
creep.moveTo(wall)
}
} else {
let hostile_structure = creep.pos.findClosestByPath(FIND_HOSTILE_STRUCTURES)
let hostile_spawn = creep.pos.findClosestByPath(FIND_HOSTILE_SPAWNS)
if (hostile_spawn != undefined) {
if (creep.attack(hostile_spawn) == ERR_NOT_IN_RANGE) {
creep.moveTo(hostile_spawn)
}
}
if (hostile_structure != undefined) {
if (creep.attack(hostile_structure) == ERR_NOT_IN_RANGE) {
creep.moveTo(hostile_structure)
}
}
}
} else {
creep.moveTo(new RoomPosition(
variable_homeSignal.attackTarget.x,
variable_homeSignal.attackTarget.y,
variable_homeSignal.attackTarget.roomName
))
}
}
}
},
claimer: function (creep) {
//不需占领时回家
if (!variable_homeSignal.claimFlag) {
creep.moveTo(new RoomPosition(creep.memory.homeRoom.x, creep.memory.homeRoom.y, creep.memory.homeRoom.name));
//如果需要占领
} else {
//如果所处房间是目标房间,则占领controller
if (creep.room.name == variable_homeSignal.claimTarget.roomName) {
if (creep.claimController(creep.room.controller) == ERR_NOT_IN_RANGE) {
creep.moveTo(creep.room.controller)
}
} else {
//如果不是,则跨房间
creep.moveTo(new RoomPosition(
variable_homeSignal.claimTarget.x,
variable_homeSignal.claimTarget.y,
variable_homeSignal.claimTarget.roomName
))
}
}
},
miner: function (creep) {
let mineral;
let container;
for (let data of creep.room.memory.mineralAndMineralContainer) {
let m = Game.getObjectById(data.mineralId)
if (m.mineralAmount > 0) {
mineral = m
if (data.containerId != undefined) {
let c = Game.getObjectById(data.containerId)
if (c.store[mineral.mineralType] != 2000) {
container = c
}
} else if (creep.room.storage != undefined) {
container = creep.room.storage
}
break
}
}
if (!creep.memory.working && creep.store[mineral.mineralType] == 0) {
creep.memory.working = true;
}
if (creep.memory.working && creep.store.getFreeCapacity() == 0) {
creep.memory.working = false;
}
//采矿 true
if (creep.memory.working) {
//使用Game.getObjectById获取mineral
if (creep.harvest(mineral) == ERR_NOT_IN_RANGE) {
creep.moveTo(mineral);
}
//搬运 false
} else {
// 搬运至container
if (creep.transfer(container, mineral.mineralType) == ERR_NOT_IN_RANGE) {
creep.moveTo(container);
}
}
},
crossRoomBuilder: function (creep) {
if (!variable_homeSignal.buildFlag) {
creep.moveTo(new RoomPosition(creep.memory.homeRoom.x, creep.memory.homeRoom.y, creep.memory.homeRoom.name));
} else {
//如果所处房间是目标房间,则占领controller
if (creep.room.name == variable_homeSignal.buildTarget.roomName) {
let targets = creep.room.find(FIND_CONSTRUCTION_SITES);
if (creep.build(targets[0]) == ERR_NOT_IN_RANGE) {
creep.moveTo(targets[0])
}
} else {
//如果不是,则跨房间
creep.moveTo(new RoomPosition(
variable_homeSignal.buildTarget.x,
variable_homeSignal.buildTarget.y,
variable_homeSignal.buildTarget.roomName
))
}
}
},
terminalCarrier: function (creep) {
let terminal = creep.room.terminal
let storage = creep.room.storage
let type = variable_homeSignal.sendTarget.resourceType
let targetAmount = variable_homeSignal.sendTarget.amount
let terminalAmount = terminal.store[type]
if (creep.memory.capacity == undefined) {
creep.memory.capacity = creep.store.getCapacity()
}
//获取creep能的总容量
let capacity = creep.memory.capacity
// 拿取
if (creep.memory.working && creep.store.getUsedCapacity() == 0) {
creep.memory.working = false;
}
// 搬运
if (!creep.memory.working && creep.store.getFreeCapacity() != capacity) {
creep.memory.working = true;
}
// 剩余目标量
let surplusMineralAmount = targetAmount - terminalAmount
// 设置需要的目标能量为mineral数量的 五分之一
// let surplusEnergyAmount = Math.ceil(targetAmount / 3 * 2) - terminal.store[RESOURCE_ENERGY]
const cost = Game.market.calcTransactionCost(variable_homeSignal.sendTarget.amount, terminal.room.name, variable_homeSignal.sendTarget.destination)
let surplusEnergyAmount = cost - terminal.store[RESOURCE_ENERGY]
if (creep.memory.working) {
// 如果小于目标值,则运送mineral
if (terminalAmount < targetAmount) {
if (creep.transfer(terminal, type) == ERR_NOT_IN_RANGE) {
creep.moveTo(terminal)
}
} else {
//运送energy至terminal
if (creep.transfer(terminal, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(terminal)
}
}
} else {
// 从storage拿取mineral
if (terminalAmount < targetAmount) {
let a;
if (surplusMineralAmount > capacity) {
a = capacity
} else {
a = surplusMineralAmount
}
if (creep.withdraw(storage, type, a) == ERR_NOT_IN_RANGE) {
creep.moveTo(storage)
}
// 从storage拿取energy
} else {
let a;
if (surplusEnergyAmount > capacity) {
a = capacity
} else {
a = surplusEnergyAmount
}
if (creep.withdraw(storage, RESOURCE_ENERGY, a) == ERR_NOT_IN_RANGE) {
creep.moveTo(storage)
}
}
}
},
cleaner: function (creep) {
let tombstones = creep.pos.findClosestByPath(FIND_TOMBSTONES, {
filter: t => Object.keys(t.store)[0] != undefined
})
let dropped_resource = creep.pos.findClosestByPath(FIND_DROPPED_RESOURCES)
let ruins = creep.pos.findClosestByPath(FIND_RUINS, {
filter: t => Object.keys(t.store)[0] != undefined
})
let terminal = creep.pos.findClosestByPath(FIND_STRUCTURES, {
filter: s => s.structureType == STRUCTURE_TERMINAL && s.store.getFreeCapacity() != s.store.getCapacity()
})
let mineralContainer;
let mineralType;
// mineralAndMineralContainer 值只有一个
let data = Game.rooms[creep.memory.homeRoom.name].memory.mineralAndMineralContainer[0].containerId
// console.log(Game.rooms[creep.memory.homeRoom.name].memory.mineralAndMineralContainer[0].containerId)
if (data.containerId != undefined) {
let c = Game.getObjectById(data.containerId)
let mt = (Game.getObjectById(data.mineralId)).mineralType
if (c.store[mt] > 0) {
mineralContainer = c
mineralType = mt
}
}
let energyContainer;
for (let data of creep.room.memory.energyAndEnergyContainer) {
if (data.containerId != undefined) {
let c = Game.getObjectById(data.containerId)
if (c.store[RESOURCE_ENERGY] > 1000) {
energyContainer = c
break
}
}
}
let flag = (
tombstones != undefined ||
ruins != undefined ||
dropped_resource != undefined ||
(terminal != undefined && variable_homeSignal.cleanTerminalFlag) ||
mineralContainer != undefined ||
energyContainer != undefined
)
if (!creep.memory.working && creep.store.getUsedCapacity() == 0) {
creep.memory.working = true;
}
if (creep.memory.working && (creep.store.getFreeCapacity() == 0 || !flag)) {
creep.memory.working = false;
}
// 找能量 true
if (creep.memory.working) {
//清理掉落的资源
if (dropped_resource != undefined) {
if (creep.pickup(dropped_resource) == ERR_NOT_IN_RANGE) {
creep.moveTo(dropped_resource);
}
//清理坟墓
} else if (tombstones != undefined) {
if (creep.withdraw(tombstones, Object.keys(tombstones.store)[0]) == ERR_NOT_IN_RANGE) {
creep.moveTo(tombstones)
}
//清理废墟
} else if (ruins != undefined) {
if (creep.withdraw(ruins, Object.keys(ruins.store)[0]) == ERR_NOT_IN_RANGE) {
creep.moveTo(ruins);
}
} else if (variable_homeSignal.cleanTerminalFlag && terminal != undefined) {
if (creep.withdraw(terminal, Object.keys(terminal.store)[0]) == ERR_NOT_IN_RANGE) {
creep.moveTo(terminal)
}
} else if (energyContainer != undefined) {
if (creep.withdraw(energyContainer, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(energyContainer)
}
} else if (mineralContainer != undefined) {
if (creep.withdraw(mineralContainer, mineralType) == ERR_NOT_IN_RANGE) {
creep.moveTo(mineralContainer)
}
} else {
for (let r of variable_homeSignal.homeRooms) {
if (creep.room.name == r.name) {
creep.moveTo(r.x, r.y)
}
}
}
//搬运至 storage
} else {
if (creep.store.getUsedCapacity() == 0) {
for (let r of variable_homeSignal.homeRooms) {
if (creep.room.name == r.name) {
creep.moveTo(r.x, r.y)
}
}
} else {
let storage = creep.room.storage
if (storage != undefined) {
if (creep.transfer(storage, Object.keys(creep.store)[0]) == ERR_NOT_IN_RANGE) {
creep.moveTo(storage)
}
}
}
}
}
}
module.exports = roles;
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/mahe666/my-screeps-code.git
git@gitee.com:mahe666/my-screeps-code.git
mahe666
my-screeps-code
myScreepsCode
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385