public void RecalculateConnectionsRec (QuadtreeNodeHolder holder, int depth, int x, int y) { if (holder.node != null) { RecalculateConnections (holder, depth, x,y); } else { int width = 1 << (System.Math.Min (editorHeightLog2,editorWidthLog2)-depth); RecalculateConnectionsRec (holder.c0,depth+1,x , y ); RecalculateConnectionsRec (holder.c1,depth+1,x + width/2, y ); RecalculateConnectionsRec (holder.c2,depth+1,x + width/2, y + width/2); RecalculateConnectionsRec (holder.c3,depth+1,x , y + width/2); } }
public override void ScanInternal (OnScanStatus statusCallback) { Width = 1 << editorWidthLog2; Height = 1 << editorHeightLog2; /** \todo Check if can clear */ map = new BitArray(Width*Height); for (int x=0;x<Width;x++) { for (int y=0;y<Height;y++) { map.Set(x+y*Width, CheckCollision (x,y)); } } QuadtreeNodeHolder h = new QuadtreeNodeHolder(); CreateNodeRec (h, 0, 0,0); root = h; RecalculateConnectionsRec (root, 0,0,0); }
public void CreateNodeRec (QuadtreeNodeHolder holder, int depth, int x, int y) { int width = 1 << (System.Math.Min (editorHeightLog2,editorWidthLog2)-depth); int walkable; if (depth < minDepth) { walkable = -1; } else { walkable = CheckNode (x,y, width); } if (walkable == 1 || walkable == 0 || width == 1) { QuadtreeNode node = new QuadtreeNode(active); node.SetPosition ((Int3)LocalToWorldPosition(x,y,width)); node.Walkable = walkable == 1; holder.node = node; } else { //walkable = -1 //Undefined holder.c0 = new QuadtreeNodeHolder (); holder.c1 = new QuadtreeNodeHolder (); holder.c2 = new QuadtreeNodeHolder (); holder.c3 = new QuadtreeNodeHolder (); CreateNodeRec (holder.c0,depth+1,x , y ); CreateNodeRec (holder.c1,depth+1,x + width/2, y ); CreateNodeRec (holder.c2,depth+1,x + width/2, y + width/2); CreateNodeRec (holder.c3,depth+1,x , y + width/2); } }
public void DrawRec (QuadtreeNodeHolder h, int depth, int x, int y, Vector3 parentPos) { int width = 1 << (System.Math.Min (editorHeightLog2,editorWidthLog2)-depth); Vector3 pos = LocalToWorldPosition (x,y,width); Debug.DrawLine (pos, parentPos, Color.red); if (h.node != null) { Debug.DrawRay (pos, Vector3.down, h.node.Walkable ? Color.green : Color.yellow); } else { DrawRec (h.c0, depth+1,x , y , pos); DrawRec (h.c1, depth+1,x+width/2, y , pos); DrawRec (h.c2, depth+1,x+width/2, y + width/2, pos); DrawRec (h.c3, depth+1,x , y + width/2, pos); } }
public void AddNeighboursRec (List<QuadtreeNode> arr, QuadtreeNodeHolder holder, int depth, int x, int y, IntRect bounds, QuadtreeNode dontInclude) { int width = 1 << (System.Math.Min (editorHeightLog2,editorWidthLog2)-depth); IntRect r = new IntRect(x,y,x+width,y+width); if (!IntRect.Intersects (r,bounds)) return; if (holder.node != null) { if (holder.node != dontInclude) { arr.Add (holder.node); } } else { AddNeighboursRec (arr, holder.c0, depth+1,x , y , bounds, dontInclude); AddNeighboursRec (arr, holder.c1, depth+1,x+width/2, y , bounds, dontInclude); AddNeighboursRec (arr, holder.c2, depth+1,x+width/2, y + width/2, bounds, dontInclude); AddNeighboursRec (arr, holder.c3, depth+1,x , y + width/2, bounds, dontInclude); } }
public void RecalculateConnections (QuadtreeNodeHolder holder, int depth, int x, int y) { if (root == null) throw new System.InvalidOperationException ("Graph contains no nodes"); if (holder.node == null) throw new System.ArgumentException ("No leaf node specified. Holder has no node."); int width = 1 << (System.Math.Min (editorHeightLog2,editorWidthLog2)-depth); List<QuadtreeNode> ls = new List<QuadtreeNode>(); AddNeighboursRec (ls, root, 0,0,0, new IntRect (x,y,x+width,y+width).Expand(0), holder.node); holder.node.connections = ls.ToArray(); holder.node.connectionCosts = new uint[ls.Count]; for (int i=0;i<ls.Count;i++) { uint d = (uint)(ls[i].position - holder.node.position).costMagnitude; holder.node.connectionCosts[i] = d; } }