public structures.pntWithDist[] findNearestCaseTo( int xo, int yo, int xDest, int yDest, byte terrain, byte player, bool tet, bool attacking ) { Point dest = new Point( xDest, yDest ); Point[] sqr = returnEmptySquare( xo, yo, 1 ); bool[] valid = new bool[ sqr.Length ]; int[] dist = new int[ sqr.Length ]; byte[] order = new byte[ sqr.Length ]; for ( int j = 0; j < sqr.Length; j ++ ) { if ( sqr[ j ].X == xDest && sqr[ j ].Y == yDest ) { structures.pntWithDist[] pwd = new xycv_ppc.structures.pntWithDist[ 1 ]; pwd[ 0 ].X = sqr[ j ].X; pwd[ 0 ].Y = sqr[ j ].Y; pwd[ 0 ].dist = 0; return pwd; } if ( ( Statistics.terrains[ game.grid[ sqr[ j ].X, sqr[ j ].Y ].type ].ew == terrain || ( game.grid[ sqr[ j ].X, sqr[ j ].Y ].city > 0 && ( game.grid[ sqr[ j ].X, sqr[ j ].Y ].territory - 1 == player || game.playerList[ player ].foreignRelation[ game.grid[ sqr[ j ].X, sqr[ j ].Y ].territory - 1 ].politic == (byte)Form1.relationPolType.alliance || game.playerList[ player ].foreignRelation[ game.grid[ sqr[ j ].X, sqr[ j ].Y ].territory - 1 ].politic == (byte)Form1.relationPolType.Protected || game.playerList[ player ].foreignRelation[ game.grid[ sqr[ j ].X, sqr[ j ].Y ].territory - 1 ].politic == (byte)Form1.relationPolType.Protector ) ) || game.grid[ sqr[ j ].X, sqr[ j ].Y ].river ) && ( game.grid[ sqr[ j ].X, sqr[ j ].Y ].territory - 1 == player || game.grid[ sqr[ j ].X, sqr[ j ].Y ].territory == 0 || game.playerList[ player ].foreignRelation[ game.grid[ sqr[ j ].X, sqr[ j ].Y ].territory - 1 ].politic == (byte)Form1.relationPolType.alliance || game.playerList[ player ].foreignRelation[ game.grid[ sqr[ j ].X, sqr[ j ].Y ].territory - 1 ].politic == (byte)Form1.relationPolType.Protected || game.playerList[ player ].foreignRelation[ game.grid[ sqr[ j ].X, sqr[ j ].Y ].territory - 1 ].politic == (byte)Form1.relationPolType.Protector || ( game.playerList[ player ].foreignRelation[ game.grid[ sqr[ j ].X, sqr[ j ].Y ].territory - 1 ].politic == (byte)Form1.relationPolType.war && tet ) || ( game.grid[ xo, yo ].territory - 1 != player && ( game.grid[ sqr[ j ].X, sqr[ j ].Y ].city == 0 || attacking ) ) ) ) { // valid case if ( !game.radius.caseOccupiedByRelationType( sqr[ j ].X, sqr[ j ].Y, player, true, true, true, false, false, false ) || ( attacking && !game.radius.caseOccupiedByRelationType( sqr[ j ].X, sqr[ j ].Y, player, false, true, true, false, false, false ) ) ) { valid[ j ] = true; dist[ j ] = getDistWith( sqr[ j ], dest ); } } else { // invalid case } } byte validCases = 0; for ( int j = 0; j < sqr.Length; j ++ ) if ( valid[ j ] ) validCases ++; structures.pntWithDist[] retour = new structures.pntWithDist[ validCases ]; for ( int j = 0, k = 0; j < sqr.Length; j ++ ) if ( valid[ j ] ) { retour[ k ].X = sqr[ j ].X; retour[ k ].Y = sqr[ j ].Y; retour[ k ].dist = dist[ j ]; k ++; } bool mod = true; structures.pntWithDist buffer; while ( mod ) { mod = false; for ( int j = 1; j < retour.Length; j ++ ) if ( retour[ j ].dist < retour[ j - 1 ].dist ) { buffer = retour[ j ]; retour[ j ] = retour[ j - 1 ]; retour[ j - 1 ] = buffer; mod = true; } } return retour; }
/* public System.Drawing.Point[] findWayTo1( byte player, Point ori, Point dest, byte terrain, bool attack ) { if ( ori.X == -1 || ori.Y == -1 || dest.X == -1 || dest.Y == -1 ) return new Point[ 1 ] { new Point( -1, -1 ) }; Point[] pnt1 = new Point[ game.width * game.height ]; int pos = 0; int indOri = 0, indDest = 0; byte[] pols = returnRelationTypeList( !attack, true, true, false, false, false ); if ( terrain == 1 ) { for ( int x = 0; x < game.width; x ++ ) for ( int y = 0; y < game.height; y ++ ) { if ( ori.X == x && ori.Y == y ) { pnt1[ pos ] = new Point( x, y ); indOri = pos; pos ++; } else if ( dest.X == x && dest.Y == y ) { pnt1[ pos ] = new Point( x, y ); indDest = pos; pos ++; } else if ( game.grid[ x, y ].continent == game.grid[ xo, yo ].continent && ( game.grid[ x, y ].territory - 1 == player || game.grid[ x, y ].territory == 0 || game.playerList[ player ].foreignRelation[ game.grid[ x, y ].territory - 1 ].politic == (byte)Form1.relationPolType.alliance || game.playerList[ player ].foreignRelation[ game.grid[ x, y ].territory - 1 ].politic == (byte)Form1.relationPolType.Protected || game.playerList[ player ].foreignRelation[ game.grid[ x, y ].territory - 1 ].politic == (byte)Form1.relationPolType.Protector ) && !caseOccupiedByRelationType( x, y, player, pols ) ) { pnt1[ pos ] = new Point( x, y ); pos ++; } } } else { for ( int x = 0; x < game.width; x ++ ) for ( int y = 0; y < game.height; y ++ ) if ( ori.X == x && ori.Y == y ) { pnt1[ pos ] = new Point( x, y ); indOri = pos; pos ++; } else if ( dest.X == x && dest.Y == y ) { pnt1[ pos ] = new Point( x, y ); indDest = pos; pos ++; } else if ( game.grid[ x, y ].continent == 0 || ( game.grid[ x, y ].city > 0 && ( game.grid[ x, y ].territory - 1 == player || game.playerList[ player ].foreignRelation[ game.grid[ x, y ].territory - 1 ].politic == (byte)Form1.relationPolType.alliance || game.playerList[ player ].foreignRelation[ game.grid[ x, y ].territory - 1 ].politic == (byte)Form1.relationPolType.Protected || game.playerList[ player ].foreignRelation[ game.grid[ x, y ].territory - 1 ].politic == (byte)Form1.relationPolType.Protector ) ) && !caseOccupiedByRelationType( x, y, player, pols ) ) { pnt1[ pos ] = new Point( x, y ); pos ++; } } Point[] pnt = new Point[ pos ]; int[] order = new int[ pos ]; int[] cost = new int[ pos ]; int[] totalCost = new int[ pos ]; bool[,] inRange = new bool[ pos, pos ]; for ( int i = 0; i < pos; i ++ ) { order[ i ] = 10000; for ( int j = i + 1; j < pos; j ++ ) if ( isNextTo( pnt[ i ], pnt[ j ] ) ) { inRange[ i, j ] = true; inRange[ j, i ] = true; } } order[ indOri ] = 0; cost[ indOri ] = 0; Point[] way; bool allOverBestFound = false; int bestCost = 30000; bool mod = true; for ( int rad = 1; mod; rad ++ ) { mod = false; allOverBestFound = true; for ( int i = 0; i < pos; i ++ ) if ( order[ i ] == rad - 1 && i != indDest ) for ( int j = 0; j < pos; j ++ ) if ( inRange[ i, j ] && order[ j ] == 1000 ) { order[ j ] = rad; prevPnt[ j ] = i; mod = true; cost[ j ] = cost[ i ] + 1; if ( j == indDest && cost[ j ] < bestCost ) { // yeah way = new Point[ rad ]; int[] wayInt = new int[ rad ]; way[ way.Length - 1 ] = dest; wayInt[ way.Length - 1 ] = j; bestCost = cost[ j ]; for ( int c = way.Length - 1; c > 0; c -- ) { wayInt[ c - 1 ] = prevPnt[ wayInt[ c ] - 1 ]; way[ c - 1 ] = new Point( game.playerList[ player ].cityList[ prevPnt[ wayInt[ c ] - 1 ] ].X, game.playerList[ player ].cityList[ prevPnt[ wayInt[ c ] - 1 ] ].Y ); } } if ( cost[ j ] < bestCost ) allOverBestFound = false; } if ( !allOverBestFound ) return bestWay; } return new Point[] { new Point( -1, -1 ) }; }*/ #endregion #region findWayTo 2 public System.Drawing.Point[] findWayTo2( int xo, int yo, int xDest, int yDest, byte terrain, byte player, bool attack/*, bool tet*/ ) { if ( xo == -1 || yo == -1 || xDest == -1 || yDest == -1 ) return new Point[ 1 ] { new Point( -1, -1 ) }; /* if ( game.grid[ xo, yo ].continent != game.grid[ xDest, yDest ].continent ) return new Point[ 1 ] { new Point( -1, -1 ) }; */ if ( game.grid[ xo, yo ].continent != game.grid[ xDest, yDest ].continent && game.grid[ xo, yo ].continent != 0 && game.grid[ xDest, yDest ].continent != 0 ) return new Point[ 1 ] { new Point( -1, -1 ) }; if ( xo == xDest && yo == yDest ) return new Point[ 1 ] { new Point( -2, -2 ) }; Point[] wayPnts = new Point[ 100 ]; // Radius radius = new Radius(); int complexity = 0; int[,] order = new int[ game.width, game.height ]; structures.pntWithDist[][][] pwd = new structures.pntWithDist[ game.width ][][]; for ( int i = 0; i < game.width; i ++ ) pwd[ i ] = new structures.pntWithDist[ game.height ][]; wayPnts[ 0 ] = new Point( xo, yo ); pwd[ xo ][ yo ] = findNearestCaseTo( xo, yo, xDest, yDest, terrain, player, attack, //tet attack ); if ( pwd[ xo ][ yo ].Length > 0 ) { if ( pwd[ xo ][ yo ][ 0 ].dist == 0 ) { Point[] wayPnts1 = new Point[ 1 ]; wayPnts1[ 0 ] = new Point( xDest, yDest ); return wayPnts1; } } else return new Point[ 1 ] { new Point( -1, -1 ) }; for ( int i = 1; i < 100; i++ ) { if ( pwd[ wayPnts[ i - 1 ].X ][ wayPnts[ i - 1 ].Y ] == null ) { pwd[ wayPnts[ i - 1 ].X ][ wayPnts[ i - 1 ].Y ] = findNearestCaseTo( wayPnts[ i - 1 ].X, wayPnts[ i - 1 ].Y, xDest, yDest, terrain, player, true, //tet attack ); for ( int k = 0; k < pwd[ wayPnts[ i - 1 ].X ][ wayPnts[ i - 1 ].Y ].Length; k ++ ) if ( pwd[ wayPnts[ i - 1 ].X ][ wayPnts[ i - 1 ].Y ][ k ].dist == 0 ) { Point[] wayPnts1 = new Point[ i ]; for ( int j = 0; j < wayPnts1.Length - 1 ; j ++ ) wayPnts1[ j ] = wayPnts[ j + 1 ]; wayPnts1[ wayPnts1.Length - 1 ] = new Point( xDest, yDest ); return wayPnts1; } } if ( pwd[ wayPnts[ i - 1 ].X ][ wayPnts[ i - 1 ].Y ].Length <= order[ wayPnts[ i - 1 ].X, wayPnts[ i - 1 ].Y ] ) return new Point[] { new Point( -1, -1 ) }; wayPnts[ i ].X = pwd[ wayPnts[ i - 1 ].X ][ wayPnts[ i - 1 ].Y ][ order[ wayPnts[ i - 1 ].X, wayPnts[ i - 1 ].Y ] ].X; wayPnts[ i ].Y = pwd[ wayPnts[ i - 1 ].X ][ wayPnts[ i - 1 ].Y ][ order[ wayPnts[ i - 1 ].X, wayPnts[ i - 1 ].Y ] ].Y; for ( int k = 0; k < i; k ++ ) if ( wayPnts[ i ] == wayPnts[ k ] ) { if ( i - 2 != k ) { order[ wayPnts[ i - 1 ].X, wayPnts[ i - 1 ].Y ] ++; i = k; } bool found = false; while ( !found ) { i --; if ( i < 0 ) return new Point[] { new Point( -1, -1 ) }; order[ wayPnts[ i ].X, wayPnts[ i ].Y ] ++; // i + 1 - 1 if ( order[ wayPnts[ i ].X, wayPnts[ i ].Y ] < pwd[ wayPnts[ i ].X ][ wayPnts[ i ].Y ].Length ) found = true; // ne dépasse pas la limite } break; } complexity ++; if ( complexity > 2000 ) return new Point[] { new Point( -1, -1 ) }; } return new Point[] { new Point( -1, -1 ) }; }