Skip to content

Commit 514cc49

Browse files
committed
Fix breaking changes from libtcod.
Private types in libtcod heaps and random were changed.
1 parent d11f17d commit 514cc49

File tree

7 files changed

+27
-31
lines changed

7 files changed

+27
-31
lines changed

build_libtcod.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838
RE_PREPROCESSOR = re.compile(r"(?!#define\s+\w+\s+\d+$)#.*?(?<!\\)$", re.DOTALL | re.MULTILINE)
3939
RE_INCLUDE = re.compile(r'#include "([^"]*)"')
4040
RE_TAGS = re.compile(
41-
r"TCODLIB_C?API|TCOD_PUBLIC|TCOD_NODISCARD|TCOD_DEPRECATED_NOMESSAGE"
41+
r"TCODLIB_C?API|TCOD_PUBLIC|TCOD_NODISCARD|TCOD_DEPRECATED_NOMESSAGE|TCOD_DEPRECATED_ENUM"
4242
r"|(TCOD_DEPRECATED|TCODLIB_FORMAT)\([^)]*\)|__restrict"
4343
)
44-
RE_VAFUNC = re.compile(r".*\(.*va_list.*\);")
44+
RE_VAFUNC = re.compile(r"^[^;]*\([^;]*va_list.*\);", re.MULTILINE)
4545
RE_INLINE = re.compile(r"(^.*?inline.*?\(.*?\))\s*\{.*?\}$", re.DOTALL | re.MULTILINE)
4646

4747

libtcod

Submodule libtcod updated 195 files

tcod/cffi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Anything included here will be accessible from tcod.loader.lib */
33
#include "../libtcod/src/libtcod/libtcod.h"
44
#include "../libtcod/src/libtcod/libtcod_int.h"
5+
#include "../libtcod/src/libtcod/renderer_xterm.h"
56
#include "../libtcod/src/libtcod/tileset_truetype.h"
67
#include "../libtcod/src/libtcod/wrappers.h"
78
#include "noise.h"

tcod/libtcodpy.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3663,7 +3663,7 @@ def random_get_instance() -> tcod.random.Random:
36633663
Returns:
36643664
Random: A Random instance using the default random number generator.
36653665
"""
3666-
return tcod.random.Random._new_from_cdata(ffi.cast("mersenne_data_t*", lib.TCOD_random_get_instance()))
3666+
return tcod.random.Random._new_from_cdata(lib.TCOD_random_get_instance())
36673667

36683668

36693669
@pending_deprecate()
@@ -3809,10 +3809,7 @@ def random_save(rnd: Optional[tcod.random.Random]) -> tcod.random.Random:
38093809
"""
38103810
return tcod.random.Random._new_from_cdata(
38113811
ffi.gc(
3812-
ffi.cast(
3813-
"mersenne_data_t*",
3814-
lib.TCOD_random_save(rnd.random_c if rnd else ffi.NULL),
3815-
),
3812+
lib.TCOD_random_save(rnd.random_c if rnd else ffi.NULL),
38163813
lib.TCOD_random_delete,
38173814
)
38183815
)

tcod/path.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,9 @@ int update_frontier_heuristic(
440440
for (int i = 0; i < frontier->heap.size; ++i) {
441441
unsigned char* heap_ptr = (unsigned char*)frontier->heap.heap;
442442
heap_ptr += frontier->heap.node_size * i;
443-
struct TCOD_HeapNode* heap_node = (void*)heap_ptr;
444-
struct FrontierNode* f_node = (struct FrontierNode*)heap_node->data;
445-
heap_node->priority = (f_node->distance + compute_heuristic(heuristic, frontier->ndim, f_node->index));
443+
int* priority = (int*)heap_ptr;
444+
struct FrontierNode* f_node = (struct FrontierNode*)(heap_ptr + frontier->heap.data_offset);
445+
*priority = (f_node->distance + compute_heuristic(heuristic, frontier->ndim, f_node->index));
446446
}
447447
TCOD_minheap_heapify(&frontier->heap);
448448
return 0;
@@ -478,8 +478,7 @@ int frontier_has_index(
478478
for (int i = 0; i < frontier->heap.size; ++i) {
479479
const unsigned char* heap_ptr = (const unsigned char*)frontier->heap.heap;
480480
heap_ptr += frontier->heap.node_size * i;
481-
const struct TCOD_HeapNode* heap_node = (const void*)heap_ptr;
482-
const struct FrontierNode* f_node = (const void*)heap_node->data;
481+
const struct FrontierNode* f_node = (const void*)(heap_ptr + frontier->heap.data_offset);
483482
bool found = 1;
484483
for (int j = 0; j < frontier->ndim; ++j) {
485484
if (index[j] != f_node->index[j]) {

tcod/random.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,7 @@ def __init__(
7575
seed = hash(seed)
7676

7777
self.random_c = ffi.gc(
78-
ffi.cast(
79-
"mersenne_data_t*",
80-
lib.TCOD_random_new_from_seed(algorithm, seed & 0xFFFFFFFF),
81-
),
78+
lib.TCOD_random_new_from_seed(algorithm, seed & 0xFFFFFFFF),
8279
lib.TCOD_random_delete,
8380
)
8481

@@ -141,22 +138,24 @@ def __getstate__(self) -> Any:
141138
"""Pack the self.random_c attribute into a portable state."""
142139
state = self.__dict__.copy()
143140
state["random_c"] = {
144-
"algo": self.random_c.algo,
145-
"distribution": self.random_c.distribution,
146-
"mt": list(self.random_c.mt),
147-
"cur_mt": self.random_c.cur_mt,
148-
"Q": list(self.random_c.Q),
149-
"c": self.random_c.c,
150-
"cur": self.random_c.cur,
141+
"mt_cmwc": {
142+
"algorithm": self.random_c.mt_cmwc.algorithm,
143+
"distribution": self.random_c.mt_cmwc.distribution,
144+
"mt": list(self.random_c.mt_cmwc.mt),
145+
"cur_mt": self.random_c.mt_cmwc.cur_mt,
146+
"Q": list(self.random_c.mt_cmwc.Q),
147+
"c": self.random_c.mt_cmwc.c,
148+
"cur": self.random_c.mt_cmwc.cur,
149+
}
151150
}
152151
return state
153152

154153
def __setstate__(self, state: Any) -> None:
155154
"""Create a new cdata object with the stored paramaters."""
156-
try:
157-
cdata = state["random_c"]
158-
except KeyError: # old/deprecated format
159-
cdata = state["cdata"]
160-
del state["cdata"]
161-
state["random_c"] = ffi.new("mersenne_data_t*", cdata)
155+
if "algo" in state["random_c"]:
156+
# Handle old/deprecated format. Covert to libtcod's new union type.
157+
state["random_c"]["algorithm"] = state["random_c"]["algo"]
158+
del state["random_c"]["algo"]
159+
state["random_c"] = {"mt_cmwc": state["random_c"]}
160+
state["random_c"] = ffi.new("TCOD_Random*", state["random_c"])
162161
self.__dict__.update(state)

tests/test_random.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def test_tcod_random_pickle() -> None:
2929
assert rand.uniform(0, 1) == rand2.uniform(0, 1)
3030

3131

32-
def test_load_rng_v13() -> None:
32+
def test_load_rng_v13_1() -> None:
3333
with open(pathlib.Path(__file__).parent / "data/random_v13.pkl", "rb") as f:
3434
rand: tcod.random.Random = pickle.load(f)
3535
assert rand.randint(0, 0xFFFF) == 56422

0 commit comments

Comments
 (0)