-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathais.cpp
245 lines (216 loc) · 6.11 KB
/
ais.cpp
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//
// author: Kovacs Marton
// email: [email protected]
// license: whatever. note my name
//
// ais.cpp
//
// artifical intelligence code, including the endpoint of remote control
//
#include "ais.hpp"
#include "flyerz.hpp"
#include <sstream>
#include <iostream>
//
// DiskShipAiRandom functions
//
void DiskShipAiRandom::Do()
{
//random movement
if (!(GetTicker() % changeDirectionInterval))
{
m_randum = Normalize(Coordinate(DrawWrapper::Random(1000) - 500, DrawWrapper::Random(1000) - 500), DiskShip::maxSpeed);
}
GetSpeed() += m_randum;
//shooting if feasible
Wiz::ShipTravel enemies = GetEnemies();
if (!enemies.empty())
{
const Hitable* enemy = enemies[DrawWrapper::Random(enemies.size())];
Shoot(enemy->GetCenter());
}
}
//
// DiskShipAiRanger functions
//
double abs(double num)
{
return ((num > 0)? (num) : (-num));
}
void DiskShipAiRanger::Do()
{
Wiz::ShipTravel enemies = GetEnemies();
if (!enemies.empty())
{
const Hitable* enemy = FindClosest(enemies, GetCenter());
//found enemy. so shoot
Coordinate targetVector = GetCenter() - enemy->GetCenter();
Coordinate::CoordType distance = Length(targetVector);
Coordinate miss = Rotate90Cw(Normalize(targetVector, missFactor));
miss = ((miss * DrawWrapper::Random(100)) / 50 - miss) * distance / 100;
Shoot(enemy->GetCenter() + miss);
//and move
Coordinate::CoordType minDistance = minDistanceRatio * std::min(DrawWrapper::GetSize().x, DrawWrapper::GetSize().y);
//if far enough, we move sideways to make it harder to hit
if (distance > minDistance)
{
if (distance > minDistance + maxDistance)
{
targetVector = -targetVector;
}
Coordinate evadeVector = Rotate90Cw(targetVector);
targetVector += evadeVector;
}
if (abs(targetVector.x) + abs(targetVector.y) > 1)
{
targetVector = Normalize(targetVector, DiskShip::maxSpeed);
GetSpeed() += targetVector;
}
}
}
//
// DiskShipAiTest functions
//
void DiskShipAiTest::Do()
{
Wiz::ShipTravel enemies = GetEnemies();
if (!enemies.empty())
{
const Hitable* enemy = FindClosest(enemies, GetCenter());
Shoot(enemy->GetCenter());
}
GetSpeed() = Coordinate();
}
//
// DiskShipAi3D functions
//
void DiskShipAi3D::Do()
{
Coordinate center = GetCenter();
if (DiskShip::maxSpeed > Distance(m_center, center))
{
up = !up;
}
Size size = DrawWrapper::GetSize();
if (up)
{
GetSpeed() = Coordinate(0, -500);
Shoot(Coordinate(size.x - 20, 20));
}
else
{
GetSpeed() = Coordinate(0, 500);
Shoot(Coordinate(size.x - 20, size.y - 20));
}
m_center = center;
}
//
// DiskShipAiRemote functions
//
void DiskShipAiRemote::Do()
{
m_communication.Send(RemoteProtocol::BEGIN);
std::string str = m_communication.Receive();
std::istringstream istr(str);
while (RemoteProtocol::END != str)
{
std::string response;
istr >> str;
if (RemoteProtocol::COMMAND_SPEED == str)
{
if (Alive())
{
double x, y;
istr >> x >> y;
GetSpeed() = Coordinate(x, y);
response = RemoteProtocol::ACK;
}
else
{
response = RemoteProtocol::DEAD;
}
}
else if (RemoteProtocol::COMMAND_SHOOT == str)
{
if (Alive())
{
double x, y;
istr >> x >> y;
Shoot(Coordinate(x, y));
response = RemoteProtocol::ACK;
}
else
{
response = RemoteProtocol::DEAD;
}
}
else if (RemoteProtocol::QUERY == str)
{
istr >> str;
std::ostringstream ostr;
if (RemoteProtocol::QUERY_POSITION == str)
{
Coordinate coords = GetCenter();
ostr << RemoteProtocol::RESPONSE_POSITION << ' ' << coords.x << ' ' << coords.y;
}
else if (RemoteProtocol::QUERY_SPEED == str)
{
ostr << RemoteProtocol::RESPONSE_SPEED << ' ' << GetSpeed().x << ' ' << GetSpeed().y;
}
else if (RemoteProtocol::QUERY_TEAM == str)
{
ostr << RemoteProtocol::RESPONSE_TEAM << ' ' << GetTeam();
}
else if (RemoteProtocol::QUERY_ENEMIES == str)
{
ostr << RemoteProtocol::RESPONSE_ENEMIES << ' ';
Wiz::ShipTravel enemies = GetEnemies();
for (Wiz::ShipTravel::iterator it = enemies.begin(); enemies.end() != it; ++it)
{
const Hitable& enemy = *(*it);
Coordinate center = enemy.GetCenter();
ostr << enemy.GetTeam() << ' ' << center.x << ' ' << center.y << ' ';
}
}
else if (RemoteProtocol::QUERY_FRIENDS == str)
{
ostr << RemoteProtocol::RESPONSE_FRIENDS << ' ';
Wiz::ShipTravel team = GetTeammates();
for (Wiz::ShipTravel::iterator it = team.begin(); team.end() != it; ++it)
{
Coordinate center = (*it)->GetCenter();
ostr << center.x << ' ' << center.y << ' ';
}
}
else if (RemoteProtocol::QUERY_BULLETS == str)
{
ostr << RemoteProtocol::RESPONSE_BULLETS << ' ';
Wiz::LaserList bullets = GetBullets();
for (Wiz::LaserList::iterator it = bullets.begin(); bullets.end() != it; ++it)
{
ostr << it->first << ' ' << it->second.first.x << ' ' << it->second.first.y << ' ' << it->second.second.x << ' ' << it->second.second.y << ' ';
}
}
else if (RemoteProtocol::QUERY_CONTEXT == str)
{
Size size = DrawWrapper::GetSize();
ostr << "context " << size.x << ' ' << size.y << ' ' << DiskShip::shipSize << ' ' << DiskShip::maxSpeed << ' ' << DiskShip::bulletLimit << ' ' <<
DiskShip::cooldown << ' ' << DiskShip::laserLength << ' ' << PulseLaser::speed << ' ' << DiskShip::deadInterval;
}
response = ostr.str();
}
else
{
response = RemoteProtocol::ACK;
}
m_communication.Send(response);
str = m_communication.Receive();
istr.clear();
istr.str(str);
}
}
int DiskShipAiRandom::changeDirectionInterval = 10;
int DiskShipAiRandom::changeTargetInterval = 3;
double DiskShipAiRanger::minDistanceRatio = 0.2;
int DiskShipAiRanger::maxDistance = 300;
int DiskShipAiRanger::missFactor = 30;