]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/effects/qc/casings.qc
Fix FL_WEAPON flag overlapping FL_JUMPRELEASED. This unintentional change was introdu...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / effects / qc / casings.qc
1 #include "casings.qh"
2
3 #include <common/replicate.qh>
4 #include <common/util.qh>
5
6 #ifdef CSQC
7 #include <common/physics/movetypes/movetypes.qh>
8 #include "rubble.qh"
9 #endif
10
11 REGISTER_NET_TEMP(casings)
12
13 REPLICATE(cvar_cl_casings, bool, "cl_casings");
14 REPLICATE(cvar_r_drawviewmodel, int, "r_drawviewmodel");
15
16 #ifdef SVQC
17 void SpawnCasing(vector vel, vector ang, int casingtype, entity casingowner, .entity weaponentity)
18 {
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;
21
22         FOREACH_CLIENT(true, {
23                 if (!(CS_CVAR(it).cvar_cl_casings))
24                         continue;
25
26                 casingtype &= 0x3F; // reset any bitflags that were set for the previous client
27
28                 if (it == casingowner || (IS_SPEC(it) && it.enemy == casingowner))
29                 {
30                         if (!(CS_CVAR(it).cvar_r_drawviewmodel))
31                                 continue;
32
33                         casingtype |= 0x40; // client will apply autocvar_cl_gunoffset in first person
34                 }
35                 else if (1 & ~checkpvs(it.origin + it.view_ofs, casingowner)) // 1 or 3 means visible
36                         continue;
37
38                 msg_entity = it; // sound_allowed checks this
39                 if (!sound_allowed(MSG_ONE, it))
40                         casingtype |= 0x80; // silent
41
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
49         });
50 }
51 #endif
52
53 #ifdef CSQC
54 entityclass(Casing);
55 classfield(Casing) .float alpha;
56 classfield(Casing) .bool silent;
57 classfield(Casing) .int state;
58 classfield(Casing) .float cnt;
59
60 // this is only needed because LimitedChildrenRubble() takes a func pointer
61 void Casing_Delete(entity this)
62 {
63     delete(this);
64 }
65
66 void Casing_Draw(entity this)
67 {
68     if (IS_ONGROUND(this))
69     {
70         this.angles_x = 0;
71         this.angles_z = 0;
72         //UNSET_ONGROUND(this);
73     }
74
75     this.renderflags = 0;
76     this.alpha = bound(0, this.cnt - time, 1);
77
78     if (this.alpha < ALPHA_MIN_VISIBLE)
79     {
80         delete(this);
81         this.drawmask = 0;
82         return;
83     }
84
85     trace_startsolid = 0; // due to cl_casings_ticrate, traces are not always performed
86     Movetype_Physics_MatchTicrate(this, autocvar_cl_casings_ticrate, autocvar_cl_casings_sloppy);
87     //if (wasfreed(this))
88     //    return; // deleted by touch function
89
90     // prevent glitchy casings when the gun model is poking into a wall
91     // doing this here is cheaper than doing it on the server as the client performs the trace anyway
92     if (trace_startsolid)
93     {
94         delete(this);
95         this.drawmask = 0;
96         return;
97     }
98 }
99
100 SOUND(BRASS1, W_Sound("brass1"));
101 SOUND(BRASS2, W_Sound("brass2"));
102 SOUND(BRASS3, W_Sound("brass3"));
103 Sound SND_BRASS_RANDOM() {
104     return REGISTRY_GET(Sounds, SND_BRASS1.m_id + floor(prandom() * 3));
105 }
106 SOUND(CASINGS1, W_Sound("casings1"));
107 SOUND(CASINGS2, W_Sound("casings2"));
108 SOUND(CASINGS3, W_Sound("casings3"));
109 Sound SND_CASINGS_RANDOM() {
110     return REGISTRY_GET(Sounds, SND_CASINGS1.m_id + floor(prandom() * 3));
111 }
112
113 void Casing_Touch(entity this, entity toucher)
114 {
115     if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
116     {
117         delete(this);
118         return;
119     }
120
121     if (!this.silent)
122     if (!trace_ent || trace_ent.solid == SOLID_BSP)
123     {
124         if(vdist(this.velocity, >, 50))
125         {
126             if (time >= this.nextthink)
127             {
128                 Sound s;
129                 switch (this.state)
130                 {
131                     case 1:
132                         s = SND_CASINGS_RANDOM();
133                         break;
134                     default:
135                         s = SND_BRASS_RANDOM();
136                         break;
137                 }
138
139                 sound (this, CH_SHOTS, s, VOL_BASE, ATTEN_LARGE);
140             }
141         }
142     }
143
144     this.nextthink = time + 0.2;
145 }
146
147 void Casing_Damage(entity this, float thisdmg, int hittype, vector org, vector thisforce)
148 {
149     if (thisforce.z < 0)
150         thisforce.z = 0;
151     this.velocity = this.velocity + thisforce + '0 0 100';
152     UNSET_ONGROUND(this);
153 }
154
155 NET_HANDLE(casings, bool isNew)
156 {
157     Casing casing = ListNewChildRubble(CasingsNGibs, new(casing));
158
159     casing.state = ReadByte();
160     casing.origin = ReadVector();
161     casing.velocity = decompressShortVector(ReadShort());
162     casing.angles_x = ReadByte() * 360 / 256;
163     casing.angles_y = ReadByte() * 360 / 256;
164
165     return = true;
166
167     casing.silent = casing.state & 0x80;
168     if ((casing.state & 0x40) && !autocvar_chase_active)
169         casing.origin += autocvar_cl_gunoffset.x * v_forward
170                        - autocvar_cl_gunoffset.y * v_right
171                        + autocvar_cl_gunoffset.z * v_up;
172     casing.state &= 0x3F; // the 2 most significant bits are reserved for the silent and casingowner bitflags
173
174     setorigin(casing, casing.origin);
175     casing.drawmask = MASK_NORMAL;
176     casing.draw = Casing_Draw;
177     if (isNew) IL_PUSH(g_drawables, casing);
178     casing.velocity += 2 * prandomvec();
179     casing.avelocity = '0 10 0' + 100 * prandomvec();
180     set_movetype(casing, MOVETYPE_BOUNCE);
181     settouch(casing, Casing_Touch);
182     casing.move_time = time;
183     casing.event_damage = Casing_Damage;
184     casing.solid = SOLID_TRIGGER;
185
186     switch (casing.state)
187     {
188         case 1:
189             setmodel(casing, MDL_CASING_SHELL);
190             casing.bouncefactor = 0.25;
191             casing.cnt = time + autocvar_cl_casings_shell_time;
192             break;
193         default:
194             setmodel(casing, MDL_CASING_BULLET);
195             casing.bouncefactor = 0.5;
196             casing.cnt = time + autocvar_cl_casings_bronze_time;
197             break;
198     }
199
200     LimitedChildrenRubble(CasingsNGibs, "casing", autocvar_cl_casings_maxcount, Casing_Delete, NULL);
201 }
202
203 #endif