Guest User

Raven mod control.lua with equipmentgrid

a guest
Sep 13th, 2017
55
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. global.flycar = {{},{}}
  2.  
  3. local function build (ent)
  4.     if ent.name =="raven-1" then
  5.         table.insert(global.flycar[1], ent)
  6.     end
  7. end
  8.  
  9. local function car_flying ()
  10.     local cars = global.flycar
  11.     for i,j in pairs(cars[1]) do
  12.         if j.valid then
  13.             if j.speed >= 0.3 then
  14.                 local fly = {position = j.position, speed = j.speed, orientation = j.orientation, health = j.health, itemburn = j.burner.currently_burning, fuel = j.burner.remaining_burning_fuel}
  15.                 local idx = j.passenger
  16.                 local surface = j.surface
  17.                 local inv = j.get_inventory(1).get_contents()
  18.                 local inv2 = j.get_inventory(2).get_contents()
  19.               local grid = {}
  20.                 for _, equipment in pairs(j.grid.equipment) do
  21.                   table.insert(grid, {name = equipment.name, position = equipment.position, shield = equipment.shield, energy = equipment.energy})
  22.                 end
  23.                 j.destroy()
  24.                 j = surface.create_entity{name = "raven-2", position = fly.position, force =  game.forces.player}
  25.                 for y,u in pairs(inv) do
  26.                     j.insert({name = y, count = u})
  27.                 end
  28.                 for y,u in pairs(inv2) do
  29.                     j.insert({name = y, count = u})
  30.                 end
  31.                 j.orientation = fly.orientation
  32.                 j.speed = fly.speed
  33.                 j.health = fly.health
  34.                 j.burner.currently_burning = fly.itemburn
  35.                 j.burner.remaining_burning_fuel = fly.fuel
  36.               for _, eqm in pairs(grid) do
  37.                     local current_equipment = j.grid.put({name = eqm.name, position = eqm.position})
  38.                     if current_equipment and current_equipment.valid then
  39.                         if current_equipment.max_shield > 0 then current_equipment.shield = eqm.shield end
  40.                         if current_equipment.max_energy > 0 then current_equipment.energy = eqm.energy end
  41.                     end
  42.                 end
  43.                 j.passenger = idx
  44.                 local shadow = surface.create_entity{name = "raven-shadow", position = fly.position, force =  game.forces.neutral}
  45.                 shadow.insert({name = "solid-fuel", count = 50})
  46.                 shadow.orientation = fly.orientation
  47.                 shadow.speed = fly.speed
  48.                 table.insert(cars[2], {j,shadow,0.1})
  49.             end
  50.         else table.remove(cars[1], i)
  51.         end
  52.     end
  53.     for i,j in pairs(cars[2]) do
  54.         if j[1].valid then
  55.             if j[1].speed <= 0.27 then
  56.                 local idx = j[1].passenger
  57.                 local surface = j[1].surface
  58.                 local tilename = surface.get_tile(math.floor(j[1].position.x), math.floor(j[1].position.y)).name
  59.                 if tilename == "deepwater" or tilename == "water" then
  60.                     j[1].die()
  61.                     j[2].destroy()
  62.                     if idx ~= nil then
  63.                         if idx.valid then
  64.                             local position = idx.position
  65.                             tilename = surface.get_tile(math.floor(position.x), math.floor(position.y)).name
  66.                             if tilename == "deepwater" or tilename == "water" then idx.die() return
  67.                             else idx.health = idx.health - math.random(1,52)
  68.                                 if idx.health == 0 then idx.die() return end
  69.                             end
  70.                         end
  71.                     end
  72.                 else
  73.                     local fly = {position = j[1].position, speed = j[1].speed, orientation = j[1].orientation, health = j[1].health, itemburn = j[1].burner.currently_burning, fuel = j[1].burner.remaining_burning_fuel}
  74.                     local inv = j[1].get_inventory(1).get_contents()
  75.                     local inv2 = j[1].get_inventory(2).get_contents()
  76.                     local grid = {}
  77.                     for _, equipment in pairs(j[1].grid.equipment) do
  78.                       table.insert(grid, {name = equipment.name, position = equipment.position, shield = equipment.shield, energy = equipment.energy})
  79.                     end
  80.                     j[1].destroy()
  81.                     j[2].destroy()
  82.                     j = surface.create_entity{name = "raven-1", position = fly.position, force =  game.forces.player}
  83.                     for y,u in pairs(inv) do
  84.                         j.insert({name = y, count = u})
  85.                     end
  86.                     for y,u in pairs(inv2) do
  87.                         j.insert({name = y, count = u})
  88.                     end
  89.                     j.orientation = fly.orientation
  90.                     j.speed = fly.speed
  91.                     j.health = fly.health
  92.                     j.burner.currently_burning = fly.itemburn
  93.                     j.burner.remaining_burning_fuel = fly.fuel
  94.                     if tilename == "sand" or tilename == "sand-dark" then j.health = j.health - math.random(1,12) end
  95.                     for _, eqm in pairs(grid) do
  96.                         local current_equipment = j.grid.put({name = eqm.name, position = eqm.position})
  97.                         if current_equipment and current_equipment.valid then
  98.                             if current_equipment.max_shield > 0 then current_equipment.shield = eqm.shield end
  99.                             if current_equipment.max_energy > 0 then current_equipment.energy = eqm.energy end
  100.                         end
  101.                     end
  102.                     j.passenger = idx
  103.                     table.insert(cars[1], j)
  104.                 end
  105.             else
  106.                 local chight = (j[1].speed - 0.29)*10 - j[3]
  107.                 j[3] = j[3] + chight
  108.                 j[1].teleport({j[1].position.x, j[1].position.y - chight})
  109.                 j[2].teleport({j[1].position.x + j[3], j[1].position.y + j[3]})
  110.                 j[2].orientation = j[1].orientation
  111.                 j[2].speed = j[1].speed
  112.             end
  113.         else
  114.             if j[2].valid then j[2].destroy() end
  115.             table.remove(cars[2], i)
  116.         end
  117.     end
  118. end
  119.  
  120. --[[    Кусок кода для версии factorio, когда можно было покинуть транспорт на воде и не помереть.
  121. local function drive_control(idx)
  122.     local player = game.players[idx]
  123.     if not player.driving then
  124.         local tilename = player.surface.get_tile(math.floor(player.position.x), math.floor(player.position.y)).name
  125.         if tilename == "deepwater" or tilename == "water" then player.character.die() return end
  126.     end
  127. end
  128.  
  129. script.on_event(defines.events.on_player_driving_changed_state, function(event)
  130.     drive_control(event.player_index)
  131. end)]]
  132.  
  133. script.on_event(defines.events.on_built_entity, function(event)
  134.     build(event.created_entity)
  135. end)
  136.  
  137. script.on_event(defines.events.on_robot_built_entity, function(event)
  138.     build(event.created_entity)
  139. end)
  140.  
  141. script.on_event(defines.events.on_tick, function()
  142.     car_flying()
  143. end)
RAW Paste Data