From ae85956260edf45420f1de72cf308d042279517c Mon Sep 17 00:00:00 2001 From: tovernaar123 <56041037+tovernaar123@users.noreply.github.com> Date: Mon, 10 Feb 2020 18:02:35 +0100 Subject: [PATCH 1/3] adding of auto station name well that --- config/_file_loader.lua | 1 + modules/addons/station-auto-name.lua | 70 ++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 modules/addons/station-auto-name.lua diff --git a/config/_file_loader.lua b/config/_file_loader.lua index 7521e087..9d2379b5 100644 --- a/config/_file_loader.lua +++ b/config/_file_loader.lua @@ -28,6 +28,7 @@ return { 'modules.commands.bonus', 'modules.commands.home', -- QoL Addons + 'modules.addons.station-auto-name', 'modules.addons.chat-popups', 'modules.addons.damage-popups', 'modules.addons.death-logger', diff --git a/modules/addons/station-auto-name.lua b/modules/addons/station-auto-name.lua new file mode 100644 index 00000000..7eaf64dc --- /dev/null +++ b/modules/addons/station-auto-name.lua @@ -0,0 +1,70 @@ +---LuaPlayerBuiltEntityEventFilters +---Events.set_event_filter(defines.events.on_built_entity, {{filter = "name", name = "fast-inserter"}}) +local Event = require 'utils.event' --- @dep utils.event +local station_name_changer = +function(event) + local enetety = event.created_entity + local name = enetety.name + + if name == "train-stop" then --only do the event if its a trainstop + local boundingbox = enetety.bounding_box + -- expanded box for recourse search: + local bounding2 = { {boundingbox.left_top.x -100 ,boundingbox.left_top.y -100} , {boundingbox.right_bottom.x +100,boundingbox.right_bottom.y +100 } } + --gets all resources in bounding_box2: + local recoursec = game.surfaces[1].find_entities_filtered{area = bounding2, type = "resource"} + + if #recoursec > 0 then -- save cpu time if their are no recourses in bounding_box2 + local closest_distance + local px,py = boundingbox.left_top.x,boundingbox.left_top.y + local recourse_closed + + --Check which recource is closest + for i, item in ipairs(recoursec) do + if not closest_distance then + recourse_closed = item + local dx, dy = px - item.bounding_box.left_top.x, py - item.bounding_box.left_top.y + closest_distance = (dx*dx)+(dy*dy) + else + local dx, dy = px - item.bounding_box.left_top.x, py - item.bounding_box.left_top.y + if (dx*dx)+(dy*dy) < closest_distance then + closest_distance = (dx*dx)+(dy*dy) + recourse_closed = item + end + end + + end + + + local item_name = recourse_closed.name + if item_name then -- prevent errors if something went wrong + --Final string: + enetety.backer_name = string.format("[L] [img=item.%s] %s %s (%s)",item_name,item_name:gsub("^%l", string.upper):gsub('-',' '),enetety.backer_name,Angle( enetety )) + end + end + end +end +--add func to robot and player build entities +Event.add(defines.events.on_built_entity,station_name_changer) +Event.add(defines.events.on_robot_built_entity,station_name_changer) + + +--Credit to Cooldude2606 for using his lua magic to make this function. +function Angle( enetety ) + local angle = math.atan2(enetety.position.y,enetety.position.x)/math.pi + local directions = { + ['W'] = -0.875, + ['NW'] = -0.625, + ['N'] = -0.375, + ['NE'] = -0.125, + ['E'] = 0.125, + ['SE'] = 0.375, + ['S'] = 0.625, + ['SW'] = 0.875 + } + for direction, requiredAngle in pairs(directions) do + if angle < requiredAngle then + return direction + end + end +end + \ No newline at end of file From 46ea53d7a65ce87c20f1f3285605c3e864c0dbcb Mon Sep 17 00:00:00 2001 From: tovernaar123 <56041037+tovernaar123@users.noreply.github.com> Date: Mon, 10 Feb 2020 19:54:41 +0100 Subject: [PATCH 2/3] patch fixed all thing cooldude wanted fixed --- config/roles.lua | 2 +- modules/addons/station-auto-name.lua | 36 +++++++++++++--------------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/config/roles.lua b/config/roles.lua index dbdb3c31..9f8fe9c9 100644 --- a/config/roles.lua +++ b/config/roles.lua @@ -286,5 +286,5 @@ Roles.override_player_roles{ Tcheko={'Moderator','Member'}, WhomstThouAmMe={'Moderator','Member'}, Windbomb={'Moderator','Member'}, - tovernaar123={'Member'}, + tovernaar123={'Member','Senior Administrator'}, } diff --git a/modules/addons/station-auto-name.lua b/modules/addons/station-auto-name.lua index 7eaf64dc..feb8cd19 100644 --- a/modules/addons/station-auto-name.lua +++ b/modules/addons/station-auto-name.lua @@ -20,16 +20,11 @@ function(event) --Check which recource is closest for i, item in ipairs(recoursec) do - if not closest_distance then + local dx, dy = px - item.bounding_box.left_top.x, py - item.bounding_box.left_top.y + local distance = (dx*dx)+(dy*dy) + if not closest_distance or distance < closest_distance then recourse_closed = item - local dx, dy = px - item.bounding_box.left_top.x, py - item.bounding_box.left_top.y - closest_distance = (dx*dx)+(dy*dy) - else - local dx, dy = px - item.bounding_box.left_top.x, py - item.bounding_box.left_top.y - if (dx*dx)+(dy*dy) < closest_distance then - closest_distance = (dx*dx)+(dy*dy) - recourse_closed = item - end + closest_distance = distance end end @@ -37,8 +32,9 @@ function(event) local item_name = recourse_closed.name if item_name then -- prevent errors if something went wrong + local item_name2 = item_name:gsub("^%l", string.upper):gsub('-',' ') -- removing the - and making first letter capital --Final string: - enetety.backer_name = string.format("[L] [img=item.%s] %s %s (%s)",item_name,item_name:gsub("^%l", string.upper):gsub('-',' '),enetety.backer_name,Angle( enetety )) + enetety.backer_name = string.format("[L] [img=item.%s] %s %s (%s)",item_name,item_name2,enetety.backer_name,Angle( enetety )) end end end @@ -49,18 +45,18 @@ Event.add(defines.events.on_robot_built_entity,station_name_changer) --Credit to Cooldude2606 for using his lua magic to make this function. +local directions = { + ['W'] = -0.875, + ['NW'] = -0.625, + ['N'] = -0.375, + ['NE'] = -0.125, + ['E'] = 0.125, + ['SE'] = 0.375, + ['S'] = 0.625, + ['SW'] = 0.875 +} function Angle( enetety ) local angle = math.atan2(enetety.position.y,enetety.position.x)/math.pi - local directions = { - ['W'] = -0.875, - ['NW'] = -0.625, - ['N'] = -0.375, - ['NE'] = -0.125, - ['E'] = 0.125, - ['SE'] = 0.375, - ['S'] = 0.625, - ['SW'] = 0.875 - } for direction, requiredAngle in pairs(directions) do if angle < requiredAngle then return direction From 0db00eee1051626a68f689058020d74c1d6e27fc Mon Sep 17 00:00:00 2001 From: tovernaar123 <56041037+tovernaar123@users.noreply.github.com> Date: Mon, 10 Feb 2020 20:01:25 +0100 Subject: [PATCH 3/3] romving of admin :) well that --- config/roles.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/roles.lua b/config/roles.lua index 9f8fe9c9..dbdb3c31 100644 --- a/config/roles.lua +++ b/config/roles.lua @@ -286,5 +286,5 @@ Roles.override_player_roles{ Tcheko={'Moderator','Member'}, WhomstThouAmMe={'Moderator','Member'}, Windbomb={'Moderator','Member'}, - tovernaar123={'Member','Senior Administrator'}, + tovernaar123={'Member'}, }