forked from Yuandiaodiaodiao/Screeps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkmanager.js
91 lines (86 loc) · 3.12 KB
/
linkmanager.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
function work(creep) {
const memory = creep.memory
const link = Game.getObjectById(memory.link)
const container = Game.getObjectById(memory.container)
const storage = creep.room.storage
if (memory.status == 'miss') {
const upgrader = Game.time - (require('upgrader').upgradertime[creep.pos.roomName] || 0)
memory._move = undefined
if (link.energy > 0) {
memory.status = 'getlink'
} else if (container.store.energy / container.store.getCapacity() < 0.5 && upgrader < 50) {
memory.status = 'getstorage'
} else if (creep.carry.energy > 0) {
memory.status = 'fillstorage'
} else if (container.store.energy > 0 && upgrader >= 50) {
memory.status = 'getcontainer'
}
}
if (memory.status == 'getcontainer') {
const act = creep.withdraw(container, RESOURCE_ENERGY)
if (act == ERR_NOT_IN_RANGE) {
creep.moveTo(container)
} else if (act == OK || act == ERR_FULL || act == ERR_NOT_ENOUGH_RESOURCES) {
memory.status = 'fillstorage'
}
} else if (memory.status == 'getlink') {
const act = creep.withdraw(link, RESOURCE_ENERGY)
if (act == ERR_NOT_IN_RANGE) {
creep.moveTo(link)
} else if (act == OK || act == ERR_FULL) {
memory.status = 'fillstorage'
}
} else if (memory.status == 'getstorage') {
if (storage.store.energy > 1e5) {
const act = creep.withdraw(storage, RESOURCE_ENERGY, Math.min(creep.carryCapacity, (container.store.getCapacity() - _.sum(container.store))))
if (act == ERR_NOT_IN_RANGE) {
creep.moveTo(storage)
} else if (act == OK || act == ERR_FULL) {
memory.status = 'fillcontainer'
}
} else {
memory.status = 'miss'
}
} else if (memory.status == 'fillstorage') {
if (_.sum(storage.store) / storage.store.getCapacity() < 0.98) {
const act = creep.transfer(storage, RESOURCE_ENERGY)
if (act == ERR_NOT_IN_RANGE) {
creep.moveTo(storage)
} else if (act == OK || act == ERR_NOT_ENOUGH_RESOURCES) {
memory.status = 'miss'
}
} else {
memory.status = 'miss'
}
} else if (memory.status == 'fillcontainer') {
const act = creep.transfer(container, RESOURCE_ENERGY)
if (act == ERR_NOT_IN_RANGE) {
creep.moveTo(container)
} else if (act == OK || act == ERR_FULL || act == ERR_NOT_ENOUGH_RESOURCES) {
memory.status = 'miss'
}
}
}
function born(spawnnow, creepname, memory) {
let body = {
'carry': 16,
'move': 8
}
let bodyarray = require('tools').generatebody(body, spawnnow)
return spawnnow.spawnCreep(
bodyarray,
creepname,
{
memory: {
status: 'getlink',
missionid: memory.roomName,
link: memory.link,
container: memory.container
}
}
)
}
module.exports = {
'work': work,
'born': born
};