3 #include <common/replicate.qh>
4 #include <common/util.qh>
7 #include <common/physics/movetypes/movetypes.qh>
11 REGISTER_NET_TEMP(casings)
13 REPLICATE(cvar_cl_casings, bool, "cl_casings");
14 REPLICATE(cvar_r_drawviewmodel, int, "r_drawviewmodel");
17 void SpawnCasing(vector vel, vector ang, int casingtype, entity casingowner, .entity weaponentity)
19 vector org = casingowner.(weaponentity).spawnorigin;
20 org = casingowner.origin + casingowner.view_ofs + org.x * v_forward - org.y * v_right + org.z * v_up;
22 FOREACH_CLIENT(true, {
23 if (!(CS_CVAR(it).cvar_cl_casings))
26 casingtype &= 0x3F; // reset any bitflags that were set for the previous client
28 if (it == casingowner || (IS_SPEC(it) && it.enemy == casingowner))
30 if (!(CS_CVAR(it).cvar_r_drawviewmodel))
33 casingtype |= 0x40; // client will apply autocvar_cl_gunoffset in first person
35 else if (1 & ~checkpvs(it.origin + it.view_ofs, casingowner)) // 1 or 3 means visible
38 msg_entity = it; // sound_allowed checks this
39 if (!sound_allowed(MSG_ONE, it))
40 casingtype |= 0x80; // silent
42 WriteHeader(MSG_ONE, casings);
43 WriteByte(MSG_ONE, casingtype);
44 WriteVector(MSG_ONE, org);
45 WriteShort(MSG_ONE, compressShortVector(vel)); // actually compressed velocity
46 WriteByte(MSG_ONE, ang.x * 256 / 360);
47 WriteByte(MSG_ONE, ang.y * 256 / 360);
48 // weapons only have pitch and yaw, so no need to send ang.z
55 classfield(Casing) .float alpha;
56 classfield(Casing) .bool silent;
57 classfield(Casing) .int state;
58 classfield(Casing) .float cnt;
60 // this is only needed because LimitedChildrenRubble() takes a func pointer
61 void Casing_Delete(entity this)
66 void Casing_Draw(entity this)
68 if (IS_ONGROUND(this))
72 //UNSET_ONGROUND(this);
76 this.alpha = bound(0, this.cnt - time, 1);
78 if (this.alpha < ALPHA_MIN_VISIBLE)
85 Movetype_Physics_MatchTicrate(this, autocvar_cl_casings_ticrate, autocvar_cl_casings_sloppy);
87 // return; // deleted by touch function
90 SOUND(BRASS1, W_Sound("brass1"));
91 SOUND(BRASS2, W_Sound("brass2"));
92 SOUND(BRASS3, W_Sound("brass3"));
93 Sound SND_BRASS_RANDOM() {
94 return REGISTRY_GET(Sounds, SND_BRASS1.m_id + floor(prandom() * 3));
96 SOUND(CASINGS1, W_Sound("casings1"));
97 SOUND(CASINGS2, W_Sound("casings2"));
98 SOUND(CASINGS3, W_Sound("casings3"));
99 Sound SND_CASINGS_RANDOM() {
100 return REGISTRY_GET(Sounds, SND_CASINGS1.m_id + floor(prandom() * 3));
103 void Casing_Touch(entity this, entity toucher)
105 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
112 if (!trace_ent || trace_ent.solid == SOLID_BSP)
114 if(vdist(this.velocity, >, 50))
116 if (time >= this.nextthink)
122 s = SND_CASINGS_RANDOM();
125 s = SND_BRASS_RANDOM();
129 sound (this, CH_SHOTS, s, VOL_BASE, ATTEN_LARGE);
134 this.nextthink = time + 0.2;
137 void Casing_Damage(entity this, float thisdmg, int hittype, vector org, vector thisforce)
141 this.velocity = this.velocity + thisforce + '0 0 100';
142 UNSET_ONGROUND(this);
145 NET_HANDLE(casings, bool isNew)
147 Casing casing = ListNewChildRubble(CasingsNGibs, new(casing));
149 casing.state = ReadByte();
150 casing.origin = ReadVector();
151 casing.velocity = decompressShortVector(ReadShort());
152 casing.angles_x = ReadByte() * 360 / 256;
153 casing.angles_y = ReadByte() * 360 / 256;
157 casing.silent = casing.state & 0x80;
158 if (casing.state & 0x40 && !autocvar_chase_active)
159 casing.origin += autocvar_cl_gunoffset.x * v_forward
160 - autocvar_cl_gunoffset.y * v_right
161 + autocvar_cl_gunoffset.z * v_up;
162 casing.state &= 0x3F; // the 2 most significant bits are reserved for the silent and casingowner bitflags
164 setorigin(casing, casing.origin);
165 casing.drawmask = MASK_NORMAL;
166 casing.draw = Casing_Draw;
167 if (isNew) IL_PUSH(g_drawables, casing);
168 casing.velocity += 2 * prandomvec();
169 casing.avelocity = '0 10 0' + 100 * prandomvec();
170 set_movetype(casing, MOVETYPE_BOUNCE);
171 settouch(casing, Casing_Touch);
172 casing.move_time = time;
173 casing.event_damage = Casing_Damage;
174 casing.solid = SOLID_TRIGGER;
176 switch (casing.state)
179 setmodel(casing, MDL_CASING_SHELL);
180 casing.bouncefactor = 0.25;
181 casing.cnt = time + autocvar_cl_casings_shell_time;
184 setmodel(casing, MDL_CASING_BULLET);
185 casing.bouncefactor = 0.5;
186 casing.cnt = time + autocvar_cl_casings_bronze_time;
190 setsize(casing, '0 0 -1', '0 0 -1');
192 LimitedChildrenRubble(CasingsNGibs, "casing", autocvar_cl_casings_maxcount, Casing_Delete, NULL);