-
Notifications
You must be signed in to change notification settings - Fork 30
/
entity_ai.cpp
163 lines (142 loc) · 4.07 KB
/
entity_ai.cpp
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
#include "entity_ai.h"
#include "stringfunc.h"
#include "window.h"
Entity_AI::Entity_AI()
{
for (int i = 0; i < PATHFEAT_MAX; i++) {
pathing_features[i] = false;
}
for (int i = 0; i < AIGOAL_MAX; i++) {
goals_in_use[i] = false;
}
area_awareness = 0;
attention_span = 15;
}
Entity_AI::~Entity_AI()
{
}
bool Entity_AI::uses_feature(Pathing_feature feat) const
{
return pathing_features[feat];
}
bool Entity_AI::uses_goal(AI_goal goal) const
{
return goals_in_use[goal];
}
bool Entity_AI::load_data(std::istream &data, std::string owner_name)
{
std::string ident, junk;
while (ident != "done") {
if ( ! (data >> ident) ) {
return false;
}
ident = no_caps(ident);
if (!ident.empty() && ident[0] == '#') {
// It's a comment
std::getline(data, junk);
} else if (ident == "awareness:" || ident == "area_awareness:") {
data >> area_awareness;
if (area_awareness < 0) {
debugmsg("Negative awareness value (%s)", owner_name.c_str());
return false;
}
std::getline(data, junk);
} else if (ident == "attention:" || ident == "attention_span:") {
data >> attention_span;
std::getline(data, junk);
} else if (ident == "recognizes:") {
std::string feat_name;
std::getline(data, feat_name);
Pathing_feature feat = lookup_pathing_feature(feat_name);
if (feat == PATHFEAT_NULL) {
debugmsg("Unknown pathing feature '%s' (%s)", feat_name.c_str(),
owner_name.c_str());
return false;
}
pathing_features[feat] = true;
} else if (ident == "goals:") {
std::string goal_name;
while (goal_name != "done") {
data >> goal_name;
goal_name = no_caps(goal_name);
goal_name = trim(goal_name);
if (goal_name != "done") {
AI_goal goal = lookup_AI_goal(goal_name);
if (goal == AIGOAL_NULL) {
debugmsg("Unknown AI goal '%s' (%s)", goal_name.c_str(),
owner_name.c_str());
return false;
}
goals.push_back(goal);
goals_in_use[goal] = true;
}
}
} else if (ident != "done") {
debugmsg("Unknown AI property '%s' (%s)", ident.c_str(),
owner_name.c_str());
return false;
}
}
return true;
}
Entity_AI& Entity_AI::operator=(const Entity_AI& rhs)
{
area_awareness = rhs.area_awareness;
attention_span = rhs.attention_span;
for (int i = 0; i < PATHFEAT_MAX; i++) {
Pathing_feature feat = Pathing_feature(i);
if (rhs.uses_feature(feat)) {
pathing_features[i] = true;
}
}
goals = rhs.goals;
return *this;
}
Pathing_feature lookup_pathing_feature(std::string name)
{
name = no_caps(name);
name = trim(name);
for (int i = 0; i < PATHFEAT_MAX; i++) {
Pathing_feature ret = Pathing_feature(i);
if ( no_caps( pathing_feature_name(ret) ) == name) {
return ret;
}
}
return PATHFEAT_NULL;
}
std::string pathing_feature_name(Pathing_feature feat)
{
switch (feat) {
case PATHFEAT_NULL: return "NULL";
case PATHFEAT_FIELDS: return "fields";
case PATHFEAT_MAX: return "BUG - PATHFEAT_MAX";
default: return "BUG - Unnamed Pathing_feature";
}
return "BUG - Escaped pathing_feature_name switch!";
}
AI_goal lookup_AI_goal(std::string name)
{
name = no_caps(name);
name = trim(name);
for (int i = 0; i < AIGOAL_MAX; i++) {
AI_goal ret = AI_goal(i);
if ( no_caps( AI_goal_name(ret) ) == name) {
return ret;
}
}
return AIGOAL_NULL;
}
std::string AI_goal_name(AI_goal goal)
{
switch (goal) {
case AIGOAL_NULL: return "NULL";
case AIGOAL_ATTACK_ENEMIES: return "attack_enemies";
case AIGOAL_ATTACK_NEUTRALS: return "attack_neutrals";
case AIGOAL_FLEE: return "flee";
case AIGOAL_EAT_CORPSES: return "eat_corpses";
case AIGOAL_COLLECT_ITEMS: return "collect_items";
case AIGOAL_MAX: return "BUG - AIGOAL_MAX";
default: return "BUG - Unnamed AI_goal";
}
return "BUG - Escaped AI_goal_name switch";
}