5 |
*/ |
*/ |
6 |
|
|
7 |
#include "missions.h" |
#include "missions.h" |
|
#include "pod.h" |
|
|
#include "../map/map.h" |
|
8 |
|
|
9 |
// a pointer to the map. |
|
10 |
Map* Mission::map; |
|
11 |
|
|
12 |
|
|
13 |
// MISSION - Base class for all missions --------\/---------- |
// MISSION - Base class for all missions --------\/---------- |
14 |
Mission::Mission() { |
Mission::Mission() { m_time=0; } |
|
m_time=0; |
|
|
} |
|
15 |
|
|
16 |
Mission::~Mission(){ |
Mission::~Mission(){ |
17 |
// TODO: shuffle the mission queue when this one is done. |
// TODO: shuffle the mission queue when this one is done. |
19 |
// TODO: set mission to IDLE if no more missions in queue |
// TODO: set mission to IDLE if no more missions in queue |
20 |
} |
} |
21 |
|
|
22 |
// map pointer setter - used for all missions |
mission_type Mission::GetType() { return m_type; } |
23 |
void Mission::SetMapPointer(Map &incoming_map){ |
int Mission::GetTime() { return m_time; } |
24 |
map = &incoming_map; |
void Mission::ResetTime() { m_time = 0; } |
25 |
|
void Mission::IncTime() { |
26 |
|
m_time++; |
27 |
|
// check if we timed out the mission |
28 |
|
if (timeout - m_time == 0) { pod->RemoveMission(0); } |
29 |
} |
} |
30 |
|
|
31 |
mission_type Mission::GetType() { |
// VIRTUAL INTERFACE FUNCTIONS |
32 |
return m_type; |
void Mission::SetLoc(int x, int y) { loc_y = y; loc_x = x; } |
33 |
|
int Mission::GetLocX() { return loc_x; } |
34 |
|
int Mission::GetLocY() { return loc_y; } |
35 |
|
void Mission::SetPersist(bool p) { persist = p; } |
36 |
|
bool Mission::GetPersist() { return persist; } |
37 |
|
void Mission::SetTimeout(int x) { timeout = x; } |
38 |
|
int Mission::GetTimeout() { return timeout; } |
39 |
|
int Mission::GetTimeLeft() { return timeout - m_time; } |
40 |
|
void Mission::SetRad(int x) { rad = x; } |
41 |
|
int Mission::GetRad() { return rad; } |
42 |
|
void Mission::SetTarget(Pod* p) { target = p; } |
43 |
|
Pod* Mission::GetTarget() { return target; } |
44 |
|
void Mission::SetTakeover(bool x) { takeover = x; } |
45 |
|
bool Mission::GetTakeover() { return takeover; } |
46 |
|
void Mission::SetPursue(bool p) { pursue = p; } |
47 |
|
bool Mission::GetPursue() { return pursue; } |
48 |
|
|
49 |
|
|
50 |
|
// STATIC FUNCTIONS |
51 |
|
mission_type Mission::TranslateMission(const char* m) { |
52 |
|
if ( strcmp(m, "Patrol")==0 ) { return MISSION_PATROL;} |
53 |
|
else if ( strcmp(m, "Idle")==0 ) { return MISSION_IDLE;} |
54 |
|
else if ( strcmp(m, "Halt")==0 ) { return MISSION_HALT;} |
55 |
|
else if ( strcmp(m, "Move")==0 ) { return MISSION_MOVE;} |
56 |
|
else if ( strcmp(m, "Intercept")==0 ) { return MISSION_INTERCEPT;} |
57 |
|
else if ( strcmp(m, "Defend")==0 ) { return MISSION_DEFEND;} |
58 |
|
else if ( strcmp(m, "Follow")==0 ) { return MISSION_FOLLOW;} |
59 |
|
else if ( strcmp(m, "Harvest")==0 ) { return MISSION_HARVEST;} |
60 |
|
else if ( strcmp(m, "Capture Flag")==0 ) { return MISSION_FLAG;} |
61 |
|
else if ( strcmp(m, "Scout")==0 ) { return MISSION_SCOUT;} |
62 |
|
else if ( strcmp(m, "Explore")==0 ) { return MISSION_EXPLORE;} |
63 |
|
else if ( strcmp(m, "Berserk")==0 ) { return MISSION_BERSERK;} |
64 |
|
else {return MISSION_IDLE; } |
65 |
} |
} |
66 |
|
|
|
int Mission::GetTime() { |
|
|
return m_time; |
|
|
} |
|
67 |
|
|
68 |
void Mission::IncTime() { |
std::string Mission::TranslateMission(mission_type m, bool active) { |
69 |
m_time++; |
if (active) { |
70 |
|
switch (m) { |
71 |
|
case MISSION_PATROL: return "On Patrol"; break; |
72 |
|
case MISSION_IDLE: return "Idled"; break; |
73 |
|
case MISSION_HALT: return "Halting"; break; |
74 |
|
case MISSION_MOVE: return "Moving"; break; |
75 |
|
case MISSION_INTERCEPT: return "Intercepting"; break; |
76 |
|
case MISSION_DEFEND: return "Defending"; break; |
77 |
|
case MISSION_FOLLOW: return "Following"; break; |
78 |
|
case MISSION_HARVEST: return "Harvesting"; break; |
79 |
|
case MISSION_FLAG: return "Capturing Flag"; break; |
80 |
|
case MISSION_SCOUT: return "Scouting"; break; |
81 |
|
case MISSION_EXPLORE: return "Exploring"; break; |
82 |
|
case MISSION_BERSERK: return "Gone Berserk!"; break; |
83 |
|
default: return "???"; |
84 |
|
} |
85 |
|
} |
86 |
|
else { |
87 |
|
switch (m) { |
88 |
|
case MISSION_PATROL: return "Patrol"; break; |
89 |
|
case MISSION_IDLE: return "Idle"; break; |
90 |
|
case MISSION_HALT: return "Halt"; break; |
91 |
|
case MISSION_MOVE: return "Move"; break; |
92 |
|
case MISSION_INTERCEPT: return "Intercept"; break; |
93 |
|
case MISSION_DEFEND: return "Defend"; break; |
94 |
|
case MISSION_FOLLOW: return "Follow"; break; |
95 |
|
case MISSION_HARVEST: return "Harvest"; break; |
96 |
|
case MISSION_FLAG: return "Capture Flag"; break; |
97 |
|
case MISSION_SCOUT: return "Scout"; break; |
98 |
|
case MISSION_EXPLORE: return "Explore"; break; |
99 |
|
case MISSION_BERSERK: return "Berserk"; break; |
100 |
|
default: return "???"; |
101 |
|
} |
102 |
|
} |
103 |
} |
} |
104 |
|
|
105 |
void Mission::ResetTime() { |
|
|
m_time = 0; |
|
|
} |
|
106 |
//------------------/\------------------------------------ |
//------------------/\------------------------------------ |
107 |
|
|
108 |
|
|
112 |
|
|
113 |
// PATROL ------------------\/---------------------------- |
// PATROL ------------------\/---------------------------- |
114 |
|
|
115 |
// *structors |
MissionPatrol::MissionPatrol(Pod* in_pod, int x, int y, int r, bool p) { |
116 |
Patrol::Patrol(Pod &in_pod, int x, int y, int r, bool p) { |
pod = in_pod; |
117 |
pod = &in_pod; |
loc_x = x; |
118 |
base_x = x; |
loc_y = y; |
|
base_y = y; |
|
119 |
rad = r; |
rad = r; |
120 |
persue = p; |
pursue = p; |
121 |
m_type = PATROL; |
timeout = MAX_TIMEOUT; |
122 |
|
m_type = MISSION_PATROL; |
123 |
|
|
124 |
//set avoidence=0 for the duration of the PATROL mission |
//set avoidence=0 for the duration of the PATROL mission |
125 |
old_avoid = pod->GetAvoid(); |
old_avoid = pod->GetAvoid(); |
126 |
pod->SetAvoid(0); |
pod->SetAvoid(0); |
127 |
} |
} |
128 |
|
|
129 |
Patrol::~Patrol() { |
MissionPatrol::~MissionPatrol() { |
130 |
// put the avoidence back to what it was before the mission started |
// put the avoidence back to what it was before the mission started |
131 |
pod->SetAvoid(old_avoid); |
pod->SetAvoid(old_avoid); |
132 |
} |
} |
|
|
|
|
|
|
|
|
|
|
//TODO: GET FUNCTIONS |
|
|
|
|
|
|
|
|
// SET FUNCTIONS |
|
|
void Patrol::SetPersue(bool x) { |
|
|
persue = x; |
|
|
} |
|
|
|
|
|
void Patrol::SetRad(int x) { |
|
|
rad = x; |
|
|
} |
|
|
|
|
|
void Patrol::SetBase(int x, int y) { |
|
|
base_x = x; |
|
|
base_y = y; |
|
|
} |
|
|
|
|
|
|
|
|
bool Patrol::GetPersue() { return persue; } |
|
|
int Patrol::GetRad() { return rad; } |
|
|
int Patrol::GetBaseX() { return base_x; } |
|
|
int Patrol::GetBaseY() { return base_y; } |
|
|
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
// MAIN EXECUTE FUNCTION |
// MAIN EXECUTE FUNCTION |
137 |
void Patrol::Exe() { |
void MissionPatrol::Exe() { |
138 |
// any intruders in the patrol area? |
// any intruders in the patrol area? |
139 |
// NOTE: this uses square radius, not true radius. |
// NOTE: this uses square radius, not true radius. |
140 |
// TODO: use spiral search, not square kitty-korner search to spot *closest* enemies |
// TODO: use spiral search, not square kitty-korner search to spot *closest* enemies |
145 |
Pod* enemy; |
Pod* enemy; |
146 |
for (int i=-rad; i <= rad; i++) { |
for (int i=-rad; i <= rad; i++) { |
147 |
for (int j=-rad; j <= rad; j++) { |
for (int j=-rad; j <= rad; j++) { |
148 |
if ( map->IsValid(base_x + i, base_y + j) == 0 ) {continue;} |
if ( themap->IsValid(loc_x + i, loc_y + j) == 0 ) {continue;} |
149 |
enemy = map->GetOccu(base_x + i, base_y + j); |
enemy = themap->GetOccu(loc_x + i, loc_y + j); |
150 |
if (enemy != NULL) { // if it's occupied |
if (enemy != NULL) { // if it's occupied |
151 |
if (enemy->GetAlign() != pod->GetAlign()) { // if he's a bad guy |
if (enemy->GetAlign() != pod->GetAlign()) { // if he's a bad guy |
152 |
// set course for intercept! |
// set course for intercept! |
153 |
// if there's no path, break: |
// if there's no path, break: |
154 |
if (pod->CreatePath(pod->GetXPos(), pod->GetYPos(), pod->GetXPos() + i, pod->GetYPos() + j) == 0) { |
if (pod->CreatePath(pod->GetXPos(), pod->GetYPos(), loc_x + i, loc_y + j) == 0) { |
155 |
cout << "[" << pod->GetName() << "]: NO PATH - CAN'T ATTACK\n"; |
cout << "[" << pod->GetName() << "]: NO PATH - CAN'T ATTACK\n"; |
|
//goto breakloop; |
|
156 |
// just continue - there may be other enemies we can get |
// just continue - there may be other enemies we can get |
157 |
continue; |
continue; |
158 |
} |
} |
159 |
// otherwise, intercept |
// otherwise, intercept |
160 |
else { |
else { |
161 |
pod->Walk(); |
if (pod->Walk() == -1) {return;} // got killed - ABORT |
162 |
walked = 1; |
walked = 1; |
163 |
goto breakloop; |
goto breakloop; |
164 |
} |
} |
167 |
} |
} |
168 |
} |
} |
169 |
|
|
|
/* USE THIS FOR PURSUING! HAPPY BUG - CHECKS RADIUS FROM CURRENT POSITION, NOT BASE POINT |
|
|
for (int i=-rad; i <= rad; i++) { |
|
|
for (int j=-rad; j <= rad; j++) { |
|
|
if ( map->IsValid(pod->GetXPos() + i, pod->GetYPos() + j) == 0 ) {continue;} |
|
|
enemy = map->GetOccu(pod->GetXPos() + i, pod->GetYPos() + j); |
|
|
if (enemy != NULL) { // if it's occupied |
|
|
if (enemy->GetAlign() != pod->GetAlign()) { // if he's a bad guy |
|
|
//set course for intercept! |
|
|
if (pod->CreatePath(pod->GetXPos(), pod->GetYPos(), pod->GetXPos() + i, pod->GetYPos() + j) == 0) { |
|
|
cout << "[" << pod->GetName() << "]: NO PATH - CAN'T ATTACK\n"; |
|
|
goto breakloop; |
|
|
} |
|
|
pod->Walk(); |
|
|
walked = 1; |
|
|
goto breakloop; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
*/ |
|
|
|
|
170 |
breakloop: |
breakloop: |
171 |
|
|
172 |
if(!walked) { // don't reset position if we are in the middle of pursuing someone! |
if(!walked) { // don't reset position if we are in the middle of pursuing someone! |
173 |
// am i where i'm supposed to be? |
// am i where i'm supposed to be? |
174 |
if ( !(pod->GetXPos() == base_x && pod->GetYPos() == base_y) ) { |
if ( !(pod->GetXPos() == loc_x && pod->GetYPos() == loc_y) ) { |
175 |
cout << "[" << pod->GetName() << "] I'm out of place!\n Am: " << pod->GetXPos() << " Should be: " << base_x << "\n Am: " << pod->GetYPos() << " Should be: " << base_y << endl; |
cout << "[" << pod->GetName() << "] I'm out of place!\n Am: " << pod->GetXPos() << " Should be: " << loc_x << "\n Am: " << pod->GetYPos() << " Should be: " << loc_y << endl; |
176 |
if (pod->CreatePath(pod->GetXPos(), pod->GetYPos(), base_x, base_y) == 0) { |
if (pod->CreatePath(pod->GetXPos(), pod->GetYPos(), loc_x, loc_y) == 0) { |
177 |
cout << "[" << pod->GetName() << "]: NO PATH - CAN'T GET BACK TO BASE POINT NOW\n"; |
cout << "[" << pod->GetName() << "]: NO PATH - CAN'T GET BACK TO BASE POINT NOW\n"; |
178 |
} |
} |
179 |
else { pod->Walk(); } |
else { if (pod->Walk() == -1) {return;} } // got killed - ABORT |
180 |
} |
} |
181 |
} |
} |
182 |
|
|
185 |
// have a "chasing" variable -> ptr to last guy we tried to run down |
// have a "chasing" variable -> ptr to last guy we tried to run down |
186 |
|
|
187 |
IncTime(); // turn is finished, so increase mission time / experience |
IncTime(); // turn is finished, so increase mission time / experience |
|
|
|
188 |
} |
} |
189 |
//------------------/\------------------------------------ |
//------------------/\------------------------------------ |
190 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
// MOVE ------------------\/---------------------------- |
198 |
|
|
199 |
|
MissionMove::MissionMove(Pod* in_pod) { |
200 |
|
pod = in_pod; |
201 |
|
loc_x = pod->GetXPos(); |
202 |
|
loc_y = pod->GetXPos(); |
203 |
|
timeout = MAX_TIMEOUT; |
204 |
|
m_type = MISSION_MOVE; |
205 |
|
} |
206 |
|
|
207 |
|
MissionMove::~MissionMove() {} |
208 |
|
|
209 |
|
void MissionMove::Exe() { |
210 |
|
// set a course - if there's no path, break: |
211 |
|
if (pod->CreatePath(pod->GetXPos(), pod->GetYPos(), loc_x, loc_y) == 0) { |
212 |
|
cout << "[" << pod->GetName() << "]: NO PATH\n"; |
213 |
|
// TODO: try to at least get close if persist is on. |
214 |
|
// for now, just abort mission |
215 |
|
pod->RemoveMission(0); // mission is over |
216 |
|
} |
217 |
|
// otherwise, go |
218 |
|
else { |
219 |
|
// if we walk and hit the goal: |
220 |
|
int result = pod->Walk(); |
221 |
|
if ( result == 1 ) { |
222 |
|
pod->RemoveMission(0); // mission is over |
223 |
|
} |
224 |
|
else if (result == -1) {return;} // got killed - ABORT |
225 |
|
} |
226 |
|
|
227 |
|
IncTime(); // turn is finished, so increase mission time / experience |
228 |
|
} |
229 |
|
|
230 |
|
// MOVE ------------------\/---------------------------- |
231 |
|
|
232 |
|
|
233 |
|
|
234 |
|
|
235 |
|
|
236 |
|
|
237 |
|
|
238 |
// IDLE ------------------\/---------------------------- |
// IDLE ------------------\/---------------------------- |
239 |
|
|
240 |
// *structors |
MissionIdle::MissionIdle(Pod* in_pod) { |
241 |
Idle::Idle(Pod &in_pod) { |
pod = in_pod; |
242 |
pod = &in_pod; |
timeout = MAX_TIMEOUT; |
243 |
m_type = IDLE; |
m_type = MISSION_IDLE; |
244 |
} |
} |
245 |
|
|
246 |
Idle::~Idle() { /* do nothing */ } |
MissionIdle::~MissionIdle() { /* do nothing */ } |
247 |
|
|
248 |
void Idle::Exe() { /* do nothing */ } |
void MissionIdle::Exe() { /* do nothing */ } |
249 |
// TODO: Idle will do more in the future besides nothing :-) |
// TODO: Idle will do more in the future besides nothing :-) |
250 |
|
|
251 |
//------------------/\------------------------------------ |
//------------------/\------------------------------------ |
252 |
|
|
253 |
|
|
254 |
|
|
255 |
|
|
256 |
|
|
257 |
|
|
258 |
|
// HALT ------------------\/---------------------------- |
259 |
|
|
260 |
|
MissionHalt::MissionHalt(Pod* in_pod) { |
261 |
|
pod = in_pod; |
262 |
|
timeout = MAX_TIMEOUT; |
263 |
|
m_type = MISSION_HALT; |
264 |
|
} |
265 |
|
|
266 |
|
MissionHalt::~MissionHalt() { /* do nothing */ } |
267 |
|
|
268 |
|
void MissionHalt::Exe() { |
269 |
|
/* do nothing */ |
270 |
|
IncTime(); |
271 |
|
} |
272 |
|
|
273 |
|
//------------------/\------------------------------------ |
274 |
|
|
275 |
|
|
276 |
|
|
277 |
|
|
278 |
|
|
279 |
|
|