MapUtil.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: MapUtil.cpp //////////////////////////////////////////////////////////////////////////////
  24. // Author: Matt Campbell, December 2001
  25. // Description: Map utility/convenience routines
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  29. #include "Common/CRC.h"
  30. #include "Common/FileSystem.h"
  31. #include "Common/LocalFileSystem.h"
  32. #include "Common/File.h"
  33. #include "Common/GlobalData.h"
  34. #include "Common/GameState.h"
  35. #include "Common/GameEngine.h"
  36. #include "Common/NameKeyGenerator.h"
  37. #include "Common/DataChunk.h"
  38. #include "Common/MapReaderWriterInfo.h"
  39. #include "Common/MessageStream.h"
  40. #include "Common/WellKnownKeys.h"
  41. #include "Common/INI.h"
  42. #include "Common/QuotedPrintable.h"
  43. #include "Common/SkirmishBattleHonors.h"
  44. #include "Common/ThingFactory.h"
  45. #include "Common/ThingTemplate.h"
  46. #include "Common/MapObject.h"
  47. #include "GameClient/GameText.h"
  48. #include "GameClient/WindowLayout.h"
  49. #include "GameClient/Gadget.h"
  50. #include "GameClient/Image.h"
  51. #include "GameClient/Shell.h"
  52. #include "GameClient/GameWindowManager.h"
  53. #include "GameClient/GadgetListBox.h"
  54. #include "GameClient/MapUtil.h"
  55. #include "GameLogic/GameLogic.h"
  56. #include "GameLogic/FPUControl.h"
  57. #include "GameNetwork/GameInfo.h"
  58. #include "GameNetwork/NetworkDefs.h"
  59. #ifdef _INTERNAL
  60. // for occasional debugging...
  61. //#pragma optimize("", off)
  62. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  63. #endif
  64. //-------------------------------------------------------------------------------
  65. // PRIVATE DATA ///////////////////////////////////////////////////////////////////////////////////
  66. static char *mapExtension = ".map";
  67. static Int m_width = 0; ///< Height map width.
  68. static Int m_height = 0; ///< Height map height (y size of array).
  69. static Int m_borderSize = 0; ///< Non-playable border area.
  70. static std::vector<ICoord2D> m_boundaries; ///< All the boundaries we use for the map
  71. static Int m_dataSize = 0; ///< size of m_data.
  72. static UnsignedByte *m_data = 0; ///< array of z(height) values in the height map.
  73. static Dict worldDict = 0;
  74. static WaypointMap *m_waypoints = 0;
  75. static Coord3DList m_supplyPositions;
  76. static Coord3DList m_techPositions;
  77. static Int m_mapDX = 0;
  78. static Int m_mapDY = 0;
  79. static UnsignedInt calcCRC( AsciiString dirName, AsciiString fname )
  80. {
  81. CRC theCRC;
  82. theCRC.clear();
  83. // Try the official map dir
  84. AsciiString asciiFile;
  85. char tempBuf[_MAX_PATH];
  86. char filenameBuf[_MAX_PATH];
  87. int length = 0;
  88. strcpy(tempBuf, fname.str());
  89. length = strlen( tempBuf );
  90. if( length >= 4 )
  91. {
  92. memset( filenameBuf, '\0', _MAX_PATH);
  93. strncpy( filenameBuf, tempBuf, length - 4);
  94. }
  95. File *fp;
  96. asciiFile = fname;
  97. fp = TheFileSystem->openFile(asciiFile.str(), File::READ);
  98. if( !fp )
  99. {
  100. DEBUG_CRASH(("Couldn't open '%s'\n", fname.str()));
  101. return 0;
  102. }
  103. UnsignedByte buf[4096];
  104. Int num;
  105. while ( (num=fp->read(buf, 4096)) > 0 )
  106. {
  107. theCRC.computeCRC(buf, num);
  108. }
  109. fp->close();
  110. fp = NULL;
  111. return theCRC.get();
  112. }
  113. static Bool ParseObjectDataChunk(DataChunkInput &file, DataChunkInfo *info, void *userData)
  114. {
  115. Bool readDict = info->version >= K_OBJECTS_VERSION_2;
  116. Coord3D loc;
  117. loc.x = file.readReal();
  118. loc.y = file.readReal();
  119. loc.z = file.readReal();
  120. if (info->version <= K_OBJECTS_VERSION_2)
  121. {
  122. loc.z = 0;
  123. }
  124. Real angle = file.readReal();
  125. Int flags = file.readInt();
  126. AsciiString name = file.readAsciiString();
  127. Dict d;
  128. if (readDict)
  129. {
  130. d = file.readDict();
  131. }
  132. MapObject *pThisOne;
  133. // create the map object
  134. pThisOne = newInstance( MapObject )( loc, name, angle, flags, &d,
  135. TheThingFactory->findTemplate( name ) );
  136. //DEBUG_LOG(("obj %s owner %s\n",name.str(),d.getAsciiString(TheKey_originalOwner).str()));
  137. if (pThisOne->getProperties()->getType(TheKey_waypointID) == Dict::DICT_INT)
  138. {
  139. pThisOne->setIsWaypoint();
  140. // grab useful info
  141. (*m_waypoints)[pThisOne->getWaypointName()] = loc;
  142. }
  143. else if (pThisOne->getThingTemplate() && pThisOne->getThingTemplate()->isKindOf(KINDOF_TECH_BUILDING))
  144. {
  145. m_techPositions.push_back(loc);
  146. }
  147. else if (pThisOne->getThingTemplate() && pThisOne->getThingTemplate()->isKindOf(KINDOF_SUPPLY_SOURCE_ON_PREVIEW))
  148. {
  149. m_supplyPositions.push_back(loc);
  150. }
  151. pThisOne->deleteInstance();
  152. return TRUE;
  153. }
  154. static Bool ParseObjectsDataChunk(DataChunkInput &file, DataChunkInfo *info, void *userData)
  155. {
  156. file.m_currentObject = NULL;
  157. file.registerParser( AsciiString("Object"), info->label, ParseObjectDataChunk );
  158. return (file.parse(userData));
  159. }
  160. static Bool ParseWorldDictDataChunk(DataChunkInput &file, DataChunkInfo *info, void *userData)
  161. {
  162. worldDict = file.readDict();
  163. return true;
  164. }
  165. static Bool ParseSizeOnly(DataChunkInput &file, DataChunkInfo *info, void *userData)
  166. {
  167. m_width = file.readInt();
  168. m_height = file.readInt();
  169. if (info->version >= K_HEIGHT_MAP_VERSION_3) {
  170. m_borderSize = file.readInt();
  171. } else {
  172. m_borderSize = 0;
  173. }
  174. if (info->version >= K_HEIGHT_MAP_VERSION_4) {
  175. Int numBorders = file.readInt();
  176. m_boundaries.resize(numBorders);
  177. for (int i = 0; i < numBorders; ++i) {
  178. m_boundaries[i].x = file.readInt();
  179. m_boundaries[i].y = file.readInt();
  180. }
  181. }
  182. return true;
  183. m_dataSize = file.readInt();
  184. m_data = NEW UnsignedByte[m_dataSize]; // pool[]ify
  185. if (m_dataSize <= 0 || (m_dataSize != (m_width*m_height))) {
  186. throw ERROR_CORRUPT_FILE_FORMAT ;
  187. }
  188. file.readArrayOfBytes((char *)m_data, m_dataSize);
  189. // Resize me.
  190. if (info->version == K_HEIGHT_MAP_VERSION_1) {
  191. Int newWidth = (m_width+1)/2;
  192. Int newHeight = (m_height+1)/2;
  193. Int i, j;
  194. for (i=0; i<newHeight; i++) {
  195. for (j=0; j<newWidth; j++) {
  196. m_data[i*newWidth+j] = m_data[2*i*m_width+2*j];
  197. }
  198. }
  199. m_width = newWidth;
  200. m_height = newHeight;
  201. }
  202. return true;
  203. }
  204. static Bool ParseSizeOnlyInChunk(DataChunkInput &file, DataChunkInfo *info, void *userData)
  205. {
  206. return ParseSizeOnly(file, info, userData);
  207. }
  208. static Bool loadMap( AsciiString filename )
  209. {
  210. char tempBuf[_MAX_PATH];
  211. char filenameBuf[_MAX_PATH];
  212. AsciiString asciiFile;
  213. int length = 0;
  214. strcpy(tempBuf, filename.str());
  215. length = strlen( tempBuf );
  216. if( length >= 4 )
  217. {
  218. memset( filenameBuf, '\0', _MAX_PATH);
  219. strncpy( filenameBuf, tempBuf, length - 4);
  220. }
  221. CachedFileInputStream fileStrm;
  222. asciiFile = filename;
  223. if( !fileStrm.open(asciiFile) )
  224. {
  225. return FALSE;
  226. }
  227. ChunkInputStream *pStrm = &fileStrm;
  228. DataChunkInput file( pStrm );
  229. m_waypoints = NEW WaypointMap;
  230. file.registerParser( AsciiString("HeightMapData"), AsciiString::TheEmptyString, ParseSizeOnlyInChunk );
  231. file.registerParser( AsciiString("WorldInfo"), AsciiString::TheEmptyString, ParseWorldDictDataChunk );
  232. file.registerParser( AsciiString("ObjectsList"), AsciiString::TheEmptyString, ParseObjectsDataChunk );
  233. if (!file.parse(NULL)) {
  234. throw(ERROR_CORRUPT_FILE_FORMAT);
  235. }
  236. m_mapDX = m_width - 2*m_borderSize;
  237. m_mapDY = m_height - 2*m_borderSize;
  238. return TRUE;
  239. }
  240. static void resetMap( void )
  241. {
  242. if (m_data)
  243. {
  244. delete[] m_data;
  245. m_data = 0;
  246. }
  247. if (m_waypoints)
  248. {
  249. delete m_waypoints;
  250. m_waypoints = 0;
  251. }
  252. m_techPositions.clear();
  253. m_supplyPositions.clear();
  254. }
  255. static void getExtent( Region3D *extent )
  256. {
  257. extent->lo.x = 0.0f;
  258. extent->lo.y = 0.0f;
  259. // Note - m_mapDX & Y are the number of height map grids wide, so we have to
  260. // multiply by the grid width.
  261. extent->hi.x = m_mapDX*MAP_XY_FACTOR;
  262. extent->hi.y = m_mapDY*MAP_XY_FACTOR;
  263. extent->lo.z = 0;
  264. extent->hi.z = 0;
  265. }
  266. //-------------------------------------------------------------------------------
  267. void WaypointMap::update( void )
  268. {
  269. if (!m_waypoints)
  270. {
  271. m_numStartSpots = 1;
  272. return;
  273. }
  274. this->clear();
  275. AsciiString startingCamName = TheNameKeyGenerator->keyToName(TheKey_InitialCameraPosition);
  276. WaypointMap::const_iterator it;
  277. it = m_waypoints->find(startingCamName);
  278. if (it != m_waypoints->end())
  279. {
  280. (*this)[startingCamName] = it->second;
  281. }
  282. m_numStartSpots = 0;
  283. for (Int i=0; i<MAX_SLOTS; ++i)
  284. {
  285. startingCamName.format("Player_%d_Start", i+1); // start pos waypoints are 1-based
  286. it = m_waypoints->find(startingCamName);
  287. if (it != m_waypoints->end())
  288. {
  289. (*this)[startingCamName] = it->second;
  290. ++m_numStartSpots;
  291. }
  292. else
  293. {
  294. break;
  295. }
  296. }
  297. m_numStartSpots = max(1, m_numStartSpots);
  298. }
  299. const char * MapCache::m_mapCacheName = "MapCache.ini";
  300. AsciiString MapCache::getMapDir() const
  301. {
  302. return AsciiString("Maps");
  303. }
  304. AsciiString MapCache::getUserMapDir() const
  305. {
  306. AsciiString tmp = TheGlobalData->getPath_UserData();
  307. tmp.concat(getMapDir());
  308. return tmp;
  309. }
  310. AsciiString MapCache::getMapExtension() const
  311. {
  312. return AsciiString("map");
  313. }
  314. void MapCache::writeCacheINI( Bool userDir )
  315. {
  316. AsciiString mapDir;
  317. if (!userDir || TheGlobalData->m_buildMapCache)
  318. {
  319. mapDir = getMapDir();
  320. }
  321. else
  322. {
  323. mapDir = getUserMapDir();
  324. }
  325. AsciiString filepath = mapDir;
  326. filepath.concat('\\');
  327. TheFileSystem->createDirectory(mapDir);
  328. filepath.concat(m_mapCacheName);
  329. FILE *fp = fopen(filepath.str(), "w");
  330. DEBUG_ASSERTCRASH(fp != NULL, ("Failed to create %s", filepath.str()));
  331. if (fp == NULL) {
  332. return;
  333. }
  334. fprintf(fp, "; FILE: %s /////////////////////////////////////////////////////////////\n", filepath.str());
  335. fprintf(fp, "; This INI file is auto-generated - do not modify\n");
  336. fprintf(fp, "; /////////////////////////////////////////////////////////////////////////////\n");
  337. mapDir.toLower();
  338. MapCache::iterator it = begin();
  339. MapMetaData md;
  340. while (it != end())
  341. {
  342. if (it->first.startsWithNoCase(mapDir.str()))
  343. {
  344. md = it->second;
  345. fprintf(fp, "\nMapCache %s\n", AsciiStringToQuotedPrintable(it->first.str()).str());
  346. fprintf(fp, " fileSize = %u\n", md.m_filesize);
  347. fprintf(fp, " fileCRC = %u\n", md.m_CRC);
  348. fprintf(fp, " timestampLo = %d\n", md.m_timestamp.m_lowTimeStamp);
  349. fprintf(fp, " timestampHi = %d\n", md.m_timestamp.m_highTimeStamp);
  350. fprintf(fp, " isOfficial = %s\n", (md.m_isOfficial)?"yes":"no");
  351. fprintf(fp, " isMultiplayer = %s\n", (md.m_isMultiplayer)?"yes":"no");
  352. fprintf(fp, " numPlayers = %d\n", md.m_numPlayers);
  353. fprintf(fp, " extentMin = X:%2.2f Y:%2.2f Z:%2.2f\n", md.m_extent.lo.x, md.m_extent.lo.y, md.m_extent.lo.z);
  354. fprintf(fp, " extentMax = X:%2.2f Y:%2.2f Z:%2.2f\n", md.m_extent.hi.x, md.m_extent.hi.y, md.m_extent.hi.z);
  355. fprintf(fp, " displayName = %s\n", UnicodeStringToQuotedPrintable(md.m_displayName).str());
  356. Coord3D pos;
  357. WaypointMap::iterator itw = md.m_waypoints.begin();
  358. while (itw != md.m_waypoints.end())
  359. {
  360. pos = itw->second;
  361. fprintf(fp, " %s = X:%2.2f Y:%2.2f Z:%2.2f\n", itw->first.str(), pos.x, pos.y, pos.z);
  362. ++itw;
  363. }
  364. Coord3DList::iterator itc3d = md.m_techPositions.begin();
  365. while (itc3d != md.m_techPositions.end())
  366. {
  367. pos = *itc3d;
  368. fprintf(fp, " techPosition = X:%2.2f Y:%2.2f Z:%2.2f\n", pos.x, pos.y, pos.z);
  369. itc3d++;
  370. }
  371. itc3d = md.m_supplyPositions.begin();
  372. while (itc3d != md.m_supplyPositions.end())
  373. {
  374. pos = *itc3d;
  375. fprintf(fp, " supplyPosition = X:%2.2f Y:%2.2f Z:%2.2f\n", pos.x, pos.y, pos.z);
  376. itc3d++;
  377. }
  378. fprintf(fp, "END\n\n");
  379. }
  380. else
  381. {
  382. //DEBUG_LOG(("%s does not start %s\n", mapDir.str(), it->first.str()));
  383. }
  384. ++it;
  385. }
  386. fclose(fp);
  387. }
  388. void MapCache::updateCache( void )
  389. {
  390. setFPMode();
  391. TheFileSystem->createDirectory(getUserMapDir());
  392. if (loadUserMaps())
  393. {
  394. writeCacheINI( TRUE );
  395. }
  396. loadStandardMaps(); // we shall overwrite info from matching user maps to prevent munkees from getting rowdy :)
  397. #if defined(_DEBUG) || defined(_INTERNAL)
  398. if (TheLocalFileSystem->doesFileExist(getMapDir().str()))
  399. {
  400. // only create the map cache file if "Maps" exist
  401. Bool wasBuildMapCache = TheGlobalData->m_buildMapCache;
  402. TheWritableGlobalData->m_buildMapCache = true;
  403. loadUserMaps();
  404. TheWritableGlobalData->m_buildMapCache = wasBuildMapCache;
  405. writeCacheINI( FALSE );
  406. }
  407. #endif
  408. }
  409. Bool MapCache::clearUnseenMaps( AsciiString dirName )
  410. {
  411. dirName.toLower();
  412. Bool erasedSomething = FALSE;
  413. std::map<AsciiString, Bool>::iterator it = m_seen.begin();
  414. while (it != m_seen.end())
  415. {
  416. AsciiString mapName = it->first;
  417. if (it->second == FALSE && mapName.startsWithNoCase(dirName.str()))
  418. {
  419. // not seen in the dir - clear it out.
  420. erase(mapName);
  421. erasedSomething = TRUE;
  422. }
  423. ++it;
  424. }
  425. return erasedSomething;
  426. }
  427. void MapCache::loadStandardMaps(void)
  428. {
  429. INI ini;
  430. AsciiString fname;
  431. fname.format("%s\\%s", getMapDir().str(), m_mapCacheName);
  432. #if defined(_DEBUG) || defined(_INTERNAL)
  433. File *fp = TheFileSystem->openFile(fname.str(), File::READ);
  434. if (fp != NULL)
  435. {
  436. fp->close();
  437. fp = NULL;
  438. #endif
  439. ini.load( fname, INI_LOAD_OVERWRITE, NULL );
  440. #if defined(_DEBUG) || defined(_INTERNAL)
  441. }
  442. #endif
  443. }
  444. Bool MapCache::loadUserMaps()
  445. {
  446. // Read in map list from disk
  447. AsciiString mapDir;
  448. if (TheGlobalData->m_buildMapCache)
  449. {
  450. mapDir = getMapDir();
  451. }
  452. else
  453. {
  454. mapDir = getUserMapDir();
  455. INI ini;
  456. AsciiString fname;
  457. fname.format("%s\\%s", mapDir.str(), m_mapCacheName);
  458. File *fp = TheFileSystem->openFile(fname.str(), File::READ);
  459. if (fp)
  460. {
  461. fp->close();
  462. ini.load( fname, INI_LOAD_OVERWRITE, NULL );
  463. }
  464. }
  465. // mark all as unseen
  466. m_seen.clear();
  467. MapCache::iterator it = begin();
  468. while (it != end())
  469. {
  470. m_seen[it->first] = FALSE;
  471. ++it;
  472. }
  473. FilenameList filenameList;
  474. FilenameListIter iter;
  475. AsciiString toplevelPattern;
  476. toplevelPattern.format("%s\\", mapDir.str());
  477. Bool parsedAMap = FALSE;
  478. AsciiString filenamepattern;
  479. filenamepattern.format("*.%s", getMapExtension().str());
  480. TheFileSystem->getFileListInDirectory(toplevelPattern, filenamepattern, filenameList, TRUE);
  481. iter = filenameList.begin();
  482. while (iter != filenameList.end()) {
  483. FileInfo fileInfo;
  484. AsciiString tempfilename;
  485. tempfilename = (*iter);
  486. tempfilename.toLower();
  487. const char *s = tempfilename.reverseFind('\\');
  488. if (!s)
  489. {
  490. DEBUG_CRASH(("Couldn't find \\ in map name!"));
  491. }
  492. else
  493. {
  494. AsciiString endingStr;
  495. AsciiString fname = s+1;
  496. for (Int i=0; i<strlen(mapExtension); ++i)
  497. fname.removeLastChar();
  498. endingStr.format("%s\\%s%s", fname.str(), fname.str(), mapExtension);
  499. Bool skipMap = FALSE;
  500. if (TheGlobalData->m_buildMapCache)
  501. {
  502. std::set<AsciiString>::const_iterator sit = m_allowedMaps.find(fname);
  503. if (m_allowedMaps.size() != 0 && sit == m_allowedMaps.end())
  504. {
  505. //DEBUG_LOG(("Skipping map: '%s'\n", fname.str()));
  506. skipMap = TRUE;
  507. }
  508. else
  509. {
  510. //DEBUG_LOG(("Parsing map: '%s'\n", fname.str()));
  511. }
  512. }
  513. if (!skipMap)
  514. {
  515. if (!tempfilename.endsWithNoCase(endingStr.str()))
  516. {
  517. DEBUG_CRASH(("Found map '%s' in wrong spot (%s)", fname.str(), tempfilename.str()));
  518. }
  519. else
  520. {
  521. if (TheFileSystem->getFileInfo(tempfilename, &fileInfo)) {
  522. char funk[_MAX_PATH];
  523. strcpy(funk, tempfilename.str());
  524. char *filenameptr = funk;
  525. char *tempchar = funk;
  526. while (*tempchar != 0) {
  527. if ((*tempchar == '\\') || (*tempchar == '/')) {
  528. filenameptr = tempchar+1;
  529. }
  530. ++tempchar;
  531. }
  532. m_seen[tempfilename] = TRUE;
  533. parsedAMap |= addMap(mapDir, *iter, &fileInfo, TheGlobalData->m_buildMapCache);
  534. } else {
  535. DEBUG_CRASH(("Could not get file info for map %s", (*iter).str()));
  536. }
  537. }
  538. }
  539. }
  540. iter++;
  541. }
  542. // clean out unseen maps
  543. if (clearUnseenMaps(mapDir))
  544. return TRUE;
  545. return parsedAMap;
  546. }
  547. //Bool MapCache::addMap( AsciiString dirName, AsciiString fname, WinTimeStamp timestamp, UnsignedInt filesize, Bool isOfficial )
  548. Bool MapCache::addMap( AsciiString dirName, AsciiString fname, FileInfo *fileInfo, Bool isOfficial)
  549. {
  550. if (fileInfo == NULL) {
  551. return FALSE;
  552. }
  553. AsciiString lowerFname;
  554. lowerFname = fname;
  555. lowerFname.toLower();
  556. MapCache::iterator it = find(lowerFname);
  557. MapMetaData md;
  558. UnsignedInt filesize = fileInfo->sizeLow;
  559. if (it != end())
  560. {
  561. // Found the map in our cache. Check to see if it has changed.
  562. md = it->second;
  563. if ((md.m_filesize == filesize) &&
  564. (md.m_CRC != 0))
  565. {
  566. // DEBUG_LOG(("MapCache::addMap - found match for map %s\n", lowerFname.str()));
  567. return FALSE; // OK, it checks out.
  568. }
  569. DEBUG_LOG(("%s didn't match file in MapCache\n", fname.str()));
  570. DEBUG_LOG(("size: %d / %d\n", filesize, md.m_filesize));
  571. DEBUG_LOG(("time1: %d / %d\n", fileInfo->timestampHigh, md.m_timestamp.m_highTimeStamp));
  572. DEBUG_LOG(("time2: %d / %d\n", fileInfo->timestampLow, md.m_timestamp.m_lowTimeStamp));
  573. // DEBUG_LOG(("size: %d / %d\n", filesize, md.m_filesize));
  574. // DEBUG_LOG(("time1: %d / %d\n", timestamp.m_highTimeStamp, md.m_timestamp.m_highTimeStamp));
  575. // DEBUG_LOG(("time2: %d / %d\n", timestamp.m_lowTimeStamp, md.m_timestamp.m_lowTimeStamp));
  576. }
  577. DEBUG_LOG(("MapCache::addMap(): caching '%s' because '%s' was not found\n", fname.str(), lowerFname.str()));
  578. loadMap(fname); // Just load for querying the data, since we aren't playing this map.
  579. // The map is now loaded. Pick out what we need.
  580. md.m_fileName = lowerFname;
  581. md.m_filesize = filesize;
  582. md.m_isOfficial = isOfficial;
  583. md.m_waypoints.update();
  584. md.m_numPlayers = md.m_waypoints.m_numStartSpots;
  585. md.m_isMultiplayer = (md.m_numPlayers >= 2);
  586. md.m_timestamp.m_highTimeStamp = fileInfo->timestampHigh;
  587. md.m_timestamp.m_lowTimeStamp = fileInfo->timestampLow;
  588. md.m_supplyPositions = m_supplyPositions;
  589. md.m_techPositions = m_techPositions;
  590. md.m_CRC = calcCRC(dirName, fname);
  591. Bool exists = false;
  592. AsciiString munkee = worldDict.getAsciiString(TheKey_mapName, &exists);
  593. if (!exists || munkee.isEmpty())
  594. {
  595. DEBUG_LOG(("Missing TheKey_mapName!\n"));
  596. AsciiString tempdisplayname;
  597. tempdisplayname = fname.reverseFind('\\') + 1;
  598. md.m_displayName.translate(tempdisplayname);
  599. if (md.m_numPlayers >= 2)
  600. {
  601. UnicodeString extension;
  602. extension.format(L" (%d)", md.m_numPlayers);
  603. md.m_displayName.concat(extension);
  604. }
  605. TheGameText->reset();
  606. }
  607. else
  608. {
  609. AsciiString stringFileName;
  610. stringFileName.format("%s\\%s", dirName.str(), fname.str());
  611. for (Int i=0; i<4; ++i)
  612. stringFileName.removeLastChar();
  613. stringFileName.concat("\\map.str");
  614. TheGameText->initMapStringFile(stringFileName);
  615. md.m_displayName = TheGameText->fetch(munkee);
  616. if (md.m_numPlayers >= 2)
  617. {
  618. UnicodeString extension;
  619. extension.format(L" (%d)", md.m_numPlayers);
  620. md.m_displayName.concat(extension);
  621. }
  622. DEBUG_LOG(("Map name is now '%ls'\n", md.m_displayName.str()));
  623. TheGameText->reset();
  624. }
  625. getExtent(&(md.m_extent));
  626. (*this)[lowerFname] = md;
  627. DEBUG_LOG((" filesize = %d bytes\n", md.m_filesize));
  628. DEBUG_LOG((" displayName = %ls\n", md.m_displayName.str()));
  629. DEBUG_LOG((" CRC = %X\n", md.m_CRC));
  630. DEBUG_LOG((" timestamp = %d\n", md.m_timestamp));
  631. DEBUG_LOG((" isOfficial = %s\n", (md.m_isOfficial)?"yes":"no"));
  632. DEBUG_LOG((" isMultiplayer = %s\n", (md.m_isMultiplayer)?"yes":"no"));
  633. DEBUG_LOG((" numPlayers = %d\n", md.m_numPlayers));
  634. DEBUG_LOG((" extent = (%2.2f,%2.2f) -> (%2.2f,%2.2f)\n",
  635. md.m_extent.lo.x, md.m_extent.lo.y,
  636. md.m_extent.hi.x, md.m_extent.hi.y));
  637. Coord3D pos;
  638. WaypointMap::iterator itw = md.m_waypoints.begin();
  639. while (itw != md.m_waypoints.end())
  640. {
  641. pos = itw->second;
  642. DEBUG_LOG((" waypoint %s: (%2.2f,%2.2f)\n", itw->first.str(), pos.x, pos.y));
  643. ++itw;
  644. }
  645. resetMap();
  646. return TRUE;
  647. }
  648. MapCache *TheMapCache = NULL;
  649. // PUBLIC FUNCTIONS //////////////////////////////////////////////////////////////////////////////
  650. Bool WouldMapTransfer( const AsciiString& mapName )
  651. {
  652. return mapName.startsWithNoCase(TheMapCache->getUserMapDir());
  653. }
  654. //-------------------------------------------------------------------------------------------------
  655. /** Load the listbox with all the map files available to play */
  656. //-------------------------------------------------------------------------------------------------
  657. Int populateMapListboxNoReset( GameWindow *listbox, Bool useSystemMaps, Bool isMultiplayer, AsciiString mapToSelect )
  658. {
  659. if(!TheMapCache)
  660. return -1;
  661. if (!listbox)
  662. return -1;
  663. // reset the listbox content
  664. //GadgetListBoxReset( listbox );
  665. Int numColumns = GadgetListBoxGetNumColumns( listbox );
  666. const Image *easyImage = NULL;
  667. const Image *mediumImage = NULL;
  668. const Image *brutalImage = NULL;
  669. SkirmishBattleHonors *battleHonors = NULL;
  670. Int w = 10, h = 10;
  671. if (numColumns > 1)
  672. {
  673. easyImage = TheMappedImageCollection->findImageByName("Star-Bronze");
  674. mediumImage = TheMappedImageCollection->findImageByName("Star-Silver");
  675. brutalImage = TheMappedImageCollection->findImageByName("Star-Gold");
  676. battleHonors = new SkirmishBattleHonors;
  677. w = (brutalImage)?brutalImage->getImageWidth():10;
  678. w = min(GadgetListBoxGetColumnWidth(listbox, 0), w);
  679. h = w;
  680. }
  681. Color color = GameMakeColor( 255, 255, 255, 255 );
  682. UnicodeString mapDisplayName;
  683. Int selectionIndex = 0; // always select *something*
  684. MapCache::iterator it = TheMapCache->begin();
  685. AsciiString mapDir;
  686. if (useSystemMaps)
  687. {
  688. mapDir = TheMapCache->getMapDir();
  689. }
  690. else
  691. {
  692. mapDir = TheGlobalData->getPath_UserData();
  693. mapDir.concat(TheMapCache->getMapDir());
  694. }
  695. mapDir.toLower();
  696. typedef std::set<UnicodeString, rts::less_than_nocase<UnicodeString> > MapNameList;
  697. typedef MapNameList::iterator MapNameListIter;
  698. typedef std::map<UnicodeString, AsciiString> MapDisplayToFileNameList;
  699. typedef MapDisplayToFileNameList::iterator MapDisplayToFileNameListIter;
  700. MapNameList tempCache;
  701. MapDisplayToFileNameList filenameMap;
  702. UnsignedInt numMapsListed = 0;
  703. UnsignedInt curNumPlayersInMap = 0;
  704. while (numMapsListed < TheMapCache->size()) {
  705. DEBUG_LOG(("Adding maps with %d players\n", curNumPlayersInMap));
  706. it = TheMapCache->begin();
  707. while (it != TheMapCache->end()) {
  708. const MapMetaData *md = &(it->second);
  709. if (md != NULL) {
  710. if (md->m_numPlayers == curNumPlayersInMap) {
  711. tempCache.insert(it->second.m_displayName);
  712. filenameMap[it->second.m_displayName] = it->first;
  713. DEBUG_LOG(("Adding map %s to temp cache.\n", it->first.str()));
  714. ++numMapsListed;
  715. }
  716. }
  717. ++it;
  718. }
  719. MapNameListIter tempit = tempCache.begin();
  720. while (tempit != tempCache.end())
  721. {
  722. AsciiString asciiMapName;
  723. asciiMapName = filenameMap[*tempit];
  724. it = TheMapCache->find(asciiMapName);
  725. /*
  726. if (it != TheMapCache->end())
  727. {
  728. DEBUG_LOG(("populateMapListbox(): looking at %s (displayName = %ls), mp = %d (== %d?) mapDir=%s (ok=%d)\n",
  729. it->first.str(), it->second.m_displayName.str(), it->second.m_isMultiplayer, isMultiplayer,
  730. mapDir.str(), it->first.startsWith(mapDir.str())));
  731. }
  732. */
  733. DEBUG_ASSERTCRASH(it != TheMapCache->end(), ("Map %s not found in map cache.", *tempit));
  734. if (it->first.startsWithNoCase(mapDir.str()) && isMultiplayer == it->second.m_isMultiplayer && !it->second.m_displayName.isEmpty())
  735. {
  736. /// @todo: mapDisplayName = TheGameText->fetch(it->second.m_displayName.str());
  737. mapDisplayName = it->second.m_displayName;
  738. Int index = -1;
  739. Int imageItemData = -1;
  740. if (numColumns > 1 && it->second.m_isMultiplayer)
  741. {
  742. Int numEasy = battleHonors->getEnduranceMedal(it->first.str(), SLOT_EASY_AI);
  743. Int numMedium = battleHonors->getEnduranceMedal(it->first.str(), SLOT_MED_AI);
  744. Int numBrutal = battleHonors->getEnduranceMedal(it->first.str(), SLOT_BRUTAL_AI);
  745. if (numBrutal)
  746. {
  747. imageItemData = 3;
  748. index = GadgetListBoxAddEntryImage( listbox, brutalImage, index, 0, w, h, TRUE);
  749. }
  750. else if (numMedium)
  751. {
  752. imageItemData = 2;
  753. index = GadgetListBoxAddEntryImage( listbox, mediumImage, index, 0, w, h, TRUE);
  754. }
  755. else if (numEasy)
  756. {
  757. imageItemData = 1;
  758. index = GadgetListBoxAddEntryImage( listbox, easyImage, index, 0, w, h, TRUE);
  759. }
  760. else
  761. {
  762. imageItemData = 0;
  763. index = GadgetListBoxAddEntryImage( listbox, NULL, index, 0, w, h, TRUE);
  764. }
  765. }
  766. index = GadgetListBoxAddEntryText( listbox, mapDisplayName, color, index, numColumns-1 );
  767. if (it->first == mapToSelect)
  768. {
  769. selectionIndex = index;
  770. }
  771. // now set the char* as the item data. this works because the map cache isn't being
  772. // modified while a map listbox is up.
  773. GadgetListBoxSetItemData( listbox, (void *)(it->first.str()), index );
  774. if (numColumns > 1)
  775. {
  776. GadgetListBoxSetItemData( listbox, (void *)imageItemData, index, 1 );
  777. }
  778. }
  779. ++tempit;
  780. }
  781. tempCache.clear();
  782. filenameMap.clear();
  783. ++curNumPlayersInMap;
  784. }
  785. if (battleHonors)
  786. {
  787. delete battleHonors;
  788. battleHonors = NULL;
  789. }
  790. GadgetListBoxSetSelected(listbox, &selectionIndex, 1);
  791. if (selectionIndex >= 0)
  792. {
  793. Int topIndex = GadgetListBoxGetTopVisibleEntry(listbox);
  794. Int bottomIndex = GadgetListBoxGetBottomVisibleEntry(listbox);
  795. Int rowsOnScreen = bottomIndex - topIndex;
  796. if (selectionIndex >= bottomIndex)
  797. {
  798. Int newTop = max( 0, selectionIndex - max( 1, rowsOnScreen / 2 ) );
  799. //The trouble is that rowsonscreen/2 can be zero if bottom is 1 and top is zero
  800. GadgetListBoxSetTopVisibleEntry( listbox, newTop );
  801. }
  802. }
  803. return selectionIndex;
  804. } // end loadMapListbox
  805. //-------------------------------------------------------------------------------------------------
  806. /** Load the listbox with all the map files available to play */
  807. //-------------------------------------------------------------------------------------------------
  808. Int populateMapListbox( GameWindow *listbox, Bool useSystemMaps, Bool isMultiplayer, AsciiString mapToSelect )
  809. {
  810. if(!TheMapCache)
  811. return -1;
  812. if (!listbox)
  813. return -1;
  814. // reset the listbox content
  815. GadgetListBoxReset( listbox );
  816. return populateMapListboxNoReset( listbox, useSystemMaps, isMultiplayer, mapToSelect );
  817. }
  818. //-------------------------------------------------------------------------------------------------
  819. /** Validate a map */
  820. //-------------------------------------------------------------------------------------------------
  821. Bool isValidMap( AsciiString mapName, Bool isMultiplayer )
  822. {
  823. if(!TheMapCache || mapName.isEmpty())
  824. return FALSE;
  825. TheMapCache->updateCache();
  826. mapName.toLower();
  827. MapCache::iterator it = TheMapCache->find(mapName);
  828. if (it != TheMapCache->end())
  829. {
  830. if (isMultiplayer == it->second.m_isMultiplayer)
  831. {
  832. return TRUE;
  833. }
  834. }
  835. return FALSE;
  836. } // end isValidMap
  837. //-------------------------------------------------------------------------------------------------
  838. /** Find a valid map */
  839. //-------------------------------------------------------------------------------------------------
  840. AsciiString getDefaultMap( Bool isMultiplayer )
  841. {
  842. if(!TheMapCache)
  843. return AsciiString::TheEmptyString;
  844. TheMapCache->updateCache();
  845. MapCache::iterator it = TheMapCache->begin();
  846. while (it != TheMapCache->end())
  847. {
  848. if (isMultiplayer == it->second.m_isMultiplayer)
  849. {
  850. return it->first;
  851. }
  852. ++it;
  853. }
  854. return AsciiString::TheEmptyString;
  855. } // end isValidMap
  856. const MapMetaData *MapCache::findMap(AsciiString mapName)
  857. {
  858. mapName.toLower();
  859. MapCache::iterator it = find(mapName);
  860. if (it == end())
  861. return NULL;
  862. return &(it->second);
  863. }
  864. // ------------------------------------------------------------------------------------------------
  865. /** Embed the pristine map into the xfer stream */
  866. // ------------------------------------------------------------------------------------------------
  867. static void copyFromBigToDir( const AsciiString& infile, const AsciiString& outfile )
  868. {
  869. // open the map file
  870. File *file = TheFileSystem->openFile( infile.str(), File::READ | File::BINARY );
  871. if( file == NULL )
  872. {
  873. DEBUG_CRASH(( "copyFromBigToDir - Error opening source file '%s'\n", infile.str() ));
  874. throw SC_INVALID_DATA;
  875. } // end if
  876. // how big is the map file
  877. Int fileSize = file->seek( 0, File::END );
  878. // rewind to beginning of file
  879. file->seek( 0, File::START );
  880. // allocate buffer big enough to hold the entire map file
  881. char *buffer = NEW char[ fileSize ];
  882. if( buffer == NULL )
  883. {
  884. DEBUG_CRASH(( "copyFromBigToDir - Unable to allocate buffer for file '%s'\n", infile.str() ));
  885. throw SC_INVALID_DATA;
  886. } // end if
  887. // copy the file to the buffer
  888. if( file->read( buffer, fileSize ) < fileSize )
  889. {
  890. DEBUG_CRASH(( "copyFromBigToDir - Error reading from file '%s'\n", infile.str() ));
  891. throw SC_INVALID_DATA;
  892. } // end if
  893. // close the BIG file
  894. file->close();
  895. File *filenew = TheFileSystem->openFile( outfile.str(), File::WRITE | File::CREATE | File::BINARY );
  896. if( !filenew || filenew->write(buffer, fileSize) < fileSize)
  897. {
  898. DEBUG_CRASH(( "copyFromBigToDir - Error writing to file '%s'\n", outfile.str() ));
  899. throw SC_INVALID_DATA;
  900. } // end if
  901. filenew->close();
  902. // delete the buffer
  903. delete [] buffer;
  904. } // end embedPristineMap
  905. Image *getMapPreviewImage( AsciiString mapName )
  906. {
  907. if(!TheGlobalData)
  908. return NULL;
  909. DEBUG_LOG(("%s Map Name \n", mapName.str()));
  910. AsciiString tgaName = mapName;
  911. AsciiString name;
  912. AsciiString tempName;
  913. AsciiString filename;
  914. tgaName.removeLastChar(); // p
  915. tgaName.removeLastChar(); // a
  916. tgaName.removeLastChar(); // m
  917. tgaName.removeLastChar(); // .
  918. name = tgaName;//.reverseFind('\\') + 1;
  919. filename = tgaName.reverseFind('\\') + 1;
  920. //tgaName = name;
  921. filename.concat(".tga");
  922. tgaName.concat(".tga");
  923. AsciiString portableName = TheGameState->realMapPathToPortableMapPath(name);
  924. tempName.set(AsciiString::TheEmptyString);
  925. for(Int i = 0; i < portableName.getLength(); ++i)
  926. {
  927. char c = portableName.getCharAt(i);
  928. if (c == '\\' || c == ':')
  929. tempName.concat('_');
  930. else
  931. tempName.concat(c);
  932. }
  933. name = tempName;
  934. name.concat(".tga");
  935. // copy file over
  936. // copy source tgaName, to name
  937. Image *image = (Image *)TheMappedImageCollection->findImageByName(tempName);
  938. if(!image)
  939. {
  940. if(!TheFileSystem->doesFileExist(tgaName.str()))
  941. return NULL;
  942. AsciiString mapPreviewDir;
  943. mapPreviewDir.format(MAP_PREVIEW_DIR_PATH, TheGlobalData->getPath_UserData().str());
  944. TheFileSystem->createDirectory(mapPreviewDir);
  945. mapPreviewDir.concat(name);
  946. Bool success = false;
  947. try
  948. {
  949. copyFromBigToDir(tgaName, mapPreviewDir);
  950. success = true;
  951. }
  952. catch (...)
  953. {
  954. success = false; // no rethrow
  955. }
  956. if (success)
  957. {
  958. image = TheMappedImageCollection->newImage();
  959. image->setName(tempName);
  960. //image->setFullPath("mission.tga");
  961. image->setFilename(name);
  962. image->setStatus(IMAGE_STATUS_NONE);
  963. Region2D uv;
  964. uv.hi.x = 1.0f;
  965. uv.hi.y = 1.0f;
  966. uv.lo.x = 0.0f;
  967. uv.lo.y = 0.0f;
  968. image->setUV(&uv);
  969. image->setTextureHeight(128);
  970. image->setTextureWidth(128);
  971. }
  972. else
  973. {
  974. image = NULL;
  975. }
  976. }
  977. return image;
  978. /*
  979. // sanity
  980. if( mapName.isEmpty() )
  981. return NULL;
  982. Region2D uv;
  983. mapPreviewImage = TheMappedImageCollection->findImageByName("MapPreview");
  984. if(mapPreviewImage)
  985. mapPreviewImage->deleteInstance();
  986. mapPreviewImage = TheMappedImageCollection->newImage();
  987. mapPreviewImage->setName("MapPreview");
  988. mapPreviewImage->setStatus(IMAGE_STATUS_RAW_TEXTURE);
  989. // allocate our terrain texture
  990. TextureClass * texture = new TextureClass( size.x, size.y,
  991. WW3D_FORMAT_X8R8G8B8, TextureClass::MIP_LEVELS_1 );
  992. uv.lo.x = 0.0f;
  993. uv.lo.y = 1.0f;
  994. uv.hi.x = 1.0f;
  995. uv.hi.y = 0.0f;
  996. mapPreviewImage->setStatus( IMAGE_STATUS_RAW_TEXTURE );
  997. mapPreviewImage->setRawTextureData( texture );
  998. mapPreviewImage->setUV( &uv );
  999. mapPreviewImage->setTextureWidth( size.x );
  1000. mapPreviewImage->setTextureHeight( size.y );
  1001. mapPreviewImage->setImageSize( &size );
  1002. CachedFileInputStream theInputStream;
  1003. if (theInputStream.open(AsciiString(mapName.str())))
  1004. {
  1005. ChunkInputStream *pStrm = &theInputStream;
  1006. pStrm->absoluteSeek(0);
  1007. DataChunkInput file( pStrm );
  1008. if (file.isValidFileType()) { // Backwards compatible files aren't valid data chunk files.
  1009. // Read the waypoints.
  1010. file.registerParser( AsciiString("MapPreview"), AsciiString::TheEmptyString, parseMapPreviewChunk );
  1011. if (!file.parse(NULL)) {
  1012. DEBUG_ASSERTCRASH(false,("Unable to read MapPreview info."));
  1013. mapPreviewImage->deleteInstance();
  1014. return NULL;
  1015. }
  1016. }
  1017. theInputStream.close();
  1018. }
  1019. else
  1020. {
  1021. mapPreviewImage->deleteInstance();
  1022. return NULL;
  1023. }
  1024. return mapPreviewImage;
  1025. */
  1026. return NULL;
  1027. }
  1028. Bool parseMapPreviewChunk(DataChunkInput &file, DataChunkInfo *info, void *userData)
  1029. {
  1030. /*
  1031. ICoord2D size;
  1032. SurfaceClass *surface;
  1033. size.x = file.readInt();
  1034. size.y = file.readInt();
  1035. surface = (TextureClass *)mapPreviewImage->getRawTextureData()->Get_Surface_Level();
  1036. //texture->Get_Surface_Level();
  1037. DEBUG_LOG(("BeginMapPreviewInfo\n"));
  1038. UnsignedInt *buffer = new UnsignedInt[size.x * size.y];
  1039. Int x,y;
  1040. for (y=0; y<size.y; y++) {
  1041. for(x = 0; x< size.x; x++)
  1042. {
  1043. surface->DrawPixel( x, y, file.readInt() );
  1044. buffer[y + x] = file.readInt();
  1045. DEBUG_LOG(("x:%d, y:%d, %X\n", x, y, buffer[y + x]));
  1046. }
  1047. }
  1048. mapPreviewImage->setRawTextureData(buffer);
  1049. DEBUG_ASSERTCRASH(file.atEndOfChunk(), ("Unexpected data left over."));
  1050. DEBUG_LOG(("EndMapPreviewInfo\n"));
  1051. REF_PTR_RELEASE(surface);
  1052. return true;
  1053. */
  1054. return FALSE;
  1055. }
  1056. void findDrawPositions( Int startX, Int startY, Int width, Int height, Region3D extent,
  1057. ICoord2D *ul, ICoord2D *lr )
  1058. {
  1059. Real ratioWidth;
  1060. Real ratioHeight;
  1061. Coord2D radar;
  1062. ratioWidth = extent.width()/(width * 1.0f);
  1063. ratioHeight = extent.height()/(height* 1.0f);
  1064. if( ratioWidth >= ratioHeight)
  1065. {
  1066. radar.x = extent.width() / ratioWidth;
  1067. radar.y = extent.height()/ ratioWidth;
  1068. ul->x = 0;
  1069. ul->y = (height - radar.y) / 2.0f;
  1070. lr->x = radar.x;
  1071. lr->y = height - ul->y;
  1072. }
  1073. else
  1074. {
  1075. radar.x = extent.width() / ratioHeight;
  1076. radar.y = extent.height()/ ratioHeight;
  1077. ul->x = (width - radar.x ) / 2.0f;
  1078. ul->y = 0;
  1079. lr->x = width - ul->x;
  1080. lr->y = radar.y;
  1081. }
  1082. // make them pixel positions
  1083. ul->x += startX;
  1084. ul->y += startY;
  1085. lr->x += startX;
  1086. lr->y += startY;
  1087. } // end findDrawPositions