AIZOMBA.CPP 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "aizomba.h"
  4. #include "actor.h"
  5. #include "db.h"
  6. #include "debug4g.h"
  7. #include "dude.h"
  8. #include "engine.h"
  9. #include "eventq.h"
  10. #include "gameutil.h"
  11. #include "globals.h"
  12. #include "misc.h"
  13. #include "multi.h"
  14. #include "player.h"
  15. #include "sfx.h"
  16. #include "trig.h"
  17. /****************************************************************************
  18. ** LOCAL CONSTANTS
  19. ****************************************************************************/
  20. #define kAxeZombieMeleeDist M2X(2.0) //M2X(1.6)
  21. /****************************************************************************
  22. ** LOCAL callback prototypes
  23. ****************************************************************************/
  24. static void HackCallback( int /* type */, int nXIndex );
  25. /****************************************************************************
  26. ** LOCAL think/move/entry function prototypes
  27. ****************************************************************************/
  28. static void thinkSearch( SPRITE *pSprite, XSPRITE *pXSprite );
  29. static void thinkGoto( SPRITE *pSprite, XSPRITE *pXSprite );
  30. static void thinkChase( SPRITE *pSprite, XSPRITE *pXSprite );
  31. static void thinkPonder( SPRITE *pSprite, XSPRITE *pXSprite );
  32. static void entryEZombie( SPRITE *pSprite, XSPRITE *pXSprite );
  33. static void entryAIdle( SPRITE */*pSprite*/, XSPRITE *pXSprite );
  34. static void myThinkTarget( SPRITE *pSprite, XSPRITE *pXSprite );
  35. static void myThinkSearch( SPRITE *pSprite, XSPRITE *pXSprite );
  36. /****************************************************************************
  37. ** GLOBAL CONSTANTS
  38. ****************************************************************************/
  39. AISTATE zombieAIdle = { kSeqDudeIdle, NULL, 0, entryAIdle, NULL, aiThinkTarget, NULL };
  40. AISTATE zombieAChase = { kSeqAxeZombieWalk, NULL, 0, NULL, aiMoveForward, thinkChase, NULL };
  41. AISTATE zombieAPonder = { kSeqDudeIdle, NULL, 0, NULL, aiMoveTurn, thinkPonder, NULL };
  42. AISTATE zombieAGoto = { kSeqAxeZombieWalk, NULL, 3600, NULL, aiMoveForward, thinkGoto, &zombieAIdle };
  43. AISTATE zombieAHack = { kSeqAxeZombieAttack, HackCallback, 120, NULL, NULL, NULL, &zombieAPonder };
  44. AISTATE zombieASearch = { kSeqAxeZombieWalk, NULL, 3600, NULL, aiMoveForward, thinkSearch, &zombieAIdle };
  45. AISTATE zombieARecoil = { kSeqDudeRecoil, NULL, 0, NULL, NULL, NULL, &zombieAPonder };
  46. AISTATE zombieEIdle = { kSeqEarthZombieIdle, NULL, 0, NULL, NULL, aiThinkTarget, NULL };
  47. AISTATE zombieEUp2 = { kSeqDudeIdle, NULL, 1, entryEZombie, NULL, NULL, &zombieASearch };
  48. AISTATE zombieEUp = { kSeqEarthZombieUp, NULL, 180, NULL, NULL, NULL, &zombieEUp2 };
  49. AISTATE zombie2Idle = { kSeqDudeIdle, NULL, 0, entryAIdle, NULL, myThinkTarget, NULL };
  50. AISTATE zombie2Search = { kSeqAxeZombieWalk, NULL, 3600, NULL, aiMoveForward, myThinkSearch, &zombie2Idle };
  51. /****************************************************************************
  52. ** LOCAL FUNCTIONS
  53. ****************************************************************************/
  54. /****************************************************************************
  55. ** TommyCallback()
  56. **
  57. **
  58. ****************************************************************************/
  59. static void HackCallback( int /* type */, int nXIndex )
  60. {
  61. XSPRITE *pXSprite = &xsprite[nXIndex];
  62. int nSprite = pXSprite->reference;
  63. SPRITE *pSprite = &sprite[nSprite];
  64. int dx = Cos(pSprite->ang) >> 16;
  65. int dy = Sin(pSprite->ang) >> 16;
  66. int dz = 0;
  67. actFireVector(nSprite, pSprite->z, dx, dy, dz, kVectorAxe);
  68. }
  69. /****************************************************************************
  70. ** thinkSearch()
  71. **
  72. **
  73. ****************************************************************************/
  74. static void thinkSearch( SPRITE *pSprite, XSPRITE *pXSprite )
  75. {
  76. aiChooseDirection(pSprite, pXSprite, pXSprite->goalAng);
  77. aiThinkTarget(pSprite, pXSprite);
  78. }
  79. /****************************************************************************
  80. ** thinkGoto()
  81. **
  82. **
  83. ****************************************************************************/
  84. static void thinkGoto( SPRITE *pSprite, XSPRITE *pXSprite )
  85. {
  86. int dx, dy, dist;
  87. dassert(pSprite->type >= kDudeBase && pSprite->type < kDudeMax);
  88. DUDEINFO *pDudeInfo = &dudeInfo[pSprite->type - kDudeBase];
  89. SPRITE *pTarget = &sprite[pXSprite->target];
  90. XSPRITE *pXTarget = &xsprite[pTarget->extra];
  91. dx = pXSprite->targetX - pSprite->x;
  92. dy = pXSprite->targetY - pSprite->y;
  93. int nAngle = getangle(dx, dy);
  94. dist = qdist(dx, dy);
  95. aiChooseDirection(pSprite, pXSprite, nAngle);
  96. // if reached target, change to search mode
  97. if ( dist < M2X(1.0) && qabs(pSprite->ang - nAngle) < pDudeInfo->periphery )
  98. {
  99. dprintf("Axe zombie %d switching to search mode\n", pXSprite->reference);
  100. aiNewState(pSprite, pXSprite, &zombieASearch);
  101. }
  102. aiThinkTarget(pSprite, pXSprite);
  103. }
  104. /****************************************************************************
  105. ** thinkChase()
  106. **
  107. **
  108. ****************************************************************************/
  109. static void thinkChase( SPRITE *pSprite, XSPRITE *pXSprite )
  110. {
  111. if ( pXSprite->target == -1)
  112. {
  113. aiNewState(pSprite, pXSprite, &zombieASearch);
  114. return;
  115. }
  116. int dx, dy, dist;
  117. dassert(pSprite->type >= kDudeBase && pSprite->type < kDudeMax);
  118. DUDEINFO *pDudeInfo = &dudeInfo[pSprite->type - kDudeBase];
  119. dassert(pXSprite->target >= 0 && pXSprite->target < kMaxSprites);
  120. SPRITE *pTarget = &sprite[pXSprite->target];
  121. XSPRITE *pXTarget = &xsprite[pTarget->extra];
  122. // check target
  123. dx = pTarget->x - pSprite->x;
  124. dy = pTarget->y - pSprite->y;
  125. aiChooseDirection(pSprite, pXSprite, getangle(dx, dy));
  126. if ( pXTarget->health == 0 )
  127. {
  128. // target is dead
  129. aiNewState(pSprite, pXSprite, &zombieASearch);
  130. return;
  131. }
  132. if ( IsPlayerSprite( pTarget ) )
  133. {
  134. PLAYER *pPlayer = &gPlayer[ pTarget->type - kDudePlayer1 ];
  135. if ( powerupCheck( pPlayer, kItemLtdInvisibility - kItemBase ) > 0
  136. || powerupCheck( pPlayer, kItemDeathMask - kItemBase ) > 0 )
  137. {
  138. aiNewState(pSprite, pXSprite, &zombieAGoto);
  139. return;
  140. }
  141. }
  142. dist = qdist(dx, dy);
  143. if ( dist <= pDudeInfo->seeDist )
  144. {
  145. int nAngle = getangle(dx, dy);
  146. int losAngle = ((kAngle180 + nAngle - pSprite->ang) & kAngleMask) - kAngle180;
  147. int eyeAboveZ = pDudeInfo->eyeHeight * pSprite->yrepeat << 2;
  148. // is there a line of sight to the target?
  149. if ( cansee(pTarget->x, pTarget->y, pTarget->z, pTarget->sectnum,
  150. pSprite->x, pSprite->y, pSprite->z - eyeAboveZ, pSprite->sectnum) )
  151. {
  152. // is the target visible?
  153. if ( qabs(losAngle) <= pDudeInfo->periphery )
  154. {
  155. aiSetTarget(pXSprite, pXSprite->target);
  156. // check to see if we can attack
  157. if ( dist < kAxeZombieMeleeDist && qabs(losAngle) < kAngle15 )
  158. {
  159. sfxCreate3DSound(pSprite->x, pSprite->y, pSprite->z, kSfxAxeAir);
  160. aiNewState(pSprite, pXSprite, &zombieAHack);
  161. }
  162. return;
  163. }
  164. }
  165. }
  166. dprintf("Axe zombie %d lost sight of target %d\n", pXSprite->reference, pXSprite->target);
  167. aiNewState(pSprite, pXSprite, &zombieAGoto);
  168. pXSprite->target = -1;
  169. }
  170. /****************************************************************************
  171. ** thinkPonder()
  172. **
  173. **
  174. ****************************************************************************/
  175. static void thinkPonder( SPRITE *pSprite, XSPRITE *pXSprite )
  176. {
  177. if ( pXSprite->target == -1)
  178. {
  179. aiNewState(pSprite, pXSprite, &zombieASearch);
  180. return;
  181. }
  182. int dx, dy, dist;
  183. dassert(pSprite->type >= kDudeBase && pSprite->type < kDudeMax);
  184. DUDEINFO *pDudeInfo = &dudeInfo[pSprite->type - kDudeBase];
  185. dassert(pXSprite->target >= 0 && pXSprite->target < kMaxSprites);
  186. SPRITE *pTarget = &sprite[pXSprite->target];
  187. XSPRITE *pXTarget = &xsprite[pTarget->extra];
  188. // check target
  189. dx = pTarget->x - pSprite->x;
  190. dy = pTarget->y - pSprite->y;
  191. aiChooseDirection(pSprite, pXSprite, getangle(dx, dy));
  192. if ( pXTarget->health == 0 )
  193. {
  194. // target is dead
  195. aiNewState(pSprite, pXSprite, &zombieASearch);
  196. return;
  197. }
  198. if ( IsPlayerSprite( pTarget ) )
  199. {
  200. PLAYER *pPlayer = &gPlayer[ pTarget->type - kDudePlayer1 ];
  201. if ( powerupCheck( pPlayer, kItemLtdInvisibility - kItemBase ) > 0
  202. || powerupCheck( pPlayer, kItemDeathMask - kItemBase ) > 0 )
  203. {
  204. aiNewState(pSprite, pXSprite, &zombieAGoto);
  205. return;
  206. }
  207. }
  208. dist = qdist(dx, dy);
  209. if ( dist <= pDudeInfo->seeDist )
  210. {
  211. int nAngle = getangle(dx, dy);
  212. int losAngle = ((kAngle180 + nAngle - pSprite->ang) & kAngleMask) - kAngle180;
  213. int eyeAboveZ = pDudeInfo->eyeHeight * pSprite->yrepeat << 2;
  214. // is there a line of sight to the target?
  215. if ( cansee(pTarget->x, pTarget->y, pTarget->z, pTarget->sectnum,
  216. pSprite->x, pSprite->y, pSprite->z - eyeAboveZ, pSprite->sectnum) )
  217. {
  218. // is the target visible?
  219. if ( qabs(losAngle) <= pDudeInfo->periphery )
  220. {
  221. aiSetTarget(pXSprite, pXSprite->target);
  222. // check to see if we can attack
  223. if ( dist < kAxeZombieMeleeDist )
  224. {
  225. if ( qabs(losAngle) < kAngle15 )
  226. {
  227. sfxCreate3DSound(pSprite->x, pSprite->y, pSprite->z, kSfxAxeAir);
  228. aiNewState(pSprite, pXSprite, &zombieAHack);
  229. }
  230. return;
  231. }
  232. }
  233. }
  234. }
  235. aiNewState(pSprite, pXSprite, &zombieAChase);
  236. }
  237. /****************************************************************************
  238. ** myThinkTarget()
  239. **
  240. **
  241. ****************************************************************************/
  242. static void myThinkTarget( SPRITE *pSprite, XSPRITE *pXSprite )
  243. {
  244. dassert(pSprite->type >= kDudeBase && pSprite->type < kDudeMax);
  245. DUDEINFO *pDudeInfo = &dudeInfo[pSprite->type - kDudeBase];
  246. for (int i = 0; i < numplayers; i++)
  247. {
  248. PLAYER *pPlayer = &gPlayer[i];
  249. // skip this player if the he owns the dude or is invisible
  250. if ( pSprite->owner == pPlayer->nSprite
  251. || pPlayer->xsprite->health == 0
  252. || powerupCheck( pPlayer, kItemLtdInvisibility - kItemBase ) > 0 )
  253. continue;
  254. int x = pPlayer->sprite->x;
  255. int y = pPlayer->sprite->y;
  256. int z = pPlayer->sprite->z;
  257. short nSector = pPlayer->sprite->sectnum;
  258. int dx = x - pSprite->x;
  259. int dy = y - pSprite->y;
  260. int dist = qdist(dx, dy);
  261. if ( dist <= pDudeInfo->seeDist || dist <= pDudeInfo->hearDist )
  262. {
  263. int eyeAboveZ = pDudeInfo->eyeHeight * pSprite->yrepeat << 2;
  264. // is there a line of sight to the player?
  265. if ( cansee(x, y, z, nSector, pSprite->x, pSprite->y, pSprite->z - eyeAboveZ,
  266. pSprite->sectnum) )
  267. {
  268. int nAngle = getangle(dx, dy);
  269. int losAngle = ((kAngle180 + nAngle - pSprite->ang) & kAngleMask) - kAngle180;
  270. // is the player visible?
  271. if ( dist < pDudeInfo->seeDist && qabs(losAngle) <= pDudeInfo->periphery )
  272. {
  273. aiSetTarget( pXSprite, pPlayer->nSprite );
  274. aiActivateDude( pSprite, pXSprite );
  275. return;
  276. }
  277. // we may want to make hearing a function of sensitivity, rather than distance
  278. if ( dist < pDudeInfo->hearDist )
  279. {
  280. aiSetTarget(pXSprite, x, y, z);
  281. aiActivateDude( pSprite, pXSprite );
  282. return;
  283. }
  284. }
  285. }
  286. }
  287. }
  288. /****************************************************************************
  289. ** myThinkSearch()
  290. **
  291. **
  292. ****************************************************************************/
  293. static void myThinkSearch( SPRITE *pSprite, XSPRITE *pXSprite )
  294. {
  295. aiChooseDirection(pSprite, pXSprite, pXSprite->goalAng);
  296. myThinkTarget(pSprite, pXSprite);
  297. }
  298. /****************************************************************************
  299. ** entryEZombie()
  300. **
  301. **
  302. ****************************************************************************/
  303. static void entryEZombie( SPRITE *pSprite, XSPRITE */*pXSprite*/ )
  304. {
  305. pSprite->flags |= kAttrMove;
  306. pSprite->type = kDudeAxeZombie;
  307. }
  308. /****************************************************************************
  309. ** entryAZombieIdle()
  310. **
  311. **
  312. ****************************************************************************/
  313. static void entryAIdle( SPRITE */*pSprite*/, XSPRITE *pXSprite )
  314. {
  315. pXSprite->target = -1;
  316. }
  317. /****************************************************************************
  318. ** aiAlertLackey()
  319. **
  320. **
  321. ****************************************************************************/
  322. void aiAlertLackey( PLAYER *pPlayer, int nLackey, int nSource, int nDamage )
  323. {
  324. // tell lackeys all about your troubles...
  325. SPRITE *pLackey = &sprite[nLackey];
  326. XSPRITE *pXLackey = &xsprite[pLackey->extra];
  327. if ( pXLackey->target == -1 || pXLackey->target == pPlayer->nSprite )
  328. {
  329. // give a dude a target
  330. //dprintf("giving dude a new target\n");
  331. aiSetTarget(pXLackey, nSource);
  332. aiActivateDude(pLackey, pXLackey);
  333. }
  334. else if (nSource != pXLackey->target)
  335. {
  336. // retarget
  337. SPRITE *pSource = &sprite[nSource];
  338. int nThresh = nDamage * dudeInfo[pSource->type - kDudeBase].changeTarget;
  339. if ( Chance(nThresh) )
  340. {
  341. //dprintf("aiSetTarget lackey #%i to %i\n",i,nSource);
  342. aiSetTarget(pXLackey, nSource);
  343. aiActivateDude(pLackey, pXLackey);
  344. }
  345. }
  346. }