public static bool isSunlight(PlanetSimulator pSim, int AltLayer, Cell cell)
 {
     if (Heating.getSunlightAngle(pSim, AltLayer, cell) > 90)
     {
         return(false);
     }
     else
     {
         return(true);
     }
 }
        public WeatherCell UpdateWeatherCell(int AltLayer, Cell cell, WeatherCell wcell)
        {
            wcell.Temperature = Heating.CalculateTemperature(this, AltLayer, cell);

            float TLR;

            if (AltLayer + 1 > LiveMap.Count - 1)
            {
                TLR = (2.7f - wcell.Temperature);
            }
            else
            {
                TLR = (LiveMap[AltLayer + 1][cell].Temperature - wcell.Temperature);
            }

            //wcell.Temperature = RandomTemperature(AltLayer, cell);
            //wcell.Temperature = 273.15f + sunAngleCallback(sunDir,AltLayer, cell);

            //wcell.Temperature = LiveMap[AltLayer][cell].Temperature + 1;

            //For pressure and density we need the base pressures for the layer... we need to get those...
            //the static of 101325 is going to f**k with calcs tbh.
            if (AltLayer + 1 > LiveMap.Count - 1)
            {
                //wcell.Pressure = WeatherFunctions.calculatePressure(LiveMap[AltLayer][cell].Pressure, TLR, wcell.Temperature, wcell.Altitude,
                //((wcell.Altitude + 2500) - wcell.Altitude), geeASL, MMOA);

                //wcell.Density = WeatherFunctions.calculateDensity(1.235f, TLR, wcell.Temperature, wcell.Altitude,
                //((wcell.Altitude + 2500) - wcell.Altitude), geeASL, MMOA);
            }
            else
            {
                //wcell.Pressure = WeatherFunctions.calculatePressure(LiveMap[AltLayer][cell].Pressure, TLR, wcell.Temperature, wcell.Altitude,
                //((LiveMap[AltLayer +1][cell].Altitude) - wcell.Altitude), geeASL, MMOA);

                //wcell.Density = WeatherFunctions.calculateDensity(1.235f, TLR, wcell.Temperature, wcell.Altitude,
                //((LiveMap[AltLayer+1][cell].Altitude) - wcell.Altitude), geeASL, MMOA);
            }
            //wcell.Pressure = GenerateRandomPressure(cell);
            wcell.Pressure = WeatherFunctions.newCalculatePressure(this, AltLayer, cell);
            //if (wcell.Pressure == 0) { wcell.Pressure = 101325f; }

            wcell.WindDirection = WeatherFunctions.CalculateWindVector(this, AltLayer, cell);

            //if (cell.Index == 10 && AltLayer == 0) { Debug.Log("Albedo: " + wcell.Albedo); }
            //if (cell.Index == 10 && AltLayer == 0) { Debug.Log("Temperature: " + wcell.Temperature); }
            //if (cell.Index == 10 && AltLayer == 0) { Debug.Log("Pressure: " + wcell.Pressure); }
            //if (cell.Index == 10 && AltLayer == 0) { Debug.Log("Density: " + wcell.Density); }
            //if (cell.Index == 10 && AltLayer == 0) { Debug.Log("Emissivity: " + wcell.Emissivity); }
            //if (cell.Index == 10 && AltLayer == 0) { Debug.Log("SunAngle: " + Heating.getSunlightAngle(this, AltLayer, cell)); }

            return(wcell);
        }
        public void Generate(int gridLevel, int Layers)
        {
            level     = gridLevel;
            LiveMap   = new List <CellMap <WeatherCell> >();
            BufferMap = new List <CellMap <WeatherCell> >();

            //Debug.Log("Layers: " + Layers);

            for (int AltLayer = 0; AltLayer < Layers; AltLayer++)
            {
                //Debug.Log("I'm currently on layer: " + AltLayer);
                CellMap <WeatherCell> buffer  = new CellMap <WeatherCell>(gridLevel);
                CellMap <WeatherCell> buffer2 = new CellMap <WeatherCell>(gridLevel);
                foreach (Cell cell in Cell.AtLevel(gridLevel))
                {
                    WeatherCell temp = new WeatherCell();
                    temp          = WeatherCell.GetDefaultWeatherCell();
                    temp.Altitude = AltLayer * 2500;
                    int rnd = Random.Range(0, 2);
                    if (rnd == 0)
                    {
                        temp.Clouded = false;
                    }
                    else
                    {
                        temp.Clouded = true;
                    }
                    temp.Albedo      = Heating.calculateAlbedo(this, AltLayer, cell);
                    temp.Pressure    = 101325f;
                    temp.Temperature = 19.45f + 273.15f;

                    buffer[cell]  = temp;
                    buffer2[cell] = temp;
                }
                LiveMap.Add(buffer);
                BufferMap.Add(buffer2);
                //Debug.Log("Finished adding layer: " + AltLayer);
            }
            LateInit();
        }
        void LateInit()
        {
            Debug.Log("LateInit!");


            for (int AltLayer = 0; AltLayer < LiveMap.Count; AltLayer++)
            {
                foreach (Cell cell in Cell.AtLevel(level))
                {
                    WeatherCell temp = LiveMap[AltLayer][cell];

                    temp.Emissivity           = Heating.calculateEmissivity(this, AltLayer, cell);
                    temp.Transmissivity       = Heating.calculateTransmissivity(this, AltLayer, cell);
                    LiveMap[AltLayer][cell]   = temp;
                    BufferMap[AltLayer][cell] = temp;
                }
            }

            foreach (Cell cell in Cell.AtLevel(level))
            {
                Heating.InitShortwaves(this, cell);
                Heating.InitLongwaves(this, cell);
            }
        }
        public void UpdateNCells(int CellsToUpdate)
        {
            CellsToUpdate = (int)Math.Min(Cell.CountAtLevel(level), currentIndex + CellsToUpdate);
            WeatherCell[] wCellColumn = new WeatherCell[LiveMap.Count];

            while (currentIndex < CellsToUpdate)
            {
                //Debug.Log(currentIndex + ", " + Cell.CountAtLevel(level));
                Cell cell = new Cell((uint)currentIndex);
                for (int i = LiveMap.Count - 1; i >= 0; i--)
                {
                    wCellColumn[i] = BufferMap[i][cell];
                }
                Heating.CalculateShortwaves(this, cell, wCellColumn);
                Heating.CalculateLongwaves(this, cell, wCellColumn);
                for (int i = LiveMap.Count - 1; i >= 0; i--)
                {
                    BufferMap[i][cell] = wCellColumn[i];
                }
                for (int AltLayer = 0; AltLayer < LiveMap.Count; AltLayer++)
                {
                    //Debug.Log("Currently Updating Cell: "+ currentIndex);
                    WeatherCell temp = wCellColumn[AltLayer];
                    //temp = UpdateWeatherCell(AltLayer, cell, BufferMap[AltLayer][cell]);
                    BufferMap[AltLayer][cell] = UpdateWeatherCell(AltLayer, cell, temp);
                    //BufferMap[AltLayer][cell] = temp;

                    //BufferMap[AltLayer][cell] = UpdateWeatherCell(AltLayer, cell, temp);
                    //LiveMap[AltLayer][cell] = UpdateWeatherCell(AltLayer, cell, BufferMap[AltLayer][cell]);
                    //temp = BufferMap[AltLayer][cell];
                    //LiveMap[AltLayer][cell] = UpdateWeatherCell(AltLayer, cell, temp);
                    //LiveMap[AltLayer][cell] = UpdateWeatherCell(AltLayer, cell, BufferMap[AltLayer][cell]);
                    //LiveMap[AltLayer][cell] = temp;
                    //LiveMap[AltLayer][cell] = BufferMap[AltLayer][cell];
                }
                currentIndex++;
            }



            if (currentIndex >= (int)Cell.CountAtLevel(level) - 1)
            {
                //Don't Worry, it makes sense. Trust me.
                Debug.Log("Resetting Index!");
                if (timesLeft > 0)
                {
                    timesLeft--;
                }

                /*
                 * for (int AltLayer = 0; AltLayer < LiveMap.Count - 1; AltLayer++ )
                 * {
                 *  foreach (Cell cell in Cell.AtLevel(level))
                 *  {
                 *      LiveMap[AltLayer][cell] = UpdateWeatherCell(AltLayer, cell, BufferMap[AltLayer][cell]);
                 *  }
                 * }
                 * //*/

                ///*
                List <CellMap <WeatherCell> > temp = LiveMap;
                LiveMap   = BufferMap;
                BufferMap = temp;
                //*/
                //LiveMap = BufferMap;
                //BufferMap = LiveMap;
                currentIndex = 0;

                sunDir = sunCallback();
                bufferFlip();
            }
        }