/// <summary> /// Takes the user selected cells from the displayed solution and saves them to a new map file /// </summary> /// <param name="currentSolution"></param> /// <param name="selectedPoints"></param> public static bool ExportSelection(List <Cell> currentSolution, List <Point> selectedPoints) { if (APIClass.CurrentMap == null) { throw new CurrentMapNullException(); } bool result = false; try { List <Cell> chosenOnes = new List <Cell>(); //take current solution and selected points and identify the common elements between them foreach (var p in selectedPoints) { var tmpCell = currentSolution.Find(c => (c.X == p.X && c.Y == p.Y)); if (tmpCell != null) { chosenOnes.Add(tmpCell.CloneJson()); } else { Debug.WriteLine("[ExportSelection] Couldn't find matching Cell element in current solution."); } } //replace those points and apply them to a clone of the CurrentMap var mapClone = CurrentMap.CloneJson(); foreach (var c in chosenOnes) { mapClone.SetCell(c); } //save the newly changed cloned map SaveMapFile(mapClone); result = true; } catch (Exception e) { DebugUtilities.DebugException(e); } return(result); }
/// <summary> /// Saves the Map object passed as an argument to a .lua file, interpretable by the LoG2 game /// </summary> /// <param name="mapToSave"></param> /// <returns></returns> public static bool SaveMapFile(Map mapToSave) //TODO: Change the temp dir to the final dir { _emergencyRestoreMap = CurrentMap.CloneJson(); if (MainForm._lockedCellList != null && MainForm._lockedCellList.Count > 0) { foreach (var c in MainForm._lockedCellList) { //var toReplace = toSave.Cells.Find(cell => (cell.X == c.X && cell.Y == c.Y)); //toReplace = MainForm._lockedCellList.Find(cell2 => (cell2.X == c.X && cell2.Y == c.Y)) ; if (mapToSave.SetCell(c)) { Debug.WriteLine("REPLACED CELL!"); } } } var result = false; StringBuilder sb = new StringBuilder(); Logger.AppendText("Saving new map to:"); Logger.AppendText(DirectoryManager.DungeonFilePath); try { sb.AppendLine("-- This file has been generated by Dungeon Editor 2.1.13 and modified by GA assisted program"); sb.AppendLine("--- level 1 ---"); sb.Append("newMap{\n"); sb.Append("\tname = " + "\"" + mapToSave.Name + "\",\n"); sb.Append("\twidth = " + mapToSave.Width + ",\n"); sb.Append("\theight = " + mapToSave.Height + ",\n"); sb.Append("\tlevelCoord = {" + mapToSave.LevelCoord[0] + "," + mapToSave.LevelCoord[1] + "," + mapToSave.LevelCoord[2] + "},\n"); sb.Append("\tambientTrack = " + "\"" + mapToSave.AmbientTrack + "\",\n"); sb.Append("\ttiles = {\n"); //for (int i = 0; i < toSave.DifferentTiles.Count; i++) //sb.Append("\t\t" + "\"" + toSave.DifferentTiles[i] + "\",\n"); sb.Append("\t\t" + "\"" + _walkableTile.Item1 + "\",\n"); sb.Append("\t\t" + "\"" + _unwalkableTile.Item1 + "\",\n"); sb.Append("\t}\n"); sb.AppendLine("}"); sb.Append("loadLayer(\"tiles\", {\n"); //tiles for (int y = 0; y < mapToSave.Height; y++) { sb.Append("\t"); for (int x = 0; x < mapToSave.Width; x++) { sb.Append(mapToSave.Cells[y * mapToSave.Width + x].CellType + ","); if (x == mapToSave.Width - 1) { sb.Append("\n"); } } } sb.AppendLine("})"); ListQueue <MapElement> fifo = new ListQueue <MapElement>(mapToSave.Elements.Values); while (fifo.Count != 0) { MapElement el = fifo.Dequeue(); sb.Append(el.Print(fifo)); } //start and ending points /*if (mapToSave.StartPoint != null) * { * sb.Append(mapToSave.StartPoint.PrintElement()); * } * if (mapToSave.EndPointList != null && mapToSave.EndPointList.Count > 0) * foreach (var e in mapToSave.EndPointList) * { * sb.Append(e.PrintElement()); * } * * foreach (Cell c in mapToSave.Cells) * { * foreach (MapElement el in c.ElementsInCell) * { * sb.Append(el.PrintElement()); * } * }*/ //foreach (string o in CurrentMap.MapObjects) //{ // sb.Append(o); // sb.Append("\n"); //} ////TODO: HACKERINO, CHECK http://stackoverflow.com/questions/24644464/json-net-type-is-an-interface-or-abstract-class-and-cannot-be-instantiated //foreach (JObject mapElement in toSave.MapElements.Values) //{ // //Logger.AppendText(mapElement.ToString()); // var rep = mapElement.ToString(); // if (rep.Contains("DOOR")) // { // //Logger.AppendText("found door"); // sb.Append((mapElement.ToObject<Door>()).PrintElement().Replace("\n", "")); // } // else if (rep.Contains("LEVER")) // { // //Logger.AppendText("found lever"); // sb.Append((mapElement.ToObject<Lever>()).PrintElement().Replace("\n", "")); // } // //sb.Append(mapElement.PrintElement().Replace("\n", "")); //} APIClass._mapSaved = true; //System.IO.File.WriteAllLines(DirectoryManager.ProjectDir + @"\testSave.lua", sb.ToString().Split(new char[]{'\n'}, StringSplitOptions.RemoveEmptyEntries)); //write back to file System.IO.File.WriteAllLines(DirectoryManager.DungeonFilePath, sb.ToString().Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries)); //write back to file Logger.AppendText("Save successful."); result = true; } catch (Exception e) { Logger.AppendText("Save failed! Check console for report."); Debug.WriteLine("Msg: " + e.Message); Debug.WriteLine("Inner: " + e.InnerException); Debug.WriteLine("Base: " + e.GetBaseException()); return(false); } return(result); }