public bool search(ASNode *firstNode, ASNode *endNode) { //return searchRecursive(firstNode, 0); PtrQueue nodeStack = p_Queue; nodeStack.Clear(); nodeStack.Push(firstNode); int count = 1; while (count != 0) { ASNode *current = (ASNode *)nodeStack.Pop(); (*current).State = ASNodeState.CLOSED; count--; //sort by total cost /* * adjacent.Sort(delegate(ASNode n1, ASNode n2) { * float n1F = n1.G + n1.H; * float n2F = n2.G + n2.H; * return n1F.CompareTo(n2F); * }); */ //get the adjacent nodes getAdjacent(current); ASNode **adjacent = p_AdjacentNodes; ASNode **adjacentEnd = adjacent + 8; while (adjacent != adjacentEnd) { if ((*adjacent) == (ASNode *)0) { break; } //we hit the end? if (*adjacent == endNode) { return(true); } //get the pointer for this node nodeStack.Push(*adjacent); adjacent++; count++; } } /*no path was found*/ return(false); }
private static islandInfo floodSearch(searchBlock *block, int islandIndex, int rowWidth, searchBlock *start, searchBlock *end) { //create a queue to store pending blocks to //be processed. PtrQueue queue = new PtrQueue(10000); queue.Push(block); int count = 1; //count how many blocks we found int found = 0; //used to detect the size and location //of the island. int minX = 0, minY = 0; int maxX = 0, maxY = 0; minX = minY = int.MaxValue; //process every block List <IntPtr> blocks = new List <IntPtr>(); while (count != 0) { searchBlock *ptr = (searchBlock *)queue.Pop(); count--; //verify not out of bounds if (ptr < start || ptr >= end) { continue; } searchBlock deref = *ptr; //searched already? if (deref.searched) { continue; } //mark as processed (*ptr).searched = true; //we hit the water? Block b = (*deref.block); if (b.TypeID == Globals.TERRAIN_WATER) { continue; } found++; //add the block to the island blocks.Add((IntPtr)deref.block); //calculate x/y and do min/max to //detect island size/location int x = (int)((long)(ptr - start) % rowWidth); int y = (int)((long)(ptr - start) / rowWidth); if (x < minX) { minX = x; } if (y < minY) { minY = y; } if (x > maxX) { maxX = x; } if (y > maxY) { maxY = y; } #region reiterate for all 8 neighbors /*top row*/ queue.Push(ptr - rowWidth); queue.Push(ptr - rowWidth - 1); queue.Push(ptr - rowWidth + 1); /*bottom row*/ queue.Push(ptr + rowWidth); queue.Push(ptr + rowWidth - 1); queue.Push(ptr + rowWidth + 1); /*left/right*/ queue.Push(ptr - 1); queue.Push(ptr + 1); count += 8; #endregion } return(new islandInfo { count = found, minX = minX, minY = minY, maxX = maxX, maxY = maxY, blocks = blocks }); }