radio.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. //******************************************************************************************
  2. //
  3. // Radio.cpp -- File contains the Radio class functions
  4. //
  5. // MechCommander 2
  6. //
  7. //---------------------------------------------------------------------------//
  8. // Copyright (C) Microsoft Corporation. All rights reserved. //
  9. //===========================================================================//
  10. //------------------------------------------------------------------------------------------
  11. // Include Files
  12. #ifndef MCLIB_H
  13. #include "mclib.h"
  14. #endif
  15. #ifndef RADIO_H
  16. #include "radio.h"
  17. #endif
  18. #ifndef GAMESOUND_H
  19. #include "gamesound.h"
  20. #endif
  21. #ifndef MOVER_H
  22. #include "mover.h"
  23. #endif
  24. //------------------------------------------------------------------------------------------
  25. // Macro Definitions
  26. extern bool useSound;
  27. PacketFilePtr Radio::noiseFile = NULL;
  28. RadioPtr Radio::radioList[MAX_RADIOS]; //Warriors no longer delete their radios.
  29. bool Radio::radioListGo = false;
  30. bool Radio::messageInfoLoaded = false;
  31. long Radio::currentRadio = 0;
  32. UserHeapPtr Radio::radioHeap = NULL;
  33. PacketFilePtr Radio::messagesFile[MAX_RADIOS];
  34. RadioMessageInfo messageInfo[RADIO_MESSAGE_COUNT];
  35. //------------------------------------------------------------------------------------------
  36. // Class Radio
  37. //------------------------------------------------------------------------------------------
  38. void *Radio::operator new (size_t mySize)
  39. {
  40. if (!radioListGo)
  41. {
  42. radioListGo = true;
  43. for (long i=0;i<MAX_RADIOS;i++)
  44. {
  45. radioList[i] = NULL;
  46. messagesFile[i] = NULL;
  47. }
  48. currentRadio = 0;
  49. radioHeap = new UserHeap;
  50. radioHeap->init((4096 * 51)-1,"Radios");
  51. radioHeap->setMallocFatals(false);
  52. }
  53. void *result = radioHeap->Malloc(mySize);
  54. return result;
  55. }
  56. //-------------------------------------------------------------------------------
  57. void Radio::operator delete (void *us)
  58. {
  59. radioHeap->Free(us);
  60. }
  61. //-------------------------------------------------------------------------------
  62. long Radio::init (char *fileName, unsigned long heapSize, char *movie)
  63. {
  64. FullPathFileName pilotAudioPath;
  65. pilotAudioPath.init(CDsoundPath,fileName,".pak");
  66. FullPathFileName noisePath;
  67. noisePath.init(CDsoundPath,"noise",".pak");
  68. //--------------------------------------
  69. // Startup the packet file.
  70. radioID = currentRadio;
  71. messagesFile[radioID] = new PacketFile;
  72. gosASSERT(messagesFile[radioID] != NULL);
  73. long result = messagesFile[radioID]->open(pilotAudioPath);
  74. gosASSERT(result == NO_ERR);
  75. if (!noiseFile)
  76. {
  77. //--------------------------------------
  78. // Startup the Noise packet file.
  79. noiseFile = new PacketFile;
  80. gosASSERT(noiseFile != NULL);
  81. result = noiseFile->open(noisePath);
  82. gosASSERT(result == NO_ERR);
  83. }
  84. // load message parameters
  85. if (!messageInfoLoaded)
  86. {
  87. if (loadMessageInfo() == NO_ERR)
  88. messageInfoLoaded = TRUE;
  89. else
  90. Fatal(0, "Unable to load message info.");
  91. }
  92. radioList[currentRadio] = this;
  93. currentRadio++;
  94. return(NO_ERR);
  95. }
  96. #define NO_PLAY -1
  97. //------------------------------------------------------------------------------------------
  98. long Radio::playMessage (RadioMessageType msgType)
  99. {
  100. long i, roll, callsign, fragmentNum, dropOut = 0;
  101. if (!useSound)
  102. return(NO_PLAY);
  103. if (!enabled)
  104. return(NO_PLAY);
  105. if (!owner)
  106. return(NO_PLAY);
  107. if (!soundSystem->checkMessage(owner, messageInfo[msgType].priority, msgType))
  108. return(NO_PLAY);
  109. if ((msgType == RADIO_AMMO_OUT) && ammoOutPlayed)
  110. return(NO_PLAY);
  111. if (messageInfo[msgType].styleCount > 1)
  112. {
  113. roll = RandomNumber(100); // choose which style of message to play
  114. for (i=0; i<messageInfo[msgType].styleCount; i++)
  115. {
  116. dropOut += messageInfo[msgType].styleChance[i];
  117. if (roll < dropOut)
  118. break;
  119. }
  120. if (i != 0 && i == messageInfo[msgType].styleCount)
  121. return NO_PLAY;
  122. if (messageInfo[msgType].messageMapping + i == owner->getLastMessage())
  123. i++;
  124. if (i >= messageInfo[msgType].styleCount)
  125. i = 0;
  126. }
  127. else
  128. i = 0;
  129. RadioData *msgData = (RadioData *)radioHeap->Malloc(sizeof(RadioData));
  130. if (!msgData)
  131. {
  132. return(NO_PLAY);
  133. }
  134. memset(msgData,0,sizeof(RadioData));
  135. msgData->noiseId = SHORT_STATIC;
  136. msgData->msgType = msgType;
  137. msgData->msgId = messageInfo[msgType].messageMapping + i;
  138. msgData->movieCode = messageInfo[msgType].movieCode;
  139. msgData->msgHeap = radioHeap;
  140. msgData->turnQueued = turn;
  141. msgData->priority = messageInfo[msgType].priority;
  142. msgData->pilot = owner;
  143. msgData->expirationDate = scenarioTime + messageInfo[msgType].shelfLife;
  144. //-----------------------------------------------------------------------
  145. // Load the pieces need for playback.
  146. callsign = 0;
  147. fragmentNum = 0;
  148. if (messageInfo[msgType].pilotIdentifiesSelf)
  149. {
  150. if (messageInfo[RADIO_CALLSIGN].styleCount > 1)
  151. {
  152. roll = RandomNumber(100); // choose which style of message to play
  153. for (i=0; i<messageInfo[RADIO_CALLSIGN].styleCount; i++)
  154. {
  155. dropOut += messageInfo[RADIO_CALLSIGN].styleChance[i];
  156. if (roll < dropOut)
  157. break;
  158. }
  159. if (i != 0 && i == messageInfo[RADIO_CALLSIGN].styleCount)
  160. callsign = 0;
  161. if (i >= messageInfo[RADIO_CALLSIGN].styleCount)
  162. callsign = 0;
  163. }
  164. else
  165. callsign = 0;
  166. callsign += messageInfo[RADIO_CALLSIGN].messageMapping;
  167. }
  168. if (callsign)
  169. {
  170. if (messagesFile[radioID]->seekPacket(callsign) == NO_ERR)
  171. {
  172. unsigned long messageSize = messagesFile[radioID]->getPacketSize();
  173. msgData->data[fragmentNum] = (MemoryPtr)radioHeap->Malloc(messageSize);
  174. if (!msgData->data[fragmentNum])
  175. {
  176. radioHeap->Free(msgData);
  177. return(NO_PLAY);
  178. }
  179. messagesFile[radioID]->readPacket(callsign,msgData->data[fragmentNum]);
  180. msgData->dataSize[fragmentNum] = messageSize;
  181. fragmentNum++;
  182. }
  183. }
  184. if (messagesFile[radioID]->seekPacket(msgData->msgId) == NO_ERR)
  185. {
  186. unsigned long messageSize = messagesFile[radioID]->getPacketSize();
  187. msgData->data[fragmentNum] = (MemoryPtr)radioHeap->Malloc(messageSize);
  188. if (!msgData->data[fragmentNum])
  189. {
  190. while (fragmentNum >= 0)
  191. {
  192. radioHeap->Free(msgData->data[fragmentNum]);
  193. fragmentNum--;
  194. }
  195. radioHeap->Free(msgData);
  196. return(NO_PLAY);
  197. }
  198. messagesFile[radioID]->readPacket(msgData->msgId,msgData->data[fragmentNum]);
  199. msgData->dataSize[fragmentNum] = messageSize;
  200. if (noiseFile->seekPacket(msgData->noiseId) == NO_ERR)
  201. {
  202. unsigned long messageSize = noiseFile->getPacketSize();
  203. msgData->noise[0] = (MemoryPtr)radioHeap->Malloc(messageSize);
  204. if (!msgData->noise[0])
  205. {
  206. radioHeap->Free(msgData);
  207. if (fragmentNum > 0)
  208. radioHeap->Free(msgData->data[0]);
  209. return(NO_PLAY);
  210. }
  211. noiseFile->readPacket(msgData->noiseId,msgData->noise[0]);
  212. msgData->noiseSize[fragmentNum] = messageSize;
  213. }
  214. }
  215. //------------------------------------------------------
  216. // Big ol bug here. We weren't checking to see if the
  217. // queue was full. If it was, memory would leak from
  218. // the smacker window. It wouldn't leak from the RadioHeap
  219. // because we clear the radio heap every mission!!
  220. if (soundSystem->queueRadioMessage(msgData) != NO_ERR)
  221. {
  222. if (msgData)
  223. {
  224. for (long j=0;j<MAX_FRAGMENTS;j++)
  225. {
  226. radioHeap->Free(msgData->data[j]);
  227. msgData->data[j] = NULL;
  228. radioHeap->Free(msgData->noise[j]);
  229. msgData->noise[j] = NULL;
  230. }
  231. radioHeap->Free(msgData);
  232. msgData = NULL;
  233. }
  234. return (NO_PLAY);
  235. }
  236. if (msgType == RADIO_AMMO_OUT)
  237. ammoOutPlayed = true;
  238. return(msgData->msgId);
  239. }
  240. //------------------------------------------------------------------------------------------
  241. long Radio::loadMessageInfo(void)
  242. {
  243. FullPathFileName messageInfoPath;
  244. FilePtr messageInfoFile;
  245. long result;
  246. char dataLine[512];
  247. char* field;
  248. messageInfoPath.init(soundPath,"radio",".csv");
  249. messageInfoFile = new File;
  250. if (!messageInfoFile)
  251. return -1; //quasit
  252. result = messageInfoFile->open(messageInfoPath);
  253. if (result != NO_ERR)
  254. {
  255. delete messageInfoFile;
  256. return result;
  257. }
  258. messageInfoFile->readLine((MemoryPtr)dataLine, 511); // skip title line
  259. for (long i=0; i<RADIO_MESSAGE_COUNT; i++)
  260. {
  261. result = messageInfoFile->readLine((MemoryPtr)dataLine, 511);
  262. if (!result)
  263. Fatal(0, "Bad Message Info File");
  264. field = strtok(dataLine, ","); // get past command name
  265. field = strtok(NULL, ",");
  266. if (field)
  267. messageInfo[i].priority = atoi(field);
  268. else
  269. messageInfo[i].priority = 4;
  270. field = strtok(NULL, ",");
  271. if (field)
  272. messageInfo[i].shelfLife = atoi(field);
  273. else
  274. messageInfo[i].shelfLife = 0;
  275. field = strtok(NULL, ",");
  276. if (field && *field != 'x')
  277. messageInfo[i].movieCode = *field;
  278. else
  279. messageInfo[i].movieCode = '\0';
  280. field = strtok(NULL, ",");
  281. if (field)
  282. messageInfo[i].styleCount = atoi(field);
  283. else
  284. messageInfo[i].styleCount = 1;
  285. field = strtok(NULL, ",");
  286. if (field)
  287. messageInfo[i].styleChance[0] = atoi(field);
  288. else
  289. messageInfo[i].styleChance[0] = 0;
  290. field = strtok(NULL, ",");
  291. if (field)
  292. messageInfo[i].styleChance[1] = atoi(field);
  293. else
  294. messageInfo[i].styleChance[1] = 0;
  295. field = strtok(NULL, ",");
  296. if (field)
  297. messageInfo[i].styleChance[2] = atoi(field);
  298. else
  299. messageInfo[i].styleChance[2] = 0;
  300. field = strtok(NULL, ",");
  301. if (field)
  302. messageInfo[i].pilotIdentifiesSelf = (*field == 'y');
  303. else
  304. messageInfo[i].pilotIdentifiesSelf = FALSE;
  305. field = strtok(NULL, ",");
  306. if (field)
  307. messageInfo[i].messageMapping = atoi(field);
  308. else
  309. messageInfo[i].messageMapping = 0;
  310. field = strtok(NULL, ",");
  311. if (field)
  312. {
  313. field = strtok(NULL, ",");
  314. if (field)
  315. {
  316. messageInfo[i].kludgeStyle = (*field == 'x');
  317. }
  318. }
  319. if (!field)
  320. messageInfo[i].kludgeStyle = FALSE;
  321. }
  322. messageInfoFile->close();
  323. delete messageInfoFile;
  324. return NO_ERR;
  325. }