private static void SetOne(Player player, MaterialState newBuilding)
        {
            var origMap  = player.factoryController.curMapID;
            var toDelete = newBuilding.ToDelete;

            Log($"Setting {newBuilding} to delete {string.Join(", ", toDelete)}");
            var currentMap   = 0;
            var maxEfficency = 0.0;
            var bestMap      = 0;
            var bestIndex    = -1;

            foreach (var map in player.factoryData.maps)
            {
                //Log($"Map unlocked?: {map.unlocked}");
                if (map.unlocked)
                {
                    player.factoryController.setNewMapID(currentMap);

                    var empty = player.factoryController.getAllEmptyTileIndexes(currentMap);

                    empty.ForEach(index => IsBetter(BuildingType.None, index));

                    foreach (var building in map.buildings)
                    {
                        var buildingType = building.building;

                        if (toDelete.Contains(buildingType))
                        {
                            IsBetter(buildingType, building.index);
                        }
                    }
                }
                currentMap++;
            }
            if (bestIndex != -1)
            {
                player.factoryController.setNewMapID(bestMap);
                Log($"DELETE {bestIndex} and setting {newBuilding} value {maxEfficency}");
                player.factoryController.doSetTile(bestIndex, newBuilding.BuildingType, TileDirection.Up);
            }
            player.factoryController.setNewMapID(origMap);

            void IsBetter(BuildingType buildingType, int index)
            {
                var production = player.factoryController.totalTileProductionMultiplier(currentMap, index, newBuilding.BuildingType);
                var testSpeed  = player.factoryController.totalTileSpeed(currentMap, index, BuildingType.CandyJuice4);
                var speed      = player.factoryController.totalTileSpeed(currentMap, index, newBuilding.BuildingType);
                var efficency  = production * speed;
                var realTime   = newBuilding.BaseTime / speed;

                // we preffer empty places
                if (buildingType == BuildingType.None)
                {
                    efficency *= 100;
                }

                // We are not properly using speed beacon, lets penalize it
                if (testSpeed > 1 && realTime == newBuilding.MinTime)
                {
                    efficency /= 100;
                }

                if (efficency > maxEfficency)
                {
                    Log($"Building?: {buildingType} {index} ===> {production}*{speed} (real time: {realTime} == min time: {newBuilding.MinTime})= {efficency} (test speed {testSpeed})");
                    maxEfficency = efficency;
                    bestMap      = currentMap;
                    bestIndex    = index;
                }
            }
        }
        public void Start()
        {
            try
            {
                _dir = Path.Combine(Environment.ExpandEnvironmentVariables("%userprofile%/Desktop"), "NGUIndustriesInjector");
                if (!Directory.Exists(_dir))
                {
                    Directory.CreateDirectory(_dir);
                }

                var logDir = Path.Combine(_dir, "logs");
                if (!Directory.Exists(logDir))
                {
                    Directory.CreateDirectory(logDir);
                }

                OutputWriter = new StreamWriter(Path.Combine(logDir, "inject.log"))
                {
                    AutoFlush = true
                };
                DebugWriter = new StreamWriter(Path.Combine(logDir, "debug.log"))
                {
                    AutoFlush = true
                };
                LootWriter = new StreamWriter(Path.Combine(logDir, "loot.log"))
                {
                    AutoFlush = true
                };
                CombatWriter = new StreamWriter(Path.Combine(logDir, "combat.log"))
                {
                    AutoFlush = true
                };
                PitSpinWriter = new StreamWriter(Path.Combine(logDir, "pitspin.log"), true)
                {
                    AutoFlush = true
                };

                var _profilesDir = Path.Combine(_dir, "profiles");
                if (!Directory.Exists(_profilesDir))
                {
                    Directory.CreateDirectory(_profilesDir);
                }

                var oldPath = Path.Combine(_dir, "allocation.json");
                var newPath = Path.Combine(_profilesDir, "default.json");

                if (File.Exists(oldPath) && !File.Exists(newPath))
                {
                    File.Move(oldPath, newPath);
                }
                SetUpDDLLoading();
            }
            catch (Exception e)
            {
                MessageBox.Show($"Loader intialization error: {e.Message}");
                Log(e.Message);
                Log(e.StackTrace);
                Loader.Unload();
                return;
            }

            try
            {
                Log("Injected");
                LogLoot("Starting Loot Writer");
                LogCombat("Starting Combat Writer");

                Settings     = new SavedSettings(_dir);
                SettingsForm = new SettingsForm();

                ConfigWatcher = new FileSystemWatcher
                {
                    Path                = _dir,
                    Filter              = "settings.json",
                    NotifyFilter        = NotifyFilters.LastWrite,
                    EnableRaisingEvents = true
                };

                ConfigWatcher.Changed += (sender, args) =>
                {
                    if (IgnoreNextChange)
                    {
                        IgnoreNextChange = false;
                        return;
                    }
                    Settings.LoadSettings();
                    SettingsForm.UpdateFromSettings(Settings);
                };

                Settings.LoadSettings();
                SettingsForm.UpdateFromSettings(Settings);
                SettingsForm.Show();

                InvokeRepeating("AutomationRoutine", 1.0f, MAIN_DELAY);
                InvokeRepeating("SnipeZone", 0.0f, .1f);
                InvokeRepeating("MonitorLog", 0.0f, 1f);
                InvokeRepeating("QuickStuff", 0.0f, .5f);
                InvokeRepeating("ShowBoostProgress", 0.0f, 60.0f);
                InvokeRepeating("SetResnipe", 0f, 1f);

                Reference = this;

                materialState = new List <MaterialState>();
                MaterialState.SetState(materialState, FindObjectOfType <Player>());
                foreach (var buildingType in Enum.GetValues(typeof(BuildingType)).Cast <BuildingType>())
                {
                    materialState.Add(new MaterialState(buildingType));
                }
            }
            catch (Exception e)
            {
                MessageBox.Show($"Loader startup error: {e.Message}");
                Log(e.ToString());
                Log(e.StackTrace);
                Log(e.InnerException.ToString());
            }
        }