SupplyWarehouseDockUpdate.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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: SupplyWarehouseDockUpdate.h /////////////////////////////////////////////////////////////////////////////
  24. // Author: Graham Smallwood Feb 2002
  25. // Desc: The action of this dock update is identifying who is docking and either taking Boxes away or giving them
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  28. #include "Common/GlobalData.h"
  29. #include "Common/Xfer.h"
  30. #include "GameClient/Drawable.h"
  31. #include "GameLogic/Module/SupplyWarehouseDockUpdate.h"
  32. #include "GameLogic/Module/SupplyTruckAIUpdate.h"
  33. #include "GameLogic/Object.h"
  34. #include "GameLogic/PartitionManager.h"
  35. #include "GameLogic/AIPathfind.h"
  36. #ifdef _INTERNAL
  37. // for occasional debugging...
  38. //#pragma optimize("", off)
  39. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  40. #endif
  41. // ------------------------------------------------------------------------------------------------
  42. // ------------------------------------------------------------------------------------------------
  43. SupplyWarehouseDockUpdateModuleData::SupplyWarehouseDockUpdateModuleData( void )
  44. {
  45. m_startingBoxesData = 1;
  46. m_deleteWhenEmpty = FALSE;
  47. }
  48. // ------------------------------------------------------------------------------------------------
  49. // ------------------------------------------------------------------------------------------------
  50. /*static*/ void SupplyWarehouseDockUpdateModuleData::buildFieldParse(MultiIniFieldParse& p)
  51. {
  52. DockUpdateModuleData::buildFieldParse( p );
  53. static const FieldParse dataFieldParse[] =
  54. {
  55. { "StartingBoxes", INI::parseInt, NULL, offsetof( SupplyWarehouseDockUpdateModuleData, m_startingBoxesData ) },
  56. { "DeleteWhenEmpty", INI::parseBool, NULL, offsetof( SupplyWarehouseDockUpdateModuleData, m_deleteWhenEmpty ) },
  57. { 0, 0, 0, 0 }
  58. };
  59. p.add(dataFieldParse);
  60. } // end buildFieldParse
  61. // ------------------------------------------------------------------------------------------------
  62. // ------------------------------------------------------------------------------------------------
  63. SupplyWarehouseDockUpdate::SupplyWarehouseDockUpdate( Thing *thing, const ModuleData* moduleData ) : DockUpdate( thing, moduleData )
  64. {
  65. m_boxesStored = getSupplyWarehouseDockUpdateModuleData()->m_startingBoxesData;
  66. }
  67. SupplyWarehouseDockUpdate::~SupplyWarehouseDockUpdate()
  68. {
  69. }
  70. void SupplyWarehouseDockUpdate::onObjectCreated()
  71. {
  72. Drawable *draw = getObject()->getDrawable();
  73. if( draw )
  74. {
  75. draw->updateDrawableSupplyStatus( getSupplyWarehouseDockUpdateModuleData()->m_startingBoxesData, m_boxesStored );
  76. }
  77. }
  78. Bool SupplyWarehouseDockUpdate::action( Object* docker, Object *drone )
  79. {
  80. if( m_boxesStored == 0 )
  81. return FALSE;
  82. // Make sure that the docker is at least reasonably close to the dock.
  83. // Basically, one bounding diameter of space or less between us.
  84. Real closeEnoughSqr = sqr(docker->getGeometryInfo().getBoundingCircleRadius()*2);
  85. Real curDistSqr = ThePartitionManager->getDistanceSquared(docker, getObject(), FROM_BOUNDINGSPHERE_2D);
  86. if (curDistSqr > closeEnoughSqr) {
  87. DEBUG_LOG(("Failing dock, dist %f, not close enough(%f).\n", sqrt(curDistSqr), sqrt(closeEnoughSqr)));
  88. // Make it twitch a little.
  89. Coord3D newPos = *docker->getPosition();
  90. Real range = 0.4*PATHFIND_CELL_SIZE_F;
  91. newPos.x += GameLogicRandomValue(-range, range);
  92. newPos.y += GameLogicRandomValue(-range, range);
  93. docker->setPosition(&newPos);
  94. return FALSE; //not close enough.
  95. }
  96. --m_boxesStored;// so the docker sees that I am shy by one box (or empty) from within his gainOneBox()
  97. SupplyTruckAIInterface *ai = docker->getAIUpdateInterface()->getSupplyTruckAIInterface();
  98. if( ai && ai->gainOneBox( m_boxesStored ) )
  99. {
  100. if( m_boxesStored == 0 && getSupplyWarehouseDockUpdateModuleData()->m_deleteWhenEmpty )
  101. {
  102. TheGameLogic->destroyObject( getObject() );
  103. return FALSE; //Yer done. And so am I.
  104. }
  105. else
  106. {
  107. Drawable *draw = getObject()->getDrawable();
  108. if( draw )
  109. {
  110. draw->updateDrawableSupplyStatus( getSupplyWarehouseDockUpdateModuleData()->m_startingBoxesData, m_boxesStored );
  111. }
  112. }
  113. return TRUE;
  114. }
  115. else
  116. ++m_boxesStored; //take it back, since there was noone to gain the box
  117. //this is important so that I have one less boxes as perceived by the docker when he gains one
  118. return FALSE;
  119. }
  120. void SupplyWarehouseDockUpdate::setDockCrippled( Bool setting )
  121. {
  122. // At this level, Crippling means I kill any activeDocker between enter and exit.
  123. if( setting )
  124. {
  125. if( m_activeDocker != INVALID_ID )
  126. {
  127. Object *victim = TheGameLogic->findObjectByID( m_activeDocker );
  128. if( victim )
  129. {
  130. if( m_dockerInside )
  131. {
  132. if( !victim->isUsingAirborneLocomotor() )
  133. victim->kill();
  134. }
  135. else
  136. {
  137. // Else, he was between Approach and Enter. Lucky guy. Tell him to stop, but then
  138. // remind him that he wants to try again later
  139. SupplyTruckAIInterface* supplyTruckAI = victim->getAI()->getSupplyTruckAIInterface();
  140. if( supplyTruckAI )
  141. {
  142. victim->getAI()->aiIdle( CMD_FROM_AI );
  143. supplyTruckAI->setForceWantingState( TRUE );
  144. }
  145. }
  146. }
  147. }
  148. }
  149. DockUpdate::setDockCrippled( setting );
  150. }
  151. void SupplyWarehouseDockUpdate::setCashValue( Int cashValue )
  152. {
  153. // A script can tell us our set value, and we need to figure out the boxes needed to provide that.
  154. m_boxesStored = ceil(cashValue / (float)TheGlobalData->m_baseValuePerSupplyBox);
  155. Drawable *draw = getObject()->getDrawable();
  156. if( draw )
  157. {
  158. draw->updateDrawableSupplyStatus( getSupplyWarehouseDockUpdateModuleData()->m_startingBoxesData, m_boxesStored );
  159. }
  160. }
  161. // ------------------------------------------------------------------------------------------------
  162. /** CRC */
  163. // ------------------------------------------------------------------------------------------------
  164. void SupplyWarehouseDockUpdate::crc( Xfer *xfer )
  165. {
  166. // extend base class
  167. DockUpdate::crc( xfer );
  168. } // end crc
  169. // ------------------------------------------------------------------------------------------------
  170. /** Xfer method
  171. * Version Info:
  172. * 1: Initial version */
  173. // ------------------------------------------------------------------------------------------------
  174. void SupplyWarehouseDockUpdate::xfer( Xfer *xfer )
  175. {
  176. // version
  177. XferVersion currentVersion = 1;
  178. XferVersion version = currentVersion;
  179. xfer->xferVersion( &version, currentVersion );
  180. // extend base class
  181. DockUpdate::xfer( xfer );
  182. // boxes stored
  183. xfer->xferInt( &m_boxesStored );
  184. } // end xfer
  185. // ------------------------------------------------------------------------------------------------
  186. /** Load post process */
  187. // ------------------------------------------------------------------------------------------------
  188. void SupplyWarehouseDockUpdate::loadPostProcess( void )
  189. {
  190. // extend base class
  191. DockUpdate::loadPostProcess();
  192. // update the drawable supply status
  193. const SupplyWarehouseDockUpdateModuleData *modData = getSupplyWarehouseDockUpdateModuleData();
  194. Object *us = getObject();
  195. Drawable *draw = us->getDrawable();
  196. if( draw )
  197. draw->updateDrawableSupplyStatus( modData->m_startingBoxesData, m_boxesStored );
  198. } // end loadPostProcess