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