Skip to content

Commit 493ce2f

Browse files
committed
Merge pull request #12 from trantorLiu/master
Add more npcs
2 parents f69102f + 40c36df commit 493ce2f

File tree

4 files changed

+86
-37
lines changed

4 files changed

+86
-37
lines changed

AIControl.cpp

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#include "AIControl.h"
22
#include "ActorStateMachine.h"
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <time.h>
6+
37
using namespace std;
48
extern char debug[1024];
59

@@ -47,13 +51,19 @@ void AIControl::moveTowardLyubu() {
4751
float distance;
4852
distance = sqrt((npcPos[0] - lyubuPos[0]) * (npcPos[0] - lyubuPos[0])
4953
+ (npcPos[1] - lyubuPos[1]) * (npcPos[1] - lyubuPos[1]));
50-
//+ (npcPos[0] - lyubuPos[0]) * (npcPos[0] - lyubuPos[0]));
54+
//+ (npcPos[2] - lyubuPos[2]) * (npcPos[2] - lyubuPos[2]));
5155

52-
//sprintf(debug, "%s distance = %f\n",debug,distance);
56+
//sprintf(debug, "%s x = %f y = %f z = %f\n",debug, lyubuPos[0], lyubuPos[1], lyubuPos[2]);
5357

5458
if (distance > ATTACK_DISTANCE && distance < KEEP_TRACK_DISTANCE) {
5559
//turn toward lyubu
56-
float newFDir[3], normalize;
60+
float newFDir[3], normalize, offset;
61+
srand ( time(NULL) );
62+
offset = rand() % NPC_MOVE_OFFSET + 1;
63+
lyubuPos[0] += offset;
64+
lyubuPos[1] += offset;
65+
lyubuPos[2] += offset;
66+
5767
newFDir[0] = lyubuPos[0] - npcPos[0];
5868
newFDir[1] = lyubuPos[1] - npcPos[1];
5969
newFDir[2] = lyubuPos[2] - npcPos[2];
@@ -79,6 +89,21 @@ void AIControl::moveTowardLyubu() {
7989
npcStateMachineList[i]->ChangeState(STATERUN, FALSE);
8090
}
8191
else if (distance <= ATTACK_DISTANCE) {
92+
//before attack, turn toward lyubu
93+
float newFDir[3], normalize, offset;
94+
95+
newFDir[0] = lyubuPos[0] - npcPos[0];
96+
newFDir[1] = lyubuPos[1] - npcPos[1];
97+
newFDir[2] = lyubuPos[2] - npcPos[2];
98+
normalize = sqrt(newFDir[0] * newFDir[0] + newFDir[1] * newFDir[1] + newFDir[2] * newFDir[2]);
99+
newFDir[0] /= normalize;
100+
newFDir[1] /= normalize;
101+
newFDir[2] /= normalize;
102+
103+
float npcFDir[3], npcUDir[3];
104+
npc.GetWorldDirection(npcFDir, npcUDir);
105+
npc.SetWorldDirection(newFDir, npcUDir);
106+
82107
//sprintf(debug, "%s distance = %f\n",debug,distance);
83108
npcStateMachineList[i]->AppendAttackCode(NORMAL_ATT);
84109
}

AIControl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
#define MOVE_DISTANCE 5
77
#define KEEP_TRACK_DISTANCE 1500
8-
#define ATTACK_DISTANCE 50
9-
8+
#define ATTACK_DISTANCE 100
9+
#define NPC_MOVE_OFFSET 100
1010

1111
//class AIControl :
1212
//public GameControl

Data/npcPos.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
3889 -3418
2+
3842 -3381
3+
3926 -3257

Main.cpp

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ Last Updated : 1010, 2004, C.Wang
1515
#include "BattleRoom.h"
1616
#include "FyFx.h"
1717
#include "FyMedia.h"
18+
#include <iostream>
19+
#include <fstream>
20+
#include <string>
21+
using namespace std;
22+
extern int errno;
1823

1924
int oldX, oldY, oldXM, oldYM, oldXMM, oldYMM;
2025

@@ -24,7 +29,8 @@ SCENEid sID;
2429
OBJECTid nID, cID, lID;
2530
OBJECTid tID;
2631
ACTORid lyubu;
27-
ACTORid donzo, robber;
32+
ACTORid donzo, robbers[100];
33+
int robbers_count = 0;
2834
KeyboardControl *kc;
2935
AIControl *npc;
3036
BattleRoom *bRoom;
@@ -266,44 +272,56 @@ BOOL initNPC(){
266272
if (actor.Play(0,START, 0.0, FALSE,TRUE) == FALSE){
267273
sprintf(debug, "%s play action failed\n", debug);
268274
}
269-
270-
robber = scene.LoadActor("Robber02");
271-
if (robber == FAILED_ID){
272-
sprintf(debug, "%s Robber02 load fail\n", debug);
273-
return FALSE;
275+
276+
ifstream fin;
277+
fin.open("Data\\npcPos.txt");
278+
if (!fin.is_open()) {
279+
sprintf(debug, "%s open file error\n", debug);
280+
sprintf(debug, "%s %s\n", debug, strerror(errno));
274281
}
275-
FnActor actor_robber;
276-
actor_robber.Object(robber);
277-
float pos_robber[3];
278-
pos_robber[0] = 3569.0;
279-
pos_robber[1] = -3010;
282+
float pos_robber[3];
280283
pos_robber[2] = 100;
281-
actor_robber.SetPosition(pos_robber);
284+
int i = 0;
285+
FnActor actor_robbers[100];
286+
while (fin >> pos_robber[0] && fin >> pos_robber[1]) {
287+
sprintf(debug, "%s in the loading npc while loop\n", debug);
288+
robbers[i] = scene.LoadActor("Robber02");
289+
if (robbers[i] == FAILED_ID){
290+
sprintf(debug, "%s Robber02 load fail\n", debug);
291+
return FALSE;
292+
}
282293

283-
flag = actor_robber.PutOnTerrain(tID,FALSE,0.0);
294+
actor_robbers[i].Object(robbers[i]);
295+
actor_robbers[i].SetPosition(pos_robber);
296+
297+
flag = actor_robbers[i].PutOnTerrain(tID,FALSE,0.0);
284298

285-
if (flag == FALSE){
286-
return FALSE;
287-
}
288-
// set donzo idle action
289-
ACTIONid idleID_robber = actor_robber.GetBodyAction(NULL,"CombatIdle");
290-
291-
//actor.MakeCurrentAction(0,NULL,idleID,0.0,TRUE);
292-
//if (actor.MakeCurrentAction(0,NULL,FAILED_ID) == FAILED_ID){
293-
if (actor_robber.MakeCurrentAction(0,NULL,idleID_robber) == FAILED_ID){
294-
sprintf(debug, "%s make current fail\n", debug);
295-
}else{
296-
sprintf(debug, "%s make action success\n", debug);
297-
}
299+
if (flag == FALSE){
300+
return FALSE;
301+
}
302+
ACTIONid idleID_robber = actor_robbers[i].GetBodyAction(NULL,"CombatIdle");
298303

299-
if (actor_robber.Play(0,START, 0.0, FALSE,TRUE) == FALSE){
300-
sprintf(debug, "%s play action failed\n", debug);
301-
}
302-
304+
//actor.MakeCurrentAction(0,NULL,idleID,0.0,TRUE);
305+
//if (actor.MakeCurrentAction(0,NULL,FAILED_ID) == FAILED_ID){
306+
if (actor_robbers[i].MakeCurrentAction(0,NULL,idleID_robber) == FAILED_ID){
307+
sprintf(debug, "%s make current fail\n", debug);
308+
}else{
309+
sprintf(debug, "%s make action success\n", debug);
310+
}
311+
312+
if (actor_robbers[i].Play(0,START, 0.0, FALSE,TRUE) == FALSE){
313+
sprintf(debug, "%s play action failed\n", debug);
314+
}
315+
i++;
316+
}
317+
robbers_count = i;
303318

304319
npc = new AIControl(lyubu);
305320
npc->AddNPC(donzo,"Data\\DozonAction.txt");
306-
npc->AddNPC(robber,"Data\\Robber02Action.txt");
321+
322+
for (i = 0; i < robbers_count; i++) {
323+
npc->AddNPC(robbers[i],"Data\\Robber02Action.txt");
324+
}
307325
return TRUE;
308326
}
309327

@@ -349,7 +367,10 @@ void Reset(WORLDid gID, BYTE code, BOOL value){
349367
scene.Object(sID);
350368
scene.DeleteActor(lyubu);
351369
scene.DeleteActor(donzo);
352-
scene.DeleteActor(robber);
370+
int i;
371+
for (i = 0; i < robbers_count; i++) {
372+
scene.DeleteActor(robbers[i]);
373+
}
353374
debug[0] = '\0';
354375
ActorStateMachine * lyubuState = kc->mainChar;
355376
delete lyubuState;

0 commit comments

Comments
 (0)