-
Notifications
You must be signed in to change notification settings - Fork 1
/
SeDropshipAsteroids.cs
486 lines (435 loc) · 11.1 KB
/
SeDropshipAsteroids.cs
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
using System;
using System.Text;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
//using SEModAPIInternal.API.Entity.Sector.SectorObject;
using Sandbox.ModAPI;
using Sandbox.Definitions;
using Sandbox.Common.ObjectBuilders.Voxels;
using VRageMath;
using VRage.Voxels;
namespace SEDropship
{
public class AsteroidCollection : CollectionBase, ICustomTypeDescriptor, IList
{
public void Add( SeDropshipAsteroids roid )
{
this.List.Add( roid );
}
public void Remove( SeDropshipAsteroids roid )
{
this.List.Remove( roid);
}
public SeDropshipAsteroids this[ int index ]
{
get
{
return (SeDropshipAsteroids)this.List[index];
}
}
public SeDropshipAsteroids First()
{
return this[0];
}
public SeDropshipAsteroids ElementAt(int index)
{
return this[index];
}
public String GetClassName()
{
return TypeDescriptor.GetClassName(this,true);
}
public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(this,true);
}
public String GetComponentName()
{
return TypeDescriptor.GetComponentName(this, true);
}
public TypeConverter GetConverter()
{
return TypeDescriptor.GetConverter(this, true);
}
public EventDescriptor GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this, true);
}
public PropertyDescriptor GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(this, true);
}
public object GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(this, editorBaseType, true);
}
public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this, attributes, true);
}
public EventDescriptorCollection GetEvents()
{
return TypeDescriptor.GetEvents(this, true);
}
public object GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
return GetProperties();
}
public PropertyDescriptorCollection GetProperties()
{
// Create a collection object to hold property descriptors
PropertyDescriptorCollection pds = new PropertyDescriptorCollection(null);
for( int i=0; i<this.List.Count; i++ )
{
AsteroidCollectionPropertyDescriptor pd = new AsteroidCollectionPropertyDescriptor(this,i);
pds.Add(pd);
}
// return the property descriptor collection
return pds;
}
}
public class AsteroidCollectionPropertyDescriptor : PropertyDescriptor
{
private AsteroidCollection collection = null;
private int index = -1;
public AsteroidCollectionPropertyDescriptor(AsteroidCollection coll, int idx)
: base("#" + idx.ToString(), null)
{
this.collection = coll;
this.index = idx;
}
public override AttributeCollection Attributes
{
get
{
return new AttributeCollection(null);
}
}
public override bool CanResetValue(object component)
{
return true;
}
public override Type ComponentType
{
get
{
return this.collection.GetType();
}
}
public override string DisplayName
{
get
{
SeDropshipAsteroids emp = this.collection[index];
return emp.asteroid.GetFriendlyName();
}
}
public override string Description
{
get
{
SeDropshipAsteroids roid = this.collection[index];
StringBuilder sb = new StringBuilder();
sb.Append(roid.asteroid.GetFriendlyName());
sb.Append(", X:");
sb.Append(roid.x);
sb.Append(", Y:");
sb.Append(roid.y);
sb.Append(", Z:");
sb.Append(roid.z);
return sb.ToString();
}
}
public override object GetValue(object component)
{
return this.collection[index];
}
public override bool IsReadOnly
{
get { return true; }
}
public override string Name
{
get { return "#"+index.ToString(); }
}
public override Type PropertyType
{
get { return this.collection[index].GetType(); }
}
public override void ResetValue(object component) {}
public override bool ShouldSerializeValue(object component)
{
return true;
}
public override void SetValue(object component, object value)
{
}
}
internal class AsteroidsConverter : ExpandableObjectConverter
{
public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture,
object value, Type destType )
{
if (destType == typeof(string) && value is SeDropshipAsteroids)
{
SeDropshipAsteroids asteroids = (SeDropshipAsteroids)value;
return asteroids.asteroid.GetFriendlyName();
}
return base.ConvertTo(context,culture,value,destType);
}
}
[Serializable()]
[TypeConverter(typeof(AsteroidsConverter))]
public class SeDropshipAsteroids
{
private IMyVoxelMap m_asteroid;
private double m_sizex = 50;
private double m_sizey = 50;
private double m_sizez = 50;
private Dictionary<MyVoxelMaterialDefinition, float> m_materialTotals = new Dictionary<MyVoxelMaterialDefinition, float>();
private MyStorageData m_cache;
private List<MyVoxelMaterialDefinition> m_Defs = new List<MyVoxelMaterialDefinition>();
private Vector3D m_position;
private string m_name;
private int m_halfextent;
private static MyVoxelMaterialDefinition m_irondef = MyDefinitionManager.Static.GetVoxelMaterialDefinition("Iron_01");
private static MyVoxelMaterialDefinition m_irondef2 = MyDefinitionManager.Static.GetVoxelMaterialDefinition("Iron_02");
private static MyVoxelMaterialDefinition m_nickeldef1 = MyDefinitionManager.Static.GetVoxelMaterialDefinition("Nickel_01");
private static MyVoxelMaterialDefinition m_cobaltdef1 = MyDefinitionManager.Static.GetVoxelMaterialDefinition("Cobalt_01");
private static MyVoxelMaterialDefinition m_magnesiumdef1 = MyDefinitionManager.Static.GetVoxelMaterialDefinition("Magnesium_01");
private static MyVoxelMaterialDefinition m_silicondef1 = MyDefinitionManager.Static.GetVoxelMaterialDefinition("Silicon_01");
private static MyVoxelMaterialDefinition m_silverdef1 = MyDefinitionManager.Static.GetVoxelMaterialDefinition("Silver_01");
private static MyVoxelMaterialDefinition m_golddef1 = MyDefinitionManager.Static.GetVoxelMaterialDefinition("Gold_01");
private static MyVoxelMaterialDefinition m_platinumdef1 = MyDefinitionManager.Static.GetVoxelMaterialDefinition("Platinum_01");
private static MyVoxelMaterialDefinition m_uraniumdef1 = MyDefinitionManager.Static.GetVoxelMaterialDefinition("Uraninite_01");
private static MyVoxelMaterialDefinition m_icedef1 = MyDefinitionManager.Static.GetVoxelMaterialDefinition("Ice_01");
private static MyVoxelMaterialDefinition m_icedef2 = MyDefinitionManager.Static.GetVoxelMaterialDefinition("Ice_02");
public SeDropshipAsteroids(IMyVoxelMap asteroid)
{
m_asteroid = asteroid;
m_name = m_asteroid.StorageName.ToString();
calculateSize();
}
public SeDropshipAsteroids()
{
}
private void calculateSize()
{
try
{
m_position = m_asteroid.PositionLeftBottomCorner - m_asteroid.Physics.Center;
m_sizex = m_position.X;
m_sizey = m_position.Y;
m_sizez = m_position.Z;
m_halfextent = (int)m_asteroid.LocalAABB.HalfExtents.Length();
}
catch (Exception)
{
Console.WriteLine("Exception thrown when attempting to cache values");
m_sizex = 50;
m_sizey = 50;
m_sizez = 50;
m_halfextent = 128;
return;
}
}
public void Refresh()
{
m_cache = new MyStorageData();
Vector3I size = m_asteroid.Storage.Size;
m_cache.Resize(size);
m_asteroid.Storage.ReadRange(m_cache, MyStorageDataTypeFlags.Material, 0, Vector3I.Zero, size - 1);
//voxelMap.Storage.ReadRange(m_cache, MyStorageDataTypeFlags.Material, Vector3I.Zero, size - 1);
// });
foreach (byte materialIndex in m_cache.Data)
{
try
{
MyVoxelMaterialDefinition material = MyDefinitionManager.Static.GetVoxelMaterialDefinition(materialIndex);
if (material == null)
continue;
if (!m_materialTotals.ContainsKey(material))
{
m_materialTotals.Add(material, 1);
m_Defs.Add(material);
}
else
m_materialTotals[material]++;
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
[Browsable(true)]
[ReadOnly(true)]
public IMyVoxelMap asteroid
{
get { return m_asteroid; }
}
[Browsable(true)]
[ReadOnly(true)]
public double x
{
get { return m_sizex; }
}
[Browsable(true)]
[ReadOnly(true)]
public double y
{
get { return m_sizey; }
}
[Browsable(true)]
[ReadOnly(true)]
public double z
{
get { return m_sizez; }
}
[Browsable(true)]
[ReadOnly(true)]
public string Name
{
get
{
if (m_name != null)
return m_name;
if (m_asteroid != null)
return m_name = m_asteroid.StorageName.ToString();
return null;
}
}
[Browsable(true)]
[ReadOnly(true)]
public bool has_iron
{
get
{
if (m_cache != null)
return m_Defs.Contains(m_irondef) || m_Defs.Contains(m_irondef2);
return false;
}
}
[Browsable(true)]
[ReadOnly(true)]
public bool has_cobalt
{
get
{
if (m_cache != null)
return m_Defs.Contains(m_cobaltdef1);
return false;
}
}
[Browsable(true)]
[ReadOnly(true)]
public bool has_nickel
{
get
{
if (m_cache != null)
return m_Defs.Contains(m_nickeldef1);
return false;
}
}
[Browsable(true)]
[ReadOnly(true)]
public bool has_magnesium
{
get
{
if (m_cache != null)
return m_Defs.Contains(m_magnesiumdef1);
return false;
}
}
[Browsable(true)]
[ReadOnly(true)]
public bool has_silicon
{
get
{
if (m_cache != null)
return m_Defs.Contains(m_silicondef1);
return false;
}
}
[Browsable(true)]
[ReadOnly(true)]
public bool has_silver
{
get
{
if (m_cache != null)
return m_Defs.Contains(m_silverdef1);
return false;
}
}
[Browsable(true)]
[ReadOnly(true)]
public bool has_gold
{
get
{
if (m_cache != null)
return m_Defs.Contains(m_golddef1);
return false;
}
}
[Browsable(true)]
[ReadOnly(true)]
public bool has_platinum
{
get
{
if (m_cache != null)
return m_Defs.Contains(m_platinumdef1);
return false;
}
}
[Browsable(true)]
[ReadOnly(true)]
public bool has_uranium
{
get
{
if (m_cache != null)
return m_Defs.Contains(m_uraniumdef1);
return false;
}
}
[Browsable(true)]
[ReadOnly(true)]
public bool has_ice
{
get
{
if (m_cache != null)
return m_Defs.Contains(m_icedef1) || m_Defs.Contains(m_icedef2);
return false;
}
}
[Browsable(true)]
[ReadOnly(true)]
public Vector3D center
{
get
{
return m_position;
}
}
public int halfextent
{
get
{
return m_halfextent;
}
}
}
}