mirror of
https://github.com/peter-tanner/spidertron-squad-control.git
synced 2024-11-30 11:00:17 +08:00
0.3.0: Add toggle between command/selecting mode, eliminate drifting of
squad and fix follow bug
This commit is contained in:
parent
0c60f31266
commit
f574bccd15
|
@ -15,3 +15,13 @@ Version: 0.2.0
|
||||||
Date: 2020-08-15
|
Date: 2020-08-15
|
||||||
Features:
|
Features:
|
||||||
- Adds follow mode for your squad to follow you either on foot or in a vehicle
|
- Adds follow mode for your squad to follow you either on foot or in a vehicle
|
||||||
|
|
||||||
|
---------------------------------------------------------------------------------------------------
|
||||||
|
Version: 0.3.0
|
||||||
|
Date: 2020-08-16
|
||||||
|
Features:
|
||||||
|
- Squads stay aligned, based on original formation when selected
|
||||||
|
- RMB now switches between commanding and selecting mode
|
||||||
|
Bugfixes:
|
||||||
|
- Fixed "wobble" or "dance" when in follow mode while in a spidertron that is in the squad that is being asked to follow
|
||||||
|
- Fixed squad drifting
|
||||||
|
|
217
control.lua
217
control.lua
|
@ -6,34 +6,14 @@
|
||||||
* Spiderbot.
|
* Spiderbot.
|
||||||
--]]
|
--]]
|
||||||
|
|
||||||
local function spiderbot_select(event)
|
local function give_tool(player, stack)
|
||||||
local index = event.player_index
|
if player.clean_cursor() and player.cursor_stack.can_set_stack(stack) then
|
||||||
local spiders = event.entities
|
if player.get_main_inventory() then
|
||||||
if event.item == "squad-spidertron-remote-sel" and #spiders > 0 then
|
player.get_main_inventory().remove("squad-spidertron-remote-sel")
|
||||||
global.spidercontrol_spidersquad[index] = spiders
|
player.get_main_inventory().remove("squad-spidertron-remote")
|
||||||
local player = game.players[index]
|
|
||||||
local stack = {
|
|
||||||
name="squad-spidertron-remote",
|
|
||||||
count=1
|
|
||||||
}
|
|
||||||
-- game.print(spiders[1])
|
|
||||||
if player.cursor_stack.can_set_stack(stack) then
|
|
||||||
player.cursor_stack.set_stack(stack)
|
|
||||||
player.cursor_stack.connected_entity=spiders[1]
|
|
||||||
end
|
end
|
||||||
end
|
player.cursor_stack.set_stack(stack)
|
||||||
end
|
return true
|
||||||
|
|
||||||
local function validate_spiders(index)
|
|
||||||
local c=0
|
|
||||||
for i, spider in pairs(global.spidercontrol_spidersquad[index]) do
|
|
||||||
if not spider.valid then
|
|
||||||
global.spidercontrol_spidersquad[index][i] = nil
|
|
||||||
c=c+1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if c > 0 then
|
|
||||||
game.players[index].print(c .. " units were destroyed since the last position command was sent")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -43,26 +23,57 @@ local function squad_center(spidersquad)
|
||||||
local c=0
|
local c=0
|
||||||
for _, spider in pairs(spidersquad) do
|
for _, spider in pairs(spidersquad) do
|
||||||
c=c+1
|
c=c+1
|
||||||
xbar=xbar+spider.position.x
|
local pos = spider.position
|
||||||
ybar=ybar+spider.position.y
|
xbar=xbar+pos.x
|
||||||
|
ybar=ybar+pos.y
|
||||||
end
|
end
|
||||||
xbar=xbar/c
|
return {xbar/c,ybar/c}
|
||||||
ybar=ybar/c
|
|
||||||
return {xbar,ybar}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function spiderbot_designate(index, position_)
|
local function spiderbot_select(event)
|
||||||
validate_spiders(index)
|
local index = event.player_index
|
||||||
local spidersquad = global.spidercontrol_spidersquad[index]
|
local spiders = event.entities
|
||||||
|
if event.item == "squad-spidertron-remote-sel" and #spiders > 0 then
|
||||||
|
local center = squad_center(spiders)
|
||||||
|
global.spidercontrol_spidersquad[index] = {spiders={}} -- some future proofing here
|
||||||
|
for _, spider in pairs(spiders) do
|
||||||
|
local pos = spider.position
|
||||||
|
table.insert(global.spidercontrol_spidersquad[index].spiders, {
|
||||||
|
spider_entity=spider,
|
||||||
|
d={pos.x-center[1],pos.y-center[2]} -- dx and dy
|
||||||
|
})
|
||||||
|
end
|
||||||
|
local player = game.players[index]
|
||||||
|
if give_tool(player, {name="squad-spidertron-remote",count=1}) then
|
||||||
|
player.cursor_stack.connected_entity=spiders[1]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local posbar = squad_center(spidersquad)
|
local function validate_spiders(index)
|
||||||
|
local c=0
|
||||||
|
if global.spidercontrol_spidersquad[index] then
|
||||||
|
for i, spider_ in pairs(global.spidercontrol_spidersquad[index].spiders) do
|
||||||
|
if not spider_.spider_entity.valid then
|
||||||
|
global.spidercontrol_spidersquad[index].spiders[i] = nil
|
||||||
|
c=c+1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if c > 0 then
|
||||||
|
game.players[index].print(c .. " units were destroyed or mined since the last position command was sent")
|
||||||
|
end
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local dx=position_.x-posbar[1]
|
local function spiderbot_designate(index, position)
|
||||||
local dy=position_.y-posbar[2]
|
if validate_spiders(index) then
|
||||||
|
local spidersquad = global.spidercontrol_spidersquad[index].spiders
|
||||||
for _, spider in pairs(spidersquad) do
|
for _, spider_ in pairs(spidersquad) do
|
||||||
local position = spider.position
|
local spider = spider_.spider_entity
|
||||||
spider.autopilot_destination = {position.x+dx, position.y+dy}
|
local d = spider_.d
|
||||||
|
spider.autopilot_destination = {position.x+d[1], position.y+d[2]}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -106,72 +117,92 @@ script.on_event(defines.events.on_lua_shortcut, function (event)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
script.on_event("squad-spidertron-remote", function(event)
|
script.on_event(defines.events.on_player_created, function (event)
|
||||||
local player = game.players[event.player_index]
|
global.spidercontrol_spidersquad[event.player_index] = {}
|
||||||
local stack = {name="squad-spidertron-remote-sel",count=1}
|
end)
|
||||||
if player.clean_cursor() 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")
|
|
||||||
end
|
|
||||||
|
|
||||||
player.cursor_stack.set_stack(stack)
|
script.on_event("squad-spidertron-remote", function(event)
|
||||||
end
|
give_tool(game.players[event.player_index], {name="squad-spidertron-remote-sel",count=1})
|
||||||
end)
|
end)
|
||||||
|
|
||||||
script.on_event("squad-spidertron-follow", function(event)
|
script.on_event("squad-spidertron-follow", function(event)
|
||||||
spiderbot_follow(game.players[event.player_index])
|
spiderbot_follow(game.players[event.player_index])
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
script.on_event("squad-spidertron-switch-modes", function(event)
|
||||||
|
local player = game.players[event.player_index]
|
||||||
|
local cursor_stack = player.cursor_stack
|
||||||
|
if cursor_stack.valid_for_read then
|
||||||
|
if cursor_stack.name == "squad-spidertron-remote" then
|
||||||
|
give_tool(player, {name="squad-spidertron-remote-sel",count=1})
|
||||||
|
elseif cursor_stack.name == "squad-spidertron-remote-sel" then
|
||||||
|
local e = global.spidercontrol_spidersquad[event.player_index]
|
||||||
|
if e.spiders and e.spiders[1].spider_entity.valid and give_tool(player, {name="squad-spidertron-remote",count=1}) then
|
||||||
|
player.cursor_stack.connected_entity=e.spiders[1].spider_entity
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
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 = 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 mov_offset_diagonal = math.sqrt(mov_offset^2/2)
|
||||||
|
|
||||||
|
local function pos_offset(position,dir)
|
||||||
|
local def_dir = defines.direction
|
||||||
|
local pos_x = position.x
|
||||||
|
local pos_y = position.y
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
return {x=pos_x, y=pos_y}
|
||||||
|
end
|
||||||
|
|
||||||
local update_interval = settings.global["spidertron-follow-update-interval"].value
|
local update_interval = settings.global["spidertron-follow-update-interval"].value
|
||||||
|
|
||||||
script.on_nth_tick(update_interval, function (event)
|
script.on_nth_tick(update_interval, function(event)
|
||||||
for _, player in pairs(game.players) do
|
for _, player in pairs(game.players) do
|
||||||
if player.is_shortcut_toggled("squad-spidertron-follow") and global.spidercontrol_spidersquad[player.index] then
|
if player.is_shortcut_toggled("squad-spidertron-follow") then
|
||||||
local index = player.index
|
local index = player.index
|
||||||
local pos = player.position
|
if global.spidercontrol_spidersquad[index].spiders[1] then
|
||||||
local pos_x = pos.x
|
local pos = player.position
|
||||||
local pos_y = pos.y
|
if player.walking_state.walking then
|
||||||
if player.walking_state.walking then
|
local dir = player.walking_state.direction
|
||||||
local dir = player.walking_state.direction
|
pos = pos_offset(pos,dir)
|
||||||
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
|
||||||
end
|
spiderbot_designate(index, pos)
|
||||||
spiderbot_designate(index, {x=pos_x, y=pos_y})
|
if player.vehicle then
|
||||||
if player.vehicle then
|
local vehicle = player.vehicle
|
||||||
local vehicle = player.vehicle
|
if vehicle.type == "spider-vehicle" then
|
||||||
if vehicle.type == "spider-vehicle" then
|
vehicle.autopilot_destination = pos
|
||||||
vehicle.autopilot_destination = squad_center(global.spidercontrol_spidersquad[index])
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "Spider_Control",
|
"name": "Spider_Control",
|
||||||
"version": "0.2.0",
|
"version": "0.3.0",
|
||||||
"factorio_version": "1.0",
|
"factorio_version": "1.0",
|
||||||
"title": "Spidertron squad control",
|
"title": "Spidertron squad control",
|
||||||
"author": "npc_strider(morley376)",
|
"author": "npc_strider(morley376)",
|
||||||
|
|
6
migrations/Spider_Control_0.3.0.lua
Normal file
6
migrations/Spider_Control_0.3.0.lua
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
if global.spidercontrol_spidersquad ~= nil then
|
||||||
|
for i,_ in pairs(global.spidercontrol_spidersquad) do
|
||||||
|
global.spidercontrol_spidersquad[i] = {spiders={}}
|
||||||
|
end
|
||||||
|
game.print("Spider control 0.3.0 migration: Reset squads")
|
||||||
|
end
|
|
@ -65,6 +65,11 @@ input_follow.name = "squad-spidertron-follow"
|
||||||
input_follow.localised_name = "Follow player"
|
input_follow.localised_name = "Follow player"
|
||||||
input_follow.key_sequence = "ALT + C"
|
input_follow.key_sequence = "ALT + C"
|
||||||
|
|
||||||
|
local input_switch_modes = util.table.deepcopy(input_remote)
|
||||||
|
input_switch_modes.name = "squad-spidertron-switch-modes"
|
||||||
|
input_switch_modes.localised_name = "Switch modes (between selecting and commanding)"
|
||||||
|
input_switch_modes.key_sequence = "mouse-button-2"
|
||||||
|
|
||||||
data:extend(
|
data:extend(
|
||||||
{
|
{
|
||||||
shortcut_remote,
|
shortcut_remote,
|
||||||
|
@ -111,5 +116,6 @@ data:extend(
|
||||||
},
|
},
|
||||||
|
|
||||||
input_remote,
|
input_remote,
|
||||||
input_follow
|
input_follow,
|
||||||
|
input_switch_modes
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue
Block a user