Refactoring, optimizations

This commit is contained in:
Tiberiu Chibici 2015-06-13 21:36:32 +03:00
parent 9ac0deec0b
commit 778d732866
37 changed files with 169 additions and 66 deletions

View File

@ -48,11 +48,13 @@
<ItemGroup>
<Compile Include="Assets\Scripts\Business\BiomeManager.cs" />
<Compile Include="Assets\Scripts\Business\ConfigManager.cs" />
<Compile Include="Assets\Scripts\Generator\BuildingGenerator.cs" />
<Compile Include="Assets\Scripts\Generator\CityGenerator.cs" />
<Compile Include="Assets\Scripts\Generator\PopulationCentersGenerator.cs" />
<Compile Include="Assets\Scripts\Generator\RoadGenerator.cs" />
<Compile Include="Assets\Scripts\Generator\TerrainGenerator.cs" />
<Compile Include="Assets\Scripts\Business\Generator\BuildingGenerator.cs" />
<Compile Include="Assets\Scripts\Business\Generator\CityGenerator.cs" />
<Compile Include="Assets\Scripts\Business\Generator\PopulationCentersGenerator.cs" />
<Compile Include="Assets\Scripts\Business\Generator\RoadGenerator.cs" />
<Compile Include="Assets\Scripts\Business\Generator\TerrainGenerator.cs" />
<Compile Include="Assets\Scripts\Business\Noise\NoiseGenerator.cs" />
<Compile Include="Assets\Scripts\Business\Noise\PerlinNoiseGenerator.cs" />
<Compile Include="Assets\Scripts\Model\Biome.cs" />
<Compile Include="Assets\Scripts\Model\Building.cs" />
<Compile Include="Assets\Scripts\Model\BuildingLot.cs" />
@ -63,14 +65,13 @@
<Compile Include="Assets\Scripts\Model\LineSegment.cs" />
<Compile Include="Assets\Scripts\Model\Map.cs" />
<Compile Include="Assets\Scripts\Model\Polygon.cs" />
<Compile Include="Assets\Scripts\Model\Range.cs" />
<Compile Include="Assets\Scripts\Model\Rectangle.cs" />
<Compile Include="Assets\Scripts\Model\Road\RoadNetwork.cs" />
<Compile Include="Assets\Scripts\Model\Road\RoadNode.cs" />
<Compile Include="Assets\Scripts\Model\Road\RoadSegment.cs" />
<Compile Include="Assets\Scripts\Model\Texture.cs" />
<Compile Include="Assets\Scripts\Model\Vector2.cs" />
<Compile Include="Assets\Scripts\Noise\NoiseGenerator.cs" />
<Compile Include="Assets\Scripts\Noise\PerlinNoiseGenerator.cs" />
<Compile Include="Assets\Scripts\Unity\BuildingMeshGenerator.cs" />
<Compile Include="Assets\Scripts\Unity\InitializeScript.cs" />
<Compile Include="Assets\Scripts\Unity\RoadMeshGenerator.cs" />
@ -82,7 +83,6 @@
<Compile Include="Assets\Scripts\Utils\Logger.cs" />
<Compile Include="Assets\Scripts\Utils\QuadTree.cs" />
<Compile Include="Assets\Scripts\Utils\RandomExtensions.cs" />
<Compile Include="Assets\Scripts\Utils\Range.cs" />
<Compile Include="Assets\Scripts\Utils\SerializationHelper.cs" />
<Compile Include="Assets\Scripts\Utils\Task.cs" />
<Compile Include="Assets\Scripts\Utils\Texture2DExtensions.cs" />
@ -91,6 +91,9 @@
<None Include="Assets\Standard Assets\Environment\Water (Basic)\Shaders\FXWaterBasic.shader" />
<None Include="Assets\Data\Biomes\Grassland.xml" />
<None Include="Assets\Data\Biomes\Mountain.xml" />
<Reference Include="Assembly-UnityScript-firstpass">
<HintPath>C:/Users/Tibi/Google Drive/FacultateCY/$ Licenta/Game/Library/ScriptAssemblies/Assembly-UnityScript-firstpass.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.UI">
<HintPath>C:/Program Files/Unity/Editor/Data/UnityExtensions/Unity/GUISystem/UnityEngine.UI.dll</HintPath>
</Reference>
@ -98,6 +101,10 @@
<HintPath>C:/Program Files/Unity/Editor/Data/PlaybackEngines/iossupport/UnityEditor.iOS.Extensions.Xcode.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="Assembly-CSharp-firstpass-vs.csproj">
<Project>{D8888F6A-ABE1-C49B-1A38-F65A81021668}</Project> <Name>Assembly-CSharp-firstpass-vs</Name> </ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

View File

@ -48,11 +48,13 @@
<ItemGroup>
<Compile Include="Assets\Scripts\Business\BiomeManager.cs" />
<Compile Include="Assets\Scripts\Business\ConfigManager.cs" />
<Compile Include="Assets\Scripts\Generator\BuildingGenerator.cs" />
<Compile Include="Assets\Scripts\Generator\CityGenerator.cs" />
<Compile Include="Assets\Scripts\Generator\PopulationCentersGenerator.cs" />
<Compile Include="Assets\Scripts\Generator\RoadGenerator.cs" />
<Compile Include="Assets\Scripts\Generator\TerrainGenerator.cs" />
<Compile Include="Assets\Scripts\Business\Generator\BuildingGenerator.cs" />
<Compile Include="Assets\Scripts\Business\Generator\CityGenerator.cs" />
<Compile Include="Assets\Scripts\Business\Generator\PopulationCentersGenerator.cs" />
<Compile Include="Assets\Scripts\Business\Generator\RoadGenerator.cs" />
<Compile Include="Assets\Scripts\Business\Generator\TerrainGenerator.cs" />
<Compile Include="Assets\Scripts\Business\Noise\NoiseGenerator.cs" />
<Compile Include="Assets\Scripts\Business\Noise\PerlinNoiseGenerator.cs" />
<Compile Include="Assets\Scripts\Model\Biome.cs" />
<Compile Include="Assets\Scripts\Model\Building.cs" />
<Compile Include="Assets\Scripts\Model\BuildingLot.cs" />
@ -63,14 +65,13 @@
<Compile Include="Assets\Scripts\Model\LineSegment.cs" />
<Compile Include="Assets\Scripts\Model\Map.cs" />
<Compile Include="Assets\Scripts\Model\Polygon.cs" />
<Compile Include="Assets\Scripts\Model\Range.cs" />
<Compile Include="Assets\Scripts\Model\Rectangle.cs" />
<Compile Include="Assets\Scripts\Model\Road\RoadNetwork.cs" />
<Compile Include="Assets\Scripts\Model\Road\RoadNode.cs" />
<Compile Include="Assets\Scripts\Model\Road\RoadSegment.cs" />
<Compile Include="Assets\Scripts\Model\Texture.cs" />
<Compile Include="Assets\Scripts\Model\Vector2.cs" />
<Compile Include="Assets\Scripts\Noise\NoiseGenerator.cs" />
<Compile Include="Assets\Scripts\Noise\PerlinNoiseGenerator.cs" />
<Compile Include="Assets\Scripts\Unity\BuildingMeshGenerator.cs" />
<Compile Include="Assets\Scripts\Unity\InitializeScript.cs" />
<Compile Include="Assets\Scripts\Unity\RoadMeshGenerator.cs" />
@ -82,7 +83,6 @@
<Compile Include="Assets\Scripts\Utils\Logger.cs" />
<Compile Include="Assets\Scripts\Utils\QuadTree.cs" />
<Compile Include="Assets\Scripts\Utils\RandomExtensions.cs" />
<Compile Include="Assets\Scripts\Utils\Range.cs" />
<Compile Include="Assets\Scripts\Utils\SerializationHelper.cs" />
<Compile Include="Assets\Scripts\Utils\Task.cs" />
<Compile Include="Assets\Scripts\Utils\Texture2DExtensions.cs" />
@ -98,6 +98,12 @@
<HintPath>C:/Program Files/Unity/Editor/Data/PlaybackEngines/iossupport/UnityEditor.iOS.Extensions.Xcode.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="Assembly-CSharp-firstpass.csproj">
<Project>{D8888F6A-ABE1-C49B-1A38-F65A81021668}</Project> <Name>Assembly-CSharp-firstpass</Name> </ProjectReference>
<ProjectReference Include="Assembly-UnityScript-firstpass.unityproj">
<Project>{6F824ECF-F2BC-827E-A29D-FB10759A3283}</Project> <Name>Assembly-UnityScript-firstpass</Name> </ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

View File

@ -100,7 +100,7 @@ namespace TransportGame.Generator
private bool CanAllocate(Vector2 pos, BuildingLot lot0)
{
if (!lotTree.Boundary.Contains(pos))
if (lot0.Points.Any(p => !lotTree.Boundary.Contains(p)))
return false;
// Test other lots
@ -144,21 +144,38 @@ namespace TransportGame.Generator
Polygon GeneratePrimitivePolygon(BuildingLot lot)
{
int sides = random.Next(3, 6); // Number of sides
List<Vector2> points = new List<Vector2>();
int sides = random.Next(4, 7); // Number of sides
float angle = 2 * (float)Math.PI / sides; // Angle between sides
bool ok;
float radius = random.NextSingle(lot.Size * 0.15f, lot.Size * 0.4f); // Length of a side
Vector2 current = lot.Position + new Vector2(random.NextSingle(-lot.Size / 4, lot.Size / 4), random.NextSingle(-lot.Size / 4, lot.Size / 4));
do
{
// Reset
ok = true;
points.Clear();
// Generate radius, start position and direction
float radius = random.NextSingle(lot.Size * 0.25f, lot.Size * 0.5f); // Length of a side
Vector2 current = lot.Position + new Vector2(random.NextSingle(-lot.Size / 2, lot.Size / 2), random.NextSingle(-lot.Size / 2, lot.Size / 2));
Vector2 dir = new Vector2(random.NextSingle(), random.NextSingle()).Normalized * radius;
List<Vector2> points = new List<Vector2>();
// Generate polygon
for (int i = 0; i < sides; i++)
{
points.Add(current);
// Make sure every point is inside
if (!lot.IsInside(current))
{
ok = false;
break;
}
current = current + dir;
dir = dir.Rotate(angle);
}
} while (!ok);
return new Polygon(points);
}
@ -185,7 +202,7 @@ namespace TransportGame.Generator
}
else
{
b.LevelHeights[i] = random.NextSingle(ConfigManager.Buildgen.MinBuildingHeight, ConfigManager.Buildgen.MaxBuildingHeight);
b.LevelHeights[i] = random.NextSingle(ConfigManager.Buildgen.MinBuildingHeight, ConfigManager.Buildgen.MaxBuildingHeight) * map.GetPopulation(lot.Position);
}
for (int j = 0; j < random.Next(ConfigManager.Buildgen.MaxPolygonsPerLevel); j++)

View File

@ -15,7 +15,7 @@ namespace TransportGame.Generator
{
// Generate range
float mp = (float)(map.Width * map.Height) / (1024 * 1024); // For 4k x 4k range should be around 900
map.PopulationCenterRange = mp * 155f / 5f + 1490f / 3f; // For 2k x 2k range should be around 600
map.PopulationCenterRange = mp * 31f + 496.66f; // For 2k x 2k range should be around 600
// Generate a number of points
int maxPoints = 16 * (int) Math.Sqrt(mp);
@ -27,8 +27,8 @@ namespace TransportGame.Generator
do
{
px = random.Next(map.Width / 6, 5 * map.Width / 6);
py = random.Next(map.Height / 6, 5 * map.Height / 6);
px = random.Next(map.Width / 8, 7 * map.Width / 8);
py = random.Next(map.Height / 8, 7 * map.Height / 8);
}
while (map.IsWater(px, py));

View File

@ -97,5 +97,29 @@ namespace TransportGame.Model
return false;
}
/// <summary>
/// Tests if a point is inside the building lot
/// </summary>
/// <param name="point"></param>
/// <returns></returns>
public bool IsInside(Vector2 point)
{
return IsInsideTriangle(point, Points[0], Points[1], Points[2]) || IsInsideTriangle(point, Points[0], Points[2], Points[3]);
}
private static float Sign(Vector2 a, Vector2 b, Vector2 c)
{
return (a.X - c.X) * (b.Y - c.Y) - (b.X - c.X) * (a.Y - c.Y);
}
private static bool IsInsideTriangle(Vector2 pt, Vector2 a, Vector2 b, Vector2 c)
{
bool b1 = Sign(pt, a, b) < 0;
bool b2 = Sign(pt, b, c) < 0;
bool b3 = Sign(pt, c, a) < 0;
return (b1 == b2) && (b2 == b3);
}
}
}

View File

@ -49,8 +49,8 @@ namespace TransportGame.Model.Config
LotSquareMaxSize = 20f;
LotSpacing = 0.1f;
MaxLotAttempts = 3;
MaxBuildingHeight = 20f;
MinBuildingHeight = 4f;
MaxBuildingHeight = 25f;
MinBuildingHeight = 5f;
MaxBuildingLevels = 7;
MaxPolygonsPerLevel = 4;
}

View File

@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace TransportGame.Utils
namespace TransportGame.Model
{
public class Range
{

View File

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using UnityEngine;
namespace TransportGame.Model
{
@ -277,5 +276,69 @@ namespace TransportGame.Model
{
return X.GetHashCode() * 7 + Y.GetHashCode();
}
private class LengthComparerImpl : IComparer<Vector2>
{
public int Compare(Vector2 a, Vector2 b)
{
if (a.LengthSq > b.LengthSq)
return 1;
if (a.LengthSq < b.LengthSq)
return -1;
return 0;
}
}
private class TrigonometricComparerImpl : IComparer<Vector2>
{
private int Quad(Vector2 v)
{
if (v.Y >= 0)
{
if (v.X >= 0)
return 0;
return 1;
}
else
{
if (v.X < 0)
return 2;
return 3;
}
}
public int Compare(Vector2 a, Vector2 b)
{
// If vectors are in different quadrants, we can use quadrant number
int qa = Quad(a), qb = Quad(b);
if (qa != qb)
{
return qa - qb;
}
// In same quadrant. Compute cross product which gives us sin(ab)*len(a)*len(b)
// Vectors are in same quadrant, so angle should be less than 90deg
float cross = Cross(a, b);
if (cross < 0) // Angle > 180 degrees => a > b
return 1;
if (cross > 0) // Angle < 180 degrees => a < b
return -1;
// Points are on the same line. Use distance
if (a.LengthSq > b.LengthSq)
return 1;
else if (a.LengthSq < b.LengthSq)
return -1;
// Points are equal
return 0;
}
}
public static IComparer<Vector2> LengthComparer = new LengthComparerImpl();
public static IComparer<Vector2> TrigonomicComparer = new TrigonometricComparerImpl();
}
}

View File

@ -139,26 +139,12 @@ namespace TransportGame.Unity
uv.Add(v2);
};
// Add current node
// Add the node
addV(node.Position, 0);
vindex += 1;
// Sort adjacent segments in trigonometric order
var segs = node.ArticulationSegments.OrderBy(segment =>
{
Vector2 dir = DirectionFrom(segment, node);
if (dir.X >= 0 && dir.Y >= 0) // First quadrant:
return dir.Y; // Y coordinate grows from 0 to 1
else if (dir.X <= 0 && dir.Y >= 0) // Second quadrant:
return 1 + Math.Abs(dir.X); // X goes from 0 to -1
else if (dir.X <= 0 && dir.Y <= 0) // Third quadrant:
return 2 + Math.Abs(dir.Y); // Y goes from 0 to -1
else return 3 + dir.X; // Fourth quadrant: X grows from 0 to 1
}).ToArray();
var segs = node.ArticulationSegments.OrderBy(segment => DirectionFrom(segment, node), Vector2.TrigonomicComparer).ToArray();
Vector2[] sideCrns = new Vector2[segs.Length];
Vector2[] strCrns = new Vector2[segs.Length];

Binary file not shown.

View File

@ -16,7 +16,7 @@ Platform assembly: C:\Users\Tibi\Google Drive\FacultateCY\$ Licenta\Game\Transpo
Loading C:\Users\Tibi\Google Drive\FacultateCY\$ Licenta\Game\TransportGame_Data\Managed\Assembly-UnityScript-firstpass.dll into Unity Child Domain
Platform assembly: C:\Users\Tibi\Google Drive\FacultateCY\$ Licenta\Game\TransportGame_Data\Managed\UnityEngine.UI.dll (this message is harmless)
Loading C:\Users\Tibi\Google Drive\FacultateCY\$ Licenta\Game\TransportGame_Data\Managed\UnityEngine.UI.dll into Unity Child Domain
- Completed reload, in 0.050 seconds
- Completed reload, in 0.045 seconds
desktop: 1920x1080 60Hz; virtual: 1920x1080 at 0,0
<RI> Initializing input.
<RI> Input initialized.

View File

@ -70,11 +70,13 @@
<ItemGroup>
<Compile Include="Assets\Scripts\Business\BiomeManager.cs" />
<Compile Include="Assets\Scripts\Business\ConfigManager.cs" />
<Compile Include="Assets\Scripts\Generator\BuildingGenerator.cs" />
<Compile Include="Assets\Scripts\Generator\CityGenerator.cs" />
<Compile Include="Assets\Scripts\Generator\PopulationCentersGenerator.cs" />
<Compile Include="Assets\Scripts\Generator\RoadGenerator.cs" />
<Compile Include="Assets\Scripts\Generator\TerrainGenerator.cs" />
<Compile Include="Assets\Scripts\Business\Generator\BuildingGenerator.cs" />
<Compile Include="Assets\Scripts\Business\Generator\CityGenerator.cs" />
<Compile Include="Assets\Scripts\Business\Generator\PopulationCentersGenerator.cs" />
<Compile Include="Assets\Scripts\Business\Generator\RoadGenerator.cs" />
<Compile Include="Assets\Scripts\Business\Generator\TerrainGenerator.cs" />
<Compile Include="Assets\Scripts\Business\Noise\NoiseGenerator.cs" />
<Compile Include="Assets\Scripts\Business\Noise\PerlinNoiseGenerator.cs" />
<Compile Include="Assets\Scripts\Model\Biome.cs" />
<Compile Include="Assets\Scripts\Model\Building.cs" />
<Compile Include="Assets\Scripts\Model\BuildingLot.cs" />
@ -85,14 +87,13 @@
<Compile Include="Assets\Scripts\Model\LineSegment.cs" />
<Compile Include="Assets\Scripts\Model\Map.cs" />
<Compile Include="Assets\Scripts\Model\Polygon.cs" />
<Compile Include="Assets\Scripts\Model\Range.cs" />
<Compile Include="Assets\Scripts\Model\Rectangle.cs" />
<Compile Include="Assets\Scripts\Model\Road\RoadNetwork.cs" />
<Compile Include="Assets\Scripts\Model\Road\RoadNode.cs" />
<Compile Include="Assets\Scripts\Model\Road\RoadSegment.cs" />
<Compile Include="Assets\Scripts\Model\Texture.cs" />
<Compile Include="Assets\Scripts\Model\Vector2.cs" />
<Compile Include="Assets\Scripts\Noise\NoiseGenerator.cs" />
<Compile Include="Assets\Scripts\Noise\PerlinNoiseGenerator.cs" />
<Compile Include="Assets\Scripts\Unity\BuildingMeshGenerator.cs" />
<Compile Include="Assets\Scripts\Unity\InitializeScript.cs" />
<Compile Include="Assets\Scripts\Unity\RoadMeshGenerator.cs" />
@ -104,7 +105,6 @@
<Compile Include="Assets\Scripts\Utils\Logger.cs" />
<Compile Include="Assets\Scripts\Utils\QuadTree.cs" />
<Compile Include="Assets\Scripts\Utils\RandomExtensions.cs" />
<Compile Include="Assets\Scripts\Utils\Range.cs" />
<Compile Include="Assets\Scripts\Utils\SerializationHelper.cs" />
<Compile Include="Assets\Scripts\Utils\Task.cs" />
<Compile Include="Assets\Scripts\Utils\Texture2DExtensions.cs" />