Removed logs from road generator

This commit is contained in:
Tiberiu Chibici 2015-05-29 19:10:27 +03:00
parent 54b833b620
commit 61a05289d3
2 changed files with 16 additions and 82 deletions

View File

@ -98,15 +98,10 @@ namespace TransportGame.Generator
} while (IsObstacle(p0) || IsObstacle(p1) || IsObstacle(p2)); } while (IsObstacle(p0) || IsObstacle(p1) || IsObstacle(p2));
// Logger.Info("Generated initial segment: {0} -> {1}", p0, p1);
// Logger.Info("Generated initial segment: {0} -> {1}", p0, p2);
// Create root node // Create root node
var node0 = map.RoadNetwork.CreateNode(p0); var node0 = map.RoadNetwork.CreateNode(p0);
qtree.Add(node0); qtree.Add(node0);
// Logger.Info("Generated root node: {0}", node0);
// Create & enqueue segments // Create & enqueue segments
queue.Add(new RoadGeneratorSegment(node0, p1, true)); queue.Add(new RoadGeneratorSegment(node0, p1, true));
queue.Add(new RoadGeneratorSegment(node0, p2, true)); queue.Add(new RoadGeneratorSegment(node0, p2, true));
@ -114,17 +109,12 @@ namespace TransportGame.Generator
public void Step() public void Step()
{ {
// Logger.Info(">>> BEGAN STEP <<<");
var segment = queue.OrderBy(x => x.Time).First(); var segment = queue.OrderBy(x => x.Time).First();
queue.Remove(segment); queue.Remove(segment);
// Logger.Info("Dequeued segment {0}", segment);
// Check local constraints // Check local constraints
if (CheckLocalConstraints(segment)) if (CheckLocalConstraints(segment))
{ {
// Logger.Info("Local constraints check succeeded.");
RoadSegment createdSegment; RoadSegment createdSegment;
// Finish to create segment // Finish to create segment
@ -142,13 +132,8 @@ namespace TransportGame.Generator
{ {
newSegment.Time += segment.Time + 1; newSegment.Time += segment.Time + 1;
queue.Add(newSegment); queue.Add(newSegment);
// Logger.Info("Added segment to queue: {0}", newSegment);
} }
} }
else
{
// Logger.Info("Local constraints check failed!");
}
} }
private bool IsObstacle(Vector2 p) private bool IsObstacle(Vector2 p)
@ -173,46 +158,30 @@ namespace TransportGame.Generator
bool highway = (segment.LanesTo1 >= 3); bool highway = (segment.LanesTo1 >= 3);
bool highwayBranched = false; bool highwayBranched = false;
// Logger.Info("> Computing global goals. prevPos={0}, dir={1}, highway={2}", prevPos, dir, highway);
// Going straight // Going straight
Vector2 straight = prevPos + dir * ((highway) ? HighwaySegmentLength : DefaultSegmentLength); Vector2 straight = prevPos + dir * ((highway) ? HighwaySegmentLength : DefaultSegmentLength);
float straightPopulation = map.GetPopulation(straight); float straightPopulation = map.GetPopulation(straight);
// Logger.Info("> Straight={0} StraightPopulation={1}", straight, straightPopulation);
// Highways... // Highways...
if (highway) if (highway)
{ {
// Logger.Info("> Highway case:");
Vector2 randomStraight = prevPos + HighwaySegmentLength * dir.RotateDeg(random.Next(-MaximumRandomStraightAngle, MaximumRandomStraightAngle)); Vector2 randomStraight = prevPos + HighwaySegmentLength * dir.RotateDeg(random.Next(-MaximumRandomStraightAngle, MaximumRandomStraightAngle));
float randomPopulation = map.GetPopulation(randomStraight); float randomPopulation = map.GetPopulation(randomStraight);
// Logger.Info("> RandomStraight={0} RandomPopulation={1}", randomStraight, randomPopulation);
if (randomPopulation > straightPopulation) if (randomPopulation > straightPopulation)
{
// Logger.Info("> Yielding random straight vector.");
yield return new RoadGeneratorSegment(segment.Terminal2, randomStraight, highway); yield return new RoadGeneratorSegment(segment.Terminal2, randomStraight, highway);
}
else else
{
// Logger.Info("> Yielding straight vector.");
yield return new RoadGeneratorSegment(segment.Terminal2, straight, highway); yield return new RoadGeneratorSegment(segment.Terminal2, straight, highway);
}
// Branch highway // Branch highway
if (Math.Max(straightPopulation, randomPopulation) > HighwayBranchPopulationTreshold) if (Math.Max(straightPopulation, randomPopulation) > HighwayBranchPopulationTreshold)
{ {
// Logger.Info("> Above treshold. Branching...");
if (random.NextSingle() < HighwayBranchProbability) if (random.NextSingle() < HighwayBranchProbability)
{ {
Vector2 leftBranch = prevPos + HighwaySegmentLength * dir.RotateDeg(-90 + random.Next(-MaximumBranchAngleVariation, MaximumBranchAngleVariation)); Vector2 leftBranch = prevPos + HighwaySegmentLength * dir.RotateDeg(-90 + random.Next(-MaximumBranchAngleVariation, MaximumBranchAngleVariation));
yield return new RoadGeneratorSegment(segment.Terminal2, leftBranch, highway, HighwayBranchDelay); yield return new RoadGeneratorSegment(segment.Terminal2, leftBranch, highway, HighwayBranchDelay);
highwayBranched = true; highwayBranched = true;
// Logger.Info("> Branch to the left: {0}", leftBranch);
} }
if (random.NextSingle() < HighwayBranchProbability) if (random.NextSingle() < HighwayBranchProbability)
{ {
@ -220,7 +189,6 @@ namespace TransportGame.Generator
yield return new RoadGeneratorSegment(segment.Terminal2, rightBranch, highway, HighwayBranchDelay); yield return new RoadGeneratorSegment(segment.Terminal2, rightBranch, highway, HighwayBranchDelay);
highwayBranched = true; highwayBranched = true;
// Logger.Info("> Branch to the right: {0}", rightBranch);
} }
} }
@ -230,42 +198,29 @@ namespace TransportGame.Generator
} }
else if (random.NextSingle() < straightPopulation) else if (random.NextSingle() < straightPopulation)
{
// Logger.Info("> Not highway. Yielding straight vector.");
yield return new RoadGeneratorSegment(segment.Terminal2, straight, false); yield return new RoadGeneratorSegment(segment.Terminal2, straight, false);
}
// Branch normal road // Branch normal road
if (straightPopulation > DefaultBranchPopulationTreshold) if (straightPopulation > DefaultBranchPopulationTreshold)
{ {
// Logger.Info("Straight population above branch treshold. Branching...");
if (random.NextSingle() < DefaultBranchProbability * straightPopulation) if (random.NextSingle() < DefaultBranchProbability * straightPopulation)
{ {
Vector2 leftBranch = prevPos + HighwaySegmentLength * dir.RotateDeg(-90 + random.Next(-MaximumBranchAngleVariation, MaximumBranchAngleVariation)); Vector2 leftBranch = prevPos + HighwaySegmentLength * dir.RotateDeg(-90 + random.Next(-MaximumBranchAngleVariation, MaximumBranchAngleVariation));
yield return new RoadGeneratorSegment(segment.Terminal2, leftBranch, false, (highway) ? HighwayBranchDelay : 0); yield return new RoadGeneratorSegment(segment.Terminal2, leftBranch, false, (highway) ? HighwayBranchDelay : 0);
// Logger.Info("> Branch to the left: {0}", leftBranch);
} }
if (random.NextSingle() < DefaultBranchProbability * straightPopulation) if (random.NextSingle() < DefaultBranchProbability * straightPopulation)
{ {
Vector2 rightBranch = prevPos + HighwaySegmentLength * dir.RotateDeg(90 + random.Next(-MaximumBranchAngleVariation, MaximumBranchAngleVariation)); Vector2 rightBranch = prevPos + HighwaySegmentLength * dir.RotateDeg(90 + random.Next(-MaximumBranchAngleVariation, MaximumBranchAngleVariation));
yield return new RoadGeneratorSegment(segment.Terminal2, rightBranch, false, (highway) ? HighwayBranchDelay : 0); yield return new RoadGeneratorSegment(segment.Terminal2, rightBranch, false, (highway) ? HighwayBranchDelay : 0);
// Logger.Info("> Branch to the right: {0}", rightBranch);
} }
} }
} }
private bool CheckLocalConstraints(RoadGeneratorSegment segment) private bool CheckLocalConstraints(RoadGeneratorSegment segment)
{ {
// Logger.Info("Checking local constraints...");
// Constraint #1: check for obstacles // Constraint #1: check for obstacles
if (IsObstacle(segment.Terminal2Pos)) if (IsObstacle(segment.Terminal2Pos))
{
// Logger.Info("Obstacle.");
return false; return false;
}
// Constraint #2: slope // Constraint #2: slope
float segmentLength = (segment.Highway) ? HighwaySegmentLength : DefaultSegmentLength; float segmentLength = (segment.Highway) ? HighwaySegmentLength : DefaultSegmentLength;
@ -273,13 +228,8 @@ namespace TransportGame.Generator
map.GetHeight((int)segment.Terminal2Pos.X, (int)segment.Terminal2Pos.Y); map.GetHeight((int)segment.Terminal2Pos.X, (int)segment.Terminal2Pos.Y);
float sinSlope = Math.Abs(levelDiff) / segmentLength; float sinSlope = Math.Abs(levelDiff) / segmentLength;
// Logger.Info("Level difference is {0}, slope is (rads) {1}", levelDiff, Math.Asin(sinSlope));
if (Math.Asin(sinSlope) > SlopeLimit) if (Math.Asin(sinSlope) > SlopeLimit)
{
// Logger.Info("Slope too big!");
return false; return false;
}
// Constraint #3: Number of intersecting roads // Constraint #3: Number of intersecting roads
if (segment.Terminal1.ArticulationSegmentIds.Count > MaximumIntersectingRoads) if (segment.Terminal1.ArticulationSegmentIds.Count > MaximumIntersectingRoads)
@ -292,19 +242,13 @@ namespace TransportGame.Generator
Math.Max(segment.Terminal1.X, segment.Terminal2Pos.X) + 3 * HighwaySegmentLength, Math.Max(segment.Terminal1.X, segment.Terminal2Pos.X) + 3 * HighwaySegmentLength,
Math.Max(segment.Terminal1.Y, segment.Terminal2Pos.Y) + 3 * HighwaySegmentLength); Math.Max(segment.Terminal1.Y, segment.Terminal2Pos.Y) + 3 * HighwaySegmentLength);
// Logger.Info("Searching area {0} for intersecting segments.", queryArea);
IEnumerable<int> segmentIds = Enumerable.Empty<int>(); IEnumerable<int> segmentIds = Enumerable.Empty<int>();
// Look for nearby segments // Look for nearby segments
foreach (var node in qtree.Query(queryArea)) foreach (var node in qtree.Query(queryArea))
{ {
// Logger.Info("Found node: {0}", node);
if (node == segment.Terminal1) if (node == segment.Terminal1)
{
// Logger.Info("Node is originating node. Will ignore.");
continue; continue;
}
// Too close to another node in the area // Too close to another node in the area
if ((node.Position - segment.Terminal2Pos).LengthSq < MinNodeDistance * MinNodeDistance) if ((node.Position - segment.Terminal2Pos).LengthSq < MinNodeDistance * MinNodeDistance)
@ -327,31 +271,22 @@ namespace TransportGame.Generator
var line1 = new LineSegment(segment.Terminal1.Position, segment.Terminal2Pos); var line1 = new LineSegment(segment.Terminal1.Position, segment.Terminal2Pos);
var line2 = new LineSegment(other.Terminal1.Position, other.Terminal2.Position); var line2 = new LineSegment(other.Terminal1.Position, other.Terminal2.Position);
// Logger.Info("Found segment: {0}. Will test intersection between segments {0} and {1}", other, line1, line2);
Vector2? inters = LineSegment.Intersect(line1, line2); Vector2? inters = LineSegment.Intersect(line1, line2);
// Case #1: there is an intersection with another segment. We cut the rest of the segment // Case #1: there is an intersection with another segment. We cut the rest of the segment
if (inters.HasValue && inters.Value != segment.Terminal1.Position) if (inters.HasValue && inters.Value != segment.Terminal1.Position)
{ {
// Logger.Info("Found intersection point: {0}", inters);
// Check angle between segments // Check angle between segments
float cos = Vector2.Dot((line1.P1 - line1.P0).Normalized, (line2.P1 - line2.P0).Normalized); float cos = Vector2.Dot((line1.P1 - line1.P0).Normalized, (line2.P1 - line2.P0).Normalized);
if (Math.Abs(Math.Acos(cos)) < RoadSegmentAngleLimit) if (Math.Abs(Math.Acos(cos)) < RoadSegmentAngleLimit)
{
// Logger.Info("Angle between segments is too small ({0} rads)", Math.Abs(Math.Acos(cos)));
return false; return false;
}
// Split segment // Split segment
var newNode = map.RoadNetwork.SplitArticulationSegment(other, inters.Value); var newNode = map.RoadNetwork.SplitArticulationSegment(other, inters.Value);
segment.Terminal2Pos = inters.Value; segment.Terminal2Pos = inters.Value;
segment.Terminal2 = newNode; segment.Terminal2 = newNode;
// Logger.Info("Performed split in point: {0}", newNode);
return true; return true;
} }
else // Logger.Info("Does not intersect.");
// Case #2: no intersection, but the point is close enough to an existing intersection // Case #2: no intersection, but the point is close enough to an existing intersection
if ((segment.Terminal2Pos - other.Terminal2.Position).LengthSq <= RoadSnapDistance * RoadSnapDistance) if ((segment.Terminal2Pos - other.Terminal2.Position).LengthSq <= RoadSnapDistance * RoadSnapDistance)
@ -361,13 +296,9 @@ namespace TransportGame.Generator
{ {
float cos = Vector2.Dot((line1.P1 - line1.P0).Normalized, (intersSeg.Terminal2.Position - intersSeg.Terminal1.Position).Normalized); float cos = Vector2.Dot((line1.P1 - line1.P0).Normalized, (intersSeg.Terminal2.Position - intersSeg.Terminal1.Position).Normalized);
if (Math.Abs(Math.Acos(cos)) < RoadSegmentAngleLimit) if (Math.Abs(Math.Acos(cos)) < RoadSegmentAngleLimit)
{
// Logger.Info("Angle between segments is too small ({0} rads)", Math.Abs(Math.Acos(cos)));
return false; return false;
} }
}
// Logger.Info("Point is close to existing intersection: {0}, will snap.", other.Terminal2);
segment.Terminal2Pos = other.Terminal2.Position; segment.Terminal2Pos = other.Terminal2.Position;
segment.Terminal2 = other.Terminal2; segment.Terminal2 = other.Terminal2;
return true; return true;

View File

@ -109,5 +109,8 @@
<None Include="Assets\Data\Config\tergen.xml" /> <None Include="Assets\Data\Config\tergen.xml" />
<None Include="Assets\Standard Assets\Environment\Water (Basic)\Shaders\FXWaterBasic.shader" /> <None Include="Assets\Standard Assets\Environment\Water (Basic)\Shaders\FXWaterBasic.shader" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Assets\Scripts\Storage\" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\SyntaxTree\UnityVS\2013\UnityVS.CSharp.targets" /> <Import Project="$(MSBuildExtensionsPath)\SyntaxTree\UnityVS\2013\UnityVS.CSharp.targets" />
</Project> </Project>