Completed
Push — master ( 50c77c...9113f9 )
by Kyle
02:07
created

Map.fov()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
1
"""libtcod map attributes and field-of-view functions.
2
3
Example::
4
5
    >>> import tcod.map
6
    >>> m = tcod.map.Map(width=3, height=4)
7
    >>> m.walkable
8
    array([[False, False, False],
9
           [False, False, False],
10
           [False, False, False],
11
           [False, False, False]], dtype=bool)
12
13
    # Like the rest of the tcod modules, all arrays here are
14
    # in row-major order and are addressed with [y,x]
15
    >>> m.transparent[:] = True # Sets all to True.
16
    >>> m.transparent[1:3,0] = False # Sets (1, 0) and (2, 0) to False.
17
    >>> m.transparent
18
    array([[ True,  True,  True],
19
           [False,  True,  True],
20
           [False,  True,  True],
21
           [ True,  True,  True]], dtype=bool)
22
23
    >>> m.compute_fov(0, 0)
24
    >>> m.fov
25
    array([[ True,  True,  True],
26
           [ True,  True,  True],
27
           [False,  True,  True],
28
           [False, False,  True]], dtype=bool)
29
    >>> m.fov[3,1]
30
    False
31
32
"""
33
34
from __future__ import absolute_import
35
36
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...
37
38
from tcod.libtcod import lib, ffi
39
40
41
class Map(object):
42
    """A map containing libtcod attributes.
43
44
    .. versionchanged:: 4.1
45
        `transparent`, `walkable`, and `fov` are now numpy boolean arrays.
46
47
    Args:
48
        width (int): Width of the new Map.
49
        height (int): Height of the new Map.
50
51
    Attributes:
52
        width (int): Read only width of this Map.
53
        height (int): Read only height of this Map.
54
        transparent: A boolean array of transparent cells.
55
        walkable: A boolean array of walkable cells.
56
        fov: A boolean array of the cells lit by :any:'compute_fov'.
57
58
    """
59
60
    def __init__(self, width, height):
61
        assert ffi.sizeof('cell_t') == 3 # assert buffer alignment
62
        self.width = width
63
        self.height = height
64
65
        self.__buffer = np.zeros((height, width, 3), dtype=np.bool_)
66
        self.map_c = self.__as_cdata()
67
68
    def __as_cdata(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...
69
        return ffi.new(
70
            'map_t *',
71
            (
72
                self.width,
73
                self.height,
74
                self.width * self.height,
75
                ffi.cast('cell_t*', self.__buffer.ctypes.data),
76
            )
77
        )
78
79
    @property
80
    def transparent(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...
81
        return self.__buffer[:,:,0]
0 ignored issues
show
Coding Style introduced by
Exactly one space required after comma
return self.__buffer[:,:,0]
^
Loading history...
Coding Style introduced by
Exactly one space required after comma
return self.__buffer[:,:,0]
^
Loading history...
82
83
    @property
84
    def walkable(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...
85
        return self.__buffer[:,:,1]
0 ignored issues
show
Coding Style introduced by
Exactly one space required after comma
return self.__buffer[:,:,1]
^
Loading history...
Coding Style introduced by
Exactly one space required after comma
return self.__buffer[:,:,1]
^
Loading history...
86
87
    @property
88
    def fov(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...
89
        return self.__buffer[:,:,2]
0 ignored issues
show
Coding Style introduced by
Exactly one space required after comma
return self.__buffer[:,:,2]
^
Loading history...
Coding Style introduced by
Exactly one space required after comma
return self.__buffer[:,:,2]
^
Loading history...
90
91
    def compute_fov(self, x, y, radius=0, light_walls=True,
0 ignored issues
show
Coding Style Naming introduced by
The name x does not conform to the argument naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name y does not conform to the argument naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
92
                    algorithm=lib.FOV_RESTRICTIVE):
93
        """Compute a field-of-view on the current instance.
94
95
        Args:
96
            x (int): Point of view, x-coordinate.
97
            y (int): Point of view, y-coordinate.
98
            radius (int): Maximum view distance from the point of view.
99
100
                A value of `0` will give an infinite distance.
101
            light_walls (bool): Light up walls, or only the floor.
102
            algorithm (int): Defaults to tcod.FOV_RESTRICTIVE
103
        """
104
        lib.TCOD_map_compute_fov(
105
            self.map_c, x, y, radius, light_walls, algorithm)
106
107
    def __setstate__(self, state):
108
        if '_Map__buffer' not in state: # deprecated
109
            self.__buffer = np.zeros((state['height'], state['width'], 3),
110
                              dtype=np.bool_)
111
            self.__buffer[:,:,0] = state['buffer'] & 0x01
0 ignored issues
show
Coding Style introduced by
Exactly one space required after comma
self.__buffer[:,:,0] = state['buffer'] & 0x01
^
Loading history...
Coding Style introduced by
Exactly one space required after comma
self.__buffer[:,:,0] = state['buffer'] & 0x01
^
Loading history...
112
            self.__buffer[:,:,1] = state['buffer'] & 0x02
0 ignored issues
show
Coding Style introduced by
Exactly one space required after comma
self.__buffer[:,:,1] = state['buffer'] & 0x02
^
Loading history...
Coding Style introduced by
Exactly one space required after comma
self.__buffer[:,:,1] = state['buffer'] & 0x02
^
Loading history...
113
            self.__buffer[:,:,2] = state['buffer'] & 0x04
0 ignored issues
show
Coding Style introduced by
Exactly one space required after comma
self.__buffer[:,:,2] = state['buffer'] & 0x04
^
Loading history...
Coding Style introduced by
Exactly one space required after comma
self.__buffer[:,:,2] = state['buffer'] & 0x04
^
Loading history...
114
            del state['buffer']
115
        self.__dict__.update(state)
116
        self.map_c = self.__as_cdata()
117
118
    def __getstate__(self):
119
        state = self.__dict__.copy()
120
        del state['map_c']
121
        return state
122