public PathfinderNode GetNearestNode(PathfinderNetwork Network, XYInt Pos, int MinClearance) { var A = 0; double Dist = 0; var tmpNode = default(PathfinderNode); var BestNode = default(PathfinderNode); double BestDist = 0; var tmpNodeTag = default(clsNodeTag); BestDist = double.MaxValue; BestNode = null; for ( A = 0; A <= Network.get_GetNodeLayer(0).GetNodeCount - 1; A++ ) { tmpNode = Network.get_GetNodeLayer(0).get_GetNode(A); if ( tmpNode.GetClearance >= MinClearance ) { tmpNodeTag = (clsNodeTag)tmpNode.Tag; Dist = (tmpNodeTag.Pos - Pos).ToDoubles().GetMagnitude(); if ( Dist < BestDist ) { BestDist = Dist; BestNode = tmpNode; } } } return BestNode; }
private PathfinderNode GetNearestNodeConnection(PathfinderNetwork Network, XYInt Pos, int MinClearance, float MaxDistance) { var A = 0; var TravelNodes = new PathfinderNode[Network.get_GetNodeLayer(0).GetNodeCount * 10]; var TravelNodeCount = 0; var NodeTravelDists = new float[Network.get_GetNodeLayer(0).GetNodeCount]; var TravelNodeNum = 0; var CurrentNode = default(PathfinderNode); var OtherNode = default(PathfinderNode); var tmpConnection = default(PathfinderConnection); PathfinderNode BestNode = null; float TravelDist = 0; var Flag = default(bool); for ( A = 0; A <= Network.get_GetNodeLayer(0).GetNodeCount - 1; A++ ) { NodeTravelDists[A] = float.MaxValue; } TravelNodes[0] = GetNearestNode(Network, Pos, 1); if ( TravelNodes[0] == null ) { return null; } TravelNodeCount = 1; NodeTravelDists[TravelNodes[0].Layer_NodeNum] = 0.0F; while ( TravelNodeNum < TravelNodeCount ) { CurrentNode = TravelNodes[TravelNodeNum]; if ( CurrentNode.Clearance >= MinClearance ) { if ( BestNode == null ) { BestNode = CurrentNode; } else if ( NodeTravelDists[CurrentNode.Layer_NodeNum] < NodeTravelDists[BestNode.Layer_NodeNum] ) { BestNode = CurrentNode; } } for ( A = 0; A <= CurrentNode.GetConnectionCount - 1; A++ ) { tmpConnection = CurrentNode.get_GetConnection(A); OtherNode = tmpConnection.GetOtherNode(CurrentNode); TravelDist = NodeTravelDists[CurrentNode.Layer_NodeNum] + tmpConnection.GetValue; if ( BestNode == null ) { Flag = true; } else if ( TravelDist < NodeTravelDists[BestNode.Layer_NodeNum] ) { Flag = true; } else { Flag = false; } if ( Flag && TravelDist < NodeTravelDists[OtherNode.Layer_NodeNum] ) { NodeTravelDists[OtherNode.Layer_NodeNum] = TravelDist; TravelNodes[TravelNodeCount] = OtherNode; TravelNodeCount++; } } TravelNodeNum++; } return BestNode; }
public clsResult GenerateLayoutTerrain() { var ReturnResult = new clsResult("Terrain heights", false); logger.Info("Generating Terrain heights"); var NodeTag = default(clsNodeTag); var tmpNodeA = default(PathfinderNode); var tmpNodeB = default(PathfinderNode); var A = 0; var B = 0; var C = 0; var D = 0; var X = 0; var Y = 0; var XY_int = new XYInt(); double Dist = 0; double BestDist = 0; var Flag = default(bool); Map = new clsMap(TileSize); GenerateTerrainTiles = new GenerateTerrainTile[Map.Terrain.TileSize.X, Map.Terrain.TileSize.Y]; GenerateTerrainVertices = new GenerateTerrainVertex[Map.Terrain.TileSize.X + 1, Map.Terrain.TileSize.Y + 1]; //set terrain heights VertexPathMap = new PathfinderNetwork(); for ( Y = 0; Y <= Map.Terrain.TileSize.Y; Y++ ) { for ( X = 0; X <= Map.Terrain.TileSize.X; X++ ) { GenerateTerrainVertices[X, Y] = new GenerateTerrainVertex(); GenerateTerrainVertices[X, Y].Node = new PathfinderNode(VertexPathMap); NodeTag = new clsNodeTag(); NodeTag.Pos = new XYInt(X * 128, Y * 128); GenerateTerrainVertices[X, Y].Node.Tag = NodeTag; } } for ( Y = 0; Y <= Map.Terrain.TileSize.Y; Y++ ) { for ( X = 0; X <= Map.Terrain.TileSize.X; X++ ) { tmpNodeA = GenerateTerrainVertices[X, Y].Node; if ( X > 0 ) { tmpNodeB = GenerateTerrainVertices[X - 1, Y].Node; GenerateTerrainVertices[X, Y].LeftLink = tmpNodeA.GetOrCreateConnection(tmpNodeB, GetNodePosDist(tmpNodeA, tmpNodeB)); } if ( Y > 0 ) { if ( X > 0 ) { tmpNodeB = GenerateTerrainVertices[X - 1, Y - 1].Node; GenerateTerrainVertices[X, Y].TopLeftLink = tmpNodeA.GetOrCreateConnection(tmpNodeB, GetNodePosDist(tmpNodeA, tmpNodeB)); } tmpNodeB = GenerateTerrainVertices[X, Y - 1].Node; GenerateTerrainVertices[X, Y].TopLink = tmpNodeA.GetOrCreateConnection(tmpNodeB, GetNodePosDist(tmpNodeA, tmpNodeB)); if ( X < Map.Terrain.TileSize.X ) { tmpNodeB = GenerateTerrainVertices[X + 1, Y - 1].Node; GenerateTerrainVertices[X, Y].TopRightLink = tmpNodeA.GetOrCreateConnection(tmpNodeB, GetNodePosDist(tmpNodeA, tmpNodeB)); } } if ( X < Map.Terrain.TileSize.X ) { tmpNodeB = GenerateTerrainVertices[X + 1, Y].Node; GenerateTerrainVertices[X, Y].RightLink = tmpNodeA.GetOrCreateConnection(tmpNodeB, GetNodePosDist(tmpNodeA, tmpNodeB)); } if ( Y < Map.Terrain.TileSize.Y ) { if ( X > 0 ) { tmpNodeB = GenerateTerrainVertices[X - 1, Y + 1].Node; GenerateTerrainVertices[X, Y].BottomLeftLink = tmpNodeA.GetOrCreateConnection(tmpNodeB, GetNodePosDist(tmpNodeA, tmpNodeB)); } tmpNodeB = GenerateTerrainVertices[X, Y + 1].Node; GenerateTerrainVertices[X, Y].BottomLink = tmpNodeA.GetOrCreateConnection(tmpNodeB, GetNodePosDist(tmpNodeA, tmpNodeB)); if ( X < Map.Terrain.TileSize.X ) { tmpNodeB = GenerateTerrainVertices[X + 1, Y + 1].Node; GenerateTerrainVertices[X, Y].BottomRightLink = tmpNodeA.GetOrCreateConnection(tmpNodeB, GetNodePosDist(tmpNodeA, tmpNodeB)); } } } } VertexPathMap.LargeArraysResize(); VertexPathMap.FindCalc(); var BaseLayer = VertexPathMap.get_GetNodeLayer(0); var JitterLayer = VertexPathMap.get_GetNodeLayer(JitterScale); A = JitterLayer.GetNodeCount - 1; var NodeLevel = new int[A + 1]; var BaseNodeLevel = new clsBaseNodeLevels(); BaseNodeLevel.NodeLevels = new float[BaseLayer.GetNodeCount]; //set position of jitter layer nodes var XY_dbl = default(XYDouble); if ( A > 0 ) { for ( B = 0; B <= A; B++ ) { tmpNodeA = JitterLayer.get_GetNode(B); C = 0; XY_dbl.X = 0.0D; XY_dbl.Y = 0.0D; CalcNodePos(tmpNodeA, ref XY_dbl, ref C); NodeTag = new clsNodeTag(); NodeTag.Pos.X = (int)(XY_dbl.X / C); NodeTag.Pos.Y = (int)(XY_dbl.Y / C); tmpNodeA.Tag = NodeTag; } } //set node heights var BestConnection = default(clsConnection); var BestNode = default(clsPassageNode); for ( A = 0; A <= JitterLayer.GetNodeCount - 1; A++ ) { NodeTag = (clsNodeTag)(JitterLayer.get_GetNode(A).Tag); NodeLevel[A] = -1; BestDist = float.MaxValue; BestConnection = null; BestNode = null; for ( B = 0; B <= ConnectionCount - 1; B++ ) { //If Not (Connections(B).PassageNodeA.IsOnBorder Or Connections(B).PassageNodeB.IsOnBorder) Then if ( Connections[B].PassageNodeA.Level == Connections[B].PassageNodeB.Level ) { //only do this if the levels are the same //this is to make sure nodes that are connected are actually connected on the terrain XY_int = MathUtil.PointGetClosestPosOnLine(Connections[B].PassageNodeA.Pos, Connections[B].PassageNodeB.Pos, NodeTag.Pos); Dist = Convert.ToSingle((XY_int - NodeTag.Pos).ToDoubles().GetMagnitude()); if ( Dist < BestDist ) { BestDist = Dist; if ( (NodeTag.Pos - Connections[B].PassageNodeA.Pos).ToDoubles().GetMagnitude() <= (NodeTag.Pos - Connections[B].PassageNodeB.Pos).ToDoubles().GetMagnitude() ) { BestNode = Connections[B].PassageNodeA; } else { BestNode = Connections[B].PassageNodeB; } Flag = true; } } } for ( C = 0; C <= PassageNodeCount - 1; C++ ) { //If Not PassageNodesA(C).IsOnBorder Then for ( D = 0; D <= SymmetryBlockCount - 1; D++ ) { Dist = Convert.ToSingle((NodeTag.Pos - PassageNodes[D, C].Pos).ToDoubles().GetMagnitude()); if ( Dist < BestDist ) { BestDist = Dist; BestNode = PassageNodes[D, C]; Flag = true; } } //End If } if ( Flag ) { NodeLevel[A] = BestNode.Level; } else { NodeLevel[A] = BestConnection.PassageNodeA.Level; } if ( NodeLevel[A] < 0 ) { ReturnResult.ProblemAdd("Error: Node height is not set."); return ReturnResult; } } for ( A = 0; A <= LevelCount - 1; A++ ) { for ( B = 0; B <= JitterLayer.GetNodeCount - 1; B++ ) { if ( NodeLevel[B] >= A ) { SetBaseLevel(JitterLayer.get_GetNode(B), A, BaseNodeLevel); } } } //make ramp slopes var MinRampLength = ((int)(LevelHeight * Map.HeightMultiplier * 2.0D)) + 128; var RampArgs = new clsSetBaseLevelRampArgs(); RampArgs.BaseLevel = BaseNodeLevel; RampArgs.RampRadius = 320.0F; for ( B = 0; B <= ConnectionCount - 1; B++ ) { RampArgs.Connection = Connections[B]; RampArgs.RampLength = Math.Max(Convert.ToInt32((Connections[B].PassageNodeA.Pos - Connections[B].PassageNodeB.Pos).ToDoubles().GetMagnitude() * 0.75D), MinRampLength * Math.Abs(Connections[B].PassageNodeA.Level - Connections[B].PassageNodeB.Level)); for ( A = 0; A <= JitterLayer.GetNodeCount - 1; A++ ) { if ( Connections[B].IsRamp ) { NodeTag = (clsNodeTag)(JitterLayer.get_GetNode(A).Tag); XY_int = MathUtil.PointGetClosestPosOnLine(Connections[B].PassageNodeA.Pos, Connections[B].PassageNodeB.Pos, NodeTag.Pos); Dist = Convert.ToSingle((XY_int - NodeTag.Pos).ToDoubles().GetMagnitude()); if ( Dist < RampArgs.RampLength * 2.0F ) { SetBaseLevelRamp(RampArgs, JitterLayer.get_GetNode(A)); } } } } for ( A = 0; A <= BaseLayer.GetNodeCount - 1; A++ ) { NodeTag = (clsNodeTag)(BaseLayer.get_GetNode(A).Tag); Map.Terrain.Vertices[(int)(NodeTag.Pos.X / 128.0F), (int)(NodeTag.Pos.Y / 128.0F)].Height = (byte)(BaseNodeLevel.NodeLevels[A] * LevelHeight); } return ReturnResult; }