From 0c60f31266a4bfe6af27ec3ed101b31286d4b91b Mon Sep 17 00:00:00 2001 From: npc-strider Date: Sat, 15 Aug 2020 17:15:37 +0800 Subject: [PATCH] 0.2.0: Add follow mode --- changelog.txt | 6 +++ control.lua | 138 ++++++++++++++++++++++++++++++++++++++++---------- info.json | 4 +- settings.lua | 31 ++++++++++++ shortcuts.lua | 107 +++++++++++++++++++++++--------------- 5 files changed, 216 insertions(+), 70 deletions(-) create mode 100644 settings.lua diff --git a/changelog.txt b/changelog.txt index ccdc16c..9e3ac9f 100644 --- a/changelog.txt +++ b/changelog.txt @@ -9,3 +9,9 @@ Version: 0.1.1 Date: 2020-08-15 Bugfixes: - Fixed issue clearing inventory of 'residual' remotes with non-god controllers (*Facepalm*) + +--------------------------------------------------------------------------------------------------- +Version: 0.2.0 +Date: 2020-08-15 + Features: + - Adds follow mode for your squad to follow you either on foot or in a vehicle diff --git a/control.lua b/control.lua index fd555a9..a2a8582 100644 --- a/control.lua +++ b/control.lua @@ -37,32 +37,45 @@ local function validate_spiders(index) end end -local function spiderbot_designate(event) - local index = event.player_index - local player = game.players[index] - if player.cursor_stack.valid_for_read and player.cursor_stack.name == "squad-spidertron-remote" then - validate_spiders(index) - local spidersquad = global.spidercontrol_spidersquad[index] - local xbar=0 - local ybar=0 - local c=0 - for _, spider in pairs(spidersquad) do - c=c+1 - -- game.print(spider.position) - xbar=xbar+spider.position.x - ybar=ybar+spider.position.y - end - xbar=xbar/c - ybar=ybar/c - - dy=event.position.y-ybar - dx=event.position.x-xbar - - for _, spider in pairs(spidersquad) do - local position = spider.position - spider.autopilot_destination = {position.x+dx, position.y+dy} - end +local function squad_center(spidersquad) + local xbar=0 + local ybar=0 + local c=0 + for _, spider in pairs(spidersquad) do + c=c+1 + xbar=xbar+spider.position.x + ybar=ybar+spider.position.y end + xbar=xbar/c + ybar=ybar/c + return {xbar,ybar} +end + +local function spiderbot_designate(index, position_) + validate_spiders(index) + local spidersquad = global.spidercontrol_spidersquad[index] + + local posbar = squad_center(spidersquad) + + local dx=position_.x-posbar[1] + local dy=position_.y-posbar[2] + + for _, spider in pairs(spidersquad) do + local position = spider.position + spider.autopilot_destination = {position.x+dx, position.y+dy} + end +end + +local function spiderbot_follow(player) + if player.character then + if player.is_shortcut_toggled("squad-spidertron-follow") then + player.set_shortcut_toggled("squad-spidertron-follow", false) + else + player.set_shortcut_toggled("squad-spidertron-follow", true) + end + else + player.print({"", {"error.error-message-box-title"}, ": ", {"player-doesnt-exist", {"gui.character"}}, " (", {"controller.god"}, "): ", {"gui-mod-info.status-disabled"}}) + end end local function initialize() @@ -77,7 +90,21 @@ script.on_configuration_changed(initialize) script.on_event(defines.events.on_player_alt_selected_area, spiderbot_select) script.on_event(defines.events.on_player_selected_area, spiderbot_select) -script.on_event(defines.events.on_player_used_spider_remote, spiderbot_designate) +script.on_event(defines.events.on_player_used_spider_remote, function (event) + local index = event.player_index + local player = game.players[index] + local cursor_stack = player.cursor_stack + if cursor_stack.valid_for_read and cursor_stack.name == "squad-spidertron-remote" and event.success then + player.set_shortcut_toggled("squad-spidertron-follow", false) + spiderbot_designate(index, event.position) + end +end) + +script.on_event(defines.events.on_lua_shortcut, function (event) + if event.prototype_name == "squad-spidertron-follow" then + spiderbot_follow(game.players[event.player_index]) + end +end) script.on_event("squad-spidertron-remote", function(event) local player = game.players[event.player_index] @@ -90,4 +117,63 @@ script.on_event("squad-spidertron-remote", function(event) player.cursor_stack.set_stack(stack) end +end) + +script.on_event("squad-spidertron-follow", function(event) + spiderbot_follow(game.players[event.player_index]) +end) + +local mov_offset = settings.global["spidertron-follow-prediction-distance"].value --This is so the player stays within the spider squad when moving +local mov_offset_diagonal = math.sqrt(mov_offset^2/2) + +local update_interval = settings.global["spidertron-follow-update-interval"].value + +script.on_nth_tick(update_interval, function (event) + for _, player in pairs(game.players) do + if player.is_shortcut_toggled("squad-spidertron-follow") and global.spidercontrol_spidersquad[player.index] then + local index = player.index + local pos = player.position + local pos_x = pos.x + local pos_y = pos.y + if player.walking_state.walking then + local dir = player.walking_state.direction + local def_dir = defines.direction + if dir == def_dir.north then + pos_y = pos_y - mov_offset + elseif dir == def_dir.northeast then + -- game.print("ne") + pos_x = pos_x + mov_offset_diagonal + pos_y = pos_y - mov_offset_diagonal + elseif dir == def_dir.east then + -- game.print("e") + pos_x = pos_x + mov_offset + elseif dir == def_dir.southeast then + -- game.print("se") + pos_x = pos_x + mov_offset_diagonal + pos_y = pos_y + mov_offset_diagonal + elseif dir == def_dir.south then + -- game.print("s") + pos_y = pos_y + mov_offset + elseif dir == def_dir.southwest then + -- game.print("sw") + pos_x = pos_x - mov_offset_diagonal + pos_y = pos_y + mov_offset_diagonal + elseif dir == def_dir.west then + -- game.print("w") + pos_x = pos_x - mov_offset + else -- northwest + -- game.print("nw") + pos_x = pos_x - mov_offset_diagonal + pos_y = pos_y - mov_offset_diagonal + end + end + spiderbot_designate(index, {x=pos_x, y=pos_y}) + if player.vehicle then + local vehicle = player.vehicle + if vehicle.type == "spider-vehicle" then + vehicle.autopilot_destination = squad_center(global.spidercontrol_spidersquad[index]) + end + end + end + end end) \ No newline at end of file diff --git a/info.json b/info.json index 4ea93e2..551d9b5 100644 --- a/info.json +++ b/info.json @@ -1,11 +1,11 @@ { "name": "Spider_Control", - "version": "0.1.1", + "version": "0.2.0", "factorio_version": "1.0", "title": "Spidertron squad control", "author": "npc_strider(morley376)", "contact": "", "homepage": "http://steamcommunity.com/id/morley376", "dependencies": ["base >= 1.0.0"], - "description": "spidertron squad controls so you don't have to use 100 remotes to control your spider army" + "description": "adds a spidertron squad remote so you don't have to use 100 remotes to control your spider army. also adds a follow mode for your spider army" } \ No newline at end of file diff --git a/settings.lua b/settings.lua new file mode 100644 index 0000000..8b7a686 --- /dev/null +++ b/settings.lua @@ -0,0 +1,31 @@ +--[[ 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 + * + * settings.lua + * Mod settings +--]] + + +data:extend({ + -- server + { + type = "double-setting", + name = "spidertron-follow-prediction-distance", + localised_name = "Follow prediction distance", + localised_description = "When in follow mode, the movement targets of the spider bot will be this far ahead of the player's position. Minimum value: 0.0, Maximum value: 500.0, Default value: 20.0", + setting_type = "runtime-global", + default_value = 20.0, + minimum_value = 0.0, + maximum_value = 500.0, + }, + { + type = "int-setting", + name = "spidertron-follow-update-interval", + localised_name = "Follow update interval", + localised_description = "Rate at which the follow mode updates the target positions of the spidertrons. This value is in ticks. Larger values are less laggy but result in less responsive squad movement. Minimum value: 5, Default value: 20", + setting_type = "runtime-global", + default_value = 20, + minimum_value = 5 + } +}) \ No newline at end of file diff --git a/shortcuts.lua b/shortcuts.lua index 6bad684..2a05541 100644 --- a/shortcuts.lua +++ b/shortcuts.lua @@ -6,43 +6,70 @@ * Shortcuts and required items etc for the spider controls --]] +require('util') + +local shortcut_remote = { + type = "shortcut", + name = "squad-spidertron-remote", + order = "a[squad-spidertron-remote]", + action = "create-blueprint-item", + localised_name = "Spidertron squad remote", + associated_control_input = "squad-spidertron-remote", + technology_to_unlock = "spidertron", + item_to_create = "squad-spidertron-remote-sel", + style = "red", + icon = + { + filename = "__base__/graphics/icons/spidertron.png", + priority = "extra-high-no-scale", + size = 64, + scale = 1, + flags = {"icon"} + }, + -- small_icon = + -- { + -- filename = "__Shortcuts__/graphics/artillery-targeting-remote-x24.png", + -- priority = "extra-high-no-scale", + -- size = 24, + -- scale = 1, + -- flags = {"icon"} + -- }, + -- disabled_small_icon = + -- { + -- filename = "__Shortcuts__/graphics/artillery-targeting-remote-x24-white.png", + -- priority = "extra-high-no-scale", + -- size = 24, + -- scale = 1, + -- flags = {"icon"} + -- }, +} + +local shortcut_follow = util.table.deepcopy(shortcut_remote) +shortcut_follow.name = "squad-spidertron-follow" +shortcut_follow.action = "lua" +shortcut_follow.localised_name = "Follow player" +shortcut_follow.associated_control_input = "squad-spidertron-follow" +shortcut_follow.style = "blue" +shortcut_follow.toggleable = true + +local input_remote = { + type = "custom-input", + name = "squad-spidertron-remote", + localised_name = "Spidertron squad remote", + key_sequence = "ALT + X", + consuming = "none" +} + +local input_follow = util.table.deepcopy(input_remote) +input_follow.name = "squad-spidertron-follow" +input_follow.localised_name = "Follow player" +input_follow.key_sequence = "ALT + C" + data:extend( { - { - type = "shortcut", - name = "squad-spidertron-remote", - order = "a[squad-spidertron-remote]", - action = "create-blueprint-item", - localised_name = "Spidertron squad remote", - associated_control_input = "squad-spidertron-remote", - technology_to_unlock = "spidertron", - item_to_create = "squad-spidertron-remote-sel", - style = "red", - icon = - { - filename = "__base__/graphics/icons/spidertron.png", - priority = "extra-high-no-scale", - size = 64, - scale = 1, - flags = {"icon"} - }, - -- small_icon = - -- { - -- filename = "__Shortcuts__/graphics/artillery-targeting-remote-x24.png", - -- priority = "extra-high-no-scale", - -- size = 24, - -- scale = 1, - -- flags = {"icon"} - -- }, - -- disabled_small_icon = - -- { - -- filename = "__Shortcuts__/graphics/artillery-targeting-remote-x24-white.png", - -- priority = "extra-high-no-scale", - -- size = 24, - -- scale = 1, - -- flags = {"icon"} - -- }, - }, + shortcut_remote, + shortcut_follow, + { type = "selection-tool", name = "squad-spidertron-remote-sel", @@ -82,11 +109,7 @@ data:extend( order = "b[personal-transport]-c[spidertron]-b[remote]", stack_size = 1 }, - { - type = "custom-input", - name = "squad-spidertron-remote", - localised_name = "Spidertron squad remote", - key_sequence = "ALT + X", - consuming = "game-only" - } + + input_remote, + input_follow })