/// <summary> /// Load a room from a file /// </summary> /// <param name="sourceFile">path of the file to use</param> public static void loadFromFile(string sourceFile, MapTemplateList templates) { Logger.Info($"Reading template in file {sourceFile}"); Logger.Push(); try { int format = 0; StreamReader reader = new StreamReader(sourceFile); Int32.TryParse(reader.ReadLine(), out format); switch (format) { case 0: Read_FileFormat_0(reader, templates); break; default: break; } } catch (Exception e) { Logger.Error(e.Message); Console.WriteLine(e.Message); } Logger.Pop(); }
/// <summary> /// Load all template files in the given directory /// </summary> /// <param name="sourceDirectory"> the directpry where to search for templates</param> /// <return> a list of all the templ ate </return> public static MapTemplateList loadFromDirectory(String sourceDirectory, string name) { MapTemplateList templates = new MapTemplateList(name); loadFromDirectory(sourceDirectory, templates); return(templates); }
/// <summary> ///Generate a random entry to be placed on the map /// </summary> /// <param name="map">the map where the entry would be placed</param> /// <param name="doors">the list of available door type</param> /// <returns>a position & door type</returns> private PatternPosition generateEntry(Map map, MapTemplateList doors) { MapTemplate door = doors.getTemplate(rnd.Next(doors.Count())); PatternPosition entry = new PatternPosition(); entry.y = rnd.Next(1, map.XSize); entry.x = rnd.Next(1, map.YSize); entry.id = door.Id; Logger.Info($"Putting Entry door type {door.Id} at {entry.x}x{entry.y}"); return(entry); }
public MapGenerator1(MapTemplateList rooms, MapTemplateList doors, ReplacementRuleList modifications) { Logger.Info($"Creating a map generator ", Logger.LogAction.PUSH); Logger.Info($"with {rooms.Count()} rooms"); Logger.Info($"with {doors.Count()} type of doors"); Logger.Info($"with {modifications.collection.Count} possible replacement"); _rooms = rooms; _doors = doors; _modifications = modifications; Debug = true; // put rooms into collections grouped by door type InitializeRooms(); Logger.Pop(); }
/// <summary> /// Load all template files in the given directory and put them in the given list /// </summary> /// <param name="sourceDirectory"> the directpry where to search for templates</param> /// <param name="templates"> The liust of template to add the templates to</param> public static void loadFromDirectory( string sourceDirectory, MapTemplateList templates ) { try { Logger.Info($"Reading templates in directory {sourceDirectory} into {templates._name}"); Logger.Push(); var txtFiles = Directory.EnumerateFiles(sourceDirectory, "*.rm1"); foreach (string currentFile in txtFiles) { loadFromFile(currentFile, templates); } templates .collection .Sort(delegate(MapTemplate x, MapTemplate y) { var xx = x.YSize * x.XSize; var yy = y.YSize * y.XSize; if (xx == yy) { return(0); } if (xx < yy) { return(-1); } else { return(1); } }); } catch (Exception e) { Logger.Error(e.Message); Console.WriteLine(e.Message); } Logger.Pop(); }
/// <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); } }
static void Main(string[] args) { Logger.initialisation(); // get char groups CharGroupsLoader.loadFromDirectory("./assets/"); // get the rooms & doors template MapTemplateList rooms = MapTemplateLoader.loadFromDirectory("./assets/rooms", "rooms"); // get the rooms & doors template MapTemplateList doors = MapTemplateLoader.loadFromDirectory("./assets/doors", "doors"); // get the cleaning template rules ReplacementRuleList modifications = ReplacementRuleLoader.loadFromDirectory("./assets/modifications", "modifications"); // create the map Map map = new Map(75, 75); // create the generator & call it MapGenerator1 generator = new MapGenerator1(rooms, doors, modifications); generator.GenerateMap(map); CharUtils.saveAsImage("./assets/map.png", map.Content); }
/// <summary> /// Try to place a room, given an exit position ( ie door ). The room will be taken in the list of room having this kind of exit. /// if a room is placed, new exits will be adde to the list, and old one (common to another room) removed. /// </summary> /// <param name="exit">the exit position & pattern</param> /// <param name="availablesExits">the list of available exit</param> /// <returns>true if a room was placed</returns> private bool PlaceRoomForDoor(Map map, PatternPosition exit, List <PatternPosition> availablesExits, List <PatternPosition> usedRooms, List <PatternPosition> rejectedExits) { ; MapTemplateList possiblesRooms = doorGroups[exit.id]; possiblesRooms.collection.Shuffle(); possiblesRooms.collection.Sort( delegate(MapTemplate x, MapTemplate y) { var xx = x.SortValue - x.Usage; var yy = y.SortValue - y.Usage; if (xx == yy) { return(0); } if (xx < yy) { return(1); } else { return(-1); } } ); bool placed = false; // for all room that contains our exit //possiblesRooms.Reset(); foreach (MapTemplate room in possiblesRooms) { // get all position in this room of the exi we're working on List <Position> possibleExits = _doors.Find(exit.id).Matches(room, true); possibleExits.Shuffle(); foreach (Position pos in possibleExits) { // check if the room can be put, mapping the choosen room exit with the exit we were given if (CheckRoom(map, room, exit.x - pos.X, exit.y - pos.Y, usedRooms)) { // place the room PlaceRoom(map, room, exit.x - pos.X, exit.y - pos.Y, availablesExits, usedRooms); // change all usage of room with the same SourceId UpdateUsage(room); // flag the map as modified placed = true; break; // leave the search for a valid exit } } // if a room has been placed onto the exit, leave the search for a room if (placed) { break; } } // if no room found, remove the exit from the list of available (if placed, this has already be done) if (!placed) { availablesExits.RemoveAll(template => (exit.x == template.x && exit.y == template.y && exit.id == template.id)); rejectedExits.Add(exit); } return(placed); }