Actually removed the Threading.Tasks using lines.
[QuantumStrutsContinued.git] /
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// QuantumStrutsContinued © 2014 toadicus
//
// This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. To view a
// copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/
//
// Continued from QuantumStrut, © 2013 BoJaN.  Used with permission.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using KSP.IO;
using UnityEngine;

namespace QuantumStrut
{
	public static class Util
	{
		static System.Random r = new System.Random();

		public static float Random(float min, float max)
		{
			return (float)((r.NextDouble() * (max - min)) + min);
		}

		public static float Random()
		{
			return (float)r.NextDouble();
		}

		public static Part getTank(Part parent)
		{
			if (parent.Resources.Count != 0)
				return parent;

			if (isValid(parent.parent) && parent.fuelCrossFeed)
				return getTank(parent.parent);

			return null;
		}

		public static Vessel Kerbal
		{
			get
			{
				if (isValid(FlightGlobals.fetch.activeVessel) && FlightGlobals.fetch.activeVessel.isEVA)
					return FlightGlobals.fetch.activeVessel;
				return null;
			}
		}

		public static bool isValid(UnityEngine.Object obj)
		{
			return (obj && obj != null);
		}

		public static void printObj(object obj)
		{
			using (KSP.IO.TextWriter writer = TextWriter.CreateForType<string>("obj.cs"))
			{
				string str = printProperties(obj, TypeDescriptor.GetProperties(obj), 0);
				writer.Write(str);
				MonoBehaviour.print(str);
			}
		}

		static string printProperties(object obj, PropertyDescriptorCollection collection, int I)
		{
			if (I > 2)
				return "";

			string prefix = "";
			string str = "";
			for (int T = 0; T < I; T++)
			{
				prefix += "    ";
			}

			foreach (PropertyDescriptor d in collection)
			{
				str += "\n" + prefix + d.Name + " = " + d.GetValue(obj);
			}
			return str;
		}

		public static Part partFromGameObject(GameObject ob)
		{
			GameObject o = ob;

			while (o)
			{
				Part p = Part.FromGO(o);
				if (p && p != null)
				{
					return p;
				}

				if (o.transform.parent)
					o = o.transform.parent.gameObject;
				else
					return null;
			}
			return null;
		}

		public static Part partFromRaycast(RaycastHit hit)
		{
			return partFromGameObject(hit.collider.gameObject);
		}

		public static Part partFromId(Vessel vessel, long id)
		{
			Console.WriteLine("Vessel Parts: " + vessel.Parts.Count);
			foreach (Part p in vessel.Parts)
			{
				MonoBehaviour.print(p.uid + " ?= " + id + ": " + (p.uid == id));
				if (p.uid == id)
					return p;
			}
			return null;
		}

		public static float GetEnergy(Vessel vessel)
		{
			double energy = 0;
			foreach (Part p in vessel.parts)
			{
				foreach (PartResource r in p.Resources)
				{
					if (r.resourceName == "ElectricCharge")
						energy += r.amount;
				}
			}
			return (float)energy;
		}
	}
}