]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/csqcmodel/sv_model.qc
Merge branch 'DefaultUser/m_itemid' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / csqcmodel / sv_model.qc
1 /*
2  * Copyright (c) 2011 Rudolf Polzer
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  */
22 #include "sv_model.qh"
23
24 #include "common.qh"
25 #include <common/animdecide.qh>
26 #include <common/constants.qh>
27 #include <common/net_linked.qh>
28 #include <common/util.qh>
29 #include <server/constants.qh>
30 #include <server/defs.qh>
31
32 // generic CSQC model code
33
34 bool CSQCModel_Send(entity this, entity to, int sf)
35 {
36         // some nice flags for CSQCMODEL_IF
37         noref bool isplayer = IS_CLIENT(this);
38         noref bool islocalplayer = (this == to);
39         noref bool isnolocalplayer = (isplayer && (this != to));
40
41         WriteHeader(MSG_ENTITY, ENT_CLIENT_MODEL);
42         WriteInt24_t(MSG_ENTITY, sf);
43         WriteByte(MSG_ENTITY, isplayer);
44
45 #define CSQCMODEL_IF(cond) if(cond) {
46 #define CSQCMODEL_ENDIF }
47 #define CSQCMODEL_PROPERTY(flag,t,r,w,f) \
48         if(sf & flag) \
49         { \
50                 w(MSG_ENTITY, this.csqcmodel_##f); \
51         }
52 #define CSQCMODEL_PROPERTY_SCALED(flag,t,r,w,f,s,mi,ma) CSQCMODEL_PROPERTY(flag,t,r,w,f)
53         ALLPROPERTIES
54 #undef CSQCMODEL_PROPERTY_SCALED
55 #undef CSQCMODEL_PROPERTY
56 #undef CSQCMODEL_ENDIF
57 #undef CSQCMODEL_IF
58
59         return true;
60 }
61
62 #if CSQCPLAYER_FORCE_UPDATES
63 .float csqcmodel_nextforcedupdate;
64 #endif
65 void CSQCModel_CheckUpdate(entity e)
66 {
67         // some nice flags for CSQCMODEL_IF
68         noref float isplayer = IS_CLIENT(e);
69         noref float islocalplayer = isplayer; // we set BOTH to 1 here as we need the sendflags
70         noref float isnolocalplayer = isplayer; // we set BOTH to 1 here as we need the sendflags
71
72 #if CSQCPLAYER_FORCE_UPDATES
73         if(isplayer && time > e.csqcmodel_nextforcedupdate)
74         {
75                 e.SendFlags |= CSQCMODEL_PROPERTY_ORIGIN;
76                 e.csqcmodel_nextforcedupdate = time + (1 / (CSQCPLAYER_FORCE_UPDATES)) * (0.5 + random()); // ensure about 4 origin sends per sec
77         }
78 #endif
79
80         if(e.effects & EF_RESTARTANIM_BIT)
81         {
82                 e.SendFlags |= CSQCMODEL_PROPERTY_FRAME | CSQCMODEL_PROPERTY_FRAME2; // full anim resend please
83                 e.effects &= ~EF_RESTARTANIM_BIT;
84         }
85
86         if(e.effects & EF_TELEPORT_BIT)
87         {
88                 e.SendFlags |= CSQCMODEL_PROPERTY_TELEPORTED; // no interpolation please
89                 e.effects &= ~EF_TELEPORT_BIT;
90         }
91
92 #define CSQCMODEL_IF(cond) if(cond) {
93 #define CSQCMODEL_ENDIF }
94 #define CSQCMODEL_PROPERTY(flag,t,r,w,f) \
95         { \
96                 t tmp = e.f; \
97                 if(tmp != e.csqcmodel_##f) \
98                 { \
99                         e.csqcmodel_##f = tmp; \
100                         e.SendFlags |= flag; \
101                 } \
102         }
103 #define CSQCMODEL_PROPERTY_SCALED(flag,t,r,w,f,s,mi,ma) \
104         { \
105                 t tmp = rint(bound(mi, s * e.f, ma) - mi); \
106                 if(tmp != e.csqcmodel_##f) \
107                 { \
108                         e.csqcmodel_##f = tmp; \
109                         e.SendFlags |= flag; \
110                 } \
111         }
112         ALLPROPERTIES
113 #undef CSQCMODEL_PROPERTY_SCALED
114 #undef CSQCMODEL_PROPERTY
115 #undef CSQCMODEL_ENDIF
116 #undef CSQCMODEL_IF
117 }
118
119 void CSQCModel_LinkEntity(entity e)
120 {
121         setSendEntity(e, CSQCModel_Send);
122         e.SendFlags = 0xFFFFFF;
123         CSQCModel_CheckUpdate(e);
124 }
125
126 void CSQCModel_UnlinkEntity(entity e)
127 {
128         setSendEntity(e, func_null);
129 }