Skip to content

Commit

Permalink
add pre-commit hook for version number
Browse files Browse the repository at this point in the history
  • Loading branch information
cff29546 committed Feb 8, 2025
1 parent bc21143 commit 29c6015
Show file tree
Hide file tree
Showing 19 changed files with 165 additions and 111 deletions.
18 changes: 18 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

echo pre-commit hook
echo -n "" > NEW_VERSION

cat VERSION | while read pv; do
path=`echo $pv | awk '{ OFS=" "; NF-=1; print $0 }'`
version=`echo $pv | awk '{ print $NF }'`
if git diff --cached --name-only $path | grep -E '.' > /dev/null ; then
old_version=$version
version=`echo $version | awk -F. '{ OFS="."; $NF+=1; print $0}'`
echo [$path] [$old_version]=\>[$version]
fi
echo $path $version>>NEW_VERSION
done
mv NEW_VERSION VERSION
git add VERSION

1 change: 1 addition & 0 deletions .githooks/setup.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
git config --local core.hooksPath .githooks/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pzmap2dzi is a command-line tool running on Windows to convert Project Zomboid m
</tr>
<tr>
<td>Marking</td>
<td>Basement</td>
<td>Basement with transparent roof layer</td>
<td>Local savegame trimmer</td>
<td>I18n support</td>
</tr>
Expand Down
2 changes: 2 additions & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pzmap2dzi main.py 1.0.1
html 1.0.0
13 changes: 2 additions & 11 deletions install_requirements.bat
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
python -c "import sys; exit(sys.version_info[0])"
if "%ERRORLEVEL%" == "3" (
python -m pip install -r "%~dp0requirements.txt"
) else (
if "%ERRORLEVEL%" == "2" (
python -m pip install -r "%~dp0requirements_python2.txt"
) else (
echo missing python2 or python3
)
)
pause
python -m pip install -r "%~dp0requirements.txt"
pause
149 changes: 91 additions & 58 deletions pzmap2dzi/pzdzi.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,111 +344,144 @@ def update_pz_map_info(self, info):


class IsoDZI(PZDZI):
SQR_HEIGHT = 64
HALF_SQR_HEIGHT = SQR_HEIGHT >> 1
SQR_WIDTH = 128
HALF_SQR_WIDTH = SQR_WIDTH >> 1
SQUARE_HEIGHT = 64
GRID_HEIGHT = SQUARE_HEIGHT // 2 # 32
SQUARE_WIDTH = 128
GRID_WIDTH = SQUARE_WIDTH // 2 # 64
LAYER_HEIGHT = 192
GRID_HEIGHT_PER_LAYER = LAYER_HEIGHT // GRID_HEIGHT # 6
# normal texture size w:128 h:256
TEXTURE_WIDTH = 128
TEXTURE_HEIGHT = 256
# jumbo tree texutre size w:384 h:512
LARGE_TEXTURE_WIDTH = 384
LARGE_TEXTURE_HEIGHT = 512

def get_sqr_center(self, gx, gy):
ox = gx * IsoDZI.HALF_SQR_WIDTH
oy = gy * IsoDZI.HALF_SQR_HEIGHT
ox = gx * IsoDZI.GRID_WIDTH
oy = gy * IsoDZI.GRID_HEIGHT
return ox, oy

def __init__(self, map_path, **options):
self.pz_init(map_path, **options)
self.sqr_height = IsoDZI.SQR_HEIGHT
self.sqr_width = IsoDZI.SQR_WIDTH
self.sqr_height = IsoDZI.SQUARE_HEIGHT
self.sqr_width = IsoDZI.SQUARE_WIDTH
self.tile_size = options.get('tile_size', 1024)
self.grid_per_tilex = self.tile_size // IsoDZI.HALF_SQR_WIDTH
self.grid_per_tiley = self.tile_size // IsoDZI.HALF_SQR_HEIGHT
self.grid_per_tilex = self.tile_size // IsoDZI.GRID_WIDTH
self.grid_per_tiley = self.tile_size // IsoDZI.GRID_HEIGHT
self.use_jumbo_tree = options.get('jumbo_tree_size', 3) > 3
self.output_margin = self.get_output_margin(True)
self.cell_margin = self.get_output_margin()
self.render_margin = options.get('render_margin', 'default')
if self.render_margin == 'default':
self.render_margin = self.get_default_render_margin()

assert self.tile_size % self.sqr_width == 0
assert self.tile_size % self.sqr_height == 0
assert len(self.cells) > 0
gxmin = None
gxmax = None
gymin = None
gymax = None
self.use_jumbo_tree = True
gxmin, gymin, gxmax, gymax = [None] * 4
for cx, cy in self.cells:
left, right, top, bottom = self.cell_grid_bound(cx, cy)
left, top, right, bottom = self.cell_grid_bound(cx, cy)
if gxmin is None:
gxmin = left
gxmax = right
gymin = top
gxmax = right
gymax = bottom
else:
gxmin = min(left, gxmin)
gxmax = max(right, gxmax)
gymin = min(top, gymin)
gxmax = max(right, gxmax)
gymax = max(bottom, gymax)

gxmin -= 2
gxmax += 2
gymin -= 2
gymax += 2
gbox = gxmin, gymin, gxmax, gymax
gxmin, gymin, gxmax, gymax = map(sum, zip(gbox, self.output_margin))

# grid offset
self.gxo = gxmin
self.gyo = gymin
self.gw = gxmax - gxmin + 1
self.gh = gymax - gymin + 1
w = self.gw * IsoDZI.HALF_SQR_WIDTH
h = self.gh * IsoDZI.HALF_SQR_HEIGHT
self.tile_gw = self.tile_size // IsoDZI.HALF_SQR_WIDTH
self.tile_gh = self.tile_size // IsoDZI.HALF_SQR_HEIGHT
self.use_jumbo_tree = options.get('jumbo_tree_size', 3) > 3
w = self.gw * IsoDZI.GRID_WIDTH
h = self.gh * IsoDZI.GRID_HEIGHT
self.grid_width_per_tile = self.tile_size // IsoDZI.GRID_WIDTH
self.grid_height_per_tile = self.tile_size // IsoDZI.GRID_HEIGHT
DZI.__init__(self, w, h, **options)

def get_output_margin(self, use_large_texture=False):
texture_width = IsoDZI.TEXTURE_WIDTH
texture_height = IsoDZI.TEXTURE_HEIGHT
if use_large_texture or self.use_jumbo_tree:
texture_width = IsoDZI.LARGE_TEXTURE_WIDTH
texture_height = IsoDZI.LARGE_TEXTURE_HEIGHT
width = (texture_width // 2) // IsoDZI.GRID_WIDTH
left = -width
right = width

# +1 for grid center to grid bottom
top = 1 - IsoDZI.GRID_HEIGHT_PER_LAYER * self.maxlayer
top -= (texture_height // IsoDZI.GRID_HEIGHT)
# +1 for grid center to grid bottom
bottom = IsoDZI.GRID_HEIGHT_PER_LAYER * (-self.minlayer) + 1
return left, top, right, bottom

def get_default_render_margin(self):
# render grid neighbours for tile
texture_width = IsoDZI.TEXTURE_WIDTH
texture_height = IsoDZI.TEXTURE_HEIGHT
if self.use_jumbo_tree:
texture_width = IsoDZI.LARGE_TEXTURE_WIDTH
texture_height = IsoDZI.LARGE_TEXTURE_HEIGHT
width = (texture_width // 2) // IsoDZI.GRID_WIDTH - 1
left = -width
right = width
top = 0
bottom = (texture_height // IsoDZI.GRID_HEIGHT) - 2
return left, top, right, bottom

def tile2grid(self, tx, ty, layer):
gx = self.gxo + self.grid_per_tilex * tx
gy = self.gyo + self.grid_per_tiley * ty + layer*6
gy = self.gyo + self.grid_per_tiley * ty
gy += IsoDZI.GRID_HEIGHT_PER_LAYER * layer
return gx, gy

def tile_grid_bound(self, tx, ty, layer):
left, top = self.tile2grid(tx, ty, layer)
right, bottom = self.tile2grid(tx + 1, ty + 1, layer)
if self.use_jumbo_tree:
return (left - 2, right + 2, top, bottom + 14)
else:
return (left, right, top, bottom + 6)

def square_grid_bound(self, sx1, sy1, sx2, sy2):
top = sx1 + sy1
bottom = sx2 + sy2
left = sx1 - sy2
top = sx1 + sy1
right = sx2 - sy1
return top, bottom, left, right
bottom = sx2 + sy2
return left, top, right, bottom

def cell_grid_bound(self, cx, cy):
sx = cx * self.cell_size
sy = cy * self.cell_size
sxmax = sx + self.cell_size - 1
symax = sy + self.cell_size - 1
top, bottom, left, right = self.square_grid_bound(sx, sy, sxmax, symax)
if self.use_jumbo_tree:
return (left - 2, right + 2, top - max(14, 6*self.maxlayer), bottom - 6*self.minlayer)
else:
return (left, right, top - 6*self.maxlayer, bottom - 6*self.minlayer)
sxmin = cx * self.cell_size
symin = cy * self.cell_size
sxmax = sxmin + self.cell_size - 1
symax = symin + self.cell_size - 1
return self.square_grid_bound(sxmin, symin, sxmax, symax)

def cell2tiles(self, cx, cy):
left, right, top, bottom = self.cell_grid_bound(cx, cy)
txmin = (left - self.gxo - 1) // self.tile_gw
txmax = (right - self.gxo) // self.tile_gw
tymin = (top - self.gyo - 1) // self.tile_gh
tymax = (bottom - self.gyo) // self.tile_gh
gbox = map(sum, zip(self.cell_grid_bound(cx, cy), self.cell_margin))
left, top, right, bottom = gbox
txmin = (left - self.gxo - 1) // self.grid_width_per_tile
txmax = (right - self.gxo) // self.grid_width_per_tile
tymin = (top - self.gyo - 1) // self.grid_height_per_tile
tymax = (bottom - self.gyo) // self.grid_height_per_tile
tiles = [(tx, ty) for ty in range(tymin, tymax + 1)
for tx in range(txmin, txmax + 1)]
return tiles

def render_tile(self, im_getter, tx, ty, layer, layer_cache):
self.render_below(im_getter, layer, layer_cache)
gx0, gy0 = self.tile2grid(tx, ty, layer)
left, right, top, bottom = self.tile_grid_bound(tx, ty, layer)

if hasattr(self.render, 'tile'):
return self.render.tile(im_getter, self, gx0, gy0, left, right, top, bottom, layer)
return self.render.tile(im_getter, self, gx0, gy0, layer)

gx1 = gx0 + self.grid_per_tilex
gy1 = gy0 + self.grid_per_tiley
gbox = gx0, gy0, gx1, gy1
if self.render_margin:
gbox = map(sum, zip(gbox, self.render_margin))
left, top, right, bottom = gbox
for gy in range(top, bottom + 1):
for gx in range(left, right + 1):
if (gx + gy) & 1:
Expand All @@ -460,9 +493,9 @@ def render_tile(self, im_getter, tx, ty, layer, layer_cache):

def update_map_info(self, info):
info = self.update_pz_map_info(info)
info['x0'] = -self.gxo * IsoDZI.HALF_SQR_WIDTH
info['y0'] = -(self.gyo + 1) * IsoDZI.HALF_SQR_HEIGHT
info['sqr'] = 2 * IsoDZI.HALF_SQR_WIDTH
info['x0'] = -self.gxo * IsoDZI.GRID_WIDTH
info['y0'] = -(self.gyo + 1) * IsoDZI.GRID_HEIGHT
info['sqr'] = 2 * IsoDZI.GRID_WIDTH
return info


Expand Down
20 changes: 10 additions & 10 deletions pzmap2dzi/render_impl/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ def render_text(draw, x, y, text, color, font):
text, color, font)


def draw_square(draw, x, y, color, size=1):
h = pzdzi.IsoDZI.HALF_SQR_HEIGHT
w = pzdzi.IsoDZI.HALF_SQR_WIDTH
def draw_square(draw, x, y, color, sizew=1, sizeh=1):
h = pzdzi.IsoDZI.GRID_HEIGHT
w = pzdzi.IsoDZI.GRID_WIDTH
path = [
x , y - h , # top
x + size*w, y - h + size*h , # right
x , y - h + 2*size*h, # bottom
x - size*w, y - h + size*h , # left
x , y - h , # top
x + sizew * w , y + (sizew - 1) * h , # right
x + (sizew - sizeh) * w, y + (sizew + sizeh - 1) * h, # bottom
x - sizeh * w , y + (sizeh - 1) * h , # left
]
draw.polygon(path, fill=color)

Expand Down Expand Up @@ -75,7 +75,7 @@ def break_long_text(text):

def render_long_text(draw, x, y, text, color, font):
dx, dy, w, h = text_size(draw, text, font)
if w >= pzdzi.IsoDZI.SQR_WIDTH:
if w >= pzdzi.IsoDZI.SQUARE_WIDTH:
text = break_long_text(text)
dx, dy, w, h = text_size(draw, text, font)
draw.text((x - dx - w // 2, y - dy - h // 2),
Expand All @@ -86,8 +86,8 @@ def render_long_text(draw, x, y, text, color, font):
_PAD_X = 10
def render_edge(draw, x, y, color, width, border_flags):
edges = geometry.get_edge_segments(border_flags, x, y,
pzdzi.IsoDZI.HALF_SQR_WIDTH,
pzdzi.IsoDZI.HALF_SQR_HEIGHT,
pzdzi.IsoDZI.GRID_WIDTH,
pzdzi.IsoDZI.GRID_HEIGHT,
_PAD_X, _PAD_Y)
for edge in edges:
draw.line(edge, fill=color, width=width)
5 changes: 5 additions & 0 deletions pzmap2dzi/render_impl/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def __init__(self, **options):
def update_options(self, options):
options['render_minlayer'] = 0
options['render_maxlayer'] = 1
options['render_margin'] = None
return options

def valid_cell_B41(self, x, y):
Expand Down Expand Up @@ -156,6 +157,10 @@ def __init__(self, **options):
self.getter = pzobjects.CachedBorderLabelMapGetter(objects_path, types)
self.cells = set(self.getter.get_cell_zones().keys())

def update_options(self, options):
options['render_margin'] = [-2, -2, 2, 2] # add margin for text
return options

def valid_cell(self, x, y):
return (x, y) in self.cells

Expand Down
4 changes: 4 additions & 0 deletions pzmap2dzi/render_impl/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ def __init__(self, **options):
font_size = options.get('default_font_size', 20)
self.font = LazyFont(font_name, int(font_size))

def update_options(self, options):
options['render_margin'] = [-2, -2, 2, 2] # add margin for text
return options

def square(self, im_getter, dzi, ox, oy, sx, sy, layer):
cx, subx = divmod(sx, dzi.cell_size)
cy, suby = divmod(sy, dzi.cell_size)
Expand Down
23 changes: 15 additions & 8 deletions pzmap2dzi/render_impl/zombie.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,28 @@ def __init__(self, **options):
def update_options(self, options):
options['render_minlayer'] = 0
options['render_maxlayer'] = 1
options['render_margin'] = None
return options

def valid_cell(self, x, y):
return load_cell(self.input, x, y) is not None

def tile(self, im_getter, dzi, gx0, gy0, gl, gr, gt, gb, layer):
def tile(self, im_getter, dzi, gx0, gy0, layer):
draw = None
sxmax = (gb + gr) >> 1
symax = (gb - gl) >> 1
sxmin = (gt + gl) >> 1
symin = (gt - gr) >> 1
for blockx in range(sxmin // dzi.block_size, (sxmax // dzi.block_size) + 1):
gx1 = gx0 + dzi.grid_per_tilex
gy1 = gy0 + dzi.grid_per_tiley
sxmax = (gy1 + gx1) >> 1
symax = (gy1 - gx0) >> 1
sxmin = (gy0 + gx0) >> 1
symin = (gy0 - gx1) >> 1
blockxmax = sxmax // dzi.block_size + 1
blockymax = symax // dzi.block_size + 1
blockxmin = sxmin // dzi.block_size
blockymin = symin // dzi.block_size
for blockx in range(blockxmin, blockxmax):
sx = blockx * dzi.block_size
cx, bx = divmod(blockx, dzi.cell_size_in_block)
for blocky in range(symin // dzi.block_size, (symax // dzi.block_size) + 1):
for blocky in range(blockymin, blockymax):
cy, by = divmod(blocky, dzi.cell_size_in_block)
zpop = load_cell(self.input, cx, cy)
if not zpop:
Expand All @@ -78,7 +85,7 @@ def tile(self, im_getter, dzi, gx0, gy0, gl, gr, gt, gb, layer):
if draw == None:
im = im_getter.get()
draw = ImageDraw.Draw(im)
draw_square(draw, ox, oy, color, dzi.block_size)
draw_square(draw, ox, oy, color, dzi.block_size, dzi.block_size)
gxz = gx + dzi.block_size - 1
gyz = gy + dzi.block_size - 1
oxz, oyz = dzi.get_sqr_center(gxz - gx0, gyz - gy0)
Expand Down
3 changes: 2 additions & 1 deletion pzmap2dzi/texture.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def color_sum(pixels):

class Texture(object):
def __init__(self, im, offset=None):
# offset is from the bottom center of the square
if offset:
ox, oy = offset
else:
Expand Down Expand Up @@ -293,7 +294,7 @@ def blend_textures(self, names):
w, h = 384, 512
im = Image.new('RGBA', (w, h))
x = w >> 1
y = h # - (pzdzi.SQR_HEIGHT // 2)
y = h
for name in names:
t = self.get_by_name(name)
if t:
Expand Down
Loading

0 comments on commit 29c6015

Please sign in to comment.