forked from Yuandiaodiaodiao/Screeps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.js
115 lines (104 loc) · 3.31 KB
/
builder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
function getting(creep) {
let room = Game.rooms[creep.memory.missionid]
let target = room.storage
if (target && target.store[RESOURCE_ENERGY] > 1e5) {
const act = creep.withdraw(target, RESOURCE_ENERGY)
if (act == ERR_NOT_IN_RANGE) {
creep.moveTo(target)
if(Game.time%20==0){
require('tools').roomCachettl[creep.pos.roomName] = 0
}
} else if (act === OK || act === ERR_FULL) {
creep.memory.status = 'building'
creep.carry.energy = 1600
require('tools').roomCachettl[creep.pos.roomName] = 0
building(creep)
}
} else {
target = creep.pos.findClosestByPath(FIND_STRUCTURES, {
filter: obj => obj.structureType == STRUCTURE_CONTAINER
&& obj.store[RESOURCE_ENERGY] > 1000
})
if (target) {
if (creep.withdraw(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(target)
}
}
if (creep.carry.energy >= creep.carryCapacity) {
creep.memory.status = 'building'
}
}
}
function building(creep) {
let target = Game.getObjectById(creep.memory.buildtarget)
if (target) {
const act = creep.build(target)
if (act == ERR_NOT_IN_RANGE) {
creep.moveTo(target, {reusePath: 10})
} else if (act == ERR_NOT_ENOUGH_RESOURCES) {
creep.memory.status = 'getting'
} else if (act == ERR_INVALID_TARGET) {
creep.memory.buildtarget = ""
} else if (act == OK) {
if (creep.carry.energy <= creep.getActiveBodyparts('work') * 5) {
creep.memory.status = 'getting'
getting(creep)
}
}
} else {
require('tools').roomCachettl[creep.pos.roomName] = 0
let room = Game.rooms[creep.memory.missionid]
target = require('tools').findrooms(room, FIND_CONSTRUCTION_SITES)[0]
if (target) {
creep.memory.buildtarget = target.id
} else {
creep.memory.status = 'sleep'
}
}
}
function work(creep) {
//build
if (creep.memory.status == 'building') {
building(creep)
} else if (creep.memory.status == 'getting') {
getting(creep)
} else if (creep.memory.status == 'sleep') {
if (Game.time % 10 == 0) {
creep.memory.status = 'building'
}
}
}
function born(spawnnow, creepname, memory) {
let body = {
'work': 24,
'carry': 9,
'move': 17
}
let bodyarray = require('tools').generatebody(body, spawnnow)
return spawnnow.spawnCreep(
bodyarray,
creepname,
{
memory: {
status: 'getting',
missionid: memory.roomName,
buildtarget: ''
}
}
)
}
function miss(room) {
room.memory.missions.builder = {}
if (require('tools').findrooms(room, FIND_CONSTRUCTION_SITES).length > 0 && (!(room.storage && room.storage.store[RESOURCE_ENERGY] / room.storage.store.getCapacity() < 0.1))) {
room.memory.missions.builder[room.name] = {
roomName: room.name
}
} else {
room.memory.missions.builder = undefined
}
}
module.exports = {
'work': work,
'born': born,
'miss': miss,
};