forked from Yuandiaodiaodiao/Screeps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupgrader.js
138 lines (130 loc) · 4.84 KB
/
upgrader.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
var upgradertime = {}
function work(creep) {
const memory = creep.memory
if (memory.status == 'going') {
let target = Game.getObjectById(memory.missionid)
if (creep.pos.getRangeTo(target) > 1) {
creep.moveTo(target, {reusePath: 10})
} else {
memory.status = 'upgrading'
creep.signController(creep.room.controller, '☕')
}
} else if (memory.status == 'upgrading') {
let target = Game.getObjectById(memory.missionid)
let container = Game.getObjectById(memory.container)
if (!container) {
container = creep.pos.findClosestByRange(FIND_STRUCTURES, {
filter: obj => obj.structureType == STRUCTURE_CONTAINER
})
if (container) memory.container = container.id
} else if (creep.carry.energy <= 40 && container.store.energy > 0) {
creep.withdraw(container, RESOURCE_ENERGY)
upgradertime[creep.pos.roomName] = Game.time
}
const action = creep.upgradeController(target)
if (action == ERR_NOT_IN_RANGE) {
creep.moveTo(target)
} else if (action == ERR_NOT_ENOUGH_RESOURCES) {
memory.status = 'getting'
} else if (action == OK) {
memory._move = undefined
}
}
if (memory.status == 'getting') {
upgradertime[creep.pos.roomName] = Game.time
let target = creep.pos.findClosestByPath(FIND_STRUCTURES, {
filter: obj => {
if (creep.room.controller.level >= 6) {
return obj.structureType == STRUCTURE_CONTAINER
} else {
return (obj.structureType == STRUCTURE_LINK && obj.energy > 0)
|| (obj.structureType == STRUCTURE_TOWER && obj.energy > 0)
|| (obj.structureType == STRUCTURE_STORAGE && obj.store[RESOURCE_ENERGY] > 5e4)
|| (obj.structureType == STRUCTURE_CONTAINER && obj.store[RESOURCE_ENERGY] > 1e3)
}
}
})
if (target) {
if (creep.withdraw(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(target)
}
}
if (creep.carry.energy >= creep.carryCapacity) {
memory.status = 'upgrading';
}
}
}
function born(spawnnow, creepname, memory) {
let body = {
'work': 40,
'carry': 4,
'move': 6
}
if (Game.getObjectById(memory.controller).level >= 8) {
body = {
'work': 15,
'carry': 4,
'move': 5
}
} else if (Game.getObjectById(memory.controller).level <= 4) {
body = {
'work': 16,
'move': 8,
'carry': 12
}
}
let bodyarray = require('tools').generatebody(body, spawnnow)
return spawnnow.spawnCreep(
bodyarray,
creepname,
{
memory: {
status: 'going',
missionid: memory.controller
}
}
)
}
function miss(room) {
let role_num_fix = Game.config.role_num_fix
role_num_fix[room.name] = role_num_fix[room.name] || {}
if (room.storage && room.controller.level >= 4 && room.controller.level <= 7) {
if (room.storage.store[RESOURCE_ENERGY] / room.storage.store.getCapacity() < 0.3) {
role_num_fix[room.name].upgrader = 0
} else if (room.storage.store[RESOURCE_ENERGY] / room.storage.store.getCapacity() < 0.4) {
role_num_fix[room.name].upgrader = 1
} else if (room.storage.store[RESOURCE_ENERGY] / room.storage.store.getCapacity() < 0.5) {
role_num_fix[room.name].upgrader = 2
} else if (room.storage.store[RESOURCE_ENERGY] / room.storage.store.getCapacity() >= 0.6) {
role_num_fix[room.name].upgrader = 3
}
}
if (room.controller.level >= 8) {
if (role_num_fix[room.name].upgrader >= 2) {
role_num_fix[room.name].upgrader = 1
}
if (room.controller.ticksToDowngrade && room.controller.ticksToDowngrade > 100000) {
role_num_fix[room.name].upgrader = 0
}else{
role_num_fix[room.name].upgrader = 1
}
} else {
role_num_fix[room.name].upgrader = 1
}
if (room.controller.ticksToDowngrade && room.controller.ticksToDowngrade < 3000 && role_num_fix[room.name].upgrader == 0) {
role_num_fix[room.name].upgrader = 1
}
if (room.storage && room.storage.store[RESOURCE_ENERGY] / room.storage.store.getCapacity() >= 0.98) {
role_num_fix[room.name].upgrader = 1
}
// if (room.name == 'E29N38') {
// console.log('E29N38 upgrader=' + role_num_fix[room.name].upgrader)
// }
return role_num_fix[room.name].upgrader
}
module.exports = {
'work': work,
'born': born,
'miss': miss,
'upgradertime': upgradertime
};