-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShipAI.cpp
More file actions
362 lines (320 loc) · 11.2 KB
/
ShipAI.cpp
File metadata and controls
362 lines (320 loc) · 11.2 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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include "ShipAI.h"
#include "GameScreen.h"
//---------------------------------------------------------------------------
ShipAI::ShipAI(Ogre::String nym, Ogre::SceneManager* mgr, Simulator* sim, GameScreen* gs, Ogre::SceneNode* sn, int &sc, SoundPlayer* sPlayer, int ops) : Ship(nym, mgr, sim, gs, NULL, sc, sPlayer, NULL, -1)
{
sceneNode = sn;
numOpponents = ops;
target = NULL;
gameStarted = true;
//paddle = paddleAI;
paddle = NULL;
yT = 0;
motorRight = true;
paddleHinge = NULL;
paces = 1000; //tentative
doneRoaming = false;
mustFlee = false;
doneFleeing = false;
roamState = true;
shootState = false;
huntState = false;
meleeState = false;
fleeState = false;
hasDecr = false;
left = false;
right = false;
forward = false;
back = false;
float maxP = 2000;
float minP = -maxP;
Ogre::Real xP = minP + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(maxP-minP)));
Ogre::Real zP = minP + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(maxP-minP)));
rootNode->setPosition(Ogre::Vector3(xP,0,zP));
}
//---------------------------------------------------------------------------
ShipAI::~ShipAI(void)
{
}
//---------------------------------------------------------------------------
void ShipAI::addToScene(void)
{
geom = sceneMgr->createEntity(name + "Ent", "rocket.mesh");
geom->setCastShadows(true);
rootNode->attachObject(geom);
mass = 10.0f;
shape = new btCapsuleShapeZ(3.0f, 15.0f);
pSys = sceneMgr->createParticleSystem(name + "PS", "ship_particles");
Ogre::SceneNode* pNode = (Ogre::SceneNode*)rootNode->createChild(Ogre::Vector3(0,-1,-8));
pNode->attachObject(pSys);
paddle->addToScene();
}
//---------------------------------------------------------------------------
void ShipAI::update(void)
{
if(hp <= 0) return;
//processing self and surroundings
//printf("Starting update\n");
survivalCheck();
incomingAst();
opponentProximityCheck();
//taking action based on state(s)
if (roamState) {
roam();
}
if (shootState) {
meleeState = false;
shoot();
}
if (huntState) {
roamState = false;
hunt();
}
if (meleeState) {
shootState = false;
melee();
}
if (fleeState) {
flee();
}
//health changes
if(!context->hit) {
hasDecr = false;
}
if (!hasDecr && context->hit && iframes <= 0){
//lose health
if (hp > 0) {
hp-=20;
if (hp <= 0){
hp = 0;
pSys->setEmitting(false);
}
if (hp > 0 && iframes < IFRAMES_ON_HIT)
iframes = IFRAMES_ON_HIT;
//printf("AI injured\n");
}
if (hp < 30) {
mustFlee = true;
}
if (!gameScreen->getIsClient() && !gameScreen->isSinglePlayer()) {
Packet p;
p << (char) SPT_HEALTH << clientId << hp;
gameScreen->getNetManager()->messageClientsTCP(p);
}
hasDecr = true;
}
if (iframes > 0) iframes--;
}
//---------------------------------------------------------------------------
/* Default state of AI */
void ShipAI::roam(void)
{
// printf("Roam function \n");
if (sqrt(getPos().x*getPos().x+getPos().z*getPos().z) > 4000){
body->applyCentralForce(btVector3(-5*getPos().x,0,-5*getPos().z));
}
if (paces >= 1000) {
//printf("Roam function randomize torque\n");
//melee();
paces = 0;
float minT = -1500;
float maxT = 1500;
yT = minT + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(maxT-minT)));
if (!doneFleeing) {
doneRoaming = true;
}
doneFleeing = false;
} else if (paces<=500){
// printf("Roam function turn\n");
// printf("paces: %d", paces);
body->setAngularFactor(btVector3(0,1,0));
body->applyTorque(btVector3(0, yT,0));
body->setAngularFactor(btVector3(0,0,0));
paces++;
} else {
//printf("Roam function move forward\n");
body->setAngularVelocity(btVector3(0,((body->getAngularVelocity()).getY())*0.95,0));
direction = rootNode->getOrientation() * Ogre::Vector3(0,0,1);
body->applyCentralForce(btVector3(6000*direction.x, 6000*direction.y, 6000*direction.z));
btVector3 shipVel = body->getLinearVelocity();
body->setLinearVelocity(btVector3(shipVel.getX()*0.99,shipVel.getY()*0.99,shipVel.getZ()*0.99));
paces++;
}
}
//---------------------------------------------------------------------------
void ShipAI::shoot(void)
{
//Enter code for how to shoot laser beam here
}
//---------------------------------------------------------------------------
void ShipAI::hunt(void)
{
paces = 0;
body->setAngularVelocity(btVector3(0,((body->getAngularVelocity()).getY())*0.95,0));
direction = rootNode->getOrientation() * Ogre::Vector3(0,0,1);
// printf("direction: (%f, %f, %f)\n", direction.x, direction.y, direction.z);
// printf("AI position: (%f, %f, %f)\n", getPos().x, getPos().y, getPos().z);
// printf("target position: (%f, %f, %f)\n", target->getPos().x, target->getPos().y, target->getPos().z);
Ogre::Vector3 huntPath = (target->getPos() - getPos()).normalisedCopy();
//printf("huntPath: (%f, %f, %f)\n", huntPath.x, huntPath.y, huntPath.z);
int count = 0;
Ogre::Quaternion amountRotation = direction.getRotationTo(huntPath);
//printf("amountRotation: (%f,%f,%f)\n", amountRotation.x, amountRotation.y, amountRotation.z);
int turn;
if (amountRotation.y < 0) {
//printf("turn left\n");
turn = -1;
} else {
//printf("turn right\n");
turn = 1;
}
if (((int)(10*direction.x)) != ((int)(10*huntPath.x)) || ((int)(10*direction.z)) != ((int)(10*huntPath.z))) {
count++;
// printf("direction1: (%f, %f, %f)\n", direction.x, direction.y, direction.z);
// printf("huntPath1: (%f, %f, %f)\n", huntPath.x, huntPath.y, huntPath.z);
body->setAngularFactor(btVector3(0,1,0));
body->applyTorque(btVector3(0,turn*10000,0));
body->setAngularFactor(btVector3(0,0,0));
direction = rootNode->getOrientation() * Ogre::Vector3(0,0,1);
// printf("direction2: (%d, %d, %d)\n", (int)(10*direction.x), (int)(10*direction.y), (int)(10*direction.z));
// printf("huntPath2: (%d, %d, %d)\n", (int)(10*huntPath.x), (int)(10*huntPath.y), (int)(10*huntPath.z));
//printf("num times through loop: %d\n", count);
}
btVector3 shipVel = body->getLinearVelocity();
body->setLinearVelocity(btVector3(shipVel.getX()*0.99,shipVel.getY()*0.99,shipVel.getZ()*0.99));
meleeState = false;
if (((int)(10*direction.x)) == ((int)(10*huntPath.x)) && ((int)(10*direction.z)) == ((int)(10*huntPath.z))) {
// printf("direction3: (%d, %d, %d)\n", (int)(10*direction.x), (int)(10*direction.y), (int)(10*direction.z));
// printf("huntPath3: (%d, %d, %d)\n", (int)(10*huntPath.x), (int)(10*huntPath.y), (int)(10*huntPath.z));
//body->setAngularFactor(btVector3(0,0,0));
body->setAngularVelocity(btVector3(0,((body->getAngularVelocity()).getY())*0.95,0));
body->applyCentralForce(btVector3(6000*direction.x, 6000*direction.y, 6000*direction.z));
btVector3 shipVel = body->getLinearVelocity();
body->setLinearVelocity(btVector3(shipVel.getX()*0.99,shipVel.getY()*0.99,shipVel.getZ()*0.99));
float dtProd = direction.dotProduct(target->getPos() - getPos());
bool inFront = dtProd > 0;
if (inFront && getPos().squaredDistance(target->getPos()) < 2500) {
meleeState = true;
} else {
meleeState = false;
}
}
}
//---------------------------------------------------------------------------
void ShipAI::melee(void)
{
paddleHinge = paddle -> getPaddleHinge();
if (motorRight)
paddleHinge->enableAngularMotor(true, -100, 1000);
else
paddleHinge->enableAngularMotor(true, 100, 1000);
motorRight = !motorRight;
}
//---------------------------------------------------------------------------
void ShipAI::flee(void)
{
//Code to Flee
//if enemy is near, begin fleeing and continue fleeing until survival check deems otherwise
std::vector<PlayerObject*>playerList = gameScreen->getPlayers();
direction = rootNode->getOrientation() * Ogre::Vector3(0,0,1);
for (PlayerObject* player : playerList) {
if (player != this && getPos().squaredDistance(player->getPos()) <= 10000) {
Ogre::Vector3 fleePath = (getPos() - player->getPos()).normalisedCopy();
Ogre::Quaternion amountRotation = direction.getRotationTo(fleePath);
int turn;
if (amountRotation.y < 0) {
turn = -1;
} else {
turn = 1;
}
if (((int)(10*direction.x)) != ((int)(10*fleePath.x)) || ((int)(10*direction.z)) != ((int)(10*fleePath.z))) {
body->setAngularFactor(btVector3(0,1,0));
body->applyTorque(btVector3(0,turn*10000,0));
body->setAngularFactor(btVector3(0,0,0));
}
btVector3 shipVel = body->getLinearVelocity();
body->setLinearVelocity(btVector3(shipVel.getX()*0.99,shipVel.getY()*0.99,shipVel.getZ()*0.99));
if (((int)(10*direction.x)) == ((int)(10*fleePath.x)) && ((int)(10*direction.z)) == ((int)(10*fleePath.z))) {
//body->setAngularFactor(btVector3(0,0,0));
body->setAngularVelocity(btVector3(0,((body->getAngularVelocity()).getY())*0.95,0));
body->applyCentralForce(btVector3(6000*direction.x, 6000*direction.y, 6000*direction.z));
btVector3 shipVel = body->getLinearVelocity();
body->setLinearVelocity(btVector3(shipVel.getX()*0.99,shipVel.getY()*0.99,shipVel.getZ()*0.99));
}
float dtProd = direction.dotProduct(player->getPos() - getPos());
bool inFront = dtProd > 0;
if (inFront && getPos().squaredDistance(player->getPos()) < 2500) {
melee();
}
}
}
}
//---------------------------------------------------------------------------
/*checks self-health*/
void ShipAI::survivalCheck(void)
{
if (hp < 30 && paces < 100 && (doneRoaming || mustFlee)) {
if (mustFlee && paces > 0) {
//printf("Health is low\n");
paces = 0;
}
fleeState = true;
roamState = false;
huntState = false;
meleeState = false;
shootState = false;
paces++;
} else {
mustFlee = false;
doneRoaming = false;
fleeState = false;
roamState = true;
}
}
//---------------------------------------------------------------------------
/*checks if an asteroid might hit self and initiates melee move if so */
void ShipAI::incomingAst(void)
{
direction = rootNode->getOrientation() * Ogre::Vector3(0,0,1);
std::vector<Asteroid*>astList = gameScreen->getAsteroids();
for (Asteroid* ast : astList) {
float dtProd = direction.dotProduct(ast->getPos() - getPos());
bool inFront = dtProd > 0;
if (inFront && getPos().squaredDistance(ast->getPos()) < 2500) {
melee();
}
}
}
//---------------------------------------------------------------------------
/*checks proximity of opponents and chooses target based on health*/
void ShipAI::opponentProximityCheck(void)
{
direction = rootNode->getOrientation() * Ogre::Vector3(0,0,1);
std::vector<PlayerObject*>playerList = gameScreen->getPlayers();
int nearbyOps = 0;
float dist;
for (PlayerObject* player : playerList) {
dist= getPos().squaredDistance(player->getPos());
if (player != this && getPos().squaredDistance(player->getPos()) <= 10000) {
if (target == NULL || player->getHealth() <= 20 || player->getHealth() > target->getHealth()) {
target = player;
huntState = true;
roamState = false;
}
nearbyOps++;
//printf("Incremented nearbyOps\n");
//melee();
}
}
if (nearbyOps == 0 && hp >= 30) {
if (hp >= 30) {
target = NULL;
}
//paces = 1000;
// btVector3 shipVel = body->getLinearVelocity();
// body->setLinearVelocity(btVector3(shipVel.getX()*0.99,shipVel.getY()*0.99,shipVel.getZ()*0.99));
huntState = false;
roamState = true;
}
}
//---------------------------------------------------------------------------