From b257bbcf7825c69fc3a6d00cf825e4c250f85031 Mon Sep 17 00:00:00 2001 From: InigoGutierrez Date: Mon, 18 May 2020 23:53:31 +0200 Subject: mapGen.sh now can read from stdin. --- mapGen/README.md | 32 +++++++++++++------- mapGen/examples/banner.jpg | Bin 0 -> 36988 bytes mapGen/mapGen.sh | 71 ++++++++++++++++++++++++--------------------- 3 files changed, 59 insertions(+), 44 deletions(-) create mode 100644 mapGen/examples/banner.jpg (limited to 'mapGen') diff --git a/mapGen/README.md b/mapGen/README.md index c64a940..3af03f0 100644 --- a/mapGen/README.md +++ b/mapGen/README.md @@ -1,6 +1,6 @@ # mapGen.sh -A shellscript to make grid maps from text files. +A shellscript to make grid maps from text input. ![Simple map example](examples/simpleBig.jpg) @@ -20,17 +20,17 @@ xxxxx ## Motivation -The want for Vim to be a map maker. +Using Vim as a map maker. ## Concept -Given a text file in which each character represents a cell, have a script draw that map. +Given a text input in which each character represents a cell, have a script draw that map. -`mapGen.sh` takes as input a text file which ideally has a block of characters of any size but of -rectangular shape with no missing ones. It also takes a folder in which tile images are supposed to -be. For each character in the file it randomly takes one image in that folder which name starts -with that character and composes it into the map image with the help of the `convert` command from -ImageMagick. +`mapGen.sh` takes as input a text file or standard input which ideally has a block of characters of +any size but of rectangular shape with no missing ones. It also takes a folder in which tile images +are supposed to be. For each character in the input it randomly takes one image in that folder which +name starts with that character and composes it into the map image with the help of the `convert` +command from ImageMagick. ## Examples @@ -70,13 +70,21 @@ XXXXXXXXXooXXXXXXXXX ``` Note that for the second image a different folder with some missing tiles where used (no tiles -starting by D or S). The script puts a red pixel there and the cyan background is seen, indicating -this absence. +starting by D or S). Absent tiles are skipped. This allows for different inputs to be used as +layers: one could have the map and other add annotations to cells by using tiles with transparency, +for example. Note that the target file will only be created if it did not exist and will be drawn on +top if it did exist. + +![Unix rules! example](examples/banner.jpg) + +``` +figlet -f banner 'Unix Rules!' | tr ' #' 'Xo' | mapGen.sh -f dg +``` ## Usage ``` -mapGen.sh [-s SIZE] [-f TILE_FOLDER] [-o OUTPUT_FILE] SOURCE_FILE +mapGen.sh [-s SIZE] [-f TILE_FOLDER] [-o OUTPUT_FILE] [SOURCE_FILE] ``` Where @@ -86,6 +94,8 @@ Where - `OUTPUT_FILE` is the path to the resulting image (default `out.jpg`), - and `SOURCE_FILE` is the text file with the layout of the map. +If `SOURCE_FILE` is not provided standard input will be used. + ## Possibilities The direct mapping of characters in text file to beginning of tile image name gives flexibility to diff --git a/mapGen/examples/banner.jpg b/mapGen/examples/banner.jpg new file mode 100644 index 0000000..927e1c8 Binary files /dev/null and b/mapGen/examples/banner.jpg differ diff --git a/mapGen/mapGen.sh b/mapGen/mapGen.sh index 0d77637..5568308 100755 --- a/mapGen/mapGen.sh +++ b/mapGen/mapGen.sh @@ -4,13 +4,46 @@ # # Generate a map with ImageMagick from an input text file # -# Usage: mapGen.sh [-s SIZE] [-f TILE_FOLDER] [-o OUTPUT_FILE] SOURCE_FILE +# Usage: mapGen.sh [-s SIZE] [-f TILE_FOLDER] [-o OUTPUT_FILE] [SOURCE_FILE] -usageMsg="Usage: mapGen.sh [-s SIZE] [-f TILE_FOLDER] [-o OUTPUT_FILE] SOURCE_FILE" +usageMsg="Usage: mapGen.sh [-s SIZE] [-f TILE_FOLDER] [-o OUTPUT_FILE] [SOURCE_FILE]" cellSize=8 tilesDir="./tiles" outputFile="out.jpg" +drawMap () { + mapText="" + while read line; do + mapText="${mapText}${line}\n" + done + mapText="$(echo "$mapText" | sed 's|\\n|\n|g')" + + height="$(echo "$mapText" | wc -l)" + #width="$(awk '{print length($0)}' "$inputFile" | sort -nr | sed 1q)" + width="$(echo "$mapText" | awk '{print length($0)}' | sort -nr | sed 1q)" + height=$((height*cellSize)) + width=$((width*cellSize)) + + [ ! -f "$outputFile" ] && convert -size "$width"x"$height" xc:cyan "$outputFile" + + row=0 + col=0 + echo "$mapText" | ( + while read line; do + echo "$line" | ( + while read -n1 char; do + tileFile="$(find $tilesDir -type f -maxdepth 1 | sed 's|.*/||' | grep "^$char" | shuf | sed 1q)" + if [ -n "$tileFile" ]; then + tileFile="${tilesDir}/${tileFile}" + convert "$outputFile" "$tileFile" -geometry +"$col"+"$row" -composite "$outputFile" + fi + col=$((col+cellSize)) + done) + row=$((row+cellSize)) + col=0 + done) +} + while echo "$1" | grep '^-' >/dev/null; do case "$1" in "-s") @@ -30,36 +63,8 @@ while echo "$1" | grep '^-' >/dev/null; do esac done -[ -z "$1" ] && echo "$usageMsg" >&2 && exit 1 - +[ ! -d "$tilesDir" ] && echo "Directory not found: $tilesDir" >&2 && exit 1 inputFile="$1" - +[ -z "$inputFile" ] && drawMap && exit [ ! -f "$inputFile" ] && echo "File not found: $inputFile" >&2 && exit 1 -[ ! -d "$tilesDir" ] && echo "Directory not found: $tilesDir" >&2 && exit 1 - -height="$(wc -l <"$inputFile")" -width="$(awk '{print length($0)}' "$inputFile" | sort -nr | sed 1q)" - -height=$((height*cellSize)) -width=$((width*cellSize)) - -echo $width $height - -convert -size "$width"x"$height" xc:cyan "$outputFile" - -row=0 -col=0 - -while read line; do - echo "$line" | ( - while read -n1 char; do - tileFile="$(find $tilesDir | sed 's|.*/||' | grep "^$char" | shuf | sed 1q)" - tileFile="${tilesDir}/${tileFile}" - echo "$tileFile" | wc -l | grep 1 >/dev/null || continue - convert "$outputFile" "$tileFile" -geometry +"$col"+"$row" -composite "$outputFile" || - convert "$outputFile" -fill red -draw "point $col,$row" "$outputFile" - col=$((col+cellSize)) - done) - row=$((row+cellSize)) - col=0 -done <"$inputFile" +drawMap <"$inputFile" -- cgit v1.2.1