WorkerAIUpdate.cpp 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472
  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: WorkerAIUpdate.cpp ///////////////////////////////////////////////////////////////////////
  24. // Author: Graham Smallwood, June 2002
  25. // Desc: A Worker is a unit that is both a Dozer and a Supply Truck.
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // USER INCLUDES //////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  29. #include "Common/ActionManager.h"
  30. #include "Common/Team.h"
  31. #include "Common/StateMachine.h"
  32. #include "Common/BuildAssistant.h"
  33. #include "Common/GameState.h"
  34. #include "Common/ThingTemplate.h"
  35. #include "Common/ThingFactory.h"
  36. #include "Common/Player.h"
  37. #include "Common/Money.h"
  38. #include "Common/Radar.h"
  39. #include "Common/RandomValue.h"
  40. #include "Common/GlobalData.h"
  41. #include "Common/ResourceGatheringManager.h"
  42. #include "GameClient/Drawable.h"
  43. #include "GameClient/GameText.h"
  44. #include "GameClient/InGameUI.h"
  45. #include "GameLogic/AIPathfind.h"
  46. #include "GameLogic/Locomotor.h"
  47. #include "GameLogic/PartitionManager.h"
  48. #include "GameLogic/Module/BodyModule.h"
  49. #include "GameLogic/Module/BridgeBehavior.h"
  50. #include "GameLogic/Module/BridgeTowerBehavior.h"
  51. #include "GameLogic/Module/CreateModule.h"
  52. #include "GameLogic/Module/SupplyTruckAIUpdate.h"
  53. #include "GameLogic/Module/SupplyCenterDockUpdate.h"
  54. #include "GameLogic/Module/SupplyWarehouseDockUpdate.h"
  55. #include "GameLogic/Module/WorkerAIUpdate.h"
  56. #ifdef _INTERNAL
  57. // for occasional debugging...
  58. //#pragma optimize("", off)
  59. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  60. #endif
  61. // FORWARD DECLARATIONS ///////////////////////////////////////////////////////////////////////////
  62. enum
  63. {
  64. AS_DOZER, ///< When not actively Gathering, or when actively building, I am a dozer
  65. AS_SUPPLY_TRUCK ///< When told explicitly by player or other object, I become a supply truck
  66. };
  67. ///////////////////////////////////////////////////////////////////////////////////////////////////
  68. ///////////////////////////////////////////////////////////////////////////////////////////////////
  69. ///////////////////////////////////////////////////////////////////////////////////////////////////
  70. //-------------------------------------------------------------------------------------------------
  71. //-------------------------------------------------------------------------------------------------
  72. WorkerAIUpdate::WorkerAIUpdate( Thing *thing, const ModuleData* moduleData ) :
  73. AIUpdateInterface( thing, moduleData )
  74. {
  75. //
  76. // initialize the dozer machine to NULL, we want to do this and create it during the update
  77. // implementation because at this point we don't have the object all setup
  78. //
  79. //Added By Sadullah Nader
  80. //Initialization(s) inserted
  81. m_isRebuild = FALSE;
  82. //
  83. m_dozerMachine = NULL;
  84. for( Int i = 0; i < DOZER_NUM_TASKS; i++ )
  85. {
  86. m_task[ i ].m_targetObjectID = INVALID_ID;
  87. m_task[ i ].m_taskOrderFrame = 0;
  88. for( Int j = 0; j < DOZER_NUM_DOCK_POINTS; j++ )
  89. {
  90. m_dockPoint[ i ][ j ].valid = FALSE;
  91. m_dockPoint[ i ][ j ].location.zero();
  92. }
  93. }
  94. m_currentTask = DOZER_TASK_INVALID;
  95. m_buildSubTask = DOZER_SELECT_BUILD_DOCK_LOCATION; // irrelavant, but I want non-garbage value
  96. m_supplyTruckStateMachine = NULL;
  97. m_numberBoxes = 0;
  98. m_forcePending = FALSE;
  99. m_forcedBusyPending = FALSE;
  100. m_workerMachine = NULL;
  101. m_suppliesDepletedVoice = getWorkerAIUpdateModuleData()->m_suppliesDepletedVoice;
  102. createMachines();
  103. }
  104. //-------------------------------------------------------------------------------------------------
  105. //-------------------------------------------------------------------------------------------------
  106. WorkerAIUpdate::~WorkerAIUpdate( void )
  107. {
  108. // delete our behavior state machine
  109. if( m_dozerMachine )
  110. m_dozerMachine->deleteInstance();
  111. if( m_supplyTruckStateMachine )
  112. m_supplyTruckStateMachine->deleteInstance();
  113. if( m_workerMachine )
  114. m_workerMachine->deleteInstance();
  115. }
  116. //-------------------------------------------------------------------------------------------------
  117. Bool WorkerAIUpdate::isCurrentlyFerryingSupplies() const
  118. {
  119. if (m_supplyTruckStateMachine)
  120. {
  121. switch (m_supplyTruckStateMachine->getCurrentStateID())
  122. {
  123. case ST_IDLE:
  124. case ST_BUSY:
  125. case ST_REGROUPING:
  126. return false;
  127. case ST_WANTING:
  128. case ST_DOCKING:
  129. return true;
  130. }
  131. }
  132. return false;
  133. }
  134. //-------------------------------------------------------------------------------------------------
  135. Bool WorkerAIUpdate::isAvailableForSupplying() const
  136. {
  137. return true;
  138. }
  139. // ------------------------------------------------------------------------------------------------
  140. Real WorkerAIUpdate::getRepairHealthPerSecond( void ) const
  141. {
  142. return getWorkerAIUpdateModuleData()->m_repairHealthPercentPerSecond;
  143. }
  144. // ------------------------------------------------------------------------------------------------
  145. Real WorkerAIUpdate::getBoredTime( void ) const
  146. {
  147. return getWorkerAIUpdateModuleData()->m_boredTime;
  148. }
  149. // ------------------------------------------------------------------------------------------------
  150. Real WorkerAIUpdate::getBoredRange( void ) const
  151. {
  152. return getWorkerAIUpdateModuleData()->m_boredRange;
  153. }
  154. // ------------------------------------------------------------------------------------------------
  155. void WorkerAIUpdate::createMachines( void )
  156. {
  157. if( m_workerMachine == NULL )
  158. {
  159. m_workerMachine = newInstance(WorkerStateMachine)( getObject() );
  160. if( m_dozerMachine == NULL )
  161. {
  162. m_dozerMachine = newInstance(DozerPrimaryStateMachine)( getObject() );
  163. m_dozerMachine->initDefaultState();
  164. }
  165. if( m_supplyTruckStateMachine == NULL )
  166. {
  167. m_supplyTruckStateMachine = newInstance(SupplyTruckStateMachine)( getObject() );
  168. m_supplyTruckStateMachine->initDefaultState();
  169. }
  170. m_workerMachine->initDefaultState();// this has to wait until all three are in place since
  171. // an immediate transition check will ask questions of the machines.
  172. //#ifdef _DEBUG
  173. // m_workerMachine->setDebugOutput(TRUE);
  174. // m_dozerMachine->setDebugOutput(TRUE);
  175. // m_supplyTruckStateMachine->setDebugOutput(TRUE);
  176. //#endif
  177. }
  178. }
  179. //-------------------------------------------------------------------------------------------------
  180. //----------------------------------------------------------------------------------------
  181. UnsignedInt WorkerAIUpdate::getActionDelayForDock( Object *dock )
  182. {
  183. // Decide whether to use my Center or Warehouse delay time
  184. static const NameKeyType key_warehouseUpdate = NAMEKEY("SupplyWarehouseDockUpdate");
  185. SupplyWarehouseDockUpdate *warehouseModule = (SupplyWarehouseDockUpdate*) dock->findUpdateModule( key_warehouseUpdate );
  186. if (warehouseModule) {
  187. return getWorkerAIUpdateModuleData()->m_warehouseDelay;
  188. }
  189. static const NameKeyType key_centerUpdate = NAMEKEY("SupplyCenterDockUpdate");
  190. SupplyCenterDockUpdate *centerModule = (SupplyCenterDockUpdate*) dock->findUpdateModule( key_centerUpdate );
  191. if (centerModule) {
  192. return getWorkerAIUpdateModuleData()->m_centerDelay;
  193. }
  194. return 0;
  195. }
  196. //-------------------------------------------------------------------------------------------------
  197. //-------------------------------------------------------------------------------------------------
  198. Real WorkerAIUpdate::getWarehouseScanDistance() const
  199. {
  200. // Ai players get larger scan range. jba.
  201. if (getObject()->getControllingPlayer()->getPlayerType() == PLAYER_COMPUTER) {
  202. return 2 * getWorkerAIUpdateModuleData()->m_warehouseScanDistance;
  203. }
  204. return getWorkerAIUpdateModuleData()->m_warehouseScanDistance;
  205. }
  206. //-------------------------------------------------------------------------------------------------
  207. //-------------------------------------------------------------------------------------------------
  208. UpdateSleepTime WorkerAIUpdate::update( void )
  209. {
  210. //
  211. // NOTE: Any changes to DozerAIUpdate::* you probably want to reflect and copy into
  212. // WorkerAIUPdate:* as well ... sigh
  213. //
  214. //
  215. // now that we're really executing we have all the necessary object modules in place to
  216. // correctly create a state machine and set the default state
  217. //
  218. // create all the machines if they don't yet exist
  219. createMachines();
  220. // DO NOT set us as being to able to move with super precision off grid locations
  221. // Causes workers to get stuck. jba.
  222. //if( getCurLocomotor() )
  223. //getCurLocomotor()->setUltraAccurate( TRUE );
  224. // extend the normal AI system
  225. AIUpdateInterface::update();
  226. // do nothing if we're dead
  227. ///@todo shouldn't this be at a higher level?
  228. if( getObject()->isEffectivelyDead() )
  229. return UPDATE_SLEEP_NONE;
  230. // run our own state machine, and the appropriate sub machine
  231. m_workerMachine->updateStateMachine();
  232. if( m_workerMachine->getCurrentStateID() == AS_DOZER )
  233. {
  234. // get and validate our current task
  235. DozerTask currentTask = getCurrentTask();
  236. if( currentTask != DOZER_TASK_INVALID )
  237. {
  238. ObjectID taskTarget = getTaskTarget( currentTask );
  239. Object *targetObject = TheGameLogic->findObjectByID( taskTarget );
  240. Bool invalidTask = FALSE;
  241. // validate the task and the target
  242. if( currentTask == DOZER_TASK_REPAIR &&
  243. TheActionManager->canRepairObject( getObject(), targetObject, getLastCommandSource() ) == FALSE )
  244. invalidTask = TRUE;
  245. // cancel the task if it's now invalid
  246. if( invalidTask == TRUE )
  247. cancelTask( currentTask );
  248. } // end if
  249. // update dozer behavior
  250. m_dozerMachine->updateStateMachine();
  251. } // end if
  252. else
  253. {
  254. m_supplyTruckStateMachine->updateStateMachine();
  255. // If we are harvesting, we can be diverted to clear mines. jba.
  256. getObject()->setWeaponSetFlag(WEAPONSET_MINE_CLEARING_DETAIL);//maybe go clear some mines, if I feel like it
  257. }
  258. return UPDATE_SLEEP_NONE;
  259. }
  260. // ------------------------------------------------------------------------------------------------
  261. // ------------------------------------------------------------------------------------------------
  262. // ------------------------------------------------------------------------------------------------
  263. // ------------------------------------------------------------------------------------------------
  264. // ------------------------------------------------------------------------------------------------
  265. // ------------------------------------------------------------------------------------------------
  266. //-------------------------------------------------------------------------------------------------
  267. /** The entry point of a construct command to the Dozer */
  268. //-------------------------------------------------------------------------------------------------
  269. Object *WorkerAIUpdate::construct( const ThingTemplate *what,
  270. const Coord3D *pos,
  271. Real angle,
  272. Player *owningPlayer,
  273. Bool isRebuild )
  274. {
  275. // !!! NOTE: If you modify this you must modify the dozer too !!!
  276. // !!! Graham: Please please please have inspiration for how to *not* duplicate this code
  277. // GS - Construct needs to be an AI primitive. Inheriting off of AIUpdate means you are writing a
  278. // master brain that will call AI primitives on the object, not something that does stuff itself.
  279. // SupplyTruckAI decides who to call AIDock on. Worker should decide to AIDock or AIConstruct
  280. // or AIRepair. Dozer should just use the latter two. No construction logic should be in
  281. // the inherited AIUpdates at all.
  282. // create our machines if they don't yet exist
  283. ///@todo make 'construct' a real AI command and you won't need a special case
  284. m_isRebuild = isRebuild;
  285. createMachines();
  286. // sanity
  287. if( what == NULL || pos == NULL || owningPlayer == NULL )
  288. return NULL;
  289. // sanity
  290. DEBUG_ASSERTCRASH( getObject()->getControllingPlayer() == owningPlayer,
  291. ("Dozer::Construct - The controlling player of the Dozer is not the owning player passed in\n") );
  292. // if we're not rebuilding, we have a few checks to pass first for sanity
  293. if( isRebuild == FALSE )
  294. {
  295. // AI has weaker restriction on building
  296. Bool dozerIsAI = owningPlayer->getPlayerType() == PLAYER_COMPUTER;
  297. if( dozerIsAI )
  298. {
  299. // validate the the position to build at is valid
  300. if( TheBuildAssistant->isLocationLegalToBuild( pos, what, angle,
  301. BuildAssistant::CLEAR_PATH |
  302. BuildAssistant::NO_OBJECT_OVERLAP,
  303. getObject(), NULL ) != LBC_OK )
  304. return NULL;
  305. } // end if
  306. else
  307. {
  308. // make sure the player is capable of building this
  309. if( TheBuildAssistant->canMakeUnit( getObject(), what ) != CANMAKE_OK )
  310. return NULL;
  311. // validate the the position to build at is valid
  312. if( TheBuildAssistant->isLocationLegalToBuild( pos, what, angle,
  313. BuildAssistant::TERRAIN_RESTRICTIONS |
  314. BuildAssistant::CLEAR_PATH |
  315. BuildAssistant::NO_OBJECT_OVERLAP |
  316. BuildAssistant::SHROUD_REVEALED,
  317. getObject(), NULL ) != LBC_OK )
  318. return NULL;
  319. } // end else
  320. } // end if
  321. // what will our initial status bits
  322. UnsignedInt statusBits = OBJECT_STATUS_UNDER_CONSTRUCTION;
  323. if( isRebuild )
  324. BitSet( statusBits, OBJECT_STATUS_RECONSTRUCTING );
  325. // create an object at the destination location
  326. Object *obj = TheThingFactory->newObject( what, owningPlayer->getDefaultTeam(),
  327. (ObjectStatusBits)statusBits );
  328. // even though we haven't actually built anything yet, this keeps things tidy
  329. obj->setProducer( getObject() );
  330. obj->setBuilder( getObject() );
  331. // leave the supply truck state and now behave like a dozer.
  332. exitingSupplyTruckState();
  333. // take the required money away from the player
  334. if( isRebuild == FALSE )
  335. {
  336. Money *money = owningPlayer->getMoney();
  337. money->withdraw( what->calcCostToBuild( owningPlayer ) );
  338. } // end if
  339. //
  340. // set a bit that this object is under construction, it is important to do this early
  341. // before the hooks add/subtract power from a player are executed
  342. //
  343. obj->setStatus( OBJECT_STATUS_UNDER_CONSTRUCTION );
  344. // initialize object
  345. obj->setPosition( pos );
  346. obj->setOrientation( angle );
  347. // Flatten the terrain underneath the object, then adjust to the flattened height. jba.
  348. TheTerrainLogic->flattenTerrain(obj);
  349. Coord3D adjustedPos = *pos;
  350. adjustedPos.z = TheTerrainLogic->getGroundHeight(pos->x, pos->y);
  351. obj->setPosition(&adjustedPos);
  352. // Note - very important that we add to map AFTER we flatten terrain. jba.
  353. TheAI->pathfinder()->addObjectToPathfindMap( obj );
  354. // "callback" event for structure created (note that it's not yet "complete")
  355. owningPlayer->onStructureCreated( getObject(), obj );
  356. // set a construction percent for the new object to zero and a status for under construction
  357. obj->setConstructionPercent( 0.0 );
  358. // newly constructed objects start at one hit point
  359. BodyModuleInterface *body = obj->getBodyModule();
  360. body->internalChangeHealth( -body->getHealth() + 1.0f );
  361. // set the model action state to awaiting construction
  362. obj->clearAndSetModelConditionFlags(
  363. MAKE_MODELCONDITION_MASK2(MODELCONDITION_PARTIALLY_CONSTRUCTED, MODELCONDITION_ACTIVELY_BEING_CONSTRUCTED),
  364. MAKE_MODELCONDITION_MASK(MODELCONDITION_AWAITING_CONSTRUCTION)
  365. );
  366. // we have a construction pending
  367. newTask( DOZER_TASK_BUILD, obj );
  368. return obj;
  369. }
  370. // ------------------------------------------------------------------------------------------------
  371. /** We just exited from a supply truck task and are now idle, we should go back to Dozer idle
  372. mode */
  373. // ------------------------------------------------------------------------------------------------
  374. void WorkerAIUpdate::exitingSupplyTruckState()
  375. {
  376. if( m_workerMachine->getCurrentStateID() == AS_SUPPLY_TRUCK )
  377. {
  378. // We've been given a Dozer specific order that the Supply Truck machine doesn't recognize
  379. // as BUSY (because this command also recognizes its own busy and is likewise waiting).
  380. // Explicitly slap it upside the head.
  381. if( getObject()->getAIUpdateInterface() )
  382. {
  383. getObject()->getAIUpdateInterface()->aiIdle(CMD_FROM_AI);
  384. }
  385. m_workerMachine->setState( AS_DOZER );
  386. // To clarify, I leave supply truck mode when I notice I am doing something not supply
  387. // truck related. When given a construct command, I wait to do anything until I notice
  388. // I'm not busy. Both states are being polite, so I must force the switch.
  389. }
  390. }
  391. // ------------------------------------------------------------------------------------------------
  392. /** Given our current task and repair target, can we accept this as a new repair target */
  393. // ------------------------------------------------------------------------------------------------
  394. Bool WorkerAIUpdate::canAcceptNewRepair( Object *obj )
  395. {
  396. // sanity
  397. if( obj == NULL )
  398. return FALSE;
  399. // if we're not repairing right now, we don't have any accept restrictions
  400. if( getCurrentTask() != DOZER_TASK_REPAIR )
  401. return TRUE;
  402. // get current repair target
  403. Object *currentRepair = TheGameLogic->findObjectByID( m_task[ DOZER_TASK_REPAIR ].m_targetObjectID );
  404. if( currentRepair )
  405. {
  406. // check for same object
  407. if( currentRepair == obj )
  408. return FALSE;
  409. // check for repairing any tower on the same bridge
  410. if( currentRepair->isKindOf( KINDOF_BRIDGE_TOWER ) &&
  411. obj->isKindOf( KINDOF_BRIDGE_TOWER ) )
  412. {
  413. BridgeTowerBehaviorInterface *currentTowerInterface = NULL;
  414. BridgeTowerBehaviorInterface *newTowerInterface = NULL;
  415. currentTowerInterface = BridgeTowerBehavior::getBridgeTowerBehaviorInterfaceFromObject( currentRepair );
  416. newTowerInterface = BridgeTowerBehavior::getBridgeTowerBehaviorInterfaceFromObject( obj );
  417. // sanity
  418. if( currentTowerInterface == NULL || newTowerInterface == NULL )
  419. {
  420. DEBUG_CRASH(( "Unable to find bridge tower interface on object\n" ));
  421. return FALSE;
  422. } // end if
  423. // if they are part of the same bridge, ignore this repair command
  424. if( currentTowerInterface->getBridgeID() == newTowerInterface->getBridgeID() )
  425. return FALSE;
  426. } // end if
  427. } // end if, currentRepair object exists
  428. // all is well
  429. return TRUE;
  430. } // end canAcceptNewRepair
  431. //----------------------------------------------------------------------------------------
  432. void WorkerAIUpdate::privateIdle(CommandSourceType cmdSource)
  433. {
  434. // Leaving this commented out to show that although the regular supply truck does this, the
  435. // worker's dozer brain will get completely screwed.
  436. // If the user gives a stop command, I have to turn off autopilot
  437. // if( cmdSource == CMD_FROM_PLAYER )
  438. // setForceBusyState(TRUE);
  439. AIUpdateInterface::privateIdle(cmdSource);
  440. }
  441. //----------------------------------------------------------------------------------------
  442. void WorkerAIUpdate::privateDock( Object *dock, CommandSourceType cmdSource )
  443. {
  444. AIUpdateInterface::privateDock( dock, cmdSource );
  445. // If this is a command from a player, I will remember this as my favorite dock to override
  446. // ResourceManager searches.
  447. if ((cmdSource == CMD_FROM_PLAYER) && dock)
  448. {
  449. // Please note, there is not a separate Warehouse and Center memory by Design. Because
  450. // we lack a UI way to click Warehouse and drag to center to set up a specific path, the
  451. // practical realization has been made that you do not want separate memory.
  452. m_preferredDock = dock->getID();
  453. }
  454. }
  455. // ------------------------------------------------------------------------------------------------
  456. // ------------------------------------------------------------------------------------------------
  457. void WorkerAIUpdate::privateRepair( Object *obj, CommandSourceType cmdSource )
  458. {
  459. Object *dozer = getObject();
  460. // sanity, if we can't repair the object then get out of there
  461. if( TheActionManager->canRepairObject( dozer, obj, cmdSource ) == FALSE )
  462. return;
  463. // if we are already repairing this target do nothing
  464. if( canAcceptNewRepair( obj ) == FALSE )
  465. return;
  466. //
  467. // if this object has already been targeted for repair by an object we won't also try to
  468. // go repair it
  469. ObjectID currentRepairer = obj->getSoleHealingBenefactor();
  470. if( currentRepairer != INVALID_ID && currentRepairer != dozer->getID() )
  471. return;
  472. // start the new task
  473. newTask( DOZER_TASK_REPAIR, obj );
  474. } // end privateRepair
  475. // ------------------------------------------------------------------------------------------------
  476. /** Resume construction on a building */
  477. // ------------------------------------------------------------------------------------------------
  478. void WorkerAIUpdate::privateResumeConstruction( Object *obj, CommandSourceType cmdSource )
  479. {
  480. // sanity
  481. if( obj == NULL )
  482. return;
  483. // make sure we can resume construction on this
  484. if( TheActionManager->canResumeConstructionOf( getObject(), obj, cmdSource ) == FALSE )
  485. return;
  486. // start the new task for construction
  487. newTask( DOZER_TASK_BUILD, obj );
  488. } // end privateResumeConstruction
  489. //-------------------------------------------------------------------------------------------------
  490. /** Issue and order to the dozer */
  491. //-------------------------------------------------------------------------------------------------
  492. void WorkerAIUpdate::newTask( DozerTask task, Object* target )
  493. {
  494. // sanity
  495. DEBUG_ASSERTCRASH( task >= 0 && task < DOZER_NUM_TASKS, ("Illegal dozer task '%d'\n", task) );
  496. // sanity
  497. if( target == NULL )
  498. return;
  499. m_preferredDock = INVALID_ID; // If we are dozing, we don't want any supply truck stuff going on. jba.
  500. //
  501. // special check for the build task, we should never be given more than one of them ...
  502. // for the other tasks we just forget what we were doing and the new target takes
  503. // precedence for the task
  504. //
  505. if( task == DOZER_TASK_BUILD || task == DOZER_TASK_REPAIR )
  506. {
  507. // handle getting two tasks
  508. if( isTaskPending( task ) == TRUE )
  509. cancelTask( task );
  510. // get our object
  511. Object *me = getObject();
  512. Coord3D position;
  513. target = DozerAIUpdate::findGoodBuildOrRepairPositionAndTarget(me, target, position);
  514. if (target == NULL)
  515. return; // could happen for some bridges
  516. //
  517. // for building, we say that even "thinking" about building or rebuilding an object
  518. // sets us as the current builder of that object. this allows any dozers that are
  519. // ordered later to resume construction on something to see that somebody is already taking
  520. // care of it and then they won't be even try to resume a build since we don't allow
  521. // multiple dozers/workers to double up on construction efforts
  522. //
  523. if( task == DOZER_TASK_BUILD )
  524. target->setBuilder( me );
  525. m_dockPoint[ task ][ DOZER_DOCK_POINT_START ].valid = TRUE;
  526. m_dockPoint[ task ][ DOZER_DOCK_POINT_START ].location = position;
  527. m_dockPoint[ task ][ DOZER_DOCK_POINT_ACTION ].valid = TRUE;
  528. m_dockPoint[ task ][ DOZER_DOCK_POINT_ACTION ].location = position;
  529. m_dockPoint[ task ][ DOZER_DOCK_POINT_END ].valid = TRUE;
  530. m_dockPoint[ task ][ DOZER_DOCK_POINT_END ].location = position;
  531. } // end if, build task
  532. // set the new task target and the frame in which we got this order
  533. m_task[ task ].m_targetObjectID = target->getID();
  534. m_task[ task ].m_taskOrderFrame = TheGameLogic->getFrame();
  535. // reset the dozer behavior so that it can re-evluate which task to continue working on
  536. m_dozerMachine->resetToDefaultState();
  537. // reset the workermachine, if we've been acting like a supply truck
  538. if( m_workerMachine->getCurrentStateID() == AS_SUPPLY_TRUCK )
  539. {
  540. // We've been given a Dozer specific order that the Supply Truck machine doesn't recognize
  541. // as BUSY (because this command also recognizes its own busy and is likewise waiting).
  542. // Explicitly slap it upside the head.
  543. if( getObject()->getAIUpdateInterface() )
  544. {
  545. getObject()->getAIUpdateInterface()->aiIdle(CMD_FROM_AI);
  546. }
  547. m_workerMachine->setState( AS_DOZER );
  548. // To clarify, I leave supply truck mode when I notice I am doing something not supply
  549. // truck related. When given a construct command, I wait to do anything until I notice
  550. // I'm not busy. Both states are being polite, so I must force the switch.
  551. }
  552. }
  553. //-------------------------------------------------------------------------------------------------
  554. /** Cancel a task and reset the dozer behavior state machine so that it can
  555. * re-evaluate what it wants to do if it was working on the task being
  556. * cancelled */
  557. //-------------------------------------------------------------------------------------------------
  558. void WorkerAIUpdate::cancelTask( DozerTask task )
  559. {
  560. // clear the order
  561. internalCancelTask( task );
  562. // reset the machine to we can re-evaluate what we want to do
  563. m_dozerMachine->resetToDefaultState();
  564. }
  565. //-------------------------------------------------------------------------------------------------
  566. /** Is there a given task waiting to be done */
  567. //-------------------------------------------------------------------------------------------------
  568. Bool WorkerAIUpdate::isTaskPending( DozerTask task )
  569. {
  570. // sanity
  571. DEBUG_ASSERTCRASH( task >= 0 && task < DOZER_NUM_TASKS, ("Illegal dozer task '%d'\n", task) );
  572. return m_task[ task ].m_targetObjectID != 0 ? TRUE : FALSE;
  573. }
  574. //-------------------------------------------------------------------------------------------------
  575. /** Is there any task pending */
  576. //-------------------------------------------------------------------------------------------------
  577. Bool WorkerAIUpdate::isAnyTaskPending( void )
  578. {
  579. for( Int i = 0; i < DOZER_NUM_TASKS; i++ )
  580. if( isTaskPending( (DozerTask)i ) )
  581. return TRUE;
  582. return FALSE;
  583. }
  584. //-------------------------------------------------------------------------------------------------
  585. /** Get the target object of a given task */
  586. //-------------------------------------------------------------------------------------------------
  587. ObjectID WorkerAIUpdate::getTaskTarget( DozerTask task )
  588. {
  589. // sanity
  590. DEBUG_ASSERTCRASH( task >= 0 && task < DOZER_NUM_TASKS, ("Illegal dozer task '%d'\n", task) );
  591. return m_task[ task ].m_targetObjectID;
  592. }
  593. //-------------------------------------------------------------------------------------------------
  594. /** Set a task as successfully completed */
  595. //-------------------------------------------------------------------------------------------------
  596. void WorkerAIUpdate::internalTaskComplete( DozerTask task )
  597. {
  598. // sanity
  599. DEBUG_ASSERTCRASH( task >= 0 && task < DOZER_NUM_TASKS, ("Illegal dozer task '%d'\n", task) );
  600. // call the single method that gets called for completing and canceling tasks
  601. internalTaskCompleteOrCancelled( task );
  602. // remove the info for this task
  603. m_task[ task ].m_targetObjectID = INVALID_ID;
  604. m_task[ task ].m_taskOrderFrame = 0;
  605. // remove dock point info for this task
  606. for( Int i = 0; i < DOZER_NUM_DOCK_POINTS; i++ )
  607. m_dockPoint[ task ][ i ].valid = FALSE;
  608. }
  609. //-------------------------------------------------------------------------------------------------
  610. /** Clear a task from the Dozer for consideration, we can use this when a goal object becomes
  611. * invalid/destroyed etc. */
  612. //-------------------------------------------------------------------------------------------------
  613. void WorkerAIUpdate::internalCancelTask( DozerTask task )
  614. {
  615. // sanity
  616. DEBUG_ASSERTCRASH( task >= 0 && task < DOZER_NUM_TASKS, ("Illegal dozer task '%d'\n", task) );
  617. // call the single method that gets called for completing and canceling tasks
  618. internalTaskCompleteOrCancelled( task );
  619. // remove the info for this task
  620. m_task[ task ].m_targetObjectID = INVALID_ID;
  621. m_task[ task ].m_taskOrderFrame = 0;
  622. // remove dock point info for this task
  623. for( Int i = 0; i < DOZER_NUM_DOCK_POINTS; i++ )
  624. m_dockPoint[ task ][ i ].valid = FALSE;
  625. // stop the dozer from moving
  626. AIUpdateInterface *ai = getObject()->getAIUpdateInterface();
  627. if( !ai )
  628. {
  629. return;
  630. }
  631. /// @todo we really need a stop command instead of making it move to it's current location
  632. ai->aiMoveToPosition( getObject()->getPosition(), CMD_FROM_AI );
  633. }
  634. // ------------------------------------------------------------------------------------------------
  635. // ------------------------------------------------------------------------------------------------
  636. void WorkerAIUpdate::internalTaskCompleteOrCancelled( DozerTask task )
  637. {
  638. switch( task )
  639. {
  640. // --------------------------------------------------------------------------------------------
  641. case DOZER_TASK_INVALID:
  642. {
  643. break; // do nothing, this is really no task
  644. } // end invalid
  645. // --------------------------------------------------------------------------------------------
  646. case DOZER_TASK_BUILD:
  647. {
  648. // the builder is no longer actively building something
  649. getObject()->clearModelConditionState( MODELCONDITION_ACTIVELY_CONSTRUCTING );
  650. // And the thing we were working on is no longer being actively built
  651. ///@todo This would be correct except that we don't have idle crane animations and it is December.
  652. // Object* goalObject = TheGameLogic->findObjectByID(m_task[task].m_targetObjectID);
  653. // if (goalObject != NULL)
  654. // {
  655. // goalObject->clearModelConditionState(MODELCONDITION_ACTIVELY_BEING_CONSTRUCTED);
  656. // }
  657. break;
  658. } // end build
  659. // --------------------------------------------------------------------------------------------
  660. case DOZER_TASK_REPAIR:
  661. {
  662. Object *obj = NULL;
  663. // the builder is no longer actively repairing something
  664. getObject()->clearModelConditionState( MODELCONDITION_ACTIVELY_CONSTRUCTING );
  665. // get object to reapir (if present)
  666. obj = TheGameLogic->findObjectByID( m_task[ task ].m_targetObjectID );
  667. if( obj )
  668. {
  669. // when we're done repairing bridges, tell the scaffolding to go away
  670. if( obj->isKindOf( KINDOF_BRIDGE_TOWER ) )
  671. removeBridgeScaffolding( obj );
  672. } // end if
  673. break;
  674. } // end repair
  675. // --------------------------------------------------------------------------------------------
  676. case DOZER_TASK_FORTIFY:
  677. {
  678. break;
  679. } // end fortify
  680. // --------------------------------------------------------------------------------------------
  681. default:
  682. {
  683. DEBUG_CRASH(( "internalTaskCompleteOrCancelled: Unknown Dozer task '%d'\n", task ));
  684. break;
  685. } // end default
  686. } // end switch( task )
  687. }
  688. //-------------------------------------------------------------------------------------------------
  689. /** If we were building something, kill the active-construction flag on it */
  690. //-------------------------------------------------------------------------------------------------
  691. void WorkerAIUpdate::onDelete( void )
  692. {
  693. Int i;
  694. // cancel any of the tasks we had queued up
  695. for( i = DOZER_TASK_FIRST; i < DOZER_NUM_TASKS; ++i )
  696. {
  697. if( isTaskPending( (DozerTask)i ) )
  698. cancelTask( (DozerTask)i );
  699. } // end for i
  700. for( i = 0; i < DOZER_NUM_TASKS; i++ )
  701. {
  702. Object* goalObject = TheGameLogic->findObjectByID(m_task[i].m_targetObjectID);
  703. if (goalObject != NULL)
  704. {
  705. goalObject->clearModelConditionState(MODELCONDITION_ACTIVELY_BEING_CONSTRUCTED);
  706. }
  707. }
  708. }
  709. //-------------------------------------------------------------------------------------------------
  710. /** Get the most recently issued task */
  711. //-------------------------------------------------------------------------------------------------
  712. DozerTask WorkerAIUpdate::getMostRecentCommand( void )
  713. {
  714. Int i;
  715. DozerTask mostRecentTask = DOZER_TASK_INVALID;
  716. UnsignedInt mostRecentFrame = 0;
  717. for( i = 0; i < DOZER_NUM_TASKS; i++ )
  718. {
  719. if( isTaskPending( (DozerTask)i ) )
  720. {
  721. if( m_task[ i ].m_taskOrderFrame > mostRecentFrame )
  722. {
  723. mostRecentTask = (DozerTask)i;
  724. mostRecentFrame = m_task[ i ].m_taskOrderFrame;
  725. }
  726. }
  727. }
  728. return mostRecentTask;
  729. }
  730. // ------------------------------------------------------------------------------------------------
  731. // ------------------------------------------------------------------------------------------------
  732. const Coord3D* WorkerAIUpdate::getDockPoint( DozerTask task, DozerDockPoint point )
  733. {
  734. // sanity
  735. if( task < 0 || task >= DOZER_NUM_TASKS )
  736. return NULL;
  737. // sanity
  738. if( point < 0 || point >= DOZER_NUM_DOCK_POINTS )
  739. return NULL;
  740. // if the point has been set (is valid) then return it
  741. if( m_dockPoint[ task ][ point ].valid )
  742. return &m_dockPoint[ task ][ point ].location;
  743. // no valid point has been set for this dock point on this task
  744. return NULL;
  745. } // end getDockPoint
  746. // ------------------------------------------------------------------------------------------------
  747. // ------------------------------------------------------------------------------------------------
  748. // ------------------------------------------------------------------------------------------------
  749. // ------------------------------------------------------------------------------------------------
  750. // ------------------------------------------------------------------------------------------------
  751. // ------------------------------------------------------------------------------------------------
  752. //-------------------------------------------------------------------------------------------------
  753. void WorkerAIUpdate::aiDoCommand(const AICommandParms* parms)
  754. {
  755. //
  756. // anytime we get a command, just remove any model condition that has us actively building
  757. // if we need to show that, that bit will be set anyway again during the build process
  758. //
  759. getObject()->clearModelConditionState( MODELCONDITION_ACTIVELY_CONSTRUCTING );
  760. if (!isAllowedToRespondToAiCommands(parms))
  761. return;
  762. // create our machines if they don't yet exist
  763. createMachines();
  764. switch( parms->m_cmd )
  765. {
  766. // --------------------------------------------------------------------------------------------
  767. case AICMD_REPAIR:
  768. {
  769. // if we have no task right now, go idle so we can immediately respond to this
  770. if( getCurrentTask() == DOZER_TASK_INVALID )
  771. aiIdle( CMD_FROM_AI );
  772. // do the repair
  773. privateRepair(parms->m_obj, parms->m_cmdSource);
  774. break;
  775. } // end repair
  776. // --------------------------------------------------------------------------------------------
  777. case AICMD_RESUME_CONSTRUCTION:
  778. {
  779. // if we have no task right now, go idle so we can immediately respond to this
  780. if( getCurrentTask() == DOZER_TASK_INVALID )
  781. aiIdle( CMD_FROM_AI );
  782. // do the command
  783. privateResumeConstruction( parms->m_obj, parms->m_cmdSource );
  784. break;
  785. } // end resume construction
  786. // --------------------------------------------------------------------------------------------
  787. default:
  788. {
  789. // if this is from the player, cancel our current task
  790. if( parms->m_cmdSource == CMD_FROM_PLAYER && getCurrentTask() != DOZER_TASK_INVALID )
  791. cancelTask( getCurrentTask() );
  792. // issue the command
  793. AIUpdateInterface::aiDoCommand(parms);
  794. // when a player issues commands, this will cause the dozer to re-evaluate what it's doing
  795. if( parms->m_cmdSource == CMD_FROM_PLAYER )
  796. m_dozerMachine->resetToDefaultState();
  797. break;
  798. } // end default
  799. } // end switch
  800. if (isClearingMines() && m_numberBoxes > 0 )
  801. {
  802. // if clearing mines, we drop any boxes we were carrying
  803. m_numberBoxes = 0;
  804. Drawable *draw = getObject()->getDrawable();
  805. if( draw )
  806. {
  807. draw->updateDrawableSupplyStatus( getWorkerAIUpdateModuleData()->m_maxBoxesData, m_numberBoxes );
  808. }
  809. }
  810. }
  811. // ------------------------------------------------------------------------------------------------
  812. // ------------------------------------------------------------------------------------------------
  813. // ------------------------------------------------------------------------------------------------
  814. // ------------------------------------------------------------------------------------------------
  815. // ------------------------------------------------------------------------------------------------
  816. // ------------------------------------------------------------------------------------------------
  817. // SupplyTruck stuff
  818. // ------------------------------------------------------------------------------------------------
  819. // ------------------------------------------------------------------------------------------------
  820. Bool WorkerAIUpdate::loseOneBox()
  821. {
  822. if( m_numberBoxes == 0 )
  823. return FALSE;
  824. m_numberBoxes--;
  825. Drawable *draw = getObject()->getDrawable();
  826. if( draw )
  827. {
  828. draw->updateDrawableSupplyStatus( getWorkerAIUpdateModuleData()->m_maxBoxesData, m_numberBoxes );
  829. }
  830. return TRUE;
  831. }
  832. // ------------------------------------------------------------------------------------------------
  833. // ------------------------------------------------------------------------------------------------
  834. Bool WorkerAIUpdate::gainOneBox( Int remainingStock )
  835. {
  836. if( getWorkerAIUpdateModuleData() && m_numberBoxes >= getWorkerAIUpdateModuleData()->m_maxBoxesData )
  837. return FALSE;
  838. ++m_numberBoxes;
  839. //if I just took the last box,
  840. //i will announce that this supply source is now empty
  841. if (remainingStock == 0)
  842. {
  843. Object* bestWarehouse = getObject()->getControllingPlayer()->getResourceGatheringManager()->findBestSupplyWarehouse( getObject() );
  844. Bool playDepleted = FALSE;
  845. if ( bestWarehouse )
  846. {
  847. //figure out whether the best one is considerably far from the previous one (current position)
  848. Coord3D delta = *getObject()->getPosition();
  849. delta.sub( bestWarehouse->getPosition() );
  850. if ( delta.length() > getWarehouseScanDistance()/4)
  851. playDepleted = TRUE;
  852. }
  853. else
  854. playDepleted = TRUE;
  855. if (playDepleted && m_suppliesDepletedVoice.getEventName().isEmpty() == false)
  856. {
  857. m_suppliesDepletedVoice.setObjectID(getObject()->getID());
  858. m_suppliesDepletedVoice.setPlayingHandle(TheAudio->addAudioEvent(&m_suppliesDepletedVoice));
  859. }
  860. }
  861. Drawable *draw = getObject()->getDrawable();
  862. if( draw )
  863. {
  864. draw->updateDrawableSupplyStatus( getWorkerAIUpdateModuleData()->m_maxBoxesData, m_numberBoxes );
  865. }
  866. return TRUE;
  867. }
  868. // ------------------------------------------------------------------------------------------------
  869. // ------------------------------------------------------------------------------------------------
  870. Bool WorkerAIUpdate::isSupplyTruckBrainActiveAndBusy()
  871. {
  872. return (m_workerMachine->getCurrentStateID() == AS_SUPPLY_TRUCK)
  873. && (m_supplyTruckStateMachine->getCurrentStateID() == ST_BUSY);
  874. }
  875. // ------------------------------------------------------------------------------------------------
  876. // ------------------------------------------------------------------------------------------------
  877. void WorkerAIUpdate::resetSupplyTruckBrain()
  878. {
  879. m_supplyTruckStateMachine->resetToDefaultState();
  880. }
  881. // ------------------------------------------------------------------------------------------------
  882. // ------------------------------------------------------------------------------------------------
  883. void WorkerAIUpdate::resetDozerBrain()
  884. {
  885. m_dozerMachine->resetToDefaultState();
  886. }
  887. // ------------------------------------------------------------------------------------------------
  888. // ------------------------------------------------------------------------------------------------
  889. // ------------------------------------------------------------------------------------------------
  890. // ------------------------------------------------------------------------------------------------
  891. // ------------------------------------------------------------------------------------------------
  892. // ------------------------------------------------------------------------------------------------
  893. // Worker's master state machine, that controls Dozerness or Supplytruckness
  894. class ActAsDozerState : public State
  895. {
  896. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(ActAsDozerState, "ActAsDozerState")
  897. protected:
  898. // snapshot interface STUBBED.
  899. virtual void crc( Xfer *xfer ){};
  900. virtual void xfer( Xfer *xfer ){};
  901. virtual void loadPostProcess(){};
  902. public:
  903. ActAsDozerState( StateMachine *machine ) :State( machine, "ActAsDozerState" ){}
  904. virtual StateReturnType onEnter();
  905. virtual StateReturnType update();
  906. virtual StateReturnType onExit();
  907. };
  908. EMPTY_DTOR(ActAsDozerState)
  909. // ------------------------------------------------------------------------------------------------
  910. // ------------------------------------------------------------------------------------------------
  911. class ActAsSupplyTruckState : public State
  912. {
  913. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(ActAsSupplyTruckState, "ActAsSupplyTruckState")
  914. protected:
  915. // snapshot interface STUBBED.
  916. virtual void crc( Xfer *xfer ){};
  917. virtual void xfer( Xfer *xfer ){};
  918. virtual void loadPostProcess(){};
  919. public:
  920. ActAsSupplyTruckState( StateMachine *machine ) :State( machine, "ActAsSupplyTruckState" ){}
  921. virtual StateReturnType onEnter();
  922. virtual StateReturnType update();
  923. virtual StateReturnType onExit();
  924. };
  925. EMPTY_DTOR(ActAsSupplyTruckState)
  926. // ------------------------------------------------------------------------------------------------
  927. // ------------------------------------------------------------------------------------------------
  928. WorkerStateMachine::WorkerStateMachine( Object *owner ) : StateMachine( owner, "WorkerStateMachine" )
  929. {
  930. static const StateConditionInfo asDozerConditions[] =
  931. {
  932. StateConditionInfo(supplyTruckSubMachineWantsToEnter, AS_SUPPLY_TRUCK, NULL),
  933. StateConditionInfo(NULL, NULL, NULL) // keep last
  934. };
  935. static const StateConditionInfo asTruckConditions[] =
  936. {
  937. StateConditionInfo(supplyTruckSubMachineReadyToLeave, AS_DOZER, NULL),
  938. StateConditionInfo(NULL, NULL, NULL) // keep last
  939. };
  940. // order matters: first state is the default state.
  941. defineState( AS_DOZER, newInstance(ActAsDozerState)( this ), INVALID_STATE_ID, INVALID_STATE_ID, asDozerConditions );
  942. defineState( AS_SUPPLY_TRUCK, newInstance(ActAsSupplyTruckState)( this ), INVALID_STATE_ID, INVALID_STATE_ID, asTruckConditions );
  943. }
  944. // ------------------------------------------------------------------------------------------------
  945. // ------------------------------------------------------------------------------------------------
  946. WorkerStateMachine::~WorkerStateMachine()
  947. {
  948. }
  949. // ------------------------------------------------------------------------------------------------
  950. /** CRC */
  951. // ------------------------------------------------------------------------------------------------
  952. void WorkerStateMachine::crc( Xfer *xfer )
  953. {
  954. StateMachine::crc(xfer);
  955. } // end crc
  956. // ------------------------------------------------------------------------------------------------
  957. /** Xfer Method */
  958. // ------------------------------------------------------------------------------------------------
  959. void WorkerStateMachine::xfer( Xfer *xfer )
  960. {
  961. XferVersion cv = 1;
  962. XferVersion v = cv;
  963. xfer->xferVersion( &v, cv );
  964. StateMachine::xfer(xfer);
  965. } // end xfer
  966. // ------------------------------------------------------------------------------------------------
  967. /** Load post process */
  968. // ------------------------------------------------------------------------------------------------
  969. void WorkerStateMachine::loadPostProcess( void )
  970. {
  971. StateMachine::loadPostProcess();
  972. } // end loadPostProcess
  973. // ------------------------------------------------------------------------------------------------
  974. // ------------------------------------------------------------------------------------------------
  975. Bool WorkerStateMachine::supplyTruckSubMachineWantsToEnter( State *thisState, void* userData )
  976. {
  977. Object *owner = thisState->getMachineOwner();
  978. WorkerAIUpdate *update = (WorkerAIUpdate*)owner->getAIUpdateInterface();
  979. if( !update )
  980. {
  981. return false;
  982. }
  983. AIStateType masterState = update->getAIStateType();
  984. //If I detect a Supply force message, or if I have been put straight in dock,
  985. //then the worker master part of me wants to switch to the Supply sub-brain
  986. return update->isForcedIntoWantingState() || (masterState == AI_DOCK);
  987. }
  988. // ------------------------------------------------------------------------------------------------
  989. // ------------------------------------------------------------------------------------------------
  990. Bool WorkerStateMachine::supplyTruckSubMachineReadyToLeave( State *thisState, void* userData )
  991. {
  992. Object *owner = thisState->getMachineOwner();
  993. WorkerAIUpdate *update = (WorkerAIUpdate*)owner->getAIUpdateInterface();
  994. if( !update )
  995. {
  996. return false;
  997. }
  998. // AIStateType masterState = update->getAIStateType();
  999. // It isn't ready to leave if it is on its way in. The first clause
  1000. // allow a latch for a moment
  1001. // so there is no transition out on the way in. Active and Busy means it isn't doing
  1002. // anything Supply related.
  1003. return !supplyTruckSubMachineWantsToEnter( thisState, NULL )
  1004. && update->isSupplyTruckBrainActiveAndBusy();
  1005. }
  1006. // ------------------------------------------------------------------------------------------------
  1007. // ------------------------------------------------------------------------------------------------
  1008. StateReturnType ActAsDozerState::onEnter()
  1009. {
  1010. return STATE_CONTINUE;
  1011. }
  1012. // ------------------------------------------------------------------------------------------------
  1013. // ------------------------------------------------------------------------------------------------
  1014. StateReturnType ActAsDozerState::update()
  1015. {
  1016. return STATE_CONTINUE;
  1017. }
  1018. // ------------------------------------------------------------------------------------------------
  1019. // ------------------------------------------------------------------------------------------------
  1020. StateReturnType ActAsDozerState::onExit()
  1021. {
  1022. Object *owner = getMachineOwner();
  1023. WorkerAIUpdate *update = (WorkerAIUpdate*)owner->getAIUpdateInterface();
  1024. if( !update )
  1025. {
  1026. return STATE_FAILURE;
  1027. }
  1028. update->resetDozerBrain();
  1029. return STATE_CONTINUE;
  1030. }
  1031. // ------------------------------------------------------------------------------------------------
  1032. // ------------------------------------------------------------------------------------------------
  1033. StateReturnType ActAsSupplyTruckState::onEnter()
  1034. {
  1035. return STATE_CONTINUE;
  1036. }
  1037. // ------------------------------------------------------------------------------------------------
  1038. // ------------------------------------------------------------------------------------------------
  1039. StateReturnType ActAsSupplyTruckState::update()
  1040. {
  1041. return STATE_CONTINUE;
  1042. }
  1043. // ------------------------------------------------------------------------------------------------
  1044. // ------------------------------------------------------------------------------------------------
  1045. StateReturnType ActAsSupplyTruckState::onExit()
  1046. {
  1047. Object *owner = getMachineOwner();
  1048. WorkerAIUpdate *update = (WorkerAIUpdate*)owner->getAIUpdateInterface();
  1049. if( !update )
  1050. {
  1051. return STATE_FAILURE;
  1052. }
  1053. update->resetSupplyTruckBrain();
  1054. return STATE_CONTINUE;
  1055. }
  1056. // ------------------------------------------------------------------------------------------------
  1057. /** Create the bridge scaffolding if necessary for the bridge that is attached to this tower */
  1058. // ------------------------------------------------------------------------------------------------
  1059. void WorkerAIUpdate::createBridgeScaffolding( Object *bridgeTower )
  1060. {
  1061. // sanity
  1062. if( bridgeTower == NULL )
  1063. return;
  1064. // get the bridge behavior interface from the bridge object that this tower is a part of
  1065. BridgeTowerBehaviorInterface *btbi = BridgeTowerBehavior::getBridgeTowerBehaviorInterfaceFromObject( bridgeTower );
  1066. if( btbi == NULL )
  1067. return;
  1068. Object *bridgeObject = TheGameLogic->findObjectByID( btbi->getBridgeID() );
  1069. if( bridgeObject == NULL )
  1070. return;
  1071. BridgeBehaviorInterface *bbi = BridgeBehavior::getBridgeBehaviorInterfaceFromObject( bridgeObject );
  1072. if( bbi == NULL )
  1073. return;
  1074. // tell the bridge to create scaffolding if necessary
  1075. bbi->createScaffolding();
  1076. } // end createBridgeScaffolding
  1077. // ------------------------------------------------------------------------------------------------
  1078. /** Remove the bridge scaffolding from the bridge object that is attached to this tower */
  1079. // ------------------------------------------------------------------------------------------------
  1080. void WorkerAIUpdate::removeBridgeScaffolding( Object *bridgeTower )
  1081. {
  1082. // sanity
  1083. if( bridgeTower == NULL )
  1084. return;
  1085. // get the bridge behavior interface from the bridge object that this tower is a part of
  1086. BridgeTowerBehaviorInterface *btbi = BridgeTowerBehavior::getBridgeTowerBehaviorInterfaceFromObject( bridgeTower );
  1087. if( btbi == NULL )
  1088. return;
  1089. Object *bridgeObject = TheGameLogic->findObjectByID( btbi->getBridgeID() );
  1090. if( bridgeObject == NULL )
  1091. return;
  1092. BridgeBehaviorInterface *bbi = BridgeBehavior::getBridgeBehaviorInterfaceFromObject( bridgeObject );
  1093. if( bbi == NULL )
  1094. return;
  1095. // tell the bridge to end any scaffolding from repairing
  1096. bbi->removeScaffolding();
  1097. } // end removeBridgeScaffolding
  1098. //------------------------------------------------------------------------------------------------
  1099. void WorkerAIUpdate::startBuildingSound( const AudioEventRTS *sound, ObjectID constructionSiteID )
  1100. {
  1101. m_buildingSound = *sound;
  1102. m_buildingSound.setObjectID( constructionSiteID );
  1103. m_buildingSound.setPlayingHandle( TheAudio->addAudioEvent( &m_buildingSound ) );
  1104. }
  1105. //------------------------------------------------------------------------------------------------
  1106. void WorkerAIUpdate::finishBuildingSound()
  1107. {
  1108. TheAudio->removeAudioEvent( m_buildingSound.getPlayingHandle() );
  1109. }
  1110. // ------------------------------------------------------------------------------------------------
  1111. /** CRC */
  1112. // ------------------------------------------------------------------------------------------------
  1113. void WorkerAIUpdate::crc( Xfer *xfer )
  1114. {
  1115. // extend base class
  1116. AIUpdateInterface::crc(xfer);
  1117. } // end crc
  1118. // ------------------------------------------------------------------------------------------------
  1119. /** Xfer method
  1120. * Version Info:
  1121. * 1: Initial version */
  1122. // ------------------------------------------------------------------------------------------------
  1123. void WorkerAIUpdate::xfer( Xfer *xfer )
  1124. {
  1125. XferVersion currentVersion = 1;
  1126. XferVersion version = currentVersion;
  1127. xfer->xferVersion( &version, currentVersion );
  1128. // extend base class
  1129. AIUpdateInterface::xfer(xfer);
  1130. //------------------------- xfer Dozer info
  1131. Int numTasks = DOZER_NUM_TASKS;
  1132. xfer->xferInt(&numTasks);
  1133. if (numTasks != DOZER_NUM_TASKS) {
  1134. DEBUG_CRASH(("DOZER_NUM_TASKS changed unexpectedly."));
  1135. throw SC_INVALID_DATA;
  1136. }
  1137. Int i, j;
  1138. for (i=0; i<DOZER_NUM_TASKS; i++) {
  1139. xfer->xferObjectID(&m_task[i].m_targetObjectID);
  1140. xfer->xferUnsignedInt(&m_task[i].m_taskOrderFrame);
  1141. }
  1142. xfer->xferSnapshot(m_dozerMachine);
  1143. xfer->xferUser(&m_currentTask, sizeof(m_currentTask));
  1144. Int dockPoints = DOZER_NUM_DOCK_POINTS;
  1145. xfer->xferInt(&dockPoints);
  1146. if (dockPoints!=DOZER_NUM_DOCK_POINTS) {
  1147. DEBUG_CRASH(("DOZER_NUM_DOCK_POINTS changed unexpectedly."));
  1148. throw SC_INVALID_DATA;
  1149. }
  1150. for (i=0; i<DOZER_NUM_TASKS; i++) {
  1151. for (j=0; j<DOZER_NUM_DOCK_POINTS; j++) {
  1152. xfer->xferBool(&m_dockPoint[i][j].valid);
  1153. xfer->xferCoord3D(&m_dockPoint[i][j].location);
  1154. }
  1155. }
  1156. xfer->xferUser(&m_buildSubTask, sizeof(m_buildSubTask));
  1157. //------------------------- xfer Supply Truck info
  1158. xfer->xferSnapshot(m_supplyTruckStateMachine);
  1159. xfer->xferObjectID(&m_preferredDock);
  1160. xfer->xferInt(&m_numberBoxes);
  1161. xfer->xferBool(&m_forcePending);
  1162. //-------------------------- xfer Worker info
  1163. xfer->xferSnapshot(m_workerMachine);
  1164. } // end xfer
  1165. // ------------------------------------------------------------------------------------------------
  1166. /** Load post process */
  1167. // ------------------------------------------------------------------------------------------------
  1168. void WorkerAIUpdate::loadPostProcess( void )
  1169. {
  1170. // extend base class
  1171. AIUpdateInterface::loadPostProcess();
  1172. } // end loadPostProcess