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 #include "actor.h"
30 #include "ai.h"
31 #include "aiinnoc.h"
32 #include "blood.h"
33 #include "db.h"
34 #include "dude.h"
35 #include "eventq.h"
36 #include "levels.h"
37 #include "player.h"
38 #include "seq.h"
39 #include "sfx.h"
40 #include "trig.h"
41 
42 static void thinkSearch(spritetype *, XSPRITE *);
43 static void thinkGoto(spritetype *, XSPRITE *);
44 static void thinkChase(spritetype *, XSPRITE *);
45 
46 AISTATE innocentIdle = { kAiStateIdle, 0, -1, 0, NULL, NULL, aiThinkTarget, NULL };
47 AISTATE innocentSearch = { kAiStateSearch, 6, -1, 1800, NULL, aiMoveForward, thinkSearch, &innocentIdle };
48 AISTATE innocentChase = { kAiStateChase, 6, -1, 0, NULL, aiMoveForward, thinkChase, NULL };
49 AISTATE innocentRecoil = { kAiStateRecoil, 5, -1, 0, NULL, NULL, NULL, &innocentChase };
50 AISTATE innocentTeslaRecoil = { kAiStateRecoil, 4, -1, 0, NULL, NULL, NULL, &innocentChase };
51 AISTATE innocentGoto = { kAiStateMove, 6, -1, 600, NULL, aiMoveForward, thinkGoto, &innocentIdle };
52 
thinkSearch(spritetype * pSprite,XSPRITE * pXSprite)53 static void thinkSearch(spritetype *pSprite, XSPRITE *pXSprite)
54 {
55     aiChooseDirection(pSprite, pXSprite, pXSprite->goalAng);
56     aiThinkTarget(pSprite, pXSprite);
57 }
58 
thinkGoto(spritetype * pSprite,XSPRITE * pXSprite)59 static void thinkGoto(spritetype *pSprite, XSPRITE *pXSprite)
60 {
61     dassert(pSprite->type >= kDudeBase && pSprite->type < kDudeMax);
62     DUDEINFO *pDudeInfo = getDudeInfo(pSprite->type);
63     int dx = pXSprite->targetX-pSprite->x;
64     int dy = pXSprite->targetY-pSprite->y;
65     int nAngle = getangle(dx, dy);
66     int nDist = approxDist(dx, dy);
67     aiChooseDirection(pSprite, pXSprite, nAngle);
68     if (nDist < 512 && klabs(pSprite->ang - nAngle) < pDudeInfo->periphery)
69         aiNewState(pSprite, pXSprite, &innocentSearch);
70     aiThinkTarget(pSprite, pXSprite);
71 }
72 
thinkChase(spritetype * pSprite,XSPRITE * pXSprite)73 static void thinkChase(spritetype *pSprite, XSPRITE *pXSprite)
74 {
75     if (pXSprite->target == -1)
76     {
77         aiNewState(pSprite, pXSprite, &innocentGoto);
78         return;
79     }
80     dassert(pSprite->type >= kDudeBase && pSprite->type < kDudeMax);
81     DUDEINFO *pDudeInfo = getDudeInfo(pSprite->type);
82     dassert(pXSprite->target >= 0 && pXSprite->target < kMaxSprites);
83     spritetype *pTarget = &sprite[pXSprite->target];
84     XSPRITE *pXTarget = &xsprite[pTarget->extra];
85     int dx = pTarget->x-pSprite->x;
86     int dy = pTarget->y-pSprite->y;
87     aiChooseDirection(pSprite, pXSprite, getangle(dx, dy));
88     if (pXTarget->health == 0)
89     {
90         aiNewState(pSprite, pXSprite, &innocentSearch);
91         return;
92     }
93     if (IsPlayerSprite(pTarget))
94     {
95         aiNewState(pSprite, pXSprite, &innocentSearch);
96         return;
97     }
98     int nDist = approxDist(dx, dy);
99     if (nDist <= pDudeInfo->seeDist)
100     {
101         int nDeltaAngle = ((getangle(dx,dy)+1024-pSprite->ang)&2047)-1024;
102         int height = (pDudeInfo->eyeHeight*pSprite->yrepeat)<<2;
103         if (cansee(pTarget->x, pTarget->y, pTarget->z, pTarget->sectnum, pSprite->x, pSprite->y, pSprite->z - height, pSprite->sectnum))
104         {
105             if (nDist < pDudeInfo->seeDist && klabs(nDeltaAngle) <= pDudeInfo->periphery)
106             {
107                 aiSetTarget(pXSprite, pXSprite->target);
108                 if (nDist < 0x666 && klabs(nDeltaAngle) < 85)
109                     aiNewState(pSprite, pXSprite, &innocentIdle);
110                 return;
111             }
112         }
113     }
114 
115     aiPlay3DSound(pSprite, 7000+Random(6), AI_SFX_PRIORITY_1, -1);
116     aiNewState(pSprite, pXSprite, &innocentGoto);
117     pXSprite->target = -1;
118 }
119 
120