Advertisement
Guest User

[GTA V] Advanced Exit Vehicle

a guest
May 20th, 2015
807
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.60 KB | None | 0 0
  1. -- GTA V Lua Script
  2. --
  3. -- === MAIN ===
  4. --
  5. -- Advanced Exit Vehicle
  6. -- Author: Baaa
  7. -- ver. 1.1 (05.20.2015)
  8. --
  9. -- === DESCRIPTION ===
  10. --
  11. -- This script allows you to leave the vehicle...
  12. -- 1. ...through a passenger door.
  13. -- 2. ...with engine on and the door opened and the radio working
  14. -- 3. ...usual way (full exit), i.e. close the door and engine off.
  15. -- 4. ...with alarm set (turns off when the player is getting in the car)
  16. -- 5. ...with doors locked (for all except player). Security is important. Don't give your car to those pedestrian theves! :D
  17. -- 6. ...with anchor set if the vehicle is a boat. So your boat will never float away. (note: seems not works properly for now)
  18. --
  19. -- What the purpose of this script?
  20. -- To make just simple exit vehicle with few features not realized in SP GTA V and without any 'useless' stuff.
  21. -- Not all of the ppl want to use big plugins with tons of features. So this is simple as shit.
  22. --
  23. -- What the difference from similar plugins?
  24. -- Better method of getting and blocking the 'exit vehicle' event. Optimized code. Better perfomance. Some other features.
  25. --
  26. -- Supports gamepad.
  27. --
  28. -- === CONTROLS ===
  29. --
  30. -- 'exit vehicle' key           --  Exit vehicle with engine on and the driver door opened.
  31. --
  32. -- Left Shift + 'exit vehicle'  --  Exit vehicle through a passenger door with engine on and the door opened.
  33. -- LB + 'exit vehicle'          --  Exit vehicle through a passenger door with engine on and the door opened.
  34. -- RB + 'exit vehicle'          --  Shuffle to a passenger seat
  35. --
  36. -- Long press makes "full exit", i.e. turns off engine and closes the door (in both cases)
  37. --
  38. -- All keys customizable. See below.
  39. --
  40. -- === VARIABLES ===
  41. -- You can configure this.
  42. --
  43. -- Additional key to:
  44. -- exit
  45. local kb_key        =   16                  --  Keyboard key to exit vehicle through a passenger door. Def. 16 (Shiftkey).
  46. local gpad_key      =   69                  --  Gamepad key to exit vehicle through a passenger door. Def. 69 (RB).
  47. -- shuffle to next seat:
  48. local shuf_kb_key   =   17                  --  Keyboard key to move to next seat. Def. 17 (ControlKey).
  49. local shuf_gpad_key =   68                  --  Gamepad key to move to next seat. Def. 68 (LB).
  50. --
  51. -- Vars
  52. local alarm         =   true                --  Set alarm on the car after "full exit" or not. Def. true
  53. local lockdoors     =   4                   --  Lock the vehicle doors after "full exit" or not. See the flags below. Def. 4
  54. -- the flags you can change:
  55. -- 1 - stay unlocked
  56. -- 2 - full lock
  57. -- 4 - lock but can break (steal the car)
  58.  
  59. local totallock     =   false               --  Locks the doors even for player. Def. false
  60. -- If you wanna check is the vehicle locks or not after "full exit" set it to true.
  61. -- So the vehicle will be locked even for you. And you'll be not able to open the doors (if lockdoors set to 2).
  62.  
  63. local anchor        =   true                --  Put the boat on the anchor after "full exit" or not. Def. true
  64. -- Seems not works properly.
  65.  
  66. -- The text for each event
  67. local alarm_text = "Alarm is set"           --  Def. "Alarm is set"
  68. local lockdoors_text = "Doors are locked"   --  Def. "Doors are locked"
  69. local anchor_text = "Anchor is dropped"     --  Def. "Anchor is dropped"
  70.  
  71. local unalarm_text = "Alarm is off"         --  Def. "Alarm is off"
  72. local unlockdoors_text = "Doors are open"   --  Def. "Doors are open"
  73. local unanchor_text = "Anchor is rised"     --  Def. "Anchor is rised"
  74.  
  75. -- === INSTALLATION ===
  76. --
  77. -- You'll need this:
  78. -- 1. Script Hook V. http://www.dev-c.com/gtav/scripthookv
  79. -- 2. LUA Plugin for Script Hook V. http://gtaforums.com/topic/789139-vrelhook-lua-plugin-for-script-hook-v
  80. --
  81. -- Put the script here: %GAME_DIRECTORY%\scripts\addins\
  82. --
  83. -- === CHANGELOG ===
  84. --
  85. -- 1.1 - Some optimizations.
  86. --     - Added features: setting alarm, locking the doors, putting the boat on anchor, shuffle to the next seat.
  87. --
  88. -- 1.0 - Initial release.
  89. --
  90. -- === NOTES ===
  91. --
  92. -- You are FREE to copy, translate (rewrite in another language),
  93. -- or distribute this code in any way, shape, or form!
  94. ------------------------------------------------------------------
  95. -- TODO:
  96. -- * Make wheels to stay turned
  97. --
  98. --
  99. -------------------------------------------------------------------
  100. -- DO NOT EDIT BELOW IF YOU REALLY DON'T KNOW WHAT ARE YOU DOING --
  101. -------------------------------------------------------------------
  102.  
  103. local aev = {}
  104.  
  105. local ver = '1.1'
  106. local thedate = '05.20.2015'
  107.  
  108. local pid = 0
  109. local pvid = 0
  110. local timer = 0
  111. local maxtimer = 10                     --  How long (ticks) to hold 'exit vehicle' button
  112. local msgtimer = 100
  113. local maxmsgtimer = 100
  114.  
  115. local event_exit = false
  116. local key_pressed = false
  117. local add_key = false
  118.  
  119. local vehicles = {}
  120.  
  121. local showmsg_anchor = 0
  122. local showmsg_alarm = 0
  123. local showmsg_lock = 0
  124.  
  125. local function getpvid(vid, array)
  126.     for i, data in ipairs(array) do
  127.         if(vid == data) then
  128.             return i
  129.         end
  130.     end
  131.     return -1
  132. end
  133.  
  134. local function removenonexistv(array)
  135.     for i, data in ipairs(array) do
  136.         if(ENTITY.GET_ENTITY_MODEL(data) <= 0) then
  137.             table.remove(array, i)
  138.         end
  139.     end
  140. end
  141.  
  142. local function iscar(vid)
  143.     if (VEHICLE.IS_THIS_MODEL_A_CAR(ENTITY.GET_ENTITY_MODEL(vid))) then
  144.         return true
  145.     end
  146.     return false
  147. end
  148.  
  149. local function isboat(vid)
  150.     if (VEHICLE.IS_THIS_MODEL_A_BOAT(ENTITY.GET_ENTITY_MODEL(vid))) then
  151.         return true
  152.     end
  153.     return false
  154. end
  155.  
  156. function aev.drawmsg(msg, x, y, r, g, b)
  157.         UI.SET_TEXT_FONT(7)
  158.         UI.SET_TEXT_SCALE(0.5, 0.6)
  159.         UI.SET_TEXT_COLOUR(r, g, b, 255)
  160.         UI.SET_TEXT_WRAP(0.0, 1.0)
  161.         UI.SET_TEXT_CENTRE(true)
  162.         UI.SET_TEXT_DROPSHADOW(2, 2, 0, 0, 0)
  163.         UI.SET_TEXT_EDGE(1, 0, 0, 0, 205)
  164.         UI._SET_TEXT_ENTRY("STRING")
  165.         UI._ADD_TEXT_COMPONENT_STRING(msg)
  166.         UI._DRAW_TEXT(y, x)
  167. end
  168.  
  169. function aev.tick()
  170.     -------------------------------------------------
  171.     -- DRAW TEXT
  172.     -------------------------------------------------
  173.     if (msgtimer < maxmsgtimer) then
  174.        
  175.         msgtimer = msgtimer + 1
  176.        
  177.         if ((msgtimer >= maxmsgtimer)) then
  178.             msgtimer = maxmsgtimer
  179.         end
  180.        
  181.         if (showmsg_anchor == 1) then
  182.             aev.drawmsg(anchor_text, 0.9, 0.5, 255, 55, 55)
  183.             return 0
  184.         elseif (showmsg_anchor == 2) then
  185.             aev.drawmsg(unanchor_text, 0.9, 0.5, 55, 255, 55)
  186.             return 0
  187.         end
  188.        
  189.         if (showmsg_alarm == 1) then
  190.             aev.drawmsg(alarm_text, 0.9, 0.5, 255, 55, 55)
  191.         elseif (showmsg_alarm == 2) then
  192.             aev.drawmsg(unalarm_text, 0.9, 0.5, 55, 255, 55)
  193.         end
  194.        
  195.         if (showmsg_lock == 1) then
  196.             aev.drawmsg(lockdoors_text, 0.945, 0.5, 255, 55, 55)
  197.         elseif (showmsg_lock == 2) then
  198.             aev.drawmsg(unlockdoors_text, 0.945, 0.5, 55, 255, 55)
  199.         end
  200.     end
  201.     -------------------------------------------------
  202.     -- DRAW TEXT
  203.     -------------------------------------------------
  204.    
  205.     -- Check the key pressed.
  206.     -- Here I should use IS_CONTROL_JUST_PRESSED but it seems IS_CONTROL_PRESSED hooks the key much faster.
  207.     -- And I noticed there is a chance that IS_CONTROL_JUST_PRESSED could not trigger.
  208.     -- Also we need to check IS_DISABLED_CONTROL_PRESSED once.
  209.    
  210.     if (CONTROLS.IS_CONTROL_PRESSED(2, 75) or CONTROLS.IS_DISABLED_CONTROL_PRESSED(2, 75)) then
  211.        
  212.         if (event_exit) then
  213.            
  214.             -------------------------------------------------------
  215.             -- Start timer and check every tick is the key released
  216.             timer = timer + 1
  217.            
  218.             --print(timer)
  219.            
  220.             if (timer < maxtimer) then
  221.                 return 0
  222.             end
  223.             -------------------------------------------------------
  224.            
  225.             -- Key is still pressed
  226.             key_pressed = true
  227.            
  228.             aev.exitv(true, add_key)
  229.            
  230.             return 0
  231.         end
  232.        
  233.         -- Prevent extra exit event
  234.         if (key_pressed) then
  235.             return 0
  236.         end
  237.        
  238.         -- We need just one key press
  239.         key_pressed = true
  240.        
  241.         pid = PLAYER.PLAYER_PED_ID()
  242.        
  243.         -- CURRENT VEHICLE
  244.         pvid = PED.GET_VEHICLE_PED_IS_IN(pid, false)
  245.        
  246.         -- Is player in the vehicle already
  247.         if (pvid <= 0) then
  248.            
  249.             -- 1 TIME TASK --
  250.             -----------------------------------------
  251.             pvid = PED.GET_VEHICLE_PED_IS_USING(pid)
  252.            
  253.             -- Is player interact with a vehicle now
  254.             if (pvid <= 0) then
  255.                 return 0
  256.             end
  257.             -----------------------------------------
  258.             -- Clear DB from nonexistent vehicles
  259.             removenonexistv(vehicles)
  260.             -----------------------------------------------------
  261.             -- Is it a vehicle we did "full exit" once. "Clear" that vehicle.
  262.             if (#vehicles > 0) then
  263.                 local vid = getpvid(pvid, vehicles)
  264.                 if (vid > 0) then
  265.                    
  266.                     -- Is the vehicle a boat
  267.                     if (isboat(pvid)) then
  268.                        
  269.                         if (anchor) then
  270.                             VEHICLE.SET_BOAT_ANCHOR(pvid, false)
  271.                            
  272.                             showmsg_anchor = 2
  273.                             msgtimer = 0
  274.                         end
  275.                        
  276.                         table.remove(vehicles, vid)
  277.                        
  278.                         return 0
  279.                     end
  280.                    
  281.                     showmsg_anchor = 0
  282.                    
  283.                     -- Remove vehicle from db
  284.                     table.remove(vehicles, vid)
  285.                    
  286.                     if (totallock) then
  287.                         return 0
  288.                     end
  289.                    
  290.                     if (alarm) then
  291.                         VEHICLE.SET_VEHICLE_ALARM(pvid, false)
  292.                        
  293.                         showmsg_alarm = 2
  294.                         msgtimer = 0
  295.                     end
  296.                    
  297.                     if (lockdoors > 1) then
  298.                         VEHICLE.SET_VEHICLE_DOORS_LOCKED(pvid, 1)
  299.                        
  300.                         showmsg_lock = 2
  301.                         msgtimer = 0
  302.                     end
  303.                    
  304.                     --VEHICLE.STEER_UNLOCK_BIAS(pvid, true)             --      unlock steer if i'll find the way to lock it
  305.                 end
  306.             end
  307.             -----------------------------------------------------
  308.             return 0
  309.         end
  310.        
  311.         -- We need to check additional key as fast as possible.
  312.         -----------------------------------------------------
  313.         if (get_key_pressed(shuf_kb_key) or CONTROLS.IS_CONTROL_PRESSED(2, shuf_gpad_key)) then
  314.             CONTROLS.DISABLE_CONTROL_ACTION(2, 75, 0)
  315.             AI.TASK_SHUFFLE_TO_NEXT_VEHICLE_SEAT(pid, pvid)
  316.             return 0
  317.         end
  318.        
  319.         if (get_key_pressed(kb_key) or CONTROLS.IS_CONTROL_PRESSED(2, gpad_key)) then
  320.             add_key = true
  321.         end
  322.         -----------------------------------------------------
  323.        
  324.         -- Is player a driver
  325.         if (VEHICLE.GET_PED_IN_VEHICLE_SEAT(pvid, -1) ~= pid) then
  326.             add_key = false
  327.             return 0
  328.         end
  329.        
  330.         -- Blocking 'exit vehicle' action
  331.         -- Note: it blocks action just once.
  332.         CONTROLS.DISABLE_CONTROL_ACTION(2, 75, 0)
  333.        
  334.         -- Reset the timer just in case
  335.         timer = 0
  336.        
  337.         -- Start exit event
  338.         event_exit = true
  339.        
  340.         return 0
  341.     end
  342.    
  343.     if (key_pressed) then
  344.         key_pressed = not key_pressed
  345.         return 0
  346.     end
  347.    
  348.     -------------------------
  349.     -- THE KEY IS RELEASED --
  350.     -------------------------
  351.    
  352.     if (not event_exit) then
  353.         return 0
  354.     end
  355.    
  356.     aev.exitv(false, add_key)
  357.    
  358.     pvid = PED.GET_VEHICLE_PED_IS_IN(pid, true)
  359.    
  360.     if (lockdoors > 1) then
  361.         VEHICLE.SET_VEHICLE_DOORS_LOCKED(pvid, 1)
  362.        
  363.         removenonexistv(vehicles)
  364.        
  365.         if (#vehicles > 0) then
  366.             local vid = getpvid(pvid, vehicles)
  367.             if (vid > 0) then
  368.                 table.remove(vehicles, vid)
  369.             end
  370.         end
  371.        
  372.         return 0
  373.     end
  374.    
  375.     if (anchor or alarm) then
  376.        
  377.         removenonexistv(vehicles)
  378.        
  379.         if (#vehicles > 0) then
  380.             pvid = PED.GET_VEHICLE_PED_IS_IN(pid, true)
  381.             local vid = getpvid(pvid, vehicles)
  382.             if (vid > 0) then
  383.                 table.remove(vehicles, vid)
  384.             end
  385.         end
  386.     end
  387. end
  388.  
  389. function aev.exitv(pressed, addkey)
  390.     event_exit = false
  391.     timer = 0
  392.    
  393.     -- Is additional key pressed
  394.     if (addkey) then
  395.         add_key = false
  396.         if (pressed) then
  397.            
  398.             AI.TASK_LEAVE_VEHICLE(pid, pvid, 262144)
  399.            
  400.             --------------------------------------------
  401.             -- ADD LAST CAR TO A DB
  402.            
  403.             -- Last used vehicle
  404.             pvid = PED.GET_VEHICLE_PED_IS_IN(pid, true)
  405.            
  406.             local isaboat = isboat(pvid)
  407.            
  408.             if (not iscar(pvid)) then
  409.                 if (not isaboat) then
  410.                     return 0
  411.                 end
  412.             end
  413.            
  414.             if (anchor or alarm or (lockdoors > 1)) then
  415.                
  416.                 removenonexistv(vehicles)
  417.                
  418.                 if (getpvid(pvid, vehicles) <= 0) then
  419.                     table.insert(vehicles, pvid)
  420.                 end
  421.             end
  422.             -------------------------------------------
  423.            
  424.             if (isaboat) then
  425.                 if (anchor) then
  426.                     VEHICLE.SET_BOAT_ANCHOR(pvid, true)
  427.                    
  428.                     showmsg_anchor = 1
  429.                     msgtimer = 0
  430.                 end
  431.                 return 0
  432.             end
  433.            
  434.             showmsg_anchor = 0
  435.            
  436.             if (alarm) then
  437.                 VEHICLE.SET_VEHICLE_ALARM(pvid, true)
  438.                
  439.                 showmsg_alarm = 1
  440.                 msgtimer = 0
  441.             end
  442.            
  443.             if (lockdoors > 1) then
  444.                 VEHICLE.SET_VEHICLE_DOORS_LOCKED(pvid, lockdoors)
  445.                
  446.                 showmsg_lock = 1
  447.                 msgtimer = 0
  448.             end
  449.            
  450.             return 0
  451.         end
  452.        
  453.         AI.TASK_LEAVE_VEHICLE(pid, pvid, 262400)
  454.        
  455.         -- Make engine on
  456.         while (pvid ~= 0) do
  457.             VEHICLE.SET_VEHICLE_ENGINE_ON(pvid, true, true)
  458.             wait(0)
  459.             pvid = PED.GET_VEHICLE_PED_IS_IN(pid, false)
  460.         end
  461.        
  462.         return 0
  463.     end
  464.    
  465.     if (pressed) then
  466.        
  467.         AI.TASK_LEAVE_VEHICLE(pid, pvid, 0)
  468.        
  469.         --------------------------------------------
  470.         -- ADD LAST CAR TO A DB
  471.        
  472.         pvid = PED.GET_VEHICLE_PED_IS_IN(pid, true)
  473.        
  474.         local isaboat = isboat(pvid)
  475.        
  476.         if (not iscar(pvid)) then
  477.             if (not isaboat) then
  478.                 return 0
  479.             end
  480.         end
  481.        
  482.         if (anchor or alarm or (lockdoors > 1)) then
  483.            
  484.             -- Clear DB from non exist vehicle
  485.             removenonexistv(vehicles)
  486.            
  487.             if (getpvid(pvid, vehicles) <= 0) then
  488.                 table.insert(vehicles, pvid)
  489.             end
  490.         end
  491.         -------------------------------------------
  492.        
  493.         if (isaboat) then
  494.             if (anchor) then
  495.                 VEHICLE.SET_BOAT_ANCHOR(pvid, true)
  496.                
  497.                 showmsg_anchor = 1
  498.                 msgtimer = 0
  499.             end
  500.             return 0
  501.         end
  502.        
  503.         showmsg_anchor = 0
  504.        
  505.         if (alarm) then
  506.             VEHICLE.SET_VEHICLE_ALARM(pvid, true)
  507.            
  508.             showmsg_alarm = 1
  509.             msgtimer = 0
  510.         end
  511.        
  512.         if (lockdoors > 1) then
  513.             VEHICLE.SET_VEHICLE_DOORS_LOCKED(pvid, lockdoors)
  514.            
  515.             showmsg_lock = 1
  516.             msgtimer = 0
  517.         end
  518.        
  519.         return 0
  520.     end
  521.    
  522.     AI.TASK_LEAVE_VEHICLE(pid, pvid, 256)
  523.    
  524.     while (pvid ~= 0) do
  525.         VEHICLE.SET_VEHICLE_ENGINE_ON(pvid, true, true)
  526.         wait(0)
  527.         pvid = PED.GET_VEHICLE_PED_IS_IN(pid, false)
  528.     end
  529. end
  530.  
  531. function aev.init()
  532.     print('*********************')
  533.     print('Advanced Exit Vehicle')
  534.     print('ver. '.. ver ..' ('.. thedate ..')')
  535.     print('by Baaa (c)')
  536.     print('*********************')
  537. end
  538.  
  539. function aev.unload()
  540. end
  541.  
  542. return aev
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement