{$CLEO .cs}

/*
    This is a template showing how to write a plugin script for the 
    Camping Mobile Save House 2.0 (CMSH) mod. If in MobileSaveHouse.ini 
    the option enablePlugins is set to 1, then the HANDLE of the 
    mobile save house (Journey, Tropic, Utilitr1, or Extra) is written 
    to the global cleo variable (cleoVarStartID - 1). For example, if 
    cleoVarStartID=925, then you can get the handle with 
    "0AB4: CAR_HANDLE = var 924".  Other scripts (plugins) like this one
    can read this global cleo variable and provide additional 
    functions to the mobile save house. 
    
    A plugin can even disable the save disc by setting the cleo variable that 
    holds the car handle to -1. This is useful if you want CJ to walk
    in the area where usually the save disc is displayed but you don't want
    to activate the save screen. To enable the save disc again set the cleo 
    variable to 0. CMSH will continue execution and update the cleo variable
    again.

    If you publish your plugin, do not include the Camping Mobile Save House
    mod in your download. It must be downloaded separately. Do not remove
    this text.
	
	Here is a tutorial on CLEO scripting: 
	http://gtaforums.com/topic/403594-sa-cleo-script-tutorial/

    Enjoy!     
    2015.09.08, GoodIdea82
*/

const
    iniFileName     = "CLEO\MobileSaveHouse.ini"

    CAR_HANDLE      = 0@
    CLEO_VAR_ID        = 1@   
    CARX            = 2@
    CARY            = 3@
    CARZ            = 4@
    SAVE_DISC_DISABLED = 5@  //1 if we have disabled the save disc. In this case we have to reenable the save disc again.
    MSG_DISPLAYED   = 6@  //Make sure that the message is displyed only once.
    TMP_INT         = 7@ 
    TMPX            = 8@ 
    TMPY            = 9@
    TMPZ            = 10@
    TMP_HANDLE      = 11@
end

var
    CAR_HANDLE      : Integer
    CLEO_VAR_ID     : Integer
    CARX            : Float
    CARY            : Float
    CARZ            : Float 
    SAVE_DISC_DISABLED : Integer 
    MSG_DISPLAYED   : Integer   
    TMP_INT         : Integer
    TMPX            : Float 
    TMPY            : Float
    TMPZ            : Float
    TMP_HANDLE      : Integer
end

//------------------------------------------------------
// MAIN TEMPLATE 
//------------------------------------------------------

03A4: name_thread 'MSH_PLG'
wait 1000 //allow urgent scripts to run first
0662: write_debug_message "MobileSaveHouse PlugIn"
gosub @INIT_OR_EXIT

while true
    wait 5

    0AB4: CAR_HANDLE = var CLEO_VAR_ID
    if and
        Player.Defined($PLAYER_CHAR) //0256
        not CAR_HANDLE == 0
        056E: car CAR_HANDLE defined
    then          
        gosub @DO_SOMETHING_INTERESTING
    else
        wait 100 //be ressource friendly since no car handle is defined.
    end
end

//------------------------------------------------------
:INIT_OR_EXIT
    if  and       
        0AF0: TMP_INT = get_int_from_ini_file iniFileName section "general" key "enablePlugins" //IF and SET
        TMP_INT > 0 
    then
        wait 0
    else
        0A93: end_custom_thread
    end
    
    if         
        0AF0: CLEO_VAR_ID = get_int_from_ini_file iniFileName section "general" key "cleoVarStartID" //IF and SET
    then
        CLEO_VAR_ID -= 1
    else
        0AD1: show_formatted_text_highpriority "Error: in MobileSaveHouse.ini, cleoVarStartID is not defined." time 3000 
        wait 3000
        0A93: end_custom_thread
    end
return

//------------------------------------------------------
// END OF MAIN TEMPLATE. 
// Customize the rest as you like, but try to follow the approach how messages are displayed.
//------------------------------------------------------

:DO_SOMETHING_INTERESTING
        Car.StorePos(CAR_HANDLE, CARX, CARY, CARZ) //00AA
        if        
            00FF: actor $PLAYER_ACTOR sphere 1 in_sphere CARX CARY CARZ radius 3.0 3.0 3.0 on_foot
        then
        
            //Display a message to the user
            if  and
                //These checks are important, because other plugins may also want to display messages. Without such checks there will be chaos.
                876F: not text_priority_displayed 
                88FE: not text_box_displayed
                MSG_DISPLAYED == 0
            then
                0AD1: show_formatted_text_highpriority "Press 3 to spawn a bike. Press 4 to disable/enable the savedisc." time 3000
                MSG_DISPLAYED = 1            
            end
            
            //Check if a key was pressed.
            if
                0AB0:  key_pressed 51 //51=vk_3
            then
                gosub @SPAWN_BIKE
                while 0AB0:  key_pressed 51  //the player perhaps still holds the key.
                    wait 0
                end
                return // after a wait statement CAR_HANDLE may be invalid (undefined)
            end
            
            if
                0AB0:  key_pressed 52 //51=vk_4
            then
                0AB3: var CLEO_VAR_ID = -1
                SAVE_DISC_DISABLED == 1
                wait 200 // Give the mobile save house time to disable the save disc                            
                while 0AB0:  key_pressed 52  //the player perhaps still holds the key.
                    wait 0
                end
                // after a "wait" statement CAR_HANDLE may be invalid (undefined) 
                gosub @DO_SOMETHING_WHEN_SAVEDISC_IS_DISABLED
                return
            end
            
        else
            MSG_DISPLAYED = 0    
        end           
return

//------------------------------------------------------
:SPAWN_BIKE
    0407: store_coords_to TMPX TMPY TMPZ from_car CAR_HANDLE with_offset 4.0 0.0 0.0  //offset for the pickup. Here: in the back of the mobile home
    
    while not Model.Available(#BMX)  
        wait 50
        0247: load_model #BMX   
        038B: load_requested_models 
    end
    
    TMP_HANDLE = Car.Create(#BMX, TMPX, TMPY, TMPZ)
    01C3: remove_references_to_car TMP_HANDLE  //Makes it like a random object that may be deleted by the engine.
return

//------------------------------------------------------
:DO_SOMETHING_WHEN_SAVEDISC_IS_DISABLED
    while  true 
        wait 10    
        // after a "wait" statement CAR_HANDLE may be invalid (undefined)
        if or  //warning there is some maximum number of conditions
            856E: not car CAR_HANDLE defined
            0119: car CAR_HANDLE wrecked
            80FF: not actor $PLAYER_ACTOR sphere 0 in_sphere CARX CARY CARZ radius 15.0 15.0 15.0 on_foot
            not Player.Defined($PLAYER_CHAR) //8256
            Actor.Driving($PLAYER_ACTOR)            
            0AB0:  key_pressed 52 //51=vk_4
        then
            Break //exit the loop
        end
    end

    0AB3: var CLEO_VAR_ID = 0 //Setting it to 0 tells the mobile save house mod to continue its execution. The cleo variable will be updated with the car handle again.
    SAVE_DISC_DISABLED = 0
    wait 500 // Give the mobile save house time to enable the save disc 

return
//------------------------------------------------------
