#[cfg(test)]
#[path = "../../tests/tf2/objectives/flags.rs"]
mod tests;
use super::{Objective, ObjectiveError};
use crate::Server;
use crate::entities::Entity;
use crate::inputs::InputValue;
use crate::tf2::scoreboard::ScoringTeam;
use sdk_raw::tf2::objectives as raw;
use std::ffi::c_int;
#[doc(alias("item_teamflag", "CCaptureFlag", "intelligence"))]
#[derive(Debug, Clone, Copy)]
pub struct CaptureFlag<'s>(Objective<'s>);
impl<'s> CaptureFlag<'s> {
pub fn new(server: Server<'s>, entity: Entity<'s>) -> Result<Self, ObjectiveError> {
Objective::new(server, entity, c"CCaptureFlag", "item_teamflag").map(Self)
}
pub fn carrier(self) -> Result<Option<Entity<'s>>, ObjectiveError> {
if self.status()? == FlagStatus::Stolen {
self.last_carrier()
} else {
Ok(None)
}
}
#[doc(alias("Disable"))]
pub fn disable(self) -> Result<(), ObjectiveError> {
self.0.input(c"Disable", InputValue::Void)
}
#[doc(alias("Enable"))]
pub fn enable(self) -> Result<(), ObjectiveError> {
self.0.input(c"Enable", InputValue::Void)
}
pub fn entity(self) -> Entity<'s> {
self.0.entity()
}
#[doc(alias("m_nType", "GameType", "GetType"))]
pub fn flag_type(self) -> Result<FlagType, ObjectiveError> {
let raw: c_int = self.0.get(c"m_nType")?;
FlagType::from_raw(raw).ok_or(ObjectiveError::UnknownValue {
name: c"m_nType",
value: raw,
})
}
#[doc(alias("ForceDrop"))]
pub fn force_drop(self) -> Result<(), ObjectiveError> {
self.0.input(c"ForceDrop", InputValue::Void)
}
#[doc(alias("m_bDisabled", "StartDisabled", "IsDisabled"))]
pub fn is_disabled(self) -> Result<bool, ObjectiveError> {
self.0.flag(c"m_bDisabled")
}
#[doc(alias("m_bGlowEnabled", "IsGlowEnabled"))]
pub fn is_glow_enabled(self) -> Result<bool, ObjectiveError> {
self.0.flag(c"m_bGlowEnabled")
}
#[doc(alias("m_bVisibleWhenDisabled", "VisibleWhenDisabled"))]
pub fn is_visible_when_disabled(self) -> Result<bool, ObjectiveError> {
self.0.flag(c"m_bVisibleWhenDisabled")
}
#[doc(alias("m_hPrevOwner", "GetPrevOwner"))]
pub fn last_carrier(self) -> Result<Option<Entity<'s>>, ObjectiveError> {
self.0.handle_entity(c"m_hPrevOwner")
}
#[doc(alias("m_flNeutralTime"))]
pub fn neutral_time(self) -> Result<Option<f32>, ObjectiveError> {
let time: f32 = self.0.get(c"m_flNeutralTime")?;
Ok((time > 0.0).then_some(time))
}
#[doc(alias("m_nPointValue", "PointValue", "GetPointValue"))]
pub fn point_value(self) -> Result<c_int, ObjectiveError> {
self.0.get(c"m_nPointValue")
}
#[doc(alias("ForceReset"))]
pub fn reset(self) -> Result<(), ObjectiveError> {
self.0.input(c"ForceReset", InputValue::Void)
}
#[doc(alias("ForceResetSilent"))]
pub fn reset_silently(self) -> Result<(), ObjectiveError> {
self.0.input(c"ForceResetSilent", InputValue::Void)
}
#[doc(alias("m_nReturnTime", "ReturnTime"))]
pub fn return_delay(self) -> Result<c_int, ObjectiveError> {
self.0.int_field(c"CCaptureFlag", c"m_nReturnTime")
}
#[doc(alias("m_flResetTime"))]
pub fn return_time(self) -> Result<Option<f32>, ObjectiveError> {
let time: f32 = self.0.get(c"m_flResetTime")?;
Ok((time > 0.0).then_some(time))
}
#[doc(alias("ForceGlowDisabled"))]
pub fn set_glow_enabled(self, enabled: bool) -> Result<(), ObjectiveError> {
self.0
.input(c"ForceGlowDisabled", InputValue::Int(c_int::from(!enabled)))
}
#[doc(alias("SetReturnTime"))]
pub fn set_return_delay(self, seconds: c_int) -> Result<(), ObjectiveError> {
self.0.input(c"SetReturnTime", InputValue::Int(seconds))
}
#[doc(alias("ShowTimer"))]
pub fn show_timer(self, seconds: c_int) -> Result<(), ObjectiveError> {
self.0.input(c"ShowTimer", InputValue::Int(seconds))
}
#[doc(alias("m_nFlagStatus"))]
pub fn status(self) -> Result<FlagStatus, ObjectiveError> {
let raw: c_int = self.0.get(c"m_nFlagStatus")?;
FlagStatus::from_raw(raw).ok_or(ObjectiveError::UnknownValue {
name: c"m_nFlagStatus",
value: raw,
})
}
#[doc(alias("m_iTeamNum", "TeamNum"))]
pub fn team(self) -> Result<Option<ScoringTeam>, ObjectiveError> {
Ok(ScoringTeam::from_raw(
self.0.int_field(c"CBaseEntity", c"m_iTeamNum")?,
))
}
}
#[doc(alias("func_capturezone", "CCaptureZone"))]
#[derive(Debug, Clone, Copy)]
pub struct CaptureZone<'s>(Objective<'s>);
impl<'s> CaptureZone<'s> {
pub fn new(server: Server<'s>, entity: Entity<'s>) -> Result<Self, ObjectiveError> {
Objective::new(server, entity, c"CCaptureZone", "func_capturezone").map(Self)
}
#[doc(alias("Disable"))]
pub fn disable(self) -> Result<(), ObjectiveError> {
self.0.input(c"Disable", InputValue::Void)
}
#[doc(alias("Enable"))]
pub fn enable(self) -> Result<(), ObjectiveError> {
self.0.input(c"Enable", InputValue::Void)
}
pub fn entity(self) -> Entity<'s> {
self.0.entity()
}
#[doc(alias("m_bDisabled", "StartDisabled", "IsDisabled"))]
pub fn is_disabled(self) -> Result<bool, ObjectiveError> {
self.0.flag(c"m_bDisabled")
}
#[doc(alias("m_iTeamNum", "TeamNum"))]
pub fn team(self) -> Result<Option<ScoringTeam>, ObjectiveError> {
Ok(ScoringTeam::from_raw(
self.0.int_field(c"CBaseEntity", c"m_iTeamNum")?,
))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FlagStatus {
#[doc(alias("TF_FLAGINFO_HOME"))]
Home,
#[doc(alias("TF_FLAGINFO_STOLEN"))]
Stolen,
#[doc(alias("TF_FLAGINFO_DROPPED"))]
Dropped,
}
impl FlagStatus {
pub const fn from_raw(raw: c_int) -> Option<Self> {
match raw {
raw::TF_FLAGINFO_HOME => Some(Self::Home),
raw::TF_FLAGINFO_STOLEN => Some(Self::Stolen),
raw::TF_FLAGINFO_DROPPED => Some(Self::Dropped),
_ => None,
}
}
pub const fn to_raw(self) -> c_int {
match self {
Self::Home => raw::TF_FLAGINFO_HOME,
Self::Stolen => raw::TF_FLAGINFO_STOLEN,
Self::Dropped => raw::TF_FLAGINFO_DROPPED,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FlagType {
#[doc(alias("TF_FLAGTYPE_CTF"))]
CaptureTheFlag,
#[doc(alias("TF_FLAGTYPE_ATTACK_DEFEND"))]
AttackDefend,
#[doc(alias("TF_FLAGTYPE_TERRITORY_CONTROL"))]
TerritoryControl,
#[doc(alias("TF_FLAGTYPE_INVADE"))]
Invade,
#[doc(alias("TF_FLAGTYPE_RESOURCE_CONTROL"))]
ResourceControl,
#[doc(alias("TF_FLAGTYPE_ROBOT_DESTRUCTION"))]
RobotDestruction,
#[doc(alias("TF_FLAGTYPE_PLAYER_DESTRUCTION"))]
PlayerDestruction,
}
impl FlagType {
pub const fn from_raw(raw: c_int) -> Option<Self> {
Some(match raw {
raw::TF_FLAGTYPE_CTF => Self::CaptureTheFlag,
raw::TF_FLAGTYPE_ATTACK_DEFEND => Self::AttackDefend,
raw::TF_FLAGTYPE_TERRITORY_CONTROL => Self::TerritoryControl,
raw::TF_FLAGTYPE_INVADE => Self::Invade,
raw::TF_FLAGTYPE_RESOURCE_CONTROL => Self::ResourceControl,
raw::TF_FLAGTYPE_ROBOT_DESTRUCTION => Self::RobotDestruction,
raw::TF_FLAGTYPE_PLAYER_DESTRUCTION => Self::PlayerDestruction,
_ => return None,
})
}
pub const fn to_raw(self) -> c_int {
match self {
Self::CaptureTheFlag => raw::TF_FLAGTYPE_CTF,
Self::AttackDefend => raw::TF_FLAGTYPE_ATTACK_DEFEND,
Self::TerritoryControl => raw::TF_FLAGTYPE_TERRITORY_CONTROL,
Self::Invade => raw::TF_FLAGTYPE_INVADE,
Self::ResourceControl => raw::TF_FLAGTYPE_RESOURCE_CONTROL,
Self::RobotDestruction => raw::TF_FLAGTYPE_ROBOT_DESTRUCTION,
Self::PlayerDestruction => raw::TF_FLAGTYPE_PLAYER_DESTRUCTION,
}
}
}