-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprototype.creep.soldier.js
200 lines (192 loc) · 6.38 KB
/
prototype.creep.soldier.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
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
200
/**
* Extend creep
*/
module.exports = function () {
/**
* Get rampart index
* @return {string|null}
*/
Creep.prototype.getRampartIndex = function () {
if (typeof this.memory.rampart_index == 'undefined') return null;
return this.memory.rampart_index;
};
/**
* Set rampart index
* @param {string|null} rampart_index
*/
Creep.prototype.setRampartIndex = function (rampart_index) {
if (this.memory.rampart_index != rampart_index) {
this.memory.rampart_index = rampart_index;
}
};
/**
* Assigns a rampart to the worker
* @returns {string|null}
*/
Creep.prototype.assignRampart = function () {
// initialize variables
var ramparts = this.room.find(FIND_MY_STRUCTURES, {
filter: (structure) => {
return structure.structureType == STRUCTURE_RAMPART
}
});
var rampartsLength = ramparts.length;
var soldiers = this.room.getAllSoldiers();
var soldiersLength = soldiers.length;
// get current soldiers ramparts
var currentSoldiersRamparts = [];
for (let i = 0; i < soldiersLength; i++) {
if (soldiers[i].getSourceIndex() !== null) {
currentSoldiersRamparts.push(soldiers[i].getSourceIndex());
}
}
// get an empty rampart
for (let i = 0; i < rampartsLength; i++) {
if (currentSoldiersRamparts.indexOf(ramparts[i].id) === -1) {
this.setSourceIndex(ramparts[i].id);
return ramparts[i].id;
}
}
return null;
};
/**
* Revoke a worker rampart.
*/
Creep.prototype.revokeRampart = function () {
this.setRampartIndex(null);
};
/**
* Defend room
*/
Creep.prototype.setToDefendRoom = function () {
if (this.getRampartIndex() === null) {
if (this.assignRampart() === null) {
this.setToAttackNearestTarget();
}
}
var rampart = Game.getObjectById(this.getRampartIndex());
if (rampart !== null) {
if (!rampart.pos.isEqualTo(this.pos)) {
this.moveTo(rampart);
} else {
var target = this.pos.findClosestByRange(FIND_HOSTILE_CREEPS);
if (target !== null) {
switch (this.memory.archetype) {
case 'attacker':
this.attack(target);
break;
case 'defender':
this.rangedAttack(target);
break;
}
}
}
} else {
// rampart does not longer exist
this.revokeRampart();
}
};
/**
* Attack room
*/
Creep.prototype.setToAttackRoom = function (roomName) {
if (this.room.name != roomName) {
var exitDir = this.room.findExitTo(roomName);
var exit = this.pos.findClosestByPath(exitDir);
this.moveTo(exit, {reusePath: 0});
} else {
this.setToAttackNearestTarget();
}
};
/**
* Attack nearest hostile creep
*/
Creep.prototype.setToAttackNearestTarget = function () {
var target = this.pos.findClosestByPath(FIND_HOSTILE_CREEPS);
if (target !== null) {
this.moveTo(target);
switch (this.memory.archetype) {
case 'attacker':
this.attack(target);
break;
case 'defender':
this.rangedAttack(target);
break;
}
} else {
var target = this.pos.findClosestByPath(FIND_HOSTILE_STRUCTURES, {
filter: (structure) => {
return structure.structureType != STRUCTURE_STORAGE && structure.structureType != STRUCTURE_CONTROLLER
}
});
if (target !== false) {
switch (this.memory.archetype) {
case 'attacker':
if (this.attack(target) == ERR_NOT_IN_RANGE) {
this.moveTo(target, {reusePath: 0});
}
break;
case 'defender':
if (this.rangedAttack(target) == ERR_NOT_IN_RANGE) {
this.moveTo(target, {reusePath: 0});
}
break;
}
}
}
};
/**
* Heal most damaged attacker or follow the nearest attacker
*/
Creep.prototype.setToHealMostDamagedAttacker = function () {
let soldiers = this.room.find(FIND_MY_CREEPS, {
filter: (creep) => {
return creep.memory.role == 'soldier' && creep.hits < creep.hitsMax
}
});
if (soldiers.length > 0) {
soldiers.sort(function (a, b) {
return a.hits - b.hits;
});
this.moveTo(soldiers[0], {reusePath: 0});
if (this.pos.isNearTo(soldiers[0])) {
this.heal(soldiers[0]);
} else {
this.rangedHeal(soldiers[0]);
}
} else {
let soldiers = this.room.find(FIND_MY_CREEPS, {
filter: (creep) => {
return creep.memory.role == 'soldier' && creep.memory.archetype == 'attacker'
}
});
if (soldiers.length > 0) {
this.moveTo(soldiers[0], {reusePath: 0});
}
}
};
/**
* Scouting Mission
*/
Creep.prototype.setToScout = function () {
/**
* Choose room to go.
* Move to room.
* Avoid other creeps.
* Maybe can get stuck so check if it's possible to go to the room.
* If we like the room claim it.
*/
if (this.room.name != 'E67N51') {
var exitDir = this.room.findExitTo('E67N51');
var exit = this.pos.findClosestByRange(exitDir);
this.moveTo(exit);
} else {
var result = this.claimController(this.room.controller);
if (result == OK) {
} else if (result == ERR_NOT_IN_RANGE) {
this.moveTo(this.room.controller);
} else {
}
}
};
};