ANIM.CPP 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  1. /*
  2. ** Command & Conquer(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. /* $Header: F:\projects\c&c\vcs\code\anim.cpv 2.18 16 Oct 1995 16:48:48 JOE_BOSTIC $ */
  19. /***********************************************************************************************
  20. *** 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 ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Dune *
  24. * *
  25. * File Name : ANIM.CPP *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : June 3, 1991 *
  30. * *
  31. *---------------------------------------------------------------------------------------------*
  32. * Functions: *
  33. * AnimClass::AI -- This is the low level anim processor. *
  34. * AnimClass::Adjust_Coord -- Adjusts anim coordinates *
  35. * AnimClass::AnimClass -- The constructor for animation objects. *
  36. * AnimClass::As_Target -- Converts the animation into a target value. *
  37. * AnimClass::Attach_To -- Attaches animation to object specified. *
  38. * AnimClass::Center_Coord -- Determine center of animation. *
  39. * AnimClass::Detach -- Remove animation if attached to target. *
  40. * AnimClass::Draw_It -- Draws the animation at the location specified. *
  41. * AnimClass::In_Which_Layer -- Determines what render layer the anim should be in. *
  42. * AnimClass::Init -- Performs pre-scenario initialization. *
  43. * AnimClass::Mark -- Signals to map that redrawing is necessary. *
  44. * AnimClass::Middle -- Processes any middle events. *
  45. * AnimClass::Occupy_List -- Determines the occupy list for the animation. *
  46. * AnimClass::Overlap_List -- Determines the overlap list for the animation. *
  47. * AnimClass::Render -- Draws an animation object. *
  48. * AnimClass::Sort_Y -- Returns with the sorting coordinate for the animation. *
  49. * AnimClass::Start -- Processes initial animation side effects. *
  50. * AnimClass::delete -- Returns an anim object back to the free pool. *
  51. * AnimClass::new -- Allocates an anim object from the pool. *
  52. * AnimClass::~AnimClass -- Destructor for anim objects. *
  53. * AnimClass::Validate -- validates anim pointer *
  54. * Shorten_Attached_Anims -- Reduces attached animation durations. *
  55. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  56. #include "function.h"
  57. /*
  58. ** This contains the value of the Virtual Function Table Pointer
  59. */
  60. void * AnimClass::VTable;
  61. /***********************************************************************************************
  62. * AnimClass::Validate -- validates anim pointer *
  63. * *
  64. * INPUT: *
  65. * none. *
  66. * *
  67. * OUTPUT: *
  68. * 1 = ok, 0 = error *
  69. * *
  70. * WARNINGS: *
  71. * none. *
  72. * *
  73. * HISTORY: *
  74. * 08/09/1995 BRR : Created. *
  75. *=============================================================================================*/
  76. #ifdef CHEAT_KEYS
  77. int AnimClass::Validate(void) const
  78. {
  79. int num;
  80. num = Anims.ID(this);
  81. if (num < 0 || num >= ANIM_MAX) {
  82. Validate_Error("ANIM");
  83. return (0);
  84. }
  85. else
  86. return (1);
  87. }
  88. #else
  89. #define Validate()
  90. #endif
  91. /***********************************************************************************************
  92. * Shorten_Attached_Anims -- Reduces attached animation durations. *
  93. * *
  94. * This routine is used to reduce the amount of time any attached animations will process. *
  95. * Typical use of this is when an object is on fire and the object should now be destroyed *
  96. * but the attached animations are to run until completion before destruction can follow. *
  97. * This routine will make the animation appear to run its course, but in as short of time *
  98. * as possible. The shortening effect is achieved by reducing the number of times the *
  99. * animation will loop. *
  100. * *
  101. * INPUT: obj -- Pointer to the object that all attached animations will be processed. *
  102. * *
  103. * OUTPUT: none *
  104. * *
  105. * WARNINGS: none *
  106. * *
  107. * HISTORY: *
  108. * 12/11/1994 JLB : Created. *
  109. *=============================================================================================*/
  110. void Shorten_Attached_Anims(ObjectClass * obj)
  111. {
  112. if (obj) {
  113. for (int index = 0; index < Anims.Count(); index++) {
  114. AnimClass & anim = *Anims.Ptr(index);
  115. if (anim.Object == obj) {
  116. anim.Loops = 0;
  117. }
  118. }
  119. }
  120. }
  121. /***********************************************************************************************
  122. * AnimClass::Sort_Y -- Returns with the sorting coordinate for the animation. *
  123. * *
  124. * This routine is used by the sorting system. Animations that are located in the ground *
  125. * layer will be sorted by this the value returned from this function. *
  126. * *
  127. * INPUT: none *
  128. * *
  129. * OUTPUT: Returns with the sort coordinate to use for this animation. *
  130. * *
  131. * WARNINGS: none *
  132. * *
  133. * HISTORY: *
  134. * 10/17/1994 JLB : Created. *
  135. * 12/15/1994 JLB : Handles flat anims (infantry decay anims). *
  136. *=============================================================================================*/
  137. COORDINATE AnimClass::Sort_Y(void) const
  138. {
  139. Validate();
  140. if (Object) {
  141. return(Coord_Add(Object->Sort_Y(), 0x00010000L));
  142. }
  143. if (*this == ANIM_MOVE_FLASH) {
  144. return(Coord_Add(Center_Coord(), XYP_COORD(0, -24)));
  145. }
  146. if (*this == ANIM_LZ_SMOKE) {
  147. return(Coord_Add(Center_Coord(), XYP_COORD(0, 14)));
  148. }
  149. return(Coord);
  150. }
  151. /***********************************************************************************************
  152. * AnimClass::Center_Coord -- Determine center of animation. *
  153. * *
  154. * This support function will return the "center" of the animation. The actual coordinate *
  155. * of the animation may be dependant on if the the animation is attached to an object. *
  156. * In such a case, it must factor in the object's location. *
  157. * *
  158. * INPUT: none *
  159. * *
  160. * OUTPUT: Returns the coordinate of the center of the animation. The coordinate is in real *
  161. * game coordinates -- taking into consideration if the animation is attached. *
  162. * *
  163. * WARNINGS: none *
  164. * *
  165. * HISTORY: *
  166. * 09/19/1994 JLB : Created. *
  167. *=============================================================================================*/
  168. COORDINATE AnimClass::Center_Coord(void) const
  169. {
  170. Validate();
  171. if (Object) {
  172. return(Coord_Add(Coord, Object->Center_Coord()));
  173. }
  174. return(Coord);
  175. }
  176. /***********************************************************************************************
  177. * AnimClass::Render -- Draws an animation object. *
  178. * *
  179. * This is the working routine that renders the animation shape. It gets called once *
  180. * per animation per frame. It needs to be fast. *
  181. * *
  182. * INPUT: bool; Should the animation be rendered in spite of render flag? *
  183. * *
  184. * OUTPUT: bool; Was the animation rendered? *
  185. * *
  186. * WARNINGS: none *
  187. * *
  188. * HISTORY: *
  189. * 05/31/1994 JLB : Created. *
  190. *=============================================================================================*/
  191. bool AnimClass::Render(bool forced)
  192. {
  193. Validate();
  194. if (Delay) return(false);
  195. IsToDisplay = true;
  196. return(ObjectClass::Render(forced));
  197. }
  198. /***********************************************************************************************
  199. * AnimClass::Draw_It -- Draws the animation at the location specified. *
  200. * *
  201. * This routine is used to render the animation object at the location specified. This is *
  202. * how the map imagery gets updated. *
  203. * *
  204. * INPUT: x,y -- The pixel coordinates to draw the animation at. *
  205. * *
  206. * window -- The to base the draw coordinates upon. *
  207. * *
  208. * OUTPUT: none *
  209. * *
  210. * WARNINGS: none *
  211. * *
  212. * HISTORY: *
  213. * 09/24/1994 JLB : Created. *
  214. * 05/19/1995 JLB : Added white translucent effect. *
  215. *=============================================================================================*/
  216. #pragma off (unreferenced)
  217. void AnimClass::Draw_It(int x, int y, WindowNumberType window)
  218. {
  219. Validate();
  220. if (!IsInvisible) {
  221. void const * shapefile = Class->Get_Image_Data();
  222. if (shapefile) {
  223. void const * transtable = NULL;
  224. int shapenum = Class->Start + Fetch_Stage();
  225. void const * remap = NULL;
  226. /*
  227. ** Some animations require special fixups.
  228. */
  229. switch (Class->Type) {
  230. case ANIM_ION_CANNON:
  231. y -= Get_Build_Frame_Height(shapefile) >> 1;
  232. y += 12;
  233. break;
  234. case ANIM_RAPT_DIE:
  235. case ANIM_STEG_DIE:
  236. case ANIM_TREX_DIE:
  237. case ANIM_TRIC_DIE:
  238. case ANIM_ATOM_BLAST:
  239. transtable = Map.UnitShadow;
  240. break;
  241. }
  242. /*
  243. ** If the translucent table hasn't been determined yet, then check to see if it
  244. ** should use the white or normal translucent tables.
  245. */
  246. if (!transtable && Class->IsWhiteTrans) transtable = Map.WhiteTranslucentTable;
  247. if (!transtable && Class->IsTranslucent) transtable = Map.TranslucentTable;
  248. /*
  249. ** Set the shape flags to properly take into account any fading or ghosting
  250. ** table necessary.
  251. */
  252. ShapeFlags_Type flags = SHAPE_CENTER|SHAPE_WIN_REL;
  253. if (IsAlternate) {
  254. flags = flags | SHAPE_FADING;
  255. remap = Map.RemapTables[HOUSE_GOOD][0];
  256. }
  257. if (transtable) flags = flags | SHAPE_GHOST;
  258. /*
  259. ** Draw the animation shape.
  260. */
  261. CC_Draw_Shape(shapefile, shapenum, x, y, window, flags, remap, transtable);
  262. }
  263. }
  264. }
  265. /***********************************************************************************************
  266. * AnimClass::Mark -- Signals to map that redrawing is necessary. *
  267. * *
  268. * This routine is used by the animation logic system to inform the map that the cells *
  269. * under the animation must be rerendered. *
  270. * *
  271. * INPUT: *
  272. * *
  273. * OUTPUT: none *
  274. * *
  275. * WARNINGS: none *
  276. * *
  277. * HISTORY: *
  278. * 05/31/1994 JLB : Created. *
  279. *=============================================================================================*/
  280. bool AnimClass::Mark(MarkType mark)
  281. {
  282. Validate();
  283. if (ObjectClass::Mark(mark)) {
  284. Map.Refresh_Cells(Coord_Cell(Center_Coord()), Overlap_List());
  285. ObjectClass::Mark(mark);
  286. return(true);
  287. }
  288. return(false);
  289. }
  290. /***********************************************************************************************
  291. * AnimClass::Overlap_List -- Determines the overlap list for the animation. *
  292. * *
  293. * Use this routine to fetch the overlap list for the animation. This overlap list is the *
  294. * cells that this animation spills over. *
  295. * *
  296. * INPUT: none *
  297. * *
  298. * OUTPUT: Returns a pointer to the overlap list for this particular instance of the *
  299. * animation. *
  300. * *
  301. * WARNINGS: none *
  302. * *
  303. * HISTORY: *
  304. * 03/19/1995 JLB : Created. *
  305. *=============================================================================================*/
  306. short const * AnimClass::Overlap_List(void) const
  307. {
  308. Validate();
  309. static short const OverlapN[] = {0, -MAP_CELL_W, -(MAP_CELL_W+1), -(MAP_CELL_W-1), -(2*MAP_CELL_W), -(2*MAP_CELL_W-1), -(2*MAP_CELL_W+1), REFRESH_EOL};
  310. static short const OverlapNW[] = {0, -1, -MAP_CELL_W, -(MAP_CELL_W+1), -(MAP_CELL_W+2), -(MAP_CELL_W*2+2), -(MAP_CELL_W*2+1), REFRESH_EOL};
  311. static short const OverlapW[] = {0, -1, -2, -(MAP_CELL_W+1), -(MAP_CELL_W+2), REFRESH_EOL};
  312. static short const OverlapSW[] = {0, -1, MAP_CELL_W, (MAP_CELL_W-1), (MAP_CELL_W-2), (MAP_CELL_W*2-2), (MAP_CELL_W*2-1), REFRESH_EOL};
  313. static short const OverlapS[] = {0, MAP_CELL_W-1, MAP_CELL_W, MAP_CELL_W+1, 2*MAP_CELL_W+1, 2*MAP_CELL_W, 2*MAP_CELL_W-1, REFRESH_EOL};
  314. static short const OverlapSE[] = {0, 1, MAP_CELL_W, (MAP_CELL_W+1), (MAP_CELL_W+2), (MAP_CELL_W*2+2), (MAP_CELL_W*2+1), REFRESH_EOL};
  315. static short const OverlapE[] = {0, 1, 2, -(MAP_CELL_W-1), -(MAP_CELL_W-2), REFRESH_EOL};
  316. static short const OverlapNE[] = {0, 1, -MAP_CELL_W, -(MAP_CELL_W-1), -(MAP_CELL_W-2), -(MAP_CELL_W*2-2), -(MAP_CELL_W*2-1), REFRESH_EOL};
  317. static short const OverlapIon[] = {
  318. (-MAP_CELL_W * 7) - 1, (-MAP_CELL_W * 7), (-MAP_CELL_W * 7) + 1,
  319. (-MAP_CELL_W * 6) - 1, (-MAP_CELL_W * 6), (-MAP_CELL_W * 6) + 1,
  320. (-MAP_CELL_W * 5) - 1, (-MAP_CELL_W * 5), (-MAP_CELL_W * 5) + 1,
  321. (-MAP_CELL_W * 4) - 1, (-MAP_CELL_W * 4), (-MAP_CELL_W * 4) + 1,
  322. (-MAP_CELL_W * 3) - 1, (-MAP_CELL_W * 3), (-MAP_CELL_W * 3) + 1,
  323. (-MAP_CELL_W * 2) - 1, (-MAP_CELL_W * 2), (-MAP_CELL_W * 2) + 1,
  324. (-MAP_CELL_W * 1) - 1, (-MAP_CELL_W * 1), (-MAP_CELL_W * 1) + 1,
  325. (-MAP_CELL_W * 0) - 1, (-MAP_CELL_W * 0), (-MAP_CELL_W * 0) + 1,
  326. REFRESH_EOL
  327. };
  328. static short const OverlapAtom[] = {
  329. (-MAP_CELL_W * 2) - 1, (-MAP_CELL_W * 2), (-MAP_CELL_W * 2) + 1,
  330. (-MAP_CELL_W * 1) - 1, (-MAP_CELL_W * 1), (-MAP_CELL_W * 1) + 1,
  331. (-MAP_CELL_W * 0) - 1, (-MAP_CELL_W * 0), (-MAP_CELL_W * 0) + 1,
  332. ( MAP_CELL_W * 1) - 1, ( MAP_CELL_W * 1), ( MAP_CELL_W * 1) + 1,
  333. ( MAP_CELL_W * 2) - 1, ( MAP_CELL_W * 2), ( MAP_CELL_W * 2) + 1,
  334. REFRESH_EOL
  335. };
  336. switch (Class->Type) {
  337. case ANIM_CHEM_N:
  338. case ANIM_FLAME_N:
  339. return(OverlapN);
  340. case ANIM_CHEM_NW:
  341. case ANIM_FLAME_NW:
  342. return(OverlapNW);
  343. case ANIM_CHEM_W:
  344. case ANIM_FLAME_W:
  345. return(OverlapW);
  346. case ANIM_CHEM_SW:
  347. case ANIM_FLAME_SW:
  348. return(OverlapSW);
  349. case ANIM_CHEM_S:
  350. case ANIM_FLAME_S:
  351. return(OverlapS);
  352. case ANIM_CHEM_SE:
  353. case ANIM_FLAME_SE:
  354. return(OverlapSE);
  355. case ANIM_CHEM_E:
  356. case ANIM_FLAME_E:
  357. return(OverlapE);
  358. case ANIM_CHEM_NE:
  359. case ANIM_FLAME_NE:
  360. return(OverlapNE);
  361. case ANIM_ION_CANNON:
  362. return(OverlapIon);
  363. case ANIM_ATOM_BLAST:
  364. return(OverlapAtom);
  365. default:
  366. break;
  367. }
  368. return(Coord_Spillage_List(Center_Coord(), Class->Size));
  369. }
  370. /***********************************************************************************************
  371. * AnimClass::Occupy_List -- Determines the occupy list for the animation. *
  372. * *
  373. * Animations always occupy only the cell that their center is located over. As such, this *
  374. * routine always returns a simple (center cell) occupation list. *
  375. * *
  376. * INPUT: none *
  377. * *
  378. * OUTPUT: Returns with the occupation list for the animation. *
  379. * *
  380. * WARNINGS: none *
  381. * *
  382. * HISTORY: *
  383. * 03/19/1995 JLB : Created. *
  384. *=============================================================================================*/
  385. short const * AnimClass::Occupy_List(void) const
  386. {
  387. Validate();
  388. static short _simple[] = {REFRESH_EOL};
  389. return(_simple);
  390. }
  391. /***********************************************************************************************
  392. * AnimClass::Init -- Performs pre-scenario initialization. *
  393. * *
  394. * This routine is used to initialize the animation system prior to a scenario being loaded *
  395. * or reloaded. It effectively removes all animations from the system. *
  396. * *
  397. * INPUT: none *
  398. * *
  399. * OUTPUT: none *
  400. * *
  401. * WARNINGS: none *
  402. * *
  403. * HISTORY: *
  404. * 05/31/1994 JLB : Created. *
  405. *=============================================================================================*/
  406. void AnimClass::Init(void)
  407. {
  408. AnimClass *ptr;
  409. Anims.Free_All();
  410. ptr = new AnimClass();
  411. VTable = ((void **)(((char *)ptr) + sizeof(AbstractClass) - 4))[0];
  412. delete ptr;
  413. }
  414. /***********************************************************************************************
  415. * AnimClass::new -- Allocates an anim object from the pool. *
  416. * *
  417. * This routine is used to allocate a free anim class object from the preallocated pool *
  418. * in the near heap. If there are no free animation objects, then null is returned. *
  419. * *
  420. * INPUT: none *
  421. * *
  422. * OUTPUT: Returns with a pointer to a free anim object. *
  423. * *
  424. * WARNINGS: none *
  425. * *
  426. * HISTORY: *
  427. * 05/31/1994 JLB : Created. *
  428. *=============================================================================================*/
  429. void *AnimClass::operator new(size_t)
  430. {
  431. void * ptr = Anims.Allocate();
  432. if (ptr) {
  433. ((AnimClass *)ptr)->IsActive = true;
  434. }
  435. return(ptr);
  436. }
  437. /***********************************************************************************************
  438. * AnimClass::delete -- Returns an anim object back to the free pool. *
  439. * *
  440. * This routine is used to return an anim object back to the pool of free anim objects. *
  441. * Anim objects so returned are available to be reallocated for the next animation. *
  442. * *
  443. * INPUT: ptr -- Pointer to the anim object to return to the pool. *
  444. * *
  445. * OUTPUT: none *
  446. * *
  447. * WARNINGS: none *
  448. * *
  449. * HISTORY: *
  450. * 05/31/1994 JLB : Created. *
  451. *=============================================================================================*/
  452. void AnimClass::operator delete(void *ptr)
  453. {
  454. if (ptr) {
  455. ((AnimClass *)ptr)->IsActive = false;
  456. }
  457. Anims.Free((AnimClass *)ptr);
  458. }
  459. /***********************************************************************************************
  460. * AnimClass::AnimClass -- The constructor for animation objects. *
  461. * *
  462. * This routine is used as the constructor of animation objects. It initializes and adds *
  463. * the animation object to the display and logic systems. *
  464. * *
  465. * INPUT: animnum -- The animation number to start. *
  466. * *
  467. * coord -- The location of the animation. *
  468. * *
  469. * timedelay-- The delay before the animation starts. *
  470. * *
  471. * loop -- The number of times to loop this animation. *
  472. * *
  473. * OUTPUT: none *
  474. * *
  475. * WARNINGS: none *
  476. * *
  477. * HISTORY: *
  478. * 05/31/1994 JLB : Created. *
  479. * 08/03/1994 JLB : Added a delayed affect parameter. *
  480. *=============================================================================================*/
  481. AnimClass::AnimClass(AnimType animnum, COORDINATE coord, unsigned char timedelay, unsigned char loop, bool alt) :
  482. Class(&AnimTypeClass::As_Reference(animnum))
  483. {
  484. Object = 0;
  485. Owner = HOUSE_NONE;
  486. if (Class->Stages == -1) {
  487. ((int&)Class->Stages) = Get_Build_Frame_Count(Class->Get_Image_Data());
  488. }
  489. if (Class->LoopEnd == -1) {
  490. ((int&)Class->LoopEnd) = Class->Stages;
  491. }
  492. if (Class->IsNormalized) {
  493. Set_Rate(Options.Normalize_Delay(Class->Delay));
  494. } else {
  495. Set_Rate(Class->Delay);
  496. }
  497. Set_Stage(0);
  498. Accum = 0;
  499. coord = Adjust_Coord(coord);
  500. Unlimbo(coord);
  501. /*
  502. ** Drop zone smoke always reveals the map around itself.
  503. */
  504. if (*this == ANIM_LZ_SMOKE) {
  505. Map.Sight_From(Coord_Cell(coord), 4, false);
  506. }
  507. /*
  508. ** Determine the time before the first animation process. For time delayed
  509. ** animations, this is the value passed as a parameter.
  510. */
  511. Delay = timedelay;
  512. Loops = (unsigned char)(MAX(loop, 1) * Class->Loops);
  513. Loops = (unsigned char)MAX(Loops, 1);
  514. IsToDelete = false;
  515. IsBrandNew = true;
  516. IsAlternate = alt;
  517. IsInvisible = false;
  518. /*
  519. ** If the animation starts immediately, then play the associated sound effect now.
  520. */
  521. if (!Delay) {
  522. Start();
  523. }
  524. }
  525. /***********************************************************************************************
  526. * AnimClass::~AnimClass -- Destructor for anim objects. *
  527. * *
  528. * This destructor handles removing the animation object from the system. It might require *
  529. * informing any object this animation is attached to that it is no longer attached. *
  530. * *
  531. * INPUT: none *
  532. * *
  533. * OUTPUT: none *
  534. * *
  535. * WARNINGS: none *
  536. * *
  537. * HISTORY: *
  538. * 11/29/1994 JLB : Created. *
  539. *=============================================================================================*/
  540. AnimClass::~AnimClass(void)
  541. {
  542. Validate();
  543. if (GameActive) {
  544. /*
  545. ** If this anim is attached to another object
  546. ** then check to see if this is the last anim attached to it. If this
  547. ** is the case, then inform the object that it is no longer attached to
  548. ** an animation.
  549. */
  550. if (Object) {
  551. ObjectClass * to = Object;
  552. Object = 0;
  553. /*
  554. ** Scan for any other animations that are attached to the object that
  555. ** this animation is attached to. If there are no others, then inform the
  556. ** attached object of this fact.
  557. */
  558. for (int index = 0; index < Anims.Count(); index++) {
  559. if (Anims.Ptr(index)->Object == to) break;
  560. }
  561. /*
  562. ** Tell the object that it is no longer being damaged.
  563. */
  564. if (index != Anims.Count()) {
  565. to->Fire_Out();
  566. }
  567. to->Mark(MARK_OVERLAP_UP);
  568. to->IsAnimAttached = false;
  569. to->Mark(MARK_OVERLAP_DOWN);
  570. Object = to;
  571. }
  572. Limbo();
  573. Object = 0;
  574. }
  575. }
  576. /***********************************************************************************************
  577. * AnimClass::AI -- This is the low level anim processor. *
  578. * *
  579. * This routine is called once per frame per animation. It handles transition between *
  580. * animation frames and marks the map for redraw as necessary. *
  581. * *
  582. * INPUT: none *
  583. * *
  584. * OUTPUT: none *
  585. * *
  586. * WARNINGS: Speed is of upmost importance. *
  587. * *
  588. * HISTORY: *
  589. * 05/31/1994 JLB : Created. *
  590. *=============================================================================================*/
  591. void AnimClass::AI(void)
  592. {
  593. Validate();
  594. /*
  595. ** For ground level based animations (ones that can run slowly as well as
  596. ** occur behind other ground objects) always cause the cell to be redrawn.
  597. */
  598. if (!Delay && Class->IsGroundLayer) {
  599. Map.Refresh_Cells(Coord_Cell(Center_Coord()), Overlap_List());
  600. }
  601. /*
  602. ** Special case check to make sure that building on top of a smoke marker
  603. ** causes the smoke marker to vanish.
  604. */
  605. if (Class->Type == ANIM_LZ_SMOKE && Map[Coord_Cell(Center_Coord())].Cell_Building()) {
  606. IsToDelete = true;
  607. }
  608. /*
  609. ** Delete this animation and bail early if the animation is flagged to be deleted
  610. ** immediately.
  611. */
  612. if (IsToDelete) {
  613. delete this;
  614. return;
  615. }
  616. /*
  617. ** If this is a brand new animation, then don't process it the first logic pass
  618. ** since it might end up skipping the first animation frame before it has had a
  619. ** chance to draw it.
  620. */
  621. if (IsBrandNew) {
  622. IsBrandNew = false;
  623. return;
  624. }
  625. if (Delay) {
  626. Delay--;
  627. if (!Delay) {
  628. Start();
  629. }
  630. } else {
  631. /*
  632. ** This is necessary because there is no recording of animations on the map
  633. ** and thus the animation cannot be intelligently flagged for redraw. Most
  634. ** animations move fast enough that they would need to be redrawn every
  635. ** game frame anyway so this isn't TOO bad.
  636. */
  637. Mark(MARK_CHANGE);
  638. if (StageClass::Graphic_Logic()) {
  639. int stage = Fetch_Stage();
  640. /*
  641. ** If this animation is attached to another object and it is a
  642. ** damaging kind of animation, then do the damage to the other
  643. ** object.
  644. */
  645. if (Object && Class->Damage) {
  646. unsigned int accum = Accum;
  647. accum += Class->Damage;
  648. if (accum > 255) {
  649. /*
  650. ** Administer the damage. If the object was destroyed by this anim,
  651. ** then the attached damaging anim is also destroyed.
  652. */
  653. int damage = accum >> 8;
  654. if (Object->Take_Damage(damage, 0, WARHEAD_FIRE) == RESULT_DESTROYED) {
  655. //Object = 0;
  656. delete this;
  657. return;
  658. }
  659. }
  660. Accum = (unsigned char)(accum & 0x00FF);
  661. }
  662. /*
  663. ** During the biggest stage (covers the most ground), perform any ground altering
  664. ** action required. This masks craters and scorch marks, so that they appear
  665. ** naturally rather than "popping" into existance while in plain sight.
  666. */
  667. if (Class->Start+stage == Class->Biggest) {
  668. Middle();
  669. }
  670. /*
  671. ** Check to see if the last frame has been displayed. If so, then the
  672. ** animation either ends or loops.
  673. */
  674. if ((Loops <= 1 && stage >= Class->Stages) || (Loops > 1 && stage >= Class->LoopEnd-Class->Start)) {
  675. /*
  676. ** Determine if this animation should loop another time. If so, then start the loop
  677. ** but if not, then proceed into the animation termination handler.
  678. */
  679. if (Loops) Loops--;
  680. if (Loops) {
  681. Set_Stage(Class->LoopStart);
  682. } else {
  683. /*
  684. ** The animation should end now, but first check to see if
  685. ** it needs to chain into another animation. If so, then the
  686. ** animation isn't technically over. It metamorphoses into the
  687. ** new form.
  688. */
  689. if (Class->ChainTo != ANIM_NONE) {
  690. // AnimTypeClass const * aptr = &AnimTypeClass::As_Reference(Class->ChainTo);
  691. ((AnimTypeClass const *&)Class) = &AnimTypeClass::As_Reference(Class->ChainTo);
  692. if (Class->IsNormalized) {
  693. Set_Rate(Options.Normalize_Delay(Class->Delay));
  694. } else {
  695. Set_Rate(Class->Delay);
  696. }
  697. Set_Stage(Class->Start);
  698. } else {
  699. delete this;
  700. }
  701. }
  702. }
  703. }
  704. }
  705. }
  706. /***********************************************************************************************
  707. * AnimClass::Attach_To -- Attaches animation to object specified. *
  708. * *
  709. * An animation can be "attached" to an object. In such cases, the animation is rendered *
  710. * as an offset from the center of the object it is attached to. This allows affects such *
  711. * as fire or smoke to be consistently placed on the vehicle it is associated with. *
  712. * *
  713. * INPUT: obj -- Pointer to the object to attach the animation to. *
  714. * *
  715. * OUTPUT: none *
  716. * *
  717. * WARNINGS: none *
  718. * *
  719. * HISTORY: *
  720. * 09/19/1994 JLB : Created. *
  721. *=============================================================================================*/
  722. void AnimClass::Attach_To(ObjectClass * obj)
  723. {
  724. Validate();
  725. if (!obj) return;
  726. obj->Mark(MARK_OVERLAP_UP);
  727. obj->IsAnimAttached = true;
  728. obj->Mark(MARK_OVERLAP_DOWN);
  729. Map.Remove(this, In_Which_Layer());
  730. Object = obj;
  731. Map.Submit(this, In_Which_Layer());
  732. Coord = Coord_Sub(Coord, obj->Center_Coord());
  733. }
  734. /***********************************************************************************************
  735. * AnimClass::In_Which_Layer -- Determines what render layer the anim should be in. *
  736. * *
  737. * Use this routine to find out which display layer (ground or air) that the animation *
  738. * should be in. This information is used to place the animation into the correct display *
  739. * list. *
  740. * *
  741. * INPUT: none *
  742. * *
  743. * OUTPUT: Returns with the layer that the animation should exist in. *
  744. * *
  745. * WARNINGS: none *
  746. * *
  747. * HISTORY: *
  748. * 12/25/1994 JLB : Created. *
  749. *=============================================================================================*/
  750. LayerType AnimClass::In_Which_Layer(void) const
  751. {
  752. Validate();
  753. if (Object || Class->IsGroundLayer) {
  754. return(LAYER_GROUND);
  755. }
  756. return(LAYER_AIR);
  757. }
  758. /***********************************************************************************************
  759. * AnimClass::Start -- Processes initial animation side effects. *
  760. * *
  761. * This routine is called when the animation first starts. Sometimes there are side effects *
  762. * associated with this animation that must occur immediately. Typically, this is the *
  763. * sound effect assigned to this animation. If this animation is supposed to attach itself *
  764. * to any object at its location, then do so at this time as well. *
  765. * *
  766. * INPUT: none *
  767. * *
  768. * OUTPUT: none *
  769. * *
  770. * WARNINGS: none *
  771. * *
  772. * HISTORY: *
  773. * 06/30/1995 JLB : Created. *
  774. *=============================================================================================*/
  775. void AnimClass::Start(void)
  776. {
  777. Validate();
  778. CELL cell = Coord_Cell(Coord);
  779. Mark();
  780. /*
  781. ** Play the sound effect for this animation.
  782. */
  783. Sound_Effect(Class->Sound, Coord);
  784. /*
  785. ** If the stage where collateral effects occur is the first stage of the animation, then
  786. ** perform this action now. Subsiquent checks against this stage value starts with the
  787. ** second frame of the animation.
  788. */
  789. if (!Class->Biggest) {
  790. Middle();
  791. }
  792. /*
  793. ** If this is the kind of animation that sticks to whatever object is in the same
  794. ** location, then attach the animation to the object. If the animation is already
  795. ** attached, then do nothing.
  796. */
  797. if (!Object && Class->IsSticky && Map.In_Radar(cell)) {
  798. UnitClass * unit = Map[cell].Cell_Unit();
  799. if (unit && *unit == UNIT_GUNBOAT) {
  800. Attach_To(unit);
  801. }
  802. }
  803. }
  804. /***********************************************************************************************
  805. * AnimClass::Middle -- Processes any middle events. *
  806. * *
  807. * This routine is called when the animation as reached its largest stage. Typically, this *
  808. * routine is used to cause scorches or craters to appear at a cosmetically pleasing *
  809. * moment. *
  810. * *
  811. * INPUT: none *
  812. * *
  813. * OUTPUT: none *
  814. * *
  815. * WARNINGS: none *
  816. * *
  817. * HISTORY: *
  818. * 06/30/1995 JLB : Created. *
  819. *=============================================================================================*/
  820. void AnimClass::Middle(void)
  821. {
  822. Validate();
  823. CELL cell = Coord_Cell(Center_Coord());
  824. CellClass * cellptr = &Map[cell];
  825. if (Class->Type == ANIM_ATOM_BLAST) {
  826. /*
  827. ** Find someone to blame the explosion on. This is necessary in
  828. ** order to properly enact retribution and record the kill for
  829. ** score purposes.
  830. */
  831. BuildingClass * building = NULL;
  832. TechnoClass * backup = NULL;
  833. if (Owner != HOUSE_NONE) {
  834. for (int index = 0; index < Logic.Count(); index++) {
  835. ObjectClass * obj = Logic[index];
  836. if (obj && obj->Is_Techno() && obj->Owner() == Owner) {
  837. backup = (TechnoClass *)obj;
  838. if (obj->What_Am_I() == RTTI_BUILDING && *((BuildingClass *)obj) == STRUCT_TEMPLE) {
  839. building = (BuildingClass *)obj;
  840. break;
  841. }
  842. }
  843. }
  844. if (!building) building = (BuildingClass *)backup;
  845. }
  846. int radius = 3;
  847. int rawdamage = 200;
  848. if (GameToPlay == GAME_NORMAL) {
  849. radius = 4;
  850. rawdamage = 1000;
  851. Fade_Palette_To(WhitePalette, 30, NULL);
  852. }
  853. for (int x = -radius; x <= radius; x++) {
  854. for (int y = -radius; y <= radius; y++) {
  855. int xpos = Cell_X(cell) + x;
  856. int ypos = Cell_Y(cell) + y;
  857. /*
  858. ** If the potential damage cell is outside of the map bounds,
  859. ** then don't process it. This unusual check method ensures that
  860. ** damage won't wrap from one side of the map to the other.
  861. */
  862. if ((unsigned)xpos > MAP_CELL_W) {
  863. continue;
  864. }
  865. if ((unsigned)ypos > MAP_CELL_H) {
  866. continue;
  867. }
  868. CELL tcell = XY_Cell(xpos, ypos);
  869. if (!Map.In_Radar(tcell)) continue;
  870. int damage = rawdamage / ((ABS(radius)/2)+1);
  871. Explosion_Damage(Cell_Coord(tcell), damage, building, WARHEAD_FIRE);
  872. new SmudgeClass(Random_Pick(SMUDGE_SCORCH1, SMUDGE_SCORCH6), Cell_Coord(tcell));
  873. }
  874. }
  875. Shake_Screen(3);
  876. if (GameToPlay == GAME_NORMAL) {
  877. Fade_Palette_To(GamePalette, 15, NULL);
  878. }
  879. }
  880. /*
  881. ** If this animation leaves scorch marks (e.g., napalm), then do so at this time.
  882. */
  883. if (Class->IsScorcher) {
  884. new SmudgeClass(Random_Pick(SMUDGE_SCORCH1, SMUDGE_SCORCH6), Center_Coord());
  885. }
  886. /*
  887. ** Some animations leave a crater when they occur. Artillery is a good example.
  888. ** Craters always remove the Tiberium where they occur.
  889. */
  890. if (Class->IsCraterForming) {
  891. /*
  892. ** Craters reduce the level of Tiberium in the cell.
  893. */
  894. cellptr->Reduce_Tiberium(6);
  895. /*
  896. ** If there already is a crater in the cell, then just expand the
  897. ** crater.
  898. */
  899. new SmudgeClass(SMUDGE_CRATER1, Center_Coord());
  900. }
  901. /*
  902. ** Flame throwers leave scorch marks in unusual positions. In addition, they leave fire
  903. ** shards in unusual positions as well.
  904. */
  905. if (Class->IsFlameThrower) {
  906. COORDINATE c2 = Coord_Move(Center_Coord(), (DirType)((Class->Type - ANIM_FLAME_N)<<5), 0x00E0);
  907. CELL cell = Coord_Cell(c2);
  908. COORDINATE c3 = Map.Closest_Free_Spot(Coord_Move(Center_Coord(), (DirType)((Class->Type - ANIM_FLAME_N)<<5), 0x0140), true);
  909. c2 = Map.Closest_Free_Spot(c2, true);
  910. if (c3 && (Random_Pick(0, 1) == 1)) {
  911. if (!Map[Coord_Cell(c3)].Cell_Terrain()) {
  912. new AnimClass(ANIM_FIRE_SMALL, c3, 0, 2);
  913. }
  914. }
  915. if (c2 && (Random_Pick(0, 1) == 1)) {
  916. if (!Map[Coord_Cell(c2)].Cell_Terrain()) {
  917. new AnimClass(ANIM_FIRE_SMALL, c2, 0, 2);
  918. }
  919. }
  920. new SmudgeClass(SMUDGE_SCORCH1, c2);
  921. if (c3 && (Random_Pick(0, 1) == 1)) {
  922. if (!Map[Coord_Cell(c3)].Cell_Terrain()) {
  923. new AnimClass(ANIM_SMOKE_M, c3);
  924. }
  925. }
  926. }
  927. AnimClass * newanim;
  928. /*
  929. ** If this animation spawns side effects during its lifetime, then
  930. ** do so now.
  931. */
  932. switch (Class->Type) {
  933. case ANIM_ION_CANNON: {
  934. BuildingClass * building = NULL;
  935. TechnoClass * backup = NULL;
  936. if (Owner != HOUSE_NONE) {
  937. for (int index = 0; index < Logic.Count(); index++) {
  938. ObjectClass * obj = Logic[index];
  939. if (obj && obj->Is_Techno() && obj->Owner() == Owner && !obj->IsInLimbo) {
  940. backup = (TechnoClass *)obj;
  941. if (obj->What_Am_I() == RTTI_BUILDING && *((BuildingClass *)obj) == STRUCT_EYE) {
  942. building = (BuildingClass *)obj;
  943. break;
  944. }
  945. }
  946. }
  947. if (!building) building = (BuildingClass *)backup;
  948. }
  949. Explosion_Damage(Center_Coord(), 600, building, WARHEAD_PB);
  950. }
  951. break;
  952. case ANIM_NAPALM1:
  953. case ANIM_NAPALM2:
  954. case ANIM_NAPALM3:
  955. new AnimClass(ANIM_FIRE_SMALL, Map.Closest_Free_Spot(Coord_Scatter(Center_Coord(), 0x0040), true), 0, ((Random_Pick(0, 1) == 1) ? 1 : 2));
  956. if (Random_Pick(0, 1) == 1) {
  957. new AnimClass(ANIM_FIRE_SMALL, Map.Closest_Free_Spot(Coord_Scatter(Center_Coord(), 0x00A0), true), 0, ((Random_Pick(0, 1) == 1) ? 1 : 2));
  958. }
  959. if (Random_Pick(0, 1) == 1) {
  960. new AnimClass(ANIM_FIRE_MED, Map.Closest_Free_Spot(Coord_Scatter(Center_Coord(), 0x0070), true), 0, ((Random_Pick(0, 1) == 1) ? 1 : 2));
  961. }
  962. break;
  963. case ANIM_FIRE_MED:
  964. case ANIM_FIRE_MED2:
  965. newanim = new AnimClass(ANIM_FIRE_SMALL, Center_Coord(), 0, ((Random_Pick(0, 1) == 1) ? 1 : 2));
  966. if (newanim && Object) {
  967. newanim->Attach_To(Object);
  968. }
  969. break;
  970. default:
  971. break;
  972. }
  973. }
  974. /***********************************************************************************************
  975. * AnimClass::As_Target -- Converts the animation into a target value. *
  976. * *
  977. * This support routine is used to convert the animation (as a pointer) into a target *
  978. * value (which is a number). *
  979. * *
  980. * INPUT: none *
  981. * *
  982. * OUTPUT: Returns the animation as a target value. *
  983. * *
  984. * WARNINGS: none *
  985. * *
  986. * HISTORY: *
  987. * 09/08/1994 JLB : Created. *
  988. *=============================================================================================*/
  989. TARGET AnimClass::As_Target(void) const
  990. {
  991. Validate();
  992. return(Build_Target(KIND_ANIMATION, Anims.ID(this)));
  993. }
  994. /***************************************************************************
  995. * AnimClass::Adjust_Coord -- Adjusts anim coordinates *
  996. * *
  997. * INPUT: none *
  998. * *
  999. * OUTPUT: none *
  1000. * *
  1001. * HISTORY: *
  1002. * 05/17/1995 PWG : Created. *
  1003. *=========================================================================*/
  1004. COORDINATE AnimClass::Adjust_Coord(COORDINATE coord)
  1005. {
  1006. Validate();
  1007. int x,y;
  1008. switch (Class->Type) {
  1009. case ANIM_ATOM_DOOR:
  1010. x = -1;
  1011. y = -36;
  1012. break;
  1013. default:
  1014. return(coord);
  1015. }
  1016. COORDINATE addval = XYPixel_Coord(x, y);
  1017. coord = Coord_Add(coord, addval);
  1018. return(coord);
  1019. }
  1020. /***********************************************************************************************
  1021. * AnimClass::Detach -- Remove animation if attached to target. *
  1022. * *
  1023. * This routine is called when the specified target is being removed from the game. If this *
  1024. * animation happens to be attached to this object, then the animation must be remove as *
  1025. * well. *
  1026. * *
  1027. * INPUT: target -- The target that is about to be destroyed. *
  1028. * *
  1029. * all -- Is the target being destroyed RIGHT NOW? If not, then it will be *
  1030. * destroyed soon. In that case, the animation should continue to remain *
  1031. * attached for cosmetic reasons. *
  1032. * *
  1033. * OUTPUT: none *
  1034. * *
  1035. * WARNINGS: none *
  1036. * *
  1037. * HISTORY: *
  1038. * 06/30/1995 JLB : Created. *
  1039. * 07/02/1995 JLB : Detach is a precursor to animation destruction. *
  1040. *=============================================================================================*/
  1041. void AnimClass::Detach(TARGET target, bool all)
  1042. {
  1043. Validate();
  1044. if (Object && Object->As_Target() == target && all) {
  1045. Map.Remove(this, In_Which_Layer());
  1046. Object = NULL;
  1047. IsToDelete = true;
  1048. }
  1049. }