quake-random-map.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #!/bin/bash
  2. # exit when any command fails
  3. set -e
  4. # debug log
  5. #set -x
  6. function show_usage (){
  7. printf "Usage: $0 [options [parameters]]\n"
  8. printf "\n"
  9. printf "Options:\n"
  10. printf " -g|--game-dir [/path/to/quake/base/directory] (Optional, default: '~/games/quake')\n"
  11. printf " -d|--mod-dir [id1|ad|jam9|quoth|hipnotic|...] (Optional, default: '*' (all subdirectories))\n"
  12. printf " -s|--skill [0, 1, 2, 3] (Optional, default: '1')\n"
  13. printf " -r|--retrolook [yes|no] (Optional, default: 'no')\n"
  14. printf " -u|--mangohud [yes|no] (Optional, default: 'no')\n"
  15. printf " -h|--help, Print help\n"
  16. exit
  17. }
  18. if [[ "$1" == "--help" ]] || [[ "$1" == "-h" ]];then
  19. show_usage
  20. fi
  21. while [ ! -z "$1" ]; do
  22. case "$1" in
  23. --game-dir|-g)
  24. shift
  25. echo "game directory: $1"
  26. QUAKE_SUB_DIR=$1
  27. ;;
  28. --mod-dir|-d)
  29. shift
  30. echo "mod directory: $1"
  31. QUAKE_SUB_DIR=$1
  32. ;;
  33. --skill|-s)
  34. shift
  35. echo "skill: $1"
  36. SKILL=$1
  37. ;;
  38. --retrolook|-r)
  39. shift
  40. echo "retro look: $1"
  41. RETRO_LOOK=$1
  42. ;;
  43. --mangohud|-u)
  44. shift
  45. echo "mangohud: $1"
  46. MANGOHUD_ENABLED=$1
  47. ;;
  48. *)
  49. show_usage
  50. ;;
  51. esac
  52. shift
  53. done
  54. ### Configuration
  55. if [[ -z $QUAKE_DIR ]]; then
  56. QUAKE_DIR=~/games/quake
  57. fi
  58. if [[ -z $QUAKE_SUB_DIR ]]; then
  59. QUAKE_SUB_DIR=*
  60. fi
  61. if [[ -z $SKILL ]]; then
  62. SKILL=1
  63. fi
  64. if [[ -z $RETRO_LOOK ]]; then
  65. RETRO_LOOK=no
  66. fi
  67. if [[ -z $MANGOHUD_ENABLED ]]; then
  68. MANGOHUD_ENABLED=no
  69. fi
  70. mapdir=
  71. SCRIPT_DIR="$(pwd "$(dirname "$0")")"
  72. ### check parameter values
  73. mangohud_enabled=(yes no)
  74. if [[ " "${mangohud_enabled[@]}" " != *" $MANGOHUD_ENABLED "* ]]; then
  75. echo "$MANGOHUD_ENABLED: not recognized. Valid options are:"
  76. echo "${mangohud_enabled[@]/%/,}"
  77. exit 1
  78. fi
  79. retro_look=(yes no)
  80. if [[ " "${retro_look[@]}" " != *" $RETRO_LOOK "* ]]; then
  81. echo "$RETRO_LOOK: not recognized. Valid options are:"
  82. echo "${retro_look[@]/%/,}"
  83. exit 1
  84. fi
  85. skill=(0 1 2 3)
  86. if [[ " "${skill[@]}" " != *" $SKILL "* ]]; then
  87. echo "$SKILL: not recognized. Valid options are:"
  88. echo "${skill[@]/%/,}"
  89. exit 1
  90. fi
  91. ### Script
  92. get_map_file() {
  93. if [ ! -d $QUAKE_DIR/$QUAKE_SUB_DIR ]; then
  94. echo "Mod directory does not exist"
  95. exit 1
  96. fi
  97. mapfile=$(find $QUAKE_DIR/$QUAKE_SUB_DIR/{maps/*.bsp,pak0.pak} -type f | shuf -n 1)
  98. if [[ $mapfile == *"pak"* ]]; then
  99. # Get map directory name
  100. mapdir="$(dirname "$mapfile")"
  101. mapdir="$(awk -F/ '{print $(NF)}' <<< "${mapdir}")"
  102. # Get map pak file map name
  103. pakfile=$mapfile
  104. mapfile=$(qpakman -l "$pakfile" | grep 'maps/' | grep '.bsp' | awk '{ print $5 }' | shuf -n 1)
  105. else
  106. # Get map directory name
  107. mapdir="$(dirname "$mapfile")"
  108. mapdir="$(awk -F/ '{print $(NF-1)}' <<< "${mapdir}")"
  109. fi
  110. }
  111. ###
  112. # Quake random map script
  113. ###
  114. get_map_file
  115. while [[ -z $mapfile || $mapfile == *"/b_"* || $mapfile == *"_obj_"* || $mapfile == *"_brk"* || $mapfile == *"/m_"* || $mapfile == *"bmodels"* ]]
  116. do
  117. echo "Incorrect map" $mapfile "getting another map..."
  118. unset pakfile
  119. unset mapfile
  120. get_map_file
  121. done
  122. # Save map info in external file
  123. if [ -n "$pakfile" ]; then
  124. play_combination="${pakfile},${mapdir},${mapfile}"
  125. else
  126. play_combination="${mapdir},${mapfile}"
  127. fi
  128. play_combination=$(sed 's/ /_/g' <<< "${play_combination}")
  129. # Check played times in external file
  130. if [ ! -z $(grep "${play_combination}" ${SCRIPT_DIR}/already_played_maps.txt) ]; then
  131. echo "Play combination found in file, updating file"
  132. current_times=$(cat ${SCRIPT_DIR}/already_played_maps.txt | grep ${play_combination} | awk -F, '{print $4}')
  133. played_times=$(echo "$(($current_times + 1))")
  134. # Update file
  135. sed -i "s|${play_combination},${current_times}|${play_combination},${played_times}|g" "${SCRIPT_DIR}"/already_played_maps.txt
  136. else
  137. echo "Play combination not found in file, adding to file"
  138. played_times="1"
  139. new_played="${play_combination},${played_times}"
  140. echo "${new_played}" >> ${SCRIPT_DIR}/already_played_maps.txt
  141. fi
  142. # Get game name
  143. if [ -n "$pakfile" ]; then
  144. gamename=$(awk -F/ '{print $6}' <<< "${pakfile}")
  145. else
  146. gamename=$(awk -F/ '{print $6}' <<< "${mapfile}")
  147. fi
  148. echo "$gamename"
  149. # get map name
  150. if [ -n "$pakfile" ]; then
  151. mapname=$(basename -- "${mapfile%.*}")
  152. else
  153. mapname=$(basename -- "${mapfile%.*}")
  154. fi
  155. echo "$mapname"
  156. if [ $MANGOHUD_ENABLED = "yes" ]; then
  157. export MANGOHUD_DLSYM=1
  158. #export MANGOHUD_CONFIG=cpu_temp,gpu_temp,core_load,cpu_core_clock,gpu_mem_clock,cpu_power,gpu_power,cpu_mhz,ram,vram,frametime,position=top-left,height=500,font_size=24
  159. export MANGOHUD_CONFIG=cpu_temp,gpu_temp,cpu_core_clock,gpu_mem_clock,cpu_power,gpu_power,cpu_mhz,ram,vram,frametime,position=top-left,height=500,font_size=18
  160. fi
  161. # Run
  162. if [ $RETRO_LOOK = "yes" ]; then
  163. PARAMS="+r_particles 2 +r_lerpmodels 0 +r_lerpmove 0 +r_viewmodel_quake 1 +r_scale 4 +scr_ofsx -2.8 +scr_sbaralpha 1 +v_gunkick 2 +gamma 1.2 +contrast 1.5 +fov 85 +fog 0.02"
  164. else
  165. PARAMS=""
  166. fi
  167. if [ -f "$HOME/src/quakespasm-quakespasm/Quake/quakespasm" ]; then
  168. BINARY_PATH=~/src/quakespasm-quakespasm/Quake/quakespasm
  169. else
  170. BINARY_PATH=quakespasm
  171. fi
  172. commandline="$BINARY_PATH -current -basedir $QUAKE_DIR -heapsize 524288 -zone 4096 -game $gamename +map $mapname +skill $SKILL $PARAMS -fitz"
  173. if [ $MANGOHUD_ENABLED = "yes" ]; then
  174. mangohud "$commandline" || true
  175. else
  176. $commandline || true
  177. fi
  178. # Print map name and pak file
  179. if [ -n "$pakfile" ]; then
  180. echo "PAK file : $pakfile"
  181. fi
  182. echo "MAP file : $mapfile"
  183. echo "Skill: : $SKILL"
  184. echo "Full command line : $commandline"
  185. echo ""
  186. echo "map: ${play_combination}"
  187. echo "map played ${played_times} times"