/// <summary>Activate form for selecting level (levelsets object)</summary> public DialogResult SelectLevel(SokobanLevelSet uLevelSet) { uAssignedLevelSet = uLevelSet;//Store levelset object handle iSelectedLevel = uAssignedLevelSet.GetCurrentLevel();//Get current level to highlight it in list menuShowUnsolved.Checked = !bShowOnlyUnsolvedLevels;//Set checking-flag for menu Enlist();//List levels in listbox DialogResult = DialogResult.Cancel;//Default result of choosing DialogResult bRv = ShowDialog();//Activate choosing in modal mode Dispose();//Release resources of dialog return bRv; }
/// <summary>Update list of levelsets</summary> public FunctionResult UpdateList() { string[] sFilesList; SokobanLevelSet uTempLevelSet = new SokobanLevelSet(); try { sFilesList = System.IO.Directory.GetFiles(sLevelsPath);//Get list of files in folder with levelsets } catch { return FunctionResult.FileSystemError;//List of files not received - error } int i,j; string sNewFile; Array.Sort(sFilesList);//Sort filenames alphabetically for (i = 0; i < sFilesList.Length; i++)//Iterate thru all detected files { sNewFile = System.IO.Path.GetFileName(sFilesList[i]).ToLower();//Get lowercased filename //Search this filename thru list of levelsets for (j = 0; j < iListUsed; j++) { if (uLevelSets[j].sFileName == sNewFile) goto lFound; } //Not found - add to list j = iListUsed;//New item iListUsed++;//Update number of levelsets EnlargeArray();//Realloc array if needed uLevelSets[j].sComment = "";//Initial comment - empty lFound: uLevelSets[j].sFileName = sNewFile;//Update filename in list uTempLevelSet.LoadTxtLevelSet(sFilesList[i],true);//Load levelset with simplified method - to get only number of levels uTempLevelSet.AssignRecordFile(sSolutionsPath + SokobanLevelSet.FileName2sID(uLevelSets[j].sFileName) + ".rec");//Load record file - to get number of solved levels uLevelSets[j].iLevelsTotal = uTempLevelSet.GetLevelsNum();//Get total number of levels uLevelSets[j].iLevelsSolved = uTempLevelSet.GetNumOfSolved();//Get number of solved levels UpdateDisplayed(j);//Update displayed text for levelset } return FunctionResult.OK;//Updated }
/// <summary>Load levelset by file (destination levelset object, filename)</summary> public FunctionResult LoadLevelSet(SokobanLevelSet uLevelSet, string sLoadFileName) { FunctionResult uRV = uLevelSet.LoadTxtLevelSet(sLevelsPath + sLoadFileName,false);//Load levelset file if (uRV != FunctionResult.OK) { return uRV;//Something wrong with loading - exit } uLevelSet.sID = SokobanLevelSet.FileName2sID(sLoadFileName);//Convert filename into ID of levelset uLevelSet.sFileName = sLoadFileName; uLevelSet.AssignRecordFile(sSolutionsPath + uLevelSet.sID + ".rec");//Load record file FindLevelSet(sLoadFileName);//Update current levelset in list return uRV; }
/// <summary>Load levelset by index (destination levelset object, index in list)</summary> public FunctionResult LoadLevelSet(SokobanLevelSet uLevelSet, int iIndex) { if (iIndex>=0 && iIndex<iListUsed) { //Index is valid iCurrentLevelSet = iIndex;//Update index of current levelset return LoadLevelSet(uLevelSet, uLevelSets[iIndex].sFileName);//Load levelset by file } return FunctionResult.NothingToDo;//Index is out of list }
///<summary>Generate null level set - 1 random level (levelset)</summary> public FunctionResult GenNullLevelSet(SokobanLevelSet uLevelSet) { uLevelSet.GenNullLevelSet();//Generate levelset with 1 random level iCurrentLevelSet = -1;//This levelset can not be in list sCurrentLevelSet = uLevelSet.sFileName; return FunctionResult.OK; }
//<summary>Lock level scrolling</summary> //private bool bScrollLock; //################################################################################################################ //Constructor ///<summary>Top-level contructor for SokobanCompact</summary> public formMain() { FunctionResult uRV; InitializeComponent(); hGameSkin = null; uBackBuffer = null; bDeadlockMessage = false; bHaveUnfinishedPosition = false; Cursor.Current = Cursors.WaitCursor;//Waiting cursor - while loading levelset and skin //Get handle of assembly hExecAssem = System.Reflection.Assembly.GetExecutingAssembly(); //Get file name of assembly string sAppFilePath = hExecAssem.GetModules()[0].FullyQualifiedName; //Get path only sApplicationDirectory = System.IO.Path.GetDirectoryName(sAppFilePath); //Add delimiter at the end if (!sApplicationDirectory.EndsWith(@"\")) sApplicationDirectory += @"\"; //Calc paths for folders //sSavesDirectory = sApplicationDirectory + @"Saves\"; sLevelsDirectory = sApplicationDirectory + @"Levels\"; sSolutionsDirectory = sApplicationDirectory + @"Solutions\"; sSkinsDirectory = sApplicationDirectory + @"Skins\"; sBackgroundsDirectory = sApplicationDirectory + @"Backgrounds\"; //Try to create all required folders, to not bother in future try { //System.IO.Directory.CreateDirectory(sSavesDirectory); System.IO.Directory.CreateDirectory(sBackgroundsDirectory); System.IO.Directory.CreateDirectory(sSkinsDirectory); System.IO.Directory.CreateDirectory(sSolutionsDirectory); System.IO.Directory.CreateDirectory(sLevelsDirectory); } catch (System.IO.IOException) {}//Dont know that to do, if creation fails //Calc rectangles on statusbar int iX = 3; int iStatusHeight = pictureStatus.Height;//Height of status bar Graphics uGr1 = CreateGraphics();//Get graphics of current form SizeF uCounterSizes = uGr1.MeasureString(" 0000", Font);//Measure sample string for get actual font sizes int iCounterWidth = (int)uCounterSizes.Width;//Width of counter - with of sample string int iCounterY0 = (int)(iStatusHeight-uCounterSizes.Height)/2;//Text positions - valign center int iSpace = iCounterWidth/10;//Space between fields - 10% if counter width uGr1.Dispose();//Release graphics of form uRectIndic = new Rectangle(iX, (iStatusHeight-16)/2, 16, 16);//for solved/not-solved indicator iX += uRectIndic.Width + iSpace; uRectMoves = new Rectangle(iX, iCounterY0, iCounterWidth, 16);//for counter of moves iX += uRectMoves.Width + iSpace; uRectPushes = new Rectangle(iX, iCounterY0, iCounterWidth, 16);//for counter of pushes iX += uRectPushes.Width + iSpace; uRectMessage = new Rectangle(iX, iCounterY0, ClientRectangle.Width - iX, 16); //all rest space - for non-modal message //Font f1 = this.Font; //f1.me //this.gr //Graphic .MeasureString //bScrollLock = false; bResize = true; iBottomOfForm = pictureStatus.Top;//this done here for perform recenter of level before any redraws //Create and load settings uSetting = new Settings(); if (uSetting.Load(sApplicationDirectory + sConfigFile) != FunctionResult.OK) { //Settings not loaded - first start or failure of file uSetting = new Settings();//reset to default, just in case... } menuScrollLock.Checked = uSetting.bScrollLock; if (uSetting.bLogActions) { uLog = new LogFile(); uLog.Start(sApplicationDirectory + sLogFile); uLog.LogString(ActionID.Start, "Started SokobanCompact; v" + hExecAssem.GetName().Version); } //Load level set list uLevelSetList = new SokobanLevelSetList(sApplicationDirectory + sLevelSetList, sLevelsDirectory, sSolutionsDirectory); uLevelSetList.LoadList(); iSkinSize = 0; //Load skinset uSkinSet = new SkinSet(); //uSkinSet.Load(sSkinsDirectory + "\\" + "oq.sks");//TODO: move skinset name into Settings uSkinSet.Load(sSkinsDirectory + uSetting.sSkinSet); if (!uSetting.bAutosize) { //No autosize? Load skin now uRV = LoadSkin(sSkinsDirectory + uSetting.sSkin); if (iSkinSize == 0) { LogSimpleLine(ActionID.ShowDialog, "Error; Failed to load skin, null skin created; " + uSetting.sSkin+"; "+uRV.ToString()); MessageBox.Show("Failed to load skin '" + uSetting.sSkin + "' \r\nNull skin will be loaded, " + uRV.ToString(), "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); GenNullSkin(); } } UpdateBackground();//Create background according to settings //Create game uGame = new SokobanGame(); //THREAD uBackgroundCalc = new BackgroundThread(BackgroundFinished);//Create thread for background iSelectorMode = 0;//Selector initialy in "play" mode SetToolBarMode();//Refresh selector //Load levelset uLevelSet = new SokobanLevelSet(); uRV = uLevelSetList.LoadLevelSet(uLevelSet, uSetting.sLastLevelSet); if (uRV != FunctionResult.OK) { //Something happens with levelset file LogSimpleLine(ActionID.ShowDialog, "Error; Failed to load levelset, random will be generated; " + uSetting.sLastLevelSet+"; "+uRV.ToString()); MessageBox.Show("Unable to load LevelSet '" + uSetting.sLastLevelSet + "', result: " + uRV.ToString() + "\r\nRandom level will be loaded.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); uLevelSetList.GenNullLevelSet(uLevelSet);//Generate levelset with 1 random level uSetting.iLastLevelPlayed = 0;//Res } //Start last played level uRV = LoadLevel(uGame, uSetting.iLastLevelPlayed); if (uRV == FunctionResult.OK) { //Loaded successfully AfterLoadLevel(); } else { //Level not loaded (only variant - FunctionResult.OutOfLevelSet) LogSimpleLine(ActionID.ShowDialog, "Error; Failed to load level, random will be choosen; " + uSetting.iLastLevelPlayed.ToString() + "; " + uRV.ToString()); MessageBox.Show("Unable to load level " + uSetting.iLastLevelPlayed.ToString() + ", result: " + uRV.ToString() + "\r\nRandom level will be selected.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); ActionRandLevel(); } if (iSkinSize < 1) { LogSimpleLine(ActionID.ShowDialog, "Error; No skin loaded, null skin created"); MessageBox.Show("Failed to load skin\r\nNull skin will be loaded", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); GenNullSkin(); RecenterLevel(); } NonModalMessage(uLevelSet.sTitle + ", " + uGame.sTitle); Cursor.Current = Cursors.Default;//remove wait cursor }