void CTilegenAction_PlaceComponent::OnBeginGeneration( CLayoutSystem *pLayoutSystem ) { int nNumOptionalRooms = pLayoutSystem->GetRandomInt( m_nMinOptionalRooms, m_nMaxOptionalRooms ); nNumOptionalRooms = MIN( nNumOptionalRooms, m_OptionalRooms.Count() ); nNumOptionalRooms = MAX( nNumOptionalRooms, 0 ); m_RoomsToPlace.RemoveAll(); for ( int i = 0; i < m_MandatoryRooms.Count(); ++ i ) { AddRoomPlacementInstance( pLayoutSystem, &m_MandatoryRooms[i] ); } bool isRoomChosen[m_nMaxTotalOptionalRooms]; Q_memset( isRoomChosen, 0, sizeof( isRoomChosen ) ); // Simplest but probably not the most efficient way to randomly choose N rooms from a list of X rooms. int nNumOptionalRoomsChosen = 0; while ( nNumOptionalRoomsChosen < nNumOptionalRooms ) { int nRoom = pLayoutSystem->GetRandomInt( 0, nNumOptionalRooms - 1 ); if ( !isRoomChosen[nRoom] ) { isRoomChosen[nRoom] = true; AddRoomPlacementInstance( pLayoutSystem, &m_OptionalRooms[nRoom] ); ++ nNumOptionalRoomsChosen; } } }
void CTilegenAction_PlaceComponent::OnBeginGeneration( CLayoutSystem *pLayoutSystem ) { int nNumOptionalRooms = pLayoutSystem->GetRandomInt( m_nMinOptionalRooms, m_nMaxOptionalRooms ); nNumOptionalRooms = MIN( nNumOptionalRooms, m_OptionalRooms.Count() ); nNumOptionalRooms = MAX( nNumOptionalRooms, 0 ); m_RoomsToPlace.RemoveAll(); for ( int i = 0; i < m_MandatoryRooms.Count(); ++ i ) { AddRoomPlacementInstance( pLayoutSystem, &m_MandatoryRooms[i] ); } bool isRoomChosen[m_nMaxTotalOptionalRooms]; Q_memset( isRoomChosen, 0, sizeof( isRoomChosen ) ); // Simplest but probably not the most efficient way to randomly choose N rooms from a list of X rooms. int nNumOptionalRoomsChosen = 0; Msg("Processing random rooms, need to add [%d] rooms. Have [%d] options\n",nNumOptionalRooms, m_OptionalRooms.Count()); Log_Msg(LOG_TilegenLayoutSystem, "Processing random rooms, need to add [%d] rooms. Have [%d] options\n",nNumOptionalRooms, m_OptionalRooms.Count()); while ( nNumOptionalRoomsChosen < nNumOptionalRooms ) { //BUG: int nRoom = pLayoutSystem->GetRandomInt( 0, nNumOptionalRooms - 1 ); //Should choose from all available rooms. int nRoom = pLayoutSystem->GetRandomInt( 0, m_OptionalRooms.Count() -1); if ( !isRoomChosen[nRoom] ) { isRoomChosen[nRoom] = true; AddRoomPlacementInstance( pLayoutSystem, &m_OptionalRooms[nRoom] ); ++ nNumOptionalRoomsChosen; Msg("Room [%d] is id [%d]\n", nNumOptionalRoomsChosen, nRoom); Log_Msg(LOG_TilegenLayoutSystem, "Room [%d] is id [%d]\n", nNumOptionalRoomsChosen, nRoom); } } Msg("Done processing random rooms.\n"); }