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