-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathprocessor.ts
199 lines (183 loc) · 6.89 KB
/
processor.ts
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import type { Direction } from 'xxscreeps/game/position';
import type { PartType } from 'xxscreeps/mods/creep/creep';
import type { Resource } from 'xxscreeps/mods/resource/resource';
import type { ResourceType, WithStore } from 'xxscreeps/mods/resource/store';
import type { RoomObject } from 'xxscreeps/game/object';
import type { Structure } from 'xxscreeps/mods/structure/structure';
import * as C from 'xxscreeps/game/constants';
import * as Fn from 'xxscreeps/utility/functional';
import { Game } from 'xxscreeps/game';
import { Creep } from 'xxscreeps/mods/creep/creep';
// eslint-disable-next-line @typescript-eslint/no-duplicate-imports
import * as CreepLib from 'xxscreeps/mods/creep/creep';
import { RoomPosition, generateRoomName, parseRoomName } from 'xxscreeps/game/position';
import { lookForStructureAt } from 'xxscreeps/mods/structure/structure';
import { isBorder } from 'xxscreeps/game/terrain';
import { writeRoomObject } from 'xxscreeps/engine/db/room';
import { typedArrayToString } from 'xxscreeps/utility/string';
import { registerIntentProcessor, registerObjectPreTickProcessor, registerObjectTickProcessor } from 'xxscreeps/engine/processor';
import * as Movement from 'xxscreeps/engine/processor/movement';
// eslint-disable-next-line no-duplicate-imports
import * as ResourceIntent from 'xxscreeps/mods/resource/processor/resource';
import { flushActionLog } from 'xxscreeps/engine/processor/object';
declare module 'xxscreeps/engine/processor' {
interface Intent { creep: typeof intents }
}
const intents = [
registerIntentProcessor(Creep, 'drop', (creep, context, resourceType: ResourceType, amount: number) => {
if (CreepLib.checkDrop(creep, resourceType, amount) === C.OK) {
creep.store['#subtract'](resourceType, amount);
ResourceIntent.drop(creep.pos, resourceType, amount);
context.didUpdate();
}
}),
registerIntentProcessor(Creep, 'move', (creep, context, direction: Direction) => {
if (CreepLib.checkMove(creep, direction) === C.OK) {
const power = calculateWeight(creep);
Movement.add(creep, power, direction);
context.didUpdate();
}
}),
registerIntentProcessor(Creep, 'pickup', (creep, context, id: string) => {
const resource = Game.getObjectById<Resource>(id)!;
if (CreepLib.checkPickup(creep, resource) === C.OK) {
const amount = Math.min(creep.store.getFreeCapacity(resource.resourceType), resource.amount);
creep.store['#add'](resource.resourceType, amount);
resource.amount -= amount;
context.didUpdate();
}
}),
registerIntentProcessor(Creep, 'say', (creep, context, message: string, isPublic: boolean) => {
if (CreepLib.checkCommon(creep) === C.OK) {
creep['#saying'] = {
isPublic,
message: `${message}`.substr(0, 10),
time: Game.time,
};
context.didUpdate();
}
}),
registerIntentProcessor(Creep, 'suicide', (creep, context) => {
if (creep.my) {
creep.room['#removeObject'](creep);
context.didUpdate();
}
}),
registerIntentProcessor(Creep, 'transfer', (creep, context, id: string, resourceType: ResourceType, amount: number) => {
const target = Game.getObjectById<RoomObject & WithStore>(id)!;
if (CreepLib.checkTransfer(creep, target, resourceType, amount) === C.OK) {
creep.store['#subtract'](resourceType, amount);
target.store['#add'](resourceType, amount);
context.didUpdate();
}
}),
registerIntentProcessor(Creep, 'withdraw', (creep, context, id: string, resourceType: ResourceType, amount: number) => {
const target = Game.getObjectById<Structure & WithStore>(id)!;
if (CreepLib.checkWithdraw(creep, target, resourceType, amount) === C.OK) {
target.store['#subtract'](resourceType, amount);
creep.store['#add'](resourceType, amount);
context.didUpdate();
}
}),
];
registerObjectPreTickProcessor(Creep, (creep, context) => {
const kRetainActionsTime = 10;
const timeLimit = Game.time - kRetainActionsTime;
flushActionLog(creep['#actionLog'], context);
// Remove `saying`
const saying = creep['#saying'];
if (saying) {
if (saying.time <= timeLimit) {
creep['#saying'] = undefined;
context.didUpdate();
} else {
context.wakeAt(saying.time + kRetainActionsTime);
}
}
});
registerObjectTickProcessor(Creep, (creep, context) => {
// Check creep death
if (
(Game.time >= creep['#ageTime'] && creep['#ageTime'] !== 0) ||
creep.hits <= 0
) {
for (const [ resourceType, amount ] of creep.store['#entries']()) {
ResourceIntent.drop(creep.pos, resourceType, amount);
}
creep.room['#removeObject'](creep);
context.didUpdate();
return;
} else if (creep.hits > creep.hitsMax) {
creep.hits = creep.hitsMax;
context.didUpdate();
}
// Dispatch movements
const nextPosition = Movement.get(creep);
if (nextPosition) {
// Move the creep
creep.room['#moveObject'](creep, nextPosition);
// Calculate base fatigue from plain/road/swamp
const fatigue = (() => {
const road = lookForStructureAt(creep.room, nextPosition, C.STRUCTURE_ROAD);
if (road) {
// Update road decay
road['#nextDecayTime'] -= C.ROAD_WEAROUT * creep.body.length;
return 1;
}
const terrain = creep.room.getTerrain().get(nextPosition.x, nextPosition.y);
if (terrain === C.TERRAIN_MASK_SWAMP) {
return 10;
} else {
return 2;
}
})();
// Update fatigue
creep.fatigue = Math.max(0, calculateWeight(creep) * fatigue);
context.didUpdate();
}
if (creep.fatigue > 0) {
// Reduce fatigue
creep.fatigue -= Math.min(creep.fatigue, calculatePower(creep, C.MOVE, 2));
context.didUpdate();
}
// Move creep to next room
if (isBorder(creep.pos.x, creep.pos.y) && creep['#user'].length > 2) {
const { rx, ry } = parseRoomName(creep.pos.roomName);
const next = function() {
if (creep.pos.x === 0) {
return new RoomPosition(49, creep.pos.y, generateRoomName(rx - 1, ry));
} else if (creep.pos.x === 49) {
return new RoomPosition(0, creep.pos.y, generateRoomName(rx + 1, ry));
} else if (creep.pos.y === 0) {
return new RoomPosition(creep.pos.x, 49, generateRoomName(rx, ry - 1));
} else {
return new RoomPosition(creep.pos.x, 0, generateRoomName(rx, ry + 1));
}
}();
creep.room['#removeObject'](creep);
// Update `creep.pos` for the import command but set it back so that `#flushObjects` can safely
// update the internal indices.
const oldPos = creep.pos;
creep.pos = next;
const importPayload = writeRoomObject(creep);
creep.pos = oldPos;
context.sendRoomIntent(next.roomName, 'import', typedArrayToString(importPayload));
context.didUpdate();
} else {
context.wakeAt(creep['#ageTime']);
}
});
export function calculatePower(creep: Creep, part: PartType, power: number) {
return Fn.accumulate(creep.body, bodyPart => {
if (bodyPart.type === part && bodyPart.hits > 0) {
return power;
}
return 0;
});
}
export function calculateWeight(creep: Creep) {
let weight = Fn.accumulate(creep.body, part =>
part.type === C.CARRY || part.type === C.MOVE ? 0 : 1);
weight += Math.ceil(creep.carry.getUsedCapacity() / C.CARRY_CAPACITY);
return weight;
}