Fixes for datatype size on amd64.
[crack-attack.git] / src / SparkleManager.cxx
blob92c5916b7927e9f2fbfd627de4ecfcd1730360a1
1 /*
2 * SparkleManager.cxx
3 * Daniel Nelson - 9/4/0
5 * Copyright (C) 2000 Daniel Nelson
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 * Daniel Nelson - aluminumangel.org
22 * 174 W. 18th Ave.
23 * Columbus, OH 43210
25 * Handles all the pretty sparkles!
28 #include "SparkleManager.h"
30 #include "Game.h"
31 #include "Displayer.h"
32 #include "MetaState.h"
33 #include "Random.h"
35 int SparkleManager::spark_count;
36 Spark SparkleManager::sparks[DC_MAX_SPARK_NUMBER];
38 int SparkleManager::mote_count;
39 Mote SparkleManager::motes[DC_MAX_MOTE_NUMBER];
41 const int SparkleManager::mote_colors[DC_NUMBER_MOTE_LEVELS]
42 = { 0, 0, 0, 4, 5, 6, 7, 8, 9, 10, 11,
43 0, 0, 0, 1, 2, 3, 3, 3, 3, 3, 3 };
45 const int SparkleManager::mote_light_colors[DC_NUMBER_MOTE_LEVELS]
46 = { 0, 0, 0, 0, 1, 0, 2, 3, 4, 5, 6,
47 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
49 const int SparkleManager::mote_types[DC_NUMBER_MOTE_LEVELS]
50 = { MT_FOUR_POINTED_STAR, MT_FIVE_POINTED_STAR,
51 MT_SIX_POINTED_STAR, MT_SPECIAL_STAR, MT_SPECIAL_STAR, MT_SPECIAL_STAR,
52 MT_SPECIAL_STAR, MT_SPECIAL_STAR, MT_SPECIAL_STAR, MT_SPECIAL_STAR,
53 MT_SPECIAL_STAR, MT_MULTIPLIER_ONE_STAR, MT_MULTIPLIER_TWO_STAR,
54 MT_MULTIPLIER_THREE_STAR, MT_MULTIPLIER_THREE_STAR,
55 MT_MULTIPLIER_THREE_STAR, MT_MULTIPLIER_THREE_STAR,
56 MT_MULTIPLIER_THREE_STAR, MT_MULTIPLIER_THREE_STAR,
57 MT_MULTIPLIER_THREE_STAR, MT_MULTIPLIER_THREE_STAR,
58 MT_MULTIPLIER_THREE_STAR };
60 const GLfloat SparkleManager::mote_sizes[DC_NUMBER_MOTE_LEVELS]
61 = { 2.0f, 2.8f, 2.8f, 3.4f, 3.4f, 3.4f, 3.4f, 3.4f, 3.4f, 3.4f, 3.4f,
62 4.0f, 2.6f, 3.5f, 3.7f, 3.9f, 4.1f, 4.3f, 4.5f, 4.7f, 4.9f, 5.1f };
64 const float SparkleManager::mote_inverse_masses[DC_NUMBER_MOTE_LEVELS]
65 = { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
66 1.0f, 1.0f, 1.0f, 1.0f, 1.0f / 1.4f, 1.0f / 1.8f, 1.0f / 2.2f,
67 1.0f / 2.6f, 1.0f / 3.0f, 1.0f / 3.4f, 1.0f / 3.8f, 1.0f / 4.2f };
69 void SparkleManager::initialize ( )
71 spark_count = 0;
72 for (int n = DC_MAX_SPARK_NUMBER; n--; )
73 sparks[n].active = false;
75 mote_count = 0;
76 for (int n = DC_MAX_MOTE_NUMBER; n--; )
77 motes[n].active = false;
80 void SparkleManager::createBlockDeathSpark ( int x, int y, int color, int n )
82 while (n--) {
83 if (spark_count == DC_MAX_SPARK_NUMBER) return;
85 int n;
86 for (n = 0; sparks[n].active; n++);
87 Spark &spark = sparks[n];
89 spark_count++;
90 spark.active = true;
91 spark.x = x * DC_GRID_ELEMENT_LENGTH + DC_PLAY_OFFSET_X;
92 spark.y = y * DC_GRID_ELEMENT_LENGTH + Displayer::play_offset_y;
94 float v = DC_MIN_SPARK_VELOCITY + (Random::number() + Random::number())
95 * (0.5f * (DC_MAX_SPARK_VELOCITY - DC_MIN_SPARK_VELOCITY));
96 Random::deathSparkAngle(spark.v_x, spark.v_y);
97 spark.v_x *= v;
98 spark.v_y *= v;
100 spark.a = Random::number(360);
101 spark.v_a = DC_MIN_SPARK_ANGULAR_VELOCITY
102 + (Random::number() + Random::number())
103 * (0.5f * (DC_MAX_SPARK_ANGULAR_VELOCITY - DC_MIN_SPARK_ANGULAR_VELOCITY));
104 if (Random::chanceIn2(2))
105 spark.v_a = -spark.v_a;
107 switch (Random::number2(4)) {
108 case 0:
109 spark.size = DC_MIN_SPARK_SIZE;
110 break;
111 case 1:
112 spark.size = DC_MIN_SPARK_SIZE + Random::number()
113 * (1.0f - DC_MIN_SPARK_SIZE);
114 break;
115 default:
116 spark.size = 1.0f;
117 break;
120 spark.color = color;
122 if (Random::chanceIn(DC_CHANCE_LONG_SPARK_LIFE_TIME))
123 spark.life_time = Random::number(10 * DC_SPREAD_SPARK_LIFE_TIME)
124 + Random::number(10 * DC_SPREAD_SPARK_LIFE_TIME)
125 + 10 * (DC_MEDIUM_SPARK_LIFE_TIME - DC_SPREAD_SPARK_LIFE_TIME);
126 else
127 spark.life_time = Random::number(DC_SPREAD_SPARK_LIFE_TIME)
128 + Random::number(DC_SPREAD_SPARK_LIFE_TIME)
129 + (DC_MEDIUM_SPARK_LIFE_TIME - DC_SPREAD_SPARK_LIFE_TIME);
133 void SparkleManager::createCelebrationSpark ( int source, int color )
135 if (spark_count == DC_MAX_SPARK_NUMBER) return;
137 int n;
138 for (n = 0; sparks[n].active; n++);
139 Spark &spark = sparks[n];
141 spark_count++;
142 spark.active = true;
144 float v;
145 if (source != 4) {
146 Random::celebrationSpark1Angle(spark.v_x, spark.v_y);
147 v = DC_MIN_CSPARK_VELOCITY + DC_SPREAD_CSPARK_VELOCITY * Random::number();
148 } else {
149 Random::celebrationSpark2Angle(spark.v_x, spark.v_y);
150 v = (2.0f * DC_MIN_CSPARK_VELOCITY)
151 + (2.0f * DC_SPREAD_CSPARK_VELOCITY) * Random::number();
153 spark.v_x *= v;
154 spark.v_y *= v;
156 switch (source) {
157 case 0:
158 if (MetaState::mode & CM_SOLO) {
159 spark.x = DC_PLAY_OFFSET_X + DC_SOURCE_1_SOLO_OFFSET_X;
160 spark.y = DC_PLAY_OFFSET_Y + DC_SOURCE_1_SOLO_OFFSET_Y;
161 } else {
162 spark.x = DC_PLAY_OFFSET_X + DC_SOURCE_1_OFFSET_X;
163 spark.y = DC_PLAY_OFFSET_Y + DC_SOURCE_1_OFFSET_Y;
165 break;
166 case 1:
167 spark.x = DC_PLAY_OFFSET_X + DC_SOURCE_2_OFFSET_X;
168 spark.y = DC_PLAY_OFFSET_Y + DC_SOURCE_2_OFFSET_Y;
169 break;
170 case 2:
171 if (MetaState::mode & CM_SOLO) {
172 spark.x = DC_PLAY_OFFSET_X + DC_SOURCE_3_SOLO_OFFSET_X;
173 spark.y = DC_PLAY_OFFSET_Y + DC_SOURCE_3_SOLO_OFFSET_Y;
174 } else {
175 spark.x = DC_PLAY_OFFSET_X + DC_SOURCE_3_OFFSET_X;
176 spark.y = DC_PLAY_OFFSET_Y + DC_SOURCE_3_OFFSET_Y;
178 spark.v_x = -spark.v_x;
179 break;
180 case 3:
181 spark.x = DC_PLAY_OFFSET_X + DC_SOURCE_4_OFFSET_X;
182 spark.y = DC_PLAY_OFFSET_Y + DC_SOURCE_4_OFFSET_Y;
183 spark.v_x = -spark.v_x;
184 break;
185 case 4:
186 if (MetaState::mode & CM_SOLO) {
187 spark.x = DC_PLAY_OFFSET_X + DC_SOURCE_5_SOLO_OFFSET_X;
188 spark.y = DC_PLAY_OFFSET_Y + DC_SOURCE_5_SOLO_OFFSET_Y;
189 } else {
190 spark.x = DC_PLAY_OFFSET_X + DC_SOURCE_5_OFFSET_X;
191 spark.y = DC_PLAY_OFFSET_Y + DC_SOURCE_5_OFFSET_Y;
193 break;
196 spark.a = Random::number(360);
197 spark.v_a = DC_MIN_SPARK_ANGULAR_VELOCITY
198 + (Random::number() + Random::number())
199 * (0.5f * (DC_MAX_SPARK_ANGULAR_VELOCITY - DC_MIN_SPARK_ANGULAR_VELOCITY));
200 if (Random::chanceIn2(2))
201 spark.v_a = -spark.v_a;
203 if (Random::chanceIn(3))
204 spark.size = DC_MIN_SPARK_SIZE
205 + Random::number() * (1.0f - DC_MIN_SPARK_SIZE);
206 else
207 spark.size = DC_MIN_SPARK_SIZE;
209 spark.color = color;
211 if (Random::chanceIn(DC_CHANCE_LONG_SPARK_LIFE_TIME))
212 spark.life_time = Random::number(10 * DC_SPREAD_SPARK_LIFE_TIME)
213 + Random::number(10 * DC_SPREAD_SPARK_LIFE_TIME)
214 + 10 * (DC_MEDIUM_SPARK_LIFE_TIME - DC_SPREAD_SPARK_LIFE_TIME);
215 else
216 spark.life_time = Random::number(DC_SPREAD_SPARK_LIFE_TIME)
217 + Random::number(DC_SPREAD_SPARK_LIFE_TIME)
218 + (DC_MEDIUM_SPARK_LIFE_TIME - DC_SPREAD_SPARK_LIFE_TIME);
221 void SparkleManager::createRewardMote ( int x, int y, int level,
222 int sibling_number )
224 if (mote_count == DC_MAX_MOTE_NUMBER) return;
226 int n;
227 for (n = 0; motes[n].active; n++);
228 Mote &mote = motes[n];
230 if (n == 0)
231 glEnable(GL_LIGHT1);
233 mote_count++;
234 mote.active = true;
236 if (level >= DC_NUMBER_MOTE_LEVELS)
237 level = DC_NUMBER_MOTE_LEVELS - 1;
238 mote.color = mote_colors[level];
239 mote.light_color = mote_light_colors[level];
240 mote.type = mote_types[level];
241 mote.size = mote_sizes[level];
242 mote.inverse_mass = mote_inverse_masses[level];
244 mote.x = x * DC_GRID_ELEMENT_LENGTH + DC_PLAY_OFFSET_X
245 - (DC_GRID_ELEMENT_LENGTH / 2.0f)
246 + (float) Random::number(20) * ((float) DC_GRID_ELEMENT_LENGTH / 20.0f);
247 mote.y = y * DC_GRID_ELEMENT_LENGTH + Displayer::play_offset_y
248 - (DC_GRID_ELEMENT_LENGTH / 2.0f)
249 + (float) Random::number(20) * ((float) DC_GRID_ELEMENT_LENGTH / 20.0f);
251 // slow down big ones
252 float v = (DC_MEDIUM_MOTE_VELOCITY - DC_SPREAD_MOTE_VELOCITY
253 + Random::number() * (2.0f * DC_SPREAD_MOTE_VELOCITY)) * mote.inverse_mass;
254 if (x < GC_PLAY_WIDTH / 2)
255 mote.v_x = -0.707107 * v;
256 else
257 mote.v_x = 0.707107 * v;
258 mote.v_y = -0.707107 * v;
260 mote.a = mote.initial_a = Random::number(360);
261 mote.v_a = (DC_MEDIUM_MOTE_ANGULAR_VELOCITY
262 - DC_SPREAD_MOTE_ANGULAR_VELOCITY + Random::number()
263 * (2.0f * DC_SPREAD_MOTE_ANGULAR_VELOCITY)) * mote.inverse_mass;
264 if (Random::chanceIn2(2))
265 mote.v_a = -mote.v_a;
267 mote.life_time = 0;
268 mote.sibling_delay = sibling_number * DC_MULTI_MOTE_FIRE_DELAY;
270 mote.associated_light = -1;
273 void SparkleManager::timeStep ( )
275 int c = spark_count;
276 for (int n = 0; c; n++)
277 if (sparks[n].active) {
278 Spark &spark = sparks[n];
279 c--;
281 if (--spark.life_time == 0) {
282 spark.active = false;
283 spark_count--;
285 } else {
286 spark.x += spark.v_x;
287 spark.y += spark.v_y;
289 spark.a += spark.v_a;
291 spark.v_y -= DC_SPARK_GRAVITY + DC_SPARK_DRAG * spark.v_y;
292 spark.v_x -= DC_SPARK_DRAG * spark.v_x;
296 c = mote_count;
297 for (int n = 0; c; n++)
298 if (motes[n].active) {
299 Mote &mote = motes[n];
300 c--;
302 if (mote.life_time >= 0)
303 if (++mote.life_time - mote.sibling_delay < GC_DYING_DELAY) {
304 mote.a += mote.v_a;
305 if (mote.life_time <= GC_DYING_DELAY)
306 mote.brightness = DC_MAX_MOTE_LIGHT_BRIGHTNESS
307 * mote.life_time * (1.0f / (float) GC_DYING_DELAY);
308 continue;
309 } else
310 mote.life_time = -1;
311 else if (mote.color > 0 && mote.color < DC_FIRST_SPECIAL_MOTE_COLOR)
312 mote.life_time--;
314 mote.y += mote.v_y;
315 if (mote.y > DC_PLAY_HEIGHT + mote.size * (DC_SPARKLE_LENGTH / 2.0f)) {
316 mote.active = false;
317 mote_count--;
318 continue;
321 mote.x += mote.v_x;
323 mote.a += mote.v_a;
325 mote.v_y += mote.inverse_mass * DC_MOTE_UPWARD_FORCE
326 - DC_MOTE_DRAG * mote.v_y;
327 mote.v_x -= mote.inverse_mass * DC_MOTE_CENTER_SPRING * mote.x
328 + DC_MOTE_DRAG * mote.v_x;
330 mote.v_a -= mote.inverse_mass
331 * DC_MOTE_TWIST_SPRING * (mote.a - mote.initial_a);