combat: force grenade war if have only grenade in hands

fix: crash bug when in some situations sv_gravity is zero
This commit is contained in:
jeefo 2024-02-10 21:58:10 +03:00
commit 9b9d4b6558
No known key found for this signature in database
GPG key ID: 927BCA0779BEA8ED
4 changed files with 38 additions and 12 deletions

View file

@ -402,6 +402,7 @@ private:
bool selectBestNextNode (); bool selectBestNextNode ();
bool hasAnyWeapons (); bool hasAnyWeapons ();
bool isKnifeMode (); bool isKnifeMode ();
bool isGrenadeWar ();
bool isDeadlyMove (const Vector &to); bool isDeadlyMove (const Vector &to);
bool isOutOfBombTimer (); bool isOutOfBombTimer ();
bool isWeaponBadAtDistance (int weaponIndex, float distance); bool isWeaponBadAtDistance (int weaponIndex, float distance);
@ -867,6 +868,7 @@ extern ConVar cv_graph_auto_save_count;
extern ConVar cv_graph_analyze_max_jump_height; extern ConVar cv_graph_analyze_max_jump_height;
extern ConVar cv_spraypaints; extern ConVar cv_spraypaints;
extern ConVar cv_whose_your_daddy; extern ConVar cv_whose_your_daddy;
extern ConVar cv_grenadier_mode;
extern ConVar mp_freezetime; extern ConVar mp_freezetime;
extern ConVar mp_roundtime; extern ConVar mp_roundtime;

View file

@ -1528,6 +1528,21 @@ bool Bot::isKnifeMode () {
return cv_jasonmode.bool_ () || (usesKnife () && !hasAnyWeapons ()) || m_isCreature; return cv_jasonmode.bool_ () || (usesKnife () && !hasAnyWeapons ()) || m_isCreature;
} }
bool Bot::isGrenadeWar () {
const bool hasSomeGreandes = bestGrenadeCarried () != -1;
// if has grenade an not other weapons, assume we're in grenade war
if (!hasAnyWeapons () && hasSomeGreandes) {
return true;
}
// if we're forced to via cvar
if (cv_grenadier_mode.bool_ ()) {
return true;
}
return game.mapIs (MapFlags::GrenadeWar); // in case map was flagged
}
void Bot::selectBestWeapon () { void Bot::selectBestWeapon () {
// this function chooses best weapon, from weapons that bot currently own, and change // this function chooses best weapon, from weapons that bot currently own, and change
// current weapon to best one. // current weapon to best one.
@ -1791,6 +1806,11 @@ Vector Bot::calcToss (const Vector &start, const Vector &stop) {
TraceResult tr {}; TraceResult tr {};
const float gravity = sv_gravity.float_ () * 0.55f; const float gravity = sv_gravity.float_ () * 0.55f;
// prevent div by zero in some strange situations
if (cr::fzero (gravity)) {
return nullptr;
}
Vector end = stop - pev->velocity; Vector end = stop - pev->velocity;
end.z -= 15.0f; end.z -= 15.0f;
@ -1845,6 +1865,12 @@ Vector Bot::calcThrow (const Vector &start, const Vector &stop) {
TraceResult tr {}; TraceResult tr {};
const float gravity = sv_gravity.float_ () * 0.55f; const float gravity = sv_gravity.float_ () * 0.55f;
// prevent div by zero in some strange situations
if (cr::fzero (gravity)) {
return nullptr;
}
float time = velocity.length () / 195.0f; float time = velocity.length () / 195.0f;
if (time < 0.01f) { if (time < 0.01f) {
@ -1902,16 +1928,18 @@ edict_t *Bot::setCorrectGrenadeVelocity (StringRef model) {
} }
void Bot::checkGrenadesThrow () { void Bot::checkGrenadesThrow () {
const auto tid = getCurrentTaskId ();
// do not check cancel if we have grenade in out hands // do not check cancel if we have grenade in out hands
const bool preventibleTasks = getCurrentTaskId () == Task::PlantBomb || getCurrentTaskId () == Task::DefuseBomb; const bool preventibleTasks = tid == Task::PlantBomb || tid == Task::DefuseBomb;
const bool isGrenadeMode = isGrenadeWar ();
auto clearThrowStates = [] (uint32_t &states) { auto clearThrowStates = [] (uint32_t &states) {
states &= ~(Sense::ThrowExplosive | Sense::ThrowFlashbang | Sense::ThrowSmoke); states &= ~(Sense::ThrowExplosive | Sense::ThrowFlashbang | Sense::ThrowSmoke);
}; };
// check if throwing a grenade is a good thing to do... // check if throwing a grenade is a good thing to do...
const auto throwingCondition = game.mapIs (MapFlags::GrenadeWar) const auto throwingCondition = isGrenadeMode
? false ? false
: (preventibleTasks : (preventibleTasks
|| isInNarrowPlace () || isInNarrowPlace ()
@ -1930,7 +1958,7 @@ void Bot::checkGrenadesThrow () {
// check again in some seconds // check again in some seconds
m_grenadeCheckTime = game.time () + kGrenadeCheckTime; m_grenadeCheckTime = game.time () + kGrenadeCheckTime;
const auto senseCondition = game.mapIs (MapFlags::GrenadeWar) ? false : !(m_states & (Sense::SuspectEnemy | Sense::HearingEnemy)); const auto senseCondition = isGrenadeMode ? false : !(m_states & (Sense::SuspectEnemy | Sense::HearingEnemy));
if (!util.isAlive (m_lastEnemy) || senseCondition) { if (!util.isAlive (m_lastEnemy) || senseCondition) {
clearThrowStates (m_states); clearThrowStates (m_states);
@ -1974,7 +2002,7 @@ void Bot::checkGrenadesThrow () {
} }
// enemy within a good throw distance? // enemy within a good throw distance?
const auto grenadeToThrowCondition = game.mapIs (MapFlags::GrenadeWar) const auto grenadeToThrowCondition = isGrenadeMode
? 100.0f ? 100.0f
: grenadeToThrow == Weapon::Smoke ? 200.0f : 400.0f; : grenadeToThrow == Weapon::Smoke ? 200.0f : 400.0f;

View file

@ -10,7 +10,7 @@
ConVar cv_csdm_mode ("csdm_mode", "0", "Enables or disables CSDM / FFA mode for bots.\nAllowed values: '0', '1', '2', '3'.\nIf '0', CSDM / FFA mode is auto-detected.\nIf '1', CSDM mode is enabled, but FFA is disabled.\nIf '2', CSDM and FFA mode is enabled.\nIf '3', CSDM and FFA mode is disabled.", true, 0.0f, 3.0f); ConVar cv_csdm_mode ("csdm_mode", "0", "Enables or disables CSDM / FFA mode for bots.\nAllowed values: '0', '1', '2', '3'.\nIf '0', CSDM / FFA mode is auto-detected.\nIf '1', CSDM mode is enabled, but FFA is disabled.\nIf '2', CSDM and FFA mode is enabled.\nIf '3', CSDM and FFA mode is disabled.", true, 0.0f, 3.0f);
ConVar cv_ignore_map_prefix_game_mode ("ignore_map_prefix_game_mode", "0", "If enabled, bots will not apply game modes based on map name prefix (fy_ and ka_ specifically)."); ConVar cv_ignore_map_prefix_game_mode ("ignore_map_prefix_game_mode", "0", "If enabled, bots will not apply game modes based on map name prefix (fy_ and ka_ specifically).");
ConVar cv_threadpool_workers ("threadpool_workers", "-1", "Maximum number of threads bot will run to process some tasks. -1 means half of CPU cores used.", true, -1.0f, static_cast <float> (plat.hardwareConcurrency ())); ConVar cv_threadpool_workers ("threadpool_workers", "-1", "Maximum number of threads bot will run to process some tasks. -1 means half of CPU cores used.", true, -1.0f, static_cast <float> (plat.hardwareConcurrency ()));
ConVar cv_grenadier_mode("grenadier_mode", "0", "If enabled, bots will not apply throwing condition on grenades."); ConVar cv_grenadier_mode ("grenadier_mode", "0", "If enabled, bots will not apply throwing condition on grenades.");
ConVar sv_skycolor_r ("sv_skycolor_r", nullptr, Var::GameRef); ConVar sv_skycolor_r ("sv_skycolor_r", nullptr, Var::GameRef);
ConVar sv_skycolor_g ("sv_skycolor_g", nullptr, Var::GameRef); ConVar sv_skycolor_g ("sv_skycolor_g", nullptr, Var::GameRef);
@ -167,10 +167,6 @@ void Game::levelInitialize (edict_t *entities, int max) {
} }
} }
if (cv_grenadier_mode.bool_()) {
m_mapFlags |= MapFlags::GrenadeWar;
}
// reset some timers // reset some timers
m_oneSecondFrame = 0.0f; m_oneSecondFrame = 0.0f;
m_halfSecondFrame = 0.0f; m_halfSecondFrame = 0.0f;

View file

@ -1084,7 +1084,7 @@ void Bot::throwExplosive_ () {
ignoreCollision (); ignoreCollision ();
if (!game.mapIs (MapFlags::GrenadeWar) && (pev->origin.distanceSq (dest) < cr::sqrf (450.0f))) { if (!isGrenadeWar () && pev->origin.distanceSq (dest) < cr::sqrf (450.0f)) {
// heck, I don't wanna blow up myself // heck, I don't wanna blow up myself
m_grenadeCheckTime = game.time () + kGrenadeCheckTime * 2.0f; m_grenadeCheckTime = game.time () + kGrenadeCheckTime * 2.0f;
@ -1099,7 +1099,7 @@ void Bot::throwExplosive_ () {
m_grenade = calcToss (pev->origin, dest); m_grenade = calcToss (pev->origin, dest);
} }
if (!game.mapIs (MapFlags::GrenadeWar) && (m_grenade.lengthSq () <= 100.0f)) { if (!isGrenadeWar () && m_grenade.lengthSq () <= 100.0f) {
m_grenadeCheckTime = game.time () + kGrenadeCheckTime * 2.0f; m_grenadeCheckTime = game.time () + kGrenadeCheckTime * 2.0f;
selectBestWeapon (); selectBestWeapon ();
@ -1218,7 +1218,7 @@ void Bot::throwSmoke_ () {
if (!game.isNullEntity (m_enemy)) { if (!game.isNullEntity (m_enemy)) {
src = src + m_enemy->v.velocity; src = src + m_enemy->v.velocity;
} }
m_grenade = (src - getEyesPos ()).normalize (); m_grenade = (src - getEyesPos ()).normalize_apx ();
if (getTask ()->time < game.time ()) { if (getTask ()->time < game.time ()) {
completeTask (); completeTask ();