forked from davechurchill/commandcenter
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCondition.cpp
More file actions
175 lines (154 loc) · 6.05 KB
/
Condition.cpp
File metadata and controls
175 lines (154 loc) · 6.05 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
#include "Condition.h"
#include "CCBot.h"
Condition::Condition()
: m_bot (nullptr)
, m_type (ConditionTypes::Invalid)
, m_player (0)
, m_lhs (nullptr)
, m_rhs (nullptr)
, m_op (ConditionOperators::Invalid)
{
}
Condition::Condition(const json & j, CCBot & bot)
: Condition()
{
m_bot = ⊥
m_json = j;
BOT_ASSERT(j.is_array(), "Condition must be an array");
// this is a condition with 2 sub-conditions and an operator
if (j.size() == 3)
{
BOT_ASSERT(j[1].is_string(), "Condition operator must be a string");
m_lhs = new Condition(j[0], bot);
m_op = GetOperator(j[1].get<std::string>());
m_rhs = new Condition(j[2], bot);
m_type = GetType(j[1].get<std::string>());
}
// this is a condition unary value with 'self' or 'enemy' followed by a unit type
else if (j.size() == 2)
{
m_type = ConditionTypes::UnaryUnitType;
BOT_ASSERT(j[0].is_string(), "First element of size 2 condition must be a string");
BOT_ASSERT(j[1].is_string(), "Second element of size 2 condition must be a string");
m_player = GetPlayer(j[0].get<std::string>());
m_strValue = j[1].get<std::string>();
}
// this is either an integer or a string denoting some fixed game value like time
else if (j.size() == 1)
{
if (j[0].is_number_integer())
{
m_type = ConditionTypes::UnaryInt;
m_intValue = j[0];
}
else if (j[0].is_string())
{
m_type = ConditionTypes::UnaryString;
m_strValue = j[0].get<std::string>();
std::transform(m_strValue.begin(), m_strValue.end(), m_strValue.begin(), ::tolower);
}
else
{
BOT_ASSERT(false, "Size 1 Condition must be int or string");
}
}
}
int Condition::intEval() const
{
if (m_type == ConditionTypes::BinaryInt)
{
switch (m_op)
{
case ConditionOperators::PLUS: { return m_lhs->intEval() + m_rhs->intEval(); }
case ConditionOperators::MINUS: { return m_lhs->intEval() - m_rhs->intEval(); }
case ConditionOperators::MULT: { return m_lhs->intEval() * m_rhs->intEval(); }
case ConditionOperators::DIV: { return m_lhs->intEval() / m_rhs->intEval(); }
}
}
else if (m_type == ConditionTypes::UnaryInt)
{
return m_intValue;
}
else if (m_type == ConditionTypes::UnaryUnitType)
{
return (int)m_bot->UnitInfo().getUnitTypeCount(m_player, UnitType::GetUnitTypeFromName(m_strValue, *m_bot));
}
else if (m_type == ConditionTypes::UnaryString)
{
if (m_strValue == "gameframe")
{
return m_bot->GetCurrentFrame();
}
BOT_ASSERT(false, "Unknown UnaryString value:", m_strValue.c_str());
}
BOT_ASSERT(false, "Can't int evaluate this Condition");
return 0;
}
bool Condition::eval() const
{
if (m_type == ConditionTypes::BinaryBool)
{
switch (m_op)
{
case ConditionOperators::AND: { return m_lhs->eval() && m_rhs->eval(); }
case ConditionOperators::OR: { return m_lhs->eval() || m_rhs->eval(); }
}
}
else if (m_type == ConditionTypes::BinaryComp)
{
switch (m_op)
{
case ConditionOperators::LT: { return m_lhs->intEval() < m_rhs->intEval(); }
case ConditionOperators::GT: { return m_lhs->intEval() > m_rhs->intEval(); }
case ConditionOperators::EQ: { return m_lhs->intEval() == m_rhs->intEval(); }
case ConditionOperators::NEQ: { return m_lhs->intEval() != m_rhs->intEval(); }
case ConditionOperators::LTE: { return m_lhs->intEval() <= m_rhs->intEval(); }
case ConditionOperators::GTE: { return m_lhs->intEval() >= m_rhs->intEval(); }
}
}
BOT_ASSERT(false, "Can't bool eval this Condition");
return false;
}
int Condition::GetOperator(const std::string & op)
{
if (op == "<") { return ConditionOperators::LT; }
if (op == ">") { return ConditionOperators::GT; }
if (op == "==") { return ConditionOperators::EQ; }
if (op == "!=") { return ConditionOperators::NEQ; }
if (op == "<=") { return ConditionOperators::LTE; }
if (op == ">=") { return ConditionOperators::GTE; }
if (op == "AND") { return ConditionOperators::AND; }
if (op == "OR") { return ConditionOperators::OR; }
if (op == "+") { return ConditionOperators::PLUS; }
if (op == "-") { return ConditionOperators::MINUS; }
if (op == "*") { return ConditionOperators::MULT; }
if (op == "/") { return ConditionOperators::DIV; }
BOT_ASSERT(false, "Invalid Condition operator: ", op.c_str());
return ConditionOperators::Invalid;
}
int Condition::GetType(const std::string & op)
{
if (op == "<") { return ConditionTypes::BinaryComp; }
if (op == ">") { return ConditionTypes::BinaryComp; }
if (op == "==") { return ConditionTypes::BinaryComp; }
if (op == "!=") { return ConditionTypes::BinaryComp; }
if (op == "<=") { return ConditionTypes::BinaryComp; }
if (op == ">=") { return ConditionTypes::BinaryComp; }
if (op == "AND") { return ConditionTypes::BinaryBool; }
if (op == "OR") { return ConditionTypes::BinaryBool; }
if (op == "+") { return ConditionTypes::BinaryInt; }
if (op == "-") { return ConditionTypes::BinaryInt; }
if (op == "*") { return ConditionTypes::BinaryInt; }
if (op == "/") { return ConditionTypes::BinaryInt; }
BOT_ASSERT(false, "Invalid Condition operator: ", op.c_str());
return ConditionOperators::Invalid;
}
int Condition::GetPlayer(const std::string & player)
{
std::string playerLower = player;
std::transform(playerLower.begin(), playerLower.end(), playerLower.begin(), ::tolower);
if (playerLower == "self") { return 0; }
if (playerLower == "enemy") { return 1; }
BOT_ASSERT(false, "Unknown Player string:", player.c_str());
return 0;
}