quake-random-map.ksh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #!/bin/ksh
  2. # exit when any command fails
  3. set -e
  4. # debug log
  5. #set -x
  6. function show_usage {
  7. printf "Usage: %s [options [parameters]]\n" "$0"
  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 [ -n "$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. SCRIPT_DIR="$(pwd $(dirname "$0"))"
  59. if [[ -z $QUAKE_SUB_DIR ]]; then
  60. QUAKE_SUB_DIR=*
  61. fi
  62. if [[ -z $SKILL ]]; then
  63. SKILL=1
  64. fi
  65. if [[ -z $RETRO_LOOK ]]; then
  66. RETRO_LOOK=no
  67. fi
  68. if [[ -z $MANGOHUD_ENABLED ]]; then
  69. MANGOHUD_ENABLED=no
  70. fi
  71. mapdir=
  72. scriptdir="$(pwd $(dirname "$0"))"
  73. ### check parameter values
  74. mangohud_enabled=(yes no)
  75. if [[ " "${mangohud_enabled[@]}" " != *" $MANGOHUD_ENABLED "* ]]; then
  76. echo "$MANGOHUD_ENABLED: not recognized. Valid options are:"
  77. echo "${mangohud_enabled[@]/%/,}"
  78. exit 1
  79. fi
  80. retro_look=(yes no)
  81. if [[ " "${retro_look[@]}" " != *" $RETRO_LOOK "* ]]; then
  82. echo "$RETRO_LOOK: not recognized. Valid options are:"
  83. echo "${retro_look[@]/%/,}"
  84. exit 1
  85. fi
  86. skill=(0 1 2 3)
  87. if [[ " "${skill[@]}" " != *" $SKILL "* ]]; then
  88. echo "$SKILL: not recognized. Valid options are:"
  89. echo "${skill[@]/%/,}"
  90. exit 1
  91. fi
  92. ### Script
  93. get_map_file() {
  94. if [ ! -d $QUAKE_DIR/"$QUAKE_SUB_DIR" ]; then
  95. echo "Mod directory does not exist"
  96. exit 1
  97. fi
  98. mapfile=$(find $QUAKE_DIR/"$QUAKE_SUB_DIR"/{maps/*.bsp,pak0.pak} -type f | shuf -n 1)
  99. if [[ $mapfile == *"pak"* ]]; then
  100. # Get map directory name
  101. mapdir="$(dirname "$mapfile")"
  102. mapdir="$(awk -F/ '{print $(NF)}' <<< "${mapdir}")"
  103. # Get map pak file map name
  104. pakfile=$mapfile
  105. mapfile=$(qpakman -l "$pakfile" | grep 'maps/' | grep '.bsp' | awk '{ print $5 }' | shuf -n 1)
  106. else
  107. # Get map directory name
  108. mapdir="$(dirname "$mapfile")"
  109. mapdir="$(awk -F/ '{print $(NF-1)}' <<< "${mapdir}")"
  110. fi
  111. }
  112. ###
  113. # Quake random map script
  114. ###
  115. get_map_file
  116. while [[ -z $mapfile || $mapfile == *"/b_"* || $mapfile == *"_obj_"* || $mapfile == *"_brk"* || $mapfile == *"/m_"* || $mapfile == *"bmodels"* ]]
  117. do
  118. echo "Incorrect map" $mapfile "getting another map..."
  119. unset pakfile
  120. unset mapfile
  121. get_map_file
  122. done
  123. # Save map info in external file
  124. if [ -n "$pakfile" ]; then
  125. play_combination="${pakfile},${mapdir},${mapfile}"
  126. else
  127. play_combination="${mapdir},${mapfile}"
  128. fi
  129. play_combination=$(sed 's/ /_/g' <<< "${play_combination}")
  130. # Check played times in external file
  131. if [ ! -z $(grep "${play_combination}" ${SCRIPT_DIR}/already_played_maps.txt) ]; then
  132. echo "Play combination found in file, updating file"
  133. current_times=$(cat ${SCRIPT_DIR}/already_played_maps.txt | grep ${play_combination} | awk -F, '{print $4}')
  134. played_times=$(echo "$(($current_times + 1))")
  135. # Update file
  136. sed -i "s|${play_combination},${current_times}|${play_combination},${played_times}|g" ${SCRIPT_DIR}/already_played_maps.txt
  137. else
  138. echo "Play combination not found in file, adding to file"
  139. played_times="1"
  140. new_played="${play_combination},${played_times}"
  141. echo "${new_played}" >> ${SCRIPT_DIR}/already_played_maps.txt
  142. fi
  143. # Get game name
  144. if [ -n "$pakfile" ]; then
  145. gamename=$(awk -F/ '{print $6}' <<< "${pakfile}")
  146. else
  147. gamename=$(awk -F/ '{print $6}' <<< "${mapfile}")
  148. fi
  149. echo "$gamename"
  150. # get map name
  151. if [ -n "$pakfile" ]; then
  152. mapname=$(basename -- "${mapfile%.*}")
  153. else
  154. mapname=$(basename -- "${mapfile%.*}")
  155. fi
  156. echo "$mapname"
  157. if [ $MANGOHUD_ENABLED = "yes" ]; then
  158. export MANGOHUD_DLSYM=1
  159. #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
  160. 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
  161. fi
  162. # Run
  163. if [ $RETRO_LOOK = "yes" ]; then
  164. commandline="quakespasm -current -basedir $QUAKE_DIR -heapsize 524288 -zone 4096 -game $gamename +map $mapname +skill $SKILL +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"
  165. else
  166. commandline="quakespasm -current -basedir $QUAKE_DIR -heapsize 524288 -zone 4096 -game $gamename +map $mapname +skill $SKILL -fitz"
  167. fi
  168. if [ $MANGOHUD_ENABLED = "yes" ]; then
  169. mangohud $commandline || true
  170. else
  171. $commandline || true
  172. fi
  173. # Print map name and pak file
  174. if [ -n $pakfile ]; then
  175. echo "PAK file : $pakfile"
  176. fi
  177. echo "MAP file : $mapfile"
  178. echo "Skill: : $skill"
  179. echo "Full command line : $commandline"
  180. echo ""
  181. echo "map: ${play_combination}"
  182. echo "map played ${played_times} times"