forked from borzh/botrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weapon.cpp
279 lines (233 loc) · 9.04 KB
/
weapon.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "source_engine.h"
#include "weapon.h"
#define BOT_AIM_ERROR 0
//----------------------------------------------------------------------------------------------------------------
good::vector<CWeaponWithAmmo> CWeapons::m_aWeapons;
//----------------------------------------------------------------------------------------------------------------
void CWeaponWithAmmo::GetLook( const Vector& vAim, float fDistance, bool bDuck,
TBotIntelligence iBotIntelligence, bool m_bSecondary, QAngle& angResult )
{
DebugAssert( m_pWeapon->bHasSecondary || !m_bSecondary );
Vector v(vAim);
if ( iBotIntelligence < EBotSmart ) // Aim at body instead of head.
v.z -= bDuck ? (CUtil::iPlayerEyeLevelCrouched/3) : (CUtil::iPlayerEyeLevel/3);
VectorAngles(v, angResult);
if ( m_pWeapon->iParabolic[m_bSecondary] != 0.0f )
angResult.x += fDistance / m_pWeapon->iParabolic[m_bSecondary];
#if BOT_AIM_ERROR
static const float fMaxErrorDistance = 1000.0f;
// Aim errors (pitch/yaw): max error for a distance = 1000, and max error for a distance = 0.
static const float aAimErrors[EBotIntelligenceTotal][2] =
{
// Pitch(when distance is 0) Yaw(when distance is 0). When distance is 1000 both pitch and yaw is 1.
{ 45, 30, }, // I.e. fool bot at distance 0 will have error in 45 degrees for pitch and 30 for yaw.
{ 35, 22, }, // At distance 1000 error should not be greater than 1 degree.
{ 25, 15, },
{ 15, 7, },
{ 5, 5, },
};
int iRangePitch = 1, iRangeYaw = 1;
if ( fDistance < fMaxErrorDistance )
{
float fPercentage = fDistance / fMaxErrorDistance;
iRangePitch = 2 * (int)(aAimErrors[iBotIntelligence][0] * fPercentage);
iRangeYaw = 2 * (int)(aAimErrors[iBotIntelligence][1] * fPercentage);
}
int iRandPitch = (rand() % iRangePitch) - (iRangePitch >> 1); // 45 degrees, errors = 22.5 up and 22.5 down
int iRandYaw = (rand() % iRangeYaw) - (iRangeYaw >> 1);
angResult.x += iRandPitch;
angResult.y += iRandYaw;
#endif // BOT_AIM_ERROR
}
//----------------------------------------------------------------------------------------------------------------
void CWeaponWithAmmo::GameFrame()
{
if ( m_bShooting )
{
if ( CBotrixPlugin::fTime >= m_fEndTime )
m_bShooting = false;
else
return;
}
else if ( m_bReloading )
{
if ( CBotrixPlugin::fTime >= m_fEndTime )
EndReload();
else
return;
}
else if ( m_bChanging )
{
if ( CBotrixPlugin::fTime >= m_fEndTime )
m_bChanging = false;
else
return;
}
else if ( m_bChangingZoom )
{
if ( CBotrixPlugin::fTime >= m_fEndTime )
m_bChangingZoom = false;
else
return;
}
// Check to reload secondary ammo.
if ( m_pWeapon->bHasSecondary && !m_pWeapon->bSecondaryUseSameBullets && !HasAmmoInClip(1) && HasAmmoExtra(1) )
{
Reload(1);
if ( m_pWeapon->fReloadTime[1] == 0.0f )
EndReload();
}
// Check to reload primary ammo.
if ( !HasAmmoInClip(0) && HasAmmoExtra(0) )
{
Reload(0);
if ( m_pWeapon->fReloadTime[0] == 0.0f )
EndReload();
}
}
//----------------------------------------------------------------------------------------------------------------
void CWeaponWithAmmo::Shoot( bool bSecondary )
{
DebugAssert( CanUse() && (HasAmmoInClip(bSecondary) || ( IsManual() || IsPhysics() ) ) );
if ( bSecondary && m_pWeapon->bSecondaryUseSameBullets )
m_iBulletsInClip[0] -= m_pWeapon->iAttackBullets[bSecondary]; // Shotgun type: uses same bullets for primary and secondary attack.
else
m_iBulletsInClip[bSecondary] -= m_pWeapon->iAttackBullets[bSecondary];
m_bShooting = true;
m_bSecondary = bSecondary;
m_fEndTime = CBotrixPlugin::fTime + m_pWeapon->fShotTime[bSecondary];
}
//----------------------------------------------------------------------------------------------------------------
void CWeaponWithAmmo::Holster( CWeaponWithAmmo& cSwitchTo )
{
DebugAssert( !m_bShooting );
m_bChanging = m_bReloading = m_bChangingZoom = m_bUsingZoom = false;
m_fEndTime = CBotrixPlugin::fTime; // Save time when switched weapon.
if ( cSwitchTo.m_pWeapon->bBackgroundReload && !cSwitchTo.HasAmmoInClip(0) && cSwitchTo.HasAmmoExtra(0) &&
CBotrixPlugin::fTime >= cSwitchTo.m_fEndTime + m_pWeapon->fReloadTime[0] )
{
cSwitchTo.m_bReloading = true;
cSwitchTo.EndReload();
}
cSwitchTo.m_bChanging = true;
cSwitchTo.m_fEndTime = CBotrixPlugin::fTime + m_pWeapon->fHolsterTime;
}
//----------------------------------------------------------------------------------------------------------------
void CWeaponWithAmmo::EndReload()
{
DebugAssert( m_bReloading && m_iBulletsInClip[m_bSecondary] < m_pWeapon->iClipSize[m_bSecondary] );
if ( m_pWeapon->iType == EWeaponShotgun ) // Reload one bullet.
{
++m_iBulletsInClip[m_bSecondary];
--m_iBulletsExtra[m_bSecondary];
if ( m_iBulletsInClip[m_bSecondary] < m_pWeapon->iClipSize[m_bSecondary] )
m_fEndTime = CBotrixPlugin::fTime + m_pWeapon->fReloadTime[m_bSecondary];
else
m_bReloading = false;
}
else
{
int iBulletsAdd = m_pWeapon->iClipSize[m_bSecondary] - m_iBulletsInClip[m_bSecondary];
if ( m_iBulletsExtra[m_bSecondary] < iBulletsAdd )
iBulletsAdd = m_iBulletsExtra[m_bSecondary];
m_iBulletsInClip[m_bSecondary] += iBulletsAdd;
m_iBulletsExtra[m_bSecondary] -= iBulletsAdd;
m_bReloading = false;
}
}
//----------------------------------------------------------------------------------------------------------------
void CWeapons::GetRespawnWeapons( good::vector<CWeaponWithAmmo>& aWeapons, int iTeam )
{
DebugAssert( (aWeapons.size() == 0) || (aWeapons.size() == m_aWeapons.size()) );
aWeapons.reserve( m_aWeapons.size() );
if ( aWeapons.size() == 0 )
for ( int i=0; i < m_aWeapons.size(); ++i )
aWeapons.push_back( m_aWeapons[i] );
else
{
DebugAssert( m_aWeapons.size() == aWeapons.size() );
for ( int i=0; i < m_aWeapons.size(); ++i )
aWeapons[i] = m_aWeapons[i];
}
// Remove default weapons that are not for that team.
for ( int i=0; i < m_aWeapons.size(); ++i )
if ( (iTeam != CMod::iUnassignedTeam) && (m_aWeapons[i].GetBaseWeapon()->iTeamOnly != iTeam) )
aWeapons[i].RemoveWeapon();
}
//----------------------------------------------------------------------------------------------------------------
TWeaponId CWeapons::GetBestRangedWeapon( const good::vector<CWeaponWithAmmo>& aWeapons )
{
// Choose best weapon. Skip grenades.
bool bCanKill = false, bOneBullet = false;
int iIdx = -1, iDamage = 0;
float fDamagePerSec = 0.0f;
for ( int i=0; i < aWeapons.size(); ++i )
{
const CWeaponWithAmmo& cWeapon = aWeapons[i];
if ( !cWeapon.IsPresent() || !cWeapon.IsRanged() || !cWeapon.HasAmmo() ) // Skip all manuals, grenades and physics or without ammo.
continue;
int iDamage0 = cWeapon.Damage(0);
int iDamage1 = cWeapon.Damage(1);
bool bOneBullet0 = (iDamage0 >= CUtil::iPlayerMaxHealth);
bool bOneBullet1 = (iDamage1 >= CUtil::iPlayerMaxHealth);
// Check if can kill with one bullet first.
if ( bOneBullet0 && cWeapon.HasAmmoInClip(0) && (!bOneBullet || (iDamage < iDamage0)) )
{
iIdx = i;
bOneBullet = bCanKill = true;
iDamage = iDamage0;
continue;
}
if ( bOneBullet1 && cWeapon.HasAmmoInClip(1) && (!bOneBullet || (iDamage < iDamage1)) )
{
iIdx = i;
bOneBullet = bCanKill = true;
iDamage = iDamage1;
continue;
}
if ( bOneBullet ) // We have founded previously weapon that can kill with one bullet.
continue;
// Check if weapon has sufficient bullets to kill.
iDamage0 = cWeapon.TotalDamage();
bool bCanKill0 = iDamage0 >= CUtil::iPlayerMaxHealth; // Has enough bullets to kill?
float fDamagePerSec0 = cWeapon.DamagePerSecond(); // How fast this weapon kills.
if ( bCanKill0 && (!bCanKill || (fDamagePerSec < fDamagePerSec0)) )
{
iIdx = i;
bCanKill = true;
fDamagePerSec = fDamagePerSec0;
continue;
}
if ( bCanKill ) // We have founded previously weapon that has enough bullets to kill.
continue;
// Check total damage.
if ( iDamage < iDamage0 ) // Previous weapon does less damage.
{
iIdx = i;
iDamage = iDamage0;
}
}
return iIdx;
}
//----------------------------------------------------------------------------------------------------------------
TWeaponId CWeapons::GetRandomWeapon( TBotIntelligence iIntelligence, const good::bitset& cSkipWeapons )
{
TWeaponId iIdx = rand() % Size();
for ( TWeaponId i = iIdx+1; i < Size(); ++i )
{
CWeaponWithAmmo& cWeapon = m_aWeapons[i];
const CWeapon* pWeapon = cWeapon.GetBaseWeapon();
if ( cWeapon.IsRanged() && (iIntelligence <= pWeapon->iBotPreference) && CItems::ExistsOnMap(pWeapon->pWeaponClass))
if ( !cSkipWeapons.test(i) )
return i;
}
for ( TWeaponId i = iIdx; i >= 0; --i )
{
CWeaponWithAmmo& cWeapon = m_aWeapons[i];
const CWeapon* pWeapon = cWeapon.GetBaseWeapon();
if ( cWeapon.IsRanged() && (iIntelligence <= pWeapon->iBotPreference) && CItems::ExistsOnMap(pWeapon->pWeaponClass))
if ( !cSkipWeapons.test(i) )
return i;
}
return -1;
}