/ source / blood / src / aihound.cpp
aihound.cpp
  1  //-------------------------------------------------------------------------
  2  /*
  3  Copyright (C) 2010-2019 EDuke32 developers and contributors
  4  Copyright (C) 2019 Nuke.YKT
  5  
  6  This file is part of NBlood.
  7  
  8  NBlood is free software; you can redistribute it and/or
  9  modify it under the terms of the GNU General Public License version 2
 10  as published by the Free Software Foundation.
 11  
 12  This program is distributed in the hope that it will be useful,
 13  but WITHOUT ANY WARRANTY; without even the implied warranty of
 14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 15  
 16  See the GNU General Public License for more details.
 17  
 18  You should have received a copy of the GNU General Public License
 19  along with this program; if not, write to the Free Software
 20  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 21  */
 22  //-------------------------------------------------------------------------
 23  #include "compat.h"
 24  #include "build.h"
 25  #include "pragmas.h"
 26  #include "mmulti.h"
 27  #include "common_game.h"
 28  
 29  
 30  #include "actor.h"
 31  #include "ai.h"
 32  #include "aihound.h"
 33  #include "blood.h"
 34  #include "db.h"
 35  #include "dude.h"
 36  #include "eventq.h"
 37  #include "levels.h"
 38  #include "player.h"
 39  #include "seq.h"
 40  #include "sfx.h"
 41  #include "trig.h"
 42  #ifdef NOONE_EXTENSIONS
 43  #include "nnexts.h"
 44  #endif
 45  
 46  static void BiteSeqCallback(int, int);
 47  static void BurnSeqCallback(int, int);
 48  static void thinkSearch(spritetype *, XSPRITE *);
 49  static void thinkGoto(spritetype *, XSPRITE *);
 50  static void thinkChase(spritetype *, XSPRITE *);
 51  
 52  static int nBiteClient = seqRegisterClient(BiteSeqCallback);
 53  static int nBurnClient = seqRegisterClient(BurnSeqCallback);
 54  
 55  AISTATE houndIdle = { kAiStateIdle, 0, -1, 0, NULL, NULL, aiThinkTarget, NULL };
 56  AISTATE houndSearch = { kAiStateMove, 8, -1, 1800, NULL, aiMoveForward, thinkSearch, &houndIdle };
 57  AISTATE houndChase = { kAiStateChase, 8, -1, 0, NULL, aiMoveForward, thinkChase, NULL };
 58  AISTATE houndRecoil = { kAiStateRecoil, 5, -1, 0, NULL, NULL, NULL, &houndSearch };
 59  AISTATE houndTeslaRecoil = { kAiStateRecoil, 4, -1, 0, NULL, NULL, NULL, &houndSearch };
 60  AISTATE houndGoto = { kAiStateMove, 8, -1, 600, NULL, aiMoveForward, thinkGoto, &houndIdle };
 61  AISTATE houndBite = { kAiStateChase, 6, nBiteClient, 60, NULL, NULL, NULL, &houndChase };
 62  AISTATE houndBurn = { kAiStateChase, 7, nBurnClient, 60, NULL, NULL, NULL, &houndChase };
 63  
 64  static void BiteSeqCallback(int, int nXSprite)
 65  {
 66      XSPRITE *pXSprite = &xsprite[nXSprite];
 67      int nSprite = pXSprite->reference;
 68      spritetype *pSprite = &sprite[nSprite];
 69      int dx = Cos(pSprite->ang)>>16;
 70      int dy = Sin(pSprite->ang)>>16;
 71      ///dassert(pSprite->type >= kDudeBase && pSprite->type < kDudeMax);
 72      if (!(pSprite->type >= kDudeBase && pSprite->type < kDudeMax)) {
 73          consoleSysMsg("pSprite->type >= kDudeBase && pSprite->type < kDudeMax");
 74          return;
 75      }
 76  
 77      ///dassert(pXSprite->target >= 0 && pXSprite->target < kMaxSprites);
 78      if (!(pXSprite->target >= 0 && pXSprite->target < kMaxSprites)) {
 79          consoleSysMsg("pXSprite->target >= 0 && pXSprite->target < kMaxSprites");
 80          return;
 81      }
 82      spritetype *pTarget = &sprite[pXSprite->target];
 83      #ifdef NOONE_EXTENSIONS
 84          if (IsPlayerSprite(pTarget) || gModernMap) // allow to hit non-player targets
 85              actFireVector(pSprite, 0, 0, dx, dy, pTarget->z - pSprite->z, kVectorHoundBite);
 86      #else
 87          if (IsPlayerSprite(pTarget))
 88              actFireVector(pSprite, 0, 0, dx, dy, pTarget->z - pSprite->z, kVectorHoundBite);
 89      #endif
 90  }
 91  
 92  static void BurnSeqCallback(int, int nXSprite)
 93  {
 94      XSPRITE *pXSprite = &xsprite[nXSprite];
 95      int nSprite = pXSprite->reference;
 96      spritetype *pSprite = &sprite[nSprite];
 97      actFireMissile(pSprite, 0, 0, Cos(pSprite->ang)>>16, Sin(pSprite->ang)>>16, 0, kMissileFlameHound);
 98  }
 99  
100  static void thinkSearch(spritetype *pSprite, XSPRITE *pXSprite)
101  {
102      aiChooseDirection(pSprite, pXSprite, pXSprite->goalAng);
103      aiThinkTarget(pSprite, pXSprite);
104  }
105  
106  static void thinkGoto(spritetype *pSprite, XSPRITE *pXSprite)
107  {
108      ///dassert(pSprite->type >= kDudeBase && pSprite->type < kDudeMax);
109      if (!(pSprite->type >= kDudeBase && pSprite->type < kDudeMax)) {
110          consoleSysMsg("pSprite->type >= kDudeBase && pSprite->type < kDudeMax");
111          return;
112      }
113      
114      DUDEINFO *pDudeInfo = getDudeInfo(pSprite->type);
115      int dx = pXSprite->targetX-pSprite->x;
116      int dy = pXSprite->targetY-pSprite->y;
117      int nAngle = getangle(dx, dy);
118      int nDist = approxDist(dx, dy);
119      aiChooseDirection(pSprite, pXSprite, nAngle);
120      if (nDist < 512 && klabs(pSprite->ang - nAngle) < pDudeInfo->periphery)
121          aiNewState(pSprite, pXSprite, &houndSearch);
122      aiThinkTarget(pSprite, pXSprite);
123  }
124  
125  static void thinkChase(spritetype *pSprite, XSPRITE *pXSprite)
126  {
127      if (pXSprite->target == -1)
128      {
129          aiNewState(pSprite, pXSprite, &houndGoto);
130          return;
131      }
132      ///dassert(pSprite->type >= kDudeBase && pSprite->type < kDudeMax);
133      if (!(pSprite->type >= kDudeBase && pSprite->type < kDudeMax)) {
134          consoleSysMsg("pSprite->type >= kDudeBase && pSprite->type < kDudeMax");
135          return;
136      }
137      DUDEINFO *pDudeInfo = getDudeInfo(pSprite->type);
138      ///dassert(pXSprite->target >= 0 && pXSprite->target < kMaxSprites);
139      if (!(pXSprite->target >= 0 && pXSprite->target < kMaxSprites)) {
140          consoleSysMsg("pXSprite->target >= 0 && pXSprite->target < kMaxSprites");
141          return;
142      }
143      spritetype *pTarget = &sprite[pXSprite->target];
144      XSPRITE *pXTarget = &xsprite[pTarget->extra];
145      int dx = pTarget->x-pSprite->x;
146      int dy = pTarget->y-pSprite->y;
147      aiChooseDirection(pSprite, pXSprite, getangle(dx, dy));
148      if (pXTarget->health == 0)
149      {
150          aiNewState(pSprite, pXSprite, &houndSearch);
151          return;
152      }
153      if (IsPlayerSprite(pTarget) && powerupCheck(&gPlayer[pTarget->type-kDudePlayer1], kPwUpShadowCloak) > 0)
154      {
155          aiNewState(pSprite, pXSprite, &houndSearch);
156          return;
157      }
158      int nDist = approxDist(dx, dy);
159      if (nDist <= pDudeInfo->seeDist)
160      {
161          int nDeltaAngle = ((getangle(dx,dy)+1024-pSprite->ang)&2047)-1024;
162          int height = (pDudeInfo->eyeHeight*pSprite->yrepeat)<<2;
163          if (cansee(pTarget->x, pTarget->y, pTarget->z, pTarget->sectnum, pSprite->x, pSprite->y, pSprite->z - height, pSprite->sectnum))
164          {
165              if (nDist < pDudeInfo->seeDist && klabs(nDeltaAngle) <= pDudeInfo->periphery)
166              {
167                  aiSetTarget(pXSprite, pXSprite->target);
168                  if (nDist < 0xb00 && nDist > 0x500 && klabs(nDeltaAngle) < 85)
169                      aiNewState(pSprite, pXSprite, &houndBurn);
170                  else if(nDist < 0x266 && klabs(nDeltaAngle) < 85)
171                      aiNewState(pSprite, pXSprite, &houndBite);
172                  return;
173              }
174          }
175      }
176  
177      aiNewState(pSprite, pXSprite, &houndGoto);
178      pXSprite->target = -1;
179  }