Beispiel #1
0
    def testPathfindNotGuaranteeShortest(self):

        tileData = level.TileLevel()

        # Map: 4x3
        tileData.terrain = [[0, 0, 0, 0], [0, 1, 1, 0], [0, 0, 0, 0]]
        tileData.building = tileData.collision = tileData.terrain
        tileData.updateLevelSize()

        self.assertEqual([(1, 0), (0, 0), (1, 0), (2, 0), (2, 1)],
                         tileData.findPath(1, 0, 1, 2))
Beispiel #2
0
    def testPathfindGoalCellIsNotMovable2(self):

        tileData = level.TileLevel()

        # Map: 3x3 (Cannot reach the destination)
        tileData.terrain = [[0, 1, 1], [0, 0, 1], [1, 1, 1]]
        tileData.building = tileData.collision = tileData.terrain
        tileData.updateLevelSize()

        self.assertEqual([(0, 0), (0, 1), (1, 1)],
                         tileData.findPath(0, 0, 2, 2))

        # Map: 3x3 (Cannot reach the destination)
        tileData.terrain = [[0, 1, 1], [0, 0, 0], [1, 1, 1]]
        tileData.building = tileData.collision = tileData.terrain
        tileData.updateLevelSize()

        self.assertEqual([(0, 0), (0, 1), (1, 1), (2, 1)],
                         tileData.findPath(0, 0, 2, 2))
        self.assertEqual([(0, 0), (0, 1), (1, 1), (2, 1)],
                         tileData.findPath(0, 0, 2, 0))

        # Map: 3x3 (Cannot reach the destination)
        tileData.terrain = [[1, 1, 1], [1, 0, 1], [1, 1, 1]]
        tileData.building = tileData.collision = tileData.terrain
        tileData.updateLevelSize()

        for i in range(3):
            for j in range(3):
                self.assertEqual([(1, 1)], tileData.findPath(1, 1, i, j))

        # Map: 4x4 (Cannot reach the destination)
        tileData.terrain = [
            [1, 1, 1, 0],
            [1, 0, 1, 0],
            [1, 1, 1, 0],
            [0, 0, 0, 0],
        ]
        tileData.building = tileData.collision = tileData.terrain
        tileData.updateLevelSize()

        for i in range(4):
            for j in range(4):
                self.assertEqual([(1, 1)], tileData.findPath(1, 1, i, j))

        self.assertEqual([(0, 3), (1, 3), (2, 3), (3, 3), (3, 2), (3, 1),
                          (3, 0)], tileData.findPath(0, 3, 3, 0))

        self.assertEqual([(0, 3)], tileData.findPath(0, 3, 0, 2))
Beispiel #3
0
    def testPathfindGoalCellIsNotMovable(self):

        tileData = level.TileLevel()

        # Map: 2x1 (Cannot move, stay still)
        tileData.terrain = [[0, 1]]
        tileData.building = tileData.collision = tileData.terrain
        tileData.updateLevelSize()

        self.assertEqual([(0, 0)], tileData.findPath(0, 0, 1, 0))

        # Map: 3x1 (Cannot reach the destination)
        tileData.terrain = [[0, 0, 1]]
        tileData.building = tileData.collision = tileData.terrain
        tileData.updateLevelSize()

        self.assertEqual([(0, 0), (1, 0)], tileData.findPath(0, 0, 2, 0))
        self.assertEqual([(1, 0)], tileData.findPath(1, 0, 2, 0))
Beispiel #4
0
    def __init__(self,
                 spawnTreeInterval=0,
                 exitOnNoHarvestables=False,
                 disableCollisionCheck=False,
                 width=10,
                 height=10,
                 useTestData=False):

        SActor.__init__(self, 'World')

        # Actor dictionaries
        self.registeredActors = {}
        self.kdActorTree = kdtree.KdTree(
            searchSpace=kdtree.Rect(0, 0, 10**9, 10**9))
        self.aboutToBeKilledActors = {}
        self.tickDisabledActors = {}
        #self.sightActors = weakref.WeakValueDictionary() #{}

        # Tick
        self.maxUpdateRate = 30
        self.updateRate = self.maxUpdateRate
        self.tickTasklet = None
        self.tickLoopEnable = True
        self.tickOnlyOnce = False

        # Level
        self.tileData = level.TileLevel(width, height, useTestData=useTestData)

        self.showHarvestResult = False
        self.spawnTreeInterval = spawnTreeInterval
        self.exitOnNoHarvestables = exitOnNoHarvestables
        self.lastSpawnTreeTime = -1
        self.disableCollisionCheck = disableCollisionCheck

        # Network
        self._server = None
        self._client = None

        self.debug('Created.')
Beispiel #5
0
    def testPathfind(self):

        tileData = level.TileLevel()

        # Map: 1x1 (No obstacle)
        tileData.terrain = [[0]]
        tileData.building = [[0]]
        tileData.collision = [[0]]
        tileData.updateLevelSize()

        self.assertEqual([(0, 0)], tileData.findPath(0, 0, 0, 0))
        self.assertRaises(IndexError, tileData.findPath, 0, 0, 1, 1)

        # Map: 1x1 (JAMMED!)
        tileData.terrain = [[1]]
        tileData.building = [[1]]
        tileData.collision = [[1]]
        tileData.updateLevelSize()

        self.assertEqual([], tileData.findPath(0, 0, 0, 0))

        # Map: 2x1 (No obstacle)
        tileData.terrain = [[0, 0]]
        tileData.building = [[0, 0]]
        tileData.collision = [[0, 0]]
        tileData.updateLevelSize()

        self.assertEqual([(0, 0)], tileData.findPath(0, 0, 0, 0))
        self.assertEqual([(1, 0)], tileData.findPath(1, 0, 1, 0))
        self.assertEqual([(0, 0), (1, 0)], tileData.findPath(0, 0, 1, 0))
        self.assertEqual([(1, 0), (0, 0)], tileData.findPath(1, 0, 0, 0))

        # Map: 3x1 (No obstacle)
        tileData.terrain = [[0, 0, 0]]
        tileData.building = [[0, 0, 0]]
        tileData.collision = [[0, 0, 0]]
        tileData.updateLevelSize()

        self.assertEqual([(0, 0), (1, 0), (2, 0)],
                         tileData.findPath(0, 0, 2, 0))
        self.assertEqual([(2, 0), (1, 0), (0, 0)],
                         tileData.findPath(2, 0, 0, 0))

        # Map: 3x1
        tileData.terrain = [[0, 0, 1]]
        tileData.building = [[0, 0, 1]]
        tileData.collision = [[0, 0, 1]]
        tileData.updateLevelSize()

        self.assertEqual([(0, 0), (1, 0)], tileData.findPath(0, 0, 1, 0))
        self.assertEqual([(1, 0), (0, 0)], tileData.findPath(1, 0, 0, 0))

        # Map: 3x1
        tileData.terrain = [[1, 0, 0]]
        tileData.building = [[1, 0, 0]]
        tileData.collision = [[1, 0, 0]]
        tileData.updateLevelSize()

        self.assertEqual([(1, 0), (2, 0)], tileData.findPath(1, 0, 2, 0))
        self.assertEqual([(2, 0), (1, 0)], tileData.findPath(2, 0, 1, 0))

        # Map: 3x1 (Unreachable)
        tileData.terrain = [[0, 1, 0]]
        tileData.building = [[0, 1, 0]]
        tileData.collision = [[0, 1, 0]]
        tileData.updateLevelSize()

        self.assertEqual([(0, 0)], tileData.findPath(0, 0, 2, 0))
        self.assertEqual([(2, 0)], tileData.findPath(2, 0, 0, 0))
        self.assertEqual([], tileData.findPath(1, 0, 0, 0))  # Jammed
        self.assertEqual([], tileData.findPath(1, 0, 2, 0))  # Jammed

        # Map: 2x2 (No obstacle)
        tileData.terrain = [[0, 0], [0, 0]]
        tileData.building = [[0, 0], [0, 0]]
        tileData.collision = [[0, 0], [0, 0]]
        tileData.updateLevelSize()

        self.assertEqual([(0, 0), (1, 0)], tileData.findPath(0, 0, 1, 0))
        self.assertEqual([(1, 0), (0, 0)], tileData.findPath(1, 0, 0, 0))
        self.assertEqual([(0, 1), (1, 1)], tileData.findPath(0, 1, 1, 1))
        self.assertEqual([(1, 1), (0, 1)], tileData.findPath(1, 1, 0, 1))
        self.assertEqual([(0, 0), (0, 1)], tileData.findPath(0, 0, 0, 1))
        self.assertEqual([(1, 0), (1, 1)], tileData.findPath(1, 0, 1, 1))

        # Map: 2x2
        tileData.terrain = [[0, 1], [0, 0]]
        tileData.building = [[0, 1], [0, 0]]
        tileData.collision = [[0, 1], [0, 0]]
        tileData.updateLevelSize()

        self.assertEqual([(0, 0), (0, 1), (1, 1)],
                         tileData.findPath(0, 0, 1, 1))

        # Map: 3x2
        tileData.terrain = [[0, 1, 0], [0, 0, 0]]
        tileData.building = [[0, 1, 0], [0, 0, 0]]
        tileData.collision = [[0, 1, 0], [0, 0, 0]]
        tileData.updateLevelSize()

        self.assertEqual([(0, 0), (0, 1), (1, 1), (2, 1), (2, 0)],
                         tileData.findPath(0, 0, 2, 0))

        # Map: 3x3
        tileData.terrain = [[0, 1, 0], [0, 0, 0], [0, 1, 0]]
        tileData.building = [[0, 1, 0], [0, 0, 0], [0, 1, 0]]
        tileData.collision = [[0, 1, 0], [0, 0, 0], [0, 1, 0]]
        tileData.updateLevelSize()

        self.assertEqual([(0, 0), (0, 1), (1, 1), (2, 1), (2, 2)],
                         tileData.findPath(0, 0, 2, 2))

        # Map: 3x3
        tileData.terrain = [[0, 0, 0], [0, 1, 0], [0, 1, 1]]
        tileData.building = [[0, 0, 0], [0, 1, 0], [0, 1, 1]]
        tileData.collision = [[0, 0, 0], [0, 1, 0], [0, 1, 1]]
        tileData.updateLevelSize()

        self.assertEqual([(0, 2), (0, 1), (0, 0), (1, 0), (2, 0), (2, 1)],
                         tileData.findPath(0, 2, 2, 1))