EditorObjectMgr.cpp 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780
  1. #define EDITOROBJECTMGR_CPP
  2. //===========================================================================//
  3. // Copyright (C) Microsoft Corporation. All rights reserved. //
  4. //===========================================================================//
  5. #ifndef BDACTOR_H
  6. #include "bdActor.h"
  7. #endif
  8. #include "EditorObjectMgr.h"
  9. #ifndef FILE_H
  10. #include "File.h"
  11. #endif
  12. #ifndef UTILITIES_H
  13. #include "Utilities.h"
  14. #endif
  15. #ifndef TERRAIN_H
  16. #include "Terrain.h"
  17. #endif
  18. #ifndef ERR_H
  19. #include "Err.h"
  20. #endif
  21. #ifndef CAMERA_H
  22. #include "Camera.h"
  23. #endif
  24. #ifndef PATHS_H
  25. #include "Paths.h"
  26. #endif
  27. #ifndef CIDENT_H
  28. #include "cident.h"
  29. #endif
  30. #ifndef PACKET_H
  31. #include "Packet.h"
  32. #endif
  33. #ifndef CAMERA_H
  34. #include "camera.h"
  35. #endif
  36. #ifndef BDACTOR_H
  37. #include "bdActor.h"
  38. #endif
  39. #ifndef MECH3D_H
  40. #include "Mech3d.h"
  41. #endif
  42. #ifndef MESSAGEBOX_H
  43. #include "MessageBox.h"
  44. #endif
  45. #ifndef APPRTYPE_H
  46. #include "apprtype.h"
  47. #endif
  48. #ifndef TACMAP_H
  49. #include "tacMap.h"
  50. #endif
  51. #ifndef EDITORDATA_H
  52. #include "EditorData.h"
  53. #endif
  54. #include "resource.h"
  55. #include "McLibResource.h"
  56. #include "BuildingLink.h"
  57. #include "Utilities.h"
  58. #include "malloc.h"
  59. #include "assert.h"
  60. // ARM
  61. #include "../ARM/Microsoft.Xna.Arm.h"
  62. using namespace Microsoft::Xna::Arm;
  63. EditorObjectMgr* EditorObjectMgr::s_instance = NULL;
  64. unsigned long gameResourceHandle = 0;
  65. extern bool renderObjects;
  66. extern bool renderTrees;
  67. extern bool justResaveAllMaps;
  68. #define TREE_GROUP 1
  69. inline float agsqrt( float _a, float _b )
  70. {
  71. return sqrt(_a*_a + _b*_b);
  72. }
  73. EditorObjectMgr::EditorObjectMgr()
  74. {
  75. gosASSERT( !s_instance );
  76. s_instance = this; // should only be one of these
  77. }
  78. //*************************************************************************************************
  79. EditorObjectMgr::~EditorObjectMgr()
  80. {
  81. clear();
  82. if ( appearanceTypeList )
  83. delete appearanceTypeList;
  84. appearanceTypeList = NULL;
  85. for( GROUP_LIST::EIterator groupIter = groups.Begin();
  86. !groupIter.IsDone(); groupIter++ )
  87. {
  88. for( EList< Building, Building& >::EIterator buildIter = (*groupIter).buildings.Begin();
  89. !buildIter.IsDone(); buildIter++ )
  90. {
  91. Building teste = (*buildIter);
  92. if ( (*buildIter ).varNames )
  93. {
  94. for ( int i = 0; i < 16; i++ )
  95. {
  96. if ( (*buildIter).varNames[i] )
  97. {
  98. free ( ( *buildIter).varNames[i] );
  99. (*buildIter).varNames[i] = NULL;
  100. }
  101. }
  102. //Kinda need to delete this OUTSIDE the above loop.
  103. // Yum, Yum!
  104. free ( ( *buildIter ).varNames );
  105. ( *buildIter ).varNames = NULL;
  106. }
  107. }
  108. }
  109. s_instance = 0;
  110. }
  111. //*************************************************************************************************
  112. void EditorObjectMgr::clear()
  113. {
  114. for ( BUILDING_LIST::EIterator iter = buildings.Begin();
  115. !iter.IsDone(); iter++ )
  116. {
  117. delete *iter;
  118. }
  119. for ( UNIT_LIST::EIterator mIter = units.Begin();
  120. !mIter.IsDone(); mIter++ )
  121. {
  122. Unit* pMech = *mIter;
  123. delete pMech;
  124. }
  125. buildings.Clear();
  126. units.Clear();
  127. for ( LINK_LIST::EIterator liter = links.Begin(); !liter.IsDone(); liter++ )
  128. delete *liter;
  129. links.Clear();
  130. selectedObjects.Clear();
  131. selectedObjectsNeedsToBeSynched = false;
  132. for ( DROP_LIST::EIterator dIter = dropZones.Begin(); !dIter.IsDone(); dIter++ )
  133. delete *dIter;
  134. dropZones.Clear();
  135. resetAvailableSquadNums();
  136. for( GROUP_LIST::EIterator groupIter = groups.Begin();!groupIter.IsDone(); groupIter++ )
  137. {
  138. for( EList< Building, Building& >::EIterator buildIter = (*groupIter).buildings.Begin();!buildIter.IsDone(); buildIter++ )
  139. {
  140. (*buildIter).appearanceType = NULL;
  141. }
  142. }
  143. for ( FOREST_LIST::EIterator fIter = forests.Begin(); !fIter.IsDone(); fIter++ )
  144. {
  145. delete ( *fIter );
  146. }
  147. forests.Clear();
  148. }
  149. //*************************************************************************************************
  150. void EditorObjectMgr::init( const char* bldgListFileName, const char* objectFileName )
  151. {
  152. File lstFile;
  153. lstFile.open( const_cast< char* >(bldgListFileName) );
  154. unsigned char buffer[512];
  155. lstFile.readLine( buffer, 512 );
  156. PacketFile objectFile;
  157. long result = objectFile.open( const_cast< char* >(objectFileName) );
  158. gosASSERT(result == NO_ERR);
  159. while( lstFile.getLogicalPosition() < lstFile.getLength() )
  160. {
  161. lstFile.readLine( buffer, 512 );
  162. if ( !strlen( (char*)buffer ) )
  163. break;
  164. Building bldg;
  165. bldg.appearanceType = NULL;
  166. bldg.specialType = UNSPECIAL;
  167. bldg.varNames = 0;
  168. bldg.forestId = 0;
  169. char tmpBuffer[64];
  170. int bufferLength = 64;
  171. unsigned char* pCur = buffer;
  172. char GroupName[64];
  173. // extract the file name
  174. if (ExtractNextString( pCur, tmpBuffer, bufferLength ) )
  175. strcpy( bldg.fileName, tmpBuffer );
  176. // extract the group
  177. if ( ExtractNextString( pCur, tmpBuffer, bufferLength ) )
  178. {
  179. int group = atoi( tmpBuffer );
  180. cLoadString( IDS_GROUPS + group, GroupName, 64, gameResourceHandle );
  181. }
  182. // extract the building name
  183. if ( ExtractNextString( pCur, tmpBuffer, bufferLength ) )
  184. {
  185. int strId = atoi( tmpBuffer );
  186. bldg.nameID = strId;
  187. cLoadString( strId, tmpBuffer, 64 );
  188. strcpy( bldg.name, tmpBuffer );
  189. }
  190. // extract the type
  191. if ( ExtractNextString( pCur, tmpBuffer, bufferLength ) )
  192. {
  193. if ( stricmp( tmpBuffer, "BUILDING" ) == 0 )
  194. bldg.type = BLDG_TYPE;
  195. else if ( stricmp( tmpBuffer, "TREE" ) == 0 )
  196. bldg.type = TREED_TYPE;
  197. else if ( stricmp( tmpBuffer, "VEHICLE" ) == 0 )
  198. bldg.type = GV_TYPE;
  199. else
  200. bldg.type = MECH_TYPE;
  201. }
  202. if ( ExtractNextString( pCur, tmpBuffer, bufferLength ) )
  203. {
  204. bldg.fitID = atoi( tmpBuffer );
  205. if ( NO_ERR != objectFile.seekPacket(bldg.fitID) )
  206. gosASSERT( false );
  207. int fileSize = objectFile.getPacketSize();
  208. FitIniFile bldgFile;
  209. result = bldgFile.open(&objectFile, fileSize);
  210. unsigned long lowTemplate, highTemplate;
  211. bldgFile.readIdULong( "LowTemplate", lowTemplate );
  212. bldgFile.readIdULong( "HighTemplate", highTemplate );
  213. bldg.impassability = (highTemplate << 32) | lowTemplate;
  214. bldgFile.readIdBoolean( "BlocksLineOfFire", bldg.blocksLineOfFire );
  215. bldgFile.seekBlock("ObjectClass");
  216. bldgFile.readIdLong("ObjectTypeNum", bldg.objectTypeNum);
  217. bldgFile.seekBlock("General");
  218. bldg.isHoverCraft = false;
  219. bldgFile.readIdBoolean("HoverCraft", bldg.isHoverCraft);
  220. bldgFile.seekBlock("ObjectType");
  221. char tmpBuf[2000];
  222. strcpy(tmpBuf, "");
  223. bldgFile.readIdString("Name", tmpBuf, 1000);
  224. if (0 == strcmp(tmpBuf, "monsoon")) {
  225. int i = 11;
  226. }
  227. bldgFile.close();
  228. }
  229. // extract the special type type
  230. if ( ExtractNextString( pCur, tmpBuffer, bufferLength ) )
  231. {
  232. if ( strstr( tmpBuffer, "TREE" ) )
  233. {
  234. bldg.forestId = atoi( tmpBuffer + 4 );
  235. }
  236. else if ( stricmp( tmpBuffer, "GATE" ) == 0 )
  237. bldg.specialType = EDITOR_GATE;
  238. else if ( stricmp( tmpBuffer, "GATE_CONTROL" ) == 0 )
  239. bldg.specialType = GATE_CONTROL;
  240. else if ( stricmp( tmpBuffer, "POWER_GENERATOR" ) == 0 )
  241. bldg.specialType = POWER_STATION;
  242. else if ( stricmp( tmpBuffer, "SENSOR_CONTROL" ) == 0 )
  243. bldg.specialType = SENSOR_CONTROL;
  244. else if ( stricmp( tmpBuffer, "SENSOR" ) == 0 )
  245. bldg.specialType = SENSOR_TOWER;
  246. else if ( stricmp( tmpBuffer, "SPOTLIGHT_CONTROL" ) == 0 )
  247. bldg.specialType = SPOTLIGHT_CONTROL;
  248. else if ( stricmp( tmpBuffer, "SPOTLIGHT" ) == 0 )
  249. bldg.specialType = SPOTLIGHT;
  250. else if ( stricmp( tmpBuffer, "LOOKOUT" ) == 0 )
  251. bldg.specialType = LOOKOUT;
  252. else if ( stricmp( tmpBuffer, "TURRET_CONTROL" ) == 0 )
  253. bldg.specialType = TURRET_CONTROL;
  254. else if ( stricmp( tmpBuffer, "TURRET_GENERATOR" ) == 0 )
  255. bldg.specialType = TURRET_GENERATOR;
  256. else if ( stricmp( tmpBuffer, "BRIDGE_CONTROL" ) == 0 )
  257. bldg.specialType = BRIDGE_CONTROL;
  258. else if ( stricmp( tmpBuffer, "BRIDGE" ) == 0 )
  259. bldg.specialType = EDITOR_BRIDGE;
  260. else if ( stricmp( tmpBuffer, "DROPZONE" ) == 0 )
  261. bldg.specialType = DROPZONE;
  262. else if ( stricmp( tmpBuffer, "TURRET" ) == 0 )
  263. bldg.specialType = EDITOR_TURRET;
  264. else if ( stricmp( tmpBuffer, "NAVMARKER" ) == 0 )
  265. bldg.specialType = NAV_MARKER;
  266. else if ( stricmp( tmpBuffer, "WALL" ) == 0 )
  267. bldg.specialType = WALL;
  268. else if ( stricmp( tmpBuffer, "RPBUILDING" ) == 0 )
  269. bldg.specialType = RESOURCE_BUILDING;
  270. else if ( stricmp( tmpBuffer, "HELICOPTER" ) == 0 )
  271. bldg.specialType = HELICOPTER;
  272. }
  273. bldg.capturable = ExtractNextInt( pCur ) ? true : false;
  274. bldg.alignable = ExtractNextInt( pCur ) ? true : false;
  275. bldg.writeOnTacMap = ExtractNextInt( pCur );
  276. if (ExtractNextString(pCur, tmpBuffer, bufferLength))
  277. strcpy(bldg.tgaName,tmpBuffer);
  278. else
  279. strcpy(bldg.tgaName,"NONE");
  280. ExtractNextFloat( pCur );
  281. bldg.scale = ExtractNextFloat( pCur );
  282. // go out and find the variant file
  283. CSVFile csvFile;
  284. FullPathFileName csvFileName;
  285. csvFileName.init(objectPath,bldg.fileName,".csv");
  286. char varName[256];
  287. if ( NO_ERR == csvFile.open( csvFileName ) )
  288. {
  289. bldg.varNames = (char**) malloc( sizeof( char* ) * 16 );
  290. for ( int i = 0; i < 16; ++i )
  291. {
  292. if ( NO_ERR == csvFile.readString( 23 + 97 * i, 2, varName, 256 ) && strlen( varName ) )
  293. {
  294. bldg.varNames[i] = (char *) malloc( sizeof( char ) * ( strlen( varName ) + 1 ) );
  295. strcpy( bldg.varNames[i], varName );
  296. }
  297. else
  298. bldg.varNames[i] = NULL;
  299. }
  300. }
  301. if ( bldg.specialType == DROPZONE )
  302. {
  303. strcpy( dropZoneGroup.name, GroupName );
  304. dropZoneGroup.buildings.Append( bldg );
  305. }
  306. else
  307. {
  308. bool bNeedGroup = true;
  309. // now need to add this building
  310. for ( GROUP_LIST::EIterator iter = groups.Begin(); !iter.IsDone(); iter++ )
  311. {
  312. if ( strcmp( (*iter).name, GroupName ) == 0 )
  313. {
  314. bNeedGroup = false;
  315. (*iter).buildings.Append( bldg );
  316. }
  317. }
  318. if ( bNeedGroup )
  319. {
  320. Group grp;
  321. strcpy( grp.name, GroupName );
  322. grp.buildings.Append( bldg );
  323. groups.Append( grp );
  324. }
  325. }
  326. }
  327. }
  328. int EditorObjectMgr::ExtractNextString( unsigned char*& pFileLine, char* pBuffer, int bufferLength )
  329. {
  330. for ( int i = 0; i < 512; ++i )
  331. {
  332. if ( pFileLine[i] == '\n' )
  333. break;
  334. else if ( pFileLine[i] == ',' )
  335. break;
  336. else if ( pFileLine[i] == NULL )
  337. break;
  338. }
  339. if ( i == 512 )
  340. return false;
  341. gosASSERT( i < bufferLength );
  342. memcpy( pBuffer, pFileLine, i );
  343. pBuffer[i] = NULL;
  344. bufferLength = i + 1;
  345. pFileLine += i + 1;
  346. return i;
  347. }
  348. //---------------------------------------------------------------------------
  349. long textToLong (const char *num)
  350. {
  351. long result = 0;
  352. //------------------------------------
  353. // Check if Hex Number
  354. char *hexOffset = (char*)strstr(num,"0x");
  355. if (hexOffset == NULL)
  356. {
  357. result = atol(num);
  358. }
  359. else
  360. {
  361. hexOffset += 2;
  362. long numDigits = strlen(hexOffset)-1;
  363. for (int i=0; i<=numDigits; i++)
  364. {
  365. if (!isalnum(hexOffset[i]) || (isalpha(hexOffset[i]) && toupper(hexOffset[i]) > 'F'))
  366. {
  367. hexOffset[i] = 0; // we've reach a "wrong" character. Either start of a comment or something illegal. Either way, stop evaluation here.
  368. break;
  369. }
  370. }
  371. numDigits = strlen(hexOffset)-1;
  372. long power = 0;
  373. for (long count = numDigits;count >= 0;count--,power++)
  374. {
  375. unsigned char currentDigit = toupper(hexOffset[count]);
  376. if (currentDigit >= 'A' && currentDigit <= 'F')
  377. {
  378. result += (currentDigit - 'A' + 10)<<(4*power);
  379. }
  380. else if (currentDigit >= '0' && currentDigit <= '9')
  381. {
  382. result += (currentDigit - '0')<<(4*power);
  383. }
  384. else
  385. {
  386. //---------------------------------------------------------
  387. // There is a digit in here I don't understand. Return 0.
  388. result = 0;
  389. break;
  390. }
  391. }
  392. }
  393. return(result);
  394. }
  395. int EditorObjectMgr::ExtractNextInt( unsigned char*& pFileLine )
  396. {
  397. char buffer[1024];
  398. int count = ExtractNextString( pFileLine, buffer, 1024 );
  399. if ( count > 0 )
  400. {
  401. return textToLong( buffer );
  402. }
  403. return -1;
  404. }
  405. float EditorObjectMgr::ExtractNextFloat( unsigned char*& pFileLine )
  406. {
  407. char buffer[1024];
  408. int count = ExtractNextString( pFileLine, buffer, 1024 );
  409. if ( count > 0 )
  410. {
  411. return atof( buffer );
  412. }
  413. return -1;
  414. }
  415. //-------------------------------------------------------------------------------------------------
  416. bool EditorObjectMgr::canAddBuilding( const Stuff::Vector3D& position,
  417. float rotation,
  418. unsigned long group,
  419. unsigned long indexWithinGroup )
  420. {
  421. long realCellI, realCellJ;
  422. land->worldToCell( position, realCellJ, realCellI );
  423. EditorObject * cellObject = getObjectAtCell( realCellJ, realCellI );
  424. if ( cellObject )
  425. {
  426. if (cellObject->appearance()->rotation == rotation )
  427. return false;
  428. }
  429. //Check if we are ON the map!!
  430. return land->IsEditorSelectTerrainPosition(position);
  431. }
  432. bool EditorObjectMgr::canAddDropZone( const Stuff::Vector3D& position, int alignment, bool bVTol )
  433. {
  434. int alignmentCount = 0;
  435. for ( DROP_LIST::EIterator iter = dropZones.Begin(); !iter.IsDone(); iter++ )
  436. {
  437. if (bVTol)
  438. {
  439. if ( (*iter)->isVTol() )
  440. return false;
  441. }
  442. else
  443. {
  444. if ( (*iter)->getAlignment() == alignment && !(*iter)->isVTol() )
  445. alignmentCount++;
  446. }
  447. }
  448. if ( alignmentCount > 3 )
  449. return false;
  450. return true;
  451. }
  452. //-------------------------------------------------------------------------------------------------
  453. bool EditorObjectMgr::addBuilding( EditorObject* pObj )
  454. {
  455. return addBuilding( pObj->appearance()->position, getGroup( pObj->id ),
  456. getIndexInGroup( pObj->id ), pObj->appearance()->teamId, pObj->appearance()->rotation ) ?
  457. true : false;
  458. }
  459. EditorObject* EditorObjectMgr::addDropZone( const Stuff::Vector3D& position, int alignment, bool bVTol )
  460. {
  461. gosASSERT( (canAddDropZone( position, alignment, bVTol ) ) );
  462. DropZone* pLast = new DropZone( position, alignment, bVTol );
  463. dropZones.Append( pLast ) ;
  464. ObjectAppearance* pAppear = getAppearance( &dropZoneGroup.buildings.GetHead() );
  465. pLast->appearInfo->appearance = pAppear;
  466. land->worldToCell( position, pLast->cellRow, pLast->cellColumn );
  467. pLast->id = DROPZONE << 24 | -1<< 16 | -1 << 8;
  468. Stuff::Vector3D realPos;
  469. land->getCellPos( pLast->cellRow, pLast->cellColumn, realPos );
  470. long homeRelations[9] = {0, 0, 2, 1, 1, 1, 1, 1, 1};
  471. gosASSERT((8 > alignment) && (-1 <= alignment));
  472. pLast->appearance()->setObjectParameters( realPos, 0, false, alignment,homeRelations[alignment+1]);
  473. eye->update();
  474. pLast->appearance()->update();
  475. pLast->appearance()->setInView( true );
  476. pLast->appearance()->rotation = 0;
  477. pLast->appearance()->teamId = alignment;
  478. return pLast;
  479. }
  480. //-------------------------------------------------------------------------------------------------
  481. EditorObject* EditorObjectMgr::getObjectAtPosition( const Stuff::Vector3D& position )
  482. {
  483. Stuff::Vector4D screenPos;
  484. Stuff::Vector4D tmpPos;
  485. eye->projectZ( const_cast<Stuff::Vector3D&>(position), screenPos );
  486. for( BUILDING_LIST::EIterator iter = buildings.Begin();
  487. !iter.IsDone(); iter++ )
  488. {
  489. ObjectAppearance* pAppearance = (*iter)->appearance();
  490. if (pAppearance->canBeSeen())
  491. {
  492. int left, top, right, bottom;
  493. left = pAppearance->upperLeft.x;
  494. top = pAppearance->upperLeft.y;
  495. right = pAppearance->lowerRight.x;
  496. bottom = pAppearance->lowerRight.y;
  497. eye->projectZ( pAppearance->position, tmpPos );
  498. if ( screenPos.x >= left && screenPos.x <= right
  499. && screenPos.y >= top && screenPos.y <= bottom
  500. && pAppearance->PerPolySelect(screenPos.x,screenPos.y))
  501. {
  502. return (*iter);
  503. }
  504. }
  505. }
  506. for( UNIT_LIST::EIterator mIter = units.Begin();
  507. !mIter.IsDone(); mIter++ )
  508. {
  509. ObjectAppearance* pAppearance = (*mIter)->appearance();
  510. if (pAppearance->canBeSeen())
  511. {
  512. int left, top, right, bottom;
  513. left = pAppearance->upperLeft.x;
  514. top = pAppearance->upperLeft.y;
  515. right = pAppearance->lowerRight.x;
  516. bottom = pAppearance->lowerRight.y;
  517. eye->projectZ( pAppearance->position, tmpPos );
  518. if ( screenPos.x >= left && screenPos.x <= right
  519. && screenPos.y >= top && screenPos.y <= bottom )
  520. {
  521. return (*mIter);
  522. }
  523. }
  524. }
  525. for ( DROP_LIST::EIterator dIter = dropZones.Begin(); !dIter.IsDone(); dIter++ )
  526. {
  527. // need to change this when we get drop zone art
  528. Stuff::Vector3D position = (*dIter)->getPosition();
  529. eye->projectZ( position, tmpPos );
  530. if ( screenPos.x >= tmpPos.x - 3 && screenPos.x <= tmpPos.x + 3 && screenPos.y <= tmpPos.y + 3 &&
  531. screenPos.y >= tmpPos.y -3 )
  532. return *dIter;
  533. }
  534. return NULL;
  535. }
  536. //-------------------------------------------------------------------------------------------------
  537. static bool areCloseEnough(float f1, float f2)
  538. {
  539. bool retval = false;
  540. float diff = fabs(f1 - f2);
  541. float bigger = fabs(f1);
  542. if (fabs(f2) > bigger) {
  543. bigger = fabs(f2);
  544. }
  545. float proportion_diff = 0.0;
  546. if (0.0 != bigger) {
  547. proportion_diff = diff / bigger;
  548. }
  549. if ((pow(10.0, -FLT_DIG) * 10.0/*arbitrary*/) > proportion_diff) {
  550. retval = true;
  551. }
  552. return retval;
  553. }
  554. EditorObject* EditorObjectMgr::getObjectAtLocation( float x, float y )
  555. {
  556. for( BUILDING_LIST::EIterator iter = buildings.Begin();
  557. !iter.IsDone(); iter++ )
  558. {
  559. if (areCloseEnough((*iter)->getPosition().x, x) && areCloseEnough((*iter)->getPosition().y, y))
  560. {
  561. return (*iter);
  562. }
  563. }
  564. for( UNIT_LIST::EIterator mIter = units.Begin(); !mIter.IsDone(); mIter++ )
  565. {
  566. if (areCloseEnough((*mIter)->getPosition().x, x) && areCloseEnough((*mIter)->getPosition().y, y))
  567. {
  568. return (*mIter);
  569. }
  570. }
  571. for ( DROP_LIST::EIterator dIter = dropZones.Begin(); !dIter.IsDone(); dIter++ )
  572. {
  573. // need to change this when we get drop zone art
  574. if (areCloseEnough((*dIter)->getPosition().x, x) && areCloseEnough((*dIter)->getPosition().y, y))
  575. {
  576. return (*dIter);
  577. }
  578. }
  579. return NULL;
  580. }
  581. //-------------------------------------------------------------------------------------------------
  582. EditorObject* EditorObjectMgr::addBuilding( const Stuff::Vector3D& position, unsigned long group,
  583. unsigned long indexWithinGroup, int alignment, float rotation,
  584. float scale, bool bSnapToCell )
  585. {
  586. if ( group == 255 )
  587. return addDropZone( position, alignment, 0 );
  588. int type = getType( group, indexWithinGroup );
  589. int specialType = getSpecialType( getID( group, indexWithinGroup ) );
  590. EditorObject* info = NULL;
  591. if ( specialType == NAV_MARKER )
  592. {
  593. info = new NavMarker;
  594. buildings.Append( info );
  595. info->appearInfo->appearance = getAppearance( group, indexWithinGroup );
  596. }
  597. else if ( (type == BLDG_TYPE) || (type == TREED_TYPE) )
  598. {
  599. info = new EditorObject;
  600. buildings.Append( info );
  601. info->appearInfo->appearance = getAppearance( group, indexWithinGroup );
  602. }
  603. else if ( type == GV_TYPE || type == MECH_TYPE )
  604. {
  605. Unit* tmp = new Unit( alignment );
  606. units.Append( tmp );
  607. info = tmp;
  608. info->appearInfo->appearance = getAppearance( group, indexWithinGroup );
  609. unsigned long c1, c2, c3;
  610. tmp->getColors(c1, c2, c3);
  611. tmp->appearance()->resetPaintScheme(c1, c2, c3);
  612. }
  613. land->worldToCell( position, info->cellRow, info->cellColumn );
  614. info->id = type << 24 | group << 16 | indexWithinGroup << 8;
  615. Stuff::Vector3D realPos;
  616. land->getCellPos( info->cellRow, info->cellColumn, realPos );
  617. long homeRelations[9] = {0, 0, 2, 1, 1, 1, 1, 1, 1};
  618. gosASSERT((8 > alignment) && (-1 <= alignment));
  619. Stuff::Vector3D pos = bSnapToCell ? realPos : position;
  620. info->appearance()->setObjectParameters( pos, 0, false, alignment,homeRelations[alignment+1]);
  621. eye->update();
  622. info->appearance()->update();
  623. info->appearance()->setInView( true );
  624. info->appearance()->rotation = rotation;
  625. info->appearance()->teamId = isAlignable( info->id ) ? alignment : EDITOR_TEAMNONE;
  626. if ( scale != 1.0 )
  627. info->appearance()->scale( scale );
  628. return info;
  629. }
  630. //*************************************************************************************************
  631. bool EditorObjectMgr::deleteBuilding( const EditorObject* pInfo )
  632. {
  633. BuildingLink* pLink = getLinkWithParent( pInfo );
  634. if ( pLink )
  635. {
  636. deleteLink( pLink );
  637. }
  638. {
  639. pLink = getLinkWithBuilding( pInfo );
  640. if ( pLink )
  641. {
  642. pLink->RemoveObject( pInfo );
  643. }
  644. }
  645. if ( buildings.Count() && pInfo )
  646. {
  647. for ( BUILDING_LIST::EIterator iter = buildings.End(); !iter.IsDone(); iter-- )
  648. {
  649. if( pInfo->cellColumn == (*iter)->cellColumn
  650. && pInfo->cellRow == (*iter)->cellRow
  651. && pInfo->appearance() == (*iter)->appearance() )
  652. {
  653. delete (*iter);
  654. selectedObjects.RemoveIfThere(*iter);
  655. EditorData::instance->handleObjectInvalidation(*iter);
  656. buildings.Delete( iter );
  657. return true;
  658. }
  659. }
  660. }
  661. if ( units.Count() && pInfo )
  662. {
  663. for ( UNIT_LIST::EIterator mIter = units.End(); !mIter.IsDone(); mIter-- )
  664. {
  665. if( pInfo->cellColumn == (*mIter)->cellColumn
  666. && pInfo->cellRow == (*mIter)->cellRow )
  667. {
  668. delete( *mIter );
  669. selectedObjects.RemoveIfThere(*mIter);
  670. EditorData::instance->handleObjectInvalidation(*mIter);
  671. units.Delete( mIter );
  672. return true;
  673. }
  674. }
  675. }
  676. for ( DROP_LIST::EIterator dIter = dropZones.Begin(); !dIter.IsDone(); dIter++ )
  677. {
  678. if( pInfo->cellColumn == (*dIter)->cellColumn
  679. && pInfo->cellRow == (*dIter)->cellRow )
  680. {
  681. delete( *dIter );
  682. selectedObjects.RemoveIfThere(*dIter);
  683. EditorData::instance->handleObjectInvalidation(*dIter);
  684. dropZones.Delete( dIter );
  685. return true;
  686. }
  687. }
  688. return false;
  689. }
  690. //*************************************************************************************************
  691. int EditorObjectMgr::getBuildingGroupCount() const
  692. {
  693. return groups.Count();
  694. }
  695. //*************************************************************************************************
  696. int EditorObjectMgr::getNumberBuildingsInGroup( int group ) const
  697. {
  698. return groups[group].buildings.Count();
  699. }
  700. //*************************************************************************************************
  701. void EditorObjectMgr::getBuildingGroupNames( const char** names, int& NumberOfNames ) const
  702. {
  703. if ( NumberOfNames < groups.Count() )
  704. NumberOfNames = groups.Count();
  705. int i = 0;
  706. for ( GROUP_LIST::EConstIterator iter = groups.Begin();
  707. !iter.IsDone(); iter++, i++ )
  708. {
  709. if ( i >= NumberOfNames )
  710. return;
  711. names[i] = (*iter).name;
  712. }
  713. }
  714. //*************************************************************************************************
  715. void EditorObjectMgr::getBuildingNamesInGroup( int group, const char** names, int& NumberOfNames ) const
  716. {
  717. Group* pGroup = &groups[group];
  718. if ( NumberOfNames < groups.Count() )
  719. NumberOfNames = pGroup->buildings.Count();
  720. int i = 0;
  721. for ( EList< Building, Building& >::EIterator iter = pGroup->buildings.Begin();
  722. !iter.IsDone(); iter++, i++ )
  723. {
  724. if ( i >= NumberOfNames )
  725. return;
  726. names[i] = (*iter).name;
  727. }
  728. NumberOfNames = i;
  729. }
  730. //*************************************************************************************************
  731. void EditorObjectMgr::update()
  732. {
  733. static long homeRelations[9] = {0, 0, 2, 1, 1, 1, 1, 1, 1};
  734. if (renderObjects)
  735. {
  736. for ( BUILDING_LIST::EIterator iter = buildings.Begin();
  737. !iter.IsDone(); iter++ )
  738. {
  739. currentFloatHelp = 0;
  740. if (renderTrees || (!renderTrees && ((*iter)->appearance()->getAppearanceClass() != TREE_APPR_TYPE)))
  741. {
  742. if ( (*iter)->appearance()->recalcBounds() )
  743. {
  744. Stuff::Vector3D pos = (*iter)->appearance()->position;
  745. long cellR, cellC;
  746. land->worldToCell(pos, cellR, cellC);
  747. /*We're calling what appears to be a redundant moveBuilding() here to
  748. ensure that the objects are level with the terrain (incase the terrain got
  749. altered I guess). Note that moveBuilding() also updates any links that
  750. the building might be a part of.*/
  751. moveBuilding((*iter), cellR, cellC);
  752. pos = (*iter)->appearance()->position;
  753. gosASSERT((8 > (*iter)->appearance()->teamId) && (-1 <= (*iter)->appearance()->teamId));
  754. (*iter)->appearance()->setObjectParameters(pos,(*iter)->appearance()->rotation,(*iter)->appearance()->selected,(*iter)->appearance()->teamId, homeRelations[(*iter)->appearance()->teamId+1]);
  755. (*iter)->appearance()->update();
  756. (*iter)->appearance()->setVisibility(true,true);
  757. }
  758. }
  759. }
  760. }
  761. //Always draw the Mechs/Vehicles
  762. for ( UNIT_LIST::EIterator mIter = units.Begin();
  763. !mIter.IsDone(); mIter++ )
  764. {
  765. EditorObject* pObj = (*mIter);
  766. currentFloatHelp = 0;
  767. if ( pObj->appearance()->recalcBounds() )
  768. {
  769. // Vehicles and mech forced to ground in Object classes in game now.
  770. // NOT in the appearance anymore since they can fly when the die!
  771. // Must force here as well.
  772. Stuff::Vector3D pos = pObj->appearance()->position;
  773. pos.z = land->getTerrainElevation(pos);
  774. if (getIsHoverCraft(pObj->id)) {
  775. float waterElevation = land->getWaterElevation();
  776. if (waterElevation > pos.z) {
  777. pos.z = waterElevation;
  778. }
  779. }
  780. gosASSERT((8 > pObj->appearance()->teamId) && (-1 <= pObj->appearance()->teamId));
  781. pObj->appearance()->setObjectParameters(pos,pObj->appearance()->rotation,pObj->appearance()->selected,pObj->appearance()->teamId,homeRelations[pObj->appearance()->teamId+1]);
  782. if (pObj->getSpecialType() == HELICOPTER)
  783. pObj->appearance()->setMoverParameters(0.0f,0.0f,0.0f,true);
  784. pObj->appearance()->update();
  785. pObj->appearance()->setVisibility(true,true);
  786. }
  787. }
  788. }
  789. //*************************************************************************************************
  790. void EditorObjectMgr::render()
  791. {
  792. //--------------------------------
  793. //Set States for Software Renderer
  794. if (Environment.Renderer == 3)
  795. {
  796. gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_OneZero);
  797. gos_SetRenderState( gos_State_ShadeMode, gos_ShadeGouraud);
  798. gos_SetRenderState( gos_State_MonoEnable, 1);
  799. gos_SetRenderState( gos_State_Perspective, 0);
  800. gos_SetRenderState( gos_State_Clipping, 1);
  801. gos_SetRenderState( gos_State_Specular, 0);
  802. gos_SetRenderState( gos_State_Dither, 0);
  803. gos_SetRenderState( gos_State_TextureMapBlend, gos_BlendModulate);
  804. gos_SetRenderState( gos_State_Filter, gos_FilterNone);
  805. gos_SetRenderState( gos_State_TextureAddress, gos_TextureWrap );
  806. gos_SetRenderState( gos_State_ZCompare, 1);
  807. gos_SetRenderState( gos_State_ZWrite, 1);
  808. }
  809. //--------------------------------
  810. //Set States for Hardware Renderer
  811. else
  812. {
  813. gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_OneZero);
  814. gos_SetRenderState( gos_State_ShadeMode, gos_ShadeGouraud);
  815. gos_SetRenderState( gos_State_MonoEnable, 0);
  816. gos_SetRenderState( gos_State_Perspective, 1);
  817. gos_SetRenderState( gos_State_Clipping, 1);
  818. gos_SetRenderState( gos_State_Specular, 1);
  819. gos_SetRenderState( gos_State_Dither, 1);
  820. gos_SetRenderState( gos_State_TextureMapBlend, gos_BlendModulate);
  821. gos_SetRenderState( gos_State_TextureAddress, gos_TextureWrap );
  822. gos_SetRenderState( gos_State_ZCompare, 1);
  823. gos_SetRenderState( gos_State_ZWrite, 1);
  824. }
  825. if (renderObjects)
  826. {
  827. for ( BUILDING_LIST::EIterator iter = buildings.Begin();
  828. !iter.IsDone(); iter++ )
  829. {
  830. currentFloatHelp = 0;
  831. if (renderTrees || (!renderTrees && ((*iter)->appearance()->getAppearanceClass() != TREE_APPR_TYPE)))
  832. {
  833. if ( (*iter)->appearance()->recalcBounds() )
  834. {
  835. if ( (*iter)->getDamage() )
  836. (*iter)->appearance()->drawBars();
  837. (*iter)->appearance()->render();
  838. if ( (*iter)->getColor() & 0xff000000 )
  839. (*iter)->appearance()->drawSelectBrackets( (*iter)->getColor() );
  840. }
  841. }
  842. }
  843. }
  844. //Always draw the Mechs/Vehicles
  845. for ( UNIT_LIST::EIterator mIter = units.Begin();
  846. !mIter.IsDone(); mIter++ )
  847. {
  848. EditorObject* pObj = (*mIter);
  849. currentFloatHelp = 0;
  850. if ( pObj->appearance()->recalcBounds() )
  851. {
  852. pObj->appearance()->render();
  853. if ( (*mIter)->getDamage() )
  854. pObj->appearance()->drawBars();
  855. if ( (*mIter)->getColor() & 0xff000000 )
  856. pObj->appearance()->drawSelectBrackets( pObj->getColor() );
  857. }
  858. }
  859. // draw the building links
  860. for ( LINK_LIST::EIterator lIter = links.Begin();
  861. !lIter.IsDone(); lIter++ )
  862. {
  863. (*lIter)->render();
  864. }
  865. // draw the drop zones -- this will change as soon as we get art
  866. for ( DROP_LIST::EIterator dIter = dropZones.Begin();
  867. !dIter.IsDone(); dIter++ )
  868. {
  869. Stuff::Vector3D pos = (*dIter)->getPosition();
  870. Stuff::Vector4D screen;
  871. eye->projectZ( pos, screen );
  872. GUI_RECT Rect;
  873. Rect.top = screen.y - 3;
  874. Rect.left = screen.x - 3;
  875. Rect.bottom = screen.y + 3;
  876. Rect.right = screen.x + 3;
  877. drawRect( Rect, (*dIter)->isVTol() ? 0xff0000ff : 0xffffff00 );
  878. }
  879. }
  880. //*************************************************************************************************
  881. void EditorObjectMgr::renderShadows()
  882. {
  883. //-----------------------------------------------------
  884. //Set render states as few times as possible.
  885. //--------------------------------
  886. //Set States for Software Renderer
  887. if (Environment.Renderer == 3)
  888. {
  889. gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_OneZero);
  890. gos_SetRenderState( gos_State_ShadeMode, gos_ShadeFlat);
  891. gos_SetRenderState( gos_State_MonoEnable, 1);
  892. gos_SetRenderState( gos_State_Perspective, 0);
  893. gos_SetRenderState( gos_State_Clipping, 1);
  894. gos_SetRenderState( gos_State_AlphaTest, 0);
  895. gos_SetRenderState( gos_State_Specular, 0);
  896. gos_SetRenderState( gos_State_Dither, 0);
  897. gos_SetRenderState( gos_State_TextureMapBlend, gos_BlendDecal);
  898. gos_SetRenderState( gos_State_Filter, gos_FilterNone);
  899. gos_SetRenderState( gos_State_TextureAddress, gos_TextureWrap );
  900. gos_SetRenderState( gos_State_ZCompare, 0);
  901. gos_SetRenderState( gos_State_ZWrite, 0);
  902. }
  903. //--------------------------------
  904. //Set States for Hardware Renderer
  905. else
  906. {
  907. gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_AlphaInvAlpha);
  908. gos_SetRenderState( gos_State_ShadeMode, gos_ShadeFlat);
  909. gos_SetRenderState( gos_State_MonoEnable, 1);
  910. gos_SetRenderState( gos_State_Perspective, 0);
  911. gos_SetRenderState( gos_State_Clipping, 1);
  912. gos_SetRenderState( gos_State_AlphaTest, 1);
  913. gos_SetRenderState( gos_State_Specular, 0);
  914. gos_SetRenderState( gos_State_Dither, 1);
  915. gos_SetRenderState( gos_State_TextureMapBlend, gos_BlendModulate);
  916. gos_SetRenderState( gos_State_Filter, gos_FilterNone);
  917. gos_SetRenderState( gos_State_TextureAddress, gos_TextureWrap );
  918. gos_SetRenderState( gos_State_ZCompare, 1);
  919. gos_SetRenderState( gos_State_ZWrite, 1);
  920. }
  921. if (renderObjects)
  922. {
  923. for ( BUILDING_LIST::EIterator iter = buildings.Begin(); !iter.IsDone(); iter++ )
  924. {
  925. if (renderTrees || (!renderTrees && ((*iter)->appearance()->getAppearanceClass() != TREE_APPR_TYPE)))
  926. {
  927. if ( (*iter)->appearance()->recalcBounds() )
  928. {
  929. //(*iter)->appearance()->update();
  930. //(*iter)->appearance()->setVisibility(true,true);
  931. (*iter)->appearance()->renderShadows();
  932. //(*iter)->appearance()->render();
  933. }
  934. }
  935. }
  936. }
  937. //Always draw the Mechs/Vehicles
  938. for ( UNIT_LIST::EIterator mIter = units.Begin();
  939. !mIter.IsDone(); mIter++ )
  940. {
  941. if ( (*mIter)->appearance()->recalcBounds() )
  942. {
  943. (*mIter)->appearance()->renderShadows();
  944. }
  945. }
  946. }
  947. //*************************************************************************************************
  948. bool EditorObjectMgr::save( PacketFile& PakFile, int whichPacket )
  949. {
  950. File file;
  951. if ( !buildings.Count() )
  952. {
  953. PakFile.writePacket( whichPacket, NULL );
  954. PakFile.writePacket( whichPacket + 1, NULL );
  955. }
  956. int bufferSize = buildings.Count() * (4 * sizeof(float) + 6 * sizeof(long) ) + 2 * sizeof(long);
  957. char* pBuffer = (char *) malloc( bufferSize );
  958. long* pTacMapPoints = (long *)malloc( sizeof(long) * ( buildings.Count() * 2 + 1 ) );
  959. long* pPoints = pTacMapPoints + 1;
  960. long pointCounter = 0;
  961. file.open(pBuffer, bufferSize );
  962. file.writeLong( buildings.Count() );
  963. gos_VERTEX point;
  964. for ( BUILDING_LIST::EIterator iter = buildings.Begin();
  965. !iter.IsDone(); iter++ )
  966. {
  967. int id = (*iter)->id;
  968. Building& bldg = groups[(id >> 16) & 0x000000ff].buildings[(id >> 8) & 0x000000ff];
  969. if ( bldg.writeOnTacMap )
  970. {
  971. TacMap::worldToTacMap( (*iter)->appearance()->position, 0, 0, 115, 115, point );
  972. *pPoints++ = point.x;
  973. *pPoints++ = point.y;
  974. pointCounter++;
  975. }
  976. file.writeLong( bldg.fitID );
  977. file.writeFloat( (*iter)->appearance()->position.x );
  978. file.writeFloat( (*iter)->appearance()->position.y );
  979. file.writeFloat( (*iter)->appearance()->position.z );
  980. file.writeFloat( (*iter)->appearance()->rotation );
  981. file.writeLong( (*iter)->appearance()->damage );
  982. file.writeLong( (*iter)->appearance()->teamId );
  983. BuildingLink* pLink = getLinkWithChild(*iter);
  984. if ( pLink )
  985. {
  986. Stuff::Vector3D pos = pLink->GetParentPosition();
  987. // convert to cells
  988. long cellI, cellJ;
  989. land->worldToCell( pos, cellJ, cellI );
  990. long val = (cellJ << 16) | cellI;
  991. //How about BEFORE we write this, we make sure there's really a building there!
  992. EditorObject* pObject = getObjectAtCell( cellJ, cellI );
  993. if (!pObject)
  994. {
  995. //Dissolve the link here. Maybe we should warn?
  996. val = 0xffffffff;
  997. PAUSE(("Turret/Gate linked to bldg @ R %d, C %d X:%f Y:%f No Bldg there! Link Deleted",cellJ,cellI,pos.x,pos.y));
  998. }
  999. file.writeLong( val );
  1000. }
  1001. else
  1002. file.writeLong( 0xffffffff );
  1003. // forest stuff
  1004. file.writeLong( (*iter)->getForestID() + 1 );
  1005. if ( (*iter)->getForestID() != -1 )
  1006. file.writeLong( (*iter)->getScale() );
  1007. else
  1008. file.writeLong( 0 );
  1009. }
  1010. int pos = file.getLogicalPosition();
  1011. file.seek( 0 );
  1012. if ( !PakFile.writePacket( whichPacket, (unsigned char*)pBuffer, pos ) )
  1013. {
  1014. gosASSERT( false );
  1015. }
  1016. *pTacMapPoints = pointCounter;
  1017. PakFile.writePacket( whichPacket + 1, (BYTE*)pTacMapPoints, ( pointCounter * 2 + 1 ) * sizeof( long ) );
  1018. free( pTacMapPoints );
  1019. free( pBuffer );
  1020. if (!justResaveAllMaps) {
  1021. int numTurrets = 0;
  1022. for ( BUILDING_LIST::EIterator iter = buildings.Begin(); !iter.IsDone(); iter++ )
  1023. {
  1024. switch ((*iter)->getSpecialType())
  1025. {
  1026. case EDITOR_TURRET:
  1027. {
  1028. numTurrets += 1;
  1029. }
  1030. break;
  1031. }
  1032. }
  1033. if (64/*max turrets*/ < numTurrets )
  1034. {
  1035. CString CStr;
  1036. CStr.Format(IDS_TOO_MANY_TURRETS, numTurrets);
  1037. AfxMessageBox(CStr);
  1038. }
  1039. }
  1040. return true;
  1041. }
  1042. bool EditorObjectMgr::load( PacketFile& PakFile, int whichPacket )
  1043. {
  1044. PakFile.seekPacket( whichPacket );
  1045. int size = PakFile.getPacketSize( );
  1046. char* pBuffer = (char *)malloc(size);
  1047. PakFile.readPacket( whichPacket, (unsigned char*)pBuffer );
  1048. File file;
  1049. file.open( pBuffer, size );
  1050. int count = file.readLong();
  1051. for ( int i = 0; i < count; ++i )
  1052. {
  1053. int id = file.readLong();
  1054. unsigned long group, index;
  1055. if ( !getBuildingFromID(id, group, index, false ) )
  1056. {
  1057. //Until buildings stabilize, just toss 'em when you can't find 'em
  1058. // -fs
  1059. //Assert( false, 0, "Couldn't find the fit id in the building list\n" );
  1060. continue;
  1061. }
  1062. Stuff::Vector3D vector;
  1063. vector.x = file.readFloat();
  1064. vector.y = file.readFloat();
  1065. vector.z = file.readFloat();
  1066. float rotation = file.readFloat();
  1067. EditorObject* pInfo = (EditorObject*)addBuilding( vector, group, index, EDITOR_TEAMNONE, rotation );
  1068. if ( pInfo )
  1069. ((EditorObject*)pInfo)->appearance()->rotation = rotation;
  1070. int damage = file.readLong();
  1071. if ( damage )
  1072. ((EditorObject*)pInfo)->appearance()->setDamage(1);
  1073. pInfo->appearance()->teamId = file.readLong();
  1074. //Most everything can have an alignment now
  1075. // -fs 6/14/2000
  1076. // if ( !isAlignable( pInfo->id ) )
  1077. // pInfo->appearance()->teamId = -1;
  1078. int cells = file.readLong();
  1079. if ( cells && (cells != 0xffffffff)) // this building is linked
  1080. {
  1081. long cellJ = cells >> 16;
  1082. long cellI = cells & 0x0000ffff;
  1083. Stuff::Vector3D pos;
  1084. land->cellToWorld( cellJ, cellI, pos );
  1085. pos.z = land->getTerrainElevation( pos );
  1086. bool bFound = false;
  1087. for ( LINK_LIST::EIterator iter = links.Begin();
  1088. !iter.IsDone(); iter++ )
  1089. {
  1090. if ( (*iter)->GetParentPosition() == pos )
  1091. {
  1092. // we've got the link
  1093. (*iter)->AddChild( pInfo );
  1094. bFound = true;
  1095. break;
  1096. }
  1097. }
  1098. if ( !bFound )
  1099. {
  1100. BuildingLink* pLink = new BuildingLink( pos );
  1101. pLink->AddChild( pInfo );
  1102. links.Append( pLink );
  1103. }
  1104. }
  1105. // forest stuff
  1106. pInfo->setForestID( file.readLong() - 1 );
  1107. long scale = file.readLong();
  1108. if ( pInfo->getForestID() != -1 )
  1109. pInfo->setScale( scale );
  1110. }
  1111. LINK_LIST linksToDelete;
  1112. // gotta go through all the links and make sure they have id's
  1113. for ( LINK_LIST::EIterator iter = links.Begin();
  1114. !iter.IsDone(); iter++ )
  1115. {
  1116. if ( (*iter)->parent.m_ID == -1 )
  1117. {
  1118. // This absolutely positively could never have worked!
  1119. // Why? Because getObjectAtPosition depends upon the object BEING ON SCREEN!!!!!!!
  1120. // At this point, we're still loading!!!
  1121. // We should, instead, be checking getObjectAtCell which ALWAYS works!
  1122. // -fs
  1123. long cellR, cellC;
  1124. land->worldToCell((*iter)->parent.pos,cellR,cellC);
  1125. EditorObject* pObject = getObjectAtCell( cellR, cellC );
  1126. gosASSERT( pObject );
  1127. if ( pObject )
  1128. (*iter)->parent.m_ID = pObject->getID();
  1129. else
  1130. linksToDelete.Append( *iter );
  1131. }
  1132. }
  1133. for ( iter = linksToDelete.Begin(); !iter.IsDone(); iter++ )
  1134. {
  1135. deleteLink( *iter );
  1136. }
  1137. free( pBuffer );
  1138. return true;
  1139. }
  1140. //*************************************************************************************************
  1141. // this will NOT RETURN MECHS AND VEHICLES
  1142. bool EditorObjectMgr::getBuildingFromID( int fitID, unsigned long& group, unsigned long& index, bool canBeMech )
  1143. {
  1144. group = 0;
  1145. index = 0;
  1146. for( GROUP_LIST::EIterator groupIter = groups.Begin();
  1147. !groupIter.IsDone(); groupIter++ )
  1148. {
  1149. index = 0;
  1150. for( EList< Building, Building& >::EIterator buildIter = (*groupIter).buildings.Begin();
  1151. !buildIter.IsDone(); buildIter++ )
  1152. {
  1153. if ( (*buildIter).fitID == fitID &&
  1154. ( canBeMech ||
  1155. (*buildIter).type != GV_TYPE
  1156. && (*buildIter).type != MECH_TYPE ) )
  1157. {
  1158. return true;
  1159. }
  1160. index++;
  1161. }
  1162. group++;
  1163. }
  1164. return false;
  1165. }
  1166. //*************************************************************************************************
  1167. int EditorObjectMgr::getFitID( int id ) const
  1168. {
  1169. return groups[getGroup(id)].buildings[getIndexInGroup(id)].fitID;
  1170. }
  1171. //*************************************************************************************************
  1172. bool EditorObjectMgr::getBlocksLineOfFire( int id ) const
  1173. {
  1174. return groups[getGroup(id)].buildings[getIndexInGroup(id)].blocksLineOfFire;
  1175. }
  1176. //*************************************************************************************************
  1177. bool EditorObjectMgr::getIsHoverCraft( int id ) const
  1178. {
  1179. return groups[getGroup(id)].buildings[getIndexInGroup(id)].isHoverCraft;
  1180. }
  1181. //*************************************************************************************************
  1182. __int64 EditorObjectMgr::getImpassability( int id )
  1183. {
  1184. return groups[getGroup(id)].buildings[getIndexInGroup(id)].impassability;
  1185. }
  1186. void EditorObjectMgr::select( const Stuff::Vector4D& pos1, const Stuff::Vector4D& pos2 )
  1187. {
  1188. int xMin, xMax;
  1189. int yMin, yMax;
  1190. if ( pos1.x < pos2.x )
  1191. {
  1192. xMin = pos1.x;
  1193. xMax = pos2.x;
  1194. }
  1195. else
  1196. {
  1197. xMin = pos2.x;
  1198. xMax = pos1.x;
  1199. }
  1200. if ( pos1.y < pos2.y )
  1201. {
  1202. yMin = pos1.y;
  1203. yMax = pos2.y;
  1204. }
  1205. else
  1206. {
  1207. yMin = pos2.y;
  1208. yMax = pos1.y;
  1209. }
  1210. Stuff::Vector4D screenPos;
  1211. for ( BUILDING_LIST::EIterator iter = buildings.Begin();
  1212. !iter.IsDone(); iter++ )
  1213. {
  1214. eye->projectZ( (*iter)->appearance()->position, screenPos );
  1215. if ( screenPos.x >= xMin && screenPos.x <= xMax
  1216. && screenPos.y >= yMin && screenPos.y <= yMax )
  1217. {
  1218. (*iter)->appearance()->selected = true;
  1219. selectedObjects.AddUnique(*iter);
  1220. }
  1221. }
  1222. for ( UNIT_LIST::EIterator mIter = units.Begin();
  1223. !mIter.IsDone(); mIter++ )
  1224. {
  1225. eye->projectZ( (*mIter)->appearance()->position, screenPos );
  1226. if ( screenPos.x >= xMin && screenPos.x <= xMax
  1227. && screenPos.y >= yMin && screenPos.y <= yMax )
  1228. {
  1229. (*mIter)->appearance()->selected = true;
  1230. selectedObjects.AddUnique(*mIter);
  1231. }
  1232. }
  1233. for ( DROP_LIST::EIterator dIter = dropZones.Begin();
  1234. !dIter.IsDone(); dIter++ )
  1235. {
  1236. Stuff::Vector3D pos = (*dIter)->getPosition();
  1237. eye->projectZ( pos, screenPos );
  1238. if ( screenPos.x >= xMin && screenPos.x <= xMax
  1239. && screenPos.y >= yMin && screenPos.y <= yMax )
  1240. {
  1241. (*dIter)->appearance()->selected = true;
  1242. selectedObjects.AddUnique(*dIter);
  1243. }
  1244. }
  1245. }
  1246. void EditorObjectMgr::select( EditorObject &object, bool bSelect )
  1247. {
  1248. // we'll just assume it's valid
  1249. object.appearance()->selected = bSelect ? DRAW_BARS | DRAW_BRACKETS | DRAW_TEXT : 0; // hack, 6 = draw bars, text,
  1250. if (true == bSelect)
  1251. {
  1252. selectedObjects.AddUnique(&object);
  1253. }
  1254. else
  1255. {
  1256. selectedObjects.RemoveIfThere(&object);
  1257. }
  1258. }
  1259. void EditorObjectMgr::unselectAll()
  1260. {
  1261. // mh: at somepoint we can change the code so that it iterates over the
  1262. // selectedObjects list instead of the lists of all the buildings and units
  1263. for ( BUILDING_LIST::EIterator iter = buildings.Begin();
  1264. !iter.IsDone(); iter++ )
  1265. {
  1266. (*iter)->appearance()->selected = false;
  1267. }
  1268. for ( UNIT_LIST::EIterator mIter = units.Begin();
  1269. !mIter.IsDone(); mIter++ )
  1270. {
  1271. (*mIter)->appearance()->selected = false;
  1272. }
  1273. for ( DROP_LIST::EIterator dIter = dropZones.Begin();
  1274. !dIter.IsDone(); dIter++ )
  1275. (*dIter)->appearance()->selected = false;
  1276. selectedObjects.Clear();
  1277. }
  1278. bool EditorObjectMgr::hasSelection()
  1279. {
  1280. #if 1
  1281. syncSelectedObjectPointerList();
  1282. if (0 < selectedObjects.Count())
  1283. {
  1284. return true;
  1285. }
  1286. #else
  1287. for ( BUILDING_LIST::EConstIterator iter = buildings.Begin();
  1288. !iter.IsDone(); iter++ )
  1289. {
  1290. if ((*iter)->appearance()->selected )
  1291. return true;
  1292. }
  1293. for ( UNIT_LIST::EConstIterator mIter = units.Begin();
  1294. !mIter.IsDone(); mIter++ )
  1295. {
  1296. if ((*mIter)->appearance()->selected )
  1297. return true;
  1298. }
  1299. #endif
  1300. return false;
  1301. }
  1302. int EditorObjectMgr::getSelectionCount()
  1303. {
  1304. #if 1
  1305. syncSelectedObjectPointerList();
  1306. return selectedObjects.Count();
  1307. #else
  1308. int counter = 0;
  1309. for ( BUILDING_LIST::EConstIterator iter = buildings.Begin();
  1310. !iter.IsDone(); iter++ )
  1311. {
  1312. if ((*iter)->appearance()->selected )
  1313. counter++;
  1314. }
  1315. for ( UNIT_LIST::EConstIterator mIter = units.Begin();
  1316. !mIter.IsDone(); mIter++ )
  1317. {
  1318. if ((*mIter)->appearance()->selected )
  1319. counter++;
  1320. }
  1321. return counter;
  1322. #endif
  1323. }
  1324. EditorObjectMgr::EDITOR_OBJECT_LIST EditorObjectMgr::getSelectedObjectList()
  1325. {
  1326. syncSelectedObjectPointerList();
  1327. return selectedObjects;
  1328. }
  1329. void EditorObjectMgr::syncSelectedObjectPointerList()
  1330. {
  1331. if (!selectedObjectsNeedsToBeSynched)
  1332. {
  1333. return;
  1334. }
  1335. selectedObjects.Clear();
  1336. for ( BUILDING_LIST::EConstIterator iter = buildings.Begin()
  1337. ;!iter.IsDone(); iter++ )
  1338. {
  1339. if ((*iter)->appearance()->selected )
  1340. selectedObjects.Append(*iter);
  1341. }
  1342. for ( UNIT_LIST::EConstIterator mIter = units.Begin();
  1343. !mIter.IsDone(); mIter++ )
  1344. {
  1345. if ((*mIter)->appearance()->selected )
  1346. selectedObjects.Append(*mIter);
  1347. }
  1348. for ( DROP_LIST::EIterator dIter = dropZones.Begin();
  1349. !dIter.IsDone(); dIter++ )
  1350. {
  1351. if ((*dIter)->appearance()->selected )
  1352. selectedObjects.Append(*dIter);
  1353. }
  1354. selectedObjectsNeedsToBeSynched = false;
  1355. }
  1356. void EditorObjectMgr::deleteSelectedObjects()
  1357. {
  1358. for ( BUILDING_LIST::EIterator iter = buildings.End();
  1359. !iter.IsDone(); )
  1360. {
  1361. if ( (*iter)->appearance()->selected )
  1362. {
  1363. BUILDING_LIST::EIterator tmpIter = iter;
  1364. iter --;
  1365. buildings.Delete( tmpIter );
  1366. }
  1367. else
  1368. iter--;
  1369. }
  1370. for ( UNIT_LIST::EIterator uIter = units.End();
  1371. !uIter.IsDone(); )
  1372. {
  1373. if ( (*uIter)->appearance()->selected )
  1374. {
  1375. UNIT_LIST::EIterator tmpIter = uIter;
  1376. uIter --;
  1377. units.Delete( tmpIter );
  1378. }
  1379. else
  1380. uIter--;
  1381. }
  1382. for ( DROP_LIST::EIterator dIter = dropZones.End();
  1383. !dIter.IsDone(); )
  1384. {
  1385. if ( (*dIter)->appearance()->selected )
  1386. {
  1387. DROP_LIST::EIterator tmpIter = dIter;
  1388. dIter --;
  1389. dropZones.Delete( tmpIter );
  1390. }
  1391. else
  1392. dIter--;
  1393. }
  1394. selectedObjects.Clear();
  1395. }
  1396. void EditorObjectMgr::adjustObjectsToNewTerrainHeights()
  1397. {
  1398. for ( BUILDING_LIST::EIterator iter = buildings.End();
  1399. !iter.IsDone(); iter++ )
  1400. {
  1401. float Z = land->getTerrainElevation( (*iter)->appearance()->position );
  1402. (*iter)->appearance()->position.z = Z;
  1403. (*iter)->appearance()->update();
  1404. }
  1405. for ( LINK_LIST::EIterator lIter = links.Begin(); !lIter.IsDone(); lIter++ )
  1406. {
  1407. (*lIter)->FixHeights();
  1408. }
  1409. for ( UNIT_LIST::EIterator uIter = units.Begin(); !uIter.IsDone(); uIter++ )
  1410. {
  1411. float Z = land->getTerrainElevation( (*uIter)->appearance()->position );
  1412. (*uIter)->appearance()->position.z = Z;
  1413. (*uIter)->appearance()->update();
  1414. }
  1415. }
  1416. ObjectAppearance* EditorObjectMgr::getAppearance( EditorObjectMgr::Building* pBuilding )
  1417. {
  1418. ObjectAppearance* appearance = NULL;
  1419. if ( !appearanceTypeList )
  1420. {
  1421. appearanceTypeList = new AppearanceTypeList;
  1422. gosASSERT(appearanceTypeList != NULL);
  1423. appearanceTypeList->init(2048000);
  1424. }
  1425. //MUST ALWAYS CALL GET, EVEN IF WE HAVE AN APPEARANCE TYPE OR REFERENCE COUNT DOES NOT INCREASE!
  1426. pBuilding->appearanceType = appearanceTypeList->getAppearance( pBuilding->type << 24, pBuilding->fileName );
  1427. if ( pBuilding->type == BLDG_TYPE )
  1428. appearance = new BldgAppearance;
  1429. else if ( pBuilding->type == MECH_TYPE )
  1430. appearance = new Mech3DAppearance;
  1431. else if ( pBuilding->type == GV_TYPE )
  1432. appearance = new GVAppearance;
  1433. else
  1434. appearance = new TreeAppearance;
  1435. appearance->init( pBuilding->appearanceType );
  1436. appearance->setObjectNameId( pBuilding->nameID );
  1437. return appearance;
  1438. }
  1439. ObjectAppearance* EditorObjectMgr::getAppearance( unsigned long group, unsigned long indexWithinGroup )
  1440. {
  1441. gosASSERT( group >= 0 && group < groups.Count() );
  1442. Group* pGroup = &groups[group];
  1443. gosASSERT( pGroup->buildings.Count() > indexWithinGroup );
  1444. Building* pBuilding = &pGroup->buildings[indexWithinGroup];
  1445. return getAppearance( pBuilding );
  1446. }
  1447. bool EditorObjectMgr::loadMechs( FitIniFile& file )
  1448. {
  1449. // disable animation loading to decrease loading time
  1450. Mech3DAppearanceType::disableAnimationLoading();
  1451. unsigned long count;
  1452. file.seekBlock( "Parts" );
  1453. file.readIdULong( "NumParts", count );
  1454. int alternativeInstancesCounter = count + 1;
  1455. Stuff::Vector3D position;
  1456. position.x = position.y = position.z = 0;
  1457. char buffer[256];
  1458. for ( int i = 1; i < count + 1; ++i )
  1459. {
  1460. sprintf( buffer, "Part%ld", i );
  1461. file.seekBlock( buffer );
  1462. unsigned long fitID;
  1463. file.readIdULong( "ObjectNumber",fitID );
  1464. unsigned long group, index;
  1465. getBuildingFromID( fitID, group, index, true );
  1466. EditorObject* pObject = (EditorObject*)(addBuilding( position, group, index, 0, 0 ));
  1467. gosASSERT( dynamic_cast<Unit*>(pObject) );
  1468. pObject->load( &file, i );
  1469. alternativeInstancesCounter += 1;
  1470. int tmpI = ((Unit *)(pObject))->tmpAlternativeStartIndex;
  1471. if (0 < tmpI)
  1472. {
  1473. alternativeInstancesCounter = tmpI;
  1474. }
  1475. int j;
  1476. for (j = 0; j < ((Unit *)(pObject))->tmpNumAlternativeInstances; j += 1)
  1477. {
  1478. sprintf( buffer, "Part%ld", alternativeInstancesCounter );
  1479. file.seekBlock( buffer );
  1480. unsigned long fitID;
  1481. file.readIdULong( "ObjectNumber",fitID );
  1482. unsigned long group, index;
  1483. getBuildingFromID( fitID, group, index, true );
  1484. EditorObject* pAltObject = (EditorObject*)(addBuilding( position, group, index, 0, 0 ));
  1485. gosASSERT( dynamic_cast<Unit*>(pAltObject) );
  1486. pAltObject->load( &file, alternativeInstancesCounter );
  1487. ((Unit *)(pObject))->pAlternativeInstances->Append(*((Unit *)(pAltObject)));
  1488. deleteBuilding(pAltObject);
  1489. alternativeInstancesCounter += 1;
  1490. }
  1491. }
  1492. return true;
  1493. }
  1494. bool EditorObjectMgr::saveMechs( FitIniFile& file )
  1495. {
  1496. file.writeBlock( "Warriors" );
  1497. UNIT_LIST unitsByPlayer[8/*max players*/];
  1498. {
  1499. for ( UNIT_LIST::EIterator iter = units.Begin(); !iter.IsDone(); iter++ )
  1500. {
  1501. int alignment = (*iter)->getAlignment();
  1502. if (8/*max players*/ > alignment)
  1503. {
  1504. unitsByPlayer[alignment].Append((*iter));
  1505. }
  1506. else
  1507. {
  1508. assert(false);
  1509. }
  1510. }
  1511. }
  1512. UNIT_LIST reorderedUnits;
  1513. UNIT_LIST reorderedUnitsByPlayer[8/*max players*/];
  1514. for (int playerNum = 0; 8/*max players*/ > playerNum; playerNum += 1)
  1515. {
  1516. /*O(n-squared) sort. Who cares.*/
  1517. int numUnitsOfThisPlayer = unitsByPlayer[playerNum].Count();
  1518. for (int i = 0; i < numUnitsOfThisPlayer; i+= 1)
  1519. {
  1520. int lowestSquadNum = INT_MAX;
  1521. UNIT_LIST::EIterator iter;
  1522. for ( iter = unitsByPlayer[playerNum].Begin(); !iter.IsDone(); iter++ )
  1523. {
  1524. if ((*iter)->getSquad() < lowestSquadNum)
  1525. {
  1526. lowestSquadNum = (*iter)->getSquad();
  1527. }
  1528. }
  1529. for ( iter = unitsByPlayer[playerNum].Begin(); !iter.IsDone(); iter++ )
  1530. {
  1531. if (lowestSquadNum == (*iter)->getSquad())
  1532. {
  1533. break;
  1534. }
  1535. }
  1536. if (!iter.IsDone())
  1537. {
  1538. reorderedUnitsByPlayer[playerNum].Append(*iter);
  1539. /*we delete the unit from unitsByPlayer[playerNum] because more than one unit
  1540. may have the same squad number*/
  1541. unitsByPlayer[playerNum].Delete(iter);
  1542. }
  1543. else
  1544. {
  1545. assert(false);
  1546. }
  1547. }
  1548. UNIT_LIST::EIterator iter;
  1549. for ( iter = reorderedUnitsByPlayer[playerNum].Begin(); !iter.IsDone(); iter++ )
  1550. {
  1551. reorderedUnits.Append(*iter);
  1552. }
  1553. }
  1554. assert(reorderedUnits.Count() == units.Count());
  1555. {
  1556. /*When saving, each player's units need to be grouped into "lances" of 12 units
  1557. for historical reasons.*/
  1558. for (int playerNum = 0; 8/*max players*/ > playerNum; playerNum += 1)
  1559. {
  1560. int lanceGroup = 0;
  1561. int mateIndex = 0;
  1562. UNIT_LIST::EIterator iter;
  1563. for ( iter = reorderedUnitsByPlayer[playerNum].Begin(); !iter.IsDone(); iter++ )
  1564. {
  1565. (*iter)->setLanceInfo(lanceGroup, mateIndex);
  1566. mateIndex += 1;
  1567. if (12/*just twelve*/ >= mateIndex)
  1568. {
  1569. mateIndex = 0;
  1570. lanceGroup += 1;
  1571. }
  1572. }
  1573. }
  1574. }
  1575. /*
  1576. if ( 1 > reorderedUnitsByPlayer[EDITOR_TEAM1].Count() )
  1577. {
  1578. EMessageBox( IDS_MUST_HAVE_PLAYER, IDS_CANT_SAVE, MB_OK );
  1579. return false;
  1580. }
  1581. */
  1582. if (!justResaveAllMaps)
  1583. {
  1584. if (EditorData::instance->IsSinglePlayer())
  1585. {
  1586. bool enemyUnitFound = false;
  1587. for (int playerNum = 1/*first non-user player*/; 8/*max players*/ > playerNum; playerNum += 1)
  1588. {
  1589. if (0/*player1's (user's) team*/ != EditorData::instance->PlayersRef().PlayerRef(playerNum).DefaultTeam())
  1590. {
  1591. if (0 < reorderedUnitsByPlayer[playerNum].Count())
  1592. {
  1593. enemyUnitFound = true;
  1594. break;
  1595. }
  1596. }
  1597. }
  1598. if (!enemyUnitFound)
  1599. {
  1600. EMessageBox( IDS_NO_ENEMY_UNITS, IDS_WARNING, MB_OK );
  1601. }
  1602. if (0 >= reorderedUnitsByPlayer[0/*user player*/].Count())
  1603. {
  1604. EMessageBox( IDS_NO_PLAYER_UNITS, IDS_WARNING, MB_OK );
  1605. }
  1606. }
  1607. else
  1608. {
  1609. for (int playerNum = 0; EditorData::instance->MaxPlayers() > playerNum; playerNum += 1)
  1610. {
  1611. if (1 > reorderedUnitsByPlayer[playerNum].Count())
  1612. {
  1613. EMessageBox( IDS_PLAYERS_WITH_NO_UNITS, IDS_WARNING, MB_OK );
  1614. break;
  1615. }
  1616. }
  1617. }
  1618. }
  1619. file.writeIdULong( "NumWarriors", units.Count() );
  1620. if (!justResaveAllMaps && (units.Count() > 104))
  1621. {
  1622. EMessageBox( IDS_TOO_MANY_PILOTS, IDS_WARNING, MB_OK);
  1623. }
  1624. file.writeBlock( "Parts" );
  1625. file.writeIdULong( "NumParts", units.Count() );
  1626. //file.writeIdBoolean( "AlliedTeam", alignmentCount[NONE] ? true : false );
  1627. char buffer[256];
  1628. int counter = 1; // for some unknown reason we index from 1
  1629. int alternativeInstancesCounter = reorderedUnits.Count() + 1;
  1630. UNIT_LIST::EIterator iter;
  1631. for ( iter = reorderedUnits.Begin(); !iter.IsDone(); counter++, iter++ )
  1632. {
  1633. sprintf( buffer, "Warrior%ld", counter );
  1634. file.writeBlock( buffer );
  1635. // ARM
  1636. // Make a new name of the form "level23_warrior8"
  1637. char armName[512] = {0};
  1638. strcpy(armName, file.getFilename());
  1639. _strlwr(armName);
  1640. char * fitExt = strstr(armName, ".fit");
  1641. *fitExt = '_';
  1642. sprintf(fitExt+1, "warrior%02d", counter);
  1643. mapAsset->AddRelationship("warrior", armName);
  1644. IProviderAssetPtr mechAssetPtr = armProvider->OpenAsset(armName, AssetType_Virtual, ProviderType_Primary);
  1645. mechAsset = (IProviderAsset*)mechAssetPtr;
  1646. (*iter)->save( &file, counter );
  1647. mechAssetPtr->Close();
  1648. mechAssetPtr = NULL;
  1649. mechAsset = NULL;
  1650. file.writeIdULong( "AlternativeStartIndex", alternativeInstancesCounter + 1 );
  1651. long IndicesOfAlternatives[15/*max alternatives*/] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
  1652. int numAlternatives = (*iter)->pAlternativeInstances->Count();
  1653. if (15/*max alternatives*/ < numAlternatives)
  1654. {
  1655. gosASSERT(false);
  1656. numAlternatives = 15/*max alternatives*/;
  1657. }
  1658. int j;
  1659. for (j = 0; j <numAlternatives ; j++)
  1660. {
  1661. IndicesOfAlternatives[j] = (alternativeInstancesCounter + 1) + j;
  1662. }
  1663. file.writeIdLongArray("IndicesOfAlternatives", IndicesOfAlternatives, 15);
  1664. alternativeInstancesCounter += 1;
  1665. int i;
  1666. for (i = 0; i < (*iter)->pAlternativeInstances->Count(); i++)
  1667. {
  1668. sprintf( buffer, "Warrior%ld", alternativeInstancesCounter );
  1669. file.writeBlock( buffer );
  1670. (*((*iter)->pAlternativeInstances))[i].save( &file, alternativeInstancesCounter );
  1671. alternativeInstancesCounter += 1;
  1672. }
  1673. }
  1674. // you should be able to get the force groups frm the units themselves
  1675. file.writeBlock( "Teams" );
  1676. //bool alliedFlag = alignmentCount[2] || alignmentCount[3] || alignmentCount[4] || alignmentCount[5] || alignmentCount[6] || alignmentCount[7];
  1677. //file.writeIdBoolean( "AlliedTeam", alliedFlag ? true : false );
  1678. {
  1679. /*When saving, each player's units need to be grouped into "lances" of 12 units
  1680. for historical reasons.*/
  1681. int reorderedUnitIndex = 1; /*1-based indexing by convention*/
  1682. for (int playerNum = 0; 8/*max players*/ > playerNum; playerNum += 1)
  1683. {
  1684. int reorderedUnitByPlayerIndex = 0;
  1685. int lanceGroup = 0;
  1686. for ( lanceGroup = 0; lanceGroup * (12/*just twelve*/) < reorderedUnitsByPlayer[playerNum].Count(); lanceGroup += 1 )
  1687. {
  1688. long mates[12/*just twelve*/];
  1689. int mateIndex = 0;
  1690. for (mateIndex = 0; 12/*just twelve*/ > mateIndex; mateIndex += 1)
  1691. {
  1692. if (reorderedUnitByPlayerIndex < reorderedUnitsByPlayer[playerNum].Count())
  1693. {
  1694. mates[mateIndex] = reorderedUnitIndex;
  1695. reorderedUnitIndex += 1;
  1696. reorderedUnitByPlayerIndex += 1;
  1697. }
  1698. else
  1699. {
  1700. mates[mateIndex] = 0;
  1701. }
  1702. }
  1703. EString commanderBlock;
  1704. commanderBlock.Format("Commander%ldGroup:%ld", playerNum, lanceGroup);
  1705. file.writeBlock( commanderBlock.Data() );
  1706. file.writeIdLongArray( "Mates", &(mates[0]), 12 );
  1707. }
  1708. }
  1709. }
  1710. return true;
  1711. }
  1712. bool EditorObjectMgr::saveForests( FitIniFile& file )
  1713. {
  1714. long counter = 0;
  1715. char header[256];
  1716. for ( FOREST_LIST::EIterator iter = forests.Begin(); !iter.IsDone(); iter++, counter++)
  1717. {
  1718. sprintf( header, "Forest%ld", counter );
  1719. file.writeBlock( header );
  1720. (*iter)->save( file );
  1721. }
  1722. return true;
  1723. }
  1724. bool EditorObjectMgr::loadForests( FitIniFile& file )
  1725. {
  1726. long counter = 0;
  1727. char header[256];
  1728. long tmp;
  1729. while( true )
  1730. {
  1731. sprintf( header, "Forest%ld", counter );
  1732. if ( NO_ERR != file.seekBlock( header ) )
  1733. break;
  1734. file.readIdString( "FileName", header, 255 );
  1735. file.readIdLong( "ID", tmp );
  1736. Forest* pForest = new Forest( tmp );
  1737. pForest->setFileName( header );
  1738. pForest->init( file );
  1739. pForest->ID = counter;
  1740. forests.Append( pForest );
  1741. counter++;
  1742. }
  1743. return true;
  1744. }
  1745. int EditorObjectMgr::getNextAvailableForestID()
  1746. {
  1747. int max = -1;
  1748. for ( FOREST_LIST::EIterator iter = forests.Begin(); !iter.IsDone(); iter++)
  1749. {
  1750. if ( (*iter)->getID() > max )
  1751. {
  1752. max = (*iter)->getID();
  1753. }
  1754. }
  1755. int ID = max + 1;
  1756. return ID;
  1757. }
  1758. long EditorObjectMgr::createForest( const Forest& forest )
  1759. {
  1760. int ID = getNextAvailableForestID();
  1761. Forest* pForest = new Forest( ID );
  1762. CString oldForestName = pForest->getFileName();
  1763. *pForest = forest;
  1764. pForest->ID = ID;
  1765. if ( !strstr( pForest->getFileName(), ".fit" ) )
  1766. pForest->setFileName( oldForestName );
  1767. forests.Append( pForest );
  1768. // here I need to go about adding the trees -- will do in a sec
  1769. doForest( *pForest );
  1770. return ID;
  1771. }
  1772. void EditorObjectMgr::editForest( long& oldID, const Forest& forest )
  1773. {
  1774. land->unselectAll();
  1775. for ( BUILDING_LIST::EIterator bIter = buildings.End(); !bIter.IsDone(); )
  1776. {
  1777. if ( (*bIter)->getForestID() == oldID )
  1778. {
  1779. long cellI, cellJ;
  1780. (*bIter)->getCells( cellJ, cellI );
  1781. long tileI = cellI/3;
  1782. long tileJ = cellJ/3;
  1783. land->selectVertex( tileJ, tileI, true );
  1784. delete *bIter;
  1785. BUILDING_LIST::EIterator tmpIter = bIter;
  1786. bIter --;
  1787. buildings.Delete( tmpIter );
  1788. }
  1789. else
  1790. bIter--;
  1791. }
  1792. // update the forest object
  1793. for ( FOREST_LIST::EIterator iter = forests.Begin(); !iter.IsDone(); iter++ )
  1794. {
  1795. if ( (*iter)->getID() == oldID )
  1796. {
  1797. *(*iter) = forest;
  1798. break;
  1799. }
  1800. }
  1801. doForest( *(*iter) );
  1802. land->unselectAll();
  1803. }
  1804. void EditorObjectMgr::removeForest( const Forest& forest )
  1805. {
  1806. unselectAll();
  1807. for ( BUILDING_LIST::EIterator bIter = buildings.End(); !bIter.IsDone(); )
  1808. {
  1809. if ( (*bIter)->getForestID() == forest.getID() )
  1810. {
  1811. delete *bIter;
  1812. BUILDING_LIST::EIterator tmpIter = bIter;
  1813. bIter--;
  1814. buildings.Delete( tmpIter );
  1815. }
  1816. else
  1817. {
  1818. bIter--;
  1819. }
  1820. }
  1821. // now remove the forest from the list
  1822. for ( FOREST_LIST::EIterator iter = forests.Begin(); !iter.IsDone(); iter++ )
  1823. {
  1824. if ( (*iter)->getID() == forest.getID() )
  1825. {
  1826. delete (*iter);
  1827. forests.Delete( iter );
  1828. return;
  1829. }
  1830. }
  1831. }
  1832. void EditorObjectMgr::selectForest( long ID )
  1833. {
  1834. unselectAll();
  1835. for ( BUILDING_LIST::EIterator bIter = buildings.Begin(); !bIter.IsDone(); bIter++ )
  1836. {
  1837. if ( (*bIter)->getForestID() == ID )
  1838. {
  1839. select( *(*bIter), true );
  1840. }
  1841. }
  1842. }
  1843. void EditorObjectMgr::doForest( const Forest& forest )
  1844. {
  1845. float centerX = forest.centerX;
  1846. float centerY = forest.centerY;
  1847. float maxDensity = forest.maxDensity/9;
  1848. float minDensity = forest.minDensity/9;
  1849. long probabilities[FOREST_TYPES];
  1850. long probabilityTotal = 0;
  1851. for ( int i= 0; i < FOREST_TYPES; i++ )
  1852. {
  1853. probabilityTotal += forest.percentages[i];
  1854. }
  1855. for ( i = 0; i < FOREST_TYPES; i++ )
  1856. {
  1857. probabilities[i] = forest.percentages[i]/probabilityTotal * 100;
  1858. if ( i > 0 )
  1859. {
  1860. probabilities[i] = probabilities[i-1] + probabilities[i];
  1861. }
  1862. }
  1863. float r2 = forest.radius; // temporary... calc real radius at some point
  1864. r2 *= r2; // square it
  1865. for ( int j = 0; j < land->realVerticesMapSide; j++ )
  1866. {
  1867. for ( int i = 0; i < land->realVerticesMapSide; i++ )
  1868. {
  1869. if ( land->isVertexSelected( j, i ) )
  1870. {
  1871. long cellI = i * 3;
  1872. long cellJ = j * 3;
  1873. for ( int l = -1; l < 2; l++ )
  1874. {
  1875. for ( int m = -1; m < 2; m++ )
  1876. {
  1877. Stuff::Vector3D pos;
  1878. if ( cellJ + l < 0 || cellJ + l > land->realVerticesMapSide * 3 )
  1879. continue;
  1880. if ( cellI + m < 0 || cellI + m > land->realVerticesMapSide * 3 )
  1881. continue;
  1882. EditorObject* pObject = getObjectAtCell( cellJ + l, cellI + m );
  1883. if ( pObject&& pObject->getForestID() == -1 ) // only forests allow multiples in a cell
  1884. {
  1885. continue;
  1886. }
  1887. // figure out how far from center we are
  1888. land->cellToWorld( cellJ + l, cellI + m, pos );
  1889. pos.z = land->getTerrainElevation( pos );
  1890. // distance
  1891. float deltaX = centerX - pos.x;
  1892. float deltaY = centerY - pos.y;
  1893. float tmpR = deltaX * deltaX + deltaY * deltaY;
  1894. tmpR = sqrt( tmpR );
  1895. float fm = (minDensity - maxDensity)/forest.radius;
  1896. float density = maxDensity + fm * ( tmpR );
  1897. long lDensity = (long)density;
  1898. float remainder = density - (float)lDensity;
  1899. long count = (int)density + (RandomNumber( 100 ) < remainder * 100 ? 1 : 0);
  1900. // OK, now we have the density randomly (if possible)
  1901. // populate the tile with these...
  1902. // might want to break down by cell
  1903. for ( int t = 0; t < count; t++ )
  1904. {
  1905. // oh god, now we need to pick a tree
  1906. long random = RandomNumber( 100 );
  1907. int group = -1;
  1908. int index = -1;
  1909. for ( int k = 0; k < FOREST_TYPES; k++ )
  1910. {
  1911. if ( random < probabilities[k] )
  1912. {
  1913. getRandomTreeFromGroup( k+1, group, index );
  1914. break;
  1915. }
  1916. }
  1917. if ( group == -1 || index == -1 )
  1918. {
  1919. continue;
  1920. }
  1921. Stuff::Vector3D treePos = pos;
  1922. // now place equally randomly.
  1923. treePos.x += (float)RandomNumber( Terrain::worldUnitsPerCell ) - Terrain::worldUnitsPerCell/2;
  1924. treePos.y += (float)RandomNumber( Terrain::worldUnitsPerCell ) - Terrain::worldUnitsPerCell/2;
  1925. int rotation = RandomNumber( 32 );
  1926. float scale = RandomNumber( 100 * (forest.maxHeight - forest.minHeight) );
  1927. scale = forest.minHeight + (scale)/100.f;
  1928. if ((AppearanceTypeList::appearanceHeap->totalCoreLeft() < 1000/*arbitrary*/)
  1929. || (AppearanceTypeList::appearanceHeap->totalCoreLeft() < 0.01/*arbitrary*/ * AppearanceTypeList::appearanceHeap->size()))
  1930. {
  1931. AfxMessageBox(IDS_APPEARANCE_HEAP_EXHAUSTED);
  1932. /*exit all loops*/
  1933. t = count;
  1934. m = 2;
  1935. l = 2;
  1936. i = land->realVerticesMapSide;
  1937. j = land->realVerticesMapSide;
  1938. }
  1939. else
  1940. {
  1941. EditorObject* pObject = (EditorObject*)addBuilding( treePos, group,
  1942. index, -1, rotation * 11.25, scale, !forest.bRandom );
  1943. pObject->setForestID( forest.getID() );
  1944. }
  1945. }
  1946. }
  1947. }
  1948. }
  1949. }
  1950. }
  1951. }
  1952. const Forest* EditorObjectMgr::getForest( long ID )
  1953. {
  1954. for ( FOREST_LIST::EIterator iter = forests.Begin(); !iter.IsDone(); iter++ )
  1955. {
  1956. if ( (*iter)->getID() == ID )
  1957. return (*iter);
  1958. }
  1959. gosASSERT( 0 ); // you shouldn't get here
  1960. return NULL;
  1961. }
  1962. long EditorObjectMgr::getForests( Forest** pForests, long& count )
  1963. {
  1964. if ( forests.Count() > count )
  1965. {
  1966. count = forests.Count();
  1967. return -1;
  1968. }
  1969. count = 0;
  1970. for ( FOREST_LIST::EIterator iter = forests.Begin(); !iter.IsDone(); iter++ )
  1971. {
  1972. pForests[count++] = (*iter);
  1973. }
  1974. return count;
  1975. }
  1976. void EditorObjectMgr::getRandomTreeFromGroup( int treeGroup, int& group, int& index )
  1977. {
  1978. group = TREE_GROUP;
  1979. index = 0;
  1980. int treeCount = 0;
  1981. // figure out how many trees are in the group
  1982. Group* pGroup = &groups[TREE_GROUP];
  1983. if ( pGroup )
  1984. {
  1985. for( EList< Building, Building& >::EIterator buildIter = groups[group].buildings.Begin();
  1986. !buildIter.IsDone(); buildIter++ )
  1987. {
  1988. if ( (*buildIter).forestId == treeGroup )
  1989. treeCount++;
  1990. }
  1991. int whichOne = RandomNumber( treeCount );
  1992. treeCount = 0;
  1993. int counter = 0;
  1994. for( buildIter = groups[group].buildings.Begin();
  1995. !buildIter.IsDone(); buildIter++, counter++ )
  1996. {
  1997. if ( (*buildIter).forestId == treeGroup )
  1998. {
  1999. if ( treeCount == whichOne )
  2000. {
  2001. index = counter;
  2002. }
  2003. treeCount++;
  2004. }
  2005. }
  2006. }
  2007. }
  2008. bool EditorObjectMgr::saveDropZones( FitIniFile& file )
  2009. {
  2010. int counter = 0;
  2011. char Header[256];
  2012. for ( DROP_LIST::EIterator iter = dropZones.Begin(); !iter.IsDone(); iter++, counter++ )
  2013. {
  2014. sprintf( Header, "DropZone%ld", counter );
  2015. file.writeBlock( Header );
  2016. (*iter)->save( &file, counter );
  2017. }
  2018. // going to do nav markers here
  2019. // need to sort this list first. ick.
  2020. EditorObject** pObjects = (EditorObject**)_alloca( sizeof( EditorObject* ) * buildings.Count() );
  2021. int i = 0;
  2022. for ( BUILDING_LIST::EIterator bIter = buildings.Begin(); !bIter.IsDone(); bIter++ )
  2023. {
  2024. pObjects[i++] = *bIter;
  2025. }
  2026. long count = i;
  2027. for ( i = 1; i < count; ++i )
  2028. {
  2029. EditorObject* cur = pObjects[i];
  2030. for ( int j = 0; j < i; ++j )
  2031. {
  2032. if ( (getFitID(cur->getID()) < getFitID(pObjects[j]->getID())) && j != i )
  2033. {
  2034. pObjects[i] = pObjects[j];
  2035. pObjects[j] = cur;
  2036. break;
  2037. }
  2038. }
  2039. }
  2040. counter = 0;
  2041. for ( i = 0; i < count; i++ )
  2042. {
  2043. if ( pObjects[i]->save(& file, counter ) )
  2044. counter++;
  2045. }
  2046. // so it writes out afterward....
  2047. file.writeBlock( "NavMarkers" );
  2048. file.writeIdULong( "NumNavMarkers", counter );
  2049. return true;
  2050. }
  2051. bool EditorObjectMgr::loadDropZones( FitIniFile& file )
  2052. {
  2053. int counter = 0;
  2054. char Header[256];
  2055. while( true )
  2056. {
  2057. sprintf( Header, "DropZone%ld", counter );
  2058. if ( NO_ERR != file.seekBlock( Header ) )
  2059. return counter > 0 ? true : false;
  2060. bool bVTol;
  2061. file.readIdBoolean( "IsVTOL", bVTol );
  2062. Stuff::Vector3D pos;
  2063. file.readIdFloat( "PositionX", pos.x );
  2064. file.readIdFloat( "PositionY", pos.y );
  2065. pos.z = land->getTerrainElevation( pos );
  2066. // assuming single player for now
  2067. addDropZone( pos, 0, bVTol );
  2068. counter++;
  2069. }
  2070. // no need to read in nav markers, they are saved in the building file as well
  2071. return true;
  2072. }
  2073. int EditorObjectMgr::getType( unsigned long group, unsigned long indexWithinGroup )
  2074. {
  2075. Group* pGroup = &groups[group];
  2076. gosASSERT( pGroup->buildings.Count() > indexWithinGroup );
  2077. Building* pBuilding = &pGroup->buildings[indexWithinGroup];
  2078. return pBuilding->type;
  2079. }
  2080. EditorObjectMgr::Building::~Building()
  2081. {
  2082. }
  2083. bool EditorObjectMgr::moveBuilding( EditorObject* pInfo, long cellJ, long cellI )
  2084. {
  2085. if (getObjectAtCell( cellJ, cellI ) && (getObjectAtCell( cellJ, cellI ) != pInfo))
  2086. {
  2087. // a building is already there
  2088. return false;
  2089. }
  2090. BuildingLink* pLink = getLinkWithParent( pInfo );
  2091. if ( pLink )
  2092. {
  2093. Stuff::Vector3D worldPos;
  2094. land->cellToWorld( cellJ, cellI, worldPos );
  2095. worldPos.z = land->getTerrainElevation( worldPos );
  2096. pLink->SetParentPosition( worldPos );
  2097. }
  2098. pLink = getLinkWithChild( pInfo );
  2099. if ( pLink )
  2100. {
  2101. pLink->RemoveObject( pInfo );
  2102. }
  2103. Stuff::Vector3D position;
  2104. land->cellToWorld( cellJ, cellI, position );
  2105. position.z = land->getTerrainElevation( position );
  2106. pInfo->appearance()->position = position;
  2107. pInfo->cellRow = cellJ;
  2108. pInfo->cellColumn = cellI;
  2109. if ( pLink )
  2110. pLink->AddChild( pInfo );
  2111. return true;
  2112. }
  2113. EditorObject* EditorObjectMgr::getObjectAtCell( long realCellJ, long realCellI )
  2114. {
  2115. for( BUILDING_LIST::EIterator iter = buildings.Begin();
  2116. !iter.IsDone(); iter++ )
  2117. {
  2118. if ( labs( realCellI - ((*iter)->cellColumn - 4)) <= 8
  2119. && labs( realCellJ - ((*iter)->cellRow - 4)) <= 8 )
  2120. {
  2121. __int64 FootPrint = getImpassability( (*iter)->id );
  2122. int i = realCellI - ((*iter)->cellColumn - 4);
  2123. int j = realCellJ - ((*iter)->cellRow - 4);
  2124. if ( (FootPrint >> ( j * 8 + i )) & 0x00000001 || (i == 4 && j == 4))
  2125. return (*iter);
  2126. }
  2127. }
  2128. for( UNIT_LIST::EIterator uIter = units.Begin();
  2129. !uIter.IsDone(); uIter++ )
  2130. {
  2131. if ( labs( realCellI - ((*uIter)->cellColumn - 4)) <= 8
  2132. && labs( realCellJ - ((*uIter)->cellRow - 4)) <= 8 )
  2133. {
  2134. __int64 FootPrint = getImpassability( (*uIter)->id );
  2135. int i = realCellI - ((*uIter)->cellColumn - 4);
  2136. int j = realCellJ - ((*uIter)->cellRow - 4);
  2137. if ( (FootPrint >> ( j * 8 + i )) & 0x00000001 || (i == 4 && j == 4))
  2138. return (*uIter);
  2139. }
  2140. }
  2141. return NULL;
  2142. }
  2143. EditorObject* EditorObjectMgr::getBuilding( const EditorObject &building )
  2144. {
  2145. for( BUILDING_LIST::EIterator iter = buildings.Begin(); !iter.IsDone(); iter++ )
  2146. {
  2147. if( building.cellColumn == (*iter)->cellColumn
  2148. && building.cellRow == (*iter)->cellRow )
  2149. {
  2150. return (*iter);
  2151. }
  2152. }
  2153. return NULL;
  2154. }
  2155. BuildingLink* EditorObjectMgr::getLinkWithBuilding( const EditorObject* pObj )
  2156. {
  2157. for ( LINK_LIST::EIterator iter = links.Begin(); !iter.IsDone(); iter++ )
  2158. {
  2159. if ( (*iter)->HasParent( pObj ) || (*iter)->HasChild( pObj ) )
  2160. return *iter;
  2161. }
  2162. return NULL;
  2163. }
  2164. BuildingLink* EditorObjectMgr::getLinkWithParent( const EditorObject* pObj )
  2165. {
  2166. for ( LINK_LIST::EIterator iter = links.Begin(); !iter.IsDone(); iter++ )
  2167. {
  2168. if ( (*iter)->HasParent( pObj ) )
  2169. return *iter;
  2170. }
  2171. return NULL;
  2172. }
  2173. BuildingLink* EditorObjectMgr::getLinkWithChild( const EditorObject* pObj )
  2174. {
  2175. for ( LINK_LIST::EIterator iter = links.Begin(); !iter.IsDone(); iter++ )
  2176. {
  2177. if ( (*iter)->HasChild( pObj ) )
  2178. return *iter;
  2179. }
  2180. return NULL;
  2181. }
  2182. void EditorObjectMgr::addLink( BuildingLink* pLink )
  2183. {
  2184. for ( LINK_LIST::EIterator iter = links.Begin(); !iter.IsDone(); iter++ )
  2185. {
  2186. if ( (*iter) == pLink )
  2187. return;
  2188. }
  2189. links.Append( pLink );
  2190. }
  2191. void EditorObjectMgr::deleteLink( BuildingLink* pLink )
  2192. {
  2193. for ( LINK_LIST::EIterator iter = links.Begin(); !iter.IsDone(); iter++ )
  2194. {
  2195. if ( (*iter) == pLink )
  2196. {
  2197. delete (*iter);
  2198. links.Delete( iter );
  2199. return;
  2200. }
  2201. }
  2202. }
  2203. const char* EditorObjectMgr::getObjectName( int ID ) const
  2204. {
  2205. return groups[getGroup(ID)].buildings[getIndexInGroup(ID)].name;
  2206. }
  2207. const char* EditorObjectMgr::getGroupName( int ID ) const
  2208. {
  2209. return groups[ID].name;
  2210. }
  2211. void EditorObjectMgr::getUnitGroupNames( const char** names, int* ids, int& numberOfEm ) const
  2212. {
  2213. int counter = 0;
  2214. int index = 0;
  2215. for ( GROUP_LIST::EConstIterator iter = groups.Begin(); !iter.IsDone(); index++, iter++ )
  2216. {
  2217. if ( (*iter).buildings.Count() && ((*iter).buildings.GetHead().type == MECH_TYPE || (*iter).buildings.GetHead().type == GV_TYPE ) )
  2218. {
  2219. if ( counter < numberOfEm )
  2220. {
  2221. names[counter] = (*iter).name;
  2222. ids[counter] = index;
  2223. }
  2224. counter++;
  2225. }
  2226. }
  2227. numberOfEm = counter;
  2228. }
  2229. int EditorObjectMgr::getUnitGroupCount( ) const
  2230. {
  2231. int counter = 0;
  2232. for ( GROUP_LIST::EConstIterator iter = groups.Begin(); !iter.IsDone(); iter++ )
  2233. {
  2234. if ( (*iter).buildings.Count() && ( (*iter).buildings.GetHead().type == MECH_TYPE ||(*iter).buildings.GetHead().type == GV_TYPE ) )
  2235. {
  2236. counter++;
  2237. }
  2238. }
  2239. return counter;
  2240. }
  2241. void EditorObjectMgr::getSelectedUnits( UNIT_LIST& selUnits )
  2242. {
  2243. for ( UNIT_LIST::EIterator iter = units.Begin(); !iter.IsDone(); iter++ )
  2244. {
  2245. if ( (*iter)->isSelected() )
  2246. selUnits.Append( (*iter) );
  2247. }
  2248. }
  2249. int EditorObjectMgr::getNumberOfVariants( int group, int indexInGroup ) const
  2250. {
  2251. Building bldg = groups[group].buildings[indexInGroup];
  2252. if ( !bldg.varNames )
  2253. return 0;
  2254. for ( int i = 0; i < 16; ++i )
  2255. {
  2256. if ( !bldg.varNames[i] )
  2257. break;
  2258. }
  2259. return i + 1;
  2260. }
  2261. void EditorObjectMgr::getVariantNames( int group, int indexInGroup, const char** names, int& numberOfNames ) const
  2262. {
  2263. Building bldg = groups[group].buildings[indexInGroup];
  2264. for ( int i = 0; i < 16 && i < numberOfNames; ++i )
  2265. {
  2266. if ( !bldg.varNames[i] )
  2267. break;
  2268. names[i] = bldg.varNames[i];
  2269. }
  2270. numberOfNames = i;
  2271. }
  2272. void EditorObjectMgr::registerSquadNum(unsigned long squadNum)
  2273. {
  2274. if (squadNum >= nextAvailableSquadNum)
  2275. {
  2276. nextAvailableSquadNum = squadNum + 1;
  2277. }
  2278. }
  2279. //*************************************************************************************************
  2280. // end of file ( EditorObjectMgr.cpp )