/////////////////////////////////////////////////////////////////////////////////////////////
///////////Gustavob's RC Race////////////////////////////////////////////////////////////////
///////////Made by Gustavob, São Paulo, Brazil///////////////////////////////////////////////
///////////Contact: gustavobittencourtcosta@hotmail.com//////////////////////////////////////
///////////Map started: October 16th, 2009///////////////////////////////////////////////////
///////////First tested: October 17th, 2009//////////////////////////////////////////////////
///////////First multiplayer tested: October 17th, 2009//////////////////////////////////////
///////////First released: October 18th, 2009////////////////////////////////////////////////
///////////Email-me if any issues are found//////////////////////////////////////////////////
///////////Special thanks to [DAFE], CA and Razor/Uee, PL for helping me with this :)////////
///////////Player gets $10000 for each lap made (theres a lap counter on/////////////////////
///////////left of screen for each player). Make it a points game with X ////////////////////
///////////points and 0 time (p1 lap counter can disappear along with game timer)////////////
///////////Multiply the total laps you want by 10000 (ten thousand) and make the result//////
///////////the target points (points to win) e.g. 5 laps * 10000 = 50000 points to win///////
/////////////////////////////////////////////////////////////////////////////////////////////


PLAYER_PED player1 = ( 160.5 , 161.5 , 5.0 ) 8 0
PLAYER_PED player2 = ( 161.5 , 161.5 , 5.0 ) 10 0
PLAYER_PED player3 = ( 162.5 , 161.5 , 5.0 ) 11 0
PLAYER_PED player4 = ( 163.5 , 161.5 , 5.0 ) 7 0


//The cars and hidden car transporters for transporting car wrecks
CAR_DATA ztype1
CAR_DATA ztype2
CAR_DATA ztype3
CAR_DATA ztype4
PARKED_CAR_DATA transporter1 = ( 152.5 , 168.5 , 2.0 ) -1 90 TRUKTRNS
PARKED_CAR_DATA transporter2 = ( 154.5 , 168.5 , 2.0 ) -1 90 TRUKTRNS
PARKED_CAR_DATA transporter3 = ( 156.5 , 168.5 , 2.0 ) -1 90 TRUKTRNS
PARKED_CAR_DATA transporter4 = ( 158.5 , 168.5 , 2.0 ) -1 90 TRUKTRNS

//The remote icons
OBJ_DATA rc1
OBJ_DATA rc2
OBJ_DATA rc3
OBJ_DATA rc4

//Oil slick
GENERATOR oil1 = ( 163.5 , 164.5 , 3.9 ) 0 COLLECT_16 1350 1400 6

LEVELSTART

SWITCH_GENERATOR ( oil1 , ON )

DISPLAY_MESSAGE ( 5504 )

//Create the remote icons
IF ( CHECK_CHARACTER_HEALTH ( player1 , 0 ) )
rc1 = CREATE_OBJ ( 160.5 , 162.5 , 5.0 ) 0 REMOTE END
ENDIF

IF ( CHECK_CHARACTER_HEALTH ( player2 , 0 ) )
rc2 = CREATE_OBJ ( 161.5 , 162.5 , 5.0 ) 0 REMOTE END
ENDIF

IF ( CHECK_CHARACTER_HEALTH ( player3 , 0 ) )
rc3 = CREATE_OBJ ( 162.5 , 162.5 , 5.0 ) 0 REMOTE END
ENDIF

IF ( CHECK_CHARACTER_HEALTH ( player4 , 0 ) )
rc4 = CREATE_OBJ ( 163.5 , 162.5 , 5.0 ) 0 REMOTE END
ENDIF

counter mainloop = 1
counter p1_remote_car_exists = 0
counter p2_remote_car_exists = 0
counter p3_remote_car_exists = 0
counter p4_remote_car_exists = 0
counter p1_lap_counter
counter p2_lap_counter
counter p3_lap_counter
counter p4_lap_counter
counter p1_checkpoint = 0
counter p2_checkpoint = 0
counter p3_checkpoint = 0
counter p4_checkpoint = 0
counter dummy_counter
onscreen_counter p1_lap_counter_display
onscreen_counter p2_lap_counter_display
onscreen_counter p3_lap_counter_display
onscreen_counter p4_lap_counter_display
onscreen_counter dummy_display

ADD_ONSCREEN_COUNTER ( dummy_display , dummy_counter )
CLEAR_ONSCREEN_COUNTER ( dummy_display )

IF ( CHECK_CHARACTER_HEALTH ( player1 , 0 ) )
 ADD_ONSCREEN_COUNTER ( p1_lap_counter_display , p1_lap_counter )
ENDIF

IF ( CHECK_CHARACTER_HEALTH ( player2 , 0 ) )
 ADD_ONSCREEN_COUNTER ( p2_lap_counter_display , p2_lap_counter )
ENDIF

IF ( CHECK_CHARACTER_HEALTH ( player3 , 0 ) )
 ADD_ONSCREEN_COUNTER ( p3_lap_counter_display , p3_lap_counter )
ENDIF

IF ( CHECK_CHARACTER_HEALTH ( player4 , 0 ) )
 ADD_ONSCREEN_COUNTER ( p4_lap_counter_display , p4_lap_counter )
ENDIF

WHILE_EXEC ( mainloop = 1 ) // MAIN LOOP - ALL COMMANDS READ EVERY GAME CYCLE

//Get remote of cars
IF ( p1_remote_car_exists = 0 )
    IF ( CHECK_CHARACTER_HEALTH ( player1 , 0 ) )
        IF ( LOCATE_CHARACTER_ON_FOOT ( player1 , 160.5 , 162.5 , 5.0 , 1.0 , 1.0 ) )
            ztype1 = CREATE_CAR ( 153.5 , 163.5 , 2.0 ) 2 270 VTYPE END
            TAKE_REMOTE_CONTROL_OF_CAR ( player1 , ztype1 )
            SET p1_remote_car_exists = 1
        ENDIF
    ENDIF
ENDIF
 IF ( p1_remote_car_exists > 0 )
    IF ( ( IS_CAR_WRECKED ( ztype1 ) ) OR ( HAS_CAR_JUST_SUNK ( ztype1 ) ) )
        IF ( NOT ( DELAY ( 200 ) ) )  //  INTRODUCES A CYCLE OF THIS EVENT BEING ABLE TO HAPPEN, SORT OF A DELAY
            PUT_CAR_ON_TRAILER ( ztype1 , transporter1 )  //  INSTANTLY TRANSPORT WRECK TO HIDDEN TRANSPORTER TRAILER TO PREVENT SPAWN AND WRECK ISSUES
            DELETE_ITEM ( ztype1 )  //  Cleanup:  Remove remote wreck
            SET p1_remote_car_exists = 0
        ENDIF
    ENDIF
ENDIF

IF ( p2_remote_car_exists = 0 )
    IF ( CHECK_CHARACTER_HEALTH ( player2 , 0 ) )
        IF ( LOCATE_CHARACTER_ON_FOOT ( player2 , 161.5 , 162.5 , 5.0 , 1.0 , 1.0 ) )
            ztype2 = CREATE_CAR ( 153.5 , 164.5 , 2.0 ) 28 270 VTYPE END
            TAKE_REMOTE_CONTROL_OF_CAR ( player2 , ztype2 )
            SET p2_remote_car_exists = 1
        ENDIF
    ENDIF
ENDIF
 IF ( p2_remote_car_exists > 0 )
    IF ( ( IS_CAR_WRECKED ( ztype2 ) ) OR ( HAS_CAR_JUST_SUNK ( ztype2 ) ) )
        IF ( NOT ( DELAY ( 200 ) ) )  //  INTRODUCES A CYCLE OF THIS EVENT BEING ABLE TO HAPPEN, SORT OF A DELAY
            PUT_CAR_ON_TRAILER ( ztype2 , transporter2 )  //  INSTANTLY TRANSPORT WRECK TO HIDDEN TRANSPORTER TRAILER TO PREVENT SPAWN AND WRECK ISSUES
            DELETE_ITEM ( ztype2 )  //  Cleanup:  Remove remote wreck
            SET p2_remote_car_exists = 0
        ENDIF
    ENDIF
ENDIF

IF ( p3_remote_car_exists = 0 )
    IF ( CHECK_CHARACTER_HEALTH ( player3 , 0 ) )
        IF ( LOCATE_CHARACTER_ON_FOOT ( player3 , 162.5 , 162.5 , 5.0 , 1.0 , 1.0 ) )
            ztype3 = CREATE_CAR ( 153.5 , 165.5 , 2.0 ) 5 270 VTYPE END
            TAKE_REMOTE_CONTROL_OF_CAR ( player3 , ztype3 )
            SET p3_remote_car_exists = 1
        ENDIF
    ENDIF
ENDIF
 IF ( p3_remote_car_exists > 0 )
    IF ( ( IS_CAR_WRECKED ( ztype3 ) ) OR ( HAS_CAR_JUST_SUNK ( ztype3 ) ) )
        IF ( NOT ( DELAY ( 200 ) ) )  //  INTRODUCES A CYCLE OF THIS EVENT BEING ABLE TO HAPPEN, SORT OF A DELAY
            PUT_CAR_ON_TRAILER ( ztype3 , transporter3 )  //  INSTANTLY TRANSPORT WRECK TO HIDDEN TRANSPORTER TRAILER TO PREVENT SPAWN AND WRECK ISSUES
            DELETE_ITEM ( ztype3 )  //  Cleanup:  Remove remote wreck
            SET p3_remote_car_exists = 0
        ENDIF
    ENDIF
ENDIF

IF ( p4_remote_car_exists = 0 )
    IF ( CHECK_CHARACTER_HEALTH ( player4 , 0 ) )
        IF ( LOCATE_CHARACTER_ON_FOOT ( player4 , 163.5 , 162.5 , 5.0 , 1.0 , 1.0 ) )
            ztype4 = CREATE_CAR ( 153.5 , 166.5 , 2.0 ) 10 270 VTYPE END
            TAKE_REMOTE_CONTROL_OF_CAR ( player4 , ztype4 )
            SET p4_remote_car_exists = 1
        ENDIF
    ENDIF
ENDIF
 IF ( p4_remote_car_exists > 4 )
    IF ( ( IS_CAR_WRECKED ( ztype4 ) ) OR ( HAS_CAR_JUST_SUNK ( ztype4 ) ) )
        IF ( NOT ( DELAY ( 200 ) ) )  //  INTRODUCES A CYCLE OF THIS EVENT BEING ABLE TO HAPPEN, SORT OF A DELAY
            PUT_CAR_ON_TRAILER ( ztype4 , transporter4 )  //  INSTANTLY TRANSPORT WRECK TO HIDDEN TRANSPORTER TRAILER TO PREVENT SPAWN AND WRECK ISSUES
            DELETE_ITEM ( ztype4 )  //  Cleanup:  Remove remote wreck
            SET p4_remote_car_exists = 0
        ENDIF
    ENDIF
ENDIF

//Count the laps
If ( (check_Character_Health ( player1 , 0 ) ) AND ( p1_remote_car_exists > 0  ) )
    IF ( p1_remote_car_exists > 90 )
        SET p1_remote_car_exists = 90
        IF ( p1_checkpoint = 0 )
            IF ( IS_CAR_IN_BLOCK ( ztype1 , 167.5 , 168.5 , 2.0 , 1.0 , 3.0 ) )
                SET p1_checkpoint = 1
            ENDIF
        ENDIF
        IF ( p1_checkpoint = 1 )
            If ( ( IS_CAR_IN_BLOCK ( ztype1 , 153.9 , 165.0 , 2.0 , 1.0 , 6.0 ) ) OR ( IS_CAR_IN_BLOCK ( ztype1 , 153.9 , 165.0 , 3.0 , 1.0 , 6.0 ) ) )
             ++p1_lap_counter
                ADD_SCORE ( player1 , 10000 )
                SET p1_checkpoint = 0
            ENDIF
        ENDIF
    ENDIF
     ++ p1_remote_car_exists
ENDIF

If ( (check_Character_Health ( player2 , 0 ) ) AND ( p2_remote_car_exists > 0  ) )
    IF ( p2_remote_car_exists > 90 )
        SET p2_remote_car_exists = 90
        IF ( p2_checkpoint = 0 )
            IF ( IS_CAR_IN_BLOCK ( ztype2 , 167.5 , 168.5 , 2.0 , 1.0 , 3.0 ) )
                SET p2_checkpoint = 1
            ENDIF
        ENDIF
        IF ( p2_checkpoint = 1 )
            If ( ( IS_CAR_IN_BLOCK ( ztype2 , 153.9 , 165.0 , 2.0 , 1.0 , 6.0 ) ) OR ( IS_CAR_IN_BLOCK ( ztype2 , 153.9 , 165.0 , 3.0 , 1.0 , 6.0 ) ) )
             ++p2_lap_counter
                ADD_SCORE ( player2 , 10000 )
                SET p2_checkpoint = 0
            ENDIF
        ENDIF
    ENDIF
     ++ p2_remote_car_exists
ENDIF

If ( (check_Character_Health ( player3 , 0 ) ) AND ( p3_remote_car_exists > 0  ) )
    IF ( p3_remote_car_exists > 90 )
        SET p3_remote_car_exists = 90
        IF ( p3_checkpoint = 0 )
            IF ( IS_CAR_IN_BLOCK ( ztype3 , 167.5 , 168.5 , 2.0 , 1.0 , 3.0 ) )
                SET p3_checkpoint = 1
            ENDIF
        ENDIF
        IF ( p3_checkpoint = 1 )
            If ( ( IS_CAR_IN_BLOCK ( ztype3 , 153.9 , 165.0 , 2.0 , 1.0 , 6.0 ) ) OR ( IS_CAR_IN_BLOCK ( ztype3 , 153.9 , 165.0 , 3.0 , 1.0 , 6.0 ) ) )
             ++p3_lap_counter
                ADD_SCORE ( player3 , 10000 )
                SET p3_checkpoint = 0
            ENDIF
        ENDIF
    ENDIF
     ++ p3_remote_car_exists
ENDIF

If ( (check_Character_Health ( player4 , 0 ) ) AND ( p4_remote_car_exists > 0  ) )
    IF ( p4_remote_car_exists > 90 )
        SET p4_remote_car_exists = 90
        IF ( p4_checkpoint = 0 )
            IF ( IS_CAR_IN_BLOCK ( ztype4 , 167.5 , 168.5 , 2.0 , 1.0 , 3.0 ) )
                SET p4_checkpoint = 1
            ENDIF
        ENDIF
        IF ( p4_checkpoint = 1 )
            If ( ( IS_CAR_IN_BLOCK ( ztype4 , 153.9 , 165.0 , 2.0 , 1.0 , 6.0 ) ) OR ( IS_CAR_IN_BLOCK ( ztype4 , 153.9 , 165.0 , 3.0 , 1.0 , 6.0 ) ) )
             ++p4_lap_counter
                ADD_SCORE ( player4 , 10000 )
                SET p4_checkpoint = 0
            ENDIF
        ENDIF
    ENDIF
     ++ p4_remote_car_exists
ENDIF

ENDWHILE // END MAIN LOOP that keeps looping while multiplayer game continues to run

LEVELEND
