0

I am trying to calculate the amount of links between one destination and another in my game, the first method that gets called is CalculateRoute and it returns a list of map routes.

At this point you're probably thinking "so whats wrong with the code you've shown?".

When the link is further than 1 room away, it doesn't take into account that it needs to get the links from the first two rooms and just ignores the rest...

Better explain, it only gets the link instructions from the room it starts from inside of the method GetRoutesForRoom and this causes a big problem for the whole mechanism.

I'm already confused by this code so I'm just asking for some help to make it count the first parts of the link inside of the GetRoutesForRoom method.

internal class MapRoute
{
    private List<int> _arrowLinks;

    public MapRoute(List<int> arrowLinks)
    {
        _arrowLinks = arrowLinks;
    }
}

Here is the CalculateRoute method...

public Dictionary<int, MapRoute> CalculateRoute(Player player, Room startLocation, Room endLocation)
{
    var possibleRoutes = new Dictionary<int, MapRoute>();

    var arrowsAtStart = startLocation.GetRoomItemHandler().GetFloor.Where(
        x => x.GetBaseItem().InteractionType == InteractionType.ARROW);

    foreach (var arrow in arrowsAtStart)
    {
        if (!ItemTeleporterFinder.IsTeleLinked(arrow.Id, startLocation))
        {
            continue;
        }

        var linkedRoomId = ItemTeleporterFinder.GetTeleRoomId(arrow.Id, startLocation);

        if (linkedRoomId == endLocation.RoomId)
        {
            possibleRoutes.Add(possibleRoutes.Count + 1, new MapRoute(new List<int> { arrow.Id }));
        }
        else if (PlusEnvironment.GetGame().GetRoomManager().TryGetRoom(linkedRoomId, out var secondRoom))
        {
            foreach (var mapRoute in GetRoutesForRoom(secondRoom, startLocation.RoomId))
            {
                possibleRoutes.Add(possibleRoutes.Count + 1, mapRoute);
            }
        }
    }

    return possibleRoutes;
}

For calculating rooms further down the link, I use another method...

public List<MapRoute> GetRoutesForRoom(Room room, int destination)
{
    var possibleRoutes = new List<MapRoute>();

    var arrowsInRoom = room.GetRoomItemHandler().GetFloor.Where(
        x => x.GetBaseItem().InteractionType == InteractionType.ARROW);

    foreach (var arrow in arrowsInRoom)
    {
        if (!ItemTeleporterFinder.IsTeleLinked(arrow.Id, room))
        {
            continue;
        }

        var linkedRoomId = ItemTeleporterFinder.GetTeleRoomId(arrow.Id, room);

        if (linkedRoomId == destination)
        {
            possibleRoutes.Add(new MapRoute(new List<int> { arrow.Id }));
        }
        else if (PlusEnvironment.GetGame().GetRoomManager().TryGetRoom(linkedRoomId, out var secondRoom))
        {
            foreach (var mapRoute in GetRoutesForRoom(secondRoom, destination))
            {
                possibleRoutes.Add(mapRoute);
            }
        }
    }

    return possibleRoutes;
}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Please go over [how to ask](https://stackoverflow.com/help/how-to-ask) and [on-topic](https://stackoverflow.com/help/on-topic) again and if you have questions provide your code as [mvce](https://stackoverflow.com/help/mcve). Your code lacks certain things to be a mvsce - namely that I can copy it and use it in my IDE to reproduce the error. You need a debugger and step through that and see where you are missing steps. – Patrick Artner Jan 21 '18 at 19:20
  • Firstly, to include all of my code it would be over 1,000 files, its a whole game. Secondly, I'm not trying to reproduce and error, I'm trying to understand how to add a certain functionality. – Harry Evans Jan 21 '18 at 19:23
  • SO is for solveable questions and errors in code. you are asking a "How to ...." question wich is ill suited for SO. You have a pathing problem, so read up on path algos,f.e. here: https://stackoverflow.com/questions/221311/what-is-the-most-efficient-way-of-finding-a-path-through-a-small-world-graph - keywords A*, Dijkstra's ( for the "best" one) - if you need all, search all paths, you do not need them, simply explore all connections until done - every posiibilities every room, collect the individual routes and return them. Beware of loops, look for graph-searches. – Patrick Artner Jan 21 '18 at 19:37

0 Answers0