Ejemplo n.º 1
0
        private Bitmap loopMatrix(Bitmap source, ProcessPixel process)
        {
            var res = (Bitmap)source.Clone();

            for (var x = 0; x < source.Width; x++)
            {
                for (var y = 0; y < source.Height; y++)
                {
                    res.SetPixel(x, y, process(x, y));
                }
            }
            return(res);
        }
Ejemplo n.º 2
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;
             * }
             */
        }