2 * Copyright (c) 2011 Rudolf Polzer
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
22 #include "sv_model.qh"
25 #include <common/animdecide.qh>
26 #include <common/constants.qh>
27 #include <common/util.qh>
28 #include <server/constants.qh>
29 #include <server/defs.qh>
31 // generic CSQC model code
33 bool CSQCModel_Send(entity this, entity to, int sf)
35 // some nice flags for CSQCMODEL_IF
36 noref bool isplayer = IS_CLIENT(this);
37 noref bool islocalplayer = (this == to);
38 noref bool isnolocalplayer = (isplayer && (this != to));
40 WriteHeader(MSG_ENTITY, ENT_CLIENT_MODEL);
41 WriteInt24_t(MSG_ENTITY, sf);
42 WriteByte(MSG_ENTITY, isplayer);
44 #define CSQCMODEL_IF(cond) if(cond) {
45 #define CSQCMODEL_ENDIF }
46 #define CSQCMODEL_PROPERTY(flag,t,r,w,f) \
49 w(MSG_ENTITY, this.csqcmodel_##f); \
51 #define CSQCMODEL_PROPERTY_SCALED(flag,t,r,w,f,s,mi,ma) CSQCMODEL_PROPERTY(flag,t,r,w,f)
53 #undef CSQCMODEL_PROPERTY_SCALED
54 #undef CSQCMODEL_PROPERTY
55 #undef CSQCMODEL_ENDIF
61 #if CSQCPLAYER_FORCE_UPDATES
62 .float csqcmodel_nextforcedupdate;
64 void CSQCModel_CheckUpdate(entity e)
66 // some nice flags for CSQCMODEL_IF
67 float isplayer = IS_CLIENT(e);
68 float islocalplayer = isplayer; // we set BOTH to 1 here as we need the sendflags
69 float isnolocalplayer = isplayer; // we set BOTH to 1 here as we need the sendflags
71 unused_float = isplayer;
72 unused_float = islocalplayer;
73 unused_float = isnolocalplayer;
75 #if CSQCPLAYER_FORCE_UPDATES
76 if(isplayer && time > e.csqcmodel_nextforcedupdate)
78 e.SendFlags |= CSQCMODEL_PROPERTY_ORIGIN;
79 e.csqcmodel_nextforcedupdate = time + (1 / (CSQCPLAYER_FORCE_UPDATES)) * (0.5 + random()); // ensure about 4 origin sends per sec
83 if(e.effects & EF_RESTARTANIM_BIT)
85 e.SendFlags |= CSQCMODEL_PROPERTY_FRAME | CSQCMODEL_PROPERTY_FRAME2; // full anim resend please
86 e.effects &= ~EF_RESTARTANIM_BIT;
89 if(e.effects & EF_TELEPORT_BIT)
91 e.SendFlags |= CSQCMODEL_PROPERTY_TELEPORTED; // no interpolation please
92 e.effects &= ~EF_TELEPORT_BIT;
95 #define CSQCMODEL_IF(cond) if(cond) {
96 #define CSQCMODEL_ENDIF }
97 #define CSQCMODEL_PROPERTY(flag,t,r,w,f) \
100 if(tmp != e.csqcmodel_##f) \
102 e.csqcmodel_##f = tmp; \
103 e.SendFlags |= flag; \
106 #define CSQCMODEL_PROPERTY_SCALED(flag,t,r,w,f,s,mi,ma) \
108 t tmp = rint(bound(mi, s * e.f, ma) - mi); \
109 if(tmp != e.csqcmodel_##f) \
111 e.csqcmodel_##f = tmp; \
112 e.SendFlags |= flag; \
116 #undef CSQCMODEL_PROPERTY_SCALED
117 #undef CSQCMODEL_PROPERTY
118 #undef CSQCMODEL_ENDIF
122 void CSQCModel_LinkEntity(entity e)
124 e.SendEntity = SendEntity_self;
125 e.SendEntity3 = CSQCModel_Send;
126 e.SendFlags = 0xFFFFFF;
127 CSQCModel_CheckUpdate(e);
130 void CSQCModel_UnlinkEntity(entity e)
132 e.SendEntity = func_null;
133 e.SendEntity3 = func_null;