File: SculptureManager.cpp

package info (click to toggle)
transcend 0.3.dfsg2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, jessie, jessie-kfreebsd, stretch
  • size: 5,796 kB
  • ctags: 3,074
  • sloc: cpp: 26,886; ansic: 693; sh: 210; makefile: 99; perl: 67
file content (1006 lines) | stat: -rw-r--r-- 30,680 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
/*
 * Modification History
 *
 * 2004-June-21   Jason Rohrer
 * Created.
 *
 * 2004-August-6   Jason Rohrer
 * Fixed consistency bugs in test for in/out of sculpture status.
 *
 * 2004-August-22   Jason Rohrer
 * Added music parts.
 *
 * 2004-August-23   Jason Rohrer
 * Added function for getting number of pieces in sculpture.
 *
 * 2004-August-29   Jason Rohrer
 * Changed so that only pieces in sculpture are animated.
 *
 * 2004-August-30   Jason Rohrer
 * Optimization:  avoid object blending whenever possible.
 * Added function for getting piece position needed to recenter.
 * Weakened the power modifier curve.
 *
 * 2004-September-3   Jason Rohrer
 * Changed to use pure distance between pieces instead of gap for in-sculpture
 * test.
 *
 * 2005-August-22   Jason Rohrer
 * Changed so that isPieceJarred returns true only if jar force is increasing.
 * Added magnet mode to smooth piece pick-up and drop.
 */



#include "SculptureManager.h"



#include <float.h>



SculptureManager::SculptureManager(
    ParameterizedObject *inFirstSculptureTemplate,
    ParameterizedObject *inSecondSculptureTemplate,
    double inSculptureScale,
    double inMaxDistanceToBeInSculpture,
    double inAnimationLoopTime,
    int inNumSculpturePieces,
    double *inPieceShapeParameters,
    Vector3D **inPieceStartingPositions,
    Angle3D **inPieceStartingRotations,
    MusicPart **inPieceMusicParts,
    FILE *inSculpturePowerUpSpaceFILE,
    char *outError,
    ShipBulletManager *inEnemyBulletManager,
    double inEnemyBulletJarPower,
    ShipBulletManager *inBossBulletManager,
    double inBossBulletJarPower,
    double inFriction,
    double inWorldWidth,
    double inWorldHeight )
    : mFirstSculpturePieceTemplate( inFirstSculptureTemplate ),
      mSecondSculpturePieceTemplate( inSecondSculptureTemplate ),
      mSculptureScale( inSculptureScale ),
      mMaxDistanceToBePartOfSculpture( inMaxDistanceToBeInSculpture ),
      mAnimationLoopTime( inAnimationLoopTime ),
      mCurrentAnimationPosition( 0 ),
      mCurrentAnimationDirection( 1 ),
      mNumSculpturePieces( inNumSculpturePieces ),
      mSculpturePieceParameters( inPieceShapeParameters ),
      mCurrentPiecePositions( inPieceStartingPositions ),
      mCurrentPieceRotations( inPieceStartingRotations ),
      mPieceMusicParts( inPieceMusicParts ),
      mEnemyBulletManager( inEnemyBulletManager ),
      mEnemyBulletJarPower( inEnemyBulletJarPower ),
      mBossBulletManager( inBossBulletManager ),
      mBossBulletJarPower( inBossBulletJarPower ),
      mFriction( inFriction ),
      mMaxXPosition( inWorldWidth / 2 ),
      mMinXPosition( - inWorldWidth / 2 ),
      mMaxYPosition( inWorldHeight / 2 ),
      mMinYPosition( - inWorldHeight / 2 ) {

    mPieceMagnetModes = new char[ mNumSculpturePieces ];
    mCurrentPieceTargetPositions = new Vector3D*[ mNumSculpturePieces ];
    mCurrentTowardTargetVelocities = new double[ mNumSculpturePieces ];
    
    mCurrentPieceRotationRates = new double[ mNumSculpturePieces ];
    mCurrentPieceRadii = new double[ mNumSculpturePieces ];
    mCurrentJarForces = new double[ mNumSculpturePieces ];
    mJarForcesIncreasing = new char[ mNumSculpturePieces ];
    
    mNumPiecesInSculpture = 0;
    mInSculptureFlags = new char[ mNumSculpturePieces ];
    mDelayAnimationStartFlags = new char[ mNumSculpturePieces ];
    mDelayAnimationStopFlags = new char[ mNumSculpturePieces ];
 
    int i;
    
    for( i=0; i<mNumSculpturePieces; i++ ) {
        mPieceMagnetModes[i] = false;
        mCurrentPieceTargetPositions[i] =
            new Vector3D( mCurrentPiecePositions[i] );
        mCurrentTowardTargetVelocities[i] = 0;
        
        mCurrentPieceRotationRates[i] = 0;
        mCurrentPieceRadii[i] = 0;
        mCurrentJarForces[i] = 0;
        mJarForcesIncreasing[i] = 0;
        
        mInSculptureFlags[i] = false;
        mDelayAnimationStartFlags[i] = false;
        mDelayAnimationStopFlags[i] = false;
        }
    
    
    int numRead = fscanf( inSculpturePowerUpSpaceFILE, "%d",
                          &mNumParameterMapAnchors );

    if( numRead != 1 ) {
        // default
        mNumParameterMapAnchors = 1;
        *outError = true;
        }

    mParameterMapAnchors = new double[ mNumParameterMapAnchors ];
    mParameterMapCloseRangeValues = new double[ mNumParameterMapAnchors ];
    mParameterMapFarRangeValues = new double[ mNumParameterMapAnchors ];


    for( i=0; i<mNumParameterMapAnchors; i++ ) {
        numRead = fscanf( inSculpturePowerUpSpaceFILE, "%lf",
                          &( mParameterMapAnchors[i] ) );
        if( numRead != 1 ) {
            // default
            mParameterMapAnchors[i] = 0.5;
            *outError = true;
            }

        numRead = fscanf( inSculpturePowerUpSpaceFILE, "%lf",
                          &( mParameterMapCloseRangeValues[i] ) );
        if( numRead != 1 ) {
            // default
            mParameterMapCloseRangeValues[i] = 0.5;
            *outError = true;
            }

        
        numRead = fscanf( inSculpturePowerUpSpaceFILE, "%lf",
                          &( mParameterMapFarRangeValues[i] ) );
        if( numRead != 1 ) {
            // default
            mParameterMapFarRangeValues[i] = 0.5;
            *outError = true;
            }
        }
    }



SculptureManager::~SculptureManager() {
    delete mFirstSculpturePieceTemplate;
    delete mSecondSculpturePieceTemplate;
    
    for( int i=0; i<mNumSculpturePieces; i++ ) {
        delete mCurrentPiecePositions[i];
        delete mCurrentPieceTargetPositions[i];
        delete mCurrentPieceRotations[i];
        delete mPieceMusicParts[i];
        }

    delete [] mSculpturePieceParameters;
    delete [] mCurrentPiecePositions;

    delete [] mPieceMagnetModes;
    delete [] mCurrentPieceTargetPositions;
    delete [] mCurrentTowardTargetVelocities;
    
    delete [] mCurrentPieceRotations;
    delete [] mPieceMusicParts;
    delete [] mCurrentPieceRotationRates;
    delete [] mCurrentPieceRadii;
    delete [] mCurrentJarForces;
    delete [] mJarForcesIncreasing;
    
    delete [] mInSculptureFlags;
    delete [] mDelayAnimationStartFlags;
    delete [] mDelayAnimationStopFlags;
    
    delete [] mParameterMapAnchors;
    delete [] mParameterMapCloseRangeValues;
    delete [] mParameterMapFarRangeValues;
    }



double SculptureManager::getCloseRangeBulletParameter() {

    double closeRangeSum = 0;
    
    for( int i=0; i<mNumSculpturePieces; i++ ) {
        if( mInSculptureFlags[i] ) {
            // piece is in sculpture

            // add its power ups
            double closeRange;
            double farRange;

            mapParameters( mSculpturePieceParameters[i],
                           &closeRange, &farRange );

            closeRangeSum += closeRange;
            }
        }

    if( closeRangeSum > 1 ) {
        closeRangeSum = 1;
        }
    
    return closeRangeSum;
    }



double SculptureManager::getFarRangeBulletParameter() {
    double farRangeSum = 0;
    
    for( int i=0; i<mNumSculpturePieces; i++ ) {
        if( mInSculptureFlags[i] ) {
            // piece is in sculpture

            // add its power ups
            double closeRange;
            double farRange;

            mapParameters( mSculpturePieceParameters[i],
                           &closeRange, &farRange );

            farRangeSum += farRange;
            }
        }

    if( farRangeSum > 1 ) {
        farRangeSum = 1;
        }
    return farRangeSum;
    }



Vector3D *SculptureManager::getSculptureCenter() {
    Vector3D *positionSum = new Vector3D( 0, 0, 0 );

    int numPiecesInSculpture = 0;
    for( int i=0; i<mNumSculpturePieces; i++ ) {
        if( mInSculptureFlags[i] ) {

            positionSum->add( mCurrentPiecePositions[i] );

            numPiecesInSculpture++;
            }
        }
    
    if( numPiecesInSculpture > 0 ) {
        // convert sum to average
        positionSum->scale( 1.0 / (double)numPiecesInSculpture );
        }

    return positionSum;
    }



double SculptureManager::getBulletPowerModifier() {
    Vector3D *center = getSculptureCenter();
    
    Vector3D *zeroVector = new Vector3D( 0, 0, 0 );

    double distanceFromCenter = zeroVector->getDistance( center );

    delete zeroVector;
    delete center;

    // use inverse exponential (power of e) to map the [0, inf] distance
    // into [1,0]
    // scale the distance somewhat so the curve is not so extreme
    double returnValue = 1 / exp( 0.05 * distanceFromCenter );
    
    // lower limit of 0.25
    return returnValue * 0.75 + 0.25;
    }



void SculptureManager::passTime( double inTimeDeltaInSeconds ) {

    // full animation loop (forward and back) is 2 parameter space units
    double animationDelta = 2 * inTimeDeltaInSeconds / mAnimationLoopTime;

    mCurrentAnimationPosition += mCurrentAnimationDirection * animationDelta;

    if( mCurrentAnimationPosition > 1 ) {
        // bounce back into range
        double extra = mCurrentAnimationPosition - 1;
        mCurrentAnimationPosition = 1 - extra;
        
        // reverse animation direction
        mCurrentAnimationDirection = -1;

        if( mCurrentAnimationPosition < 0 ) {
            // we bounced back too far

            // default to known safe state
            mCurrentAnimationPosition = 0;
            mCurrentAnimationDirection = 1;
            }            
        }
    else if( mCurrentAnimationPosition < 0 ) {
        // bounce back into range
        double extra = 0 - mCurrentAnimationPosition;
        mCurrentAnimationPosition = 0 + extra;
        
        // reverse animation direction
        mCurrentAnimationDirection = 1;

        if( mCurrentAnimationPosition > 1 ) {
            // we bounced back too far

            // default to known safe state
            mCurrentAnimationPosition = 1;
            mCurrentAnimationDirection = -1;
            }

        // we have reached the animation starting point
        // start any animations that are waiting to start, and stop
        // any that are waiting to stop (to ensure clean starts and stops)
        for( int i=0; i<mNumSculpturePieces; i++ ) {
            mDelayAnimationStartFlags[i] = false;
            mDelayAnimationStopFlags[i] = false;
            }
        }

    
    for( int i=0; i<mNumSculpturePieces; i++ ) {
        // rotate piece at its current rate
        Angle3D *currentPieceRotation = mCurrentPieceRotations[i];

        currentPieceRotation->mZ +=
            inTimeDeltaInSeconds * mCurrentPieceRotationRates[i];

        
        // check if piece being hit by bullet
        
        double enemyBulletPower =
            mEnemyBulletManager->getBulletPowerInCircle(
                mCurrentPiecePositions[i],
                mCurrentPieceRadii[i] );
        double bossBulletPower = 
            mBossBulletManager->getBulletPowerInCircle(
                mCurrentPiecePositions[i],
                mCurrentPieceRadii[i] );

        // scale bullet power by time delta
        double jarForceIncrease =
            inTimeDeltaInSeconds * enemyBulletPower * mEnemyBulletJarPower +
            inTimeDeltaInSeconds * bossBulletPower * mBossBulletJarPower;


        // save last jar force to detect whether force has increased
        double lastJarForce = mCurrentJarForces[i];
        
        // increased by being hit by bullets
        mCurrentJarForces[i] += jarForceIncrease;
        
        // decayed by friction
        mCurrentJarForces[i] -= mFriction * inTimeDeltaInSeconds;

        // never negative
        if( mCurrentJarForces[i] < 0 ) {
            mCurrentJarForces[i] = 0;
            }

        double jarForceDelta = mCurrentJarForces[i] - lastJarForce;

        if( jarForceDelta > 0 ) {
            mJarForcesIncreasing[i] = true;
            }
        else {
            mJarForcesIncreasing[i] = false;
            }
        
        
        if( mCurrentJarForces[i] > 0 ) {

            // jar piece away from center
            Vector3D *jarVector = new Vector3D( 0, 0, 0 );
            jarVector->subtract( mCurrentPiecePositions[i] );

            jarVector->scale( -1 );
            
            if( jarVector->getLength() > 0 ) {
                jarVector->normalize();
                }
            else {
                // cannot compute a proper vector for moving piece "away"
                // from center, since it is at center.
                
                // default to moving piece in y direction
                jarVector->mY = 1;
                }
            
            jarVector->scale( mCurrentJarForces[i] * inTimeDeltaInSeconds );
            mCurrentPiecePositions[i]->add( jarVector );

            Vector3D *currentPosition =
                mCurrentPiecePositions[i];

            // bound jarring action to inside of world

            // jarring stops when we hit a world edge
            
            if( currentPosition->mX > mMaxXPosition ) {
                currentPosition->mX = mMaxXPosition;
                mCurrentJarForces[i] = 0;
                }
            if( currentPosition->mX < mMinXPosition ) {
                currentPosition->mX = mMinXPosition;
                mCurrentJarForces[i] = 0;
                }
            if( currentPosition->mY > mMaxYPosition ) {
                currentPosition->mY = mMaxYPosition;
                mCurrentJarForces[i] = 0;
                }
            if( currentPosition->mY < mMinYPosition ) {
                currentPosition->mY = mMinYPosition;
                mCurrentJarForces[i] = 0;
                }
            
            delete jarVector;

            updateInOutStatusOfAllPieces();
            }

        
        if( mPieceMagnetModes[i] ) {
            // move piece toward target

            // velocity toward target increases at rate of 20 unit/sec per sec
            mCurrentTowardTargetVelocities[i] += 20;

            double totalDistance =
                mCurrentPieceTargetPositions[i]->getDistance(
                    mCurrentPiecePositions[i] );
            
            double moveDistance =
                inTimeDeltaInSeconds *
                mCurrentTowardTargetVelocities[i];

            if( moveDistance >= totalDistance ) {
                // we will hit or pass our target with this move
                delete mCurrentPiecePositions[i];
                mCurrentPiecePositions[i] =
                    new Vector3D( mCurrentPieceTargetPositions[i] );

                // velocity goes back to 0 instantly
                mCurrentTowardTargetVelocities[i] = 0;

                // turn off magnet mode
                mPieceMagnetModes[i] = false;
                }
            else {
                // this move does not hit or pass our target, so execute it
                Vector3D *moveVector =
                    new Vector3D( mCurrentPieceTargetPositions[i] );

                moveVector->subtract( mCurrentPiecePositions[i] );
                
                moveVector->normalize();
                
                moveVector->scale( moveDistance );
                
                mCurrentPiecePositions[i]->add( moveVector );
                
                delete moveVector;
                }
            }
        
        }
        
    }



int SculptureManager::getSculpturePieceInCircle( Vector3D *inCenter,
                                                 double inRadius ) {
    int closestPiece = -1;
    double closestDistance = DBL_MAX;
    
    for( int i=0; i<mNumSculpturePieces; i++ ) {
        double distance = inCenter->getDistance( mCurrentPiecePositions[i] );

        distance = distance - mCurrentPieceRadii[i];
        
        if( distance <= inRadius && distance < closestDistance ) {
            closestPiece = i;
            closestDistance = distance;
            }
        }

    return closestPiece;
    }



Vector3D *SculptureManager::getPositionOfClosestSculpturePiece(
    Vector3D *inPosition ) {
    
    Vector3D *closestPiecePosition = NULL;
    double closestDistance = DBL_MAX;
    
    for( int i=0; i<mNumSculpturePieces; i++ ) {
        if( mInSculptureFlags[i] ) {
            double distance =
                inPosition->getDistance( mCurrentPiecePositions[i] );
            
            if( distance < closestDistance ) {
                closestPiecePosition = mCurrentPiecePositions[i];
                closestDistance = distance;
                }
            }
        }

    if( closestPiecePosition != NULL ) {
        return new Vector3D( closestPiecePosition );
        }
    else {
        return NULL;
        }
    }



int SculptureManager::getNumPiecesInSculpture() {
    int count = 0;
    for( int i=0; i<mNumSculpturePieces; i++ ) {
        if( mInSculptureFlags[i] ) {
            count++;
            }
        }
    return count;
    }



Vector3D **SculptureManager::getPiecePositions( int *outNumPieces ) {

    SimpleVector<Vector3D*> *positions = new SimpleVector<Vector3D*>();

    for( int i=0; i<mNumSculpturePieces; i++ ) {
        if( mInSculptureFlags[i] ) {

            positions->push_back( new Vector3D( mCurrentPiecePositions[i] ) );
            }
        }

    *outNumPieces = positions->size();

    Vector3D **returnArray = positions->getElementArray();

    delete positions;

    return returnArray;
    }



MusicPart **SculptureManager::getPieceMusicParts( int *outNumPieces ) {

    SimpleVector<MusicPart*> *parts = new SimpleVector<MusicPart*>();

    for( int i=0; i<mNumSculpturePieces; i++ ) {
        if( mInSculptureFlags[i] ) {

            parts->push_back( mPieceMusicParts[i] );
            }
        }

    *outNumPieces = parts->size();

    MusicPart **returnArray = parts->getElementArray();

    delete parts;

    return returnArray;
    }



void SculptureManager::updateInOutStatusOfAllPieces() {
    // first, flag all pieces as out, except those that are close to origin

    // origin position
    Vector3D *zeroVector = new Vector3D( 0, 0, 0 );

    // update our count
    int oldCount = mNumPiecesInSculpture;
    mNumPiecesInSculpture = 0;
    

    // remember old flags
    char *oldFlags = new char[ mNumSculpturePieces ];

    memcpy( oldFlags, mInSculptureFlags,
            sizeof( char ) * mNumSculpturePieces );
    
    int i;
    for( i=0; i<mNumSculpturePieces; i++ ) {
        double distanceToOrigin =
            zeroVector->getDistance( mCurrentPiecePositions[ i ] );
        
        if( distanceToOrigin <= mMaxDistanceToBePartOfSculpture ) {
            mInSculptureFlags[i] = true;
            mNumPiecesInSculpture++;
            }
        else {
            mInSculptureFlags[i] = false;
            }
        }
    
    delete zeroVector;

    // now walk through pieces over and over and expand the set by looking
    // for pieces not in the set that are close to pieces that are already
    // in the set

    // stop when no piece was added in the last round
    char piecesAdded = true;

    while( piecesAdded ) {
        piecesAdded = false;

        for( i=0; i<mNumSculpturePieces; i++ ) {

            if( mInSculptureFlags[i] == false ) {
                // a piece not in the set yet
                
                char added = false;
                
                for( int j=0; j<mNumSculpturePieces && !added; j++ ) {

                    if( mInSculptureFlags[j] == true ) {
                        // a piece in the set

                        double distance =
                            mCurrentPiecePositions[i]->getDistance(
                                mCurrentPiecePositions[j] );

                        if( distance <= mMaxDistanceToBePartOfSculpture ) {
                            mInSculptureFlags[i] = true;
                            mNumPiecesInSculpture++;
                            
                            added = true;
                            piecesAdded = true;
                            }
                        }                
                    }
                }
            }
        }


    
    char animationReset = false;
    
    if( oldCount == 0 &&
        mNumPiecesInSculpture != 0 ) {

        // sculpture used to be empty... but now has pieces

        // make sure no piece is in the process of its stop animation
        char pieceStopping = false;
        for( i=0; i<mNumSculpturePieces && !pieceStopping; i++ ) {
            pieceStopping = mDelayAnimationStopFlags[i];
            }

        if( !pieceStopping ) {
            animationReset = true;
            // reset the animation to give instant gratification

            mCurrentAnimationPosition = 0;
            mCurrentAnimationDirection = 1;

            // do not delay animations at all
            for( i=0; i<mNumSculpturePieces; i++ ) {
                mDelayAnimationStartFlags[i] = false;
                }
            }
        }

    if( !animationReset ) {

        // check for pieces that have changed status
    
        for( i=0; i<mNumSculpturePieces; i++ ) {

            if( oldFlags[i] != mInSculptureFlags[i] ) {

                if( oldFlags[i] ) {
                    // piece just removed

                // make sure piece not waiting to start animation
                    if( ! mDelayAnimationStartFlags[i] ) {
                        mDelayAnimationStopFlags[i] = true;
                        }
                    else {
                        // piece added and removed before it got a chance
                        // to start its animation
                        mDelayAnimationStartFlags[i] = false;
                        }
                    }
                else{
                    // piece just added

                    // make sure piece not waiting to stop animation
                    if( ! mDelayAnimationStopFlags[i] ) {
                        mDelayAnimationStartFlags[i] = true;
                        }
                    else {
                        // piece removed and re-added before it got a chance
                        // to stop its animation
                        mDelayAnimationStopFlags[i] = false;
                        }
                    }
                }
            }
        }

    delete [] oldFlags;
    }



Vector3D *SculptureManager::getPiecePositionNeededToRecenter() {

    int numPieces = getNumPiecesInSculpture();

    Vector3D *center = getSculptureCenter();


    // opposite weighted center
    center->scale( -numPieces );

    return center;    
    }



void SculptureManager::turnMagnetModeOn( int inPieceHandle ) {
    mPieceMagnetModes[ inPieceHandle ] = true;
    }



void SculptureManager::setPiecePosition( int inPieceHandle,
                                         Vector3D *inNewPosition ) {
    Vector3D *position;

    if( mPieceMagnetModes[ inPieceHandle ] ) {
        // piece in magnet mode, set target position
        position = mCurrentPieceTargetPositions[ inPieceHandle ];
        }
    else {
        // not in magnet mode, set current position
        position = mCurrentPiecePositions[ inPieceHandle ];
        }
    
    position->mX = inNewPosition->mX;
    position->mY = inNewPosition->mY;
    position->mZ = inNewPosition->mZ;

    updateInOutStatusOfAllPieces();
    }



SimpleVector<DrawableObject *> *SculptureManager::getDrawableObjects() {
    SimpleVector<DrawableObject *> *returnVector =
        new SimpleVector<DrawableObject *>();

    for( int i=0; i<mNumSculpturePieces; i++ ) {
        double pieceRotationRate;


        double animPosition = 0;

        // if piece is part of sculpture and it is not waiting to start
        // its animation
        // or if piece is no longer part of sculpture but its animation
        // is finishing up
        if( ( mInSculptureFlags[i] && ! mDelayAnimationStartFlags[i] )
            ||
            mDelayAnimationStopFlags[i] ) {
            
            // animate it
            animPosition = mCurrentAnimationPosition;
            }
        
        // avoid blending if possible
        ObjectParameterSpaceControlPoint *animationPoint;

        if( animPosition == 0 ) {
            // use pure first point
            animationPoint =
                mFirstSculpturePieceTemplate->getBlendedControlPoint(
                    mSculpturePieceParameters[i] );
            }
        else if( animPosition == 1 ) {
            // use pure second point
            animationPoint =
                mSecondSculpturePieceTemplate->getBlendedControlPoint(
                    mSculpturePieceParameters[i] );
            }
        else {
            ObjectParameterSpaceControlPoint *firstControlPoint =
                mFirstSculpturePieceTemplate->getBlendedControlPoint(
                    mSculpturePieceParameters[i] );

            ObjectParameterSpaceControlPoint *secondControlPoint =
                mSecondSculpturePieceTemplate->getBlendedControlPoint(
                    mSculpturePieceParameters[i] );


        
            animationPoint =
                (ObjectParameterSpaceControlPoint *)(
                    firstControlPoint->createLinearBlend(
                        secondControlPoint,
                        animPosition ) );
            delete firstControlPoint;
            delete secondControlPoint;
            }

        pieceRotationRate = animationPoint->getRotationRate();
        
        SimpleVector<DrawableObject *> *pieceObjects =
            animationPoint->getDrawableObjects();

        delete animationPoint;
        
        mCurrentPieceRotationRates[i] = pieceRotationRate;

        int numObjects = pieceObjects->size();

        double maxRadius = 0;
        
        for( int j=0; j<numObjects; j++ ) {
            DrawableObject *currentObject = *( pieceObjects->getElement( j ) );

            currentObject->scale( mSculptureScale );
            
            currentObject->rotate(
                mCurrentPieceRotations[i] );

            currentObject->move( mCurrentPiecePositions[i] );

            double radius =
                currentObject->getBorderMaxDistance(
                    mCurrentPiecePositions[i] );

            if( radius > maxRadius ) {
                maxRadius = radius;
                }
            
            returnVector->push_back( currentObject );
            }

        mCurrentPieceRadii[i] = maxRadius;
        
        delete pieceObjects;
        }

    return returnVector;
    }



char SculptureManager::isPieceJarred( int inPieceHandle ) {
    // piece is only being jarred if force is increasing
    if( mJarForcesIncreasing[ inPieceHandle ] > 0 ) {
        return true;
        }
    else {
        return false;
        }
    }



void SculptureManager::mapParameters( double inParameter,
                                      double *outCloseRangePowerUp,
                                      double *outFarRangePowerUp ) {

    
    if( mNumParameterMapAnchors >= 2 ) {
        // find the 2 surrounding anchor points

        // all distances will be no more than 1
        double distanceFromClosestLargerPoint = 2;
        double distanceFromClosestSmallerPoint = 3;
        
        int indexOfClosestLargerPoint = -1;
        int indexOfClosestSmallerPoint = -1;

        for( int i=0; i<mNumParameterMapAnchors; i++ ) {

            double distanceFromThisPoint =
                fabs( mParameterMapAnchors[i] - inParameter );  

            if( mParameterMapAnchors[i] >= inParameter ) {
                // a larger point
                if( distanceFromThisPoint < distanceFromClosestLargerPoint ) {
                    // closer than our closest largerpoint
                    
                    indexOfClosestLargerPoint = i;
                    distanceFromClosestLargerPoint = distanceFromThisPoint;
                    }
                }
            else {
                // a smaller point
                if( distanceFromThisPoint < distanceFromClosestSmallerPoint ) {
                    // closer than our closest smallerpoint

                    indexOfClosestSmallerPoint = i;
                    distanceFromClosestSmallerPoint = distanceFromThisPoint;
                    }
                }
            }
        if( indexOfClosestLargerPoint != -1  &&
            indexOfClosestSmallerPoint != -1 ) {
            // found two points
            
            // compute weights
            double distanceBetweenSurroundingPoints =
                fabs( mParameterMapAnchors[
                          indexOfClosestLargerPoint ] -
                      mParameterMapAnchors[
                          indexOfClosestSmallerPoint ] );

            double weightOnLargerPoint =
                distanceFromClosestSmallerPoint /
                distanceBetweenSurroundingPoints;
            double weightOnSmallerPoint = 1 - weightOnLargerPoint;

            // blend the two points, using the distance to weight them
            *outCloseRangePowerUp =
                weightOnLargerPoint *
                mParameterMapCloseRangeValues[ indexOfClosestLargerPoint ] +
                weightOnSmallerPoint *
                mParameterMapCloseRangeValues[ indexOfClosestSmallerPoint ];

            *outFarRangePowerUp =
                weightOnLargerPoint *
                mParameterMapFarRangeValues[ indexOfClosestLargerPoint ] +
                weightOnSmallerPoint *
                mParameterMapFarRangeValues[ indexOfClosestSmallerPoint ];
            }
        else {
            // found only one point

            int indexOfPoint;
            
            if( indexOfClosestLargerPoint != -1 ) {
                indexOfPoint = indexOfClosestLargerPoint;
                }
            else if( indexOfClosestLargerPoint != -1 ) {
                indexOfPoint = indexOfClosestSmallerPoint;
                }
            else {
                printf( "Error:  found no closest sculpture parameter map "
                        " anchor point.\n" );
                indexOfPoint = 0;
                }

            // return this point's power ups
            *outCloseRangePowerUp =
                mParameterMapCloseRangeValues[ indexOfPoint ];
            *outCloseRangePowerUp =
                mParameterMapCloseRangeValues[ indexOfPoint ];
            }
        }
    else if( mNumParameterMapAnchors == 1 ) {
        // only one anchor point... return its power ups

        *outCloseRangePowerUp =
            mParameterMapCloseRangeValues[ 0 ];
        *outCloseRangePowerUp =
            mParameterMapCloseRangeValues[ 0  ];
        }
    else {
        printf( "Error:  no anchor points in sculpture parameter space.\n" );

        *outCloseRangePowerUp = 0;
        *outFarRangePowerUp = 0;
        }
    }