Merge remote branch 'refs/remotes/origin/fruitiex/racefixes'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / w_tuba.qc
1 #ifdef REGISTER_WEAPON
2 REGISTER_WEAPON(TUBA, w_tuba, 0, 1, WEP_FLAG_HIDDEN | WEP_TYPE_SPLASH, BOT_PICKUP_RATING_MID, "tuba", "tuba", "@!#%'n Tuba");
3 #else
4 #ifdef SVQC
5 //#define TUBA_NOTE(n) strcat("weapons/tuba_note", ftos(n), ".wav")
6 .float tuba_notecount;
7 .entity tuba_note;
8 .float tuba_smoketime;
9
10 float Tuba_GetNote(entity pl, float hittype)
11 {
12         float note;
13         float movestate;
14         movestate = 5;
15         if(pl.movement_x < 0) movestate -= 3;
16         if(pl.movement_x > 0) movestate += 3;
17         if(pl.movement_y < 0) movestate -= 1;
18         if(pl.movement_y > 0) movestate += 1;
19         switch(movestate)
20         {
21         // layout: originally I wanted
22         //   eb e  e#=f
23         //   B  c  d
24         //   Gb G  G#
25         // but then you only use forward and right key. So to make things more
26         // interesting, I swapped B with e#. Har har har...
27         //   eb e  B
28         // f=e# c  d
29         //   Gb G  G#
30                 case 1: note = -6; break; // Gb
31                 case 2: note = -5; break; // G
32                 case 3: note = -4; break; // G#
33                 case 4: note = +5; break; // e#
34                 case 5: note =  0; break; // c
35                 case 6: note = +2; break; // d
36                 case 7: note = +3; break; // eb
37                 case 8: note = +4; break; // e
38                 case 9: note = -1; break; // B
39         }
40         if(pl.BUTTON_CROUCH)
41                 note -= 12;
42         if(pl.BUTTON_JUMP)
43                 note += 12;
44         if(hittype & HITTYPE_SECONDARY)
45                 note += 7;
46         
47         // we support two kinds of tubas, those tuned in Eb and those tuned in C
48         // kind of tuba currently is player slot number, or team number if in
49         // teamplay
50         // that way, holes in the range of notes are "plugged"
51         if(teams_matter)
52         {
53                 if(pl.team == COLOR_TEAM2 || pl.team == COLOR_TEAM4)
54                         note += 3;
55         }
56         else
57         {
58                 if(pl.clientcolors & 1)
59                         note += 3;
60         }
61         
62         // total range of notes:
63         //                       0
64         //                 ***  ** ****
65         //                        ***  ** ****
66         //     ***  ** ****
67         //            ***  ** ****
68         //     ***  ********************* ****
69         //     -18.........................+12
70         //        ***  ********************* ****
71         //     -18............................+15
72         //     with jump: ... +24
73         //     ... +27
74         return note;
75 }
76
77 float W_Tuba_NoteSendEntity(entity to, float sf)
78 {
79         WriteByte(MSG_ENTITY, ENT_CLIENT_TUBANOTE);
80         WriteByte(MSG_ENTITY, (sf & 1) | ((self.cnt + 42) * 2));
81         if(sf & 1)
82         {
83                 WriteCoord(MSG_ENTITY, self.origin_x);
84                 WriteCoord(MSG_ENTITY, self.origin_y);
85                 WriteCoord(MSG_ENTITY, self.origin_z);
86                 WriteByte(MSG_ENTITY, self.owner != to);
87         }
88         return TRUE;
89 }
90
91 void W_Tuba_NoteThink()
92 {
93         float dist_mult;
94         float vol0, vol1;
95         vector dir0, dir1;
96         vector v;
97         entity e;
98         if(time > self.teleport_time)
99         {
100                 self.owner.tuba_note = world;
101                 remove(self);
102                 return;
103         }
104         self.nextthink = time;
105         dist_mult = cvar("g_balance_tuba_attenuation") / cvar("snd_soundradius");
106         FOR_EACH_REALCLIENT(e)
107         if(e != self.owner)
108         {
109                 v = self.origin - (e.origin + e.view_ofs);
110                 vol0 = max(0, 1 - vlen(v) * dist_mult);
111                 dir0 = normalize(v);
112                 v = self.owner.origin - (e.origin + e.view_ofs);
113                 vol1 = max(0, 1 - vlen(v) * dist_mult);
114                 dir1 = normalize(v);
115                 if(fabs(vol0 - vol1) > 0.005) // 0.5 percent change in volume
116                 {
117                         setorigin(self, self.owner.origin);
118                         self.SendFlags |= 1;
119                         break;
120                 }
121                 if(dir0 * dir1 < 0.9994) // 2 degrees change in angle
122                 {
123                         setorigin(self, self.owner.origin);
124                         self.SendFlags |= 1;
125                         break;
126                 }
127         }
128 }
129
130 void W_Tuba_Attack(float hittype)
131 {
132         vector o;
133         float c, n;
134         W_SetupShot(self, FALSE, 2, "", cvar("g_balance_tuba_damage"));
135         if(self.tuba_notecount)
136         {
137                 self.tuba_notecount = FALSE;
138                 c = CHAN_WEAPON;
139         }
140         else
141         {
142                 self.tuba_notecount = TRUE;
143                 c = CHAN_WEAPON2;
144         }
145
146         n = Tuba_GetNote(self, hittype);
147
148         if(self.tuba_note)
149         {
150                 if(self.tuba_note.cnt != n)
151                 {
152                         /*
153                         self.tuba_note.cnt = n;
154                         self.tuba_note.SendFlags |= 2;
155                         */
156                         remove(self.tuba_note);
157                         self.tuba_note = world;
158                 }
159         }
160
161         if not(self.tuba_note)
162         {
163                 self.tuba_note = spawn();
164                 self.tuba_note.owner = self;
165                 self.tuba_note.cnt = n;
166                 self.tuba_note.think = W_Tuba_NoteThink;
167                 self.tuba_note.nextthink = time;
168                 Net_LinkEntity(self.tuba_note, FALSE, 0, W_Tuba_NoteSendEntity);
169         }
170
171         self.tuba_note.teleport_time = time + cvar("g_balance_tuba_refire") * 2; // so it can get prolonged safely
172
173         //sound(self, c, TUBA_NOTE(n), bound(0, VOL_BASE * cvar("g_balance_tuba_volume"), 1), cvar("g_balance_tuba_attenuation"));
174         RadiusDamage(self, self, cvar("g_balance_tuba_damage"), cvar("g_balance_tuba_edgedamage"), cvar("g_balance_tuba_radius"), world, cvar("g_balance_tuba_force"), hittype | WEP_TUBA, world);
175
176         o = gettaginfo(self.exteriorweaponentity, 0);
177         if(time > self.tuba_smoketime)
178         {
179                 pointparticles(particleeffectnum("smoke_ring"), o + v_up * 45 + v_right * -6 + v_forward * 8, v_up * 100, 1);
180                 self.tuba_smoketime = time + 0.25;
181         }
182 }
183
184 void spawnfunc_weapon_tuba (void)
185 {
186         weapon_defaultspawnfunc(WEP_TUBA);
187 }
188
189 float w_tuba(float req)
190 {
191         if (req == WR_AIM)
192         {
193                 // bots cannot play the Tuba well yet
194                 // I think they should start with the recorder first
195                 if(vlen(self.origin - self.enemy.origin) < cvar("g_balance_tuba_radius"))
196                 {
197                         if(random() > 0.5)
198                                 self.BUTTON_ATCK = 1;
199                         else
200                                 self.BUTTON_ATCK2 = 1;
201                 }
202         }
203         else if (req == WR_THINK)
204         {
205                 if (self.BUTTON_ATCK)
206                 if (weapon_prepareattack(0, cvar("g_balance_tuba_refire")))
207                 {
208                         W_Tuba_Attack(0);
209                         //weapon_thinkf(WFRAME_FIRE1, cvar("g_balance_tuba_animtime"), w_ready);
210                         weapon_thinkf(WFRAME_IDLE, cvar("g_balance_tuba_animtime"), w_ready);
211                 }
212                 if (self.BUTTON_ATCK2)
213                 if (weapon_prepareattack(1, cvar("g_balance_tuba_refire")))
214                 {
215                         W_Tuba_Attack(HITTYPE_SECONDARY);
216                         //weapon_thinkf(WFRAME_FIRE2, cvar("g_balance_tuba_animtime"), w_ready);
217                         weapon_thinkf(WFRAME_IDLE, cvar("g_balance_tuba_animtime"), w_ready);
218                 }
219                 if(self.tuba_note)
220                 {
221                         if(!self.BUTTON_ATCK && !self.BUTTON_ATCK2)
222                         {
223                                 remove(self.tuba_note);
224                                 self.tuba_note = world;
225                         }
226                 }
227         }
228         else if (req == WR_PRECACHE)
229         {
230                 precache_model ("models/weapons/g_tuba.md3");
231                 precache_model ("models/weapons/v_tuba.md3");
232                 precache_model ("models/weapons/h_tuba.iqm");
233
234                 //float i;
235                 //for(i = -18; i <= +27; ++i)
236                 //      precache_sound(TUBA_NOTE(i));
237         }
238         else if (req == WR_SETUP)
239                 weapon_setup(WEP_TUBA);
240         else if (req == WR_CHECKAMMO1)
241                 return TRUE; // TODO use fuel?
242         else if (req == WR_CHECKAMMO2)
243                 return TRUE; // TODO use fuel?
244         return TRUE;
245 };
246 #endif
247 #ifdef CSQC
248 float w_tuba(float req)
249 {
250         if(req == WR_IMPACTEFFECT)
251         {
252                 // nothing to do here; particles of tuba are handled differently
253         }
254         else if(req == WR_PRECACHE)
255         {
256                 // nothing to do
257         }
258         else if (req == WR_SUICIDEMESSAGE)
259         {
260                 w_deathtypestring = "%s hurt his own ears with the @!#%%'n Tuba";
261         }
262         else if (req == WR_KILLMESSAGE)
263         {
264                 w_deathtypestring = "%s died of %s's great playing on the @!#%%'n Tuba";
265         }
266         return TRUE;
267 }
268 #endif
269 #endif