Procedural Map Generation - UQdeco2800/2022-studio-3 GitHub Wiki

Introduction

To keep gameplay unique and enhance replay value, the game map will be dynamically generated each playthrough. This means that players will have to adapt their strategy for building their militia and working force, as resources and enemies will not appear statically from game to game. In order to assist with this, the MapGenerator class has been developed, which features functionality to create and output a randomly generated map

Usage

It is necessary to include the class in all implementations

import com.deco2800.game.areas.MapGenerator.MapGenerator;

Generate a map

Generating a map should be done in a TerrainFactory class - for example - an implementation in the default TerrainFactory class, within the fillTiles() function:

    //Generate a map with the following specifications
    MapGenerator mg = new MapGenerator(mapWidth, mapHeight, citySize, islandSize);
    //Store the map
    char[][] map = mg.getMap();

    for (int x = 0; x < mapWidth; x++) {
      for (int y = 0; y < mapHeight; y++) {
        Cell cell = new Cell();
        if (map[y][x] == mg.getOceanChar()) {
          //Set ocean tiles to ocean textures
          cell.setTile(oceanTile);
        } else {
          //Set non-ocean tiles to sand textures
          cell.setTile(sandTile);
        }
        //Add the cell to the map
        layer.setCell(x, mapHeight - y, cell);
      }
    }

In this usage case, variables mapWidth, mapHeight, citySize, islandSize were all defined as static constants beforehand, and textures oceanTile and sandTile were also pre-defined TextureRegions.

Note that when setting the created cell to the layer, the y component is mapped inversely, as the MapGenerator's y component is defined inversely to the layer's.

Save human-readable representation of the generated map as a text file

//Create a new MapGenerator
MapGenerator mg = new MapGenerator(mapWidth, mapHeight, citySize, islandSize);
//Write the map to an output location - replace path as necessary
mg.writeMap("C:\\dir1\\dir2\\map.txt");

Which will give an output of the following form, with ocean tiles indicated '*', island tiles indicated 'I', and city tiles indicated 'c'