CreateCrateDie.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: CreateCrateDie.cpp /////////////////////////////////////////////////////////////////////////////
  24. // Author: Graham Smallwood, February 2002
  25. // Desc: A chance to create a crate on death according to certain condition checks
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  28. #include "Common/PlayerList.h"
  29. #include "Common/Player.h"
  30. #include "Common/RandomValue.h"
  31. #include "Common/ThingFactory.h"
  32. #include "Common/ThingTemplate.h"
  33. #include "Common/Xfer.h"
  34. #include "GameLogic/CrateSystem.h"
  35. #include "GameLogic/ExperienceTracker.h"
  36. #include "GameLogic/GameLogic.h"
  37. #include "GameLogic/Object.h"
  38. #include "GameLogic/PartitionManager.h"
  39. #include "GameLogic/Module/CreateCrateDie.h"
  40. #include "GameLogic/Module/AIUpdate.h"
  41. //-------------------------------------------------------------------------------------------------
  42. //-------------------------------------------------------------------------------------------------
  43. CreateCrateDie::CreateCrateDie( Thing *thing, const ModuleData* moduleData ) : DieModule( thing, moduleData )
  44. {
  45. }
  46. //-------------------------------------------------------------------------------------------------
  47. //-------------------------------------------------------------------------------------------------
  48. CreateCrateDie::~CreateCrateDie( void )
  49. {
  50. }
  51. void CreateCrateDieModuleData::parseCrateData( INI* ini, void *instance, void * /*store*/, const void* /*userData*/ )
  52. {
  53. CreateCrateDieModuleData* self = (CreateCrateDieModuleData*)instance;
  54. AsciiString crateName = ini->getNextToken();
  55. self->m_crateNameList.push_back( crateName );
  56. }
  57. //-------------------------------------------------------------------------------------------------
  58. /** The die callback. */
  59. //-------------------------------------------------------------------------------------------------
  60. void CreateCrateDie::onDie( const DamageInfo * damageInfo )
  61. {
  62. if (!isDieApplicable(damageInfo))
  63. return;
  64. CrateTemplate const *currentCrateData = NULL;
  65. Object *killer = TheGameLogic->findObjectByID( damageInfo->in.m_sourceID );
  66. Object *me = getObject();
  67. if( killer && killer->getRelationship( me ) == ALLIES )
  68. return; //Nope, no crate for killing ally at all.
  69. for( AsciiStringListConstIterator iter = getCreateCrateDieModuleData()->m_crateNameList.begin();
  70. iter != getCreateCrateDieModuleData()->m_crateNameList.end();
  71. iter++
  72. )
  73. {
  74. currentCrateData = TheCrateSystem->findCrateTemplate( *iter );
  75. if( currentCrateData )
  76. {
  77. if( ! testCreationChance( currentCrateData ) )
  78. continue;// always test this
  79. if( (currentCrateData->m_veterancyLevel != LEVEL_INVALID) && ! testVeterancyLevel( currentCrateData ) )
  80. continue; //If this is set up to test and it fails
  81. if( KINDOFMASK_ANY_SET(currentCrateData->m_killedByTypeKindof) && !testKillerType( currentCrateData, killer ) )
  82. continue; //If this is set up to test and it fails
  83. if( (currentCrateData->m_killerScience != SCIENCE_INVALID) && !testKillerScience( currentCrateData, killer ) )
  84. continue; //If this is set up to test and it fails
  85. Object *crate = createCrate( currentCrateData );//Make the crate
  86. if( crate )
  87. {
  88. // Design needs to set ownership of crates sometimes
  89. if( currentCrateData->m_isOwnedByMaker )
  90. {
  91. crate->setTeam( me->getControllingPlayer()->getDefaultTeam() );
  92. }
  93. if (killer)
  94. {
  95. // If the killer is a computer controlled player, notify that the crate exists.
  96. if (killer->getControllingPlayer() &&
  97. killer->getControllingPlayer()->getPlayerType()==PLAYER_COMPUTER)
  98. {
  99. AIUpdateInterface *ai = killer->getAIUpdateInterface();
  100. if (ai)
  101. {
  102. ai->notifyCrate( crate->getID() );
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
  110. Bool CreateCrateDie::testCreationChance( CrateTemplate const *currentCrateData )
  111. {
  112. Real testAgainst = currentCrateData->m_creationChance;
  113. Real testWith = GameLogicRandomValueReal( 0, 1 );
  114. return testWith < testAgainst;
  115. }
  116. Bool CreateCrateDie::testVeterancyLevel( CrateTemplate const *currentCrateData )
  117. {
  118. VeterancyLevel testAgainst = currentCrateData->m_veterancyLevel;
  119. VeterancyLevel testWith = getObject()->getVeterancyLevel();
  120. return testAgainst == testWith;
  121. }
  122. Bool CreateCrateDie::testKillerType( CrateTemplate const *currentCrateData, Object *killer )
  123. {
  124. if( killer == NULL )
  125. return FALSE;
  126. // Must match the whole group of bits set in the KilledBy description (most likely One).
  127. if( ! killer->getTemplate()->isKindOfMulti( currentCrateData->m_killedByTypeKindof, KINDOFMASK_NONE ) )
  128. return FALSE;
  129. return TRUE;
  130. }
  131. Bool CreateCrateDie::testKillerScience( CrateTemplate const *currentCrateData, Object *killer )
  132. {
  133. if( killer == NULL )
  134. return FALSE;
  135. // killer's player must have the listed science
  136. Player *killerPlayer = killer->getControllingPlayer();
  137. if( killerPlayer == NULL )
  138. return FALSE;
  139. if( ! killerPlayer->hasScience( currentCrateData->m_killerScience ) )
  140. return FALSE;
  141. return TRUE;
  142. }
  143. Object *CreateCrateDie::createCrate( CrateTemplate const *currentCrateData )
  144. {
  145. Coord3D centerPoint = *getObject()->getPosition();
  146. PathfindLayerEnum layer = getObject()->getLayer();
  147. // CreationChance is used for the success of this block, but this block can have any number of potential actual crates
  148. Real multipleCratePick = GameLogicRandomValueReal( 0, 1 );
  149. Real multipleCrateRunningTotal = 0;
  150. AsciiString crateName = "";
  151. for( crateCreationEntryConstIterator iter = currentCrateData->m_possibleCrates.begin();
  152. iter != currentCrateData->m_possibleCrates.end();
  153. iter++
  154. )
  155. {
  156. multipleCrateRunningTotal += (*iter).crateChance;
  157. if( multipleCrateRunningTotal > multipleCratePick )
  158. {
  159. // Run through the list of possibles, and if the sum of the chances is greater than my random pick,
  160. // then this is the correct one. (This simulates contiguous %, or weighted distribution)
  161. crateName = (*iter).crateName;
  162. break;
  163. }
  164. }
  165. // At this point, I could very well have a "" for the type, if the Designer didn't make the sum of chances 1
  166. ThingTemplate const *crateType = TheThingFactory->findTemplate( crateName );
  167. if( crateType == NULL )
  168. return NULL;
  169. Bool spotFound = FALSE;
  170. Coord3D creationPoint;
  171. FindPositionOptions fpOptions;
  172. fpOptions.minRadius = 0.0f;
  173. fpOptions.maxRadius = 5.0f;
  174. fpOptions.relationshipObject = getObject();
  175. fpOptions.flags = FPF_IGNORE_ALLY_OR_NEUTRAL_UNITS; // So the dead guy won't block, nor will his dead hulk.
  176. if (layer != LAYER_GROUND) {
  177. creationPoint = centerPoint;
  178. spotFound = true;
  179. } else if( ThePartitionManager->findPositionAround( &centerPoint, &fpOptions, &creationPoint ) )
  180. {
  181. spotFound = TRUE;
  182. }
  183. else
  184. {
  185. // If the tight ignore units scan fails, then try a great big scan so we appear on the edge
  186. // of the large dead thing (building rubble)
  187. fpOptions.minRadius = 0.0f;
  188. fpOptions.maxRadius = 125.0f;
  189. fpOptions.relationshipObject = NULL;
  190. fpOptions.flags = FPF_NONE;
  191. if( ThePartitionManager->findPositionAround( &centerPoint, &fpOptions, &creationPoint ) )
  192. {
  193. spotFound = TRUE;
  194. }
  195. }
  196. if( spotFound )
  197. {
  198. Object *newCrate = TheThingFactory->newObject( crateType, NULL );
  199. newCrate->setPosition( &creationPoint );
  200. newCrate->setOrientation( GameLogicRandomValueReal( 0, 2*PI ) );
  201. newCrate->setLayer(layer);
  202. Drawable *crateDrawable = newCrate->getDrawable();
  203. if( crateDrawable )
  204. {
  205. crateDrawable->setTerrainDecal(TERRAIN_DECAL_CRATE);
  206. crateDrawable->setTerrainDecalSize(2.5f * newCrate->getGeometryInfo().getMajorRadius(),
  207. 2.5f * newCrate->getGeometryInfo().getMajorRadius() ) ;
  208. crateDrawable->setTerrainDecalFadeTarget(1.0f, 0.03f);
  209. }
  210. return newCrate;
  211. }
  212. return NULL;
  213. }
  214. // ------------------------------------------------------------------------------------------------
  215. /** CRC */
  216. // ------------------------------------------------------------------------------------------------
  217. void CreateCrateDie::crc( Xfer *xfer )
  218. {
  219. // extend base class
  220. DieModule::crc( xfer );
  221. } // end crc
  222. // ------------------------------------------------------------------------------------------------
  223. /** Xfer method
  224. * Version Info:
  225. * 1: Initial version */
  226. // ------------------------------------------------------------------------------------------------
  227. void CreateCrateDie::xfer( Xfer *xfer )
  228. {
  229. // version
  230. XferVersion currentVersion = 1;
  231. XferVersion version = currentVersion;
  232. xfer->xferVersion( &version, currentVersion );
  233. // extend base class
  234. DieModule::xfer( xfer );
  235. } // end xfer
  236. // ------------------------------------------------------------------------------------------------
  237. /** Load post process */
  238. // ------------------------------------------------------------------------------------------------
  239. void CreateCrateDie::loadPostProcess( void )
  240. {
  241. // extend base class
  242. DieModule::loadPostProcess();
  243. } // end loadPostProcess