void CLocationCriteriaTableEntry::InitSelector (SSelectorInitCtx &InitCtx, CCompositeImageSelector *retSelector) // InitSelector // // Initializes the selector { int i; // Generate the table based on criteria and location TProbabilityTable<int> ProbTable; for (i = 0; i < m_Table.GetCount(); i++) { // Compute the probability of this entry at the // given location. int iChance = m_Table[i].Criteria.CalcLocationWeight(InitCtx.pSystem, InitCtx.sLocAttribs, InitCtx.vObjPos); if (iChance > 0) ProbTable.Insert(i, iChance); } // If none match, then add the default element if (ProbTable.GetCount() == 0 && m_iDefault != -1) ProbTable.Insert(m_iDefault, 1000); // Roll if (ProbTable.GetCount() > 0) { int iRoll = ProbTable.RollPos(); int iIndex = ProbTable[iRoll]; retSelector->AddVariant(GetID(), iIndex); m_Table[iIndex].pImage->InitSelector(InitCtx, retSelector); } }
CSoundType *CSoundtrackManager::CalcGameTrackToPlay (const CString &sRequiredAttrib) // CalcGameTrackToPlay // // Calculates a game track to play. { int i; CSystem *pSystem = g_pUniverse->GetCurrentSystem(); // Create a probability table of tracks to play. TSortMap<int, TProbabilityTable<CSoundType *>> Table(DescendingSort); for (i = 0; i < g_pUniverse->GetSoundTypeCount(); i++) { CSoundType *pTrack = g_pUniverse->GetSoundType(i); // If this is not appropriate music then skip it if (!pTrack->HasAttribute(sRequiredAttrib)) continue; // If this is a system track and we've already played a // system track, then skip it. else if (m_bSystemTrackPlayed && pTrack->HasAttribute(ATTRIB_SYSTEM_SOUNDTRACK)) continue; // Calculate the chance for this track based on location // criteria. int iChance = (pSystem ? pSystem->CalcMatchStrength(pTrack->GetLocationCriteria()) : 1000); if (iChance == 0) continue; // Adjust probability based on when we last played this tack. switch (GetLastPlayedRank(pTrack->GetUNID())) { case 0: iChance = 0; break; case 1: iChance = iChance / 10; break; case 2: iChance = iChance / 5; break; case 3: case 4: iChance = iChance / 3; break; case 5: case 6: case 7: iChance = iChance / 2; break; case 8: case 9: case 10: iChance = 2 * iChance / 3; break; } if (iChance == 0) continue; // Add to the probability table TProbabilityTable<CSoundType *> *pEntry = Table.SetAt(pTrack->GetPriority()); pEntry->Insert(pTrack, iChance); } // If the table is empty, then there is nothing to play. if (Table.GetCount() == 0) return NULL; // Otherwise, roll out of the first table. TProbabilityTable<CSoundType *> &Entry = Table[0]; return Entry.GetAt(Entry.RollPos()); }