Skip to content

Commit 90959a2

Browse files
committed
I decided not to consider npc blocking condition
1 parent 6421675 commit 90959a2

File tree

4 files changed

+45
-9
lines changed

4 files changed

+45
-9
lines changed

AIControl.cpp

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "AIControl.h"
2-
2+
#include "ActorStateMachine.h"
33
using namespace std;
44
extern char debug[1024];
55

@@ -27,7 +27,7 @@ void AIControl::PlayAction(int skip){
2727
npcStateMachineList[i]->PlayAction(skip);
2828
}
2929
}
30-
float AIControl::distanceBetweenLyubu() {
30+
void AIControl::moveTowardLyubu() {
3131
for (int i = 0;i< this->npcStateMachineList.size(); i++){
3232
int npcId = npcStateMachineList[i]->character;
3333

@@ -47,9 +47,40 @@ float AIControl::distanceBetweenLyubu() {
4747
+ (npcPos[0] - lyubuPos[0]) * (npcPos[0] - lyubuPos[0])
4848
+ (npcPos[0] - lyubuPos[0]) * (npcPos[0] - lyubuPos[0]));
4949

50-
sprintf(debug, "%s distance = %f\n",debug,distance);
51-
52-
return distance;
50+
//sprintf(debug, "%s distance = %f\n",debug,distance);
51+
52+
if (distance > ATTACK_DISTANCE && distance < KEEP_TRACK_DISTANCE) {
53+
//turn toward lyubu
54+
float newFDir[3], normalize;
55+
newFDir[0] = lyubuPos[0] - npcPos[0];
56+
newFDir[1] = lyubuPos[1] - npcPos[1];
57+
newFDir[2] = lyubuPos[2] - npcPos[2];
58+
normalize = sqrt(newFDir[0] * newFDir[0] + newFDir[1] * newFDir[1] + newFDir[2] * newFDir[2]);
59+
newFDir[0] /= normalize;
60+
newFDir[1] /= normalize;
61+
newFDir[2] /= normalize;
5362

63+
float npcFDir[3], npcUDir[3];
64+
npc.GetWorldDirection(npcFDir, npcUDir);
65+
npc.SetWorldDirection(newFDir, npcUDir);
66+
67+
//move forward
68+
int block = npc.MoveForward(MOVE_DISTANCE,TRUE, FALSE, 0.0, TRUE);
69+
if (block) {
70+
//sprintf(debug, "%s npc is blocked\n",debug);
71+
while (npc.MoveForward(MOVE_DISTANCE,TRUE, FALSE, 0.0, TRUE)) {
72+
sprintf(debug, "%s npc turn right\n",debug);
73+
npc.TurnRight(300);
74+
npc.MoveForward(MOVE_DISTANCE,TRUE, FALSE, 0.0, TRUE);
75+
}
76+
}
77+
npcStateMachineList[i]->ChangeState(STATERUN, FALSE);
78+
}
79+
else if (distance <= ATTACK_DISTANCE) {
80+
npcStateMachineList[i]->AppendAttackCode(NORMAL_ATT);
81+
}
82+
else {
83+
npcStateMachineList[i]->ChangeState(STATEIDLE, TRUE);
84+
}
5485
}
55-
}
86+
}

AIControl.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
#include "ActorStateMachine.h"
44
#include <vector>
55

6+
#define MOVE_DISTANCE 5
7+
#define KEEP_TRACK_DISTANCE 1500
8+
#define ATTACK_DISTANCE 50
9+
10+
611
//class AIControl :
712
//public GameControl
813
class AIControl
@@ -14,7 +19,7 @@ class AIControl
1419
virtual ~AIControl(void);
1520
int AddNPC(ACTORid ncp, char * ActionFilename);
1621
void PlayAction(int skip);
17-
float distanceBetweenLyubu();
22+
void moveTowardLyubu();
1823
private:
1924
int lyubuId;
2025
};

BattleRoom.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void BattleRoom::RefreshArena(){
3131
for (int i =0;i< this->npcStateMachineList.size();i++){
3232
npc.Object(this->npcStateMachineList[i]->character);
3333
npc.GetWorldPosition(npcPos);
34-
this->npcStateMachineList[i]->AppendAttackCode(NORMAL_ATT);
34+
//this->npcStateMachineList[i]->AppendAttackCode(NORMAL_ATT);
3535

3636
if (this->CheckDistanceAndState(playerPos, npcPos,
3737
this->playerStateMachine->state, this->npcStateMachineList[i]->state ) == TRUE){

Main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ void GameAI(int skip)
447447
kc->Command();
448448
bRoom->RefreshArena();
449449
kc->CamPointToActor();
450-
npc->distanceBetweenLyubu();
450+
npc->moveTowardLyubu();
451451

452452
}
453453

0 commit comments

Comments
 (0)