Esempio n. 1
0
    def compute_path(self, start_x, start_y, dest_x, dest_y,
                     diagonal_cost=_math.sqrt(2)):
        """Get the shortest path between two points.

        The start position is not included in the list.

        @type diagnalCost: float
        @param diagnalCost: Multiplier for diagonal movement.

                            Can be set to zero to disable diagonal movement
                            entirely.
        @rtype: [(x, y), ...]
        @return: Returns a the shortest list of points to get to the destination
                 position from the starting position
        """
        # refresh cdata
        _lib.TDL_map_data_from_buffer(self._map_cdata,
                                      self._array_cdata_flat)
        path_cdata = _lib.TCOD_path_new_using_map(self._map_cdata, diagonal_cost)
        try:
            _lib.TCOD_path_compute(path_cdata, start_x, start_y, dest_x, dest_y)
            x = _ffi.new('int *')
            y = _ffi.new('int *')
            length = _lib.TCOD_path_size(path_cdata)
            path = [None] * length
            for i in range(length):
                _lib.TCOD_path_get(path_cdata, i, x, y)
                path[i] = ((x[0], y[0]))
        finally:
            _lib.TCOD_path_delete(path_cdata)
        return path
Esempio n. 2
0
    def get_path(self, origX, origY, destX, destY):
        """
        Get the shortest path from origXY to destXY.

        @rtype: [(x, y), ...]
        @return: Returns a list walking the path from origXY to destXY.
                 This excludes the starting point and includes the destination.

                 If no path is found then an empty list is returned.
        """
        found = _lib.TCOD_path_compute(self._as_parameter_, origX, origY, destX, destY)
        if not found:
            return [] # path not found
        x, y = _ffi.new('int *'), _ffi.new('int *')
        recalculate = True
        path = []
        while _lib.TCOD_path_walk(self._as_parameter_, x, y, recalculate):
            path.append((x[0], y[0]))
        return path
Esempio n. 3
0
    def get_path(self, origX, origY, destX, destY):
        """
        Get the shortest path from origXY to destXY.

        @rtype: [(x, y), ...]
        @return: Returns a list walking the path from origXY to destXY.
                 This excludes the starting point and includes the destination.

                 If no path is found then an empty list is returned.
        """
        found = _lib.TCOD_path_compute(self._as_parameter_, origX, origY,
                                       destX, destY)
        if not found:
            return []  # path not found
        x, y = _ffi.new('int *'), _ffi.new('int *')
        recalculate = True
        path = []
        while _lib.TCOD_path_walk(self._as_parameter_, x, y, recalculate):
            path.append((x[0], y[0]))
        return path
Esempio n. 4
0
    def compute_path(self,
                     start_x,
                     start_y,
                     dest_x,
                     dest_y,
                     diagonal_cost=_math.sqrt(2)):
        """Get the shortest path between two points.

        The start position is not included in the list.

        @type diagnalCost: float
        @param diagnalCost: Multiplier for diagonal movement.

                            Can be set to zero to disable diagonal movement
                            entirely.
        @rtype: [(x, y), ...]
        @return: Returns a the shortest list of points to get to the destination
                 position from the starting position
        """
        # refresh cdata
        _lib.TDL_map_data_from_buffer(self._map_cdata, self._array_cdata_flat)
        path_cdata = _lib.TCOD_path_new_using_map(self._map_cdata,
                                                  diagonal_cost)
        try:
            _lib.TCOD_path_compute(path_cdata, start_x, start_y, dest_x,
                                   dest_y)
            x = _ffi.new('int *')
            y = _ffi.new('int *')
            length = _lib.TCOD_path_size(path_cdata)
            path = [None] * length
            for i in range(length):
                _lib.TCOD_path_get(path_cdata, i, x, y)
                path[i] = ((x[0], y[0]))
        finally:
            _lib.TCOD_path_delete(path_cdata)
        return path
Esempio n. 5
0
    def __init__(self, width, height):
        """Create a new Map with width and height.

        @type width: int
        @type height: int
        @param width: Width of the new Map instance, in tiles.
        @param width: Height of the new Map instance, in tiles.
        """
        self.width = width
        self.height = height
        self._map_cdata = _lib.TCOD_map_new(width, height)
        # cast array into cdata format: uint8[y][x]
        # for quick Python access
        self._array_cdata = _ffi.new('uint8[%i][%i]' % (height, width))
        # flat array to pass to TDL's C helpers
        self._array_cdata_flat = _ffi.cast('uint8 *', self._array_cdata)
        self.transparent = self._MapAttribute(self, 0)
        self.walkable = self._MapAttribute(self, 1)
        self.fov = self._MapAttribute(self, 2)
Esempio n. 6
0
 def get_point(self, *position):
     """Return the noise value of a specific position.
     
     Example usage: value = noise.getPoint(x, y, z)
     @type position: floats
     @param position: 
     
     @rtype: float
     @return: Returns the noise value at position.
              This will be a floating point in the 0.0-1.0 range.
     """
     #array = self._array
     #for d, pos in enumerate(position):
     #    array[d] = pos
     #array = self._cFloatArray(*position)
     array = _ffi.new(self._arrayType, position)
     if self._useOctaves:
         return (self._noiseFunc(self._noise, array, self._octaves) + 1) * 0.5
     return (self._noiseFunc(self._noise, array) + 1) * 0.5
Esempio n. 7
0
    def __init__(self, width, height):
        """Create a new Map with width and height.

        @type width: int
        @type height: int
        @param width: Width of the new Map instance, in tiles.
        @param width: Height of the new Map instance, in tiles.
        """
        self.width = width
        self.height = height
        self._map_cdata = _lib.TCOD_map_new(width, height)
        # cast array into cdata format: uint8[y][x]
        # for quick Python access
        self._array_cdata = _ffi.new('uint8[%i][%i]' % (width, height))
        # flat array to pass to TDL's C helpers
        self._array_cdata_flat = _ffi.cast('uint8 *', self._array_cdata)
        self.transparent = self._MapAttribute(self, 0)
        self.walkable = self._MapAttribute(self, 1)
        self.fov = self._MapAttribute(self, 2)
Esempio n. 8
0
    def get_point(self, *position):
        """Return the noise value of a specific position.

        Example usage: value = noise.getPoint(x, y, z)
        @type position: floats
        @param position:

        @rtype: float
        @return: Returns the noise value at position.
                 This will be a floating point in the 0.0-1.0 range.
        """
        #array = self._array
        #for d, pos in enumerate(position):
        #    array[d] = pos
        #array = self._cFloatArray(*position)
        array = _ffi.new(self._arrayType, position)
        if self._useOctaves:
            return (self._noiseFunc(self._noise, array, self._octaves) +
                    1) * 0.5
        return (self._noiseFunc(self._noise, array) + 1) * 0.5
Esempio n. 9
0
    def get_point(self, *position):
        """Return the noise value of a specific position.

        Example usage: value = noise.getPoint(x, y, z)

        Args:
            position (Tuple[float, ...]): The point to sample at.

        Returns:
            float: The noise value at position.

                This will be a floating point in the 0.0-1.0 range.
        """
        #array = self._array
        #for d, pos in enumerate(position):
        #    array[d] = pos
        #array = self._cFloatArray(*position)
        array = _ffi.new(self._arrayType, position)
        if self._useOctaves:
            return (self._noiseFunc(self._noise, array, self._octaves) +
                    1) * 0.5
        return (self._noiseFunc(self._noise, array) + 1) * 0.5
Esempio n. 10
0
def _processEvents():
    """Flushes the event queue from libtcod into the global list _eventQueue"""
    global _mousel, _mousem, _mouser, _eventsflushed, _pushedEvents
    _eventsflushed = True
    events = _pushedEvents # get events from event.push
    _pushedEvents = [] # then clear the pushed events queue

    mouse = _ffi.new('TCOD_mouse_t *')
    libkey = _ffi.new('TCOD_key_t *')
    while 1:
        libevent = _lib.TCOD_sys_check_for_event(_lib.TCOD_EVENT_ANY, libkey, mouse)
        if not libevent: # no more events from libtcod
            break

        #if mouse.dx or mouse.dy:
        if libevent & _lib.TCOD_EVENT_MOUSE_MOVE:
            events.append(MouseMotion((mouse.x, mouse.y),
                                      (mouse.cx, mouse.cy),
                                      (mouse.dx, mouse.dy),
                                      (mouse.dcx, mouse.dcy)))

        mousepos = ((mouse.x, mouse.y), (mouse.cx, mouse.cy))

        for oldstate, newstate, released, button in \
            zip((_mousel, _mousem, _mouser),
                (mouse.lbutton, mouse.mbutton, mouse.rbutton),
                (mouse.lbutton_pressed, mouse.mbutton_pressed,
                 mouse.rbutton_pressed),
                (1, 2, 3)):
            if released:
                if not oldstate:
                    events.append(MouseDown(button, *mousepos))
                events.append(MouseUp(button, *mousepos))
                if newstate:
                    events.append(MouseDown(button, *mousepos))
            elif newstate and not oldstate:
                events.append(MouseDown(button, *mousepos))

        if mouse.wheel_up:
            events.append(MouseDown(4, *mousepos))
        if mouse.wheel_down:
            events.append(MouseDown(5, *mousepos))

        _mousel = mouse.lbutton
        _mousem = mouse.mbutton
        _mouser = mouse.rbutton

        if libkey.vk == _lib.TCODK_NONE:
            break
        if libkey.pressed:
            keyevent = KeyDown
        else:
            keyevent = KeyUp

        events.append(
            keyevent(
                libkey.vk,
                libkey.c.decode('ascii', errors='ignore'),
                _ffi.string(libkey.text).decode('utf-8'),
                libkey.shift,
                libkey.lalt,
                libkey.ralt,
                libkey.lctrl,
                libkey.rctrl,
                libkey.lmeta,
                libkey.rmeta,
                )
            )

    if _lib.TCOD_console_is_window_closed():
        events.append(Quit())

    _eventQueue.extend(events)
Esempio n. 11
0
def _processEvents():
    """Flushes the event queue from libtcod into the global list _eventQueue"""
    global _mousel, _mousem, _mouser, _eventsflushed, _pushedEvents
    _eventsflushed = True
    events = _pushedEvents # get events from event.push
    _pushedEvents = [] # then clear the pushed events queue
    
    mouse = _ffi.new('TCOD_mouse_t *')
    libkey = _ffi.new('TCOD_key_t *')
    while 1:
        libevent = _lib.TCOD_sys_check_for_event(_lib.TCOD_EVENT_ANY, libkey, mouse)
        if not libevent: # no more events from libtcod
            break
            
        #if mouse.dx or mouse.dy:
        if libevent & _lib.TCOD_EVENT_MOUSE_MOVE:
            events.append(MouseMotion((mouse.x, mouse.y),
                                      (mouse.cx, mouse.cy),
                                      (mouse.dx, mouse.dy),
                                      (mouse.dcx, mouse.dcy)))

        mousepos = ((mouse.x, mouse.y), (mouse.cx, mouse.cy))

        for oldstate, newstate, released, button in \
            zip((_mousel, _mousem, _mouser),
                (mouse.lbutton, mouse.mbutton, mouse.rbutton),
                (mouse.lbutton_pressed, mouse.mbutton_pressed,
                 mouse.rbutton_pressed),
                (1, 2, 3)):
            if released:
                if not oldstate:
                    events.append(MouseDown(button, *mousepos))
                events.append(MouseUp(button, *mousepos))
                if newstate:
                    events.append(MouseDown(button, *mousepos))
            elif newstate and not oldstate:
                events.append(MouseDown(button, *mousepos))
        
        if mouse.wheel_up:
            events.append(MouseDown(4, *mousepos))
        if mouse.wheel_down:
            events.append(MouseDown(5, *mousepos))
            
        _mousel = mouse.lbutton
        _mousem = mouse.mbutton
        _mouser = mouse.rbutton

        if libkey.vk == _lib.TCODK_NONE:
            break
        if libkey.pressed:
            keyevent = KeyDown
        else:
            keyevent = KeyUp
        events.append(keyevent(libkey.vk, libkey.c,
                               libkey.lalt, libkey.lctrl,
                               libkey.ralt, libkey.rctrl, libkey.shift))
    
    if _lib.TCOD_console_is_window_closed():
        events.append(Quit())

    _eventQueue.extend(events)