What algorithm could I use for fire

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

What algorithm could I use for fire

Post by Gunroar:Cannon() »

Jsut wondering which algorithm is used for fire and gas in roguelikes? A*?
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
togFox
Party member
Posts: 770
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: What algorithm could I use for fire

Post 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.
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: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: What algorithm could I use for fire

Post by Gunroar:Cannon() »

Thnx, and yes fire spreading on a roguelike. Does that also work for water?
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
togFox
Party member
Posts: 770
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: What algorithm could I use for fire

Post 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.
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: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: What algorithm could I use for fire

Post 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.
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: No registered users and 54 guests