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
use crate::{
    base_objects::{
        extra_pools::{get_entity_by_id, AnyEntity},
        network_object,
    },
    helpers::{self, IntoHash},
    meta::entity_stream_synced_meta::StreamSyncedEntityMeta,
    sdk,
    vector::Vector3,
    SomeResult, VoidResult,
};

/// # **`NetworkObject implementation`**
impl network_object::NetworkObject {
    pub fn get_by_id(id: u32) -> SomeResult<network_object::NetworkObjectContainer> {
        get_entity_by_id!(AnyEntity::NetworkObject, id)
            .ok_or(anyhow::anyhow!("No network object with id: {id}"))
    }

    /// Creates new instance of NetworkObject with default params.
    /// See [network_object::NetworkObject::new_with_params]
    /// if you want to create network object with custom params.
    ///
    /// # Errors
    /// When the maximum number of entities is created (2^16).
    ///
    /// # Examples
    /// ```rust
    /// let object = altv::NetworkObject::new("prop_bench_04", (0, 0, 71), (1.0, 2.0, 3.0)).unwrap();
    /// ```
    pub fn new(
        model: impl IntoHash,
        pos: impl Into<Vector3>,
        rot: impl Into<Vector3>,
    ) -> SomeResult<network_object::NetworkObjectContainer> {
        Self::new_with_params(model, pos, rot, 255, 0, 100)
    }

    /// Creates new instance of NetworkObject with custom params.
    ///
    /// # Errors
    /// When the maximum number of entities is created (2^16).
    ///
    /// # Examples
    /// ```rust
    /// let object = altv::NetworkObject::new_with_params(
    ///    "prop_bench_04",
    ///    (0, 0, 71),
    ///    (1.0, 2.0, 3.0),
    ///    150,
    ///    0,
    ///    999
    /// ).unwrap();
    /// ```
    pub fn new_with_params(
        model: impl IntoHash,
        pos: impl Into<Vector3>,
        rot: impl Into<Vector3>,
        alpha: u8,
        texture_variation: u8,
        lod_distance: u16,
    ) -> SomeResult<network_object::NetworkObjectContainer> {
        let pos = pos.into();
        let rot = rot.into();

        Ok(helpers::create_base_object!(
            network_object,
            sdk::ICore::CreateNetworkObject(
                model.into_hash(),
                pos.x(),
                pos.y(),
                pos.z(),
                rot.x(),
                rot.y(),
                rot.z(),
                alpha,
                texture_variation,
                lod_distance,
            ),
            anyhow::bail!("NetworkObject model is incorrect or there is no free id for new entity")
        ))
    }

    pub fn destroy(&self) -> VoidResult {
        network_object::remove_from_pool!(self)?;
        self.internal_destroy()
    }

    pub fn alpha(&self) -> SomeResult<u8> {
        Ok(unsafe { sdk::INetworkObject::GetAlpha(self.raw_ptr()?) })
    }

    pub fn set_alpha(&self, alpha: u8) -> VoidResult {
        unsafe { sdk::INetworkObject::SetAlpha(self.raw_ptr()?, alpha) }
        Ok(())
    }

    pub fn texture_variation(&self) -> SomeResult<u8> {
        Ok(unsafe { sdk::INetworkObject::GetTextureVariation(self.raw_ptr()?) })
    }

    pub fn set_texture_variation(&self, texture_variation: u8) -> VoidResult {
        unsafe { sdk::INetworkObject::SetTextureVariation(self.raw_ptr()?, texture_variation) }
        Ok(())
    }

    pub fn lod_distance(&self) -> SomeResult<u16> {
        Ok(unsafe { sdk::INetworkObject::GetLodDistance(self.raw_ptr()?) })
    }

    pub fn set_lod_distance(&self, lod_distance: u16) -> VoidResult {
        unsafe { sdk::INetworkObject::SetLodDistance(self.raw_ptr()?, lod_distance) }
        Ok(())
    }

    pub fn activate_physics(&self) -> VoidResult {
        unsafe { sdk::INetworkObject::ActivatePhysics(self.raw_ptr()?) }
        Ok(())
    }

    pub fn place_on_ground_properly(&self) -> VoidResult {
        unsafe { sdk::INetworkObject::PlaceOnGroundProperly(self.raw_ptr()?) }
        Ok(())
    }
}

impl StreamSyncedEntityMeta for network_object::NetworkObject {}