-
Notifications
You must be signed in to change notification settings - Fork 13
/
GridTurrets.cs
187 lines (171 loc) · 6.29 KB
/
GridTurrets.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
// From Equinox: https://gist.github.com/Equinox-/430ce7ac39d1cc0b50941e99960bcadd
using System;
using System.Collections.Generic;
using Sandbox.Common.ObjectBuilders;
using Sandbox.Definitions;
using Sandbox.Game.Entities;
using Sandbox.Game.Weapons;
using VRage;
using VRage.Game;
using VRage.Game.ModAPI;
using VRage.ModAPI;
using VRage.ObjectBuilders;
public class GridTurrets : IDisposable
{
private readonly IMyCubeGrid _grid;
private readonly Dictionary<MyWeaponDefinition, MyObjectBuilder_PhysicalObject[]> _ammoTypes;
private readonly List<IMyGunBaseUser> _turretsForUpdate;
private readonly List<IMyGunBaseUser> _turretsSleeping;
public GridTurrets(IMyCubeGrid grid)
{
_ammoTypes = new Dictionary<MyWeaponDefinition, MyObjectBuilder_PhysicalObject[]>();
_turretsForUpdate = new List<IMyGunBaseUser>();
_turretsSleeping = new List<IMyGunBaseUser>();
_grid = grid;
_grid.OnBlockAdded += OnBlockAdded;
_grid.OnBlockRemoved += OnBlockRemoved;
}
private void OnBlockAdded(IMySlimBlock obj)
{
var gun = obj?.FatBlock as IMyGunBaseUser;
if (gun == null)
return;
Register(gun);
}
private void OnBlockRemoved(IMySlimBlock obj)
{
var gun = obj?.FatBlock as IMyGunBaseUser;
if (gun == null)
return;
Unregister(gun);
}
private void Register(IMyGunBaseUser gun)
{
var block = (IMyCubeBlock)gun;
var weapon = WeaponShortcuts.GetWeaponDefinition(block);
if (weapon == null || !weapon.HasAmmoMagazines())
return;
block.IsWorkingChanged += Block_IsWorkingChanged;
Block_IsWorkingChanged(block);
}
private void Unregister(IMyGunBaseUser gun, bool remove = true)
{
var block = (IMyCubeBlock)gun;
block.IsWorkingChanged -= Block_IsWorkingChanged;
if (remove)
{
_turretsForUpdate.Remove(gun);
_turretsSleeping.Remove(gun);
}
}
private void Block_IsWorkingChanged(IMyCubeBlock obj)
{
var gun = obj as IMyGunBaseUser;
_turretsForUpdate.Remove(gun);
_turretsSleeping.Remove(gun);
if (obj.IsWorking)
_turretsForUpdate.Add(gun);
else
_turretsSleeping.Add(gun);
}
private static readonly MyFixedPoint _addAmount = 50;
private static readonly MyFixedPoint _thresholdAmount = 25;
private int _updateId;
public void Update(int spread = 1)
{
for (var i = _updateId; i < _turretsForUpdate.Count; i += spread)
{
var target = _turretsForUpdate[i];
var inv = target.AmmoInventory;
if (inv == null)
continue;
var entity = (IMyEntity)target;
var weapon = WeaponShortcuts.GetWeaponDefinition(entity);
if (weapon == null || !weapon.HasAmmoMagazines())
continue;
MyObjectBuilder_PhysicalObject[] ammoMags;
if (!_ammoTypes.TryGetValue(weapon, out ammoMags))
{
ammoMags = new MyObjectBuilder_PhysicalObject[weapon.AmmoMagazinesId.Length];
for (var j = 0; j < ammoMags.Length; j++)
ammoMags[j] = new MyObjectBuilder_AmmoMagazine() { SubtypeName = weapon.AmmoMagazinesId[j].SubtypeName };
_ammoTypes[weapon] = ammoMags;
}
foreach (var mag in ammoMags)
{
if (inv.GetItemAmount(mag.GetObjectId(), mag.Flags) > _thresholdAmount)
continue;
inv.AddItems(_addAmount, mag);
}
}
_updateId = (_updateId + 1) % spread;
}
public void Dispose()
{
_grid.OnBlockAdded -= OnBlockAdded;
_grid.OnBlockRemoved -= OnBlockRemoved;
foreach (var x in _turretsForUpdate)
Unregister(x, false);
_turretsForUpdate.Clear();
foreach (var x in _turretsSleeping)
Unregister(x, false);
_turretsSleeping.Clear();
_ammoTypes.Clear();
}
}
public static class WeaponShortcuts
{
public static MyWeaponDefinition GetWeaponDefinition(IMyEntity ent)
{
try
{
var block = ent as IMyCubeBlock;
if (block != null && ent is IMyGunBaseUser)
{
var def = MyDefinitionManager.Static.GetCubeBlockDefinition(block.BlockDefinition);
var wep = def as MyWeaponBlockDefinition;
if (wep != null)
return MyDefinitionManager.Static.GetWeaponDefinition(wep.WeaponDefinitionId);
return MyDefinitionManager.Static.GetWeaponDefinition(
GetBackwardCompatibleDefinitionId(def.Id.TypeId));
}
var gun = ent as IMyHandheldGunObject<MyToolBase>;
if (gun != null)
{
var def = gun.PhysicalItemDefinition;
var pdef = def as MyWeaponItemDefinition;
if (pdef != null)
return MyDefinitionManager.Static.GetWeaponDefinition(pdef.WeaponDefinitionId);
}
}
catch
{
// ignored
}
return null;
}
private static MyDefinitionId GetBackwardCompatibleDefinitionId(MyObjectBuilderType typeId)
{
if (typeId == typeof(MyObjectBuilder_LargeGatlingTurret))
{
return new MyDefinitionId(typeof(MyObjectBuilder_WeaponDefinition), "LargeGatlingTurret");
}
if (typeId == typeof(MyObjectBuilder_LargeMissileTurret))
{
return new MyDefinitionId(typeof(MyObjectBuilder_WeaponDefinition), "LargeMissileTurret");
}
if (typeId == typeof(MyObjectBuilder_InteriorTurret))
{
return new MyDefinitionId(typeof(MyObjectBuilder_WeaponDefinition), "LargeInteriorTurret");
}
if (typeId == typeof(MyObjectBuilder_SmallMissileLauncher) || typeId == typeof(MyObjectBuilder_SmallMissileLauncherReload))
{
return new MyDefinitionId(typeof(MyObjectBuilder_WeaponDefinition), "SmallMissileLauncher");
}
if (typeId == typeof(MyObjectBuilder_SmallGatlingGun))
{
return new MyDefinitionId(typeof(MyObjectBuilder_WeaponDefinition), "GatlingGun");
}
return default(MyDefinitionId);
}
}