Debian Codegraph @

apps/demos/drop/TsTrickle.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 /*
38 _______________________________________________________________________
39 ______________ S I L I C O N G R A P H I C S I N C . ____________
40 |
41 | $Revision: 1.1.1.1 $
42 |
43 | Classes : TsField::tricklePiece
44 | TsField::stopPiece
45 | TsField::collision
46 |
47 | Author : Dave Immel
48 |
49 ______________ S I L I C O N G R A P H I C S I N C . ____________
50 _______________________________________________________________________
51 */
52
53 //--------------------------- Include ----------------------------------
54 #include <cstdio>
55 #include <Inventor/SbLinear.h>
56 #include <Inventor/nodes/SoMaterial.h>
57 #include <Inventor/nodes/SoSeparator.h>
58 #include <Inventor/nodes/SoTranslation.h>
59 #include <Inventor/sensors/SoAlarmSensor.h>
60 #include <Inventor/sensors/SoTimerSensor.h>
61 #include "TsPieces.h"
62 #include "TsField.h"
63
64 #define REMOVAL_HIGHLIGHT_DURATION 1.0
65
66
67 ////////////////////////////////////////////////////////////////////////
68 //
69 // Description:
70 // This routine translates the current piece down in Y by a small
71 // amount. The amount is determined by the current level of play.
72 // If the piece crosses an integer boundary, a collision test must
73 // be made. If a collision has occurred, the piece must stop at
74 // the integer position, a check must be made to see if a whole
75 // row may be removed, and a new piece is started.
76 //
77 // Use: public
78
79 void
80 TsField::tricklePiece( void *data, SoSensor * )
81
82 //
83 ////////////////////////////////////////////////////////////////////////
84 {
85 TsField *field = (TsField *)data;
86 SbTime elapsedTime;
87 float tmpFraction, shiftDistance;
88 time_t clockSeconds;
89 long clockMSeconds; // System long
90
91 //
92 // Calculate the distance the piece should fall. This is calculated
93 // given the change in time and the rate at which the piece is falling.
94 // The distance is expressed as a value from 2.0 to 0.0. This represents
95 // the intermediate distance from one integer position to the next.
96 //
97 elapsedTime.setToTimeOfDay();
98 elapsedTime -= field->wallClock;
99 field->wallClock.setToTimeOfDay();
100 elapsedTime.getValue (clockSeconds, clockMSeconds);
101 shiftDistance = (clockSeconds + clockMSeconds * 1e-6) * field->currentRate;
102 tmpFraction = field->currentFraction - shiftDistance;
103
104 if (tmpFraction >= 0.0)
105 {
106 //
107 // Just translate down.
108 //
109 field->currentFraction = tmpFraction;
110 }
111 else while (tmpFraction < 0.0)
112 {
113 int newPosition[3], localPos[4], fieldPos[4];
114
115 //
116 // The piece has crossed an integer boundary.
117 // Temporarily shift the standard cube position down one level and
118 // get the positions of the piece blocks at this new position.
119 //
120 newPosition[TS_X_AXIS] = field->standardPosition[TS_X_AXIS];
121 newPosition[TS_Y_AXIS] = field->standardPosition[TS_Y_AXIS] - 1;
122 newPosition[TS_Z_AXIS] = field->standardPosition[TS_Z_AXIS];
123 (void)field->currentPiece->getPosition (localPos);
124 field->getFieldPositions (field->numPieceBlocks, localPos, fieldPos,
125 newPosition);
126
127 //
128 // Check this new position for a collision with blocks in the field
129 // table and the floor. If there is a collision, stop the current
130 // piece.
131 //
132 if (field->collision (field->numPieceBlocks, fieldPos))
133 {
134 field->trickleSensor->unschedule();
135 field->stopPiece();
136 return;
137 }
138
139 // No collision. Finalize the standard cube position.
140 --field->standardPosition[TS_Y_AXIS];
141
142 tmpFraction += 2.0;
143 field->currentFraction = tmpFraction;
144 }
145
146 // Update the PieceYTranslation node to move the piece downwards.
147 const SbVec3f &tpos = field->pieceYTranslation->translation.getValue ();
148 SbVec3f spos;
149 spos[0] = tpos[0];
150 spos[1] = tpos[1] - shiftDistance;
151 spos[2] = tpos[2];
152 field->pieceYTranslation->translation.setValue (spos);
153 }
154
155
156
157
158 ////////////////////////////////////////////////////////////////////////
159 //
160 // Description:
161 // This routine places blocks representing the current piece in
162 // with the other static blocks on the field and fills in the
163 // correct indices in the field table corresponding to the positions
164 // of the blocks of the piece.
165 //
166 // The routine also checks the levels in which the current piece came
167 // to rest and changes those rows to be gold. A sensor is scheduled
168 // to elimate the rows.
169 //
170 // The routine ends the game if the current piece lands at the top of
171 // the field.
172 //
173 // This routine gives the time the piece spent falling and the
174 // number of rows removed to the scoreboard.
175 //
176 // If no rows have been removed, a new piece is put into play.
177
178 // Use: private
179
180 void
181 TsField::stopPiece()
182
183 //
184 ////////////////////////////////////////////////////////////////////////
185 {
186 int numBlocks, localPos[4], fieldPos[4];
187 int height, rowIndex, i, j, tmp;
188
189 // Initialize a list used for removing completed rows;
190 for (i=0; i<3; i++) removalList[i] = -1;
191 numRemovals = 0;
192
193 //
194 // Calculate the height and XZ position of each block based on the
195 // standard cube location in the field and the local positions of
196 // the blocks. Use the height to index into the level groups and the
197 // XZ position to index into the nodelist of static block positions.
198 // For each block add a child to the correct level group from the nodelist
199 // of static block positions and fill in the field table for its position.
200 //
201 numBlocks = currentPiece->getPosition (localPos);
202 getFieldPositions (numBlocks, localPos, fieldPos, standardPosition);
203 for (i=0; i<numBlocks; i++)
204 {
205 height = fieldPos[i] / (resolution*resolution);
206 rowIndex = fieldPos[i] % (resolution*resolution);
207
208 //
209 // If the top of the piece is above the total height of the game,
210 // the game is over.
211 //
212 if (height > currentHeight) currentHeight = height;
213 if (height >= totalHeight)
214 {
215 isGameOver = TRUE;
216 return;
217 }
218
219 SoSeparator *level;
220
221 level = (SoSeparator *)(otherBlocks->getChild(height));
222 level->addChild(staticBlock[rowIndex]);
223
224 fieldTable[fieldPos[i]] = 1;
225
226 //
227 // Check the number of blocks now occupying the given level.
228 // If the level is full, add the level on a sorted list as all
229 // of its blocks will be removed.
230 //
231 if (level->getNumChildren() == (resolution*resolution+2))
232 {
233 for (j=0; j<numRemovals; j++)
234 if (removalList[j] == height) break;
235 if (j == numRemovals)
236 {
237 removalList[numRemovals++] = height;
238 for (j=numRemovals; --j>0;)
239 if (removalList[j] < removalList[j-1])
240 {
241 tmp = removalList[j];
242 removalList[j] = removalList[j-1];
243 removalList[j-1] = tmp;
244 }
245 }
246 }
247 }
248
249 // If the piece was dropped, restore the original falling rate.
250 if (isDropping)
251 {
252 isDropping = FALSE;
253 currentRate = saveRate;
254 }
255
256 // Award some points based on where the piece ended up.
257 if (pointsEarned == 0)
258 pointsEarned = standardPosition[TS_Y_AXIS] + 1;
259
260 if (numRemovals > 0)
261 {
262 SoSeparator *levelSep;
263
264 // Award some more points based on how many rows were removed.
265 switch (numRemovals) {
266 case 1: pointsEarned += 100; break;
267 case 2: pointsEarned += 200; break;
268 case 3: pointsEarned += 900; break;
269 case 4: pointsEarned += 1600; break;
270 }
271
272 // Remove the falling piece from the scene graph
273 fieldRoot->removeChild (fallingRoot);
274
275 //
276 // For each row to be removed, substitute a new material
277 // for the normal level material. Then, schedule an alarm
278 // sensor to go off after a time which will actually remove
279 // the rows and animate the remaining rows downwards.
280 //
281 for (i=0; i<numRemovals; i++)
282 {
283 levelSep = (SoSeparator *)levelGroup[removalList[i]];
284 levelSep->replaceChild(levelMaterial[removalList[i]],
285 removalMaterial);
286 }
287 removalSensor->setTimeFromNow(SbTime(REMOVAL_HIGHLIGHT_DURATION));
288 removalSensor->schedule();
289
290 // The row removal code will add a new piece to the field, so control
291 // can just return here.
292 return;
293 }
294 else {
295
296 }
297
298
299 // Add a new piece to the playing field.
300 addPoints( pointsEarned );
301 pointsEarned = 0;
302 addPiece ();
303 }
304
305
306 ////////////////////////////////////////////////////////////////////////
307 //
308 // Description:
309 // This routine checks to see if the current piece has collided with
310 // other blocks in the field.
311 //
312 // Use: private
313
314 SbBool
315 TsField::collision(
316 int numBlocks, // The number of blocks in the piece
317 int *fieldPos) // The field positions of the blocks in the piece
318
319 //
320 ////////////////////////////////////////////////////////////////////////
321 {
322 int i;
323
324 //
325 // Check for intersection with another piece or the floor.
326 // For each block in the piece, if its position is < 0 or
327 // if the field is occupied at that position, a collision
328 // has occurred.
329 //
330 for (i=0; i<numBlocks; i++)
331 {
332 if ((fieldPos[i] < 0) || (fieldTable[fieldPos[i]]))
333 return TRUE;
334 }
335
336 return FALSE;
337 }
338
339
340
341 ////////////////////////////////////////////////////////////////////////
342 //
343 // Description:
344 // This routine checks to see if the current piece will leave the
345 // playing field if the given position of the piece is used.
346 //
347 // Use: private
348
349 SbBool
350 TsField::validPosition(
351 int numBlocks, // The number of blocks in the piece
352 int *pos, // The positions of the blocks in the piece
353 int *standard ) // The row, col, height of the standard cube
354
355 //
356 ////////////////////////////////////////////////////////////////////////
357 {
358 int i;
359
360 //
361 // For each block, check to see if the placement of the standard cube
362 // causes a block to be out of bounds.
363 //
364 for (i=0; i<numBlocks; i++)
365 {
366 if (standard[TS_X_AXIS] == (resolution-1))
367 {
368 if ((pos[i] % 3) > 0)
369 return FALSE;
370 }
371 else if (standard[TS_X_AXIS] == (resolution-2))
372 {
373 if ((pos[i] % 3) > 1)
374 return FALSE;
375 }
376 if (standard[TS_X_AXIS] == -1)
377 {
378 if ((pos[i] % 3) < 1)
379 return FALSE;
380 }
381 else if (standard[TS_X_AXIS] == -2)
382 {
383 if ((pos[i] % 3) < 2)
384 return FALSE;
385 }
386
387 if (standard[TS_Z_AXIS] == (resolution-1))
388 {
389 if ((pos[i] % 9) > 2)
390 return FALSE;
391 }
392 else if (standard[TS_Z_AXIS] == (resolution-2))
393 {
394 if ((pos[i] % 9) > 5)
395 return FALSE;
396 }
397 else if (standard[TS_Z_AXIS] == -1)
398 {
399 if ((pos[i] % 9) < 3)
400 return FALSE;
401 }
402 else if (standard[TS_Z_AXIS] == -2)
403 {
404 if ((pos[i] % 9) < 6)
405 return FALSE;
406 }
407 }
408
409 return TRUE;
410 }
411
412
413
414 ////////////////////////////////////////////////////////////////////////
415 //
416 // Description:
417 // This routine returns a set of global positions given a set of
418 // local positions and the location of the standard cube.
419 //
420 // Use: private
421
422 void
423 TsField::getFieldPositions(
424 int numBlocks, // The number of blocks in the piece
425 int *localPos, // The local positions of the blocks in the piece
426 int *fieldPos, // The field positions of the blocks in the piece
427 int *standard ) // The row, col, height of the standard cube
428
429 //
430 ////////////////////////////////////////////////////////////////////////
431 {
432 int i;
433
434 for (i=0; i<numBlocks; i++)
435 {
436 int lp = localPos[i];
437 fieldPos[i] =
438 (standard[TS_Y_AXIS] + lp/9) * resolution * resolution +
439 (standard[TS_Z_AXIS] + (lp%9)/3) * resolution +
440 (standard[TS_X_AXIS] + lp%3);
441 }
442 }
443
444
445
446
447
448