/// <summary> //VESTIGIAL PROTOTYPE... /// Draws the next turn based off the previous frame /// Avoids all the 'new'ing that takes place in the other DrawTurn /// </summary> /// <param name="frame"></param> /// <returns></returns> public override SimFrame DrawTurn(SimFrame frame) { frame.Find("ss").Position = new Vec2(SpaceShip.X, (FrameResolution.Y - (SpaceShip.Y / (SimVars.MoonLanderVars.SIM_HEIGHT / FrameResolution.Y))) - 60); frame.Find("downThrust").Position = new Vec2(SpaceShip.X + 4, (FrameResolution.Y - (SpaceShip.Y / (SimVars.MoonLanderVars.SIM_HEIGHT / FrameResolution.Y))) - 20); frame.Find("leftThrust").Position = new Vec2(SpaceShip.X - 10 - (32 * SpaceShip.Left / 3), (FrameResolution.Y - (SpaceShip.Y / (SimVars.MoonLanderVars.SIM_HEIGHT / FrameResolution.Y))) - 50); frame.Find("rightThrust").Position = new Vec2(SpaceShip.X + 42, (FrameResolution.Y - (SpaceShip.Y / (SimVars.MoonLanderVars.SIM_HEIGHT / FrameResolution.Y))) - 50); ((Sprite)frame.Find("downThrust")).Scale = new Vec2(1, SpaceShip.Throttle * .04); ((Sprite)frame.Find("leftThrust")).Scale = new Vec2(SpaceShip.Left / 3, .5); ((Sprite)frame.Find("rightThrust")).Scale = new Vec2(SpaceShip.Right / 3, .5); if (SpaceShip.Landed) { if (SpaceShip.Boom) { frame.AddText("FAIL", new Vec2(250, 150), new SolidBrush(Color.Red), 100); frame.ChangeImage("ss", SpriteList.SpaceShip_Explode); Fail(); } else { frame.AddText("SAFE", new Vec2(250, 150), new SolidBrush(Color.Green), 100); frame.ChangeImage("ss", SpriteList.SpaceShip_Landed); } } frame.ChangeText("ySpeed", "Speed Y: " + SpaceShip.YVel.ToString("F")); frame.ChangeText("xSpeed", "Speed X: " + SpaceShip.XVel.ToString("F")); frame.ChangeText("height", "Height: " + SpaceShip.Y.ToString("F")); frame.ChangeText("xPos", "X-Pos: " + SpaceShip.X.ToString("F")); frame.ChangeText("throttle", "Throttle: " + SpaceShip.Throttle.ToString("F")); frame.ChangeText("Fuel", "Fuel: " + SpaceShip.Fuel.ToString("F")); return frame; }
async Task <SimFrame> LoadFrame(int idx) { System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); //using (var mmf = MemoryMappedFile.OpenExisting("spatial_cell_buf")) //{ // mmf.CreateViewStream(); // using (var accessor = mmf.CreateViewAccessor(0, 2 * 1024 * 1024 * 1023)) // { // int colorSize = Marshal.SizeOf(typeof(MyColor)); // MyColor color; // // Make changes to the view. // for (long i = 0; i < 1500000; i += colorSize) // { // accessor.Read(i, out color); // color.Brighten(20); // accessor.Write(i, ref color); // } // } //} var frame = new SimFrame(); using (var fs = File.OpenRead(@"../spatial_cell_sim/results/frames.dat")) //using (var mmf = MemoryMappedFile.OpenExisting("spatial_cell_buf")) { //using (var accessor = mmf.CreateViewAccessor(0, 2 * 1024 * 1024 * 1023)) //{ //var br = new BinaryReader(fs); //var memStream = mmf.CreateViewStream(); var br = new BinaryReader(fs); br.BaseStream.Position = frameOffsets[idx]; //var frame = new SimFrame(); frame.numParticles = br.ReadUInt32(); frame.particles = new NativeArray <Particle>((int)frame.numParticles, Allocator.Persistent); int frameSize = (int)(particleStructSize * frame.numParticles); //var bytes = br.ReadBytes(frameSize); var bytes = new byte[frameSize]; await br.BaseStream.ReadAsync(bytes, 0, frameSize); unsafe { //byte* ptr = (byte*)0; //accessor.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr); fixed(void *bytesPointer = bytes) { //UnsafeUtility.CopyStructureToPtr((byte*)bytes[0], frame.particles.GetUnsafePtr()); UnsafeUtility.MemCpy(frame.particles.GetUnsafePtr(), bytesPointer, UnsafeUtility.SizeOf <Particle>() * frame.numParticles); //UnsafeUtility.MemCpy(frame.particles.GetUnsafePtr(), ptr + br.BaseStream.Position, UnsafeUtility.SizeOf<Particle>() * frame.numParticles); } //accessor.SafeMemoryMappedViewHandle.ReleasePointer(); //fixed (Particle* bytesPointer = frame.particles.ToArray()) //{ // NativeList<Particle> typeList; // frame.particleMap.TryGetValue(p.type, out typeList); //} } //br.BaseStream.Position += frameSize; frame.particleMap = new Dictionary <int, NativeList <Particle> >(); var particleList = new NativeList <Particle>((int)frame.numParticles, Allocator.Persistent); particleList.AddRange(frame.particles); frame.particleMap.Add(0, particleList); //var p = frame.particles.ToArray()[100]; //Debug.Log("p " + p.id + ", " + p.pos.UnityVector().ToString("F4")); //foreach (var p in frame.particles.ToArray()) //{ // //Debug.Log("p " + p.id + ", " + p.pos.UnityVector().ToString("F4")); // NativeList<Particle> typeList; // if (!frame.particleMap.ContainsKey(p.type)) // { // typeList = new NativeList<Particle>(0, Allocator.Persistent); // frame.particleMap.Add(p.type, typeList); // } // else // { // typeList = frame.particleMap[p.type]; // } // typeList.Add(p); //} //foreach (var entry in frame.particleMap) //{ // int particleCount = 0; // particleCount = particleMaxCountByType.TryGetValue(entry.Key, out particleCount) ? particleCount : 0; // particleCount = Math.Max(particleCount, entry.Value.Length); // particleMaxCountByType[entry.Key] = particleCount; //} frame.numMetabolicParticles = br.ReadUInt32(); frame.metabolicParticles = new NativeArray <MetabolicParticle>((int)frame.numMetabolicParticles, Allocator.Persistent); frameSize = (int)(metabolicParticleStructSize * frame.numMetabolicParticles); //bytes = br.ReadBytes(frameSize); bytes = new byte[frameSize]; await br.BaseStream.ReadAsync(bytes, 0, frameSize); unsafe { fixed(void *bytesPointer = bytes) { UnsafeUtility.MemCpy(frame.metabolicParticles.GetUnsafePtr(), bytesPointer, UnsafeUtility.SizeOf <MetabolicParticle>() * frame.numMetabolicParticles); } } //UnsafeUtility. //NativeArray. //for (var i = 0; i < frame.numParticles; i++) // frame.particles[i] = Marshal.PtrToStructure<Particle>(Marshal.UnsafeAddrOfPinnedArrayElement(bytes, particleStructSize * i)); //frames.Add(frame); //unsafe //{ // fixed (float* m = frame.metabolicParticles.ToArray()[0].metabolites) // { // Debug.Log("frame.metabolicParticles " + m[0] + ", " + m[1] + ", " + m[2] + ", " + m[3]); // } //} //return; } sw.Stop(); Debug.Log(string.Format("Frame {0} loaded in {1}s", idx, ((double)sw.ElapsedTicks / System.Diagnostics.Stopwatch.Frequency))); return(frame); }
private bool CheckForFinishText(SimFrame ret) { if (SpaceShip.Landed) { if (SpaceShip.Boom) { ret.AddTextWithShadow("Failure", "FAIL", new Vec2(150, 150), 100, new SolidBrush(Color.Red), new SolidBrush(Color.White)); ret.ChangeImage("ss", SpriteList.SpaceShip_Explode); } else { ret.AddTextWithShadow("Success", "SAFE", new Vec2(150, 150), 100, new SolidBrush(Color.Green), new SolidBrush(Color.White)); ret.ChangeImage("ss", SpriteList.SpaceShip_Landed); } return true; } return false; }
private void DrawText(int x, SimFrame ret) { Brush colour = new SolidBrush(Color.Orange); Brush shadow = new SolidBrush(Color.Green); //ADD STRINGS ret.AddTextWithShadow("title", "---SpaceShip---", new Vec2(x, 30), colour, shadow); ret.AddTextWithShadow("ySpeed", "Speed Y: " + SpaceShip.YVel.ToString("F"), new Vec2(x, 50), colour, shadow); ret.AddTextWithShadow("xSpeed", "Speed X: " + SpaceShip.XVel.ToString("F"), new Vec2(x, 70), colour, shadow); ret.AddTextWithShadow("height", "Height: " + SpaceShip.Y.ToString("F"), new Vec2(x, 90), colour, shadow); ret.AddTextWithShadow("xPos", "X-Pos: " + SpaceShip.X.ToString("F"), new Vec2(x, 110), colour, shadow); ret.AddTextWithShadow("throttle", "Throttle: " + SpaceShip.Throttle.ToString("F"), new Vec2(x, 130), colour, shadow); ret.AddTextWithShadow("Fuel", "Fuel: " + SpaceShip.Fuel.ToString("F"), new Vec2(x, 150), colour, shadow); }
private void DrawDebug(SimFrame ret, Vec2 leftT, Vec2 downT, Vec2 rightT) { Brush colour = new SolidBrush(Color.Red); Brush shadow = new SolidBrush(Color.White); ret.AddTextWithShadow("debug_left", String.Format("{0}", SpaceShip.Left.ToString("F")), leftT, 10, colour, shadow); ret.AddTextWithShadow("debug_down", String.Format("{0}", SpaceShip.Throttle.ToString("F")), downT, 10, colour, shadow); ret.AddTextWithShadow("debug_right", String.Format("{0}", SpaceShip.Right.ToString("F")), rightT, 10, colour, shadow); }
private void AddTracePathBlip(SimFrame ret) { if (Globals.TurnCount % 10 == 0) TracePath.Add(ret.PlotLocation(new Vec2(SpaceShip.X+20, ToY(SpaceShip.Y)), Color.Firebrick)); }
/// <summary> /// Renders MoonLander visual components based on their correlating simulation values /// </summary> /// <param name="xscale"></param> /// <param name="yscale"></param> /// <returns>Rendered instance of Simulation moment (SimFrame)</returns> public override SimFrame DrawTurn(Vec2 scale) { // NEW FRAME SimFrame ret = new SimFrame { Error = false, ToBeDrawn = true, BackGround = Color.Black, FrameId = Globals.turnCount, Renderables = new List<Drawable>() }; //ADD STRINGS ret.AddText("---SpaceShip---", new Vec2(500, 30), new SolidBrush(Color.Cornsilk)); ret.AddText("Speed X: " + SpaceShip.Variables["YVel"].ToString(), new Vec2(500, 50), new SolidBrush(Color.Cornsilk)); ret.AddText("Height: " + SpaceShip.Variables["Y"].ToString(), new Vec2(500, 70), new SolidBrush(Color.Cornsilk)); ret.AddText("Throttle: " + SpaceShip.Variables["Throttle"].ToString(), new Vec2(500, 90), new SolidBrush(Color.Cornsilk)); ret.AddText("Fuel: " + SpaceShip.Variables["Fuel"].ToString(), new Vec2(500, 110), new SolidBrush(Color.Cornsilk)); //ADD MOON BACKGROUND ret.AddRenderable(new Sprite {Picture = SpriteList.MoonSurface, Position = new Vec2(0, FrameResolution.Y - 70)}); //SET UP THE SPACESHIP Sprite shipSprite = new Sprite(); shipSprite.Picture = SpriteList.SpaceShip; //Set Image for ship if (bool.Parse(SpaceShip.Variables["Landed"].ToString())) { if (bool.Parse(SpaceShip.Variables["Boom"].ToString())) { ret.AddText("FAIL", new Vec2(250, 150), new SolidBrush(Color.Red), 100); shipSprite.Picture = SpriteList.SpaceShip_Explode; Fail(); } else { ret.AddText("SAFE", new Vec2(250, 150), new SolidBrush(Color.Green), 100); shipSprite.Picture = SpriteList.SpaceShip_Landed; } } //CALCULATE SS RELATIVE POSITION ON SCREEN int simHeight = 5000; shipSprite.Position = new Vec2(double.Parse(SpaceShip.Variables["X"].ToString()), (FrameResolution.Y - (double.Parse(SpaceShip.Variables["Y"].ToString())/(simHeight/FrameResolution.Y))) - 60); ret.AddRenderable(shipSprite);//ADD THE SS OBJECT return ret; //RETURN THE FINAL FRAME }
/// <summary> /// Renders MoonLander visual components based on their correlating simulation values /// </summary> /// <param name="scale"></param> /// <returns>Rendered instance of Simulation moment (SimFrame)</returns> public override SimFrame DrawTurn(Vec2 scale) { #region Draws and spits out a SimFrame based on current SpaceShip state // NEW FRAME SimFrame ret = new SimFrame { Error = false, ToBeDrawn = true, BackGround = Color.Black, FrameId = Globals.TurnCount, Renderables = new List<Drawable>() }; AddTracePathBlip(ret); DrawText(30, ret); //ADD MOON BACKGROUND ret.AddRenderable(new Sprite("surface1") { Picture = SpriteList.MoonSurface, Position = new Vec2(0, FrameResolution.Y - 20) }); ret.AddRenderable(new Sprite("surface2") { Picture = SpriteList.MoonSurface_Flip, Position = new Vec2(SpriteList.MoonSurface.Width, FrameResolution.Y - 20) }); //SET UP THE SPACESHIP Sprite shipSprite = new Sprite("ss"); shipSprite.Picture = SpriteList.SpaceShip; //Set Image for ship //Add the thrust to the Space Ship Sprite downThrust = new Sprite("downThrust") { Picture = SpriteList.ThrustDown }; Sprite leftThrust = new Sprite("leftThrust") { Picture = SpriteList.ThrustLeft }; Sprite rightThrust = new Sprite("rightThrust") { Picture = SpriteList.ThrustRight }; if (Globals.DrawTracePath) DrawTracePath(ret); //CALCULATE SS RELATIVE POSITION ON SCREEN shipSprite.Position = new Vec2(SpaceShip.X, (FrameResolution.Y - (SpaceShip.Y / (SimVars.MoonLanderVars.SIM_HEIGHT / FrameResolution.Y))) - 60); ret.AddRenderable(shipSprite);//ADD THE SS OBJECT if(!CheckForFinishText(ret)) // true if finished //do the thrust image { downThrust.Scale = new Vec2(1, SpaceShip.Throttle * .04); leftThrust.Scale = new Vec2(SpaceShip.Left /3, .5); rightThrust.Scale = new Vec2(SpaceShip.Right/3, .5); downThrust.Position = new Vec2(SpaceShip.X + 4, (FrameResolution.Y - (SpaceShip.Y / (SimVars.MoonLanderVars.SIM_HEIGHT / FrameResolution.Y))) - 20); leftThrust.Position = new Vec2(SpaceShip.X - 10 - (leftThrust.Picture.Width * leftThrust.Scale.X), (FrameResolution.Y - (SpaceShip.Y / (SimVars.MoonLanderVars.SIM_HEIGHT / FrameResolution.Y))) - 50); rightThrust.Position = new Vec2(SpaceShip.X + 42, (FrameResolution.Y - (SpaceShip.Y / (SimVars.MoonLanderVars.SIM_HEIGHT / FrameResolution.Y))) - 50); if (SpaceShip.Fuel > 0) { ret.AddRenderable(downThrust); //add the thrust object to the frame ret.AddRenderable(leftThrust); //add the thrust object to the frame ret.AddRenderable(rightThrust); //add the thrust object to the frame } } //DRAW ROCKS(IF APPLICABLE) List<Drawable> rocks = DrawRocks(); if (rocks != null) foreach (Drawable d in rocks) ret.AddRenderable(d); if (Globals.DrawDebug && State != SimulatorStateEnum.Complete) DrawDebug(ret, new Vec2(leftThrust.Position.X - 15 - (SpaceShip.Left * leftThrust.Scale.X * 6), leftThrust.Position.Y) , new Vec2(downThrust.Position.X + 1, downThrust.Position.Y + (30 * downThrust.Scale.Y)), new Vec2(rightThrust.Position.X + (SpaceShip.Right * rightThrust.Scale.X * 12) , rightThrust.Position.Y)); if (Globals.DrawTracePath) if(TracePath != null) for (int i = 1; i < TracePath.Count; i++) { ret.AddRenderable(new Line(TracePath[i - 1].Position, TracePath[i].Position, new SolidBrush(Color.MediumBlue), .5f )); } #endregion ret.TracePath = TracePath; return ret; //RETURN THE FINAL FRAME }
private void DrawLines(SimFrame ret) { double xx = 0, yy = 0; float throttleThickness = (float)((((Harrier.Throttle) - 60)/40) * 8f); //throttleThickness = 0.05f; if (throttleThickness < .05f) throttleThickness = .05f; Move2DInDirHS(Harrier.X, Harrier.Y, DegToRad(270 - Harrier.ThrustVector), Harrier.Throttle/4, ref xx, ref yy); ret.AddRenderable(new Line(new Vec2(ToX(Harrier.X+1), ToY(Harrier.Y-2)), new Vec2(ToX(xx+1), ToY(yy)), new SolidBrush(Color.Red), throttleThickness) { Id = "ThrustLine" }); ret.AddRenderable(new Line(new Vec2(ToX(Harrier.UnsafeX1), ToY(Harrier.SafeYDraw + 1)), new Vec2(ToX(Harrier.SafeX2), ToY(Harrier.SafeYDraw + 1)), new SolidBrush(Color.Red), 3f) {Id = "LandingLine"}); ret.AddRenderable(new Line(new Vec2(ToX(Harrier.MidSafeX - 3), ToY(Harrier.SafeYDraw + 1)), new Vec2(ToX(Harrier.MidSafeX + 3), ToY(Harrier.SafeYDraw + 1)), new SolidBrush(Color.Green), 3f) {Id = "MiddleSafe"}); }
private void DrawText(int x, ref SimFrame ret) { Brush colour = new SolidBrush(Color.Black); Brush shadow = new SolidBrush(Color.White); ret.AddTextWithShadow("location", String.Format("X, Y: \t\t\t {0}, {1}", Harrier.X.ToString("F"), Harrier.Y.ToString("F")), new Vec2(x, 20), colour, shadow); ret.AddTextWithShadow("velocity", String.Format("X-Vel, Y-Vel: \t\t {0}, {1}", Harrier.XVel.ToString("F"), Harrier.YVel.ToString("F")), new Vec2(x, 40), colour, shadow); ret.AddTextWithShadow("consumption", String.Format("Secs, Fuel: \t\t {0}, {1}", ((double)Globals.TurnCount / Harrier.TickPerSecond).ToString("F"), Harrier.Fuel.ToString("F")), new Vec2(x, 60), colour, shadow); ret.AddTextWithShadow("physics", String.Format("Mass, Throttle: \t\t {0}, {1}", Harrier.Mass.ToString("F"), Harrier.Throttle.ToString("F")), new Vec2(x, 80), colour, shadow); ret.AddTextWithShadow("wind", String.Format("ThrustVec, WindGust: \t {0}, {1}", Harrier.ThrustVector.ToString("F"), Harrier.WindSpeed.ToString("F")), new Vec2(x, 100), colour, shadow); ret.AddTextWithShadow("relative", String.Format("Speed Relative to Ship: \t {0}", Harrier.RelativeXVel.ToString("F")), new Vec2(x, 120), colour, shadow); ret.AddTextWithShadow("shipsSpeed", String.Format("Ship Speed knots (mps): \t {0}, {1}", Harrier.shipSpeed.ToString("F"), (Harrier.shipSpeed*Harrier.ConvertKnotsToMps).ToString("F")), new Vec2(x, 140), colour, shadow); ret.AddTextWithShadow("safeX", String.Format("Safe X: \t\t\t {0}", (Harrier.X - Harrier.MidSafeX - 40).ToString("F")), new Vec2(x, 160), colour, shadow); }
private void DrawDebug(SimFrame ret) { Brush colour = new SolidBrush(Color.Red); Brush shadow = new SolidBrush(Color.White); double xx = 0, yy = 0; Move2DInDirHS(Harrier.X, Harrier.Y, DegToRad(270 - Harrier.ThrustVector), Harrier.Throttle / 4, ref xx, ref yy); ret.AddTextWithShadow("debug_TV", String.Format("{0}", Harrier.ThrustVector.ToString("F")), new Vec2(ToX(Harrier.X+1), ToY(Harrier.Y-5)), 10, colour, shadow); ret.AddTextWithShadow("debug_Thrust", String.Format("{0}", Harrier.Throttle.ToString("F")), new Vec2(ToX(xx+2), ToY(yy)), 10, colour, shadow); }
private void CheckForFinishText(SimFrame ret) { if(Harrier.Landed) if (Harrier.Boom) { ret.AddTextWithShadow("Failure", "FAIL", new Vec2(150, 150), 100 , new SolidBrush(Color.Red), new SolidBrush(Color.DimGray)); } else { ret.AddTextWithShadow("Success", "SAFE", new Vec2(150, 150), 100, new SolidBrush(Color.Green), new SolidBrush(Color.DimGray)); } }
private void AddTracePathBlip(SimFrame ret) { if (Globals.TurnCount%20 == 0) TracePath.Add(ret.PlotLocation(new Vec2(ToX(Harrier.X - 8), ToY(Harrier.Y + 4)), Color.Firebrick)); }
public override SimFrame DrawTurn(SimFrame frame) { return frame; }
//scale provides the height (Y) public override SimFrame DrawTurn(Vec2 scale) { double height = scale.Y; Harrier.CarrierImageY = scale.Y - Harrier.CarrierHeightPixels - 28; SimFrame ret = new SimFrame { Error = false, ToBeDrawn = true, BackGround = Color.FromArgb(255, 0, 255, 255), Renderables = new List<Drawable> {} }; AddTracePathBlip(ret); ret.AddRenderable((new Sprite("ship") { Picture = Harrier.ShipImage, Position = new Vec2(Harrier.CarrierImageX, Harrier.CarrierImageY) } )); DrawLines(ret); CheckForFinishText(ret); if (Globals.DrawTracePath) DrawTracePath(ret); ret.AddRenderable(new Sprite("hs") { Picture = Harrier.PlaneImage, Position = new Vec2(ToX(Harrier.X-8),ToY(Harrier.Y+4)), Scale = new Vec2(1.5,1.5) } ); DrawText(30, ref ret); if(Globals.DrawDebug) DrawDebug(ret); ret.TracePath = TracePath; return ret; }