Skip to content

Commit

Permalink
feat: upgrade to Factorio v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuukis committed Oct 25, 2024
1 parent 8982c0c commit e499764
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 80 deletions.
69 changes: 34 additions & 35 deletions control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ end

local function change_visual_area(player, state, opacity)

if state.visual_area_id then
if rendering.is_valid(state.visual_area_id) then
rendering.destroy(state.visual_area_id)
end
state.visual_area_id = nil
if state.visual_area then
state.visual_area.destroy()
state.visual_area = nil
end

-- player.print("enable_visual_area ".. (state.enable_visual_area and "true" or "false"))
-- player.print("opacity ".. (opacity or "nil"))
-- player.print("player.character "..serpent.block(player.character))
-- player.print("build_distance "..(state.build_distance or "nil"))
-- if(player) then
-- player.print("enable_visual_area ".. (state.enable_visual_area and "true" or "false"))
-- player.print("opacity ".. (opacity or "nil"))
-- player.print("player.character "..serpent.block(player.character))
-- player.print("build_distance "..(state.build_distance or "nil"))
-- end

if not state.enable_visual_area then return end
if not opacity or opacity <= 0 then return end
Expand All @@ -55,17 +55,16 @@ local function change_visual_area(player, state, opacity)

local radius = state.build_distance + 0.5

state.visual_area_id = rendering.draw_rectangle({
state.visual_area = rendering.draw_rectangle({
surface = player.surface,
filled = true,
draw_on_ground = true,
color = { r=(opacity/200), g=(opacity/200), b=0, a=(opacity/200) },
left_top = player.character,
left_top_offset = { -1*radius, -1*radius },
right_bottom = player.character,
right_bottom_offset = { radius, radius },
left_top = { entity=player.character, offset={ -1*radius, -1*radius } },
right_bottom = { entity=player.character, offset={ radius, radius } },
players = { player },
})

end

-- on_character_swapped_event
Expand All @@ -81,18 +80,18 @@ local function on_character_swapped_event(event)
if not player.index then return end

local state = get_player_state(player.index)
if not state.visual_area_id then return end
if not state.visual_area then return end
if not state.build_distance then return end

local radius = state.build_distance + 0.5
if not rendering.is_valid(state.visual_area_id) then
state.visual_area_id = nil
if state.visual_area and not state.visual_area.valid() then
state.visual_area = nil
return
end

local target = rendering.get_left_top(state.visual_area_id)
local target = rendering.get_left_top(state.visual_area)
if target and target.entity and target.entity.unit_number == event.old_unit_number then
rendering.set_corners(state.visual_area_id,
rendering.set_corners(state.visual_area,
event.new_character, { -1*radius, -1*radius },
event.new_character, { radius, radius })
end
Expand All @@ -101,12 +100,12 @@ end
remote.add_interface("autobuild", { on_character_swapped = on_character_swapped_event } )

local function on_load()
player_state = global.player_state
player_state = storage.player_state
end
script.on_load(on_load)

local function on_init()
global.player_state = {}
storage.player_state = {}
on_load()
end
script.on_init(on_init)
Expand All @@ -121,7 +120,7 @@ local function on_configuration_changed()
player.set_shortcut_toggled(construction_toggle_name, false)
end
end
global.player_state = {}
storage.player_state = {}
cache = NDCache.new(Scanner.generator)
on_load()
end
Expand Down Expand Up @@ -269,10 +268,10 @@ local to_place_cache = {}
local function to_place(entity_name)
local stacks = to_place_cache[entity_name]
if not stacks then
local prototype = game.entity_prototypes[entity_name] or game.tile_prototypes[entity_name]
local prototype = prototypes.entity[entity_name] or prototypes.tile[entity_name]
stacks = prototype and prototype.items_to_place_this or {}
for _, stack in pairs(stacks) do
stack.prototype = game.item_prototypes[stack.name]
stack.prototype = prototypes.item[stack.name]
end
to_place_cache[entity_name] = stacks or {}
end
Expand Down Expand Up @@ -836,25 +835,25 @@ local build_actions =
local function is_assigned_to_other_robot(entity, action_type, force_name)

if action_type == ActionTypes.DECONSTRUCT or action_type == ActionTypes.DECONSTRUCT_TILE then
-- is_registered_for_deconstruction(force) -> boolean
-- Is this entity registered for deconstruction with this force?
-- If false, it means a construction robot has been dispatched to deconstruct it,
-- or it is not marked for deconstruction.
-- is_registered_for_deconstruction(force) -> boolean
-- Is this entity registered for deconstruction with this force?
-- If false, it means a construction robot has been dispatched to deconstruct it,
-- or it is not marked for deconstruction.
-- The complexity is effectively O(1) - it depends on the number of objects targeting this entity which should be small enough.
return not entity.is_registered_for_deconstruction(force_name or "no_force")

elseif action_type == ActionTypes.ENTITY_GHOST or action_type == ActionTypes.TILE_GHOST then
-- is_registered_for_construction() -> boolean
-- Is this entity or tile ghost or item request proxy registered for construction?
-- If false, it means a construction robot has been dispatched to build the entity,
-- is_registered_for_construction() -> boolean
-- Is this entity or tile ghost or item request proxy registered for construction?
-- If false, it means a construction robot has been dispatched to build the entity,
-- or it is not an entity that can be constructed.
return not entity.is_registered_for_construction()

elseif action_type == ActionTypes.UPGRADE then
-- is_registered_for_upgrade() -> boolean
-- Is this entity registered for upgrade?
-- If false, it means a construction robot has been dispatched to upgrade it,
-- or it is not marked for upgrade.
-- Is this entity registered for upgrade?
-- If false, it means a construction robot has been dispatched to upgrade it,
-- or it is not marked for upgrade.
-- This is worst-case O(N) complexity where N is the current number of things in the upgrade queue.
return not entity.is_registered_for_upgrade()
end
Expand Down Expand Up @@ -957,7 +956,7 @@ end
-- Determines, whether the area around the player should be rechecked (mainly from cache, but might also get rescanned, if something changed in the blueprint)
-- Also, the building candidates are getting reordered according to action_type and player position.
-- returns true -> yes: recheck
-- returns false -> no: don't recheck, build further on the old location
-- returns false -> no: don't recheck, build further on the old location
local function needs_recheck(state)
-- increment current_cycle
state.current_cycle = (state.current_cycle or 0) + 1
Expand Down
18 changes: 9 additions & 9 deletions data.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ require "custominput"
require "shortcuts"


data:extend{
{
type = "flying-text",
name = "autobuild-flying-text",
flags = {"not-on-map", "placeable-off-grid"},
time_to_live = 150,
speed = 0.05
}
}
-- data:extend{
-- {
-- type = "flying-text",
-- name = "autobuild-flying-text",
-- flags = {"not-on-map", "placeable-off-grid"},
-- time_to_live = 150,
-- speed = 0.05
-- }
-- }
5 changes: 2 additions & 3 deletions flying_text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ function flying_text.create_flying_text_entities(surface, position, flying_text_
local offset = 0
for itemname, info in pairs(flying_text_infos) do
local sign = (info.amount > 0) and " +" or " "
surface.create_entity(
player.create_local_flying_text(
{
name = "autobuild-flying-text",
position = { position.x - 0.5 , position.y + (offset or 0) },
text = {"", sign, info.amount, " ", game.item_prototypes[itemname].localised_name, " (", info.total ,")"},
text = {"", sign, info.amount, " ", prototypes.item[itemname].localised_name, " (", info.total ,")"},
color = { r = 1, g = 1, b = 1 } -- white
})
offset = offset - 0.5
Expand Down
2 changes: 1 addition & 1 deletion info.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "autobuild",
"version": "0.5.9",
"factorio_version": "1.1",
"factorio_version": "2.0",
"title": "Autobuild",
"author": "Therax",
"homepage": "https://mods.factorio.com/mod/autobuild",
Expand Down
Empty file modified pack.sh
100755 → 100644
Empty file.
38 changes: 6 additions & 32 deletions shortcuts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,11 @@ data:extend{
action = "lua",
toggleable = true,
localised_name = {"autobuild-shortcut.autobuild-shortcut-toggle-construction"},
icon =
{
filename = "__autobuild__/graphics/wrench-x32.png",
priority = "extra-high-no-scale",
size = 32,
scale = 1,
flags = {"icon"}
},
small_icon =
{
filename = "__autobuild__/graphics/wrench-x24.png",
priority = "extra-high-no-scale",
size = 24,
scale = 1,
flags = {"icon"}
},
disabled_icon =
{
filename = "__autobuild__/graphics/wrench-x32-white.png",
priority = "extra-high-no-scale",
size = 32,
scale = 1,
flags = {"icon"}
},
disabled_small_icon =
{
filename = "__autobuild__/graphics/wrench-x24-white.png",
priority = "extra-high-no-scale",
size = 24,
scale = 1,
flags = {"icon"}
},
icon = "__autobuild__/graphics/wrench-x32.png",
icon_size = 32,
small_icon = "__autobuild__/graphics/wrench-x24.png",
small_icon_size = 24,
disabled_icon = "__autobuild__/graphics/wrench-x32-white.png",
disabled_small_icon = "__autobuild__/graphics/wrench-x24-white.png",
},
}

0 comments on commit e499764

Please sign in to comment.