typically used to get a (predefined) random entry point for the action music, but will work on any defined type with entry points, defaults safely to 0.0f if no info available...
| 960 | // defaults safely to 0.0f if no info available... |
| 961 | // |
| 962 | float Music_GetRandomEntryTime( MusicState_e eMusicState ) |
| 963 | { |
| 964 | MusicData_t::iterator itMusicData = MusicData->find( Music_BaseStateToString( eMusicState ) ); |
| 965 | if (itMusicData != MusicData->end()) |
| 966 | { |
| 967 | MusicFile_t &MusicFile = (*itMusicData).second; |
| 968 | |
| 969 | if (MusicFile.MusicEntryTimes.size()) // make sure at least one defined, else default to start |
| 970 | { |
| 971 | // Quake's random number generator isn't very good, so instead of this: |
| 972 | // |
| 973 | // int iRandomEntryNum = Q_irand(0, (MusicFile.MusicEntryTimes.size()-1) ); |
| 974 | // |
| 975 | // ... I'll do this (ensuring we don't get the same result on two consecutive calls, but without while-loop)... |
| 976 | // |
| 977 | static int iPrevRandomNumber = -1; |
| 978 | static int iCallCount = 0; |
| 979 | iCallCount++; |
| 980 | int iRandomEntryNum = (rand()+iCallCount) % (MusicFile.MusicEntryTimes.size()); // legal range |
| 981 | if (iRandomEntryNum == iPrevRandomNumber && MusicFile.MusicEntryTimes.size()>1) |
| 982 | { |
| 983 | iRandomEntryNum += 1; |
| 984 | iRandomEntryNum %= (MusicFile.MusicEntryTimes.size()); |
| 985 | } |
| 986 | iPrevRandomNumber = iRandomEntryNum; |
| 987 | |
| 988 | // OutputDebugString(va("Music_GetRandomEntryTime(): Entry %d\n",iRandomEntryNum)); |
| 989 | |
| 990 | for (MusicEntryTimes_t::iterator itEntryTime = MusicFile.MusicEntryTimes.begin(); itEntryTime != MusicFile.MusicEntryTimes.end(); ++itEntryTime) |
| 991 | { |
| 992 | if (!iRandomEntryNum--) |
| 993 | { |
| 994 | return (*itEntryTime).second; |
| 995 | } |
| 996 | } |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | return 0.0f; |
| 1001 | } |
| 1002 | |
| 1003 | // info only, used in "soundinfo" command... |
| 1004 | // |
no test coverage detected