@@ -13,6 +13,7 @@ export class GameMap {
1313 readonly areaSizes : number [ ] ;
1414 readonly tileInfluence : Uint32Array ;
1515 readonly distanceMap : Int16Array ;
16+ readonly boatTargets : { tile : number , distance : number , midPoint : number } [ ] [ ] = [ ] ;
1617
1718 constructor ( name : string , width : number , height : number , tileTypes : TileTypeBase [ ] ) {
1819 this . name = name ;
@@ -112,6 +113,7 @@ export class GameMap {
112113 const queue = new PriorityQueue < [ number , number , number [ ] ] > ( ( a , b ) => Math . abs ( a [ 0 ] ) < Math . abs ( b [ 0 ] ) ) ;
113114 const waterCache = new Map < number , number [ ] > ( ) ;
114115 const landCache = new Map < number , number [ ] > ( ) ;
116+ const markerCache = new Map < number , boolean [ ] > ( ) ;
115117
116118 for ( let i = 0 ; i < this . areaMap . length ; i ++ ) {
117119 if ( this . getTile ( i ) . navigable ) {
@@ -156,13 +158,27 @@ export class GameMap {
156158 while ( ! queue . isEmpty ( ) ) {
157159 const [ distance , bias , array ] = queue . pop ( ) ;
158160 const newArray : number [ ] = [ ] ;
161+ const markerAlias = new Map < number , Map < number , [ number , number ] > > ( ) ;
159162 for ( let i = 0 ; i < array . length ; i += 2 ) {
160163 this . onNeighbors ( array [ i ] , neighbor => {
161164 if ( this . distanceMap [ neighbor ] === - ( 2 ** 15 ) ) {
162165 this . distanceMap [ neighbor ] = distance ;
163166 this . tileInfluence [ neighbor ] = array [ i + 1 ] ;
164167 newArray . push ( neighbor ) ;
165168 newArray . push ( array [ i + 1 ] ) ;
169+ } else if ( distance < 0 && array [ i + 1 ] < this . tileInfluence [ neighbor ] && ! markerCache . get ( array [ i + 1 ] ) ?. [ this . tileInfluence [ neighbor ] ] ) {
170+ const a1 = neighbor % this . width - array [ i + 1 ] % this . width ;
171+ const a2 = Math . floor ( neighbor / this . width ) - Math . floor ( array [ i + 1 ] / this . width ) ;
172+ const b1 = neighbor % this . width - this . tileInfluence [ neighbor ] % this . width ;
173+ const b2 = Math . floor ( neighbor / this . width ) - Math . floor ( this . tileInfluence [ neighbor ] / this . width ) ;
174+ const angle = Math . acos ( ( a1 * b1 + a2 * b2 ) / Math . sqrt ( ( a1 ** 2 + a2 ** 2 ) * ( b1 ** 2 + b2 ** 2 ) ) ) ;
175+ //TODO: clean up; remove senseless markers on borders
176+ // @ts -expect-error - TS doesn't know that the key exists
177+ if ( angle >= Math . PI / 2 && ( ! markerAlias . get ( array [ i + 1 ] ) ?. has ( this . tileInfluence [ neighbor ] ) || angle > markerAlias . get ( array [ i + 1 ] ) ?. get ( this . tileInfluence [ neighbor ] ) ?. [ 0 ] ) ) {
178+ if ( ! markerAlias . has ( array [ i + 1 ] ) ) markerAlias . set ( array [ i + 1 ] , new Map ( ) ) ;
179+ // @ts -expect-error - TS doesn't know that the key exists
180+ markerAlias . get ( array [ i + 1 ] ) . set ( this . tileInfluence [ neighbor ] , [ angle , neighbor ] ) ;
181+ }
166182 }
167183 } ) ;
168184 }
0 commit comments