Skip to content
This repository was archived by the owner on Jun 23, 2023. It is now read-only.

Commit 70033d1

Browse files
committed
Performance improvement in turret management
1 parent 24e7457 commit 70033d1

File tree

3 files changed

+35
-45
lines changed

3 files changed

+35
-45
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
//289
1+
//291
22
//
33
// This code was generated by a tool. Any changes made manually will be lost
44
// the next time this code is regenerated.
55
//
66

77
using System.Reflection;
88

9-
[assembly: AssemblyFileVersion("1.13.7.289")]
10-
[assembly: AssemblyVersion("1.13.7.289")]
9+
[assembly: AssemblyFileVersion("1.13.7.291")]
10+
[assembly: AssemblyVersion("1.13.7.291")]

EssentialsPlugin/EntityManagers/TurretManagement.cs

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,23 @@ public static TurretManagement Instance
2727
}
2828
}
2929

30+
private List<MyEntity> _entityCache = new List<MyEntity>();
31+
3032
public void ProcessTurrets( )
3133
{
32-
List<MyEntity> entities = new List<MyEntity>( );
33-
Wrapper.GameAction( ( ) => entities = MyEntities.GetEntities( ).ToList( ) );
34-
35-
foreach (MyEntity entity in entities)
36-
{
37-
MyCubeGrid grid = entity as MyCubeGrid;
38-
if (grid == null)
39-
continue;
40-
41-
if (DoesGridHaveTarget( grid ))
42-
EnableTurrets( grid );
43-
else
44-
DisableTurrets( grid );
45-
}
34+
Wrapper.GameAction( ( ) => _entityCache = MyEntities.GetEntities( ).ToList( ) );
35+
36+
Parallel.ForEach( _entityCache, entity =>
37+
{
38+
MyCubeGrid grid = entity as MyCubeGrid;
39+
if (grid == null)
40+
return;
41+
42+
if (DoesGridHaveTarget( grid ))
43+
EnableTurrets( grid );
44+
else
45+
DisableTurrets( grid );
46+
} );
4647
}
4748

4849
public void EnableAllTurrets( )
@@ -76,12 +77,12 @@ private void DisableTurrets( MyCubeGrid grid )
7677
if (turret == null)
7778
continue;
7879

79-
if (!turret.Enabled)
80-
continue;
81-
82-
turret.Enabled = false;
80+
//if (!turret.Enabled)
81+
// continue;
82+
//
83+
//turret.Enabled = false;
8384
MyEntities.UnregisterForUpdate( turret );
84-
count++;
85+
//count++;
8586
}
8687
} );
8788
if (count > 0)
@@ -101,12 +102,12 @@ private void EnableTurrets( MyCubeGrid grid )
101102
if (turret == null)
102103
continue;
103104

104-
if (turret.Enabled)
105-
continue;
106-
107-
turret.Enabled = true;
105+
//if (turret.Enabled)
106+
// continue;
107+
//
108+
//turret.Enabled = true;
108109
MyEntities.RegisterForUpdate( turret );
109-
count++;
110+
//count++;
110111
}
111112
} );
112113
if (count > 0)
@@ -117,36 +118,28 @@ private bool DoesGridHaveTarget( MyCubeGrid grid )
117118
{
118119
HashSet<MySlimBlock> blocks = grid.GetBlocks( );
119120
BoundingSphereD sphere = new BoundingSphereD( Vector3D.Zero, 0 );
120-
bool hasTurrets = false;
121121
foreach (MySlimBlock block in blocks)
122122
{
123123
MyLargeTurretBase turret = block.FatBlock as MyLargeTurretBase;
124124
if (turret == null)
125125
continue;
126126

127-
hasTurrets = true;
128-
129127
BoundingSphereD gunSphere = new BoundingSphereD( turret.PositionComp.GetPosition( ), turret.ShootingRange * 2 );
130128
if (sphere.Radius == 0)
131129
sphere = gunSphere;
132130
else
133131
sphere.Include( gunSphere );
134132
}
135133

136-
if (!hasTurrets)
134+
if (sphere.Radius == 0)
137135
return false;
138136

139-
MyEntity[] targets;
140-
try
141-
{
142-
var list = new List<MyEntity>( );
143-
Wrapper.GameAction( ( ) => list = MyEntities.GetTopMostEntitiesInSphere( ref sphere ) );
144-
targets = list.ToArray( );
145-
}
146-
catch (Exception ex)
137+
var targets = new HashSet<MyEntity>();
138+
139+
foreach (var ent in _entityCache)
147140
{
148-
Essentials.Log.Error( ex );
149-
return true;
141+
if (sphere.Contains( ent.PositionComp.WorldVolume ) != ContainmentType.Disjoint)
142+
targets.Add( ent );
150143
}
151144

152145
foreach (MyEntity target in targets)
@@ -200,10 +193,7 @@ private bool DoesGridHaveTarget( MyCubeGrid grid )
200193
break;
201194
}
202195
if (!grid.SmallOwners.Contains( controllingIdentity ))
203-
{
204-
Essentials.Log.Debug( "!" );
205196
return true;
206-
}
207197
break;
208198
default:
209199
throw new ArgumentOutOfRangeException( );

EssentialsPlugin/ProcessHandlers/ProcessTurrets.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class ProcessTurrets : ProcessHandlerBase
77
{
88
public override int GetUpdateResolution( )
99
{
10-
return 1000;
10+
return 5000;
1111
}
1212

1313
public override void Handle( )

0 commit comments

Comments
 (0)