-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathai.cpp
66 lines (58 loc) · 2.22 KB
/
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
#include "ai.h"
#include "level.h"
AI::AI(Level* lvl)
{
level = lvl;
}
void AI::planEverything()
{
for (VictimList::iterator it = level->victims->begin(); it != level->victims->end(); it++) {
Victim* victim = *it;
if (victim->isDying || victim->isDead || (victim->plan != Victim::PLAN_NOTHING)) {
continue;
}
if (victim->plan == Victim::PLAN_WANDER) {
// Don't stop wandering for food or they all clog together.
continue;
}
if (victim->plan == Victim::PLAN_CHASE_FOOD) {
// Don't immediately look for new food once food is found.
victim->walkSteps++;
if (victim->walkSteps < MIN_FOOD_STEPS) continue;
}
// this victim needs a plan - try smelling for food
for (FoodList::iterator it = level->foods->begin(); it != level->foods->end(); it++) {
Food* food = *it;
if (food->isConsumed || food->isDead || !level->isInLevelBoundaries(food->position)) {
continue;
}
if (distance(food->position, victim->position) <= SMELL_DISTANCE) {
// Don't alwys take first food.
int r = rand() % 100;
if (r < BACKOFF_CHANCE_PERCENT) continue;
victim->plan = Victim::PLAN_CHASE_FOOD;
victim->target = food->position;
victim->walkSteps = 0;
break;
}
}
// still no plan? Wander around a bit
if (victim->plan == Victim::PLAN_NOTHING) {
victim->plan = Victim::PLAN_WANDER;
float wanderDistanceX = WANDER_DISTANCE_MIN + (WANDER_DISTANCE_MAX - WANDER_DISTANCE_MIN) * rand() / RAND_MAX;
float wanderDistanceY = WANDER_DISTANCE_MIN + (WANDER_DISTANCE_MAX - WANDER_DISTANCE_MIN) * rand() / RAND_MAX;
if (rand() % 2) wanderDistanceX *= -1;
if (rand() % 2) wanderDistanceY *= -1;
victim->target.x = victim->position.x + wanderDistanceX;
victim->target.y = victim->position.y + wanderDistanceY;
}
fixTarget(victim);
}
}
void AI::fixTarget(Victim* victim)
{
if (victim->target.x < Level::BORDER_ZONE) victim->target.x = Level::BORDER_ZONE;
if (victim->target.y < Level::BORDER_ZONE) victim->target.y = Level::BORDER_ZONE;
if (victim->target.x > level->pixelWidth - Level::BORDER_ZONE) victim->target.x = level->pixelWidth - Level::BORDER_ZONE;
if (victim->target.y > level->pixelHeight - Level::BORDER_ZONE) victim->target.y = level->pixelHeight - Level::BORDER_ZONE;
}