Ejemplo n.º 1
0
        private void Awake()
        {
            // Disabling physics. Not used in project.
            Physics.autoSimulation = false;

            // Parsing Countries.
            var(_, tagLookup, _) = CountriesLoad.Names();

            // Creating StateCountryProcessing system
            var stateCountryProcessing = World.DefaultGameObjectInjectionWorld
                                         .GetOrCreateSystem(typeof(StateCountryProcessing));

            // Parsing goods

            /*
             * var fileTree = new List<(string, object)>();
             * ParseFile(Path.Combine(Application.streamingAssetsPath, "common", "goods.txt"), fileTree);
             * var goodsLookup = new Dictionary<string, int>();
             * // Ignoring good groups
             * var counter = 0;
             * foreach (var (_, value) in fileTree)
             * foreach (var (key, innerValue) in (List<(string, object)>) value)
             * {
             *  var good = new Goods {Name = key};
             *  goodsLookup.Add(key, counter++);
             *  foreach (var (type, data) in (List<(string, object)>) innerValue)
             *  {
             *      switch (type)
             *      {
             *          case "cost":
             *              good.Cost = float.Parse((string) data);
             *              continue;
             *          case "color":
             *              good.Color = ParseColor32((string) data);
             *              continue;
             *      }
             *  }
             *
             *  em.SetComponentData(em.CreateEntity(typeof(Goods)), good);
             * }
             */

            // Parsing provinces
            var colorLookup  = new NativeHashMap <Color, int>(1, Allocator.TempJob);
            var oceanDefault = tagLookup["OCEAN"];

            var(provNames, idLookup, provEntityLookup) =
                DefinitionsLoad.Main(colorLookup, oceanDefault);

            //var map = LoadPng(Path.Combine(Application.streamingAssetsPath, "map", "provinces.png"));

            // DEBUG
            var map = new Texture2D(ProvinceMap.width, ProvinceMap.height, TextureFormat.RGBA32, false)
            {
                filterMode = FilterMode.Point
            };

            Graphics.CopyTexture(ProvinceMap, map);

            // Begin CPU pixel processing jobs.
            var colorMap = new NativeArray <Color32>(map.GetPixels32(), Allocator.TempJob);

            var pixelCollector = new NativeMultiHashMap <int, int>(colorMap.Length, Allocator.TempJob);

            var pixelHandle = new CollectPixels
            {
                ColorMap    = colorMap,
                ColorLookup = colorLookup,
                Collector   = pixelCollector.AsParallelWriter()
            }.Schedule(colorMap.Length, 32);

            // Parsing states
            var stateLookup = new NativeHashMap <int, int>(1, Allocator.TempJob);

            var(stateNames, stateToProvReference, provToStateReference) =
                StatesLoad.Main(idLookup, stateLookup, provEntityLookup);
            BlobAssetReferences.Enqueue(stateToProvReference);
            BlobAssetReferences.Enqueue(provToStateReference);

            var idMap = new NativeArray <Color32>(colorMap.Length, Allocator.TempJob);

            pixelHandle = new ProcessPixel
            {
                ColorLookup = colorLookup,
                ColorMap    = colorMap,
                IdMap       = idMap,
                StateLookup = stateLookup
            }.Schedule(colorMap.Length, 32, pixelHandle);

            stateLookup.Dispose(pixelHandle);

            colorMap.Dispose(pixelHandle);
            colorLookup.Dispose(pixelHandle);

            var centroids = new NativeArray <Color32>(idLookup.Count, Allocator.TempJob);

            pixelHandle = new FindCentroid
            {
                Collector = pixelCollector,
                Width     = ProvinceMap.width,
                Centroids = centroids
            }.Schedule(centroids.Length, 2, pixelHandle);

            pixelCollector.Dispose(pixelHandle);

            var(factories, maxEmploy) = AgentsLoad.Main();
            foreach (var blobAssetReference in factories)
            {
                BlobAssetReferences.Enqueue(blobAssetReference);
            }

            ProvinceLoad.Main(provEntityLookup, tagLookup, factories, maxEmploy, provToStateReference);
            // Pops load outputs a blob asset reference. Just inlining the two calls.
            BlobAssetReferences.Enqueue(PopsLoad.Main(provToStateReference));

            // Tag states that are not completely owned.
            // Also attaching owned states (plus incomplete which is duplicated) to countries.
            StateCountryProcessing.CallMethod = StateCountryProcessing.ManualMethodCall.TagOwnedStatesAndAttachToCountry;
            stateCountryProcessing.Update();

            // DEBUG
            StateCountryProcessing.MarketIdentities = factories;
            StateCountryProcessing.MaxEmploy        = maxEmploy;
            StateCountryProcessing.CallMethod       = StateCountryProcessing.ManualMethodCall.SetDebugValues;
            stateCountryProcessing.Update();

            StateCountryProcessing.CallMethod = StateCountryProcessing.ManualMethodCall.DebugSpawnFactories;
            stateCountryProcessing.Update();

            StateCountryProcessing.CallMethod = StateCountryProcessing.ManualMethodCall.DisposeDebugFactoryTemplates;
            stateCountryProcessing.Update();

            // Deleting initialization system.
            World.DefaultGameObjectInjectionWorld.DestroySystem(stateCountryProcessing);

            pixelHandle.Complete();

            map.SetPixels32(idMap.ToArray());
            map.Apply();

            var centroidTex = new Texture2D(centroids.Length, 1, TextureFormat.RGBA32, false)
            {
                filterMode = FilterMode.Point
            };

            centroidTex.SetPixels32(centroids.ToArray());
            centroidTex.Apply();

            LoadMap.MapTexture       = map;
            ScalarSystem.IdMapTex    = map;
            ScalarSystem.CentroidTex = centroidTex;

            //File.WriteAllBytes(Path.Combine(Application.streamingAssetsPath, "test.png"), centroidTex.EncodeToPNG());

            idMap.Dispose();
            centroids.Dispose();

            /*
             * Texture2D LoadPng(string filePath)
             * {
             *  if (!File.Exists(filePath))
             *      throw new Exception("Texture: " + filePath + " does not exist.");
             *
             *  var fileData = File.ReadAllBytes(filePath);
             *  var tex = new Texture2D(2, 2, TextureFormat.RGBA32, false);
             *  tex.LoadImage(fileData); //..this will auto-resize the texture dimensions.
             *  return tex;
             * }
             */
        }
Ejemplo n.º 2
0
        private void Start()
        {
            // TODO: Json inline checksum verification.
            // Very expensive conversion between Paradox files to Json arrays.

            Debug.Log("Start: " + Time.realtimeSinceStartup);
            var start = Time.realtimeSinceStartup;

            /*
             * // Loading color maps!
             * var colorBytes = File.ReadAllBytes(Path.Combine(Application.streamingAssetsPath, "Map", "provinces.png"));
             * var map = new Texture2D(2, 2);
             * map.LoadImage(colorBytes);
             * _mapComponent.ColorSize = new int2(map.width, map.height);
             * _colorMap = new NativeArray<Color>(map.GetPixels(), Allocator.TempJob);
             *
             * colorBytes = File.ReadAllBytes(Path.Combine(Application.streamingAssetsPath, "Map", "terrain.png"));
             * map.LoadImage(colorBytes);
             *
             * if (map.width != _mapComponent.ColorSize.x || map.height != _mapComponent.ColorSize.y)
             *  throw new Exception("Dimension mismatch between province and terrain textures!");
             *
             * _terrainMap = new NativeArray<Color>(map.GetPixels(), Allocator.TempJob);
             */
            //LoadScreen.SetLoadingScreen(LoadingStages.Terrain);

            var(terrainNames, terrainPalette) = TerrainLoad.Main(false);
            LookupDictionaries.AssignDictionary("TerrainNames", terrainNames);

            _terrainLookup = new NativeHashMap <Color, int>(terrainPalette.Count, Allocator.TempJob);
            foreach (var(palette, index) in terrainPalette)
            {
                _terrainLookup.TryAdd(palette, index);
            }

            //LoadScreen.SetLoadingScreen(LoadingStages.Definitions);

            var(definedNames, idIndexes, foundColors) = DefinitionsLoad.Main(false);
            _uniqueColors = new NativeHashMap <Color, int>(definedNames.Count, Allocator.TempJob);
            _idIndex      = new NativeHashMap <int, int>(definedNames.Count, Allocator.TempJob);

            // Populating idIndexes
            for (var i = 0; i < idIndexes.Count; i++)
            {
                _idIndex.TryAdd(idIndexes[i], i);
            }
            UnifiedVariables.IdIndex = _idIndex;
            // Populating unique colors
            for (var i = 0; i < foundColors.Count; i++)
            {
                _uniqueColors.TryAdd(foundColors[i], i);
            }

            //LoadScreen.SetLoadingScreen(LoadingStages.States);

            var(stateProvinces, stateIds) = RegionsLoad.Main(false, _idIndex);

            _stateLookup = new NativeHashMap <int, int>(stateIds.Count, Allocator.TempJob);
            foreach (var stateKvP in stateProvinces)
            {
                _stateLookup.TryAdd(stateKvP.x, stateKvP.y);
            }

            //LoadScreen.SetLoadingScreen(LoadingStages.Pixels);

            /*
             * _idMap = new NativeArray<Color32>(_colorMap.Length, Allocator.TempJob,
             *  NativeArrayOptions.UninitializedMemory);
             *
             * _provTerrains = new NativeMultiHashMap<int, int>(_colorMap.Length, Allocator.TempJob);
             *
             * _pixelJobHandle = new ProcessPixel
             * {
             *  ColorMap = _colorMap,
             *  UniqueColors = _uniqueColors,
             *  StateLookup = _stateLookup,
             *  IdMap = _newDataBox.IdMap,
             *
             *  TerrainMap = _terrainMap,
             *  NumOfTerrains = terrainNames.Count,
             *  TerrainLookup = _terrainLookup,
             *  ProvTerrains = _provTerrains.ToConcurrent()
             * }.Schedule(_colorMap.Length, 100);
             *
             * // Border processing, used in path finding!
             * _borderStart = new NativeHashMap<int2, bool>(_colorMap.Length * 3, Allocator.TempJob);
             *
             * _borderJobHandle = new ProcessBorderGetBorders
             * {
             *  BorderIntermediate = _borderStart.ToConcurrent(),
             *  ColorMap = _colorMap,
             *  UniqueColors = _uniqueColors,
             *  Dimensions = _mapComponent.ColorSize
             * }.Schedule(_colorMap.Length, 100);
             */

            //LoadScreen.SetLoadingScreen(LoadingStages.Continents);
            var(continentProvinces, continentNames) = ContinentLoad.Main(false, _idIndex);
            LookupDictionaries.AssignDictionary("ContinentNames", continentNames);

            //LoadScreen.SetLoadingScreen(LoadingStages.CountryNames);
            var(countryNames, countryTags, countryPaths) = CountriesLoad.Names(false);
            LookupDictionaries.AssignDictionary("CountryTags", countryTags);

            //LoadScreen.SetLoadingScreen(LoadingStages.Religions);
            var(religionNames, religionGroupNames) = ReligionsLoad.Main(false);
            LookupDictionaries.AssignDictionary("ReligionNames", religionNames);

            //LoadScreen.SetLoadingScreen(LoadingStages.Cultures);
            var(cultureNames, cultureGroupNames) = CulturesLoad.Main(false);
            LookupDictionaries.AssignDictionary("CultureNames", cultureNames);
            LookupDictionaries.AssignDictionary("CultureGroupNames", cultureGroupNames);

            //LoadScreen.SetLoadingScreen(LoadingStages.Ideologies);
            var(ideologies, ideologyGroups, ideologyNames, ideologyGroupNames) = IdeologiesLoad.Main(false);

            /*
             * //LoadScreen.SetLoadingScreen(LoadingStages.Governments);
             * var (governments, governmentNames) = GovernmentsLoad.Main();
             *
             * //LoadScreen.SetLoadingScreen(LoadingStages.Goods);
             * var (goods, goodsCategory, goodsNames, goodsCategoryNames) = GoodsLoad.Main();
             *
             * //LoadScreen.SetLoadingScreen(LoadingStages.Buildings);
             * var (buildings, buildingNames) = BuildingsLoad.Main();
             *
             * //LoadScreen.SetLoadingScreen(LoadingStages.Units);
             * var (units, unitNames) = UnitsLoad.Main();
             *
             * //LoadScreen.SetLoadingScreen(LoadingStages.PopTypes);
             * var (popTypes, popTypeNames) = PopTypesLoad.Main();
             *
             * //LoadScreen.SetLoadingScreen(LoadingStages.PolicyNames);
             * var (issueFileTree, issueValues, subPolicies, policyGroups, subPolicyNames, policyGroupNames)
             *  = IssuesLoad.PolicyNames();
             * _stringBox.PolicyGroupNames = policyGroupNames;
             * _stringBox.SubPolicyNames = subPolicyNames;
             *
             * //LoadScreen.SetLoadingScreen(LoadingStages.Technology);
             * var (technologies, schools, folders, incompleteInventions) = TechLoad.Main(ref _stringBox);
             *
             * //LoadScreen.SetLoadingScreen(LoadingStages.Crimes);
             * var (crimes, crimeNames) = CrimeLoad.Main();
             * _stringBox.CrimeNames = crimeNames;
             *
             * //LoadScreen.SetLoadingScreen(LoadingStages.EventModifiers);
             * var (eventModifiers, eventModifierNames) = EventModifierLoad.Main();
             * _stringBox.EventModifierNames = eventModifierNames;
             *
             * //LoadScreen.SetLoadingScreen(LoadingStages.NationalValues);, literally identical to Event Modifiers. Merge or generify?
             * var (nationalValues, nationalValueNames) = NationalValueLoad.Main();
             * _stringBox.NationalValueNames = nationalValueNames;
             *
             * //LoadScreen.SetLoadingScreen(LoadingStages.Inventions);
             * var inventions = InventionsLoad.Main(technologies, ref _stringBox);
             *
             * //LoadScreen.SetLoadingScreen(LoadingStages.Issues);
             * IssuesLoad.Main(issueFileTree, issueValues, subPolicies, policyGroups);
             *
             * //LoadScreen.SetLoadingScreen(LoadingStages.TechnologyNames);
             *
             * InventionsLoad.CompleteInventions(technologies, incompleteInventions);
             *
             * //LoadScreen.SetLoadingScreen(LoadingStages.Histories);
             *
             * var provinces = ProvinceHistoryLoad.Main(_newDataBox.IdIndex);
             * ProvincePopulationLoad.Main(provinces, _newDataBox.IdIndex);
             *
             * var (countries, tempRulingParty) = CountryHistoryLoad.Main(technologies, inventions, cultures,
             *  ideologies, subPolicies, governments);
             *
             * //LoadScreen.SetLoadingScreen(LoadingStages.Countries);
             * var (countryParties, countryPartyNames) = CountriesLoad.Main(countries, countryPaths);
             * _stringBox.CountryPartyNames = countryPartyNames;
             *
             * _pixelJobHandle.Complete();
             * _borderJobHandle.Complete();
             *
             * _uniqueColors.Dispose();
             * _stateLookup.Dispose();
             * _colorMap.Dispose();
             * _terrainMap.Dispose();
             * _terrainLookup.Dispose();
             * _provTerrains.Dispose();
             * _borderStart.Dispose();
             */

            Debug.Log("End: " + Time.realtimeSinceStartup);
            Debug.Log("Duration: " + (Time.realtimeSinceStartup - start));
        }