]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/state.qh
Merge branch 'master' into TimePath/notifications
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / state.qh
1 #pragma once
2
3 /**
4  * Purpose: common player state, usable on client and server
5  * Client: singleton representing the viewed player
6  * Server: instance per client, clients decoupled from players
7  */
8 CLASS(PlayerState, Object)
9         ATTRIB(PlayerState, m_client, entity, NULL)
10         CONSTRUCTOR(PlayerState, entity client)
11         {
12                 CONSTRUCT(PlayerState);
13                 this.m_client = client;
14         }
15         ATTRIB(PlayerState, m_switchingweapon, Weapon, Weapons_from(-1))
16         ATTRIB(PlayerState, m_switchweapon, Weapon, Weapons_from(-1))
17         ATTRIB(PlayerState, m_weapon, Weapon, Weapons_from(-1))
18         METHOD(PlayerState, ps_push, void(PlayerState this, entity cl))
19         {
20                 STAT(ACTIVEWEAPON, cl) = this.m_weapon.m_id;
21                 STAT(SWITCHINGWEAPON, cl) = this.m_switchingweapon.m_id;
22                 STAT(SWITCHWEAPON, cl) = this.m_switchweapon.m_id;
23         }
24 ENDCLASS(PlayerState)
25
26 .PlayerState _ps;
27 #if NDEBUG
28         #define PS(this) (this._ps)
29 #else
30         PlayerState PS(entity this) { assert(IS_CLIENT(this)); return this._ps; }
31 #endif
32
33 void Inventory_new(entity this);
34 void Inventory_delete(entity this);
35
36 // TODO: renew on death
37
38 void PlayerState_attach(entity this)
39 {
40         this._ps = NEW(PlayerState, this);
41
42         Inventory_new(this);
43 }
44
45 void PlayerState_detach(entity this)
46 {
47         if (!PS(this)) return;  // initial connect
48         remove(PS(this));
49         this._ps = NULL;
50
51     Inventory_delete(self);
52 }
53
54 /**
55  * Purpose: common client state, usable on client and server
56  * Client: singleton representing the viewed player
57  * Server: instance per client
58  */
59 CLASS(ClientState, Object)
60         ATTRIB(ClientState, m_client, entity, NULL)
61         CONSTRUCTOR(ClientState, entity client)
62         {
63                 CONSTRUCT(ClientState);
64                 this.m_client = client;
65         }
66 ENDCLASS(ClientState)
67
68 .ClientState _cs;
69
70 #if NDEBUG
71         #define CS(this) (this._cs)
72 #else
73         ClientState CS(entity this) { assert(IS_CLIENT(this)); assert(this._cs); return this._cs; }
74 #endif
75
76 void GetCvars(int);
77 void DecodeLevelParms(entity this);
78 void PlayerScore_Attach(entity this);
79 void ClientData_Attach(entity this);
80 void accuracy_init(entity this);
81 void entcs_attach(entity this);
82 void playerdemo_init(entity this);
83 void anticheat_init(entity this);
84 void W_HitPlotOpen(entity this);
85 void bot_clientconnect(entity this);
86
87 void ClientState_attach(entity this)
88 {
89         this._cs = NEW(ClientState, this);
90
91     GetCvars(0);  // get other cvars from player
92
93         // TODO: xonstat elo.txt support, until then just 404s
94         if (false && IS_REAL_CLIENT(this)) { PlayerStats_PlayerBasic_CheckUpdate(this); }
95
96         // TODO: fold all of these into ClientState
97
98         DecodeLevelParms(this);
99
100         PlayerScore_Attach(this);
101         ClientData_Attach(this);
102         accuracy_init(this);
103         entcs_attach(this);
104         playerdemo_init(this);
105         anticheat_init(this);
106         W_HitPlotOpen(this);
107
108         bot_clientconnect(this);
109 }
110
111 void bot_clientdisconnect();
112 void W_HitPlotClose(entity this);
113 void anticheat_report();
114 void playerdemo_shutdown();
115 void entcs_detach(entity this);
116 void accuracy_free(entity this);
117 void ClientData_Detach(entity this);
118 void PlayerScore_Detach(entity this);
119
120 void ClientState_detach(entity this)
121 {
122         remove(CS(this));
123         this._cs = NULL;
124
125     GetCvars(-1);  // free cvars
126
127     bot_clientdisconnect();
128
129     W_HitPlotClose(this);
130     anticheat_report();
131     playerdemo_shutdown();
132     entcs_detach(this);
133     accuracy_free(self);
134     ClientData_Detach(this);
135     PlayerScore_Detach(self);
136 }