game2001/server/gameserver/android.ai.cc
2019-04-13 10:46:25 +08:00

115 lines
2.8 KiB
C++

#include "precompile.h"
#include "android.ai.h"
#include "android.h"
#include "room.h"
#include "metamgr.h"
void AndroidAI::Update(int delta_time)
{
Human* hum = (Human*)owner;
if (hum->poisoning) {
hum->poisoning_time += delta_time;
}
state_elapsed_time += delta_time;
if (hum->poisoning) {
hum->UpdatePoisoning();
}
if (hum->dead) {
return;
}
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 (owner->updated_times % 2 == 0) {
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;
}
}
}
}
void AndroidAI::DoAttack()
{
if (owner->updated_times % 10 == 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);
}
}
}