Protection Script

From Pandorabox
Jump to navigation Jump to search

This script assists to calculate coordinates to protect a larger area with multiple smaller areas.

#!/usr/bin/lua
-- Code to prepare coordinates to protect bigger areas
-- Author: SwissalpS
-- License: MIT
-- Version: 2250327.1338
-- Written to be run in terminal of a linux host. With few
-- changes this code can also be used in a moon-controller,
-- lua-controller, lua-tube or other in-game lua nodes.
------------------------------------------------------------
------------------- CONFIG BEGIN --------------------
------------------------------------------------------------
-- Should slashes be prefixed to output lines.
-- If you are going to copy paste in-game set to true.
-- Leave false if you are going to use web-ui.
local bPS = false

-- Format string for protecting. Placeholders (%d) are
-- populated with x, y and z counts of areas.
local sName = 'protect <Insert Name> %d %d %d'

-- First position. Bottom, South West corner.
local tPos1 = { x = 8649, y = 5000, z = 22949 }
-- Last position. Top, North East corner.
local tPos2 = { x = 9924, y = 5615, z = 23306 }

local tSizeMedium = { x = 64, y = 128, z = 64 }
local tSizeHuge = { x = 512, y = 512, z = 512 }
-- Maximum size of subareas. Use one of the above predefined
-- or create your own custom size.
local tSize = tSizeMedium

------------------------------------------------------------
------------------- CONFIG END --------------------
------------------------------------------------------------
-----------------------------------------------------
-----------------------------------------------------

-- Roughly validate coordinates
if tPos2.x < tPos1.x
	or tPos2.y < tPos1.y
	or tPos2.z < tPos1.z
then
	print('Pos 2 coordinates must be greater than Pos 1.')
	os.exit(1)
end

local min = math.min
-- Prepare command strings with or without slash.
local sP = bPS and '/' or ''
sName = sP .. sName
local sP1 = sP .. 'area_pos1 %d %d %d'
local sP2 = sP .. 'area_pos2 %d %d %d'

-- Subarea counters
local ix, iy, iz = 1, 1, 1
-- Temporary position 1
local t1 = { x = tPos1.x, y = tPos1.y, z = tPos1.z }
-- Temporary position 2
local t2 = {
	x = min(tPos2.x, t1.x + tSize.x - 1),
	y = min(tPos2.y, t1.y + tSize.y - 1),
	z = min(tPos2.z, t1.z + tSize.z - 1),
}
-- Loop in y-direction last. (Upward)
while t1.y <= tPos2.y do
	-- Loop in z-direction second. (Northward)
	while t1.z <= tPos2.z do
		-- Loop in x-direction first. (Eastward)
		while t1.x <= tPos2.x do
			-- Output chatcommands.
			print(string.format(sP1, t1.x, t1.y, t1.z))
			print(string.format(sP2, t2.x, t2.y, t2.z))
			print(string.format(sName, ix, iy, iz))

			-- Increment counter.
			ix = ix + 1
			-- Increment x-coordinates.
			t1.x = t2.x + 1
			t2.x = min(tPos2.x, t1.x + tSize.x - 1)
		end
		-- Reset counter and x-coordinates.
		ix = 1
		t1.x = tPos1.x
		t2.x = min(tPos2.x, t1.x + tSize.x - 1)

		-- Increment counter.
		iz = iz + 1
		-- Increment z-coordinates.
		t1.z = t2.z + 1
		t2.z = min(tPos2.z, t1.z + tSize.z - 1)
	end
	-- Reset counter and z-coordinates.
	iz = 1
	t1.z = tPos1.z
	t2.z = min(tPos2.z, t1.z + tSize.z - 1)

	-- Increment counter.
	iy = iy + 1
	-- Increment y-coordinates.
	t1.y = t2.y + 1
	t2.y = min(tPos2.y, t1.y + tSize.y - 1)
end