File: pathfinder.c

package info (click to toggle)
ale-clone 1.15pre16.3-1
  • links: PTS
  • area: contrib
  • in suites: potato
  • size: 2,432 kB
  • ctags: 3,217
  • sloc: ansic: 37,483; sh: 1,105; makefile: 343
file content (838 lines) | stat: -rw-r--r-- 17,825 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
/*
**	A clone of a famous game.
*/
/**@name pathfinder.c	-	The path finder routines. */
/*
**	I use breadth-first.
**
**	(c) Copyright 1998 by Lutz Sammer
**
**	$Id: pathfinder.c,v 1.22 1999/12/05 18:37:11 root Exp $
*/

//@{

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "clone.h"
#include "video.h"
#include "tileset.h"
#include "map.h"
#include "sound_id.h"
#include "unitsound.h"
#include "unittype.h"
#include "player.h"
#include "unit.h"
#include "pathfinder.h"

#define MAX_PATH	9		/// Maximal path part returned.

//
//	The matrix is used to generated the paths.
//
//		 0:	Nothing
//		1-8:	Field on the path 1->2->3->4->5...
//		88:	Marks the possible goal fields.
//		98:	Marks map border, for faster limits checks.
//
global unsigned char Matrix[(MaxMapWidth+1)*(MaxMapHeight+1)];	/// Path matrix

IfDebug(
global unsigned PfCounterFail;
global unsigned PfCounterOk;
global unsigned PfCounterDepth;
global unsigned PfCounterNotReachable;
);

/*----------------------------------------------------------------------------
--	PATH-FINDER LOW-LEVEL
----------------------------------------------------------------------------*/

/**
**	Create empty movement matrix.
**
**		98 98 98 98 98
**		98	    98
**		98	    98
**		98 98 98 98 98
*/
global unsigned char* CreateMatrix(void)
{
    unsigned char* matrix;
    unsigned i;
    unsigned w;
    unsigned h;
    unsigned e;

    matrix=Matrix;
    w=TheMap.Width+2;
    h=TheMap.Height+2;

    memset(matrix,98,w);
    memset(matrix+w,0,(w*h)-w*2);	// initialize matrix

    e=w*TheMap.Height;
    for( i=w; i<=e; ) {			// mark left and right border
	matrix[i]=98;
	i+=w;
	matrix[i-1]=98;
    }
    memset(matrix+i,98,w);

    return matrix;
}

/**
**	Mark place in matrix.
**
**	@param x	X position
**	@param y	Y position
**	@param w	Width
**	@param h	Height
**	@param matrix	Matrix
*/
local void MarkPlaceInMatrix(int x,int y,int w,int h,unsigned char* matrix)
{
    int xi;
    int xe;
    int mw;

    if( x<0 ) {				// reduce to map limits
	w=x;
	x=0;
    }
    if( x+w>TheMap.Width ) {
	w=TheMap.Width-x;
    }
    if( y<0 ) {
	w=y;
	y=0;
    }
    if( y+h>TheMap.Height ) {
	h=TheMap.Height-y;
    }

    DebugCheck( h==0 || w==0 );

    mw=TheMap.Width+2;
    xe=x+w;
    while( h-- ) {			// mark the rectangle
	for( xi=x; xi<xe; ++xi ) {
	    matrix[mw+1+xi+y*mw]=88;
	}
	++y;
    }
}

/**
**	Make goal in matrix.
**	'range' is how near we must reach.
**
**	       rrrr
**	       rggr
**	       rggr
**	       rrrr
*/
local void MarkGoalInMatrix(Unit* unit,int range,unsigned char* matrix)
{
    int x;
    int y;
    int w;
    int h;
    UnitType* type;

    x=unit->X-range;
    y=unit->Y-range;
    type=unit->Type;
    w=type->TileWidth+range*2;
    h=type->TileHeight+range*2;

    MarkPlaceInMatrix(x,y,w,h,matrix);
}

/**
**	Trace path in the matrix back to origin.
**
**	FIXME: didn't need to check the goal field again!
**	FIXME: only the last 8 points are returned!
*/
local void PathTraceBack(const unsigned char* matrix,int x,int y,int n
	,int* path)
{
    int w;
    int w2;
    const unsigned char* m;

    DebugLevel3(__FUNCTION__": %d\n",n);

    w=TheMap.Width+2;
    w2=w+w;

    path[n*2+0]=x;
    path[n*2+1]=y;

    m=matrix+1+w+y*w;

    for( ;; ) {
	DebugLevel3("%d,%d(%d)\n",x,y,n);
	n=((n-1)&0x7)+1;

	x++;					// +1, 0
	if( m[x]!=n ) {			// test all neighbors
	  x-=2;					// -1, 0
	  if( m[x]!=n ) {
	    x++;
	    y++;				//  0,+1
	    m+=w;
	    if( m[x]!=n ) {
	      y-=2;				//  0,-1
	      m-=w2;
	      if( m[x]!=n ) {
		x++;				// +1,-1
		if( m[x]!=n ) {
		  x-=2;				// -1,-1
		  if( m[x]!=n ) {
		    y+=2;			// -1,+1
		    m+=w2;
		    if( m[x]!=n ) {
		      x+=2;			// +1,+1
			if( m[x]!=n ) {
			    DebugLevel3("Start reached\n");
			    return;
			}
		    }
		  }
		}
	      }
	    }
	  }
	}
	// found: continue
	--n;
	path[n*2+0]=x;
	path[n*2+1]=y;
    }
}

/*
**	Mark path to goal.
*/
local int MarkPathInMatrix(Unit* unit,unsigned char* matrix)
{
    static int xoffset[]={  0,-1,+1, 0, -1,+1,-1,+1 };
    static int yoffset[]={ -1, 0, 0,+1, -1,-1,+1,+1 };
    struct {
	int X;
	int Y;
    } points[TheMap.Width*TheMap.Height];
    int x;
    int y;
    int mask;
    int wp;
    int rp;
    int ep;
    int n;
    int j;
    int w;

    w=TheMap.Width+2;
    mask=UnitMovementMask(unit);
#ifdef MOVE_MAP
    mask&=~(MapMoveLandUnit|MapMoveAirUnit|MapMoveSeaUnit);
#else
    mask&=~(MapFieldLandUnit|MapFieldAirUnit|MapFieldSeaUnit);
#endif

    points[0].X=x=unit->X;
    points[0].Y=y=unit->Y;
    matrix[w+1+x+y*w]=1;			// mark start point
    n=rp=0;
    ep=1;
    wp=1;					// start with one point

    //
    //	Pop a point from stack, push all neightbors which could be entered.
    //
    for( ;; ) {
	++n;
	while( rp!=ep ) {
	    for( j=0; j<8; ++j ) {		// mark all neighbors
		x=points[rp].X+xoffset[j];
		y=points[rp].Y+yoffset[j];
		if( matrix[w+1+x+y*w] ) {	// already reached
		    //
		    //	Check if goal reached.
		    //
		    if( matrix[w+1+x+y*w]==88 ) {
			DebugLevel3("Goal reached ");
			return 1;
		    }
		    continue;
		}
		if( CanMoveToMask(x,y,mask) ) {	// reachable
		    DebugLevel3("OK: %d,%d\n",x,y);
		    matrix[w+1+x+y*w]=n+1;
		    points[wp].X=x;
		    points[wp].Y=y;
		    wp++;
		    if( wp>=sizeof(points) ) {	// round about
			wp=0;
		    }
		} else {			// unreachable
		    DebugLevel3("NO: %d,%d\n",x,y);
		    matrix[w+1+x+y*w]=99;
		}
	    }

	    rp++;
	    if( rp>=sizeof(points) ) {	// round about
		rp=0;
	    }
	}
	DebugLevel3("%d,%d\n",rp,wp);

	//
	//	Continue with next frame.
	//
	if( rp==wp ) {			// unreachable, no points available
	    break;
	}
	ep=wp;
	n&=7;
    }

    return -1;
}

/*----------------------------------------------------------------------------
--	PATH-FINDER USE
----------------------------------------------------------------------------*/

/*
**	Can the unit 'src' reach the place x,y.
*/
global int PlaceReachable(Unit* src,int x,int y)
{
    unsigned char* matrix;

    DebugLevel3(__FUNCTION__": %p -> %d,%d\n",src,x,y);

    //
    //	Setup movement.
    //
    matrix=CreateMatrix();
    MarkPlaceInMatrix(x,y,1,1,matrix);

    if( MarkPathInMatrix(src,matrix) < 0 ) {
	DebugLevel3("Can't move to destination\n");
	return 0;
    }

    return 1;
}

/*
**	Can the unit 'src' reach the unit 'dst'.
*/
global int UnitReachable(Unit* src,Unit* dst)
{
    unsigned char* matrix;

    DebugLevel3(__FUNCTION__": %Zd(%d,%d,%s)->%Zd(%d,%d,%s) "
	,src-UnitsPool,src->X,src->Y,src->Type->Ident
	,dst-UnitsPool,dst->X,dst->Y,dst->Type->Ident);

    //
    //	Setup movement.
    //
    matrix=CreateMatrix();
    MarkGoalInMatrix(dst,1,matrix);

    if( MarkPathInMatrix(src,matrix)<0 ) {
	DebugLevel3("NO WAY\n");
	return 0;
    }
    DebugLevel3("OK\n");

    return 1;
}

/*----------------------------------------------------------------------------
--	REAL PATH-FINDER
----------------------------------------------------------------------------*/

/**
**	Fast search new path.
**
**	@param unit	Path for this unit.
**
**	@param xdp	OUT: Pointer for x direction.
**	@param xdp	OUT: Pointer for y direction.
**	@return		True, if could move to goal. False otherwise.
*/
local int FastNewPath(const Unit* unit,int *xdp,int* ydp)
{
    int x;
    int y;
    int xd;
    int yd;
    const UnitType* type;
    int mask;
    int steps;
    const Unit* goal;
    int r;

    //
    //	1) Fast check if the way is blocked in sight.
    //
    type=unit->Type;
    mask=TypeMovementMask(type);
    steps=unit->Stats->SightRange;	// only in sight range!

    x=unit->X;
    y=unit->Y;
    goal=unit->Command.Data.Move.Goal;
    if( goal ) {
	type=goal->Type;
	xd=goal->X;
	yd=goal->Y;
    } else {
	xd=unit->Command.Data.Move.DX;
	yd=unit->Command.Data.Move.DY;
    }

    xd-=x;
    if( xd<0 ) xd=-1; else if( xd>0 ) xd=1;
    *xdp=xd;
    yd-=y;
    if( yd<0 ) yd=-1; else if( yd>0 ) yd=1;
    *ydp=yd;
    DebugLevel3(__FUNCTION__": %d,%d\n",xd,yd);

    r=unit->Command.Data.Move.Range;
    while( steps-- ) {
	x+=xd;
	y+=yd;
	DebugLevel3(__FUNCTION__": Check %d,%d=%x\n",x,y,mask);

	//
	//	Now check if we can move to this field
	//
	if( !CheckedCanMoveToMask(x,y,mask) ) {	// blocked
	    DebugLevel3(__FUNCTION__": The way is blocked in sight range\n");
	    return 0;
	}

	//
	//	Check if goal is reached
	//	FIXME: This could be improved
	//
	if( goal ) {
	    DebugLevel3(__FUNCTION__": Unit %d, %d Goal %d,%d - %d,%d\n"
		,x,y
		,goal->X-r,goal->Y-r
		,goal->X+type->TileWidth+r
		,goal->Y+type->TileHeight+r);
	    if( x>=goal->X-r && x<goal->X+type->TileWidth+r
		    && y>=goal->Y-r && y<goal->Y+type->TileHeight+r ) {
		DebugLevel3(_FUNCTION__": Goal in sight\n");
		return 1;
	    }
	    xd=goal->X;
	    yd=goal->Y;
	} else {
	    if( x>=unit->Command.Data.Move.DX
		    && x<=unit->Command.Data.Move.DX+r
		    && y>=unit->Command.Data.Move.DY
		    && y<=unit->Command.Data.Move.DY+r ) {
		DebugLevel3(__FUNCTION__": Field in sight\n");
		return 1;
	    }
	    xd=unit->Command.Data.Move.DX;
	    yd=unit->Command.Data.Move.DY;
	}

	//
	//	Try next field.
	//	From now on ignore any units.
	//
	mask&=~(MapMoveLandUnit|MapMoveAirUnit|MapMoveSeaUnit);
	xd-=x;
	if( xd<0 ) xd=-1; else if( xd>0 ) xd=1;
	yd-=y;
	if( yd<0 ) yd=-1; else if( yd>0 ) yd=1;
    }
    DebugLevel3(__FUNCTION__": Nothing in sight range\n");

    return 1;
}

/**
**	Complex search new path.
**
**	@param unit	Path for this unit.
**
**	@param xdp	OUT: Pointer for x direction.
**	@param xdp	OUT: Pointer for y direction.
**
**	@return		1=Reachable 0=Reached -1=Unreachable
*/
local int ComplexNewPath(Unit* unit,int *xdp,int* ydp)
{
    static int xoffset[]={  0,-1,+1, 0, -1,+1,-1,+1 };
    static int yoffset[]={ -1, 0, 0,+1, -1,-1,+1,+1 };
    unsigned char* matrix;
    unsigned char points[TheMap.Width*TheMap.Height];
    int path[MAX_PATH*2];
    Unit* goal;
    int bestx;
    int besty;
    int bestd;
    int bestn;
    int x;
    int y;
    int xd;
    int yd;
    int mask;
    int wp;
    int rp;
    int ep;
    int n;
    int i;
    int j;
    int w;
    int r;
    int f;
    int depth;

    DebugLevel3(__FUNCTION__": %s(%Zd) to %Zd=%d,%d~%d\n"
	    ,unit ? unit->Type->Ident : ""
	    ,unit-UnitsPool
	    ,unit->Command.Data.Move.Goal
		? unit->Command.Data.Move.Goal-UnitsPool : 0
	    ,unit->Command.Data.Move.DX
	    ,unit->Command.Data.Move.DY
	    ,unit->Command.Data.Move.Range);

    matrix=CreateMatrix();
    w=TheMap.Width+2;
    mask=UnitMovementMask(unit);

#if 0
// If reachable it is better to start at goal
// We needn't trace back the route
    points[0]=unit->Command.Data.Move.DX;		// start at goal
    points[1]=unit->Command.Data.Move.DY;
#endif

    points[0]=x=unit->X;
    points[1]=y=unit->Y;
    matrix[w+1+x+y*w]=n=1;			// mark start point
    depth=1;
    rp=0;
    ep=2;
    wp=2;					// start with one point

    bestx=x;
    besty=y;
    xd=abs(unit->Command.Data.Move.DX-x);
    yd=abs(unit->Command.Data.Move.DY-y);
    bestd=xd>yd ? xd : yd;
    bestn=1;					// needed if goal not reachable

    //
    //	Mark goal (FIXME: can't enter field?)
    //
    r=unit->Command.Data.Move.Range;
    if( unit->Command.Data.Move.Goal ) {
	goal=unit->Command.Data.Move.Goal;
	x=goal->X;
	y=goal->Y;
	j=goal->Type->Type;
	// FIXME: need only mark rectangle
	// FIXME: don't mark outside the matrix!
	i=1;
	for( yd=UnitTypes[j].TileHeight+r; yd-->-r; ) {
	    for( xd=UnitTypes[j].TileWidth+r; xd-->-r; ) {
		if( CheckedCanMoveToMask(x+xd,y+yd,mask) ) {	// reachable
		    matrix[w+1+x+xd+(y+yd)*w]=88;
		    i=0;
		}
	    }
	    if( i ) {
		DebugLevel3("Goal never reachable (%s,%d,%d+%d)\n"
			,goal->Type->Ident,x,y,r);
		PfCounterNotReachable++;
	    }
	}
    } else {
	x=unit->Command.Data.Move.DX;
	y=unit->Command.Data.Move.DY;
	i=1;
	// FIXME: need only mark rectangle
	// FIXME: don't mark outside the matrix!
	for( yd=r+1; yd-->0; ) {
	    for( xd=r+1; xd-->0; ) {
		DebugLevel3("%d,%d\n",x+xd,y+yd);
		if( CheckedCanMoveToMask(x+xd,y+yd,mask) ) {	// reachable
		    matrix[w+1+x+xd+(y+yd)*w]=88;
		    i=0;
		}
	    }
	}
	if( i ) {
	    DebugLevel3("Goal never reachable (%d,%d+%d)\n",x,y,r);
	    PfCounterNotReachable++;
	}
    }
    f=1;

    for( ;; ) {
	while( rp!=ep ) {
	    for( j=0; j<8; ++j ) {		// mark all neighbors
		x=points[rp]+xoffset[j];
		y=points[rp+1]+yoffset[j];
		if( matrix[w+1+x+y*w] ) {	// already reached
		    //
		    //	Check if goal reached.
		    //
		    if( matrix[w+1+x+y*w]==88 ) {
			DebugLevel3("Goal reached %d ",n);
			PathTraceBack(matrix,x,y,n,path);
			xd=path[2]-unit->X;
			yd=path[3]-unit->Y;
			DebugLevel3("%d,%d: %d,%d\n",unit->X,unit->Y,xd,yd);
			DebugCheck( xd>1 || xd<-1 );
			DebugCheck( yd>1 || yd<-1 );

			*xdp=xd;
			*ydp=yd;
			IfDebug( PfCounterOk++;
			    //PfCounterDepth+=n;
			    //PfCounterDepth/=2;
			);
			return 1;
		    }
		    continue;
		}
#if 0
		if( x<0 || y<0 || x>=128 || y>=128 ) {
		    DebugLevel1("MATRIX-ERROR: %d\n",matrix[w+1+x+y*w]);
		    DebugLevel1("MATRIX-ERROR: %d\n",matrix[w+1+x+(y+1)*w]);
		}
#endif

		//
		//	Look if destination field is blocked.
		//	Fields blocked by moving units, are considered free.
		//

#ifdef MOVE_MAP
		i=TheMap.MovementMap[x+y*TheMap.Width]&mask;
#else
		i=TheMap.Fields[x+y*TheMap.Width].Flags&mask;
#endif
		if( i ) {		// unreachable
		    // Blocked by non-moving in not first round
		    DebugLevel3("%x - %x\n",mask,i);
#ifdef MOVE_MAP
		    if( f || (i&~(MapMoveLandUnit
				|MapMoveAirUnit|MapMoveSeaUnit)) )
#else
		    if( f || (i&~(MapFieldLandUnit
				|MapFieldAirUnit|MapFieldSeaUnit)) )
#endif
		    {
			DebugLevel3("NO: %d,%d\n",x,y);
			matrix[w+1+x+y*w]=99;
			continue;
		    }
		    goal=UnitCacheOnXY(x,y,unit->Type->UnitType);
		    if( !goal ) {
			DebugLevel0(__FUNCTION__
			    ": %p No goal for %d,%d on %d,%d?\n",
				    unit,unit->X,unit->Y,x,y);
			//	Clear movement flag from MovementMap
			//	for this type of unit
			//TheMap.MovementMap[x+y*TheMap.Width]&=
			//	~UnitMovement(unit);
			matrix[w+1+x+y*w]=99;
			continue;
		    }
#if 0
		    // FIXME: Have more move actions
		    if( goal->Command.Action!=UnitActionMove ) {
			matrix[w+1+x+y*w]=99;
			continue;
		    }
#endif
		    if( !goal->Moving ) {
			matrix[w+1+x+y*w]=99;
			continue;
		    }
		}

		DebugLevel3("OK: %d,%d\n",x,y);
		matrix[w+1+x+y*w]=n+1;
		points[wp]=x;
		points[wp+1]=y;
		wp+=2;
		if( wp>=sizeof(points) ) {	// round about
		    wp=0;
		}
		xd=abs(unit->Command.Data.Move.DX-x);
		yd=abs(unit->Command.Data.Move.DY-y);
		DebugLevel3("Best: %d,%d-%d - %d,%d\n"
		    ,bestx,besty,bestd
		    ,xd,yd);
		if( xd>yd && xd<bestd ) {
		    bestd=xd;
		    bestx=x;
		    besty=y;
		    bestn=n;
		}
		if( yd>xd && yd<bestd ) {
		    bestd=yd;
		    bestx=x;
		    besty=y;
		    bestn=n;
		}
	    }

	    rp+=2;
	    if( rp>=sizeof(points) ) {	// round about
		rp=0;
	    }
	}
	DebugLevel3("%d,%d\n",rp,wp);

	//
	//	Continue with next frame.
	//
	if( rp==wp ) {			// unreachable, no points available
	    break;
	}
	ep=wp;
	f=0;
#if 0
	//	FIXME: if we ignore distant units we run left-right
	//	Ignore any units more than 5 fields from start.
	if( n>6 ) {
	    mask&=~(MapMoveLandUnit|MapMoveAirUnit|MapMoveSeaUnit);
	}
#endif
	n=(n+1)&7;
	++depth;
    }

    DebugLevel3("Unreachable Best %d=%d,%d\n",bestn,bestx,besty);

    PathTraceBack(matrix,bestx,besty,bestn,path);
    xd=path[2]-unit->X;
    yd=path[3]-unit->Y;

    DebugCheck( xd>1 || xd<-1 );
    DebugCheck( yd>1 || yd<-1 );

    DebugLevel3("Move: %d,%d\n",*xdp,*ydp);

    *xdp=xd;
    *ydp=yd;

    IfDebug(
	PfCounterFail++;
	PfCounterDepth+=depth;
	PfCounterDepth/=2;
    );
    return -1;
}

/**
**	Find new path.
**
**	@param unit	Path for this unit.
**	@param xdp	Pointer for x direction return.
**	@param xdp	Pointer for y direction return.
**	@return		1 for reached -1 unreachable, 0 on the way.
*/
global int NewPath(Unit* unit,int* xdp,int* ydp)
{
    int x;
    int y;
    Unit* goal;
    int r;
    UnitType* type;

    //
    //	Check if goal reached.
    //
    x=unit->X;
    y=unit->Y;
    r=unit->Command.Data.Move.Range;
    goal=unit->Command.Data.Move.Goal;
    DebugLevel3(__FUNCTION__": Goal %p\n",unit->Command.Data.Move.Goal);

    if( goal ) {			// goal unit
	type=goal->Type;
	DebugLevel3(__FUNCTION__": Unit %d,%d Goal %d,%d - %d,%d\n"
	    ,x,y
	    ,goal->X-r,goal->Y-r
	    ,goal->X+type->TileWidth+r
	    ,goal->Y+type->TileHeight+r);
	if( x>=goal->X-r && x<goal->X+type->TileWidth+r
		&& y>=goal->Y-r && y<goal->Y+type->TileHeight+r ) {
	    DebugLevel3(__FUNCTION__": Goal reached\n");
	    *xdp=*ydp=0;
	    return 1;
	}
    } else {				// goal map field
	if( x>=unit->Command.Data.Move.DX
		&& x<=unit->Command.Data.Move.DX+r
		&& y>=unit->Command.Data.Move.DY
		&& y<=unit->Command.Data.Move.DY+r ) {
	    DebugLevel3(__FUNCTION__": Field reached\n");
	    *xdp=*ydp=0;
	    return 1;
	}
	// This reduces the processor use,
	// If target isn't reachable and were beside it
	if( r==0 && x>=unit->Command.Data.Move.DX-1
		&& x<=unit->Command.Data.Move.DX+1
		&& y>=unit->Command.Data.Move.DY-1
		&& y<=unit->Command.Data.Move.DY+1 ) {
	    if( !CheckedCanMoveToMask(unit->Command.Data.Move.DX
		    ,unit->Command.Data.Move.DY
		    ,UnitMovementMask(unit)) ) {	// blocked
		DebugLevel3(__FUNCTION__": Field unreached\n");
		*xdp=*ydp=0;
		return -1;
	    }
	}
    }

    //
    //	If possible, try fast.
    //
    if( unit->Command.Data.Move.Fast ) {
	if( FastNewPath(unit,xdp,ydp) ) {
	    DebugCheck( *xdp==0 && *ydp==0 );
	    return 0;
	}
	unit->Command.Data.Move.Fast=0;
	DebugLevel3(__FUNCTION__": Fallback to slow method\n");
    }

    //
    //	Fall back to slow complex method.
    //
    r=ComplexNewPath(unit,xdp,ydp);
    // FIXME: must be done by complex!
    if( *xdp==0 && *ydp==0 ) {
	return -1;
    }
    return 0;
}

//@}