Skip to content

Commit

Permalink
Merge pull request #4 from jodokus31/queue_again
Browse files Browse the repository at this point in the history
Prevent interaction with entities out of reach
  • Loading branch information
jodokus31 authored May 22, 2023
2 parents 7943dc9 + 8a11ff0 commit 4deb0ee
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
7 changes: 7 additions & 0 deletions extended-fasttransfer/changelog.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
---------------------------------------------------------------------------------------------------
Version: 0.0.5
Date: 2023-05-22
Bugfixes:
- Prevent interaction with entities out of reach
- Play sound and emit "Cant reach" text, when entity is not reachable
- Slightly reduce interval to detect a drag.
---------------------------------------------------------------------------------------------------
Version: 0.0.4
Date: 2023-05-07
Features:
Expand Down
32 changes: 27 additions & 5 deletions extended-fasttransfer/control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,27 @@ function get_player_state(player_index)
return state
end

local entity_groups =
{
["assembling-machine"] = "assembling-machine",
["container"] = "container",
["logistic-container"] = "container",
["ammo-turret"] = "ammo-turret"
}

local function handle_action_on_entity(player, selected_entity, state, tick, is_from_drag)

local entity_group = entity_groups[selected_entity.type]
if not entity_group then
return nil
end

if not player.can_reach_entity(selected_entity) then
return nil
end

local flying_text_infos = nil
if selected_entity.type == "container" or selected_entity.type == "logistic-container" then
if entity_group == "container" then

local inventory = selected_entity.get_inventory(defines.inventory.chest)
if not player.cursor_stack or not player.cursor_stack.valid_for_read then
Expand All @@ -56,7 +73,7 @@ local function handle_action_on_entity(player, selected_entity, state, tick, is_
end
end

elseif selected_entity.type == "ammo-turret" then
elseif entity_group == "ammo-turret" then

local inventory = selected_entity.get_inventory(defines.inventory.turret_ammo)
if not player.cursor_stack or not player.cursor_stack.valid_for_read then
Expand All @@ -71,7 +88,7 @@ local function handle_action_on_entity(player, selected_entity, state, tick, is_
end
end

elseif selected_entity.type == "assembling-machine" then
elseif entity_group == "assembling-machine" then

local inventory = selected_entity.get_inventory(defines.inventory.assembling_machine_input)

Expand Down Expand Up @@ -131,12 +148,17 @@ local function common_custominput_handler(e)

logger.print(player, "should omit direct call?: "..tick
.." state.last_drag_action_happened: " .. (state.last_drag_action_happened or "nil"))

if entity_groups[selected_entity.type] and not player.can_reach_entity(selected_entity) then
player.play_sound({ path = "utility/cannot_build" })
flying_text.create_flying_text_entity_for_cant_reach(selected_entity)
return
end

if not state.last_drag_action_happened or (state.last_drag_action_happened + IGNORE_DIRECT_ACTION_AFTER_DRAG) <= tick then
handle_action_on_entity(player, selected_entity, state, tick, false)
state.last_drag_action_happened = nil
end

--actiontype.reset_action(state)
end

script.on_event(custom_inputs.topupplayerstacks, common_custominput_handler)
Expand Down
2 changes: 1 addition & 1 deletion extended-fasttransfer/info.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "extended-fasttransfer",
"version": "0.0.4",
"version": "0.0.5",
"title": "Extended Fast Transfer",
"author": "jodokus31",
"contact": "",
Expand Down
2 changes: 1 addition & 1 deletion extended-fasttransfer/scripts/actiontype.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local actiontype = {}

local TICKS_TO_DETECT_DRAG = 15
local TICKS_TO_DETECT_DRAG = 10

local logger = require("scripts/logger")

Expand Down
10 changes: 10 additions & 0 deletions extended-fasttransfer/scripts/flying_text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,14 @@ function flying_text.create_flying_text_entities(entity, flying_text_infos)
end
end

function flying_text.create_flying_text_entity_for_cant_reach(entity)
entity.surface.create_entity(
{
name = "extended-fasttransfer-flying-text",
position = { entity.position.x - 0.5, entity.position.y + (offset or 0) },
text = {"cant-reach"},
color = { r = 1, g = 1, b = 1 } -- white
})
end

return flying_text

0 comments on commit 4deb0ee

Please sign in to comment.