From e4997643bd5e14ad71664d329b3504a7e5ac466a Mon Sep 17 00:00:00 2001 From: Akuukis Date: Fri, 25 Oct 2024 22:05:57 +0300 Subject: [PATCH] feat: upgrade to Factorio v2 --- control.lua | 69 ++++++++++++++++++++++++------------------------- data.lua | 18 ++++++------- flying_text.lua | 5 ++-- info.json | 2 +- pack.sh | 0 shortcuts.lua | 38 +++++---------------------- 6 files changed, 52 insertions(+), 80 deletions(-) mode change 100755 => 100644 pack.sh diff --git a/control.lua b/control.lua index d12da16..2fb5d08 100644 --- a/control.lua +++ b/control.lua @@ -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 @@ -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 @@ -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 @@ -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) @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/data.lua b/data.lua index 23b2869..f5b766b 100644 --- a/data.lua +++ b/data.lua @@ -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 - } -} \ No newline at end of file +-- data:extend{ +-- { +-- type = "flying-text", +-- name = "autobuild-flying-text", +-- flags = {"not-on-map", "placeable-off-grid"}, +-- time_to_live = 150, +-- speed = 0.05 +-- } +-- } \ No newline at end of file diff --git a/flying_text.lua b/flying_text.lua index 61585e3..6892e8c 100644 --- a/flying_text.lua +++ b/flying_text.lua @@ -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 diff --git a/info.json b/info.json index 422c347..2cf4eb1 100644 --- a/info.json +++ b/info.json @@ -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", diff --git a/pack.sh b/pack.sh old mode 100755 new mode 100644 diff --git a/shortcuts.lua b/shortcuts.lua index ec56782..318c5ca 100644 --- a/shortcuts.lua +++ b/shortcuts.lua @@ -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", }, }