Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.70 KB | None | 0 0
  1. //Each Cell will have properties to it, all ranging in variable types
  2. //Example being Cell[CellID].Pressure, and Cell[CellID].isStorming. where pressure is a double, and isStorming is a bool.
  3. //Cell Startup will be called once, and will attempt to grab all the cell's data from the persistence file.
  4.  
  5. //Cell.AtLevel(n) is the grid level, and each step of n multiplies cells by 4
  6. //CellMaps store data at a specific grid level
  7. //Note: Do not use Cell.index, use CellMap and CellSet instead.
  8. //var grid = new CellMap<Something>(5);
  9. //var humidity = new CellMap<double>(5);
  10.  
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Text;
  15. using UnityEngine;
  16. using KSP.IO;
  17. using System.Threading;
  18. using System.ComponentModel;
  19. using Temperature;
  20. using GeodesicGrid;
  21.  
  22. namespace Database
  23. {
  24.  
  25. public class KWSCell : MonoBehaviour, IConfigNode
  26. {
  27. private BackgroundWorker KWSCellWorker = new BackgroundWorker();
  28.  
  29. //Private Variables
  30. private static CelestialBody body = FlightGlobals.Bodies[0];
  31. //private string bodyName;
  32. //private Vector3 cellWindDirTendancy;
  33. //private static Vector3 cellPosition;
  34. private static int numberOfCells;
  35. //private static int numberOfKWSBodies;
  36. private CelestialBody cellBody;
  37. private static bool hasGenerated = false;
  38.  
  39.  
  40.  
  41. //Public Variables
  42. public bool isCellStorming;
  43. public bool isCellRaining;
  44. public bool isCellClouded;
  45. public bool isCellSnowing;
  46. public bool isCellDaytime;
  47. public bool isCellHigherPressure;
  48.  
  49. public int CellID;
  50.  
  51. public double Pressure;
  52. public float Temperature;
  53. public double Humidity;
  54. public double Latitude;
  55. public double Longitude;
  56. public double Altitude;
  57. public float Density;
  58. public double Albedo;
  59.  
  60. public float WindSpeed;
  61.  
  62. public Vector3 WindDirection;
  63. public Vector3 CellPosition;
  64. public Vector3 CellPressureGradientAcc;
  65.  
  66. public static KWSCell[] Cells = new KWSCell[numberOfCells];
  67. //public static Array KWSBODY = new Array[numberOfKWSBodies];
  68. public static Dictionary<CelestialBody, List<KWSCell>> KWSBODY = new Dictionary<CelestialBody, List<KWSCell>>();
  69.  
  70.  
  71.  
  72. void Awake()
  73. {
  74. //Debug.Log("Awake!");
  75. if (hasGenerated == false) { GenerateNewCells(); }
  76. if (hasGenerated == false) { GenerateNewGrid(); }
  77. //KWSCellWorker.WorkerReportsProgress = true;
  78. //KWSCellWorker.WorkerSupportsCancellation = true;
  79. //KWSCellWorker.DoWork += UpdateCellData;
  80. //KWSCellWorker.RunWorkerAsync();
  81.  
  82. //GeodesicGrid.Cell.AtLevel(n);
  83.  
  84. }
  85. private void StartThread()
  86. {
  87.  
  88. }
  89. void DoWork(object sender, DoWorkEventArgs e)
  90. {
  91.  
  92. }
  93. void FixedUpdate()
  94. {
  95. //Debug.Log("Fixed Update");
  96. UpdateCellData();
  97. }
  98.  
  99. public void Save(ConfigNode node)
  100. {
  101.  
  102. }
  103. public void Load(ConfigNode node)
  104. {
  105.  
  106. }
  107.  
  108. internal static void GenerateNewGrid()
  109. {
  110. Debug.Log("Generating New Grid.");
  111.  
  112. CellMap<KWSCell> grid = new CellMap<KWSCell>(5);
  113.  
  114.  
  115. }
  116.  
  117. internal static void GenerateNewCells()
  118. {
  119. int CellID = 0;
  120. Debug.Log("Generating New Cells!");
  121. double altitude = 0;
  122.  
  123. foreach (CelestialBody body in FlightGlobals.Bodies)
  124. {
  125. if (body.atmosphere) //Check if the body has atmosphere
  126. {
  127. Debug.Log(body.name + " Has Atmosphere!");
  128. KWSBODY[body] = new List<KWSCell>();
  129. ///*
  130. for (altitude = 0; altitude < 12500; altitude += 2500)
  131. {
  132. for (double latitude = -90; latitude <= 90; latitude += Settings.cellDefinitionWidth)//iterate through the latitudes
  133. {
  134. for (double longitude = -180; longitude < 180; longitude += (Settings.cellDefinitionWidth))//iterate through the longitudes
  135. {
  136. KWSCell newCell = new KWSCell(latitude, longitude, altitude, body);
  137. newCell.CellID = CellID;
  138. newCell.Latitude = latitude;
  139. newCell.Longitude = longitude;
  140. newCell.Altitude = altitude;
  141. newCell.cellBody = body;
  142. newCell.CellPosition = body.GetWorldSurfacePosition(latitude, longitude, altitude);
  143. newCell.Temperature = FlightGlobals.getExternalTemperature((float)newCell.Altitude, body);
  144. newCell.Pressure = FlightGlobals.getStaticPressure(newCell.Altitude, body);
  145. newCell.Density = (101325f * (float)(newCell.Pressure)) / 287.058f * newCell.Temperature;
  146. //Cells[CellID] = newCell;
  147. //cells.Add(newCell);
  148. KWSBODY[body].Add(newCell);
  149. //Debug.Log(body + ", " + CellID + " : " + KWSBODY[body][CellID].Latitude + ", " + KWSBODY[body][CellID].Longitude + ", " + KWSBODY[body][CellID].Altitude);
  150. numberOfCells++;
  151. CellID++;
  152.  
  153. }
  154. }
  155. //Debug.Log("Cell Altitude: " + altitude);
  156. //Debug.Log(FlightGlobals.getExternalTemperature((float)altitude, body));
  157. }
  158. //*/
  159. /*
  160. for (altitude = 0; altitude < body.maxAtmosphereAltitude; altitude += Settings.cellDefinitionAlt )
  161. {
  162. for (double latitude = -90; latitude < 90; latitude += Settings.cellDefinitionWidth)//iterate through the latitudes
  163. {
  164. for (double longitude = -180; longitude < 180; longitude += (Settings.cellDefinitionWidth))//iterate through the longitudes
  165. {
  166. Cell newCell = new Cell(latitude, longitude, altitude);
  167. newCell.Latitude = latitude;
  168. newCell.Longitude = longitude;
  169. newCell.Altitude = altitude;
  170. newCell.cellBody = body;
  171.  
  172. //Cells[CellID] = newCell;
  173. //cells.Add(newCell);
  174. KWSBODY[body].Add(newCell);
  175. //Debug.Log(body + ", " + CellID + " : " + KWSBODY[body][CellID].Latitude + ", " + KWSBODY[body][CellID].Longitude + ", " + KWSBODY[body][CellID].Altitude);
  176. numberOfCells++;
  177. CellID++;
  178.  
  179. }
  180. }
  181. }
  182. */
  183. /*
  184. for (double latitude = -90; latitude < 90; latitude += Settings.cellDefinitionWidth)//iterate through the latitudes
  185. {
  186. for (double longitude = -180; longitude < 180; longitude += (Settings.cellDefinitionWidth))//iterate through the longitudes
  187. {
  188. Cell newCell = new Cell(latitude, longitude, altitude);
  189. newCell.Latitude = latitude;
  190. newCell.Longitude = longitude;
  191. newCell.Altitude = altitude;
  192. newCell.cellBody = body;
  193.  
  194. //Cells[CellID] = newCell;
  195. //cells.Add(newCell);
  196. KWSBODY[body].Add(newCell);
  197. //Debug.Log(body + ", " + CellID + " : " + KWSBODY[body][CellID].Latitude + ", " + KWSBODY[body][CellID].Longitude + ", " + KWSBODY[body][CellID].Altitude);
  198. numberOfCells++;
  199. CellID++;
  200.  
  201. }
  202. }
  203. */
  204. CellID = 0;
  205. numberOfCells = 0;
  206. altitude = 0;
  207. //if (altitude < body.maxAtmosphereAltitude) { altitude += Settings.cellDefinitionAlt; }//Up the altitude so that a new layer of cells can be created
  208. //else { CellID = 0; numberOfCells = 0; altitude = 0; }
  209. }
  210. else
  211. {
  212. Debug.Log(body.name + " Doesn't Have Atmosphere!");
  213. CellID = 0;
  214. numberOfCells = 0;
  215. altitude = 0;
  216. }
  217. }
  218.  
  219. hasGenerated = true;
  220. }
  221.  
  222. public KWSCell(double latitude, double longitude, double altitude, CelestialBody body)
  223. {
  224. CellPosition = body.GetWorldSurfacePosition(latitude, longitude, altitude);
  225. Temperature = FlightGlobals.getExternalTemperature(CellPosition);
  226. Pressure = FlightGlobals.getStaticPressure(CellPosition);
  227. Altitude = altitude;
  228. }
  229.  
  230. public void UpdateCellData()
  231. {
  232. ///Debug.Log("Updating Kerbin's Cell Data!");
  233. //Update Kerbin's cells.
  234. /*
  235. foreach (KWSCell newCell in KWSBODY[FlightGlobals.Bodies[1]])
  236. {
  237. newCell.Albedo = updateCellAlbedo(body, CellID);
  238. newCell.Temperature = (float)Heating.UpdateCellTemperature(body, CellID);
  239. newCell.Density = (101325f * (float)(newCell.Pressure)) / 287.058f * newCell.Temperature;
  240. newCell.WindDirection = WeatherDatabase.getCellPressureGradientVector(body, CellID);
  241. }
  242. */
  243.  
  244. foreach(KeyValuePair<CelestialBody, List<KWSCell>> kvp in KWSBODY)
  245. {
  246. foreach(KWSCell newCell in kvp.Value)
  247. {
  248. body = newCell.cellBody;
  249. newCell.Albedo = updateCellAlbedo(body, CellID);
  250. newCell.Temperature = (float)Heating.UpdateCellTemperature(body, CellID);
  251. newCell.Density = (101325f * (float)(newCell.Pressure)) / 287.058f * newCell.Temperature;
  252. newCell.WindDirection = WeatherDatabase.getCellPressureGradientVector(body, CellID);
  253. }
  254.  
  255. }
  256. }
  257.  
  258. public void UpdateCellData(object sender, DoWorkEventArgs args)
  259. {
  260. //Update Kerbin's cells.
  261. foreach(KWSCell newCell in KWSBODY[FlightGlobals.Bodies[1]])
  262. {
  263. body = newCell.cellBody;
  264. newCell.Albedo = updateCellAlbedo(body, CellID);
  265. newCell.Temperature = (float)Heating.UpdateCellTemperature(body, CellID);
  266. newCell.Density = (101325f * (float)(newCell.Pressure)) / 287.058f * newCell.Temperature;
  267. newCell.WindDirection = WeatherDatabase.getCellPressureGradientVector(body, CellID);
  268. }
  269. }
  270.  
  271. public double updateCellAlbedo(CelestialBody body, int CellID)
  272. {
  273. //Values only valid for kerbin...
  274. if (KWSBODY[body][CellID].isCellClouded) { Albedo = 0.35; }
  275. else { Albedo = 0.3; }
  276.  
  277.  
  278. return Albedo;
  279. }
  280.  
  281. }
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement