Advertisement
Ruxias

Dwarf Fortress World Generator

Aug 20th, 2012
4,579
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.62 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # Title: Dwarf Fortress World Generator
  4. # Author: Ruxias
  5. # Version: 1.0
  6. # Date: 2012-08-20 (YYYY-MM-DD)
  7. #
  8. # Description:
  9. #     This script enhances command-line world generation in Dwarf Fortress.
  10. #   First, it generates a world with ID 99 and the selected params and seed.
  11. #   Then, it renames the new world's folder to the name of the world and makes
  12. #   a folder ("info") to hold all the information files the game generates.
  13. #   After moving and renaming the information files, it may convert the map
  14. #   images to PNG and create a composite of both of the maps, provided you have
  15. #   ImageMagick installed and the corresponding options enabled in this script.
  16. #   If you include a number when executing this script it will make that many
  17. #   worlds. Otherwise, it will make the default amount.
  18. #
  19. #   IMPORTANT: Always keep this script in the Dwarf Fortress directory!
  20. #              The default name is "df_linux" but may be something different.
  21. #
  22. # Requirements:
  23. #   - BASH
  24. #   - ImageMagick (For image conversion and compositing.)
  25. #   - tar and gzip (For new world backups.)
  26. #
  27. # Changelog:
  28. #   + = Added, x = Removed, * = Changed, # = Fixed, ! = Note
  29. #   Version 1.0 [2012-08-20]
  30. #       ! Initial Release
  31.  
  32. # BEGIN SCRIPT SETTINGS
  33. cycles=1 # Default number of worlds to generate if no number was passed. [1]
  34. params=AUTO # Name of the parameter set in "data/init/world_gen.txt" to use. [AUTO]
  35. seed=RANDOM # Seed to use for world generation. [RANDOM]
  36. convert=YES # Convert maps from BMP to PNG? (Smaller filesize; requires ImageMagick.) [YES]
  37. composite=YES # Create a combined image from the height and biome maps? (Requires ImageMagick.) [YES]
  38. backup=YES # Make a backup of the newly created world? [YES]
  39. pause=0 # Number of seconds to pause after completion. If less than zero, wait for keypress. [0]
  40. # END SCRIPT SETTINGS
  41.  
  42. # BEGIN SCRIPT OPERATION
  43. statuscode=0 # Set the status code to 0 (Success) for now.
  44. errorcode=0 # Set the detailed error code to 0 (None) for now.
  45. count=0 # Set the count to 0.
  46.  
  47. if [[ -n "$1" && $1 =~ ^[0-9]+$ ]]; then # If a number was passed to the script, then ...
  48.     cycles=$1 # Set cycles to that number.
  49. fi
  50.  
  51. rootdir=$(dirname "$(readlink -fn "$0")") # Change the current directory to where the script is located for compatibility with launchers.
  52. cd "$rootdir"
  53.  
  54. function quietly() { "$@" > /dev/null; } # Simple function to silence the output of commands.
  55.  
  56. while [[ $count -lt $cycles && $statuscode -ne 1 ]]; do # While count is less than cycles and status code isn't 1 (Failure) ...
  57.     echo "[?] Creating World ($(( $count+1 ))/$cycles)"
  58.     if [ -d "data/save/region99" ]; then # If a world with ID 99 exists in "data/save" then ...
  59.         echo "[!] Folder 'data/save/region99' already exists!" # Echo an error message and set the status code to 1 (Failure) and the error code to 1. (World ID 99 Exists)
  60.         statuscode=1
  61.         errorcode=1
  62.     else # Otherwise ...
  63.         echo -n "    Generating world... "
  64.         quietly ./df -gen 99 $seed $params # Quietly execute Dwarf Fortress in world generation mode.
  65.         if [ -d "data/save/region99" ]; then # If "region99" was created during world generation, then ...
  66.             echo "Done!"
  67.             echo -n "    Reading world name... "
  68.             name=$(head -n1 region99-world_history.txt) # Read the first line of "region99-world_history.txt" to get the world name.
  69.             echo " Done! ($name)"
  70.             echo -n "    Organizing files... "
  71.             if [ -d "data/save/$name" ]; then # If a world already exists that has the same name ...
  72.                 echo "Fail!" # Echo an error message and delete the generated files.
  73.                 echo "[?] World with that name already exists! Regenerating world..."
  74.                 rm world_map-region99*.bmp
  75.                 rm world_graphic-region99*.bmp
  76.                 rm region99-world_history.txt
  77.                 rm region99-world_sites_and_pops.txt
  78.                 rm region99-world_gen_param.txt
  79.                 rm -r data/save/region99 # ("-r" option needed to delete world folder.)
  80.             else # Otherwise ...
  81.                 cd data/save/ # Move to the save directory.
  82.                 mv "region99" "$name" # Make the world's folder name reflect the actual world name.
  83.                 cd "$name" # Move to the world's directory.
  84.                 mkdir info # Make the "info" folder.
  85.                 mv ../../../world_map-region99*.bmp "info/Biome Map.bmp" # Move the info files to the "info" folder and clean up their file names.
  86.                 mv ../../../world_graphic-region99*.bmp "info/Height Map.bmp"
  87.                 mv ../../../region99-world_history.txt "info/History.txt"
  88.                 mv ../../../region99-world_sites_and_pops.txt "info/Sites.txt"
  89.                 mv ../../../region99-world_gen_param.txt "info/Parameters.txt"
  90.                 echo "Done!"
  91.                 if [ $convert == YES ]; then # If "convert" is "YES", then ...
  92.                     echo -n "    Converting images to PNG... "
  93.                     cd info # Move to the info directory.
  94.                     convert "Biome Map.bmp" "Biome Map.png" # Convert each map to PNG.
  95.                     convert "Height Map.bmp" "Height Map.png"
  96.                     rm "Biome Map.bmp" # Delete the old maps.
  97.                     rm "Height Map.bmp"
  98.                     echo "Done!"
  99.                     if [ $composite == YES ]; then # If "composite" is "YES", then ...
  100.                         echo -n "    Creating composite map... "
  101.                         composite -dissolve 30,100 "Height Map.png" "Biome Map.png" "Composite Map.png" # Create the composite map.
  102.                         echo "Done!"
  103.                     fi
  104.                 fi
  105.                 if [ $backup == YES ]; then # If "backup" is "YES", then ...
  106.                     echo -n "    Performing backup... "
  107.                     cd ../.. # Go to the save directory and create a backup of the world.
  108.                     tar --create --gzip --file="$name (Fresh World).tar.gz" "$name"
  109.                     echo "Done!"
  110.                 fi
  111.                 cd "$rootdir" # Change directory to where the script is located.
  112.                 count=$count+1 # Increase count by one.
  113.             fi
  114.         else # Otherwise ...
  115.             echo "Fail!" # Echo an error message and set the status code to 1 (Failure) and the error code to 2. (World Folder Not Found)
  116.             echo "[!] Folder 'data/save/region99' not found!"
  117.             statuscode=1
  118.             errorcode=2
  119.         fi
  120.     fi
  121. done
  122.  
  123. # Output the appropriate message.
  124. if [ $statuscode == 0 ]; then # If status code is 0 (Success), then ...
  125.     echo "[?] Operation completed successfully." # Report success.
  126. else # Otherwise ...
  127.     echo "[?] An error occurred during operation." # Report failure.
  128.     case $errorcode in # Output a detailed error message according to the error code.
  129.         1 )
  130.             echo "[?] A saved world named 'region99' already exists."
  131.             echo "    Rename, move, or delete this folder."
  132.             ;;
  133.         2 )
  134.             echo "[?] Dwarf Fortress didn't generate a world folder. Perhaps you aborted?"
  135.             ;;
  136.     esac
  137. fi
  138.  
  139. if (( $pause >= 0 )); then # If "pause" is greater than or equal to 0, then ...
  140.     sleep $pause # Sleep for "pause" seconds.
  141. else # Otherwise ...
  142.     read -p "" -n1 -s # Wait for a keypress.
  143. fi
  144.  
  145. exit $statuscode # Exit, reporting status code.
  146. # END SCRIPT OPERATION
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement