forest.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #define FOREST_CPP
  2. /*************************************************************************************************\
  3. forest.cpp : Implementation of the forest component.
  4. //---------------------------------------------------------------------------//
  5. // Copyright (C) Microsoft Corporation. All rights reserved. //
  6. //===========================================================================//
  7. \*************************************************************************************************/
  8. #include "forest.h"
  9. #include "mclib.h"
  10. #include "resource.h"
  11. #include "EditorObjectMgr.h"
  12. extern unsigned long gameResourceHandle;
  13. Forest::Forest( int newID )
  14. {
  15. centerX = centerY = 0;
  16. radius = 1.0f;
  17. for ( int i = 0; i < FOREST_TYPES; i++ )
  18. {
  19. percentages[i] = 0;
  20. }
  21. ID = newID;
  22. if ((-1 == newID) && (EditorObjectMgr::instance())) {
  23. ID = EditorObjectMgr::instance()->getNextAvailableForestID();
  24. }
  25. maxDensity = 7.f;
  26. minDensity = .001f;
  27. maxHeight = 1.25;
  28. minHeight = .75;
  29. bRandom = true;
  30. char tmp[256];
  31. cLoadString( IDS_UNNAMDE_FOREST, tmp, 255, gameResourceHandle );
  32. name.Format( tmp, ID );
  33. }
  34. Forest::Forest( const Forest& src )
  35. {
  36. centerX = src.centerX;
  37. centerY = src.centerY;
  38. fileName = src.fileName;
  39. bRandom = src.bRandom;
  40. maxHeight = src.maxHeight;
  41. minHeight = src.minHeight;
  42. maxDensity = src.maxDensity;
  43. minDensity = src.minDensity;
  44. name = src.name;
  45. radius = src.radius;
  46. for ( int i = 0; i < FOREST_TYPES; i++ )
  47. {
  48. percentages[i] = src.percentages[i];
  49. }
  50. ID = src.ID; // this is a bit worisome
  51. }
  52. Forest& Forest::operator=( const Forest& src )
  53. {
  54. if ( &src != this )
  55. {
  56. centerX = src.centerX;
  57. centerY = src.centerY;
  58. fileName = src.fileName;
  59. bRandom = src.bRandom;
  60. maxHeight = src.maxHeight;
  61. minHeight = src.minHeight;
  62. maxDensity = src.maxDensity;
  63. minDensity = src.minDensity;
  64. radius = src.radius;
  65. for ( int i = 0; i < FOREST_TYPES; i++ )
  66. {
  67. percentages[i] = src.percentages[i];
  68. }
  69. ID = src.ID; // this is a bit worisome
  70. name = src.name;
  71. }
  72. return *this;
  73. }
  74. void Forest::init()
  75. {
  76. FitIniFile file;
  77. if ( NO_ERR != file.open( fileName ) )
  78. {
  79. char errorString[256];
  80. sprintf( errorString, "Couldn't open file %s", (const char*)fileName);
  81. Assert( 0, 0, errorString );
  82. return;
  83. }
  84. file.seekBlock( "ForestInfo" );
  85. init( file );
  86. }
  87. void Forest::init( FitIniFile& file )
  88. {
  89. char headerName[256];
  90. for ( int i = 0; i < FOREST_TYPES; i++ )
  91. {
  92. sprintf( headerName, "TreeType%ld", i );
  93. file.readIdFloat( headerName, percentages[i] );
  94. }
  95. char tmp[256];
  96. tmp[0] = 0;
  97. file.readIdFloat( "HeightMin", minHeight );
  98. file.readIdFloat( "HeightMax", maxHeight );
  99. file.readIdFloat( "DensityMin", minDensity );
  100. file.readIdFloat( "DensityMax", maxDensity );
  101. file.readIdFloat( "CenterX", centerX );
  102. file.readIdFloat( "CenterY", centerY );
  103. file.readIdBoolean( "Random", bRandom );
  104. file.readIdFloat( "Radius", radius );
  105. file.readIdString( "Name", tmp, 255);
  106. name = tmp;
  107. tmp[0] = 0;
  108. file.readIdString( "FileName", tmp, 255 );
  109. fileName = tmp;
  110. }
  111. void Forest::save()
  112. {
  113. FitIniFile file;
  114. if ( NO_ERR != file.create( (char*)(const char*)fileName ) )
  115. {
  116. char errorString[256];
  117. sprintf( errorString, "Couldn't create file %s", (const char*)fileName);
  118. Assert( 0, 0, errorString );
  119. return;
  120. }
  121. file.writeBlock( "ForestInfo" );
  122. save( file );
  123. }
  124. void Forest::save( FitIniFile& file )
  125. {
  126. char headerName[256];
  127. for ( int i = 0; i < FOREST_TYPES; i++ )
  128. {
  129. sprintf( headerName, "TreeType%ld", i );
  130. file.writeIdFloat( headerName, percentages[i] );
  131. }
  132. file.writeIdFloat( "HeightMin", minHeight );
  133. file.writeIdFloat( "HeightMax", maxHeight );
  134. file.writeIdFloat( "DensityMin", minDensity );
  135. file.writeIdFloat( "DensityMax", maxDensity );
  136. file.writeIdFloat( "CenterX", centerX );
  137. file.writeIdFloat( "CenterY", centerY );
  138. file.writeIdBoolean( "Random", bRandom );
  139. file.writeIdFloat( "Radius", radius );
  140. file.writeIdString( "Name", name );
  141. file.writeIdString( "FileName", fileName );
  142. }
  143. //*************************************************************************************************
  144. // end of file ( forest.cpp )