Page 1 of 1

What algorithm could I use for fire

Posted: Tue Jul 06, 2021 1:11 pm
by Gunroar:Cannon()
Jsut wondering which algorithm is used for fire and gas in roguelikes? A*?

Re: What algorithm could I use for fire

Posted: Tue Jul 06, 2021 10:38 pm
by togFox
as in fire spreading and growing on a tiled map?

I roll my own based on random probabilities along 4 axis or 8 axis adjusted by the degree of flammable material in each tile.

Re: What algorithm could I use for fire

Posted: Wed Jul 07, 2021 12:51 pm
by Gunroar:Cannon()
Thnx, and yes fire spreading on a roguelike. Does that also work for water?

Re: What algorithm could I use for fire

Posted: Wed Jul 07, 2021 11:23 pm
by togFox
Since I'm literally (re)writing my fire routine, here is my pseudo/code I call from love.update(dt):

Code: Select all

function FireGrowAndSpread()
-- check if each tile has a fire and if so, check if the fire grows and then check if it spreads to a neighbouring tile
-- firesize = the intensity of the fire within that tile from 0 -> 100.  A small fire won't spread until it becomes a larger fire.
-- firetime = the time (dt) since the fire last spread. We need to cap this so fires don't instantly grow to 100% take over the whole map.

	local firegrowspeed = 10	-- how fast a fire can grow on each tile (up to 100 (%))
	local growthreshold = 50	-- a fire that is size 50 (%) has the potential to spread
	local spreadchance = 50 	-- the chance of a fire spreading (if greater than growthreshold)
	local firetimer = 5			-- the frequency that fire growth and spread should be checked (in seconds)
	local newfiresize = 20		-- the size new fires start at (%)

	for each row
		for each col
			get firesize for current tile
			if firesize > 0 and firetime <= 0 then
				-- increse the firesize on this tile
				firesize = firesize + love.math.random(1,firegrowspeed)
				if firesize > 100 then firesize = 100
				firetime = firetimer	
				
				-- see if fire spreads to another tile
				if firesize >= growthreshold then
					if love.math.random(0,100) <= spreadchance then
						-- fire spreads
						-- determine direction
						local intRowDirection = love.math.random(-1,1)  -- this could result in 0,0 and that's okay (freebie)
						local intColDirection = love.math.random(-1,1)
						
						-- determine tile
						local newfiretilerow = row + intRowDirection
						local newfiretilecol = col + intColDirection
						
						-- check if new tile is a wall segment (walls don't catch fire)
						if newtile.tiletype = enum.tileNormal then
							-- if this new tile has no fire then start one
							if newtile.firesize < newfiresize then
								newtile.firesize = newfiresize
							end
						end
					end
				end
			end
		end
	end
end
You'll need to make it work for yourself and pass parameters etc but the above displays a natural fire spread that can get out of control if players don't react correctly. One day I'll expand it to naturally die down after a period of time. Shouldn't be hard.

Water is a completely different beast as the expectation is that will flow uniformly and not randomly. Not something I've needed to address just yet.

Re: What algorithm could I use for fire

Posted: Thu Jul 08, 2021 9:02 am
by Gunroar:Cannon()
Yeah, that's what I'm talking about :ultrahappy: . It's strange because I can never find water/gas/fire tutorials or anything like that online. FOV? Sure. Pathfinding? Yep? Hunger clocks? Always. But the most I've heard about implementing those others is cellular automaton. Thanks.