require("mod-gui") require("util") --serpent = require("stdlib.serpent") curvedRail = require("stdlib.curvedRail") --function init_debug() -- game.write_file("landfill_e.log", "Log file for Landfill Everything 0.0.1") --end -- --function debug(obj) -- game.write_file("landfill_e.log", serpent.block(obj), true) --end -- --function trace(text) -- for _, player in pairs(game.players) do -- player.print("LF - " .. text) -- end --end function rotate(box) local new_box = { left_top = { x = 0, y = 0 }, right_bottom = { x = 0, y = 0 } } new_box.left_top.x = box.left_top.y new_box.left_top.y = box.left_top.x new_box.right_bottom.x = box.right_bottom.y new_box.right_bottom.y = box.right_bottom.x return new_box end function add_landfill(blueprint,player) local entities = blueprint.get_blueprint_entities() local oldTiles = blueprint.get_blueprint_tiles() local do_tiles = settings.get_player_settings(player)["landfill-under-tiles"].value local landfillTile = {name = "landfill"} if game.active_mods["Dectorio"] then landfillTile.name = "dect-landfill" end local railOffset = 0 local tileIndex = 0 --pre-allocate all the prototypes local protos = {} local new_tiles = {} -- check for rails and cache prototypes for k = 1, #entities, 1 do local name = entities[k].name if protos[name] == nil then protos[name] = game.entity_prototypes[name] if railOffset == 0 and string.find(protos[name].type, "rail", 1, true) ~= nil then railOffset = 0.5 end end end if entities then for i, ent in pairs(entities) do local name = ent.name if "curved-rail" ~= name then -- special case for curved rail local box = protos[name].selection_box local pos = ent.position if protos[ent.name].collision_mask["ground-tile"] == nil then -- Rotate the box if needed if ent.direction == defines.direction.east or ent.direction == defines.direction.west then box = rotate(box) end local width = math.ceil(box.right_bottom.x - box.left_top.x) local height = math.ceil(box.right_bottom.y - box.left_top.y) local start_x = math.ceil(pos.x - railOffset) - math.floor(width / 2.0) local start_y = math.ceil(pos.y - railOffset) - math.floor(height / 2.0) for x = 0, width - 1, 1 do for y = 0, height - 1, 1 do new_tiles[tileIndex + 1] = { name = landfillTile.name, position = { start_x + x, start_y + y } } tileIndex = tileIndex + 1 end end end else -- curved Rail local dir = ent.direction if dir == nil then dir = 8 end local curveMask = getCurveMask(dir) local pos = ent.position for m = 1, #curveMask do local x = curveMask[m].x + pos.x local y = curveMask[m].y + pos.y new_tiles[tileIndex + 1] = { name = landfillTile.name, position = { x, y } } tileIndex = tileIndex + 1 end end end end if do_tiles then if oldTiles then for i, oldT in pairs(oldTiles) do local pos = oldT.position new_tiles[tileIndex + 1] = { name = landfillTile.name, position = { pos.x, pos.y } } tileIndex = tileIndex + 1 end end end return {tiles=new_tiles} end function get_le_flow(player) local button_flow = mod_gui.get_button_flow(player) local flow = button_flow.le_flow if not flow then flow = button_flow.add { type = "flow", name = "le_flow", direction = "horizontal" } end return flow end function add_top_button(player) if player.gui.top.le_flow then player.gui.top.le_flow.destroy() end -- remove the old flow local flow = get_le_flow(player) if flow["search_flow"] then flow["search_flow"].destroy() end local search_flow = flow.add { type = "flow", name = "search_flow", direction = "horizontal" } search_flow.add { type = "sprite-button", name = "le_button", sprite = "item/landfill", style = mod_gui.button_style, tooltip = { "le_tooltip" } } end function is_valid_slot(slot, state) if not slot or not slot.valid_for_read then return false end --if state then if state == "empty" then return not slot.is_blueprint_setup() elseif state == "setup" then return slot.is_blueprint_setup() end --end return true end function get_blueprint_on_cursor(player) local stack = player.cursor_stack if stack.valid_for_read then if (stack.type == "blueprint" and is_valid_slot(stack, 'setup')) then return stack elseif stack.type == "blueprint-book" then local active = stack.get_inventory(defines.inventory.item_main)[stack.active_index] if is_valid_slot(active, 'setup') then return active end end end return false end script.on_init(function() for _, player in pairs(game.players) do add_top_button(player) end end) script.on_event(defines.events.on_player_created, function(event) local player = game.players[event.player_index] add_top_button(player) end) script.on_event(defines.events.on_gui_click, function(event) local player = game.players[event.player_index] if event.element.name == "le_button" then if player.cursor_stack.valid_for_read then -- TODO: check that landfill has been researched local blueprint = get_blueprint_on_cursor(player) if blueprint then local modified = add_landfill(blueprint,player) blueprint.set_blueprint_tiles(modified.tiles) end end end end)