mirror of
https://github.com/copyrighttxt/watrbx-game-engine.git
synced 2026-09-04 20:57:49 +00:00
240 lines
7.5 KiB
Lua
240 lines
7.5 KiB
Lua
-----------------
|
|
--DEFAULT VALUES-
|
|
-----------------
|
|
loaded = false
|
|
on = false
|
|
|
|
|
|
|
|
|
|
---------------
|
|
--PLUGIN SETUP-
|
|
---------------
|
|
self = PluginManager():CreatePlugin()
|
|
mouse = self:GetMouse()
|
|
mouse.Button1Down:connect(function() onClicked(mouse) end)
|
|
self.Deactivation:connect(function()
|
|
Off()
|
|
end)
|
|
toolbar = self:CreateToolbar("Terrain")
|
|
toolbarbutton = toolbar:CreateButton("", "Builder", "builder.png")
|
|
toolbarbutton.Click:connect(function()
|
|
if on then
|
|
Off()
|
|
elseif loaded then
|
|
On()
|
|
end
|
|
end)
|
|
|
|
|
|
-----------------------
|
|
--FUNCTION DEFINITIONS-
|
|
-----------------------
|
|
function dist(x1, y1, x2, y2)
|
|
return math.sqrt(math.pow(x2-x1, 2) + math.pow(y2-y1, 2))
|
|
end
|
|
|
|
function dist3d(x1, y1, z1, x2, y2, z2)
|
|
return math.sqrt(math.pow(dist(x1, y1, x2, y2), 2) + math.pow(z2-z1, 2))
|
|
end
|
|
|
|
--converts the coordinate coord into a cluster coordinate where clusterlength is the length of the cluster in the same dimension as coord
|
|
function clusterCoord(coord, clusterlength)
|
|
return math.floor((coord + clusterlength/2)/4)
|
|
end
|
|
|
|
--makes a wedge at location x, y, z
|
|
--sets cell x, y, z to default material if parameter is provided, if not sets cell x, y, z to be whatever material it previously was
|
|
--returns true if made a wedge, false if the cell remains a block
|
|
function MakeWedge(x, y, z, defaultmaterial)
|
|
local c = game.Workspace.Terrain
|
|
--gather info about all the cells around x, y, z
|
|
surroundings = {} --surroundings is a 3 x 3 x 3 array of the material of the cells adjacent to x, y, z
|
|
for i = x - 1, x + 1 do
|
|
surroundings[i] = {}
|
|
for j = y - 1, y + 1 do
|
|
surroundings[i][j] = {}
|
|
for k = z - 1, z + 1 do
|
|
local material, wedge, rotation = c:GetCell(i, j, k)
|
|
surroundings[i][j][k] = material.Value
|
|
end
|
|
end
|
|
end
|
|
--make some useful arrays and counters
|
|
local sides = {} --sides is an array of the material of the 4 adjacent sides
|
|
sides[0] = surroundings[x - 1][y][z]
|
|
sides[1] = surroundings[x][y][z + 1]
|
|
sides[2] = surroundings[x + 1][y][z]
|
|
sides[3] = surroundings[x][y][z - 1]
|
|
local adjacentSides = 0
|
|
for n = 0, 3 do
|
|
if sides[n] > 0 then
|
|
adjacentSides = adjacentSides + 1
|
|
end
|
|
end
|
|
local sidesAbove = {} --sides is an array of the material of the 4 adjacent sides 1 height above
|
|
sidesAbove[0] = surroundings[x - 1][y + 1][z]
|
|
sidesAbove[1] = surroundings[x][y + 1][z + 1]
|
|
sidesAbove[2] = surroundings[x + 1][y + 1][z]
|
|
sidesAbove[3] = surroundings[x][y + 1][z - 1]
|
|
local adjacentSidesAbove = 0
|
|
for n = 0, 3 do
|
|
if sidesAbove[n] > 0 then
|
|
adjacentSidesAbove = adjacentSidesAbove + 1
|
|
end
|
|
end
|
|
local corners = {} --corners is an array of the material of the 4 adjacent corners
|
|
corners[0] = surroundings[x - 1][y][z - 1]
|
|
corners[1] = surroundings[x - 1][y][z + 1]
|
|
corners[2] = surroundings[x + 1][y][z + 1]
|
|
corners[3] = surroundings[x + 1][y][z - 1]
|
|
local adjacentCorners = 0
|
|
for n = 0, 3 do
|
|
if corners[n] > 0 then
|
|
adjacentCorners = adjacentCorners + 1
|
|
end
|
|
end
|
|
local cornersAbove = {} --corners is an array of the material of the 4 adjacent corners 1 height above
|
|
cornersAbove[0] = surroundings[x - 1][y + 1][z - 1]
|
|
cornersAbove[1] = surroundings[x - 1][y + 1][z + 1]
|
|
cornersAbove[2] = surroundings[x + 1][y + 1][z + 1]
|
|
cornersAbove[3] = surroundings[x + 1][y + 1][z - 1]
|
|
local adjacentCornersAbove = 0
|
|
for n = 0, 3 do
|
|
if cornersAbove[n] > 0 then
|
|
adjacentCornersAbove = adjacentCornersAbove + 1
|
|
end
|
|
end
|
|
--determine what type of wedge to make
|
|
local material = nil
|
|
local wedge = nil
|
|
local rotation = nil
|
|
if defaultmaterial then
|
|
material = defaultmaterial
|
|
else
|
|
material, wedge, rotation = c:GetCell(x, y, z) --start with the existing material, wedge, and rotation
|
|
end
|
|
wedge = 0 --default wedge is a block
|
|
rotation = 0 --default rotation is 0
|
|
--type 1: 45 degree ramp //must not have a block on top and must have a block under, and be surrounded by 1 side; or 3 sides and the 2 corners between them
|
|
if surroundings[x][y + 1][z] == 0 and surroundings[x][y - 1][z] > 0 then
|
|
if adjacentSides == 1 then
|
|
for n = 0, 3 do
|
|
if sides[n] > 0 then
|
|
wedge = 1
|
|
rotation = (n + 1) % 4
|
|
c:SetCell(x, y, z, material, wedge, rotation)
|
|
return true
|
|
end
|
|
end
|
|
elseif adjacentSides == 3 then
|
|
for n = 0, 3 do
|
|
if sides[n] > 0 and corners[(n + 1) % 4] > 0 and sides[(n + 1) % 4] > 0 and corners[(n + 2) % 4] > 0 and sides[(n + 2) % 4] > 0 then
|
|
wedge = 1
|
|
rotation = (n + 2) % 4
|
|
c:SetCell(x, y, z, material, wedge, rotation)
|
|
return true
|
|
end
|
|
end
|
|
end
|
|
end
|
|
--type 2: 45 degree corner //must not have a block on top and must have a block under, and be surrounded by 2 sides and the 1 corner between them; or 3 sides and 1 corner between 2 of them (facing towards that corner)
|
|
if surroundings[x][y + 1][z] == 0 and surroundings[x][y - 1][z] > 0 then
|
|
for n = 0, 3 do
|
|
if sides[n] > 0 and corners[(n + 1) % 4] > 0 and sides[(n + 1) % 4] > 0 and (adjacentSides == 2 or (adjacentSides == 3 and (corners[(n + 3) % 4] > 0 or (sides[(n + 2) % 4] > 0 and corners[(n + 2) % 4] > 0) or (sides[(n + 3) % 4] > 0 and corners[n] > 0)))) then
|
|
wedge = 2
|
|
rotation = (n + 2) % 4
|
|
c:SetCell(x, y, z, material, wedge, rotation)
|
|
return true
|
|
end
|
|
end
|
|
end
|
|
--type 3: 45 degree inverse corner //surrounded by three sides or 4 sides and 3 corners, with nothing above or else a block on top surrounded on 2 sides and the corner between them
|
|
if adjacentSides == 3 and surroundings[x][y + 1][z] > 0 then
|
|
if adjacentCorners > 1 then
|
|
for n = 0, 3 do
|
|
if (corners[n] == 0 or cornersAbove[n] == 0) and (sides[(n - 1) % 4] == 0 or sides[n] == 0) and (sidesAbove[n] == 0 and sidesAbove[(n + 1) % 4] > 0 and sidesAbove[(n + 2) % 4] > 0 and sidesAbove[(n + 3) % 4] == 0) then
|
|
wedge = 3
|
|
rotation = (n + 3) % 4
|
|
c:SetCell(x, y, z, material, wedge, rotation)
|
|
return true
|
|
end
|
|
end
|
|
end
|
|
elseif adjacentSides == 4 and adjacentCorners == 3 then
|
|
for n = 0, 3 do
|
|
if corners[n] == 0 and (surroundings[x][y + 1][z] == 0 or (sidesAbove[n] == 0 and sidesAbove[(n + 1) % 4] > 0 and cornersAbove[(n + 2) % 4] > 0 and sidesAbove[(n + 2) % 4] > 0 and sidesAbove[(n + 3) % 4] == 0)) then
|
|
wedge = 3
|
|
rotation = (n + 3) % 4
|
|
c:SetCell(x, y, z, material, wedge, rotation)
|
|
return true
|
|
end
|
|
end
|
|
end
|
|
--type 4: half a cube, as if it were cut diagonally from front to back //surrounded by 2 sides
|
|
if adjacentSides == 2 and adjacentCorners < 4 then
|
|
for n = 0, 3 do
|
|
if sides[n] == 0 and sides[(n + 1) % 4] == 0 and (surroundings[x][y + 1][z] == 0 or (sidesAbove[n] == 0 and sidesAbove[(n + 1) % 4] == 0 and sidesAbove[(n + 2) % 4] > 0 and sidesAbove[(n + 3) % 4] > 0)) then
|
|
wedge = 4
|
|
rotation = n
|
|
c:SetCell(x, y, z, material, wedge, rotation)
|
|
return true
|
|
end
|
|
end
|
|
end
|
|
c:SetCell(x, y, z, material, wedge, rotation)
|
|
return false
|
|
end
|
|
|
|
|
|
function onClicked(mouse)
|
|
if on then
|
|
|
|
c = game.Workspace.Terrain
|
|
|
|
local cellPos = c:WorldToCellPreferEmpty(Vector3.new(mouse.Hit.x, mouse.Hit.y, mouse.Hit.z))
|
|
local x = cellPos.x
|
|
local y = cellPos.y
|
|
local z = cellPos.z
|
|
|
|
c:SetCell(x, y, z, 1, 0, 0)
|
|
for i = x - 1, x + 1 do
|
|
for j = y - 1, y + 1 do
|
|
for k = z - 1, z + 1 do
|
|
MakeWedge(i, j, k)
|
|
end
|
|
end
|
|
end
|
|
print("Block built at: "..x..", "..y..", "..z)
|
|
end
|
|
end
|
|
|
|
function On()
|
|
self:Activate(true)
|
|
toolbarbutton:SetActive(true)
|
|
on = true
|
|
end
|
|
|
|
function Off()
|
|
toolbarbutton:SetActive(false)
|
|
on = false
|
|
end
|
|
|
|
|
|
|
|
|
|
------
|
|
--GUI-
|
|
------
|
|
|
|
--screengui
|
|
g = Instance.new("ScreenGui", game:GetService("CoreGui"))
|
|
|
|
|
|
|
|
|
|
--------------------------
|
|
--SUCCESSFUL LOAD MESSAGE-
|
|
--------------------------
|
|
loaded = true
|
|
print("Builder Plugin Loaded") |