forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
creep.action.avoiding.js
89 lines (84 loc) · 3.03 KB
/
creep.action.avoiding.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
const action = new Creep.Action('avoiding');
module.exports = action;
action.lairDangerTime = 24;
action.targetRange = 0;
action.reachedRange = 0;
action.isActiveLair = function(target) {
return !(target.ticksToSpawn > action.lairDangerTime); // non-lair => true
};
action.isValidAction = function(creep){
return creep.data.destiny && creep.data.destiny.room === creep.room.name &&
(Room.isSKRoom(creep.room.name) || creep.room.situation.invasion);
};
action.isAddableAction = function(creep) {
return true;
};
action.isValidTarget = function(target, creep){
if (Task.reputation.npcOwner(target)) {
return action.isActiveLair(target);
} else if (Task.reputation.hostileOwner(target) && target.hasActiveBodyparts) {
return target.hasActiveBodyparts([ATTACK,RANGED_ATTACK]);
}
return false;
};
action.newTarget = function(creep) {
if (Room.isSKRoom(creep.pos.roomName)) {
const target = _.first(creep.room.find(FIND_STRUCTURES, {filter: function (t) {
return !_.isUndefined(t.ticksToSpawn) && action.isActiveLair(t) && creep.pos.getRangeTo(t.pos) < 15;
}}));
if (target) {
return target;
}
}
if (creep.room.situation.invasion) {
const target = _.chain(creep.room.hostiles).filter(function(target) {
return action.isValidTarget(target);
}).map(function(target) {
// TODO react to players? getStrategyHandler
let score = 0;
const range = creep.pos.getRangeTo(target);
if (creep.owner.username === "Invader") {
score = range - 51;
} else if (range < 10) {
score = range - 11;
} else {
score = 0;
}
return {target, score};
}).filter('score').sortBy('score').first().get('target').value();
if (target) {
return target;
}
}
};
action.work = function(creep) {
if (!(creep.data.safeSpot && creep.data.safeSpot.roomName)) {
const flag = creep.data.destiny && Game.flags[creep.data.destiny.targetName];
if (flag) {
creep.data.safeSpot = flag.pos;
} else {
// find the route home, move toward the exit until out of danger
const exit = _.chain(creep.room.findRoute(creep.data.homeRoom)).first().get('exit').value();
if (exit) {
creep.data.safeSpot = creep.pos.findClosestByRange(exit);
creep.data.safeSpot.roomName = creep.pos.roomName;
}
}
}
if (creep.data.safeSpot) {
if (creep.pos.getRangeTo(creep.target) < 10) {
creep.travelTo(creep.data.safeSpot);
} else {
creep.idleMove();
}
}
};
action.run = function(creep) {
if( action.isValidAction(creep) ) {
if (creep.action === action && action.isValidTarget(creep.target, creep) ||
action.isAddableAction(creep) && action.assign(creep) ) {
action.work(creep);
return true;
}
}
};