Skip to content

Commit

Permalink
Refactoring pass, fix or suppress warnings
Browse files Browse the repository at this point in the history
Fix critical issues with samples_tcod.py
  • Loading branch information
HexDecimal committed Jan 16, 2024
1 parent 70ec2cc commit 392b0ce
Show file tree
Hide file tree
Showing 24 changed files with 170 additions and 170 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"ALTERASE",
"arange",
"ARCHS",
"arctan",
"asarray",
"ascontiguousarray",
"astar",
Expand Down Expand Up @@ -190,6 +191,7 @@
"intersphinx",
"isinstance",
"isort",
"issubdtype",
"itemsize",
"itleref",
"ivar",
Expand Down Expand Up @@ -338,6 +340,7 @@
"pypiwin",
"pypy",
"pytest",
"PYTHONHASHSEED",
"PYTHONOPTIMIZE",
"Pyup",
"quickstart",
Expand Down
50 changes: 25 additions & 25 deletions examples/samples_tcod.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def on_draw(self) -> None:
self.slide_corner_colors()
self.interpolate_corner_colors()
self.darken_background_characters()
self.randomize_sample_conole()
self.randomize_sample_console()
self.print_banner()

def slide_corner_colors(self) -> None:
Expand All @@ -143,7 +143,7 @@ def darken_background_characters(self) -> None:
sample_console.fg[:] = sample_console.bg[:]
sample_console.fg[:] //= 2

def randomize_sample_conole(self) -> None:
def randomize_sample_console(self) -> None:
# randomize sample console characters
sample_console.ch[:] = np.random.randint(
low=ord("a"),
Expand Down Expand Up @@ -406,7 +406,7 @@ def on_draw(self) -> None:
rect_h = 13
if self.implementation == tcod.noise.Implementation.SIMPLE:
rect_h = 10
sample_console.draw_semigraphics(self.img)
sample_console.draw_semigraphics(np.asarray(self.img))
sample_console.draw_rect(
2,
2,
Expand Down Expand Up @@ -669,7 +669,7 @@ def __init__(self) -> None:
self.using_astar = True
self.recalculate = False
self.busy = 0.0
self.oldchar = " "
self.old_char = " "

self.map = tcod.map.Map(SAMPLE_SCREEN_WIDTH, SAMPLE_SCREEN_HEIGHT)
for y in range(SAMPLE_SCREEN_HEIGHT):
Expand Down Expand Up @@ -778,33 +778,33 @@ def on_draw(self) -> None:
def ev_keydown(self, event: tcod.event.KeyDown) -> None:
if event.sym == tcod.event.KeySym.i and self.dy > 0:
# destination move north
libtcodpy.console_put_char(sample_console, self.dx, self.dy, self.oldchar, libtcodpy.BKGND_NONE)
libtcodpy.console_put_char(sample_console, self.dx, self.dy, self.old_char, libtcodpy.BKGND_NONE)
self.dy -= 1
self.oldchar = sample_console.ch[self.dx, self.dy]
self.old_char = sample_console.ch[self.dx, self.dy]
libtcodpy.console_put_char(sample_console, self.dx, self.dy, "+", libtcodpy.BKGND_NONE)
if SAMPLE_MAP[self.dx, self.dy] == " ":
self.recalculate = True
elif event.sym == tcod.event.KeySym.k and self.dy < SAMPLE_SCREEN_HEIGHT - 1:
# destination move south
libtcodpy.console_put_char(sample_console, self.dx, self.dy, self.oldchar, libtcodpy.BKGND_NONE)
libtcodpy.console_put_char(sample_console, self.dx, self.dy, self.old_char, libtcodpy.BKGND_NONE)
self.dy += 1
self.oldchar = sample_console.ch[self.dx, self.dy]
self.old_char = sample_console.ch[self.dx, self.dy]
libtcodpy.console_put_char(sample_console, self.dx, self.dy, "+", libtcodpy.BKGND_NONE)
if SAMPLE_MAP[self.dx, self.dy] == " ":
self.recalculate = True
elif event.sym == tcod.event.KeySym.j and self.dx > 0:
# destination move west
libtcodpy.console_put_char(sample_console, self.dx, self.dy, self.oldchar, libtcodpy.BKGND_NONE)
libtcodpy.console_put_char(sample_console, self.dx, self.dy, self.old_char, libtcodpy.BKGND_NONE)
self.dx -= 1
self.oldchar = sample_console.ch[self.dx, self.dy]
self.old_char = sample_console.ch[self.dx, self.dy]
libtcodpy.console_put_char(sample_console, self.dx, self.dy, "+", libtcodpy.BKGND_NONE)
if SAMPLE_MAP[self.dx, self.dy] == " ":
self.recalculate = True
elif event.sym == tcod.event.KeySym.l and self.dx < SAMPLE_SCREEN_WIDTH - 1:
# destination move east
libtcodpy.console_put_char(sample_console, self.dx, self.dy, self.oldchar, libtcodpy.BKGND_NONE)
libtcodpy.console_put_char(sample_console, self.dx, self.dy, self.old_char, libtcodpy.BKGND_NONE)
self.dx += 1
self.oldchar = sample_console.ch[self.dx, self.dy]
self.old_char = sample_console.ch[self.dx, self.dy]
libtcodpy.console_put_char(sample_console, self.dx, self.dy, "+", libtcodpy.BKGND_NONE)
if SAMPLE_MAP[self.dx, self.dy] == " ":
self.recalculate = True
Expand All @@ -822,10 +822,10 @@ def ev_mousemotion(self, event: tcod.event.MouseMotion) -> None:
mx = event.tile.x - SAMPLE_SCREEN_X
my = event.tile.y - SAMPLE_SCREEN_Y
if 0 <= mx < SAMPLE_SCREEN_WIDTH and 0 <= my < SAMPLE_SCREEN_HEIGHT and (self.dx != mx or self.dy != my):
libtcodpy.console_put_char(sample_console, self.dx, self.dy, self.oldchar, libtcodpy.BKGND_NONE)
libtcodpy.console_put_char(sample_console, self.dx, self.dy, self.old_char, libtcodpy.BKGND_NONE)
self.dx = mx
self.dy = my
self.oldchar = sample_console.ch[self.dx, self.dy]
self.old_char = sample_console.ch[self.dx, self.dy]
libtcodpy.console_put_char(sample_console, self.dx, self.dy, "+", libtcodpy.BKGND_NONE)
if SAMPLE_MAP[self.dx, self.dy] == " ":
self.recalculate = True
Expand Down Expand Up @@ -1139,7 +1139,7 @@ class NameGeneratorSample(Sample):
def __init__(self) -> None:
self.name = "Name generator"

self.curset = 0
self.current_set = 0
self.delay = 0.0
self.names: list[str] = []
self.sets: list[str] = []
Expand All @@ -1159,7 +1159,7 @@ def on_draw(self) -> None:
sample_console.print(
1,
1,
"%s\n\n+ : next generator\n- : prev generator" % self.sets[self.curset],
"%s\n\n+ : next generator\n- : prev generator" % self.sets[self.current_set],
fg=WHITE,
bg=None,
)
Expand All @@ -1175,18 +1175,18 @@ def on_draw(self) -> None:
self.delay += frame_length[-1]
if self.delay > 0.5:
self.delay -= 0.5
self.names.append(libtcodpy.namegen_generate(self.sets[self.curset]))
self.names.append(libtcodpy.namegen_generate(self.sets[self.current_set]))

def ev_keydown(self, event: tcod.event.KeyDown) -> None:
if event.sym == tcod.event.KeySym.EQUALS:
self.curset += 1
self.current_set += 1
self.names.append("======")
elif event.sym == tcod.event.KeySym.MINUS:
self.curset -= 1
self.current_set -= 1
self.names.append("======")
else:
super().ev_keydown(event)
self.curset %= len(self.sets)
self.current_set %= len(self.sets)


#############################################
Expand Down Expand Up @@ -1294,9 +1294,9 @@ def on_draw(self) -> None:
for v in range(RES_V - int_t, RES_V):
for u in range(RES_U):
tex_v = (v + int_abs_t) / float(RES_V)
texture[u, v] = tcod.noise_get_fbm(noise2d, [u / float(RES_U), tex_v], 32.0) + tcod.noise_get_fbm(
noise2d, [1 - u / float(RES_U), tex_v], 32.0
)
texture[u, v] = libtcodpy.noise_get_fbm(
noise2d, [u / float(RES_U), tex_v], 32.0
) + libtcodpy.noise_get_fbm(noise2d, [1 - u / float(RES_U), tex_v], 32.0)

# squared distance from center,
# clipped to sensible minimum and maximum values
Expand Down Expand Up @@ -1324,9 +1324,9 @@ def on_draw(self) -> None:
y = random.uniform(-0.5, 0.5)
strength = random.uniform(MIN_LIGHT_STRENGTH, 1.0)

color = tcod.Color(0, 0, 0) # create bright colors with random hue
color = libtcodpy.Color(0, 0, 0) # create bright colors with random hue
hue = random.uniform(0, 360)
tcod.color_set_hsv(color, hue, 0.5, strength)
libtcodpy.color_set_hsv(color, hue, 0.5, strength)
self.lights.append(Light(x, y, TEX_STRETCH, color.r, color.g, color.b, strength))

# eliminate lights that are going to be out of view
Expand Down
20 changes: 11 additions & 9 deletions tcod/_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
import warnings
from pathlib import Path
from types import TracebackType
from typing import Any, AnyStr, Callable, NoReturn, SupportsInt, TypeVar, cast
from typing import TYPE_CHECKING, Any, AnyStr, Callable, NoReturn, SupportsInt, TypeVar, cast

import numpy as np
from numpy.typing import ArrayLike, NDArray
from typing_extensions import Literal

from tcod.cffi import ffi, lib

if TYPE_CHECKING:
import tcod.image

FuncType = Callable[..., Any]
F = TypeVar("F", bound=FuncType)
T = TypeVar("T")
Expand Down Expand Up @@ -198,18 +201,17 @@ def _get_cdata_from_args(*args: Any, **kwargs: Any) -> Any: # noqa: ANN401
def __hash__(self) -> int:
return hash(self.cdata)

def __eq__(self, other: Any) -> Any:
try:
return self.cdata == other.cdata
except AttributeError:
def __eq__(self, other: object) -> bool:
if not isinstance(other, _CDataWrapper):
return NotImplemented
return bool(self.cdata == other.cdata)

def __getattr__(self, attr: str) -> Any:
def __getattr__(self, attr: str) -> Any: # noqa: ANN401
if "cdata" in self.__dict__:
return getattr(self.__dict__["cdata"], attr)
raise AttributeError(attr)

def __setattr__(self, attr: str, value: Any) -> None:
def __setattr__(self, attr: str, value: Any) -> None: # noqa: ANN401
if hasattr(self, "cdata") and hasattr(self.cdata, attr):
setattr(self.cdata, attr, value)
else:
Expand Down Expand Up @@ -240,7 +242,7 @@ def __init__(self, array: ArrayLike) -> None:
"""Initialize an image from the given array. May copy or reference the array."""
self._array: NDArray[np.uint8] = np.ascontiguousarray(array, dtype=np.uint8)
height, width, depth = self._array.shape
if depth != 3:
if depth != 3: # noqa: PLR2004
msg = f"Array must have RGB channels. Shape is: {self._array.shape!r}"
raise TypeError(msg)
self._buffer = ffi.from_buffer("TCOD_color_t[]", self._array)
Expand All @@ -265,7 +267,7 @@ def __init__(self, array: ArrayLike) -> None:
)


def _as_image(image: Any) -> TempImage:
def _as_image(image: ArrayLike | tcod.image.Image) -> TempImage | tcod.image.Image:
"""Convert this input into an Image-like object."""
if hasattr(image, "image_c"):
return image # type: ignore
Expand Down
4 changes: 2 additions & 2 deletions tcod/bsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def h(self) -> int: # noqa: D102
def h(self, value: int) -> None:
self.height = value

def _as_cdata(self) -> Any:
def _as_cdata(self) -> Any: # noqa: ANN401
cdata = ffi.gc(
lib.TCOD_bsp_new_with_size(self.x, self.y, self.width, self.height),
lib.TCOD_bsp_delete,
Expand All @@ -115,7 +115,7 @@ def __repr__(self) -> str:
status,
)

def _unpack_bsp_tree(self, cdata: Any) -> None:
def _unpack_bsp_tree(self, cdata: Any) -> None: # noqa: ANN401
self.x = cdata.x
self.y = cdata.y
self.width = cdata.w
Expand Down
Loading

0 comments on commit 392b0ce

Please sign in to comment.