AICULT.CPP 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "aicult.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 kCultistTFireDist M2X(100)
  21. #define kCultistSFireDist M2X(10)
  22. #define kCultistThrowDist1 M2X(16)
  23. #define kCultistThrowDist2 M2X(10)
  24. #define kSlopeThrow -8192 // tan(26)
  25. #define kVectorsPerBarrel 8
  26. /****************************************************************************
  27. ** LOCAL callback prototypes
  28. ****************************************************************************/
  29. static void TommyCallback( int /* type */, int nXIndex );
  30. static void ShotCallback( int /* type */, int nXIndex );
  31. static void ThrowCallback( int /* type */, int nXIndex );
  32. /****************************************************************************
  33. ** LOCAL think/move function prototypes
  34. ****************************************************************************/
  35. static void thinkSearch( SPRITE *pSprite, XSPRITE *pXSprite );
  36. static void thinkGoto( SPRITE *pSprite, XSPRITE *pXSprite );
  37. static void thinkChase( SPRITE *pSprite, XSPRITE *pXSprite );
  38. /****************************************************************************
  39. ** GLOBAL CONSTANTS
  40. ****************************************************************************/
  41. AISTATE cultistIdle = { kSeqDudeIdle, NULL, 0, NULL, NULL, aiThinkTarget, NULL };
  42. AISTATE cultistChase = { kSeqCultistWalk, NULL, 0, NULL, aiMoveForward, thinkChase, NULL };
  43. AISTATE cultistDodge = { kSeqCultistWalk, NULL, 90, NULL, aiMoveDodge, NULL, &cultistChase };
  44. AISTATE cultistGoto = { kSeqCultistWalk, NULL, 3600, NULL, aiMoveForward, thinkGoto, &cultistIdle };
  45. AISTATE cultistTThrow = { kSeqCultistAttack2, ThrowCallback, 120, NULL, NULL, NULL, &cultistTFire };
  46. AISTATE cultistSThrow = { kSeqCultistAttack2, ThrowCallback, 120, NULL, NULL, NULL, &cultistSFire };
  47. AISTATE cultistSearch = { kSeqCultistWalk, NULL, 3600, NULL, aiMoveForward, thinkSearch, &cultistIdle };
  48. AISTATE cultistSFire = { kSeqCultistAttack1, ShotCallback, 60, NULL, NULL, NULL, &cultistChase };
  49. AISTATE cultistTFire = { kSeqCultistAttack1, TommyCallback, 0, NULL, aiMoveTurn, thinkChase, &cultistTFire };
  50. AISTATE cultistRecoil = { kSeqDudeRecoil, NULL, 0, NULL, NULL, NULL, &cultistDodge };
  51. /****************************************************************************
  52. ** LOCAL FUNCTIONS
  53. ****************************************************************************/
  54. /****************************************************************************
  55. ** TommyCallback()
  56. **
  57. **
  58. ****************************************************************************/
  59. static void TommyCallback( 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 = gDudeSlope[nXIndex];
  67. // dispersal modifiers here
  68. dx += BiRandom(1000);
  69. dy += BiRandom(1000);
  70. dz += BiRandom(1000);
  71. actFireVector(nSprite, pSprite->z, dx, dy, dz, kVectorBullet);
  72. sfxCreate3DSound(pSprite->x, pSprite->y, pSprite->z, kSfxTomFire);
  73. }
  74. /****************************************************************************
  75. ** ShotCallback()
  76. **
  77. **
  78. ****************************************************************************/
  79. static void ShotCallback( int /* type */, int nXIndex )
  80. {
  81. XSPRITE *pXSprite = &xsprite[nXIndex];
  82. int nSprite = pXSprite->reference;
  83. SPRITE *pSprite = &sprite[nSprite];
  84. int dx = Cos(pSprite->ang) >> 16;
  85. int dy = Sin(pSprite->ang) >> 16;
  86. int dz = gDudeSlope[nXIndex];
  87. // aim modifiers
  88. dx += BiRandom(4000);
  89. dy += BiRandom(4000);
  90. dz += BiRandom(4000);
  91. for ( int i = 0; i < kVectorsPerBarrel; i++ )
  92. {
  93. actFireVector(nSprite, pSprite->z, dx + BiRandom(3000), dy + BiRandom(3000),
  94. dz + BiRandom(3000), kVectorShell);
  95. }
  96. sfxCreate3DSound(pSprite->x, pSprite->y, pSprite->z, kSfxShotFire);
  97. }
  98. /****************************************************************************
  99. ** ThrowCallback()
  100. **
  101. **
  102. ****************************************************************************/
  103. static void ThrowCallback( int /* type */, int nXIndex )
  104. {
  105. XSPRITE *pXSprite = &xsprite[nXIndex];
  106. int nSprite = pXSprite->reference;
  107. SPRITE *pSprite = &sprite[nSprite];
  108. sfxCreate3DSound(pSprite->x, pSprite->y, pSprite->z, kSfxTNTToss);
  109. int nThing = actFireThing(
  110. nSprite, pSprite->z, gDudeSlope[nXIndex] + kSlopeThrow, kThingTNTStick, (M2X(10.0) << 4) / kTimerRate);
  111. evPost(nThing, SS_SPRITE, 2 * kTimerRate); // 2 second burn off
  112. }
  113. /****************************************************************************
  114. ** TargetNearExplosion()
  115. **
  116. **
  117. ****************************************************************************/
  118. static BOOL TargetNearExplosion( SPRITE *pTarget )
  119. {
  120. for (short nSprite = headspritesect[pTarget->sectnum]; nSprite >= 0; nSprite = nextspritesect[nSprite])
  121. {
  122. // check for TNT sticks or explosions in the same sector as the target
  123. if (sprite[nSprite].type == kThingTNTStick || sprite[nSprite].statnum == kStatExplosion)
  124. return TRUE; // indicate danger
  125. }
  126. return FALSE;
  127. }
  128. /****************************************************************************
  129. ** thinkSearch()
  130. **
  131. **
  132. ****************************************************************************/
  133. static void thinkSearch( SPRITE *pSprite, XSPRITE *pXSprite )
  134. {
  135. aiChooseDirection(pSprite, pXSprite, pXSprite->goalAng);
  136. aiThinkTarget(pSprite, pXSprite);
  137. }
  138. /****************************************************************************
  139. ** thinkGoto()
  140. **
  141. **
  142. ****************************************************************************/
  143. static void thinkGoto( SPRITE *pSprite, XSPRITE *pXSprite )
  144. {
  145. int dx, dy, dist;
  146. dassert(pSprite->type >= kDudeBase && pSprite->type < kDudeMax);
  147. DUDEINFO *pDudeInfo = &dudeInfo[pSprite->type - kDudeBase];
  148. SPRITE *pTarget = &sprite[pXSprite->target];
  149. XSPRITE *pXTarget = &xsprite[pTarget->extra];
  150. dx = pXSprite->targetX - pSprite->x;
  151. dy = pXSprite->targetY - pSprite->y;
  152. int nAngle = getangle(dx, dy);
  153. dist = qdist(dx, dy);
  154. aiChooseDirection(pSprite, pXSprite, nAngle);
  155. // if reached target, change to search mode
  156. if ( dist < M2X(1.0) && qabs(pSprite->ang - nAngle) < pDudeInfo->periphery )
  157. aiNewState(pSprite, pXSprite, &cultistSearch);
  158. aiThinkTarget(pSprite, pXSprite);
  159. }
  160. /****************************************************************************
  161. ** thinkChase()
  162. **
  163. **
  164. ****************************************************************************/
  165. static void thinkChase( SPRITE *pSprite, XSPRITE *pXSprite )
  166. {
  167. if ( pXSprite->target == -1)
  168. {
  169. aiNewState(pSprite, pXSprite, &cultistGoto);
  170. return;
  171. }
  172. int dx, dy, dist;
  173. dassert(pSprite->type >= kDudeBase && pSprite->type < kDudeMax);
  174. DUDEINFO *pDudeInfo = &dudeInfo[pSprite->type - kDudeBase];
  175. dassert(pXSprite->target >= 0 && pXSprite->target < kMaxSprites);
  176. SPRITE *pTarget = &sprite[pXSprite->target];
  177. XSPRITE *pXTarget = &xsprite[pTarget->extra];
  178. // check target
  179. dx = pTarget->x - pSprite->x;
  180. dy = pTarget->y - pSprite->y;
  181. aiChooseDirection(pSprite, pXSprite, getangle(dx, dy));
  182. if ( pXTarget->health == 0 )
  183. {
  184. // target is dead
  185. aiNewState(pSprite, pXSprite, &cultistSearch);
  186. return;
  187. }
  188. if ( IsPlayerSprite( pTarget ) )
  189. {
  190. PLAYER *pPlayer = &gPlayer[ pTarget->type - kDudePlayer1 ];
  191. if ( powerupCheck( pPlayer, kItemLtdInvisibility - kItemBase ) > 0 )
  192. {
  193. aiNewState(pSprite, pXSprite, &cultistSearch);
  194. return;
  195. }
  196. }
  197. dist = qdist(dx, dy);
  198. if ( dist <= pDudeInfo->seeDist )
  199. {
  200. int nAngle = getangle(dx, dy);
  201. int losAngle = ((kAngle180 + nAngle - pSprite->ang) & kAngleMask) - kAngle180;
  202. int eyeAboveZ = pDudeInfo->eyeHeight * pSprite->yrepeat << 2;
  203. // is there a line of sight to the target?
  204. if ( cansee(pTarget->x, pTarget->y, pTarget->z, pTarget->sectnum,
  205. pSprite->x, pSprite->y, pSprite->z - eyeAboveZ, pSprite->sectnum) )
  206. {
  207. // is the target visible?
  208. if ( dist < pDudeInfo->seeDist && qabs(losAngle) <= pDudeInfo->periphery )
  209. {
  210. aiSetTarget(pXSprite, pXSprite->target);
  211. int nXSprite = sprite[pXSprite->reference].extra;
  212. gDudeSlope[nXSprite] = divscale(pTarget->z - pSprite->z, dist, 10);
  213. // check to see if we can attack
  214. switch ( pSprite->type )
  215. {
  216. case kDudeTommyCultist:
  217. if ( dist < kCultistThrowDist1 && dist > kCultistThrowDist2 && qabs(losAngle) < kAngle15
  218. && !TargetNearExplosion(pTarget) && (pTarget->flags & kAttrGravity)
  219. && !( IsPlayerSprite(pXSprite->target) && gPlayer[pTarget->type - kDudePlayer1].run )
  220. && Chance(0x4000) )
  221. aiNewState(pSprite, pXSprite, &cultistTThrow);
  222. else if ( dist < kCultistTFireDist && qabs(losAngle) < kAngle15 )
  223. aiNewState(pSprite, pXSprite, &cultistTFire);
  224. break;
  225. case kDudeShotgunCultist:
  226. if ( dist < kCultistThrowDist1 && dist > kCultistThrowDist2 && qabs(losAngle) < kAngle15
  227. && !TargetNearExplosion(pTarget) && (pTarget->flags & kAttrGravity)
  228. && !(IsPlayerSprite(pXSprite->target) && gPlayer[pTarget->type - kDudePlayer1].run)
  229. && Chance(0x4000) )
  230. aiNewState(pSprite, pXSprite, &cultistSThrow);
  231. else if ( dist < kCultistSFireDist && qabs(losAngle) < kAngle15 )
  232. aiNewState(pSprite, pXSprite, &cultistSFire);
  233. break;
  234. }
  235. return;
  236. }
  237. }
  238. }
  239. dprintf("Cultist %d lost sight of target %d\n", pXSprite->reference, pXSprite->target);
  240. aiNewState(pSprite, pXSprite, &cultistGoto);
  241. pXSprite->target = -1;
  242. }