-
Notifications
You must be signed in to change notification settings - Fork 0
/
AIFOLPTH.CPP
398 lines (350 loc) · 9.46 KB
/
AIFOLPTH.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* ========================================================================
Copyright (c) 1990,1996 Synergistic Software
All Rights Reserved
========================================================================
Filename: aifolpth.cpp
Author: Greg Hightower
======================================================================== */
/* -----------------------------------------------------------------
additional includes
----------------------------------------------------------------- */
#include "AIFOLPTH.HXX"
#include "FILEUTIL.H"
#include "AVATAR.HXX"
/* -----------------------------------------------------------------
class definition
----------------------------------------------------------------- */
/* -----------------------------------------------------------------
inline member functions
----------------------------------------------------------------- */
void FOLLOW_PATH_DATA::mfWriteData(FILE * fp)
{
fprintf(fp, "%ld\n", PathCount);
if (PathCount > 0)
{
LONG i;
PTR_PATH_PTS pPathPt = (PTR_PATH_PTS) BLKPTR(hPath);
for (i = 0 ; i < PathCount; i++)
{
pPathPt[i].mfWriteData(fp);
}
}
}
LONG FOLLOW_PATH_DATA::mfReadData(FILE *fp)
{
char cpBuffer[80];
LONG Result = GetNextLine(fp, cpBuffer, sizeof(cpBuffer));
PathIndex = 0;
oldP.x = 0;
oldP.y = 0;
if (1 == sscanf(cpBuffer, "%ld", &PathCount))
{
if (PathCount > 0)
{
hPath = NewBlock(sizeof(PATH_PT) * PathCount);
if (hPath != fERROR)
{
SHORT i;
SetBlockAttr(hPath,LOCKED, LOCKED);
PTR_PATH_PTS pPathPts = (PTR_PATH_PTS) BLKPTR(hPath);
for (i = 0; i < PathCount && Result != EOF; i++)
{
Result = pPathPts[i].mfReadData(fp);
}
if (i < PathCount)
{
// Premature end of file. Lets at least use bad data.
PathCount = i;
}
ClrLock(hPath);
}
else
{
PathCount = 0;
hPath = fERROR;
Result = EOF; // Hey we're nearly out of memory anyway.
}
}
}
else
{
PathCount = 0;
hPath = fERROR;
Result = EOF;
}
return Result;
}
BOOL FOLLOW_PATH_DATA::mfGetNextPt(LONG &X, LONG &Y, LONG &Z, LONG &A)
{
BOOL retVal = TRUE;
if (hPath != fERROR)
{
PathIndex++;
if (PathIndex >= PathCount)
{
PathIndex = 0;
retVal = FALSE;
}
PTR_PATH_PTS pPathPts = (PTR_PATH_PTS) BLKPTR(hPath);
X = pPathPts[PathIndex].X;
Y = pPathPts[PathIndex].Y;
Z = pPathPts[PathIndex].Z;
A = pPathPts[PathIndex].A;
}
else
{
retVal = FALSE;
}
return retVal;
}
/* ========================================================================
Function - FollowPath
Description - AI function to follow a point path from a file on disk
Returns - Current state
======================================================================== */
void CAvatar::FollowPath (CAvatar *pAvatar, CAvatar::AISTATUS Status)
{
pAvatar->attrib.IsFollowPath = TRUE;
switch( Status )
{
case AI_INIT:
pAvatar->fFollowPath.Wave = 0;
if (pAvatar->fFollowPath.PathCount > 0)
{
//LONG X;
//LONG Y;
//LONG Z;
//LONG A;
//pAvatar->fFollowPath.mfGetCurrentPt(X, Y, Z, A);
pAvatar->Status = AI_MOVING;
if(pAvatar->mfType() == WYVERN_1 ||
pAvatar->mfType() == T_WYVERN_1 ||
pAvatar->mfType() == WYVERN_2 ||
pAvatar->mfType() == T_WYVERN_2)
{
pAvatar->mfStartAnimationLoop(MARCHSEQ);
}
else
{
pAvatar->mfStartAnimationLoop( WALKSEQ);
}
}
else
{
pAvatar->mfStartAnimationLoop( STANDSEQ);
}
break;
case AI_RELEASE:
pAvatar->Status = AI_INIT;
break;
case AI_MOVING:
{
// only x and y work right now
LONG X;
LONG Y;
LONG Z;
LONG A;
PLAYER TempPlayer;
LONG Rate;
FIXED_VECTOR_3D NewVector;
WadThingType bump;
LONG tmpA;
LONG distance;
SHORT count=0;
LONG BumpDistance;
if(pAvatar->mfType() == WYVERN_1||
pAvatar->mfType() == T_WYVERN_1 ||
pAvatar->mfType() == T_WYVERN_2 ||
pAvatar->mfType() == WYVERN_2)
{
Rate = pAvatar->mfGetMarchRate();
}
else
{
Rate = pAvatar->mfGetWalkRate();
}
pAvatar->hEnemy = pAvatar->mfFindClosestFoe();
if (pAvatar->hEnemy != fERROR)
{
CAvatar * pAvatarTarget = (CAvatar *) BLKPTR(pAvatar->hEnemy);
distance = aprox_dist(
pAvatar->mfX(), pAvatar->mfY(),
pAvatarTarget->mfX(), pAvatarTarget->mfY());
if ( pAvatar->mfCanSeeAvatar(pAvatarTarget)
&&distance <= CAvatar::ActivationDistance)
{
pAvatar->fFollowPath.oldP.x = pAvatar->mfX();
pAvatar->fFollowPath.oldP.y = pAvatar->mfY();
switch (pAvatar->mfType())
{
case GIANT_SPIDER:
case T_GIANT_SPIDER:
case CEILING_SPIDER:
case T_CEILING_SPIDER:
pAvatar->SetAIFuncIndex(CAvatar::AI_FUNC_SPIDERS);
break;
case HARPY:
case T_HARPY:
pAvatar->SetAIFuncIndex(CAvatar::AI_FUNC_HARPIES);
break;
case WYVERN_1:
case T_WYVERN_1:
case WYVERN_2:
case T_WYVERN_2:
pAvatar->SetAIFuncIndex(CAvatar::AI_FUNC_WYVERNS);
break;
case DOG:
case T_DOG:
case HELL_HOUND:
case T_HELL_HOUND:
pAvatar->SetAIFuncIndex(CAvatar::AI_FUNC_HELLHOUND);
break;
default:
pAvatar->SetAIFuncIndex(CAvatar::AI_FUNC_PING_PONG);
break;
}
pAvatar->mfInitVals();
pAvatar->DoAI(CAvatar::AI_INIT);
break;
}
else
goto Continue_Follow_Path;
}
else
{
Continue_Follow_Path:
pAvatar->fFollowPath.mfGetCurrentPt(X, Y, Z, A);
if( ( pAvatar->mfX() == X ) &&
( pAvatar->mfY() == Y ) )
{
// You've made it to the new location, get the next
// requested destination.
pAvatar->fFollowPath.mfGetNextPt(X, Y, Z, A);
pAvatar->fFollowPath.oldP.x = 0;
pAvatar->fFollowPath.oldP.y = 0;
}
pAvatar->mfConvertPositionToPlayer(TempPlayer);
if(pAvatar->fFollowPath.oldP.x != 0 || pAvatar->fFollowPath.oldP.y != 0)
{
distance = aprox_dist(pAvatar->mfX(), pAvatar->mfY(), X, Y);
tmpA = AngleFromPoint2(pAvatar->mfX(), pAvatar->mfY(), X, Y, 0);
bump=CheckLongMove(&TempPlayer, tmpA, distance, CHECKLINE_MONSTER, Z, &BumpDistance);
while (bump!= iNOTHING && count >= pAvatar->fFollowPath.PathCount)
{
pAvatar->fFollowPath.mfGetNextPt(X, Y, Z, A);
distance = aprox_dist(pAvatar->mfX(), pAvatar->mfY(), X, Y);
tmpA = AngleFromPoint2(pAvatar->mfX(), pAvatar->mfY(), X, Y, 0);
bump=CheckLongMove(&TempPlayer, tmpA, distance, CHECKLINE_MONSTER, Z, &BumpDistance);
count++;
}
if(count >= pAvatar->fFollowPath.PathCount) // couldn't get back to the path, change to pong
{
switch (pAvatar->mfType())
{
case GIANT_SPIDER:
case T_GIANT_SPIDER:
case CEILING_SPIDER:
case T_CEILING_SPIDER:
pAvatar->SetAIFuncIndex(CAvatar::AI_FUNC_SPIDERS);
break;
case HARPY:
case T_HARPY:
pAvatar->SetAIFuncIndex(CAvatar::AI_FUNC_HARPIES);
break;
case WYVERN_1:
case T_WYVERN_1:
case WYVERN_2:
case T_WYVERN_2:
pAvatar->SetAIFuncIndex(CAvatar::AI_FUNC_WYVERNS);
break;
case DOG:
case T_DOG:
case HELL_HOUND:
case T_HELL_HOUND:
pAvatar->SetAIFuncIndex(CAvatar::AI_FUNC_HELLHOUND);
break;
default:
pAvatar->SetAIFuncIndex(CAvatar::AI_FUNC_PING_PONG);
break;
}
pAvatar->mfInitVals();
pAvatar->DoAI(CAvatar::AI_INIT);
pAvatar->attrib.IsFollowPath = FALSE;
break;
}
pAvatar->FaceTo(X, Y);
}
const LONG dx = X - pAvatar->mfX();
const LONG dy = Y - pAvatar->mfY();
const LONG dz = Z - pAvatar->mfZ();
distance = aprox_dist(
pAvatar->mfX(), pAvatar->mfY(),
X, Y);
if (ABS(distance) < Rate)
{
NewVector.dx = dx;
NewVector.dy = dy;
NewVector.dz = dz;
}
else
{
NewVector.dx = ((dx * Rate)/distance);
NewVector.dy = ((dy * Rate)/distance);
NewVector.dz = ((dz * Rate)/distance);
}
NewVector.dx <<= PLAYER_FIXEDPT;
NewVector.dy <<= PLAYER_FIXEDPT;
//if (pAvatar->mfIsVisible())
//{
CheckBump(&TempPlayer, &NewVector, pAvatar->mfGetWalkRate());
//}
if(pAvatar->fFollowPath.oldP.x != 0 || pAvatar->fFollowPath.oldP.y != 0)
pAvatar->mfMoveToXYZA(
pAvatar->mfX() + PLAYER_INT_VAL(NewVector.dx),
pAvatar->mfY() + PLAYER_INT_VAL(NewVector.dy),
pAvatar->mfZ() + NewVector.dz,
pAvatar->mfAngle());
else
pAvatar->mfMoveToXYZA(
pAvatar->mfX() + PLAYER_INT_VAL(NewVector.dx),
pAvatar->mfY() + PLAYER_INT_VAL(NewVector.dy),
pAvatar->mfZ() + NewVector.dz,
A);
}
}
break;
case AI_BEGIN_PAUSE:
pAvatar->mfBeginPause();
break;
case AI_PAUSED:
pAvatar->mfStandAndFidget();
break;
case AI_END_PAUSE:
pAvatar->mfEndPause();
break;
case AI_BEGIN_LISTEN:
pAvatar->mfBeginListen();
break;
case AI_LISTEN:
// Follow the camera around.
pAvatar->mfListen();
break;
case AI_END_LISTEN:
pAvatar->mfEndListen();
break;
case AI_BEGIN_LISTEN_BOW:
pAvatar->mfBeginListenBow();
break;
case AI_END_LISTEN_BOW:
pAvatar->mfEndListenBow();
break;
case AI_ROTATE_TO_CAMERA:
pAvatar->mfRotateToCamera();
break;
case AI_RETURN_TO_POSITION:
pAvatar->mfReturnToPosition();
break;
default:
pAvatar->Status = AI_PAUSED;
}
}