void makeRandomTree (treegen_t* tg, edgefn ef)
{
    resetStack(tg->sp);
    resetTree(tg->tp);
    genTree (tg->N, tg->T, tg->sp, tg->tp);
    writeTree (tg->tp, ef);
}
Beispiel #2
0
int main(int argc, char **argv)
{
    int i;

    shuffle = malloc(sizeof(shuffle[0]) * RCOUNT);
    if (shuffle == NULL)
	return EXIT_FAILURE;

    root = resetTree();

    for (i =0;i<10;i++) {
	struct testnode *n;

	n = newnode();
	n->key = i;

	printf("Add node %d\n", i);
	AVL_AddNode(&root, &n->node, (AVLNODECOMP)nodecmp_int2);
    }

    dumporder(root);
    dumprorder(root);

    printf("remove by key\n");
    for (i = 0;i<10;i++) {
	AVL_RemNodeByKey(&root, (AVLKey)(IPTR)i, keycmp_int);
    }

    dumporder(root);
    dumprorder(root);

    printf("find next node by key\n");
    root = resetTree();
    for (i =0;i<20;i+=2) {
	struct testnode *n;

	n = newnode();
	n->key = i;

	printf("Add node %d\n", i);
	AVL_AddNode(&root, &n->node, nodecmp_int);
    }

    for (i =0;i<20;i++) {
	struct testnode *n = (struct testnode *)AVL_FindNextNodeByKey(root, (AVLKey)(IPTR)i, keycmp_int);
	int next;

	printf("next node %d = %d\n", i, n ? n->key : -1);

	if ((i % 2) == 0)
	    next = i;
	else
	    next = i + 1;

	if ((n != NULL && n->key != next)
	    || (n == NULL && i != 19))
	    printf("next node by key is wrong!  got %d expected %d\n", (n == NULL ? -1 : n->key), i+1);
    }

    root = resetTree();
    root = NULL;
    printf("insert reverse order\n");
    for (i =9;i>=0;i--) {
	struct testnode *n;

	n = newnode();
	n->key = i;

	AVL_AddNode(&root, &n->node, nodecmp_int);
    }

    dumporder(root);
    dumprorder(root);

    printf("remove by key\n");
    for (i = 0;i<10;i++) {
	AVL_RemNodeByKey(&root, (AVLKey)(IPTR)i, keycmp_int);
    }

    dumporder(root);
    dumprorder(root);

    // root should now be empty
    if (root != NULL) {
	printf("tree not empty!?");
    }

    root = resetTree();
    srandom(0);
    printf("insert random order\n");
    for (i = 0;i<RCOUNT;i++)
	shuffle[i] = i;
    for (i = 0;i<RCOUNT;i++) {
	int f = random() % RCOUNT;
	int t = random() % RCOUNT;
	int tmp;

	tmp = shuffle[f];
	shuffle[f] = shuffle[t];
	shuffle[t] = tmp;
    }
    //for (i=0;i<RCOUNT;i++) {
    //printf(" %d\n", shuffle[i]);
    //}

    for (i=0;i<RCOUNT;i++) {
	struct testnode *n;

	n = newnode();
	n->key = shuffle[i];

	AVL_AddNode(&root, &n->node, nodecmp_int);
    }
    
    //dumporder(root);
    //dumprorder(root);

    srandom(1);
    root = resetTree();

    printf("remove random order\n");
    for (i = 0;i<RCOUNT;i++) {
	struct testnode *n;

	shuffle[i] = i;

	n = newnode();
	n->key = i;

	AVL_AddNode(&root, &n->node, nodecmp_int);
	if ((i % 256) == 0)
	    checkbalance(root);
    }
    for (i = 0;i<RCOUNT;i++) {
	int f = random() % RCOUNT;
	int t = random() % RCOUNT;
	int tmp;
	
	tmp = shuffle[f];
	shuffle[f] = shuffle[t];
	shuffle[t] = tmp;
    }

    for (i=0;i<RCOUNT;i++) {
	AVL_RemNodeByKey(&root, (AVLKey)(IPTR)shuffle[i], keycmp_int);
	if ((i % 256) == 0)
	    checkbalance(root);
    }

    DeletePool(mempool);
    free(shuffle);

    return EXIT_SUCCESS;
}
Beispiel #3
0
void GameScene::step(float dt)
{
    
    particles_counter += dt;
    if (particles_counter > 0.15) {
        particles->setEmissionRate(0);
    }
    
	// Return if game suspended
	if(gameSuspended) return;

	// Get the jumper sprite
	CCSprite *jumper = (CCSprite*)getChildByTag(kjumper);
	
	// Update the player x position based on velocity and delta time
	jumper_pos.x += jumper_vel.x * dt;
	
	jumper->setRotation(jumper_vel.x);

	// Flip the player based on it's x velocity and current looking direction
	if(jumper_vel.x < -30.0f && jumperLookingRight) 
	{
		jumperLookingRight = false;
		jumper->setScaleX(-1.0f);
	}
	else if (jumper_vel.x > 30.0f && !jumperLookingRight) 
	{
		jumperLookingRight = true;
		jumper->setScaleX(1.0f);
	}

	// Calculate the max and min x values for the jumper
	// based on the screen and jumper widths
	CCSize jumper_size = jumper->getContentSize();
//	float max_x = (float)CCDirector::sharedDirector()->getWinSize().width - jumper_size.width/2;
//	float min_x = jumper_size.width/2;
	
	// Limit the jumper position based on max and min values allowed
//	if(jumper_pos.x>max_x) jumper_pos.x = max_x;
//	if(jumper_pos.x<min_x) jumper_pos.x = min_x;
    
    float max_x = (float)CCDirector::sharedDirector()->getWinSize().width + jumper_size.width/2.0;
	float min_x = -jumper_size.width/2.0;
    
    
	// Limit the character position based on max and min values allowed
	if(jumper_pos.x>max_x)
        jumper_pos.x = jumper_size.width/2.0;
	if(jumper_pos.x<min_x)
        jumper_pos.x = (float)CCDirector::sharedDirector()->getWinSize().width - jumper_size.width/2.0;

	// Update the jumper velocity based on acceleration and time
//    if (isPowerPicked)
//    {
//        jumper_vel.y += (jumper_acc.y * dt)*3;
//    }
//    else
    {
      jumper_vel.y += jumper_acc.y * dt;
    }
	

	// Update the jumper y positin based on velocity and time
	jumper_pos.y += jumper_vel.y * dt;
	
	////////////////////////////////////////////////////////////////////////////
	// Handle the bonus scoring
    
    
    
    float delta = jumper_pos.y - ((float)CCDirector::sharedDirector()->getWinSize().height / 2);
    

	// If bonus is visible then see if the jumper is within range to get the bonus
	

	// If the jumper has stopped moving then make it jump from the platform it is on
	int t;
	if(jumper_vel.y < 0) 
	{
		t = kPlatformsStartTag;

		// Search through all the platforms and compare the jumpers position with the platfor position
        
		for(t; t < kPlatformsStartTag + kNumPlatforms; t++) 
		{
			CCSprite *platform = (CCSprite*)getChildByTag(t);

			CCSize platform_size = platform->getContentSize();
			CCPoint platform_pos = platform->getPosition();
			
			max_x = platform_pos.x - platform_size.width/2 - 2;
			min_x = platform_pos.x + platform_size.width/2 + 2;

			float min_y = platform_pos.y + (platform_size.height+jumper_size.height)/2 - kPlatformTopPadding;
			
			if(jumper_pos.x > (max_x) &&
			   jumper_pos.x < (min_x) &&
			   jumper_pos.y > platform_pos.y &&
			   jumper_pos.y < min_y) 
			{
                if (platform->getTag() == kKillerPlatformTag)
                {
                    coin50Platform = 12;
                    coin100Platform = 15;
                    coinJumpPlatform = 25;
                    SharedData::getSharedInstance()->gameover_Text = "Gameover";
                    CCScene *pScene = GameOverScene::scene();
                    CCDirector::sharedDirector()->pushScene(pScene);
                    this->unscheduleAllSelectors();
                }
                else
                {
				jump();
				break;	// Can only jump from one platform at a time to break out of the loop
                }
			}
		}
	
		// If the jumper has fallen below the screen then game over
		if(jumper_pos.y < - jumper_size.height/2) 
		{
            coin50Platform = 12;
            coin100Platform = 15;
            coinJumpPlatform = 25;
            SharedData::getSharedInstance()->gameover_Text = "Gameover";
            CCScene *pScene = GameOverScene::scene();
            CCDirector::sharedDirector()->pushScene(pScene);
            this->unscheduleAllSelectors();
            
//			resetjumper();
		}
	} 
	else if ( jumper_pos.y > ((float)CCDirector::sharedDirector()->getWinSize().height / 2)) 
	{
        
		// If jumper position is greater than the middle of the screen then move the platforms
		// the difference between the jumper y position and middle point of the screen
		
        resetTree();
		// Set the jumper y position to the middle of the screen
		jumper_pos.y = (float)CCDirector::sharedDirector()->getWinSize().height / 2;

		// Move the current platform y by the delta amount
		currentPlatformY -= delta;

		// Move the clouds vertically and reset if necessary
		t = kCloudsStartTag;
		for (t; t < kCloudsStartTag + kNumClouds; t++) 
		{
			CCSprite *cloud = (CCSprite*) getChildByTag(t);

			CCPoint pos = cloud->getPosition();

			// Calculate new position for cloud
			pos.y -= delta * cloud->getScaleY() * 0.8f;

			// If the cloud is off the screen then need to reset this cloud else set its new position
			if (pos.y < -cloud->getContentSize().height/2) 
			{
				currentCloudTag = t;
				resetCloud();
			} 
			else 
			{	// Update the new y position for the cloud.
				cloud->setPosition(pos);
			}
		}

		// Move the platforms vertically and reset if necessary
		t = kPlatformsStartTag;
        int t_coin = kBonusStartTag;
		for (t; t < kPlatformsStartTag + kNumPlatforms; t++) 
		{
			CCSprite *platform = (CCSprite*)getChildByTag(t);
			
			CCPoint pos = platform->getPosition();

			// Calculate new position for platform
			pos = ccp(pos.x, pos.y - delta);

			// If the platform is off the screen then reset the platform else set its new position
			if(pos.y < - platform->getContentSize().height/2) 
			{
				currentPlatformTag = t;
				resetPlatform();
                
			} 
			else 
			{
				platform->setPosition(pos);
                
			}
		}

       
        //move particle system
        if (particles) {
            CCPoint pos = particles->getPosition();
            pos.y -= delta;
            particles->setPosition(pos);
        }
        
		// If the bonus is visible then adjust it's y position
		
		
		// Update score based on how much the jumper has moved
		score += (int)delta*0.3 ;
        
//        if (score >= target_Score)
//        {
//            SharedData::getSharedInstance()->gameover_Text = "Level Cleared";
//            CCScene *pScene = GameOverScene::scene();
//            CCDirector::sharedDirector()->pushScene(pScene);
//            this->unscheduleAllSelectors();
//        }

		// Display the new score value
		char scoreStr[10] = {0};
		sprintf(scoreStr, "%d", score);
		CCLabelTTF* scoreLabel = (CCLabelTTF*) getChildByTag(kScoreLabel);
		scoreLabel->setString(scoreStr);
        
        int t_coins = kBonusStartTag;
        for (t_coins; t_coins < (kNumCoines + kBonusStartTag); t_coins++)
        {
            
            ////////////////////////////////////////////////////////////////////////////
            // Handle the coin scoring
            CCSprite *coin = (CCSprite*)getChildByTag(t_coins);
            
            // If coin is visible then see if the character is within range to get the coin
            if(coin->isVisible() )
            {
                CCPoint coin_pos = coin->getPosition();
                float range = 30.0f;
                
                CCPoint pos = coin->getPosition();
                {
                    // Calculate new position of coin
                    pos.y -= delta;
                    
                    // If the coin is off the screen then reset the coin else set its new position
                    if(pos.y < -coin->getContentSize().height/2 )
                    {
                        //                        resetCoin();
                    }
                    else
                    {
                        coin->setPosition(pos);
                    }
                }
                
            }
            
        }
        t_coins = kBonusStartTag+30;
        for (t_coins; t_coins < (kNumCoines + kBonusStartTag+30); t_coins++)
        {
            
            ////////////////////////////////////////////////////////////////////////////
            // Handle the coin scoring
            CCSprite *coin = (CCSprite*)getChildByTag(t_coins);
            
            // If coin is visible then see if the character is within range to get the coin
            if(coin->isVisible() )
            {
                CCPoint coin_pos = coin->getPosition();
                float range = 30.0f;
                
                CCPoint pos = coin->getPosition();
                {
                    // Calculate new position of coin
                    pos.y -= delta;
                    
                    // If the coin is off the screen then reset the coin else set its new position
                    if(pos.y < -coin->getContentSize().height/2 )
                    {
                        //                        resetCoin();
                    }
                    else
                    {
                        coin->setPosition(pos);
                    }
                }
                
            }
            
        }
        t_coins = kBonusStartTag+60;
        for (t_coins; t_coins < (kNumCoines + kBonusStartTag+60); t_coins++)
        {
            
            ////////////////////////////////////////////////////////////////////////////
            // Handle the coin scoring
            CCSprite *coin = (CCSprite*)getChildByTag(t_coins);
            
            // If coin is visible then see if the character is within range to get the coin
            if(coin->isVisible() )
            {
                CCPoint coin_pos = coin->getPosition();
                float range = 30.0f;
                
                CCPoint pos = coin->getPosition();
                {
                    // Calculate new position of coin
                    pos.y -= delta;
                    
                    // If the coin is off the screen then reset the coin else set its new position
                    if(pos.y < -coin->getContentSize().height/2 )
                    {
                        //                        resetCoin();
                    }
                    else
                    {
                        coin->setPosition(pos);
                    }
                }
                
            }
            
        }
        
	}
    
    int t_coins = kBonusStartTag;
    for (t_coins; t_coins < (kNumCoines + kBonusStartTag); t_coins++)
    {
        
        ////////////////////////////////////////////////////////////////////////////
        // Handle the coin scoring
        CCSprite *coin = (CCSprite*)getChildByTag(t_coins);
        
        // If coin is visible then see if the character is within range to get the coin
        if(coin->isVisible() )
        {
            CCPoint coin_pos = coin->getPosition();
            float range = 30.0f;
            
            
            // If the player is within range of the coin value then award the prize
            if(jumper_pos.x > coin_pos.x - range &&
               jumper_pos.x < coin_pos.x + range &&
               jumper_pos.y > coin_pos.y - range &&
               jumper_pos.y < coin_pos.y + range )
            {
        
                // Build the score string to display
                char scoreStr[10] = {0};
                score += 50;
                sprintf(scoreStr, "%d", score);
                CCLabelTTF* scoreLabel = (CCLabelTTF*) getChildByTag(kScoreLabel);
                scoreLabel->setString(scoreStr);
                coin->setVisible(false);
            }
        }
        
    }
    t_coins = kBonusStartTag+30;
    for (t_coins; t_coins < (kNumCoines + kBonusStartTag+30); t_coins++)
    {
        
        ////////////////////////////////////////////////////////////////////////////
        // Handle the coin scoring
        CCSprite *coin = (CCSprite*)getChildByTag(t_coins);
        
        // If coin is visible then see if the character is within range to get the coin
        if(coin->isVisible() )
        {
            CCPoint coin_pos = coin->getPosition();
            float range = 30.0f;
            
            
            // If the player is within range of the coin value then award the prize
            if(jumper_pos.x > coin_pos.x - range &&
               jumper_pos.x < coin_pos.x + range &&
               jumper_pos.y > coin_pos.y - range &&
               jumper_pos.y < coin_pos.y + range )
            {
                
                // Build the score string to display
                char scoreStr[10] = {0};
                score += 100;
                sprintf(scoreStr, "%d", score);
                CCLabelTTF* scoreLabel = (CCLabelTTF*) getChildByTag(kScoreLabel);
                scoreLabel->setString(scoreStr);
                coin->setVisible(false);
            }
        }
        
    }
    t_coins = kBonusStartTag+60;
    for (t_coins; t_coins < (kNumCoines + kBonusStartTag+60); t_coins++)
    {
        
        ////////////////////////////////////////////////////////////////////////////
        // Handle the coin scoring
        CCSprite *coin = (CCSprite*)getChildByTag(t_coins);
        
        // If coin is visible then see if the character is within range to get the coin
        if(coin->isVisible() )
        {
            CCPoint coin_pos = coin->getPosition();
            float range = 30.0f;
            
            
            // If the player is within range of the coin value then award the prize
            if(jumper_pos.x > coin_pos.x - range &&
               jumper_pos.x < coin_pos.x + range &&
               jumper_pos.y > coin_pos.y - range &&
               jumper_pos.y < coin_pos.y + range )
            {
                
                // Build the score string to display
                this->powerPicked();
                coin->setVisible(false);
            }
        }
        
    }
    
    for (int i = kTree1Tag; i <= kTree3Tag; i++)
    {
        float delta = jumper_pos.y - ((float)CCDirector::sharedDirector()->getWinSize().height / 2);
        CCSprite * tree = (CCSprite *)this->getChildByTag(i);
        if ( jumper_pos.y > ((float)CCDirector::sharedDirector()->getWinSize().height / 2))
        {
            tree->setPosition(ccp(tree->getPosition().x,tree->getPosition().y-delta));
        }
        
       
        
        if (tree->getPosition().y <= -0.5*CCDirector::sharedDirector()->getWinSize().height )
        {
            float posY;
            if (i == kTree1Tag)
            {
                CCSprite * temp = (CCSprite *)this->getChildByTag(kTree3Tag);
                posY = temp->getPosition().y + temp->getContentSize().height*0.99;
                tree->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width*0.5, posY));
            }
            else if (i == kTree2Tag)
            {
                CCSprite * temp = (CCSprite *)this->getChildByTag(kTree1Tag);
                posY = temp->getPosition().y + temp->getContentSize().height*0.99;
                tree->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width*0.5, posY));
            }
            else if (i == kTree3Tag)
            {
                CCSprite * temp = (CCSprite *)this->getChildByTag(kTree2Tag);
                posY = temp->getPosition().y + temp->getContentSize().height*0.99;
                tree->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width*0.5, posY));
            }
            
        }
    }
    for (int i = kTree11Tag; i <= kTree33Tag; i++)
    {
        float delta = jumper_pos.y - ((float)CCDirector::sharedDirector()->getWinSize().height / 2);
        CCSprite * tree = (CCSprite *)this->getChildByTag(i);
        if ( jumper_pos.y > ((float)CCDirector::sharedDirector()->getWinSize().height / 2))
        {
            tree->setPosition(ccp(tree->getPosition().x,tree->getPosition().y-delta*0.1));
        }
        
        
        
        if (tree->getPosition().y <= -0.5*CCDirector::sharedDirector()->getWinSize().height )
        {
            float posY;
            if (i == kTree11Tag)
            {
                CCSprite * temp = (CCSprite *)this->getChildByTag(kTree33Tag);
                posY = temp->getPosition().y + temp->getContentSize().height*0.99;
                tree->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width*0.5, posY));
            }
            else if (i == kTree22Tag)
            {
                CCSprite * temp = (CCSprite *)this->getChildByTag(kTree11Tag);
                posY = temp->getPosition().y + temp->getContentSize().height*0.99;
                tree->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width*0.5, posY));
            }
            else if (i == kTree33Tag)
            {
                CCSprite * temp = (CCSprite *)this->getChildByTag(kTree22Tag);
                posY = temp->getPosition().y + temp->getContentSize().height*0.99;
                tree->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width*0.5, posY));
            }
            
        }
    }

    
	// Set the jumpers position
	jumper->setPosition(jumper_pos);
}
Beispiel #4
0
void TreeManager::update(float dt){
    trees1->moveWithScene();
    trees2->moveWithScene();
    trees3->moveWithScene();
    resetTree();
}