Skip to content

Commit b444f0a

Browse files
committed
Add more typehints (removing now-redundant world.pxd)
1 parent 6e56be2 commit b444f0a

12 files changed

+110
-240
lines changed

biome_explorer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from terrain import BiomeGenerator
44
import globals as G
55

6-
with open(os.path.join(G.game_dir, "world", "seed"), "rb") as f:
6+
with open(os.path.join(G.game_dir, "world", "seed"), "r") as f:
77
SEED = f.read()
88

99
b = BiomeGenerator(SEED)

controllers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ def on_close(self):
691691
def show_map(self):
692692
print("map called...")
693693
# taken from Nebual's biome_explorer, this is ment to be a full screen map that uses mini tiles to make a full 2d map.
694-
with open(os.path.join(G.game_dir, "world", "seed"), "rb") as f:
694+
with open(os.path.join(G.game_dir, "world", "seed"), "r") as f:
695695
SEED = f.read()
696696
b = BiomeGenerator(SEED)
697697
x, y, z = self.player.position
@@ -719,7 +719,7 @@ def show_map(self):
719719
tmap = letters[b.get_biome_type(x,y)]
720720
tile_map = load_image('resources', 'textures', tmap +'.png')
721721
tile_map.anchor_x = x * 8
722-
tile_map.anchor_Y = y * 8
722+
tile_map.anchor_y = y * 8
723723
sprite = pyglet.sprite.Sprite(tile_map, x=x * 8, y=y * 8, batch=pbatch)
724724
game_map = image_sprite(tile_map, pbatch, pgroup, x * 8, y * 8, 8, 8)
725725
game_map = pyglet.sprite.Sprite(image,x=G.WINDOW_WIDTH, y=G.WINDOW_HEIGHT,batch=pbatch, group=pgroup)

custom_types.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from typing import Tuple
2+
3+
iVector = Tuple[int, int, int]
4+
fVector = Tuple[float, float, float]

main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22

33
# Imports, sorted alphabetically.
44

savingsystem.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
import struct
88
import time
99
import sqlite3
10+
from typing import Optional
1011

1112
# Third-party packages
1213
# Nothing for now...
1314

1415
# Modules from this project
16+
from custom_types import iVector
17+
1518
from blocks import BlockID
1619
from debug import performance_info
1720
import globals as G
@@ -34,22 +37,22 @@
3437
null1024 = null2*512 #1024 \0's
3538
air = G.BLOCKS_DIR[(0,0)]
3639

37-
def sector_to_filename(secpos: (int, int, int)) -> str:
40+
def sector_to_filename(secpos: iVector) -> str:
3841
x,y,z = secpos
3942
return "%i.%i.%i.pyr" % (x//4, y//4, z//4)
4043

41-
def region_to_filename(region: (int, int, int)) -> str:
44+
def region_to_filename(region: iVector) -> str:
4245
return "%i.%i.%i.pyr" % region
4346

44-
def sector_to_region(secpos: (int, int, int)) -> (int, int, int):
47+
def sector_to_region(secpos: iVector) -> iVector:
4548
x,y,z = secpos
4649
return (x//4, y//4, z//4)
4750

48-
def sector_to_offset(secpos: (int, int, int)) -> int:
51+
def sector_to_offset(secpos: iVector) -> int:
4952
x,y,z = secpos
5053
return ((x % 4)*16 + (y % 4)*4 + (z % 4)) * 1024
5154

52-
def sector_to_blockpos(secpos: (int, int, int)) -> (int, int, int):
55+
def sector_to_blockpos(secpos: iVector) -> iVector:
5356
x,y,z = secpos
5457
return x*8, y*8, z*8
5558

@@ -68,7 +71,7 @@ def connect_db(world=None):
6871
return sqlite3.connect(os.path.join(world_dir, G.DB_NAME))
6972

7073

71-
def save_sector_to_bytes(blocks, secpos: (int, int, int)) -> bytes:
74+
def save_sector_to_bytes(blocks, secpos: iVector) -> bytes:
7275
"""
7376
7477
:type blocks: world_server.WorldServer
@@ -150,8 +153,7 @@ def sector_exists(sector, world=None):
150153
if world is None: world = "world"
151154
return os.path.lexists(os.path.join(G.game_dir, world, sector_to_filename(sector)))
152155

153-
def load_region(world, world_name: str = None, region: (int, int, int) = None, sector: (int, int, int) = None):
154-
if world_name is None: world_name = "world"
156+
def load_region(world, world_name: str = "world", region: Optional[iVector] = None, sector: Optional[iVector] = None):
155157
sectors = world.sectors
156158
blocks = world
157159
SECTOR_SIZE = G.SECTOR_SIZE

server.py

+7-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
# Python packages
1+
#!/usr/bin/env python3
2+
23
from _socket import SHUT_RDWR
34
import socket
45
import struct
56
import time
67
import timer
7-
8-
try: # Python 3
9-
import socketserver
10-
except ImportError: # Python 2
11-
import socketserver as socketserver
8+
import socketserver
129
import threading
13-
# Third-party packages
1410

15-
# Modules from this project
1611
import globals as G
1712
from savingsystem import save_sector_to_bytes, save_blocks, save_world, load_player, save_player
1813
from world_server import WorldServer
@@ -32,11 +27,11 @@ def sendpacket(self, size: int, packet: bytes):
3227
self.request.sendall(struct.pack("i", 5 + size) + packet)
3328

3429
def sendchat(self, txt: str, color=(255,255,255,255)):
35-
txt = txt.encode('utf-8')
36-
self.sendpacket(len(txt) + 4, b"\5" + txt + struct.pack("BBBB", *color))
30+
txt_bytes = txt.encode('utf-8')
31+
self.sendpacket(len(txt_bytes) + 4, b"\5" + txt_bytes + struct.pack("BBBB", *color))
3732
def sendinfo(self, info: str, color=(255,255,255,255)):
38-
info = info.encode('utf-8')
39-
self.sendpacket(len(info) + 4, b"\5" + info + struct.pack("BBBB", *color))
33+
info_bytes = info.encode('utf-8')
34+
self.sendpacket(len(info_bytes) + 4, b"\5" + info_bytes + struct.pack("BBBB", *color))
4035
def broadcast(self, txt: str):
4136
for player in self.server.players.values():
4237
player.sendchat(txt)

terrain.pxd

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import cython
2-
cimport world
32
cimport perlin
43

54
#cython: boundscheck=False

utils.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
# Python packages
44
import os
55
import struct
6+
from typing import Tuple, List
67

78
# Third-party packages
89
import pyglet
910
from pyglet.gl import *
1011

1112
# Modules from this project
1213
import globals as G
14+
from custom_types import iVector, fVector
1315

1416

1517
__all__ = (
@@ -81,7 +83,7 @@ def get_block_icon(block, icon_size, world):
8183
return block_icon
8284

8385

84-
FACES = (
86+
FACES: Tuple[iVector, ...] = (
8587
( 0, 1, 0),
8688
( 0, -1, 0),
8789
(-1, 0, 0),
@@ -90,7 +92,7 @@ def get_block_icon(block, icon_size, world):
9092
( 0, 0, -1),
9193
)
9294

93-
FACES_WITH_DIAGONALS = FACES + (
95+
FACES_WITH_DIAGONALS: Tuple[iVector, ...] = FACES + (
9496
(-1, -1, 0),
9597
(-1, 0, -1),
9698
( 0, -1, -1),
@@ -133,12 +135,12 @@ def normalize_float(f: float) -> int:
133135
return int_f - 1
134136

135137

136-
def normalize(position: (float, float, float)) -> (float, float, float):
138+
def normalize(position: fVector) -> fVector:
137139
x, y, z = position
138140
return normalize_float(x), normalize_float(y), normalize_float(z)
139141

140142

141-
def sectorize(position: (int, int, int)) -> (int, int, int):
143+
def sectorize(position: iVector) -> iVector:
142144
x, y, z = normalize(position)
143145
x, y, z = (x // G.SECTOR_SIZE,
144146
y // G.SECTOR_SIZE,
@@ -164,13 +166,19 @@ def unset_state(self):
164166
def make_int_packet(i: int) -> bytes:
165167
return struct.pack('i', i)
166168

167-
def extract_int_packet(packet):
169+
def extract_int_packet(packet: bytes):
170+
"""
171+
:rtype: (bytes, int)
172+
"""
168173
return packet[4:], struct.unpack('i', packet[:4])[0]
169174

170175
def make_string_packet(s: str) -> bytes:
171176
return struct.pack('i', len(s)) + s.encode('utf-8')
172177

173-
def extract_string_packet(packet: bytes) -> (bytes, str):
178+
def extract_string_packet(packet: bytes):
179+
"""
180+
:rtype: (bytes, str)
181+
"""
174182
strlen = struct.unpack('i', packet[:4])[0]
175183
packet = packet[4:]
176184
s = packet[:strlen].decode('utf-8')

world.pxd

-100
This file was deleted.

0 commit comments

Comments
 (0)