Advertisement
Nevinyrral

electric-offshore-pump.lua

Jun 6th, 2020 (edited)
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.52 KB | None | 0 0
  1. local KRASTORIO_OFFSHORE_PUMP_EVENT_FILTER =
  2. {
  3.     -- offshore pump entity name
  4.     {
  5.         filter = "name",
  6.         name   = "offshore-pump"
  7.     },
  8. }
  9.  
  10. -- @event, on_built_entity or on_robot_built_entity
  11. local function onBuiltAnEntity(event)
  12.     local old_entity = event.created_entity or event.entity
  13.     if old_entity.valid and old_entity.name == "offshore-pump" then
  14.         local with_damage        = old_entity.prototype.max_health - old_entity.health
  15.         local network            = old_entity.circuit_connection_definitions
  16.         local pos                = old_entity.position
  17.         local force              = old_entity.force
  18.         local direction          = old_entity.direction
  19.         local surface            = old_entity.surface
  20.        
  21.         if old_entity and old_entity.valid then
  22.             old_entity.destroy()
  23.         end
  24.        
  25.         local new_entity = surface.create_entity
  26.         {
  27.             name                      = "kr-electric-offshore-pump",
  28.             position                  = pos,
  29.             direction                 = direction,
  30.             force                     = force,
  31.             player                    = player_index,
  32.             create_build_effect_smoke = false,
  33.             raise_built               = false
  34.         }
  35.        
  36.         -- Set in the new entity the health of the destroyed entity
  37.         if with_damage > 0 then
  38.             new_entity.damage(with_damage, game.forces.neutral)
  39.         end
  40.     end
  41. end
  42.  
  43. function onBlueprint(event)
  44.     local player = game.players[event.player_index]
  45.     if
  46.         player and
  47.         player.valid and
  48.         player.cursor_stack and
  49.         player.cursor_stack.valid_for_read and
  50.         player.cursor_stack.is_blueprint and
  51.         player.cursor_stack.is_blueprint_setup()
  52.     then
  53.         local blueprint_entities = player.cursor_stack.get_blueprint_entities()
  54.         if blueprint_entities and next(blueprint_entities) then
  55.             local have_an_offshore_pump = false
  56.             for _, entity in pairs(blueprint_entities) do
  57.                 if entity.name == "kr-electric-offshore-pump" then
  58.                     entity.name = "offshore-pump"
  59.                     have_an_offshore_pump = true
  60.                 end
  61.             end
  62.             if have_an_offshore_pump then
  63.                 player.cursor_stack.set_blueprint_entities(blueprint_entities)
  64.             end
  65.         end
  66.     end
  67. end    
  68.  
  69. if script.active_mods["aai-industry"] then
  70.     return {}
  71. else
  72.     return
  73.     {
  74.         -- -- Actions      
  75.         { onBuiltAnEntity, "on_built_entity", KRASTORIO_OFFSHORE_PUMP_EVENT_FILTER },
  76.         { onBuiltAnEntity, "on_robot_built_entity", KRASTORIO_OFFSHORE_PUMP_EVENT_FILTER },
  77.         { onBuiltAnEntity, "script_raised_built" },
  78.         { onBuiltAnEntity, "script_raised_revive" },
  79.         { onBlueprint, "on_player_setup_blueprint" },
  80.         { onBlueprint, "on_player_configured_blueprint" }
  81.     }
  82. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement