#merging blocks end up overlapping

53 messages · Page 1 of 1 (latest)

somber stirrup
#

im working on a script to merge blocks that are the same colour, they currently merge in invalid spots though, any ideas on how i could implement a check for this in my --y code?
relevant snippet:

    -- x
    for x = 2, GRIDSIZE do
        for z = 1, GRIDSIZE do
            for y, size in pairs(sizes[x][z]) do

            if sizes[x - 1][z][y] == nil then continue end
            if size.X ~= sizes[x - 1][z][y].X then continue end
            if blocks[x][z][y].Colour ~= blocks[x - 1][z][y].Colour then continue end

            sizes[x][z][y].X += sizes[x-1][z][y].X
            sizes[x-1][z][y] = nil
            end
        end
    end

    -- z
    for x = 1, GRIDSIZE do
        for z = 2, GRIDSIZE do
            for y, size in pairs(sizes[x][z]) do
            
            if sizes[x][z-1][y] == nil then continue end
            if size.Z ~= sizes[x][z-1][y].Z then continue end
            if blocks[x][z].Colour ~= blocks[x][z - 1][y].Colour then continue end

            sizes[x][z][y].Z += sizes[x][z - 1][y].Z
            sizes[x][z - 1][y] =  nil
            end
        end
    end
    
    
-- y
for x = 1, GRIDSIZE do
    for z = 1, GRIDSIZE do
        for y, size in pairs(sizes[x][z]) do
            if sizes[x][z][y-1] == nil then continue end
            if size.Y ~= sizes[x][z][y-1].Y then continue end
            if blocks[x][z][y].Colour ~= blocks[x][z][y-1].Colour then continue end

                sizes[x][z][y].Y = sizes[x][z][y].Y + sizes[x][z][y-1].Y
                sizes[x][z][y-1] = nil
                end
            end
        end
#

representation of whats happening:

thin harness
#

wazap

#

interesting problem u got here

#

I'm a little confused on the goal

#

well I know the goal

#

I'm a little confused on what each block of code is supposed to do

#

you're like adding the axis coordinates together

#

and setting one of them to nil

somber stirrup
#

each block iterates over a grid in one direction, if the requirements are met it merges them, it sets the one it merged with to nil so it cannot be merged with any other part

thin harness
#

also do you want corner pieces to be possible does it only merge one time?

#

in the example you gave you said it tries to merge with this and merges with the brown one too which you don't want

#

should it not even try?

somber stirrup
#

i want the corner pieces to be possible, i dont want them to overlap with others though

somber stirrup
somber stirrup
thin harness
#

oh my

#

okay so

#

I think you definitely need some functions just to make this more readable. The problem you're having is when a part has 2 block wide and it finds a match it just adds onto one axis. In order to get corner pieces we kind of NEED the parts to overlap so here's what I'm thinking. Every merged part is at most 2 blocks wide and each part will expand backwards and invalidate the original. Let me draw a diagram

somber stirrup
#

wdym with expand backwards

thin harness
#

like actually scaling the part we're overlapping the neighbor with the original

#

never modifying the one we're actually checking

#
local sizes = {}
local blocks = {}
local GRIDSIZE = 10

local directions = {
    Vector3.new(0, 1, 0), -- up
    Vector3.new(0, 1, 0), -- down
    Vector3.new(-1, 0, 0), -- left
    Vector3.new(1, 0, 0), -- right
    Vector3.new(0, 0, -1), -- back
    Vector3.new(0, 0, 1), -- forwards
}

local function merge(x1, y1, z1)
    local cur = blocks[x1][y1][z1]

    for _, dir in directions do
        if
            not (blocks[x1 + dir.X] and blocks[x1 + dir.X][y1 + dir.Y] and blocks[x1 + dir.X][y1 + dir.Y][z1 + dir.Z])
        then
            continue
        end

        local targetNeighbor = blocks[x1 + dir.X][y1 + dir.Y][z1 + dir.Z]

        if targetNeighbor.Colour == cur.Colour then
            targetNeighbor.Position = (cur.Position + targetNeighbor.Position) / 2
            targetNeighbor.Size += dir * -1
            break
        end
    end
    blocks[x1][y1][z1] = nil
end

#

do something like this on every single block

somber stirrup
#

okay, so what youre doing is adding a direction from the directions table to increase the size of a block?

thin harness
#

yeah in the opposite direction

#

because if the target is in that direction that would mean current relative to that is in the negative dir

#

so actually

#

i shouldn't be reducing the size ur right

#
local sizes = {}
local blocks = {}
local GRIDSIZE = 10

local directions = {
    Vector3.new(0, 1, 0), -- up
    Vector3.new(0, 1, 0), -- down
    Vector3.new(-1, 0, 0), -- left
    Vector3.new(1, 0, 0), -- right
    Vector3.new(0, 0, -1), -- back
    Vector3.new(0, 0, 1), -- forwards
}

local function merge(x1, y1, z1)
    local cur = blocks[x1][y1][z1]

    for _, dir in directions do
        if
            not (blocks[x1 + dir.X] and blocks[x1 + dir.X][y1 + dir.Y] and blocks[x1 + dir.X][y1 + dir.Y][z1 + dir.Z])
        then
            continue
        end

        local targetNeighbor = blocks[x1 + dir.X][y1 + dir.Y][z1 + dir.Z]

        if targetNeighbor.Colour == cur.Colour then
            targetNeighbor.Position = (cur.Position + targetNeighbor.Position) / 2
            targetNeighbor.Size += Vector3.new(math.abs(dir.X), math.abs(dir.Y), math.abs(dir.Z))
            break
        end
    end
    blocks[x1][y1][z1] = nil
end

#

there i added absolute value

#

the position part should handle the rest

somber stirrup
#

im just a bit confused on what this changes except for changing the size of its neighbour instead of itself

thin harness
#

and thats exactly what makes it work

#

because if a part needs to expand into multiple directions it can't scale the main part

#

or it'd make a like 2x2 instead of a corner piece

somber stirrup
thin harness
#

oh maybe ur right

#

okay new solution

somber stirrup
#

what i have right now is a bit messy, but it mostly works

#

i just need to implement some small changes

#

heres what it currently generates

thin harness
#

yeah that doesn't make corner pieces tho

somber stirrup
#

you cant do that with a single part

#

youll always need 2 parts to merge around a corner

thin harness
#

right but u can just overlap them if u wanted to

somber stirrup
#

that wont do much though

#

im just looking to increase performance

#

the visual effect of that wont be noticeable if i remove the offset between each part

thin harness
#

alright well I I think your problem would be a little more manageable anyways if u made it into a function one thing you can do is have each coordinate refer to a table that has the coordinates of each part in it individually that you need to merge and then after you do a first pass to find every group you do another pass to calculate the biggest possible part that would fit in the space from the cluster of coordinates you have.

#

i g2g rn but i might show u when i get back