]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/csqcmodel/sv_model.qc
take3: format 903 files
[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
26 // generic CSQC model code
27
28 bool CSQCModel_Send(entity this, entity to, int sf)
29 {
30         // some nice flags for CSQCMODEL_IF
31         noref bool isplayer = IS_CLIENT(this);
32         noref bool islocalplayer = (this == to);
33         noref bool isnolocalplayer = (isplayer && (this != to));
34
35         WriteHeader(MSG_ENTITY, ENT_CLIENT_MODEL);
36         WriteInt24_t(MSG_ENTITY, sf);
37         WriteByte(MSG_ENTITY, isplayer);
38
39 #define CSQCMODEL_IF(cond) if (cond) {
40 #define CSQCMODEL_ENDIF \
41         }
42 #define CSQCMODEL_PROPERTY(flag, t, r, w, f) \
43         if (sf & flag) { \
44                 w(MSG_ENTITY, this.csqcmodel_##f); \
45         }
46 #define CSQCMODEL_PROPERTY_SCALED(flag, t, r, w, f, s, mi, ma) CSQCMODEL_PROPERTY(flag, t, r, w, f)
47         ALLPROPERTIES
48 #undef CSQCMODEL_PROPERTY_SCALED
49 #undef CSQCMODEL_PROPERTY
50 #undef CSQCMODEL_ENDIF
51 #undef CSQCMODEL_IF
52
53         return true;
54 }
55
56 #if CSQCPLAYER_FORCE_UPDATES
57 .float csqcmodel_nextforcedupdate;
58 #endif
59 void CSQCModel_CheckUpdate(entity e)
60 {
61         // some nice flags for CSQCMODEL_IF
62         noref float isplayer = IS_CLIENT(e);
63         noref float islocalplayer = isplayer;   // we set BOTH to 1 here as we need the sendflags
64         noref float isnolocalplayer = isplayer; // we set BOTH to 1 here as we need the sendflags
65
66 #if CSQCPLAYER_FORCE_UPDATES
67         if (isplayer && time > e.csqcmodel_nextforcedupdate) {
68                 e.SendFlags |= CSQCMODEL_PROPERTY_ORIGIN;
69                 e.csqcmodel_nextforcedupdate = time + (1 / (CSQCPLAYER_FORCE_UPDATES)) * (0.5 + random()); // ensure about 4 origin sends per sec
70         }
71 #endif
72
73         if (e.effects & EF_RESTARTANIM_BIT) {
74                 e.SendFlags |= CSQCMODEL_PROPERTY_FRAME | CSQCMODEL_PROPERTY_FRAME2; // full anim resend please
75                 e.effects &= ~EF_RESTARTANIM_BIT;
76         }
77
78         if (e.effects & EF_TELEPORT_BIT) {
79                 e.SendFlags |= CSQCMODEL_PROPERTY_TELEPORTED; // no interpolation please
80                 e.effects &= ~EF_TELEPORT_BIT;
81         }
82
83 #define CSQCMODEL_IF(cond) if (cond) {
84 #define CSQCMODEL_ENDIF \
85         }
86 #define CSQCMODEL_PROPERTY(flag, t, r, w, f) \
87         { \
88                 t tmp = e.f; \
89                 if (tmp != e.csqcmodel_##f) { \
90                         e.csqcmodel_##f = tmp; \
91                         e.SendFlags |= flag; \
92                 } \
93         }
94 #define CSQCMODEL_PROPERTY_SCALED(flag, t, r, w, f, s, mi, ma) \
95         { \
96                 t tmp = rint(bound(mi, s * e.f, ma) - mi); \
97                 if (tmp != e.csqcmodel_##f) { \
98                         e.csqcmodel_##f = tmp; \
99                         e.SendFlags |= flag; \
100                 } \
101         }
102         ALLPROPERTIES
103 #undef CSQCMODEL_PROPERTY_SCALED
104 #undef CSQCMODEL_PROPERTY
105 #undef CSQCMODEL_ENDIF
106 #undef CSQCMODEL_IF
107 }
108
109 void CSQCModel_LinkEntity(entity e)
110 {
111         setSendEntity(e, CSQCModel_Send);
112         e.SendFlags = 0xFFFFFF;
113         CSQCModel_CheckUpdate(e);
114 }
115
116 void CSQCModel_UnlinkEntity(entity e)
117 {
118         setSendEntity(e, func_null);
119 }