/// <summary> /// Determines if the condition to stop advancing the board state is met /// </summary> /// <param name="condition"></param> /// <param name="curStep"></param> /// <param name="curNoteCount"></param> /// <param name="curMsPassed"></param> /// <param name="nrOfStepsToProcess"></param> /// <param name="nrOfNotesToPlay"></param> /// <param name="nrOfMsToPass"></param> /// <param name="progress"></param> /// <returns></returns> private bool ExportConditionMet(ExportConditionEnum condition, int curStep, int curNoteCount, int curMsPassed, int nrOfStepsToProcess, int nrOfNotesToPlay, int nrOfMsToPass, out double progress) { if (condition == ExportConditionEnum.StepLimit) { progress = curStep / (double)nrOfStepsToProcess; return(curStep >= nrOfStepsToProcess); } else if (condition == ExportConditionEnum.NoteLimit) { progress = curNoteCount / (double)nrOfNotesToPlay; return(curNoteCount >= nrOfNotesToPlay); } else if (condition == ExportConditionEnum.TimeLimit) { progress = curMsPassed / (double)nrOfMsToPass; return(curMsPassed >= nrOfMsToPass); } else { progress = 0; return(true); } }
/// <summary> /// Generate notes from the board /// </summary> /// <returns></returns> private List <Note> GenerateNotes() { pbProgress.Visible = true; lblStatus.Text = "Generating..."; // determine ending value and condition int nrOfStepsToProcess = (int)nudNumberOfSteps.Value; int nrOfNotesToPlay = (int)nudNumberOfNotes.Value; int nrOfMsToPass = (int)nudTimeElapsed.Value * 1000; ExportConditionEnum condition = ExportConditionEnum.TimeLimit; if (rdbNumberOfNotes.Checked) { condition = ExportConditionEnum.NoteLimit; } else if (rdbNumberOfSteps.Checked) { condition = ExportConditionEnum.StepLimit; } else if (rdbTimeElapsed.Checked) { condition = ExportConditionEnum.TimeLimit; } // keep track of steps since last note to prevent infinite loop int stepsSinceLastNote = 0; int curStep = 0; int curNoteCount = 0; int curMsPassed = 0; DateTime startTime = DateTime.Now; // take a deep copy of the board settings (don't want to alter the current board) BoardSettings boardsettings; if (!BoardMapper.TryGetBoardSettingsFromString(BoardMapper.GetStringFromBoardSettings(boardSettings), out boardsettings)) { MessageBox.Show("Could not create a copy of the current board", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); return(null); } List <Note> notes = new List <Note>(); double progress; // continue advancing to next state until either cancelled or condition is met while (!cancelGenerating && !ExportConditionMet(condition, curStep, curNoteCount, curMsPassed, nrOfStepsToProcess, nrOfNotesToPlay, nrOfMsToPass, out progress)) { // advance board boardsettings.Board.NextState(); // get the active notes of the board var activeNotes = GetNotesForActiveCells(boardsettings); // and update the correct time played foreach (var n in activeNotes) { n.TimeDown = startTime.AddMilliseconds(curMsPassed); } // and add them to the notes list notes.AddRange(activeNotes); curStep++; curNoteCount += activeNotes.Count; curMsPassed += boardsettings.Speed; // check steps since last note if (activeNotes.Count == 0) { stepsSinceLastNote++; } else { stepsSinceLastNote = 0; } // prevent infinite loop when no notes are played anymore, ask user to continue if (stepsSinceLastNote > 10000) { DialogResult result = MessageBox.Show("There have been no notes for 10000 steps now, do you want to continue generating?", "No notes for a long time", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == System.Windows.Forms.DialogResult.No) { break; } else { stepsSinceLastNote = 0; // reset steps since last note } } // update gui each 100 steps if (curStep % 100 == 0) { pbProgress.Value = (int)(progress * 100); lblStatus.Text = "Generating... | Current iteration " + curStep; Application.DoEvents(); } } pbProgress.Visible = false; if (notes.Count <= 0) { lblStatus.Text = "No midi generated, there weren't any notes to record"; } else { lblStatus.Text = "Midi generated"; } return(notes); }