This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
LeDark Lua's profile picture

Grid based tiling?

Started by LeDark Lua, 18 August 2015 - 09:47 AM
LeDark Lua #1
Posted 18 August 2015 - 11:47 AM
Yup i know its possible, but how to make rectangle grids in which the tiles could be placed. These grids are self aware when they are colliding with other tile and or out of the screen bounds.

I cant think of any good ideas/formulas to make that grid based tiling so I don't have code.

The for ex: 4x4 tile basing is easy or any of those. But 4x6, 1x4, 5x2, … I cant think of what to do.
Bomb Bloke #2
Posted 18 August 2015 - 03:05 PM
Not exactly sure what you're asking, here… are you wanting to create a tiled pattern, filling as much space in the grid as possible, with randomly sized shapes? Or are you simply wanting to allow placement where ever, so long as no tiles overlap? More details on your final goal may be helpful here - when are the tiles being placed, do they ever move, etc…
LeDark Lua #3
Posted 18 August 2015 - 03:22 PM
tiles cant move. and placement is where ever in X axis.
COOLGAMETUBE #4
Posted 18 August 2015 - 03:28 PM
tiles cant move. and placement is where ever in X axis.

Shoud they move but can't do it

Or shoudn't they move as Information?
Exerro #5
Posted 18 August 2015 - 03:35 PM
Grid based tiling is easy as long as each tile is the same width/height.


local displayWidth, displayHeight = term.getSize()
local tileWidth = 5 -- the width of a tile
local tileHeight = 3 -- the height of a tile
local xMargin = 1 -- the horizontal offset
local yMargin = 1 -- the vertical offset
local xPadding = 1 -- the padding between tiles horizontally
local yPadding = 1 -- the padding between tiles vertically

local columns = math.floor( ( displayWidth - 2 * xMargin + xPadding ) / ( tileWidth + xPadding ) )
local rows = math.floor( ( displayHeight - 2 * yMargin + yPadding ) / ( tileHeight + yPadding ) )

for n = 1, columns do
    for m = 1, rows do
        local tileX = xMargin + ( n - 1 ) * ( tileWidth + xPadding )
        local tileY = yMargin + ( m - 1 ) * ( tileHeight + yPadding )
    end
end

That should do it.

If you're having multiple sized tiles, however, I have absolutely no idea. It depends on how you want them to stack, the degree of different sizing (i.e. rounded to 2 pixels or literally anything?), and whether the order of tiles matters.
LeDark Lua #6
Posted 18 August 2015 - 04:08 PM

term.width, term.heigth = term.getSize()
local tileRows = {currTile=1;}
for i=1,19 do
tileRows[i]={maxWidth=term.width;filledWidth=1;y=i;tiles={};combinedWidth=1;};
end
local icons={}
local event={}

function createIcon( name )
local newT = {}
newT.x=1
newT.y=1
newT.name=name
newT.width=#name
function newT:draw()
term.setCursorPos(self.x, self.y)
term.write(self.name)
end
icons[#icons+1]=newT
return newT
end

function updateIcons()
for i=1, #icons do
icons[i]:draw()
end
end

function autoPlaceTiles()
for i=1, #icons do
if tileRows[tileRows.currTile].maxWidth < tileRows[tileRows.currTile].filledWidth+(icons[i].width+1) then
tileRows.currTile=tileRows.currTile+1
end
if tileRows[tileRows.currTile].maxWidth > tileRows[tileRows.currTile].filledWidth then
icons[i].x=tileRows[tileRows.currTile].filledWidth
icons[i].y=tileRows[tileRows.currTile].y
tileRows[tileRows.currTile].combinedWidth = tileRows[tileRows.currTile].combinedWidth + icons[i].width
tileRows[tileRows.currTile].tiles[i]=icons[i]
tileRows[tileRows.currTile].filledWidth=tileRows[tileRows.currTile].filledWidth+(icons[i].width+1)
end
end
end

function repositionTiles()
for i=1, tileRows.currTile do
--Repositioning with combinedWidth/2 and positionig like that
end
end

for i=1, 95 do
createIcon( "|Test: "..i.."|" )
end

autoPlaceTiles()

repositionTiles()

while true do
term.clear()
updateIcons()
event = {os.pullEvent()}
updateIcons()
end

this is what I have right now. It works fine but I need to make so it positions itself in the middle if there is still room left.
Edited on 18 August 2015 - 02:11 PM
LeDark Lua #7
Posted 18 August 2015 - 05:55 PM
Ok this works jus fine for me, tell me if you agree:

function autoPlaceTiles()
for i=1, #term.icons do
if tileRows[tileRows.currTile].maxWidth < tileRows[tileRows.currTile].filledWidth+term.icons[i].width-2 then
tileRows.currTile=tileRows.currTile+1
end
if tileRows[tileRows.currTile].maxWidth > tileRows[tileRows.currTile].filledWidth-2 then
tileRows[tileRows.currTile].tiles[#tileRows[tileRows.currTile].tiles + 1]=term.icons[i]
term.icons[i].x=tileRows[tileRows.currTile].filledWidth
term.icons[i].y=tileRows[tileRows.currTile].y
term.icons[i].oldY=tileRows[tileRows.currTile].y
tileRows[tileRows.currTile].combinedWidth = tileRows[tileRows.currTile].combinedWidth + term.icons[i].width
tileRows[tileRows.currTile].filledWidth=tileRows[tileRows.currTile].filledWidth+(term.icons[i].width+1)
end
end
end

function repositionTiles()
for i=1, tileRows.currTile do
local addX = term.width - tileRows[i].combinedWidth-8
local tiles = tileRows[i].tiles
for j=1, #tiles do
tiles[j].x = tiles[j].x + math.floor(addX/2)
end
end
end