forked from wingzero0/GameProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AIControl.cpp
143 lines (125 loc) · 4.19 KB
/
AIControl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "AIControl.h"
#include "ActorStateMachine.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
extern char debug[1024];
AIControl::AIControl(ACTORid id)
{
this->lyubuId = id;
}
AIControl::~AIControl(void)
{
for (unsigned int i = 0;i< this->npcStateMachineList.size(); i++){
delete this->npcStateMachineList[i];
}
}
int AIControl::AddNPC(ACTORid npc, char * ActionFilename){
ActorStateMachine* stm = new ActorStateMachine(npc, ActionFilename);
this->npcStateMachineList.push_back(stm);
stm->life = 10;
stm->UpdateLifeBillboard();
return 0;
}
int AIControl::AddBossNPC(ACTORid npc, char * ActionFilename){
ActorStateMachine* stm = new ActorStateMachine(npc, ActionFilename);
this->npcStateMachineList.push_back(stm);
this->bossStateMachine = stm;
this->bossStateMachine->life = 100;
return 0;
}
void AIControl::PlayAction(int skip){
for (unsigned int i = 0;i< this->npcStateMachineList.size(); i++){
npcStateMachineList[i]->PlayAction(skip);
}
}
void AIControl::moveTowardLyubu() {
for (int i = 0;i< this->npcStateMachineList.size(); i++){
if (npcStateMachineList[i]->CanBeControl() == FALSE){
continue;
}
int npcId = npcStateMachineList[i]->character;
FnActor lyubu;
FnActor npc;
lyubu.Object(this->lyubuId);
npc.Object(npcId);
float lyubuPos[3];
float npcPos[3];
lyubu.GetWorldPosition(lyubuPos);
npc.GetWorldPosition(npcPos);
float distance;
distance = sqrt((npcPos[0] - lyubuPos[0]) * (npcPos[0] - lyubuPos[0])
+ (npcPos[1] - lyubuPos[1]) * (npcPos[1] - lyubuPos[1]));
//+ (npcPos[2] - lyubuPos[2]) * (npcPos[2] - lyubuPos[2]));
//sprintf(debug, "%s x = %f y = %f z = %f\n",debug, lyubuPos[0], lyubuPos[1], lyubuPos[2]);
if (distance > ATTACK_DISTANCE && ((npcStateMachineList[i] == this->bossStateMachine && distance < BOSS_KEEP_TRACK_DISTANCE) || distance < KEEP_TRACK_DISTANCE)) {
//turn toward lyubu
float newFDir[3], normalize, offset;
if (npcStateMachineList[i] != this->bossStateMachine) {
srand ( time(NULL) + i);
offset = rand() % NPC_MOVE_OFFSET + 1;
lyubuPos[0] += offset;
lyubuPos[1] += offset;
lyubuPos[2] += offset;
}
newFDir[0] = lyubuPos[0] - npcPos[0];
newFDir[1] = lyubuPos[1] - npcPos[1];
newFDir[2] = lyubuPos[2] - npcPos[2];
normalize = sqrt(newFDir[0] * newFDir[0] + newFDir[1] * newFDir[1] + newFDir[2] * newFDir[2]);
newFDir[0] /= normalize;
newFDir[1] /= normalize;
newFDir[2] /= normalize;
float npcFDir[3], npcUDir[3];
npc.GetWorldDirection(npcFDir, npcUDir);
npc.SetWorldDirection(newFDir, npcUDir);
//move forward
int block = npc.MoveForward(MOVE_DISTANCE,TRUE, FALSE, 0.0, TRUE);
if (block) {
//sprintf(debug, "%s npc is blocked\n",debug);
while (npc.MoveForward(MOVE_DISTANCE,TRUE, FALSE, 0.0, TRUE)) {
sprintf(debug, "%s npc turn right\n",debug);
npc.TurnRight(300);
npc.MoveForward(MOVE_DISTANCE,TRUE, FALSE, 0.0, TRUE);
}
}
npcStateMachineList[i]->ChangeState(STATERUN, FALSE);
}
else if (distance <= ATTACK_DISTANCE) {
//before attack, turn toward lyubu
float newFDir[3], normalize, offset;
newFDir[0] = lyubuPos[0] - npcPos[0];
newFDir[1] = lyubuPos[1] - npcPos[1];
newFDir[2] = lyubuPos[2] - npcPos[2];
normalize = sqrt(newFDir[0] * newFDir[0] + newFDir[1] * newFDir[1] + newFDir[2] * newFDir[2]);
newFDir[0] /= normalize;
newFDir[1] /= normalize;
newFDir[2] /= normalize;
float npcFDir[3], npcUDir[3];
npc.GetWorldDirection(npcFDir, npcUDir);
npc.SetWorldDirection(newFDir, npcUDir);
//sprintf(debug, "%s distance = %f\n",debug,distance);
srand ( time(NULL) + i);
float rate = rand() % 100;
if (this->npcStateMachineList[i] == this->bossStateMachine){
if (rate > GUARD_RATE ) {
npcStateMachineList[i]->AppendAttackCode(NORMAL_ATT);
npcStateMachineList[i]->AppendAttackCode(NORMAL_ATT);
npcStateMachineList[i]->AppendAttackCode(HEAVY_ATT);
}else{
npcStateMachineList[i]->CharacterSetGuard();
}
}else {
if (rate > GUARD_RATE * 2) {
npcStateMachineList[i]->AppendAttackCode(NORMAL_ATT);
}
else{
npcStateMachineList[i]->CharacterSetIdle();
}
}
}
else {
npcStateMachineList[i]->CharacterSetIdle();
}
}
}