-
Notifications
You must be signed in to change notification settings - Fork 40
/
role_prototype.js
214 lines (176 loc) · 4.43 KB
/
role_prototype.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
var proto = {
/**
* The creep for this role
*
* @type creep
*/
creep: null,
/**
* Set the creep for this role
*
* @param {Creep} creep
*/
setCreep: function(creep)
{
this.creep = creep;
return this;
},
run: function()
{
if(this.creep.memory.onSpawned == undefined) {
this.onSpawn();
this.creep.memory.onSpawned = true;
}
this.action(this.creep);
if(this.creep.ticksToLive == 1)
this.beforeAge();
},
handleEvents: function()
{
if(this.creep.memory.onSpawned == undefined) {
this.onSpawnStart();
this.onSpawn();
this.creep.memory.onSpawned = true;
}
if(this.creep.memory.onSpawnEnd == undefined && !this.creep.spawning) {
this.onSpawnEnd();
this.creep.memory.onSpawnEnd = true;
}
},
getParts: function() {
var _ = require('lodash');
var extensions = Game.getRoom('1-1').find(Game.MY_STRUCTURES, {
filter: function(structure)
{
return (structure.structureType == Game.STRUCTURE_EXTENSION && structure.energy >= 200);
}
}).length;
var parts = _.cloneDeep(this.parts);
if(typeof parts[0] != "object")
return this.parts;
parts.reverse();
for(var i in parts)
{
if((parts[i].length - 5) <= extensions) {
return parts[i];
}
}
},
action: function() { },
onSpawn: function() { },
onSpawnStart: function() { },
onSpawnEnd: function() { },
beforeAge: function() { },
/**
* All credit goes to Djinni
* @url https://bitbucket.org/Djinni/screeps/
*/
rest: function(civilian)
{
var creep = this.creep;
var distance = 4;
var restTarget = creep.pos.findNearest(Game.MY_SPAWNS);
if(!civilian) {
var flags = Game.flags;
for (var i in flags) {
var flag = flags[i];
if (creep.pos.inRangeTo(flag, distance) || creep.pos.findPathTo(flag).length > 0) {
restTarget = flag;
break;
}
}
}
// var flag = Game.flags['Flag1'];
// if(flag !== undefined && civilian !== true)
// restTarget = flag;
//
// var flag2 = Game.flags['Flag2'];
// if(flag !== undefined && civilian !== true && !creep.pos.inRangeTo(flag, distance) && !creep.pos.findPathTo(flag).length)
// restTarget = flag2;
if (creep.getActiveBodyparts(Game.HEAL)) {
// distance = distance - 1;
}
else if (creep.getActiveBodyparts(Game.RANGED_ATTACK)) {
// distance = distance - 1;
}
if (creep.pos.findPathTo(restTarget).length > distance) {
creep.moveTo(restTarget);
}
},
/**
* All credit goes to Djinni
* @url https://bitbucket.org/Djinni/screeps/
*/
rangedAttack: function(target) {
var creep = this.creep;
if(!target)
target = creep.pos.findNearest(Game.HOSTILE_CREEPS);
if(target) {
if (target.pos.inRangeTo(creep.pos, 3) ) {
creep.rangedAttack(target);
return target;
}
}
return null;
},
keepAwayFromEnemies: function()
{
var creep = this.creep;
var target = creep.pos.findNearest(Game.HOSTILE_CREEPS);
if(target !== null && target.pos.inRangeTo(creep.pos, 3))
creep.moveTo(creep.pos.x + creep.pos.x - target.pos.x, creep.pos.y + creep.pos.y - target.pos.y );
},
/**
* All credit goes to Djinni
* @url https://bitbucket.org/Djinni/screeps/
*/
kite: function(target) {
var creep = this.creep;
if (target.pos.inRangeTo(creep.pos, 2)) {
creep.moveTo(creep.pos.x + creep.pos.x - target.pos.x, creep.pos.y + creep.pos.y - target.pos.y );
return true;
} else if (target.pos.inRangeTo(creep.pos, 3)) {
return true;
}
else {
creep.moveTo(target);
return true;
}
return false;
},
getRangedTarget: function()
{
var creep = this.creep;
var closeArchers = creep.pos.findNearest(Game.HOSTILE_CREEPS, {
filter: function(enemy)
{
return enemy.getActiveBodyparts(Game.RANGED_ATTACK) > 0
&& creep.pos.inRangeTo(enemy, 3);
}
});
if(closeArchers != null)
return closeArchers;
var closeMobileMelee = creep.pos.findNearest(Game.HOSTILE_CREEPS, {
filter: function(enemy)
{
return enemy.getActiveBodyparts(Game.ATTACK) > 0
&& enemy.getActiveBodyparts(Game.MOVE) > 0
&& creep.pos.inRangeTo(enemy, 3);
}
});
if(closeMobileMelee != null)
return closeMobileMelee;
var closeHealer = creep.pos.findNearest(Game.HOSTILE_CREEPS, {
filter: function(enemy)
{
return enemy.getActiveBodyparts(Game.HEAL) > 0
&& enemy.getActiveBodyparts(Game.MOVE) > 0
&& creep.pos.inRangeTo(enemy, 3);
}
});
if(closeHealer != null)
return closeHealer;
return creep.pos.findNearest(Game.HOSTILE_CREEPS);
}
};
module.exports = proto;