-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroom.defender.js
174 lines (151 loc) · 6.74 KB
/
room.defender.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
/*
This class handles defenses for rooms and remote mining operations
Each room will store the following in memory
-defcon: Threat level used to determine response
-numHostiles: #of hostile creeps present
-defconLength: # of ticks that the room has been at a certain threat level
-civiliansActive: Boolean value indicating wheter civilians are currently active in this room
LEVELS:
0: no hostiles present
1: 1-2 hostiles present
2: 3+ hostiles present
NOTES:
-blue flags which do not have a fourth argument specifying that they are military creeps will be turned purple until the end of the threat
TODO:
-create
-implement tick counting
-add multiple threat levels and responses
-add safemode to the max threat level
-add code to determine wheter walls have been breached
*/
var roomDefender = {
initRoom:function(room_name){
//initiallize defense variables
Memory[room_name].defense={defcon:0,
numHostiles:0,
defconLength:0,
civiliansActive:true
}
},
assessThreats:function(room_name){
//this function manages transitions between defcon levels
var room = Game.rooms[room_name];
var numHostiles = room.find(FIND_HOSTILE_CREEPS,{filter: (c)=> !_.contains(Memory.allies,c.owner.username)}).length;
// rooms with keeper lairs aren't included atm TODO, include them
var numKeeperLairs = room.find(FIND_HOSTILE_STRUCTURES,{filter: (s)=> s.structureType==STRUCTURE_KEEPER_LAIR}).length;
if (numKeeperLairs){
Memory[room_name].defense.defcon=0;
return;
}
if(numHostiles>2){
Memory[room_name].defense.defcon=2;
}else if (numHostiles>0){
Memory[room_name].defense.defcon=1;
}else{
Memory[room_name].defense.defcon=0;
}
},
run:function(room_name){
// main function
if (Memory[room_name].defense ===undefined){
this.initRoom(room_name);
}
this.assessThreats(room_name);
this.manageGates(room_name);
// execute the appropriate response
if(Memory[room_name].defense.defcon==2){
this.defcon2(room_name);
}
else if (Memory[room_name].defense.defcon==1){
this.defcon1(room_name);
}else if(Memory[room_name].defense.defcon==0){
this.defcon0(room_name);
}
},
deactivateCivilians:function(room_name){
Memory[room_name].defense.civiliansActive=false;
// query blue flags in the room
var blueFlags = Game.rooms[room_name].find(FIND_FLAGS,{filter: (f) => {return (f.color ==COLOR_BLUE); }});
// iterate over the flags and turn the civilian flags purple
for (var i in blueFlags){
var params = blueFlags[i].name.split("_");
// the fourth param indicates wheter or not a creep is military
if (params[3] !="military"){
blueFlags[i].setColor(COLOR_PURPLE);
}
}
},
activateCivilians:function(room_name){
Memory[room_name].defense.civiliansActive=true;
// query purple flags in the room
var purpleFlags = Game.rooms[room_name].find(FIND_FLAGS,{filter: (f) => {return (f.color ==COLOR_PURPLE); }});
// iterate over the flags and turn the civilian flags blue
for (var i in purpleFlags){
var params = purpleFlags[i].name.split("_");
// the fourth param indicates wheter or not a creep is military
if (params[3] !="military"){
purpleFlags[i].setColor(COLOR_BLUE);
}
}
},
defcon0:function(room_name){
// this is a peace time function
// if civilians are inactive, activate them
if(Memory[room_name].defense.civiliansActive===false){
Memory[room_name].defense.civiliansActive=true;
this.activateCivilians(room_name);// the reason for the if statement is so that we aren't calling this too often
}
},
defcon1:function(room_name){
//response to a defcon 1 threat
console.log("Defcon 1 event in room " +room_name);
Memory[room_name].defense.lastAttack= Game.time;
var room = Game.rooms[room_name];
var numSourceFlags = _.filter(Game.flags, f => (f.pos.roomName ==room_name)&&(f.color ==COLOR_RED)&&(f.secondaryColor ==COLOR_RED) ).length;
if (numSourceFlags||(room.controller!=undefined&& room.controller.reservation != undefined)){
// assuming we will have vision if this function is called, right? right.
this.defcon1Remote(room_name);
}
// place a defender flag
},
defcon1Remote:function(room_name){
// deactivate civilians, the guard should hadle the rest
if(Memory[room_name].defense.civiliansActive!=false){
Memory[room_name].defense.civiliansActive=false;
this.deactivateCivilians(room_name);
}
},
defcon2:function(room_name){
//response to a defcon 2 threat
console.log("Defcon 2 event in room " +room_name);
Memory[room_name].defense.lastAttack= Game.time;
var room = Game.rooms[room_name];
var numSourceFlags = _.filter(Game.flags, f => (f.pos.roomName ==room_name)&&(f.color ==COLOR_RED)&&(f.secondaryColor ==COLOR_RED) ).length;
if (numSourceFlags||(room.controller!=undefined&& room.controller.reservation != undefined)){
// assuming we will have vision if this function is called, right? right.
this.defcon1Remote(room_name);
}
//place a squad flag
},
defcon2Remote:function(room_name){
// TODO differentiate this function for level 1
if(Memory[room_name].defense.civiliansActive!=false){
Memory[room_name].defense.civiliansActive=false;
this.deactivateCivilians(room_name);
}
},
manageGates: function(room_name){
// make my ramparts public if there are no hostiles in the room and there is an ally present
var room = Game.rooms[room_name];
if (room.controller ===undefined||!room.controller.my){
return;
}
var numAllies = room.find(FIND_HOSTILE_CREEPS,{filter: (c)=> _.contains(Memory.allies,c.owner.username)}).length;
var ramparts= room.find(FIND_STRUCTURES, {filter: (s)=> s.structureType ==STRUCTURE_RAMPART});
var open= (numAllies && Memory[room_name].defense.defcon==0);
for (let i in ramparts){
ramparts[i].setPublic(open);
}
}
};
module.exports=roomDefender;