-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
103 lines (86 loc) · 2.79 KB
/
main.cpp
File metadata and controls
103 lines (86 loc) · 2.79 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
#include "GameGOAPLib.h"
#include "demo/Action/AttackAction.h"
#include "demo/Action/GoToEnemyAction.h"
#include "demo/Action/SearchAction.h"
#include "demo/Variable/EnemyDeadVariable.h"
#include "demo/Variable/EnemyInRangeVariable.h"
#include "demo/Variable/EnemyLostVariable.h"
#include "Blackboard.h"
#include <iostream>
#include <vector>
#include <chrono>
#include <thread>
using namespace GameGOAP;
using namespace std;
using namespace std::chrono;
// reset node
int GameGOAP::Node::lastID = 0;
int main(void)
{
std::cout << "Example running...\n";
// create action list
std::vector<Action*> actions;
actions.reserve(MAX_ACTIONS);
// create variable list
std::vector<Variable*> variables;
actions.reserve(MAX_VARIABLES);
// create variable map
std::vector<std::string> variableMap;
// create initial and goal states
WorldState* initialState = new WorldState;
WorldState* goalState = new WorldState;
// create factory
ActionFactory* actionFactory = new ActionFactory;
actionFactory->registerClass("AttackAction", &AttackAction::create);
actionFactory->registerClass("GoToEnemyAction", &GoToEnemyAction::create);
actionFactory->registerClass("SearchAction", &SearchAction::create);
VariableFactory* variableFactory = new VariableFactory;
variableFactory->registerClass("EnemyDeadVariable", &EnemyDeadVariable::create);
variableFactory->registerClass("EnemyInRangeVariable", &EnemyInRangeVariable::create);
variableFactory->registerClass("EnemyLostVariable", &EnemyLostVariable::create);
// load from file
GOAPLoader* loader = new GOAPLoader;
loader->loaGOAP("GOAPTest1.xml", actionFactory, actions, variableFactory, variables, *initialState, *goalState, true);
// check actions
for (auto action : actions)
{
std::cout << action->getName() << " preconditions = " << action->preconditiins() << ", effects = " << action->effects() << std::endl;
}
// create planner
Planner* planner = new Planner;
// Set up blackboard
Blackboard* blackboard = new Blackboard;
float health = 100.f;
float enemyHealth = 100.f;
float position = 30.f;
float enemyPosition = 0.f;
blackboard->registerEntry("health", &health);
blackboard->registerEntry("enemyHealth", &enemyHealth);
blackboard->registerEntry("position", &position);
blackboard->registerEntry("enemyPosition", &enemyPosition);
//// run planner
//planner->plan(*initialState, *goalState, actions);
//std::cin.get();
// create runner
Runner* runner = new Runner(actions, planner, initialState, goalState);
// run
time_point<system_clock> t = system_clock::now();
int i = 0;
while (true)
{
std::cout << "------------------------" << std::endl;
if (i % 10 == 0)
{
i = 0;
runner->tick(blackboard, true);
}
else
{
runner->tick(blackboard);
}
t += milliseconds(1000);
this_thread::sleep_until(t);
i++;
}
return 0;
}