Example #1
0
 def to_json(self):
     return obj_to_json(
         self, {
             "screen_x_factor": Hexagon(self.x_offset,
                                        self.z_offset).screen_x_factor,
             "screen_y_factor": Hexagon(self.x_offset,
                                        self.z_offset).screen_y_factor,
             "hexagons": list(self.hexagons)
         })
Example #2
0
def planet_hexagons():
    return {
        Hexagon(0, 0, planet=Planet(PlanetType.BLUE)),
        Hexagon(0, 2, planet=Planet(PlanetType.RED)),
        Hexagon(-1,
                1,
                planet=Planet(PlanetType.ORANGE,
                              building=Building(Factions.AMBAS,
                                                BuildingType.MINE)))
    }
Example #3
0
    def create_sector_hexagons(self, central_hexagon: Hexagon,
                               planet_hexagons: Set[Hexagon]) -> Set[Hexagon]:
        sector_hexagons = central_hexagon.get_hexagons_in_range(
            self.radius) - planet_hexagons

        sector_hexagons.update(planet_hexagons)
        return sector_hexagons
Example #4
0
def test_sector_adjust_offset(planet_hexagons, orig_x_offset, orig_z_offset,
                              x_offset, z_offset):
    sector = Sector(planet_hexagons,
                    x_offset=orig_x_offset,
                    z_offset=orig_z_offset)
    sector.adjust_offset(x_offset=x_offset, z_offset=z_offset)
    assert all(
        h.distance(Hexagon(x_offset, z_offset)) <= 2 for h in sector.hexagons)
    assert sector.x_offset == x_offset
    assert sector.z_offset == z_offset
Example #5
0
    def __init__(self,
                 planet_hexagons: Set[Hexagon],
                 radius: int = 2,
                 x_offset: int = 0,
                 z_offset: int = 0):
        assert radius >= 0, "Radius must be greater or equal to zero"

        self.radius = radius
        self.x_offset = x_offset
        self.z_offset = z_offset

        planet_hexagons = {
            Hexagon(x=planet_hex.x + self.x_offset,
                    z=planet_hex.z + self.z_offset,
                    planet=planet_hex.planet)
            for planet_hex in planet_hexagons
        }

        center = Hexagon(self.x_offset, self.z_offset)
        self.hexagons = self.create_sector_hexagons(center, planet_hexagons)
Example #6
0
    def get_hexagons_in_range(self, hexagon: Hexagon, distance: int, only_inhabited: bool = False) \
            -> Set[Hexagon]:
        hexagons_in_range = hexagon.get_hexagons_in_range(distance)
        map_hexagons_in_range = set()

        for hexagon in hexagons_in_range:
            map_hexagon = self.get_hexagon(hexagon)
            if only_inhabited:
                if map_hexagon is not None and map_hexagon.planet is not None and map_hexagon.planet.is_inhabited():
                    map_hexagons_in_range.add(map_hexagon)
            else:
                map_hexagons_in_range.add(map_hexagon)

        return map_hexagons_in_range
Example #7
0
    def get_tile_mapping_from_config(config_path: str) -> Dict[int, GameTile]:
        with open(config_path) as config:
            config = json.load(config)

        tile_mapping = dict()

        for tile in config["tiles"]:
            game_tile = GameTile()
            tile_mapping[tile["number"]] = game_tile

            radius = tile["radius"]
            for side in tile["sides"]:
                hexagons = {
                    Hexagon(p["x"], p["z"], Planet(PlanetType[p["type"]]))
                    for p in side
                }
                game_tile.sides.append(Sector(hexagons, radius))

        return tile_mapping
Example #8
0
import pytest

from gaia.turns.turn_validator import Turn
from gaia.turns.actions import (PlaceMineAction, PassAction, GaiaformAction,
                                GainRangeAction, ExchangeOreForCreditAction,
                                StartGaiaProjectAction)
from gaia.board.hexagons import Hexagon


@pytest.mark.parametrize(
    "actions,should_be_valid,main_reason",
    [([PlaceMineAction(Hexagon(0, 0)),
       PassAction()], False,
      "PlaceMineAction ends the turn, but was not the last action in the turn."
      ), ([PlaceMineAction(Hexagon(0, 0))], True, None),
     ([ExchangeOreForCreditAction(1),
       PlaceMineAction(Hexagon(0, 0))], True, None),
     ([GaiaformAction(Hexagon(0, 0))], False,
      "The last action in a turn must end the turn, but GaiaformAction did not"
      ),
     ([GaiaformAction(Hexagon(0, 0)),
       PlaceMineAction(Hexagon(0, 0))], True, None),
     ([GaiaformAction(Hexagon(0, 0)),
       PlaceMineAction(Hexagon(1, 0))
       ], False, GaiaformAction.ILLEGAL_PLACEMENT_MESSAGE),
     ([GaiaformAction(Hexagon(0, 0)), PassAction
       ], False, GaiaformAction.ILLEGAL_ACTION_MESSAGE),
     ([GainRangeAction(), PlaceMineAction(Hexagon(0, 0))], True, None),
     ([GainRangeAction(),
       StartGaiaProjectAction(Hexagon(0, 0))], True, None),
     ([GainRangeAction(), PassAction()
Example #9
0
import pytest
from copy import copy
import json

from gaia.board.hexagons import Hexagon
from gaia.board.sectors import Sector
from gaia.board.map_loader import GameTile, MapLoader


@pytest.mark.parametrize("hex1,hex2,distance",
                         [(Hexagon(0, 0), Hexagon(0, 1), 1),
                          (Hexagon(0, 0), Hexagon(2, 1), 3),
                          (Hexagon(0, 0), Hexagon(-2, 2), 2),
                          (Hexagon(0, 0), Hexagon(2, 2), 4),
                          (Hexagon(-3, 0), Hexagon(3, 0), 6),
                          (Hexagon(-3, -2), Hexagon(5, -3), 8),
                          (Hexagon(2, -5), Hexagon(3, 2), 8),
                          (Hexagon(-3, -2), Hexagon(3, 2), 10)])
def test_hexagon_get_distance(hex1, hex2, distance):
    assert hex1.distance(hex2) == distance


@pytest.mark.parametrize("hex1,hex2,x_offset_diff,z_offset_diff",
                         [(Hexagon(0, 0), Hexagon(1, 1), 1, 1),
                          (Hexagon(1, 5), Hexagon(2, 3), 1, -2),
                          (Hexagon(4, -2), Hexagon(5, -3), 1, -1)])
def test_hexagon_adjust_offset(hex1, hex2, x_offset_diff, z_offset_diff):
    assert hex1.adjust_offset(x_offset_diff, z_offset_diff) == hex2


@pytest.mark.parametrize("orig,new,degrees",
Example #10
0
import pytest

from gaia.board.planets import Planet
from gaia.board.hexagons import Hexagon

from gaia.utils.enums import BuildingType, PlanetType
from gaia.turns.actions import PlaceMineAction
from gaia.gamestate.players import PlayerResources

from tests.util import TestBuilding


@pytest.mark.parametrize("planet_hexagons,action,should_be_valid,main_reason", [
    (
        [
            Hexagon(0, 1, planet=Planet(PlanetType.WHITE)),
            Hexagon(0, 0, planet=Planet(PlanetType.WHITE, building=TestBuilding(BuildingType.MINE)))
        ],
        PlaceMineAction(Hexagon(0, 1)),
        True,
        None
    ),
    (
        [
            Hexagon(0, 1, planet=Planet(PlanetType.TRANSDIM)),
            Hexagon(0, 0, planet=Planet(PlanetType.WHITE, building=TestBuilding(BuildingType.MINE)))
        ],
        PlaceMineAction(Hexagon(0, 1)),
        False,
        "Cannot build on transdim planets"
    ),
Example #11
0
def test_range_gamestate(starting_gamestate):
    player_1 = starting_gamestate.players["p1"]
    starting_gamestate.game_map.inhabit_planet(
        Hexagon(0, 1), Building(player_1.faction, BuildingType.MINE))

    return starting_gamestate