Skip to content

Commit 1995a76

Browse files
committed
Merge pull request #11 from trantorLiu/master
I decided not to consider npc blocking
2 parents ba9ff3f + 90959a2 commit 1995a76

File tree

4 files changed

+76
-5
lines changed

4 files changed

+76
-5
lines changed

AIControl.cpp

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#include "AIControl.h"
2+
#include "ActorStateMachine.h"
3+
using namespace std;
4+
extern char debug[1024];
25

3-
4-
AIControl::AIControl(void)
6+
AIControl::AIControl(int id)
57
{
8+
this->lyubuId = id;
69
}
710

811

@@ -23,4 +26,61 @@ void AIControl::PlayAction(int skip){
2326
for (int i = 0;i< this->npcStateMachineList.size(); i++){
2427
npcStateMachineList[i]->PlayAction(skip);
2528
}
26-
}
29+
}
30+
void AIControl::moveTowardLyubu() {
31+
for (int i = 0;i< this->npcStateMachineList.size(); i++){
32+
int npcId = npcStateMachineList[i]->character;
33+
34+
FnActor lyubu;
35+
FnActor npc;
36+
lyubu.Object(this->lyubuId);
37+
npc.Object(npcId);
38+
39+
float lyubuPos[3];
40+
float npcPos[3];
41+
42+
lyubu.GetWorldPosition(lyubuPos);
43+
npc.GetWorldPosition(npcPos);
44+
45+
float distance;
46+
distance = sqrt((npcPos[0] - lyubuPos[0]) * (npcPos[0] - lyubuPos[0])
47+
+ (npcPos[0] - lyubuPos[0]) * (npcPos[0] - lyubuPos[0])
48+
+ (npcPos[0] - lyubuPos[0]) * (npcPos[0] - lyubuPos[0]));
49+
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;
62+
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+
}
85+
}
86+
}

AIControl.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,24 @@
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
914
{
1015
public:
16+
AIControl(int);
1117
std::vector<ActorStateMachine *> npcStateMachineList;
1218
AIControl(void);
1319
virtual ~AIControl(void);
1420
int AddNPC(ACTORid ncp, char * ActionFilename);
1521
void PlayAction(int skip);
22+
void moveTowardLyubu();
23+
private:
24+
int lyubuId;
1625
};
1726

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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ BOOL initNPC(){
301301
}
302302

303303

304-
npc = new AIControl();
304+
npc = new AIControl(lyubu);
305305
npc->AddNPC(donzo,"Data\\DozonAction.txt");
306306
npc->AddNPC(robber,"Data\\Robber02Action.txt");
307307
return TRUE;
@@ -447,6 +447,8 @@ void GameAI(int skip)
447447
kc->Command();
448448
bRoom->RefreshArena();
449449
kc->CamPointToActor();
450+
npc->moveTowardLyubu();
451+
450452
}
451453

452454
void Render(int skip){

0 commit comments

Comments
 (0)