diff --git a/event.lua b/event.lua index 1234567..89abcde 100644 --- a/event.lua +++ b/event.lua @@ require "util" require "event" -local function apply_settings() - local settings = settings.global - game.forces["player"].character_build_distance_bonus = settings["themightygugi_longreach-build-distance-bonus"]["value"] - game.forces["player"].character_item_drop_distance_bonus = settings["themightygugi_longreach-item-drop-distance-bonus"]["value"] - game.forces["player"].character_reach_distance_bonus = settings["themightygugi_longreach-reach-distance-bonus"]["value"] - game.forces["player"].character_resource_reach_distance_bonus = settings["themightygugi_longreach-resource-reach-distance-bonus"]["value"] -end - -local function set_join_options(event) - apply_settings() - - -- an earlier version of this mod set these two settings, and causes major game lag if 1000. Ajust them to something acceptable - if game.players[event.player_index].force_item_pickup_distance_bonus > 10 then - game.players[event.player_index].force_item_pickup_distance_bonus = 1 - end - if game.players[event.player_index].force_loot_pickup_distance_bonus > 10 then - game.players[event.player_index].force_loot_pickup_distance_bonus = 1 - end -end - -function On_Init() apply_settings() end -function On_Change() apply_settings() end - -script.on_init(function() On_Init() end) -script.on_configuration_changed(function() On_Change() end) -script.on_event(defines.events.on_runtime_mod_setting_changed,function () apply_settings() end) -Event.register(defines.events.on_player_joined_game, set_join_options) +local LongReach = {} + +LongReach.debug = false + +local function log_debug(msg) + if LongReach.debug and game and not game.is_multiplayer() then + game.print("[LongReach] " .. msg) + end +end + +local function detect_space_age() + LongReach.remote = { has_space_age = false, api_name = nil } + + if remote and remote.interfaces then + for name, _ in pairs(remote.interfaces) do + if string.find(name, "space%-?age") or string.find(name, "space_age") then + LongReach.remote.has_space_age = true + LongReach.remote.api_name = name + break + end + end + end + + if LongReach.remote.has_space_age then + log_debug("Detected Space Age interface: " .. LongReach.remote.api_name) + else + log_debug("No Space Age interface detected; using vanilla reach logic.") + end +end + +local function apply_settings() + local settings = settings.global + + for _, force in pairs(game.forces) do + if force.valid and force.name ~= "enemy" and force.name ~= "neutral" then + if LongReach.remote.has_space_age and LongReach.remote.api_name then + local ok = pcall(function() + if remote.interfaces[LongReach.remote.api_name]["update_reach_bonus"] then + remote.call(LongReach.remote.api_name, "update_reach_bonus", force.name) + end + end) + if ok then + log_debug("Delegated reach bonus update to Space Age for force: " .. force.name) + goto continue + end + end + + force.character_build_distance_bonus = settings["themightygugi_longreach-build-distance-bonus"]["value"] + force.character_item_drop_distance_bonus = settings["themightygugi_longreach-item-drop-distance-bonus"]["value"] + force.character_reach_distance_bonus = settings["themightygugi_longreach-reach-distance-bonus"]["value"] + force.character_resource_reach_distance_bonus = settings["themightygugi_longreach-resource-reach-distance-bonus"]["value"] + + ::continue:: + end + end +end + +local function set_join_options(event) + apply_settings() + local player = game.get_player(event.player_index) + if not player or not player.valid then return end + + if player.force_item_pickup_distance_bonus > 10 then + player.force_item_pickup_distance_bonus = 1 + end + if player.force_loot_pickup_distance_bonus > 10 then + player.force_loot_pickup_distance_bonus = 1 + end +end + +local function on_init() + detect_space_age() + apply_settings() +end + +local function on_change() + detect_space_age() + apply_settings() +end + +script.on_init(on_init) +script.on_configuration_changed(on_change) +script.on_event(defines.events.on_runtime_mod_setting_changed, apply_settings) +Event.register(defines.events.on_player_joined_game, set_join_options)