Cache additional info to avoid entity queries

This commit is contained in:
NGnius (Graham) 2020-08-07 12:05:49 -04:00
parent 7f5a36cb62
commit 8354123169

View file

@ -24,6 +24,10 @@ namespace GamecraftModdingAPI.Blocks
protected bool inputToOutput; protected bool inputToOutput;
protected byte startPort;
protected byte endPort;
public static Wire Connect(SignalingBlock start, byte startPort, SignalingBlock end, byte endPort) public static Wire Connect(SignalingBlock start, byte startPort, SignalingBlock end, byte endPort)
{ {
WireEntityStruct wire = signalEngine.CreateNewWire(start.Id, startPort, end.Id, endPort); WireEntityStruct wire = signalEngine.CreateNewWire(start.Id, startPort, end.Id, endPort);
@ -90,6 +94,8 @@ namespace GamecraftModdingAPI.Blocks
startPortEGID = signalEngine.MatchBlockOutputToPort(start, wire.sourcePortUsage, out exists); startPortEGID = signalEngine.MatchBlockOutputToPort(start, wire.sourcePortUsage, out exists);
if (!exists) throw new WireInvalidException("Wire start port not found"); if (!exists) throw new WireInvalidException("Wire start port not found");
inputToOutput = false; inputToOutput = false;
endPort = wire.destinationPortUsage;
startPort = wire.sourcePortUsage;
} }
else else
{ {
@ -105,6 +111,8 @@ namespace GamecraftModdingAPI.Blocks
inputToOutput = true; // end is actually the source inputToOutput = true; // end is actually the source
// NB: start and end are handled exactly as they're received as params. // NB: start and end are handled exactly as they're received as params.
// This makes wire traversal easier, but makes logic in this class a bit more complex // This makes wire traversal easier, but makes logic in this class a bit more complex
endPort = wire.sourcePortUsage;
startPort = wire.destinationPortUsage;
} }
else else
{ {
@ -142,6 +150,8 @@ namespace GamecraftModdingAPI.Blocks
startPortEGID = signalEngine.MatchBlockOutputToPort(start, startPort, out exists); startPortEGID = signalEngine.MatchBlockOutputToPort(start, startPort, out exists);
if (!exists) throw new WireInvalidException("Wire start port not found"); if (!exists) throw new WireInvalidException("Wire start port not found");
} }
this.startPort = startPort;
this.endPort = endPort;
} }
/// <summary> /// <summary>
@ -159,6 +169,8 @@ namespace GamecraftModdingAPI.Blocks
if (!exists) throw new WireInvalidException("Wire end port not found"); if (!exists) throw new WireInvalidException("Wire end port not found");
startPortEGID = signalEngine.MatchBlockOutputToPort(wire.sourceBlockEGID, wire.sourcePortUsage, out exists); startPortEGID = signalEngine.MatchBlockOutputToPort(wire.sourceBlockEGID, wire.sourcePortUsage, out exists);
if (!exists) throw new WireInvalidException("Wire start port not found"); if (!exists) throw new WireInvalidException("Wire start port not found");
this.endPort = wire.destinationPortUsage;
this.startPort = wire.sourcePortUsage;
} }
internal Wire(WireEntityStruct wire) internal Wire(WireEntityStruct wire)
@ -171,6 +183,8 @@ namespace GamecraftModdingAPI.Blocks
if (!exists) throw new WireInvalidException("Wire end port not found"); if (!exists) throw new WireInvalidException("Wire end port not found");
startPortEGID = signalEngine.MatchBlockOutputToPort(wire.sourceBlockEGID, wire.sourcePortUsage, out exists); startPortEGID = signalEngine.MatchBlockOutputToPort(wire.sourceBlockEGID, wire.sourcePortUsage, out exists);
if (!exists) throw new WireInvalidException("Wire start port not found"); if (!exists) throw new WireInvalidException("Wire start port not found");
this.endPort = wire.destinationPortUsage;
this.startPort = wire.sourcePortUsage;
} }
/// <summary> /// <summary>
@ -275,15 +289,7 @@ namespace GamecraftModdingAPI.Blocks
/// </summary> /// </summary>
public byte StartPort public byte StartPort
{ {
get get => startPort;
{
WireEntityStruct wire = signalEngine.GetWire(wireEGID);
if (inputToOutput)
{
return wire.destinationPortUsage;
}
return wire.sourcePortUsage;
}
} }
/// <summary> /// <summary>
@ -299,15 +305,7 @@ namespace GamecraftModdingAPI.Blocks
/// </summary> /// </summary>
public byte EndPort public byte EndPort
{ {
get get => endPort;
{
WireEntityStruct wire = signalEngine.GetWire(wireEGID);
if (inputToOutput)
{
return wire.sourcePortUsage;
}
return wire.destinationPortUsage;
}
} }
/// <summary> /// <summary>
@ -322,7 +320,7 @@ namespace GamecraftModdingAPI.Blocks
/// <summary> /// <summary>
/// Convert the wire object to the direction the signal flows. /// Convert the wire object to the direction the signal flows.
/// Signals on wires always flows from a block output port to a block input port. /// Signals on wires always flow from a block output port to a block input port.
/// This is simply a different memory configuration and does not affect the in-game wire (which is always output -> input). /// This is simply a different memory configuration and does not affect the in-game wire (which is always output -> input).
/// </summary> /// </summary>
public void OutputToInputInPlace() public void OutputToInputInPlace()
@ -337,6 +335,9 @@ namespace GamecraftModdingAPI.Blocks
temp = endPortEGID; temp = endPortEGID;
endPortEGID = startPortEGID; endPortEGID = startPortEGID;
startPortEGID = temp; startPortEGID = temp;
byte tempPortNumber = endPort;
endPort = startPort;
startPort = tempPortNumber;
} }
} }
@ -346,7 +347,7 @@ namespace GamecraftModdingAPI.Blocks
{ {
return $"{nameof(Id)}: {Id}, Start{nameof(Start.Id)}: {Start.Id}, End{nameof(End.Id)}: {End.Id}, ({Start.Type}::{StartPort} aka {Start.PortName(StartPort, inputToOutput)}) -> ({End.Type}::{EndPort} aka {End.PortName(EndPort, !inputToOutput)})"; return $"{nameof(Id)}: {Id}, Start{nameof(Start.Id)}: {Start.Id}, End{nameof(End.Id)}: {End.Id}, ({Start.Type}::{StartPort} aka {Start.PortName(StartPort, inputToOutput)}) -> ({End.Type}::{EndPort} aka {End.PortName(EndPort, !inputToOutput)})";
} }
return $"{nameof(Id)}: {Id}, Start{nameof(Start.Id)}: {Start.Id}, End{nameof(End.Id)}: {End.Id}, ({Start.Type} -> {End.Type})"; return $"{nameof(Id)}: {Id}, Start{nameof(Start.Id)}: {Start.Id}, End{nameof(End.Id)}: {End.Id}, ({Start.Type}::{StartPort} -> {End.Type}::{EndPort})";
} }
internal static void Init() { } internal static void Init() { }