Completed
Push — master ( e9e2ae...b6e5e0 )
by Kyle
01:34
created

BufferBitProxy.__init__()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
1
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2
from __future__ import absolute_import as _
3
4
import numpy as np
0 ignored issues
show
Configuration introduced by
The import numpy could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
5
6
from tcod.libtcod import lib, ffi
7
8
9
class BufferBitProxy(object):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
10
11
    def __init__(self, buffer, bitmask):
12
        self.buffer = buffer
13
        self.bitmask = np.asarray(bitmask, dtype=np.uint8)
14
15
    def __getitem__(self, index):
16
        return (self.buffer[index] & self.bitmask) != 0
17
18
    def __setitem__(self, index, values):
19
        self.buffer[index] &= 0xff ^ self.bitmask
20
        self.buffer[index] |= self.bitmask * np.asarray(values, dtype=bool)
21
22
23
class Map(object):
24
    """
25
    .. versionadded:: 2.0
26
27
    Args:
28
        width (int): Width of the new Map.
29
        height (int): Height of the new Map.
30
31
    Attributes:
32
        width (int): Read only width of this Map.
33
        height (int): Read only height of this Map.
34
        map_c (CData): A cffi pointer to a TCOD_map_t object.
35
            .. versionadded:: 2.5
36
    """
37
38
    def __init__(self, width, height):
39
        assert ffi.sizeof('cell_t') == 1 # assert buffer alignment
40
        self.buffer = np.zeros((height, width), dtype=np.uint8)
41
42
        self.map_c = ffi.new('map_t*')
43
        self.map_c.width = self.width = width
44
        self.map_c.height = self.height = height
45
        self.map_c.nbcells = width * height
46
        self.map_c.cells = ffi.cast('cell_t*', self.buffer.ctypes.data)
47
48
        self._init_proxies()
49
50
    def _init_proxies(self):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
51
        self.transparent = BufferBitProxy(self.buffer, 0x01)
52
        self.walkable = BufferBitProxy(self.buffer, 0x02)
53
        self.fov = BufferBitProxy(self.buffer, 0x04)
54
55
    def compute_fov(self, x, y, radius=0, light_walls=True,
56
                    algorithm=lib.FOV_RESTRICTIVE):
57
        """
58
59
        Args:
60
            x (int):
61
            y (int):
62
            radius (int):
63
            light_walls (bool):
64
            algorithm (int): Defaults to FOV_RESTRICTIVE
65
        """
66
        lib.TCOD_map_compute_fov(self.map_c, x, y, radius, light_walls,
67
                                 algorithm)
68
69
    def __getstate__(self):
70
        """Return this objects state.
71
72
        This method allows the usage of the :mod:`copy` and :mod:`pickle`
73
        modules with this class.
74
        """
75
        state = self.__dict__.copy()
76
        del state['map_c']
77
        return state
78
        return {'size': (self.width, self.height), 'buffer': self.buffer}
0 ignored issues
show
Unused Code introduced by
This code does not seem to be reachable.
Loading history...
79
80
    def __setstate__(self, state):
81
        """Unpack this object from a saved state.  A new buffer is used."""
82
        if 'width' not in state or 'height' not in state:
83
            state['width'], state['height'] = state['size']
84
            del state['size']
85
86
        self.__dict__.update(state)
87
88
        self.map_c = ffi.new(
89
            'map_t *',
90
            (
91
                self.width,
92
                self.height,
93
                self.width * self.height,
94
                ffi.cast('cell_t*', self.buffer.ctypes.data),
95
            )
96
        )
97
98
        self._init_proxies()
99