/// <summary> /// Move context gost to point with speed /// </summary> /// <param name="to">Target point</param> /// <param name="speed">Moving speed</param> public void MoveTo(FieldPoint to, Speeds speed) { if (this.IsThinkEachTurn) { } else { _ghost.Path = new PathCreator(_fieldMatrix).GetWay(_ghost.FieldPointNow, to); } }
public Image GetImage(int width, int height) { PathDrawer drawer = new PathDrawer(width, height) { PathFinder = PathFinder, SpeedPoints = Points.ToArray(), SpeedValues = Speeds.ToArray(), ShowSpeedPoints = true }; return(drawer.Image); }
private void LoadSettings() { using (PauseNotify.For(this)) { using (var reg = new EngineRegistrySettings()) { SelectedSpeed = Speeds.FirstOrDefault(speed => (int)speed.Value == reg.Speed); SelectedEngine = Engines.FirstOrDefault(key => (CaptureMethod)key.Value == reg.CaptureMethod); AutoStart = reg.AutoStart; ShowControls = reg.GetOrCreate("ShowControls", true); } } }
IStrategyBuilder IStrategyBuilder.ShallowCopy() { StrategyBuilder copy = new StrategyBuilder { Limits = Limits.ToArray(), Speeds = Speeds.ToArray(), Threading = Threading, FinishTimeout = FinishTimeout, ScenarioFactory = ScenarioFactory, InitialUserData = InitialUserData, Aggregators = Aggregators.ToArray() }; return(copy); }
/// <summary> /// Samples the current speed of the task. /// </summary> /// <param name="speed">The speed of the task.</param> protected void SampleSpeed(float speed) { lock (Speeds) { IList <KeyValuePair <DateTime, double> > outliers = Speeds.GetOutliers(0.95); if (outliers != null) { List <KeyValuePair <DateTime, double> > recentOutliers = outliers.Where( sample => (DateTime.Now - sample.Key).TotalSeconds <= 60).ToList(); if (recentOutliers.Count >= 5) { Speeds.Reset(); recentOutliers.ForEach(sample => Speeds.Add(sample.Value)); } } Speeds.Add(speed); PredictedSpeed = Speeds.Predict(0.50); } }
public void InitPreDataSpeed() { tick = 0; Speeds.Clear(); if (autoMode)//自动模式 { if (incSpeed != 0 && preLine > 0) { float s = startSpeed; float inc = incSpeed; do { float abs = Math.Abs(startSpeed - endSpeed); if (incSpeed > abs) { inc = abs; } for (int i = 0; i < preLine; i++) { Speeds.Add(s); } s += inc; } while (s <= endSpeed); } } else { foreach (var item in userData) { if (item.Speed > 0 && item.Line > 0) { for (int i = 0; i < item.Line; i++) { Speeds.Add(item.Speed); } } } } }
/// <summary> /// Resets the starting time of the task. The speed measurement is /// automatically started when the ProgressManagerBase object is created. /// </summary> public void Restart() { StartTime = DateTime.Now; lock (Speeds) Speeds.Reset(); }
static void Main(string[] args) { const int NoiseSilencerEffect = 10; ChannelType channelType = ChannelType.Null; AudioFileReader audioStream = null; long currentBaseOffset = 0; float[] buffer = null; int bufferPointer = 0; long peakCount = 0; TextWriter outRawWriter = null; string outputDirectory = null; float upperPeak, lowerPeak; Tuple <float, long> peak = new Tuple <float, long>(0, 0); long lastPeakOffset = 0; bool upper = true; Speeds speed = Speeds.s600bps; int OnePeaks; int ZeroPeaks; int ThresholdPeakCount; int TypicalOneCount; int TypicalZeroCount; int peakCount1 = 0; int peakCount0 = 0; bool?[] shiftRegister = new bool?[11]; DetectMode currentMode = DetectMode.WaitHeader; int valueCounter = 0; string currentFileName = ""; byte[] currentFileImage = new byte[32767]; int currentFileImageSize = 0; bool bitsInPeakLog = true; int tryingBits = 0; #if DEBUG var waveRecorder = new List <Tuple <float, long> >(); var peakRecorder = new List <Tuple <float, long> >(); var waveLogEnabled = false; #endif if (args.Length == 0) { Console.Error.WriteLine("Soft CMT Interface by autumn"); Console.Error.WriteLine("usage: softcmtif INPUT_FILE_NAME [--verbose] [--300|--600|--1200] [--right|--left|--average|--maximum] [--peaklog FILE_NAME] [--bitsinpeaklog] [--outraw FILE_NAME] [--outdir PATH]"); return; } bool bVerbose = false; bool peaklogWaiting = false; bool outRawWaiting = false; bool outDirWaiting = false; foreach (var item in args.Skip(1)) { if (peaklogWaiting) { peaklogWriter = File.CreateText(item); peaklogWaiting = false; } else if (outRawWaiting) { outRawWriter = File.CreateText(item); outRawWaiting = false; } else if (outDirWaiting) { outputDirectory = item; outDirWaiting = false; } else if (item == "--verbose") { bVerbose = true; } else if (item == "--right") { channelType = ChannelType.Right; } else if (item == "--left") { channelType = ChannelType.Left; } else if (item == "--average") { channelType = ChannelType.Average; } else if (item == "--maximum") { channelType = ChannelType.Maximum; } else if (item == "--bitsinpeaklog") { bitsInPeakLog = true; } else if (item == "--peaklog") { peaklogWaiting = true; } else if (item == "--outraw") { outRawWaiting = true; } else if (item == "--outdir") { outDirWaiting = true; } else if (item == "--300") { speed = Speeds.s300bps; } else if (item == "--600") { speed = Speeds.s600bps; } else if (item == "--1200") { speed = Speeds.s1200bps; } else { Console.WriteLine($"Unknwon option {item}"); return; } } if (bVerbose) { Console.WriteLine($"File Length: {new FileInfo(args[0]).Length}"); } using (audioStream = new AudioFileReader(args[0])) { audioStream.Position = 0; setSpeed(); clearShiftRegister(); if (bVerbose) { Console.WriteLine($"Audio Stream Length: {audioStream.Length}"); Console.WriteLine($"BlockAlign: {audioStream.BlockAlign}"); Console.WriteLine($"Channels: {audioStream.WaveFormat.Channels}"); Console.WriteLine($"BitsPerSample: {audioStream.WaveFormat.BitsPerSample}"); Console.WriteLine($"Encoding: {audioStream.WaveFormat.Encoding}"); Console.WriteLine($"ExtraSize: {audioStream.WaveFormat.ExtraSize}"); Console.WriteLine($"SampleRate: {audioStream.WaveFormat.SampleRate}"); } if (audioStream.WaveFormat.Channels == 1) { if (channelType != ChannelType.Null) { Console.WriteLine("Mono file not support --right|--left|--average"); return; } } else if (audioStream.WaveFormat.Channels == 2) { if (channelType == ChannelType.Null) { channelType = ChannelType.Maximum; } } else { Console.WriteLine($"Not supported channels {audioStream.WaveFormat.Channels}"); return; } if (bVerbose) { Console.WriteLine($"Channel selected: ChannelType:{channelType}"); } // detect upper peak and lower peak audioStream.Position = 0; upperAndLowerPeak(); // detect wave peaks audioStream.Position = 0; peakCount1 = 0; peakCount0 = 0; currentBaseOffset = 0; #if DEBUG waveLogEnabled = true; #endif for (; ;) { var d = readUnit(); if (d == null) { break; } //if (peaklogWriter != null) peaklogWriter.WriteLine($"[{d.Item1} {d.Item2}]"); if (upper) { if (d.Item1 > peak.Item1) { peak = d; } else if (d.Item1 < 0.0f) { setPeak(d, false); } } else { if (d.Item1 < peak.Item1) { peak = d; } else if (d.Item1 > 0.0f) { setPeak(d, true); } } } //float[] samples = new float[audioStream.Length / audioStream.BlockAlign * audioStream.WaveFormat.Channels]; //audioStream.Read(samples, 0, samples.Length); // playback //var outputDevice = new WaveOutEvent(); //outputDevice.Init(audioStream); //outputDevice.Play(); //await Task.Delay(10000); if (bVerbose) { Console.WriteLine($"Detected: {peakCount} peaks."); } } if (peaklogWriter != null) { peaklogWriter.Close(); } if (outRawWriter != null) { outRawWriter.Close(); } Console.WriteLine("Done"); #if DEBUG void waveLogSaver() { //Console.WriteLine($"waveRecorder.Count()=={waveRecorder.Count()}"); //Console.WriteLine($"waveRecorder[0].Item2=={waveRecorder[0].Item2}"); //Console.WriteLine($"waveRecorder[1].Item2=={waveRecorder[1].Item2}"); using (var w = File.CreateText("wavelog1.csv")) { //var waves = waveRecorder.TakeLast(300); var waves = waveRecorder; foreach (var item in waves) { w.WriteLine($"{item.Item2},{item.Item1}"); } } using (var w = File.CreateText("peaklog1.csv")) { foreach (var item in peakRecorder.TakeLast(100)) { w.WriteLine($"{item.Item2},{item.Item1}"); } } Console.WriteLine("Fatal Exit"); if (peaklogWriter != null) { peaklogWriter.Close(); } if (outRawWriter != null) { outRawWriter.Close(); } Environment.Exit(0); } #endif void saveFile() { if (outputDirectory == null) { Console.WriteLine("If you want to save this file, use --outdir option"); return; } var fullpath = Path.Combine(outputDirectory, DateTime.Now.ToString("yyyyMMddHHmmss") + " " + currentFileName + ".bin"); using (var stream = File.Create(fullpath)) { stream.Write(currentFileImage, 0, currentFileImageSize - 9 + 2); } Console.WriteLine($"{fullpath} saved"); } void fileDetector(int value) { if (currentMode == DetectMode.WaitHeader) { if (value == 0xd3) { valueCounter++; if (valueCounter >= 10) { valueCounter = 0; currentFileName = ""; currentMode = DetectMode.GettingFileName; } } else { valueCounter = 0; } } else if (currentMode == DetectMode.GettingFileName) { if (value != 0) { currentFileName += (char)value; } valueCounter++; if (valueCounter >= 6) { Console.WriteLine($"Found: {currentFileName} (N-BASIC Binary Image)"); valueCounter = 0; currentMode = DetectMode.GettingBody; currentFileImageSize = 0; goToCarrierDetectMode(); } } else if (currentMode == DetectMode.GettingBody) { currentFileImage[currentFileImageSize++] = (byte)value; if (value == 0) { valueCounter++; if (valueCounter == 12) { saveFile(); valueCounter = 0; currentMode = DetectMode.WaitHeader; goToCarrierDetectMode(); } } else { valueCounter = 0; } } } void notifyByte(int value) { if (peaklogWriter != null) { peaklogWriter.WriteLine($"BYTE {value:X2}"); } if (outRawWriter != null) { outRawWriter.Write((char)value); } fileDetector(value); // TBW } void clearShiftRegister() { for (int i = 0; i < shiftRegister.Length; i++) { shiftRegister[i] = true; } } void tapeReadError() { Console.WriteLine($"Tape Read Error [offset:{currentBaseOffset + bufferPointer}]"); //Environment.Exit(0); waveLogSaver(); } void notifyBit(bool?bit) { tryingBits++; if (bitsInPeakLog && peaklogWriter != null) { peaklogWriter.WriteLine($"[{bit} {peakCount0} {peakCount1}]"); } for (int i = 0; i < shiftRegister.Length - 1; i++) { shiftRegister[i] = shiftRegister[i + 1]; } shiftRegister[shiftRegister.Length - 1] = bit; // check start bit and two stop bit if (shiftRegister[0] == false && shiftRegister[9] == true && shiftRegister[10] == true) { // found frame var val = 0; for (int j = 0; j < 8; j++) { val >>= 1; if (shiftRegister[j + 1] == true) { val |= 0x80; } else if (shiftRegister[j + 1] == null) { if (carrierDetectMode == CDMode.TryingFirstByte) { carrierDetectMode = CDMode.TransferMode; return; } tapeReadError(); return; } } notifyByte(val); if (carrierDetectMode == CDMode.TryingFirstByte) { carrierDetectMode = CDMode.TransferMode; } clearShiftRegister(); } else if (tryingBits == 10) { if (carrierDetectMode == CDMode.TryingFirstByte) { carrierDetectMode = CDMode.CarrierDetecting; } } } void setPeak(Tuple <float, long> d, bool upperValue) { #if DEBUG if (waveLogEnabled) { peakRecorder.Add(d); } #endif var timeOffset = (peak.Item2 - lastPeakOffset) / audioStream.WaveFormat.Channels; notifyPeak(timeOffset); //if (peaklogWriter != null) peaklogWriter.WriteLine($"PEAK {timeOffset} {peak.Item2}"); lastPeakOffset = peak.Item2; peak = d; upper = upperValue; } void goToCarrierDetectMode() { carrierDetectMode = CDMode.CarrierDetecting; } void notifyPeak(long timeOffset) { peakCount++; //if (bVerbose && peakCount < 20) Console.Write($"{timeOffset},"); var b = timeOffset < ThresholdPeakCount; //if (peaklogWriter != null) peaklogWriter.WriteLine($"[{(b ? 1 : 0)}]"); if (carrierDetectMode == CDMode.CarrierDetecting) { if (b == false) { return; } carrierDetectMode = CDMode.CarrierDetected; return; } else if (carrierDetectMode == CDMode.CarrierDetected) { if (b == true) { return; } carrierDetectMode = CDMode.TryingFirstByte; clearShiftRegister(); tryingBits = 0; peakCount1 = 0; peakCount0 = 1; return; } if (b) { peakCount1++; } else { peakCount0++; } if (peakCount1 == OnePeaks && peakCount0 == 0) { notifyBit(true); peakCount1 = 0; peakCount0 = 0; } else if (peakCount1 == 0 && peakCount0 == ZeroPeaks) { notifyBit(false); peakCount1 = 0; peakCount0 = 0; } else if (peakCount1 + peakCount0 * 2 >= OnePeaks) { #if DEBUG if (bufferPointer + currentBaseOffset == 5380) { waveLogSaver(); } #endif notifyBit(null); peakCount1 = 0; peakCount0 = 0; if (carrierDetectMode == CDMode.TryingFirstByte) { carrierDetectMode = CDMode.CarrierDetecting; } } } float[] readBlock() { var buf = new float[256]; var bytes = audioStream.Read(buf, 0, buf.Length); if (bytes == 0) { return(null); } return(buf); } Tuple <float, long> readRawUnit() { if (buffer == null || bufferPointer >= buffer.Length) { buffer = readBlock(); if (buffer == null) { return(null); } //currentBaseOffset = audioStream.Position; currentBaseOffset += bufferPointer; bufferPointer = 0; } var v = buffer[bufferPointer]; #if NOISE_SILENCER_ENABLE // noise silencer if (v > 0 && v < upperPeak / NoiseSilencerEffect) { v = 0; //if (peaklogWriter != null) peaklogWriter.WriteLine("Detect upper cancel"); } if (v < 0 && v > lowerPeak / NoiseSilencerEffect) { v = 0; //if (peaklogWriter != null) peaklogWriter.WriteLine("Detect lower cancel"); } #endif var r = new Tuple <float, long>(v, bufferPointer + currentBaseOffset); bufferPointer++; return(r); } Tuple <float, long> readUnit() { Tuple <float, long> t = null; var l = readRawUnit(); if (l == null) { return(null); } if (audioStream.WaveFormat.Channels == 1) { t = l; } else { var r = readRawUnit(); if (r == null) { return(null); } switch (channelType) { case ChannelType.Left: t = l; break; case ChannelType.Right: t = r; break; case ChannelType.Maximum: if (r.Item1 >= 0 && l.Item1 >= 0) { t = new Tuple <float, long>(Math.Max(r.Item1, l.Item1), l.Item2); } else { t = new Tuple <float, long>(Math.Min(r.Item1, l.Item1), l.Item2); } break; default: t = new Tuple <float, long>((r.Item1 + l.Item1) / 2, l.Item2); break; } } #if DEBUG if (waveLogEnabled) { waveRecorder.Add(t); } #endif return(t); } void setSpeed() { TypicalZeroCount = (int)(1.0 / 1200 / 2 * audioStream.WaveFormat.SampleRate); TypicalOneCount = (int)(1.0 / 2400 / 2 * audioStream.WaveFormat.SampleRate); ThresholdPeakCount = (TypicalZeroCount + TypicalOneCount) / 2; if (bVerbose) { Console.WriteLine($"TypicalZeroCount:{TypicalZeroCount} TypicalOneCount:{TypicalOneCount} ThresholdPeakCount:{ThresholdPeakCount}"); } switch (speed) { case Speeds.s300bps: OnePeaks = 8 * 2; ZeroPeaks = 4 * 2; break; case Speeds.s600bps: OnePeaks = 4 * 2; ZeroPeaks = 2 * 2; break; default: OnePeaks = 2 * 2; ZeroPeaks = 1 * 2; break; } } void upperAndLowerPeak() { upperPeak = 0.0f; lowerPeak = 0.0f; for (; ;) { var pair = readUnit(); if (pair == null) { break; } if (upperPeak < pair.Item1) { upperPeak = pair.Item1; } if (lowerPeak > pair.Item1) { lowerPeak = pair.Item1; } } if (bVerbose) { Console.WriteLine($"Detect upperPeak/lowerPeak: {upperPeak}/{lowerPeak}"); } } }
public Fleet SendFleet(Celestial origin, Ships ships, Coordinate destination, Missions mission, Speeds speed, Model.Resources payload) { var request = new RestRequest { Resource = "/bot/planets/" + origin.ID + "/send-fleet", Method = Method.POST, }; request.AddParameter(new Parameter("galaxy", destination.Galaxy, ParameterType.GetOrPost)); request.AddParameter(new Parameter("system", destination.System, ParameterType.GetOrPost)); request.AddParameter(new Parameter("position", destination.Position, ParameterType.GetOrPost)); request.AddParameter(new Parameter("type", (int)destination.Type, ParameterType.GetOrPost)); Buildables buildable; foreach (PropertyInfo prop in ships.GetType().GetProperties()) { int qty = (int)prop.GetValue(ships, null); if (qty == 0) { continue; } if (Enum.TryParse <Buildables>(prop.Name, out buildable)) { request.AddParameter(new Parameter("ships", (int)buildable + "," + prop.GetValue(ships, null), ParameterType.GetOrPost)); } } request.AddParameter(new Parameter("mission", (int)mission, ParameterType.GetOrPost)); request.AddParameter(new Parameter("speed", (int)speed, ParameterType.GetOrPost)); request.AddParameter(new Parameter("metal", payload.Metal, ParameterType.GetOrPost)); request.AddParameter(new Parameter("crystal", payload.Crystal, ParameterType.GetOrPost)); request.AddParameter(new Parameter("deuterium", payload.Deuterium, ParameterType.GetOrPost)); var result = JsonConvert.DeserializeObject <OgamedResponse>(Client.Execute(request).Content); if (result.Status != "ok") { throw new Exception("An error has occurred: Status: " + result.Status + " - Message: " + result.Message); } else { return(JsonConvert.DeserializeObject <Fleet>(JsonConvert.SerializeObject(result.Result))); } }
private static int SendFleet(Celestial origin, Ships ships, Coordinate destination, Missions mission, Speeds speed, Model.Resources payload = null, bool force = false) { Helpers.WriteLog(LogType.Info, LogSender.Tbot, "Sending fleet from " + origin.Coordinate.ToString() + " to " + destination.ToString() + ". Mission: " + mission.ToString() + ". Ships: " + ships.ToString()); UpdateSlots(); if (slots.Free > 1 || force) { if (payload == null) { payload = new Resources { Metal = 0, Crystal = 0, Deuterium = 0 }; } try { Fleet fleet = ogamedService.SendFleet(origin, ships, destination, mission, speed, payload); fleets = ogamedService.GetFleets(); UpdateSlots(); return(fleet.ID); } catch (Exception e) { Helpers.WriteLog(LogType.Error, LogSender.Defender, "Exception: " + e.Message); return(0); } } else { Helpers.WriteLog(LogType.Warning, LogSender.Tbot, "Unable to send fleet, no slots available"); return(0); } }
/** The game database contains "difficulty", "speed", "map", "size", and "civilization" keys, as well as "[player#]-name", and "[player#]-civ" keys (e.g. "1-civ", "2-name") which give information on the other players and their civilizations in the game. */ protected override void ParseDatabaseEntries(Dictionary<string, string> pairs) { // Game info Civilization = pairs["civilization"]; Map = pairs["map"]; Speeds speed; Enum.TryParse(pairs["speed"], out speed); Speed = speed; Difficulties diff; Enum.TryParse(pairs["difficulty"], out diff); Difficulty = diff; MapSizes size; Enum.TryParse(pairs["size"], out size); Size = size; LoadedTurn = int.Parse(pairs["loaded-turn"]); // Players in the game civilizations.Clear(); foreach (KeyValuePair<string, string> entry in pairs) { if (Char.IsNumber(entry.Key[0])) { // player information if the first character is a number char playerNumber = entry.Key[0]; string civilizationName = pairs[playerNumber + "-civ"]; string playerName = pairs[playerNumber + "-name"]; if (!civilizations.Any(x => x.Leader == playerName)) civilizations.Add(new Civilization(civilizationName, playerName)); } } }
/// <summary> /// Adds test iterations per time limiting strategies. /// </summary> /// <param name="speed">list of strategies add</param> public StrategyBuilder AddSpeed(params ISpeedStrategy[] speed) { Speeds = Speeds.Concat(speed).ToArray(); return(this); }