/// <summary> /// replace occurence of the template in the map by another template, based on a roll. /// The operations allows to perform the same action on mirrors / rotation of the template. /// In this case, the same operations will be performed on the result template /// </summary> /// <param name="map">the map to check the templace for</param> /// <param name="result">the replacing template</param> /// <param name="chance">the chance that the template will be replaced by the result, for each match. Must be between 0 and 100</param> /// <returns>true if at least one replacement was made </returns> public static bool Replace(char[,] map, char[,] toFind, char[,] toReplace, int chance, String operations, bool strict = false) { Random rnd = new Random(111); bool value = false; bool done = false; List <char[, ]> patterns = CharUtils.CreateMirrors(toFind, operations); List <char[, ]> results = CharUtils.CreateMirrors(toReplace, operations); for (int i = 0; i < patterns.Count; i++) { // basic checks; if (patterns[i].GetLength(0) < map.GetLength(0) && patterns[i].GetLength(1) < map.GetLength(1)) { // for each postion on the map for (int y1 = 0; y1 < map.GetLength(0) - patterns[i].GetLength(0) + 1; y1++) { for (int x1 = 0; x1 < map.GetLength(1) - patterns[i].GetLength(1) + 1; x1++) { // if it matche value = Match(map, patterns[i], x1, y1); if (value) { if (rnd.Next(100) < chance) { Place(map, results[i], x1, y1); done = true; } } } } } } return(done); }
/// <summary> /// check the template against a given map and return the list of position where the template matche /// </summary> /// <param name="map">The map to chek the template against</param> /// <param name="template">The template to chek </param> /// <param name="operation">operations to perform on the template (RXY) </param> /// <returns>List of position where the template Matches</returns> public static List <Position> Matches(char[,] map, char[,] template, String operations, bool strict = false) { List <char[, ]> patterns = CharUtils.CreateMirrors(template, operations); List <Position> response = new List <Position>(); bool value = false; foreach (char[,] pattern in patterns) { if (pattern.GetLength(0) <= map.GetLength(0) && pattern.GetLength(1) <= map.GetLength(1)) { // for each postion on the map for (int y1 = 0; y1 < map.GetLength(0) - pattern.GetLength(0) + 1; y1++) { for (int x1 = 0; x1 < map.GetLength(1) - pattern.GetLength(1) + 1; x1++) { value = Match(map, pattern, x1, y1, strict); if (value) { response.Add(new Position(x1, y1)); } } } } } return(response); }
/// <summary> /// Read a room in the "0" FileFormat /// First line is XSize ///Second Line is YSize ///Third line are operation to generate Mirror rooms (Rotate : X, H Mirror : X, V Mirror : Y) ///Followed by YSize lines of XSize chars /// </summary> /// <param name="reader"></param> public static void Read_FileFormat_1(StreamReader reader, ReplacementRuleList templates) { try { int priority = 1; int xsize = 0; int ysize = 0; int chance = 100; Int32.TryParse(reader.ReadLine(), out priority); Int32.TryParse(reader.ReadLine(), out xsize); Int32.TryParse(reader.ReadLine(), out ysize); string operations = reader.ReadLine(); char[,] initialData = loadData(reader, xsize, ysize); Int32.TryParse(reader.ReadLine(), out chance); char[,] replacementData = loadData(reader, xsize, ysize); // if requested, create mirrored copies List <char[, ]> initialMirrors = CharUtils.CreateMirrors(initialData, operations); List <char[, ]> replacementMirrors = CharUtils.CreateMirrors(replacementData, operations); for (int i = 0; i < initialMirrors.Count; i++) { MapTemplate initial = new MapTemplate(initialMirrors[i]); MapTemplate replacement = new MapTemplate(replacementMirrors[i]); ReplacementRule replace = new ReplacementRule(); replace.InitialContent = initial; replace.ReplacementContent = replacement; replace.Priority = priority; replace.Chance = chance; if (replace.Check()) { templates.Add(replace); } } } catch (Exception e) { Logger.Error(e.Message); Console.WriteLine(e.Message); } }
/// <summary> /// Read a pattern in the "0" FileFormat /// First line is XSize ///Second Line is YSize ///Third line are operation to generate Mirror pattern (Rotate : X, H Mirror : X, V Mirror : Y) ///Followed by YSize lines of XSize chars /// </summary> /// <param name="reader"></param> public static void Read_FileFormat_0(StreamReader reader, MapTemplateList templates) { try { int xsize = 0; int ysize = 0; Int32.TryParse(reader.ReadLine(), out xsize); Int32.TryParse(reader.ReadLine(), out ysize); string operations = reader.ReadLine(); char[,] roomData = loadData(reader, xsize, ysize); // if requested, create mirrored copies List <char[, ]> mirrors = CharUtils.CreateMirrors(roomData, operations); int count = 0; int sourceId = 0; MapTemplate tmp; foreach (char[,] copy in mirrors) { if (count == 0) { tmp = new MapTemplate(copy); sourceId = tmp.Id; CharUtils.saveAsImage($"./assets/images/{templates._name}_{tmp.Id}.png", copy); } else { tmp = new MapTemplate(copy, sourceId); } templates.Add(tmp); //CharUtils.saveAsImage($"./assets/images/{templates._name}_{tmp.Id}.png", copy); count++; } } catch (Exception e) { Logger.Error(e.Message); Console.WriteLine(e.Message); } }