bool isv(TreeNode *root, long long min, long long max) {
     if(root == NULL) return true;
     if((root->val <= min) || (root->val >= max)) return false;
     
     if(!isv(root->left,min,(long long) root->val)) return false;
     if(!isv(root->right, (long long) root->val,max)) return false;
     return true;
 }
 bool isValidBST(TreeNode *root) {
     return isv(root, -10000000000ll, 10000000000ll);
 }
Exemplo n.º 3
0
void CollideEntity::updateMovement(float dt)
{	
	if (isEntityDead()) return;
	if (position.isFollowingPath()) return;
	vel.capLength2D(getMaxSpeed()*maxSpeedLerp.x);
	/*
	if (vel.getSquaredLength2D() > sqr(getMaxSpeed()))
	{
		vel.setLength2D(getMaxSpeed());
		vel.z = 0;
	}
	*/
	//Vector lastPos = pos;

	updateVel2(dt);

	if (doCusion)
	{
		Vector push;
		TileVector t(position+vel*dt);
		if (dsq->game->isObstructed(TileVector(t.x-1, t.y)))
		{
			push += Vector(1.25,0);
		}
		if (dsq->game->isObstructed(TileVector(t.x+1, t.y)))
		{
			push += Vector(-1.25,0);
		}
		if (dsq->game->isObstructed(TileVector(t.x, t.y-1)))
		{
			push += Vector(0,1.25);
		}
		if (dsq->game->isObstructed(TileVector(t.x, t.y+1)))
		{
			push += Vector(0,-1.25);
		}
		if (dsq->game->isObstructed(TileVector(t.x-1, t.y-1)))
		{
			push += Vector(0.5,0.5);
		}
		if (dsq->game->isObstructed(TileVector(t.x-1, t.y+1)))
		{
			push += Vector(0.5,-0.5);
		}
		if (dsq->game->isObstructed(TileVector(t.x+1, t.y-1)))
		{
			push += Vector(-0.5,0.5);
		}
		if (dsq->game->isObstructed(TileVector(t.x+1, t.y+1)))
		{
			push += Vector(-0.5,-0.5);
		}

		// cushion
		
		if (push.x != 0 || push.y != 0)
		{
			if (vel.getSquaredLength2D() > sqr(10))
			{
				push.setLength2D(100 * dt * 60);
				push.z = 0;
			}
			vel += push;
		}
	}
	
	Vector lastPosition = position;

	bool underWater = isUnderWater();
	if (!canLeaveWater)
	{
		if (!underWater && wasUnderWater)
		{
			// do clamp
			if (waterBubble)
			{
				waterBubble->clampPosition(&position, collideRadius);
			}
			else
			{
				position.y = dsq->game->getWaterLevel()+collideRadius;
			}
			
		}
	}
	/*
	if (!canLeaveWater)
	{
		if (waterBubble)
		{
		}
		else
		{
			if (position.y-collideRadius < dsq->game->getWaterLevel())
			{
				
			}
		}
	}
	*/

	bool collided = false;
	
	if (vel.x != 0 || vel.y != 0)
	{

		const int hw = collideRadius;
		bool freeRange = false;

		if (isv(EV_COLLIDELEVEL,1))
		{
			
			

			bool doesFreeRange = !isPullable();
			if (doesFreeRange)
			{
				if (dsq->game->collideCircleWithGrid(position, hw))
				{
					// starting in a collision state
					freeRange = true;
				}
			}
		}
		
		//Vector lastPosition = lastPos;
		Vector newPosition = position + (getMoveVel() * dt);
		position = newPosition;

		if (isv(EV_COLLIDELEVEL,1))
		{
			if (getState() == STATE_PUSH)
			{
				if (!freeRange && dsq->game->collideCircleWithGrid(position, hw))
				{
					position = lastPosition;
					collided = true;
					bounce(bounceAmount);
				}
			}
			else
			{			
				if (!freeRange && ((!canLeaveWater && !isUnderWater() && wasUnderWater) || dsq->game->collideCircleWithGrid(position, hw)))
				{
					position = lastPosition;
					onHitWall();
					bounce(bounceAmount);
					collided = true;
				}
			}
		}
	}

	if (collided && friction != 0 && (vel.x != 0 || vel.y != 0))
	{
		Vector fric = vel;
		fric.setLength2D(-friction);
		vel.z = 0;
		vel += fric*dt;
	}

	//doFriction(dt);
	
	if (!collided && weight != 0)
	{
		vel += Vector(0, weight*dt);
	}
	for (int i = 0; i < attachedEntities.size(); i++)
	{
		attachedEntities[i]->position = this->position + attachedEntitiesOffsets[i];
		attachedEntities[i]->rotation = this->rotation;
	}	

	wasUnderWater = underWater;
}