2015-02-19 16:02:56 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
MAPDIR=$1
|
|
|
|
SPAWNPOS=$2
|
|
|
|
DIMENSIONS=$3
|
|
|
|
|
|
|
|
if [ -z $MAPPERDIR ]; then
|
|
|
|
MAPPERDIR=.
|
|
|
|
fi
|
|
|
|
|
2016-01-17 17:40:12 +01:00
|
|
|
mapperpath=$MAPPERDIR/minetestmapper
|
|
|
|
|
|
|
|
if [ ! -f $mapperpath ]; then
|
|
|
|
echo "Error, "$mapperpath" doesn't exist."
|
|
|
|
echo "Please specify the path to the minetestmapper executable via the MAPPERDIR variable"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2016-01-17 19:32:15 +01:00
|
|
|
if [ -z "$JOBNUM" ]; then
|
|
|
|
JOBNUM=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
case "$JOBNUM" in
|
|
|
|
''|*[!0-9]*) echo "Bad number of jobs '$JOBNUM'. Please specify a positive integer." ; exit 1 ;;
|
|
|
|
*) ;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
# echo "Number of jobs: $JOBNUM"
|
|
|
|
|
2016-01-17 20:34:34 +01:00
|
|
|
prefix_func="\
|
|
|
|
prefix() { \
|
|
|
|
while read line ; do \
|
|
|
|
echo \"\${1}\${line}\"; \
|
|
|
|
done \
|
|
|
|
}"
|
|
|
|
|
|
|
|
# retains || usage when doing | prefix "sth"
|
|
|
|
# use it when needed
|
|
|
|
prefix_pipefail="\
|
|
|
|
set -o pipefail ; \
|
|
|
|
$prefix_func"
|
|
|
|
|
|
|
|
# prefix should be usable within this script as well
|
|
|
|
eval $prefix_func
|
|
|
|
|
|
|
|
#sh -c "$prefix_func ; echo -e 'a\nb\nc\n' | prefix 'PREF '"
|
|
|
|
#bash -c "$prefix_pipefail ; (echo -e 'a\nb\nc\n' ; false) | prefix 'PREF '" || echo "ERR"
|
|
|
|
#exit 1
|
|
|
|
|
2015-02-19 16:02:56 +01:00
|
|
|
scriptdir=`readlink -f $0`
|
|
|
|
scriptdir=`dirname $scriptdir`
|
|
|
|
|
|
|
|
tiledir=$scriptdir/www/tiles
|
|
|
|
|
|
|
|
spawnx=${SPAWNPOS%%,*}
|
|
|
|
spawny=${SPAWNPOS##*,}
|
|
|
|
#echo "spawn position: $spawnx $spawny"
|
|
|
|
tilesize=256
|
|
|
|
zoomlevelnum=3
|
|
|
|
|
|
|
|
tilenum=$(($DIMENSIONS/$tilesize))
|
|
|
|
|
|
|
|
mkdir -p ${tiledir}/20
|
|
|
|
rm -f ${tiledir}/20/*
|
|
|
|
|
2016-01-17 19:32:15 +01:00
|
|
|
jobparam="-P$JOBNUM"
|
|
|
|
|
2015-02-19 16:02:56 +01:00
|
|
|
#create tile images
|
|
|
|
for x in $(seq 0 $tilenum)
|
|
|
|
do
|
|
|
|
for y in $(seq 0 $tilenum)
|
|
|
|
do
|
|
|
|
posx=$(($spawnx+$tilesize*($x-$tilenum/2)))
|
|
|
|
posy=$(($spawny+$tilesize*($tilenum/2-$y)))
|
2016-01-17 19:32:15 +01:00
|
|
|
# Execute sh -c "something"
|
|
|
|
echo "-c"
|
2016-01-17 20:34:34 +01:00
|
|
|
echo "$prefix_pipefail ; $mapperpath ${MAPPERPARAMS} -i ${MAPDIR} --geometry ${posx},${posy}+${tilesize}+${tilesize} -o ${tiledir}/20/map_${x}_${y}.png \
|
2016-01-17 20:43:00 +01:00
|
|
|
2> >(>&2 prefix '[TILEGEN $x,$y ERR]: ') | prefix '[TILEGEN $x,$y]: ' \
|
2016-01-17 20:34:34 +01:00
|
|
|
|| (>&2 echo 'minetesmapper for tile [${x},${y}] ended with non zero exit code'; exit 255)"
|
2015-02-19 16:02:56 +01:00
|
|
|
done
|
2016-01-17 20:34:34 +01:00
|
|
|
done | xargs -n2 $jobparam -d '\n' bash # bash required because of "set -o pipefail" usage
|
2016-01-17 19:32:15 +01:00
|
|
|
|
|
|
|
xargs_exit=$?
|
|
|
|
if [ $xargs_exit -ne 0 ]; then
|
|
|
|
# xargs unfortunately exits immediately, without even waiting for sub-processes
|
|
|
|
sleep 1
|
|
|
|
>&2 echo "Error: xargs ended with non-zero exit code $xargs_exit."
|
|
|
|
exit 1
|
|
|
|
fi
|
2015-02-19 16:02:56 +01:00
|
|
|
|
|
|
|
#join the images and make them smaller
|
|
|
|
mult=1
|
|
|
|
for s in $(seq 1 $zoomlevelnum)
|
|
|
|
do
|
|
|
|
mult=$(($mult*2))
|
|
|
|
tnum=$(($tilenum/$mult-1))
|
|
|
|
zoomlevel=$((20-$s))
|
|
|
|
zoomlevelbefore=$((20-$s+1))
|
|
|
|
dir=$tiledir/$zoomlevel
|
|
|
|
dirb=$tiledir/$zoomlevelbefore
|
|
|
|
mkdir -p $dir
|
|
|
|
rm -f ${dir}/20/*
|
|
|
|
for x in $(seq 0 $tnum)
|
|
|
|
do
|
|
|
|
for y in $(seq 0 $tnum)
|
|
|
|
do
|
|
|
|
montage $dirb/map_$(($x*2))_$((y*2)).png $dirb/map_$(($x*2+1))_$((y*2)).png $dirb/map_$(($x*2))_$((y*2+1)).png $dirb/map_$(($x*2+1))_$((y*2+1)).png -geometry +0+0 $dir/map_${x}_${y}.png
|
2016-01-17 18:11:14 +01:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
echo "montage exited with non zero exit code, aborting."
|
|
|
|
exit 1
|
|
|
|
fi
|
2015-02-19 16:02:56 +01:00
|
|
|
convert $dir/map_${x}_${y}.png -resize 50% $dir/map_${x}_${y}.png
|
2016-01-17 18:11:14 +01:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
echo "convert exited with non zero exit code, aborting."
|
|
|
|
exit 1
|
|
|
|
fi
|
2015-02-19 16:02:56 +01:00
|
|
|
done
|
|
|
|
done
|
|
|
|
done
|
|
|
|
|
|
|
|
zoommin=$((20-$zoomlevelnum))
|
|
|
|
#write the resulting config into a json file
|
2016-01-17 17:40:12 +01:00
|
|
|
echo "{\"mapsize\":$DIMENSIONS, \"spawn\":{\"x\":$spawnx,\"y\":$spawny}, \"zoommin\":$zoommin}" > $scriptdir/www/conf.json
|