From f9436c315be925d717b7a3049e05bd0d00a8f311 Mon Sep 17 00:00:00 2001 From: npc-strider Date: Sun, 29 Nov 2020 01:17:37 +0800 Subject: [PATCH] Added mod options: gui tickrate, max templates, default remotes --- changelog.txt | 4 ++-- constants.lua | 17 +++++++++++++++ control.lua | 27 ++++++++++++++++-------- control/give_remote.lua | 44 ++++++++++++++++++++++++++++++++------- control/gui.lua | 6 +++--- settings.lua | 46 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 122 insertions(+), 22 deletions(-) create mode 100644 constants.lua diff --git a/changelog.txt b/changelog.txt index aa456a9..4c5c395 100644 --- a/changelog.txt +++ b/changelog.txt @@ -56,7 +56,7 @@ Date: 2020-11-28 - Completely refactor code to be cleaner - Spidertron Waypoints mod compatibility - Added menu/gui to save and load squad configurations - - Spidertron link tool (Link squads to an entity) - - 2NOTICE: THIS VERSION IS A WORK IN PROGRESS - CHANGELOG IS NOT FINAL + - Added Spidertron link tool (Link squads to an entity) + - Added mod option to override the default tool/remote given by shortcut - 3NOTICE: THIS VERSION IS A WORK IN PROGRESS - CHANGELOG IS NOT FINAL - 4NOTICE: THIS VERSION IS A WORK IN PROGRESS - CHANGELOG IS NOT FINAL diff --git a/constants.lua b/constants.lua new file mode 100644 index 0000000..32aa482 --- /dev/null +++ b/constants.lua @@ -0,0 +1,17 @@ +--[[ Copyright (c) 2020 npc_strider + * For direct use of code or graphics, credit is appreciated. See LICENSE.txt for more information. + * This mod may contain modified code sourced from base/core Factorio + * + * constants.lua + * Constants. +--]] + +-- Settings constants +SETTING_REMOTE_SEL = "Spidertron squad selection" +SETTING_REMOTE = "Spidertron squad remote" + +SETTING_LINK = "Spidertron link tool" +SETTING_UNLINK = "Spidertron unlink tool" + +SETTING_AUTOMATIC = "Automatic" +-- \ No newline at end of file diff --git a/control.lua b/control.lua index 8718e4e..eda705d 100644 --- a/control.lua +++ b/control.lua @@ -8,6 +8,7 @@ require("util") +require("constants") require("control.debug") -- REMEMBER TO COMMENT DEBUG OUT IN RELEASE!! -- REMEMBER TO COMMENT DEBUG OUT IN RELEASE!! @@ -25,7 +26,6 @@ require("control.entity_follow") require("control.functions") -- require("control.select") - ------------------------------------------------------------------------ -- EVENTS ------------------------------------------------------------------------ @@ -38,25 +38,34 @@ end) -- link tool script.on_event("squad-spidertron-link-tool", function(event) - GiveLinkTool(event.player_index) + local index = event.player_index + local settings = settings.get_player_settings(game.players[index]) + GiveLinkTool(index, settings) +end) + +script.on_event("squad-spidertron-remote", function(event) + local index = event.player_index + local settings = settings.get_player_settings(game.players[index]) + GiveSquadTool(index, settings) end) script.on_event("squad-spidertron-list", function(event) ToggleGuiList(event.player_index) end) -script.on_event(defines.events.on_lua_shortcut, function (event) +script.on_event(defines.events.on_lua_shortcut, function(event) + local index = event.player_index + local settings = settings.get_player_settings(game.players[index]) local name = event.prototype_name if name == "squad-spidertron-remote" then - GiveStack(game.players[event.player_index], {name = "squad-spidertron-remote-sel", count = 1}) + GiveLinkTool(index, settings) + elseif name == "squad-spidertron-link-tool" then + GiveSquadTool(index, settings) elseif name == "squad-spidertron-follow" then - local index = event.player_index -- squad_leader_state(index) SpiderbotFollow(game.players[index]) - elseif name == "squad-spidertron-link-tool" then - GiveLinkTool(event.player_index) elseif name == "squad-spidertron-list" then - ToggleGuiList(event.player_index) + ToggleGuiList(index) end end) @@ -64,7 +73,7 @@ script.on_nth_tick(settings.global["spidertron-follow-update-interval"].value, f UpdateFollow() UpdateFollowEntity() end) -script.on_nth_tick(60, function(event) +script.on_nth_tick(settings.global["spidertron-gui-update-interval"].value, function(event) for _, player in pairs(game.players) do UpdateGuiList(player) end diff --git a/control/give_remote.lua b/control/give_remote.lua index 3eaab65..d4e49a2 100644 --- a/control/give_remote.lua +++ b/control/give_remote.lua @@ -8,21 +8,49 @@ require("control.functions") -function GiveLinkTool(index) +local function giveTwoTool(index, stack0, stack1) local d = global.spidercontrol_player_s[index].active if (#d > 0 and d[1].spider.valid) then local player = game.players[index] - if GiveStack(player, {name="squad-spidertron-link-tool",count=1}) then + if GiveStack(player, stack0) then player.cursor_stack.connected_entity = d[1].spider end else - GiveStack(game.players[index], {name="squad-spidertron-unlink-tool",count=1}) + GiveStack(game.players[index], stack1) + end +end + +function GiveLinkTool(index, settings) + local player = game.players[index] + local value = settings["spidertron-default-link-remote"].value + if value == SETTING_LINK then + GiveStack(player, {name = "squad-spidertron-link-tool", count = 1}) + elseif value == SETTING_UNLINK then + GiveStack(player, {name = "squad-spidertron-unlink-tool", count = 1}) + else -- value == AUTOMATIC + giveTwoTool(index, + {name="squad-spidertron-link-tool",count=1}, + {name="squad-spidertron-unlink-tool",count=1} + ) + end +end + +function GiveSquadTool(index, settings) + local player = game.players[index] + local value = settings["spidertron-default-squad-remote"].value + if value == SETTING_REMOTE_SEL then + GiveStack(player, {name = "squad-spidertron-remote-sel", count = 1}) + elseif value == SETTING_LINK then + GiveStack(player, {name = "squad-spidertron-remote", count = 1}) + else -- value == AUTOMATIC + giveTwoTool( + index, + {name="squad-spidertron-remote",count=1}, + {name="squad-spidertron-remote-sel",count=1} + ) end end -script.on_event("squad-spidertron-remote", function(event) - GiveStack(game.players[event.player_index], {name="squad-spidertron-remote-sel",count=1}) -end) script.on_event("squad-spidertron-switch-modes", function(event) local player = game.players[event.player_index] @@ -40,7 +68,7 @@ script.on_event("squad-spidertron-switch-modes", function(event) elseif name == "squad-spidertron-link-tool" then GiveStack(player, {name="squad-spidertron-unlink-tool",count=1}) elseif name == "squad-spidertron-unlink-tool" then - GiveLinkTool(event.player_index) + GiveStack(player, {name = "squad-spidertron-link-tool", count = 1}) end end end) @@ -53,7 +81,7 @@ end) -- squad_leader_state(index) -- spiderbot_follow(game.players[index]) -- elseif name == "squad-spidertron-link-tool" then --- GiveLinkTool(event.player_index) +-- GiveStack(player, {name = "squad-spidertron-link-tool", count = 1}) -- end -- end) diff --git a/control/gui.lua b/control/gui.lua index 95cea89..8bfc9b6 100644 --- a/control/gui.lua +++ b/control/gui.lua @@ -138,15 +138,15 @@ function ToggleGuiList(index) end -commands.add_command("ssc_gui", "Create gui", function(cmd) - gui(game.players[cmd.player_index]) +commands.add_command("ssc_gui", "Spidertron squad control configured squads gui toggle", function(cmd) + ToggleGuiList(cmd.player_index) end) script.on_event(defines.events.on_gui_click, function(event) local gui = event.element local index = event.player_index local player = game.players[index] - local limit = 20 + local limit = settings.global["spidertron-max-squads"].value if not (player and player.valid and gui and gui.valid) then return end diff --git a/settings.lua b/settings.lua index 8b7a686..885e1db 100644 --- a/settings.lua +++ b/settings.lua @@ -6,6 +6,7 @@ * Mod settings --]] +require("constants") data:extend({ -- server @@ -27,5 +28,50 @@ data:extend({ setting_type = "runtime-global", default_value = 20, minimum_value = 5 + }, + { + type = "int-setting", + name = "spidertron-gui-update-interval", + localised_name = "GUI update interval", + localised_description = "Rate at which the spidertron save/load GUI is updated. This value is in ticks. Larger values are less laggy but result in less responsive information. Minimum value: 5, Default value: 60", + setting_type = "runtime-global", + default_value = 60, + minimum_value = 5 + }, + { + type = "int-setting", + name = "spidertron-max-squads", + localised_name = "Maximum templates per player", + localised_description = "Maximum number of squad templates a player can save. Minimum value: 1, Default value: 20", + setting_type = "runtime-global", + default_value = 20, + minimum_value = 1 + }, + -- player + { + type = "string-setting", + name = "spidertron-default-squad-remote", + localised_name = "Default squad tool", + localised_description = "Type of remote to give when the squad remote shortcut is used. Default value: "..SETTING_REMOTE_SEL, + setting_type = "runtime-per-user", + default_value = SETTING_REMOTE_SEL, + allowed_values = { + SETTING_AUTOMATIC, + SETTING_REMOTE_SEL, + SETTING_REMOTE + } + }, + { + type = "string-setting", + name = "spidertron-default-link-remote", + localised_name = "Default link tool", + localised_description = "Type of remote to give when the Link shortcut is used. Default value: "..SETTING_AUTOMATIC, + setting_type = "runtime-per-user", + default_value = SETTING_AUTOMATIC, + allowed_values = { + SETTING_AUTOMATIC, + SETTING_LINK, + SETTING_UNLINK + } } }) \ No newline at end of file