]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/tuba.qc
Merge remote branch 'origin/master' into samual/hud_updates
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / tuba.qc
1 #define TUBA_MIN -18
2 #define TUBA_MAX  27
3 #define TUBA_INSTRUMENTS 1
4
5 #define TUBA_STARTNOTE(i,n) strcat("weapons/tuba", (i ? ftos(i) : ""), "_loopnote", ftos(n), ".wav")
6 .float note; // note
7 .float attenuate; // if set, attenuate it
8 .float cnt; // current volume
9 .float count; // initial volume
10 .float tuba_instrument;
11
12 float Tuba_PitchStep;
13
14 void tubasound(entity e, float restart)
15 {
16         string snd1;
17
18         snd1 = string_null;
19
20         if(Tuba_PitchStep)
21         {
22                 string snd2;
23                 float f1, f2;
24                 float p1, p2;
25                 float m;
26
27                 f1 = 1;
28                 p1 = 1;
29                 snd2 = string_null;
30                 f2 = 0;
31                 p2 = 1;
32
33                 m = mod(e.note, Tuba_PitchStep);
34                 if(m)
35                 {
36                         if(e.note - m < TUBA_MIN)
37                         {
38                                 if(restart)
39                                         snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m + Tuba_PitchStep);
40                                 p1 = pow(2.0, (m - Tuba_PitchStep) / 12.0);
41                         }
42                         else if(e.note - m + Tuba_PitchStep > TUBA_MAX)
43                         {
44                                 if(restart)
45                                         snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m);
46                                 p1 = pow(2.0, m / 12.0);
47                         }
48                         else
49                         {
50                                 if(restart)
51                                         snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m);
52                                 f1 = cos(M_PI_2 * m / Tuba_PitchStep);
53                                 p1 = pow(2.0, m / 12.0);
54                                 if(restart)
55                                         snd2 = TUBA_STARTNOTE(e.tuba_instrument, e.note - m + Tuba_PitchStep);
56                                 f2 = sin(M_PI_2 * m / Tuba_PitchStep);
57                                 p2 = pow(2.0, (m - Tuba_PitchStep) / 12.0);
58                         }
59                 }
60                 else
61                 {
62                         if(restart)
63                                 snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note);
64                 }
65
66                 sound7(e, CH_TUBA, snd1, e.cnt * f1, e.attenuate * autocvar_g_balance_tuba_attenuation, 100 * p1, 0);
67                 if(f2)
68                         sound7(e.enemy, CH_TUBA, snd2, e.cnt * f2, e.attenuate * autocvar_g_balance_tuba_attenuation, 100 * p2, 0);
69         }
70         else
71         {
72                 if(restart)
73                         snd1 = TUBA_STARTNOTE(e.tuba_instrument, e.note);
74                 sound(e, CH_TUBA, snd1, e.cnt, e.attenuate * autocvar_g_balance_tuba_attenuation);
75         }
76 }
77
78 void Ent_TubaNote_Think()
79 {
80         float f;
81         f = autocvar_g_balance_tuba_fadetime;
82         if(f > 0)
83                 self.cnt -= frametime * self.count / f;
84         else
85                 self.cnt = 0;
86         self.nextthink = time;
87         if(self.cnt <= 0)
88         {
89                 sound(self, CH_TUBA, "misc/null.wav", 0, 0);
90                 if(self.enemy)
91                 {
92                         sound(self.enemy, CH_TUBA, "misc/null.wav", 0, 0);
93                         remove(self.enemy);
94                 }
95                 remove(self);
96         }
97         else
98         {
99                 tubasound(self, 0);
100         }
101 }
102
103 void Ent_TubaNote_UpdateSound()
104 {
105         self.enemy.cnt = bound(0, VOL_BASE * autocvar_g_balance_tuba_volume, 1);
106         self.enemy.count = self.enemy.cnt;
107         self.enemy.note = self.note;
108         self.enemy.tuba_instrument = self.tuba_instrument;
109         tubasound(self.enemy, 1);
110 }
111
112 void Ent_TubaNote_StopSound()
113 {
114         self.enemy.nextthink = time;
115         self.enemy = world;
116 }
117
118 void Ent_TubaNote(float bIsNew)
119 {
120         float f, n, i, att;
121         f = ReadByte();
122
123         if(f & 1)
124         {
125                 n = ReadChar();
126                 i = ReadByte();
127                 att = (i & 1);
128                 i = floor(i / 2);
129
130                 if(n != self.note || i != self.tuba_instrument || bIsNew)
131                 {
132                         if(self.enemy)
133                                 Ent_TubaNote_StopSound();
134                 }
135
136                 if(!self.enemy)
137                 {
138                         self.enemy = spawn();
139                         self.enemy.classname = "tuba_note";
140                         if(Tuba_PitchStep)
141                         {
142                                 self.enemy.enemy = spawn();
143                                 self.enemy.enemy.classname = "tuba_note_2";
144                         }
145                         bIsNew = TRUE;
146                 }
147
148                 self.enemy.attenuate = att;
149
150                 if(bIsNew)
151                 {
152                         self.note = n;
153                         self.tuba_instrument = i;
154                         Ent_TubaNote_UpdateSound();
155                 }
156         }
157
158         if(f & 2)
159         {
160                 self.enemy.origin_x = ReadCoord();
161                 self.enemy.origin_y = ReadCoord();
162                 self.enemy.origin_z = ReadCoord();
163                 setorigin(self.enemy, self.enemy.origin);
164                 if(self.enemy.enemy)
165                         setorigin(self.enemy.enemy, self.enemy.origin);
166         }
167
168         self.think = Ent_TubaNote_StopSound;
169         self.entremove = Ent_TubaNote_StopSound;
170         self.enemy.think = Ent_TubaNote_Think;
171         self.enemy.nextthink = time + 10;
172 }
173
174 void Tuba_Precache()
175 {
176         float i, n;
177         Tuba_PitchStep = autocvar_g_balance_tuba_pitchstep;
178         if(Tuba_PitchStep)
179         {
180                 if(!checkextension("DP_SND_SOUND7_WIP2") && !checkextension("DP_SND_SOUND7"))
181                 {
182                         print("^1NOTE:^7 requested pitch shifting, but not supported by this engine build\n");
183                         Tuba_PitchStep = 0;
184                 }
185         }
186         for(n = TUBA_MIN; n <= TUBA_MAX; ++n)
187         {
188                 if(!Tuba_PitchStep || (mod(n, Tuba_PitchStep) == 0))
189                 {
190                         for(i = 0; i < TUBA_INSTRUMENTS; ++i)
191                                 precache_sound(TUBA_STARTNOTE(i, n));
192                 }
193         }
194 }