Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Each Cell will have properties to it, all ranging in variable types
- //Example being Cell[CellID].Pressure, and Cell[CellID].isStorming. where pressure is a double, and isStorming is a bool.
- //Cell Startup will be called once, and will attempt to grab all the cell's data from the persistence file.
- //Cell.AtLevel(n) is the grid level, and each step of n multiplies cells by 4
- //CellMaps store data at a specific grid level
- //Note: Do not use Cell.index, use CellMap and CellSet instead.
- //var grid = new CellMap<Something>(5);
- //var humidity = new CellMap<double>(5);
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using UnityEngine;
- using KSP.IO;
- using System.Threading;
- using System.ComponentModel;
- using Temperature;
- using GeodesicGrid;
- namespace Database
- {
- public class KWSCell : MonoBehaviour, IConfigNode
- {
- private BackgroundWorker KWSCellWorker = new BackgroundWorker();
- //Private Variables
- private static CelestialBody body = FlightGlobals.Bodies[0];
- //private string bodyName;
- //private Vector3 cellWindDirTendancy;
- //private static Vector3 cellPosition;
- private static int numberOfCells;
- //private static int numberOfKWSBodies;
- private CelestialBody cellBody;
- private static bool hasGenerated = false;
- //Public Variables
- public bool isCellStorming;
- public bool isCellRaining;
- public bool isCellClouded;
- public bool isCellSnowing;
- public bool isCellDaytime;
- public bool isCellHigherPressure;
- public int CellID;
- public double Pressure;
- public float Temperature;
- public double Humidity;
- public double Latitude;
- public double Longitude;
- public double Altitude;
- public float Density;
- public double Albedo;
- public float WindSpeed;
- public Vector3 WindDirection;
- public Vector3 CellPosition;
- public Vector3 CellPressureGradientAcc;
- public static KWSCell[] Cells = new KWSCell[numberOfCells];
- //public static Array KWSBODY = new Array[numberOfKWSBodies];
- public static Dictionary<CelestialBody, List<KWSCell>> KWSBODY = new Dictionary<CelestialBody, List<KWSCell>>();
- void Awake()
- {
- //Debug.Log("Awake!");
- if (hasGenerated == false) { GenerateNewCells(); }
- if (hasGenerated == false) { GenerateNewGrid(); }
- //KWSCellWorker.WorkerReportsProgress = true;
- //KWSCellWorker.WorkerSupportsCancellation = true;
- //KWSCellWorker.DoWork += UpdateCellData;
- //KWSCellWorker.RunWorkerAsync();
- //GeodesicGrid.Cell.AtLevel(n);
- }
- private void StartThread()
- {
- }
- void DoWork(object sender, DoWorkEventArgs e)
- {
- }
- void FixedUpdate()
- {
- //Debug.Log("Fixed Update");
- UpdateCellData();
- }
- public void Save(ConfigNode node)
- {
- }
- public void Load(ConfigNode node)
- {
- }
- internal static void GenerateNewGrid()
- {
- Debug.Log("Generating New Grid.");
- CellMap<KWSCell> grid = new CellMap<KWSCell>(5);
- }
- internal static void GenerateNewCells()
- {
- int CellID = 0;
- Debug.Log("Generating New Cells!");
- double altitude = 0;
- foreach (CelestialBody body in FlightGlobals.Bodies)
- {
- if (body.atmosphere) //Check if the body has atmosphere
- {
- Debug.Log(body.name + " Has Atmosphere!");
- KWSBODY[body] = new List<KWSCell>();
- ///*
- for (altitude = 0; altitude < 12500; altitude += 2500)
- {
- for (double latitude = -90; latitude <= 90; latitude += Settings.cellDefinitionWidth)//iterate through the latitudes
- {
- for (double longitude = -180; longitude < 180; longitude += (Settings.cellDefinitionWidth))//iterate through the longitudes
- {
- KWSCell newCell = new KWSCell(latitude, longitude, altitude, body);
- newCell.CellID = CellID;
- newCell.Latitude = latitude;
- newCell.Longitude = longitude;
- newCell.Altitude = altitude;
- newCell.cellBody = body;
- newCell.CellPosition = body.GetWorldSurfacePosition(latitude, longitude, altitude);
- newCell.Temperature = FlightGlobals.getExternalTemperature((float)newCell.Altitude, body);
- newCell.Pressure = FlightGlobals.getStaticPressure(newCell.Altitude, body);
- newCell.Density = (101325f * (float)(newCell.Pressure)) / 287.058f * newCell.Temperature;
- //Cells[CellID] = newCell;
- //cells.Add(newCell);
- KWSBODY[body].Add(newCell);
- //Debug.Log(body + ", " + CellID + " : " + KWSBODY[body][CellID].Latitude + ", " + KWSBODY[body][CellID].Longitude + ", " + KWSBODY[body][CellID].Altitude);
- numberOfCells++;
- CellID++;
- }
- }
- //Debug.Log("Cell Altitude: " + altitude);
- //Debug.Log(FlightGlobals.getExternalTemperature((float)altitude, body));
- }
- //*/
- /*
- for (altitude = 0; altitude < body.maxAtmosphereAltitude; altitude += Settings.cellDefinitionAlt )
- {
- for (double latitude = -90; latitude < 90; latitude += Settings.cellDefinitionWidth)//iterate through the latitudes
- {
- for (double longitude = -180; longitude < 180; longitude += (Settings.cellDefinitionWidth))//iterate through the longitudes
- {
- Cell newCell = new Cell(latitude, longitude, altitude);
- newCell.Latitude = latitude;
- newCell.Longitude = longitude;
- newCell.Altitude = altitude;
- newCell.cellBody = body;
- //Cells[CellID] = newCell;
- //cells.Add(newCell);
- KWSBODY[body].Add(newCell);
- //Debug.Log(body + ", " + CellID + " : " + KWSBODY[body][CellID].Latitude + ", " + KWSBODY[body][CellID].Longitude + ", " + KWSBODY[body][CellID].Altitude);
- numberOfCells++;
- CellID++;
- }
- }
- }
- */
- /*
- for (double latitude = -90; latitude < 90; latitude += Settings.cellDefinitionWidth)//iterate through the latitudes
- {
- for (double longitude = -180; longitude < 180; longitude += (Settings.cellDefinitionWidth))//iterate through the longitudes
- {
- Cell newCell = new Cell(latitude, longitude, altitude);
- newCell.Latitude = latitude;
- newCell.Longitude = longitude;
- newCell.Altitude = altitude;
- newCell.cellBody = body;
- //Cells[CellID] = newCell;
- //cells.Add(newCell);
- KWSBODY[body].Add(newCell);
- //Debug.Log(body + ", " + CellID + " : " + KWSBODY[body][CellID].Latitude + ", " + KWSBODY[body][CellID].Longitude + ", " + KWSBODY[body][CellID].Altitude);
- numberOfCells++;
- CellID++;
- }
- }
- */
- CellID = 0;
- numberOfCells = 0;
- altitude = 0;
- //if (altitude < body.maxAtmosphereAltitude) { altitude += Settings.cellDefinitionAlt; }//Up the altitude so that a new layer of cells can be created
- //else { CellID = 0; numberOfCells = 0; altitude = 0; }
- }
- else
- {
- Debug.Log(body.name + " Doesn't Have Atmosphere!");
- CellID = 0;
- numberOfCells = 0;
- altitude = 0;
- }
- }
- hasGenerated = true;
- }
- public KWSCell(double latitude, double longitude, double altitude, CelestialBody body)
- {
- CellPosition = body.GetWorldSurfacePosition(latitude, longitude, altitude);
- Temperature = FlightGlobals.getExternalTemperature(CellPosition);
- Pressure = FlightGlobals.getStaticPressure(CellPosition);
- Altitude = altitude;
- }
- public void UpdateCellData()
- {
- ///Debug.Log("Updating Kerbin's Cell Data!");
- //Update Kerbin's cells.
- /*
- foreach (KWSCell newCell in KWSBODY[FlightGlobals.Bodies[1]])
- {
- newCell.Albedo = updateCellAlbedo(body, CellID);
- newCell.Temperature = (float)Heating.UpdateCellTemperature(body, CellID);
- newCell.Density = (101325f * (float)(newCell.Pressure)) / 287.058f * newCell.Temperature;
- newCell.WindDirection = WeatherDatabase.getCellPressureGradientVector(body, CellID);
- }
- */
- foreach(KeyValuePair<CelestialBody, List<KWSCell>> kvp in KWSBODY)
- {
- foreach(KWSCell newCell in kvp.Value)
- {
- body = newCell.cellBody;
- newCell.Albedo = updateCellAlbedo(body, CellID);
- newCell.Temperature = (float)Heating.UpdateCellTemperature(body, CellID);
- newCell.Density = (101325f * (float)(newCell.Pressure)) / 287.058f * newCell.Temperature;
- newCell.WindDirection = WeatherDatabase.getCellPressureGradientVector(body, CellID);
- }
- }
- }
- public void UpdateCellData(object sender, DoWorkEventArgs args)
- {
- //Update Kerbin's cells.
- foreach(KWSCell newCell in KWSBODY[FlightGlobals.Bodies[1]])
- {
- body = newCell.cellBody;
- newCell.Albedo = updateCellAlbedo(body, CellID);
- newCell.Temperature = (float)Heating.UpdateCellTemperature(body, CellID);
- newCell.Density = (101325f * (float)(newCell.Pressure)) / 287.058f * newCell.Temperature;
- newCell.WindDirection = WeatherDatabase.getCellPressureGradientVector(body, CellID);
- }
- }
- public double updateCellAlbedo(CelestialBody body, int CellID)
- {
- //Values only valid for kerbin...
- if (KWSBODY[body][CellID].isCellClouded) { Albedo = 0.35; }
- else { Albedo = 0.3; }
- return Albedo;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement