mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-27 11:35:22 +09:00
Added protection commands
This commit is contained in:
98
modules/commands/protection.lua
Normal file
98
modules/commands/protection.lua
Normal file
@@ -0,0 +1,98 @@
|
||||
--[[-- Commands Module - Protection
|
||||
- Adds a commands that can add and remove protection
|
||||
@commands Protection
|
||||
]]
|
||||
|
||||
local Commands = require 'expcore.commands' --- @dep expcore.commands
|
||||
local EntityProtection = require 'modules.control.protection' --- @dep modules.control.protection
|
||||
local Selection = require 'modules.control.selection' --- @dep modules.control.selection
|
||||
|
||||
local SelectionProtectEntity = 'ProtectEntity'
|
||||
local SelectionProtectArea = 'ProtectArea'
|
||||
|
||||
--- Test if a point is inside an aabb
|
||||
local function aabb_point_enclosed(point, aabb)
|
||||
return point.x >= aabb.left_top.x and point.y >= aabb.left_top.y
|
||||
and point.x <= aabb.right_bottom.x and point.y <= aabb.right_bottom.y
|
||||
end
|
||||
|
||||
--- Test if an aabb is inside another aabb
|
||||
local function aabb_area_enclosed(aabbOne, aabbTwo)
|
||||
return aabb_point_enclosed(aabbOne.left_top, aabbTwo)
|
||||
and aabb_point_enclosed(aabbOne.right_bottom, aabbTwo)
|
||||
end
|
||||
|
||||
--- Align an aabb to the grid by expanding it
|
||||
local function aabb_align_expand(aabb)
|
||||
return {
|
||||
left_top = { x = math.floor(aabb.left_top.x), y = math.floor(aabb.left_top.y) },
|
||||
right_bottom = { x = math.ceil(aabb.right_bottom.x), y = math.ceil(aabb.right_bottom.y) }
|
||||
}
|
||||
end
|
||||
|
||||
--- Toggles entity protection selection
|
||||
-- @command protect-entity
|
||||
Commands.new_command('protect-entity', 'Toggles entity protection selection, hold shift to remove protection')
|
||||
:add_alias('pe')
|
||||
:register(function(player)
|
||||
if Selection.is_selecting(player, SelectionProtectEntity) then
|
||||
Selection.stop(player)
|
||||
else
|
||||
Selection.start(player, SelectionProtectEntity)
|
||||
return Commands.success{'expcom-protection.entered-entity-selection'}
|
||||
end
|
||||
end)
|
||||
|
||||
--- Toggles area protection selection
|
||||
-- @command protect-area
|
||||
Commands.new_command('protect-area', 'Toggles area protection selection, hold shift to remove protection')
|
||||
:add_alias('pa')
|
||||
:register(function(player)
|
||||
if Selection.is_selecting(player, SelectionProtectArea) then
|
||||
Selection.stop(player)
|
||||
else
|
||||
Selection.start(player, SelectionProtectArea)
|
||||
return Commands.success{'expcom-protection.entered-entity-selection'}
|
||||
end
|
||||
end)
|
||||
|
||||
--- When an area is selected to add protection to entities
|
||||
Selection.on_selection(SelectionProtectEntity, function(_, event)
|
||||
for _, entity in ipairs(event.entities) do
|
||||
EntityProtection.add_entity(entity)
|
||||
end
|
||||
return Commands.success{'expcom-protection.protected-entities', #event.entities}
|
||||
end)
|
||||
|
||||
--- When an area is selected to remove protection from entities
|
||||
Selection.on_alt_selection(SelectionProtectEntity, function(_, event)
|
||||
for _, entity in ipairs(event.entities) do
|
||||
EntityProtection.remove_entity(entity)
|
||||
end
|
||||
return Commands.success{'expcom-protection.unprotected-entities', #event.entities}
|
||||
end)
|
||||
|
||||
--- When an area is selected to add protection to the area
|
||||
Selection.on_selection(SelectionProtectEntity, function(_, event)
|
||||
local area = aabb_align_expand(event.area)
|
||||
local areas = EntityProtection.get_areas(event.surface)
|
||||
for _, next_area in pairs(areas) do
|
||||
if aabb_area_enclosed(area, next_area) then
|
||||
return Commands.error{'expcom-protection.already-protected'}
|
||||
end
|
||||
end
|
||||
EntityProtection.add_area(event.surface, area)
|
||||
return Commands.success{'expcom-protection.protected-area'}
|
||||
end)
|
||||
|
||||
--- When an area is selected to remove protection from the area
|
||||
Selection.on_alt_selection(SelectionProtectEntity, function(_, event)
|
||||
local area = aabb_align_expand(event.area)
|
||||
local areas = EntityProtection.get_areas(event.surface)
|
||||
for _, next_area in pairs(areas) do
|
||||
if aabb_area_enclosed(next_area, area) then
|
||||
EntityProtection.remove_area(event.surface, area)
|
||||
Commands.print{'expcom-protection.unprotected-area'}
|
||||
end
|
||||
end
|
||||
end)
|
||||
@@ -72,9 +72,15 @@ end
|
||||
|
||||
--- Test if a player is selecting something
|
||||
-- @tparam LuaPlayer player The player to test
|
||||
function Selection.is_selecting(player)
|
||||
-- @tparam[opt] string selection_name If given will only return true if the selection is this selection
|
||||
function Selection.is_selecting(player, selection_name)
|
||||
if selection_name ~= nil then
|
||||
if not selections[player.index] then return false end
|
||||
return selections[player.index].name == selection_name
|
||||
else
|
||||
return player.cursor_stack.is_selection_tool
|
||||
end
|
||||
end
|
||||
|
||||
--- Filter on_player_selected_area to this custom selection, pretends with player and appends with selection arguments
|
||||
-- @tparam string selection_name The name of the selection to listen for
|
||||
|
||||
Reference in New Issue
Block a user