Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

Get Ore Markers in the Programmable Block #567

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
2463505
finished the pull request
Oct 12, 2016
507bea4
Refined a couple names.
Oct 12, 2016
2a6f511
refined a couple names.
Oct 12, 2016
27334a6
refined a couple names.
Oct 12, 2016
79ef9a8
changed struct name in order to follow the code design rules.
Oct 12, 2016
1edb274
refined some names.
Oct 12, 2016
d424eb2
Refactored slightly to maximize performance.
Oct 12, 2016
10e04ad
Checks for null and does its job anyway.
Oct 12, 2016
95ec518
Added more information to struct MyOreMarker and added more informati…
Oct 12, 2016
855aa1d
no message
Oct 12, 2016
fd1bfde
no message
Oct 12, 2016
288ccee
no message
Oct 12, 2016
fb0c938
exposed subtypeID due to request.
Oct 13, 2016
c45256a
Deposit unpacking method now matches the visible ore markers.
Oct 19, 2016
3cbf20e
fixed a small compilation error.
Oct 22, 2016
4fb7a9c
fixed a null exception in my code.
Oct 22, 2016
cf6d8b3
Finished, Polished, Tested.
Oct 22, 2016
2ef491c
Realised I dont need to prevent access of an ore detector with no owner.
Oct 22, 2016
ffebf73
minor text change
Oct 23, 2016
75ac63c
ref makes it clear to the user how exactly they will get their ore ma…
Oct 26, 2016
16f9340
GetOreMarkers() now prevents excessively fast runs. Removed duplicate…
Dec 4, 2016
86cd578
tweaked MyOreDetectorComponent.Update() to perform exactly the same a…
Dec 4, 2016
84e8841
Made the rate the method can run at in line with simulation updates.
Dec 8, 2016
d92c5bf
Removed unnecessary ref
Pharap Mar 1, 2017
a1feff6
Removed unnecessary ref
Pharap Mar 1, 2017
c51dfcb
Merge pull request #1 from Pharap/GetOreMarkersInPB
Mar 2, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Sources/Sandbox.Common/ModAPI/Ingame/IMyOreDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,30 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VRageMath;

namespace Sandbox.ModAPI.Ingame
{
public interface IMyOreDetector : IMyFunctionalBlock
{
float Range {get;}
bool BroadcastUsingAntennas {get;}

/// <summary>
///Returns your own List filled with visible ore markers.
/// </summary>
void GetOreMarkers (List <MyOreMarker> outputList);
}

public struct MyOreMarker
{
public readonly string ElementName;
public readonly Vector3D Location;

public MyOreMarker (string inputElement, Vector3D inputCoords)
{
this.ElementName = inputElement;
this.Location = inputCoords;
}
}
}
45 changes: 45 additions & 0 deletions Sources/Sandbox.Game/Game/Entities/Blocks/MyOreDetector.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#region Using

using System.Collections.Generic;

using Sandbox.Common.ObjectBuilders;
using Sandbox.Definitions;
using Sandbox.Game.Gui;
Expand Down Expand Up @@ -203,5 +205,48 @@ public bool BroadcastUsingAntennas

bool ModAPI.Ingame.IMyOreDetector.BroadcastUsingAntennas { get { return m_oreDetectorComponent.BroadcastUsingAntennas; } }
float ModAPI.Ingame.IMyOreDetector.Range { get { return Range; } }

public void GetOreMarkers (List <ModAPI.Ingame.MyOreMarker> usersList) //Imprinting on the reference parameter is cheaper than a return List<T> due to heap allocations.
{
usersList.Clear();
Vector3D blockCoordinates = new Vector3D (base.PositionComp.GetPosition());
m_oreDetectorComponent.Update (blockCoordinates, true);

foreach (MyEntityOreDeposit deposit in m_oreDetectorComponent.DetectedDeposits)
{
for (int i = 0; i < deposit.Materials.Count; i++)
{
MyEntityOreDeposit.Data depositData = deposit.Materials[i];
Vector3D cachesPosition = new Vector3D();
depositData.ComputeWorldPosition (deposit.VoxelMap, out cachesPosition);
string cachesElement = deposit.Materials[i].Material.MinedOre;

if (m_nearestDetections.ContainsKey (cachesElement) == false)
{
m_nearestDetections.Add (cachesElement, cachesPosition); //I decided Dictionary was the best way to group nearest markers since all I need is two variables.
}

else
{
Vector3D difference = blockCoordinates - cachesPosition;
Vector3D previousDifference = m_nearestDetections[cachesElement] - cachesPosition;
float distanceToCache = (float)difference.LengthSquared(); //explicitly converted in order to estimate the actual hud markers as close as possible.
float previousDistance = (float)previousDifference.LengthSquared();

if (distanceToCache < previousDistance)
{
m_nearestDetections[cachesElement] = cachesPosition; //I only want the nearest of each element.
}
}
}
}

foreach (KeyValuePair <string, Vector3D> marker in m_nearestDetections)
{
usersList.Add (new ModAPI.Ingame.MyOreMarker (marker.Key,
marker.Value));
}
m_nearestDetections.Clear();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ class MyOreDetectorComponent
public bool BroadcastUsingAntennas { get; set; }

private readonly Dictionary<MyVoxelBase, MyOreDepositGroup> m_depositGroupsByEntity = new Dictionary<MyVoxelBase, MyOreDepositGroup>();
public HashSet <MyEntityOreDeposit> DetectedDeposits { get; private set; }

public MyOreDetectorComponent()
{
Expand Down Expand Up @@ -247,6 +248,7 @@ public void Update(Vector3D position, bool checkControl = true)
if (deposit != null)
{
MyHud.OreMarkers.RegisterMarker(deposit);
DetectedDeposits.Add (deposit);
}
}
}
Expand All @@ -264,8 +266,8 @@ public void Clear()
MyHud.OreMarkers.UnregisterMarker(deposit);
}
}
DetectedDeposits.Clear();
}

}

/// <summary>
Expand Down