-
Notifications
You must be signed in to change notification settings - Fork 12
/
task_withdraw.ts
63 lines (53 loc) · 1.85 KB
/
task_withdraw.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
/* This is the withdrawal task for non-energy resources. */
import {Task} from '../Task';
import {EnergyStructure, isEnergyStructure, isStoreStructure, StoreStructure} from '../utilities/helpers';
export type withdrawTargetType =
EnergyStructure
| StoreStructure
| StructureLab
| StructureNuker
| StructurePowerSpawn
| Tombstone;
export class TaskWithdraw extends Task {
static taskName = 'withdraw';
target: withdrawTargetType;
data: {
resourceType: ResourceConstant,
amount: number | undefined,
};
constructor(target: withdrawTargetType,
resourceType: ResourceConstant = RESOURCE_ENERGY,
amount: number | undefined = undefined,
options = {} as TaskOptions) {
super(TaskWithdraw.taskName, target, options);
// Settings
this.settings.oneShot = true;
this.data.resourceType = resourceType;
this.data.amount = amount;
}
isValidTask() {
let amount = this.data.amount || 1;
return (_.sum(this.creep.carry) <= this.creep.carryCapacity - amount);
}
isValidTarget() {
let amount = this.data.amount || 1;
let target = this.target;
if (target instanceof Tombstone || isStoreStructure(target)) {
return (target.store[this.data.resourceType] || 0) >= amount;
} else if (isEnergyStructure(target) && this.data.resourceType == RESOURCE_ENERGY) {
return target.energy >= amount;
} else {
if (target instanceof StructureLab) {
return this.data.resourceType == target.mineralType && target.mineralAmount >= amount;
} else if (target instanceof StructureNuker) {
return this.data.resourceType == RESOURCE_GHODIUM && target.ghodium >= amount;
} else if (target instanceof StructurePowerSpawn) {
return this.data.resourceType == RESOURCE_POWER && target.power >= amount;
}
}
return false;
}
work() {
return this.creep.withdraw(this.target, this.data.resourceType, this.data.amount);
}
}