[solved] Floodfill provided for those that need it

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
togFox
Party member
Posts: 774
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

[solved] Floodfill provided for those that need it

Post by togFox »

@Gunroar:Cannon() was asking how to make water flow and since I needed a way to floodfill my spaceship (with vacuum) I thought I'd give this a shot. This function is a traditional floodfill function - meaning - it will fill up a room instantly. To get slowly flowing water spreading across a room then suggest you apply some dt in a loop:

Code: Select all

function randomfunctions.floodfill(map, row, col, roomID)
 -- tile is a single element in map i.e tile = map[row][col]
 -- roomID is the number that is to be assigned to the tile if it is deemed to be in the floodfill
 
    local Q = {}                -- a queue to help recursion
    local tile = map[row][col]    -- the initial tile (starting point)
    
    table.insert(Q,map[row][col])
	
 	repeat
		local newtile                -- this is a neighbouring tile
		local tilerow = Q[1].row
		local tilecol = Q[1].col
		Q[1].roomID = roomID
	
		-- check tiles around this one and add them if they are not walls or doors
		-- check north,s,e,w
		newtile = map[tilerow - 1][tilecol]
		if newtile.tiletype == enum.tileNormal and newtile.roomID == 0 then table.insert(Q, newtile) end
		
		newtile = map[tilerow + 1][tilecol]
		if newtile.tiletype == enum.tileNormal and newtile.roomID == 0 then table.insert(Q, newtile) end
		
		newtile = map[tilerow][tilecol - 1]
		if newtile.tiletype == enum.tileNormal and newtile.roomID == 0 then table.insert(Q, newtile) end
		
		newtile = map[tilerow][tilecol + 1]
		if newtile.tiletype == enum.tileNormal and newtile.roomID == 0 then table.insert(Q, newtile) end        

		table.remove(Q,1)
	until #Q < 1
    
end
It's not clean code (the input parameters are terrible!!) and it sets roomID to a number value but you could easily change it so that it sets something else to something else.
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
User avatar
Gunroar:Cannon()
Party member
Posts: 1088
Joined: Thu Dec 10, 2020 1:57 am

Re: [solved] Floodfill provided for those that need it

Post by Gunroar:Cannon() »

WAIT :brows: ... I'm Gunroar:Cannon() :o . Yeah, togs, I'll use it, thnx :awesome: :up: (for thumbs up, but that's not here :( )

Edit: woah that's strange, I typed :*up: (without the star) and then that signal appeared, but I don't see it as an opition :halloween: I must find more :sign: :look: :fly:...:down: :left: :right: :a: :ab: :power: :cry:...yes it worked...oh no, that one is already there :( .
(PS: I don't press preview)

Edit - edit: Woah. I had no idea :*a: and :*ab: would work (when I typed yes, it worked, was talking about :*cry:) , meant it as a reference to combos/cheats(up-up-down-down-left-right-a-b(?))...hmmm let see...:m: :e: ... :b: :brows: :P
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 137 guests