#[cfg(test)]
#[path = "../../tests/tf2/objectives/zones.rs"]
mod tests;
use super::{Objective, ObjectiveError};
use crate::Server;
use crate::entities::Entity;
use crate::inputs::InputValue;
use crate::tf2::objects::ObjectKind;
use crate::tf2::scoreboard::ScoringTeam;
use std::ffi::{CStr, CString};
#[doc(alias("func_nobuild", "CFuncNoBuild"))]
#[derive(Debug, Clone, Copy)]
pub struct NoBuildZone<'s>(Objective<'s>);
impl<'s> NoBuildZone<'s> {
pub fn new(server: Server<'s>, entity: Entity<'s>) -> Result<Self, ObjectiveError> {
Objective::new(server, entity, c"CFuncNoBuild", "func_nobuild").map(Self)
}
#[doc(alias("m_bAllowSentry", "m_bAllowDispenser", "m_bAllowTeleporters"))]
pub fn allows(self, kind: ObjectKind) -> Result<bool, ObjectiveError> {
let field = match kind {
ObjectKind::Dispenser => c"m_bAllowDispenser",
ObjectKind::Sentry => c"m_bAllowSentry",
ObjectKind::Teleporter => c"m_bAllowTeleporters",
};
self.0.bool_field(c"CFuncNoBuild", field)
}
#[doc(alias("m_bDestroyBuildingsOnActive", "DestroyBuildings"))]
pub fn destroys_buildings(self) -> Result<bool, ObjectiveError> {
self.0
.bool_field(c"CFuncNoBuild", c"m_bDestroyBuildingsOnActive")
}
pub fn entity(self) -> Entity<'s> {
self.0.entity()
}
#[doc(alias("m_bDisabled", "StartDisabled", "GetActive"))]
pub fn is_active(self) -> Result<bool, ObjectiveError> {
Ok(!self.0.bool_field(c"CBaseTrigger", c"m_bDisabled")?)
}
#[doc(alias("SetActive", "SetInactive"))]
pub fn set_active(self, active: bool) -> Result<(), ObjectiveError> {
let input = if active { c"SetActive" } else { c"SetInactive" };
self.0.input(input, InputValue::Void)
}
#[doc(alias("AllowSentry", "AllowDispenser", "AllowTeleporters"))]
pub fn set_allows(self, kind: ObjectKind, allows: bool) -> Result<(), ObjectiveError> {
let key = match kind {
ObjectKind::Dispenser => c"AllowDispenser",
ObjectKind::Sentry => c"AllowSentry",
ObjectKind::Teleporter => c"AllowTeleporters",
};
self.0.set_key_value(key, flag_value(allows))
}
#[doc(alias("m_iTeamNum", "TeamNum"))]
pub fn team(self) -> Result<Option<ScoringTeam>, ObjectiveError> {
team(self.0)
}
#[doc(alias("ToggleActive"))]
pub fn toggle_active(self) -> Result<(), ObjectiveError> {
self.0.input(c"ToggleActive", InputValue::Void)
}
}
#[doc(alias("func_regenerate", "CRegenerateZone", "resupply"))]
#[derive(Debug, Clone, Copy)]
pub struct RegenerateZone<'s>(Objective<'s>);
impl<'s> RegenerateZone<'s> {
pub fn new(server: Server<'s>, entity: Entity<'s>) -> Result<Self, ObjectiveError> {
Objective::new(server, entity, c"CRegenerateZone", "func_regenerate").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_iTeamNum", "TeamNum"))]
pub fn team(self) -> Result<Option<ScoringTeam>, ObjectiveError> {
team(self.0)
}
#[doc(alias("Toggle"))]
pub fn toggle(self) -> Result<(), ObjectiveError> {
self.0.input(c"Toggle", InputValue::Void)
}
}
#[doc(alias("func_respawnroom", "CFuncRespawnRoom", "spawn room"))]
#[derive(Debug, Clone, Copy)]
pub struct RespawnRoom<'s>(Objective<'s>);
impl<'s> RespawnRoom<'s> {
pub fn new(server: Server<'s>, entity: Entity<'s>) -> Result<Self, ObjectiveError> {
Objective::new(server, entity, c"CFuncRespawnRoom", "func_respawnroom").map(Self)
}
pub fn entity(self) -> Entity<'s> {
self.0.entity()
}
#[doc(alias("SetActive", "SetInactive"))]
pub fn set_active(self, active: bool) -> Result<(), ObjectiveError> {
let input = if active { c"SetActive" } else { c"SetInactive" };
self.0.input(input, InputValue::Void)
}
#[doc(alias("m_iTeamNum", "TeamNum"))]
pub fn team(self) -> Result<Option<ScoringTeam>, ObjectiveError> {
team(self.0)
}
#[doc(alias("ToggleActive"))]
pub fn toggle_active(self) -> Result<(), ObjectiveError> {
self.0.input(c"ToggleActive", InputValue::Void)
}
}
#[doc(alias("func_respawnroomvisualizer", "CFuncRespawnRoomVisualizer"))]
#[derive(Debug, Clone, Copy)]
pub struct RespawnRoomVisualizer<'s>(Objective<'s>);
impl<'s> RespawnRoomVisualizer<'s> {
pub fn new(server: Server<'s>, entity: Entity<'s>) -> Result<Self, ObjectiveError> {
Objective::new(
server,
entity,
c"CFuncRespawnRoomVisualizer",
"func_respawnroomvisualizer",
)
.map(Self)
}
pub fn entity(self) -> Entity<'s> {
self.0.entity()
}
#[doc(alias("m_bSolid", "solid_to_enemies"))]
pub fn is_solid(self) -> Result<bool, ObjectiveError> {
self.0
.bool_field(c"CFuncRespawnRoomVisualizer", c"m_bSolid")
}
pub fn room(self) -> Result<Option<RespawnRoom<'s>>, ObjectiveError> {
let name = self.room_name()?;
super::find_by_name(self.0.server(), &name, RespawnRoom::new)
}
#[doc(alias("m_iszRespawnRoomName", "respawnroomname"))]
pub fn room_name(self) -> Result<CString, ObjectiveError> {
self.0
.string_key(c"CFuncRespawnRoomVisualizer", c"respawnroomname")
}
#[doc(alias("SetSolid"))]
pub fn set_solid(self, solid: bool) -> Result<(), ObjectiveError> {
self.0.input(c"SetSolid", InputValue::Bool(solid))
}
#[doc(alias("m_iTeamNum", "TeamNum"))]
pub fn team(self) -> Result<Option<ScoringTeam>, ObjectiveError> {
team(self.0)
}
}
fn flag_value(value: bool) -> &'static CStr {
if value { c"1" } else { c"0" }
}
fn team(objective: Objective<'_>) -> Result<Option<ScoringTeam>, ObjectiveError> {
Ok(ScoringTeam::from_raw(
objective.int_field(c"CBaseEntity", c"m_iTeamNum")?,
))
}