game2001/server/gameserver/android.ai.cc
aozhiwei 62373e21b4 1
2019-03-22 17:40:23 +08:00

120 lines
3.1 KiB
C++

#include "precompile.h"
#include "android.ai.h"
#include "android.h"
#include "movement.h"
#include "room.h"
#include "metamgr.h"
void AndroidAI::Update(int delta_time)
{
state_elapsed_time += delta_time;
switch (state) {
case AS_thinking:
{
if (state_elapsed_time > 1500 + rand() % 3000) {
int rnd = rand();
if (rnd % 100 < 30) {
ChangeToState(AS_moving);
} else if (rnd % 100 < 50) {
ChangeToState(AS_attack);
}
}
}
break;
case AS_moving:
{
if (state_elapsed_time < 1000 + rand() % 2000) {
DoMove();
} else {
int rnd = rand();
if (rnd % 100 < 30) {
ChangeToState(AS_thinking);
} else if (rnd % 100 < 50) {
ChangeToState(AS_attack);
}
}
}
break;
case AS_attack:
{
if (state_elapsed_time < 1000) {
DoAttack();
} else {
int rnd = rand();
if (rnd % 100 < 30) {
ChangeToState(AS_moving);
} else if (rnd % 100 < 50) {
ChangeToState(AS_thinking);
}
}
}
break;
}
}
void AndroidAI::ChangeToState(AndroidState_e to_state)
{
state = to_state;
state_elapsed_time = 0;
switch (state) {
case AS_moving:
{
Human* hum = (Human*)owner;
hum->move_dir = Vector2D(1.0f, 0);
hum->move_dir.Rotate(a8::RandAngle());
hum->move_dir.Normalize();
hum->attack_dir = hum->move_dir;
}
break;
default:
break;
}
}
void AndroidAI::DoMove()
{
#if 1
Human* hum = (Human*)owner;
int speed = std::max(1, (int)hum->GetSpeed());
for (int i = 0; i < speed; ++i) {
Vector2D old_pos = hum->pos;
hum->pos = hum->pos + hum->move_dir;
if (hum->IsCollision()) {
hum->pos = old_pos;
if (i == 0) {
hum->FindPath();
}
break;
}
}
#else
if (owner->movement) {
if (owner->movement->Arrived()) {
float distance = 8.0f + rand() % 10;
Vector2D out_pos;
if (owner->room->RandomPos((Human*)owner, distance, out_pos)) {
Human* hum = (Human*)owner;
hum->movement->ClearPath();
hum->movement->AddPathPoint(out_pos, distance, owner->GetSpeed());
hum->attack_dir = out_pos - owner->pos;
hum->attack_dir.Normalize();
}
}
}
#endif
}
void AndroidAI::DoAttack()
{
if (owner->updated_times % 2 == 0) {
Human* enemy = owner->room->FindEnemy((Human*)owner);
if (enemy) {
Human* sender = (Human*)owner;
Vector2D shot_dir = enemy->pos;
shot_dir.Normalize();
sender->Shot(shot_dir);
}
}
}