Skip to content

Commit e14dc52

Browse files
unknownunknown
authored andcommitted
Merge branch 'master' of [email protected]:wingzero0/GameProgramming.git into ai
2 parents 1277025 + 3d73152 commit e14dc52

File tree

7 files changed

+123
-11
lines changed

7 files changed

+123
-11
lines changed

BattleRoom.cpp

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ void BattleRoom::RefreshArena(){
3535
if (this->CheckDistanceAndState(playerPos, npcPos,
3636
this->playerStateMachine->state, this->npcStateMachineList[i]->state ) == TRUE){
3737
this->JoinArena( this->npcStateMachineList[i] );
38+
this->npcStateMachineList[i]->ChangeState(STATEATTACK,TRUE);
3839
}
3940
}
41+
//sprintf(debug, "%s npc state machine list size = %d\n",debug,this->npcStateMachineList.size());
4042
this->PerformAttack();
4143
}
4244

@@ -72,19 +74,19 @@ void BattleRoom::PerformAttack(){
7274
actor.Object(this->playerStateMachine->character);
7375
float pPos[3];
7476
float pDir[3];
75-
actor.GetWorldDirection(NULL,pDir);
77+
//float pUDir[3];
78+
actor.GetWorldDirection(pDir,NULL);
7679
actor.GetWorldPosition(pPos);
7780

7881
FnActor npc;
7982
float nPos[3];
8083

8184
for (int i= 0;i< this->AreanList.size();i++){
8285
ACTORid tmpid = AreanList[i]->character;
83-
npc.Object(tmpid);
84-
npc.GetWorldPosition(nPos);
85-
86-
if (this->AttackCheck(pDir, pPos, nPos, BATTLE_RADIUS / 2) == TRUE){// lyubu attack area is the half of Battle raidus
87-
if (playerHitMap.find(tmpid) == playerHitMap.end()){
86+
if (playerHitMap.find(tmpid) == playerHitMap.end()){
87+
npc.Object(tmpid);
88+
npc.GetWorldPosition(nPos);
89+
if (this->AttackCheck(pDir, pPos, nPos, BATTLE_RADIUS / 1.5) == TRUE){// lyubu attack area is the half of Battle raidus
8890
// get a new victim;
8991
sprintf(debug, "%s new victim\n",debug);
9092
if ( this->AreanList[i]->state != STATEDIE){
@@ -98,5 +100,29 @@ void BattleRoom::PerformAttack(){
98100
}
99101

100102
BOOL BattleRoom::AttackCheck(float attackerDir[3], float attackerPos[3], float vitimPos[3], float attackRange){
101-
return TRUE;// for test
103+
float dist = 0.0;
104+
for (int i = 0;i< 3;i++){
105+
dist += (attackerPos[i] - vitimPos[i]) * (attackerPos[i] - vitimPos[i]);
106+
}
107+
//sprintf(debug, "%s dist = %lf\n",debug,dist);
108+
if ( dist >= attackRange){
109+
return FALSE;
110+
}
111+
float cosine,dotProduct;
112+
//float v[3];
113+
dotProduct = 0.0;
114+
for (int i = 0;i< 3;i++){
115+
dotProduct += (vitimPos[i] - attackerPos[i]) * attackerDir[i];
116+
}
117+
float length = 0.0;
118+
for (int i = 0;i< 3;i++){
119+
length += (vitimPos[i] - attackerPos[i])* (vitimPos[i] - attackerPos[i]);
120+
}
121+
cosine = dotProduct / length;
122+
//sprintf(debug, "%s cosine = %lf\n",debug,cosine);
123+
if (cosine >= 0){
124+
return TRUE;
125+
}else{
126+
return FALSE;
127+
}
102128
}

Data/Lyubu.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Actions 17
2+
Run for running
3+
CombatIdle idle pose for combat
4+
NormalAttack1 standard attack no. 1
5+
NormalAttack2 standard attack no. 2
6+
NormalAttack3 standard attack no. 3
7+
NormalAttack4 standard attack no. 4
8+
HeavyAttack1 heavy attack no. 1
9+
HeavyAttack2 heavy attack no. 2
10+
HeavyAttack3 heavy attack no. 3
11+
HeavyDamaged damage pose after attacked
12+
RightDamaged damage pose after attacked from right side
13+
LeftDamaged damage pose after attacked from left side
14+
Die for dying
15+
Idle idle
16+
Walk for walking
17+
guard defense from attacks
18+
UltimateAttack ultimate attack

Data/Robber02Action.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Run Run
2+
CombatIdle CombatIdle
3+
N1 NormalAttack1
4+
N2 NormalAttack2 H1 HeavyAttack1
5+
6+
HeavyDamage Damage1
7+
LightDamage Damage2
8+
9+
Die Dead

Data/donzo.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Actions 10
2+
CombatIdle idle for combats
3+
AttackL1 attack pose no. 1
4+
AttackL2 attack pose no. 2
5+
DamageL damage pose for attacking (low damage)
6+
DamageH damage pose for attacking (high damage)
7+
Run for running
8+
AttackH heavy attack
9+
Defance defense pose
10+
Die die pose
11+
HeavyAttack another heavy attack

Data/robber02.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Actions 10
2+
CombatIdle idle pose for combats
3+
MoveRight move to right
4+
MoveLeft move to left
5+
NormalAttack1 attack pose no. 1
6+
NormalAttack2 attack pose no. 2
7+
HeavyAttack1 heavy attack
8+
Damage1 damage pose no. 1
9+
Damage2 damage pose no. 2
10+
Dead for dying
11+
Run for running

LyubuStateMachine.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,13 @@ BOOL LyubuStateMachine::UpdateEffectiveAttack(){
9090
actor.Object(this->character);
9191
float frame = actor.QueryCurrentFrame(0);
9292
//sprintf(debug, "%s frame:%lf\n", debug, frame );
93-
if (this->currentAttackIndex > 0 || this->attackKeyQueue[currentAttackIndex] == HEAVY_ATT){
93+
if (this->attackKeyQueue[currentAttackIndex]){
94+
if (frame > 20.0){
95+
this->effectiveAttack = TRUE;
96+
}
97+
}else if (this->currentAttackIndex > 0){
9498
if (frame > 10.0){
9599
this->effectiveAttack = TRUE;
96-
}else{
97-
this->effectiveAttack = FALSE;
98100
}
99101
}else{
100102
this->effectiveAttack = TRUE;

Main.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ SCENEid sID;
2323
OBJECTid nID, cID, lID;
2424
OBJECTid tID;
2525
ACTORid lyubu;
26-
ACTORid donzo;
26+
ACTORid donzo, robber;
2727
KeyboardControl *kc;
2828
AIControl *npc;
2929
BattleRoom *bRoom;
@@ -266,8 +266,43 @@ BOOL initNPC(){
266266
sprintf(debug, "%s play action failed\n", debug);
267267
}
268268

269+
robber = scene.LoadActor("Robber02");
270+
if (robber == FAILED_ID){
271+
return FALSE;
272+
}
273+
FnActor actor_robber;
274+
actor_robber.Object(robber);
275+
float pos_robber[3];
276+
pos_robber[0] = 3569.0;
277+
pos_robber[1] = -3010;
278+
pos_robber[2] = 100;
279+
actor_robber.SetPosition(pos_robber);
280+
281+
flag = actor_robber.PutOnTerrain(tID,FALSE,0.0);
282+
283+
if (flag == FALSE){
284+
sprintf(debug, "%s put on fail\n", debug);
285+
return FALSE;
286+
}
287+
// set donzo idle action
288+
ACTIONid idleID_robber = actor_robber.GetBodyAction(NULL,"CombatIdle");
289+
290+
//actor.MakeCurrentAction(0,NULL,idleID,0.0,TRUE);
291+
//if (actor.MakeCurrentAction(0,NULL,FAILED_ID) == FAILED_ID){
292+
if (actor_robber.MakeCurrentAction(0,NULL,idleID_robber) == FAILED_ID){
293+
sprintf(debug, "%s make current fail\n", debug);
294+
}else{
295+
sprintf(debug, "%s make action success\n", debug);
296+
}
297+
298+
if (actor_robber.Play(0,START, 0.0, FALSE,TRUE) == FALSE){
299+
sprintf(debug, "%s play action failed\n", debug);
300+
}
301+
302+
269303
npc = new AIControl();
270304
npc->AddNPC(donzo,"Data\\DozonAction.txt");
305+
npc->AddNPC(robber,"Data\\Robber02Action.txt");
271306
return TRUE;
272307
}
273308

0 commit comments

Comments
 (0)