]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/effects/qc/casings.qc
d07557f9ac011be83a17d6cd2aacaa74bce1a1bb
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / effects / qc / casings.qc
1 #include "casings.qh"
2
3 #include <common/util.qh>
4
5 #ifdef CSQC
6 #include <common/physics/movetypes/movetypes.qh>
7 #include "rubble.qh"
8 #endif
9
10 REGISTER_NET_TEMP(casings)
11
12 #if defined(SVQC)
13 .bool cvar_cl_casings;
14 #elif defined(CSQC)
15 bool cvar_cl_casings;
16 #endif
17 REPLICATE(cvar_cl_casings, bool, "cl_casings");
18
19 #ifdef SVQC
20 void SpawnCasing(vector vel, float randomvel, vector ang, vector avel, float randomavel, int casingtype, entity casingowner, .entity weaponentity)
21 {
22         entity wep = casingowner.(weaponentity);
23         vector org = casingowner.origin + casingowner.view_ofs + wep.spawnorigin.x * v_forward - wep.spawnorigin.y * v_right + wep.spawnorigin.z * v_up;
24
25         FOREACH_CLIENT(true, {
26                 if (!(CS(it).cvar_cl_casings))
27                         continue;
28
29                 msg_entity = it;
30                 if (!sound_allowed(MSG_ONE, it))
31                         casingtype |= 0x80; // silent
32
33                 WriteHeader(MSG_ONE, casings);
34                 WriteByte(MSG_ONE, casingtype);
35                 WriteVector(MSG_ONE, org);
36                 WriteShort(MSG_ONE, compressShortVector(vel)); // actually compressed velocity
37                 WriteByte(MSG_ONE, ang.x * 256 / 360);
38                 WriteByte(MSG_ONE, ang.y * 256 / 360);
39                 WriteByte(MSG_ONE, ang.z * 256 / 360);
40         });
41 }
42 #endif
43
44 #ifdef CSQC
45 entityclass(Casing);
46 classfield(Casing) .float alpha;
47 classfield(Casing) .bool silent;
48 classfield(Casing) .int state;
49 classfield(Casing) .float cnt;
50
51 void Casing_Delete(entity this)
52 {
53     delete(this);
54 }
55
56 void Casing_Draw(entity this)
57 {
58     if (IS_ONGROUND(this))
59     {
60         this.angles_x = 0;
61         this.angles_z = 0;
62         //UNSET_ONGROUND(this);
63     }
64
65     this.renderflags = 0;
66     this.alpha = bound(0, this.cnt - time, 1);
67
68     if (this.alpha < ALPHA_MIN_VISIBLE)
69     {
70         Casing_Delete(this);
71         this.drawmask = 0;
72         return;
73     }
74
75     Movetype_Physics_MatchTicrate(this, autocvar_cl_casings_ticrate, autocvar_cl_casings_sloppy);
76     //if (wasfreed(this))
77     //    return; // deleted by touch function
78 }
79
80 SOUND(BRASS1, W_Sound("brass1"));
81 SOUND(BRASS2, W_Sound("brass2"));
82 SOUND(BRASS3, W_Sound("brass3"));
83 Sound SND_BRASS_RANDOM() {
84     return Sounds_from(SND_BRASS1.m_id + floor(prandom() * 3));
85 }
86 SOUND(CASINGS1, W_Sound("casings1"));
87 SOUND(CASINGS2, W_Sound("casings2"));
88 SOUND(CASINGS3, W_Sound("casings3"));
89 Sound SND_CASINGS_RANDOM() {
90     return Sounds_from(SND_CASINGS1.m_id + floor(prandom() * 3));
91 }
92
93 void Casing_Touch(entity this, entity toucher)
94 {
95     if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
96     {
97         Casing_Delete(this);
98         return;
99     }
100
101     if (!this.silent)
102     if (!trace_ent || trace_ent.solid == SOLID_BSP)
103     {
104         if(vdist(this.velocity, >, 50))
105         {
106             if (time >= this.nextthink)
107             {
108                 Sound s;
109                 switch (this.state)
110                 {
111                     case 1:
112                         s = SND_CASINGS_RANDOM();
113                         break;
114                     default:
115                         s = SND_BRASS_RANDOM();
116                         break;
117                 }
118
119                 sound (this, CH_SHOTS, s, VOL_BASE, ATTEN_LARGE);
120             }
121         }
122     }
123
124     this.nextthink = time + 0.2;
125 }
126
127 void Casing_Damage(entity this, float thisdmg, int hittype, vector org, vector thisforce)
128 {
129     if (thisforce.z < 0)
130         thisforce.z = 0;
131     this.velocity = this.velocity + thisforce + '0 0 100';
132     UNSET_ONGROUND(this);
133 }
134
135 NET_HANDLE(casings, bool isNew)
136 {
137     int _state = ReadByte();
138     vector org = ReadVector();
139     vector vel = decompressShortVector(ReadShort());
140     vector ang;
141     ang_x = ReadByte() * 360 / 256;
142     ang_y = ReadByte() * 360 / 256;
143     ang_z = ReadByte() * 360 / 256;
144     return = true;
145
146     Casing casing = RubbleNew("casing");
147     casing.silent = (_state & 0x80);
148     casing.state = (_state & 0x7F);
149     casing.origin = org;
150     setorigin(casing, casing.origin);
151     casing.velocity = vel;
152     casing.angles = ang;
153     casing.drawmask = MASK_NORMAL;
154
155     casing.draw = Casing_Draw;
156     if (isNew) IL_PUSH(g_drawables, casing);
157     casing.velocity = casing.velocity + 2 * prandomvec();
158     casing.avelocity = '0 250 0' + 100 * prandomvec();
159     set_movetype(casing, MOVETYPE_BOUNCE);
160     casing.bouncefactor = 0.25;
161     settouch(casing, Casing_Touch);
162     casing.move_time = time;
163     casing.event_damage = Casing_Damage;
164     casing.solid = SOLID_TRIGGER;
165
166     switch (casing.state)
167     {
168         case 1:
169             setmodel(casing, MDL_CASING_SHELL);
170             casing.cnt = time + autocvar_cl_casings_shell_time;
171             break;
172         default:
173             setmodel(casing, MDL_CASING_BULLET);
174             casing.cnt = time + autocvar_cl_casings_bronze_time;
175             break;
176     }
177
178     setsize(casing, '0 0 -1', '0 0 -1');
179
180     RubbleLimit("casing", autocvar_cl_casings_maxcount, Casing_Delete);
181 }
182
183 #endif