aboutsummaryrefslogtreecommitdiff
path: root/src/parsec/part_sys.cpp
blob: 6a115f1a1e12791b4c4807ba42bbcf7e52d42ea9 (plain)
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
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
/*
 * PARSEC - System Core
 *
 * $Author: uberlinuxguy $ - $Date: 2004/09/15 12:25:24 $
 *
 * Orginally written by:
 *   Copyright (c) Markus Hadwiger     <msh@parsec.org>   1997-1999
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */ 

// C library
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// compilation flags/debug support
#include "config.h"
#include "debug.h"

// general definitions
#include "general.h"
#include "objstruc.h"

// global externals
#include "globals.h"

// subsystem headers
#include "aud_defs.h"
#include "vid_defs.h"

// drawing subsystem
#include "d_bmap.h"
#include "d_iter.h"
#include "d_misc.h"

// rendering subsystem
#include "r_part.h"

// mathematics header
#include "utl_math.h"

// model header
#include "utl_model.h"

// particle types
#include "parttype.h"

// local module header
#include "part_sys.h"

// proprietary module headers
#include "con_aux.h"
#include "e_color.h"
#include "e_record.h"
#include "h_supp.h"
#include "obj_ctrl.h"
#include "obj_game.h"
#include "part_ani.h"
#include "part_api.h"
#include "g_sfx.h"
#include "g_wfx.h"


// flags
#define USE_Z_BUFFER				// use z buffer to draw particles
//#define STORE_MAX_Z_VALS			// log max/min z values
//#define USER_SPHERE_CREATION		// enable user sphere creation (console)
#define DO_CLUSTER_CULLING			// enable culling of whole clusters
#define USE_NEW_CULLING_CODE		// use new culling function
//#define USE_BOUNDING_BOX_CULLING	// cull bounding box instead of sphere
#define ENABLE_PARTICLE_TRIANGLES	// enable triangle drawing for particles


// bitmap size below which it is replaced by a square of a single color
#define BITMAP_SIZE_BOUNDARY		3

// typical lower boundary of particle bitmap size
int partbitmap_size_bound		= BITMAP_SIZE_BOUNDARY;


// flag if initialization of particle system already done ---------------------
//
static int particle_init_done	= FALSE;


// list of particle clusters --------------------------------------------------
//
pcluster_s *Particles			= NULL;
pcluster_s *CurLinearCluster	= NULL;
pcluster_s *CustomDrawCluster	= NULL;


// init particle system -------------------------------------------------------
//
void InitParticleSystem()
{
	ASSERT( !particle_init_done );

	if ( !particle_init_done ) {

		// create single element for dummy head and tail
		if ( ( Particles = (pcluster_s *) ALLOCMEM( sizeof( pcluster_s ) ) ) == NULL )
			OUTOFMEM( 0 );

		// set pointers thusly
		Particles->next  = (pcluster_s *) ( (char*)Particles + sizeof( pcluster_s* ) );
		Particles->prec  = NULL;
		Particles->rep   = (particle_s *) Particles;

		// initially there are no elements and no allocated storage
		Particles->numel    = 0;
		Particles->maxnumel = 0;

		// init sizes of particles
		InitParticleSizes();

		particle_init_done = TRUE;
	}
}


// make resoscale available for hacking ---------------------------------------
//
float cur_particle_resoscale = 1.0f;


// init reference z values for particles according to resolution --------------
//
void InitParticleSizes()
{
	float resoscale = 1.8f * D_Value / 1024.0f; // FIXME: not accurate with different FOVs

	cur_particle_resoscale = resoscale;

	PRT_InitParticleSizes( resoscale );
	PAN_InitParticleSizes( resoscale );
	WFX_InitParticleSizes( resoscale );
	SFX_InitParticleSizes( resoscale );
}


// kill particle system -------------------------------------------------------
//
void KillParticleSystem()
{
	if ( particle_init_done ) {
		particle_init_done = FALSE;

		FreeParticles();

		FREEMEM( Particles );
		Particles = NULL;
	}
}


// remove all particles and free storage --------------------------------------
//
void FreeParticles()
{
	// free clusters attached to local ship
	PRT_FreeAttachedClusterList( MyShip );

	//NOTE:
	// all other attached clusters have already been
	// freed, since FreeParticles() is always invoked
	// after FreeObjects() which in turn invokes
	// PRT_FreeAttachedClusterList() for each object
	// it frees. apart from MyShip, that is.

	ASSERT( Particles->next != NULL );

	// free all clusters contained in list (except head/tail)
	pcluster_s *scan = NULL;
	for ( scan = Particles->next; scan->next; ) {

		pcluster_s *temp = scan->next;
		FREEMEM( scan->rep );
		FREEMEM( scan );
		scan = temp;
	}

	// reinit next field of head and prec field of tail
	Particles->next = scan;
	scan->prec		= Particles;

	// no particles contained in list anymore
	Particles->numel    = 0;
	Particles->maxnumel = 0;

	// invalidate global cluster pointers
	CurLinearCluster  = NULL;
	CustomDrawCluster = NULL;

	// reset number of particle extras
	CurrentNumPrtExtras = 0;
}


// let user create spheres via console aux commands ---------------------------
//
PRIVATE
void DoUserControlledSphereCreation()
{

#ifdef USER_SPHERE_CREATION

	ShipObject *ship1 = (ShipObject *) FetchFirstShip();

	if ( AUXDATA_PSPHERE_CREATION_CODE == 7 ) {

		if ( PRT_ObjectHasAttachedClusters( MyShip ) ) {
			PRT_FreeAttachedClusterList( MyShip );
		} else {
			objectbase_pcluster_s *cluster = PRT_CreateObjectCenteredSphere( MyShip, MyShip->BoundingSphere, SAT_STOCHASTIC_MOTION, SPHERE_PARTICLES, INFINITE_LIFETIME, 0, PlayerId );
		}

	} else if ( AUXDATA_PSPHERE_CREATION_CODE == 1 ) {

		if ( SFX_EnableInvulnerabilityShield( MyShip ) )
			MyShip->MegaShieldAbsorption = MegaShieldStrength;

	} else if ( AUXDATA_PSPHERE_CREATION_CODE == 2 ) {

		if ( ship1 ) {
			ShipObject *shippo = (ShipObject*) ship1;

			if ( PRT_ObjectHasAttachedClusters( shippo ) ) {
				PRT_FreeAttachedClusterList( shippo );
			} else {
				objectbase_pcluster_s *cluster = PRT_CreateObjectCenteredSphere( shippo, shippo->BoundingSphere, SAT_ROTATING, SPHERE_PARTICLES, INFINITE_LIFETIME, 0, PlayerId );
			}
		}

	} else if ( AUXDATA_PSPHERE_CREATION_CODE == 3 ) {

		if ( ship1 && ship1->NextObj ) {
			ShipObject *shippo = (ShipObject*) ship1->NextObj;

			if ( PRT_ObjectHasAttachedClusters( shippo ) ) {
				PRT_FreeAttachedClusterList( shippo );
			} else {
				objectbase_pcluster_s *cluster = PRT_CreateObjectCenteredSphere( shippo, shippo->BoundingSphere, SAT_ROTATING, SPHERE_PARTICLES, INFINITE_LIFETIME, 0, PlayerId );
			}
		}

	} else if ( AUXDATA_PSPHERE_CREATION_CODE == 4 ) {

		if ( ship1 && ship1->NextObj && ship1->NextObj->NextObj ) {
			ShipObject *shippo = (ShipObject*) ship1->NextObj->NextObj;

			if ( PRT_ObjectHasAttachedClusters( shippo ) ) {
				PRT_FreeAttachedClusterList( shippo );
			} else {
				objectbase_pcluster_s *cluster = PRT_CreateObjectCenteredSphere( shippo, shippo->BoundingSphere, SAT_ROTATING, SPHERE_PARTICLES, INFINITE_LIFETIME, 0, PlayerId );
			}
		}

	} else if ( AUXDATA_PSPHERE_CREATION_CODE == 5 ) {

		if ( ship1 && ship1->NextObj && ship1->NextObj->NextObj && ship1->NextObj->NextObj->NextObj ) {
			ShipObject *shippo = (ShipObject*) ship1->NextObj->NextObj->NextObj;

			if ( PRT_ObjectHasAttachedClusters( shippo ) ) {
				PRT_FreeAttachedClusterList( shippo );
			} else {
				objectbase_pcluster_s *cluster = PRT_CreateObjectCenteredSphere( shippo, shippo->BoundingSphere, SAT_ROTATING, SPHERE_PARTICLES, INFINITE_LIFETIME, 0, PlayerId );
			}
		}

	} else if ( AUXDATA_PSPHERE_CREATION_CODE == 6 ) {

		Vertex3 position;

//		FetchTVector( MyShip->ObjPosition, &position );

		position.X = position.Y = position.Z = 0;

		int type;
		switch ( AUXDATA_PSPHERE_TYPE ) {
			case 1:
				type = SAT_ROTATING;
				break;

			case 2:
				type = SAT_PULSATING;
				break;

			case 3:
				type = SAT_CONTRACTING;
				break;

			case 4:
				type = SAT_EXPLODING;
				break;

			default:
				type = SAT_NO_ANIMATION;
				break;
		}

		pcluster_s *cluster;
		if ( AUXDATA_PSPHERE_RADIUS > 0 ) {
			if ( type == SAT_EXPLODING ) {
				cluster = PRT_CreateParticleSphereObject(
					position, FIXED_TO_GEOMV( AUXDATA_PSPHERE_RADIUS ), SAT_EXPLODING,
					SPHERE_EXPL_PARTICLES, SPHERE_EXPLOSION_DURATION, NULL, PlayerId );
			} else if ( type == SAT_PULSATING ) {
				cluster = PRT_CreateParticleSphereObject(
					position, FIXED_TO_GEOMV( AUXDATA_PSPHERE_RADIUS ), SAT_PULSATING,
					SPHERE_PARTICLES, INFINITE_LIFETIME, NULL, PlayerId );
			} else if ( type == SAT_CONTRACTING ) {
				cluster = PRT_CreateParticleSphereObject(
					position, FIXED_TO_GEOMV( AUXDATA_PSPHERE_RADIUS ), SAT_CONTRACTING,
					SPHERE_PARTICLES, CONTRACTING_SPHERE_LIFETIME, NULL, PlayerId );
			} else if ( type == SAT_NO_ANIMATION ) {
				cluster = PRT_CreateParticleSphereObject(
					position, FIXED_TO_GEOMV( AUXDATA_PSPHERE_RADIUS ), SAT_NO_ANIMATION,
					SPHERE_PARTICLES, INFINITE_LIFETIME, NULL, PlayerId );
			} else {
				cluster = PRT_CreateParticleSphereObject(
					position, FIXED_TO_GEOMV( AUXDATA_PSPHERE_RADIUS ), SAT_ROTATING,
					SPHERE_PARTICLES, INFINITE_LIFETIME, NULL, PlayerId );
			}
		} else {
			if ( type == SAT_EXPLODING ) {
				cluster = PRT_CreateParticleSphereObject(
					position, FIXED_TO_GEOMV( 0x500 ), SAT_EXPLODING,
					SPHERE_EXPL_PARTICLES, SPHERE_EXPLOSION_DURATION, NULL, PlayerId );
			} else if ( type == SAT_PULSATING ) {
				cluster = PRT_CreateParticleSphereObject(
					position, FLOAT_TO_GEOMV( 16.0 ), SAT_PULSATING,
					SPHERE_PARTICLES, INFINITE_LIFETIME, NULL, PlayerId );
			} else if ( type == SAT_CONTRACTING ) {
				cluster = PRT_CreateParticleSphereObject(
					position, FIXED_TO_GEOMV( 0x1000 ), SAT_CONTRACTING,
					SPHERE_PARTICLES, CONTRACTING_SPHERE_LIFETIME, NULL, PlayerId );
			} else if ( type == SAT_NO_ANIMATION ) {
				cluster = PRT_CreateParticleSphereObject(
					position, FLOAT_TO_GEOMV( 28.0 ), SAT_NO_ANIMATION,
					SPHERE_PARTICLES, INFINITE_LIFETIME, NULL, PlayerId );
			} else {
				cluster = PRT_CreateParticleSphereObject(
					position, FLOAT_TO_GEOMV( 28.0 ), SAT_ROTATING,
					SPHERE_PARTICLES, INFINITE_LIFETIME, NULL, PlayerId );
			}
		}


	}

	// reset creation code
	AUXDATA_PSPHERE_CREATION_CODE = 0;

#endif

}


// log maximum and minimum z values encountered -------------------------------
//
INLINE
void LogZValues( int intdepth )
{

#ifdef STORE_MAX_Z_VALS

	if ( AUXDATA_LEAST_VERTEX_1_OVER_Z == 0 )
		AUXDATA_LEAST_VERTEX_1_OVER_Z = intdepth;
	else if ( intdepth < AUXDATA_LEAST_VERTEX_1_OVER_Z ) {
		AUXDATA_LEAST_VERTEX_1_OVER_Z = intdepth;
	}

	if ( AUXDATA_GREATEST_VERTEX_1_OVER_Z == 0 )
		AUXDATA_GREATEST_VERTEX_1_OVER_Z = intdepth;
	else if ( intdepth > AUXDATA_GREATEST_VERTEX_1_OVER_Z ) {
		AUXDATA_GREATEST_VERTEX_1_OVER_Z = intdepth;
	}

#endif

}


// particle drawing info ------------------------------------------------------
//
struct pdrawinf_s {

	int			width;
	int			height;
	int			scalewidth;
	int			scaleheight;
	depth_t		depthvalue;

	TextureMap*	texmap;
	char*		bitmap;
	imgtrafo_s*	imgtrafo;
};


// draw particle as square of single color ------------------------------------
//
INLINE
void DrawParticleSquare( SPoint *spt, pdrawinf_s *pdi, visual_t drawcol )
{
	ASSERT( spt != NULL );
	ASSERT( pdi != NULL );

	int drawsiz = ( pdi->scaleheight > 0 ) ? pdi->scaleheight : 1;

#ifdef USE_Z_BUFFER

	if ( ZBufferEnabled ) {
		int intdepth = DEPTH_TO_INTEGER( pdi->depthvalue );
		D_DrawSquareZ( spt->X - drawsiz/2, spt->Y - drawsiz/2, drawcol, drawsiz, intdepth );
		LogZValues( intdepth );
	} else {
		D_DrawSquare( spt->X - drawsiz/2, spt->Y - drawsiz/2, drawcol, drawsiz );
	}

#else

	D_DrawSquare( spt->X - drawsiz/2, spt->Y - drawsiz/2, drawcol, drawsiz );

#endif

}


// draw particle as bitmap ----------------------------------------------------
//
INLINE
void DrawParticleBitmap( SPoint *spt, pdrawinf_s *pdi )
{
	ASSERT( spt != NULL );
	ASSERT( pdi != NULL );
	ASSERT( pdi->scalewidth > 0 );
	ASSERT( pdi->scaleheight > 0 );

#ifdef USE_Z_BUFFER

	if ( ZBufferEnabled ) {
		int intdepth = DEPTH_TO_INTEGER( pdi->depthvalue );
		D_PutSTCBitmapZ( pdi->bitmap, pdi->width, pdi->height,
						 pdi->scalewidth, pdi->scaleheight,
					     spt->X - pdi->scalewidth/2, spt->Y - pdi->scaleheight/2,
						 intdepth );
		LogZValues( intdepth );
	} else {
		D_PutSTCBitmap( pdi->bitmap, pdi->width, pdi->height,
						pdi->scalewidth, pdi->scaleheight,
						spt->X - pdi->scalewidth/2, spt->Y - pdi->scaleheight/2 );
	}

#else

	D_PutSTCBitmap( pdi->bitmap, pdi->width, pdi->height,
					pdi->scalewidth, pdi->scaleheight,
					spt->X - pdi->scalewidth/2, spt->Y - pdi->scaleheight/2 );
#endif

}


// rasterizer configuration for particle drawing ------------------------------
//
static word particle_raststate = rast_nozwrite | rast_texclamp | rast_chromakeyoff;
static word particle_rastmask  = rast_mask_zcompare;


// draw particle as texture ---------------------------------------------------
//
INLINE
void DrawParticleTexture( SPoint *spt, pdrawinf_s *pdi, dword itertype )
{
	ASSERT( spt != NULL );
	ASSERT( pdi != NULL );
	ASSERT( pdi->scalewidth > 0 );
	ASSERT( pdi->scaleheight > 0 );

//TODO:
//	pdi->imgtrafo

#ifdef USE_Z_BUFFER
	depth_t depthvalue = pdi->depthvalue;
	LogZValues( DEPTH_TO_INTEGER( depthvalue ) );
#else
	depth_t depthvalue = 0;
#endif

	// unclipped coordinates
	int x1 = spt->X - pdi->scalewidth/2;
	int y1 = spt->Y - pdi->scaleheight/2;
	int u1 = 0;
	int v1 = 0;

	int x2 = spt->X + pdi->scalewidth/2 - 1;
	int y2 = spt->Y - pdi->scaleheight/2;
	int u2 = pdi->width;
	int v2 = 0;

	int x3 = spt->X + pdi->scalewidth/2 - 1;
	int y3 = spt->Y + pdi->scaleheight/2 - 1;
	int u3 = pdi->width;
	int v3 = pdi->height;

	int x4 = spt->X - pdi->scalewidth/2;
	int y4 = spt->Y + pdi->scaleheight/2 - 1;
	int u4 = 0;
	int v4 = pdi->height;

	// calc inverse scale factors
	float sfacx = (float) pdi->width / pdi->scalewidth;
	float sfacy = (float) pdi->height / pdi->scaleheight;

#ifdef ENABLE_PARTICLE_TRIANGLES
	int drawtriangle = AUX_SMALL_PARTICLE_TRIANGLES;
	#define PARTICLE_CLIPPED() drawtriangle = 0
#else
	#define PARTICLE_CLIPPED()
#endif

	// clip coordinates
	if ( x1 < 0 ) {
		PARTICLE_CLIPPED();
		u1 -= (int)( x1 * sfacx );
		x1  = 0;
	}
	if ( y1 < 0 ) {
		PARTICLE_CLIPPED();
		v1 -= (int)( y1 * sfacy );
		y1  = 0;
	}

	if ( x2 >= Screen_Width ) {
		PARTICLE_CLIPPED();
		u2 -= (int)( ( x2 - Screen_Width ) * sfacx );
		x2  = Screen_Width - 1;
	}
	if ( y2 < 0 ) {
		PARTICLE_CLIPPED();
		v2 = (int)( -y2 * sfacy );
		y2 = 0;
	}

	if ( x3 >= Screen_Width ) {
		PARTICLE_CLIPPED();
		u3 -= (int)( ( x3 - Screen_Width ) * sfacx );
		x3  = Screen_Width - 1;
	}
	if ( y3 >= Screen_Height ) {
		PARTICLE_CLIPPED();
		v3 -= (int)( ( y3 - Screen_Height ) * sfacy );
		y3  = Screen_Height - 1;
	}

	if ( x4 < 0 ) {
		PARTICLE_CLIPPED();
		u4 -= (int)( x4 * sfacx );
		x4  = 0;
	}
	if ( y4 >= Screen_Height ) {
		PARTICLE_CLIPPED();
		v4 -= (int)( ( y4 - Screen_Height ) * sfacy );
		y4  = Screen_Height - 1;
	}

#ifdef ENABLE_PARTICLE_TRIANGLES

	if ( drawtriangle ) {

		// determine sizebound
		int sizebound = ( Screen_Width * drawtriangle ) / 256;

		// only if small enough (fill-rate!)
		if ( pdi->scalewidth < sizebound ) {

			y1 -= pdi->scaleheight;
			v1 -= pdi->height;
			x2 += pdi->scalewidth;
			y2 += pdi->scaleheight;
			u2 += pdi->width;
			v2 += pdi->height;
			x3  = x4;
			y3  = y4;
			u3  = u4;
			v3  = v4;

		} else {
			drawtriangle = 0;
		}
	}

#endif

	// fill iter header
	IterRectangle2 itrect;
	itrect.flags	 = ITERFLAG_NONE;
	itrect.itertype	 = itertype;
	itrect.raststate = particle_raststate;
	itrect.rastmask  = particle_rastmask;
	itrect.texmap	 = pdi->texmap;

	// fill vertex structs
	itrect.Vtxs[ 0 ].X = INT_TO_RASTV( x1 );
	itrect.Vtxs[ 0 ].Y = INT_TO_RASTV( y1 );
	itrect.Vtxs[ 0 ].Z = DEPTH_TO_RASTV( depthvalue );
	itrect.Vtxs[ 0 ].W = GEOMV_1;
	itrect.Vtxs[ 0 ].U = INT_TO_GEOMV( u1 );
	itrect.Vtxs[ 0 ].V = INT_TO_GEOMV( v1 );
	itrect.Vtxs[ 0 ].R = 220;
	itrect.Vtxs[ 0 ].G = 220;
	itrect.Vtxs[ 0 ].B = 220;
	itrect.Vtxs[ 0 ].A = 180;

	itrect.Vtxs[ 1 ].X = INT_TO_RASTV( x2 );
	itrect.Vtxs[ 1 ].Y = INT_TO_RASTV( y2 );
	itrect.Vtxs[ 1 ].Z = itrect.Vtxs[ 0 ].Z;
	itrect.Vtxs[ 1 ].W = GEOMV_1;
	itrect.Vtxs[ 1 ].U = INT_TO_GEOMV( u2 );
	itrect.Vtxs[ 1 ].V = INT_TO_GEOMV( v2 );
	itrect.Vtxs[ 1 ].R = itrect.Vtxs[ 0 ].R;
	itrect.Vtxs[ 1 ].G = itrect.Vtxs[ 0 ].G;
	itrect.Vtxs[ 1 ].B = itrect.Vtxs[ 0 ].B;
	itrect.Vtxs[ 1 ].A = itrect.Vtxs[ 0 ].A;

	itrect.Vtxs[ 2 ].X = INT_TO_RASTV( x3 );
	itrect.Vtxs[ 2 ].Y = INT_TO_RASTV( y3 );
	itrect.Vtxs[ 2 ].Z = itrect.Vtxs[ 0 ].Z;
	itrect.Vtxs[ 2 ].W = GEOMV_1;
	itrect.Vtxs[ 2 ].U = INT_TO_GEOMV( u3 );
	itrect.Vtxs[ 2 ].V = INT_TO_GEOMV( v3 );
	itrect.Vtxs[ 2 ].R = itrect.Vtxs[ 0 ].R;
	itrect.Vtxs[ 2 ].G = itrect.Vtxs[ 0 ].G;
	itrect.Vtxs[ 2 ].B = itrect.Vtxs[ 0 ].B;
	itrect.Vtxs[ 2 ].A = itrect.Vtxs[ 0 ].A;

#ifdef ENABLE_PARTICLE_TRIANGLES

	if ( drawtriangle ) {

		D_DrawIterTriangle2( (IterTriangle2 *) &itrect );

	} else

#endif

	{
		itrect.Vtxs[ 3 ].X = INT_TO_RASTV( x4 );
		itrect.Vtxs[ 3 ].Y = INT_TO_RASTV( y4 );
		itrect.Vtxs[ 3 ].Z = itrect.Vtxs[ 0 ].Z;
		itrect.Vtxs[ 3 ].W = GEOMV_1;
		itrect.Vtxs[ 3 ].U = INT_TO_GEOMV( u4 );
		itrect.Vtxs[ 3 ].V = INT_TO_GEOMV( v4 );
		itrect.Vtxs[ 3 ].R = itrect.Vtxs[ 0 ].R;
		itrect.Vtxs[ 3 ].G = itrect.Vtxs[ 0 ].G;
		itrect.Vtxs[ 3 ].B = itrect.Vtxs[ 0 ].B;
		itrect.Vtxs[ 3 ].A = itrect.Vtxs[ 0 ].A;

		D_DrawIterRectangle2( &itrect );
	}
}


// stores pointer to extinfo that should be used for entire cluster -----------
//
static pextinfo_s *cluster_global_extinfo = NULL;


// draw new standard particle (with extinfo) ----------------------------------
//
INLINE
void DrawParticleExtInfo( particle_s *particle, SPoint *spt, geomv_t zdist )
{
	ASSERT( particle != NULL );
	ASSERT( spt != NULL );

	pdrawinf_s pdi;

	// field bitmap is actually rendering flags
	dword rendflags = particle->bitmap;

	if ( rendflags & PART_REND_POINTVIS ) {

		// midpoint not on screen means particle invisible
		if ( ( spt->X < 0 ) || ( spt->X >= Screen_Width  ) ||
			 ( spt->Y < 0 ) || ( spt->Y >= Screen_Height ) ) {
			return;
		}

		pdi.depthvalue = DEPTHBUFF_OOZ( zdist );
	}

	// fetch extinfo (current texture and trafo frame)
	pextinfo_s *extinfo  = cluster_global_extinfo ?
						   cluster_global_extinfo : particle->extinfo;
	ASSERT( extinfo != NULL );

	pdef_s *pdef = extinfo->partdef; //TODO: partdef_dest
	ASSERT( pdef != NULL );

	ASSERT( pdef->tex_table != NULL );
	texfrm_s *curtexframe = &pdef->tex_table[ extinfo->tex_pos ];
	xfofrm_s *curxfoframe = pdef->xfo_table ?
		&pdef->xfo_table[ extinfo->xfo_pos ] : NULL;

	TextureMap *curtex = curtexframe->texmap;
	ASSERT( curtex != NULL );

	// fill drawing info
	pdi.width    = 1 << curtex->Width;
	pdi.height   = 1 << curtex->Height;
	pdi.texmap   = curtex;
	pdi.bitmap   = NULL;
	pdi.imgtrafo = curxfoframe ? curxfoframe->imgtrafo : NULL;

	// calc texture size
	if ( rendflags & PART_REND_NODEPTHSCALE ) {

		// skip particle entirely if too far away
		if ( zdist > Far_View_Plane/2 ) {
			return;
		}

		//NOTE:
		// for no-depthscale particles the ref_z directly
		// determines the size of the particle with respect
		// to its texture width and height.

		// fixed texture size
		pdi.scalewidth	= ( (int) ( pdi.width  * particle->ref_z ) ) & ~1; //TODO: cache?
		pdi.scaleheight	= ( (int) ( pdi.height * particle->ref_z ) ) & ~1; //TODO: remove even?

	} else {

		// calc scale factor according to reference z
		float scalefac = particle->ref_z / GEOMV_TO_FLOAT( zdist );

		// scale texture size
		pdi.scalewidth	= ( (int) ( pdi.width  * scalefac ) ) & ~1; //TODO: remove even?
		pdi.scaleheight	= ( (int) ( pdi.height * scalefac ) ) & ~1;
	}

	// check lower size boundary and draw either as texture or square
	if ( ( pdi.scalewidth  <= particle->sizebound ) ||
		 ( pdi.scaleheight <= particle->sizebound ) ) {

		visual_t drawcol = COLINDX_TO_VISUAL( particle->color );
		if ( ( rendflags & PART_REND_POINTVIS ) == 0 )
			pdi.depthvalue = DEPTHBUFF_OOZ( zdist );
		DrawParticleSquare( spt, &pdi, drawcol );

	} else if ( ( pdi.scalewidth > 0 ) && ( pdi.scaleheight > 0 ) ) {

		int swidth2  = pdi.scalewidth  / 2;
		int sheight2 = pdi.scaleheight / 2;

		if ( ( spt->X > -swidth2  ) && ( spt->X < Screen_Width  + swidth2  ) &&
			 ( spt->Y > -sheight2 ) && ( spt->Y < Screen_Height + sheight2 ) ) {

			// save rast
			word oldraststate = particle_raststate;
			word oldrastmask  = particle_rastmask;
			
			// use z-compare instead of slow depth buffer reads
			particle_raststate |= rast_zcompare;
			particle_rastmask = rast_nomask;

			// disable z-cmp if specified
			// disabled for now: using z compare instead of depth buffer reads
			/*
			if ( rendflags & PART_REND_NODEPTHCMP ) {
				particle_raststate &= ~rast_mask_zcompare;
				particle_rastmask  &= ~rast_mask_zcompare;
			}
			 */

			if ( ( rendflags & PART_REND_POINTVIS ) == 0 )
				pdi.depthvalue = DEPTHBUFF_OOZ( zdist );
			DrawParticleTexture( spt, &pdi, rendflags & PART_REND_MASK_ITER );

			// enable z-cmp
			particle_raststate = oldraststate;
			particle_rastmask  = oldrastmask;
		}
	}
}


// draw legacy particle (just bitmap, no extinfo) -----------------------------
//
INLINE
void DrawParticleLegacy( particle_s *particle, SPoint *spt, geomv_t zdist )
{
	ASSERT( particle != NULL );
	ASSERT( spt != NULL );

	// use simple bitmap to draw particle
	int bindx	 = particle->bitmap;

	pdrawinf_s pdi;
	pdi.width    = BitmapInfo[ bindx ].width;
	pdi.height   = BitmapInfo[ bindx ].height;
	pdi.texmap   = NULL;
	pdi.bitmap   = BitmapInfo[ bindx ].bitmappointer;
	pdi.imgtrafo = NULL;

	// calc scale factor according to reference z
	float scalefac = particle->ref_z / GEOMV_TO_FLOAT( zdist );

	// scale texture size
	pdi.scalewidth	= ( (int) ( pdi.width  * scalefac ) ) & ~1; //TODO: remove even?
	pdi.scaleheight	= ( (int) ( pdi.height * scalefac ) ) & ~1;

	// check lower size boundary and draw either as bitmap or square
	if ( ( pdi.scalewidth  <= particle->sizebound ) ||
		 ( pdi.scaleheight <= particle->sizebound ) ) {

		visual_t drawcol = COLINDX_TO_VISUAL( particle->color );
		pdi.depthvalue   = DEPTHBUFF_OOZ( zdist );
		DrawParticleSquare( spt, &pdi, drawcol );

	} else if ( ( pdi.scalewidth > 0 ) && ( pdi.scaleheight > 0 ) ) {

		int swidth2  = pdi.scalewidth  / 2;
		int sheight2 = pdi.scaleheight / 2;

		if ( ( spt->X > -swidth2  ) && ( spt->X < Screen_Width  + swidth2 ) &&
			 ( spt->Y > -sheight2 ) && ( spt->Y < Screen_Height + sheight2 ) ) {

			pdi.depthvalue = DEPTHBUFF_OOZ( zdist );
			DrawParticleBitmap( spt, &pdi );
		}
	}
}


// draw single particle at specified view-space coordinates -------------------
//
INLINE
void DrawParticle( particle_s *particle, Vertex3 *position )
{
	ASSERT( particle != NULL );
	ASSERT( position != NULL );

	// project particle to screen
	SPoint screenpos;
	PROJECT_TO_SCREEN( *position, screenpos );

	// draw particle depending on drawing type
	if ( particle->extinfo == NULL ) {

		// legacy particle without extinfo
		DrawParticleLegacy( particle, &screenpos, position->Z );

	} else {

		// standard particle with extinfo
		DrawParticleExtInfo( particle, &screenpos, position->Z );
	}
}


// check if ship entered energy field -----------------------------------------
//
INLINE
void CheckEnergyField( pcluster_s *cluster )
{
	if ( ( cluster->type & CT_TYPEMASK ) == CT_PARTICLE_SPHERE ) {
 		sphereobj_pcluster_s *objcluster = (sphereobj_pcluster_s *) cluster;
		if ( objcluster->animtype == SAT_ENERGYFIELD_SPHERE ) {
			if ( PRT_ParticleInBoundingSphere( MyShip, objcluster->origin ) ) {
				OBJ_BoostEnergy( MyShip, CurScreenRefFrames * 2 );
			}
		}
	}
}


// determine if sphere is entirely outside the viewing frustum ----------------
//
INLINE
int CullSphereAgainstFrustum( sphereobj_pcluster_s *cluster )
{
	geomv_t radius = cluster->bdsphere;

#ifdef USE_NEW_CULLING_CODE

	Vertex3 origin = cluster->origin;

	#ifdef USE_BOUNDING_BOX_CULLING

		CullBox3 cullbox;

		cullbox.minmax[ 0 ] = origin.X - radius;
		cullbox.minmax[ 1 ] = origin.Y - radius;
		cullbox.minmax[ 2 ] = origin.Z - radius;

		cullbox.minmax[ 3 ] = origin.X + radius;
		cullbox.minmax[ 4 ] = origin.Y + radius;
		cullbox.minmax[ 5 ] = origin.Z + radius;

		dword cullmask = 0x3f;
		return CULL_BoxAgainstCullVolume( &cullbox, World_CullVolume, &cullmask );

	#else

		Sphere3 sphere;
		sphere.X = origin.X;
		sphere.Y = origin.Y;
		sphere.Z = origin.Z;
		sphere.R = radius;

		dword cullmask = 0x3f;
		return CULL_SphereAgainstVolume( &sphere, World_ViewVolume, &cullmask );

	#endif

#else

	// transform origin into view-space
	Vertex3 origin;
	MtxVctMUL( ViewCamera, &cluster->origin, &origin );

	// calc potentially nearest and farthest z coordinates
	geomv_t nearestZ  = origin.Z - radius;
	geomv_t farthestZ = origin.Z + radius;

	// too near?
	if ( farthestZ < Near_View_Plane )
		return TRUE;
	// too far away?
	if ( nearestZ > Far_View_Plane )
		return TRUE;

	geomv_t criterionX = GEOMV_MUL( farthestZ, Criterion_X );
	// too far to the left or right?
	if ( ( origin.X - radius ) > criterionX )
		return TRUE;
	if ( ( origin.X + radius ) < -criterionX )
		return TRUE;

	geomv_t criterionY = GEOMV_MUL( farthestZ, Criterion_Y );
	// too far up or down?
	if ( ( origin.Y - radius ) > criterionY )
		return TRUE;
	if ( ( origin.Y + radius ) < -criterionY )
		return TRUE;

	return FALSE;

#endif

}


// cull cluster as a whole against viewing frustum ----------------------------
//
INLINE
int CullWholeCluster( pcluster_s *cluster )
{
	if ( AUX_DONT_CULL_PARTICLE_OBJECTS )
		return FALSE;

#ifdef DO_CLUSTER_CULLING

	//TODO:
	// cull all clusters where bdsphere is valid.

	if ( ( cluster->type & CT_TYPEMASK ) == CT_PARTICLE_SPHERE ) {

 		sphereobj_pcluster_s *objcluster = (sphereobj_pcluster_s *) cluster;
		if ( objcluster->animtype == SAT_ENERGYFIELD_SPHERE ) {

			// return culling status
			return CullSphereAgainstFrustum( objcluster );
		}
	}

#endif

	return FALSE;
}


// check if point is contained in ship's bounding sphere ----------------------
//
int PRT_ParticleInBoundingSphere( ShipObject *shippo, Vertex3& point )
{
	ASSERT( shippo != NULL );

	geomv_t objX = shippo->ObjPosition[ 0 ][ 3 ];
	geomv_t objY = shippo->ObjPosition[ 1 ][ 3 ];
	geomv_t objZ = shippo->ObjPosition[ 2 ][ 3 ];

	geomv_t bdsphere  = shippo->BoundingSphere;
	geomv_t bdsphere2 = shippo->BoundingSphere2;

	if ( point.X < ( objX - bdsphere ) ) return FALSE;
	if ( point.X > ( objX + bdsphere ) ) return FALSE;
	if ( point.Y < ( objY - bdsphere ) ) return FALSE;
	if ( point.Y > ( objY + bdsphere ) ) return FALSE;
	if ( point.Z < ( objZ - bdsphere ) ) return FALSE;
	if ( point.Z > ( objZ + bdsphere ) ) return FALSE;

	Vector3 dvec;
	dvec.X = point.X - objX;
	dvec.Y = point.Y - objY;
	dvec.Z = point.Z - objZ;

	return ( DOT_PRODUCT( &dvec, &dvec ) < bdsphere2 );
}


// draw customdraw particles belonging to specified object --------------------
//
int DrawCustomParticles( const GenObject *baseobject )
{
	int cfound = FALSE;

	// save rast
	word oldraststate = particle_raststate;
	word oldrastmask  = particle_rastmask;

	// disable z-cmp
	particle_raststate &= ~rast_mask_zcompare;
	particle_rastmask  &= ~rast_mask_zcompare;

	//TODO:
	// use clusters attached to baseobject to
	// avoid scanning all clusters.

	// walk list of clusters
	pcluster_s *scan = Particles->next;
	for ( ; scan->next; scan = scan->next ) {
		if ( ( scan->type & CT_TYPEMASK ) == CT_CUSTOMDRAW ) {

			customdraw_pcluster_s *cluster = (customdraw_pcluster_s *) scan;

			if ( cluster->baseobject != baseobject )
				continue;

			// to return if at least one found
			cfound = TRUE;

			// draw particles in cluster
			for ( int curp = 0; curp < cluster->numel; curp++ ) {

				particle_s *particle = &cluster->rep[ curp ];

				// skip inactive particles
				if ( ( particle->flags & PARTICLE_ACTIVE ) == 0 )
					continue;

				// transform position into view-space
				Vertex3 tempvert;
				MtxVctMUL( ViewCamera, &particle->position, &tempvert );

				// draw view-space particle
				if ( ( tempvert.Z > Near_View_Plane ) && ( tempvert.Z < Far_View_Plane ) ) {
					DrawParticle( particle, &tempvert );
				}
			}
		}
	}

	// enable z-cmp
	particle_raststate = oldraststate;
	particle_rastmask  = oldrastmask;

	return cfound;
}


// draw all particles in a cluster --------------------------------------------
//
INLINE
int DrawClusterParticles( pcluster_s *cluster )
{
	ASSERT( cluster != NULL );

	// early-out
	if ( cluster->numel == 0 )
		return 0;

	// count particles found active
	int numactive = 0;

	// try to use faster rendering function if enabled
	if ( AUX_DIRECT_CLUSTER_RENDERING ) {

		// the function determines whether it can render this cluster
		if ( R_DrawParticleCluster( cluster, &numactive ) ) {
			return numactive;
		}
	}

	// use only one extinfo if specified
	cluster_global_extinfo = ( cluster->type & CT_CLUSTER_GLOBAL_EXTINFO ) ?
		cluster->rep[ 0 ].extinfo : NULL;

	// render each particle separately
	for ( int curp = 0; curp < cluster->numel; curp++ ) {

		particle_s *particle = &cluster->rep[ curp ];

		// skip inactive particles
		if ( ( particle->flags & PARTICLE_ACTIVE ) == 0 ) {
			continue;
		} else {
			numactive++;
		}

		Vertex3 tempvert;
		Vertex3 posvec = particle->position;

		// if position is in (particle)object-space transform into world-space
		if ( cluster->type & CT_PARTICLE_OBJ_MASK ) {

			Vertex3& origin = ((particleobj_pcluster_s*)cluster)->origin;

			posvec.X += origin.X;
			posvec.Y += origin.Y;
			posvec.Z += origin.Z;

		// if position is in object-space transform into world-space
		} else if ( cluster->type & CT_GENOBJECTRELATIVE_OBJ_MASK ) {

			GenObject *baseobject = ((objectbase_pcluster_s*)cluster)->baseobject;
			ASSERT( baseobject != NULL );
			MtxVctMUL( baseobject->ObjPosition, &posvec, &tempvert );
			posvec = tempvert;
		}

		// transform position into view-space
		MtxVctMUL( ViewCamera, &posvec, &tempvert );

		// draw view-space particle
		if ( ( tempvert.Z > Near_View_Plane ) && ( tempvert.Z < Far_View_Plane ) ) {
			DrawParticle( particle, &tempvert );
		}
	}

	// avoid dangling extinfo
	cluster_global_extinfo = NULL;

	return numactive;
}


// draw all clusters in global list of particle clusters ----------------------
//
PRIVATE
void DrawClusterList()
{
	// count number of clusters in list and actually visible clusters
	int walked_clusters  = 0;
	int visible_clusters = 0;

	// walk list of clusters
	pcluster_s *cluster = Particles->next;
	for ( ; cluster->next; ++walked_clusters ) {

		// skip cluster if it should not be drawn here
		if ( cluster->type & CT_DONT_DRAW_AUTOMATICALLY ) {
			cluster = cluster->next;
			continue;
		}

		// don't draw particles if cockpit view active?
		if ( cluster->type & CT_DONT_DRAW_IN_COCKPIT_MASK ) {
			ASSERT( cluster->type & CT_GENOBJECTRELATIVE_OBJ_MASK );
			objectbase_pcluster_s *bcluster = (objectbase_pcluster_s*) cluster;
			if ( ( bcluster->baseobject == MyShip ) && !ObjCameraActive ) {
				cluster = cluster->next;
				continue;
			}
		}

		// don't draw genobject clusters if baseobject not visible
		if ( ( cluster->type & CT_TYPEMASK ) == CT_GENOBJECT_PARTICLES ) {

			if ( AUX_DISABLE_GENOBJECT_PARTICLES ) {
				cluster = cluster->next;
				continue;
			}

			genobject_pcluster_s *gencluster =  (genobject_pcluster_s *) cluster;

			GenObject *baseobject = gencluster->baseobject;
			ASSERT( baseobject != NULL );

			if ( ( cluster->type & CT_DONT_CULL_WITH_GENOBJECT ) == 0 ) {
				// cull cluster if object is invisible
				if ( baseobject->VisibleFrame != CurVisibleFrame ) {
					cluster = cluster->next;
					continue;
				}
			}
		}

		// don't draw cluster if base ship invisible during explosion
		if ( cluster->type & CT_DONT_DRAW_IF_BASE_VISNEVER ) {
			objectbase_pcluster_s *bcluster = (objectbase_pcluster_s*) cluster;
			ASSERT( bcluster->baseobject != NULL );
			if ( bcluster->baseobject->VisibleFrame == VISFRAME_NEVER ) {
				cluster = cluster->next;
				continue;
			}
		}

		// determine if cluster visible
		if ( CullWholeCluster( cluster ) ) {
			cluster = cluster->next;
			continue;
		}
		visible_clusters++;

		// check if ship entered energy field
		CheckEnergyField( cluster );

		// draw particles in cluster
		int numactive = DrawClusterParticles( cluster );

		// remember next cluster in list
		pcluster_s *temp = cluster->next;

		// remove cluster if no active particles contained anymore
		if ( numactive == 0 ) {
//			ASSERT(0);//FIXME: we currently assume this will never be hit!

   			PRT_DeleteCluster( cluster );
		}

		cluster = temp;
	}

	AUXDATA_NUM_PARTICLE_CLUSTERS = walked_clusters;
	AUXDATA_NUM_VISIBLE_PCLUSTERS = visible_clusters;
}


// main entry point of the particle system ------------------------------------
//
void PRTSYS_DrawParticles()
{
	//NOTE:
	// this function is called each frame by
	// R_DrawParticles() (Rx_PART.C), which is
	// called by the gameloop (G_MAIN.C).

	// particle system enabled?
	if ( !ParticleSysEnabled || AUX_DISABLE_PARTICLE_SYSTEM )
		return;

	// let user manually create/destroy spheres (via console)
	DoUserControlledSphereCreation();

	// perform particle actions
	PAN_AnimateParticles();

	// walk list of clusters
	DrawClusterList();
}