]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/tuba.qc
Declare more ints as ints
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / tuba.qc
1 const float TUBA_MIN = -18;
2 const float TUBA_MAX =  27;
3 const float TUBA_INSTRUMENTS = 3;
4
5 #define TUBA_STARTNOTE(i,n) strcat("weapons/tuba", (i ? ftos(i) : ""), "_loopnote", ftos(n), ".wav")
6 .int 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 int 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 = 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_SINGLE, snd1, e.cnt * f1, e.attenuate * autocvar_g_balance_tuba_attenuation, 100 * p1, 0);
67                 if(f2)
68                         sound7(e.enemy, CH_TUBA_SINGLE, 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_SINGLE, 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_SINGLE, "misc/null.wav", 0, 0);
90                 if(self.enemy)
91                 {
92                         sound(self.enemy, CH_TUBA_SINGLE, "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     int f, n, i;
121         float att, upd;
122         f = ReadByte();
123
124         upd = 0;
125
126         if(f & 1)
127         {
128                 n = ReadChar();
129                 i = ReadByte();
130                 att = (i & 1);
131                 i = floor(i / 2);
132
133                 if(n != self.note || i != self.tuba_instrument || bIsNew)
134                 {
135                         if(self.enemy)
136                                 Ent_TubaNote_StopSound();
137                 }
138
139                 if(!self.enemy)
140                 {
141                         self.enemy = spawn();
142                         self.enemy.classname = "tuba_note";
143                         if(Tuba_PitchStep)
144                         {
145                                 self.enemy.enemy = spawn();
146                                 self.enemy.enemy.classname = "tuba_note_2";
147                         }
148                         bIsNew = true;
149                 }
150
151                 self.enemy.attenuate = att;
152
153                 if(bIsNew)
154                 {
155                         self.note = n;
156                         self.tuba_instrument = i;
157                         upd = 1;
158                 }
159         }
160
161         if(f & 2)
162         {
163                 self.enemy.origin_x = ReadCoord();
164                 self.enemy.origin_y = ReadCoord();
165                 self.enemy.origin_z = ReadCoord();
166                 setorigin(self.enemy, self.enemy.origin);
167                 if(self.enemy.enemy)
168                         setorigin(self.enemy.enemy, self.enemy.origin);
169         }
170
171         self.think = Ent_TubaNote_StopSound;
172         self.entremove = Ent_TubaNote_StopSound;
173         self.enemy.think = Ent_TubaNote_Think;
174         self.enemy.nextthink = time + 10;
175
176         if(upd)
177                 Ent_TubaNote_UpdateSound();
178 }
179
180 void Tuba_Precache()
181 {
182         float i;
183     int n;
184         Tuba_PitchStep = autocvar_g_balance_tuba_pitchstep;
185         if(Tuba_PitchStep)
186         {
187                 if(!checkextension("DP_SND_SOUND7_WIP2") && !checkextension("DP_SND_SOUND7"))
188                 {
189                         print("^1NOTE:^7 requested pitch shifting, but not supported by this engine build\n");
190                         Tuba_PitchStep = 0;
191                 }
192         }
193         for(n = TUBA_MIN; n <= TUBA_MAX; ++n)
194         {
195                 if(!Tuba_PitchStep || ((n % Tuba_PitchStep) == 0))
196                 {
197                         for(i = 0; i < TUBA_INSTRUMENTS; ++i)
198                                 precache_sound(TUBA_STARTNOTE(i, n));
199                 }
200         }
201 }