Skip to content

Commit 8240847

Browse files
authored
Reservation, carry, getEnergyFromStorage (#642)
- Improve and fix reservation logic. Intended reservered rooms were flagged as unreservered - Reduce code complexity - New carrys more first to the target before accepting transfers. E.g. commodity carry are integrated into the carry network and reached the target to late - Fix getEnergyFromStorage, especially on `misplacedSpawn` - Adapt code to the quest logic from the documentation
1 parent a88c203 commit 8240847

21 files changed

+341
-156
lines changed

doc/API.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ The sign has the format:
3838
"type": "quest",
3939
"id": "QUEST_ID",
4040
"origin": "ROOM_NAME",
41-
"info": "http://tooangel.github.io/screeps",
41+
"info": "http://tooangel.github.io/screeps"
4242
}
4343
```
4444

@@ -48,6 +48,7 @@ To apply for a Quest send a message via terminal transfer to the `origin` room,
4848
{
4949
"type": "quest",
5050
"id": "QUEST_ID",
51+
"action": "apply"
5152
}
5253
```
5354

@@ -58,7 +59,6 @@ The actual quest will be send to the room the transfer was initiated from.
5859
Quests can be received from the TooAngel NPC and also send to the TooAngel NPC.
5960
When quests are solved the reputation increases.
6061
When quests are send to the TooAngel NPC the reputation is decreased.
61-
In case quests are requested from the TooAngel NPC, while the reputation is too low, these are not executed and a portion is still reduced as a arrogance (TODO maybe find a better word) fee.
6262

6363
### Quest format
6464

@@ -69,7 +69,7 @@ Quests are send via terminal transfer:
6969
"type": "quest",
7070
"id": "QUEST_ID",
7171
"room": "ROOM_NAME, in which the quest needs to be solved",
72-
"type": "TYPE OF QUEST",
72+
"quest": "TYPE OF QUEST",
7373
"end": "Game.time when the quest needs to be finished"
7474
}
7575
```
@@ -85,6 +85,6 @@ Internally the reputation is increased.
8585
{
8686
"type": "quest",
8787
"id": "QUEST_ID",
88-
"result": "won",
88+
"result": "won"
8989
};
9090
```

doc/BaseBuilding.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
## Room
1+
# Room
22

3-
### Setup
3+
## Setup
44

55
Positions:
66
- `upgrader` creep next to the `controller`
@@ -15,3 +15,10 @@ extension, lab, observer, terminal, tower) next to it. Next to `filler` a link,
1515
tower and power_spawn is located. `Link`s are placed next to the sources and at
1616
the paths to the exits. Layers of walls are placed at the exits, positions
1717
within the precalculated paths are replaced by ramparts.
18+
19+
## Pathing
20+
21+
Paths are precalculated and cached and reused by most of the creeps.
22+
23+
Swamps are ignored because roads will be built automatically over time.
24+
The creeps only move on the precalculated paths (to reduce complexity). Instead, blockers are recognized and `structurer` are sent to destroy the structure, `carry` creeps try it as well.

docker-compose-setup.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ services:
88
command: redis-server --loglevel warning
99
mongo:
1010
image: mongo
11+
volumes:
12+
- ./tmp-test-server-database/:/data/db
1113
ports:
1214
- 27017:27017
1315
command: mongod --quiet --logpath /dev/null

docker-compose.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ services:
88
command: redis-server --loglevel warning
99
mongo:
1010
image: mongo
11+
volumes:
12+
- ./tmp-test-server-database/:/data/db
1113
ports:
1214
- 27017:27017
1315
command: mongod --quiet --logpath /dev/null

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "screeps-bot-tooangel",
3-
"version": "1.4.2",
3+
"version": "1.4.3",
44
"description": "",
55
"main": "src/main.js",
66
"screeps_bot": true,

src/brain_nextroom.js

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,44 +75,58 @@ function getNextRoomValuatedRoomMap(rooms) {
7575
return evaluatedRooms;
7676
}
7777

78-
brain.handleNextroomer = function() {
79-
if (!Memory.myRooms) {
80-
return;
81-
}
82-
if (Memory.myRooms.length >= Game.gcl.level) {
83-
return;
84-
}
85-
if (Game.time % config.nextRoom.intervalToCheck !== 0) {
86-
return;
87-
}
88-
debugLog('nextroomer', 'handleNextroomer !!!!!!!!!!!!!!!!!!!!!!!');
78+
/**
79+
* haveEnoughSystemResources
80+
*
81+
* @return {bool}
82+
*/
83+
function haveEnoughSystemResources() {
8984
if (config.nextRoom.resourceStats) {
9085
debugLog('nextroomer', `stats: ${JSON.stringify(global.stats)}`);
9186
const myRoomsLength = Memory.myRooms.length;
9287
const cpuPerRoom = global.stats.cpuUsed / myRoomsLength;
9388
if (cpuPerRoom > global.stats.cpuIdle) {
9489
debugLog('nextroomer', `not enough cpu: ${cpuPerRoom} > ${global.stats.cpuIdle}`);
95-
return;
90+
return false;
9691
}
9792
const heapPerRoom = global.stats.heapUsed / myRoomsLength;
9893
if (heapPerRoom > global.stats.heapFree) {
9994
debugLog('nextroomer', `not enough heap: ${heapPerRoom} > ${global.stats.heapFree}`);
100-
return;
95+
return false;
10196
}
10297
const memoryPerRoom = global.stats.memoryUsed / myRoomsLength;
10398
if (memoryPerRoom > global.stats.memoryFree) {
10499
debugLog('nextroomer', `not enough heap: ${memoryPerRoom} > ${global.stats.memoryFree}`);
105-
return;
100+
return false;
106101
}
107102
} else {
108103
if (Memory.myRooms.length >= 0) { // config.nextRoom.maxRooms) {
109-
return;
104+
return false;
110105
}
111106

112107
if ((Memory.myRooms.length + 1) * config.nextRoom.cpuPerRoom >= Game.cpu.limit) {
113-
return;
108+
return false;
114109
}
115110
}
111+
return true;
112+
}
113+
114+
brain.handleNextroomer = function() {
115+
if (!Memory.myRooms) {
116+
return;
117+
}
118+
if (Memory.myRooms.length >= Game.gcl.level) {
119+
return;
120+
}
121+
if (Game.time % config.nextRoom.intervalToCheck !== 0) {
122+
return;
123+
}
124+
125+
debugLog('nextroomer', 'handleNextroom !!!!!!!!!!!!!!!!!!!!!!!');
126+
if (!haveEnoughSystemResources()) {
127+
return;
128+
}
129+
116130

117131
debugLog('nextroomer', 'handleNextroomer');
118132

src/config.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ global.config = {
182182
},
183183

184184
external: {
185-
distance: 2,
186185
defendDistance: 1,
187186
checkForReservingInterval: 1499,
188187
},
@@ -222,15 +221,15 @@ global.config = {
222221

223222
room: {
224223
reservedRCL: {
225-
0: 1,
226-
1: 1,
227-
2: 1,
228-
3: 1,
229-
4: 1,
230-
5: 1,
231-
6: 2,
232-
7: 2,
233-
8: 2,
224+
0: 4,
225+
1: 4,
226+
2: 4,
227+
3: 4,
228+
4: 4,
229+
5: 4,
230+
6: 4,
231+
7: 8,
232+
8: 8,
234233
},
235234
isHealthyStorageThreshold: 50000,
236235
handleNukeAttackInterval: 132,

src/diplomacy.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function checkPlayers() {
4949
module.exports.checkPlayers = checkPlayers;
5050

5151
const findRoomPairs = function(player) {
52-
for (const roomName of Object.keys(player.rooms).sort((a, b) => 0.5 - Math.random())) {
52+
for (const roomName of Object.keys(player.rooms).sort(() => 0.5 - Math.random())) {
5353
debugLog('diplomacy', `findRoomPairs: room ${roomName} data: ${JSON.stringify(global.data.rooms[roomName])}`);
5454
const minRCL = ((global.data.rooms[roomName] || {}).controller || {}).level || 8;
5555
const range = 7;
@@ -123,7 +123,7 @@ function handleRetaliation(player) {
123123
debugLog('diplomacy', `handleRetaliation: Can not find a fitting room pair`);
124124
return;
125125
}
126-
possibleActions.sort((a, b) => 0.5 - Math.random());
126+
possibleActions.sort(() => 0.5 - Math.random());
127127
debugLog('diplomacy', `Running attach roomPair: ${JSON.stringify(roomPair)} action: ${JSON.stringify(possibleActions[0])}`);
128128
player.lastAttacked = Game.time;
129129
if (config.autoAttack.notify) {
@@ -166,7 +166,9 @@ function addToReputation(name, value) {
166166
try {
167167
handleRetaliation(player);
168168
} catch (e) {
169+
console.log('addToReputation');
169170
console.log(e);
171+
console.log(e.stack);
170172
}
171173
}
172174
}
@@ -193,6 +195,12 @@ function initPlayer(name) {
193195
}
194196
module.exports.initPlayer = initPlayer;
195197

198+
/**
199+
* addRoomToPlayer
200+
*
201+
* @param {object} player
202+
* @param {object} room
203+
*/
196204
function addRoomToPlayer(player, room) {
197205
if (!player.rooms) {
198206
player.rooms = {};

src/logging.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/**
2+
* debugLog
3+
*
4+
* @param {string} type
5+
* @param {...string} messages
6+
*/
17
function debugLog(type, ...messages) {
28
if (config.debug[type]) {
39
console.log(`${Game.time} ${messages.join(' ')}`);

src/prototype_creep.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,12 @@ Creep.prototype.mySignController = function() {
5151
if (config.quests.enabled && this.memory.role === 'reserver' && Game.rooms[this.memory.base].terminal) {
5252
if (Math.random() < config.quests.signControllerPercentage) {
5353
const quest = {
54+
type: 'quest',
5455
id: Math.floor(Math.random() * 100000),
5556
origin: this.memory.base,
56-
end: Math.floor(Game.time / 100) * 100 + config.quests.endTime,
57-
type: 'Quest',
5857
info: 'http://tooangel.github.io/screeps',
5958
};
6059
text = JSON.stringify(quest);
61-
// Memory.quests[quest.id] = quest;
6260
this.room.debugLog('quests', `Attach quest: ${text}`);
6361
}
6462
}

src/prototype_creep_resources.js

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -52,49 +52,6 @@ Creep.prototype.checkCarryEnergyForBringingBackToStorage = function(otherCreep)
5252
return offset + this.store.getUsedCapacity() > carryPercentage * this.store.getCapacity();
5353
};
5454

55-
// return true if helper can't transfer to creep
56-
Creep.prototype.checkHelperNoTransfer = function(creep) {
57-
return creep.memory.base !== this.memory.base;
58-
};
59-
60-
Creep.prototype.findCreepWhichCanTransfer = function(creeps) {
61-
for (let i = 0; i < creeps.length; i++) {
62-
const otherCreep = creeps[i];
63-
if (!Game.creeps[otherCreep.name] || otherCreep.store.getUsedCapacity() < 50 || otherCreep.memory.recycle) {
64-
continue;
65-
}
66-
67-
if (otherCreep.memory.role === 'carry') {
68-
if (otherCreep.checkHelperNoTransfer(this)) {
69-
continue;
70-
}
71-
if (otherCreep.memory.routing.pathPos < 0) {
72-
continue;
73-
}
74-
return this.checkCarryEnergyForBringingBackToStorage(otherCreep);
75-
}
76-
continue;
77-
}
78-
return false;
79-
};
80-
81-
Creep.prototype.checkForTransfer = function(direction) {
82-
if (!direction) {
83-
this.creepLog(`checkForTransfer no direction}`);
84-
return false;
85-
}
86-
87-
const adjacentPos = this.pos.getAdjacentPosition(direction);
88-
89-
if (adjacentPos.isBorder(-2)) {
90-
this.creepLog(`checkForTransfer isBorder}`);
91-
return false;
92-
}
93-
94-
const creeps = adjacentPos.lookFor(LOOK_CREEPS);
95-
return this.findCreepWhichCanTransfer(creeps);
96-
};
97-
9855
Creep.prototype.pickupWhileMoving = function() {
9956
if (this.inBase() && this.memory.routing.pathPos < 2) {
10057
return false;
@@ -225,53 +182,6 @@ Creep.prototype.pickupEnergy = function() {
225182
return this.giveSourcersEnergy();
226183
};
227184

228-
const checkCreepForTransfer = function(otherCreep, thisCreep) {
229-
if (!Memory.creeps[otherCreep.name]) {
230-
return false;
231-
}
232-
// don't transfer to extractor, fixes full terminal with 80% energy?
233-
if (Memory.creeps[otherCreep.name].role === 'extractor') {
234-
return false;
235-
}
236-
// don't transfer to mineral, fixes full terminal with 80% energy?
237-
if (Memory.creeps[otherCreep.name].role === 'mineral') {
238-
return false;
239-
}
240-
if (!thisCreep.store[RESOURCE_ENERGY] && Memory.creeps[otherCreep.name].role === 'sourcer') {
241-
return false;
242-
}
243-
// Do we want this?
244-
if (Memory.creeps[otherCreep.name].role === 'powertransporter') {
245-
return false;
246-
}
247-
if (otherCreep.store.getFreeCapacity() === 0) {
248-
return false;
249-
}
250-
return true;
251-
};
252-
253-
Creep.prototype.transferToCreep = function(direction) {
254-
const adjacentPos = this.pos.getAdjacentPosition(direction);
255-
if (!adjacentPos.isValid()) {
256-
return false;
257-
}
258-
259-
const creeps = adjacentPos.lookFor('creep');
260-
for (let i = 0; i < creeps.length; i++) {
261-
const otherCreep = creeps[i];
262-
if (!checkCreepForTransfer(otherCreep, this) || this.checkHelperNoTransfer(otherCreep)) {
263-
continue;
264-
}
265-
for (const resource of Object.keys(this.store)) {
266-
const returnCode = this.transfer(otherCreep, resource);
267-
if (returnCode === OK) {
268-
return this.store.getUsedCapacity() * 0.5 <= otherCreep.store.getCapacity() - otherCreep.store.getUsedCapacity();
269-
}
270-
}
271-
}
272-
return false;
273-
};
274-
275185
const canStoreEnergy = function(object) {
276186
const structureTypes = [STRUCTURE_CONTROLLER, STRUCTURE_ROAD, STRUCTURE_WALL, STRUCTURE_RAMPART, STRUCTURE_OBSERVER];
277187
if (structureTypes.indexOf(object.structureType) >= 0) {

src/prototype_creep_startup_tasks.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,11 @@ Creep.prototype.getEnergyFromStorage = function() {
165165
return false;
166166
}
167167

168-
if (!this.room.memory.misplacedSpawn && this.room.isStruggling()) {
168+
if (this.room.memory.misplacedSpawn) {
169+
return false;
170+
}
171+
172+
if (this.room.isStruggeling()) {
169173
return false;
170174
}
171175

0 commit comments

Comments
 (0)