repairbaygameobj.cpp 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. /*
  2. ** Command & Conquer Renegade(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. *** Confidential - Westwood Studios ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Commando *
  23. * *
  24. * $Archive:: /Commando/Code/Combat/repairbaygameobj.cpp $*
  25. * *
  26. * $Author:: Byon_g $*
  27. * *
  28. * $Modtime:: 11/15/01 6:37p $*
  29. * *
  30. * $Revision:: 11 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "repairbaygameobj.h"
  36. #include "basecontroller.h"
  37. #include "wwhack.h"
  38. #include "simpledefinitionfactory.h"
  39. #include "persistfactory.h"
  40. #include "definitionmgr.h"
  41. #include "combatchunkid.h"
  42. #include "debug.h"
  43. #include "scriptzone.h"
  44. #include "wwprofile.h"
  45. #include "basecontroller.h"
  46. #include "vehicle.h"
  47. #include "soldier.h"
  48. #include "playertype.h"
  49. #include "playerdata.h"
  50. #include "mesh.h"
  51. #include "meshmdl.h"
  52. #include "crandom.h"
  53. #include "simplegameobj.h"
  54. #include "objlibrary.h"
  55. #include "phys.h"
  56. #include "hanim.h"
  57. #include "combat.h"
  58. #include "bitpackids.h"
  59. ////////////////////////////////////////////////////////////////
  60. // Hacks
  61. ////////////////////////////////////////////////////////////////
  62. DECLARE_FORCE_LINK (RepairBay)
  63. ////////////////////////////////////////////////////////////////
  64. // Namespaces
  65. ////////////////////////////////////////////////////////////////
  66. using namespace BuildingConstants;
  67. ////////////////////////////////////////////////////////////////
  68. // Constants
  69. ////////////////////////////////////////////////////////////////
  70. const char *RepairBayGameObj::BoneNames[RepairBayGameObj::BONE_COUNT] =
  71. {
  72. "BONE02",
  73. "BONE03",
  74. "BONE04",
  75. "BONE05",
  76. "BONE06",
  77. "BONE07",
  78. };
  79. ////////////////////////////////////////////////////////////////
  80. // Editable and persist factories
  81. ////////////////////////////////////////////////////////////////
  82. SimplePersistFactoryClass <RepairBayGameObjDef, CHUNKID_GAME_OBJECT_DEF_REPAIR_BAY> _RepairBayGameObjDefPersistFactory;
  83. SimplePersistFactoryClass <RepairBayGameObj, CHUNKID_GAME_OBJECT_REPAIR_BAY> _RepairBayGameObjPersistFactory;
  84. DECLARE_DEFINITION_FACTORY (RepairBayGameObjDef, CLASSID_GAME_OBJECT_DEF_REPAIR_BAY, "Repair Bay") _RepairBayGameObjDefDefFactory;
  85. ////////////////////////////////////////////////////////////////
  86. // Save/Load constants
  87. ////////////////////////////////////////////////////////////////
  88. enum
  89. {
  90. CHUNKID_DEF_PARENT = 0x02200638,
  91. CHUNKID_DEF_VARIABLES,
  92. MICROCHUNKID_DEF_REPAIR_PER_SEC = 1,
  93. MICROCHUNKID_DEF_REPARING_STATICANIM_DEFID
  94. };
  95. enum
  96. {
  97. CHUNKID_PARENT = 0x0219043,
  98. CHUNKID_VARIABLES,
  99. MICROCHUNKID_UNUSED = 1,
  100. };
  101. ////////////////////////////////////////////////////////////////
  102. //
  103. // RepairBayGameObjDef
  104. //
  105. ////////////////////////////////////////////////////////////////
  106. RepairBayGameObjDef::RepairBayGameObjDef (void) :
  107. RepairPerSec (0),
  108. RepairingStaticAnimDefID (0)
  109. {
  110. //
  111. // Editable support
  112. //
  113. EDITABLE_PARAM (RepairBayGameObjDef, ParameterClass::TYPE_FLOAT, RepairPerSec);
  114. #ifdef PARAM_EDITING_ON
  115. GenericDefParameterClass *param = new GenericDefParameterClass (&RepairingStaticAnimDefID);
  116. param->Set_Class_ID (CLASSID_TILE);
  117. param->Set_Name ("Repairing Static Anim Type");
  118. GENERIC_EDITABLE_PARAM (RepairBayGameObjDef, param)
  119. #endif //PARAM_EDITING_ON
  120. return ;
  121. }
  122. ////////////////////////////////////////////////////////////////
  123. //
  124. // ~RepairBayGameObjDef
  125. //
  126. ////////////////////////////////////////////////////////////////
  127. RepairBayGameObjDef::~RepairBayGameObjDef (void)
  128. {
  129. return ;
  130. }
  131. ////////////////////////////////////////////////////////////////
  132. //
  133. // Get_Class_ID
  134. //
  135. ////////////////////////////////////////////////////////////////
  136. uint32
  137. RepairBayGameObjDef::Get_Class_ID (void) const
  138. {
  139. return CLASSID_GAME_OBJECT_DEF_REPAIR_BAY;
  140. }
  141. ////////////////////////////////////////////////////////////////
  142. //
  143. // Create
  144. //
  145. ////////////////////////////////////////////////////////////////
  146. PersistClass *
  147. RepairBayGameObjDef::Create (void) const
  148. {
  149. RepairBayGameObj *building = new RepairBayGameObj;
  150. building->Init (*this);
  151. return building;
  152. }
  153. ////////////////////////////////////////////////////////////////
  154. //
  155. // Save
  156. //
  157. ////////////////////////////////////////////////////////////////
  158. bool
  159. RepairBayGameObjDef::Save (ChunkSaveClass &csave)
  160. {
  161. csave.Begin_Chunk (CHUNKID_DEF_PARENT);
  162. BuildingGameObjDef::Save (csave);
  163. csave.End_Chunk ();
  164. csave.Begin_Chunk (CHUNKID_DEF_VARIABLES);
  165. WRITE_MICRO_CHUNK (csave, MICROCHUNKID_DEF_REPAIR_PER_SEC, RepairPerSec);
  166. WRITE_MICRO_CHUNK (csave, MICROCHUNKID_DEF_REPARING_STATICANIM_DEFID, RepairingStaticAnimDefID);
  167. csave.End_Chunk ();
  168. return true;
  169. }
  170. ////////////////////////////////////////////////////////////////
  171. //
  172. // Load
  173. //
  174. ////////////////////////////////////////////////////////////////
  175. bool
  176. RepairBayGameObjDef::Load (ChunkLoadClass &cload)
  177. {
  178. while (cload.Open_Chunk ())
  179. {
  180. switch (cload.Cur_Chunk_ID ())
  181. {
  182. case CHUNKID_DEF_PARENT:
  183. BuildingGameObjDef::Load (cload);
  184. break;
  185. case CHUNKID_DEF_VARIABLES:
  186. Load_Variables (cload);
  187. break;
  188. default:
  189. Debug_Say (("Unrecognized RepairBay Def chunkID\n"));
  190. break;
  191. }
  192. cload.Close_Chunk ();
  193. }
  194. return true;
  195. }
  196. ////////////////////////////////////////////////////////////////
  197. //
  198. // Load_Variables
  199. //
  200. ////////////////////////////////////////////////////////////////
  201. void
  202. RepairBayGameObjDef::Load_Variables (ChunkLoadClass &cload)
  203. {
  204. while (cload.Open_Micro_Chunk ()) {
  205. switch (cload.Cur_Micro_Chunk_ID ())
  206. {
  207. READ_MICRO_CHUNK (cload, MICROCHUNKID_DEF_REPAIR_PER_SEC, RepairPerSec);
  208. READ_MICRO_CHUNK (cload, MICROCHUNKID_DEF_REPARING_STATICANIM_DEFID, RepairingStaticAnimDefID);
  209. default:
  210. Debug_Say (("Unrecognized RepairBay Def Variable chunkID\n"));
  211. break;
  212. }
  213. cload.Close_Micro_Chunk();
  214. }
  215. return ;
  216. }
  217. ////////////////////////////////////////////////////////////////
  218. //
  219. // Get_Factory
  220. //
  221. ////////////////////////////////////////////////////////////////
  222. const PersistFactoryClass &
  223. RepairBayGameObjDef::Get_Factory (void) const
  224. {
  225. return _RepairBayGameObjDefPersistFactory;
  226. }
  227. ////////////////////////////////////////////////////////////////
  228. //
  229. // RepairBayGameObj
  230. //
  231. ////////////////////////////////////////////////////////////////
  232. RepairBayGameObj::RepairBayGameObj (void) :
  233. RepairTimer (0),
  234. RepairZone (Vector3 (0, 0, 0), Vector3 (0, 0, 0)),
  235. RepairAnimationID (0),
  236. IsReparing (false),
  237. RepairMesh (NULL)
  238. {
  239. ::memset (ArcObjects, 0, sizeof (ArcObjects));
  240. for (int index = 0; index < ARC_OBJ_COUNT; index ++) {
  241. ArcLifeRemaining[index] = 0;
  242. }
  243. return ;
  244. }
  245. ////////////////////////////////////////////////////////////////
  246. //
  247. // ~RepairBayGameObj
  248. //
  249. ////////////////////////////////////////////////////////////////
  250. RepairBayGameObj::~RepairBayGameObj (void)
  251. {
  252. for (int index = 0; index < ARC_OBJ_COUNT; index ++) {
  253. ArcObjects[index] = NULL;
  254. }
  255. return ;
  256. }
  257. ////////////////////////////////////////////////////////////////
  258. //
  259. // Get_Factory
  260. //
  261. ////////////////////////////////////////////////////////////////
  262. const PersistFactoryClass &
  263. RepairBayGameObj::Get_Factory (void) const
  264. {
  265. return _RepairBayGameObjPersistFactory;
  266. }
  267. ////////////////////////////////////////////////////////////////
  268. //
  269. // Init
  270. //
  271. ////////////////////////////////////////////////////////////////
  272. void RepairBayGameObj::Init( void )
  273. {
  274. Init( Get_Definition() );
  275. }
  276. ////////////////////////////////////////////////////////////////
  277. //
  278. // Init
  279. //
  280. ////////////////////////////////////////////////////////////////
  281. void
  282. RepairBayGameObj::Init (const RepairBayGameObjDef &definition)
  283. {
  284. BuildingGameObj::Init (definition);
  285. return ;
  286. }
  287. ////////////////////////////////////////////////////////////////
  288. //
  289. // Get_Definition
  290. //
  291. ////////////////////////////////////////////////////////////////
  292. const RepairBayGameObjDef &
  293. RepairBayGameObj::Get_Definition (void) const
  294. {
  295. return (const RepairBayGameObjDef &)BaseGameObj::Get_Definition ();
  296. }
  297. ////////////////////////////////////////////////////////////////
  298. //
  299. // Save
  300. //
  301. ////////////////////////////////////////////////////////////////
  302. bool
  303. RepairBayGameObj::Save (ChunkSaveClass &csave)
  304. {
  305. csave.Begin_Chunk (CHUNKID_PARENT);
  306. BuildingGameObj::Save (csave);
  307. csave.End_Chunk ();
  308. csave.Begin_Chunk (CHUNKID_VARIABLES);
  309. csave.End_Chunk ();
  310. return true;
  311. }
  312. ////////////////////////////////////////////////////////////////
  313. //
  314. // Load
  315. //
  316. ////////////////////////////////////////////////////////////////
  317. bool
  318. RepairBayGameObj::Load (ChunkLoadClass &cload)
  319. {
  320. while (cload.Open_Chunk ()) {
  321. switch (cload.Cur_Chunk_ID ()) {
  322. case CHUNKID_PARENT:
  323. BuildingGameObj::Load (cload);
  324. break;
  325. case CHUNKID_VARIABLES:
  326. Load_Variables (cload);
  327. break;
  328. default:
  329. Debug_Say (("Unrecognized RepairBay chunkID\n"));
  330. break;
  331. }
  332. cload.Close_Chunk();
  333. }
  334. return true;
  335. }
  336. ////////////////////////////////////////////////////////////////
  337. //
  338. // Load_Variables
  339. //
  340. ////////////////////////////////////////////////////////////////
  341. void
  342. RepairBayGameObj::Load_Variables (ChunkLoadClass &cload)
  343. {
  344. while (cload.Open_Micro_Chunk ()) {
  345. /*switch (cload.Cur_Micro_Chunk_ID ())
  346. {
  347. default:
  348. Debug_Say (("Unrecognized RepairBay Variable chunkID\n"));
  349. break;
  350. }*/
  351. cload.Close_Micro_Chunk();
  352. }
  353. return ;
  354. }
  355. ////////////////////////////////////////////////////////////////
  356. //
  357. // CnC_Initialize
  358. //
  359. ////////////////////////////////////////////////////////////////
  360. void
  361. RepairBayGameObj::CnC_Initialize (BaseControllerClass *base)
  362. {
  363. BuildingGameObj::CnC_Initialize (base);
  364. //
  365. // Get the building's "position"
  366. //
  367. Vector3 pos;
  368. Get_Position (&pos);
  369. //
  370. // Find the closest repair bay zone to the building
  371. //
  372. ScriptZoneGameObj *zone = NULL;
  373. zone = ScriptZoneGameObj::Find_Closest_Zone (pos, ZoneConstants::TYPE_VEHICLE_REPAIR);
  374. if (zone != NULL) {
  375. RepairZone = zone->Get_Bounding_Box ();
  376. //
  377. // Get rid of the zone (if the game doesn't need it anymore)
  378. //
  379. if (zone->Get_Observers ().Count () == 0) {
  380. zone->Set_Delete_Pending ();
  381. }
  382. }
  383. //
  384. // Try to find the mesh that contains the
  385. //
  386. RefMultiListIterator<BuildingAggregateClass> mesh_iterator (&Aggregates);
  387. for (mesh_iterator.First (); !mesh_iterator.Is_Done (); mesh_iterator.Next ()) {
  388. StaticPhysClass *phys_obj = mesh_iterator.Peek_Obj ();
  389. if (phys_obj != NULL && phys_obj->Peek_Model () != NULL) {
  390. if (::lstrcmpi (phys_obj->Peek_Model ()->Get_Name (), "rep^NOD_fx") == 0) {
  391. RepairMesh = phys_obj;
  392. break;
  393. }
  394. }
  395. }
  396. //
  397. // Find the closest reparing static anim phys
  398. //
  399. float closest2 = 99999.0F;
  400. RefPhysListIterator iterator = PhysicsSceneClass::Get_Instance()->Get_Static_Object_Iterator ();
  401. for (iterator.First (); !iterator.Is_Done (); iterator.Next ()) {
  402. StaticAnimPhysClass *anim_phys_obj = iterator.Peek_Obj ()->As_StaticAnimPhysClass ();
  403. //
  404. // Is this a repairing static anim phys?
  405. //
  406. if ( anim_phys_obj != NULL &&
  407. anim_phys_obj->Get_Definition ()->Get_ID () == (uint32)Get_Definition ().RepairingStaticAnimDefID)
  408. {
  409. //
  410. // Is this the closest one we've found so far?
  411. //
  412. Vector3 anim_pos;
  413. anim_phys_obj->Get_Position (&anim_pos);
  414. float dist2 = (anim_pos - pos).Length2 ();
  415. if (dist2 < closest2) {
  416. closest2 = dist2;
  417. RepairAnimationID = anim_phys_obj->Get_ID ();
  418. }
  419. }
  420. }
  421. //
  422. // Create the simple game object's that we'll use to display the repair-arc effect
  423. //
  424. for (int index = 0; index < ARC_OBJ_COUNT; index ++) {
  425. ArcObjects[index] = (SimpleGameObj *)ObjectLibraryManager::Create_Object ("Arc Effect");
  426. if ( ArcObjects[index]->Peek_Physical_Object () != NULL &&
  427. ArcObjects[index]->Peek_Physical_Object ()->Peek_Model () != NULL)
  428. {
  429. ArcObjects[index]->Peek_Physical_Object ()->Peek_Model ()->Set_Hidden (true);
  430. }
  431. ArcLifeRemaining[index] = 0;
  432. }
  433. //
  434. // Get information about the model
  435. //
  436. RenderObjClass *model = ArcObjects[0]->Peek_Physical_Object ()->Peek_Model ();
  437. if (model != NULL) {
  438. //
  439. // Store the original transforms of each bone
  440. //
  441. for (int index = 0; index < BONE_COUNT; index ++) {
  442. Bones[index] = model->Get_Bone_Transform (BoneNames[index]);
  443. }
  444. //
  445. // Lookup the original transform of the end-bone
  446. //
  447. int end_index = model->Get_Bone_Index ("bone_end");
  448. EndTM = model->Get_Bone_Transform (end_index);
  449. }
  450. return ;
  451. }
  452. ////////////////////////////////////////////////////////////////
  453. //
  454. // Play_Repairing_Animation
  455. //
  456. ////////////////////////////////////////////////////////////////
  457. void
  458. RepairBayGameObj::Play_Repairing_Animation (bool onoff)
  459. {
  460. //
  461. // Lookup the static animation object we need to play
  462. //
  463. StaticPhysClass *static_phys_obj = PhysicsSceneClass::Get_Instance ()->Find_Static_Object (RepairAnimationID);
  464. if (static_phys_obj != NULL) {
  465. StaticAnimPhysClass *anim_phys_obj = static_phys_obj->As_StaticAnimPhysClass ();
  466. if (anim_phys_obj != NULL) {
  467. //
  468. // Configure the animation
  469. //
  470. AnimCollisionManagerClass &anim_mgr = anim_phys_obj->Get_Animation_Manager ();
  471. anim_mgr.Set_Animation_Mode (AnimCollisionManagerClass::ANIMATE_TARGET);
  472. //
  473. // Either play the animation forward or backward
  474. //
  475. if (onoff && anim_mgr.Get_Target_Frame () == 0) {
  476. anim_mgr.Set_Target_Frame_End ();
  477. } else if (onoff == false && anim_mgr.Get_Target_Frame () != 0) {
  478. anim_mgr.Set_Target_Frame (0);
  479. }
  480. }
  481. }
  482. return ;
  483. }
  484. ////////////////////////////////////////////////////////////////
  485. //
  486. // Think
  487. //
  488. ////////////////////////////////////////////////////////////////
  489. void
  490. RepairBayGameObj::Think (void)
  491. {
  492. WWPROFILE ("Repair Bay Think");
  493. //
  494. // Check to see if we should repair the vehicle a little more...
  495. //
  496. if (IsDestroyed == false) {
  497. RepairTimer -= TimeManager::Get_Frame_Seconds ();
  498. if (RepairTimer <= 0) {
  499. RepairTimer = 0.5F;
  500. //
  501. // Repair any vehicle that is in our zone
  502. //
  503. bool is_repairing = Repair_Vehicle ();
  504. //
  505. // Play the repairing animation
  506. //
  507. if (is_repairing != IsReparing) {
  508. IsReparing = is_repairing;
  509. }
  510. }
  511. if (IsReparing) {
  512. Update_Repairing_Animations ();
  513. }
  514. }
  515. //
  516. // Hide any arc-effects that have finished
  517. //
  518. for (int index = 0; index < ARC_OBJ_COUNT; index ++) {
  519. if (ArcLifeRemaining[index] > 0) {
  520. ArcLifeRemaining[index] -= TimeManager::Get_Frame_Seconds ();
  521. if (ArcLifeRemaining[index] <= 0) {
  522. PhysClass *phys_obj = ArcObjects[index]->Peek_Physical_Object ();
  523. phys_obj->Peek_Model ()->Set_Hidden (true);
  524. }
  525. }
  526. }
  527. BuildingGameObj::Think ();
  528. return ;
  529. }
  530. ////////////////////////////////////////////////////////////////
  531. //
  532. // Repair_Vehicle
  533. //
  534. ////////////////////////////////////////////////////////////////
  535. bool
  536. RepairBayGameObj::Repair_Vehicle (void)
  537. {
  538. bool is_repairing = false;
  539. VehicleList.Delete_All ();
  540. //
  541. // Collect the dynamic physics objects in the repair zone
  542. //
  543. NonRefPhysListClass objs_to_repair;
  544. PhysicsSceneClass::Get_Instance ()->Collect_Objects (RepairZone, false, true, &objs_to_repair);
  545. //
  546. // Loop over all the objects
  547. //
  548. NonRefPhysListIterator it (&objs_to_repair);
  549. for (it.First (); !it.Is_Done (); it.Next ()) {
  550. PhysicalGameObj *gameobj = NULL;
  551. //
  552. // Get the game object from this physics object
  553. //
  554. if (it.Peek_Obj()->Get_Observer () != NULL) {
  555. gameobj = ((CombatPhysObserverClass *)it.Peek_Obj()->Get_Observer())->As_PhysicalGameObj();
  556. }
  557. if (gameobj != NULL) {
  558. //
  559. // See if this is a vehicle game object
  560. //
  561. VehicleGameObj *vehicle = gameobj->As_VehicleGameObj ();
  562. if (vehicle != NULL) {
  563. //
  564. // Check to ensure the driver is of the right team
  565. //
  566. SoldierGameObj *driver = vehicle->Get_Driver ();
  567. if ( driver != NULL &&
  568. driver->Get_Player_Type () == BaseController->Get_Player_Type() )
  569. {
  570. PlayerDataClass *player_data = driver->Get_Player_Data ();
  571. float shield_max = vehicle->Get_Defense_Object ()->Get_Shield_Strength_Max ();
  572. float health_max = vehicle->Get_Defense_Object ()->Get_Health_Max ();
  573. float curr_health = vehicle->Get_Defense_Object ()->Get_Health ();
  574. float curr_shield = vehicle->Get_Defense_Object ()->Get_Shield_Strength ();
  575. //
  576. // Avoid divide-by-zero
  577. //
  578. health_max = max (health_max, WWMATH_EPSILON);
  579. shield_max = max (shield_max, WWMATH_EPSILON);
  580. //
  581. // Calculate how much health and shield to restore and how much it will cost
  582. //
  583. float vehicle_cost = 1000.0F;//vehicle->Get_Cost ();
  584. float repair_rate = (Get_Definition ().RepairPerSec * 0.5F);
  585. float total_damage = (health_max - curr_health) + (shield_max - curr_shield);
  586. int damage_points = (int)min (repair_rate, total_damage);
  587. float repair_cost_per_pt = vehicle_cost / (health_max + shield_max);
  588. //int available_funds = 1000; //player_data->Get_Score ()
  589. #pragma message("(TSS) available_funds = 1000 looks suspect...")
  590. int available_funds = 1000; //player_data->Get_Money ()
  591. int points_restored = int(available_funds / repair_cost_per_pt);
  592. points_restored = max (points_restored, 0);
  593. points_restored = min (points_restored, damage_points);
  594. float total_cost = points_restored * repair_cost_per_pt;
  595. int health_restored = min (int(health_max - curr_health), points_restored);
  596. int shield_restored = min (int(shield_max - curr_shield), (points_restored - health_restored));
  597. //
  598. // Debit the player's account
  599. //
  600. //player_data->Purchase_Item (total_cost);
  601. is_repairing = true;
  602. //
  603. // Repair the vehicle's health if the player has enough money
  604. //
  605. if (health_restored > 0) {
  606. if (CombatManager::I_Am_Server ()) {
  607. vehicle->Get_Defense_Object ()->Add_Health (health_restored);
  608. }
  609. is_repairing = true;
  610. }
  611. //
  612. // Repair the vehicle's shield if the player has enough money
  613. //
  614. if (shield_restored > 0) {
  615. if (CombatManager::I_Am_Server ()) {
  616. vehicle->Get_Defense_Object ()->Add_Shield_Strength (shield_restored);
  617. }
  618. is_repairing = true;
  619. }
  620. //
  621. // Add this vehicle to the list of vehicles that are being repaired
  622. //
  623. if (is_repairing) {
  624. VehicleList.Add (vehicle);
  625. }
  626. }
  627. }
  628. }
  629. }
  630. return is_repairing;
  631. }
  632. ////////////////////////////////////////////////////////////////
  633. //
  634. // Find_Random_Mesh
  635. //
  636. ////////////////////////////////////////////////////////////////
  637. MeshClass *
  638. Find_Random_Mesh (RenderObjClass *model)
  639. {
  640. MeshClass *mesh = NULL;
  641. //
  642. // Is this model a mesh?
  643. //
  644. if (mesh == NULL && model->Class_ID () == RenderObjClass::CLASSID_MESH) {
  645. mesh = reinterpret_cast<MeshClass *> (model);
  646. }
  647. //
  648. // Pick a random start index in the list
  649. //
  650. int count = model->Get_Num_Sub_Objects ();
  651. int start_index = FreeRandom.Get_Int ( count );
  652. //
  653. // Loop over all the objects
  654. //
  655. for (int index = 0; mesh == NULL && index < count; index ++) {
  656. //
  657. // Get a pointer to the current subobject
  658. //
  659. int real_index = (start_index + index) % count;
  660. RenderObjClass *subobj = model->Get_Sub_Object (real_index);
  661. if (subobj != NULL) {
  662. //
  663. // Try to find a mesh inside this object
  664. //
  665. mesh = Find_Random_Mesh (subobj);
  666. REF_PTR_RELEASE (subobj);
  667. }
  668. }
  669. return mesh;
  670. }
  671. ////////////////////////////////////////////////////////////////
  672. //
  673. // Update_Repairing_Animations
  674. //
  675. ////////////////////////////////////////////////////////////////
  676. void
  677. RepairBayGameObj::Update_Repairing_Animations (void)
  678. {
  679. //
  680. // Loop over all the vehicles that are currently being repaired
  681. //
  682. for (int index = 0; index < VehicleList.Count (); index ++) {
  683. ScriptableGameObj *game_obj = VehicleList[index];
  684. if (game_obj != NULL) {
  685. PhysicalGameObj *physical_game_obj = game_obj->As_PhysicalGameObj ();
  686. RenderObjClass *model = physical_game_obj->Peek_Model ();
  687. if (model != NULL) {
  688. //
  689. // Randomize the welding-arc's that this vehicle gets
  690. //
  691. if (FreeRandom.Get_Int (4) == 1) {
  692. Emit_Welding_Arc (model);
  693. }
  694. }
  695. }
  696. }
  697. return ;
  698. }
  699. ////////////////////////////////////////////////////////////////
  700. //
  701. // Emit_Welding_Arc
  702. //
  703. ////////////////////////////////////////////////////////////////
  704. void
  705. RepairBayGameObj::Emit_Welding_Arc (RenderObjClass *vehicle_model)
  706. {
  707. if (RepairMesh == NULL) {
  708. return ;
  709. }
  710. //
  711. // Pick a random "arc welder" to emit the animation from
  712. //
  713. Vector3 startpoint (0, 0, 0);
  714. Get_Position (&startpoint);
  715. //
  716. // Pick a random bone to emit the animation from
  717. //
  718. RenderObjClass *aggregate_model = RepairMesh->Peek_Model ();
  719. int count = 8;//aggregate_model->Get_Num_Bones ();
  720. int bone_index = FreeRandom.Get_Int (count) + 1;
  721. StringClass bone_name;
  722. bone_name.Format ("REP^NODRIM_FX%d", bone_index);
  723. startpoint = aggregate_model->Get_Bone_Transform (bone_name).Get_Translation ();
  724. //
  725. // Default the end of the vector to the vehicles start position
  726. //
  727. Vector3 endpoint = vehicle_model->Get_Transform ().Get_Translation ();
  728. //
  729. // Find a random mesh inside the vehicle's model
  730. //
  731. MeshClass *mesh = Find_Random_Mesh (vehicle_model);
  732. if (mesh != NULL) {
  733. MeshModelClass *mesh_model = mesh->Get_Model ();
  734. if (mesh_model != NULL) {
  735. //
  736. // Get a random vertex inside the model
  737. //
  738. int vertex_count = mesh_model->Get_Vertex_Count ();
  739. int random_vert_index = FreeRandom.Get_Int (vertex_count);
  740. if (vertex_count > 0) {
  741. endpoint = mesh->Get_Transform () * mesh_model->Get_Vertex_Array ()[random_vert_index];
  742. }
  743. REF_PTR_RELEASE (mesh_model);
  744. }
  745. }
  746. //
  747. // Try to find an available welding-arc
  748. //
  749. for (int index = 0; index < ARC_OBJ_COUNT; index ++) {
  750. if ((ArcLifeRemaining[index] <= 0) && (FreeRandom.Get_Int (4) == 1)) {
  751. //
  752. // Get the model of the object we'll be displaying
  753. //
  754. RenderObjClass *model = ArcObjects[index]->Peek_Physical_Object ()->Peek_Model ();
  755. //startpoint.Set (0, 0, 0);
  756. //endpoint.Set (0, 0, 10);
  757. //
  758. // Make the object "look" at its endpoint
  759. //
  760. Matrix3D start_tm;
  761. start_tm.Obj_Look_At (startpoint, endpoint, 0);
  762. model->Set_Transform (start_tm);
  763. //
  764. // Now scale the bone's that control the length of the arc so it
  765. // will fit perfectly between the start and endpoints.
  766. //
  767. for (int bone_index = 0; bone_index < BONE_COUNT; bone_index ++) {
  768. float percent = WWMath::Fabs (Bones[bone_index].Get_Translation ().X / EndTM.Get_Translation ().X);
  769. Vector3 new_pos = startpoint + (endpoint - startpoint) * percent;
  770. //
  771. // Calculate the world space position of the bone
  772. //
  773. Vector3 world_space_pos = start_tm * Bones[bone_index].Get_Translation ();
  774. Vector3 world_space_offset = new_pos - world_space_pos;
  775. Matrix3D bone_tm (world_space_offset);
  776. //
  777. // Control the bone
  778. //
  779. int bone_id = model->Get_Bone_Index (BoneNames[bone_index]);
  780. model->Capture_Bone (bone_id);
  781. model->Control_Bone (bone_id, bone_tm, true);
  782. }
  783. //
  784. // Calculate the world space position of the endpoint
  785. //
  786. Vector3 world_space_end_pos = start_tm * EndTM.Get_Translation ();
  787. Vector3 world_space_offset = endpoint - world_space_end_pos;
  788. Matrix3D end_tm (world_space_offset);
  789. //
  790. // Control the ending bone
  791. //
  792. int end_index = model->Get_Bone_Index ("bone_end");
  793. model->Capture_Bone (end_index);
  794. model->Control_Bone (end_index, end_tm, true);
  795. //
  796. // Start the animation
  797. //
  798. ArcLifeRemaining[index] = 3.0F;
  799. HAnimClass *anim = model->Peek_Animation ();
  800. if (anim != NULL) {
  801. model->Set_Animation (anim, 0, RenderObjClass::ANIM_MODE_ONCE);
  802. ArcLifeRemaining[index] = anim->Get_Total_Time ();
  803. }
  804. //
  805. // Show the model
  806. //
  807. model->Set_Hidden (false);
  808. }
  809. }
  810. return ;
  811. }
  812. /////////////////////////////////////////////////////////////////////////////
  813. //
  814. // Export_Creation
  815. //
  816. /////////////////////////////////////////////////////////////////////////////
  817. void
  818. RepairBayGameObj::Export_Creation (BitStreamClass &packet)
  819. {
  820. BuildingGameObj::Export_Creation (packet);
  821. //
  822. // Send the repair zone's position and size
  823. //
  824. packet.Add (RepairZone.Center.X, BITPACK_WORLD_POSITION_X);
  825. packet.Add (RepairZone.Center.Y, BITPACK_WORLD_POSITION_Y);
  826. packet.Add (RepairZone.Center.Z, BITPACK_WORLD_POSITION_Z);
  827. packet.Add (RepairZone.Extent.X, BITPACK_WORLD_POSITION_X);
  828. packet.Add (RepairZone.Extent.Y, BITPACK_WORLD_POSITION_Y);
  829. packet.Add (RepairZone.Extent.Z, BITPACK_WORLD_POSITION_Z);
  830. //
  831. // Send the zone's facing (almost 100% of the time, this zone should
  832. // simply be an AABox rotated about its Z-axis).
  833. //
  834. float facing = RepairZone.Basis.Get_Z_Rotation ();
  835. packet.Add (facing);
  836. return ;
  837. }
  838. /////////////////////////////////////////////////////////////////////////////
  839. //
  840. // Import_Creation
  841. //
  842. /////////////////////////////////////////////////////////////////////////////
  843. void
  844. RepairBayGameObj::Import_Creation (BitStreamClass &packet)
  845. {
  846. BuildingGameObj::Import_Creation (packet);
  847. //
  848. // Get the repair zone's position and size
  849. //
  850. packet.Get (RepairZone.Center.X, BITPACK_WORLD_POSITION_X);
  851. packet.Get (RepairZone.Center.Y, BITPACK_WORLD_POSITION_Y);
  852. packet.Get (RepairZone.Center.Z, BITPACK_WORLD_POSITION_Z);
  853. packet.Get (RepairZone.Extent.X, BITPACK_WORLD_POSITION_X);
  854. packet.Get (RepairZone.Extent.Y, BITPACK_WORLD_POSITION_Y);
  855. packet.Get (RepairZone.Extent.Z, BITPACK_WORLD_POSITION_Z);
  856. //
  857. // Get the zone's facing (almost 100% of the time, this zone should
  858. // simply be an AABox rotated about its Z-axis).
  859. //
  860. float facing = 0;
  861. packet.Get (facing);
  862. RepairZone.Basis.Rotate_Z (facing);
  863. return ;
  864. }