private void reconstruct_path(BidimensionalArray <Coord?> came_from, Coord current_node) { if (came_from[current_node] != null) { path.Add((Coord)current_node); reconstruct_path(came_from, (Coord)came_from[current_node]); } }
private void createRoom(Rectangle room, BidimensionalArray <Atom> map) { for (int x = room.Left + 1; x < room.Right; x++) { for (int y = room.Top + 1; y < room.Bottom; y++) { AddAtom(new Floor(new Coord(x, y))); } } }
private void createVerticalTunnel(BidimensionalArray <Atom> table, int y1, int y2, int x) { for (int y = Math.Min(y1, y2); y <= Math.Max(y1, y2); y++) { AddAtom(new Floor(new Coord(x, y))); } }
//def place_objects_all_rooms(con, map, objects): // for room in rooms: // place_objects(con, map, room, objects) //def place_objects(con, map, room, objects): // num_monsters = libt.random_get_int(0,0,MAX_ROOM_MONSTERS) // for i in range(num_monsters): // x = libt.random_get_int(0, room.x1, room.x2) // y = libt.random_get_int(0, room.y1, room.y2) // if not tile.is_blocked(x, y, map, objects): // if libt.random_get_int(0,0,100) < 80: #80% di possibilita di pupparsi un orco // monster = Object(con, x, y,'o','Orc', libt.desaturated_green, blocks = True) // else: // monster = Object(con, x, y,'T','Troll', libt.darker_green, blocks = True) // objects.append(monster) // monster.insertInMap(map) private void createHorizontalTunnel(BidimensionalArray <Atom> table, int x1, int x2, int y) { for (int x = Math.Min(x1, x2); x <= Math.Max(x1, x2); x++) { AddAtom(new Floor(new Coord(x, y))); } }
private void createEmptyTable(out BidimensionalArray <Atom> table) { table = new BidimensionalArray <Atom>(Height, Width, (pos) => new Floor(pos)); //for (int r = 0; r < table.Rows; r++) //{ // for (int c = 0; c < table.Cols; c++) // { // table[r, c] = new Floor(new Coord() { X = c, Y = r }); // } //} }
public Map(SerializationInfo info, StreamingContext context) { name = (string)info.GetValue(nameSerializableName, typeof(string)); table = (BidimensionalArray <Atom>)info.GetValue(tableSerializableName, typeof(BidimensionalArray <Atom>)); buffer = (BidimensionalArray <Atom>)info.GetValue(bufferSerializableName, typeof(BidimensionalArray <Atom>)); dark = (BidimensionalArray <bool>)info.GetValue(darkSerializableName, typeof(BidimensionalArray <bool>)); explored = (BidimensionalArray <TernaryValue>)info.GetValue(exploredSerializableName, typeof(BidimensionalArray <TernaryValue>)); untangibles = (BidimensionalArray <AtomCollection>)info.GetValue(untangiblesSerializableName, typeof(BidimensionalArray <AtomCollection>)); playerInitialPosition = (Coord)info.GetValue(playerInitialPosSerializableName, typeof(Coord)); //views = (List<IMapViewer>)info.GetValue(viewsSerializableName, typeof(List<IMapViewer>)); this.views = new List <IMapViewer>(); table.ForEach(a => a.SetMap(this)); buffer.ForEach(a => a.SetMap(this)); untangibles.ForEach(uL => uL.ForEach(a => a.SetMap(this))); }
private int findMin(List <Coord> openset, BidimensionalArray <int> f_score) { var min = f_score[openset[0]]; var pos = 0; for (int i = 1; i < openset.Count; i++) { var val = f_score[openset[i]]; if (val < min) { pos = i; min = val; } } return(pos); }
public Map(string name, Coord playerInitialPosition, BidimensionalArray <Atom> table, TernaryValue notToExplore, bool dark) { this.name = name; this.playerInitialPosition = playerInitialPosition; this.table = table; this.table.ForEach(a => a.SetMap(this)); this.buffer = new BidimensionalArray <Atom>(table.Rows, table.Cols, (pos) => { var f = new Floor(pos); f.SetMap(this); return(f); }); this.untangibles = new BidimensionalArray <AtomCollection>(table.Rows, table.Cols, () => new AtomCollection()); this.explored = new BidimensionalArray <TernaryValue>(table.Rows, table.Cols, notToExplore); this.dark = new BidimensionalArray <bool>(table.Rows, table.Cols, dark); this.views = new List <IMapViewer>(); table.ForEach(atom => atom.InsertInMap(this, atom.Position, true)); }
public Map Create() { //var table = new BidimensionalArray<Atom>(Height, Width); BidimensionalArray <Atom> table; var explored = new BidimensionalArray <bool>(Height, Width); Map map = null; switch (MapCreationMode) { case TableCreationMode.FromFile: LoadFromFile(@"currentLevel.map", out map); table = null; break; case TableCreationMode.FromGameFile: LoadFromFile(out map); table = null; break; case TableCreationMode.Random: makeMap(out table); break; case TableCreationMode.Empty: default: createEmptyTable(out table); break; } if (map == null) { map = new Map(Name, PlayerInitialPosition, table, Explored, !Lightened); foreach (var atom in elements) { //map.Insert(atom); if (singleMsgListeners != null && atom.SupportsSingleMsgListener()) { singleMsgListeners.ForEach(l => atom.RegisterView(l)); } atom.InsertInMap(map, atom.Position, true); } } else { if (singleMsgListeners != null) { map.EveryAtom().ForEach(atom => { singleMsgListeners.ForEach(l => atom.RegisterView(l)); }); } } for (int i = 0; i < views.Count; i++) { map.RegisterView(views[i]); } return(map); }
private void makeMap(out BidimensionalArray <Atom> map, int MAX_ROOMS = 20, int ROOM_MIN_SIZE = 3, int ROOM_MAX_SIZE = 10) { map = new BidimensionalArray <Atom>(Height, Width, () => new Wall()); List <Rectangle> rooms = new List <Rectangle>(MAX_ROOMS); int num_rooms = 0; int maxMinusMin = ROOM_MAX_SIZE - ROOM_MIN_SIZE; for (int r = 0; r < MAX_ROOMS; r++) { //W e H random int w = Dice.Throws(maxMinusMin) + ROOM_MIN_SIZE - 1; int h = Dice.Throws(maxMinusMin) + ROOM_MIN_SIZE - 1; // Random position into map borders int x = Dice.Throws(Width - w - 1) - 1; int y = Dice.Throws(Height - h - 1) - 1; var new_room = new Rectangle(x, y, w, h); var failed = false; foreach (var other_room in rooms) { if (new_room.IntersectsWith(other_room)) { failed = true; break; } } if (!failed) { createRoom(new_room, map); var newCoord = new_room.Center(); if (num_rooms == 0) { PlayerInitialPosition = newCoord; } else { var prevCoord = rooms[num_rooms - 1].Center(); if (Dice.Throws(2) == 1) { createHorizontalTunnel(map, prevCoord.X, newCoord.X, prevCoord.Y); createVerticalTunnel(map, prevCoord.Y, newCoord.Y, newCoord.X); } else { createVerticalTunnel(map, prevCoord.Y, newCoord.Y, newCoord.X); createHorizontalTunnel(map, prevCoord.X, newCoord.X, prevCoord.Y); } } rooms.Add(new_room); num_rooms++; } } }
private void reconstruct_path(BidimensionalArray<Coord?> came_from, Coord current_node) { if (came_from[current_node] != null) { path.Add((Coord)current_node); reconstruct_path(came_from, (Coord)came_from[current_node]); } }
public AStar(Map map, Coord start, Coord goal) { this.path = new List<Coord>(); Success = false;//failure var closedset = new List<Coord>(); var openset = new List<Coord>(); var g_score = new BidimensionalArray<int>(map.Height, map.Width); var h_score = new BidimensionalArray<int>(map.Height, map.Width); var f_score = new BidimensionalArray<int>(map.Height, map.Width); var came_from = new BidimensionalArray<Coord?>(map.Height, map.Width); openset.Add(start);// The set of tentative nodes to be evaluated, initially containing the start node came_from[start] = null; g_score[start] = 0; // Cost from start along best known path. h_score[start] = heuristic_cost_estimate(start, goal); f_score[start] = g_score[start] + h_score[start]; // Estimated total cost from start to goal through y. while (openset.Count > 0) { var x = findMin(openset, f_score); if (openset[x] == goal) { reconstruct_path(came_from, (Coord)goal); Success = true;//success break; } //add x to closedset closedset.Add(openset[x]); //remove x from openset openset.RemoveAt(x); var neighbours = new List<Coord>(); findNeighbours(map, closedset[closedset.Count - 1], neighbours); bool tentative_is_better = false; //foreach y in neighbor_nodes(x) int current = 0; foreach (var coord in neighbours) { if (closedset.Contains(neighbours[current])) { current++; continue; } int tentative_g_score = g_score[closedset[closedset.Count - 1]] + 1; if (!openset.Contains(neighbours[current])) //y not in openset { //add y to openset openset.Add(neighbours[current]); h_score[neighbours[current]] = heuristic_cost_estimate(neighbours[current], goal); tentative_is_better = true; } else { if (tentative_g_score < g_score[neighbours[current]]) { tentative_is_better = true; } else { tentative_is_better = false; } } if (tentative_is_better == true) { came_from[neighbours[current]] = closedset[closedset.Count - 1]; g_score[neighbours[current]] = tentative_g_score; f_score[neighbours[current]] = g_score[neighbours[current]] + h_score[neighbours[current]]; } current++; } } }
private int findMin(List<Coord> openset, BidimensionalArray<int> f_score) { var min = f_score[openset[0]]; var pos = 0; for (int i = 1; i < openset.Count; i++) { var val = f_score[openset[i]]; if (val < min) { pos = i; min = val; } } return pos; }
public AStar(Map map, Coord start, Coord goal) { this.path = new List <Coord>(); Success = false;//failure var closedset = new List <Coord>(); var openset = new List <Coord>(); var g_score = new BidimensionalArray <int>(map.Height, map.Width); var h_score = new BidimensionalArray <int>(map.Height, map.Width); var f_score = new BidimensionalArray <int>(map.Height, map.Width); var came_from = new BidimensionalArray <Coord?>(map.Height, map.Width); openset.Add(start);// The set of tentative nodes to be evaluated, initially containing the start node came_from[start] = null; g_score[start] = 0; // Cost from start along best known path. h_score[start] = heuristic_cost_estimate(start, goal); f_score[start] = g_score[start] + h_score[start]; // Estimated total cost from start to goal through y. while (openset.Count > 0) { var x = findMin(openset, f_score); if (openset[x] == goal) { reconstruct_path(came_from, (Coord)goal); Success = true;//success break; } //add x to closedset closedset.Add(openset[x]); //remove x from openset openset.RemoveAt(x); var neighbours = new List <Coord>(); findNeighbours(map, closedset[closedset.Count - 1], neighbours); bool tentative_is_better = false; //foreach y in neighbor_nodes(x) int current = 0; foreach (var coord in neighbours) { if (closedset.Contains(neighbours[current])) { current++; continue; } int tentative_g_score = g_score[closedset[closedset.Count - 1]] + 1; if (!openset.Contains(neighbours[current])) //y not in openset { //add y to openset openset.Add(neighbours[current]); h_score[neighbours[current]] = heuristic_cost_estimate(neighbours[current], goal); tentative_is_better = true; } else { if (tentative_g_score < g_score[neighbours[current]]) { tentative_is_better = true; } else { tentative_is_better = false; } } if (tentative_is_better == true) { came_from[neighbours[current]] = closedset[closedset.Count - 1]; g_score[neighbours[current]] = tentative_g_score; f_score[neighbours[current]] = g_score[neighbours[current]] + h_score[neighbours[current]]; } current++; } } }