File: xmover.C

package info (click to toggle)
flying 6.20-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 752 kB
  • ctags: 1,873
  • sloc: cpp: 10,966; makefile: 189
file content (1320 lines) | stat: -rw-r--r-- 33,473 bytes parent folder | download | duplicates (3)
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
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
//
// XMover: Modul zur Optimierten Ball-Darstellung unter XWindows
// - flackerfreies Bewegen der B�lle durch Hilfspixmaps
// - Beleuchtungseffekt durch Raytracing-Bitmaps
// - Darstellung halber Kugeln durch vorberechnete Pixmaps
//

#ifndef _vec3_h
#	include "vec3.h"
#endif

#define	LEFT_RIGHT_OPT
#define	USE_STUFFER

/*----------------------------------------------------------------------------*/

PixmapCache::PixmapCache() {
	count = 0;
}

PixmapCache::~PixmapCache() {
}

Pixmap PixmapCache::Lock( int id_in, int size_in ) {
	for (int i=0;i<count;i++) {
		if (id_in==id[i]&&size_in==size[i]) {
			lockcount[i]++;
			return pmap[i];
		}
	}
	return 0;
}

void PixmapCache::Unlock( Display *dpy, Pixmap pix ) {
//
// free the given pixmap, if nobody else will use it.
//
	for (int i=0;i<count;i++) {
		if (pix==pmap[i]) {
			if (--lockcount[i]==0) {
				XFreePixmap(dpy,pix);
				count--;
				while( i<count ) {
					pmap[i]      = pmap[i+1];
					size[i]      = size[i+1];
					id[i]        = id[i+1];
					lockcount[i] = lockcount[i+1];
					i++;
				}
			}
			return;
		}
	}
}

void PixmapCache::InsertAndLock( Display * /*dpy*/, Pixmap pix, int id_in, int size_in ) {
	if (Lock(id_in,size_in)) {
		fprintf(stderr,"internal error 1 in PixmapCache::InsertAndLock\n" );
		return;
	}
	if (count>=PCACHE_MAX) {
		fprintf(stderr,"internal error 2 in PixmapCache::InsertAndLock\n" );
		return;
	}
	pmap[count]      = pix;
	size[count]      = size_in;
	id[count]        = id_in;
	lockcount[count] = 1;
	count++;
}

PixmapCache PixmapCache::pcache;

/*----------------------------------------------------------------------------*/

//
// Klasse zur Komprimierung der RingState-Felder
//
class Stuffer {
	public:
		Stuffer() : ws(0), wl(sizeof(RingState))			{}

		void Init( int ws_in, int wl_in ) {
				ws = ws_in;
				if (wl!=wl_in) {
					printf( "ERROR: Stuffer used with illegal input size !\n" );
					exit(0);
				}
		}
		void Shrink( RingState *addr, int len );
		void Expand( RingState *addr, int len );
		int  SSize( int len )
#ifdef USE_STUFFER
		{		return ((len*ws+7)/8);	}
#else
		{		return ((len*wl+7)/8);	}
#endif
		int  LSize( int len )
		{		return ((len*wl+7)/8);	}

	private:
		int	ws;	// Bits klein
		const int	wl;	// Bits gross
		static mtab[9];
};

int Stuffer::mtab[9] = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };

#ifdef USE_STUFFER
void Stuffer::Shrink( RingState *addr, int len ) {
RingState		*r_p;
RingState		r_val;
int				r_len;
unsigned char	*w_p;
unsigned char	w_val;
int	w_space;

	r_p     = addr;
	w_p     = (unsigned char*)addr;
	w_space = 8;
	w_val   = 0;
	while( len-- ) {
		r_val = *r_p++;
		r_len = ws;

		while (r_len>=w_space) {
			w_val  |= ((unsigned char)r_val)&mtab[w_space];
			*w_p++  = w_val;
			w_val   = 0;
			r_val >>= w_space;
			r_len  -= w_space;
			w_space = 8;
		}

		if (r_len) {
			w_val   |= (((unsigned char)r_val)&mtab[w_space])<<(w_space-r_len);
			w_space -= r_len;
		}
	}
	*w_p++ = w_val;
}

void Stuffer::Expand( RingState *addr, int len ) {
RingState	*w_p;						// Schreibzeiger - expandiertes Ende
RingState	w_val;
int	w_space;							// fehlende Bits zum Gesamtwort
unsigned char	*r_p;					// Lesezeiger - komprimiertes Ende
unsigned char	r_val;				// gueltige Bits
int	r_len;							// Zahl gueltige Bits an Lesezeigerposition

	r_p     = (unsigned char*)addr + len*ws/8;		// Adresse des letzten Byte
	r_len   = (len*ws)%8;			// g�ltige Bits im letzen Byte
	if (r_len)	r_val = *r_p;		// letzte Bits holen
	else {		r_val = *--r_p;
					r_len = 8;
	}
	w_p     = addr + len;

	while( len-- ) {
		w_space = ws;
		w_val   =  0;
		while( r_len<=w_space ) {
			w_val <<= r_len;
			w_val  |= r_val>>(8-r_len);
			w_space-= r_len;
			r_val   = *--r_p;			// ABR: liest 1 Byte ueber die Untergrenze
			r_len   = 8;
		}
		if (w_space) {
			w_val   <<= w_space;
			w_val    |= r_val&mtab[w_space];
			r_len    -= w_space;
		}
		*--w_p  = w_val;
	}
}

#else
void Stuffer::Shrink( RingState *, int ) {}
void Stuffer::Expand( RingState *, int ) {}
#endif

/*----------------------------------------------------------------------------*/

class Bitmap {
public:
	void Clear()	{	memset( bits, 0, bytes_per_line*height ); }

	void Init( int w, int h, char *bmap ) {
		width				= w;
		bytes_per_line = (width+7)/8;
		height 			= h;
		bits 				= bmap;
	}
	Bitmap( int w, int h, char *bmap )	{ Init(w,h,bmap); }
	Bitmap()										{}

	int GetPixel( int x, int y ) {
		if ((x<0)||(x>=width)||(y<0)||(y>=height))		return 0;
		else {
			unsigned char mask = 1<<(x&7);
			return (bits[bytes_per_line*y+(x>>3)]&mask)?1:0;
		}
	}
	void SetPixel( int x, int y ) {
		unsigned char mask = 1<<(x&7);
		bits[bytes_per_line*y+(x>>3)] |= mask;
	}
	void ClrPixel( int x, int y ) {
		unsigned char mask = 1<<(x&7);
		bits[bytes_per_line*y+(x>>3)] &= ~mask;
	}


	Pixmap Create() {
		return XCreatePixmapFromBitmapData(dpy,win,bits,width,height,1,0,1 );
	}
	void Print();

	int	width;
	int	height;
protected:
	int	bytes_per_line;
	char *bits;
};

class LocBitmap : public Bitmap {
public:
	void Init( int w, int h ) {
		Bitmap::Init(w,h,0);
		bits 				= new char[bytes_per_line*height];
		Clear();
	}
	void Init( int w, int h,  FILE *fp ) {
		Bitmap::Init(w,h,0);
		bits 				= new char[bytes_per_line*height];
		fread(bits,sizeof(char),bytes_per_line*height,fp);
	}
	void Write( FILE *fp ) {
		fwrite(bits,sizeof(char),bytes_per_line*height,fp);
	}
	LocBitmap( int w, int h )		{ Init(w,h); }

	LocBitmap()		{}
	~LocBitmap()		{ delete bits; }

	// change for a sun sparc-station runing 4.1.3 that thinks:
	// xmover.c:463: no non-hidden member function 'LocBitmap::create' defined
	Pixmap Create() { return Bitmap::Create(); }
};


void Bitmap::Print() {
	for(int y=0;y<height;y++) {
		for(int x=0;x<width;x++)	printf( "%d", GetPixel(x,y) );
		printf( "\n" );
	}
}

/*----------------------------------------------------------------------------*/

#ifdef STATISTICS
unsigned long	BallMover::moves = 0l;
#endif

BallMover::BallMover( const Real &r_in ) {
	r = (int)(r_in*w2n);	// Radius als Member festhalten
	d = 2*r;					// Bem: Durchmesser immer gerade

	bpix = help = 0;
}


BallMover::~BallMover() {
	if (help)	XFreePixmap( dpy, help );
	if (bpix)	XFreePixmap( dpy, bpix );
}


void BallMover::Init() {
LocBitmap	*bit;			// Kugelbitmap
int		x,y;
double	dr=r-0.5;		// tats�chlicher (Welt-)Radius

	bit = new LocBitmap( d, d );
	max_rad  = r-0.25+EPS;
	max_rad2 = max_rad * max_rad;
	for (y=0;y<d;y++) {
		for (x=0;x<d;x++) {
			double dx = x-dr;								// X-Abstand zur Kreismitte
			double dy = y-dr;								// Y-Abstand zur Kreismitte
			double dist2 = dx*dx + dy*dy;
			if (dist2<max_rad2) {
				bit->SetPixel(x,y);
			}
		}
	}

	bpix = bit->Create();

	d_help	= 2*d-1;
	help		= XCreatePixmap(dpy,win,d_help,d_help,DefaultDepth(dpy,0));

	delete bit;
}

void BallMover::DrawBallAt( int x, int y, int col_x )
{
#ifndef NODRAW
	XFillRectangle(dpy,help,gc_bclear,0,0,d,d);
	XCopyPlane(dpy,bpix,help,gc_ball[col_x],0,0,d,d,0,0,1);
	Pixmap	shadow = GetShadowMap(x,y);
	if (shadow)	XCopyPlane(dpy,shadow,help,gc_lay2,0,0,d,d,0,0,1);
	XCopyArea(dpy,help,win,gc_bxor,0,0,d,d,x-r, y-r );
#endif
}


void BallMover::MoveBallOnScreen( int oldx, int oldy, int newx, int newy, int col_x ) {

int	dx = newx-oldx;
int	dy = newy-oldy;
int absx = (dx>0)?dx:-dx;
int absy = (dy>0)?dy:-dy;

#ifndef NODRAW
	if ( (absx<d_help-d) && (absy<d_help-d) ) {
		Pixmap	shadow;

		int	width = d+absx;		// tats�chlicher Ausschnittgr��e der Pixmap
		int	height= d+absy;
		int	ox = (dx>0)?0:absx;	// relativer Abstand alte Position
		int	oy = (dy>0)?0:absy;
		int	nx = (dx>0)?absx:0;	// relativer Abstand neue Position
		int	ny = (dy>0)?absy:0;

		XFillRectangle(dpy,help,gc_bclear,0,0,width,height);

		XCopyPlane(dpy,bpix,help,gc_ball[col_x],0,0,d,d,ox,oy,1);
		shadow = GetShadowMap(oldx,oldy);
		if (shadow)	XCopyPlane(dpy,shadow,help,gc_lay2,0,0,d,d,ox,oy,1);

		XCopyPlane(dpy,bpix,help,gc_ball[col_x],0,0,d,d,nx,ny,1);
		shadow = GetShadowMap(newx,newy);
		if (shadow)	XCopyPlane(dpy,shadow,help,gc_lay2,0,0,d,d,nx,ny,1);

		XCopyArea(dpy,help,win,gc_bxor,0,0,width,height,oldx-ox-r, oldy-oy-r );
	}
	else {
		DrawBallAt(oldx,oldy,col_x);
		DrawBallAt(newx,newy,col_x);
	}
#endif

#ifdef STATISTICS
	moves++;
#endif
}

Pixmap BallMover::GetShadowMap( int /*x*/, int /*y*/ )		{ return 0; }

// -------------------------------------------------------------------------

DiscMover::DiscMover( const Real &r ) :
	BallMover( r )
{
	lpix = 0;
}

DiscMover::~DiscMover()
{
	if (lpix)	XFreePixmap(dpy,lpix);
}

void DiscMover::Init() {
LocBitmap	*lbit;		// Kreisbitmap
int		x,y;
double	dr=r-0.5;		// tats�chlicher (Welt-)Radius

	BallMover::Init();

	lbit	= new LocBitmap( d, d );
double	shade_rad	= 1.2;			// Pixelbreite in WS-umgerechnet
double	inner1		= r*1/4;
double	inner2		= r*2/4;

	for (y=0;y<d;y++) {
		for (x=0;x<d;x++) {
			double dx = x-dr;								// X-Abstand zur Kreismitte
			double dy = y-dr;								// Y-Abstand zur Kreismitte
			double dist = sqrt(dx*dx + dy*dy);
			if (dist<max_rad) {
				if (  (dist>max_rad-shade_rad)
					|| (dist>inner1-shade_rad)&&(dist<inner1)
					|| (dist>inner2-shade_rad)&&(dist<inner2) ) {
					lbit->SetPixel(x,y);
				}
			}
		}
	}

	lpix = lbit->Create();
	delete lbit;
}

Pixmap DiscMover::GetShadowMap( int /*x*/, int /*y*/ )		{ return lpix; }

// -------------------------------------------------------------------------

ShadedBallMover::ShadedBallMover( const Real &r ) :
	BallMover( r )
{
	lpix=0;
}

ShadedBallMover::~ShadedBallMover()
{
	if (lpix) {
		for (int i=0;i<lpixs_all;i++) {
			XFreePixmap( dpy, lpix[i] );
		}
		delete [] lpix;
		lpix = 0;
	}
}

#if (0)
const	double circ_x	=  -50.0;
const	double circ_y	=  -50.0;
const	double circ_r1 =  150.0;
const	double circ_r2 =  300.0;
#endif


void ShadedBallMover::Init() {
int			x,y,i;

LocBitmap	*lbit;	// Feld der Hilfbitmaps

double	dr=r-0.5;		// tats�chlicher (Welt-)Radius

int		ncircs;			// number of lamps
double	circ_z;			// hight of lamps
struct lightdata {
	double	x,y;			// position of lamps
	double	r1, r2;		// size (inner/outer) of lamps
}			circ[3];

	BallMover::Init();

	if (light_flag) {
		lpixs_x = 24;     							// Zahl der Bitmaps in X-Richtung
		lpixs_y = 12;									// Zahl der Bitmaps in Y-Richtung
	}
	else {
		lpixs_x = 0;
		lpixs_y = 0;
	}
	lpixs_all = lpixs_x * lpixs_y;			// Gesamtzahl
	distx = max_x / lpixs_x;					// Weltkoordinatenraster X
	disty = max_y / lpixs_y;					// Weltkoordinatenraster Y
	lbit = new LocBitmap[lpixs_all];			// Anlegen der Bitmaps
	for (i=0;i<lpixs_all;i++) 	lbit[i].Init(d,d);

	if (light_flag<=1) {
		ncircs = 1;								// single light, far far away
		circ_z = 2000.0;
		circ[0].x= -50.0; circ[0].y= -50.0; circ[0].r1= 150.0; circ[0].r2 = 300.0;
	}
	else {
		ncircs = 3;
		circ_z = MaxX() * 2.0 / 3.0;		// einfach 'mal so angenommen
		for (int l=0;l<ncircs;l++) {
			circ[l].x  = MaxX() * (double)(l+1) / 4.0;
			circ[l].y  = MaxY() /  2.0;
			circ[l].r1 = MaxX() / 16.0;
			circ[l].r2 = MaxX() /  8.0;
		}
	}


	DBG1( UnixTrace, "Starting image processing with ball size %d pixel:\n", d );
	DBG1( UnixTrace, "BitmapData: >%d bytes\n", (d+7)/8*d*lpixs_all );

	for (y=0;y<d;y++) {							// F�r alle Punkte der Bitmaps
		for (x=0;x<d;x++) {
			double dx = x-dr;								// X-Abstand zur Kreismitte
			double dy = y-dr;								// Y-Abstand zur Kreismitte
			double dist2 = dx*dx + dy*dy;
			if (dist2<max_rad2) {

				double dz = dr*dr-dist2;				// quadratischer Z-Abstand
				if (dz>=0) {
					double f = circ_z/sqrt(dz);		// Multiplikator zur Decke
					dx *= f;									//	XPos an Decke
					dy *= f;									// YPos an Decke

					for (int px=0;px<lpixs_x;px++) {	// f�r alle Bitmaps
						for (int py=0;py<lpixs_y;py++) {
							int	lflag=0;		// 0-dark, 1-medium, 2-light
							for (int l=0;l<ncircs;l++) {
							// Weltposition relativ zu Kreislicht
								double wx = (px*distx+(distx>>1)+dx)/w2n-circ[l].x;
								double wy = (py*disty+(disty>>1)+dy)/w2n-circ[l].y;
								double d = wx*wx+wy*wy;
								if (d<circ[l].r2*circ[l].r2) {
									if (d<circ[l].r1*circ[l].r1) {
										// inner circle
											lflag=2; break;
									}
									else	lflag=1;
								}
							}
							if ( (lflag==2) || (lflag&&((x^y)&1)) ) {
								lbit[px+py*lpixs_x].SetPixel(x,y);
							}
						}
					}
				}
			}
		}
	}

	lpix = new Pixmap[lpixs_all];
	for (i=0;i<lpixs_all;i++) {
		lpix[i] = lbit[i].Create();
	}
	delete [] lbit;
}


Pixmap ShadedBallMover::GetShadowMap( int x, int y ) {
int	xp = x/distx;
	if (xp<0)			xp=0;
	if (xp>=lpixs_x)	xp=lpixs_x-1;
int	yp = y/disty;
	if (yp<0)			yp=0;
	if (yp>=lpixs_y)	yp=lpixs_y-1;

	return lpix[xp+yp*lpixs_x];
}


void ShadedBallMover::CreateLightWindow() {
Window	help;
const int o=2;

	help=CreateWindow( "Beleuchtung", lpixs_x*(d+o)+1 , lpixs_y*(d+o)+1 );
	for (int px=0;px<lpixs_x;px++) {
		for (int py=0;py<lpixs_y;py++) {
			XCopyPlane(dpy,bpix,help,gc_ball[2],0,0,d,d,o/2+px*(d+o),o/2+py*(d+o),1);
			XCopyPlane(dpy,lpix[px+py*lpixs_x],help,gc_lay2,0,0,d,d,o/2+px*(d+o),o/2+py*(d+o),1);

		}
	}
}

// -------------------------------------------------------------------------

const int HalfBallMover::o=4;

HalfBallMover::HalfBallMover( const Real &r, int mode_in ) :
	ShadedBallMover( r )
{
	rpix	= 0;		// Ring-Pixmap-Feld
	nbpix = 0;		// negative Pixmap zum Debuggen
	tw		= 0;
	mode	= mode_in;

	right = left = up = down = 0;
}

HalfBallMover* HalfBallMover::Create( const Real &r, int mode_in ) {
	switch (enhanced_mover) {
	case 2:		return new HiEnhancedHalfBallMover(r,mode_in);
	case 1:		return new LoEnhancedHalfBallMover(r,mode_in);
	default:		return new HalfBallMover(r,mode_in);
	}
}

HalfBallMover::~HalfBallMover()
{
	if (rpix) {
		for (int i=0;i<rpixs_all;i++) {
			XFreePixmap( dpy, rpix[i] );
		}
		delete [] rpix;
		rpix = 0;
	}
	if (nbpix)		XFreePixmap( dpy, nbpix );

	if (right)		delete [] right;
	if (left)		delete [] left;
	if (up)			delete [] up;
	if (down)		delete [] down;
}

void HalfBallMover::Init() {
int			x,y,i,l,b;

LocBitmap	*rbit;	// Feld der Ring-Bitmaps
Vec3			*pv;		// Vec2 zum Pol der Kugel der entsprechenden Bitmap

	ShadedBallMover::Init();

//
// Zahl der Pixmap's, Gr��e der Felder bestimmen
//
	sym = (mode>1)?2.0:1.0;
	//sym = 1.0;

	if (mode) {
		rpixs_l   = (int)(3.14*d/sym);   // Zahl der Bitmaps in L�ngen-Richtung
		rpixs_b   = (int)(3.14*d/2.0);	// Zahl der Bitmaps in Breiten-Richtung
		mult	= 6;								// Internes Raster
	}
	else {
		rpixs_l = rpixs_b = 1;
		mult = 1;
	}
	mult2 = mult/2;
	rpixs_all = rpixs_l * rpixs_b;		// Gesamtzahl

	vecs_l    = rpixs_l * mult;			// Hilfsraster feiner als Bitmap-Raster
	vecs_b    = rpixs_b * mult;

	vecs_all  = vecs_l * vecs_b;			// Gesamtgr��e des Rasters
	for (i=0;i<(int)sizeof(RingState);i++)	vecs_all/=256;
	if (vecs_all>0) {
		printf( "too many RingStates (%d) -> switch type to unsigned long (in xmover.h)\n",
					vecs_l * vecs_b );
		exit(0);
	}
	vecs_all  = vecs_l * vecs_b;			// Gesamtgr��e des Rasters

char	fname[80];
	sprintf(fname,"%s/fly%d-%d.dta",DATA_DIRECTORY,mode,d);
FILE	*fp;

#ifdef DEBUG
	fp = (debug&ForceCalc)?0:fopen( fname, "r" );
#else
	fp = fopen( fname, "r" );
#endif

	rbit = new LocBitmap[rpixs_all];		// Anlegen der Bitmaps
	for (l=0;l<rpixs_l;l++) {				// f�r alle Bitmaps
		for (b=0;b<rpixs_b;b++) {
			if (!fp)	rbit[PixIndex(l,b)].Init(d,d);
			else		rbit[PixIndex(l,b)].Init(d,d,fp);
		}
	}

	pv   = new Vec3[d*d];
	for (y=0;y<d;y++) {
		for (x=0;x<d;x++) {
			double dx = x-r+0.5;								// X-Abstand zur Kreismitte
			double dy = y-r+0.5;								// Y-Abstand zur Kreismitte
			double dist2 = dx*dx + dy*dy;					// Dist. zur sichtbaren Mitte
			if (dist2<max_rad2) {
		//		double dzr = sqrt(dr*dr-dy*dy);		// Kugel-Radius auf H�he z
		//		double dz  = sqrt(dzr*dzr-dx*dx);	// fehlende Tiefenkoordinate
				double dz  = sqrt(r*r-dist2);
				pv[x+y*d] = Vec3( dx, dz, dy );
			}
			else {
				pv[x+y*d] = Vec3( 0.0, 0.0, 0.0 );
			}
		}
	}

#ifdef DEBUG
double	start_time = GetCurrentTime();
#endif

	if (!fp) {
		DBG3( UnixTrace, "Rings need %dx%d=%d Bitmaps\n",rpixs_l,rpixs_b,rpixs_all);
		DBG1( UnixTrace, "BitmapData:   >%7d Bytes\n", (d+7)/8*d*rpixs_all );
		DBG1( UnixTrace, "+ temporary:  >%7d Bytes\n",
					 d*d*(sizeof(Vec3)+sizeof(Bitmap)+(d*(d+7)/8) ) );

//
// Bitmaps ausrechen
//
		printf( "image processing: 00%%" ); fflush(stdout);

		for (l=0;l<rpixs_l;l++) {	// f�r alle Bitmaps
			printf( "\b\b\b%02d%%", (int)100*l/rpixs_l ); fflush(stdout);
			for (b=0;b<rpixs_b;b++) {
				Vec3	v = Vec3( lToDeg(l*mult+mult2), bToDeg(b*mult+mult2) )*Real(r);
				switch(mode) {
				case 1:	for (y=0;y<d;y++) {		// Volle Kugeln
								for (x=0;x<d;x++) {
									if (!pv[x+y*d].IsZero()) {
										Real	ang = v.AngleRadTo( pv[x+y*d] );
										if (ang>0.35)	rbit[PixIndex(l,b)].SetPixel(x,y);
									}
								}
							}
							break;
				case 2:	for (y=0;y<d;y++) {		// Halbe Kugeln
								for (x=0;x<d;x++) {
									if (!pv[x+y*d].IsZero()) {
										Real	ang = v.AngleRadTo( pv[x+y*d] );
										if (ang>1.0708 && ang<2.0708)
											rbit[PixIndex(l,b)].SetPixel(x,y);
									}
								}
							}
							break;
				case 3:	for (y=0;y<d;y++) {		// Volle Kugeln 2
								for (x=0;x<d;x++) {
									if (!pv[x+y*d].IsZero()) {
										Real	ang = v.AngleRadTo( pv[x+y*d] );
										if (ang>0.35)	rbit[PixIndex(l,b)].SetPixel(x,y);
									}
								}
							}
							break;
				case 4:	for (y=0;y<d;y++) {		// Halbe Kugeln 2
								for (x=0;x<d;x++) {
									if (!pv[x+y*d].IsZero()) {
										Real	ang = v.AngleRadTo( pv[x+y*d] );
										if ((ang>0.35&&ang<1.27)||(ang>1.87&&ang<2.79))
											rbit[PixIndex(l,b)].SetPixel(x,y);
									}
								}
							}
							break;
				default:	for (y=0;y<d;y++) {
								for (x=0;x<d;x++) {
									if (!pv[x+y*d].IsZero()) {
										rbit[PixIndex(l,b)].SetPixel(x,y);
									}
								}
							}
							break;
				}
			}
		}

		printf( "\b\b\bdone\n" );
	}



//
// RingState-Felder anfordern und initialisieren
// - zu jeder Pixmap existiert dabei in ein Eintrage mit einer
//   Folgepixmap in Feldern zu jeder Himmelsrichtung.
//
	DBG4( UnixTrace, "RingStateData:>%7d Bytes (for %dx%d=%d States)\n",
				 4*vecs_all*sizeof(RingState), vecs_l, vecs_b, vecs_all );

	right	= new RingState[vecs_all];
	left	= new RingState[vecs_all];
	up		= new RingState[vecs_all];
	down	= new RingState[vecs_all];

//
// Stuffer anlegen und initialisieren
//
Stuffer	stuff;
	for (i=0,l=vecs_all;l;l>>=1)	i++;		// Bits in vecs_all zaehlen
	stuff.Init(i,sizeof(RingState));

	if (fp) {
		printf( "reading %s\n", fname );

#ifndef LEFT_RIGHT_OPT
		fread( right,1,stuff.SSize(vecs_all), fp );
		stuff.Expand( right, vecs_all );
		fread( left, 1,stuff.SSize(vecs_all), fp );
		stuff.Expand( left, vecs_all );
#endif
		fread( up,   1,stuff.SSize(vecs_all), fp );
		stuff.Expand( up, vecs_all );
		fread( down, 1,stuff.SSize(vecs_all), fp );
		stuff.Expand( down, vecs_all );
	}
	else {
Real	b_ang = (double)mult*M_PI/vecs_b;	// Rotationswinkel pro Pixel

		printf( "state processing: 00%%" ); fflush(stdout);

		for (l=0;l<vecs_l;l++) {
			printf( "\b\b\b%02d%%", (int)100*l/vecs_l ); fflush(stdout);

			for (int b=0;b<vecs_b;b++) {
				RingState	st = AngVec2St(l,b);				// aktueller Index
				RingState	nst;									// auszurechnender State
				Real	newl, newb;

				Vec3	v( lToDeg(l), bToDeg(b) );

				v.XTurnAngleRad(b_ang).GetPolarRad(&newl,&newb);
				nst = AngRad2St( newl, newb );
				if (st==nst)	DBG2( Loops, "Loop-Warning Up:    %03d/%03d\n",l,b );
				up[st] = nst;


				v.XTurnAngleRad(-b_ang).GetPolarRad(&newl,&newb);
				nst = AngRad2St( newl, newb );
				if (st==nst)	DBG2( Loops, "Loop-Warning Down:  %03d/%03d\n",l,b );
				down[st] = nst;

#ifndef LEFT_RIGHT_OPT
				v.ZTurnAngleRad(-b_ang).GetPolarRad(&newl,&newb);
				nst = AngRad2St( newl, newb );
				if (st==nst)	DBG2( Loops, "Loop-Warning Right: %03d/%03d\n",l,b );
				right[st] = nst;

				v.ZTurnAngleRad(b_ang).GetPolarRad(&newl,&newb);
				nst = AngRad2St( newl, newb );
				if (st==nst)	DBG2( Loops, "Loop-Warning Left:  %03d/%03d\n",l,b );
				left[st] = nst;
#endif
			}
		}

		printf( "\b\b\bdone\n" );

		DBG1( UnixTrace, "Time: %g secs.\n", GetCurrentTime()-start_time );
	}
	
#ifdef LEFT_RIGHT_OPT
	for (l=0;l<vecs_l;l++) {
		for (int b=0;b<vecs_b;b++) {
			RingState	st = AngVec2St(l,b);				// aktueller Index
			right[st] = AngVec2StBnd((l+(vecs_l*(int)sym)-mult)%(vecs_l*(int)sym),b);
			left[st]  = AngVec2StBnd((l+mult)%(vecs_l*(int)sym),b);
		}
	}
#endif

#ifdef DEBUG
	if (debug&ShowRings) {
		//
		// Eine spezielle Pixmap wird erzeugt, die eine Negation der Balldarstellung
		// ist, um in dem Trace-Window einzelne Pixmap's herzuheben.
		//
		nbpix	= XCreatePixmap(dpy,bpix,d+o,d+o,1);
		GC	gc = XCreateGC( dpy, nbpix, 0, 0l );
		XSetFunction(dpy,gc,GXset);
		XFillRectangle(dpy,nbpix,gc,0,0,d+o,d+o);			// alle Pixmap setzen
		XSetFunction(dpy,gc,GXxor);
		XCopyArea(dpy,bpix,nbpix,gc,0,0,d,d,o/2,o/2);	// Ball l�schen
		XFreeGC(dpy,gc);
	}
#endif

	if (fp) {
		fclose(fp);
		fp = 0;
	}
	else {
		fp = fopen( fname, "w" );
	}
//
// Bitmaps in Pixmaps wandeln und interne Strukturen freigeben
//
	rpix = new Pixmap[rpixs_all];
	for (i=0;i<rpixs_all;i++) {
		rpix[i] = rbit[i].Create();
		if (fp) rbit[i].Write( fp );
	}
	delete [] rbit;
	delete [] pv;

	if (fp) {
		printf( "writing %s\n", fname );

#ifndef LEFT_RIGHT_OPT
		stuff.Shrink( right, vecs_all );
		fwrite( right,1,stuff.SSize(vecs_all), fp );
		stuff.Expand( right, vecs_all );
		stuff.Shrink( left, vecs_all );
		fwrite( left, 1,stuff.SSize(vecs_all), fp );
		stuff.Expand( left, vecs_all );
#endif
		stuff.Shrink( up, vecs_all );
		fwrite( up,   1,stuff.SSize(vecs_all), fp );
		stuff.Expand( up, vecs_all );
		stuff.Shrink( down, vecs_all );
		fwrite( down, 1,stuff.SSize(vecs_all), fp );
		stuff.Expand( down, vecs_all );
		fclose(fp);
	}
}



void HalfBallMover::CreateRingWindow() {
	tw=CreateWindow( "Ringverteilung", rpixs_l*(d+o)+1 , rpixs_b*(d+o)+1 );
	for (int l=0;l<rpixs_l;l++) {
		for (int b=0;b<rpixs_b;b++) {
			XCopyPlane(dpy,bpix,tw,gc_ballwhite,0,0,d,d,o/2+l*(d+o),o/2+b*(d+o),1);
			XCopyPlane(dpy,lpix[(l*lpixs_x/rpixs_l)+(b*lpixs_y/rpixs_b)*lpixs_x],tw,gc_lay2,0,0,d,d,o/2+l*(d+o),o/2+b*(d+o),1);
			XCopyPlane(dpy,rpix[PixIndex(rpixs_l-1-l,b)],tw,gc_ball[7],0,0,d,d,o/2+l*(d+o),o/2+b*(d+o),1);
		}
	}
#ifdef DEBUG
	if (debug&xwd) {
		char command[200];
		XSync(dpy,0);
		sprintf( command, "/usr/bin/X11/xwd -id %ld -frame > XWD/rings", tw );
		system(command);
	}
#endif
}


#define _INFO

void HalfBallMover::CreateTurnWindow() {
Window	win;

	win=CreateWindow( "Senkrecht-Rotation", rpixs_b*(d+o)+1,2*rpixs_b*(d+o)+1 );
	for (int b=0;b<rpixs_b;b++) {
#ifdef INFO
		printf( "Row %d: angle: %f\n", b, (double)bToDeg(b*mult+mult2) );
#endif
		RingState	st = AngPix2St(0,b);
		for (int y=0;y<2*rpixs_b;y++) {
#ifdef INFO
		int lp,bp;
		int lv,bv;
	//
	// Ausgabe der Tabellenerte
	//
		St2AngVec(st,&lv,&bv);
		St2AngPix(st,&lp,&bp);
		printf( "  Line %2d: Vec2: %03d/%03d, Pixmap: %02d/%02d",
			y, lv, bv, lp, bp );

	//
	// Berechnung der direkten Werte zum Vergleich
	//
		Real	b_ang=(double)mult*M_PI/vecs_b;   // Rotationswinkel pro Pixel
		Vec3	v( lToDeg(mult2), bToDeg(b*mult+mult2) );
		Real	newl, newb;
		v.XTurnAngleRad((y*b_ang)).GetPolarRad(&newl,&newb);
		RingState nst = AngRad2St( newl, newb );
	// Ausgabe
		St2AngVec(nst,&lv,&bv);
		St2AngPix(nst,&lp,&bp);
		printf( " <=> Vec2: %03d/%03d, Pixmap: %02d/%02d\n",
			lv, bv, lp, bp );

		if (((b^y)&1)) {
			XCopyPlane(dpy,bpix,win,gc_ballwhite,0,0,d,d,o/2+b*(d+o),o/2+y*(d+o),1);
			XCopyPlane(dpy,lpix[(b*lpixs_x/rpixs_b)+(y/2*lpixs_y/rpixs_b)*lpixs_x],win,gc_lay2,0,0,d,d,o/2+b*(d+o),o/2+y*(d+o),1);
			XCopyPlane(dpy,rpix[PixIndex(nst)],win,gc_ball[7],0,0,d,d,o/2+b*(d+o),o/2+y*(d+o),1);
		}
		else
#endif
		{
			XCopyPlane(dpy,bpix,win,gc_ballwhite,0,0,d,d,o/2+b*(d+o),o/2+y*(d+o),1);
			XCopyPlane(dpy,lpix[(b*lpixs_x/rpixs_b)+(y/2*lpixs_y/rpixs_b)*lpixs_x],win,gc_lay2,0,0,d,d,o/2+b*(d+o),o/2+y*(d+o),1);
			XCopyPlane(dpy,rpix[PixIndex(st)],win,gc_ball[8],0,0,d,d,o/2+b*(d+o),o/2+y*(d+o),1);
		}
			st = Turn(st,0,1);
		}
	}
#ifdef DEBUG
	if (debug&xwd) {
		char command[200];
		XSync(dpy,0);
		sprintf( command, "/usr/bin/X11/xwd -id %ld -frame > XWD/turns", win );
		system(command);
	}
#endif
}



#if (1)

#define	LINE(DY,YDEC,YADDOP,YTAB,DX,XLESS,XTAB) \
	int	dy_c = DY; \
	int	c    = 0; \
	while(DY) { \
		st = YTAB [st]; \
		DY YDEC; \
		c -= DX;	\
		if (c XLESS 0) {	\
			c YADDOP dy_c;	\
			st = XTAB [st];	\
		};	\
	}


RingState HalfBallMover::Turn( RingState st, int dx, int dy ) {

	if (dx>0) {
		if (dy>0) {
			if (dy>dx)	{ LINE(dy,--,+=,up,   dx,<,right);	}
			else			{ LINE(dx,--,+=,right,dy,<,up);		}
		}
		else {				// dy negativ => alle DY-Parameter umkehren
			if (-dy>dx)	{ LINE(dy,++,-=,down, dx,<,right);	}
			else			{ LINE(dx,--,+=,right,dy,>,down);	}
		}
	}
	else {				// dx negativ => alle DX-Parameter umkehren
		if (dy>0) {
			if (dy>-dx)	{ LINE(dy,--,+=,up,   dx,>,left);	}
			else			{ LINE(dx,++,-=,left ,dy,<,up);		}
		}
		else {				// dy negativ => alle DY-Parameter umkehren
			if (-dy>-dx){ LINE(dy,++,-=,down, dx,>,left);	}
			else			{ LINE(dx,++,-=,left ,dy,>,down);	}
		}
	}

	return st;
}

#else
RingState HalfBallMover::Turn( RingState st, int dx, int dy ) {
	while( dx && dy ) {
		if (dx>0)				{	dx--; st = right[st];	}
		else if (dx<0)			{	dx++; st = left[st]; }
		if (dy>0)				{	dy--;	st = up[st];	}
		else if (dy<0)			{	dy++;	st = down[st];	}
	}
	if (dx>0)				{ while(dx--)		st = right[st]; }
	else if (dx<0)			{ while(dx++)		st = left[st]; }
	if (dy>0)				{ while(dy--)		st = up[st]; }
	else if (dy<0)			{ while(dy++)		st = down[st]; }
	return st;
}
#endif


#ifdef DEBUG
void HalfBallMover::ShowDebugRing(RingState st, int col_x) {
	int	l,b;
	St2AngPix(st,&l,&b);
	XCopyPlane(dpy,nbpix,tw,gc_ballwhite,  0,0,d+o,d+o,(rpixs_l-1-l)*(d+o),b*(d+o),1);
	XCopyPlane(dpy,nbpix,tw,gc_ball[col_x],0,0,d+o,d+o,(rpixs_l-1-l)*(d+o),b*(d+o),1);
}
#endif

void HalfBallMover::RollBallAt( int x, int y, RingState st, int col_x )
{
#ifndef NODRAW
	XFillRectangle(dpy,help,gc_bclear,0,0,d,d);
	XCopyPlane(dpy,bpix,help,gc_ballwhite,0,0,d,d,0,0,1);
	XCopyPlane(dpy,GetShadowMap(x,y),help,gc_lay2,0,0,d,d,0,0,1);
	XCopyPlane(dpy,rpix[PixIndex(st)],help,gc_ball[col_x],0,0,d,d,0,0,1);
	XCopyArea(dpy,help,win,gc_bxor,0,0,d,d,x-r, y-r );

#ifdef DEBUG
	if ((tw)&&(debug&ShowRings))		ShowDebugRing(st,col_x);
#endif
#endif
}


void HalfBallMover::RollBallOnScreen( int oldx, int oldy, RingState ost,
				int newx, int newy, RingState *nst, int col_x ) {

int	dx = newx-oldx;
int	dy = newy-oldy;

		*nst = Turn( ost, dx, dy );

int absx = (dx>0)?dx:-dx;
int absy = (dy>0)?dy:-dy;

#ifndef NODRAW
	if ( (absx<d_help-d) && (absy<d_help-d) ) {
		int	width = d+absx;		// tats�chlicher Ausschnittgr��e der Pixmap
		int	height= d+absy;
		int	ox = (dx>0)?0:absx;	// relativer Abstand alte Position
		int	oy = (dy>0)?0:absy;
		int	nx = (dx>0)?absx:0;	// relativer Abstand neue Position
		int	ny = (dy>0)?absy:0;

		XFillRectangle(dpy,help,gc_bclear,0,0,width,height);

		XCopyPlane(dpy,bpix,help,gc_ballwhite,0,0,d,d,ox,oy,1);
		XCopyPlane(dpy,GetShadowMap(oldx,oldy),help,gc_lay2,0,0,d,d,ox,oy,1);
		XCopyPlane(dpy,rpix[PixIndex(ost)],help,gc_ball[col_x],0,0,d,d,ox,oy,1);

		XCopyPlane(dpy,bpix,help,gc_ballwhite,0,0,d,d,nx,ny,1);
		XCopyPlane(dpy,GetShadowMap(newx,newy),help,gc_lay2,0,0,d,d,nx,ny,1);
		XCopyPlane(dpy,rpix[PixIndex(*nst)],help,gc_ball[col_x],0,0,d,d,nx,ny,1);

		XCopyArea(dpy,help,win,gc_bxor,0,0,width,height,oldx-ox-r, oldy-oy-r );

#ifdef DEBUG
		if ((tw)&&(debug&ShowRings)) {
			ShowDebugRing(ost,col_x);
			ShowDebugRing(*nst,col_x);
		}
#endif

	}
	else {
		RollBallAt(oldx,oldy,ost,col_x);
		RollBallAt(newx,newy,*nst,col_x);
	}
#endif

#ifdef STATISTICS
	moves++;
#endif
}


// ---------------------------------------------------------------------------
//                          LoEnhancedHalfBallMover
// ---------------------------------------------------------------------------


LoEnhancedHalfBallMover::LoEnhancedHalfBallMover( const Real &r, int mode_in )
: HalfBallMover( r, mode_in ) {
	lbpix = 0;
	boffset = 0;
	lb_dist = boffset+d;
}

LoEnhancedHalfBallMover::~LoEnhancedHalfBallMover() {
	if (lbpix) {
		PixmapCache::pcache.Unlock( dpy, lbpix );
		lbpix = 0;
	}
}

void LoEnhancedHalfBallMover::Init() {
	HalfBallMover::Init();

// create initialisation pixmap for all possible shades, which
// can directly be used as presets in the RollBall() functions
// A large Pixmap is created that contains all neccessary background-presets
// in a certain distance, so that a direct copy can be done, which presets
// the whole background help-pixmap.

	lbpix = PixmapCache::pcache.Lock( d, ENHANCED_LBPIX_ID );
	if (!lbpix ) {

// create map and clear it

		DBG1( UnixTrace, "EnhancedData: >%d bytes\n",
					(lb_dist*lpixs_x+d-1)*(lb_dist*lpixs_y+d-1) );
		lbpix = XCreatePixmap(dpy,win,
					lb_dist*lpixs_x+d-1,lb_dist*lpixs_y+d-1,DefaultDepth(dpy,0));
		XFillRectangle(dpy,lbpix,gc_bclear, 0,0,
					lb_dist*lpixs_x+d-1,lb_dist*lpixs_y+d-1);

// copy the balls & shades into the map

		for (int x=0;x<lpixs_x;x++) {
			for (int y=0;y<lpixs_y;y++) {
				XCopyPlane(dpy,bpix,   lbpix,gc_ballwhite,
										0,0,d,d,boffset+x*lb_dist,boffset+y*lb_dist,1);
				XCopyPlane(dpy,lpix[x+y*lpixs_x],lbpix,gc_lay2,    
										0,0,d,d,boffset+x*lb_dist,boffset+y*lb_dist,1);
			}
		}
		PixmapCache::pcache.InsertAndLock(dpy,lbpix,d,ENHANCED_LBPIX_ID);
	}
}


void LoEnhancedHalfBallMover::RollBallAt( int x, int y, RingState st, int col_x )
{
#ifdef DEBUG
	if (enhanced_mover<1) {
			HalfBallMover::RollBallAt(x,y,st,col_x);
			return;
	}
#endif

#ifndef NODRAW
int	xp,yp;
	WC2Index(x,y,&xp,&yp);
	XCopyArea(dpy,lbpix,help,gc_default,boffset+xp*lb_dist,boffset+yp*lb_dist,d,d,0,0);
	XCopyPlane(dpy,rpix[PixIndex(st)],help,gc_ball[col_x],0,0,d,d,0,0,1);
	XCopyArea(dpy,help,win,gc_bxor,0,0,d,d,x-r, y-r );

#ifdef DEBUG
	if ((tw)&&(debug&ShowRings))		ShowDebugRing(st,col_x);
#endif
#endif
}


void LoEnhancedHalfBallMover::RollBallOnScreen( int oldx, int oldy, RingState ost,
				int newx, int newy, RingState *nst, int col_x ) {
#ifdef DEBUG
	if (enhanced_mover<1) {
			HalfBallMover::RollBallOnScreen(oldx,oldy,ost,newx,newy,nst,col_x);
			return;
	}
#endif

int	dx = newx-oldx;
int	dy = newy-oldy;

		*nst = Turn( ost, dx, dy );

int absx = (dx>0)?dx:-dx;
int absy = (dy>0)?dy:-dy;

#ifndef NODRAW
	if ( (absx<d_help-d) && (absy<d_help-d) ) {
		int	width = d+absx;		// tats�chlicher Ausschnittgr��e der Pixmap
		int	height= d+absy;
		int	ox = (dx>0)?0:absx;	// relativer Abstand alte Position
		int	oy = (dy>0)?0:absy;
		int	nx = (dx>0)?absx:0;	// relativer Abstand neue Position
		int	ny = (dy>0)?absy:0;

		int	xp,yp;
		WC2Index(oldx,oldy,&xp,&yp);
		XFillRectangle(dpy,help,gc_bclear,0,0,width,height);

		XCopyArea(dpy,lbpix,help,gc_bxor,
					boffset-ox+xp*lb_dist,boffset-oy+yp*lb_dist,d_help,d_help,0,0);
		XCopyPlane(dpy,rpix[PixIndex(ost)],help,gc_ball[col_x],0,0,d,d,ox,oy,1);

		WC2Index(newx,newy,&xp,&yp);
		XCopyArea(dpy,lbpix,help,gc_bxor,
					boffset+xp*lb_dist,boffset+yp*lb_dist,d,d,nx,ny);
		XCopyPlane(dpy,rpix[PixIndex(*nst)],help,gc_ball[col_x],0,0,d,d,nx,ny,1);

		XCopyArea(dpy,help,win,gc_bxor,0,0,width,height,oldx-ox-r, oldy-oy-r );

#ifdef DEBUG
		if ((tw)&&(debug&ShowRings)) {
			ShowDebugRing(ost,col_x);
			ShowDebugRing(*nst,col_x);
		}
#endif
	}
	else {
		RollBallAt(oldx,oldy,ost,col_x);
		RollBallAt(newx,newy,*nst,col_x);
	}
#endif

#ifdef STATISTICS
	moves++;
#endif
}

// ---------------------------------------------------------------------------
//                          HiEnhancedHalfBallMover
// ---------------------------------------------------------------------------


HiEnhancedHalfBallMover::HiEnhancedHalfBallMover( const Real &r, int mode_in )
: LoEnhancedHalfBallMover( r, mode_in ) {
	boffset = d-1;
	lb_dist = 2*d-1;
}

HiEnhancedHalfBallMover::~HiEnhancedHalfBallMover() {
}

void HiEnhancedHalfBallMover::RollBallOnScreen( int oldx, int oldy, RingState ost,
				int newx, int newy, RingState *nst, int col_x ) {
#ifdef DEBUG
	if (enhanced_mover<2) {
			LoEnhancedHalfBallMover::RollBallOnScreen(oldx,oldy,ost,newx,newy,nst,col_x);
			return;
	}
#endif

int	dx = newx-oldx;
int	dy = newy-oldy;

		*nst = Turn( ost, dx, dy );

int absx = (dx>0)?dx:-dx;
int absy = (dy>0)?dy:-dy;

#ifndef NODRAW
	if ( (absx<d_help-d) && (absy<d_help-d) ) {
		int	width = d+absx;		// tats�chlicher Ausschnittgr��e der Pixmap
		int	height= d+absy;
		int	ox = (dx>0)?0:absx;	// relativer Abstand alte Position
		int	oy = (dy>0)?0:absy;
		int	nx = (dx>0)?absx:0;	// relativer Abstand neue Position
		int	ny = (dy>0)?absy:0;

		int	xp,yp;
		WC2Index(oldx,oldy,&xp,&yp);
		XCopyArea(dpy,lbpix,help,gc_default,
					boffset-ox+xp*lb_dist,boffset-oy+yp*lb_dist,d_help,d_help,0,0);
		XCopyPlane(dpy,rpix[PixIndex(ost)],help,gc_ball[col_x],0,0,d,d,ox,oy,1);

		WC2Index(newx,newy,&xp,&yp);
		XCopyArea(dpy,lbpix,help,gc_bxor,
					boffset+xp*lb_dist,boffset+yp*lb_dist,d,d,nx,ny);
		XCopyPlane(dpy,rpix[PixIndex(*nst)],help,gc_ball[col_x],0,0,d,d,nx,ny,1);

		XCopyArea(dpy,help,win,gc_bxor,0,0,width,height,oldx-ox-r, oldy-oy-r );

#ifdef DEBUG
		if ((tw)&&(debug&ShowRings)) {
			ShowDebugRing(ost,col_x);
			ShowDebugRing(*nst,col_x);
		}
#endif
	}
	else {
		RollBallAt(oldx,oldy,ost,col_x);
		RollBallAt(newx,newy,*nst,col_x);
	}
#endif

#ifdef STATISTICS
	moves++;
#endif
}