Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
WorldState.cpp
Go to the documentation of this file.
1
2
3#include "WorldState.h"
4
5namespace Divide::goap
6{
7
8WorldState::WorldState(const string& name) noexcept
9 : priority_( 0 )
10 , name_( name )
11{
12 //nop
13}
14
15void WorldState::setVariable(const I32 var_id, const bool value)
16{
17 vars_[var_id] = value;
18}
19
20bool WorldState::getVariable(const I32 var_id) const {
21 return vars_.at(var_id);
22}
23
24
25bool WorldState::operator==(const WorldState& other) const {
26 return (vars_ == other.vars_);
27}
28
29bool WorldState::meetsGoal(const WorldState& goal_state) const
30{
31 for (const auto& kv : goal_state.vars_) {
32 try {
33 if (vars_.at(kv.first) != kv.second) {
34 return false;
35 }
36 }
37 catch (const std::out_of_range&) {
38 return false;
39 }
40 }
41 return true;
42}
43
44I32 WorldState::distanceTo(const WorldState& goal_state) const
45{
46 I32 result = 0;
47
48 for (const auto& kv : goal_state.vars_) {
49 auto itr = vars_.find(kv.first);
50 if (itr == end(vars_) || itr->second != kv.second) {
51 ++result;
52 }
53 }
54
55 return result;
56}
57
59{
60 string ret = "WorldState { ";
61 for ( const auto& kv : vars_ )
62 {
63 ret.append(kv.second ? "TRUE " : "FALSE ");
64 }
65 ret.append("}");
66
67 return ret;
68}
69
70} //namespace Divide::goap
int32_t I32
int distanceTo(const WorldState &goal_state) const
Definition: WorldState.cpp:44
string toString() const
Definition: WorldState.cpp:58
bool meetsGoal(const WorldState &goal_state) const
Definition: WorldState.cpp:29
WorldState(const Divide::string &name="") noexcept
Definition: WorldState.cpp:8
bool getVariable(const int var_id) const
Definition: WorldState.cpp:20
bool operator==(const WorldState &other) const
Definition: WorldState.cpp:25
void setVariable(const int var_id, const bool value)
Definition: WorldState.cpp:15
hashMap< I32, bool > vars_
Definition: WorldState.h:16