BULLET.CPP 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. /* $Header: F:\projects\c&c\vcs\code\bullet.cpv 2.18 16 Oct 1995 16:50:00 JOE_BOSTIC $ */
  15. /***********************************************************************************************
  16. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  17. ***********************************************************************************************
  18. * *
  19. * Project Name : Command & Conquer *
  20. * *
  21. * File Name : BULLET.CPP *
  22. * *
  23. * Programmer : Joe L. Bostic *
  24. * *
  25. * Start Date : April 23, 1994 *
  26. * *
  27. * Last Update : March 18, 1995 [JLB] *
  28. * *
  29. *---------------------------------------------------------------------------------------------*
  30. * Functions: *
  31. * BulletClass::AI -- Logic processing for bullet. *
  32. * BulletClass::As_Target -- Converts the bullet into a target value. *
  33. * BulletClass::BulletClass -- Bullet constructor. *
  34. * BulletClass::BulletClass -- Default constructor for bullet objects. *
  35. * BulletClass::Detach -- Removes specified target from this bullet's targeting system. *
  36. * BulletClass::Draw_It -- Displays the bullet at location specified. *
  37. * BulletClass::Init -- Clears the bullets array for scenario preparation. *
  38. * BulletClass::Mark -- Performs related map refreshing under bullet. *
  39. * BulletClass::Occupy_List -- Determines the bullet occupation list. *
  40. * BulletClass::Unlimbo -- Transitions a bullet object into the game render/logic system. *
  41. * BulletClass::delete -- Bullet memory delete. *
  42. * BulletClass::new -- Allocates memory for bullet object. *
  43. * BulletClass::Validate -- validates bullet pointer *
  44. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  45. #include "function.h"
  46. #define GRAVITY 3
  47. /*
  48. ** This contains the value of the Virtual Function Table Pointer
  49. */
  50. void * BulletClass::VTable;
  51. /***********************************************************************************************
  52. * BulletClass::Validate -- validates bullet pointer *
  53. * *
  54. * INPUT: *
  55. * none. *
  56. * *
  57. * OUTPUT: *
  58. * 1 = ok, 0 = error *
  59. * *
  60. * WARNINGS: *
  61. * none. *
  62. * *
  63. * HISTORY: *
  64. * 08/09/1995 BRR : Created. *
  65. *=============================================================================================*/
  66. #ifdef CHEAT_KEYS
  67. int BulletClass::Validate(void) const
  68. {
  69. int num;
  70. num = Bullets.ID(this);
  71. if (num < 0 || num >= BULLET_MAX) {
  72. Validate_Error("BULLET");
  73. return (0);
  74. }
  75. else
  76. return (1);
  77. }
  78. #else
  79. #define Validate()
  80. #endif
  81. /***********************************************************************************************
  82. * BulletClass::BulletClass -- Default constructor for bullet objects. *
  83. * *
  84. * This is the default constructor for bullet objects. A bullet constructed by this routine *
  85. * is not in a usable state for game purposes. It must be constructed by the full *
  86. * (parameterized) constructor -- usually called as part of the overloaded "new" operator. *
  87. * *
  88. * INPUT: none *
  89. * *
  90. * OUTPUT: none *
  91. * *
  92. * WARNINGS: Do not use bullets that were constructed solely by this routine. *
  93. * *
  94. * HISTORY: *
  95. * 09/24/1994 JLB : Created. *
  96. *=============================================================================================*/
  97. BulletClass::BulletClass(void) :
  98. Class(0)
  99. {
  100. Payback = NULL;
  101. IsToAnimate = false;
  102. Altitude = 0;
  103. Riser = 0;
  104. TarCom = TARGET_NONE;
  105. Strength = 0;
  106. IsLocked = true;
  107. IsInaccurate = false;
  108. }
  109. /***********************************************************************************************
  110. * BulletClass::new -- Allocates memory for bullet object. *
  111. * *
  112. * This function will "allocate" a block of memory for a bullet object. *
  113. * This memory block is merely lifted from a fixed pool of blocks. *
  114. * *
  115. * INPUT: size -- The size of the memory block needed. *
  116. * *
  117. * OUTPUT: Returns with a pointer to an available bullet object block. *
  118. * *
  119. * WARNINGS: none *
  120. * *
  121. * HISTORY: *
  122. * 05/02/1994 JLB : Created. *
  123. *=============================================================================================*/
  124. void * BulletClass::operator new(size_t )
  125. {
  126. void * ptr = Bullets.Allocate();
  127. if (ptr) {
  128. ((BulletClass *)ptr)->Set_Active();
  129. }
  130. return(ptr);
  131. }
  132. /***********************************************************************************************
  133. * BulletClass::delete -- Bullet memory delete. *
  134. * *
  135. * Since bullets memory is merely "allocated" out of a pool, it never *
  136. * actually gets deleted. *
  137. * *
  138. * INPUT: ptr -- Generic pointer to bullet object. *
  139. * *
  140. * OUTPUT: none *
  141. * *
  142. * WARNINGS: none *
  143. * *
  144. * HISTORY: *
  145. * 05/02/1994 JLB : Created. *
  146. *=============================================================================================*/
  147. void BulletClass::operator delete(void *ptr)
  148. {
  149. if (ptr) {
  150. ((BulletClass *)ptr)->IsActive = false;
  151. }
  152. Bullets.Free((BulletClass *)ptr);
  153. }
  154. /***********************************************************************************************
  155. * BulletClass::BulletClass -- Bullet constructor. *
  156. * *
  157. * This is the constructor for the bullet class. It handles all *
  158. * initialization of the bullet and starting it in motion toward its *
  159. * target. *
  160. * *
  161. * INPUT: id -- The type of bullet this is (could be missile). *
  162. * *
  163. * OUTPUT: none *
  164. * *
  165. * WARNINGS: none *
  166. * *
  167. * HISTORY: *
  168. * 05/02/1994 JLB : Created. *
  169. * 06/20/1994 JLB : Firer is a base class pointer. *
  170. * 12/10/1994 JLB : Auto calculate range optional. *
  171. * 12/12/1994 JLB : Handles small arms as an instantaneous effect. *
  172. * 12/23/1994 JLB : Fixed scatter algorithm for non-homing projectiles. *
  173. * 12/31/1994 JLB : Removed range parameter (not needed). *
  174. *=============================================================================================*/
  175. BulletClass::BulletClass(BulletType id) :
  176. Class(&BulletTypeClass::As_Reference(id))
  177. {
  178. Altitude = 0;
  179. IsInaccurate = false;
  180. IsLocked = true;
  181. // IsLocked = false;
  182. IsToAnimate = false;
  183. Payback = 0;
  184. Riser = 0;
  185. Strength = Class->MaxStrength;
  186. TarCom = TARGET_NONE;
  187. }
  188. /***********************************************************************************************
  189. * BulletClass::Occupy_List -- Determines the bullet occupation list. *
  190. * *
  191. * This function will determine the cell occupation list and return a pointer to it. Most *
  192. * bullets are small and the list is usually short, but on occasion, it can be a list that *
  193. * rivals the size of regular vehicles. *
  194. * *
  195. * INPUT: none *
  196. * *
  197. * OUTPUT: Returns with a pointer to the cell offset list that covers all the cells a bullet *
  198. * is over. *
  199. * *
  200. * WARNINGS: none *
  201. * *
  202. * HISTORY: *
  203. * 06/20/1994 JLB : Created. *
  204. * 01/05/1995 JLB : Handles projectiles with altitude. *
  205. *=============================================================================================*/
  206. short const *BulletClass::Occupy_List(void) const
  207. {
  208. Validate();
  209. switch (*this) {
  210. case BULLET_FLAME:
  211. return(Coord_Spillage_List(Coord, 25));
  212. case BULLET_NUKE_UP:
  213. case BULLET_NUKE_DOWN:
  214. return(Coord_Spillage_List(Coord, 48));
  215. default:
  216. if (Altitude) {
  217. static CELL _list[10];
  218. const short * ptr = Coord_Spillage_List(Coord, 5);
  219. int index = 0;
  220. CELL cell1 = Coord_Cell(Coord);
  221. while (ptr[index] != REFRESH_EOL) {
  222. _list[index] = ptr[index];
  223. index++;
  224. }
  225. COORDINATE coord = XY_Coord(0, Altitude);
  226. coord = Coord_Sub(Coord, coord);
  227. CELL cell2 = Coord_Cell(coord);
  228. ptr = Coord_Spillage_List(coord, 5);
  229. while (*ptr != REFRESH_EOL) {
  230. _list[index++] = (*ptr++) + (cell2 - cell1);
  231. }
  232. _list[index] = REFRESH_EOL;
  233. return(_list);
  234. }
  235. }
  236. return(Coord_Spillage_List(Coord, 10));
  237. }
  238. /***********************************************************************************************
  239. * BulletClass::Mark -- Performs related map refreshing under bullet. *
  240. * *
  241. * This routine marks the objects under the bullet so that they will *
  242. * be redrawn. This is necessary as the bullet moves -- objects under *
  243. * its path need to be restored. *
  244. * *
  245. * INPUT: none *
  246. * *
  247. * OUTPUT: none *
  248. * *
  249. * WARNINGS: none *
  250. * *
  251. * HISTORY: *
  252. * 05/02/1994 JLB : Created. *
  253. *=============================================================================================*/
  254. bool BulletClass::Mark(MarkType mark)
  255. {
  256. Validate();
  257. if (ObjectClass::Mark(mark)) {
  258. if (!Class->IsInvisible) {
  259. Map.Refresh_Cells(Coord_Cell(Coord), Occupy_List());
  260. }
  261. return(true);
  262. }
  263. return(false);
  264. }
  265. /***********************************************************************************************
  266. * BulletClass::AI -- Logic processing for bullet. *
  267. * *
  268. * This routine will perform all logic (flight) logic on the bullet. *
  269. * Primarily this is motion, fuse tracking, and detonation logic. Call *
  270. * this routine no more than once per bullet per game tick. *
  271. * *
  272. * INPUT: none *
  273. * *
  274. * OUTPUT: none *
  275. * *
  276. * WARNINGS: none *
  277. * *
  278. * HISTORY: *
  279. * 05/02/1994 JLB : Created. *
  280. *=============================================================================================*/
  281. void BulletClass::AI(void)
  282. {
  283. Validate();
  284. COORDINATE coord;
  285. ObjectClass::AI();
  286. /*
  287. ** Balistic objects are handled here.
  288. */
  289. bool forced = false; // Forced explosion.
  290. if (Class->IsArcing) {
  291. Altitude += Riser;
  292. if (Altitude <= 0) {
  293. forced = true;
  294. }
  295. if (Riser > -100) {
  296. Riser -= GRAVITY;
  297. }
  298. }
  299. if (Class->IsDropping) {
  300. Altitude += Riser;
  301. if (Altitude <= 0) {
  302. forced = true;
  303. }
  304. if (Riser > -100) {
  305. Riser -= 1;
  306. }
  307. }
  308. /*
  309. ** Homing projectiles constantly change facing to face toward the target but
  310. ** they only do so every other game frame (improves game speed and makes
  311. ** missiles not so deadly).
  312. */
  313. if ((Frame & 0x01) && Class->IsHoming && Target_Legal(TarCom)) {
  314. PrimaryFacing.Set_Desired(Direction256(Coord, ::As_Coord(TarCom)));
  315. }
  316. /*
  317. ** Move the projectile forward according to its speed
  318. ** and direction.
  319. */
  320. coord = Coord;
  321. if (Class->IsFlameEquipped) {
  322. if (IsToAnimate) {
  323. new AnimClass(ANIM_SMOKE_PUFF, coord, 1);
  324. }
  325. IsToAnimate = !IsToAnimate;
  326. }
  327. /*
  328. ** Handle any body rotation at this time. This process must
  329. ** occur every game fame in order to achieve smooth rotation.
  330. */
  331. if (PrimaryFacing.Is_Rotating()) {
  332. PrimaryFacing.Rotation_Adjust(Class->ROT);
  333. }
  334. switch (Physics(coord, PrimaryFacing)) {
  335. /*
  336. ** When a projectile reaches the edge of the world, it
  337. ** vanishes from existence -- presumed to explode off
  338. ** map.
  339. */
  340. case IMPACT_EDGE:
  341. // if (IsLocked) {
  342. Mark();
  343. Delete_This();
  344. // }
  345. break;
  346. default:
  347. case IMPACT_NONE:
  348. /*
  349. ** The projectile has moved. Check its fuse. If detonation
  350. ** is signaled, then do so. Otherwise, just move.
  351. */
  352. case IMPACT_NORMAL:
  353. Mark();
  354. // IsLocked = true;
  355. if (!Class->IsHigh) {
  356. CellClass * cellptr = &Map[Coord_Cell(coord)];
  357. if (cellptr->Overlay != OVERLAY_NONE && OverlayTypeClass::As_Reference(cellptr->Overlay).IsHigh) {
  358. forced = true;
  359. Coord = coord = Cell_Coord(Coord_Cell(coord));
  360. }
  361. }
  362. /*
  363. ** Bullets are generaly more effective when they are fired at aircraft.
  364. */
  365. if (Class->IsAntiAircraft && As_Aircraft(TarCom) && Distance(TarCom) < 0x0080) {
  366. forced = true;
  367. if (*this == BULLET_TOW) {
  368. Strength += Strength/3;
  369. } else {
  370. Strength += Strength/2;
  371. }
  372. }
  373. if (!forced && (Class->IsDropping || !Fuse_Checkup(coord))) {
  374. Coord = coord;
  375. Mark();
  376. /*
  377. ** Certain projectiles loose strength when they travel.
  378. */
  379. if (*this == BULLET_BULLET) {
  380. if (Strength > 5) Strength--;
  381. }
  382. } else {
  383. /*
  384. ** When the target is reached, explode and do the damage
  385. ** required of it. For homing objects, don't force the explosion to
  386. ** match the target position. Non-homing projectiles adjust position so
  387. ** that they hit the target. This compensates for the error in line of
  388. ** flight logic.
  389. */
  390. Mark();
  391. if (!forced && !Class->IsArcing && !Class->IsHoming && Fuse_Target()) {
  392. Coord = Fuse_Target();
  393. }
  394. /*
  395. ** Non-aircraft targets apply damage to the ground.
  396. */
  397. if (!Is_Target_Aircraft(TarCom) || As_Aircraft(TarCom)->In_Which_Layer() == LAYER_GROUND) {
  398. Explosion_Damage(Coord, Strength, Payback, Class->Warhead);
  399. } else {
  400. /*
  401. ** Special damage apply for SAM missiles. This is the only way that missile
  402. ** damage affects the aircraft target.
  403. */
  404. if (Distance(TarCom) < 0x0080) {
  405. AircraftClass * object = As_Aircraft(TarCom);
  406. int str = Strength;
  407. if (object) object->Take_Damage(str, 0, Class->Warhead, Payback);
  408. }
  409. }
  410. /*
  411. ** For projectiles that are invisible while travelling toward the target,
  412. ** allow scatter effect for the impact animation.
  413. */
  414. if (Class->IsInvisible) {
  415. Coord = Coord_Scatter(Coord, 0x0020);
  416. }
  417. if (Class->Explosion != ANIM_NONE) {
  418. AnimClass * newanim = new AnimClass(Class->Explosion, Coord);
  419. if (newanim) {
  420. newanim->Sort_Above(TarCom);
  421. }
  422. // MBL 05.20.2020
  423. // Fix for Nuke or Atom Bomb killing structures and units during animation sequence and not getting kills tracked
  424. // Per https://jaas.ea.com/browse/TDRA-6610
  425. //
  426. if (newanim && Class->Explosion == ANIM_ATOM_BLAST && newanim->OwnerHouse == HOUSE_NONE) {
  427. if (Payback && Payback->House && Payback->House->Class) {
  428. newanim->Set_Owner(Payback->House->Class->House);
  429. }
  430. }
  431. }
  432. Delete_This();
  433. return;
  434. }
  435. break;
  436. }
  437. }
  438. /***********************************************************************************************
  439. * BulletClass::Draw_It -- Displays the bullet at location specified. *
  440. * *
  441. * This routine displays the bullet visual at the location specified. *
  442. * *
  443. * INPUT: x,y -- The center coordinate to render the bullet at. *
  444. * *
  445. * window -- The window to clip to. *
  446. * *
  447. * OUTPUT: none *
  448. * *
  449. * WARNINGS: none *
  450. * *
  451. * HISTORY: *
  452. * 06/20/1994 JLB : Created. *
  453. * 06/27/1994 JLB : Takes a window clipping parameter. *
  454. * 01/08/1995 JLB : Handles translucent colors if necessary. *
  455. *=============================================================================================*/
  456. void BulletClass::Draw_It(int x, int y, WindowNumberType window)
  457. {
  458. Validate();
  459. int facing = Facing_To_32(PrimaryFacing);
  460. /*
  461. ** Certain projectiles aren't visible. This includes small bullets (which are actually
  462. ** invisible) and flame thrower flames (which are rendered as an animation instead of a projectile).
  463. */
  464. if (Class->IsInvisible) return;
  465. /*
  466. ** If there is no shape loaded for this object, then
  467. ** it obviously can't be rendered -- just bail.
  468. */
  469. void const * shapeptr = Class->Get_Image_Data();
  470. if (!shapeptr) return;
  471. /*
  472. ** Get the basic shape number for this projectile.
  473. */
  474. int shapenum = 0;
  475. if (!Class->IsFaceless) {
  476. shapenum = UnitClass::BodyShape[facing];
  477. }
  478. /*
  479. ** For tumbling projectiles, fetch offset stage.
  480. */
  481. if (*this == BULLET_NAPALM) {
  482. shapenum += Frame % 6;
  483. }
  484. if (*this == BULLET_GRENADE) {
  485. shapenum += Frame % 8;
  486. // Timer++;
  487. }
  488. /*
  489. ** For flying projectiles, draw the shadow and adjust the actual projectile body
  490. ** render position.
  491. */
  492. if (Altitude) {
  493. CC_Draw_Shape(shapeptr, shapenum, x, y, window, SHAPE_PREDATOR|SHAPE_CENTER|SHAPE_WIN_REL|SHAPE_FADING, NULL, Map.FadingShade);
  494. y -= Lepton_To_Pixel(Altitude);
  495. }
  496. /*
  497. ** Draw the main body of the projectile.
  498. */
  499. ShapeFlags_Type flags = SHAPE_NORMAL;
  500. if (Class->IsTranslucent) {
  501. flags = SHAPE_GHOST;
  502. }
  503. CC_Draw_Shape(this, shapeptr, shapenum, x, y, window, flags|SHAPE_CENTER|SHAPE_WIN_REL, NULL, Map.UnitShadow);
  504. }
  505. /***********************************************************************************************
  506. * BulletClass::Init -- Clears the bullets array for scenario preparation. *
  507. * *
  508. * This routine will zero out the bullet tracking list and object array in preparation for *
  509. * the start of a new scenario. All bullets cease to exists after this function is *
  510. * called. *
  511. * *
  512. * INPUT: none *
  513. * *
  514. * OUTPUT: none *
  515. * *
  516. * WARNINGS: none *
  517. * *
  518. * HISTORY: *
  519. * 08/15/1994 JLB : Created. *
  520. *=============================================================================================*/
  521. void BulletClass::Init(void)
  522. {
  523. BulletClass *ptr;
  524. Bullets.Free_All();
  525. ptr = new BulletClass();
  526. VTable = ((void **)(((char *)ptr) + sizeof(AbstractClass) - 4))[0];
  527. delete ptr;
  528. }
  529. /***********************************************************************************************
  530. * BulletClass::Detach -- Removes specified target from this bullet's targeting system. *
  531. * *
  532. * When an object is removed from the game system, it must be removed all targeting and *
  533. * tracking systems as well. This routine is used to remove the specified object from the *
  534. * bullet. If the object isn't part of this bullet's tracking system, then no action is *
  535. * performed. *
  536. * *
  537. * INPUT: target -- The target to remove from this tracking system. *
  538. * *
  539. * all -- Is the target going away for good as opposed to just cloaking/hiding? *
  540. * *
  541. * OUTPUT: none *
  542. * *
  543. * WARNINGS: none *
  544. * *
  545. * HISTORY: *
  546. * 09/24/1994 JLB : Created. *
  547. *=============================================================================================*/
  548. void BulletClass::Detach(TARGET target, bool all)
  549. {
  550. Validate();
  551. ObjectClass * obj = As_Object(target);
  552. if (obj == Payback) {
  553. Payback = 0;
  554. }
  555. if (all && target == TarCom) {
  556. TarCom = TARGET_NONE;
  557. }
  558. }
  559. /***********************************************************************************************
  560. * BulletClass::Unlimbo -- Transitions a bullet object into the game render/logic system. *
  561. * *
  562. * This routine is used to take a bullet object that is in limbo and transition it to the *
  563. * game system. A bullet object so transitioned, will be drawn and logic processing *
  564. * performed. In effect, it comes into existance. *
  565. * *
  566. * INPUT: coord -- The location where the bullet object is to appear. *
  567. * *
  568. * dir -- The initial facing for the bullet object. *
  569. * *
  570. * OUTPUT: bool; Was the unlimbo successful? *
  571. * *
  572. * WARNINGS: none *
  573. * *
  574. * HISTORY: *
  575. * 01/10/1995 JLB : Created. *
  576. *=============================================================================================*/
  577. bool BulletClass::Unlimbo(COORDINATE coord, DirType dir)
  578. {
  579. Validate();
  580. /*
  581. ** Try to unlimbo the bullet as far as the base class is concerned. Use the already
  582. ** set direction and strength if the "punt" values were passed in. This allows a bullet
  583. ** to be setup prior to being launched.
  584. */
  585. if (ObjectClass::Unlimbo(coord)) {
  586. COORDINATE tcoord = As_Coord(TarCom);
  587. /*
  588. ** Homing projectiles (missiles) do NOT override facing. They just fire in the
  589. ** direction specified and let the chips fall where they may.
  590. */
  591. if (!Class->IsHoming && !Class->IsDropping) {
  592. dir = Direction(tcoord);
  593. }
  594. /*
  595. ** Possibly adjust the target if this projectile is inaccurate. This occurs whenever
  596. ** certain weapons are trained upon targets they were never designed to attack. Example: when
  597. ** turrets or anti-tank missiles are fired at infantry. Indirect
  598. ** fire is inherently inaccurate.
  599. */
  600. if (IsInaccurate || Class->IsInaccurate ||
  601. ((Is_Target_Cell(TarCom) || Is_Target_Infantry(TarCom)) && (Class->Warhead == WARHEAD_AP || Class->IsFueled))) {
  602. /*
  603. ** Inaccuracy for low velocity or homing projectiles manifests itself as a standard
  604. ** Cicular Error of Probability (CEP) algorithm. High speed projectiles usually
  605. ** just overshoot the target by extending the straight line flight.
  606. */
  607. if (Class->IsHoming || Class->IsArcing) {
  608. int scatterdist = ::Distance(coord, tcoord)/3;
  609. scatterdist = MIN(scatterdist, 0x0200);
  610. if (*this == BULLET_GRENADE) {
  611. scatterdist = ::Distance(coord, tcoord)/4;
  612. scatterdist = MIN(scatterdist, 0x0080);
  613. }
  614. dir = (DirType)((dir + (Random_Pick(0, 10)-5)) & 0x00FF);
  615. tcoord = Coord_Scatter(tcoord, Random_Pick(0, scatterdist));
  616. } else {
  617. tcoord = Coord_Move(tcoord, dir, Random_Pick(0, 0x0100));
  618. }
  619. /*
  620. ** Limit scatter to the weapon range of the firer.
  621. */
  622. if (Payback) {
  623. if (!Payback->In_Range(tcoord, 0) && !Payback->In_Range(tcoord, 1)) {
  624. tcoord = Coord_Move(tcoord, ::Direction(tcoord, Coord), Distance(tcoord) - MAX(Payback->Weapon_Range(0), Payback->Weapon_Range(1)));
  625. }
  626. }
  627. }
  628. /*
  629. ** For very fast and invisible projectiles, just make the projectile exist at the target
  630. ** location and dispense with the actual flight.
  631. */
  632. if (Class->MaxSpeed == MPH_LIGHT_SPEED && Class->IsInvisible) {
  633. Coord = tcoord;
  634. }
  635. /*
  636. ** Set the range equal to either the class defined range or the calculated
  637. ** number of game frames it would take for the projectile to reach the target.
  638. */
  639. int range = 0xFF;
  640. if (!Class->Range) {
  641. if (!Class->IsDropping) {
  642. range = (::Distance(tcoord, Coord) / Class->MaxSpeed) + 4;
  643. }
  644. } else {
  645. range = Class->Range;
  646. }
  647. /*
  648. ** Projectile speed is usually the default value for that projectile, but
  649. ** certian projectiles alter speed according to the distance to the
  650. ** target.
  651. */
  652. int speed = Class->MaxSpeed;
  653. if (speed == MPH_LIGHT_SPEED) speed = MPH_IMMOBILE;
  654. if (Class->IsArcing) {
  655. speed = Class->MaxSpeed + (Distance(tcoord)>>5);
  656. /*
  657. ** Set minimum speed (i.e., distance) for arcing projectiles.
  658. */
  659. speed = MAX(speed, 25);
  660. }
  661. if (!Class->IsDropping) {
  662. Fly_Speed(255, (MPHType)speed);
  663. }
  664. /*
  665. ** Arm the fuse.
  666. */
  667. Arm_Fuse(Coord, tcoord, range, ((As_Aircraft(TarCom)!=0) ? 0 : Class->Arming));
  668. /*
  669. ** Projectiles that make a ballistic flight to impact point must determine a
  670. ** vertical component for the projectile launch. This is crudely simulated
  671. ** by biasing ground speed according to target distance and then giving
  672. ** enough vertical velocity to keep the projectile airborne for the
  673. ** desired amount of time. The mathematically correct solution would be to
  674. ** calculate launch angle (given fixed projectile velocity) and then derive
  675. ** the vertical and horizontal components. This solution would require a
  676. ** of square root and an arcsine lookup table. OUCH!
  677. */
  678. Altitude = 0;
  679. Riser = 0;
  680. if (Class->IsArcing) {
  681. Altitude = 1;
  682. Riser = ((Distance(tcoord)/2) / (speed+1))*GRAVITY;
  683. Riser = MAX(Riser, (signed char)10);
  684. }
  685. if (Class->IsDropping) {
  686. Altitude = Pixel_To_Lepton(24);
  687. Riser = 0;
  688. }
  689. PrimaryFacing = dir;
  690. return(true);
  691. }
  692. return(false);
  693. }
  694. /***********************************************************************************************
  695. * BulletClass::As_Target -- Converts the bullet into a target value. *
  696. * *
  697. * This support routine is used to convert the bullet (as a pointer) into a target *
  698. * value (which is a number). *
  699. * *
  700. * INPUT: none *
  701. * *
  702. * OUTPUT: Returns the bullet as a target value. *
  703. * *
  704. * WARNINGS: none *
  705. * *
  706. * HISTORY: *
  707. * 09/08/1994 JLB : Created. *
  708. *=============================================================================================*/
  709. TARGET BulletClass::As_Target(void) const
  710. {
  711. Validate();
  712. return(Build_Target(KIND_BULLET, Bullets.ID(this)));
  713. }