diff --git a/control/functions.lua b/control/functions.lua index ec29354..9549aa4 100644 --- a/control/functions.lua +++ b/control/functions.lua @@ -6,6 +6,16 @@ * General/commonly used/important functions. --]] +function AlertPlayer(player, str) + local enabled = settings.get_player_settings(player)["spidertron-alerts"].value + if (enabled) then + player.print(str) + return true + else + return false + end +end + function SpidertronWaypointsCompatibility() -- Compatability for Spidertron Waypoints if remote.interfaces["SpidertronWaypoints"] then @@ -22,8 +32,9 @@ end function GiveStack(player, stack) if player.clear_cursor() and player.cursor_stack and player.cursor_stack.can_set_stack(stack) then if player.get_main_inventory() then - player.get_main_inventory().remove("squad-spidertron-remote-sel") - player.get_main_inventory().remove("squad-spidertron-remote") + local inv = player.get_main_inventory() + inv.remove("squad-spidertron-remote-sel") + inv.remove("squad-spidertron-remote") end player.cursor_stack.set_stack(stack) return true @@ -83,15 +94,15 @@ local function resetSprites(indices) end end -- Original GotoPlayer function before waypoints compat. -local function GotoPlayer_(index, position) +function GotoPlayerUpdate(index, position) local active = global.spidercontrol_player_s[index].active local active_n = #active local active_updated = Goto(active, position) global.spidercontrol_player_s[index].active = active_updated - + if (active_n > #active_updated) then - local str = (active_n - #active_updated) .. " units were destroyed or mined" - game.players[index].print(str) + local str = "[img=utility/warning_icon] " .. (active_n - #active_updated) .. " units were destroyed or mined" + AlertPlayer(game.players[index], str) end end @@ -101,22 +112,27 @@ local function GotoPlayerSW(index, position) local linear = player.is_shortcut_toggled("spidertron-remote-waypoint") local cyclic = player.is_shortcut_toggled("spidertron-remote-patrol") if (cyclic) then + if player.is_shortcut_toggled("squad-spidertron-follow") then + return + end active[1].spider.autopilot_destination = nil local patrol = global.spidercontrol_spidertronwaypoints_patrol[index] if (patrol) then local start = rendering.get_target(patrol[1]).position if (util.distance(position, start) < 5) then for j = 1, #active do - local waypoints = {} - for i = 1, #patrol do - local position = IJAdd( - rendering.get_target(patrol[i]).position, - active[j].delta - ) - waypoints[#waypoints+1] = {position = position} + if (active[j] and active[j].spider.valid) then + local waypoints = {} + for i = 1, #patrol do + local position = IJAdd( + rendering.get_target(patrol[i]).position, + active[j].delta + ) + waypoints[#waypoints+1] = {position = position} + end + + remote.call("SpidertronWaypoints", "assign_patrol", active[j].spider, waypoints) end - - remote.call("SpidertronWaypoints", "assign_patrol", active[j].spider, waypoints) end resetSprites(patrol) global.spidercontrol_player_s[index].active = {} @@ -142,7 +158,7 @@ local function GotoPlayerSW(index, position) resetSprites(global.spidercontrol_spidertronwaypoints_patrol[index]) global.spidercontrol_spidertronwaypoints_patrol[index] = nil else - GotoPlayer_(index, position) + GotoPlayerUpdate(index, position) resetSprites(global.spidercontrol_spidertronwaypoints_patrol[index]) global.spidercontrol_spidertronwaypoints_patrol[index] = nil @@ -153,9 +169,8 @@ function GotoPlayer(index, position) if SPIDERTRON_WAYPOINTS then GotoPlayerSW(index, position) else - GotoPlayer_(index, position) + GotoPlayerUpdate(index, position) end - end function GotoEntity(index) @@ -171,12 +186,12 @@ function GotoEntity(index) global.spidercontrol_linked_s[index].s = active_updated if (active_n > #active_updated) then - local str = (active_n - #active_updated) .. " units were destroyed or mined near [gps="..pos.x..","..pos.y.."], linked to "..entity.localised_name + local str = {"", "[img=utility/warning_icon] " .. (active_n - #active_updated) .. " units were destroyed or mined near [gps="..pos.x..","..pos.y.."], linked to ", entity.localised_name} game.forces[t.force].print(str) end if (#active_updated == 0) then - local str = {"", "Spidertron squad has been destroyed or unlinked from ", entity.localised_name, " near [gps="..pos.x..","..pos.y.."]"} + local str = {"", "[img=utility/warning_icon] Spidertron squad has been destroyed or unlinked from ", entity.localised_name, " near [gps="..pos.x..","..pos.y.."]"} game.forces[t.force].print(str) -- using force comms because this could be the death of a spidertron, not only removal end else @@ -188,9 +203,9 @@ function GotoEntity(index) end if (e) then local pos = e.position - game.forces[t.force].print("Target entity of spidertron squad has been destroyed or removed near [gps="..pos.x..","..pos.y.."]") + game.forces[t.force].print("[img=utility/warning_icon] Target entity of spidertron squad has been destroyed or removed near [gps="..pos.x..","..pos.y.."]") else - game.forces[t.force].print("Target entity of spidertron squad has been destroyed or removed") + game.forces[t.force].print("[img=utility/warning_icon] Target entity of spidertron squad has been destroyed or removed") end global.spidercontrol_linked_s = Remove(global.spidercontrol_linked_s, {index}) diff --git a/control/give_remote.lua b/control/give_remote.lua index d4e49a2..42ed908 100644 --- a/control/give_remote.lua +++ b/control/give_remote.lua @@ -10,10 +10,17 @@ require("control.functions") local function giveTwoTool(index, stack0, stack1) local d = global.spidercontrol_player_s[index].active - if (#d > 0 and d[1].spider.valid) then + local s + for i = 1, #d do + if (d[i].spider.valid) then + s = d[i].spider + break + end + end + if (s) then local player = game.players[index] if GiveStack(player, stack0) then - player.cursor_stack.connected_entity = d[1].spider + player.cursor_stack.connected_entity = s end else GiveStack(game.players[index], stack1) diff --git a/control/gui.lua b/control/gui.lua index 8bfc9b6..e9156de 100644 --- a/control/gui.lua +++ b/control/gui.lua @@ -164,7 +164,7 @@ script.on_event(defines.events.on_gui_click, function(event) UpdateGuiList(player) destroyRenameGui(get_frame_flow_(player, "center")) else - player.print("You have too many saved templates! Remove some before adding more. Maximum amount is "..limit) + player.print("[img=utility/danger_icon] You have too many saved templates! Remove some before adding more. Maximum amount is "..limit) end end elseif (name == "delete-active") then diff --git a/control/player_follow.lua b/control/player_follow.lua index 5884320..d263ca3 100644 --- a/control/player_follow.lua +++ b/control/player_follow.lua @@ -9,7 +9,9 @@ local function spidertronWaypointsOverride(s) if SPIDERTRON_WAYPOINTS then for i = 1, #s do - remote.call("SpidertronWaypoints", "clear_waypoints", s[i].spider.unit_number) + if (s[i] and s[i].spider) then + remote.call("SpidertronWaypoints", "clear_waypoints", s[i].spider.unit_number) + end end end end @@ -22,11 +24,14 @@ function SpiderbotFollow(player) GotoPlayer(index, player.position) spidertronWaypointsOverride(global.spidercontrol_player_s[index].active) else + if SPIDERTRON_WAYPOINTS and player.is_shortcut_toggled("spidertron-remote-patrol") then + return + end player.set_shortcut_toggled("squad-spidertron-follow", true) global.spidercontrol_player_s[player.index].p_pos = nil end else - player.print({"", {"error.error-message-box-title"}, ": ", {"player-doesnt-exist", {"gui.character"}}, " (", {"controller.god"}, "): ", {"gui-mod-info.status-disabled"}}) + player.print({"", "[img=utility/danger_icon] ", {"error.error-message-box-title"}, ": ", {"player-doesnt-exist", {"gui.character"}}, " (", {"controller.god"}, "): ", {"gui-mod-info.status-disabled"}}) end end @@ -55,13 +60,12 @@ function UpdateFollow() end end - -- player.print("running" .. game.tick) if player.walking_state.walking then local dir = player.walking_state.direction pos = IJAhead(pos, dir, mov_offset) end GotoPlayer(index, pos) - spidertronWaypointsOverride(active) + spidertronWaypointsOverride(global.spidercontrol_player_s[index].active) global.spidercontrol_player_s[index].p_pos = player.position end end diff --git a/control/player_man_designate.lua b/control/player_man_designate.lua index 10251c5..a07f643 100644 --- a/control/player_man_designate.lua +++ b/control/player_man_designate.lua @@ -51,28 +51,31 @@ end local function link(index, vehicle) local player = game.players[index] - local n = #global.spidercontrol_player_s[index].active - if player.selected and player.selected.valid and n > 0 then - local selected = player.selected - local pos = selected.position - local scale = 1.5 - local force = player.force - player.print({"", "Linked ".. n .. " spiders to ", selected.localised_name, " near [gps=" .. pos.x .. "," .. pos.y .. "]"}) - local sprite = drawSprite(selected, scale, force) - local s = util.table.deepcopy(global.spidercontrol_player_s[index].active) - for i = 1, #s do - s[i].sprite = drawSprite(s[i].spider, scale, force, {r=1,g=0,b=0}, {x=0,y=-0.3}) + local selected = player.selected + if (selected and selected.valid) then + GotoPlayerUpdate(index, selected.position) + local n = #global.spidercontrol_player_s[index].active + if (n > 0) then + local pos = selected.position + local scale = 1.5 + local force = player.force + local sprite = drawSprite(selected, scale, force) + local s = util.table.deepcopy(global.spidercontrol_player_s[index].active) + for i = 1, #s do + s[i].sprite = drawSprite(s[i].spider, scale, force, {r=1,g=0,b=0}, {x=0,y=-0.3}) + end + global.spidercontrol_linked_s[#global.spidercontrol_linked_s+1] = { + force=player.force.index, + target=selected, + sprite=sprite, + s=s + } + global.spidercontrol_player_s[index].active = {} -- We're taking away player control of this squad! + -- Probably should print the squad ID, the target entity id and other information + GiveStack(player, {name="squad-spidertron-unlink-tool",count=1}) + UpdateGuiList(player) + AlertPlayer(player, {"", "[img=virtual-signal/signal-info] Linked ".. n .. " spiders to ", selected.localised_name, " near [gps=" .. pos.x .. "," .. pos.y .. "]"}) end - global.spidercontrol_linked_s[#global.spidercontrol_linked_s+1] = { - force=player.force.index, - target=selected, - sprite=sprite, - s=s - } - global.spidercontrol_player_s[index].active = {} -- We're taking away player control of this squad! - -- Probably should print the squad ID, the target entity id and other information - GiveStack(player, {name="squad-spidertron-unlink-tool",count=1}) - UpdateGuiList(player) end vehicle.autopilot_destination = vehicle.position -- Just to look better end diff --git a/control/player_select.lua b/control/player_select.lua index a5727c3..2e09f68 100644 --- a/control/player_select.lua +++ b/control/player_select.lua @@ -12,28 +12,28 @@ require("control.2dvec") local function messageSpiders(target, s, force, n) if target.valid then local pos = target.position - game.forces[force].print({"", n.." spidertrons have been unlinked from a ", target.localised_name, " near [gps="..pos.x..","..pos.y.."]"}) + game.forces[force].print({"", "[img=utility/warning_icon] "..n.." spidertrons have been unlinked from a ", target.localised_name, " near [gps="..pos.x..","..pos.y.."]"}) else local e = FirstValid(s) if e then local pos = e.position - game.forces[force].print(n.." spidertrons have been unlinked from an entity near [gps="..pos.x..","..pos.y.."]") + game.forces[force].print("[img=utility/warning_icon] "..n.." spidertrons have been unlinked from an entity near [gps="..pos.x..","..pos.y.."]") else - game.forces[force].print(n.." spidertrons have been unlinked from an entity") + game.forces[force].print("[img=utility/warning_icon] "..n.." spidertrons have been unlinked from an entity") end end end local function messageS(target, s, force) if target.valid then local pos = target.position - game.forces[force].print({"", "Spidertron squad has been unlinked from ", target.localised_name, " near [gps="..pos.x..","..pos.y.."]"}) -- using force comms because this could be the death of a spidertron, not only removal + game.forces[force].print({"", "[img=utility/warning_icon] Spidertron squad has been unlinked from ", target.localised_name, " near [gps="..pos.x..","..pos.y.."]"}) -- using force comms because this could be the death of a spidertron, not only removal else local e = FirstValid(s) if e then local pos = e.position - game.forces[force].print({"", "Spidertron squad has been unlinked from a target near [gps="..pos.x..","..pos.y.."]"}) + game.forces[force].print({"", "[img=utility/warning_icon] Spidertron squad has been unlinked from a target near [gps="..pos.x..","..pos.y.."]"}) else - game.forces[force].print({"", "Spidertron squad has been unlinked from a target"}) + game.forces[force].print({"", "[img=utility/warning_icon] Spidertron squad has been unlinked from a target"}) end end end @@ -62,7 +62,6 @@ function SpiderDeSelect(spiders, force) s[j].spider.autopilot_destination = nil rem_id[#rem_id+1] = k rem_s[#rem_s+1] = j - -- game.print("REMOVE["..i.."]["..j.."]") end end if (#rem_id > 0) then diff --git a/settings.lua b/settings.lua index 885e1db..e9f6508 100644 --- a/settings.lua +++ b/settings.lua @@ -73,5 +73,13 @@ data:extend({ SETTING_LINK, SETTING_UNLINK } + }, + { + type = "bool-setting", + name = "spidertron-alerts", + localised_name = "Spidertron alerts", + localised_description = "If enabled an alert is given when a spidertron is removed or destroyed from a player squad. Default value: true", + setting_type = "runtime-per-user", + default_value = true } }) \ No newline at end of file