-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlienStateMachine.cpp
More file actions
172 lines (162 loc) · 8.05 KB
/
AlienStateMachine.cpp
File metadata and controls
172 lines (162 loc) · 8.05 KB
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
#include "Alien.hpp"
#include "Mobile.hpp"
#include "AlienStateMachine.hpp"
#include "Triple.hpp"
#include "game.hpp"
#include "util.hpp"
#include "Wander.hpp"
#include "Arrive.hpp"
#include "Pursue.hpp"
#include "Evade.hpp"
#include "PathFollowing.hpp"
#define DEBUG_ALIENSTATEMACHINE
#ifdef DEBUG_ALIENSTATEMACHINE
# include <iostream>
#endif
AlienStateMachine::AlienStateMachine(std::string name ,
Mobile *character ,
Mobile *target ,
double maxRotationW ,
double targetRadiusW,
double slowRadiusW ,
double wanderOffsetW,
double wanderRadiusW,
double wanderRateW ,
double wanderTimeW ,
double maxSpeedW ,
double maxSpeedA ,
double targetRadiusA,
double slowRadiusA ,
double maxSpeedP ,
double maxSpeedE ,
double maxSpeed ,
double targetRadius ,
double slowRadius ):
DirectKinematicV(name),
character(character),
target(target),
state(States::Wander),
wander(new Wander(name + "Wander", character, maxRotationW, targetRadiusW, slowRadiusW, wanderOffsetW, wanderRadiusW, wanderRateW, wanderTimeW, maxSpeedW)),
arrive(new Arrive(name + "Arrive", character, target, maxSpeedA, targetRadiusA, slowRadiusA)),
pursue(new Pursue(name + "Pursue", character, target, maxSpeedP)),
evade(new Evade(name + "Evade", character, target, maxSpeedE)),
maxSpeed(maxSpeed),
targetRadius(targetRadius),
slowRadius(slowRadius),
last_ticks(0)
{
for (auto it = cover.begin(); it != cover.end(); ++it) {
paths.push_back(new PathFollowing(name + "PathFollowing", character, dynamic_cast<Mobile *>(*it), maxSpeed, targetRadius, slowRadius));
this->paths.back()->active = false;
}
for (auto it = recovery.begin(); it != recovery.end(); ++it) {
paths.push_back(new PathFollowing(name + "PathFollowing", character, dynamic_cast<Mobile *>(*it), maxSpeed, targetRadius, slowRadius));
this->paths.back()->active = false;
}
std::cout << "Alien paths size" << paths.size() << std::endl;
this->arrive->active = false;
this->pursue->active = false;
this->evade->active = false;
}
AlienStateMachine::~AlienStateMachine() {
for (unsigned int i = paths.size() - 1; i >= 0; i--)
delete this->paths[i];
delete this->evade;
delete this->pursue;
delete this->arrive;
delete this->wander;
}
std::vector<Triple> AlienStateMachine::getVel(unsigned int ticks, unsigned int delta_ticks) {
Triple cp, tp;
double distance, p;
std::vector<Triple> temp;
std::vector<Triple> out;
if (this->last_ticks != ticks) {
this->last_ticks = ticks;
std::tie(cp, tp) = points(character, target);
distance = (tp - cp).length();
if (dynamic_cast<Alien *>(character)->hp >= 30) {
if (distance > 20) {
if (state != States::Wander) {
std::cout << "Wander" << std::endl;
this->state = States::Wander;
this->wander->active = true ;
this->arrive->active = false;
this->pursue->active = false;
this->evade->active = false;
for (unsigned int i = 0; i < paths.size(); i++) {
if(this->paths[i]->active)
this->paths[i]->active = false;
}
}
} else {
if (state != States::Arrive) {
std::cout << "Arrive" << std::endl;
this->state = States::Arrive;
this->wander->active = false;
this->arrive->active = true ;
this->pursue->active = false;
this->evade->active = false;
for (unsigned int i = 0; i < paths.size(); i++) {
if(this->paths[i]->active)
this->paths[i]->active = false;
}
}
}
} else {
if (distance > 20) {
if (state != States::Path) {
std::cout << "PathFollowing" << std::endl;
this->state = States::Path;
this->wander->active = false;
this->arrive->active = false;
this->pursue->active = false;
this->evade->active = false;
if (paths.size() > 0) {
p = RandBin(0, paths.size() - 1);
this->paths[p]->active = true;
}
else
this->evade->active = true;
}
}
else if (distance < 5) {
if (state != States::Evade) {
std::cout << "Evade" << std::endl;
this->state = States::Evade;
this->wander->active = true;
this->arrive->active = false;
this->pursue->active = false;
this->evade->active = true;
if (paths.size() > 0) {
p = RandBin(0, paths.size() - 1);
this->paths[p]->active = false;
}
}
}
}
}
if (this->wander->active) {
temp = wander->getVel(ticks, delta_ticks);
out.insert(out.begin(), temp.begin(), temp.end());
}
if (this->arrive->active) {
temp = arrive->getVel(ticks, delta_ticks);
out.insert(out.begin(), temp.begin(), temp.end());
}
if (this->pursue->active) {
temp = pursue->getVel(ticks, delta_ticks);
out.insert(out.begin(), temp.begin(), temp.end());
}
if (this->evade->active) {
temp = evade->getVel(ticks, delta_ticks);
out.insert(out.begin(), temp.begin(), temp.end());
}
for (unsigned int i = 0; i < paths.size(); i++) {
if (this->paths[i]->active) {
temp = paths[i]->getVel(ticks, delta_ticks);
out.insert(out.begin(), temp.begin(), temp.end());
}
}
return out;
}