diff --git a/control.lua b/control.lua index 80fc68c0..68e22c33 100644 --- a/control.lua +++ b/control.lua @@ -1,7 +1,7 @@ --Main file for mod runtime -local util = require('util') -local fa_utils = require('scripts.fa-utils') -local fa_localising = require('scripts.localising') +local util = require("util") +local fa_utils = require("scripts.fa-utils") +local fa_localising = require("scripts.localising") local fa_crafting = require("scripts.crafting") local fa_electrical = require("scripts.electrical") local fa_equipment = require("scripts.equipment") @@ -21,14 +21,14 @@ local fa_driving = require("scripts.driving") local fa_scanner = require("scripts.scanner") local fa_spidertrons = require("scripts.spidertron") local fa_belts = require("scripts.transport-belts") -local fa_zoom = require('scripts.zoom') +local fa_zoom = require("scripts.zoom") local fa_bot_logistics = require("scripts.worker-robots") local fa_blueprints = require("scripts.blueprints") local fa_travel = require("scripts.travel-tools") local fa_teleport = require("scripts.teleport") local fa_warnings = require("scripts.warnings") -local circuit_networks = require('scripts.circuit-networks') +local circuit_networks = require("scripts.circuit-networks") groups = {} entity_types = {} @@ -36,9 +36,57 @@ production_types = {} building_types = {} local dirs = defines.direction -ENT_NAMES_CLEARED_AS_OBSTACLES = {"tree-01-stump","tree-02-stump","tree-03-stump","tree-04-stump","tree-05-stump","tree-06-stump","tree-07-stump","tree-08-stump","tree-09-stump","small-scorchmark","small-scorchmark-tintable","medium-scorchmark","medium-scorchmark-tintable","big-scorchmark","big-scorchmark-tintable","huge-scorchmark","huge-scorchmark-tintable","rock-big","rock-huge","sand-rock-big"} -ENT_TYPES_YOU_CAN_WALK_OVER = {"resource", "transport-belt", "underground-belt", "splitter", "item-entity", "entity-ghost", "heat-pipe", "pipe", "pipe-to-ground", "character", "rail-signal", "flying-text", "highlight-box", "combat-robot", "logistic-robot", "construction-robot", "rocket-silo-rocket-shadow" } -ENT_TYPES_YOU_CAN_BUILD_OVER = {"resource", "entity-ghost", "flying-text", "highlight-box", "combat-robot", "logistic-robot", "construction-robot", "rocket-silo-rocket-shadow"} +ENT_NAMES_CLEARED_AS_OBSTACLES = { + "tree-01-stump", + "tree-02-stump", + "tree-03-stump", + "tree-04-stump", + "tree-05-stump", + "tree-06-stump", + "tree-07-stump", + "tree-08-stump", + "tree-09-stump", + "small-scorchmark", + "small-scorchmark-tintable", + "medium-scorchmark", + "medium-scorchmark-tintable", + "big-scorchmark", + "big-scorchmark-tintable", + "huge-scorchmark", + "huge-scorchmark-tintable", + "rock-big", + "rock-huge", + "sand-rock-big", +} +ENT_TYPES_YOU_CAN_WALK_OVER = { + "resource", + "transport-belt", + "underground-belt", + "splitter", + "item-entity", + "entity-ghost", + "heat-pipe", + "pipe", + "pipe-to-ground", + "character", + "rail-signal", + "flying-text", + "highlight-box", + "combat-robot", + "logistic-robot", + "construction-robot", + "rocket-silo-rocket-shadow", +} +ENT_TYPES_YOU_CAN_BUILD_OVER = { + "resource", + "entity-ghost", + "flying-text", + "highlight-box", + "combat-robot", + "logistic-robot", + "construction-robot", + "rocket-silo-rocket-shadow", +} --This function gets scheduled. function call_to_fix_zoom(pindex) @@ -67,44 +115,32 @@ end --Returns the entity at this player's cursor selected tile function get_selected_ent(pindex) - local tile=players[pindex].tile + local tile = players[pindex].tile local ent while true do - if tile.index > #tile.ents then - tile.index = #tile.ents - end - if tile.index == 0 then - return nil - end + if tile.index > #tile.ents then tile.index = #tile.ents end + if tile.index == 0 then return nil end ent = tile.ents[tile.index] - if not ent then - print(serpent.line(tile.ents),tile.index,ent) - end + if not ent then print(serpent.line(tile.ents), tile.index, ent) end -- if ent.valid then - -- game.print(ent.name) + -- game.print(ent.name) -- end - if ent.valid and (ent.type ~= 'character' or players[pindex].cursor or ent.player ~= pindex) then - return ent - end - table.remove(tile.ents,tile.index) + if ent.valid and (ent.type ~= "character" or players[pindex].cursor or ent.player ~= pindex) then return ent end + table.remove(tile.ents, tile.index) end end --Outputs basic entity info, usually called when the cursor selects an entity. function ent_info(pindex, ent, description) local p = game.get_player(pindex) - local result = fa_localising.get(ent,pindex) - if result == nil or result == "" then - result = ent.name - end - if game.players[pindex].name == "Crimso" then - result = result .. " " .. ent.type .. " " - end + local result = fa_localising.get(ent, pindex) + if result == nil or result == "" then result = ent.name end + if game.players[pindex].name == "Crimso" then result = result .. " " .. ent.type .. " " end if ent.type == "resource" then if ent.name ~= "crude-oil" then result = result .. ", x " .. ent.amount else - result = result .. ", x " .. math.floor(ent.amount/3000) .. "%" + result = result .. ", x " .. math.floor(ent.amount / 3000) .. "%" end end if ent.name == "entity-ghost" then @@ -131,10 +167,7 @@ function ent_info(pindex, ent, description) result = result .. " X " end - if p ~= nil and p.valid and p.index == pindex and not players[pindex].cursor then - return "" - end - + if p ~= nil and p.valid and p.index == pindex and not players[pindex].cursor then return "" end elseif ent.name == "character-corpse" then if ent.character_corpse_player_index == pindex then result = result .. " of your character " @@ -147,7 +180,7 @@ function ent_info(pindex, ent, description) local itemset = ent.get_inventory(defines.inventory.chest).get_contents() local itemtable = {} for name, count in pairs(itemset) do - table.insert(itemtable, {name = name, count = count}) + table.insert(itemtable, { name = name, count = count }) end table.sort(itemtable, function(k1, k2) return k1.count > k2.count @@ -155,16 +188,29 @@ function ent_info(pindex, ent, description) if #itemtable == 0 then result = result .. " with nothing " else - result = result .. " with " .. fa_localising.get_item_from_name(itemtable[1].name,pindex) .. " times " .. itemtable[1].count .. ", " + result = result + .. " with " + .. fa_localising.get_item_from_name(itemtable[1].name, pindex) + .. " times " + .. itemtable[1].count + .. ", " if #itemtable > 1 then - result = result .. " and " .. fa_localising.get_item_from_name(itemtable[2].name,pindex) .. " times " .. itemtable[2].count .. ", " + result = result + .. " and " + .. fa_localising.get_item_from_name(itemtable[2].name, pindex) + .. " times " + .. itemtable[2].count + .. ", " end if #itemtable > 2 then - result = result .. " and " .. fa_localising.get_item_from_name(itemtable[3].name,pindex) .. " times " .. itemtable[3].count .. ", " - end - if #itemtable > 3 then - result = result .. "and other items " + result = result + .. " and " + .. fa_localising.get_item_from_name(itemtable[3].name, pindex) + .. " times " + .. itemtable[3].count + .. ", " end + if #itemtable > 3 then result = result .. "and other items " end end if ent.type == "logistic-container" then local network = ent.surface.find_logistic_network_by_position(ent.position, ent.force) @@ -174,8 +220,15 @@ function ent_info(pindex, ent, description) result = result .. ", not in a network, no networks found within 5000 tiles" else local dist = math.ceil(util.distance(ent.position, nearest_roboport.position) - 25) - local dir = fa_utils.direction_lookup(fa_utils.get_direction_biased(nearest_roboport.position, ent.position)) - result = result .. ", not in a network, nearest network " .. nearest_roboport.backer_name .. " is about " .. dist .. " to the " .. dir + local dir = + fa_utils.direction_lookup(fa_utils.get_direction_biased(nearest_roboport.position, ent.position)) + result = result + .. ", not in a network, nearest network " + .. nearest_roboport.backer_name + .. " is about " + .. dist + .. " to the " + .. dir end else local network_name = network.cells[1].owner.backer_name @@ -184,27 +237,31 @@ function ent_info(pindex, ent, description) end end --Pipe ends are labelled to distinguish them - if ent.name == "pipe" and fa_building_tools.is_a_pipe_end(ent,pindex) then - result = result .. " end, " - end + if ent.name == "pipe" and fa_building_tools.is_a_pipe_end(ent, pindex) then result = result .. " end, " end --Explain the contents of a pipe or storage tank or etc. - if ent.type == "pipe" or ent.type == "pipe-to-ground" or ent.type == "storage-tank" or ent.type == "pump" or ent.name == "boiler" or ent.name == "heat-exchanger" or ent.type == "generator" then + if + ent.type == "pipe" + or ent.type == "pipe-to-ground" + or ent.type == "storage-tank" + or ent.type == "pump" + or ent.name == "boiler" + or ent.name == "heat-exchanger" + or ent.type == "generator" + then local dict = ent.get_fluid_contents() local fluids = {} for name, count in pairs(dict) do - table.insert(fluids, {name = name, count = count}) + table.insert(fluids, { name = name, count = count }) end table.sort(fluids, function(k1, k2) return k1.count > k2.count end) if #fluids > 0 and fluids[1].count ~= nil then - result = result .. " with " .. fa_localising.get_fluid_from_name(fluids[1].name,pindex) --can check amount by opening the ent menu - if #fluids > 1 and fluids[2].count ~= nil then - result = result .. " and " .. fa_localising.get_fluid_from_name(fluids[2].name,pindex) --(this should not happen because it means different fluids mixed!) - end - if #fluids > 2 then - result = result .. ", and other fluids " - end + result = result .. " with " .. fa_localising.get_fluid_from_name(fluids[1].name, pindex) --can check amount by opening the ent menu + if #fluids > 1 and fluids[2].count ~= nil then + result = result .. " and " .. fa_localising.get_fluid_from_name(fluids[2].name, pindex) --(this should not happen because it means different fluids mixed!) + end + if #fluids > 2 then result = result .. ", and other fluids " end else result = result .. " empty " end @@ -229,14 +286,23 @@ function ent_info(pindex, ent, description) end for i, belt in pairs(outputs) do outload_count = outload_count + 1 - outload_dir = belt.direction--Note: there should be only one of these belts anyway.2 + outload_dir = belt.direction --Note: there should be only one of these belts anyway.2 if belt.type == "transport-belt" and (belt.belt_shape == "right" or belt.belt_shape == "left") then outload_is_corner = true end end --Check what the neighbor info reveals about the belt local say_middle = false - result = result .. fa_belts.transport_belt_junction_info(sideload_count, backload_count, outload_count, this_dir, outload_dir, say_middle, outload_is_corner) + result = result + .. fa_belts.transport_belt_junction_info( + sideload_count, + backload_count, + outload_count, + this_dir, + outload_dir, + say_middle, + outload_is_corner + ) --Check contents local left = ent.get_transport_line(1).get_contents() @@ -251,26 +317,28 @@ function ent_info(pindex, ent, description) end local contents = {} for name, count in pairs(left) do - table.insert(contents, {name = name, count = count}) + table.insert(contents, { name = name, count = count }) end table.sort(contents, function(k1, k2) return k1.count > k2.count end) if #contents > 0 then - result = result .. " carrying " .. fa_localising.get_item_from_name(contents[1].name,pindex)--***localize + result = result .. " carrying " .. fa_localising.get_item_from_name(contents[1].name, pindex) --***localize if #contents > 1 then - result = result .. ", and " .. fa_localising.get_item_from_name(contents[2].name,pindex) - if #contents > 2 then - result = result .. ", and other item types " - end + result = result .. ", and " .. fa_localising.get_item_from_name(contents[2].name, pindex) + if #contents > 2 then result = result .. ", and other item types " end end - else --No currently carried items: Now try to announce likely recently carried items by checking the next belt over (must have only this belt as input) local next_belt = ent.belt_neighbours["outputs"][1] --Check contents of next belt local next_contents = {} - if next_belt ~= nil and next_belt.valid and #next_belt.belt_neighbours["inputs"] == 1 and next_belt.name ~= "entity-ghost" then + if + next_belt ~= nil + and next_belt.valid + and #next_belt.belt_neighbours["inputs"] == 1 + and next_belt.name ~= "entity-ghost" + then local left = next_belt.get_transport_line(1).get_contents() local right = next_belt.get_transport_line(2).get_contents() @@ -282,7 +350,7 @@ function ent_info(pindex, ent, description) end end for name, count in pairs(left) do - table.insert(next_contents, {name = name, count = count}) + table.insert(next_contents, { name = name, count = count }) end table.sort(next_contents, function(k1, k2) return k1.count > k2.count @@ -293,7 +361,7 @@ function ent_info(pindex, ent, description) local prev_belts = ent.belt_neighbours["inputs"] local prev_contents = {} for i, prev_belt in ipairs(prev_belts) do - --Check contents + --Check contents if prev_belt ~= nil and prev_belt.valid and prev_belt.name ~= "entity-ghost" then local left = prev_belt.get_transport_line(1).get_contents() local right = prev_belt.get_transport_line(2).get_contents() @@ -306,7 +374,7 @@ function ent_info(pindex, ent, description) end end for name, count in pairs(left) do - table.insert(prev_contents, {name = name, count = count}) + table.insert(prev_contents, { name = name, count = count }) end table.sort(prev_contents, function(k1, k2) return k1.count > k2.count @@ -314,26 +382,22 @@ function ent_info(pindex, ent, description) end end - --Report assumed carried items based on input/output neighbors + --Report assumed carried items based on input/output neighbors if #next_contents > 0 then - result = result .. " assumed carrying " .. fa_localising.get_item_from_name(next_contents[1].name,pindex) + result = result .. " assumed carrying " .. fa_localising.get_item_from_name(next_contents[1].name, pindex) if #next_contents > 1 then - result = result .. ", and " .. fa_localising.get_item_from_name(next_contents[2].name,pindex) - if #next_contents > 2 then - result = result .. ", and other item types " - end + result = result .. ", and " .. fa_localising.get_item_from_name(next_contents[2].name, pindex) + if #next_contents > 2 then result = result .. ", and other item types " end end elseif #prev_contents > 0 then - result = result .. " assumed carrying " .. fa_localising.get_item_from_name(prev_contents[1].name,pindex) + result = result .. " assumed carrying " .. fa_localising.get_item_from_name(prev_contents[1].name, pindex) if #prev_contents > 1 then - result = result .. ", and " .. fa_localising.get_item_from_name(prev_contents[2].name,pindex) - if #prev_contents > 2 then - result = result .. ", and other item types " - end + result = result .. ", and " .. fa_localising.get_item_from_name(prev_contents[2].name, pindex) + if #prev_contents > 2 then result = result .. ", and other item types " end end else --No currently or recently carried items - result = result .. " carrying nothing, " + result = result .. " carrying nothing, " end end end @@ -341,10 +405,10 @@ function ent_info(pindex, ent, description) --For underground belts, note whether entrance or Exited if ent.type == "underground-belt" then if ent.belt_to_ground_type == "input" then - result = result .. " entrance " - elseif ent.belt_to_ground_type == "output" then - result = result .. " exit " - end + result = result .. " entrance " + elseif ent.belt_to_ground_type == "output" then + result = result .. " exit " + end end --Explain the recipe of a machine without pause and before the direction @@ -361,10 +425,13 @@ function ent_info(pindex, ent, description) result = result .. " of train " .. fa_trains.get_train_name(ent.train) end --Report the entity facing direction - if (ent.prototype.is_building and ent.supports_direction) or (ent.name == "entity-ghost" and ent.ghost_prototype.is_building and ent.ghost_prototype.supports_direction) then + if + (ent.prototype.is_building and ent.supports_direction) + or (ent.name == "entity-ghost" and ent.ghost_prototype.is_building and ent.ghost_prototype.supports_direction) + then result = result .. ", Facing " .. fa_utils.direction_lookup(ent.direction) if ent.type == "generator" then - --For steam engines and steam turbines, north = south and east = west + --For steam engines and steam turbines, north = south and east = west result = result .. " and " .. fa_utils.direction_lookup(fa_utils.rotate_180(ent.direction)) end elseif ent.type == "locomotive" or ent.type == "car" then @@ -381,24 +448,36 @@ function ent_info(pindex, ent, description) result = result .. ", " local power1 = ent.energy_generated_last_tick * 60 local power2 = ent.prototype.max_energy_production * 60 - local power_load_pct = math.ceil(power1/power2 * 100) + local power_load_pct = math.ceil(power1 / power2 * 100) if power2 ~= nil then - result = result .. " at " .. power_load_pct .. " percent load, producing " .. fa_electrical.get_power_string(power1) .. " out of " .. fa_electrical.get_power_string(power2) .. " capacity, " + result = result + .. " at " + .. power_load_pct + .. " percent load, producing " + .. fa_electrical.get_power_string(power1) + .. " out of " + .. fa_electrical.get_power_string(power2) + .. " capacity, " else result = result .. " producing " .. fa_electrical.get_power_string(power1) .. " " end end if ent.type == "underground-belt" then if ent.neighbours ~= nil then - result = result .. ", Connected to " .. fa_utils.direction(ent.position, ent.neighbours.position) .. " via " .. math.floor(fa_utils.distance(ent.position, ent.neighbours.position)) - 1 .. " tiles underground, " + result = result + .. ", Connected to " + .. fa_utils.direction(ent.position, ent.neighbours.position) + .. " via " + .. math.floor(fa_utils.distance(ent.position, ent.neighbours.position)) - 1 + .. " tiles underground, " else result = result .. ", not connected " end elseif ent.type == "splitter" then --Splitter priority info result = result .. fa_belts.splitter_priority_info(ent) - elseif (ent.name == "pipe") and ent.neighbours ~= nil then - --List connected neighbors + elseif (ent.name == "pipe") and ent.neighbours ~= nil then + --List connected neighbors result = result .. " connects " local con_counter = 0 for i, nbrs in pairs(ent.neighbours) do @@ -406,16 +485,20 @@ function ent_info(pindex, ent, description) local box = nil local f_name = nil local dir_from_pos = nil - box, f_name, dir_from_pos = fa_building_tools.get_relevant_fluidbox_and_fluid_name(nbr, ent.position, dirs.north) - --Extra checks for pipes to ground + box, f_name, dir_from_pos = + fa_building_tools.get_relevant_fluidbox_and_fluid_name(nbr, ent.position, dirs.north) + --Extra checks for pipes to ground if f_name == nil then - box, f_name, dir_from_pos = fa_building_tools.get_relevant_fluidbox_and_fluid_name(nbr, ent.position, dirs.east) + box, f_name, dir_from_pos = + fa_building_tools.get_relevant_fluidbox_and_fluid_name(nbr, ent.position, dirs.east) end if f_name == nil then - box, f_name, dir_from_pos = fa_building_tools.get_relevant_fluidbox_and_fluid_name(nbr, ent.position, dirs.south) + box, f_name, dir_from_pos = + fa_building_tools.get_relevant_fluidbox_and_fluid_name(nbr, ent.position, dirs.south) end if f_name == nil then - box, f_name, dir_from_pos = fa_building_tools.get_relevant_fluidbox_and_fluid_name(nbr, ent.position, dirs.west) + box, f_name, dir_from_pos = + fa_building_tools.get_relevant_fluidbox_and_fluid_name(nbr, ent.position, dirs.west) end if f_name ~= nil then --"empty" is a name too result = result .. fa_utils.direction_lookup(dir_from_pos) .. ", " @@ -424,18 +507,18 @@ function ent_info(pindex, ent, description) end end end - if con_counter == 0 then - result = result .. " to nothing" - end + if con_counter == 0 then result = result .. " to nothing" end elseif (ent.name == "pipe-to-ground") and ent.neighbours ~= nil then result = result .. " connects " local connections = ent.fluidbox.get_pipe_connections(1) local aboveground_found = false local underground_found = false - for i,con in ipairs(connections) do + for i, con in ipairs(connections) do if con.target ~= nil then - local dist = math.ceil(util.distance(ent.position,con.target.get_pipe_connections(1)[1].position)) - result = result .. fa_utils.direction_lookup(fa_utils.get_direction_biased(con.target_position,ent.position)) .. " " + local dist = math.ceil(util.distance(ent.position, con.target.get_pipe_connections(1)[1].position)) + result = result + .. fa_utils.direction_lookup(fa_utils.get_direction_biased(con.target_position, ent.position)) + .. " " if con.connection_type == "underground" then result = result .. " via " .. dist - 1 .. " tiles underground, " underground_found = true @@ -455,17 +538,16 @@ function ent_info(pindex, ent, description) end elseif next(ent.prototype.fluidbox_prototypes) ~= nil then --For a fluidbox inside a building, give info about the connection directions - local relative_position = {x = players[pindex].cursor_pos.x - ent.position.x, y = players[pindex].cursor_pos.y - ent.position.y} - local direction = ent.direction/2 + local relative_position = + { x = players[pindex].cursor_pos.x - ent.position.x, y = players[pindex].cursor_pos.y - ent.position.y } + local direction = ent.direction / 2 local inputs = 0 for i, box in pairs(ent.prototype.fluidbox_prototypes) do for i1, pipe in pairs(box.pipe_connections) do - if pipe.type == "input" then - inputs = inputs + 1 - end - local adjusted = {position = nil, direction = nil} + if pipe.type == "input" then inputs = inputs + 1 end + local adjusted = { position = nil, direction = nil } if ent.name == "offshore-pump" then - adjusted.position = {x = 0, y = 0} + adjusted.position = { x = 0, y = 0 } if direction == 0 then adjusted.direction = "South" elseif direction == 1 then @@ -482,55 +564,109 @@ function ent_info(pindex, ent, description) if ent.type == "assembling-machine" and ent.get_recipe() ~= nil then if ent.name == "oil-refinery" and ent.get_recipe().name == "basic-oil-processing" then if i == 2 then - result = result .. ", " .. fa_localising.get_fluid_from_name("crude-oil",pindex) .. " Flow " .. pipe.type .. " 1 " .. adjusted.direction .. ", at " .. fa_utils.get_entity_part_at_cursor(pindex) + result = result + .. ", " + .. fa_localising.get_fluid_from_name("crude-oil", pindex) + .. " Flow " + .. pipe.type + .. " 1 " + .. adjusted.direction + .. ", at " + .. fa_utils.get_entity_part_at_cursor(pindex) elseif i == 5 then - result = result .. ", " .. fa_localising.get_fluid_from_name("petroleum-gas",pindex) .. " Flow " .. pipe.type .. " 1 " .. adjusted.direction .. ", at " .. fa_utils.get_entity_part_at_cursor(pindex) + result = result + .. ", " + .. fa_localising.get_fluid_from_name("petroleum-gas", pindex) + .. " Flow " + .. pipe.type + .. " 1 " + .. adjusted.direction + .. ", at " + .. fa_utils.get_entity_part_at_cursor(pindex) else - result = result .. ", " .. "Unused" .. " Flow " .. pipe.type .. " 1 " .. adjusted.direction .. ", at " .. fa_utils.get_entity_part_at_cursor(pindex) + result = result + .. ", " + .. "Unused" + .. " Flow " + .. pipe.type + .. " 1 " + .. adjusted.direction + .. ", at " + .. fa_utils.get_entity_part_at_cursor(pindex) end else if pipe.type == "input" then local inputs = ent.get_recipe().ingredients for i2 = #inputs, 1, -1 do - if inputs[i2].type ~= "fluid" then - table.remove(inputs, i2) - end + if inputs[i2].type ~= "fluid" then table.remove(inputs, i2) end end if #inputs > 0 then - local i3 = (i%#inputs) - if i3 == 0 then - i3 = #inputs - end + local i3 = (i % #inputs) + if i3 == 0 then i3 = #inputs end local filter = inputs[i3] - result = result .. ", " .. filter.name .. " Flow " .. pipe.type .. " 1 " .. adjusted.direction .. ", at " .. fa_utils.get_entity_part_at_cursor(pindex) + result = result + .. ", " + .. filter.name + .. " Flow " + .. pipe.type + .. " 1 " + .. adjusted.direction + .. ", at " + .. fa_utils.get_entity_part_at_cursor(pindex) else - result = result .. ", " .. "Unused" .. " Flow " .. pipe.type .. " 1 " .. adjusted.direction .. ", at " .. fa_utils.get_entity_part_at_cursor(pindex) + result = result + .. ", " + .. "Unused" + .. " Flow " + .. pipe.type + .. " 1 " + .. adjusted.direction + .. ", at " + .. fa_utils.get_entity_part_at_cursor(pindex) end else local outputs = ent.get_recipe().products for i2 = #outputs, 1, -1 do - if outputs[i2].type ~= "fluid" then - table.remove(outputs, i2) - end + if outputs[i2].type ~= "fluid" then table.remove(outputs, i2) end end if #outputs > 0 then - local i3 = ((i-inputs)%#outputs) - if i3 == 0 then - i3 = #outputs - end + local i3 = ((i - inputs) % #outputs) + if i3 == 0 then i3 = #outputs end local filter = outputs[i3] - result = result .. ", " .. filter.name .. " Flow " .. pipe.type .. " 1 " .. adjusted.direction .. ", at " .. fa_utils.get_entity_part_at_cursor(pindex) + result = result + .. ", " + .. filter.name + .. " Flow " + .. pipe.type + .. " 1 " + .. adjusted.direction + .. ", at " + .. fa_utils.get_entity_part_at_cursor(pindex) else - result = result .. ", " .. "Unused" .. " Flow " .. pipe.type .. " 1 " .. adjusted.direction .. ", at " .. fa_utils.get_entity_part_at_cursor(pindex) + result = result + .. ", " + .. "Unused" + .. " Flow " + .. pipe.type + .. " 1 " + .. adjusted.direction + .. ", at " + .. fa_utils.get_entity_part_at_cursor(pindex) end - end end - else --Other ent types and assembling machines with no recipes - local filter = box.filter or {name = ""} - result = result .. ", " .. filter.name .. " Flow " .. pipe.type .. " 1 " .. adjusted.direction .. ", at " .. fa_utils.get_entity_part_at_cursor(pindex) + local filter = box.filter or { name = "" } + result = result + .. ", " + .. filter.name + .. " Flow " + .. pipe.type + .. " 1 " + .. adjusted.direction + .. ", at " + .. fa_utils.get_entity_part_at_cursor(pindex) end end end @@ -561,11 +697,11 @@ function ent_info(pindex, ent, description) local insert_spots_left = 0 local insert_spots_right = 0 if not left.can_insert_at_back() and right.can_insert_at_back() then - result = result .. ", " .. left_dir .. " lane full and stopped, " + result = result .. ", " .. left_dir .. " lane full and stopped, " elseif left.can_insert_at_back() and not right.can_insert_at_back() then - result = result .. ", " .. right_dir .. " lane full and stopped, " + result = result .. ", " .. right_dir .. " lane full and stopped, " elseif not left.can_insert_at_back() and not right.can_insert_at_back() then - result = result .. ", both lanes full and stopped, " + result = result .. ", both lanes full and stopped, " --game.get_player(pindex).print(", both lanes full and stopped, ") else result = result .. ", both lanes open, " @@ -576,7 +712,7 @@ function ent_info(pindex, ent, description) local itemset = ent.get_inventory(defines.inventory.cargo_wagon).get_contents() local itemtable = {} for name, count in pairs(itemset) do - table.insert(itemtable, {name = name, count = count}) + table.insert(itemtable, { name = name, count = count }) end table.sort(itemtable, function(k1, k2) return k1.count > k2.count @@ -588,21 +724,19 @@ function ent_info(pindex, ent, description) if #itemtable > 1 then result = result .. " and " .. itemtable[2].name .. " times " .. itemtable[2].count .. ", " end - if #itemtable > 2 then - result = result .. "and other items " - end + if #itemtable > 2 then result = result .. "and other items " end end elseif ent.type == "radar" then result = result .. ", " .. radar_charting_info(ent) --game.print(result)--test elseif ent.type == "electric-pole" then - --List connected wire neighbors + --List connected wire neighbors result = result .. wire_neighbours_info(ent, false) --Count number of entities being supplied within supply area. local pos = ent.position local sdist = ent.prototype.supply_area_distance - local supply_area = {{pos.x - sdist, pos.y - sdist}, {pos.x + sdist, pos.y + sdist}} - local supplied_ents = ent.surface.find_entities_filtered{area = supply_area} + local supply_area = { { pos.x - sdist, pos.y - sdist }, { pos.x + sdist, pos.y + sdist } } + local supplied_ents = ent.surface.find_entities_filtered({ area = supply_area }) local supplied_count = 0 local producer_count = 0 for i, ent2 in ipairs(supplied_ents) do @@ -613,9 +747,7 @@ function ent_info(pindex, ent, description) end end result = result .. " supplying " .. supplied_count .. " buildings, " - if producer_count > 0 then - result = result .. " drawing from " .. producer_count .. " buildings, " - end + if producer_count > 0 then result = result .. " drawing from " .. producer_count .. " buildings, " end result = result .. "Check status for power flow information. " elseif ent.type == "power-switch" then if ent.power_switch_state == false then @@ -623,32 +755,31 @@ function ent_info(pindex, ent, description) elseif ent.power_switch_state == true then result = result .. " on, " end - if (#ent.neighbours.red + #ent.neighbours.green) > 0 then - result = result .. " observes circuit condition, " - end - result = result .. wire_neighbours_info(ent,true) + if (#ent.neighbours.red + #ent.neighbours.green) > 0 then result = result .. " observes circuit condition, " end + result = result .. wire_neighbours_info(ent, true) elseif ent.name == "rail-signal" or ent.name == "rail-chain-signal" then result = result .. ", " .. fa_rails.get_signal_state_info(ent) elseif ent.name == "roboport" then local cell = ent.logistic_cell local network = ent.logistic_cell.logistic_network - result = result .. " of network " .. fa_bot_logistics.get_network_name(ent) .. "," .. fa_bot_logistics.roboport_contents_info(ent) + result = result + .. " of network " + .. fa_bot_logistics.get_network_name(ent) + .. "," + .. fa_bot_logistics.roboport_contents_info(ent) elseif ent.type == "spider-vehicle" then local label = ent.entity_label - if label == nil then - label = "" - end + if label == nil then label = "" end result = result .. label elseif ent.type == "spider-leg" then - local spiders = ent.surface.find_entities_filtered{position = ent.position, radius = 5, type = "spider-vehicle"} - local spider = ent.surface.get_closest(ent.position, spiders) + local spiders = + ent.surface.find_entities_filtered({ position = ent.position, radius = 5, type = "spider-vehicle" }) + local spider = ent.surface.get_closest(ent.position, spiders) local label = spider.entity_label - if label == nil then - label = "" - end + if label == nil then label = "" end result = result .. label end - --Inserters: Explain held items, pickup and drop positions + --Inserters: Explain held items, pickup and drop positions if ent.type == "inserter" then --Declare filters if ent.filter_slot_count > 0 then @@ -658,70 +789,62 @@ function ent_info(pindex, ent, description) local filt = ent.get_filter(i) if filt ~= nil then active_filter_count = active_filter_count + 1 - if active_filter_count > 1 then - filter_result = filter_result .. " and " - end - local local_name = fa_localising.get(game.item_prototypes[filt],pindex) - if local_name == nil then - local_name = filt or " unknown item " - end + if active_filter_count > 1 then filter_result = filter_result .. " and " end + local local_name = fa_localising.get(game.item_prototypes[filt], pindex) + if local_name == nil then local_name = filt or " unknown item " end filter_result = filter_result .. local_name end end - if active_filter_count > 0 then - result = result .. filter_result .. ", " - end + if active_filter_count > 0 then result = result .. filter_result .. ", " end end --Read held item if ent.held_stack ~= nil and ent.held_stack.valid_for_read and ent.held_stack.valid then result = result .. ", holding " .. ent.held_stack.name - if ent.held_stack.count > 1 then - result = result .. " times " .. ent.held_stack.count - end + if ent.held_stack.count > 1 then result = result .. " times " .. ent.held_stack.count end end --Take note of long handed inserters local pickup_dist_dir = " at 1 " .. fa_utils.direction_lookup(ent.direction) - local drop_dist_dir = " at 1 " .. fa_utils.direction_lookup(fa_utils.rotate_180(ent.direction)) + local drop_dist_dir = " at 1 " .. fa_utils.direction_lookup(fa_utils.rotate_180(ent.direction)) if ent.name == "long-handed-inserter" then pickup_dist_dir = " at 2 " .. fa_utils.direction_lookup(ent.direction) - drop_dist_dir = " at 2 " .. fa_utils.direction_lookup(fa_utils.rotate_180(ent.direction)) + drop_dist_dir = " at 2 " .. fa_utils.direction_lookup(fa_utils.rotate_180(ent.direction)) end --Read the pickup position local pickup = ent.pickup_target local pickup_name = nil if pickup ~= nil and pickup.valid then - pickup_name = fa_localising.get(pickup,pindex) + pickup_name = fa_localising.get(pickup, pindex) else pickup_name = "ground" - local area_ents = ent.surface.find_entities_filtered{position = ent.pickup_position} + local area_ents = ent.surface.find_entities_filtered({ position = ent.pickup_position }) for i, area_ent in ipairs(area_ents) do if area_ent.type == "straight-rail" or area_ent.type == "curved-rail" then - pickup_name = fa_localising.get(area_ent,pindex) + pickup_name = fa_localising.get(area_ent, pindex) end end end result = result .. " picks up from " .. pickup_name .. pickup_dist_dir - --Read the drop position + --Read the drop position local drop = ent.drop_target local drop_name = nil if drop ~= nil and drop.valid then - drop_name = fa_localising.get(drop,pindex) + drop_name = fa_localising.get(drop, pindex) else drop_name = "ground" - local drop_area_ents = ent.surface.find_entities_filtered{position = ent.drop_position} + local drop_area_ents = ent.surface.find_entities_filtered({ position = ent.drop_position }) for i, drop_area_ent in ipairs(drop_area_ents) do if drop_area_ent.type == "straight-rail" or drop_area_ent.type == "curved-rail" then - drop_name = fa_localising.get(drop_area_ent,pindex) + drop_name = fa_localising.get(drop_area_ent, pindex) end end end result = result .. ", drops to " .. drop_name .. drop_dist_dir end - if ent.type == "mining-drill" then + if ent.type == "mining-drill" then local pos = ent.position local radius = ent.prototype.mining_drill_radius - local area = {{pos.x - radius, pos.y - radius}, {pos.x + radius, pos.y + radius}} - local resources = ent.surface.find_entities_filtered{area = area, type = "resource"} + local area = { { pos.x - radius, pos.y - radius }, { pos.x + radius, pos.y + radius } } + local resources = ent.surface.find_entities_filtered({ area = area, type = "resource" }) local dict = {} for i, resource in pairs(resources) do if dict[resource.name] == nil then @@ -733,19 +856,17 @@ function ent_info(pindex, ent, description) local drop = ent.drop_target local drop_name = nil if drop ~= nil and drop.valid then - drop_name = fa_localising.get(drop,pindex) + drop_name = fa_localising.get(drop, pindex) else drop_name = "ground" - local drop_area_ents = ent.surface.find_entities_filtered{position = ent.drop_position} + local drop_area_ents = ent.surface.find_entities_filtered({ position = ent.drop_position }) for i, drop_area_ent in ipairs(drop_area_ents) do if drop_area_ent.type == "straight-rail" or drop_area_ent.type == "curved-rail" then - drop_name = fa_localising.get(drop_area_ent,pindex) + drop_name = fa_localising.get(drop_area_ent, pindex) end end end - if drop ~= nil and drop.valid then - result = result .. " outputs to " .. drop_name - end + if drop ~= nil and drop.valid then result = result .. " outputs to " .. drop_name end if ent.status == defines.entity_status.waiting_for_space_in_destination then result = result .. " output full " end @@ -753,14 +874,14 @@ function ent_info(pindex, ent, description) result = result .. ", Mining from " for i, amount in pairs(dict) do if i == "crude-oil" then - result = result .. " " .. i .. " times " .. math.floor(amount/3000)/10 .. " per second " + result = result .. " " .. i .. " times " .. math.floor(amount / 3000) / 10 .. " per second " else result = result .. " " .. i .. " times " .. fa_utils.floor_to_nearest_k_after_10k(amount) end end end end - --Explain if no fuel + --Explain if no fuel if ent.prototype.burner_prototype ~= nil then if ent.energy == 0 and fa_driving.fuel_inventory_info(ent) == "Contains no fuel." then result = result .. ", Out of Fuel " @@ -770,13 +891,20 @@ function ent_info(pindex, ent, description) local status = ent.status local stat = defines.entity_status if status ~= nil and status ~= stat.normal and status ~= stat.working then - if status == stat.no_ingredients or status == stat.no_input_fluid or status == stat.no_minable_resources or status == stat.item_ingredient_shortage or status == stat.missing_required_fluid or status == stat.no_ammo then + if + status == stat.no_ingredients + or status == stat.no_input_fluid + or status == stat.no_minable_resources + or status == stat.item_ingredient_shortage + or status == stat.missing_required_fluid + or status == stat.no_ammo + then result = result .. ", input missing " elseif status == stat.full_output or status == stat.full_burnt_result_output then result = result .. " output full " end end - --Explain power connected status + --Explain power connected status if ent.prototype.electric_energy_source_prototype ~= nil and ent.is_connected_to_electric_network() == false then result = result .. " power not Connected" elseif ent.prototype.electric_energy_source_prototype ~= nil and ent.energy == 0 and ent.type ~= "solar-panel" then @@ -787,7 +915,7 @@ function ent_info(pindex, ent, description) local charge = math.ceil(ent.energy / 1000) --In kilojoules result = result .. ", " .. level .. " percent full, containing " .. charge .. " kilojoules. " elseif ent.type == "solar-panel" then - local s_time = ent.surface.daytime*24 --We observed 18 = peak solar start, 6 = peak solar end, 11 = night start, 13 = night end + local s_time = ent.surface.daytime * 24 --We observed 18 = peak solar start, 6 = peak solar end, 11 = night start, 13 = night end local solar_status = "" if s_time > 13 and s_time <= 18 then solar_status = ", increasing production, morning hours. " @@ -801,32 +929,28 @@ function ent_info(pindex, ent, description) result = result .. solar_status elseif ent.name == "rocket-silo" then if ent.rocket_parts ~= nil and ent.rocket_parts < 100 then - result = result .. ", " .. ent.rocket_parts .. " finished out of 100. " - elseif ent.rocket_parts ~= nil then + result = result .. ", " .. ent.rocket_parts .. " finished out of 100. " + elseif ent.rocket_parts ~= nil then result = result .. ", rocket ready, press SPACE to launch. " - end + end elseif ent.name == "beacon" then local modules = ent.get_module_inventory() - if modules.get_item_count() == 0 then - result = result .. " with no modules " - elseif modules.get_item_count() == 1 then - result = result .. " with " .. modules[1].name - elseif modules.get_item_count() == 2 then - result = result .. " with " .. modules[1].name .. " and " .. modules[2].name + if modules.get_item_count() == 0 then + result = result .. " with no modules " + elseif modules.get_item_count() == 1 then + result = result .. " with " .. modules[1].name + elseif modules.get_item_count() == 2 then + result = result .. " with " .. modules[1].name .. " and " .. modules[2].name elseif modules.get_item_count() > 2 then - result = result .. " with " .. modules[1].name .. " and " .. modules[2].name .. " and other modules " + result = result .. " with " .. modules[1].name .. " and " .. modules[2].name .. " and other modules " end elseif ent.temperature ~= nil and ent.name ~= "heat-pipe" then --ent.name == "nuclear-reactor" or ent.name == "heat-pipe" or ent.name == "heat-exchanger" then result = result .. ", temperature " .. math.floor(ent.temperature) .. " degrees C " - if ent.name == "nuclear-reactor" then - if ent.temperature > 900 then - result = result .. ", danger " - end - if ent.energy > 0 then - result = result .. ", consuming fuel cell " - end - result = result .. ", neighbour bonus " .. ent.neighbour_bonus * 100 .. " percent " - end + if ent.name == "nuclear-reactor" then + if ent.temperature > 900 then result = result .. ", danger " end + if ent.energy > 0 then result = result .. ", consuming fuel cell " end + result = result .. ", neighbour bonus " .. ent.neighbour_bonus * 100 .. " percent " + end elseif ent.name == "item-on-ground" then result = result .. ", " .. ent.stack.name end @@ -835,29 +959,64 @@ function ent_info(pindex, ent, description) result = result .. " connects " local con_targets = fa_building_tools.get_heat_connection_target_positions(ent.name, ent.position, ent.direction) local con_count = 0 - local con_counts = {0,0,0,0,0,0,0,0} - con_counts[dirs.north+1] = 0 - con_counts[dirs.south+1] = 0 - con_counts[dirs.east+1] = 0 - con_counts[dirs.west+1] = 0 + local con_counts = { 0, 0, 0, 0, 0, 0, 0, 0 } + con_counts[dirs.north + 1] = 0 + con_counts[dirs.south + 1] = 0 + con_counts[dirs.east + 1] = 0 + con_counts[dirs.west + 1] = 0 if #con_targets > 0 then for i, con_target_pos in ipairs(con_targets) do --For each heat connection target position - rendering.draw_circle{color = {1.0, 0.0, 0.5},radius = 0.1,width = 2,target = con_target_pos, surface = ent.surface, time_to_live = 30} - local target_ents = ent.surface.find_entities_filtered{position = con_target_pos} + rendering.draw_circle({ + color = { 1.0, 0.0, 0.5 }, + radius = 0.1, + width = 2, + target = con_target_pos, + surface = ent.surface, + time_to_live = 30, + }) + local target_ents = ent.surface.find_entities_filtered({ position = con_target_pos }) for j, target_ent in ipairs(target_ents) do - if target_ent.valid and #fa_building_tools.get_heat_connection_positions(target_ent.name, target_ent.position, target_ent.direction) > 0 then - for k, spot in ipairs(fa_building_tools.get_heat_connection_positions(target_ent.name, target_ent.position, target_ent.direction)) do - --For each heat connection of the found target entity - rendering.draw_circle{color = {1.0, 1.0, 0.5},radius = 0.2,width = 2,target = spot, surface = ent.surface, time_to_live = 30} - if util.distance(con_target_pos,spot) < 0.2 then + if + target_ent.valid + and #fa_building_tools.get_heat_connection_positions( + target_ent.name, + target_ent.position, + target_ent.direction + ) + > 0 + then + for k, spot in + ipairs( + fa_building_tools.get_heat_connection_positions( + target_ent.name, + target_ent.position, + target_ent.direction + ) + ) + do + --For each heat connection of the found target entity + rendering.draw_circle({ + color = { 1.0, 1.0, 0.5 }, + radius = 0.2, + width = 2, + target = spot, + surface = ent.surface, + time_to_live = 30, + }) + if util.distance(con_target_pos, spot) < 0.2 then --For each match - rendering.draw_circle{color = {0.5, 1.0, 0.5},radius = 0.3,width = 2,target = spot, surface = ent.surface, time_to_live = 30} + rendering.draw_circle({ + color = { 0.5, 1.0, 0.5 }, + radius = 0.3, + width = 2, + target = spot, + surface = ent.surface, + time_to_live = 30, + }) con_count = con_count + 1 - local con_dir = fa_utils.get_direction_biased(con_target_pos,ent.position) - if con_count > 1 then - result = result .. " and " - end + local con_dir = fa_utils.get_direction_biased(con_target_pos, ent.position) + if con_count > 1 then result = result .. " and " end result = result .. fa_utils.direction_lookup(con_dir) end end @@ -865,16 +1024,12 @@ function ent_info(pindex, ent, description) end end end - if con_count == 0 then - result = result .. " to nothing " - end + if con_count == 0 then result = result .. " to nothing " end if ent.name == "heat-pipe" then --For this ent in particular, read temp after direction result = result .. ", temperature " .. math.floor(ent.temperature) .. " degrees C " end end - if ent.type == "constant-combinator" then - result = result .. constant_combinator_signals_info(ent, pindex) - end + if ent.type == "constant-combinator" then result = result .. constant_combinator_signals_info(ent, pindex) end return result end @@ -883,17 +1038,15 @@ function radar_charting_info(radar) local charting_range = radar.prototype.max_distance_of_sector_revealed local count = 0 local total = 0 - local centerx = math.floor(radar.position.x/32) - local centery = math.floor(radar.position.y/32) - for i = (centerx - charting_range), (centerx + charting_range), (1) do - for j = (centery - charting_range), (centery + charting_range), (1) do - if radar.force.is_chunk_charted(radar.surface,{i, j}) then - count = count + 1 - end + local centerx = math.floor(radar.position.x / 32) + local centery = math.floor(radar.position.y / 32) + for i = (centerx - charting_range), (centerx + charting_range), 1 do + for j = (centery - charting_range), (centery + charting_range), 1 do + if radar.force.is_chunk_charted(radar.surface, { i, j }) then count = count + 1 end total = total + 1 end end - local percent_charted = math.floor(count/total * 100) + local percent_charted = math.floor(count / total * 100) local result = percent_charted .. " percent charted, " .. charting_range * 32 .. " tiles charting range " return result end @@ -908,19 +1061,11 @@ function prune_item_groups(array) local check2 = true for i1, v1 in ipairs(groups) do - if v1.name == v.group.name then - check1 = false - end - if v1.name == v.subgroup.name then - check2 = false - end - end - if check1 then - table.insert(groups, v.group) - end - if check2 then - table.insert(groups, v.subgroup) + if v1.name == v.group.name then check1 = false end + if v1.name == v.subgroup.name then check2 = false end end + if check1 then table.insert(groups, v.group) end + if check2 then table.insert(groups, v.subgroup) end end end local i = 1 @@ -933,9 +1078,7 @@ function prune_item_groups(array) break end end - if check then - table.remove(array, i) - end + if check then table.remove(array, i) end end end @@ -946,7 +1089,7 @@ end --Ent info: Gives the distance and direction of a fluidbox connection target? Todo: update to clarify and include localization function get_adjacent_source(box, pos, dir) - local result = {position = pos, direction = ""} + local result = { position = pos, direction = "" } ebox = table.deepcopy(box) if dir == 1 or dir == 3 then ebox.left_top.x = box.left_top.y @@ -954,16 +1097,16 @@ function get_adjacent_source(box, pos, dir) ebox.right_bottom.x = box.right_bottom.y ebox.right_bottom.y = box.right_bottom.x end --- print(ebox.left_top.x .. " " .. ebox.left_top.y) - ebox.left_top.x = math.ceil(ebox.left_top.x * 2)/2 - ebox.left_top.y = math.ceil(ebox.left_top.y * 2)/2 - ebox.right_bottom.x = math.floor(ebox.right_bottom.x * 2)/2 - ebox.right_bottom.y = math.floor(ebox.right_bottom.y * 2)/2 + -- print(ebox.left_top.x .. " " .. ebox.left_top.y) + ebox.left_top.x = math.ceil(ebox.left_top.x * 2) / 2 + ebox.left_top.y = math.ceil(ebox.left_top.y * 2) / 2 + ebox.right_bottom.x = math.floor(ebox.right_bottom.x * 2) / 2 + ebox.right_bottom.y = math.floor(ebox.right_bottom.y * 2) / 2 if pos.x < ebox.left_top.x then result.position.x = result.position.x + 1 result.direction = "West" - elseif pos.x > ebox.right_bottom.x then + elseif pos.x > ebox.right_bottom.x then result.position.x = result.position.x - 1 result.direction = "East" elseif pos.y < ebox.left_top.y then @@ -983,22 +1126,28 @@ function read_inventory_slot(pindex, start_phrase_in, inv_in) local inv = inv_in or players[pindex].inventory.lua_inventory if index < 1 then index = 1 - elseif index > #inv then + elseif index > #inv then index = #inv end players[pindex].inventory.index = index local stack = inv[index] if stack == nil or not stack.valid_for_read then - printout(start_phrase .. "Empty Slot",pindex) + printout(start_phrase .. "Empty Slot", pindex) return end if stack.is_blueprint then - printout(fa_blueprints.get_blueprint_info(stack,false),pindex) + printout(fa_blueprints.get_blueprint_info(stack, false), pindex) elseif stack.valid_for_read then - if stack.health < 1 then - start_phrase = start_phrase .. " damaged " - end - printout(start_phrase .. fa_localising.get(stack,pindex) .. " x " .. stack.count .. " " .. stack.prototype.subgroup.name , pindex) + if stack.health < 1 then start_phrase = start_phrase .. " damaged " end + printout( + start_phrase + .. fa_localising.get(stack, pindex) + .. " x " + .. stack.count + .. " " + .. stack.prototype.subgroup.name, + pindex + ) end end @@ -1012,8 +1161,8 @@ function read_hand(pindex) local cursor_ghost = game.get_player(pindex).cursor_ghost if cursor_stack and cursor_stack.valid_for_read then if cursor_stack.is_blueprint then - --Blueprint extra info - printout(fa_blueprints.get_blueprint_info(cursor_stack,true),pindex) + --Blueprint extra info + printout(fa_blueprints.get_blueprint_info(cursor_stack, true), pindex) elseif cursor_stack.name == "spidertron-remote" then local remote_info = "" if cursor_stack.connected_entity == nil then @@ -1025,50 +1174,50 @@ function read_hand(pindex) remote_info = " for spidertron " .. cursor_stack.connected_entity.entity_label end end - printout(fa_localising.get(cursor_stack,pindex) .. remote_info, pindex) + printout(fa_localising.get(cursor_stack, pindex) .. remote_info, pindex) else --Any other valid item - local out={"access.cursor-description"} - table.insert(out,cursor_stack.prototype.localised_name) + local out = { "access.cursor-description" } + table.insert(out, cursor_stack.prototype.localised_name) local build_entity = cursor_stack.prototype.place_result if build_entity and build_entity.supports_direction then - table.insert(out,1) - table.insert(out,{"access.facing-direction",players[pindex].building_direction}) + table.insert(out, 1) + table.insert(out, { "access.facing-direction", players[pindex].building_direction }) else - table.insert(out,0) - table.insert(out,"") + table.insert(out, 0) + table.insert(out, "") end - table.insert(out,cursor_stack.count) + table.insert(out, cursor_stack.count) local extra = game.get_player(pindex).get_main_inventory().get_item_count(cursor_stack.name) if extra > 0 then - table.insert(out,cursor_stack.count+extra) + table.insert(out, cursor_stack.count + extra) else - table.insert(out,0) + table.insert(out, 0) end printout(out, pindex) end elseif cursor_ghost ~= nil then --Any ghost - local out={"access.cursor-description"} - table.insert(out,cursor_ghost.localised_name) - local build_entity = cursor_ghost.place_result - if build_entity and build_entity.supports_direction then - table.insert(out,1) - table.insert(out,{"access.facing-direction",players[pindex].building_direction}) - else - table.insert(out,0) - table.insert(out,"") - end - table.insert(out,0) - local extra = 0 - if extra > 0 then - table.insert(out,cursor_stack.count+extra) - else - table.insert(out,0) - end - printout(out, pindex) + local out = { "access.cursor-description" } + table.insert(out, cursor_ghost.localised_name) + local build_entity = cursor_ghost.place_result + if build_entity and build_entity.supports_direction then + table.insert(out, 1) + table.insert(out, { "access.facing-direction", players[pindex].building_direction }) + else + table.insert(out, 0) + table.insert(out, "") + end + table.insert(out, 0) + local extra = 0 + if extra > 0 then + table.insert(out, cursor_stack.count + extra) + else + table.insert(out, 0) + end + printout(out, pindex) else - printout({"access.empty_cursor"}, pindex) + printout({ "access.empty_cursor" }, pindex) end end @@ -1085,7 +1234,7 @@ function locate_hand_in_player_inventory(pindex) end if players[pindex].in_menu and players[pindex].menu ~= "inventory" then --Unsupported menu type, laterdo add support for building menu and closing the menu with a call - printout("Another menu is open.",pindex) + printout("Another menu is open.", pindex) return end if not players[pindex].in_menu then @@ -1101,10 +1250,8 @@ function locate_hand_in_player_inventory(pindex) local successful = p.clear_cursor() if not successful then local message = "Unable to empty hand" - if inv.count_empty_stacks() == 0 then - message = message .. ", inventory full" - end - printout(message,pindex) + if inv.count_empty_stacks() == 0 then message = message .. ", inventory full" end + printout(message, pindex) return end @@ -1113,19 +1260,16 @@ function locate_hand_in_player_inventory(pindex) local i = 0 while not found and i < #inv do i = i + 1 - if inv[i] and inv[i].valid_for_read and inv[i].name == item_name then - found = true - end + if inv[i] and inv[i].valid_for_read and inv[i].name == item_name then found = true end end --If found, read it from the inventory if not found then - printout("Error: " .. fa_localising.get(stack,pindex) .. " not found in player inventory",pindex) + printout("Error: " .. fa_localising.get(stack, pindex) .. " not found in player inventory", pindex) return else players[pindex].inventory.index = i read_inventory_slot(pindex, "inventory ") end - end --Clears the item in hand and then locates it from the first found building output slot @@ -1140,7 +1284,13 @@ function locate_hand_in_building_output_inventory(pindex) --Hand is empty return end - if players[pindex].in_menu and (players[pindex].menu == "building" or players[pindex].menu == "vehicle") and pb.sectors and pb.sectors[pb.sector] and pb.sectors[pb.sector].name == "Output" then + if + players[pindex].in_menu + and (players[pindex].menu == "building" or players[pindex].menu == "vehicle") + and pb.sectors + and pb.sectors[pb.sector] + and pb.sectors[pb.sector].name == "Output" + then inv = p.opened.get_output_inventory() else --Unsupported menu type @@ -1154,10 +1304,8 @@ function locate_hand_in_building_output_inventory(pindex) local successful = p.clear_cursor() if not successful then local message = "Unable to empty hand" - if inv.count_empty_stacks() == 0 then - message = message .. ", inventory full" - end - printout(message,pindex) + if inv.count_empty_stacks() == 0 then message = message .. ", inventory full" end + printout(message, pindex) return end @@ -1166,19 +1314,16 @@ function locate_hand_in_building_output_inventory(pindex) local i = 0 while not found and i < #inv do i = i + 1 - if inv[i] and inv[i].valid_for_read and inv[i].name == item_name then - found = true - end + if inv[i] and inv[i].valid_for_read and inv[i].name == item_name then found = true end end --If found, read it from the inventory if not found then - printout(fa_localising.get(stack,pindex) .. " not found in building output",pindex) + printout(fa_localising.get(stack, pindex) .. " not found in building output", pindex) return else players[pindex].building.index = i fa_sectors.read_sector_slot(pindex, false) end - end --Clears the item in hand and then locates its recipe from the crafting menu. Closes some other menus, does not run in some other menus, uses the menu search function. @@ -1192,9 +1337,14 @@ function locate_hand_in_crafting_menu(pindex) --Hand is empty return end - if players[pindex].in_menu and players[pindex].menu ~= "inventory" and players[pindex].menu ~= "building" and players[pindex].menu ~= "crafting" then + if + players[pindex].in_menu + and players[pindex].menu ~= "inventory" + and players[pindex].menu ~= "building" + and players[pindex].menu ~= "crafting" + then --Unsupported menu types... - printout("Another menu is open.",pindex) + printout("Another menu is open.", pindex) return end @@ -1205,33 +1355,33 @@ function locate_hand_in_crafting_menu(pindex) p.opened = p --Get the name - local item_name = string.lower(fa_utils.get_substring_before_space(fa_utils.get_substring_before_dash(fa_localising.get(stack.prototype,pindex)))) + local item_name = string.lower( + fa_utils.get_substring_before_space( + fa_utils.get_substring_before_dash(fa_localising.get(stack.prototype, pindex)) + ) + ) players[pindex].menu_search_term = item_name - --Empty hand stack (clear cursor stack) after getting the name + --Empty hand stack (clear cursor stack) after getting the name players[pindex].skip_read_hand = true local successful = p.clear_cursor() if not successful then local message = "Unable to empty hand" - if inv.count_empty_stacks() == 0 then - message = message .. ", inventory full" - end - printout(message,pindex) + if inv.count_empty_stacks() == 0 then message = message .. ", inventory full" end + printout(message, pindex) return end --Run the search - fa_menu_search.fetch_next(pindex,item_name,nil) + fa_menu_search.fetch_next(pindex, item_name, nil) end --If there is an entity to select, moves the mouse pointer to it, else moves to the cursor tile. function target(pindex) - if players[pindex].vanilla_mode then - return - end + if players[pindex].vanilla_mode then return end local ent = get_selected_ent(pindex) if ent then - fa_mouse.move_mouse_pointer(ent.position,pindex) + fa_mouse.move_mouse_pointer(ent.position, pindex) else fa_mouse.move_mouse_pointer(players[pindex].cursor_pos, pindex) end @@ -1239,28 +1389,26 @@ end --Checks the cursor tile for a new entity and reads out ent info. Used when a tile has multiple overlapping entities. function tile_cycle(pindex) - local tile=players[pindex].tile + local tile = players[pindex].tile tile.index = tile.index + 1 - if tile.index > #tile.ents then - tile.index = 0 - end + if tile.index > #tile.ents then tile.index = 0 end local ent = get_selected_ent(pindex) if ent then - printout(ent_info(pindex,ent,""),pindex) + printout(ent_info(pindex, ent, ""), pindex) else printout(tile.tile, pindex) end end ---Checks if the global players table has been created, and if the table entry for this player exists. Otherwise it is initialized. +--Checks if the global players table has been created, and if the table entry for this player exists. Otherwise it is initialized. function check_for_player(index) if not players then global.players = global.players or {} players = global.players end if players[index] == nil then - initialize(game.get_player(index)) - return false + initialize(game.get_player(index)) + return false else return true end @@ -1273,12 +1421,8 @@ function printout(str, pindex) else return end - if players[pindex].vanilla_mode == nil then - players[pindex].vanilla_mode = false - end - if not players[pindex].vanilla_mode then - localised_print{"","out "..pindex.." ",str} - end + if players[pindex].vanilla_mode == nil then players[pindex].vanilla_mode = false end + if not players[pindex].vanilla_mode then localised_print({ "", "out " .. pindex .. " ", str }) end end --Reprints the last sent string to the Factorio Access Launcher app for the vocalizer to read out. @@ -1295,7 +1439,7 @@ function toggle_cursor_mode(pindex) return end - if (not players[pindex].cursor) and (not players[pindex].hide_cursor) then + if (not players[pindex].cursor) and not players[pindex].hide_cursor then --Enable players[pindex].cursor = true players[pindex].build_lock = false @@ -1306,19 +1450,18 @@ function toggle_cursor_mode(pindex) else --Disable players[pindex].cursor = false - players[pindex].cursor_pos = fa_utils.offset_position(players[pindex].position,players[pindex].player_direction,1) + players[pindex].cursor_pos = + fa_utils.offset_position(players[pindex].position, players[pindex].player_direction, 1) players[pindex].cursor_pos = fa_utils.center_of_tile(players[pindex].cursor_pos) - fa_mouse.move_mouse_pointer(players[pindex].cursor_pos,pindex) + fa_mouse.move_mouse_pointer(players[pindex].cursor_pos, pindex) fa_graphics.sync_build_cursor_graphics(pindex) target(pindex) players[pindex].player_direction = p.character.direction players[pindex].build_lock = false - if p.driving and p.vehicle then - p.vehicle.active = true - end + if p.driving and p.vehicle then p.vehicle.active = true end read_tile(pindex, "Cursor mode disabled, ") - --Close Remote view + --Close Remote view players[pindex].remote_view = false p.close_map() end @@ -1331,9 +1474,15 @@ function toggle_cursor_mode(pindex) fa_graphics.draw_cursor_highlight(pindex, nil, nil) end else - local left_top = {math.floor(players[pindex].cursor_pos.x)-players[pindex].cursor_size,math.floor(players[pindex].cursor_pos.y)-players[pindex].cursor_size} - local right_bottom = {math.floor(players[pindex].cursor_pos.x)+players[pindex].cursor_size+1,math.floor(players[pindex].cursor_pos.y)+players[pindex].cursor_size+1} - fa_graphics.draw_large_cursor(left_top,right_bottom,pindex) + local left_top = { + math.floor(players[pindex].cursor_pos.x) - players[pindex].cursor_size, + math.floor(players[pindex].cursor_pos.y) - players[pindex].cursor_size, + } + local right_bottom = { + math.floor(players[pindex].cursor_pos.x) + players[pindex].cursor_size + 1, + math.floor(players[pindex].cursor_pos.y) + players[pindex].cursor_size + 1, + } + fa_graphics.draw_large_cursor(left_top, right_bottom, pindex) end end @@ -1344,12 +1493,12 @@ function toggle_remote_view(pindex, force_true, force_false) players[pindex].cursor = true players[pindex].build_lock = false center_player_character(pindex) - printout("Remote view opened",pindex) + printout("Remote view opened", pindex) else players[pindex].remote_view = false players[pindex].cursor = false players[pindex].build_lock = false - printout("Remote view closed",pindex) + printout("Remote view closed", pindex) game.get_player(pindex).close_map() end end @@ -1357,45 +1506,50 @@ end --Teleports the player character to the nearest tile center position to allow grid aligned cursor movement. function center_player_character(pindex) local p = game.get_player(pindex) - local can_port = p.surface.can_place_entity{name = "character", position = fa_utils.center_of_tile(p.position)} - local ents = p.surface.find_entities_filtered{position = fa_utils.center_of_tile(p.position), radius = 0.1, type = {"character"}, invert = true} + local can_port = p.surface.can_place_entity({ name = "character", position = fa_utils.center_of_tile(p.position) }) + local ents = p.surface.find_entities_filtered({ + position = fa_utils.center_of_tile(p.position), + radius = 0.1, + type = { "character" }, + invert = true, + }) if #ents > 0 and ents[1].valid then local ent = ents[1] --Ignore ents you can walk through, laterdo better collision checks** can_port = can_port or all_ents_are_walkable(p.position) end - if can_port then - p.teleport(fa_utils.center_of_tile(p.position)) - end + if can_port then p.teleport(fa_utils.center_of_tile(p.position)) end players[pindex].position = p.position players[pindex].cursor_pos = fa_utils.center_of_tile(players[pindex].cursor_pos) - fa_mouse.move_mouse_pointer(players[pindex].cursor_pos,pindex) + fa_mouse.move_mouse_pointer(players[pindex].cursor_pos, pindex) end --Teleports the cursor to the player character function jump_to_player(pindex) local first_player = game.get_player(pindex) - players[pindex].cursor_pos.x = math.floor(first_player.position.x)+.5 - players[pindex].cursor_pos.y = math.floor(first_player.position.y) + .5 + players[pindex].cursor_pos.x = math.floor(first_player.position.x) + 0.5 + players[pindex].cursor_pos.y = math.floor(first_player.position.y) + 0.5 read_coords(pindex, "Cursor returned ") if players[pindex].cursor_size < 2 then fa_graphics.draw_cursor_highlight(pindex, nil, nil) else - local scan_left_top = {math.floor(players[pindex].cursor_pos.x)-players[pindex].cursor_size,math.floor(players[pindex].cursor_pos.y)-players[pindex].cursor_size} - local scan_right_bottom = {math.floor(players[pindex].cursor_pos.x)+players[pindex].cursor_size+1,math.floor(players[pindex].cursor_pos.y)+players[pindex].cursor_size+1} - fa_graphics.draw_large_cursor(scan_left_top,scan_right_bottom,pindex) + local scan_left_top = { + math.floor(players[pindex].cursor_pos.x) - players[pindex].cursor_size, + math.floor(players[pindex].cursor_pos.y) - players[pindex].cursor_size, + } + local scan_right_bottom = { + math.floor(players[pindex].cursor_pos.x) + players[pindex].cursor_size + 1, + math.floor(players[pindex].cursor_pos.y) + players[pindex].cursor_size + 1, + } + fa_graphics.draw_large_cursor(scan_left_top, scan_right_bottom, pindex) end end function return_cursor_to_character(pindex) - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local ent = get_selected_ent(pindex) - if not (players[pindex].in_menu) then - if players[pindex].cursor then - jump_to_player(pindex) - end + if not players[pindex].in_menu then + if players[pindex].cursor then jump_to_player(pindex) end end end @@ -1407,26 +1561,32 @@ function refresh_player_tile(pindex) --search_area[1]=add_position(search_area[1],search_center) --search_area[2]=add_position(search_area[2],search_center) local c_pos = players[pindex].cursor_pos - if math.floor(c_pos.x) == math.ceil(c_pos.x) then - c_pos.x = c_pos.x - 0.01 - end - if math.floor(c_pos.y) == math.ceil(c_pos.y) then - c_pos.y = c_pos.y - 0.01 - end - local search_area = {{x = math.floor(c_pos.x)+0.01,y = math.floor(c_pos.y)+0.01} , {x = math.ceil(c_pos.x)-0.01,y = math.ceil(c_pos.y)-0.01}} - local excluded_names = {"highlight-box","flying-text"} - players[pindex].tile.ents = surf.find_entities_filtered{area = search_area, name = excluded_names, invert = true} + if math.floor(c_pos.x) == math.ceil(c_pos.x) then c_pos.x = c_pos.x - 0.01 end + if math.floor(c_pos.y) == math.ceil(c_pos.y) then c_pos.y = c_pos.y - 0.01 end + local search_area = { + { x = math.floor(c_pos.x) + 0.01, y = math.floor(c_pos.y) + 0.01 }, + { x = math.ceil(c_pos.x) - 0.01, y = math.ceil(c_pos.y) - 0.01 }, + } + local excluded_names = { "highlight-box", "flying-text" } + players[pindex].tile.ents = surf.find_entities_filtered({ area = search_area, name = excluded_names, invert = true }) --rendering.draw_rectangle{left_top = search_area[1], right_bottom = search_area[2], color = {1,0,1}, surface = surf, time_to_live = 100}-- - local wide_area = {{x = math.floor(c_pos.x)-0.01,y = math.floor(c_pos.y)-0.01} , {x = math.ceil(c_pos.x)+0.01,y = math.ceil(c_pos.y)+0.01}} - local remnants = surf.find_entities_filtered{area = wide_area, type = "corpse"} + local wide_area = { + { x = math.floor(c_pos.x) - 0.01, y = math.floor(c_pos.y) - 0.01 }, + { x = math.ceil(c_pos.x) + 0.01, y = math.ceil(c_pos.y) + 0.01 }, + } + local remnants = surf.find_entities_filtered({ area = wide_area, type = "corpse" }) for i, remnant in ipairs(remnants) do table.insert(players[pindex].tile.ents, remnant) end players[pindex].tile.index = #players[pindex].tile.ents == 0 and 0 or 1 - if not(pcall(function() - players[pindex].tile.tile = surf.get_tile(players[pindex].cursor_pos.x, players[pindex].cursor_pos.y).name - players[pindex].tile.tile_object = surf.get_tile(players[pindex].cursor_pos.x, players[pindex].cursor_pos.y) - end)) then + if + not ( + pcall(function() + players[pindex].tile.tile = surf.get_tile(players[pindex].cursor_pos.x, players[pindex].cursor_pos.y).name + players[pindex].tile.tile_object = surf.get_tile(players[pindex].cursor_pos.x, players[pindex].cursor_pos.y) + end) + ) + then return false end return true @@ -1444,15 +1604,22 @@ function read_tile(pindex, start_text) --If there is no ent, read the tile instead players[pindex].tile.previous = nil local tile = players[pindex].tile.tile - result = result .. fa_localising.get(players[pindex].tile.tile_object,pindex) - if tile == "water" or tile == "deepwater" or tile == "water-green" or tile == "deepwater-green" or tile == "water-shallow" or tile == "water-mud" or tile == "water-wube" then + result = result .. fa_localising.get(players[pindex].tile.tile_object, pindex) + if + tile == "water" + or tile == "deepwater" + or tile == "water-green" + or tile == "deepwater-green" + or tile == "water-shallow" + or tile == "water-mud" + or tile == "water-wube" + then --Identify shores and crevices and so on for water tiles result = result .. fa_utils.identify_water_shores(pindex) end fa_graphics.draw_cursor_highlight(pindex, nil, nil) game.get_player(pindex).selected = nil - - else--laterdo tackle the issue here where entities such as tree stumps block preview info + else --laterdo tackle the issue here where entities such as tree stumps block preview info result = result .. ent_info(pindex, ent) fa_graphics.draw_cursor_highlight(pindex, ent, nil) game.get_player(pindex).selected = ent @@ -1460,11 +1627,11 @@ function read_tile(pindex, start_text) --game.get_player(pindex).print(result)-- players[pindex].tile.previous = ent end - if not ent or ent.type == "resource" then--possible bug here with the h box being a new tile ent + if not ent or ent.type == "resource" then --possible bug here with the h box being a new tile ent local stack = game.get_player(pindex).cursor_stack --Run build preview checks if stack and stack.valid_for_read and stack.valid and stack.prototype.place_result ~= nil then - result = result .. fa_building_tools.build_preview_checks_info(stack,pindex) + result = result .. fa_building_tools.build_preview_checks_info(stack, pindex) --game.get_player(pindex).print(result)-- end end @@ -1472,20 +1639,16 @@ function read_tile(pindex, start_text) --If the player is holding a cut-paste tool, every entity being read gets mined as soon as you read a new tile. local stack = game.get_player(pindex).cursor_stack if stack and stack.valid_for_read and stack.name == "cut-paste-tool" and not players[pindex].vanilla_mode then - if ent and ent.valid then--not while loop, because it causes crashes + if ent and ent.valid then --not while loop, because it causes crashes local name = ent.name - game.get_player(pindex).play_sound{path = "player-mine"} - if fa_mining_tools.try_to_mine_with_soun(ent,pindex) then - result = result .. name .. " mined, " - end + game.get_player(pindex).play_sound({ path = "player-mine" }) + if fa_mining_tools.try_to_mine_with_soun(ent, pindex) then result = result .. name .. " mined, " end --Second round, in case two entities are there. While loops do not work! ent = get_selected_ent(pindex) - if ent and ent.valid and players[pindex].walk ~= 2 then--not while + if ent and ent.valid and players[pindex].walk ~= 2 then --not while local name = ent.name - game.get_player(pindex).play_sound{path = "player-mine"} - if fa_mining_tools.try_to_mine_with_soun(ent,pindex) then - result = result .. name .. " mined, " - end + game.get_player(pindex).play_sound({ path = "player-mine" }) + if fa_mining_tools.try_to_mine_with_soun(ent, pindex) then result = result .. name .. " mined, " end end end end @@ -1502,51 +1665,76 @@ function read_coords(pindex, start_phrase) local result = start_phrase local ent = players[pindex].building.ent local offset = 0 - if (players[pindex].menu == "building" or players[pindex].menu == "vehicle") and players[pindex].building.recipe_list ~= nil then + if + (players[pindex].menu == "building" or players[pindex].menu == "vehicle") + and players[pindex].building.recipe_list ~= nil + then offset = 1 end - if not(players[pindex].in_menu) or players[pindex].menu == "structure-travel" or players[pindex].menu == "travel" then - if players[pindex].vanilla_mode then - players[pindex].cursor_pos = game.get_player(pindex).position - end + if not players[pindex].in_menu or players[pindex].menu == "structure-travel" or players[pindex].menu == "travel" then + if players[pindex].vanilla_mode then players[pindex].cursor_pos = game.get_player(pindex).position end if game.get_player(pindex).driving then --Give vehicle coords and orientation and speed --laterdo find exact speed coefficient local vehicle = game.get_player(pindex).vehicle local speed = vehicle.speed * 215 if vehicle.type ~= "spider-vehicle" then if speed > 0 then - result = result .. " heading " .. fa_utils.get_heading_info(vehicle) .. " at " .. math.floor(speed) .. " kilometers per hour " + result = result + .. " heading " + .. fa_utils.get_heading_info(vehicle) + .. " at " + .. math.floor(speed) + .. " kilometers per hour " elseif speed < 0 then - result = result .. " facing " .. fa_utils.get_heading_info(vehicle) .. " while reversing at " .. math.floor(-speed) .. " kilometers per hour " + result = result + .. " facing " + .. fa_utils.get_heading_info(vehicle) + .. " while reversing at " + .. math.floor(-speed) + .. " kilometers per hour " else result = result .. " parked facing " .. fa_utils.get_heading_info(vehicle) end else - result = result .. " moving at " .. math.floor(speed) .. " kilometers per hour " + result = result .. " moving at " .. math.floor(speed) .. " kilometers per hour " end - result = result .. " in " .. fa_localising.get(vehicle,pindex) .. " at point " + result = result .. " in " .. fa_localising.get(vehicle, pindex) .. " at point " printout(result .. math.floor(vehicle.position.x) .. ", " .. math.floor(vehicle.position.y), pindex) else --Simply give coords local location = fa_utils.get_entity_part_at_cursor(pindex) - if location == nil then - location = " " - end - local marked_pos = {x = players[pindex].cursor_pos.x, y = players[pindex].cursor_pos.y} - local printed_pos = {x = math.floor(players[pindex].cursor_pos.x * 10) / 10, y = math.floor(players[pindex].cursor_pos.y * 10) / 10} + if location == nil then location = " " end + local marked_pos = { x = players[pindex].cursor_pos.x, y = players[pindex].cursor_pos.y } + local printed_pos = { + x = math.floor(players[pindex].cursor_pos.x * 10) / 10, + y = math.floor(players[pindex].cursor_pos.y * 10) / 10, + } --Floor the marked and read pos for consistency and conciseness marked_pos.x = math.floor(marked_pos.x + 0.0) marked_pos.y = math.floor(marked_pos.y + 0.0) result = result .. " " .. location .. ", at " .. marked_pos.x .. ", " .. marked_pos.y - game.get_player(pindex).print("At " .. printed_pos.x .. ", " .. printed_pos.y , {volume_modifier = 0}) - rendering.draw_circle{color = {1.0, 0.2, 0.0},radius = 0.1,width = 5, target = players[pindex].cursor_pos,surface = game.get_player(pindex).surface,time_to_live = 180} + game.get_player(pindex).print("At " .. printed_pos.x .. ", " .. printed_pos.y, { volume_modifier = 0 }) + rendering.draw_circle({ + color = { 1.0, 0.2, 0.0 }, + radius = 0.1, + width = 5, + target = players[pindex].cursor_pos, + surface = game.get_player(pindex).surface, + time_to_live = 180, + }) --rendering.draw_circle{color = {0.2, 0.8, 0.2},radius = 0.2,width = 5, target = marked_pos,surface = game.get_player(pindex).surface,time_to_live = 180} --If there is a build preview, give its dimensions and which way they extend local stack = game.get_player(pindex).cursor_stack - if stack and stack.valid_for_read and stack.valid and stack.prototype.place_result ~= nil and (stack.prototype.place_result.tile_height > 1 or stack.prototype.place_result.tile_width > 1) then + if + stack + and stack.valid_for_read + and stack.valid + and stack.prototype.place_result ~= nil + and (stack.prototype.place_result.tile_height > 1 or stack.prototype.place_result.tile_width > 1) + then local dir = players[pindex].building_direction turn_to_cursor_direction_cardinal(pindex) local p_dir = players[pindex].player_direction @@ -1572,34 +1760,62 @@ function read_coords(pindex, start_phrase) preview_str = preview_str .. " to the North " end result = result .. preview_str - elseif stack and stack.valid_for_read and stack.valid and stack.is_blueprint and stack.is_blueprint_setup() then - --Blueprints have their own data + elseif + stack + and stack.valid_for_read + and stack.valid + and stack.is_blueprint + and stack.is_blueprint_setup() + then + --Blueprints have their own data local left_top, right_bottom, build_pos = fa_blueprints.get_blueprint_corners(pindex, false) local bp_dim_1 = right_bottom.x - left_top.x local bp_dim_2 = right_bottom.y - left_top.y - local preview_str = ", blueprint preview is " .. bp_dim_1 .. " tiles wide to the East and " .. bp_dim_2 .. " tiles high to the South" + local preview_str = ", blueprint preview is " + .. bp_dim_1 + .. " tiles wide to the East and " + .. bp_dim_2 + .. " tiles high to the South" result = result .. preview_str elseif stack and stack.valid_for_read and stack.valid and stack.prototype.place_as_tile_result ~= nil then --Paving preview size local preview_str = ", paving preview " local player = players[pindex] - preview_str = ", paving preview is " .. (player.cursor_size * 2 + 1) .. " by " .. (player.cursor_size * 2 + 1) .. " tiles, centered on this tile. " + preview_str = ", paving preview is " + .. (player.cursor_size * 2 + 1) + .. " by " + .. (player.cursor_size * 2 + 1) + .. " tiles, centered on this tile. " if players[pindex].cursor and players[pindex].preferences.tiles_placed_from_northwest_corner then - preview_str = ", paving preview extends " .. (player.cursor_size * 2 + 1) .. " east and " .. (player.cursor_size * 2 + 1) .. " south, starting from this tile. " + preview_str = ", paving preview extends " + .. (player.cursor_size * 2 + 1) + .. " east and " + .. (player.cursor_size * 2 + 1) + .. " south, starting from this tile. " end end - printout(result,pindex) + printout(result, pindex) end - elseif players[pindex].menu == "inventory" or players[pindex].menu == "player_trash" or ((players[pindex].menu == "building" or players[pindex].menu == "vehicle") and players[pindex].building.sector > offset + #players[pindex].building.sectors) then + elseif + players[pindex].menu == "inventory" + or players[pindex].menu == "player_trash" + or ( + (players[pindex].menu == "building" or players[pindex].menu == "vehicle") + and players[pindex].building.sector > offset + #players[pindex].building.sectors + ) + then --Give slot coords (player inventory) - local x = players[pindex].inventory.index %10 - local y = math.floor(players[pindex].inventory.index/10) + 1 + local x = players[pindex].inventory.index % 10 + local y = math.floor(players[pindex].inventory.index / 10) + 1 if x == 0 then x = x + 10 y = y - 1 end printout(result .. " slot " .. x .. ", on row " .. y, pindex) - elseif (players[pindex].menu == "building" or players[pindex].menu == "vehicle") and players[pindex].building.recipe_selection == false then + elseif + (players[pindex].menu == "building" or players[pindex].menu == "vehicle") + and players[pindex].building.recipe_selection == false + then --Give slot coords (chest/building inventory) local x = -1 --Col number local y = -1 --Row number @@ -1611,33 +1827,28 @@ function read_coords(pindex, start_phrase) y = y - 1 end printout(result .. " slot " .. x .. ", on row " .. y, pindex) - elseif players[pindex].menu == "crafting" then --Read recipe ingredients / products (crafting menu) - local recipe = players[pindex].crafting.lua_recipes[players[pindex].crafting.category][players[pindex].crafting.index] + local recipe = + players[pindex].crafting.lua_recipes[players[pindex].crafting.category][players[pindex].crafting.index] result = result .. "Ingredients: " for i, v in pairs(recipe.ingredients) do ---@type LuaItemPrototype | LuaFluidPrototype local proto = game.item_prototypes[v.name] - if proto == nil then - proto = game.fluid_prototypes[v.name] - end - local localised_name = fa_localising.get(proto,pindex) + if proto == nil then proto = game.fluid_prototypes[v.name] end + local localised_name = fa_localising.get(proto, pindex) result = result .. ", " .. localised_name .. " times " .. v.amount end result = result .. ", Products: " for i, v in pairs(recipe.products) do ---@type LuaItemPrototype | LuaFluidPrototype local proto = game.item_prototypes[v.name] - if proto == nil then - proto = game.fluid_prototypes[v.name] - end - local localised_name = fa_localising.get(proto,pindex) + if proto == nil then proto = game.fluid_prototypes[v.name] end + local localised_name = fa_localising.get(proto, pindex) result = result .. ", " .. localised_name .. " times " .. v.amount end result = result .. ", craft time " .. recipe.energy .. " seconds by default." printout(result, pindex) - elseif players[pindex].menu == "technology" then --Read research requirements local techs = {} @@ -1656,41 +1867,42 @@ function read_coords(pindex, start_phrase) for a, b in pairs(dict) do pre_count = pre_count + 1 end - if pre_count == 0 then - result = result .. " None " - end + if pre_count == 0 then result = result .. " None " end for i, preq in pairs(techs[players[pindex].technology.index].prerequisites) do - result = result .. fa_localising.get(preq,pindex) .. " , " + result = result .. fa_localising.get(preq, pindex) .. " , " end - result = result .. ", and equipment " .. techs[players[pindex].technology.index].research_unit_count .. " times " - for i, ingredient in pairs(techs[players[pindex].technology.index].research_unit_ingredients ) do - result = result .. fa_localising.get_item_from_name(ingredient.name,pindex) .. ", " + result = result + .. ", and equipment " + .. techs[players[pindex].technology.index].research_unit_count + .. " times " + for i, ingredient in pairs(techs[players[pindex].technology.index].research_unit_ingredients) do + result = result .. fa_localising.get_item_from_name(ingredient.name, pindex) .. ", " end printout(result, pindex) end end - if (players[pindex].menu == "building" or players[pindex].menu == "vehicle") and players[pindex].building.recipe_selection then + if + (players[pindex].menu == "building" or players[pindex].menu == "vehicle") + and players[pindex].building.recipe_selection + then --Read recipe ingredients / products (building recipe selection) - local recipe = players[pindex].building.recipe_list[players[pindex].building.category][players[pindex].building.index] + local recipe = + players[pindex].building.recipe_list[players[pindex].building.category][players[pindex].building.index] result = result .. "Ingredients: " for i, v in pairs(recipe.ingredients) do ---@type LuaItemPrototype | LuaFluidPrototype local proto = game.item_prototypes[v.name] - if proto == nil then - proto = game.fluid_prototypes[v.name] - end - local localised_name = fa_localising.get(proto,pindex) + if proto == nil then proto = game.fluid_prototypes[v.name] end + local localised_name = fa_localising.get(proto, pindex) result = result .. ", " .. localised_name .. " x" .. v.amount .. " per cycle " end result = result .. ", products: " for i, v in pairs(recipe.products) do ---@type LuaItemPrototype | LuaFluidPrototype local proto = game.item_prototypes[v.name] - if proto == nil then - proto = game.fluid_prototypes[v.name] - end - local localised_name = fa_localising.get(proto,pindex) + if proto == nil then proto = game.fluid_prototypes[v.name] end + local localised_name = fa_localising.get(proto, pindex) result = result .. ", " .. localised_name .. " x" .. v.amount .. " per cycle " end result = result .. ", craft time " .. recipe.energy .. " seconds at default speed." @@ -1700,9 +1912,9 @@ end --Initialize the globally saved data tables for a specific player. function initialize(player) - local force=player.force.index + local force = player.force.index global.forces[force] = global.forces[force] or {} - local fa_force=global.forces[force] + local fa_force = global.forces[force] global.players[player.index] = global.players[player.index] or {} local faplayer = global.players[player.index] @@ -1737,10 +1949,11 @@ function initialize(player) faplayer.num_elements = faplayer.num_elements or 0 faplayer.player_direction = faplayer.player_direction or character.walking_state.direction faplayer.position = faplayer.position or fa_utils.center_of_tile(character.position) - faplayer.cursor_pos = faplayer.cursor_pos or fa_utils.offset_position(faplayer.position,faplayer.player_direction,1) + faplayer.cursor_pos = faplayer.cursor_pos + or fa_utils.offset_position(faplayer.position, faplayer.player_direction, 1) faplayer.walk = faplayer.walk or 0 faplayer.move_queue = faplayer.move_queue or {} - faplayer.building_direction = faplayer.building_direction or dirs.north--top + faplayer.building_direction = faplayer.building_direction or dirs.north --top faplayer.building_footprint = faplayer.building_footprint or nil faplayer.building_dir_arrow = faplayer.building_dir_arrow or nil faplayer.overhead_sprite = nil @@ -1776,235 +1989,252 @@ function initialize(player) faplayer.preferences = faplayer.preferences or {} faplayer.preferences.building_inventory_row_length = faplayer.preferences.building_inventory_row_length or 8 - if faplayer.preferences.inventory_wraps_around == nil then - faplayer.preferences.inventory_wraps_around = true - end - if faplayer.preferences.tiles_placed_from_northwest_corner ==nil then + if faplayer.preferences.inventory_wraps_around == nil then faplayer.preferences.inventory_wraps_around = true end + if faplayer.preferences.tiles_placed_from_northwest_corner == nil then faplayer.preferences.tiles_placed_from_northwest_corner = false end - faplayer.nearby = faplayer.nearby or { - index = 0, - selection = 0, - count = false, - category = 1, - ents = {}, - resources = {}, - containers = {}, - buildings = {}, - vehicles = {}, - players = {}, - enemies = {}, - other = {} - } + faplayer.nearby = faplayer.nearby + or { + index = 0, + selection = 0, + count = false, + category = 1, + ents = {}, + resources = {}, + containers = {}, + buildings = {}, + vehicles = {}, + players = {}, + enemies = {}, + other = {}, + } faplayer.nearby.ents = faplayer.nearby.ents or {} faplayer.tile = faplayer.tile or { ents = {}, tile = "", index = 1, - previous = nil + previous = nil, } faplayer.inventory = faplayer.inventory or { lua_inventory = nil, max = 0, - index = 1 - } - - faplayer.crafting = faplayer.crafting or { - lua_recipes = nil, - max = 0, index = 1, - category = 1 } + faplayer.crafting = faplayer.crafting + or { + lua_recipes = nil, + max = 0, + index = 1, + category = 1, + } + faplayer.crafting_queue = faplayer.crafting_queue or { index = 1, max = 0, - lua_queue = nil + lua_queue = nil, } - faplayer.technology = faplayer.technology or { - index = 1, - category = 1, - lua_researchable = {}, - lua_unlocked = {}, - lua_locked = {} - } + faplayer.technology = faplayer.technology + or { + index = 1, + category = 1, + lua_researchable = {}, + lua_unlocked = {}, + lua_locked = {}, + } - faplayer.building = faplayer.building or { - index = 0, - ent = nil, - sectors = nil, - sector = 0, - recipe_selection = false, - item_selection = false, - category = 0, - recipe = nil, - recipe_list = nil - } + faplayer.building = faplayer.building + or { + index = 0, + ent = nil, + sectors = nil, + sector = 0, + recipe_selection = false, + item_selection = false, + category = 0, + recipe = nil, + recipe_list = nil, + } - faplayer.belt = faplayer.belt or { - index = 1, - sector = 1, - ent = nil, - line1 = nil, - line2 = nil, - network = {}, - side = 0 - } - faplayer.warnings = faplayer.warnings or { - short = {}, - medium = {}, - long = {}, - sector = 1, - index = 1, - category = 1 - } + faplayer.belt = faplayer.belt + or { + index = 1, + sector = 1, + ent = nil, + line1 = nil, + line2 = nil, + network = {}, + side = 0, + } + faplayer.warnings = faplayer.warnings + or { + short = {}, + medium = {}, + long = {}, + sector = 1, + index = 1, + category = 1, + } faplayer.pump = faplayer.pump or { index = 0, - positions = {} + positions = {}, } faplayer.item_selector = faplayer.item_selector or { index = 0, group = 0, - subgroup = 0 + subgroup = 0, } - faplayer.travel = faplayer.travel or { - index = {x = 1, y = 0}, - creating = false, - renaming = false - } + faplayer.travel = faplayer.travel + or { + index = { x = 1, y = 0 }, + creating = false, + renaming = false, + } - faplayer.structure_travel = faplayer.structure_travel or { - network = {}, - current = nil, - index = 0, - direction = "none" - } + faplayer.structure_travel = faplayer.structure_travel + or { + network = {}, + current = nil, + index = 0, + direction = "none", + } - faplayer.rail_builder = faplayer.rail_builder or { - index = 0, - index_max = 1, - rail = nil, - rail_type = 0 - } + faplayer.rail_builder = faplayer.rail_builder + or { + index = 0, + index_max = 1, + rail = nil, + rail_type = 0, + } - faplayer.train_menu = faplayer.train_menu or { - index = 0, - renaming = false, - locomotive = nil, - wait_time = 300, - index_2 = 0, - selecting_station = false - } + faplayer.train_menu = faplayer.train_menu + or { + index = 0, + renaming = false, + locomotive = nil, + wait_time = 300, + index_2 = 0, + selecting_station = false, + } faplayer.spider_menu = faplayer.spider_menu or { index = 0, renaming = false, -spider = nil + spider = nil, } - faplayer.train_stop_menu = faplayer.train_stop_menu or { - index = 0, - renaming = false, - stop = nil, - wait_condition = "time", - wait_time_seconds = 30, - safety_wait_enabled = true - } + faplayer.train_stop_menu = faplayer.train_stop_menu + or { + index = 0, + renaming = false, + stop = nil, + wait_condition = "time", + wait_time_seconds = 30, + safety_wait_enabled = true, + } faplayer.valid_train_stop_list = faplayer.valid_train_stop_list or {} faplayer.roboport_menu = faplayer.roboport_menu or { port = nil, index = 0, - renaming = false + renaming = false, } - faplayer.blueprint_menu = faplayer.blueprint_menu or { - index = 0, - edit_label = false, - edit_description = false, - edit_export = false, - edit_import = false - } + faplayer.blueprint_menu = faplayer.blueprint_menu + or { + index = 0, + edit_label = false, + edit_description = false, + edit_export = false, + edit_import = false, + } - faplayer.blueprint_book_menu = faplayer.blueprint_book_menu or { - index = 0, - menu_length = 0, - list_mode = true, - edit_label = false, - edit_description = false, - edit_export = false, - edit_import = false + faplayer.blueprint_book_menu = faplayer.blueprint_book_menu + or { + index = 0, + menu_length = 0, + list_mode = true, + edit_label = false, + edit_description = false, + edit_export = false, + edit_import = false, } - if table_size(faplayer.mapped) == 0 then - player.force.rechart() - end + if table_size(faplayer.mapped) == 0 then player.force.rechart() end faplayer.localisations = faplayer.localisations or {} faplayer.translation_id_lookup = faplayer.translation_id_lookup or {} fa_localising.check_player(player.index) - faplayer.bump = faplayer.bump or { - last_bump_tick = 1, --Updated in bump checker - last_dir_key_tick = 1, --Updated in key press handlers - last_dir_key_1st = nil, --Updated in key press handlers - last_dir_key_2nd = nil, --Updated in key press handlers - last_pos_1 = nil, --Updated in bump checker - last_pos_2 = nil, --Updated in bump checker - last_pos_3 = nil, --Updated in bump checker - last_pos_4 = nil, --Updated in bump checker - last_dir_2 = nil, --Updated in bump checker - last_dir_1 = nil --Updated in bump checker + faplayer.bump = faplayer.bump + or { + last_bump_tick = 1, --Updated in bump checker + last_dir_key_tick = 1, --Updated in key press handlers + last_dir_key_1st = nil, --Updated in key press handlers + last_dir_key_2nd = nil, --Updated in key press handlers + last_pos_1 = nil, --Updated in bump checker + last_pos_2 = nil, --Updated in bump checker + last_pos_3 = nil, --Updated in bump checker + last_pos_4 = nil, --Updated in bump checker + last_dir_2 = nil, --Updated in bump checker + last_dir_1 = nil, --Updated in bump checker } - end --Update the position info and cursor info during smooth walking. -script.on_event(defines.events.on_player_changed_position,function(event) +script.on_event(defines.events.on_player_changed_position, function(event) local pindex = event.player_index local p = game.get_player(pindex) - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end if players[pindex].walk == 2 then players[pindex].position = p.position - local pos = (p.position) + local pos = p.position if p.walking_state.direction ~= players[pindex].player_direction and players[pindex].cursor == false then --Directions mismatch. Turn to new direction --turn (Note, this code handles diagonal turns and other direction changes) if p.character ~= nil then players[pindex].player_direction = p.character.direction else players[pindex].player_direction = p.walking_state.direction - if p.walking_state.direction == nil then - players[pindex].player_direction = dirs.north - end + if p.walking_state.direction == nil then players[pindex].player_direction = dirs.north end end - local new_pos = (fa_utils.offset_position(pos,players[pindex].player_direction,1.0)) + local new_pos = (fa_utils.offset_position(pos, players[pindex].player_direction, 1.0)) players[pindex].cursor_pos = new_pos --Build lock building + rotate belts in hand unless cursor mode local stack = p.cursor_stack - if players[pindex].build_lock and stack.valid_for_read and stack.valid and stack.prototype.place_result ~= nil and (stack.prototype.place_result.type == "transport-belt" or stack.name == "rail") then + if + players[pindex].build_lock + and stack.valid_for_read + and stack.valid + and stack.prototype.place_result ~= nil + and (stack.prototype.place_result.type == "transport-belt" or stack.name == "rail") + then turn_to_cursor_direction_cardinal(pindex) players[pindex].building_direction = players[pindex].player_direction - fa_building_tools.build_item_in_hand(pindex)--build extra belt when turning + fa_building_tools.build_item_in_hand(pindex) --build extra belt when turning end elseif players[pindex].cursor == false then --Directions same: Walk straight - local new_pos = (fa_utils.offset_position(pos,players[pindex].player_direction,1)) + local new_pos = (fa_utils.offset_position(pos, players[pindex].player_direction, 1)) players[pindex].cursor_pos = new_pos --Build lock building + rotate belts in hand unless cursor mode if players[pindex].build_lock then local stack = p.cursor_stack - if stack and stack.valid_for_read and stack.valid and stack.prototype.place_result ~= nil and stack.prototype.place_result.type == "transport-belt" then + if + stack + and stack.valid_for_read + and stack.valid + and stack.prototype.place_result ~= nil + and stack.prototype.place_result.type == "transport-belt" + then turn_to_cursor_direction_cardinal(pindex) players[pindex].building_direction = players[pindex].player_direction end @@ -2014,23 +2244,29 @@ script.on_event(defines.events.on_player_changed_position,function(event) --Update cursor graphics local stack = p.cursor_stack - if stack and stack.valid_for_read and stack.valid then - fa_graphics.sync_build_cursor_graphics(pindex) - end + if stack and stack.valid_for_read and stack.valid then fa_graphics.sync_build_cursor_graphics(pindex) end --Name a detected entity that you can or cannot walk on, or a tile you cannot walk on, and play a sound to indicate multiple consecutive detections refresh_player_tile(pindex) local ent = get_selected_ent(pindex) - if not players[pindex].vanilla_mode and ((ent ~= nil and ent.valid) or (p.surface.can_place_entity{name = "character", position = players[pindex].cursor_pos} == false)) then + if + not players[pindex].vanilla_mode + and ( + (ent ~= nil and ent.valid) + or (p.surface.can_place_entity({ name = "character", position = players[pindex].cursor_pos }) == false) + ) + then fa_graphics.draw_cursor_highlight(pindex, ent, nil) - if p.driving then - return - end + if p.driving then return end - if ent ~= nil and ent.valid and (p.character == nil or (p.character ~= nil and p.character.unit_number ~= ent.unit_number)) then + if + ent ~= nil + and ent.valid + and (p.character == nil or (p.character ~= nil and p.character.unit_number ~= ent.unit_number)) + then fa_graphics.draw_cursor_highlight(pindex, ent, nil) p.selected = ent - p.play_sound{path = "Close-Inventory-Sound", volume_modifier = 0.75} + p.play_sound({ path = "Close-Inventory-Sound", volume_modifier = 0.75 }) else fa_graphics.draw_cursor_highlight(pindex, nil, nil) p.selected = nil @@ -2045,15 +2281,15 @@ script.on_event(defines.events.on_player_changed_position,function(event) end) --Calls the appropriate menu movement function for a player and the input direction. -function menu_cursor_move(direction,pindex) - players[pindex].preferences.inventory_wraps_around = true--laterdo make this a setting to toggle - if direction == defines.direction.north then +function menu_cursor_move(direction, pindex) + players[pindex].preferences.inventory_wraps_around = true --laterdo make this a setting to toggle + if direction == defines.direction.north then menu_cursor_up(pindex) elseif direction == defines.direction.south then menu_cursor_down(pindex) - elseif direction == defines.direction.east then + elseif direction == defines.direction.east then menu_cursor_right(pindex) - elseif direction == defines.direction.west then + elseif direction == defines.direction.west then menu_cursor_left(pindex) end end @@ -2077,87 +2313,88 @@ function menu_cursor_up(pindex) players[pindex].item_selector.index = players[pindex].item_selector.subgroup players[pindex].item_selector.subgroup = 0 read_item_selector_slot(pindex) - end - + end elseif players[pindex].menu == "inventory" then - players[pindex].inventory.index = players[pindex].inventory.index -10 + players[pindex].inventory.index = players[pindex].inventory.index - 10 if players[pindex].inventory.index < 1 then if players[pindex].preferences.inventory_wraps_around == true then --Wrap around setting: Move to the inventory end and read slot players[pindex].inventory.index = players[pindex].inventory.max + players[pindex].inventory.index - game.get_player(pindex).play_sound{path = "inventory-wrap-around"} + game.get_player(pindex).play_sound({ path = "inventory-wrap-around" }) read_inventory_slot(pindex) else --Border setting: Undo change and play "wall" sound - players[pindex].inventory.index = players[pindex].inventory.index +10 - game.get_player(pindex).play_sound{path = "inventory-edge"} + players[pindex].inventory.index = players[pindex].inventory.index + 10 + game.get_player(pindex).play_sound({ path = "inventory-edge" }) --printout("Border.", pindex) end else - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) read_inventory_slot(pindex) end elseif players[pindex].menu == "player_trash" then local trash_inv = game.get_player(pindex).get_inventory(defines.inventory.character_trash) - players[pindex].inventory.index = players[pindex].inventory.index -10 + players[pindex].inventory.index = players[pindex].inventory.index - 10 if players[pindex].inventory.index < 1 then if players[pindex].preferences.inventory_wraps_around == true then --Wrap around setting: Move to the inventory end and read slot players[pindex].inventory.index = #trash_inv + players[pindex].inventory.index - game.get_player(pindex).play_sound{path = "inventory-wrap-around"} + game.get_player(pindex).play_sound({ path = "inventory-wrap-around" }) read_inventory_slot(pindex, "", trash_inv) else --Border setting: Undo change and play "wall" sound - players[pindex].inventory.index = players[pindex].inventory.index +10 - game.get_player(pindex).play_sound{path = "inventory-edge"} + players[pindex].inventory.index = players[pindex].inventory.index + 10 + game.get_player(pindex).play_sound({ path = "inventory-edge" }) --printout("Border.", pindex) end else - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) read_inventory_slot(pindex, "", trash_inv) end elseif players[pindex].menu == "crafting" then - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) players[pindex].crafting.index = 1 players[pindex].crafting.category = players[pindex].crafting.category - 1 - if players[pindex].crafting.category < 1 then - players[pindex].crafting.category = players[pindex].crafting.max - end + if players[pindex].crafting.category < 1 then players[pindex].crafting.category = players[pindex].crafting.max end fa_crafting.read_crafting_slot(pindex, "", true) elseif players[pindex].menu == "crafting_queue" then - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) fa_crafting.load_crafting_queue(pindex) players[pindex].crafting_queue.index = 1 fa_crafting.read_crafting_queue(pindex) - elseif (players[pindex].menu == "building" or players[pindex].menu == "vehicle") then + elseif players[pindex].menu == "building" or players[pindex].menu == "vehicle" then --Move one row up in a building inventory of some kind if players[pindex].building.sector <= #players[pindex].building.sectors then --Most building sectors, eg. chest rows - if players[pindex].building.sectors[players[pindex].building.sector].inventory == nil or #players[pindex].building.sectors[players[pindex].building.sector].inventory < 1 then + if + players[pindex].building.sectors[players[pindex].building.sector].inventory == nil + or #players[pindex].building.sectors[players[pindex].building.sector].inventory < 1 + then printout("blank sector", pindex) return end --Move one row up in building inventory local row_length = players[pindex].preferences.building_inventory_row_length if #players[pindex].building.sectors[players[pindex].building.sector].inventory > row_length then - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) players[pindex].building.index = players[pindex].building.index - row_length if players[pindex].building.index < 1 then --Wrap around to building inventory last row - game.get_player(pindex).play_sound{path = "inventory-wrap-around"} - players[pindex].building.index = players[pindex].building.index + #players[pindex].building.sectors[players[pindex].building.sector].inventory + game.get_player(pindex).play_sound({ path = "inventory-wrap-around" }) + players[pindex].building.index = players[pindex].building.index + + #players[pindex].building.sectors[players[pindex].building.sector].inventory end else --Inventory size < row length: Wrap over to the same slot - game.get_player(pindex).play_sound{path = "inventory-wrap-around"} + game.get_player(pindex).play_sound({ path = "inventory-wrap-around" }) --players[pindex].building.index = 1 end - fa_sectors.read_sector_slot(pindex,false) + fa_sectors.read_sector_slot(pindex, false) elseif players[pindex].building.recipe_list == nil then --Move one row up in player inventory - game.get_player(pindex).play_sound{path = "Inventory-Move"} - players[pindex].inventory.index = players[pindex].inventory.index -10 + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) + players[pindex].inventory.index = players[pindex].inventory.index - 10 if players[pindex].inventory.index < 1 then players[pindex].inventory.index = players[pindex].inventory.max + players[pindex].inventory.index end @@ -2166,7 +2403,7 @@ function menu_cursor_up(pindex) if players[pindex].building.sector == #players[pindex].building.sectors + 1 then --Last building sector. Case = ??? ** if players[pindex].building.recipe_selection then - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) players[pindex].building.category = players[pindex].building.category - 1 players[pindex].building.index = 1 if players[pindex].building.category < 1 then @@ -2176,14 +2413,14 @@ function menu_cursor_up(pindex) fa_sectors.read_building_recipe(pindex) else --Case = ??? - game.get_player(pindex).play_sound{path = "Inventory-Move"} - players[pindex].inventory.index = players[pindex].inventory.index -10 + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) + players[pindex].inventory.index = players[pindex].inventory.index - 10 if players[pindex].inventory.index < 1 then players[pindex].inventory.index = players[pindex].inventory.max + players[pindex].inventory.index end read_inventory_slot(pindex) - end end + end elseif players[pindex].menu == "technology" then if players[pindex].technology.category > 1 then players[pindex].technology.category = players[pindex].technology.category - 1 @@ -2196,11 +2433,13 @@ function menu_cursor_up(pindex) elseif players[pindex].technology.category == 3 then printout("Past Research", pindex) end - elseif players[pindex].menu == "belt" then if players[pindex].belt.sector == 1 then - if (players[pindex].belt.side == 1 and players[pindex].belt.line1.valid and players[pindex].belt.index > 1) or (players[pindex].belt.side == 2 and players[pindex].belt.line2.valid and players[pindex].belt.index > 1) then - game.get_player(pindex).play_sound{path = "Inventory-Move"} + if + (players[pindex].belt.side == 1 and players[pindex].belt.line1.valid and players[pindex].belt.index > 1) + or (players[pindex].belt.side == 2 and players[pindex].belt.line2.valid and players[pindex].belt.index > 1) + then + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) players[pindex].belt.index = players[pindex].belt.index - 1 end elseif players[pindex].belt.sector == 2 then @@ -2211,7 +2450,7 @@ function menu_cursor_up(pindex) max = #players[pindex].belt.network.combined.right end if players[pindex].belt.index > 1 then - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) players[pindex].belt.index = math.min(players[pindex].belt.index - 1, max) end elseif players[pindex].belt.sector == 3 then @@ -2222,7 +2461,7 @@ function menu_cursor_up(pindex) max = #players[pindex].belt.network.downstream.right end if players[pindex].belt.index > 1 then - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) players[pindex].belt.index = math.min(players[pindex].belt.index - 1, max) end elseif players[pindex].belt.sector == 4 then @@ -2233,21 +2472,20 @@ function menu_cursor_up(pindex) max = #players[pindex].belt.network.upstream.right end if players[pindex].belt.index > 1 then - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) players[pindex].belt.index = math.min(players[pindex].belt.index - 1, max) end - end fa_belts.read_belt_slot(pindex) elseif players[pindex].menu == "warnings" then if players[pindex].warnings.category > 1 then players[pindex].warnings.category = players[pindex].warnings.category - 1 - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) players[pindex].warnings.index = 1 end fa_warnings.read_warnings_slot(pindex) elseif players[pindex].menu == "pump" then - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) players[pindex].pump.index = math.max(1, players[pindex].pump.index - 1) local dir = "" if players[pindex].pump.positions[players[pindex].pump.index].direction == 0 then @@ -2260,7 +2498,25 @@ function menu_cursor_up(pindex) dir = " West" end - printout("Option " .. players[pindex].pump.index .. ": " .. math.floor(fa_utils.distance(game.get_player(pindex).position, players[pindex].pump.positions[players[pindex].pump.index].position)) .. " meters " .. fa_utils.direction(game.get_player(pindex).position, players[pindex].pump.positions[players[pindex].pump.index].position) .. " Facing " .. dir, pindex) + printout( + "Option " + .. players[pindex].pump.index + .. ": " + .. math.floor( + fa_utils.distance( + game.get_player(pindex).position, + players[pindex].pump.positions[players[pindex].pump.index].position + ) + ) + .. " meters " + .. fa_utils.direction( + game.get_player(pindex).position, + players[pindex].pump.positions[players[pindex].pump.index].position + ) + .. " Facing " + .. dir, + pindex + ) elseif players[pindex].menu == "travel" then fa_travel.fast_travel_menu_up(pindex) elseif players[pindex].menu == "structure-travel" then @@ -2282,7 +2538,6 @@ function menu_cursor_up(pindex) signal_selector_group_up(pindex) read_selected_signal_group(pindex, "") end - end --Moves downwards in a menu. Todo: split by menu."menu_down" @@ -2290,122 +2545,119 @@ function menu_cursor_down(pindex) if players[pindex].item_selection then if players[pindex].item_selector.group == 0 then players[pindex].item_selector.group = players[pindex].item_selector.index - players[pindex].item_cache = fa_utils.get_iterable_array(players[pindex].item_cache[players[pindex].item_selector.group].subgroups) + players[pindex].item_cache = + fa_utils.get_iterable_array(players[pindex].item_cache[players[pindex].item_selector.group].subgroups) prune_item_groups(players[pindex].item_cache) players[pindex].item_selector.index = 1 read_item_selector_slot(pindex) elseif players[pindex].item_selector.subgroup == 0 then players[pindex].item_selector.subgroup = players[pindex].item_selector.index - local prototypes = game.get_filtered_item_prototypes{{filter="subgroup",subgroup = players[pindex].item_cache[players[pindex].item_selector.index].name}} + local prototypes = game.get_filtered_item_prototypes({ + { filter = "subgroup", subgroup = players[pindex].item_cache[players[pindex].item_selector.index].name }, + }) players[pindex].item_cache = fa_utils.get_iterable_array(prototypes) players[pindex].item_selector.index = 1 read_item_selector_slot(pindex) else printout("Press left bracket to confirm your selection.", pindex) - end - + end elseif players[pindex].menu == "inventory" then - players[pindex].inventory.index = players[pindex].inventory.index +10 + players[pindex].inventory.index = players[pindex].inventory.index + 10 if players[pindex].inventory.index > players[pindex].inventory.max then if players[pindex].preferences.inventory_wraps_around == true then --Wrap around setting: Wrap over to first row players[pindex].inventory.index = players[pindex].inventory.index % 10 - if players[pindex].inventory.index == 0 then - players[pindex].inventory.index = 10 - end - game.get_player(pindex).play_sound{path = "inventory-wrap-around"} + if players[pindex].inventory.index == 0 then players[pindex].inventory.index = 10 end + game.get_player(pindex).play_sound({ path = "inventory-wrap-around" }) read_inventory_slot(pindex) else --Border setting: Undo change and play "wall" sound - players[pindex].inventory.index = players[pindex].inventory.index -10 - game.get_player(pindex).play_sound{path = "inventory-edge"} + players[pindex].inventory.index = players[pindex].inventory.index - 10 + game.get_player(pindex).play_sound({ path = "inventory-edge" }) --printout("Border.", pindex) end else - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) read_inventory_slot(pindex) end elseif players[pindex].menu == "player_trash" then local trash_inv = game.get_player(pindex).get_inventory(defines.inventory.character_trash) - players[pindex].inventory.index = players[pindex].inventory.index +10 + players[pindex].inventory.index = players[pindex].inventory.index + 10 if players[pindex].inventory.index > #trash_inv then if players[pindex].preferences.inventory_wraps_around == true then --Wrap around setting: Wrap over to first row players[pindex].inventory.index = players[pindex].inventory.index % 10 - if players[pindex].inventory.index == 0 then - players[pindex].inventory.index = 10 - end - game.get_player(pindex).play_sound{path = "inventory-wrap-around"} + if players[pindex].inventory.index == 0 then players[pindex].inventory.index = 10 end + game.get_player(pindex).play_sound({ path = "inventory-wrap-around" }) read_inventory_slot(pindex, "", trash_inv) else --Border setting: Undo change and play "wall" sound - players[pindex].inventory.index = players[pindex].inventory.index -10 - game.get_player(pindex).play_sound{path = "inventory-edge"} + players[pindex].inventory.index = players[pindex].inventory.index - 10 + game.get_player(pindex).play_sound({ path = "inventory-edge" }) --printout("Border.", pindex) end else - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) read_inventory_slot(pindex, "", trash_inv) end elseif players[pindex].menu == "crafting" then - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) players[pindex].crafting.index = 1 players[pindex].crafting.category = players[pindex].crafting.category + 1 - if players[pindex].crafting.category > players[pindex].crafting.max then - players[pindex].crafting.category = 1 - end + if players[pindex].crafting.category > players[pindex].crafting.max then players[pindex].crafting.category = 1 end fa_crafting.read_crafting_slot(pindex, "", true) elseif players[pindex].menu == "crafting_queue" then - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) fa_crafting.load_crafting_queue(pindex) players[pindex].crafting_queue.index = players[pindex].crafting_queue.max fa_crafting.read_crafting_queue(pindex) - elseif (players[pindex].menu == "building" or players[pindex].menu == "vehicle") then + elseif players[pindex].menu == "building" or players[pindex].menu == "vehicle" then --Move one row down in a building inventory of some kind if players[pindex].building.sector <= #players[pindex].building.sectors then --Most building sectors, eg. chest rows - if players[pindex].building.sectors[players[pindex].building.sector].inventory == nil or #players[pindex].building.sectors[players[pindex].building.sector].inventory < 1 then + if + players[pindex].building.sectors[players[pindex].building.sector].inventory == nil + or #players[pindex].building.sectors[players[pindex].building.sector].inventory < 1 + then printout("blank sector", pindex) return end - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) local row_length = players[pindex].preferences.building_inventory_row_length if #players[pindex].building.sectors[players[pindex].building.sector].inventory > row_length then --Move one row down players[pindex].building.index = players[pindex].building.index + row_length - if players[pindex].building.index > #players[pindex].building.sectors[players[pindex].building.sector].inventory then + if + players[pindex].building.index + > #players[pindex].building.sectors[players[pindex].building.sector].inventory + then --Wrap around to the building inventory first row - game.get_player(pindex).play_sound{path = "inventory-wrap-around"} + game.get_player(pindex).play_sound({ path = "inventory-wrap-around" }) players[pindex].building.index = players[pindex].building.index % row_length --If the row is shorter than usual, get to its end - if players[pindex].building.index < 1 then - players[pindex].building.index = row_length - end + if players[pindex].building.index < 1 then players[pindex].building.index = row_length end end else --Inventory size < row length: Wrap over to the same slot - game.get_player(pindex).play_sound{path = "inventory-wrap-around"} + game.get_player(pindex).play_sound({ path = "inventory-wrap-around" }) end - fa_sectors.read_sector_slot(pindex,false) + fa_sectors.read_sector_slot(pindex, false) elseif players[pindex].building.recipe_list == nil then --Move one row down in player inventory - game.get_player(pindex).play_sound{path = "Inventory-Move"} - players[pindex].inventory.index = players[pindex].inventory.index +10 + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) + players[pindex].inventory.index = players[pindex].inventory.index + 10 if players[pindex].inventory.index > players[pindex].inventory.max then - players[pindex].inventory.index = players[pindex].inventory.index%10 - if players[pindex].inventory.index == 0 then - players[pindex].inventory.index = 10 - end - + players[pindex].inventory.index = players[pindex].inventory.index % 10 + if players[pindex].inventory.index == 0 then players[pindex].inventory.index = 10 end end read_inventory_slot(pindex) else if players[pindex].building.sector == #players[pindex].building.sectors + 1 then --Last building sector. Case = ??? ** if players[pindex].building.recipe_selection then - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) players[pindex].building.index = 1 players[pindex].building.category = players[pindex].building.category + 1 if players[pindex].building.category > #players[pindex].building.recipe_list then @@ -2415,17 +2667,15 @@ function menu_cursor_down(pindex) fa_sectors.read_building_recipe(pindex) else --Case = ??? - game.get_player(pindex).play_sound{path = "Inventory-Move"} - players[pindex].inventory.index = players[pindex].inventory.index +10 + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) + players[pindex].inventory.index = players[pindex].inventory.index + 10 if players[pindex].inventory.index > players[pindex].inventory.max then - players[pindex].inventory.index = players[pindex].inventory.index%10 - if players[pindex].inventory.index == 0 then - players[pindex].inventory.index = 10 - end + players[pindex].inventory.index = players[pindex].inventory.index % 10 + if players[pindex].inventory.index == 0 then players[pindex].inventory.index = 10 end end read_inventory_slot(pindex) - end end + end elseif players[pindex].menu == "technology" then if players[pindex].technology.category < 3 then players[pindex].technology.category = players[pindex].technology.category + 1 @@ -2438,11 +2688,13 @@ function menu_cursor_down(pindex) elseif players[pindex].technology.category == 3 then printout("Past Research", pindex) end - elseif players[pindex].menu == "belt" then if players[pindex].belt.sector == 1 then - if (players[pindex].belt.side == 1 and players[pindex].belt.line1.valid and players[pindex].belt.index < 4) or (players[pindex].belt.side == 2 and players[pindex].belt.line2.valid and players[pindex].belt.index < 4) then - game.get_player(pindex).play_sound{path = "Inventory-Move"} + if + (players[pindex].belt.side == 1 and players[pindex].belt.line1.valid and players[pindex].belt.index < 4) + or (players[pindex].belt.side == 2 and players[pindex].belt.line2.valid and players[pindex].belt.index < 4) + then + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) players[pindex].belt.index = players[pindex].belt.index + 1 end elseif players[pindex].belt.sector == 2 then @@ -2453,7 +2705,7 @@ function menu_cursor_down(pindex) max = #players[pindex].belt.network.combined.right end if players[pindex].belt.index < max then - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) players[pindex].belt.index = math.min(players[pindex].belt.index + 1, max) end elseif players[pindex].belt.sector == 3 then @@ -2464,7 +2716,7 @@ function menu_cursor_down(pindex) max = #players[pindex].belt.network.downstream.right end if players[pindex].belt.index < max then - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) players[pindex].belt.index = math.min(players[pindex].belt.index + 1, max) end elseif players[pindex].belt.sector == 4 then @@ -2475,10 +2727,9 @@ function menu_cursor_down(pindex) max = #players[pindex].belt.network.upstream.right end if players[pindex].belt.index < max then - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) players[pindex].belt.index = math.min(players[pindex].belt.index + 1, max) end - end fa_belts.read_belt_slot(pindex) elseif players[pindex].menu == "warnings" then @@ -2488,16 +2739,16 @@ function menu_cursor_down(pindex) elseif players[pindex].warnings.sector == 2 then warnings = players[pindex].warnings.medium.warnings elseif players[pindex].warnings.sector == 3 then - warnings= players[pindex].warnings.long.warnings + warnings = players[pindex].warnings.long.warnings end if players[pindex].warnings.category < #warnings then players[pindex].warnings.category = players[pindex].warnings.category + 1 - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) players[pindex].warnings.index = 1 end fa_warnings.read_warnings_slot(pindex) elseif players[pindex].menu == "pump" then - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) players[pindex].pump.index = math.min(#players[pindex].pump.positions, players[pindex].pump.index + 1) local dir = "" if players[pindex].pump.positions[players[pindex].pump.index].direction == 0 then @@ -2510,7 +2761,25 @@ function menu_cursor_down(pindex) dir = " West" end - printout("Option " .. players[pindex].pump.index .. ": " .. math.floor(fa_utils.distance(game.get_player(pindex).position, players[pindex].pump.positions[players[pindex].pump.index].position)) .. " meters " .. fa_utils.direction(game.get_player(pindex).position, players[pindex].pump.positions[players[pindex].pump.index].position) .. " Facing " .. dir, pindex) + printout( + "Option " + .. players[pindex].pump.index + .. ": " + .. math.floor( + fa_utils.distance( + game.get_player(pindex).position, + players[pindex].pump.positions[players[pindex].pump.index].position + ) + ) + .. " meters " + .. fa_utils.direction( + game.get_player(pindex).position, + players[pindex].pump.positions[players[pindex].pump.index].position + ) + .. " Facing " + .. dir, + pindex + ) elseif players[pindex].menu == "travel" then fa_travel.fast_travel_menu_down(pindex) elseif players[pindex].menu == "structure-travel" then @@ -2532,62 +2801,59 @@ function menu_cursor_down(pindex) signal_selector_group_down(pindex) read_selected_signal_group(pindex, "") end - end --Moves to the left in a menu. Todo: split by menu."menu_left" function menu_cursor_left(pindex) if players[pindex].item_selection then - players[pindex].item_selector.index = math.max(1, players[pindex].item_selector.index - 1) - read_item_selector_slot(pindex) - + players[pindex].item_selector.index = math.max(1, players[pindex].item_selector.index - 1) + read_item_selector_slot(pindex) elseif players[pindex].menu == "inventory" then - players[pindex].inventory.index = players[pindex].inventory.index -1 - if players[pindex].inventory.index%10 == 0 then + players[pindex].inventory.index = players[pindex].inventory.index - 1 + if players[pindex].inventory.index % 10 == 0 then if players[pindex].preferences.inventory_wraps_around == true then --Wrap around setting: Move and play move sound and read slot players[pindex].inventory.index = players[pindex].inventory.index + 10 - game.get_player(pindex).play_sound{path = "inventory-wrap-around"} + game.get_player(pindex).play_sound({ path = "inventory-wrap-around" }) read_inventory_slot(pindex) else --Border setting: Undo change and play "wall" sound - players[pindex].inventory.index = players[pindex].inventory.index +1 - game.get_player(pindex).play_sound{path = "inventory-edge"} + players[pindex].inventory.index = players[pindex].inventory.index + 1 + game.get_player(pindex).play_sound({ path = "inventory-edge" }) --printout("Border.", pindex) end else - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) read_inventory_slot(pindex) end elseif players[pindex].menu == "player_trash" then local trash_inv = game.get_player(pindex).get_inventory(defines.inventory.character_trash) - players[pindex].inventory.index = players[pindex].inventory.index -1 - if players[pindex].inventory.index%10 == 0 then + players[pindex].inventory.index = players[pindex].inventory.index - 1 + if players[pindex].inventory.index % 10 == 0 then if players[pindex].preferences.inventory_wraps_around == true then --Wrap around setting: Move and play move sound and read slot players[pindex].inventory.index = players[pindex].inventory.index + 10 - game.get_player(pindex).play_sound{path = "inventory-wrap-around"} + game.get_player(pindex).play_sound({ path = "inventory-wrap-around" }) read_inventory_slot(pindex, "", trash_inv) else --Border setting: Undo change and play "wall" sound - players[pindex].inventory.index = players[pindex].inventory.index +1 - game.get_player(pindex).play_sound{path = "inventory-edge"} + players[pindex].inventory.index = players[pindex].inventory.index + 1 + game.get_player(pindex).play_sound({ path = "inventory-edge" }) --printout("Border.", pindex) end else - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) read_inventory_slot(pindex, "", trash_inv) end elseif players[pindex].menu == "crafting" then - game.get_player(pindex).play_sound{path = "Inventory-Move"} - players[pindex].crafting.index = players[pindex].crafting.index -1 + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) + players[pindex].crafting.index = players[pindex].crafting.index - 1 if players[pindex].crafting.index < 1 then players[pindex].crafting.index = #players[pindex].crafting.lua_recipes[players[pindex].crafting.category] end fa_crafting.read_crafting_slot(pindex) - elseif players[pindex].menu == "crafting_queue" then - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) fa_crafting.load_crafting_queue(pindex) if players[pindex].crafting_queue.index < 2 then players[pindex].crafting_queue.index = players[pindex].crafting_queue.max @@ -2595,40 +2861,48 @@ function menu_cursor_left(pindex) players[pindex].crafting_queue.index = players[pindex].crafting_queue.index - 1 end fa_crafting.read_crafting_queue(pindex) - elseif (players[pindex].menu == "building" or players[pindex].menu == "vehicle") then + elseif players[pindex].menu == "building" or players[pindex].menu == "vehicle" then --Move along a row in a building inventory if players[pindex].building.sector <= #players[pindex].building.sectors then --Most building sectors, e.g. chest rows - if players[pindex].building.sectors[players[pindex].building.sector].inventory == nil or #players[pindex].building.sectors[players[pindex].building.sector].inventory < 1 then + if + players[pindex].building.sectors[players[pindex].building.sector].inventory == nil + or #players[pindex].building.sectors[players[pindex].building.sector].inventory < 1 + then printout("blank sector", pindex) return end - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) local row_length = players[pindex].preferences.building_inventory_row_length if #players[pindex].building.sectors[players[pindex].building.sector].inventory > row_length then players[pindex].building.index = players[pindex].building.index - 1 if players[pindex].building.index % row_length < 1 then --Wrap around to the end of this row - game.get_player(pindex).play_sound{path = "inventory-wrap-around"} + game.get_player(pindex).play_sound({ path = "inventory-wrap-around" }) players[pindex].building.index = players[pindex].building.index + row_length - if players[pindex].building.index > #players[pindex].building.sectors[players[pindex].building.sector].inventory then + if + players[pindex].building.index + > #players[pindex].building.sectors[players[pindex].building.sector].inventory + then --If this final row is short, just jump to the end of the inventory - players[pindex].building.index = #players[pindex].building.sectors[players[pindex].building.sector].inventory + players[pindex].building.index = + #players[pindex].building.sectors[players[pindex].building.sector].inventory end end else players[pindex].building.index = players[pindex].building.index - 1 if players[pindex].building.index < 1 then --Wrap around to the end of this single-row inventory - game.get_player(pindex).play_sound{path = "inventory-wrap-around"} - players[pindex].building.index = #players[pindex].building.sectors[players[pindex].building.sector].inventory + game.get_player(pindex).play_sound({ path = "inventory-wrap-around" }) + players[pindex].building.index = + #players[pindex].building.sectors[players[pindex].building.sector].inventory end end - fa_sectors.read_sector_slot(pindex,false) + fa_sectors.read_sector_slot(pindex, false) elseif players[pindex].building.recipe_list == nil then - game.get_player(pindex).play_sound{path = "Inventory-Move"} - players[pindex].inventory.index = players[pindex].inventory.index -1 - if players[pindex].inventory.index%10 < 1 then + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) + players[pindex].inventory.index = players[pindex].inventory.index - 1 + if players[pindex].inventory.index % 10 < 1 then players[pindex].inventory.index = players[pindex].inventory.index + 10 end read_inventory_slot(pindex) @@ -2636,35 +2910,35 @@ function menu_cursor_left(pindex) if players[pindex].building.sector == #players[pindex].building.sectors + 1 then --Recipe selection if players[pindex].building.recipe_selection then - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) players[pindex].building.index = players[pindex].building.index - 1 if players[pindex].building.index < 1 then - players[pindex].building.index = #players[pindex].building.recipe_list[players[pindex].building.category] + players[pindex].building.index = + #players[pindex].building.recipe_list[players[pindex].building.category] end end fa_sectors.read_building_recipe(pindex) else --Case ??? - game.get_player(pindex).play_sound{path = "Inventory-Move"} - players[pindex].inventory.index = players[pindex].inventory.index -1 - if players[pindex].inventory.index%10 < 1 then + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) + players[pindex].inventory.index = players[pindex].inventory.index - 1 + if players[pindex].inventory.index % 10 < 1 then players[pindex].inventory.index = players[pindex].inventory.index + 10 end read_inventory_slot(pindex) - end end - + end elseif players[pindex].menu == "technology" then if players[pindex].technology.index > 1 then players[pindex].technology.index = players[pindex].technology.index - 1 - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) end read_technology_slot(pindex) elseif players[pindex].menu == "belt" then if players[pindex].belt.side == 2 then players[pindex].belt.side = 1 - game.get_player(pindex).play_sound{path = "Inventory-Move"} - if not pcall(function() + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) + if not pcall(function() fa_belts.read_belt_slot(pindex) end) then printout("Blank", pindex) @@ -2673,7 +2947,7 @@ function menu_cursor_left(pindex) elseif players[pindex].menu == "warnings" then if players[pindex].warnings.index > 1 then players[pindex].warnings.index = players[pindex].warnings.index - 1 - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) end fa_warnings.read_warnings_slot(pindex) elseif players[pindex].menu == "travel" then @@ -2689,56 +2963,55 @@ end ----Moves to the right in a menu. Todo: split by menu. "menu_right" function menu_cursor_right(pindex) if players[pindex].item_selection then - players[pindex].item_selector.index = math.min(#players[pindex].item_cache, players[pindex].item_selector.index + 1) - read_item_selector_slot(pindex) - + players[pindex].item_selector.index = + math.min(#players[pindex].item_cache, players[pindex].item_selector.index + 1) + read_item_selector_slot(pindex) elseif players[pindex].menu == "inventory" then - players[pindex].inventory.index = players[pindex].inventory.index +1 - if players[pindex].inventory.index%10 == 1 then + players[pindex].inventory.index = players[pindex].inventory.index + 1 + if players[pindex].inventory.index % 10 == 1 then if players[pindex].preferences.inventory_wraps_around == true then --Wrap around setting: Move and play move sound and read slot players[pindex].inventory.index = players[pindex].inventory.index - 10 - game.get_player(pindex).play_sound{path = "inventory-wrap-around"} + game.get_player(pindex).play_sound({ path = "inventory-wrap-around" }) read_inventory_slot(pindex) else --Border setting: Undo change and play "wall" sound - players[pindex].inventory.index = players[pindex].inventory.index -1 - game.get_player(pindex).play_sound{path = "inventory-edge"} + players[pindex].inventory.index = players[pindex].inventory.index - 1 + game.get_player(pindex).play_sound({ path = "inventory-edge" }) --printout("Border.", pindex) end else - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) read_inventory_slot(pindex) end elseif players[pindex].menu == "player_trash" then local trash_inv = game.get_player(pindex).get_inventory(defines.inventory.character_trash) - players[pindex].inventory.index = players[pindex].inventory.index +1 - if players[pindex].inventory.index%10 == 1 then + players[pindex].inventory.index = players[pindex].inventory.index + 1 + if players[pindex].inventory.index % 10 == 1 then if players[pindex].preferences.inventory_wraps_around == true then --Wrap around setting: Move and play move sound and read slot players[pindex].inventory.index = players[pindex].inventory.index - 10 - game.get_player(pindex).play_sound{path = "inventory-wrap-around"} + game.get_player(pindex).play_sound({ path = "inventory-wrap-around" }) read_inventory_slot(pindex, "", trash_inv) else --Border setting: Undo change and play "wall" sound - players[pindex].inventory.index = players[pindex].inventory.index -1 - game.get_player(pindex).play_sound{path = "inventory-edge"} + players[pindex].inventory.index = players[pindex].inventory.index - 1 + game.get_player(pindex).play_sound({ path = "inventory-edge" }) --printout("Border.", pindex) end else - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) read_inventory_slot(pindex, "", trash_inv) end elseif players[pindex].menu == "crafting" then - game.get_player(pindex).play_sound{path = "Inventory-Move"} - players[pindex].crafting.index = players[pindex].crafting.index +1 + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) + players[pindex].crafting.index = players[pindex].crafting.index + 1 if players[pindex].crafting.index > #players[pindex].crafting.lua_recipes[players[pindex].crafting.category] then players[pindex].crafting.index = 1 end fa_crafting.read_crafting_slot(pindex) - elseif players[pindex].menu == "crafting_queue" then - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) fa_crafting.load_crafting_queue(pindex) if players[pindex].crafting_queue.index >= players[pindex].crafting_queue.max then players[pindex].crafting_queue.index = 1 @@ -2746,36 +3019,42 @@ function menu_cursor_right(pindex) players[pindex].crafting_queue.index = players[pindex].crafting_queue.index + 1 end fa_crafting.read_crafting_queue(pindex) - elseif (players[pindex].menu == "building" or players[pindex].menu == "vehicle") then + elseif players[pindex].menu == "building" or players[pindex].menu == "vehicle" then --Move along a row in a building inventory if players[pindex].building.sector <= #players[pindex].building.sectors then --Most building sectors, e.g. chest inventories - if players[pindex].building.sectors[players[pindex].building.sector].inventory == nil or #players[pindex].building.sectors[players[pindex].building.sector].inventory < 1 then + if + players[pindex].building.sectors[players[pindex].building.sector].inventory == nil + or #players[pindex].building.sectors[players[pindex].building.sector].inventory < 1 + then printout("blank sector", pindex) return end - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) local row_length = players[pindex].preferences.building_inventory_row_length if #players[pindex].building.sectors[players[pindex].building.sector].inventory > row_length then players[pindex].building.index = players[pindex].building.index + 1 if players[pindex].building.index % row_length == 1 then --Wrap back around to the start of this row - game.get_player(pindex).play_sound{path = "inventory-wrap-around"} + game.get_player(pindex).play_sound({ path = "inventory-wrap-around" }) players[pindex].building.index = players[pindex].building.index - row_length end else players[pindex].building.index = players[pindex].building.index + 1 - if players[pindex].building.index > #players[pindex].building.sectors[players[pindex].building.sector].inventory then + if + players[pindex].building.index + > #players[pindex].building.sectors[players[pindex].building.sector].inventory + then --Wrap around to the start of the single-row inventory - game.get_player(pindex).play_sound{path = "inventory-wrap-around"} + game.get_player(pindex).play_sound({ path = "inventory-wrap-around" }) players[pindex].building.index = 1 end end - fa_sectors.read_sector_slot(pindex,false) + fa_sectors.read_sector_slot(pindex, false) elseif players[pindex].building.recipe_list == nil then - game.get_player(pindex).play_sound{path = "Inventory-Move"} - players[pindex].inventory.index = players[pindex].inventory.index +1 - if players[pindex].inventory.index%10 == 1 then + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) + players[pindex].inventory.index = players[pindex].inventory.index + 1 + if players[pindex].inventory.index % 10 == 1 then players[pindex].inventory.index = players[pindex].inventory.index - 10 end read_inventory_slot(pindex) @@ -2783,26 +3062,28 @@ function menu_cursor_right(pindex) if players[pindex].building.sector == #players[pindex].building.sectors + 1 then --Recipe selection if players[pindex].building.recipe_selection then - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) players[pindex].building.index = players[pindex].building.index + 1 - if players[pindex].building.index > #players[pindex].building.recipe_list[players[pindex].building.category] then - players[pindex].building.index = 1 + if + players[pindex].building.index + > #players[pindex].building.recipe_list[players[pindex].building.category] + then + players[pindex].building.index = 1 end end fa_sectors.read_building_recipe(pindex) else --Case = ??? - game.get_player(pindex).play_sound{path = "Inventory-Move"} - players[pindex].inventory.index = players[pindex].inventory.index +1 - if players[pindex].inventory.index%10 == 1 then + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) + players[pindex].inventory.index = players[pindex].inventory.index + 1 + if players[pindex].inventory.index % 10 == 1 then players[pindex].inventory.index = players[pindex].inventory.index - 10 end read_inventory_slot(pindex) - end end + end elseif players[pindex].menu == "technology" then - local techs = {} if players[pindex].technology.category == 1 then techs = players[pindex].technology.lua_researchable @@ -2812,17 +3093,15 @@ function menu_cursor_right(pindex) techs = players[pindex].technology.lua_unlocked end if players[pindex].technology.index < #techs then - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) players[pindex].technology.index = players[pindex].technology.index + 1 end read_technology_slot(pindex) - - elseif players[pindex].menu == "belt" then if players[pindex].belt.side == 1 then players[pindex].belt.side = 2 - game.get_player(pindex).play_sound{path = "Inventory-Move"} - if not pcall(function() + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) + if not pcall(function() fa_belts.read_belt_slot(pindex) end) then printout("Blank", pindex) @@ -2835,13 +3114,13 @@ function menu_cursor_right(pindex) elseif players[pindex].warnings.sector == 2 then warnings = players[pindex].warnings.medium.warnings elseif players[pindex].warnings.sector == 3 then - warnings= players[pindex].warnings.long.warnings + warnings = players[pindex].warnings.long.warnings end if warnings[players[pindex].warnings.category] ~= nil then local ents = warnings[players[pindex].warnings.category].ents if players[pindex].warnings.index < #ents then players[pindex].warnings.index = players[pindex].warnings.index + 1 - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) end end fa_warnings.read_warnings_slot(pindex) @@ -2856,18 +3135,16 @@ function menu_cursor_right(pindex) end --Schedules a function to be called after a certain number of ticks. -function schedule(ticks_in_the_future,func_to_call, data_to_pass_1, data_to_pass_2, data_to_pass_3) - if type(_G[func_to_call]) ~= "function" then - error(func_to_call .. " is not a function") - end - if ticks_in_the_future <=0 then +function schedule(ticks_in_the_future, func_to_call, data_to_pass_1, data_to_pass_2, data_to_pass_3) + if type(_G[func_to_call]) ~= "function" then error(func_to_call .. " is not a function") end + if ticks_in_the_future <= 0 then _G[func_to_call](data_to_pass_1, data_to_pass_2, data_to_pass_3) return end local tick = game.tick + ticks_in_the_future local schedule = global.scheduled_events schedule[tick] = schedule[tick] or {} - table.insert(schedule[tick], {func_to_call, data_to_pass_1, data_to_pass_2, data_to_pass_3}) + table.insert(schedule[tick], { func_to_call, data_to_pass_1, data_to_pass_2, data_to_pass_3 }) end --Handles a player joining into a game session. @@ -2876,43 +3153,39 @@ function on_player_join(pindex) schedule(3, "call_to_fix_zoom", pindex) schedule(4, "call_to_sync_graphics", pindex) fa_localising.check_player(pindex) - local playerList={} - for _ , p in pairs(game.connected_players) do - playerList["_" .. p.index]=p.name + local playerList = {} + for _, p in pairs(game.connected_players) do + playerList["_" .. p.index] = p.name end print("playerList " .. game.table_to_json(playerList)) if game.players[pindex].name == "Crimso" then - --Debug stuff + --Debug stuff local player = game.get_player(pindex).cutscene_character or game.get_player(pindex).character player.force.research_all_technologies() --game.write_file('map.txt', game.table_to_json(game.parse_map_exchange_string(">>>eNpjZGBksGUAgwZ7EOZgSc5PzIHxgNiBKzm/oCC1SDe/KBVZmDO5qDQlVTc/E1Vxal5qbqVuUmIxsmJ7jsyi/Dx0E1iLS/LzUEVKilJTi5E1cpcWJeZlluai62VgnPIl9HFDixwDCP+vZ1D4/x+EgawHQL+AMANjA0glIyNQDAZYk3My09IYGBQcGRgKnFev0rJjZGSsFlnn/rBqij0jRI2eA5TxASpyIAkm4glj+DnglFKBMUyQzDEGg89IDIilJUAroKo4HBAMiGQLSJKREeZ2xl91WXtKJlfYM3qs3zPr0/UqO6A0O0iCCU7MmgkCO2FeYYCZ+cAeKnXTnvHsGRB4Y8/ICtIhAiIcLIDEAW9mBkYBPiBrQQ+QUJBhgDnNDmaMiANjGhh8g/nkMYxx2R7dH8CAsAEZLgciToAIsIVwl0F95tDvwOggD5OVRCgB6jdiQHZDCsKHJ2HWHkayH80hmBGB7A80ERUHLNHABbIwBU68YIa7BhieF9hhPIf5DozMIAZI1RegGIQHkoEZBaEFHMDBzcyAAMC0cepk2C4A0ySfhQ==<<<"))) - player.insert{name="pipe", count=100} + player.insert({ name = "pipe", count = 100 }) for i = 0, 10 do for j = 0, 10 do - player.surface.create_entity{name = "iron-ore", position = {i + .5, j + .5}} + player.surface.create_entity({ name = "iron-ore", position = { i + 0.5, j + 0.5 } }) end end - -- player.force.research_all_technologies() + -- player.force.research_all_technologies() end - + --Reset the player building direction to match the vanilla behavior. - players[pindex].building_direction = dirs.north-- + players[pindex].building_direction = dirs.north -- end -script.on_event(defines.events.on_player_joined_game,function(event) - if game.is_multiplayer() then - on_player_join(event.player_index) - end +script.on_event(defines.events.on_player_joined_game, function(event) + if game.is_multiplayer() then on_player_join(event.player_index) end end) function on_initial_joining_tick(event) - if not game.is_multiplayer() then - on_player_join(game.connected_players[1].index) - end + if not game.is_multiplayer() then on_player_join(game.connected_players[1].index) end on_tick(event) - script.on_event(defines.events.on_tick,on_tick) + script.on_event(defines.events.on_tick, on_tick) end --Called every tick. Used to call scheduled and repeated functions. @@ -2929,8 +3202,8 @@ function on_tick(event) if event.tick % 15 == 0 then for pindex, player in pairs(players) do --Bump checks - check_and_play_bump_alert_sound(pindex,event.tick) - check_and_play_stuck_alert_sound(pindex,event.tick) + check_and_play_bump_alert_sound(pindex, event.tick) + check_and_play_stuck_alert_sound(pindex, event.tick) end elseif event.tick % 15 == 1 then --Check and play train track warning sounds at appropriate frequencies @@ -2969,8 +3242,8 @@ function on_tick(event) elseif event.tick % 30 == 6 then --Check and play train horns for pindex, player in pairs(players) do - fa_trains.check_and_honk_at_trains_in_same_block(event.tick,pindex) - fa_trains.check_and_honk_at_closed_signal(event.tick,pindex) + fa_trains.check_and_honk_at_trains_in_same_block(event.tick, pindex) + fa_trains.check_and_honk_at_closed_signal(event.tick, pindex) fa_trains.check_and_play_sound_for_turning_trains(pindex) end elseif event.tick % 30 == 7 then @@ -2987,14 +3260,12 @@ function on_tick(event) for pindex, player in pairs(players) do --If within 50 tiles of an enemy, try to aim at enemies and play sound to notify of enemies within shooting range local p = game.get_player(pindex) - local enemy = p.surface.find_nearest_enemy{position = p.position, max_distance = 50, force = p.force} - if enemy ~= nil and enemy.valid then - fa_combat.aim_gun_at_nearest_enemy(pindex,enemy) - end + local enemy = p.surface.find_nearest_enemy({ position = p.position, max_distance = 50, force = p.force }) + if enemy ~= nil and enemy.valid then fa_combat.aim_gun_at_nearest_enemy(pindex, enemy) end --If crafting, play a sound if p.character and p.crafting_queue ~= nil and #p.crafting_queue > 0 and p.crafting_queue_size > 0 then - p.play_sound{path = "player-crafting", volume_modifier = 0.5} + p.play_sound({ path = "player-crafting", volume_modifier = 0.5 }) end end elseif event.tick % 90 == 13 then @@ -3010,13 +3281,13 @@ function on_tick(event) elseif players[pindex].tutorial == nil then printout("Press 'H' to open the tutorial", pindex) elseif game.get_player(pindex).ticks_to_respawn ~= nil then - printout(math.floor(game.get_player(pindex).ticks_to_respawn/60) .. " seconds until respawn", pindex) + printout(math.floor(game.get_player(pindex).ticks_to_respawn / 60) .. " seconds until respawn", pindex) end end end end -script.on_event(defines.events.on_tick,on_initial_joining_tick) +script.on_event(defines.events.on_tick, on_initial_joining_tick) --Called for every player on every tick, to manage automatic walking and enforcing mouse pointer position syncs. Todo: move the mouse pointer stuff to its own function. function move_characters(event) @@ -3024,30 +3295,35 @@ function move_characters(event) if player.vanilla_mode == true then player.player.game_view_settings.update_entity_selection = true elseif player.player.game_view_settings.update_entity_selection == false then - --Force the mouse pointer to the mod cursor if there is an item in hand + --Force the mouse pointer to the mod cursor if there is an item in hand --(so that the game does not make a mess when you left click while the cursor is actually locked) local stack = game.get_player(pindex).cursor_stack if players[pindex].in_menu == false and stack and stack.valid_for_read then - if stack.prototype.place_result ~= nil or stack.prototype.place_as_tile_result ~= nil or stack.is_blueprint or stack.is_deconstruction_item or stack.is_upgrade_item then + if + stack.prototype.place_result ~= nil + or stack.prototype.place_as_tile_result ~= nil + or stack.is_blueprint + or stack.is_deconstruction_item + or stack.is_upgrade_item + then --Force the pointer to the build preview location fa_graphics.sync_build_cursor_graphics(pindex) else --Force the pointer to the cursor location (if on screen) if fa_mouse.cursor_position_is_on_screen_with_player_centered(pindex) then - fa_mouse.move_mouse_pointer(players[pindex].cursor_pos,pindex) + fa_mouse.move_mouse_pointer(players[pindex].cursor_pos, pindex) else - fa_mouse.move_mouse_pointer(players[pindex].position,pindex) + fa_mouse.move_mouse_pointer(players[pindex].position, pindex) end end end - end if player.walk ~= 2 or player.cursor or player.in_menu then local walk = false while #player.move_queue > 0 do local next_move = player.move_queue[1] - player.player.walking_state = {walking = true, direction = next_move.direction} + player.player.walking_state = { walking = true, direction = next_move.direction } if next_move.direction == defines.direction.north then walk = player.player.position.y > next_move.dest.y elseif next_move.direction == defines.direction.south then @@ -3061,70 +3337,64 @@ function move_characters(event) if walk then break else - table.remove(player.move_queue,1) + table.remove(player.move_queue, 1) end end if not walk and players[pindex].kruise_kontrolling ~= true then - player.player.walking_state = {walking = true, direction= player.player_direction} - player.player.walking_state = {walking = false} + player.player.walking_state = { walking = true, direction = player.player_direction } + player.player.walking_state = { walking = false } end end end end --Move player character (and adapt the cursor to smooth walking) -function move(direction,pindex) +function move(direction, pindex) local p = game.get_player(pindex) - if p.driving then - return - end + if p.driving then return end local first_player = game.get_player(pindex) local pos = players[pindex].position - local new_pos = fa_utils.offset_position(pos,direction,1) - + local new_pos = fa_utils.offset_position(pos, direction, 1) + --Compare the input direction and facing direction if players[pindex].player_direction == direction then --Same direction: Move character: - if players[pindex].walk == 2 then - return - end + if players[pindex].walk == 2 then return end new_pos = fa_utils.center_of_tile(new_pos) - can_port = first_player.surface.can_place_entity{name = "character", position = new_pos} + can_port = first_player.surface.can_place_entity({ name = "character", position = new_pos }) if can_port then if players[pindex].walk == 1 then - table.insert(players[pindex].move_queue,{direction=direction,dest=new_pos}) + table.insert(players[pindex].move_queue, { direction = direction, dest = new_pos }) else teleported = first_player.teleport(new_pos) - if not teleported then - printout("Teleport Failed", pindex) - end + if not teleported then printout("Teleport Failed", pindex) end end players[pindex].position = new_pos - players[pindex].cursor_pos = fa_utils.offset_position(players[pindex].position, direction,1) + players[pindex].cursor_pos = fa_utils.offset_position(players[pindex].position, direction, 1) --Telestep walking sounds: todo fix bug here (?) about walking sounds from inside menus - if players[pindex].tile.previous ~= nil and players[pindex].tile.previous.valid and players[pindex].tile.previous.type == "transport-belt" then - game.get_player(pindex).play_sound{path = "utility/metal_walking_sound", volume_modifier = 1} + if + players[pindex].tile.previous ~= nil + and players[pindex].tile.previous.valid + and players[pindex].tile.previous.type == "transport-belt" + then + game.get_player(pindex).play_sound({ path = "utility/metal_walking_sound", volume_modifier = 1 }) else - local tile = game.get_player(pindex).surface .get_tile(new_pos.x, new_pos.y) + local tile = game.get_player(pindex).surface.get_tile(new_pos.x, new_pos.y) local sound_path = "tile-walking/" .. tile.name if game.is_valid_sound_path(sound_path) then - game.get_player(pindex).play_sound{path = "tile-walking/" .. tile.name, volume_modifier = 1} + game.get_player(pindex).play_sound({ path = "tile-walking/" .. tile.name, volume_modifier = 1 }) else - game.get_player(pindex).play_sound{path = "player-walk", volume_modifier = 1} + game.get_player(pindex).play_sound({ path = "player-walk", volume_modifier = 1 }) end end - if not game.get_player(pindex).driving then - read_tile(pindex) - end + if not game.get_player(pindex).driving then read_tile(pindex) end local stack = first_player.cursor_stack if stack and stack.valid_for_read and stack.valid and stack.prototype.place_result ~= nil then fa_graphics.sync_build_cursor_graphics(pindex) end - if players[pindex].build_lock then - fa_building_tools.build_item_in_hand(pindex) - end + if players[pindex].build_lock then fa_building_tools.build_item_in_hand(pindex) end else printout("Tile Occupied", pindex) end @@ -3132,10 +3402,10 @@ function move(direction,pindex) --New direction: Turn character: --turn if players[pindex].walk == 0 then new_pos = fa_utils.center_of_tile(new_pos) - game.get_player(pindex).play_sound{path = "player-turned"} + game.get_player(pindex).play_sound({ path = "player-turned" }) elseif players[pindex].walk == 1 then new_pos = fa_utils.center_of_tile(new_pos) - table.insert(players[pindex].move_queue,{direction=direction,dest=pos}) + table.insert(players[pindex].move_queue, { direction = direction, dest = pos }) end players[pindex].player_direction = direction players[pindex].cursor_pos = new_pos @@ -3155,7 +3425,15 @@ function move(direction,pindex) elseif players[pindex].walk == 2 then refresh_player_tile(pindex) local ent = get_selected_ent(pindex) - if not players[pindex].vanilla_mode and ((ent ~= nil and ent.valid) or not game.get_player(pindex).surface.can_place_entity{name = "character", position = players[pindex].cursor_pos}) then + if + not players[pindex].vanilla_mode + and ( + (ent ~= nil and ent.valid) + or not game + .get_player(pindex).surface + .can_place_entity({ name = "character", position = players[pindex].cursor_pos }) + ) + then target(pindex) read_tile(pindex) end @@ -3163,7 +3441,13 @@ function move(direction,pindex) --Rotate belts in hand for build lock Mode local stack = game.get_player(pindex).cursor_stack - if players[pindex].build_lock and stack.valid_for_read and stack.valid and stack.prototype.place_result ~= nil and stack.prototype.place_result.type == "transport-belt" then + if + players[pindex].build_lock + and stack.valid_for_read + and stack.valid + and stack.prototype.place_result ~= nil + and stack.prototype.place_result.type == "transport-belt" + then players[pindex].building_direction = players[pindex].player_direction end end @@ -3176,7 +3460,7 @@ function move(direction,pindex) fa_graphics.draw_cursor_highlight(pindex, nil, nil) end - --Unless the cut-paste tool is in hand, restore the reading of flying text + --Unless the cut-paste tool is in hand, restore the reading of flying text local stack = game.get_player(pindex).cursor_stack if not (stack and stack.valid_for_read and stack.name == "cut-paste-tool") then players[pindex].allow_reading_flying_text = true @@ -3184,11 +3468,9 @@ function move(direction,pindex) end --Chooses the function to call after a movement keypress, according to the current mode. -function move_key(direction,event, force_single_tile) +function move_key(direction, event, force_single_tile) local pindex = event.player_index - if not check_for_player(pindex) or players[pindex].menu == "prompt" then - return - end + if not check_for_player(pindex) or players[pindex].menu == "prompt" then return end --Stop any enabled mouse entity selection if players[pindex].vanilla_mode ~= true then game.get_player(pindex).game_view_settings.update_entity_selection = false @@ -3202,23 +3484,21 @@ function move_key(direction,event, force_single_tile) if players[pindex].in_menu and players[pindex].menu ~= "prompt" then -- Menus: move menu cursor - menu_cursor_move(direction,pindex) + menu_cursor_move(direction, pindex) elseif players[pindex].cursor then - -- Cursor mode: Move cursor on map + -- Cursor mode: Move cursor on map cursor_mode_move(direction, pindex, force_single_tile) else -- General case: Move character - move(direction,pindex) + move(direction, pindex) end --Play a sound to indicate ongoing selection - if pex.bp_selecting then - game.get_player(pindex).play_sound{path = "utility/upgrade_selection_started"} - end + if pex.bp_selecting then game.get_player(pindex).play_sound({ path = "utility/upgrade_selection_started" }) end --Play a sound to indicate ongoing ghost rail planner if pex.ghost_rail_planning then - game.get_player(pindex).play_sound{path = "utility/upgrade_selection_started"} + game.get_player(pindex).play_sound({ path = "utility/upgrade_selection_started" }) end --Stop kruise kontrol related permissions @@ -3228,9 +3508,7 @@ end --Moves the cursor, and conducts an area scan for larger cursors. If the player is in a slow moving vehicle, it is stopped. function cursor_mode_move(direction, pindex, single_only) local diff = players[pindex].cursor_size * 2 + 1 - if single_only then - diff = 1 - end + if single_only then diff = 1 end local p = game.get_player(pindex) if p.driving and p.vehicle and (p.vehicle.type == "car" or p.vehicle.type == "locomotive") then @@ -3240,7 +3518,8 @@ function cursor_mode_move(direction, pindex, single_only) end end - players[pindex].cursor_pos = fa_utils.center_of_tile(fa_utils.offset_position(players[pindex].cursor_pos, direction, diff)) + players[pindex].cursor_pos = + fa_utils.center_of_tile(fa_utils.offset_position(players[pindex].cursor_pos, direction, diff)) if players[pindex].cursor_size == 0 then -- Cursor size 0 ("1 by 1"): Read tile @@ -3248,14 +3527,17 @@ function cursor_mode_move(direction, pindex, single_only) --Update drawn cursor local stack = p.cursor_stack - if stack and stack.valid_for_read and stack.valid and (stack.prototype.place_result ~= nil or stack.is_blueprint) then + if + stack + and stack.valid_for_read + and stack.valid + and (stack.prototype.place_result ~= nil or stack.is_blueprint) + then fa_graphics.sync_build_cursor_graphics(pindex) end --Apply build lock if active - if players[pindex].build_lock then - fa_building_tools.build_item_in_hand(pindex) - end + if players[pindex].build_lock then fa_building_tools.build_item_in_hand(pindex) end --Update cursor highlight local ent = get_selected_ent(pindex) @@ -3266,15 +3548,27 @@ function cursor_mode_move(direction, pindex, single_only) end else -- Larger cursor sizes: scan area - local scan_left_top = {math.floor(players[pindex].cursor_pos.x)-players[pindex].cursor_size,math.floor(players[pindex].cursor_pos.y)-players[pindex].cursor_size} - local scan_right_bottom = {math.floor(players[pindex].cursor_pos.x)+players[pindex].cursor_size+1,math.floor(players[pindex].cursor_pos.y)+players[pindex].cursor_size+1} + local scan_left_top = { + math.floor(players[pindex].cursor_pos.x) - players[pindex].cursor_size, + math.floor(players[pindex].cursor_pos.y) - players[pindex].cursor_size, + } + local scan_right_bottom = { + math.floor(players[pindex].cursor_pos.x) + players[pindex].cursor_size + 1, + math.floor(players[pindex].cursor_pos.y) + players[pindex].cursor_size + 1, + } players[pindex].nearby.index = 1 - players[pindex].nearby.ents = fa_scanner.scan_area(math.floor(players[pindex].cursor_pos.x)-players[pindex].cursor_size, math.floor(players[pindex].cursor_pos.y)-players[pindex].cursor_size, players[pindex].cursor_size * 2 + 1, players[pindex].cursor_size * 2 + 1, pindex) + players[pindex].nearby.ents = fa_scanner.scan_area( + math.floor(players[pindex].cursor_pos.x) - players[pindex].cursor_size, + math.floor(players[pindex].cursor_pos.y) - players[pindex].cursor_size, + players[pindex].cursor_size * 2 + 1, + players[pindex].cursor_size * 2 + 1, + pindex + ) fa_scanner.populate_list_categories(pindex) players[pindex].cursor_scan_center = players[pindex].cursor_pos local scan_summary = fa_scanner.area_scan_summary_info(scan_left_top, scan_right_bottom, pindex) - fa_graphics.draw_large_cursor(scan_left_top,scan_right_bottom,pindex) - printout(scan_summary,pindex) + fa_graphics.draw_large_cursor(scan_left_top, scan_right_bottom, pindex) + printout(scan_summary, pindex) end --Update player direction to face the cursor (after the vanilla move event that turns the character too, and only ends when the movement key is released) @@ -3282,11 +3576,10 @@ function cursor_mode_move(direction, pindex, single_only) --Play Sound if players[pindex].remote_view then - p.play_sound{path = "Close-Inventory-Sound", position = players[pindex].cursor_pos, volume_modifier = 0.75} + p.play_sound({ path = "Close-Inventory-Sound", position = players[pindex].cursor_pos, volume_modifier = 0.75 }) else - p.play_sound{path = "Close-Inventory-Sound", position = players[pindex].position, volume_modifier = 0.75} + p.play_sound({ path = "Close-Inventory-Sound", position = players[pindex].position, volume_modifier = 0.75 }) end - end --Focuses camera on the cursor position. @@ -3299,9 +3592,7 @@ end --Makes the character face the cursor, choosing the nearest of 4 cardinal directions. Can be overwriten by vanilla move keys. function turn_to_cursor_direction_cardinal(pindex) local p = game.get_player(pindex) - if p.character == nil then - return - end + if p.character == nil then return end local pex = players[pindex] local dir = fa_utils.get_direction_precise(pex.cursor_pos, p.position) if dir == dirs.northwest or dir == dirs.north or dir == dirs.northeast then @@ -3321,9 +3612,7 @@ end --Makes the character face the cursor, choosing the nearest of 8 directions. Can be overwriten by vanilla move keys. function turn_to_cursor_direction_precise(pindex) local p = game.get_player(pindex) - if p.character == nil then - return - end + if p.character == nil then return end local pex = players[pindex] local dir = fa_utils.get_direction_precise(pex.cursor_pos, p.position) pex.player_direction = dir @@ -3334,32 +3623,26 @@ end --Called when a player enters or exits a vehicle script.on_event(defines.events.on_player_driving_changed_state, function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end reset_bump_stats(pindex) game.get_player(pindex).clear_cursor() players[pindex].last_train_orientation = nil if game.get_player(pindex).driving then players[pindex].last_vehicle = game.get_player(pindex).vehicle - printout("Entered " .. game.get_player(pindex).vehicle.name ,pindex) + printout("Entered " .. game.get_player(pindex).vehicle.name, pindex) if players[pindex].last_vehicle.train ~= nil and players[pindex].last_vehicle.train.schedule == nil then players[pindex].last_vehicle.train.manual_mode = true end elseif players[pindex].last_vehicle ~= nil then - printout("Exited " .. players[pindex].last_vehicle.name ,pindex) + printout("Exited " .. players[pindex].last_vehicle.name, pindex) if players[pindex].last_vehicle.train ~= nil and players[pindex].last_vehicle.train.schedule == nil then players[pindex].last_vehicle.train.manual_mode = true end fa_teleport.teleport_to_closest(pindex, players[pindex].last_vehicle.position, true, true) - if players[pindex].menu == "train_menu" then - fa_trains.menu_close(pindex, false) - end - if players[pindex].menu == "spider_menu" then - fa_spidertrons.spider_menu_close(pindex, false) - end + if players[pindex].menu == "train_menu" then fa_trains.menu_close(pindex, false) end + if players[pindex].menu == "spider_menu" then fa_spidertrons.spider_menu_close(pindex, false) end else - printout("Driving state changed." ,pindex) + printout("Driving state changed.", pindex) end end) @@ -3367,7 +3650,7 @@ end) script.on_event("pause-game-fa", function(event) local pindex = event.player_index game.get_player(pindex).close_map() - game.get_player(pindex).play_sound{path = "Close-Inventory-Sound"} + game.get_player(pindex).play_sound({ path = "Close-Inventory-Sound" }) if players[pindex].remote_view == true then players[pindex].remote_view = false printout("Remote view closed", pindex) @@ -3381,7 +3664,7 @@ script.on_event("pause-game-fa", function(event) if game.get_player(pindex).opened ~= nil then printout("Menu closed", pindex) else - --printout("Game resumed", pindex)--This is always incorrect cos this event fires before the pause happens. + --printout("Game resumed", pindex)--This is always incorrect cos this event fires before the pause happens. end end end @@ -3396,40 +3679,36 @@ script.on_event("pause-game-fa", function(event) end) script.on_event("cursor-up", function(event) - move_key(defines.direction.north,event) + move_key(defines.direction.north, event) end) script.on_event("cursor-down", function(event) - move_key(defines.direction.south,event) + move_key(defines.direction.south, event) end) script.on_event("cursor-left", function(event) - move_key(defines.direction.west,event) + move_key(defines.direction.west, event) end) script.on_event("cursor-right", function(event) - move_key(defines.direction.east,event) + move_key(defines.direction.east, event) end) --Read coordinates of the cursor. Extra info as well such as entity part if an entity is selected, and heading and speed info for vehicles. script.on_event("read-cursor-coords", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end read_coords(pindex) -end -) +end) --Get distance and direction of cursor from player. script.on_event("read-cursor-distance-and-direction", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end if players[pindex].menu == "crafting" then --Read recipe ingredients / products (crafting menu) - local recipe = players[pindex].crafting.lua_recipes[players[pindex].crafting.category][players[pindex].crafting.index] + local recipe = + players[pindex].crafting.lua_recipes[players[pindex].crafting.category][players[pindex].crafting.index] local result = fa_crafting.recipe_raw_ingredients_info(recipe, pindex) --game.get_player(pindex).print(recipe.name)--** --game.get_player(pindex).print(result)--** @@ -3440,53 +3719,65 @@ script.on_event("read-cursor-distance-and-direction", function(event) local cursor_location_description = "At" local cursor_production = " " local cursor_description_of = " " - local result={"access.thing-producing-listpos-dirdist",cursor_location_description} - table.insert(result,cursor_production)--no production - table.insert(result,cursor_description_of)--listpos - table.insert(result,dir_dist) - printout(result,pindex) - game.get_player(pindex).print(result,{volume_modifier=0}) - rendering.draw_circle{color = {1, 0.2, 0}, radius = 0.1, width = 5, target = players[pindex].cursor_pos, surface = game.get_player(pindex).surface, time_to_live = 180} + local result = { "access.thing-producing-listpos-dirdist", cursor_location_description } + table.insert(result, cursor_production) --no production + table.insert(result, cursor_description_of) --listpos + table.insert(result, dir_dist) + printout(result, pindex) + game.get_player(pindex).print(result, { volume_modifier = 0 }) + rendering.draw_circle({ + color = { 1, 0.2, 0 }, + radius = 0.1, + width = 5, + target = players[pindex].cursor_pos, + surface = game.get_player(pindex).surface, + time_to_live = 180, + }) end end) --Get distance and direction of cursor from player as a vector with a horizontal component and vertical component. script.on_event("read-cursor-distance-vector", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end if players[pindex].menu ~= "crafting" then local c_pos = players[pindex].cursor_pos local p_pos = players[pindex].position local diff_x = math.floor(c_pos.x - p_pos.x) local diff_y = math.floor(c_pos.y - p_pos.y) local dir_x = dirs.east - if diff_x < 0 then - dir_x = dirs.west - end + if diff_x < 0 then dir_x = dirs.west end local dir_y = dirs.south - if diff_y < 0 then - dir_y = dirs.north - end - local result = "At " .. math.abs(diff_x) .. " " .. fa_utils.direction_lookup(dir_x) .. " and " .. math.abs(diff_y) .. " " .. fa_utils.direction_lookup(dir_y) - printout(result,pindex) - game.get_player(pindex).print(result,{volume_modifier=0}) - rendering.draw_circle{color = {1, 0.2, 0}, radius = 0.1, width = 5, target = players[pindex].cursor_pos, surface = game.get_player(pindex).surface, time_to_live = 180} + if diff_y < 0 then dir_y = dirs.north end + local result = "At " + .. math.abs(diff_x) + .. " " + .. fa_utils.direction_lookup(dir_x) + .. " and " + .. math.abs(diff_y) + .. " " + .. fa_utils.direction_lookup(dir_y) + printout(result, pindex) + game.get_player(pindex).print(result, { volume_modifier = 0 }) + rendering.draw_circle({ + color = { 1, 0.2, 0 }, + radius = 0.1, + width = 5, + target = players[pindex].cursor_pos, + surface = game.get_player(pindex).surface, + time_to_live = 180, + }) end end) script.on_event("read-character-coords", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local pos = game.get_player(pindex).position local result = "Character at " .. math.floor(pos.x) .. ", " .. math.floor(pos.y) - printout(result,pindex) - game.get_player(pindex).print(result, {volume_modifier = 0}) -end -) + printout(result, pindex) + game.get_player(pindex).print(result, { volume_modifier = 0 }) +end) --Returns the cursor to the player position. script.on_event("return-cursor-to-player", function(event) @@ -3496,63 +3787,49 @@ end) script.on_event("cursor-bookmark-save", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local pos = players[pindex].cursor_pos players[pindex].cursor_bookmark = pos - printout("Saved cursor bookmark at " .. math.floor(pos.x) .. ", " .. math.floor(pos.y) ,pindex) - game.get_player(pindex).play_sound{path = "Close-Inventory-Sound"} + printout("Saved cursor bookmark at " .. math.floor(pos.x) .. ", " .. math.floor(pos.y), pindex) + game.get_player(pindex).play_sound({ path = "Close-Inventory-Sound" }) end) script.on_event("cursor-bookmark-load", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local pos = players[pindex].cursor_bookmark - if pos == nil or pos.x == nil or pos.y == nil then - return - end + if pos == nil or pos.x == nil or pos.y == nil then return end players[pindex].cursor_pos = pos fa_graphics.draw_cursor_highlight(pindex, nil, nil) fa_graphics.sync_build_cursor_graphics(pindex) - printout("Loaded cursor bookmark at " .. math.floor(pos.x) .. ", " .. math.floor(pos.y) ,pindex) - game.get_player(pindex).play_sound{path = "Close-Inventory-Sound"} + printout("Loaded cursor bookmark at " .. math.floor(pos.x) .. ", " .. math.floor(pos.y), pindex) + game.get_player(pindex).play_sound({ path = "Close-Inventory-Sound" }) end) script.on_event("type-cursor-target", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end type_cursor_position(pindex) end) script.on_event("teleport-to-cursor", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end fa_teleport.teleport_to_cursor(pindex, false, false, false) end) script.on_event("teleport-to-cursor-forced", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end fa_teleport.teleport_to_cursor(pindex, false, true, false) end) script.on_event("teleport-to-alert-forced", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local alert_pos = players[pindex].last_damage_alert_pos if alert_pos == nil then - printout("No target",pindex) + printout("No target", pindex) return end players[pindex].cursor_pos = alert_pos @@ -3567,10 +3844,8 @@ end) script.on_event("toggle-cursor", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - if not (players[pindex].in_menu) then + if not check_for_player(pindex) then return end + if not players[pindex].in_menu then players[pindex].move_queue = {} toggle_cursor_mode(pindex) end @@ -3578,10 +3853,8 @@ end) script.on_event("toggle-remote-view", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - if not (players[pindex].in_menu) then + if not check_for_player(pindex) then return end + if not players[pindex].in_menu then players[pindex].move_queue = {} toggle_remote_view(pindex) end @@ -3591,10 +3864,8 @@ end) --We have cursor sizes 1,3,5,11,21,51,101,251 script.on_event("cursor-size-increment", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - if not (players[pindex].in_menu) then + if not check_for_player(pindex) then return end + if not players[pindex].in_menu then if players[pindex].cursor_size == 0 then players[pindex].cursor_size = 1 elseif players[pindex].cursor_size == 1 then @@ -3613,22 +3884,26 @@ script.on_event("cursor-size-increment", function(event) local say_size = players[pindex].cursor_size * 2 + 1 printout("Cursor size " .. say_size .. " by " .. say_size, pindex) - local scan_left_top = {math.floor(players[pindex].cursor_pos.x)-players[pindex].cursor_size,math.floor(players[pindex].cursor_pos.y)-players[pindex].cursor_size} - local scan_right_bottom = {math.floor(players[pindex].cursor_pos.x)+players[pindex].cursor_size+1,math.floor(players[pindex].cursor_pos.y)+players[pindex].cursor_size+1} - fa_graphics.draw_large_cursor(scan_left_top,scan_right_bottom,pindex) + local scan_left_top = { + math.floor(players[pindex].cursor_pos.x) - players[pindex].cursor_size, + math.floor(players[pindex].cursor_pos.y) - players[pindex].cursor_size, + } + local scan_right_bottom = { + math.floor(players[pindex].cursor_pos.x) + players[pindex].cursor_size + 1, + math.floor(players[pindex].cursor_pos.y) + players[pindex].cursor_size + 1, + } + fa_graphics.draw_large_cursor(scan_left_top, scan_right_bottom, pindex) end --Play Sound - game.get_player(pindex).play_sound{path = "Close-Inventory-Sound", volume_modifier = 0.75} + game.get_player(pindex).play_sound({ path = "Close-Inventory-Sound", volume_modifier = 0.75 }) end) --We have cursor sizes 1,3,5,11,21,51,101,251 script.on_event("cursor-size-decrement", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - if not (players[pindex].in_menu) then + if not check_for_player(pindex) then return end + if not players[pindex].in_menu then if players[pindex].cursor_size == 1 then players[pindex].cursor_size = 0 elseif players[pindex].cursor_size == 2 then @@ -3647,149 +3922,133 @@ script.on_event("cursor-size-decrement", function(event) local say_size = players[pindex].cursor_size * 2 + 1 printout("Cursor size " .. say_size .. " by " .. say_size, pindex) - local scan_left_top = {math.floor(players[pindex].cursor_pos.x)-players[pindex].cursor_size,math.floor(players[pindex].cursor_pos.y)-players[pindex].cursor_size} - local scan_right_bottom = {math.floor(players[pindex].cursor_pos.x)+players[pindex].cursor_size+1,math.floor(players[pindex].cursor_pos.y)+players[pindex].cursor_size+1} - fa_graphics.draw_large_cursor(scan_left_top,scan_right_bottom,pindex) + local scan_left_top = { + math.floor(players[pindex].cursor_pos.x) - players[pindex].cursor_size, + math.floor(players[pindex].cursor_pos.y) - players[pindex].cursor_size, + } + local scan_right_bottom = { + math.floor(players[pindex].cursor_pos.x) + players[pindex].cursor_size + 1, + math.floor(players[pindex].cursor_pos.y) + players[pindex].cursor_size + 1, + } + fa_graphics.draw_large_cursor(scan_left_top, scan_right_bottom, pindex) end --Play Sound - game.get_player(pindex).play_sound{path = "Close-Inventory-Sound", volume_modifier = 0.75} + game.get_player(pindex).play_sound({ path = "Close-Inventory-Sound", volume_modifier = 0.75 }) end) script.on_event("increase-inventory-bar-by-1", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end if players[pindex].in_menu and (players[pindex].menu == "building" or players[pindex].menu == "vehicle") then --Chest bar setting: Increase - local ent = get_selected_ent(pindex) - local result = fa_sectors.add_to_inventory_bar(ent, 1) - printout(result, pindex) + local ent = get_selected_ent(pindex) + local result = fa_sectors.add_to_inventory_bar(ent, 1) + printout(result, pindex) end end) script.on_event("increase-inventory-bar-by-5", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end if players[pindex].in_menu and (players[pindex].menu == "building" or players[pindex].menu == "vehicle") then --Chest bar setting: Increase - local ent = get_selected_ent(pindex) - local result = fa_sectors.add_to_inventory_bar(ent, 5) - printout(result, pindex) + local ent = get_selected_ent(pindex) + local result = fa_sectors.add_to_inventory_bar(ent, 5) + printout(result, pindex) end end) script.on_event("increase-inventory-bar-by-100", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end if players[pindex].in_menu and (players[pindex].menu == "building" or players[pindex].menu == "vehicle") then --Chest bar setting: Increase - local ent = get_selected_ent(pindex) - local result = fa_sectors.add_to_inventory_bar(ent, 100) - printout(result, pindex) + local ent = get_selected_ent(pindex) + local result = fa_sectors.add_to_inventory_bar(ent, 100) + printout(result, pindex) end end) script.on_event("decrease-inventory-bar-by-1", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end if players[pindex].in_menu and (players[pindex].menu == "building" or players[pindex].menu == "vehicle") then --Chest bar setting: Decrease - local ent = get_selected_ent(pindex) - local result = fa_sectors.add_to_inventory_bar(ent, -1) - printout(result, pindex) + local ent = get_selected_ent(pindex) + local result = fa_sectors.add_to_inventory_bar(ent, -1) + printout(result, pindex) end end) script.on_event("decrease-inventory-bar-by-5", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end if players[pindex].in_menu and (players[pindex].menu == "building" or players[pindex].menu == "vehicle") then --Chest bar setting: Decrease - local ent = get_selected_ent(pindex) - local result = fa_sectors.add_to_inventory_bar(ent, -5) - printout(result, pindex) + local ent = get_selected_ent(pindex) + local result = fa_sectors.add_to_inventory_bar(ent, -5) + printout(result, pindex) end end) script.on_event("decrease-inventory-bar-by-100", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end if players[pindex].in_menu and (players[pindex].menu == "building" or players[pindex].menu == "vehicle") then --Chest bar setting: Decrease - local ent = get_selected_ent(pindex) - local result = fa_sectors.add_to_inventory_bar(ent, -100) - printout(result, pindex) + local ent = get_selected_ent(pindex) + local result = fa_sectors.add_to_inventory_bar(ent, -100) + printout(result, pindex) end end) script.on_event("increase-train-wait-times-by-5", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end if players[pindex].in_menu and players[pindex].menu == "train_menu" then - fa_trains.change_instant_schedule_wait_time(5,pindex) + fa_trains.change_instant_schedule_wait_time(5, pindex) elseif players[pindex].in_menu and players[pindex].menu == "train_stop_menu" then - fa_train_stops.nearby_train_schedule_add_to_wait_time(5,pindex) + fa_train_stops.nearby_train_schedule_add_to_wait_time(5, pindex) end end) script.on_event("increase-train-wait-times-by-60", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end if players[pindex].in_menu and players[pindex].menu == "train_menu" then - fa_trains.change_instant_schedule_wait_time(60,pindex) + fa_trains.change_instant_schedule_wait_time(60, pindex) elseif players[pindex].in_menu and players[pindex].menu == "train_stop_menu" then - fa_train_stops.nearby_train_schedule_add_to_wait_time(60,pindex) + fa_train_stops.nearby_train_schedule_add_to_wait_time(60, pindex) end end) script.on_event("decrease-train-wait-times-by-5", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end if players[pindex].in_menu and players[pindex].menu == "train_menu" then - fa_trains.change_instant_schedule_wait_time(-5,pindex) + fa_trains.change_instant_schedule_wait_time(-5, pindex) elseif players[pindex].in_menu and players[pindex].menu == "train_stop_menu" then - fa_train_stops.nearby_train_schedule_add_to_wait_time(-5,pindex) + fa_train_stops.nearby_train_schedule_add_to_wait_time(-5, pindex) end end) script.on_event("decrease-train-wait-times-by-60", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end if players[pindex].in_menu and players[pindex].menu == "train_menu" then - fa_trains.change_instant_schedule_wait_time(-60,pindex) + fa_trains.change_instant_schedule_wait_time(-60, pindex) elseif players[pindex].in_menu and players[pindex].menu == "train_stop_menu" then - fa_train_stops.nearby_train_schedule_add_to_wait_time(-60,pindex) + fa_train_stops.nearby_train_schedule_add_to_wait_time(-60, pindex) end end) script.on_event("read-rail-structure-ahead", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local ent = get_selected_ent(pindex) if game.get_player(pindex).driving and game.get_player(pindex).vehicle.train ~= nil then - fa_trains.train_read_next_rail_entity_ahead(pindex,false) + fa_trains.train_read_next_rail_entity_ahead(pindex, false) elseif ent ~= nil and ent.valid and (ent.name == "straight-rail" or ent.name == "curved-rail") then --Report what is along the rail fa_rails.rail_read_next_rail_entity_ahead(pindex, ent, true) @@ -3798,21 +4057,21 @@ end) script.on_event("read-driving-structure-ahead", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local p = game.get_player(pindex) if p.driving and (p.vehicle.train ~= nil or p.vehicle.type == "car") then local ent = players[pindex].last_driving_alert_ent if ent and ent.valid then local dir = fa_utils.get_heading_value(p.vehicle) - local dir_ent = fa_utils.get_direction_biased(ent.position,p.vehicle.position) - if p.vehicle.speed >= 0 and (dir_ent == dir or math.abs(dir_ent - dir) == 1 or math.abs(dir_ent - dir) == 7) then - local dist = math.floor(util.distance(p.vehicle.position,ent.position)) - printout(fa_localising.get(ent,pindex) .. " ahead in " .. dist .. " meters", pindex) + local dir_ent = fa_utils.get_direction_biased(ent.position, p.vehicle.position) + if + p.vehicle.speed >= 0 and (dir_ent == dir or math.abs(dir_ent - dir) == 1 or math.abs(dir_ent - dir) == 7) + then + local dist = math.floor(util.distance(p.vehicle.position, ent.position)) + printout(fa_localising.get(ent, pindex) .. " ahead in " .. dist .. " meters", pindex) elseif p.vehicle.speed <= 0 and dir_ent == fa_utils.rotate_180(dir) then - local dist = math.floor(util.distance(p.vehicle.position,ent.position)) - printout(fa_localising.get(ent,pindex) .. " behind in " .. dist .. " meters", pindex) + local dist = math.floor(util.distance(p.vehicle.position, ent.position)) + printout(fa_localising.get(ent, pindex) .. " behind in " .. dist .. " meters", pindex) end end end @@ -3820,12 +4079,10 @@ end) script.on_event("read-rail-structure-behind", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local ent = get_selected_ent(pindex) if game.get_player(pindex).driving and game.get_player(pindex).vehicle.train ~= nil then - fa_trains.train_read_next_rail_entity_ahead(pindex,true) + fa_trains.train_read_next_rail_entity_ahead(pindex, true) elseif ent ~= nil and ent.valid and (ent.name == "straight-rail" or ent.name == "curved-rail") then --Report what is along the rail fa_rails.rail_read_next_rail_entity_ahead(pindex, ent, false) @@ -3834,212 +4091,224 @@ end) script.on_event("rescan", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - if not (players[pindex].in_menu) then + if not check_for_player(pindex) then return end + if not players[pindex].in_menu then fa_scanner.run_scanner_effects(pindex) - schedule(2,"call_to_run_scan",pindex, nil, false) + schedule(2, "call_to_run_scan", pindex, nil, false) end end) script.on_event("scan-facing-direction", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local p = game.get_player(pindex) - if p.character == nil then - return - end - if not (players[pindex].in_menu) then - --Set the filter direction + if p.character == nil then return end + if not players[pindex].in_menu then + --Set the filter direction local p = game.get_player(pindex) local dir = p.character.direction fa_scanner.run_scanner_effects(pindex) - schedule(2,"call_to_run_scan",pindex, dir, false) + schedule(2, "call_to_run_scan", pindex, dir, false) end end) script.on_event("a-scan-list-main-up-key", function(event) --laterdo: find a more elegant scan list solution here. It depends on hardcoded keybindings and alphabetically named event handling pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end players[pindex].last_pg_key_tick = event.tick end) script.on_event("a-scan-list-main-down-key", function(event) --laterdo: find a more elegant scan list solution here. It depends on hardcoded keybindings and alphabetically named event handling pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end players[pindex].last_pg_key_tick = event.tick end) script.on_event("scan-list-up", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - if not players[pindex].in_menu and not players[pindex].cursor then - fa_scanner.list_up(pindex) - end - if players[pindex].cursor and players[pindex].last_pg_key_tick ~= nil and event.tick - players[pindex].last_pg_key_tick < 10 then + if not check_for_player(pindex) then return end + if not players[pindex].in_menu and not players[pindex].cursor then fa_scanner.list_up(pindex) end + if + players[pindex].cursor + and players[pindex].last_pg_key_tick ~= nil + and event.tick - players[pindex].last_pg_key_tick < 10 + then fa_scanner.list_up(pindex) end end) script.on_event("scan-list-down", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - if not players[pindex].in_menu and not players[pindex].cursor then - fa_scanner.list_down(pindex) - end - if players[pindex].cursor and players[pindex].last_pg_key_tick ~= nil and event.tick - players[pindex].last_pg_key_tick < 10 then + if not check_for_player(pindex) then return end + if not players[pindex].in_menu and not players[pindex].cursor then fa_scanner.list_down(pindex) end + if + players[pindex].cursor + and players[pindex].last_pg_key_tick ~= nil + and event.tick - players[pindex].last_pg_key_tick < 10 + then fa_scanner.list_down(pindex) end end) script.on_event("scan-list-middle", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - if not players[pindex].in_menu then - fa_scanner.list_current(pindex) - end -end) - -script.on_event("jump-to-scan", function(event)--NOTE: This might be deprecated or redundant, since the cursor already goes to the scanned object now. laterdo remove? - pindex = event.player_index - if not check_for_player(pindex) then - return - end - if not (players[pindex].in_menu) then - if (players[pindex].nearby.category == 1 and next(players[pindex].nearby.ents) == nil) or - (players[pindex].nearby.category == 2 and next(players[pindex].nearby.resources) == nil) or - (players[pindex].nearby.category == 3 and next(players[pindex].nearby.containers) == nil) or - (players[pindex].nearby.category == 4 and next(players[pindex].nearby.buildings) == nil) or - (players[pindex].nearby.category == 5 and next(players[pindex].nearby.vehicles) == nil) or - (players[pindex].nearby.category == 6 and next(players[pindex].nearby.players) == nil) or - (players[pindex].nearby.category == 7 and next(players[pindex].nearby.enemies) == nil) or - (players[pindex].nearby.category == 8 and next(players[pindex].nearby.other) == nil) then - printout("No entities found. Try refreshing with end key.", pindex) - else - local ents = {} - if players[pindex].nearby.category == 1 then - ents = players[pindex].nearby.ents - elseif players[pindex].nearby.category == 2 then - ents = players[pindex].nearby.resources - elseif players[pindex].nearby.category == 3 then - ents = players[pindex].nearby.containers - elseif players[pindex].nearby.category == 4 then - ents = players[pindex].nearby.buildings - elseif players[pindex].nearby.category == 5 then - ents = players[pindex].nearby.vehicles - elseif players[pindex].nearby.category == 6 then - ents = players[pindex].nearby.players - elseif players[pindex].nearby.category == 7 then - ents = players[pindex].nearby.enemies - elseif players[pindex].nearby.category == 8 then - ents = players[pindex].nearby.other - end - local ent = nil - if ents.aggregate == false then - local i = 1 - while i <= #ents[players[pindex].nearby.index].ents do - if ents[players[pindex].nearby.index].ents[i].valid then - i = i + 1 - else - table.remove(ents[players[pindex].nearby.index].ents, i) - if players[pindex].nearby.selection > i then - players[pindex].nearby.selection = players[pindex].nearby.selection - 1 + if not check_for_player(pindex) then return end + if not players[pindex].in_menu then fa_scanner.list_current(pindex) end +end) + +script.on_event( + "jump-to-scan", + function(event) --NOTE: This might be deprecated or redundant, since the cursor already goes to the scanned object now. laterdo remove? + pindex = event.player_index + if not check_for_player(pindex) then return end + if not players[pindex].in_menu then + if + (players[pindex].nearby.category == 1 and next(players[pindex].nearby.ents) == nil) + or (players[pindex].nearby.category == 2 and next(players[pindex].nearby.resources) == nil) + or (players[pindex].nearby.category == 3 and next(players[pindex].nearby.containers) == nil) + or (players[pindex].nearby.category == 4 and next(players[pindex].nearby.buildings) == nil) + or (players[pindex].nearby.category == 5 and next(players[pindex].nearby.vehicles) == nil) + or (players[pindex].nearby.category == 6 and next(players[pindex].nearby.players) == nil) + or (players[pindex].nearby.category == 7 and next(players[pindex].nearby.enemies) == nil) + or (players[pindex].nearby.category == 8 and next(players[pindex].nearby.other) == nil) + then + printout("No entities found. Try refreshing with end key.", pindex) + else + local ents = {} + if players[pindex].nearby.category == 1 then + ents = players[pindex].nearby.ents + elseif players[pindex].nearby.category == 2 then + ents = players[pindex].nearby.resources + elseif players[pindex].nearby.category == 3 then + ents = players[pindex].nearby.containers + elseif players[pindex].nearby.category == 4 then + ents = players[pindex].nearby.buildings + elseif players[pindex].nearby.category == 5 then + ents = players[pindex].nearby.vehicles + elseif players[pindex].nearby.category == 6 then + ents = players[pindex].nearby.players + elseif players[pindex].nearby.category == 7 then + ents = players[pindex].nearby.enemies + elseif players[pindex].nearby.category == 8 then + ents = players[pindex].nearby.other + end + local ent = nil + if ents.aggregate == false then + local i = 1 + while i <= #ents[players[pindex].nearby.index].ents do + if ents[players[pindex].nearby.index].ents[i].valid then + i = i + 1 + else + table.remove(ents[players[pindex].nearby.index].ents, i) + if players[pindex].nearby.selection > i then + players[pindex].nearby.selection = players[pindex].nearby.selection - 1 + end end end - end - if #ents[players[pindex].nearby.index].ents == 0 then - table.remove(ents,players[pindex].nearby.index) - players[pindex].nearby.index = math.min(players[pindex].nearby.index, #ents) - fa_scanner.list_index(pindex) - return - end + if #ents[players[pindex].nearby.index].ents == 0 then + table.remove(ents, players[pindex].nearby.index) + players[pindex].nearby.index = math.min(players[pindex].nearby.index, #ents) + fa_scanner.list_index(pindex) + return + end - table.sort(ents[players[pindex].nearby.index].ents, function(k1, k2) - local pos = players[pindex].position - return util.distance(pos, k1.position) < util.distance(pos, k2.position) - end) - if players[pindex].nearby.selection > #ents[players[pindex].nearby.index].ents then - players[pindex].selection = 1 - end + table.sort(ents[players[pindex].nearby.index].ents, function(k1, k2) + local pos = players[pindex].position + return util.distance(pos, k1.position) < util.distance(pos, k2.position) + end) + if players[pindex].nearby.selection > #ents[players[pindex].nearby.index].ents then + players[pindex].selection = 1 + end - ent = ents[players[pindex].nearby.index].ents[players[pindex].nearby.selection] - if ent == nil then - printout("Error: This object no longer exists. Try rescanning.", pindex) - return - end - if not ent.valid then - printout("Error: This object is no longer valid. Try rescanning.", pindex) - return - end - else - if players[pindex].nearby.selection > #ents[players[pindex].nearby.index].ents then - players[pindex].selection = 1 - end - local name = ents[players[pindex].nearby.index].name - local entry = ents[players[pindex].nearby.index].ents[players[pindex].nearby.selection] - if table_size(entry) == 0 then - table.remove(ents[players[pindex].nearby.index].ents, players[pindex].nearby.selection) - players[pindex].nearby.selection = players[pindex].nearby.selection - 1 - fa_scanner.list_index(pindex) - return - end - if entry == nil then - printout("Error: This scanned object no longer exists. Try rescanning.", pindex) - return + ent = ents[players[pindex].nearby.index].ents[players[pindex].nearby.selection] + if ent == nil then + printout("Error: This object no longer exists. Try rescanning.", pindex) + return + end + if not ent.valid then + printout("Error: This object is no longer valid. Try rescanning.", pindex) + return + end + else + if players[pindex].nearby.selection > #ents[players[pindex].nearby.index].ents then + players[pindex].selection = 1 + end + local name = ents[players[pindex].nearby.index].name + local entry = ents[players[pindex].nearby.index].ents[players[pindex].nearby.selection] + if table_size(entry) == 0 then + table.remove(ents[players[pindex].nearby.index].ents, players[pindex].nearby.selection) + players[pindex].nearby.selection = players[pindex].nearby.selection - 1 + fa_scanner.list_index(pindex) + return + end + if entry == nil then + printout("Error: This scanned object no longer exists. Try rescanning.", pindex) + return + end + if + not entry.valid + and not ( + name == "water" + or name == "coal" + or name == "stone" + or name == "iron-ore" + or name == "copper-ore" + or name == "uranium-ore" + or name == "crude-oil" + or name == "forest" + ) + then --laterdo maybe this check needs to just be an aggregate check + printout("Error: This scanned object is no longer valid. Try rescanning.", pindex) --laterdo possible crash when trying to teleport to an entry that was depleted... + --game.get_player(pindex).print("invalid: " .. name) + return + end + ent = { name = name, position = table.deepcopy(entry.position) } --**beta** (fixed) end - if not entry.valid and not (name == "water" or name == "coal" or name == "stone" or name == "iron-ore" or name == "copper-ore" or name == "uranium-ore" or name == "crude-oil" or name == "forest") then--laterdo maybe this check needs to just be an aggregate check - printout("Error: This scanned object is no longer valid. Try rescanning.", pindex)--laterdo possible crash when trying to teleport to an entry that was depleted... - --game.get_player(pindex).print("invalid: " .. name) - return + if players[pindex].cursor then + players[pindex].cursor_pos = fa_utils.center_of_tile(ent.position) + fa_graphics.draw_cursor_highlight(pindex, ent, nil) + fa_graphics.sync_build_cursor_graphics(pindex) + printout( + "Cursor has jumped to " + .. ent.name + .. " at " + .. math.floor(players[pindex].cursor_pos.x) + .. " " + .. math.floor(players[pindex].cursor_pos.y), + pindex + ) + else + fa_teleport.teleport_to_closest(pindex, ent.position, false, false) + players[pindex].cursor_pos = + fa_utils.offset_position(players[pindex].position, players[pindex].player_direction, 1) + fa_graphics.draw_cursor_highlight(pindex, nil, nil) --laterdo check for new cursor ent here, to update the highlight? + fa_graphics.sync_build_cursor_graphics(pindex) end - ent = {name = name, position = table.deepcopy(entry.position)}--**beta** (fixed) - end - if players[pindex].cursor then - players[pindex].cursor_pos = fa_utils.center_of_tile(ent.position) - fa_graphics.draw_cursor_highlight(pindex, ent, nil) - fa_graphics.sync_build_cursor_graphics(pindex) - printout("Cursor has jumped to " .. ent.name .. " at " .. math.floor(players[pindex].cursor_pos.x) .. " " .. math.floor(players[pindex].cursor_pos.y), pindex) - else - fa_teleport.teleport_to_closest(pindex, ent.position, false, false) - players[pindex].cursor_pos = fa_utils.offset_position(players[pindex].position, players[pindex].player_direction, 1) - fa_graphics.draw_cursor_highlight(pindex, nil, nil)--laterdo check for new cursor ent here, to update the highlight? - fa_graphics.sync_build_cursor_graphics(pindex) end end end -end) +) script.on_event("scan-category-up", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - if not (players[pindex].in_menu) then + if not check_for_player(pindex) then return end + if not players[pindex].in_menu then local new_category = players[pindex].nearby.category - 1 - while new_category > 0 and ( - (new_category == 1 and next(players[pindex].nearby.ents) == nil) or - (new_category == 2 and next(players[pindex].nearby.resources) == nil) or - (new_category == 3 and next(players[pindex].nearby.containers) == nil) or - (new_category == 4 and next(players[pindex].nearby.buildings) == nil) or - (new_category == 5 and next(players[pindex].nearby.vehicles) == nil) or - (new_category == 6 and next(players[pindex].nearby.players) == nil) or - (new_category == 7 and next(players[pindex].nearby.enemies) == nil) or - (new_category == 8 and next(players[pindex].nearby.other) == nil)) do + while + new_category > 0 + and ( + (new_category == 1 and next(players[pindex].nearby.ents) == nil) + or (new_category == 2 and next(players[pindex].nearby.resources) == nil) + or (new_category == 3 and next(players[pindex].nearby.containers) == nil) + or (new_category == 4 and next(players[pindex].nearby.buildings) == nil) + or (new_category == 5 and next(players[pindex].nearby.vehicles) == nil) + or (new_category == 6 and next(players[pindex].nearby.players) == nil) + or (new_category == 7 and next(players[pindex].nearby.enemies) == nil) + or (new_category == 8 and next(players[pindex].nearby.other) == nil) + ) + do new_category = new_category - 1 end if new_category > 0 then @@ -4068,20 +4337,22 @@ end) script.on_event("scan-category-down", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - if not (players[pindex].in_menu) then - local new_category = players[pindex].nearby.category + 1 - while new_category <= 8 and ( - (new_category == 1 and next(players[pindex].nearby.ents) == nil) or - (new_category == 2 and next(players[pindex].nearby.resources) == nil) or - (new_category == 3 and next(players[pindex].nearby.containers) == nil) or - (new_category == 4 and next(players[pindex].nearby.buildings) == nil) or - (new_category == 5 and next(players[pindex].nearby.vehicles) == nil) or - (new_category == 6 and next(players[pindex].nearby.players) == nil) or - (new_category == 7 and next(players[pindex].nearby.enemies) == nil) or - (new_category == 8 and next(players[pindex].nearby.other) == nil) ) do + if not check_for_player(pindex) then return end + if not players[pindex].in_menu then + local new_category = players[pindex].nearby.category + 1 + while + new_category <= 8 + and ( + (new_category == 1 and next(players[pindex].nearby.ents) == nil) + or (new_category == 2 and next(players[pindex].nearby.resources) == nil) + or (new_category == 3 and next(players[pindex].nearby.containers) == nil) + or (new_category == 4 and next(players[pindex].nearby.buildings) == nil) + or (new_category == 5 and next(players[pindex].nearby.vehicles) == nil) + or (new_category == 6 and next(players[pindex].nearby.players) == nil) + or (new_category == 7 and next(players[pindex].nearby.enemies) == nil) + or (new_category == 8 and next(players[pindex].nearby.other) == nil) + ) + do new_category = new_category + 1 end if new_category <= 8 then @@ -4109,13 +4380,10 @@ script.on_event("scan-category-down", function(event) end end) - script.on_event("scan-sort-by-distance", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - if not (players[pindex].in_menu) then + if not check_for_player(pindex) then return end + if not players[pindex].in_menu then local ent = game.get_player(pindex).selected or get_selected_ent(pindex) if ent ~= nil and ent.valid == true and (ent.get_control_behavior() ~= nil or ent.type == "electric-pole") then --Open the circuit network menu for the selected ent instead. @@ -4128,13 +4396,10 @@ script.on_event("scan-sort-by-distance", function(event) end end) - script.on_event("scan-sort-by-count", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - if not (players[pindex].in_menu) then + if not check_for_player(pindex) then return end + if not players[pindex].in_menu then players[pindex].nearby.index = 1 players[pindex].nearby.count = true printout("Sorting scan results by total count", pindex) @@ -4145,14 +4410,12 @@ end) --Move along different inmstances of the same item type script.on_event("scan-selection-up", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - if not (players[pindex].in_menu) then + if not check_for_player(pindex) then return end + if not players[pindex].in_menu then if players[pindex].nearby.selection > 1 then players[pindex].nearby.selection = players[pindex].nearby.selection - 1 else - game.get_player(pindex).play_sound{path = "inventory-edge"} + game.get_player(pindex).play_sound({ path = "inventory-edge" }) players[pindex].nearby.selection = 1 end fa_scanner.list_index(pindex) @@ -4162,18 +4425,18 @@ end) --Move along different inmstances of the same item type script.on_event("scan-selection-down", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - if not (players[pindex].in_menu) then - if (players[pindex].nearby.category == 1 and next(players[pindex].nearby.ents) == nil) or - (players[pindex].nearby.category == 2 and next(players[pindex].nearby.resources) == nil) or - (players[pindex].nearby.category == 3 and next(players[pindex].nearby.containers) == nil) or - (players[pindex].nearby.category == 4 and next(players[pindex].nearby.buildings) == nil) or - (players[pindex].nearby.category == 5 and next(players[pindex].nearby.vehicles) == nil) or - (players[pindex].nearby.category == 6 and next(players[pindex].nearby.players) == nil) or - (players[pindex].nearby.category == 7 and next(players[pindex].nearby.enemies) == nil) or - (players[pindex].nearby.category == 8 and next(players[pindex].nearby.other) == nil) then + if not check_for_player(pindex) then return end + if not players[pindex].in_menu then + if + (players[pindex].nearby.category == 1 and next(players[pindex].nearby.ents) == nil) + or (players[pindex].nearby.category == 2 and next(players[pindex].nearby.resources) == nil) + or (players[pindex].nearby.category == 3 and next(players[pindex].nearby.containers) == nil) + or (players[pindex].nearby.category == 4 and next(players[pindex].nearby.buildings) == nil) + or (players[pindex].nearby.category == 5 and next(players[pindex].nearby.vehicles) == nil) + or (players[pindex].nearby.category == 6 and next(players[pindex].nearby.players) == nil) + or (players[pindex].nearby.category == 7 and next(players[pindex].nearby.enemies) == nil) + or (players[pindex].nearby.category == 8 and next(players[pindex].nearby.other) == nil) + then printout("No entities found. Try refreshing with end key.", pindex) else local ents = {} @@ -4198,7 +4461,7 @@ script.on_event("scan-selection-down", function(event) if players[pindex].nearby.selection < #ents[players[pindex].nearby.index].ents then players[pindex].nearby.selection = players[pindex].nearby.selection + 1 else - game.get_player(pindex).play_sound{path = "inventory-edge"} + game.get_player(pindex).play_sound({ path = "inventory-edge" }) players[pindex].nearby.selection = #ents[players[pindex].nearby.index].ents end end @@ -4209,37 +4472,41 @@ end) --Repeats the last thing read out. Not just the scanner. script.on_event("repeat-last-spoken", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end repeat_last_spoken(pindex) end) --Calls function to notify if items are being picked up via vanilla F key. script.on_event("pickup-items-info", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local p = game.get_player(pindex) local bp = p.cursor_stack - if bp and bp.valid_for_read and bp.is_blueprint then - return - end + if bp and bp.valid_for_read and bp.is_blueprint then return end read_item_pickup_state(pindex) end) function read_item_pickup_state(pindex) if players[pindex].in_menu then - printout("Cannot pickup items while in a menu",pindex) + printout("Cannot pickup items while in a menu", pindex) return end local p = game.get_player(pindex) local result = "" local check_last_pickup = false - local nearby_belts = p.surface.find_entities_filtered{position = p.position, radius = 1.25, type = "transport-belt"} - local nearby_ground_items = p.surface.find_entities_filtered{position = p.position, radius = 1.25, name = "item-on-ground"} - rendering.draw_circle{color = {0.3, 1, 0.3},radius = 1.25,width = 1,target = p.position, surface = p.surface,time_to_live = 60, draw_on_ground = true} + local nearby_belts = + p.surface.find_entities_filtered({ position = p.position, radius = 1.25, type = "transport-belt" }) + local nearby_ground_items = + p.surface.find_entities_filtered({ position = p.position, radius = 1.25, name = "item-on-ground" }) + rendering.draw_circle({ + color = { 0.3, 1, 0.3 }, + radius = 1.25, + width = 1, + target = p.position, + surface = p.surface, + time_to_live = 60, + draw_on_ground = true, + }) --Check if there is a belt within n tiles if #nearby_belts > 0 then result = "Picking up " @@ -4247,7 +4514,7 @@ function read_item_pickup_state(pindex) local ent = nearby_belts[1] if ent == nil or not ent.valid then result = result .. " from nearby belts" - printout(result,pindex) + printout(result, pindex) return end local left = ent.get_transport_line(1).get_contents() @@ -4262,7 +4529,7 @@ function read_item_pickup_state(pindex) end local contents = {} for name, count in pairs(left) do - table.insert(contents, {name = name, count = count}) + table.insert(contents, { name = name, count = count }) end table.sort(contents, function(k1, k2) return k1.count > k2.count @@ -4271,13 +4538,11 @@ function read_item_pickup_state(pindex) result = result .. contents[1].name if #contents > 1 then result = result .. ", and " .. contents[2].name - if #contents > 2 then - result = result .. ", and other item types " - end + if #contents > 2 then result = result .. ", and other item types " end end end result = result .. " from nearby belts" - --Check if there are ground items within n tiles + --Check if there are ground items within n tiles elseif #nearby_ground_items > 0 then result = "Picking up " if nearby_ground_items[1] and nearby_ground_items[1].valid then @@ -4287,17 +4552,23 @@ function read_item_pickup_state(pindex) else result = "No items within range to pick up" end - printout(result,pindex) + printout(result, pindex) end --Save info about last item pickup and draw radius script.on_event(defines.events.on_picked_up_item, function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local p = game.get_player(pindex) - rendering.draw_circle{color = {0.3, 1, 0.3},radius = 1.25,width = 1,target = p.position, surface = p.surface,time_to_live = 10, draw_on_ground = true} + rendering.draw_circle({ + color = { 0.3, 1, 0.3 }, + radius = 1.25, + width = 1, + target = p.position, + surface = p.surface, + time_to_live = 10, + draw_on_ground = true, + }) players[pindex].last_pickup_tick = event.tick players[pindex].last_item_picked_up = event.item_stack.name end) @@ -4305,32 +4576,28 @@ end) --Reads other entities on the same tile? Note: Possibly unneeded script.on_event("tile-cycle", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - if not (players[pindex].in_menu) then - tile_cycle(pindex) - end + if not check_for_player(pindex) then return end + if not players[pindex].in_menu then tile_cycle(pindex) end end) script.on_event("open-inventory", function(event) pindex = event.player_index if not check_for_player(pindex) then return - elseif (players[pindex].in_menu) or players[pindex].last_menu_toggle_tick == event.tick then + elseif players[pindex].in_menu or players[pindex].last_menu_toggle_tick == event.tick then return - elseif not (players[pindex].in_menu) then - open_player_inventory(event.tick,pindex) + elseif not players[pindex].in_menu then + open_player_inventory(event.tick, pindex) end end) --Sets up mod character menus. Cannot actually open the character GUI. -function open_player_inventory(tick,pindex) - game.get_player(pindex).play_sound{path = "Open-Inventory-Sound"} +function open_player_inventory(tick, pindex) + game.get_player(pindex).play_sound({ path = "Open-Inventory-Sound" }) game.get_player(pindex).selected = nil players[pindex].last_menu_toggle_tick = tick players[pindex].in_menu = true - players[pindex].menu="inventory" + players[pindex].menu = "inventory" players[pindex].inventory.lua_inventory = game.get_player(pindex).get_main_inventory() players[pindex].inventory.max = #players[pindex].inventory.lua_inventory players[pindex].inventory.index = 1 @@ -4350,22 +4617,16 @@ function open_player_inventory(tick,pindex) else local check = true for i1, preq in pairs(tech.prerequisites) do - if not(preq.researched) then - check = false - end + if not preq.researched then check = false end end if check then table.insert(players[pindex].technology.lua_researchable, tech) else local check = false for i1, preq in pairs(tech.prerequisites) do - if preq.researched then - check = true - end - end - if check then - table.insert(players[pindex].technology.lua_locked, tech) + if preq.researched then check = true end end + if check then table.insert(players[pindex].technology.lua_locked, tech) end end end end @@ -4386,7 +4647,7 @@ function read_technology_slot(pindex, start_phrase) if next(techs) ~= nil and players[pindex].technology.index > 0 and players[pindex].technology.index <= #techs then local tech = techs[players[pindex].technology.index] if tech.valid then - printout(start_phrase .. fa_localising.get(tech,pindex), pindex) + printout(start_phrase .. fa_localising.get(tech, pindex), pindex) else printout(start_phrase .. "Error loading technology", pindex) end @@ -4395,18 +4656,22 @@ function read_technology_slot(pindex, start_phrase) end end -script.on_event("close-menu-access", function(event)--close_menu, menu closed +script.on_event("close-menu-access", function(event) --close_menu, menu closed pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end players[pindex].move_queue = {} if not players[pindex].in_menu or players[pindex].last_menu_toggle_tick == event.tick then return elseif players[pindex].in_menu and players[pindex].menu ~= "prompt" then printout("Menu closed.", pindex) - if players[pindex].menu == "inventory" or players[pindex].menu == "crafting" or players[pindex].menu == "technology" or players[pindex].menu == "crafting_queue" or players[pindex].menu == "warnings" then--**laterdo open close inv sounds in other menus? - game.get_player(pindex).play_sound{path="Close-Inventory-Sound"} + if + players[pindex].menu == "inventory" + or players[pindex].menu == "crafting" + or players[pindex].menu == "technology" + or players[pindex].menu == "crafting_queue" + or players[pindex].menu == "warnings" + then --**laterdo open close inv sounds in other menus? + game.get_player(pindex).play_sound({ path = "Close-Inventory-Sound" }) end players[pindex].last_menu_toggle_tick = event.tick close_menu_resets(pindex) @@ -4437,9 +4702,7 @@ function close_menu_resets(pindex) circuit_network_menu_close(pindex, false) end - if p.gui.screen["cursor-jump"] ~= nil then - p.gui.screen["cursor-jump"].destroy() - end + if p.gui.screen["cursor-jump"] ~= nil then p.gui.screen["cursor-jump"].destroy() end --Stop any enabled mouse entity selection if players[pindex].vanilla_mode ~= true then @@ -4454,7 +4717,7 @@ function close_menu_resets(pindex) players[pindex].menu_search_index_2 = nil players[pindex].item_selection = false players[pindex].item_cache = {} - players[pindex].item_selector = {index = 0, group = 0, subgroup = 0} + players[pindex].item_selector = { index = 0, group = 0, subgroup = 0 } players[pindex].building = { index = 0, ent = nil, @@ -4464,21 +4727,19 @@ function close_menu_resets(pindex) item_selection = false, category = 0, recipe = nil, - recipe_list = nil + recipe_list = nil, } end -script.on_event("read-menu-name", function(event)--read_menu_name +script.on_event("read-menu-name", function(event) --read_menu_name pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local menu_name = "menu " if players[pindex].in_menu == false then menu_name = "no menu" elseif players[pindex].menu ~= nil and players[pindex].menu ~= "" then menu_name = players[pindex].menu - if (players[pindex].menu == "building" or players[pindex].menu == "vehicle") then + if players[pindex].menu == "building" or players[pindex].menu == "vehicle" then --Name the building local pb = players[pindex].building menu_name = menu_name .. " " .. pb.ent.name @@ -4496,72 +4757,68 @@ script.on_event("read-menu-name", function(event)--read_menu_name else menu_name = "unknown menu" end - printout(menu_name,pindex) + printout(menu_name, pindex) end) --Quickbar even handlers local quickbar_slots = {} local set_quickbar_names = {} local quickbar_pages = {} -for i = 1,10 do - table.insert(quickbar_slots,"quickbar-"..i) - table.insert(set_quickbar_names,"set-quickbar-"..i) - table.insert(quickbar_pages,"quickbar-page-"..i) +for i = 1, 10 do + table.insert(quickbar_slots, "quickbar-" .. i) + table.insert(set_quickbar_names, "set-quickbar-" .. i) + table.insert(quickbar_pages, "quickbar-page-" .. i) end ---@param event EventData.CustomInputEvent local function quickbar_slots_handler(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - if players[pindex].menu == "inventory" or players[pindex].menu == "none" or (players[pindex].menu == "building" or players[pindex].menu == "vehicle") then - local num=tonumber(string.sub(event.input_name,-1)) - if num == 0 then - num = 10 - end - read_quick_bar_slot(num,pindex) + if not check_for_player(pindex) then return end + if + players[pindex].menu == "inventory" + or players[pindex].menu == "none" + or (players[pindex].menu == "building" or players[pindex].menu == "vehicle") + then + local num = tonumber(string.sub(event.input_name, -1)) + if num == 0 then num = 10 end + read_quick_bar_slot(num, pindex) end end -script.on_event(quickbar_slots,quickbar_slots_handler ) +script.on_event(quickbar_slots, quickbar_slots_handler) --all 10 quickbar slot setting event handlers ---@param event EventData.CustomInputEvent local function set_quickbar_names_handler(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - if players[pindex].menu == "inventory" or players[pindex].menu == "none" or (players[pindex].menu == "building" or players[pindex].menu == "vehicle") then - local num=tonumber(string.sub(event.input_name,-1)) - if num == 0 then - num = 10 - end + if not check_for_player(pindex) then return end + if + players[pindex].menu == "inventory" + or players[pindex].menu == "none" + or (players[pindex].menu == "building" or players[pindex].menu == "vehicle") + then + local num = tonumber(string.sub(event.input_name, -1)) + if num == 0 then num = 10 end set_quick_bar_slot(num, pindex) end end -script.on_event(set_quickbar_names,set_quickbar_names_handler) +script.on_event(set_quickbar_names, set_quickbar_names_handler) --all 10 quickbar page setting event handlers ---@param event EventData.CustomInputEvent local function quickbar_pages_handler(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end - local num=tonumber(string.sub(event.input_name,-1)) - if num == 0 then - num = 10 - end + local num = tonumber(string.sub(event.input_name, -1)) + if num == 0 then num = 10 end read_switched_quick_bar(num, pindex) end -script.on_event(quickbar_pages,quickbar_pages_handler) +script.on_event(quickbar_pages, quickbar_pages_handler) -function read_quick_bar_slot(index,pindex) - page = game.get_player(pindex).get_active_quick_bar_page(1)-1 - local item = game.get_player(pindex).get_quick_bar_slot(index+ 10*page) +function read_quick_bar_slot(index, pindex) + page = game.get_player(pindex).get_active_quick_bar_page(1) - 1 + local item = game.get_player(pindex).get_quick_bar_slot(index + 10 * page) if item ~= nil then local count = game.get_player(pindex).get_main_inventory().get_item_count(item.name) local stack = game.get_player(pindex).cursor_stack @@ -4571,57 +4828,54 @@ function read_quick_bar_slot(index,pindex) else printout("selected " .. fa_localising.get(item, pindex) .. " x " .. count, pindex) end - else - printout("Empty quickbar slot",pindex)--does this print, maybe not working because it is linked to the game control? + printout("Empty quickbar slot", pindex) --does this print, maybe not working because it is linked to the game control? end end function set_quick_bar_slot(index, pindex) local p = game.get_player(pindex) - local page = game.get_player(pindex).get_active_quick_bar_page(1)-1 + local page = game.get_player(pindex).get_active_quick_bar_page(1) - 1 local stack_cur = game.get_player(pindex).cursor_stack local stack_inv = players[pindex].inventory.lua_inventory[players[pindex].inventory.index] local ent = get_selected_ent(pindex) if stack_cur and stack_cur.valid_for_read and stack_cur.valid == true then - game.get_player(pindex).set_quick_bar_slot(index + 10*page, stack_cur) + game.get_player(pindex).set_quick_bar_slot(index + 10 * page, stack_cur) printout("Quickbar assigned " .. index .. " " .. fa_localising.get(stack_cur, pindex), pindex) - elseif players[pindex].menu == "inventory" and stack_inv and stack_inv.valid_for_read and stack_inv.valid == true then - game.get_player(pindex).set_quick_bar_slot(index + 10*page, stack_inv) + elseif + players[pindex].menu == "inventory" + and stack_inv + and stack_inv.valid_for_read + and stack_inv.valid == true + then + game.get_player(pindex).set_quick_bar_slot(index + 10 * page, stack_inv) printout("Quickbar assigned " .. index .. " " .. fa_localising.get(stack_inv, pindex), pindex) elseif ent ~= nil and ent.valid and ent.force == p.force and game.item_prototypes[ent.name] ~= nil then - game.get_player(pindex).set_quick_bar_slot(index + 10*page, ent.name) + game.get_player(pindex).set_quick_bar_slot(index + 10 * page, ent.name) printout("Quickbar assigned " .. index .. " " .. fa_localising.get(ent, pindex), pindex) else --Clear the slot - local item = game.get_player(pindex).get_quick_bar_slot(index+ 10*page) + local item = game.get_player(pindex).get_quick_bar_slot(index + 10 * page) local item_name = "" - if item ~= nil then - item_name = fa_localising.get(item, pindex) - end + if item ~= nil then item_name = fa_localising.get(item, pindex) end ---@diagnostic disable-next-line: param-type-mismatch - game.get_player(pindex).set_quick_bar_slot(index + 10*page, nil) + game.get_player(pindex).set_quick_bar_slot(index + 10 * page, nil) printout("Quickbar unassigned " .. index .. " " .. item_name, pindex) end end -function read_switched_quick_bar(index,pindex) +function read_switched_quick_bar(index, pindex) page = game.get_player(pindex).get_active_quick_bar_page(index) - local item = game.get_player(pindex).get_quick_bar_slot(1 + 10*(index-1)) + local item = game.get_player(pindex).get_quick_bar_slot(1 + 10 * (index - 1)) local item_name = "empty slot" - if item ~= nil then - item_name = fa_localising.get(item, pindex) - end + if item ~= nil then item_name = fa_localising.get(item, pindex) end local result = "Quickbar " .. index .. " selected starting with " .. item_name printout(result, pindex) - end script.on_event("switch-menu-or-gun", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end if players[pindex].started ~= true then players[pindex].started = true @@ -4633,8 +4887,8 @@ script.on_event("switch-menu-or-gun", function(event) local logistics_researched = (trash_inv ~= nil and trash_inv.valid and #trash_inv > 0) if players[pindex].in_menu and players[pindex].menu ~= "prompt" then - game.get_player(pindex).play_sound{path="Change-Menu-Tab-Sound"} - if (players[pindex].menu == "building" or players[pindex].menu == "vehicle") then + game.get_player(pindex).play_sound({ path = "Change-Menu-Tab-Sound" }) + if players[pindex].menu == "building" or players[pindex].menu == "vehicle" then players[pindex].building.index = 1 players[pindex].building.category = 1 players[pindex].building.recipe_selection = false @@ -4646,15 +4900,15 @@ script.on_event("switch-menu-or-gun", function(event) players[pindex].item_selector = { index = 0, group = 0, - subgroup = 0 + subgroup = 0, } if players[pindex].building.sector <= #players[pindex].building.sectors then fa_sectors.read_sector_slot(pindex, true) players[pindex].building.sector_name = "other" --- if inventory == players[pindex].building.sectors[players[pindex].building.sector+1].inventory then --- printout("Big Problem!", pindex) - -- end + -- if inventory == players[pindex].building.sectors[players[pindex].building.sector+1].inventory then + -- printout("Big Problem!", pindex) + -- end elseif players[pindex].building.recipe_list == nil then if players[pindex].building.sector == (#players[pindex].building.sectors + 1) then read_inventory_slot(pindex, "Player Inventory, ") @@ -4664,7 +4918,7 @@ script.on_event("switch-menu-or-gun", function(event) fa_sectors.read_sector_slot(pindex, true) end else - if players[pindex].building.sector == #players[pindex].building.sectors + 1 then --Recipe selection sector + if players[pindex].building.sector == #players[pindex].building.sectors + 1 then --Recipe selection sector fa_sectors.read_building_recipe(pindex, "Select a Recipe, ") players[pindex].building.sector_name = "recipe_selection" elseif players[pindex].building.sector == #players[pindex].building.sectors + 2 then --Player inventory sector @@ -4681,14 +4935,21 @@ script.on_event("switch-menu-or-gun", function(event) elseif players[pindex].menu == "crafting" then players[pindex].menu = "crafting_queue" fa_crafting.load_crafting_queue(pindex) - fa_crafting.read_crafting_queue(pindex, "Crafting queue, " .. fa_crafting.get_crafting_que_total(pindex) .. " total, ") + fa_crafting.read_crafting_queue( + pindex, + "Crafting queue, " .. fa_crafting.get_crafting_que_total(pindex) .. " total, " + ) elseif players[pindex].menu == "crafting_queue" then players[pindex].menu = "technology" read_technology_slot(pindex, "Technology, Researchable Technologies, ") elseif players[pindex].menu == "technology" then if logistics_researched then players[pindex].menu = "player_trash" - read_inventory_slot(pindex, "Logistic trash, ", game.get_player(pindex).get_inventory(defines.inventory.character_trash)) + read_inventory_slot( + pindex, + "Logistic trash, ", + game.get_player(pindex).get_inventory(defines.inventory.character_trash) + ) else players[pindex].menu = "inventory" read_inventory_slot(pindex, "Inventory, ") @@ -4699,9 +4960,7 @@ script.on_event("switch-menu-or-gun", function(event) elseif players[pindex].menu == "belt" then players[pindex].belt.index = 1 players[pindex].belt.sector = players[pindex].belt.sector + 1 - if players[pindex].belt.sector == 5 then - players[pindex].belt.sector = 1 - end + if players[pindex].belt.sector == 5 then players[pindex].belt.sector = 1 end local sector = players[pindex].belt.sector if sector == 1 then printout("Local Lanes", pindex) @@ -4714,9 +4973,7 @@ script.on_event("switch-menu-or-gun", function(event) end elseif players[pindex].menu == "warnings" then players[pindex].warnings.sector = players[pindex].warnings.sector + 1 - if players[pindex].warnings.sector > 3 then - players[pindex].warnings.sector = 1 - end + if players[pindex].warnings.sector > 3 then players[pindex].warnings.sector = 1 end if players[pindex].warnings.sector == 1 then printout("Short Range: " .. players[pindex].warnings.short.summary, pindex) elseif players[pindex].warnings.sector == 2 then @@ -4724,15 +4981,12 @@ script.on_event("switch-menu-or-gun", function(event) elseif players[pindex].warnings.sector == 3 then printout("Long Range: " .. players[pindex].warnings.long.summary, pindex) end - end end --Gun related changes (this seems to run before the actual switch happens so even when we write the new index, it will change, so we need to be predictive) local p = game.get_player(pindex) - if p.character == nil then - return - end + if p.character == nil then return end if p.vehicle ~= nil then --laterdo tank weapon naming *** return @@ -4744,10 +4998,10 @@ script.on_event("switch-menu-or-gun", function(event) if players[pindex].in_menu then --switch_success = swap_weapon_backward(pindex,true) - switched_index = swap_weapon_backward(pindex,true) + switched_index = swap_weapon_backward(pindex, true) return else - switched_index = swap_weapon_forward(pindex,false) + switched_index = swap_weapon_forward(pindex, false) end --Declare the selected weapon @@ -4759,30 +5013,28 @@ script.on_event("switch-menu-or-gun", function(event) result = "No ready weapons" else local ammo_stack = ammo_inv[gun_index] - local gun_stack = guns_inv[gun_index] + local gun_stack = guns_inv[gun_index] --game.print("print " .. gun_index)-- result = gun_stack.name .. " with " .. ammo_stack.count .. " " .. ammo_stack.name .. "s " end if not players[pindex].in_menu then --p.play_sound{path = "Inventory-Move"} - printout(result,pindex) + printout(result, pindex) end end) script.on_event("reverse-switch-menu-or-gun", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end --Check if logistics have been researched local trash_inv = game.get_player(pindex).get_inventory(defines.inventory.character_trash) local logistics_researched = (trash_inv ~= nil and trash_inv.valid and #trash_inv > 0) if players[pindex].in_menu and players[pindex].menu ~= "prompt" then - game.get_player(pindex).play_sound{path="Change-Menu-Tab-Sound"} - if (players[pindex].menu == "building" or players[pindex].menu == "vehicle") then + game.get_player(pindex).play_sound({ path = "Change-Menu-Tab-Sound" }) + if players[pindex].menu == "building" or players[pindex].menu == "vehicle" then players[pindex].building.category = 1 players[pindex].building.recipe_selection = false players[pindex].building.index = 1 @@ -4794,7 +5046,7 @@ script.on_event("reverse-switch-menu-or-gun", function(event) players[pindex].item_selector = { index = 0, group = 0, - subgroup = 0 + subgroup = 0, } if players[pindex].building.sector < 1 then @@ -4805,7 +5057,6 @@ script.on_event("reverse-switch-menu-or-gun", function(event) end players[pindex].building.sector_name = "player_inventory" read_inventory_slot(pindex, "Player Inventory, ") - elseif players[pindex].building.sector <= #players[pindex].building.sectors then fa_sectors.read_sector_slot(pindex, true) players[pindex].building.sector_name = "other" @@ -4823,12 +5074,14 @@ script.on_event("reverse-switch-menu-or-gun", function(event) players[pindex].building.sector_name = "player_inventory" end end - - elseif players[pindex].menu == "inventory" then if logistics_researched then players[pindex].menu = "player_trash" - read_inventory_slot(pindex, "Logistic trash, ", game.get_player(pindex).get_inventory(defines.inventory.character_trash)) + read_inventory_slot( + pindex, + "Logistic trash, ", + game.get_player(pindex).get_inventory(defines.inventory.character_trash) + ) else players[pindex].menu = "technology" read_technology_slot(pindex, "Technology, Researchable Technologies, ") @@ -4842,16 +5095,17 @@ script.on_event("reverse-switch-menu-or-gun", function(event) elseif players[pindex].menu == "technology" then players[pindex].menu = "crafting_queue" fa_crafting.load_crafting_queue(pindex) - fa_crafting.read_crafting_queue(pindex, "Crafting queue, " .. fa_crafting.get_crafting_que_total(pindex) .. " total, ") + fa_crafting.read_crafting_queue( + pindex, + "Crafting queue, " .. fa_crafting.get_crafting_que_total(pindex) .. " total, " + ) elseif players[pindex].menu == "crafting" then players[pindex].menu = "inventory" read_inventory_slot(pindex, "Inventory, ") elseif players[pindex].menu == "belt" then players[pindex].belt.index = 1 players[pindex].belt.sector = players[pindex].belt.sector - 1 - if players[pindex].belt.sector == 0 then - players[pindex].belt.sector = 4 - end + if players[pindex].belt.sector == 0 then players[pindex].belt.sector = 4 end local sector = players[pindex].belt.sector if sector == 1 then printout("Local Lanes", pindex) @@ -4864,9 +5118,7 @@ script.on_event("reverse-switch-menu-or-gun", function(event) end elseif players[pindex].menu == "warnings" then players[pindex].warnings.sector = players[pindex].warnings.sector - 1 - if players[pindex].warnings.sector < 1 then - players[pindex].warnings.sector = 3 - end + if players[pindex].warnings.sector < 1 then players[pindex].warnings.sector = 3 end if players[pindex].warnings.sector == 1 then printout("Short Range: " .. players[pindex].warnings.short.summary, pindex) elseif players[pindex].warnings.sector == 2 then @@ -4874,15 +5126,12 @@ script.on_event("reverse-switch-menu-or-gun", function(event) elseif players[pindex].warnings.sector == 3 then printout("Long Range: " .. players[pindex].warnings.long.summary, pindex) end - end end --Gun related changes (Vanilla Factorio DOES NOT have shift + tab weapon revserse switching, so we add it without prediction needed) local p = game.get_player(pindex) - if p.character == nil then - return - end + if p.character == nil then return end if p.vehicle ~= nil then --laterdo tank weapon naming *** return @@ -4896,7 +5145,7 @@ script.on_event("reverse-switch-menu-or-gun", function(event) --do nothing return else - switched_index = swap_weapon_backward(pindex,true) + switched_index = swap_weapon_backward(pindex, true) end --Declare the selected weapon @@ -4908,14 +5157,14 @@ script.on_event("reverse-switch-menu-or-gun", function(event) result = "No ready weapons" else local ammo_stack = ammo_inv[gun_index] - local gun_stack = guns_inv[gun_index] + local gun_stack = guns_inv[gun_index] --game.print("print " .. gun_index)-- result = gun_stack.name .. " with " .. ammo_stack.count .. " " .. ammo_stack.name .. "s " end if not players[pindex].in_menu then - p.play_sound{path = "Inventory-Move"} - printout(result,pindex) + p.play_sound({ path = "Inventory-Move" }) + printout(result, pindex) end end) @@ -4930,22 +5179,28 @@ function swap_weapon_forward(pindex, write_to_character) --Simple index increment (not needed) gun_index = gun_index + 1 - if gun_index > 3 then - gun_index = 1 - end + if gun_index > 3 then gun_index = 1 end --game.print("start " .. gun_index)-- --Increment again if the new index has no guns or no ammo local ammo_stack = ammo_inv[gun_index] - local gun_stack = guns_inv[gun_index] + local gun_stack = guns_inv[gun_index] local tries = 0 - while tries < 4 and (ammo_stack == nil or not ammo_stack.valid_for_read or not ammo_stack.valid or gun_stack == nil or not gun_stack.valid_for_read or not gun_stack.valid) do + while + tries < 4 + and ( + ammo_stack == nil + or not ammo_stack.valid_for_read + or not ammo_stack.valid + or gun_stack == nil + or not gun_stack.valid_for_read + or not gun_stack.valid + ) + do gun_index = gun_index + 1 - if gun_index > 3 then - gun_index = 1 - end + if gun_index > 3 then gun_index = 1 end ammo_stack = ammo_inv[gun_index] - gun_stack = guns_inv[gun_index] + gun_stack = guns_inv[gun_index] tries = tries + 1 end @@ -4954,9 +5209,7 @@ function swap_weapon_forward(pindex, write_to_character) return -1 end - if write_to_character then - p.character.selected_gun_index = gun_index - end + if write_to_character then p.character.selected_gun_index = gun_index end --game.print("end " .. gun_index)-- return gun_index end @@ -4967,54 +5220,52 @@ function swap_weapon_backward(pindex, write_to_character) return 0 --TODO: does this cause problems??? end local gun_index = p.character.selected_gun_index - if gun_index==nil then - return 0 - end + if gun_index == nil then return 0 end local guns_inv = p.get_inventory(defines.inventory.character_guns) local ammo_inv = game.get_player(pindex).get_inventory(defines.inventory.character_ammo) --Simple index increment (not needed) gun_index = gun_index - 1 - if gun_index < 1 then - gun_index = 3 - end + if gun_index < 1 then gun_index = 3 end --Increment again if the new index has no guns or no ammo local ammo_stack = ammo_inv[gun_index] - local gun_stack = guns_inv[gun_index] + local gun_stack = guns_inv[gun_index] local tries = 0 - while tries < 4 and (ammo_stack == nil or not ammo_stack.valid_for_read or not ammo_stack.valid or gun_stack == nil or not gun_stack.valid_for_read or not gun_stack.valid) do + while + tries < 4 + and ( + ammo_stack == nil + or not ammo_stack.valid_for_read + or not ammo_stack.valid + or gun_stack == nil + or not gun_stack.valid_for_read + or not gun_stack.valid + ) + do gun_index = gun_index - 1 - if gun_index < 1 then - gun_index = 3 - end + if gun_index < 1 then gun_index = 3 end ammo_stack = ammo_inv[gun_index] - gun_stack = guns_inv[gun_index] + gun_stack = guns_inv[gun_index] tries = tries + 1 end - if tries > 3 then - return -1 - end + if tries > 3 then return -1 end - if write_to_character then - p.character.selected_gun_index = gun_index - end + if write_to_character then p.character.selected_gun_index = gun_index end return gun_index end ---Creates sound effects for vanilla mining +--Creates sound effects for vanilla mining script.on_event("mine-access-sounds", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - if not (players[pindex].in_menu) and not players[pindex].vanilla_mode then + if not check_for_player(pindex) then return end + if not players[pindex].in_menu and not players[pindex].vanilla_mode then target(pindex) local ent = get_selected_ent(pindex) if ent and ent.valid and (ent.prototype.mineable_properties.products ~= nil) and ent.type ~= "resource" then game.get_player(pindex).selected = ent - game.get_player(pindex).play_sound{path = "player-mine"} + game.get_player(pindex).play_sound({ path = "player-mine" }) elseif ent and ent.valid and ent.name == "character-corpse" then printout("Collecting items ", pindex) end @@ -5024,10 +5275,8 @@ end) --Mines tiles such as stone brick or concrete within the cursor area, including enlarged cursors script.on_event("mine-tiles", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - if not (players[pindex].in_menu) and not players[pindex].vanilla_mode then + if not check_for_player(pindex) then return end + if not players[pindex].in_menu and not players[pindex].vanilla_mode then --Mine tiles around the cursor local stack = game.get_player(pindex).cursor_stack local surf = game.get_player(pindex).surface @@ -5035,14 +5284,12 @@ script.on_event("mine-tiles", function(event) players[pindex].allow_reading_flying_text = false local c_pos = players[pindex].cursor_pos local c_size = players[pindex].cursor_size - local left_top = {x = math.floor(c_pos.x - c_size), y = math.floor(c_pos.y - c_size)} - local right_bottom = {x = math.floor(c_pos.x + 1 + c_size), y = math.floor(c_pos.y + 1 + c_size)} - local tiles = surf.find_tiles_filtered{area = {left_top, right_bottom}} - for i , tile in ipairs(tiles) do + local left_top = { x = math.floor(c_pos.x - c_size), y = math.floor(c_pos.y - c_size) } + local right_bottom = { x = math.floor(c_pos.x + 1 + c_size), y = math.floor(c_pos.y + 1 + c_size) } + local tiles = surf.find_tiles_filtered({ area = { left_top, right_bottom } }) + for i, tile in ipairs(tiles) do local mined = game.get_player(pindex).mine_tile(tile) - if mined then - game.get_player(pindex).play_sound{path = "entity-mined/stone-furnace"} - end + if mined then game.get_player(pindex).play_sound({ path = "entity-mined/stone-furnace" }) end end end end @@ -5051,34 +5298,33 @@ end) --Flush the selected fluid script.on_event("flush-fluid", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - if players[pindex].menu ~= "building" then - return - end - if players[pindex].building.ent ~= nil and players[pindex].building.ent.valid and players[pindex].building.ent.type == "fluid-turret" and players[pindex].building.index ~= 1 then + if not check_for_player(pindex) then return end + if players[pindex].menu ~= "building" then return end + if + players[pindex].building.ent ~= nil + and players[pindex].building.ent.valid + and players[pindex].building.ent.type == "fluid-turret" + and players[pindex].building.index ~= 1 + then --Prevent fluid turret crashes players[pindex].building.index = 1 end local building_sector = players[pindex].building.sectors[players[pindex].building.sector] - local box = building_sector.inventory--= players[pindex].building.fluidbox -- - if building_sector.name ~= "Fluid" then - return - end + local box = building_sector.inventory --= players[pindex].building.fluidbox -- + if building_sector.name ~= "Fluid" then return end if box == nil or #box == 0 then - printout("No fluids to flush" , pindex) + printout("No fluids to flush", pindex) return end local fluid = box[players[pindex].building.index] local len = #box - local name = "Nothing" + local name = "Nothing" local amount = 0 if fluid ~= nil and fluid.name ~= nil then amount = fluid.amount - name = fluid.name--does not localize..?** + name = fluid.name --does not localize..?** else - printout("No fluids to flush" , pindex) + printout("No fluids to flush", pindex) return end --Read the fluid found, including amount if any @@ -5089,22 +5335,23 @@ end) --Mines groups of entities depending on the name or type. Includes trees and rocks, rails. script.on_event("mine-area", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - if players[pindex].in_menu then - return - end - local ent = get_selected_ent(pindex) + if not check_for_player(pindex) then return end + if players[pindex].in_menu then return end + local ent = get_selected_ent(pindex) local cleared_count = 0 local cleared_total = 0 local comment = "" --Check if within reach - if ent ~= nil and ent.valid and util.distance(game.get_player(pindex).position, ent.position) > game.get_player(pindex).reach_distance - or util.distance(game.get_player(pindex).position, players[pindex].cursor_pos) > game.get_player(pindex).reach_distance then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} - printout("This area is out of player reach",pindex) + if + ent ~= nil + and ent.valid + and util.distance(game.get_player(pindex).position, ent.position) > game.get_player(pindex).reach_distance + or util.distance(game.get_player(pindex).position, players[pindex].cursor_pos) + > game.get_player(pindex).reach_distance + then + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) + printout("This area is out of player reach", pindex) return end @@ -5116,46 +5363,68 @@ script.on_event("mine-area", function(event) if ent then local surf = ent.surface local pos = ent.position - if ent.type == "tree" or ent.name == "rock-big" or ent.name == "rock-huge" or ent.name == "sand-rock-big" or ent.name == "item-on-ground" then + if + ent.type == "tree" + or ent.name == "rock-big" + or ent.name == "rock-huge" + or ent.name == "sand-rock-big" + or ent.name == "item-on-ground" + then --Obstacles within 5 tiles: trees and rocks and ground items - game.get_player(pindex).play_sound{path = "player-mine"} + game.get_player(pindex).play_sound({ path = "player-mine" }) cleared_count, comment = fa_mining_tools.clear_obstacles_in_circle(pos, 5, pindex) elseif ent.name == "straight-rail" or ent.name == "curved-rail" then --Railway objects within 10 tiles (and their signals) - local rail_ents = surf.find_entities_filtered{position = pos, radius = 10, name = {"straight-rail", "curved-rail", "rail-signal", "rail-chain-signal", "train-stop"}} - for i,rail_ent in ipairs(rail_ents) do + local rail_ents = surf.find_entities_filtered({ + position = pos, + radius = 10, + name = { "straight-rail", "curved-rail", "rail-signal", "rail-chain-signal", "train-stop" }, + }) + for i, rail_ent in ipairs(rail_ents) do if rail_ent.name == "straight-rail" or rail_ent.name == "curved-rail" then - fa_rails.mine_signals(rail_ent,pindex) + fa_rails.mine_signals(rail_ent, pindex) end - game.get_player(pindex).play_sound{path = "entity-mined/straight-rail"} - game.get_player(pindex).mine_entity(rail_ent,true) + game.get_player(pindex).play_sound({ path = "entity-mined/straight-rail" }) + game.get_player(pindex).mine_entity(rail_ent, true) cleared_count = cleared_count + 1 end - rendering.draw_circle{color = {0, 1, 0}, radius = 10, width = 2, target = pos, surface = surf,time_to_live = 60} + rendering.draw_circle({ + color = { 0, 1, 0 }, + radius = 10, + width = 2, + target = pos, + surface = surf, + time_to_live = 60, + }) printout(" Cleared away " .. cleared_count .. " railway objects within 10 tiles. ", pindex) return elseif ent.name == "entity-ghost" then --Ghosts within 10 tiles - local ghosts = surf.find_entities_filtered{position = pos, radius = 10, name = {"entity-ghost"}} - for i,ghost in ipairs(ghosts) do - game.get_player(pindex).mine_entity(ghost,true) + local ghosts = surf.find_entities_filtered({ position = pos, radius = 10, name = { "entity-ghost" } }) + for i, ghost in ipairs(ghosts) do + game.get_player(pindex).mine_entity(ghost, true) cleared_count = cleared_count + 1 end - game.get_player(pindex).play_sound{path = "utility/item_deleted"} - rendering.draw_circle{color = {0, 1, 0}, radius = 10, width = 2, target = pos, surface = surf,time_to_live = 60} + game.get_player(pindex).play_sound({ path = "utility/item_deleted" }) + rendering.draw_circle({ + color = { 0, 1, 0 }, + radius = 10, + width = 2, + target = pos, + surface = surf, + time_to_live = 60, + }) printout(" Cleared away " .. cleared_count .. " entity ghosts within 10 tiles. ", pindex) return else --Check if it is a remnant ent, clear obstacles local ent_is_remnant = false local remnant_names = ENT_NAMES_CLEARED_AS_OBSTACLES - for i,name in ipairs(remnant_names) do - if ent.name == name then - ent_is_remnant = true - end + for i, name in ipairs(remnant_names) do + if ent.name == name then ent_is_remnant = true end end if ent_is_remnant then - game.get_player(pindex).play_sound{path = "player-mine"} + game.get_player(pindex).play_sound({ path = "player-mine" }) cleared_count, comment = fa_mining_tools.clear_obstacles_in_circle(players[pindex].cursor_pos, 5, pindex) end @@ -5163,24 +5432,23 @@ script.on_event("mine-area", function(event) end else --For empty tiles, clear obstacles - game.get_player(pindex).play_sound{path = "player-mine"} + game.get_player(pindex).play_sound({ path = "player-mine" }) cleared_count, comment = fa_mining_tools.clear_obstacles_in_circle(players[pindex].cursor_pos, 5, pindex) end cleared_total = cleared_total + cleared_count - --If cut-paste tool in hand, mine every non-resource entity in the area that you can. + --If cut-paste tool in hand, mine every non-resource entity in the area that you can. local p = game.get_player(pindex) local stack = p.cursor_stack if stack and stack.valid_for_read and stack.name == "cut-paste-tool" then players[pindex].allow_reading_flying_text = false - local all_ents = p.surface.find_entities_filtered{position = p.position, radius = 5, force = {p.force, "neutral"}} - for i,ent in ipairs(all_ents) do + local all_ents = + p.surface.find_entities_filtered({ position = p.position, radius = 5, force = { p.force, "neutral" } }) + for i, ent in ipairs(all_ents) do if ent and ent.valid then local name = ent.name - game.get_player(pindex).play_sound{path = "player-mine"} - if fa_mining_tools.try_to_mine_with_soun(ent,pindex) then - cleared_total = cleared_total + 1 - end + game.get_player(pindex).play_sound({ path = "player-mine" }) + if fa_mining_tools.try_to_mine_with_soun(ent, pindex) then cleared_total = cleared_total + 1 end end end end @@ -5188,14 +5456,13 @@ script.on_event("mine-area", function(event) --If the deconstruction planner is in hand, mine every entity marked for deconstruction except for cliffs. if stack and stack.valid_for_read and stack.is_deconstruction_item then players[pindex].allow_reading_flying_text = false - local all_ents = p.surface.find_entities_filtered{position = p.position, radius = 5, force = {p.force, "neutral"}} - for i,ent in ipairs(all_ents) do + local all_ents = + p.surface.find_entities_filtered({ position = p.position, radius = 5, force = { p.force, "neutral" } }) + for i, ent in ipairs(all_ents) do if ent and ent.valid and ent.is_registered_for_deconstruction(p.force) then local name = ent.name - game.get_player(pindex).play_sound{path = "player-mine"} - if fa_mining_tools.try_to_mine_with_soun(ent,pindex) then - cleared_total = cleared_total + 1 - end + game.get_player(pindex).play_sound({ path = "player-mine" }) + if fa_mining_tools.try_to_mine_with_soun(ent, pindex) then cleared_total = cleared_total + 1 end end end end @@ -5203,37 +5470,29 @@ script.on_event("mine-area", function(event) --Calculate collected stack count local stacks_collected = init_empty_stacks - game.get_player(pindex).get_main_inventory().count_empty_stacks() - --Print result + --Print result local result = " Cleared away " .. cleared_total .. " objects " - if stacks_collected >= 0 then - result = result .. " and collected " .. stacks_collected .. " new item stacks." - end + if stacks_collected >= 0 then result = result .. " and collected " .. stacks_collected .. " new item stacks." end printout(result, pindex) end) --Cut-paste-tool. NOTE: This keybind needs to be the same as that for the cut paste tool (default CONTROL + X). laterdo maybe keybind to game control somehow script.on_event("cut-paste-tool-comment", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local stack = game.get_player(pindex).cursor_stack if stack == nil then --(do nothing when the cut paste tool is not enabled) elseif stack and stack.valid_for_read and stack.name == "cut-paste-tool" then - printout("To disable this tool empty the hand, by pressing SHIFT + Q",pindex) + printout("To disable this tool empty the hand, by pressing SHIFT + Q", pindex) end end) --Right click actions in menus (click_menu) script.on_event("click-menu-right", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - if players[pindex].last_click_tick == event.tick then - return - end + if not check_for_player(pindex) then return end + if players[pindex].last_click_tick == event.tick then return end if players[pindex].in_menu then players[pindex].last_click_tick = event.tick if players[pindex].menu == "inventory" then @@ -5241,7 +5500,7 @@ script.on_event("click-menu-right", function(event) local p = game.get_player(pindex) local stack_cur = p.cursor_stack local stack_inv = table.deepcopy(players[pindex].inventory.lua_inventory[players[pindex].inventory.index]) - p.play_sound{path = "utility/inventory_click"} + p.play_sound({ path = "utility/inventory_click" }) if stack_inv and stack_inv.valid_for_read and (stack_inv.is_blueprint or stack_inv.is_blueprint_book) then --Do not grab it return @@ -5250,31 +5509,34 @@ script.on_event("click-menu-right", function(event) --Take half (sorted inventory) local name = stack_inv.name p.cursor_stack.swap_stack(players[pindex].inventory.lua_inventory[players[pindex].inventory.index]) - local bigger_half = math.ceil(p.cursor_stack.count/2) - local smaller_half = math.floor(p.cursor_stack.count/2) + local bigger_half = math.ceil(p.cursor_stack.count / 2) + local smaller_half = math.floor(p.cursor_stack.count / 2) p.cursor_stack.count = smaller_half - p.get_main_inventory().insert({name = name, count = bigger_half}) + p.get_main_inventory().insert({ name = name, count = bigger_half }) end players[pindex].inventory.max = #players[pindex].inventory.lua_inventory - - elseif (players[pindex].menu == "building" or players[pindex].menu == "vehicle") then + elseif players[pindex].menu == "building" or players[pindex].menu == "vehicle" then local sectors_i = players[pindex].building.sectors[players[pindex].building.sector] - if players[pindex].building.sector <= #players[pindex].building.sectors and #sectors_i.inventory > 0 and (sectors_i.name == "Output" or sectors_i.name == "Input" or sectors_i.name == "Fuel") then + if + players[pindex].building.sector <= #players[pindex].building.sectors + and #sectors_i.inventory > 0 + and (sectors_i.name == "Output" or sectors_i.name == "Input" or sectors_i.name == "Fuel") + then --Building invs: Take half** elseif players[pindex].building.recipe_list == nil or #players[pindex].building.recipe_list == 0 then --Player inventory: Take half local p = game.get_player(pindex) local stack_cur = p.cursor_stack local stack_inv = table.deepcopy(players[pindex].inventory.lua_inventory[players[pindex].inventory.index]) - p.play_sound{path = "utility/inventory_click"} + p.play_sound({ path = "utility/inventory_click" }) if not (stack_cur and stack_cur.valid_for_read) and (stack_inv and stack_inv.valid_for_read) then --Take half (sorted inventory) local name = stack_inv.name p.cursor_stack.swap_stack(players[pindex].inventory.lua_inventory[players[pindex].inventory.index]) - local bigger_half = math.ceil(p.cursor_stack.count/2) - local smaller_half = math.floor(p.cursor_stack.count/2) + local bigger_half = math.ceil(p.cursor_stack.count / 2) + local smaller_half = math.floor(p.cursor_stack.count / 2) p.cursor_stack.count = smaller_half - p.get_main_inventory().insert({name = name, count = bigger_half}) + p.get_main_inventory().insert({ name = name, count = bigger_half }) end players[pindex].inventory.max = #players[pindex].inventory.lua_inventory end @@ -5282,39 +5544,34 @@ script.on_event("click-menu-right", function(event) end end) -script.on_event("leftbracket-key-id", function(event) -end) +script.on_event("leftbracket-key-id", function(event) end) -script.on_event("rightbracket-key-id", function(event) -end) +script.on_event("rightbracket-key-id", function(event) end) --Left click actions in menus (click_menu) script.on_event("click-menu", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - if players[pindex].last_click_tick == event.tick then - return - end + if not check_for_player(pindex) then return end + if players[pindex].last_click_tick == event.tick then return end local p = game.get_player(pindex) if players[pindex].in_menu then players[pindex].last_click_tick = event.tick if players[pindex].menu == "inventory" then --Swap stacks - game.get_player(pindex).play_sound{path = "utility/inventory_click"} + game.get_player(pindex).play_sound({ path = "utility/inventory_click" }) local stack = players[pindex].inventory.lua_inventory[players[pindex].inventory.index] game.get_player(pindex).cursor_stack.swap_stack(stack) - players[pindex].inventory.max = #players[pindex].inventory.lua_inventory + players[pindex].inventory.max = #players[pindex].inventory.lua_inventory elseif players[pindex].menu == "player_trash" then local trash_inv = game.get_player(pindex).get_inventory(defines.inventory.character_trash) --Swap stacks - game.get_player(pindex).play_sound{path = "utility/inventory_click"} + game.get_player(pindex).play_sound({ path = "utility/inventory_click" }) local stack = trash_inv[players[pindex].inventory.index] game.get_player(pindex).cursor_stack.swap_stack(stack) elseif players[pindex].menu == "crafting" then --Check recipe category - local recipe = players[pindex].crafting.lua_recipes[players[pindex].crafting.category][players[pindex].crafting.index] + local recipe = + players[pindex].crafting.lua_recipes[players[pindex].crafting.category][players[pindex].crafting.index] if p.cheat_mode == false or (p.cheat_mode == true and recipe.subgroup == "fluid-recipes") then if recipe.category == "advanced-crafting" then printout("An assembling machine is required to craft this", pindex) @@ -5345,34 +5602,41 @@ script.on_event("click-menu", function(event) --Craft 1 local T = { count = 1, - recipe = players[pindex].crafting.lua_recipes[players[pindex].crafting.category][players[pindex].crafting.index], - silent = false + recipe = players[pindex].crafting.lua_recipes[players[pindex].crafting.category][players[pindex].crafting.index], + silent = false, } local count = game.get_player(pindex).begin_crafting(T) if count > 0 then local total_count = fa_crafting.count_in_crafting_queue(T.recipe.name, pindex) - printout("Started crafting " .. count .. " " .. fa_localising.get_recipe_from_name(recipe.name,pindex) .. ", " .. total_count .. " total in queue", pindex) + printout( + "Started crafting " + .. count + .. " " + .. fa_localising.get_recipe_from_name(recipe.name, pindex) + .. ", " + .. total_count + .. " total in queue", + pindex + ) else local result = fa_crafting.recipe_missing_ingredients_info(pindex) printout(result, pindex) end - elseif players[pindex].menu == "crafting_queue" then --Cancel 1 fa_crafting.load_crafting_queue(pindex) if players[pindex].crafting_queue.max >= 1 then local T = { - index = players[pindex].crafting_queue.index, - count = 1 + index = players[pindex].crafting_queue.index, + count = 1, } game.get_player(pindex).cancel_crafting(T) fa_crafting.load_crafting_queue(pindex) fa_crafting.read_crafting_queue(pindex, "cancelled 1, ") end - - elseif (players[pindex].menu == "building" or players[pindex].menu == "vehicle") then + elseif players[pindex].menu == "building" or players[pindex].menu == "vehicle" then local sectors_i = players[pindex].building.sectors[players[pindex].building.sector] - if players[pindex].building.sector <= #players[pindex].building.sectors and #sectors_i.inventory > 0 then + if players[pindex].building.sector <= #players[pindex].building.sectors and #sectors_i.inventory > 0 then if sectors_i.name == "Fluid" then --Do nothing return @@ -5392,25 +5656,37 @@ script.on_event("click-menu", function(event) else players[pindex].building.ent.inserter_filter_mode = "whitelist" end - sectors_i.inventory[players[pindex].building.index] = players[pindex].building.ent.inserter_filter_mode - fa_sectors.read_sector_slot(pindex,false) + sectors_i.inventory[players[pindex].building.index] = + players[pindex].building.ent.inserter_filter_mode + fa_sectors.read_sector_slot(pindex, false) elseif players[pindex].building.item_selection then if players[pindex].item_selector.group == 0 then players[pindex].item_selector.group = players[pindex].item_selector.index - players[pindex].item_cache = fa_utils.get_iterable_array(players[pindex].item_cache[players[pindex].item_selector.group].subgroups) + players[pindex].item_cache = fa_utils.get_iterable_array( + players[pindex].item_cache[players[pindex].item_selector.group].subgroups + ) prune_item_groups(players[pindex].item_cache) players[pindex].item_selector.index = 1 read_item_selector_slot(pindex) elseif players[pindex].item_selector.subgroup == 0 then players[pindex].item_selector.subgroup = players[pindex].item_selector.index - local prototypes = game.get_filtered_item_prototypes{{filter="subgroup",subgroup = players[pindex].item_cache[players[pindex].item_selector.index].name}} + local prototypes = game.get_filtered_item_prototypes({ + { + filter = "subgroup", + subgroup = players[pindex].item_cache[players[pindex].item_selector.index].name, + }, + }) players[pindex].item_cache = fa_utils.get_iterable_array(prototypes) players[pindex].item_selector.index = 1 read_item_selector_slot(pindex) else - players[pindex].building.ent.set_filter(players[pindex].building.index, players[pindex].item_cache[players[pindex].item_selector.index].name) - sectors_i.inventory[players[pindex].building.index] = players[pindex].building.ent.get_filter(players[pindex].building.index) + players[pindex].building.ent.set_filter( + players[pindex].building.index, + players[pindex].item_cache[players[pindex].item_selector.index].name + ) + sectors_i.inventory[players[pindex].building.index] = + players[pindex].building.ent.get_filter(players[pindex].building.index) printout("Filter set.", pindex) players[pindex].building.item_selection = false players[pindex].item_selection = false @@ -5419,10 +5695,10 @@ script.on_event("click-menu", function(event) players[pindex].item_selector.group = 0 players[pindex].item_selector.subgroup = 0 players[pindex].item_selector.index = 1 - players[pindex].item_selection = true + players[pindex].item_selection = true players[pindex].building.item_selection = true players[pindex].item_cache = fa_utils.get_iterable_array(game.item_group_prototypes) - prune_item_groups(players[pindex].item_cache) + prune_item_groups(players[pindex].item_cache) read_item_selector_slot(pindex) end return @@ -5435,16 +5711,21 @@ script.on_event("click-menu", function(event) stack.transfer_stack(cursor_stack) cursor_stack = game.get_player(pindex).cursor_stack if sectors_i.name == "Modules" and cursor_stack.is_module then - printout(" Only one module can be added per module slot " , pindex) + printout(" Only one module can be added per module slot ", pindex) elseif cursor_stack.valid_for_read then - printout(" Adding to stack of " .. cursor_stack.name , pindex) + printout(" Adding to stack of " .. cursor_stack.name, pindex) else - printout(" Added" , pindex) + printout(" Added", pindex) end return end --Special case for filling module slots - if sectors_i.name == "Modules" and cursor_stack ~= nil and cursor_stack.valid_for_read and cursor_stack.is_module then + if + sectors_i.name == "Modules" + and cursor_stack ~= nil + and cursor_stack.valid_for_read + and cursor_stack.is_module + then local p_inv = game.get_player(pindex).get_main_inventory() local result = "" if stack.valid_for_read and stack.count > 0 then @@ -5460,7 +5741,8 @@ script.on_event("click-menu", function(event) stack = sectors_i.inventory[players[pindex].building.index] if (stack == nil or stack.count == 0) and sectors_i.inventory.can_insert(cursor_stack) then local module_name = cursor_stack.name - local successful = sectors_i.inventory[players[pindex].building.index].set_stack({name = module_name, count = 1}) + local successful = + sectors_i.inventory[players[pindex].building.index].set_stack({ name = module_name, count = 1 }) if not successful then printout(" Failed to add module ", pindex) return @@ -5475,54 +5757,63 @@ script.on_event("click-menu", function(event) end --Try to swap stacks and report if there is an error if cursor_stack.swap_stack(stack) then - game.get_player(pindex).play_sound{path = "utility/inventory_click"} --- read_building_slot(pindex,false) + game.get_player(pindex).play_sound({ path = "utility/inventory_click" }) + -- read_building_slot(pindex,false) else local name = "This item" - if (stack == nil or not stack.valid_for_read) and (cursor_stack == nil or not cursor_stack.valid_for_read) then + if + (stack == nil or not stack.valid_for_read) + and (cursor_stack == nil or not cursor_stack.valid_for_read) + then printout("Empty", pindex) return end - if cursor_stack.valid_for_read then - name = cursor_stack.name - end + if cursor_stack.valid_for_read then name = cursor_stack.name end printout("Cannot insert " .. name .. " in this slot", pindex) end elseif players[pindex].building.recipe_list == nil then --Player inventory: Swap stack - game.get_player(pindex).play_sound{path = "utility/inventory_click"} + game.get_player(pindex).play_sound({ path = "utility/inventory_click" }) local stack = players[pindex].inventory.lua_inventory[players[pindex].inventory.index] game.get_player(pindex).cursor_stack.swap_stack(stack) players[pindex].inventory.max = #players[pindex].inventory.lua_inventory --- read_inventory_slot(pindex) + -- read_inventory_slot(pindex) else if players[pindex].building.sector == #players[pindex].building.sectors + 1 then --Building recipe selection if players[pindex].building.recipe_selection then - if not(pcall(function() - local there_was_a_recipe_before = false - players[pindex].building.recipe = players[pindex].building.recipe_list[players[pindex].building.category][players[pindex].building.index] - if players[pindex].building.ent.valid then - there_was_a_recipe_before = (players[pindex].building.ent.get_recipe() ~= nil) - players[pindex].building.ent.set_recipe(players[pindex].building.recipe) - end - players[pindex].building.recipe_selection = false - players[pindex].building.index = 1 - printout("Selected", pindex) - game.get_player(pindex).play_sound{path = "utility/inventory_click"} - --Open GUI if not already - local p = game.get_player(pindex) - if there_was_a_recipe_before == false and players[pindex].building.ent.valid then - --Refresh the GUI --**laterdo figure this out, closing and opening in the same tick does not work. - --players[pindex].refreshing_building_gui = true - --p.opened = nil - --p.opened = players[pindex].building.ent - --players[pindex].refreshing_building_gui = false - end - end)) then - printout("For this building, recipes are selected automatically based on the input item, this menu is for information only.", pindex) + if + not ( + pcall(function() + local there_was_a_recipe_before = false + players[pindex].building.recipe = + players[pindex].building.recipe_list[players[pindex].building.category][players[pindex].building.index] + if players[pindex].building.ent.valid then + there_was_a_recipe_before = (players[pindex].building.ent.get_recipe() ~= nil) + players[pindex].building.ent.set_recipe(players[pindex].building.recipe) + end + players[pindex].building.recipe_selection = false + players[pindex].building.index = 1 + printout("Selected", pindex) + game.get_player(pindex).play_sound({ path = "utility/inventory_click" }) + --Open GUI if not already + local p = game.get_player(pindex) + if there_was_a_recipe_before == false and players[pindex].building.ent.valid then + --Refresh the GUI --**laterdo figure this out, closing and opening in the same tick does not work. + --players[pindex].refreshing_building_gui = true + --p.opened = nil + --p.opened = players[pindex].building.ent + --players[pindex].refreshing_building_gui = false + end + end) + ) + then + printout( + "For this building, recipes are selected automatically based on the input item, this menu is for information only.", + pindex + ) end elseif #players[pindex].building.recipe_list > 0 then - game.get_player(pindex).play_sound{path = "utility/inventory_click"} + game.get_player(pindex).play_sound({ path = "utility/inventory_click" }) players[pindex].building.recipe_selection = true players[pindex].building.category = 1 players[pindex].building.index = 1 @@ -5532,16 +5823,14 @@ script.on_event("click-menu", function(event) end else --Player inventory again: swap stack - game.get_player(pindex).play_sound{path = "utility/inventory_click"} + game.get_player(pindex).play_sound({ path = "utility/inventory_click" }) local stack = players[pindex].inventory.lua_inventory[players[pindex].inventory.index] game.get_player(pindex).cursor_stack.swap_stack(stack) - players[pindex].inventory.max = #players[pindex].inventory.lua_inventory ----- read_inventory_slot(pindex) + players[pindex].inventory.max = #players[pindex].inventory.lua_inventory + ---- read_inventory_slot(pindex) end - end - elseif players[pindex].menu == "technology" then local techs = {} if players[pindex].technology.category == 1 then @@ -5552,25 +5841,27 @@ script.on_event("click-menu", function(event) techs = players[pindex].technology.lua_unlocked end - if next(techs) ~= nil and players[pindex].technology.index > 0 and players[pindex].technology.index <= #techs then + if + next(techs) ~= nil + and players[pindex].technology.index > 0 + and players[pindex].technology.index <= #techs + then if game.get_player(pindex).force.add_research(techs[players[pindex].technology.index]) then printout("Research started.", pindex) else printout("Research locked, first complete the prerequisites.", pindex) end end - elseif players[pindex].menu == "pump" then if players[pindex].pump.index == 0 then printout("Move up and down to select a location.", pindex) return end local entry = players[pindex].pump.positions[players[pindex].pump.index] - game.get_player(pindex).build_from_cursor{position = entry.position, direction = entry.direction} + game.get_player(pindex).build_from_cursor({ position = entry.position, direction = entry.direction }) players[pindex].in_menu = false players[pindex].menu = "none" printout("Pump placed.", pindex) - elseif players[pindex].menu == "warnings" then local warnings = {} if players[pindex].warnings.sector == 1 then @@ -5578,29 +5869,37 @@ script.on_event("click-menu", function(event) elseif players[pindex].warnings.sector == 2 then warnings = players[pindex].warnings.medium.warnings elseif players[pindex].warnings.sector == 3 then - warnings= players[pindex].warnings.long.warnings + warnings = players[pindex].warnings.long.warnings end - if players[pindex].warnings.category <= #warnings and players[pindex].warnings.index <= #warnings[players[pindex].warnings.category].ents then + if + players[pindex].warnings.category <= #warnings + and players[pindex].warnings.index <= #warnings[players[pindex].warnings.category].ents + then local ent = warnings[players[pindex].warnings.category].ents[players[pindex].warnings.index] if ent ~= nil and ent.valid then players[pindex].cursor = true players[pindex].cursor_pos = fa_utils.center_of_tile(ent.position) fa_graphics.draw_cursor_highlight(pindex, ent, nil) fa_graphics.sync_build_cursor_graphics(pindex) - printout({"access.teleported-cursor-to", "".. math.floor(players[pindex].cursor_pos.x) .. " " .. math.floor(players[pindex].cursor_pos.y)}, pindex) --- players[pindex].menu = "" --- players[pindex].in_menu = false + printout({ + "access.teleported-cursor-to", + "" .. math.floor(players[pindex].cursor_pos.x) .. " " .. math.floor(players[pindex].cursor_pos.y), + }, pindex) + -- players[pindex].menu = "" + -- players[pindex].in_menu = false else printout("Blank", pindex) end else - printout("No warnings for this range. Press tab to pick a larger range, or press E to close this menu.", pindex) + printout( + "No warnings for this range. Press tab to pick a larger range, or press E to close this menu.", + pindex + ) end - elseif players[pindex].menu == "travel" then fa_travel.fast_travel_menu_click(pindex) - elseif players[pindex].menu == "structure-travel" then--Also called "b stride" - ---@type LuaEntity + elseif players[pindex].menu == "structure-travel" then --Also called "b stride" + ---@type LuaEntity local tar = nil local network = players[pindex].structure_travel.network local index = players[pindex].structure_travel.index @@ -5620,7 +5919,8 @@ script.on_event("click-menu", function(event) if success and players[pindex].cursor then players[pindex].cursor_pos = table.deepcopy(tar.position) else - players[pindex].cursor_pos = fa_utils.offset_position(players[pindex].position, players[pindex].player_direction, 1) + players[pindex].cursor_pos = + fa_utils.offset_position(players[pindex].position, players[pindex].player_direction, 1) end fa_graphics.sync_build_cursor_graphics(pindex) game.get_player(pindex).opened = nil @@ -5637,14 +5937,18 @@ script.on_event("click-menu", function(event) else fa_graphics.draw_cursor_highlight(pindex, nil, nil) end - elseif players[pindex].menu == "rail_builder" then fa_rail_builder.run_menu(pindex, true) - fa_rail_builder.close_menu(pindex,false) + fa_rail_builder.close_menu(pindex, false) elseif players[pindex].menu == "train_menu" then fa_trains.run_train_menu(players[pindex].train_menu.index, pindex, true) elseif players[pindex].menu == "spider_menu" then - fa_spidertrons.run_spider_menu(players[pindex].spider_menu.index, pindex, game.get_player(pindex).cursor_stack, true) + fa_spidertrons.run_spider_menu( + players[pindex].spider_menu.index, + pindex, + game.get_player(pindex).cursor_stack, + true + ) elseif players[pindex].menu == "train_stop_menu" then fa_train_stops.run_train_stop_menu(players[pindex].train_stop_menu.index, pindex, true) elseif players[pindex].menu == "roboport_menu" then @@ -5657,14 +5961,18 @@ script.on_event("click-menu", function(event) elseif players[pindex].menu == "circuit_network_menu" then circuit_network_menu(pindex, nil, players[pindex].circuit_network_menu.index, true, false) elseif players[pindex].menu == "signal_selector" then - apply_selected_signal_to_enabled_condition(pindex, players[pindex].signal_selector.ent, players[pindex].signal_selector.editing_first_slot) + apply_selected_signal_to_enabled_condition( + pindex, + players[pindex].signal_selector.ent, + players[pindex].signal_selector.editing_first_slot + ) end end end) --Different behavior when you click on an inventory slot depending on the item in hand and the item in the slot (WIP) function player_inventory_click(pindex, left_click) - --****todo finish this to include all interaction cases, then generalize it to building inventories . + --****todo finish this to include all interaction cases, then generalize it to building inventories . --Use code from above and then replace above clutter with calls to this. --Use stack.transfer_stack(other_stack) local click_is_left = left_click or true @@ -5677,28 +5985,21 @@ function player_inventory_click(pindex, left_click) if stack_inv and stack_inv.valid_for_read and stack_inv.name ~= stack_cur.name then else end - else --Empty hand - end --Play sound and update known inv size - p.play_sound{path = "utility/inventory_click"} + p.play_sound({ path = "utility/inventory_click" }) players[pindex].inventory.max = #players[pindex].inventory.lua_inventory - end --Left click actions with items in hand script.on_event("click-hand", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local p = game.get_player(pindex) - if players[pindex].last_click_tick == event.tick then - return - end + if players[pindex].last_click_tick == event.tick then return end if players[pindex].in_menu then return else @@ -5717,8 +6018,12 @@ script.on_event("click-hand", function(event) return end - --If something is in hand... - if stack.prototype ~= nil and (stack.prototype.place_result ~= nil or stack.prototype.place_as_tile_result ~= nil) and stack.name ~= "offshore-pump" then + --If something is in hand... + if + stack.prototype ~= nil + and (stack.prototype.place_result ~= nil or stack.prototype.place_as_tile_result ~= nil) + and stack.name ~= "offshore-pump" + then --If holding a preview of a building/tile, try to place it here fa_building_tools.build_item_in_hand(pindex) elseif stack.name == "offshore-pump" then @@ -5729,18 +6034,23 @@ script.on_event("click-hand", function(event) fa_spidertrons.run_spider_menu(3, pindex, stack.connected_entity, true, nil) elseif stack.is_repair_tool then --If holding a repair pack, try to use it (will not work on enemies) - fa_combat.repair_pack_used(ent,pindex) + fa_combat.repair_pack_used(ent, pindex) elseif stack.is_blueprint and stack.is_blueprint_setup() and players[pindex].blueprint_reselecting ~= true then - --Paste a ready blueprint + --Paste a ready blueprint players[pindex].last_held_blueprint = stack fa_blueprints.paste_blueprint(pindex) - elseif stack.is_blueprint and (stack.is_blueprint_setup() == false or players[pindex].blueprint_reselecting == true) then - --Select blueprint + elseif + stack.is_blueprint and (stack.is_blueprint_setup() == false or players[pindex].blueprint_reselecting == true) + then + --Select blueprint local pex = players[pindex] if pex.bp_selecting ~= true then pex.bp_selecting = true pex.bp_select_point_1 = pex.cursor_pos - printout("Started blueprint selection at " .. math.floor(pex.cursor_pos.x) .. "," .. math.floor(pex.cursor_pos.y) , pindex) + printout( + "Started blueprint selection at " .. math.floor(pex.cursor_pos.x) .. "," .. math.floor(pex.cursor_pos.y), + pindex + ) else pex.bp_selecting = false pex.bp_select_point_2 = pex.cursor_pos @@ -5759,41 +6069,58 @@ script.on_event("click-hand", function(event) if pex.bp_selecting ~= true then pex.bp_selecting = true pex.bp_select_point_1 = pex.cursor_pos - printout("Started deconstruction selection at " .. math.floor(pex.cursor_pos.x) .. "," .. math.floor(pex.cursor_pos.y) , pindex) + printout( + "Started deconstruction selection at " + .. math.floor(pex.cursor_pos.x) + .. "," + .. math.floor(pex.cursor_pos.y), + pindex + ) else pex.bp_selecting = false pex.bp_select_point_2 = pex.cursor_pos --Mark area for deconstruction - local left_top, right_bottom = fa_utils.get_top_left_and_bottom_right(pex.bp_select_point_1, pex.bp_select_point_2) - p.surface.deconstruct_area{area={left_top, right_bottom}, force=p.force, player=p, item=p.cursor_stack} - local ents = p.surface.find_entities_filtered{area={left_top, right_bottom}} + local left_top, right_bottom = + fa_utils.get_top_left_and_bottom_right(pex.bp_select_point_1, pex.bp_select_point_2) + p.surface.deconstruct_area({ + area = { left_top, right_bottom }, + force = p.force, + player = p, + item = p.cursor_stack, + }) + local ents = p.surface.find_entities_filtered({ area = { left_top, right_bottom } }) local decon_counter = 0 for i, ent in ipairs(ents) do - if ent.valid and ent.to_be_deconstructed() then - decon_counter = decon_counter + 1 - end + if ent.valid and ent.to_be_deconstructed() then decon_counter = decon_counter + 1 end end printout(decon_counter .. " entities marked to be deconstructed.", pindex) end elseif stack.is_upgrade_item then - --Mark upgrade + --Mark upgrade local pex = players[pindex] if pex.bp_selecting ~= true then pex.bp_selecting = true pex.bp_select_point_1 = pex.cursor_pos - printout("Started upgrading selection at " .. math.floor(pex.cursor_pos.x) .. "," .. math.floor(pex.cursor_pos.y) , pindex) + printout( + "Started upgrading selection at " .. math.floor(pex.cursor_pos.x) .. "," .. math.floor(pex.cursor_pos.y), + pindex + ) else pex.bp_selecting = false pex.bp_select_point_2 = pex.cursor_pos --Mark area for upgrading - local left_top, right_bottom = fa_utils.get_top_left_and_bottom_right(pex.bp_select_point_1, pex.bp_select_point_2) - p.surface.upgrade_area{area={left_top, right_bottom}, force=p.force, player=p, item=p.cursor_stack} - local ents = p.surface.find_entities_filtered{area={left_top, right_bottom}} + local left_top, right_bottom = + fa_utils.get_top_left_and_bottom_right(pex.bp_select_point_1, pex.bp_select_point_2) + p.surface.upgrade_area({ + area = { left_top, right_bottom }, + force = p.force, + player = p, + item = p.cursor_stack, + }) + local ents = p.surface.find_entities_filtered({ area = { left_top, right_bottom } }) local ent_counter = 0 for i, ent in ipairs(ents) do - if ent.valid and ent.to_be_upgraded() then - ent_counter = ent_counter + 1 - end + if ent.valid and ent.to_be_upgraded() then ent_counter = ent_counter + 1 end end printout(ent_counter .. " entities marked to be upgraded.", pindex) end @@ -5801,7 +6128,7 @@ script.on_event("click-hand", function(event) drag_wire_and_read(pindex) elseif stack.prototype ~= nil and stack.prototype.type == "capsule" then --If holding a capsule type, e.g. cliff explosives or robot capsules, or remotes, try to use it at the cursor position (no feedback about successful usage) - local cursor_dist = util.distance(game.get_player(pindex).position,players[pindex].cursor_pos) + local cursor_dist = util.distance(game.get_player(pindex).position, players[pindex].cursor_pos) local range = 20 if stack.name == "cliff-explosives" then range = 10 @@ -5811,21 +6138,19 @@ script.on_event("click-hand", function(event) if stack.name == "artillery-targeting-remote" then game.get_player(pindex).use_from_cursor(players[pindex].cursor_pos) --Play sound **laterdo better sound - game.get_player(pindex).play_sound{path = "Close-Inventory-Sound"} - if cursor_dist < 7 then - printout("Warning, you are in the target area!",pindex) - end + game.get_player(pindex).play_sound({ path = "Close-Inventory-Sound" }) + if cursor_dist < 7 then printout("Warning, you are in the target area!", pindex) end elseif cursor_dist < range then game.get_player(pindex).use_from_cursor(players[pindex].cursor_pos) else - game.get_player(pindex).play_sound{path = "utility/cannot_build"} - printout("Target is out of range",pindex) + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) + printout("Target is out of range", pindex) end elseif ent ~= nil then --If holding an item with no special left click actions, allow entity left click actions. - clicked_on_entity(ent,pindex) + clicked_on_entity(ent, pindex) else - printout("No actions for " .. stack.name .. " in hand",pindex) + printout("No actions for " .. stack.name .. " in hand", pindex) end end end) @@ -5833,13 +6158,9 @@ end) --Right click actions with items in hand script.on_event("click-hand-right", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local p = game.get_player(pindex) - if players[pindex].last_click_tick == event.tick then - return - end + if players[pindex].last_click_tick == event.tick then return end if players[pindex].in_menu then return else @@ -5853,26 +6174,42 @@ script.on_event("click-hand-right", function(event) return end - --If something is in hand... - if stack.prototype ~= nil and (stack.prototype.place_result ~= nil or stack.prototype.place_as_tile_result ~= nil) and stack.name ~= "offshore-pump" then - --Laterdo here: build as ghost + --If something is in hand... + if + stack.prototype ~= nil + and (stack.prototype.place_result ~= nil or stack.prototype.place_as_tile_result ~= nil) + and stack.name ~= "offshore-pump" + then + --Laterdo here: build as ghost elseif stack.is_blueprint then fa_blueprints.blueprint_menu_open(pindex) elseif stack.is_blueprint_book then fa_blueprints.blueprint_book_menu_open(pindex, false) elseif stack.is_deconstruction_item then - --Cancel deconstruction + --Cancel deconstruction local pex = players[pindex] if pex.bp_selecting ~= true then pex.bp_selecting = true pex.bp_select_point_1 = pex.cursor_pos - printout("Started deconstruction selection at " .. math.floor(pex.cursor_pos.x) .. "," .. math.floor(pex.cursor_pos.y) , pindex) + printout( + "Started deconstruction selection at " + .. math.floor(pex.cursor_pos.x) + .. "," + .. math.floor(pex.cursor_pos.y), + pindex + ) else pex.bp_selecting = false pex.bp_select_point_2 = pex.cursor_pos --Cancel area for deconstruction - local left_top, right_bottom = fa_utils.get_top_left_and_bottom_right(pex.bp_select_point_1, pex.bp_select_point_2) - p.surface.cancel_deconstruct_area{area={left_top, right_bottom}, force=p.force, player=p, item=p.cursor_stack} + local left_top, right_bottom = + fa_utils.get_top_left_and_bottom_right(pex.bp_select_point_1, pex.bp_select_point_2) + p.surface.cancel_deconstruct_area({ + area = { left_top, right_bottom }, + force = p.force, + player = p, + item = p.cursor_stack, + }) printout("Canceled deconstruction in selected area", pindex) end elseif stack.is_upgrade_item then @@ -5880,13 +6217,22 @@ script.on_event("click-hand-right", function(event) if pex.bp_selecting ~= true then pex.bp_selecting = true pex.bp_select_point_1 = pex.cursor_pos - printout("Started upgrading selection at " .. math.floor(pex.cursor_pos.x) .. "," .. math.floor(pex.cursor_pos.y) , pindex) + printout( + "Started upgrading selection at " .. math.floor(pex.cursor_pos.x) .. "," .. math.floor(pex.cursor_pos.y), + pindex + ) else pex.bp_selecting = false pex.bp_select_point_2 = pex.cursor_pos --Cancel area for upgrading - local left_top, right_bottom = fa_utils.get_top_left_and_bottom_right(pex.bp_select_point_1, pex.bp_select_point_2) - p.surface.cancel_upgrade_area{area={left_top, right_bottom}, force=p.force, player=p, item=p.cursor_stack} + local left_top, right_bottom = + fa_utils.get_top_left_and_bottom_right(pex.bp_select_point_1, pex.bp_select_point_2) + p.surface.cancel_upgrade_area({ + area = { left_top, right_bottom }, + force = p.force, + player = p, + item = p.cursor_stack, + }) printout("Canceled upgrading in selected area", pindex) end elseif stack.name == "spidertron-remote" then @@ -5899,15 +6245,9 @@ end) --Left click actions with no menu and no items in hand script.on_event("click-entity", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - if players[pindex].last_click_tick == event.tick then - return - end - if players[pindex].vanilla_mode == true then - return - end + if not check_for_player(pindex) then return end + if players[pindex].last_click_tick == event.tick then return end + if players[pindex].vanilla_mode == true then return end if players[pindex].in_menu then return else @@ -5923,25 +6263,25 @@ script.on_event("click-entity", function(event) end --If the hand is empty... - clicked_on_entity(ent,pindex) + clicked_on_entity(ent, pindex) end end) -function clicked_on_entity(ent,pindex) +function clicked_on_entity(ent, pindex) local p = game.get_player(pindex) if p.vehicle ~= nil and p.vehicle.train ~= nil then --If player is on a train, open it fa_trains.menu_open(pindex) return elseif ent == nil then - --No entity clicked + --No entity clicked p.selected = nil return elseif not ent.valid then --Invalid entity clicked - p.print("Invalid entity clicked",{volume_modifier=0}) + p.print("Invalid entity clicked", { volume_modifier = 0 }) if p.opened ~= nil and p.opened.object_name == "LuaEntity" and p.opened.valid then - p.print("Opened " .. p.opened.name,{volume_modifier=0}) + p.print("Opened " .. p.opened.name, { volume_modifier = 0 }) ent = p.opened return else @@ -5962,69 +6302,68 @@ function clicked_on_entity(ent,pindex) --For a train stop, open train stop menu fa_train_stops.train_stop_menu_open(pindex) elseif ent.name == "roboport" then - --For a roboport, open roboport menu + --For a roboport, open roboport menu fa_bot_logistics.roboport_menu_open(pindex) elseif ent.type == "power-switch" then - --Toggle it, if in manual mode + --Toggle it, if in manual mode if (#ent.neighbours.red + #ent.neighbours.green) > 0 then - printout("observes circuit condition",pindex) + printout("observes circuit condition", pindex) else ent.power_switch_state = not ent.power_switch_state if ent.power_switch_state == true then - printout("Switched on",pindex) + printout("Switched on", pindex) elseif ent.power_switch_state == false then - printout("Switched off",pindex) + printout("Switched off", pindex) end end elseif ent.type == "constant-combinator" then - --Toggle it - ent.get_control_behavior().enabled = not (ent.get_control_behavior().enabled) + --Toggle it + ent.get_control_behavior().enabled = not ent.get_control_behavior().enabled local enabled = ent.get_control_behavior().enabled if enabled == true then - printout("Switched on",pindex) + printout("Switched on", pindex) elseif enabled == false then - printout("Switched off",pindex) + printout("Switched off", pindex) end elseif ent.operable and ent.prototype.is_building then --If checking an operable building, open its menu - fa_sectors.open_operable_building(ent,pindex) + fa_sectors.open_operable_building(ent, pindex) elseif ent.type == "car" or ent.type == "spider-vehicle" or ent.train ~= nil then - fa_sectors.open_operable_vehicle(ent,pindex) + fa_sectors.open_operable_vehicle(ent, pindex) elseif ent.type == "spider-leg" then --Find and open the spider - local spiders = ent.surface.find_entities_filtered{position = ent.position, radius = 5, type = "spider-vehicle"} - local spider = ent.surface.get_closest(ent.position, spiders) - if spider and spider.valid then - fa_sectors.open_operable_vehicle(spider,pindex) - end + local spiders = + ent.surface.find_entities_filtered({ position = ent.position, radius = 5, type = "spider-vehicle" }) + local spider = ent.surface.get_closest(ent.position, spiders) + if spider and spider.valid then fa_sectors.open_operable_vehicle(spider, pindex) end elseif ent.name == "rocket-silo-rocket-shadow" or ent.name == "rocket-silo-rocket" then --Find and open the silo - local silos = ent.surface.find_entities_filtered{position = ent.position, radius = 5, type = "rocket-silo"} - local silo = ent.surface.get_closest(ent.position, silos) - if silo and silo.valid then - fa_sectors.open_operable_building(silo,pindex) - end + local silos = ent.surface.find_entities_filtered({ position = ent.position, radius = 5, type = "rocket-silo" }) + local silo = ent.surface.get_closest(ent.position, silos) + if silo and silo.valid then fa_sectors.open_operable_building(silo, pindex) end elseif ent.operable then - printout("No menu for " .. ent.name,pindex) + printout("No menu for " .. ent.name, pindex) elseif ent.type == "resource" and ent.name ~= "crude-oil" and ent.name ~= "uranium-ore" then - printout("No menu for " .. ent.name .. " but it can be mined by hand." ,pindex) + printout("No menu for " .. ent.name .. " but it can be mined by hand.", pindex) else - printout("No menu for " .. ent.name,pindex) + printout("No menu for " .. ent.name, pindex) end end --For a building, opens circuit menu script.on_event("open-circuit-menu", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local p = game.get_player(pindex) --In a building menu - if players[pindex].menu == "building" or players[pindex].menu == "building_no_sectors" or players[pindex].menu == "belt" then + if + players[pindex].menu == "building" + or players[pindex].menu == "building_no_sectors" + or players[pindex].menu == "belt" + then local ent = p.opened if ent == nil or ent.valid == false then - printout("Error: Missing building interface",pindex) + printout("Error: Missing building interface", pindex) return end if ent.type == "electric-pole" then @@ -6041,14 +6380,14 @@ script.on_event("open-circuit-menu", function(event) --Building has control behavior local control = ent.get_control_behavior() if control == nil then - printout("No control behavior for this building",pindex) + printout("No control behavior for this building", pindex) return end --Building has a circuit network local nw1 = control.get_circuit_network(defines.wire_type.red) local nw2 = control.get_circuit_network(defines.wire_type.green) if nw1 == nil and nw2 == nil then - printout(" not connected to a circuit network",pindex) + printout(" not connected to a circuit network", pindex) return end --Open the menu @@ -6076,7 +6415,7 @@ script.on_event("open-circuit-menu", function(event) local nw1 = control.get_circuit_network(defines.wire_type.red) local nw2 = control.get_circuit_network(defines.wire_type.green) if nw1 == nil and nw2 == nil then - printout(fa_localising.get(ent,pindex) .. " not connected to a circuit network",pindex) + printout(fa_localising.get(ent, pindex) .. " not connected to a circuit network", pindex) return end --Open the menu @@ -6086,12 +6425,8 @@ end) script.on_event("repair-area", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - if players[pindex].last_click_tick == event.tick then - return - end + if not check_for_player(pindex) then return end + if players[pindex].last_click_tick == event.tick then return end if players[pindex].in_menu then return else @@ -6105,47 +6440,52 @@ script.on_event("repair-area", function(event) return end - --If something is in hand... + --If something is in hand... if stack.is_repair_tool then --If holding a repair pack - fa_combat.repair_area(math.ceil(game.get_player(pindex).reach_distance),pindex) + fa_combat.repair_area(math.ceil(game.get_player(pindex).reach_distance), pindex) end end end) - script.on_event("crafting-all", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end if players[pindex].in_menu then if players[pindex].menu == "crafting" then - local recipe = players[pindex].crafting.lua_recipes[players[pindex].crafting.category][players[pindex].crafting.index] + local recipe = + players[pindex].crafting.lua_recipes[players[pindex].crafting.category][players[pindex].crafting.index] local T = { count = game.get_player(pindex).get_craftable_count(recipe), - recipe = players[pindex].crafting.lua_recipes[players[pindex].crafting.category][players[pindex].crafting.index], - silent = false + recipe = players[pindex].crafting.lua_recipes[players[pindex].crafting.category][players[pindex].crafting.index], + silent = false, } local count = game.get_player(pindex).begin_crafting(T) if count > 0 then local total_count = fa_crafting.count_in_crafting_queue(T.recipe.name, pindex) - printout("Started crafting " .. count .. " " .. fa_localising.get_recipe_from_name(recipe.name,pindex) .. ", " .. total_count .. " total in queue", pindex) + printout( + "Started crafting " + .. count + .. " " + .. fa_localising.get_recipe_from_name(recipe.name, pindex) + .. ", " + .. total_count + .. " total in queue", + pindex + ) else printout("Not enough materials", pindex) end - elseif players[pindex].menu == "crafting_queue" then fa_crafting.load_crafting_queue(pindex) if players[pindex].crafting_queue.max >= 1 then local T = { - index = players[pindex].crafting_queue.index, - count = players[pindex].crafting_queue.lua_queue[players[pindex].crafting_queue.index].count + index = players[pindex].crafting_queue.index, + count = players[pindex].crafting_queue.lua_queue[players[pindex].crafting_queue.index].count, } game.get_player(pindex).cancel_crafting(T) fa_crafting.load_crafting_queue(pindex) fa_crafting.read_crafting_queue(pindex, "cancelled all, ") - end end end @@ -6154,55 +6494,67 @@ end) --Transfers a stack from one inventory to another. Preserves BP data. script.on_event("transfer-one-stack", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end if players[pindex].in_menu then - if (players[pindex].menu == "building" or players[pindex].menu == "vehicle") then - if players[pindex].building.sector <= #players[pindex].building.sectors and #players[pindex].building.sectors[players[pindex].building.sector].inventory > 0 and players[pindex].building.sectors[players[pindex].building.sector].name ~= "Fluid" then + if players[pindex].menu == "building" or players[pindex].menu == "vehicle" then + if + players[pindex].building.sector <= #players[pindex].building.sectors + and #players[pindex].building.sectors[players[pindex].building.sector].inventory > 0 + and players[pindex].building.sectors[players[pindex].building.sector].name ~= "Fluid" + then --Transfer stack from building to player inventory - local stack = players[pindex].building.sectors[players[pindex].building.sector].inventory[players[pindex].building.index] + local stack = + players[pindex].building.sectors[players[pindex].building.sector].inventory[players[pindex].building.index] if stack and stack.valid and stack.valid_for_read then - if players[pindex].menu == "vehicle" and game.get_player(pindex).opened.type == "spider-vehicle" and stack.prototype.place_as_equipment_result ~= nil then + if + players[pindex].menu == "vehicle" + and game.get_player(pindex).opened.type == "spider-vehicle" + and stack.prototype.place_as_equipment_result ~= nil + then return end if game.get_player(pindex).can_insert(stack) then - game.get_player(pindex).play_sound{path = "utility/inventory_move"} + game.get_player(pindex).play_sound({ path = "utility/inventory_move" }) local result = stack.name local inserted = game.get_player(pindex).insert(stack) - players[pindex].building.sectors[players[pindex].building.sector].inventory.remove{name = stack.name, count = inserted} - result = "Moved " .. inserted .. " " .. result .. " to player's inventory."--**laterdo note that ammo gets inserted to ammo slots first + players[pindex].building.sectors[players[pindex].building.sector].inventory.remove({ + name = stack.name, + count = inserted, + }) + result = "Moved " .. inserted .. " " .. result .. " to player's inventory." --**laterdo note that ammo gets inserted to ammo slots first printout(result, pindex) else local result = "Cannot insert " .. stack.name .. " to player's inventory, " - if game.get_player(pindex).get_main_inventory().count_empty_stacks() == 0 then - result = result .. "because it is full." - end - printout(result,pindex) + if game.get_player(pindex).get_main_inventory().count_empty_stacks() == 0 then + result = result .. "because it is full." + end + printout(result, pindex) end end else local offset = 1 - if players[pindex].building.recipe_list ~= nil then - offset = offset + 1 - end + if players[pindex].building.recipe_list ~= nil then offset = offset + 1 end if players[pindex].building.sector == #players[pindex].building.sectors + offset then - --Transfer stack from player inventory to building + --Transfer stack from player inventory to building local stack = players[pindex].inventory.lua_inventory[players[pindex].inventory.index] if stack and stack.valid and stack.valid_for_read then - if players[pindex].menu == "vehicle" and game.get_player(pindex).opened.type == "spider-vehicle" and stack.prototype.place_as_equipment_result ~= nil then + if + players[pindex].menu == "vehicle" + and game.get_player(pindex).opened.type == "spider-vehicle" + and stack.prototype.place_as_equipment_result ~= nil + then return end if players[pindex].building.ent.can_insert(stack) then - game.get_player(pindex).play_sound{path = "utility/inventory_move"} + game.get_player(pindex).play_sound({ path = "utility/inventory_move" }) local result = stack.name local inserted = players[pindex].building.ent.insert(stack) - players[pindex].inventory.lua_inventory.remove{name = stack.name, count = inserted} + players[pindex].inventory.lua_inventory.remove({ name = stack.name, count = inserted }) result = "Moved " .. inserted .. " " .. result .. " to " .. players[pindex].building.ent.name printout(result, pindex) else - local result = "Cannot insert " .. stack.name .. " to " .. players[pindex].building.ent.name - printout(result,pindex) + local result = "Cannot insert " .. stack.name .. " to " .. players[pindex].building.ent.name + printout(result, pindex) end end end @@ -6214,20 +6566,22 @@ end) --You can equip armor, armor equipment, guns, ammo. You can equip from the hand, or from the inventory with an empty hand. script.on_event("equip-item", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local stack = game.get_player(pindex).cursor_stack local result = "" if stack ~= nil and stack.valid_for_read and stack.valid then --Equip item grabbed in hand, for selected menus - if not players[pindex].in_menu or players[pindex].menu == "inventory" or (players[pindex].menu == "vehicle" and game.get_player(pindex).opened.type == "spider-vehicle") then - result = fa_equipment.equip_it(stack,pindex) + if + not players[pindex].in_menu + or players[pindex].menu == "inventory" + or (players[pindex].menu == "vehicle" and game.get_player(pindex).opened.type == "spider-vehicle") + then + result = fa_equipment.equip_it(stack, pindex) end elseif players[pindex].menu == "inventory" then --Equip the selected item from its inventory slot directly local stack = game.get_player(pindex).get_main_inventory()[players[pindex].inventory.index] - result = fa_equipment.equip_it(stack,pindex) + result = fa_equipment.equip_it(stack, pindex) elseif players[pindex].menu == "vehicle" and game.get_player(pindex).opened.type == "spider-vehicle" then --Equip the selected item from its inventory slot directly local stack @@ -6237,34 +6591,30 @@ script.on_event("equip-item", function(event) else stack = players[pindex].inventory.lua_inventory[players[pindex].inventory.index] end - result = fa_equipment.equip_it(stack,pindex) - elseif (players[pindex].menu == "building" or players[pindex].menu == "vehicle") then + result = fa_equipment.equip_it(stack, pindex) + elseif players[pindex].menu == "building" or players[pindex].menu == "vehicle" then --Something will be smart-inserted so do nothing here return end if result ~= "" then --game.get_player(pindex).print(result)--** - printout(result,pindex) + printout(result, pindex) end end) --Has the same input as the ghost placement function and so it uses that script.on_event("open-rail-builder", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end if players[pindex].in_menu then - if players[pindex].ghost_rail_planning == true then - game.get_player(pindex).clear_cursor() - end + if players[pindex].ghost_rail_planning == true then game.get_player(pindex).clear_cursor() end return elseif players[pindex].ghost_rail_planning == true then fa_rails.end_ghost_rail_planning(pindex) else --Not in a menu - local ent = get_selected_ent(pindex) + local ent = get_selected_ent(pindex) local stack = game.get_player(pindex).cursor_stack if ent then if ent.name == "straight-rail" then @@ -6289,32 +6639,20 @@ end) script.on_event("quick-build-rail-left-turn", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local ent = get_selected_ent(pindex) - if not ent then - return - end + if not ent then return end --Build left turns on end rails - if ent.name == "straight-rail" then - fa_rail_builder.build_rail_turn_left_45_degrees(ent, pindex) - end + if ent.name == "straight-rail" then fa_rail_builder.build_rail_turn_left_45_degrees(ent, pindex) end end) script.on_event("quick-build-rail-right-turn", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local ent = get_selected_ent(pindex) - if not ent then - return - end + if not ent then return end --Build left turns on end rails - if ent.name == "straight-rail" then - fa_rail_builder.build_rail_turn_right_45_degrees(ent, pindex) - end + if ent.name == "straight-rail" then fa_rail_builder.build_rail_turn_right_45_degrees(ent, pindex) end end) --[[Imitates vanilla behavior: @@ -6322,14 +6660,12 @@ end) * Control click an empty slot to try to smart transfer ALL items from that inventory. ]] script.on_event("transfer-all-stacks", function(event) - pindex = event.player_index - if not check_for_player(pindex) then - return - end + pindex = event.player_index + if not check_for_player(pindex) then return end if players[pindex].in_menu then - if (players[pindex].menu == "building" or players[pindex].menu == "vehicle") then - do_multi_stack_transfer(1,pindex) + if players[pindex].menu == "building" or players[pindex].menu == "vehicle" then + do_multi_stack_transfer(1, pindex) end end end) @@ -6337,16 +6673,14 @@ end) --Default is control clicking script.on_event("fa-alternate-build", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end if players[pindex].in_menu then return else --Not in a menu local stack = game.get_player(pindex).cursor_stack - local ent = get_selected_ent(pindex) + local ent = get_selected_ent(pindex) if stack == nil or stack.valid_for_read == false or stack.valid == false then return elseif stack.name == "rail" then @@ -6364,13 +6698,11 @@ end) ]] script.on_event("transfer-half-of-all-stacks", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end if players[pindex].in_menu then - if (players[pindex].menu == "building" or players[pindex].menu == "vehicle") then - do_multi_stack_transfer(0.5,pindex) + if players[pindex].menu == "building" or players[pindex].menu == "vehicle" then + do_multi_stack_transfer(0.5, pindex) end end end) @@ -6378,91 +6710,88 @@ end) --[[Manages inventory transfers that are bigger than one stack. * Has checks and printouts! ]] -function do_multi_stack_transfer(ratio,pindex) - local result = {""} +function do_multi_stack_transfer(ratio, pindex) + local result = { "" } local sector = players[pindex].building.sectors[players[pindex].building.sector] if sector and sector.name ~= "Fluid" and players[pindex].building.sector_name ~= "player_inventory" then --This is the section where we move from the building to the player. - local item_name="" + local item_name = "" local stack = sector.inventory[players[pindex].building.index] - if stack and stack.valid and stack.valid_for_read then - item_name = stack.name - end + if stack and stack.valid and stack.valid_for_read then item_name = stack.name end - local moved, full = transfer_inventory{from=sector.inventory,to=game.players[pindex],name=item_name,ratio=ratio} + local moved, full = + transfer_inventory({ from = sector.inventory, to = game.players[pindex], name = item_name, ratio = ratio }) if full then - table.insert(result,{"inventory-full-message.main"}) - table.insert(result,", ") + table.insert(result, { "inventory-full-message.main" }) + table.insert(result, ", ") end if table_size(moved) == 0 then - table.insert(result,{"access.grabbed-nothing"}) + table.insert(result, { "access.grabbed-nothing" }) else - game.get_player(pindex).play_sound{path = "utility/inventory_move"} - local item_list={""} + game.get_player(pindex).play_sound({ path = "utility/inventory_move" }) + local item_list = { "" } local other_items = 0 local listed_count = 0 for name, amount in pairs(moved) do if listed_count <= 5 then - table.insert(item_list,{"access.item-quantity",game.item_prototypes[name].localised_name,amount}) - table.insert(item_list,", ") + table.insert(item_list, { "access.item-quantity", game.item_prototypes[name].localised_name, amount }) + table.insert(item_list, ", ") else other_items = other_items + amount end listed_count = listed_count + 1 end if other_items > 0 then - table.insert(item_list,{"access.item-quantity", "other items",other_items})--***todo localize "other items - table.insert(item_list,", ") + table.insert(item_list, { "access.item-quantity", "other items", other_items }) --***todo localize "other items + table.insert(item_list, ", ") end --trim traling comma off - item_list[#item_list]=nil - table.insert(result,{"access.grabbed-stuff",item_list}) + item_list[#item_list] = nil + table.insert(result, { "access.grabbed-stuff", item_list }) end - elseif sector and sector.name == "fluid" then --Do nothing else local offset = 1 - if players[pindex].building.recipe_list ~= nil then - offset = offset + 1 - end + if players[pindex].building.recipe_list ~= nil then offset = offset + 1 end if players[pindex].building.sector_name == "player_inventory" then game.print("path 3b") --This is the section where we move from the player to the building. - local item_name="" + local item_name = "" local stack = players[pindex].inventory.lua_inventory[players[pindex].inventory.index] - if stack and stack.valid and stack.valid_for_read then - item_name = stack.name - end + if stack and stack.valid and stack.valid_for_read then item_name = stack.name end - local moved, full = transfer_inventory{from=game.get_player(pindex).get_main_inventory(),to=players[pindex].building.ent,name=item_name,ratio=ratio} + local moved, full = transfer_inventory({ + from = game.get_player(pindex).get_main_inventory(), + to = players[pindex].building.ent, + name = item_name, + ratio = ratio, + }) - if full then - table.insert(result,"Inventory full or not applicable, ") - end + if full then table.insert(result, "Inventory full or not applicable, ") end if table_size(moved) == 0 then - table.insert(result,{"access.placed-nothing"}) + table.insert(result, { "access.placed-nothing" }) else - game.get_player(pindex).play_sound{path = "utility/inventory_move"} - local item_list={""} + game.get_player(pindex).play_sound({ path = "utility/inventory_move" }) + local item_list = { "" } local other_items = 0 local listed_count = 0 for name, amount in pairs(moved) do if listed_count <= 5 then - table.insert(item_list,{"access.item-quantity",game.item_prototypes[name].localised_name,amount}) - table.insert(item_list,", ") + table.insert(item_list, { "access.item-quantity", game.item_prototypes[name].localised_name, amount }) + table.insert(item_list, ", ") else other_items = other_items + amount end listed_count = listed_count + 1 end if other_items > 0 then - table.insert(item_list,{"access.item-quantity", "other items",other_items})--***todo localize "other items - table.insert(item_list,", ") + table.insert(item_list, { "access.item-quantity", "other items", other_items }) --***todo localize "other items + table.insert(item_list, ", ") end --trim trailing comma off - item_list[#item_list]=nil - table.insert(result,{"access.placed-stuff",fa_utils.breakup_string(item_list)}) + item_list[#item_list] = nil + table.insert(result, { "access.placed-stuff", fa_utils.breakup_string(item_list) }) end end end @@ -6479,7 +6808,7 @@ end function transfer_inventory(args) args.name = args.name or "" args.ratio = args.ratio or 1 - local transfer_list={} + local transfer_list = {} if args.name ~= "" then --Known name: transfer only this transfer_list[args.name] = args.from.get_item_count(args.name) @@ -6489,20 +6818,20 @@ function transfer_inventory(args) --No name: Transfer everything transfer_list = args.from.get_contents() end - local full=false + local full = false local res = {} for name, amount in pairs(transfer_list) do if name ~= "blueprint" and name ~= "blueprint-book" then amount = math.ceil(amount * args.ratio) - local actual_amount = args.to.insert({name=name, count=amount}) + local actual_amount = args.to.insert({ name = name, count = amount }) if actual_amount ~= amount then - print(name,amount,actual_amount) + print(name, amount, actual_amount) amount = actual_amount full = true end if amount > 0 then res[name] = amount - args.from.remove({name=name, count=amount}) + args.from.remove({ name = name, count = amount }) end end end @@ -6512,33 +6841,40 @@ end script.on_event("crafting-5", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local ent = get_selected_ent(pindex) local stack = game.get_player(pindex).cursor_stack if players[pindex].in_menu then if players[pindex].menu == "crafting" then - local recipe = players[pindex].crafting.lua_recipes[players[pindex].crafting.category][players[pindex].crafting.index] + local recipe = + players[pindex].crafting.lua_recipes[players[pindex].crafting.category][players[pindex].crafting.index] local T = { count = 5, - recipe = players[pindex].crafting.lua_recipes[players[pindex].crafting.category][players[pindex].crafting.index], - silent = false + recipe = players[pindex].crafting.lua_recipes[players[pindex].crafting.category][players[pindex].crafting.index], + silent = false, } local count = game.get_player(pindex).begin_crafting(T) if count > 0 then local total_count = fa_crafting.count_in_crafting_queue(T.recipe.name, pindex) - printout("Started crafting " .. count .. " " .. fa_localising.get_recipe_from_name(recipe.name,pindex) .. ", " .. total_count .. " total in queue", pindex) + printout( + "Started crafting " + .. count + .. " " + .. fa_localising.get_recipe_from_name(recipe.name, pindex) + .. ", " + .. total_count + .. " total in queue", + pindex + ) else printout("Not enough materials", pindex) end - elseif players[pindex].menu == "crafting_queue" then fa_crafting.load_crafting_queue(pindex) if players[pindex].crafting_queue.max >= 1 then local T = { - index = players[pindex].crafting_queue.index, - count = 5 + index = players[pindex].crafting_queue.index, + count = 5, } game.get_player(pindex).cancel_crafting(T) fa_crafting.load_crafting_queue(pindex) @@ -6550,26 +6886,35 @@ end) script.on_event("menu-clear-filter", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local ent = get_selected_ent(pindex) local stack = game.get_player(pindex).cursor_stack if players[pindex].in_menu then - if (players[pindex].menu == "building" or players[pindex].menu == "vehicle") then + if players[pindex].menu == "building" or players[pindex].menu == "vehicle" then local stack = game.get_player(pindex).cursor_stack if players[pindex].building.sector <= #players[pindex].building.sectors then if stack and stack.valid_for_read and stack.valid and stack.count > 0 then local iName = players[pindex].building.sectors[players[pindex].building.sector].name - if iName == "Filters" and players[pindex].item_selection == false and players[pindex].building.index < #players[pindex].building.sectors[players[pindex].building.sector].inventory then + if + iName == "Filters" + and players[pindex].item_selection == false + and players[pindex].building.index + < #players[pindex].building.sectors[players[pindex].building.sector].inventory + then players[pindex].building.ent.set_filter(players[pindex].building.index, nil) - players[pindex].building.sectors[players[pindex].building.sector].inventory[players[pindex].building.index] = "No filter selected." + players[pindex].building.sectors[players[pindex].building.sector].inventory[players[pindex].building.index] = + "No filter selected." printout("Filter cleared", pindex) - end - elseif players[pindex].building.sectors[players[pindex].building.sector].name == "Filters" and players[pindex].building.item_selection == false and players[pindex].building.index < #players[pindex].building.sectors[players[pindex].building.sector].inventory then + elseif + players[pindex].building.sectors[players[pindex].building.sector].name == "Filters" + and players[pindex].building.item_selection == false + and players[pindex].building.index + < #players[pindex].building.sectors[players[pindex].building.sector].inventory + then players[pindex].building.ent.set_filter(players[pindex].building.index, nil) - players[pindex].building.sectors[players[pindex].building.sector].inventory[players[pindex].building.index] = "No filter selected." + players[pindex].building.sectors[players[pindex].building.sector].inventory[players[pindex].building.index] = + "No filter selected." printout("Filter cleared.", pindex) end end @@ -6580,59 +6925,61 @@ end) --Reads the entity status but also adds on extra info depending on the entity script.on_event("read-entity-status", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local ent = get_selected_ent(pindex) - if not ent then - return - end + if not ent then return end local stack = game.get_player(pindex).cursor_stack - if players[pindex].in_menu then - return - end + if players[pindex].in_menu then return end --Print out the status of a machine, if it exists. - local result = {""} + local result = { "" } local ent_status_id = ent.status local ent_status_text = "" local status_lookup = fa_utils.into_lookup(defines.entity_status) - status_lookup[23] = "Full burnt result output"--weird exception + status_lookup[23] = "Full burnt result output" --weird exception if ent.name == "cargo-wagon" then - --Instead of status, read contents + --Instead of status, read contents table.insert(result, fa_trains.cargo_wagon_top_contents_info(ent)) elseif ent.name == "fluid-wagon" then - --Instead of status, read contents + --Instead of status, read contents table.insert(result, fa_trains.fluid_contents_info(ent)) elseif ent_status_id ~= nil then --Print status if it exists ent_status_text = status_lookup[ent_status_id] if ent_status_text == nil then - print("Weird no entity status lookup".. ent.name .. '-' .. ent.type .. '-' .. ent.status) + print("Weird no entity status lookup" .. ent.name .. "-" .. ent.type .. "-" .. ent.status) end - table.insert(result, {"entity-status."..ent_status_text:gsub("_","-")}) - else--There is no status + table.insert(result, { "entity-status." .. ent_status_text:gsub("_", "-") }) + else --There is no status --When there is no status, for entities with fuel inventories, read that out instead. This is typical for vehicles. if ent.get_fuel_inventory() ~= nil then - table.insert(result, fa_driving.fuel_inventory_info(ent)) + table.insert(result, fa_driving.fuel_inventory_info(ent)) elseif ent.type == "electric-pole" then --For electric poles with no power flow, report the nearest electric pole with a power flow. if fa_electrical.get_electricity_satisfaction(ent) > 0 then - table.insert(result, fa_electrical.get_electricity_satisfaction(ent) .. " percent network satisfaction, with " .. fa_electrical.get_electricity_flow_info(ent)) + table.insert( + result, + fa_electrical.get_electricity_satisfaction(ent) + .. " percent network satisfaction, with " + .. fa_electrical.get_electricity_flow_info(ent) + ) else - table.insert(result, "No power, " .. fa_electrical.report_nearest_supplied_electric_pole(ent)) + table.insert(result, "No power, " .. fa_electrical.report_nearest_supplied_electric_pole(ent)) end else - table.insert(result, "No status.") + table.insert(result, "No status.") end end --For working or normal entities, give some extra info about specific entities. - if #result == 1 then - table.insert(result, "result error") - end + if #result == 1 then table.insert(result, "result error") end --For working or normal entities, give some extra info about specific entities in terms of speeds or bonuses. local list = defines.entity_status - if ent.status ~= nil and ent.status ~= list.no_power and ent.status ~= list.no_power and ent.status ~= list.no_fuel then + if + ent.status ~= nil + and ent.status ~= list.no_power + and ent.status ~= list.no_power + and ent.status ~= list.no_fuel + then if ent.type == "inserter" then --items per minute based on rotation speed and the STATED hand capacity local cap = ent.force.inserter_stack_size_bonus + 1 if ent.name == "stack-inserter" or ent.name == "stack-filter-inserter" then @@ -6643,7 +6990,10 @@ script.on_event("read-entity-status", function(event) end if ent.prototype ~= nil and ent.prototype.belt_speed ~= nil and ent.prototype.belt_speed > 0 then --items per minute by simple reading if ent.type == "splitter" then - table.insert(result, ", can process " .. math.floor(ent.prototype.belt_speed * 480 * 2) .. " items per second") + table.insert( + result, + ", can process " .. math.floor(ent.prototype.belt_speed * 480 * 2) .. " items per second" + ) else table.insert(result, ", can move " .. math.floor(ent.prototype.belt_speed * 480) .. " items per second") end @@ -6652,51 +7002,80 @@ script.on_event("read-entity-status", function(event) local progress = ent.crafting_progress local speed = ent.crafting_speed local recipe_time = 0 - local cycles = 0-- crafting cycles completed per minute for this recipe + local cycles = 0 -- crafting cycles completed per minute for this recipe if ent.get_recipe() ~= nil and ent.get_recipe().valid then recipe_time = ent.get_recipe().energy cycles = 60 / recipe_time * speed end local cycles_string = string.format(" %.2f ", cycles) - if cycles == math.floor(cycles) then - cycles_string = string.format(" %d ", cycles) - end + if cycles == math.floor(cycles) then cycles_string = string.format(" %d ", cycles) end local speed_string = string.format(" %.2f ", speed) - if speed == math.floor(speed) then - speed_string = string.format(" %d ", cycles) - end + if speed == math.floor(speed) then speed_string = string.format(" %d ", cycles) end if cycles < 10 then --more than 6 seconds to craft table.insert(result, ", recipe progress " .. math.floor(progress * 100) .. " percent ") end - if cycles > 0 then - table.insert(result, ", can complete " .. cycles_string .. " recipe cycles per minute ") - end - table.insert(result, ", with a crafting speed of " .. speed_string .. ", at " .. math.floor(100 * (1 + ent.speed_bonus) + 0.5) .. " percent ") + if cycles > 0 then table.insert(result, ", can complete " .. cycles_string .. " recipe cycles per minute ") end + table.insert( + result, + ", with a crafting speed of " + .. speed_string + .. ", at " + .. math.floor(100 * (1 + ent.speed_bonus) + 0.5) + .. " percent " + ) if ent.productivity_bonus ~= 0 then - table.insert(result, ", with productivity bonus " .. math.floor(100 * (0 + ent.productivity_bonus) + 0.5) .. " percent ") + table.insert( + result, + ", with productivity bonus " .. math.floor(100 * (0 + ent.productivity_bonus) + 0.5) .. " percent " + ) end elseif ent.type == "mining-drill" then - table.insert(result, ", producing " .. string.format(" %.2f ",ent.prototype.mining_speed * 60 * (1 + ent.speed_bonus)) .. " items per minute ") + table.insert( + result, + ", producing " + .. string.format(" %.2f ", ent.prototype.mining_speed * 60 * (1 + ent.speed_bonus)) + .. " items per minute " + ) if ent.speed_bonus ~= 0 then - table.insert(result, ", with speed " .. math.floor(100 * (1 + ent.speed_bonus) + 0.5) .. " percent " ) + table.insert(result, ", with speed " .. math.floor(100 * (1 + ent.speed_bonus) + 0.5) .. " percent ") end if ent.productivity_bonus ~= 0 then - table.insert(result, ", with productivity bonus " .. math.floor(100 * (0 + ent.productivity_bonus) + 0.5) .. " percent ") + table.insert( + result, + ", with productivity bonus " .. math.floor(100 * (0 + ent.productivity_bonus) + 0.5) .. " percent " + ) end elseif ent.name == "lab" then if ent.speed_bonus ~= 0 then - table.insert(result, ", with speed " .. math.floor(100 * (1 + ent.force.laboratory_speed_modifier * (1 + (ent.speed_bonus - ent.force.laboratory_speed_modifier))) + 0.5) .. " percent " )--laterdo fix bug** + table.insert( + result, + ", with speed " + .. math.floor( + 100 + * (1 + ent.force.laboratory_speed_modifier * (1 + (ent.speed_bonus - ent.force.laboratory_speed_modifier))) + + 0.5 + ) + .. " percent " + ) --laterdo fix bug** --game.get_player(pindex).print(result) end if ent.productivity_bonus ~= 0 then - table.insert(result, ", with productivity bonus " .. math.floor(100 * (0 + ent.productivity_bonus + ent.force.laboratory_productivity_bonus) + 0.5) .. " percent ") + table.insert( + result, + ", with productivity bonus " + .. math.floor(100 * (0 + ent.productivity_bonus + ent.force.laboratory_productivity_bonus) + 0.5) + .. " percent " + ) end else --All other entities with the an applicable status if ent.speed_bonus ~= 0 then table.insert(result, ", with speed " .. math.floor(100 * (1 + ent.speed_bonus) + 0.5) .. " percent ") end if ent.productivity_bonus ~= 0 then - table.insert(result, ", with productivity bonus " .. math.floor(100 * (0 + ent.productivity_bonus) + 0.5) .. " percent ") + table.insert( + result, + ", with productivity bonus " .. math.floor(100 * (0 + ent.productivity_bonus) + 0.5) .. " percent " + ) end end --laterdo maybe pump speed? @@ -6711,45 +7090,61 @@ script.on_event("read-entity-status", function(event) drain = 0 end local uses_energy = false - if drain > 0 or (ent.prototype ~= nil and ent.prototype.max_energy_usage ~= nil and ent.prototype.max_energy_usage > 0) then + if + drain > 0 + or (ent.prototype ~= nil and ent.prototype.max_energy_usage ~= nil and ent.prototype.max_energy_usage > 0) + then uses_energy = true end if ent.status ~= nil and uses_energy and ent.status == list.working then - table.insert(result, ", consuming " .. fa_electrical.get_power_string(ent.prototype.max_energy_usage * 60 * power_rate + drain)) + table.insert( + result, + ", consuming " .. fa_electrical.get_power_string(ent.prototype.max_energy_usage * 60 * power_rate + drain) + ) elseif ent.status ~= nil and uses_energy and ent.status == list.no_power or ent.status == list.low_power then - table.insert(result, ", consuming less than " .. fa_electrical.get_power_string(ent.prototype.max_energy_usage * 60 * power_rate + drain)) - elseif ent.status ~= nil and uses_energy or (ent.prototype ~= nil and ent.prototype.max_energy_usage ~= nil and ent.prototype.max_energy_usage > 0) then + table.insert( + result, + ", consuming less than " + .. fa_electrical.get_power_string(ent.prototype.max_energy_usage * 60 * power_rate + drain) + ) + elseif + ent.status ~= nil and uses_energy + or (ent.prototype ~= nil and ent.prototype.max_energy_usage ~= nil and ent.prototype.max_energy_usage > 0) + then table.insert(result, ", idle and consuming " .. fa_electrical.get_power_string(drain)) end - if uses_energy and ent.prototype.burner_prototype ~= nil then - table.insert(result, " as burner fuel ") - end + if uses_energy and ent.prototype.burner_prototype ~= nil then table.insert(result, " as burner fuel ") end - --Entity Health + --Entity Health if ent.is_entity_with_health and ent.get_health_ratio() == 1 then - table.insert(result, {"access.full-health"}) + table.insert(result, { "access.full-health" }) elseif ent.is_entity_with_health then - table.insert(result, {"access.percent-health", math.floor(ent.get_health_ratio() * 100) }) + table.insert(result, { "access.percent-health", math.floor(ent.get_health_ratio() * 100) }) end -- Report nearest rail intersection position -- laterdo find better keybind if ent.name == "straight-rail" then local nearest, dist = fa_rails.find_nearest_intersection(ent, pindex) if nearest == nil then - table.insert(result, ", no rail intersections within " .. dist .. " tiles " ) + table.insert(result, ", no rail intersections within " .. dist .. " tiles ") else - table.insert(result, ", nearest rail intersection at " .. dist .. " " .. fa_utils.direction_lookup(fa_utils.get_direction_biased(nearest.position,ent.position))) + table.insert( + result, + ", nearest rail intersection at " + .. dist + .. " " + .. fa_utils.direction_lookup(fa_utils.get_direction_biased(nearest.position, ent.position)) + ) end end --Spawners: Report evolution factor if ent.type == "unit-spawner" then - table.insert(result, ", evolution factor " .. math.floor(1000 * ent.force.evolution_factor)/1000 ) + table.insert(result, ", evolution factor " .. math.floor(1000 * ent.force.evolution_factor) / 1000) end - printout(result ,pindex) + printout(result, pindex) --game.get_player(pindex).print(result)--** - end) script.on_event("rotate-building", function(event) @@ -6762,101 +7157,94 @@ end) script.on_event("flip-blueprint-horizontal-info", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local p = game.get_player(pindex) local bp = p.cursor_stack - if bp == nil or bp.valid_for_read == false or bp.is_blueprint == false then - return - end - printout("Flipping horizontal",pindex) + if bp == nil or bp.valid_for_read == false or bp.is_blueprint == false then return end + printout("Flipping horizontal", pindex) end) script.on_event("flip-blueprint-vertical-info", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local p = game.get_player(pindex) local bp = p.cursor_stack - if bp == nil or bp.valid_for_read == false or bp.is_blueprint == false then - return - end - printout("Flipping vertical",pindex) + if bp == nil or bp.valid_for_read == false or bp.is_blueprint == false then return end + printout("Flipping vertical", pindex) end) script.on_event("inventory-read-weapons-data", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - if not(players[pindex].in_menu) then + if not check_for_player(pindex) then return end + if not players[pindex].in_menu then return elseif players[pindex].menu == "inventory" then --Read Weapon data - local result = fa_equipment.read_weapons_and_ammo(pindex) - --game.get_player(pindex).print(result)-- - printout(result,pindex) + local result = fa_equipment.read_weapons_and_ammo(pindex) + --game.get_player(pindex).print(result)-- + printout(result, pindex) end end) script.on_event("inventory-reload-weapons", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end if players[pindex].menu == "inventory" then --Reload weapons - local result = fa_equipment.reload_weapons(pindex) - --game.get_player(pindex).print(result) - printout(result,pindex) + local result = fa_equipment.reload_weapons(pindex) + --game.get_player(pindex).print(result) + printout(result, pindex) end end) script.on_event("inventory-remove-all-weapons-and-ammo", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end if players[pindex].menu == "inventory" then - local result = fa_equipment.remove_weapons_and_ammo(pindex) - --game.get_player(pindex).print(result) - printout(result,pindex) + local result = fa_equipment.remove_weapons_and_ammo(pindex) + --game.get_player(pindex).print(result) + printout(result, pindex) end end) --Reads the custom info for an item selected. If you are driving, it returns custom vehicle info script.on_event("item-info", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end if game.get_player(pindex).driving and players[pindex].menu ~= "train_menu" then - printout(fa_driving.vehicle_info(pindex),pindex) + printout(fa_driving.vehicle_info(pindex), pindex) return end local offset = 0 - if (players[pindex].menu == "building" or players[pindex].menu == "vehicle") and players[pindex].building.recipe_list ~= nil then + if + (players[pindex].menu == "building" or players[pindex].menu == "vehicle") + and players[pindex].building.recipe_list ~= nil + then offset = 1 end if not players[pindex].in_menu then - local ent = get_selected_ent(pindex) + local ent = get_selected_ent(pindex) if ent and ent.valid then game.get_player(pindex).selected = ent local str = ent.localised_description - if str == nil or str == "" then - str = "No description for this entity" - end + if str == nil or str == "" then str = "No description for this entity" end printout(str, pindex) else printout("Nothing selected, use this key to describe an entity or item that you select.", pindex) end elseif players[pindex].in_menu then - if players[pindex].menu == "inventory" or players[pindex].menu == "player_trash" or ((players[pindex].menu == "building" or players[pindex].menu == "vehicle") and players[pindex].building.sector > offset + #players[pindex].building.sectors) then + if + players[pindex].menu == "inventory" + or players[pindex].menu == "player_trash" + or ( + (players[pindex].menu == "building" or players[pindex].menu == "vehicle") + and players[pindex].building.sector > offset + #players[pindex].building.sectors + ) + then local stack = players[pindex].inventory.lua_inventory[players[pindex].inventory.index] if players[pindex].menu == "player_trash" then - stack = game.get_player(pindex).get_inventory(defines.inventory.character_trash)[players[pindex].inventory.index] + stack = + game.get_player(pindex).get_inventory(defines.inventory.character_trash)[players[pindex].inventory.index] end if stack and stack.valid_for_read and stack.valid == true then local str = "" @@ -6865,14 +7253,11 @@ script.on_event("item-info", function(event) else str = stack.prototype.localised_description end - if str == nil or str == "" then - str = "No description" - end + if str == nil or str == "" then str = "No description" end printout(str, pindex) else printout("No description", pindex) end - elseif players[pindex].menu == "technology" then local techs = {} if players[pindex].technology.category == 1 then @@ -6883,17 +7268,19 @@ script.on_event("item-info", function(event) techs = players[pindex].technology.lua_unlocked end - if next(techs) ~= nil and players[pindex].technology.index > 0 and players[pindex].technology.index <= #techs then - local result = {""} + if + next(techs) ~= nil + and players[pindex].technology.index > 0 + and players[pindex].technology.index <= #techs + then + local result = { "" } table.insert(result, "Description: ") - table.insert(result, techs[players[pindex].technology.index].localised_description or "No description") - table.insert(result,", Rewards: ") + table.insert(result, techs[players[pindex].technology.index].localised_description or "No description") + table.insert(result, ", Rewards: ") local rewards = techs[players[pindex].technology.index].effects for i, reward in ipairs(rewards) do for i1, v in pairs(reward) do - if v then - table.insert(result, ", " .. tostring(v)) - end + if v then table.insert(result, ", " .. tostring(v)) end end end if techs[players[pindex].technology.index].name == "electronics" then @@ -6901,9 +7288,9 @@ script.on_event("item-info", function(event) end printout(result, pindex) end - elseif players[pindex].menu == "crafting" then - local recipe = players[pindex].crafting.lua_recipes[players[pindex].crafting.category][players[pindex].crafting.index] + local recipe = + players[pindex].crafting.lua_recipes[players[pindex].crafting.category][players[pindex].crafting.index] if recipe ~= nil and #recipe.products > 0 then local product_name = recipe.products[1].name ---@type LuaItemPrototype | LuaFluidPrototype @@ -6912,7 +7299,7 @@ script.on_event("item-info", function(event) if product == nil then product = game.fluid_prototypes[product_name] product_is_item = false - elseif (product_name == "empty-barrel" and recipe.products[2] ~= nil) then + elseif product_name == "empty-barrel" and recipe.products[2] ~= nil then product_name = recipe.products[2].name product = game.fluid_prototypes[product_name] product_is_item = false @@ -6924,33 +7311,32 @@ script.on_event("item-info", function(event) else str = product.localised_description end - if str == nil or str == "" then - str = "No description found for this" - end + if str == nil or str == "" then str = "No description found for this" end printout(str, pindex) else printout("No description found, menu error", pindex) end - elseif (players[pindex].menu == "building" or players[pindex].menu == "vehicle") then + elseif players[pindex].menu == "building" or players[pindex].menu == "vehicle" then if players[pindex].building.recipe_selection then - local recipe = players[pindex].building.recipe_list[players[pindex].building.category][players[pindex].building.index] + local recipe = + players[pindex].building.recipe_list[players[pindex].building.category][players[pindex].building.index] if recipe ~= nil and #recipe.products > 0 then local product_name = recipe.products[1].name local product = game.item_prototypes[product_name] or game.fluid_prototypes[product_name] local str = product.localised_description - if str == nil or str == "" then - str = "No description found for this" - end + if str == nil or str == "" then str = "No description found for this" end printout(str, pindex) else printout("No description found, menu error", pindex) end elseif players[pindex].building.sector <= #players[pindex].building.sectors then local inventory = players[pindex].building.sectors[players[pindex].building.sector].inventory - if inventory == nil or not inventory.valid then - printout("No description found, menu error", pindex) - end - if players[pindex].building.sectors[players[pindex].building.sector].name ~= "Fluid" and players[pindex].building.sectors[players[pindex].building.sector].name ~= "Filters" and inventory.is_empty() then + if inventory == nil or not inventory.valid then printout("No description found, menu error", pindex) end + if + players[pindex].building.sectors[players[pindex].building.sector].name ~= "Fluid" + and players[pindex].building.sectors[players[pindex].building.sector].name ~= "Filters" + and inventory.is_empty() + then printout("No description found, menu error", pindex) return end @@ -6962,9 +7348,7 @@ script.on_event("item-info", function(event) else str = stack.prototype.localised_description end - if str == nil or str == "" then - str = "No description found for this item" - end + if str == nil or str == "" then str = "No description found for this item" end printout(str, pindex) else printout("No description found, menu error", pindex) @@ -6973,41 +7357,32 @@ script.on_event("item-info", function(event) else --Another menu printout("Descriptions are not supported for this menu.", pindex) end - end end) --Reads the custom info for the last indexed scanner item script.on_event("item-info-last-indexed", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end if players[pindex].in_menu then - printout("Error: Cannot check scanned item descriptions while in a menu",pindex) + printout("Error: Cannot check scanned item descriptions while in a menu", pindex) return end local ent = players[pindex].last_indexed_ent if ent == nil or not ent.valid then - printout("No description, note that most resources need to be examined from up close",pindex)--laterdo find a workaround for aggregate ents + printout("No description, note that most resources need to be examined from up close", pindex) --laterdo find a workaround for aggregate ents return end local str = ent.localised_description - if str == nil or str == "" then - str = "No description found for this entity" - end + if str == nil or str == "" then str = "No description found for this entity" end printout(str, pindex) end) ---Read production statistics info for the selected item, in the hand or else selected in the inventory menu +--Read production statistics info for the selected item, in the hand or else selected in the inventory menu script.on_event("item-production-info", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - if game.get_player(pindex).driving then - return - end + if not check_for_player(pindex) then return end + if game.get_player(pindex).driving then return end local str = selected_item_production_stats_info(pindex) printout(str, pindex) end) @@ -7016,32 +7391,32 @@ end) --For realism, if we adjust by 12 hours, we get 23 to 1 as midnight and 6 to 18 as peak solar. script.on_event("read-time-and-research-progress", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end --Get local time local surf = game.get_player(pindex).surface - local hour = math.floor((24*surf.daytime + 12) % 24) - local minute = math.floor((24* surf.daytime - math.floor(24*surf.daytime)) * 60) + local hour = math.floor((24 * surf.daytime + 12) % 24) + local minute = math.floor((24 * surf.daytime - math.floor(24 * surf.daytime)) * 60) local time_string = " The local time is " .. hour .. ":" .. string.format("%02d", minute) .. ", " --Get total playtime - local total_hours = math.floor(game.tick/216000) - local total_minutes = math.floor((game.tick % 216000)/3600) - local total_time_string = " The total mission time is " .. total_hours .. " hours and " .. total_minutes .. " minutes " + local total_hours = math.floor(game.tick / 216000) + local total_minutes = math.floor((game.tick % 216000) / 3600) + local total_time_string = " The total mission time is " + .. total_hours + .. " hours and " + .. total_minutes + .. " minutes " --Add research progress info local progress_string = " No research in progress, " local tech = game.get_player(pindex).force.current_research if tech ~= nil then - local research_progress = math.floor(game.get_player(pindex).force.research_progress* 100) + local research_progress = math.floor(game.get_player(pindex).force.research_progress * 100) progress_string = " Researching " .. tech.name .. ", " .. research_progress .. "%, " end printout(time_string .. progress_string .. total_time_string, pindex) - if players[pindex].vanilla_mode then - game.get_player(pindex).open_technology_gui() - end + if players[pindex].vanilla_mode then game.get_player(pindex).open_technology_gui() end --Temporarily disable research queue, add it as a feature laterdo** game.get_player(pindex).force.research_queue_enabled = false @@ -7049,15 +7424,13 @@ end) script.on_event(defines.events.on_player_cursor_stack_changed, function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local stack = game.get_player(pindex).cursor_stack local new_item_name = "" if stack and stack.valid_for_read then new_item_name = stack.name if stack.is_blueprint and players[pindex].blueprint_hand_direction ~= dirs.north then - --Reset blueprint rotation + --Reset blueprint rotation players[pindex].blueprint_hand_direction = dirs.north fa_blueprints.refresh_blueprint_in_hand(pindex) end @@ -7076,11 +7449,11 @@ script.on_event(defines.events.on_player_cursor_stack_changed, function(event) fa_graphics.sync_build_cursor_graphics(pindex) end) -script.on_event(defines.events.on_player_mined_item,function(event) +script.on_event(defines.events.on_player_mined_item, function(event) local pindex = event.player_index - --Play item pickup sound - game.get_player(pindex).play_sound{path = "utility/picked_up_item", volume_modifier = 1} - game.get_player(pindex).play_sound{path = "Close-Inventory-Sound", volume_modifier = 1} + --Play item pickup sound + game.get_player(pindex).play_sound({ path = "utility/picked_up_item", volume_modifier = 1 }) + game.get_player(pindex).play_sound({ path = "Close-Inventory-Sound", volume_modifier = 1 }) end) function ensure_global_structures_are_up_to_date() @@ -7096,7 +7469,15 @@ function ensure_global_structures_are_up_to_date() local types = {} for _, ent in pairs(game.entity_prototypes) do - if types[ent.type] == nil and ent.weight == nil and (ent.burner_prototype ~= nil or ent.electric_energy_source_prototype~= nil or ent.automated_ammo_count ~= nil)then + if + types[ent.type] == nil + and ent.weight == nil + and ( + ent.burner_prototype ~= nil + or ent.electric_energy_source_prototype ~= nil + or ent.automated_ammo_count ~= nil + ) + then types[ent.type] = true end end @@ -7112,10 +7493,20 @@ function ensure_global_structures_are_up_to_date() local ents = game.entity_prototypes local types = {} for i, ent in pairs(ents) do --- if (ent.get_inventory_size(defines.inventory.fuel) ~= nil or ent.get_inventory_size(defines.inventory.chest) ~= nil or ent.get_inventory_size(defines.inventory.assembling_machine_input) ~= nil) and ent.weight == nil then - if ent.speed == nil and ent.consumption == nil and (ent.burner_prototype ~= nil or ent.mining_speed ~= nil or ent.crafting_speed ~= nil or ent.automated_ammo_count ~= nil or ent.construction_radius ~= nil) then + -- if (ent.get_inventory_size(defines.inventory.fuel) ~= nil or ent.get_inventory_size(defines.inventory.chest) ~= nil or ent.get_inventory_size(defines.inventory.assembling_machine_input) ~= nil) and ent.weight == nil then + if + ent.speed == nil + and ent.consumption == nil + and ( + ent.burner_prototype ~= nil + or ent.mining_speed ~= nil + or ent.crafting_speed ~= nil + or ent.automated_ammo_count ~= nil + or ent.construction_radius ~= nil + ) + then types[ent.type] = true - end + end end for i, type in pairs(types) do table.insert(production_types, i) @@ -7129,9 +7520,7 @@ function ensure_global_structures_are_up_to_date() local ents = game.entity_prototypes local types = {} for i, ent in pairs(ents) do - if ent.is_building then - types[ent.type] = true - end + if ent.is_building then types[ent.type] = true end end types["transport-belt"] = nil for i, type in pairs(types) do @@ -7140,7 +7529,6 @@ function ensure_global_structures_are_up_to_date() table.insert(building_types, "character") global.scheduled_events = global.scheduled_events or {} - end script.on_load(function() @@ -7155,9 +7543,7 @@ script.on_init(function() ---@type any local skip_intro_message = remote.interfaces["freeplay"] skip_intro_message = skip_intro_message and skip_intro_message["set_skip_intro"] - if skip_intro_message then - remote.call("freeplay","set_skip_intro",true) - end + if skip_intro_message then remote.call("freeplay", "set_skip_intro", true) end ensure_global_structures_are_up_to_date() end) @@ -7186,23 +7572,21 @@ end) script.on_event(defines.events.on_player_created, function(event) initialize(game.players[event.player_index]) - if not game.is_multiplayer() then - printout("Press 'TAB' to continue", pindex) - end + if not game.is_multiplayer() then printout("Press 'TAB' to continue", pindex) end end) script.on_event(defines.events.on_gui_closed, function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end --Other resets players[pindex].move_queue = {} - if players[pindex].in_menu == true and players[pindex].menu ~= "prompt"then + if players[pindex].in_menu == true and players[pindex].menu ~= "prompt" then if players[pindex].menu == "inventory" then - game.get_player(pindex).play_sound{path="Close-Inventory-Sound"} - elseif players[pindex].menu == "travel" or players[pindex].menu == "structure-travel" and event.element ~= nil then + game.get_player(pindex).play_sound({ path = "Close-Inventory-Sound" }) + elseif + players[pindex].menu == "travel" or players[pindex].menu == "structure-travel" and event.element ~= nil + then event.element.destroy() end players[pindex].in_menu = false @@ -7212,7 +7596,7 @@ script.on_event(defines.events.on_gui_closed, function(event) players[pindex].item_selector = { index = 0, group = 0, - subgroup = 0 + subgroup = 0, } players[pindex].building.item_selection = false close_menu_resets(pindex) @@ -7221,21 +7605,16 @@ end) script.on_event("save-game-manually", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end game.auto_save("manual") printout("Saving Game, please do not quit yet.", pindex) - end) --Reads flying text script.on_nth_tick(10, function(event) for pindex, player in pairs(players) do if player.allow_reading_flying_text == nil or player.allow_reading_flying_text == true then - if player.past_flying_texts == nil then - player.past_flying_texts = {} - end + if player.past_flying_texts == nil then player.past_flying_texts = {} end local flying_texts = {} local search = { type = "flying-text", @@ -7245,17 +7624,13 @@ script.on_nth_tick(10, function(event) for _, ftext in pairs(game.get_player(pindex).surface.find_entities_filtered(search)) do local id = ftext.text - if type(id) == 'table' then - id = serpent.line(id) - end + if type(id) == "table" then id = serpent.line(id) end flying_texts[id] = (flying_texts[id] or 0) + 1 end for id, count in pairs(flying_texts) do if count > (player.past_flying_texts[id] or 0) then local ok, local_text = serpent.load(id) - if ok then - printout(local_text,pindex) - end + if ok then printout(local_text, pindex) end end end player.past_flying_texts = flying_texts @@ -7263,50 +7638,42 @@ script.on_nth_tick(10, function(event) end end) -walk_type_speech={ +walk_type_speech = { "Telestep enabled", "Step by walk enabled", - "Walking smoothly enabled" + "Walking smoothly enabled", } -script.on_event("toggle-walk",function(event) +script.on_event("toggle-walk", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end reset_bump_stats(pindex) players[pindex].move_queue = {} if players[pindex].walk == 0 then --Mode 1 (walk-by-step) is temporarily disabled until it comes back as an in game setting. players[pindex].walk = 2 - game.get_player(pindex).character_running_speed_modifier = 0 -- 100% + 0 = 100% - else--walk == 1 or walk == 2 + game.get_player(pindex).character_running_speed_modifier = 0 -- 100% + 0 = 100% + else --walk == 1 or walk == 2 players[pindex].walk = 0 game.get_player(pindex).character_running_speed_modifier = -1 -- 100% - 100% = 0% end --players[pindex].walk = (players[pindex].walk + 1) % 3 - printout(walk_type_speech[players[pindex].walk +1], pindex) + printout(walk_type_speech[players[pindex].walk + 1], pindex) end) function fix_walk(pindex) - if not check_for_player(pindex) then - return - end - if game.get_player(pindex).character == nil or game.get_player(pindex).character.valid == false then - return - end + if not check_for_player(pindex) then return end + if game.get_player(pindex).character == nil or game.get_player(pindex).character.valid == false then return end if players[pindex].walk == 0 then game.get_player(pindex).character_running_speed_modifier = -1 -- 100% - 100% = 0% - else--walk > 0 - game.get_player(pindex).character_running_speed_modifier = 0 -- 100% + 0 = 100% + else --walk > 0 + game.get_player(pindex).character_running_speed_modifier = 0 -- 100% + 0 = 100% end end --Toggle building while walking script.on_event("toggle-build-lock", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end if not (players[pindex].in_menu == true) then if players[pindex].build_lock == true then players[pindex].build_lock = false @@ -7318,12 +7685,10 @@ script.on_event("toggle-build-lock", function(event) end end) -script.on_event("toggle-vanilla-mode",function(event) +script.on_event("toggle-vanilla-mode", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - game.get_player(pindex).play_sound{path = "utility/confirm"} + if not check_for_player(pindex) then return end + game.get_player(pindex).play_sound({ path = "utility/confirm" }) if players[pindex].vanilla_mode == false then game.get_player(pindex).print("Vanilla mode : ON") players[pindex].cursor = false @@ -7340,11 +7705,9 @@ script.on_event("toggle-vanilla-mode",function(event) end end) -script.on_event("toggle-cursor-hiding",function(event) +script.on_event("toggle-cursor-hiding", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end if players[pindex].hide_cursor == nil or players[pindex].hide_cursor == false then players[pindex].hide_cursor = true printout("Cursor hiding enabled", pindex) @@ -7356,11 +7719,9 @@ script.on_event("toggle-cursor-hiding",function(event) end end) -script.on_event("clear-renders",function(event) +script.on_event("clear-renders", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end game.get_player(pindex).gui.screen.clear() rendering.clear() @@ -7374,32 +7735,26 @@ script.on_event("clear-renders",function(event) player.custom_GUI_frame = nil player.custom_GUI_sprite = nil end - printout("Cleared renders",pindex) + printout("Cleared renders", pindex) end) -script.on_event("recalibrate-zoom",function(event) +script.on_event("recalibrate-zoom", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end fa_zoom.fix_zoom(pindex) fa_graphics.sync_build_cursor_graphics(pindex) - printout("Recalibrated",pindex) + printout("Recalibrated", pindex) end) -script.on_event("enable-mouse-update-entity-selection",function(event) +script.on_event("enable-mouse-update-entity-selection", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end game.get_player(pindex).game_view_settings.update_entity_selection = true end) -script.on_event("pipette-tool-info",function(event) +script.on_event("pipette-tool-info", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local ent = get_selected_ent(pindex) local p = game.get_player(pindex) if ent and ent.valid then @@ -7408,93 +7763,67 @@ script.on_event("pipette-tool-info",function(event) players[pindex].building_direction = ent.direction players[pindex].cursor_rotation_offset = 0 end - if players[pindex].cursor then - players[pindex].cursor_pos = fa_utils.get_ent_northwest_corner_position(ent) - end + if players[pindex].cursor then players[pindex].cursor_pos = fa_utils.get_ent_northwest_corner_position(ent) end fa_graphics.sync_build_cursor_graphics(pindex) fa_graphics.draw_cursor_highlight(pindex, ent, nil, nil) end end) -script.on_event("copy-entity-settings-info",function(event) +script.on_event("copy-entity-settings-info", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local ent = get_selected_ent(pindex) local p = game.get_player(pindex) - if ent and ent.valid then - p.selected = ent - end + if ent and ent.valid then p.selected = ent end end) -script.on_event("paste-entity-settings-info",function(event) +script.on_event("paste-entity-settings-info", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local ent = get_selected_ent(pindex) local p = game.get_player(pindex) - if ent and ent.valid then - p.selected = ent - end + if ent and ent.valid then p.selected = ent end end) -script.on_event("fast-entity-transfer-info",function(event) +script.on_event("fast-entity-transfer-info", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local ent = get_selected_ent(pindex) local p = game.get_player(pindex) - if ent and ent.valid then - p.selected = ent - end + if ent and ent.valid then p.selected = ent end end) -script.on_event("fast-entity-split-info",function(event) +script.on_event("fast-entity-split-info", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local ent = get_selected_ent(pindex) local p = game.get_player(pindex) - if ent and ent.valid then - p.selected = ent - end + if ent and ent.valid then p.selected = ent end end) -script.on_event("drop-cursor-info",function(event) +script.on_event("drop-cursor-info", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local ent = get_selected_ent(pindex) local p = game.get_player(pindex) - if ent and ent.valid then - p.selected = ent - end + if ent and ent.valid then p.selected = ent end end) -script.on_event("read-hand",function(event) +script.on_event("read-hand", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end read_hand(pindex) end) --Empties hand and opens the item from the player/building inventory -script.on_event("locate-hand-in-inventory",function(event) +script.on_event("locate-hand-in-inventory", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end if players[pindex].in_menu == false then locate_hand_in_player_inventory(pindex) elseif players[pindex].menu == "inventory" then locate_hand_in_player_inventory(pindex) - elseif (players[pindex].menu == "building" or players[pindex].menu == "vehicle") then + elseif players[pindex].menu == "building" or players[pindex].menu == "vehicle" then locate_hand_in_building_output_inventory(pindex) else printout("Cannot locate items in this menu", pindex) @@ -7502,72 +7831,50 @@ script.on_event("locate-hand-in-inventory",function(event) end) --Empties hand and opens the item from the crafting menu -script.on_event("locate-hand-in-crafting-menu",function(event) +script.on_event("locate-hand-in-crafting-menu", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end locate_hand_in_crafting_menu(pindex) end) --ENTER KEY by default -script.on_event("menu-search-open",function(event) +script.on_event("menu-search-open", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - if players[pindex].in_menu == false then - return - end - if players[pindex].menu == "train_menu" then - return - end - if game.get_player(pindex).vehicle ~= nil then - return - end - if event.tick - players[pindex].last_menu_search_tick < 5 then - return - end + if not check_for_player(pindex) then return end + if players[pindex].in_menu == false then return end + if players[pindex].menu == "train_menu" then return end + if game.get_player(pindex).vehicle ~= nil then return end + if event.tick - players[pindex].last_menu_search_tick < 5 then return end fa_menu_search.open_search_box(pindex) end) -script.on_event("menu-search-get-next",function(event) +script.on_event("menu-search-get-next", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - if players[pindex].in_menu == false then - return - end + if not check_for_player(pindex) then return end + if players[pindex].in_menu == false then return end local str = players[pindex].menu_search_term if str == nil or str == "" then - printout("Press 'CONTROL + F' to start typing in a search term",pindex) + printout("Press 'CONTROL + F' to start typing in a search term", pindex) return end - fa_menu_search.fetch_next(pindex,str) + fa_menu_search.fetch_next(pindex, str) end) -script.on_event("menu-search-get-last",function(event) +script.on_event("menu-search-get-last", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - if players[pindex].in_menu == false then - return - end + if not check_for_player(pindex) then return end + if players[pindex].in_menu == false then return end local str = players[pindex].menu_search_term if str == nil or str == "" then - printout("Press 'CONTROL + F' to start typing in a search term",pindex) + printout("Press 'CONTROL + F' to start typing in a search term", pindex) return end - fa_menu_search.fetch_last(pindex,str) + fa_menu_search.fetch_last(pindex, str) end) script.on_event("open-warnings-menu", function(event) pindex = event.player_index - if not check_for_player(pindex) or players[pindex].vanilla_mode then - return - end + if not check_for_player(pindex) or players[pindex].vanilla_mode then return end if players[pindex].in_menu == false or game.get_player(pindex).opened_gui_type == defines.gui_type.production then players[pindex].warnings.short = fa_warnings.scan_for_warnings(30, 30, pindex) players[pindex].warnings.medium = fa_warnings.scan_for_warnings(100, 100, pindex) @@ -7579,50 +7886,42 @@ script.on_event("open-warnings-menu", function(event) players[pindex].in_menu = true players[pindex].move_queue = {} game.get_player(pindex).selected = nil - game.get_player(pindex).play_sound{path = "Open-Inventory-Sound"} + game.get_player(pindex).play_sound({ path = "Open-Inventory-Sound" }) printout("Short Range: " .. players[pindex].warnings.short.summary, pindex) else - printout("Another menu is open. ",pindex) + printout("Another menu is open. ", pindex) end end) script.on_event("honk", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local p = game.get_player(pindex) if p.driving == true then local vehicle = p.vehicle if vehicle == nil or vehicle.valid == false then return elseif vehicle.type == "locomotive" or vehicle.train ~= nil then - game.play_sound{path = "train-honk-low-long", position = vehicle.position} + game.play_sound({ path = "train-honk-low-long", position = vehicle.position }) elseif vehicle.name == "tank" then - game.play_sound{path = "tank-honk", position = vehicle.position} + game.play_sound({ path = "tank-honk", position = vehicle.position }) else - game.play_sound{path = "car-honk", position = vehicle.position} + game.play_sound({ path = "car-honk", position = vehicle.position }) end end end) script.on_event("open-fast-travel-menu", function(event) pindex = event.player_index - if not check_for_player(pindex) or players[pindex].vanilla_mode then - return - end - if game.get_player(pindex).driving ~= true then - fa_travel.fast_travel_menu_open(pindex) - end + if not check_for_player(pindex) or players[pindex].vanilla_mode then return end + if game.get_player(pindex).driving ~= true then fa_travel.fast_travel_menu_open(pindex) end end) --GUI action confirmed, such as by pressing ENTER -script.on_event(defines.events.on_gui_confirmed,function(event) +script.on_event(defines.events.on_gui_confirmed, function(event) local pindex = event.player_index local p = game.get_player(pindex) - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end if players[pindex].cursor_jumping == true then --Jump the cursor players[pindex].cursor_jumping = false @@ -7634,7 +7933,7 @@ script.on_event(defines.events.on_gui_confirmed,function(event) local valid_coords = new_x ~= nil and new_y ~= nil --Change cursor position or return error if valid_coords then - players[pindex].cursor_pos = {x = new_x, y = new_y} + players[pindex].cursor_pos = { x = new_x, y = new_y } printout("Cursor jumped to " .. new_x .. ", " .. new_y, pindex) fa_graphics.draw_cursor_highlight(pindex) fa_graphics.sync_build_cursor_graphics(pindex) @@ -7649,17 +7948,13 @@ script.on_event(defines.events.on_gui_confirmed,function(event) players[pindex].menu = "none" players[pindex].in_menu = false --play sound - p.play_sound{path="Close-Inventory-Sound"} + p.play_sound({ path = "Close-Inventory-Sound" }) --Destroy text fields - if p.gui.screen["cursor-jump"] ~= nil then - p.gui.screen["cursor-jump"].destroy() - end - if p.opened ~= nil then - p.opened = nil - end + if p.gui.screen["cursor-jump"] ~= nil then p.gui.screen["cursor-jump"].destroy() end + if p.opened ~= nil then p.opened = nil end elseif players[pindex].menu == "circuit_network_menu" then - --Take the constant number + --Take the constant number local result = event.element.text if result ~= nil and result ~= "" then local constant = tonumber(result) @@ -7668,7 +7963,8 @@ script.on_event(defines.events.on_gui_confirmed,function(event) if valid_number then if players[pindex].signal_selector.ent.type == "constant-combinator" then --Constant combinators (set last signal value) - local success = constant_combinator_set_last_signal_count(constant, players[pindex].signal_selector.ent, pindex) + local success = + constant_combinator_set_last_signal_count(constant, players[pindex].signal_selector.ent, pindex) if success then printout("Set " .. result, pindex) else @@ -7679,11 +7975,17 @@ script.on_event(defines.events.on_gui_confirmed,function(event) local control = players[pindex].signal_selector.ent.get_control_behavior() local circuit_condition = control.circuit_condition local cond = control.circuit_condition.condition - cond.second_signal = nil--{name = nil, type = signal_type} + cond.second_signal = nil --{name = nil, type = signal_type} cond.constant = constant circuit_condition.condition = cond players[pindex].signal_selector.ent.get_control_behavior().circuit_condition = circuit_condition - printout("Set " .. result .. ", condition now checks if " .. read_circuit_condition(players[pindex].signal_selector.ent, true) , pindex) + printout( + "Set " + .. result + .. ", condition now checks if " + .. read_circuit_condition(players[pindex].signal_selector.ent, true), + pindex + ) end else printout("Invalid input", pindex) @@ -7697,48 +7999,55 @@ script.on_event(defines.events.on_gui_confirmed,function(event) players[pindex].menu = "none" players[pindex].in_menu = false --play sound - p.play_sound{path="Close-Inventory-Sound"} + p.play_sound({ path = "Close-Inventory-Sound" }) --Destroy text fields - if p.gui.screen["circuit-condition-constant"] ~= nil then - p.gui.screen["circuit-condition-constant"].destroy() - end - if p.opened ~= nil then - p.opened = nil - end + if p.gui.screen["circuit-condition-constant"] ~= nil then p.gui.screen["circuit-condition-constant"].destroy() end + if p.opened ~= nil then p.opened = nil end elseif players[pindex].menu == "travel" then --Edit a travel point local result = event.element.text - if result == nil or result == "" then - result = "blank" - end + if result == nil or result == "" then result = "blank" end if players[pindex].travel.creating then --Create new point players[pindex].travel.creating = false - table.insert(global.players[pindex].travel, {name = result, position = fa_utils.center_of_tile(players[pindex].position), description = "No description"}) + table.insert(global.players[pindex].travel, { + name = result, + position = fa_utils.center_of_tile(players[pindex].position), + description = "No description", + }) table.sort(global.players[pindex].travel, function(k1, k2) return k1.name < k2.name end) - printout("Fast travel point ".. result .. " created at " .. math.floor(players[pindex].position.x) .. ", " .. math.floor(players[pindex].position.y), pindex) + printout( + "Fast travel point " + .. result + .. " created at " + .. math.floor(players[pindex].position.x) + .. ", " + .. math.floor(players[pindex].position.y), + pindex + ) elseif players[pindex].travel.renaming then --Renaming selected point players[pindex].travel.renaming = false players[pindex].travel[players[pindex].travel.index.y].name = result fa_travel.read_travel_slot(pindex) elseif players[pindex].travel.describing then - --Save the new description + --Save the new description players[pindex].travel.describing = false players[pindex].travel[players[pindex].travel.index.y].description = result - printout("Description updated for point " .. players[pindex].travel[players[pindex].travel.index.y].name, pindex) + printout( + "Description updated for point " .. players[pindex].travel[players[pindex].travel.index.y].name, + pindex + ) end players[pindex].travel.index.x = 1 event.element.destroy() elseif players[pindex].train_menu.renaming == true then players[pindex].train_menu.renaming = false local result = event.element.text - if result == nil or result == "" then - result = "unknown" - end + if result == nil or result == "" then result = "unknown" end fa_trains.set_train_name(players[pindex].train_menu.locomotive.train, result) printout("Train renamed to " .. result .. ", menu closed.", pindex) event.element.destroy() @@ -7746,9 +8055,7 @@ script.on_event(defines.events.on_gui_confirmed,function(event) elseif players[pindex].spider_menu.renaming == true then players[pindex].spider_menu.renaming = false local result = event.element.text - if result == nil or result == "" then - result = "unknown" - end + if result == nil or result == "" then result = "unknown" end game.get_player(pindex).cursor_stack.connected_entity.entity_label = result printout("spidertron renamed to " .. result .. ", menu closed.", pindex) event.element.destroy() @@ -7756,9 +8063,7 @@ script.on_event(defines.events.on_gui_confirmed,function(event) elseif players[pindex].train_stop_menu.renaming == true then players[pindex].train_stop_menu.renaming = false local result = event.element.text - if result == nil or result == "" then - result = "unknown" - end + if result == nil or result == "" then result = "unknown" end players[pindex].train_stop_menu.stop.backer_name = result printout("Train stop renamed to " .. result .. ", menu closed.", pindex) event.element.destroy() @@ -7766,9 +8071,7 @@ script.on_event(defines.events.on_gui_confirmed,function(event) elseif players[pindex].roboport_menu.renaming == true then players[pindex].roboport_menu.renaming = false local result = event.element.text - if result == nil or result == "" then - result = "unknown" - end + if result == nil or result == "" then result = "unknown" end fa_bot_logistics.set_network_name(players[pindex].roboport_menu.port, result) printout("Network renamed to " .. result .. ", menu closed.", pindex) event.element.destroy() @@ -7778,7 +8081,7 @@ script.on_event(defines.events.on_gui_confirmed,function(event) event.element.focus() players[pindex].menu_search_term = term if term ~= "" then - printout("Searching for " .. term .. ", go through results with 'SHIFT + ENTER' or 'CONTROL + ENTER' ",pindex) + printout("Searching for " .. term .. ", go through results with 'SHIFT + ENTER' or 'CONTROL + ENTER' ", pindex) end event.element.destroy() players[pindex].menu_search_frame.destroy() @@ -7787,52 +8090,36 @@ script.on_event(defines.events.on_gui_confirmed,function(event) --Apply the new label players[pindex].blueprint_menu.edit_label = false local result = event.element.text - if result == nil or result == "" then - result = "unknown" - end - fa_blueprints.set_blueprint_label(p.cursor_stack,result) - printout("Blueprint label changed to " .. result , pindex) + if result == nil or result == "" then result = "unknown" end + fa_blueprints.set_blueprint_label(p.cursor_stack, result) + printout("Blueprint label changed to " .. result, pindex) event.element.destroy() - if p.gui.screen["blueprint-edit-label"] ~= nil then - p.gui.screen["blueprint-edit-label"].destroy() - end + if p.gui.screen["blueprint-edit-label"] ~= nil then p.gui.screen["blueprint-edit-label"].destroy() end elseif players[pindex].blueprint_menu.edit_description == true then - --Apply the new desc + --Apply the new desc players[pindex].blueprint_menu.edit_description = false local result = event.element.text - if result == nil or result == "" then - result = "unknown" - end - fa_blueprints.set_blueprint_description(p.cursor_stack,result) + if result == nil or result == "" then result = "unknown" end + fa_blueprints.set_blueprint_description(p.cursor_stack, result) printout("Blueprint description changed.", pindex) event.element.destroy() - if p.gui.screen["blueprint-edit-description"] ~= nil then - p.gui.screen["blueprint-edit-description"].destroy() - end + if p.gui.screen["blueprint-edit-description"] ~= nil then p.gui.screen["blueprint-edit-description"].destroy() end elseif players[pindex].blueprint_menu.edit_import == true then --Apply the new import players[pindex].blueprint_menu.edit_import = false local result = event.element.text - if result == nil or result == "" then - result = "unknown" - end + if result == nil or result == "" then result = "unknown" end fa_blueprints.apply_blueprint_import(pindex, result) event.element.destroy() - if p.gui.screen["blueprint-edit-import"] ~= nil then - p.gui.screen["blueprint-edit-import"].destroy() - end + if p.gui.screen["blueprint-edit-import"] ~= nil then p.gui.screen["blueprint-edit-import"].destroy() end elseif players[pindex].blueprint_menu.edit_export == true then --Instruct export players[pindex].blueprint_menu.edit_export = false local result = event.element.text - if result == nil or result == "" then - result = "unknown" - end - printout("Text box closed" , pindex) + if result == nil or result == "" then result = "unknown" end + printout("Text box closed", pindex) event.element.destroy() - if p.gui.screen["blueprint-edit-export"] ~= nil then - p.gui.screen["blueprint-edit-export"].destroy() - end + if p.gui.screen["blueprint-edit-export"] ~= nil then p.gui.screen["blueprint-edit-export"].destroy() end else --Stray text box, so do nothing and destroy it if event.element.parent then @@ -7846,9 +8133,7 @@ end) script.on_event("open-structure-travel-menu", function(event) local pindex = event.player_index - if not check_for_player(pindex) or players[pindex].vanilla_mode then - return - end + if not check_for_player(pindex) or players[pindex].vanilla_mode then return end if players[pindex].in_menu == false then game.get_player(pindex).selected = nil players[pindex].menu = "structure-travel" @@ -7859,11 +8144,11 @@ script.on_event("open-structure-travel-menu", function(event) local initial_scan_radius = 50 if ent ~= nil and ent.valid and ent.unit_number ~= nil and building_types[ent.type] then players[pindex].structure_travel.current = ent.unit_number - players[pindex].structure_travel.network = fa_travel.compile_building_network(ent, initial_scan_radius,pindex) + players[pindex].structure_travel.network = fa_travel.compile_building_network(ent, initial_scan_radius, pindex) else ent = game.get_player(pindex).character players[pindex].structure_travel.current = ent.unit_number - players[pindex].structure_travel.network = fa_travel.compile_building_network(ent, initial_scan_radius,pindex) + players[pindex].structure_travel.network = fa_travel.compile_building_network(ent, initial_scan_radius, pindex) end local description = "" local network = players[pindex].structure_travel.network @@ -7872,68 +8157,64 @@ script.on_event("open-structure-travel-menu", function(event) if network[current].north and #network[current].north > 0 then description = description .. ", " .. #network[current].north .. " connections north," end - if network[current].east and #network[current].east > 0 then + if network[current].east and #network[current].east > 0 then description = description .. ", " .. #network[current].east .. " connections east," end if network[current].south and #network[current].south > 0 then description = description .. ", " .. #network[current].south .. " connections south," end - if network[current].west and #network[current].west > 0 then + if network[current].west and #network[current].west > 0 then description = description .. ", " .. #network[current].west .. " connections west," end - if description == "" then - description = "No nearby buildings." - end - printout("Now at " .. ent.name .. " " .. fa_scanner.ent_extra_list_info(ent,pindex,true) .. " " .. description .. ", Select a direction, confirm with same direction, and use perpendicular directions to select a target, press left bracket to teleport to selection", pindex) + if description == "" then description = "No nearby buildings." end + printout( + "Now at " + .. ent.name + .. " " + .. fa_scanner.ent_extra_list_info(ent, pindex, true) + .. " " + .. description + .. ", Select a direction, confirm with same direction, and use perpendicular directions to select a target, press left bracket to teleport to selection", + pindex + ) local screen = game.get_player(pindex).gui.screen - local frame = screen.add{type = "frame", name = "structure-travel"} + local frame = screen.add({ type = "frame", name = "structure-travel" }) frame.bring_to_front() frame.force_auto_center() frame.focus() game.get_player(pindex).opened = frame else - printout("Another menu is open. ",pindex) + printout("Another menu is open. ", pindex) end - end) script.on_event("cursor-skip-north", function(event) local pindex = event.player_index - if not check_for_player(pindex) or players[pindex].vanilla_mode then - return - end + if not check_for_player(pindex) or players[pindex].vanilla_mode then return end cursor_skip(pindex, defines.direction.north) end) script.on_event("cursor-skip-south", function(event) local pindex = event.player_index - if not check_for_player(pindex) or players[pindex].vanilla_mode then - return - end + if not check_for_player(pindex) or players[pindex].vanilla_mode then return end cursor_skip(pindex, defines.direction.south) end) script.on_event("cursor-skip-west", function(event) local pindex = event.player_index - if not check_for_player(pindex) or players[pindex].vanilla_mode then - return - end + if not check_for_player(pindex) or players[pindex].vanilla_mode then return end cursor_skip(pindex, defines.direction.west) end) script.on_event("cursor-skip-east", function(event) local pindex = event.player_index - if not check_for_player(pindex) or players[pindex].vanilla_mode then - return - end + if not check_for_player(pindex) or players[pindex].vanilla_mode then return end cursor_skip(pindex, defines.direction.east) end) ---Runs the cursor skip iteration and reads out results +--Runs the cursor skip iteration and reads out results function cursor_skip(pindex, direction, iteration_limit) - if players[pindex].cursor == false then - return - end + if players[pindex].cursor == false then return end local p = game.get_player(pindex) local limit = iteration_limit or 100 local result = "" @@ -7945,29 +8226,29 @@ function cursor_skip(pindex, direction, iteration_limit) result = "Skipped " .. limit .. " tiles without a change, " --Play Sound if players[pindex].remote_view then - p.play_sound{path = "inventory-wrap-around", position = players[pindex].cursor_pos, volume_modifier = 1} + p.play_sound({ path = "inventory-wrap-around", position = players[pindex].cursor_pos, volume_modifier = 1 }) else - p.play_sound{path = "inventory-wrap-around", position = players[pindex].position, volume_modifier = 1} + p.play_sound({ path = "inventory-wrap-around", position = players[pindex].position, volume_modifier = 1 }) end elseif moved_count == 1 then --Play Sound if players[pindex].remote_view then - p.play_sound{path = "Close-Inventory-Sound", position = players[pindex].cursor_pos, volume_modifier = 1} + p.play_sound({ path = "Close-Inventory-Sound", position = players[pindex].cursor_pos, volume_modifier = 1 }) else - p.play_sound{path = "Close-Inventory-Sound", position = players[pindex].position, volume_modifier = 1} + p.play_sound({ path = "Close-Inventory-Sound", position = players[pindex].position, volume_modifier = 1 }) end elseif moved_count > 1 then - --Change found, with more than 1 tile moved + --Change found, with more than 1 tile moved result = "Skipped " .. moved_count .. " tiles, " --Play Sound if players[pindex].remote_view then - p.play_sound{path = "inventory-wrap-around", position = players[pindex].cursor_pos, volume_modifier = 1} + p.play_sound({ path = "inventory-wrap-around", position = players[pindex].cursor_pos, volume_modifier = 1 }) else - p.play_sound{path = "inventory-wrap-around", position = players[pindex].position, volume_modifier = 1} + p.play_sound({ path = "inventory-wrap-around", position = players[pindex].position, volume_modifier = 1 }) end end - --Read the tile reached + --Read the tile reached read_tile(pindex, result) fa_graphics.sync_build_cursor_graphics(pindex) end @@ -7984,10 +8265,10 @@ function cursor_skip_iteration(pindex, direction, iteration_limit) --For underground belts and pipes in the relevant direction, apply a special case where you jump to the underground neighbour if start ~= nil and start.valid and start.type == "pipe-to-ground" then local connections = start.fluidbox.get_pipe_connections(1) - for i,con in ipairs(connections) do + for i, con in ipairs(connections) do if con.target ~= nil then - local dist = math.ceil(util.distance(start.position,con.target.get_pipe_connections(1)[1].position)) - local dir_neighbor = fa_utils.get_direction_biased(con.target_position,start.position) + local dist = math.ceil(util.distance(start.position, con.target.get_pipe_connections(1)[1].position)) + local dir_neighbor = fa_utils.get_direction_biased(con.target_position, start.position) if con.connection_type == "underground" and dir_neighbor == direction then players[pindex].cursor_pos = con.target.get_pipe_connections(1)[1].position refresh_player_tile(pindex) @@ -8000,8 +8281,8 @@ function cursor_skip_iteration(pindex, direction, iteration_limit) local neighbour = start.neighbours if neighbour then local other_end = neighbour - local dist = math.ceil(util.distance(start.position,other_end.position)) - local dir_neighbor = fa_utils.get_direction_biased(other_end.position,start.position) + local dist = math.ceil(util.distance(start.position, other_end.position)) + local dir_neighbor = fa_utils.get_direction_biased(other_end.position, start.position) if dir_neighbor == direction then players[pindex].cursor_pos = other_end.position refresh_player_tile(pindex) @@ -8010,7 +8291,7 @@ function cursor_skip_iteration(pindex, direction, iteration_limit) end end end - --Iterate first tile + --Iterate first tile players[pindex].cursor_pos = fa_utils.offset_position(players[pindex].cursor_pos, direction, 1) refresh_player_tile(pindex) current = get_selected_ent(pindex) @@ -8019,7 +8300,7 @@ function cursor_skip_iteration(pindex, direction, iteration_limit) while moved < limit do if current == nil or current.valid == false then if start == nil or start.valid == false then - --Both are nil: skip + --Both are nil: skip else --Valid start to nil return moved @@ -8033,7 +8314,7 @@ function cursor_skip_iteration(pindex, direction, iteration_limit) if start.unit_number == current.unit_number then --They are the same ent: skip else - --They are differemt ents + --They are differemt ents if start.name ~= current.name then --They have different names: return --p.print("RET 1, start: " .. start.name .. ", current: " .. current.name .. ", comment:" .. comment)-- @@ -8057,7 +8338,11 @@ function cursor_skip_iteration(pindex, direction, iteration_limit) local start_output_neighbors = #start.belt_neighbours["outputs"] local current_input_neighbors = #current.belt_neighbours["inputs"] local current_output_neighbors = #current.belt_neighbours["outputs"] - if start_input_neighbors ~= current_input_neighbors or start_output_neighbors ~= current_output_neighbors or start.belt_shape ~= current.belt_shape then + if + start_input_neighbors ~= current_input_neighbors + or start_output_neighbors ~= current_output_neighbors + or start.belt_shape ~= current.belt_shape + then --p.print("RET 3, start: " .. start.name .. ", current: " .. current.name .. ", comment:" .. comment)-- return moved end @@ -8080,26 +8365,24 @@ function cursor_skip_iteration(pindex, direction, iteration_limit) end script.on_event("nudge-up", function(event) - fa_building_tools.nudge_key(defines.direction.north,event) + fa_building_tools.nudge_key(defines.direction.north, event) end) script.on_event("nudge-down", function(event) - fa_building_tools.nudge_key(defines.direction.south,event) + fa_building_tools.nudge_key(defines.direction.south, event) end) script.on_event("nudge-left", function(event) - fa_building_tools.nudge_key(defines.direction.west,event) + fa_building_tools.nudge_key(defines.direction.west, event) end) script.on_event("nudge-right", function(event) - fa_building_tools.nudge_key(defines.direction.east,event) + fa_building_tools.nudge_key(defines.direction.east, event) end) script.on_event("alternative-menu-up", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end if players[pindex].in_menu and players[pindex].menu == "train_menu" then fa_trains.menu_up(pindex) elseif players[pindex].in_menu and players[pindex].menu == "spider_menu" then @@ -8107,11 +8390,9 @@ script.on_event("alternative-menu-up", function(event) end end) -script.on_event("alternative-menu-down", function(event) - local pindex = event.player_index - if not check_for_player(pindex) then - return - end +script.on_event("alternative-menu-down", function(event) + local pindex = event.player_index + if not check_for_player(pindex) then return end if players[pindex].in_menu and players[pindex].menu == "train_menu" then fa_trains.menu_down(pindex) elseif players[pindex].in_menu and players[pindex].menu == "spider_menu" then @@ -8121,162 +8402,122 @@ end) script.on_event("alternative-menu-left", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end - if players[pindex].in_menu and players[pindex].menu == "train_menu" then - fa_trains.menu_left(pindex) - end + if not check_for_player(pindex) then return end + if players[pindex].in_menu and players[pindex].menu == "train_menu" then fa_trains.menu_left(pindex) end end) script.on_event("alternative-menu-right", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end - if players[pindex].in_menu and players[pindex].menu == "train_menu" then - fa_trains.menu_right(pindex) - end + if not check_for_player(pindex) then return end + if players[pindex].in_menu and players[pindex].menu == "train_menu" then fa_trains.menu_right(pindex) end end) script.on_event("cursor-one-tile-north", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end - if players[pindex].cursor then - move_key(dirs.north,event, true) - end + if not check_for_player(pindex) then return end + if players[pindex].cursor then move_key(dirs.north, event, true) end end) script.on_event("cursor-one-tile-south", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end - if players[pindex].cursor then - move_key(dirs.south,event, true) - end + if not check_for_player(pindex) then return end + if players[pindex].cursor then move_key(dirs.south, event, true) end end) script.on_event("cursor-one-tile-east", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end - if players[pindex].cursor then - move_key(dirs.east,event, true) - end + if not check_for_player(pindex) then return end + if players[pindex].cursor then move_key(dirs.east, event, true) end end) script.on_event("cursor-one-tile-west", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end - if players[pindex].cursor then - move_key(dirs.west,event, true) - end + if not check_for_player(pindex) then return end + if players[pindex].cursor then move_key(dirs.west, event, true) end end) script.on_event("set-splitter-input-priority-left", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local ent = get_selected_ent(pindex) if not ent then return elseif ent.valid and ent.type == "splitter" then local result = fa_belts.set_splitter_priority(ent, true, true, nil) - printout(result,pindex) + printout(result, pindex) end end) script.on_event("set-splitter-input-priority-right", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end - local ent = get_selected_ent(pindex) + if not check_for_player(pindex) then return end + local ent = get_selected_ent(pindex) if not ent then return elseif ent.valid and ent.type == "splitter" then local result = fa_belts.set_splitter_priority(ent, true, false, nil) - printout(result,pindex) + printout(result, pindex) end end) script.on_event("set-splitter-output-priority-left", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local ent = get_selected_ent(pindex) - if not ent then - return - end + if not ent then return end if ent.valid and ent.type == "splitter" then local result = fa_belts.set_splitter_priority(ent, false, true, nil) - printout(result,pindex) + printout(result, pindex) end end) script.on_event("set-splitter-output-priority-right", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local ent = get_selected_ent(pindex) - if not ent then - return - end + if not ent then return end --Build left turns on end rails if ent.valid and ent.type == "splitter" then local result = fa_belts.set_splitter_priority(ent, false, false, nil) - printout(result,pindex) + printout(result, pindex) end end) --Sets splitter filter and also contant combinator signals script.on_event("set-entity-filter-from-hand", function(event) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end if players[pindex].in_menu then return else --Not in a menu local stack = game.get_player(pindex).cursor_stack - local ent = get_selected_ent(pindex) - if ent == nil or ent.valid == false then - return - end + local ent = get_selected_ent(pindex) + if ent == nil or ent.valid == false then return end if stack == nil or not stack.valid_for_read or not stack.valid then if ent.type == "splitter" then --Clear the filter local result = fa_belts.set_splitter_priority(ent, nil, nil, nil, true) - printout(result,pindex) + printout(result, pindex) elseif ent.type == "constant-combinator" then --Remove the last signal constant_combinator_remove_last_signal(ent, pindex) elseif ent.type == "inserter" then local result = set_inserter_filter_by_hand(pindex, ent) - printout(result,pindex) + printout(result, pindex) end else if ent.type == "splitter" then --Set the filter local result = fa_belts.set_splitter_priority(ent, nil, nil, stack) - printout(result,pindex) + printout(result, pindex) elseif ent.type == "constant-combinator" then --Add a new signal constant_combinator_add_stack_signal(ent, stack, pindex) elseif ent.type == "inserter" then local result = set_inserter_filter_by_hand(pindex, ent) - printout(result,pindex) + printout(result, pindex) end end end @@ -8286,9 +8527,7 @@ end) script.on_event("connect-rail-vehicles", function(event) local pindex = event.player_index local vehicle = nil - if not check_for_player(pindex) or players[pindex].in_menu then - return - end + if not check_for_player(pindex) or players[pindex].in_menu then return end local ent = get_selected_ent(pindex) if game.get_player(pindex).vehicle ~= nil and game.get_player(pindex).vehicle.train ~= nil then vehicle = game.get_player(pindex).vehicle @@ -8299,22 +8538,14 @@ script.on_event("connect-rail-vehicles", function(event) if vehicle ~= nil then --Connect rolling stock (or check if the default key bindings make the connection) local connected = 0 - if vehicle.connect_rolling_stock(defines.rail_direction.front) then - connected = connected + 1 - end - if vehicle.connect_rolling_stock(defines.rail_direction.back) then - connected = connected + 1 - end + if vehicle.connect_rolling_stock(defines.rail_direction.front) then connected = connected + 1 end + if vehicle.connect_rolling_stock(defines.rail_direction.back) then connected = connected + 1 end if connected > 0 then printout("Connected this vehicle.", pindex) else connected = 0 - if vehicle.get_connected_rolling_stock(defines.rail_direction.front) ~= nil then - connected = connected + 1 - end - if vehicle.get_connected_rolling_stock(defines.rail_direction.back) ~= nil then - connected = connected + 1 - end + if vehicle.get_connected_rolling_stock(defines.rail_direction.front) ~= nil then connected = connected + 1 end + if vehicle.get_connected_rolling_stock(defines.rail_direction.back) ~= nil then connected = connected + 1 end if connected > 0 then printout("Connected this vehicle.", pindex) else @@ -8328,9 +8559,7 @@ end) script.on_event("disconnect-rail-vehicles", function(event) local pindex = event.player_index local vehicle = nil - if not check_for_player(pindex) or players[pindex].in_menu then - return - end + if not check_for_player(pindex) or players[pindex].in_menu then return end local ent = get_selected_ent(pindex) if game.get_player(pindex).vehicle ~= nil and game.get_player(pindex).vehicle.train ~= nil then vehicle = game.get_player(pindex).vehicle @@ -8341,22 +8570,14 @@ script.on_event("disconnect-rail-vehicles", function(event) if vehicle ~= nil then --Disconnect rolling stock local disconnected = 0 - if vehicle.disconnect_rolling_stock(defines.rail_direction.front) then - disconnected = disconnected + 1 - end - if vehicle.disconnect_rolling_stock(defines.rail_direction.back) then - disconnected = disconnected + 1 - end + if vehicle.disconnect_rolling_stock(defines.rail_direction.front) then disconnected = disconnected + 1 end + if vehicle.disconnect_rolling_stock(defines.rail_direction.back) then disconnected = disconnected + 1 end if disconnected > 0 then printout("Disconnected this vehicle.", pindex) else local connected = 0 - if vehicle.get_connected_rolling_stock(defines.rail_direction.front) ~= nil then - connected = connected + 1 - end - if vehicle.get_connected_rolling_stock(defines.rail_direction.back) ~= nil then - connected = connected + 1 - end + if vehicle.get_connected_rolling_stock(defines.rail_direction.front) ~= nil then connected = connected + 1 end + if vehicle.get_connected_rolling_stock(defines.rail_direction.back) ~= nil then connected = connected + 1 end if connected > 0 then printout("Disconnection error.", pindex) else @@ -8369,127 +8590,122 @@ end) script.on_event("inventory-read-armor-stats", function(event) local pindex = event.player_index local vehicle = nil - if not check_for_player(pindex) or not players[pindex].in_menu then - return - end - if (players[pindex].in_menu and players[pindex].menu == "inventory") or (players[pindex].menu == "vehicle" and game.get_player(pindex).opened.type == "spider-vehicle") then - local result = fa_equipment.read_armor_stats(pindex) - --game.get_player(pindex).print(result)-- - printout(result,pindex) + if not check_for_player(pindex) or not players[pindex].in_menu then return end + if + (players[pindex].in_menu and players[pindex].menu == "inventory") + or (players[pindex].menu == "vehicle" and game.get_player(pindex).opened.type == "spider-vehicle") + then + local result = fa_equipment.read_armor_stats(pindex) + --game.get_player(pindex).print(result)-- + printout(result, pindex) end end) script.on_event("inventory-read-equipment-list", function(event) local pindex = event.player_index local vehicle = nil - if not check_for_player(pindex) or not players[pindex].in_menu then - return - end - if (players[pindex].in_menu and players[pindex].menu == "inventory") or (players[pindex].menu == "vehicle" and game.get_player(pindex).opened.type == "spider-vehicle") then - local result = fa_equipment.read_equipment_list(pindex) - --game.get_player(pindex).print(result)-- - printout(result,pindex) + if not check_for_player(pindex) or not players[pindex].in_menu then return end + if + (players[pindex].in_menu and players[pindex].menu == "inventory") + or (players[pindex].menu == "vehicle" and game.get_player(pindex).opened.type == "spider-vehicle") + then + local result = fa_equipment.read_equipment_list(pindex) + --game.get_player(pindex).print(result)-- + printout(result, pindex) end end) script.on_event("inventory-remove-all-equipment-and-armor", function(event) local pindex = event.player_index local vehicle = nil - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end - if (players[pindex].in_menu and players[pindex].menu == "inventory") or (players[pindex].menu == "vehicle" and game.get_player(pindex).opened.type == "spider-vehicle") then - local result = fa_equipment.remove_equipment_and_armor(pindex) - --game.get_player(pindex).print(result)-- - printout(result,pindex) + if + (players[pindex].in_menu and players[pindex].menu == "inventory") + or (players[pindex].menu == "vehicle" and game.get_player(pindex).opened.type == "spider-vehicle") + then + local result = fa_equipment.remove_equipment_and_armor(pindex) + --game.get_player(pindex).print(result)-- + printout(result, pindex) end - end) -script.on_event("shoot-weapon-fa", function(event) --WIP todo*** consumes shoot event and so it can simply not shoot if atomic bomb in range - local pindex = event.player_index - if not check_for_player(pindex) then - return - end - local p = game.get_player(pindex) - if p.character == nil then - return - end - local p = game.get_player(pindex) - local main_inv = p.get_inventory(defines.inventory.character_main) - local ammo_inv = p.get_inventory(defines.inventory.character_ammo) - local ammos_count = #ammo_inv - ammo_inv.count_empty_stacks() - local selected_ammo = ammo_inv[p.character.selected_gun_index] - local target_pos = p.shooting_state.position - local abort_missle = false - local abort_message = "" - - if selected_ammo == nil or selected_ammo.valid_for_read == false then - return - end +script.on_event( + "shoot-weapon-fa", + function(event) --WIP todo*** consumes shoot event and so it can simply not shoot if atomic bomb in range + local pindex = event.player_index + if not check_for_player(pindex) then return end + local p = game.get_player(pindex) + if p.character == nil then return end + local p = game.get_player(pindex) + local main_inv = p.get_inventory(defines.inventory.character_main) + local ammo_inv = p.get_inventory(defines.inventory.character_ammo) + local ammos_count = #ammo_inv - ammo_inv.count_empty_stacks() + local selected_ammo = ammo_inv[p.character.selected_gun_index] + local target_pos = p.shooting_state.position + local abort_missle = false + local abort_message = "" + + if selected_ammo == nil or selected_ammo.valid_for_read == false then return end + + if target_pos == nil or util.distance(p.position, target_pos) < 1.5 then + target_pos = players[pindex].cursor_pos + p.shooting_state.position = players[pindex].cursor_pos + if selected_ammo.name == "atomic-bomb" then + abort_missle = true + abort_message = "Aiming alert, scroll mouse wheel to zoom out." + end + end - if target_pos == nil or util.distance(p.position, target_pos) < 1.5 then - target_pos = players[pindex].cursor_pos - p.shooting_state.position = players[pindex].cursor_pos - if selected_ammo.name == "atomic-bomb" then + local aim_dist_1 = util.distance(p.position, target_pos) + local aim_dist_2 = util.distance(p.position, players[pindex].cursor_pos) + if aim_dist_1 < 1.5 and selected_ammo.name == "atomic-bomb" then abort_missle = true abort_message = "Aiming alert, scroll mouse wheel to zoom out." + elseif util.distance(target_pos, players[pindex].cursor_pos) > 2 and selected_ammo.name == "atomic-bomb" then + abort_missle = true + abort_message = "Aiming alert, move cursor to sync mouse." end - end - - local aim_dist_1 = util.distance(p.position, target_pos) - local aim_dist_2 = util.distance(p.position, players[pindex].cursor_pos) - if aim_dist_1 < 1.5 and selected_ammo.name == "atomic-bomb" then - abort_missle = true - abort_message = "Aiming alert, scroll mouse wheel to zoom out." - elseif util.distance(target_pos, players[pindex].cursor_pos) > 2 and selected_ammo.name == "atomic-bomb" then - abort_missle = true - abort_message = "Aiming alert, move cursor to sync mouse." - end - if (aim_dist_1 < 35 or aim_dist_2 < 35) and selected_ammo.name == "atomic-bomb" then - abort_missle = true - abort_message = "Range alert, target too close, hold to fire anyway." - end - --p.print("abort check") - if abort_missle then - - --Remove all atomic bombs - fa_equipment.delete_equipped_atomic_bombs(pindex) + if (aim_dist_1 < 35 or aim_dist_2 < 35) and selected_ammo.name == "atomic-bomb" then + abort_missle = true + abort_message = "Range alert, target too close, hold to fire anyway." + end + --p.print("abort check") + if abort_missle then + --Remove all atomic bombs + fa_equipment.delete_equipped_atomic_bombs(pindex) - --Warn the player - p.play_sound{path = "utility/cannot_build"} - printout(abort_message, pindex) + --Warn the player + p.play_sound({ path = "utility/cannot_build" }) + printout(abort_message, pindex) - --Schedule to restore the items on a later tick - schedule(310, "call_to_restore_equipped_atomic_bombs", pindex) - else - --Suppress alerts for 10 seconds? + --Schedule to restore the items on a later tick + schedule(310, "call_to_restore_equipped_atomic_bombs", pindex) + else + --Suppress alerts for 10 seconds? + end end - -end) +) --Attempt to launch a rocket script.on_event("launch-rocket", function(event) local pindex = event.player_index local ent = get_selected_ent(pindex) - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end --For rocket entities, return the silo instead if ent and (ent.name == "rocket-silo-rocket-shadow" or ent.name == "rocket-silo-rocket") then - local ents = ent.surface.find_entities_filtered{position = ent.position, radius = 20, name = "rocket-silo"} - for i,silo in ipairs(ents) do - ent = silo + local ents = ent.surface.find_entities_filtered({ position = ent.position, radius = 20, name = "rocket-silo" }) + for i, silo in ipairs(ents) do + ent = silo end end --Try to launch from the silo if ent ~= nil and ent.valid and ent.name == "rocket-silo" then local try_launch = ent.launch_rocket() if try_launch then - printout("Launch successful!",pindex) + printout("Launch successful!", pindex) else - printout("Not ready to launch!",pindex) + printout("Not ready to launch!", pindex) end end end) @@ -8497,69 +8713,53 @@ end) --Help key and tutorial system WIP script.on_event("help-read", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end fa_tutorial.read_current_step(pindex) end) script.on_event("help-next", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end fa_tutorial.next_step(pindex) end) script.on_event("help-back", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end fa_tutorial.prev_step(pindex) end) script.on_event("help-chapter-next", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end fa_tutorial.next_chapter(pindex) end) script.on_event("help-chapter-back", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end fa_tutorial.prev_chapter(pindex) end) script.on_event("help-toggle-header-mode", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end fa_tutorial.toggle_header_detail(pindex) end) script.on_event("help-get-other", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end fa_tutorial.read_other_once(pindex) end) --**Use this key to test stuff (ALT-G) script.on_event("debug-test-key", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local p = game.get_player(pindex) local pex = players[pindex] - local ent = get_selected_ent(pindex) + local ent = get_selected_ent(pindex) local stack = game.get_player(pindex).cursor_stack --get_blueprint_corners(pindex, true) @@ -8576,67 +8776,49 @@ end) script.on_event("logistic-request-read", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end - if game.get_player(pindex).driving == false then - fa_bot_logistics.logistics_info_key_handler(pindex) - end + if not check_for_player(pindex) then return end + if game.get_player(pindex).driving == false then fa_bot_logistics.logistics_info_key_handler(pindex) end end) script.on_event("logistic-request-increment-min", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end fa_bot_logistics.logistics_request_increment_min_handler(pindex) end) script.on_event("logistic-request-decrement-min", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end fa_bot_logistics.logistics_request_decrement_min_handler(pindex) end) script.on_event("logistic-request-increment-max", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end fa_bot_logistics.logistics_request_increment_max_handler(pindex) end) script.on_event("logistic-request-decrement-max", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end fa_bot_logistics.logistics_request_decrement_max_handler(pindex) end) script.on_event("logistic-request-toggle-personal-logistics", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end fa_bot_logistics.logistics_request_toggle_handler(pindex) end) script.on_event("send-selected-stack-to-logistic-trash", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end fa_bot_logistics.send_selected_stack_to_logistic_trash(pindex) end) script.on_event(defines.events.on_gui_opened, function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local p = game.get_player(pindex) players[pindex].move_queue = {} @@ -8649,7 +8831,11 @@ script.on_event(defines.events.on_gui_opened, function(event) p.selected = nil --GUI mismatch checks - if event.gui_type == defines.gui_type.controller and players[pindex].menu == "none" and event.tick - players[pindex].last_menu_toggle_tick < 5 then + if + event.gui_type == defines.gui_type.controller + and players[pindex].menu == "none" + and event.tick - players[pindex].last_menu_toggle_tick < 5 + then --If closing another menu toggles the player GUI screen, we close this screen p.opened = nil --game.print("Closed an extra controller GUI",{volume_modifier = 0})--**checks GUI shenanigans @@ -8660,13 +8846,11 @@ script.on_event(defines.events.on_gui_opened, function(event) end end) -script.on_event(defines.events.on_chunk_charted,function(event) +script.on_event(defines.events.on_chunk_charted, function(event) pindex = event.force.players[1].index if not check_for_player(pindex) then end - if players[pindex].mapped[fa_utils.pos2str(event.position)] ~= nil then - return - end + if players[pindex].mapped[fa_utils.pos2str(event.position)] ~= nil then return end players[pindex].mapped[fa_utils.pos2str(event.position)] = true local islands = fa_scanner.find_islands(game.surfaces[event.surface_index], event.area, pindex) @@ -8677,16 +8861,16 @@ script.on_event(defines.events.on_chunk_charted,function(event) patches = {}, queue = {}, index = 1, - positions = {} + positions = {}, } end local merged_groups = {} local many2many = {} if players[pindex].resources[i].queue[fa_utils.pos2str(event.position)] ~= nil then for dir, positions in pairs(players[pindex].resources[i].queue[fa_utils.pos2str(event.position)]) do --- islands[i].neighbors[dir] = nil + -- islands[i].neighbors[dir] = nil for i3, pos in pairs(positions) do - local dirs = {dir - 1, dir, dir + 1} + local dirs = { dir - 1, dir, dir + 1 } if dir == 0 then dirs[1] = 7 end local new_edges = {} for i1, d in ipairs(dirs) do @@ -8701,19 +8885,14 @@ script.on_event(defines.events.on_chunk_charted,function(event) if new_edges[p] then if islands[i].resources[p] ~= nil then local island_group = islands[i].resources[p].group - if merged_groups[island_group] == nil then - merged_groups[island_group] = {} - end + if merged_groups[island_group] == nil then merged_groups[island_group] = {} end merged_groups[island_group][players[pindex].resources[i].positions[pos]] = true else edge = true end else - if players[pindex].resources[i].positions[p] == nil then - edge = true - end + if players[pindex].resources[i].positions[p] == nil then edge = true end end - end if edge == false then local group = players[pindex].resources[i].positions[pos] @@ -8733,14 +8912,12 @@ script.on_event(defines.events.on_chunk_charted,function(event) end if edge == false then islands[i].resources[p].edge = false - islands[i].edges[p]= nil + islands[i].edges[p] = nil else - islands[i].edges[p]= false + islands[i].edges[p] = false end end - end - end end end @@ -8749,9 +8926,7 @@ script.on_event(defines.events.on_chunk_charted,function(event) for i1, ref in ipairs(many2many) do local match = false for i2, v2 in pairs(resource_groups) do - if match then - break - end + if match then break end for i3, v3 in pairs(ref["old"]) do if i2 == i3 then table.insert(matches, i1) @@ -8762,7 +8937,7 @@ script.on_event(defines.events.on_chunk_charted,function(event) end end ---@diagnostic disable-next-line: undefined-global - local old = table.deepcopy(resource_group)--todo debug: maybe this was meant to be "island_group"? *** + local old = table.deepcopy(resource_group) --todo debug: maybe this was meant to be "island_group"? *** if old ~= nil then local new = {} new[island_group] = true @@ -8774,7 +8949,7 @@ script.on_event(defines.events.on_chunk_charted,function(event) else table.sort(matches, function(k1, k2) return k1 > k2 - end) + end) for i1, merge_index in ipairs(matches) do for i2, v2 in pairs(many2many[merge_index]["old"]) do @@ -8807,12 +8982,22 @@ script.on_event(defines.events.on_chunk_charted,function(event) new_group = math.min(new_group, resource_group) end for resource_group, b in pairs(resource_groups) do - if new_group < resource_group and players[pindex].resources[i].patches ~= nil and players[pindex].resources[i].patches[resource_group] ~= nil and islands[i] ~= nil and islands[i].resources ~= nil and islands[i].resources[b] ~= nil then--**beta changed "p" to "b" + if + new_group < resource_group + and players[pindex].resources[i].patches ~= nil + and players[pindex].resources[i].patches[resource_group] ~= nil + and islands[i] ~= nil + and islands[i].resources ~= nil + and islands[i].resources[b] ~= nil + then --**beta changed "p" to "b" for i1, pos in pairs(players[pindex].resources[i].patches[resource_group].positions) do players[pindex].resources[i].positions[pos] = new_group - players[pindex].resources[i].count = islands[i].resources[b].count--**beta "p" to "b" + players[pindex].resources[i].count = islands[i].resources[b].count --**beta "p" to "b" end - fa_utils.table_concat(players[pindex].resources[i].patches[new_group].positions, players[pindex].resources[i].patches[resource_group].positions) + fa_utils.table_concat( + players[pindex].resources[i].patches[new_group].positions, + players[pindex].resources[i].patches[resource_group].positions + ) for pos, val in pairs(players[pindex].resources[i].patches[resource_group].edges) do players[pindex].resources[i].patches[new_group].edges[pos] = val end @@ -8821,7 +9006,9 @@ script.on_event(defines.events.on_chunk_charted,function(event) end for pos, val in pairs(islands[i].groups[island_group]) do players[pindex].resources[i].positions[pos] = new_group -if 'number' == type(players[pindex].resources[i].patches[new_group]) then new_group = players[pindex].resources[i].patches[new_group] end + if "number" == type(players[pindex].resources[i].patches[new_group]) then + new_group = players[pindex].resources[i].patches[new_group] + end table.insert(players[pindex].resources[i].patches[new_group].positions, pos) if islands[i].edges[pos] ~= nil then players[pindex].resources[i].patches[new_group].edges[pos] = islands[i].edges[pos] @@ -8832,17 +9019,17 @@ if 'number' == type(players[pindex].resources[i].patches[new_group]) then new_gr for dir, v1 in pairs(islands[i].neighbors) do local chunk_pos = fa_utils.pos2str(fa_utils.offset_position(event.position, dir, 1)) - if players[pindex].resources[i].queue[chunk_pos] == nil then - players[pindex].resources[i].queue[chunk_pos] = {} - end - players[pindex].resources[i].queue[chunk_pos][dir] = {} + if players[pindex].resources[i].queue[chunk_pos] == nil then + players[pindex].resources[i].queue[chunk_pos] = {} + end + players[pindex].resources[i].queue[chunk_pos][dir] = {} end - for old_index , group in pairs(v.groups) do + for old_index, group in pairs(v.groups) do if true then local new_index = players[pindex].resources[i].index players[pindex].resources[i].patches[new_index] = { positions = {}, - edges = {} + edges = {}, } players[pindex].resources[i].index = players[pindex].resources[i].index + 1 for i2, pos in pairs(group) do @@ -8853,7 +9040,6 @@ if 'number' == type(players[pindex].resources[i].patches[new_group]) then new_gr if islands[i].edges[pos] then local position = fa_utils.str2pos(pos) if fa_utils.area_edge(event.area, 0, position, i) then - local chunk_pos = fa_utils.pos2str(fa_utils.offset_position(event.position, 0, 1)) if players[pindex].resources[i].queue[chunk_pos][4] == nil then players[pindex].resources[i].queue[chunk_pos][4] = {} @@ -8881,39 +9067,32 @@ if 'number' == type(players[pindex].resources[i].patches[new_group]) then new_gr end table.insert(players[pindex].resources[i].queue[chunk_pos][6], pos) end - end - - end end end end end --- print(event.area.left_top.x .. " " .. event.area.left_top.y) --- print(event.area.right_bottom.x .. " " .. event.area.right_bottom.y) --- for name, obj in pairs(resources) do --- print(name .. ": " .. table_size(obj.patches)) --- end + -- print(event.area.left_top.x .. " " .. event.area.left_top.y) + -- print(event.area.right_bottom.x .. " " .. event.area.right_bottom.y) + -- for name, obj in pairs(resources) do + -- print(name .. ": " .. table_size(obj.patches)) + -- end end end) -script.on_event(defines.events.on_entity_destroyed,function(event) --DOES NOT HAVE THE KEY PLAYER_INDEX +script.on_event(defines.events.on_entity_destroyed, function(event) --DOES NOT HAVE THE KEY PLAYER_INDEX local ent = nil for pindex, player in pairs(players) do --If the destroyed entity is destroyed by any player, it will be detected. Laterdo consider logged out players etc? if players[pindex] ~= nil then local try_ent = players[pindex].destroyed[event.registration_number] - if try_ent ~= nil and try_ent.valid then - ent = try_ent - end + if try_ent ~= nil and try_ent.valid then ent = try_ent end end end - if ent == nil then - return - end + if ent == nil then return end local str = fa_utils.pos2str(ent.position) if ent.type == "resource" then - if ent.name ~= "crude-oil" and players[pindex].resources[ent.name].positions[str] ~= nil then--**beta added a check here to not run for nil "group"s... + if ent.name ~= "crude-oil" and players[pindex].resources[ent.name].positions[str] ~= nil then --**beta added a check here to not run for nil "group"s... local group = players[pindex].resources[ent.name].positions[str] players[pindex].resources[ent.name].positions[str] = nil --game.get_player(pindex).print("Pos str: " .. str) @@ -8941,10 +9120,17 @@ script.on_event(defines.events.on_entity_destroyed,function(event) --DOES NOT HA end elseif ent.type == "tree" then local adj = {} - adj[fa_utils.pos2str({x = math.floor(ent.area.left_top.x/32),y = math.floor(ent.area.left_top.y/32)})] = true - adj[fa_utils.pos2str({x = math.floor(ent.area.right_bottom.x/32),y = math.floor(ent.area.left_top.y/32)})] = true - adj[fa_utils.pos2str({x = math.floor(ent.area.left_top.x/32),y = math.floor(ent.area.right_bottom.y/32)})] = true - adj[fa_utils.pos2str({x = math.floor(ent.area.right_bottom.x/32),y = math.floor(ent.area.right_bottom.y/32)})] = true + adj[fa_utils.pos2str({ x = math.floor(ent.area.left_top.x / 32), y = math.floor(ent.area.left_top.y / 32) })] = + true + adj[fa_utils.pos2str({ x = math.floor(ent.area.right_bottom.x / 32), y = math.floor(ent.area.left_top.y / 32) })] = + true + adj[fa_utils.pos2str({ x = math.floor(ent.area.left_top.x / 32), y = math.floor(ent.area.right_bottom.y / 32) })] = + true + adj[fa_utils.pos2str({ + x = math.floor(ent.area.right_bottom.x / 32), + y = math.floor(ent.area.right_bottom.y / 32), + })] = + true for pos, val in pairs(adj) do --players[pindex].tree_chunks[pos].count = players[pindex].tree_chunks[pos].count - 1--**beta Forests need updating but these lines are incorrectly named end @@ -8953,39 +9139,39 @@ script.on_event(defines.events.on_entity_destroyed,function(event) --DOES NOT HA end) --Scripts regarding train state changes. NOTE: NO PINDEX -script.on_event(defines.events.on_train_changed_state,function(event) +script.on_event(defines.events.on_train_changed_state, function(event) if event.train.state == defines.train_state.no_schedule then --Trains with no schedule are set back to manual mode event.train.manual_mode = true elseif event.train.state == defines.train_state.arrive_station then --Announce station to players on the train - for i,player in ipairs(event.train.passengers) do + for i, player in ipairs(event.train.passengers) do local stop = event.train.path_end_stop - if stop ~= nil then - str = " Arriving at station " .. stop.backer_name .. " " - players[player.index].last = str - localised_print{"","out ",str} - end + if stop ~= nil then + str = " Arriving at station " .. stop.backer_name .. " " + players[player.index].last = str + localised_print({ "", "out ", str }) + end end elseif event.train.state == defines.train_state.on_the_path then --laterdo make this announce only when near another trainstop. --Announce station to players on the train - for i,player in ipairs(event.train.passengers) do + for i, player in ipairs(event.train.passengers) do local stop = event.train.path_end_stop - if stop ~= nil then - str = " Heading to station " .. stop.backer_name .. " " - players[player.index].last = str - localised_print{"","out ",str} - end + if stop ~= nil then + str = " Heading to station " .. stop.backer_name .. " " + players[player.index].last = str + localised_print({ "", "out ", str }) + end end elseif event.train.state == defines.train_state.wait_signal then --Announce the wait to players on the train - for i,player in ipairs(event.train.passengers) do + for i, player in ipairs(event.train.passengers) do local stop = event.train.path_end_stop - if stop ~= nil then - str = " Waiting at signal. " - players[player.index].last = str - localised_print{"","out ",str} - end + if stop ~= nil then + str = " Waiting at signal. " + players[player.index].last = str + localised_print({ "", "out ", str }) + end end end end) @@ -8993,15 +9179,13 @@ end) --If a filter inserter is selected, the item in hand is set as its output filter item. function set_inserter_filter_by_hand(pindex, ent) local stack = game.get_player(pindex).cursor_stack - if ent.filter_slot_count == 0 then - return "This inserter has no filters to set" - end + if ent.filter_slot_count == 0 then return "This inserter has no filters to set" end if stack == nil or stack.valid_for_read == false then --Delete last filter for i = ent.filter_slot_count, 1, -1 do local filt = ent.get_filter(i) if filt ~= nil then - ent.set_filter(i,nil) + ent.set_filter(i, nil) return "Last filter cleared" end end @@ -9011,7 +9195,7 @@ function set_inserter_filter_by_hand(pindex, ent) for i = 1, ent.filter_slot_count, 1 do local filt = ent.get_filter(i) if filt == nil then - ent.set_filter(i,stack.name) + ent.set_filter(i, stack.name) if ent.get_filter(i) == stack.name then return "Added filter" else @@ -9021,35 +9205,38 @@ function set_inserter_filter_by_hand(pindex, ent) end return "All filters full" end - end --Feature for typing in coordinates for moving the mod cursor. function type_cursor_position(pindex) printout("Enter new co-ordinates for the cursor, separated by a space", pindex) players[pindex].cursor_jumping = true - local frame = game.get_player(pindex).gui.screen.add{type = "frame", name = "cursor-jump"} + local frame = game.get_player(pindex).gui.screen.add({ type = "frame", name = "cursor-jump" }) frame.bring_to_front() frame.force_auto_center() frame.focus() - local input = frame.add{type="textfield", name = "input"} + local input = frame.add({ type = "textfield", name = "input" }) input.focus() end --Alerts a force's players when their structures are destroyed. 300 ticks of cooldown. -script.on_event(defines.events.on_entity_damaged,function(event) +script.on_event(defines.events.on_entity_damaged, function(event) local ent = event.entity local tick = event.tick if ent == nil or not ent.valid then return elseif ent.name == "character" then --Check character has any energy shield health remaining - if ent.player == nil or not ent.player.valid then - return - end + if ent.player == nil or not ent.player.valid then return end local shield_left = nil local armor_inv = ent.player.get_inventory(defines.inventory.character_armor) - if armor_inv[1] and armor_inv[1].valid_for_read and armor_inv[1].valid and armor_inv[1].grid and armor_inv[1].grid.valid then + if + armor_inv[1] + and armor_inv[1].valid_for_read + and armor_inv[1].valid + and armor_inv[1].grid + and armor_inv[1].grid.valid + then local grid = armor_inv[1].grid if grid.shield > 0 then shield_left = grid.shield @@ -9057,15 +9244,13 @@ script.on_event(defines.events.on_entity_damaged,function(event) end end --Play shield and/or character damaged sound - if shield_left ~= nil then - ent.player.play_sound{path = "player-damaged-shield",volume_modifier=0.8} - end + if shield_left ~= nil then ent.player.play_sound({ path = "player-damaged-shield", volume_modifier = 0.8 }) end if shield_left == nil or (shield_left < 1.0 and ent.get_health_ratio() < 1.0) then - ent.player.play_sound{path = "player-damaged-character",volume_modifier=0.4} + ent.player.play_sound({ path = "player-damaged-character", volume_modifier = 0.4 }) end return elseif ent.get_health_ratio() == 1.0 then - --Ignore alerts if an entity has full health despite being damaged + --Ignore alerts if an entity has full health despite being damaged return elseif tick < 3600 and tick > 600 then --No alerts for the first 10th to 60th seconds (because of the alert spam from spaceship fire damage) @@ -9076,22 +9261,25 @@ script.on_event(defines.events.on_entity_damaged,function(event) local damaged_force = ent.force --Alert all players of the damaged force for pindex, player in pairs(players) do - if players[pindex] ~= nil and game.get_player(pindex).force.name == damaged_force.name - and (players[pindex].last_damage_alert_tick == nil or (tick - players[pindex].last_damage_alert_tick) > 300) then + if + players[pindex] ~= nil + and game.get_player(pindex).force.name == damaged_force.name + and (players[pindex].last_damage_alert_tick == nil or (tick - players[pindex].last_damage_alert_tick) > 300) + then players[pindex].last_damage_alert_tick = tick players[pindex].last_damage_alert_pos = ent.position - local dist = math.ceil(util.distance(players[pindex].position,ent.position)) - local dir = fa_utils.direction_lookup(fa_utils.get_direction_biased(ent.position,players[pindex].position)) + local dist = math.ceil(util.distance(players[pindex].position, ent.position)) + local dir = fa_utils.direction_lookup(fa_utils.get_direction_biased(ent.position, players[pindex].position)) local result = ent.name .. " damaged by " .. attacker_force.name .. " forces at " .. dist .. " " .. dir - printout(result,pindex) + printout(result, pindex) --game.get_player(pindex).print(result,{volume_modifier=0})--** - game.get_player(pindex).play_sound{path = "alert-structure-damaged",volume_modifier=0.3} + game.get_player(pindex).play_sound({ path = "alert-structure-damaged", volume_modifier = 0.3 }) end end end) --Alerts a force's players when their structures are destroyed. No cooldown. -script.on_event(defines.events.on_entity_died,function(event) +script.on_event(defines.events.on_entity_died, function(event) local ent = event.entity local causer = event.cause if ent == nil then @@ -9106,108 +9294,95 @@ script.on_event(defines.events.on_entity_died,function(event) if players[pindex] ~= nil and game.get_player(pindex).force.name == damaged_force.name then players[pindex].last_damage_alert_tick = event.tick players[pindex].last_damage_alert_pos = ent.position - local dist = math.ceil(util.distance(players[pindex].position,ent.position)) - local dir = fa_utils.direction_lookup(fa_utils.get_direction_biased(ent.position,players[pindex].position)) + local dist = math.ceil(util.distance(players[pindex].position, ent.position)) + local dir = fa_utils.direction_lookup(fa_utils.get_direction_biased(ent.position, players[pindex].position)) local result = ent.name .. " destroyed by " .. attacker_force.name .. " forces at " .. dist .. " " .. dir - printout(result,pindex) + printout(result, pindex) --game.get_player(pindex).print(result,{volume_modifier=0})--** - game.get_player(pindex).play_sound{path = "utility/alert_destroyed",volume_modifier=0.5} + game.get_player(pindex).play_sound({ path = "utility/alert_destroyed", volume_modifier = 0.5 }) end end end) --Notify all players when a player character dies -script.on_event(defines.events.on_player_died,function(event) +script.on_event(defines.events.on_player_died, function(event) local pindex = event.player_index local p = game.get_player(pindex) local causer = event.cause - local bodies = p.surface.find_entities_filtered{name = "character-corpse"} + local bodies = p.surface.find_entities_filtered({ name = "character-corpse" }) local latest_body = nil local latest_death_tick = 0 local name = p.name - if name == nil then - name = " " - end + if name == nil then name = " " end --Find the most recent character corpse - for i,body in ipairs(bodies) do - if body.character_corpse_player_index == pindex and body.character_corpse_tick_of_death > latest_death_tick then + for i, body in ipairs(bodies) do + if body.character_corpse_player_index == pindex and body.character_corpse_tick_of_death > latest_death_tick then latest_body = body latest_death_tick = latest_body.character_corpse_tick_of_death end end --Verify the latest death - if event.tick - latest_death_tick > 120 then - latest_body = nil - end + if event.tick - latest_death_tick > 120 then latest_body = nil end --Generate death message local result = "Player " .. name if causer == nil or not causer.valid then result = result .. " died " elseif causer.name == "character" and causer.player ~= nil and causer.player.valid then local other_name = causer.player.name - if other_name == nil then - other_name = "" - end + if other_name == nil then other_name = "" end result = result .. " was killed by player " .. other_name else result = result .. " was killed by " .. causer.name end if latest_body ~= nil and latest_body.valid then - result = result .. " at " .. math.floor(0.5+latest_body.position.x) .. ", " .. math.floor(0.5+latest_body.position.y) .. "." + result = result + .. " at " + .. math.floor(0.5 + latest_body.position.x) + .. ", " + .. math.floor(0.5 + latest_body.position.y) + .. "." end --Notify all players for pindex, player in pairs(players) do players[pindex].last_damage_alert_tick = event.tick - printout(result,pindex) - game.get_player(pindex).print(result)--**laterdo unique sound, for now use console sound + printout(result, pindex) + game.get_player(pindex).print(result) --**laterdo unique sound, for now use console sound end end) -script.on_event(defines.events.on_player_display_resolution_changed,function(event) +script.on_event(defines.events.on_player_display_resolution_changed, function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local new_res = game.get_player(pindex).display_resolution - if players and players[pindex] then - players[pindex].display_resolution = new_res - end - game.get_player(pindex).print("Display resolution changed: " .. new_res.width .. " x " .. new_res.height ,{volume_modifier = 0}) + if players and players[pindex] then players[pindex].display_resolution = new_res end + game + .get_player(pindex) + .print("Display resolution changed: " .. new_res.width .. " x " .. new_res.height, { volume_modifier = 0 }) schedule(3, "call_to_fix_zoom", pindex) schedule(4, "call_to_sync_graphics", pindex) end) -script.on_event(defines.events.on_player_display_scale_changed,function(event) +script.on_event(defines.events.on_player_display_scale_changed, function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local new_sc = game.get_player(pindex).display_scale - if players and players[pindex] then - players[pindex].display_resolution = new_sc - end - game.get_player(pindex).print("Display scale changed: " .. new_sc ,{volume_modifier = 0}) + if players and players[pindex] then players[pindex].display_resolution = new_sc end + game.get_player(pindex).print("Display scale changed: " .. new_sc, { volume_modifier = 0 }) schedule(3, "call_to_fix_zoom", pindex) schedule(4, "call_to_sync_graphics", pindex) end) -script.on_event(defines.events.on_string_translated,fa_localising.handler) +script.on_event(defines.events.on_string_translated, fa_localising.handler) --If the player has unexpected lateral movement while smooth running in a cardinal direction, like from bumping into an entity or being at the edge of water, play a sound. -function check_and_play_bump_alert_sound(pindex,this_tick) - if not check_for_player(pindex) or players[pindex].menu == "prompt" then - return - end +function check_and_play_bump_alert_sound(pindex, this_tick) + if not check_for_player(pindex) or players[pindex].menu == "prompt" then return end local p = game.get_player(pindex) - if p == nil or p.character == nil then - return - end + if p == nil or p.character == nil then return end local face_dir = p.character.direction - --Initialize - if players[pindex].bump == nil then - reset_bump_stats(pindex) - end + --Initialize + if players[pindex].bump == nil then reset_bump_stats(pindex) end --Return and reset if in a menu or a vehicle or in a different walking mode than smooth walking if players[pindex].in_menu or p.vehicle ~= nil or players[pindex].walk ~= 2 then @@ -9239,7 +9414,12 @@ function check_and_play_bump_alert_sound(pindex,this_tick) if this_tick - players[pindex].bump.last_bump_tick < 15 then return end --Return if player changed direction recently - if this_tick - players[pindex].bump.last_dir_key_tick < 30 and players[pindex].bump.last_dir_key_1st ~= players[pindex].bump.last_dir_key_2nd then return end + if + this_tick - players[pindex].bump.last_dir_key_tick < 30 + and players[pindex].bump.last_dir_key_1st ~= players[pindex].bump.last_dir_key_2nd + then + return + end --Return if current running direction is not equal to the last (e.g. letting go of a key) if face_dir ~= players[pindex].bump.last_dir_key_1st then return end @@ -9251,7 +9431,9 @@ function check_and_play_bump_alert_sound(pindex,this_tick) if players[pindex].bump.last_dir_2 == nil then return end --Return if not walking in a cardinal direction - if face_dir ~= dirs.north and face_dir ~= dirs.east and face_dir ~= dirs.south and face_dir ~= dirs.west then return end + if face_dir ~= dirs.north and face_dir ~= dirs.east and face_dir ~= dirs.south and face_dir ~= dirs.west then + return + end --Return if last dir is different if players[pindex].bump.last_dir_1 ~= players[pindex].bump.last_dir_2 then return end @@ -9274,43 +9456,31 @@ function check_and_play_bump_alert_sound(pindex,this_tick) was_going_straight = true else if face_dir == dirs.north or face_dir == dirs.south then - if math.abs(diff_x2) < TOLERANCE and math.abs(diff_x3) < TOLERANCE then - was_going_straight = true - end + if math.abs(diff_x2) < TOLERANCE and math.abs(diff_x3) < TOLERANCE then was_going_straight = true end elseif face_dir == dirs.east or face_dir == dirs.west then - if math.abs(diff_y2) < TOLERANCE and math.abs(diff_y3) < TOLERANCE then - was_going_straight = true - end + if math.abs(diff_y2) < TOLERANCE and math.abs(diff_y3) < TOLERANCE then was_going_straight = true end end end --Return if was not going straight earlier (like was running diagonally, as confirmed by last positions) - if not was_going_straight then - return - end + if not was_going_straight then return end --game.print("checking bump",{volume_modifier=0})-- --Check if latest movement has been straight local is_going_straight = false if face_dir == dirs.north or face_dir == dirs.south then - if math.abs(diff_x1) < TOLERANCE then - is_going_straight = true - end + if math.abs(diff_x1) < TOLERANCE then is_going_straight = true end elseif face_dir == dirs.east or face_dir == dirs.west then - if math.abs(diff_y1) < TOLERANCE then - is_going_straight = true - end + if math.abs(diff_y1) < TOLERANCE then is_going_straight = true end end --Return if going straight now - if is_going_straight then - return - end + if is_going_straight then return end --Now we can confirm that there is a sudden lateral movement players[pindex].bump.last_bump_tick = this_tick - p.play_sound{path = "player-bump-alert"} + p.play_sound({ path = "player-bump-alert" }) local bump_was_ent = false local bump_was_cliff = false local bump_was_tile = false @@ -9318,14 +9488,28 @@ function check_and_play_bump_alert_sound(pindex,this_tick) --Check if there is an ent in front of the player local found_ent = get_selected_ent(pindex) local ent = nil - if found_ent and found_ent.valid and found_ent.type ~= "resource" and found_ent.type ~= "transport-belt" and found_ent.type ~= "item-entity" and found_ent.type ~= "entity-ghost" and found_ent.type ~= "character" then + if + found_ent + and found_ent.valid + and found_ent.type ~= "resource" + and found_ent.type ~= "transport-belt" + and found_ent.type ~= "item-entity" + and found_ent.type ~= "entity-ghost" + and found_ent.type ~= "character" + then ent = found_ent end if ent == nil or ent.valid == false then - local ents = p.surface.find_entities_filtered{position = p.position, radius = 0.75} + local ents = p.surface.find_entities_filtered({ position = p.position, radius = 0.75 }) for i, found_ent in ipairs(ents) do --Ignore ents you can walk through, laterdo better collision checks** - if found_ent.type ~= "resource" and found_ent.type ~= "transport-belt" and found_ent.type ~= "item-entity" and found_ent.type ~= "entity-ghost" and found_ent.type ~= "character" then + if + found_ent.type ~= "resource" + and found_ent.type ~= "transport-belt" + and found_ent.type ~= "item-entity" + and found_ent.type ~= "entity-ghost" + and found_ent.type ~= "character" + then ent = found_ent end end @@ -9334,19 +9518,19 @@ function check_and_play_bump_alert_sound(pindex,this_tick) if bump_was_ent then if ent.type == "cliff" then - p.play_sound{path = "player-bump-slide"} + p.play_sound({ path = "player-bump-slide" }) else - p.play_sound{path = "player-bump-trip"} + p.play_sound({ path = "player-bump-trip" }) end --game.print("bump: ent:" .. ent.name,{volume_modifier=0})-- return end --Check if there is a cliff nearby (the weird size can make it affect the player without being read) - local ents = p.surface.find_entities_filtered{position = p.position, radius = 2, type = "cliff" } + local ents = p.surface.find_entities_filtered({ position = p.position, radius = 2, type = "cliff" }) bump_was_cliff = (#ents > 0) if bump_was_cliff then - p.play_sound{path = "player-bump-slide"} + p.play_sound({ path = "player-bump-slide" }) --game.print("bump: cliff",{volume_modifier=0})-- return end @@ -9356,7 +9540,7 @@ function check_and_play_bump_alert_sound(pindex,this_tick) bump_was_tile = (tile ~= nil and tile.valid and tile.collides_with("player-layer")) if bump_was_tile then - p.play_sound{path = "player-bump-slide"} + p.play_sound({ path = "player-bump-slide" }) --game.print("bump: tile:" .. tile.name,{volume_modifier=0})-- return end @@ -9368,21 +9552,15 @@ function check_and_play_bump_alert_sound(pindex,this_tick) end --If walking but recently position has been unchanged, play alert -function check_and_play_stuck_alert_sound(pindex,this_tick) - if not check_for_player(pindex) or players[pindex].menu == "prompt" then - return - end +function check_and_play_stuck_alert_sound(pindex, this_tick) + if not check_for_player(pindex) or players[pindex].menu == "prompt" then return end local p = game.get_player(pindex) - --Initialize - if players[pindex].bump == nil then - reset_bump_stats(pindex) - end + --Initialize + if players[pindex].bump == nil then reset_bump_stats(pindex) end --Return if in a menu or a vehicle or in a different walking mode than smooth walking - if players[pindex].in_menu or p.vehicle ~= nil or players[pindex].walk ~= 2 then - return - end + if players[pindex].in_menu or p.vehicle ~= nil or players[pindex].walk ~= 2 then return end --Return if not walking if p.walking_state.walking == false then return end @@ -9406,9 +9584,8 @@ function check_and_play_stuck_alert_sound(pindex,this_tick) --Check if earlier movement has been straight if diff_x1 == 0 and diff_y1 == 0 and diff_x2 == 0 and diff_y2 == 0 then --and diff_x3 == 0 and diff_y3 == 0 then - p.play_sound{path = "player-bump-stuck-alert"} + p.play_sound({ path = "player-bump-stuck-alert" }) end - end function reset_bump_stats(pindex) @@ -9422,12 +9599,17 @@ function reset_bump_stats(pindex) last_pos_3 = nil, last_pos_4 = nil, last_dir_2 = nil, - last_dir_1 = nil + last_dir_1 = nil, } end function all_ents_are_walkable(pos) - local ents = game.surfaces[1].find_entities_filtered{position = fa_utils.center_of_tile(pos), radius = 0.4, invert = true, type = ENT_TYPES_YOU_CAN_WALK_OVER} + local ents = game.surfaces[1].find_entities_filtered({ + position = fa_utils.center_of_tile(pos), + radius = 0.4, + invert = true, + type = ENT_TYPES_YOU_CAN_WALK_OVER, + }) for i, ent in ipairs(ents) do return false end @@ -9440,15 +9622,15 @@ function regenerate_all_uncharted_spawners(surface_in) --Get spawner names local spawner_names = {} - for name, prot in pairs(game.get_filtered_entity_prototypes({{filter = "type", type = "unit-spawner"}})) do - table.insert(spawner_names,name) + for name, prot in pairs(game.get_filtered_entity_prototypes({ { filter = "type", type = "unit-spawner" } })) do + table.insert(spawner_names, name) end for chunk in surf.get_chunks() do local is_charted = false --Check if the chunk is charted by any players for pindex, player in pairs(players) do - is_charted = is_charted or (player.force and player.force.is_chunk_charted(surf, {x = chunk.x, y = chunk.y})) + is_charted = is_charted or (player.force and player.force.is_chunk_charted(surf, { x = chunk.x, y = chunk.y })) end --Regenerate the spawners if NOT charted by any player forces if is_charted == false then @@ -9459,15 +9641,15 @@ function regenerate_all_uncharted_spawners(surface_in) end end -function general_mod_menu_up(pindex, menu, lower_limit_in)--todo*** use +function general_mod_menu_up(pindex, menu, lower_limit_in) --todo*** use local lower_limit = lower_limit_in or 0 menu.index = menu.index - 1 if menu.index < lower_limit then menu.index = lower_limit - game.get_player(pindex).play_sound{path = "inventory-edge"} + game.get_player(pindex).play_sound({ path = "inventory-edge" }) else --Play sound - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) end end @@ -9475,10 +9657,10 @@ function general_mod_menu_down(pindex, menu, upper_limit) menu.index = menu.index + 1 if menu.index > upper_limit then menu.index = upper_limit - game.get_player(pindex).play_sound{path = "inventory-edge"} + game.get_player(pindex).play_sound({ path = "inventory-edge" }) else --Play sound - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) end end @@ -9493,16 +9675,12 @@ function selected_item_production_stats_info(pindex) --Select the cursor stack item_stack = p.cursor_stack - if item_stack and item_stack.valid_for_read then - internal_name = item_stack.prototype.name - end + if item_stack and item_stack.valid_for_read then internal_name = item_stack.prototype.name end --Otherwise select the selected inventory stack if internal_name == nil and players[pindex].menu == "inventory" then item_stack = players[pindex].inventory.lua_inventory[players[pindex].inventory.index] - if item_stack and item_stack.valid_for_read then - internal_name = item_stack.prototype.name - end + if item_stack and item_stack.valid_for_read then internal_name = item_stack.prototype.name end end --Otherwise select the selected crafting recipe @@ -9515,7 +9693,7 @@ function selected_item_production_stats_info(pindex) prototype = game.item_prototypes[recipe.products[1].name] if prototype then internal_name = prototype.name - result = fa_localising.get_item_from_name(recipe.products[1].name,pindex) .. " " + result = fa_localising.get_item_from_name(recipe.products[1].name, pindex) .. " " end end if recipe.products[1].type == "fluid" then @@ -9524,16 +9702,16 @@ function selected_item_production_stats_info(pindex) prototype = game.fluid_prototypes[recipe.name] if prototype then internal_name = prototype.name - result = fa_localising.get_fluid_from_name(recipe.products[1].name,pindex) .. " " + result = fa_localising.get_fluid_from_name(recipe.products[1].name, pindex) .. " " end end - if (recipe.products[2] and recipe.products[2].type == "fluid") then + if recipe.products[2] and recipe.products[2].type == "fluid" then --Select product fluid #2 (instead) stats = p.force.fluid_production_statistics prototype = game.fluid_prototypes[recipe.products[2].name] if prototype then internal_name = prototype.name - result = fa_localising.get_fluid_from_name(recipe.products[2].name,pindex) .. " " + result = fa_localising.get_fluid_from_name(recipe.products[2].name, pindex) .. " " end end end @@ -9544,10 +9722,18 @@ function selected_item_production_stats_info(pindex) return result end local interval = defines.flow_precision_index - local last_minute = stats.get_flow_count{name = internal_name, input = true, precision_index = interval.one_minute, count = true} - local last_10_minutes = stats.get_flow_count{name = internal_name, input = true, precision_index = interval.ten_minutes, count = true} - local last_hour = stats.get_flow_count{name = internal_name, input = true, precision_index = interval.one_hour, count = true} - local thousand_hours = stats.get_flow_count{name = internal_name, input = true, precision_index = interval.one_thousand_hours, count = true} + local last_minute = + stats.get_flow_count({ name = internal_name, input = true, precision_index = interval.one_minute, count = true }) + local last_10_minutes = + stats.get_flow_count({ name = internal_name, input = true, precision_index = interval.ten_minutes, count = true }) + local last_hour = + stats.get_flow_count({ name = internal_name, input = true, precision_index = interval.one_hour, count = true }) + local thousand_hours = stats.get_flow_count({ + name = internal_name, + input = true, + precision_index = interval.one_thousand_hours, + count = true, + }) last_minute = fa_utils.floor_to_nearest_k_after_10k(last_minute) last_10_minutes = fa_utils.floor_to_nearest_k_after_10k(last_10_minutes) last_hour = fa_utils.floor_to_nearest_k_after_10k(last_hour) @@ -9569,7 +9755,10 @@ script.on_event("fa-pda-cruise-control-info", function(event) end) script.on_event("fa-pda-cruise-control-set-speed-info", function(event) - printout("Type in the new cruise control speed and press 'ENTER' and then 'E' to confirm, or press 'ESC' to exit",pindex) + printout( + "Type in the new cruise control speed and press 'ENTER' and then 'E' to confirm, or press 'ESC' to exit", + pindex + ) end) --Reports if the cursor tile is uncharted/blurred and also if it is distant (offscreen) @@ -9577,10 +9766,10 @@ function cursor_visibility_info(pindex) local p = game.get_player(pindex) local result = "" local pos = players[pindex].cursor_pos - local chunk_pos = {x = math.floor(pos.x/32), y = math.floor(pos.y/32)} - if p.force.is_chunk_charted(p.surface,chunk_pos) == false then + local chunk_pos = { x = math.floor(pos.x / 32), y = math.floor(pos.y / 32) } + if p.force.is_chunk_charted(p.surface, chunk_pos) == false then result = result .. " uncharted " - elseif p.force.is_chunk_visible(p.surface,chunk_pos) == false then + elseif p.force.is_chunk_visible(p.surface, chunk_pos) == false then result = result .. " blurred " end if fa_mouse.cursor_position_is_on_screen_with_player_centered(pindex) == false then @@ -9591,17 +9780,16 @@ end script.on_event("nearest-damaged-ent-info", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end - read_nearest_damaged_ent_info(players[pindex].cursor_pos,pindex) + if not check_for_player(pindex) then return end + read_nearest_damaged_ent_info(players[pindex].cursor_pos, pindex) end) --Reads out the distance and direction to the nearest damaged entity within 1000 tiles. -function read_nearest_damaged_ent_info(pos,pindex) +function read_nearest_damaged_ent_info(pos, pindex) local p = game.get_player(pindex) --Scan for ents of your force - local ents = p.surface.find_entities_filtered{position = players[pindex].cursor_pos, radius = 1000, force = p.force} + local ents = + p.surface.find_entities_filtered({ position = players[pindex].cursor_pos, radius = 1000, force = p.force }) --Check for entities with health if ents == nil or #ents == 0 then printout("No damaged structures within 1000 tiles.", pindex) @@ -9612,7 +9800,7 @@ function read_nearest_damaged_ent_info(pos,pindex) for i, ent in ipairs(ents) do if ent.is_entity_with_health == true and ent.type ~= "character" and ent.get_health_ratio() < 1 then at_least_one_has_damage = true - table.insert(damaged_ents,ent) + table.insert(damaged_ents, ent) end end if at_least_one_has_damage == false then @@ -9623,13 +9811,11 @@ function read_nearest_damaged_ent_info(pos,pindex) local closest = nil local min_dist = 1001 for i, ent in ipairs(damaged_ents) do - local dist = util.distance(pos,ent.position) + local dist = util.distance(pos, ent.position) if dist < min_dist then min_dist = dist closest = ent - if min_dist < 2 then - break - end + if min_dist < 2 then break end end end if closest == nil then @@ -9637,22 +9823,24 @@ function read_nearest_damaged_ent_info(pos,pindex) return else min_dist = math.floor(min_dist) - local dir = fa_utils.get_direction_biased(closest.position,pos) - local result = fa_localising.get(closest, pindex) .. " damaged at " .. min_dist .. " " .. fa_utils.direction_lookup(dir) + local dir = fa_utils.get_direction_biased(closest.position, pos) + local result = fa_localising.get(closest, pindex) + .. " damaged at " + .. min_dist + .. " " + .. fa_utils.direction_lookup(dir) printout(result, pindex) end end script.on_event("cursor-pollution-info", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end - read_pollution_level_at_position(players[pindex].cursor_pos,pindex) + if not check_for_player(pindex) then return end + read_pollution_level_at_position(players[pindex].cursor_pos, pindex) end) --Reads out the relative pollution level at the input position. The categories are based on data like map view shaders, water discoloration rates. For example, in default settings trees are damaged after pollution exceeds 60 and water is discolored after 90, and the deepest shader applies after 150. -function read_pollution_level_at_position(pos,pindex) +function read_pollution_level_at_position(pos, pindex) local p = game.get_player(pindex) local pol = p.surface.get_pollution(pos) local result = " pollution detected" @@ -9678,9 +9866,7 @@ end script.on_event("klient-alt-move-to", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end if players[pindex].remote_view == true then players[pindex].kruise_kontrolling = true @@ -9698,13 +9884,8 @@ end) script.on_event("klient-cancel-enter", function(event) local pindex = event.player_index - if not check_for_player(pindex) then - return - end - if players[pindex].kruise_kontrolling == true then - printout("Cancelled action.",pindex) - end + if not check_for_player(pindex) then return end + if players[pindex].kruise_kontrolling == true then printout("Cancelled action.", pindex) end players[pindex].kruise_kontrolling = false toggle_remote_view(pindex, false, true) end) - diff --git a/data-updates.lua b/data-updates.lua index 7be04810..79f98641 100644 --- a/data-updates.lua +++ b/data-updates.lua @@ -1,75 +1,71 @@ - - - for name, proto in pairs(data.raw.container) do - proto.open_sound = proto.open_sound or { filename = "__base__/sound/metallic-chest-open.ogg" , volume = 0.43 } + proto.open_sound = proto.open_sound or { filename = "__base__/sound/metallic-chest-open.ogg", volume = 0.43 } proto.close_sound = proto.close_sound or { filename = "__base__/sound/metallic-chest-close.ogg", volume = 0.43 } end data.raw.character.character.has_belt_immunity = true -for _,item in pairs(vanilla_tip_and_tricks_item_table) do - remove_tip_and_tricks_item(item); +for _, item in pairs(vanilla_tip_and_tricks_item_table) do + remove_tip_and_tricks_item(item) end ---Modifications to Kruise Kontrol inputs -local alt_input = -{ - name = "klient-alt-move-to", - type = "custom-input", - key_sequence = "CONTROL + ALT + RIGHTBRACKET", - consuming = "game-only" +--Modifications to Kruise Kontrol inputs +local alt_input = { + name = "klient-alt-move-to", + type = "custom-input", + key_sequence = "CONTROL + ALT + RIGHTBRACKET", + consuming = "game-only", } -local cancel_enter = -{ - name = "klient-cancel-enter", - type = "custom-input", - linked_game_control = "toggle-driving", - consuming = "none", - key_sequence = "" +local cancel_enter = { + name = "klient-cancel-enter", + type = "custom-input", + linked_game_control = "toggle-driving", + consuming = "none", + key_sequence = "", } -data:extend{alt_input, cancel_enter} +data:extend({ alt_input, cancel_enter }) --Modifications to Pavement Driving Assist Continued inputs data:extend({ - { - type = "custom-input", - name = "toggle_drive_assistant", - key_sequence = "L", - consuming = "game-only" - }, - { - type = "custom-input", - name = "toggle_cruise_control", - key_sequence = "O", - consuming = "game-only" - }, - { - type = "custom-input", - name = "set_cruise_control_limit", - key_sequence = "CONTROL + O", - consuming = "game-only" - }, - { - type = "custom-input", - name = "confirm_set_cruise_control_limit", - key_sequence = "", - linked_game_control = "confirm-gui" - }, + { + type = "custom-input", + name = "toggle_drive_assistant", + key_sequence = "L", + consuming = "game-only", + }, + { + type = "custom-input", + name = "toggle_cruise_control", + key_sequence = "O", + consuming = "game-only", + }, + { + type = "custom-input", + name = "set_cruise_control_limit", + key_sequence = "CONTROL + O", + consuming = "game-only", + }, + { + type = "custom-input", + name = "confirm_set_cruise_control_limit", + key_sequence = "", + linked_game_control = "confirm-gui", + }, }) ---Modify base prototypes to remove their default descriptions -for name, pack in pairs(data.raw.tool) do - if pack.localised_description and pack.localised_description[1] == "item-description.science-pack" then - pack.localised_description = nil +--Modify base prototypes to remove their default descriptions +for name, pack in pairs(data.raw.tool) do + if pack.localised_description and pack.localised_description[1] == "item-description.science-pack" then + pack.localised_description = nil end end -for name, mod in pairs(data.raw.module) do - if mod.localised_description and mod.localised_description[1] == "item-description.effectivity-module" - or mod.localised_description and mod.localised_description[1] == "item-description.productivity-module" - or mod.localised_description and mod.localised_description[1] == "item-description.speed-module" then - mod.localised_description = nil +for name, mod in pairs(data.raw.module) do + if + mod.localised_description and mod.localised_description[1] == "item-description.effectivity-module" + or mod.localised_description and mod.localised_description[1] == "item-description.productivity-module" + or mod.localised_description and mod.localised_description[1] == "item-description.speed-module" + then + mod.localised_description = nil end -end - \ No newline at end of file +end diff --git a/data.lua b/data.lua index af2908a5..ebffe0f1 100644 --- a/data.lua +++ b/data.lua @@ -8,157 +8,146 @@ data.raw.character.character.has_belt_immunity = true ---Make selected vanilla objects not collide with players ----Pipes local pipe = data.raw.pipe.pipe -pipe.collision_mask = {"object-layer", "floor-layer", "water-tile"} +pipe.collision_mask = { "object-layer", "floor-layer", "water-tile" } local pipe_to_ground = data.raw["pipe-to-ground"]["pipe-to-ground"] -pipe_to_ground.collision_mask = {"object-layer", "floor-layer", "water-tile"} +pipe_to_ground.collision_mask = { "object-layer", "floor-layer", "water-tile" } ----Small & medium electric poles local small_electric_pole = data.raw["electric-pole"]["small-electric-pole"] -small_electric_pole.collision_mask = {"object-layer", "floor-layer", "water-tile"} +small_electric_pole.collision_mask = { "object-layer", "floor-layer", "water-tile" } local medium_electric_pole = data.raw["electric-pole"]["medium-electric-pole"] -medium_electric_pole.collision_mask = {"object-layer", "floor-layer", "water-tile"} +medium_electric_pole.collision_mask = { "object-layer", "floor-layer", "water-tile" } ----Constant combinators (because of when they are placed to explain transport belts) local constant_combinator = data.raw["constant-combinator"]["constant-combinator"] -constant_combinator.collision_mask = {"object-layer", "floor-layer", "water-tile"} +constant_combinator.collision_mask = { "object-layer", "floor-layer", "water-tile" } ----Inserters local inserter = data.raw["inserter"]["inserter"] -inserter.collision_mask = {"object-layer", "floor-layer", "water-tile"} +inserter.collision_mask = { "object-layer", "floor-layer", "water-tile" } local burner_inserter = data.raw["inserter"]["burner-inserter"] -burner_inserter.collision_mask = {"object-layer", "floor-layer", "water-tile"} +burner_inserter.collision_mask = { "object-layer", "floor-layer", "water-tile" } local fast_inserter = data.raw["inserter"]["fast-inserter"] -fast_inserter.collision_mask = {"object-layer", "floor-layer", "water-tile"} +fast_inserter.collision_mask = { "object-layer", "floor-layer", "water-tile" } local long_handed_inserter = data.raw["inserter"]["long-handed-inserter"] -long_handed_inserter.collision_mask = {"object-layer", "floor-layer", "water-tile"} +long_handed_inserter.collision_mask = { "object-layer", "floor-layer", "water-tile" } local stack_inserter = data.raw["inserter"]["stack-inserter"] -stack_inserter.collision_mask = {"object-layer", "floor-layer", "water-tile"} +stack_inserter.collision_mask = { "object-layer", "floor-layer", "water-tile" } ----Filter inserters local filter_inserter = data.raw["inserter"]["filter-inserter"] -filter_inserter.collision_mask = {"object-layer", "floor-layer", "water-tile"} +filter_inserter.collision_mask = { "object-layer", "floor-layer", "water-tile" } local stack_filter_inserter = data.raw["inserter"]["stack-filter-inserter"] -stack_filter_inserter.collision_mask = {"object-layer", "floor-layer", "water-tile"} +stack_filter_inserter.collision_mask = { "object-layer", "floor-layer", "water-tile" } --Removal of vanilla tips and tricks-- -vanilla_tip_and_tricks_item_table= -{ +vanilla_tip_and_tricks_item_table = { "introduction", - "game-interaction", - "show-info", - --"e-confirm", - "clear-cursor", - "pipette", - "stack-transfers", - - "entity-transfers", - "z-dropping", - "shoot-targeting", - "bulk-crafting", - - + "game-interaction", + "show-info", + --"e-confirm", + "clear-cursor", + "pipette", + "stack-transfers", + + "entity-transfers", + "z-dropping", + "shoot-targeting", + "bulk-crafting", + "inserters", - "burner-inserter-refueling", - "long-handed-inserters", - "move-between-labs", - "insertion-limits", - "limit-chests", - - - "transport-belts",--in category "belt" - "belt-lanes", - "splitters", - "splitter-filters", - "underground-belts", - - + "burner-inserter-refueling", + "long-handed-inserters", + "move-between-labs", + "insertion-limits", + "limit-chests", + + "transport-belts", --in category "belt" + "belt-lanes", + "splitters", + "splitter-filters", + "underground-belts", + "electric-network", - "steam-power", - "low-power", - "electric-pole-connections", - "connect-switch", - - - "copy-entity-settings",--in category "copy-paste" - "copy-paste-trains", - "copy-paste-filters", - "copy-paste-requester-chest", - "copy-paste-spidertron", - - + "steam-power", + "low-power", + "electric-pole-connections", + "connect-switch", + + "copy-entity-settings", --in category "copy-paste" + "copy-paste-trains", + "copy-paste-filters", + "copy-paste-requester-chest", + "copy-paste-spidertron", + "drag-building", - "drag-building-poles", - "pole-dragging-coverage", - "drag-building-underground-belts", - "fast-belt-bending", - "fast-obstacle-traversing", - - + "drag-building-poles", + "pole-dragging-coverage", + "drag-building-underground-belts", + "fast-belt-bending", + "fast-obstacle-traversing", + "trains", - "rail-building", - "train-stops", - "rail-signals-basic", - "rail-signals-advanced", - "gate-over-rail", - - "pump-connection", - "train-stop-same-name", - - + "rail-building", + "train-stops", + "rail-signals-basic", + "rail-signals-advanced", + "gate-over-rail", + + "pump-connection", + "train-stop-same-name", + "logistic-network", - "personal-logistics", - "construction-robots", - "passive-provider-chest", - "storage-chest", - "requester-chest", - - "active-provider-chest", - "buffer-chest", - - + "personal-logistics", + "construction-robots", + "passive-provider-chest", + "storage-chest", + "requester-chest", + + "active-provider-chest", + "buffer-chest", + "ghost-building", - "ghost-rail-planner", - "copy-paste", - - + "ghost-rail-planner", + "copy-paste", + "fast-replace", - "fast-replace-direction", - "fast-replace-belt-splitter", - "fast-replace-belt-underground", - + "fast-replace-direction", + "fast-replace-belt-splitter", + "fast-replace-belt-underground", + --no category - "rotating-assemblers", - "circuit-network", - -}; + "rotating-assemblers", + "circuit-network", +} function remove_tip_and_tricks_item(inname) - data.raw["tips-and-tricks-item"][inname]=nil; - for _,item in pairs(data.raw["tips-and-tricks-item"]) do - if(item.dependencies) then - local backup=table.deepcopy(item.dependencies); - item.dependencies={"e-confirm"}; - ---@diagnostic disable-next-line: param-type-mismatch - for _,str in pairs(backup) do - if(str~=inname) then table.insert(item.dependencies,str); end + data.raw["tips-and-tricks-item"][inname] = nil + for _, item in pairs(data.raw["tips-and-tricks-item"]) do + if item.dependencies then + local backup = table.deepcopy(item.dependencies) + item.dependencies = { "e-confirm" } + ---@diagnostic disable-next-line: param-type-mismatch + for _, str in pairs(backup) do + if str ~= inname then table.insert(item.dependencies, str) end end end end end -data.raw["tips-and-tricks-item"]["introduction"].category="game-interaction"; -data.raw["tips-and-tricks-item"]["introduction"].trigger=nil; +data.raw["tips-and-tricks-item"]["introduction"].category = "game-interaction" +data.raw["tips-and-tricks-item"]["introduction"].trigger = nil -data.raw["tips-and-tricks-item"]["show-info"].starting_status="unlocked"; -data.raw["tips-and-tricks-item"]["show-info"].dependencies=nil; +data.raw["tips-and-tricks-item"]["show-info"].starting_status = "unlocked" +data.raw["tips-and-tricks-item"]["show-info"].dependencies = nil -data.raw["tips-and-tricks-item"]["e-confirm"].starting_status="unlocked"; -data.raw["tips-and-tricks-item"]["e-confirm"].trigger=nil; -data.raw["tips-and-tricks-item"]["e-confirm"].skip_trigger={type="use-confirm"};--**nil -data.raw["tips-and-tricks-item"]["e-confirm"].dependencies=nil; +data.raw["tips-and-tricks-item"]["e-confirm"].starting_status = "unlocked" +data.raw["tips-and-tricks-item"]["e-confirm"].trigger = nil +data.raw["tips-and-tricks-item"]["e-confirm"].skip_trigger = { type = "use-confirm" } --**nil +data.raw["tips-and-tricks-item"]["e-confirm"].dependencies = nil -for _,item in pairs(vanilla_tip_and_tricks_item_table) do - remove_tip_and_tricks_item(item); +for _, item in pairs(vanilla_tip_and_tricks_item_table) do + remove_tip_and_tricks_item(item) end --New prototypes-- @@ -167,31 +156,31 @@ end local resource_map_node = {} resource_map_node.name = "map-node" resource_map_node.type = "simple-entity-with-force" -resource_map_node.collision_box = {{-0, -0}, {0, 0}} +resource_map_node.collision_box = { { -0, -0 }, { 0, 0 } } resource_map_node.collision_mask = {} resource_map_node.selection_box = nil resource_map_node.order = "z" resource_map_node.max_health = 2147483648 resource_map_node.picture = { - filename = "__FactorioAccess__/Graphics/invisible.png", - width = 1, - height = 1, - direction_count = 1 + filename = "__FactorioAccess__/Graphics/invisible.png", + width = 1, + height = 1, + direction_count = 1, } -data:extend({resource_map_node}) +data:extend({ resource_map_node }) ---New radar type: This radar scans a new sector every 5 seconds instead of 33, and it refreshes its short range every 5 seconds (precisely fast enough) instead of 1 second, but the short range is smaller and the radar costs double the power. -local ar_tint = {r=0.5,g=0.5,b=0.5,a=0.9} +local ar_tint = { r = 0.5, g = 0.5, b = 0.5, a = 0.9 } local access_radar = table.deepcopy(data.raw["radar"]["radar"]) access_radar.icons = { - { - icon = access_radar.icon, - icon_size = access_radar.icon_size, - tint = ar_tint - } + { + icon = access_radar.icon, + icon_size = access_radar.icon_size, + tint = ar_tint, + }, } access_radar.name = "access-radar" -access_radar.energy_usage = "600kW" --Default: "300kW" +access_radar.energy_usage = "600kW" --Default: "300kW" access_radar.energy_per_sector = "3MJ" --Default: "10MJ" access_radar.energy_per_nearby_scan = "3MJ" --Default: "250kJ" access_radar.max_distance_of_sector_revealed = 32 --Default: 14, now scans up to 1024 tiles away instead of 448 @@ -199,1793 +188,1795 @@ access_radar.max_distance_of_nearby_sector_revealed = 2 --Default: 3 access_radar.rotation_speed = 0.01 --Default: 0.01 access_radar.tint = ar_tint access_radar.minable.result = "access-radar" -access_radar.pictures.layers[1].tint = ar_tint--grey -access_radar.pictures.layers[2].tint = ar_tint--grey +access_radar.pictures.layers[1].tint = ar_tint --grey +access_radar.pictures.layers[2].tint = ar_tint --grey local access_radar_item = table.deepcopy(data.raw["item"]["radar"]) access_radar_item.name = "access-radar" access_radar_item.place_result = "access-radar" access_radar_item.icons = { - { - icon = access_radar_item.icon, - icon_size = access_radar_item.icon_size, - tint = ar_tint - } + { + icon = access_radar_item.icon, + icon_size = access_radar_item.icon_size, + tint = ar_tint, + }, } local access_radar_recipe = table.deepcopy(data.raw["recipe"]["radar"]) access_radar_recipe.enabled = true access_radar_recipe.name = "access-radar" access_radar_recipe.result = "access-radar" -access_radar_recipe.ingredients = {{"electronic-circuit", 10}, {"iron-gear-wheel", 10}, {"iron-plate", 20}} +access_radar_recipe.ingredients = { { "electronic-circuit", 10 }, { "iron-gear-wheel", 10 }, { "iron-plate", 20 } } -data:extend{access_radar,access_radar_item} -data:extend{access_radar_item,access_radar_recipe} +data:extend({ access_radar, access_radar_item }) +data:extend({ access_radar_item, access_radar_recipe }) ---New presets for map generation (deprecated?) -resource_def={richness = 4} +resource_def = { richness = 4 } data.raw["map-gen-presets"].default["faccess-compass-valley"] = { - order="_A", - basic_settings={ - autoplace_controls = { - coal = resource_def, - ["copper-ore"] = resource_def, - ["crude-oil"] = resource_def, - ["iron-ore"] = resource_def, - stone = resource_def, - ["uranium-ore"] = resource_def - }, - seed = 3814061204, - starting_area = 4, - peaceful_mode = true, - cliff_settings = { - name = "cliff", - cliff_elevation_0 = 10, - cliff_elevation_interval = 240, - richness = 0.1666666716337204 - } - }, - advanced_settings ={ - enemy_evolution = { - enabled = true, - time_factor = 0, - destroy_factor = 0.006, - pollution_factor = 1e-07 - }, - enemy_expansion ={ - enabled = false - } - } + order = "_A", + basic_settings = { + autoplace_controls = { + coal = resource_def, + ["copper-ore"] = resource_def, + ["crude-oil"] = resource_def, + ["iron-ore"] = resource_def, + stone = resource_def, + ["uranium-ore"] = resource_def, + }, + seed = 3814061204, + starting_area = 4, + peaceful_mode = true, + cliff_settings = { + name = "cliff", + cliff_elevation_0 = 10, + cliff_elevation_interval = 240, + richness = 0.1666666716337204, + }, + }, + advanced_settings = { + enemy_evolution = { + enabled = true, + time_factor = 0, + destroy_factor = 0.006, + pollution_factor = 1e-07, + }, + enemy_expansion = { + enabled = false, + }, + }, } data.raw["map-gen-presets"].default["faccess-enemies-off"] = { - order="_B", - basic_settings={ - autoplace_controls = { - ["enemy-base"] = {frequency=0} - } - } + order = "_B", + basic_settings = { + autoplace_controls = { + ["enemy-base"] = { frequency = 0 }, + }, + }, } data.raw["map-gen-presets"].default["faccess-peaceful"] = { - order="_C", - basic_settings={ - peaceful_mode = true, - } + order = "_C", + basic_settings = { + peaceful_mode = true, + }, } --New sound files-- data:extend({ -{ - type = "sound", - name = "alert-enemy-presence-high", - filename = "__FactorioAccess__/Audio/alert-enemy-presence-high-zapsplat-trimmed-science_fiction_alarm_fast_high_pitched_warning_tone_emergency_003_60104.wav", - volume = 0.4, - preload = true -}, - -{ - type = "sound", - name = "alert-enemy-presence-low", - filename = "__FactorioAccess__/Audio/alert-enemy-presence-low-zapsplat-modified_multimedia_game_tone_short_bright_futuristic_beep_action_tone_002_59161.wav", - volume = 0.4, - preload = true -}, - -{ - type = "sound", - name = "alert-structure-damaged", - filename = "__FactorioAccess__/Audio/alert-structure-damaged-zapsplat-modified-emergency_alarm_003.wav", - volume = 1, - preload = true -}, - -{ - type = "sound", - name = "Open-Inventory-Sound", - filename = "__core__/sound/gui-green-button.ogg", - volume = 1, - preload = true -}, - -{ - type = "sound", - name = "Close-Inventory-Sound", - filename = "__core__/sound/gui-green-confirm.ogg", - volume = 1, - preload = true -}, - -{ - type = "sound", - name = "Change-Menu-Tab-Sound", - filename = "__core__/sound/gui-switch.ogg", - volume = 1, - preload = true -}, - -{ - type = "sound", - name = "inventory-edge", - filename = "__FactorioAccess__/Audio/inventory-edge-zapsplat_vehicles_car_roof_light_switch_click_002_80933.wav", - volume = 1, - preload = true -}, - -{ - type = "sound", - name = "Inventory-Move", - filename = "__FactorioAccess__/Audio/inventory-move.ogg", - volume = 1, - preload = true -}, - -{ - type = "sound", - name = "inventory-wrap-around", - filename = "__FactorioAccess__/Audio/inventory-wrap-around-zapsplat_leisure_toy_plastic_wind_up_003_13198.wav", - volume = 1, - preload = true -}, - -{ - type = "sound", - name = "player-aim-locked", - filename = "__FactorioAccess__/Audio/player-aim-locked-zapsplat_multimedia_game_beep_high_pitched_generic_002_25862.wav", - volume = 0.5, - preload = true -}, - -{ - type = "sound", - name = "player-bump-alert", - filename = "__FactorioAccess__/Audio/player-bump-alert-zapsplat-trimmed_multimedia_game_sound_synth_digital_tone_beep_001_38533.wav", - volume = 0.75, - preload = true -}, - -{ - type = "sound", - name = "player-bump-stuck-alert", - filename = "__FactorioAccess__/Audio/player-bump-stuck-alert-zapsplat_multimedia_game_sound_synth_digital_tone_beep_005_38537.wav", - volume = 0.75, - preload = true -}, - -{ - type = "sound", - name = "player-bump-slide", - filename = "__FactorioAccess__/Audio/player-bump-slide-zapsplat_foley_footstep_boot_kick_gravel_stones_out_002.wav", - volume = 1, - preload = true -}, - -{ - type = "sound", - name = "player-bump-trip", - filename = "__FactorioAccess__/Audio/player-bump-trip-zapsplat-trimmed_industrial_tool_pick_axe_single_hit_strike_wood_tree_trunk_001_103466.wav", - volume = 1, - preload = true -}, - -{ - type = "sound", - name = "player-crafting", - filename = "__FactorioAccess__/Audio/player-crafting-zapsplat-modified_industrial_mechanical_wind_up_manual_001_86125.wav", - volume = 0.25, - preload = true -}, - -{ - type = "sound", - name = "player-damaged-character", - filename = "__FactorioAccess__/Audio/player-damaged-character-zapsplat-modified_multimedia_beep_harsh_synth_single_high_pitched_87498.wav", - volume = 0.75, - preload = true -}, - -{ - type = "sound", - name = "player-damaged-shield", - filename = "__FactorioAccess__/Audio/player-damaged-shield-zapsplat_multimedia_game_sound_sci_fi_futuristic_beep_action_tone_001_64989.wav", - volume = 0.75, - preload = true -}, - -{ - type = "sound", - name = "player-mine", - filename = "__FactorioAccess__/Audio/player-mine_02.ogg", - volume = 1, - preload = true -}, - -{ - type = "sound", - name = "player-teleported", - filename = "__FactorioAccess__/Audio/player-teleported-zapsplat_science_fiction_computer_alarm_single_medium_ring_beep_fast_004_84296.wav", - volume = 0.75, - preload = true -}, - -{ - type = "sound", - name = "player-turned", - filename = "__FactorioAccess__/Audio/player-turned-1face_dir.ogg", - volume = 1, - preload = true -}, - -{ - type = "sound", - name = "player-walk", - filename = "__FactorioAccess__/Audio/player-walk-zapsplat-little_robot_sound_factory_fantasy_Footstep_Dirt_001.wav", - volume = 1, - preload = true -}, - -{ - type = "sound", - name = "Rotate-Hand-Sound", - filename = "__core__/sound/gui-back.ogg", - volume = 1, - preload = true -}, - -{ - type = "sound", - name = "scanner-pulse", - filename = "__FactorioAccess__/Audio/scanner-pulse-zapsplat_science_fiction_computer_alarm_single_medium_ring_beep_fast_001_84293.wav", - volume = 0.3, - preload = true -}, - -{ - type = "sound", - name = "train-alert-high", - filename = "__FactorioAccess__/Audio/train-alert-high-zapsplat-trimmed_science_fiction_alarm_warning_buzz_harsh_large_reverb_60111.wav", - volume = 0.3, - preload = true -}, - -{ - type = "sound", - name = "train-alert-low", - filename = "__FactorioAccess__/Audio/train-alert-low-zapsplat_multimedia_beep_digital_high_tech_electronic_001_87483.wav", - volume = 0.3, - preload = true -}, - -{ - type = "sound", - name = "train-clack", - filename = "__FactorioAccess__/Audio/train-clack-zapsplat-cut-transport_steam_train_arrive_at_station_with_tannoy_announcement.wav", - volume = 1, - preload = true -}, - -{ - type = "sound", - name = "train-honk-short", - filename = "__FactorioAccess__/Audio/train-honk-short-2x-GotLag.ogg", - volume = 1, - preload = true -}, - -{ - type = "sound", - name = "train-honk-long", - filename = "__FactorioAccess__/Audio/train-honk-long-pixabay-modified-diesel-horn-02-98042.wav", - volume = 1, - preload = true -}, - -{ - type = "sound", - name = "train-honk-low-long", - filename = "__FactorioAccess__/Audio/train-honk-long-pixabay-modified-lower-diesel-horn-02-98042.wav", - volume = 1, - preload = true -}, - -{ - type = "sound", - name = "car-honk", - filename = "__FactorioAccess__/Audio/car-horn-zapsplat_transport_car_horn_single_beep_external_toyota_corolla_002_18246.wav", - volume = 1, - preload = true -}, - -{ - type = "sound", - name = "tank-honk", - filename = "__FactorioAccess__/Audio/tank-horn-zapsplat-Blastwave_FX_FireTruckHornHonk_SFXB.458.wav", - volume = 1, - preload = true -}}) + { + type = "sound", + name = "alert-enemy-presence-high", + filename = "__FactorioAccess__/Audio/alert-enemy-presence-high-zapsplat-trimmed-science_fiction_alarm_fast_high_pitched_warning_tone_emergency_003_60104.wav", + volume = 0.4, + preload = true, + }, + + { + type = "sound", + name = "alert-enemy-presence-low", + filename = "__FactorioAccess__/Audio/alert-enemy-presence-low-zapsplat-modified_multimedia_game_tone_short_bright_futuristic_beep_action_tone_002_59161.wav", + volume = 0.4, + preload = true, + }, + + { + type = "sound", + name = "alert-structure-damaged", + filename = "__FactorioAccess__/Audio/alert-structure-damaged-zapsplat-modified-emergency_alarm_003.wav", + volume = 1, + preload = true, + }, + + { + type = "sound", + name = "Open-Inventory-Sound", + filename = "__core__/sound/gui-green-button.ogg", + volume = 1, + preload = true, + }, + + { + type = "sound", + name = "Close-Inventory-Sound", + filename = "__core__/sound/gui-green-confirm.ogg", + volume = 1, + preload = true, + }, + + { + type = "sound", + name = "Change-Menu-Tab-Sound", + filename = "__core__/sound/gui-switch.ogg", + volume = 1, + preload = true, + }, + + { + type = "sound", + name = "inventory-edge", + filename = "__FactorioAccess__/Audio/inventory-edge-zapsplat_vehicles_car_roof_light_switch_click_002_80933.wav", + volume = 1, + preload = true, + }, + + { + type = "sound", + name = "Inventory-Move", + filename = "__FactorioAccess__/Audio/inventory-move.ogg", + volume = 1, + preload = true, + }, + + { + type = "sound", + name = "inventory-wrap-around", + filename = "__FactorioAccess__/Audio/inventory-wrap-around-zapsplat_leisure_toy_plastic_wind_up_003_13198.wav", + volume = 1, + preload = true, + }, + + { + type = "sound", + name = "player-aim-locked", + filename = "__FactorioAccess__/Audio/player-aim-locked-zapsplat_multimedia_game_beep_high_pitched_generic_002_25862.wav", + volume = 0.5, + preload = true, + }, + + { + type = "sound", + name = "player-bump-alert", + filename = "__FactorioAccess__/Audio/player-bump-alert-zapsplat-trimmed_multimedia_game_sound_synth_digital_tone_beep_001_38533.wav", + volume = 0.75, + preload = true, + }, + + { + type = "sound", + name = "player-bump-stuck-alert", + filename = "__FactorioAccess__/Audio/player-bump-stuck-alert-zapsplat_multimedia_game_sound_synth_digital_tone_beep_005_38537.wav", + volume = 0.75, + preload = true, + }, + + { + type = "sound", + name = "player-bump-slide", + filename = "__FactorioAccess__/Audio/player-bump-slide-zapsplat_foley_footstep_boot_kick_gravel_stones_out_002.wav", + volume = 1, + preload = true, + }, + + { + type = "sound", + name = "player-bump-trip", + filename = "__FactorioAccess__/Audio/player-bump-trip-zapsplat-trimmed_industrial_tool_pick_axe_single_hit_strike_wood_tree_trunk_001_103466.wav", + volume = 1, + preload = true, + }, + + { + type = "sound", + name = "player-crafting", + filename = "__FactorioAccess__/Audio/player-crafting-zapsplat-modified_industrial_mechanical_wind_up_manual_001_86125.wav", + volume = 0.25, + preload = true, + }, + + { + type = "sound", + name = "player-damaged-character", + filename = "__FactorioAccess__/Audio/player-damaged-character-zapsplat-modified_multimedia_beep_harsh_synth_single_high_pitched_87498.wav", + volume = 0.75, + preload = true, + }, + + { + type = "sound", + name = "player-damaged-shield", + filename = "__FactorioAccess__/Audio/player-damaged-shield-zapsplat_multimedia_game_sound_sci_fi_futuristic_beep_action_tone_001_64989.wav", + volume = 0.75, + preload = true, + }, + + { + type = "sound", + name = "player-mine", + filename = "__FactorioAccess__/Audio/player-mine_02.ogg", + volume = 1, + preload = true, + }, + + { + type = "sound", + name = "player-teleported", + filename = "__FactorioAccess__/Audio/player-teleported-zapsplat_science_fiction_computer_alarm_single_medium_ring_beep_fast_004_84296.wav", + volume = 0.75, + preload = true, + }, + + { + type = "sound", + name = "player-turned", + filename = "__FactorioAccess__/Audio/player-turned-1face_dir.ogg", + volume = 1, + preload = true, + }, + + { + type = "sound", + name = "player-walk", + filename = "__FactorioAccess__/Audio/player-walk-zapsplat-little_robot_sound_factory_fantasy_Footstep_Dirt_001.wav", + volume = 1, + preload = true, + }, + + { + type = "sound", + name = "Rotate-Hand-Sound", + filename = "__core__/sound/gui-back.ogg", + volume = 1, + preload = true, + }, + + { + type = "sound", + name = "scanner-pulse", + filename = "__FactorioAccess__/Audio/scanner-pulse-zapsplat_science_fiction_computer_alarm_single_medium_ring_beep_fast_001_84293.wav", + volume = 0.3, + preload = true, + }, + + { + type = "sound", + name = "train-alert-high", + filename = "__FactorioAccess__/Audio/train-alert-high-zapsplat-trimmed_science_fiction_alarm_warning_buzz_harsh_large_reverb_60111.wav", + volume = 0.3, + preload = true, + }, + + { + type = "sound", + name = "train-alert-low", + filename = "__FactorioAccess__/Audio/train-alert-low-zapsplat_multimedia_beep_digital_high_tech_electronic_001_87483.wav", + volume = 0.3, + preload = true, + }, + + { + type = "sound", + name = "train-clack", + filename = "__FactorioAccess__/Audio/train-clack-zapsplat-cut-transport_steam_train_arrive_at_station_with_tannoy_announcement.wav", + volume = 1, + preload = true, + }, + + { + type = "sound", + name = "train-honk-short", + filename = "__FactorioAccess__/Audio/train-honk-short-2x-GotLag.ogg", + volume = 1, + preload = true, + }, + + { + type = "sound", + name = "train-honk-long", + filename = "__FactorioAccess__/Audio/train-honk-long-pixabay-modified-diesel-horn-02-98042.wav", + volume = 1, + preload = true, + }, + + { + type = "sound", + name = "train-honk-low-long", + filename = "__FactorioAccess__/Audio/train-honk-long-pixabay-modified-lower-diesel-horn-02-98042.wav", + volume = 1, + preload = true, + }, + + { + type = "sound", + name = "car-honk", + filename = "__FactorioAccess__/Audio/car-horn-zapsplat_transport_car_horn_single_beep_external_toyota_corolla_002_18246.wav", + volume = 1, + preload = true, + }, + + { + type = "sound", + name = "tank-honk", + filename = "__FactorioAccess__/Audio/tank-horn-zapsplat-Blastwave_FX_FireTruckHornHonk_SFXB.458.wav", + volume = 1, + preload = true, + }, +}) --New custom input events-- data:extend({ -{ - type = "custom-input", - name = "pause-game-fa", - key_sequence = "ESCAPE", - linked_game_control = "toggle-menu", - consuming = "none" -}, - -{ - type = "custom-input", - name = "cursor-up", - key_sequence = "W", - linked_game_control = "move-up", - consuming = "none" -}, -{ - type = "custom-input", - name = "cursor-down", - key_sequence = "S", - linked_game_control = "move-down", - consuming = "none" -}, -{ - type = "custom-input", - name = "cursor-left", - key_sequence = "A", - linked_game_control = "move-left", - consuming = "none" -}, -{ - type = "custom-input", - name = "cursor-right", - key_sequence = "D", - linked_game_control = "move-right", - consuming = "none" -}, - -{ - type = "custom-input", - name = "cursor-skip-north", - key_sequence = "SHIFT + W", - consuming = "none" -}, -{ - type = "custom-input", - name = "cursor-skip-south", - key_sequence = "SHIFT + S", - consuming = "none" -}, -{ - type = "custom-input", - name = "cursor-skip-west", - key_sequence = "SHIFT + A", - consuming = "none" -}, -{ - type = "custom-input", - name = "cursor-skip-east", - key_sequence = "SHIFT + D", - consuming = "none" -}, - -{ - type = "custom-input", - name = "nudge-up", - key_sequence = "CONTROL + SHIFT + W", - consuming = "none" -}, -{ - type = "custom-input", - name = "nudge-down", - key_sequence = "CONTROL + SHIFT + S", - consuming = "none" -}, -{ - type = "custom-input", - name = "nudge-left", - key_sequence = "CONTROL + SHIFT + A", - consuming = "none" -}, -{ - type = "custom-input", - name = "nudge-right", - key_sequence = "CONTROL + SHIFT + D", - consuming = "none" -}, - -{ - type = "custom-input", - name = "read-cursor-coords", - key_sequence = "K", - consuming = "none" -}, - -{ - type = "custom-input", - name = "read-cursor-distance-and-direction", - key_sequence = "SHIFT + K", - consuming = "none" -}, - -{ - type = "custom-input", - name = "read-cursor-distance-vector", - key_sequence = "ALT + K", - consuming = "none" -}, - -{ - type = "custom-input", - name = "read-character-coords", - key_sequence = "CONTROL + K", - consuming = "none" -}, - -{ - type = "custom-input", - name = "return-cursor-to-player", - key_sequence = "J", - consuming = "none" -}, - -{ - type = "custom-input", - name = "cursor-bookmark-save", - key_sequence = "SHIFT + B", - consuming = "none" -}, - -{ - type = "custom-input", - name = "cursor-bookmark-load", - key_sequence = "B", - consuming = "none" -}, - -{ - type = "custom-input", - name = "type-cursor-target", - key_sequence = "ALT + T", - consuming = "none" -}, - -{ - type = "custom-input", - name = "teleport-to-cursor", - key_sequence = "SHIFT + T", - consuming = "none" -}, - -{ - type = "custom-input", - name = "teleport-to-cursor-forced", - key_sequence = "CONTROL + SHIFT + T", - consuming = "none" -}, - -{ - type = "custom-input", - name = "teleport-to-alert-forced", - key_sequence = "CONTROL + SHIFT + P", - consuming = "none" -}, - -{ - type = "custom-input", - name = "toggle-cursor", - key_sequence = "I", - consuming = "none" -}, - -{ - type = "custom-input", - name = "cursor-size-increment", - key_sequence = "SHIFT + I", - consuming = "none" -}, - -{ - type = "custom-input", - name = "cursor-size-decrement", - key_sequence = "CONTROL + I", - consuming = "none" -}, - -{ - type = "custom-input", - name = "toggle-remote-view", - key_sequence = "ALT + I", - consuming = "none" -}, - -{ - type = "custom-input", - name = "increase-inventory-bar-by-1", - key_sequence = "PAGEUP", - alternative_key_sequence = "ALT + UP", - consuming = "none" -}, - -{ - type = "custom-input", - name = "increase-inventory-bar-by-5", - key_sequence = "SHIFT + PAGEUP", - alternative_key_sequence = "SHIFT + UP", - consuming = "none" -}, - -{ - type = "custom-input", - name = "increase-inventory-bar-by-100", - key_sequence = "CONTROL + PAGEUP", - alternative_key_sequence = "CTRL + UP", - consuming = "none" -}, - -{ - type = "custom-input", - name = "decrease-inventory-bar-by-1", - key_sequence = "PAGEDOWN", - alternative_key_sequence = "ALT + DOWN", - consuming = "none" -}, - -{ - type = "custom-input", - name = "decrease-inventory-bar-by-5", - key_sequence = "SHIFT + PAGEDOWN", - alternative_key_sequence = "SHIFT + DOWN", - consuming = "none" -}, - -{ - type = "custom-input", - name = "decrease-inventory-bar-by-100", - key_sequence = "CONTROL + PAGEDOWN", - alternative_key_sequence = "CTRL + DOWN", - consuming = "none" -}, - -{ - type = "custom-input", - name = "increase-train-wait-times-by-5", - key_sequence = "PAGEUP", - alternative_key_sequence = "ALT + UP", - consuming = "none" -}, - -{ - type = "custom-input", - name = "increase-train-wait-times-by-60", - key_sequence = "CONTROL + PAGEUP", - alternative_key_sequence = "CTRL + UP", - consuming = "none" -}, - -{ - type = "custom-input", - name = "decrease-train-wait-times-by-5", - key_sequence = "PAGEDOWN", - alternative_key_sequence = "ALT + DOWN", - consuming = "none" -}, - -{ - type = "custom-input", - name = "decrease-train-wait-times-by-60", - key_sequence = "CONTROL + PAGEDOWN", - alternative_key_sequence = "CTRL + DOWN", - consuming = "none" -}, - -{ - type = "custom-input", - name = "read-rail-structure-ahead", - key_sequence = "SHIFT + J", - consuming = "none" -}, - -{ - type = "custom-input", - name = "read-driving-structure-ahead", - key_sequence = "J", - consuming = "none" -}, - -{ - type = "custom-input", - name = "read-rail-structure-behind", - key_sequence = "CONTROL + J", - consuming = "none" -}, - -{ - type = "custom-input", - name = "rescan", - key_sequence = "END", - alternative_key_sequence = "RCTRL", - consuming = "none" -}, - -{ - type = "custom-input", - name = "scan-facing-direction", - key_sequence = "SHIFT + END", - alternative_key_sequence = "SHIFT + RCTRL", - consuming = "none" -}, - -{ - type = "custom-input", - name = "a-scan-list-main-up-key", - key_sequence = "PAGEUP", - consuming = "none" -}, - -{ - type = "custom-input", - name = "a-scan-list-main-down-key", - key_sequence = "PAGEDOWN", - consuming = "none" -}, - -{ - type = "custom-input", - name = "scan-list-up", - key_sequence = "PAGEUP", - alternative_key_sequence = "ALT + UP", - consuming = "none" -}, - -{ - type = "custom-input", - name = "scan-list-down", - key_sequence = "PAGEDOWN", - alternative_key_sequence = "ALT + DOWN", - consuming = "none" -}, - -{ - type = "custom-input", - name = "scan-list-middle", - key_sequence = "HOME", - alternative_key_sequence = "RSHIFT", - consuming = "none" -}, -{ - type = "custom-input", - name = "jump-to-scan", - key_sequence = "CONTROL + HOME", - alternative_key_sequence = "CONTROL + RSHIFT", - consuming = "none" -}, - -{ - type = "custom-input", - name = "scan-category-up", - key_sequence = "CONTROL + PAGEUP", - alternative_key_sequence = "CONTROL + UP", - consuming = "none" -}, - -{ - type = "custom-input", - name = "scan-category-down", - key_sequence = "CONTROL + PAGEDOWN", - alternative_key_sequence = "CONTROL + DOWN", - consuming = "none" -}, - -{ - type = "custom-input", - name = "scan-sort-by-count", - key_sequence = "SHIFT + N", - consuming = "none" -}, - -{ - type = "custom-input", - name = "scan-sort-by-distance", - key_sequence = "N", - consuming = "none" -}, - -{ - type = "custom-input", - name = "scan-selection-up", - key_sequence = "SHIFT + PAGEUP", - alternative_key_sequence = "SHIFT + UP", - consuming = "none" -}, - -{ - type = "custom-input", - name = "scan-selection-down", - key_sequence = "SHIFT + PAGEDOWN", - alternative_key_sequence = "SHIFT + DOWN", - consuming = "none" -}, - -{ - type = "custom-input", - name = "repeat-last-spoken", - key_sequence = "CONTROL + TAB", - consuming = "none" -}, - -{ - type = "custom-input", - name = "tile-cycle", - key_sequence = "SHIFT + F", - consuming = "none" -}, - -{ - type = "custom-input", - name = "pickup-items-info", - key_sequence = "F", - linked_game_control = "pick-items", - consuming = "none" -}, - -{ - type = "custom-input", - name = "open-inventory", - key_sequence = "E", - consuming = "none" -}, - -{ - type = "custom-input", - name = "close-menu-access", - key_sequence = "E", - consuming = "none" -}, - -{ - type = "custom-input", - name = "read-menu-name", - key_sequence = "SHIFT + E", - consuming = "none" -}, - -{ - type = "custom-input", - name = "quickbar-1", - key_sequence = "1", - linked_game_control = "quick-bar-button-1", - consuming = "none" -}, - -{ - type = "custom-input", - name = "quickbar-2", - key_sequence = "2", - linked_game_control = "quick-bar-button-2", - consuming = "none" -}, - -{ - type = "custom-input", - name = "quickbar-3", - key_sequence = "3", - linked_game_control = "quick-bar-button-3", - consuming = "none" -}, - -{ - type = "custom-input", - name = "quickbar-4", - key_sequence = "4", - linked_game_control = "quick-bar-button-4", - consuming = "none" -}, - -{ - type = "custom-input", - name = "quickbar-5", - key_sequence = "5", - linked_game_control = "quick-bar-button-5", - consuming = "none" -}, - -{ - type = "custom-input", - name = "quickbar-6", - key_sequence = "6", - linked_game_control = "quick-bar-button-6", - consuming = "none" -}, - -{ - type = "custom-input", - name = "quickbar-7", - key_sequence = "7", - linked_game_control = "quick-bar-button-7", - consuming = "none" -}, - -{ - type = "custom-input", - name = "quickbar-8", - key_sequence = "8", - linked_game_control = "quick-bar-button-8", - consuming = "none" -}, - -{ - type = "custom-input", - name = "quickbar-9", - key_sequence = "9", - linked_game_control = "quick-bar-button-9", - consuming = "none" -}, - -{ - type = "custom-input", - name = "quickbar-10", - key_sequence = "0", - linked_game_control = "quick-bar-button-10", - consuming = "none" -}, - -{ - type = "custom-input", - name = "set-quickbar-1", - key_sequence = "CONTROL + 1", - consuming = "none" -}, - -{ - type = "custom-input", - name = "set-quickbar-2", - key_sequence = "CONTROL + 2", - consuming = "none" -}, - -{ - type = "custom-input", - name = "set-quickbar-3", - key_sequence = "CONTROL + 3", - consuming = "none" -}, - -{ - type = "custom-input", - name = "set-quickbar-4", - key_sequence = "CONTROL + 4", - consuming = "none" -}, - -{ - type = "custom-input", - name = "set-quickbar-5", - key_sequence = "CONTROL + 5", - consuming = "none" -}, - -{ - type = "custom-input", - name = "set-quickbar-6", - key_sequence = "CONTROL + 6", - consuming = "none" -}, - -{ - type = "custom-input", - name = "set-quickbar-7", - key_sequence = "CONTROL + 7", - consuming = "none" -}, - -{ - type = "custom-input", - name = "set-quickbar-8", - key_sequence = "CONTROL + 8", - consuming = "none" -}, - -{ - type = "custom-input", - name = "set-quickbar-9", - key_sequence = "CONTROL + 9", - consuming = "none" -}, - -{ - type = "custom-input", - name = "set-quickbar-10", - key_sequence = "CONTROL + 0", - consuming = "none" -}, - -{ - type = "custom-input", - name = "quickbar-page-1", - key_sequence = "SHIFT + 1", - consuming = "none" -}, - -{ - type = "custom-input", - name = "quickbar-page-2", - key_sequence = "SHIFT + 2", - consuming = "none" -}, - -{ - type = "custom-input", - name = "quickbar-page-3", - key_sequence = "SHIFT + 3", - consuming = "none" -}, - -{ - type = "custom-input", - name = "quickbar-page-4", - key_sequence = "SHIFT + 4", - consuming = "none" -}, - -{ - type = "custom-input", - name = "quickbar-page-5", - key_sequence = "SHIFT + 5", - consuming = "none" -}, - -{ - type = "custom-input", - name = "quickbar-page-6", - key_sequence = "SHIFT + 6", - consuming = "none" -}, - -{ - type = "custom-input", - name = "quickbar-page-7", - key_sequence = "SHIFT + 7", - consuming = "none" -}, - -{ - type = "custom-input", - name = "quickbar-page-8", - key_sequence = "SHIFT + 8", - consuming = "none" -}, - -{ - type = "custom-input", - name = "quickbar-page-9", - key_sequence = "SHIFT + 9", - consuming = "none" -}, - -{ - type = "custom-input", - name = "quickbar-page-10", - key_sequence = "SHIFT + 0", - consuming = "none" -}, - -{ - type = "custom-input", - name = "switch-menu-or-gun", - key_sequence = "TAB", - consuming = "none" -}, - -{ - type = "custom-input", - name = "reverse-switch-menu-or-gun", - key_sequence = "SHIFT + TAB", - consuming = "none" -}, - -{ - type = "custom-input", - name = "mine-access-sounds", - key_sequence = "X", - linked_game_control = "mine", - consuming = "none" -}, - -{ - type = "custom-input", - name = "mine-tiles", - key_sequence = "X", - linked_game_control = "mine", - consuming = "none" -}, - -{ - type = "custom-input", - name = "flush-fluid", - key_sequence = "X", - linked_game_control = "mine", - consuming = "none" -}, - -{ - type = "custom-input", - name = "mine-area", - key_sequence = "SHIFT + X", - consuming = "none" -}, - -{ - type = "custom-input", - name = "cut-paste-tool-comment", - key_sequence = "CONTROL + X", - consuming = "none" -}, - -{ - type = "custom-input", - name = "leftbracket-key-id", - key_sequence = "LEFTBRACKET", - consuming = "none" -}, - -{ - type = "custom-input", - name = "rightbracket-key-id", - key_sequence = "RIGHTBRACKET", - consuming = "none" -}, - -{ - type = "custom-input", - name = "click-menu", - key_sequence = "LEFTBRACKET", - consuming = "none" -}, - -{ - type = "custom-input", - name = "click-menu-right", - key_sequence = "RIGHTBRACKET", - consuming = "none" -}, - -{ - type = "custom-input", - name = "click-hand", - key_sequence = "LEFTBRACKET", - consuming = "none" -}, - -{ - type = "custom-input", - name = "click-hand-right", - key_sequence = "RIGHTBRACKET", - consuming = "none" -}, - -{ - type = "custom-input", - name = "click-entity", - key_sequence = "LEFTBRACKET", - alternative_key_sequence = "mouse-button-1", - consuming = "none" -}, - -{ - type = "custom-input", - name = "open-circuit-menu", - key_sequence = "N", - consuming = "none" -}, - -{ - type = "custom-input", - name = "repair-area", - key_sequence = "CONTROL + SHIFT + LEFTBRACKET", - consuming = "none" -}, - -{ - type = "custom-input", - name = "crafting-all", - key_sequence = "SHIFT + LEFTBRACKET", - consuming = "none" -}, - -{ - type = "custom-input", - name = "transfer-one-stack", - key_sequence = "SHIFT + LEFTBRACKET", - consuming = "none" -}, - -{ - type = "custom-input", - name = "equip-item", - key_sequence = "SHIFT + LEFTBRACKET", - consuming = "none" -}, - -{ - type = "custom-input", - name = "open-rail-builder", - key_sequence = "SHIFT + LEFTBRACKET", - consuming = "none" -}, - -{ - type = "custom-input", - name = "quick-build-rail-left-turn", - key_sequence = "CONTROL + LEFT", - consuming = "none" -}, - -{ - type = "custom-input", - name = "quick-build-rail-right-turn", - key_sequence = "CONTROL + RIGHT", - consuming = "none" -}, - -{ - type = "custom-input", - name = "transfer-all-stacks", - key_sequence = "CONTROL + LEFTBRACKET", - consuming = "none" -}, - -{ - type = "custom-input", - name = "fa-alternate-build", - key_sequence = "CONTROL + LEFTBRACKET", - consuming = "none" -}, - -{ - type = "custom-input", - name = "transfer-half-of-all-stacks", - key_sequence = "CONTROL + RIGHTBRACKET", - consuming = "none" -}, - -{ - type = "custom-input", - name = "crafting-5", - key_sequence = "RIGHTBRACKET", - consuming = "none" -}, - -{ - type = "custom-input", - name = "menu-clear-filter", - key_sequence = "RIGHTBRACKET", - consuming = "none" -}, - -{ - type = "custom-input", - name = "read-entity-status", - key_sequence = "RIGHTBRACKET", - consuming = "none" -}, - -{ - type = "custom-input", - name = "rotate-building", - key_sequence = "R", - linked_game_control = "rotate", - consuming = "none" -}, - -{ - type = "custom-input", - name = "reverse-rotate-building", - key_sequence = "SHIFT + R", - linked_game_control = "reverse-rotate", - consuming = "none" -}, - -{ - type = "custom-input", - name = "flip-blueprint-horizontal-info", - key_sequence = "F", - linked_game_control = "flip-blueprint-horizontal", - consuming = "none" -}, - -{ - type = "custom-input", - name = "flip-blueprint-vertical-info", - key_sequence = "G", - linked_game_control = "flip-blueprint-vertical", - consuming = "none" -}, - -{ - type = "custom-input", - name = "inventory-read-weapons-data", - key_sequence = "R", - consuming = "none" -}, - -{ - type = "custom-input", - name = "inventory-reload-weapons", - key_sequence = "SHIFT + R", - consuming = "none" -}, - -{ - type = "custom-input", - name = "inventory-remove-all-weapons-and-ammo", - key_sequence = "CONTROL + SHIFT + R", - consuming = "none" -}, - -{ - type = "custom-input", - name = "item-info", - key_sequence = "Y", - consuming = "none" -}, - -{ - type = "custom-input", - name = "item-info-last-indexed", - key_sequence = "SHIFT + Y", - consuming = "none" -}, - -{ - type = "custom-input", - name = "item-production-info", - key_sequence = "U", - consuming = "none" -}, - -{ - type = "custom-input", - name = "cursor-pollution-info", - key_sequence = "SHIFT + U", - consuming = "none" -}, - -{ - type = "custom-input", - name = "read-time-and-research-progress", - key_sequence = "T", - consuming = "none" -}, - -{ - type = "custom-input", - name = "save-game-manually", - key_sequence = "F1", - consuming = "none" -}, - -{ - type = "custom-input", - name = "toggle-walk", - key_sequence = "CONTROL + W", - consuming = "none" -}, - -{ - type = "custom-input", - name = "toggle-build-lock", - key_sequence = "CONTROL + B", - consuming = "none" -}, - -{ - type = "custom-input", - name = "toggle-vanilla-mode", - key_sequence = "CONTROL + ALT + V", - consuming = "none" -}, - -{ - type = "custom-input", - name = "toggle-cursor-hiding", - key_sequence = "CONTROL + ALT + C", - consuming = "none" -}, - -{ - type = "custom-input", - name = "clear-renders", - key_sequence = "CONTROL + ALT + R", - consuming = "none" -}, - -{ - type = "custom-input", - name = "recalibrate-zoom", - key_sequence = "CONTROL + END", - alternative_key_sequence = "CONTROL + RCTRL", - consuming = "none" -}, - -{ - type = "custom-input", - name = "enable-mouse-update-entity-selection", - key_sequence = "mouse-button-3", - consuming = "none" -}, - -{ - type = "custom-input", - name = "pipette-tool-info", - key_sequence = "Q", - linked_game_control = "smart-pipette", - consuming = "none" -}, - -{ - type = "custom-input", - name = "copy-entity-settings-info", - key_sequence = "SHIFT + RIGHTBRACKET", - linked_game_control = "copy-entity-settings", - consuming = "none" -}, - -{ - type = "custom-input", - name = "paste-entity-settings-info", - key_sequence = "SHIFT + LEFTBRACKET", - linked_game_control = "paste-entity-settings", - consuming = "none" -}, - -{ - type = "custom-input", - name = "fast-entity-transfer-info", - key_sequence = "CONTROL + LEFTBRACKET", - linked_game_control = "fast-entity-transfer", - consuming = "none" -}, - -{ - type = "custom-input", - name = "fast-entity-split-info", - key_sequence = "CONTROL + RIGHTBRACKET", - linked_game_control = "fast-entity-split", - consuming = "none" -}, - -{ - type = "custom-input", - name = "drop-cursor-info", - key_sequence = "Z", - linked_game_control = "drop-cursor", - consuming = "none" -}, - -{ - type = "custom-input", - name = "read-hand", - key_sequence = "SHIFT + Q", - consuming = "none" -}, - -{ - type = "custom-input", - name = "locate-hand-in-inventory", - key_sequence = "CONTROL + Q", - consuming = "none" -}, - -{ - type = "custom-input", - name = "locate-hand-in-crafting-menu", - key_sequence = "CONTROL + SHIFT + Q", - consuming = "none" -}, - -{ - type = "custom-input", - name = "menu-search-open", - key_sequence = "CONTROL + F", - linked_game_control = "focus-search", - consuming = "game-only" -}, - -{ - type = "custom-input", - name = "menu-search-get-next", - key_sequence = "SHIFT + ENTER", - consuming = "none" -}, - -{ - type = "custom-input", - name = "menu-search-get-last", - key_sequence = "CONTROL + ENTER", - consuming = "none" -}, - -{ - type = "custom-input", - name = "open-warnings-menu", - key_sequence = "P", - consuming = "none" -}, - -{ - type = "custom-input", - name = "nearest-damaged-ent-info", - key_sequence = "SHIFT + P", - consuming = "none" -}, - -{ - type = "custom-input", - name = "open-fast-travel-menu", - key_sequence = "V", - consuming = "none" -}, - -{ - type = "custom-input", - name = "open-structure-travel-menu", - key_sequence = "CONTROL + S", - consuming = "none" -}, - -{ - type = "custom-input", - name = "alternative-menu-up", - key_sequence = "UP", - consuming = "none" -}, - -{ - type = "custom-input", - name = "alternative-menu-down", - key_sequence = "DOWN", - consuming = "none" -}, - -{ - type = "custom-input", - name = "alternative-menu-left", - key_sequence = "LEFT", - consuming = "none" -}, - -{ - type = "custom-input", - name = "alternative-menu-right", - key_sequence = "RIGHT", - consuming = "none" -}, - -{ - type = "custom-input", - name = "cursor-one-tile-north", - key_sequence = "UP", - consuming = "none" -}, - -{ - type = "custom-input", - name = "cursor-one-tile-south", - key_sequence = "DOWN", - consuming = "none" -}, - -{ - type = "custom-input", - name = "cursor-one-tile-east", - key_sequence = "RIGHT", - consuming = "none" -}, - -{ - type = "custom-input", - name = "cursor-one-tile-west", - key_sequence = "LEFT", - consuming = "none" -}, - -{ - type = "custom-input", - name = "set-splitter-input-priority-left", - key_sequence = "SHIFT + LEFT", - consuming = "none" -}, - -{ - type = "custom-input", - name = "set-splitter-input-priority-right", - key_sequence = "SHIFT + RIGHT", - consuming = "none" -}, - -{ - type = "custom-input", - name = "set-splitter-output-priority-left", - key_sequence = "CONTROL + LEFT", - consuming = "none" -}, - -{ - type = "custom-input", - name = "set-splitter-output-priority-right", - key_sequence = "CONTROL + RIGHT", - consuming = "none" -}, - -{ - type = "custom-input", - name = "set-entity-filter-from-hand", - key_sequence = "CONTROL + LEFTBRACKET", - consuming = "none" -}, - -{ - type = "custom-input", - name = "connect-rail-vehicles", - key_sequence = "G", - consuming = "none" -}, - -{ - type = "custom-input", - name = "disconnect-rail-vehicles", - key_sequence = "SHIFT + G", - consuming = "none" -}, - -{ - type = "custom-input", - name = "inventory-read-armor-stats", - key_sequence = "G", - consuming = "none" -}, - -{ - type = "custom-input", - name = "inventory-read-equipment-list", - key_sequence = "SHIFT + G", - consuming = "none" -}, - -{ - type = "custom-input", - name = "inventory-remove-all-equipment-and-armor", - key_sequence = "CONTROL + SHIFT + G", - consuming = "none" -}, - -{ - type = "custom-input", - name = "shoot-weapon-fa", - key_sequence = "SPACE", - --linked_game_control = "shoot-enemy", - consuming = "none" -}, - -{ - type = "custom-input", - name = "honk", - key_sequence = "V", - consuming = "none" -}, - -{ - type = "custom-input", - name = "launch-rocket", - key_sequence = "SPACE", - consuming = "none" -}, - -{ - type = "custom-input", - name = "help-read", - key_sequence = "H", - consuming = "none" -}, - -{ - type = "custom-input", - name = "help-next", - key_sequence = "CONTROL + H", - consuming = "none" -}, - -{ - type = "custom-input", - name = "help-back", - key_sequence = "SHIFT + H", - consuming = "none" -}, - -{ - type = "custom-input", - name = "help-chapter-next", - key_sequence = "CONTROL + ALT + H", - consuming = "none" -}, - -{ - type = "custom-input", - name = "help-chapter-back", - key_sequence = "SHIFT + ALT + H", - consuming = "none" -}, - -{ - type = "custom-input", - name = "help-toggle-header-mode", - key_sequence = "CONTROL + SHIFT + H", - consuming = "none" -}, - -{ - type = "custom-input", - name = "help-get-other", - key_sequence = "ALT + H", - consuming = "none" -}, - -{ - type = "custom-input", - name = "debug-test-key", - key_sequence = "ALT + G", - consuming = "none" -}, - -{ - type = "custom-input", - name = "fa-alt-zoom-in", - key_sequence = "X", - linked_game_control = "alt-zoom-in", - consuming = "none" -}, - -{ - type = "custom-input", - name = "fa-alt-zoom-out", - key_sequence = "X", - linked_game_control = "alt-zoom-out", - consuming = "none" -}, - -{ - type = "custom-input", - name = "fa-zoom-out", - key_sequence = "X", - linked_game_control = "zoom-out", - consuming = "none" -}, - -{ - type = "custom-input", - name = "fa-zoom-in", - key_sequence = "X", - linked_game_control = "zoom-in", - consuming = "none" -}, - -{ - type = "custom-input", - name = "fa-debug-reset-zoom-2x", - key_sequence = "X", - linked_game_control = "debug-reset-zoom-2x", - consuming = "none" -}, - -{ - type = "custom-input", - name = "fa-debug-reset-zoom", - key_sequence = "X", - linked_game_control = "debug-reset-zoom", - consuming = "none" -}, - -{ - type = "custom-input", - name = "logistic-request-read", - key_sequence = "L", - consuming = "none" -}, - -{ - type = "custom-input", - name = "logistic-request-increment-min", - key_sequence = "SHIFT + L", - consuming = "none" -}, - -{ - type = "custom-input", - name = "logistic-request-decrement-min", - key_sequence = "CONTROL + L", - consuming = "none" -}, - -{ - type = "custom-input", - name = "logistic-request-increment-max", - key_sequence = "SHIFT + ALT + L", - consuming = "none" -}, - -{ - type = "custom-input", - name = "logistic-request-decrement-max", - key_sequence = "CONTROL + ALT + L", - consuming = "none" -}, - -{ - type = "custom-input", - name = "logistic-request-toggle-personal-logistics", - key_sequence = "CONTROL + SHIFT + L", - consuming = "none" -}, - -{ - type = "custom-input", - name = "send-selected-stack-to-logistic-trash", - key_sequence = "O", - consuming = "none" -}, - -{ - type = "custom-input", - name = "fa-pda-driving-assistant-info", - key_sequence = "L", - consuming = "none" -}, - -{ - type = "custom-input", - name = "fa-pda-cruise-control-info", - key_sequence = "O", - consuming = "none" -}, - -{ - type = "custom-input", - name = "fa-pda-cruise-control-set-speed-info", - key_sequence = "CONTROL + O", - consuming = "none" -}, - -{ - type = "custom-input", - name = "access-config-version1-DO-NOT-EDIT", - key_sequence = "A", - consuming = "none" -}, - -{ - type = "custom-input", - name = "access-config-version2-DO-NOT-EDIT", - key_sequence = "A", - consuming = "none" -}}) + { + type = "custom-input", + name = "pause-game-fa", + key_sequence = "ESCAPE", + linked_game_control = "toggle-menu", + consuming = "none", + }, + + { + type = "custom-input", + name = "cursor-up", + key_sequence = "W", + linked_game_control = "move-up", + consuming = "none", + }, + { + type = "custom-input", + name = "cursor-down", + key_sequence = "S", + linked_game_control = "move-down", + consuming = "none", + }, + { + type = "custom-input", + name = "cursor-left", + key_sequence = "A", + linked_game_control = "move-left", + consuming = "none", + }, + { + type = "custom-input", + name = "cursor-right", + key_sequence = "D", + linked_game_control = "move-right", + consuming = "none", + }, + + { + type = "custom-input", + name = "cursor-skip-north", + key_sequence = "SHIFT + W", + consuming = "none", + }, + { + type = "custom-input", + name = "cursor-skip-south", + key_sequence = "SHIFT + S", + consuming = "none", + }, + { + type = "custom-input", + name = "cursor-skip-west", + key_sequence = "SHIFT + A", + consuming = "none", + }, + { + type = "custom-input", + name = "cursor-skip-east", + key_sequence = "SHIFT + D", + consuming = "none", + }, + + { + type = "custom-input", + name = "nudge-up", + key_sequence = "CONTROL + SHIFT + W", + consuming = "none", + }, + { + type = "custom-input", + name = "nudge-down", + key_sequence = "CONTROL + SHIFT + S", + consuming = "none", + }, + { + type = "custom-input", + name = "nudge-left", + key_sequence = "CONTROL + SHIFT + A", + consuming = "none", + }, + { + type = "custom-input", + name = "nudge-right", + key_sequence = "CONTROL + SHIFT + D", + consuming = "none", + }, + + { + type = "custom-input", + name = "read-cursor-coords", + key_sequence = "K", + consuming = "none", + }, + + { + type = "custom-input", + name = "read-cursor-distance-and-direction", + key_sequence = "SHIFT + K", + consuming = "none", + }, + + { + type = "custom-input", + name = "read-cursor-distance-vector", + key_sequence = "ALT + K", + consuming = "none", + }, + + { + type = "custom-input", + name = "read-character-coords", + key_sequence = "CONTROL + K", + consuming = "none", + }, + + { + type = "custom-input", + name = "return-cursor-to-player", + key_sequence = "J", + consuming = "none", + }, + + { + type = "custom-input", + name = "cursor-bookmark-save", + key_sequence = "SHIFT + B", + consuming = "none", + }, + + { + type = "custom-input", + name = "cursor-bookmark-load", + key_sequence = "B", + consuming = "none", + }, + + { + type = "custom-input", + name = "type-cursor-target", + key_sequence = "ALT + T", + consuming = "none", + }, + + { + type = "custom-input", + name = "teleport-to-cursor", + key_sequence = "SHIFT + T", + consuming = "none", + }, + + { + type = "custom-input", + name = "teleport-to-cursor-forced", + key_sequence = "CONTROL + SHIFT + T", + consuming = "none", + }, + + { + type = "custom-input", + name = "teleport-to-alert-forced", + key_sequence = "CONTROL + SHIFT + P", + consuming = "none", + }, + + { + type = "custom-input", + name = "toggle-cursor", + key_sequence = "I", + consuming = "none", + }, + + { + type = "custom-input", + name = "cursor-size-increment", + key_sequence = "SHIFT + I", + consuming = "none", + }, + + { + type = "custom-input", + name = "cursor-size-decrement", + key_sequence = "CONTROL + I", + consuming = "none", + }, + + { + type = "custom-input", + name = "toggle-remote-view", + key_sequence = "ALT + I", + consuming = "none", + }, + + { + type = "custom-input", + name = "increase-inventory-bar-by-1", + key_sequence = "PAGEUP", + alternative_key_sequence = "ALT + UP", + consuming = "none", + }, + + { + type = "custom-input", + name = "increase-inventory-bar-by-5", + key_sequence = "SHIFT + PAGEUP", + alternative_key_sequence = "SHIFT + UP", + consuming = "none", + }, + + { + type = "custom-input", + name = "increase-inventory-bar-by-100", + key_sequence = "CONTROL + PAGEUP", + alternative_key_sequence = "CTRL + UP", + consuming = "none", + }, + + { + type = "custom-input", + name = "decrease-inventory-bar-by-1", + key_sequence = "PAGEDOWN", + alternative_key_sequence = "ALT + DOWN", + consuming = "none", + }, + + { + type = "custom-input", + name = "decrease-inventory-bar-by-5", + key_sequence = "SHIFT + PAGEDOWN", + alternative_key_sequence = "SHIFT + DOWN", + consuming = "none", + }, + + { + type = "custom-input", + name = "decrease-inventory-bar-by-100", + key_sequence = "CONTROL + PAGEDOWN", + alternative_key_sequence = "CTRL + DOWN", + consuming = "none", + }, + + { + type = "custom-input", + name = "increase-train-wait-times-by-5", + key_sequence = "PAGEUP", + alternative_key_sequence = "ALT + UP", + consuming = "none", + }, + + { + type = "custom-input", + name = "increase-train-wait-times-by-60", + key_sequence = "CONTROL + PAGEUP", + alternative_key_sequence = "CTRL + UP", + consuming = "none", + }, + + { + type = "custom-input", + name = "decrease-train-wait-times-by-5", + key_sequence = "PAGEDOWN", + alternative_key_sequence = "ALT + DOWN", + consuming = "none", + }, + + { + type = "custom-input", + name = "decrease-train-wait-times-by-60", + key_sequence = "CONTROL + PAGEDOWN", + alternative_key_sequence = "CTRL + DOWN", + consuming = "none", + }, + + { + type = "custom-input", + name = "read-rail-structure-ahead", + key_sequence = "SHIFT + J", + consuming = "none", + }, + + { + type = "custom-input", + name = "read-driving-structure-ahead", + key_sequence = "J", + consuming = "none", + }, + + { + type = "custom-input", + name = "read-rail-structure-behind", + key_sequence = "CONTROL + J", + consuming = "none", + }, + + { + type = "custom-input", + name = "rescan", + key_sequence = "END", + alternative_key_sequence = "RCTRL", + consuming = "none", + }, + + { + type = "custom-input", + name = "scan-facing-direction", + key_sequence = "SHIFT + END", + alternative_key_sequence = "SHIFT + RCTRL", + consuming = "none", + }, + + { + type = "custom-input", + name = "a-scan-list-main-up-key", + key_sequence = "PAGEUP", + consuming = "none", + }, + + { + type = "custom-input", + name = "a-scan-list-main-down-key", + key_sequence = "PAGEDOWN", + consuming = "none", + }, + + { + type = "custom-input", + name = "scan-list-up", + key_sequence = "PAGEUP", + alternative_key_sequence = "ALT + UP", + consuming = "none", + }, + + { + type = "custom-input", + name = "scan-list-down", + key_sequence = "PAGEDOWN", + alternative_key_sequence = "ALT + DOWN", + consuming = "none", + }, + + { + type = "custom-input", + name = "scan-list-middle", + key_sequence = "HOME", + alternative_key_sequence = "RSHIFT", + consuming = "none", + }, + { + type = "custom-input", + name = "jump-to-scan", + key_sequence = "CONTROL + HOME", + alternative_key_sequence = "CONTROL + RSHIFT", + consuming = "none", + }, + + { + type = "custom-input", + name = "scan-category-up", + key_sequence = "CONTROL + PAGEUP", + alternative_key_sequence = "CONTROL + UP", + consuming = "none", + }, + + { + type = "custom-input", + name = "scan-category-down", + key_sequence = "CONTROL + PAGEDOWN", + alternative_key_sequence = "CONTROL + DOWN", + consuming = "none", + }, + + { + type = "custom-input", + name = "scan-sort-by-count", + key_sequence = "SHIFT + N", + consuming = "none", + }, + + { + type = "custom-input", + name = "scan-sort-by-distance", + key_sequence = "N", + consuming = "none", + }, + + { + type = "custom-input", + name = "scan-selection-up", + key_sequence = "SHIFT + PAGEUP", + alternative_key_sequence = "SHIFT + UP", + consuming = "none", + }, + + { + type = "custom-input", + name = "scan-selection-down", + key_sequence = "SHIFT + PAGEDOWN", + alternative_key_sequence = "SHIFT + DOWN", + consuming = "none", + }, + + { + type = "custom-input", + name = "repeat-last-spoken", + key_sequence = "CONTROL + TAB", + consuming = "none", + }, + + { + type = "custom-input", + name = "tile-cycle", + key_sequence = "SHIFT + F", + consuming = "none", + }, + + { + type = "custom-input", + name = "pickup-items-info", + key_sequence = "F", + linked_game_control = "pick-items", + consuming = "none", + }, + + { + type = "custom-input", + name = "open-inventory", + key_sequence = "E", + consuming = "none", + }, + + { + type = "custom-input", + name = "close-menu-access", + key_sequence = "E", + consuming = "none", + }, + + { + type = "custom-input", + name = "read-menu-name", + key_sequence = "SHIFT + E", + consuming = "none", + }, + + { + type = "custom-input", + name = "quickbar-1", + key_sequence = "1", + linked_game_control = "quick-bar-button-1", + consuming = "none", + }, + + { + type = "custom-input", + name = "quickbar-2", + key_sequence = "2", + linked_game_control = "quick-bar-button-2", + consuming = "none", + }, + + { + type = "custom-input", + name = "quickbar-3", + key_sequence = "3", + linked_game_control = "quick-bar-button-3", + consuming = "none", + }, + + { + type = "custom-input", + name = "quickbar-4", + key_sequence = "4", + linked_game_control = "quick-bar-button-4", + consuming = "none", + }, + + { + type = "custom-input", + name = "quickbar-5", + key_sequence = "5", + linked_game_control = "quick-bar-button-5", + consuming = "none", + }, + + { + type = "custom-input", + name = "quickbar-6", + key_sequence = "6", + linked_game_control = "quick-bar-button-6", + consuming = "none", + }, + + { + type = "custom-input", + name = "quickbar-7", + key_sequence = "7", + linked_game_control = "quick-bar-button-7", + consuming = "none", + }, + + { + type = "custom-input", + name = "quickbar-8", + key_sequence = "8", + linked_game_control = "quick-bar-button-8", + consuming = "none", + }, + + { + type = "custom-input", + name = "quickbar-9", + key_sequence = "9", + linked_game_control = "quick-bar-button-9", + consuming = "none", + }, + + { + type = "custom-input", + name = "quickbar-10", + key_sequence = "0", + linked_game_control = "quick-bar-button-10", + consuming = "none", + }, + + { + type = "custom-input", + name = "set-quickbar-1", + key_sequence = "CONTROL + 1", + consuming = "none", + }, + + { + type = "custom-input", + name = "set-quickbar-2", + key_sequence = "CONTROL + 2", + consuming = "none", + }, + + { + type = "custom-input", + name = "set-quickbar-3", + key_sequence = "CONTROL + 3", + consuming = "none", + }, + + { + type = "custom-input", + name = "set-quickbar-4", + key_sequence = "CONTROL + 4", + consuming = "none", + }, + + { + type = "custom-input", + name = "set-quickbar-5", + key_sequence = "CONTROL + 5", + consuming = "none", + }, + + { + type = "custom-input", + name = "set-quickbar-6", + key_sequence = "CONTROL + 6", + consuming = "none", + }, + + { + type = "custom-input", + name = "set-quickbar-7", + key_sequence = "CONTROL + 7", + consuming = "none", + }, + + { + type = "custom-input", + name = "set-quickbar-8", + key_sequence = "CONTROL + 8", + consuming = "none", + }, + + { + type = "custom-input", + name = "set-quickbar-9", + key_sequence = "CONTROL + 9", + consuming = "none", + }, + + { + type = "custom-input", + name = "set-quickbar-10", + key_sequence = "CONTROL + 0", + consuming = "none", + }, + + { + type = "custom-input", + name = "quickbar-page-1", + key_sequence = "SHIFT + 1", + consuming = "none", + }, + + { + type = "custom-input", + name = "quickbar-page-2", + key_sequence = "SHIFT + 2", + consuming = "none", + }, + + { + type = "custom-input", + name = "quickbar-page-3", + key_sequence = "SHIFT + 3", + consuming = "none", + }, + + { + type = "custom-input", + name = "quickbar-page-4", + key_sequence = "SHIFT + 4", + consuming = "none", + }, + + { + type = "custom-input", + name = "quickbar-page-5", + key_sequence = "SHIFT + 5", + consuming = "none", + }, + + { + type = "custom-input", + name = "quickbar-page-6", + key_sequence = "SHIFT + 6", + consuming = "none", + }, + + { + type = "custom-input", + name = "quickbar-page-7", + key_sequence = "SHIFT + 7", + consuming = "none", + }, + + { + type = "custom-input", + name = "quickbar-page-8", + key_sequence = "SHIFT + 8", + consuming = "none", + }, + + { + type = "custom-input", + name = "quickbar-page-9", + key_sequence = "SHIFT + 9", + consuming = "none", + }, + + { + type = "custom-input", + name = "quickbar-page-10", + key_sequence = "SHIFT + 0", + consuming = "none", + }, + + { + type = "custom-input", + name = "switch-menu-or-gun", + key_sequence = "TAB", + consuming = "none", + }, + + { + type = "custom-input", + name = "reverse-switch-menu-or-gun", + key_sequence = "SHIFT + TAB", + consuming = "none", + }, + + { + type = "custom-input", + name = "mine-access-sounds", + key_sequence = "X", + linked_game_control = "mine", + consuming = "none", + }, + + { + type = "custom-input", + name = "mine-tiles", + key_sequence = "X", + linked_game_control = "mine", + consuming = "none", + }, + + { + type = "custom-input", + name = "flush-fluid", + key_sequence = "X", + linked_game_control = "mine", + consuming = "none", + }, + + { + type = "custom-input", + name = "mine-area", + key_sequence = "SHIFT + X", + consuming = "none", + }, + + { + type = "custom-input", + name = "cut-paste-tool-comment", + key_sequence = "CONTROL + X", + consuming = "none", + }, + + { + type = "custom-input", + name = "leftbracket-key-id", + key_sequence = "LEFTBRACKET", + consuming = "none", + }, + + { + type = "custom-input", + name = "rightbracket-key-id", + key_sequence = "RIGHTBRACKET", + consuming = "none", + }, + + { + type = "custom-input", + name = "click-menu", + key_sequence = "LEFTBRACKET", + consuming = "none", + }, + + { + type = "custom-input", + name = "click-menu-right", + key_sequence = "RIGHTBRACKET", + consuming = "none", + }, + + { + type = "custom-input", + name = "click-hand", + key_sequence = "LEFTBRACKET", + consuming = "none", + }, + + { + type = "custom-input", + name = "click-hand-right", + key_sequence = "RIGHTBRACKET", + consuming = "none", + }, + + { + type = "custom-input", + name = "click-entity", + key_sequence = "LEFTBRACKET", + alternative_key_sequence = "mouse-button-1", + consuming = "none", + }, + + { + type = "custom-input", + name = "open-circuit-menu", + key_sequence = "N", + consuming = "none", + }, + + { + type = "custom-input", + name = "repair-area", + key_sequence = "CONTROL + SHIFT + LEFTBRACKET", + consuming = "none", + }, + + { + type = "custom-input", + name = "crafting-all", + key_sequence = "SHIFT + LEFTBRACKET", + consuming = "none", + }, + + { + type = "custom-input", + name = "transfer-one-stack", + key_sequence = "SHIFT + LEFTBRACKET", + consuming = "none", + }, + + { + type = "custom-input", + name = "equip-item", + key_sequence = "SHIFT + LEFTBRACKET", + consuming = "none", + }, + + { + type = "custom-input", + name = "open-rail-builder", + key_sequence = "SHIFT + LEFTBRACKET", + consuming = "none", + }, + + { + type = "custom-input", + name = "quick-build-rail-left-turn", + key_sequence = "CONTROL + LEFT", + consuming = "none", + }, + + { + type = "custom-input", + name = "quick-build-rail-right-turn", + key_sequence = "CONTROL + RIGHT", + consuming = "none", + }, + + { + type = "custom-input", + name = "transfer-all-stacks", + key_sequence = "CONTROL + LEFTBRACKET", + consuming = "none", + }, + + { + type = "custom-input", + name = "fa-alternate-build", + key_sequence = "CONTROL + LEFTBRACKET", + consuming = "none", + }, + + { + type = "custom-input", + name = "transfer-half-of-all-stacks", + key_sequence = "CONTROL + RIGHTBRACKET", + consuming = "none", + }, + + { + type = "custom-input", + name = "crafting-5", + key_sequence = "RIGHTBRACKET", + consuming = "none", + }, + + { + type = "custom-input", + name = "menu-clear-filter", + key_sequence = "RIGHTBRACKET", + consuming = "none", + }, + + { + type = "custom-input", + name = "read-entity-status", + key_sequence = "RIGHTBRACKET", + consuming = "none", + }, + + { + type = "custom-input", + name = "rotate-building", + key_sequence = "R", + linked_game_control = "rotate", + consuming = "none", + }, + + { + type = "custom-input", + name = "reverse-rotate-building", + key_sequence = "SHIFT + R", + linked_game_control = "reverse-rotate", + consuming = "none", + }, + + { + type = "custom-input", + name = "flip-blueprint-horizontal-info", + key_sequence = "F", + linked_game_control = "flip-blueprint-horizontal", + consuming = "none", + }, + + { + type = "custom-input", + name = "flip-blueprint-vertical-info", + key_sequence = "G", + linked_game_control = "flip-blueprint-vertical", + consuming = "none", + }, + + { + type = "custom-input", + name = "inventory-read-weapons-data", + key_sequence = "R", + consuming = "none", + }, + + { + type = "custom-input", + name = "inventory-reload-weapons", + key_sequence = "SHIFT + R", + consuming = "none", + }, + + { + type = "custom-input", + name = "inventory-remove-all-weapons-and-ammo", + key_sequence = "CONTROL + SHIFT + R", + consuming = "none", + }, + + { + type = "custom-input", + name = "item-info", + key_sequence = "Y", + consuming = "none", + }, + + { + type = "custom-input", + name = "item-info-last-indexed", + key_sequence = "SHIFT + Y", + consuming = "none", + }, + + { + type = "custom-input", + name = "item-production-info", + key_sequence = "U", + consuming = "none", + }, + + { + type = "custom-input", + name = "cursor-pollution-info", + key_sequence = "SHIFT + U", + consuming = "none", + }, + + { + type = "custom-input", + name = "read-time-and-research-progress", + key_sequence = "T", + consuming = "none", + }, + + { + type = "custom-input", + name = "save-game-manually", + key_sequence = "F1", + consuming = "none", + }, + + { + type = "custom-input", + name = "toggle-walk", + key_sequence = "CONTROL + W", + consuming = "none", + }, + + { + type = "custom-input", + name = "toggle-build-lock", + key_sequence = "CONTROL + B", + consuming = "none", + }, + + { + type = "custom-input", + name = "toggle-vanilla-mode", + key_sequence = "CONTROL + ALT + V", + consuming = "none", + }, + + { + type = "custom-input", + name = "toggle-cursor-hiding", + key_sequence = "CONTROL + ALT + C", + consuming = "none", + }, + + { + type = "custom-input", + name = "clear-renders", + key_sequence = "CONTROL + ALT + R", + consuming = "none", + }, + + { + type = "custom-input", + name = "recalibrate-zoom", + key_sequence = "CONTROL + END", + alternative_key_sequence = "CONTROL + RCTRL", + consuming = "none", + }, + + { + type = "custom-input", + name = "enable-mouse-update-entity-selection", + key_sequence = "mouse-button-3", + consuming = "none", + }, + + { + type = "custom-input", + name = "pipette-tool-info", + key_sequence = "Q", + linked_game_control = "smart-pipette", + consuming = "none", + }, + + { + type = "custom-input", + name = "copy-entity-settings-info", + key_sequence = "SHIFT + RIGHTBRACKET", + linked_game_control = "copy-entity-settings", + consuming = "none", + }, + + { + type = "custom-input", + name = "paste-entity-settings-info", + key_sequence = "SHIFT + LEFTBRACKET", + linked_game_control = "paste-entity-settings", + consuming = "none", + }, + + { + type = "custom-input", + name = "fast-entity-transfer-info", + key_sequence = "CONTROL + LEFTBRACKET", + linked_game_control = "fast-entity-transfer", + consuming = "none", + }, + + { + type = "custom-input", + name = "fast-entity-split-info", + key_sequence = "CONTROL + RIGHTBRACKET", + linked_game_control = "fast-entity-split", + consuming = "none", + }, + + { + type = "custom-input", + name = "drop-cursor-info", + key_sequence = "Z", + linked_game_control = "drop-cursor", + consuming = "none", + }, + + { + type = "custom-input", + name = "read-hand", + key_sequence = "SHIFT + Q", + consuming = "none", + }, + + { + type = "custom-input", + name = "locate-hand-in-inventory", + key_sequence = "CONTROL + Q", + consuming = "none", + }, + + { + type = "custom-input", + name = "locate-hand-in-crafting-menu", + key_sequence = "CONTROL + SHIFT + Q", + consuming = "none", + }, + + { + type = "custom-input", + name = "menu-search-open", + key_sequence = "CONTROL + F", + linked_game_control = "focus-search", + consuming = "game-only", + }, + + { + type = "custom-input", + name = "menu-search-get-next", + key_sequence = "SHIFT + ENTER", + consuming = "none", + }, + + { + type = "custom-input", + name = "menu-search-get-last", + key_sequence = "CONTROL + ENTER", + consuming = "none", + }, + + { + type = "custom-input", + name = "open-warnings-menu", + key_sequence = "P", + consuming = "none", + }, + + { + type = "custom-input", + name = "nearest-damaged-ent-info", + key_sequence = "SHIFT + P", + consuming = "none", + }, + + { + type = "custom-input", + name = "open-fast-travel-menu", + key_sequence = "V", + consuming = "none", + }, + + { + type = "custom-input", + name = "open-structure-travel-menu", + key_sequence = "CONTROL + S", + consuming = "none", + }, + + { + type = "custom-input", + name = "alternative-menu-up", + key_sequence = "UP", + consuming = "none", + }, + + { + type = "custom-input", + name = "alternative-menu-down", + key_sequence = "DOWN", + consuming = "none", + }, + + { + type = "custom-input", + name = "alternative-menu-left", + key_sequence = "LEFT", + consuming = "none", + }, + + { + type = "custom-input", + name = "alternative-menu-right", + key_sequence = "RIGHT", + consuming = "none", + }, + + { + type = "custom-input", + name = "cursor-one-tile-north", + key_sequence = "UP", + consuming = "none", + }, + + { + type = "custom-input", + name = "cursor-one-tile-south", + key_sequence = "DOWN", + consuming = "none", + }, + + { + type = "custom-input", + name = "cursor-one-tile-east", + key_sequence = "RIGHT", + consuming = "none", + }, + + { + type = "custom-input", + name = "cursor-one-tile-west", + key_sequence = "LEFT", + consuming = "none", + }, + + { + type = "custom-input", + name = "set-splitter-input-priority-left", + key_sequence = "SHIFT + LEFT", + consuming = "none", + }, + + { + type = "custom-input", + name = "set-splitter-input-priority-right", + key_sequence = "SHIFT + RIGHT", + consuming = "none", + }, + + { + type = "custom-input", + name = "set-splitter-output-priority-left", + key_sequence = "CONTROL + LEFT", + consuming = "none", + }, + + { + type = "custom-input", + name = "set-splitter-output-priority-right", + key_sequence = "CONTROL + RIGHT", + consuming = "none", + }, + + { + type = "custom-input", + name = "set-entity-filter-from-hand", + key_sequence = "CONTROL + LEFTBRACKET", + consuming = "none", + }, + + { + type = "custom-input", + name = "connect-rail-vehicles", + key_sequence = "G", + consuming = "none", + }, + + { + type = "custom-input", + name = "disconnect-rail-vehicles", + key_sequence = "SHIFT + G", + consuming = "none", + }, + + { + type = "custom-input", + name = "inventory-read-armor-stats", + key_sequence = "G", + consuming = "none", + }, + + { + type = "custom-input", + name = "inventory-read-equipment-list", + key_sequence = "SHIFT + G", + consuming = "none", + }, + + { + type = "custom-input", + name = "inventory-remove-all-equipment-and-armor", + key_sequence = "CONTROL + SHIFT + G", + consuming = "none", + }, + + { + type = "custom-input", + name = "shoot-weapon-fa", + key_sequence = "SPACE", + --linked_game_control = "shoot-enemy", + consuming = "none", + }, + + { + type = "custom-input", + name = "honk", + key_sequence = "V", + consuming = "none", + }, + + { + type = "custom-input", + name = "launch-rocket", + key_sequence = "SPACE", + consuming = "none", + }, + + { + type = "custom-input", + name = "help-read", + key_sequence = "H", + consuming = "none", + }, + + { + type = "custom-input", + name = "help-next", + key_sequence = "CONTROL + H", + consuming = "none", + }, + + { + type = "custom-input", + name = "help-back", + key_sequence = "SHIFT + H", + consuming = "none", + }, + + { + type = "custom-input", + name = "help-chapter-next", + key_sequence = "CONTROL + ALT + H", + consuming = "none", + }, + + { + type = "custom-input", + name = "help-chapter-back", + key_sequence = "SHIFT + ALT + H", + consuming = "none", + }, + + { + type = "custom-input", + name = "help-toggle-header-mode", + key_sequence = "CONTROL + SHIFT + H", + consuming = "none", + }, + + { + type = "custom-input", + name = "help-get-other", + key_sequence = "ALT + H", + consuming = "none", + }, + + { + type = "custom-input", + name = "debug-test-key", + key_sequence = "ALT + G", + consuming = "none", + }, + + { + type = "custom-input", + name = "fa-alt-zoom-in", + key_sequence = "X", + linked_game_control = "alt-zoom-in", + consuming = "none", + }, + + { + type = "custom-input", + name = "fa-alt-zoom-out", + key_sequence = "X", + linked_game_control = "alt-zoom-out", + consuming = "none", + }, + + { + type = "custom-input", + name = "fa-zoom-out", + key_sequence = "X", + linked_game_control = "zoom-out", + consuming = "none", + }, + + { + type = "custom-input", + name = "fa-zoom-in", + key_sequence = "X", + linked_game_control = "zoom-in", + consuming = "none", + }, + + { + type = "custom-input", + name = "fa-debug-reset-zoom-2x", + key_sequence = "X", + linked_game_control = "debug-reset-zoom-2x", + consuming = "none", + }, + + { + type = "custom-input", + name = "fa-debug-reset-zoom", + key_sequence = "X", + linked_game_control = "debug-reset-zoom", + consuming = "none", + }, + + { + type = "custom-input", + name = "logistic-request-read", + key_sequence = "L", + consuming = "none", + }, + + { + type = "custom-input", + name = "logistic-request-increment-min", + key_sequence = "SHIFT + L", + consuming = "none", + }, + + { + type = "custom-input", + name = "logistic-request-decrement-min", + key_sequence = "CONTROL + L", + consuming = "none", + }, + + { + type = "custom-input", + name = "logistic-request-increment-max", + key_sequence = "SHIFT + ALT + L", + consuming = "none", + }, + + { + type = "custom-input", + name = "logistic-request-decrement-max", + key_sequence = "CONTROL + ALT + L", + consuming = "none", + }, + + { + type = "custom-input", + name = "logistic-request-toggle-personal-logistics", + key_sequence = "CONTROL + SHIFT + L", + consuming = "none", + }, + + { + type = "custom-input", + name = "send-selected-stack-to-logistic-trash", + key_sequence = "O", + consuming = "none", + }, + + { + type = "custom-input", + name = "fa-pda-driving-assistant-info", + key_sequence = "L", + consuming = "none", + }, + + { + type = "custom-input", + name = "fa-pda-cruise-control-info", + key_sequence = "O", + consuming = "none", + }, + + { + type = "custom-input", + name = "fa-pda-cruise-control-set-speed-info", + key_sequence = "CONTROL + O", + consuming = "none", + }, + + { + type = "custom-input", + name = "access-config-version1-DO-NOT-EDIT", + key_sequence = "A", + consuming = "none", + }, + + { + type = "custom-input", + name = "access-config-version2-DO-NOT-EDIT", + key_sequence = "A", + consuming = "none", + }, +}) diff --git a/scenarios/fa-demo-map-0-compass-valley-blank/freeplay.lua b/scenarios/fa-demo-map-0-compass-valley-blank/freeplay.lua index 3e04f3fe..9eff3500 100644 --- a/scenarios/fa-demo-map-0-compass-valley-blank/freeplay.lua +++ b/scenarios/fa-demo-map-0-compass-valley-blank/freeplay.lua @@ -2,244 +2,228 @@ local util = require("util") local crash_site = require("crash-site") local created_items = function() - return - { - ["iron-plate"] = 8, - ["wood"] = 1, - ["pistol"] = 1, - ["firearm-magazine"] = 10, - ["burner-mining-drill"] = 1, - ["stone-furnace"] = 1 - } + return { + ["iron-plate"] = 8, + ["wood"] = 1, + ["pistol"] = 1, + ["firearm-magazine"] = 10, + ["burner-mining-drill"] = 1, + ["stone-furnace"] = 1, + } end local respawn_items = function() - return - { - ["pistol"] = 1, - ["firearm-magazine"] = 10 - } + return { + ["pistol"] = 1, + ["firearm-magazine"] = 10, + } end local ship_items = function() - return - { - ["firearm-magazine"] = 8 - } + return { + ["firearm-magazine"] = 8, + } end local debris_items = function() - return - { - ["iron-plate"] = 8 - } + return { + ["iron-plate"] = 8, + } end local ship_parts = function() - return crash_site.default_ship_parts() + return crash_site.default_ship_parts() end local chart_starting_area = function() - local r = global.chart_distance or 200 - local force = game.forces.player - local surface = game.surfaces[1] - local origin = force.get_spawn_position(surface) - force.chart(surface, {{origin.x - r, origin.y - r}, {origin.x + r, origin.y + r}}) + local r = global.chart_distance or 200 + local force = game.forces.player + local surface = game.surfaces[1] + local origin = force.get_spawn_position(surface) + force.chart(surface, { { origin.x - r, origin.y - r }, { origin.x + r, origin.y + r } }) end - local on_player_created = function(event) - local player = game.get_player(event.player_index) - util.insert_safe(player, global.created_items) - - if not global.init_ran then - - --This is so that other mods and scripts have a chance to do remote calls before we do things like charting the starting area, creating the crash site, etc. - global.init_ran = true - - chart_starting_area() - - if not global.disable_crashsite then - local surface = player.surface - surface.daytime = 0.7 - crash_site.create_crash_site(surface, {-5,-6}, util.copy(global.crashed_ship_items), util.copy(global.crashed_debris_items), util.copy(global.crashed_ship_parts)) - util.remove_safe(player, global.crashed_ship_items) - util.remove_safe(player, global.crashed_debris_items) - player.get_main_inventory().sort_and_merge() - if player.character then - player.character.destructible = false + local player = game.get_player(event.player_index) + util.insert_safe(player, global.created_items) + + if not global.init_ran then + --This is so that other mods and scripts have a chance to do remote calls before we do things like charting the starting area, creating the crash site, etc. + global.init_ran = true + + chart_starting_area() + + if not global.disable_crashsite then + local surface = player.surface + surface.daytime = 0.7 + crash_site.create_crash_site( + surface, + { -5, -6 }, + util.copy(global.crashed_ship_items), + util.copy(global.crashed_debris_items), + util.copy(global.crashed_ship_parts) + ) + util.remove_safe(player, global.crashed_ship_items) + util.remove_safe(player, global.crashed_debris_items) + player.get_main_inventory().sort_and_merge() + if player.character then player.character.destructible = false end + global.crash_site_cutscene_active = true + crash_site.create_cutscene(player, { -5, -4 }) + return end - global.crash_site_cutscene_active = true - crash_site.create_cutscene(player, {-5, -4}) - return - end - - end - - if not global.skip_intro then - if game.is_multiplayer() then - player.print(global.custom_intro_message or {"msg-intro"}) - else - game.show_message_dialog{text = global.custom_intro_message or {"msg-intro"}} - end - end + end + if not global.skip_intro then + if game.is_multiplayer() then + player.print(global.custom_intro_message or { "msg-intro" }) + else + game.show_message_dialog({ text = global.custom_intro_message or { "msg-intro" } }) + end + end end local on_player_respawned = function(event) - local player = game.get_player(event.player_index) - util.insert_safe(player, global.respawn_items) + local player = game.get_player(event.player_index) + util.insert_safe(player, global.respawn_items) end local on_cutscene_waypoint_reached = function(event) - if not global.crash_site_cutscene_active then return end - if not crash_site.is_crash_site_cutscene(event) then return end + if not global.crash_site_cutscene_active then return end + if not crash_site.is_crash_site_cutscene(event) then return end - local player = game.get_player(event.player_index) + local player = game.get_player(event.player_index) - player.exit_cutscene() + player.exit_cutscene() - if not global.skip_intro then - if game.is_multiplayer() then - player.print(global.custom_intro_message or {"msg-intro"}) - else - game.show_message_dialog{text = global.custom_intro_message or {"msg-intro"}} - end - end + if not global.skip_intro then + if game.is_multiplayer() then + player.print(global.custom_intro_message or { "msg-intro" }) + else + game.show_message_dialog({ text = global.custom_intro_message or { "msg-intro" } }) + end + end end local skip_crash_site_cutscene = function(event) - if not global.crash_site_cutscene_active then return end - if event.player_index ~= 1 then return end - local player = game.get_player(event.player_index) - if player.controller_type == defines.controllers.cutscene then - player.exit_cutscene() - end + if not global.crash_site_cutscene_active then return end + if event.player_index ~= 1 then return end + local player = game.get_player(event.player_index) + if player.controller_type == defines.controllers.cutscene then player.exit_cutscene() end end local on_cutscene_cancelled = function(event) - if not global.crash_site_cutscene_active then return end - if event.player_index ~= 1 then return end - global.crash_site_cutscene_active = nil - local player = game.get_player(event.player_index) - if player.gui.screen.skip_cutscene_label then - player.gui.screen.skip_cutscene_label.destroy() - end - if player.character then - player.character.destructible = true - end - player.zoom = 1.5 + if not global.crash_site_cutscene_active then return end + if event.player_index ~= 1 then return end + global.crash_site_cutscene_active = nil + local player = game.get_player(event.player_index) + if player.gui.screen.skip_cutscene_label then player.gui.screen.skip_cutscene_label.destroy() end + if player.character then player.character.destructible = true end + player.zoom = 1.5 end local on_player_display_refresh = function(event) - crash_site.on_player_display_refresh(event) -end - -local freeplay_interface = -{ - get_created_items = function() - return global.created_items - end, - set_created_items = function(map) - global.created_items = map or error("Remote call parameter to freeplay set created items can't be nil.") - end, - get_respawn_items = function() - return global.respawn_items - end, - set_respawn_items = function(map) - global.respawn_items = map or error("Remote call parameter to freeplay set respawn items can't be nil.") - end, - set_skip_intro = function(bool) - global.skip_intro = bool - end, - get_skip_intro = function() - return global.skip_intro - end, - set_custom_intro_message = function(message) - global.custom_intro_message = message - end, - get_custom_intro_message = function() - return global.custom_intro_message - end, - set_chart_distance = function(value) - global.chart_distance = tonumber(value) or error("Remote call parameter to freeplay set chart distance must be a number") - end, - get_disable_crashsite = function() - return global.disable_crashsite - end, - set_disable_crashsite = function(bool) - global.disable_crashsite = bool - end, - get_init_ran = function() - return global.init_ran - end, - get_ship_items = function() - return global.crashed_ship_items - end, - set_ship_items = function(map) - global.crashed_ship_items = map or error("Remote call parameter to freeplay set created items can't be nil.") - end, - get_debris_items = function() - return global.crashed_debris_items - end, - set_debris_items = function(map) - global.crashed_debris_items = map or error("Remote call parameter to freeplay set respawn items can't be nil.") - end, - get_ship_parts = function() - return global.crashed_ship_parts - end, - set_ship_parts = function(parts) - global.crashed_ship_parts = parts or error("Remote call parameter to freeplay set ship parts can't be nil.") - end + crash_site.on_player_display_refresh(event) +end + +local freeplay_interface = { + get_created_items = function() + return global.created_items + end, + set_created_items = function(map) + global.created_items = map or error("Remote call parameter to freeplay set created items can't be nil.") + end, + get_respawn_items = function() + return global.respawn_items + end, + set_respawn_items = function(map) + global.respawn_items = map or error("Remote call parameter to freeplay set respawn items can't be nil.") + end, + set_skip_intro = function(bool) + global.skip_intro = bool + end, + get_skip_intro = function() + return global.skip_intro + end, + set_custom_intro_message = function(message) + global.custom_intro_message = message + end, + get_custom_intro_message = function() + return global.custom_intro_message + end, + set_chart_distance = function(value) + global.chart_distance = tonumber(value) + or error("Remote call parameter to freeplay set chart distance must be a number") + end, + get_disable_crashsite = function() + return global.disable_crashsite + end, + set_disable_crashsite = function(bool) + global.disable_crashsite = bool + end, + get_init_ran = function() + return global.init_ran + end, + get_ship_items = function() + return global.crashed_ship_items + end, + set_ship_items = function(map) + global.crashed_ship_items = map or error("Remote call parameter to freeplay set created items can't be nil.") + end, + get_debris_items = function() + return global.crashed_debris_items + end, + set_debris_items = function(map) + global.crashed_debris_items = map or error("Remote call parameter to freeplay set respawn items can't be nil.") + end, + get_ship_parts = function() + return global.crashed_ship_parts + end, + set_ship_parts = function(parts) + global.crashed_ship_parts = parts or error("Remote call parameter to freeplay set ship parts can't be nil.") + end, } -if not remote.interfaces["freeplay"] then - remote.add_interface("freeplay", freeplay_interface) -end +if not remote.interfaces["freeplay"] then remote.add_interface("freeplay", freeplay_interface) end local is_debug = function() - local surface = game.surfaces.nauvis - local map_gen_settings = surface.map_gen_settings - return map_gen_settings.width == 50 and map_gen_settings.height == 50 + local surface = game.surfaces.nauvis + local map_gen_settings = surface.map_gen_settings + return map_gen_settings.width == 50 and map_gen_settings.height == 50 end local freeplay = {} -freeplay.events = -{ - [defines.events.on_player_created] = on_player_created, - [defines.events.on_player_respawned] = on_player_respawned, - [defines.events.on_cutscene_waypoint_reached] = on_cutscene_waypoint_reached, - ["crash-site-skip-cutscene"] = skip_crash_site_cutscene, - [defines.events.on_player_display_resolution_changed] = on_player_display_refresh, - [defines.events.on_player_display_scale_changed] = on_player_display_refresh, - [defines.events.on_cutscene_cancelled] = on_cutscene_cancelled +freeplay.events = { + [defines.events.on_player_created] = on_player_created, + [defines.events.on_player_respawned] = on_player_respawned, + [defines.events.on_cutscene_waypoint_reached] = on_cutscene_waypoint_reached, + ["crash-site-skip-cutscene"] = skip_crash_site_cutscene, + [defines.events.on_player_display_resolution_changed] = on_player_display_refresh, + [defines.events.on_player_display_scale_changed] = on_player_display_refresh, + [defines.events.on_cutscene_cancelled] = on_cutscene_cancelled, } freeplay.on_configuration_changed = function() - global.created_items = global.created_items or created_items() - global.respawn_items = global.respawn_items or respawn_items() - global.crashed_ship_items = global.crashed_ship_items or ship_items() - global.crashed_debris_items = global.crashed_debris_items or debris_items() - global.crashed_ship_parts = global.crashed_ship_parts or ship_parts() - - if not global.init_ran then - -- migrating old saves. - global.init_ran = #game.players > 0 - end + global.created_items = global.created_items or created_items() + global.respawn_items = global.respawn_items or respawn_items() + global.crashed_ship_items = global.crashed_ship_items or ship_items() + global.crashed_debris_items = global.crashed_debris_items or debris_items() + global.crashed_ship_parts = global.crashed_ship_parts or ship_parts() + + if not global.init_ran then + -- migrating old saves. + global.init_ran = #game.players > 0 + end end freeplay.on_init = function() - global.created_items = created_items() - global.respawn_items = respawn_items() - global.crashed_ship_items = ship_items() - global.crashed_debris_items = debris_items() - global.crashed_ship_parts = ship_parts() - global.skip_intro = true - if is_debug() then - global.disable_crashsite = true - end - + global.created_items = created_items() + global.respawn_items = respawn_items() + global.crashed_ship_items = ship_items() + global.crashed_debris_items = debris_items() + global.crashed_ship_parts = ship_parts() + global.skip_intro = true + if is_debug() then global.disable_crashsite = true end end return freeplay diff --git a/scenarios/fa-sandbox-world-1/freeplay.lua b/scenarios/fa-sandbox-world-1/freeplay.lua index 953fa585..99d30bc8 100644 --- a/scenarios/fa-sandbox-world-1/freeplay.lua +++ b/scenarios/fa-sandbox-world-1/freeplay.lua @@ -2,245 +2,231 @@ local util = require("util") local crash_site = require("crash-site") local created_items = function() - return - { - ["iron-plate"] = 8, - ["wood"] = 1, - ["pistol"] = 1, - ["firearm-magazine"] = 10, - ["burner-mining-drill"] = 1, - ["stone-furnace"] = 1 - } + return { + ["iron-plate"] = 8, + ["wood"] = 1, + ["pistol"] = 1, + ["firearm-magazine"] = 10, + ["burner-mining-drill"] = 1, + ["stone-furnace"] = 1, + } end local respawn_items = function() - return - { - ["pistol"] = 1, - ["firearm-magazine"] = 10 - } + return { + ["pistol"] = 1, + ["firearm-magazine"] = 10, + } end local ship_items = function() - return - { - ["firearm-magazine"] = 8 - } + return { + ["firearm-magazine"] = 8, + } end local debris_items = function() - return - { - ["iron-plate"] = 8 - } + return { + ["iron-plate"] = 8, + } end local ship_parts = function() - return crash_site.default_ship_parts() + return crash_site.default_ship_parts() end local chart_starting_area = function() - local r = global.chart_distance or 200 - local force = game.forces.player - local surface = game.surfaces[1] - local origin = force.get_spawn_position(surface) - force.chart(surface, {{origin.x - r, origin.y - r}, {origin.x + r, origin.y + r}}) + local r = global.chart_distance or 200 + local force = game.forces.player + local surface = game.surfaces[1] + local origin = force.get_spawn_position(surface) + force.chart(surface, { { origin.x - r, origin.y - r }, { origin.x + r, origin.y + r } }) end - local on_player_created = function(event) - local player = game.get_player(event.player_index) - util.insert_safe(player, global.created_items) - - if not global.init_ran then - - --This is so that other mods and scripts have a chance to do remote calls before we do things like charting the starting area, creating the crash site, etc. - global.init_ran = true - - chart_starting_area() - - if not global.disable_crashsite then - local surface = player.surface - surface.daytime = 0.7 - crash_site.create_crash_site(surface, {-5,-6}, util.copy(global.crashed_ship_items), util.copy(global.crashed_debris_items), util.copy(global.crashed_ship_parts)) - util.remove_safe(player, global.crashed_ship_items) - util.remove_safe(player, global.crashed_debris_items) - player.get_main_inventory().sort_and_merge() - if player.character then - player.character.destructible = false + local player = game.get_player(event.player_index) + util.insert_safe(player, global.created_items) + + if not global.init_ran then + --This is so that other mods and scripts have a chance to do remote calls before we do things like charting the starting area, creating the crash site, etc. + global.init_ran = true + + chart_starting_area() + + if not global.disable_crashsite then + local surface = player.surface + surface.daytime = 0.7 + crash_site.create_crash_site( + surface, + { -5, -6 }, + util.copy(global.crashed_ship_items), + util.copy(global.crashed_debris_items), + util.copy(global.crashed_ship_parts) + ) + util.remove_safe(player, global.crashed_ship_items) + util.remove_safe(player, global.crashed_debris_items) + player.get_main_inventory().sort_and_merge() + if player.character then player.character.destructible = false end + global.crash_site_cutscene_active = true + crash_site.create_cutscene(player, { -5, -4 }) + return end - global.crash_site_cutscene_active = true - crash_site.create_cutscene(player, {-5, -4}) - return - end - - end - - if not global.skip_intro then - if game.is_multiplayer() then - player.print(global.custom_intro_message or {"msg-intro"}) - else - game.show_message_dialog{text = global.custom_intro_message or {"msg-intro"}} - end - end + end + if not global.skip_intro then + if game.is_multiplayer() then + player.print(global.custom_intro_message or { "msg-intro" }) + else + game.show_message_dialog({ text = global.custom_intro_message or { "msg-intro" } }) + end + end end local on_player_respawned = function(event) - local player = game.get_player(event.player_index) - util.insert_safe(player, global.respawn_items) + local player = game.get_player(event.player_index) + util.insert_safe(player, global.respawn_items) end local on_cutscene_waypoint_reached = function(event) - if not global.crash_site_cutscene_active then return end - if not crash_site.is_crash_site_cutscene(event) then return end + if not global.crash_site_cutscene_active then return end + if not crash_site.is_crash_site_cutscene(event) then return end - local player = game.get_player(event.player_index) + local player = game.get_player(event.player_index) - player.exit_cutscene() + player.exit_cutscene() - if not global.skip_intro then - if game.is_multiplayer() then + if not global.skip_intro then + if game.is_multiplayer() then --player.print(global.custom_intro_message or {"msg-intro"}) - else - --game.show_message_dialog{text = global.custom_intro_message or {"msg-intro"}} - end - end + else + --game.show_message_dialog{text = global.custom_intro_message or {"msg-intro"}} + end + end end local skip_crash_site_cutscene = function(event) - if not global.crash_site_cutscene_active then return end - if event.player_index ~= 1 then return end - local player = game.get_player(event.player_index) - if player.controller_type == defines.controllers.cutscene then - player.exit_cutscene() - end + if not global.crash_site_cutscene_active then return end + if event.player_index ~= 1 then return end + local player = game.get_player(event.player_index) + if player.controller_type == defines.controllers.cutscene then player.exit_cutscene() end end local on_cutscene_cancelled = function(event) - if not global.crash_site_cutscene_active then return end - if event.player_index ~= 1 then return end - global.crash_site_cutscene_active = nil - local player = game.get_player(event.player_index) - if player.gui.screen.skip_cutscene_label then - player.gui.screen.skip_cutscene_label.destroy() - end - if player.character then - player.character.destructible = true - end - player.zoom = 1.5 + if not global.crash_site_cutscene_active then return end + if event.player_index ~= 1 then return end + global.crash_site_cutscene_active = nil + local player = game.get_player(event.player_index) + if player.gui.screen.skip_cutscene_label then player.gui.screen.skip_cutscene_label.destroy() end + if player.character then player.character.destructible = true end + player.zoom = 1.5 end local on_player_display_refresh = function(event) - crash_site.on_player_display_refresh(event) -end - -local freeplay_interface = -{ - get_created_items = function() - return global.created_items - end, - set_created_items = function(map) - global.created_items = map or error("Remote call parameter to freeplay set created items can't be nil.") - end, - get_respawn_items = function() - return global.respawn_items - end, - set_respawn_items = function(map) - global.respawn_items = map or error("Remote call parameter to freeplay set respawn items can't be nil.") - end, - set_skip_intro = function(bool) - global.skip_intro = bool - end, - get_skip_intro = function() - return global.skip_intro - end, - set_custom_intro_message = function(message) - global.custom_intro_message = message - end, - get_custom_intro_message = function() - return global.custom_intro_message - end, - set_chart_distance = function(value) - global.chart_distance = tonumber(value) or error("Remote call parameter to freeplay set chart distance must be a number") - end, - get_disable_crashsite = function() - return global.disable_crashsite - end, - set_disable_crashsite = function(bool) - global.disable_crashsite = bool - end, - get_init_ran = function() - return global.init_ran - end, - get_ship_items = function() - return global.crashed_ship_items - end, - set_ship_items = function(map) - global.crashed_ship_items = map or error("Remote call parameter to freeplay set created items can't be nil.") - end, - get_debris_items = function() - return global.crashed_debris_items - end, - set_debris_items = function(map) - global.crashed_debris_items = map or error("Remote call parameter to freeplay set respawn items can't be nil.") - end, - get_ship_parts = function() - return global.crashed_ship_parts - end, - set_ship_parts = function(parts) - global.crashed_ship_parts = parts or error("Remote call parameter to freeplay set ship parts can't be nil.") - end + crash_site.on_player_display_refresh(event) +end + +local freeplay_interface = { + get_created_items = function() + return global.created_items + end, + set_created_items = function(map) + global.created_items = map or error("Remote call parameter to freeplay set created items can't be nil.") + end, + get_respawn_items = function() + return global.respawn_items + end, + set_respawn_items = function(map) + global.respawn_items = map or error("Remote call parameter to freeplay set respawn items can't be nil.") + end, + set_skip_intro = function(bool) + global.skip_intro = bool + end, + get_skip_intro = function() + return global.skip_intro + end, + set_custom_intro_message = function(message) + global.custom_intro_message = message + end, + get_custom_intro_message = function() + return global.custom_intro_message + end, + set_chart_distance = function(value) + global.chart_distance = tonumber(value) + or error("Remote call parameter to freeplay set chart distance must be a number") + end, + get_disable_crashsite = function() + return global.disable_crashsite + end, + set_disable_crashsite = function(bool) + global.disable_crashsite = bool + end, + get_init_ran = function() + return global.init_ran + end, + get_ship_items = function() + return global.crashed_ship_items + end, + set_ship_items = function(map) + global.crashed_ship_items = map or error("Remote call parameter to freeplay set created items can't be nil.") + end, + get_debris_items = function() + return global.crashed_debris_items + end, + set_debris_items = function(map) + global.crashed_debris_items = map or error("Remote call parameter to freeplay set respawn items can't be nil.") + end, + get_ship_parts = function() + return global.crashed_ship_parts + end, + set_ship_parts = function(parts) + global.crashed_ship_parts = parts or error("Remote call parameter to freeplay set ship parts can't be nil.") + end, } -if not remote.interfaces["freeplay"] then - remote.add_interface("freeplay", freeplay_interface) -end +if not remote.interfaces["freeplay"] then remote.add_interface("freeplay", freeplay_interface) end local is_debug = function() - local surface = game.surfaces.nauvis - local map_gen_settings = surface.map_gen_settings - return map_gen_settings.width == 50 and map_gen_settings.height == 50 + local surface = game.surfaces.nauvis + local map_gen_settings = surface.map_gen_settings + return map_gen_settings.width == 50 and map_gen_settings.height == 50 end local freeplay = {} -freeplay.events = -{ - [defines.events.on_player_created] = on_player_created, - [defines.events.on_player_respawned] = on_player_respawned, - [defines.events.on_cutscene_waypoint_reached] = on_cutscene_waypoint_reached, - ["crash-site-skip-cutscene"] = skip_crash_site_cutscene, - [defines.events.on_player_display_resolution_changed] = on_player_display_refresh, - [defines.events.on_player_display_scale_changed] = on_player_display_refresh, - [defines.events.on_cutscene_cancelled] = on_cutscene_cancelled +freeplay.events = { + [defines.events.on_player_created] = on_player_created, + [defines.events.on_player_respawned] = on_player_respawned, + [defines.events.on_cutscene_waypoint_reached] = on_cutscene_waypoint_reached, + ["crash-site-skip-cutscene"] = skip_crash_site_cutscene, + [defines.events.on_player_display_resolution_changed] = on_player_display_refresh, + [defines.events.on_player_display_scale_changed] = on_player_display_refresh, + [defines.events.on_cutscene_cancelled] = on_cutscene_cancelled, } freeplay.on_configuration_changed = function() - global.created_items = global.created_items or created_items() - global.respawn_items = global.respawn_items or respawn_items() - global.crashed_ship_items = global.crashed_ship_items or ship_items() - global.crashed_debris_items = global.crashed_debris_items or debris_items() - global.crashed_ship_parts = global.crashed_ship_parts or ship_parts() - - if not global.init_ran then - -- migrating old saves. - global.init_ran = #game.players > 0 - end + global.created_items = global.created_items or created_items() + global.respawn_items = global.respawn_items or respawn_items() + global.crashed_ship_items = global.crashed_ship_items or ship_items() + global.crashed_debris_items = global.crashed_debris_items or debris_items() + global.crashed_ship_parts = global.crashed_ship_parts or ship_parts() + + if not global.init_ran then + -- migrating old saves. + global.init_ran = #game.players > 0 + end end freeplay.on_init = function() - global.created_items = created_items() - global.respawn_items = respawn_items() - global.crashed_ship_items = ship_items() - global.crashed_debris_items = debris_items() - global.crashed_ship_parts = ship_parts() - - if is_debug() then - global.skip_intro = true - global.disable_crashsite = true - end - + global.created_items = created_items() + global.respawn_items = respawn_items() + global.crashed_ship_items = ship_items() + global.crashed_debris_items = debris_items() + global.crashed_ship_parts = ship_parts() + + if is_debug() then + global.skip_intro = true + global.disable_crashsite = true + end end return freeplay diff --git a/scripts/blueprints.lua b/scripts/blueprints.lua index 3d3146f7..d6dae65e 100644 --- a/scripts/blueprints.lua +++ b/scripts/blueprints.lua @@ -12,40 +12,36 @@ local mod = {} function mod.get_bp_data_for_edit(stack) ---@diagnostic disable-next-line: param-type-mismatch - return game.json_to_table(game.decode_string(string.sub(stack.export_stack(),2))) + return game.json_to_table(game.decode_string(string.sub(stack.export_stack(), 2))) end -function mod.set_stack_bp_from_data(stack,bp_data) - stack.import_stack("0"..game.encode_string(game.table_to_json(bp_data))) +function mod.set_stack_bp_from_data(stack, bp_data) + stack.import_stack("0" .. game.encode_string(game.table_to_json(bp_data))) end -function mod.set_blueprint_description(stack,description) +function mod.set_blueprint_description(stack, description) local bp_data = mod.get_bp_data_for_edit(stack) bp_data.blueprint.description = description - mod.set_stack_bp_from_data(stack,bp_data) + mod.set_stack_bp_from_data(stack, bp_data) end function mod.get_blueprint_description(stack) local bp_data = mod.get_bp_data_for_edit(stack) local desc = bp_data.blueprint.description - if desc == nil then - desc = "" - end + if desc == nil then desc = "" end return desc end -function mod.set_blueprint_label(stack,label) - local bp_data=mod.get_bp_data_for_edit(stack) +function mod.set_blueprint_label(stack, label) + local bp_data = mod.get_bp_data_for_edit(stack) bp_data.blueprint.label = label - mod.set_stack_bp_from_data(stack,bp_data) + mod.set_stack_bp_from_data(stack, bp_data) end function mod.get_blueprint_label(stack) local bp_data = mod.get_bp_data_for_edit(stack) local label = bp_data.blueprint.label - if label == nil then - label = "" - end + if label == nil then label = "" end return label end @@ -55,98 +51,108 @@ function mod.create_blueprint(pindex, point_1, point_2, prior_bp_data) local p = game.get_player(pindex) if prior_bp_data ~= nil then --First clear the bp in hand - p.cursor_stack.set_stack({name = "blueprint", count = 1}) + p.cursor_stack.set_stack({ name = "blueprint", count = 1 }) end - if not p.cursor_stack.valid_for_read or p.cursor_stack.valid_for_read and not (p.cursor_stack.is_blueprint and p.cursor_stack.is_blueprint_setup() == false and prior_bp_data == nil) then + if + not p.cursor_stack.valid_for_read + or p.cursor_stack.valid_for_read + and not (p.cursor_stack.is_blueprint and p.cursor_stack.is_blueprint_setup() == false and prior_bp_data == nil) + then local cleared = p.clear_cursor() if not cleared then printout("Error: cursor full.", pindex) return end end - p.cursor_stack.set_stack({name = "blueprint"}) - p.cursor_stack.create_blueprint{surface = p.surface, force = p.force, area = {top_left,bottom_right}} - - --Avoid empty blueprints + p.cursor_stack.set_stack({ name = "blueprint" }) + p.cursor_stack.create_blueprint({ surface = p.surface, force = p.force, area = { top_left, bottom_right } }) + + --Avoid empty blueprints local ent_count = p.cursor_stack.get_blueprint_entity_count() if ent_count == 0 then - if prior_bp_data == nil then - p.cursor_stack.set_stack({name = "blueprint"}) - end + if prior_bp_data == nil then p.cursor_stack.set_stack({ name = "blueprint" }) end local result = "Blueprint selection area was empty, " - if prior_bp_data ~= nil then - result = result .. " keeping old entities " - end + if prior_bp_data ~= nil then result = result .. " keeping old entities " end printout(result, pindex) else local prior_name = "" - if prior_bp_data ~= nil then - prior_name = prior_bp_data.blueprint.label or "" - end - printout("Blueprint ".. prior_name .. " with " .. ent_count .. " entities created in hand.", pindex) + if prior_bp_data ~= nil then prior_name = prior_bp_data.blueprint.label or "" end + printout("Blueprint " .. prior_name .. " with " .. ent_count .. " entities created in hand.", pindex) end - + --Copy label and description and icons from previous version if prior_bp_data ~= nil then local bp_data = mod.get_bp_data_for_edit(p.cursor_stack) bp_data.blueprint.label = prior_bp_data.blueprint.label or "" - bp_data.blueprint.label_color = prior_bp_data.blueprint.label_color or {1,1,1} + bp_data.blueprint.label_color = prior_bp_data.blueprint.label_color or { 1, 1, 1 } bp_data.blueprint.description = prior_bp_data.blueprint.description or "" bp_data.blueprint.icons = prior_bp_data.blueprint.icons or {} - if ent_count == 0 then - bp_data.blueprint.entities = prior_bp_data.blueprint.entities - end - mod.set_stack_bp_from_data(p.cursor_stack,bp_data) + if ent_count == 0 then bp_data.blueprint.entities = prior_bp_data.blueprint.entities end + mod.set_stack_bp_from_data(p.cursor_stack, bp_data) end --Use this opportunity to update saved information about the blueprint's corners (used when drawing the footprint) local width, height = mod.get_blueprint_width_and_height(pindex) players[pindex].blueprint_width_in_hand = width + 1 players[pindex].blueprint_height_in_hand = height + 1 -end +end --Building function for bluelprints function mod.paste_blueprint(pindex) local p = game.get_player(pindex) local bp = p.cursor_stack local pos = players[pindex].cursor_pos - + --Not a blueprint - if bp.is_blueprint == false then - return nil - end + if bp.is_blueprint == false then return nil end --Empty blueprint - if not bp.is_blueprint_setup() then - return nil - end - + if not bp.is_blueprint_setup() then return nil end + --Get the offset blueprint positions local left_top, right_bottom, build_pos = mod.get_blueprint_corners(pindex, false) - + --Clear build area (if not far away) if util.distance(p.position, build_pos) < 2 * p.reach_distance then fa_mining_tools.clear_obstacles_in_rectangle(left_top, right_bottom, pindex) end - + --Build it and check if successful local dir = players[pindex].blueprint_hand_direction - local result = bp.build_blueprint{surface = p.surface, force = p.force, position = build_pos, direction = dir, by_player = p, force_build = false} + local result = bp.build_blueprint({ + surface = p.surface, + force = p.force, + position = build_pos, + direction = dir, + by_player = p, + force_build = false, + }) if result == nil or #result == 0 then - p.play_sound{path = "utility/cannot_build"} + p.play_sound({ path = "utility/cannot_build" }) --Explain build error local result = "Cannot place there " - local build_area = {left_top, right_bottom} - local ents_in_area = p.surface.find_entities_filtered{area = build_area, invert = true, type = ENT_TYPES_YOU_CAN_BUILD_OVER} - local tiles_in_area = p.surface.find_tiles_filtered{area = build_area, invert = false, name = {"water", "deepwater", "water-green", "deepwater-green", "water-shallow", "water-mud", "water-wube"}} + local build_area = { left_top, right_bottom } + local ents_in_area = + p.surface.find_entities_filtered({ area = build_area, invert = true, type = ENT_TYPES_YOU_CAN_BUILD_OVER }) + local tiles_in_area = p.surface.find_tiles_filtered({ + area = build_area, + invert = false, + name = { "water", "deepwater", "water-green", "deepwater-green", "water-shallow", "water-mud", "water-wube" }, + }) local obstacle_ent_name = nil local obstacle_tile_name = nil --Check for an entity in the way - for i, area_ent in ipairs(ents_in_area) do - if area_ent.valid and area_ent.prototype.tile_width and area_ent.prototype.tile_width > 0 and area_ent.prototype.tile_height and area_ent.prototype.tile_height > 0 then - obstacle_ent_name = localising.get(area_ent,pindex) + for i, area_ent in ipairs(ents_in_area) do + if + area_ent.valid + and area_ent.prototype.tile_width + and area_ent.prototype.tile_width > 0 + and area_ent.prototype.tile_height + and area_ent.prototype.tile_height > 0 + then + obstacle_ent_name = localising.get(area_ent, pindex) end end - + --Report obstacles if obstacle_ent_name ~= nil then result = result .. ", " .. obstacle_ent_name .. " in the way." @@ -156,8 +162,8 @@ function mod.paste_blueprint(pindex) printout(result, pindex) return false else - p.play_sound{path = "Close-Inventory-Sound"}--laterdo maybe better blueprint placement sound - printout("Placed blueprint " .. mod.get_blueprint_label(bp), pindex) + p.play_sound({ path = "Close-Inventory-Sound" }) --laterdo maybe better blueprint placement sound + printout("Placed blueprint " .. mod.get_blueprint_label(bp), pindex) return true end end @@ -166,9 +172,7 @@ end function mod.get_blueprint_corners(pindex, draw_rect) local p = game.get_player(pindex) local bp = p.cursor_stack - if bp == nil or bp.valid_for_read == false or bp.is_blueprint == false then - error("invalid call. no blueprint") - end + if bp == nil or bp.valid_for_read == false or bp.is_blueprint == false then error("invalid call. no blueprint") end local pos = players[pindex].cursor_pos local ents = bp.get_blueprint_entities() or {} local west_most_x = 0 @@ -176,16 +180,16 @@ function mod.get_blueprint_corners(pindex, draw_rect) local north_most_y = 0 local south_most_y = 0 local first_ent = true - --Empty blueprint: Just circle the cursor + --Empty blueprint: Just circle the cursor if bp.is_blueprint_setup() == false then - local left_top = {x = math.floor(pos.x), y = math.floor(pos.y)} - local right_bottom = {x = math.ceil(pos.x), y = math.ceil(pos.y)} + local left_top = { x = math.floor(pos.x), y = math.floor(pos.y) } + local right_bottom = { x = math.ceil(pos.x), y = math.ceil(pos.y) } --local rect = rendering.draw_rectangle{left_top = left_top, right_bottom = right_bottom, color = {r = 0.25, b = 0.25, g = 1.0, a = 0.75}, draw_on_ground = true, surface = game.get_player(pindex).surface, players = nil } return left_top, right_bottom, pos end - - --Find the blueprint borders and corners - for i, ent in ipairs(ents) do + + --Find the blueprint borders and corners + for i, ent in ipairs(ents) do local ent_width = game.entity_prototypes[ent.name].tile_width local ent_height = game.entity_prototypes[ent.name].tile_height if ent.direction == dirs.east or ent.direction == dirs.west then @@ -193,10 +197,10 @@ function mod.get_blueprint_corners(pindex, draw_rect) ent_height = game.entity_prototypes[ent.name].tile_width end --Find the edges of this ent - local ent_north = ent.position.y - math.floor(ent_height/2) - local ent_east = ent.position.x + math.floor(ent_width/2) - local ent_south = ent.position.y + math.floor(ent_height/2) - local ent_west = ent.position.x - math.floor(ent_width/2) + local ent_north = ent.position.y - math.floor(ent_height / 2) + local ent_east = ent.position.x + math.floor(ent_width / 2) + local ent_south = ent.position.y + math.floor(ent_height / 2) + local ent_west = ent.position.x - math.floor(ent_width / 2) --Initialize with this entity if first_ent then first_ent = false @@ -205,45 +209,48 @@ function mod.get_blueprint_corners(pindex, draw_rect) north_most_y = ent_north south_most_y = ent_south else - --Compare ent edges with the blueprint edges - if west_most_x > ent_west then - west_most_x = ent_west - end - if east_most_x < ent_east then - east_most_x = ent_east - end - if north_most_y > ent_north then - north_most_y = ent_north - end - if south_most_y < ent_south then - south_most_y = ent_south - end + --Compare ent edges with the blueprint edges + if west_most_x > ent_west then west_most_x = ent_west end + if east_most_x < ent_east then east_most_x = ent_east end + if north_most_y > ent_north then north_most_y = ent_north end + if south_most_y < ent_south then south_most_y = ent_south end end end --Determine blueprint dimensions from the final edges - local bp_left_top = {x = math.floor(west_most_x), y = math.floor(north_most_y)} - local bp_right_bottom = {x = math.ceil(east_most_x), y = math.ceil(south_most_y)} + local bp_left_top = { x = math.floor(west_most_x), y = math.floor(north_most_y) } + local bp_right_bottom = { x = math.ceil(east_most_x), y = math.ceil(south_most_y) } local bp_width = bp_right_bottom.x - bp_left_top.x - 1 local bp_height = bp_right_bottom.y - bp_left_top.y - 1 - if players[pindex].blueprint_hand_direction == dirs.east or players[pindex].blueprint_hand_direction == dirs.west then + if + players[pindex].blueprint_hand_direction == dirs.east or players[pindex].blueprint_hand_direction == dirs.west + then --Flip width and height bp_width = bp_right_bottom.y - bp_left_top.y - 1 bp_height = bp_right_bottom.x - bp_left_top.x - 1 end - local left_top = {x = math.floor(pos.x), y = math.floor(pos.y)} - local right_bottom = {x = math.ceil(pos.x + bp_width), y = math.ceil(pos.y + bp_height)} - + local left_top = { x = math.floor(pos.x), y = math.floor(pos.y) } + local right_bottom = { x = math.ceil(pos.x + bp_width), y = math.ceil(pos.y + bp_height) } + --Draw the build preview (default is false) if draw_rect == true then --Draw a temporary rectangle for debugging - rendering.draw_rectangle{left_top = left_top, right_bottom = right_bottom, color = {r = 0.25, b = 0.25, g = 1.0, a = 0.75}, width = 2, draw_on_ground = true, surface = p.surface, players = nil, time_to_live = 100} + rendering.draw_rectangle({ + left_top = left_top, + right_bottom = right_bottom, + color = { r = 0.25, b = 0.25, g = 1.0, a = 0.75 }, + width = 2, + draw_on_ground = true, + surface = p.surface, + players = nil, + time_to_live = 100, + }) end - + --Get the mouse pointer position - local mouse_pos = {x = pos.x + bp_width/2, y = pos.y + bp_height/2} - + local mouse_pos = { x = pos.x + bp_width / 2, y = pos.y + bp_height / 2 } + return left_top, right_bottom, mouse_pos -end +end function mod.get_blueprint_width_and_height(pindex) local p = game.get_player(pindex) @@ -251,9 +258,7 @@ function mod.get_blueprint_width_and_height(pindex) if bp == nil or bp.valid_for_read == false or bp.is_blueprint == false then bp = game.get_player(pindex).get_main_inventory()[players[pindex].inventory.index] end - if bp == nil or bp.valid_for_read == false or bp.is_blueprint == false then - return nil, nil - end + if bp == nil or bp.valid_for_read == false or bp.is_blueprint == false then return nil, nil end local pos = players[pindex].cursor_pos local ents = bp.get_blueprint_entities() local west_most_x = 0 @@ -261,13 +266,11 @@ function mod.get_blueprint_width_and_height(pindex) local north_most_y = 0 local south_most_y = 0 local first_ent = true - + --Empty blueprint - if not ents or bp.is_blueprint_setup() == false then - return 0, 0 - end - - --Find the blueprint borders and corners + if not ents or bp.is_blueprint_setup() == false then return 0, 0 end + + --Find the blueprint borders and corners for i, ent in ipairs(ents) do local ent_width = game.entity_prototypes[ent.name].tile_width local ent_height = game.entity_prototypes[ent.name].tile_height @@ -276,10 +279,10 @@ function mod.get_blueprint_width_and_height(pindex) ent_height = game.entity_prototypes[ent.name].tile_width end --Find the edges of this ent - local ent_north = ent.position.y - math.floor(ent_height/2) - local ent_east = ent.position.x + math.floor(ent_width/2) - local ent_south = ent.position.y + math.floor(ent_height/2) - local ent_west = ent.position.x - math.floor(ent_width/2) + local ent_north = ent.position.y - math.floor(ent_height / 2) + local ent_east = ent.position.x + math.floor(ent_width / 2) + local ent_south = ent.position.y + math.floor(ent_height / 2) + local ent_west = ent.position.x - math.floor(ent_width / 2) --Initialize with this entity if first_ent then first_ent = false @@ -288,40 +291,32 @@ function mod.get_blueprint_width_and_height(pindex) north_most_y = ent_north south_most_y = ent_south else - --Compare ent edges with the blueprint edges - if west_most_x > ent_west then - west_most_x = ent_west - end - if east_most_x < ent_east then - east_most_x = ent_east - end - if north_most_y > ent_north then - north_most_y = ent_north - end - if south_most_y < ent_south then - south_most_y = ent_south - end + --Compare ent edges with the blueprint edges + if west_most_x > ent_west then west_most_x = ent_west end + if east_most_x < ent_east then east_most_x = ent_east end + if north_most_y > ent_north then north_most_y = ent_north end + if south_most_y < ent_south then south_most_y = ent_south end end end --Determine blueprint dimensions from the final edges - local bp_left_top = {x = math.floor(west_most_x), y = math.floor(north_most_y)} - local bp_right_bottom = {x = math.ceil(east_most_x), y = math.ceil(south_most_y)} + local bp_left_top = { x = math.floor(west_most_x), y = math.floor(north_most_y) } + local bp_right_bottom = { x = math.ceil(east_most_x), y = math.ceil(south_most_y) } local bp_width = bp_right_bottom.x - bp_left_top.x - 1 local bp_height = bp_right_bottom.y - bp_left_top.y - 1 - if players[pindex].blueprint_hand_direction == dirs.east or players[pindex].blueprint_hand_direction == dirs.west then + if + players[pindex].blueprint_hand_direction == dirs.east or players[pindex].blueprint_hand_direction == dirs.west + then --Flip width and height bp_width = bp_right_bottom.y - bp_left_top.y - 1 bp_height = bp_right_bottom.x - bp_left_top.x - 1 end return bp_width, bp_height -end +end --Export and import the same blueprint so that its parameters reset, e.g. rotation. function mod.refresh_blueprint_in_hand(pindex) local p = game.get_player(pindex) - if p.cursor_stack.is_blueprint_setup() == false then - return - end + if p.cursor_stack.is_blueprint_setup() == false then return end local bp_data = mod.get_bp_data_for_edit(p.cursor_stack) mod.set_stack_bp_from_data(p.cursor_stack, bp_data) end @@ -329,42 +324,32 @@ end --Basic info for when the blueprint item is read. function mod.get_blueprint_info(stack, in_hand) --Not a blueprint - if stack.is_blueprint == false then - return "" - end + if stack.is_blueprint == false then return "" end --Empty blueprint - if not stack.is_blueprint_setup() then - return "Blueprint empty" - end - + if not stack.is_blueprint_setup() then return "Blueprint empty" end + --Get name local name = mod.get_blueprint_label(stack) - if name == nil then - name = "" - end - --Construct result + if name == nil then name = "" end + --Construct result local result = "Blueprint " .. name .. " features " - if in_hand then - result = "Blueprint " .. name .. "in hand, features " - end + if in_hand then result = "Blueprint " .. name .. "in hand, features " end --Use icons as extra info (in case it is not named) local icons = stack.blueprint_icons if icons == nil or #icons == 0 then result = result .. " no details " return result end - - for i, signal in ipairs(icons) do - if signal.index > 1 then - result = result .. " and " - end + + for i, signal in ipairs(icons) do + if signal.index > 1 then result = result .. " and " end if signal.signal.name ~= nil then - result = result .. signal.signal.name --***todo localise + result = result .. signal.signal.name --***todo localise else result = result .. "unknown icon" end end - + result = result .. ", " .. stack.get_blueprint_entity_count() .. " entities in total " --game.print(result) @@ -383,11 +368,9 @@ function mod.get_blueprint_icons_info(bp_table) result = result .. " no icons " return result end - - for i, signal in ipairs(icons) do - if signal.index > 1 then - result = result .. " and " - end + + for i, signal in ipairs(icons) do + if signal.index > 1 then result = result .. " and " end if signal.signal.name ~= nil then result = result .. signal.signal.name else @@ -409,7 +392,7 @@ function mod.apply_blueprint_import(pindex, text) else printout("Successfully imported unknown planner item", pindex) end - elseif result == -1 then + elseif result == -1 then if bp.is_blueprint then printout("Imported with errors, blueprint " .. mod.get_blueprint_label(bp), pindex) elseif bp.is_blueprint_book then @@ -417,7 +400,7 @@ function mod.apply_blueprint_import(pindex, text) else printout("Imported with errors, unknown planner item", pindex) end - else--result == 1 + else --result == 1 printout("Failed to import blueprint item", pindex) end end @@ -446,42 +429,52 @@ function mod.run_blueprint_menu(menu_index, pindex, clicked, other_input) local p = game.get_player(pindex) ---@type LuaItemStack local bp = p.cursor_stack - + if bp.is_blueprint_setup() == false then if index == 0 then --Give basic info ... - printout("Empty blueprint with limited options" - .. ", Press 'W' and 'S' to navigate options, press 'LEFT BRACKET' to select an option or press 'E' to exit this menu.", pindex) - elseif index == 1 then + printout( + "Empty blueprint with limited options" + .. ", Press 'W' and 'S' to navigate options, press 'LEFT BRACKET' to select an option or press 'E' to exit this menu.", + pindex + ) + elseif index == 1 then --Import a text string to save into this blueprint if not clicked then local result = "Import a text string to fill this blueprint" printout(result, pindex) else players[pindex].blueprint_menu.edit_import = true - local frame = game.get_player(pindex).gui.screen.add{type = "frame", name = "blueprint-edit-import"} + local frame = game.get_player(pindex).gui.screen.add({ type = "frame", name = "blueprint-edit-import" }) frame.bring_to_front() frame.force_auto_center() frame.focus() - local input = frame.add{type="textfield", name = "input"} + local input = frame.add({ type = "textfield", name = "input" }) input.focus() local result = "Paste a copied blueprint text string in this box and then press ENTER to load it" printout(result, pindex) end --elseif index == 2 then --use last selected area *** - else + else players[pindex].blueprint_menu.index = 0 - p.play_sound{path = "inventory-wrap-around"} - printout("Empty blueprint with limited options" - .. ", Press 'W' and 'S' to navigate options, press 'LEFT BRACKET' to select an option or press 'E' to exit this menu.", pindex) + p.play_sound({ path = "inventory-wrap-around" }) + printout( + "Empty blueprint with limited options" + .. ", Press 'W' and 'S' to navigate options, press 'LEFT BRACKET' to select an option or press 'E' to exit this menu.", + pindex + ) end return end - + if index == 0 then --Give basic info ... - printout("Blueprint " .. mod.get_blueprint_label(bp) - .. ", Press 'W' and 'S' to navigate options, press 'LEFT BRACKET' to select an option or press 'E' to exit this menu.", pindex) + printout( + "Blueprint " + .. mod.get_blueprint_label(bp) + .. ", Press 'W' and 'S' to navigate options, press 'LEFT BRACKET' to select an option or press 'E' to exit this menu.", + pindex + ) elseif index == 1 then --Read the description of this blueprint if not clicked then @@ -489,9 +482,7 @@ function mod.run_blueprint_menu(menu_index, pindex, clicked, other_input) printout(result, pindex) else local result = mod.get_blueprint_description(bp) - if result == nil or result == "" then - result = "no description" - end + if result == nil or result == "" then result = "no description" end printout(result, pindex) end elseif index == 2 then @@ -503,32 +494,30 @@ function mod.run_blueprint_menu(menu_index, pindex, clicked, other_input) local result = "This blueprint features " if bp.blueprint_icons and #bp.blueprint_icons > 0 then --Icon 1 - if bp.blueprint_icons[1] ~= nil then - result = result .. bp.blueprint_icons[1].signal.name .. ", " - end - if bp.blueprint_icons[2] ~= nil then - result = result .. bp.blueprint_icons[2].signal.name .. ", " - end - if bp.blueprint_icons[3] ~= nil then - result = result .. bp.blueprint_icons[3].signal.name .. ", " - end - if bp.blueprint_icons[4] ~= nil then - result = result .. bp.blueprint_icons[4].signal.name .. ", " - end + if bp.blueprint_icons[1] ~= nil then result = result .. bp.blueprint_icons[1].signal.name .. ", " end + if bp.blueprint_icons[2] ~= nil then result = result .. bp.blueprint_icons[2].signal.name .. ", " end + if bp.blueprint_icons[3] ~= nil then result = result .. bp.blueprint_icons[3].signal.name .. ", " end + if bp.blueprint_icons[4] ~= nil then result = result .. bp.blueprint_icons[4].signal.name .. ", " end else result = result .. "nothing" end printout(result, pindex) end elseif index == 3 then - --Read the blueprint dimensions and total component count + --Read the blueprint dimensions and total component count if not clicked then local result = "Read the blueprint dimensions and total component count" printout(result, pindex) else local count = bp.get_blueprint_entity_count() local width, height = mod.get_blueprint_width_and_height(pindex) - local result = "This blueprint is " .. (width + 1) .. " tiles wide and " .. (height + 1) .. " tiles high and contains " .. count .. " entities " + local result = "This blueprint is " + .. (width + 1) + .. " tiles wide and " + .. (height + 1) + .. " tiles high and contains " + .. count + .. " entities " printout(result, pindex) end elseif index == 4 then @@ -542,7 +531,7 @@ function mod.run_blueprint_menu(menu_index, pindex, clicked, other_input) local ent_counts = {} local unique_ent_count = 0 --p.print("blueprint total entity count: " .. #ents)-- - for i, ent in ipairs(ents) do + for i, ent in ipairs(ents) do local str = ent.name if ent_counts[str] == nil then ent_counts[str] = 1 @@ -555,17 +544,15 @@ function mod.run_blueprint_menu(menu_index, pindex, clicked, other_input) end --p.print("blueprint unique entity count: " .. unique_ent_count) --Sort by count - table.sort(ent_counts, function(a,b) + table.sort(ent_counts, function(a, b) return ent_counts[a] < ent_counts[b] end) --List results local result = "Blueprint contains " - for name, count in pairs(ent_counts) do + for name, count in pairs(ent_counts) do result = result .. count .. " " .. name .. ", " end - if unique_ent_count == 0 then - result = result .. "nothing" - end + if unique_ent_count == 0 then result = result .. "nothing" end printout(result, pindex) --p.print(result)-- end @@ -580,7 +567,7 @@ function mod.run_blueprint_menu(menu_index, pindex, clicked, other_input) local ent_counts = {} local unique_ent_count = 0 --p.print("blueprint total entity count: " .. #ents)-- - for i, ent in ipairs(ents) do + for i, ent in ipairs(ents) do local str = ent.name if ent_counts[str] == nil then ent_counts[str] = 1 @@ -594,7 +581,7 @@ function mod.run_blueprint_menu(menu_index, pindex, clicked, other_input) --p.print("blueprint unique entity count: " .. unique_ent_count) --Subtract inventory amounts local result = "Blueprint contains " - for name, count in pairs(ent_counts) do + for name, count in pairs(ent_counts) do local inv_count = p.get_main_inventory().get_item_count(name) if inv_count >= count then ent_counts[name] = 0 @@ -603,21 +590,19 @@ function mod.run_blueprint_menu(menu_index, pindex, clicked, other_input) end end --Sort by count - table.sort(ent_counts, function(a,b) + table.sort(ent_counts, function(a, b) return ent_counts[a] < ent_counts[b] end) --Read results local result = "You are missing " unique_ent_count = 0 - for name, count in pairs(ent_counts) do + for name, count in pairs(ent_counts) do if count > 0 then result = result .. count .. " " .. name .. ", " unique_ent_count = unique_ent_count + 1 end end - if unique_ent_count == 0 then - result = result .. "nothing" - end + if unique_ent_count == 0 then result = result .. "nothing" end result = result .. " to build this blueprint " printout(result, pindex) --p.print(result)-- @@ -629,11 +614,11 @@ function mod.run_blueprint_menu(menu_index, pindex, clicked, other_input) printout(result, pindex) else players[pindex].blueprint_menu.edit_label = true - local frame = game.get_player(pindex).gui.screen.add{type = "frame", name = "blueprint-edit-label"} + local frame = game.get_player(pindex).gui.screen.add({ type = "frame", name = "blueprint-edit-label" }) frame.bring_to_front() frame.force_auto_center() frame.focus() - local input = frame.add{type="textfield", name = "input"} + local input = frame.add({ type = "textfield", name = "input" }) input.focus() local result = "Type in a new name for this blueprint and press 'ENTER' to confirm, or press 'ESC' to cancel." printout(result, pindex) @@ -645,13 +630,14 @@ function mod.run_blueprint_menu(menu_index, pindex, clicked, other_input) printout(result, pindex) else players[pindex].blueprint_menu.edit_description = true - local frame = game.get_player(pindex).gui.screen.add{type = "frame", name = "blueprint-edit-description"} + local frame = game.get_player(pindex).gui.screen.add({ type = "frame", name = "blueprint-edit-description" }) frame.bring_to_front() frame.force_auto_center() frame.focus() - local input = frame.add{type="textfield", name = "input"}--, text = get_blueprint_description(bp)} + local input = frame.add({ type = "textfield", name = "input" }) --, text = get_blueprint_description(bp)} input.focus() - local result = "Type in the new description text box for this blueprint and press 'ENTER' to confirm, or press 'ESC' to cancel." + local result = + "Type in the new description text box for this blueprint and press 'ENTER' to confirm, or press 'ESC' to cancel." printout(result, pindex) end elseif index == 8 then @@ -670,8 +656,8 @@ function mod.run_blueprint_menu(menu_index, pindex, clicked, other_input) local result = "Delete this blueprint" printout(result, pindex) else - bp.set_stack({name = "blueprint", count = 1}) - bp.set_stack(nil)--calls event handler to delete empty planners. + bp.set_stack({ name = "blueprint", count = 1 }) + bp.set_stack(nil) --calls event handler to delete empty planners. local result = "Blueprint deleted and menu closed" printout(result, pindex) mod.blueprint_menu_close(pindex) @@ -683,13 +669,14 @@ function mod.run_blueprint_menu(menu_index, pindex, clicked, other_input) printout(result, pindex) else players[pindex].blueprint_menu.edit_export = true - local frame = game.get_player(pindex).gui.screen.add{type = "frame", name = "blueprint-edit-export"} + local frame = game.get_player(pindex).gui.screen.add({ type = "frame", name = "blueprint-edit-export" }) frame.bring_to_front() frame.force_auto_center() frame.focus() - local input = frame.add{type="textfield", name = "input", text = bp.export_stack()} + local input = frame.add({ type = "textfield", name = "input", text = bp.export_stack() }) input.focus() - local result = "Copy the text from this box using 'CONTROL + A' and then 'CONTROL + C' and then press ENTER to exit" + local result = + "Copy the text from this box using 'CONTROL + A' and then 'CONTROL + C' and then press ENTER to exit" printout(result, pindex) end elseif index == 11 then @@ -699,11 +686,11 @@ function mod.run_blueprint_menu(menu_index, pindex, clicked, other_input) printout(result, pindex) else players[pindex].blueprint_menu.edit_import = true - local frame = game.get_player(pindex).gui.screen.add{type = "frame", name = "blueprint-edit-import"} + local frame = game.get_player(pindex).gui.screen.add({ type = "frame", name = "blueprint-edit-import" }) frame.bring_to_front() frame.force_auto_center() frame.focus() - local input = frame.add{type="textfield", name = "input"} + local input = frame.add({ type = "textfield", name = "input" }) input.focus() local result = "Paste a copied blueprint text string in this box and then press ENTER to load it" printout(result, pindex) @@ -724,27 +711,25 @@ end BLUEPRINT_MENU_LENGTH = 12 function mod.blueprint_menu_open(pindex) - if players[pindex].vanilla_mode then - return - end + if players[pindex].vanilla_mode then return end --Set the player menu tracker to this menu players[pindex].menu = "blueprint_menu" players[pindex].in_menu = true players[pindex].move_queue = {} - + --Set the menu line counter to 0 players[pindex].blueprint_menu = { index = 0, edit_label = false, edit_description = false, edit_export = false, - edit_import = false - } - + edit_import = false, + } + --Play sound - game.get_player(pindex).play_sound{path = "Open-Inventory-Sound"} - - --Load menu + game.get_player(pindex).play_sound({ path = "Open-Inventory-Sound" }) + + --Load menu mod.run_blueprint_menu(players[pindex].blueprint_menu.index, pindex, false) end @@ -756,38 +741,34 @@ function mod.blueprint_menu_close(pindex, mute_in) --Set the menu line counter to 0 players[pindex].blueprint_menu.index = 0 - + --play sound - if not mute then - game.get_player(pindex).play_sound{path="Close-Inventory-Sound"} - end - + if not mute then game.get_player(pindex).play_sound({ path = "Close-Inventory-Sound" }) end + --Destroy text fields - if game.get_player(pindex).gui.screen["blueprint-edit-label"] ~= nil then + if game.get_player(pindex).gui.screen["blueprint-edit-label"] ~= nil then game.get_player(pindex).gui.screen["blueprint-edit-label"].destroy() end - if game.get_player(pindex).gui.screen["blueprint-edit-description"] ~= nil then + if game.get_player(pindex).gui.screen["blueprint-edit-description"] ~= nil then game.get_player(pindex).gui.screen["blueprint-edit-description"].destroy() end - if game.get_player(pindex).gui.screen["blueprint-edit-export"] ~= nil then + if game.get_player(pindex).gui.screen["blueprint-edit-export"] ~= nil then game.get_player(pindex).gui.screen["blueprint-edit-export"].destroy() end if game.get_player(pindex).gui.screen["blueprint-edit-import"] ~= nil then game.get_player(pindex).gui.screen["blueprint-edit-import"].destroy() end - if game.get_player(pindex).opened ~= nil then - game.get_player(pindex).opened = nil - end + if game.get_player(pindex).opened ~= nil then game.get_player(pindex).opened = nil end end function mod.blueprint_menu_up(pindex) players[pindex].blueprint_menu.index = players[pindex].blueprint_menu.index - 1 if players[pindex].blueprint_menu.index < 0 then players[pindex].blueprint_menu.index = 0 - game.get_player(pindex).play_sound{path = "inventory-edge"} + game.get_player(pindex).play_sound({ path = "inventory-edge" }) else --Play sound - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) end --Load menu mod.run_blueprint_menu(players[pindex].blueprint_menu.index, pindex, false) @@ -797,10 +778,10 @@ function mod.blueprint_menu_down(pindex) players[pindex].blueprint_menu.index = players[pindex].blueprint_menu.index + 1 if players[pindex].blueprint_menu.index > BLUEPRINT_MENU_LENGTH then players[pindex].blueprint_menu.index = BLUEPRINT_MENU_LENGTH - game.get_player(pindex).play_sound{path = "inventory-edge"} + game.get_player(pindex).play_sound({ path = "inventory-edge" }) else --Play sound - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) end --Load menu mod.run_blueprint_menu(players[pindex].blueprint_menu.index, pindex, false) @@ -808,7 +789,7 @@ end local function get_bp_book_data_for_edit(stack) ---@diagnostic disable-next-line: param-type-mismatch - return game.json_to_table(game.decode_string(string.sub(stack.export_stack(),2))) + return game.json_to_table(game.decode_string(string.sub(stack.export_stack(), 2))) end --We run the export just once because it eats UPS @@ -819,9 +800,7 @@ end function mod.blueprint_book_get_name(pindex) local bp_data = players[pindex].blueprint_book_menu.book_data local label = bp_data.blueprint_book.label - if label == nil then - label = "" - end + if label == nil then label = "" end return label end @@ -830,14 +809,14 @@ function mod.blueprint_book_set_name(pindex, new_name) bp_data.blueprint_book.label = new_name -- TODO: stack is a global but too generically named to find. If it's not -- this is nil and that's bad. - mod.set_stack_bp_from_data(stack,bp_data) + mod.set_stack_bp_from_data(stack, bp_data) end function mod.blueprint_book_get_item_count(pindex) local bp_data = players[pindex].blueprint_book_menu.book_data local items = bp_data.blueprint_book.blueprints if items == nil or items == {} then - return 0 + return 0 else return #items end @@ -846,39 +825,39 @@ end function mod.blueprint_book_data_get_item_count(book_data) local items = book_data.blueprint_book.blueprints if items == nil or items == {} then - return 0 + return 0 else return #items end end --Reads a blueprint within the blueprint book -function mod.blueprint_book_read_item(pindex,i) +function mod.blueprint_book_read_item(pindex, i) local bp_data = players[pindex].blueprint_book_menu.book_data local items = bp_data.blueprint_book.blueprints return items[i]["blueprint"] end ---Puts the book away and imports the selected blueprint to hand -function mod.blueprint_book_copy_item_to_hand(pindex,i) +--Puts the book away and imports the selected blueprint to hand +function mod.blueprint_book_copy_item_to_hand(pindex, i) local bp_data = players[pindex].blueprint_book_menu.book_data local items = bp_data.blueprint_book.blueprints local item = items[i]["blueprint"] local item_string = "0" .. game.encode_string(game.table_to_json(items[i])) - - local p = game.get_player(pindex) + + local p = game.get_player(pindex) p.clear_cursor() p.cursor_stack.import_stack(item_string) - printout("Copied blueprint to hand",pindex) + printout("Copied blueprint to hand", pindex) end --todo *** -function mod.blueprint_book_take_out_item(pindex,index) +function mod.blueprint_book_take_out_item(pindex, index) --todo *** end --todo *** -function mod.blueprint_book_add_item(pindex,bp) +function mod.blueprint_book_add_item(pindex, bp) --todo *** end @@ -905,34 +884,35 @@ function mod.run_blueprint_book_menu(pindex, menu_index, list_mode, left_clicked local item_count = mod.blueprint_book_get_item_count(pindex) --Update menu length players[pindex].blueprint_book_menu.menu_length = BLUEPRINT_BOOK_SETTINGS_MENU_LENGTH - if list_mode then - players[pindex].blueprint_book_menu.menu_length = item_count - end - + if list_mode then players[pindex].blueprint_book_menu.menu_length = item_count end + --Run menu if list_mode then - --Blueprint book list mode + --Blueprint book list mode if index == 0 then --stuff - printout("Browsing blueprint book " .. mod.blueprint_book_get_name(pindex) .. ", with " .. item_count .. " items," - .. ", Press 'W' and 'S' to navigate options, press 'LEFT BRACKET' to copy a blueprint to hand, press 'E' to exit this menu.", pindex) + printout( + "Browsing blueprint book " + .. mod.blueprint_book_get_name(pindex) + .. ", with " + .. item_count + .. " items," + .. ", Press 'W' and 'S' to navigate options, press 'LEFT BRACKET' to copy a blueprint to hand, press 'E' to exit this menu.", + pindex + ) else - --Examine items + --Examine items local item = mod.blueprint_book_read_item(pindex, index) local name = "" if item == nil or item.item == nil then name = "Unknown item (" .. index .. ")" elseif item.item == "blueprint" then local label = item.label - if label == nil then - label = "" - end + if label == nil then label = "" end name = "Blueprint " .. label .. ", featuring " .. mod.get_blueprint_icons_info(item) elseif item.item == "blueprint-book" or item.item == "blueprint_book" or item.item == "book" then local label = item.label - if label == nil then - label = "" - end + if label == nil then label = "" end -- TODO: nil in the following line used to be an undefined global -- book_data. Someone needs to determine what that's supposed to -- be. @@ -940,31 +920,41 @@ function mod.run_blueprint_book_menu(pindex, menu_index, list_mode, left_clicked else name = "unknown item " .. item.item end - if left_clicked == false and right_clicked == false then + if left_clicked == false and right_clicked == false then --Read blueprint info local result = name printout(result, pindex) - elseif left_clicked == true and right_clicked == false then + elseif left_clicked == true and right_clicked == false then --Copy the blueprint to hand if item == nil or item.item == nil then printout("Cannot get this.", pindex) elseif item.item == "blueprint" or item.item == "blueprint-book" then - mod.blueprint_book_copy_item_to_hand(pindex,index) + mod.blueprint_book_copy_item_to_hand(pindex, index) else printout("Cannot get this.", pindex) end - elseif left_clicked == false and right_clicked == true then + elseif left_clicked == false and right_clicked == true then --Take the blueprint to hand (Therefore both copy and delete) --... end end else - --Blueprint book settings mode + --Blueprint book settings mode if true then - printout("Settings for blueprint book " .. mod.blueprint_book_get_name(pindex) .. " not yet implemented ", pindex)--*** + printout( + "Settings for blueprint book " .. mod.blueprint_book_get_name(pindex) .. " not yet implemented ", + pindex + ) --*** elseif index == 0 then - printout("Settings for blueprint book " .. mod.blueprint_book_get_name(pindex) .. ", with " .. item_count .. " items," - .. " Press 'W' and 'S' to navigate options, press 'LEFT BRACKET' to select, press 'E' to exit this menu.", pindex) + printout( + "Settings for blueprint book " + .. mod.blueprint_book_get_name(pindex) + .. ", with " + .. item_count + .. " items," + .. " Press 'W' and 'S' to navigate options, press 'LEFT BRACKET' to select, press 'E' to exit this menu.", + pindex + ) elseif index == 1 then --Read the icons of this blueprint book, which are its featured components if left_clicked ~= true then @@ -990,7 +980,7 @@ function mod.run_blueprint_book_menu(pindex, menu_index, list_mode, left_clicked --Stuff *** end elseif index == 4 then - --Clear this blueprint book + --Clear this blueprint book if left_clicked ~= true then local result = "Clear this blueprint book" printout(result, pindex) @@ -998,7 +988,7 @@ function mod.run_blueprint_book_menu(pindex, menu_index, list_mode, left_clicked --Stuff *** end elseif index == 5 then - --Export this blueprint book as a text string + --Export this blueprint book as a text string if left_clicked ~= true then local result = "Export this blueprint book as a text string" printout(result, pindex) @@ -1014,36 +1004,34 @@ function mod.run_blueprint_book_menu(pindex, menu_index, list_mode, left_clicked --Stuff *** end end - end + end end BLUEPRINT_BOOK_SETTINGS_MENU_LENGTH = 1 function mod.blueprint_book_menu_open(pindex, open_in_list_mode) - if players[pindex].vanilla_mode then - return - end + if players[pindex].vanilla_mode then return end --Set the player menu tracker to this menu players[pindex].menu = "blueprint_book_menu" players[pindex].in_menu = true players[pindex].move_queue = {} - + --Set the menu line counter to 0 players[pindex].blueprint_book_menu = { book_data = nil, index = 0, menu_length = 0, - list_mode = open_in_list_mode, + list_mode = open_in_list_mode, edit_label = false, edit_description = false, edit_export = false, - edit_import = false - } + edit_import = false, + } set_bp_book_data_from_cursor(pindex) - + --Play sound - game.get_player(pindex).play_sound{path = "Open-Inventory-Sound"} - - --Load menu + game.get_player(pindex).play_sound({ path = "Open-Inventory-Sound" }) + + --Load menu local bpb_menu = players[pindex].blueprint_book_menu mod.run_blueprint_book_menu(pindex, bpb_menu.index, bpb_menu.list_mode, false, false) end @@ -1056,38 +1044,34 @@ function mod.blueprint_book_menu_close(pindex, mute_in) --Set the menu line counter to 0 players[pindex].blueprint_book_menu.index = 0 - + --play sound - if not mute then - game.get_player(pindex).play_sound{path="Close-Inventory-Sound"} - end - + if not mute then game.get_player(pindex).play_sound({ path = "Close-Inventory-Sound" }) end + --Destroy text fields - if game.get_player(pindex).gui.screen["blueprint-book-edit-label"] ~= nil then + if game.get_player(pindex).gui.screen["blueprint-book-edit-label"] ~= nil then game.get_player(pindex).gui.screen["blueprint-book-edit-label"].destroy() end - if game.get_player(pindex).gui.screen["blueprint-book-edit-description"] ~= nil then + if game.get_player(pindex).gui.screen["blueprint-book-edit-description"] ~= nil then game.get_player(pindex).gui.screen["blueprint-book-edit-description"].destroy() end - if game.get_player(pindex).gui.screen["blueprint-book-edit-export"] ~= nil then + if game.get_player(pindex).gui.screen["blueprint-book-edit-export"] ~= nil then game.get_player(pindex).gui.screen["blueprint-book-edit-export"].destroy() end if game.get_player(pindex).gui.screen["blueprint-book-edit-import"] ~= nil then game.get_player(pindex).gui.screen["blueprint-book-edit-import"].destroy() end - if game.get_player(pindex).opened ~= nil then - game.get_player(pindex).opened = nil - end + if game.get_player(pindex).opened ~= nil then game.get_player(pindex).opened = nil end end function mod.blueprint_book_menu_up(pindex) players[pindex].blueprint_book_menu.index = players[pindex].blueprint_book_menu.index - 1 if players[pindex].blueprint_book_menu.index < 0 then players[pindex].blueprint_book_menu.index = 0 - game.get_player(pindex).play_sound{path = "inventory-edge"} + game.get_player(pindex).play_sound({ path = "inventory-edge" }) else --Play sound - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) end --Load menu local bpb_menu = players[pindex].blueprint_book_menu @@ -1098,14 +1082,14 @@ function mod.blueprint_book_menu_down(pindex) players[pindex].blueprint_book_menu.index = players[pindex].blueprint_book_menu.index + 1 if players[pindex].blueprint_book_menu.index > players[pindex].blueprint_book_menu.menu_length then players[pindex].blueprint_book_menu.index = players[pindex].blueprint_book_menu.menu_length - game.get_player(pindex).play_sound{path = "inventory-edge"} + game.get_player(pindex).play_sound({ path = "inventory-edge" }) else --Play sound - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) end --Load menu local bpb_menu = players[pindex].blueprint_book_menu mod.run_blueprint_book_menu(pindex, bpb_menu.index, bpb_menu.list_mode, false, false) end -return mod \ No newline at end of file +return mod diff --git a/scripts/building-tools.lua b/scripts/building-tools.lua index 84a37a45..50614461 100644 --- a/scripts/building-tools.lua +++ b/scripts/building-tools.lua @@ -22,8 +22,8 @@ function mod.build_item_in_hand(pindex, free_place_straight_rail) --Valid stack check if not (stack and stack.valid and stack.valid_for_read) then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} - local message = "Invalid item in hand!" + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) + local message = "Invalid item in hand!" if game.get_player(pindex).is_cursor_empty() then local auto_cancel_when_empty = true --laterdo this check may become a toggle-able game setting if players[pindex].build_lock == true and auto_cancel_when_empty then @@ -31,7 +31,7 @@ function mod.build_item_in_hand(pindex, free_place_straight_rail) message = "Build lock disabled, emptied hand." end end - printout(message,pindex) + printout(message, pindex) return end @@ -47,8 +47,8 @@ function mod.build_item_in_hand(pindex, free_place_straight_rail) return end elseif stack.name == "rail-signal" or stack.name == "rail-chain-signal" then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} - printout("You need to use the building menu of a rail.",pindex) + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) + printout("You need to use the building menu of a rail.", pindex) return end --General build cases @@ -58,10 +58,15 @@ function mod.build_item_in_hand(pindex, free_place_straight_rail) local position = nil local placing_underground_belt = stack.prototype.place_result.type == "underground-belt" - if not(players[pindex].cursor) then - --Not in cursor mode + if not players[pindex].cursor then + --Not in cursor mode local old_pos = game.get_player(pindex).position - if stack.name == "locomotive" or stack.name == "cargo-wagon" or stack.name == "fluid-wagon" or stack.name == "artillery-wagon" then + if + stack.name == "locomotive" + or stack.name == "cargo-wagon" + or stack.name == "fluid-wagon" + or stack.name == "artillery-wagon" + then --Allow easy placement onto rails by simply offsetting to the faced direction. local rail_vehicle_offset = 2.5 position = fa_utils.offset_position(old_pos, players[pindex].player_direction, rail_vehicle_offset) @@ -69,18 +74,19 @@ function mod.build_item_in_hand(pindex, free_place_straight_rail) --Apply offsets according to building direction and player direction local width = stack.prototype.place_result.tile_width local height = stack.prototype.place_result.tile_height - local left_top = {x = math.floor(players[pindex].cursor_pos.x),y = math.floor(players[pindex].cursor_pos.y)} - local right_bottom = {x = left_top.x + width, y = left_top.y + height} + local left_top = + { x = math.floor(players[pindex].cursor_pos.x), y = math.floor(players[pindex].cursor_pos.y) } + local right_bottom = { x = left_top.x + width, y = left_top.y + height } local dir = players[pindex].building_direction turn_to_cursor_direction_cardinal(pindex) local p_dir = players[pindex].player_direction --Flip height and width if the object is rotated sideways if dir == dirs.east or dir == dirs.west then - --Note, diagonal cases are rounded to north/south cases + --Note, diagonal cases are rounded to north/south cases height = stack.prototype.place_result.tile_width width = stack.prototype.place_result.tile_height - right_bottom = {x = left_top.x + width, y = left_top.y + height} + right_bottom = { x = left_top.x + width, y = left_top.y + height } end --Apply offsets when facing west or north so that items can be placed in front of the character @@ -92,7 +98,7 @@ function mod.build_item_in_hand(pindex, free_place_straight_rail) right_bottom.y = (right_bottom.y - height + 1) end - position = {x = left_top.x + math.floor(width/2),y = left_top.y + math.floor(height/2)} + position = { x = left_top.x + math.floor(width / 2), y = left_top.y + math.floor(height / 2) } --In build lock mode and outside cursor mode, build from behind the player if players[pindex].build_lock and not players[pindex].cursor and stack.name ~= "rail" then @@ -103,38 +109,39 @@ function mod.build_item_in_hand(pindex, free_place_straight_rail) elseif p_dir == dirs.east or p_dir == dirs.west then size_offset = -width + 1 end - position = fa_utils.offset_position(position, players[pindex].player_direction, base_offset + size_offset) + position = + fa_utils.offset_position(position, players[pindex].player_direction, base_offset + size_offset) end end - else --Cursor mode offset: to the top left corner, according to building direction - local width = stack.prototype.place_result.tile_width - local height = stack.prototype.place_result.tile_height - local left_top = {x = math.floor(players[pindex].cursor_pos.x),y = math.floor(players[pindex].cursor_pos.y)} - local right_bottom = {x = left_top.x + width, y = left_top.y + height} - local dir = players[pindex].building_direction - turn_to_cursor_direction_cardinal(pindex) - local p_dir = players[pindex].player_direction - - --Flip height and width if the object is rotated - if dir == dirs.east or dir == dirs.west then--Note, does not cover diagonal directions for non-square objects. - local temp = height - height = width - width = temp - right_bottom = {x = left_top.x + width, y = left_top.y + height} - end + local width = stack.prototype.place_result.tile_width + local height = stack.prototype.place_result.tile_height + local left_top = { x = math.floor(players[pindex].cursor_pos.x), y = math.floor(players[pindex].cursor_pos.y) } + local right_bottom = { x = left_top.x + width, y = left_top.y + height } + local dir = players[pindex].building_direction + turn_to_cursor_direction_cardinal(pindex) + local p_dir = players[pindex].player_direction + + --Flip height and width if the object is rotated + if dir == dirs.east or dir == dirs.west then --Note, does not cover diagonal directions for non-square objects. + local temp = height + height = width + width = temp + right_bottom = { x = left_top.x + width, y = left_top.y + height } + end - position = {x = left_top.x + math.floor(width/2),y = left_top.y + math.floor(height/2)} + position = { x = left_top.x + math.floor(width / 2), y = left_top.y + math.floor(height / 2) } end if stack.name == "small-electric-pole" and players[pindex].build_lock == true then --Place a small electric pole in this position only if it is within 6.5 to 7.6 tiles of another small electric pole local surf = game.get_player(pindex).surface - local small_poles = surf.find_entities_filtered{position = position, radius = 7.6, name = "small-electric-pole"} + local small_poles = + surf.find_entities_filtered({ position = position, radius = 7.6, name = "small-electric-pole" }) local all_beyond_6_5 = true local any_connects = false local any_found = false - for i,pole in ipairs(small_poles) do + for i, pole in ipairs(small_poles) do any_found = true if util.distance(fa_utils.center_of_tile(position), pole.position) < 6.5 then all_beyond_6_5 = false @@ -143,20 +150,19 @@ function mod.build_item_in_hand(pindex, free_place_straight_rail) end end if not (all_beyond_6_5 and any_connects) then - game.get_player(pindex).play_sound{path = "Inventory-Move"} - if not any_found then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} - end + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) + if not any_found then game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) end return end - elseif stack.name == "medium-electric-pole" and players[pindex].build_lock == true then + elseif stack.name == "medium-electric-pole" and players[pindex].build_lock == true then --Place a medium electric pole in this position only if it is within 6.5 to 8 tiles of another medium electric pole local surf = game.get_player(pindex).surface - local med_poles = surf.find_entities_filtered{position = position, radius = 8, name = "medium-electric-pole"} + local med_poles = + surf.find_entities_filtered({ position = position, radius = 8, name = "medium-electric-pole" }) local all_beyond_6_5 = true local any_connects = false local any_found = false - for i,pole in ipairs(med_poles) do + for i, pole in ipairs(med_poles) do any_found = true if util.distance(fa_utils.center_of_tile(position), pole.position) < 6.5 then all_beyond_6_5 = false @@ -165,21 +171,19 @@ function mod.build_item_in_hand(pindex, free_place_straight_rail) end end if not (all_beyond_6_5 and any_connects) then - game.get_player(pindex).play_sound{path = "Inventory-Move"} - if not any_found then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} - end + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) + if not any_found then game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) end return end elseif stack.name == "big-electric-pole" and players[pindex].build_lock == true then --Place a big electric pole in this position only if it is within 29 to 30 tiles of another medium electric pole position = fa_utils.offset_position(position, players[pindex].player_direction, -1) local surf = game.get_player(pindex).surface - local big_poles = surf.find_entities_filtered{position = position, radius = 30, name = "big-electric-pole"} + local big_poles = surf.find_entities_filtered({ position = position, radius = 30, name = "big-electric-pole" }) local all_beyond_min = true local any_connects = false local any_found = false - for i,pole in ipairs(big_poles) do + for i, pole in ipairs(big_poles) do any_found = true if util.distance(position, pole.position) < 28.5 then all_beyond_min = false @@ -188,21 +192,19 @@ function mod.build_item_in_hand(pindex, free_place_straight_rail) end end if not (all_beyond_min and any_connects) then - game.get_player(pindex).play_sound{path = "Inventory-Move"} - if not any_found then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} - end + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) + if not any_found then game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) end return end elseif stack.name == "substation" and players[pindex].build_lock == true then --Place a substation in this position only if it is within 16 to 18 tiles of another medium electric pole position = fa_utils.offset_position(position, players[pindex].player_direction, -1) local surf = game.get_player(pindex).surface - local sub_poles = surf.find_entities_filtered{position = position, radius = 18.01, name = "substation"} + local sub_poles = surf.find_entities_filtered({ position = position, radius = 18.01, name = "substation" }) local all_beyond_min = true local any_connects = false local any_found = false - for i,pole in ipairs(sub_poles) do + for i, pole in ipairs(sub_poles) do any_found = true if util.distance(position, pole.position) < 17.01 then all_beyond_min = false @@ -211,10 +213,8 @@ function mod.build_item_in_hand(pindex, free_place_straight_rail) end end if not (all_beyond_min and any_connects) then - game.get_player(pindex).play_sound{path = "Inventory-Move"} - if not any_found then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} - end + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) + if not any_found then game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) end return end elseif placing_underground_belt and players[pindex].underground_connects == true then @@ -222,41 +222,72 @@ function mod.build_item_in_hand(pindex, free_place_straight_rail) players[pindex].building_direction = (players[pindex].building_direction + dirs.south) % (2 * dirs.south) end - --Clear build area obstacles - fa_mining_tools.clear_obstacles_in_rectangle(players[pindex].building_footprint_left_top, players[pindex].building_footprint_right_bottom, pindex) - - --Teleport player out of build area - mod.teleport_player_out_of_build_area(players[pindex].building_footprint_left_top, players[pindex].building_footprint_right_bottom, pindex) - - --Try to build it + --Clear build area obstacles + fa_mining_tools.clear_obstacles_in_rectangle( + players[pindex].building_footprint_left_top, + players[pindex].building_footprint_right_bottom, + pindex + ) + + --Teleport player out of build area + mod.teleport_player_out_of_build_area( + players[pindex].building_footprint_left_top, + players[pindex].building_footprint_right_bottom, + pindex + ) + + --Try to build it local build_successful = false local building = { position = position, --position = center_of_tile(position), direction = players[pindex].building_direction, - alt = false + alt = false, } --game.print("pos = " .. position.x .. " , " .. position.y,{volume_modifier=0}) if building.position ~= nil and game.get_player(pindex).can_build_from_cursor(building) then --Build it game.get_player(pindex).build_from_cursor(building) - schedule(2,"read_tile",pindex) + schedule(2, "read_tile", pindex) build_successful = true else --Report errors - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) if players[pindex].build_lock == false then --Explain build error local result = "Cannot place that there " - local build_area = {players[pindex].building_footprint_left_top, players[pindex].building_footprint_right_bottom} - local ents_in_area = p.surface.find_entities_filtered{area = build_area, invert = true, type = ENT_TYPES_YOU_CAN_BUILD_OVER} - local tiles_in_area = p.surface.find_tiles_filtered{area = build_area, invert = false, name = {"water", "deepwater", "water-green", "deepwater-green", "water-shallow", "water-mud", "water-wube"}} + local build_area = + { players[pindex].building_footprint_left_top, players[pindex].building_footprint_right_bottom } + local ents_in_area = p.surface.find_entities_filtered({ + area = build_area, + invert = true, + type = ENT_TYPES_YOU_CAN_BUILD_OVER, + }) + local tiles_in_area = p.surface.find_tiles_filtered({ + area = build_area, + invert = false, + name = { + "water", + "deepwater", + "water-green", + "deepwater-green", + "water-shallow", + "water-mud", + "water-wube", + }, + }) local obstacle_ent_name = nil local obstacle_tile_name = nil --Check for an entity in the way for i, area_ent in ipairs(ents_in_area) do - if area_ent.valid and area_ent.prototype.tile_width and area_ent.prototype.tile_width > 0 and area_ent.prototype.tile_height and area_ent.prototype.tile_height > 0 then - obstacle_ent_name = localising.get(area_ent,pindex) + if + area_ent.valid + and area_ent.prototype.tile_width + and area_ent.prototype.tile_width > 0 + and area_ent.prototype.tile_height + and area_ent.prototype.tile_height > 0 + then + obstacle_ent_name = localising.get(area_ent, pindex) end end @@ -269,35 +300,47 @@ function mod.build_item_in_hand(pindex, free_place_straight_rail) printout(result, pindex) end end - --Restore the original underground belt chute preview + --Restore the original underground belt chute preview if placing_underground_belt and players[pindex].underground_connects == true then players[pindex].building_direction = (players[pindex].building_direction + dirs.south) % (2 * dirs.south) local stack = game.get_player(pindex).cursor_stack - if stack and stack.valid_for_read and stack.valid and stack.prototype.place_result.type == "underground-belt" then - stack.set_stack({name = stack.name, count = stack.count}) + if + stack + and stack.valid_for_read + and stack.valid + and stack.prototype.place_result.type == "underground-belt" + then + stack.set_stack({ name = stack.name, count = stack.count }) end end - --Flip pipe-to-ground in hand - if build_successful and stack and stack.valid_for_read and stack.valid and stack.prototype.place_result ~= nil and stack.prototype.place_result.name == "pipe-to-ground" then + --Flip pipe-to-ground in hand + if + build_successful + and stack + and stack.valid_for_read + and stack.valid + and stack.prototype.place_result ~= nil + and stack.prototype.place_result.name == "pipe-to-ground" + then players[pindex].building_direction = fa_utils.rotate_180(players[pindex].building_direction) - game.get_player(pindex).play_sound{path = "Rotate-Hand-Sound"} + game.get_player(pindex).play_sound({ path = "Rotate-Hand-Sound" }) end elseif stack and stack.valid_for_read and stack.valid and stack.prototype.place_as_tile_result ~= nil then - --Tile placement - local p = game.get_player(pindex) - local t_size = players[pindex].cursor_size * 2 + 1 - local pos = players[pindex].cursor_pos--Center on the cursor in default - if players[pindex].cursor and players[pindex].preferences.tiles_placed_from_northwest_corner then - pos.x = pos.x - players[pindex].cursor_size - pos.y = pos.y - players[pindex].cursor_size - end - if p.can_build_from_cursor{position = pos, terrain_building_size = t_size} then - p.build_from_cursor{position = pos, terrain_building_size = t_size} - else - p.play_sound{path = "utility/cannot_build"} - end + --Tile placement + local p = game.get_player(pindex) + local t_size = players[pindex].cursor_size * 2 + 1 + local pos = players[pindex].cursor_pos --Center on the cursor in default + if players[pindex].cursor and players[pindex].preferences.tiles_placed_from_northwest_corner then + pos.x = pos.x - players[pindex].cursor_size + pos.y = pos.y - players[pindex].cursor_size + end + if p.can_build_from_cursor({ position = pos, terrain_building_size = t_size }) then + p.build_from_cursor({ position = pos, terrain_building_size = t_size }) + else + p.play_sound({ path = "utility/cannot_build" }) + end else - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) end --Update cursor highlight (end) @@ -324,9 +367,13 @@ function mod.build_offshore_pump_in_hand(pindex) for i1 = -10, 10 do for i2 = -10, 10 do for i3 = 0, 3 do - local position = {x = initial_position.x + i1, y = initial_position.y + i2} - if game.get_player(pindex).can_build_from_cursor{name = "offshore-pump", position = position, direction = i3 * 2} then - table.insert(players[pindex].pump.positions, {position = position, direction = i3*2}) + local position = { x = initial_position.x + i1, y = initial_position.y + i2 } + if + game + .get_player(pindex) + .can_build_from_cursor({ name = "offshore-pump", position = position, direction = i3 * 2 }) + then + table.insert(players[pindex].pump.positions, { position = position, direction = i3 * 2 }) end end end @@ -337,7 +384,12 @@ function mod.build_offshore_pump_in_hand(pindex) players[pindex].in_menu = true players[pindex].menu = "pump" players[pindex].move_queue = {} - printout("There are " .. #players[pindex].pump.positions .. " possibilities, scroll up and down, then select one to build, or press e to cancel.", pindex) + printout( + "There are " + .. #players[pindex].pump.positions + .. " possibilities, scroll up and down, then select one to build, or press e to cancel.", + pindex + ) table.sort(players[pindex].pump.positions, function(k1, k2) return util.distance(initial_position, k1.position) < util.distance(initial_position, k2.position) end) @@ -350,23 +402,18 @@ end --Reads the result of trying to rotate a building, which is a vanilla action. function mod.rotate_building_info_read(event, forward) pindex = event.player_index - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local mult = 1 - if forward == false then - mult = -1 - end + if forward == false then mult = -1 end if players[pindex].in_menu == false or players[pindex].menu == "blueprint_menu" then local ent = get_selected_ent(pindex) local stack = game.get_player(pindex).cursor_stack local build_dir = players[pindex].building_direction if stack and stack.valid_for_read and stack.valid and stack.prototype.place_result ~= nil then if stack.prototype.place_result.supports_direction then - --Update the assumed hand direction - if not(players[pindex].lag_building_direction) then - game.get_player(pindex).play_sound{path="Rotate-Hand-Sound"} + if not players[pindex].lag_building_direction then + game.get_player(pindex).play_sound({ path = "Rotate-Hand-Sound" }) build_dir = (build_dir + dirs.east * mult) % (2 * dirs.south) end @@ -387,15 +434,25 @@ function mod.rotate_building_info_read(event, forward) end end players[pindex].cursor_rotation_offset = rot_offset - if rot_offset ~= 0 then - build_dir = (build_dir - dirs.northeast * mult) % (2 * dirs.south) - end + if rot_offset ~= 0 then build_dir = (build_dir - dirs.northeast * mult) % (2 * dirs.south) end --Printout warning if rot_offset > 0 then - printout(fa_utils.direction_lookup(build_dir) .. " rail rotation warning: rotate a rail " .. rot_offset .. " times backward to re-align cursor, and then rotate a different item in hand to select the rotation you want before placing a rail", pindex) + printout( + fa_utils.direction_lookup(build_dir) + .. " rail rotation warning: rotate a rail " + .. rot_offset + .. " times backward to re-align cursor, and then rotate a different item in hand to select the rotation you want before placing a rail", + pindex + ) elseif rot_offset < 0 then - printout(fa_utils.direction_lookup(build_dir) .. " rail rotation warning: rotate a rail " .. -rot_offset .. " times forward to re-align cursor, and then rotate a different item in hand to select the rotation you want before placing a rail", pindex) + printout( + fa_utils.direction_lookup(build_dir) + .. " rail rotation warning: rotate a rail " + .. -rot_offset + .. " times forward to re-align cursor, and then rotate a different item in hand to select the rotation you want before placing a rail", + pindex + ) else printout(fa_utils.direction_lookup(build_dir) .. ", cursor rotation is aligned", pindex) end @@ -411,9 +468,10 @@ function mod.rotate_building_info_read(event, forward) printout(stack.name .. " never needs rotating.", pindex) end elseif stack.valid_for_read and stack.is_blueprint and stack.is_blueprint_setup() then - --Rotate blueprints: They are tracked separately, and we reset them to north when cursor stack changes - game.get_player(pindex).play_sound{path="Rotate-Hand-Sound"} - players[pindex].blueprint_hand_direction = (players[pindex].blueprint_hand_direction + dirs.east * mult) % (2 * dirs.south) + --Rotate blueprints: They are tracked separately, and we reset them to north when cursor stack changes + game.get_player(pindex).play_sound({ path = "Rotate-Hand-Sound" }) + players[pindex].blueprint_hand_direction = (players[pindex].blueprint_hand_direction + dirs.east * mult) + % (2 * dirs.south) printout(fa_utils.direction_lookup(players[pindex].blueprint_hand_direction), pindex) --Flip the saved bp width and height @@ -427,16 +485,22 @@ function mod.rotate_building_info_read(event, forward) game.get_player(pindex).selected = ent if ent.supports_direction then - --Assuming that the vanilla rotate event will now rotate the ent local new_dir = (ent.direction + dirs.east * mult) % (2 * dirs.south) - if ent.name == "steam-engine" or ent.name == "steam-turbine" or ent.name == "rail" or ent.name == "straight-rail" or ent.name == "curved-rail" or ent.name == "character" then + if + ent.name == "steam-engine" + or ent.name == "steam-turbine" + or ent.name == "rail" + or ent.name == "straight-rail" + or ent.name == "curved-rail" + or ent.name == "character" + then --Exception: These ents do not rotate new_dir = (new_dir - dirs.east * mult) % (2 * dirs.south) elseif (ent.tile_width ~= ent.tile_height and ent.supports_direction) or ent.type == "underground-belt" then --Exceptions: None-square ents rotate 2x , while underground belts simply flip instead - --Examples, boiler, pump, flamethrower, heat exchanger, + --Examples, boiler, pump, flamethrower, heat exchanger, new_dir = (new_dir + dirs.east * mult) % (2 * dirs.south) end @@ -453,17 +517,15 @@ function mod.rotate_building_info_read(event, forward) end --Does everything to handle the nudging feature, taking the keypress event and the nudge direction as the input. Nothing happens if an entity cannot be selected. -function mod.nudge_key(direction,event) +function mod.nudge_key(direction, event) local pindex = event.player_index local p = game.get_player(pindex) - if not check_for_player(pindex) or players[pindex].menu == "prompt" then - return - end + if not check_for_player(pindex) or players[pindex].menu == "prompt" then return end local ent = get_selected_ent(pindex) if ent and ent.valid then if ent.force == game.get_player(pindex).force then local old_pos = ent.position - local new_pos = fa_utils.offset_position(ent.position,direction,1) + local new_pos = fa_utils.offset_position(ent.position, direction, 1) local temporary_teleported = false local actually_teleported = false @@ -473,47 +535,70 @@ function mod.nudge_key(direction,event) local width = ent.tile_width local height = ent.tile_height if ent.direction == dirs.east or ent.direction == dirs.west then - --Flip width and height. Note: diagonal cases are rounded to north/south cases + --Flip width and height. Note: diagonal cases are rounded to north/south cases height = ent.tile_width width = ent.tile_height end - left_top = {x = math.floor(ent.position.x - math.floor(width/2)), y = math.floor(ent.position.y - math.floor(height/2))} - left_top = fa_utils.offset_position(left_top,direction,1) - right_bottom = {x = math.ceil(left_top.x + width), y = math.ceil(left_top.y + height)} + left_top = { + x = math.floor(ent.position.x - math.floor(width / 2)), + y = math.floor(ent.position.y - math.floor(height / 2)), + } + left_top = fa_utils.offset_position(left_top, direction, 1) + right_bottom = { x = math.ceil(left_top.x + width), y = math.ceil(left_top.y + height) } fa_mining_tools.clear_obstacles_in_rectangle(left_top, right_bottom, pindex) --First teleport the ent to 0,0 temporarily - temporary_teleported = ent.teleport({0,0}) + temporary_teleported = ent.teleport({ 0, 0 }) if not temporary_teleported then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} - printout({"access.failed-to-nudge"}, pindex) + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) + printout({ "access.failed-to-nudge" }, pindex) return end --Now check if the ent can be placed at its new location, and proceed or revert accordingly local check_name = ent.name - if check_name == "entity-ghost" then - check_name = ent.ghost_name - end - if ent.surface.can_place_entity{name = check_name, position = new_pos, direction = ent.direction} then + if check_name == "entity-ghost" then check_name = ent.ghost_name end + if ent.surface.can_place_entity({ name = check_name, position = new_pos, direction = ent.direction }) then actually_teleported = ent.teleport(new_pos) else --Cannot build in new location, so send it back actually_teleported = ent.teleport(old_pos) - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) --Explain build error local result = "Cannot nudge" - local build_area = {left_top, right_bottom} - local ents_in_area = p.surface.find_entities_filtered{area = build_area, invert = true, type = ENT_TYPES_YOU_CAN_BUILD_OVER} - local tiles_in_area = p.surface.find_tiles_filtered{area = build_area, invert = false, name = {"water", "deepwater", "water-green", "deepwater-green", "water-shallow", "water-mud", "water-wube"}} + local build_area = { left_top, right_bottom } + local ents_in_area = p.surface.find_entities_filtered({ + area = build_area, + invert = true, + type = ENT_TYPES_YOU_CAN_BUILD_OVER, + }) + local tiles_in_area = p.surface.find_tiles_filtered({ + area = build_area, + invert = false, + name = { + "water", + "deepwater", + "water-green", + "deepwater-green", + "water-shallow", + "water-mud", + "water-wube", + }, + }) local obstacle_ent_name = nil local obstacle_tile_name = nil --Check for an entity in the way for i, area_ent in ipairs(ents_in_area) do - if area_ent.valid and area_ent.prototype.tile_width and area_ent.prototype.tile_width > 0 and area_ent.prototype.tile_height and area_ent.prototype.tile_height > 0 then - obstacle_ent_name = localising.get(area_ent,pindex) + if + area_ent.valid + and area_ent.prototype.tile_width + and area_ent.prototype.tile_width > 0 + and area_ent.prototype.tile_height + and area_ent.prototype.tile_height > 0 + then + obstacle_ent_name = localising.get(area_ent, pindex) end end @@ -528,20 +613,20 @@ function mod.nudge_key(direction,event) end if not actually_teleported then --Failed to teleport - printout({"access.failed-to-nudge"}, pindex) + printout({ "access.failed-to-nudge" }, pindex) return else --Successfully teleported and so nudged - printout({"access.nudged-one-direction",{"access.direction",direction}}, pindex) + printout({ "access.nudged-one-direction", { "access.direction", direction } }, pindex) if players[pindex].cursor then - players[pindex].cursor_pos = fa_utils.offset_position(players[pindex].cursor_pos,direction,1) + players[pindex].cursor_pos = fa_utils.offset_position(players[pindex].cursor_pos, direction, 1) fa_graphics.draw_cursor_highlight(pindex, ent, "train-visualization") fa_graphics.sync_build_cursor_graphics(pindex) end if ent.type == "electric-pole" then -- laterdo **bugfix when nudged electric poles have extra wire reach, cut wires -- if ent.clone{position = new_pos, surface = ent.surface, force = ent.force, create_build_effect_smoke = false} == true then - -- ent.destroy{} + -- ent.destroy{} -- end end end @@ -552,7 +637,6 @@ function mod.nudge_key(direction,event) else printout("Nudged nothing.", pindex) end - end --Returns a list of positions for this entity where it has its heat pipe connections. @@ -560,20 +644,20 @@ function mod.get_heat_connection_positions(ent_name, ent_position, ent_direction local pos = ent_position local positions = {} if ent_name == "heat-pipe" then - table.insert(positions, {x = pos.x, y = pos.y}) + table.insert(positions, { x = pos.x, y = pos.y }) elseif ent_name == "heat-exchanger" then table.insert(positions, fa_utils.offset_position(pos, fa_utils.rotate_180(ent_direction), 0.5)) elseif ent_name == "nuclear-reactor" then - table.insert(positions, {x = pos.x-2, y = pos.y-2}) - table.insert(positions, {x = pos.x-0, y = pos.y-2}) - table.insert(positions, {x = pos.x+2, y = pos.y-2}) + table.insert(positions, { x = pos.x - 2, y = pos.y - 2 }) + table.insert(positions, { x = pos.x - 0, y = pos.y - 2 }) + table.insert(positions, { x = pos.x + 2, y = pos.y - 2 }) - table.insert(positions, {x = pos.x-2, y = pos.y-0}) - table.insert(positions, {x = pos.x+2, y = pos.y-0}) + table.insert(positions, { x = pos.x - 2, y = pos.y - 0 }) + table.insert(positions, { x = pos.x + 2, y = pos.y - 0 }) - table.insert(positions, {x = pos.x-2, y = pos.y+2}) - table.insert(positions, {x = pos.x-0, y = pos.y+2}) - table.insert(positions, {x = pos.x+2, y = pos.y+2}) + table.insert(positions, { x = pos.x - 2, y = pos.y + 2 }) + table.insert(positions, { x = pos.x - 0, y = pos.y + 2 }) + table.insert(positions, { x = pos.x + 2, y = pos.y + 2 }) end return positions end @@ -583,111 +667,198 @@ function mod.get_heat_connection_target_positions(ent_name, ent_position, ent_di local pos = ent_position local positions = {} if ent_name == "heat-pipe" then - table.insert(positions, {x = pos.x-1, y = pos.y-0}) - table.insert(positions, {x = pos.x+1, y = pos.y-0}) - table.insert(positions, {x = pos.x-0, y = pos.y-1}) - table.insert(positions, {x = pos.x-0, y = pos.y+1}) + table.insert(positions, { x = pos.x - 1, y = pos.y - 0 }) + table.insert(positions, { x = pos.x + 1, y = pos.y - 0 }) + table.insert(positions, { x = pos.x - 0, y = pos.y - 1 }) + table.insert(positions, { x = pos.x - 0, y = pos.y + 1 }) elseif ent_name == "heat-exchanger" then table.insert(positions, fa_utils.offset_position(pos, fa_utils.rotate_180(ent_direction), 1.5)) elseif ent_name == "nuclear-reactor" then - table.insert(positions, {x = pos.x-2, y = pos.y-3}) - table.insert(positions, {x = pos.x-0, y = pos.y-3}) - table.insert(positions, {x = pos.x+2, y = pos.y-3}) + table.insert(positions, { x = pos.x - 2, y = pos.y - 3 }) + table.insert(positions, { x = pos.x - 0, y = pos.y - 3 }) + table.insert(positions, { x = pos.x + 2, y = pos.y - 3 }) - table.insert(positions, {x = pos.x-3, y = pos.y-2}) - table.insert(positions, {x = pos.x+3, y = pos.y-2}) + table.insert(positions, { x = pos.x - 3, y = pos.y - 2 }) + table.insert(positions, { x = pos.x + 3, y = pos.y - 2 }) - table.insert(positions, {x = pos.x-3, y = pos.y-0}) - table.insert(positions, {x = pos.x+3, y = pos.y-0}) + table.insert(positions, { x = pos.x - 3, y = pos.y - 0 }) + table.insert(positions, { x = pos.x + 3, y = pos.y - 0 }) - table.insert(positions, {x = pos.x-3, y = pos.y+2}) - table.insert(positions, {x = pos.x+3, y = pos.y+2}) + table.insert(positions, { x = pos.x - 3, y = pos.y + 2 }) + table.insert(positions, { x = pos.x + 3, y = pos.y + 2 }) - table.insert(positions, {x = pos.x-2, y = pos.y+3}) - table.insert(positions, {x = pos.x-0, y = pos.y+3}) - table.insert(positions, {x = pos.x+2, y = pos.y+3}) + table.insert(positions, { x = pos.x - 2, y = pos.y + 3 }) + table.insert(positions, { x = pos.x - 0, y = pos.y + 3 }) + table.insert(positions, { x = pos.x + 2, y = pos.y + 3 }) end return positions end --Returns an info string about trying to build the entity in hand. The info type depends on the entity. Note: Limited usefulness for entities with sizes greater than 1 by 1. function mod.build_preview_checks_info(stack, pindex) - if stack == nil or not stack.valid_for_read or not stack.valid then - return "invalid stack" - end + if stack == nil or not stack.valid_for_read or not stack.valid then return "invalid stack" end local p = game.get_player(pindex) local surf = game.get_player(pindex).surface local pos = table.deepcopy(players[pindex].cursor_pos) local result = "" local build_dir = players[pindex].building_direction local ent_p = stack.prototype.place_result --it is an entity prototype! - if ent_p == nil or not ent_p.valid then - return "invalid entity" - end + if ent_p == nil or not ent_p.valid then return "invalid entity" end --Notify before all else if surface/player cannot place this entity. laterdo extend this valid placement check by copying over build offset stuff - if ent_p.tile_width <= 1 and ent_p.tile_height <= 1 and not surf.can_place_entity{name = stack.name, position = pos, direction = build_dir} then + if + ent_p.tile_width <= 1 + and ent_p.tile_height <= 1 + and not surf.can_place_entity({ name = stack.name, position = pos, direction = build_dir }) + then return " cannot place this here " end --For belt types, check if it would form a corner or junction here. **Laterdo include underground exits and possibly ghost belts. if ent_p.type == "transport-belt" then - local ents_north = p.surface.find_entities_filtered{position = {x = pos.x+0 ,y = pos.y-1}, type = {"transport-belt", "underground-belt"}} - local ents_south = p.surface.find_entities_filtered{position = {x = pos.x+0 ,y = pos.y+1}, type = {"transport-belt", "underground-belt"}} - local ents_east = p.surface.find_entities_filtered{position = {x = pos.x+1 ,y = pos.y+0}, type = {"transport-belt", "underground-belt"}} - local ents_west = p.surface.find_entities_filtered{position = {x = pos.x-1 ,y = pos.y+0}, type = {"transport-belt", "underground-belt"}} - - rendering.draw_circle{color = {1, 0.0, 0.5},radius = 0.1,width = 2,target = {x = pos.x+0 ,y = pos.y-1}, surface = p.surface, time_to_live = 30} - rendering.draw_circle{color = {1, 0.0, 0.5},radius = 0.1,width = 2,target = {x = pos.x+0 ,y = pos.y+1}, surface = p.surface, time_to_live = 30} - rendering.draw_circle{color = {1, 0.0, 0.5},radius = 0.1,width = 2,target = {x = pos.x-1 ,y = pos.y-0}, surface = p.surface, time_to_live = 30} - rendering.draw_circle{color = {1, 0.0, 0.5},radius = 0.1,width = 2,target = {x = pos.x+1 ,y = pos.y-0}, surface = p.surface, time_to_live = 30} + local ents_north = p.surface.find_entities_filtered({ + position = { x = pos.x + 0, y = pos.y - 1 }, + type = { "transport-belt", "underground-belt" }, + }) + local ents_south = p.surface.find_entities_filtered({ + position = { x = pos.x + 0, y = pos.y + 1 }, + type = { "transport-belt", "underground-belt" }, + }) + local ents_east = p.surface.find_entities_filtered({ + position = { x = pos.x + 1, y = pos.y + 0 }, + type = { "transport-belt", "underground-belt" }, + }) + local ents_west = p.surface.find_entities_filtered({ + position = { x = pos.x - 1, y = pos.y + 0 }, + type = { "transport-belt", "underground-belt" }, + }) + + rendering.draw_circle({ + color = { 1, 0.0, 0.5 }, + radius = 0.1, + width = 2, + target = { x = pos.x + 0, y = pos.y - 1 }, + surface = p.surface, + time_to_live = 30, + }) + rendering.draw_circle({ + color = { 1, 0.0, 0.5 }, + radius = 0.1, + width = 2, + target = { x = pos.x + 0, y = pos.y + 1 }, + surface = p.surface, + time_to_live = 30, + }) + rendering.draw_circle({ + color = { 1, 0.0, 0.5 }, + radius = 0.1, + width = 2, + target = { x = pos.x - 1, y = pos.y - 0 }, + surface = p.surface, + time_to_live = 30, + }) + rendering.draw_circle({ + color = { 1, 0.0, 0.5 }, + radius = 0.1, + width = 2, + target = { x = pos.x + 1, y = pos.y - 0 }, + surface = p.surface, + time_to_live = 30, + }) if #ents_north > 0 or #ents_south > 0 or #ents_east > 0 or #ents_west > 0 then local sideload_count = 0 local backload_count = 0 - local outload_count = 0 + local outload_count = 0 local this_dir = build_dir local outload_dir = nil local outload_is_corner = false - --Find the outloading belt and its direction and shape, if any + --Find the outloading belt and its direction and shape, if any if this_dir == dirs.north and ents_north[1] ~= nil and ents_north[1].valid then - rendering.draw_circle{color = {0.5, 0.5, 1},radius = 0.3,width = 2,target = ents_north[1].position,surface = ents_north[1].surface,time_to_live = 30} + rendering.draw_circle({ + color = { 0.5, 0.5, 1 }, + radius = 0.3, + width = 2, + target = ents_north[1].position, + surface = ents_north[1].surface, + time_to_live = 30, + }) outload_dir = ents_north[1].direction outload_count = 1 local outload_inputs = ents_north[1].belt_neighbours["inputs"] - if (outload_inputs == nil or #outload_inputs == 0) and (this_dir == fa_utils.rotate_90(outload_dir) or this_dir == fa_utils.rotate_270(outload_dir)) then + if + (outload_inputs == nil or #outload_inputs == 0) + and (this_dir == fa_utils.rotate_90(outload_dir) or this_dir == fa_utils.rotate_270(outload_dir)) + then outload_is_corner = true end elseif this_dir == dirs.east and ents_east[1] ~= nil and ents_east[1].valid then - rendering.draw_circle{color = {0.5, 0.5, 1},radius = 0.3,width = 2,target = ents_east[1].position,surface = ents_east[1].surface,time_to_live = 30} + rendering.draw_circle({ + color = { 0.5, 0.5, 1 }, + radius = 0.3, + width = 2, + target = ents_east[1].position, + surface = ents_east[1].surface, + time_to_live = 30, + }) outload_dir = ents_east[1].direction outload_count = 1 local outload_inputs = ents_east[1].belt_neighbours["inputs"] - if (outload_inputs == nil or #outload_inputs == 0) and (this_dir == fa_utils.rotate_90(outload_dir) or this_dir == fa_utils.rotate_270(outload_dir)) then + if + (outload_inputs == nil or #outload_inputs == 0) + and (this_dir == fa_utils.rotate_90(outload_dir) or this_dir == fa_utils.rotate_270(outload_dir)) + then outload_is_corner = true end elseif this_dir == dirs.south and ents_south[1] ~= nil and ents_south[1].valid then - rendering.draw_circle{color = {0.5, 0.5, 1},radius = 0.3,width = 2,target = ents_south[1].position,surface = ents_south[1].surface,time_to_live = 30} + rendering.draw_circle({ + color = { 0.5, 0.5, 1 }, + radius = 0.3, + width = 2, + target = ents_south[1].position, + surface = ents_south[1].surface, + time_to_live = 30, + }) outload_dir = ents_south[1].direction outload_count = 1 local outload_inputs = ents_south[1].belt_neighbours["inputs"] - if (outload_inputs == nil or #outload_inputs == 0) and (this_dir == fa_utils.rotate_90(outload_dir) or this_dir == fa_utils.rotate_270(outload_dir)) then + if + (outload_inputs == nil or #outload_inputs == 0) + and (this_dir == fa_utils.rotate_90(outload_dir) or this_dir == fa_utils.rotate_270(outload_dir)) + then outload_is_corner = true end elseif this_dir == dirs.west and ents_west[1] ~= nil and ents_west[1].valid then - rendering.draw_circle{color = {0.5, 0.5, 1},radius = 0.3,width = 2,target = ents_west[1].position,surface = ents_west[1].surface,time_to_live = 30} + rendering.draw_circle({ + color = { 0.5, 0.5, 1 }, + radius = 0.3, + width = 2, + target = ents_west[1].position, + surface = ents_west[1].surface, + time_to_live = 30, + }) outload_dir = ents_west[1].direction outload_count = 1 local outload_inputs = ents_west[1].belt_neighbours["inputs"] - if (outload_inputs == nil or #outload_inputs == 0) and (this_dir == fa_utils.rotate_90(outload_dir) or this_dir == fa_utils.rotate_270(outload_dir)) then + if + (outload_inputs == nil or #outload_inputs == 0) + and (this_dir == fa_utils.rotate_90(outload_dir) or this_dir == fa_utils.rotate_270(outload_dir)) + then outload_is_corner = true end end --Find the backloading and sideloading belts, if any if ents_north[1] ~= nil and ents_north[1].valid and ents_north[1].direction == dirs.south then - rendering.draw_circle{color = {1, 1, 0.2},radius = 0.2,width = 2,target = ents_north[1].position,surface = ents_north[1].surface,time_to_live = 30} + rendering.draw_circle({ + color = { 1, 1, 0.2 }, + radius = 0.2, + width = 2, + target = ents_north[1].position, + surface = ents_north[1].surface, + time_to_live = 30, + }) if this_dir == dirs.east or this_dir == dirs.west then sideload_count = sideload_count + 1 elseif this_dir == dirs.south then @@ -696,7 +867,14 @@ function mod.build_preview_checks_info(stack, pindex) end if ents_south[1] ~= nil and ents_south[1].valid and ents_south[1].direction == dirs.north then - rendering.draw_circle{color = {1, 1, 0.4},radius = 0.2,width = 2,target = ents_south[1].position,surface = ents_south[1].surface,time_to_live = 30} + rendering.draw_circle({ + color = { 1, 1, 0.4 }, + radius = 0.2, + width = 2, + target = ents_south[1].position, + surface = ents_south[1].surface, + time_to_live = 30, + }) if this_dir == dirs.east or this_dir == dirs.west then sideload_count = sideload_count + 1 elseif this_dir == dirs.north then @@ -705,7 +883,14 @@ function mod.build_preview_checks_info(stack, pindex) end if ents_east[1] ~= nil and ents_east[1].valid and ents_east[1].direction == dirs.west then - rendering.draw_circle{color = {1, 1, 0.6},radius = 0.2,width = 2,target = ents_east[1].position,surface = ents_east[1].surface,time_to_live = 30} + rendering.draw_circle({ + color = { 1, 1, 0.6 }, + radius = 0.2, + width = 2, + target = ents_east[1].position, + surface = ents_east[1].surface, + time_to_live = 30, + }) if this_dir == dirs.north or this_dir == dirs.south then sideload_count = sideload_count + 1 elseif this_dir == dirs.west then @@ -714,7 +899,14 @@ function mod.build_preview_checks_info(stack, pindex) end if ents_west[1] ~= nil and ents_west[1].valid and ents_west[1].direction == dirs.east then - rendering.draw_circle{color = {1, 1, 0.8},radius = 0.2,width = 2,target = ents_west[1].position,surface = ents_west[1].surface,time_to_live = 30} + rendering.draw_circle({ + color = { 1, 1, 0.8 }, + radius = 0.2, + width = 2, + target = ents_west[1].position, + surface = ents_west[1].surface, + time_to_live = 30, + }) if this_dir == dirs.north or this_dir == dirs.south then sideload_count = sideload_count + 1 elseif this_dir == dirs.east then @@ -723,125 +915,208 @@ function mod.build_preview_checks_info(stack, pindex) end --Determine expected junction info - if sideload_count + backload_count + outload_count > 0 then--Skips "unit" because it is obvious + if sideload_count + backload_count + outload_count > 0 then --Skips "unit" because it is obvious local say_middle = true - result = ", forms belt " .. fa_belts.transport_belt_junction_info(sideload_count, backload_count, outload_count, this_dir, outload_dir, say_middle, outload_is_corner) + result = ", forms belt " + .. fa_belts.transport_belt_junction_info( + sideload_count, + backload_count, + outload_count, + this_dir, + outload_dir, + say_middle, + outload_is_corner + ) end end end --For underground belts, state the potential neighbor: any neighborless matching underground of the same name and same/opposite direction, and along the correct axis - if ent_p.type == "underground-belt" then + if ent_p.type == "underground-belt" then local connected = false local check_dist = 5 - if stack.name == "fast-underground-belt" then - check_dist = 7 - elseif stack.name == "express-underground-belt" then - check_dist = 9 - end - local candidates = game.get_player(pindex).surface.find_entities_filtered{ name = stack.name, position = pos, radius = check_dist, direction = build_dir } - if #candidates > 0 then - for i,cand in ipairs(candidates) do - rendering.draw_circle{color = {1, 1, 0},radius = 0.5,width = 3,target = cand.position,surface = cand.surface,time_to_live = 60} + if stack.name == "fast-underground-belt" then + check_dist = 7 + elseif stack.name == "express-underground-belt" then + check_dist = 9 + end + local candidates = game + .get_player(pindex).surface + .find_entities_filtered({ name = stack.name, position = pos, radius = check_dist, direction = build_dir }) + if #candidates > 0 then + for i, cand in ipairs(candidates) do + rendering.draw_circle({ + color = { 1, 1, 0 }, + radius = 0.5, + width = 3, + target = cand.position, + surface = cand.surface, + time_to_live = 60, + }) local dist_x = cand.position.x - pos.x local dist_y = cand.position.y - pos.y - if cand.direction == build_dir and cand.neighbours == nil and cand.belt_to_ground_type == "input" - and (fa_utils.get_direction_biased(cand.position,pos) == fa_utils.rotate_180(build_dir)) and (dist_x == 0 or dist_y == 0) then - rendering.draw_circle{color = {0, 1, 0},radius = 1.0,width = 3,target = cand.position,surface = cand.surface,time_to_live = 60} - result = result .. " connects " .. fa_utils.direction_lookup(build_dir) .. " with " .. math.floor(util.distance(cand.position,pos)) - 1 .. " tiles underground, " + if + cand.direction == build_dir + and cand.neighbours == nil + and cand.belt_to_ground_type == "input" + and (fa_utils.get_direction_biased(cand.position, pos) == fa_utils.rotate_180(build_dir)) + and (dist_x == 0 or dist_y == 0) + then + rendering.draw_circle({ + color = { 0, 1, 0 }, + radius = 1.0, + width = 3, + target = cand.position, + surface = cand.surface, + time_to_live = 60, + }) + result = result + .. " connects " + .. fa_utils.direction_lookup(build_dir) + .. " with " + .. math.floor(util.distance(cand.position, pos)) - 1 + .. " tiles underground, " connected = true players[pindex].underground_connects = true - end + end end - end + end if not connected then result = result .. " not connected " players[pindex].underground_connects = false end end - --For pipes to ground, state when connected + --For pipes to ground, state when connected if stack.name == "pipe-to-ground" then local connected = false local check_dist = 10 local closest_dist = 11 local closest_cand = nil - local candidates = game.get_player(pindex).surface.find_entities_filtered{ name = stack.name, position = pos, radius = check_dist, direction = fa_utils.rotate_180(build_dir) } - if #candidates > 0 then - for i,cand in ipairs(candidates) do - rendering.draw_circle{color = {1, 1, 0},radius = 0.5,width = 3,target = cand.position,surface = cand.surface,time_to_live = 60} + local candidates = + game + .get_player(pindex).surface + .find_entities_filtered({ name = stack.name, position = pos, radius = check_dist, direction = fa_utils.rotate_180(build_dir) }) + if #candidates > 0 then + for i, cand in ipairs(candidates) do + rendering.draw_circle({ + color = { 1, 1, 0 }, + radius = 0.5, + width = 3, + target = cand.position, + surface = cand.surface, + time_to_live = 60, + }) local dist_x = cand.position.x - pos.x local dist_y = cand.position.y - pos.y - if cand.direction == fa_utils.rotate_180(build_dir) - and (fa_utils.get_direction_biased(pos,cand.position) == build_dir) and (dist_x == 0 or dist_y == 0) then - rendering.draw_circle{color = {0, 1, 0},radius = 1.0,width = 3,target = cand.position,surface = cand.surface,time_to_live = 60} + if + cand.direction == fa_utils.rotate_180(build_dir) + and (fa_utils.get_direction_biased(pos, cand.position) == build_dir) + and (dist_x == 0 or dist_y == 0) + then + rendering.draw_circle({ + color = { 0, 1, 0 }, + radius = 1.0, + width = 3, + target = cand.position, + surface = cand.surface, + time_to_live = 60, + }) connected = true --Check if closest cand - local cand_dist = util.distance(cand.position,pos) + local cand_dist = util.distance(cand.position, pos) if cand_dist <= closest_dist then closest_dist = cand_dist closest_cand = cand end - end + end end --Report the closest candidate (therefore the correct one) if closest_cand ~= nil then - result = result .. " connects " .. fa_utils.direction_lookup(fa_utils.rotate_180(build_dir)) .. " with " .. math.floor(util.distance(closest_cand.position,pos)) - 1 .. " tiles underground, " + result = result + .. " connects " + .. fa_utils.direction_lookup(fa_utils.rotate_180(build_dir)) + .. " with " + .. math.floor(util.distance(closest_cand.position, pos)) - 1 + .. " tiles underground, " end - end - if not connected then - result = result .. " not connected underground, " end + if not connected then result = result .. " not connected underground, " end end --For pipes, read the fluids in fluidboxes of surrounding entities, if any. Also warn if there are multiple fluids, hence a mixing error. Pipe preview if stack.name == "pipe" then - rendering.draw_circle{color = {1, 0.0, 0.5},radius = 0.1,width = 2,target = {x = pos.x+0 ,y = pos.y-1}, surface = p.surface, time_to_live = 30} - rendering.draw_circle{color = {1, 0.0, 0.5},radius = 0.1,width = 2,target = {x = pos.x+0 ,y = pos.y+1}, surface = p.surface, time_to_live = 30} - rendering.draw_circle{color = {1, 0.0, 0.5},radius = 0.1,width = 2,target = {x = pos.x-1 ,y = pos.y-0}, surface = p.surface, time_to_live = 30} - rendering.draw_circle{color = {1, 0.0, 0.5},radius = 0.1,width = 2,target = {x = pos.x+1 ,y = pos.y-0}, surface = p.surface, time_to_live = 30} - local ents_north = p.surface.find_entities_filtered{position = {x = pos.x+0, y = pos.y-1} } - local ents_south = p.surface.find_entities_filtered{position = {x = pos.x+0, y = pos.y+1} } - local ents_east = p.surface.find_entities_filtered{position = {x = pos.x+1, y = pos.y+0} } - local ents_west = p.surface.find_entities_filtered{position = {x = pos.x-1, y = pos.y+0} } + rendering.draw_circle({ + color = { 1, 0.0, 0.5 }, + radius = 0.1, + width = 2, + target = { x = pos.x + 0, y = pos.y - 1 }, + surface = p.surface, + time_to_live = 30, + }) + rendering.draw_circle({ + color = { 1, 0.0, 0.5 }, + radius = 0.1, + width = 2, + target = { x = pos.x + 0, y = pos.y + 1 }, + surface = p.surface, + time_to_live = 30, + }) + rendering.draw_circle({ + color = { 1, 0.0, 0.5 }, + radius = 0.1, + width = 2, + target = { x = pos.x - 1, y = pos.y - 0 }, + surface = p.surface, + time_to_live = 30, + }) + rendering.draw_circle({ + color = { 1, 0.0, 0.5 }, + radius = 0.1, + width = 2, + target = { x = pos.x + 1, y = pos.y - 0 }, + surface = p.surface, + time_to_live = 30, + }) + local ents_north = p.surface.find_entities_filtered({ position = { x = pos.x + 0, y = pos.y - 1 } }) + local ents_south = p.surface.find_entities_filtered({ position = { x = pos.x + 0, y = pos.y + 1 } }) + local ents_east = p.surface.find_entities_filtered({ position = { x = pos.x + 1, y = pos.y + 0 } }) + local ents_west = p.surface.find_entities_filtered({ position = { x = pos.x - 1, y = pos.y + 0 } }) local relevant_fluid_north = nil - local relevant_fluid_east = nil + local relevant_fluid_east = nil local relevant_fluid_south = nil - local relevant_fluid_west = nil + local relevant_fluid_west = nil local box = nil local dir_from_pos = nil local north_ent = nil for i, ent_cand in ipairs(ents_north) do - if ent_cand.valid and ent_cand.fluidbox ~= nil then - north_ent = ent_cand - end + if ent_cand.valid and ent_cand.fluidbox ~= nil then north_ent = ent_cand end end local south_ent = nil for i, ent_cand in ipairs(ents_south) do - if ent_cand.valid and ent_cand.fluidbox ~= nil then - south_ent = ent_cand - end + if ent_cand.valid and ent_cand.fluidbox ~= nil then south_ent = ent_cand end end local east_ent = nil for i, ent_cand in ipairs(ents_east) do - if ent_cand.valid and ent_cand.fluidbox ~= nil then - east_ent = ent_cand - end + if ent_cand.valid and ent_cand.fluidbox ~= nil then east_ent = ent_cand end end local west_ent = nil for i, ent_cand in ipairs(ents_west) do - if ent_cand.valid and ent_cand.fluidbox ~= nil then - west_ent = ent_cand - end + if ent_cand.valid and ent_cand.fluidbox ~= nil then west_ent = ent_cand end end box, relevant_fluid_north, dir_from_pos = mod.get_relevant_fluidbox_and_fluid_name(north_ent, pos, dirs.north) box, relevant_fluid_south, dir_from_pos = mod.get_relevant_fluidbox_and_fluid_name(south_ent, pos, dirs.south) - box, relevant_fluid_east, dir_from_pos = mod.get_relevant_fluidbox_and_fluid_name(east_ent, pos, dirs.east) - box, relevant_fluid_west, dir_from_pos = mod.get_relevant_fluidbox_and_fluid_name(west_ent, pos, dirs.west) - - --Prepare result string - if relevant_fluid_north ~= nil or relevant_fluid_east ~= nil or relevant_fluid_south ~= nil or relevant_fluid_west ~= nil then + box, relevant_fluid_east, dir_from_pos = mod.get_relevant_fluidbox_and_fluid_name(east_ent, pos, dirs.east) + box, relevant_fluid_west, dir_from_pos = mod.get_relevant_fluidbox_and_fluid_name(west_ent, pos, dirs.west) + + --Prepare result string + if + relevant_fluid_north ~= nil + or relevant_fluid_east ~= nil + or relevant_fluid_south ~= nil + or relevant_fluid_west ~= nil + then local count = 0 result = result .. ", pipe can connect to " @@ -850,7 +1125,7 @@ function mod.build_preview_checks_info(stack, pindex) count = count + 1 end if relevant_fluid_east ~= nil then - result = result .. relevant_fluid_east .. " at east, " + result = result .. relevant_fluid_east .. " at east, " count = count + 1 end if relevant_fluid_south ~= nil then @@ -858,48 +1133,64 @@ function mod.build_preview_checks_info(stack, pindex) count = count + 1 end if relevant_fluid_west ~= nil then - result = result .. relevant_fluid_west .. " at west, " + result = result .. relevant_fluid_west .. " at west, " count = count + 1 end --Check which fluids are empty or equal (and thus not mixing invalidly). "Empty" counts too because sometimes a pipe itself is empty but it is still part of a network... - if relevant_fluid_north ~= nil and (relevant_fluid_north == relevant_fluid_south or relevant_fluid_north == relevant_fluid_east or relevant_fluid_north == relevant_fluid_west) then - count = count - 1 - end - if relevant_fluid_east ~= nil and (relevant_fluid_east == relevant_fluid_south or relevant_fluid_east == relevant_fluid_west) then + if + relevant_fluid_north ~= nil + and ( + relevant_fluid_north == relevant_fluid_south + or relevant_fluid_north == relevant_fluid_east + or relevant_fluid_north == relevant_fluid_west + ) + then count = count - 1 end - if relevant_fluid_south ~= nil and (relevant_fluid_south == relevant_fluid_west) then + if + relevant_fluid_east ~= nil + and (relevant_fluid_east == relevant_fluid_south or relevant_fluid_east == relevant_fluid_west) + then count = count - 1 end + if relevant_fluid_south ~= nil and (relevant_fluid_south == relevant_fluid_west) then count = count - 1 end - if count > 1 then - result = result .. " warning: there may be mixing fluids " - end + if count > 1 then result = result .. " warning: there may be mixing fluids " end end - --Same as pipe preview but for the faced direction only + --Same as pipe preview but for the faced direction only elseif stack.name == "pipe-to-ground" then local face_dir = players[pindex].building_direction - local ent_pos = fa_utils.offset_position(pos,face_dir,1) - rendering.draw_circle{color = {1, 0.0, 0.5},radius = 0.1,width = 2,target = ent_pos, surface = p.surface, time_to_live = 30} - - local ents_faced = p.surface.find_entities_filtered{position = ent_pos } + local ent_pos = fa_utils.offset_position(pos, face_dir, 1) + rendering.draw_circle({ + color = { 1, 0.0, 0.5 }, + radius = 0.1, + width = 2, + target = ent_pos, + surface = p.surface, + time_to_live = 30, + }) + + local ents_faced = p.surface.find_entities_filtered({ position = ent_pos }) local relevant_fluid_faced = nil local box = nil local dir_from_pos = nil local faced_ent = nil for i, ent_cand in ipairs(ents_faced) do - if ent_cand.valid and ent_cand.fluidbox ~= nil then - faced_ent = ent_cand - end + if ent_cand.valid and ent_cand.fluidbox ~= nil then faced_ent = ent_cand end end box, relevant_fluid_faced, dir_from_pos = mod.get_relevant_fluidbox_and_fluid_name(faced_ent, pos, face_dir) - --Prepare result string + --Prepare result string if relevant_fluid_faced ~= nil then local count = 0 - result = result .. ", connects ".. fa_utils.direction_lookup(face_dir) .. " to " .. relevant_fluid_faced .. " directly " + result = result + .. ", connects " + .. fa_utils.direction_lookup(face_dir) + .. " to " + .. relevant_fluid_faced + .. " directly " else result = result .. ", not connected above ground " end @@ -913,21 +1204,48 @@ function mod.build_preview_checks_info(stack, pindex) if #con_targets > 0 then for i, con_target_pos in ipairs(con_targets) do --For each heat connection target position - rendering.draw_circle{color = {1.0, 0.0, 0.5},radius = 0.1,width = 2,target = con_target_pos, surface = p.surface, time_to_live = 30} - local target_ents = p.surface.find_entities_filtered{position = con_target_pos} + rendering.draw_circle({ + color = { 1.0, 0.0, 0.5 }, + radius = 0.1, + width = 2, + target = con_target_pos, + surface = p.surface, + time_to_live = 30, + }) + local target_ents = p.surface.find_entities_filtered({ position = con_target_pos }) for j, target_ent in ipairs(target_ents) do - if target_ent.valid and #mod.get_heat_connection_positions(target_ent.name, target_ent.position, target_ent.direction) > 0 then - for k, spot in ipairs(mod.get_heat_connection_positions(target_ent.name, target_ent.position, target_ent.direction)) do - --For each heat connection of the found target entity - rendering.draw_circle{color = {1.0, 1.0, 0.5},radius = 0.2,width = 2,target = spot, surface = p.surface, time_to_live = 30} - if util.distance(con_target_pos,spot) < 0.2 then + if + target_ent.valid + and #mod.get_heat_connection_positions(target_ent.name, target_ent.position, target_ent.direction) + > 0 + then + for k, spot in + ipairs( + mod.get_heat_connection_positions(target_ent.name, target_ent.position, target_ent.direction) + ) + do + --For each heat connection of the found target entity + rendering.draw_circle({ + color = { 1.0, 1.0, 0.5 }, + radius = 0.2, + width = 2, + target = spot, + surface = p.surface, + time_to_live = 30, + }) + if util.distance(con_target_pos, spot) < 0.2 then --For each match - rendering.draw_circle{color = {0.5, 1.0, 0.5},radius = 0.3,width = 2,target = spot, surface = p.surface, time_to_live = 30} + rendering.draw_circle({ + color = { 0.5, 1.0, 0.5 }, + radius = 0.3, + width = 2, + target = spot, + surface = p.surface, + time_to_live = 30, + }) con_count = con_count + 1 - local con_dir = fa_utils.get_direction_biased(con_target_pos,pos) - if con_count > 1 then - result = result .. " and " - end + local con_dir = fa_utils.get_direction_biased(con_target_pos, pos) + if con_count > 1 then result = result .. " and " end result = result .. fa_utils.direction_lookup(con_dir) end end @@ -935,48 +1253,51 @@ function mod.build_preview_checks_info(stack, pindex) end end end - if con_count == 0 then - result = result .. " to nothing " - end + if con_count == 0 then result = result .. " to nothing " end end --For electric poles, report the directions of up to 5 wire-connectible electric poles that can connect if ent_p.type == "electric-pole" then - local pole_dict = surf.find_entities_filtered{type = "electric-pole", position = pos, radius = ent_p.max_wire_distance} - local poles = {} - for i, v in pairs(pole_dict) do - if v.prototype.max_wire_distance ~= nil and v.prototype.max_wire_distance >= ent_p.max_wire_distance then --Select only the poles that can connect back - table.insert(poles, v) - end - end - if #poles > 0 then - --List the first 4 poles within range - result = result .. " connecting " - for i, pole in ipairs(poles) do - if i < 5 then - local dist = math.ceil(util.distance(pole.position,pos)) - local dir = fa_utils.get_direction_biased(pole.position,pos) - result = result .. dist .. " tiles " .. fa_utils.direction_lookup(dir) .. ", " - end - end - else - --Notify if no connections and state nearest electric pole - result = result .. " not connected, " - local nearest_pole, min_dist = fa_electrical.find_nearest_electric_pole(nil,false,50,surf,pos) - if min_dist == nil or min_dist >= 1000 then - result = result .. " no electric poles within 1000 tiles, " - else - local dir = fa_utils.get_direction_biased(nearest_pole.position,pos) - result = result .. math.ceil(min_dist) .. " tiles " .. fa_utils.direction_lookup(dir) .. " to nearest electric pole, " - end - end + local pole_dict = + surf.find_entities_filtered({ type = "electric-pole", position = pos, radius = ent_p.max_wire_distance }) + local poles = {} + for i, v in pairs(pole_dict) do + if v.prototype.max_wire_distance ~= nil and v.prototype.max_wire_distance >= ent_p.max_wire_distance then --Select only the poles that can connect back + table.insert(poles, v) + end + end + if #poles > 0 then + --List the first 4 poles within range + result = result .. " connecting " + for i, pole in ipairs(poles) do + if i < 5 then + local dist = math.ceil(util.distance(pole.position, pos)) + local dir = fa_utils.get_direction_biased(pole.position, pos) + result = result .. dist .. " tiles " .. fa_utils.direction_lookup(dir) .. ", " + end + end + else + --Notify if no connections and state nearest electric pole + result = result .. " not connected, " + local nearest_pole, min_dist = fa_electrical.find_nearest_electric_pole(nil, false, 50, surf, pos) + if min_dist == nil or min_dist >= 1000 then + result = result .. " no electric poles within 1000 tiles, " + else + local dir = fa_utils.get_direction_biased(nearest_pole.position, pos) + result = result + .. math.ceil(min_dist) + .. " tiles " + .. fa_utils.direction_lookup(dir) + .. " to nearest electric pole, " + end + end end --For roboports, like electric poles, list possible neighbors (anything within 100 distx or 100 disty will be a neighbor if ent_p.name == "roboport" then local reach = 48.5 - local top_left = {x = math.floor(pos.x - reach), y = math.floor(pos.y - reach)} - local bottom_right = {x = math.ceil(pos.x + reach), y = math.ceil(pos.y + reach)} - local port_dict = surf.find_entities_filtered{type = "roboport", area = {top_left, bottom_right}} + local top_left = { x = math.floor(pos.x - reach), y = math.floor(pos.y - reach) } + local bottom_right = { x = math.ceil(pos.x + reach), y = math.ceil(pos.y + reach) } + local port_dict = surf.find_entities_filtered({ type = "roboport", area = { top_left, bottom_right } }) local ports = {} for i, v in pairs(port_dict) do table.insert(ports, v) @@ -986,8 +1307,8 @@ function mod.build_preview_checks_info(stack, pindex) result = result .. " connecting " for i, port in ipairs(ports) do if i <= 5 then - local dist = math.ceil(util.distance(port.position,pos)) - local dir = fa_utils.get_direction_biased(port.position,pos) + local dist = math.ceil(util.distance(port.position, pos)) + local dir = fa_utils.get_direction_biased(port.position, pos) result = result .. dist .. " tiles " .. fa_utils.direction_lookup(dir) .. ", " end end @@ -999,8 +1320,12 @@ function mod.build_preview_checks_info(stack, pindex) if min_dist == nil or min_dist >= max_dist then result = result .. " no other roboports within " .. max_dist .. " tiles, " else - local dir = fa_utils.get_direction_biased(nearest_port.position,pos) - result = result .. math.ceil(min_dist) .. " tiles " .. fa_utils.direction_lookup(dir) .. " to nearest roboport, " + local dir = fa_utils.get_direction_biased(nearest_port.position, pos) + result = result + .. math.ceil(min_dist) + .. " tiles " + .. fa_utils.direction_lookup(dir) + .. " to nearest roboport, " end end end @@ -1015,7 +1340,13 @@ function mod.build_preview_checks_info(stack, pindex) else local dist = math.ceil(util.distance(pos, nearest_roboport.position) - 25) local dir = fa_utils.direction_lookup(fa_utils.get_direction_biased(nearest_roboport.position, pos)) - result = result .. ", not in a network, nearest network " .. nearest_roboport.backer_name .. " is about " .. dist .. " to the " .. dir + result = result + .. ", not in a network, nearest network " + .. nearest_roboport.backer_name + .. " is about " + .. dist + .. " to the " + .. dir end else local network_name = network.cells[1].owner.backer_name @@ -1025,102 +1356,122 @@ function mod.build_preview_checks_info(stack, pindex) --For all electric powered entities, note whether powered, and from which direction. Otherwise report the nearest power pole. if ent_p.electric_energy_source_prototype ~= nil then - local position = pos - local build_dir = players[pindex].building_direction - if players[pindex].cursor then - position.x = position.x + math.ceil(2*ent_p.selection_box.right_bottom.x)/2 - .5 - position.y = position.y + math.ceil(2*ent_p.selection_box.right_bottom.y)/2 - .5 - elseif players[pindex].player_direction == defines.direction.north then - if build_dir == dirs.north or build_dir == dirs.south then - position.y = position.y + math.ceil(2* ent_p.selection_box.left_top.y)/2 + .5 - elseif build_dir == dirs.east or build_dir == dirs.west then - position.y = position.y + math.ceil(2* ent_p.selection_box.left_top.x)/2 + .5 - end - elseif players[pindex].player_direction == defines.direction.south then - if build_dir == dirs.north or build_dir == dirs.south then - position.y = position.y + math.ceil(2* ent_p.selection_box.right_bottom.y)/2 - .5 - elseif build_dir == dirs.east or build_dir == dirs.west then - position.y = position.y + math.ceil(2* ent_p.selection_box.right_bottom.x)/2 - .5 - end - elseif players[pindex].player_direction == defines.direction.west then - if build_dir == dirs.north or build_dir == dirs.south then - position.x = position.x + math.ceil(2* ent_p.selection_box.left_top.x)/2 + .5 - elseif build_dir == dirs.east or build_dir == dirs.west then - position.x = position.x + math.ceil(2* ent_p.selection_box.left_top.y)/2 + .5 - end - elseif players[pindex].player_direction == defines.direction.east then - if build_dir == dirs.north or build_dir == dirs.south then - position.x = position.x + math.ceil(2* ent_p.selection_box.right_bottom.x)/2 - .5 - elseif build_dir == dirs.east or build_dir == dirs.west then - position.x = position.x + math.ceil(2* ent_p.selection_box.right_bottom.y)/2 - .5 - end + local position = pos + local build_dir = players[pindex].building_direction + if players[pindex].cursor then + position.x = position.x + math.ceil(2 * ent_p.selection_box.right_bottom.x) / 2 - 0.5 + position.y = position.y + math.ceil(2 * ent_p.selection_box.right_bottom.y) / 2 - 0.5 + elseif players[pindex].player_direction == defines.direction.north then + if build_dir == dirs.north or build_dir == dirs.south then + position.y = position.y + math.ceil(2 * ent_p.selection_box.left_top.y) / 2 + 0.5 + elseif build_dir == dirs.east or build_dir == dirs.west then + position.y = position.y + math.ceil(2 * ent_p.selection_box.left_top.x) / 2 + 0.5 end - local dict = game.get_filtered_entity_prototypes{{filter = "type", type = "electric-pole"}} - local poles = {} - for i, v in pairs(dict) do - table.insert(poles, v) + elseif players[pindex].player_direction == defines.direction.south then + if build_dir == dirs.north or build_dir == dirs.south then + position.y = position.y + math.ceil(2 * ent_p.selection_box.right_bottom.y) / 2 - 0.5 + elseif build_dir == dirs.east or build_dir == dirs.west then + position.y = position.y + math.ceil(2 * ent_p.selection_box.right_bottom.x) / 2 - 0.5 end - table.sort(poles, function(k1, k2) return k1.supply_area_distance < k2.supply_area_distance end) - local check = false - ---@type LuaEntity - local found_pole = nil - for i, pole in ipairs(poles) do - local names = {} - for i1 = i, #poles, 1 do - table.insert(names, poles[i1].name) - end - local supply_dist = pole.supply_area_distance - if supply_dist > 15 then - supply_dist = supply_dist - 2 - end - local area = { - left_top = {(position.x + math.ceil(ent_p.selection_box.left_top.x) - supply_dist), (position.y + math.ceil(ent_p.selection_box.left_top.y) - supply_dist)}, - right_bottom = {(position.x + math.floor(ent_p.selection_box.right_bottom.x) + supply_dist), (position.y + math.floor(ent_p.selection_box.right_bottom.y) + supply_dist)}, - orientation = players[pindex].building_direction/(2 * dirs.south) - }--**laterdo "connected" check is a little buggy at the supply area edges, need to trim and tune, maybe re-enable direction based offset? The offset could be due to the pole width: 1 vs 2, maybe just make it more conservative? - local T = { - area = area, - name = names - } - local supplier_poles = surf.find_entities_filtered(T) - if #supplier_poles > 0 then - check = true - found_pole = supplier_poles[1] - break - end + elseif players[pindex].player_direction == defines.direction.west then + if build_dir == dirs.north or build_dir == dirs.south then + position.x = position.x + math.ceil(2 * ent_p.selection_box.left_top.x) / 2 + 0.5 + elseif build_dir == dirs.east or build_dir == dirs.west then + position.x = position.x + math.ceil(2 * ent_p.selection_box.left_top.y) / 2 + 0.5 + end + elseif players[pindex].player_direction == defines.direction.east then + if build_dir == dirs.north or build_dir == dirs.south then + position.x = position.x + math.ceil(2 * ent_p.selection_box.right_bottom.x) / 2 - 0.5 + elseif build_dir == dirs.east or build_dir == dirs.west then + position.x = position.x + math.ceil(2 * ent_p.selection_box.right_bottom.y) / 2 - 0.5 end - if check then - result = result .. " Power connected " - if found_pole.valid then - local dist = math.ceil(util.distance(found_pole.position,pos)) - local dir = fa_utils.get_direction_biased(found_pole.position,pos) - result = result .. " from " .. dist .. " tiles " .. fa_utils.direction_lookup(dir) .. ", " - end + end + local dict = game.get_filtered_entity_prototypes({ { filter = "type", type = "electric-pole" } }) + local poles = {} + for i, v in pairs(dict) do + table.insert(poles, v) + end + table.sort(poles, function(k1, k2) + return k1.supply_area_distance < k2.supply_area_distance + end) + local check = false + ---@type LuaEntity + local found_pole = nil + for i, pole in ipairs(poles) do + local names = {} + for i1 = i, #poles, 1 do + table.insert(names, poles[i1].name) + end + local supply_dist = pole.supply_area_distance + if supply_dist > 15 then supply_dist = supply_dist - 2 end + local area = { + left_top = { + (position.x + math.ceil(ent_p.selection_box.left_top.x) - supply_dist), + (position.y + math.ceil(ent_p.selection_box.left_top.y) - supply_dist), + }, + right_bottom = { + (position.x + math.floor(ent_p.selection_box.right_bottom.x) + supply_dist), + (position.y + math.floor(ent_p.selection_box.right_bottom.y) + supply_dist), + }, + orientation = players[pindex].building_direction / (2 * dirs.south), + } --**laterdo "connected" check is a little buggy at the supply area edges, need to trim and tune, maybe re-enable direction based offset? The offset could be due to the pole width: 1 vs 2, maybe just make it more conservative? + local T = { + area = area, + name = names, + } + local supplier_poles = surf.find_entities_filtered(T) + if #supplier_poles > 0 then + check = true + found_pole = supplier_poles[1] + break + end + end + if check then + result = result .. " Power connected " + if found_pole.valid then + local dist = math.ceil(util.distance(found_pole.position, pos)) + local dir = fa_utils.get_direction_biased(found_pole.position, pos) + result = result .. " from " .. dist .. " tiles " .. fa_utils.direction_lookup(dir) .. ", " + end + else + result = result .. " Power Not Connected, " + --Notify if no connections and state nearest electric pole + local nearest_pole, min_dist = fa_electrical.find_nearest_electric_pole(nil, false, 50, surf, pos) + if min_dist == nil or min_dist >= 1000 then + result = result .. " no electric poles within 1000 tiles, " else - result = result .. " Power Not Connected, " - --Notify if no connections and state nearest electric pole - local nearest_pole, min_dist = fa_electrical.find_nearest_electric_pole(nil,false,50,surf,pos) - if min_dist == nil or min_dist >= 1000 then - result = result .. " no electric poles within 1000 tiles, " - else - local dir = fa_utils.get_direction_biased(nearest_pole.position,pos) - result = result .. math.ceil(min_dist) .. " tiles " .. fa_utils.direction_lookup(dir) .. " to nearest electric pole, " - end + local dir = fa_utils.get_direction_biased(nearest_pole.position, pos) + result = result + .. math.ceil(min_dist) + .. " tiles " + .. fa_utils.direction_lookup(dir) + .. " to nearest electric pole, " end + end end - if players[pindex].cursor and util.distance(players[pindex].cursor_pos , players[pindex].position) > p.reach_distance + 2 then + if + players[pindex].cursor + and util.distance(players[pindex].cursor_pos, players[pindex].position) > p.reach_distance + 2 + then result = result .. ", cursor out of reach " end return result end ---For a building with fluidboxes, returns the external fluidbox and fluid name that would connect to one of the building's own fluidboxes at a particular position, from a particular direction. Importantly, ignores fluidboxes that are positioned correctly but would not connect, such as a pipe to ground facing a perpebdicular direction. +--For a building with fluidboxes, returns the external fluidbox and fluid name that would connect to one of the building's own fluidboxes at a particular position, from a particular direction. Importantly, ignores fluidboxes that are positioned correctly but would not connect, such as a pipe to ground facing a perpebdicular direction. function mod.get_relevant_fluidbox_and_fluid_name(building, pos, dir_from_pos) local relevant_box = nil local relevant_fluid_name = nil if building ~= nil and building.valid and building.fluidbox ~= nil then - rendering.draw_circle{color = {1, 1, 0},radius = 0.2,width = 2,target = building.position, surface = building.surface, time_to_live = 30} + rendering.draw_circle({ + color = { 1, 1, 0 }, + radius = 0.2, + width = 2, + target = building.position, + surface = building.surface, + time_to_live = 30, + }) --Run checks to see if we have any fluidboxes that are relevant for i = 1, #building.fluidbox, 1 do --game.print("box " .. i .. ": " .. building.fluidbox[i].name) @@ -1128,10 +1479,27 @@ function mod.get_relevant_fluidbox_and_fluid_name(building, pos, dir_from_pos) local target_pos = con.target_position local con_pos = con.position --game.print("new connection at: " .. target_pos.x .. "," .. target_pos.y) - rendering.draw_circle{color = {1, 0, 0},radius = 0.2,width = 2,target = target_pos, surface = building.surface, time_to_live = 30} - if util.distance(target_pos, pos) < 0.3 and fa_utils.get_direction_biased(con_pos,pos) == dir_from_pos - and not (building.name == "pipe-to-ground" and building.direction == dir_from_pos) then--Note: We correctly ignore the backside of a pipe to ground. - rendering.draw_circle{color = {0, 1, 0},radius = 0.3,width = 2,target = target_pos, surface = building.surface, time_to_live = 30} + rendering.draw_circle({ + color = { 1, 0, 0 }, + radius = 0.2, + width = 2, + target = target_pos, + surface = building.surface, + time_to_live = 30, + }) + if + util.distance(target_pos, pos) < 0.3 + and fa_utils.get_direction_biased(con_pos, pos) == dir_from_pos + and not (building.name == "pipe-to-ground" and building.direction == dir_from_pos) + then --Note: We correctly ignore the backside of a pipe to ground. + rendering.draw_circle({ + color = { 0, 1, 0 }, + radius = 0.3, + width = 2, + target = target_pos, + surface = building.surface, + time_to_live = 30, + }) relevant_box = building.fluidbox[i] if building.fluidbox[i] ~= nil then relevant_fluid_name = building.fluidbox[i].name @@ -1150,20 +1518,18 @@ end function mod.teleport_player_out_of_build_area(left_top, right_bottom, pindex) local p = game.get_player(pindex) local pos = p.position - if pos.x < left_top.x or pos.x > right_bottom.x or pos.y < left_top.y or pos.y > right_bottom.y then - return - end + if pos.x < left_top.x or pos.x > right_bottom.x or pos.y < left_top.y or pos.y > right_bottom.y then return end local exits = {} - exits[1] = {x = left_top.x - 1, y = left_top.y - 0} - exits[2] = {x = left_top.x - 0, y = left_top.y - 1} - exits[3] = {x = left_top.x - 1, y = left_top.y - 1} - exits[4] = {x = left_top.x - 2, y = left_top.y - 0} - exits[5] = {x = left_top.x - 0, y = left_top.y - 2} - exits[6] = {x = left_top.x - 2, y = left_top.y - 2} + exits[1] = { x = left_top.x - 1, y = left_top.y - 0 } + exits[2] = { x = left_top.x - 0, y = left_top.y - 1 } + exits[3] = { x = left_top.x - 1, y = left_top.y - 1 } + exits[4] = { x = left_top.x - 2, y = left_top.y - 0 } + exits[5] = { x = left_top.x - 0, y = left_top.y - 2 } + exits[6] = { x = left_top.x - 2, y = left_top.y - 2 } --Teleport to exit spots if possible - for i,pos in ipairs(exits) do - if p.surface.can_place_entity{name = "character", position = pos} then + for i, pos in ipairs(exits) do + if p.surface.can_place_entity({ name = "character", position = pos }) then fa_teleport.teleport_to_closest(pindex, pos, false, true) return end @@ -1179,25 +1545,38 @@ function mod.snap_place_steam_engine_to_a_boiler(pindex) local found_empty_spot = false local found_valid_spot = false --Locate all boilers within 10m - local boilers = p.surface.find_entities_filtered{name = "boiler", position = p.position, radius = 10} + local boilers = p.surface.find_entities_filtered({ name = "boiler", position = p.position, radius = 10 }) --If none then locate all boilers within 25m if boilers == nil or #boilers == 0 then - boilers = p.surface.find_entities_filtered{name = "boiler", position = p.position, radius = 25} + boilers = p.surface.find_entities_filtered({ name = "boiler", position = p.position, radius = 25 }) end if boilers == nil or #boilers == 0 then - p.play_sound{path = "utility/cannot_build"} - printout("Error: No boilers found nearby",pindex) + p.play_sound({ path = "utility/cannot_build" }) + printout("Error: No boilers found nearby", pindex) return end --For each boiler found: - for i,boiler in ipairs(boilers) do + for i, boiler in ipairs(boilers) do --Check if there is any entity in front of it - local output_location = fa_utils.offset_position(boiler.position,boiler.direction,1.5) - rendering.draw_circle{color = {1, 1, 0.25},radius = 0.25,width = 2,target = output_location, surface = p.surface, time_to_live = 60, draw_on_ground = false} - local output_ents = p.surface.find_entities_filtered{position = output_location, radius = 0.25, type = {"resource","generator"}, invert = true} + local output_location = fa_utils.offset_position(boiler.position, boiler.direction, 1.5) + rendering.draw_circle({ + color = { 1, 1, 0.25 }, + radius = 0.25, + width = 2, + target = output_location, + surface = p.surface, + time_to_live = 60, + draw_on_ground = false, + }) + local output_ents = p.surface.find_entities_filtered({ + position = output_location, + radius = 0.25, + type = { "resource", "generator" }, + invert = true, + }) if output_ents == nil or #output_ents == 0 then --Determine engine position based on boiler direction found_empty_spot = true @@ -1206,33 +1585,47 @@ function mod.snap_place_steam_engine_to_a_boiler(pindex) local old_building_dir = players[pindex].building_direction players[pindex].building_direction = dir if dir == dirs.east then - engine_position = fa_utils.offset_position(engine_position, dirs.east,2) + engine_position = fa_utils.offset_position(engine_position, dirs.east, 2) elseif dir == dirs.south then - engine_position = fa_utils.offset_position(engine_position, dirs.south,2) + engine_position = fa_utils.offset_position(engine_position, dirs.south, 2) elseif dir == dirs.west then - engine_position = fa_utils.offset_position(engine_position, dirs.west,2) + engine_position = fa_utils.offset_position(engine_position, dirs.west, 2) elseif dir == dirs.north then - engine_position = fa_utils.offset_position(engine_position, dirs.north,2) + engine_position = fa_utils.offset_position(engine_position, dirs.north, 2) end - rendering.draw_circle{color = {0.25, 1, 0.25},radius = 0.5,width = 2,target = engine_position, surface = p.surface, time_to_live = 60, draw_on_ground = false} + rendering.draw_circle({ + color = { 0.25, 1, 0.25 }, + radius = 0.5, + width = 2, + target = engine_position, + surface = p.surface, + time_to_live = 60, + draw_on_ground = false, + }) fa_mining_tools.clear_obstacles_in_circle(engine_position, 4, pindex) --Check if can build from cursor to the relative position - if p.can_build_from_cursor{position = engine_position, direction = dir} then - p.build_from_cursor{position = engine_position, direction = dir} + if p.can_build_from_cursor({ position = engine_position, direction = dir }) then + p.build_from_cursor({ position = engine_position, direction = dir }) found_valid_spot = true - printout("Placed steam engine near boiler at " .. math.floor(boiler.position.x) .. "," .. math.floor(boiler.position.y),pindex) + printout( + "Placed steam engine near boiler at " + .. math.floor(boiler.position.x) + .. "," + .. math.floor(boiler.position.y), + pindex + ) return end end end --If all have been skipped and none were found then play error if found_empty_spot == false then - p.play_sound{path = "utility/cannot_build"} - printout("Error: All boilers nearby are blocked or already connected.",pindex) + p.play_sound({ path = "utility/cannot_build" }) + printout("Error: All boilers nearby are blocked or already connected.", pindex) return elseif found_valid_spot == false then - p.play_sound{path = "utility/cannot_build"} - printout("Error: Boilers found but unable to build in front of them, check build area for obstacles.",pindex) + p.play_sound({ path = "utility/cannot_build" }) + printout("Error: Boilers found but unable to build in front of them, check build area for obstacles.", pindex) return end end @@ -1241,14 +1634,14 @@ end function mod.is_a_pipe_end(ent, pindex) local p = game.get_player(pindex) local pos = players[pindex].cursor_pos - local ents_north = p.surface.find_entities_filtered{position = {x = pos.x+0, y = pos.y-1} } - local ents_south = p.surface.find_entities_filtered{position = {x = pos.x+0, y = pos.y+1} } - local ents_east = p.surface.find_entities_filtered{position = {x = pos.x+1, y = pos.y+0} } - local ents_west = p.surface.find_entities_filtered{position = {x = pos.x-1, y = pos.y+0} } + local ents_north = p.surface.find_entities_filtered({ position = { x = pos.x + 0, y = pos.y - 1 } }) + local ents_south = p.surface.find_entities_filtered({ position = { x = pos.x + 0, y = pos.y + 1 } }) + local ents_east = p.surface.find_entities_filtered({ position = { x = pos.x + 1, y = pos.y + 0 } }) + local ents_west = p.surface.find_entities_filtered({ position = { x = pos.x - 1, y = pos.y + 0 } }) local relevant_fluid_north = nil - local relevant_fluid_east = nil + local relevant_fluid_east = nil local relevant_fluid_south = nil - local relevant_fluid_west = nil + local relevant_fluid_west = nil local box = nil local dir_from_pos = nil local total_ent_count = 0 @@ -1259,7 +1652,7 @@ function mod.is_a_pipe_end(ent, pindex) if ent_cand.valid and ent_cand.fluidbox ~= nil then north_ent = ent_cand total_ent_count = total_ent_count + 1 - if (ent_cand.type == "pipe" or (ent_cand.type == "pipe-to-ground" and ent_cand.direction == dirs.north)) then + if ent_cand.type == "pipe" or (ent_cand.type == "pipe-to-ground" and ent_cand.direction == dirs.north) then north_count = 1 total_ent_count = total_ent_count - 1 end @@ -1271,7 +1664,7 @@ function mod.is_a_pipe_end(ent, pindex) if ent_cand.valid and ent_cand.fluidbox ~= nil then south_ent = ent_cand total_ent_count = total_ent_count + 1 - if (ent_cand.type == "pipe" or (ent_cand.type == "pipe-to-ground" and ent_cand.direction == dirs.south)) then + if ent_cand.type == "pipe" or (ent_cand.type == "pipe-to-ground" and ent_cand.direction == dirs.south) then south_count = 1 total_ent_count = total_ent_count - 1 end @@ -1283,7 +1676,7 @@ function mod.is_a_pipe_end(ent, pindex) if ent_cand.valid and ent_cand.fluidbox ~= nil then east_ent = ent_cand total_ent_count = total_ent_count + 1 - if (ent_cand.type == "pipe" or (ent_cand.type == "pipe-to-ground" and ent_cand.direction == dirs.east)) then + if ent_cand.type == "pipe" or (ent_cand.type == "pipe-to-ground" and ent_cand.direction == dirs.east) then east_count = 1 total_ent_count = total_ent_count - 1 end @@ -1295,7 +1688,7 @@ function mod.is_a_pipe_end(ent, pindex) if ent_cand.valid and ent_cand.fluidbox ~= nil then west_ent = ent_cand total_ent_count = total_ent_count + 1 - if (ent_cand.type == "pipe" or (ent_cand.type == "pipe-to-ground" and ent_cand.direction == dirs.west)) then + if ent_cand.type == "pipe" or (ent_cand.type == "pipe-to-ground" and ent_cand.direction == dirs.west) then west_count = 1 total_ent_count = total_ent_count - 1 end @@ -1316,7 +1709,7 @@ function mod.is_a_pipe_end(ent, pindex) --Choose false to avoid false positives return false else - --More than 1 anything + --More than 1 anything return false end end @@ -1324,7 +1717,7 @@ end function mod.delete_empty_planners_in_inventory(pindex) local inv = game.get_player(pindex).get_main_inventory() local length = #inv - for i=1,length,1 do + for i = 1, length, 1 do local stack = inv[i] if stack and stack.valid_for_read then if stack.name == "cut-paste-tool" or stack.name == "copy-paste-tool" then @@ -1338,4 +1731,4 @@ function mod.delete_empty_planners_in_inventory(pindex) end end -return mod \ No newline at end of file +return mod diff --git a/scripts/building-vehicle-sectors.lua b/scripts/building-vehicle-sectors.lua index 0ffce924..89ac3d7f 100644 --- a/scripts/building-vehicle-sectors.lua +++ b/scripts/building-vehicle-sectors.lua @@ -1,8 +1,8 @@ --Here: functions specific to building menus -local util = require('util') -local fa_utils = require('scripts.fa-utils') +local util = require("util") +local fa_utils = require("scripts.fa-utils") local fa_crafting = require("scripts.crafting") -local localising = require('scripts.localising') +local localising = require("scripts.localising") local fa_belts = require("scripts.transport-belts") local fa_blueprints = require("scripts.blueprints") @@ -16,12 +16,8 @@ function mod.add_to_inventory_bar(ent, amount) local inventory = ent.get_inventory(defines.inventory.chest) --Checks - if not inventory then - return {"access.failed-inventory-limit-ajust-notcontainter"} - end - if not inventory.supports_bar() then - return {"access.failed-inventory-limit-ajust-no-limit"} - end + if not inventory then return { "access.failed-inventory-limit-ajust-notcontainter" } end + if not inventory.supports_bar() then return { "access.failed-inventory-limit-ajust-no-limit" } end local max_bar = #inventory + 1 local current_bar = inventory.get_bar() @@ -40,32 +36,33 @@ function mod.add_to_inventory_bar(ent, amount) --Return result ---@ type LocalisedString - local value = current_bar -1 --Mismatch correction + local value = current_bar - 1 --Mismatch correction if current_bar == max_bar then - value = {"gui.all"} - current_bar=1000 + value = { "gui.all" } + current_bar = 1000 else current_bar = value end - return {"access.inventory-limit-status",value,current_bar} + return { "access.inventory-limit-status", value, current_bar } end --Loads and opens the building menu -function mod.open_operable_building(ent,pindex) +function mod.open_operable_building(ent, pindex) if ent.operable and ent.prototype.is_building then --Check if within reach - if util.distance(game.get_player(pindex).position, players[pindex].cursor_pos) > game.get_player(pindex).reach_distance then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} - printout("Building is out of player reach",pindex) + if + util.distance(game.get_player(pindex).position, players[pindex].cursor_pos) + > game.get_player(pindex).reach_distance + then + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) + printout("Building is out of player reach", pindex) game.get_player(pindex).selected = nil game.get_player(pindex).opened = nil return end --Open GUI if not already local p = game.get_player(pindex) - if p.opened == nil then - p.opened = ent - end + if p.opened == nil then p.opened = ent end --Other stuff... players[pindex].menu_search_index = 0 players[pindex].menu_search_index_2 = 0 @@ -106,32 +103,38 @@ function mod.open_operable_building(ent,pindex) if ent.get_output_inventory() ~= nil then table.insert(players[pindex].building.sectors, { name = "Output", - inventory = ent.get_output_inventory()}) + inventory = ent.get_output_inventory(), + }) end if ent.get_fuel_inventory() ~= nil then table.insert(players[pindex].building.sectors, { name = "Fuel", - inventory = ent.get_fuel_inventory()}) + inventory = ent.get_fuel_inventory(), + }) end if ent.prototype.ingredient_count ~= nil then table.insert(players[pindex].building.sectors, { name = "Input", - inventory = ent.get_inventory(defines.inventory.assembling_machine_input)}) + inventory = ent.get_inventory(defines.inventory.assembling_machine_input), + }) end if ent.get_module_inventory() ~= nil and #ent.get_module_inventory() > 0 then table.insert(players[pindex].building.sectors, { name = "Modules", - inventory = ent.get_module_inventory()}) + inventory = ent.get_module_inventory(), + }) end if ent.get_burnt_result_inventory() ~= nil and #ent.get_burnt_result_inventory() > 0 then table.insert(players[pindex].building.sectors, { name = "Burnt result", - inventory = ent.get_burnt_result_inventory()}) + inventory = ent.get_burnt_result_inventory(), + }) end if ent.fluidbox ~= nil and #ent.fluidbox > 0 then table.insert(players[pindex].building.sectors, { name = "Fluid", - inventory = ent.fluidbox}) + inventory = ent.fluidbox, + }) end --Special inventories @@ -140,33 +143,36 @@ function mod.open_operable_building(ent,pindex) if ent.get_inventory(invs.rocket_silo_rocket) ~= nil and #ent.get_inventory(invs.rocket_silo_rocket) > 0 then table.insert(players[pindex].building.sectors, { name = "Rocket", - inventory = ent.get_inventory(invs.rocket_silo_rocket)}) + inventory = ent.get_inventory(invs.rocket_silo_rocket), + }) end end if ent.filter_slot_count > 0 and ent.type == "inserter" then table.insert(players[pindex].building.sectors, { name = "Filters", - inventory = {}}) + inventory = {}, + }) for i = 1, ent.filter_slot_count do local filter = ent.get_filter(i) - if filter == nil then - filter = "No filter selected." - end + if filter == nil then filter = "No filter selected." end table.insert(players[pindex].building.sectors[#players[pindex].building.sectors].inventory, filter) end - table.insert(players[pindex].building.sectors[#players[pindex].building.sectors].inventory, ent.inserter_filter_mode) + table.insert( + players[pindex].building.sectors[#players[pindex].building.sectors].inventory, + ent.inserter_filter_mode + ) players[pindex].item_selection = false players[pindex].item_cache = {} players[pindex].item_selector = { index = 0, group = 0, - subgroup = 0 + subgroup = 0, } end - for i1=#players[pindex].building.sectors, 2, -1 do - for i2 = i1-1, 1, -1 do + for i1 = #players[pindex].building.sectors, 2, -1 do + for i2 = i1 - 1, 1, -1 do if players[pindex].building.sectors[i1].inventory == players[pindex].building.sectors[i2].inventory then table.remove(players[pindex].building.sectors, i2) i2 = i2 + 1 @@ -183,7 +189,11 @@ function mod.open_operable_building(ent,pindex) --For assembling machine types with no recipe, open recipe building sector directly local recipe = players[pindex].building.recipe - if (recipe == nil or not recipe.valid) and (ent.prototype.type == "assembling-machine") and players[pindex].building.recipe_list ~= nil then + if + (recipe == nil or not recipe.valid) + and (ent.prototype.type == "assembling-machine") + and players[pindex].building.recipe_list ~= nil + then players[pindex].building.sector = #players[pindex].building.sectors + 1 players[pindex].building.index = 1 players[pindex].building.category = 1 @@ -195,7 +205,7 @@ function mod.open_operable_building(ent,pindex) players[pindex].item_selector = { index = 0, group = 0, - subgroup = 0 + subgroup = 0, } mod.read_building_recipe(pindex, "Select a Recipe, ") return @@ -207,13 +217,13 @@ function mod.open_operable_building(ent,pindex) players[pindex].building.ent = ent players[pindex].in_menu = true players[pindex].menu = "building_no_sectors" - local result = localising.get(ent,pindex) .. ", this menu has no options " + local result = localising.get(ent, pindex) .. ", this menu has no options " if ent.get_control_behavior() ~= nil then result = result .. ", press 'N' to open the circuit network menu " end printout(result, pindex) else - printout(localising.get(ent,pindex) .. " has no menu ", pindex) + printout(localising.get(ent, pindex) .. " has no menu ", pindex) end end else @@ -222,21 +232,22 @@ function mod.open_operable_building(ent,pindex) end --Loads and opens the vehicle menu -function mod.open_operable_vehicle(ent,pindex) +function mod.open_operable_vehicle(ent, pindex) if ent.valid and ent.operable then --Check if within reach - if util.distance(game.get_player(pindex).position, players[pindex].cursor_pos) > game.get_player(pindex).reach_distance then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + if + util.distance(game.get_player(pindex).position, players[pindex].cursor_pos) + > game.get_player(pindex).reach_distance + then + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) game.get_player(pindex).selected = nil game.get_player(pindex).opened = nil - printout("Vehicle is out of player reach",pindex) + printout("Vehicle is out of player reach", pindex) return end --Open GUI if not already local p = game.get_player(pindex) - if p.opened == nil then - p.opened = ent - end + if p.opened == nil then p.opened = ent end --Other stuff... players[pindex].menu_search_index = 0 players[pindex].menu_search_index_2 = 0 @@ -259,17 +270,20 @@ function mod.open_operable_vehicle(ent,pindex) if ent.get_output_inventory() ~= nil then table.insert(players[pindex].building.sectors, { name = "Output", - inventory = ent.get_output_inventory()}) + inventory = ent.get_output_inventory(), + }) end if ent.get_fuel_inventory() ~= nil then table.insert(players[pindex].building.sectors, { name = "Fuel", - inventory = ent.get_fuel_inventory()}) + inventory = ent.get_fuel_inventory(), + }) end if ent.get_burnt_result_inventory() ~= nil and #ent.get_burnt_result_inventory() > 0 then table.insert(players[pindex].building.sectors, { name = "Burnt result", - inventory = ent.get_burnt_result_inventory()}) + inventory = ent.get_burnt_result_inventory(), + }) end --Special inventories @@ -279,29 +293,33 @@ function mod.open_operable_vehicle(ent,pindex) if ent.get_inventory(invs.car_ammo) ~= nil and #ent.get_inventory(invs.car_ammo) > 0 then table.insert(players[pindex].building.sectors, { name = "Ammo", - inventory = ent.get_inventory(invs.car_ammo)}) + inventory = ent.get_inventory(invs.car_ammo), + }) end end if ent.type == "spider-vehicle" then if ent.get_inventory(invs.spider_trunk) ~= nil and #ent.get_inventory(invs.spider_trunk) > 0 then table.insert(players[pindex].building.sectors, { name = "Output", - inventory = ent.get_inventory(invs.spider_trunk)}) + inventory = ent.get_inventory(invs.spider_trunk), + }) end if ent.get_inventory(invs.spider_trash) ~= nil and #ent.get_inventory(invs.spider_trash) > 0 then table.insert(players[pindex].building.sectors, { name = "Trash", - inventory = ent.get_inventory(invs.spider_trash)}) + inventory = ent.get_inventory(invs.spider_trash), + }) end if ent.get_inventory(invs.spider_ammo) ~= nil and #ent.get_inventory(invs.spider_ammo) > 0 then table.insert(players[pindex].building.sectors, { name = "Ammo", - inventory = ent.get_inventory(invs.spider_ammo)}) + inventory = ent.get_inventory(invs.spider_ammo), + }) end end - for i1=#players[pindex].building.sectors, 2, -1 do - for i2 = i1-1, 1, -1 do + for i1 = #players[pindex].building.sectors, 2, -1 do + for i2 = i1 - 1, 1, -1 do if players[pindex].building.sectors[i1].inventory == players[pindex].building.sectors[i2].inventory then table.remove(players[pindex].building.sectors, i2) i2 = i2 + 1 @@ -332,15 +350,26 @@ function mod.open_operable_vehicle(ent,pindex) end end ---Building recipe selection sector: Read the selected recipe +--Building recipe selection sector: Read the selected recipe function mod.read_building_recipe(pindex, start_phrase) start_phrase = start_phrase or "" if players[pindex].building.recipe_selection then --inside the selector - local recipe = players[pindex].building.recipe_list[players[pindex].building.category][players[pindex].building.index] + local recipe = + players[pindex].building.recipe_list[players[pindex].building.category][players[pindex].building.index] if recipe and recipe.valid then - printout(start_phrase .. localising.get(recipe,pindex) .. " " .. recipe.category .. " " .. recipe.group.name .. " " .. recipe.subgroup.name, pindex) + printout( + start_phrase + .. localising.get(recipe, pindex) + .. " " + .. recipe.category + .. " " + .. recipe.group.name + .. " " + .. recipe.subgroup.name, + pindex + ) else - printout(start_phrase .. "blank",pindex) + printout(start_phrase .. "blank", pindex) end else local recipe = players[pindex].building.recipe @@ -358,37 +387,44 @@ function mod.read_sector_slot(pindex, prefix_inventory_size_and_name) if building_sector.name == "Filters" then local inventory = building_sector.inventory local start_phrase = #inventory .. " " .. building_sector.name .. ", " - if not prefix_inventory_size_and_name then - start_phrase = "" - end - printout(start_phrase .. players[pindex].building.index .. ", " .. building_sector.inventory[players[pindex].building.index], pindex) + if not prefix_inventory_size_and_name then start_phrase = "" end + printout( + start_phrase + .. players[pindex].building.index + .. ", " + .. building_sector.inventory[players[pindex].building.index], + pindex + ) elseif building_sector.name == "Fluid" then - if players[pindex].building.ent ~= nil and players[pindex].building.ent.valid and players[pindex].building.ent.type == "fluid-turret" and players[pindex].building.index ~= 1 then + if + players[pindex].building.ent ~= nil + and players[pindex].building.ent.valid + and players[pindex].building.ent.type == "fluid-turret" + and players[pindex].building.index ~= 1 + then --Prevent fluid turret crashes players[pindex].building.index = 1 end local box = building_sector.inventory if #box == 0 then - printout("No fluid" , pindex) + printout("No fluid", pindex) return elseif players[pindex].building.index > #box or players[pindex].building.index == 0 then players[pindex].building.index = 1 - game.get_player(pindex).play_sound{path = "inventory-wrap-around"} + game.get_player(pindex).play_sound({ path = "inventory-wrap-around" }) end local capacity = box.get_capacity(players[pindex].building.index) local type = box.get_prototype(players[pindex].building.index).production_type local fluid = box[players[pindex].building.index] local len = #box local start_phrase = len .. " " .. building_sector.name .. ", " - if not prefix_inventory_size_and_name then - start_phrase = "" - end + if not prefix_inventory_size_and_name then start_phrase = "" end --fluid = {name = "water", amount = 1} - local name = "Any" + local name = "Any" local amount = 0 if fluid ~= nil then amount = fluid.amount - name = fluid.name--does not locallise..?** + name = fluid.name --does not locallise..?** end --laterdo use fluidbox.get_locked_fluid(i) if needed. --Read the fluid ingredients & products --Note: We could have separated by input/output but right now the "type" is "input" for all fluids it seeems? @@ -413,20 +449,16 @@ function mod.read_sector_slot(pindex, prefix_inventory_size_and_name) output_item_count = output_item_count + 1 end end - if index < 0 then - index = 0 - end + if index < 0 then index = 0 end local prev_name = name name = "Empty slot reserved for " if index <= input_fluid_count then index = index + input_item_count for i, v in pairs(recipe.ingredients) do if v.type == "fluid" and i == index then - local localised_name = localising.get(game.fluid_prototypes[v.name],pindex) + local localised_name = localising.get(game.fluid_prototypes[v.name], pindex) name = name .. " input " .. localised_name .. " times " .. v.amount .. " per cycle " - if prev_name ~= "Any" then - name = "input " .. prev_name .. " times " .. math.floor(0.5 + amount) - end + if prev_name ~= "Any" then name = "input " .. prev_name .. " times " .. math.floor(0.5 + amount) end end end else @@ -434,11 +466,9 @@ function mod.read_sector_slot(pindex, prefix_inventory_size_and_name) index = index + output_item_count for i, v in pairs(recipe.products) do if v.type == "fluid" and i == index then - local localised_name = localising.get(game.fluid_prototypes[v.name],pindex) + local localised_name = localising.get(game.fluid_prototypes[v.name], pindex) name = name .. " output " .. localised_name .. " times " .. v.amount .. " per cycle " - if prev_name ~= "Any" then - name = "output " .. prev_name .. " times " .. math.floor(0.5 + amount) - end + if prev_name ~= "Any" then name = "output " .. prev_name .. " times " .. math.floor(0.5 + amount) end end end end @@ -447,19 +477,16 @@ function mod.read_sector_slot(pindex, prefix_inventory_size_and_name) end --Read the fluid found, including amount if any printout(start_phrase .. " " .. name, pindex) - elseif #building_sector.inventory > 0 then - --Item inventories - local inventory=building_sector.inventory + --Item inventories + local inventory = building_sector.inventory local start_phrase = #inventory .. " " .. building_sector.name .. ", " if inventory.supports_bar() and #inventory > inventory.get_bar() - 1 then --local unlocked = inventory.supports_bar() and inventory.get_bar() - 1 or nil local unlocked = inventory.get_bar() - 1 start_phrase = start_phrase .. ", " .. unlocked .. " unlocked, " end - if not prefix_inventory_size_and_name then - start_phrase = "" - end + if not prefix_inventory_size_and_name then start_phrase = "" end --Mention if a slot is locked if inventory.supports_bar() and players[pindex].building.index > inventory.get_bar() - 1 then start_phrase = start_phrase .. " locked " @@ -468,11 +495,9 @@ function mod.read_sector_slot(pindex, prefix_inventory_size_and_name) stack = building_sector.inventory[players[pindex].building.index] if stack and stack.valid_for_read and stack.valid then if stack.is_blueprint then - printout(fa_blueprints.get_blueprint_info(stack,false),pindex) + printout(fa_blueprints.get_blueprint_info(stack, false), pindex) else - if stack.health < 1 then - start_phrase = start_phrase .. " damaged " - end + if stack.health < 1 then start_phrase = start_phrase .. " damaged " end local remote_info = "" if stack.name == "spidertron-remote" then if stack.connected_entity == nil then @@ -485,14 +510,12 @@ function mod.read_sector_slot(pindex, prefix_inventory_size_and_name) end end end - printout(start_phrase .. localising.get(stack,pindex) .. remote_info .. " x " .. stack.count, pindex) + printout(start_phrase .. localising.get(stack, pindex) .. remote_info .. " x " .. stack.count, pindex) end else --Read the "empty slot" local result = "Empty slot" - if building_sector.name == "Modules" then - result = "Empty module slot" - end + if building_sector.name == "Modules" then result = "Empty module slot" end local recipe = players[pindex].building.recipe if recipe ~= nil then if building_sector.name == "Input" then @@ -500,7 +523,7 @@ function mod.read_sector_slot(pindex, prefix_inventory_size_and_name) result = result .. " reserved for " for i, v in pairs(recipe.ingredients) do if v.type == "item" and i == players[pindex].building.index then - local localised_name = localising.get(game.item_prototypes[v.name],pindex) + local localised_name = localising.get(game.item_prototypes[v.name], pindex) result = result .. localised_name .. " times " .. v.amount .. " per cycle " end end @@ -510,24 +533,38 @@ function mod.read_sector_slot(pindex, prefix_inventory_size_and_name) result = result .. " reserved for " for i, v in pairs(recipe.products) do if v.type == "item" and i == players[pindex].building.index then - local localised_name = localising.get(game.item_prototypes[v.name],pindex) + local localised_name = localising.get(game.item_prototypes[v.name], pindex) result = result .. localised_name .. " times " .. v.amount .. " per cycle " end end --result = result .. "nothing" end - elseif players[pindex].building.ent ~= nil and players[pindex].building.ent.valid and players[pindex].building.ent.type == "lab" and building_sector.name == "Input" then + elseif + players[pindex].building.ent ~= nil + and players[pindex].building.ent.valid + and players[pindex].building.ent.type == "lab" + and building_sector.name == "Input" + then --laterdo switch to {"item-name.".. ent.prototype.lab_inputs[players[pindex].building.index] } result = result .. " reserved for science pack type " .. players[pindex].building.index - elseif players[pindex].building.ent ~= nil and players[pindex].building.ent.valid and players[pindex].building.ent.type == "roboport" then + elseif + players[pindex].building.ent ~= nil + and players[pindex].building.ent.valid + and players[pindex].building.ent.type == "roboport" + then result = result .. " reserved for worker robots " - elseif players[pindex].building.ent ~= nil and players[pindex].building.ent.valid and players[pindex].building.ent.type == "ammo-turret" or players[pindex].building.ent.type == "artillery-turret" then + elseif + players[pindex].building.ent ~= nil + and players[pindex].building.ent.valid + and players[pindex].building.ent.type == "ammo-turret" + or players[pindex].building.ent.type == "artillery-turret" + then result = result .. " reserved for ammo " end printout(start_phrase .. result, pindex) end elseif prefix_inventory_size_and_name then - printout("0 " .. building_sector.name,pindex) + printout("0 " .. building_sector.name, pindex) end end diff --git a/scripts/circuit-networks.lua b/scripts/circuit-networks.lua index 61b68066..3e99b7c9 100644 --- a/scripts/circuit-networks.lua +++ b/scripts/circuit-networks.lua @@ -1,12 +1,11 @@ --Here: Functions relating to circuit networks, virtual signals, wiring and unwiring buildings, and the such. --Does not include event handlers directly, but can have functions called by them. -local circular = require('scripts.ds.circular-options-list') -local localising = require('scripts.localising') -local util = require('util') -local fa_utils = require('scripts.fa-utils') -local descriptors = require('scripts.descriptors') -local multistate_switch = require('scripts.ui.low-level.multistate-switch') - +local circular = require("scripts.ds.circular-options-list") +local localising = require("scripts.localising") +local util = require("util") +local fa_utils = require("scripts.fa-utils") +local descriptors = require("scripts.descriptors") +local multistate_switch = require("scripts.ui.low-level.multistate-switch") local dcb = defines.control_behavior @@ -26,9 +25,7 @@ local function make_read_switch_for_entity_uncached(entity) local desc = descriptors.PROTOTYPES[type] -- We may not understand this object at all or (for the time being) be -- handling it on older code paths. - if not desc then - return nil - end + if not desc then return nil end local cn = desc.circuit_network if not cn then @@ -45,7 +42,7 @@ local function make_read_switch_for_entity_uncached(entity) return multistate_switch.create({ on_off_field = reading.toggle_field, - state_field= reading.mode_field, + state_field = reading.mode_field, off_label = reading.disabled_label, choices = reading.choices, }) @@ -54,9 +51,7 @@ end -- @param entity LuaEntity -- @returns MultistateSwitch? local function make_read_switch_for_entity(entity) - if read_mode_switch_cache[entity.prototype.type] then - return read_mode_switch_cache[entity.prototype.type] - end + if read_mode_switch_cache[entity.prototype.type] then return read_mode_switch_cache[entity.prototype.type] end local uncached = make_read_switch_for_entity_uncached(entity) if uncached then @@ -71,10 +66,10 @@ end function drag_wire_and_read(pindex) --Start/end dragging wire local p = game.get_player(pindex) - local something_happened = p.drag_wire { position = players[pindex].cursor_pos } + local something_happened = p.drag_wire({ position = players[pindex].cursor_pos }) --Comment on it if not something_happened then - p.play_sound { path = "utility/cannot_build" } + p.play_sound({ path = "utility/cannot_build" }) return end local result = "" @@ -91,13 +86,35 @@ function drag_wire_and_read(pindex) end local drag_target = p.drag_target - local ents_at_position = p.surface.find_entities_filtered { position = players[pindex].cursor_pos, radius = 0.2, type = { "transport-belt", "inserter", "container", "logistic-container", "storage-tank", "gate", "rail-signal", "rail-chain-signal", "train-stop", "accumulator", "roboport", "mining-drill", "pumpjack", "power-switch", "programmable-speaker", "lamp", "offshore-pump", "pump", "electric-pole" } } + local ents_at_position = p.surface.find_entities_filtered({ + position = players[pindex].cursor_pos, + radius = 0.2, + type = { + "transport-belt", + "inserter", + "container", + "logistic-container", + "storage-tank", + "gate", + "rail-signal", + "rail-chain-signal", + "train-stop", + "accumulator", + "roboport", + "mining-drill", + "pumpjack", + "power-switch", + "programmable-speaker", + "lamp", + "offshore-pump", + "pump", + "electric-pole", + }, + }) local c_ent = ents_at_position[1] local last_c_ent = players[pindex].last_wire_ent local network_found = nil - if c_ent == nil or c_ent.valid == false then - c_ent = p.selected - end + if c_ent == nil or c_ent.valid == false then c_ent = p.selected end if c_ent == nil or c_ent.valid == false then result = wire_name .. " , " .. " no ent " elseif wire_type == "red-wire" then @@ -124,7 +141,10 @@ function drag_wire_and_read(pindex) else network_found = network_found.network_id end - result = " Connected " .. localising.get(target_ent, pindex) .. " to green circuit network ID " .. network_found + result = " Connected " + .. localising.get(target_ent, pindex) + .. " to green circuit network ID " + .. network_found else result = " Disconnected " .. wire_name end @@ -133,15 +153,14 @@ function drag_wire_and_read(pindex) local target_ent = drag_target.target_entity local target_network = drag_target.target_wire_id network_found = c_ent.electric_network_id - if network_found == nil then - network_found = "nil" - end + if network_found == nil then network_found = "nil" end result = " Connected " .. localising.get(target_ent, pindex) .. " to electric network ID " .. network_found - elseif (c_ent ~= nil and c_ent.name == "power-switch") or (last_c_ent ~= nil and last_c_ent.valid and last_c_ent.name == "power-switch") then + elseif + (c_ent ~= nil and c_ent.name == "power-switch") + or (last_c_ent ~= nil and last_c_ent.valid and last_c_ent.name == "power-switch") + then network_found = c_ent.electric_network_id - if network_found == nil then - network_found = "nil" - end + if network_found == nil then network_found = "nil" end result = " Wiring power switch" --result = " Connected " .. localising.get(c_ent,pindex) .. " to electric network ID " .. network_found else @@ -166,26 +185,18 @@ function wire_neighbours_info(ent, read_network_ids) for i, pole in ipairs(ent.neighbours.copper) do local dir = fa_utils.get_direction_biased(pole.position, ent.position) local dist = util.distance(pole.position, ent.position) - if neighbour_count > 0 then - result = result .. " and " - end + if neighbour_count > 0 then result = result .. " and " end local id = pole.electric_network_id - if id == nil then - id = "nil" - end + if id == nil then id = "nil" end result = result .. math.ceil(dist) .. " tiles " .. fa_utils.direction_lookup(dir) - if read_network_ids == true then - result = result .. " to electric network number " .. id - end + if read_network_ids == true then result = result .. " to electric network number " .. id end result = result .. ", " neighbour_count = neighbour_count + 1 end for i, nbr in ipairs(ent.neighbours.red) do local dir = fa_utils.get_direction_biased(nbr.position, ent.position) local dist = util.distance(nbr.position, ent.position) - if neighbour_count > 0 then - result = result .. " and " - end + if neighbour_count > 0 then result = result .. " and " end result = result .. " red wire " .. math.ceil(dist) .. " tiles " .. fa_utils.direction_lookup(dir) if nbr.type == "electric-pole" then local id = nbr.get_circuit_network(defines.wire_type.red, defines.circuit_connector_id.electric_pole) @@ -194,9 +205,7 @@ function wire_neighbours_info(ent, read_network_ids) else id = id.network_id end - if read_network_ids == true then - result = result .. " network number " .. id - end + if read_network_ids == true then result = result .. " network number " .. id end end result = result .. ", " neighbour_count = neighbour_count + 1 @@ -204,9 +213,7 @@ function wire_neighbours_info(ent, read_network_ids) for i, nbr in ipairs(ent.neighbours.green) do local dir = fa_utils.get_direction_biased(nbr.position, ent.position) local dist = util.distance(nbr.position, ent.position) - if neighbour_count > 0 then - result = result .. " and " - end + if neighbour_count > 0 then result = result .. " and " end result = result .. " green wire " .. math.ceil(dist) .. " tiles " .. fa_utils.direction_lookup(dir) if nbr.type == "electric-pole" then local id = nbr.get_circuit_network(defines.wire_type.green, defines.circuit_connector_id.electric_pole) @@ -215,9 +222,7 @@ function wire_neighbours_info(ent, read_network_ids) else id = id.network_id end - if read_network_ids == true then - result = result .. " network number " .. id - end + if read_network_ids == true then result = result .. " network number " .. id end end result = result .. ", " neighbour_count = neighbour_count + 1 @@ -227,9 +232,7 @@ function wire_neighbours_info(ent, read_network_ids) end function localise_signal_name(signal, pindex) - if signal == nil then - return "nil" - end + if signal == nil then return "nil" end local sig_name = signal.name local sig_type = signal.type if sig_name == nil then @@ -241,21 +244,15 @@ function localise_signal_name(signal, pindex) sig_type = "nil" elseif sig_type == "item" then sig_name = localising.get(game.item_prototypes[signal.name], pindex) - if sig_name == nil then - sig_name = signal.name - end + if sig_name == nil then sig_name = signal.name end elseif sig_type == "fluid" then sig_name = localising.get(game.fluid_prototypes[signal.name], pindex) - if sig_name == nil then - sig_name = signal.name - end + if sig_name == nil then sig_name = signal.name end elseif sig_type == "virtual" then sig_name = localising.get(game.virtual_signal_prototypes[signal.name], pindex) - if sig_name == nil then - sig_name = signal.name - end + if sig_name == nil then sig_name = signal.name end end - local result = (sig_name) + local result = sig_name return result end @@ -264,9 +261,7 @@ function constant_combinator_count_valid_signals(ent) local combinator = ent.get_control_behavior() local max_signals_count = combinator.signals_count for i = 1, max_signals_count, 1 do - if combinator.get_signal(i).signal ~= nil then - count = count + 1 - end + if combinator.get_signal(i).signal ~= nil then count = count + 1 end end return count end @@ -275,9 +270,7 @@ function constant_combinator_get_first_empty_slot_id(ent) local combinator = ent.get_control_behavior() local max_signals_count = combinator.signals_count for i = 1, max_signals_count, 1 do - if combinator.get_signal(i).signal == nil then - return i - end + if combinator.get_signal(i).signal == nil then return i end end return max_signals_count end @@ -300,9 +293,7 @@ function constant_combinator_signals_info(ent, pindex) local signal = combinator.get_signal(i) if signal.signal ~= nil then local signal_name = localise_signal_name(signal.signal, pindex) - if i > 1 then - result = result .. " and " - end + if i > 1 then result = result .. " and " end result = result .. signal_name .. " times " .. signal.count .. ", " end end @@ -312,19 +303,19 @@ function constant_combinator_signals_info(ent, pindex) end function constant_combinator_add_stack_signal(ent, stack, pindex) - local combinator = ent.get_control_behavior() + local combinator = ent.get_control_behavior() local first_empty_slot = constant_combinator_get_first_empty_slot_id(ent) - local new_signal_id = { type = "item", name = stack.name } - local new_signal = { signal = new_signal_id, count = 1 } + local new_signal_id = { type = "item", name = stack.name } + local new_signal = { signal = new_signal_id, count = 1 } combinator.set_signal(first_empty_slot, new_signal) printout("Added signal for " .. localising.get(stack, pindex), pindex) end function constant_combinator_add_selector_signal(prototype, signal_type, ent, pindex) - local combinator = ent.get_control_behavior() + local combinator = ent.get_control_behavior() local first_empty_slot = constant_combinator_get_first_empty_slot_id(ent) - local new_signal_id = { type = signal_type, name = prototype.name } - local new_signal = { signal = new_signal_id, count = 1 } + local new_signal_id = { type = signal_type, name = prototype.name } + local new_signal = { signal = new_signal_id, count = 1 } combinator.set_signal(first_empty_slot, new_signal) printout("Added signal for " .. localising.get(prototype, pindex), pindex) end @@ -347,11 +338,11 @@ end function constant_combinator_type_last_signal_count(pindex, ent) players[pindex].signal_selector = {} players[pindex].signal_selector.ent = ent - local frame = game.get_player(pindex).gui.screen.add { type = "frame", name = "circuit-condition-constant" } + local frame = game.get_player(pindex).gui.screen.add({ type = "frame", name = "circuit-condition-constant" }) frame.bring_to_front() frame.force_auto_center() frame.focus() - local input = frame.add { type = "textfield", name = "input" } + local input = frame.add({ type = "textfield", name = "input" }) input.focus() return "Type in a number press 'ENTER' to confirm, or press 'ESC' to exit" end @@ -430,9 +421,7 @@ function get_circuit_read_mode_name(ent) else -- Try to go through the new code path. local possible_new = read_mode_multistate_call(ent, "current") - if possible_new then - result = possible_new - end + if possible_new then result = possible_new end end return result @@ -530,13 +519,13 @@ function get_circuit_operation_mode_name(ent) result = "Nil" end elseif ent.type == "rail-signal" then - result = "None" --"Undefined"--**laterdo + result = "None" --"Undefined"--**laterdo elseif ent.type == "train-stop" then result = "Send signals to parked train" --"Undefined"--**laterdo elseif ent.type == "mining-drill" then - result = "None" --"Undefined"--**laterdo + result = "None" --"Undefined"--**laterdo elseif ent.type == "pumpjack" then - result = "None" --"Undefined"--**laterdo + result = "None" --"Undefined"--**laterdo elseif ent.type == "power-switch" then if control.circuit_condition ~= nil or control.disabled == true then result = "Enable with condition" @@ -667,9 +656,7 @@ function read_circuit_condition(ent, comparator_in_words) local result = "" if cond.second_signal == nil then second_signal_name = cond.constant - if cond.constant == nil then - second_signal_name = 0 - end + if cond.constant == nil then second_signal_name = 0 end end if comparator_in_words == true then if comparator == "=" then @@ -766,9 +753,9 @@ function play_selected_speaker_note(ent, mute) end local note_id = control.circuit_parameters.note_id if note_id < 1 then - note_id = 1 - local params = control.circuit_parameters - params.note_id = 1 + note_id = 1 + local params = control.circuit_parameters + params.note_id = 1 control.circuit_parameters = params end if mute ~= true then @@ -835,8 +822,11 @@ function circuit_network_menu(pindex, ent_in, menu_index, clicked, other_input) --First 3 lines of the menus are in common if index == 0 then --Menu info - local result = " Circuit network " .. nw_name .. ", menu for " .. localising.get(ent, pindex) - .. ", Navigate up and down with 'W' and 'S' and select an option with 'LEFT BRACKET', or exit with 'ESC'" + local result = " Circuit network " + .. nw_name + .. ", menu for " + .. localising.get(ent, pindex) + .. ", Navigate up and down with 'W' and 'S' and select an option with 'LEFT BRACKET', or exit with 'ESC'" printout(result, pindex) elseif index == 1 then --List all active signals of this network @@ -849,20 +839,14 @@ function circuit_network_menu(pindex, ent_in, menu_index, clicked, other_input) end local result = "" if nwr ~= nil then - if nwg ~= nil then - result = result .. "Red network: " - end + if nwg ~= nil then result = result .. "Red network: " end result = result .. circuit_network_signals_info(pindex, nwr) end if nwg ~= nil then - if nwr ~= nil then - result = result .. "Green network: " - end + if nwr ~= nil then result = result .. "Green network: " end result = result .. circuit_network_signals_info(pindex, nwg) end - if result == "" then - result = "No signals at the moment" - end + if result == "" then result = "No signals at the moment" end printout(result, pindex) end elseif index == 2 then @@ -876,20 +860,14 @@ function circuit_network_menu(pindex, ent_in, menu_index, clicked, other_input) end local result = "" if nwr ~= nil then - if nwg ~= nil then - result = result .. "Red network: " - end + if nwg ~= nil then result = result .. "Red network: " end result = result .. circuit_network_members_info(pindex, ent, defines.wire_type.red) end if nwg ~= nil then - if nwr ~= nil then - result = result .. "Green network: " - end + if nwr ~= nil then result = result .. "Green network: " end result = result .. circuit_network_members_info(pindex, ent, defines.wire_type.green) end - if result == "" then - result = "Error: No network" - end + if result == "" then result = "Error: No network" end printout(result, pindex) end elseif index == 3 then @@ -903,20 +881,14 @@ function circuit_network_menu(pindex, ent_in, menu_index, clicked, other_input) end local result = "" if nwr ~= nil then - if nwg ~= nil then - result = result .. "Red network: " - end + if nwg ~= nil then result = result .. "Red network: " end result = result .. circuit_network_neighbors_info(pindex, ent, defines.wire_type.red) end if nwg ~= nil then - if nwr ~= nil then - result = result .. "Green network: " - end + if nwr ~= nil then result = result .. "Green network: " end result = result .. circuit_network_neighbors_info(pindex, ent, defines.wire_type.green) end - if result == "" then - result = "Error: No network" - end + if result == "" then result = "Error: No network" end printout(result, pindex) end end @@ -925,7 +897,7 @@ function circuit_network_menu(pindex, ent_in, menu_index, clicked, other_input) --Menu for electric poles if index > 3 then --(inventory edge: play sound and set index and call this menu again) - p.play_sound { path = "inventory-edge" } + p.play_sound({ path = "inventory-edge" }) players[pindex].circuit_network_menu.index = 3 circuit_network_menu(pindex, ent, players[pindex].circuit_network_menu.index, false, false) end @@ -954,7 +926,7 @@ function circuit_network_menu(pindex, ent_in, menu_index, clicked, other_input) else result = constant_combinator_type_last_signal_count(pindex, ent) printout(result, pindex) - p.play_sound { path = "Inventory-Move" } + p.play_sound({ path = "Inventory-Move" }) end elseif index == 7 then --Delete the last signal @@ -968,7 +940,7 @@ function circuit_network_menu(pindex, ent_in, menu_index, clicked, other_input) if not clicked then printout("Toggle switch", pindex) else - ent.get_control_behavior().enabled = not (ent.get_control_behavior().enabled) + ent.get_control_behavior().enabled = not ent.get_control_behavior().enabled local enabled = ent.get_control_behavior().enabled if enabled == true then printout("Switched on", pindex) @@ -978,7 +950,7 @@ function circuit_network_menu(pindex, ent_in, menu_index, clicked, other_input) end elseif index > 8 then --(inventory edge: play sound and set index and call this menu again) - p.play_sound { path = "inventory-edge" } + p.play_sound({ path = "inventory-edge" }) players[pindex].circuit_network_menu.index = 8 circuit_network_menu(pindex, ent, players[pindex].circuit_network_menu.index, false, false) end @@ -994,15 +966,17 @@ function circuit_network_menu(pindex, ent_in, menu_index, clicked, other_input) printout("No circuit network connected", pindex) return end - local control_has_no_circuit_conditions = ent.type == "container" or ent.type == "logistic-container" or - ent.type == "storage-tank" or ent.type == "rail-chain-signal" or ent.type == "accumulator" or - ent.type == "roboport" or ent.type == "constant-combinator" + local control_has_no_circuit_conditions = ent.type == "container" + or ent.type == "logistic-container" + or ent.type == "storage-tank" + or ent.type == "rail-chain-signal" + or ent.type == "accumulator" + or ent.type == "roboport" + or ent.type == "constant-combinator" local circuit_cond = nil local read_mode = get_circuit_read_mode_name(ent) local op_mode, uses_condition = get_circuit_operation_mode_name(ent) - if control_has_no_circuit_conditions == false then - circuit_cond = control.circuit_condition - end + if control_has_no_circuit_conditions == false then circuit_cond = control.circuit_condition end if index == 4 then --Read machine behavior summary if not clicked then @@ -1011,9 +985,7 @@ function circuit_network_menu(pindex, ent_in, menu_index, clicked, other_input) local result = "" result = result .. "Reading mode: " .. read_mode .. ", " result = result .. "Operation mode: " .. op_mode .. ", " - if uses_condition == true then - result = result .. read_circuit_condition(ent, true) - end + if uses_condition == true then result = result .. read_circuit_condition(ent, true) end printout(result, pindex) end elseif index == 5 then @@ -1023,10 +995,8 @@ function circuit_network_menu(pindex, ent_in, menu_index, clicked, other_input) else local result, changed = toggle_circuit_read_mode(ent) printout(result, pindex) - p.play_sound { path = "Inventory-Move" } - if changed == false then - p.play_sound { path = "inventory-edge" } - end + p.play_sound({ path = "Inventory-Move" }) + if changed == false then p.play_sound({ path = "inventory-edge" }) end end elseif index == 6 then --Toggle machine control mode @@ -1035,10 +1005,8 @@ function circuit_network_menu(pindex, ent_in, menu_index, clicked, other_input) else local result, changed = toggle_circuit_operation_mode(ent) printout(result, pindex) - p.play_sound { path = "Inventory-Move" } - if changed == false then - p.play_sound { path = "inventory-edge" } - end + p.play_sound({ path = "Inventory-Move" }) + if changed == false then p.play_sound({ path = "inventory-edge" }) end end elseif index == 7 then --Toggle enabled condition comparing rule @@ -1046,11 +1014,9 @@ function circuit_network_menu(pindex, ent_in, menu_index, clicked, other_input) printout("Toggle enabled condition comparing rule ", pindex) else local result = "Not using a condition" - if uses_condition == true then - result = toggle_condition_comparator(ent, pindex, true) - end + if uses_condition == true then result = toggle_condition_comparator(ent, pindex, true) end printout(result, pindex) - p.play_sound { path = "Inventory-Move" } + p.play_sound({ path = "Inventory-Move" }) end elseif index == 8 then --Set enabled condition first signal @@ -1063,7 +1029,7 @@ function circuit_network_menu(pindex, ent_in, menu_index, clicked, other_input) open_signal_selector(pindex, ent, true) end printout(result, pindex) - p.play_sound { path = "Inventory-Move" } + p.play_sound({ path = "Inventory-Move" }) end elseif index == 9 then --Set enabled condition second signal @@ -1076,7 +1042,7 @@ function circuit_network_menu(pindex, ent_in, menu_index, clicked, other_input) open_signal_selector(pindex, ent, false) end printout(result, pindex) - p.play_sound { path = "Inventory-Move" } + p.play_sound({ path = "Inventory-Move" }) end elseif index == 10 then --Set enabled condition second signal as a constant number @@ -1084,11 +1050,9 @@ function circuit_network_menu(pindex, ent_in, menu_index, clicked, other_input) printout("Set a constant number for enabled condition second signal", pindex) else local result = "Not using a condition" - if uses_condition == true then - result = type_circuit_condition_constant(pindex, ent) - end + if uses_condition == true then result = type_circuit_condition_constant(pindex, ent) end printout(result, pindex) - p.play_sound { path = "Inventory-Move" } + p.play_sound({ path = "Inventory-Move" }) end else if ent.type ~= "programmable-speaker" or (ent.type == "programmable-speaker" and control == nil) then @@ -1096,7 +1060,7 @@ function circuit_network_menu(pindex, ent_in, menu_index, clicked, other_input) printout("The rest of this menu is for network connected programmable speakers only", pindex) elseif index == 12 then --(inventory edge: play sound and set index and call this menu again) - p.play_sound { path = "inventory-edge" } + p.play_sound({ path = "inventory-edge" }) players[pindex].circuit_network_menu.index = 11 circuit_network_menu(pindex, ent, players[pindex].circuit_network_menu.index, false, false) end @@ -1128,9 +1092,7 @@ function circuit_network_menu(pindex, ent_in, menu_index, clicked, other_input) else local params = ent.parameters params.playback_volume = params.playback_volume + 0.1 - if params.playback_volume > 1 then - params.playback_volume = 1 - end + if params.playback_volume > 1 then params.playback_volume = 1 end ent.parameters = params printout(ent.parameters.playback_volume, pindex) play_selected_speaker_note(ent) @@ -1142,9 +1104,7 @@ function circuit_network_menu(pindex, ent_in, menu_index, clicked, other_input) else local params = ent.parameters params.playback_volume = params.playback_volume - 0.1 - if params.playback_volume < 0 then - params.playback_volume = 0 - end + if params.playback_volume < 0 then params.playback_volume = 0 end ent.parameters = params printout(ent.parameters.playback_volume, pindex) play_selected_speaker_note(ent) @@ -1265,7 +1225,7 @@ function circuit_network_menu(pindex, ent_in, menu_index, clicked, other_input) end elseif index == 20 then --(inventory edge: play sound and set index and call this menu again) - p.play_sound { path = "inventory-edge" } + p.play_sound({ path = "inventory-edge" }) players[pindex].circuit_network_menu.index = 19 circuit_network_menu(pindex, ent, players[pindex].circuit_network_menu.index, false, false) end @@ -1278,9 +1238,7 @@ end CIRCUIT_NETWORK_MENU_LENGTH = 20 function circuit_network_menu_open(pindex, ent) - if players[pindex].vanilla_mode then - return - end + if players[pindex].vanilla_mode then return end --Set the player menu tracker to this menu players[pindex].menu = "circuit_network_menu" players[pindex].in_menu = true @@ -1288,11 +1246,11 @@ function circuit_network_menu_open(pindex, ent) --Set the menu line counter to 0 players[pindex].circuit_network_menu = { - index = 0 + index = 0, } --Play sound - game.get_player(pindex).play_sound { path = "Open-Inventory-Sound" } + game.get_player(pindex).play_sound({ path = "Open-Inventory-Sound" }) --Load menu local cn_menu = players[pindex].circuit_network_menu @@ -1309,17 +1267,13 @@ function circuit_network_menu_close(pindex, mute_in) players[pindex].circuit_network_menu.index = 0 --play sound - if not mute then - game.get_player(pindex).play_sound { path = "Close-Inventory-Sound" } - end + if not mute then game.get_player(pindex).play_sound({ path = "Close-Inventory-Sound" }) end --Close GUIs if game.get_player(pindex).gui.screen["signal-name-enter"] ~= nil then game.get_player(pindex).gui.screen["signal-name-enter"].destroy() end - if game.get_player(pindex).opened ~= nil then - game.get_player(pindex).opened = nil - end + if game.get_player(pindex).opened ~= nil then game.get_player(pindex).opened = nil end end --Reads the total list of the circuit network neighbors of this entity. Gives details. @@ -1334,17 +1288,17 @@ function circuit_network_neighbors_info(pindex, ent, wire_type) end local connected_circuit_count = ent.get_circuit_network(wire_type).connected_circuit_count local members_list = add_neighbors_to_circuit_network_member_list({}, ent, color, 1, 2) - if members_list == nil or #members_list == 0 then - return "Error: No members" - end + if members_list == nil or #members_list == 0 then return "Error: No members" end local result = "Connected to " for i, member in ipairs(members_list) do if member.unit_number ~= ent.unit_number then - result = result .. - localising.get(member, pindex) .. - " at " .. - math.ceil(util.distance(member.position, ent.position)) .. - " " .. fa_utils.direction_lookup(fa_utils.get_direction_biased(member.position, ent.position)) .. ", " + result = result + .. localising.get(member, pindex) + .. " at " + .. math.ceil(util.distance(member.position, ent.position)) + .. " " + .. fa_utils.direction_lookup(fa_utils.get_direction_biased(member.position, ent.position)) + .. ", " end end return result @@ -1362,9 +1316,7 @@ function circuit_network_members_info(pindex, ent, wire_type) end local connected_circuit_count = ent.get_circuit_network(wire_type).connected_circuit_count local members_list = add_neighbors_to_circuit_network_member_list({}, ent, color, 1, 10) - if members_list == nil or #members_list == 0 then - return "Error: No members" - end + if members_list == nil or #members_list == 0 then return "Error: No members" end local pole_counter = 0 local ent_counter = 0 local result = "Total of " .. connected_circuit_count .. " members, including " @@ -1376,9 +1328,7 @@ function circuit_network_members_info(pindex, ent, wire_type) result = result .. localising.get(member, pindex) .. ", " end end - if ent_counter > 0 then - result = result .. " and " - end + if ent_counter > 0 then result = result .. " and " end result = result .. pole_counter .. " electric poles, " return result end @@ -1391,14 +1341,10 @@ function add_neighbors_to_circuit_network_member_list(list_in, ent_in, color_in, local iteration = iteration_in or 1 --Stop after iteration_limit to prevent UPS drain - if iteration > iteration_limit then - return list - end + if iteration > iteration_limit then return list end --Add this ent to the list if not already - if ent == nil or ent.valid == false then - return list - end + if ent == nil or ent.valid == false then return list end local num = ent.unit_number local exists = false for i, list_ent in ipairs(list) do @@ -1408,16 +1354,12 @@ function add_neighbors_to_circuit_network_member_list(list_in, ent_in, color_in, return list end end - if exists == false then - table.insert(list, ent) - end + if exists == false then table.insert(list, ent) end --Get all circuit neighbors and run again iteration = iteration + 1 local neighbors = ent.circuit_connected_entities[color] - if neighbors == nil or #neighbors == 0 then - return list - end + if neighbors == nil or #neighbors == 0 then return list end for i, neighbor_ent in ipairs(neighbors) do add_neighbors_to_circuit_network_member_list(list, neighbor_ent, color, iteration, iteration_limit) end @@ -1481,9 +1423,7 @@ function build_signal_selector(pindex) players[pindex].signal_selector.signals[group] = fa_utils.get_iterable_array(game.virtual_signal_prototypes) else for j, item in ipairs(items) do - if item.group.name == group then - table.insert(players[pindex].signal_selector.signals[group], item) - end + if item.group.name == group then table.insert(players[pindex].signal_selector.signals[group], item) end end end --game.print("Created group " .. group .. " with " .. #players[pindex].signal_selector.signals[group] .. " items ") @@ -1499,9 +1439,7 @@ end --Returns the currently selected item/fluid/virtual signal prototype and the signal type function get_selected_signal_slot_with_type(pindex) - if players[pindex].signal_selector == nil then - build_signal_selector(pindex) - end + if players[pindex].signal_selector == nil then build_signal_selector(pindex) end local group_index = players[pindex].signal_selector.group_index local signal_index = players[pindex].signal_selector.signal_index local group_name = players[pindex].signal_selector.group_names[group_index] @@ -1547,7 +1485,7 @@ function apply_selected_signal_to_enabled_condition(pindex, ent, first) local start_phrase = "" local prototype, signal_type = get_selected_signal_slot_with_type(pindex) if prototype == nil or prototype.valid == false then - game.get_player(pindex).play_sound { path = "utility/cannot_build" } + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) return end if ent.type == "constant-combinator" then @@ -1570,27 +1508,29 @@ function apply_selected_signal_to_enabled_condition(pindex, ent, first) players[pindex].menu = "circuit_network_menu" players[pindex].signal_selector = nil printout( - set_message .. localising.get(prototype, pindex) .. ", condition now checks if " .. read_circuit_condition(ent, true), - pindex) + set_message + .. localising.get(prototype, pindex) + .. ", condition now checks if " + .. read_circuit_condition(ent, true), + pindex + ) end function type_circuit_condition_constant(pindex, ent) players[pindex].signal_selector = {} players[pindex].signal_selector.ent = ent - local frame = game.get_player(pindex).gui.screen.add { type = "frame", name = "circuit-condition-constant" } + local frame = game.get_player(pindex).gui.screen.add({ type = "frame", name = "circuit-condition-constant" }) frame.bring_to_front() frame.force_auto_center() frame.focus() - local input = frame.add { type = "textfield", name = "input" } + local input = frame.add({ type = "textfield", name = "input" }) input.focus() return "Type in a number for comparing and press 'ENTER' to confirm, or press 'ESC' to exit" end function signal_selector_group_up(pindex) - if players[pindex].signal_selector == nil then - build_signal_selector(pindex) - end - game.get_player(pindex).play_sound { path = "Inventory-Move" } + if players[pindex].signal_selector == nil then build_signal_selector(pindex) end + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) local jumps = 1 if players[pindex].signal_selector.group_index <= 1 then players[pindex].signal_selector.group_index = #players[pindex].signal_selector.group_names @@ -1620,10 +1560,8 @@ function signal_selector_group_up(pindex) end function signal_selector_group_down(pindex) - if players[pindex].signal_selector == nil then - build_signal_selector(pindex) - end - game.get_player(pindex).play_sound { path = "Inventory-Move" } + if players[pindex].signal_selector == nil then build_signal_selector(pindex) end + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) local jumps = 1 if players[pindex].signal_selector.group_index < #players[pindex].signal_selector.group_names then players[pindex].signal_selector.group_index = players[pindex].signal_selector.group_index + 1 @@ -1660,7 +1598,7 @@ function signal_selector_signal_next(pindex) if players[pindex].signal_selector.signal_index < #group then players[pindex].signal_selector.signal_index = players[pindex].signal_selector.signal_index + 1 else - game.get_player(pindex).play_sound { path = "inventory-wrap-around" } + game.get_player(pindex).play_sound({ path = "inventory-wrap-around" }) players[pindex].signal_selector.signal_index = 1 end end @@ -1673,7 +1611,7 @@ function signal_selector_signal_prev(pindex) if players[pindex].signal_selector.signal_index > 1 then players[pindex].signal_selector.signal_index = players[pindex].signal_selector.signal_index - 1 else - game.get_player(pindex).play_sound { path = "inventory-wrap-around" } + game.get_player(pindex).play_sound({ path = "inventory-wrap-around" }) players[pindex].signal_selector.signal_index = #group end end diff --git a/scripts/combat.lua b/scripts/combat.lua index f00cebd2..c78b1c12 100644 --- a/scripts/combat.lua +++ b/scripts/combat.lua @@ -1,30 +1,37 @@ ---Here: functions relating to combat, repair packs +--Here: functions relating to combat, repair packs --Does not include event handlers, guns and equipment maanagement -local util = require('util') +local util = require("util") local fa_graphics = require("scripts.graphics") local fa_mouse = require("scripts.mouse") local mod = {} --One-click repair pack usage. -function mod.repair_pack_used(ent,pindex) +function mod.repair_pack_used(ent, pindex) local p = game.get_player(pindex) local stack = p.cursor_stack --Repair the entity found - if ent and ent.valid and ent.is_entity_with_health and ent.get_health_ratio() < 1 - and ent.type ~= "resource" and not ent.force.is_enemy(p.force) and ent.name ~= "character" then - p.play_sound{path = "utility/default_manual_repair"} + if + ent + and ent.valid + and ent.is_entity_with_health + and ent.get_health_ratio() < 1 + and ent.type ~= "resource" + and not ent.force.is_enemy(p.force) + and ent.name ~= "character" + then + p.play_sound({ path = "utility/default_manual_repair" }) local health_diff = ent.prototype.max_health - ent.health local dura = stack.durability or 0 if health_diff < 10 then --free repair for tiny damages - ent.health = ent.prototype.max_health + ent.health = ent.prototype.max_health printout("Fully repaired " .. ent.name, pindex) elseif health_diff < dura then - ent.health = ent.prototype.max_health + ent.health = ent.prototype.max_health stack.drain_durability(health_diff) printout("Fully repaired " .. ent.name, pindex) - else--if health_diff >= dura then + else --if health_diff >= dura then stack.drain_durability(dura) ent.health = ent.health + dura printout("Partially repaired " .. ent.name .. " and consumed a repair pack", pindex) @@ -35,84 +42,115 @@ function mod.repair_pack_used(ent,pindex) end --Tries to repair all relevant entities within a certain distance from the player -function mod.repair_area(radius_in,pindex) +function mod.repair_area(radius_in, pindex) local p = game.get_player(pindex) local stack = p.cursor_stack local repaired_count = 0 local packs_used = 0 - local radius = math.min(radius_in,25) + local radius = math.min(radius_in, 25) if stack.count < 2 then --If you are low on repair packs, stop - printout("You need at least 2 repair packs to repair the area.",pindex) - return + printout("You need at least 2 repair packs to repair the area.", pindex) + return end - local ents = p.surface.find_entities_filtered{position = p.position, radius = radius} - for i,ent in ipairs(ents) do + local ents = p.surface.find_entities_filtered({ position = p.position, radius = radius }) + for i, ent in ipairs(ents) do --Repair the entity found - if ent and ent.valid and ent.is_entity_with_health and ent.get_health_ratio() < 1 - and ent.type ~= "resource" and not ent.force.is_enemy(p.force) and ent.name ~= "character" then - p.play_sound{path = "utility/default_manual_repair"} + if + ent + and ent.valid + and ent.is_entity_with_health + and ent.get_health_ratio() < 1 + and ent.type ~= "resource" + and not ent.force.is_enemy(p.force) + and ent.name ~= "character" + then + p.play_sound({ path = "utility/default_manual_repair" }) local health_diff = ent.prototype.max_health - ent.health local dura = stack.durability or 0 if health_diff < 10 then --free repair for tiny damages - ent.health = ent.prototype.max_health + ent.health = ent.prototype.max_health repaired_count = repaired_count + 1 elseif health_diff < dura then - ent.health = ent.prototype.max_health + ent.health = ent.prototype.max_health stack.drain_durability(health_diff) repaired_count = repaired_count + 1 elseif stack.count < 2 then --If you are low on repair packs, stop - printout("Repaired " .. repaired_count .. " structures using " .. packs_used .. " repair packs, stopped because you are low on repair packs.",pindex) - return + printout( + "Repaired " + .. repaired_count + .. " structures using " + .. packs_used + .. " repair packs, stopped because you are low on repair packs.", + pindex + ) + return else --Finish the current repair pack stack.drain_durability(dura) packs_used = packs_used + 1 ent.health = ent.health + dura - + --Repeat unhtil fully repaired or out of packs while ent.get_health_ratio() < 1 do health_diff = ent.prototype.max_health - ent.health dura = stack.durability or 0 if health_diff < 10 then --free repair for tiny damages - ent.health = ent.prototype.max_health + ent.health = ent.prototype.max_health repaired_count = repaired_count + 1 elseif health_diff < dura then - ent.health = ent.prototype.max_health + ent.health = ent.prototype.max_health stack.drain_durability(health_diff) repaired_count = repaired_count + 1 elseif stack.count < 2 then --If you are low on repair packs, stop - printout("Repaired " .. repaired_count .. " structures using " .. packs_used .. " repair packs, stopped because you are low on repair packs.",pindex) - return + printout( + "Repaired " + .. repaired_count + .. " structures using " + .. packs_used + .. " repair packs, stopped because you are low on repair packs.", + pindex + ) + return else --Finish the current repair pack stack.drain_durability(dura) packs_used = packs_used + 1 ent.health = ent.health + dura end - end + end end end end if repaired_count == 0 then - printout("Nothing to repair within " .. radius .. " tiles of you.",pindex) + printout("Nothing to repair within " .. radius .. " tiles of you.", pindex) return end - printout("Repaired all " .. repaired_count .. " structures within " .. radius .. " tiles of you, using " .. packs_used .. " repair packs.",pindex) + printout( + "Repaired all " + .. repaired_count + .. " structures within " + .. radius + .. " tiles of you, using " + .. packs_used + .. " repair packs.", + pindex + ) end --Plays enemy proximity alert sounds. Frequency is determined by distance andmode, and intensity is determined by the threat level. function mod.check_and_play_enemy_alert_sound(mode_in) for pindex, player in pairs(players) do - local mode =mode_in or 1 + local mode = mode_in or 1 local p = game.get_player(pindex) if p ~= nil and p.valid then - local nearest_enemy = p.surface.find_nearest_enemy{position = p.position, max_distance = 100, force = p.force} + local nearest_enemy = + p.surface.find_nearest_enemy({ position = p.position, max_distance = 100, force = p.force }) local dist = -1 if nearest_enemy ~= nil and nearest_enemy.valid then - dist = math.floor(util.distance(nearest_enemy.position,p.position)) + dist = math.floor(util.distance(nearest_enemy.position, p.position)) else return end @@ -122,100 +160,80 @@ function mod.check_and_play_enemy_alert_sound(mode_in) local x_offset = 0 if math.abs(diffx) > 2 * math.abs(diffy) then --Counts as east or west - if diffx > 0 then + if diffx > 0 then x_offset = 7 elseif diffx < 0 then x_offset = -7 end end - local pos = {x = p.position.x + x_offset, y = p.position.y } - + local pos = { x = p.position.x + x_offset, y = p.position.y } + --Play sounds according tomode - if mode == 1 then -- Nearest enemy is far (lowest freq) - if dist < 100 then - p.play_sound{path = "alert-enemy-presence-low", position = pos} - end + if mode == 1 then -- Nearest enemy is far (lowest freq) + if dist < 100 then p.play_sound({ path = "alert-enemy-presence-low", position = pos }) end --Additional alert if there are more than 5 enemies nearby local enemies = p.surface.find_enemy_units(p.position, 25, p.force) if #enemies > 5 then - p.play_sound{path = "alert-enemy-presence-high", position = pos} + p.play_sound({ path = "alert-enemy-presence-high", position = pos }) else - for i, enemy in ipairs(enemies) do + for i, enemy in ipairs(enemies) do --Also check for strong enemies: big/huge biters, huge spitters, medium or larger worms, not spawners if enemy.prototype.max_health > 360 then - p.play_sound{path = "alert-enemy-presence-high", position = pos} + p.play_sound({ path = "alert-enemy-presence-high", position = pos }) return end end end elseif mode == 2 then -- Nearest enemy is closer (medium freq) - if dist < 50 then - p.play_sound{path = "alert-enemy-presence-low", position = pos} - end + if dist < 50 then p.play_sound({ path = "alert-enemy-presence-low", position = pos }) end --Additional alert if there are more than 10 enemies nearby local enemies = p.surface.find_enemy_units(p.position, 25, p.force) - if #enemies > 10 then - p.play_sound{path = "alert-enemy-presence-high", position = pos} - end + if #enemies > 10 then p.play_sound({ path = "alert-enemy-presence-high", position = pos }) end elseif mode == 3 then -- Nearest enemy is too close (highest freq) - if dist < 25 then - p.play_sound{path = "alert-enemy-presence-low", position = pos} - end + if dist < 25 then p.play_sound({ path = "alert-enemy-presence-low", position = pos }) end end end end end --Locks the cursor to the nearest enemy within 50 tiles. Also plays a sound if the enemy is within range of the gun in hand. -function mod.aim_gun_at_nearest_enemy(pindex,enemy_in) +function mod.aim_gun_at_nearest_enemy(pindex, enemy_in) local p = game.get_player(pindex) - if p == nil or p.character == nil or p.character.valid == false then - return - end - local gun_index = p.character.selected_gun_index - local guns_inv = p.get_inventory(defines.inventory.character_guns) - local ammo_inv = game.get_player(pindex).get_inventory(defines.inventory.character_ammo) - local gun_stack = guns_inv[gun_index] + if p == nil or p.character == nil or p.character.valid == false then return end + local gun_index = p.character.selected_gun_index + local guns_inv = p.get_inventory(defines.inventory.character_guns) + local ammo_inv = game.get_player(pindex).get_inventory(defines.inventory.character_ammo) + local gun_stack = guns_inv[gun_index] local ammo_stack = ammo_inv[gun_index] local enemy = enemy_in --Return if missing a gun or ammo - if gun_stack == nil or not gun_stack.valid_for_read or not gun_stack.valid then - return false - end - if ammo_stack == nil or not ammo_stack.valid_for_read or not ammo_stack.valid then - return false - end + if gun_stack == nil or not gun_stack.valid_for_read or not gun_stack.valid then return false end + if ammo_stack == nil or not ammo_stack.valid_for_read or not ammo_stack.valid then return false end --Return if in Cursormode - if players[pindex].cursor then - return false - end + if players[pindex].cursor then return false end --Return if in a menu - if players[pindex].in_menu then - return false - end + if players[pindex].in_menu then return false end --Check for nearby enemies if enemy_in == nil or not enemy_in.valid then - enemy = p.surface.find_nearest_enemy{position = p.position, max_distance = 50, force = p.force} + enemy = p.surface.find_nearest_enemy({ position = p.position, max_distance = 50, force = p.force }) end - if enemy == nil or not enemy.valid then - return false - end - --Play a sound when the enemy is within range of the gun + if enemy == nil or not enemy.valid then return false end + --Play a sound when the enemy is within range of the gun local range = gun_stack.prototype.attack_parameters.range - local dist = util.distance(p.position,enemy.position) - if dist < range and p.character.can_shoot(enemy,enemy.position) then - p.play_sound{path = "player-aim-locked",volume_modifier=0.5} + local dist = util.distance(p.position, enemy.position) + if dist < range and p.character.can_shoot(enemy, enemy.position) then + p.play_sound({ path = "player-aim-locked", volume_modifier = 0.5 }) end --Return if there is a gun and ammo combination that already aims by itself if gun_stack.name == "pistol" or gun_stack.name == "submachine-gun" and dist < 10 then --or ammo_stack.name == "rocket" or ammo_stack.name == "explosive-rocket" then - --**Note: normal/explosive rockets only fire when they lock on a target anyway. Meanwhile the SMG auto-aims only when close enough + --**Note: normal/explosive rockets only fire when they lock on a target anyway. Meanwhile the SMG auto-aims only when close enough return true - end + end --If in range, move the cursor onto the enemy to aim the gun - if dist < range then + if dist < range then players[pindex].cursor_pos = enemy.position - fa_mouse.move_mouse_pointer(enemy.position,pindex) - fa_graphics.draw_cursor_highlight(pindex,nil,nil,true) + fa_mouse.move_mouse_pointer(enemy.position, pindex) + fa_graphics.draw_cursor_highlight(pindex, nil, nil, true) end return true end diff --git a/scripts/crafting.lua b/scripts/crafting.lua index 2146281c..399de7ec 100644 --- a/scripts/crafting.lua +++ b/scripts/crafting.lua @@ -1,20 +1,18 @@ --Here: Crafting menu, crafting queue menu, technology menu, related functions -local util = require('util') -local fa_utils = require('scripts.fa-utils') -local localising = require('scripts.localising') +local util = require("util") +local fa_utils = require("scripts.fa-utils") +local localising = require("scripts.localising") local mod = {} --Returns a navigable list of all unlocked recipes, for the recipe categories supported by the selected entity. Optionally can return all unlocked recipes for all categories. function mod.get_recipes(pindex, ent, load_all_categories) - if not ent then - return {} - end - local category_filters={} + if not ent then return {} end + local category_filters = {} --Load the supported recipe categories for this entity for category_name, _ in pairs(ent.prototype.crafting_categories) do - table.insert(category_filters, {filter="category", category=category_name}) + table.insert(category_filters, { filter = "category", category = category_name }) end local all_machine_recipes = game.get_filtered_recipe_prototypes(category_filters) local unlocked_machine_recipes = {} @@ -29,15 +27,13 @@ function mod.get_recipes(pindex, ent, load_all_categories) --Load only the unlocked recipes for recipe_name, recipe in pairs(all_machine_recipes) do if force_recipes[recipe_name] ~= nil and force_recipes[recipe_name].enabled then - if unlocked_machine_recipes[recipe.group.name] == nil then - unlocked_machine_recipes[recipe.group.name]={} - end - table.insert(unlocked_machine_recipes[recipe.group.name],force_recipes[recipe.name]) + if unlocked_machine_recipes[recipe.group.name] == nil then unlocked_machine_recipes[recipe.group.name] = {} end + table.insert(unlocked_machine_recipes[recipe.group.name], force_recipes[recipe.name]) end end - local result={} + local result = {} for group, recipes in pairs(unlocked_machine_recipes) do - table.insert(result,recipes) + table.insert(result, recipes) end return result end @@ -48,22 +44,23 @@ function mod.read_crafting_queue(pindex, start_phrase) if players[pindex].crafting_queue.max ~= 0 then local item = players[pindex].crafting_queue.lua_queue[players[pindex].crafting_queue.index] local recipe_name_only = item.recipe - printout(start_phrase .. localising.get(game.recipe_prototypes[recipe_name_only],pindex) .. " x " .. item.count, pindex) + printout( + start_phrase .. localising.get(game.recipe_prototypes[recipe_name_only], pindex) .. " x " .. item.count, + pindex + ) else printout(start_phrase .. "Blank", pindex) end end ---Returns a count of how many batches of this recipe are listed in the (entire) crafting queue. +--Returns a count of how many batches of this recipe are listed in the (entire) crafting queue. function mod.count_in_crafting_queue(recipe_name, pindex) local count = 0 if game.get_player(pindex).crafting_queue == nil or #game.get_player(pindex).crafting_queue == 0 then return count end for i, item in ipairs(game.get_player(pindex).crafting_queue) do - if item.recipe == recipe_name then - count = count + item.count - end + if item.recipe == recipe_name then count = count + item.count end --game.print(item.recipe .. " vs " .. recipe_name) end return count @@ -78,28 +75,26 @@ function mod.load_crafting_queue(pindex) players[pindex].crafting_queue.index = math.max(1, players[pindex].crafting_queue.index - delta) players[pindex].crafting_queue.max = #players[pindex].crafting_queue.lua_queue else - players[pindex].crafting_queue.index = 1 - players[pindex].crafting_queue.max = 0 + players[pindex].crafting_queue.index = 1 + players[pindex].crafting_queue.max = 0 end else players[pindex].crafting_queue.lua_queue = game.get_player(pindex).crafting_queue - players[pindex].crafting_queue.index = 1 + players[pindex].crafting_queue.index = 1 if players[pindex].crafting_queue.lua_queue ~= nil then - players[pindex].crafting_queue.max = # players[pindex].crafting_queue.lua_queue + players[pindex].crafting_queue.max = #players[pindex].crafting_queue.lua_queue else players[pindex].crafting_queue.max = 0 end end end ---Returns a count of total recipe batches left in the player crafting queue. +--Returns a count of total recipe batches left in the player crafting queue. function mod.get_crafting_que_total(pindex) local p = game.get_player(pindex) local total_items = 0 - if p.crafting_queue == nil or p.crafting_queue == {} then - return 0 - end - for i,q_item in ipairs(p.crafting_queue) do + if p.crafting_queue == nil or p.crafting_queue == {} then return 0 end + for i, q_item in ipairs(p.crafting_queue) do total_items = total_items + q_item.count end return total_items @@ -108,20 +103,26 @@ end --Reads the currently selected recipe in the player crafting menu. function mod.read_crafting_slot(pindex, start_phrase, new_category) start_phrase = start_phrase or "" - local recipe = players[pindex].crafting.lua_recipes[players[pindex].crafting.category][players[pindex].crafting.index] + local recipe = + players[pindex].crafting.lua_recipes[players[pindex].crafting.category][players[pindex].crafting.index] if recipe.valid == true then - if new_category == true then - start_phrase = start_phrase .. localising.get_alt(recipe.group,pindex) .. ", " - end - printout(start_phrase .. localising.get_recipe_from_name(recipe.name,pindex) .. ", can craft " .. game.get_player(pindex).get_craftable_count(recipe.name), pindex) + if new_category == true then start_phrase = start_phrase .. localising.get_alt(recipe.group, pindex) .. ", " end + printout( + start_phrase + .. localising.get_recipe_from_name(recipe.name, pindex) + .. ", can craft " + .. game.get_player(pindex).get_craftable_count(recipe.name), + pindex + ) else - printout("Blank",pindex) + printout("Blank", pindex) end end --Returns an info string about how many units of which ingredients are missing in order to craft one batch of this recipe. function mod.recipe_missing_ingredients_info(pindex, recipe_in) - local recipe = recipe_in or players[pindex].crafting.lua_recipes[players[pindex].crafting.category][players[pindex].crafting.index] + local recipe = recipe_in + or players[pindex].crafting.lua_recipes[players[pindex].crafting.category][players[pindex].crafting.index] local p = game.get_player(pindex) local inv = p.get_main_inventory() local result = "Missing " @@ -131,15 +132,11 @@ function mod.recipe_missing_ingredients_info(pindex, recipe_in) local needed = ing.amount - on_hand if needed > 0 then missing = missing + 1 - if missing > 1 then - result = result .. " and " - end - result = result .. needed .. " " .. localising.get_item_from_name(ing.name,pindex) + if missing > 1 then result = result .. " and " end + result = result .. needed .. " " .. localising.get_item_from_name(ing.name, pindex) end end - if missing == 0 then - result = "" - end + if missing == 0 then result = "" end return result end @@ -158,7 +155,7 @@ function mod.recipe_raw_ingredients_info(recipe, pindex) end end if is_in_table == false then - --Add a new table entry + --Add a new table entry table.insert(merged_table, ing) end end @@ -195,17 +192,22 @@ function mod.get_raw_ingredients_table(recipe, pindex, count_in) local sub_recipe = game.recipe_prototypes[ing.name] if sub_recipe ~= nil and sub_recipe.valid then --If the sub-recipe cannot be crafted by hand, add this ingredient to the main table - if sub_recipe.category ~= "basic-crafting" and sub_recipe.category ~= "crafting" and sub_recipe.category ~= "" and sub_recipe.category ~= nil then - for i = 1,count,1 do + if + sub_recipe.category ~= "basic-crafting" + and sub_recipe.category ~= "crafting" + and sub_recipe.category ~= "" + and sub_recipe.category ~= nil + then + for i = 1, count, 1 do table.insert(raw_ingredients_table, ing) end else --Check the sub-recipe recursively - local sub_table = mod.get_raw_ingredients_table(sub_recipe, pindex)--, ing.amount) + local sub_table = mod.get_raw_ingredients_table(sub_recipe, pindex) --, ing.amount) if sub_table ~= nil then --Copy the sub_table to the main table for j, ing2 in ipairs(sub_table) do - for i = 1,count,1 do + for i = 1, count, 1 do table.insert(raw_ingredients_table, ing2) end end @@ -213,7 +215,7 @@ function mod.get_raw_ingredients_table(recipe, pindex, count_in) end else --If a sub-recipe does not exist, add this ingredient to the main table - for i = 1,count,1 do + for i = 1, count, 1 do table.insert(raw_ingredients_table, ing) end end @@ -221,4 +223,4 @@ function mod.get_raw_ingredients_table(recipe, pindex, count_in) return raw_ingredients_table end -return mod \ No newline at end of file +return mod diff --git a/scripts/descriptors.lua b/scripts/descriptors.lua index 347b0f61..019807ec 100644 --- a/scripts/descriptors.lua +++ b/scripts/descriptors.lua @@ -21,11 +21,11 @@ moment--we leave documentation of the schema aside. Otherwise this comment will probably become stale very quickly. The exammples here should be reasonably self-explanatory. ]] -local F = require('scripts.field-ref') +local F = require("scripts.field-ref") local dcb = defines.control_behavior -local mod = {} +local mod = {} -- Prototypes, by type. mod.PROTOTYPES = { @@ -36,12 +36,12 @@ mod.PROTOTYPES = { mode_field = F.circuit_hand_read_mode(), disabled_label = "None", choices = { - { dcb.inserter.hand_read_mode.hold, "Reading held items" }, - { dcb.inserter.hand_read_mode.pulse, "pulsing held items" } + { dcb.inserter.hand_read_mode.hold, "Reading held items" }, + { dcb.inserter.hand_read_mode.pulse, "pulsing held items" }, }, }, }, }, } -return mod \ No newline at end of file +return mod diff --git a/scripts/driving.lua b/scripts/driving.lua index 51469f54..676944f2 100644 --- a/scripts/driving.lua +++ b/scripts/driving.lua @@ -1,5 +1,5 @@ ---Here: Functions about driving, mainly cars. ---Note: Some train-specific functions are in rails-and-trains.lua +--Here: Functions about driving, mainly cars. +--Note: Some train-specific functions are in rails-and-trains.lua local util = require("util") local fa_utils = require("scripts.fa-utils") @@ -11,11 +11,9 @@ local mod = {} --Report more info about a vehicle. For trains, this would include the name, ID, and train state. function mod.vehicle_info(pindex) local result = "" - if not game.get_player(pindex).driving then - return "Not in a vehicle." - end - - local vehicle = game.get_player(pindex).vehicle + if not game.get_player(pindex).driving then return "Not in a vehicle." end + + local vehicle = game.get_player(pindex).vehicle local train = game.get_player(pindex).vehicle.train if train == nil then --This is a type of car or tank. @@ -24,23 +22,21 @@ function mod.vehicle_info(pindex) return result else --This is a type of locomotive or wagon. - + --Add the train name result = "On board " .. vehicle.name .. " of train " .. fa_trains.get_train_name(train) .. ", " - + --Add the train state result = result .. fa_trains.get_train_state_info(train) .. ", " - - --Declare destination if any. + + --Declare destination if any. if train.path_end_stop ~= nil then result = result .. " heading to station " .. train.path_end_stop.backer_name .. ", " - -- result = result .. " traveled a distance of " .. train.path.travelled_distance .. " out of " train.path.total_distance " distance, " + -- result = result .. " traveled a distance of " .. train.path.travelled_distance .. " out of " train.path.total_distance " distance, " end - + --Note that more info and options are found in the train menu - if vehicle.name == "locomotive" then - result = result .. " Press LEFT BRACKET to open the train menu. " - end + if vehicle.name == "locomotive" then result = result .. " Press LEFT BRACKET to open the train menu. " end return result end end @@ -51,7 +47,7 @@ function mod.fuel_inventory_info(ent) local itemset = ent.get_fuel_inventory().get_contents() local itemtable = {} for name, count in pairs(itemset) do - table.insert(itemtable, {name = name, count = count}) + table.insert(itemtable, { name = name, count = count }) end table.sort(itemtable, function(k1, k2) return k1.count > k2.count @@ -73,29 +69,23 @@ function mod.check_and_play_driving_alert_sound(pindex, tick, mode_in) for pindex, player in pairs(players) do local mode = mode_in or 1 local p = game.get_player(pindex) - local surf = p.surface - if p == nil or p.valid == false or p.driving == false or p.vehicle == nil then - return false - end + local surf = p.surface + if p == nil or p.valid == false or p.driving == false or p.vehicle == nil then return false end --Return if beeped recently local min_delay = 15 - if players[pindex].last_driving_alert_tick == nil then + if players[pindex].last_driving_alert_tick == nil then players[pindex].last_driving_alert_tick = tick return false end local last_driving_alert_tick = players[pindex].last_driving_alert_tick local time_since = tick - last_driving_alert_tick - if last_driving_alert_tick ~= nil and time_since < min_delay then - return false - end + if last_driving_alert_tick ~= nil and time_since < min_delay then return false end --Scan area "ahead" according to direction local v = p.vehicle local dir = fa_utils.get_heading_value(v) - if v.speed < 0 then - dir = fa_utils.rotate_180(dir) - end - - --Set the trigger distance + if v.speed < 0 then dir = fa_utils.rotate_180(dir) end + + --Set the trigger distance local trigger = 1 if mode == 1 then trigger = 3 @@ -106,77 +96,133 @@ function mod.check_and_play_driving_alert_sound(pindex, tick, mode_in) else trigger = 50 end - + --Scan for entities within the radius local ents_around = {} if p.vehicle.type == "car" then local radius = trigger + 5 --For cars, exclude anything they cannot collide with - ents_around = surf.find_entities_filtered{area = {{v.position.x-radius, v.position.y-radius,},{v.position.x+radius, v.position.y+radius}}, type = {"resource", "highlight-box", "flying-text", "corpse", "straight-rail", "curved-rail", "rail-signal", "rail-chain-signal", "transport-belt", "underground-belt", "splitter", "item-entity", "pipe", "pipe-to-ground", "inserter", "small-electric-pole", "medium-electric-pole"}, invert = true} - elseif p.vehicle.train ~= nil then + ents_around = surf.find_entities_filtered({ + area = { + { v.position.x - radius, v.position.y - radius }, + { v.position.x + radius, v.position.y + radius }, + }, + type = { + "resource", + "highlight-box", + "flying-text", + "corpse", + "straight-rail", + "curved-rail", + "rail-signal", + "rail-chain-signal", + "transport-belt", + "underground-belt", + "splitter", + "item-entity", + "pipe", + "pipe-to-ground", + "inserter", + "small-electric-pole", + "medium-electric-pole", + }, + invert = true, + }) + elseif p.vehicle.train ~= nil then trigger = trigger * 3 local radius = trigger + 5 --For trains, search for anything they can collide with - ents_around = surf.find_entities_filtered{area = {{v.position.x-radius, v.position.y-radius,},{v.position.x+radius, v.position.y+radius}}, type = {"locomotive", "cargo-wagon", "fluid-wagon", "artillery-wagon","character","car","unit"}, invert = false} + ents_around = surf.find_entities_filtered({ + area = { + { v.position.x - radius, v.position.y - radius }, + { v.position.x + radius, v.position.y + radius }, + }, + type = { "locomotive", "cargo-wagon", "fluid-wagon", "artillery-wagon", "character", "car", "unit" }, + invert = false, + }) end - + --Filter entities by direction - local ents_ahead = {} + local ents_ahead = {} for i, ent in ipairs(ents_around) do - local dir_ent = fa_utils.get_direction_biased(ent.position,v.position) + local dir_ent = fa_utils.get_direction_biased(ent.position, v.position) if dir_ent == dir then if p.vehicle.type == "car" and ent.unit_number ~= p.vehicle.unit_number then --For cars, take the entity as it is - table.insert(ents_ahead,ent) + table.insert(ents_ahead, ent) elseif p.vehicle.train ~= nil and ent.unit_number ~= p.vehicle.unit_number then --For trains, the entity must also be near/on rails - local ent_straight_rails = surf.find_entities_filtered{position = ent.position, radius = 2, type = {"straight-rail"}} - local ent_curved_rails = surf.find_entities_filtered{position = ent.position, radius = 4, type = {"curved-rail"}} - if (ent_straight_rails ~= nil and #ent_straight_rails > 0) or (ent_curved_rails ~= nil and #ent_curved_rails > 0) then - if not (ent.train and ent.train.id == v.train.id) then - table.insert(ents_ahead,ent) - end + local ent_straight_rails = + surf.find_entities_filtered({ position = ent.position, radius = 2, type = { "straight-rail" } }) + local ent_curved_rails = + surf.find_entities_filtered({ position = ent.position, radius = 4, type = { "curved-rail" } }) + if + (ent_straight_rails ~= nil and #ent_straight_rails > 0) + or (ent_curved_rails ~= nil and #ent_curved_rails > 0) + then + if not (ent.train and ent.train.id == v.train.id) then table.insert(ents_ahead, ent) end end end - elseif mode < 2 and util.distance(v.position, ent.position) < 5 and (math.abs(dir_ent - dir) == 1 or math.abs(dir_ent - dir) == 7) then + elseif + mode < 2 + and util.distance(v.position, ent.position) < 5 + and (math.abs(dir_ent - dir) == 1 or math.abs(dir_ent - dir) == 7) + then --Take very nearby ents at diagonal directions if p.vehicle.type == "car" and ent.unit_number ~= p.vehicle.unit_number then --For cars, take the entity as it is - table.insert(ents_ahead,ent) + table.insert(ents_ahead, ent) elseif p.vehicle.train ~= nil and ent.unit_number ~= p.vehicle.unit_number then --For trains, the entity must also be near/on rails and not from the same train (if reversing) - local ent_straight_rails = surf.find_entities_filtered{position = ent.position, radius = 2, type = {"straight-rail"}} - local ent_curved_rails = surf.find_entities_filtered{position = ent.position, radius = 4, type = {"curved-rail"}} - if (ent_straight_rails ~= nil and #ent_straight_rails > 0) or (ent_curved_rails ~= nil and #ent_curved_rails > 0) then - if not (ent.train and ent.train.id == v.train.id) then - table.insert(ents_ahead,ent) - end + local ent_straight_rails = + surf.find_entities_filtered({ position = ent.position, radius = 2, type = { "straight-rail" } }) + local ent_curved_rails = + surf.find_entities_filtered({ position = ent.position, radius = 4, type = { "curved-rail" } }) + if + (ent_straight_rails ~= nil and #ent_straight_rails > 0) + or (ent_curved_rails ~= nil and #ent_curved_rails > 0) + then + if not (ent.train and ent.train.id == v.train.id) then table.insert(ents_ahead, ent) end end end end end - + --Skip if nothing is ahead if #ents_ahead == 0 then return true else end - + --Get distance to nearest entity ahead local nearest = v.surface.get_closest(v.position, ents_ahead) if nearest == nil then --Skip if nearest does not exist return true end - local edge_dist = util.distance(v.position, nearest.position) - 1/4*(nearest.tile_width + nearest.tile_height) - rendering.draw_circle{color = {0.8, 0.8, 0.8},radius = 2,width = 2,target = nearest,surface = p.surface,time_to_live = 15} - + local edge_dist = util.distance(v.position, nearest.position) - 1 / 4 * (nearest.tile_width + nearest.tile_height) + rendering.draw_circle({ + color = { 0.8, 0.8, 0.8 }, + radius = 2, + width = 2, + target = nearest, + surface = p.surface, + time_to_live = 15, + }) + --Beep - if edge_dist < trigger then - p.play_sound{path = "player-bump-stuck-alert"} + if edge_dist < trigger then + p.play_sound({ path = "player-bump-stuck-alert" }) players[pindex].last_driving_alert_tick = last_driving_alert_tick - players[pindex].last_driving_alert_ent = nearest - rendering.draw_circle{color = {1.0, 0.4, 0.2},radius = 2,width = 2,target = nearest,surface = p.surface,time_to_live = 15} + players[pindex].last_driving_alert_ent = nearest + rendering.draw_circle({ + color = { 1.0, 0.4, 0.2 }, + radius = 2, + width = 2, + target = nearest, + surface = p.surface, + time_to_live = 15, + }) return false end return true @@ -208,16 +254,16 @@ end --Pavement Driving Assist: Read CC state function mod.pda_get_state_of_cruise_control(pindex) if remote.interfaces.PDA and remote.interfaces.PDA.get_state_of_cruise_control then - return remote.call("PDA", "get_state_of_cruise_control",pindex) + return remote.call("PDA", "get_state_of_cruise_control", pindex) else return nil end end --Pavement Driving Assist: Set CC state -function mod.pda_set_state_of_cruise_control(pindex,new_state) +function mod.pda_set_state_of_cruise_control(pindex, new_state) if remote.interfaces.PDA and remote.interfaces.PDA.set_state_of_cruise_control then - remote.call("PDA", "set_state_of_cruise_control",pindex,new_state) + remote.call("PDA", "set_state_of_cruise_control", pindex, new_state) return 1 else return nil @@ -227,16 +273,16 @@ end --Pavement Driving Assist: Read CC speed limit in kmh function mod.pda_get_cruise_control_limit(pindex) if remote.interfaces.PDA and remote.interfaces.PDA.get_cruise_control_limit then - return remote.call("PDA", "get_cruise_control_limit",pindex) + return remote.call("PDA", "get_cruise_control_limit", pindex) else return nil end end --Pavement Driving Assist: Set CC speed limit in kmh -function mod.pda_set_cruise_control_limit(pindex,new_value) +function mod.pda_set_cruise_control_limit(pindex, new_value) if remote.interfaces.PDA and remote.interfaces.PDA.set_cruise_control_limit then - remote.call("PDA", "set_cruise_control_limit",pindex,new_value) + remote.call("PDA", "set_cruise_control_limit", pindex, new_value) return 1 else return nil @@ -246,16 +292,16 @@ end --Pavement Driving Assist: Read assistant state function mod.pda_get_state_of_driving_assistant(pindex) if remote.interfaces.PDA and remote.interfaces.PDA.get_state_of_driving_assistant then - return remote.call("PDA", "get_state_of_driving_assistant",pindex) + return remote.call("PDA", "get_state_of_driving_assistant", pindex) else return nil end end --Pavement Driving Assist: Set assistant state -function mod.pda_set_state_of_driving_assistant(pindex,new_state) +function mod.pda_set_state_of_driving_assistant(pindex, new_state) if remote.interfaces.PDA and remote.interfaces.PDA.set_state_of_driving_assistant then - remote.call("PDA", "set_state_of_driving_assistant",pindex,new_state) + remote.call("PDA", "set_state_of_driving_assistant", pindex, new_state) return 1 else return nil @@ -264,31 +310,31 @@ end --Pavement Driving Assist: Read assistant state after it has been toggled function mod.pda_read_assistant_toggled_info(pindex) - if game.get_player(pindex).driving then + if game.get_player(pindex).driving then local is_on = not mod.pda_get_state_of_driving_assistant(pindex) - if is_on == true then - printout("Enabled pavement driving asssistant",pindex) - elseif is_on == false then - printout("Disabled pavement driving asssistant",pindex) + if is_on == true then + printout("Enabled pavement driving asssistant", pindex) + elseif is_on == false then + printout("Disabled pavement driving asssistant", pindex) else - printout("Missing pavement driving asssistant",pindex) + printout("Missing pavement driving asssistant", pindex) end - end + end end --Pavement Driving Assist: Read CC state after it has been toggled function mod.pda_read_cruise_control_toggled_info(pindex) - if game.get_player(pindex).driving then + if game.get_player(pindex).driving then local is_on = not mod.pda_get_state_of_cruise_control(pindex) if is_on == true then - printout("Enabled cruise control",pindex) + printout("Enabled cruise control", pindex) elseif is_on == false then - printout("Disabled cruise control",pindex) + printout("Disabled cruise control", pindex) else - printout("Missing cruise control",pindex) + printout("Missing cruise control", pindex) end - mod.pda_set_cruise_control_limit(pindex,0.16) + mod.pda_set_cruise_control_limit(pindex, 0.16) end end -return mod \ No newline at end of file +return mod diff --git a/scripts/ds/circular-options-list.lua b/scripts/ds/circular-options-list.lua index 52e46b77..fe4e2746 100644 --- a/scripts/ds/circular-options-list.lua +++ b/scripts/ds/circular-options-list.lua @@ -58,7 +58,7 @@ fact that it doesn't handle missing values, it doesn't handle empty lists either Operations are all O(N). ]] -local math_helpers = require('math-helpers') +local math_helpers = require("math-helpers") local mod = {} @@ -106,7 +106,7 @@ function mod.kv_list(list, comparer) end return { values = vals, - options = { comparer = comparer } + options = { comparer = comparer }, } end diff --git a/scripts/electrical.lua b/scripts/electrical.lua index 790e0653..5878d387 100644 --- a/scripts/electrical.lua +++ b/scripts/electrical.lua @@ -9,7 +9,7 @@ local mod = {} function mod.get_power_string(power) result = "" if power > 1000000000000 then - power = power/1000000000000 + power = power / 1000000000000 result = result .. string.format(" %.1f Terawatts", power) elseif power > 1000000000 then power = power / 1000000000 @@ -29,9 +29,14 @@ end --Spawns a lamp at the electric pole and uses its energy level to approximate the network satisfaction percentage with high accuracy function mod.get_electricity_satisfaction(electric_pole) local satisfaction = -1 - local test_lamp = electric_pole.surface.create_entity{name = "small-lamp", position = electric_pole.position, raise_built = false, force = electric_pole.force} - satisfaction = math.ceil(test_lamp.energy * 9/8)--Experimentally found coefficient - test_lamp.destroy{} + local test_lamp = electric_pole.surface.create_entity({ + name = "small-lamp", + position = electric_pole.position, + raise_built = false, + force = electric_pole.force, + }) + satisfaction = math.ceil(test_lamp.energy * 9 / 8) --Experimentally found coefficient + test_lamp.destroy({}) return satisfaction end @@ -42,23 +47,32 @@ function mod.get_electricity_flow_info(ent) local power = 0 local capacity = 0 for i, v in pairs(ent.electric_network_statistics.output_counts) do - power = power + (ent.electric_network_statistics.get_flow_count{name = i, input = false, precision_index = defines.flow_precision_index.five_seconds}) + power = power + + ( + ent.electric_network_statistics.get_flow_count({ + name = i, + input = false, + precision_index = defines.flow_precision_index.five_seconds, + }) + ) local cap_add = 0 - for _, power_ent in pairs(ent.surface.find_entities_filtered{name=i,force = ent.force}) do - if power_ent.electric_network_id == ent.electric_network_id then - cap_add = cap_add + 1 - end + for _, power_ent in pairs(ent.surface.find_entities_filtered({ name = i, force = ent.force })) do + if power_ent.electric_network_id == ent.electric_network_id then cap_add = cap_add + 1 end end cap_add = cap_add * game.entity_prototypes[i].max_energy_production if game.entity_prototypes[i].type == "solar-panel" then - cap_add = cap_add * ent.surface.solar_power_multiplier * (1-ent.surface.darkness) + cap_add = cap_add * ent.surface.solar_power_multiplier * (1 - ent.surface.darkness) end capacity = capacity + cap_add end - power = power * 60 - capacity = capacity * 60 - result = result .. mod.get_power_string(power) .. " being produced out of " .. mod.get_power_string(capacity) .. " capacity, " - return result + power = power * 60 + capacity = capacity * 60 + result = result + .. mod.get_power_string(power) + .. " being produced out of " + .. mod.get_power_string(capacity) + .. " capacity, " + return result end --Finds the neearest electric pole. Can be set to determine whether to check only for poles with electricity flow. Can call using only the first two parameters. @@ -73,14 +87,14 @@ function mod.find_nearest_electric_pole(ent, require_supplied, radius, alt_surfa local pos = nil if ent ~= nil and ent.valid then surface = ent.surface - pos = ent.position + pos = ent.position else surface = alt_surface - pos = alt_pos + pos = alt_pos end --Scan nearby for electric poles, expand radius if not successful - local poles = surface.find_entities_filtered{ type = "electric-pole" , position = pos , radius = radius} + local poles = surface.find_entities_filtered({ type = "electric-pole", position = pos, radius = radius }) if #poles == 0 then if radius < 100 then radius = 100 @@ -97,41 +111,48 @@ function mod.find_nearest_electric_pole(ent, require_supplied, radius, alt_surfa end --Find the nearest among the poles with electric networks - for i,pole in ipairs(poles) do + for i, pole in ipairs(poles) do --Check if the pole's network has power producers - local has_power = mod.get_electricity_satisfaction(pole) > 0 + local has_power = mod.get_electricity_satisfaction(pole) > 0 local dict = pole.electric_network_statistics.output_counts local network_producers = {} for name, count in pairs(dict) do - table.insert(network_producers, {name = name, count = count}) + table.insert(network_producers, { name = name, count = count }) end - local network_producer_count = #network_producers --laterdo test again if this is working, it should pick up even 0.001% satisfaction... + local network_producer_count = #network_producers --laterdo test again if this is working, it should pick up even 0.001% satisfaction... local dist = 0 - if has_power or network_producer_count > 0 or (not require_supplied) then - dist = math.ceil(util.distance(pos, pole.position)) - --Set as nearest if valid - if dist < min_dist then - min_dist = dist - nearest = pole - end - end + if has_power or network_producer_count > 0 or not require_supplied then + dist = math.ceil(util.distance(pos, pole.position)) + --Set as nearest if valid + if dist < min_dist then + min_dist = dist + nearest = pole + end + end end --Return the nearst found, possibly nil if nearest == nil then if radius < 100 then - radius = 100 - return mod.find_nearest_electric_pole(ent, require_supplied, radius, alt_surface, alt_pos) - elseif radius < 1000 then - radius = 1000 - return mod.find_nearest_electric_pole(ent, require_supplied, radius, alt_surface, alt_pos) - elseif radius < 10000 then - radius = 10000 - return mod.find_nearest_electric_pole(ent, require_supplied, radius, alt_surface, alt_pos) - else - return nil, nil --Nothing within 10000 tiles! - end + radius = 100 + return mod.find_nearest_electric_pole(ent, require_supplied, radius, alt_surface, alt_pos) + elseif radius < 1000 then + radius = 1000 + return mod.find_nearest_electric_pole(ent, require_supplied, radius, alt_surface, alt_pos) + elseif radius < 10000 then + radius = 10000 + return mod.find_nearest_electric_pole(ent, require_supplied, radius, alt_surface, alt_pos) + else + return nil, nil --Nothing within 10000 tiles! + end end - rendering.draw_circle{color = {1, 1, 0}, radius = 2, width = 2, target = nearest.position, surface = nearest.surface, time_to_live = 60} + rendering.draw_circle({ + color = { 1, 1, 0 }, + radius = 2, + width = 2, + target = nearest.position, + surface = nearest.surface, + time_to_live = 60, + }) return nearest, min_dist end @@ -141,7 +162,7 @@ function mod.report_nearest_supplied_electric_pole(ent) local pole, dist = mod.find_nearest_electric_pole(ent, true) local dir = -1 if pole ~= nil then - dir = fa_utils.get_direction_biased(pole.position,ent.position) + dir = fa_utils.get_direction_biased(pole.position, ent.position) result = "The nearest powered electric pole is " .. dist .. " tiles to the " .. fa_utils.direction_lookup(dir) else result = "And there are no powered electric poles within ten thousand tiles. Generators may be out of energy." diff --git a/scripts/equipment.lua b/scripts/equipment.lua index 66b99bab..24959137 100644 --- a/scripts/equipment.lua +++ b/scripts/equipment.lua @@ -1,25 +1,23 @@ --Here: functions relating to guns and equipment management --Does not include event handlers, combat, repair packs -local localising = require('scripts.localising') +local localising = require("scripts.localising") local fa_electrical = require("scripts.electrical") local mod = {} --Tries to equip a stack. For now called only for a stack in hand when the only the inventory is open. -function mod.equip_it(stack,pindex) +function mod.equip_it(stack, pindex) local message = "" if players[pindex].menu == "vehicle" and game.get_player(pindex).opened.type == "spider-vehicle" then - message = localising.get_alt(game.entity_prototypes["spidertron"]) + message = localising.get_alt(game.entity_prototypes["spidertron"]) if message == nil then - message = "Spidertron "--laterdo possible bug here + message = "Spidertron " --laterdo possible bug here end end - if stack == nil or not stack.valid_for_read or not stack.valid then - return "Nothing found to equip." - end - + if stack == nil or not stack.valid_for_read or not stack.valid then return "Nothing found to equip." end + if stack.is_armor then local armor = game.get_player(pindex).get_inventory(defines.inventory.character_armor) if armor.is_empty() then @@ -31,82 +29,79 @@ function mod.equip_it(stack,pindex) players[pindex].skip_read_hand = true elseif stack.type == "gun" then --Equip gun ("arms") - local gun_inv = game.get_player(pindex).get_inventory(defines.inventory.character_guns) - if gun_inv.can_insert(stack) then - local inserted = gun_inv.insert(stack) - message = message .. " Equipped " .. stack.name - stack.count = stack.count - inserted - players[pindex].skip_read_hand = true - else - if gun_inv.count_empty_stacks() == 0 then - message = message .. " All gun slots full." - else - message = message .. " Cannot insert " .. stack.name - end - end + local gun_inv = game.get_player(pindex).get_inventory(defines.inventory.character_guns) + if gun_inv.can_insert(stack) then + local inserted = gun_inv.insert(stack) + message = message .. " Equipped " .. stack.name + stack.count = stack.count - inserted + players[pindex].skip_read_hand = true + else + if gun_inv.count_empty_stacks() == 0 then + message = message .. " All gun slots full." + else + message = message .. " Cannot insert " .. stack.name + end + end elseif stack.type == "ammo" then --Equip ammo - local ammo_inv = game.get_player(pindex).get_inventory(defines.inventory.character_ammo) - if ammo_inv.can_insert(stack) then - local inserted = ammo_inv.insert(stack) - message = message .. " Reloaded with " .. stack.name - stack.count = stack.count - inserted - players[pindex].skip_read_hand = true - else - if ammo_inv.count_empty_stacks() == 0 then - message = message .. " All ammo slots full." - else - message = message .. " Cannot insert " .. stack.name - end - end + local ammo_inv = game.get_player(pindex).get_inventory(defines.inventory.character_ammo) + if ammo_inv.can_insert(stack) then + local inserted = ammo_inv.insert(stack) + message = message .. " Reloaded with " .. stack.name + stack.count = stack.count - inserted + players[pindex].skip_read_hand = true + else + if ammo_inv.count_empty_stacks() == 0 then + message = message .. " All ammo slots full." + else + message = message .. " Cannot insert " .. stack.name + end + end elseif stack.prototype.place_as_equipment_result ~= nil then --Equip equipment ("gear") - local armor_inv - local grid + local armor_inv + local grid if players[pindex].menu == "vehicle" and game.get_player(pindex).opened.type == "spider-vehicle" then grid = game.get_player(pindex).opened.grid else armor_inv = game.get_player(pindex).get_inventory(defines.inventory.character_armor) - if armor_inv.is_empty() then - return "Equipment requires armor with an equipment grid." - end - if armor_inv[1].grid == nil or not armor_inv[1].grid.valid then + if armor_inv.is_empty() then return "Equipment requires armor with an equipment grid." end + if armor_inv[1].grid == nil or not armor_inv[1].grid.valid then return "Equipment requires armor with an equipment grid." end grid = armor_inv[1].grid - end - --Iterate across the whole grid, trying to place the item. - local placed = nil - for i = 0, grid.width-1, 1 do - for j = 0, grid.height-1, 1 do - placed = grid.put{name = stack.name, position = {i,j}, by_player = pindex} - if placed ~= nil then - break - end - end - if placed ~= nil then - break - end - end - local slots_left = mod.count_empty_equipment_slots(grid) - if placed ~= nil then - message = message .. " equipped " .. stack.name .. ", " .. slots_left .. " empty slots remaining." - stack.count = stack.count - 1 - players[pindex].skip_read_hand = true - else - --Check if the grid is full - if slots_left == 0 then - message = message .. " All armor equipment slots are full." - else - message = message .. " This equipment does not fit in the remaining ".. slots_left .. " slots." - end - end - elseif players[pindex].in_menu == false and (stack.prototype.place_result ~= nil or stack.prototype.place_as_tile_result ~= nil) then + end + --Iterate across the whole grid, trying to place the item. + local placed = nil + for i = 0, grid.width - 1, 1 do + for j = 0, grid.height - 1, 1 do + placed = grid.put({ name = stack.name, position = { i, j }, by_player = pindex }) + if placed ~= nil then break end + end + if placed ~= nil then break end + end + local slots_left = mod.count_empty_equipment_slots(grid) + if placed ~= nil then + message = message .. " equipped " .. stack.name .. ", " .. slots_left .. " empty slots remaining." + stack.count = stack.count - 1 + players[pindex].skip_read_hand = true + else + --Check if the grid is full + if slots_left == 0 then + message = message .. " All armor equipment slots are full." + else + message = message .. " This equipment does not fit in the remaining " .. slots_left .. " slots." + end + end + elseif + players[pindex].in_menu == false + and (stack.prototype.place_result ~= nil or stack.prototype.place_as_tile_result ~= nil) + then message = "" else message = message .. " Cannot equip " .. stack.name end - + return message end @@ -114,29 +109,25 @@ end function mod.read_weapons_and_ammo(pindex) local guns_inv = game.get_player(pindex).get_inventory(defines.inventory.character_guns) local ammo_inv = game.get_player(pindex).get_inventory(defines.inventory.character_ammo) - local guns_count = #guns_inv - guns_inv.count_empty_stacks() + local guns_count = #guns_inv - guns_inv.count_empty_stacks() local ammos_count = #ammo_inv - ammo_inv.count_empty_stacks() local result = "Weapons, " - + for i = 1, 3, 1 do - if i > 1 then - result = result .. " and " - end - if guns_inv[i] and guns_inv[i].valid and guns_inv[i].valid_for_read then - result = result .. guns_inv[i].name - else - result = result .. "empty weapon slot" - end - if ammo_inv[i] ~= nil and ammo_inv[i].valid and ammo_inv[i].valid_for_read then - result = result .. " with " .. ammo_inv[i].count .. " " .. ammo_inv[i].name .. "s, " - else - result = result .. " with no ammunition, " - end - end - if guns_count == 0 then - result = " No weapons equipped." + if i > 1 then result = result .. " and " end + if guns_inv[i] and guns_inv[i].valid and guns_inv[i].valid_for_read then + result = result .. guns_inv[i].name + else + result = result .. "empty weapon slot" + end + if ammo_inv[i] ~= nil and ammo_inv[i].valid and ammo_inv[i].valid_for_read then + result = result .. " with " .. ammo_inv[i].count .. " " .. ammo_inv[i].name .. "s, " + else + result = result .. " with no ammunition, " + end end - + if guns_count == 0 then result = " No weapons equipped." end + return result end @@ -150,7 +141,7 @@ function mod.reload_weapons(pindex) return result end --Apply an inventory transfer to the ammo inventory. - local res, full = transfer_inventory{from = main_inv, to = ammo_inv} + local res, full = transfer_inventory({ from = main_inv, to = ammo_inv }) --**laterdo fail conditions messages, and maybe add reload sound? --Check fullness if ammo_inv.is_full() then @@ -166,41 +157,41 @@ function mod.remove_weapons_and_ammo(pindex) local guns_inv = game.get_player(pindex).get_inventory(defines.inventory.character_guns) local ammo_inv = game.get_player(pindex).get_inventory(defines.inventory.character_ammo) local main_inv = game.get_player(pindex).get_inventory(defines.inventory.character_main) - local guns_count = #guns_inv - guns_inv.count_empty_stacks() + local guns_count = #guns_inv - guns_inv.count_empty_stacks() local ammos_count = #ammo_inv - ammo_inv.count_empty_stacks() local expected_remove_count = guns_count + ammos_count local resulted_remove_count = 0 local message = "" - + --Remove all ammo for i = 1, ammos_count, 1 do if main_inv.can_insert(ammo_inv[i]) then local inserted = main_inv.insert(ammo_inv[i]) local removed = ammo_inv.remove(ammo_inv[i]) if inserted ~= removed then - game.get_player(pindex).print("ammo removal count error",{volume_modifier=0})--todo fix + game.get_player(pindex).print("ammo removal count error", { volume_modifier = 0 }) --todo fix end - resulted_remove_count = resulted_remove_count + math.ceil(removed / 1000 )--counts how many stacks are removed - end + resulted_remove_count = resulted_remove_count + math.ceil(removed / 1000) --counts how many stacks are removed + end end - + --Remove all guns for i = 1, guns_count, 1 do if main_inv.can_insert(guns_inv[i]) then - local inserted = main_inv.insert(guns_inv[i]) - local removed = guns_inv.remove(guns_inv[i]) - if inserted ~= removed then - game.get_player(pindex).print("gun removal count error",{volume_modifier=0})--todo fix - end - resulted_remove_count = resulted_remove_count + math.ceil(removed / 1000)--counts how many stacks are removed - end + local inserted = main_inv.insert(guns_inv[i]) + local removed = guns_inv.remove(guns_inv[i]) + if inserted ~= removed then + game.get_player(pindex).print("gun removal count error", { volume_modifier = 0 }) --todo fix + end + resulted_remove_count = resulted_remove_count + math.ceil(removed / 1000) --counts how many stacks are removed + end end - + message = "Collected " .. resulted_remove_count .. " of " .. expected_remove_count .. " item stacks," - if game.get_player(pindex).get_main_inventory().count_empty_stacks() == 0 then + if game.get_player(pindex).get_main_inventory().count_empty_stacks() == 0 then message = message .. " Inventory full. " end - + return message end @@ -210,21 +201,21 @@ function mod.delete_equipped_atomic_bombs(pindex) local main_inv = game.get_player(pindex).get_inventory(defines.inventory.character_main) local ammos_count = #ammo_inv - ammo_inv.count_empty_stacks() local resulted_remove_count = 0 - + --Remove all atomic bombs for i = 1, ammos_count, 1 do if ammo_inv[i] and ammo_inv[i].valid_for_read and ammo_inv[i].name == "atomic-bomb" then local removed = ammo_inv.remove(ammo_inv[i]) resulted_remove_count = resulted_remove_count + removed - end + end end - + --Save removed amount local restore_count = players[pindex].restore_count if restore_count == nil or restore_count < resulted_remove_count then players[pindex].restore_count = resulted_remove_count end - return + return end --Temporary safety measure for preventing accidental shooting of atomic bombs @@ -232,96 +223,93 @@ function mod.restore_equipped_atomic_bombs(pindex) local guns_inv = game.get_player(pindex).get_inventory(defines.inventory.character_guns) local ammo_inv = game.get_player(pindex).get_inventory(defines.inventory.character_ammo) local main_inv = game.get_player(pindex).get_inventory(defines.inventory.character_main) - local guns_count = #guns_inv - guns_inv.count_empty_stacks() + local guns_count = #guns_inv - guns_inv.count_empty_stacks() local ammos_count = #ammo_inv - ammo_inv.count_empty_stacks() - - --Create stack + + --Create stack local restore_count = players[pindex].restore_count - if restore_count == nil then - restore_count = 1 - end - local stack = {name = "atomic-bomb", count = restore_count} - - --Equip all atomic bombs according to count - if restore_count > 0 and ammo_inv.can_insert(stack) then - local inserted = ammo_inv.insert(stack) - end + if restore_count == nil then restore_count = 1 end + local stack = { name = "atomic-bomb", count = restore_count } + + --Equip all atomic bombs according to count + if restore_count > 0 and ammo_inv.can_insert(stack) then local inserted = ammo_inv.insert(stack) end end function mod.count_empty_equipment_slots(grid) - local slots_left = 0 - for i = 0, grid.width-1, 1 do - for j = 0, grid.height-1, 1 do - local check = grid.get({i,j}) - if check == nil then - slots_left = slots_left + 1 - end - end - end - return slots_left + local slots_left = 0 + for i = 0, grid.width - 1, 1 do + for j = 0, grid.height - 1, 1 do + local check = grid.get({ i, j }) + if check == nil then slots_left = slots_left + 1 end + end + end + return slots_left end --Read armor stats such as type and bonuses function mod.read_armor_stats(pindex) local armor_inv = game.get_player(pindex).get_inventory(defines.inventory.character_armor) local result = "" - if armor_inv.is_empty() then - return "No armor equipped." - end - if armor_inv[1].grid == nil or not armor_inv[1].grid.valid then + if armor_inv.is_empty() then return "No armor equipped." end + if armor_inv[1].grid == nil or not armor_inv[1].grid.valid then return armor_inv[1].name .. " equipped, with no equipment grid." end --Armor with Equipment - local grid + local grid if players[pindex].menu == "vehicle" and game.get_player(pindex).opened.type == "spider-vehicle" then grid = game.get_player(pindex).opened.grid - result = localising.get_alt(game.entity_prototypes["spidertron"]) + result = localising.get_alt(game.entity_prototypes["spidertron"]) if result == nil then - result = "Spidertron "--laterdo possible bug here + result = "Spidertron " --laterdo possible bug here end else grid = armor_inv[1].grid result = armor_inv[1].name .. " equipped, " end - if grid.count() == 0 then - return result .. " no armor equipment installed. " - end + if grid.count() == 0 then return result .. " no armor equipment installed. " end --Read shield level if grid.max_shield > 0 then if grid.shield == grid.max_shield then - result = result .. " shields full, " - else - result = result .. " shields at " .. math.ceil(100 * grid.shield / grid.max_shield) .. " percent, " - end + result = result .. " shields full, " + else + result = result .. " shields at " .. math.ceil(100 * grid.shield / grid.max_shield) .. " percent, " + end end --Read battery level if grid.battery_capacity > 0 then if grid.available_in_batteries == grid.battery_capacity then - result = result .. " batteries full, " - elseif grid.available_in_batteries == 0 then - result = result .. " batteries empty " - else - result = result .. " batteries at " .. math.ceil(100 * grid.available_in_batteries / grid.battery_capacity) .. " percent, " - end + result = result .. " batteries full, " + elseif grid.available_in_batteries == 0 then + result = result .. " batteries empty " + else + result = result + .. " batteries at " + .. math.ceil(100 * grid.available_in_batteries / grid.battery_capacity) + .. " percent, " + end else result = result .. " no batteries, " - end + end --Energy Producers if grid.generator_energy > 0 or grid.max_solar_energy > 0 then result = result .. " generating " - if grid.generator_energy > 0 then - result = result .. fa_electrical.get_power_string(grid.generator_energy*60) .. " nonstop, " - end - if grid.max_solar_energy > 0 then - result = result .. fa_electrical.get_power_string(grid.max_solar_energy*60) .. " at daytime, " - end + if grid.generator_energy > 0 then + result = result .. fa_electrical.get_power_string(grid.generator_energy * 60) .. " nonstop, " + end + if grid.max_solar_energy > 0 then + result = result .. fa_electrical.get_power_string(grid.max_solar_energy * 60) .. " at daytime, " + end end - + --Movement bonus if grid.count("exoskeleton-equipment") > 0 then - result = result .. " movement bonus " .. grid.count("exoskeleton-equipment") * 30 .. " percent for " .. fa_electrical.get_power_string(grid.count("exoskeleton-equipment")*200000) + result = result + .. " movement bonus " + .. grid.count("exoskeleton-equipment") * 30 + .. " percent for " + .. fa_electrical.get_power_string(grid.count("exoskeleton-equipment") * 200000) end - + return result end @@ -329,33 +317,27 @@ end function mod.read_equipment_list(pindex) local armor_inv = game.get_player(pindex).get_inventory(defines.inventory.character_armor) local result = "" - if armor_inv.is_empty() then - return "No armor equipped." - end - if armor_inv[1].grid == nil or not armor_inv[1].grid.valid then - return "No equipment grid." - end + if armor_inv.is_empty() then return "No armor equipped." end + if armor_inv[1].grid == nil or not armor_inv[1].grid.valid then return "No equipment grid." end --Armor with Equipment - local grid + local grid if players[pindex].menu == "vehicle" and game.get_player(pindex).opened.type == "spider-vehicle" then grid = game.get_player(pindex).opened.grid - result = localising.get_alt(game.entity_prototypes["spidertron"]) + result = localising.get_alt(game.entity_prototypes["spidertron"]) if result == nil then - result = "Spidertron "--laterdo possible bug here + result = "Spidertron " --laterdo possible bug here end else grid = armor_inv[1].grid result = "Armor " end - if grid.equipment == nil or grid.equipment == {} then - return " No armor equipment installed. " - end + if grid.equipment == nil or grid.equipment == {} then return " No armor equipment installed. " end --Read Equipment List - result = result .." equipped, " - local contents = grid.get_contents() + result = result .. " equipped, " + local contents = grid.get_contents() local itemtable = {} for name, count in pairs(contents) do - table.insert(itemtable, {name = name, count = count}) + table.insert(itemtable, { name = name, count = count }) end if #itemtable == 0 then result = result .. " nothing, " @@ -364,9 +346,9 @@ function mod.read_equipment_list(pindex) result = result .. itemtable[i].count .. " " .. itemtable[i].name .. ", " end end - + result = result .. mod.count_empty_equipment_slots(grid) .. " empty slots remaining " - + return result end @@ -374,35 +356,33 @@ end function mod.remove_equipment_and_armor(pindex) local armor_inv = game.get_player(pindex).get_inventory(defines.inventory.character_armor) local result = "" - if armor_inv.is_empty() then - return "No armor." - end - - local grid + if armor_inv.is_empty() then return "No armor." end + + local grid if players[pindex].menu == "vehicle" and game.get_player(pindex).opened.type == "spider-vehicle" then grid = game.get_player(pindex).opened.grid else grid = armor_inv[1].grid end - if grid ~= nil and grid.valid then - local e_count = grid.count() - --Take all items - for i = 0, grid.width-1, 1 do - for j = 0, grid.height-1, 1 do - local check = grid.get({i,j}) - local inv = game.get_player(pindex).get_main_inventory() - if check ~= nil and inv.can_insert({name = check.name}) then - inv.insert({name = check.name}) - grid.take{position = {i,j}} - end - end - end - result = "Collected " .. e_count - grid.count() .. " of " .. e_count .. " items, " + if grid ~= nil and grid.valid then + local e_count = grid.count() + --Take all items + for i = 0, grid.width - 1, 1 do + for j = 0, grid.height - 1, 1 do + local check = grid.get({ i, j }) + local inv = game.get_player(pindex).get_main_inventory() + if check ~= nil and inv.can_insert({ name = check.name }) then + inv.insert({ name = check.name }) + grid.take({ position = { i, j } }) + end + end + end + result = "Collected " .. e_count - grid.count() .. " of " .. e_count .. " items, " end - + --Remove armor if players[pindex].menu == "vehicle" and game.get_player(pindex).opened.type == "spider-vehicle" then - --do nothing + --do nothing elseif game.get_player(pindex).get_inventory(defines.inventory.character_main).count_empty_stacks() == 0 then result = result .. " inventory full " else @@ -412,7 +392,7 @@ function mod.remove_equipment_and_armor(pindex) stack2.swap_stack(armor_inv[1]) game.get_player(pindex).clear_cursor() end - + return result end diff --git a/scripts/fa-settings-menus.lua b/scripts/fa-settings-menus.lua index e37f440c..b91ee6e6 100644 --- a/scripts/fa-settings-menus.lua +++ b/scripts/fa-settings-menus.lua @@ -9,7 +9,7 @@ function mod.top_menu_open(pindex) if settings_menu == nil then settings_menu = { submenu = "", - index = 0 + index = 0, } players[pindex].mod_menu = settings_menu end @@ -18,14 +18,14 @@ function mod.top_menu_open(pindex) players[pindex].menu = "mod_menu" players[pindex].in_menu = true players[pindex].move_queue = {} - + --Reset the menu line index to 0 players[pindex].mod_menu.index = 0 - + --Play sound - game.get_player(pindex).play_sound{path = "Open-Inventory-Sound"} - - --Load menu + game.get_player(pindex).play_sound({ path = "Open-Inventory-Sound" }) + + --Load menu mod.run_top_menu(pindex, players[pindex].mod_menu.index, false) end @@ -39,11 +39,14 @@ end ]] function mod.run_top_menu(pindex, menu_index, clicked) local index = menu_index - + if index == 0 then --About this menu and instructions - printout("Mod settings menu " - .. ", Press 'W' and 'S' to navigate options, press 'LEFT BRACKET' to select an option or press 'E' to exit this menu.", pindex) + printout( + "Mod settings menu " + .. ", Press 'W' and 'S' to navigate options, press 'LEFT BRACKET' to select an option or press 'E' to exit this menu.", + pindex + ) elseif index == 1 then --Mod controls list (read only) [All controls are listed directly in game] if not clicked then @@ -74,8 +77,8 @@ function mod.controls_menu_open(pindex) local menu_data = players[pindex].fa_mod_controls_menu if menu_data == nil then menu_data = { - index = 0, - mod.load_mod_controls_list(pindex) + index = 0, + mod.load_mod_controls_list(pindex), } players[pindex].fa_mod_controls_menu = menu_data end @@ -83,19 +86,18 @@ function mod.controls_menu_open(pindex) --Set the player menu tracker to this menu players[pindex].menu = "fa_mod_controls_menu" players[pindex].in_menu = true - + --Reset the menu line index to 0 players[pindex].fa_mod_controls_menu.index = 0 - + --Play sound - game.get_player(pindex).play_sound{path = "Open-Inventory-Sound"} - - --Load menu + game.get_player(pindex).play_sound({ path = "Open-Inventory-Sound" }) + + --Load menu mod.run_controls_menu(pindex, players[pindex].fa_mod_controls_menu.index, false) end -function mod.load_mod_controls_list(pindex)--****todo, like loading tutorial strings for the help system - +function mod.load_mod_controls_list(pindex) --****todo, like loading tutorial strings for the help system end --[[ @@ -105,13 +107,16 @@ end ]] function mod.run_controls_menu(pindex, menu_index, clicked, pg_up, pg_down) local index = menu_index - + if index == 0 then --About this menu and instructions - printout("Mod controls menu, with a read-only list of mod controls " - .. ", Press 'W' and 'S' to navigate options, press 'LEFT BRACKET' to select an option or press 'E' to exit this menu.", pindex) + printout( + "Mod controls menu, with a read-only list of mod controls " + .. ", Press 'W' and 'S' to navigate options, press 'LEFT BRACKET' to select an option or press 'E' to exit this menu.", + pindex + ) else - --...read the appropriate localized string + --...read the appropriate localized string end end MOD_CONTROLS_MENU_LENGTH = 2 @@ -121,7 +126,7 @@ function mod.preferences_menu_open(pindex) local menu_data = players[pindex].fa_mod_preferences_menu if menu_data == nil then menu_data = { - index = 0 + index = 0, } players[pindex].fa_mod_preferences_menu = menu_data end @@ -129,14 +134,14 @@ function mod.preferences_menu_open(pindex) --Set the player menu tracker to this menu players[pindex].menu = "fa_mod_preferences_menu" players[pindex].in_menu = true - + --Reset the menu line index to 0 players[pindex].fa_mod_preferences_menu.index = 0 - + --Play sound - game.get_player(pindex).play_sound{path = "Open-Inventory-Sound"} - - --Load menu + game.get_player(pindex).play_sound({ path = "Open-Inventory-Sound" }) + + --Load menu mod.run_preferences_menu(pindex, players[pindex].fa_mod_preferences_menu.index, false) end @@ -149,11 +154,14 @@ end ]] function mod.run_preferences_menu(pindex, menu_index, clicked, pg_up, pg_down) local index = menu_index - + if index == 0 then --About this menu and instructions - printout("Mod preferences menu, with settings that affect interface but have minimal gameplay changes " - .. ", Press 'W' and 'S' to navigate options, press 'LEFT BRACKET' to select an option or press 'E' to exit this menu.", pindex) + printout( + "Mod preferences menu, with settings that affect interface but have minimal gameplay changes " + .. ", Press 'W' and 'S' to navigate options, press 'LEFT BRACKET' to select an option or press 'E' to exit this menu.", + pindex + ) elseif index == 1 then --... if not clicked then @@ -186,7 +194,6 @@ function mod.run_preferences_menu(pindex, menu_index, clicked, pg_up, pg_down) end MOD_PREFERENCES_MENU_LENGTH = 4 - --[[ Mod advanced settings menu 0. About this menu and instructions @@ -196,11 +203,14 @@ MOD_PREFERENCES_MENU_LENGTH = 4 ]] function mod.run_advanced_settings_menu(pindex, menu_index, clicked, pg_up, pg_down) local index = menu_index - + if index == 0 then --About this menu and instructions - printout("Mod advanced settings menu, with settings that strongly affect gameplay " - .. ", Press 'W' and 'S' to navigate options, press 'LEFT BRACKET' to select an option or press 'E' to exit this menu.", pindex) + printout( + "Mod advanced settings menu, with settings that strongly affect gameplay " + .. ", Press 'W' and 'S' to navigate options, press 'LEFT BRACKET' to select an option or press 'E' to exit this menu.", + pindex + ) elseif index == 1 then --... if not clicked then @@ -226,4 +236,4 @@ function mod.run_advanced_settings_menu(pindex, menu_index, clicked, pg_up, pg_d end MOD_ADVANCED_SETTINGS_MENU_LENGTH = 2 -return mod \ No newline at end of file +return mod diff --git a/scripts/fa-utils.lua b/scripts/fa-utils.lua index fee6e743..2f9d17dd 100644 --- a/scripts/fa-utils.lua +++ b/scripts/fa-utils.lua @@ -1,78 +1,76 @@ --Here: Utility functions called by other files. Examples include distance and position calculations, string processing. -local util = require('util') +local util = require("util") local dirs = defines.direction local mod = {} function mod.center_of_tile(pos) - return {x = math.floor(pos.x)+0.5, y = math.floor(pos.y)+ 0.5} + return { x = math.floor(pos.x) + 0.5, y = math.floor(pos.y) + 0.5 } end -function mod.add_position(p1,p2) - return { x = p1.x + p2.x, y = p1.y + p2.y} +function mod.add_position(p1, p2) + return { x = p1.x + p2.x, y = p1.y + p2.y } end -function mod.sub_position(p1,p2) - return { x = p1.x - p2.x, y = p1.y - p2.y} +function mod.sub_position(p1, p2) + return { x = p1.x - p2.x, y = p1.y - p2.y } end -function mod.mult_position(p,m) +function mod.mult_position(p, m) return { x = p.x * m, y = p.y * m } end -function mod.offset_position(oldpos,direction,distance) +function mod.offset_position(oldpos, direction, distance) if direction == defines.direction.north then - return { x = oldpos.x, y = oldpos.y - distance} + return { x = oldpos.x, y = oldpos.y - distance } elseif direction == defines.direction.south then - return { x = oldpos.x, y = oldpos.y + distance} + return { x = oldpos.x, y = oldpos.y + distance } elseif direction == defines.direction.east then - return { x = oldpos.x + distance, y = oldpos.y} + return { x = oldpos.x + distance, y = oldpos.y } elseif direction == defines.direction.west then - return { x = oldpos.x - distance, y = oldpos.y} + return { x = oldpos.x - distance, y = oldpos.y } elseif direction == defines.direction.northwest then - return { x = oldpos.x - distance, y = oldpos.y - distance} + return { x = oldpos.x - distance, y = oldpos.y - distance } elseif direction == defines.direction.northeast then - return { x = oldpos.x + distance, y = oldpos.y - distance} + return { x = oldpos.x + distance, y = oldpos.y - distance } elseif direction == defines.direction.southwest then - return { x = oldpos.x - distance, y = oldpos.y + distance} + return { x = oldpos.x - distance, y = oldpos.y + distance } elseif direction == defines.direction.southeast then - return { x = oldpos.x + distance, y = oldpos.y + distance} + return { x = oldpos.x + distance, y = oldpos.y + distance } end end -function mod.dir_dist(pos1,pos2) +function mod.dir_dist(pos1, pos2) local x1 = pos1.x local x2 = pos2.x local dx = x2 - x1 local y1 = pos1.y local y2 = pos2.y local dy = y2 - y1 - if dx == 0 and dy == 0 then - return {8,0} - end - local dir = math.atan2(dy,dx) --scaled -pi to pi 0 being east - dir = dir + math.sin(4*dir)/4 --bias towards the diagonals - dir = dir/math.pi -- now scaled as -0.5 north, 0 east, 0.5 south - dir=math.floor(dir*defines.direction.south + defines.direction.east + 0.5) --now scaled correctly - dir=dir%(2*defines.direction.south) --now wrapped correctly - local dist = math.sqrt(dx*dx+dy*dy) - return {dir, dist} + if dx == 0 and dy == 0 then return { 8, 0 } end + local dir = math.atan2(dy, dx) --scaled -pi to pi 0 being east + dir = dir + math.sin(4 * dir) / 4 --bias towards the diagonals + dir = dir / math.pi -- now scaled as -0.5 north, 0 east, 0.5 south + dir = math.floor(dir * defines.direction.south + defines.direction.east + 0.5) --now scaled correctly + dir = dir % (2 * defines.direction.south) --now wrapped correctly + local dist = math.sqrt(dx * dx + dy * dy) + return { dir, dist } end -function mod.dir(pos1,pos2) - return mod.dir_dist(pos1,pos2)[1] +function mod.dir(pos1, pos2) + return mod.dir_dist(pos1, pos2)[1] end function mod.direction(pos1, pos2) - return mod.direction_lookup(mod.dir(pos1,pos2)) + return mod.direction_lookup(mod.dir(pos1, pos2)) end function mod.distance(pos1, pos2) - return mod.dir_dist( pos1, pos2)[2] + return mod.dir_dist(pos1, pos2)[2] end function mod.squared_distance(pos1, pos2) - local offset = {x = pos1.x - pos2.x, y = pos1.y - pos2.y} + local offset = { x = pos1.x - pos2.x, y = pos1.y - pos2.y } local result = offset.x * offset.x + offset.y * offset.y return result end @@ -82,42 +80,40 @@ end * Returns 1 of 8 main directions, based on the ratios of the x and y distances. * The deciding ratio is 1 to 4, meaning that for an object that is 100 tiles north, it can be offset by up to 25 tiles east or west before it stops being counted as "directly" in the north. * The arctangent of 1/4 is about 14 degrees, meaning that the field of view that directly counts as a cardinal direction is about 30 degrees, while for a diagonal direction it is about 60 degrees.]] -function mod.get_direction_biased(pos_that,pos_this) +function mod.get_direction_biased(pos_that, pos_this) local diff_x = pos_that.x - pos_this.x local diff_y = pos_that.y - pos_this.y local dir = -1 - + if math.abs(diff_x) > 4 * math.abs(diff_y) then --along east-west - if diff_x > 0 then - dir = defines.direction.east - else - dir = defines.direction.west - end + if diff_x > 0 then + dir = defines.direction.east + else + dir = defines.direction.west + end elseif math.abs(diff_y) > 4 * math.abs(diff_x) then --along north-south - if diff_y > 0 then - dir = defines.direction.south - else - dir = defines.direction.north - end + if diff_y > 0 then + dir = defines.direction.south + else + dir = defines.direction.north + end else --along diagonals if diff_x > 0 and diff_y > 0 then - dir = defines.direction.southeast + dir = defines.direction.southeast elseif diff_x > 0 and diff_y < 0 then - dir = defines.direction.northeast + dir = defines.direction.northeast elseif diff_x < 0 and diff_y > 0 then - dir = defines.direction.southwest - elseif diff_x < 0 and diff_y < 0 then - dir = defines.direction.northwest - elseif diff_x == 0 and diff_y == 0 then - dir = defines.direction.north - else - dir = -2 - end + dir = defines.direction.southwest + elseif diff_x < 0 and diff_y < 0 then + dir = defines.direction.northwest + elseif diff_x == 0 and diff_y == 0 then + dir = defines.direction.north + else + dir = -2 + end end - if dir < 0 then - dir = dirs.north - end + if dir < 0 then dir = dirs.north end return dir end @@ -126,58 +122,54 @@ end * Returns 1 of 8 main directions, based on the ratios of the x and y distances. * The deciding ratio is 1 to 2.5, meaning that for an object that is 25 tiles north, it can be offset by up to 10 tiles east or west before it stops being counted as "directly" in the north. * The arctangent of 1/2.5 is about 22 degrees, meaning that the field of view that directly counts as a cardinal direction is about 44 degrees, while for a diagonal direction it is about 46 degrees.]] -function mod.get_direction_precise(pos_that,pos_this) +function mod.get_direction_precise(pos_that, pos_this) local diff_x = pos_that.x - pos_this.x local diff_y = pos_that.y - pos_this.y local dir = -1 - + if math.abs(diff_x) > 2.5 * math.abs(diff_y) then --along east-west - if diff_x > 0 then - dir = defines.direction.east - else - dir = defines.direction.west - end + if diff_x > 0 then + dir = defines.direction.east + else + dir = defines.direction.west + end elseif math.abs(diff_y) > 2.5 * math.abs(diff_x) then --along north-south - if diff_y > 0 then - dir = defines.direction.south - else - dir = defines.direction.north - end + if diff_y > 0 then + dir = defines.direction.south + else + dir = defines.direction.north + end else --along diagonals if diff_x > 0 and diff_y > 0 then - dir = defines.direction.southeast + dir = defines.direction.southeast elseif diff_x > 0 and diff_y < 0 then - dir = defines.direction.northeast + dir = defines.direction.northeast elseif diff_x < 0 and diff_y > 0 then - dir = defines.direction.southwest - elseif diff_x < 0 and diff_y < 0 then - dir = defines.direction.northwest - elseif diff_x == 0 and diff_y == 0 then - dir = defines.direction.north - else - dir = -2 - end + dir = defines.direction.southwest + elseif diff_x < 0 and diff_y < 0 then + dir = defines.direction.northwest + elseif diff_x == 0 and diff_y == 0 then + dir = defines.direction.north + else + dir = -2 + end end - if dir < 0 then - dir = dirs.north - end + if dir < 0 then dir = dirs.north end return dir end ---Converts an input direction into a localised string. +--Converts an input direction into a localised string. --Note: Directions are integeres but we need to use only defines because they will change in update 2.0. Todo: localise error cases. function mod.direction_lookup(dir) local reading = "unknown" - if dir < 0 then - return "unknown direction ID " .. dir - end + if dir < 0 then return "unknown direction ID " .. dir end if dir >= dirs.north and dir <= dirs.northwest then return game.direction_to_string(dir) else if dir == 8 then --Returned by the game when there is no direction in particular reading = "" - elseif dir == 99 then --Defined by mod + elseif dir == 99 then --Defined by mod reading = "Here" else reading = "unknown direction ID " .. dir @@ -206,9 +198,7 @@ end function mod.get_heading_info(ent) ---@diagnostic disable: cast-local-type local heading = "unknown" - if ent == nil then - return "nil error" - end + if ent == nil then return "nil error" end local ori = ent.orientation if ori < 0.0625 then heading = mod.direction_lookup(dirs.north) @@ -227,37 +217,35 @@ function mod.get_heading_info(ent) elseif ori < 0.9375 then heading = mod.direction_lookup(dirs.northwest) else - heading = mod.direction_lookup(dirs.north)--default - end + heading = mod.direction_lookup(dirs.north) --default + end return heading end --Converts the entity orientation into a heading direction, with all directions having equal bias. function mod.get_heading_value(ent) local heading = nil - if ent == nil then - return nil - end + if ent == nil then return nil end local ori = ent.orientation if ori < 0.0625 then - heading = (dirs.north) + heading = dirs.north elseif ori < 0.1875 then - heading = (dirs.northeast) + heading = dirs.northeast elseif ori < 0.3125 then - heading = (dirs.east) + heading = dirs.east elseif ori < 0.4375 then - heading = (dirs.southeast) + heading = dirs.southeast elseif ori < 0.5625 then - heading = (dirs.south) + heading = dirs.south elseif ori < 0.6875 then - heading = (dirs.southwest) + heading = dirs.southwest elseif ori < 0.8125 then - heading = (dirs.west) + heading = dirs.west elseif ori < 0.9375 then - heading = (dirs.northwest) + heading = dirs.northwest else - heading = (dirs.north)--default - end + heading = dirs.north --default + end return heading end @@ -267,147 +255,201 @@ function mod.get_tile_dimensions(item, dir) local dimensions = item.place_result.selection_box x = math.ceil(dimensions.right_bottom.x - dimensions.left_top.x) y = math.ceil(dimensions.right_bottom.y - dimensions.left_top.y) - if (dir/2)%2 == 0 then - return {x = x, y = y} + if (dir / 2) % 2 == 0 then + return { x = x, y = y } else - return {x = y, y = x} + return { x = y, y = x } end end - return {x = 0, y = 0} + return { x = 0, y = 0 } end - --Small utility function for getting an entity's footprint area using just its name. -function mod.get_ent_area_from_name(ent_name,pindex) +function mod.get_ent_area_from_name(ent_name, pindex) -- local ents = game.get_player(pindex).surface.find_entities_filtered{name = ent_name, limit = 1} -- if #ents == 0 then - -- return -1 + -- return -1 -- else - -- return ents[1].tile_height * ents[1].tile_width + -- return ents[1].tile_height * ents[1].tile_width -- end return game.entity_prototypes[ent_name].tile_width * game.entity_prototypes[ent_name].tile_height end --Returns true/false on whether an entity is located within a defined area. function mod.is_ent_inside_area(ent_name, area_left_top, area_right_bottom, pindex) - local ents = game.get_player(pindex).surface.find_entities_filtered{name = ent_name, area = {area_left_top,area_right_bottom}, limit = 1} + local ents = game + .get_player(pindex).surface + .find_entities_filtered({ name = ent_name, area = { area_left_top, area_right_bottom }, limit = 1 }) return #ents > 0 end --Returns the map position of the northwest corner of an entity. function mod.get_ent_northwest_corner_position(ent) - if ent.valid == false or ent.tile_width == nil then - return ent.position - end - local width = ent.tile_width + if ent.valid == false or ent.tile_width == nil then return ent.position end + local width = ent.tile_width local height = ent.tile_height if ent.direction == dirs.east or ent.direction == dirs.west then - width = ent.tile_height + width = ent.tile_height height = ent.tile_width end - local pos = mod.center_of_tile({x = ent.position.x - math.floor(width/2), y = ent.position.y - math.floor(height/2)}) + local pos = + mod.center_of_tile({ x = ent.position.x - math.floor(width / 2), y = ent.position.y - math.floor(height / 2) }) --rendering.draw_rectangle{color = {0.75,1,1,0.75}, surface = ent.surface, draw_on_ground = true, players = nil, width = 2, left_top = {math.floor(pos.x)+0.05,math.floor(pos.y)+0.05}, right_bottom = {math.ceil(pos.x)-0.05,math.ceil(pos.y)-0.05}, time_to_live = 30} return pos end --Reports which part of the selected entity has the cursor. E.g. southwest corner, center... function mod.get_entity_part_at_cursor(pindex) - local p = game.get_player(pindex) - local x = players[pindex].cursor_pos.x - local y = players[pindex].cursor_pos.y - local excluded_names = {"character", "flying-text", "highlight-box", "combat-robot", "logistic-robot", "construction-robot", "rocket-silo-rocket-shadow"} - local ents = p.surface.find_entities_filtered{position = {x = x,y = y}, name = excluded_names, invert = true} - local north_same = false - local south_same = false - local east_same = false - local west_same = false - local location = nil - - --First check if there is an entity at the cursor - if #ents > 0 then + local p = game.get_player(pindex) + local x = players[pindex].cursor_pos.x + local y = players[pindex].cursor_pos.y + local excluded_names = { + "character", + "flying-text", + "highlight-box", + "combat-robot", + "logistic-robot", + "construction-robot", + "rocket-silo-rocket-shadow", + } + local ents = p.surface.find_entities_filtered({ position = { x = x, y = y }, name = excluded_names, invert = true }) + local north_same = false + local south_same = false + local east_same = false + local west_same = false + local location = nil + + --First check if there is an entity at the cursor + if #ents > 0 then --Choose something else if ore is selected local preferred_ent = ents[1] for i, ent in ipairs(ents) do - if ent.valid and ent.type ~= "resource" then - preferred_ent = ent - end + if ent.valid and ent.type ~= "resource" then preferred_ent = ent end end p.selected = preferred_ent - --Report which part of the entity the cursor covers. - rendering.draw_circle{color = {1, 0.0, 0.5},radius = 0.1,width = 2,target = {x = x+0 ,y = y-1}, surface = p.surface, time_to_live = 30} - rendering.draw_circle{color = {1, 0.0, 0.5},radius = 0.1,width = 2,target = {x = x+0 ,y = y+1}, surface = p.surface, time_to_live = 30} - rendering.draw_circle{color = {1, 0.0, 0.5},radius = 0.1,width = 2,target = {x = x-1 ,y = y-0}, surface = p.surface, time_to_live = 30} - rendering.draw_circle{color = {1, 0.0, 0.5},radius = 0.1,width = 2,target = {x = x+1 ,y = y-0}, surface = p.surface, time_to_live = 30} - - local ent_north = p.surface.find_entities_filtered{position = {x = x,y = y-1}, name = excluded_names, invert = true} - if #ent_north > 0 and ent_north[1].unit_number == preferred_ent.unit_number then north_same = true - elseif #ent_north > 1 and ent_north[2].unit_number == preferred_ent.unit_number then north_same = true - elseif #ent_north > 2 and ent_north[3].unit_number == preferred_ent.unit_number then north_same = true end - local ent_south = p.surface.find_entities_filtered{position = {x = x,y = y+1}, name = excluded_names, invert = true} - if #ent_south > 0 and ent_south[1].unit_number == preferred_ent.unit_number then south_same = true - elseif #ent_south > 1 and ent_south[2].unit_number == preferred_ent.unit_number then south_same = true - elseif #ent_south > 2 and ent_south[3].unit_number == preferred_ent.unit_number then south_same = true end - local ent_east = p.surface.find_entities_filtered{position = {x = x+1,y = y}, name = excluded_names, invert = true} - if #ent_east > 0 and ent_east[1].unit_number == preferred_ent.unit_number then east_same = true - elseif #ent_east > 1 and ent_east[2].unit_number == preferred_ent.unit_number then east_same = true - elseif #ent_east > 2 and ent_east[3].unit_number == preferred_ent.unit_number then east_same = true end - local ent_west = p.surface.find_entities_filtered{position = {x = x-1,y = y}, name = excluded_names, invert = true} - if #ent_west > 0 and ent_west[1].unit_number == preferred_ent.unit_number then west_same = true - elseif #ent_west > 1 and ent_west[2].unit_number == preferred_ent.unit_number then west_same = true - elseif #ent_west > 2 and ent_west[3].unit_number == preferred_ent.unit_number then west_same = true end - - if north_same and south_same then - if east_same and west_same then - location = "center" - elseif east_same and not west_same then - location = "west edge" - elseif not east_same and west_same then - location = "east edge" - elseif not east_same and not west_same then - location = "middle" - end - elseif north_same and not south_same then - if east_same and west_same then - location = "south edge" - elseif east_same and not west_same then - location = "southwest corner" - elseif not east_same and west_same then - location = "southeast corner" - elseif not east_same and not west_same then - location = "south tip" - end - elseif not north_same and south_same then - if east_same and west_same then - location = "north edge" - elseif east_same and not west_same then - location = "northwest corner" - elseif not east_same and west_same then - location = "northeast corner" - elseif not east_same and not west_same then - location = "north tip" - end - elseif not north_same and not south_same then - if east_same and west_same then - location = "middle" - elseif east_same and not west_same then - location = "west tip" - elseif not east_same and west_same then - location = "east tip" - elseif not east_same and not west_same then - location = "all" - end - end - end - return location + --Report which part of the entity the cursor covers. + rendering.draw_circle({ + color = { 1, 0.0, 0.5 }, + radius = 0.1, + width = 2, + target = { x = x + 0, y = y - 1 }, + surface = p.surface, + time_to_live = 30, + }) + rendering.draw_circle({ + color = { 1, 0.0, 0.5 }, + radius = 0.1, + width = 2, + target = { x = x + 0, y = y + 1 }, + surface = p.surface, + time_to_live = 30, + }) + rendering.draw_circle({ + color = { 1, 0.0, 0.5 }, + radius = 0.1, + width = 2, + target = { x = x - 1, y = y - 0 }, + surface = p.surface, + time_to_live = 30, + }) + rendering.draw_circle({ + color = { 1, 0.0, 0.5 }, + radius = 0.1, + width = 2, + target = { x = x + 1, y = y - 0 }, + surface = p.surface, + time_to_live = 30, + }) + + local ent_north = + p.surface.find_entities_filtered({ position = { x = x, y = y - 1 }, name = excluded_names, invert = true }) + if #ent_north > 0 and ent_north[1].unit_number == preferred_ent.unit_number then + north_same = true + elseif #ent_north > 1 and ent_north[2].unit_number == preferred_ent.unit_number then + north_same = true + elseif #ent_north > 2 and ent_north[3].unit_number == preferred_ent.unit_number then + north_same = true + end + local ent_south = + p.surface.find_entities_filtered({ position = { x = x, y = y + 1 }, name = excluded_names, invert = true }) + if #ent_south > 0 and ent_south[1].unit_number == preferred_ent.unit_number then + south_same = true + elseif #ent_south > 1 and ent_south[2].unit_number == preferred_ent.unit_number then + south_same = true + elseif #ent_south > 2 and ent_south[3].unit_number == preferred_ent.unit_number then + south_same = true + end + local ent_east = + p.surface.find_entities_filtered({ position = { x = x + 1, y = y }, name = excluded_names, invert = true }) + if #ent_east > 0 and ent_east[1].unit_number == preferred_ent.unit_number then + east_same = true + elseif #ent_east > 1 and ent_east[2].unit_number == preferred_ent.unit_number then + east_same = true + elseif #ent_east > 2 and ent_east[3].unit_number == preferred_ent.unit_number then + east_same = true + end + local ent_west = + p.surface.find_entities_filtered({ position = { x = x - 1, y = y }, name = excluded_names, invert = true }) + if #ent_west > 0 and ent_west[1].unit_number == preferred_ent.unit_number then + west_same = true + elseif #ent_west > 1 and ent_west[2].unit_number == preferred_ent.unit_number then + west_same = true + elseif #ent_west > 2 and ent_west[3].unit_number == preferred_ent.unit_number then + west_same = true + end + + if north_same and south_same then + if east_same and west_same then + location = "center" + elseif east_same and not west_same then + location = "west edge" + elseif not east_same and west_same then + location = "east edge" + elseif not east_same and not west_same then + location = "middle" + end + elseif north_same and not south_same then + if east_same and west_same then + location = "south edge" + elseif east_same and not west_same then + location = "southwest corner" + elseif not east_same and west_same then + location = "southeast corner" + elseif not east_same and not west_same then + location = "south tip" + end + elseif not north_same and south_same then + if east_same and west_same then + location = "north edge" + elseif east_same and not west_same then + location = "northwest corner" + elseif not east_same and west_same then + location = "northeast corner" + elseif not east_same and not west_same then + location = "north tip" + end + elseif not north_same and not south_same then + if east_same and west_same then + location = "middle" + elseif east_same and not west_same then + location = "west tip" + elseif not east_same and west_same then + location = "east tip" + elseif not east_same and not west_same then + location = "all" + end + end + end + return location end --For a list of edge points of an aggregate entity, returns the nearest one. function mod.nearest_edge(edges, pos, name) local pos = table.deepcopy(pos) if name == "forest" then - pos.x = pos.x / 8 - pos.y = pos.y / 8 + pos.x = pos.x / 8 + pos.y = pos.y / 8 end local result = {} local min = math.huge @@ -436,7 +478,7 @@ function mod.scale_area(area, factor) end --todo: use defines directions here -function mod.area_edge(area,dir,pos,name) +function mod.area_edge(area, dir, pos, name) local adjusted_area = table.deepcopy(area) if name == "forest" then local chunk_size = 8 @@ -452,18 +494,17 @@ function mod.area_edge(area,dir,pos,name) return false end elseif dir == 2 then - if adjusted_area.right_bottom.x == math.ceil( .001 + pos.x) then + if adjusted_area.right_bottom.x == math.ceil(0.001 + pos.x) then return true else return false end elseif dir == 4 then - if adjusted_area.right_bottom.y == math.ceil(.001+pos.y) then + if adjusted_area.right_bottom.y == math.ceil(0.001 + pos.y) then return true else return false end - elseif dir == 6 then if adjusted_area.left_top.x == math.floor(pos.x) then return true @@ -475,17 +516,17 @@ end --Returns the top left and bottom right corners for a rectangle that takes pos_1 and pos_2 as any of its four corners. function mod.get_top_left_and_bottom_right(pos_1, pos_2) - local top_left = {x = math.min(pos_1.x, pos_2.x), y = math.min(pos_1.y, pos_2.y)} - local bottom_right = {x = math.max(pos_1.x, pos_2.x), y = math.max(pos_1.y, pos_2.y)} + local top_left = { x = math.min(pos_1.x, pos_2.x), y = math.min(pos_1.y, pos_2.y) } + local bottom_right = { x = math.max(pos_1.x, pos_2.x), y = math.max(pos_1.y, pos_2.y) } return top_left, bottom_right end --Finds the nearest roboport -function mod.find_nearest_roboport(surf,pos,radius_in) +function mod.find_nearest_roboport(surf, pos, radius_in) local nearest = nil local min_dist = radius_in - local ports = surf.find_entities_filtered{name = "roboport" , position = pos , radius = radius_in} - for i,port in ipairs(ports) do + local ports = surf.find_entities_filtered({ name = "roboport", position = pos, radius = radius_in }) + for i, port in ipairs(ports) do local dist = math.ceil(util.distance(pos, port.position)) if dist < min_dist then min_dist = dist @@ -493,24 +534,27 @@ function mod.find_nearest_roboport(surf,pos,radius_in) end end if nearest ~= nil then - rendering.draw_circle{color = {1, 1, 0}, radius = 4, width = 4, target = nearest.position, surface = surf, time_to_live = 90} + rendering.draw_circle({ + color = { 1, 1, 0 }, + radius = 4, + width = 4, + target = nearest.position, + surface = surf, + time_to_live = 90, + }) end return nearest, min_dist end -function mod.table_concat (T1, T2) - if T2 == nil then - return - end - if T1 == nil then - T1 = {} - end +function mod.table_concat(T1, T2) + if T2 == nil then return end + if T1 == nil then T1 = {} end for i, v in pairs(T2) do - table.insert(T1, v) + table.insert(T1, v) end end -function mod.pos2str (pos) +function mod.pos2str(pos) return pos.x .. " " .. pos.y end @@ -519,19 +563,17 @@ function mod.str2pos(str) for s in string.gmatch(str, "([^%s]+)") do table.insert(t, s) end - return {x = t[1], y = t[2]} + return { x = t[1], y = t[2] } end function mod.breakup_string(str) - result = {""} + result = { "" } if table_size(str) > 20 then local i = 0 while i < #str do - if i%20 == 0 then - table.insert(result, {""}) - end + if i % 20 == 0 then table.insert(result, { "" }) end ---@diagnostic disable-next-line: param-type-mismatch - table.insert(result[math.ceil((i+1)/20)+1], table.deepcopy(str[i+1])) + table.insert(result[math.ceil((i + 1) / 20) + 1], table.deepcopy(str[i + 1])) i = i + 1 end return result @@ -551,91 +593,85 @@ end --Converts an array into a lookup table based on the keys it has. function mod.into_lookup(array) - local lookup = {} - for key, value in pairs(array) do - lookup[value] = key - end - return lookup + local lookup = {} + for key, value in pairs(array) do + lookup[value] = key + end + return lookup end --Returns the part of a substring before a space character. BUG: Breaks when parsing dashes. function mod.get_substring_before_space(str) - local first, final = string.find(str," ") + local first, final = string.find(str, " ") if first == nil or first == 1 then --No space, or space at the start only return str else - return string.sub(str,1,first-1) + return string.sub(str, 1, first - 1) end end --Returns the part of a substring after a space character. BUG: Breaks when parsing dashes. function mod.get_substring_after_space(str) - local first, final = string.find(str," ") + local first, final = string.find(str, " ") if final == nil then --No spaces return str end if first == 1 then --spaces at start only - return string.sub(str,final+1,string.len(str)) + return string.sub(str, final + 1, string.len(str)) end - + if final == string.len(str) then --space at the end only? return str end - - return string.sub(str,final+1,string.len(str)) + + return string.sub(str, final + 1, string.len(str)) end --Returns the part of a substring before a comma character. BUG: Breaks when parsing dashes. function mod.get_substring_before_comma(str) - local first, final = string.find(str,",") + local first, final = string.find(str, ",") if first == nil or first == 1 then return str else - return string.sub(str,1,first-1) + return string.sub(str, 1, first - 1) end end function mod.get_substring_before_dash(str) - local first, final = string.find(str,"-") + local first, final = string.find(str, "-") if first == nil or first == 1 then return str else - return string.sub(str,1,first-1) + return string.sub(str, 1, first - 1) end end function mod.dir_dist_locale_h(dir_dist) - return {"access.dir-dist",{"access.direction",dir_dist[1]},math.floor(dir_dist[2]+0.5)} + return { "access.dir-dist", { "access.direction", dir_dist[1] }, math.floor(dir_dist[2] + 0.5) } end -function mod.dir_dist_locale(pos1,pos2) - return mod.dir_dist_locale_h( mod.dir_dist(pos1,pos2) ) +function mod.dir_dist_locale(pos1, pos2) + return mod.dir_dist_locale_h(mod.dir_dist(pos1, pos2)) end function mod.ent_name_locale(ent) if ent.name == "water" then print("todo: water isn't an entity") - return {"gui-map-generator.water"} + return { "gui-map-generator.water" } end if ent.name == "forest" then print("todo: forest isn't an entity") - return {"access.forest"} - end - if not game.entity_prototypes[ent.name] then - error(ent.name .. " is not an entity") + return { "access.forest" } end + if not game.entity_prototypes[ent.name] then error(ent.name .. " is not an entity") end return ent.localised_name or game.entity_prototypes[ent.name].localised_name end --small utility function for getting the index of a named object from an array of objects. function mod.index_of_entity(array, value) - if next(array) == nil then - return nil - end - for i = 1, #array,1 do - if array[i].name == value then - return i - end + if next(array) == nil then return nil end + for i = 1, #array, 1 do + if array[i].name == value then return i end end return nil end @@ -644,29 +680,25 @@ end function mod.floor_to_nearest_k_after_10k(num_in) local num = num_in num = math.ceil(num) - if num > 10000 then - num = 1000 * math.floor(num/1000) - end - if num > 1000000 then - num = 100000 * math.floor(num/100000) - end + if num > 10000 then num = 1000 * math.floor(num / 1000) end + if num > 1000000 then num = 100000 * math.floor(num / 100000) end return num end --Returns a string to say the quantity of an item in terms of stacks, if there is at least one stack -function mod.express_in_stacks(count,stack_size,precise) +function mod.express_in_stacks(count, stack_size, precise) local result = "" local new_count = "unknown amount of" local units = " units " if count == nil then count = 0 - elseif count == 0 then + elseif count == 0 then units = " units " new_count = "0" - elseif count == 1 then + elseif count == 1 then units = " unit " new_count = "1" - elseif count < stack_size then + elseif count < stack_size then units = " units " new_count = tostring(count) elseif count == stack_size then @@ -680,9 +712,7 @@ function mod.express_in_stacks(count,stack_size,precise) if precise and count > stack_size and count % stack_size > 0 then result = result .. " and " .. count % stack_size .. " units " end - if count > 10000 then - result = "infinite" - end + if count > 10000 then result = "infinite" end return result end @@ -701,30 +731,67 @@ end --If the cursor is over a water tile, this function is called to check if it is open water or a shore. function mod.identify_water_shores(pindex) local p = game.get_player(pindex) - local water_tile_names = {"water", "deepwater", "water-green", "deepwater-green", "water-shallow", "water-mud", "water-wube"} + local water_tile_names = + { "water", "deepwater", "water-green", "deepwater-green", "water-shallow", "water-mud", "water-wube" } local pos = players[pindex].cursor_pos - rendering.draw_circle{color = {1, 0.0, 0.5},radius = 0.1,width = 2,target = {x = pos.x+0 ,y = pos.y-1}, surface = p.surface, time_to_live = 30} - rendering.draw_circle{color = {1, 0.0, 0.5},radius = 0.1,width = 2,target = {x = pos.x+0 ,y = pos.y+1}, surface = p.surface, time_to_live = 30} - rendering.draw_circle{color = {1, 0.0, 0.5},radius = 0.1,width = 2,target = {x = pos.x-1 ,y = pos.y-0}, surface = p.surface, time_to_live = 30} - rendering.draw_circle{color = {1, 0.0, 0.5},radius = 0.1,width = 2,target = {x = pos.x+1 ,y = pos.y-0}, surface = p.surface, time_to_live = 30} - - local tile_north = #p.surface.find_tiles_filtered{position = {x = pos.x+0, y = pos.y-1},radius = 0.1, name = water_tile_names } - local tile_south = #p.surface.find_tiles_filtered{position = {x = pos.x+0, y = pos.y+1},radius = 0.1, name = water_tile_names } - local tile_east = #p.surface.find_tiles_filtered{position = {x = pos.x+1, y = pos.y+0},radius = 0.1, name = water_tile_names } - local tile_west = #p.surface.find_tiles_filtered{position = {x = pos.x-1, y = pos.y+0},radius = 0.1, name = water_tile_names } - - if (tile_north > 0) then - tile_north = 1 - end - if (tile_south > 0) then - tile_south = 1 - end - if (tile_east > 0) then - tile_east = 1 - end - if (tile_west > 0) then - tile_west = 1 - end + rendering.draw_circle({ + color = { 1, 0.0, 0.5 }, + radius = 0.1, + width = 2, + target = { x = pos.x + 0, y = pos.y - 1 }, + surface = p.surface, + time_to_live = 30, + }) + rendering.draw_circle({ + color = { 1, 0.0, 0.5 }, + radius = 0.1, + width = 2, + target = { x = pos.x + 0, y = pos.y + 1 }, + surface = p.surface, + time_to_live = 30, + }) + rendering.draw_circle({ + color = { 1, 0.0, 0.5 }, + radius = 0.1, + width = 2, + target = { x = pos.x - 1, y = pos.y - 0 }, + surface = p.surface, + time_to_live = 30, + }) + rendering.draw_circle({ + color = { 1, 0.0, 0.5 }, + radius = 0.1, + width = 2, + target = { x = pos.x + 1, y = pos.y - 0 }, + surface = p.surface, + time_to_live = 30, + }) + + local tile_north = #p.surface.find_tiles_filtered({ + position = { x = pos.x + 0, y = pos.y - 1 }, + radius = 0.1, + name = water_tile_names, + }) + local tile_south = #p.surface.find_tiles_filtered({ + position = { x = pos.x + 0, y = pos.y + 1 }, + radius = 0.1, + name = water_tile_names, + }) + local tile_east = #p.surface.find_tiles_filtered({ + position = { x = pos.x + 1, y = pos.y + 0 }, + radius = 0.1, + name = water_tile_names, + }) + local tile_west = #p.surface.find_tiles_filtered({ + position = { x = pos.x - 1, y = pos.y + 0 }, + radius = 0.1, + name = water_tile_names, + }) + + if tile_north > 0 then tile_north = 1 end + if tile_south > 0 then tile_south = 1 end + if tile_east > 0 then tile_east = 1 end + if tile_west > 0 then tile_west = 1 end local sum = tile_north + tile_south + tile_east + tile_west local result = " " diff --git a/scripts/field-ref.lua b/scripts/field-ref.lua index 0064a980..728d87f3 100644 --- a/scripts/field-ref.lua +++ b/scripts/field-ref.lua @@ -147,7 +147,7 @@ local function compile(path) set = function(object, val) local ret, last = follow_path(object, path, true) ret[last] = val - end + end, } return setmetatable(funcs, { __newindex = no_setting_meta }) @@ -167,7 +167,7 @@ local function capture_path_and_build(path) -- cloned simply by being created, but an extra clone doesn't hurt on -- the slow, infrequent path. return compile(copy(path)) - end + end, } -- The empty table has no keys and so will always call our metatable methods. @@ -188,11 +188,13 @@ local test_value = { f1 = "f1", f2 = { f1 = "f2.f1", - } + }, } -- No root path compilation. -ok = pcall(function() F() end) +ok = pcall(function() + F() +end) assert(ok == false) -- 1 field deep works. @@ -211,7 +213,7 @@ assert(test_value.f2.f1 == "new2") -- We can work over something mixed between strings and numbers. local mixed = { - f = { 1, 2, 3 } + f = { 1, 2, 3 }, } local mixed_ref = F.f[2]() assert(mixed_ref.get(mixed) == 2) diff --git a/scripts/graphics.lua b/scripts/graphics.lua index ba4f4994..754bcec9 100644 --- a/scripts/graphics.lua +++ b/scripts/graphics.lua @@ -15,7 +15,7 @@ function mod.show_sprite_demo(pindex) local sprite3 = "item-group.environment" local sprite4 = "item-group.other" local sprite5 = "item.iron-gear-wheel" - --Let the gunction do the rest. Clear it with CTRL + ALT + R + --Let the gunction do the rest. Clear it with CTRL + ALT + R local player = players[pindex] local p = game.get_player(pindex) @@ -27,41 +27,21 @@ function mod.show_sprite_demo(pindex) local s5 = nil --Set the frame if f == nil or not f.valid then - f = game.get_player(pindex).gui.screen.add{type="frame"} + f = game.get_player(pindex).gui.screen.add({ type = "frame" }) f.force_auto_center() f.bring_to_front() end --Set the main sprite - if s1 == nil or not s1.valid then - s1 = f.add{type="sprite",caption = "custom menu"} - end - if s1.sprite ~= sprite1 then - s1.sprite = sprite1 - end - if s2 == nil or not s2.valid then - s2 = f.add{type="sprite",caption = "custom menu"} - end - if s2.sprite ~= sprite2 then - s2.sprite = sprite2 - end - if s3 == nil or not s3.valid then - s3 = f.add{type="sprite",caption = "custom menu"} - end - if s3.sprite ~= sprite3 then - s3.sprite = sprite3 - end - if s4 == nil or not s4.valid then - s4 = f.add{type="sprite",caption = "custom menu"} - end - if s4.sprite ~= sprite4 then - s4.sprite = sprite4 - end - if s5 == nil or not s5.valid then - s5 = f.add{type="sprite",caption = "custom menu"} - end - if s5.sprite ~= sprite5 then - s5.sprite = sprite5 - end + if s1 == nil or not s1.valid then s1 = f.add({ type = "sprite", caption = "custom menu" }) end + if s1.sprite ~= sprite1 then s1.sprite = sprite1 end + if s2 == nil or not s2.valid then s2 = f.add({ type = "sprite", caption = "custom menu" }) end + if s2.sprite ~= sprite2 then s2.sprite = sprite2 end + if s3 == nil or not s3.valid then s3 = f.add({ type = "sprite", caption = "custom menu" }) end + if s3.sprite ~= sprite3 then s3.sprite = sprite3 end + if s4 == nil or not s4.valid then s4 = f.add({ type = "sprite", caption = "custom menu" }) end + if s4.sprite ~= sprite4 then s4.sprite = sprite4 end + if s5 == nil or not s5.valid then s5 = f.add({ type = "sprite", caption = "custom menu" }) end + if s5.sprite ~= sprite5 then s5.sprite = sprite5 end --test style changes... s5.style.size = 5 @@ -72,107 +52,105 @@ function mod.update_menu_visuals() for pindex, player in pairs(players) do if player.in_menu then if player.menu == "technology" then - mod.update_overhead_sprite("item.lab",2,1.25,pindex) + mod.update_overhead_sprite("item.lab", 2, 1.25, pindex) mod.update_custom_GUI_sprite("item.lab", 3, pindex) elseif player.menu == "inventory" then - mod.update_overhead_sprite("item.wooden-chest",2,1.25,pindex) + mod.update_overhead_sprite("item.wooden-chest", 2, 1.25, pindex) mod.update_custom_GUI_sprite("item.wooden-chest", 3, pindex) - if players[pindex].vanilla_mode then - mod.update_custom_GUI_sprite(nil,1,pindex) - end + if players[pindex].vanilla_mode then mod.update_custom_GUI_sprite(nil, 1, pindex) end elseif player.menu == "crafting" then - mod.update_overhead_sprite("item.repair-pack",2,1.25,pindex) + mod.update_overhead_sprite("item.repair-pack", 2, 1.25, pindex) mod.update_custom_GUI_sprite("item.repair-pack", 3, pindex) elseif player.menu == "crafting_queue" then - mod.update_overhead_sprite("item.repair-pack",2,1.25,pindex) + mod.update_overhead_sprite("item.repair-pack", 2, 1.25, pindex) mod.update_custom_GUI_sprite("item.repair-pack", 3, pindex, "utility.clock") elseif player.menu == "player_trash" then - mod.update_overhead_sprite("utility.trash_white",2,1.25,pindex) + mod.update_overhead_sprite("utility.trash_white", 2, 1.25, pindex) mod.update_custom_GUI_sprite("utility.trash_white", 3, pindex) elseif player.menu == "travel" then - mod.update_overhead_sprite("utility.downloading_white",4,1.25,pindex) + mod.update_overhead_sprite("utility.downloading_white", 4, 1.25, pindex) mod.update_custom_GUI_sprite("utility.downloading_white", 3, pindex) elseif player.menu == "warnings" then - mod.update_overhead_sprite("utility.warning_white",4,1.25,pindex) + mod.update_overhead_sprite("utility.warning_white", 4, 1.25, pindex) mod.update_custom_GUI_sprite("utility.warning_white", 3, pindex) elseif player.menu == "rail_builder" then - mod.update_overhead_sprite("item.rail",2,1.25,pindex) + mod.update_overhead_sprite("item.rail", 2, 1.25, pindex) mod.update_custom_GUI_sprite("item.rail", 3, pindex) elseif player.menu == "train_menu" then - mod.update_overhead_sprite("item.locomotive",2,1.25,pindex) + mod.update_overhead_sprite("item.locomotive", 2, 1.25, pindex) mod.update_custom_GUI_sprite("item.locomotive", 3, pindex) elseif player.menu == "spider_menu" then - mod.update_overhead_sprite("item.spidertron",2,1.25,pindex) + mod.update_overhead_sprite("item.spidertron", 2, 1.25, pindex) mod.update_custom_GUI_sprite("item.spidertron", 3, pindex) elseif player.menu == "train_stop_menu" then - mod.update_overhead_sprite("item.train-stop",2,1.25,pindex) + mod.update_overhead_sprite("item.train-stop", 2, 1.25, pindex) mod.update_custom_GUI_sprite("item.train-stop", 3, pindex) elseif player.menu == "roboport_menu" then - mod.update_overhead_sprite("item.roboport",2,1.25,pindex) + mod.update_overhead_sprite("item.roboport", 2, 1.25, pindex) mod.update_custom_GUI_sprite("item.roboport", 3, pindex) elseif player.menu == "blueprint_menu" then - mod.update_overhead_sprite("item.blueprint",2,1.25,pindex) + mod.update_overhead_sprite("item.blueprint", 2, 1.25, pindex) mod.update_custom_GUI_sprite("item.blueprint", 3, pindex) elseif player.menu == "blueprint_book_menu" then - mod.update_overhead_sprite("item.blueprint-book",2,1.25,pindex) + mod.update_overhead_sprite("item.blueprint-book", 2, 1.25, pindex) mod.update_custom_GUI_sprite("item.blueprint-book", 3, pindex) elseif player.menu == "circuit_network_menu" then - mod.update_overhead_sprite("item.electronic-circuit",2,1.25,pindex) + mod.update_overhead_sprite("item.electronic-circuit", 2, 1.25, pindex) mod.update_custom_GUI_sprite("item.electronic-circuit", 3, pindex) elseif player.menu == "signal_selector" then local sprite = "item-group.signals" - mod.update_overhead_sprite(sprite,1,1.25,pindex) + mod.update_overhead_sprite(sprite, 1, 1.25, pindex) mod.update_custom_GUI_sprite(sprite, 0.5, pindex) elseif player.menu == "pump" then - mod.update_overhead_sprite("item.offshore-pump",2,1.25,pindex) + mod.update_overhead_sprite("item.offshore-pump", 2, 1.25, pindex) mod.update_custom_GUI_sprite("item.offshore-pump", 3, pindex) elseif player.menu == "belt" then - mod.update_overhead_sprite("item.transport-belt",2,1.25,pindex) - mod.update_custom_GUI_sprite(nil,1,pindex) - elseif (players[pindex].menu == "building" or players[pindex].menu == "vehicle") then + mod.update_overhead_sprite("item.transport-belt", 2, 1.25, pindex) + mod.update_custom_GUI_sprite(nil, 1, pindex) + elseif players[pindex].menu == "building" or players[pindex].menu == "vehicle" then if game.get_player(pindex).opened == nil then --Open building menu with no GUI - mod.update_overhead_sprite("utility.search_white",2,1.25,pindex) + mod.update_overhead_sprite("utility.search_white", 2, 1.25, pindex) mod.update_custom_GUI_sprite("utility.search_white", 3, pindex) else --A building with a GUI is open - mod.update_overhead_sprite("utility.search_white",2,1.25,pindex) - mod.update_custom_GUI_sprite(nil,1,pindex) + mod.update_overhead_sprite("utility.search_white", 2, 1.25, pindex) + mod.update_custom_GUI_sprite(nil, 1, pindex) end - elseif (players[pindex].menu == "building_no_sectors" or players[pindex].menu == "vehicle_no_sectors") then + elseif players[pindex].menu == "building_no_sectors" or players[pindex].menu == "vehicle_no_sectors" then if game.get_player(pindex).opened == nil then --Open building menu with no GUI - mod.update_overhead_sprite("utility.search_white",2,1.25,pindex) - mod.update_custom_GUI_sprite("utility.search_white", 3, pindex,"utility.questionmark") + mod.update_overhead_sprite("utility.search_white", 2, 1.25, pindex) + mod.update_custom_GUI_sprite("utility.search_white", 3, pindex, "utility.questionmark") else --A building with a GUI is open - mod.update_overhead_sprite("utility.search_white",2,1.25,pindex) - mod.update_custom_GUI_sprite(nil,1,pindex) + mod.update_overhead_sprite("utility.search_white", 2, 1.25, pindex) + mod.update_custom_GUI_sprite(nil, 1, pindex) end elseif player.menu == "structure-travel" then - mod.update_overhead_sprite("utility.expand_dots_white",2,1.25,pindex) - mod.update_custom_GUI_sprite("utility.expand_dots_white",3,pindex) + mod.update_overhead_sprite("utility.expand_dots_white", 2, 1.25, pindex) + mod.update_custom_GUI_sprite("utility.expand_dots_white", 3, pindex) else --Other menu type ... if player.vanilla_mode then --No "missing image" - mod.update_overhead_sprite(nil,1,1,pindex) - mod.update_custom_GUI_sprite(nil,1,pindex) + mod.update_overhead_sprite(nil, 1, 1, pindex) + mod.update_custom_GUI_sprite(nil, 1, pindex) else --"Missing image" - mod.update_overhead_sprite("utility.select_icon_white",1,1,pindex) - mod.update_custom_GUI_sprite("utility.select_icon_white",1,pindex) + mod.update_overhead_sprite("utility.select_icon_white", 1, 1, pindex) + mod.update_custom_GUI_sprite("utility.select_icon_white", 1, pindex) end end else if game.get_player(pindex).opened ~= nil then --Not in menu, but open GUI - mod.update_overhead_sprite("utility.white_square",2,1.25,pindex) - mod.update_custom_GUI_sprite(nil,1,pindex) + mod.update_overhead_sprite("utility.white_square", 2, 1.25, pindex) + mod.update_custom_GUI_sprite(nil, 1, pindex) else --Not in menu, no open GUI - mod.update_overhead_sprite(nil,1,1,pindex) - mod.update_custom_GUI_sprite(nil,1,pindex) + mod.update_overhead_sprite(nil, 1, 1, pindex) + mod.update_custom_GUI_sprite(nil, 1, pindex) end end end @@ -181,14 +159,10 @@ end --Updates graphics to match the mod's current construction preview in hand. Draws stuff like the building footprint, direction indicator arrow, selection tool selection box. Also moves the mouse pointer to hold the preview at the correct position on screen. function mod.sync_build_cursor_graphics(pindex) local player = players[pindex] - if player == nil or player.player.character == nil then - return - end + if player == nil or player.player.character == nil then return end local p = game.get_player(pindex) local stack = game.get_player(pindex).cursor_stack - if player.building_direction == nil then - player.building_direction = dirs.north - end + if player.building_direction == nil then player.building_direction = dirs.north end turn_to_cursor_direction_cardinal(pindex) local dir = player.building_direction local dir_indicator = player.building_dir_arrow @@ -204,30 +178,41 @@ function mod.sync_build_cursor_graphics(pindex) if players[pindex].build_lock and not players[pindex].cursor and stack.name ~= "rail" then arrow_pos = fa_utils.center_of_tile(fa_utils.offset_position(arrow_pos, players[pindex].player_direction, -2)) end - player.building_dir_arrow = rendering.draw_sprite{sprite = "fluid.crude-oil", tint = {r = 0.25, b = 0.25, g = 1.0, a = 0.75}, render_layer = "254", - surface = game.get_player(pindex).surface, players = nil, target = arrow_pos, orientation = dir/(2 * dirs.south)} + player.building_dir_arrow = rendering.draw_sprite({ + sprite = "fluid.crude-oil", + tint = { r = 0.25, b = 0.25, g = 1.0, a = 0.75 }, + render_layer = "254", + surface = game.get_player(pindex).surface, + players = nil, + target = arrow_pos, + orientation = dir / (2 * dirs.south), + }) dir_indicator = player.building_dir_arrow - rendering.set_visible(dir_indicator,true) - if players[pindex].hide_cursor or stack.name == "locomotive" or stack.name == "cargo-wagon" or stack.name == "fluid-wagon" or stack.name == "artillery-wagon" then - rendering.set_visible(dir_indicator,false) + rendering.set_visible(dir_indicator, true) + if + players[pindex].hide_cursor + or stack.name == "locomotive" + or stack.name == "cargo-wagon" + or stack.name == "fluid-wagon" + or stack.name == "artillery-wagon" + then + rendering.set_visible(dir_indicator, false) end --Redraw footprint (ent) - if player.building_footprint ~= nil then - rendering.destroy(player.building_footprint) - end + if player.building_footprint ~= nil then rendering.destroy(player.building_footprint) end --Get correct width and height width = stack.prototype.place_result.tile_width height = stack.prototype.place_result.tile_height if dir == dirs.east or dir == dirs.west then - --Flip width and height. Note: diagonal cases are rounded to north/south cases + --Flip width and height. Note: diagonal cases are rounded to north/south cases height = stack.prototype.place_result.tile_width width = stack.prototype.place_result.tile_height end - left_top = {x = math.floor(player.cursor_pos.x),y = math.floor(player.cursor_pos.y)} - right_bottom = {x = (left_top.x + width), y = (left_top.y + height)} + left_top = { x = math.floor(player.cursor_pos.x), y = math.floor(player.cursor_pos.y) } + right_bottom = { x = (left_top.x + width), y = (left_top.y + height) } if not player.cursor then --Apply offsets when facing west or north so that items can be placed in front of the character @@ -249,41 +234,55 @@ function mod.sync_build_cursor_graphics(pindex) size_offset = -width + 1 end left_top = fa_utils.offset_position(left_top, players[pindex].player_direction, base_offset + size_offset) - right_bottom = fa_utils.offset_position(right_bottom, players[pindex].player_direction, base_offset + size_offset) + right_bottom = + fa_utils.offset_position(right_bottom, players[pindex].player_direction, base_offset + size_offset) end end - --Update the footprint info and draw it + --Update the footprint info and draw it player.building_footprint_left_top = left_top player.building_footprint_right_bottom = right_bottom - player.building_footprint = rendering.draw_rectangle{left_top = left_top, right_bottom = right_bottom , color = {r = 0.25, b = 0.25, g = 1.0, a = 0.75}, draw_on_ground = true, surface = game.get_player(pindex).surface, players = nil } - rendering.set_visible(player.building_footprint,true) + player.building_footprint = rendering.draw_rectangle({ + left_top = left_top, + right_bottom = right_bottom, + color = { r = 0.25, b = 0.25, g = 1.0, a = 0.75 }, + draw_on_ground = true, + surface = game.get_player(pindex).surface, + players = nil, + }) + rendering.set_visible(player.building_footprint, true) --Hide the drawing in the desired cases - if players[pindex].hide_cursor or stack.name == "locomotive" or stack.name == "cargo-wagon" or stack.name == "fluid-wagon" or stack.name == "artillery-wagon" then - rendering.set_visible(player.building_footprint,false) + if + players[pindex].hide_cursor + or stack.name == "locomotive" + or stack.name == "cargo-wagon" + or stack.name == "fluid-wagon" + or stack.name == "artillery-wagon" + then + rendering.set_visible(player.building_footprint, false) end --Move mouse cursor according to building box if player.cursor then --Adjust for cursor - local new_pos = {x = (left_top.x + width/2),y = (left_top.y + height/2)} - fa_mouse.move_mouse_pointer(new_pos,pindex) + local new_pos = { x = (left_top.x + width / 2), y = (left_top.y + height / 2) } + fa_mouse.move_mouse_pointer(new_pos, pindex) else --Adjust for direct placement local pos = player.cursor_pos if p_dir == dirs.north then - pos = fa_utils.offset_position(pos, dirs.north, height/2 - 0.5) - pos = fa_utils.offset_position(pos, dirs.east, width/2 - 0.5) + pos = fa_utils.offset_position(pos, dirs.north, height / 2 - 0.5) + pos = fa_utils.offset_position(pos, dirs.east, width / 2 - 0.5) elseif p_dir == dirs.east then - pos = fa_utils.offset_position(pos, dirs.south, height/2 - 0.5) - pos = fa_utils.offset_position(pos, dirs.east, width/2 - 0.5) + pos = fa_utils.offset_position(pos, dirs.south, height / 2 - 0.5) + pos = fa_utils.offset_position(pos, dirs.east, width / 2 - 0.5) elseif p_dir == dirs.south then - pos = fa_utils.offset_position(pos, dirs.south, height/2 - 0.5) - pos = fa_utils.offset_position(pos, dirs.east, width/2 - 0.5) + pos = fa_utils.offset_position(pos, dirs.south, height / 2 - 0.5) + pos = fa_utils.offset_position(pos, dirs.east, width / 2 - 0.5) elseif p_dir == dirs.west then - pos = fa_utils.offset_position(pos, dirs.south, height/2 - 0.5) - pos = fa_utils.offset_position(pos, dirs.west, width/2 - 0.5) + pos = fa_utils.offset_position(pos, dirs.south, height / 2 - 0.5) + pos = fa_utils.offset_position(pos, dirs.west, width / 2 - 0.5) end --In build lock mode and outside cursor mode, build from behind the player @@ -297,13 +296,19 @@ function mod.sync_build_cursor_graphics(pindex) end pos = fa_utils.offset_position(pos, players[pindex].player_direction, base_offset + size_offset) end - fa_mouse.move_mouse_pointer(pos,pindex) + fa_mouse.move_mouse_pointer(pos, pindex) end elseif stack == nil or not stack.valid_for_read then --Invalid stack: Hide the objects - if dir_indicator ~= nil then rendering.set_visible(dir_indicator,false) end - if player.building_footprint ~= nil then rendering.set_visible(player.building_footprint,false) end - elseif stack and stack.valid_for_read and stack.is_blueprint and stack.is_blueprint_setup() and players[pindex].blueprint_reselecting ~= true then + if dir_indicator ~= nil then rendering.set_visible(dir_indicator, false) end + if player.building_footprint ~= nil then rendering.set_visible(player.building_footprint, false) end + elseif + stack + and stack.valid_for_read + and stack.is_blueprint + and stack.is_blueprint_setup() + and players[pindex].blueprint_reselecting ~= true + then --Blueprints have their own data: --Redraw the direction indicator arrow if dir_indicator ~= nil then rendering.destroy(player.building_dir_arrow) end @@ -313,61 +318,90 @@ function mod.sync_build_cursor_graphics(pindex) players[pindex].blueprint_hand_direction = dirs.north dir = dirs.north end - player.building_dir_arrow = rendering.draw_sprite{sprite = "fluid.crude-oil", tint = {r = 0.25, b = 0.25, g = 1.0, a = 0.75}, render_layer = "254", - surface = game.get_player(pindex).surface, players = nil, target = arrow_pos, orientation = dir/(2 * dirs.south)} + player.building_dir_arrow = rendering.draw_sprite({ + sprite = "fluid.crude-oil", + tint = { r = 0.25, b = 0.25, g = 1.0, a = 0.75 }, + render_layer = "254", + surface = game.get_player(pindex).surface, + players = nil, + target = arrow_pos, + orientation = dir / (2 * dirs.south), + }) dir_indicator = player.building_dir_arrow - rendering.set_visible(dir_indicator,true) + rendering.set_visible(dir_indicator, true) --Redraw the bp footprint - if player.building_footprint ~= nil then - rendering.destroy(player.building_footprint) - end + if player.building_footprint ~= nil then rendering.destroy(player.building_footprint) end local bp_width = players[pindex].blueprint_width_in_hand local bp_height = players[pindex].blueprint_height_in_hand if bp_width ~= nil then - local left_top = {x = math.floor(player.cursor_pos.x),y = math.floor(player.cursor_pos.y)} - local right_bottom = {x = (left_top.x + bp_width), y = (left_top.y + bp_height)} - local center_pos = {x = (left_top.x + bp_width/2), y = (left_top.y + bp_height/2)} - player.building_footprint = rendering.draw_rectangle{left_top = left_top, right_bottom = right_bottom, color = {r = 0.25, b = 0.25, g = 1.0, a = 0.75}, width = 2, draw_on_ground = true, surface = p.surface, players = nil} - rendering.set_visible(player.building_footprint,true) + local left_top = { x = math.floor(player.cursor_pos.x), y = math.floor(player.cursor_pos.y) } + local right_bottom = { x = (left_top.x + bp_width), y = (left_top.y + bp_height) } + local center_pos = { x = (left_top.x + bp_width / 2), y = (left_top.y + bp_height / 2) } + player.building_footprint = rendering.draw_rectangle({ + left_top = left_top, + right_bottom = right_bottom, + color = { r = 0.25, b = 0.25, g = 1.0, a = 0.75 }, + width = 2, + draw_on_ground = true, + surface = p.surface, + players = nil, + }) + rendering.set_visible(player.building_footprint, true) --Move the mouse pointer if fa_mouse.cursor_position_is_on_screen_with_player_centered(pindex) then - fa_mouse.move_mouse_pointer(center_pos,pindex) + fa_mouse.move_mouse_pointer(center_pos, pindex) else - fa_mouse.move_mouse_pointer(players[pindex].position,pindex) + fa_mouse.move_mouse_pointer(players[pindex].position, pindex) end end else --Hide the objects - if dir_indicator ~= nil then rendering.set_visible(dir_indicator,false) end - if player.building_footprint ~= nil then rendering.set_visible(player.building_footprint,false) end + if dir_indicator ~= nil then rendering.set_visible(dir_indicator, false) end + if player.building_footprint ~= nil then rendering.set_visible(player.building_footprint, false) end --Tile placement preview if stack.valid and stack.prototype.place_as_tile_result and players[pindex].blueprint_reselecting ~= true then - local left_top = {math.floor(players[pindex].cursor_pos.x)-players[pindex].cursor_size,math.floor(players[pindex].cursor_pos.y)-players[pindex].cursor_size} - local right_bottom = {math.floor(players[pindex].cursor_pos.x)+players[pindex].cursor_size+1,math.floor(players[pindex].cursor_pos.y)+players[pindex].cursor_size+1} - mod.draw_large_cursor(left_top,right_bottom,pindex, {r = 0.25, b = 0.25, g = 1.0, a = 0.75}) - elseif (stack.is_blueprint or stack.is_deconstruction_item or stack.is_upgrade_item) and (players[pindex].bp_selecting == true) then + local left_top = { + math.floor(players[pindex].cursor_pos.x) - players[pindex].cursor_size, + math.floor(players[pindex].cursor_pos.y) - players[pindex].cursor_size, + } + local right_bottom = { + math.floor(players[pindex].cursor_pos.x) + players[pindex].cursor_size + 1, + math.floor(players[pindex].cursor_pos.y) + players[pindex].cursor_size + 1, + } + mod.draw_large_cursor(left_top, right_bottom, pindex, { r = 0.25, b = 0.25, g = 1.0, a = 0.75 }) + elseif + (stack.is_blueprint or stack.is_deconstruction_item or stack.is_upgrade_item) + and (players[pindex].bp_selecting == true) + then --Draw planner rectangles - local top_left, bottom_right = fa_utils.get_top_left_and_bottom_right(players[pindex].bp_select_point_1, players[pindex].cursor_pos) - local color = {1,1,1} + local top_left, bottom_right = + fa_utils.get_top_left_and_bottom_right(players[pindex].bp_select_point_1, players[pindex].cursor_pos) + local color = { 1, 1, 1 } if stack.is_blueprint then - color = {r = 0.25, b = 1.00, g = 0.50, a = 0.75} + color = { r = 0.25, b = 1.00, g = 0.50, a = 0.75 } elseif stack.is_deconstruction_item then - color = {r = 1.00, b = 0.25, g = 0.50, a = 0.75} + color = { r = 1.00, b = 0.25, g = 0.50, a = 0.75 } elseif stack.is_upgrade_item then - color = {r = 0.25, b = 0.25, g = 1.00, a = 0.75} + color = { r = 0.25, b = 0.25, g = 1.00, a = 0.75 } end - player.building_footprint = rendering.draw_rectangle{color = color, width = 2, surface = game.get_player(pindex).surface, left_top = top_left, right_bottom = bottom_right, draw_on_ground = false, players = nil} - rendering.set_visible(player.building_footprint,true) + player.building_footprint = rendering.draw_rectangle({ + color = color, + width = 2, + surface = game.get_player(pindex).surface, + left_top = top_left, + right_bottom = bottom_right, + draw_on_ground = false, + players = nil, + }) + rendering.set_visible(player.building_footprint, true) end end --Recolor cursor boxes if multiplayer - if game.is_multiplayer() then - mod.set_cursor_colors_to_player_colors(pindex) - end + if game.is_multiplayer() then mod.set_cursor_colors_to_player_colors(pindex) end end --Draws the mod cursor box and highlights an entity selected by the cursor. Also moves the mouse pointer to the mod cursor position. @@ -376,15 +410,9 @@ function mod.draw_cursor_highlight(pindex, ent, box_type, skip_mouse_movement) local c_pos = players[pindex].cursor_pos local h_box = players[pindex].cursor_ent_highlight_box local h_tile = players[pindex].cursor_tile_highlight_box - if c_pos == nil then - return - end - if h_box ~= nil and h_box.valid then - h_box.destroy() - end - if h_tile ~= nil and rendering.is_valid(h_tile) then - rendering.destroy(h_tile) - end + if c_pos == nil then return end + if h_box ~= nil and h_box.valid then h_box.destroy() end + if h_tile ~= nil and rendering.is_valid(h_tile) then rendering.destroy(h_tile) end if players[pindex].hide_cursor then players[pindex].cursor_ent_highlight_box = nil @@ -393,8 +421,15 @@ function mod.draw_cursor_highlight(pindex, ent, box_type, skip_mouse_movement) end if ent ~= nil and ent.valid and ent.name ~= "highlight-box" and ent.type ~= "flying-text" then - h_box = p.surface.create_entity{name = "highlight-box", force = "neutral", surface = p.surface, render_player_index = pindex, box_type = "entity", - position = c_pos, source = ent} + h_box = p.surface.create_entity({ + name = "highlight-box", + force = "neutral", + surface = p.surface, + render_player_index = pindex, + box_type = "entity", + position = c_pos, + source = ent, + }) if box_type ~= nil then h_box.highlight_box_type = box_type else @@ -407,73 +442,77 @@ function mod.draw_cursor_highlight(pindex, ent, box_type, skip_mouse_movement) end --Highlight the currently focused ground tile. - if math.floor(c_pos.x) == math.ceil(c_pos.x) then - c_pos.x = c_pos.x - 0.01 - end - if math.floor(c_pos.y) == math.ceil(c_pos.y) then - c_pos.y = c_pos.y - 0.01 - end - h_tile = rendering.draw_rectangle{color = {0.75,1,1,0.75}, surface = p.surface, draw_on_ground = true, players = nil, - left_top = {math.floor(c_pos.x)+0.05,math.floor(c_pos.y)+0.05}, right_bottom = {math.ceil(c_pos.x)-0.05,math.ceil(c_pos.y)-0.05}} + if math.floor(c_pos.x) == math.ceil(c_pos.x) then c_pos.x = c_pos.x - 0.01 end + if math.floor(c_pos.y) == math.ceil(c_pos.y) then c_pos.y = c_pos.y - 0.01 end + h_tile = rendering.draw_rectangle({ + color = { 0.75, 1, 1, 0.75 }, + surface = p.surface, + draw_on_ground = true, + players = nil, + left_top = { math.floor(c_pos.x) + 0.05, math.floor(c_pos.y) + 0.05 }, + right_bottom = { math.ceil(c_pos.x) - 0.05, math.ceil(c_pos.y) - 0.05 }, + }) players[pindex].cursor_ent_highlight_box = h_box players[pindex].cursor_tile_highlight_box = h_tile --Recolor cursor boxes if multiplayer - if game.is_multiplayer() then - mod.set_cursor_colors_to_player_colors(pindex) - end + if game.is_multiplayer() then mod.set_cursor_colors_to_player_colors(pindex) end --Highlight nearby entities by default means (reposition the cursor) - if players[pindex].vanilla_mode or skip_mouse_movement == true then - return - end + if players[pindex].vanilla_mode or skip_mouse_movement == true then return end local stack = game.get_player(pindex).cursor_stack - if stack ~= nil and stack.valid_for_read and stack.valid and (stack.prototype.place_result ~= nil or stack.is_blueprint) then + if + stack ~= nil + and stack.valid_for_read + and stack.valid + and (stack.prototype.place_result ~= nil or stack.is_blueprint) + then return end - --Move the mouse cursor to the object on screen or to the player position for objects off screen + --Move the mouse cursor to the object on screen or to the player position for objects off screen if fa_mouse.cursor_position_is_on_screen_with_player_centered(pindex) then - fa_mouse.move_mouse_pointer(fa_utils.center_of_tile(c_pos),pindex) + fa_mouse.move_mouse_pointer(fa_utils.center_of_tile(c_pos), pindex) else - fa_mouse.move_mouse_pointer(fa_utils.center_of_tile(p.position),pindex) + fa_mouse.move_mouse_pointer(fa_utils.center_of_tile(p.position), pindex) end end --Redraws the player's cursor highlight box as a rectangle around the defined area. -function mod.draw_large_cursor(input_left_top,input_right_bottom,pindex, colour_in) +function mod.draw_large_cursor(input_left_top, input_right_bottom, pindex, colour_in) local h_tile = players[pindex].cursor_tile_highlight_box - if h_tile ~= nil then - rendering.destroy(h_tile) - end - local colour = {0.75,1,1} - if colour_in ~= nil then - colour = colour_in - end - h_tile = rendering.draw_rectangle{color = colour,surface = game.get_player(pindex).surface, left_top = input_left_top, right_bottom = input_right_bottom, draw_on_ground = true, players = nil} - rendering.set_visible(h_tile,true) + if h_tile ~= nil then rendering.destroy(h_tile) end + local colour = { 0.75, 1, 1 } + if colour_in ~= nil then colour = colour_in end + h_tile = rendering.draw_rectangle({ + color = colour, + surface = game.get_player(pindex).surface, + left_top = input_left_top, + right_bottom = input_right_bottom, + draw_on_ground = true, + players = nil, + }) + rendering.set_visible(h_tile, true) players[pindex].cursor_tile_highlight_box = h_tile --Recolor cursor boxes if multiplayer - if game.is_multiplayer() then - mod.set_cursor_colors_to_player_colors(pindex) - end + if game.is_multiplayer() then mod.set_cursor_colors_to_player_colors(pindex) end end ---@param sig SignalID local function sprite_name(sig) - local typemap={ - item="item", - fluid="fluid", - virtual="virtual-signal" + local typemap = { + item = "item", + fluid = "fluid", + virtual = "virtual-signal", } return typemap[sig.type] .. "." .. sig.name end ---@param elem LuaGuiElement ---@param icon BlueprintSignalIcon | nil -local function prep_blueprint_icon(elem,icon) +local function prep_blueprint_icon(elem, icon) if icon and icon.signal and icon.signal.name then elem.sprite = sprite_name(icon.signal) elem.visible = true @@ -496,43 +535,44 @@ function mod.update_custom_GUI_sprite(sprite, scale_in, pindex, sprite_2) local s2 = player.custom_GUI_sprite_2 --Set the frame if f == nil or not f.valid then - f = game.get_player(pindex).gui.screen.add{type="frame"} + f = game.get_player(pindex).gui.screen.add({ type = "frame" }) f.force_auto_center() f.bring_to_front() end --Set the main sprite if s1 == nil or not s1.valid then - s1 = f.add{type="sprite",caption = "custom menu"} + s1 = f.add({ type = "sprite", caption = "custom menu" }) player.custom_GUI_sprite = s1 end - if s1.sprite ~= sprite then - s1.sprite = sprite - end + if s1.sprite ~= sprite then s1.sprite = sprite end --Set the secondary sprite if sprite_2 == nil and s2 ~= nil and s2.valid then player.custom_GUI_sprite_2.visible = false elseif sprite_2 ~= nil then if s2 == nil or not s2.valid then - s2 = f.add{type="sprite",caption = "custom menu"} + s2 = f.add({ type = "sprite", caption = "custom menu" }) player.custom_GUI_sprite_2 = s2 end - if s2.sprite ~= sprite_2 then - s2.sprite = sprite_2 - end + if s2.sprite ~= sprite_2 then s2.sprite = sprite_2 end player.custom_GUI_sprite_2.visible = true end --If a blueprint is in hand, set the blueprint sprites - if players[pindex].menu == "blueprint_menu" and p.cursor_stack and p.cursor_stack.valid_for_read and p.cursor_stack.is_blueprint then + if + players[pindex].menu == "blueprint_menu" + and p.cursor_stack + and p.cursor_stack.valid_for_read + and p.cursor_stack.is_blueprint + then local bp = p.cursor_stack - local bp_icons=bp.blueprint_icons or {} - for i = 1,4 do - local player_sprite_handle='custom_GUI_sprite_'..(i+1) - local icon_sprite=player[player_sprite_handle] - if icon_sprite==nil or not icon_sprite.valid then - icon_sprite=f.add{type="sprite",caption = "custom menu"} - player[player_sprite_handle]=icon_sprite + local bp_icons = bp.blueprint_icons or {} + for i = 1, 4 do + local player_sprite_handle = "custom_GUI_sprite_" .. (i + 1) + local icon_sprite = player[player_sprite_handle] + if icon_sprite == nil or not icon_sprite.valid then + icon_sprite = f.add({ type = "sprite", caption = "custom menu" }) + player[player_sprite_handle] = icon_sprite end - prep_blueprint_icon(icon_sprite,bp_icons[i]) + prep_blueprint_icon(icon_sprite, bp_icons[i]) end end @@ -550,33 +590,41 @@ function mod.update_overhead_sprite(sprite, scale_in, radius_in, pindex) local scale = scale_in local radius = radius_in - if player.overhead_circle ~= nil then - rendering.destroy(player.overhead_circle) - end - if player.overhead_sprite ~= nil then - rendering.destroy(player.overhead_sprite) - end + if player.overhead_circle ~= nil then rendering.destroy(player.overhead_circle) end + if player.overhead_sprite ~= nil then rendering.destroy(player.overhead_sprite) end if sprite ~= nil then - player.overhead_circle = rendering.draw_circle{color = {r = 0.2, b = 0.2, g = 0.2, a = 0.9}, radius = radius, draw_on_ground = true,--laterdo figure out render layer blend issue - surface = p.surface, target = {x = p.position.x, y = p.position.y - 3 - radius}, filled = true} - rendering.set_visible(player.overhead_circle,true) - player.overhead_sprite = rendering.draw_sprite{sprite = sprite, x_scale = scale, y_scale = scale,--tint = {r = 0.9, b = 0.9, g = 0.9, a = 1.0}, - surface = p.surface, target = {x = p.position.x, y = p.position.y - 3 - radius}, orientation = dirs.north} - rendering.set_visible(player.overhead_sprite,true) + player.overhead_circle = rendering.draw_circle({ + color = { r = 0.2, b = 0.2, g = 0.2, a = 0.9 }, + radius = radius, + draw_on_ground = true, --laterdo figure out render layer blend issue + surface = p.surface, + target = { x = p.position.x, y = p.position.y - 3 - radius }, + filled = true, + }) + rendering.set_visible(player.overhead_circle, true) + player.overhead_sprite = rendering.draw_sprite({ + sprite = sprite, + x_scale = scale, + y_scale = scale, --tint = {r = 0.9, b = 0.9, g = 0.9, a = 1.0}, + surface = p.surface, + target = { x = p.position.x, y = p.position.y - 3 - radius }, + orientation = dirs.north, + }) + rendering.set_visible(player.overhead_sprite, true) end end --Recolors the mod cursor box to match the player's color. Useful in multiplayer when multiple cursors are on screen. function mod.set_cursor_colors_to_player_colors(pindex) - if not check_for_player(pindex) then - return - end + if not check_for_player(pindex) then return end local p = game.get_player(pindex) - if players[pindex].cursor_tile_highlight_box ~= nil and rendering.is_valid(players[pindex].cursor_tile_highlight_box) then - rendering.set_color(players[pindex].cursor_tile_highlight_box,p.color) + if + players[pindex].cursor_tile_highlight_box ~= nil and rendering.is_valid(players[pindex].cursor_tile_highlight_box) + then + rendering.set_color(players[pindex].cursor_tile_highlight_box, p.color) end if players[pindex].building_footprint ~= nil and rendering.is_valid(players[pindex].building_footprint) then - rendering.set_color(players[pindex].building_footprint,p.color) + rendering.set_color(players[pindex].building_footprint, p.color) end end diff --git a/scripts/localising.lua b/scripts/localising.lua index 3120c5db..b23c360e 100644 --- a/scripts/localising.lua +++ b/scripts/localising.lua @@ -2,95 +2,86 @@ local mod = {} --Returns the localised name of an object as a string. Used for ents and items and fluids ---@return string -function mod.get(object,pindex) - -- Everything, everything uses this function without checking the return - -- values. Use really annoying strings to make it very clear there's a - -- bug. +function mod.get(object, pindex) + -- Everything, everything uses this function without checking the return + -- values. Use really annoying strings to make it very clear there's a + -- bug. if pindex == nil then game.print("localising: pindex is nil error") return "NOT LOCALIZED!" end - if object == nil then - return "LOCALIZED OBJECT IS NIL!" - end - if object.valid and string.sub(object.object_name,-9) ~= "Prototype" then - object = object.prototype - end + if object == nil then return "LOCALIZED OBJECT IS NIL!" end + if object.valid and string.sub(object.object_name, -9) ~= "Prototype" then object = object.prototype end local result = players[pindex].localisations result = result and result[object.object_name] result = result and result[object.name] --for debugging if not result then - game.get_player(pindex).print("translation fallback for " .. object.object_name .. " " .. object.name,{volume_modifier=0}) + game + .get_player(pindex) + .print("translation fallback for " .. object.object_name .. " " .. object.name, { volume_modifier = 0 }) end result = result or object.name return result end --Used for recipes -function mod.get_alt(object,pindex) +function mod.get_alt(object, pindex) if pindex == nil then printout("localising: pindex is nil error") return "(nil)" end - if object == nil then - return "(nil)" - end + if object == nil then return "(nil)" end local result = players[pindex].localisations result = result and result[object.object_name] result = result and result[object.name] --for debugging if not result then - game.get_player(pindex).print("translation fallback for " .. object.object_name .. " " .. object.name,{volume_modifier=0}) + game + .get_player(pindex) + .print("translation fallback for " .. object.object_name .. " " .. object.name, { volume_modifier = 0 }) end result = result or object.name return result or "(nil)" end -function mod.get_item_from_name(name,pindex) +function mod.get_item_from_name(name, pindex) local proto = game.item_prototypes[name] - if proto == nil then - return "(nil)" - end - local result = mod.get(proto,pindex) + if proto == nil then return "(nil)" end + local result = mod.get(proto, pindex) return result or "(nil)" end -function mod.get_fluid_from_name(name,pindex) +function mod.get_fluid_from_name(name, pindex) local proto = game.fluid_prototypes[name] - if proto == nil then - return "nil" - end - local result = mod.get(proto,pindex) + if proto == nil then return "nil" end + local result = mod.get(proto, pindex) return result end -function mod.get_recipe_from_name(name,pindex) +function mod.get_recipe_from_name(name, pindex) local proto = game.recipe_prototypes[name] - if proto == nil then - return "nil" - end - local result = mod.get_alt(proto,pindex) + if proto == nil then return "nil" end + local result = mod.get_alt(proto, pindex) return result end -function mod.get_item_group_from_name(name,pindex) +function mod.get_item_group_from_name(name, pindex) local proto = game.item_group_prototypes[name] - if proto == nil then - return "nil" - end - local result = mod.get_alt(proto,pindex) + if proto == nil then return "nil" end + local result = mod.get_alt(proto, pindex) return result end -function mod.request_localisation(thing,pindex) +function mod.request_localisation(thing, pindex) local id = game.players[pindex].request_translation(thing.localised_name) - local lookup=players[pindex].translation_id_lookup - lookup[id]={thing.object_name,thing.name} + local lookup = players[pindex].translation_id_lookup + lookup[id] = { thing.object_name, thing.name } end function mod.request_all_the_translations(pindex) - for _, cat in pairs({"entity", + for _, cat in pairs({ + "entity", "item", "fluid", "tile", @@ -108,9 +99,10 @@ function mod.request_all_the_translations(pindex) "fuel_category", "achievement", "equipment_category", - "shortcut"}) do - for _, proto in pairs(game[cat.."_prototypes"]) do - mod.request_localisation(proto,pindex) + "shortcut", + }) do + for _, proto in pairs(game[cat .. "_prototypes"]) do + mod.request_localisation(proto, pindex) end end end @@ -118,15 +110,13 @@ end --Populates the appropriate localised string arrays for every translation function mod.handler(event) local pindex = event.player_index - local player=players[pindex] + local player = players[pindex] local successful = event.translated - local translated_thing=player.translation_id_lookup[event.id] - if not translated_thing then - return - end + local translated_thing = player.translation_id_lookup[event.id] + if not translated_thing then return end player.translation_id_lookup[event.id] = nil if not successful then - if player.translation_issue_counter == nil then + if player.translation_issue_counter == nil then player.translation_issue_counter = 1 else player.translation_issue_counter = player.translation_issue_counter + 1 @@ -134,11 +124,9 @@ function mod.handler(event) --print("translation request ".. event.id .. " failed, request: [" .. serpent.line(event.localised_string) .. "] for:" .. translated_thing[1] .. ":" .. translated_thing[2] .. ", total issues: " .. players[pindex].translation_issue_counter) return end - if translated_thing=="test_translation" then + if translated_thing == "test_translation" then local last_try = player.localisation_test - if last_try == event.result then - return - end + if last_try == event.result then return end mod.request_all_the_translations(pindex) player.localisation_test = event.result return @@ -148,17 +136,15 @@ function mod.handler(event) print(translated_thing) localised[translated_thing[1]] = localised[translated_thing[1]] or {} local translated_list = localised[translated_thing[1]] - translated_list[ translated_thing[2] ] = event.result + translated_list[translated_thing[2]] = event.result end function mod.check_player(pindex) - local player=players[pindex] - local id=game.players[pindex].request_translation({"error.crash-to-desktop-message"}) - if not id then - return - end + local player = players[pindex] + local id = game.players[pindex].request_translation({ "error.crash-to-desktop-message" }) + if not id then return end player.translation_id_lookup = player.translation_id_lookup or {} player.translation_id_lookup[id] = "test_translation" end -return mod \ No newline at end of file +return mod diff --git a/scripts/menu-search.lua b/scripts/menu-search.lua index 201bf635..c7f71791 100644 --- a/scripts/menu-search.lua +++ b/scripts/menu-search.lua @@ -1,28 +1,26 @@ --Here: Menu search and directly related functions -local util = require('util') -local fa_utils = require('scripts.fa-utils') +local util = require("util") +local fa_utils = require("scripts.fa-utils") local fa_crafting = require("scripts.crafting") -local localising = require('scripts.localising') +local localising = require("scripts.localising") local fa_sectors = require("scripts.building-vehicle-sectors") local mod = {} --Returns the index for the next inventory item to match the search term, for any lua inventory -local function inventory_find_index_of_next_name_match(inv,index,str,pindex) +local function inventory_find_index_of_next_name_match(inv, index, str, pindex) local repeat_i = -1 - if index < 1 then - index = 1 - end + if index < 1 then index = 1 end --Iterate until the end of the inventory for a match - for i=index, #inv, 1 do + for i = index, #inv, 1 do local stack = inv[i] if stack ~= nil and (stack.object_name == "LuaTechnology" or stack.valid_for_read) then - local name = string.lower(localising.get(stack.prototype,pindex)) + local name = string.lower(localising.get(stack.prototype, pindex)) local result = string.find(name, str) if result ~= nil then if name ~= players[pindex].menu_search_last_name then players[pindex].menu_search_last_name = name - game.get_player(pindex).play_sound{path = "Inventory-Move"}--sound for finding the next + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) --sound for finding the next return i else repeat_i = i @@ -32,16 +30,16 @@ local function inventory_find_index_of_next_name_match(inv,index,str,pindex) end end --End of inventory reached, circle back - game.get_player(pindex).play_sound{path = "inventory-wrap-around"}--sound for having cicled around - for i=1, index, 1 do + game.get_player(pindex).play_sound({ path = "inventory-wrap-around" }) --sound for having cicled around + for i = 1, index, 1 do local stack = inv[i] if stack ~= nil and (stack.object_name == "LuaTechnology" or stack.valid_for_read) then - local name = string.lower(localising.get(stack.prototype,pindex)) + local name = string.lower(localising.get(stack.prototype, pindex)) local result = string.find(name, str) if result ~= nil then if name ~= players[pindex].menu_search_last_name then players[pindex].menu_search_last_name = name - game.get_player(pindex).play_sound{path = "Inventory-Move"}--sound for finding the next + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) --sound for finding the next return i else repeat_i = i @@ -51,29 +49,25 @@ local function inventory_find_index_of_next_name_match(inv,index,str,pindex) end end --Check if any repeats found - if repeat_i > 0 then - return repeat_i - end + if repeat_i > 0 then return repeat_i end --No matches found at all return -1 end --Returns the index for the last inventory item to match the search term, for any lua inventory -local function inventory_find_index_of_last_name_match(inv,index,str,pindex) +local function inventory_find_index_of_last_name_match(inv, index, str, pindex) local repeat_i = -1 - if index < 1 then - index = 1 - end + if index < 1 then index = 1 end --Iterate until the start of the inventory for a match - for i=index, 1, -1 do + for i = index, 1, -1 do local stack = inv[i] if stack ~= nil and stack.valid_for_read then - local name = string.lower(localising.get(stack.prototype,pindex)) + local name = string.lower(localising.get(stack.prototype, pindex)) local result = string.find(name, str) if result ~= nil then if name ~= players[pindex].menu_search_last_name then players[pindex].menu_search_last_name = name - game.get_player(pindex).play_sound{path = "Inventory-Move"}--sound for finding the next + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) --sound for finding the next return i else repeat_i = i @@ -83,16 +77,16 @@ local function inventory_find_index_of_last_name_match(inv,index,str,pindex) end end --Start of inventory reached, circle back - game.get_player(pindex).play_sound{path = "inventory-wrap-around"}--sound for having cicled around - for i=#inv, index, -1 do + game.get_player(pindex).play_sound({ path = "inventory-wrap-around" }) --sound for having cicled around + for i = #inv, index, -1 do local stack = inv[i] if stack ~= nil and stack.valid_for_read then - local name = string.lower(localising.get(stack.prototype,pindex)) + local name = string.lower(localising.get(stack.prototype, pindex)) local result = string.find(name, str) if result ~= nil then if name ~= players[pindex].menu_search_last_name then players[pindex].menu_search_last_name = name - game.get_player(pindex).play_sound{path = "Inventory-Move"}--sound for finding the next + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) --sound for finding the next return i else repeat_i = i @@ -102,38 +96,32 @@ local function inventory_find_index_of_last_name_match(inv,index,str,pindex) end end --Check if any repeats found - if repeat_i > 0 then - return repeat_i - end + if repeat_i > 0 then return repeat_i end --No matches found at all return -1 end --Returns the index for the next recipe to match the search term, designed for the way recipes are saved in players[pindex] -local function crafting_find_index_of_next_name_match(str,pindex,last_i, last_j, recipe_set) +local function crafting_find_index_of_next_name_match(str, pindex, last_i, last_j, recipe_set) local recipes = recipe_set local cata_total = #recipes local repeat_i = -1 local repeat_j = -1 - if last_i < 1 then - last_i = 1 - end - if last_j < 1 then - last_j = 1 - end + if last_i < 1 then last_i = 1 end + if last_j < 1 then last_j = 1 end --Iterate until the end of the inventory for a match for i = last_i, cata_total, 1 do for j = last_j, #recipes[i], 1 do local recipe = recipes[i][j] if recipe and recipe.valid then - local name = string.lower(localising.get(recipe,pindex)) + local name = string.lower(localising.get(recipe, pindex)) local result = string.find(name, str) --game.print(i .. "," .. j .. " : " .. name .. " vs. " .. str,{volume_modifier=0}) if result ~= nil then --game.print(" * " .. i .. "," .. j .. " : " .. name .. " vs. " .. str .. " * ",{volume_modifier=0}) if name ~= players[pindex].menu_search_last_name then players[pindex].menu_search_last_name = name - game.get_player(pindex).play_sound{path = "Inventory-Move"}--sound for finding the next + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) --sound for finding the next --game.print(" ** " .. recipes[i][j].name .. " ** ") return i, j else @@ -146,19 +134,19 @@ local function crafting_find_index_of_next_name_match(str,pindex,last_i, last_j, last_j = 1 end --End of inventory reached, circle back - game.get_player(pindex).play_sound{path = "inventory-wrap-around"}--sound for having cicled around + game.get_player(pindex).play_sound({ path = "inventory-wrap-around" }) --sound for having cicled around for i = 1, cata_total, 1 do for j = 1, #recipes[i], 1 do local recipe = recipes[i][j] if recipe and recipe.valid then - local name = string.lower(localising.get(recipe,pindex)) + local name = string.lower(localising.get(recipe, pindex)) local result = string.find(name, str) --game.print(i .. "," .. j .. " : " .. name .. " vs. " .. str,{volume_modifier=0}) if result ~= nil then --game.print(" * " .. i .. "," .. j .. " : " .. name .. " vs. " .. str .. " * ",{volume_modifier=0}) if name ~= players[pindex].menu_search_last_name then players[pindex].menu_search_last_name = name - game.get_player(pindex).play_sound{path = "Inventory-Move"}--sound for finding the next + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) --sound for finding the next --game.print(" ** " .. recipes[i][j].name .. " ** ") return i, j else @@ -170,29 +158,25 @@ local function crafting_find_index_of_next_name_match(str,pindex,last_i, last_j, end end --Check if any repeats found - if repeat_i > 0 then - return repeat_i, repeat_j - end + if repeat_i > 0 then return repeat_i, repeat_j end --No matches found at all return -1, -1 end --Returns the index for the next prototypes array item to match the search term. -local function prototypes_find_index_of_next_name_match(array,index,str,pindex) +local function prototypes_find_index_of_next_name_match(array, index, str, pindex) local repeat_i = -1 - if index < 1 then - index = 1 - end + if index < 1 then index = 1 end --Iterate until the end of the inventory for a match - for i=index, #array, 1 do + for i = index, #array, 1 do local prototype = array[i] if prototype ~= nil and prototype.valid then - local name = string.lower(localising.get(prototype,pindex)) + local name = string.lower(localising.get(prototype, pindex)) local result = string.find(name, str) if result ~= nil then if name ~= players[pindex].menu_search_last_name then players[pindex].menu_search_last_name = name - game.get_player(pindex).play_sound{path = "Inventory-Move"}--sound for finding the next + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) --sound for finding the next --game.print("found: " .. i .. " : " .. name .. " vs. " .. str .. ", last: " .. players[pindex].menu_search_last_name,{volume_modifier=0})-- return i else @@ -202,7 +186,7 @@ local function prototypes_find_index_of_next_name_match(array,index,str,pindex) --game.print(i .. " : " .. name .. " vs. " .. str .. ", last: " .. players[pindex].menu_search_last_name,{volume_modifier=0})-- end end - --End of array reached, assume failed and will move on to next. + --End of array reached, assume failed and will move on to next. return -1 end @@ -210,11 +194,19 @@ end function mod.open_search_box(pindex) --Only allow "inventory" and "building" menus for now if not players[pindex].in_menu then - printout("This menu does not support searching.",pindex) + printout("This menu does not support searching.", pindex) return end - if players[pindex].menu ~= "inventory" and players[pindex].menu ~= "building" and players[pindex].menu ~= "vehicle" and players[pindex].menu ~= "crafting" and players[pindex].menu ~= "technology" and players[pindex].menu ~= "signal_selector" and players[pindex].menu ~= "player_trash" then - printout(players[pindex].menu .. " menu does not support searching.",pindex) + if + players[pindex].menu ~= "inventory" + and players[pindex].menu ~= "building" + and players[pindex].menu ~= "vehicle" + and players[pindex].menu ~= "crafting" + and players[pindex].menu ~= "technology" + and players[pindex].menu ~= "signal_selector" + and players[pindex].menu ~= "player_trash" + then + printout(players[pindex].menu .. " menu does not support searching.", pindex) return end @@ -226,27 +218,35 @@ function mod.open_search_box(pindex) players[pindex].menu_search_frame.destroy() players[pindex].menu_search_frame = nil end - local frame = game.get_player(pindex).gui.screen.add{type = "frame", name = "enter-search-term"} + local frame = game.get_player(pindex).gui.screen.add({ type = "frame", name = "enter-search-term" }) frame.bring_to_front() frame.force_auto_center() frame.focus() players[pindex].menu_search_frame = frame - local input = frame.add{type="textfield", name = "input"} + local input = frame.add({ type = "textfield", name = "input" }) input.focus() --Inform the player - printout(players[pindex].menu .. " enter a search term and press 'ENTER' ",pindex) + printout(players[pindex].menu .. " enter a search term and press 'ENTER' ", pindex) end --Reads out the next inventory/menu item to match the search term. Used in all searchable menus. function mod.fetch_next(pindex, str, start_phrase_in) --Only allow "inventory" and "building" menus for now if not players[pindex].in_menu then - printout("This menu does not support searching.",pindex) + printout("This menu does not support searching.", pindex) return end - if players[pindex].menu ~= "inventory" and players[pindex].menu ~= "building" and players[pindex].menu ~= "vehicle" and players[pindex].menu ~= "crafting" and players[pindex].menu ~= "technology"and players[pindex].menu ~= "signal_selector" and players[pindex].menu ~= "player_trash" then - printout(players[pindex].menu .. " menu does not support searching.",pindex) + if + players[pindex].menu ~= "inventory" + and players[pindex].menu ~= "building" + and players[pindex].menu ~= "vehicle" + and players[pindex].menu ~= "crafting" + and players[pindex].menu ~= "technology" + and players[pindex].menu ~= "signal_selector" + and players[pindex].menu ~= "player_trash" + then + printout(players[pindex].menu .. " menu does not support searching.", pindex) return end if str == nil or str == "" then @@ -255,9 +255,7 @@ function mod.fetch_next(pindex, str, start_phrase_in) end --Start phrase local start_phrase = "" - if start_phrase_in ~= nil then - start_phrase = start_phrase_in - end + if start_phrase_in ~= nil then start_phrase = start_phrase_in end --Get the current search index local search_index = players[pindex].menu_search_index local search_index_2 = players[pindex].menu_search_index_2 @@ -278,13 +276,30 @@ function mod.fetch_next(pindex, str, start_phrase_in) elseif players[pindex].menu == "player_trash" then inv = game.get_player(pindex).get_inventory(defines.inventory.character_trash) new_index = inventory_find_index_of_next_name_match(inv, search_index, str, pindex) - elseif (players[pindex].menu == "building" or players[pindex].menu == "vehicle") and pb.sectors and pb.sectors[pb.sector] and pb.sectors[pb.sector].name == "Output" then + elseif + (players[pindex].menu == "building" or players[pindex].menu == "vehicle") + and pb.sectors + and pb.sectors[pb.sector] + and pb.sectors[pb.sector].name == "Output" + then inv = game.get_player(pindex).opened.get_output_inventory() new_index = inventory_find_index_of_next_name_match(inv, search_index, str, pindex) elseif players[pindex].menu == "crafting" then - new_index, new_index_2 = crafting_find_index_of_next_name_match(str,pindex, search_index, search_index_2, players[pindex].crafting.lua_recipes) + new_index, new_index_2 = crafting_find_index_of_next_name_match( + str, + pindex, + search_index, + search_index_2, + players[pindex].crafting.lua_recipes + ) elseif (players[pindex].menu == "building" or players[pindex].menu == "vehicle") and pb.recipe_selection == true then - new_index, new_index_2 = crafting_find_index_of_next_name_match(str,pindex, search_index, search_index_2, players[pindex].building.recipe_list) + new_index, new_index_2 = crafting_find_index_of_next_name_match( + str, + pindex, + search_index, + search_index_2, + players[pindex].building.recipe_list + ) elseif players[pindex].menu == "technology" then --Search the selected tech catagory local techs = {} @@ -296,12 +311,10 @@ function mod.fetch_next(pindex, str, start_phrase_in) techs = players[pindex].technology.lua_unlocked end new_index = inventory_find_index_of_next_name_match(techs, search_index, str, pindex) - --Search the second tech category + --Search the second tech category if new_index <= 0 then players[pindex].technology.category = players[pindex].technology.category + 1 - if players[pindex].technology.category > 3 then - players[pindex].technology.category = 1 - end + if players[pindex].technology.category > 3 then players[pindex].technology.category = 1 end if players[pindex].technology.category == 1 then techs = players[pindex].technology.lua_researchable elseif players[pindex].technology.category == 2 then @@ -311,12 +324,10 @@ function mod.fetch_next(pindex, str, start_phrase_in) end new_index = inventory_find_index_of_next_name_match(techs, search_index, str, pindex) end - --Search the third tech category + --Search the third tech category if new_index <= 0 then players[pindex].technology.category = players[pindex].technology.category + 1 - if players[pindex].technology.category > 3 then - players[pindex].technology.category = 1 - end + if players[pindex].technology.category > 3 then players[pindex].technology.category = 1 end if players[pindex].technology.category == 1 then techs = players[pindex].technology.lua_researchable elseif players[pindex].technology.category == 2 then @@ -326,12 +337,10 @@ function mod.fetch_next(pindex, str, start_phrase_in) end new_index = inventory_find_index_of_next_name_match(techs, search_index, str, pindex) end - --Circle back to the original category if nothing found + --Circle back to the original category if nothing found if new_index <= 0 then players[pindex].technology.category = players[pindex].technology.category + 1 - if players[pindex].technology.category > 3 then - players[pindex].technology.category = 1 - end + if players[pindex].technology.category > 3 then players[pindex].technology.category = 1 end end elseif players[pindex].menu == "signal_selector" then --Search the currently selected group @@ -349,7 +358,7 @@ function mod.fetch_next(pindex, str, start_phrase_in) group = players[pindex].signal_selector.signals[group_name] new_index = prototypes_find_index_of_next_name_match(group, 0, str, pindex) if tries > 0 and group_index == starting_group_index then - game.get_player(pindex).play_sound{path = "inventory-wrap-around"}--sound for having cicled around + game.get_player(pindex).play_sound({ path = "inventory-wrap-around" }) --sound for having cicled around end tries = tries + 1 end @@ -359,13 +368,13 @@ function mod.fetch_next(pindex, str, start_phrase_in) end --game.print("tries: " .. tries,{volume_modifier=0})-- else - printout("This menu or building sector does not support searching.",pindex) + printout("This menu or building sector does not support searching.", pindex) return end - --Return a menu output according to the index found + --Return a menu output according to the index found if new_index <= 0 then - printout("Could not find " .. str,pindex) - game.get_player(pindex).print("Menu search: Could not find " .. str,{volume_modifier = 0}) + printout("Could not find " .. str, pindex) + game.get_player(pindex).print("Menu search: Could not find " .. str, { volume_modifier = 0 }) players[pindex].menu_search_last_name = "(none)" return elseif players[pindex].menu == "inventory" then @@ -376,22 +385,33 @@ function mod.fetch_next(pindex, str, start_phrase_in) players[pindex].menu_search_index = new_index players[pindex].inventory.index = new_index read_inventory_slot(pindex, start_phrase, inv) - elseif (players[pindex].menu == "building" or players[pindex].menu == "vehicle") and pb.sectors and pb.sectors[pb.sector] and pb.sectors[pb.sector].name == "Output" then + elseif + (players[pindex].menu == "building" or players[pindex].menu == "vehicle") + and pb.sectors + and pb.sectors[pb.sector] + and pb.sectors[pb.sector].name == "Output" + then players[pindex].menu_search_index = new_index players[pindex].building.index = new_index - fa_sectors.read_sector_slot(pindex,false) - elseif (players[pindex].menu == "building" or players[pindex].menu == "vehicle") and players[pindex].building.sector_name == "player_inventory" then + fa_sectors.read_sector_slot(pindex, false) + elseif + (players[pindex].menu == "building" or players[pindex].menu == "vehicle") + and players[pindex].building.sector_name == "player_inventory" + then players[pindex].menu_search_index = new_index players[pindex].building.index = new_index players[pindex].inventory.index = new_index - read_inventory_slot(pindex,false) + read_inventory_slot(pindex, false) elseif players[pindex].menu == "crafting" then players[pindex].menu_search_index = new_index players[pindex].menu_search_index_2 = new_index_2 players[pindex].crafting.category = new_index players[pindex].crafting.index = new_index_2 fa_crafting.read_crafting_slot(pindex, start_phrase) - elseif (players[pindex].menu == "building" or players[pindex].menu == "vehicle") and players[pindex].building.recipe_selection == true then + elseif + (players[pindex].menu == "building" or players[pindex].menu == "vehicle") + and players[pindex].building.recipe_selection == true + then players[pindex].menu_search_index = new_index players[pindex].menu_search_index_2 = new_index_2 players[pindex].building.category = new_index @@ -418,20 +438,20 @@ function mod.fetch_next(pindex, str, start_phrase_in) players[pindex].signal_selector.signal_index = new_index read_selected_signal_slot(pindex, start_phrase) else - printout("Search error",pindex) + printout("Search error", pindex) return end end --Reads out the last inventory/menu item to match the search term. Used only in some menus. -function mod.fetch_last(pindex,str) +function mod.fetch_last(pindex, str) --Only allow "inventory" and "building" menus for now if not players[pindex].in_menu then - printout("This menu does not support searching backwards.",pindex) + printout("This menu does not support searching backwards.", pindex) return end if players[pindex].menu ~= "inventory" and players[pindex].menu ~= "building" then - printout(players[pindex].menu .. " menu does not support searching backwards.",pindex) + printout(players[pindex].menu .. " menu does not support searching backwards.", pindex) return end if str == nil or str == "" then @@ -451,27 +471,37 @@ function mod.fetch_last(pindex,str) if players[pindex].menu == "inventory" then inv = game.get_player(pindex).get_main_inventory() new_index = inventory_find_index_of_last_name_match(inv, search_index, str, pindex) - elseif (players[pindex].menu == "building" or players[pindex].menu == "vehicle") and pb.sectors and pb.sectors[pb.sector] and pb.sectors[pb.sector].name == "Output" then + elseif + (players[pindex].menu == "building" or players[pindex].menu == "vehicle") + and pb.sectors + and pb.sectors[pb.sector] + and pb.sectors[pb.sector].name == "Output" + then inv = game.get_player(pindex).opened.get_output_inventory() new_index = inventory_find_index_of_last_name_match(inv, search_index, str, pindex) else - printout("This menu or building sector does not support searching backwards.",pindex) + printout("This menu or building sector does not support searching backwards.", pindex) return end - --Return a menu output according to the index found + --Return a menu output according to the index found if new_index <= 0 then - printout("Could not find " .. str,pindex) + printout("Could not find " .. str, pindex) return elseif players[pindex].menu == "inventory" then players[pindex].menu_search_index = new_index players[pindex].inventory.index = new_index read_inventory_slot(pindex) - elseif (players[pindex].menu == "building" or players[pindex].menu == "vehicle") and pb.sectors and pb.sectors[pb.sector] and pb.sectors[pb.sector].name == "Output" then + elseif + (players[pindex].menu == "building" or players[pindex].menu == "vehicle") + and pb.sectors + and pb.sectors[pb.sector] + and pb.sectors[pb.sector].name == "Output" + then players[pindex].menu_search_index = new_index players[pindex].building.index = new_index - fa_sectors.read_sector_slot(pindex,false) + fa_sectors.read_sector_slot(pindex, false) else - printout("Search error",pindex) + printout("Search error", pindex) return end end diff --git a/scripts/methods.lua b/scripts/methods.lua index 29bea339..81709eae 100644 --- a/scripts/methods.lua +++ b/scripts/methods.lua @@ -65,7 +65,7 @@ local mod = {} -- on ephemeron tables, in section 2.5.2. local bound_cache = {} setmetatable(bound_cache, { - __mode = 'k', + __mode = "k", }) local seenh_unique_names = {} @@ -75,24 +75,18 @@ local seenh_unique_names = {} -- This is useful because we have self-tests near the end of this file, and -- those can be run from a shell. local function maybe_register(name, metatable) - if script ~= nil and script.register_metatable ~= nil then - script.register_metatable(name, metatable) - end + if script ~= nil and script.register_metatable ~= nil then script.register_metatable(name, metatable) end end function mod.link(unique_name, methods_table) - if seenh_unique_names[unique_name] then - error("Attempt to double-register " .. unique_name) - end + if seenh_unique_names[unique_name] then error("Attempt to double-register " .. unique_name) end seenh_unique_names[unique_name] = true local meta_name = unique_name .. "-methods" local meta = { __index = function(table, key) - if not methods_table[key] then - return nil - end + if not methods_table[key] then return nil end local cached = bound_cache[table] if not cached then @@ -134,7 +128,7 @@ if script == nil then self.count = self.count - by end - local linker = mod.link('methods_self_test', counter_methods) + local linker = mod.link("methods_self_test", counter_methods) local instance = { count = 0 } linker(instance) diff --git a/scripts/mining-tools.lua b/scripts/mining-tools.lua index a5afaa43..6a6f1ecc 100644 --- a/scripts/mining-tools.lua +++ b/scripts/mining-tools.lua @@ -3,34 +3,34 @@ local mod = {} function mod.play_mining_sound(pindex) - local player= game.players[pindex] + local player = game.players[pindex] --game.print("1",{volume_modifier=0})-- if player and player.mining_state.mining and player.selected and player.selected.valid then --game.print("2",{volume_modifier=0})-- if player.selected.prototype.is_building then - player.play_sound{path = "player-mine"} + player.play_sound({ path = "player-mine" }) --game.print("3A",{volume_modifier=0})-- else - player.play_sound{path = "player-mine"}--Mine other things, eg. character corpses, laterdo new sound + player.play_sound({ path = "player-mine" }) --Mine other things, eg. character corpses, laterdo new sound --game.print("3B",{volume_modifier=0})-- end end end --Mines an entity with the right sound -function mod.try_to_mine_with_soun(ent,pindex) +function mod.try_to_mine_with_soun(ent, pindex) if ent ~= nil and ent.valid and ((ent.destructible and ent.type ~= "resource") or ent.name == "item-on-ground") then - local ent_name = ent.name - if game.get_player(pindex).mine_entity(ent,false) and game.is_valid_sound_path("entity-mined/" .. ent_name) then - game.get_player(pindex).play_sound{path = "entity-mined/" .. ent_name} - return true - else - return false - end + local ent_name = ent.name + if game.get_player(pindex).mine_entity(ent, false) and game.is_valid_sound_path("entity-mined/" .. ent_name) then + game.get_player(pindex).play_sound({ path = "entity-mined/" .. ent_name }) + return true + else + return false + end end end ---Mines all trees and rocks and ground items in a selected circular area. Useful when placing structures. Forces mining. laterdo add deleting stumps maybe but they do fade away eventually +--Mines all trees and rocks and ground items in a selected circular area. Useful when placing structures. Forces mining. laterdo add deleting stumps maybe but they do fade away eventually function mod.clear_obstacles_in_circle(position, radius, pindex) local surf = game.get_player(pindex).surface local comment = "" @@ -41,47 +41,95 @@ function mod.clear_obstacles_in_circle(position, radius, pindex) players[pindex].allow_reading_flying_text = false --Find and mine trees - local trees = surf.find_entities_filtered{position = position, radius = radius, type = "tree"} - for i,tree_ent in ipairs(trees) do - rendering.draw_circle{color = {1, 0, 0},radius = 1,width = 1,target = tree_ent.position,surface = tree_ent.surface,time_to_live = 60} - game.get_player(pindex).mine_entity(tree_ent,true) - trees_cleared = trees_cleared + 1 + local trees = surf.find_entities_filtered({ position = position, radius = radius, type = "tree" }) + for i, tree_ent in ipairs(trees) do + rendering.draw_circle({ + color = { 1, 0, 0 }, + radius = 1, + width = 1, + target = tree_ent.position, + surface = tree_ent.surface, + time_to_live = 60, + }) + game.get_player(pindex).mine_entity(tree_ent, true) + trees_cleared = trees_cleared + 1 end --Find and mine rocks. Note that they are resource entities with specific names - local resources = surf.find_entities_filtered{position = position, radius = radius, name = {"rock-big","rock-huge","sand-rock-big"}} - for i,resource_ent in ipairs(resources) do + local resources = surf.find_entities_filtered({ + position = position, + radius = radius, + name = { "rock-big", "rock-huge", "sand-rock-big" }, + }) + for i, resource_ent in ipairs(resources) do if resource_ent ~= nil and resource_ent.valid then - rendering.draw_circle{color = {1, 0, 0},radius = 2,width = 2,target = resource_ent.position,surface = resource_ent.surface,time_to_live = 60} - game.get_player(pindex).mine_entity(resource_ent,true) + rendering.draw_circle({ + color = { 1, 0, 0 }, + radius = 2, + width = 2, + target = resource_ent.position, + surface = resource_ent.surface, + time_to_live = 60, + }) + game.get_player(pindex).mine_entity(resource_ent, true) rocks_cleared = rocks_cleared + 1 end end --Find and mine corpse entities such as building remnants - local remnant_ents = surf.find_entities_filtered{position = position, radius = radius, name = ENT_NAMES_CLEARED_AS_OBSTACLES} - for i,remnant_ent in ipairs(remnant_ents) do + local remnant_ents = + surf.find_entities_filtered({ position = position, radius = radius, name = ENT_NAMES_CLEARED_AS_OBSTACLES }) + for i, remnant_ent in ipairs(remnant_ents) do if remnant_ent ~= nil and remnant_ent.valid then - rendering.draw_circle{color = {1, 0, 0},radius = 2,width = 2,target = remnant_ent.position,surface = remnant_ent.surface,time_to_live = 60} - remnant_ent.destroy{} + rendering.draw_circle({ + color = { 1, 0, 0 }, + radius = 2, + width = 2, + target = remnant_ent.position, + surface = remnant_ent.surface, + time_to_live = 60, + }) + remnant_ent.destroy({}) remnants_cleared = remnants_cleared + 1 end end --game.get_player(pindex).print("remnants cleared: " .. remnants_cleared)--debug --Find and mine items on the ground - local ground_items = surf.find_entities_filtered{position = position, radius = 5, name = "item-on-ground"} - for i,ground_item in ipairs(ground_items) do - rendering.draw_circle{color = {1, 0, 0},radius = 0.25,width = 2,target = ground_item.position,surface = surf,time_to_live = 60} - game.get_player(pindex).mine_entity(ground_item,true) + local ground_items = surf.find_entities_filtered({ position = position, radius = 5, name = "item-on-ground" }) + for i, ground_item in ipairs(ground_items) do + rendering.draw_circle({ + color = { 1, 0, 0 }, + radius = 0.25, + width = 2, + target = ground_item.position, + surface = surf, + time_to_live = 60, + }) + game.get_player(pindex).mine_entity(ground_item, true) ground_items_cleared = ground_items_cleared + 1 end --Report clear and pickup counts if trees_cleared + rocks_cleared + ground_items_cleared + remnants_cleared > 0 then - comment = "cleared " .. trees_cleared .. " trees and " .. rocks_cleared .. " rocks and " .. remnants_cleared .. " remnants and " .. ground_items_cleared .. " ground items " + comment = "cleared " + .. trees_cleared + .. " trees and " + .. rocks_cleared + .. " rocks and " + .. remnants_cleared + .. " remnants and " + .. ground_items_cleared + .. " ground items " end - rendering.draw_circle{color = {0, 1, 0},radius = radius,width = radius,target = position,surface = surf,time_to_live = 60} + rendering.draw_circle({ + color = { 0, 1, 0 }, + radius = radius, + width = radius, + target = position, + surface = surf, + time_to_live = 60, + }) return (trees_cleared + rocks_cleared + remnants_cleared + ground_items_cleared), comment end @@ -96,49 +144,87 @@ function mod.clear_obstacles_in_rectangle(left_top, right_bottom, pindex) players[pindex].allow_reading_flying_text = false --Check for valid positions - if left_top == nil or right_bottom == nil then - return - end + if left_top == nil or right_bottom == nil then return end --Find and mine trees - local trees = surf.find_entities_filtered{area = {left_top, right_bottom}, type = "tree"} - for i,tree_ent in ipairs(trees) do - rendering.draw_circle{color = {1, 0, 0},radius = 1,width = 1,target = tree_ent.position,surface = tree_ent.surface,time_to_live = 60} - game.get_player(pindex).mine_entity(tree_ent,true) - trees_cleared = trees_cleared + 1 + local trees = surf.find_entities_filtered({ area = { left_top, right_bottom }, type = "tree" }) + for i, tree_ent in ipairs(trees) do + rendering.draw_circle({ + color = { 1, 0, 0 }, + radius = 1, + width = 1, + target = tree_ent.position, + surface = tree_ent.surface, + time_to_live = 60, + }) + game.get_player(pindex).mine_entity(tree_ent, true) + trees_cleared = trees_cleared + 1 end --Find and mine rocks. Note that they are resource entities with specific names - local resources = surf.find_entities_filtered{area = {left_top, right_bottom}, name = {"rock-big","rock-huge","sand-rock-big"}} - for i,resource_ent in ipairs(resources) do + local resources = surf.find_entities_filtered({ + area = { left_top, right_bottom }, + name = { "rock-big", "rock-huge", "sand-rock-big" }, + }) + for i, resource_ent in ipairs(resources) do if resource_ent ~= nil and resource_ent.valid then - rendering.draw_circle{color = {1, 0, 0},radius = 2,width = 2,target = resource_ent.position,surface = resource_ent.surface,time_to_live = 60} - game.get_player(pindex).mine_entity(resource_ent,true) + rendering.draw_circle({ + color = { 1, 0, 0 }, + radius = 2, + width = 2, + target = resource_ent.position, + surface = resource_ent.surface, + time_to_live = 60, + }) + game.get_player(pindex).mine_entity(resource_ent, true) rocks_cleared = rocks_cleared + 1 end end --Find and mine corpse entities such as building remnants - local remnant_ents = surf.find_entities_filtered{area = {left_top, right_bottom}, name = ENT_NAMES_CLEARED_AS_OBSTACLES} - for i,remnant_ent in ipairs(remnant_ents) do + local remnant_ents = + surf.find_entities_filtered({ area = { left_top, right_bottom }, name = ENT_NAMES_CLEARED_AS_OBSTACLES }) + for i, remnant_ent in ipairs(remnant_ents) do if remnant_ent ~= nil and remnant_ent.valid then - rendering.draw_circle{color = {1, 0, 0},radius = 2,width = 2,target = remnant_ent.position,surface = remnant_ent.surface,time_to_live = 60} - remnant_ent.destroy{} + rendering.draw_circle({ + color = { 1, 0, 0 }, + radius = 2, + width = 2, + target = remnant_ent.position, + surface = remnant_ent.surface, + time_to_live = 60, + }) + remnant_ent.destroy({}) remnants_cleared = remnants_cleared + 1 end end --game.get_player(pindex).print("remnants cleared: " .. remnants_cleared)--debug --Find and mine items on the ground - local ground_items = surf.find_entities_filtered{area = {left_top, right_bottom}, name = "item-on-ground"} - for i,ground_item in ipairs(ground_items) do - rendering.draw_circle{color = {1, 0, 0},radius = 0.25,width = 2,target = ground_item.position,surface = surf,time_to_live = 60} - game.get_player(pindex).mine_entity(ground_item,true) + local ground_items = surf.find_entities_filtered({ area = { left_top, right_bottom }, name = "item-on-ground" }) + for i, ground_item in ipairs(ground_items) do + rendering.draw_circle({ + color = { 1, 0, 0 }, + radius = 0.25, + width = 2, + target = ground_item.position, + surface = surf, + time_to_live = 60, + }) + game.get_player(pindex).mine_entity(ground_item, true) ground_items_cleared = ground_items_cleared + 1 end if trees_cleared + rocks_cleared + ground_items_cleared + remnants_cleared > 0 then - comment = "cleared " .. trees_cleared .. " trees and " .. rocks_cleared .. " rocks and " .. remnants_cleared .. " remnants and " .. ground_items_cleared .. " ground items " + comment = "cleared " + .. trees_cleared + .. " trees and " + .. rocks_cleared + .. " rocks and " + .. remnants_cleared + .. " remnants and " + .. ground_items_cleared + .. " ground items " end if not players[pindex].hide_cursor then --rendering.draw_rectangle{color = {0, 1, 0, 0.5}, left_top = left_top, right_bottom = right_bottom, width = 4, surface = surf, time_to_live = 60, draw_on_ground = true} @@ -146,4 +232,4 @@ function mod.clear_obstacles_in_rectangle(left_top, right_bottom, pindex) return (trees_cleared + rocks_cleared + remnants_cleared + ground_items_cleared), comment end -return mod \ No newline at end of file +return mod diff --git a/scripts/mouse.lua b/scripts/mouse.lua index 8940c039..c0a26866 100644 --- a/scripts/mouse.lua +++ b/scripts/mouse.lua @@ -5,8 +5,8 @@ local fa_utils = require("scripts.fa-utils") local dirs = defines.direction local mod = {} ---Moves the mouse pointer to the correct pixel on the screen for an input map position. If the position is off screen, then the pointer is centered on the player character instead. Does not run in vanilla mode or if the mouse is released from synchronizing. -function mod.move_mouse_pointer(position,pindex) +--Moves the mouse pointer to the correct pixel on the screen for an input map position. If the position is off screen, then the pointer is centered on the player character instead. Does not run in vanilla mode or if the mouse is released from synchronizing. +function mod.move_mouse_pointer(position, pindex) local pos = position if players[pindex].vanilla_mode or game.get_player(pindex).game_view_settings.update_entity_selection == true then return @@ -16,26 +16,34 @@ function mod.move_mouse_pointer(position,pindex) --return end local player = players[pindex] - local pixels = fa_utils.mult_position( fa_utils.sub_position(pos, player.position), 32*player.zoom) + local pixels = fa_utils.mult_position(fa_utils.sub_position(pos, player.position), 32 * player.zoom) local screen = game.players[pindex].display_resolution - local screen_c = {x = screen.width, y = screen.height} - pixels = fa_utils.add_position(pixels,fa_utils.mult_position(screen_c,0.5)) + local screen_c = { x = screen.width, y = screen.height } + pixels = fa_utils.add_position(pixels, fa_utils.mult_position(screen_c, 0.5)) mod.move_pointer_to_pixels(pixels.x, pixels.y, pindex) --game.get_player(pindex).print("moved to " .. math.floor(pixels.x) .. " , " .. math.floor(pixels.y), {volume_modifier=0})-- end --Moves the mouse pointer to specified pixels on the screen. -function mod.move_pointer_to_pixels(x,y, pindex) - if x >= 0 and y >=0 and x < game.players[pindex].display_resolution.width and y < game.players[pindex].display_resolution.height then - print ("setCursor " .. pindex .. " " .. math.ceil(x) .. "," .. math.ceil(y)) +function mod.move_pointer_to_pixels(x, y, pindex) + if + x >= 0 + and y >= 0 + and x < game.players[pindex].display_resolution.width + and y < game.players[pindex].display_resolution.height + then + print("setCursor " .. pindex .. " " .. math.ceil(x) .. "," .. math.ceil(y)) end end --Checks if the map position of the mod cursor falls on screen when the camera is locked on the player character. function mod.cursor_position_is_on_screen_with_player_centered(pindex) - local range_y = math.floor(16/players[pindex].zoom)--found experimentally by counting tile ranges at different zoom levels - local range_x = range_y * game.get_player(pindex).display_scale * 1.5--found experimentally by checking scales - return (math.abs(players[pindex].cursor_pos.y - players[pindex].position.y) <= range_y and math.abs(players[pindex].cursor_pos.x - players[pindex].position.x) <= range_x) + local range_y = math.floor(16 / players[pindex].zoom) --found experimentally by counting tile ranges at different zoom levels + local range_x = range_y * game.get_player(pindex).display_scale * 1.5 --found experimentally by checking scales + return ( + math.abs(players[pindex].cursor_pos.y - players[pindex].position.y) <= range_y + and math.abs(players[pindex].cursor_pos.x - players[pindex].position.x) <= range_x + ) end return mod diff --git a/scripts/rail-builder.lua b/scripts/rail-builder.lua index d2cdf2cb..10df6645 100644 --- a/scripts/rail-builder.lua +++ b/scripts/rail-builder.lua @@ -2,7 +2,7 @@ --Does not include event handlers ---@diagnostic disable: assign-type-mismatch -local util = require('util') +local util = require("util") local fa_utils = require("scripts.fa-utils") local fa_mining_tools = require("scripts.mining-tools") local fa_rails = require("scripts.rails") @@ -23,17 +23,17 @@ function mod.append_rail(pos, pindex) local is_end_rail = nil local end_rail_dir = nil local comment = "" - + --0 Check if there is at least 1 rail in hand, else return if not (stack.valid and stack.valid_for_read and stack.name == "rail" and stack.count > 0) then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout("You need at least 1 rail in hand.", pindex) return end - + --1 Check the cursor entity. If it is an end rail, use this instead of scanning to extend the rail you want. local ent = players[pindex].tile.ents[1] - is_end_rail, end_rail_dir, comment = fa_rails.check_end_rail(ent,pindex) + is_end_rail, end_rail_dir, comment = fa_rails.check_end_rail(ent, pindex) if is_end_rail then end_found = ent end_rail_1, end_dir_1 = ent.get_rail_segment_end(defines.rail_direction.front) @@ -45,242 +45,260 @@ function mod.append_rail(pos, pindex) end else --2 Scan the area around within a X tile radius of pos - local ents = surf.find_entities_filtered{position = pos, radius = 3, name = "straight-rail"} + local ents = surf.find_entities_filtered({ position = pos, radius = 3, name = "straight-rail" }) if #ents == 0 then - ents = surf.find_entities_filtered{position = pos, radius = 3, name = "curved-rail"} + ents = surf.find_entities_filtered({ position = pos, radius = 3, name = "curved-rail" }) if #ents == 0 then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) if players[pindex].build_lock == false then - printout("No rails found nearby.",pindex) + printout("No rails found nearby.", pindex) return end end end --3 For the first rail found, check if it is at the end of its segment and if the rail is not within X tiles of pos, try the other end - for i,rail in ipairs(ents) do + for i, rail in ipairs(ents) do end_rail_1, end_dir_1 = rail.get_rail_segment_end(defines.rail_direction.front) end_rail_2, end_dir_2 = rail.get_rail_segment_end(defines.rail_direction.back) - if util.distance(pos, end_rail_1.position) < 3 then--is within range + if util.distance(pos, end_rail_1.position) < 3 then --is within range end_found = end_rail_1 end_dir = end_dir_1 - elseif util.distance(pos, end_rail_2.position) < 3 then--is within range + elseif util.distance(pos, end_rail_2.position) < 3 then --is within range end_found = end_rail_2 end_dir = end_dir_2 end - end + end if end_found == nil then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} - if players[pindex].build_lock == false then - printout("No end rails found nearby", pindex) - end + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) + if players[pindex].build_lock == false then printout("No end rails found nearby", pindex) end return end - + --4 Check if the found segment end is an end rail - is_end_rail, end_rail_dir, comment = fa_rails.check_end_rail(end_found,pindex) + is_end_rail, end_rail_dir, comment = fa_rails.check_end_rail(end_found, pindex) if not is_end_rail then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) --printout(comment, pindex) printout("No end rails found nearby", pindex) return end end - + --5 Confirmed as an end rail. Get its position and find the correct position and direction for the appended rail. end_rail_pos = end_found.position end_rail_dir = end_found.direction append_rail_dir = -1 append_rail_pos = nil rail_api_dir = end_found.direction - + --printout(" Rail end found at " .. end_found.position.x .. " , " .. end_found.position.y .. " , facing " .. end_found.direction, pindex)--Checks if end_found.name == "straight-rail" then - if end_rail_dir == dirs.north or end_rail_dir == dirs.south then + if end_rail_dir == dirs.north or end_rail_dir == dirs.south then append_rail_dir = dirs.north if end_dir == defines.rail_direction.front then - append_rail_pos = {end_rail_pos.x+0, end_rail_pos.y-2} + append_rail_pos = { end_rail_pos.x + 0, end_rail_pos.y - 2 } else - append_rail_pos = {end_rail_pos.x+0, end_rail_pos.y+2} + append_rail_pos = { end_rail_pos.x + 0, end_rail_pos.y + 2 } end - elseif end_rail_dir == dirs.east or end_rail_dir == dirs.west then append_rail_dir = dirs.east if end_dir == defines.rail_direction.front then - append_rail_pos = {end_rail_pos.x+2, end_rail_pos.y+0} + append_rail_pos = { end_rail_pos.x + 2, end_rail_pos.y + 0 } else - append_rail_pos = {end_rail_pos.x-2, end_rail_pos.y-0} + append_rail_pos = { end_rail_pos.x - 2, end_rail_pos.y - 0 } end - elseif end_rail_dir == dirs.northeast then append_rail_dir = dirs.southwest if end_dir == defines.rail_direction.front then - append_rail_pos = {end_rail_pos.x+0, end_rail_pos.y-2} + append_rail_pos = { end_rail_pos.x + 0, end_rail_pos.y - 2 } else - append_rail_pos = {end_rail_pos.x+2, end_rail_pos.y+0} + append_rail_pos = { end_rail_pos.x + 2, end_rail_pos.y + 0 } end elseif end_rail_dir == dirs.southwest then append_rail_dir = dirs.northeast if end_dir == defines.rail_direction.front then - append_rail_pos = {end_rail_pos.x+0, end_rail_pos.y+2} + append_rail_pos = { end_rail_pos.x + 0, end_rail_pos.y + 2 } else - append_rail_pos = {end_rail_pos.x-2, end_rail_pos.y+0} + append_rail_pos = { end_rail_pos.x - 2, end_rail_pos.y + 0 } end - elseif end_rail_dir == dirs.southeast then append_rail_dir = dirs.northwest if end_dir == defines.rail_direction.front then - append_rail_pos = {end_rail_pos.x+2, end_rail_pos.y+0} + append_rail_pos = { end_rail_pos.x + 2, end_rail_pos.y + 0 } else - append_rail_pos = {end_rail_pos.x+0, end_rail_pos.y+2} + append_rail_pos = { end_rail_pos.x + 0, end_rail_pos.y + 2 } end elseif end_rail_dir == dirs.northwest then append_rail_dir = dirs.southeast if end_dir == defines.rail_direction.front then - append_rail_pos = {end_rail_pos.x-2, end_rail_pos.y+0} + append_rail_pos = { end_rail_pos.x - 2, end_rail_pos.y + 0 } else - append_rail_pos = {end_rail_pos.x+0, end_rail_pos.y-2} + append_rail_pos = { end_rail_pos.x + 0, end_rail_pos.y - 2 } end end - elseif end_found.name == "curved-rail" then --Make sure to use the reported end direction for curved rails - is_end_rail, end_rail_dir, comment = fa_rails.check_end_rail(ent,pindex) + is_end_rail, end_rail_dir, comment = fa_rails.check_end_rail(ent, pindex) if end_rail_dir == dirs.north then if rail_api_dir == dirs.south then - append_rail_pos = {end_rail_pos.x-2, end_rail_pos.y-6} + append_rail_pos = { end_rail_pos.x - 2, end_rail_pos.y - 6 } append_rail_dir = dirs.north elseif rail_api_dir == dirs.southwest then - append_rail_pos = {end_rail_pos.x-0, end_rail_pos.y-6} + append_rail_pos = { end_rail_pos.x - 0, end_rail_pos.y - 6 } append_rail_dir = dirs.north end elseif end_rail_dir == dirs.northeast then if rail_api_dir == dirs.northeast then - append_rail_pos = {end_rail_pos.x+2, end_rail_pos.y-4} + append_rail_pos = { end_rail_pos.x + 2, end_rail_pos.y - 4 } append_rail_dir = dirs.northwest elseif rail_api_dir == dirs.east then - append_rail_pos = {end_rail_pos.x+2, end_rail_pos.y-4} + append_rail_pos = { end_rail_pos.x + 2, end_rail_pos.y - 4 } append_rail_dir = dirs.southeast end elseif end_rail_dir == dirs.east then if rail_api_dir == dirs.west then - append_rail_pos = {end_rail_pos.x+4, end_rail_pos.y-2} + append_rail_pos = { end_rail_pos.x + 4, end_rail_pos.y - 2 } append_rail_dir = dirs.east elseif rail_api_dir == dirs.northwest then - append_rail_pos = {end_rail_pos.x+4, end_rail_pos.y-0} + append_rail_pos = { end_rail_pos.x + 4, end_rail_pos.y - 0 } append_rail_dir = dirs.east - end + end elseif end_rail_dir == dirs.southeast then if rail_api_dir == dirs.southeast then - append_rail_pos = {end_rail_pos.x+2, end_rail_pos.y+2} + append_rail_pos = { end_rail_pos.x + 2, end_rail_pos.y + 2 } append_rail_dir = dirs.northeast elseif rail_api_dir == dirs.south then - append_rail_pos = {end_rail_pos.x+2, end_rail_pos.y+2} + append_rail_pos = { end_rail_pos.x + 2, end_rail_pos.y + 2 } append_rail_dir = dirs.southwest end elseif end_rail_dir == dirs.south then if rail_api_dir == dirs.north then - append_rail_pos = {end_rail_pos.x-0, end_rail_pos.y+4} + append_rail_pos = { end_rail_pos.x - 0, end_rail_pos.y + 4 } append_rail_dir = dirs.north elseif rail_api_dir == dirs.northeast then - append_rail_pos = {end_rail_pos.x-2, end_rail_pos.y+4} + append_rail_pos = { end_rail_pos.x - 2, end_rail_pos.y + 4 } append_rail_dir = dirs.north end elseif end_rail_dir == dirs.southwest then if rail_api_dir == dirs.southwest then - append_rail_pos = {end_rail_pos.x-4, end_rail_pos.y+2} + append_rail_pos = { end_rail_pos.x - 4, end_rail_pos.y + 2 } append_rail_dir = dirs.southeast elseif rail_api_dir == dirs.west then - append_rail_pos = {end_rail_pos.x-4, end_rail_pos.y+2} + append_rail_pos = { end_rail_pos.x - 4, end_rail_pos.y + 2 } append_rail_dir = dirs.northwest end elseif end_rail_dir == dirs.west then if rail_api_dir == dirs.east then - append_rail_pos = {end_rail_pos.x-6, end_rail_pos.y-0} + append_rail_pos = { end_rail_pos.x - 6, end_rail_pos.y - 0 } append_rail_dir = dirs.east elseif rail_api_dir == dirs.southeast then - append_rail_pos = {end_rail_pos.x-6, end_rail_pos.y-2} + append_rail_pos = { end_rail_pos.x - 6, end_rail_pos.y - 2 } append_rail_dir = dirs.east - end + end elseif end_rail_dir == dirs.northwest then if rail_api_dir == dirs.north then - append_rail_pos = {end_rail_pos.x-4, end_rail_pos.y-4} + append_rail_pos = { end_rail_pos.x - 4, end_rail_pos.y - 4 } append_rail_dir = dirs.northeast elseif rail_api_dir == dirs.northwest then - append_rail_pos = {end_rail_pos.x-4, end_rail_pos.y-4} + append_rail_pos = { end_rail_pos.x - 4, end_rail_pos.y - 4 } append_rail_dir = dirs.southwest - end + end end end - + --6. Clear trees and rocks nearby and check if the selected 2x2 space is free for building, else return if append_rail_pos == nil then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} - printout(end_rail_dir .. " and " .. rail_api_dir .. ", rail appending direction error.",pindex) + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) + printout(end_rail_dir .. " and " .. rail_api_dir .. ", rail appending direction error.", pindex) return end - temp1, build_comment = fa_mining_tools.clear_obstacles_in_circle(append_rail_pos,4, pindex) - if not surf.can_place_entity{name = "straight-rail", position = append_rail_pos, direction = append_rail_dir} then + temp1, build_comment = fa_mining_tools.clear_obstacles_in_circle(append_rail_pos, 4, pindex) + if + not surf.can_place_entity({ name = "straight-rail", position = append_rail_pos, direction = append_rail_dir }) + then --Check if you can build from cursor or if you have other rails here already -- local other_rails_present = false -- local ents = surf.find_entities_filtered{position = append_rail_pos} -- for i,ent in ipairs(ents) do - -- if ent.name == "straight-rail" or ent.name == "curved-rail" then - -- other_rails_present = true - -- end + -- if ent.name == "straight-rail" or ent.name == "curved-rail" then + -- other_rails_present = true + -- end -- end -- if game.get_player(pindex).can_build_from_cursor({name = "straight-rail", position = append_rail_pos, direction = append_rail_dir}) then--**maybe thisll work - -- game.get_player(pindex).print("Can build from hand",{volume_modifier = 0}) + -- game.get_player(pindex).print("Can build from hand",{volume_modifier = 0}) -- end -- if other_rails_present == true then - -- game.get_player(pindex).print("Other rails present",{volume_modifier = 0}) + -- game.get_player(pindex).print("Other rails present",{volume_modifier = 0}) -- end --Patch a bug with South and West dirs in certain conditions such as after a train stop, where it is detected as North/East if end_rail_dir == dirs.east then - append_rail_pos = {end_rail_pos.x-2, end_rail_pos.y-0} + append_rail_pos = { end_rail_pos.x - 2, end_rail_pos.y - 0 } elseif end_rail_dir == dirs.north then - append_rail_pos = {end_rail_pos.x-0, end_rail_pos.y+2} - end - if not surf.can_place_entity{name = "straight-rail", position = append_rail_pos, direction = append_rail_dir} then - printout("Cannot place here to extend the rail.",pindex) - game.get_player(pindex).play_sound{path = "utility/cannot_build"} - rendering.draw_circle{color = {1, 0, 0},radius = 0.5,width = 5,target = append_rail_pos,surface = surf,time_to_live = 120} + append_rail_pos = { end_rail_pos.x - 0, end_rail_pos.y + 2 } + end + if + not surf.can_place_entity({ name = "straight-rail", position = append_rail_pos, direction = append_rail_dir }) + then + printout("Cannot place here to extend the rail.", pindex) + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) + rendering.draw_circle({ + color = { 1, 0, 0 }, + radius = 0.5, + width = 5, + target = append_rail_pos, + surface = surf, + time_to_live = 120, + }) return end end - + --7. Create the appended rail and subtract 1 rail from the hand. - created_rail = surf.create_entity{name = "straight-rail", position = append_rail_pos, direction = append_rail_dir, force = game.forces.player} - + created_rail = surf.create_entity({ + name = "straight-rail", + position = append_rail_pos, + direction = append_rail_dir, + force = game.forces.player, + }) + if not (created_rail ~= nil and created_rail.valid) then - created_rail = game.get_player(pindex).build_from_cursor({name = "straight-rail", position = append_rail_pos, direction = append_rail_dir}) + created_rail = game + .get_player(pindex) + .build_from_cursor({ name = "straight-rail", position = append_rail_pos, direction = append_rail_dir }) if not (created_rail ~= nil and created_rail.valid) then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} - printout("Error: Invalid appended rail, try placing by hand.",pindex) - rendering.draw_circle{color = {1, 0, 0},radius = 0.5,width = 5,target = append_rail_pos,surface = surf,time_to_live = 120} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) + printout("Error: Invalid appended rail, try placing by hand.", pindex) + rendering.draw_circle({ + color = { 1, 0, 0 }, + radius = 0.5, + width = 5, + target = append_rail_pos, + surface = surf, + time_to_live = 120, + }) return end end - + game.get_player(pindex).cursor_stack.count = game.get_player(pindex).cursor_stack.count - 1 - game.get_player(pindex).play_sound{path = "entity-build/straight-rail"} - + game.get_player(pindex).play_sound({ path = "entity-build/straight-rail" }) + --8. Check if the appended rail is with 4 tiles of a parallel rail. If so, delete it. - if created_rail.valid and fa_rails.has_parallel_neighbor(created_rail,pindex) then - game.get_player(pindex).mine_entity(created_rail,true) - game.get_player(pindex).play_sound{path = "utility/cannot_build"} - printout("Cannot place, parallel rail segments should be at least 4 tiles apart.",pindex) + if created_rail.valid and fa_rails.has_parallel_neighbor(created_rail, pindex) then + game.get_player(pindex).mine_entity(created_rail, true) + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) + printout("Cannot place, parallel rail segments should be at least 4 tiles apart.", pindex) end - + --9. Check if the appended rail has created an intersection. If so, notify the player. - if created_rail.valid and fa_rails.is_intersection_rail(created_rail,pindex) then - printout("Intersection created.",pindex) + if created_rail.valid and fa_rails.is_intersection_rail(created_rail, pindex) then + printout("Intersection created.", pindex) end - end ---Builds a 45 degree rail turn to the right from a horizontal or vertical end rail that is the anchor rail. +--Builds a 45 degree rail turn to the right from a horizontal or vertical end rail that is the anchor rail. function mod.build_rail_turn_right_45_degrees(anchor_rail, pindex) local build_comment = "" local surf = game.get_player(pindex).surface @@ -292,12 +310,12 @@ function mod.build_rail_turn_right_45_degrees(anchor_rail, pindex) local can_place_all = true local is_end_rail local anchor_dir = anchor_rail.direction - - --1. Firstly, check if the player has enough rails to place this (3 units) + + --1. Firstly, check if the player has enough rails to place this (3 units) if not (stack.valid and stack.valid_for_read and stack.name == "rail" and stack.count >= 3) then --Check if the inventory has enough if players[pindex].inventory.lua_inventory.get_item_count("rail") < 3 then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout("You need at least 3 rails in your inventory to build this turn.", pindex) return else @@ -308,156 +326,466 @@ function mod.build_rail_turn_right_45_degrees(anchor_rail, pindex) players[pindex].inventory.max = #players[pindex].inventory.lua_inventory end end - + --2. Secondly, verify the end rail and find its direction - is_end_rail, dir, build_comment = fa_rails.check_end_rail(anchor_rail,pindex) + is_end_rail, dir, build_comment = fa_rails.check_end_rail(anchor_rail, pindex) if not is_end_rail then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout(build_comment, pindex) game.get_player(pindex).clear_cursor() return end pos = anchor_rail.position - + --3. Clear trees and rocks in the build area, can be tuned later... -- if dir == dirs.north or dir == dirs.northeast then - -- build_area = {{pos.x-9, pos.y+9},{pos.x+16,pos.y-16}} + -- build_area = {{pos.x-9, pos.y+9},{pos.x+16,pos.y-16}} -- elseif dir == dirs.east or dir == dirs.southeast then - -- build_area = {{pos.x-9, pos.y-9},{pos.x+16,pos.y+16}} + -- build_area = {{pos.x-9, pos.y-9},{pos.x+16,pos.y+16}} -- elseif dir == dirs.south or dir == dirs.southwest then - -- build_area = {{pos.x+9, pos.y-9},{pos.x-16,pos.y+16}} + -- build_area = {{pos.x+9, pos.y-9},{pos.x-16,pos.y+16}} -- elseif dir == dirs.west or dir == dirs.northwest then - -- build_area = {{pos.x+9, pos.y+9},{pos.x-16,pos.y-16}} - -- end - temp1, build_comment = fa_mining_tools.clear_obstacles_in_circle(pos,12, pindex) - + -- build_area = {{pos.x+9, pos.y+9},{pos.x-16,pos.y-16}} + -- end + temp1, build_comment = fa_mining_tools.clear_obstacles_in_circle(pos, 12, pindex) + --4. Check if every object can be placed - if dir == dirs.north then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+2, pos.y-4}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+4, pos.y-8}, direction = dirs.northwest, force = game.forces.player} + if dir == dirs.north then + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 2, pos.y - 4 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 4, pos.y - 8 }, + direction = dirs.northwest, + force = game.forces.player, + }) elseif dir == dirs.east then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+6, pos.y+2}, direction = dirs.southeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+8, pos.y+4}, direction = dirs.northeast, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 6, pos.y + 2 }, + direction = dirs.southeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 8, pos.y + 4 }, + direction = dirs.northeast, + force = game.forces.player, + }) elseif dir == dirs.south then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+0, pos.y+6}, direction = dirs.southwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-4, pos.y+8}, direction = dirs.southeast, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 0, pos.y + 6 }, + direction = dirs.southwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 4, pos.y + 8 }, + direction = dirs.southeast, + force = game.forces.player, + }) elseif dir == dirs.west then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x-4, pos.y+0}, direction = dirs.northwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-8, pos.y-4}, direction = dirs.southwest, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 4, pos.y + 0 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 8, pos.y - 4 }, + direction = dirs.southwest, + force = game.forces.player, + }) elseif dir == dirs.northeast then if anchor_dir == dirs.northwest then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+4, pos.y-2}, direction = dirs.west, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+8, pos.y-4}, direction = dirs.east, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 4, pos.y - 2 }, + direction = dirs.west, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 8, pos.y - 4 }, + direction = dirs.east, + force = game.forces.player, + }) elseif anchor_dir == dirs.southeast then - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+2, pos.y-0}, direction = dirs.northwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+6, pos.y-2}, direction = dirs.west, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+10, pos.y-4}, direction = dirs.east, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 2, pos.y - 0 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 6, pos.y - 2 }, + direction = dirs.west, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 10, pos.y - 4 }, + direction = dirs.east, + force = game.forces.player, + }) end elseif dir == dirs.southwest then - if anchor_dir == dirs.southeast then--2 - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x-2, pos.y+4}, direction = dirs.east, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-8, pos.y+4}, direction = dirs.east, force = game.forces.player} - elseif anchor_dir == dirs.northwest then--3 - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-2, pos.y+0}, direction = dirs.southeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x-4, pos.y+4}, direction = dirs.east, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-10, pos.y+4}, direction = dirs.east, force = game.forces.player} + if anchor_dir == dirs.southeast then --2 + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 2, pos.y + 4 }, + direction = dirs.east, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 8, pos.y + 4 }, + direction = dirs.east, + force = game.forces.player, + }) + elseif anchor_dir == dirs.northwest then --3 + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 2, pos.y + 0 }, + direction = dirs.southeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 4, pos.y + 4 }, + direction = dirs.east, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 10, pos.y + 4 }, + direction = dirs.east, + force = game.forces.player, + }) end elseif dir == dirs.southeast then - if anchor_dir == dirs.northeast then--2 - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+4, pos.y+4}, direction = dirs.north, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+4, pos.y+8}, direction = dirs.north, force = game.forces.player} - elseif anchor_dir == dirs.southwest then--3 - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-0, pos.y+2}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+4, pos.y+6}, direction = dirs.north, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+4, pos.y+10}, direction = dirs.north, force = game.forces.player} + if anchor_dir == dirs.northeast then --2 + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 4, pos.y + 4 }, + direction = dirs.north, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 4, pos.y + 8 }, + direction = dirs.north, + force = game.forces.player, + }) + elseif anchor_dir == dirs.southwest then --3 + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 0, pos.y + 2 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 4, pos.y + 6 }, + direction = dirs.north, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 4, pos.y + 10 }, + direction = dirs.north, + force = game.forces.player, + }) end elseif dir == dirs.northwest then - if anchor_dir == dirs.southwest then--2 - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x-2, pos.y-2}, direction = dirs.south, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-4, pos.y-8}, direction = dirs.north, force = game.forces.player} - elseif anchor_dir == dirs.northeast then--3 - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-0, pos.y-2}, direction = dirs.southwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x-2, pos.y-4}, direction = dirs.south, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-4, pos.y-10}, direction = dirs.north, force = game.forces.player} + if anchor_dir == dirs.southwest then --2 + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 2, pos.y - 2 }, + direction = dirs.south, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 4, pos.y - 8 }, + direction = dirs.north, + force = game.forces.player, + }) + elseif anchor_dir == dirs.northeast then --3 + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 0, pos.y - 2 }, + direction = dirs.southwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 2, pos.y - 4 }, + direction = dirs.south, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 4, pos.y - 10 }, + direction = dirs.north, + force = game.forces.player, + }) end end - + if not can_place_all then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout("Building area occupied.", pindex) game.get_player(pindex).clear_cursor() return end - + --5. Build the rail entities to create the turn - if dir == dirs.north then - surf.create_entity{name = "curved-rail", position = {pos.x+2, pos.y-4}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+4, pos.y-8}, direction = dirs.northwest, force = game.forces.player} + if dir == dirs.north then + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 2, pos.y - 4 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 4, pos.y - 8 }, + direction = dirs.northwest, + force = game.forces.player, + }) elseif dir == dirs.east then - surf.create_entity{name = "curved-rail", position = {pos.x+6, pos.y+2}, direction = dirs.southeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+8, pos.y+4}, direction = dirs.northeast, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 6, pos.y + 2 }, + direction = dirs.southeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 8, pos.y + 4 }, + direction = dirs.northeast, + force = game.forces.player, + }) elseif dir == dirs.south then - surf.create_entity{name = "curved-rail", position = {pos.x+0, pos.y+6}, direction = dirs.southwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-4, pos.y+8}, direction = dirs.southeast, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 0, pos.y + 6 }, + direction = dirs.southwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 4, pos.y + 8 }, + direction = dirs.southeast, + force = game.forces.player, + }) elseif dir == dirs.west then - surf.create_entity{name = "curved-rail", position = {pos.x-4, pos.y+0}, direction = dirs.northwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-8, pos.y-4}, direction = dirs.southwest, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 4, pos.y + 0 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 8, pos.y - 4 }, + direction = dirs.southwest, + force = game.forces.player, + }) elseif dir == dirs.northeast then if anchor_dir == dirs.northwest then - surf.create_entity{name = "curved-rail", position = {pos.x+4, pos.y-2}, direction = dirs.west, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+8, pos.y-4}, direction = dirs.east, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 4, pos.y - 2 }, + direction = dirs.west, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 8, pos.y - 4 }, + direction = dirs.east, + force = game.forces.player, + }) elseif anchor_dir == dirs.southeast then - surf.create_entity{name = "straight-rail", position = {pos.x+2, pos.y-0}, direction = dirs.northwest, force = game.forces.player} - surf.create_entity{name = "curved-rail", position = {pos.x+6, pos.y-2}, direction = dirs.west, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+10, pos.y-4}, direction = dirs.east, force = game.forces.player} + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 2, pos.y - 0 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 6, pos.y - 2 }, + direction = dirs.west, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 10, pos.y - 4 }, + direction = dirs.east, + force = game.forces.player, + }) end elseif dir == dirs.southwest then - if anchor_dir == dirs.southeast then--2 - surf.create_entity{name = "curved-rail", position = {pos.x-2, pos.y+4}, direction = dirs.east, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-8, pos.y+4}, direction = dirs.east, force = game.forces.player} - elseif anchor_dir == dirs.northwest then--3 - surf.create_entity{name = "straight-rail", position = {pos.x-2, pos.y+0}, direction = dirs.southeast, force = game.forces.player} - surf.create_entity{name = "curved-rail", position = {pos.x-4, pos.y+4}, direction = dirs.east, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-10, pos.y+4}, direction = dirs.east, force = game.forces.player} + if anchor_dir == dirs.southeast then --2 + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 2, pos.y + 4 }, + direction = dirs.east, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 8, pos.y + 4 }, + direction = dirs.east, + force = game.forces.player, + }) + elseif anchor_dir == dirs.northwest then --3 + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 2, pos.y + 0 }, + direction = dirs.southeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 4, pos.y + 4 }, + direction = dirs.east, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 10, pos.y + 4 }, + direction = dirs.east, + force = game.forces.player, + }) end elseif dir == dirs.southeast then - if anchor_dir == dirs.northeast then--2 - surf.create_entity{name = "curved-rail", position = {pos.x+4, pos.y+4}, direction = dirs.north, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+4, pos.y+8}, direction = dirs.north, force = game.forces.player} - elseif anchor_dir == dirs.southwest then--3 - surf.create_entity{name = "straight-rail", position = {pos.x-0, pos.y+2}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "curved-rail", position = {pos.x+4, pos.y+6}, direction = dirs.north, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+4, pos.y+10}, direction = dirs.north, force = game.forces.player} + if anchor_dir == dirs.northeast then --2 + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 4, pos.y + 4 }, + direction = dirs.north, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 4, pos.y + 8 }, + direction = dirs.north, + force = game.forces.player, + }) + elseif anchor_dir == dirs.southwest then --3 + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 0, pos.y + 2 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 4, pos.y + 6 }, + direction = dirs.north, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 4, pos.y + 10 }, + direction = dirs.north, + force = game.forces.player, + }) end elseif dir == dirs.northwest then - if anchor_dir == dirs.southwest then--2 - surf.create_entity{name = "curved-rail", position = {pos.x-2, pos.y-2}, direction = dirs.south, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-4, pos.y-8}, direction = dirs.north, force = game.forces.player} - elseif anchor_dir == dirs.northeast then--3 - surf.create_entity{name = "straight-rail", position = {pos.x-0, pos.y-2}, direction = dirs.southwest, force = game.forces.player} - surf.create_entity{name = "curved-rail", position = {pos.x-2, pos.y-4}, direction = dirs.south, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-4, pos.y-10}, direction = dirs.north, force = game.forces.player} + if anchor_dir == dirs.southwest then --2 + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 2, pos.y - 2 }, + direction = dirs.south, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 4, pos.y - 8 }, + direction = dirs.north, + force = game.forces.player, + }) + elseif anchor_dir == dirs.northeast then --3 + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 0, pos.y - 2 }, + direction = dirs.southwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 2, pos.y - 4 }, + direction = dirs.south, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 4, pos.y - 10 }, + direction = dirs.north, + force = game.forces.player, + }) end end - - + --6 Remove rail units from the player's hand game.get_player(pindex).cursor_stack.count = game.get_player(pindex).cursor_stack.count - 2 - if (dir == dirs.northeast and anchor_dir == dirs.southeast) or (dir == dirs.southwest and anchor_dir == dirs.northwest) or (dir == dirs.southeast and anchor_dir == dirs.southwest) or (dir == dirs.northwest and anchor_dir == dirs.northeast) then + if + (dir == dirs.northeast and anchor_dir == dirs.southeast) + or (dir == dirs.southwest and anchor_dir == dirs.northwest) + or (dir == dirs.southeast and anchor_dir == dirs.southwest) + or (dir == dirs.northwest and anchor_dir == dirs.northeast) + then game.get_player(pindex).cursor_stack.count = game.get_player(pindex).cursor_stack.count - 1 end game.get_player(pindex).clear_cursor() - + --7. Sounds and results - game.get_player(pindex).play_sound{path = "entity-build/straight-rail"} - game.get_player(pindex).play_sound{path = "entity-build/curved-rail"} + game.get_player(pindex).play_sound({ path = "entity-build/straight-rail" }) + game.get_player(pindex).play_sound({ path = "entity-build/curved-rail" }) printout("Rail turn built 45 degrees right, " .. build_comment, pindex) return - end - ---Builds a 90 degree rail turn to the right from a horizontal or vertical end rail that is the anchor rail. +--Builds a 90 degree rail turn to the right from a horizontal or vertical end rail that is the anchor rail. function mod.build_rail_turn_right_90_degrees(anchor_rail, pindex) local build_comment = "" local surf = game.get_player(pindex).surface @@ -468,12 +796,12 @@ function mod.build_rail_turn_right_90_degrees(anchor_rail, pindex) local build_area = nil local can_place_all = true local is_end_rail - - --1. Firstly, check if the player has enough rails to place this (10 units) + + --1. Firstly, check if the player has enough rails to place this (10 units) if not (stack.valid and stack.valid_for_read and stack.name == "rail" and stack.count >= 10) then --Check if the inventory has enough if players[pindex].inventory.lua_inventory.get_item_count("rail") < 10 then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout("You need at least 10 rails in your inventory to build this turn.", pindex) return else @@ -484,102 +812,276 @@ function mod.build_rail_turn_right_90_degrees(anchor_rail, pindex) players[pindex].inventory.max = #players[pindex].inventory.lua_inventory end end - + --2. Secondly, verify the end rail and find its direction - is_end_rail, dir, build_comment = fa_rails.check_end_rail(anchor_rail,pindex) + is_end_rail, dir, build_comment = fa_rails.check_end_rail(anchor_rail, pindex) if not is_end_rail then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout(build_comment, pindex) game.get_player(pindex).clear_cursor() return end pos = anchor_rail.position if dir == dirs.northeast or dir == dirs.southeast or dir == dirs.southwest or dir == dirs.northwest then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout("This structure is for horizontal or vertical end rails only.", pindex) game.get_player(pindex).clear_cursor() return end - + --3. Clear trees and rocks in the build area -- if dir == dirs.north then - -- build_area = {{pos.x-2, pos.y+2},{pos.x+16,pos.y-16}} + -- build_area = {{pos.x-2, pos.y+2},{pos.x+16,pos.y-16}} -- elseif dir == dirs.east then - -- build_area = {{pos.x-2, pos.y-2},{pos.x+16,pos.y+16}} + -- build_area = {{pos.x-2, pos.y-2},{pos.x+16,pos.y+16}} -- elseif dir == dirs.south then - -- build_area = {{pos.x+2, pos.y-2},{pos.x-16,pos.y+16}} + -- build_area = {{pos.x+2, pos.y-2},{pos.x-16,pos.y+16}} -- elseif dir == dirs.west then - -- build_area = {{pos.x+2, pos.y+2},{pos.x-16,pos.y-16}} - -- end - temp1, build_comment = fa_mining_tools.clear_obstacles_in_circle(pos,18, pindex) - + -- build_area = {{pos.x+2, pos.y+2},{pos.x-16,pos.y-16}} + -- end + temp1, build_comment = fa_mining_tools.clear_obstacles_in_circle(pos, 18, pindex) + --4. Check if every object can be placed - if dir == dirs.north then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+2, pos.y-4}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+4, pos.y-8}, direction = dirs.northwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+8, pos.y-10}, direction = dirs.west, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+12, pos.y-12}, direction = dirs.east, force = game.forces.player} + if dir == dirs.north then + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 2, pos.y - 4 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 4, pos.y - 8 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 8, pos.y - 10 }, + direction = dirs.west, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 12, pos.y - 12 }, + direction = dirs.east, + force = game.forces.player, + }) elseif dir == dirs.east then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+6, pos.y+2}, direction = dirs.southeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+8, pos.y+4}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+12, pos.y+8}, direction = dirs.north, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+12, pos.y+12}, direction = dirs.south, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 6, pos.y + 2 }, + direction = dirs.southeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 8, pos.y + 4 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 12, pos.y + 8 }, + direction = dirs.north, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 12, pos.y + 12 }, + direction = dirs.south, + force = game.forces.player, + }) elseif dir == dirs.south then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+0, pos.y+6}, direction = dirs.southwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-4, pos.y+8}, direction = dirs.southeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x-6, pos.y+12}, direction = dirs.east, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-12, pos.y+12}, direction = dirs.west, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 0, pos.y + 6 }, + direction = dirs.southwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 4, pos.y + 8 }, + direction = dirs.southeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 6, pos.y + 12 }, + direction = dirs.east, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 12, pos.y + 12 }, + direction = dirs.west, + force = game.forces.player, + }) elseif dir == dirs.west then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x-4, pos.y+0}, direction = dirs.northwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-8, pos.y-4}, direction = dirs.southwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x-10, pos.y-6}, direction = dirs.south, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-12, pos.y-12}, direction = dirs.north, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 4, pos.y + 0 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 8, pos.y - 4 }, + direction = dirs.southwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 10, pos.y - 6 }, + direction = dirs.south, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 12, pos.y - 12 }, + direction = dirs.north, + force = game.forces.player, + }) end - + if not can_place_all then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout("Building area occupied.", pindex) game.get_player(pindex).clear_cursor() return end - + --5. Build the five rail entities to create the turn - if dir == dirs.north then - surf.create_entity{name = "curved-rail", position = {pos.x+2, pos.y-4}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+4, pos.y-8}, direction = dirs.northwest, force = game.forces.player} - surf.create_entity{name = "curved-rail", position = {pos.x+8, pos.y-10}, direction = dirs.west, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+12, pos.y-12}, direction = dirs.east, force = game.forces.player} + if dir == dirs.north then + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 2, pos.y - 4 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 4, pos.y - 8 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 8, pos.y - 10 }, + direction = dirs.west, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 12, pos.y - 12 }, + direction = dirs.east, + force = game.forces.player, + }) elseif dir == dirs.east then - surf.create_entity{name = "curved-rail", position = {pos.x+6, pos.y+2}, direction = dirs.southeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+8, pos.y+4}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "curved-rail", position = {pos.x+12, pos.y+8}, direction = dirs.north, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+12, pos.y+12}, direction = dirs.south, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 6, pos.y + 2 }, + direction = dirs.southeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 8, pos.y + 4 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 12, pos.y + 8 }, + direction = dirs.north, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 12, pos.y + 12 }, + direction = dirs.south, + force = game.forces.player, + }) elseif dir == dirs.south then - surf.create_entity{name = "curved-rail", position = {pos.x+0, pos.y+6}, direction = dirs.southwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-4, pos.y+8}, direction = dirs.southeast, force = game.forces.player} - surf.create_entity{name = "curved-rail", position = {pos.x-6, pos.y+12}, direction = dirs.east, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-12, pos.y+12}, direction = dirs.west, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 0, pos.y + 6 }, + direction = dirs.southwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 4, pos.y + 8 }, + direction = dirs.southeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 6, pos.y + 12 }, + direction = dirs.east, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 12, pos.y + 12 }, + direction = dirs.west, + force = game.forces.player, + }) elseif dir == dirs.west then - surf.create_entity{name = "curved-rail", position = {pos.x-4, pos.y+0}, direction = dirs.northwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-8, pos.y-4}, direction = dirs.southwest, force = game.forces.player} - surf.create_entity{name = "curved-rail", position = {pos.x-10, pos.y-6}, direction = dirs.south, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-12, pos.y-12}, direction = dirs.north, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 4, pos.y + 0 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 8, pos.y - 4 }, + direction = dirs.southwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 10, pos.y - 6 }, + direction = dirs.south, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 12, pos.y - 12 }, + direction = dirs.north, + force = game.forces.player, + }) end - + --6 Remove 10 rail units from the player's hand game.get_player(pindex).cursor_stack.count = game.get_player(pindex).cursor_stack.count - 10 game.get_player(pindex).clear_cursor() - + --7. Sounds and results - game.get_player(pindex).play_sound{path = "entity-build/straight-rail"} - game.get_player(pindex).play_sound{path = "entity-build/curved-rail"} + game.get_player(pindex).play_sound({ path = "entity-build/straight-rail" }) + game.get_player(pindex).play_sound({ path = "entity-build/curved-rail" }) printout("Rail turn built 90 degrees right, " .. build_comment, pindex) return - end - ---Builds a 45 degree rail turn to the left from a horizontal or vertical end rail that is the anchor rail. +--Builds a 45 degree rail turn to the left from a horizontal or vertical end rail that is the anchor rail. function mod.build_rail_turn_left_45_degrees(anchor_rail, pindex) local build_comment = "" local surf = game.get_player(pindex).surface @@ -591,12 +1093,12 @@ function mod.build_rail_turn_left_45_degrees(anchor_rail, pindex) local can_place_all = true local is_end_rail local anchor_dir = anchor_rail.direction - - --1. Firstly, check if the player has enough rails to place this (3 units) + + --1. Firstly, check if the player has enough rails to place this (3 units) if not (stack.valid and stack.valid_for_read and stack.name == "rail" and stack.count >= 3) then --Check if the inventory has enough if players[pindex].inventory.lua_inventory.get_item_count("rail") < 3 then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout("You need at least 3 rails in your inventory to build this turn.", pindex) return else @@ -607,156 +1109,466 @@ function mod.build_rail_turn_left_45_degrees(anchor_rail, pindex) players[pindex].inventory.max = #players[pindex].inventory.lua_inventory end end - + --2. Secondly, verify the end rail and find its direction - is_end_rail, dir, build_comment = fa_rails.check_end_rail(anchor_rail,pindex) + is_end_rail, dir, build_comment = fa_rails.check_end_rail(anchor_rail, pindex) if not is_end_rail then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout(build_comment, pindex) game.get_player(pindex).clear_cursor() return end pos = anchor_rail.position - + --3. Clear trees and rocks in the build area, can be tuned later... -- if dir == dirs.north or dir == dirs.northeast then - -- build_area = {{pos.x+9, pos.y+9},{pos.x-16,pos.y-16}} + -- build_area = {{pos.x+9, pos.y+9},{pos.x-16,pos.y-16}} -- elseif dir == dirs.east or dir == dirs.southeast then - -- build_area = {{pos.x-9, pos.y+9},{pos.x+16,pos.y-16}} + -- build_area = {{pos.x-9, pos.y+9},{pos.x+16,pos.y-16}} -- elseif dir == dirs.south or dir == dirs.southwest then - -- build_area = {{pos.x-9, pos.y-9},{pos.x+16,pos.y+16}} + -- build_area = {{pos.x-9, pos.y-9},{pos.x+16,pos.y+16}} -- elseif dir == dirs.west or dir == dirs.northwest then - -- build_area = {{pos.x+9, pos.y-9},{pos.x-16,pos.y+16}} - -- end - temp1, build_comment = fa_mining_tools.clear_obstacles_in_circle(pos,12, pindex) - + -- build_area = {{pos.x+9, pos.y-9},{pos.x-16,pos.y+16}} + -- end + temp1, build_comment = fa_mining_tools.clear_obstacles_in_circle(pos, 12, pindex) + --4. Check if every object can be placed - if dir == dirs.north then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+0, pos.y-4}, direction = dirs.north, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-4, pos.y-8}, direction = dirs.northeast, force = game.forces.player} + if dir == dirs.north then + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 0, pos.y - 4 }, + direction = dirs.north, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 4, pos.y - 8 }, + direction = dirs.northeast, + force = game.forces.player, + }) elseif dir == dirs.east then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+6, pos.y+0}, direction = dirs.east, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+8, pos.y-4}, direction = dirs.southeast, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 6, pos.y + 0 }, + direction = dirs.east, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 8, pos.y - 4 }, + direction = dirs.southeast, + force = game.forces.player, + }) elseif dir == dirs.south then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+2, pos.y+6}, direction = dirs.south, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+4, pos.y+8}, direction = dirs.southwest, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 2, pos.y + 6 }, + direction = dirs.south, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 4, pos.y + 8 }, + direction = dirs.southwest, + force = game.forces.player, + }) elseif dir == dirs.west then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x-4, pos.y+2}, direction = dirs.west, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-8, pos.y+4}, direction = dirs.northwest, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 4, pos.y + 2 }, + direction = dirs.west, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 8, pos.y + 4 }, + direction = dirs.northwest, + force = game.forces.player, + }) elseif dir == dirs.northeast then - if anchor_dir == dirs.southeast then--2 - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+4, pos.y-2}, direction = dirs.southwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+4, pos.y-8}, direction = dirs.north, force = game.forces.player} - elseif anchor_dir == dirs.northwest then--3 - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+0, pos.y-2}, direction = dirs.southeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+4, pos.y-4}, direction = dirs.southwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+4, pos.y-10}, direction = dirs.north, force = game.forces.player} + if anchor_dir == dirs.southeast then --2 + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 4, pos.y - 2 }, + direction = dirs.southwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 4, pos.y - 8 }, + direction = dirs.north, + force = game.forces.player, + }) + elseif anchor_dir == dirs.northwest then --3 + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 0, pos.y - 2 }, + direction = dirs.southeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 4, pos.y - 4 }, + direction = dirs.southwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 4, pos.y - 10 }, + direction = dirs.north, + force = game.forces.player, + }) end elseif dir == dirs.southwest then - if anchor_dir == dirs.northwest then--2 - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x-2, pos.y+4}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-4, pos.y+8}, direction = dirs.north, force = game.forces.player} - elseif anchor_dir == dirs.southeast then--3 - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-0, pos.y+2}, direction = dirs.northwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x-2, pos.y+6}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-4, pos.y+10}, direction = dirs.north, force = game.forces.player} + if anchor_dir == dirs.northwest then --2 + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 2, pos.y + 4 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 4, pos.y + 8 }, + direction = dirs.north, + force = game.forces.player, + }) + elseif anchor_dir == dirs.southeast then --3 + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 0, pos.y + 2 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 2, pos.y + 6 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 4, pos.y + 10 }, + direction = dirs.north, + force = game.forces.player, + }) end elseif dir == dirs.southeast then - if anchor_dir == dirs.southwest then--2 - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+4, pos.y+4}, direction = dirs.northwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+8, pos.y+4}, direction = dirs.east, force = game.forces.player} - elseif anchor_dir == dirs.northeast then--3 - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+2, pos.y+0}, direction = dirs.southwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+6, pos.y+4}, direction = dirs.northwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+10, pos.y+4}, direction = dirs.east, force = game.forces.player} + if anchor_dir == dirs.southwest then --2 + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 4, pos.y + 4 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 8, pos.y + 4 }, + direction = dirs.east, + force = game.forces.player, + }) + elseif anchor_dir == dirs.northeast then --3 + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 2, pos.y + 0 }, + direction = dirs.southwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 6, pos.y + 4 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 10, pos.y + 4 }, + direction = dirs.east, + force = game.forces.player, + }) end elseif dir == dirs.northwest then - if anchor_dir == dirs.northeast then--2 - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x-2, pos.y-2}, direction = dirs.southeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-8, pos.y-4}, direction = dirs.east, force = game.forces.player} - elseif anchor_dir == dirs.southwest then--3 - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-2, pos.y-0}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x-4, pos.y-2}, direction = dirs.southeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-10, pos.y-4}, direction = dirs.east, force = game.forces.player} + if anchor_dir == dirs.northeast then --2 + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 2, pos.y - 2 }, + direction = dirs.southeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 8, pos.y - 4 }, + direction = dirs.east, + force = game.forces.player, + }) + elseif anchor_dir == dirs.southwest then --3 + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 2, pos.y - 0 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 4, pos.y - 2 }, + direction = dirs.southeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 10, pos.y - 4 }, + direction = dirs.east, + force = game.forces.player, + }) end end - + if not can_place_all then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout("Building area occupied.", pindex) game.get_player(pindex).clear_cursor() return end - + --5. Build the rail entities to create the turn - if dir == dirs.north then - surf.create_entity{name = "curved-rail", position = {pos.x+0, pos.y-4}, direction = dirs.north, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-4, pos.y-8}, direction = dirs.northeast, force = game.forces.player} + if dir == dirs.north then + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 0, pos.y - 4 }, + direction = dirs.north, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 4, pos.y - 8 }, + direction = dirs.northeast, + force = game.forces.player, + }) elseif dir == dirs.east then - surf.create_entity{name = "curved-rail", position = {pos.x+6, pos.y+0}, direction = dirs.east, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+8, pos.y-4}, direction = dirs.southeast, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 6, pos.y + 0 }, + direction = dirs.east, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 8, pos.y - 4 }, + direction = dirs.southeast, + force = game.forces.player, + }) elseif dir == dirs.south then - surf.create_entity{name = "curved-rail", position = {pos.x+2, pos.y+6}, direction = dirs.south, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+4, pos.y+8}, direction = dirs.southwest, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 2, pos.y + 6 }, + direction = dirs.south, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 4, pos.y + 8 }, + direction = dirs.southwest, + force = game.forces.player, + }) elseif dir == dirs.west then - surf.create_entity{name = "curved-rail", position = {pos.x-4, pos.y+2}, direction = dirs.west, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-8, pos.y+4}, direction = dirs.northwest, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 4, pos.y + 2 }, + direction = dirs.west, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 8, pos.y + 4 }, + direction = dirs.northwest, + force = game.forces.player, + }) elseif dir == dirs.northeast then - if anchor_dir == dirs.southeast then--2 - surf.create_entity{name = "curved-rail", position = {pos.x+4, pos.y-2}, direction = dirs.southwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+4, pos.y-8}, direction = dirs.north, force = game.forces.player} - elseif anchor_dir == dirs.northwest then--3 - surf.create_entity{name = "straight-rail", position = {pos.x+0, pos.y-2}, direction = dirs.southeast, force = game.forces.player} - surf.create_entity{name = "curved-rail", position = {pos.x+4, pos.y-4}, direction = dirs.southwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+4, pos.y-10}, direction = dirs.north, force = game.forces.player} + if anchor_dir == dirs.southeast then --2 + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 4, pos.y - 2 }, + direction = dirs.southwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 4, pos.y - 8 }, + direction = dirs.north, + force = game.forces.player, + }) + elseif anchor_dir == dirs.northwest then --3 + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 0, pos.y - 2 }, + direction = dirs.southeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 4, pos.y - 4 }, + direction = dirs.southwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 4, pos.y - 10 }, + direction = dirs.north, + force = game.forces.player, + }) end elseif dir == dirs.southwest then - if anchor_dir == dirs.northwest then--2 - surf.create_entity{name = "curved-rail", position = {pos.x-2, pos.y+4}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-4, pos.y+8}, direction = dirs.north, force = game.forces.player} - elseif anchor_dir == dirs.southeast then--3 - surf.create_entity{name = "straight-rail", position = {pos.x-0, pos.y+2}, direction = dirs.northwest, force = game.forces.player} - surf.create_entity{name = "curved-rail", position = {pos.x-2, pos.y+6}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-4, pos.y+10}, direction = dirs.north, force = game.forces.player} + if anchor_dir == dirs.northwest then --2 + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 2, pos.y + 4 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 4, pos.y + 8 }, + direction = dirs.north, + force = game.forces.player, + }) + elseif anchor_dir == dirs.southeast then --3 + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 0, pos.y + 2 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 2, pos.y + 6 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 4, pos.y + 10 }, + direction = dirs.north, + force = game.forces.player, + }) end elseif dir == dirs.southeast then - if anchor_dir == dirs.southwest then--2 - surf.create_entity{name = "curved-rail", position = {pos.x+4, pos.y+4}, direction = dirs.northwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+8, pos.y+4}, direction = dirs.east, force = game.forces.player} - elseif anchor_dir == dirs.northeast then--3 - surf.create_entity{name = "straight-rail", position = {pos.x+2, pos.y+0}, direction = dirs.southwest, force = game.forces.player} - surf.create_entity{name = "curved-rail", position = {pos.x+6, pos.y+4}, direction = dirs.northwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+10, pos.y+4}, direction = dirs.east, force = game.forces.player} + if anchor_dir == dirs.southwest then --2 + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 4, pos.y + 4 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 8, pos.y + 4 }, + direction = dirs.east, + force = game.forces.player, + }) + elseif anchor_dir == dirs.northeast then --3 + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 2, pos.y + 0 }, + direction = dirs.southwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 6, pos.y + 4 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 10, pos.y + 4 }, + direction = dirs.east, + force = game.forces.player, + }) end elseif dir == dirs.northwest then - if anchor_dir == dirs.northeast then--2 - surf.create_entity{name = "curved-rail", position = {pos.x-2, pos.y-2}, direction = dirs.southeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-8, pos.y-4}, direction = dirs.east, force = game.forces.player} - elseif anchor_dir == dirs.southwest then--3 - surf.create_entity{name = "straight-rail", position = {pos.x-2, pos.y-0}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "curved-rail", position = {pos.x-4, pos.y-2}, direction = dirs.southeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-10, pos.y-4}, direction = dirs.east, force = game.forces.player} + if anchor_dir == dirs.northeast then --2 + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 2, pos.y - 2 }, + direction = dirs.southeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 8, pos.y - 4 }, + direction = dirs.east, + force = game.forces.player, + }) + elseif anchor_dir == dirs.southwest then --3 + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 2, pos.y - 0 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 4, pos.y - 2 }, + direction = dirs.southeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 10, pos.y - 4 }, + direction = dirs.east, + force = game.forces.player, + }) end end - - + --6 Remove rail units from the player's hand game.get_player(pindex).cursor_stack.count = game.get_player(pindex).cursor_stack.count - 2 - if (dir == dirs.northeast and anchor_dir == dirs.northwest) or (dir == dirs.southwest and anchor_dir == dirs.southeast) or (dir == dirs.southeast and anchor_dir == dirs.northeast) or (dir == dirs.northwest and anchor_dir == dirs.southwest) then + if + (dir == dirs.northeast and anchor_dir == dirs.northwest) + or (dir == dirs.southwest and anchor_dir == dirs.southeast) + or (dir == dirs.southeast and anchor_dir == dirs.northeast) + or (dir == dirs.northwest and anchor_dir == dirs.southwest) + then game.get_player(pindex).cursor_stack.count = game.get_player(pindex).cursor_stack.count - 1 end game.get_player(pindex).clear_cursor() - + --7. Sounds and results - game.get_player(pindex).play_sound{path = "entity-build/straight-rail"} - game.get_player(pindex).play_sound{path = "entity-build/curved-rail"} + game.get_player(pindex).play_sound({ path = "entity-build/straight-rail" }) + game.get_player(pindex).play_sound({ path = "entity-build/curved-rail" }) printout("Rail turn built 45 degrees left, " .. build_comment, pindex) return - end - ---Builds a 90 degree rail turn to the left from a horizontal or vertical end rail that is the anchor rail. +--Builds a 90 degree rail turn to the left from a horizontal or vertical end rail that is the anchor rail. function mod.build_rail_turn_left_90_degrees(anchor_rail, pindex) local build_comment = "" local surf = game.get_player(pindex).surface @@ -767,12 +1579,12 @@ function mod.build_rail_turn_left_90_degrees(anchor_rail, pindex) local build_area = nil local can_place_all = true local is_end_rail - + --1. Firstly, check if the player has enough rails to place this (10 units) if not (stack.valid and stack.valid_for_read and stack.name == "rail" and stack.count > 10) then --Check if the inventory has enough if players[pindex].inventory.lua_inventory.get_item_count("rail") < 10 then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout("You need at least 10 rails in your inventory to build this turn.", pindex) return else @@ -783,100 +1595,275 @@ function mod.build_rail_turn_left_90_degrees(anchor_rail, pindex) players[pindex].inventory.max = #players[pindex].inventory.lua_inventory end end - + --2. Secondly, verify the end rail and find its direction - is_end_rail, dir, build_comment = fa_rails.check_end_rail(anchor_rail,pindex) + is_end_rail, dir, build_comment = fa_rails.check_end_rail(anchor_rail, pindex) if not is_end_rail then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout(build_comment, pindex) game.get_player(pindex).clear_cursor() return end pos = anchor_rail.position if dir == dirs.northeast or dir == dirs.southeast or dir == dirs.southwest or dir == dirs.northwest then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout("This structure is for horizontal or vertical end rails only.", pindex) game.get_player(pindex).clear_cursor() return end - + --3. Clear trees and rocks in the build area -- if dir == dirs.north then - -- build_area = {{pos.x+2, pos.y+2},{pos.x-16,pos.y-16}} + -- build_area = {{pos.x+2, pos.y+2},{pos.x-16,pos.y-16}} -- elseif dir == dirs.east then - -- build_area = {{pos.x+2, pos.y+2},{pos.x+16,pos.y-16}} + -- build_area = {{pos.x+2, pos.y+2},{pos.x+16,pos.y-16}} -- elseif dir == dirs.south then - -- build_area = {{pos.x+2, pos.y+2},{pos.x+16,pos.y+16}} + -- build_area = {{pos.x+2, pos.y+2},{pos.x+16,pos.y+16}} -- elseif dir == dirs.west then - -- build_area = {{pos.x+2, pos.y+2},{pos.x-16,pos.y+16}} - -- end - temp1, build_comment = fa_mining_tools.clear_obstacles_in_circle(pos,18, pindex) - + -- build_area = {{pos.x+2, pos.y+2},{pos.x-16,pos.y+16}} + -- end + temp1, build_comment = fa_mining_tools.clear_obstacles_in_circle(pos, 18, pindex) + --4. Check if every object can be placed - if dir == dirs.north then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+0, pos.y-4}, direction = dirs.north, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-4, pos.y-8}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x-6, pos.y-10}, direction = dirs.southeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-12, pos.y-12}, direction = dirs.east, force = game.forces.player} + if dir == dirs.north then + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 0, pos.y - 4 }, + direction = dirs.north, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 4, pos.y - 8 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 6, pos.y - 10 }, + direction = dirs.southeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 12, pos.y - 12 }, + direction = dirs.east, + force = game.forces.player, + }) elseif dir == dirs.east then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+6, pos.y+0}, direction = dirs.east, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+8, pos.y-4}, direction = dirs.southeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+12, pos.y-6}, direction = dirs.southwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+12, pos.y-12}, direction = dirs.south, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 6, pos.y + 0 }, + direction = dirs.east, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 8, pos.y - 4 }, + direction = dirs.southeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 12, pos.y - 6 }, + direction = dirs.southwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 12, pos.y - 12 }, + direction = dirs.south, + force = game.forces.player, + }) elseif dir == dirs.south then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+2, pos.y+6}, direction = dirs.south, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+4, pos.y+8}, direction = dirs.southwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+8, pos.y+12}, direction = dirs.northwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+12, pos.y+12}, direction = dirs.west, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 2, pos.y + 6 }, + direction = dirs.south, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 4, pos.y + 8 }, + direction = dirs.southwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 8, pos.y + 12 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 12, pos.y + 12 }, + direction = dirs.west, + force = game.forces.player, + }) elseif dir == dirs.west then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x-4, pos.y+2}, direction = dirs.west, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-8, pos.y+4}, direction = dirs.northwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x-10, pos.y+8}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-12, pos.y+12}, direction = dirs.north, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 4, pos.y + 2 }, + direction = dirs.west, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 8, pos.y + 4 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 10, pos.y + 8 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 12, pos.y + 12 }, + direction = dirs.north, + force = game.forces.player, + }) end - + if not can_place_all then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout("Building area occupied.", pindex) game.get_player(pindex).clear_cursor() return end - + --5. Build the five rail entities to create the turn - if dir == dirs.north then - surf.create_entity{name = "curved-rail", position = {pos.x+0, pos.y-4}, direction = dirs.north, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-4, pos.y-8}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "curved-rail", position = {pos.x-6, pos.y-10}, direction = dirs.southeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-12, pos.y-12}, direction = dirs.east, force = game.forces.player} + if dir == dirs.north then + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 0, pos.y - 4 }, + direction = dirs.north, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 4, pos.y - 8 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 6, pos.y - 10 }, + direction = dirs.southeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 12, pos.y - 12 }, + direction = dirs.east, + force = game.forces.player, + }) elseif dir == dirs.east then - surf.create_entity{name = "curved-rail", position = {pos.x+6, pos.y+0}, direction = dirs.east, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+8, pos.y-4}, direction = dirs.southeast, force = game.forces.player} - surf.create_entity{name = "curved-rail", position = {pos.x+12, pos.y-6}, direction = dirs.southwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+12, pos.y-12}, direction = dirs.south, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 6, pos.y + 0 }, + direction = dirs.east, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 8, pos.y - 4 }, + direction = dirs.southeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 12, pos.y - 6 }, + direction = dirs.southwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 12, pos.y - 12 }, + direction = dirs.south, + force = game.forces.player, + }) elseif dir == dirs.south then - surf.create_entity{name = "curved-rail", position = {pos.x+2, pos.y+6}, direction = dirs.south, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+4, pos.y+8}, direction = dirs.southwest, force = game.forces.player} - surf.create_entity{name = "curved-rail", position = {pos.x+8, pos.y+12}, direction = dirs.northwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+12, pos.y+12}, direction = dirs.west, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 2, pos.y + 6 }, + direction = dirs.south, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 4, pos.y + 8 }, + direction = dirs.southwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 8, pos.y + 12 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 12, pos.y + 12 }, + direction = dirs.west, + force = game.forces.player, + }) elseif dir == dirs.west then - surf.create_entity{name = "curved-rail", position = {pos.x-4, pos.y+2}, direction = dirs.west, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-8, pos.y+4}, direction = dirs.northwest, force = game.forces.player} - surf.create_entity{name = "curved-rail", position = {pos.x-10, pos.y+8}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-12, pos.y+12}, direction = dirs.north, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 4, pos.y + 2 }, + direction = dirs.west, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 8, pos.y + 4 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 10, pos.y + 8 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 12, pos.y + 12 }, + direction = dirs.north, + force = game.forces.player, + }) end - + --6 Remove 10 rail units from the player's hand game.get_player(pindex).cursor_stack.count = game.get_player(pindex).cursor_stack.count - 10 game.get_player(pindex).clear_cursor() - + --7. Sounds and results - game.get_player(pindex).play_sound{path = "entity-build/straight-rail"} - game.get_player(pindex).play_sound{path = "entity-build/curved-rail"} + game.get_player(pindex).play_sound({ path = "entity-build/straight-rail" }) + game.get_player(pindex).play_sound({ path = "entity-build/curved-rail" }) printout("Rail turn built 90 degrees left, " .. build_comment, pindex) return end - --Builds a fork at the end rail with exits 45 degrees left, and 45 degrees right, and forward. function mod.build_fork_at_end_rail(anchor_rail, pindex, include_forward) local build_comment = "" @@ -889,12 +1876,12 @@ function mod.build_fork_at_end_rail(anchor_rail, pindex, include_forward) local can_place_all = true local is_end_rail local anchor_dir = anchor_rail.direction - - --1. Firstly, check if the player has enough rails to place this (5 units) + + --1. Firstly, check if the player has enough rails to place this (5 units) if not (stack.valid and stack.valid_for_read and stack.name == "rail" and stack.count >= 5) then --Check if the inventory has enough if players[pindex].inventory.lua_inventory.get_item_count("rail") < 5 then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout("You need at least 5 rails in your inventory to build this turn.", pindex) return else @@ -905,314 +1892,1103 @@ function mod.build_fork_at_end_rail(anchor_rail, pindex, include_forward) players[pindex].inventory.max = #players[pindex].inventory.lua_inventory end end - + --2. Secondly, verify the end rail and find its direction - is_end_rail, dir, build_comment = fa_rails.check_end_rail(anchor_rail,pindex) + is_end_rail, dir, build_comment = fa_rails.check_end_rail(anchor_rail, pindex) if not is_end_rail then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout(build_comment, pindex) game.get_player(pindex).clear_cursor() return end pos = anchor_rail.position - + --3. Clear trees and rocks in the build area, can be tuned later... -- if dir == dirs.north or dir == dirs.northeast then - -- build_area = {{pos.x+9, pos.y+9},{pos.x-16,pos.y-16}} + -- build_area = {{pos.x+9, pos.y+9},{pos.x-16,pos.y-16}} -- elseif dir == dirs.east or dir == dirs.southeast then - -- build_area = {{pos.x-9, pos.y+9},{pos.x+16,pos.y-16}} + -- build_area = {{pos.x-9, pos.y+9},{pos.x+16,pos.y-16}} -- elseif dir == dirs.south or dir == dirs.southwest then - -- build_area = {{pos.x-9, pos.y-9},{pos.x+16,pos.y+16}} + -- build_area = {{pos.x-9, pos.y-9},{pos.x+16,pos.y+16}} -- elseif dir == dirs.west or dir == dirs.northwest then - -- build_area = {{pos.x+9, pos.y-9},{pos.x-16,pos.y+16}} - -- end - temp1, build_comment = fa_mining_tools.clear_obstacles_in_circle(pos,12, pindex) - + -- build_area = {{pos.x+9, pos.y-9},{pos.x-16,pos.y+16}} + -- end + temp1, build_comment = fa_mining_tools.clear_obstacles_in_circle(pos, 12, pindex) + --4A. Check if every object can be placed (LEFT) - if dir == dirs.north then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+0, pos.y-4}, direction = dirs.north, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-4, pos.y-8}, direction = dirs.northeast, force = game.forces.player} + if dir == dirs.north then + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 0, pos.y - 4 }, + direction = dirs.north, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 4, pos.y - 8 }, + direction = dirs.northeast, + force = game.forces.player, + }) elseif dir == dirs.east then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+6, pos.y+0}, direction = dirs.east, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+8, pos.y-4}, direction = dirs.southeast, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 6, pos.y + 0 }, + direction = dirs.east, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 8, pos.y - 4 }, + direction = dirs.southeast, + force = game.forces.player, + }) elseif dir == dirs.south then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+2, pos.y+6}, direction = dirs.south, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+4, pos.y+8}, direction = dirs.southwest, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 2, pos.y + 6 }, + direction = dirs.south, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 4, pos.y + 8 }, + direction = dirs.southwest, + force = game.forces.player, + }) elseif dir == dirs.west then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x-4, pos.y+2}, direction = dirs.west, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-8, pos.y+4}, direction = dirs.northwest, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 4, pos.y + 2 }, + direction = dirs.west, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 8, pos.y + 4 }, + direction = dirs.northwest, + force = game.forces.player, + }) elseif dir == dirs.northeast then - if anchor_dir == dirs.southeast then--2 - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+4, pos.y-2}, direction = dirs.southwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+4, pos.y-8}, direction = dirs.north, force = game.forces.player} - elseif anchor_dir == dirs.northwest then--3 - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+0, pos.y-2}, direction = dirs.southeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+4, pos.y-4}, direction = dirs.southwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+4, pos.y-10}, direction = dirs.north, force = game.forces.player} + if anchor_dir == dirs.southeast then --2 + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 4, pos.y - 2 }, + direction = dirs.southwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 4, pos.y - 8 }, + direction = dirs.north, + force = game.forces.player, + }) + elseif anchor_dir == dirs.northwest then --3 + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 0, pos.y - 2 }, + direction = dirs.southeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 4, pos.y - 4 }, + direction = dirs.southwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 4, pos.y - 10 }, + direction = dirs.north, + force = game.forces.player, + }) end elseif dir == dirs.southwest then - if anchor_dir == dirs.northwest then--2 - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x-2, pos.y+4}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-4, pos.y+8}, direction = dirs.north, force = game.forces.player} - elseif anchor_dir == dirs.southeast then--3 - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-0, pos.y+2}, direction = dirs.northwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x-2, pos.y+6}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-4, pos.y+10}, direction = dirs.north, force = game.forces.player} + if anchor_dir == dirs.northwest then --2 + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 2, pos.y + 4 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 4, pos.y + 8 }, + direction = dirs.north, + force = game.forces.player, + }) + elseif anchor_dir == dirs.southeast then --3 + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 0, pos.y + 2 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 2, pos.y + 6 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 4, pos.y + 10 }, + direction = dirs.north, + force = game.forces.player, + }) end elseif dir == dirs.southeast then - if anchor_dir == dirs.southwest then--2 - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+4, pos.y+4}, direction = dirs.northwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+8, pos.y+4}, direction = dirs.east, force = game.forces.player} - elseif anchor_dir == dirs.northeast then--3 - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+2, pos.y+0}, direction = dirs.southwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+6, pos.y+4}, direction = dirs.northwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+10, pos.y+4}, direction = dirs.east, force = game.forces.player} + if anchor_dir == dirs.southwest then --2 + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 4, pos.y + 4 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 8, pos.y + 4 }, + direction = dirs.east, + force = game.forces.player, + }) + elseif anchor_dir == dirs.northeast then --3 + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 2, pos.y + 0 }, + direction = dirs.southwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 6, pos.y + 4 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 10, pos.y + 4 }, + direction = dirs.east, + force = game.forces.player, + }) end elseif dir == dirs.northwest then - if anchor_dir == dirs.northeast then--2 - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x-2, pos.y-2}, direction = dirs.southeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-8, pos.y-4}, direction = dirs.east, force = game.forces.player} - elseif anchor_dir == dirs.southwest then--3 - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-2, pos.y-0}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x-4, pos.y-2}, direction = dirs.southeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-10, pos.y-4}, direction = dirs.east, force = game.forces.player} + if anchor_dir == dirs.northeast then --2 + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 2, pos.y - 2 }, + direction = dirs.southeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 8, pos.y - 4 }, + direction = dirs.east, + force = game.forces.player, + }) + elseif anchor_dir == dirs.southwest then --3 + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 2, pos.y - 0 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 4, pos.y - 2 }, + direction = dirs.southeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 10, pos.y - 4 }, + direction = dirs.east, + force = game.forces.player, + }) end end - + --4B. Check if every object can be placed (RIGHT) - if dir == dirs.north then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+2, pos.y-4}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+4, pos.y-8}, direction = dirs.northwest, force = game.forces.player} + if dir == dirs.north then + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 2, pos.y - 4 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 4, pos.y - 8 }, + direction = dirs.northwest, + force = game.forces.player, + }) elseif dir == dirs.east then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+6, pos.y+2}, direction = dirs.southeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+8, pos.y+4}, direction = dirs.northeast, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 6, pos.y + 2 }, + direction = dirs.southeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 8, pos.y + 4 }, + direction = dirs.northeast, + force = game.forces.player, + }) elseif dir == dirs.south then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+0, pos.y+6}, direction = dirs.southwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-4, pos.y+8}, direction = dirs.southeast, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 0, pos.y + 6 }, + direction = dirs.southwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 4, pos.y + 8 }, + direction = dirs.southeast, + force = game.forces.player, + }) elseif dir == dirs.west then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x-4, pos.y+0}, direction = dirs.northwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-8, pos.y-4}, direction = dirs.southwest, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 4, pos.y + 0 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 8, pos.y - 4 }, + direction = dirs.southwest, + force = game.forces.player, + }) elseif dir == dirs.northeast then if anchor_dir == dirs.northwest then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+4, pos.y-2}, direction = dirs.west, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+8, pos.y-4}, direction = dirs.east, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 4, pos.y - 2 }, + direction = dirs.west, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 8, pos.y - 4 }, + direction = dirs.east, + force = game.forces.player, + }) elseif anchor_dir == dirs.southeast then - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+2, pos.y-0}, direction = dirs.northwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+6, pos.y-2}, direction = dirs.west, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+10, pos.y-4}, direction = dirs.east, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 2, pos.y - 0 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 6, pos.y - 2 }, + direction = dirs.west, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 10, pos.y - 4 }, + direction = dirs.east, + force = game.forces.player, + }) end elseif dir == dirs.southwest then - if anchor_dir == dirs.southeast then--2 - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x-2, pos.y+4}, direction = dirs.east, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-8, pos.y+4}, direction = dirs.east, force = game.forces.player} - elseif anchor_dir == dirs.northwest then--3 - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-2, pos.y+0}, direction = dirs.southeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x-4, pos.y+4}, direction = dirs.east, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-10, pos.y+4}, direction = dirs.east, force = game.forces.player} + if anchor_dir == dirs.southeast then --2 + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 2, pos.y + 4 }, + direction = dirs.east, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 8, pos.y + 4 }, + direction = dirs.east, + force = game.forces.player, + }) + elseif anchor_dir == dirs.northwest then --3 + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 2, pos.y + 0 }, + direction = dirs.southeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 4, pos.y + 4 }, + direction = dirs.east, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 10, pos.y + 4 }, + direction = dirs.east, + force = game.forces.player, + }) end elseif dir == dirs.southeast then - if anchor_dir == dirs.northeast then--2 - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+4, pos.y+4}, direction = dirs.north, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+4, pos.y+8}, direction = dirs.north, force = game.forces.player} - elseif anchor_dir == dirs.southwest then--3 - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-0, pos.y+2}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x+4, pos.y+6}, direction = dirs.north, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+4, pos.y+10}, direction = dirs.north, force = game.forces.player} + if anchor_dir == dirs.northeast then --2 + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 4, pos.y + 4 }, + direction = dirs.north, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 4, pos.y + 8 }, + direction = dirs.north, + force = game.forces.player, + }) + elseif anchor_dir == dirs.southwest then --3 + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 0, pos.y + 2 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 4, pos.y + 6 }, + direction = dirs.north, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 4, pos.y + 10 }, + direction = dirs.north, + force = game.forces.player, + }) end elseif dir == dirs.northwest then - if anchor_dir == dirs.southwest then--2 - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x-2, pos.y-2}, direction = dirs.south, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-4, pos.y-8}, direction = dirs.north, force = game.forces.player} - elseif anchor_dir == dirs.northeast then--3 - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-0, pos.y-2}, direction = dirs.southwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail", position = {pos.x-2, pos.y-4}, direction = dirs.south, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-4, pos.y-10}, direction = dirs.north, force = game.forces.player} + if anchor_dir == dirs.southwest then --2 + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 2, pos.y - 2 }, + direction = dirs.south, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 4, pos.y - 8 }, + direction = dirs.north, + force = game.forces.player, + }) + elseif anchor_dir == dirs.northeast then --3 + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 0, pos.y - 2 }, + direction = dirs.southwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 2, pos.y - 4 }, + direction = dirs.south, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 4, pos.y - 10 }, + direction = dirs.north, + force = game.forces.player, + }) end end - - --4C. Check if can append forward + + --4C. Check if can append forward if include_forward then - if dir == dirs.north then - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-0, pos.y-2}, direction = dir, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-0, pos.y-4}, direction = dir, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-0, pos.y-6}, direction = dir, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-0, pos.y-8}, direction = dir, force = game.forces.player} - elseif dir == dirs.east then - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+2, pos.y-0}, direction = dir, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+4, pos.y-0}, direction = dir, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+6, pos.y-0}, direction = dir, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+8, pos.y-0}, direction = dir, force = game.forces.player} + if dir == dirs.north then + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 0, pos.y - 2 }, + direction = dir, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 0, pos.y - 4 }, + direction = dir, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 0, pos.y - 6 }, + direction = dir, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 0, pos.y - 8 }, + direction = dir, + force = game.forces.player, + }) + elseif dir == dirs.east then + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 2, pos.y - 0 }, + direction = dir, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 4, pos.y - 0 }, + direction = dir, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 6, pos.y - 0 }, + direction = dir, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 8, pos.y - 0 }, + direction = dir, + force = game.forces.player, + }) elseif dir == dirs.south then - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-0, pos.y+2}, direction = dir, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-0, pos.y+4}, direction = dir, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-0, pos.y+6}, direction = dir, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-0, pos.y+8}, direction = dir, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 0, pos.y + 2 }, + direction = dir, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 0, pos.y + 4 }, + direction = dir, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 0, pos.y + 6 }, + direction = dir, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 0, pos.y + 8 }, + direction = dir, + force = game.forces.player, + }) elseif dir == dirs.west then - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-2, pos.y-0}, direction = dir, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-4, pos.y-0}, direction = dir, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-6, pos.y-0}, direction = dir, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-8, pos.y-0}, direction = dir, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 2, pos.y - 0 }, + direction = dir, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 4, pos.y - 0 }, + direction = dir, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 6, pos.y - 0 }, + direction = dir, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 8, pos.y - 0 }, + direction = dir, + force = game.forces.player, + }) else - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout("Error: rail placement not defined", pindex) game.get_player(pindex).clear_cursor() return end end - + if not can_place_all then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout("Building area occupied.", pindex) game.get_player(pindex).clear_cursor() return end - + --5A. Build the rail entities to create the turn (LEFT) - if dir == dirs.north then - surf.create_entity{name = "curved-rail", position = {pos.x+0, pos.y-4}, direction = dirs.north, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-4, pos.y-8}, direction = dirs.northeast, force = game.forces.player} + if dir == dirs.north then + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 0, pos.y - 4 }, + direction = dirs.north, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 4, pos.y - 8 }, + direction = dirs.northeast, + force = game.forces.player, + }) elseif dir == dirs.east then - surf.create_entity{name = "curved-rail", position = {pos.x+6, pos.y+0}, direction = dirs.east, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+8, pos.y-4}, direction = dirs.southeast, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 6, pos.y + 0 }, + direction = dirs.east, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 8, pos.y - 4 }, + direction = dirs.southeast, + force = game.forces.player, + }) elseif dir == dirs.south then - surf.create_entity{name = "curved-rail", position = {pos.x+2, pos.y+6}, direction = dirs.south, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+4, pos.y+8}, direction = dirs.southwest, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 2, pos.y + 6 }, + direction = dirs.south, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 4, pos.y + 8 }, + direction = dirs.southwest, + force = game.forces.player, + }) elseif dir == dirs.west then - surf.create_entity{name = "curved-rail", position = {pos.x-4, pos.y+2}, direction = dirs.west, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-8, pos.y+4}, direction = dirs.northwest, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 4, pos.y + 2 }, + direction = dirs.west, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 8, pos.y + 4 }, + direction = dirs.northwest, + force = game.forces.player, + }) elseif dir == dirs.northeast then - if anchor_dir == dirs.southeast then--2 - surf.create_entity{name = "curved-rail", position = {pos.x+4, pos.y-2}, direction = dirs.southwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+4, pos.y-8}, direction = dirs.north, force = game.forces.player} - elseif anchor_dir == dirs.northwest then--3 - surf.create_entity{name = "straight-rail", position = {pos.x+0, pos.y-2}, direction = dirs.southeast, force = game.forces.player} - surf.create_entity{name = "curved-rail", position = {pos.x+4, pos.y-4}, direction = dirs.southwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+4, pos.y-10}, direction = dirs.north, force = game.forces.player} + if anchor_dir == dirs.southeast then --2 + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 4, pos.y - 2 }, + direction = dirs.southwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 4, pos.y - 8 }, + direction = dirs.north, + force = game.forces.player, + }) + elseif anchor_dir == dirs.northwest then --3 + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 0, pos.y - 2 }, + direction = dirs.southeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 4, pos.y - 4 }, + direction = dirs.southwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 4, pos.y - 10 }, + direction = dirs.north, + force = game.forces.player, + }) end elseif dir == dirs.southwest then - if anchor_dir == dirs.northwest then--2 - surf.create_entity{name = "curved-rail", position = {pos.x-2, pos.y+4}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-4, pos.y+8}, direction = dirs.north, force = game.forces.player} - elseif anchor_dir == dirs.southeast then--3 - surf.create_entity{name = "straight-rail", position = {pos.x-0, pos.y+2}, direction = dirs.northwest, force = game.forces.player} - surf.create_entity{name = "curved-rail", position = {pos.x-2, pos.y+6}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-4, pos.y+10}, direction = dirs.north, force = game.forces.player} + if anchor_dir == dirs.northwest then --2 + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 2, pos.y + 4 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 4, pos.y + 8 }, + direction = dirs.north, + force = game.forces.player, + }) + elseif anchor_dir == dirs.southeast then --3 + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 0, pos.y + 2 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 2, pos.y + 6 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 4, pos.y + 10 }, + direction = dirs.north, + force = game.forces.player, + }) end elseif dir == dirs.southeast then - if anchor_dir == dirs.southwest then--2 - surf.create_entity{name = "curved-rail", position = {pos.x+4, pos.y+4}, direction = dirs.northwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+8, pos.y+4}, direction = dirs.east, force = game.forces.player} - elseif anchor_dir == dirs.northeast then--3 - surf.create_entity{name = "straight-rail", position = {pos.x+2, pos.y+0}, direction = dirs.southwest, force = game.forces.player} - surf.create_entity{name = "curved-rail", position = {pos.x+6, pos.y+4}, direction = dirs.northwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+10, pos.y+4}, direction = dirs.east, force = game.forces.player} + if anchor_dir == dirs.southwest then --2 + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 4, pos.y + 4 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 8, pos.y + 4 }, + direction = dirs.east, + force = game.forces.player, + }) + elseif anchor_dir == dirs.northeast then --3 + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 2, pos.y + 0 }, + direction = dirs.southwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 6, pos.y + 4 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 10, pos.y + 4 }, + direction = dirs.east, + force = game.forces.player, + }) end elseif dir == dirs.northwest then - if anchor_dir == dirs.northeast then--2 - surf.create_entity{name = "curved-rail", position = {pos.x-2, pos.y-2}, direction = dirs.southeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-8, pos.y-4}, direction = dirs.east, force = game.forces.player} - elseif anchor_dir == dirs.southwest then--3 - surf.create_entity{name = "straight-rail", position = {pos.x-2, pos.y-0}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "curved-rail", position = {pos.x-4, pos.y-2}, direction = dirs.southeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-10, pos.y-4}, direction = dirs.east, force = game.forces.player} + if anchor_dir == dirs.northeast then --2 + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 2, pos.y - 2 }, + direction = dirs.southeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 8, pos.y - 4 }, + direction = dirs.east, + force = game.forces.player, + }) + elseif anchor_dir == dirs.southwest then --3 + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 2, pos.y - 0 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 4, pos.y - 2 }, + direction = dirs.southeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 10, pos.y - 4 }, + direction = dirs.east, + force = game.forces.player, + }) end end - + --5B. Build the rail entities to create the turn (RIGHT) - if dir == dirs.north then - surf.create_entity{name = "curved-rail", position = {pos.x+2, pos.y-4}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+4, pos.y-8}, direction = dirs.northwest, force = game.forces.player} + if dir == dirs.north then + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 2, pos.y - 4 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 4, pos.y - 8 }, + direction = dirs.northwest, + force = game.forces.player, + }) elseif dir == dirs.east then - surf.create_entity{name = "curved-rail", position = {pos.x+6, pos.y+2}, direction = dirs.southeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+8, pos.y+4}, direction = dirs.northeast, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 6, pos.y + 2 }, + direction = dirs.southeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 8, pos.y + 4 }, + direction = dirs.northeast, + force = game.forces.player, + }) elseif dir == dirs.south then - surf.create_entity{name = "curved-rail", position = {pos.x+0, pos.y+6}, direction = dirs.southwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-4, pos.y+8}, direction = dirs.southeast, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 0, pos.y + 6 }, + direction = dirs.southwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 4, pos.y + 8 }, + direction = dirs.southeast, + force = game.forces.player, + }) elseif dir == dirs.west then - surf.create_entity{name = "curved-rail", position = {pos.x-4, pos.y+0}, direction = dirs.northwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-8, pos.y-4}, direction = dirs.southwest, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 4, pos.y + 0 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 8, pos.y - 4 }, + direction = dirs.southwest, + force = game.forces.player, + }) elseif dir == dirs.northeast then if anchor_dir == dirs.northwest then - surf.create_entity{name = "curved-rail", position = {pos.x+4, pos.y-2}, direction = dirs.west, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+8, pos.y-4}, direction = dirs.east, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 4, pos.y - 2 }, + direction = dirs.west, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 8, pos.y - 4 }, + direction = dirs.east, + force = game.forces.player, + }) elseif anchor_dir == dirs.southeast then - surf.create_entity{name = "straight-rail", position = {pos.x+2, pos.y-0}, direction = dirs.northwest, force = game.forces.player} - surf.create_entity{name = "curved-rail", position = {pos.x+6, pos.y-2}, direction = dirs.west, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+10, pos.y-4}, direction = dirs.east, force = game.forces.player} + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 2, pos.y - 0 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 6, pos.y - 2 }, + direction = dirs.west, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 10, pos.y - 4 }, + direction = dirs.east, + force = game.forces.player, + }) end elseif dir == dirs.southwest then - if anchor_dir == dirs.southeast then--2 - surf.create_entity{name = "curved-rail", position = {pos.x-2, pos.y+4}, direction = dirs.east, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-8, pos.y+4}, direction = dirs.east, force = game.forces.player} - elseif anchor_dir == dirs.northwest then--3 - surf.create_entity{name = "straight-rail", position = {pos.x-2, pos.y+0}, direction = dirs.southeast, force = game.forces.player} - surf.create_entity{name = "curved-rail", position = {pos.x-4, pos.y+4}, direction = dirs.east, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-10, pos.y+4}, direction = dirs.east, force = game.forces.player} + if anchor_dir == dirs.southeast then --2 + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 2, pos.y + 4 }, + direction = dirs.east, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 8, pos.y + 4 }, + direction = dirs.east, + force = game.forces.player, + }) + elseif anchor_dir == dirs.northwest then --3 + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 2, pos.y + 0 }, + direction = dirs.southeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 4, pos.y + 4 }, + direction = dirs.east, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 10, pos.y + 4 }, + direction = dirs.east, + force = game.forces.player, + }) end elseif dir == dirs.southeast then - if anchor_dir == dirs.northeast then--2 - surf.create_entity{name = "curved-rail", position = {pos.x+4, pos.y+4}, direction = dirs.north, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+4, pos.y+8}, direction = dirs.north, force = game.forces.player} - elseif anchor_dir == dirs.southwest then--3 - surf.create_entity{name = "straight-rail", position = {pos.x-0, pos.y+2}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "curved-rail", position = {pos.x+4, pos.y+6}, direction = dirs.north, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+4, pos.y+10}, direction = dirs.north, force = game.forces.player} + if anchor_dir == dirs.northeast then --2 + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 4, pos.y + 4 }, + direction = dirs.north, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 4, pos.y + 8 }, + direction = dirs.north, + force = game.forces.player, + }) + elseif anchor_dir == dirs.southwest then --3 + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 0, pos.y + 2 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 4, pos.y + 6 }, + direction = dirs.north, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 4, pos.y + 10 }, + direction = dirs.north, + force = game.forces.player, + }) end elseif dir == dirs.northwest then - if anchor_dir == dirs.southwest then--2 - surf.create_entity{name = "curved-rail", position = {pos.x-2, pos.y-2}, direction = dirs.south, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-4, pos.y-8}, direction = dirs.north, force = game.forces.player} - elseif anchor_dir == dirs.northeast then--3 - surf.create_entity{name = "straight-rail", position = {pos.x-0, pos.y-2}, direction = dirs.southwest, force = game.forces.player} - surf.create_entity{name = "curved-rail", position = {pos.x-2, pos.y-4}, direction = dirs.south, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-4, pos.y-10}, direction = dirs.north, force = game.forces.player} + if anchor_dir == dirs.southwest then --2 + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 2, pos.y - 2 }, + direction = dirs.south, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 4, pos.y - 8 }, + direction = dirs.north, + force = game.forces.player, + }) + elseif anchor_dir == dirs.northeast then --3 + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 0, pos.y - 2 }, + direction = dirs.southwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 2, pos.y - 4 }, + direction = dirs.south, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 4, pos.y - 10 }, + direction = dirs.north, + force = game.forces.player, + }) end end - + --5C. Add Forward section if include_forward then - if dir == dirs.north then - surf.create_entity{name = "straight-rail", position = {pos.x-0, pos.y-2}, direction = dir, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-0, pos.y-4}, direction = dir, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-0, pos.y-6}, direction = dir, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-0, pos.y-8}, direction = dir, force = game.forces.player} - elseif dir == dirs.east then - surf.create_entity{name = "straight-rail", position = {pos.x+2, pos.y-0}, direction = dir, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+4, pos.y-0}, direction = dir, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+6, pos.y-0}, direction = dir, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+8, pos.y-0}, direction = dir, force = game.forces.player} + if dir == dirs.north then + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 0, pos.y - 2 }, + direction = dir, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 0, pos.y - 4 }, + direction = dir, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 0, pos.y - 6 }, + direction = dir, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 0, pos.y - 8 }, + direction = dir, + force = game.forces.player, + }) + elseif dir == dirs.east then + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 2, pos.y - 0 }, + direction = dir, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 4, pos.y - 0 }, + direction = dir, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 6, pos.y - 0 }, + direction = dir, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 8, pos.y - 0 }, + direction = dir, + force = game.forces.player, + }) elseif dir == dirs.south then - surf.create_entity{name = "straight-rail", position = {pos.x-0, pos.y+2}, direction = dir, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-0, pos.y+4}, direction = dir, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-0, pos.y+6}, direction = dir, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-0, pos.y+8}, direction = dir, force = game.forces.player} + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 0, pos.y + 2 }, + direction = dir, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 0, pos.y + 4 }, + direction = dir, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 0, pos.y + 6 }, + direction = dir, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 0, pos.y + 8 }, + direction = dir, + force = game.forces.player, + }) elseif dir == dirs.west then - surf.create_entity{name = "straight-rail", position = {pos.x-2, pos.y-0}, direction = dir, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-4, pos.y-0}, direction = dir, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-6, pos.y-0}, direction = dir, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-8, pos.y-0}, direction = dir, force = game.forces.player} + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 2, pos.y - 0 }, + direction = dir, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 4, pos.y - 0 }, + direction = dir, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 6, pos.y - 0 }, + direction = dir, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 8, pos.y - 0 }, + direction = dir, + force = game.forces.player, + }) else - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout("Error: rail placement not defined", pindex) game.get_player(pindex).clear_cursor() return end end - + --6 Remove rail units from the player's hand game.get_player(pindex).cursor_stack.count = game.get_player(pindex).cursor_stack.count - 5 game.get_player(pindex).clear_cursor() - + --7. Sounds and results - game.get_player(pindex).play_sound{path = "entity-build/straight-rail"} - game.get_player(pindex).play_sound{path = "entity-build/curved-rail"} + game.get_player(pindex).play_sound({ path = "entity-build/straight-rail" }) + game.get_player(pindex).play_sound({ path = "entity-build/curved-rail" }) local result = "Rail fork built with 2 exits, " .. build_comment - if include_forward then - result = "Rail fork built with 3 exits, " .. build_comment - end - printout(result,pindex) + if include_forward then result = "Rail fork built with 3 exits, " .. build_comment end + printout(result, pindex) return - end --Builds a starter for a rail bypass junction with 2 rails @@ -1227,12 +3003,12 @@ function mod.build_rail_bypass_junction(anchor_rail, pindex) local can_place_all = true local is_end_rail local anchor_dir = anchor_rail.direction - - --1A. Firstly, check if the player has enough rails to place this (20 units) + + --1A. Firstly, check if the player has enough rails to place this (20 units) if not (stack.valid and stack.valid_for_read and stack.name == "rail" and stack.count >= 20) then --Check if the inventory has enough if players[pindex].inventory.lua_inventory.get_item_count("rail") < 20 then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout("You need at least 20 rails in your inventory to build this.", pindex) return else @@ -1243,211 +3019,826 @@ function mod.build_rail_bypass_junction(anchor_rail, pindex) players[pindex].inventory.max = #players[pindex].inventory.lua_inventory end end - - --1B. Check if the player has enough rail signals to place this (4 units) + + --1B. Check if the player has enough rail signals to place this (4 units) if not (stack.valid and stack.valid_for_read and stack.name == "rail-chain-signal" and stack.count >= 4) then --Check if the inventory has enough if players[pindex].inventory.lua_inventory.get_item_count("rail-chain-signal") < 4 then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout("You need at least 4 rail chain signals in your inventory to build this.", pindex) return else --Good to go. end end - + --2. Secondly, verify the end rail and find its direction - is_end_rail, dir, build_comment = fa_rails.check_end_rail(anchor_rail,pindex) + is_end_rail, dir, build_comment = fa_rails.check_end_rail(anchor_rail, pindex) if not is_end_rail then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout(build_comment, pindex) game.get_player(pindex).clear_cursor() return end pos = anchor_rail.position - + --3. Clear trees and rocks in the build area, can be tuned later... - temp1, build_comment = fa_mining_tools.clear_obstacles_in_circle(pos,21,pindex) - + temp1, build_comment = fa_mining_tools.clear_obstacles_in_circle(pos, 21, pindex) + --4A. Check if every object can be placed (LEFT) - if dir == dirs.north then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x-00, pos.y-04}, direction = dirs.north, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-04, pos.y-08}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-04, pos.y-10}, direction = dirs.southwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x-06, pos.y-12}, direction = dirs.south, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-08, pos.y-18}, direction = dirs.north, force = game.forces.player} + if dir == dirs.north then + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 00, pos.y - 04 }, + direction = dirs.north, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 04, pos.y - 08 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 04, pos.y - 10 }, + direction = dirs.southwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 06, pos.y - 12 }, + direction = dirs.south, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 08, pos.y - 18 }, + direction = dirs.north, + force = game.forces.player, + }) elseif dir == dirs.east then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x+06, pos.y-00}, direction = dirs.east, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+08, pos.y-04}, direction = dirs.southeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+10, pos.y-04}, direction = dirs.northwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x+14, pos.y-06}, direction = dirs.west, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+18, pos.y-08}, direction = dirs.east, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 06, pos.y - 00 }, + direction = dirs.east, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 08, pos.y - 04 }, + direction = dirs.southeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 10, pos.y - 04 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 14, pos.y - 06 }, + direction = dirs.west, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 18, pos.y - 08 }, + direction = dirs.east, + force = game.forces.player, + }) elseif dir == dirs.south then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x+02, pos.y+06}, direction = dirs.south, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+04, pos.y+08}, direction = dirs.southwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+04, pos.y+10}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x+08, pos.y+14}, direction = dirs.north, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+08, pos.y+18}, direction = dirs.north, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 02, pos.y + 06 }, + direction = dirs.south, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 04, pos.y + 08 }, + direction = dirs.southwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 04, pos.y + 10 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 08, pos.y + 14 }, + direction = dirs.north, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 08, pos.y + 18 }, + direction = dirs.north, + force = game.forces.player, + }) elseif dir == dirs.west then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x-04, pos.y+02}, direction = dirs.west, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-08, pos.y+04}, direction = dirs.northwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-10, pos.y+04}, direction = dirs.southeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x-12, pos.y+08}, direction = dirs.east, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-18, pos.y+08}, direction = dirs.west, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 04, pos.y + 02 }, + direction = dirs.west, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 08, pos.y + 04 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 10, pos.y + 04 }, + direction = dirs.southeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 12, pos.y + 08 }, + direction = dirs.east, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 18, pos.y + 08 }, + direction = dirs.west, + force = game.forces.player, + }) end - + --4B. Check if every object can be placed (RIGHT) - if dir == dirs.north then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x+02, pos.y-04}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+04, pos.y-08}, direction = dirs.northwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+04, pos.y-10}, direction = dirs.southeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x+08, pos.y-12}, direction = dirs.southwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+08, pos.y-18}, direction = dirs.north, force = game.forces.player} + if dir == dirs.north then + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 02, pos.y - 04 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 04, pos.y - 08 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 04, pos.y - 10 }, + direction = dirs.southeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 08, pos.y - 12 }, + direction = dirs.southwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 08, pos.y - 18 }, + direction = dirs.north, + force = game.forces.player, + }) elseif dir == dirs.east then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x+06, pos.y+02}, direction = dirs.southeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+08, pos.y+04}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+10, pos.y+04}, direction = dirs.southwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x+14, pos.y+08}, direction = dirs.northwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+18, pos.y+08}, direction = dirs.east, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 06, pos.y + 02 }, + direction = dirs.southeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 08, pos.y + 04 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 10, pos.y + 04 }, + direction = dirs.southwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 14, pos.y + 08 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 18, pos.y + 08 }, + direction = dirs.east, + force = game.forces.player, + }) elseif dir == dirs.south then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x-00, pos.y+06}, direction = dirs.southwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-04, pos.y+08}, direction = dirs.southeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-04, pos.y+10}, direction = dirs.northwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x-06, pos.y+14}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-08, pos.y+18}, direction = dirs.south, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 00, pos.y + 06 }, + direction = dirs.southwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 04, pos.y + 08 }, + direction = dirs.southeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 04, pos.y + 10 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 06, pos.y + 14 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 08, pos.y + 18 }, + direction = dirs.south, + force = game.forces.player, + }) elseif dir == dirs.west then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x-04, pos.y-00}, direction = dirs.northwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-08, pos.y-04}, direction = dirs.southwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-10, pos.y-04}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x-12, pos.y-06}, direction = dirs.southeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-18, pos.y-08}, direction = dirs.west, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 04, pos.y - 00 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 08, pos.y - 04 }, + direction = dirs.southwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 10, pos.y - 04 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 12, pos.y - 06 }, + direction = dirs.southeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 18, pos.y - 08 }, + direction = dirs.west, + force = game.forces.player, + }) end - + --4C. Check if every object can be placed (SIGNALS) - if dir == dirs.north then - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x+01, pos.y-00}, direction = dirs.south, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x-02, pos.y-00}, direction = dirs.north, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-signal" , position = {pos.x+03, pos.y-07}, direction = dirs.southwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x-04, pos.y-07}, direction = dirs.northwest, force = game.forces.player} + if dir == dirs.north then + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x + 01, pos.y - 00 }, + direction = dirs.south, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x - 02, pos.y - 00 }, + direction = dirs.north, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-signal", + position = { pos.x + 03, pos.y - 07 }, + direction = dirs.southwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x - 04, pos.y - 07 }, + direction = dirs.northwest, + force = game.forces.player, + }) elseif dir == dirs.east then - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x-01, pos.y+01}, direction = dirs.west , force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x-01, pos.y-02}, direction = dirs.east , force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-signal" , position = {pos.x+06, pos.y+03}, direction = dirs.northwest , force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x+06, pos.y-04}, direction = dirs.northeast , force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x - 01, pos.y + 01 }, + direction = dirs.west, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x - 01, pos.y - 02 }, + direction = dirs.east, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-signal", + position = { pos.x + 06, pos.y + 03 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x + 06, pos.y - 04 }, + direction = dirs.northeast, + force = game.forces.player, + }) elseif dir == dirs.south then - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x-02, pos.y-00}, direction = dirs.north, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x+01, pos.y-00}, direction = dirs.south, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-signal" , position = {pos.x-04, pos.y+06}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x+03, pos.y+06}, direction = dirs.southeast, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x - 02, pos.y - 00 }, + direction = dirs.north, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x + 01, pos.y - 00 }, + direction = dirs.south, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-signal", + position = { pos.x - 04, pos.y + 06 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x + 03, pos.y + 06 }, + direction = dirs.southeast, + force = game.forces.player, + }) elseif dir == dirs.west then - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x-00, pos.y-02}, direction = dirs.east , force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x-00, pos.y+01}, direction = dirs.west , force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-signal" , position = {pos.x-07, pos.y-04}, direction = dirs.southeast , force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x-07, pos.y+03}, direction = dirs.southwest , force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x - 00, pos.y - 02 }, + direction = dirs.east, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x - 00, pos.y + 01 }, + direction = dirs.west, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-signal", + position = { pos.x - 07, pos.y - 04 }, + direction = dirs.southeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x - 07, pos.y + 03 }, + direction = dirs.southwest, + force = game.forces.player, + }) end - + if dir == dirs.northeast or dir == dirs.northwest or dir == dirs.southeast or dir == dirs.southwest then can_place_all = false end - + if not can_place_all then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout("Building area occupied.", pindex) game.get_player(pindex).clear_cursor() return end - + --5A. Build the rail entities to create the turn (LEFT) - if dir == dirs.north then - surf.create_entity{name = "curved-rail" , position = {pos.x-00, pos.y-04}, direction = dirs.north, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-04, pos.y-08}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-04, pos.y-10}, direction = dirs.southwest, force = game.forces.player} - surf.create_entity{name = "curved-rail" , position = {pos.x-06, pos.y-12}, direction = dirs.south, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-08, pos.y-18}, direction = dirs.north, force = game.forces.player} + if dir == dirs.north then + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 00, pos.y - 04 }, + direction = dirs.north, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 04, pos.y - 08 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 04, pos.y - 10 }, + direction = dirs.southwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 06, pos.y - 12 }, + direction = dirs.south, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 08, pos.y - 18 }, + direction = dirs.north, + force = game.forces.player, + }) elseif dir == dirs.east then - surf.create_entity{name = "curved-rail" , position = {pos.x+06, pos.y-00}, direction = dirs.east, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+08, pos.y-04}, direction = dirs.southeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+10, pos.y-04}, direction = dirs.northwest, force = game.forces.player} - surf.create_entity{name = "curved-rail" , position = {pos.x+14, pos.y-06}, direction = dirs.west, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+18, pos.y-08}, direction = dirs.east, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 06, pos.y - 00 }, + direction = dirs.east, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 08, pos.y - 04 }, + direction = dirs.southeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 10, pos.y - 04 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 14, pos.y - 06 }, + direction = dirs.west, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 18, pos.y - 08 }, + direction = dirs.east, + force = game.forces.player, + }) elseif dir == dirs.south then - surf.create_entity{name = "curved-rail" , position = {pos.x+02, pos.y+06}, direction = dirs.south, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+04, pos.y+08}, direction = dirs.southwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+04, pos.y+10}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "curved-rail" , position = {pos.x+08, pos.y+14}, direction = dirs.north, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+08, pos.y+18}, direction = dirs.north, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 02, pos.y + 06 }, + direction = dirs.south, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 04, pos.y + 08 }, + direction = dirs.southwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 04, pos.y + 10 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 08, pos.y + 14 }, + direction = dirs.north, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 08, pos.y + 18 }, + direction = dirs.north, + force = game.forces.player, + }) elseif dir == dirs.west then - surf.create_entity{name = "curved-rail" , position = {pos.x-04, pos.y+02}, direction = dirs.west, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-08, pos.y+04}, direction = dirs.northwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-10, pos.y+04}, direction = dirs.southeast, force = game.forces.player} - surf.create_entity{name = "curved-rail" , position = {pos.x-12, pos.y+08}, direction = dirs.east, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-18, pos.y+08}, direction = dirs.west, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 04, pos.y + 02 }, + direction = dirs.west, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 08, pos.y + 04 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 10, pos.y + 04 }, + direction = dirs.southeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 12, pos.y + 08 }, + direction = dirs.east, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 18, pos.y + 08 }, + direction = dirs.west, + force = game.forces.player, + }) end - + --5B. Build the rail entities to create the turn (RIGHT) - if dir == dirs.north then - surf.create_entity{name = "curved-rail" , position = {pos.x+02, pos.y-04}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+04, pos.y-08}, direction = dirs.northwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+04, pos.y-10}, direction = dirs.southeast, force = game.forces.player} - surf.create_entity{name = "curved-rail" , position = {pos.x+08, pos.y-12}, direction = dirs.southwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+08, pos.y-18}, direction = dirs.north, force = game.forces.player} + if dir == dirs.north then + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 02, pos.y - 04 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 04, pos.y - 08 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 04, pos.y - 10 }, + direction = dirs.southeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 08, pos.y - 12 }, + direction = dirs.southwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 08, pos.y - 18 }, + direction = dirs.north, + force = game.forces.player, + }) elseif dir == dirs.east then - surf.create_entity{name = "curved-rail" , position = {pos.x+06, pos.y+02}, direction = dirs.southeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+08, pos.y+04}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+10, pos.y+04}, direction = dirs.southwest, force = game.forces.player} - surf.create_entity{name = "curved-rail" , position = {pos.x+14, pos.y+08}, direction = dirs.northwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+18, pos.y+08}, direction = dirs.east, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 06, pos.y + 02 }, + direction = dirs.southeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 08, pos.y + 04 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 10, pos.y + 04 }, + direction = dirs.southwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 14, pos.y + 08 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 18, pos.y + 08 }, + direction = dirs.east, + force = game.forces.player, + }) elseif dir == dirs.south then - surf.create_entity{name = "curved-rail" , position = {pos.x-00, pos.y+06}, direction = dirs.southwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-04, pos.y+08}, direction = dirs.southeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-04, pos.y+10}, direction = dirs.northwest, force = game.forces.player} - surf.create_entity{name = "curved-rail" , position = {pos.x-06, pos.y+14}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-08, pos.y+18}, direction = dirs.south, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 00, pos.y + 06 }, + direction = dirs.southwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 04, pos.y + 08 }, + direction = dirs.southeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 04, pos.y + 10 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 06, pos.y + 14 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 08, pos.y + 18 }, + direction = dirs.south, + force = game.forces.player, + }) elseif dir == dirs.west then - surf.create_entity{name = "curved-rail" , position = {pos.x-04, pos.y-00}, direction = dirs.northwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-08, pos.y-04}, direction = dirs.southwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-10, pos.y-04}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "curved-rail" , position = {pos.x-12, pos.y-06}, direction = dirs.southeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-18, pos.y-08}, direction = dirs.west, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 04, pos.y - 00 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 08, pos.y - 04 }, + direction = dirs.southwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 10, pos.y - 04 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 12, pos.y - 06 }, + direction = dirs.southeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 18, pos.y - 08 }, + direction = dirs.west, + force = game.forces.player, + }) end - + --5C. Place rail signals (4) - if dir == dirs.north then - surf.create_entity{name = "rail-chain-signal", position = {pos.x+01, pos.y-00}, direction = dirs.south, force = game.forces.player} - surf.create_entity{name = "rail-chain-signal", position = {pos.x-02, pos.y-00}, direction = dirs.north, force = game.forces.player} - surf.create_entity{name = "rail-signal" , position = {pos.x+03, pos.y-07}, direction = dirs.southwest, force = game.forces.player} - surf.create_entity{name = "rail-chain-signal", position = {pos.x-04, pos.y-07}, direction = dirs.northwest, force = game.forces.player} + if dir == dirs.north then + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x + 01, pos.y - 00 }, + direction = dirs.south, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x - 02, pos.y - 00 }, + direction = dirs.north, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-signal", + position = { pos.x + 03, pos.y - 07 }, + direction = dirs.southwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x - 04, pos.y - 07 }, + direction = dirs.northwest, + force = game.forces.player, + }) elseif dir == dirs.east then - surf.create_entity{name = "rail-chain-signal", position = {pos.x-01, pos.y+01}, direction = dirs.west , force = game.forces.player} - surf.create_entity{name = "rail-chain-signal", position = {pos.x-01, pos.y-02}, direction = dirs.east , force = game.forces.player} - surf.create_entity{name = "rail-signal" , position = {pos.x+06, pos.y+03}, direction = dirs.northwest , force = game.forces.player} - surf.create_entity{name = "rail-chain-signal", position = {pos.x+06, pos.y-04}, direction = dirs.northeast , force = game.forces.player} + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x - 01, pos.y + 01 }, + direction = dirs.west, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x - 01, pos.y - 02 }, + direction = dirs.east, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-signal", + position = { pos.x + 06, pos.y + 03 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x + 06, pos.y - 04 }, + direction = dirs.northeast, + force = game.forces.player, + }) elseif dir == dirs.south then - surf.create_entity{name = "rail-chain-signal", position = {pos.x-02, pos.y-00}, direction = dirs.north, force = game.forces.player} - surf.create_entity{name = "rail-chain-signal", position = {pos.x+01, pos.y-00}, direction = dirs.south, force = game.forces.player} - surf.create_entity{name = "rail-signal" , position = {pos.x-04, pos.y+06}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "rail-chain-signal", position = {pos.x+03, pos.y+06}, direction = dirs.southeast, force = game.forces.player} + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x - 02, pos.y - 00 }, + direction = dirs.north, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x + 01, pos.y - 00 }, + direction = dirs.south, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-signal", + position = { pos.x - 04, pos.y + 06 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x + 03, pos.y + 06 }, + direction = dirs.southeast, + force = game.forces.player, + }) elseif dir == dirs.west then - surf.create_entity{name = "rail-chain-signal", position = {pos.x-00, pos.y-02}, direction = dirs.east , force = game.forces.player} - surf.create_entity{name = "rail-chain-signal", position = {pos.x-00, pos.y+01}, direction = dirs.west , force = game.forces.player} - surf.create_entity{name = "rail-signal" , position = {pos.x-07, pos.y-04}, direction = dirs.southeast , force = game.forces.player} - surf.create_entity{name = "rail-chain-signal", position = {pos.x-07, pos.y+03}, direction = dirs.southwest , force = game.forces.player} + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x - 00, pos.y - 02 }, + direction = dirs.east, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x - 00, pos.y + 01 }, + direction = dirs.west, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-signal", + position = { pos.x - 07, pos.y - 04 }, + direction = dirs.southeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x - 07, pos.y + 03 }, + direction = dirs.southwest, + force = game.forces.player, + }) end - + --6 Remove rail units from the player's hand game.get_player(pindex).cursor_stack.count = game.get_player(pindex).cursor_stack.count - 20 game.get_player(pindex).clear_cursor() - game.get_player(pindex).get_main_inventory().remove({name="rail-chain-signal", count=4}) - + game.get_player(pindex).get_main_inventory().remove({ name = "rail-chain-signal", count = 4 }) + --7. Sounds and results - game.get_player(pindex).play_sound{path = "entity-build/straight-rail"} - game.get_player(pindex).play_sound{path = "entity-build/curved-rail"} - game.get_player(pindex).play_sound{path = "entity-build/straight-rail"} - game.get_player(pindex).play_sound{path = "entity-build/curved-rail"} + game.get_player(pindex).play_sound({ path = "entity-build/straight-rail" }) + game.get_player(pindex).play_sound({ path = "entity-build/curved-rail" }) + game.get_player(pindex).play_sound({ path = "entity-build/straight-rail" }) + game.get_player(pindex).play_sound({ path = "entity-build/curved-rail" }) local result = "Rail bypass junction built, " .. build_comment - printout(result,pindex) + printout(result, pindex) return - end --Builds a starter for a rail bypass junction with 3 rails ***todo complete and test @@ -1462,12 +3853,12 @@ function mod.build_rail_bypass_junction_triple(anchor_rail, pindex) local can_place_all = true local is_end_rail local anchor_dir = anchor_rail.direction - - --1A. Firstly, check if the player has enough rails to place this (25 units) + + --1A. Firstly, check if the player has enough rails to place this (25 units) if not (stack.valid and stack.valid_for_read and stack.name == "rail" and stack.count >= 25) then --Check if the inventory has enough if players[pindex].inventory.lua_inventory.get_item_count("rail") < 25 then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout("You need at least 25 rails in your inventory to build this.", pindex) return else @@ -1478,237 +3869,904 @@ function mod.build_rail_bypass_junction_triple(anchor_rail, pindex) players[pindex].inventory.max = #players[pindex].inventory.lua_inventory end end - - --1B. Check if the player has enough rail signals to place this (6 units) + + --1B. Check if the player has enough rail signals to place this (6 units) if not (stack.valid and stack.valid_for_read and stack.name == "rail-chain-signal" and stack.count >= 6) then --Check if the inventory has enough if players[pindex].inventory.lua_inventory.get_item_count("rail-chain-signal") < 6 then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout("You need at least 6 rail chain signals in your inventory to build this.", pindex) return else --Good to go. end end - + --2. Secondly, verify the end rail and find its direction - is_end_rail, dir, build_comment = fa_rails.check_end_rail(anchor_rail,pindex) + is_end_rail, dir, build_comment = fa_rails.check_end_rail(anchor_rail, pindex) if not is_end_rail then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout(build_comment, pindex) game.get_player(pindex).clear_cursor() return end pos = anchor_rail.position - + --3. Clear trees and rocks in the build area, can be tuned later... - temp1, build_comment = fa_mining_tools.clear_obstacles_in_circle(pos,21,pindex) - + temp1, build_comment = fa_mining_tools.clear_obstacles_in_circle(pos, 21, pindex) + --4A. Check if every object can be placed (LEFT) - if dir == dirs.north then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x-00, pos.y-04}, direction = dirs.north, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-04, pos.y-08}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-04, pos.y-10}, direction = dirs.southwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x-06, pos.y-12}, direction = dirs.south, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-08, pos.y-18}, direction = dirs.north, force = game.forces.player} + if dir == dirs.north then + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 00, pos.y - 04 }, + direction = dirs.north, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 04, pos.y - 08 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 04, pos.y - 10 }, + direction = dirs.southwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 06, pos.y - 12 }, + direction = dirs.south, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 08, pos.y - 18 }, + direction = dirs.north, + force = game.forces.player, + }) elseif dir == dirs.east then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x+06, pos.y-00}, direction = dirs.east, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+08, pos.y-04}, direction = dirs.southeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+10, pos.y-04}, direction = dirs.northwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x+14, pos.y-06}, direction = dirs.west, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+18, pos.y-08}, direction = dirs.east, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 06, pos.y - 00 }, + direction = dirs.east, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 08, pos.y - 04 }, + direction = dirs.southeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 10, pos.y - 04 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 14, pos.y - 06 }, + direction = dirs.west, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 18, pos.y - 08 }, + direction = dirs.east, + force = game.forces.player, + }) elseif dir == dirs.south then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x+02, pos.y+06}, direction = dirs.south, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+04, pos.y+08}, direction = dirs.southwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+04, pos.y+10}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x+08, pos.y+14}, direction = dirs.north, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+08, pos.y+18}, direction = dirs.north, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 02, pos.y + 06 }, + direction = dirs.south, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 04, pos.y + 08 }, + direction = dirs.southwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 04, pos.y + 10 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 08, pos.y + 14 }, + direction = dirs.north, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 08, pos.y + 18 }, + direction = dirs.north, + force = game.forces.player, + }) elseif dir == dirs.west then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x-04, pos.y+02}, direction = dirs.west, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-08, pos.y+04}, direction = dirs.northwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-10, pos.y+04}, direction = dirs.southeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x-12, pos.y+08}, direction = dirs.east, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-18, pos.y+08}, direction = dirs.west, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 04, pos.y + 02 }, + direction = dirs.west, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 08, pos.y + 04 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 10, pos.y + 04 }, + direction = dirs.southeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 12, pos.y + 08 }, + direction = dirs.east, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 18, pos.y + 08 }, + direction = dirs.west, + force = game.forces.player, + }) end - + --4B. Check if every object can be placed (RIGHT) - if dir == dirs.north then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x+02, pos.y-04}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+04, pos.y-08}, direction = dirs.northwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+04, pos.y-10}, direction = dirs.southeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x+08, pos.y-12}, direction = dirs.southwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+08, pos.y-18}, direction = dirs.north, force = game.forces.player} + if dir == dirs.north then + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 02, pos.y - 04 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 04, pos.y - 08 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 04, pos.y - 10 }, + direction = dirs.southeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 08, pos.y - 12 }, + direction = dirs.southwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 08, pos.y - 18 }, + direction = dirs.north, + force = game.forces.player, + }) elseif dir == dirs.east then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x+06, pos.y+02}, direction = dirs.southeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+08, pos.y+04}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+10, pos.y+04}, direction = dirs.southwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x+14, pos.y+08}, direction = dirs.northwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x+18, pos.y+08}, direction = dirs.east, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 06, pos.y + 02 }, + direction = dirs.southeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 08, pos.y + 04 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 10, pos.y + 04 }, + direction = dirs.southwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x + 14, pos.y + 08 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 18, pos.y + 08 }, + direction = dirs.east, + force = game.forces.player, + }) elseif dir == dirs.south then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x-00, pos.y+06}, direction = dirs.southwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-04, pos.y+08}, direction = dirs.southeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-04, pos.y+10}, direction = dirs.northwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x-06, pos.y+14}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-08, pos.y+18}, direction = dirs.south, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 00, pos.y + 06 }, + direction = dirs.southwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 04, pos.y + 08 }, + direction = dirs.southeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 04, pos.y + 10 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 06, pos.y + 14 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 08, pos.y + 18 }, + direction = dirs.south, + force = game.forces.player, + }) elseif dir == dirs.west then - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x-04, pos.y-00}, direction = dirs.northwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-08, pos.y-04}, direction = dirs.southwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-10, pos.y-04}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "curved-rail" , position = {pos.x-12, pos.y-06}, direction = dirs.southeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "straight-rail", position = {pos.x-18, pos.y-08}, direction = dirs.west, force = game.forces.player} - end - - --4C. Check if every object can be placed (MIDDLE) todo*** also be okay with there already being straight rails here - if dir == dirs.north then - can_place_all = can_place_all and (surf.can_place_entity{name = "straight-rail", position = {pos.x+08, pos.y-18}, direction = dirs.north, force = game.forces.player} or true) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 04, pos.y - 00 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 08, pos.y - 04 }, + direction = dirs.southwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 10, pos.y - 04 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "curved-rail", + position = { pos.x - 12, pos.y - 06 }, + direction = dirs.southeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 18, pos.y - 08 }, + direction = dirs.west, + force = game.forces.player, + }) + end + + --4C. Check if every object can be placed (MIDDLE) todo*** also be okay with there already being straight rails here + if dir == dirs.north then + can_place_all = can_place_all + and ( + surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 08, pos.y - 18 }, + direction = dirs.north, + force = game.forces.player, + }) or true + ) elseif dir == dirs.east then - can_place_all = can_place_all and (surf.can_place_entity{name = "straight-rail", position = {pos.x+18, pos.y+08}, direction = dirs.east, force = game.forces.player} or true) + can_place_all = can_place_all + and ( + surf.can_place_entity({ + name = "straight-rail", + position = { pos.x + 18, pos.y + 08 }, + direction = dirs.east, + force = game.forces.player, + }) or true + ) elseif dir == dirs.south then - can_place_all = can_place_all and (surf.can_place_entity{name = "straight-rail", position = {pos.x-08, pos.y+18}, direction = dirs.south, force = game.forces.player} or true) + can_place_all = can_place_all + and ( + surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 08, pos.y + 18 }, + direction = dirs.south, + force = game.forces.player, + }) or true + ) elseif dir == dirs.west then - can_place_all = can_place_all and (surf.can_place_entity{name = "straight-rail", position = {pos.x-18, pos.y-08}, direction = dirs.west, force = game.forces.player} or true) + can_place_all = can_place_all + and ( + surf.can_place_entity({ + name = "straight-rail", + position = { pos.x - 18, pos.y - 08 }, + direction = dirs.west, + force = game.forces.player, + }) or true + ) end - + --4D. Check if every object can be placed (SIGNALS) todo *** - if dir == dirs.north then - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x+01, pos.y-00}, direction = dirs.south, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x-02, pos.y-00}, direction = dirs.north, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-signal" , position = {pos.x+03, pos.y-07}, direction = dirs.southwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x-04, pos.y-07}, direction = dirs.northwest, force = game.forces.player} + if dir == dirs.north then + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x + 01, pos.y - 00 }, + direction = dirs.south, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x - 02, pos.y - 00 }, + direction = dirs.north, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-signal", + position = { pos.x + 03, pos.y - 07 }, + direction = dirs.southwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x - 04, pos.y - 07 }, + direction = dirs.northwest, + force = game.forces.player, + }) elseif dir == dirs.east then - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x-01, pos.y+01}, direction = dirs.west , force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x-01, pos.y-02}, direction = dirs.east , force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-signal" , position = {pos.x+06, pos.y+03}, direction = dirs.northwest , force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x+06, pos.y-04}, direction = dirs.northeast , force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x - 01, pos.y + 01 }, + direction = dirs.west, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x - 01, pos.y - 02 }, + direction = dirs.east, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-signal", + position = { pos.x + 06, pos.y + 03 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x + 06, pos.y - 04 }, + direction = dirs.northeast, + force = game.forces.player, + }) elseif dir == dirs.south then - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x-02, pos.y-00}, direction = dirs.north, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x+01, pos.y-00}, direction = dirs.south, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-signal" , position = {pos.x-04, pos.y+06}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x+03, pos.y+06}, direction = dirs.southeast, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x - 02, pos.y - 00 }, + direction = dirs.north, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x + 01, pos.y - 00 }, + direction = dirs.south, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-signal", + position = { pos.x - 04, pos.y + 06 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x + 03, pos.y + 06 }, + direction = dirs.southeast, + force = game.forces.player, + }) elseif dir == dirs.west then - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x-00, pos.y-02}, direction = dirs.east , force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x-00, pos.y+01}, direction = dirs.west , force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-signal" , position = {pos.x-07, pos.y-04}, direction = dirs.southeast , force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x-07, pos.y+03}, direction = dirs.southwest , force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x - 00, pos.y - 02 }, + direction = dirs.east, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x - 00, pos.y + 01 }, + direction = dirs.west, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-signal", + position = { pos.x - 07, pos.y - 04 }, + direction = dirs.southeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x - 07, pos.y + 03 }, + direction = dirs.southwest, + force = game.forces.player, + }) end - + if dir == dirs.northeast or dir == dirs.northwest or dir == dirs.southeast or dir == dirs.southwest then can_place_all = false end - + if not can_place_all then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout("Building area occupied.", pindex) game.get_player(pindex).clear_cursor() return end - + --5A. Build the rail entities to create the turn (LEFT) - if dir == dirs.north then - surf.create_entity{name = "curved-rail" , position = {pos.x-00, pos.y-04}, direction = dirs.north, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-04, pos.y-08}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-04, pos.y-10}, direction = dirs.southwest, force = game.forces.player} - surf.create_entity{name = "curved-rail" , position = {pos.x-06, pos.y-12}, direction = dirs.south, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-08, pos.y-18}, direction = dirs.north, force = game.forces.player} + if dir == dirs.north then + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 00, pos.y - 04 }, + direction = dirs.north, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 04, pos.y - 08 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 04, pos.y - 10 }, + direction = dirs.southwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 06, pos.y - 12 }, + direction = dirs.south, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 08, pos.y - 18 }, + direction = dirs.north, + force = game.forces.player, + }) elseif dir == dirs.east then - surf.create_entity{name = "curved-rail" , position = {pos.x+06, pos.y-00}, direction = dirs.east, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+08, pos.y-04}, direction = dirs.southeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+10, pos.y-04}, direction = dirs.northwest, force = game.forces.player} - surf.create_entity{name = "curved-rail" , position = {pos.x+14, pos.y-06}, direction = dirs.west, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+18, pos.y-08}, direction = dirs.east, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 06, pos.y - 00 }, + direction = dirs.east, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 08, pos.y - 04 }, + direction = dirs.southeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 10, pos.y - 04 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 14, pos.y - 06 }, + direction = dirs.west, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 18, pos.y - 08 }, + direction = dirs.east, + force = game.forces.player, + }) elseif dir == dirs.south then - surf.create_entity{name = "curved-rail" , position = {pos.x+02, pos.y+06}, direction = dirs.south, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+04, pos.y+08}, direction = dirs.southwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+04, pos.y+10}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "curved-rail" , position = {pos.x+08, pos.y+14}, direction = dirs.north, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+08, pos.y+18}, direction = dirs.north, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 02, pos.y + 06 }, + direction = dirs.south, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 04, pos.y + 08 }, + direction = dirs.southwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 04, pos.y + 10 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 08, pos.y + 14 }, + direction = dirs.north, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 08, pos.y + 18 }, + direction = dirs.north, + force = game.forces.player, + }) elseif dir == dirs.west then - surf.create_entity{name = "curved-rail" , position = {pos.x-04, pos.y+02}, direction = dirs.west, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-08, pos.y+04}, direction = dirs.northwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-10, pos.y+04}, direction = dirs.southeast, force = game.forces.player} - surf.create_entity{name = "curved-rail" , position = {pos.x-12, pos.y+08}, direction = dirs.east, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-18, pos.y+08}, direction = dirs.west, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 04, pos.y + 02 }, + direction = dirs.west, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 08, pos.y + 04 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 10, pos.y + 04 }, + direction = dirs.southeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 12, pos.y + 08 }, + direction = dirs.east, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 18, pos.y + 08 }, + direction = dirs.west, + force = game.forces.player, + }) end - + --5B. Build the rail entities to create the turn (RIGHT) - if dir == dirs.north then - surf.create_entity{name = "curved-rail" , position = {pos.x+02, pos.y-04}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+04, pos.y-08}, direction = dirs.northwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+04, pos.y-10}, direction = dirs.southeast, force = game.forces.player} - surf.create_entity{name = "curved-rail" , position = {pos.x+08, pos.y-12}, direction = dirs.southwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+08, pos.y-18}, direction = dirs.north, force = game.forces.player} + if dir == dirs.north then + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 02, pos.y - 04 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 04, pos.y - 08 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 04, pos.y - 10 }, + direction = dirs.southeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 08, pos.y - 12 }, + direction = dirs.southwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 08, pos.y - 18 }, + direction = dirs.north, + force = game.forces.player, + }) elseif dir == dirs.east then - surf.create_entity{name = "curved-rail" , position = {pos.x+06, pos.y+02}, direction = dirs.southeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+08, pos.y+04}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+10, pos.y+04}, direction = dirs.southwest, force = game.forces.player} - surf.create_entity{name = "curved-rail" , position = {pos.x+14, pos.y+08}, direction = dirs.northwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x+18, pos.y+08}, direction = dirs.east, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 06, pos.y + 02 }, + direction = dirs.southeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 08, pos.y + 04 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 10, pos.y + 04 }, + direction = dirs.southwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x + 14, pos.y + 08 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 18, pos.y + 08 }, + direction = dirs.east, + force = game.forces.player, + }) elseif dir == dirs.south then - surf.create_entity{name = "curved-rail" , position = {pos.x-00, pos.y+06}, direction = dirs.southwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-04, pos.y+08}, direction = dirs.southeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-04, pos.y+10}, direction = dirs.northwest, force = game.forces.player} - surf.create_entity{name = "curved-rail" , position = {pos.x-06, pos.y+14}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-08, pos.y+18}, direction = dirs.south, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 00, pos.y + 06 }, + direction = dirs.southwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 04, pos.y + 08 }, + direction = dirs.southeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 04, pos.y + 10 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 06, pos.y + 14 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 08, pos.y + 18 }, + direction = dirs.south, + force = game.forces.player, + }) elseif dir == dirs.west then - surf.create_entity{name = "curved-rail" , position = {pos.x-04, pos.y-00}, direction = dirs.northwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-08, pos.y-04}, direction = dirs.southwest, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-10, pos.y-04}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "curved-rail" , position = {pos.x-12, pos.y-06}, direction = dirs.southeast, force = game.forces.player} - surf.create_entity{name = "straight-rail", position = {pos.x-18, pos.y-08}, direction = dirs.west, force = game.forces.player} + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 04, pos.y - 00 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 08, pos.y - 04 }, + direction = dirs.southwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 10, pos.y - 04 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "curved-rail", + position = { pos.x - 12, pos.y - 06 }, + direction = dirs.southeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 18, pos.y - 08 }, + direction = dirs.west, + force = game.forces.player, + }) end - + --5C. Build the rail entities to create the exit (MIDDLE) todo *** - if dir == dirs.north then - surf.create_entity{name = "straight-rail", position = {pos.x+08, pos.y-18}, direction = dirs.north, force = game.forces.player} + if dir == dirs.north then + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 08, pos.y - 18 }, + direction = dirs.north, + force = game.forces.player, + }) elseif dir == dirs.east then - surf.create_entity{name = "straight-rail", position = {pos.x+18, pos.y+08}, direction = dirs.east, force = game.forces.player} + surf.create_entity({ + name = "straight-rail", + position = { pos.x + 18, pos.y + 08 }, + direction = dirs.east, + force = game.forces.player, + }) elseif dir == dirs.south then - surf.create_entity{name = "straight-rail", position = {pos.x-08, pos.y+18}, direction = dirs.south, force = game.forces.player} + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 08, pos.y + 18 }, + direction = dirs.south, + force = game.forces.player, + }) elseif dir == dirs.west then - surf.create_entity{name = "straight-rail", position = {pos.x-18, pos.y-08}, direction = dirs.west, force = game.forces.player} + surf.create_entity({ + name = "straight-rail", + position = { pos.x - 18, pos.y - 08 }, + direction = dirs.west, + force = game.forces.player, + }) end - + --5D. Place rail signals (6) todo *** - if dir == dirs.north then - surf.create_entity{name = "rail-chain-signal", position = {pos.x+01, pos.y-00}, direction = dirs.south, force = game.forces.player} - surf.create_entity{name = "rail-chain-signal", position = {pos.x-02, pos.y-00}, direction = dirs.north, force = game.forces.player} - surf.create_entity{name = "rail-signal" , position = {pos.x+03, pos.y-07}, direction = dirs.southwest, force = game.forces.player} - surf.create_entity{name = "rail-chain-signal", position = {pos.x-04, pos.y-07}, direction = dirs.northwest, force = game.forces.player} + if dir == dirs.north then + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x + 01, pos.y - 00 }, + direction = dirs.south, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x - 02, pos.y - 00 }, + direction = dirs.north, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-signal", + position = { pos.x + 03, pos.y - 07 }, + direction = dirs.southwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x - 04, pos.y - 07 }, + direction = dirs.northwest, + force = game.forces.player, + }) elseif dir == dirs.east then - surf.create_entity{name = "rail-chain-signal", position = {pos.x-01, pos.y+01}, direction = dirs.west , force = game.forces.player} - surf.create_entity{name = "rail-chain-signal", position = {pos.x-01, pos.y-02}, direction = dirs.east , force = game.forces.player} - surf.create_entity{name = "rail-signal" , position = {pos.x+06, pos.y+03}, direction = dirs.northwest , force = game.forces.player} - surf.create_entity{name = "rail-chain-signal", position = {pos.x+06, pos.y-04}, direction = dirs.northeast , force = game.forces.player} + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x - 01, pos.y + 01 }, + direction = dirs.west, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x - 01, pos.y - 02 }, + direction = dirs.east, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-signal", + position = { pos.x + 06, pos.y + 03 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x + 06, pos.y - 04 }, + direction = dirs.northeast, + force = game.forces.player, + }) elseif dir == dirs.south then - surf.create_entity{name = "rail-chain-signal", position = {pos.x-02, pos.y-00}, direction = dirs.north, force = game.forces.player} - surf.create_entity{name = "rail-chain-signal", position = {pos.x+01, pos.y-00}, direction = dirs.south, force = game.forces.player} - surf.create_entity{name = "rail-signal" , position = {pos.x-04, pos.y+06}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "rail-chain-signal", position = {pos.x+03, pos.y+06}, direction = dirs.southeast, force = game.forces.player} + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x - 02, pos.y - 00 }, + direction = dirs.north, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x + 01, pos.y - 00 }, + direction = dirs.south, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-signal", + position = { pos.x - 04, pos.y + 06 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x + 03, pos.y + 06 }, + direction = dirs.southeast, + force = game.forces.player, + }) elseif dir == dirs.west then - surf.create_entity{name = "rail-chain-signal", position = {pos.x-00, pos.y-02}, direction = dirs.east , force = game.forces.player} - surf.create_entity{name = "rail-chain-signal", position = {pos.x-00, pos.y+01}, direction = dirs.west , force = game.forces.player} - surf.create_entity{name = "rail-signal" , position = {pos.x-07, pos.y-04}, direction = dirs.southeast , force = game.forces.player} - surf.create_entity{name = "rail-chain-signal", position = {pos.x-07, pos.y+03}, direction = dirs.southwest , force = game.forces.player} + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x - 00, pos.y - 02 }, + direction = dirs.east, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x - 00, pos.y + 01 }, + direction = dirs.west, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-signal", + position = { pos.x - 07, pos.y - 04 }, + direction = dirs.southeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x - 07, pos.y + 03 }, + direction = dirs.southwest, + force = game.forces.player, + }) end - + --6 Remove rail units from the player's hand game.get_player(pindex).cursor_stack.count = game.get_player(pindex).cursor_stack.count - 25 game.get_player(pindex).clear_cursor() - game.get_player(pindex).get_main_inventory().remove({name="rail-chain-signal", count=6}) - + game.get_player(pindex).get_main_inventory().remove({ name = "rail-chain-signal", count = 6 }) + --7. Sounds and results - game.get_player(pindex).play_sound{path = "entity-build/straight-rail"} - game.get_player(pindex).play_sound{path = "entity-build/curved-rail"} - game.get_player(pindex).play_sound{path = "entity-build/straight-rail"} - game.get_player(pindex).play_sound{path = "entity-build/curved-rail"} + game.get_player(pindex).play_sound({ path = "entity-build/straight-rail" }) + game.get_player(pindex).play_sound({ path = "entity-build/curved-rail" }) + game.get_player(pindex).play_sound({ path = "entity-build/straight-rail" }) + game.get_player(pindex).play_sound({ path = "entity-build/curved-rail" }) local result = "Rail bypass junction built with 3 branches, " .. build_comment - printout(result,pindex) + printout(result, pindex) return - end --Places a chain signal pair around a rail depending on its direction. May fail if the spots are full. -function mod.place_chain_signal_pair(rail,pindex) +function mod.place_chain_signal_pair(rail, pindex) local stack = game.get_player(pindex).cursor_stack local stack2 = nil local build_comment = "no comment" @@ -1717,65 +4775,137 @@ function mod.place_chain_signal_pair(rail,pindex) local pos = rail.position local surf = rail.surface local can_place_all = true - + --1. Check if signals can be placed, based on direction if dir == dirs.north or dir == dirs.south then - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x+1, pos.y}, direction = dirs.south, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x-2, pos.y}, direction = dirs.north, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x + 1, pos.y }, + direction = dirs.south, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x - 2, pos.y }, + direction = dirs.north, + force = game.forces.player, + }) elseif dir == dirs.east or dir == dirs.west then - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x, pos.y-2}, direction = dirs.east, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x, pos.y+1}, direction = dirs.west, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x, pos.y - 2 }, + direction = dirs.east, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x, pos.y + 1 }, + direction = dirs.west, + force = game.forces.player, + }) elseif dir == dirs.northeast then - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x-1, pos.y-0}, direction = dirs.northwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x+1, pos.y-2}, direction = dirs.southeast, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x - 1, pos.y - 0 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x + 1, pos.y - 2 }, + direction = dirs.southeast, + force = game.forces.player, + }) elseif dir == dirs.southwest then - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x-2, pos.y+1}, direction = dirs.northwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x+0, pos.y-1}, direction = dirs.southeast, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x - 2, pos.y + 1 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x + 0, pos.y - 1 }, + direction = dirs.southeast, + force = game.forces.player, + }) elseif dir == dirs.southeast then - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x-1, pos.y-1}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x+1, pos.y+1}, direction = dirs.southwest, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x - 1, pos.y - 1 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x + 1, pos.y + 1 }, + direction = dirs.southwest, + force = game.forces.player, + }) elseif dir == dirs.northwest then - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x-2, pos.y-2}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-chain-signal", position = {pos.x+0, pos.y+0}, direction = dirs.southwest, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x - 2, pos.y - 2 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-chain-signal", + position = { pos.x + 0, pos.y + 0 }, + direction = dirs.southwest, + force = game.forces.player, + }) else successful = false - game.get_player(pindex).play_sound{path = "utility/cannot_build"} - build_comment = "direction error" - return successful, build_comment + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) + build_comment = "direction error" + return successful, build_comment end - + if not can_place_all then successful = false - game.get_player(pindex).play_sound{path = "utility/cannot_build"} - build_comment = "cannot place" - return successful, build_comment + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) + build_comment = "cannot place" + return successful, build_comment end - + --2. Check if there are already chain signals or rail signals nearby. If yes, stop. local signals_found = 0 - local signals = surf.find_entities_filtered{position = pos, radius = 3, name="rail-chain-signal"} - for i,signal in ipairs(signals) do + local signals = surf.find_entities_filtered({ position = pos, radius = 3, name = "rail-chain-signal" }) + for i, signal in ipairs(signals) do signals_found = signals_found + 1 end - local signals = surf.find_entities_filtered{position = pos, radius = 3, name="rail-signal"} - for i,signal in ipairs(signals) do + local signals = surf.find_entities_filtered({ position = pos, radius = 3, name = "rail-signal" }) + for i, signal in ipairs(signals) do signals_found = signals_found + 1 end if signals_found > 0 then successful = false - game.get_player(pindex).play_sound{path = "utility/cannot_build"} - build_comment = "Too close to existing signals." - return successful, build_comment + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) + build_comment = "Too close to existing signals." + return successful, build_comment end - + --3. Check whether the player has enough rail chain signals. if not (stack.valid and stack.valid_for_read and stack.name == "rail-chain-signal" and stack.count >= 2) then --Check if the inventory has one instead if players[pindex].inventory.lua_inventory.get_item_count("rail-chain-signal") < 2 then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) build_comment = "You need to have at least 2 rail chain signals on you." - successful = false - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + successful = false + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) return successful, build_comment else --Take from the inventory. @@ -1785,44 +4915,104 @@ function mod.place_chain_signal_pair(rail,pindex) players[pindex].inventory.max = #players[pindex].inventory.lua_inventory end end - + --4. Place the signals. if dir == dirs.north or dir == dirs.south then - surf.create_entity{name = "rail-chain-signal", position = {pos.x+1, pos.y}, direction = dirs.south, force = game.forces.player} - surf.create_entity{name = "rail-chain-signal", position = {pos.x-2, pos.y}, direction = dirs.north, force = game.forces.player} + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x + 1, pos.y }, + direction = dirs.south, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x - 2, pos.y }, + direction = dirs.north, + force = game.forces.player, + }) elseif dir == dirs.east or dir == dirs.west then - surf.create_entity{name = "rail-chain-signal", position = {pos.x, pos.y-2}, direction = dirs.east, force = game.forces.player} - surf.create_entity{name = "rail-chain-signal", position = {pos.x, pos.y+1}, direction = dirs.west, force = game.forces.player} + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x, pos.y - 2 }, + direction = dirs.east, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x, pos.y + 1 }, + direction = dirs.west, + force = game.forces.player, + }) elseif dir == dirs.northeast then - surf.create_entity{name = "rail-chain-signal", position = {pos.x-1, pos.y-0}, direction = dirs.northwest, force = game.forces.player} - surf.create_entity{name = "rail-chain-signal", position = {pos.x+1, pos.y-2}, direction = dirs.southeast, force = game.forces.player} + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x - 1, pos.y - 0 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x + 1, pos.y - 2 }, + direction = dirs.southeast, + force = game.forces.player, + }) elseif dir == dirs.southwest then - surf.create_entity{name = "rail-chain-signal", position = {pos.x-2, pos.y+1}, direction = dirs.northwest, force = game.forces.player} - surf.create_entity{name = "rail-chain-signal", position = {pos.x+0, pos.y-1}, direction = dirs.southeast, force = game.forces.player} + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x - 2, pos.y + 1 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x + 0, pos.y - 1 }, + direction = dirs.southeast, + force = game.forces.player, + }) elseif dir == dirs.southeast then - surf.create_entity{name = "rail-chain-signal", position = {pos.x-1, pos.y-1}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "rail-chain-signal", position = {pos.x+1, pos.y+1}, direction = dirs.southwest, force = game.forces.player} + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x - 1, pos.y - 1 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x + 1, pos.y + 1 }, + direction = dirs.southwest, + force = game.forces.player, + }) elseif dir == dirs.northwest then - surf.create_entity{name = "rail-chain-signal", position = {pos.x-2, pos.y-2}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "rail-chain-signal", position = {pos.x+0, pos.y+0}, direction = dirs.southwest, force = game.forces.player} + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x - 2, pos.y - 2 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-chain-signal", + position = { pos.x + 0, pos.y + 0 }, + direction = dirs.southwest, + force = game.forces.player, + }) else successful = false - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) build_comment = "direction error" return successful, build_comment end - + --Reduce the signal count and restore the cursor and wrap up game.get_player(pindex).cursor_stack.count = game.get_player(pindex).cursor_stack.count - 2 game.get_player(pindex).clear_cursor() - - game.get_player(pindex).play_sound{path = "entity-build/rail-chain-signal"} - game.get_player(pindex).play_sound{path = "entity-build/rail-chain-signal"} + + game.get_player(pindex).play_sound({ path = "entity-build/rail-chain-signal" }) + game.get_player(pindex).play_sound({ path = "entity-build/rail-chain-signal" }) return successful, build_comment end --Places a rail signal pair around a rail depending on its direction. May fail if the spots are full. Copy of chain signal function -function mod.place_rail_signal_pair(rail,pindex) +function mod.place_rail_signal_pair(rail, pindex) local stack = game.get_player(pindex).cursor_stack local stack2 = nil local build_comment = "no comment" @@ -1831,65 +5021,137 @@ function mod.place_rail_signal_pair(rail,pindex) local pos = rail.position local surf = rail.surface local can_place_all = true - + --1. Check if signals can be placed, based on direction if dir == dirs.north or dir == dirs.south then - can_place_all = can_place_all and surf.can_place_entity{name = "rail-signal", position = {pos.x+1, pos.y}, direction = dirs.south, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-signal", position = {pos.x-2, pos.y}, direction = dirs.north, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-signal", + position = { pos.x + 1, pos.y }, + direction = dirs.south, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-signal", + position = { pos.x - 2, pos.y }, + direction = dirs.north, + force = game.forces.player, + }) elseif dir == dirs.east or dir == dirs.west then - can_place_all = can_place_all and surf.can_place_entity{name = "rail-signal", position = {pos.x, pos.y-2}, direction = dirs.east, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-signal", position = {pos.x, pos.y+1}, direction = dirs.west, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-signal", + position = { pos.x, pos.y - 2 }, + direction = dirs.east, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-signal", + position = { pos.x, pos.y + 1 }, + direction = dirs.west, + force = game.forces.player, + }) elseif dir == dirs.northeast then - can_place_all = can_place_all and surf.can_place_entity{name = "rail-signal", position = {pos.x-1, pos.y-0}, direction = dirs.northwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-signal", position = {pos.x+1, pos.y-2}, direction = dirs.southeast, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-signal", + position = { pos.x - 1, pos.y - 0 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-signal", + position = { pos.x + 1, pos.y - 2 }, + direction = dirs.southeast, + force = game.forces.player, + }) elseif dir == dirs.southwest then - can_place_all = can_place_all and surf.can_place_entity{name = "rail-signal", position = {pos.x-2, pos.y+1}, direction = dirs.northwest, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-signal", position = {pos.x+0, pos.y-1}, direction = dirs.southeast, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-signal", + position = { pos.x - 2, pos.y + 1 }, + direction = dirs.northwest, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-signal", + position = { pos.x + 0, pos.y - 1 }, + direction = dirs.southeast, + force = game.forces.player, + }) elseif dir == dirs.southeast then - can_place_all = can_place_all and surf.can_place_entity{name = "rail-signal", position = {pos.x-1, pos.y-1}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-signal", position = {pos.x+1, pos.y+1}, direction = dirs.southwest, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-signal", + position = { pos.x - 1, pos.y - 1 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-signal", + position = { pos.x + 1, pos.y + 1 }, + direction = dirs.southwest, + force = game.forces.player, + }) elseif dir == dirs.northwest then - can_place_all = can_place_all and surf.can_place_entity{name = "rail-signal", position = {pos.x-2, pos.y-2}, direction = dirs.northeast, force = game.forces.player} - can_place_all = can_place_all and surf.can_place_entity{name = "rail-signal", position = {pos.x+0, pos.y+0}, direction = dirs.southwest, force = game.forces.player} + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-signal", + position = { pos.x - 2, pos.y - 2 }, + direction = dirs.northeast, + force = game.forces.player, + }) + can_place_all = can_place_all + and surf.can_place_entity({ + name = "rail-signal", + position = { pos.x + 0, pos.y + 0 }, + direction = dirs.southwest, + force = game.forces.player, + }) else successful = false - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) build_comment = "direction error" return successful, build_comment end - + if not can_place_all then successful = false - game.get_player(pindex).play_sound{path = "utility/cannot_build"} - build_comment = "cannot place" - return successful, build_comment + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) + build_comment = "cannot place" + return successful, build_comment end - + --2. Check if there are already chain signals or rail signals nearby. If yes, stop. local signals_found = 0 - local signals = surf.find_entities_filtered{position = pos, radius = 3, name="rail-chain-signal"} - for i,signal in ipairs(signals) do + local signals = surf.find_entities_filtered({ position = pos, radius = 3, name = "rail-chain-signal" }) + for i, signal in ipairs(signals) do signals_found = signals_found + 1 end - local signals = surf.find_entities_filtered{position = pos, radius = 3, name="rail-signal"} - for i,signal in ipairs(signals) do + local signals = surf.find_entities_filtered({ position = pos, radius = 3, name = "rail-signal" }) + for i, signal in ipairs(signals) do signals_found = signals_found + 1 end if signals_found > 0 then successful = false - game.get_player(pindex).play_sound{path = "utility/cannot_build"} - build_comment = "Too close to existing signals." - return successful, build_comment + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) + build_comment = "Too close to existing signals." + return successful, build_comment end - + --3. Check whether the player has enough rail chain signals. if not (stack.valid and stack.valid_for_read and stack.name == "rail-signal" and stack.count >= 2) then --Check if the inventory has one instead if players[pindex].inventory.lua_inventory.get_item_count("rail-signal") < 2 then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) build_comment = "You need to have at least 2 rail signals on you." - successful = false - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + successful = false + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) return successful, build_comment else --Take from the inventory. @@ -1899,50 +5161,111 @@ function mod.place_rail_signal_pair(rail,pindex) players[pindex].inventory.max = #players[pindex].inventory.lua_inventory end end - + --4. Place the signals. if dir == dirs.north or dir == dirs.south then - surf.create_entity{name = "rail-signal", position = {pos.x+1, pos.y}, direction = dirs.south, force = game.forces.player} - surf.create_entity{name = "rail-signal", position = {pos.x-2, pos.y}, direction = dirs.north, force = game.forces.player} + surf.create_entity({ + name = "rail-signal", + position = { pos.x + 1, pos.y }, + direction = dirs.south, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-signal", + position = { pos.x - 2, pos.y }, + direction = dirs.north, + force = game.forces.player, + }) elseif dir == dirs.east or dir == dirs.west then - surf.create_entity{name = "rail-signal", position = {pos.x, pos.y-2}, direction = dirs.east, force = game.forces.player} - surf.create_entity{name = "rail-signal", position = {pos.x, pos.y+1}, direction = dirs.west, force = game.forces.player} + surf.create_entity({ + name = "rail-signal", + position = { pos.x, pos.y - 2 }, + direction = dirs.east, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-signal", + position = { pos.x, pos.y + 1 }, + direction = dirs.west, + force = game.forces.player, + }) elseif dir == dirs.northeast then - surf.create_entity{name = "rail-signal", position = {pos.x-1, pos.y-0}, direction = dirs.northwest, force = game.forces.player} - surf.create_entity{name = "rail-signal", position = {pos.x+1, pos.y-2}, direction = dirs.southeast, force = game.forces.player} + surf.create_entity({ + name = "rail-signal", + position = { pos.x - 1, pos.y - 0 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-signal", + position = { pos.x + 1, pos.y - 2 }, + direction = dirs.southeast, + force = game.forces.player, + }) elseif dir == dirs.southwest then - surf.create_entity{name = "rail-signal", position = {pos.x-2, pos.y+1}, direction = dirs.northwest, force = game.forces.player} - surf.create_entity{name = "rail-signal", position = {pos.x+0, pos.y-1}, direction = dirs.southeast, force = game.forces.player} + surf.create_entity({ + name = "rail-signal", + position = { pos.x - 2, pos.y + 1 }, + direction = dirs.northwest, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-signal", + position = { pos.x + 0, pos.y - 1 }, + direction = dirs.southeast, + force = game.forces.player, + }) elseif dir == dirs.southeast then - surf.create_entity{name = "rail-signal", position = {pos.x-1, pos.y-1}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "rail-signal", position = {pos.x+1, pos.y+1}, direction = dirs.southwest, force = game.forces.player} + surf.create_entity({ + name = "rail-signal", + position = { pos.x - 1, pos.y - 1 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-signal", + position = { pos.x + 1, pos.y + 1 }, + direction = dirs.southwest, + force = game.forces.player, + }) elseif dir == dirs.northwest then - surf.create_entity{name = "rail-signal", position = {pos.x-2, pos.y-2}, direction = dirs.northeast, force = game.forces.player} - surf.create_entity{name = "rail-signal", position = {pos.x+0, pos.y+0}, direction = dirs.southwest, force = game.forces.player} + surf.create_entity({ + name = "rail-signal", + position = { pos.x - 2, pos.y - 2 }, + direction = dirs.northeast, + force = game.forces.player, + }) + surf.create_entity({ + name = "rail-signal", + position = { pos.x + 0, pos.y + 0 }, + direction = dirs.southwest, + force = game.forces.player, + }) else successful = false - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) build_comment = "direction error" return successful, build_comment end - + --Reduce the signal count and restore the cursor and wrap up game.get_player(pindex).cursor_stack.count = game.get_player(pindex).cursor_stack.count - 2 game.get_player(pindex).clear_cursor() - - game.get_player(pindex).play_sound{path = "entity-build/rail-signal"} - game.get_player(pindex).play_sound{path = "entity-build/rail-signal"} + + game.get_player(pindex).play_sound({ path = "entity-build/rail-signal" }) + game.get_player(pindex).play_sound({ path = "entity-build/rail-signal" }) return successful, build_comment end --Deletes rail signals around a rail. function mod.destroy_signals(rail) - local chains = rail.surface.find_entities_filtered{position = rail.position, radius = 2, name = "rail-chain-signal"} - for i,chain in ipairs(chains) do + local chains = + rail.surface.find_entities_filtered({ position = rail.position, radius = 2, name = "rail-chain-signal" }) + for i, chain in ipairs(chains) do chain.destroy() end - local signals = rail.surface.find_entities_filtered{position = rail.position, radius = 2, name = "rail-signal"} - for i,signal in ipairs(signals) do + local signals = rail.surface.find_entities_filtered({ position = rail.position, radius = 2, name = "rail-signal" }) + for i, signal in ipairs(signals) do signal.destroy() end end @@ -1958,12 +5281,12 @@ function mod.build_train_stop(anchor_rail, pindex) local build_area = nil local can_place_all = true local is_end_rail - + --1. Firstly, check if the player has a train stop in hand if not (stack.valid and stack.valid_for_read and stack.name == "train-stop" and stack.count > 0) then --Check if the inventory has enough if players[pindex].inventory.lua_inventory.get_item_count("train-stop") < 1 then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout("You need at least 1 train stop in your inventory to build this turn.", pindex) return else @@ -1974,13 +5297,13 @@ function mod.build_train_stop(anchor_rail, pindex) players[pindex].inventory.max = #players[pindex].inventory.lua_inventory end end - + --2. Secondly, find the direction based on end rail or player direction - is_end_rail, end_rail_dir, build_comment = fa_rails.check_end_rail(anchor_rail,pindex) + is_end_rail, end_rail_dir, build_comment = fa_rails.check_end_rail(anchor_rail, pindex) if is_end_rail then dir = end_rail_dir else - --Choose the dir based on player direction + --Choose the dir based on player direction turn_to_cursor_direction_cardinal(pindex) if anchor_rail.direction == dirs.north or anchor_rail.direction == dirs.south then if players[pindex].player_direction == dirs.north or players[pindex].player_direction == dirs.east then @@ -1998,90 +5321,124 @@ function mod.build_train_stop(anchor_rail, pindex) end pos = anchor_rail.position if dir == dirs.northeast or dir == dirs.southeast or dir == dirs.southwest or dir == dirs.northwest then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout("This structure is for horizontal or vertical end rails only.", pindex) game.get_player(pindex).clear_cursor() return end - + --3. Clear trees and rocks in the build area - temp1, build_comment = fa_mining_tools.clear_obstacles_in_circle(pos,3, pindex) - + temp1, build_comment = fa_mining_tools.clear_obstacles_in_circle(pos, 3, pindex) + --4. Check if every object can be placed - if dir == dirs.north then - can_place_all = can_place_all and surf.can_place_entity{name = "train-stop", position = {pos.x+2, pos.y+0}, direction = dirs.north, force = game.forces.player} - + if dir == dirs.north then + can_place_all = can_place_all + and surf.can_place_entity({ + name = "train-stop", + position = { pos.x + 2, pos.y + 0 }, + direction = dirs.north, + force = game.forces.player, + }) elseif dir == dirs.east then - can_place_all = can_place_all and surf.can_place_entity{name = "train-stop", position = {pos.x+0, pos.y+2}, direction = dirs.east, force = game.forces.player} - + can_place_all = can_place_all + and surf.can_place_entity({ + name = "train-stop", + position = { pos.x + 0, pos.y + 2 }, + direction = dirs.east, + force = game.forces.player, + }) elseif dir == dirs.south then - can_place_all = can_place_all and surf.can_place_entity{name = "train-stop", position = {pos.x-2, pos.y+0}, direction = dirs.south, force = game.forces.player} - + can_place_all = can_place_all + and surf.can_place_entity({ + name = "train-stop", + position = { pos.x - 2, pos.y + 0 }, + direction = dirs.south, + force = game.forces.player, + }) elseif dir == dirs.west then - can_place_all = can_place_all and surf.can_place_entity{name = "train-stop", position = {pos.x-0, pos.y-2}, direction = dirs.west, force = game.forces.player} - + can_place_all = can_place_all + and surf.can_place_entity({ + name = "train-stop", + position = { pos.x - 0, pos.y - 2 }, + direction = dirs.west, + force = game.forces.player, + }) end - + if not can_place_all then - game.get_player(pindex).play_sound{path = "utility/cannot_build"} + game.get_player(pindex).play_sound({ path = "utility/cannot_build" }) printout("Building area occupied, possibly by the player. Cursor mode recommended.", pindex) game.get_player(pindex).clear_cursor() return end - - --5. Build the five rail entities to create the structure - if dir == dirs.north then - surf.create_entity{name = "train-stop", position = {pos.x+2, pos.y+0}, direction = dirs.north, force = game.forces.player} - + + --5. Build the five rail entities to create the structure + if dir == dirs.north then + surf.create_entity({ + name = "train-stop", + position = { pos.x + 2, pos.y + 0 }, + direction = dirs.north, + force = game.forces.player, + }) elseif dir == dirs.east then - surf.create_entity{name = "train-stop", position = {pos.x+0, pos.y+2}, direction = dirs.east, force = game.forces.player} - + surf.create_entity({ + name = "train-stop", + position = { pos.x + 0, pos.y + 2 }, + direction = dirs.east, + force = game.forces.player, + }) elseif dir == dirs.south then - surf.create_entity{name = "train-stop", position = {pos.x-2, pos.y+0}, direction = dirs.south, force = game.forces.player} - + surf.create_entity({ + name = "train-stop", + position = { pos.x - 2, pos.y + 0 }, + direction = dirs.south, + force = game.forces.player, + }) elseif dir == dirs.west then - surf.create_entity{name = "train-stop", position = {pos.x-0, pos.y-2}, direction = dirs.west, force = game.forces.player} - + surf.create_entity({ + name = "train-stop", + position = { pos.x - 0, pos.y - 2 }, + direction = dirs.west, + force = game.forces.player, + }) end - + --6 Remove 5 rail units from the player's hand game.get_player(pindex).cursor_stack.count = game.get_player(pindex).cursor_stack.count - 1 game.get_player(pindex).clear_cursor() - + --7. Sounds and results - game.get_player(pindex).play_sound{path = "entity-build/train-stop"} + game.get_player(pindex).play_sound({ path = "entity-build/train-stop" }) printout("Train stop built facing" .. fa_utils.direction_lookup(dir) .. ", " .. build_comment, pindex) return end --Loads and opens the rail builder menu function mod.open_menu(pindex, rail) - if players[pindex].vanilla_mode then - return - end + if players[pindex].vanilla_mode then return end --Set the player menu tracker to this menu players[pindex].menu = "rail_builder" players[pindex].in_menu = true players[pindex].move_queue = {} - + --Set the menu line counter to 0 players[pindex].rail_builder.index = 0 - + --Determine rail type - local is_end_rail, end_dir, comment = fa_rails.check_end_rail(rail,pindex) + local is_end_rail, end_dir, comment = fa_rails.check_end_rail(rail, pindex) local dir = rail.direction if is_end_rail then - if dir == dirs.north or dir == dirs.east or dir == dirs.south or dir == dirs.west then + if dir == dirs.north or dir == dirs.east or dir == dirs.south or dir == dirs.west then --Straight end rails players[pindex].rail_builder.rail_type = 1 players[pindex].rail_builder.index_max = 8 - else + else --Diagonal end rails players[pindex].rail_builder.rail_type = 2 players[pindex].rail_builder.index_max = 2 end else - if dir == dirs.north or dir == dirs.east or dir == dirs.south or dir == dirs.west then + if dir == dirs.north or dir == dirs.east or dir == dirs.south or dir == dirs.west then --Straight mid rails players[pindex].rail_builder.rail_type = 3 players[pindex].rail_builder.index_max = 3 @@ -2091,11 +5448,11 @@ function mod.open_menu(pindex, rail) players[pindex].rail_builder.index_max = 3 end end - + --Play sound - game.get_player(pindex).play_sound{path = "Open-Inventory-Sound"} - - --Load menu + game.get_player(pindex).play_sound({ path = "Open-Inventory-Sound" }) + + --Load menu players[pindex].rail_builder.rail = rail mod.run_menu(pindex, false) end @@ -2109,11 +5466,9 @@ function mod.close_menu(pindex, mute_in) --Set the menu line counter to 0 players[pindex].rail_builder.index = 0 - + --play sound - if not mute then - game.get_player(pindex).play_sound{path="Close-Inventory-Sound"} - end + if not mute then game.get_player(pindex).play_sound({ path = "Close-Inventory-Sound" }) end end --Moves up the rail builder menu @@ -2124,13 +5479,13 @@ function mod.menu_up(pindex) --Check the index against the limit if players[pindex].rail_builder.index < 0 then players[pindex].rail_builder.index = 0 - game.get_player(pindex).play_sound{path = "inventory-edge"} + game.get_player(pindex).play_sound({ path = "inventory-edge" }) else --Play sound - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) end - - --Load menu + + --Load menu mod.run_menu(pindex, false) end @@ -2142,13 +5497,13 @@ function mod.menu_down(pindex) --Check the index against the limit if players[pindex].rail_builder.index > players[pindex].rail_builder.index_max then players[pindex].rail_builder.index = players[pindex].rail_builder.index_max - game.get_player(pindex).play_sound{path = "inventory-edge"} + game.get_player(pindex).play_sound({ path = "inventory-edge" }) else --Play sound - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) end - - --Load menu + + --Load menu mod.run_menu(pindex, false) end @@ -2159,26 +5514,27 @@ function mod.run_menu(pindex, clicked_in) local menu_line = players[pindex].rail_builder.index local rail_type = players[pindex].rail_builder.rail_type local rail = players[pindex].rail_builder.rail - + if rail == nil then comment = " Rail nil error " - printout(comment,pindex) + printout(comment, pindex) mod.close_menu(pindex, false) return end - + if menu_line == 0 then - comment = comment .. "Select a structure to build by going up or down this menu, attempt to build it via LEFT BRACKET, " - printout(comment,pindex) + comment = comment + .. "Select a structure to build by going up or down this menu, attempt to build it via LEFT BRACKET, " + printout(comment, pindex) return end - + if rail_type == 1 then --Straight end rails if menu_line == 1 then if not clicked then comment = comment .. "Left turn 45 degrees" - printout(comment,pindex) + printout(comment, pindex) else --Build it here mod.build_rail_turn_left_45_degrees(rail, pindex) @@ -2186,7 +5542,7 @@ function mod.run_menu(pindex, clicked_in) elseif menu_line == 2 then if not clicked then comment = comment .. "Right turn 45 degrees" - printout(comment,pindex) + printout(comment, pindex) else --Build it here mod.build_rail_turn_right_45_degrees(rail, pindex) @@ -2194,7 +5550,7 @@ function mod.run_menu(pindex, clicked_in) elseif menu_line == 3 then if not clicked then comment = comment .. "Left turn 90 degrees" - printout(comment,pindex) + printout(comment, pindex) else --Build it here mod.build_rail_turn_left_90_degrees(rail, pindex) @@ -2202,7 +5558,7 @@ function mod.run_menu(pindex, clicked_in) elseif menu_line == 4 then if not clicked then comment = comment .. "Right turn 90 degrees" - printout(comment,pindex) + printout(comment, pindex) else --Build it here mod.build_rail_turn_right_90_degrees(rail, pindex) @@ -2210,7 +5566,7 @@ function mod.run_menu(pindex, clicked_in) elseif menu_line == 5 then if not clicked then comment = comment .. "Train stop facing end rail direction" - printout(comment,pindex) + printout(comment, pindex) else --Build it here mod.build_train_stop(rail, pindex) @@ -2218,7 +5574,7 @@ function mod.run_menu(pindex, clicked_in) elseif menu_line == 6 then if not clicked then comment = comment .. "Rail fork with 2 exits" - printout(comment,pindex) + printout(comment, pindex) else --Build it here mod.build_fork_at_end_rail(rail, pindex, false) @@ -2226,7 +5582,7 @@ function mod.run_menu(pindex, clicked_in) elseif menu_line == 7 then if not clicked then comment = comment .. "Rail fork with 3 exits" - printout(comment,pindex) + printout(comment, pindex) else --Build it here mod.build_fork_at_end_rail(rail, pindex, true) @@ -2234,7 +5590,7 @@ function mod.run_menu(pindex, clicked_in) elseif menu_line == 8 then if not clicked then comment = comment .. "Rail bypass junction" - printout(comment,pindex) + printout(comment, pindex) else --Build it here mod.build_rail_bypass_junction(rail, pindex) @@ -2245,7 +5601,7 @@ function mod.run_menu(pindex, clicked_in) if menu_line == 1 then if not clicked then comment = comment .. "Left turn 45 degrees" - printout(comment,pindex) + printout(comment, pindex) else --Build it here mod.build_rail_turn_left_45_degrees(rail, pindex) @@ -2253,7 +5609,7 @@ function mod.run_menu(pindex, clicked_in) elseif menu_line == 2 then if not clicked then comment = comment .. "Right turn 45 degrees" - printout(comment,pindex) + printout(comment, pindex) else --Build it here mod.build_rail_turn_right_45_degrees(rail, pindex) @@ -2261,76 +5617,80 @@ function mod.run_menu(pindex, clicked_in) end elseif rail_type == 3 then --Straight mid rails - if menu_line == 1 then + if menu_line == 1 then if not clicked then comment = comment .. "Pair of chain rail signals." - printout(comment,pindex) + printout(comment, pindex) else - local success, build_comment = mod.place_chain_signal_pair(rail,pindex) + local success, build_comment = mod.place_chain_signal_pair(rail, pindex) if success then comment = "Chain signals placed." else comment = comment .. build_comment end - printout(comment,pindex) - end - elseif menu_line == 2 then + printout(comment, pindex) + end + elseif menu_line == 2 then if not clicked then - comment = comment .. "Pair of regular rail signals, warning: do not use regular rail signals unless you are sure about what you are doing because trains can easily get deadlocked at them" - printout(comment,pindex) + comment = comment + .. "Pair of regular rail signals, warning: do not use regular rail signals unless you are sure about what you are doing because trains can easily get deadlocked at them" + printout(comment, pindex) else - local success, build_comment = mod.place_rail_signal_pair(rail,pindex) + local success, build_comment = mod.place_rail_signal_pair(rail, pindex) if success then - comment = "Rail signals placed, warning: do not use regular rail signals unless you are sure about what you are doing because trains can easily get deadlocked at them" + comment = + "Rail signals placed, warning: do not use regular rail signals unless you are sure about what you are doing because trains can easily get deadlocked at them" else comment = comment .. build_comment end - printout(comment,pindex) + printout(comment, pindex) end - elseif menu_line == 3 then + elseif menu_line == 3 then if not clicked then comment = comment .. "Clear rail signals" - printout(comment,pindex) + printout(comment, pindex) else - fa_rails.mine_signals(rail,pindex) - printout("Signals cleared.",pindex) + fa_rails.mine_signals(rail, pindex) + printout("Signals cleared.", pindex) end end elseif rail_type == 4 then --Diagonal mid rails if menu_line == 1 then if not clicked then - comment = comment .. "Pair of chain rail signals." - printout(comment,pindex) + comment = comment .. "Pair of chain rail signals." + printout(comment, pindex) else - local success, build_comment = mod.place_chain_signal_pair(rail,pindex) + local success, build_comment = mod.place_chain_signal_pair(rail, pindex) if success then comment = "Chain signals placed." else comment = comment .. build_comment end - printout(comment,pindex) - end + printout(comment, pindex) + end elseif menu_line == 2 then if not clicked then - comment = comment .. "Pair of regular rail signals, warning: do not use regular rail signals unless you are sure about what you are doing because trains can easily get deadlocked at them" - printout(comment,pindex) + comment = comment + .. "Pair of regular rail signals, warning: do not use regular rail signals unless you are sure about what you are doing because trains can easily get deadlocked at them" + printout(comment, pindex) else - local success, build_comment = mod.place_rail_signal_pair(rail,pindex) + local success, build_comment = mod.place_rail_signal_pair(rail, pindex) if success then - comment = "Rail signals placed, warning: do not use regular rail signals unless you are sure about what you are doing because trains can easily get deadlocked at them" + comment = + "Rail signals placed, warning: do not use regular rail signals unless you are sure about what you are doing because trains can easily get deadlocked at them" else comment = comment .. build_comment end - printout(comment,pindex) + printout(comment, pindex) end - elseif menu_line == 3 then + elseif menu_line == 3 then if not clicked then comment = comment .. "Clear rail signals" - printout(comment,pindex) + printout(comment, pindex) else - fa_rails.mine_signals(rail,pindex) - printout("Signals cleared.",pindex) + fa_rails.mine_signals(rail, pindex) + printout("Signals cleared.", pindex) end end end diff --git a/scripts/rails.lua b/scripts/rails.lua index e08e5ad9..60cf627e 100644 --- a/scripts/rails.lua +++ b/scripts/rails.lua @@ -1,31 +1,29 @@ ---Here: Functions about rail systems, excluding building them +--Here: Functions about rail systems, excluding building them --Does not include event handlers -local util = require('util') -local fa_utils = require('scripts.fa-utils') -local fa_mouse = require('scripts.mouse') +local util = require("util") +local fa_utils = require("scripts.fa-utils") +local fa_mouse = require("scripts.mouse") local dirs = defines.direction local mod = {} ---Key information about rail units. -function mod.rail_ent_info(pindex, ent, description) +--Key information about rail units. +function mod.rail_ent_info(pindex, ent, description) local result = "" local is_end_rail = false local is_horz_or_vert = false - + --Check if end rail: The rail is at the end of its segment and is also not connected to another rail - is_end_rail, end_rail_dir, build_comment = mod.check_end_rail(ent,pindex) + is_end_rail, end_rail_dir, build_comment = mod.check_end_rail(ent, pindex) if is_end_rail then --Further check if it is a single rail - if build_comment == "single rail" then - result = result .. "Single " - end + if build_comment == "single rail" then result = result .. "Single " end result = result .. "End rail " else result = result .. "Rail " end - + --Explain the rail facing direction if ent.name == "straight-rail" and is_end_rail then result = result .. " straight " @@ -46,7 +44,6 @@ function mod.rail_ent_info(pindex, ent, description) elseif end_rail_dir == dirs.northwest then result = result .. " facing Northwest " end - elseif ent.name == "straight-rail" and is_end_rail == false then if ent.direction == dirs.north or ent.direction == dirs.south then --always reports 0 it seems result = result .. " vertical " @@ -54,7 +51,6 @@ function mod.rail_ent_info(pindex, ent, description) elseif ent.direction == dirs.east or ent.direction == dirs.west then --always reports 2 it seems result = result .. " horizontal " is_horz_or_vert = true - elseif ent.direction == dirs.northeast then result = result .. " on falling diagonal, left half " elseif ent.direction == dirs.southwest then @@ -64,7 +60,6 @@ function mod.rail_ent_info(pindex, ent, description) elseif ent.direction == dirs.northwest then result = result .. " on rising diagonal, right half " end - elseif ent.name == "curved-rail" and is_end_rail == true then result = result .. " curved " if end_rail_dir == dirs.north then @@ -84,53 +79,63 @@ function mod.rail_ent_info(pindex, ent, description) elseif end_rail_dir == dirs.northwest then result = result .. " facing Northwest " end - elseif ent.name == "curved-rail" and is_end_rail == false then result = result .. " curved " if ent.direction == dirs.north then --0 - result = result .. " facing south and falling diagonal " + result = result .. " facing south and falling diagonal " elseif ent.direction == dirs.northeast then - result = result .. " facing south and rising diagonal " + result = result .. " facing south and rising diagonal " elseif ent.direction == dirs.east then - result = result .. " facing west and rising diagonal " + result = result .. " facing west and rising diagonal " elseif ent.direction == dirs.southeast then - result = result .. " facing west and falling diagonal " + result = result .. " facing west and falling diagonal " elseif ent.direction == dirs.south then - result = result .. " facing north and falling diagonal " + result = result .. " facing north and falling diagonal " elseif ent.direction == dirs.southwest then - result = result .. " facing north and rising diagonal " + result = result .. " facing north and rising diagonal " elseif ent.direction == dirs.west then - result = result .. " facing east and rising diagonal " + result = result .. " facing east and rising diagonal " elseif ent.direction == dirs.northwest then --7 - result = result .. " facing east and falling diagonal " + result = result .. " facing east and falling diagonal " end end - + --Check if intersection - if mod.is_intersection_rail(ent, pindex) then - result = result .. ", intersection " - end + if mod.is_intersection_rail(ent, pindex) then result = result .. ", intersection " end --Check if at junction: The rail has at least 3 connections local connection_count = mod.count_rail_connections(ent) - if connection_count > 2 then - result = result .. ", fork " - end - - --Check if it has rail signals + if connection_count > 2 then result = result .. ", fork " end + + --Check if it has rail signals local chain_s_count = 0 local rail_s_count = 0 - local signals = ent.surface.find_entities_filtered{position = ent.position, radius = 2, name = "rail-chain-signal"} - for i,s in ipairs(signals) do + local signals = + ent.surface.find_entities_filtered({ position = ent.position, radius = 2, name = "rail-chain-signal" }) + for i, s in ipairs(signals) do chain_s_count = chain_s_count + 1 - rendering.draw_circle{color = {0.5, 0.5, 1},radius = 2,width = 2,target = ent,surface = ent.surface,time_to_live = 90} + rendering.draw_circle({ + color = { 0.5, 0.5, 1 }, + radius = 2, + width = 2, + target = ent, + surface = ent.surface, + time_to_live = 90, + }) end - - signals = ent.surface.find_entities_filtered{position = ent.position, radius = 2, name = "rail-signal"} - for i,s in ipairs(signals) do + + signals = ent.surface.find_entities_filtered({ position = ent.position, radius = 2, name = "rail-signal" }) + for i, s in ipairs(signals) do rail_s_count = rail_s_count + 1 - rendering.draw_circle{color = {0.5, 0.5, 1},radius = 2,width = 2,target = ent,surface = ent.surface,time_to_live = 90} + rendering.draw_circle({ + color = { 0.5, 0.5, 1 }, + radius = 2, + width = 2, + target = ent, + surface = ent.surface, + time_to_live = 90, + }) end - + if chain_s_count + rail_s_count == 0 then --(nothing) elseif chain_s_count + rail_s_count == 1 then @@ -140,27 +145,33 @@ function mod.rail_ent_info(pindex, ent, description) elseif chain_s_count + rail_s_count > 2 then result = result .. " with many signals, " end - + --Check if there is a train stop nearby, to announce station spaces if is_horz_or_vert then local stop = nil local segment_ent_1 = ent.get_rail_segment_entity(defines.rail_direction.front, false) local segment_ent_2 = ent.get_rail_segment_entity(defines.rail_direction.back, false) - if segment_ent_1 ~= nil and segment_ent_1.name == "train-stop" and util.distance(ent.position, segment_ent_1.position) < 45 then + if + segment_ent_1 ~= nil + and segment_ent_1.name == "train-stop" + and util.distance(ent.position, segment_ent_1.position) < 45 + then stop = segment_ent_1 - elseif segment_ent_2 ~= nil and segment_ent_2.name == "train-stop" and util.distance(ent.position, segment_ent_2.position) < 45 then + elseif + segment_ent_2 ~= nil + and segment_ent_2.name == "train-stop" + and util.distance(ent.position, segment_ent_2.position) < 45 + then stop = segment_ent_2 end - if stop == nil then - return result - end - + if stop == nil then return result end + --Check if this rail is in the correct direction of the train stop local rail_dir_1 = segment_ent_1 == stop local rail_dir_2 = segment_ent_2 == stop local stop_dir = stop.connected_rail_direction local pairing_correct = false - + if rail_dir_1 and stop_dir == defines.rail_direction.front then --result = result .. ", pairing 1, " pairing_correct = true @@ -177,11 +188,9 @@ function mod.rail_ent_info(pindex, ent, description) result = result .. ", pairing error, " pairing_correct = false end - - if not pairing_correct then - return result - end - + + if not pairing_correct then return result end + --Count distance and determine railcar slot local dist = util.distance(ent.position, stop.position) --result = result .. " stop distance " .. dist @@ -233,69 +242,81 @@ function mod.rail_ent_info(pindex, ent, description) result = result .. " station space 6 middle" end end - + return result end --Determines how many connections a rail has function mod.count_rail_connections(ent) - local front_left_rail,r_dir_back,c_dir_back = ent.get_connected_rail{ rail_direction = defines.rail_direction.front,rail_connection_direction = defines.rail_connection_direction.left} - local front_right_rail,r_dir_back,c_dir_back = ent.get_connected_rail{rail_direction = defines.rail_direction.front,rail_connection_direction = defines.rail_connection_direction.right} - local back_left_rail,r_dir_back,c_dir_back = ent.get_connected_rail{ rail_direction = defines.rail_direction.back,rail_connection_direction = defines.rail_connection_direction.left} - local back_right_rail,r_dir_back,c_dir_back = ent.get_connected_rail{rail_direction = defines.rail_direction.back,rail_connection_direction = defines.rail_connection_direction.right} - local next_rail,r_dir_back,c_dir_back = ent.get_connected_rail{rail_direction = defines.rail_direction.front, rail_connection_direction = defines.rail_connection_direction.straight} - local prev_rail,r_dir_back,c_dir_back = ent.get_connected_rail{rail_direction = defines.rail_direction.back, rail_connection_direction = defines.rail_connection_direction.straight} - + local front_left_rail, r_dir_back, c_dir_back = ent.get_connected_rail({ + rail_direction = defines.rail_direction.front, + rail_connection_direction = defines.rail_connection_direction.left, + }) + local front_right_rail, r_dir_back, c_dir_back = ent.get_connected_rail({ + rail_direction = defines.rail_direction.front, + rail_connection_direction = defines.rail_connection_direction.right, + }) + local back_left_rail, r_dir_back, c_dir_back = ent.get_connected_rail({ + rail_direction = defines.rail_direction.back, + rail_connection_direction = defines.rail_connection_direction.left, + }) + local back_right_rail, r_dir_back, c_dir_back = ent.get_connected_rail({ + rail_direction = defines.rail_direction.back, + rail_connection_direction = defines.rail_connection_direction.right, + }) + local next_rail, r_dir_back, c_dir_back = ent.get_connected_rail({ + rail_direction = defines.rail_direction.front, + rail_connection_direction = defines.rail_connection_direction.straight, + }) + local prev_rail, r_dir_back, c_dir_back = ent.get_connected_rail({ + rail_direction = defines.rail_direction.back, + rail_connection_direction = defines.rail_connection_direction.straight, + }) + local connection_count = 0 - if next_rail ~= nil then - connection_count = connection_count + 1 - end - if prev_rail ~= nil then - connection_count = connection_count + 1 - end - if front_left_rail ~= nil then - connection_count = connection_count + 1 - end - if front_right_rail ~= nil then - connection_count = connection_count + 1 - end - if back_left_rail ~= nil then - connection_count = connection_count + 1 - end - if back_right_rail ~= nil then - connection_count = connection_count + 1 - end + if next_rail ~= nil then connection_count = connection_count + 1 end + if prev_rail ~= nil then connection_count = connection_count + 1 end + if front_left_rail ~= nil then connection_count = connection_count + 1 end + if front_right_rail ~= nil then connection_count = connection_count + 1 end + if back_left_rail ~= nil then connection_count = connection_count + 1 end + if back_right_rail ~= nil then connection_count = connection_count + 1 end return connection_count end --Determines how many connections a rail has function mod.list_rail_fork_directions(ent) local result = "" - local front_left_rail,r_dir_back,c_dir_back = ent.get_connected_rail{ rail_direction = defines.rail_direction.front,rail_connection_direction = defines.rail_connection_direction.left} - local front_right_rail,r_dir_back,c_dir_back = ent.get_connected_rail{rail_direction = defines.rail_direction.front,rail_connection_direction = defines.rail_connection_direction.right} - local back_left_rail,r_dir_back,c_dir_back = ent.get_connected_rail{ rail_direction = defines.rail_direction.back,rail_connection_direction = defines.rail_connection_direction.left} - local back_right_rail,r_dir_back,c_dir_back = ent.get_connected_rail{rail_direction = defines.rail_direction.back,rail_connection_direction = defines.rail_connection_direction.right} - local next_rail,r_dir_back,c_dir_back = ent.get_connected_rail{rail_direction = defines.rail_direction.front, rail_connection_direction = defines.rail_connection_direction.straight} - local prev_rail,r_dir_back,c_dir_back = ent.get_connected_rail{rail_direction = defines.rail_direction.back, rail_connection_direction = defines.rail_connection_direction.straight} - - if next_rail ~= nil then - result = result .. "straight forward, " - end - if front_left_rail ~= nil then - result = result .. "left forward, " - end - if front_right_rail ~= nil then - result = result .. "right forward, " - end - if prev_rail ~= nil then - result = result .. "straight back, " - end - if back_left_rail ~= nil then - result = result .. "left back, " - end - if back_right_rail ~= nil then - result = result .. "right back, " - end + local front_left_rail, r_dir_back, c_dir_back = ent.get_connected_rail({ + rail_direction = defines.rail_direction.front, + rail_connection_direction = defines.rail_connection_direction.left, + }) + local front_right_rail, r_dir_back, c_dir_back = ent.get_connected_rail({ + rail_direction = defines.rail_direction.front, + rail_connection_direction = defines.rail_connection_direction.right, + }) + local back_left_rail, r_dir_back, c_dir_back = ent.get_connected_rail({ + rail_direction = defines.rail_direction.back, + rail_connection_direction = defines.rail_connection_direction.left, + }) + local back_right_rail, r_dir_back, c_dir_back = ent.get_connected_rail({ + rail_direction = defines.rail_direction.back, + rail_connection_direction = defines.rail_connection_direction.right, + }) + local next_rail, r_dir_back, c_dir_back = ent.get_connected_rail({ + rail_direction = defines.rail_direction.front, + rail_connection_direction = defines.rail_connection_direction.straight, + }) + local prev_rail, r_dir_back, c_dir_back = ent.get_connected_rail({ + rail_direction = defines.rail_direction.back, + rail_connection_direction = defines.rail_connection_direction.straight, + }) + + if next_rail ~= nil then result = result .. "straight forward, " end + if front_left_rail ~= nil then result = result .. "left forward, " end + if front_right_rail ~= nil then result = result .. "right forward, " end + if prev_rail ~= nil then result = result .. "straight back, " end + if back_left_rail ~= nil then result = result .. "left back, " end + if back_right_rail ~= nil then result = result .. "right back, " end return result end @@ -304,7 +325,7 @@ function mod.check_end_rail(check_rail, pindex) local is_end_rail = false local dir = -1 local comment = "Check function error." - + --Check if the entity is a rail if check_rail == nil then is_end_rail = false @@ -321,25 +342,32 @@ function mod.check_end_rail(check_rail, pindex) comment = "Not a rail." return is_end_rail, -1, comment end - + --Check if end rail: The rail is at the end of its segment and has only 1 connection. end_rail_1, end_dir_1 = check_rail.get_rail_segment_end(defines.rail_direction.front) end_rail_2, end_dir_2 = check_rail.get_rail_segment_end(defines.rail_direction.back) local connection_count = mod.count_rail_connections(check_rail) - if (check_rail.unit_number == end_rail_1.unit_number or check_rail.unit_number == end_rail_2.unit_number) and connection_count < 2 then + if + (check_rail.unit_number == end_rail_1.unit_number or check_rail.unit_number == end_rail_2.unit_number) + and connection_count < 2 + then --End rail confirmed, get direction is_end_rail = true comment = "End rail confirmed." - if connection_count == 0 then - comment = "single rail" - end + if connection_count == 0 then comment = "single rail" end if check_rail.name == "straight-rail" then - local next_rail_straight,temp1,temp2 = check_rail.get_connected_rail{rail_direction = defines.rail_direction.front, - rail_connection_direction = defines.rail_connection_direction.straight} - local next_rail_left,temp1,temp2 = check_rail.get_connected_rail{rail_direction = defines.rail_direction.front, - rail_connection_direction = defines.rail_connection_direction.left} - local next_rail_right,temp1,temp2 = check_rail.get_connected_rail{rail_direction = defines.rail_direction.front, - rail_connection_direction = defines.rail_connection_direction.right} + local next_rail_straight, temp1, temp2 = check_rail.get_connected_rail({ + rail_direction = defines.rail_direction.front, + rail_connection_direction = defines.rail_connection_direction.straight, + }) + local next_rail_left, temp1, temp2 = check_rail.get_connected_rail({ + rail_direction = defines.rail_direction.front, + rail_connection_direction = defines.rail_connection_direction.left, + }) + local next_rail_right, temp1, temp2 = check_rail.get_connected_rail({ + rail_direction = defines.rail_direction.front, + rail_connection_direction = defines.rail_connection_direction.right, + }) local next_rail = nil if next_rail_straight ~= nil then next_rail = next_rail_straight @@ -348,12 +376,18 @@ function mod.check_end_rail(check_rail, pindex) elseif next_rail_right ~= nil then next_rail = next_rail_right end - local prev_rail_straight,temp1,temp2 = check_rail.get_connected_rail{rail_direction = defines.rail_direction.back, - rail_connection_direction = defines.rail_connection_direction.straight} - local prev_rail_left,temp1,temp2 = check_rail.get_connected_rail{rail_direction = defines.rail_direction.back, - rail_connection_direction = defines.rail_connection_direction.left} - local prev_rail_right,temp1,temp2 = check_rail.get_connected_rail{rail_direction = defines.rail_direction.back, - rail_connection_direction = defines.rail_connection_direction.right} + local prev_rail_straight, temp1, temp2 = check_rail.get_connected_rail({ + rail_direction = defines.rail_direction.back, + rail_connection_direction = defines.rail_connection_direction.straight, + }) + local prev_rail_left, temp1, temp2 = check_rail.get_connected_rail({ + rail_direction = defines.rail_direction.back, + rail_connection_direction = defines.rail_connection_direction.left, + }) + local prev_rail_right, temp1, temp2 = check_rail.get_connected_rail({ + rail_direction = defines.rail_direction.back, + rail_connection_direction = defines.rail_connection_direction.right, + }) local prev_rail = nil if prev_rail_straight ~= nil then prev_rail = prev_rail_straight @@ -400,11 +434,15 @@ function mod.check_end_rail(check_rail, pindex) comment = "Rail direction error." return is_end_rail, -3, comment end - elseif check_rail.name == "curved-rail" then - local next_rail,r_dir_back,c_dir_back = check_rail.get_connected_rail{rail_direction = defines.rail_direction.front, - rail_connection_direction = defines.rail_connection_direction.straight} - local prev_rail,r_dir_back,c_dir_back = check_rail.get_connected_rail{rail_direction = defines.rail_direction.back, - rail_connection_direction = defines.rail_connection_direction.straight} + elseif check_rail.name == "curved-rail" then + local next_rail, r_dir_back, c_dir_back = check_rail.get_connected_rail({ + rail_direction = defines.rail_direction.front, + rail_connection_direction = defines.rail_connection_direction.straight, + }) + local prev_rail, r_dir_back, c_dir_back = check_rail.get_connected_rail({ + rail_direction = defines.rail_direction.back, + rail_connection_direction = defines.rail_connection_direction.straight, + }) if check_rail.direction == dirs.north and next_rail == nil then dir = dirs.south elseif check_rail.direction == dirs.north and prev_rail == nil then @@ -450,7 +488,7 @@ function mod.check_end_rail(check_rail, pindex) comment = "This rail is not the end rail." return is_end_rail, -4, comment end - + return is_end_rail, dir, comment end @@ -459,10 +497,8 @@ function mod.cursor_is_at_straight_end_rail_tip(pindex) local p = game.get_player(pindex) local pos = players[pindex].cursor_pos --Get the rail at the cursor - local rails_at_cursor = p.surface.find_entities_filtered{name = "straight-rail",position = pos} - if rails_at_cursor == nil or #rails_at_cursor == 0 then - return false - end + local rails_at_cursor = p.surface.find_entities_filtered({ name = "straight-rail", position = pos }) + if rails_at_cursor == nil or #rails_at_cursor == 0 then return false end --Check if it is an end rail that faces a cardinal direction local rail_at_cursor = rails_at_cursor[1] local is_end_rail, dir, comment = mod.check_end_rail(rail_at_cursor, pindex) @@ -471,23 +507,21 @@ function mod.cursor_is_at_straight_end_rail_tip(pindex) end --Check if any rails around the cursor position have a different unit number local perimeter = {} - perimeter[1] = fa_utils.add_position(pos,{x = -1, y = -1}) - perimeter[2] = fa_utils.add_position(pos,{x = -1, y = 0}) - perimeter[3] = fa_utils.add_position(pos,{x = -1, y = 1}) - perimeter[4] = fa_utils.add_position(pos,{x = 0, y = -1}) - perimeter[5] = fa_utils.add_position(pos,{x = 0, y = 1}) - perimeter[6] = fa_utils.add_position(pos,{x = 1, y = -1}) - perimeter[7] = fa_utils.add_position(pos,{x = 1, y = 0}) - perimeter[8] = fa_utils.add_position(pos,{x = 1, y = 1}) + perimeter[1] = fa_utils.add_position(pos, { x = -1, y = -1 }) + perimeter[2] = fa_utils.add_position(pos, { x = -1, y = 0 }) + perimeter[3] = fa_utils.add_position(pos, { x = -1, y = 1 }) + perimeter[4] = fa_utils.add_position(pos, { x = 0, y = -1 }) + perimeter[5] = fa_utils.add_position(pos, { x = 0, y = 1 }) + perimeter[6] = fa_utils.add_position(pos, { x = 1, y = -1 }) + perimeter[7] = fa_utils.add_position(pos, { x = 1, y = 0 }) + perimeter[8] = fa_utils.add_position(pos, { x = 1, y = 1 }) for pos_p in perimeter do --Find rails, if any - local ents = p.surface.find_entities_filtered{name = {"straight-rail","curved-rail"},position = pos_p} + local ents = p.surface.find_entities_filtered({ name = { "straight-rail", "curved-rail" }, position = pos_p }) if ents ~= nil and #ents > 0 then for rail in ents do --For rails found, check whether the unit number is different - if rail.unit_number ~= rail_at_cursor.unit_number then - return false - end + if rail.unit_number ~= rail_at_cursor.unit_number then return false end end end end @@ -499,7 +533,7 @@ end function mod.start_ghost_rail_planning(pindex) --Notify the ghost rail planner starting players[pindex].ghost_rail_planning = true - players[pindex].ghost_rail_start_pos = {x = players[pindex].cursor_pos.x, y = players[pindex].cursor_pos.y} + players[pindex].ghost_rail_start_pos = { x = players[pindex].cursor_pos.x, y = players[pindex].cursor_pos.y } printout("Started ghost rail planner", pindex) end @@ -508,7 +542,8 @@ end function mod.end_ghost_rail_planning(pindex) local p = game.get_player(pindex) --Check if cursor is on screen OR if remote view is running - local on_screen = fa_mouse.cursor_position_is_on_screen_with_player_centered(pindex) == true or players[pindex].remote_view == true + local on_screen = fa_mouse.cursor_position_is_on_screen_with_player_centered(pindex) == true + or players[pindex].remote_view == true if not on_screen then p.clear_cursor() printout("Rail planner error: cursor was not on screen", pindex) @@ -517,7 +552,7 @@ function mod.end_ghost_rail_planning(pindex) --Check if too close local start_pos = players[pindex].ghost_rail_start_pos local end_pos = players[pindex].cursor_pos - local far_enough = 50 > util.distance(start_pos,end_pos) + local far_enough = 50 > util.distance(start_pos, end_pos) --Give warning and clear hand if too close if not far_enough then p.clear_cursor() @@ -528,10 +563,10 @@ function mod.end_ghost_rail_planning(pindex) --No errors, but rail planner may still fail at invalid placements. Clear the cursor anyway p.clear_cursor() - --Check whether there is a ghost rail at the cursor location (from before processing this action) + --Check whether there is a ghost rail at the cursor location (from before processing this action) --todo**** --Schedule to check whether successful (which can be verified by there being a rail ghost near the cursor 2 ticks later) - schedule(2,"call_to_check_ghost_rails",pindex) + schedule(2, "call_to_check_ghost_rails", pindex) end --WIP todo: Reports on whether the rail planning was successful based on whether there is a ghost rail near the cursor @@ -549,15 +584,15 @@ function mod.get_signal_state_info(signal) local result = "" if signal.name == "rail-signal" then state_id = signal.signal_state - state_lookup = fa_utils.into_lookup(defines.signal_state) - state_name = state_lookup[state_id] - result = state_name - elseif signal.name == "rail-chain-signal" then + state_lookup = fa_utils.into_lookup(defines.signal_state) + state_name = state_lookup[state_id] + result = state_name + elseif signal.name == "rail-chain-signal" then state_id = signal.chain_signal_state - state_lookup = fa_utils.into_lookup(defines.chain_signal_state) - state_name = state_lookup[state_id] - result = state_name - if state_name == "none_open" then result = "closed" end + state_lookup = fa_utils.into_lookup(defines.chain_signal_state) + state_name = state_lookup[state_id] + result = state_name + if state_name == "none_open" then result = "closed" end end return result end @@ -566,7 +601,7 @@ end function mod.get_rail_segment_other_end(rail) local end_rail_1, end_dir_1 = rail.get_rail_segment_end(defines.rail_direction.front) --Cannot be nil local end_rail_2, end_dir_2 = rail.get_rail_segment_end(defines.rail_direction.back) --Cannot be nil - + if rail.unit_number == end_rail_1.unit_number and rail.unit_number ~= end_rail_2.unit_number then return end_rail_2 elseif rail.unit_number ~= end_rail_1.unit_number and rail.unit_number == end_rail_2.unit_number then @@ -582,51 +617,73 @@ function mod.get_neighbor_rail_segment_end(rail, con_dir_in) local dir = con_dir_in or nil local requested_neighbor_rail_1 = nil local requested_neighbor_rail_2 = nil - local neighbor_rail,r_dir_back,c_dir_back = nil, nil, nil - + local neighbor_rail, r_dir_back, c_dir_back = nil, nil, nil + if dir ~= nil then --Check requested neighbor - requested_neighbor_rail_1, req_dir_1, req_con_dir_1 = rail.get_connected_rail{ rail_direction = defines.rail_direction.front,rail_connection_direction = dir} - requested_neighbor_rail_2, req_dir_2, req_con_dir_2 = rail.get_connected_rail{ rail_direction = defines.rail_direction.back ,rail_connection_direction = dir} + requested_neighbor_rail_1, req_dir_1, req_con_dir_1 = + rail.get_connected_rail({ rail_direction = defines.rail_direction.front, rail_connection_direction = dir }) + requested_neighbor_rail_2, req_dir_2, req_con_dir_2 = + rail.get_connected_rail({ rail_direction = defines.rail_direction.back, rail_connection_direction = dir }) if requested_neighbor_rail_1 ~= nil and not rail.is_rail_in_same_rail_segment_as(requested_neighbor_rail_1) then return requested_neighbor_rail_1, req_dir_1, req_con_dir_1 - elseif requested_neighbor_rail_2 ~= nil and not rail.is_rail_in_same_rail_segment_as(requested_neighbor_rail_2) then + elseif + requested_neighbor_rail_2 ~= nil and not rail.is_rail_in_same_rail_segment_as(requested_neighbor_rail_2) + then return requested_neighbor_rail_2, req_dir_2, req_con_dir_2 else return nil, nil, nil end - else + else --Try all 6 options until you get any - neighbor_rail,r_dir_back,c_dir_back = rail.get_connected_rail{rail_direction = defines.rail_direction.front, rail_connection_direction = defines.rail_connection_direction.straight} + neighbor_rail, r_dir_back, c_dir_back = rail.get_connected_rail({ + rail_direction = defines.rail_direction.front, + rail_connection_direction = defines.rail_connection_direction.straight, + }) if neighbor_rail ~= nil and not neighbor_rail.is_rail_in_same_rail_segment_as(rail) then - return neighbor_rail,r_dir_back,c_dir_back + return neighbor_rail, r_dir_back, c_dir_back end - - neighbor_rail,r_dir_back,c_dir_back = rail.get_connected_rail{rail_direction = defines.rail_direction.back, rail_connection_direction = defines.rail_connection_direction.straight} + + neighbor_rail, r_dir_back, c_dir_back = rail.get_connected_rail({ + rail_direction = defines.rail_direction.back, + rail_connection_direction = defines.rail_connection_direction.straight, + }) if neighbor_rail ~= nil and not neighbor_rail.is_rail_in_same_rail_segment_as(rail) then - return neighbor_rail,r_dir_back,c_dir_back + return neighbor_rail, r_dir_back, c_dir_back end - - neighbor_rail,r_dir_back,c_dir_back = rail.get_connected_rail{ rail_direction = defines.rail_direction.front,rail_connection_direction = defines.rail_connection_direction.left} + + neighbor_rail, r_dir_back, c_dir_back = rail.get_connected_rail({ + rail_direction = defines.rail_direction.front, + rail_connection_direction = defines.rail_connection_direction.left, + }) if neighbor_rail ~= nil and not neighbor_rail.is_rail_in_same_rail_segment_as(rail) then - return neighbor_rail,r_dir_back,c_dir_back + return neighbor_rail, r_dir_back, c_dir_back end - - neighbor_rail,r_dir_back,c_dir_back = rail.get_connected_rail{rail_direction = defines.rail_direction.front,rail_connection_direction = defines.rail_connection_direction.right} + + neighbor_rail, r_dir_back, c_dir_back = rail.get_connected_rail({ + rail_direction = defines.rail_direction.front, + rail_connection_direction = defines.rail_connection_direction.right, + }) if neighbor_rail ~= nil and not neighbor_rail.is_rail_in_same_rail_segment_as(rail) then - return neighbor_rail,r_dir_back,c_dir_back + return neighbor_rail, r_dir_back, c_dir_back end - - neighbor_rail,r_dir_back,c_dir_back = rail.get_connected_rail{ rail_direction = defines.rail_direction.back,rail_connection_direction = defines.rail_connection_direction.left} + + neighbor_rail, r_dir_back, c_dir_back = rail.get_connected_rail({ + rail_direction = defines.rail_direction.back, + rail_connection_direction = defines.rail_connection_direction.left, + }) if neighbor_rail ~= nil and not neighbor_rail.is_rail_in_same_rail_segment_as(rail) then - return neighbor_rail,r_dir_back,c_dir_back + return neighbor_rail, r_dir_back, c_dir_back end - - neighbor_rail,r_dir_back,c_dir_back = rail.get_connected_rail{rail_direction = defines.rail_direction.back,rail_connection_direction = defines.rail_connection_direction.right} + + neighbor_rail, r_dir_back, c_dir_back = rail.get_connected_rail({ + rail_direction = defines.rail_direction.back, + rail_connection_direction = defines.rail_connection_direction.right, + }) if neighbor_rail ~= nil and not neighbor_rail.is_rail_in_same_rail_segment_as(rail) then - return neighbor_rail,r_dir_back,c_dir_back + return neighbor_rail, r_dir_back, c_dir_back end - + return nil, nil, nil end end @@ -638,58 +695,58 @@ function mod.read_all_rail_segment_entities(pindex, rail) local message = "" local ent_f1 = rail.get_rail_segment_entity(defines.rail_direction.front, true) local ent_f2 = rail.get_rail_segment_entity(defines.rail_direction.front, false) - local ent_b1 = rail.get_rail_segment_entity(defines.rail_direction.back, true) - local ent_b2 = rail.get_rail_segment_entity(defines.rail_direction.back, false) - + local ent_b1 = rail.get_rail_segment_entity(defines.rail_direction.back, true) + local ent_b2 = rail.get_rail_segment_entity(defines.rail_direction.back, false) + if ent_f1 == nil then message = message .. "forward 1 is nil, " elseif ent_f1.name == "train-stop" then - message = message .. "forward 1 is train stop " .. ent_f1.backer_name .. ", " - elseif ent_f1.name == "rail-signal" then + message = message .. "forward 1 is train stop " .. ent_f1.backer_name .. ", " + elseif ent_f1.name == "rail-signal" then message = message .. "forward 1 is rails signal with signal " .. mod.get_signal_state_info(ent_f1) .. ", " - elseif ent_f1.name == "rail-chain-signal" then + elseif ent_f1.name == "rail-chain-signal" then message = message .. "forward 1 is chain signal with signal " .. mod.get_signal_state_info(ent_f1) .. ", " else - message = message .. "forward 1 is else, " .. ent_f1.name .. ", " + message = message .. "forward 1 is else, " .. ent_f1.name .. ", " end - + if ent_f2 == nil then message = message .. "forward 2 is nil, " elseif ent_f2.name == "train-stop" then - message = message .. "forward 2 is train stop " .. ent_f2.backer_name .. ", " - elseif ent_f2.name == "rail-signal" then + message = message .. "forward 2 is train stop " .. ent_f2.backer_name .. ", " + elseif ent_f2.name == "rail-signal" then message = message .. "forward 2 is rails signal with signal " .. mod.get_signal_state_info(ent_f2) .. ", " - elseif ent_f2.name == "rail-chain-signal" then + elseif ent_f2.name == "rail-chain-signal" then message = message .. "forward 2 is chain signal with signal " .. mod.get_signal_state_info(ent_f2) .. ", " else - message = message .. "forward 2 is else, " .. ent_f2.name .. ", " + message = message .. "forward 2 is else, " .. ent_f2.name .. ", " end - + if ent_b1 == nil then message = message .. "back 1 is nil, " elseif ent_b1.name == "train-stop" then - message = message .. "back 1 is train stop " .. ent_b1.backer_name .. ", " - elseif ent_b1.name == "rail-signal" then + message = message .. "back 1 is train stop " .. ent_b1.backer_name .. ", " + elseif ent_b1.name == "rail-signal" then message = message .. "back 1 is rails signal with signal " .. mod.get_signal_state_info(ent_b1) .. ", " - elseif ent_b1.name == "rail-chain-signal" then + elseif ent_b1.name == "rail-chain-signal" then message = message .. "back 1 is chain signal with signal " .. mod.get_signal_state_info(ent_b1) .. ", " else - message = message .. "back 1 is else, " .. ent_b1.name .. ", " + message = message .. "back 1 is else, " .. ent_b1.name .. ", " end - + if ent_b2 == nil then message = message .. "back 2 is nil, " elseif ent_b2.name == "train-stop" then - message = message .. "back 2 is train stop " .. ent_b2.backer_name .. ", " - elseif ent_b2.name == "rail-signal" then + message = message .. "back 2 is train stop " .. ent_b2.backer_name .. ", " + elseif ent_b2.name == "rail-signal" then message = message .. "back 2 is rails signal with signal " .. mod.get_signal_state_info(ent_b2) .. ", " - elseif ent_b2.name == "rail-chain-signal" then + elseif ent_b2.name == "rail-chain-signal" then message = message .. "back 2 is chain signal with signal " .. mod.get_signal_state_info(ent_b2) .. ", " else - message = message .. "back 2 is else, " .. ent_b2.name .. ", " + message = message .. "back 2 is else, " .. ent_b2.name .. ", " end - - printout(message,pindex) + + printout(message, pindex) return end @@ -709,28 +766,31 @@ function mod.identify_rail_segment_end_object(rail, dir_ahead, accept_only_forwa local result_entity_label = "" local result_extra = nil local result_is_forward = nil - + if rail == nil or rail.valid == false then --Error result_entity = nil result_entity_label = "missing rail" return result_entity, result_entity_label, result_extra, result_is_forward end - + --Correction: Flip the correct direction ahead for mismatching diagonal rails - if rail.name == "straight-rail" and (rail.direction == dirs.southwest or rail.direction == dirs.northwest) - or rail.name == "curved-rail" and (rail.direction == dirs.north or rail.direction == dirs.northeast or rail.direction == dirs.east or rail.direction == dirs.southeast) then + if + rail.name == "straight-rail" and (rail.direction == dirs.southwest or rail.direction == dirs.northwest) + or rail.name == "curved-rail" + and (rail.direction == dirs.north or rail.direction == dirs.northeast or rail.direction == dirs.east or rail.direction == dirs.southeast) + then dir_ahead = mod.get_opposite_rail_direction(dir_ahead) end - + local segment_last_rail = rail.get_rail_segment_end(dir_ahead) local entity_ahead = nil - local entity_ahead_forward = rail.get_rail_segment_entity(dir_ahead,false) - local entity_ahead_reverse = rail.get_rail_segment_entity(dir_ahead,true) - + local entity_ahead_forward = rail.get_rail_segment_entity(dir_ahead, false) + local entity_ahead_reverse = rail.get_rail_segment_entity(dir_ahead, true) + local segment_last_is_end_rail, end_rail_dir, comment = mod.check_end_rail(segment_last_rail, pindex) local segment_last_neighbor_count = mod.count_rail_connections(segment_last_rail) - + if entity_ahead_forward ~= nil then entity_ahead = entity_ahead_forward result_is_forward = true @@ -738,12 +798,12 @@ function mod.identify_rail_segment_end_object(rail, dir_ahead, accept_only_forwa entity_ahead = entity_ahead_reverse result_is_forward = false end - - if prefer_back == true and entity_ahead_reverse ~= nil and accept_only_forward == false then + + if prefer_back == true and entity_ahead_reverse ~= nil and accept_only_forward == false then entity_ahead = entity_ahead_reverse result_is_forward = false end - + --When no entity ahead, check if the segment end is an end rail or fork rail? if entity_ahead == nil then if segment_last_is_end_rail then @@ -760,33 +820,33 @@ function mod.identify_rail_segment_end_object(rail, dir_ahead, accept_only_forwa return result_entity, result_entity_label, result_extra, result_is_forward else --The neighbor of the segment end rail is either a fork or an end rail or has an entity instead - neighbor_rail, neighbor_r_dir, neighbor_c_dir = mod.get_neighbor_rail_segment_end(segment_last_rail, nil) - if neighbor_rail == nil then - --This must be a closed loop? - result_entity = nil - result_entity_label = "loop" + neighbor_rail, neighbor_r_dir, neighbor_c_dir = mod.get_neighbor_rail_segment_end(segment_last_rail, nil) + if neighbor_rail == nil then + --This must be a closed loop? + result_entity = nil + result_entity_label = "loop" result_extra = nil - return result_entity, result_entity_label, result_extra, result_is_forward - elseif mod.count_rail_connections(neighbor_rail) > 2 then - --The neighbor is a forking rail - result_entity = neighbor_rail - result_entity_label = "fork merge" + return result_entity, result_entity_label, result_extra, result_is_forward + elseif mod.count_rail_connections(neighbor_rail) > 2 then + --The neighbor is a forking rail + result_entity = neighbor_rail + result_entity_label = "fork merge" result_extra = nil - return result_entity, result_entity_label, result_extra, result_is_forward - elseif mod.count_rail_connections(neighbor_rail) == 1 then - --The neighbor is an end rail - local neighbor_is_end_rail, end_rail_dir, comment = mod.check_end_rail(neighbor_rail, pindex) - result_entity = neighbor_rail - result_entity_label = "neighbor end" + return result_entity, result_entity_label, result_extra, result_is_forward + elseif mod.count_rail_connections(neighbor_rail) == 1 then + --The neighbor is an end rail + local neighbor_is_end_rail, end_rail_dir, comment = mod.check_end_rail(neighbor_rail, pindex) + result_entity = neighbor_rail + result_entity_label = "neighbor end" result_extra = end_rail_dir - return result_entity, result_entity_label, result_extra, result_is_forward - else - --The neighbor rail should have an entity? + return result_entity, result_entity_label, result_extra, result_is_forward + else + --The neighbor rail should have an entity? result_entity = segment_last_rail - result_entity_label = "other rail" + result_entity_label = "other rail" result_extra = nil return result_entity, result_entity_label, result_extra, result_is_forward - end + end end --When entity ahead, check its type else @@ -815,50 +875,54 @@ function mod.identify_rail_segment_end_object(rail, dir_ahead, accept_only_forwa end end ---Reads out the nearest railway object ahead with relevant details. Skips to the next segment if needed. ---The output could be an end rail, junction rail, rail signal, chain signal, or train stop. +--Reads out the nearest railway object ahead with relevant details. Skips to the next segment if needed. +--The output could be an end rail, junction rail, rail signal, chain signal, or train stop. function mod.get_next_rail_entity_ahead(origin_rail, dir_ahead, only_this_segment) - local next_entity, next_entity_label, result_extra, next_is_forward = mod.identify_rail_segment_end_object(origin_rail, dir_ahead, false, false) + local next_entity, next_entity_label, result_extra, next_is_forward = + mod.identify_rail_segment_end_object(origin_rail, dir_ahead, false, false) local iteration_count = 1 local segment_end_ahead, dir_se = origin_rail.get_rail_segment_end(dir_ahead) local prev_rail = segment_end_ahead local current_rail = origin_rail local neighbor_r_dir = dir_ahead local neighbor_c_dir = nil - + --First correction for the train stop exception if next_entity_label == "train stop" and next_is_forward == false then - next_entity, next_entity_label, result_extra, next_is_forward = mod.identify_rail_segment_end_object(current_rail, neighbor_r_dir, true, false) + next_entity, next_entity_label, result_extra, next_is_forward = + mod.identify_rail_segment_end_object(current_rail, neighbor_r_dir, true, false) end - + --Skip all "other rail" cases - while not only_this_segment and next_entity_label == "other rail" and iteration_count < 100 do + while not only_this_segment and next_entity_label == "other rail" and iteration_count < 100 do if iteration_count % 2 == 1 then --Switch to neighboring segment current_rail, neighbor_r_dir, neighbor_c_dir = mod.get_neighbor_rail_segment_end(prev_rail, nil) prev_rail = current_rail - next_entity, next_entity_label, result_extra, next_is_forward = mod.identify_rail_segment_end_object(current_rail, neighbor_r_dir, false, true) + next_entity, next_entity_label, result_extra, next_is_forward = + mod.identify_rail_segment_end_object(current_rail, neighbor_r_dir, false, true) --Correction for the train stop exception if next_entity_label == "train stop" and next_is_forward == false then - next_entity, next_entity_label, result_extra, next_is_forward = mod.identify_rail_segment_end_object(current_rail, neighbor_r_dir, true, true) + next_entity, next_entity_label, result_extra, next_is_forward = + mod.identify_rail_segment_end_object(current_rail, neighbor_r_dir, true, true) end --Correction for flipped direction - if next_is_forward ~= nil then - next_is_forward = not next_is_forward - end + if next_is_forward ~= nil then next_is_forward = not next_is_forward end iteration_count = iteration_count + 1 else --Check other end of the segment. NOTE: Never got more than 2 iterations in tests so far... neighbor_r_dir = mod.get_opposite_rail_direction(neighbor_r_dir) - next_entity, next_entity_label, result_extra, next_is_forward = mod.identify_rail_segment_end_object(current_rail, neighbor_r_dir, false, false) + next_entity, next_entity_label, result_extra, next_is_forward = + mod.identify_rail_segment_end_object(current_rail, neighbor_r_dir, false, false) --Correction for the train stop exception if next_entity_label == "train stop" and next_is_forward == false then - next_entity, next_entity_label, result_extra, next_is_forward = mod.identify_rail_segment_end_object(current_rail, neighbor_r_dir, true, false) + next_entity, next_entity_label, result_extra, next_is_forward = + mod.identify_rail_segment_end_object(current_rail, neighbor_r_dir, true, false) end iteration_count = iteration_count + 1 end end - + return next_entity, next_entity_label, result_extra, next_is_forward, iteration_count end @@ -869,50 +933,52 @@ function mod.rail_read_next_rail_entity_ahead(pindex, rail, is_forward) local dir_ahead = defines.rail_direction.front if not is_forward then dir_ahead = defines.rail_direction.back - message = "Down this rail, " + message = "Down this rail, " end - local next_entity, next_entity_label, result_extra, next_is_forward, iteration_count = mod.get_next_rail_entity_ahead(origin_rail, dir_ahead, false) + local next_entity, next_entity_label, result_extra, next_is_forward, iteration_count = + mod.get_next_rail_entity_ahead(origin_rail, dir_ahead, false) if next_entity == nil then - printout("Analysis error. This rail might be looping.",pindex) + printout("Analysis error. This rail might be looping.", pindex) return end local distance = math.floor(util.distance(origin_rail.position, next_entity.position)) - + --Test message --message = message .. iteration_count .. " iterations, " - + --Maybe check for trains here, but there is no point because the checks use signal blocks... --local trains_in_origin_block = origin_rail.trains_in_block --local trains_in_current_block = current_rail.trains_in_block - + --Report opposite direction entities. - if next_is_forward == false and (next_entity_label == "train stop" or next_entity_label == "rail signal" or next_entity_label == "chain signal") then + if + next_is_forward == false + and ( + next_entity_label == "train stop" + or next_entity_label == "rail signal" + or next_entity_label == "chain signal" + ) + then message = message .. " Opposite direction's " end - + --Add more info depending on entity label if next_entity_label == "end rail" then message = message .. next_entity_label - elseif next_entity_label == "fork split" then - local entering_segment_rail = result_extra + local entering_segment_rail = result_extra message = message .. "rail fork splitting " message = message .. mod.list_rail_fork_directions(next_entity) - elseif next_entity_label == "fork merge" then - local entering_segment_rail = result_extra + local entering_segment_rail = result_extra message = message .. "rail fork merging " - elseif next_entity_label == "neighbor end" then - local entering_segment_rail = result_extra + local entering_segment_rail = result_extra message = message .. "end rail " - elseif next_entity_label == "rail signal" then message = message .. "rail signal with state " .. mod.get_signal_state_info(next_entity) .. " " - elseif next_entity_label == "chain signal" then message = message .. "chain signal with state " .. mod.get_signal_state_info(next_entity) .. " " - elseif next_entity_label == "train stop" then local stop_name = next_entity.backer_name --Add more specific distance info @@ -923,19 +989,17 @@ function mod.rail_read_next_rail_entity_ahead(pindex, rail, is_forward) if math.abs(distance) <= 0.2 then message = " Aligned with train stop " .. stop_name elseif distance > 0.2 then - message = math.floor(distance * 10) / 10 .. " meters away from train stop " .. stop_name .. ". " + message = math.floor(distance * 10) / 10 .. " meters away from train stop " .. stop_name .. ". " elseif distance < 0.2 then - message = math.floor((-distance) * 10) / 10 .. " meters past train stop " .. stop_name .. ". " + message = math.floor(-distance * 10) / 10 .. " meters past train stop " .. stop_name .. ". " end end - elseif next_entity_label == "other rail" then message = message .. "unspecified entity" - elseif next_entity_label == "other entity" then message = message .. next_entity.name end - + --Add general distance info if next_entity_label ~= "train stop" then message = message .. " in " .. distance .. " meters, " @@ -943,13 +1007,20 @@ function mod.rail_read_next_rail_entity_ahead(pindex, rail, is_forward) message = message .. " facing " .. fa_utils.direction_lookup(result_extra) end end - printout(message,pindex) + printout(message, pindex) --Draw circles for visual debugging - rendering.draw_circle{color = {0, 1, 0},radius = 1,width = 10,target = next_entity,surface = next_entity.surface,time_to_live = 100} + rendering.draw_circle({ + color = { 0, 1, 0 }, + radius = 1, + width = 10, + target = next_entity, + surface = next_entity.surface, + time_to_live = 100, + }) end --WIP. laterdo here: Rail analyzer menu where you will use arrow keys to go forward/back and left/right along a rail. -function mod.run_rail_analyzer_menu(pindex, origin_rail,is_called_from_train) +function mod.run_rail_analyzer_menu(pindex, origin_rail, is_called_from_train) return end @@ -962,19 +1033,26 @@ function mod.count_rails_within_range(rail, range, pindex) --1. Scan around the rail for other rails local counter = 0 local pos = rail.position - local scan_area = {{pos.x-range,pos.y-range},{pos.x+range,pos.y+range}} - local ents = game.get_player(pindex).surface.find_entities_filtered{area = scan_area, name = "straight-rail"} - for i,other_rail in ipairs(ents) do + local scan_area = { { pos.x - range, pos.y - range }, { pos.x + range, pos.y + range } } + local ents = game.get_player(pindex).surface.find_entities_filtered({ area = scan_area, name = "straight-rail" }) + for i, other_rail in ipairs(ents) do --2. Increase counter for each straight rail - counter = counter + 1 + counter = counter + 1 end - ents = game.get_player(pindex).surface.find_entities_filtered{area = scan_area, name = "curved-rail"} - for i,other_rail in ipairs(ents) do + ents = game.get_player(pindex).surface.find_entities_filtered({ area = scan_area, name = "curved-rail" }) + for i, other_rail in ipairs(ents) do --3. Increase counter for each curved rail - counter = counter + 1 + counter = counter + 1 end --Draw the range for visual debugging - rendering.draw_circle{color = {0, 1, 0}, radius = range, width = range, target = rail, surface = rail.surface,time_to_live = 100} + rendering.draw_circle({ + color = { 0, 1, 0 }, + radius = range, + width = range, + target = rail, + surface = rail.surface, + time_to_live = 100, + }) return counter end @@ -984,22 +1062,27 @@ function mod.has_parallel_neighbor(rail, pindex) local pos = rail.position local dir = rail.direction local range = 4 - if dir % 2 == 1 then - range = 3 - end - local scan_area = {{pos.x-range,pos.y-range},{pos.x+range,pos.y+range}} - local ents = game.get_player(pindex).surface.find_entities_filtered{area = scan_area, name = "straight-rail"} - for i,other_rail in ipairs(ents) do - --2. For each rail, does it have the same rotation but a different segment? If yes return true. - local pos2 = other_rail.position - if rail.direction == other_rail.direction and not rail.is_rail_in_same_rail_segment_as(other_rail) then - --3. Also ignore cases where the rails are directly facing each other so that they can be connected - if (pos.x ~= pos2.x) and (pos.y ~= pos2.y) and (math.abs(pos.x - pos2.x) - math.abs(pos.y - pos2.y)) > 1 then - --4. Parallel neighbor found - rendering.draw_circle{color = {1, 0, 0},radius = range,width = range,target = pos,surface = rail.surface,time_to_live = 100} - return true - end - end + if dir % 2 == 1 then range = 3 end + local scan_area = { { pos.x - range, pos.y - range }, { pos.x + range, pos.y + range } } + local ents = game.get_player(pindex).surface.find_entities_filtered({ area = scan_area, name = "straight-rail" }) + for i, other_rail in ipairs(ents) do + --2. For each rail, does it have the same rotation but a different segment? If yes return true. + local pos2 = other_rail.position + if rail.direction == other_rail.direction and not rail.is_rail_in_same_rail_segment_as(other_rail) then + --3. Also ignore cases where the rails are directly facing each other so that they can be connected + if (pos.x ~= pos2.x) and (pos.y ~= pos2.y) and (math.abs(pos.x - pos2.x) - math.abs(pos.y - pos2.y)) > 1 then + --4. Parallel neighbor found + rendering.draw_circle({ + color = { 1, 0, 0 }, + radius = range, + width = range, + target = pos, + surface = rail.surface, + time_to_live = 100, + }) + return true + end + end end --4. No parallel neighbor found return false @@ -1010,17 +1093,24 @@ function mod.is_intersection_rail(rail, pindex) --1. Scan around the rail for other rails local pos = rail.position local dir = rail.direction - local scan_area = {{pos.x-1,pos.y-1},{pos.x+1,pos.y+1}} - local ents = game.get_player(pindex).surface.find_entities_filtered{area = scan_area, name = "straight-rail"} - for i,other_rail in ipairs(ents) do + local scan_area = { { pos.x - 1, pos.y - 1 }, { pos.x + 1, pos.y + 1 } } + local ents = game.get_player(pindex).surface.find_entities_filtered({ area = scan_area, name = "straight-rail" }) + for i, other_rail in ipairs(ents) do --2. For each rail, does it have a different rotation and a different segment? If yes return true. - local dir_2 = other_rail.direction - dir = dir % dirs.south --N/S or E/W does not matter - dir_2 = dir_2 % dirs.south --N/S or E/W does not matter - if dir ~= dir_2 and not rail.is_rail_in_same_rail_segment_as(other_rail) then - rendering.draw_circle{color = {0, 0, 1},radius = 1.5,width = 1.5,target = pos,surface = rail.surface,time_to_live = 100} + local dir_2 = other_rail.direction + dir = dir % dirs.south --N/S or E/W does not matter + dir_2 = dir_2 % dirs.south --N/S or E/W does not matter + if dir ~= dir_2 and not rail.is_rail_in_same_rail_segment_as(other_rail) then + rendering.draw_circle({ + color = { 0, 0, 1 }, + radius = 1.5, + width = 1.5, + target = pos, + surface = rail.surface, + time_to_live = 100, + }) return true - end + end end return false end @@ -1029,38 +1119,48 @@ function mod.find_nearest_intersection(rail, pindex, radius_in) --1. Scan around the rail for other rails local radius = radius_in or 1000 local pos = rail.position - local scan_area = {{pos.x-radius,pos.y-radius},{pos.x+radius,pos.y+radius}} - local ents = game.get_player(pindex).surface.find_entities_filtered{area = scan_area, name = {"straight-rail","curved-rail"}} + local scan_area = { { pos.x - radius, pos.y - radius }, { pos.x + radius, pos.y + radius } } + local ents = game + .get_player(pindex).surface + .find_entities_filtered({ area = scan_area, name = { "straight-rail", "curved-rail" } }) local nearest = nil local min_dist = radius - for i,other_rail in ipairs(ents) do + for i, other_rail in ipairs(ents) do --2. For each rail, is it an intersection rail? if other_rail.valid and mod.is_intersection_rail(other_rail, pindex) then local dist = math.ceil(util.distance(pos, other_rail.position)) - --Set as nearest if valid - if dist < min_dist then - min_dist = dist - nearest = other_rail - end + --Set as nearest if valid + if dist < min_dist then + min_dist = dist + nearest = other_rail + end end end --Return the nearest found, possibly nil if nearest == nil then - return nil, radius --Nothing within radius tiles! + return nil, radius --Nothing within radius tiles! end - rendering.draw_circle{color = {0, 0, 1}, radius = 2, width = 2, target = nearest.position, surface = nearest.surface, time_to_live = 60} + rendering.draw_circle({ + color = { 0, 0, 1 }, + radius = 2, + width = 2, + target = nearest.position, + surface = nearest.surface, + time_to_live = 60, + }) return nearest, min_dist end --Mines for the player the rail signals around a rail. -function mod.mine_signals(rail,pindex) - local chains = rail.surface.find_entities_filtered{position = rail.position, radius = 2, name = "rail-chain-signal"} - for i,chain in ipairs(chains) do - game.get_player(pindex).mine_entity(chain,true) +function mod.mine_signals(rail, pindex) + local chains = + rail.surface.find_entities_filtered({ position = rail.position, radius = 2, name = "rail-chain-signal" }) + for i, chain in ipairs(chains) do + game.get_player(pindex).mine_entity(chain, true) end - local signals = rail.surface.find_entities_filtered{position = rail.position, radius = 2, name = "rail-signal"} - for i,signal in ipairs(signals) do - game.get_player(pindex).mine_entity(signal,true) + local signals = rail.surface.find_entities_filtered({ position = rail.position, radius = 2, name = "rail-signal" }) + for i, signal in ipairs(signals) do + game.get_player(pindex).mine_entity(signal, true) end end @@ -1069,78 +1169,175 @@ function mod.check_and_play_train_track_alert_sounds(step) for pindex, player in pairs(players) do --Check if the player is standing on a rail local p = game.get_player(pindex) - local floor_ent = p.surface.find_entities_filtered{position = p.position, limit = 1}[1] + local floor_ent = p.surface.find_entities_filtered({ position = p.position, limit = 1 })[1] local facing_ent = players[p.index].tile.ents[1] local found_rail = nil local skip = false if p.driving then skip = true - elseif floor_ent ~= nil and floor_ent.valid and (floor_ent.name == "straight-rail" or floor_ent.name == "curved-rail") then + elseif + floor_ent ~= nil + and floor_ent.valid + and (floor_ent.name == "straight-rail" or floor_ent.name == "curved-rail") + then found_rail = floor_ent - elseif facing_ent ~= nil and facing_ent.valid and (facing_ent.name == "straight-rail" or facing_ent.name == "curved-rail") then + elseif + facing_ent ~= nil + and facing_ent.valid + and (facing_ent.name == "straight-rail" or facing_ent.name == "curved-rail") + then found_rail = facing_ent else --Check further around the player because the other scans do not cover the back - local floor_ent_2 = p.surface.find_entities_filtered{name = {"straight-rail","curved-rail"}, position = p.position, radius = 1, limit = 1}[1] + local floor_ent_2 = p.surface.find_entities_filtered({ + name = { "straight-rail", "curved-rail" }, + position = p.position, + radius = 1, + limit = 1, + })[1] if floor_ent_2 ~= nil and floor_ent_2.valid then found_rail = floor_ent_2 else skip = true end end - + --Condition for step 1: Any moving trains nearby (within 400 tiles) if not skip and step == 1 then local trains = p.surface.get_trains() - for i,train in ipairs(trains) do - if train.speed ~= 0 and (util.distance(p.position,train.front_stock.position) < 400 or util.distance(p.position,train.back_stock.position) < 400) then - p.play_sound{path = "train-alert-low"} - rendering.draw_circle{color = {1, 1, 0},radius = 2,width = 2,target = found_rail.position,surface = found_rail.surface,time_to_live = 15} + for i, train in ipairs(trains) do + if + train.speed ~= 0 + and ( + util.distance(p.position, train.front_stock.position) < 400 + or util.distance(p.position, train.back_stock.position) < 400 + ) + then + p.play_sound({ path = "train-alert-low" }) + rendering.draw_circle({ + color = { 1, 1, 0 }, + radius = 2, + width = 2, + target = found_rail.position, + surface = found_rail.surface, + time_to_live = 15, + }) end end --Condition for step 2: Any moving trains nearby (within 200 tiles), and heading towards the player elseif not skip and step == 2 then - local trains = p.surface.get_trains() - for i,train in ipairs(trains) do - if train.speed ~= 0 and (util.distance(p.position,train.front_stock.position) < 200 or util.distance(p.position,train.back_stock.position) < 200) - and ((train.speed > 0 and util.distance(p.position,train.front_stock.position) <= util.distance(p.position,train.back_stock.position)) - or (train.speed < 0 and util.distance(p.position,train.front_stock.position) >= util.distance(p.position,train.back_stock.position))) then - p.play_sound{path = "train-alert-low"} - rendering.draw_circle{color = {1, 0.5, 0},radius = 3,width = 4,target = found_rail.position,surface = found_rail.surface,time_to_live = 15} + local trains = p.surface.get_trains() + for i, train in ipairs(trains) do + if + train.speed ~= 0 + and (util.distance(p.position, train.front_stock.position) < 200 or util.distance( + p.position, + train.back_stock.position + ) < 200) + and ( + ( + train.speed > 0 + and util.distance(p.position, train.front_stock.position) + <= util.distance(p.position, train.back_stock.position) + ) + or ( + train.speed < 0 + and util.distance(p.position, train.front_stock.position) + >= util.distance(p.position, train.back_stock.position) + ) + ) + then + p.play_sound({ path = "train-alert-low" }) + rendering.draw_circle({ + color = { 1, 0.5, 0 }, + radius = 3, + width = 4, + target = found_rail.position, + surface = found_rail.surface, + time_to_live = 15, + }) end end --Condition for step 3: Any moving trains in the same rail block, and heading towards the player OR if the block inbound signals are yellow. More urgent sound if also within 200 distance of the player elseif not skip and step == 3 then local trains = p.surface.get_trains() - for i,train in ipairs(trains) do - if train.speed ~= 0 and (found_rail.is_rail_in_same_rail_block_as(train.front_rail) or found_rail.is_rail_in_same_rail_block_as(train.back_rail)) - and ((train.speed > 0 and util.distance(p.position,train.front_stock.position) <= util.distance(p.position,train.back_stock.position)) - or (train.speed < 0 and util.distance(p.position,train.front_stock.position) >= util.distance(p.position,train.back_stock.position))) then - if (util.distance(p.position,train.front_stock.position) < 200 or util.distance(p.position,train.back_stock.position) < 200) then - p.play_sound{path = "train-alert-high"} - rendering.draw_circle{color = {1, 0.0, 0},radius = 4,width = 8,target = found_rail.position,surface = found_rail.surface,time_to_live = 15} + for i, train in ipairs(trains) do + if + train.speed ~= 0 + and (found_rail.is_rail_in_same_rail_block_as(train.front_rail) or found_rail.is_rail_in_same_rail_block_as( + train.back_rail + )) + and ( + ( + train.speed > 0 + and util.distance(p.position, train.front_stock.position) + <= util.distance(p.position, train.back_stock.position) + ) + or ( + train.speed < 0 + and util.distance(p.position, train.front_stock.position) + >= util.distance(p.position, train.back_stock.position) + ) + ) + then + if + util.distance(p.position, train.front_stock.position) < 200 + or util.distance(p.position, train.back_stock.position) < 200 + then + p.play_sound({ path = "train-alert-high" }) + rendering.draw_circle({ + color = { 1, 0.0, 0 }, + radius = 4, + width = 8, + target = found_rail.position, + surface = found_rail.surface, + time_to_live = 15, + }) else - p.play_sound{path = "train-alert-low"} - rendering.draw_circle{color = {1, 0.4, 0},radius = 4,width = 8,target = found_rail.position,surface = found_rail.surface,time_to_live = 15} + p.play_sound({ path = "train-alert-low" }) + rendering.draw_circle({ + color = { 1, 0.4, 0 }, + radius = 4, + width = 8, + target = found_rail.position, + surface = found_rail.surface, + time_to_live = 15, + }) end end end local signals = found_rail.get_inbound_signals() - for i,signal in ipairs(signals) do + for i, signal in ipairs(signals) do if signal.signal_state == defines.signal_state.reserved then - for i,train in ipairs(trains) do - if (util.distance(p.position,train.front_stock.position) < 200 or util.distance(p.position,train.back_stock.position) < 200) then - p.play_sound{path = "train-alert-high"} - rendering.draw_circle{color = {1, 0.0, 0},radius = 4,width = 8,target = found_rail.position,surface = found_rail.surface,time_to_live = 15} + for i, train in ipairs(trains) do + if + util.distance(p.position, train.front_stock.position) < 200 + or util.distance(p.position, train.back_stock.position) < 200 + then + p.play_sound({ path = "train-alert-high" }) + rendering.draw_circle({ + color = { 1, 0.0, 0 }, + radius = 4, + width = 8, + target = found_rail.position, + surface = found_rail.surface, + time_to_live = 15, + }) else - p.play_sound{path = "train-alert-low"} - rendering.draw_circle{color = {1, 0.4, 0},radius = 4,width = 8,target = found_rail.position,surface = found_rail.surface,time_to_live = 15} + p.play_sound({ path = "train-alert-low" }) + rendering.draw_circle({ + color = { 1, 0.4, 0 }, + radius = 4, + width = 8, + target = found_rail.position, + surface = found_rail.surface, + time_to_live = 15, + }) end end end end end - end end diff --git a/scripts/scanner.lua b/scripts/scanner.lua index 76f1f355..6c811ede 100644 --- a/scripts/scanner.lua +++ b/scripts/scanner.lua @@ -1,32 +1,37 @@ --Here: Functions relating to the scanner tool --Does not include event handlers directly, but can have functions called by them. -local util = require('util') +local util = require("util") local fa_utils = require("scripts.fa-utils") -local localising = require('scripts.localising') +local localising = require("scripts.localising") local dirs = defines.direction local fa_graphics = require("scripts.graphics") local fa_building_tools = require("scripts.building-tools") local fa_trains = require("scripts.trains") -local fa_zoom = require('scripts.zoom') +local fa_zoom = require("scripts.zoom") local fa_bot_logistics = require("scripts.worker-robots") -local mod = {} +local mod = {} --Find islands of resources or water or trees to create the aggregate entries in the scanner list. Does not run for every scan. function mod.find_islands(surf, area, pindex) local islands = {} - local ents = surf.find_entities_filtered{area = area, type = "resource"} - local waters = surf.find_tiles_filtered{area = area, name = "water"} - local trents = surf.find_entities_filtered{area = area, type = "tree"} --- if trents ~= nil and #trents > 0 then printout("trees galore", pindex) end + local ents = surf.find_entities_filtered({ area = area, type = "resource" }) + local waters = surf.find_tiles_filtered({ area = area, name = "water" }) + local trents = surf.find_entities_filtered({ area = area, type = "tree" }) + -- if trents ~= nil and #trents > 0 then printout("trees galore", pindex) end local i = 1 while i <= #trents do local trent = trents[i] - local check = (trent.position.x >= area.left_top.x and trent.position.y >= area.left_top.y and trent.position.x < area.right_bottom.x and trent.position.y < area.right_bottom.y) - + local check = ( + trent.position.x >= area.left_top.x + and trent.position.y >= area.left_top.y + and trent.position.x < area.right_bottom.x + and trent.position.y < area.right_bottom.y + ) + if check == false then - table.remove(trents, i) + table.remove(trents, i) else - i = i + 1 + i = i + 1 end end if #trents > 0 then @@ -36,18 +41,19 @@ function mod.find_islands(surf, area, pindex) for i, ent in ipairs(ents) do local destroy_id = script.register_on_entity_destroyed(ent) - players[pindex].destroyed[destroy_id] = {name = ent.name, position = ent.position, type = ent.type, area = ent.bounding_box} + players[pindex].destroyed[destroy_id] = + { name = ent.name, position = ent.position, type = ent.type, area = ent.bounding_box } if islands[ent.name] == nil then islands[ent.name] = { name = ent.name, groups = {}, resources = {}, edges = {}, - neighbors = {} + neighbors = {}, } end - islands[ent.name].groups[i] = {fa_utils.pos2str(ent.position)} - islands[ent.name].resources[fa_utils.pos2str(ent.position)] = {group=i, edge = false} + islands[ent.name].groups[i] = { fa_utils.pos2str(ent.position) } + islands[ent.name].resources[fa_utils.pos2str(ent.position)] = { group = i, edge = false } end if #waters > 0 then islands["water"] = { @@ -55,14 +61,14 @@ function mod.find_islands(surf, area, pindex) groups = {}, resources = {}, edges = {}, - neighbors = {} + neighbors = {}, } end for i, water in pairs(waters) do local str = fa_utils.pos2str(water.position) if islands["water"].resources[str] == nil then - islands["water"].groups[i] = {str} - islands["water"].resources[str] = {group=i, edge = false} + islands["water"].groups[i] = { str } + islands["water"].resources[str] = { group = i, edge = false } end end if #trents > 0 then @@ -71,23 +77,24 @@ function mod.find_islands(surf, area, pindex) groups = {}, resources = {}, edges = {}, - neighbors = {} + neighbors = {}, } end for i, trent in pairs(trents) do local destroy_id = script.register_on_entity_destroyed(trent) - players[pindex].destroyed[destroy_id] = {name = trent.name, position = trent.position, type = trent.type, area = trent.bounding_box} + players[pindex].destroyed[destroy_id] = + { name = trent.name, position = trent.position, type = trent.type, area = trent.bounding_box } local pos = table.deepcopy(trent.position) - pos.x = math.floor(pos.x/8) - pos.y = math.floor(pos.y/8) + pos.x = math.floor(pos.x / 8) + pos.y = math.floor(pos.y / 8) local str = fa_utils.pos2str(pos) if islands["forest"].resources[str] == nil then - islands["forest"].groups[i] = {str} - islands["forest"].resources[str] = {group=i, edge = false, count = 1} - else + islands["forest"].groups[i] = { str } + islands["forest"].resources[str] = { group = i, edge = false, count = 1 } + else islands["forest"].resources[str].count = islands["forest"].resources[str].count + 1 end end @@ -97,7 +104,7 @@ function mod.find_islands(surf, area, pindex) local position = fa_utils.str2pos(pos) local adj = {} for dir = 0, 7 do - adj[dir] = fa_utils.pos2str(fa_utils.offset_position(position, dir, 1)) + adj[dir] = fa_utils.pos2str(fa_utils.offset_position(position, dir, 1)) end local new_group = resource.group for dir, index in ipairs(adj) do @@ -105,24 +112,24 @@ function mod.find_islands(surf, area, pindex) resource.edge = true else new_group = math.min(new_group, entry.resources[index].group) - end + end end if resource.edge then --- table.insert(entry.edges, pos) + -- table.insert(entry.edges, pos) entry.edges[pos] = false if fa_utils.area_edge(area, 0, position, name) then entry.neighbors[0] = true - entry.edges[pos] = true + entry.edges[pos] = true end if fa_utils.area_edge(area, 6, position, name) then entry.neighbors[6] = true - entry.edges[pos] = true + entry.edges[pos] = true end if fa_utils.area_edge(area, 4, position, name) then entry.neighbors[4] = true - entry.edges[pos] = true - end - if fa_utils.area_edge(area, 2, position, name) then + entry.edges[pos] = true + end + if fa_utils.area_edge(area, 2, position, name) then entry.neighbors[2] = true entry.edges[pos] = true end @@ -138,132 +145,139 @@ function mod.find_islands(surf, area, pindex) entry.groups[old_group] = nil end end - end end return islands end --Run any sort of scan -function mod.scan_area(x,y,w,h, pindex, filter_direction, start_with_existing_list, close_object_limit_in) +function mod.scan_area(x, y, w, h, pindex, filter_direction, start_with_existing_list, close_object_limit_in) local first_player = game.get_player(pindex) local surf = first_player.surface - local ents = surf.find_entities_filtered{area = {{x, y},{x+w, y+h}}, type = {"resource", "tree", "highlight-box", "flying-text"}, invert = true} --Get all ents in the area except for these types + local ents = surf.find_entities_filtered({ + area = { { x, y }, { x + w, y + h } }, + type = { "resource", "tree", "highlight-box", "flying-text" }, + invert = true, + }) --Get all ents in the area except for these types local result = {} - if start_with_existing_list == true then - result = players[pindex].nearby.ents - end + if start_with_existing_list == true then result = players[pindex].nearby.ents end local pos = players[pindex].position local forest_density = nil local close_object_limit = close_object_limit_in or 10.1 - + --Find the nearest edges of already-loaded resource groups according to player pos, and insert them to the initial list as aggregates for name, resource in pairs(players[pindex].resources) do - --Insert scanner entries - table.insert(result, {name = name, count = table_size(players[pindex].resources[name].patches), ents = {}, aggregate = true}) + --Insert scanner entries + table.insert( + result, + { name = name, count = table_size(players[pindex].resources[name].patches), ents = {}, aggregate = true } + ) --Insert instances for the entry local index = #result for group, patch in pairs(resource.patches) do local nearest_edge = fa_utils.nearest_edge(patch.edges, pos, name) --Filter check 1: Is the entity in the filter diection? (If a filter is set at all) - local dir_of_ent = fa_utils.get_direction_biased(nearest_edge,pos) + local dir_of_ent = fa_utils.get_direction_biased(nearest_edge, pos) local filter_passed = (filter_direction == nil or filter_direction == dir_of_ent) if not filter_passed then --Filter check 2: Is the entity nearby and almost within the filter diection? - if util.distance(nearest_edge,pos) < close_object_limit then - local new_dir_of_ent = fa_utils.get_direction_precise(nearest_edge,pos)--Check with less bias towards diagonal directions to preserve 135 degrees FOV + if util.distance(nearest_edge, pos) < close_object_limit then + local new_dir_of_ent = fa_utils.get_direction_precise(nearest_edge, pos) --Check with less bias towards diagonal directions to preserve 135 degrees FOV local CW_dir = (filter_direction + dirs.northeast) % (2 * dirs.south) local CCW_dir = (filter_direction - dirs.northeast) % (2 * dirs.south) - filter_passed = (new_dir_of_ent == filter_direction or new_dir_of_ent == CW_dir or new_dir_of_ent == CCW_dir) + filter_passed = ( + new_dir_of_ent == filter_direction + or new_dir_of_ent == CW_dir + or new_dir_of_ent == CCW_dir + ) end end - if filter_passed then + if filter_passed then --If it is a forest, check density if name == "forest" then local forest_pos = nearest_edge - forest_density = mod.classify_forest(forest_pos,pindex,false) + forest_density = mod.classify_forest(forest_pos, pindex, false) else forest_density = nil end --Insert to the list if this group is not a forest at all, or not an empty or tiny forest - if forest_density == nil or (forest_density ~= "empty" and forest_density ~= "patch") then - table.insert(result[index].ents, {group = group, position = nearest_edge}) + if forest_density == nil or (forest_density ~= "empty" and forest_density ~= "patch") then + table.insert(result[index].ents, { group = group, position = nearest_edge }) end end end --Remove empty entries if result[index].ents == nil or result[index].ents == {} or result[index].ents[1] == nil then - table.remove(result,index) + table.remove(result, index) end end --Insert entities to the initial list - for i=1, #ents, 1 do - local extra_entry_info = mod.ent_extra_list_info(ents[i],pindex,false) + for i = 1, #ents, 1 do + local extra_entry_info = mod.ent_extra_list_info(ents[i], pindex, false) local scan_entry = ents[i].name .. extra_entry_info local index = fa_utils.index_of_entity(result, scan_entry) - + --Filter check 1: Is the entity in the filter diection? (If a filter is set at all) - local dir_of_ent = fa_utils.get_direction_biased(ents[i].position,pos) + local dir_of_ent = fa_utils.get_direction_biased(ents[i].position, pos) local filter_passed = (filter_direction == nil or filter_direction == dir_of_ent) if not filter_passed then --Filter check 2: Is the entity nearby and almost within the filter diection? - if util.distance(ents[i].position,pos) < close_object_limit then - local new_dir_of_ent = fa_utils.get_direction_precise(ents[i].position,pos)--Check with less bias towards diagonal directions to preserve 135 degrees FOV + if util.distance(ents[i].position, pos) < close_object_limit then + local new_dir_of_ent = fa_utils.get_direction_precise(ents[i].position, pos) --Check with less bias towards diagonal directions to preserve 135 degrees FOV local CW_dir = (filter_direction + 1) % (2 * dirs.south) local CCW_dir = (filter_direction - 1) % (2 * dirs.south) - filter_passed = (new_dir_of_ent == filter_direction or new_dir_of_ent == CW_dir or new_dir_of_ent == CCW_dir) + filter_passed = ( + new_dir_of_ent == filter_direction + or new_dir_of_ent == CW_dir + or new_dir_of_ent == CCW_dir + ) end end - if filter_passed then + if filter_passed then if index == nil then --The entry is not already indexed, so add a new entry line to the list - table.insert(result, {name = scan_entry, count = 1, ents = {ents[i]}, aggregate = false}) - + table.insert(result, { name = scan_entry, count = 1, ents = { ents[i] }, aggregate = false }) elseif #result[index] >= 100 then --If there are more than 100 instances of this specific entry (?), replace a random one of them to add this table.remove(result[index].ents, math.random(100)) table.insert(result[index].ents, ents[i]) result[index].count = result[index].count + 1 - else table.insert(result[index].ents, ents[i]) --Add this ent as another instance of the entry - result[index].count = result[index].count + 1 - -- result[index] = ents[i] + result[index].count = result[index].count + 1 + -- result[index] = ents[i] end end end - + --Sort the list - if players[pindex].nearby.count == nil then - players[pindex].nearby.count = false - end + if players[pindex].nearby.count == nil then players[pindex].nearby.count = false end if players[pindex].nearby.count == false then --Sort results by distance to player position when first creating the scanner list - table.sort(result, function(k1, k2) + table.sort(result, function(k1, k2) local pos = players[pindex].position local ent1 = nil local ent2 = nil if k1.aggregate then - table.sort( k1.ents , function(k3, k4) - return fa_utils.squared_distance(pos, k3.position) < fa_utils.squared_distance(pos, k4.position) - end) - ent1 = k1.ents[1] --- end + table.sort(k1.ents, function(k3, k4) + return fa_utils.squared_distance(pos, k3.position) < fa_utils.squared_distance(pos, k4.position) + end) + ent1 = k1.ents[1] + -- end else ent1 = surf.get_closest(pos, k1.ents) end if k2.aggregate then - table.sort( k2.ents , function(k3, k4) - return fa_utils.squared_distance(pos, k3.position) < fa_utils.squared_distance(pos, k4.position) - end) - ent2 = k2.ents[1] --- end + table.sort(k2.ents, function(k3, k4) + return fa_utils.squared_distance(pos, k3.position) < fa_utils.squared_distance(pos, k4.position) + end) + ent2 = k2.ents[1] + -- end else - ent2 = surf.get_closest(pos, k2.ents) + ent2 = surf.get_closest(pos, k2.ents) end return util.distance(pos, ent1.position) < util.distance(pos, ent2.position) end) - else --Sort results by count table.sort(result, function(k1, k2) @@ -279,46 +293,48 @@ function mod.scan_nearby_trees(pindex, filter_direction, radius_in) local pos = players[pindex].position local surf = first_player.surface local radius_s = radius_in or 25 - local close_object_limit = 10.1 + local close_object_limit = 10.1 local result = {} - local ents = surf.find_entities_filtered{position = p.position, radius = radius_s, type = "tree", limit = 200} - if ents == nil or #ents == 0 then - return result - end - - local scan_entry = "tree type"--**laterdo localise here - + local ents = surf.find_entities_filtered({ position = p.position, radius = radius_s, type = "tree", limit = 200 }) + if ents == nil or #ents == 0 then return result end + + local scan_entry = "tree type" --**laterdo localise here + --Insert entities to the initial list - for i=1, #ents, 1 do + for i = 1, #ents, 1 do local index = fa_utils.index_of_entity(result, scan_entry) --Filter check 1: Is the entity in the filter diection? (If a filter is set at all) - local dir_of_ent = fa_utils.get_direction_biased(ents[i].position,pos) + local dir_of_ent = fa_utils.get_direction_biased(ents[i].position, pos) local filter_passed = (filter_direction == nil or filter_direction == dir_of_ent) if not filter_passed then --Filter check 2: Is the entity nearby and almost within the filter diection? - if util.distance(ents[i].position,pos) < close_object_limit then - local new_dir_of_ent = fa_utils.get_direction_precise(ents[i].position,pos)--Check with less bias towards diagonal directions to preserve 135 degrees FOV + if util.distance(ents[i].position, pos) < close_object_limit then + local new_dir_of_ent = fa_utils.get_direction_precise(ents[i].position, pos) --Check with less bias towards diagonal directions to preserve 135 degrees FOV local CW_dir = (filter_direction + 1) % (2 * dirs.south) local CCW_dir = (filter_direction - 1) % (2 * dirs.south) - filter_passed = (new_dir_of_ent == filter_direction or new_dir_of_ent == CW_dir or new_dir_of_ent == CCW_dir) + filter_passed = ( + new_dir_of_ent == filter_direction + or new_dir_of_ent == CW_dir + or new_dir_of_ent == CCW_dir + ) end end - if filter_passed then + if filter_passed then if index == nil then --The entry is not already indexed, so add a new entry line to the list - table.insert(result, {name = scan_entry, count = 1, ents = {ents[i]}, aggregate = false}) + table.insert(result, { name = scan_entry, count = 1, ents = { ents[i] }, aggregate = false }) elseif #result[index] >= 100 then --If there are more than 100 instances of this specific entry (?), replace a random one of them to add this table.remove(result[index].ents, math.random(100)) table.insert(result[index].ents, ents[i]) result[index].count = result[index].count + 1 else table.insert(result[index].ents, ents[i]) --Add this ent as another instance of the entry - result[index].count = result[index].count + 1 - -- result[index] = ents[i] + result[index].count = result[index].count + 1 + -- result[index] = ents[i] end end end - - return result + + return result end --Adds scanned ents to categories of the scan results list. @@ -333,7 +349,7 @@ function mod.populate_list_categories(pindex) for i, ent in ipairs(players[pindex].nearby.ents) do if ent.aggregate then - table.insert(players[pindex].nearby.resources, ent) + table.insert(players[pindex].nearby.resources, ent) else while #ent.ents > 0 and ent.ents[1].valid == false do table.remove(ent.ents, 1) @@ -341,20 +357,43 @@ function mod.populate_list_categories(pindex) if #ent.ents == 0 then print("Empty ent") elseif ent.name == "water" then - table.insert(players[pindex].nearby.resources, ent) - elseif ent.ents[1].type == "resource" or ent.ents[1].type == "tree" or ent.ents[1].name == "sand-rock-big" or ent.ents[1].name == "rock-big" or ent.ents[1].name == "rock-huge" then --Note: There is no rock type, so they are specified by name. table.insert(players[pindex].nearby.resources, ent) - elseif ent.ents[1].type == "container" or ent.ents[1].type == "logistic-container" or ent.ents[1].type == "storage-tank" then + elseif + ent.ents[1].type == "resource" + or ent.ents[1].type == "tree" + or ent.ents[1].name == "sand-rock-big" + or ent.ents[1].name == "rock-big" + or ent.ents[1].name == "rock-huge" + then --Note: There is no rock type, so they are specified by name. + table.insert(players[pindex].nearby.resources, ent) + elseif + ent.ents[1].type == "container" + or ent.ents[1].type == "logistic-container" + or ent.ents[1].type == "storage-tank" + then table.insert(players[pindex].nearby.containers, ent) - elseif ent.ents[1].prototype.is_building and ent.ents[1].type ~= "unit-spawner" and ent.ents[1].type ~= "turret" and ent.ents[1].name ~= "train-stop" then + elseif + ent.ents[1].prototype.is_building + and ent.ents[1].type ~= "unit-spawner" + and ent.ents[1].type ~= "turret" + and ent.ents[1].name ~= "train-stop" + then table.insert(players[pindex].nearby.buildings, ent) - elseif ent.ents[1].type == "car" or ent.ents[1].type == "locomotive" or ent.ents[1].type == "cargo-wagon" or ent.ents[1].type == "fluid-wagon" or ent.ents[1].type == "artillery-wagon" or ent.ents[1].type == "spider-vehicle" or ent.ents[1].name == "train-stop" then + elseif + ent.ents[1].type == "car" + or ent.ents[1].type == "locomotive" + or ent.ents[1].type == "cargo-wagon" + or ent.ents[1].type == "fluid-wagon" + or ent.ents[1].type == "artillery-wagon" + or ent.ents[1].type == "spider-vehicle" + or ent.ents[1].name == "train-stop" + then table.insert(players[pindex].nearby.vehicles, ent) elseif ent.ents[1].type == "character" or ent.ents[1].type == "character-corpse" then table.insert(players[pindex].nearby.players, ent) elseif ent.ents[1].type == "unit" or ent.ents[1].type == "unit-spawner" or ent.ents[1].type == "turret" then table.insert(players[pindex].nearby.enemies, ent) - else--if ent.ents[1].type == "simple-entity" or ent.ents[1].type == "simple-entity-with-owner" or ent.ents[1].type == "entity-ghost" or ent.ents[1].type == "item-entity" then --(allowing all makes it include corpses/remnants as well) + else --if ent.ents[1].type == "simple-entity" or ent.ents[1].type == "simple-entity-with-owner" or ent.ents[1].type == "entity-ghost" or ent.ents[1].type == "item-entity" then --(allowing all makes it include corpses/remnants as well) table.insert(players[pindex].nearby.other, ent) end end @@ -367,24 +406,31 @@ function mod.populate_list_categories(pindex) -- game.print("'players' count: " .. #players[pindex].nearby.players,{volume_modifier = 0}) -- game.print("enemies count: " .. #players[pindex].nearby.enemies,{volume_modifier = 0}) -- game.print("other count: " .. #players[pindex].nearby.other,{volume_modifier = 0}) - end --Run the entity scanner tool ("rescan") -function mod.run_scan(pindex,filter_dir, mute) +function mod.run_scan(pindex, filter_dir, mute) players[pindex].nearby.index = 1 players[pindex].nearby.selection = 1 first_player = game.get_player(pindex) players[pindex].nearby.ents = mod.scan_nearby_trees(pindex, filter_dir, 25) - players[pindex].nearby.ents = mod.scan_area(math.floor(players[pindex].cursor_pos.x)-2500, math.floor(players[pindex].cursor_pos.y)-2500, 5000, 5000, pindex, filter_dir, true) + players[pindex].nearby.ents = mod.scan_area( + math.floor(players[pindex].cursor_pos.x) - 2500, + math.floor(players[pindex].cursor_pos.y) - 2500, + 5000, + 5000, + pindex, + filter_dir, + true + ) mod.populate_list_categories(pindex) players[pindex].nearby.index = 1 players[pindex].nearby.selection = 1 players[pindex].cursor_scanned = false - - --Use the waiting period as a chance to recalibrate + + --Use the waiting period as a chance to recalibrate fa_zoom.fix_zoom(pindex) - + if mute ~= true then if filter_dir == nil then printout("Scan complete.", pindex) @@ -393,18 +439,34 @@ function mod.run_scan(pindex,filter_dir, mute) end end end - + --Sound and visual effects for the scanner function mod.run_scanner_effects(pindex) --Scanner visual and sound effects - game.get_player(pindex).play_sound{path = "scanner-pulse"} - rendering.draw_circle{color = {1, 1, 1},radius = 1,width = 4,target = game.get_player(pindex).position, surface = game.get_player(pindex).surface, draw_on_ground = true, time_to_live = 60} - rendering.draw_circle{color = {1, 1, 1},radius = 2,width = 8,target = game.get_player(pindex).position, surface = game.get_player(pindex).surface, draw_on_ground = true, time_to_live = 60} + game.get_player(pindex).play_sound({ path = "scanner-pulse" }) + rendering.draw_circle({ + color = { 1, 1, 1 }, + radius = 1, + width = 4, + target = game.get_player(pindex).position, + surface = game.get_player(pindex).surface, + draw_on_ground = true, + time_to_live = 60, + }) + rendering.draw_circle({ + color = { 1, 1, 1 }, + radius = 2, + width = 8, + target = game.get_player(pindex).position, + surface = game.get_player(pindex).surface, + draw_on_ground = true, + time_to_live = 60, + }) end --Sort scanner list entries by distance from the reference position, or by total count function mod.list_sort(pindex) - for i, name in ipairs(players[pindex].nearby.ents ) do + for i, name in ipairs(players[pindex].nearby.ents) do local i1 = 1 while i1 <= #name.ents do --this appears to be removing invalid ents within a set. if name.ents[i1] == nil or (name.ents[i1].valid == false and name.aggregate == false) then @@ -420,19 +482,19 @@ function mod.list_sort(pindex) if players[pindex].nearby.count == false then --Sort by distance to player position - table.sort(players[pindex].nearby.ents, function(k1, k2) + table.sort(players[pindex].nearby.ents, function(k1, k2) local pos = players[pindex].position local surf = game.get_player(pindex).surface local ent1 = nil local ent2 = nil if k1.name == "water" then - table.sort( k1.ents , function(k3, k4) + table.sort(k1.ents, function(k3, k4) return fa_utils.squared_distance(pos, k3.position) < fa_utils.squared_distance(pos, k4.position) end) ent1 = k1.ents[1] else if k1.aggregate then - table.sort( k1.ents , function(k3, k4) + table.sort(k1.ents, function(k3, k4) return fa_utils.squared_distance(pos, k3.position) < fa_utils.squared_distance(pos, k4.position) end) ent1 = k1.ents[1] @@ -441,13 +503,13 @@ function mod.list_sort(pindex) end end if k2.name == "water" then - table.sort( k2.ents , function(k3, k4) + table.sort(k2.ents, function(k3, k4) return fa_utils.squared_distance(pos, k3.position) < fa_utils.squared_distance(pos, k4.position) end) ent2 = k2.ents[1] else if k2.aggregate then - table.sort( k2.ents , function(k3, k4) + table.sort(k2.ents, function(k3, k4) return fa_utils.squared_distance(pos, k3.position) < fa_utils.squared_distance(pos, k4.position) end) ent2 = k2.ents[1] @@ -457,7 +519,6 @@ function mod.list_sort(pindex) end return fa_utils.squared_distance(pos, ent1.position) < fa_utils.squared_distance(pos, ent2.position) end) - else --Sort table by count table.sort(players[pindex].nearby.ents, function(k1, k2) @@ -465,24 +526,24 @@ function mod.list_sort(pindex) end) end mod.populate_list_categories(pindex) - end ---Reads the currently selected entity of the scanner list +--Reads the currently selected entity of the scanner list function mod.list_index(pindex) if not check_for_player(pindex) then printout("Scan pindex error.", pindex) return end - if (players[pindex].nearby.category == 1 and next(players[pindex].nearby.ents) == nil) - or (players[pindex].nearby.category == 2 and next(players[pindex].nearby.resources) == nil) - or (players[pindex].nearby.category == 3 and next(players[pindex].nearby.containers) == nil) + if + (players[pindex].nearby.category == 1 and next(players[pindex].nearby.ents) == nil) + or (players[pindex].nearby.category == 2 and next(players[pindex].nearby.resources) == nil) + or (players[pindex].nearby.category == 3 and next(players[pindex].nearby.containers) == nil) or (players[pindex].nearby.category == 4 and next(players[pindex].nearby.buildings) == nil) or (players[pindex].nearby.category == 5 and next(players[pindex].nearby.vehicles) == nil) or (players[pindex].nearby.category == 6 and next(players[pindex].nearby.players) == nil) or (players[pindex].nearby.category == 7 and next(players[pindex].nearby.enemies) == nil) - or (players[pindex].nearby.category == 8 and next(players[pindex].nearby.other) == nil) - then + or (players[pindex].nearby.category == 8 and next(players[pindex].nearby.other) == nil) + then printout("No entities found. Try refreshing with end key.", pindex) else local ents = {} @@ -511,8 +572,22 @@ function mod.list_index(pindex) --Remove invalid or unwanted instances of the entity while i <= #ents[players[pindex].nearby.index].ents do local ents_i = ents[players[pindex].nearby.index].ents[i] - if ents_i.valid and ents_i.name ~= "highlight-box" and ents_i.type ~= "flying-text" and ents_i.name ~= "rocket-silo-rocket" and ents_i.name ~= "rocket-silo-rocket-shadow" and ents_i.type ~= "spider-leg" - and (players[pindex].cursor_scanned ~= true or (players[pindex].cursor_scanned == true and util.distance(ents_i.position,players[pindex].cursor_scan_center) < players[pindex].cursor_size + 1 )) then + if + ents_i.valid + and ents_i.name ~= "highlight-box" + and ents_i.type ~= "flying-text" + and ents_i.name ~= "rocket-silo-rocket" + and ents_i.name ~= "rocket-silo-rocket-shadow" + and ents_i.type ~= "spider-leg" + and ( + players[pindex].cursor_scanned ~= true + or ( + players[pindex].cursor_scanned == true + and util.distance(ents_i.position, players[pindex].cursor_scan_center) + < players[pindex].cursor_size + 1 + ) + ) + then i = i + 1 else table.remove(ents[players[pindex].nearby.index].ents, i) @@ -523,13 +598,13 @@ function mod.list_index(pindex) end --If there is none left of the entity, remove it if #ents[players[pindex].nearby.index].ents == 0 then - table.remove(ents,players[pindex].nearby.index) + table.remove(ents, players[pindex].nearby.index) players[pindex].nearby.index = math.min(players[pindex].nearby.index, #ents) mod.list_index(pindex) return end --Sort by distance to player pos while describing indexed entries - table.sort(ents[players[pindex].nearby.index].ents, function(k1, k2) + table.sort(ents[players[pindex].nearby.index].ents, function(k1, k2) local pos = players[pindex].position return fa_utils.squared_distance(pos, k1.position) < fa_utils.squared_distance(pos, k2.position) end) @@ -549,11 +624,9 @@ function mod.list_index(pindex) --Select northwest corner unless it is a spaceship wreck players[pindex].cursor_pos = fa_utils.get_ent_northwest_corner_position(ent) local check = ent.name - local a = string.find(check,"spaceship") - if a ~= nil then - players[pindex].cursor_pos = ent.position - end - fa_graphics.draw_cursor_highlight(pindex, ent, "train-visualization")--focus on scanned item + local a = string.find(check, "spaceship") + if a ~= nil then players[pindex].cursor_pos = ent.position end + fa_graphics.draw_cursor_highlight(pindex, ent, "train-visualization") --focus on scanned item fa_graphics.sync_build_cursor_graphics(pindex) players[pindex].last_indexed_ent = ent else @@ -564,7 +637,7 @@ function mod.list_index(pindex) local name = ents[players[pindex].nearby.index].name local entry = ents[players[pindex].nearby.index].ents[players[pindex].nearby.selection] --If there is none left of the entry or it is an unwanted type (does this ever happen?), remove it - if entry ~= nil then + if entry ~= nil then if table_size(entry) == 0 or name == "highlight-box" then table.remove(ents[players[pindex].nearby.index].ents, players[pindex].nearby.selection) players[pindex].nearby.selection = players[pindex].nearby.selection - 1 @@ -572,7 +645,7 @@ function mod.list_index(pindex) return end --The scan target is an aggregate, select it now - ent = {name = name, position = table.deepcopy(entry.position), group = entry.group} --maybe use "aggregate = true" ? + ent = { name = name, position = table.deepcopy(entry.position), group = entry.group } --maybe use "aggregate = true" ? players[pindex].cursor_pos = ent.position fa_graphics.draw_cursor_highlight(pindex, nil, "train-visualization") fa_graphics.sync_build_cursor_graphics(pindex) @@ -580,45 +653,57 @@ function mod.list_index(pindex) game.get_player(pindex).selected = nil end end - - if ent == nil or (ents[players[pindex].nearby.index].aggregate == false and (ent == nil or ent.valid ~= true)) then + + if + ent == nil or (ents[players[pindex].nearby.index].aggregate == false and (ent == nil or ent.valid ~= true)) + then printout("Error: Invalid object, maybe try rescanning.", pindex) return end - - if (players[pindex].cursor_scanned == true and util.distance(ent.position,players[pindex].cursor_scan_center) > players[pindex].cursor_size + 1 ) then - local final_result = {""} - table.insert(final_result,fa_utils.ent_name_locale(ent)) - table.insert(final_result," reference point outside of scan area") + + if + players[pindex].cursor_scanned == true + and util.distance(ent.position, players[pindex].cursor_scan_center) > players[pindex].cursor_size + 1 + then + local final_result = { "" } + table.insert(final_result, fa_utils.ent_name_locale(ent)) + table.insert(final_result, " reference point outside of scan area") printout(final_result, pindex) return end - + refresh_player_tile(pindex) - + local dir_dist = fa_utils.dir_dist_locale(players[pindex].position, ent.position) if players[pindex].nearby.count == false then --Read the entity in terms of distance and direction - local result={"access.thing-producing-listpos-dirdist",fa_utils.ent_name_locale(ent)} - table.insert(result,mod.ent_extra_list_info(ent,pindex,true)) - table.insert(result,{"description.of", players[pindex].nearby.selection , #ents[players[pindex].nearby.index].ents})--"X of Y" - table.insert(result,dir_dist) - local final_result = {""} - table.insert(final_result,result) - table.insert(final_result,", ") - table.insert(final_result,cursor_visibility_info(pindex)) - printout(final_result,pindex) + local result = { "access.thing-producing-listpos-dirdist", fa_utils.ent_name_locale(ent) } + table.insert(result, mod.ent_extra_list_info(ent, pindex, true)) + table.insert( + result, + { "description.of", players[pindex].nearby.selection, #ents[players[pindex].nearby.index].ents } + ) --"X of Y" + table.insert(result, dir_dist) + local final_result = { "" } + table.insert(final_result, result) + table.insert(final_result, ", ") + table.insert(final_result, cursor_visibility_info(pindex)) + printout(final_result, pindex) else --Read the entity in terms of count, and give the direction and distance of an example - local result = {"access.item_and_quantity-example-at-dirdist", {"access.item-quantity",fa_utils.ent_name_locale(ent),ents[players[pindex].nearby.index].count}, dir_dist} - local final_result = {""} - table.insert(final_result,result) - table.insert(final_result,", ") - table.insert(final_result,cursor_visibility_info(pindex)) - printout(final_result,pindex) + local result = { + "access.item_and_quantity-example-at-dirdist", + { "access.item-quantity", fa_utils.ent_name_locale(ent), ents[players[pindex].nearby.index].count }, + dir_dist, + } + local final_result = { "" } + table.insert(final_result, result) + table.insert(final_result, ", ") + table.insert(final_result, cursor_visibility_info(pindex)) + printout(final_result, pindex) end end -end +end --Move up one entry in the scanner list function mod.list_up(pindex) @@ -632,29 +717,31 @@ function mod.list_up(pindex) elseif players[pindex].nearby.index <= 1 then players[pindex].nearby.index = 1 players[pindex].nearby.selection = 1 - game.get_player(pindex).play_sound{path = "inventory-edge"} + game.get_player(pindex).play_sound({ path = "inventory-edge" }) end mod.list_index(pindex) - end +end ---Move down one entry in the scanner list +--Move down one entry in the scanner list function mod.list_down(pindex) if players[pindex].in_menu then --These keys may overlap a lot so might as well return end - if (players[pindex].nearby.category == 1 and players[pindex].nearby.index < #players[pindex].nearby.ents) or - (players[pindex].nearby.category == 2 and players[pindex].nearby.index < #players[pindex].nearby.resources) or - (players[pindex].nearby.category == 3 and players[pindex].nearby.index < #players[pindex].nearby.containers) or - (players[pindex].nearby.category == 4 and players[pindex].nearby.index < #players[pindex].nearby.buildings) or - (players[pindex].nearby.category == 5 and players[pindex].nearby.index < #players[pindex].nearby.vehicles) or - (players[pindex].nearby.category == 6 and players[pindex].nearby.index < #players[pindex].nearby.players) or - (players[pindex].nearby.category == 7 and players[pindex].nearby.index < #players[pindex].nearby.enemies) or - (players[pindex].nearby.category == 8 and players[pindex].nearby.index < #players[pindex].nearby.other) then + if + (players[pindex].nearby.category == 1 and players[pindex].nearby.index < #players[pindex].nearby.ents) + or (players[pindex].nearby.category == 2 and players[pindex].nearby.index < #players[pindex].nearby.resources) + or (players[pindex].nearby.category == 3 and players[pindex].nearby.index < #players[pindex].nearby.containers) + or (players[pindex].nearby.category == 4 and players[pindex].nearby.index < #players[pindex].nearby.buildings) + or (players[pindex].nearby.category == 5 and players[pindex].nearby.index < #players[pindex].nearby.vehicles) + or (players[pindex].nearby.category == 6 and players[pindex].nearby.index < #players[pindex].nearby.players) + or (players[pindex].nearby.category == 7 and players[pindex].nearby.index < #players[pindex].nearby.enemies) + or (players[pindex].nearby.category == 8 and players[pindex].nearby.index < #players[pindex].nearby.other) + then players[pindex].nearby.index = players[pindex].nearby.index + 1 players[pindex].nearby.selection = 1 - else - game.get_player(pindex).play_sound{path = "inventory-edge"} + else + game.get_player(pindex).play_sound({ path = "inventory-edge" }) players[pindex].nearby.selection = 1 end mod.list_index(pindex) @@ -691,26 +778,30 @@ function mod.list_current(pindex) players[pindex].nearby.index = #ents end - if not(pcall(function() + if not (pcall(function() mod.list_index(pindex) end)) then table.remove(ents, players[pindex].nearby.index) mod.list_current(pindex) end - end +end --Returns an info string about the entities and tiles found within an area scan done by an enlarged cursor. -function mod.area_scan_summary_info(scan_left_top, scan_right_bottom, pindex) +function mod.area_scan_summary_info(scan_left_top, scan_right_bottom, pindex) local result = "" - local explored_left_top = {x = math.floor((players[pindex].cursor_pos.x - 1 - players[pindex].cursor_size) / 32), y = math.floor((players[pindex].cursor_pos.y - 1 - players[pindex].cursor_size)/32)} - local explored_right_bottom = {x = math.floor((players[pindex].cursor_pos.x + 1 + players[pindex].cursor_size)/32), y = math.floor((players[pindex].cursor_pos.y + 1 + players[pindex].cursor_size)/32)} + local explored_left_top = { + x = math.floor((players[pindex].cursor_pos.x - 1 - players[pindex].cursor_size) / 32), + y = math.floor((players[pindex].cursor_pos.y - 1 - players[pindex].cursor_size) / 32), + } + local explored_right_bottom = { + x = math.floor((players[pindex].cursor_pos.x + 1 + players[pindex].cursor_size) / 32), + y = math.floor((players[pindex].cursor_pos.y + 1 + players[pindex].cursor_size) / 32), + } local count = 0 local total = 0 for i = explored_left_top.x, explored_right_bottom.x do for i1 = explored_left_top.y, explored_right_bottom.y do - if game.get_player(pindex).surface.is_chunk_generated({i, i1}) then - count = count + 1 - end + if game.get_player(pindex).surface.is_chunk_generated({ i, i1 }) then count = count + 1 end total = total + 1 end end @@ -718,96 +809,106 @@ function mod.area_scan_summary_info(scan_left_top, scan_right_bottom, pindex) result = result .. "Charted 0%, you need to chart this area by approaching it or using a radar." return result elseif total > 0 and count < total then - result = result .. "Charted " .. math.floor((count/total) * 100) .. "%, " + result = result .. "Charted " .. math.floor((count / total) * 100) .. "%, " end - + local percentages = {} local percent_total = 0 local surf = game.get_player(pindex).surface --Scan for Tiles and Resources, because they behave weirdly in scan_area due to aggregation, or are skipped local percent = 0 - local res_count = surf.count_tiles_filtered{ name = {"water", "deepwater", "water-green", "deepwater-green", "water-shallow", "water-mud", "water-wube"}, area = {scan_left_top,scan_right_bottom} } - percent = math.floor((res_count / ((1+players[pindex].cursor_size * 2) ^2) * 100) + .5) - if percent > 0 then - table.insert(percentages, {name = "water", percent = percent, count = "resource"}) - end - percent_total = percent_total + percent--water counts as filling a space - - res_count = surf.count_tiles_filtered{ name = "stone-path", area = {scan_left_top,scan_right_bottom} } - percent = math.floor((res_count / ((1+players[pindex].cursor_size * 2) ^2) * 100) + .5) - if percent > 0 then - table.insert(percentages, {name = "stone-brick-path", percent = percent, count = "flooring"}) - end - - res_count = surf.count_tiles_filtered{ name = {"concrete","hazard-concrete-left","hazard-concrete-right"}, area = {scan_left_top,scan_right_bottom} } - percent = math.floor((res_count / ((1+players[pindex].cursor_size * 2) ^2) * 100) + .5) - if percent > 0 then - table.insert(percentages, {name = "concrete", percent = percent, count = "flooring"}) - end - - res_count = surf.count_tiles_filtered{ name = {"refined-concrete","refined-hazard-concrete-left","refined-hazard-concrete-right"}, area = {scan_left_top,scan_right_bottom} } - percent = math.floor((res_count / ((1+players[pindex].cursor_size * 2) ^2) * 100) + .5) - if percent > 0 then - table.insert(percentages, {name = "refined-concrete", percent = percent, count = "flooring"}) - end - - res_count = surf.count_entities_filtered{ name = "coal", area = {scan_left_top,scan_right_bottom} } - percent = math.floor((res_count / ((1+players[pindex].cursor_size * 2) ^2) * 100) + .5) - if percent > 0 then - table.insert(percentages, {name = "coal", percent = percent, count = "resource"}) - end - - res_count = surf.count_entities_filtered{ name = "stone", area = {scan_left_top,scan_right_bottom} } - percent = math.floor((res_count / ((1+players[pindex].cursor_size * 2) ^2) * 100) + .5) - if percent > 0 then - table.insert(percentages, {name = "stone", percent = percent, count = "resource"}) - end - - res_count = surf.count_entities_filtered{ name = "iron-ore", area = {scan_left_top,scan_right_bottom} } - percent = math.floor((res_count / ((1+players[pindex].cursor_size * 2) ^2) * 100) + .5) - if percent > 0 then - table.insert(percentages, {name = "iron-ore", percent = percent, count = "resource"}) - end - - res_count = surf.count_entities_filtered{ name = "copper-ore", area = {scan_left_top,scan_right_bottom} } - percent = math.floor((res_count / ((1+players[pindex].cursor_size * 2) ^2) * 100) + .5) - if percent > 0 then - table.insert(percentages, {name = "copper-ore", percent = percent, count = "resource"}) - end - - res_count = surf.count_entities_filtered{ name = "uranium-ore", area = {scan_left_top,scan_right_bottom} } - percent = math.floor((res_count / ((1+players[pindex].cursor_size * 2) ^2) * 100) + .5) - if percent > 0 then - table.insert(percentages, {name = "uranium-ore", percent = percent, count = "resource"}) - end - - res_count = surf.count_entities_filtered{ name = "crude-oil", area = {scan_left_top,scan_right_bottom} } - percent = math.floor((9 * res_count / ((1+players[pindex].cursor_size * 2) ^2) * 100) + .5) + local res_count = surf.count_tiles_filtered({ + name = { "water", "deepwater", "water-green", "deepwater-green", "water-shallow", "water-mud", "water-wube" }, + area = { scan_left_top, scan_right_bottom }, + }) + percent = math.floor((res_count / ((1 + players[pindex].cursor_size * 2) ^ 2) * 100) + 0.5) + if percent > 0 then table.insert(percentages, { name = "water", percent = percent, count = "resource" }) end + percent_total = percent_total + percent --water counts as filling a space + + res_count = surf.count_tiles_filtered({ name = "stone-path", area = { scan_left_top, scan_right_bottom } }) + percent = math.floor((res_count / ((1 + players[pindex].cursor_size * 2) ^ 2) * 100) + 0.5) if percent > 0 then - table.insert(percentages, {name = "crude-oil", percent = percent, count = "resource"}) + table.insert(percentages, { name = "stone-brick-path", percent = percent, count = "flooring" }) end - - res_count = surf.count_entities_filtered{ type = "tree", area = {scan_left_top,scan_right_bottom} } - percent = math.floor((res_count * 4 / ((1+players[pindex].cursor_size * 2) ^2) * 100) + .5)--trees are bigger than 1 tile + + res_count = surf.count_tiles_filtered({ + name = { "concrete", "hazard-concrete-left", "hazard-concrete-right" }, + area = { scan_left_top, scan_right_bottom }, + }) + percent = math.floor((res_count / ((1 + players[pindex].cursor_size * 2) ^ 2) * 100) + 0.5) + if percent > 0 then table.insert(percentages, { name = "concrete", percent = percent, count = "flooring" }) end + + res_count = surf.count_tiles_filtered({ + name = { "refined-concrete", "refined-hazard-concrete-left", "refined-hazard-concrete-right" }, + area = { scan_left_top, scan_right_bottom }, + }) + percent = math.floor((res_count / ((1 + players[pindex].cursor_size * 2) ^ 2) * 100) + 0.5) if percent > 0 then - table.insert(percentages, {name = "trees", percent = percent, count = res_count}) + table.insert(percentages, { name = "refined-concrete", percent = percent, count = "flooring" }) end + + res_count = surf.count_entities_filtered({ name = "coal", area = { scan_left_top, scan_right_bottom } }) + percent = math.floor((res_count / ((1 + players[pindex].cursor_size * 2) ^ 2) * 100) + 0.5) + if percent > 0 then table.insert(percentages, { name = "coal", percent = percent, count = "resource" }) end + + res_count = surf.count_entities_filtered({ name = "stone", area = { scan_left_top, scan_right_bottom } }) + percent = math.floor((res_count / ((1 + players[pindex].cursor_size * 2) ^ 2) * 100) + 0.5) + if percent > 0 then table.insert(percentages, { name = "stone", percent = percent, count = "resource" }) end + + res_count = surf.count_entities_filtered({ name = "iron-ore", area = { scan_left_top, scan_right_bottom } }) + percent = math.floor((res_count / ((1 + players[pindex].cursor_size * 2) ^ 2) * 100) + 0.5) + if percent > 0 then table.insert(percentages, { name = "iron-ore", percent = percent, count = "resource" }) end + + res_count = surf.count_entities_filtered({ name = "copper-ore", area = { scan_left_top, scan_right_bottom } }) + percent = math.floor((res_count / ((1 + players[pindex].cursor_size * 2) ^ 2) * 100) + 0.5) + if percent > 0 then table.insert(percentages, { name = "copper-ore", percent = percent, count = "resource" }) end + + res_count = surf.count_entities_filtered({ name = "uranium-ore", area = { scan_left_top, scan_right_bottom } }) + percent = math.floor((res_count / ((1 + players[pindex].cursor_size * 2) ^ 2) * 100) + 0.5) + if percent > 0 then table.insert(percentages, { name = "uranium-ore", percent = percent, count = "resource" }) end + + res_count = surf.count_entities_filtered({ name = "crude-oil", area = { scan_left_top, scan_right_bottom } }) + percent = math.floor((9 * res_count / ((1 + players[pindex].cursor_size * 2) ^ 2) * 100) + 0.5) + if percent > 0 then table.insert(percentages, { name = "crude-oil", percent = percent, count = "resource" }) end + + res_count = surf.count_entities_filtered({ type = "tree", area = { scan_left_top, scan_right_bottom } }) + percent = math.floor((res_count * 4 / ((1 + players[pindex].cursor_size * 2) ^ 2) * 100) + 0.5) --trees are bigger than 1 tile + if percent > 0 then table.insert(percentages, { name = "trees", percent = percent, count = res_count }) end percent_total = percent_total + percent - + if #players[pindex].nearby.ents > 0 then --Note: Resources are included here as aggregates. for i, ent in ipairs(players[pindex].nearby.ents) do local area = 0 --this confirmation is necessary because all we have is the ent name, and some distant resources show up on the list. - if fa_utils.is_ent_inside_area( fa_utils.get_substring_before_space(fa_utils.get_substring_before_comma(ent.name)) , scan_left_top , scan_right_bottom, pindex) then - area = fa_utils.get_ent_area_from_name(fa_utils.get_substring_before_space(fa_utils.get_substring_before_comma(ent.name)),pindex) + if + fa_utils.is_ent_inside_area( + fa_utils.get_substring_before_space(fa_utils.get_substring_before_comma(ent.name)), + scan_left_top, + scan_right_bottom, + pindex + ) + then + area = fa_utils.get_ent_area_from_name( + fa_utils.get_substring_before_space(fa_utils.get_substring_before_comma(ent.name)), + pindex + ) if area == -1 then area = 1 - game.get_player(pindex).print(fa_utils.get_substring_before_space(fa_utils.get_substring_before_comma(ent.name)) .. " could not be found for the area check ",{volume_modifier = 0})--bug: unable to get area from name + game.get_player(pindex).print( + fa_utils.get_substring_before_space(fa_utils.get_substring_before_comma(ent.name)) + .. " could not be found for the area check ", + { volume_modifier = 0 } + ) --bug: unable to get area from name end - end - local percentage = math.floor((area * players[pindex].nearby.ents[i].count / ((1+players[pindex].cursor_size * 2) ^2) * 100) + .95)--Tolerate up to 0.05% + end + local percentage = math.floor( + (area * players[pindex].nearby.ents[i].count / ((1 + players[pindex].cursor_size * 2) ^ 2) * 100) + 0.95 + ) --Tolerate up to 0.05% if not ent.aggregate and percentage > 0 then - table.insert(percentages, {name = ent.name, percent = percentage, count = players[pindex].nearby.ents[i].count}) + table.insert( + percentages, + { name = ent.name, percent = percentage, count = players[pindex].nearby.ents[i].count } + ) end percent_total = percent_total + percentage end @@ -817,18 +918,18 @@ function mod.area_scan_summary_info(scan_left_top, scan_right_bottom, pindex) result = result .. " Area contains " local i = 1 while i <= #percentages and (i <= 4 or percentages[i].percent > 1) do - result = result .. ", " .. percentages[i].count .. " " .. percentages[i].name .. " " - if percentages[i].count == "resource" or percentages[i].count == "flooring" then + result = result .. ", " .. percentages[i].count .. " " .. percentages[i].name .. " " + if percentages[i].count == "resource" or percentages[i].count == "flooring" then result = result .. percentages[i].percent .. "% " end i = i + 1 end - if percent_total == 0 then--Note there are still some entities in here, but with zero area... + if percent_total == 0 then --Note there are still some entities in here, but with zero area... result = result .. " nothing " - elseif i >= 4 then + elseif i >= 4 then result = result .. ", and other things " end - result = result .. ", total space occupied " .. math.floor(percent_total) .. " percent " + result = result .. ", total space occupied " .. math.floor(percent_total) .. " percent " else result = result .. " Empty Area " end @@ -837,15 +938,15 @@ function mod.area_scan_summary_info(scan_left_top, scan_right_bottom, pindex) end --Brief extra entity info is given here, for mentioning in the scanner list. If the parameter "info_comes_after_indexing" is not true, then this info distinguishes the entity plus its description as a new line of the scanner list, such as how assembling machines with different recipes are listed separately. -function mod.ent_extra_list_info(ent,pindex,info_comes_after_indexing) +function mod.ent_extra_list_info(ent, pindex, info_comes_after_indexing) local result = "" - if ent.name ~= "water" and ent.type == "mining-drill" then - --Mining drill products + if ent.name ~= "water" and ent.type == "mining-drill" then + --Mining drill products local pos = ent.position local radius = ent.prototype.mining_drill_radius - local area = {{pos.x - radius, pos.y - radius}, {pos.x + radius, pos.y + radius}} - local resources = ent.surface.find_entities_filtered{area = area, type = "resource"} + local area = { { pos.x - radius, pos.y - radius }, { pos.x + radius, pos.y + radius } } + local resources = ent.surface.find_entities_filtered({ area = area, type = "resource" }) local dict = {} for i, resource in pairs(resources) do if dict[resource.name] == nil then @@ -869,23 +970,21 @@ function mod.ent_extra_list_info(ent,pindex,info_comes_after_indexing) result = result .. " producing " .. ent.get_recipe().name elseif ent.type == "furnace" and #ent.get_output_inventory() > 0 then local output_item = ent.get_output_inventory()[1] - if output_item and output_item.valid_for_read then - result = result .. " producing " .. output_item.name - end + if output_item and output_item.valid_for_read then result = result .. " producing " .. output_item.name end end end) - + if ent.name == "entity-ghost" then - --Ghost names + --Ghost names result = " of " .. ent.ghost_name end - - if ent.type == "container" or ent.type == "logistic-container" then - --Chests are identified by whether they contain nothing a specific item, or simply various items + + if ent.type == "container" or ent.type == "logistic-container" then + --Chests are identified by whether they contain nothing a specific item, or simply various items local itemset = ent.get_inventory(defines.inventory.chest).get_contents() local itemtable = {} for name, count in pairs(itemset) do - table.insert(itemtable, {name = name, count = count}) + table.insert(itemtable, { name = name, count = count }) end --table.sort(itemtable, function(k1, k2) -- return k1.count > k2.count @@ -898,39 +997,37 @@ function mod.ent_extra_list_info(ent,pindex,info_comes_after_indexing) result = result .. " with various items " end elseif ent.type == "unit-spawner" then - --Group spawners by pollution level + --Group spawners by pollution level if ent.absorbed_pollution > 0 then result = " polluted lightly " - if ent.absorbed_pollution > 99 then - result = " polluted heavily " - end + if ent.absorbed_pollution > 99 then result = " polluted heavily " end else local pos = ent.position local pollution_nearby = false - pollution_nearby = pollution_nearby and (ent.surface.get_pollution({pos.x+00,pos.y+00}) > 0) - pollution_nearby = pollution_nearby and (ent.surface.get_pollution({pos.x+33,pos.y+00}) > 0) - pollution_nearby = pollution_nearby and (ent.surface.get_pollution({pos.x-33,pos.y+00}) > 0) - pollution_nearby = pollution_nearby and (ent.surface.get_pollution({pos.x+00,pos.y+33}) > 0) - pollution_nearby = pollution_nearby and (ent.surface.get_pollution({pos.x+00,pos.y-33}) > 0) + pollution_nearby = pollution_nearby and (ent.surface.get_pollution({ pos.x + 00, pos.y + 00 }) > 0) + pollution_nearby = pollution_nearby and (ent.surface.get_pollution({ pos.x + 33, pos.y + 00 }) > 0) + pollution_nearby = pollution_nearby and (ent.surface.get_pollution({ pos.x - 33, pos.y + 00 }) > 0) + pollution_nearby = pollution_nearby and (ent.surface.get_pollution({ pos.x + 00, pos.y + 33 }) > 0) + pollution_nearby = pollution_nearby and (ent.surface.get_pollution({ pos.x + 00, pos.y - 33 }) > 0) if pollution_nearby then - result = " almost polluted "--**laterdo bug: this does not seem to ever be reached + result = " almost polluted " --**laterdo bug: this does not seem to ever be reached else result = " normal " end end end - + if info_comes_after_indexing == true and ent.train ~= nil and ent.train.valid then - --Train name for train vehicles + --Train name for train vehicles result = result .. " of train " .. fa_trains.get_train_name(ent.train) elseif ent.name == "character" then - --Character names + --Character names local p = ent.player local p2 = ent.associated_player if p ~= nil and p.valid and p.name ~= nil and p.name ~= "" then - result = result .. " " .. p.name + result = result .. " " .. p.name elseif p2 ~= nil and p2.valid and p2.name ~= nil and p2.name ~= "" then - result = result .. " " .. p2.name + result = result .. " " .. p2.name elseif p ~= nil and p.valid and p.index == pindex then result = result .. " you " elseif pindex ~= nil then @@ -939,41 +1036,37 @@ function mod.ent_extra_list_info(ent,pindex,info_comes_after_indexing) result = result .. " X " end elseif ent.name == "character-corpse" then - --Character corpse info + --Character corpse info if ent.character_corpse_player_index == pindex then result = result .. " of your character " elseif ent.character_corpse_player_index ~= nil then result = result .. " of another character " end elseif info_comes_after_indexing == true and ent.name == "train-stop" then - --Train stop name + --Train stop name result = result .. " " .. ent.backer_name elseif ent.name == "forest" then - --Forest type by density - result = result .. mod.classify_forest(ent.position,pindex,true) + --Forest type by density + result = result .. mod.classify_forest(ent.position, pindex, true) elseif ent.name == "roboport" then - --Roboport network name + --Roboport network name result = result .. " of network " .. fa_bot_logistics.get_network_name(ent) elseif ent.type == "spider-vehicle" then local label = ent.entity_label - if label == nil then - label = "" - end + if label == nil then label = "" end result = result .. label elseif ent.name == "pipe" or ent.name == "storage-tank" then --Pipe ends are labelled to distinguish them - if ent.name == "pipe" and fa_building_tools.is_a_pipe_end(ent,pindex) then - result = result .. " end, " - end - --Pipes and storage tanks are separated depending on the fluid they contain + if ent.name == "pipe" and fa_building_tools.is_a_pipe_end(ent, pindex) then result = result .. " end, " end + --Pipes and storage tanks are separated depending on the fluid they contain local dict = ent.get_fluid_contents() local fluids = {} for name, count in pairs(dict) do - table.insert(fluids, {name = name, count = count}) + table.insert(fluids, { name = name, count = count }) end if #fluids > 0 and fluids[1].count ~= nil then if #fluids == 1 then - result = result .. " with " .. localising.get_fluid_from_name(fluids[1].name,pindex) + result = result .. " with " .. localising.get_fluid_from_name(fluids[1].name, pindex) elseif #fluids > 1 and fluids[2].count ~= nil then result = result .. " with multiple fluids " end @@ -981,21 +1074,39 @@ function mod.ent_extra_list_info(ent,pindex,info_comes_after_indexing) result = result .. " empty " end end - + return result end --Examines a forest position and classifies it by tree density. Used for the scanner list. -function mod.classify_forest(position,pindex,drawing) +function mod.classify_forest(position, pindex, drawing) local tree_count = 0 - local tree_group = game.get_player(pindex).surface.find_entities_filtered{type = "tree", position = position, radius = 16, limit = 15} + local tree_group = game + .get_player(pindex).surface + .find_entities_filtered({ type = "tree", position = position, radius = 16, limit = 15 }) if drawing then - rendering.draw_circle{color = {0, 1, 0.25},radius = 16,width = 4,target = position, surface = game.get_player(pindex).surface, time_to_live = 60, draw_on_ground = true} + rendering.draw_circle({ + color = { 0, 1, 0.25 }, + radius = 16, + width = 4, + target = position, + surface = game.get_player(pindex).surface, + time_to_live = 60, + draw_on_ground = true, + }) end - for i,tree in ipairs(tree_group) do + for i, tree in ipairs(tree_group) do tree_count = tree_count + 1 if drawing then - rendering.draw_circle{color = {0, 1, 0.5},radius = 1,width = 4,target = tree.position, surface = tree.surface, time_to_live = 60, draw_on_ground = true} + rendering.draw_circle({ + color = { 0, 1, 0.5 }, + radius = 1, + width = 4, + target = tree.position, + surface = tree.surface, + time_to_live = 60, + draw_on_ground = true, + }) end end if tree_count < 1 then @@ -1009,4 +1120,4 @@ function mod.classify_forest(position,pindex,drawing) end end -return mod \ No newline at end of file +return mod diff --git a/scripts/spidertron.lua b/scripts/spidertron.lua index a2bf599d..2f34b90a 100644 --- a/scripts/spidertron.lua +++ b/scripts/spidertron.lua @@ -1,4 +1,4 @@ ---Here: Spidertron remote menu +--Here: Spidertron remote menu local mod = {} @@ -6,33 +6,34 @@ local mod = {} function mod.run_spider_menu(menu_index, pindex, spiderin, clicked, other_input) local index = menu_index local spider - local remote = game.get_player(pindex).cursor_stack + local remote = game.get_player(pindex).cursor_stack local other = other_input or -1 local cursortarget = get_selected_ent(pindex) local spidertron = game.get_player(pindex).cursor_stack.connected_entity - if spiderin ~= nil then - spider = spiderin - end + if spiderin ~= nil then spider = spiderin end if index == 0 then --Give basic info about this spider, such as its name and ID. local res if remote.connected_entity ~= nil then if spidertron.entity_label ~= nil then res = spidertron.entity_label .. "connected to this remote. " - else + else res = "unlabelled spidertron connected" end else res = "this remote is not connected to a spidertron" end - printout(res - .. ", Press UP ARROW and DOWN ARROW to navigate options, press LEFT BRACKET to select an option or press E to exit this menu.", pindex) + printout( + res + .. ", Press UP ARROW and DOWN ARROW to navigate options, press LEFT BRACKET to select an option or press E to exit this menu.", + pindex + ) elseif index == 1 then --spidertron linking and unlinking from the remote if not clicked then if remote.connected_entity ~= nil then local spidername - if game.get_player(pindex).cursor_stack.connected_entity.entity_label~= nil then + if game.get_player(pindex).cursor_stack.connected_entity.entity_label ~= nil then spidername = game.get_player(pindex).cursor_stack.connected_entity.entity_label else spidername = "an unlabelled spidertron" @@ -50,17 +51,19 @@ function mod.run_spider_menu(menu_index, pindex, spiderin, clicked, other_input) remote.connected_entity = nil printout("remote link severed.", pindex) else - local result + local result if cursortarget == nil or (cursortarget.type ~= "spider-vehicle" and cursortarget.type ~= "spider-leg") then result = "Invalid object to link to this remote. " else if cursortarget.type == "spider-vehicle" then remote.connected_entity = cursortarget else - local spiders = cursortarget.surface.find_entities_filtered{position = cursortarget.position, radius = 5, type = "spider-vehicle"} - if spiders[1] and spiders[1].valid then - remote.connected_entity = spiders[1] - end + local spiders = cursortarget.surface.find_entities_filtered({ + position = cursortarget.position, + radius = 5, + type = "spider-vehicle", + }) + if spiders[1] and spiders[1].valid then remote.connected_entity = spiders[1] end end result = "remote connected to " if game.get_player(pindex).cursor_stack.connected_entity.entity_label ~= nil then @@ -82,17 +85,17 @@ function mod.run_spider_menu(menu_index, pindex, spiderin, clicked, other_input) else printout("Enter a new name for this spidertron, then press ENTER to confirm.", pindex) players[pindex].spider_menu.renaming = true - local frame = game.get_player(pindex).gui.screen.add{type = "frame", name = "spider-rename"} + local frame = game.get_player(pindex).gui.screen.add({ type = "frame", name = "spider-rename" }) frame.bring_to_front() frame.force_auto_center() frame.focus() game.get_player(pindex).opened = frame - local input = frame.add{type="textfield", name = "input"} + local input = frame.add({ type = "textfield", name = "input" }) input.focus() end end elseif index == 3 then - --Set the cursor position as the spidertron autopilot target + --Set the cursor position as the spidertron autopilot target if not clicked then printout("Set the cursor position as the spidertron autopilot target ", pindex) else @@ -105,7 +108,7 @@ function mod.run_spider_menu(menu_index, pindex, spiderin, clicked, other_input) end end elseif index == 4 then - --Add the cursor position to the spidertron autopilot queue + --Add the cursor position to the spidertron autopilot queue if not clicked then printout("add the cursor position to the spidertron autopilot queue", pindex) else @@ -114,7 +117,14 @@ function mod.run_spider_menu(menu_index, pindex, spiderin, clicked, other_input) else cursor = players[pindex].cursor_pos game.get_player(pindex).cursor_stack.connected_entity.add_autopilot_destination(cursor) - printout("Coordinates " .. math.floor(cursor.x) .. ", " .. math.floor(cursor.y) .. "added to this spidertron's autopilot queue.", pindex) + printout( + "Coordinates " + .. math.floor(cursor.x) + .. ", " + .. math.floor(cursor.y) + .. "added to this spidertron's autopilot queue.", + pindex + ) end end elseif index == 5 then @@ -124,17 +134,26 @@ function mod.run_spider_menu(menu_index, pindex, spiderin, clicked, other_input) else if not clicked then local targetstate - if game.get_player(pindex).cursor_stack.connected_entity.vehicle_automatic_targeting_parameters.auto_target_without_gunner == true then + if + game.get_player(pindex).cursor_stack.connected_entity.vehicle_automatic_targeting_parameters.auto_target_without_gunner + == true + then targetstate = "enabled" else targetstate = "disabled" end printout("auto target enemies when the spidertron is working by itself, currently" .. targetstate, pindex) else - local switch = {auto_target_without_gunner = not game.get_player(pindex).cursor_stack.connected_entity.vehicle_automatic_targeting_parameters.auto_target_without_gunner, auto_target_with_gunner = game.get_player(pindex).cursor_stack.connected_entity.vehicle_automatic_targeting_parameters.auto_target_with_gunner} + local switch = { + auto_target_without_gunner = not game.get_player(pindex).cursor_stack.connected_entity.vehicle_automatic_targeting_parameters.auto_target_without_gunner, + auto_target_with_gunner = game.get_player(pindex).cursor_stack.connected_entity.vehicle_automatic_targeting_parameters.auto_target_with_gunner, + } game.get_player(pindex).cursor_stack.connected_entity.vehicle_automatic_targeting_parameters = switch local targetstate - if game.get_player(pindex).cursor_stack.connected_entity.vehicle_automatic_targeting_parameters.auto_target_without_gunner == true then + if + game.get_player(pindex).cursor_stack.connected_entity.vehicle_automatic_targeting_parameters.auto_target_without_gunner + == true + then targetstate = "enabled" else targetstate = "disabled" @@ -149,17 +168,26 @@ function mod.run_spider_menu(menu_index, pindex, spiderin, clicked, other_input) else if not clicked then local targetstate - if game.get_player(pindex).cursor_stack.connected_entity.vehicle_automatic_targeting_parameters.auto_target_with_gunner == true then + if + game.get_player(pindex).cursor_stack.connected_entity.vehicle_automatic_targeting_parameters.auto_target_with_gunner + == true + then targetstate = "enabled" else targetstate = "disabled" end printout("auto target enemies with gunner inside, currently" .. targetstate, pindex) else - local switch = {auto_target_without_gunner = game.get_player(pindex).cursor_stack.connected_entity.vehicle_automatic_targeting_parameters.auto_target_without_gunner, auto_target_with_gunner = not game.get_player(pindex).cursor_stack.connected_entity.vehicle_automatic_targeting_parameters.auto_target_with_gunner} + local switch = { + auto_target_without_gunner = game.get_player(pindex).cursor_stack.connected_entity.vehicle_automatic_targeting_parameters.auto_target_without_gunner, + auto_target_with_gunner = not game.get_player(pindex).cursor_stack.connected_entity.vehicle_automatic_targeting_parameters.auto_target_with_gunner, + } game.get_player(pindex).cursor_stack.connected_entity.vehicle_automatic_targeting_parameters = switch local targetstate - if game.get_player(pindex).cursor_stack.connected_entity.vehicle_automatic_targeting_parameters.auto_target_with_gunner == true then + if + game.get_player(pindex).cursor_stack.connected_entity.vehicle_automatic_targeting_parameters.auto_target_with_gunner + == true + then targetstate = "enabled" else targetstate = "disabled" @@ -179,15 +207,12 @@ function mod.run_spider_menu(menu_index, pindex, spiderin, clicked, other_input) printout("To use this menu item, link a spidertron to this remote.", pindex) end end - end end SPIDER_MENU_LENGTH = 7 function mod.spider_menu_open(pindex, stack) - if players[pindex].vanilla_mode then - return - end + if players[pindex].vanilla_mode then return end --Set the player menu tracker to this menu players[pindex].menu = "spider_menu" players[pindex].in_menu = true @@ -195,15 +220,14 @@ function mod.spider_menu_open(pindex, stack) local spider = stack --Set the menu line counter to 0 players[pindex].spider_menu.index = 0 - + --Play sound - game.get_player(pindex).play_sound{path = "Open-Inventory-Sound"} - - --Load menu + game.get_player(pindex).play_sound({ path = "Open-Inventory-Sound" }) + + --Load menu mod.run_spider_menu(players[pindex].spider_menu.index, pindex, spider, false) end - function mod.spider_menu_close(pindex, mute_in) local mute = mute_in --Set the player menu tracker to none @@ -212,29 +236,25 @@ function mod.spider_menu_close(pindex, mute_in) --Set the menu line counter to 0 players[pindex].spider_menu.index = 0 - + --play sound - if not mute then - game.get_player(pindex).play_sound{path="Close-Inventory-Sound"} - end - + if not mute then game.get_player(pindex).play_sound({ path = "Close-Inventory-Sound" }) end + --Destroy GUI if game.get_player(pindex).gui.screen["spider-rename"] ~= nil then game.get_player(pindex).gui.screen["spider-rename"].destroy() end - if game.get_player(pindex).opened ~= nil then - game.get_player(pindex).opened = nil - end + if game.get_player(pindex).opened ~= nil then game.get_player(pindex).opened = nil end end function mod.spider_menu_up(pindex, spider) players[pindex].spider_menu.index = players[pindex].spider_menu.index - 1 if players[pindex].spider_menu.index < 0 then players[pindex].spider_menu.index = 0 - game.get_player(pindex).play_sound{path = "inventory-edge"} + game.get_player(pindex).play_sound({ path = "inventory-edge" }) else --Play sound - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) end --Load menu mod.run_spider_menu(players[pindex].spider_menu.index, pindex, spider, false) @@ -244,14 +264,14 @@ function mod.spider_menu_down(pindex, spider) players[pindex].spider_menu.index = players[pindex].spider_menu.index + 1 if players[pindex].spider_menu.index > SPIDER_MENU_LENGTH then players[pindex].spider_menu.index = SPIDER_MENU_LENGTH - game.get_player(pindex).play_sound{path = "inventory-edge"} + game.get_player(pindex).play_sound({ path = "inventory-edge" }) else --Play sound - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) end --Load menu mod.run_spider_menu(players[pindex].spider_menu.index, pindex, spider, false) end -return mod \ No newline at end of file +return mod diff --git a/scripts/teleport.lua b/scripts/teleport.lua index bb44a6f1..29a5a114 100644 --- a/scripts/teleport.lua +++ b/scripts/teleport.lua @@ -8,9 +8,7 @@ local mod = {} --Teleports the player character to the cursor position. function mod.teleport_to_cursor(pindex, muted, ignore_enemies, return_cursor) local result = mod.teleport_to_closest(pindex, players[pindex].cursor_pos, muted, ignore_enemies) - if return_cursor then - players[pindex].cursor_pos = players[pindex].position - end + if return_cursor then players[pindex].cursor_pos = players[pindex].position end return result end @@ -20,11 +18,11 @@ function mod.teleport_to_closest(pindex, pos, muted, ignore_enemies) local muted = muted or false local first_player = game.get_player(pindex) local surf = first_player.surface - local radius = .5 - local new_pos = surf.find_non_colliding_position("character", pos, radius, .1, true) + local radius = 0.5 + local new_pos = surf.find_non_colliding_position("character", pos, radius, 0.1, true) while new_pos == nil do radius = radius + 1 - new_pos = surf.find_non_colliding_position("character", pos, radius, .1, true) + new_pos = surf.find_non_colliding_position("character", pos, radius, 0.1, true) end --Do not teleport if in a vehicle, in a menu, or already at the desitination if first_player.vehicle ~= nil and first_player.vehicle.valid then @@ -33,31 +31,62 @@ function mod.teleport_to_closest(pindex, pos, muted, ignore_enemies) elseif util.distance(game.get_player(pindex).position, pos) <= 1.5 then printout("Already at target", pindex) return false - elseif players[pindex].in_menu and players[pindex].menu ~= "travel" and players[pindex].menu ~= "structure-travel" then + elseif + players[pindex].in_menu + and players[pindex].menu ~= "travel" + and players[pindex].menu ~= "structure-travel" + then printout("Cannot teleport while in a menu.", pindex) return false end --Do not teleport near enemies unless instructed to ignore them if not ignore_enemies then - local enemy = first_player.surface.find_nearest_enemy{position = new_pos, max_distance = 30, force = first_player.force} + local enemy = + first_player.surface.find_nearest_enemy({ position = new_pos, max_distance = 30, force = first_player.force }) if enemy and enemy.valid then - printout("Warning: There are enemies at this location, but you can force teleporting if you press CONTROL + SHIFT + T", pindex) + printout( + "Warning: There are enemies at this location, but you can force teleporting if you press CONTROL + SHIFT + T", + pindex + ) return false end end --Attempt teleport - local can_port = first_player.surface.can_place_entity{name = "character", position = new_pos} + local can_port = first_player.surface.can_place_entity({ name = "character", position = new_pos }) if can_port then local old_pos = table.deepcopy(first_player.position) if not muted then --Teleporting visuals at origin - rendering.draw_circle{color = {0.8, 0.2, 0.0},radius = 0.5,width = 15,target = old_pos, surface = first_player.surface, draw_on_ground = true, time_to_live = 60} - rendering.draw_circle{color = {0.6, 0.1, 0.1},radius = 0.3,width = 20,target = old_pos, surface = first_player.surface, draw_on_ground = true, time_to_live = 60} - local smoke_effect = first_player.surface.create_entity{name = "iron-chest", position = first_player.position, raise_built = false, force = first_player.force} - smoke_effect.destroy{} + rendering.draw_circle({ + color = { 0.8, 0.2, 0.0 }, + radius = 0.5, + width = 15, + target = old_pos, + surface = first_player.surface, + draw_on_ground = true, + time_to_live = 60, + }) + rendering.draw_circle({ + color = { 0.6, 0.1, 0.1 }, + radius = 0.3, + width = 20, + target = old_pos, + surface = first_player.surface, + draw_on_ground = true, + time_to_live = 60, + }) + local smoke_effect = first_player.surface.create_entity({ + name = "iron-chest", + position = first_player.position, + raise_built = false, + force = first_player.force, + }) + smoke_effect.destroy({}) --Teleport sound at origin - game.get_player(pindex).play_sound{path = "player-teleported", volume_modifier = 0.2, position = old_pos} - game.get_player(pindex).play_sound{path = "utility/scenario_message", volume_modifier = 0.8, position = old_pos} + game.get_player(pindex).play_sound({ path = "player-teleported", volume_modifier = 0.2, position = old_pos }) + game + .get_player(pindex) + .play_sound({ path = "utility/scenario_message", volume_modifier = 0.8, position = old_pos }) end local teleported = false if muted then @@ -66,34 +95,69 @@ function mod.teleport_to_closest(pindex, pos, muted, ignore_enemies) teleported = first_player.teleport(new_pos) end if teleported then - first_player.force.chart(first_player.surface, {{new_pos.x-15,new_pos.y-15},{new_pos.x+15,new_pos.y+15}}) + first_player.force.chart( + first_player.surface, + { { new_pos.x - 15, new_pos.y - 15 }, { new_pos.x + 15, new_pos.y + 15 } } + ) players[pindex].position = table.deepcopy(new_pos) reset_bump_stats(pindex) if not muted then --Teleporting visuals at target - rendering.draw_circle{color = {0.3, 0.3, 0.9},radius = 0.5,width = 15,target = new_pos, surface = first_player.surface, draw_on_ground = true, time_to_live = 60} - rendering.draw_circle{color = {0.0, 0.0, 0.9},radius = 0.3,width = 20,target = new_pos, surface = first_player.surface, draw_on_ground = true, time_to_live = 60} - local smoke_effect = first_player.surface.create_entity{name = "iron-chest", position = first_player.position, raise_built = false, force = first_player.force} - smoke_effect.destroy{} + rendering.draw_circle({ + color = { 0.3, 0.3, 0.9 }, + radius = 0.5, + width = 15, + target = new_pos, + surface = first_player.surface, + draw_on_ground = true, + time_to_live = 60, + }) + rendering.draw_circle({ + color = { 0.0, 0.0, 0.9 }, + radius = 0.3, + width = 20, + target = new_pos, + surface = first_player.surface, + draw_on_ground = true, + time_to_live = 60, + }) + local smoke_effect = first_player.surface.create_entity({ + name = "iron-chest", + position = first_player.position, + raise_built = false, + force = first_player.force, + }) + smoke_effect.destroy({}) --Teleport sound at target - game.get_player(pindex).play_sound{path = "player-teleported", volume_modifier = 0.2, position = new_pos} - game.get_player(pindex).play_sound{path = "utility/scenario_message", volume_modifier = 0.8, position = new_pos} + game + .get_player(pindex) + .play_sound({ path = "player-teleported", volume_modifier = 0.2, position = new_pos }) + game + .get_player(pindex) + .play_sound({ path = "utility/scenario_message", volume_modifier = 0.8, position = new_pos }) end if new_pos.x ~= pos.x or new_pos.y ~= pos.y then if not muted then - printout("Teleported " .. math.ceil(fa_utils.distance(pos,first_player.position)) .. " " .. fa_utils.direction(pos, first_player.position) .. " of target", pindex) + printout( + "Teleported " + .. math.ceil(fa_utils.distance(pos, first_player.position)) + .. " " + .. fa_utils.direction(pos, first_player.position) + .. " of target", + pindex + ) end end --Update cursor after teleport players[pindex].cursor_pos = table.deepcopy(new_pos) - fa_mouse.move_mouse_pointer(fa_utils.center_of_tile(players[pindex].cursor_pos),pindex) - fa_graphics.draw_cursor_highlight(pindex,nil,nil) + fa_mouse.move_mouse_pointer(fa_utils.center_of_tile(players[pindex].cursor_pos), pindex) + fa_graphics.draw_cursor_highlight(pindex, nil, nil) else printout("Teleport Failed", pindex) return false end else - printout("Cannot teleport", pindex)--this is unlikely to be reached because we find the first non-colliding position + printout("Cannot teleport", pindex) --this is unlikely to be reached because we find the first non-colliding position return false end @@ -103,4 +167,4 @@ function mod.teleport_to_closest(pindex, pos, muted, ignore_enemies) return true end -return mod \ No newline at end of file +return mod diff --git a/scripts/train-stops.lua b/scripts/train-stops.lua index a343c5fd..66ffdf71 100644 --- a/scripts/train-stops.lua +++ b/scripts/train-stops.lua @@ -8,7 +8,7 @@ function mod.run_train_stop_menu(menu_index, pindex, clicked, other_input) local index = menu_index local other = other_input or -1 local train_stop = nil - if players[pindex].tile.ents[1] ~= nil and players[pindex].tile.ents[1].name == "train-stop" then + if players[pindex].tile.ents[1] ~= nil and players[pindex].tile.ents[1].name == "train-stop" then train_stop = players[pindex].tile.ents[1] players[pindex].train_stop_menu.stop = train_stop else @@ -16,32 +16,45 @@ function mod.run_train_stop_menu(menu_index, pindex, clicked, other_input) players[pindex].train_stop_menu.stop = nil return end - + if index == 0 then - printout("Train stop " .. train_stop.backer_name .. ", Press W and S to navigate options, press LEFT BRACKET to select an option or press E to exit this menu.", pindex) + printout( + "Train stop " + .. train_stop.backer_name + .. ", Press W and S to navigate options, press LEFT BRACKET to select an option or press E to exit this menu.", + pindex + ) elseif index == 1 then if not clicked then printout("Select here to rename this train stop.", pindex) else - printout("Enter a new name for this train stop, then press 'ENTER' to confirm, or press 'ESC' to cancel.", pindex) + printout( + "Enter a new name for this train stop, then press 'ENTER' to confirm, or press 'ESC' to cancel.", + pindex + ) players[pindex].train_stop_menu.renaming = true - local frame = game.get_player(pindex).gui.screen.add{type = "frame", name = "train-stop-rename"} + local frame = game.get_player(pindex).gui.screen.add({ type = "frame", name = "train-stop-rename" }) frame.bring_to_front() frame.force_auto_center() frame.focus() game.get_player(pindex).opened = frame - local input = frame.add{type="textfield", name = "input"} + local input = frame.add({ type = "textfield", name = "input" }) input.focus() end elseif index == 2 then local result = mod.nearby_train_schedule_read_this_stop(train_stop) - printout(result .. ", Use the below menu options to modify the train schedule.",pindex) + printout(result .. ", Use the below menu options to modify the train schedule.", pindex) elseif index == 3 then if not clicked then if players[pindex].train_stop_menu.wait_condition == nil then players[pindex].train_stop_menu.wait_condition = "time" end - printout("Proposed wait condition: " .. players[pindex].train_stop_menu.wait_condition .. " selected, change by selecting here, this change needs to also be applied.",pindex) + printout( + "Proposed wait condition: " + .. players[pindex].train_stop_menu.wait_condition + .. " selected, change by selecting here, this change needs to also be applied.", + pindex + ) else local condi = players[pindex].train_stop_menu.wait_condition if condi == "time" then @@ -58,13 +71,23 @@ function mod.run_train_stop_menu(menu_index, pindex, clicked, other_input) condi = "time" end players[pindex].train_stop_menu.wait_condition = condi - printout(" " .. players[pindex].train_stop_menu.wait_condition .. " condition proposed, change by selecting here, this change needs to also be applied.",pindex) + printout( + " " + .. players[pindex].train_stop_menu.wait_condition + .. " condition proposed, change by selecting here, this change needs to also be applied.", + pindex + ) end elseif index == 4 then if players[pindex].train_stop_menu.wait_time_seconds == nil then players[pindex].train_stop_menu.wait_time_seconds = 60 end - printout("Proposed wait time: " .. players[pindex].train_stop_menu.wait_time_seconds .. " seconds selected, if applicable, change using page up or page down, and hold control to increase step size. This change needs to also be applied.",pindex) + printout( + "Proposed wait time: " + .. players[pindex].train_stop_menu.wait_time_seconds + .. " seconds selected, if applicable, change using page up or page down, and hold control to increase step size. This change needs to also be applied.", + pindex + ) elseif index == 5 then if not clicked then if players[pindex].train_stop_menu.safety_wait_enabled == nil then @@ -72,65 +95,82 @@ function mod.run_train_stop_menu(menu_index, pindex, clicked, other_input) end local result = "" if players[pindex].train_stop_menu.safety_wait_enabled == true then - result = "ENABLED proposed safety waiting, select here to disable it, Enabling it makes the train wait at this stop for 5 seconds regardless of the main wait condition, this change needs to also be applied." + result = + "ENABLED proposed safety waiting, select here to disable it, Enabling it makes the train wait at this stop for 5 seconds regardless of the main wait condition, this change needs to also be applied." else - result = "DISABLED proposed safety waiting, select here to enable it, Enabling it makes the train wait at this stop for 5 seconds regardless of the main wait condition, this change needs to also be applied." + result = + "DISABLED proposed safety waiting, select here to enable it, Enabling it makes the train wait at this stop for 5 seconds regardless of the main wait condition, this change needs to also be applied." end - printout(result,pindex) + printout(result, pindex) else players[pindex].train_stop_menu.safety_wait_enabled = not players[pindex].train_stop_menu.safety_wait_enabled if players[pindex].train_stop_menu.safety_wait_enabled == true then - result = "ENABLED proposed safety waiting, select here to disable it, Enabling it makes the train wait at this stop for 5 seconds regardless of the main wait condition, this change needs to also be applied." + result = + "ENABLED proposed safety waiting, select here to disable it, Enabling it makes the train wait at this stop for 5 seconds regardless of the main wait condition, this change needs to also be applied." else - result = "DISABLED proposed safety waiting, select here to enable it, Enabling it makes the train wait at this stop for 5 seconds regardless of the main wait condition, this change needs to also be applied." + result = + "DISABLED proposed safety waiting, select here to enable it, Enabling it makes the train wait at this stop for 5 seconds regardless of the main wait condition, this change needs to also be applied." end - printout(result,pindex) + printout(result, pindex) end elseif index == 6 then if not clicked then - printout("ADD A NEW ENTRY for this train stop by selecting here, with the proposed conditions applied, for a train parked by this train stop.",pindex) + printout( + "ADD A NEW ENTRY for this train stop by selecting here, with the proposed conditions applied, for a train parked by this train stop.", + pindex + ) else - local result = mod.nearby_train_schedule_add_stop(train_stop, players[pindex].train_stop_menu.wait_condition, players[pindex].train_stop_menu.wait_time_seconds) - printout(result,pindex) + local result = mod.nearby_train_schedule_add_stop( + train_stop, + players[pindex].train_stop_menu.wait_condition, + players[pindex].train_stop_menu.wait_time_seconds + ) + printout(result, pindex) end elseif index == 7 then if not clicked then - printout("UPDATE ALL ENTRIES for this train stop by selecting here, with the proposed conditions applied, for a train parked by this train stop.",pindex) + printout( + "UPDATE ALL ENTRIES for this train stop by selecting here, with the proposed conditions applied, for a train parked by this train stop.", + pindex + ) else - local result = mod.nearby_train_schedule_update_stop(train_stop, players[pindex].train_stop_menu.wait_condition, players[pindex].train_stop_menu.wait_time_seconds) - printout(result,pindex) + local result = mod.nearby_train_schedule_update_stop( + train_stop, + players[pindex].train_stop_menu.wait_condition, + players[pindex].train_stop_menu.wait_time_seconds + ) + printout(result, pindex) end elseif index == 8 then if not clicked then - printout("REMOVE ALL ENTRIES for this train stop by selecting here, for a train parked by this train stop.",pindex) + printout( + "REMOVE ALL ENTRIES for this train stop by selecting here, for a train parked by this train stop.", + pindex + ) else local result = mod.nearby_train_schedule_remove_stop(train_stop) - printout(result,pindex) + printout(result, pindex) end end end - function mod.train_stop_menu_open(pindex) - if players[pindex].vanilla_mode then - return - end + if players[pindex].vanilla_mode then return end --Set the player menu tracker to this menu players[pindex].menu = "train_stop_menu" players[pindex].in_menu = true players[pindex].move_queue = {} - + --Set the menu line counter to 0 players[pindex].train_stop_menu.index = 0 - + --Play sound - game.get_player(pindex).play_sound{path = "Open-Inventory-Sound"} - - --Load menu + game.get_player(pindex).play_sound({ path = "Open-Inventory-Sound" }) + + --Load menu mod.run_train_stop_menu(players[pindex].train_stop_menu.index, pindex, false) end - function mod.train_stop_menu_close(pindex, mute_in) local mute = mute_in --Set the player menu tracker to none @@ -139,52 +179,46 @@ function mod.train_stop_menu_close(pindex, mute_in) --Set the menu line counter to 0 players[pindex].train_stop_menu.index = 0 - + --Destroy GUI if game.get_player(pindex).gui.screen["train-stop-rename"] ~= nil then game.get_player(pindex).gui.screen["train-stop-rename"].destroy() end - + --play sound - if not mute then - game.get_player(pindex).play_sound{path="Close-Inventory-Sound"} - end + if not mute then game.get_player(pindex).play_sound({ path = "Close-Inventory-Sound" }) end end - function mod.train_stop_menu_up(pindex) players[pindex].train_stop_menu.index = players[pindex].train_stop_menu.index - 1 if players[pindex].train_stop_menu.index < 0 then players[pindex].train_stop_menu.index = 0 - game.get_player(pindex).play_sound{path = "inventory-edge"} + game.get_player(pindex).play_sound({ path = "inventory-edge" }) else --Play sound - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) end - --Load menu + --Load menu mod.run_train_stop_menu(players[pindex].train_stop_menu.index, pindex, false) end - function mod.train_stop_menu_down(pindex) players[pindex].train_stop_menu.index = players[pindex].train_stop_menu.index + 1 if players[pindex].train_stop_menu.index > 8 then players[pindex].train_stop_menu.index = 8 - game.get_player(pindex).play_sound{path = "inventory-edge"} + game.get_player(pindex).play_sound({ path = "inventory-edge" }) else --Play sound - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) end - --Load menu + --Load menu mod.run_train_stop_menu(players[pindex].train_stop_menu.index, pindex, false) end ---For the selected train stop, changes assigned wait time in seconds for the parked train. The increment is a positive or negative integer. -function mod.nearby_train_schedule_add_to_wait_time(increment,pindex) +--For the selected train stop, changes assigned wait time in seconds for the parked train. The increment is a positive or negative integer. +function mod.nearby_train_schedule_add_to_wait_time(increment, pindex) local seconds = players[pindex].train_stop_menu.wait_time_seconds - if seconds == nil then - seconds = 300 - end + if seconds == nil then seconds = 300 end seconds = seconds + increment if seconds < 5 then seconds = 5 @@ -192,7 +226,7 @@ function mod.nearby_train_schedule_add_to_wait_time(increment,pindex) seconds = 10000 end players[pindex].train_stop_menu.wait_time_seconds = seconds - printout(players[pindex].train_stop_menu.wait_time_seconds .. " seconds wait time set.",pindex) + printout(players[pindex].train_stop_menu.wait_time_seconds .. " seconds wait time set.", pindex) end --Returns an info string on what the parked train at this stop is scheduled to do at this stop. @@ -203,7 +237,8 @@ function mod.nearby_train_schedule_read_this_stop(train_stop) --Locate the nearby train local train = train_stop.get_stopped_train() if train == nil or not train.valid then - local locos = train_stop.surface.find_entities_filtered{position = train_stop.position, radius = 5, name = "locomotive"} + local locos = + train_stop.surface.find_entities_filtered({ position = train_stop.position, radius = 5, name = "locomotive" }) if locos[1] ~= nil and locos[1].valid then train = locos[1].train else @@ -223,7 +258,7 @@ function mod.nearby_train_schedule_read_this_stop(train_stop) else local records = schedule.records result = "Reading parked train, " - for i,r in ipairs(records) do + for i, r in ipairs(records) do if r.station == train_stop.backer_name then found_any = true result = result .. ", at this stop it waits for " @@ -243,7 +278,7 @@ function mod.nearby_train_schedule_read_this_stop(train_stop) end end end - + if found_any == false then result = "Reading parked train: Error: The nearby train schedule does not contain this train stop," end @@ -256,7 +291,8 @@ function mod.nearby_train_schedule_add_stop(train_stop, wait_condition_type, wai --Locate the nearby train local train = train_stop.get_stopped_train() if train == nil or not train.valid then - local locos = train_stop.surface.find_entities_filtered{position = train_stop.position, radius = 5, name = "locomotive"} + local locos = + train_stop.surface.find_entities_filtered({ position = train_stop.position, radius = 5, name = "locomotive" }) if locos[1] ~= nil and locos[1].valid then train = locos[1].train else @@ -269,20 +305,24 @@ function mod.nearby_train_schedule_add_stop(train_stop, wait_condition_type, wai return result end --Create new record - local wait_condition_1 = {type = wait_condition_type , ticks = wait_time_seconds * 60 , compare_type = "and"} - local wait_condition_2 = {type = "time", ticks = 300, compare_type = "and"} - local new_record = {wait_conditions = {wait_condition_1}, station = train_stop.backer_name, temporary = false} + local wait_condition_1 = { type = wait_condition_type, ticks = wait_time_seconds * 60, compare_type = "and" } + local wait_condition_2 = { type = "time", ticks = 300, compare_type = "and" } + local new_record = { wait_conditions = { wait_condition_1 }, station = train_stop.backer_name, temporary = false } if players[pindex].train_stop_menu.safety_wait_enabled then - new_record = {wait_conditions = {wait_condition_1,wait_condition_2}, station = train_stop.backer_name, temporary = false} + new_record = { + wait_conditions = { wait_condition_1, wait_condition_2 }, + station = train_stop.backer_name, + temporary = false, + } end --Copy and modify the schedule local schedule = train.schedule local records = nil if schedule == nil then - schedule = {current = 1, records = {new_record}} + schedule = { current = 1, records = { new_record } } else records = schedule.records - table.insert(records,#records+1, new_record) + table.insert(records, #records + 1, new_record) end --Apply the new schedule train.manual_mode = true @@ -298,7 +338,8 @@ function mod.nearby_train_schedule_update_stop(train_stop, wait_condition_type, --Locate the nearby train local train = train_stop.get_stopped_train() if train == nil or not train.valid then - local locos = train_stop.surface.find_entities_filtered{position = train_stop.position, radius = 5, name = "locomotive"} + local locos = + train_stop.surface.find_entities_filtered({ position = train_stop.position, radius = 5, name = "locomotive" }) if locos[1] ~= nil and locos[1].valid then train = locos[1].train else @@ -311,11 +352,15 @@ function mod.nearby_train_schedule_update_stop(train_stop, wait_condition_type, return result end --Create new record - local wait_condition_1 = {type = wait_condition_type , ticks = wait_time_seconds * 60 , compare_type = "and"} - local wait_condition_2 = {type = "time", ticks = 300, compare_type = "and"} - local new_record = {wait_conditions = {wait_condition_1}, station = train_stop.backer_name, temporary = false} + local wait_condition_1 = { type = wait_condition_type, ticks = wait_time_seconds * 60, compare_type = "and" } + local wait_condition_2 = { type = "time", ticks = 300, compare_type = "and" } + local new_record = { wait_conditions = { wait_condition_1 }, station = train_stop.backer_name, temporary = false } if players[pindex].train_stop_menu.safety_wait_enabled then - new_record = {wait_conditions = {wait_condition_1,wait_condition_2}, station = train_stop.backer_name, temporary = false} + new_record = { + wait_conditions = { wait_condition_1, wait_condition_2 }, + station = train_stop.backer_name, + temporary = false, + } end --Copy and modify the schedule local schedule = train.schedule @@ -327,13 +372,13 @@ function mod.nearby_train_schedule_update_stop(train_stop, wait_condition_type, else records = schedule.records local new_records = {} - for i,r in ipairs(records) do + for i, r in ipairs(records) do if r.station == train_stop.backer_name then updated_any = true - table.insert(new_records,new_record) + table.insert(new_records, new_record) --game.get_player(pindex).print(" hit " .. i) else - table.insert(new_records,r) + table.insert(new_records, r) --game.get_player(pindex).print(" miss " .. i) end end @@ -357,7 +402,8 @@ function mod.nearby_train_schedule_remove_stop(train_stop) --Locate the nearby train local train = train_stop.get_stopped_train() if train == nil or not train.valid then - local locos = train_stop.surface.find_entities_filtered{position = train_stop.position, radius = 5, name = "locomotive"} + local locos = + train_stop.surface.find_entities_filtered({ position = train_stop.position, radius = 5, name = "locomotive" }) if locos[1] ~= nil and locos[1].valid then train = locos[1].train else @@ -379,13 +425,13 @@ function mod.nearby_train_schedule_remove_stop(train_stop) else records = schedule.records local new_records = {} - for i,r in ipairs(records) do + for i, r in ipairs(records) do if r.station == train_stop.backer_name then records[i] = nil updated_any = true --game.get_player(pindex).print(" hit ".. i) else - table.insert(new_records,r) + table.insert(new_records, r) --game.get_player(pindex).print(" miss ".. i) end end diff --git a/scripts/trains.lua b/scripts/trains.lua index f50cb1bd..608fd311 100644 --- a/scripts/trains.lua +++ b/scripts/trains.lua @@ -1,8 +1,8 @@ --Here: Functions relating to train topics such as train info, train stops, train scheduling --Does not include event handlers -local util = require('util') -local fa_utils = require('scripts.fa-utils') +local util = require("util") +local fa_utils = require("scripts.fa-utils") local fa_rails = require("scripts.rails") local dirs = defines.direction @@ -18,7 +18,7 @@ function mod.get_train_state_info(train) else train_state_text = "None" end - + --Explanations if train_state_text == "wait_station" then train_state_text = "waiting at a station" @@ -35,29 +35,23 @@ function mod.get_train_name(train) local locos = train.locomotives local train_name = "" local multiple_names = false - - if locos == nil then - return "without locomotives" - end - - for i,loco in ipairs(locos["front_movers"]) do - if train_name ~= "" and train_name ~= loco.backer_name then - multiple_names = true - end + + if locos == nil then return "without locomotives" end + + for i, loco in ipairs(locos["front_movers"]) do + if train_name ~= "" and train_name ~= loco.backer_name then multiple_names = true end train_name = loco.backer_name end - for i,loco in ipairs(locos["back_movers"]) do - if train_name ~= "" and train_name ~= loco.backer_name then - multiple_names = true - end + for i, loco in ipairs(locos["back_movers"]) do + if train_name ~= "" and train_name ~= loco.backer_name then multiple_names = true end train_name = loco.backer_name end - + if train_name == "" then return "without a name" elseif multiple_names then local oldest_name = mod.resolve_train_name(train) - mod.set_train_name(train,oldest_name) + mod.set_train_name(train, oldest_name) return oldest_name else return train_name @@ -65,18 +59,14 @@ function mod.get_train_name(train) end --Sets a train's name. The idea is that every locomotive on a train has the same backer name and this is the train's name. -function mod.set_train_name(train,new_name) - if new_name == nil or new_name == "" then - return false - end +function mod.set_train_name(train, new_name) + if new_name == nil or new_name == "" then return false end local locos = train.locomotives - if locos == nil then - return false - end - for i,loco in ipairs(locos["front_movers"]) do + if locos == nil then return false end + for i, loco in ipairs(locos["front_movers"]) do loco.backer_name = new_name end - for i,loco in ipairs(locos["back_movers"]) do + for i, loco in ipairs(locos["back_movers"]) do loco.backer_name = new_name end return true @@ -86,26 +76,24 @@ end function mod.resolve_train_name(train) local locos = train.locomotives local oldest_loco = nil - - if locos == nil then - return "without locomotives" - end - - for i,loco in ipairs(locos["front_movers"]) do + + if locos == nil then return "without locomotives" end + + for i, loco in ipairs(locos["front_movers"]) do if oldest_loco == nil then oldest_loco = loco elseif oldest_loco.unit_number > loco.unit_number then oldest_loco = loco end end - for i,loco in ipairs(locos["back_movers"]) do + for i, loco in ipairs(locos["back_movers"]) do if oldest_loco == nil then oldest_loco = loco elseif oldest_loco.unit_number > loco.unit_number then oldest_loco = loco end end - + if oldest_loco ~= nil then return oldest_loco.backer_name else @@ -115,7 +103,7 @@ end --Checks if the train is all in one segment, which means the front and back rails are in the same segment. function mod.train_is_all_in_one_segment(train) - return train.front_rail.is_rail_in_same_rail_segment_as(train.back_rail) + return train.front_rail.is_rail_in_same_rail_segment_as(train.back_rail) end --[[ @@ -135,31 +123,27 @@ function mod.get_relative_leading_rail_and_train_dir(pindex, train) local vehicle = game.get_player(pindex).vehicle local front_rail = train.front_rail - local back_rail = train.back_rail + local back_rail = train.back_rail local locos = train.locomotives local vehicle_is_a_front_loco = nil - - --Find the leading rail. If any "front" locomotive velocity is positive, the front stock is the one going ahead and its rail is the leading rail. + + --Find the leading rail. If any "front" locomotive velocity is positive, the front stock is the one going ahead and its rail is the leading rail. if vehicle.name == "locomotive" then --Leading direction is the one this loconotive faces - for i,loco in ipairs(locos["front_movers"]) do - if vehicle.unit_number == loco.unit_number then - vehicle_is_a_front_loco = true - end + for i, loco in ipairs(locos["front_movers"]) do + if vehicle.unit_number == loco.unit_number then vehicle_is_a_front_loco = true end end if vehicle_is_a_front_loco == true then leading_rail = front_rail - trailing_rail = back_rail - leading_stock = train.front_stock + trailing_rail = back_rail + leading_stock = train.front_stock else - for i,loco in ipairs(locos["back_movers"]) do - if vehicle.unit_number == loco.unit_number then - vehicle_is_a_front_loco = false - end + for i, loco in ipairs(locos["back_movers"]) do + if vehicle.unit_number == loco.unit_number then vehicle_is_a_front_loco = false end end if vehicle_is_a_front_loco == false then leading_rail = back_rail - trailing_rail = front_rail + trailing_rail = front_rail leading_stock = train.back_stock else --Unexpected place @@ -169,43 +153,59 @@ function mod.get_relative_leading_rail_and_train_dir(pindex, train) else --Just assume the front stock is leading leading_rail = front_rail - trailing_rail = back_rail + trailing_rail = back_rail leading_stock = train.front_stock end - + --Error check - if leading_rail == nil then - return nil, -2, nil - end - - --Find the ahead direction. For the leading rail, the connected rail that is farthest from the leading stock is in the "ahead" direction. + if leading_rail == nil then return nil, -2, nil end + + --Find the ahead direction. For the leading rail, the connected rail that is farthest from the leading stock is in the "ahead" direction. --Repurpose the variables named front_rail and back_rail - front_rail = leading_rail.get_connected_rail{ rail_direction = defines.rail_direction.front, rail_connection_direction = defines.rail_connection_direction.straight} + front_rail = leading_rail.get_connected_rail({ + rail_direction = defines.rail_direction.front, + rail_connection_direction = defines.rail_connection_direction.straight, + }) if front_rail == nil then - front_rail = leading_rail.get_connected_rail{ rail_direction = defines.rail_direction.front, rail_connection_direction = defines.rail_connection_direction.left} + front_rail = leading_rail.get_connected_rail({ + rail_direction = defines.rail_direction.front, + rail_connection_direction = defines.rail_connection_direction.left, + }) end if front_rail == nil then - front_rail = leading_rail.get_connected_rail{ rail_direction = defines.rail_direction.front, rail_connection_direction = defines.rail_connection_direction.right} + front_rail = leading_rail.get_connected_rail({ + rail_direction = defines.rail_direction.front, + rail_connection_direction = defines.rail_connection_direction.right, + }) end if front_rail == nil then --The leading rail is an end rail at the front direction return leading_rail, defines.rail_direction.front, leading_stock end - - back_rail = leading_rail.get_connected_rail{ rail_direction = defines.rail_direction.back, rail_connection_direction = defines.rail_connection_direction.straight} + + back_rail = leading_rail.get_connected_rail({ + rail_direction = defines.rail_direction.back, + rail_connection_direction = defines.rail_connection_direction.straight, + }) if back_rail == nil then - back_rail = leading_rail.get_connected_rail{ rail_direction = defines.rail_direction.back, rail_connection_direction = defines.rail_connection_direction.left} + back_rail = leading_rail.get_connected_rail({ + rail_direction = defines.rail_direction.back, + rail_connection_direction = defines.rail_connection_direction.left, + }) end if back_rail == nil then - back_rail = leading_rail.get_connected_rail{ rail_direction = defines.rail_direction.back, rail_connection_direction = defines.rail_connection_direction.right} + back_rail = leading_rail.get_connected_rail({ + rail_direction = defines.rail_direction.back, + rail_connection_direction = defines.rail_connection_direction.right, + }) end if back_rail == nil then --The leading rail is an end rail at the back direction return leading_rail, defines.rail_direction.back, leading_stock end - - local front_dist = math.abs(util.distance(leading_stock.position, front_rail.position)) - local back_dist = math.abs(util.distance(leading_stock.position, back_rail.position)) + + local front_dist = math.abs(util.distance(leading_stock.position, front_rail.position)) + local back_dist = math.abs(util.distance(leading_stock.position, back_rail.position)) --The connected rail that is farther from the leading stock is in the ahead direction. if front_dist > back_dist then return leading_rail, defines.rail_direction.front, leading_stock @@ -229,80 +229,75 @@ function mod.train_read_next_rail_entity_ahead(pindex, invert, mute_in) local message = "Ahead, " local honk_score = 0 local train = game.get_player(pindex).vehicle.train - local leading_rail, dir_ahead, leading_stock = mod.get_relative_leading_rail_and_train_dir(pindex,train) + local leading_rail, dir_ahead, leading_stock = mod.get_relative_leading_rail_and_train_dir(pindex, train) if invert then dir_ahead = fa_rails.get_opposite_rail_direction(dir_ahead) - message = "Behind, " + message = "Behind, " end --Correction for trains: Flip the correct direction ahead for mismatching diagonal rails - if leading_rail.name == "straight-rail" and (leading_rail.direction == dirs.southwest or leading_rail.direction == dirs.northwest) then + if + leading_rail.name == "straight-rail" + and (leading_rail.direction == dirs.southwest or leading_rail.direction == dirs.northwest) + then dir_ahead = fa_rails.get_opposite_rail_direction(dir_ahead) end --Correction for trains: Curved rails report different directions based on where the train sits and so are unreliable. if leading_rail.name == "curved-rail" then - if mute_in == true then - return -1 - end - printout("Curved rail analysis error, check from another rail.",pindex) + if mute_in == true then return -1 end + printout("Curved rail analysis error, check from another rail.", pindex) return -1 end - local next_entity, next_entity_label, result_extra, next_is_forward, iteration_count = fa_rails.get_next_rail_entity_ahead(leading_rail, dir_ahead, false) + local next_entity, next_entity_label, result_extra, next_is_forward, iteration_count = + fa_rails.get_next_rail_entity_ahead(leading_rail, dir_ahead, false) if next_entity == nil then - if mute_in == true then - return -1 - end - printout("Analysis error, this rail might be looping.",pindex) + if mute_in == true then return -1 end + printout("Analysis error, this rail might be looping.", pindex) return -1 end local distance = math.floor(util.distance(leading_stock.position, next_entity.position)) - if distance < 10 then - honk_score = honk_score + 1 - end - + if distance < 10 then honk_score = honk_score + 1 end + --Test message --message = message .. iteration_count .. " iterations, " - + --Maybe check for trains here, but there is no point because the checks use signal blocks... --local trains_in_origin_block = origin_rail.trains_in_block --local trains_in_current_block = current_rail.trains_in_block - + --Report opposite direction entities. - if next_is_forward == false and (next_entity_label == "train stop" or next_entity_label == "rail signal" or next_entity_label == "chain signal") then + if + next_is_forward == false + and ( + next_entity_label == "train stop" + or next_entity_label == "rail signal" + or next_entity_label == "chain signal" + ) + then message = message .. " Opposite direction's " honk_score = -100 end - + --Add more info depending on entity label if next_entity_label == "end rail" then message = message .. next_entity_label - elseif next_entity_label == "fork split" then - local entering_segment_rail = result_extra + local entering_segment_rail = result_extra message = message .. "rail fork splitting " message = message .. fa_rails.list_rail_fork_directions(next_entity) - elseif next_entity_label == "fork merge" then - local entering_segment_rail = result_extra + local entering_segment_rail = result_extra message = message .. "rail fork merging " - elseif next_entity_label == "neighbor end" then - local entering_segment_rail = result_extra + local entering_segment_rail = result_extra message = message .. "end rail " - elseif next_entity_label == "rail signal" then local signal_state = fa_rails.get_signal_state_info(next_entity) message = message .. "rail signal with state " .. signal_state .. " " - if signal_state == "closed" then - honk_score = honk_score + 1 - end - + if signal_state == "closed" then honk_score = honk_score + 1 end elseif next_entity_label == "chain signal" then local signal_state = fa_rails.get_signal_state_info(next_entity) message = message .. "chain signal with state " .. signal_state .. " " - if signal_state == "closed" then - honk_score = honk_score + 1 - end - + if signal_state == "closed" then honk_score = honk_score + 1 end elseif next_entity_label == "train stop" then local stop_name = next_entity.backer_name --Add more specific distance info @@ -313,19 +308,23 @@ function mod.train_read_next_rail_entity_ahead(pindex, invert, mute_in) if math.abs(distance) <= 0.2 then message = " Aligned with train stop " .. stop_name elseif distance > 0.2 then - message = math.floor(distance * 10) / 10 .. " meters away from train stop " .. stop_name .. ", for the frontmost vehicle. " + message = math.floor(distance * 10) / 10 + .. " meters away from train stop " + .. stop_name + .. ", for the frontmost vehicle. " elseif distance < 0.2 then - message = math.floor((-distance) * 10) / 10 .. " meters past train stop " .. stop_name .. ", for the frontmost vehicle. " + message = math.floor(-distance * 10) / 10 + .. " meters past train stop " + .. stop_name + .. ", for the frontmost vehicle. " end end - elseif next_entity_label == "other rail" then message = message .. "unspecified entity" - elseif next_entity_label == "other entity" then message = message .. next_entity.name end - + --Add general distance info if next_entity_label ~= "train stop" then message = message .. " in " .. distance .. " meters. " @@ -342,41 +341,60 @@ function mod.train_read_next_rail_entity_ahead(pindex, invert, mute_in) local first_reset = false --Scan behind the leading stock for 15m for passed train stops if heading == "North" then --scan the south - scan_area = {{pos.x-4,pos.y-4},{pos.x+4,pos.y+15}} + scan_area = { { pos.x - 4, pos.y - 4 }, { pos.x + 4, pos.y + 15 } } elseif heading == "South" then - scan_area = {{pos.x-4,pos.y-15},{pos.x+4,pos.y+4}} + scan_area = { { pos.x - 4, pos.y - 15 }, { pos.x + 4, pos.y + 4 } } elseif heading == "East" then --scan the west - scan_area = {{pos.x-15,pos.y-4},{pos.x+4,pos.y+4}} + scan_area = { { pos.x - 15, pos.y - 4 }, { pos.x + 4, pos.y + 4 } } elseif heading == "West" then - scan_area = {{pos.x-4,pos.y-4},{pos.x+15,pos.y+4}} + scan_area = { { pos.x - 4, pos.y - 4 }, { pos.x + 15, pos.y + 4 } } else --message = " Rail object scan error " .. heading .. " " - scan_area = {{pos.x+4,pos.y+4},{pos.x+4,pos.y+4}} + scan_area = { { pos.x + 4, pos.y + 4 }, { pos.x + 4, pos.y + 4 } } end - local ents = game.get_player(pindex).surface.find_entities_filtered{area = scan_area, name = "train-stop"} - for i,passed_stop in ipairs(ents) do - distance = util.distance(leading_stock.position, passed_stop.position) - 0 - --message = message .. " found stop " - if distance < 12.5 and fa_utils.direction_lookup(passed_stop.direction) == fa_utils.get_heading_info(leading_stock) then + local ents = game.get_player(pindex).surface.find_entities_filtered({ area = scan_area, name = "train-stop" }) + for i, passed_stop in ipairs(ents) do + distance = util.distance(leading_stock.position, passed_stop.position) - 0 + --message = message .. " found stop " + if + distance < 12.5 + and fa_utils.direction_lookup(passed_stop.direction) == fa_utils.get_heading_info(leading_stock) + then if not first_reset then message = "" first_reset = true end - message = message .. math.floor(distance+0.5) .. " meters past train stop " .. passed_stop.backer_name .. ", " + message = message + .. math.floor(distance + 0.5) + .. " meters past train stop " + .. passed_stop.backer_name + .. ", " end end - if first_reset then - message = message .. " for the front vehicle. " - end + if first_reset then message = message .. " for the front vehicle. " end end if not mute_in == true then - printout(message,pindex) + printout(message, pindex) --Draw circles for visual debugging - rendering.draw_circle{color = {0, 0.5, 1},radius = 1,width = 8,target = next_entity,surface = next_entity.surface,time_to_live = 100} + rendering.draw_circle({ + color = { 0, 0.5, 1 }, + radius = 1, + width = 8, + target = next_entity, + surface = next_entity.surface, + time_to_live = 100, + }) end - + if honk_score > 1 then - rendering.draw_circle{color = {1, 0, 0},radius = 1,width = 4,target = next_entity,surface = next_entity.surface,time_to_live = 60} + rendering.draw_circle({ + color = { 1, 0, 0 }, + radius = 1, + width = 4, + target = next_entity, + surface = next_entity.surface, + time_to_live = 60, + }) end return honk_score end @@ -411,11 +429,17 @@ function mod.run_train_menu(menu_index, pindex, clicked, other_input) return end local train = locomotive.train - + if index == 0 then --Give basic info about this train, such as its name and ID. Instructions. - printout("Train ".. mod.get_train_name(train) .. ", with ID " .. train.id - .. ", Press UP ARROW and DOWN ARROW to navigate options, press LEFT BRACKET to select an option or press E to exit this menu.", pindex) + printout( + "Train " + .. mod.get_train_name(train) + .. ", with ID " + .. train.id + .. ", Press UP ARROW and DOWN ARROW to navigate options, press LEFT BRACKET to select an option or press E to exit this menu.", + pindex + ) elseif index == 1 then --Get train state and toggle manual control if not clicked then @@ -444,48 +468,55 @@ function mod.run_train_menu(menu_index, pindex, clicked, other_input) end printout("Enter a new name for this train, then press 'ENTER' to confirm, or press 'ESC' to cancel.", pindex) players[pindex].train_menu.renaming = true - local frame = game.get_player(pindex).gui.screen.add{type = "frame", name = "train-rename"} + local frame = game.get_player(pindex).gui.screen.add({ type = "frame", name = "train-rename" }) frame.bring_to_front() frame.force_auto_center() frame.focus() game.get_player(pindex).opened = frame - local input = frame.add{type="textfield", name = "input"} + local input = frame.add({ type = "textfield", name = "input" }) input.focus() end elseif index == 3 then --Train vehicles info local locos = train.locomotives - printout("Vehicle counts, " .. #locos["front_movers"] .. " locomotives facing front, " - .. #locos["back_movers"] .. " locomotives facing back, " .. #train.cargo_wagons .. " cargo wagons, " - .. #train.fluid_wagons .. " fluid wagons, ", pindex) - elseif index == 4 then - --Train cargo info + printout( + "Vehicle counts, " + .. #locos["front_movers"] + .. " locomotives facing front, " + .. #locos["back_movers"] + .. " locomotives facing back, " + .. #train.cargo_wagons + .. " cargo wagons, " + .. #train.fluid_wagons + .. " fluid wagons, ", + pindex + ) + elseif index == 4 then + --Train cargo info printout("Cargo, " .. mod.train_top_contents_info(train) .. " ", pindex) - elseif index == 5 then + elseif index == 5 then --Train schedule info local result = "" local namelist = "" local schedule = train.schedule local records = {} - if schedule ~= nil then - records = schedule.records - end + if schedule ~= nil then records = schedule.records end if schedule == nil or records == nil or #records == 0 then result = " No schedule, " else - for i,record in ipairs(records) do + for i, record in ipairs(records) do if record.station ~= nil then if record.temporary == false or record.temporary == nil then - namelist = namelist .. ", station " .. record.station + namelist = namelist .. ", station " .. record.station else - namelist = namelist .. ", temporary station " .. record.station + namelist = namelist .. ", temporary station " .. record.station end local wait_cond_1 = record.wait_conditions[1] if wait_cond_1 ~= nil then local cond = wait_cond_1.type namelist = namelist .. ", waiting for " .. cond if cond == "time" or cond == "inactivity" then - namelist = namelist .. " " .. math.ceil(wait_cond_1.ticks/60) .. " seconds " + namelist = namelist .. " " .. math.ceil(wait_cond_1.ticks / 60) .. " seconds " end end local wait_cond_2 = record.wait_conditions[2] @@ -493,48 +524,50 @@ function mod.run_train_menu(menu_index, pindex, clicked, other_input) local cond = wait_cond_2.type namelist = namelist .. ", and waiting for " .. cond if cond == "time" or cond == "inactivity" then - namelist = namelist .. " " .. math.ceil(wait_cond_2.ticks/60) .. " seconds " - end + namelist = namelist .. " " .. math.ceil(wait_cond_2.ticks / 60) .. " seconds " + end end namelist = namelist .. ", " end end - if namelist == "" then - namelist = " is empty" - end + if namelist == "" then namelist = " is empty" end result = " Train schedule" .. namelist end - printout(result,pindex) - elseif index == 6 then - --Set instant schedule - if players[pindex].train_menu.wait_time == nil then - players[pindex].train_menu.wait_time = 300 - end - if not clicked then - printout(" Set a new instant schedule for the train here by pressing LEFT BRACKET, where the train waits for a set amount of time at immediately reachable station, modify this time with PAGE UP or PAGE DOWN before settting the schedule and hold CONTROL to increase the step size", pindex) + printout(result, pindex) + elseif index == 6 then + --Set instant schedule + if players[pindex].train_menu.wait_time == nil then players[pindex].train_menu.wait_time = 300 end + if not clicked then + printout( + " Set a new instant schedule for the train here by pressing LEFT BRACKET, where the train waits for a set amount of time at immediately reachable station, modify this time with PAGE UP or PAGE DOWN before settting the schedule and hold CONTROL to increase the step size", + pindex + ) else - local comment = mod.instant_schedule(train,players[pindex].train_menu.wait_time) - printout(comment,pindex) + local comment = mod.instant_schedule(train, players[pindex].train_menu.wait_time) + printout(comment, pindex) end - elseif index == 7 then - --Clear schedule + elseif index == 7 then + --Clear schedule if not clicked then printout("Clear the schedule here by pressing LEFT BRACKET ", pindex) else train.schedule = nil train.manual_mode = true - printout("Train schedule cleared.",pindex) + printout("Train schedule cleared.", pindex) end - elseif index == 8 then + elseif index == 8 then if not players[pindex].train_menu.selecting_station then --Subautomatic travel to a selected train stop if not clicked then - printout("Single-time travel to a reachable train stop, press LEFT BRACKET to select one, the train waits there until all passengers get off, then it resumes its original schedule.", pindex) + printout( + "Single-time travel to a reachable train stop, press LEFT BRACKET to select one, the train waits there until all passengers get off, then it resumes its original schedule.", + pindex + ) else local comment = "Select a station with LEFT and RIGHT arrow keys and confirm with LEFT BRACKET." - printout(comment,pindex) + printout(comment, pindex) players[pindex].train_menu.selecting_station = true - mod.refresh_valid_train_stop_list(train,pindex) + mod.refresh_valid_train_stop_list(train, pindex) train.manual_mode = true end else @@ -544,7 +577,7 @@ function mod.run_train_menu(menu_index, pindex, clicked, other_input) mod.read_valid_train_stop_from_list(pindex) else --Go to the list item - mod.go_to_valid_train_stop_from_list(pindex,train) + mod.go_to_valid_train_stop_from_list(pindex, train) players[pindex].train_menu.selecting_station = false end end @@ -554,21 +587,19 @@ TRAIN_MENU_LENGTH = 8 --Loads and opens the train menu function mod.menu_open(pindex) - if players[pindex].vanilla_mode then - return - end + if players[pindex].vanilla_mode then return end --Set the player menu tracker to this menu players[pindex].menu = "train_menu" players[pindex].in_menu = true players[pindex].move_queue = {} - + --Set the menu line counter to 0 players[pindex].train_menu.index = 0 - + --Play sound - game.get_player(pindex).play_sound{path = "Open-Inventory-Sound"} - - --Load menu + game.get_player(pindex).play_sound({ path = "Open-Inventory-Sound" }) + + --Load menu mod.run_train_menu(players[pindex].train_menu.index, pindex, false) end @@ -583,29 +614,25 @@ function mod.menu_close(pindex, mute_in) players[pindex].train_menu.index = 0 players[pindex].train_menu.index_2 = 0 players[pindex].train_menu.selecting_station = false - + --play sound - if not mute then - game.get_player(pindex).play_sound{path="Close-Inventory-Sound"} - end - + if not mute then game.get_player(pindex).play_sound({ path = "Close-Inventory-Sound" }) end + --Destroy GUI if game.get_player(pindex).gui.screen["train-rename"] ~= nil then game.get_player(pindex).gui.screen["train-rename"].destroy() end - if game.get_player(pindex).opened ~= nil then - game.get_player(pindex).opened = nil - end + if game.get_player(pindex).opened ~= nil then game.get_player(pindex).opened = nil end end function mod.menu_up(pindex) players[pindex].train_menu.index = players[pindex].train_menu.index - 1 if players[pindex].train_menu.index < 0 then players[pindex].train_menu.index = 0 - game.get_player(pindex).play_sound{path = "inventory-edge"} + game.get_player(pindex).play_sound({ path = "inventory-edge" }) else --Play sound - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) end --Load menu mod.run_train_menu(players[pindex].train_menu.index, pindex, false) @@ -615,10 +642,10 @@ function mod.menu_down(pindex) players[pindex].train_menu.index = players[pindex].train_menu.index + 1 if players[pindex].train_menu.index > TRAIN_MENU_LENGTH then players[pindex].train_menu.index = TRAIN_MENU_LENGTH - game.get_player(pindex).play_sound{path = "inventory-edge"} + game.get_player(pindex).play_sound({ path = "inventory-edge" }) else --Play sound - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) end --Load menu mod.run_train_menu(players[pindex].train_menu.index, pindex, false) @@ -628,12 +655,12 @@ function mod.menu_left(pindex) local index = players[pindex].train_menu.index_2 if index == nil then index = 1 - else + else index = index - 1 end if index == 0 then index = 1 - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) end players[pindex].train_menu.index_2 = index --Load menu @@ -644,12 +671,12 @@ function mod.menu_right(pindex) local index = players[pindex].train_menu.index_2 if index == nil then index = 1 - else + else index = index + 1 end if index > #players[pindex].valid_train_stop_list then index = #players[pindex].valid_train_stop_list - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) end players[pindex].train_menu.index_2 = index --Load menu @@ -662,7 +689,7 @@ function mod.cargo_wagon_top_contents_info(wagon) local itemset = wagon.get_inventory(defines.inventory.cargo_wagon).get_contents() local itemtable = {} for name, count in pairs(itemset) do - table.insert(itemtable, {name = name, count = count}) + table.insert(itemtable, { name = name, count = count }) end table.sort(itemtable, function(k1, k2) return k1.count > k2.count @@ -683,9 +710,7 @@ function mod.cargo_wagon_top_contents_info(wagon) if #itemtable > 4 then result = result .. " and " .. itemtable[5].name .. " times " .. itemtable[5].count .. ", " end - if #itemtable > 5 then - result = result .. " and other items " - end + if #itemtable > 5 then result = result .. " and other items " end end result = result .. ", Use inserters or cursor shortcuts to fill and empty this wagon. " return result @@ -697,7 +722,7 @@ function mod.fluid_contents_info(wagon) local itemset = wagon.get_fluid_contents() local itemtable = {} for name, amount in pairs(itemset) do - table.insert(itemtable, {name = name, amount = amount}) + table.insert(itemtable, { name = name, amount = amount }) end table.sort(itemtable, function(k1, k2) return k1.amount > k2.amount @@ -705,16 +730,29 @@ function mod.fluid_contents_info(wagon) if #itemtable == 0 then result = result .. " Contains no fluids. " else - result = result .. " Contains " .. itemtable[1].name .. " times " .. string.format(" %.0f ", itemtable[1].amount) .. ", " - if #itemtable > 1 then - result = result .. " and " .. itemtable[2].name .. " times " .. string.format(" %.0f ", itemtable[2].amount) .. ", " - end - if #itemtable > 2 then - result = result .. " and " .. itemtable[3].name .. " times " .. string.format(" %.0f ", itemtable[3].amount) .. ", " + result = result + .. " Contains " + .. itemtable[1].name + .. " times " + .. string.format(" %.0f ", itemtable[1].amount) + .. ", " + if #itemtable > 1 then + result = result + .. " and " + .. itemtable[2].name + .. " times " + .. string.format(" %.0f ", itemtable[2].amount) + .. ", " end - if #itemtable > 3 then - result = result .. " and other fluids " + if #itemtable > 2 then + result = result + .. " and " + .. itemtable[3].name + .. " times " + .. string.format(" %.0f ", itemtable[3].amount) + .. ", " end + if #itemtable > 3 then result = result .. " and other fluids " end end if wagon.object_name ~= "LuaTrain" and wagon.name == "fluid-wagon" then result = result .. ", Use pumps to fill and empty this wagon. " @@ -728,7 +766,7 @@ function mod.train_top_contents_info(train) local itemset = train.get_contents() local itemtable = {} for name, count in pairs(itemset) do - table.insert(itemtable, {name = name, count = count}) + table.insert(itemtable, { name = name, count = count }) end table.sort(itemtable, function(k1, k2) return k1.count > k2.count @@ -743,98 +781,96 @@ function mod.train_top_contents_info(train) if #itemtable > 2 then result = result .. " and " .. itemtable[3].name .. " times " .. itemtable[3].count .. ", " end - if #itemtable > 3 then - result = result .. " and other items, " - end + if #itemtable > 3 then result = result .. " and other items, " end end result = result .. mod.fluid_contents_info(train) return result end --For the selected train, adds every reachable train stop to its schedule with the waiting condition of 5 minutes. -function mod.instant_schedule(train,seconds_in) +function mod.instant_schedule(train, seconds_in) local seconds = seconds_in or 300 local surf = train.front_stock.surface local train_stops = surf.get_train_stops() local valid_stops = 0 train.schedule = nil - for i,stop in ipairs(train_stops) do + for i, stop in ipairs(train_stops) do --Add the stop to the schedule's first row - local wait_condition_1 = {type = "time" , ticks = seconds * 60 , compare_type = "and"} - local new_record = {wait_conditions = {wait_condition_1}, station = stop.backer_name, temporary = false} - - local schedule = train.schedule - if schedule == nil then - schedule = {current = 1, records = {new_record}} - --game.get_player(pindex).print("made new schedule") - else - local records = schedule.records - table.insert(records,1, new_record) - --game.get_player(pindex).print("added to schedule row 1, schedule length now " .. #records) - end - train.schedule = schedule - - --Make the train aim for the stop - train.go_to_station(1) - train.recalculate_path() - - --React according to valid path - if not train.has_path then - --Clear the invalid schedule record - --game.get_player(pindex).print("invalid " .. stop.backer_name) - local schedule = train.schedule - if schedule ~= nil then - --game.get_player(pindex).print("Removing " .. stop.backer_name) - local records = schedule.records - table.remove(records, 1) - if records == nil or #records == 0 then - train.schedule = nil - train.manual_mode = true - else - train.schedule = schedule - end - --game.get_player(pindex).print("schedule length now " .. #records) - end - else - --Valid station and path selected. - valid_stops = valid_stops + 1 - --game.get_player(pindex).print("valid " .. stop.backer_name .. ", path size " .. train.path.size) - end + local wait_condition_1 = { type = "time", ticks = seconds * 60, compare_type = "and" } + local new_record = { wait_conditions = { wait_condition_1 }, station = stop.backer_name, temporary = false } + + local schedule = train.schedule + if schedule == nil then + schedule = { current = 1, records = { new_record } } + --game.get_player(pindex).print("made new schedule") + else + local records = schedule.records + table.insert(records, 1, new_record) + --game.get_player(pindex).print("added to schedule row 1, schedule length now " .. #records) + end + train.schedule = schedule + + --Make the train aim for the stop + train.go_to_station(1) + train.recalculate_path() + + --React according to valid path + if not train.has_path then + --Clear the invalid schedule record + --game.get_player(pindex).print("invalid " .. stop.backer_name) + local schedule = train.schedule + if schedule ~= nil then + --game.get_player(pindex).print("Removing " .. stop.backer_name) + local records = schedule.records + table.remove(records, 1) + if records == nil or #records == 0 then + train.schedule = nil + train.manual_mode = true + else + train.schedule = schedule + end + --game.get_player(pindex).print("schedule length now " .. #records) + end + else + --Valid station and path selected. + valid_stops = valid_stops + 1 + --game.get_player(pindex).print("valid " .. stop.backer_name .. ", path size " .. train.path.size) + end end if valid_stops == 0 then --Announce error to all passengers - str = " Error: No reachable trainstops detected. Check whether you have locomotives facing both directions as required." - for i,player in ipairs(train.passengers) do + str = + " Error: No reachable trainstops detected. Check whether you have locomotives facing both directions as required." + for i, player in ipairs(train.passengers) do players[player.index].last = str - localised_print{"","out ",str} - end + localised_print({ "", "out ", str }) + end elseif valid_stops == 1 then --Announce error to all passengers - str = " Error: Only one reachable trainstop detected. Check whether you have locomotives facing both directions as required." - for i,player in ipairs(train.passengers) do + str = + " Error: Only one reachable trainstop detected. Check whether you have locomotives facing both directions as required." + for i, player in ipairs(train.passengers) do players[player.index].last = str - localised_print{"","out ",str} - end - train.schedule = nil + localised_print({ "", "out ", str }) + end + train.schedule = nil else if seconds_in == nil then str = "Train schedule created with " .. valid_stops .. " stops, waiting " .. seconds .. " seconds at each. " else str = seconds .. " seconds waited at each of " .. valid_stops .. " stops. " end - for i,player in ipairs(train.passengers) do + for i, player in ipairs(train.passengers) do players[player.index].last = str - localised_print{"","out ",str} + localised_print({ "", "out ", str }) end end return str end -function mod.change_instant_schedule_wait_time(increment,pindex) +function mod.change_instant_schedule_wait_time(increment, pindex) local seconds = players[pindex].train_menu.wait_time - if seconds == nil then - seconds = 300 - end + if seconds == nil then seconds = 300 end seconds = seconds + increment if seconds < 5 then seconds = 5 @@ -842,7 +878,11 @@ function mod.change_instant_schedule_wait_time(increment,pindex) seconds = 10000 end players[pindex].train_menu.wait_time = seconds - printout(players[pindex].train_menu.wait_time .. " seconds waited at each station. Use arrow keys to navigate the train menu and apply the new wait time by re-creating the schedule.",pindex) + printout( + players[pindex].train_menu.wait_time + .. " seconds waited at each station. Use arrow keys to navigate the train menu and apply the new wait time by re-creating the schedule.", + pindex + ) end --Subautomatic one-time travel to a reachable train stop that is at least 3 rails away. Does not delete the train schedule. Note: Now obsolete? @@ -850,88 +890,89 @@ function mod.sub_automatic_travel_to_other_stop(train) local surf = train.front_stock.surface local train_stops = surf.get_train_stops() local str = "" - for i,stop in ipairs(train_stops) do + for i, stop in ipairs(train_stops) do --Set a stop - local wait_condition_1 = {type = "passenger_not_present", compare_type = "and"} - local wait_condition_2 = {type = "time", ticks = 60, compare_type = "and"} - local new_record = {wait_conditions = {wait_condition_1,wait_condition_2}, station = stop.backer_name, temporary = true} - - --train.schedule = {current = 1, records = {new_record}} - local schedule = train.schedule - if schedule == nil then - schedule = {current = 1, records = {new_record}} - --game.get_player(pindex).print("made new schedule") - else - local records = schedule.records - table.insert(records,1, new_record) - end - train.schedule = schedule - - --Make the train aim for the stop - train.go_to_station(1) - if not train.has_path or train.path.size < 3 then - --Invalid path or path to an station nearby - local records = schedule.records - table.remove(records, 1) - if records == nil or #records == 0 then - train.schedule = nil - train.manual_mode = true - else - train.schedule = schedule - end - else - --Valid station and path selected. - --(do nothing) - end - + local wait_condition_1 = { type = "passenger_not_present", compare_type = "and" } + local wait_condition_2 = { type = "time", ticks = 60, compare_type = "and" } + local new_record = + { wait_conditions = { wait_condition_1, wait_condition_2 }, station = stop.backer_name, temporary = true } + + --train.schedule = {current = 1, records = {new_record}} + local schedule = train.schedule + if schedule == nil then + schedule = { current = 1, records = { new_record } } + --game.get_player(pindex).print("made new schedule") + else + local records = schedule.records + table.insert(records, 1, new_record) + end + train.schedule = schedule + + --Make the train aim for the stop + train.go_to_station(1) + if not train.has_path or train.path.size < 3 then + --Invalid path or path to an station nearby + local records = schedule.records + table.remove(records, 1) + if records == nil or #records == 0 then + train.schedule = nil + train.manual_mode = true + else + train.schedule = schedule + end + else + --Valid station and path selected. + --(do nothing) + end end - + if train.path_end_stop == nil then --Announce error to all passengers - str = " No reachable trainstops detected. Check whether you have locomotives facing both directions as required." - for i,player in ipairs(train.passengers) do - players[player.index].last = str - localised_print{"","out ",str} - end + str = " No reachable trainstops detected. Check whether you have locomotives facing both directions as required." + for i, player in ipairs(train.passengers) do + players[player.index].last = str + localised_print({ "", "out ", str }) + end else str = "Path set." end return str end -function mod.refresh_valid_train_stop_list(train,pindex)--table.insert +function mod.refresh_valid_train_stop_list(train, pindex) --table.insert players[pindex].valid_train_stop_list = {} train.manual_mode = true local surf = train.front_stock.surface local train_stops = surf.get_train_stops() local str = "" - for i,stop in ipairs(train_stops) do + for i, stop in ipairs(train_stops) do --Set a stop - local wait_condition_1 = {type = "passenger_not_present", compare_type = "and"} - local wait_condition_2 = {type = "time", ticks = 60, compare_type = "and"} - local new_record = {wait_conditions = {wait_condition_1,wait_condition_2}, station = stop.backer_name, temporary = true} - - local schedule = train.schedule - if schedule == nil then - schedule = {current = 1, records = {new_record}} - --game.get_player(pindex).print("made new schedule") - else - local records = schedule.records - table.insert(records,1, new_record) - end - train.schedule = schedule - - --Make the train aim for the stop - train.go_to_station(1) - if not train.has_path then - --Invalid path: Do not add to list - else - --Valid station and path selected. - table.insert(players[pindex].valid_train_stop_list, stop.backer_name) - end - - --Clear the record - local records = schedule.records + local wait_condition_1 = { type = "passenger_not_present", compare_type = "and" } + local wait_condition_2 = { type = "time", ticks = 60, compare_type = "and" } + local new_record = + { wait_conditions = { wait_condition_1, wait_condition_2 }, station = stop.backer_name, temporary = true } + + local schedule = train.schedule + if schedule == nil then + schedule = { current = 1, records = { new_record } } + --game.get_player(pindex).print("made new schedule") + else + local records = schedule.records + table.insert(records, 1, new_record) + end + train.schedule = schedule + + --Make the train aim for the stop + train.go_to_station(1) + if not train.has_path then + --Invalid path: Do not add to list + else + --Valid station and path selected. + table.insert(players[pindex].valid_train_stop_list, stop.backer_name) + end + + --Clear the record + local records = schedule.records table.remove(records, 1) if records == nil or #records == 0 then train.schedule = nil @@ -948,52 +989,48 @@ function mod.read_valid_train_stop_from_list(pindex) local index = players[pindex].train_menu.index_2 local name = "" if players[pindex].valid_train_stop_list == nil or #players[pindex].valid_train_stop_list == 0 then - printout("Error: No reachable train stops found",pindex) + printout("Error: No reachable train stops found", pindex) return end - if index == nil then - index = 1 - end + if index == nil then index = 1 end players[pindex].train_menu.index_2 = index - + name = players[pindex].valid_train_stop_list[index] --Return the name - printout(name,pindex) + printout(name, pindex) end -function mod.go_to_valid_train_stop_from_list(pindex,train) +function mod.go_to_valid_train_stop_from_list(pindex, train) local index = players[pindex].train_menu.index_2 local name = "" if players[pindex].valid_train_stop_list == nil or #players[pindex].valid_train_stop_list == 0 then - printout("Error: No reachable train stops found",pindex) + printout("Error: No reachable train stops found", pindex) return end - if index == nil then - index = 1 - end + if index == nil then index = 1 end players[pindex].train_menu.index_2 = index name = players[pindex].valid_train_stop_list[index] - + --Set the station target - local wait_condition_1 = {type = "passenger_not_present", compare_type = "and"} - local wait_condition_2 = {type = "time", ticks = 60, compare_type = "and"} - local new_record = {wait_conditions = {wait_condition_1,wait_condition_2}, station = name, temporary = true} + local wait_condition_1 = { type = "passenger_not_present", compare_type = "and" } + local wait_condition_2 = { type = "time", ticks = 60, compare_type = "and" } + local new_record = { wait_conditions = { wait_condition_1, wait_condition_2 }, station = name, temporary = true } local schedule = train.schedule if schedule == nil then - schedule = {current = 1, records = {new_record}} - --game.get_player(pindex).print("made new schedule") + schedule = { current = 1, records = { new_record } } + --game.get_player(pindex).print("made new schedule") else - local records = schedule.records - table.insert(records,1, new_record) + local records = schedule.records + table.insert(records, 1, new_record) end train.schedule = schedule --Make the train aim for the stop train.go_to_station(1) if not train.has_path or train.path.size < 3 then - --Invalid path or path to an station nearby - local records = schedule.records + --Invalid path or path to an station nearby + local records = schedule.records table.remove(records, 1) if records == nil or #records == 0 then train.schedule = nil @@ -1002,39 +1039,33 @@ function mod.go_to_valid_train_stop_from_list(pindex,train) train.schedule = schedule end else - --Valid station and path selected. - --(do nothing) + --Valid station and path selected. + --(do nothing) end - + --Check valid path again local str = "" if train.path_end_stop == nil then --Announce error to all passengers str = "Error: Train stop pathing error." - for i,player in ipairs(train.passengers) do + for i, player in ipairs(train.passengers) do players[player.index].last = str - localised_print{"","out ",str} + localised_print({ "", "out ", str }) end else --Train will announce its new path by itself end - + mod.menu_close(pindex, false) end --Honks if the following conditions are met: 1. The player is manually driving a train, 2. The train is moving, 3. Ahead of the train is a closed rail signal or rail chain signal, 4. It has been 5 seconds since the last honk. -function mod.check_and_honk_at_closed_signal(tick,pindex) - if not check_for_player(pindex) then - return - end +function mod.check_and_honk_at_closed_signal(tick, pindex) + if not check_for_player(pindex) then return end --0. Check if it has been 5 seconds since the last honk - if players[pindex].last_honk_tick == nil then - players[pindex].last_honk_tick = 1 - end - if tick - players[pindex].last_honk_tick < 300 then - return - end - --1. Check if the player is on a train + if players[pindex].last_honk_tick == nil then players[pindex].last_honk_tick = 1 end + if tick - players[pindex].last_honk_tick < 300 then return end + --1. Check if the player is on a train local p = game.get_player(pindex) local train = nil if p.vehicle == nil or p.vehicle.train == nil then @@ -1043,32 +1074,22 @@ function mod.check_and_honk_at_closed_signal(tick,pindex) train = p.vehicle.train end --2. Check if the train is manually driving and has nonzero speed - if train.speed == 0 or not train.manual_mode then - return - end + if train.speed == 0 or not train.manual_mode then return end --3. Check if ahead of the train is a closed rail signal or rail chain signal local honk_score = mod.train_read_next_rail_entity_ahead(pindex, false, true) - if honk_score < 2 then - return - end + if honk_score < 2 then return end --4. HONK (short) - game.get_player(pindex).play_sound{path="train-honk-short"} + game.get_player(pindex).play_sound({ path = "train-honk-short" }) players[pindex].last_honk_tick = tick end --Honks if the following conditions are met: 1. The player is on a train, 2. The train is moving, 3. There is another train within the same rail block, 4. It has been 5 seconds since the last honk. -function mod.check_and_honk_at_trains_in_same_block(tick,pindex) - if not check_for_player(pindex) then - return - end +function mod.check_and_honk_at_trains_in_same_block(tick, pindex) + if not check_for_player(pindex) then return end --0. Check if it has been 5 seconds since the last honk - if players[pindex].last_honk_tick == nil then - players[pindex].last_honk_tick = 1 - end - if tick - players[pindex].last_honk_tick < 300 then - return - end - --1. Check if the player is on a train + if players[pindex].last_honk_tick == nil then players[pindex].last_honk_tick = 1 end + if tick - players[pindex].last_honk_tick < 300 then return end + --1. Check if the player is on a train local p = game.get_player(pindex) local train = nil if p.vehicle == nil or p.vehicle.train == nil then @@ -1077,30 +1098,24 @@ function mod.check_and_honk_at_trains_in_same_block(tick,pindex) train = p.vehicle.train end --2. Check if the train has nonzero speed - if train.speed == 0 then - return - end + if train.speed == 0 then return end --3. Check if there is another train within the same rail block (for both the front rail and the back rail) if train.front_rail == nil or not train.front_rail.valid or train.back_rail == nil or not train.back_rail.valid then return end - if train.front_rail.trains_in_block < 2 and train.back_rail.trains_in_block < 2 then - return - end + if train.front_rail.trains_in_block < 2 and train.back_rail.trains_in_block < 2 then return end --4. HONK (long) - game.get_player(pindex).play_sound{path="train-honk-long"} + game.get_player(pindex).play_sound({ path = "train-honk-long" }) players[pindex].last_honk_tick = tick end --Play a sound to indicate the train is turning function mod.check_and_play_sound_for_turning_trains(pindex) local p = game.get_player(pindex) - if p.vehicle == nil or p.vehicle.valid == false or p.vehicle.train == nil then - return - end + if p.vehicle == nil or p.vehicle.valid == false or p.vehicle.train == nil then return end local ori = p.vehicle.orientation if players[pindex].last_train_orientation ~= nil and players[pindex].last_train_orientation ~= ori then - p.play_sound{path = "train-clack"} + p.play_sound({ path = "train-clack" }) end players[pindex].last_train_orientation = ori end diff --git a/scripts/transport-belts.lua b/scripts/transport-belts.lua index 7b9632a4..085364db 100644 --- a/scripts/transport-belts.lua +++ b/scripts/transport-belts.lua @@ -1,17 +1,25 @@ --Here: functions about belts, splitters, underground belts local localising = require("scripts.localising") -local util = require('util') -local fa_utils = require('scripts.fa-utils') +local util = require("util") +local fa_utils = require("scripts.fa-utils") local mod = {} --Takes some stats about a belt unit and explains what type of junction the belt is. -function mod.transport_belt_junction_info(sideload_count, backload_count, outload_count, this_dir, outload_dir, say_middle, outload_is_corner) +function mod.transport_belt_junction_info( + sideload_count, + backload_count, + outload_count, + this_dir, + outload_dir, + say_middle, + outload_is_corner +) local say_middle = say_middle or false local outload_is_corner = outload_is_corner or false local result = "" - if sideload_count == 0 and backload_count == 0 and outload_count == 0 then + if sideload_count == 0 and backload_count == 0 and outload_count == 0 then result = " unit " elseif sideload_count == 0 and backload_count == 1 and outload_count == 0 then result = " stopping end " @@ -79,8 +87,10 @@ function mod.transport_belt_junction_info(sideload_count, backload_count, outloa else result = " safe merging junction " end - elseif sideload_count + backload_count > 1 and (outload_count == 0 or (outload_count == 1 and this_dir == outload_dir)) then - result = " unidentified junction "--this should not be reachable any more + elseif + sideload_count + backload_count > 1 and (outload_count == 0 or (outload_count == 1 and this_dir == outload_dir)) + then + result = " unidentified junction " --this should not be reachable any more elseif sideload_count + backload_count > 1 and outload_count == 1 and this_dir ~= outload_dir then result = " unidentified pouring end " elseif outload_count > 1 then @@ -94,7 +104,11 @@ end --Belt analyzer: Returns a navigable list of items that are found in the input transport belt line. function mod.get_line_items(network) - local result = {combined = {left = {}, right = {}}, downstream = {left = {}, right = {}}, upstream = {left = {}, right = {}}} + local result = { + combined = { left = {}, right = {} }, + downstream = { left = {}, right = {} }, + upstream = { left = {}, right = {} }, + } local dict = {} for i, line in pairs(network.downstream.left) do for name, count in pairs(line.get_contents()) do @@ -107,7 +121,13 @@ function mod.get_line_items(network) end local total = table_size(network.downstream.left) * 4 for name, count in pairs(dict) do - table.insert(result.downstream.left, {name = name, count = count, percent = math.floor(1000* count/total)/10, valid = true, valid_for_read = true}) + table.insert(result.downstream.left, { + name = name, + count = count, + percent = math.floor(1000 * count / total) / 10, + valid = true, + valid_for_read = true, + }) end table.sort(result.downstream.left, function(k1, k2) return k1.percent > k2.percent @@ -125,7 +145,13 @@ function mod.get_line_items(network) end local total = table_size(network.downstream.right) * 4 for name, count in pairs(dict) do - table.insert(result.downstream.right, {name = name, count = count, percent = math.floor(1000* count/total)/10, valid = true, valid_for_read = true}) + table.insert(result.downstream.right, { + name = name, + count = count, + percent = math.floor(1000 * count / total) / 10, + valid = true, + valid_for_read = true, + }) end table.sort(result.downstream.right, function(k1, k2) return k1.percent > k2.percent @@ -143,7 +169,13 @@ function mod.get_line_items(network) end local total = table_size(network.upstream.left) * 4 for name, count in pairs(dict) do - table.insert(result.upstream.left, {name = name, count = count, percent = math.floor(1000* count/total)/10, valid = true, valid_for_read = true}) + table.insert(result.upstream.left, { + name = name, + count = count, + percent = math.floor(1000 * count / total) / 10, + valid = true, + valid_for_read = true, + }) end table.sort(result.upstream.left, function(k1, k2) return k1.percent > k2.percent @@ -161,14 +193,20 @@ function mod.get_line_items(network) end local total = table_size(network.upstream.right) * 4 for name, count in pairs(dict) do - table.insert(result.upstream.right, {name = name, count = count, percent = math.floor(1000* count/total)/10, valid = true, valid_for_read = true}) + table.insert(result.upstream.right, { + name = name, + count = count, + percent = math.floor(1000 * count / total) / 10, + valid = true, + valid_for_read = true, + }) end table.sort(result.upstream.right, function(k1, k2) return k1.percent > k2.percent end) local dict = {} for i, item in pairs(result.downstream.left) do - dict[item.name] = item.count + dict[item.name] = item.count end for i, item in pairs(result.upstream.left) do if dict[item.name] == nil then @@ -181,7 +219,13 @@ function mod.get_line_items(network) local total = table_size(network.combined.left) * 4 for name, count in pairs(dict) do - table.insert(result.combined.left, {name = name, count = count, percent = math.floor(1000 * count/total) / 10, valid = true, valid_for_read = true}) + table.insert(result.combined.left, { + name = name, + count = count, + percent = math.floor(1000 * count / total) / 10, + valid = true, + valid_for_read = true, + }) end table.sort(result.combined.left, function(k1, k2) return k1.percent > k2.percent @@ -189,7 +233,7 @@ function mod.get_line_items(network) local dict = {} for i, item in pairs(result.downstream.right) do - dict[item.name] = item.count + dict[item.name] = item.count end for i, item in pairs(result.upstream.right) do if dict[item.name] == nil then @@ -202,14 +246,19 @@ function mod.get_line_items(network) local total = table_size(network.combined.right) * 4 for name, count in pairs(dict) do - table.insert(result.combined.right, {name = name, count = count, percent = math.floor(1000 * count/total) / 10, valid = true, valid_for_read = true}) + table.insert(result.combined.right, { + name = name, + count = count, + percent = math.floor(1000 * count / total) / 10, + valid = true, + valid_for_read = true, + }) end table.sort(result.combined.right, function(k1, k2) return k1.percent > k2.percent end) return result - end --Belt analyzer: Creates a list of transport lines that involve the belt uint B. @@ -227,27 +276,27 @@ function mod.get_connected_lines(B) if belt.name ~= "entity-ghost" then if hash[belt.unit_number] ~= true then hash[belt.unit_number] = true - table.insert(frontier, {side = 1, belt = belt}) + table.insert(frontier, { side = 1, belt = belt }) end end end - for i, belt in pairs(inputs) do - if belt.name ~= "entity-ghost" then - if hash[belt.unit_number] ~= true then - local side = 1 - if #inputs == 1 then - side = 1 - elseif belt.direction == (B.direction + 2) % 8 then - side = 0 - elseif belt.direction == (B.direction + 6) % 8 then - side = 2 - end - - table.insert(precursors, {side = side, belt = belt}) + for i, belt in pairs(inputs) do + if belt.name ~= "entity-ghost" then + if hash[belt.unit_number] ~= true then + local side = 1 + if #inputs == 1 then + side = 1 + elseif belt.direction == (B.direction + 2) % 8 then + side = 0 + elseif belt.direction == (B.direction + 6) % 8 then + side = 2 end + + table.insert(precursors, { side = side, belt = belt }) end end + end table.insert(left, B.get_transport_line(1)) table.insert(right, B.get_transport_line(2)) @@ -261,7 +310,7 @@ function mod.get_connected_lines(B) if hash[belt.unit_number] ~= true then hash[belt.unit_number] = true - table.insert(frontier, {side = 1, belt = belt}) + table.insert(frontier, { side = 1, belt = belt }) end end end @@ -280,17 +329,17 @@ function mod.get_connected_lines(B) side = 2 end - table.insert(upstreams, {side = side, belt = belt}) + table.insert(upstreams, { side = side, belt = belt }) end end end -if explored.side == 0 then + if explored.side == 0 then table.insert(left, explored.belt.get_transport_line(1)) table.insert(left, explored.belt.get_transport_line(2)) elseif explored.side == 2 then table.insert(right, explored.belt.get_transport_line(1)) table.insert(right, explored.belt.get_transport_line(2)) - elseif explored.side == 1 then + elseif explored.side == 1 then table.insert(left, explored.belt.get_transport_line(1)) table.insert(right, explored.belt.get_transport_line(2)) end @@ -322,22 +371,19 @@ if explored.side == 0 then side = 2 end - - table.insert(frontier, {side = side, belt = belt}) + table.insert(frontier, { side = side, belt = belt }) end end end -if explored.side == 0 then + if explored.side == 0 then table.insert(left, explored.belt.get_transport_line(1)) table.insert(left, explored.belt.get_transport_line(2)) elseif explored.side == 2 then table.insert(right, explored.belt.get_transport_line(1)) table.insert(right, explored.belt.get_transport_line(2)) - - elseif explored.side == 1 then + elseif explored.side == 1 then table.insert(left, explored.belt.get_transport_line(1)) table.insert(right, explored.belt.get_transport_line(2)) - end end @@ -348,9 +394,8 @@ if explored.side == 0 then end end - - local downstream = {left = table.deepcopy(left), right = table.deepcopy(right)} - local upstream = {left = {}, right = {}} + local downstream = { left = table.deepcopy(left), right = table.deepcopy(right) } + local upstream = { left = {}, right = {} } while #frontier > 0 do local explored = table.remove(frontier, 1) @@ -371,41 +416,35 @@ if explored.side == 0 then side = 2 end - - table.insert(frontier, {side = side, belt = belt}) + table.insert(frontier, { side = side, belt = belt }) end end end -if explored.side == 0 then + if explored.side == 0 then table.insert(left, explored.belt.get_transport_line(1)) table.insert(left, explored.belt.get_transport_line(2)) table.insert(upstream.left, explored.belt.get_transport_line(1)) table.insert(upstream.left, explored.belt.get_transport_line(2)) - elseif explored.side == 2 then table.insert(right, explored.belt.get_transport_line(1)) table.insert(right, explored.belt.get_transport_line(2)) table.insert(upstream.right, explored.belt.get_transport_line(1)) table.insert(upstream.right, explored.belt.get_transport_line(2)) - - elseif explored.side == 1 then + elseif explored.side == 1 then table.insert(left, explored.belt.get_transport_line(1)) table.insert(right, explored.belt.get_transport_line(2)) table.insert(upstream.left, explored.belt.get_transport_line(1)) table.insert(upstream.right, explored.belt.get_transport_line(2)) - end end - - return {combined = {left = left, right = right}, upstream = upstream, downstream = downstream} - + return { combined = { left = left, right = right }, upstream = upstream, downstream = downstream } end --Belt analyzer: Returns a hash table of the belt units connected to the belt unit B. function mod.get_connected_belts(B) local result = {} - local frontier = {table.deepcopy(B)} + local frontier = { table.deepcopy(B) } local hash = {} hash[B.unit_number] = true while #frontier > 0 do @@ -425,10 +464,9 @@ function mod.get_connected_belts(B) end end table.insert(result, table.deepcopy(explored)) - end - return {hash = hash, ents = result} + return { hash = hash, ents = result } end --Transport belt analyzer: Read a results list slot @@ -464,7 +502,6 @@ function mod.read_belt_slot(pindex, start_phrase) else result = result .. "Unspecified lane, " end - end --Read lane contents if players[pindex].belt.sector == 1 and players[pindex].belt.side == 1 then @@ -489,7 +526,6 @@ function mod.read_belt_slot(pindex, start_phrase) elseif players[pindex].belt.side == 2 then array = players[pindex].belt.network.upstream.right end - else return end @@ -499,16 +535,13 @@ function mod.read_belt_slot(pindex, start_phrase) if stack ~= nil and stack.valid_for_read and stack.valid then result = result .. stack.name .. " x " .. stack.count - if players[pindex].belt.sector > 1 then - result = result .. ", " .. stack.percent .. "%" - end + if players[pindex].belt.sector > 1 then result = result .. ", " .. stack.percent .. "%" end else result = result .. "Empty slot" end printout(result, pindex) end - --Set the input priority or the output priority or filter for a splitter function mod.set_splitter_priority(splitter, is_input, is_left, filter_item_stack, clear) local clear = clear or false @@ -528,7 +561,7 @@ function mod.set_splitter_priority(splitter, is_input, is_left, filter_item_stac splitter.splitter_output_priority = "left" result = result .. ", from the left" end - elseif is_input and is_left then + elseif is_input and is_left then if splitter.splitter_input_priority == "left" then splitter.splitter_input_priority = "none" result = "equal input priority" @@ -594,30 +627,62 @@ function mod.splitter_priority_info(ent) if input == "none" then result = result .. " input balanced, " elseif input == "right" then - result = result .. " input priority " .. "right" .. " which is " .. fa_utils.direction_lookup(fa_utils.rotate_90(ent.direction)) .. ", " + result = result + .. " input priority " + .. "right" + .. " which is " + .. fa_utils.direction_lookup(fa_utils.rotate_90(ent.direction)) + .. ", " elseif input == "left" then - result = result .. " input priority " .. "left" .. " which is " .. fa_utils.direction_lookup(fa_utils.rotate_270(ent.direction)) .. ", " + result = result + .. " input priority " + .. "left" + .. " which is " + .. fa_utils.direction_lookup(fa_utils.rotate_270(ent.direction)) + .. ", " end if filter == nil then if output == "none" then result = result .. " output balanced, " elseif output == "right" then - result = result .. " output priority " .. "right" .. " which is " .. fa_utils.direction_lookup(fa_utils.rotate_90(ent.direction)) .. ", " + result = result + .. " output priority " + .. "right" + .. " which is " + .. fa_utils.direction_lookup(fa_utils.rotate_90(ent.direction)) + .. ", " elseif output == "left" then - result = result .. " output priority " .. "left" .. " which is " .. fa_utils.direction_lookup(fa_utils.rotate_270(ent.direction)) .. ", " + result = result + .. " output priority " + .. "left" + .. " which is " + .. fa_utils.direction_lookup(fa_utils.rotate_270(ent.direction)) + .. ", " end else - local item_name = localising.get(filter,pindex) - if item_name == nil or item_name == "" then - item_name = "unknown item" - end + local item_name = localising.get(filter, pindex) + if item_name == nil or item_name == "" then item_name = "unknown item" end if output == "right" then - result = result .. " output filtering " .. item_name .. " towards the " .. "right" .. " which is " .. fa_utils.direction_lookup(fa_utils.rotate_90(ent.direction)) .. ", " + result = result + .. " output filtering " + .. item_name + .. " towards the " + .. "right" + .. " which is " + .. fa_utils.direction_lookup(fa_utils.rotate_90(ent.direction)) + .. ", " elseif output == "left" then - result = result .. " output filtering " .. item_name .. " towards the " .. "left" .. " which is " .. fa_utils.direction_lookup(fa_utils.rotate_270(ent.direction)) .. ", " + result = result + .. " output filtering " + .. item_name + .. " towards the " + .. "left" + .. " which is " + .. fa_utils.direction_lookup(fa_utils.rotate_270(ent.direction)) + .. ", " end end return result end -return mod \ No newline at end of file +return mod diff --git a/scripts/travel-tools.lua b/scripts/travel-tools.lua index 86619d5a..f8a3facf 100644 --- a/scripts/travel-tools.lua +++ b/scripts/travel-tools.lua @@ -20,41 +20,53 @@ function mod.move_cursor_structure(pindex, dir) local current = players[pindex].structure_travel.current local index = players[pindex].structure_travel.index if direction == "none" then - if #network[current][adjusted[(0 + dir) %8]] > 0 then - players[pindex].structure_travel.direction = adjusted[(0 + dir)%8] + if #network[current][adjusted[(0 + dir) % 8]] > 0 then + players[pindex].structure_travel.direction = adjusted[(0 + dir) % 8] players[pindex].structure_travel.index = 1 local index = players[pindex].structure_travel.index - local dx = network[current][adjusted[(0 + dir)%8]][index].dx - local dy = network[current][adjusted[(0 + dir) %8]][index].dy + local dx = network[current][adjusted[(0 + dir) % 8]][index].dx + local dy = network[current][adjusted[(0 + dir) % 8]][index].dy local description = "" - if math.floor(math.abs(dx)+ .5) ~= 0 then + if math.floor(math.abs(dx) + 0.5) ~= 0 then if dx < 0 then - description = description .. math.floor(math.abs(dx)+.5) .. " " .. "tiles west, " + description = description .. math.floor(math.abs(dx) + 0.5) .. " " .. "tiles west, " elseif dx > 0 then - description = description .. math.floor(math.abs(dx)+.5) .. " " .. "tiles east, " + description = description .. math.floor(math.abs(dx) + 0.5) .. " " .. "tiles east, " end end - if math.floor(math.abs(dy)+ .5) ~= 0 then + if math.floor(math.abs(dy) + 0.5) ~= 0 then if dy < 0 then - description = description .. math.floor(math.abs(dy)+.5) .. " " .. "tiles north, " + description = description .. math.floor(math.abs(dy) + 0.5) .. " " .. "tiles north, " elseif dy > 0 then - description = description .. math.floor(math.abs(dy)+.5) .. " " .. "tiles south, " + description = description .. math.floor(math.abs(dy) + 0.5) .. " " .. "tiles south, " end end - local ent = network[network[current][adjusted[(0 + dir) %8]][index].num] + local ent = network[network[current][adjusted[(0 + dir) % 8]][index].num] if ent.ent.valid then fa_graphics.draw_cursor_highlight(pindex, ent.ent, nil) - fa_mouse.move_mouse_pointer(ent.ent.position,pindex) + fa_mouse.move_mouse_pointer(ent.ent.position, pindex) players[pindex].cursor_pos = ent.ent.position --Case 1: Proposing a new structure - printout("To " .. ent.name .. " " .. fa_scanner.ent_extra_list_info(ent.ent,pindex,true) .. ", " .. description .. ", " .. index .. " of " .. #network[current][adjusted[(0 + dir) % 8]], pindex) + printout( + "To " + .. ent.name + .. " " + .. fa_scanner.ent_extra_list_info(ent.ent, pindex, true) + .. ", " + .. description + .. ", " + .. index + .. " of " + .. #network[current][adjusted[(0 + dir) % 8]], + pindex + ) else printout("Missing " .. ent.name .. " " .. description, pindex) end else - printout("There are no buildings directly " .. adjusted[(0 + dir) %8] .. " of this one.", pindex) + printout("There are no buildings directly " .. adjusted[(0 + dir) % 8] .. " of this one.", pindex) end - elseif direction == adjusted[(4 + dir)%8] then + elseif direction == adjusted[(4 + dir) % 8] then players[pindex].structure_travel.direction = "none" local description = "" if #network[current].north > 0 then @@ -69,22 +81,28 @@ function mod.move_cursor_structure(pindex, dir) if #network[current].west > 0 then description = description .. ", " .. #network[current].west .. " connections west," end - if description == "" then - description = "No nearby buildings." - end + if description == "" then description = "No nearby buildings." end local ent = network[current] if ent.ent.valid then fa_graphics.draw_cursor_highlight(pindex, ent.ent, nil) - fa_mouse.move_mouse_pointer(ent.ent.position,pindex) + fa_mouse.move_mouse_pointer(ent.ent.position, pindex) players[pindex].cursor_pos = ent.ent.position --Case 2: Returning to the current structure - printout("Back at " .. ent.name .. " " .. fa_scanner.ent_extra_list_info(ent.ent,pindex,true) .. ", " .. description, pindex) + printout( + "Back at " + .. ent.name + .. " " + .. fa_scanner.ent_extra_list_info(ent.ent, pindex, true) + .. ", " + .. description, + pindex + ) else printout("Missing " .. ent.name .. " " .. description, pindex) end - elseif direction == adjusted[(0 + dir) %8] then + elseif direction == adjusted[(0 + dir) % 8] then players[pindex].structure_travel.direction = "none" - players[pindex].structure_travel.current = network[current][adjusted[(0 + dir) %8]][index].num + players[pindex].structure_travel.current = network[current][adjusted[(0 + dir) % 8]][index].num local current = players[pindex].structure_travel.current local description = "" @@ -100,77 +118,98 @@ function mod.move_cursor_structure(pindex, dir) if #network[current].west > 0 then description = description .. ", " .. #network[current].west .. " connections west," end - if description == "" then - description = "No nearby buildings." - end + if description == "" then description = "No nearby buildings." end local ent = network[current] - if ent.ent.valid then + if ent.ent.valid then fa_graphics.draw_cursor_highlight(pindex, ent.ent, nil) - fa_mouse.move_mouse_pointer(ent.ent.position,pindex) + fa_mouse.move_mouse_pointer(ent.ent.position, pindex) players[pindex].cursor_pos = ent.ent.position --Case 3: Moved to the new structure - printout("Now at " .. ent.name .. " " .. fa_scanner.ent_extra_list_info(ent.ent,pindex,true) .. ", " .. description, pindex) + printout( + "Now at " .. ent.name .. " " .. fa_scanner.ent_extra_list_info(ent.ent, pindex, true) .. ", " .. description, + pindex + ) else printout("Missing " .. ent.name .. " " .. description, pindex) end - elseif direction == adjusted[(2 + dir)%8] or direction == adjusted[(6 + dir) %8] then + elseif direction == adjusted[(2 + dir) % 8] or direction == adjusted[(6 + dir) % 8] then if (dir == 0 or dir == 6) and index > 1 then - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) players[pindex].structure_travel.index = index - 1 elseif (dir == 2 or dir == 4) and index < #network[current][direction] then - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) players[pindex].structure_travel.index = index + 1 end local index = players[pindex].structure_travel.index local dx = network[current][direction][index].dx local dy = network[current][direction][index].dy local description = "" - if math.floor(math.abs(dx)+ .5) ~= 0 then + if math.floor(math.abs(dx) + 0.5) ~= 0 then if dx < 0 then - description = description .. math.floor(math.abs(dx)+.5) .. " " .. "tiles west, " + description = description .. math.floor(math.abs(dx) + 0.5) .. " " .. "tiles west, " elseif dx > 0 then - description = description .. math.floor(math.abs(dx)+.5) .. " " .. "tiles east, " + description = description .. math.floor(math.abs(dx) + 0.5) .. " " .. "tiles east, " end end - if math.floor(math.abs(dy)+ .5) ~= 0 then + if math.floor(math.abs(dy) + 0.5) ~= 0 then if dy < 0 then - description = description .. math.floor(math.abs(dy)+.5) .. " " .. "tiles north, " + description = description .. math.floor(math.abs(dy) + 0.5) .. " " .. "tiles north, " elseif dy > 0 then - description = description .. math.floor(math.abs(dy)+.5) .. " " .. "tiles south, " + description = description .. math.floor(math.abs(dy) + 0.5) .. " " .. "tiles south, " end end local ent = network[network[current][direction][index].num] if ent.ent.valid then fa_graphics.draw_cursor_highlight(pindex, ent.ent, nil) - fa_mouse.move_mouse_pointer(ent.ent.position,pindex) + fa_mouse.move_mouse_pointer(ent.ent.position, pindex) players[pindex].cursor_pos = ent.ent.position --Case 4: Propose a new structure within the same direction - printout("To " .. ent.name .. " " .. fa_scanner.ent_extra_list_info(ent.ent,pindex,true) .. ", " .. description .. ", " .. index .. " of " .. #network[current][direction], pindex) + printout( + "To " + .. ent.name + .. " " + .. fa_scanner.ent_extra_list_info(ent.ent, pindex, true) + .. ", " + .. description + .. ", " + .. index + .. " of " + .. #network[current][direction], + pindex + ) else printout("Missing " .. ent.name .. " " .. description, pindex) end end end ---Structure travel: Creates the building network that is traveled during structure travel. +--Structure travel: Creates the building network that is traveled during structure travel. --**Todo bug: Some neighboring structures are not picked up when they should be such as machines next to inserters -function mod.compile_building_network(ent, radius_in,pindex) +function mod.compile_building_network(ent, radius_in, pindex) local radius = radius_in - local ents = ent.surface.find_entities_filtered{position = ent.position, radius = radius} + local ents = ent.surface.find_entities_filtered({ position = ent.position, radius = radius }) game.get_player(pindex).print(#ents .. " ents at first pass") if #ents < 100 then radius = radius_in * 2 - ents = ent.surface.find_entities_filtered{position = ent.position, radius = radius} + ents = ent.surface.find_entities_filtered({ position = ent.position, radius = radius }) elseif #ents > 2000 then - radius = math.floor(radius_in/4) - ents = ent.surface.find_entities_filtered{position = ent.position, radius = radius} + radius = math.floor(radius_in / 4) + ents = ent.surface.find_entities_filtered({ position = ent.position, radius = radius }) elseif #ents > 1000 then - radius = math.floor(radius_in/2) - ents = ent.surface.find_entities_filtered{position = ent.position, radius = radius} + radius = math.floor(radius_in / 2) + ents = ent.surface.find_entities_filtered({ position = ent.position, radius = radius }) end - rendering.draw_circle{color = {1, 1, 1},radius = radius,width = 20,target = ent.position, surface = ent.surface, draw_on_ground = true, time_to_live = 300} + rendering.draw_circle({ + color = { 1, 1, 1 }, + radius = radius, + width = 20, + target = ent.position, + surface = ent.surface, + draw_on_ground = true, + time_to_live = 300, + }) --game.get_player(pindex).print(#ents .. " ents at start") - local adj = {hor = {}, vert = {}} + local adj = { hor = {}, vert = {} } local PQ = {} local result = {} --game.get_player(pindex).print("checkpoint 0") @@ -187,14 +226,14 @@ function mod.compile_building_network(ent, radius_in,pindex) north = {}, east = {}, south = {}, - west = {} + west = {}, } else table.remove(ents, i) end end - game.get_player(pindex).print(#ents .. " buildings found")--**keep here intentionally + game.get_player(pindex).print(#ents .. " buildings found") --**keep here intentionally --game.get_player(pindex).print("checkpoint 1") for i, row in pairs(ents) do @@ -214,58 +253,52 @@ function mod.compile_building_network(ent, radius_in,pindex) dest = col, dx = col.position.x - row.position.x, dy = col.position.y - row.position.y, - man = math.abs(col.position.x - row.position.x) + math.abs(col.position.y - row.position.y) + man = math.abs(col.position.x - row.position.x) + math.abs(col.position.y - row.position.y), }) - end end - end end --game.get_player(pindex).print("checkpoint 2") - table.sort(PQ, function (k1, k2) + table.sort(PQ, function(k1, k2) return k1.man > k2.man end) --game.get_player(pindex).print("checkpoint 3, #PQ = " .. #PQ)-- local entry = table.remove(PQ) local loop_count = 0 - while entry~= nil and loop_count < #PQ * 2 do + while entry ~= nil and loop_count < #PQ * 2 do loop_count = loop_count + 1 if math.abs(entry.dy) >= math.abs(entry.dx) then if not adj.vert[entry.source.unit_number][entry.dest.unit_number] then for i, explored in pairs(adj.vert[entry.source.unit_number]) do adj.vert[entry.source.unit_number][i] = (explored or adj.vert[entry.dest.unit_number][i]) end - for i, row in pairs(adj.vert) do - if adj.vert[entry.source.unit_number][i] then - adj.vert[i] = adj.vert[entry.source.unit_number] + for i, row in pairs(adj.vert) do + if adj.vert[entry.source.unit_number][i] then adj.vert[i] = adj.vert[entry.source.unit_number] end end - end if entry.dy > 0 then - table.insert(result[entry.source.unit_number].south, { num = entry.dest.unit_number, dx = entry.dx, - dy = entry.dy + dy = entry.dy, }) table.insert(result[entry.dest.unit_number].north, { num = entry.source.unit_number, dx = entry.dx * -1, - dy = entry.dy * -1 + dy = entry.dy * -1, }) else table.insert(result[entry.source.unit_number].north, { num = entry.dest.unit_number, dx = entry.dx, - dy = entry.dy + dy = entry.dy, }) table.insert(result[entry.dest.unit_number].south, { num = entry.source.unit_number, dx = entry.dx * -1, - dy = entry.dy * -1 + dy = entry.dy * -1, }) - end end end @@ -274,37 +307,33 @@ function mod.compile_building_network(ent, radius_in,pindex) for i, explored in pairs(adj.hor[entry.source.unit_number]) do adj.hor[entry.source.unit_number][i] = explored or adj.hor[entry.dest.unit_number][i] end - for i, row in pairs(adj.hor) do - if adj.hor[entry.source.unit_number][i] then - adj.hor[i] = adj.hor[entry.source.unit_number] + for i, row in pairs(adj.hor) do + if adj.hor[entry.source.unit_number][i] then adj.hor[i] = adj.hor[entry.source.unit_number] end end - end if entry.dx > 0 then table.insert(result[entry.source.unit_number].east, { num = entry.dest.unit_number, dx = entry.dx, - dy = entry.dy + dy = entry.dy, }) table.insert(result[entry.dest.unit_number].west, { num = entry.source.unit_number, dx = entry.dx * -1, - dy = entry.dy * -1 + dy = entry.dy * -1, }) else table.insert(result[entry.source.unit_number].west, { num = entry.dest.unit_number, dx = entry.dx, - dy = entry.dy + dy = entry.dy, }) table.insert(result[entry.dest.unit_number].east, { num = entry.source.unit_number, dx = entry.dx * -1, - dy = entry.dy * -1 + dy = entry.dy * -1, }) - end end - end entry = table.remove(PQ) end @@ -313,19 +342,26 @@ function mod.compile_building_network(ent, radius_in,pindex) end function mod.fast_travel_menu_open(pindex) - if players[pindex].in_menu == false and game.get_player(pindex).driving == false and game.get_player(pindex).opened == nil then + if + players[pindex].in_menu == false + and game.get_player(pindex).driving == false + and game.get_player(pindex).opened == nil + then game.get_player(pindex).selected = nil players[pindex].menu = "travel" players[pindex].in_menu = true players[pindex].move_queue = {} - players[pindex].travel.index = {x = 1, y = 0} + players[pindex].travel.index = { x = 1, y = 0 } players[pindex].travel.creating = false players[pindex].travel.renaming = false players[pindex].travel.describing = false - printout("Navigate up and down with W and S to select a fast travel location, and jump to it with LEFT BRACKET. Alternatively, select an option by navigating left and right with A and D.", pindex) + printout( + "Navigate up and down with W and S to select a fast travel location, and jump to it with LEFT BRACKET. Alternatively, select an option by navigating left and right with A and D.", + pindex + ) local screen = game.get_player(pindex).gui.screen - local frame = screen.add{type = "frame", name = "travel"} + local frame = screen.add({ type = "frame", name = "travel" }) frame.bring_to_front() frame.force_auto_center() frame.focus() @@ -342,12 +378,8 @@ function mod.fast_travel_menu_open(pindex) if game.get_player(pindex).vehicle ~= nil and game.get_player(pindex).vehicle.train ~= nil then vehicle = game.get_player(pindex).vehicle local connected = 0 - if vehicle.get_connected_rolling_stock(defines.rail_direction.front) ~= nil then - connected = connected + 1 - end - if vehicle.get_connected_rolling_stock(defines.rail_direction.back) ~= nil then - connected = connected + 1 - end + if vehicle.get_connected_rolling_stock(defines.rail_direction.front) ~= nil then connected = connected + 1 end + if vehicle.get_connected_rolling_stock(defines.rail_direction.back) ~= nil then connected = connected + 1 end if connected == 0 then printout("Warning, this vehicle was disconnected. Please review mod settings.", pindex) --Attempt to reconnect (does not work) @@ -363,26 +395,42 @@ function mod.read_fast_travel_slot(pindex) printout("Move towards the right and select Create to get started.", pindex) else local entry = players[pindex].travel[players[pindex].travel.index.y] - printout(entry.name .. " at " .. math.floor(entry.position.x) .. ", " .. math.floor(entry.position.y) .. ", cursor moved.", pindex) + printout( + entry.name + .. " at " + .. math.floor(entry.position.x) + .. ", " + .. math.floor(entry.position.y) + .. ", cursor moved.", + pindex + ) players[pindex].cursor_pos = fa_utils.center_of_tile(entry.position) fa_graphics.draw_cursor_highlight(pindex, nil, "train-visualization") end end function mod.fast_travel_menu_click(pindex) - if players[pindex].travel.input_box then - players[pindex].travel.input_box.destroy() - end + if players[pindex].travel.input_box then players[pindex].travel.input_box.destroy() end if #global.players[pindex].travel == 0 and players[pindex].travel.index.x < TRAVEL_MENU_LENGTH then printout("Move towards the right and select Create New to get started.", pindex) elseif players[pindex].travel.index.y == 0 and players[pindex].travel.index.x < TRAVEL_MENU_LENGTH then - printout("Navigate up and down to select a fast travel point, then press LEFT BRACKET to get there quickly.", pindex) + printout( + "Navigate up and down to select a fast travel point, then press LEFT BRACKET to get there quickly.", + pindex + ) elseif players[pindex].travel.index.x == 1 then --Travel - local success = fa_teleport.teleport_to_closest(pindex, global.players[pindex].travel[players[pindex].travel.index.y].position, false, false) + local success = fa_teleport.teleport_to_closest( + pindex, + global.players[pindex].travel[players[pindex].travel.index.y].position, + false, + false + ) if success and players[pindex].cursor then - players[pindex].cursor_pos = table.deepcopy(global.players[pindex].travel[players[pindex].travel.index.y].position) + players[pindex].cursor_pos = + table.deepcopy(global.players[pindex].travel[players[pindex].travel.index.y].position) else - players[pindex].cursor_pos = fa_utils.offset_position(players[pindex].position, players[pindex].player_direction, 1) + players[pindex].cursor_pos = + fa_utils.offset_position(players[pindex].position, players[pindex].player_direction, 1) end fa_graphics.sync_build_cursor_graphics(pindex) game.get_player(pindex).opened = nil @@ -407,10 +455,13 @@ function mod.fast_travel_menu_click(pindex) end printout(desc, pindex) elseif players[pindex].travel.index.x == 3 then --Rename - printout("Type in a new name for this fast travel point, then press 'ENTER' to confirm, or press 'ESC' to cancel.", pindex) + printout( + "Type in a new name for this fast travel point, then press 'ENTER' to confirm, or press 'ESC' to cancel.", + pindex + ) players[pindex].travel.renaming = true local frame = game.get_player(pindex).gui.screen["travel"] - players[pindex].travel.input_box = frame.add{type="textfield", name = "input"} + players[pindex].travel.input_box = frame.add({ type = "textfield", name = "input" }) local input = players[pindex].travel.input_box input.focus() input.select(1, 0) @@ -423,13 +474,22 @@ function mod.fast_travel_menu_click(pindex) printout("Type in the new description text, then press 'ENTER' to confirm, or press 'ESC' to cancel.", pindex) players[pindex].travel.describing = true local frame = game.get_player(pindex).gui.screen["travel"] - players[pindex].travel.input_box = frame.add{type="textfield", name = "input"} + players[pindex].travel.input_box = frame.add({ type = "textfield", name = "input" }) local input = players[pindex].travel.input_box input.focus() input.select(1, 0) elseif players[pindex].travel.index.x == 5 then --Relocate to current character position - players[pindex].travel[players[pindex].travel.index.y].position = fa_utils.center_of_tile(players[pindex].position) - printout("Relocated point ".. players[pindex].travel[players[pindex].travel.index.y].name .. " to " .. math.floor(players[pindex].position.x) .. ", " .. math.floor(players[pindex].position.y), pindex) + players[pindex].travel[players[pindex].travel.index.y].position = + fa_utils.center_of_tile(players[pindex].position) + printout( + "Relocated point " + .. players[pindex].travel[players[pindex].travel.index.y].name + .. " to " + .. math.floor(players[pindex].position.x) + .. ", " + .. math.floor(players[pindex].position.y), + pindex + ) players[pindex].cursor_pos = players[pindex].position fa_graphics.draw_cursor_highlight(pindex) elseif players[pindex].travel.index.x == 6 then --Delete @@ -437,11 +497,14 @@ function mod.fast_travel_menu_click(pindex) table.remove(global.players[pindex].travel, players[pindex].travel.index.y) players[pindex].travel.x = 1 players[pindex].travel.index.y = players[pindex].travel.index.y - 1 - elseif players[pindex].travel.index.x == 7 then --Create new - printout("Type in a name for this fast travel point, then press 'ENTER' to confirm, or press 'ESC' to cancel.", pindex) + elseif players[pindex].travel.index.x == 7 then --Create new + printout( + "Type in a name for this fast travel point, then press 'ENTER' to confirm, or press 'ESC' to cancel.", + pindex + ) players[pindex].travel.creating = true local frame = game.get_player(pindex).gui.screen["travel"] - players[pindex].travel.input_box = frame.add{type="textfield", name = "input"} + players[pindex].travel.input_box = frame.add({ type = "textfield", name = "input" }) local input = players[pindex].travel.input_box input.focus() input.select(1, 0) @@ -451,11 +514,11 @@ TRAVEL_MENU_LENGTH = 7 function mod.fast_travel_menu_up(pindex) if players[pindex].travel.index.y > 1 then - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) players[pindex].travel.index.y = players[pindex].travel.index.y - 1 else players[pindex].travel.index.y = 1 - game.get_player(pindex).play_sound{path = "inventory-edge"} + game.get_player(pindex).play_sound({ path = "inventory-edge" }) end players[pindex].travel.index.x = 1 mod.read_fast_travel_slot(pindex) @@ -463,11 +526,11 @@ end function mod.fast_travel_menu_down(pindex) if players[pindex].travel.index.y < #players[pindex].travel then - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) players[pindex].travel.index.y = players[pindex].travel.index.y + 1 else players[pindex].travel.index.y = #players[pindex].travel - game.get_player(pindex).play_sound{path = "inventory-edge"} + game.get_player(pindex).play_sound({ path = "inventory-edge" }) end players[pindex].travel.index.x = 1 mod.read_fast_travel_slot(pindex) @@ -475,10 +538,10 @@ end function mod.fast_travel_menu_right(pindex) if players[pindex].travel.index.x < TRAVEL_MENU_LENGTH then - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) players[pindex].travel.index.x = players[pindex].travel.index.x + 1 else - game.get_player(pindex).play_sound{path = "inventory-edge"} + game.get_player(pindex).play_sound({ path = "inventory-edge" }) end if players[pindex].travel.index.x == 1 then printout("Travel", pindex) @@ -499,10 +562,10 @@ end function mod.fast_travel_menu_left(pindex) if players[pindex].travel.index.x > 1 then - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) players[pindex].travel.index.x = players[pindex].travel.index.x - 1 else - game.get_player(pindex).play_sound{path = "inventory-edge"} + game.get_player(pindex).play_sound({ path = "inventory-edge" }) end if players[pindex].travel.index.x == 1 then printout("Travel", pindex) @@ -522,9 +585,7 @@ function mod.fast_travel_menu_left(pindex) end function mod.fast_travel_menu_close(pindex) - if game.get_player(pindex).gui.screen["travel"] then - game.get_player(pindex).gui.screen["travel"].destroy() - end + if game.get_player(pindex).gui.screen["travel"] then game.get_player(pindex).gui.screen["travel"].destroy() end players[pindex].menu = "none" players[pindex].in_menu = false end @@ -537,4 +598,4 @@ function mod.structure_travel_menu_close(pindex) players[pindex].in_menu = false end -return mod \ No newline at end of file +return mod diff --git a/scripts/tutorial-system.lua b/scripts/tutorial-system.lua index 27814729..eb6f300f 100644 --- a/scripts/tutorial-system.lua +++ b/scripts/tutorial-system.lua @@ -5,13 +5,13 @@ local mod = {} --The tutorial strings are fetched according to locale, with the main set of tutorial strings being im English ("en"). --Other locales are expected to have the same arrangement of tutorial steps. function mod.load_tutorial(pindex) - local tutorial = players[pindex].tutorial + local tutorial = players[pindex].tutorial local p = game.get_player(pindex) tutorial = {} - + --Load tutorial header and detail strings - tutorial.step_headers = {} --2D array of localised strings - tutorial.step_details = {} --2D array of localised strings + tutorial.step_headers = {} --2D array of localised strings + tutorial.step_details = {} --2D array of localised strings local CHAPTER_1_LENGTH = 21 --Lengths are constants depending on how we write the "en" strings. local CHAPTER_2_LENGTH = 30 local CHAPTER_3_LENGTH = 27 @@ -26,50 +26,72 @@ function mod.load_tutorial(pindex) local CHAPTER_12_LENGTH = 14 local CHAPTER_13_LENGTH = 25 - tutorial.chapter_lengths = {CHAPTER_1_LENGTH, CHAPTER_2_LENGTH, CHAPTER_3_LENGTH, CHAPTER_4_LENGTH, CHAPTER_5_LENGTH, CHAPTER_6_LENGTH, CHAPTER_7_LENGTH, CHAPTER_8_LENGTH, CHAPTER_9_LENGTH, CHAPTER_10_LENGTH, CHAPTER_11_LENGTH, CHAPTER_12_LENGTH, CHAPTER_13_LENGTH} - + tutorial.chapter_lengths = { + CHAPTER_1_LENGTH, + CHAPTER_2_LENGTH, + CHAPTER_3_LENGTH, + CHAPTER_4_LENGTH, + CHAPTER_5_LENGTH, + CHAPTER_6_LENGTH, + CHAPTER_7_LENGTH, + CHAPTER_8_LENGTH, + CHAPTER_9_LENGTH, + CHAPTER_10_LENGTH, + CHAPTER_11_LENGTH, + CHAPTER_12_LENGTH, + CHAPTER_13_LENGTH, + } + local str_count = 0 local err_count = 0 for i = 1, #tutorial.chapter_lengths, 1 do --for every chapter - local chapter_length = tutorial.chapter_lengths[i] + local chapter_length = tutorial.chapter_lengths[i] tutorial.step_headers[i] = {} tutorial.step_details[i] = {} - + for j = 1, chapter_length, 1 do --for every step local header_str_name = "tutorial.tutorial-chapter-" .. i .. "-step-" .. j .. "-header" - local header_localised_str = {header_str_name} + local header_localised_str = { header_str_name } if header_localised_str ~= nil then table.insert(tutorial.step_headers[i], j, header_localised_str) --for each step else err_count = err_count + 1 --p.print("error in preparing tutorial header string " .. i .. "-" .. j,{volume_modifier = 0}) end - + local detail_str_name = "tutorial.tutorial-chapter-" .. i .. "-step-" .. j .. "-detail" - local detail_localised_str = {detail_str_name} + local detail_localised_str = { detail_str_name } if detail_localised_str ~= nil then table.insert(tutorial.step_details[i], j, detail_localised_str) --for each step else err_count = err_count + 1 --p.print("error in preparing tutorial detail string " .. i .. "-" .. j,{volume_modifier = 0}) end - + str_count = str_count + 1 end end if err_count > 0 then - p.print(err_count .. " errors while preparing ".. str_count .. " tutorial strings",{volume_modifier = 0}) + p.print(err_count .. " errors while preparing " .. str_count .. " tutorial strings", { volume_modifier = 0 }) end - + --Load Chapter 0 strings - tutorial.chapter_0_messages = { {"tutorial.tutorial-chapter-0-message-1"}, {"tutorial.tutorial-chapter-0-message-2"}, - {"tutorial.tutorial-chapter-0-message-3"}, {"tutorial.tutorial-chapter-0-message-4"} } - tutorial.chapter_0_headers = { {"tutorial.tutorial-chapter-0-header-1"}, {"tutorial.tutorial-chapter-0-header-2"}, - {"tutorial.tutorial-chapter-0-header-3"}, {"tutorial.tutorial-chapter-0-header-4"} } - + tutorial.chapter_0_messages = { + { "tutorial.tutorial-chapter-0-message-1" }, + { "tutorial.tutorial-chapter-0-message-2" }, + { "tutorial.tutorial-chapter-0-message-3" }, + { "tutorial.tutorial-chapter-0-message-4" }, + } + tutorial.chapter_0_headers = { + { "tutorial.tutorial-chapter-0-header-1" }, + { "tutorial.tutorial-chapter-0-header-2" }, + { "tutorial.tutorial-chapter-0-header-3" }, + { "tutorial.tutorial-chapter-0-header-4" }, + } + --Load other tutorial strings --... - + --Load other tutorial variables tutorial.chapter_index = 0 tutorial.step_index = 1 @@ -85,88 +107,86 @@ function mod.read_current_step(pindex) local tutorial = players[pindex].tutorial if tutorial == nil then mod.load_tutorial(pindex) - mod.run_tutorial_menu(pindex, players[pindex].tutorial.reading_the_header, players[pindex].tutorial.clicked) + mod.run_tutorial_menu(pindex, players[pindex].tutorial.reading_the_header, players[pindex].tutorial.clicked) return end players[pindex].tutorial = tutorial - mod.run_tutorial_menu(pindex, players[pindex].tutorial.reading_the_header, players[pindex].tutorial.clicked) + mod.run_tutorial_menu(pindex, players[pindex].tutorial.reading_the_header, players[pindex].tutorial.clicked) end function mod.toggle_header_detail(pindex) local tutorial = players[pindex].tutorial if tutorial == nil then mod.load_tutorial(pindex) - mod.run_tutorial_menu(pindex, players[pindex].tutorial.reading_the_header, players[pindex].tutorial.clicked) - return + mod.run_tutorial_menu(pindex, players[pindex].tutorial.reading_the_header, players[pindex].tutorial.clicked) + return end tutorial.reading_the_header = not tutorial.reading_the_header players[pindex].tutorial = tutorial - mod.run_tutorial_menu(pindex, players[pindex].tutorial.reading_the_header, players[pindex].tutorial.clicked) -end + mod.run_tutorial_menu(pindex, players[pindex].tutorial.reading_the_header, players[pindex].tutorial.clicked) +end --Reads the header in detail mode and vice versa. function mod.read_other_once(pindex) local tutorial = players[pindex].tutorial - if tutorial == nil then - mod.load_tutorial(pindex) - end - mod.run_tutorial_menu(pindex, (not players[pindex].tutorial.reading_the_header), players[pindex].tutorial.clicked) -end + if tutorial == nil then mod.load_tutorial(pindex) end + mod.run_tutorial_menu(pindex, not players[pindex].tutorial.reading_the_header, players[pindex].tutorial.clicked) +end function mod.prev_step(pindex) - local tutorial = players[pindex].tutorial + local tutorial = players[pindex].tutorial if tutorial == nil then mod.load_tutorial(pindex) mod.run_tutorial_menu(pindex, players[pindex].tutorial.reading_the_header, players[pindex].tutorial.clicked) - return + return end - tutorial.step_index = tutorial.step_index - 1 - + tutorial.step_index = tutorial.step_index - 1 + --End of the previous chapter - if tutorial.step_index == 0 then - tutorial.chapter_index = tutorial.chapter_index - 1 - if tutorial.chapter_index == -1 then + if tutorial.step_index == 0 then + tutorial.chapter_index = tutorial.chapter_index - 1 + if tutorial.chapter_index == -1 then --Top of the entire list - tutorial.chapter_index = 0 - tutorial.step_index = 1 - game.get_player(pindex).play_sound{path = "inventory-edge"} - elseif tutorial.chapter_index == 0 then + tutorial.chapter_index = 0 + tutorial.step_index = 1 + game.get_player(pindex).play_sound({ path = "inventory-edge" }) + elseif tutorial.chapter_index == 0 then --End of chapter 0 - tutorial.step_index = #tutorial.chapter_0_messages - else + tutorial.step_index = #tutorial.chapter_0_messages + else --End of another chapter - tutorial.step_index = tutorial.chapter_lengths[tutorial.chapter_index] - end - end - + tutorial.step_index = tutorial.chapter_lengths[tutorial.chapter_index] + end + end + --Play sound - game.get_player(pindex).play_sound{path = "Inventory-Move"} - - --Load menu + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) + + --Load menu players[pindex].tutorial = tutorial mod.run_tutorial_menu(pindex, players[pindex].tutorial.reading_the_header, players[pindex].tutorial.clicked) end function mod.prev_chapter(pindex) - local tutorial = players[pindex].tutorial + local tutorial = players[pindex].tutorial if tutorial == nil then mod.load_tutorial(pindex) mod.run_tutorial_menu(pindex, players[pindex].tutorial.reading_the_header, players[pindex].tutorial.clicked) - return + return end - tutorial.step_index = 1 + tutorial.step_index = 1 tutorial.chapter_index = tutorial.chapter_index - 1 - + --Check index - if tutorial.chapter_index < 0 then - tutorial.chapter_index = 0 - game.get_player(pindex).play_sound{path = "inventory-edge"} - end - + if tutorial.chapter_index < 0 then + tutorial.chapter_index = 0 + game.get_player(pindex).play_sound({ path = "inventory-edge" }) + end + --Play sound - game.get_player(pindex).play_sound{path = "Inventory-Move"} - - --Load menu + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) + + --Load menu players[pindex].tutorial = tutorial mod.run_tutorial_menu(pindex, players[pindex].tutorial.reading_the_header, players[pindex].tutorial.clicked) end @@ -176,138 +196,150 @@ function mod.next_step(pindex) if tutorial == nil then mod.load_tutorial(pindex) mod.run_tutorial_menu(pindex, players[pindex].tutorial.reading_the_header, players[pindex].tutorial.clicked) - return + return end - local tutorial = players[pindex].tutorial - tutorial.step_index = tutorial.step_index + 1 - + local tutorial = players[pindex].tutorial + tutorial.step_index = tutorial.step_index + 1 + --End of Chapter 0 if tutorial.chapter_index == 0 and tutorial.step_index <= #tutorial.chapter_0_messages then --(do nothing) elseif tutorial.chapter_index == 0 and tutorial.step_index > #tutorial.chapter_0_messages then tutorial.chapter_index = 1 tutorial.step_index = 1 - + --End of another chapter - elseif tutorial.step_index > tutorial.chapter_lengths[tutorial.chapter_index] then - tutorial.chapter_index = tutorial.chapter_index + 1 - if tutorial.chapter_index > #tutorial.chapter_lengths or tutorial.chapter_lengths[tutorial.chapter_index] == 0 then + elseif tutorial.step_index > tutorial.chapter_lengths[tutorial.chapter_index] then + tutorial.chapter_index = tutorial.chapter_index + 1 + if + tutorial.chapter_index > #tutorial.chapter_lengths or tutorial.chapter_lengths[tutorial.chapter_index] == 0 + then --End of the entire list - tutorial.chapter_index = tutorial.chapter_index - 1 - tutorial.step_index = tutorial.step_index - 1 - game.get_player(pindex).play_sound{path = "inventory-edge"} - else + tutorial.chapter_index = tutorial.chapter_index - 1 + tutorial.step_index = tutorial.step_index - 1 + game.get_player(pindex).play_sound({ path = "inventory-edge" }) + else --start of the next chapter - tutorial.step_index = 1 - end - end - + tutorial.step_index = 1 + end + end + --Play sound - game.get_player(pindex).play_sound{path = "Inventory-Move"} - - --Load menu + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) + + --Load menu players[pindex].tutorial = tutorial mod.run_tutorial_menu(pindex, players[pindex].tutorial.reading_the_header, players[pindex].tutorial.clicked) end function mod.next_chapter(pindex) - local tutorial = players[pindex].tutorial + local tutorial = players[pindex].tutorial if tutorial == nil then mod.load_tutorial(pindex) mod.run_tutorial_menu(pindex, players[pindex].tutorial.reading_the_header, players[pindex].tutorial.clicked) - return + return end - tutorial.step_index = 1 + tutorial.step_index = 1 tutorial.chapter_index = tutorial.chapter_index + 1 - + --Check index - if tutorial.chapter_index > #tutorial.chapter_lengths or tutorial.chapter_lengths[tutorial.chapter_index] == 0 then - tutorial.chapter_index = tutorial.chapter_index - 1 - game.get_player(pindex).play_sound{path = "inventory-edge"} - end - + if tutorial.chapter_index > #tutorial.chapter_lengths or tutorial.chapter_lengths[tutorial.chapter_index] == 0 then + tutorial.chapter_index = tutorial.chapter_index - 1 + game.get_player(pindex).play_sound({ path = "inventory-edge" }) + end + --Play sound - game.get_player(pindex).play_sound{path = "Inventory-Move"} - - --Load menu + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) + + --Load menu players[pindex].tutorial = tutorial mod.run_tutorial_menu(pindex, players[pindex].tutorial.reading_the_header, players[pindex].tutorial.clicked) end function mod.read_current_header(pindex) - local tutorial = players[pindex].tutorial - local i = tutorial.chapter_index - local j = tutorial.step_index - local str = tutorial.step_headers[i][j] - printout(str,pindex) - game.get_player(pindex).print("Tutorial message summary, chapter " .. players[pindex].tutorial.chapter_index .. " , step " .. players[pindex].tutorial.step_index .. ": ",{volume_modifier=0})-- - game.get_player(pindex).print(str,{volume_modifier=0})-- + local tutorial = players[pindex].tutorial + local i = tutorial.chapter_index + local j = tutorial.step_index + local str = tutorial.step_headers[i][j] + printout(str, pindex) + game.get_player(pindex).print( + "Tutorial message summary, chapter " + .. players[pindex].tutorial.chapter_index + .. " , step " + .. players[pindex].tutorial.step_index + .. ": ", + { volume_modifier = 0 } + ) -- + game.get_player(pindex).print(str, { volume_modifier = 0 }) -- end function mod.read_current_detail(pindex) - local tutorial = players[pindex].tutorial - local i = tutorial.chapter_index - local j = tutorial.step_index - local str = tutorial.step_details[i][j] + local tutorial = players[pindex].tutorial + local i = tutorial.chapter_index + local j = tutorial.step_index + local str = tutorial.step_details[i][j] local str_h = tutorial.step_headers[i][j] - printout(str,pindex) - game.get_player(pindex).print("Tutorial message, chapter " .. players[pindex].tutorial.chapter_index .. " , step " .. players[pindex].tutorial.step_index .. ": ",{volume_modifier=0})-- - game.get_player(pindex).print(str_h,{volume_modifier=0})-- - game.get_player(pindex).print(str,{volume_modifier=0})-- + printout(str, pindex) + game.get_player(pindex).print( + "Tutorial message, chapter " + .. players[pindex].tutorial.chapter_index + .. " , step " + .. players[pindex].tutorial.step_index + .. ": ", + { volume_modifier = 0 } + ) -- + game.get_player(pindex).print(str_h, { volume_modifier = 0 }) -- + game.get_player(pindex).print(str, { volume_modifier = 0 }) -- end --For most steps this reads the already-loaded strings function mod.run_tutorial_menu(pindex, reading_the_header, clicked) - local tutorial = players[pindex].tutorial - local chap = tutorial.chapter_index - local step = tutorial.step_index + local tutorial = players[pindex].tutorial + local chap = tutorial.chapter_index + local step = tutorial.step_index local p = game.get_player(pindex) - if chap == 0 and step == 1 then - --Read out chapter 0 start message + if chap == 0 and step == 1 then + --Read out chapter 0 start message if reading_the_header == false then - printout(tutorial.chapter_0_messages[step],pindex) - game.get_player(pindex).print("Tutorial start message " .. step .. ":",{volume_modifier=0})-- - game.get_player(pindex).print(tutorial.chapter_0_messages[step],{volume_modifier=0}) + printout(tutorial.chapter_0_messages[step], pindex) + game.get_player(pindex).print("Tutorial start message " .. step .. ":", { volume_modifier = 0 }) -- + game.get_player(pindex).print(tutorial.chapter_0_messages[step], { volume_modifier = 0 }) else - printout(tutorial.chapter_0_headers[step],pindex) - game.get_player(pindex).print("Tutorial start message " .. step .. ":",{volume_modifier=0})-- - game.get_player(pindex).print(tutorial.chapter_0_headers[step],{volume_modifier=0}) + printout(tutorial.chapter_0_headers[step], pindex) + game.get_player(pindex).print("Tutorial start message " .. step .. ":", { volume_modifier = 0 }) -- + game.get_player(pindex).print(tutorial.chapter_0_headers[step], { volume_modifier = 0 }) end - + --Give rocket fuel - if players[pindex].tutorial.starting_fuel_provided ~= true then - p.insert{name = "coal", count = 50} - end - + if players[pindex].tutorial.starting_fuel_provided ~= true then p.insert({ name = "coal", count = 50 }) end + --Reload tutorial - game.get_player(pindex).play_sound{path = "Open-Inventory-Sound"} + game.get_player(pindex).play_sound({ path = "Open-Inventory-Sound" }) mod.load_tutorial(pindex) players[pindex].tutorial.starting_fuel_provided = true - elseif chap == 0 and step > 1 then --Read out chapter 0 start message if reading_the_header == false then - printout(tutorial.chapter_0_messages[step],pindex) - game.get_player(pindex).print("Tutorial start message " .. step .. ":",{volume_modifier=0})-- - game.get_player(pindex).print(tutorial.chapter_0_messages[step],{volume_modifier=0}) + printout(tutorial.chapter_0_messages[step], pindex) + game.get_player(pindex).print("Tutorial start message " .. step .. ":", { volume_modifier = 0 }) -- + game.get_player(pindex).print(tutorial.chapter_0_messages[step], { volume_modifier = 0 }) else - printout(tutorial.chapter_0_headers[step],pindex) - game.get_player(pindex).print("Tutorial start message " .. step .. ":",{volume_modifier=0})-- - game.get_player(pindex).print(tutorial.chapter_0_headers[step],{volume_modifier=0}) + printout(tutorial.chapter_0_headers[step], pindex) + game.get_player(pindex).print("Tutorial start message " .. step .. ":", { volume_modifier = 0 }) -- + game.get_player(pindex).print(tutorial.chapter_0_headers[step], { volume_modifier = 0 }) end - - elseif chap == -1 and step == -1 then --Example - --Do a specific action for this step, e.g. provide an item or run a check + elseif chap == -1 and step == -1 then --Example + --Do a specific action for this step, e.g. provide an item or run a check if clicked == false then --Read this step's header/detail if reading_the_header == true then - mod.read_current_header(pindex)--Check step header, e.g. "multiple furnaces check" + mod.read_current_header(pindex) --Check step header, e.g. "multiple furnaces check" else - mod.read_current_detail(pindex)--Check step detail, e.g. "click here to run a check for this step" + mod.read_current_detail(pindex) --Check step detail, e.g. "click here to run a check for this step" end else --if clicked == true then --Run the check and print the appropriate tutorial check result string - local ents = p.surface.find_entities_filtered{position = p.position, radius = 100, name = "stone-furnace"} + local ents = p.surface.find_entities_filtered({ position = p.position, radius = 100, name = "stone-furnace" }) if #ents > 1 then --(more checks here) --printout(tutorial.check_passed,pindex) --e.g. "Check passed" elseif #ents == 1 then --(more checks here) @@ -315,17 +347,17 @@ function mod.run_tutorial_menu(pindex, reading_the_header, clicked) else --printout(tutorial.check_failed,pindex) --e.g. "Check failed" end - end - elseif chap > 0 and step > 0 then - --All other steps: Just read the header/detail - if reading_the_header == true then - mod.read_current_header(pindex) - else - mod.read_current_detail(pindex) - end - else - printout({"tutorial.tutorial-error"},pindex)--** - end + end + elseif chap > 0 and step > 0 then + --All other steps: Just read the header/detail + if reading_the_header == true then + mod.read_current_header(pindex) + else + mod.read_current_detail(pindex) + end + else + printout({ "tutorial.tutorial-error" }, pindex) --** + end end -return mod \ No newline at end of file +return mod diff --git a/scripts/ui/low-level/multistate-switch.lua b/scripts/ui/low-level/multistate-switch.lua index fc166851..2c33b18b 100644 --- a/scripts/ui/low-level/multistate-switch.lua +++ b/scripts/ui/low-level/multistate-switch.lua @@ -48,9 +48,9 @@ local cur_label = switch.current(e) Note that this abstraction does not care whether the entity is really a factorio entity. Tables work, for example. ]] -local circular_list = require('scripts.ds.circular-options-list') -local F = require('scripts.field-ref') -- for self-tests -local methods = require('scripts.methods') +local circular_list = require("scripts.ds.circular-options-list") +local F = require("scripts.field-ref") -- for self-tests +local methods = require("scripts.methods") local mod = {} @@ -105,7 +105,7 @@ function multistate_methods:current(entity) return generic_movement(self, entity, circular_list.current, false) end -local linker = methods.link('multistate-switch', multistate_methods) +local linker = methods.link("multistate-switch", multistate_methods) --- @param opts MultistateSwitchOptions --- @returns MultistateSwitch @@ -125,11 +125,8 @@ function mod.create(opts) { { false, circular_list.ANY }, { label = opts.off_label } }, } - for i = 1, #opts.choices do - table.insert( - choices, - { { true, opts.choices[i][1], }, { label = opts.choices[i][2] } } - ) + for i = 1, #opts.choices do + table.insert(choices, { { true, opts.choices[i][1] }, { label = opts.choices[i][2] } }) end local choice_list = circular_list.kv_list(choices, circular_list.tuples) @@ -147,7 +144,7 @@ local test_switch = mod.create({ choices = { { 0, "is 0" }, { 1, "is 1" }, - { 2, "is 2" } + { 2, "is 2" }, }, }) diff --git a/scripts/warnings.lua b/scripts/warnings.lua index bb57d4f4..95215da1 100644 --- a/scripts/warnings.lua +++ b/scripts/warnings.lua @@ -11,12 +11,24 @@ function mod.read_warnings_slot(pindex) elseif players[pindex].warnings.sector == 2 then warnings = players[pindex].warnings.medium.warnings elseif players[pindex].warnings.sector == 3 then - warnings= players[pindex].warnings.long.warnings + warnings = players[pindex].warnings.long.warnings end - if players[pindex].warnings.category <= #warnings and players[pindex].warnings.index <= #warnings[players[pindex].warnings.category].ents then + if + players[pindex].warnings.category <= #warnings + and players[pindex].warnings.index <= #warnings[players[pindex].warnings.category].ents + then local ent = warnings[players[pindex].warnings.category].ents[players[pindex].warnings.index] if ent ~= nil and ent.valid then - printout(ent.name .. " has " .. warnings[players[pindex].warnings.category].name .. " at " .. math.floor(ent.position.x) .. ", " .. math.floor(ent.position.y), pindex) + printout( + ent.name + .. " has " + .. warnings[players[pindex].warnings.category].name + .. " at " + .. math.floor(ent.position.x) + .. ", " + .. math.floor(ent.position.y), + pindex + ) else printout("Blank", pindex) end @@ -28,8 +40,8 @@ end --Warnings menu: Creates a structured data network to track production systems. function mod.generate_production_network(pindex) local surf = game.get_player(pindex).surface - local connectors = surf.find_entities_filtered{type="inserter"} - local sources = surf.find_entities_filtered{type = "mining-drill"} + local connectors = surf.find_entities_filtered({ type = "inserter" }) + local sources = surf.find_entities_filtered({ type = "mining-drill" }) local hash = {} local lines = {} local function explore_source(source) @@ -38,9 +50,9 @@ function mod.generate_production_network(pindex) production_line = math.huge, inputs = {}, outputs = {}, - ent = source + ent = source, } - local target = surf.find_entities_filtered{position = source.drop_position, type = production_types}[1] + local target = surf.find_entities_filtered({ position = source.drop_position, type = production_types })[1] if target ~= nil then if target.type == "mining-drill" then table.insert(hash[source.unit_number].outputs, target.unit_number) @@ -52,22 +64,21 @@ function mod.generate_production_network(pindex) table.insert(lines[new_line], source.unit_number) elseif target.type == "transport-belt" then if hash[target.unit_number] == nil then - local belts = fa_belts.get_connected_belts(target) for i, belt in pairs(belts.hash) do - hash[i] = {link = target.unit_number} + hash[i] = { link = target.unit_number } end - local new_line = table.maxn(lines)+1 + local new_line = table.maxn(lines) + 1 hash[target.unit_number] = { production_line = new_line, - inputs = {source.unit_number}, + inputs = { source.unit_number }, outputs = {}, - ent = target + ent = target, } hash[source.unit_number].production_line = new_line - lines[new_line] = {source.unit_number, target.unit_number} + lines[new_line] = { source.unit_number, target.unit_number } else if hash[target.unit_number].link ~= nil then hash[target.unit_number].ent = target @@ -82,15 +93,15 @@ function mod.generate_production_network(pindex) end else if hash[target.unit_number] == nil then - local new_line = table.maxn(lines)+1 + local new_line = table.maxn(lines) + 1 hash[target.unit_number] = { production_line = new_line, - inputs = {source.unit_number}, + inputs = { source.unit_number }, outputs = {}, - ent = target + ent = target, } hash[source.unit_number].production_line = new_line - lines[new_line] = {source.unit_number, target.unit_number} + lines[new_line] = { source.unit_number, target.unit_number } else table.insert(hash[target.unit_number].inputs, source.unit_number) table.insert(hash[source.unit_number].outputs, target.unit_number) @@ -101,10 +112,10 @@ function mod.generate_production_network(pindex) else local new_line = table.maxn(lines) + 1 hash[source.unit_number].production_line = new_line - lines[new_line] = {source.unit_number} + lines[new_line] = { source.unit_number } end end - end + end for i, source in pairs(sources) do explore_source(source) end @@ -115,44 +126,38 @@ function mod.generate_production_network(pindex) production_line = math.huge, inputs = {}, outputs = {}, - ent = connector + ent = connector, } - local drop_target = surf.find_entities_filtered{position = connector.drop_position, type = production_types}[1] - local pickup_target = surf.find_entities_filtered{position = connector.pickup_position, type = production_types}[1] + local drop_target = + surf.find_entities_filtered({ position = connector.drop_position, type = production_types })[1] + local pickup_target = + surf.find_entities_filtered({ position = connector.pickup_position, type = production_types })[1] if drop_target ~= nil then if drop_target.type == "inserter" then explore_connector(drop_target) local check = true for i, v in pairs(hash[drop_target.unit_number].inputs) do - if v == connector.unit_number then - check = false - end - end - if check then - table.insert(hash[drop_target.unit_number].inputs, connector.unit_number) + if v == connector.unit_number then check = false end end + if check then table.insert(hash[drop_target.unit_number].inputs, connector.unit_number) end local check = true for i, v in pairs(hash[connector.unit_number].outputs) do - if v == drop_target.unit_number then - check = false - end - end - if check then - table.insert(hash[connector.unit_number].outputs, drop_target.unit_number) + if v == drop_target.unit_number then check = false end end + if check then table.insert(hash[connector.unit_number].outputs, drop_target.unit_number) end elseif drop_target.type == "transport-belt" then if hash[drop_target.unit_number] == nil then local belts = fa_belts.get_connected_belts(drop_target) for i, belt in pairs(belts.hash) do - hash[i] = {link = drop_target.unit_number} + hash[i] = { link = drop_target.unit_number } end hash[drop_target.unit_number] = { production_line = math.huge, - inputs = {connector.unit_number}, + inputs = { connector.unit_number }, outputs = {}, - ent = drop_target + ent = drop_target, } table.insert(hash[connector.unit_number].outputs, drop_target.unit_number) else @@ -169,7 +174,7 @@ function mod.generate_production_network(pindex) production_line = math.huge, inputs = {}, outputs = {}, - ent = drop_target + ent = drop_target, } end table.insert(hash[drop_target.unit_number].inputs, connector.unit_number) @@ -182,38 +187,28 @@ function mod.generate_production_network(pindex) explore_connector(pickup_target) local check = true for i, v in pairs(hash[pickup_target.unit_number].outputs) do - if v == connector.unit_number then - check = false - end - end - if check then - table.insert(hash[pickup_target.unit_number].outputs, connector.unit_number) + if v == connector.unit_number then check = false end end + if check then table.insert(hash[pickup_target.unit_number].outputs, connector.unit_number) end local check = true for i, v in pairs(hash[connector.unit_number].inputs) do - if v == pickup_target.unit_number then - check = false - end + if v == pickup_target.unit_number then check = false end end - if check then - table.insert(hash[connector.unit_number].inputs, pickup_target.unit_number) - end - + if check then table.insert(hash[connector.unit_number].inputs, pickup_target.unit_number) end elseif pickup_target.type == "transport-belt" then if hash[pickup_target.unit_number] == nil then local belts = fa_belts.get_connected_belts(pickup_target) for i, belt in pairs(belts.hash) do - hash[i] = {link = pickup_target.unit_number} + hash[i] = { link = pickup_target.unit_number } end hash[pickup_target.unit_number] = { production_line = math.huge, inputs = {}, - outputs = {connector.unit_number}, - ent = pickup_target + outputs = { connector.unit_number }, + ent = pickup_target, } table.insert(hash[connector.unit_number].outputs, pickup_target.unit_number) - else if hash[pickup_target.unit_number].link ~= nil then hash[pickup_target.unit_number].ent = pickup_target @@ -228,27 +223,22 @@ function mod.generate_production_network(pindex) production_line = math.huge, inputs = {}, outputs = {}, - ent = pickup_target + ent = pickup_target, } end table.insert(hash[pickup_target.unit_number].outputs, connector.unit_number) table.insert(hash[connector.unit_number].inputs, pickup_target.unit_number) - end end - local choices = {hash[connector.unit_number]} - if drop_target ~= nil then - table.insert(choices, hash[drop_target.unit_number]) - end - if pickup_target ~= nil then - table.insert(choices, hash[pickup_target.unit_number]) - end + local choices = { hash[connector.unit_number] } + if drop_target ~= nil then table.insert(choices, hash[drop_target.unit_number]) end + if pickup_target ~= nil then table.insert(choices, hash[pickup_target.unit_number]) end local line_choices = {} for i, choice in pairs(choices) do table.insert(line_choices, choice.production_line) end - table.insert(line_choices, table.maxn(lines)+1) + table.insert(line_choices, table.maxn(lines) + 1) local new_line = math.min(unpack(line_choices)) for i, choice in pairs(choices) do if choice.production_line ~= new_line then @@ -262,9 +252,7 @@ function mod.generate_production_network(pindex) lines[old_line] = nil else choice.production_line = new_line - if lines[new_line] == nil then - lines[new_line] = {} - end + if lines[new_line] == nil then lines[new_line] = {} end table.insert(lines[new_line], choice.ent.unit_number) end end @@ -276,32 +264,32 @@ function mod.generate_production_network(pindex) explore_connector(connector) end --- print(table_size(lines)) --- print(table_size(hash)) + -- print(table_size(lines)) + -- print(table_size(hash)) --- local count = 0 --- for i, entry in pairs(hash) do --- if entry.ent ~= nil then --- count = count + 1 --- end --- end --- print(count) - return {hash = hash, lines = lines} + -- local count = 0 + -- for i, entry in pairs(hash) do + -- if entry.ent ~= nil then + -- count = count + 1 + -- end + -- end + -- print(count) + return { hash = hash, lines = lines } end --Warnings menu: scans for problems in the production network it defines and creates the warnings list. -function mod.scan_for_warnings(L,H,pindex) - local prod = mod.generate_production_network(pindex) +function mod.scan_for_warnings(L, H, pindex) + local prod = mod.generate_production_network(pindex) local surf = game.get_player(pindex).surface local pos = players[pindex].cursor_pos - local area = {{pos.x - L, pos.y - H}, {pos.x + L, pos.y + H}} - local ents = surf.find_entities_filtered{area = area, type = entity_types} + local area = { { pos.x - L, pos.y - H }, { pos.x + L, pos.y + H } } + local ents = surf.find_entities_filtered({ area = area, type = entity_types }) local warnings = {} warnings["noFuel"] = {} warnings["noRecipe"] = {} warnings["noInserters"] = {} warnings["noPower"] = {} - warnings ["notConnected"] = {} + warnings["notConnected"] = {} for i, ent in pairs(ents) do if ent.prototype.burner_prototype ~= nil then local fuel_inv = ent.get_fuel_inventory() @@ -318,34 +306,26 @@ function mod.scan_for_warnings(L,H,pindex) local recipe = nil if pcall(function() recipe = ent.get_recipe() - end) then - if recipe == nil and ent.type ~= "furnace" then - table.insert(warnings["noRecipe"], ent) - end + end) then + if recipe == nil and ent.type ~= "furnace" then table.insert(warnings["noRecipe"], ent) end end local check = false for i1, type in pairs(production_types) do - if ent.type == type then - check = true - end - end - if check and prod.hash[ent.unit_number] == nil then - table.insert(warnings["noInserters"], ent) + if ent.type == type then check = true end end + if check and prod.hash[ent.unit_number] == nil then table.insert(warnings["noInserters"], ent) end end local str = "" local result = {} for i, warning in pairs(warnings) do if #warning > 0 then str = str .. i .. " " .. #warning .. ", " - table.insert(result, {name = i, ents = warning}) + table.insert(result, { name = i, ents = warning }) end end - if str == "" then - str = "No warnings displayed " - end + if str == "" then str = "No warnings displayed " end str = string.sub(str, 1, -3) - return {summary = str, warnings = result} + return { summary = str, warnings = result } end -return mod \ No newline at end of file +return mod diff --git a/scripts/worker-robots.lua b/scripts/worker-robots.lua index a9667a81..ba2d234b 100644 --- a/scripts/worker-robots.lua +++ b/scripts/worker-robots.lua @@ -1,7 +1,7 @@ --Here: Functions relating worker robots, roboports, logistic systems, blueprints and other planners, ghosts --Does not include event handlers directly, but can have functions called by them. -local util = require('util') -local fa_utils = require('scripts.fa-utils') +local util = require("util") +local fa_utils = require("scripts.fa-utils") local dirs = defines.direction local MAX_STACK_COUNT = 10 @@ -11,42 +11,42 @@ local mod = {} --Increments: nil, 1, half-stack, 1 stack, n stacks local function increment_logistic_request_min_amount(stack_size, amount_min_in) local amount_min = amount_min_in - + if amount_min == nil or amount_min == 0 then amount_min = 1 elseif amount_min == 1 then - amount_min = math.max(math.floor(stack_size/2),2)-- 0 --> 2 - elseif amount_min <= math.floor(stack_size/2) then + amount_min = math.max(math.floor(stack_size / 2), 2) -- 0 --> 2 + elseif amount_min <= math.floor(stack_size / 2) then amount_min = stack_size elseif amount_min <= stack_size then amount_min = amount_min + stack_size elseif amount_min > stack_size then amount_min = amount_min + stack_size end - + return amount_min end --Increments: nil, 1, half-stack, 1 stack, n stacks local function decrement_logistic_request_min_amount(stack_size, amount_min_in) local amount_min = amount_min_in - + if amount_min == nil or amount_min == 0 then amount_min = nil elseif amount_min == 1 then amount_min = nil - elseif amount_min <= math.floor(stack_size/2) then + elseif amount_min <= math.floor(stack_size / 2) then amount_min = 1 elseif amount_min <= stack_size then - amount_min = math.floor(stack_size/2) + amount_min = math.floor(stack_size / 2) elseif amount_min > stack_size then amount_min = amount_min - stack_size end - + if amount_min == 0 then -- 0 --> "0" amount_min = nil end - + return amount_min end @@ -59,28 +59,28 @@ local function increment_logistic_request_max_amount(stack_size, amount_max_in) amount_max = amount_max + stack_size elseif amount_max >= stack_size then amount_max = amount_max + stack_size - elseif amount_max >= math.floor(stack_size/2) then + elseif amount_max >= math.floor(stack_size / 2) then amount_max = stack_size elseif amount_max >= 1 then - amount_max = math.max(math.floor(stack_size/2),2)-- 0 --> 2 + amount_max = math.max(math.floor(stack_size / 2), 2) -- 0 --> 2 elseif amount_max == nil or amount_max == 0 then amount_max = stack_size end - + return amount_max end --Increments: 0, 1, half-stack, 1 stack, n stacks local function decrement_logistic_request_max_amount(stack_size, amount_max_in) local amount_max = amount_max_in - + if amount_max > stack_size * MAX_STACK_COUNT then - amount_max = stack_size * MAX_STACK_COUNT + amount_max = stack_size * MAX_STACK_COUNT elseif amount_max > stack_size then amount_max = amount_max - stack_size elseif amount_max >= stack_size then - amount_max = math.floor(stack_size/2) - elseif amount_max >= math.floor(stack_size/2) then + amount_max = math.floor(stack_size / 2) + elseif amount_max >= math.floor(stack_size / 2) then amount_max = 1 if stack_size == 1 then -- 0 --> 0 amount_max = 0 @@ -92,7 +92,7 @@ local function decrement_logistic_request_max_amount(stack_size, amount_max_in) elseif amount_max == nil then amount_max = stack_size end - + return amount_max end @@ -100,23 +100,23 @@ local function logistics_request_toggle_personal_logistics(pindex) local p = game.get_player(pindex) p.character_personal_logistic_requests_enabled = not p.character_personal_logistic_requests_enabled if p.character_personal_logistic_requests_enabled then - printout("Resumed personal logistics requests",pindex) + printout("Resumed personal logistics requests", pindex) else - printout("Paused personal logistics requests",pindex) - end + printout("Paused personal logistics requests", pindex) + end end -local function logistics_request_toggle_spidertron_logistics(spidertron,pindex) +local function logistics_request_toggle_spidertron_logistics(spidertron, pindex) spidertron.vehicle_logistic_requests_enabled = not spidertron.vehicle_logistic_requests_enabled if spidertron.vehicle_logistic_requests_enabled then - printout("Resumed spidertron logistics requests",pindex) + printout("Resumed spidertron logistics requests", pindex) else - printout("Paused spidertron logistics requests",pindex) - end + printout("Paused spidertron logistics requests", pindex) + end end --Checks if the request for the given item is fulfilled. You can pass the personal logistics request slot index if you have it already -function mod.is_this_player_logistic_request_fulfilled(item_stack,pindex,slot_index_in) +function mod.is_this_player_logistic_request_fulfilled(item_stack, pindex, slot_index_in) local result = false local slot_index = slot_index_in or nil --todo** @@ -124,26 +124,29 @@ function mod.is_this_player_logistic_request_fulfilled(item_stack,pindex,slot_in end --Returns info string on the current logistics network, or the nearest one, for the current position -function mod.logistics_networks_info(ent,pos_in) +function mod.logistics_networks_info(ent, pos_in) local result = "" local result_code = -1 local network = nil local pos = pos_in - if pos_in == nil then - pos = ent.position - end - --Check if in range of a logistic network + if pos_in == nil then pos = ent.position end + --Check if in range of a logistic network network = ent.surface.find_logistic_network_by_position(pos, ent.force) if network ~= nil and network.valid then result_code = 1 - result = "Logistics connected to a network with " .. (network.all_logistic_robots + network.all_construction_robots) .. " robots" + result = "Logistics connected to a network with " + .. (network.all_logistic_robots + network.all_construction_robots) + .. " robots" else --If not, report nearest logistic network network = ent.surface.find_closest_logistic_network_by_position(pos, ent.force) if network ~= nil and network.valid then result_code = 2 local pos_n = network.find_cell_closest_to(pos).owner.position - result = "No logistics connected, nearest network is " .. util.distance(pos,pos_n) .. " tiles " .. fa_utils.direction_lookup(fa_utils.get_direction_biased(pos_n,pos)) + result = "No logistics connected, nearest network is " + .. util.distance(pos, pos_n) + .. " tiles " + .. fa_utils.direction_lookup(fa_utils.get_direction_biased(pos_n, pos)) else result_code = 3 result = "No logistics connected, no logistic networks nearby, " @@ -153,14 +156,14 @@ function mod.logistics_networks_info(ent,pos_in) end --Finds or assigns the logistic request slot for the item -local function get_personal_logistic_slot_index(item_stack,pindex) +local function get_personal_logistic_slot_index(item_stack, pindex) local p = game.get_player(pindex) local slots_nil_counter = 0 local slot_found = false local current_slot = nil local correct_slot_id = nil local slot_id = 0 - + --Find the correct request slot for this item, if any while not slot_found and slots_nil_counter < 250 do slot_id = slot_id + 1 @@ -174,7 +177,7 @@ local function get_personal_logistic_slot_index(item_stack,pindex) --do nothing end end - + --If needed, find the first empty slot and set it as the correct one if not slot_found then slot_id = 0 @@ -189,12 +192,10 @@ local function get_personal_logistic_slot_index(item_stack,pindex) end end end - + --If no correct or empty slots found then return with error (all slots full) - if not slot_found then - return -1 - end - + if not slot_found then return -1 end + return correct_slot_id end @@ -204,230 +205,230 @@ local function count_active_personal_logistic_slots(pindex) --**laterdo count fu local slots_found = 0 local current_slot = nil local slot_id = 0 - - --Find non-empty request slots + + --Find non-empty request slots while slots_nil_counter < 250 do slot_id = slot_id + 1 current_slot = p.get_personal_logistic_slot(slot_id) if current_slot == nil or current_slot.name == nil then slots_nil_counter = slots_nil_counter + 1 - else + else slot_founds = slots_found + 1 end end - + return slots_found end -local function count_active_spidertron_logistic_slots(spidertron,pindex) +local function count_active_spidertron_logistic_slots(spidertron, pindex) local slots_max_count = spidertron.request_slot_count local slots_nil_counter = 0 local slots_found = 0 local current_slot = nil local slot_id = 0 - - --Find non-empty request slots - while slots_nil_counter < slots_max_count do + + --Find non-empty request slots + while slots_nil_counter < slots_max_count do slot_id = slot_id + 1 current_slot = spidertron.get_vehicle_logistic_slot(slot_id) if current_slot == nil or current_slot.name == nil then slots_nil_counter = slots_nil_counter + 1 - else + else slot_founds = slots_found + 1 end end - + return slots_found end -local function player_logistic_request_increment_min(item_stack,pindex) +local function player_logistic_request_increment_min(item_stack, pindex) local p = game.get_player(pindex) local current_slot = nil local correct_slot_id = nil - + --Check if logistics have been researched for i, tech in pairs(game.get_player(pindex).force.technologies) do if tech.name == "logistic-robotics" and not tech.researched then - printout("Error: You need to research logistic robotics to use this feature.",pindex) + printout("Error: You need to research logistic robotics to use this feature.", pindex) return end end - + --Find the correct request slot for this item - local correct_slot_id = get_personal_logistic_slot_index(item_stack,pindex) - + local correct_slot_id = get_personal_logistic_slot_index(item_stack, pindex) + if correct_slot_id == -1 then - printout("Error: No empty slots available for this request",pindex) + printout("Error: No empty slots available for this request", pindex) return false elseif correct_slot_id == nil or correct_slot_id < 1 then - printout("Error: Invalid slot ID",pindex) + printout("Error: Invalid slot ID", pindex) return false end - + --Read the correct slot id value, increment it, set it current_slot = p.get_personal_logistic_slot(correct_slot_id) if current_slot == nil or current_slot.name == nil then --Create a fresh request - local new_slot = {name = item_stack.name, min = 1, max = nil} - p.set_personal_logistic_slot(correct_slot_id,new_slot) + local new_slot = { name = item_stack.name, min = 1, max = nil } + p.set_personal_logistic_slot(correct_slot_id, new_slot) else --Update existing request - current_slot.min = increment_logistic_request_min_amount(item_stack.prototype.stack_size,current_slot.min) + current_slot.min = increment_logistic_request_min_amount(item_stack.prototype.stack_size, current_slot.min) --Force min <= max if current_slot.min ~= nil and current_slot.max ~= nil and current_slot.min > current_slot.max then - printout("Error: Minimum request value cannot exceed maximum",pindex) + printout("Error: Minimum request value cannot exceed maximum", pindex) return end - p.set_personal_logistic_slot(correct_slot_id,current_slot) + p.set_personal_logistic_slot(correct_slot_id, current_slot) end - + --Read new status - mod.player_logistic_request_read(item_stack,pindex,false) + mod.player_logistic_request_read(item_stack, pindex, false) end -local function player_logistic_request_decrement_min(item_stack,pindex) +local function player_logistic_request_decrement_min(item_stack, pindex) local p = game.get_player(pindex) local current_slot = nil local correct_slot_id = nil - + --Check if logistics have been researched for i, tech in pairs(game.get_player(pindex).force.technologies) do if tech.name == "logistic-robotics" and not tech.researched then - printout("Error: You need to research logistic robotics to use this feature.",pindex) + printout("Error: You need to research logistic robotics to use this feature.", pindex) return end end - + --Find the correct request slot for this item - local correct_slot_id = get_personal_logistic_slot_index(item_stack,pindex) - + local correct_slot_id = get_personal_logistic_slot_index(item_stack, pindex) + if correct_slot_id == -1 then - printout("Error: No empty slots available for this request",pindex) + printout("Error: No empty slots available for this request", pindex) return false elseif correct_slot_id == nil or correct_slot_id < 1 then - printout("Error: Invalid slot ID",pindex) + printout("Error: Invalid slot ID", pindex) return false end - + --Read the correct slot id value, decrement it, set it current_slot = p.get_personal_logistic_slot(correct_slot_id) if current_slot == nil or current_slot.name == nil then --Create a fresh request - local new_slot = {name = item_stack.name, min = 0, max = nil} - p.set_personal_logistic_slot(correct_slot_id,new_slot) + local new_slot = { name = item_stack.name, min = 0, max = nil } + p.set_personal_logistic_slot(correct_slot_id, new_slot) else --Update existing request - current_slot.min = decrement_logistic_request_min_amount(item_stack.prototype.stack_size,current_slot.min) + current_slot.min = decrement_logistic_request_min_amount(item_stack.prototype.stack_size, current_slot.min) --Force min <= max if current_slot.min ~= nil and current_slot.max ~= nil and current_slot.min > current_slot.max then - printout("Error: Minimum request value cannot exceed maximum",pindex) + printout("Error: Minimum request value cannot exceed maximum", pindex) return end - p.set_personal_logistic_slot(correct_slot_id,current_slot) + p.set_personal_logistic_slot(correct_slot_id, current_slot) end - + --Read new status - mod.player_logistic_request_read(item_stack,pindex) + mod.player_logistic_request_read(item_stack, pindex) end -local function player_logistic_request_increment_max(item_stack,pindex) +local function player_logistic_request_increment_max(item_stack, pindex) local p = game.get_player(pindex) local current_slot = nil local correct_slot_id = nil - + --Check if logistics have been researched for i, tech in pairs(game.get_player(pindex).force.technologies) do if tech.name == "logistic-robotics" and not tech.researched then - printout("Error: You need to research logistic robotics to use this feature.",pindex) + printout("Error: You need to research logistic robotics to use this feature.", pindex) return end end - + --Find the correct request slot for this item - local correct_slot_id = get_personal_logistic_slot_index(item_stack,pindex) - + local correct_slot_id = get_personal_logistic_slot_index(item_stack, pindex) + if correct_slot_id == -1 then - printout("Error: No empty slots available for this request",pindex) + printout("Error: No empty slots available for this request", pindex) return false elseif correct_slot_id == nil or correct_slot_id < 1 then - printout("Error: Invalid slot ID",pindex) + printout("Error: Invalid slot ID", pindex) return false end - + --Read the correct slot id value, decrement it, set it current_slot = p.get_personal_logistic_slot(correct_slot_id) if current_slot == nil or current_slot.name == nil then --Create a fresh request - local new_slot = {name = item_stack.name, min = 0, max = MAX_STACK_COUNT * item_stack.prototype.stack_size} - p.set_personal_logistic_slot(correct_slot_id,new_slot) + local new_slot = { name = item_stack.name, min = 0, max = MAX_STACK_COUNT * item_stack.prototype.stack_size } + p.set_personal_logistic_slot(correct_slot_id, new_slot) else --Update existing request - current_slot.max = increment_logistic_request_max_amount(item_stack.prototype.stack_size,current_slot.max) + current_slot.max = increment_logistic_request_max_amount(item_stack.prototype.stack_size, current_slot.max) --Force min <= max if current_slot.min ~= nil and current_slot.max ~= nil and current_slot.min > current_slot.max then - printout("Error: Minimum request value cannot exceed maximum",pindex) + printout("Error: Minimum request value cannot exceed maximum", pindex) return end - p.set_personal_logistic_slot(correct_slot_id,current_slot) + p.set_personal_logistic_slot(correct_slot_id, current_slot) end - + --Read new status - mod.player_logistic_request_read(item_stack,pindex) + mod.player_logistic_request_read(item_stack, pindex) end -local function player_logistic_request_decrement_max(item_stack,pindex) +local function player_logistic_request_decrement_max(item_stack, pindex) local p = game.get_player(pindex) local current_slot = nil local correct_slot_id = nil - + --Check if logistics have been researched for i, tech in pairs(game.get_player(pindex).force.technologies) do if tech.name == "logistic-robotics" and not tech.researched then - printout("Error: You need to research logistic robotics to use this feature.",pindex) + printout("Error: You need to research logistic robotics to use this feature.", pindex) return end end - + --Find the correct request slot for this item - local correct_slot_id = get_personal_logistic_slot_index(item_stack,pindex) - + local correct_slot_id = get_personal_logistic_slot_index(item_stack, pindex) + if correct_slot_id == -1 then - printout("Error: No empty slots available for this request",pindex) + printout("Error: No empty slots available for this request", pindex) return false elseif correct_slot_id == nil or correct_slot_id < 1 then - printout("Error: Invalid slot ID",pindex) + printout("Error: Invalid slot ID", pindex) return false end - + --Read the correct slot id value, increment it, set it current_slot = p.get_personal_logistic_slot(correct_slot_id) if current_slot == nil or current_slot.name == nil then --Create a fresh request - local new_slot = {name = item_stack.name, min = 0, max = MAX_STACK_COUNT * item_stack.prototype.stack_size} - p.set_personal_logistic_slot(correct_slot_id,new_slot) + local new_slot = { name = item_stack.name, min = 0, max = MAX_STACK_COUNT * item_stack.prototype.stack_size } + p.set_personal_logistic_slot(correct_slot_id, new_slot) else --Update existing request - current_slot.max = decrement_logistic_request_max_amount(item_stack.prototype.stack_size,current_slot.max) + current_slot.max = decrement_logistic_request_max_amount(item_stack.prototype.stack_size, current_slot.max) --Force min <= max if current_slot.min ~= nil and current_slot.max ~= nil and current_slot.min > current_slot.max then - printout("Error: Minimum request value cannot exceed maximum",pindex) + printout("Error: Minimum request value cannot exceed maximum", pindex) return end - p.set_personal_logistic_slot(correct_slot_id,current_slot) + p.set_personal_logistic_slot(correct_slot_id, current_slot) end - + --Read new status - mod.player_logistic_request_read(item_stack,pindex,false) + mod.player_logistic_request_read(item_stack, pindex, false) end ---Finds or assigns the logistic request slot for the item, for chests or vehicles -local function get_entity_logistic_slot_index(item_stack,chest) +--Finds or assigns the logistic request slot for the item, for chests or vehicles +local function get_entity_logistic_slot_index(item_stack, chest) local slots_max_count = chest.request_slot_count local slot_found = false local current_slot = nil local correct_slot_id = nil local slot_id = 0 - + --Find the correct request slot for this item, if any while not slot_found and slot_id < slots_max_count do slot_id = slot_id + 1 @@ -441,7 +442,7 @@ local function get_entity_logistic_slot_index(item_stack,chest) --do nothing end end - + --If needed, find the first empty slot and set it as the correct one if not slot_found then slot_id = 0 @@ -456,278 +457,280 @@ local function get_entity_logistic_slot_index(item_stack,chest) end end end - + --If no correct or empty slots found then return with error (all slots full) - if not slot_found then - return -1 - end - + if not slot_found then return -1 end + return correct_slot_id end --Increments min value -local function chest_logistic_request_increment_min(item_stack,chest,pindex) +local function chest_logistic_request_increment_min(item_stack, chest, pindex) local current_slot = nil local correct_slot_id = nil - + --Check if logistics have been researched for i, tech in pairs(game.get_player(pindex).force.technologies) do if tech.name == "logistic-system" and not tech.researched then - printout("Error: You need to research logistic system, with utility science, to use this feature.",pindex) + printout("Error: You need to research logistic system, with utility science, to use this feature.", pindex) return end end - + --Find the correct request slot for this item - local correct_slot_id = get_entity_logistic_slot_index(item_stack,chest) - + local correct_slot_id = get_entity_logistic_slot_index(item_stack, chest) + if correct_slot_id == -1 then - printout("Error: No empty slots available for this request",pindex) + printout("Error: No empty slots available for this request", pindex) return false elseif correct_slot_id == nil or correct_slot_id < 1 then - printout("Error: Invalid slot ID",pindex) + printout("Error: Invalid slot ID", pindex) return false end - + --Read the correct slot id value, increment it, set it current_slot = chest.get_request_slot(correct_slot_id) if current_slot == nil or current_slot.name == nil then --Create a fresh request - local new_slot = {name = item_stack.name, count = item_stack.prototype.stack_size} + local new_slot = { name = item_stack.name, count = item_stack.prototype.stack_size } chest.set_request_slot(new_slot, correct_slot_id) else --Update existing request - current_slot.count = increment_logistic_request_min_amount(item_stack.prototype.stack_size,current_slot.count) - chest.set_request_slot(current_slot,correct_slot_id) + current_slot.count = increment_logistic_request_min_amount(item_stack.prototype.stack_size, current_slot.count) + chest.set_request_slot(current_slot, correct_slot_id) end - + --Read new status - mod.chest_logistic_request_read(item_stack,chest,pindex) + mod.chest_logistic_request_read(item_stack, chest, pindex) end --Decrements min value -local function chest_logistic_request_decrement_min(item_stack,chest, pindex) +local function chest_logistic_request_decrement_min(item_stack, chest, pindex) local current_slot = nil local correct_slot_id = nil - + --Check if logistics have been researched for i, tech in pairs(game.get_player(pindex).force.technologies) do if tech.name == "logistic-system" and not tech.researched then - printout("Error: You need to research logistic system, with utility science, to use this feature.",pindex) + printout("Error: You need to research logistic system, with utility science, to use this feature.", pindex) return end end - + --Find the correct request slot for this item - local correct_slot_id = get_entity_logistic_slot_index(item_stack,chest) - + local correct_slot_id = get_entity_logistic_slot_index(item_stack, chest) + if correct_slot_id == -1 then - printout("Error: No empty slots available for this request",pindex) + printout("Error: No empty slots available for this request", pindex) return false elseif correct_slot_id == nil or correct_slot_id < 1 then - printout("Error: Invalid slot ID",pindex) + printout("Error: Invalid slot ID", pindex) return false end - + --Read the correct slot id value, decrement it, set it current_slot = chest.get_request_slot(correct_slot_id) if current_slot == nil or current_slot.name == nil then --Create a fresh request - local new_slot = {name = item_stack.name, count = item_stack.prototype.stack_size} + local new_slot = { name = item_stack.name, count = item_stack.prototype.stack_size } chest.set_request_slot(new_slot, correct_slot_id) else --Update existing request - current_slot.count = decrement_logistic_request_min_amount(item_stack.prototype.stack_size,current_slot.count) - if current_slot.count == nil or current_slot.count == 0 then + current_slot.count = decrement_logistic_request_min_amount(item_stack.prototype.stack_size, current_slot.count) + if current_slot.count == nil or current_slot.count == 0 then chest.clear_request_slot(correct_slot_id) else - chest.set_request_slot(current_slot,correct_slot_id) + chest.set_request_slot(current_slot, correct_slot_id) end end - + --Read new status - mod.chest_logistic_request_read(item_stack,chest,pindex) + mod.chest_logistic_request_read(item_stack, chest, pindex) end -local function spidertron_logistic_request_increment_min(item_stack,spidertron,pindex) +local function spidertron_logistic_request_increment_min(item_stack, spidertron, pindex) local current_slot = nil local correct_slot_id = nil - + --Check if logistics have been researched for i, tech in pairs(game.get_player(pindex).force.technologies) do if tech.name == "logistic-robotics" and not tech.researched then - printout("Error: You need to research logistic robotics to use this feature.",pindex) + printout("Error: You need to research logistic robotics to use this feature.", pindex) return end end - + --Find the correct request slot for this item - local correct_slot_id = get_entity_logistic_slot_index(item_stack,spidertron) - + local correct_slot_id = get_entity_logistic_slot_index(item_stack, spidertron) + if correct_slot_id == -1 then - printout("Error: No empty slots available for this request",pindex) + printout("Error: No empty slots available for this request", pindex) return false elseif correct_slot_id == nil or correct_slot_id < 1 then - printout("Error: Invalid slot ID",pindex) + printout("Error: Invalid slot ID", pindex) return false end - + --Read the correct slot id value, increment it, set it current_slot = spidertron.get_vehicle_logistic_slot(correct_slot_id) if current_slot == nil or current_slot.name == nil then --Create a fresh request - local new_slot = {name = item_stack.name, min = 1, max = nil} - spidertron.set_vehicle_logistic_slot(correct_slot_id,new_slot) + local new_slot = { name = item_stack.name, min = 1, max = nil } + spidertron.set_vehicle_logistic_slot(correct_slot_id, new_slot) else --Update existing request - current_slot.min = increment_logistic_request_min_amount(item_stack.prototype.stack_size,current_slot.min) + current_slot.min = increment_logistic_request_min_amount(item_stack.prototype.stack_size, current_slot.min) --Force min <= max if current_slot.min ~= nil and current_slot.max ~= nil and current_slot.min > current_slot.max then - printout("Error: Minimum request value cannot exceed maximum",pindex) + printout("Error: Minimum request value cannot exceed maximum", pindex) return end - spidertron.set_vehicle_logistic_slot(correct_slot_id,current_slot) + spidertron.set_vehicle_logistic_slot(correct_slot_id, current_slot) end - + --Read new status - mod.spidertron_logistic_request_read(item_stack,spidertron,pindex,false) + mod.spidertron_logistic_request_read(item_stack, spidertron, pindex, false) end -local function spidertron_logistic_request_decrement_min(item_stack,spidertron,pindex) +local function spidertron_logistic_request_decrement_min(item_stack, spidertron, pindex) local current_slot = nil local correct_slot_id = nil - + --Check if logistics have been researched for i, tech in pairs(game.get_player(pindex).force.technologies) do if tech.name == "logistic-robotics" and not tech.researched then - printout("Error: You need to research logistic robotics to use this feature.",pindex) + printout("Error: You need to research logistic robotics to use this feature.", pindex) return end end - + --Find the correct request slot for this item - local correct_slot_id = get_entity_logistic_slot_index(item_stack,spidertron) - + local correct_slot_id = get_entity_logistic_slot_index(item_stack, spidertron) + if correct_slot_id == -1 then - printout("Error: No empty slots available for this request",pindex) + printout("Error: No empty slots available for this request", pindex) return false elseif correct_slot_id == nil or correct_slot_id < 1 then - printout("Error: Invalid slot ID",pindex) + printout("Error: Invalid slot ID", pindex) return false end - + --Read the correct slot id value, decrement it, set it current_slot = spidertron.get_vehicle_logistic_slot(correct_slot_id) if current_slot == nil or current_slot.name == nil then --Create a fresh request - local new_slot = {name = item_stack.name, min = 0, max = nil} - spidertron.set_vehicle_logistic_slot(correct_slot_id,new_slot) + local new_slot = { name = item_stack.name, min = 0, max = nil } + spidertron.set_vehicle_logistic_slot(correct_slot_id, new_slot) else --Update existing request - current_slot.min = decrement_logistic_request_min_amount(item_stack.prototype.stack_size,current_slot.min) + current_slot.min = decrement_logistic_request_min_amount(item_stack.prototype.stack_size, current_slot.min) --Force min <= max if current_slot.min ~= nil and current_slot.max ~= nil and current_slot.min > current_slot.max then - printout("Error: Minimum request value cannot exceed maximum",pindex) + printout("Error: Minimum request value cannot exceed maximum", pindex) return end - spidertron.set_vehicle_logistic_slot(correct_slot_id,current_slot) + spidertron.set_vehicle_logistic_slot(correct_slot_id, current_slot) end - + --Read new status - mod.spidertron_logistic_request_read(item_stack,spidertron,pindex,false) + mod.spidertron_logistic_request_read(item_stack, spidertron, pindex, false) end -local function spidertron_logistic_request_increment_max(item_stack,spidertron,pindex) +local function spidertron_logistic_request_increment_max(item_stack, spidertron, pindex) local current_slot = nil local correct_slot_id = nil - + --Check if logistics have been researched for i, tech in pairs(game.get_player(pindex).force.technologies) do if tech.name == "logistic-robotics" and not tech.researched then - printout("Error: You need to research logistic robotics to use this feature.",pindex) + printout("Error: You need to research logistic robotics to use this feature.", pindex) return end end - + --Find the correct request slot for this item - local correct_slot_id = get_entity_logistic_slot_index(item_stack,spidertron) - + local correct_slot_id = get_entity_logistic_slot_index(item_stack, spidertron) + if correct_slot_id == -1 then - printout("Error: No empty slots available for this request",pindex) + printout("Error: No empty slots available for this request", pindex) return false elseif correct_slot_id == nil or correct_slot_id < 1 then - printout("Error: Invalid slot ID",pindex) + printout("Error: Invalid slot ID", pindex) return false end - + --Read the correct slot id value, decrement it, set it current_slot = spidertron.get_vehicle_logistic_slot(correct_slot_id) if current_slot == nil or current_slot.name == nil then --Create a fresh request - local new_slot = {name = item_stack.name, min = 0, max = MAX_STACK_COUNT * item_stack.prototype.stack_size} - spidertron.set_vehicle_logistic_slot(correct_slot_id,new_slot) + local new_slot = { name = item_stack.name, min = 0, max = MAX_STACK_COUNT * item_stack.prototype.stack_size } + spidertron.set_vehicle_logistic_slot(correct_slot_id, new_slot) else --Update existing request - current_slot.max = increment_logistic_request_max_amount(item_stack.prototype.stack_size,current_slot.max) + current_slot.max = increment_logistic_request_max_amount(item_stack.prototype.stack_size, current_slot.max) --Force min <= max if current_slot.min ~= nil and current_slot.max ~= nil and current_slot.min > current_slot.max then - printout("Error: Minimum request value cannot exceed maximum",pindex) + printout("Error: Minimum request value cannot exceed maximum", pindex) return end - spidertron.set_vehicle_logistic_slot(correct_slot_id,current_slot) + spidertron.set_vehicle_logistic_slot(correct_slot_id, current_slot) end - + --Read new status - mod.spidertron_logistic_request_read(item_stack,spidertron,pindex,false) + mod.spidertron_logistic_request_read(item_stack, spidertron, pindex, false) end -local function spidertron_logistic_request_decrement_max(item_stack,spidertron,pindex) +local function spidertron_logistic_request_decrement_max(item_stack, spidertron, pindex) local current_slot = nil local correct_slot_id = nil - + --Check if logistics have been researched for i, tech in pairs(game.get_player(pindex).force.technologies) do if tech.name == "logistic-robotics" and not tech.researched then - printout("Error: You need to research logistic robotics to use this feature.",pindex) + printout("Error: You need to research logistic robotics to use this feature.", pindex) return end end - + --Find the correct request slot for this item - local correct_slot_id = get_entity_logistic_slot_index(item_stack,spidertron) - + local correct_slot_id = get_entity_logistic_slot_index(item_stack, spidertron) + if correct_slot_id == -1 then - printout("Error: No empty slots available for this request",pindex) + printout("Error: No empty slots available for this request", pindex) return false elseif correct_slot_id == nil or correct_slot_id < 1 then - printout("Error: Invalid slot ID",pindex) + printout("Error: Invalid slot ID", pindex) return false end - + --Read the correct slot id value, increment it, set it current_slot = spidertron.get_vehicle_logistic_slot(correct_slot_id) if current_slot == nil or current_slot.name == nil then --Create a fresh request - local new_slot = {name = item_stack.name, min = 0, max = MAX_STACK_COUNT * item_stack.prototype.stack_size} - spidertron.set_vehicle_logistic_slot(correct_slot_id,new_slot) + local new_slot = { name = item_stack.name, min = 0, max = MAX_STACK_COUNT * item_stack.prototype.stack_size } + spidertron.set_vehicle_logistic_slot(correct_slot_id, new_slot) else --Update existing request - current_slot.max = decrement_logistic_request_max_amount(item_stack.prototype.stack_size,current_slot.max) + current_slot.max = decrement_logistic_request_max_amount(item_stack.prototype.stack_size, current_slot.max) --Force min <= max if current_slot.min ~= nil and current_slot.max ~= nil and current_slot.min > current_slot.max then - printout("Error: Minimum request value cannot exceed maximum",pindex) + printout("Error: Minimum request value cannot exceed maximum", pindex) return end - spidertron.set_vehicle_logistic_slot(correct_slot_id,current_slot) + spidertron.set_vehicle_logistic_slot(correct_slot_id, current_slot) end - + --Read new status - mod.spidertron_logistic_request_read(item_stack,spidertron,pindex,false) + mod.spidertron_logistic_request_read(item_stack, spidertron, pindex, false) end ---Calls the appropriate function after a keypress for logistic info +--Calls the appropriate function after a keypress for logistic info function mod.logistics_info_key_handler(pindex) - if players[pindex].in_menu == false or players[pindex].menu == "inventory" or players[pindex].menu == "player_trash" then + if + players[pindex].in_menu == false + or players[pindex].menu == "inventory" + or players[pindex].menu == "player_trash" + then --Personal logistics local stack = game.get_player(pindex).cursor_stack local stack_inv = game.get_player(pindex).get_main_inventory()[players[pindex].inventory.index] @@ -735,31 +738,30 @@ function mod.logistics_info_key_handler(pindex) --Check item in hand or item in inventory if stack and stack.valid_for_read and stack.valid then --Item in hand - mod.player_logistic_request_read(stack,pindex,true) + mod.player_logistic_request_read(stack, pindex, true) elseif players[pindex].menu == "inventory" and stack_inv and stack_inv.valid_for_read and stack_inv.valid then --Item in inv - mod.player_logistic_request_read(stack_inv,pindex,true) + mod.player_logistic_request_read(stack_inv, pindex, true) elseif players[pindex].menu == "player_trash" and stack_tra and stack_tra.valid_for_read and stack_tra.valid then - stack_tra = game.get_player(pindex).get_inventory(defines.inventory.character_trash)[players[pindex].inventory.index] - mod.player_logistic_request_read(stack_tra,pindex,true) + stack_tra = + game.get_player(pindex).get_inventory(defines.inventory.character_trash)[players[pindex].inventory.index] + mod.player_logistic_request_read(stack_tra, pindex, true) else --Logistic chest in front local ent = get_selected_ent(pindex) if mod.can_make_logistic_requests(ent) then - mod.read_entity_requests_summary(ent,pindex) + mod.read_entity_requests_summary(ent, pindex) return elseif mod.can_set_logistic_filter(ent) then local filter = ent.storage_filter local result = "Nothing" - if filter ~= nil then - result = filter.name - end - printout(result .. " set as logistic storage filter",pindex) + if filter ~= nil then result = filter.name end + printout(result .. " set as logistic storage filter", pindex) return end --Empty hand and empty inventory slot local result = mod.player_logistic_requests_summary_info(pindex) - printout(result,pindex) + printout(result, pindex) end elseif players[pindex].menu == "building" and mod.can_make_logistic_requests(game.get_player(pindex).opened) then --Chest logistics @@ -775,7 +777,7 @@ function mod.logistics_info_key_handler(pindex) mod.chest_logistic_request_read(stack_inv, chest, pindex) else --Empty hand, empty inventory slot - mod.read_entity_requests_summary(chest,pindex) + mod.read_entity_requests_summary(chest, pindex) end elseif players[pindex].menu == "vehicle" and mod.can_make_logistic_requests(game.get_player(pindex).opened) then --spidertron logistics @@ -792,19 +794,17 @@ function mod.logistics_info_key_handler(pindex) mod.spidertron_logistic_request_read(stack_inv, spidertron, pindex, true) else --Empty hand, empty inventory slot - mod.read_entity_requests_summary(spidertron,pindex) -end + mod.read_entity_requests_summary(spidertron, pindex) + end elseif players[pindex].menu == "building" and mod.can_set_logistic_filter(game.get_player(pindex).opened) then local filter = game.get_player(pindex).opened.storage_filter local result = "Nothing" - if filter ~= nil then - result = filter.name - end - printout(result .. " set as logistic storage filter",pindex) + if filter ~= nil then result = filter.name end + printout(result .. " set as logistic storage filter", pindex) elseif players[pindex].menu == "building" then - printout("Logistic requests not supported for this building",pindex) + printout("Logistic requests not supported for this building", pindex) else - printout("No logistics summary available in this menu",pindex) + printout("No logistics summary available in this menu", pindex) end end @@ -817,13 +817,18 @@ function mod.logistics_request_increment_min_handler(pindex) --Check item in hand or item in inventory if stack ~= nil and stack.valid_for_read and stack.valid then --Item in hand - player_logistic_request_increment_min(stack,pindex) - elseif players[pindex].menu == "inventory" and stack_inv ~= nil and stack_inv.valid_for_read and stack_inv.valid then + player_logistic_request_increment_min(stack, pindex) + elseif + players[pindex].menu == "inventory" + and stack_inv ~= nil + and stack_inv.valid_for_read + and stack_inv.valid + then --Item in inv - player_logistic_request_increment_min(stack_inv,pindex) + player_logistic_request_increment_min(stack_inv, pindex) elseif players[pindex].menu == "player_trash" then --Item in trash - printout("Take this item in hand to change its requests",pindex) + printout("Take this item in hand to change its requests", pindex) else --Empty hand, empty inventory slot --(do nothing) @@ -842,7 +847,7 @@ function mod.logistics_request_increment_min_handler(pindex) chest_logistic_request_increment_min(stack_inv, chest, pindex) else --Empty hand, empty inventory slot - printout("No actions",pindex) + printout("No actions", pindex) end elseif players[pindex].menu == "vehicle" and mod.can_make_logistic_requests(game.get_player(pindex).opened) then --spidertron logistics @@ -859,7 +864,7 @@ function mod.logistics_request_increment_min_handler(pindex) spidertron_logistic_request_increment_min(stack_inv, spidertron, pindex) else --Empty hand, empty inventory slot - printout("No actions",pindex) + printout("No actions", pindex) end elseif players[pindex].menu == "building" and mod.can_set_logistic_filter(game.get_player(pindex).opened) then --Chest logistics @@ -878,10 +883,10 @@ function mod.logistics_request_increment_min_handler(pindex) mod.set_logistic_filter(nil, chest, pindex) end elseif players[pindex].menu == "building" then - printout("Logistic requests not supported for this building",pindex) + printout("Logistic requests not supported for this building", pindex) else --Other menu - printout("No actions",pindex) + printout("No actions", pindex) end end @@ -894,13 +899,18 @@ function mod.logistics_request_decrement_min_handler(pindex) --Check item in hand or item in inventory if stack ~= nil and stack.valid_for_read and stack.valid then --Item in hand - player_logistic_request_decrement_min(stack,pindex) - elseif players[pindex].menu == "inventory" and stack_inv ~= nil and stack_inv.valid_for_read and stack_inv.valid then + player_logistic_request_decrement_min(stack, pindex) + elseif + players[pindex].menu == "inventory" + and stack_inv ~= nil + and stack_inv.valid_for_read + and stack_inv.valid + then --Item in inv - player_logistic_request_decrement_min(stack_inv,pindex) + player_logistic_request_decrement_min(stack_inv, pindex) elseif players[pindex].menu == "player_trash" then --Item in trash - printout("Take this item in hand to change its requests",pindex) + printout("Take this item in hand to change its requests", pindex) else --Empty hand, empty inventory slot --(do nothing) @@ -919,7 +929,7 @@ function mod.logistics_request_decrement_min_handler(pindex) chest_logistic_request_decrement_min(stack_inv, chest, pindex) else --Empty hand, empty inventory slot - printout("No actions",pindex) + printout("No actions", pindex) end elseif players[pindex].menu == "vehicle" and mod.can_make_logistic_requests(game.get_player(pindex).opened) then --spidertron logistics @@ -936,7 +946,7 @@ function mod.logistics_request_decrement_min_handler(pindex) spidertron_logistic_request_decrement_min(stack_inv, spidertron, pindex) else --Empty hand, empty inventory slot - printout("No actions",pindex) + printout("No actions", pindex) end elseif players[pindex].menu == "building" and mod.can_set_logistic_filter(game.get_player(pindex).opened) then --Chest logistics @@ -955,10 +965,10 @@ function mod.logistics_request_decrement_min_handler(pindex) mod.set_logistic_filter(nil, chest, pindex) end elseif players[pindex].menu == "building" then - printout("Logistic requests not supported for this building",pindex) + printout("Logistic requests not supported for this building", pindex) else --Other menu - printout("No actions",pindex) + printout("No actions", pindex) end end @@ -971,13 +981,18 @@ function mod.logistics_request_increment_max_handler(pindex) --Check item in hand or item in inventory if stack ~= nil and stack.valid_for_read and stack.valid then --Item in hand - player_logistic_request_increment_max(stack,pindex) - elseif players[pindex].menu == "inventory" and stack_inv ~= nil and stack_inv.valid_for_read and stack_inv.valid then + player_logistic_request_increment_max(stack, pindex) + elseif + players[pindex].menu == "inventory" + and stack_inv ~= nil + and stack_inv.valid_for_read + and stack_inv.valid + then --Item in inv - player_logistic_request_increment_max(stack_inv,pindex) + player_logistic_request_increment_max(stack_inv, pindex) elseif players[pindex].menu == "player_trash" then --Item in trash - printout("Take this item in hand to change its requests",pindex) + printout("Take this item in hand to change its requests", pindex) else --Empty hand, empty inventory slot --(do nothing) @@ -997,7 +1012,7 @@ function mod.logistics_request_increment_max_handler(pindex) spidertron_logistic_request_increment_max(stack_inv, spidertron, pindex) else --Empty hand, empty inventory slot - printout("No actions",pindex) + printout("No actions", pindex) end else --Other menu @@ -1014,13 +1029,18 @@ function mod.logistics_request_decrement_max_handler(pindex) --Check item in hand or item in inventory if stack ~= nil and stack.valid_for_read and stack.valid then --Item in hand - player_logistic_request_decrement_max(stack,pindex) - elseif players[pindex].menu == "inventory" and stack_inv ~= nil and stack_inv.valid_for_read and stack_inv.valid then + player_logistic_request_decrement_max(stack, pindex) + elseif + players[pindex].menu == "inventory" + and stack_inv ~= nil + and stack_inv.valid_for_read + and stack_inv.valid + then --Item in inv - player_logistic_request_decrement_max(stack_inv,pindex) + player_logistic_request_decrement_max(stack_inv, pindex) elseif players[pindex].menu == "player_trash" then --Item in trash - printout("Take this item in hand to change its requests",pindex) + printout("Take this item in hand to change its requests", pindex) else --Empty hand, empty inventory slot --(do nothing) @@ -1040,7 +1060,7 @@ function mod.logistics_request_decrement_max_handler(pindex) spidertron_logistic_request_decrement_max(stack_inv, spidertron, pindex) else --Empty hand, empty inventory slot - printout("No actions",pindex) + printout("No actions", pindex) end else --Other menu @@ -1062,7 +1082,7 @@ function mod.logistics_request_toggle_handler(pindex) if mod.can_make_logistic_requests(ent) then ent.request_from_buffers = not ent.request_from_buffers else - return + return end if ent.request_from_buffers then printout("Enabled requesting from buffers", pindex) @@ -1079,82 +1099,82 @@ function mod.player_logistic_requests_summary_info(pindex) local current_slot = nil local correct_slot_id = nil local result = "" - + --Check if logistics have been researched for i, tech in pairs(game.get_player(pindex).force.technologies) do if tech.name == "logistic-robotics" and not tech.researched == true then - printout("Logistic requests not available, research required.",pindex) + printout("Logistic requests not available, research required.", pindex) return end end - + --Check if inside any logistic network or not (simpler than logistics network info) local network = p.surface.find_logistic_network_by_position(p.position, p.force) if network == nil or not network.valid then --Check whether in construction range - local nearest, min_dist = fa_utils.find_nearest_roboport(p.surface,p.position,60) + local nearest, min_dist = fa_utils.find_nearest_roboport(p.surface, p.position, 60) if nearest == nil or min_dist > 55 then result = result .. "Not in a network, " else - result = result .. "In construction range of network " .. nearest.backer_name .. ", " + result = result .. "In construction range of network " .. nearest.backer_name .. ", " end else --Definitely within range - local nearest, min_dist = fa_utils.find_nearest_roboport(p.surface,p.position,30) - result = result .. "In logistic range of network " .. nearest.backer_name .. ", " + local nearest, min_dist = fa_utils.find_nearest_roboport(p.surface, p.position, 30) + result = result .. "In logistic range of network " .. nearest.backer_name .. ", " end - + --Check if personal logistics are enabled - if not p.character_personal_logistic_requests_enabled then - result = result .. "Requests paused, " - end - + if not p.character_personal_logistic_requests_enabled then result = result .. "Requests paused, " end + --Count logistics requests result = result .. count_active_personal_logistic_slots(pindex) .. " personal logistic requests set, " return result end --Read the current personal logistics request set for this item -function mod.player_logistic_request_read(item_stack,pindex,additional_checks) +function mod.player_logistic_request_read(item_stack, pindex, additional_checks) local p = game.get_player(pindex) local current_slot = nil local correct_slot_id = nil local result = "" - + --Check if logistics have been researched for i, tech in pairs(game.get_player(pindex).force.technologies) do if tech.name == "logistic-robotics" and not tech.researched then - printout("Logistic requests not available, research required.",pindex) + printout("Logistic requests not available, research required.", pindex) return end end - + if additional_checks then --Check if inside any logistic network or not (simpler than logistics network info) local network = p.surface.find_logistic_network_by_position(p.position, p.force) - if network == nil or not network.valid then - result = result .. "Not in a network, " - end - + if network == nil or not network.valid then result = result .. "Not in a network, " end + --Check if personal logistics are enabled - if not p.character_personal_logistic_requests_enabled then - result = result .. "Requests paused, " - end + if not p.character_personal_logistic_requests_enabled then result = result .. "Requests paused, " end end - + --Find the correct request slot for this item - local correct_slot_id = get_personal_logistic_slot_index(item_stack,pindex) - + local correct_slot_id = get_personal_logistic_slot_index(item_stack, pindex) + if correct_slot_id == nil or correct_slot_id < 1 then - printout(result .. "Error: Invalid slot ID",pindex) - return + printout(result .. "Error: Invalid slot ID", pindex) + return end - + --Read the correct slot id value current_slot = p.get_personal_logistic_slot(correct_slot_id) if current_slot == nil or current_slot.name == nil then --No requests found - printout(result .. "No personal logistic requests set for " .. item_stack.name .. ", use the L key and modifier keys to set requests.",pindex) + printout( + result + .. "No personal logistic requests set for " + .. item_stack.name + .. ", use the L key and modifier keys to set requests.", + pindex + ) return else --Report request counts and inventory counts @@ -1163,53 +1183,73 @@ function mod.player_logistic_request_read(item_stack,pindex,additional_checks) local max_result = "" local inv_result = "" local trash_result = "" - + if current_slot.min ~= nil then - min_result = fa_utils.express_in_stacks(current_slot.min, item_stack.prototype.stack_size, false) .. " minimum and " + min_result = fa_utils.express_in_stacks(current_slot.min, item_stack.prototype.stack_size, false) + .. " minimum and " end - + if current_slot.max ~= nil then - max_result = fa_utils.express_in_stacks(current_slot.max, item_stack.prototype.stack_size, false) .. " maximum " + max_result = fa_utils.express_in_stacks(current_slot.max, item_stack.prototype.stack_size, false) + .. " maximum " end - + local inv_count = p.get_main_inventory().get_item_count(item_stack.name) inv_result = fa_utils.express_in_stacks(inv_count, item_stack.prototype.stack_size, false) .. " in inventory, " - + local trash_count = p.get_inventory(defines.inventory.character_trash).get_item_count(item_stack.name) - trash_result = fa_utils.express_in_stacks(trash_count, item_stack.prototype.stack_size, false) .. " in personal trash, " - - printout(result .. min_result .. max_result .. " requested for " .. item_stack.name .. ", " .. inv_result .. trash_result .. " use the L key and modifier keys to set requests.",pindex) + trash_result = fa_utils.express_in_stacks(trash_count, item_stack.prototype.stack_size, false) + .. " in personal trash, " + + printout( + result + .. min_result + .. max_result + .. " requested for " + .. item_stack.name + .. ", " + .. inv_result + .. trash_result + .. " use the L key and modifier keys to set requests.", + pindex + ) return else --All requests are nil - printout(result .. "No personal logistic requests set for " .. item_stack.name .. ", use the L key and modifier keys to set requests.",pindex) + printout( + result + .. "No personal logistic requests set for " + .. item_stack.name + .. ", use the L key and modifier keys to set requests.", + pindex + ) return end end end --Read the chest's current logistics request set for this item -function mod.chest_logistic_request_read(item_stack,chest,pindex) +function mod.chest_logistic_request_read(item_stack, chest, pindex) local current_slot = nil local correct_slot_id = nil local result = "" - + --Check if logistics have been researched for i, tech in pairs(game.get_player(pindex).force.technologies) do if tech.name == "logistic-system" and not tech.researched then - printout("Error: You need to research logistic system, with utility science, to use this feature.",pindex) + printout("Error: You need to research logistic system, with utility science, to use this feature.", pindex) return end end - + --Find the correct request slot for this item - local correct_slot_id = get_entity_logistic_slot_index(item_stack,chest) - + local correct_slot_id = get_entity_logistic_slot_index(item_stack, chest) + if correct_slot_id == -1 then - printout("Error: No empty slots available for this request",pindex) + printout("Error: No empty slots available for this request", pindex) return false elseif correct_slot_id == nil or correct_slot_id < 1 then - printout("Error: Invalid slot ID",pindex) + printout("Error: Invalid slot ID", pindex) return false end @@ -1217,21 +1257,32 @@ function mod.chest_logistic_request_read(item_stack,chest,pindex) current_slot = chest.get_request_slot(correct_slot_id) if current_slot == nil or current_slot.name == nil then --No requests found - printout("No logistic requests set for " .. item_stack.name .. ", use the 'L' key and modifier keys to set requests.",pindex) + printout( + "No logistic requests set for " .. item_stack.name .. ", use the 'L' key and modifier keys to set requests.", + pindex + ) return else --Report request counts and inventory counts local req_result = "" local inv_result = "" - + if current_slot.count ~= nil then - req_result = fa_utils.express_in_stacks(current_slot.count, item_stack.prototype.stack_size, false) + req_result = fa_utils.express_in_stacks(current_slot.count, item_stack.prototype.stack_size, false) end - + local inv_count = chest.get_output_inventory().get_item_count(item_stack.name) - inv_result = fa_utils.express_in_stacks(inv_count, item_stack.prototype.stack_size, false) - - printout(req_result .. " requested and " .. inv_result .. " supplied for " .. item_stack.name .. ", use the 'L' key and modifier keys to set requests.",pindex) + inv_result = fa_utils.express_in_stacks(inv_count, item_stack.prototype.stack_size, false) + + printout( + req_result + .. " requested and " + .. inv_result + .. " supplied for " + .. item_stack.name + .. ", use the 'L' key and modifier keys to set requests.", + pindex + ) return end end @@ -1239,108 +1290,114 @@ end function mod.send_selected_stack_to_logistic_trash(pindex) local p = game.get_player(pindex) local stack = p.cursor_stack - --Check cursor stack + --Check cursor stack if stack == nil or stack.valid_for_read == false or stack.is_deconstruction_item or stack.is_upgrade_item then stack = p.get_main_inventory()[players[pindex].inventory.index] end --Check inventory stack - if players[pindex].menu ~= "inventory" or stack == nil or stack.valid_for_read == false or stack.is_deconstruction_item or stack.is_upgrade_item then + if + players[pindex].menu ~= "inventory" + or stack == nil + or stack.valid_for_read == false + or stack.is_deconstruction_item + or stack.is_upgrade_item + then return end local trash_inv = p.get_inventory(defines.inventory.character_trash) if trash_inv.can_insert(stack) then local inserted_count = trash_inv.insert(stack) if inserted_count < stack.count then - stack.set_stack({name = stack.name, count = stack.count - inserted_count}) - printout("Partially sent stack to logistic trash",pindex) + stack.set_stack({ name = stack.name, count = stack.count - inserted_count }) + printout("Partially sent stack to logistic trash", pindex) else stack.set_stack(nil) - printout("Sent stack to logistic trash",pindex) + printout("Sent stack to logistic trash", pindex) end end end -function mod.spidertron_logistic_requests_summary_info(spidertron,pindex) +function mod.spidertron_logistic_requests_summary_info(spidertron, pindex) --***todo improve: "y of z personal logistic requests fulfilled, x items in trash, missing items include [3], take an item in hand and press L to check its request status." maybe use logistics_networks_info(ent,pos_in) local p = game.get_player(pindex) local current_slot = nil local correct_slot_id = nil local result = "Spidertron " - + --Check if logistics have been researched for i, tech in pairs(game.get_player(pindex).force.technologies) do if tech.name == "logistic-robotics" and not tech.researched == true then - printout("Logistic requests not available, research required.",pindex) + printout("Logistic requests not available, research required.", pindex) return end end - + --Check if inside any logistic network or not (simpler than logistics network info) local network = p.surface.find_logistic_network_by_position(spidertron.position, p.force) if network == nil or not network.valid then --Check whether in construction range - local nearest, min_dist = fa_utils.find_nearest_roboport(p.surface,spidertron.position,60) + local nearest, min_dist = fa_utils.find_nearest_roboport(p.surface, spidertron.position, 60) if nearest == nil or min_dist > 55 then result = result .. "Not in a network, " else - result = result .. "In construction range of network " .. nearest.backer_name .. ", " + result = result .. "In construction range of network " .. nearest.backer_name .. ", " end else --Definitely within range - local nearest, min_dist = fa_utils.find_nearest_roboport(p.surface,spidertron.position,30) - result = result .. "In logistic range of network " .. nearest.backer_name .. ", " + local nearest, min_dist = fa_utils.find_nearest_roboport(p.surface, spidertron.position, 30) + result = result .. "In logistic range of network " .. nearest.backer_name .. ", " end - + --Check if personal logistics are enabled - if not spidertron.vehicle_logistic_requests_enabled then - result = result .. "Requests paused, " - end - + if not spidertron.vehicle_logistic_requests_enabled then result = result .. "Requests paused, " end + --Count logistics requests result = result .. count_active_spidertron_logistic_slots(pindex) .. " spidertron logistic requests set, " return result end --Read the current spidertron's logistics request set for this item -function mod.spidertron_logistic_request_read(item_stack,spidertron,pindex,additional_checks) +function mod.spidertron_logistic_request_read(item_stack, spidertron, pindex, additional_checks) local current_slot = nil local correct_slot_id = nil local result = "" - + --Check if logistics have been researched for i, tech in pairs(game.get_player(pindex).force.technologies) do if tech.name == "logistic-robotics" and not tech.researched then - printout("Logistic requests not available, research required.",pindex) + printout("Logistic requests not available, research required.", pindex) return end end - + if additional_checks then --Check if inside any logistic network or not (simpler than logistics network info) local network = spidertron.surface.find_logistic_network_by_position(spidertron.position, spidertron.force) - if network == nil or not network.valid then - result = result .. "Not in a network, " - end - + if network == nil or not network.valid then result = result .. "Not in a network, " end + --Check if personal logistics are enabled - if not spidertron.vehicle_logistic_requests_enabled then - result = result .. "Requests paused, " - end + if not spidertron.vehicle_logistic_requests_enabled then result = result .. "Requests paused, " end end - + --Find the correct request slot for this item - local correct_slot_id = get_entity_logistic_slot_index(item_stack,spidertron) - + local correct_slot_id = get_entity_logistic_slot_index(item_stack, spidertron) + if correct_slot_id == nil or correct_slot_id < 1 then - printout(result .. "Error: Invalid slot ID",pindex) - return + printout(result .. "Error: Invalid slot ID", pindex) + return end - + --Read the correct slot id value current_slot = spidertron.get_vehicle_logistic_slot(correct_slot_id) if current_slot == nil or current_slot.name == nil then --No requests found - printout(result .. "No logistic requests set for " .. item_stack.name .. " in this spidertron, use the L key and modifier keys to set requests.",pindex) + printout( + result + .. "No logistic requests set for " + .. item_stack.name + .. " in this spidertron, use the L key and modifier keys to set requests.", + pindex + ) return else --Report request counts and inventory counts @@ -1349,26 +1406,46 @@ function mod.spidertron_logistic_request_read(item_stack,spidertron,pindex,addit local max_result = "" local inv_result = "" local trash_result = "" - + if current_slot.min ~= nil then - min_result = fa_utils.express_in_stacks(current_slot.min, item_stack.prototype.stack_size, false) .. " minimum and " + min_result = fa_utils.express_in_stacks(current_slot.min, item_stack.prototype.stack_size, false) + .. " minimum and " end - + if current_slot.max ~= nil then - max_result = fa_utils.express_in_stacks(current_slot.max, item_stack.prototype.stack_size, false) .. " maximum " + max_result = fa_utils.express_in_stacks(current_slot.max, item_stack.prototype.stack_size, false) + .. " maximum " end - + local inv_count = spidertron.get_inventory(defines.inventory.spider_trunk).get_item_count(item_stack.name) inv_result = fa_utils.express_in_stacks(inv_count, item_stack.prototype.stack_size, false) .. " in inventory, " - + local trash_count = spidertron.get_inventory(defines.inventory.spider_trash).get_item_count(item_stack.name) - trash_result = fa_utils.express_in_stacks(trash_count, item_stack.prototype.stack_size, false) .. " in spidertron trash, " - - printout(result .. min_result .. max_result .. " requested for " .. item_stack.name .. ", " .. inv_result .. trash_result .. " use the L key and modifier keys to set requests.",pindex) + trash_result = fa_utils.express_in_stacks(trash_count, item_stack.prototype.stack_size, false) + .. " in spidertron trash, " + + printout( + result + .. min_result + .. max_result + .. " requested for " + .. item_stack.name + .. ", " + .. inv_result + .. trash_result + .. " use the L key and modifier keys to set requests.", + pindex + ) return else --All requests are nil - printout(result .. "No spidertron logistic requests set for " .. item_stack.name .. ", use the L key and modifier keys to set requests.",pindex) + printout( + result + .. "No spidertron logistic requests set for " + .. item_stack.name + .. ", use the L key and modifier keys to set requests.", + pindex + ) return end end @@ -1376,56 +1453,46 @@ end --Logistic requests can be made by chests or spidertrons function mod.can_make_logistic_requests(ent) - if ent == nil or ent.valid == false then - return false - end - if ent.type == "spider-vehicle" then - return true - end + if ent == nil or ent.valid == false then return false end + if ent.type == "spider-vehicle" then return true end local point = ent.get_logistic_point(defines.logistic_member_index.logistic_container) - if point == nil or point.valid == false then - return false - end + if point == nil or point.valid == false then return false end if point.mode == defines.logistic_mode.requester or point.mode == defines.logistic_mode.buffer then return true else - return false + return false end end --Logistic filters are set by storage chests function mod.can_set_logistic_filter(ent) - if ent == nil or ent.valid == false then - return false - end + if ent == nil or ent.valid == false then return false end local point = ent.get_logistic_point(defines.logistic_member_index.logistic_container) - if point == nil or point.valid == false then - return false - end + if point == nil or point.valid == false then return false end if point.mode == defines.logistic_mode.storage then return true else - return false + return false end end function mod.set_logistic_filter(stack, ent, pindex) if stack == nil or stack.valid_for_read == false then ent.storage_filter = nil - printout("logistic storage filter cleared",pindex) + printout("logistic storage filter cleared", pindex) return end - + if ent.storage_filter == stack.prototype then ent.storage_filter = nil - printout("logistic storage filter cleared",pindex) + printout("logistic storage filter cleared", pindex) else ent.storage_filter = stack.prototype - printout(stack.name .. " set as logistic storage filter ",pindex) + printout(stack.name .. " set as logistic storage filter ", pindex) end end -function mod.read_entity_requests_summary(ent,pindex)--**laterdo improve +function mod.read_entity_requests_summary(ent, pindex) --**laterdo improve if ent.type == "spider-vehicle" then printout(ent.request_slot_count .. " spidertron logistic requests set", pindex) else @@ -1442,25 +1509,17 @@ function mod.get_network_name(port) end --Sets a logistic network's name. The idea is that every roboport of the network has the same backer name and this is the networks's name. -function mod.set_network_name(port,new_name) +function mod.set_network_name(port, new_name) --Rename this port - if new_name == nil or new_name == "" then - return false - end + if new_name == nil or new_name == "" then return false end port.backer_name = new_name --Rename the rest, if any local nw = port.logistic_network - if nw == nil then - return true - end + if nw == nil then return true end local cells = nw.cells - if cells == nil or cells == {} then - return true - end - for i,cell in ipairs(cells) do - if cell.owner.supports_backer_name then - cell.owner.backer_name = new_name - end + if cells == nil or cells == {} then return true end + for i, cell in ipairs(cells) do + if cell.owner.supports_backer_name then cell.owner.backer_name = new_name end end return true end @@ -1470,20 +1529,16 @@ function mod.resolve_network_name(port_in) local oldest_port = port_in local nw = oldest_port.logistic_network --No network means resolved - if nw == nil then - return - end + if nw == nil then return end local cells = nw.cells --Check others - for i,cell in ipairs(cells) do + for i, cell in ipairs(cells) do local port = cell.owner - if port ~= nil and port.valid and oldest_port.unit_number > port.unit_number then - oldest_port = port - end + if port ~= nil and port.valid and oldest_port.unit_number > port.unit_number then oldest_port = port end end --Rename all mod.set_network_name(oldest_port, oldest_port.backer_name) - return + return end --[[--Logistic network menu options summary @@ -1513,11 +1568,15 @@ function mod.run_roboport_menu(menu_index, pindex, clicked) return end local nw = port.logistic_network - + if index == 0 then --0. Roboport of logistic network NAME, instructions - printout("Roboport of logistic network ".. mod.get_network_name(port) - .. ", Press 'W' and 'S' to navigate options, press 'LEFT BRACKET' to select an option or press 'E' to exit this menu.", pindex) + printout( + "Roboport of logistic network " + .. mod.get_network_name(port) + .. ", Press 'W' and 'S' to navigate options, press 'LEFT BRACKET' to select an option or press 'E' to exit this menu.", + pindex + ) elseif index == 1 then --1. Rename roboport networks if not clicked then @@ -1525,12 +1584,12 @@ function mod.run_roboport_menu(menu_index, pindex, clicked) else printout("Enter a new name for this network, then press 'ENTER' to confirm, or press 'ESC' to cancel.", pindex) players[pindex].roboport_menu.renaming = true - local frame = game.get_player(pindex).gui.screen.add{type = "frame", name = "network-rename"} + local frame = game.get_player(pindex).gui.screen.add({ type = "frame", name = "network-rename" }) frame.bring_to_front() frame.force_auto_center() frame.focus() --game.get_player(pindex).opened = frame - local input = frame.add{type="textfield", name = "input"} + local input = frame.add({ type = "textfield", name = "input" }) input.focus() end elseif index == 2 then @@ -1590,25 +1649,21 @@ end ROBOPORT_MENU_LENGTH = 6 function mod.roboport_menu_open(pindex) - if players[pindex].vanilla_mode then - return - end + if players[pindex].vanilla_mode then return end --Set the player menu tracker to this menu players[pindex].menu = "roboport_menu" players[pindex].in_menu = true players[pindex].move_queue = {} - + --Initialize if needed - if players[pindex].roboport_menu == nil then - players[pindex].roboport_menu = {} - end + if players[pindex].roboport_menu == nil then players[pindex].roboport_menu = {} end --Set the menu line counter to 0 players[pindex].roboport_menu.index = 0 - + --Play sound - game.get_player(pindex).play_sound{path = "Open-Inventory-Sound"} - - --Load menu + game.get_player(pindex).play_sound({ path = "Open-Inventory-Sound" }) + + --Load menu mod.run_roboport_menu(players[pindex].roboport_menu.index, pindex, false) end @@ -1621,29 +1676,25 @@ function mod.roboport_menu_close(pindex, mute_in) --Set the menu line counter to 0 players[pindex].roboport_menu.index = 0 players[pindex].roboport_menu.port = nil - + --play sound - if not mute then - game.get_player(pindex).play_sound{path="Close-Inventory-Sound"} - end - + if not mute then game.get_player(pindex).play_sound({ path = "Close-Inventory-Sound" }) end + --Destroy GUI if game.get_player(pindex).gui.screen["network-rename"] ~= nil then game.get_player(pindex).gui.screen["network-rename"].destroy() end - if game.get_player(pindex).opened ~= nil then - game.get_player(pindex).opened = nil - end + if game.get_player(pindex).opened ~= nil then game.get_player(pindex).opened = nil end end function mod.roboport_menu_up(pindex) players[pindex].roboport_menu.index = players[pindex].roboport_menu.index - 1 if players[pindex].roboport_menu.index < 0 then players[pindex].roboport_menu.index = 0 - game.get_player(pindex).play_sound{path = "inventory-edge"} + game.get_player(pindex).play_sound({ path = "inventory-edge" }) else --Play sound - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) end --Load menu mod.run_roboport_menu(players[pindex].roboport_menu.index, pindex, false) @@ -1653,10 +1704,10 @@ function mod.roboport_menu_down(pindex) players[pindex].roboport_menu.index = players[pindex].roboport_menu.index + 1 if players[pindex].roboport_menu.index > ROBOPORT_MENU_LENGTH then players[pindex].roboport_menu.index = ROBOPORT_MENU_LENGTH - game.get_player(pindex).play_sound{path = "inventory-edge"} + game.get_player(pindex).play_sound({ path = "inventory-edge" }) else --Play sound - game.get_player(pindex).play_sound{path = "Inventory-Move"} + game.get_player(pindex).play_sound({ path = "Inventory-Move" }) end --Load menu mod.run_roboport_menu(players[pindex].roboport_menu.index, pindex, false) @@ -1665,9 +1716,20 @@ end function mod.roboport_contents_info(port) local result = "" local cell = port.logistic_cell - result = result .. " charging " .. cell.charging_robot_count .. " robots with " .. cell.to_charge_robot_count .. " in queue, " .. - " stationed " .. cell.stationed_logistic_robot_count .. " logistic robots and " .. cell.stationed_construction_robot_count .. " construction robots " .. - " and " .. port.get_inventory(defines.inventory.roboport_material).get_item_count() .. " repair packs " + result = result + .. " charging " + .. cell.charging_robot_count + .. " robots with " + .. cell.to_charge_robot_count + .. " in queue, " + .. " stationed " + .. cell.stationed_logistic_robot_count + .. " logistic robots and " + .. cell.stationed_construction_robot_count + .. " construction robots " + .. " and " + .. port.get_inventory(defines.inventory.roboport_material).get_item_count() + .. " repair packs " return result end @@ -1676,19 +1738,17 @@ function mod.roboport_neighbours_info(port) local cell = port.logistic_cell local neighbour_count = #cell.neighbours local neighbour_dirs = "" - for i, neighbour in ipairs(cell.neighbours) do + for i, neighbour in ipairs(cell.neighbours) do local dir = fa_utils.direction_lookup(fa_utils.get_direction_biased(neighbour.owner.position, port.position)) - if i > 1 then - neighbour_dirs = neighbour_dirs .. " and " - end - neighbour_dirs = neighbour_dirs .. dir + if i > 1 then neighbour_dirs = neighbour_dirs .. " and " end + neighbour_dirs = neighbour_dirs .. dir end - if neighbour_count > 0 then + if neighbour_count > 0 then result = neighbour_count .. " neighbours" .. ", at the " .. neighbour_dirs else result = neighbour_count .. " neighbours" end - + return result end @@ -1700,7 +1760,17 @@ function mod.logistic_network_members_info(port) result = " Error: no network " return result end - result = " Network has " .. #nw.cells .. " roboports, and " .. nw.all_logistic_robots .. " logistic robots with " .. nw.available_logistic_robots .. " available, and " .. nw.all_construction_robots .. " construction robots with " .. nw.available_construction_robots .. " available " + result = " Network has " + .. #nw.cells + .. " roboports, and " + .. nw.all_logistic_robots + .. " logistic robots with " + .. nw.available_logistic_robots + .. " available, and " + .. nw.all_construction_robots + .. " construction robots with " + .. nw.available_construction_robots + .. " available " return result end @@ -1708,42 +1778,43 @@ function mod.logistic_network_chests_info(port) local result = "" local cell = port.logistic_cell local nw = cell.logistic_network - + if nw == nil or nw.valid == false then result = " Error, no network " return result end - + local storage_chest_count = 0 - for i,ent in ipairs(nw.storage_points) do - if ent.owner.type == "logistic-container" then - storage_chest_count = storage_chest_count + 1 - end + for i, ent in ipairs(nw.storage_points) do + if ent.owner.type == "logistic-container" then storage_chest_count = storage_chest_count + 1 end end local passive_provider_chest_count = 0 - for i,ent in ipairs(nw.passive_provider_points) do - if ent.owner.type == "logistic-container" then - passive_provider_chest_count = passive_provider_chest_count + 1 - end + for i, ent in ipairs(nw.passive_provider_points) do + if ent.owner.type == "logistic-container" then passive_provider_chest_count = passive_provider_chest_count + 1 end end local active_provider_chest_count = 0 - for i,ent in ipairs(nw.active_provider_points) do - if ent.owner.type == "logistic-container" then - active_provider_chest_count = active_provider_chest_count + 1 - end + for i, ent in ipairs(nw.active_provider_points) do + if ent.owner.type == "logistic-container" then active_provider_chest_count = active_provider_chest_count + 1 end end local requester_chest_count = 0 - for i,ent in ipairs(nw.requester_points) do - if ent.owner.type == "logistic-container" then - requester_chest_count = requester_chest_count + 1 - end - end - local total_chest_count = storage_chest_count + passive_provider_chest_count + active_provider_chest_count + requester_chest_count - result = " Network has " .. total_chest_count .. " chests in total, with " .. - storage_chest_count .. " storage chests, " .. - passive_provider_chest_count .. " passive provider chests, " .. - active_provider_chest_count .. " active provider chests, " .. - requester_chest_count .. " requester chests or buffer chests, " + for i, ent in ipairs(nw.requester_points) do + if ent.owner.type == "logistic-container" then requester_chest_count = requester_chest_count + 1 end + end + local total_chest_count = storage_chest_count + + passive_provider_chest_count + + active_provider_chest_count + + requester_chest_count + result = " Network has " + .. total_chest_count + .. " chests in total, with " + .. storage_chest_count + .. " storage chests, " + .. passive_provider_chest_count + .. " passive provider chests, " + .. active_provider_chest_count + .. " active provider chests, " + .. requester_chest_count + .. " requester chests or buffer chests, " --game.print(result,{volume_modifier=0})-- return result end @@ -1758,7 +1829,7 @@ function mod.logistic_network_items_info(port) local itemset = nw.get_contents() local itemtable = {} for name, count in pairs(itemset) do - table.insert(itemtable, {name = name, count = count}) + table.insert(itemtable, { name = name, count = count }) end table.sort(itemtable, function(k1, k2) return k1.count > k2.count @@ -1779,9 +1850,7 @@ function mod.logistic_network_items_info(port) if #itemtable > 4 then result = result .. " and " .. itemtable[5].name .. " times " .. itemtable[5].count .. ", " end - if #itemtable > 5 then - result = result .. " and other items " - end + if #itemtable > 5 then result = result .. " and other items " end end return result end diff --git a/scripts/zoom.lua b/scripts/zoom.lua index cd2efc07..9ce9b41b 100644 --- a/scripts/zoom.lua +++ b/scripts/zoom.lua @@ -9,7 +9,7 @@ local ln_zoom = math.log(ZOOM_PER_TICK) local mod = {} function mod.get_zoom_tick(pindex) - return math.floor(math.log(global.players[pindex].zoom)/ln_zoom + 0.5) + return math.floor(math.log(global.players[pindex].zoom) / ln_zoom + 0.5) end function mod.tick_to_zoom(zoom_tick) @@ -20,10 +20,10 @@ function mod.fix_zoom(pindex) game.players[pindex].zoom = global.players[pindex].zoom end -local function zoom_change(pindex,etick,change_by_tick) +local function zoom_change(pindex, etick, change_by_tick) -- if global.players[pindex].last_zoom_event_tick == etick then - -- print("maybe duplicate") - -- return + -- print("maybe duplicate") + -- return -- end -- global.players[pindex].last_zoom_event_tick = etick if game.players[pindex].render_mode == defines.render_mode.game then @@ -33,7 +33,7 @@ local function zoom_change(pindex,etick,change_by_tick) if zoom < MAX_ZOOM and zoom > MIN_ZOOM then global.players[pindex].zoom = zoom local stack = game.get_player(pindex).cursor_stack - if stack and stack.valid_for_read and stack.valid and stack.prototype.place_result ~= nil then + if stack and stack.valid_for_read and stack.valid and stack.prototype.place_result ~= nil then fa_graphics.sync_build_cursor_graphics(pindex) else fa_graphics.draw_cursor_highlight(pindex, nil, nil) @@ -50,17 +50,15 @@ function mod.zoom_out(event) zoom_change(event.player_index, event.tick, -1) end -script.on_event("fa-zoom-in" , mod.zoom_in ) +script.on_event("fa-zoom-in", mod.zoom_in) script.on_event("fa-zoom-out", mod.zoom_out) -script.on_event(defines.events.on_cutscene_waypoint_reached,function(event) - if game.players[event.player_index].render_mode == defines.render_mode.game then - mod.fix_zoom(event.player_index) - end +script.on_event(defines.events.on_cutscene_waypoint_reached, function(event) + if game.players[event.player_index].render_mode == defines.render_mode.game then mod.fix_zoom(event.player_index) end end) -script.on_event("fa-debug-reset-zoom",function(event) +script.on_event("fa-debug-reset-zoom", function(event) global.players[event.player_index].zoom = 1 end) -script.on_event("fa-debug-reset-zoom-2x",function(event) +script.on_event("fa-debug-reset-zoom-2x", function(event) global.players[event.player_index].zoom = 2 end) diff --git a/settings-updates.lua b/settings-updates.lua index 567fd020..b2afc08a 100644 --- a/settings-updates.lua +++ b/settings-updates.lua @@ -1,27 +1,27 @@ data:extend({ - { - type = "int-setting", - name = "VehicleSnap_amount", - setting_type = "runtime-per-user", - minimum_value = 4, - default_value = 8 - }, - { - type = "string-setting", - name = "aai-loaders-mode", - setting_type = "startup", - default_value = "expensive", - allowed_values = {"lubricated", "expensive", "graphics-only"}, - order = "a" - }, - { + { + type = "int-setting", + name = "VehicleSnap_amount", + setting_type = "runtime-per-user", + minimum_value = 4, + default_value = 8, + }, + { + type = "string-setting", + name = "aai-loaders-mode", + setting_type = "startup", + default_value = "expensive", + allowed_values = { "lubricated", "expensive", "graphics-only" }, + order = "a", + }, + { type = "bool-setting", name = "PDA-setting-smart-roads-enabled", setting_type = "startup", default_value = true, order = "ab", - }, - { + }, + { type = "int-setting", name = "PDA-setting-assist-min-speed", setting_type = "runtime-global", @@ -29,5 +29,5 @@ data:extend({ minimum_value = 6, maximum_value = 10000, order = "d", - } -}) \ No newline at end of file + }, +})