LogisticsComponent.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #define LOGISTICSCOMPONENT_CPP
  2. /*************************************************************************************************\
  3. LogisticsComponent.cpp : Implementation of the LogisticsComponent component.
  4. //---------------------------------------------------------------------------//
  5. // Copyright (C) Microsoft Corporation. All rights reserved. //
  6. //===========================================================================//
  7. \*************************************************************************************************/
  8. #include "LogisticsComponent.h"
  9. #include <gameos.hpp>
  10. #include "Cmponent.h"
  11. #include "..\resource.h"
  12. #include "utilities.h"
  13. extern char* ComponentFormString[];
  14. long LogisticsComponent::XICON_FACTOR = 48;
  15. long LogisticsComponent::YICON_FACTOR = 32;
  16. float LogisticsComponent::MAX_HEAT = 20.f;
  17. float LogisticsComponent::MAX_DAMAGE = 15.f;
  18. float LogisticsComponent::MAX_RECYCLE = 10.75f;
  19. float LogisticsComponent::MAX_RANGE = 3.f;
  20. /*char* LogisticsComponent::ComponentFormString[NUM_COMPONENT_FORMS] =
  21. {
  22. "Simple",
  23. "Cockpit",
  24. "Sensor",
  25. "Actuator",
  26. "Engine",
  27. "HeatSink",
  28. "Weapon",]
  29. "EnergyWeapon",
  30. "BallsticWeapon",
  31. "MissileWeapon",
  32. "Ammo",
  33. "JumpJet",
  34. "Case",
  35. "LifeSuport",
  36. "Gyroscope",
  37. "PowerAmplifier",
  38. "ECM",
  39. "Probe",
  40. "Jammer",
  41. "Bulk",
  42. };*/
  43. LogisticsComponent::LogisticsComponent()
  44. {
  45. name = NULL;
  46. ID = -1;
  47. flavorText = NULL;
  48. bHead = bTorso = bLegs = 0;
  49. iconFileName = 0;
  50. pictureFileName = 0;
  51. iconX = iconY = 0;
  52. bAvailable = 0;
  53. recycleTime = 0.f;
  54. }
  55. //-------------------------------------------------------------------------------------------------
  56. LogisticsComponent::~LogisticsComponent()
  57. {
  58. if ( name )
  59. delete name;
  60. if ( iconFileName )
  61. delete iconFileName;
  62. if ( pictureFileName )
  63. delete pictureFileName;
  64. }
  65. int LogisticsComponent::init( char* dataLine )
  66. {
  67. char* line = dataLine;
  68. char* pLine = line;
  69. char pBuffer[1025];
  70. ID = (extractInt( pLine ));
  71. // the type
  72. extractString( pLine, pBuffer, 1024 );
  73. for ( int i = 0; i < NUM_COMPONENT_FORMS; ++i )
  74. {
  75. if ( 0 == stricmp( ComponentFormString[i], pBuffer ) )
  76. {
  77. Type = i;
  78. break;
  79. }
  80. }
  81. if ( i == NUM_COMPONENT_FORMS )
  82. return -1;
  83. // name, probably aren't going to use this, they should be in the RC.
  84. extractString( pLine, pBuffer, 1024 );
  85. // name, probably aren't going to use this, they should be in the RC.
  86. extractString( pLine, pBuffer, 1024 ); // ignore critical hits
  87. recycleTime = extractFloat( pLine );
  88. heat = extractFloat( pLine );
  89. // weight
  90. weight = extractFloat( pLine );
  91. damage = extractFloat( pLine );
  92. // ignore battle rating
  93. extractString( pLine, pBuffer, 1024 );
  94. // cost
  95. cost = extractInt( pLine );
  96. // range
  97. extractString( pLine, pBuffer, 1024 );
  98. if ( !isWeapon() )
  99. rangeType = NO_RANGE;
  100. else if ( !strcmp( pBuffer, "long" ) )
  101. rangeType = LONG;
  102. else if ( !strcmp( pBuffer, "medium" ) )
  103. rangeType = MEDIUM;
  104. else
  105. rangeType = SHORT;
  106. // we need to figure out where things can go
  107. extractString( pLine, pBuffer, 1024 );
  108. bHead = stricmp( pBuffer, "Yes" ) ? false : true;
  109. extractString( pLine, pBuffer, 1024 );
  110. bTorso = stricmp( pBuffer, "Yes" ) ? false : true;
  111. // ignore the next 4 columns
  112. for ( i = 0; i < 4; ++i )
  113. extractString( pLine, pBuffer, 1024 );
  114. extractString( pLine, pBuffer, 1024 );
  115. bLegs = stricmp( pBuffer, "Yes" ) ? false : true;
  116. // ignore the next 4 columns
  117. for ( i = 0; i < 4; ++i )
  118. extractString( pLine, pBuffer, 1024 );
  119. Ammo = extractInt( pLine );
  120. // now read in icon info
  121. extractString( pLine, pBuffer, 1024 );
  122. if ( *pBuffer && (pBuffer[0] != '0') )
  123. {
  124. iconFileName = new char[strlen( pBuffer ) + 1];
  125. strcpy( iconFileName, pBuffer );
  126. }
  127. else
  128. return -1; // fail if no picture
  129. extractString( pLine, pBuffer, 1024 );
  130. if ( *pBuffer )
  131. {
  132. pictureFileName = new char[strlen( pBuffer ) + 1]; //Forgot the NULL all over the place did we?
  133. strcpy( pictureFileName, pBuffer );
  134. }
  135. stringID = extractInt( pLine );
  136. helpStringID = extractInt( pLine );
  137. iconX = extractInt( pLine );
  138. iconY = extractInt( pLine );
  139. char nameBuffer[256];
  140. cLoadString( stringID, nameBuffer, 256 );
  141. name = flavorText = new char[strlen( nameBuffer ) + 1]; //Lets not forget the NULL!!!
  142. strcpy( name, nameBuffer );
  143. return ID;
  144. }
  145. int LogisticsComponent::extractString( char*& pFileLine, char* pBuffer, int bufferLength )
  146. {
  147. *pBuffer = 0;
  148. for ( int i = 0; i < 512; ++i )
  149. {
  150. if ( pFileLine[i] == '\n' )
  151. break;
  152. else if ( pFileLine[i] == ',' )
  153. break;
  154. else if ( pFileLine[i] == NULL )
  155. break;
  156. }
  157. if ( i == 512 )
  158. return false;
  159. gosASSERT( i < bufferLength );
  160. memcpy( pBuffer, pFileLine, i );
  161. pBuffer[i] = NULL;
  162. bufferLength = i + 1;
  163. pFileLine += i + 1;
  164. return i;
  165. }
  166. int LogisticsComponent::extractInt( char*& pFileLine )
  167. {
  168. char buffer[1024];
  169. int count = extractString( pFileLine, buffer, 1024 );
  170. if ( count > 0 )
  171. {
  172. return atoi( buffer );
  173. }
  174. return -1;
  175. }
  176. float LogisticsComponent::extractFloat( char*& pFileLine )
  177. {
  178. char buffer[1024];
  179. int count = extractString( pFileLine, buffer, 1024 );
  180. if ( count > 0 )
  181. {
  182. return atof( buffer );
  183. }
  184. return -1;
  185. }
  186. bool LogisticsComponent::compare( LogisticsComponent* second, int type )
  187. {
  188. switch( type )
  189. {
  190. case DAMAGE:
  191. return second->damage > damage;
  192. break;
  193. case WEIGHT:
  194. return second->weight > damage;
  195. break;
  196. case HEAT:
  197. return second->heat > heat;
  198. break;
  199. case NAME:
  200. return stricmp( name, second->name ) > 0;
  201. break;
  202. case RANGE:
  203. return second->damage > damage;
  204. break;
  205. }
  206. return 0;
  207. }
  208. bool LogisticsComponent::isWeapon()
  209. {
  210. return Type == COMPONENT_FORM_WEAPON ||
  211. Type == COMPONENT_FORM_WEAPON_ENERGY ||
  212. Type == COMPONENT_FORM_WEAPON_BALLISTIC ||
  213. Type == COMPONENT_FORM_WEAPON_MISSILE;
  214. }
  215. //*************************************************************************************************
  216. // end of file ( LogisticsComponent.cpp )