get_doom_mappack_random_level.sh 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/bin/sh
  2. # fail if any commands fails
  3. set -e
  4. # debug log
  5. #set -x
  6. show_usage (){
  7. printf "Usage: $0 [options [parameters]]\n"
  8. printf "\n"
  9. printf "Mandatory options:\n"
  10. printf " -p|--pwad [game pwad file]\n"
  11. printf "\n"
  12. printf "Options:\n"
  13. printf " -d|--game-dir [/path/to/doom/base/directory] (Optional, default: '~/games/doom')\n"
  14. printf " -h|--help, Print help\n"
  15. exit
  16. }
  17. if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
  18. show_usage
  19. fi
  20. if [ -z "$1" ]; then
  21. show_usage
  22. fi
  23. while [ -n "$1" ]; do
  24. case "$1" in
  25. --game-dir|-d)
  26. shift
  27. param_game_dir=$1
  28. ;;
  29. --pwad|-p)
  30. shift
  31. #echo "INFO: param - pwad: $1"
  32. param_pwad=$1
  33. ;;
  34. *)
  35. show_usage
  36. ;;
  37. esac
  38. shift
  39. done
  40. ### Configuration
  41. if [ -z "$param_game_dir" ]; then
  42. param_game_dir="$HOME/games/doom"
  43. fi
  44. if [ -z "$param_pwad" ]; then
  45. echo "ERROR: pwad parameter is mandatory"
  46. exit 1
  47. fi
  48. config_iwads_dir="$param_game_dir/wads/iwads"
  49. config_pwad_basegame=$(awk -F/ '{print $7}' <<< ${param_pwad})
  50. #echo "INFO: iwad name: $config_pwad_basegame"
  51. config_pwad_name=$(awk -F/ '{print $10}' <<< ${param_pwad})
  52. config_pwad_name=$(basename -- "${config_pwad_name%.*}")
  53. #echo "INFO: pwad name: $config_pwad_name"
  54. # Get maps from pwad
  55. gzdoom -iwad "$config_iwads_dir/$config_pwad_basegame.wad" -file "$param_pwad" -norun -hashfiles > /dev/null || true
  56. if [ "$config_pwad_basegame" = "doom2" ]; then
  57. config_pwad_map=$(cat fileinfo.txt | grep "$config_pwad_name" | grep -e " MAP" -e " maps/" | awk '{print $4}' | sed -e "s/^MAP//" -e 's/,//g' | shuf -n 1)
  58. if [ -z "$config_pwad_map" ]; then
  59. config_pwad_map=$(cat fileinfo.txt | grep "$config_pwad_name" | grep " maps/" | awk '{print $4}' | sed -e "s/^maps\/map//" -e 's/.wad,//g' | shuf -n 1)
  60. fi
  61. #echo "INFO: pwad map number: $config_pwad_map"
  62. mapnumbercheck=$(echo "$config_pwad_map" | awk '$0 ~/[^0-9]/ { print "NOT_NUMBER" }')
  63. elif [ "$config_pwad_basegame" = "doom" ]; then
  64. config_pwad_map=$(cat fileinfo.txt | grep "$config_pwad_name" | grep -E " E[1-5]M" | awk '{print $4}' | shuf -n 1 | sed -r 's/[EM]+/ /g' | sed -e "s/^0//" -e 's/,//g')
  65. #echo "INFO: pwad map number: $config_pwad_map"
  66. elif [ "$config_pwad_basegame" = "heretic" ]; then
  67. config_pwad_map=$(cat fileinfo.txt | grep "$config_pwad_name" | grep -E " E[1-5]M" | awk '{print $4}' | shuf -n 1 | sed -r 's/[EM]+/ /g' | sed -e "s/^0//" -e 's/,//g')
  68. #echo "INFO: pwad map number: $config_pwad_map"
  69. elif [ "$config_pwad_basegame" = "hexen" ]; then
  70. config_pwad_map=$(cat fileinfo.txt | grep "$config_pwad_name" | grep -E " E[1-5]M" | awk '{print $4}' | shuf -n 1 | sed -r 's/[EM]+/ /g' | sed -e "s/^0//" -e 's/,//g')
  71. #echo "INFO: pwad map number: $config_pwad_map"
  72. fi
  73. # Remove temporary file
  74. rm fileinfo.txt
  75. echo "$config_pwad_map"