]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/effects/qc/casings.qc
0e851da7f79809e6eec5a03de2f48fcd53701c25
[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, float randomvel, vector ang, vector avel, float randomavel, 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 void Casing_Delete(entity this)
61 {
62     delete(this);
63 }
64
65 void Casing_Draw(entity this)
66 {
67     if (IS_ONGROUND(this))
68     {
69         this.angles_x = 0;
70         this.angles_z = 0;
71         //UNSET_ONGROUND(this);
72     }
73
74     this.renderflags = 0;
75     this.alpha = bound(0, this.cnt - time, 1);
76
77     if (this.alpha < ALPHA_MIN_VISIBLE)
78     {
79         Casing_Delete(this);
80         this.drawmask = 0;
81         return;
82     }
83
84     Movetype_Physics_MatchTicrate(this, autocvar_cl_casings_ticrate, autocvar_cl_casings_sloppy);
85     //if (wasfreed(this))
86     //    return; // deleted by touch function
87 }
88
89 SOUND(BRASS1, W_Sound("brass1"));
90 SOUND(BRASS2, W_Sound("brass2"));
91 SOUND(BRASS3, W_Sound("brass3"));
92 Sound SND_BRASS_RANDOM() {
93     return REGISTRY_GET(Sounds, SND_BRASS1.m_id + floor(prandom() * 3));
94 }
95 SOUND(CASINGS1, W_Sound("casings1"));
96 SOUND(CASINGS2, W_Sound("casings2"));
97 SOUND(CASINGS3, W_Sound("casings3"));
98 Sound SND_CASINGS_RANDOM() {
99     return REGISTRY_GET(Sounds, SND_CASINGS1.m_id + floor(prandom() * 3));
100 }
101
102 void Casing_Touch(entity this, entity toucher)
103 {
104     if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
105     {
106         Casing_Delete(this);
107         return;
108     }
109
110     if (!this.silent)
111     if (!trace_ent || trace_ent.solid == SOLID_BSP)
112     {
113         if(vdist(this.velocity, >, 50))
114         {
115             if (time >= this.nextthink)
116             {
117                 Sound s;
118                 switch (this.state)
119                 {
120                     case 1:
121                         s = SND_CASINGS_RANDOM();
122                         break;
123                     default:
124                         s = SND_BRASS_RANDOM();
125                         break;
126                 }
127
128                 sound (this, CH_SHOTS, s, VOL_BASE, ATTEN_LARGE);
129             }
130         }
131     }
132
133     this.nextthink = time + 0.2;
134 }
135
136 void Casing_Damage(entity this, float thisdmg, int hittype, vector org, vector thisforce)
137 {
138     if (thisforce.z < 0)
139         thisforce.z = 0;
140     this.velocity = this.velocity + thisforce + '0 0 100';
141     UNSET_ONGROUND(this);
142 }
143
144 NET_HANDLE(casings, bool isNew)
145 {
146     Casing casing = ListNewChildRubble(CasingsNGibs, new(casing));
147
148     casing.state = ReadByte();
149     casing.origin = ReadVector();
150     casing.velocity = decompressShortVector(ReadShort());
151     casing.angles_x = ReadByte() * 360 / 256;
152     casing.angles_y = ReadByte() * 360 / 256;
153
154     return = true;
155
156     casing.silent = casing.state & 0x80;
157     if (casing.state & 0x40 && !autocvar_chase_active)
158         casing.origin += autocvar_cl_gunoffset.x * v_forward
159                        - autocvar_cl_gunoffset.y * v_right
160                        + autocvar_cl_gunoffset.z * v_up;
161     casing.state &= 0x3F; // the 2 most significant bits are reserved for the silent and casingowner bitflags
162
163     setorigin(casing, casing.origin);
164     casing.drawmask = MASK_NORMAL;
165     casing.draw = Casing_Draw;
166     if (isNew) IL_PUSH(g_drawables, casing);
167     casing.velocity += 2 * prandomvec();
168     casing.avelocity = '0 250 0' + 100 * prandomvec();
169     set_movetype(casing, MOVETYPE_BOUNCE);
170     casing.bouncefactor = 0.25;
171     settouch(casing, Casing_Touch);
172     casing.move_time = time;
173     casing.event_damage = Casing_Damage;
174     casing.solid = SOLID_TRIGGER;
175
176     switch (casing.state)
177     {
178         case 1:
179             setmodel(casing, MDL_CASING_SHELL);
180             casing.cnt = time + autocvar_cl_casings_shell_time;
181             break;
182         default:
183             setmodel(casing, MDL_CASING_BULLET);
184             casing.cnt = time + autocvar_cl_casings_bronze_time;
185             break;
186     }
187
188     setsize(casing, '0 0 -1', '0 0 -1');
189
190     LimitedChildrenRubble(CasingsNGibs, "casing", autocvar_cl_casings_maxcount, Casing_Delete, NULL);
191 }
192
193 #endif