-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
176 lines (137 loc) · 5.21 KB
/
main.cpp
File metadata and controls
176 lines (137 loc) · 5.21 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
#include <iostream>
#include <vector>
#include "raylib.h"
#include "Headers/Agent.h"
#include "Headers/GotoPointBehaviour.h"
#include "Headers/NodeMap.h"
#include "Headers/PathAgent.h"
#include "Headers/Pathfinding.h"
#include "Headers/WanderBehaviour.h"
#include "Headers/FollowBehaviour.h"
#include "Headers/DefendBehaviour.h"
#include "Headers/DistanceCondition.h"
#include "Headers/DefendCondition.h"
#include "Headers/StateMachine.h"
#include "Headers/State.h"
#include "Headers/UtilityAI.h"
using namespace AIForGames;
std::vector<std::string> GenerateMap(Image image);
int main() {
constexpr int screenWidth = 1200;
constexpr int screenHeight = 800;
srand(time(nullptr));
InitWindow(screenWidth, screenHeight, "AI For Games");
Image mapImage = LoadImage("GrayscaleMaze.png");
std::vector<std::string> asciiMap = GenerateMap(mapImage);
auto *nodeMap = new NodeMap;
nodeMap->SetMapImage(mapImage);
nodeMap->Initialise(asciiMap, 50);
Node* start = nodeMap->GetNode(1, 1);
Node* end = nodeMap->GetNode(1, 1);
std::vector<Node*> path = NodeMap::AStarSearch(start, end);
// Regular Agent
Agent agent(nodeMap, new GotoPointBehaviour());
agent.SetNode(start);
agent.SetLineColour(Color(24, 204, 240, 255));
// Regular Agent
Agent agent2(nodeMap, new WanderBehaviour());
agent2.SetNode(nodeMap->GetRandomNode());
agent2.SetLineColour(Color(240, 157, 24, 255));
// FSM (Finite State Machine Agent)
Node* agentBaseNode = nodeMap->GetRandomNode();
// auto* closerThan5 = new DistanceCondition(5.0f * nodeMap->GetCellSize(), true);
// auto* furtherThan7 = new DistanceCondition(7.0f * nodeMap->GetCellSize(), false);
//
// auto* withinRange = new DefendCondition(agentBaseNode->position, 5 * nodeMap->GetCellSize(), true);
// auto* notWithinRange = new DefendCondition(agentBaseNode->position, 5 * nodeMap->GetCellSize(), false);
//
// auto* wanderState = new State(new WanderBehaviour());
// auto* followState = new State(new FollowBehaviour());
// auto* defendState = new State(new DefendBehaviour());
//
// wanderState->AddTransition(closerThan5, followState);
// wanderState->AddTransition(withinRange, defendState);
//
// followState->AddTransition(furtherThan7, wanderState);
// followState->AddTransition(withinRange, defendState);
//
// defendState->AddTransition(notWithinRange, wanderState);
// auto* sm = new StateMachine(wanderState);
// sm->AddState(wanderState);
// sm->AddState(followState);
// sm->AddState(defendState);
// Non-FSM (Finite State Machine) behaviours
auto* utilityAI = new UtilityAI();
utilityAI->SetStartBehaviour(new WanderBehaviour());
utilityAI->AddBehaviour(new WanderBehaviour());
utilityAI->AddBehaviour(new FollowBehaviour());
utilityAI->AddBehaviour(new DefendBehaviour());
Agent agent3(nodeMap, utilityAI);
agent3.SetNode(nodeMap->GetRandomNode());
agent3.SetBaseNode(agentBaseNode);
agent3.SetTarget(&agent);
agent3.SetSpeed(128 * 2.5);
agent3.SetLineColour(Color(24, 240, 49, 255));
auto time = static_cast<float>(GetTime());
bool switchedAlgo = false;
while (!WindowShouldClose()) {
const auto fTime = static_cast<float>(GetTime());
const float deltaTime = fTime - time;
time = fTime;
if (IsKeyPressed(KEY_S)) {
switchedAlgo = !switchedAlgo;
if (switchedAlgo) {
agent.SwitchAlgorithm(DIJKSTRA);
agent2.SwitchAlgorithm(DIJKSTRA);
agent3.SwitchAlgorithm(DIJKSTRA);
} else {
agent.SwitchAlgorithm(ASTAR);
agent2.SwitchAlgorithm(ASTAR);
agent3.SwitchAlgorithm(ASTAR);
}
}
BeginDrawing();
ClearBackground(Color(62, 65, 66));
nodeMap->Draw();
agent.Update(deltaTime);
agent.Draw();
agent2.Update(deltaTime);
agent2.Draw();
agent3.Update(deltaTime);
agent3.Draw();
float halfSize = nodeMap->GetCellSize() * 0.5;
DrawRectangleV({ agentBaseNode->position.x - halfSize, agentBaseNode->position.y - halfSize},
{ nodeMap->GetCellSize(), nodeMap->GetCellSize() }, {255, 0, 0, 75});
DrawText("Press S to switch Algorithm", 25, 15, 25, LIME);
if (agent.GetAlgorithm() == DIJKSTRA) {
DrawText("Current Algorithm: DIJKSTRA", 400, 15, 25, BLUE);
} else {
DrawText("Current Algorithm: ASTAR", 400, 15, 25, BLUE);
}
// DrawText(TextFormat("&.2f", ))
EndDrawing();
}
delete nodeMap;
UnloadImage(mapImage);
// delete closerThan5;
// delete furtherThan7;
// delete withinRange;
// delete notWithinRange;
CloseWindow();
return 0;
}
std::vector<std::string> GenerateMap(Image image) {
std::vector<std::string> map = {};
for (int y = 0; y < image.height; ++y) {
std::string line;
for (int x = 0; x < image.width; ++x) {
if (GetImageColor(image, x, y).r > 127) {
line.append("1");
} else {
line.append("0");
}
}
map.push_back(line);
}
return map;
}