#define  COMMON_BALL_H aaa

//------------------------------------------------------------------------------------------
SetBallVidsSpeed(int iBallSpeed)
{
	SetVidData(500, VID_SPEED, iBallSpeed);
	SetVidData(501, VID_SPEED, iBallSpeed);
	SetVidData(502, VID_SPEED, iBallSpeed);
	SetVidData(503, VID_SPEED, iBallSpeed);
	SetVidData(504, VID_SPEED, iBallSpeed);
	//
	int i;
	for ( i = FirstUnit(U_UNIT); i; i = NextUnit() )
	{
		if ( GetVidData(GetUnitVid(i), VID_CLASS) == B_BALL )
			Action(i, ACT_SET_MAX_SPEED, iBallSpeed);
	}
}


//------------------------------------------------------------------------------------------
F500_14(int unit)//на рождение нового шарика
{
    NoBall++;
//    Action(unit, ACT_ADD_ITEM, 0);// no_otskok
//    Action(unit, ACT_ADD_ITEM, NORMAL_BALL_SPEED);// max_speed
}
F501_14(int unit) { F500_14(unit); }
F502_14(int unit) { F500_14(unit); }
F503_14(int unit) { F500_14(unit); }
F504_14(int unit) { F500_14(unit); }

F500_15(int unit)//на смерть шарика
{
    if ( GameState == GAMESTATE_WIN && GetAnimation(unit) == ANI_DEATH )
        Action(unit, ANI_DEATH2);
    else
        NoBall--;
}
F500_16(int unit) { F500_15(unit); }
F501_15(int unit) { F500_15(unit); }
F501_16(int unit) { F500_15(unit); }
F502_15(int unit) { F500_15(unit); }
F502_16(int unit) { F500_15(unit); }
F503_15(int unit) { F500_15(unit); }
F503_16(int unit) { F500_15(unit); }
F504_15(int unit) { F500_15(unit); }
F504_16(int unit) { F500_15(unit); }

static int LastCollisionTime = 0;// время последнего столкновения с ракеткой или предметом
// вызывается при столкновении шарика с стенками
F500_11(int unit)
{
/*    int diff_speed, no_otskok, max_speed;
    no_otskok = Action(unit, ACT_GET_ITEM, 0);
    max_speed = Action(unit, ACT_GET_ITEM, 1);
    diff_speed = (++no_otskok * INC_BALL_SPEED) / 100;
//Log(itoa(no_otskok) + " "+itoa(max_speed) + " "+itoa(diff_speed));
    if ( diff_speed > 3 )
    {
        no_otskok = 0;
        if ( (max_speed + diff_speed) <= MAX_BALL_SPEED)
            max_speed += diff_speed;
        else
            max_speed = MAX_BALL_SPEED;
        Action(unit, ACT_SET_MAX_SPEED, max_speed);
    }
    Action(unit, ACT_DELETE_ALL_ITEM);
    Action(unit, ACT_ADD_ITEM, no_otskok);
    Action(unit, ACT_ADD_ITEM, max_speed);*/
    if ( (GetTime() - LastCollisionTime) < 50 )
        return;
    int diff_speed;
    int inc_ball_speed = INC_BALL_SPEED;
    if ( LevelNumber >= 5 )
        inc_ball_speed = INC_BALL_SPEED + ((INC_BALL_SPEED_9LEVEL - INC_BALL_SPEED) * (LevelNumber - 4))/5;
    if ( NoBall > 0 )
        diff_speed = (++NoOtskok * inc_ball_speed) / 100 / NoBall;
    else
        diff_speed = (++NoOtskok * inc_ball_speed) / 100;
    if ( diff_speed > 3 )
    {
        NoOtskok = 0;
        if ( (BallSpeed + diff_speed) <= MAX_BALL_SPEED)
            BallSpeed += diff_speed;
        else
            BallSpeed = MAX_BALL_SPEED;
    }
    LastCollisionTime = GetTime();
}
F501_11(int unit) { F500_11(unit); }
F502_11(int unit) { F500_11(unit); }
F503_11(int unit) { F500_11(unit); }
F504_11(int unit) { F500_11(unit); }
// вызывается при столкновении шарика с объектами
F500_12(int unit)
{
/*    int diff_speed, no_otskok, max_speed;
    no_otskok = Action(unit, ACT_GET_ITEM, 0);
    max_speed = Action(unit, ACT_GET_ITEM, 1);
    diff_speed = (++no_otskok * INC_BALL_SPEED) / 100;
//Log(itoa(no_otskok) + " "+itoa(max_speed) + " "+itoa(diff_speed));
    if ( diff_speed > 3 )
    {
        no_otskok = 0;
        if ( (max_speed + diff_speed) <= MAX_BALL_SPEED)
            max_speed += diff_speed;
        else
            max_speed = MAX_BALL_SPEED;
        Action(unit, ACT_SET_MAX_SPEED, max_speed);
    }
    Action(unit, ACT_DELETE_ALL_ITEM);
    Action(unit, ACT_ADD_ITEM, no_otskok);
    Action(unit, ACT_ADD_ITEM, max_speed);*/
    if ( (GetTime() - LastCollisionTime) < 50 )
        return;
    int diff_speed;
    int inc_ball_speed = INC_BALL_SPEED;
    if ( LevelNumber >= 5 )
        inc_ball_speed = INC_BALL_SPEED + ((INC_BALL_SPEED_9LEVEL - INC_BALL_SPEED) * (LevelNumber - 4))/5;
    diff_speed = (++NoOtskok * inc_ball_speed) / 100 / NoBall;
    if ( diff_speed > 3 )
    {
        NoOtskok = 0;
        if ( (BallSpeed + diff_speed) <= MAX_BALL_SPEED)
            BallSpeed += diff_speed;
        else
            BallSpeed = MAX_BALL_SPEED;
    }
    LastCollisionTime = GetTime();
}
F501_12(int unit) { F500_12(unit); }
F502_12(int unit) { F500_12(unit); }
F503_12(int unit) { F500_12(unit); }
F504_12(int unit) { F500_12(unit); }

// если шарик ударил тележку игрока
F500_9(int unit)
{
//    LastCollisionTime = GetTime();
// прилипаем если тележка липкая и шарик еще не прилеплен
    if ( VehiIsSticky && BallInVehi < 10 && !Action(unit, ACT_HAVE_ITEM, -999999) 
    	&& !(GetInputState()&(INPUT_LCLICK | INPUT_LDOWN)) && PlayerVehi)
    {
        BallInVehi++;
        Log2("BallInVehi = " + itoa(BallInVehi));
        //
        int shift_x, shield;
        shield  = Action(PlayerVehi, ACT_GET_LINK);
        shift_x = ((GetX(unit) - GetX(shield)) * 200) / GetVidData(GetUnitVid(shield), VID_SIZE_X);
        // выставим позицию шарика на тележке -100, 100
        Action(unit, ACT_ADD_ITEM, shift_x);
        Action(unit, ACT_ADD_ITEM, -999999);
        Action(unit, ACT_STOP, 1);
    }
}
F501_9(int unit) { F500_9(unit); }
F502_9(int unit) { F500_9(unit); }
F503_9(int unit) { F500_9(unit); }
F504_9(int unit) { F500_9(unit); }
/*
// делаем снова видимым шарик после того как превращение одного вида шарика в др закончено
F521_DESTROY(int unit)
{
//    int uplink;
//    for ( uplink = Action(unit, ACT_GET_UPLINK); Action(uplink, ACT_GET_UPLINK); uplink = Action(uplink, ACT_GET_UPLINK) );
    int i;
    for ( i = FirstUnit(U_UNIT); i; i = NextUnit() )
        if ( GetVidData(GetUnitVid(i), VID_CLASS) == B_BALL )//&& GetX(i) == GetX(unit) && GetY(i) == (GetY(unit) - 5) && GetZ(i) == (GetZ(unit) - 5) )
        {
            Action(i, ACT_SET_INVISIBLE, 0);
//            break;
        }
}
F522_DESTROY(int unit) { F521_DESTROY(unit); }
F523_DESTROY(int unit) { F521_DESTROY(unit); }
F524_DESTROY(int unit) { F521_DESTROY(unit); }
F525_DESTROY(int unit) { F521_DESTROY(unit); }
F526_DESTROY(int unit) { F521_DESTROY(unit); }
F527_DESTROY(int unit) { F521_DESTROY(unit); }
F528_DESTROY(int unit) { F521_DESTROY(unit); }
F529_DESTROY(int unit) { F521_DESTROY(unit); }
F530_DESTROY(int unit) { F521_DESTROY(unit); }
F531_DESTROY(int unit) { F521_DESTROY(unit); }
F532_DESTROY(int unit) { F521_DESTROY(unit); }
F533_DESTROY(int unit) { F521_DESTROY(unit); }
F534_DESTROY(int unit) { F521_DESTROY(unit); }
F535_DESTROY(int unit) { F521_DESTROY(unit); }
F536_DESTROY(int unit) { F521_DESTROY(unit); }
F537_DESTROY(int unit) { F521_DESTROY(unit); }
F538_DESTROY(int unit) { F521_DESTROY(unit); }
F539_DESTROY(int unit) { F521_DESTROY(unit); }
F540_DESTROY(int unit) { F521_DESTROY(unit); }
*/