Skip to content

Instantly share code, notes, and snippets.

@mrpoundsign
Last active January 12, 2016 00:22
Show Gist options
  • Save mrpoundsign/1250a1cea349bb0847da to your computer and use it in GitHub Desktop.
Save mrpoundsign/1250a1cea349bb0847da to your computer and use it in GitHub Desktop.
LustyMap complex map generator
#!/bin/bash
usage() {
echo "Usage: $0 [map_file=download.png] [threads=2xCPU] [prefix=map]"
exit 1
}
if [ -z $1 ]; then
MAP_FILE=download.png
fi
if [ -z $2 ]; then
PREFIX="map" # prefix for output files
fi
if [ -z $3 ]; then
THREAD_MAX=`echo "$(nproc)*2" | bc` # How many CPUs you have * 2
fi
if [ ! -f $MAP_FILE ]; then
echo "Could not find $MAP_FILE"
usage
fi
SIZE=$(identify -format "%w" $MAP_FILE) # The map size (must be square and divisable by 20)
if [ $SIZE -ne $(identify -format "%w" $MAP_FILE) ]; then
echo "Image must be square."
usage
fi
if [ $(echo "$SIZE%20" | bc) -ne "0" ]; then
echo "SIZE must be divisable by 20."
usage
fi
GRID_SIZE=$(echo "$SIZE/20" | bc)
echo "Grid size is $GRID_SIZE"
thread_max() {
local CHECK_INTERVAL="0.1"
local CUR_THREADS=
local MAX=
[[ $1 ]] && MAX=$1 || return 127
# reset MAX value, 0 is easy to remember
[ $MAX -eq 0 ] && {
MAX=1
echo "waiting for all tasks finish"
}
while true; do
CUR_THREADS=`jobs -p | wc -w`
# workaround about jobs bug. If don't execute it explicitily,
# CUR_THREADS will stick at 1, even no jobs running anymore.
jobs &>/dev/null
if [ $CUR_THREADS -ge $MAX ]; then
sleep $CHECK_INTERVAL
else
return 0
fi
done
}
rm *.jpeg
rm *.gz
echo -n "Generating 400 map files ($PREFIX-X-Y.jpeg)"
NUM=0
for x in {0..19}; do
(( xp = $x * $GRID_SIZE))
for y in {0..19}; do
(( yp = $y * $GRID_SIZE))
(( NUM = $NUM + 1 ))
thread_max $THREAD_MAX
echo -n " $NUM/400"
convert $MAP_FILE -crop ${GRID_SIZE}x${GRID_SIZE}+$xp+$yp $PREFIX-$y-$x.jpeg &
done
done
echo
thread_max 0
echo "Archiving to ${PREFIX}_parts.tar.gz"
tar czf ${PREFIX}_parts.tar.gz *.jpeg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment