Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
using System;
using System.Collections.Generic;
using Sandbox.ModAPI;
using VRage.Game.ModAPI;

namespace Epstein_Fusion_DS.HeatParts
{
/// <summary>
/// Copy this class into another mod and call Load() in LoadData() to use the grid heat API.
/// </summary>
public sealed class GridHeatApi
{
private const long Channel = 11920040L;
private const string ApiRequest = "GridHeatAPI.Request";

private Action _onReady;
private Dictionary<string, Delegate> _api;
private bool _registered;

public bool IsReady => _api != null;

public void Load(Action onReady = null)
{
if (_registered)
return;

_onReady = onReady;
_registered = true;
MyAPIGateway.Utilities.RegisterMessageHandler(Channel, HandleMessage);
MyAPIGateway.Utilities.SendModMessage(Channel, ApiRequest);
}

public void Unload()
{
if (!_registered)
return;

MyAPIGateway.Utilities.UnregisterMessageHandler(Channel, HandleMessage);
_registered = false;
_api = null;
_onReady = null;
}

public int Version()
{
var method = Get<Func<int>>("Version");
return method?.Invoke() ?? 0;
}

public bool AddHeat(IMyCubeGrid grid, float heat)
{
var method = Get<Func<IMyCubeGrid, float, bool>>("AddHeat");
return method != null && method(grid, heat);
}

public bool SetHeat(IMyCubeGrid grid, float heat)
{
var method = Get<Func<IMyCubeGrid, float, bool>>("SetHeat");
return method != null && method(grid, heat);
}

public float GetHeat(IMyCubeGrid grid)
{
var method = Get<Func<IMyCubeGrid, float>>("GetHeat");
return method?.Invoke(grid) ?? -1;
}

public float GetHeatRatio(IMyCubeGrid grid)
{
var method = Get<Func<IMyCubeGrid, float>>("GetHeatRatio");
return method?.Invoke(grid) ?? -1;
}

public float GetHeatCapacity(IMyCubeGrid grid)
{
var method = Get<Func<IMyCubeGrid, float>>("GetHeatCapacity");
return method?.Invoke(grid) ?? -1;
}

public float GetHeatDissipation(IMyCubeGrid grid)
{
var method = Get<Func<IMyCubeGrid, float>>("GetHeatDissipation");
return method?.Invoke(grid) ?? -1;
}

public float GetHeatGeneration(IMyCubeGrid grid)
{
var method = Get<Func<IMyCubeGrid, float>>("GetHeatGeneration");
return method?.Invoke(grid) ?? -1;
}

public bool SetHeatCapacity(IMyCubeGrid grid, float capacity)
{
var method = Get<Func<IMyCubeGrid, float, bool>>("SetHeatCapacity");
return method != null && method(grid, capacity);
}

public bool SetHeatDissipation(IMyCubeGrid grid, float dissipation)
{
var method = Get<Func<IMyCubeGrid, float, bool>>("SetHeatDissipation");
return method != null && method(grid, dissipation);
}

public bool SetHeatGeneration(IMyCubeGrid grid, float generation)
{
var method = Get<Func<IMyCubeGrid, float, bool>>("SetHeatGeneration");
return method != null && method(grid, generation);
}

public void SetHudVisible(bool visible)
{
Get<Action<bool>>("SetHudVisible")?.Invoke(visible);
}

public void SetHudOffset(float x, float y, float z)
{
Get<Action<float, float, float>>("SetHudOffset")?.Invoke(x, y, z);
}

private T Get<T>(string name) where T : class
{
if (_api == null || !_api.ContainsKey(name))
return null;

return _api[name] as T;
}

private void HandleMessage(object obj)
{
var api = obj as Dictionary<string, Delegate>;
if (api == null)
return;

_api = api;
_onReady?.Invoke();
_onReady = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ internal class GridHeatManager

public float HeatGeneration;

public float ApiHeatCapacity;

public float ApiHeatDissipation;

public float ApiHeatGeneration;

public float HeatRatio = float.Epsilon;

public float HeatStored;
Expand All @@ -34,15 +40,20 @@ public GridHeatManager(IMyCubeGrid grid)

public MyCubeGrid Grid { get; }

public float GrossHeatDissipation => (HeatDissipation + BaseHeatDissipation) * HeatRatio;
public float TotalHeatCapacity => HeatCapacity + BaseHeatCapacity + ApiHeatCapacity;

public float GrossHeatDissipation => (HeatDissipation + BaseHeatDissipation + ApiHeatDissipation) * HeatRatio;

public bool HasApiHeat =>
ApiHeatCapacity > 0 || ApiHeatDissipation > 0 || ApiHeatGeneration != 0 || HeatStored > 0;

public void UpdateTick()
{
if (_ticks % 15 == 0)
Update15Tick();
_ticks++;

if (HeatCapacity + BaseHeatCapacity == 0)
if (TotalHeatCapacity == 0)
{
HeatRatio = 1;
HeatStored = 0;
Expand All @@ -52,10 +63,28 @@ public void UpdateTick()
HeatStored += (HeatGeneration - GrossHeatDissipation) / 60;
if (HeatStored < 0)
HeatStored = 0;
else if (HeatStored > HeatCapacity + BaseHeatCapacity)
HeatStored = HeatCapacity + BaseHeatCapacity;
else if (HeatStored > TotalHeatCapacity)
HeatStored = TotalHeatCapacity;

HeatRatio = HeatStored / TotalHeatCapacity;
}

public float AddHeat(float heat)
{
return SetHeat(HeatStored + heat);
}

public float SetHeat(float heat)
{
if (heat < 0)
heat = 0;

if (TotalHeatCapacity > 0 && heat > TotalHeatCapacity)
heat = TotalHeatCapacity;

HeatRatio = HeatStored / (HeatCapacity + BaseHeatCapacity);
HeatStored = heat;
HeatRatio = TotalHeatCapacity > 0 ? HeatStored / TotalHeatCapacity : HeatStored > 0 ? 1 : float.Epsilon;
return HeatStored;
}

public void RemoveAssembly(int assemblyId)
Expand All @@ -71,7 +100,7 @@ private void Update15Tick()
BaseHeatDissipation = 2 * (gridSize.X * gridSize.Y + gridSize.Y * gridSize.Z + gridSize.Z * gridSize.X) *
BaseHeatDissipationModifier;

HeatGeneration = 0;
HeatGeneration = ApiHeatGeneration;
foreach (var assemblyId in ModularApi.GetGridAssemblies(Grid))
HeatGeneration +=
ModularApi.GetAssemblyProperty<float>(assemblyId,
Expand Down Expand Up @@ -106,4 +135,4 @@ public void OnPartRemove(int assemblyId, IMyCubeBlock block, bool isBaseBlock)
}
}
}
}
}
Loading