Guest User

Untitled

a guest
May 19th, 2015
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.31 KB | None | 0 0
  1. -- Config
  2.  
  3. -- Assuming driver is first seat in vehicle tag then leave at 0
  4. SEAT_INDEX = 0
  5.  
  6. -- In seconds, the amount of time to keep a destroyed vehicle spawned
  7. DESTROY_TIME = 60
  8.  
  9. -- True or false, whether you want SAPP to respawn starting vehicles after DESTROY_TIME has elapsed
  10. RESPAWN_STARTING_VEHICLES = true
  11.  
  12. -- First vehicle is the destroyable vehicle, second vehicle is the destroyed variant
  13. VEHICLES = {
  14.     { "vehicles\\warthog\\mp_warthog", "vehicles\\ghost\\ghost_mp" },
  15.     { "vehicles\\rwarthog\\rwarthog", "vehicles\\banshee\\banshee_mp" },
  16. }
  17.  
  18. -- End of config
  19.  
  20. TAG_DATA = {}
  21. STARTING_VEHICLES = {}
  22. STARTING_OBJECT_IDS = {}
  23. GAME_STARTED = false
  24. GAME_COUNT = 1
  25.  
  26. api_version = "1.7.0.0"
  27.  
  28. function OnScriptLoad()
  29.     register_callback(cb['EVENT_DIE'], "OnPlayerDeath")
  30.     register_callback(cb['EVENT_GAME_START'], "OnGameStart")
  31.     register_callback(cb['EVENT_GAME_END'], "OnGameEnd")
  32.     register_callback(cb['EVENT_OBJECT_SPAWN'], "OnObjectSpawn")
  33. end
  34.  
  35. function OnPlayerDeath(PlayerIndex)
  36.     local player = get_dynamic_player(PlayerIndex)
  37.     local vehicle = read_dword(player + 0x11C)
  38.     local seatIndex = read_byte(player + 0x2F0)
  39.     local vehicleObject = get_object_memory(vehicle)
  40.     if(vehicleObject ~= 0 and seatIndex == SEAT_INDEX) then
  41.  
  42.         local destroyedVehicle = nil
  43.         local vehicleName = nil
  44.         local tagDataAddress = read_dword(0x40440000) + read_word(vehicleObject) * 0x20 + 0x14  
  45.  
  46.         local count = 0
  47.         for _,v in pairs(TAG_DATA) do
  48.             count = count + 1
  49.             if(tagDataAddress == v[2]) then
  50.                 vehicleName = v[1]
  51.                 destroyedVehicle = count
  52.             end
  53.         end
  54.  
  55.         local startingVehicleIndex = nil
  56.         count = 0
  57.         for _,v in pairs(STARTING_VEHICLES) do
  58.             count = count + 1
  59.             if(vehicle == v[1]) then startingVehicleIndex = count end
  60.         end
  61.  
  62.         if(destroyedVehicle ~= nil) then
  63.             for i=1,16 do
  64.                 if(i ~= PlayerIndex and player_present(i) and player_alive(i)) then
  65.                     local tempVehicleID = read_dword(get_dynamic_player(i) + 0x11C)
  66.                     if(tempVehicle == vehicle) then kill(i) end
  67.                 end
  68.             end
  69.  
  70.             timer(34, "create_destroyed_vehicle", vehicle, destroyedVehicle)
  71.             if(startingVehicleIndex ~= nil) then
  72.                 timer(67 + (1000 * DESTROY_TIME), "respawn_vehicle", startingVehicleIndex, vehicleName, GAME_COUNT)
  73.             end
  74.         end
  75.     end
  76. end
  77.  
  78. function create_destroyed_vehicle(vehicle, destroyedVehicle)
  79.     local vehicleObject = get_object_memory(vehicle)
  80.     if(vehicleObject == 0) then return false end
  81.  
  82.     local x, y, z = read_vector3d(vehicleObject + 0x5c)
  83.  
  84.     local a = read_float(vehicleObject + 0x76)
  85.     local b = read_float(vehicleObject + 0x7A)
  86.     local c = read_float(vehicleObject + 0x7E)
  87.     local d = read_float(vehicleObject + 0x82)
  88.  
  89.     destroy_object(vehicle)
  90.     newVehicleID = spawn_object("vehi", VEHICLES[tonumber(destroyedVehicle)][2], x, y, z, 0)
  91.     newVehicle = get_object_memory(newVehicleID)
  92.     if(newVehicle ~= nil) then
  93.         write_float(newVehicle + 0x76, a)
  94.         write_float(newVehicle + 0x7A, b)
  95.         write_float(newVehicle + 0x7E, c)
  96.         write_float(newVehicle + 0x82, d)
  97.     end
  98.  
  99.     timer(1000 * DESTROY_TIME, "destroy_vehicle", newVehicleID, GAME_COUNT)
  100.  
  101.     return false
  102. end
  103.  
  104. function destroy_vehicle(ObjectID, gameCount)
  105.     if(tonumber(gameCount) ~= GAME_COUNT) then return false end
  106.     destroy_object(ObjectID)
  107.     return false
  108. end
  109.  
  110. function respawn_vehicle(startingVehicleIndex, vehicleName, gameCount)
  111.     if(not RESPAWN_STARTING_VEHICLES or tonumber(gameCount) ~= GAME_COUNT) then return false end
  112.     local a = STARTING_VEHICLES[tonumber(startingVehicleIndex)]
  113.     respawnedVehicleID = spawn_object("vehi", vehicleName, a[2], a[3], a[4], 0)
  114.     respawnedVehicle = get_object_memory(respawnedVehicleID)
  115.     if(respawnedVehicle ~= nil) then
  116.         write_float(respawnedVehicle + 0x76, a[5])
  117.         write_float(respawnedVehicle + 0x7A, a[6])
  118.         write_float(respawnedVehicle + 0x7E, a[7])
  119.         write_float(respawnedVehicle + 0x82, a[8])
  120.         STARTING_VEHICLES[tonumber(startingVehicleIndex)][1] = respawnedVehicleID
  121.     end
  122.     return false
  123. end
  124.  
  125. function FindVehicleTag(TagName)
  126.     local tag_array = read_dword(0x40440000)
  127.     for i=0,read_word(0x4044000C)-1 do
  128.         local tag = tag_array + i * 0x20
  129.         if(read_dword(tag) == 1986357353 and read_string(read_dword(tag + 0x10)) == TagName) then
  130.             return tag + 0x14
  131.         end
  132.     end
  133. end
  134.  
  135. function OnGameStart()
  136.     local count = 0
  137.     for _,v in pairs(VEHICLES) do
  138.         count = count + 1
  139.         TAG_DATA[count] = { VEHICLES[count][1], FindVehicleTag(VEHICLES[count][1]) }
  140.     end
  141.     start_vehicle_check()
  142. end
  143.  
  144. function OnGameEnd()
  145.     TAG_DATA = {}
  146.     STARTING_VEHICLES = {}
  147.     STARTING_OBJECT_IDS = {}
  148.     GAME_COUNT = GAME_COUNT + 1
  149.     timer(15000, "reset_game")
  150. end
  151.  
  152. function reset_game()
  153.     GAME_STARTED = false
  154.     return false
  155. end
  156.  
  157. function start_vehicle_check()
  158.     for _,id in pairs(STARTING_OBJECT_IDS) do
  159.         local object = get_object_memory(id)
  160.         if(object ~= 0) then
  161.             if(read_byte(object + 0xB4) == 0x01) then
  162.                 local tagDataAddress = read_dword(0x40440000) + read_word(object) * 0x20 + 0x14  
  163.                 local count = 0
  164.                 for _,v in pairs(TAG_DATA) do
  165.                     count = count + 1
  166.                     if(tagDataAddress == v[2]) then
  167.                         local x, y, z = read_vector3d(object + 0x5c)
  168.                         local a = read_float(object + 0x76)
  169.                         local b = read_float(object + 0x7A)
  170.                         local c = read_float(object + 0x7E)
  171.                         local d = read_float(object + 0x82)
  172.                         table.insert(STARTING_VEHICLES, {id, x, y, z, a, b, c, d})
  173.                     end
  174.                 end
  175.             end
  176.         end
  177.     end
  178.     STARTING_OBJECT_IDS = {}
  179. end
  180.  
  181. function OnObjectSpawn(PlayerIndex, MapID, ParentID, ObjectID)
  182.     if(RESPAWN_STARTING_VEHICLES and not GAME_STARTED) then
  183.         table.insert(STARTING_OBJECT_IDS, ObjectID)
  184.     end
  185.     return true
  186. end
  187.  
  188. function OnScriptUnload() end
Advertisement
Add Comment
Please, Sign In to add comment