require 'default:scriptcommands'
GodCommandFuncs.CreateRegion = function()
-- create the two event handlers for target 1 and target 2
RegisterSingleEventHandler(EventType.ClientTargetLocResponse, "CreateRegionTarget1", function (success,targetLoc,targetObj,user)
if(success and targetLoc and user) then
-- set up transfer temp objvar on caller of this command
this:SetObjVar("TempTargetData",targetLoc)
-- send client targeting to caller of this command for target 2
this:RequestClientTargetLoc(this, "CreateRegionTarget2")
end
end)
RegisterSingleEventHandler(EventType.ClientTargetLocResponse, "CreateRegionTarget2", function (success,targetLoc,targetObj,user)
if(success and targetLoc and user) then
firstTarget = this:GetObjVar("TempTargetData")
ParseRegionToXml(GetRegionDataTable(firstTarget,targetLoc))
this:DelObjVar("TempTargetData")
end
end)
-- send client targeting to caller of this command for target 1
this:RequestClientTargetLoc(this, "CreateRegionTarget1")
-- make sure to delete the tempTargetData when done
end
function GetRegionDataTable(target1,target2)
local x1 = target1.X
local y1 = target1.Z
local x2 = target2.X
local y2 = target2.Z
local bigX,smallX,bigY,smallY = 0
if(x1 > x2) then
bigX = x1
smallX = x2
end
if(x1 < x2) then
bigX = x2
smallX = x1
end
if(y1 > y2) then
bigY = y1
smallY = y2
end
if(y1 < y2) then
bigY = y2
smallY = y1
end
local Size = Loc((bigX-smallX),0.1,(bigY-smallY))
local Center = Loc(bigX - (Size.X/2),0,bigY - (Size.Z/2))
local regionDataTable =
{
SizeOfRegion = Size,
CenterOfRegeion = Center,
RectOfRegion =
{
MaxX = bigX,
MinX = smallX,
MaxY = bigY,
MinY = smallY,
},
}
return regionDataTable
end
function ParseRegionToXml(regionDataTable)
-- create references to elements
local regionDefinitionElement = xml.new("RegionDefinition")
local regionNameElement = xml.new("RegionName")
local category = xml.new("Category")
local regionBounds = xml.new("RegionBounds")
--region bound elements
local rect = xml.new("Rect")
local center = xml.new("Center")
local size = xml.new("Size")
local position = xml.new("Position")
local rotation = xml.new("Rotation")
local scale = xml.new("Scale")
position[1] = "(0,0,0)"
rotation[1] = "(0,0,0)"
scale[1] = "(1,1,1)"
size[1] = "(" .. regionDataTable.SizeOfRegion.X .. ",0,"..regionDataTable.SizeOfRegion.Z ..")"
center[1] = "(" .. regionDataTable.CenterOfRegeion.X .. ",0,"..regionDataTable.CenterOfRegeion.Z ..")"
rect[1] = GetRectElementData(regionDataTable)
regionNameElement[1] = "ChangeMe"
category[1] = "Main"
--append the elements together now
regionBounds:append(position)
regionBounds:append(rotation)
regionBounds:append(scale)
regionBounds:append(size)
regionBounds:append(center)
regionBounds:append(rect)
regionDefinitionElement:append(regionNameElement)
regionDefinitionElement:append(category)
regionDefinitionElement:append(regionBounds)
local clipString = string.gsub(tostring(regionDefinitionElement),""","\"")
io.popen('clip','w'):write(clipString):close()
end
function GetRectElementData(regionDataTable)
local rectString = string.format("[(%s,%s,%s)(%s,%s,%s)(%s,%s,%s)(%s,%s,%s)(%s,%s,%s)(%s,%s,%s)(%s,%s,%s)(%s,%s,%s)]",
regionDataTable.RectOfRegion.MaxX,0.05,regionDataTable.RectOfRegion.MinY,
regionDataTable.RectOfRegion.MinX,0.05,regionDataTable.RectOfRegion.MinY,
regionDataTable.RectOfRegion.MinX,0.05,regionDataTable.RectOfRegion.MaxY,
regionDataTable.RectOfRegion.MaxX,0.05,regionDataTable.RectOfRegion.MaxY,
regionDataTable.RectOfRegion.MaxX,-0.05,regionDataTable.RectOfRegion.MinY,
regionDataTable.RectOfRegion.MinX,-0.05,regionDataTable.RectOfRegion.MinY,
regionDataTable.RectOfRegion.MinX,-0.05,regionDataTable.RectOfRegion.MaxY,
regionDataTable.RectOfRegion.MaxX,-0.05,regionDataTable.RectOfRegion.MaxY)
return rectString
end
RegisterCommand { Command="createregion",Category="GizmosTools",AccessLevel=AccessLevel.God,Func=GodCommandFuncs.CreateRegion,Usage="<createregion>", Desc="You will be presented to click 2 locations for form a bounding box, then the region info will be sent to your clip board to CTRL+V paste into your WorldData.xml file." }