Enemy Soldier Save.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173
  1. #ifdef PRECOMPILEDHEADERS
  2. #include "Tactical All.h"
  3. #include "Enemy Soldier Save.h"
  4. #else
  5. #include <stdio.h>
  6. #include <memory.h>
  7. #include "types.h"
  8. #include "strategicmap.h"
  9. #include "overhead.h"
  10. #include "isometric utils.h"
  11. #include "soldier add.h"
  12. #include "soldier create.h"
  13. #include "Soldier Init List.h"
  14. #include "debug.h"
  15. #include "Random.h"
  16. #include "items.h"
  17. #include "cursors.h"
  18. #include "Map Information.h"
  19. #include "soldier profile.h"
  20. #include "Sys Globals.h"
  21. #include "EditorMercs.h"
  22. #include "Animation Data.h"
  23. #include "message.h"
  24. #include "Font Control.h"
  25. #endif
  26. extern void AddPlacementToWorld( SOLDIERINITNODE *pNode );
  27. BOOLEAN CountNumberOfElitesRegularsAdminsAndCreaturesFromEnemySoldiersTempFile( UINT8 *pubNumElites, UINT8 *pubNumRegulars, UINT8 *pubNumAdmins, UINT8 *pubNumCreatures );
  28. BOOLEAN gfRestoringEnemySoldiersFromTempFile = FALSE;
  29. BOOLEAN gfRestoringCiviliansFromTempFile = FALSE;
  30. void RemoveCivilianTempFile( INT16 sSectorX, INT16 sSectorY, INT8 bSectorZ );
  31. void RemoveEnemySoldierTempFile( INT16 sSectorX, INT16 sSectorY, INT8 bSectorZ )
  32. {
  33. CHAR8 zMapName[ 128 ];
  34. if( GetSectorFlagStatus( sSectorX, sSectorY, bSectorZ, SF_ENEMY_PRESERVED_TEMP_FILE_EXISTS ) )
  35. {
  36. //Delete any temp file that is here and toast the flag that say's one exists.
  37. ReSetSectorFlag( sSectorX, sSectorY, bSectorZ, SF_ENEMY_PRESERVED_TEMP_FILE_EXISTS );
  38. // GetMapFileName( gWorldSectorX, gWorldSectorY, gbWorldSectorZ, zTempName, FALSE );
  39. //add the 'e' for 'Enemy preserved' to the front of the map name
  40. // sprintf( zMapName, "%s\\e_%s", MAPS_DIR, zTempName);
  41. GetMapTempFileName( SF_ENEMY_PRESERVED_TEMP_FILE_EXISTS, zMapName, sSectorX, sSectorY, bSectorZ );
  42. //Delete the temp file.
  43. FileDelete( zMapName );
  44. }
  45. }
  46. void RemoveCivilianTempFile( INT16 sSectorX, INT16 sSectorY, INT8 bSectorZ )
  47. {
  48. //CHAR8 zTempName[ 128 ];
  49. CHAR8 zMapName[ 128 ];
  50. if( GetSectorFlagStatus( sSectorX, sSectorY, bSectorZ, SF_CIV_PRESERVED_TEMP_FILE_EXISTS ) )
  51. {
  52. //Delete any temp file that is here and toast the flag that say's one exists.
  53. ReSetSectorFlag( sSectorX, sSectorY, bSectorZ, SF_CIV_PRESERVED_TEMP_FILE_EXISTS );
  54. //GetMapFileName( gWorldSectorX, gWorldSectorY, gbWorldSectorZ, zTempName, FALSE );
  55. GetMapTempFileName( SF_CIV_PRESERVED_TEMP_FILE_EXISTS, zMapName, sSectorX, sSectorY, bSectorZ );
  56. //Delete the temp file.
  57. FileDelete( zMapName );
  58. }
  59. }
  60. //OLD SAVE METHOD: This is the old way of loading the enemies and civilians
  61. BOOLEAN LoadEnemySoldiersFromTempFile()
  62. {
  63. SOLDIERINITNODE *curr;
  64. SOLDIERCREATE_STRUCT tempDetailedPlacement;
  65. INT32 i;
  66. INT32 slots = 0;
  67. UINT32 uiNumBytesRead;
  68. UINT32 uiTimeStamp;
  69. HWFILE hfile;
  70. INT16 sSectorX, sSectorY;
  71. UINT16 usCheckSum, usFileCheckSum;
  72. CHAR8 zMapName[ 128 ];
  73. #ifdef JA2TESTVERSION
  74. CHAR8 zReason[256];
  75. #endif
  76. INT8 bSectorZ;
  77. UINT8 ubSectorID;
  78. UINT8 ubNumElites = 0, ubNumTroops = 0, ubNumAdmins = 0, ubNumCreatures = 0;
  79. UINT8 ubStrategicElites, ubStrategicTroops, ubStrategicAdmins, ubStrategicCreatures;
  80. gfRestoringEnemySoldiersFromTempFile = TRUE;
  81. //STEP ONE: Set up the temp file to read from.
  82. //Convert the current sector location into a file name
  83. // GetMapFileName( gWorldSectorX, gWorldSectorY, gbWorldSectorZ, zTempName, FALSE );
  84. //add the 'e' for 'Enemy preserved' to the front of the map name
  85. // sprintf( zMapName, "%s\\e_%s", MAPS_DIR, zTempName);
  86. GetMapTempFileName( SF_ENEMY_PRESERVED_TEMP_FILE_EXISTS, zMapName, gWorldSectorX, gWorldSectorY, gbWorldSectorZ );
  87. //Open the file for reading
  88. hfile = FileOpen( zMapName, FILE_ACCESS_READ | FILE_OPEN_EXISTING, FALSE );
  89. if( hfile == 0 )
  90. { //Error opening map modification file
  91. return FALSE;
  92. }
  93. //STEP TWO: determine whether or not we should use this data.
  94. //because it is the demo, it is automatically used.
  95. FileRead( hfile, &sSectorY, 2, &uiNumBytesRead );
  96. if( uiNumBytesRead != 2 )
  97. {
  98. #ifdef JA2TESTVERSION
  99. sprintf( zReason, "EnemySoldier -- EOF while reading sSectorY. KM" );
  100. #endif
  101. goto FAIL_LOAD;
  102. }
  103. if( gWorldSectorY != sSectorY )
  104. {
  105. #ifdef JA2TESTVERSION
  106. sprintf( zReason, "EnemySoldier -- sSectorY mismatch. KM" );
  107. #endif
  108. goto FAIL_LOAD;
  109. }
  110. LoadSoldierInitListLinks( hfile );
  111. //STEP THREE: read the data
  112. FileRead( hfile, &sSectorX, 2, &uiNumBytesRead );
  113. if( uiNumBytesRead != 2 )
  114. {
  115. #ifdef JA2TESTVERSION
  116. sprintf( zReason, "EnemySoldier -- EOF while reading sSectorX. KM" );
  117. #endif
  118. goto FAIL_LOAD;
  119. }
  120. if( gWorldSectorX != sSectorX )
  121. {
  122. #ifdef JA2TESTVERSION
  123. sprintf( zReason, "EnemySoldier -- sSectorX mismatch. KM" );
  124. #endif
  125. goto FAIL_LOAD;
  126. }
  127. FileRead( hfile, &slots, 4, &uiNumBytesRead );
  128. if( uiNumBytesRead != 4 )
  129. {
  130. #ifdef JA2TESTVERSION
  131. sprintf( zReason, "EnemySoldier -- EOF while reading slots. KM" );
  132. #endif
  133. goto FAIL_LOAD;
  134. }
  135. FileRead( hfile, &uiTimeStamp, 4, &uiNumBytesRead );
  136. if( uiNumBytesRead != 4 )
  137. {
  138. #ifdef JA2TESTVERSION
  139. sprintf( zReason, "EnemySoldier -- EOF while reading uiTimeStamp. KM" );
  140. #endif
  141. goto FAIL_LOAD;
  142. }
  143. FileRead( hfile, &bSectorZ, 1, &uiNumBytesRead );
  144. if( uiNumBytesRead != 1 )
  145. {
  146. #ifdef JA2TESTVERSION
  147. sprintf( zReason, "EnemySoldier -- EOF while reading bSectorZ. KM" );
  148. #endif
  149. goto FAIL_LOAD;
  150. }
  151. if( GetWorldTotalMin() > uiTimeStamp + 300 )
  152. { //the file has aged. Use the regular method for adding soldiers.
  153. FileClose( hfile );
  154. RemoveEnemySoldierTempFile( sSectorX, sSectorY, bSectorZ );
  155. gfRestoringEnemySoldiersFromTempFile = FALSE;
  156. return TRUE;
  157. }
  158. if( gbWorldSectorZ != bSectorZ )
  159. {
  160. #ifdef JA2TESTVERSION
  161. sprintf( zReason, "EnemySoldier -- bSectorZ mismatch. KM" );
  162. #endif
  163. goto FAIL_LOAD;
  164. }
  165. if( !slots )
  166. { //no need to restore the enemy's to the map. This means we are restoring a saved game.
  167. gfRestoringEnemySoldiersFromTempFile = FALSE;
  168. FileClose( hfile );
  169. return TRUE;
  170. }
  171. if( slots < 0 || slots >= 64 )
  172. { //bad IO!
  173. #ifdef JA2TESTVERSION
  174. sprintf( zReason, "EnemySoldier -- illegal slot value of %d. KM", slots );
  175. #endif
  176. goto FAIL_LOAD;
  177. }
  178. //For all the enemy slots (enemy/creature), clear the fPriorityExistance flag. We will use these flags
  179. //to determine which slots have been modified as we load the data into the map pristine soldier init list.
  180. curr = gSoldierInitHead;
  181. while( curr )
  182. {
  183. if( curr->pBasicPlacement->fPriorityExistance )
  184. {
  185. if( curr->pBasicPlacement->bTeam == ENEMY_TEAM || curr->pBasicPlacement->bTeam == CREATURE_TEAM || curr->pBasicPlacement->bTeam == CIV_TEAM )
  186. {
  187. curr->pBasicPlacement->fPriorityExistance = FALSE;
  188. }
  189. }
  190. curr = curr->next;
  191. }
  192. //get the number of enemies in this sector.
  193. if( bSectorZ )
  194. {
  195. UNDERGROUND_SECTORINFO *pSector;
  196. pSector = FindUnderGroundSector( sSectorX, sSectorY, bSectorZ );
  197. if( !pSector )
  198. {
  199. #ifdef JA2TESTVERSION
  200. sprintf( zReason, "EnemySoldier -- Couldn't find underground sector info for (%d,%d,%d) KM", sSectorX, sSectorY, bSectorZ );
  201. #endif
  202. goto FAIL_LOAD;
  203. }
  204. ubStrategicElites = pSector->ubNumElites;
  205. ubStrategicTroops = pSector->ubNumTroops;
  206. ubStrategicAdmins = pSector->ubNumAdmins;
  207. ubStrategicCreatures = pSector->ubNumCreatures;
  208. }
  209. else
  210. {
  211. SECTORINFO *pSector;
  212. pSector = &SectorInfo[ SECTOR( sSectorX, sSectorY ) ];
  213. ubStrategicCreatures = pSector->ubNumCreatures;
  214. GetNumberOfEnemiesInSector( sSectorX, sSectorY, &ubStrategicAdmins, &ubStrategicTroops, &ubStrategicElites );
  215. }
  216. for( i = 0; i < slots; i++ )
  217. {
  218. FileRead( hfile, &tempDetailedPlacement, sizeof( SOLDIERCREATE_STRUCT ), &uiNumBytesRead );
  219. if( uiNumBytesRead != sizeof( SOLDIERCREATE_STRUCT ) )
  220. {
  221. #ifdef JA2TESTVERSION
  222. sprintf( zReason, "EnemySoldier -- EOF while reading tempDetailedPlacment %d. KM", i );
  223. #endif
  224. goto FAIL_LOAD;
  225. }
  226. curr = gSoldierInitHead;
  227. while( curr )
  228. {
  229. if( !curr->pBasicPlacement->fPriorityExistance )
  230. {
  231. if( !curr->pDetailedPlacement || curr->pDetailedPlacement && curr->pDetailedPlacement->ubProfile == NO_PROFILE )
  232. {
  233. if( curr->pBasicPlacement->bTeam == tempDetailedPlacement.bTeam )
  234. {
  235. curr->pBasicPlacement->fPriorityExistance = TRUE;
  236. if( !curr->pDetailedPlacement )
  237. { //need to upgrade the placement to detailed placement
  238. curr->pDetailedPlacement = (SOLDIERCREATE_STRUCT*)MemAlloc( sizeof( SOLDIERCREATE_STRUCT ) );
  239. }
  240. //now replace the map pristine placement info with the temp map file version..
  241. memcpy( curr->pDetailedPlacement, &tempDetailedPlacement, sizeof( SOLDIERCREATE_STRUCT ) );
  242. curr->pBasicPlacement->fPriorityExistance = TRUE;
  243. curr->pBasicPlacement->bDirection = curr->pDetailedPlacement->bDirection;
  244. curr->pBasicPlacement->bOrders = curr->pDetailedPlacement->bOrders;
  245. curr->pBasicPlacement->bAttitude = curr->pDetailedPlacement->bAttitude;
  246. curr->pBasicPlacement->bBodyType = curr->pDetailedPlacement->bBodyType;
  247. curr->pBasicPlacement->fOnRoof = curr->pDetailedPlacement->fOnRoof;
  248. curr->pBasicPlacement->ubSoldierClass = curr->pDetailedPlacement->ubSoldierClass;
  249. curr->pBasicPlacement->ubCivilianGroup = curr->pDetailedPlacement->ubCivilianGroup;
  250. curr->pBasicPlacement->fHasKeys = curr->pDetailedPlacement->fHasKeys;
  251. curr->pBasicPlacement->usStartingGridNo = curr->pDetailedPlacement->sInsertionGridNo;
  252. curr->pBasicPlacement->bPatrolCnt = curr->pDetailedPlacement->bPatrolCnt;
  253. memcpy( curr->pBasicPlacement->sPatrolGrid, curr->pDetailedPlacement->sPatrolGrid,
  254. sizeof( INT16 ) * curr->pBasicPlacement->bPatrolCnt );
  255. FileRead( hfile, &usCheckSum, 2, &uiNumBytesRead );
  256. if( uiNumBytesRead != 2 )
  257. {
  258. #ifdef JA2TESTVERSION
  259. sprintf( zReason, "EnemySoldier -- EOF while reading usCheckSum %d. KM", i );
  260. #endif
  261. goto FAIL_LOAD;
  262. }
  263. //verify the checksum equation (anti-hack) -- see save
  264. usFileCheckSum =
  265. curr->pDetailedPlacement->bLife * 7 +
  266. curr->pDetailedPlacement->bLifeMax * 8 -
  267. curr->pDetailedPlacement->bAgility * 2 +
  268. curr->pDetailedPlacement->bDexterity * 1 +
  269. curr->pDetailedPlacement->bExpLevel * 5 -
  270. curr->pDetailedPlacement->bMarksmanship * 9 +
  271. curr->pDetailedPlacement->bMedical * 10 +
  272. curr->pDetailedPlacement->bMechanical * 3 +
  273. curr->pDetailedPlacement->bExplosive * 4 +
  274. curr->pDetailedPlacement->bLeadership * 5 +
  275. curr->pDetailedPlacement->bStrength * 7 +
  276. curr->pDetailedPlacement->bWisdom * 11 +
  277. curr->pDetailedPlacement->bMorale * 7 +
  278. curr->pDetailedPlacement->bAIMorale * 3 -
  279. curr->pDetailedPlacement->bBodyType * 7 +
  280. 4 * 6 +
  281. curr->pDetailedPlacement->sSectorX * 7 -
  282. curr->pDetailedPlacement->ubSoldierClass * 4 +
  283. curr->pDetailedPlacement->bTeam * 7 +
  284. curr->pDetailedPlacement->bDirection * 5 +
  285. curr->pDetailedPlacement->fOnRoof * 17 +
  286. curr->pDetailedPlacement->sInsertionGridNo * 1 +
  287. 3;
  288. if( usCheckSum != usFileCheckSum )
  289. { //Hacker has modified the stats on the enemy placements.
  290. #ifdef JA2TESTVERSION
  291. sprintf( zReason, "EnemySoldier -- checksum for placement %d failed. KM", i );
  292. #endif
  293. goto FAIL_LOAD;
  294. }
  295. if( curr->pBasicPlacement->bTeam == CIV_TEAM )
  296. {
  297. AddPlacementToWorld( curr );
  298. break;
  299. }
  300. else
  301. {
  302. //Add preserved placements as long as they don't exceed the actual population.
  303. switch( curr->pBasicPlacement->ubSoldierClass )
  304. {
  305. case SOLDIER_CLASS_ELITE:
  306. ubNumElites++;
  307. if( ubNumElites < ubStrategicElites )
  308. {
  309. AddPlacementToWorld( curr );
  310. }
  311. break;
  312. case SOLDIER_CLASS_ARMY:
  313. ubNumTroops++;
  314. if( ubNumTroops < ubStrategicTroops )
  315. {
  316. AddPlacementToWorld( curr );
  317. }
  318. break;
  319. case SOLDIER_CLASS_ADMINISTRATOR:
  320. ubNumAdmins++;
  321. if( ubNumAdmins < ubStrategicAdmins )
  322. {
  323. AddPlacementToWorld( curr );
  324. }
  325. break;
  326. case SOLDIER_CLASS_CREATURE:
  327. ubNumCreatures++;
  328. if( ubNumCreatures < ubStrategicCreatures )
  329. {
  330. AddPlacementToWorld( curr );
  331. }
  332. break;
  333. }
  334. break;
  335. }
  336. }
  337. }
  338. }
  339. curr = curr->next;
  340. }
  341. }
  342. FileRead( hfile, &ubSectorID, 1, &uiNumBytesRead );
  343. if( uiNumBytesRead != 1 )
  344. {
  345. #ifdef JA2TESTVERSION
  346. sprintf( zReason, "EnemySoldier -- EOF while reading ubSectorID. KM" );
  347. #endif
  348. goto FAIL_LOAD;
  349. }
  350. if( ubSectorID != SECTOR( sSectorX, sSectorY ) )
  351. {
  352. #ifdef JA2TESTVERSION
  353. sprintf( zReason, "EnemySoldier -- ubSectorID mismatch. KM" );
  354. #endif
  355. goto FAIL_LOAD;
  356. }
  357. //now add any extra enemies that have arrived since the temp file was made.
  358. if( ubStrategicTroops > ubNumTroops || ubStrategicElites > ubNumElites || ubStrategicAdmins > ubNumAdmins )
  359. {
  360. ubStrategicTroops = ( ubStrategicTroops > ubNumTroops ) ? ubStrategicTroops - ubNumTroops : 0;
  361. ubStrategicElites = ( ubStrategicElites > ubNumElites ) ? ubStrategicElites - ubNumElites : 0;
  362. ubStrategicAdmins = ( ubStrategicAdmins > ubNumAdmins ) ? ubStrategicAdmins - ubNumAdmins : 0;
  363. AddSoldierInitListEnemyDefenceSoldiers( ubStrategicAdmins, ubStrategicTroops, ubStrategicElites );
  364. }
  365. if( ubStrategicCreatures > ubNumCreatures )
  366. {
  367. ubStrategicCreatures; //not sure if this wil ever happen. If so, needs to be handled.
  368. }
  369. //successful
  370. FileClose( hfile );
  371. return TRUE;
  372. FAIL_LOAD:
  373. //The temp file load failed either because of IO problems related to hacking/logic, or
  374. //various checks failed for hacker validation. If we reach this point, the "error: exit game"
  375. //dialog would appear in a non-testversion.
  376. FileClose( hfile );
  377. #ifdef JA2TESTVERSION
  378. AssertMsg( 0, zReason );
  379. #endif
  380. return FALSE;
  381. }
  382. //OLD SAVE METHOD: This is the older way of saving the civilian and the enemies placement into a temp file
  383. BOOLEAN SaveEnemySoldiersToTempFile( INT16 sSectorX, INT16 sSectorY, INT8 bSectorZ, UINT8 ubFirstIdTeam, UINT8 ubLastIdTeam, BOOLEAN fAppendToFile )
  384. {
  385. SOLDIERINITNODE *curr;
  386. SOLDIERTYPE *pSoldier;
  387. INT32 i;
  388. INT32 slots = 0;
  389. INT32 iSlotsAlreadyInUse=0;
  390. UINT32 uiNumBytesWritten;
  391. UINT32 uiTimeStamp;
  392. HWFILE hfile;
  393. SCHEDULENODE *pSchedule;
  394. UINT16 usCheckSum;
  395. CHAR8 zMapName[ 128 ];
  396. UINT8 ubSectorID;
  397. //STEP ONE: Prep the soldiers for saving...
  398. //modify the map's soldier init list to reflect the changes to the member's still alive...
  399. for( i = gTacticalStatus.Team[ ubFirstIdTeam ].bFirstID; i <= gTacticalStatus.Team[ ubLastIdTeam ].bLastID; i++ )
  400. {
  401. pSoldier = MercPtrs[ i ];
  402. if( pSoldier->bActive /*&& pSoldier->bInSector*/ && pSoldier->bLife )
  403. { //soldier is valid, so find the matching soldier init list entry for modification.
  404. curr = gSoldierInitHead;
  405. while( curr && curr->pSoldier != pSoldier )
  406. {
  407. curr = curr->next;
  408. }
  409. if( curr && curr->pSoldier == pSoldier && pSoldier->ubProfile == NO_PROFILE )
  410. { //found a match.
  411. if( !(gTacticalStatus.uiFlags & LOADING_SAVED_GAME) )
  412. {
  413. if( !curr->pDetailedPlacement )
  414. { //need to upgrade the placement to detailed placement
  415. curr->pBasicPlacement->fDetailedPlacement = TRUE;
  416. curr->pDetailedPlacement = (SOLDIERCREATE_STRUCT*)MemAlloc( sizeof( SOLDIERCREATE_STRUCT ) );
  417. memset( curr->pDetailedPlacement, 0, sizeof( SOLDIERCREATE_STRUCT ) );
  418. }
  419. //Copy over the data of the soldier.
  420. curr->pDetailedPlacement->ubProfile = NO_PROFILE;
  421. curr->pDetailedPlacement->bLife = pSoldier->bLife;
  422. curr->pDetailedPlacement->bLifeMax = pSoldier->bLifeMax;
  423. curr->pDetailedPlacement->bAgility = pSoldier->bAgility;
  424. curr->pDetailedPlacement->bDexterity = pSoldier->bDexterity;
  425. curr->pDetailedPlacement->bExpLevel = pSoldier->bExpLevel;
  426. curr->pDetailedPlacement->bMarksmanship = pSoldier->bMarksmanship;
  427. curr->pDetailedPlacement->bMedical = pSoldier->bMedical;
  428. curr->pDetailedPlacement->bMechanical = pSoldier->bMechanical;
  429. curr->pDetailedPlacement->bExplosive = pSoldier->bExplosive;
  430. curr->pDetailedPlacement->bLeadership = pSoldier->bLeadership;
  431. curr->pDetailedPlacement->bStrength = pSoldier->bStrength;
  432. curr->pDetailedPlacement->bWisdom = pSoldier->bWisdom;
  433. curr->pDetailedPlacement->bAttitude = pSoldier->bAttitude;
  434. curr->pDetailedPlacement->bOrders = pSoldier->bOrders;
  435. curr->pDetailedPlacement->bMorale = pSoldier->bMorale;
  436. curr->pDetailedPlacement->bAIMorale = pSoldier->bAIMorale;
  437. curr->pDetailedPlacement->bBodyType = pSoldier->ubBodyType;
  438. curr->pDetailedPlacement->ubCivilianGroup = pSoldier->ubCivilianGroup;
  439. //If the soldier has a real schedule (not a default schedule), then store it.
  440. //All other cases should be 0.
  441. curr->pDetailedPlacement->ubScheduleID = 0;
  442. if( pSoldier->ubScheduleID )
  443. {
  444. pSchedule = GetSchedule( pSoldier->ubScheduleID );
  445. if( pSchedule && !(pSchedule->usFlags & SCHEDULE_FLAGS_TEMPORARY) )
  446. {
  447. curr->pDetailedPlacement->ubScheduleID = pSoldier->ubScheduleID;
  448. }
  449. }
  450. curr->pDetailedPlacement->fHasKeys = pSoldier->bHasKeys;
  451. curr->pDetailedPlacement->sSectorX = pSoldier->sSectorX;
  452. curr->pDetailedPlacement->sSectorY = pSoldier->sSectorY;
  453. curr->pDetailedPlacement->bSectorZ = pSoldier->bSectorZ;
  454. curr->pDetailedPlacement->ubSoldierClass = pSoldier->ubSoldierClass;
  455. curr->pDetailedPlacement->bTeam = pSoldier->bTeam;
  456. curr->pDetailedPlacement->bDirection = pSoldier->bDirection;
  457. //we don't want the player to think that all the enemies start in the exact position when we
  458. //left the map, so randomize the start locations either current position or original position.
  459. if( PreRandom( 2 ) )
  460. { //use current position
  461. curr->pDetailedPlacement->fOnRoof = pSoldier->bLevel;
  462. curr->pDetailedPlacement->sInsertionGridNo = pSoldier->sGridNo;
  463. }
  464. else
  465. { //use original position
  466. curr->pDetailedPlacement->fOnRoof = curr->pBasicPlacement->fOnRoof;
  467. curr->pDetailedPlacement->sInsertionGridNo = curr->pBasicPlacement->usStartingGridNo;
  468. }
  469. swprintf( curr->pDetailedPlacement->name, pSoldier->name );
  470. //Copy patrol points
  471. curr->pDetailedPlacement->bPatrolCnt = pSoldier->bPatrolCnt;
  472. memcpy( curr->pDetailedPlacement->sPatrolGrid, pSoldier->usPatrolGrid, sizeof( INT16 ) * MAXPATROLGRIDS );
  473. //copy colors for soldier based on the body type.
  474. sprintf( curr->pDetailedPlacement->HeadPal, pSoldier->HeadPal );
  475. sprintf( curr->pDetailedPlacement->VestPal, pSoldier->VestPal );
  476. sprintf( curr->pDetailedPlacement->SkinPal, pSoldier->SkinPal );
  477. sprintf( curr->pDetailedPlacement->PantsPal, pSoldier->PantsPal );
  478. sprintf( curr->pDetailedPlacement->MiscPal, pSoldier->MiscPal );
  479. //copy soldier's inventory
  480. memcpy( curr->pDetailedPlacement->Inv, pSoldier->inv, sizeof( OBJECTTYPE ) * NUM_INV_SLOTS );
  481. }
  482. //DONE, now increment the counter, so we know how many there are.
  483. slots++;
  484. }
  485. }
  486. }
  487. if( !slots )
  488. { //No need to save anything, so return successfully
  489. RemoveEnemySoldierTempFile( sSectorX, sSectorY, bSectorZ );
  490. return TRUE;
  491. }
  492. //STEP TWO: Set up the temp file to write to.
  493. //Convert the current sector location into a file name
  494. // GetMapFileName( sSectorX, sSectorY, bSectorZ, zTempName, FALSE );
  495. //add the 'e' for 'Enemy preserved' to the front of the map name
  496. // sprintf( zMapName, "%s\\e_%s", MAPS_DIR, zTempName);
  497. GetMapTempFileName( SF_ENEMY_PRESERVED_TEMP_FILE_EXISTS, zMapName, sSectorX, sSectorY, bSectorZ );
  498. //if the file doesnt exist
  499. if( FileSize( zMapName ) == 0 )
  500. {
  501. //set it so we are not appending
  502. fAppendToFile = FALSE;
  503. }
  504. //if we are to append to the file
  505. if( fAppendToFile )
  506. {
  507. //Open the file for writing, Create it if it doesnt exist
  508. hfile = FileOpen( zMapName, FILE_ACCESS_READ | FILE_OPEN_ALWAYS, FALSE );
  509. if( hfile == 0 )
  510. { //Error opening map modification file
  511. return FALSE;
  512. }
  513. //advance for bytes and read the #of slots already used
  514. FileSeek( hfile, 4, FILE_SEEK_FROM_START );
  515. FileRead( hfile, &iSlotsAlreadyInUse, 4, &uiNumBytesWritten );
  516. if( uiNumBytesWritten != 4 )
  517. {
  518. goto FAIL_SAVE;
  519. }
  520. FileClose( hfile );
  521. //Open the file for writing, Create it if it doesnt exist
  522. hfile = FileOpen( zMapName, FILE_ACCESS_WRITE | FILE_OPEN_ALWAYS, FALSE );
  523. if( hfile == 0 )
  524. { //Error opening map modification file
  525. return FALSE;
  526. }
  527. slots += iSlotsAlreadyInUse;
  528. //advance for bytes and read the #of slots already used
  529. FileSeek( hfile, 4, FILE_SEEK_FROM_START );
  530. FileWrite( hfile, &slots, 4, &uiNumBytesWritten );
  531. if( uiNumBytesWritten != 4 )
  532. {
  533. goto FAIL_SAVE;
  534. }
  535. FileSeek( hfile, 0, FILE_SEEK_FROM_END );
  536. }
  537. else
  538. {
  539. //Open the file for writing, Create it if it doesnt exist
  540. hfile = FileOpen( zMapName, FILE_ACCESS_WRITE | FILE_OPEN_ALWAYS, FALSE );
  541. if( hfile == 0 )
  542. { //Error opening map modification file
  543. return FALSE;
  544. }
  545. }
  546. //if we are to append to the file
  547. if( !fAppendToFile )
  548. {
  549. FileWrite( hfile, &sSectorY, 2, &uiNumBytesWritten );
  550. if( uiNumBytesWritten != 2 )
  551. {
  552. goto FAIL_SAVE;
  553. }
  554. //STEP THREE: Save the data
  555. SaveSoldierInitListLinks( hfile );
  556. FileWrite( hfile, &sSectorX, 2, &uiNumBytesWritten );
  557. if( uiNumBytesWritten != 2 )
  558. {
  559. goto FAIL_SAVE;
  560. }
  561. //This check may appear confusing. It is intended to abort if the player is saving the game. It is only
  562. //supposed to preserve the links to the placement list, so when we finally do leave the level with enemies remaining,
  563. //we will need the links that are only added when the map is loaded, and are normally lost when restoring a save.
  564. if( gTacticalStatus.uiFlags & LOADING_SAVED_GAME )
  565. {
  566. slots = 0;
  567. }
  568. FileWrite( hfile, &slots, 4, &uiNumBytesWritten );
  569. if( uiNumBytesWritten != 4 )
  570. {
  571. goto FAIL_SAVE;
  572. }
  573. uiTimeStamp = GetWorldTotalMin();
  574. FileWrite( hfile, &uiTimeStamp, 4, &uiNumBytesWritten );
  575. if( uiNumBytesWritten != 4 )
  576. {
  577. goto FAIL_SAVE;
  578. }
  579. FileWrite( hfile, &bSectorZ, 1, &uiNumBytesWritten );
  580. if( uiNumBytesWritten != 1 )
  581. {
  582. goto FAIL_SAVE;
  583. }
  584. }
  585. if( gTacticalStatus.uiFlags & LOADING_SAVED_GAME )
  586. {
  587. //if we are saving the game, we don't need to preserve the soldier information, just
  588. //preserve the links to the placement list.
  589. slots = 0;
  590. FileClose( hfile );
  591. SetSectorFlag( sSectorX, sSectorY, bSectorZ, SF_ENEMY_PRESERVED_TEMP_FILE_EXISTS );
  592. return TRUE;
  593. }
  594. for( i = gTacticalStatus.Team[ ubFirstIdTeam ].bFirstID; i <= gTacticalStatus.Team[ ubLastIdTeam ].bLastID; i++ )
  595. {
  596. pSoldier = MercPtrs[ i ];
  597. if( pSoldier->bActive /*&& pSoldier->bInSector*/ && pSoldier->bLife )
  598. { //soldier is valid, so find the matching soldier init list entry for modification.
  599. curr = gSoldierInitHead;
  600. while( curr && curr->pSoldier != pSoldier )
  601. {
  602. curr = curr->next;
  603. }
  604. if( curr && curr->pSoldier == pSoldier && pSoldier->ubProfile == NO_PROFILE )
  605. { //found a match.
  606. FileWrite( hfile, curr->pDetailedPlacement, sizeof( SOLDIERCREATE_STRUCT ), &uiNumBytesWritten );
  607. if( uiNumBytesWritten != sizeof( SOLDIERCREATE_STRUCT ) )
  608. {
  609. goto FAIL_SAVE;
  610. }
  611. //insert a checksum equation (anti-hack)
  612. usCheckSum =
  613. curr->pDetailedPlacement->bLife * 7 +
  614. curr->pDetailedPlacement->bLifeMax * 8 -
  615. curr->pDetailedPlacement->bAgility * 2 +
  616. curr->pDetailedPlacement->bDexterity * 1 +
  617. curr->pDetailedPlacement->bExpLevel * 5 -
  618. curr->pDetailedPlacement->bMarksmanship * 9 +
  619. curr->pDetailedPlacement->bMedical * 10 +
  620. curr->pDetailedPlacement->bMechanical * 3 +
  621. curr->pDetailedPlacement->bExplosive * 4 +
  622. curr->pDetailedPlacement->bLeadership * 5 +
  623. curr->pDetailedPlacement->bStrength * 7 +
  624. curr->pDetailedPlacement->bWisdom * 11 +
  625. curr->pDetailedPlacement->bMorale * 7 +
  626. curr->pDetailedPlacement->bAIMorale * 3 -
  627. curr->pDetailedPlacement->bBodyType * 7 +
  628. 4 * 6 +
  629. curr->pDetailedPlacement->sSectorX * 7 -
  630. curr->pDetailedPlacement->ubSoldierClass * 4 +
  631. curr->pDetailedPlacement->bTeam * 7 +
  632. curr->pDetailedPlacement->bDirection * 5 +
  633. curr->pDetailedPlacement->fOnRoof * 17 +
  634. curr->pDetailedPlacement->sInsertionGridNo * 1 +
  635. 3;
  636. FileWrite( hfile, &usCheckSum, 2, &uiNumBytesWritten );
  637. if( uiNumBytesWritten != 2 )
  638. {
  639. goto FAIL_SAVE;
  640. }
  641. }
  642. }
  643. }
  644. //if we are to append to the file
  645. if( !fAppendToFile )
  646. {
  647. ubSectorID = SECTOR( sSectorX, sSectorY );
  648. FileWrite( hfile, &ubSectorID, 1, &uiNumBytesWritten );
  649. if( uiNumBytesWritten != 1 )
  650. {
  651. goto FAIL_SAVE;
  652. }
  653. }
  654. FileClose( hfile );
  655. SetSectorFlag( sSectorX, sSectorY, bSectorZ, SF_ENEMY_PRESERVED_TEMP_FILE_EXISTS );
  656. return TRUE;
  657. FAIL_SAVE:
  658. FileClose( hfile );
  659. return FALSE;
  660. }
  661. BOOLEAN NewWayOfLoadingEnemySoldiersFromTempFile()
  662. {
  663. SOLDIERINITNODE *curr;
  664. SOLDIERCREATE_STRUCT tempDetailedPlacement;
  665. INT32 i;
  666. INT32 slots = 0;
  667. UINT32 uiNumBytesRead;
  668. UINT32 uiTimeStamp;
  669. HWFILE hfile;
  670. INT16 sSectorX, sSectorY;
  671. UINT16 usCheckSum, usFileCheckSum;
  672. CHAR8 zMapName[ 128 ];
  673. #ifdef JA2TESTVERSION
  674. CHAR8 zReason[256];
  675. #endif
  676. INT8 bSectorZ;
  677. UINT8 ubSectorID;
  678. UINT8 ubNumElites = 0, ubNumTroops = 0, ubNumAdmins = 0, ubNumCreatures = 0;
  679. UINT8 ubStrategicElites, ubStrategicTroops, ubStrategicAdmins, ubStrategicCreatures;
  680. gfRestoringEnemySoldiersFromTempFile = TRUE;
  681. //STEP ONE: Set up the temp file to read from.
  682. //Convert the current sector location into a file name
  683. // GetMapFileName( gWorldSectorX, gWorldSectorY, gbWorldSectorZ, zTempName, FALSE );
  684. //add the 'e' for 'Enemy preserved' to the front of the map name
  685. // sprintf( zMapName, "%s\\e_%s", MAPS_DIR, zTempName);
  686. GetMapTempFileName( SF_ENEMY_PRESERVED_TEMP_FILE_EXISTS, zMapName, gWorldSectorX, gWorldSectorY, gbWorldSectorZ );
  687. //Count the number of enemies ( elites, regulars, admins and creatures ) that are in the temp file.
  688. if( gbWorldSectorZ )
  689. {
  690. UNDERGROUND_SECTORINFO *pSector;
  691. pSector = FindUnderGroundSector( gWorldSectorX, gWorldSectorY, gbWorldSectorZ );
  692. if( !pSector )
  693. {
  694. #ifdef JA2TESTVERSION
  695. sprintf( zReason, "EnemySoldier -- Couldn't find underground sector info for (%d,%d,%d) KM", gWorldSectorX, gWorldSectorY, gbWorldSectorZ );
  696. #endif
  697. goto FAIL_LOAD;
  698. }
  699. }
  700. else
  701. {
  702. SECTORINFO *pSector;
  703. pSector = &SectorInfo[ SECTOR( gWorldSectorX, gWorldSectorY ) ];
  704. ubNumElites = pSector->ubNumElites;
  705. ubNumTroops = pSector->ubNumTroops;
  706. ubNumAdmins = pSector->ubNumAdmins;
  707. ubNumCreatures = pSector->ubNumCreatures;
  708. }
  709. if( !( gTacticalStatus.uiFlags & LOADING_SAVED_GAME ) )
  710. {
  711. // Get the number of enemies form the temp file
  712. CountNumberOfElitesRegularsAdminsAndCreaturesFromEnemySoldiersTempFile( &ubStrategicElites, &ubStrategicTroops, &ubStrategicAdmins, &ubStrategicCreatures );
  713. //If any of the counts differ from what is in memory
  714. if( ubStrategicElites != ubNumElites || ubStrategicTroops != ubNumTroops || ubStrategicAdmins != ubNumAdmins || ubStrategicCreatures != ubNumCreatures )
  715. {
  716. //remove the file
  717. RemoveEnemySoldierTempFile( gWorldSectorX, gWorldSectorY, gbWorldSectorZ );
  718. return( TRUE );
  719. }
  720. }
  721. //reset
  722. ubNumElites = ubNumTroops = ubNumAdmins = ubNumCreatures = 0;
  723. //Open the file for reading
  724. hfile = FileOpen( zMapName, FILE_ACCESS_READ | FILE_OPEN_EXISTING, FALSE );
  725. if( hfile == 0 )
  726. { //Error opening map modification file
  727. return FALSE;
  728. }
  729. //STEP TWO: determine whether or not we should use this data.
  730. //because it is the demo, it is automatically used.
  731. FileRead( hfile, &sSectorY, 2, &uiNumBytesRead );
  732. if( uiNumBytesRead != 2 )
  733. {
  734. #ifdef JA2TESTVERSION
  735. sprintf( zReason, "EnemySoldier -- EOF while reading sSectorY. KM" );
  736. #endif
  737. goto FAIL_LOAD;
  738. }
  739. if( gWorldSectorY != sSectorY )
  740. {
  741. #ifdef JA2TESTVERSION
  742. sprintf( zReason, "EnemySoldier -- sSectorY mismatch. KM" );
  743. #endif
  744. goto FAIL_LOAD;
  745. }
  746. // LoadSoldierInitListLinks( hfile );
  747. NewWayOfLoadingEnemySoldierInitListLinks( hfile );
  748. //STEP THREE: read the data
  749. FileRead( hfile, &sSectorX, 2, &uiNumBytesRead );
  750. if( uiNumBytesRead != 2 )
  751. {
  752. #ifdef JA2TESTVERSION
  753. sprintf( zReason, "EnemySoldier -- EOF while reading sSectorX. KM" );
  754. #endif
  755. goto FAIL_LOAD;
  756. }
  757. if( gWorldSectorX != sSectorX )
  758. {
  759. #ifdef JA2TESTVERSION
  760. sprintf( zReason, "EnemySoldier -- sSectorX mismatch. KM" );
  761. #endif
  762. goto FAIL_LOAD;
  763. }
  764. FileRead( hfile, &slots, 4, &uiNumBytesRead );
  765. if( uiNumBytesRead != 4 )
  766. {
  767. #ifdef JA2TESTVERSION
  768. sprintf( zReason, "EnemySoldier -- EOF while reading slots. KM" );
  769. #endif
  770. goto FAIL_LOAD;
  771. }
  772. FileRead( hfile, &uiTimeStamp, 4, &uiNumBytesRead );
  773. if( uiNumBytesRead != 4 )
  774. {
  775. #ifdef JA2TESTVERSION
  776. sprintf( zReason, "EnemySoldier -- EOF while reading uiTimeStamp. KM" );
  777. #endif
  778. goto FAIL_LOAD;
  779. }
  780. FileRead( hfile, &bSectorZ, 1, &uiNumBytesRead );
  781. if( uiNumBytesRead != 1 )
  782. {
  783. #ifdef JA2TESTVERSION
  784. sprintf( zReason, "EnemySoldier -- EOF while reading bSectorZ. KM" );
  785. #endif
  786. goto FAIL_LOAD;
  787. }
  788. if( gbWorldSectorZ != bSectorZ )
  789. {
  790. #ifdef JA2TESTVERSION
  791. sprintf( zReason, "EnemySoldier -- bSectorZ mismatch. KM" );
  792. #endif
  793. goto FAIL_LOAD;
  794. }
  795. if( GetWorldTotalMin() > uiTimeStamp + 300 )
  796. { //the file has aged. Use the regular method for adding soldiers.
  797. FileClose( hfile );
  798. RemoveEnemySoldierTempFile( sSectorX, sSectorY, bSectorZ );
  799. gfRestoringEnemySoldiersFromTempFile = FALSE;
  800. return TRUE;
  801. }
  802. if( !slots )
  803. { //no need to restore the enemy's to the map. This means we are restoring a saved game.
  804. gfRestoringEnemySoldiersFromTempFile = FALSE;
  805. FileClose( hfile );
  806. return TRUE;
  807. }
  808. if( slots < 0 || slots >= 64 )
  809. { //bad IO!
  810. #ifdef JA2TESTVERSION
  811. sprintf( zReason, "EnemySoldier -- illegal slot value of %d. KM", slots );
  812. #endif
  813. goto FAIL_LOAD;
  814. }
  815. //For all the enemy slots (enemy/creature), clear the fPriorityExistance flag. We will use these flags
  816. //to determine which slots have been modified as we load the data into the map pristine soldier init list.
  817. curr = gSoldierInitHead;
  818. while( curr )
  819. {
  820. if( curr->pBasicPlacement->fPriorityExistance )
  821. {
  822. if( curr->pBasicPlacement->bTeam == ENEMY_TEAM || curr->pBasicPlacement->bTeam == CREATURE_TEAM )
  823. {
  824. curr->pBasicPlacement->fPriorityExistance = FALSE;
  825. }
  826. }
  827. curr = curr->next;
  828. }
  829. //get the number of enemies in this sector.
  830. if( bSectorZ )
  831. {
  832. UNDERGROUND_SECTORINFO *pSector;
  833. pSector = FindUnderGroundSector( sSectorX, sSectorY, bSectorZ );
  834. if( !pSector )
  835. {
  836. #ifdef JA2TESTVERSION
  837. sprintf( zReason, "EnemySoldier -- Couldn't find underground sector info for (%d,%d,%d) KM", sSectorX, sSectorY, bSectorZ );
  838. #endif
  839. goto FAIL_LOAD;
  840. }
  841. ubStrategicElites = pSector->ubNumElites;
  842. ubStrategicTroops = pSector->ubNumTroops;
  843. ubStrategicAdmins = pSector->ubNumAdmins;
  844. ubStrategicCreatures = pSector->ubNumCreatures;
  845. }
  846. else
  847. {
  848. SECTORINFO *pSector;
  849. pSector = &SectorInfo[ SECTOR( sSectorX, sSectorY ) ];
  850. ubStrategicCreatures = pSector->ubNumCreatures;
  851. GetNumberOfEnemiesInSector( sSectorX, sSectorY, &ubStrategicAdmins, &ubStrategicTroops, &ubStrategicElites );
  852. }
  853. for( i = 0; i < slots; i++ )
  854. {
  855. FileRead( hfile, &tempDetailedPlacement, sizeof( SOLDIERCREATE_STRUCT ), &uiNumBytesRead );
  856. if( uiNumBytesRead != sizeof( SOLDIERCREATE_STRUCT ) )
  857. {
  858. #ifdef JA2TESTVERSION
  859. sprintf( zReason, "EnemySoldier -- EOF while reading tempDetailedPlacment %d. KM", i );
  860. #endif
  861. goto FAIL_LOAD;
  862. }
  863. curr = gSoldierInitHead;
  864. while( curr )
  865. {
  866. if( !curr->pBasicPlacement->fPriorityExistance )
  867. {
  868. if( curr->pBasicPlacement->bTeam == tempDetailedPlacement.bTeam )
  869. {
  870. curr->pBasicPlacement->fPriorityExistance = TRUE;
  871. if( !curr->pDetailedPlacement )
  872. { //need to upgrade the placement to detailed placement
  873. curr->pDetailedPlacement = (SOLDIERCREATE_STRUCT*)MemAlloc( sizeof( SOLDIERCREATE_STRUCT ) );
  874. }
  875. //now replace the map pristine placement info with the temp map file version..
  876. memcpy( curr->pDetailedPlacement, &tempDetailedPlacement, sizeof( SOLDIERCREATE_STRUCT ) );
  877. curr->pBasicPlacement->fPriorityExistance = TRUE;
  878. curr->pBasicPlacement->bDirection = curr->pDetailedPlacement->bDirection;
  879. curr->pBasicPlacement->bOrders = curr->pDetailedPlacement->bOrders;
  880. curr->pBasicPlacement->bAttitude = curr->pDetailedPlacement->bAttitude;
  881. curr->pBasicPlacement->bBodyType = curr->pDetailedPlacement->bBodyType;
  882. curr->pBasicPlacement->fOnRoof = curr->pDetailedPlacement->fOnRoof;
  883. curr->pBasicPlacement->ubSoldierClass = curr->pDetailedPlacement->ubSoldierClass;
  884. curr->pBasicPlacement->ubCivilianGroup = curr->pDetailedPlacement->ubCivilianGroup;
  885. curr->pBasicPlacement->fHasKeys = curr->pDetailedPlacement->fHasKeys;
  886. curr->pBasicPlacement->usStartingGridNo = curr->pDetailedPlacement->sInsertionGridNo;
  887. curr->pBasicPlacement->bPatrolCnt = curr->pDetailedPlacement->bPatrolCnt;
  888. memcpy( curr->pBasicPlacement->sPatrolGrid, curr->pDetailedPlacement->sPatrolGrid,
  889. sizeof( INT16 ) * curr->pBasicPlacement->bPatrolCnt );
  890. FileRead( hfile, &usCheckSum, 2, &uiNumBytesRead );
  891. if( uiNumBytesRead != 2 )
  892. {
  893. #ifdef JA2TESTVERSION
  894. sprintf( zReason, "EnemySoldier -- EOF while reading usCheckSum %d. KM", i );
  895. #endif
  896. goto FAIL_LOAD;
  897. }
  898. //verify the checksum equation (anti-hack) -- see save
  899. usFileCheckSum =
  900. curr->pDetailedPlacement->bLife * 7 +
  901. curr->pDetailedPlacement->bLifeMax * 8 -
  902. curr->pDetailedPlacement->bAgility * 2 +
  903. curr->pDetailedPlacement->bDexterity * 1 +
  904. curr->pDetailedPlacement->bExpLevel * 5 -
  905. curr->pDetailedPlacement->bMarksmanship * 9 +
  906. curr->pDetailedPlacement->bMedical * 10 +
  907. curr->pDetailedPlacement->bMechanical * 3 +
  908. curr->pDetailedPlacement->bExplosive * 4 +
  909. curr->pDetailedPlacement->bLeadership * 5 +
  910. curr->pDetailedPlacement->bStrength * 7 +
  911. curr->pDetailedPlacement->bWisdom * 11 +
  912. curr->pDetailedPlacement->bMorale * 7 +
  913. curr->pDetailedPlacement->bAIMorale * 3 -
  914. curr->pDetailedPlacement->bBodyType * 7 +
  915. 4 * 6 +
  916. curr->pDetailedPlacement->sSectorX * 7 -
  917. curr->pDetailedPlacement->ubSoldierClass * 4 +
  918. curr->pDetailedPlacement->bTeam * 7 +
  919. curr->pDetailedPlacement->bDirection * 5 +
  920. curr->pDetailedPlacement->fOnRoof * 17 +
  921. curr->pDetailedPlacement->sInsertionGridNo * 1 +
  922. 3;
  923. if( usCheckSum != usFileCheckSum )
  924. { //Hacker has modified the stats on the enemy placements.
  925. #ifdef JA2TESTVERSION
  926. sprintf( zReason, "EnemySoldier -- checksum for placement %d failed. KM", i );
  927. #endif
  928. goto FAIL_LOAD;
  929. }
  930. //Add preserved placements as long as they don't exceed the actual population.
  931. switch( curr->pBasicPlacement->ubSoldierClass )
  932. {
  933. case SOLDIER_CLASS_ELITE:
  934. ubNumElites++;
  935. if( ubNumElites <= ubStrategicElites )
  936. {
  937. //def: AddPlacementToWorld( curr );
  938. }
  939. break;
  940. case SOLDIER_CLASS_ARMY:
  941. ubNumTroops++;
  942. if( ubNumTroops <= ubStrategicTroops )
  943. {
  944. //def: AddPlacementToWorld( curr );
  945. }
  946. break;
  947. case SOLDIER_CLASS_ADMINISTRATOR:
  948. ubNumAdmins++;
  949. if( ubNumAdmins <= ubStrategicAdmins )
  950. {
  951. //def: AddPlacementToWorld( curr );
  952. }
  953. break;
  954. case SOLDIER_CLASS_CREATURE:
  955. ubNumCreatures++;
  956. if( ubNumCreatures <= ubStrategicCreatures )
  957. {
  958. //def: AddPlacementToWorld( curr );
  959. }
  960. break;
  961. }
  962. break;
  963. }
  964. }
  965. curr = curr->next;
  966. }
  967. }
  968. FileRead( hfile, &ubSectorID, 1, &uiNumBytesRead );
  969. if( uiNumBytesRead != 1 )
  970. {
  971. #ifdef JA2TESTVERSION
  972. sprintf( zReason, "EnemySoldier -- EOF while reading ubSectorID. KM" );
  973. #endif
  974. goto FAIL_LOAD;
  975. }
  976. if( ubSectorID != SECTOR( sSectorX, sSectorY ) )
  977. {
  978. #ifdef JA2TESTVERSION
  979. sprintf( zReason, "EnemySoldier -- ubSectorID mismatch. KM" );
  980. #endif
  981. goto FAIL_LOAD;
  982. }
  983. //now add any extra enemies that have arrived since the temp file was made.
  984. if( ubStrategicTroops > ubNumTroops || ubStrategicElites > ubNumElites || ubStrategicAdmins > ubNumAdmins )
  985. {
  986. ubStrategicTroops = ( ubStrategicTroops > ubNumTroops ) ? ubStrategicTroops - ubNumTroops : 0;
  987. ubStrategicElites = ( ubStrategicElites > ubNumElites ) ? ubStrategicElites - ubNumElites : 0;
  988. ubStrategicAdmins = ( ubStrategicAdmins > ubNumAdmins ) ? ubStrategicAdmins - ubNumAdmins : 0;
  989. AddSoldierInitListEnemyDefenceSoldiers( ubStrategicAdmins, ubStrategicTroops, ubStrategicElites );
  990. }
  991. if( ubStrategicCreatures > ubNumCreatures )
  992. {
  993. ubStrategicCreatures; //not sure if this wil ever happen. If so, needs to be handled.
  994. }
  995. //set the number of enemies in the sector
  996. if( bSectorZ )
  997. {
  998. UNDERGROUND_SECTORINFO *pSector;
  999. pSector = FindUnderGroundSector( gWorldSectorX, gWorldSectorY, gbWorldSectorZ );
  1000. if( !pSector )
  1001. {
  1002. #ifdef JA2TESTVERSION
  1003. sprintf( zReason, "EnemySoldier -- Couldn't find underground sector info for (%d,%d,%d) KM", sSectorX, sSectorY, bSectorZ );
  1004. #endif
  1005. goto FAIL_LOAD;
  1006. /*
  1007. pSector->ubElitesInBattle = ubStrategicElites;
  1008. pSector->ubTroopsInBattle = ubStrategicTroops;
  1009. pSector->ubAdminsInBattle = ubStrategicAdmins;
  1010. pSector->ubCreaturesInBattle = ubStrategicCreatures;
  1011. */
  1012. }
  1013. }
  1014. else
  1015. {
  1016. SECTORINFO *pSector;
  1017. pSector = &SectorInfo[ SECTOR( gWorldSectorX, gWorldSectorY ) ];
  1018. /*
  1019. pSector->ubElitesInBattle = ubStrategicElites;
  1020. pSector->ubTroopsInBattle = ubStrategicTroops;
  1021. pSector->ubAdminsInBattle = ubStrategicAdmins;
  1022. pSector->ubCreaturesInBattle = ubStrategicCreatures;
  1023. */
  1024. }
  1025. //if in battle, what about the ubNumInBAttle
  1026. //successful
  1027. FileClose( hfile );
  1028. return TRUE;
  1029. FAIL_LOAD:
  1030. //The temp file load failed either because of IO problems related to hacking/logic, or
  1031. //various checks failed for hacker validation. If we reach this point, the "error: exit game"
  1032. //dialog would appear in a non-testversion.
  1033. FileClose( hfile );
  1034. #ifdef JA2TESTVERSION
  1035. AssertMsg( 0, zReason );
  1036. #endif
  1037. return FALSE;
  1038. }
  1039. BOOLEAN NewWayOfLoadingCiviliansFromTempFile()
  1040. {
  1041. SOLDIERINITNODE *curr, *temp;
  1042. SOLDIERCREATE_STRUCT tempDetailedPlacement;
  1043. INT32 i;
  1044. INT32 slots = 0;
  1045. UINT32 uiNumBytesRead;
  1046. UINT32 uiTimeStamp;
  1047. UINT32 uiTimeSinceLastLoaded;
  1048. HWFILE hfile;
  1049. INT16 sSectorX, sSectorY;
  1050. UINT16 usCheckSum, usFileCheckSum;
  1051. // CHAR8 zTempName[ 128 ];
  1052. CHAR8 zMapName[ 128 ];
  1053. #ifdef JA2TESTVERSION
  1054. CHAR8 zReason[256];
  1055. #endif
  1056. INT8 bSectorZ;
  1057. UINT8 ubSectorID;
  1058. UINT8 ubNumElites = 0, ubNumTroops = 0, ubNumAdmins = 0, ubNumCreatures = 0;
  1059. BOOLEAN fDeleted;
  1060. // UINT8 ubStrategicElites, ubStrategicTroops, ubStrategicAdmins, ubStrategicCreatures;
  1061. gfRestoringCiviliansFromTempFile = TRUE;
  1062. //STEP ONE: Set up the temp file to read from.
  1063. //Convert the current sector location into a file name
  1064. //GetMapFileName( gWorldSectorX, gWorldSectorY, gbWorldSectorZ, zTempName, FALSE );
  1065. //add the 'e' for 'Enemy preserved' to the front of the map name
  1066. //sprintf( zMapName, "%s\\c_%s", MAPS_DIR, zTempName);
  1067. GetMapTempFileName( SF_CIV_PRESERVED_TEMP_FILE_EXISTS, zMapName, gWorldSectorX, gWorldSectorY, gbWorldSectorZ );
  1068. //Open the file for reading
  1069. hfile = FileOpen( zMapName, FILE_ACCESS_READ | FILE_OPEN_EXISTING, FALSE );
  1070. if( hfile == 0 )
  1071. { //Error opening map modification file
  1072. return FALSE;
  1073. }
  1074. //STEP TWO: determine whether or not we should use this data.
  1075. //because it is the demo, it is automatically used.
  1076. FileRead( hfile, &sSectorY, 2, &uiNumBytesRead );
  1077. if( uiNumBytesRead != 2 )
  1078. {
  1079. #ifdef JA2TESTVERSION
  1080. sprintf( zReason, "Civilian -- EOF while reading sSectorY. KM" );
  1081. #endif
  1082. goto FAIL_LOAD;
  1083. }
  1084. if( gWorldSectorY != sSectorY )
  1085. {
  1086. #ifdef JA2TESTVERSION
  1087. sprintf( zReason, "Civilian -- sSectorY mismatch. KM" );
  1088. #endif
  1089. goto FAIL_LOAD;
  1090. }
  1091. //LoadSoldierInitListLinks( hfile );
  1092. NewWayOfLoadingCivilianInitListLinks( hfile );
  1093. //STEP THREE: read the data
  1094. FileRead( hfile, &sSectorX, 2, &uiNumBytesRead );
  1095. if( uiNumBytesRead != 2 )
  1096. {
  1097. #ifdef JA2TESTVERSION
  1098. sprintf( zReason, "Civilian -- EOF while reading sSectorX. KM" );
  1099. #endif
  1100. goto FAIL_LOAD;
  1101. }
  1102. if( gWorldSectorX != sSectorX )
  1103. {
  1104. #ifdef JA2TESTVERSION
  1105. sprintf( zReason, "Civilian -- sSectorX mismatch. KM" );
  1106. #endif
  1107. goto FAIL_LOAD;
  1108. }
  1109. FileRead( hfile, &slots, 4, &uiNumBytesRead );
  1110. if( uiNumBytesRead != 4 )
  1111. {
  1112. #ifdef JA2TESTVERSION
  1113. sprintf( zReason, "Civilian -- EOF while reading slots. KM" );
  1114. #endif
  1115. goto FAIL_LOAD;
  1116. }
  1117. FileRead( hfile, &uiTimeStamp, 4, &uiNumBytesRead );
  1118. if( uiNumBytesRead != 4 )
  1119. {
  1120. #ifdef JA2TESTVERSION
  1121. sprintf( zReason, "Civilian -- EOF while reading uiTimeStamp. KM" );
  1122. #endif
  1123. goto FAIL_LOAD;
  1124. }
  1125. uiTimeSinceLastLoaded = GetWorldTotalMin() - uiTimeStamp;
  1126. FileRead( hfile, &bSectorZ, 1, &uiNumBytesRead );
  1127. if( uiNumBytesRead != 1 )
  1128. {
  1129. #ifdef JA2TESTVERSION
  1130. sprintf( zReason, "Civilian -- EOF while reading bSectorZ. KM" );
  1131. #endif
  1132. goto FAIL_LOAD;
  1133. }
  1134. if( gbWorldSectorZ != bSectorZ )
  1135. {
  1136. #ifdef JA2TESTVERSION
  1137. sprintf( zReason, "Civilian -- bSectorZ mismatch. KM" );
  1138. #endif
  1139. goto FAIL_LOAD;
  1140. }
  1141. if( !slots )
  1142. {
  1143. //no need to restore the enemy's to the map. This means we are restoring a saved game.
  1144. gfRestoringCiviliansFromTempFile = FALSE;
  1145. FileClose( hfile );
  1146. return TRUE;
  1147. }
  1148. if( slots < 0 || slots >= 64 )
  1149. {
  1150. //bad IO!
  1151. #ifdef JA2TESTVERSION
  1152. sprintf( zReason, "Civilian -- illegal slot value of %d. KM", slots );
  1153. #endif
  1154. goto FAIL_LOAD;
  1155. }
  1156. //For all the enemy slots (enemy/creature), clear the fPriorityExistance flag. We will use these flags
  1157. //to determine which slots have been modified as we load the data into the map pristine soldier init list.
  1158. curr = gSoldierInitHead;
  1159. while( curr )
  1160. {
  1161. if( curr->pBasicPlacement->fPriorityExistance )
  1162. {
  1163. if( curr->pBasicPlacement->bTeam == CIV_TEAM )
  1164. {
  1165. curr->pBasicPlacement->fPriorityExistance = FALSE;
  1166. }
  1167. }
  1168. curr = curr->next;
  1169. }
  1170. for( i = 0; i < slots; i++ )
  1171. {
  1172. FileRead( hfile, &tempDetailedPlacement, sizeof( SOLDIERCREATE_STRUCT ), &uiNumBytesRead );
  1173. if( uiNumBytesRead != sizeof( SOLDIERCREATE_STRUCT ) )
  1174. {
  1175. #ifdef JA2TESTVERSION
  1176. sprintf( zReason, "Civilian -- EOF while reading tempDetailedPlacment %d. KM", i );
  1177. #endif
  1178. goto FAIL_LOAD;
  1179. }
  1180. curr = gSoldierInitHead;
  1181. while( curr )
  1182. {
  1183. if( !curr->pBasicPlacement->fPriorityExistance )
  1184. {
  1185. if( !curr->pDetailedPlacement || curr->pDetailedPlacement && curr->pDetailedPlacement->ubProfile == NO_PROFILE )
  1186. {
  1187. if( curr->pBasicPlacement->bTeam == tempDetailedPlacement.bTeam )
  1188. {
  1189. curr->pBasicPlacement->fPriorityExistance = TRUE;
  1190. if( !curr->pDetailedPlacement )
  1191. {
  1192. //need to upgrade the placement to detailed placement
  1193. curr->pDetailedPlacement = (SOLDIERCREATE_STRUCT*)MemAlloc( sizeof( SOLDIERCREATE_STRUCT ) );
  1194. }
  1195. //now replace the map pristine placement info with the temp map file version..
  1196. memcpy( curr->pDetailedPlacement, &tempDetailedPlacement, sizeof( SOLDIERCREATE_STRUCT ) );
  1197. curr->pBasicPlacement->fPriorityExistance = TRUE;
  1198. curr->pBasicPlacement->bDirection = curr->pDetailedPlacement->bDirection;
  1199. curr->pBasicPlacement->bOrders = curr->pDetailedPlacement->bOrders;
  1200. curr->pBasicPlacement->bAttitude = curr->pDetailedPlacement->bAttitude;
  1201. curr->pBasicPlacement->bBodyType = curr->pDetailedPlacement->bBodyType;
  1202. curr->pBasicPlacement->fOnRoof = curr->pDetailedPlacement->fOnRoof;
  1203. curr->pBasicPlacement->ubSoldierClass = curr->pDetailedPlacement->ubSoldierClass;
  1204. curr->pBasicPlacement->ubCivilianGroup = curr->pDetailedPlacement->ubCivilianGroup;
  1205. curr->pBasicPlacement->fHasKeys = curr->pDetailedPlacement->fHasKeys;
  1206. curr->pBasicPlacement->usStartingGridNo = curr->pDetailedPlacement->sInsertionGridNo;
  1207. curr->pBasicPlacement->bPatrolCnt = curr->pDetailedPlacement->bPatrolCnt;
  1208. memcpy( curr->pBasicPlacement->sPatrolGrid, curr->pDetailedPlacement->sPatrolGrid,
  1209. sizeof( INT16 ) * curr->pBasicPlacement->bPatrolCnt );
  1210. FileRead( hfile, &usCheckSum, 2, &uiNumBytesRead );
  1211. if( uiNumBytesRead != 2 )
  1212. {
  1213. #ifdef JA2TESTVERSION
  1214. sprintf( zReason, "Civilian -- EOF while reading usCheckSum %d. KM", i );
  1215. #endif
  1216. goto FAIL_LOAD;
  1217. }
  1218. //verify the checksum equation (anti-hack) -- see save
  1219. usFileCheckSum =
  1220. curr->pDetailedPlacement->bLife * 7 +
  1221. curr->pDetailedPlacement->bLifeMax * 8 -
  1222. curr->pDetailedPlacement->bAgility * 2 +
  1223. curr->pDetailedPlacement->bDexterity * 1 +
  1224. curr->pDetailedPlacement->bExpLevel * 5 -
  1225. curr->pDetailedPlacement->bMarksmanship * 9 +
  1226. curr->pDetailedPlacement->bMedical * 10 +
  1227. curr->pDetailedPlacement->bMechanical * 3 +
  1228. curr->pDetailedPlacement->bExplosive * 4 +
  1229. curr->pDetailedPlacement->bLeadership * 5 +
  1230. curr->pDetailedPlacement->bStrength * 7 +
  1231. curr->pDetailedPlacement->bWisdom * 11 +
  1232. curr->pDetailedPlacement->bMorale * 7 +
  1233. curr->pDetailedPlacement->bAIMorale * 3 -
  1234. curr->pDetailedPlacement->bBodyType * 7 +
  1235. 4 * 6 +
  1236. curr->pDetailedPlacement->sSectorX * 7 -
  1237. curr->pDetailedPlacement->ubSoldierClass * 4 +
  1238. curr->pDetailedPlacement->bTeam * 7 +
  1239. curr->pDetailedPlacement->bDirection * 5 +
  1240. curr->pDetailedPlacement->fOnRoof * 17 +
  1241. curr->pDetailedPlacement->sInsertionGridNo * 1 +
  1242. 3;
  1243. if( usCheckSum != usFileCheckSum )
  1244. {
  1245. //Hacker has modified the stats on the enemy placements.
  1246. #ifdef JA2TESTVERSION
  1247. sprintf( zReason, "Civilian -- checksum for placement %d failed. KM", i );
  1248. #endif
  1249. goto FAIL_LOAD;
  1250. }
  1251. if( curr->pDetailedPlacement->bLife < curr->pDetailedPlacement->bLifeMax )
  1252. { //Add 4 life for every hour that passes.
  1253. INT32 iNewLife;
  1254. iNewLife = curr->pDetailedPlacement->bLife + uiTimeSinceLastLoaded / 15;
  1255. iNewLife = min( curr->pDetailedPlacement->bLifeMax, iNewLife );
  1256. curr->pDetailedPlacement->bLife = (INT8)iNewLife;
  1257. }
  1258. if( curr->pBasicPlacement->bTeam == CIV_TEAM )
  1259. {
  1260. //def: AddPlacementToWorld( curr );
  1261. break;
  1262. }
  1263. }
  1264. }
  1265. }
  1266. curr = curr->next;
  1267. }
  1268. }
  1269. // now remove any non-priority placement which matches the conditions!
  1270. curr = gSoldierInitHead;
  1271. fDeleted = FALSE;
  1272. while( curr )
  1273. {
  1274. if( !curr->pBasicPlacement->fPriorityExistance )
  1275. {
  1276. if( !curr->pDetailedPlacement || curr->pDetailedPlacement && curr->pDetailedPlacement->ubProfile == NO_PROFILE )
  1277. {
  1278. if( curr->pBasicPlacement->bTeam == tempDetailedPlacement.bTeam )
  1279. {
  1280. // Save pointer to the next guy in the list
  1281. // and after deleting, set the 'curr' to that guy
  1282. temp = curr->next;
  1283. RemoveSoldierNodeFromInitList( curr );
  1284. curr = temp;
  1285. fDeleted = TRUE;
  1286. }
  1287. }
  1288. }
  1289. if ( fDeleted )
  1290. {
  1291. // we've already done our pointer update so don't advance the pointer
  1292. fDeleted = FALSE;
  1293. }
  1294. else
  1295. {
  1296. curr = curr->next;
  1297. }
  1298. }
  1299. FileRead( hfile, &ubSectorID, 1, &uiNumBytesRead );
  1300. if( uiNumBytesRead != 1 )
  1301. {
  1302. #ifdef JA2TESTVERSION
  1303. sprintf( zReason, "Civilian -- EOF while reading ubSectorID. KM" );
  1304. #endif
  1305. goto FAIL_LOAD;
  1306. }
  1307. /*
  1308. if( ubSectorID != SECTOR( sSectorX, sSectorY ) )
  1309. {
  1310. #ifdef JA2TESTVERSION
  1311. sprintf( zReason, "Civilian -- ubSectorID mismatch. KM" );
  1312. #endif
  1313. goto FAIL_LOAD;
  1314. }
  1315. */
  1316. //successful
  1317. FileClose( hfile );
  1318. return TRUE;
  1319. FAIL_LOAD:
  1320. //The temp file load failed either because of IO problems related to hacking/logic, or
  1321. //various checks failed for hacker validation. If we reach this point, the "error: exit game"
  1322. //dialog would appear in a non-testversion.
  1323. FileClose( hfile );
  1324. #ifdef JA2TESTVERSION
  1325. AssertMsg( 0, zReason );
  1326. #endif
  1327. return FALSE;
  1328. }
  1329. //If we are saving a game and we are in the sector, we will need to preserve the links between the
  1330. //soldiers and the soldier init list. Otherwise, the temp file will be deleted.
  1331. BOOLEAN NewWayOfSavingEnemyAndCivliansToTempFile( INT16 sSectorX, INT16 sSectorY, INT8 bSectorZ, BOOLEAN fEnemy, BOOLEAN fValidateOnly )
  1332. {
  1333. SOLDIERINITNODE *curr;
  1334. SOLDIERTYPE *pSoldier;
  1335. INT32 i;
  1336. INT32 slots = 0;
  1337. UINT32 uiNumBytesWritten;
  1338. UINT32 uiTimeStamp;
  1339. HWFILE hfile;
  1340. // CHAR8 zTempName[ 128 ];
  1341. CHAR8 zMapName[ 128 ];
  1342. UINT8 ubSectorID;
  1343. UINT16 usCheckSum;
  1344. UINT8 ubStartID=0;
  1345. UINT8 ubEndID = 0;
  1346. //if we are saving the enemy info to the enemy temp file
  1347. if( fEnemy )
  1348. {
  1349. ubStartID = ENEMY_TEAM;
  1350. ubEndID = CREATURE_TEAM;
  1351. }
  1352. //else its the civilian team
  1353. else
  1354. {
  1355. ubStartID = CIV_TEAM;
  1356. ubEndID = CIV_TEAM;
  1357. }
  1358. //STEP ONE: Prep the soldiers for saving...
  1359. //modify the map's soldier init list to reflect the changes to the member's still alive...
  1360. for( i = gTacticalStatus.Team[ ubStartID ].bFirstID; i <= gTacticalStatus.Team[ ubEndID ].bLastID; i++ )
  1361. {
  1362. pSoldier = MercPtrs[ i ];
  1363. //make sure the person is active, alive, in the sector, and is not a profiled person
  1364. if( pSoldier->bActive /*&& pSoldier->bInSector*/ && pSoldier->bLife && pSoldier->ubProfile == NO_PROFILE )
  1365. { //soldier is valid, so find the matching soldier init list entry for modification.
  1366. curr = gSoldierInitHead;
  1367. while( curr && curr->pSoldier != pSoldier )
  1368. {
  1369. curr = curr->next;
  1370. }
  1371. if( curr && curr->pSoldier == pSoldier && pSoldier->ubProfile == NO_PROFILE )
  1372. { //found a match.
  1373. if( !fValidateOnly )
  1374. {
  1375. if( !(gTacticalStatus.uiFlags & LOADING_SAVED_GAME) )
  1376. {
  1377. if( !curr->pDetailedPlacement )
  1378. { //need to upgrade the placement to detailed placement
  1379. curr->pBasicPlacement->fDetailedPlacement = TRUE;
  1380. curr->pDetailedPlacement = (SOLDIERCREATE_STRUCT*)MemAlloc( sizeof( SOLDIERCREATE_STRUCT ) );
  1381. memset( curr->pDetailedPlacement, 0, sizeof( SOLDIERCREATE_STRUCT ) );
  1382. }
  1383. //Copy over the data of the soldier.
  1384. curr->pDetailedPlacement->ubProfile = NO_PROFILE;
  1385. curr->pDetailedPlacement->bLife = pSoldier->bLife;
  1386. curr->pDetailedPlacement->bLifeMax = pSoldier->bLifeMax;
  1387. curr->pDetailedPlacement->bAgility = pSoldier->bAgility;
  1388. curr->pDetailedPlacement->bDexterity = pSoldier->bDexterity;
  1389. curr->pDetailedPlacement->bExpLevel = pSoldier->bExpLevel;
  1390. curr->pDetailedPlacement->bMarksmanship = pSoldier->bMarksmanship;
  1391. curr->pDetailedPlacement->bMedical = pSoldier->bMedical;
  1392. curr->pDetailedPlacement->bMechanical = pSoldier->bMechanical;
  1393. curr->pDetailedPlacement->bExplosive = pSoldier->bExplosive;
  1394. curr->pDetailedPlacement->bLeadership = pSoldier->bLeadership;
  1395. curr->pDetailedPlacement->bStrength = pSoldier->bStrength;
  1396. curr->pDetailedPlacement->bWisdom = pSoldier->bWisdom;
  1397. curr->pDetailedPlacement->bAttitude = pSoldier->bAttitude;
  1398. curr->pDetailedPlacement->bOrders = pSoldier->bOrders;
  1399. curr->pDetailedPlacement->bMorale = pSoldier->bMorale;
  1400. curr->pDetailedPlacement->bAIMorale = pSoldier->bAIMorale;
  1401. curr->pDetailedPlacement->bBodyType = pSoldier->ubBodyType;
  1402. curr->pDetailedPlacement->ubCivilianGroup = pSoldier->ubCivilianGroup;
  1403. curr->pDetailedPlacement->ubScheduleID = pSoldier->ubScheduleID;
  1404. curr->pDetailedPlacement->fHasKeys = pSoldier->bHasKeys;
  1405. curr->pDetailedPlacement->sSectorX = pSoldier->sSectorX;
  1406. curr->pDetailedPlacement->sSectorY = pSoldier->sSectorY;
  1407. curr->pDetailedPlacement->bSectorZ = pSoldier->bSectorZ;
  1408. curr->pDetailedPlacement->ubSoldierClass = pSoldier->ubSoldierClass;
  1409. curr->pDetailedPlacement->bTeam = pSoldier->bTeam;
  1410. curr->pDetailedPlacement->bDirection = pSoldier->bDirection;
  1411. //we don't want the player to think that all the enemies start in the exact position when we
  1412. //left the map, so randomize the start locations either current position or original position.
  1413. if( PreRandom( 2 ) )
  1414. { //use current position
  1415. curr->pDetailedPlacement->fOnRoof = pSoldier->bLevel;
  1416. curr->pDetailedPlacement->sInsertionGridNo = pSoldier->sGridNo;
  1417. }
  1418. else
  1419. { //use original position
  1420. curr->pDetailedPlacement->fOnRoof = curr->pBasicPlacement->fOnRoof;
  1421. curr->pDetailedPlacement->sInsertionGridNo = curr->pBasicPlacement->usStartingGridNo;
  1422. }
  1423. swprintf( curr->pDetailedPlacement->name, pSoldier->name );
  1424. //Copy patrol points
  1425. curr->pDetailedPlacement->bPatrolCnt = pSoldier->bPatrolCnt;
  1426. memcpy( curr->pDetailedPlacement->sPatrolGrid, pSoldier->usPatrolGrid, sizeof( INT16 ) * MAXPATROLGRIDS );
  1427. //copy colors for soldier based on the body type.
  1428. sprintf( curr->pDetailedPlacement->HeadPal, pSoldier->HeadPal );
  1429. sprintf( curr->pDetailedPlacement->VestPal, pSoldier->VestPal );
  1430. sprintf( curr->pDetailedPlacement->SkinPal, pSoldier->SkinPal );
  1431. sprintf( curr->pDetailedPlacement->PantsPal, pSoldier->PantsPal );
  1432. sprintf( curr->pDetailedPlacement->MiscPal, pSoldier->MiscPal );
  1433. //copy soldier's inventory
  1434. memcpy( curr->pDetailedPlacement->Inv, pSoldier->inv, sizeof( OBJECTTYPE ) * NUM_INV_SLOTS );
  1435. }
  1436. }
  1437. //DONE, now increment the counter, so we know how many there are.
  1438. slots++;
  1439. }
  1440. }
  1441. }
  1442. if( !slots )
  1443. {
  1444. if( fEnemy )
  1445. {
  1446. //No need to save anything, so return successfully
  1447. RemoveEnemySoldierTempFile( sSectorX, sSectorY, bSectorZ );
  1448. return TRUE;
  1449. }
  1450. else
  1451. {
  1452. //No need to save anything, so return successfully
  1453. RemoveCivilianTempFile( sSectorX, sSectorY, bSectorZ );
  1454. return( TRUE );
  1455. }
  1456. }
  1457. if( fValidateOnly )
  1458. {
  1459. return( TRUE );
  1460. }
  1461. //STEP TWO: Set up the temp file to write to.
  1462. //Convert the current sector location into a file name
  1463. //GetMapFileName( sSectorX, sSectorY, bSectorZ, zTempName, FALSE );
  1464. if( fEnemy )
  1465. {
  1466. //add the 'e' for 'Enemy preserved' to the front of the map name
  1467. //sprintf( zMapName, "%s\\e_%s", MAPS_DIR, zTempName);
  1468. GetMapTempFileName( SF_ENEMY_PRESERVED_TEMP_FILE_EXISTS, zMapName, sSectorX, sSectorY, bSectorZ );
  1469. }
  1470. else
  1471. {
  1472. //add the 'e' for 'Enemy preserved' to the front of the map name
  1473. //sprintf( zMapName, "%s\\c_%s", MAPS_DIR, zTempName);
  1474. GetMapTempFileName( SF_CIV_PRESERVED_TEMP_FILE_EXISTS, zMapName, sSectorX, sSectorY, bSectorZ );
  1475. }
  1476. //Open the file for writing, Create it if it doesnt exist
  1477. hfile = FileOpen( zMapName, FILE_ACCESS_WRITE | FILE_OPEN_ALWAYS, FALSE );
  1478. if( hfile == 0 )
  1479. { //Error opening map modification file
  1480. return FALSE;
  1481. }
  1482. FileWrite( hfile, &sSectorY, 2, &uiNumBytesWritten );
  1483. if( uiNumBytesWritten != 2 )
  1484. {
  1485. goto FAIL_SAVE;
  1486. }
  1487. //STEP THREE: Save the data
  1488. //this works for both civs and enemies
  1489. SaveSoldierInitListLinks( hfile );
  1490. FileWrite( hfile, &sSectorX, 2, &uiNumBytesWritten );
  1491. if( uiNumBytesWritten != 2 )
  1492. {
  1493. goto FAIL_SAVE;
  1494. }
  1495. //This check may appear confusing. It is intended to abort if the player is saving the game. It is only
  1496. //supposed to preserve the links to the placement list, so when we finally do leave the level with enemies remaining,
  1497. //we will need the links that are only added when the map is loaded, and are normally lost when restoring a save.
  1498. if( gTacticalStatus.uiFlags & LOADING_SAVED_GAME )
  1499. {
  1500. slots = 0;
  1501. }
  1502. FileWrite( hfile, &slots, 4, &uiNumBytesWritten );
  1503. if( uiNumBytesWritten != 4 )
  1504. {
  1505. goto FAIL_SAVE;
  1506. }
  1507. uiTimeStamp = GetWorldTotalMin();
  1508. FileWrite( hfile, &uiTimeStamp, 4, &uiNumBytesWritten );
  1509. if( uiNumBytesWritten != 4 )
  1510. {
  1511. goto FAIL_SAVE;
  1512. }
  1513. FileWrite( hfile, &bSectorZ, 1, &uiNumBytesWritten );
  1514. if( uiNumBytesWritten != 1 )
  1515. {
  1516. goto FAIL_SAVE;
  1517. }
  1518. if( gTacticalStatus.uiFlags & LOADING_SAVED_GAME )
  1519. {
  1520. //if we are saving the game, we don't need to preserve the soldier information, just
  1521. //preserve the links to the placement list.
  1522. slots = 0;
  1523. FileClose( hfile );
  1524. if( fEnemy )
  1525. {
  1526. SetSectorFlag( sSectorX, sSectorY, bSectorZ, SF_ENEMY_PRESERVED_TEMP_FILE_EXISTS );
  1527. }
  1528. else
  1529. {
  1530. SetSectorFlag( sSectorX, sSectorY, bSectorZ, SF_CIV_PRESERVED_TEMP_FILE_EXISTS );
  1531. }
  1532. return TRUE;
  1533. }
  1534. for( i = gTacticalStatus.Team[ ubStartID ].bFirstID; i <= gTacticalStatus.Team[ ubEndID ].bLastID; i++ )
  1535. {
  1536. pSoldier = MercPtrs[ i ];
  1537. // CJC: note that bInSector is not required; the civ could be offmap!
  1538. if( pSoldier->bActive /*&& pSoldier->bInSector*/ && pSoldier->bLife )
  1539. {
  1540. //soldier is valid, so find the matching soldier init list entry for modification.
  1541. curr = gSoldierInitHead;
  1542. while( curr && curr->pSoldier != pSoldier )
  1543. {
  1544. curr = curr->next;
  1545. }
  1546. if( curr && curr->pSoldier == pSoldier && pSoldier->ubProfile == NO_PROFILE )
  1547. {
  1548. //found a match.
  1549. FileWrite( hfile, curr->pDetailedPlacement, sizeof( SOLDIERCREATE_STRUCT ), &uiNumBytesWritten );
  1550. if( uiNumBytesWritten != sizeof( SOLDIERCREATE_STRUCT ) )
  1551. {
  1552. goto FAIL_SAVE;
  1553. }
  1554. //insert a checksum equation (anti-hack)
  1555. usCheckSum =
  1556. curr->pDetailedPlacement->bLife * 7 +
  1557. curr->pDetailedPlacement->bLifeMax * 8 -
  1558. curr->pDetailedPlacement->bAgility * 2 +
  1559. curr->pDetailedPlacement->bDexterity * 1 +
  1560. curr->pDetailedPlacement->bExpLevel * 5 -
  1561. curr->pDetailedPlacement->bMarksmanship * 9 +
  1562. curr->pDetailedPlacement->bMedical * 10 +
  1563. curr->pDetailedPlacement->bMechanical * 3 +
  1564. curr->pDetailedPlacement->bExplosive * 4 +
  1565. curr->pDetailedPlacement->bLeadership * 5 +
  1566. curr->pDetailedPlacement->bStrength * 7 +
  1567. curr->pDetailedPlacement->bWisdom * 11 +
  1568. curr->pDetailedPlacement->bMorale * 7 +
  1569. curr->pDetailedPlacement->bAIMorale * 3 -
  1570. curr->pDetailedPlacement->bBodyType * 7 +
  1571. 4 * 6 +
  1572. curr->pDetailedPlacement->sSectorX * 7 -
  1573. curr->pDetailedPlacement->ubSoldierClass * 4 +
  1574. curr->pDetailedPlacement->bTeam * 7 +
  1575. curr->pDetailedPlacement->bDirection * 5 +
  1576. curr->pDetailedPlacement->fOnRoof * 17 +
  1577. curr->pDetailedPlacement->sInsertionGridNo * 1 +
  1578. 3;
  1579. FileWrite( hfile, &usCheckSum, 2, &uiNumBytesWritten );
  1580. if( uiNumBytesWritten != 2 )
  1581. {
  1582. goto FAIL_SAVE;
  1583. }
  1584. }
  1585. }
  1586. }
  1587. ubSectorID = SECTOR( sSectorX, sSectorY );
  1588. FileWrite( hfile, &ubSectorID, 1, &uiNumBytesWritten );
  1589. if( uiNumBytesWritten != 1 )
  1590. {
  1591. goto FAIL_SAVE;
  1592. }
  1593. FileClose( hfile );
  1594. if( fEnemy )
  1595. {
  1596. SetSectorFlag( sSectorX, sSectorY, bSectorZ, SF_ENEMY_PRESERVED_TEMP_FILE_EXISTS );
  1597. }
  1598. else
  1599. {
  1600. SetSectorFlag( sSectorX, sSectorY, bSectorZ, SF_CIV_PRESERVED_TEMP_FILE_EXISTS );
  1601. }
  1602. return TRUE;
  1603. FAIL_SAVE:
  1604. FileClose( hfile );
  1605. return FALSE;
  1606. }
  1607. BOOLEAN CountNumberOfElitesRegularsAdminsAndCreaturesFromEnemySoldiersTempFile( UINT8 *pubNumElites, UINT8 *pubNumRegulars, UINT8 *pubNumAdmins, UINT8 *pubNumCreatures )
  1608. {
  1609. // SOLDIERINITNODE *curr;
  1610. SOLDIERCREATE_STRUCT tempDetailedPlacement;
  1611. INT32 i;
  1612. INT32 slots = 0;
  1613. UINT32 uiNumBytesRead;
  1614. UINT32 uiTimeStamp;
  1615. HWFILE hfile;
  1616. INT16 sSectorX, sSectorY;
  1617. UINT16 usCheckSum;
  1618. CHAR8 zMapName[ 128 ];
  1619. #ifdef JA2TESTVERSION
  1620. CHAR8 zReason[256];
  1621. #endif
  1622. INT8 bSectorZ;
  1623. UINT8 ubSectorID;
  1624. // UINT8 ubNumElites = 0, ubNumTroops = 0, ubNumAdmins = 0, ubNumCreatures = 0;
  1625. // UINT8 ubStrategicElites, ubStrategicTroops, ubStrategicAdmins, ubStrategicCreatures;
  1626. //make sure the variables are initialized
  1627. *pubNumElites = 0;
  1628. *pubNumRegulars = 0;
  1629. *pubNumAdmins = 0;
  1630. *pubNumCreatures = 0;
  1631. //STEP ONE: Set up the temp file to read from.
  1632. //Convert the current sector location into a file name
  1633. // GetMapFileName( gWorldSectorX, gWorldSectorY, gbWorldSectorZ, zTempName, FALSE );
  1634. //add the 'e' for 'Enemy preserved' to the front of the map name
  1635. // sprintf( zMapName, "%s\\e_%s", MAPS_DIR, zTempName);
  1636. GetMapTempFileName( SF_ENEMY_PRESERVED_TEMP_FILE_EXISTS, zMapName, gWorldSectorX, gWorldSectorY, gbWorldSectorZ );
  1637. //Open the file for reading
  1638. hfile = FileOpen( zMapName, FILE_ACCESS_READ | FILE_OPEN_EXISTING, FALSE );
  1639. if( hfile == 0 )
  1640. { //Error opening map modification file
  1641. return FALSE;
  1642. }
  1643. //STEP TWO: determine whether or not we should use this data.
  1644. //because it is the demo, it is automatically used.
  1645. FileRead( hfile, &sSectorY, 2, &uiNumBytesRead );
  1646. if( uiNumBytesRead != 2 )
  1647. {
  1648. #ifdef JA2TESTVERSION
  1649. sprintf( zReason, "Check EnemySoldier -- EOF while reading sSectorY. KM" );
  1650. #endif
  1651. goto FAIL_LOAD;
  1652. }
  1653. if( gWorldSectorY != sSectorY )
  1654. {
  1655. #ifdef JA2TESTVERSION
  1656. sprintf( zReason, "Check EnemySoldier -- sSectorY mismatch. KM" );
  1657. #endif
  1658. goto FAIL_LOAD;
  1659. }
  1660. // LoadSoldierInitListLinks( hfile );
  1661. LookAtButDontProcessEnemySoldierInitListLinks( hfile );
  1662. //STEP THREE: read the data
  1663. FileRead( hfile, &sSectorX, 2, &uiNumBytesRead );
  1664. if( uiNumBytesRead != 2 )
  1665. {
  1666. #ifdef JA2TESTVERSION
  1667. sprintf( zReason, "Check EnemySoldier -- EOF while reading sSectorX. KM" );
  1668. #endif
  1669. goto FAIL_LOAD;
  1670. }
  1671. if( gWorldSectorX != sSectorX )
  1672. {
  1673. #ifdef JA2TESTVERSION
  1674. sprintf( zReason, "Check EnemySoldier -- sSectorX mismatch. KM" );
  1675. #endif
  1676. goto FAIL_LOAD;
  1677. }
  1678. FileRead( hfile, &slots, 4, &uiNumBytesRead );
  1679. if( uiNumBytesRead != 4 )
  1680. {
  1681. #ifdef JA2TESTVERSION
  1682. sprintf( zReason, "Check EnemySoldier -- EOF while reading slots. KM" );
  1683. #endif
  1684. goto FAIL_LOAD;
  1685. }
  1686. FileRead( hfile, &uiTimeStamp, 4, &uiNumBytesRead );
  1687. if( uiNumBytesRead != 4 )
  1688. {
  1689. #ifdef JA2TESTVERSION
  1690. sprintf( zReason, "Check EnemySoldier -- EOF while reading uiTimeStamp. KM" );
  1691. #endif
  1692. goto FAIL_LOAD;
  1693. }
  1694. FileRead( hfile, &bSectorZ, 1, &uiNumBytesRead );
  1695. if( uiNumBytesRead != 1 )
  1696. {
  1697. #ifdef JA2TESTVERSION
  1698. sprintf( zReason, "Check EnemySoldier -- EOF while reading bSectorZ. KM" );
  1699. #endif
  1700. goto FAIL_LOAD;
  1701. }
  1702. if( gbWorldSectorZ != bSectorZ )
  1703. {
  1704. #ifdef JA2TESTVERSION
  1705. sprintf( zReason, "Check EnemySoldier -- bSectorZ mismatch. KM" );
  1706. #endif
  1707. goto FAIL_LOAD;
  1708. }
  1709. if( !slots )
  1710. {
  1711. //no need to restore the enemy's to the map. This means we are restoring a saved game.
  1712. FileClose( hfile );
  1713. return TRUE;
  1714. }
  1715. if( slots < 0 || slots >= 64 )
  1716. { //bad IO!
  1717. #ifdef JA2TESTVERSION
  1718. sprintf( zReason, "Check EnemySoldier -- illegal slot value of %d. KM", slots );
  1719. #endif
  1720. goto FAIL_LOAD;
  1721. }
  1722. /*
  1723. //get the number of enemies in this sector.
  1724. if( bSectorZ )
  1725. {
  1726. UNDERGROUND_SECTORINFO *pSector;
  1727. pSector = FindUnderGroundSector( sSectorX, sSectorY, bSectorZ );
  1728. if( !pSector )
  1729. {
  1730. #ifdef JA2TESTVERSION
  1731. sprintf( zReason, "EnemySoldier -- Couldn't find underground sector info for (%d,%d,%d) KM", sSectorX, sSectorY, bSectorZ );
  1732. #endif
  1733. goto FAIL_LOAD;
  1734. }
  1735. ubStrategicElites = pSector->ubNumElites;
  1736. ubStrategicTroops = pSector->ubNumTroops;
  1737. ubStrategicAdmins = pSector->ubNumAdmins;
  1738. ubStrategicCreatures = pSector->ubNumCreatures;
  1739. }
  1740. else
  1741. {
  1742. SECTORINFO *pSector;
  1743. pSector = &SectorInfo[ SECTOR( sSectorX, sSectorY ) ];
  1744. ubStrategicCreatures = pSector->ubNumCreatures;
  1745. GetNumberOfEnemiesInSector( sSectorX, sSectorY, &ubStrategicAdmins, &ubStrategicTroops, &ubStrategicElites );
  1746. }
  1747. */
  1748. for( i = 0; i < slots; i++ )
  1749. {
  1750. FileRead( hfile, &tempDetailedPlacement, sizeof( SOLDIERCREATE_STRUCT ), &uiNumBytesRead );
  1751. if( uiNumBytesRead != sizeof( SOLDIERCREATE_STRUCT ) )
  1752. {
  1753. #ifdef JA2TESTVERSION
  1754. sprintf( zReason, "Check EnemySoldier -- EOF while reading tempDetailedPlacment %d. KM", i );
  1755. #endif
  1756. goto FAIL_LOAD;
  1757. }
  1758. //increment the current type of soldier
  1759. switch( tempDetailedPlacement.ubSoldierClass )
  1760. {
  1761. case SOLDIER_CLASS_ELITE:
  1762. (*pubNumElites)++;
  1763. break;
  1764. case SOLDIER_CLASS_ARMY:
  1765. (*pubNumRegulars)++;
  1766. break;
  1767. case SOLDIER_CLASS_ADMINISTRATOR:
  1768. (*pubNumAdmins)++;
  1769. break;
  1770. case SOLDIER_CLASS_CREATURE:
  1771. (*pubNumCreatures)++;
  1772. break;
  1773. }
  1774. FileRead( hfile, &usCheckSum, 2, &uiNumBytesRead );
  1775. if( uiNumBytesRead != 2 )
  1776. {
  1777. #ifdef JA2TESTVERSION
  1778. sprintf( zReason, "Check EnemySoldier -- EOF while reading usCheckSum %d. KM", i );
  1779. #endif
  1780. goto FAIL_LOAD;
  1781. }
  1782. /*
  1783. while( curr )
  1784. {
  1785. if( !curr->pBasicPlacement->fPriorityExistance )
  1786. {
  1787. if( curr->pBasicPlacement->bTeam == tempDetailedPlacement.bTeam )
  1788. {
  1789. curr->pBasicPlacement->fPriorityExistance = TRUE;
  1790. if( !curr->pDetailedPlacement )
  1791. { //need to upgrade the placement to detailed placement
  1792. curr->pDetailedPlacement = (SOLDIERCREATE_STRUCT*)MemAlloc( sizeof( SOLDIERCREATE_STRUCT ) );
  1793. }
  1794. //now replace the map pristine placement info with the temp map file version..
  1795. memcpy( curr->pDetailedPlacement, &tempDetailedPlacement, sizeof( SOLDIERCREATE_STRUCT ) );
  1796. curr->pBasicPlacement->fPriorityExistance = TRUE;
  1797. curr->pBasicPlacement->bDirection = curr->pDetailedPlacement->bDirection;
  1798. curr->pBasicPlacement->bOrders = curr->pDetailedPlacement->bOrders;
  1799. curr->pBasicPlacement->bAttitude = curr->pDetailedPlacement->bAttitude;
  1800. curr->pBasicPlacement->bBodyType = curr->pDetailedPlacement->bBodyType;
  1801. curr->pBasicPlacement->fOnRoof = curr->pDetailedPlacement->fOnRoof;
  1802. curr->pBasicPlacement->ubSoldierClass = curr->pDetailedPlacement->ubSoldierClass;
  1803. curr->pBasicPlacement->ubCivilianGroup = curr->pDetailedPlacement->ubCivilianGroup;
  1804. curr->pBasicPlacement->fHasKeys = curr->pDetailedPlacement->fHasKeys;
  1805. curr->pBasicPlacement->usStartingGridNo = curr->pDetailedPlacement->sInsertionGridNo;
  1806. curr->pBasicPlacement->bPatrolCnt = curr->pDetailedPlacement->bPatrolCnt;
  1807. memcpy( curr->pBasicPlacement->sPatrolGrid, curr->pDetailedPlacement->sPatrolGrid,
  1808. sizeof( INT16 ) * curr->pBasicPlacement->bPatrolCnt );
  1809. FileRead( hfile, &usCheckSum, 2, &uiNumBytesRead );
  1810. if( uiNumBytesRead != 2 )
  1811. {
  1812. #ifdef JA2TESTVERSION
  1813. sprintf( zReason, "EnemySoldier -- EOF while reading usCheckSum %d. KM", i );
  1814. #endif
  1815. goto FAIL_LOAD;
  1816. }
  1817. //verify the checksum equation (anti-hack) -- see save
  1818. usFileCheckSum =
  1819. curr->pDetailedPlacement->bLife * 7 +
  1820. curr->pDetailedPlacement->bLifeMax * 8 -
  1821. curr->pDetailedPlacement->bAgility * 2 +
  1822. curr->pDetailedPlacement->bDexterity * 1 +
  1823. curr->pDetailedPlacement->bExpLevel * 5 -
  1824. curr->pDetailedPlacement->bMarksmanship * 9 +
  1825. curr->pDetailedPlacement->bMedical * 10 +
  1826. curr->pDetailedPlacement->bMechanical * 3 +
  1827. curr->pDetailedPlacement->bExplosive * 4 +
  1828. curr->pDetailedPlacement->bLeadership * 5 +
  1829. curr->pDetailedPlacement->bStrength * 7 +
  1830. curr->pDetailedPlacement->bWisdom * 11 +
  1831. curr->pDetailedPlacement->bMorale * 7 +
  1832. curr->pDetailedPlacement->bAIMorale * 3 -
  1833. curr->pDetailedPlacement->bBodyType * 7 +
  1834. 4 * 6 +
  1835. curr->pDetailedPlacement->sSectorX * 7 -
  1836. curr->pDetailedPlacement->ubSoldierClass * 4 +
  1837. curr->pDetailedPlacement->bTeam * 7 +
  1838. curr->pDetailedPlacement->bDirection * 5 +
  1839. curr->pDetailedPlacement->fOnRoof * 17 +
  1840. curr->pDetailedPlacement->sInsertionGridNo * 1 +
  1841. 3;
  1842. if( usCheckSum != usFileCheckSum )
  1843. { //Hacker has modified the stats on the enemy placements.
  1844. #ifdef JA2TESTVERSION
  1845. sprintf( zReason, "EnemySoldier -- checksum for placement %d failed. KM", i );
  1846. #endif
  1847. goto FAIL_LOAD;
  1848. }
  1849. //Add preserved placements as long as they don't exceed the actual population.
  1850. switch( curr->pBasicPlacement->ubSoldierClass )
  1851. {
  1852. case SOLDIER_CLASS_ELITE:
  1853. ubNumElites++;
  1854. if( ubNumElites <= ubStrategicElites )
  1855. {
  1856. AddPlacementToWorld( curr );
  1857. }
  1858. break;
  1859. case SOLDIER_CLASS_ARMY:
  1860. ubNumTroops++;
  1861. if( ubNumTroops <= ubStrategicTroops )
  1862. {
  1863. AddPlacementToWorld( curr );
  1864. }
  1865. break;
  1866. case SOLDIER_CLASS_ADMINISTRATOR:
  1867. ubNumAdmins++;
  1868. if( ubNumAdmins <= ubStrategicAdmins )
  1869. {
  1870. AddPlacementToWorld( curr );
  1871. }
  1872. break;
  1873. case SOLDIER_CLASS_CREATURE:
  1874. ubNumCreatures++;
  1875. if( ubNumCreatures <= ubStrategicCreatures )
  1876. {
  1877. AddPlacementToWorld( curr );
  1878. }
  1879. break;
  1880. }
  1881. break;
  1882. }
  1883. }
  1884. curr = curr->next;
  1885. }
  1886. */
  1887. }
  1888. FileRead( hfile, &ubSectorID, 1, &uiNumBytesRead );
  1889. if( uiNumBytesRead != 1 )
  1890. {
  1891. #ifdef JA2TESTVERSION
  1892. sprintf( zReason, "Check EnemySoldier -- EOF while reading ubSectorID. KM" );
  1893. #endif
  1894. goto FAIL_LOAD;
  1895. }
  1896. if( ubSectorID != SECTOR( sSectorX, sSectorY ) )
  1897. {
  1898. #ifdef JA2TESTVERSION
  1899. sprintf( zReason, "Check EnemySoldier -- ubSectorID mismatch. KM" );
  1900. #endif
  1901. goto FAIL_LOAD;
  1902. }
  1903. //successful
  1904. FileClose( hfile );
  1905. return TRUE;
  1906. FAIL_LOAD:
  1907. //The temp file load failed either because of IO problems related to hacking/logic, or
  1908. //various checks failed for hacker validation. If we reach this point, the "error: exit game"
  1909. //dialog would appear in a non-testversion.
  1910. FileClose( hfile );
  1911. #ifdef JA2TESTVERSION
  1912. AssertMsg( 0, zReason );
  1913. #endif
  1914. return FALSE;
  1915. }