for <@830057380359438396>
Posted by: sovde from the skUnity Discord. View the thread on skUnity Discord here
Posted by: sovde from the skUnity Discord. View the thread on skUnity Discord here
Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.
If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the community!
Now, what are you waiting for? Join the community now!
applescript
on load:
set {-vector1} to vector(-0.5,0,-0.5)
set {-vector2} to vector(0.5,0,-0.5)
set {-vector3} to vector(0.5,0,0.5)
set {-vector4} to vector(-0.5,0,0.5)
function getOutermostCorners(chunks: chunks) :: locations:
set {_world} to world of {_chunks::1}
# get all unique edges, parse them into locations
# and set up a map of valid edges
loop getUniqueEdges({_chunks::*}):
set {_i} to 0
loop split loop-value by ";":
set {_coords::*} to split loop-value-2 by "|"
set {_x} to {_coords::1} parsed as number
set {_z} to {_coords::2} parsed as number
set {_parsed} to location({_x}, 0, {_z}, {_world})
add 1 to {_i}
set {_parsed-corners::%{_i}%} to {_parsed}
set {_parsed-edges::%{_parsed-corners::1}%-%{_parsed-corners::2}%} to {_parsed-corners::1}
set {_parsed-edges::%{_parsed-corners::2}%-%{_parsed-corners::1}%} to {_parsed-corners::2}
# order CCW
set {_offset::1} to vector(0,0,16)
set {_offset::2} to vector(-16,0,0)
set {_offset::3} to vector(0,0,-16)
set {_offset::4} to vector(16,0,0)
set {_current} to first element of {_parsed-edges::*}
set {_corners::0} to {_current}
loop size of {_parsed-edges::*} / 2 times:
# look in SWNE order
loop {_offset::*}:
# get prospective corner
set {_prospective} to {_current} ~ loop-value-2
# if we have an edge to it, it's the next corner
# otherwise, keep looking
if {_parsed-edges::%{_current}%-%{_prospective}%} is set:
# delete edges we've used
delete {_parsed-edges::%{_current}%-%{_prospective}%}
delete {_parsed-edges::%{_prospective}%-%{_current}%}
# add corner to list and move to next corner
set {_corners::%loop-value-1%} to {_prospective}
set {_current} to {_prospective}
exit 1 loop
return {_corners::*}
function getUniqueEdges(chunks: chunks) :: strings:
loop {_chunks::*}:
loop getEdges(loop-value):
# check for duplicate edge
if {_edges::%loop-value-2%} is set:
delete {_edges::%loop-value-2%}
continue
# check for duplicate reverse edge
set {_corners::*} to split loop-value-2 by ";"
set {_reverse-edge} to join reversed {_corners::*} with ";"
if {_edges::%{_reverse-edge}%} is set:
delete {_edges::%{_reverse-edge}%}
continue
# store edge
set {_edges::%loop-value-2%} to loop-value-2
return {_edges::*}
function getEdges(chunk: chunk) :: strings:
set {_corners::1} to block at 0,0,0 of {_chunk} ~ {-vector1}
set {_corners::2} to block at 15,0,0 of {_chunk} ~ {-vector2}
set {_corners::3} to block at 15,0,15 of {_chunk} ~ {-vector3}
set {_corners::4} to block at 0,0,15 of {_chunk} ~ {-vector4}
# add all edges in x|z;x|z format
loop {_corners::*}:
add 1 to {_i}
set {_next} to {_corners::%mod({_i}, 4) + 1%}
set {_edges::%loop-index%} to "%x coord of loop-value%|%z coord of loop-value%;%x coord of {_next}%|%z coord of {_next}%"
return {_edges::*}
So getUniqueEdges will give you every single boundary edge, be it a connected shape, an inner or outer boundary, or whatever
it doesn't care, and it doesn't order anything
it'll just give you all the data
Then in getOutermostCorners we have basically two things happening
1) We take the output of getUniqueEdges, turn it into actual locations, and create a list of valid edges
applescript
# get all unique edges, parse them into locations
# and set up a map of valid edges
loop getUniqueEdges({_chunks::*}):
set {_i} to 0
loop split loop-value by ";":
set {_coords::*} to split loop-value-2 by "|"
set {_x} to {_coords::1} parsed as number
set {_z} to {_coords::2} parsed as number
set {_parsed} to location({_x}, 0, {_z}, {_world})
add 1 to {_i}
set {_parsed-corners::%{_i}%} to {_parsed}
set {_parsed-edges::%{_parsed-corners::1}%-%{_parsed-corners::2}%} to {_parsed-corners::1}
set {_parsed-edges::%{_parsed-corners::2}%-%{_parsed-corners::1}%} to {_parsed-corners::2}
This is all so we can order it later
parsed-edges is just a set of valid edges (see the indices?)
we make the value a location just for ease of getting started, it's not that necessary
the indices are the important part
So then this code does the actual ordering of the corners
applescript
# order CCW
set {_offset::1} to vector(0,0,16)
set {_offset::2} to vector(-16,0,0)
set {_offset::3} to vector(0,0,-16)
set {_offset::4} to vector(16,0,0)
set {_current} to first element of {_parsed-edges::*}
set {_corners::0} to {_current}
loop size of {_parsed-edges::*} / 2 times:
# look in SWNE order
loop {_offset::*}:
# get prospective corner
set {_prospective} to {_current} ~ loop-value-2
# if we have an edge to it, it's the next corner
# otherwise, keep looking
if {_parsed-edges::%{_current}%-%{_prospective}%} is set:
# delete edges we've used
delete {_parsed-edges::%{_current}%-%{_prospective}%}
delete {_parsed-edges::%{_prospective}%-%{_current}%}
# add corner to list and move to next corner
set {_corners::%loop-value-1%} to {_prospective}
set {_current} to {_prospective}
exit 1 loop
applescript
function drawOutlines(chunks: chunks) :: number:
set {_world} to world of {_chunks::1}
# get all unique edges, parse them into locations
# and set up a map of valid edges
loop getUniqueEdges({_chunks::*}):
set {_i} to 0
loop split loop-value by ";":
set {_coords::*} to split loop-value-2 by "|"
set {_x} to {_coords::1} parsed as number
set {_z} to {_coords::2} parsed as number
set {_parsed} to location({_x}, 0, {_z}, {_world})
add 1 to {_i}
set {_parsed-corners::%{_i}%} to {_parsed}
set {_parsed-edges::%{_parsed-corners::1}%-%{_parsed-corners::2}%} to {_parsed-corners::1}
set {_parsed-edges::%{_parsed-corners::2}%-%{_parsed-corners::1}%} to {_parsed-corners::2}
# order CCW
set {_offset::1} to vector(0,0,16)
set {_offset::2} to vector(-16,0,0)
set {_offset::3} to vector(0,0,-16)
set {_offset::4} to vector(16,0,0)
set {_current} to first element of {_parsed-edges::*}
set {_corners::0} to {_current}
while true is true:
add 1 to {_iterations}
set {_found} to false
# look in SWNE order
loop {_offset::*}:
# get prospective corner
set {_prospective} to {_current} ~ loop-value
# if we have an edge to it, it's the next corner
# otherwise, keep looking
if {_parsed-edges::%{_current}%-%{_prospective}%} is set:
# delete edges we've used
delete {_parsed-edges::%{_current}%-%{_prospective}%}
delete {_parsed-edges::%{_prospective}%-%{_current}%}
# add corner to list and move to next corner
set {_corners::%{_iterations}%} to {_prospective}
set {_current} to {_prospective}
set {_found} to true
exit 1 loop
if {_iterations} > 1000:
broadcast "too many iterations"
return -1
{_found} is false
# if we get here, we've looped around and found no corner
# this means we've found the last corner of this shape
# so we'll draw the shape and start a new one
drawBoundary({_corners::*})
add size of {_corners::*} to {_total}
clear {_corners::*}
# if the list is empty, we're done
# otherwise, get a new corner to start from
if size of {_parsed-edges::*} is 0:
return {_total}
set {_current} to first element of {_parsed-edges::*}
set {_corners::0} to {_current}
broadcast "new corner is %{_current}%"
return {_total}