public async Task TraceComponent(DeltaComponent component, DeltaTraceTypes traceType, bool showDebugMessages) { var traceFlag = traceType == DeltaTraceTypes.Protocol ? TmNativeDefs.DeltaTraceFlags.Usr : TmNativeDefs.DeltaTraceFlags.Drv; var traceChain = component.TraceChain; if (traceType == DeltaTraceTypes.Physical) { traceChain[0] = ~traceChain[0]; } await Task.Run(() => _native.TmcDntBeginTraceEx(_cid, (uint)traceChain.Length, traceChain, (uint)traceFlag, 0, 0)) .ConfigureAwait(false); if (showDebugMessages) { await Task.Run(() => _native.TmcDntStopDebug(_cid)) .ConfigureAwait(false); } }
private async Task UpdateDeltaComponentPortStats(DeltaComponent component) { const int bufLength = 1024; var buf = new byte[bufLength]; var result = await Task.Run(() => _native.TmcDntGetPortStats(_cid, component.TraceChain, ref buf, bufLength)) .ConfigureAwait(false); var portStatsString = EncodingUtil.Win1251BytesToUtf8(buf); if (result == 0 || portStatsString.IsNullOrEmpty()) { return; } var(ticks, statusCount, analogCount, accumCount, messagesCount) = ParsePortStatsString(portStatsString); if (component.InitialPerformanceStats == null) { component.SetInitialPerformanceStats(ticks, statusCount, analogCount, accumCount, messagesCount); return; } component.UpdatePerformanceStatsAndString(ticks, statusCount, analogCount, accumCount, messagesCount); }
private float[] CalculatePath(Entity entity) { /// Will calculate how the entity will move float[] entity_direction = new float[] { 0, 0 }; //Will decide which directions the entity will move DeltaComponent entity_delta = entity.Get <DeltaComponent>(); StatusComponent entity_status = entity.Get <StatusComponent>(); /// Computes path for entity based on type of entity switch (entity_status.Type) { case (Game1.EntityType.Lightning): ComputeLightningDelta(entity); break; case (Game1.EntityType.Meteor): ComputeMeteorDelta(entity); break; case (Game1.EntityType.Fire): break; } entity_direction[(int)Delta_Direction.Horizontal] = entity_delta.XDelta; entity_direction[(int)Delta_Direction.Vertical] = entity_delta.YDelta; return(entity_direction); }
private void ComputeMeteorDelta(Entity entity) { /// Calculate path for meteor to move to village DeltaComponent entity_delta = entity.Get <DeltaComponent>(); PositionComponent entity_position = entity.Get <PositionComponent>(); /// Compute position from Meteor point to Village point //PositionComponent village_position = new PositionComponent(W_WIDTH_CENTER, W_HEIGHT_CENTER, 0); //int compute_X = Math.Abs(entity_position.XCoor - village_position.XCoor); //int compute_Y = Math.Abs(entity_position.YCoor - village_position.YCoor); //int gcd = GCD(compute_X, compute_Y); float new_delta_x = 4, new_delta_y = 2; //Temporarily hard-coded if (entity_position.XCoor > W_WIDTH_CENTER) { new_delta_x *= -1; // Changes vertical direction } if (entity_position.YCoor > W_HEIGHT_CENTER) { new_delta_y *= -1; // Changes horizontal direction } entity_delta.XDelta = new_delta_x; entity_delta.YDelta = new_delta_y; entity.Attach(entity_delta); }
/// <summary> /// Generates xml element containing the settings. /// </summary> /// <param name="rootElemName">Name to be used as a name of the root element.</param> /// <param name="suppressDefaults">Specifies whether to ommit optional nodes having set default values</param> /// <returns>XElement containing the settings</returns> public override XElement GetXml(string rootElemName, bool suppressDefaults) { XElement rootElem = new XElement(rootElemName); if (!suppressDefaults || !IsDefaultComponentHalfCodeLength) { rootElem.Add(new XAttribute("componentHalfCodeLength", ComponentHalfCodeLength.ToString(CultureInfo.InvariantCulture))); } if (!suppressDefaults || !IsDefaultLowestThreshold) { rootElem.Add(new XAttribute("lowestThreshold", LowestThreshold.ToString(CultureInfo.InvariantCulture))); } if (!suppressDefaults || !IsDefaultThresholdFullSpikeSet) { rootElem.Add(new XAttribute("thresholdFullSpikeSet", ThresholdFullSpikeSet.ToString(CultureInfo.InvariantCulture).ToLowerInvariant())); } if (!suppressDefaults || !IsDefaultSignalComponent) { rootElem.Add(new XAttribute("signalComponent", SignalComponent.ToString(CultureInfo.InvariantCulture).ToLowerInvariant())); } if (!suppressDefaults || !IsDefaultDeltaComponent) { rootElem.Add(new XAttribute("deltaComponent", DeltaComponent.ToString(CultureInfo.InvariantCulture).ToLowerInvariant())); } if (!suppressDefaults || !IsDefaultBinaryComponent) { rootElem.Add(new XAttribute("binaryComponent", BinaryComponent.ToString(CultureInfo.InvariantCulture).ToLowerInvariant())); } Validate(rootElem, XsdTypeName); return(rootElem); }
private void ComputeLightningDelta(Entity entity) { /// Calculate path for lighningh to move in circular formation DeltaComponent entity_delta = entity.Get <DeltaComponent>(); PositionComponent entity_position = entity.Get <PositionComponent>(); float new_delta_X = 2, new_delta_Y = 1; if (entity_position.YCoor > W_HEIGHT_CENTER) { new_delta_X *= -1; // Changes horizontal direction } if (entity_position.XCoor < W_WIDTH_CENTER) { new_delta_Y *= -1; // Changes vertical direction } entity_delta.XDelta = new_delta_X; entity_delta.YDelta = new_delta_Y; entity.Attach(entity_delta); // Replaces current delta component of lightning entity }
public static DeltaComponent FindNode(this IEnumerable <DeltaComponent> tree, DeltaComponent comparison) { if (comparison == null) { return(null); } foreach (var root in tree) { if (root.TraceChainString == comparison.TraceChainString) { return(root); } var child = root.Children.FindNode(comparison); if (child != null) { return(child); } } return(null); }
public async Task GetComponentsItems(DeltaComponent component) { var componentItemsPtr = await Task.Run(() => _native.TmcDntOpenItem(_cid, (uint)component.TraceChain.Length, component.TraceChain)) .ConfigureAwait(false); if (componentItemsPtr == IntPtr.Zero) { return; } component.ClearItems(); while (true) { var itemPtr = await Task.Run(() => _native.TmcDntGetNextItem(componentItemsPtr)) .ConfigureAwait(false); if (itemPtr == IntPtr.Zero) { break; } var deltaCommonStruct = Marshal.PtrToStructure <TmNativeDefs.DeltaCommon>(itemPtr); switch ((TmNativeDefs.DeltaItemTypes)deltaCommonStruct.Type) { case TmNativeDefs.DeltaItemTypes.Description: var descriptionStruct = Marshal.PtrToStructure <TmNativeDefs.DeltaDescription>(itemPtr); var descriptionStructSize = Marshal.SizeOf(descriptionStruct); var descriptionString = EncodingUtil.Win1251ToUtf8(Marshal.PtrToStringAnsi(IntPtr.Add(itemPtr, descriptionStructSize - 1))); if (descriptionStruct.Text[0] == '*') { component.Items.Add(DeltaItem.CreateDescriptionDeltaItem(descriptionString)); } else { component.Description += descriptionString + Environment.NewLine; } break; case TmNativeDefs.DeltaItemTypes.Status: var statusStruct = Marshal.PtrToStructure <TmNativeDefs.DeltaStatus>(itemPtr); var statusStructSize = Marshal.SizeOf(statusStruct); var numStatus = GetDeltaItemNum(itemPtr, statusStructSize, statusStruct.Length, statusStruct.Number); var addStringStatus = GetDeltaItemAdditionalText(itemPtr, statusStructSize, statusStruct.Length); var tmAddrStatus = statusStruct.TmsRtu == 0 || statusStruct.TmsRtu == 0 ? null : new TmAddr(TmType.Status, statusStruct.TmsChn, statusStruct.TmsRtu, statusStruct.TmsPoint); var statusObjectName = await GetObjectName(tmAddrStatus).ConfigureAwait(false); component.Items.Add(DeltaItem.CreateStatusDeltaItem(numStatus, statusStruct.LastUpdate, (TmNativeDefs.DeltaItemsFlags)statusStruct.DeltaFlags, statusStruct.Value, addStringStatus, tmAddrStatus, statusObjectName)); break; case TmNativeDefs.DeltaItemTypes.Analog: var analogStruct = Marshal.PtrToStructure <TmNativeDefs.DeltaAnalog>(itemPtr); var analogStructSize = Marshal.SizeOf(analogStruct); var numAnalog = GetDeltaItemNum(itemPtr, analogStructSize, analogStruct.Length, analogStruct.Number); var addStringAnalog = GetDeltaItemAdditionalText(itemPtr, analogStructSize, analogStruct.Length); var tmAddrAnalog = analogStruct.TmsRtu == 0 || analogStruct.TmsRtu == 0 ? null : new TmAddr(TmType.Analog, analogStruct.TmsChn, analogStruct.TmsRtu, analogStruct.TmsPoint); var analogObjectName = await GetObjectName(tmAddrAnalog).ConfigureAwait(false); component.Items.Add(DeltaItem.CreateAnalogDeltaItem(numAnalog, analogStruct.LastUpdate, (TmNativeDefs.DeltaItemsFlags)analogStruct.DeltaFlags, analogStruct.Value, addStringAnalog, tmAddrAnalog, analogObjectName)); break; case TmNativeDefs.DeltaItemTypes.Accum: var accumStruct = Marshal.PtrToStructure <TmNativeDefs.DeltaAccum>(itemPtr); var accumStructSize = Marshal.SizeOf(accumStruct); var numAccum = GetDeltaItemNum(itemPtr, accumStructSize, accumStruct.Length, accumStruct.Number); var addStringAccum = GetDeltaItemAdditionalText(itemPtr, accumStructSize, accumStruct.Length); var tmAddrAccum = accumStruct.TmsRtu == 0 || accumStruct.TmsRtu == 0 ? null : new TmAddr(TmType.Accum, accumStruct.TmsChn, accumStruct.TmsRtu, accumStruct.TmsPoint); var accumObjectName = await GetObjectName(tmAddrAccum).ConfigureAwait(false); component.Items.Add(DeltaItem.CreateAccumDeltaItem(numAccum, accumStruct.LastUpdate, (TmNativeDefs.DeltaItemsFlags)accumStruct.DeltaFlags, accumStruct.Value, addStringAccum, tmAddrAccum, accumObjectName)); break; case TmNativeDefs.DeltaItemTypes.Control: var controlStruct = Marshal.PtrToStructure <TmNativeDefs.DeltaControl>(itemPtr); var controlStructSize = Marshal.SizeOf(controlStruct); var numControl = GetDeltaItemNum(itemPtr, controlStructSize, controlStruct.Length, controlStruct.Number); var addStringControl = GetDeltaItemAdditionalText(itemPtr, controlStructSize, controlStruct.Length); var tmAddrControl = controlStruct.TmsRtu == 0 || controlStruct.TmsRtu == 0 ? null : new TmAddr(TmType.Status, controlStruct.TmsChn, controlStruct.TmsRtu, controlStruct.TmsPoint); var controlObjectName = await GetObjectName(tmAddrControl).ConfigureAwait(false); component.Items.Add(DeltaItem.CreateControlDeltaItem(numControl, controlStruct.LastUpdate, (TmNativeDefs.DeltaItemsFlags)controlStruct .DeltaFlags, controlStruct.CtrlBlock, controlStruct.CtrlGroup, controlStruct.CtrlPoint, addStringControl, tmAddrControl, controlObjectName)); break; case TmNativeDefs.DeltaItemTypes.AnalogF: var analogFStruct = Marshal.PtrToStructure <TmNativeDefs.DeltaAnalogF>(itemPtr); var analogFStructSize = Marshal.SizeOf(analogFStruct); var numAnalogF = GetDeltaItemNum(itemPtr, analogFStructSize, analogFStruct.Length, analogFStruct.Number); var addStringAnalogF = GetDeltaItemAdditionalText(itemPtr, analogFStructSize, analogFStruct.Length); var tmAddrAnalogF = analogFStruct.TmsRtu == 0 || analogFStruct.TmsRtu == 0 ? null : new TmAddr(TmType.Analog, analogFStruct.TmsChn, analogFStruct.TmsRtu, analogFStruct.TmsPoint); var analogFObjectName = await GetObjectName(tmAddrAnalogF).ConfigureAwait(false); component.Items.Add(DeltaItem.CreateAnalogFloatDeltaItem(numAnalogF, analogFStruct.LastUpdate, (TmNativeDefs.DeltaItemsFlags)analogFStruct .DeltaFlags, analogFStruct.Value, addStringAnalogF, tmAddrAnalogF, analogFObjectName)); break; case TmNativeDefs.DeltaItemTypes.AccumF: var accumFStruct = Marshal.PtrToStructure <TmNativeDefs.DeltaAccumF>(itemPtr); var accumFStructSize = Marshal.SizeOf(accumFStruct); var numAccumF = GetDeltaItemNum(itemPtr, accumFStructSize, accumFStruct.Length, accumFStruct.Number); var addStringAccumF = GetDeltaItemAdditionalText(itemPtr, accumFStructSize, accumFStruct.Length); var tmAddrAccumF = accumFStruct.TmsRtu == 0 || accumFStruct.TmsRtu == 0 ? null : new TmAddr(TmType.Accum, accumFStruct.TmsChn, accumFStruct.TmsRtu, accumFStruct.TmsPoint); var accumFObjectName = await GetObjectName(tmAddrAccumF).ConfigureAwait(false); component.Items.Add(DeltaItem.CreateAccumFloatDeltaItem(numAccumF, accumFStruct.LastUpdate, (TmNativeDefs.DeltaItemsFlags)accumFStruct .DeltaFlags, accumFStruct.Value, addStringAccumF, tmAddrAccumF, accumFObjectName)); break; case TmNativeDefs.DeltaItemTypes.StrVal: var strValStruct = Marshal.PtrToStructure <TmNativeDefs.DeltaStrval>(itemPtr); var strValStructSize = Marshal.SizeOf(strValStruct); var strValValueString = EncodingUtil.Win1251ToUtf8(Marshal.PtrToStringAnsi(IntPtr.Add(itemPtr, strValStructSize - 1))); var structSizeWithValueString = strValStructSize + strValValueString.Length; var strValDescriptionString = strValStruct.Length > structSizeWithValueString ? EncodingUtil.Win1251ToUtf8(Marshal.PtrToStringAnsi(IntPtr.Add(itemPtr, structSizeWithValueString))) : ""; var tmAddrStrVal = strValStruct.TmsRtu == 0 || strValStruct.TmsRtu == 0 ? null : new TmAddr(TmType.Unknown, strValStruct.TmsChn, strValStruct.TmsRtu, strValStruct.TmsPoint); component.Items.Add(DeltaItem.CreateStrValDeltaItem(strValStruct.Number, strValStruct.LastUpdate, (TmNativeDefs.DeltaItemsFlags)strValStruct.DeltaFlags, strValValueString, strValDescriptionString, tmAddrStrVal)); break; default: continue; } } await Task.Run(() => _native.TmcDntCloseItem(componentItemsPtr)) .ConfigureAwait(false); }
private async void BuildLightningEntity(int newLightningID) { /// using newLightningID we can grab the correct entity in _world and call Attach<T>(T component) to attach our own custom components to the existing entity Entity placeHolder = Game1._world.GetEntity(newLightningID); /// Attach all the correct components to the entity // First give it a starting position - this is randomly selected from 4 pre-defined spawn points // Second, give it a delta which should be calculated based off of the initial position GodsOfCalamityBeta.ECSComponents.PositionComponent InitialPosition; int spawnPoint = randomNumGen.Next(1, 5); float initX; float initY; float initAngle; switch (spawnPoint) { case 1: // calculate starting position initX = (float)0.25 * Game1.screenWidth; initY = (float)0.5 * Game1.screenHeight; initAngle = 0; InitialPosition = new PositionComponent(initX, initY, initAngle); placeHolder.Attach(InitialPosition); break; case 2: // calculate starting position initX = (float)0.5 * Game1.screenWidth; initY = (float)0.25 * Game1.screenHeight; initAngle = 0; InitialPosition = new PositionComponent(initX, initY, initAngle); placeHolder.Attach(InitialPosition); break; case 3: // calculate starting position initX = (float)0.75 * Game1.screenWidth; initY = (float)0.5 * Game1.screenHeight; initAngle = 0; InitialPosition = new PositionComponent(initX, initY, initAngle); placeHolder.Attach(InitialPosition); break; case 4: // calculate starting position initX = (float)0.5 * Game1.screenWidth; initY = (float)0.75 * Game1.screenHeight; initAngle = 0; InitialPosition = new PositionComponent(initX, initY, initAngle); placeHolder.Attach(InitialPosition); break; default: break; } /// Initializes Components SpriteComponent new_sprite = new SpriteComponent(Game1.SpriteDict["lightning"], 1); DeltaComponent new_delta = new DeltaComponent(0, 0, 0); StatusComponent new_status = new StatusComponent(false, Game1.EntityType.Lightning); CollisionBoxComponet new_collision = new CollisionBoxComponet(new_sprite.Sprite, placeHolder.Get <PositionComponent>()); /// Attaches Components placeHolder.Attach(new_sprite); placeHolder.Attach(new_delta); placeHolder.Attach(new_status); placeHolder.Attach(new_collision); await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { Game1.myGamePage.CreateNewLightning(newLightningID); } ); return; }