Repository navigation
Expand file tree
/
Copy pathcBotSimple2.cpp
More file actions
73 lines (57 loc) · 1.55 KB
/
Copy pathcBotSimple2.cpp
File metadata and controls
73 lines (57 loc) · 1.55 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
#include "bots.h"
#include "stdlib.h"
cBotSimple2::cBotSimple2() {}
cBotSimple2::~cBotSimple2() {}
// Always move closer to the target in terms of X and Y.
void cBotSimple2::ChooseNextGridPosition() {
int newX = PositionX();
int newY = PositionY();
if (PositionX() < gTarget.PositionX()) {
newX += 1;
}
else if (PositionX() > gTarget.PositionX()) {
newX -= 1;
}
if (PositionY() < gTarget.PositionY()) {
newY += 1;
}
else if (PositionY() > gTarget.PositionY()) {
newY -= 1;
}
// NOT FULLY IMPLEMENTED
int deltaX = newX - PositionX();
int deltaY = newY - PositionY();
int correctedX = PositionX();
int correctedY = PositionY();
int random = rand() % 2;
if (random == 0) { random = -1; }
if (deltaX < 0) { // object is moving left
correctedY += random;
} else if (deltaX > 0) { //object is moving right
correctedY += random;
} //else { // no lateral movement
if (deltaY < 0) {
//object is moving up
} else if (deltaX > 0) {
//object is moving down
} else {
// no vertical movement
}
// If the intended move fails, try to move in a different direction.
if (!SetNext(newX, newY, gLevel)) {
int correctedX = PositionX();
int correctedY = PositionY();
int random = rand() % 2;
if (random == 0) { random = -1; }
if (newX != PositionX()) {
correctedY = PositionY() + random;
}
if (newY != PositionY()) {
correctedX = PositionX() + random;
}
// If the new intended move fails, don't move for this cycle.
if (!SetNext(correctedX, correctedY, gLevel)) {
SetNext(PositionX(), PositionY(), gLevel);
}
}
}