Skip to content

Commit 9a99db1

Browse files
author
4b796c65
committed
Added a field of view class.
1 parent d6cbe69 commit 9a99db1

File tree

3 files changed

+119
-2
lines changed

3 files changed

+119
-2
lines changed

tdl/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import itertools
3232
import textwrap
3333

34-
from . import event, path
34+
from . import event, path, fov
3535
from .__tcod import _lib, _Color, _unpackfile
3636

3737
_IS_PYTHON3 = (sys.version_info[0] == 3)
@@ -978,6 +978,6 @@ def forceResolution(width, height):
978978
@type height: int
979979
"""
980980
_lib.TCOD_sys_force_fullscreen_resolution(width, height)
981-
981+
982982
__all__ = [_var for _var in locals().keys() if _var[0] != '_' and _var not in ['sys', 'os', 'ctypes', 'array', 'weakref', 'itertools', 'textwrap']]
983983
__all__ += ['_MetaConsole'] # keep this object public to show the documentation in epydoc

tdl/__tcod.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,69 @@ def __repr__(self):
465465
_lib.TCOD_sys_check_for_event.restype = c_int
466466
_lib.TCOD_sys_check_for_event.argtypes = (c_int, POINTER(_Key), POINTER(_Mouse))
467467

468+
# FOV
469+
470+
TCOD_map_t = c_void_p
471+
472+
_lib.TCOD_map_new.restype = TCOD_map_t
473+
_lib.TCOD_map_new.argtypes = (c_int, c_int)
474+
_lib.TCOD_map_delete.restype = None
475+
_lib.TCOD_map_delete.argtypes = (TCOD_map_t,)
476+
_lib.TCOD_map_set_properties.restype = None
477+
_lib.TCOD_map_set_properties.argtypes = (TCOD_map_t, c_int, c_int, c_bool, c_bool)
478+
479+
_lib.TCOD_map_compute_fov.restype = None
480+
_lib.TCOD_map_compute_fov.argtypes = (TCOD_map_t, c_int, c_int, c_int, c_bool, c_int)
481+
_lib.TCOD_map_is_in_fov.restype = c_bool
482+
_lib.TCOD_map_is_in_fov.argtypes = (TCOD_map_t, c_int, c_int)
483+
484+
# PATH
485+
486+
TCOD_path_t = c_void_p
487+
PATHCALL = CFUNCTYPE(c_float, (c_int, c_int, c_int, c_int, c_void_p))
488+
489+
_lib.TCOD_path_new_using_map.restype = TCOD_path_t
490+
_lib.TCOD_path_new_using_map.argtypes = (TCOD_map_t, c_float)
491+
_lib.TCOD_path_new_using_function.restype = TCOD_path_t
492+
_lib.TCOD_path_new_using_function.argtypes = (c_int, c_int, PATHCALL, c_void_p, c_float)
493+
_lib.TCOD_path_compute.restype = c_bool
494+
_lib.TCOD_path_compute.argtypes = (TCOD_path_t, c_int, c_int, c_int, c_int)
495+
_lib.TCOD_path_walk.restype = c_bool
496+
_lib.TCOD_path_walk.argtypes = (TCOD_path_t, POINTER(c_int), POINTER(c_int), c_bool)
497+
_lib.TCOD_path_size.restype = c_int
498+
_lib.TCOD_path_size.argtypes = (TCOD_path_t,)
499+
_lib.TCOD_path_delete.restype = None
500+
_lib.TCOD_path_delete.argtypes = (TCOD_path_t,)
501+
502+
# Dijkstra
503+
504+
TCOD_dijkstra_t = c_void_p
505+
506+
_lib.TCOD_dijkstra_new.restype = TCOD_dijkstra_t
507+
_lib.TCOD_dijkstra_new.argtypes = (TCOD_map_t, c_float)
508+
_lib.TCOD_dijkstra_new_using_function.restype = TCOD_dijkstra_t
509+
_lib.TCOD_dijkstra_new_using_function.argtypes = (c_int, c_int, PATHCALL, c_void_p, c_float)
510+
_lib.TCOD_dijkstra_compute.restype = None
511+
_lib.TCOD_dijkstra_compute.argtypes = (TCOD_dijkstra_t, c_int, c_int)
512+
_lib.TCOD_dijkstra_get_distance.restype = c_float
513+
_lib.TCOD_dijkstra_get_distance.argtypes = (TCOD_dijkstra_t, c_int, c_int)
514+
_lib.TCOD_dijkstra_path_set.restype = c_bool
515+
_lib.TCOD_dijkstra_path_set.argtypes = (TCOD_dijkstra_t, c_int, c_int)
516+
_lib.TCOD_dijkstra_size.restype = c_int
517+
_lib.TCOD_dijkstra_size.argtypes = (TCOD_dijkstra_t,)
518+
_lib.TCOD_dijkstra_reverse.restype = None
519+
_lib.TCOD_dijkstra_reverse.argtypes = (TCOD_dijkstra_t,)
520+
_lib.TCOD_dijkstra_path_walk.restype = None
521+
_lib.TCOD_dijkstra_path_walk.argtypes = (TCOD_path_t, POINTER(c_int), POINTER(c_int))
522+
_lib.TCOD_dijkstra_delete.restype = None
523+
_lib.TCOD_dijkstra_delete.argtypes = (TCOD_dijkstra_t,)
524+
525+
#_lib..restype = None
526+
#_lib..argtypes =
527+
468528
# these functions should perform ultra fast once I set them up
529+
# after testing it seems that converting python types to ctypes is slow no
530+
# matter how they are passed to C
469531
_lib.TCOD_console_fill_background.restype = None
470532
_lib.TCOD_console_fill_background.argtypes = (TCOD_console_t, POINTER(c_int), POINTER(c_int), POINTER(c_int))
471533
_lib.TCOD_console_fill_foreground.restype = None

tdl/fov.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
import itertools
3+
4+
from .__tcod import _lib
5+
6+
class Map(object):
7+
8+
def __init__(self, width, height):
9+
self._width = int(width)
10+
self._height = int(height)
11+
self._size = self._width * self._height
12+
self._tcodMap = _lib.TCOD_map_new(width, height)
13+
self._as_parameter_ = self._tcodMap
14+
self._fovConf = (0, 0, 0) # fov settings (x, y, radius)
15+
#self._walkable = array.array('b', [0] * self._size)
16+
#self._transparent = array.array('b', [0] * self._size)
17+
18+
def __del__(self):
19+
_lib.TCOD_map_delete(self)
20+
21+
def setFromCallbacks(self, walkableCall, transparentCall):
22+
for x, y in itertools.product(range(self._width), range(self._height)):
23+
_lib.TCOD_map_set_properties(self._tcodMap, x, y,
24+
walkableCall(x, y),
25+
transparentCall(x, y))
26+
27+
def set(self, x, y, walkable, transparent):
28+
#walkable = bool(walkable)
29+
#transparent = bool(transparent)
30+
_lib.TCOD_map_set_properties(self._as_parameter_,
31+
x, y, walkable, transparent)
32+
33+
def computeFOV(self, x, y, fov='PERMISSIVE', radius=8, lightWalls=True):
34+
oldFOV = fov
35+
fov = str(fov).upper()
36+
FOVTYPES = {'BASIC' : 0, 'DIAMOND': 1, 'SHADOW': 2, 'RESTRICTIVE': 12,
37+
'PERMISSIVE': 11}
38+
if fov in FOVTYPES:
39+
fov = FOVTYPES[fov]
40+
elif fov[:10] == 'PERMISSIVE' and fov[10].isdigit():
41+
fov = 4 + int(fov[10])
42+
else:
43+
raise TDLError('No such fov as %s' % oldFOV)
44+
_lib.TCOD_map_compute_fov(self, x, y, radius, lightWalls, fov)
45+
self._fovConf = (x, y, radius)
46+
return self._iterFOV()
47+
48+
def _iterFOV(self):
49+
x, y, radius = self._fovConf
50+
inFOV = _lib.TCOD_map_is_in_fov
51+
map = self._as_parameter_
52+
for x,y in itertools.product(range(x - radius, x + radius + 1),
53+
range(y - radius, y + radius + 1)):
54+
if inFOV(map, x, y):
55+
yield(x, y)

0 commit comments

Comments
 (0)