-
Notifications
You must be signed in to change notification settings - Fork 599
/
monsterstate.cpp
234 lines (208 loc) · 6.45 KB
/
monsterstate.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
/***
*
* Copyright (c) 1996-2001, Valve LLC. All rights reserved.
*
* This product contains software technology licensed from Id
* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc.
* All Rights Reserved.
*
* This source code contains proprietary and confidential information of
* Valve LLC and its suppliers. Access to this code is restricted to
* persons who have executed a written SDK license with Valve. Any access,
* use or distribution of this code by or to any unlicensed person is illegal.
*
****/
//=========================================================
// monsterstate.cpp - base class monster functions for
// controlling core AI.
//=========================================================
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "nodes.h"
#include "monsters.h"
#include "animation.h"
#include "saverestore.h"
#include "soundent.h"
//=========================================================
// SetState
//=========================================================
void CBaseMonster :: SetState ( MONSTERSTATE State )
{
/*
if ( State != m_MonsterState )
{
ALERT ( at_aiconsole, "State Changed to %d\n", State );
}
*/
switch( State )
{
// Drop enemy pointers when going to idle
case MONSTERSTATE_IDLE:
if ( m_hEnemy != NULL )
{
m_hEnemy = NULL;// not allowed to have an enemy anymore.
ALERT ( at_aiconsole, "Stripped\n" );
}
break;
}
m_MonsterState = State;
m_IdealMonsterState = State;
}
//=========================================================
// RunAI
//=========================================================
void CBaseMonster :: RunAI ( void )
{
// to test model's eye height
//UTIL_ParticleEffect ( pev->origin + pev->view_ofs, g_vecZero, 255, 10 );
// IDLE sound permitted in ALERT state is because monsters were silent in ALERT state. Only play IDLE sound in IDLE state
// once we have sounds for that state.
if ( ( m_MonsterState == MONSTERSTATE_IDLE || m_MonsterState == MONSTERSTATE_ALERT ) && RANDOM_LONG(0,99) == 0 && !(pev->flags & SF_MONSTER_GAG) )
{
IdleSound();
}
if ( m_MonsterState != MONSTERSTATE_NONE &&
m_MonsterState != MONSTERSTATE_PRONE &&
m_MonsterState != MONSTERSTATE_DEAD )// don't bother with this crap if monster is prone.
{
// collect some sensory Condition information.
// don't let monsters outside of the player's PVS act up, or most of the interesting
// things will happen before the player gets there!
// UPDATE: We now let COMBAT state monsters think and act fully outside of player PVS. This allows the player to leave
// an area where monsters are fighting, and the fight will continue.
if ( !FNullEnt( FIND_CLIENT_IN_PVS( edict() ) ) || ( m_MonsterState == MONSTERSTATE_COMBAT ) )
{
Look( m_flDistLook );
Listen();// check for audible sounds.
// now filter conditions.
ClearConditions( IgnoreConditions() );
GetEnemy();
}
// do these calculations if monster has an enemy.
if ( m_hEnemy != NULL )
{
CheckEnemy( m_hEnemy );
}
CheckAmmo();
}
FCheckAITrigger();
PrescheduleThink();
MaintainSchedule();
// if the monster didn't use these conditions during the above call to MaintainSchedule() or CheckAITrigger()
// we throw them out cause we don't want them sitting around through the lifespan of a schedule
// that doesn't use them.
m_afConditions &= ~( bits_COND_LIGHT_DAMAGE | bits_COND_HEAVY_DAMAGE );
}
//=========================================================
// GetIdealState - surveys the Conditions information available
// and finds the best new state for a monster.
//=========================================================
MONSTERSTATE CBaseMonster :: GetIdealState ( void )
{
int iConditions;
iConditions = IScheduleFlags();
// If no schedule conditions, the new ideal state is probably the reason we're in here.
switch ( m_MonsterState )
{
case MONSTERSTATE_IDLE:
/*
IDLE goes to ALERT upon hearing a sound
-IDLE goes to ALERT upon being injured
IDLE goes to ALERT upon seeing food
-IDLE goes to COMBAT upon sighting an enemy
IDLE goes to HUNT upon smelling food
*/
{
if ( iConditions & bits_COND_NEW_ENEMY )
{
// new enemy! This means an idle monster has seen someone it dislikes, or
// that a monster in combat has found a more suitable target to attack
m_IdealMonsterState = MONSTERSTATE_COMBAT;
}
else if ( iConditions & bits_COND_LIGHT_DAMAGE )
{
MakeIdealYaw ( m_vecEnemyLKP );
m_IdealMonsterState = MONSTERSTATE_ALERT;
}
else if ( iConditions & bits_COND_HEAVY_DAMAGE )
{
MakeIdealYaw ( m_vecEnemyLKP );
m_IdealMonsterState = MONSTERSTATE_ALERT;
}
else if ( iConditions & bits_COND_HEAR_SOUND )
{
CSound *pSound;
pSound = PBestSound();
ASSERT( pSound != NULL );
if ( pSound )
{
MakeIdealYaw ( pSound->m_vecOrigin );
if ( pSound->m_iType & (bits_SOUND_COMBAT|bits_SOUND_DANGER) )
m_IdealMonsterState = MONSTERSTATE_ALERT;
}
}
else if ( iConditions & (bits_COND_SMELL | bits_COND_SMELL_FOOD) )
{
m_IdealMonsterState = MONSTERSTATE_ALERT;
}
break;
}
case MONSTERSTATE_ALERT:
/*
ALERT goes to IDLE upon becoming bored
-ALERT goes to COMBAT upon sighting an enemy
ALERT goes to HUNT upon hearing a noise
*/
{
if ( iConditions & (bits_COND_NEW_ENEMY|bits_COND_SEE_ENEMY) )
{
// see an enemy we MUST attack
m_IdealMonsterState = MONSTERSTATE_COMBAT;
}
else if ( iConditions & bits_COND_HEAR_SOUND )
{
m_IdealMonsterState = MONSTERSTATE_ALERT;
CSound *pSound = PBestSound();
ASSERT( pSound != NULL );
if ( pSound )
MakeIdealYaw ( pSound->m_vecOrigin );
}
break;
}
case MONSTERSTATE_COMBAT:
/*
COMBAT goes to HUNT upon losing sight of enemy
COMBAT goes to ALERT upon death of enemy
*/
{
if ( m_hEnemy == NULL )
{
m_IdealMonsterState = MONSTERSTATE_ALERT;
// pev->effects = EF_BRIGHTFIELD;
ALERT ( at_aiconsole, "***Combat state with no enemy!\n" );
}
break;
}
case MONSTERSTATE_HUNT:
/*
HUNT goes to ALERT upon seeing food
HUNT goes to ALERT upon being injured
HUNT goes to IDLE if goal touched
HUNT goes to COMBAT upon seeing enemy
*/
{
break;
}
case MONSTERSTATE_SCRIPT:
if ( iConditions & (bits_COND_TASK_FAILED|bits_COND_LIGHT_DAMAGE|bits_COND_HEAVY_DAMAGE) )
{
ExitScriptedSequence(); // This will set the ideal state
}
break;
case MONSTERSTATE_DEAD:
m_IdealMonsterState = MONSTERSTATE_DEAD;
break;
}
return m_IdealMonsterState;
}