Curious about the efficiency of this code (preparing to implement Dijkstra's algorithm).

Hello everyone, I recently got back into making a game along the lines of Advance Wars, and I’m finding that I’m making quick progress after a long break. I don’t want to bore you all with the details there though, so let’s jump to it.

I am setting up the following situation in preparation to make my first test of dijkstra’s algorithm. As I’m sure anyone familiar with the AW series or any turn-based strategy game would know. I have units and terrain I am dealing with. The units have movement types that do better or worse on certain terrain. Therefore, I have to implement a movement cost for each unit on each terrain type.

Ultimately, I’m looking for a critique of this code from more experience programmers. I am playing this by ear with no formal training. I’ve heard that a large quantity of if statements is supposed to be bad, and I have 13, with switch statements nested inside.

Here is the code itself:

public class TerrainData : MonoBehaviour {

/*I have about 13 terrain tiles. Each one will have this script component "TerrainData". The terrain type will be set in the editor for the prefab terrain game objects, which should take care of any terrain instance that is created.*/

    enum movementType { foot, mech, tireA, tireB, tread, flying, naval };
    public enum terrainType { plain, forest, river, mountain, sea, road, shore, fog, city, unitBase, airport, port, headquarters };
    public terrainType terrain = terrainType.plain;


    public bool occupied = false;

/*Dijkstra's algorithm will call the function below to determine the movement cost of the currently selected unit. The movement type parameter from the unit will be set within the algorithm*/

    int GetMovementCostA(movementType m)
    {
        int movementCost=1;

/*I am defining only the plain terrain for this code snippet.*/

/*I elected to use this method where every terrain shares the same Data script because the  Dijkstra script can then make one call to the function "GetMovementCostA()" and this will work for any terrain instead of having unique call functions for each terrain*/


        if (terrain == terrainType.plain)
        {
            switch (m)
            {
                case movementType.foot:
                    movementCost = 1;
                    break;
                case movementType.mech:
                    movementCost = 1;
                    break;
                case movementType.tireA:
                    movementCost = 1;
                    break;
                case movementType.tireB:
                    movementCost = 2;
                    break;
                case movementType.tread:
                    movementCost = 1;
                    break;
                case movementType.flying:
                    movementCost = 1;
                    break;
                case movementType.naval:
                    movementCost = 9999;
                    break;
                default:
                    movementCost = 1;
                    break;
            }
/*As a side question, I noticed that I can't use "else if" statements in my code without generating error (sorry I don't have it infront of me); so the if statement is repeated 13 times, and if my understanding is correct, that means there will be 13 checks for terrain type even if the first condition is met. This seems somewhat bad, but this function isn't called on an update function, so I guess this is okay?*/
        }
      
...

        return movementCost;
    }

Thanks for any help!

Hey LemonCookie,
in your case is it probably better to use a static array for the movementCost like this:

enum movementType { foot, mech, tireA, tireB, tread, flying, naval };
public enum terrainType { plain, forest, river, mountain, sea, road, shore, fog, city, unitBase, airport, port, headquarters };
public terrainType terrain = terrainType.plain;

static int[,] cost = new int[,] {
    { 0, 1, 2, 2, 3, 4, 5 }, // plain
    { 1, 1, 2, 2, 3, 4, 5 }, // forest
    { 1, 1, 2, 2, 3, 4, 5 }, // river
    { 1, 1, 2, 2, 3, 4, 5 }, // ...
    { 1, 1, 2, 2, 3, 4, 5 },
    { 1, 1, 2, 2, 3, 4, 5 },
    { 1, 1, 2, 2, 3, 4, 5 },
    { 1, 1, 2, 2, 3, 4, 5 },
    { 1, 1, 2, 2, 3, 4, 5 },
    { 1, 1, 2, 2, 3, 4, 5 },
    { 1, 1, 2, 2, 3, 4, 5 },
    { 1, 1, 2, 2, 3, 4, 5 },
    { 1, 1, 2, 2, 3, 4, 7 },
};

Since the enumerators are just numbers internally (plain=0, forest=1 etc.) you can get the cost by calling:

int GetMovementCostA(movementType m)
{
    return cost[(int)terrain, (int)m];
}

remember to make the cost-array static so that it’s the same for all tiles and you don’t waste memory.

Hope that helps.

1 Like

That looks interesting. I was using the enumerators because it allowed me to keep the enum names consistent across a variety of scripts (since I’ll always remember that an infantry unit has a movement type of foot, but I might sometimes forget the int equivalent). Using int internally for this script seems like it could be a really good idea, though.

I think I will give implementing this a shot tonight. I’m kinda rusty on arrays, but I think I understand what you’re getting at and that this is what I was looking for. Thanks again!

First up I’ve got to challenge your use case. Why use Dijkstra’s when A* is generally faster? Using the right algorithm will often do more for performance then micro optimisation on the code.

I personally wouldn’t use switch-case for this. Generally this type of code is hard to maintain and modify. At the very least you want to expose the various terrain types and costs to the inspector for quick modification.

That’s my mistake for missing details. I have no opposition towards using A*, I’m simply going with what I know at the moment. I’m vaguely familiar with A*. It seems like Dijkstra’s algorithm with the programming equivalent of a position vector added in (the heuristic?). Although my end goal is to make this game. This is also a learning process for me, so I’m trying to make sure I understand everything that I’m doing. In this case, I was thinking if I can’t implement Dijkstra’s algorithm, A* isn’t happening. If I can implement Dijkstra’s algorithm, implementing A* after the fact shouldn’t be as much of a challenge.

For the second comment, can you expand upon that a bit? I already have the enums exposed to the inspector with the public prefix. That’s how I planned on defining all of my terrain–from within the inspector. It seems tedious, but it also seems to save time in the long run.

Here is my setup.

I will have all unit types (let’s say 20 prefabs), and all terrain types (let’s say 13 prefabs), dropped out of view in any scene where the aforementioned game objects are necessary. Both of them will have a generic unit data script or terrain data script respectively. Focusing on terrain only, I’m going to define my terrain enum for the prefabs within the inspector and apply it so it’s set for all instances of the objects. Then I can make a universal called to transform.gameObject.getComponent().getMovementCost. If my RayCast picks up a terrain, there won’t be a need to figure out what terrain type was just hit to get the proper values. Everything will be determined internally by the function which will just return the proper int value.

Are you saying I should be doing something in addition to this or revamping it completely?