Level Generation

From Binding of Isaac: Rebirth Wiki
Jump to: navigation, search

Pseudo-Code[edit | edit source]

Some parts of the level generation are based on the following pseudo-code:

--Pseudo lua code for getting the number of rooms for a floor.

NumberOfRooms = Min(20, Rand(0, 1) + 5 + Floor(StageId * 10 / 3))
if CurseOfTheLabyrinth then
 NumberOfRooms = Min(45, Floor(NumberOfRooms * 1.8))
elseif CurseOfTheLost then
 NumberOfRooms += 4
end
if StageId == 12 then --The Void
 NumberOfRooms = 50 + (Rand() % 10)
end
if IsHardDifficulty then
 NumberOfRooms += 2 + Rand(0, 1)
end

--Pseudo lua code for getting the number of deadends a floor must have.

MinDeadEnds = 5
if StageId ~= 1 then
 MinDeadEnds += 1
end
if CurseOfTheLabyrinth then
 MinDeadEnds += 1
end
if StageId == 12 then
	MinDeadEnds += 2
end


--Deadends are sorted by distance from the starting room descending.
--e.g. Boss room is placed in the farthest deadend, super secret is placed in the next farthest deadend

--DequeueDeadend also resizes the deadend. This is important to note because that means most deadends get changed to a 1x1 by the end of this.

PlaceRoom(ROOM_BOSS, DequeueDeadend())
--Super Secret
PlaceRoom(ROOM_SUPERSECRET, DequeueDeadend())
--Shop
if StageId < 7 or (StageId < 9 and HasTrinket(SilverDollar)) and VictoryLap < 3 then
	PlaceRoom(ROOM_SHOP, DequeueDeadend())
end
--Treasure
if StageId < 7 or (StageId < 9 and HasTrinket(BloodyCrown)) then
	PlaceRoom(ROOM_TREASURE, DequeueDeadend())
end
--Dice and Sacrifice
if StageId < 12 then
	local deadend = DequeueDeadend() --resizes the room :(
	if deadend ~= nil then
		local roomType = nil
		if Rng() % 50 == 0 or (Rng() % 5 == 0 and GetNumKeys() > 1) then 
			roomType = ROOM_DICE
		else
			roomType = ROOM_SACRIFICE
		end
		if Rng() % 7 == 0 or (Rng() & 3 == 0 and GetHearts() + GetSoulHearts() >= GetMaxHearts()) then
			PlaceRoom(roomType)
		else
			QueueDeadend(deadend)
		end
	end
end
--Library
local deadend = DequeueDeadend()
if deadend ~= nil then
	if Rng() % 20 == 0 or (Rng() & 3 == 0 and GetStateFlag(STATE_BOOK_PICKED_UP)) then
		PlaceRoom(ROOM_LIBRARY, deadend)
	else
		QueueDeadend(deadend)
	end
end
--Curse
local deadend = DequeueDeadend()
if deadend ~= nil then
	if Rng() & 1 == 0 or (Rng() & 3 == 0 and GetStateFlag(STATE_DEVILROOM_VISITED)) then
		PlaceRoom(ROOM_CURSE, deadend)
	else
		QueueDeadend(deadend)
	end
end
--MiniBoss
local boss = GetMiniBossRoom() --mini boss is picked based on floor, STATE_ULTRAPRIDE_SPAWNED and rng
local deadend = DequeueDeadend()
if deadend ~= nil then
	if Rng() & 3 == 0 or (Rng() & 3 == 0 and StageId ~= 1) then
		PlaceRoom(boss, deadend)
	else
		QueueDeadend(deadend)
	end
end
--Challenge
local deadend = DequeueDeadend()
if deadend ~= nil then
	if (Rng() & 1 == 0 or StageId >= 2) and GetHearts() + GetSoulHearts() >= GetMaxHearts() and StageId > 1 then
		PlaceRoom(boss, deadend)
	else
		QueueDeadend(deadend)
	end
end
--Chest and Arcade
local deadend = DequeueDeadend()
if deadend ~= nil then
	local roomType = nil
	if Rng() % 10 == 0 or (Rng() % 3 == 0 and GetNumKeys() > 1) then 
		roomType = ROOM_CHEST
	else
		roomType = ROOM_ARCADE
	end
	if GetNumCoins() >= 5 and (StageId == 2 or StageId == 4 or StageId == 6 or StageId == 8) then
		PlaceRoom(roomType)
	else
		QueueDeadend(deadend)
	end
end
--Bedroom
if StageId < 7 then
	local deadend = DequeueDeadend()
	if deadend ~= nil then
		local roomType = nil
		if Rng() & 1 == 0 then
			roomType = ROOM_ISAACS
		else
			roomType = ROOM_BARREN
		end
		
		local maxHearts = nil
		if GetPlayerType() == PLAYER_THELOST or GetPlayerType() == PLAYER_XXX or GetPlayerType() == PLAYER_THESOUL then
			maxHearts = GetMaxHearts()
		else
			maxHearts = GetMaxHearts() + GetBoneHearts() * 2
		end
		
		local second = false
		if (GetHearts() < 2 and GetSoulHearts() <= 0) or (maxHearts <= 0 and GetSoulHearts() <= 2) then
			second = true
		end

		if Rng() % 50 == 0 or (Rng() % 5 == 0 and second) then
			PlaceRoom(roomType)
		else
			QueueDeadend(deadend)
		end
	end
end
--Secret
TryPlacingSecret()
if HasTrinket(TRINKET_FRAGMENTED_CARD) then
	TryPlacingSecret()
end
-- Grave Room
if StageId == 11 and StageType == 0 then --Dark Room
	PlaceGrave(DequeueDeadend())
end

This is taken from Blade's GitHub Gist. (Blade is also known as blcd / Will.) He reverse engineered the game using a disassembler in order to create this pseudo-code.

Special Room Selection[edit | edit source]

From the above code, we know that several things affect special room selection. ("Max health" is defined as: red hearts + soul/black hearts >= max red hearts)

  1. Dice Rooms are more likely if the player has 2+ keys.
  2. Dice Rooms & Sacrifice Rooms are more likely if the player has max health.
  3. Dice Rooms & Sacrifice Rooms are mutually exclusive, both cannot be generated on the same floor.
  4. Libraries are more likely if the player has touched 1+ books.
  5. Curse Rooms are more likely if 1+ Devil Rooms have been visited.
  6. Mini-boss Rooms are more likely on floors 2+.
  7. Challenge Rooms only spawn if the player has max health. (But they can't spawn on Basement 1.)
  8. Chest Rooms are more likely if the player has 2+ keys.
  9. Chest Rooms and Arcades only spawn if the player has 5+ coins. Furthermore, they only spawn on floors 2, 4, 6, and 8.
  10. Chest Rooms & Arcades are mutually exclusive, both cannot be generated on the same floor.
  11. Bedrooms are more likely if the player is at "critical" health (meaning 1 total heart or less).