File: fireball.cpp

package info (click to toggle)
postal1 2015.git20250526%2Bds-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 14,024 kB
  • sloc: cpp: 130,877; ansic: 38,942; python: 874; makefile: 351; sh: 61
file content (1105 lines) | stat: -rw-r--r-- 36,458 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 RWS Inc, All Rights Reserved
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of version 2 of the GNU General Public License as published by
// the Free Software Foundation
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
// fireball.cpp
// Project: Nostril (aka Postal)
//
// This module implements the CFireball weapon class which is a burning flame
//	for several different effects and weapons.
// 
//
// History:
//		01/17/97 BRH	Started this fireball ammo for use in making a flamethrower
//
//		04/25/97	JMI	PreLoad() was not properly assigning into sResult.
//
//		04/29/97	JMI	Changed name of ProcessMessages() to 
//							ProcessFireballMessages() to avoid conflicts with 
//							CWeapon's ProcessMessages().
//							Also, Update() was not moving the m_smash (it was merely
//							set the one time in Init()) which was causing it to not
//							collide with anything (I guess it might've collided with
//							things hovering around the upper, left corner of the realm).
//
//		05/29/97	JMI	Removed ASSERT on m_pRealm->m_pAttribMap which no longer
//							exists.
//
//		06/11/97 BRH	Passed on shooter ID to the message it sends when it burns
//							someone.
//
//		06/17/97	JMI	Converted all occurrences of rand() to GetRand() and
//							srand() to SeedRand().
//
//		07/02/97 BRH	Added CFirestream object that creates several CFireball
//							objects.
//
//		07/04/97 BRH	Added auto alpha based on the time to live.
//
//		07/07/97 BRH	Fixed casing on MIN to compile on both PC and Mac.
//
//		07/08/97	JMI	Put in code in Render() to avoid an unlikely divide by zero.
//							Still have a divide by zero release mode problem though.
//							Also, there was initialization typo in CFireball::Setup().
//
//		07/08/97	JMI	Fixed Render() to distribute the homogeneous alpha level
//							better.  Still needs tuning.
//
//					JMI	Made heavier again.
//
//		07/09/97	JMI	Now uses m_pRealm->Make2dResPath() to get the fullpath
//							for 2D image components.
//
//		07/09/97	JMI	Changed Preload() to take a pointer to the calling realm
//							as a parameter.
//
//		07/10/97	JMI	Now uses alpha mask and level for animation.
//
//		07/12/97 BRH	Added IsPathClear check to path of fireball since its
//							velocity is so high that it can pass through walls
//							otherwise.
//
//		07/13/97 BRH	Added a check to the Firestream creation of the
//							fireballs to make sure that the iniial position is
//							not in a wall.  If it is, then the other two fireballs
//							with a further offset should not be created, otherwise
//							it will shoot through walls.  
//
//							Also changed the alpha to use 1 mask and tuned the levels
//							so that you can see it at the gun end.
//
//		07/14/97	JMI	Now will detach itself from its parent via 
//							m_sprite.m_psprParent->RemoveChild() in the rare event 
//							that it is still parented going into the Render().
//							Also, threw in some random to the m_dRot before they are
//							separated from the CFirestream.
//
//		07/27/97	JMI	Changed to use Z position (i.e., X/Z plane) instead of
//							Y2 position (i.e., viewing plane) position for draw 
//							priority.
//
//		07/30/97	JMI	Changed several deletes that occurred just before reading
//							instantiable member variables.  Now both 
//							ProcessFireballMessage() functions (there are two (one for
//							CFireballs and one for CFireStreams) ) do NOT delete this
//							when they receive a delete message.  They instead set the
//							state to delete and allow the calling function to do the
//							delete.  The problem here was that the calling function,
//							Update(), needed to look at one more member var, m_eState,
//							before returning, but, at that point (that is, after the
//							delete) this was invalid.  This works find though when
//							using SmartHeap I think b/c of SmartHeap's bounds checking
//							areas to detect overwrites.  The Alpha does not have these
//							areas (no SmartHeap) and, so, crashes easily in these cases.
//							There was also a problem in Update() where it deleted this
//							but did not return right away and one more member var access
//							was done after that point (probably added later).  Fixed.
//
//		07/30/97	JMI	Now uses m_dHorizVel for its velocity which is initially 
//							set to ms_dFireVelocity but can be overridden after the
//							Setup() call.
//
//		08/08/97	JMI	Now CFirestream::ProcessFireballMessages() passes on
//							delete messages to any fireballs it owns.
//
//		08/08/97	JMI	Changed m_pFireball1, 2, & 3 to m_idFireball1, 2, & 3.
//
////////////////////////////////////////////////////////////////////////////////
#define FIREBALL_CPP

#include "RSPiX.h"
#include <math.h>

#include "fireball.h"
#include "game.h"
#include "reality.h"


////////////////////////////////////////////////////////////////////////////////
// Macros/types/etc.
////////////////////////////////////////////////////////////////////////////////

#define SMALL_FILE			"tinyfire.aan"

#define MIN_ALPHA				30
#define MAX_ALPHA				200
// Note the sum of these two values should not exceed 255

// Gets a random between -range / 2 and range / 2.
#define RAND_SWAY(sway)	((GetRand() % sway) - sway / 2)

#define FIREBALL_SWAY		15

////////////////////////////////////////////////////////////////////////////////
// Variables/data
////////////////////////////////////////////////////////////////////////////////

// Let this auto-init to 0
int16_t CFirestream::ms_sFileCount;
int16_t CFirestream::ms_sOffset1 = 12;		// pixels from 1st to 2nd fireball
int16_t CFirestream::ms_sOffset2 = 24;	// pixels from 1st to 3rd fireball

////////////////////////////////////////////////////////////////////////////////
// Load object (should call base class version!)
////////////////////////////////////////////////////////////////////////////////
int16_t CFirestream::Load(										// Returns 0 if successfull, non-zero otherwise
	RFile* pFile,											// In:  File to load from
	bool bEditMode,										// In:  True for edit mode, false otherwise
	int16_t sFileCount,										// In:  File count (unique per file, never 0)
	uint32_t	ulFileVersion)									// In:  Version of file format to load.
	{
	int16_t sResult = CThing::Load(pFile, bEditMode, sFileCount, ulFileVersion);

	if (sResult == 0)
	{
		// Load common data just once per file (not with each object)
		if (ms_sFileCount != sFileCount)
		{
			ms_sFileCount = sFileCount;

			// Load static data.
			switch (ulFileVersion)
			{
				default:
				case 1:
					break;
			}
		}

		// Load instance data.
		switch (ulFileVersion)
		{
			default:
			case 1:
				break;
		}

		// Make sure there were no file errors or format errors . . .
		if (!pFile->Error() && sResult == 0)
		{
		}
		else
		{
			sResult = -1;
			TRACE("CFirestream::Load(): Error reading from file!\n");
		}
	}
	else
	{
		TRACE("CFirestream::Load():  CThing::Load() failed.\n");
	}

	return sResult;
}


////////////////////////////////////////////////////////////////////////////////
// Save object (should call base class version!)
////////////////////////////////////////////////////////////////////////////////
int16_t CFirestream::Save(										// Returns 0 if successfull, non-zero otherwise
	RFile* pFile,											// In:  File to save to
	int16_t sFileCount)										// In:  File count (unique per file, never 0)
{
	int16_t	sResult	= CThing::Save(pFile, sFileCount);
	if (sResult == 0)
	{
		// Save common data just once per file (not with each object)
		if (ms_sFileCount != sFileCount)
		{
			ms_sFileCount = sFileCount;

			// Save static data
		}

	}
	else
	{
		TRACE("CFirestream::Save(): CThing::Save() failed.\n");
	}

	return sResult;
}


////////////////////////////////////////////////////////////////////////////////
// Startup object
////////////////////////////////////////////////////////////////////////////////
int16_t CFirestream::Startup(void)								// Returns 0 if successfull, non-zero otherwise
{
	return Init();
}

////////////////////////////////////////////////////////////////////////////////
// Shutdown object
////////////////////////////////////////////////////////////////////////////////
int16_t CFirestream::Shutdown(void)							// Returns 0 if successfull, non-zero otherwise
{
	return 0;
}

////////////////////////////////////////////////////////////////////////////////
// Suspend object
////////////////////////////////////////////////////////////////////////////////
void CFirestream::Suspend(void)
{
	m_sSuspend++;
}

////////////////////////////////////////////////////////////////////////////////
// Resume object
////////////////////////////////////////////////////////////////////////////////
void CFirestream::Resume(void)
{
	m_sSuspend--;

	// If we're actually going to start updating again, we need to reset
	// the time so as to ignore any time that passed while we were suspended.
	// This method is far from precise, but I'm hoping it's good enough.
	if (m_sSuspend == 0)
		m_lPrevTime = m_pRealm->m_time.GetGameTime();
}


////////////////////////////////////////////////////////////////////////////////
// Update object
////////////////////////////////////////////////////////////////////////////////
void CFirestream::Update(void)
{
	int32_t lThisTime;

	if (!m_sSuspend)
	{
		lThisTime = m_pRealm->m_time.GetGameTime();

		// See if we killed ourselves
		if (ProcessFireballMessages() == State_Deleted)
			{
			delete this;
			return;
			}

		CFireball*	pfireball1	= NULL;
		CFireball*	pfireball2	= NULL;
		CFireball*	pfireball3	= NULL;

		// Update the other fireballs
		if (m_pRealm->m_idbank.GetThingByID((CThing**)&pfireball1, m_idFireball1) == 0)
			{
			pfireball1->m_dX		= m_dX;
			pfireball1->m_dY		= m_dY;
			pfireball1->m_dZ		= m_dZ;
			pfireball1->m_dRot	= rspMod360(m_dRot + RAND_SWAY(FIREBALL_SWAY) );
			}
		else
			{
			m_idFireball1	= CIdBank::IdNil;
			}

		if (m_pRealm->m_idbank.GetThingByID((CThing**)&pfireball2, m_idFireball2) == 0)
			{
			pfireball2->m_dX		= m_dX + COSQ[(int16_t) m_dRot] * ms_sOffset1;
			pfireball2->m_dY		= m_dY;
			pfireball2->m_dZ		= m_dZ - SINQ[(int16_t) m_dRot] * ms_sOffset1;
			pfireball2->m_dRot	= rspMod360(m_dRot + RAND_SWAY(FIREBALL_SWAY) );
			}
		else
			{
			m_idFireball2	= CIdBank::IdNil;
			}

		if (m_pRealm->m_idbank.GetThingByID((CThing**)&pfireball3, m_idFireball3) == 0)
			{
			pfireball3->m_dX		= m_dX + COSQ[(int16_t) m_dRot] * ms_sOffset2;
			pfireball3->m_dY		= m_dY;
			pfireball3->m_dZ		= m_dZ - SINQ[(int16_t) m_dRot] * ms_sOffset2;
			pfireball3->m_dRot	= rspMod360(m_dRot + RAND_SWAY(FIREBALL_SWAY) );
			}
		else
			{
			m_idFireball3	= CIdBank::IdNil;
			}

		if (m_eState == CWeapon::State_Fire)
		{
			if (pfireball1)
				pfireball1->m_eState = CWeapon::State_Fire;
			if (pfireball2)
				pfireball2->m_eState = CWeapon::State_Fire;
			if (pfireball3)
				pfireball3->m_eState = CWeapon::State_Fire;
			delete this;
			return;
		}
	}
}


////////////////////////////////////////////////////////////////////////////////
// Render object
////////////////////////////////////////////////////////////////////////////////
void CFirestream::Render(void)
{
	// If we have a parent . . .
	if (m_sprite.m_psprParent)
		{
		// Get outta there!
		m_sprite.m_psprParent->RemoveChild(&m_sprite);
		}

	// This should never ever be rendered.
	ASSERT(m_sprite.m_psprParent == NULL);
	ASSERT( (m_sprite.m_sInFlags & CSprite::PrivInserted) == 0);
}

////////////////////////////////////////////////////////////////////////////////
// Setup
////////////////////////////////////////////////////////////////////////////////

int16_t CFirestream::Setup(									// Returns 0 if successfull, non-zero otherwise
	int16_t sX,												// In:  New x coord
	int16_t sY,												// In:  New y coord
	int16_t sZ,												// In:  New z coord
	int16_t sDir,												// In:  Direction of travel
	int32_t lTimeToLive,										// In:  Number of milliseconds to burn, default 1sec
	U16 u16ShooterID)										// In:  Shooter's ID so you don't hit him
{
	int16_t sResult = 0;
	double dX;
	double dZ;
	
	// Use specified position
	m_dX = (double)sX;
	m_dY = (double)sY;
	m_dZ = (double)sZ;
	m_dRot = sDir;
	m_lPrevTime = m_pRealm->m_time.GetGameTime();
	m_u16ShooterID = u16ShooterID;

	// Make sure that the starting positions are valid before creating
	// fireballs here, otherwise they will shoot through walls.
	dX = m_dX + COSQ[(int16_t) m_dRot] * ms_sOffset2;	// Second interval 
	dZ = m_dZ - SINQ[(int16_t) m_dRot] * ms_sOffset2;	
	if (m_pRealm->IsPathClear(		// Returns true, if the entire path is clear.                 
											// Returns false, if only a portion of the path is clear.     
											// (see *psX, *psY, *psZ).                                    
			(int16_t) m_dX, 				// In:  Starting X.                                           
			(int16_t) m_dY, 				// In:  Starting Y.                                           
			(int16_t) m_dZ, 				// In:  Starting Z.                                           
			3.0, 							// In:  Rate at which to scan ('crawl') path in pixels per    
											// iteration.                                                 
											// NOTE: Values less than 1.0 are inefficient.                
											// NOTE: We scan terrain using GetHeight()                    
											// at only one pixel.                                         
											// NOTE: We could change this to a speed in pixels per second 
											// where we'd assume a certain frame rate.                    
			(int16_t) dX,		 			// In:  Destination X.                                        
			(int16_t) dZ,					// In:  Destination Z.                                        
			0,								// In:  Max traverser can step up.                      
			NULL,							// Out: If not NULL, last clear point on path.                
			NULL,							// Out: If not NULL, last clear point on path.                
			NULL,							// Out: If not NULL, last clear point on path.                
			false) )						// In:  If true, will consider the edge of the realm a path
											// inhibitor.  If false, reaching the edge of the realm    
											// indicates a clear path.                                 
	{
		m_lTimeToLive = m_pRealm->m_time.GetGameTime() + lTimeToLive;

		CFireball*	pfireball;
		if (CThing::ConstructWithID(CThing::CFireballID, m_pRealm, (CThing**) &pfireball) == 0)
		{
			pfireball->Setup(m_dX, m_dY, m_dZ, sDir, lTimeToLive, u16ShooterID);
			m_idFireball1	= pfireball->GetInstanceID();
		}

		dX = m_dX + COSQ[(int16_t) m_dRot] * ms_sOffset1;	// First interval
		dZ = m_dZ - SINQ[(int16_t) m_dRot] * ms_sOffset1;	
		if (CThing::ConstructWithID(CThing::CFireballID, m_pRealm, (CThing**) &pfireball) == 0)
		{
			pfireball->Setup(dX, m_dY, dZ, sDir, lTimeToLive, u16ShooterID);
			m_idFireball2	= pfireball->GetInstanceID();
		}

		dX = m_dX + COSQ[(int16_t) m_dRot] * ms_sOffset2;	// Second interval 
		dZ = m_dZ - SINQ[(int16_t) m_dRot] * ms_sOffset2;	
		if (CThing::ConstructWithID(CThing::CFireballID, m_pRealm, (CThing**) &pfireball) == 0)
		{
			pfireball->Setup(dX, m_dY, dZ, sDir, lTimeToLive, u16ShooterID);
			m_idFireball3	= pfireball->GetInstanceID();
		}

		if (sResult == SUCCESS)
			sResult = Init();
	}

	return sResult;

}

////////////////////////////////////////////////////////////////////////////////
// Init
////////////////////////////////////////////////////////////////////////////////

int16_t CFirestream::Init(void)
{
	int16_t sResult = SUCCESS;
	
	return sResult;
}

////////////////////////////////////////////////////////////////////////////////
// Called by editor to init new object at specified position
////////////////////////////////////////////////////////////////////////////////
int16_t CFirestream::EditNew(									// Returns 0 if successfull, non-zero otherwise
	int16_t sX,												// In:  New x coord
	int16_t sY,												// In:  New y coord
	int16_t sZ)												// In:  New z coord
{
	int16_t sResult = 0;
	
	// Use specified position
	m_dX = (double)sX;
	m_dY = (double)sY;
	m_dZ = (double)sZ;
	m_lTimer = GetRand(); //m_pRealm->m_time.GetGameTime() + 1000;
	m_lPrevTime = m_pRealm->m_time.GetGameTime();

	return sResult;
}


////////////////////////////////////////////////////////////////////////////////
// Called by editor to modify object
////////////////////////////////////////////////////////////////////////////////
int16_t CFirestream::EditModify(void)
{
	return 0;
}


////////////////////////////////////////////////////////////////////////////////
// Called by editor to move object to specified position
////////////////////////////////////////////////////////////////////////////////
int16_t CFirestream::EditMove(									// Returns 0 if successfull, non-zero otherwise
	int16_t sX,												// In:  New x coord
	int16_t sY,												// In:  New y coord
	int16_t sZ)												// In:  New z coord
{
	m_dX = (double)sX;
	m_dY = (double)sY;
	m_dZ = (double)sZ;

	return 0;
}


////////////////////////////////////////////////////////////////////////////////
// Called by editor to update object
////////////////////////////////////////////////////////////////////////////////
void CFirestream::EditUpdate(void)
{
}


////////////////////////////////////////////////////////////////////////////////
// Called by editor to render object
////////////////////////////////////////////////////////////////////////////////
void CFirestream::EditRender(void)
{
	// In some cases, object's might need to do a special-case render in edit
	// mode because Startup() isn't called.  In this case it doesn't matter, so
	// we can call the normal Render().
	Render();
}



////////////////////////////////////////////////////////////////////////////////
// Preload - basically trick the resource manager into caching resources for fire
//				 animations before play begins so that when a fire is set for
//				 the first time, there won't be a delay while it loads.
////////////////////////////////////////////////////////////////////////////////

int16_t CFirestream::Preload(
	CRealm* /*prealm*/)			// In:  Calling realm.
{
return 0;
}


////////////////////////////////////////////////////////////////////////////////
// ProcessFireballMessages
////////////////////////////////////////////////////////////////////////////////

CFirestream::CFirestreamState CFirestream::ProcessFireballMessages(void)
{
	CFirestreamState eNewState = State_Idle;

	GameMessage msg;

	if (m_MessageQueue.DeQ(&msg) == true)
	{
		switch(msg.msg_Generic.eType)
		{
			case typeObjectDelete:
				{
				// Pass the message on . . .
				SendThingMessage(&msg, m_idFireball1);
				SendThingMessage(&msg, m_idFireball2);
				SendThingMessage(&msg, m_idFireball3);

				m_MessageQueue.Empty();
				return State_Deleted;
				break;
				}
		}
	}
	// Dump the rest of the messages
	m_MessageQueue.Empty();

	return eNewState;
}



////////////////////////////////// Fireball ////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Macros/types/etc.
////////////////////////////////////////////////////////////////////////////////

#define SMALL_FILE			"tinyfire.aan"

////////////////////////////////////////////////////////////////////////////////
// Variables/data
////////////////////////////////////////////////////////////////////////////////

// Let this auto-init to 0
int16_t CFireball::ms_sFileCount;
int16_t CFireball::ms_sSmallRadius = 8;
int32_t  CFireball::ms_lCollisionTime = 250;			// Check for collisions this often
double CFireball::ms_dFireVelocity = 300;

////////////////////////////////////////////////////////////////////////////////
// Load object (should call base class version!)
////////////////////////////////////////////////////////////////////////////////
int16_t CFireball::Load(										// Returns 0 if successfull, non-zero otherwise
	RFile* pFile,											// In:  File to load from
	bool bEditMode,										// In:  True for edit mode, false otherwise
	int16_t sFileCount,										// In:  File count (unique per file, never 0)
	uint32_t	ulFileVersion)									// In:  Version of file format to load.
	{
	int16_t sResult = CThing::Load(pFile, bEditMode, sFileCount, ulFileVersion);

	if (sResult == 0)
	{
		// Load common data just once per file (not with each object)
		if (ms_sFileCount != sFileCount)
		{
			ms_sFileCount = sFileCount;

			// Load static data.
			switch (ulFileVersion)
			{
				default:
				case 1:
					break;
			}
		}

		// Load instance data.
		switch (ulFileVersion)
		{
			default:
			case 1:
				break;
		}

		// Make sure there were no file errors or format errors . . .
		if (!pFile->Error() && sResult == 0)
		{
			// Get resources
			sResult = GetResources();
		}
		else
		{
			sResult = -1;
			TRACE("CFireball::Load(): Error reading from file!\n");
		}
	}
	else
	{
		TRACE("CFireball::Load():  CThing::Load() failed.\n");
	}

	return sResult;
}


////////////////////////////////////////////////////////////////////////////////
// Save object (should call base class version!)
////////////////////////////////////////////////////////////////////////////////
int16_t CFireball::Save(										// Returns 0 if successfull, non-zero otherwise
	RFile* pFile,											// In:  File to save to
	int16_t sFileCount)										// In:  File count (unique per file, never 0)
{
	int16_t	sResult	= CThing::Save(pFile, sFileCount);
	if (sResult == 0)
	{
		// Save common data just once per file (not with each object)
		if (ms_sFileCount != sFileCount)
		{
			ms_sFileCount = sFileCount;

			// Save static data
		}

	}
	else
	{
		TRACE("CFireball::Save(): CThing::Save() failed.\n");
	}

	return sResult;
}


////////////////////////////////////////////////////////////////////////////////
// Startup object
////////////////////////////////////////////////////////////////////////////////
int16_t CFireball::Startup(void)								// Returns 0 if successfull, non-zero otherwise
{
	return Init();
}

////////////////////////////////////////////////////////////////////////////////
// Shutdown object
////////////////////////////////////////////////////////////////////////////////
int16_t CFireball::Shutdown(void)							// Returns 0 if successfull, non-zero otherwise
{
	return 0;
}

////////////////////////////////////////////////////////////////////////////////
// Suspend object
////////////////////////////////////////////////////////////////////////////////
void CFireball::Suspend(void)
{
	m_sSuspend++;
}

////////////////////////////////////////////////////////////////////////////////
// Resume object
////////////////////////////////////////////////////////////////////////////////
void CFireball::Resume(void)
{
	m_sSuspend--;

	// If we're actually going to start updating again, we need to reset
	// the time so as to ignore any time that passed while we were suspended.
	// This method is far from precise, but I'm hoping it's good enough.
	if (m_sSuspend == 0)
		m_lPrevTime = m_pRealm->m_time.GetGameTime();
}


////////////////////////////////////////////////////////////////////////////////
// Update object
////////////////////////////////////////////////////////////////////////////////
void CFireball::Update(void)
{
	int32_t lThisTime;
	double dSeconds;
	double dDistance;
	double dNewX;
	double dNewZ;

	if (!m_sSuspend)
	{
		lThisTime = m_pRealm->m_time.GetGameTime();
		m_lAnimTime += lThisTime - m_lPrevTime;

		// See if we killed ourselves
		if (ProcessFireballMessages() == State_Deleted)
			{
			delete this;
			return;
			}

		switch (m_eState)
		{
			case State_Fire:
				if (lThisTime < m_lTimeToLive)
				{
					if (m_bMoving)
					{
						// Update position using wind direction and velocity
						dSeconds = ((double) lThisTime - (double) m_lPrevTime) / 1000.0;
						// Apply internal velocity.
						dDistance	= m_dHorizVel * dSeconds;
						dNewX	= m_dX + COSQ[(int16_t) m_dRot] * dDistance;
						dNewZ	= m_dZ - SINQ[(int16_t) m_dRot] * dDistance;

						// Check attribute map for walls, and if you hit a wall, 
						// set the timer so you will die off next time around.
						int16_t sHeight = m_pRealm->GetHeight((int16_t) dNewX, (int16_t) dNewZ);
						// If it hits a wall taller than itself, then it will rotate in the
						// predetermined direction until it is free to move.
						if ((int16_t) m_dY < sHeight ||
							!m_pRealm->IsPathClear(	// Returns true, if the entire path is clear.                 
															// Returns false, if only a portion of the path is clear.     
															// (see *psX, *psY, *psZ).                                    
							(int16_t) m_dX, 				// In:  Starting X.                                           
							(int16_t) m_dY, 				// In:  Starting Y.                                           
							(int16_t) m_dZ, 				// In:  Starting Z.                                           
							3.0, 							// In:  Rate at which to scan ('crawl') path in pixels per    
															// iteration.                                                 
															// NOTE: Values less than 1.0 are inefficient.                
															// NOTE: We scan terrain using GetHeight()                    
															// at only one pixel.                                         
															// NOTE: We could change this to a speed in pixels per second 
															// where we'd assume a certain frame rate.                    
							(int16_t) dNewX, 			// In:  Destination X.                                        
							(int16_t) dNewZ,				// In:  Destination Z.                                        
							0,								// In:  Max traverser can step up.                      
							NULL,							// Out: If not NULL, last clear point on path.                
							NULL,							// Out: If not NULL, last clear point on path.                
							NULL,							// Out: If not NULL, last clear point on path.                
							false) 						// In:  If true, will consider the edge of the realm a path
															// inhibitor.  If false, reaching the edge of the realm    
															// indicates a clear path.                                 
						)
						{
							// Stop moving and fix yourself on a random spot on the wall.
							m_bMoving = false;				
							m_dX += (-3 + GetRand() % 7);
							m_dY += (-3 + GetRand() % 7);
							m_dZ += (-3 + GetRand() % 7);

							// Update sphere
							m_smash.m_sphere.sphere.X = m_dX;
							m_smash.m_sphere.sphere.Y = m_dY;
							m_smash.m_sphere.sphere.Z = m_dZ;
						}
						else
						{
							m_dX = dNewX;
							m_dZ = dNewZ;

							// Update sphere
							m_smash.m_sphere.sphere.X = m_dX;
							m_smash.m_sphere.sphere.Y = m_dY;
							m_smash.m_sphere.sphere.Z = m_dZ;

							// Check for collisions
							CSmash* pSmashed = NULL;
							GameMessage msg;
							msg.msg_Burn.eType = typeBurn;
							msg.msg_Burn.sPriority = 0;
							msg.msg_Burn.sDamage = 10;
							msg.msg_Burn.u16ShooterID = m_u16ShooterID;
							m_pRealm->m_smashatorium.QuickCheckReset(&m_smash, m_u32CollideIncludeBits,
																				  m_u32CollideDontcareBits, 
																				  m_u32CollideExcludeBits);
							while (m_pRealm->m_smashatorium.QuickCheckNext(&pSmashed))
								if (pSmashed->m_pThing->GetInstanceID() != m_u16ShooterID)
									SendThingMessage(&msg, pSmashed->m_pThing);				
						}
					}

				}
				else
				{
					delete this;
					return;
				}
				break;

			default:
				break;
		}
		m_lPrevTime = lThisTime;
	}
}


////////////////////////////////////////////////////////////////////////////////
// Render object
////////////////////////////////////////////////////////////////////////////////
void CFireball::Render(void)
{
	
	CAlphaAnim* pAnim;

	pAnim = (CAlphaAnim*) m_pAnimChannel->GetAtTime(m_lAnimTime % m_pAnimChannel->TotalTime());

	if (pAnim) // && m_sCurrentAlphaChannel >= 0)
	{
		// No special flags
		m_sprite.m_sInFlags = 0; 

		// Map from 3d to 2d coords
		Map3Dto2D(m_dX, m_dY, m_dZ, &(m_sprite.m_sX2), &(m_sprite.m_sY2) );
		// Offset by animations 2D offsets.
		m_sprite.m_sX2	+= pAnim->m_sX;
		m_sprite.m_sY2	+= pAnim->m_sY;

		// Priority is based on our Z position.
		m_sprite.m_sPriority = m_dZ;

		// Layer should be based on info we get from attribute map.
		m_sprite.m_sLayer = CRealm::GetLayerViaAttrib(m_pRealm->GetLayer((int16_t) m_dX, (int16_t) m_dZ));

//		m_sprite.m_sAlphaLevel = 200;
		if (m_lTotalFlameTime == 0)
			{
			// Safety.
			m_lTotalFlameTime = 500;
			}

//		m_sprite.m_sAlphaLevel = MIN((long)255, (long) (((m_lTimeToLive - m_pRealm->m_time.GetGameTime()) / m_lTotalFlameTime) * 255));

		// Copy the color info and the alpha channel to the Alpha Sprite
		m_sprite.m_pImage = &(pAnim->m_imColor);

		// Now there is only 1 alpha mask.
		m_sCurrentAlphaChannel = 0; //MIN(m_sCurrentAlphaChannel, (short) (m_sTotalAlphaChannels - 1));
		m_sprite.m_pimAlpha = &(pAnim->m_pimAlphaArray[0]);
		// Adjust level between 0 and max so it gets more opaque with time.
		m_sprite.m_sAlphaLevel = MIN_ALPHA + MAX_ALPHA - (MAX_ALPHA * (m_lTimeToLive - m_pRealm->m_time.GetGameTime()) ) / m_lTotalFlameTime ;
		// Keep in range.
		if (m_sprite.m_sAlphaLevel < 0)
			m_sprite.m_sAlphaLevel = 0;
		else if (m_sprite.m_sAlphaLevel > MAX_ALPHA)
			m_sprite.m_sAlphaLevel = MAX_ALPHA;

		// Update sprite in scene
		m_pRealm->m_scene.UpdateSprite(&m_sprite);
		
	}
}

////////////////////////////////////////////////////////////////////////////////
// Setup
////////////////////////////////////////////////////////////////////////////////

int16_t CFireball::Setup(									// Returns 0 if successfull, non-zero otherwise
	int16_t sX,												// In:  New x coord
	int16_t sY,												// In:  New y coord
	int16_t sZ,												// In:  New z coord
	int16_t sDir,												// In:  Direction of travel
	int32_t lTimeToLive,										// In:  Number of milliseconds to burn, default 1sec
	U16 u16ShooterID)										// In:  Shooter's ID so you don't hit him
{
	int16_t sResult = 0;
	
	// Use specified position
	m_dX = (double)sX;
	m_dY = (double)sY;
	m_dZ = (double)sZ;
	m_dRot = sDir;
	m_lPrevTime = m_pRealm->m_time.GetGameTime();
	m_lCollisionTimer = m_lPrevTime + ms_lCollisionTime;
	m_u16ShooterID = u16ShooterID;
	m_lAnimTime = GetRandom();
	m_dHorizVel	= ms_dFireVelocity; 

	m_lTotalFlameTime = lTimeToLive;
	m_lTimeToLive = m_pRealm->m_time.GetGameTime() + lTimeToLive;
	m_sCurrentAlphaChannel = 0;
	
	// Load resources
	sResult = GetResources();

	if (sResult == SUCCESS)
		sResult = Init();

	return sResult;

}

////////////////////////////////////////////////////////////////////////////////
// Init
////////////////////////////////////////////////////////////////////////////////

int16_t CFireball::Init(void)
{
	int16_t sResult = SUCCESS;
	CAlphaAnim* pAnim = NULL;


	// Update sphere
	m_smash.m_sphere.sphere.X = m_dX;
	m_smash.m_sphere.sphere.Y = m_dY;
	m_smash.m_sphere.sphere.Z = m_dZ;
	m_smash.m_sphere.sphere.lRadius = ms_sSmallRadius;
	m_smash.m_bits = CSmash::Fire;
	m_smash.m_pThing = this;
	// Update the smash
	m_pRealm->m_smashatorium.Update(&m_smash);
	m_eState = CWeapon::State_Idle;

	// Set the collision bits
	m_u32CollideIncludeBits = CSmash::Character | CSmash::Barrel | CSmash::Mine | CSmash::Misc;
	m_u32CollideDontcareBits = CSmash::Good | CSmash::Bad;
	m_u32CollideExcludeBits = 0;

	return sResult;
}

////////////////////////////////////////////////////////////////////////////////
// Called by editor to init new object at specified position
////////////////////////////////////////////////////////////////////////////////
int16_t CFireball::EditNew(									// Returns 0 if successfull, non-zero otherwise
	int16_t sX,												// In:  New x coord
	int16_t sY,												// In:  New y coord
	int16_t sZ)												// In:  New z coord
{
	int16_t sResult = 0;
	
	// Use specified position
	m_dX = (double)sX;
	m_dY = (double)sY;
	m_dZ = (double)sZ;
	m_lTimer = GetRand(); //m_pRealm->m_time.GetGameTime() + 1000;
	m_lPrevTime = m_pRealm->m_time.GetGameTime();

	// Load resources
	sResult = GetResources();

	return sResult;
}


////////////////////////////////////////////////////////////////////////////////
// Called by editor to modify object
////////////////////////////////////////////////////////////////////////////////
int16_t CFireball::EditModify(void)
{
	return 0;
}


////////////////////////////////////////////////////////////////////////////////
// Called by editor to move object to specified position
////////////////////////////////////////////////////////////////////////////////
int16_t CFireball::EditMove(									// Returns 0 if successfull, non-zero otherwise
	int16_t sX,												// In:  New x coord
	int16_t sY,												// In:  New y coord
	int16_t sZ)												// In:  New z coord
{
	m_dX = (double)sX;
	m_dY = (double)sY;
	m_dZ = (double)sZ;

	return 0;
}


////////////////////////////////////////////////////////////////////////////////
// Called by editor to update object
////////////////////////////////////////////////////////////////////////////////
void CFireball::EditUpdate(void)
{
}


////////////////////////////////////////////////////////////////////////////////
// Called by editor to render object
////////////////////////////////////////////////////////////////////////////////
void CFireball::EditRender(void)
{
	// In some cases, object's might need to do a special-case render in edit
	// mode because Startup() isn't called.  In this case it doesn't matter, so
	// we can call the normal Render().
	Render();
}


////////////////////////////////////////////////////////////////////////////////
// Get all required resources
////////////////////////////////////////////////////////////////////////////////
int16_t CFireball::GetResources(void)			// Returns 0 if successfull, non-zero otherwise
{
	int16_t sResult = SUCCESS;

	sResult = rspGetResource(&g_resmgrGame, m_pRealm->Make2dResPath(SMALL_FILE), &m_pAnimChannel, RFile::LittleEndian);

	if (sResult != 0)
		TRACE("CFireball::GetResources - Error getting fire animation resource\n");

	return sResult;
}


////////////////////////////////////////////////////////////////////////////////
// Free all resources
////////////////////////////////////////////////////////////////////////////////
int16_t CFireball::FreeResources(void)						// Returns 0 if successfull, non-zero otherwise
{
	int16_t sResult = 0;

	rspReleaseResource(&g_resmgrGame, &m_pAnimChannel);

	return sResult;
}

////////////////////////////////////////////////////////////////////////////////
// Preload - basically trick the resource manager into caching resources for fire
//				 animations before play begins so that when a fire is set for
//				 the first time, there won't be a delay while it loads.
////////////////////////////////////////////////////////////////////////////////

int16_t CFireball::Preload(
	CRealm* prealm)				// In:  Calling realm.
{
	int16_t sResult;
	ChannelAA* pRes;
	sResult = rspGetResource(&g_resmgrGame, prealm->Make2dResPath(SMALL_FILE), &pRes, RFile::LittleEndian);
	rspReleaseResource(&g_resmgrGame, &pRes);
	return sResult;
}


////////////////////////////////////////////////////////////////////////////////
// ProcessFireballMessages
////////////////////////////////////////////////////////////////////////////////

CFireball::CFireballState CFireball::ProcessFireballMessages(void)
{
	CFireballState eNewState = State_Idle;

	GameMessage msg;

	if (m_MessageQueue.DeQ(&msg) == true)
	{
		switch(msg.msg_Generic.eType)
		{
			case typeObjectDelete:
				m_MessageQueue.Empty();
				return State_Deleted;
				break;
		}
	}
	// Dump the rest of the messages
	m_MessageQueue.Empty();

	return eNewState;
}

////////////////////////////////////////////////////////////////////////////////
// EOF
////////////////////////////////////////////////////////////////////////////////