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