-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit_ai.cpp
More file actions
271 lines (248 loc) · 8.29 KB
/
unit_ai.cpp
File metadata and controls
271 lines (248 loc) · 8.29 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include "unit_ai.hpp"
// using namespace std;
AI::AI(Unit* controlled, Map* curMap) : controlled(controlled), curMap(curMap) {
timeSinceLastUpdate = UNIT_AI_UPDATE_TIME;
lastKnownPos = TexXYToTileXY(controlled->realX, controlled->realY);
}
bool AI::isUnitCloseToCenterOfTile(point unitLoc) {
#ifdef AI_MOVE_CENTER_OF_TILE
point centerPoint = TileXYToTexXY(unitLoc.tileX, unitLoc.tileY);
float dx = controlled->realX - centerPoint.realX;
float dy = controlled->realY - centerPoint.realY;
return dx * dx + dy * dy < DISTANCE_FROM_CENTER_SQ;
#else
return true;
#endif
}
bool AI::walkToPoint(point targetPoint, point curPoint, int distance_allowed) {
std::vector<point> route = AStarSearch(curMap, curPoint.tileX, curPoint.tileY, targetPoint.tileX, targetPoint.tileY, 1);
if (route.size() != 0) {
// Remove the last point in the route, since it is the current position
route.pop_back();
controlled->walkTo(route);
return true;
}
return false;
}
void AI::cancelJob(Job* job, std::string reason) {
std::cerr << "Job suspended: " << reason << std::endl;
jobState = 0;
controlled->state = STATE_IDLE;
job->suspended = true;
}
// Cause the controlled unit to move to the next point on its list
void AI::moveToNextPoint() {
controlled->curPath.pop_back();
}
void AI::continueWalking() {
point curPos = TexXYToTileXY(controlled->realX, controlled->realY);
if (curPos.tileX != lastKnownPos.tileX || curPos.tileY != lastKnownPos.tileY) {
if (isUnitCloseToCenterOfTile(curPos)) {
// Are we at the target location?
point nextLoc = controlled->curPath.back();
if (curPos.tileX != nextLoc.tileX || curPos.tileY != nextLoc.tileY) {
DEBUG_OUTPUT << "Unit ended up somewhere unexpected - need to recreate path" << std::endl;
DEBUG_OUTPUT << "(Expected " << nextLoc.tileX << "," << nextLoc.tileY << "), got (" << curPos.tileX << "," << curPos.tileY << "))" << std::endl;
DEBUG_OUTPUT << "Last known position:" << lastKnownPos.tileX << "," << lastKnownPos.tileY << std::endl;
// We meed to recreate the path
int targetX, targetY;
while (!controlled->curPath.empty()) {
point pathPoint = controlled->curPath.back();
targetX = pathPoint.tileX;
targetY = pathPoint.tileY;
controlled->curPath.pop_back();
}
controlled->curPath = AStarSearch(curMap, curPos.tileX, curPos.tileY, targetX, targetY);
}
moveToNextPoint();
lastKnownPos.tileX = curPos.tileX;
lastKnownPos.tileY = curPos.tileY;
}
}
}
bool AI::meetsJobRequirements(Job* job) {
// Can't pick up a suspended job
if (job->suspended) {
return false;
}
// Can't pick up jobs that are assigned to someone else
if (!(job->assigned == NULL || job->assigned == controlled)) {
return false;
}
// Can't pick up jobs we're not qualified for
if ((job->requirements & controlled->skills) != job->requirements) {
return false;
}
return true;
}
bool AI::checkJobBoard() {
// Check the job board for stuff to do
point curPoint = TexXYToTileXY(controlled->realX, controlled->realY);
point closestPoint;
std::vector<point> closestRoute;
curJob = NULL;
for (unsigned int i = 0; i < JobQueue::jobQueue.size(); i++) {
point targetPoint;
Job* job = JobQueue::jobQueue[i];
if (!meetsJobRequirements(job)) {
continue;
}
// Passed all checks: determine what to do depending on the type of job
lastKnownPos.tileX = curPoint.tileX;
lastKnownPos.tileY = curPoint.tileY;
std::vector<point> route;
switch(job->type) {
case JOB_TYPE_MINING: {
if (job->targetEnt == NULL) {
std::cerr << "Error: mining job created without a target ent" << std::endl;
continue;
}
targetPoint = TexXYToTileXY(job->targetEnt->realX, job->targetEnt->realY);
break;
}
case JOB_TYPE_WOODCUT: {
if (job->targetEnt == NULL) {
std::cerr << "Error: woodcut job created without a target ent" << std::endl;
continue;
}
targetPoint = TexXYToTileXY(job->targetEnt->realX, job->targetEnt->realY);
break;
}
case JOB_TYPE_BUILD: {
if (job->targetEnt == NULL || ((Item*) job->targetEnt)->inInventory == true) {
cancelJob(job, "item destroyed or missing");
continue;
} else if (job->targetPoint == NULL) {
std::cerr << "Error: building job created without a target point" << std::endl;
continue;
}
targetPoint = TexXYToTileXY(job->targetEnt->realX, job->targetEnt->realY);
break;
}
}
route = AStarSearch(curMap, curPoint.tileX, curPoint.tileY, targetPoint.tileX, targetPoint.tileY, 1);
if (route.size() != 0 && (curJob == NULL || route.size() < closestRoute.size())) {
closestRoute = route;
curJob = job;
closestPoint = targetPoint;
} else if (route.size() == 0) {
cancelJob(job, "could not reach target");
}
}
if (curJob != NULL) {
// Pathfind to the job and pick it up
if (walkToPoint(closestPoint, curPoint, 1)) {
curJob->assigned = controlled;
jobState = JOB_STAGE_WALKING_TO_DEST;
return true;
}
}
return false;
}
void AI::finishJob() {
jobState = 0;
controlled->state = STATE_IDLE;
switch(curJob->type) {
case JOB_TYPE_MINING: {
if (curJob->targetEnt != NULL) {
RequestQueues::entityRequests.push_back(entRequest::delEntRequest(curJob->targetEnt->uid, ENT_TYPE_DOODAD));
}
break;
}
case JOB_TYPE_WOODCUT: {
if (curJob->targetEnt == NULL) {
cancelJob(curJob, "target destroyed or missing");
return;
}
RequestQueues::entityRequests.push_back(entRequest::newEntRequest("Wood", curJob->targetEnt->realX, curJob->targetEnt->realY, ENT_TYPE_ITEM));
RequestQueues::entityRequests.push_back(entRequest::delEntRequest(curJob->targetEnt->uid, ENT_TYPE_DOODAD));
break;
}
case JOB_TYPE_BUILD: {
if (curJob->targetEnt == NULL) {
cancelJob(curJob, "item destroyed or missing");
return;
}
RequestQueues::entityRequests.push_back(entRequest::delEntRequest(curJob->targetEnt->uid, ENT_TYPE_ITEM));
curMap->setTasked(curJob->targetPoint->tileX, curJob->targetPoint->tileY, false);
curMap->setColor(curJob->targetPoint->tileX, curJob->targetPoint->tileY, COLOR_NONE);
curMap->setRoom(curJob->targetPoint->tileX, curJob->targetPoint->tileY, curJob->roomIDToBuild);
// DEBUG:
curMap->setTile(curJob->targetPoint->tileX, curJob->targetPoint->tileY, 2);
break;
}
}
for (std::vector<Job*>::iterator it = JobQueue::jobQueue.begin() ; it != JobQueue::jobQueue.end(); ++it) {
if ((*it) == curJob) {
JobQueue::jobQueue.erase(it);
delete curJob;
break;
}
}
}
// Like so many other things, this has become a mess
void AI::progressJobStage() {
switch(jobState) {
case JOB_STAGE_WALKING_TO_DEST: {
switch(curJob->type) {
case JOB_TYPE_BUILD: {
point controlledPoint = TexXYToTileXY(controlled->realX, controlled->realY);
if (curJob->targetEnt == NULL) {
cancelJob(curJob, "item destroyed or missing");
return;
}
Item* targetItem = ((Item*) curJob->targetEnt);
if (controlled->hasItem(targetItem)) {
if (tileDistSq(*(curJob->targetPoint), controlledPoint) > UNIT_PICKUP_DISTANCE) {
walkToPoint(*(curJob->targetPoint), controlledPoint, UNIT_PICKUP_DISTANCE);
} else {
// Job success
jobState = JOB_STAGE_ACTING;
controlled->startTask(0.5);
}
} else if (targetItem->inInventory) {
cancelJob(curJob, "item destroyed or missing");
return;
} else {
point targetPoint = TexXYToTileXY(curJob->targetEnt->realX, curJob->targetEnt->realY);
if (tileDistSq(targetPoint, controlledPoint) > UNIT_PICKUP_DISTANCE) {
walkToPoint(targetPoint, controlledPoint, UNIT_PICKUP_DISTANCE);
} else {
Item* targetItem = (Item*)curJob->targetEnt;
controlled->pickupItem(targetItem);
}
}
break;
}
case JOB_TYPE_MINING:
case JOB_TYPE_WOODCUT: {
jobState = JOB_STAGE_ACTING;
controlled->startTask(0.5);
break;
}
}
break;
}
case JOB_STAGE_ACTING: {
finishJob();
break;
}
default:
break;
}
}
// Dunno how much (if at all) I want to subclass AI, so it is non-virtual for now
void AI::update(float dt) {
if (controlled->state == STATE_WALKING) {
continueWalking();
} else if (controlled->state == STATE_FINISHED_JOB) {
progressJobStage();
} else {
timeSinceLastUpdate -= dt;
if (timeSinceLastUpdate <= 0 && controlled->state == STATE_IDLE) {
if (!checkJobBoard()) {
timeSinceLastUpdate = UNIT_AI_UPDATE_TIME;
}
}
}
}