. standard-vars-sh ##. screen-vars-sh . screen-funcs-sh . string-funcs-sh B=`set_attr $bold` RV=`set_attr $reverse` init() { trap cleanup 0 2 3 4 5 6 7 8 9 trap reload 1 printf "$cu_invis" _a= _b= _c= _d= _e= _f= _g= _h= _i= _j= _k= _l= _m= score= selection= loop=0 round=1 defaults define_dice k_1=0 k_2=0 k_3=0 k_4=0 k_5=0 k_6=0 da_setup=`set_fgbgattr $da_fg $da_bg $da_attr` db_setup=`set_fgbgattr $db_fg $db_bg $db_attr` dc_setup=`set_fgbgattr $dc_fg $dc_bg $dc_attr` printf "$d_attr" configure=0 c= #echo "$score_so" | hd; exit selections=12345abcdefghijklm _random= } set_awk() { oldIFS=$IFS IFS=: set -- $PATH IFS=$oldIFS for dir do for awk in nawk mawk gawk do [ -x "$dir/$awk" ] && return done done awk=awk } rand_opts() { OPTIND=1 while getopts b:n:l:u:s:r var do case $var in n) num=$OPTARG ;; l) lower=$OPTARG ;; u) upper=$OPTARG ;; r) _random= ;; ## reset b) bank=$OPTARG ;; s) seed=$OPTARG ;; esac done } if [ "$RANDOM" = "$RANDOM" ] then random() { IFS=${default_IFS:=`printf " \t\n"`} num=1 lower=1 upper=32767 bank=200 seed= case $* in *?*) rand_opts "$@" ;; esac set -- $_random if [ $# -lt "$num" ] then set -- `${awk:-awk} -v bank=$bank -v seed="$seed" \ -v lower=$lower -v upper=$upper ' BEGIN{ if ( length(seed) > 0) srand(seed) else srand() while ( n++ < bank ) printf "%s ", int(rand()*(upper - lower + 1)) + lower }' ` fi _N=0 _RANDOM= while [ $_N -lt $num ] do _RANDOM="${_RANDOM:+$_RANDOM }$1" _N=$(( $_N + 1 )) case ${1+X} in X) shift ;; esac done _random=$* } else random() { num=1 lower=1 upper=32767 rand_opts "$@" _RANDOM= _N=0 while [ $_N -lt $num ] do _RANDOM="${_RANDOM:+$_RANDOM }$(( $RANDOM % ( $upper - $lower + 1 ) + $lower ))" _N=$(( $_N + 1 )) done } fi printat() { printf "$cu_row_col" ${1:-1} ${2:-1} case ${2+X} in X) shift 2 ;; esac case "$*" in *?*) printf "%s" "$*" ;; esac } version() { echo " $progname, version $version Copyright $copyright, $author $email This is free software, released under the terms of the GNU General Public License. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. " } get_colour() { case $colour in [Bb][Ll][Aa][Cc][Kk]) colour=0 ;; [Rr][Ee][Dd]) colour=1 ;; [Gg][Rr][Ee][Ee][Nn]) colour=2 ;; [Yy][Ee][Ll][Ll][Oo][Ww]) colour=3 ;; [Bb][Ll][Uu][Ee]) colour=4 ;; [Mm][Aa][Gg][Ee][Nn][Tt][Aa]) colour=5 ;; [Cc][Yy][Aa][Nn]) colour=6 ;; [Ww][Hh][Ii][Tt][Ee]) colour=7 ;; esac } set_da_attr() { set_fgbgattr "$da_fg" "$da_bg" $da_attr } set_db_attr() { set_fgbgattr "$db_fg" "$db_bg" $db_attr } set_dc_attr() { set_fgbgattr "$dc_fg" "$dc_bg" $dc_attr } set_fgbgattr() { printf "$NA" case $3 in [0-7]) set_attr "$3";; esac case $4 in [0-7]) set_attr "$4";; esac case $5 in [0-7]) set_attr "$5";; esac case $1 in [0-7]) set_fg "$1";; esac case $2 in [0-7]) set_bg "$2";; esac } blank() { set_fgbg $blank_fg $blank_bg bl_w=${blank_width:-$COLUMNS} bl_l=${blank_height:-$LINES} blank_fmt="$cu_save$blank_attr%${bl_w}.${bl_w}s${cu_restore}" while [ $bl_l -gt 0 ] do printf "$blank_fmt" "$blank_str" [ $bl_l -gt 1 ] && printf "$cu_d1" bl_l=$(( $bl_l - 1 )) [ "${#blank_shift}" -gt 0 ] && { shiftless=${blank_str#$blank_shift} blank_str=$shiftless${blank_str#$shiftless} } done } which_shell() { case $BASH_VERSION in *?*) _shell=bash${BASH_VERSION%%.*}; return ;; esac case $KSH_VERSION in *PD*KSH*) _shell=pdksh; return ;; *?*) _shell=ksh; return ;; esac type typeset >/dev/null 2>&1 || { _shell=sh; return; } x=4 q() { typeset x; x=5; }; q case $x in 5) _shell=ksh93;; esac _shell='???' } which_shell case $_shell in bash[2-9]|bash[0-9][0-9]|ksh93) . b2k93-funcs ;; *) sub() { str=$1 [ "$2" ] || return 1 srch=${2} rep=$3 case $str in *$srch*) sr1=${str%%$srch*} sr2=${str#*$srch} str=$sr1$rep$sr2 ;; *) false ;; esac printf "%s\n" "$str" } gsub() { str=$1 [ "$2" ] || return 1 srch=${2} rep=$3 sr2=$str sr1= while : do case $sr2 in *$srch*) sr1=$sr1${sr2%%$srch*}$rep sr2=${sr2#*$srch} ;; *) break ;; esac done printf "%s\n" "$sr1$sr2" } substr() { printf "%s\n" "${1}" | cut -c$2-$(( $2 + $3 - 1 )) } esac rsub() { str=$1 [ "$2" ] || return 1 srch=${2} rep=$3 case $str in *$srch*) sr1=${str%$srch*} sr2=${str##*$srch} str=$sr1$rep$sr2 ;; *) false ;; esac printf "%s\n" "$str" } insert_str() ## USAGE: insert_str string insert [position] { _string=$1 i_string=$2 [ -n "$3" ] && i_c=$3 || i_c=$(( ${#_string} / 2 )) printf "%s%s%s\n" "`substr $_string 1 $(( $i_c - 1 ))`"\ "$i_string"\ "`substr $_string $i_c`" } define_dice() { ## requires standard-vars: cu_save cu_restore cu_d1 NA ## optional ds, the character to use for the pips ## provides variables d_w, dice_defined, dice{1..15}. ds=${ds:-o} ds2=$ds$ds ds3=$ds$ds$ds ds3x="$ds $ds $ds" ds4=$ds$ds$ds$ds ds5=$ds$ds$ds$ds$ds d_w=5 dice_defined=1 d_fmt="${cu_save?} %-${d_w?}.${d_w?}s ${cu_NL?}" dice1=` printf "$d_fmt" "" "" " $ds" "" ""` dice2=` printf "$d_fmt" "" "$ds" "" " $ds" ""` dice3=` printf "$d_fmt" "" "$ds" " $ds" " $ds" ""` dice4=` printf "$d_fmt" "" "$ds $ds" "" "$ds $ds" ""` dice5=` printf "$d_fmt" "" "$ds $ds" " $ds" "$ds $ds" ""` dice6=` printf "$d_fmt" "" "$ds3x" "" "$ds3x" ""` dice7=` printf "$d_fmt" "" "$ds3x" " $ds" "$ds3x" ""` dice8=` printf "$d_fmt" "" "$ds3x" " $ds $ds" "$ds3x" ""` dice9=` printf "$d_fmt" "" "$ds3x" "$ds3x" "$ds3x" ""` dice10=`printf "$d_fmt" "" "$ds3x" "$ds2 $ds2" "$ds3x" ""` dice11=`printf "$d_fmt" "" "$ds3 $ds" "$ds3x " "$ds $ds3" ""` dice12=`printf "$d_fmt" "" "$ds3 $ds" "$ds2 $ds2" "$ds $ds3" ""` dice13=`printf "$d_fmt" "" "$ds5" "$ds3x" "$ds5" ""` dice14=`printf "$d_fmt" "" "$ds5" "$ds3 $ds" "$ds5" ""` dice15=`printf "$d_fmt" "" "$ds5" "$ds5" "$ds5" ""` export dice1 dice2 dice3 dice4 dice5 dice6 dice7 dice8 dice9 export dice10 dice11 dice12 dice13 dice14 dice15 dice_defined d_w } rand_str() { random rnd_num=$(( $_RANDOM % $# + 1 )) eval _RAND_STR=\$$rnd_num printf "%s" "_RAND_STR" } case ${BASH_VERSION%%.*} in [2-9]|[1-9][0-9]*) read_opts="-ep" key_opts="-sn1 -d ''" get_key() { IFS= read -r $key_opts ${1:-_KEY} } ;; *) get_key() { [ -t 0 ] && { _STTY=`stty -g` stty -echo -icanon time ${_GET_TIME:-0} ##min ${_GET_MIN:-1} } _KEY=`dd bs=1 count=1 2>/dev/null` [ -n "$1" ] && eval "$1=\"$_KEY\"" [ -t 0 ] && stty $_STTY [ -n "$_KEY" ] } read_opts= key_opts= ;; esac var_toggle() { eval _VAR_TOGGLE=\$$1 case ${_VAR_TOGGLE:-0} in ${3:-0}) _VAR_TOGGLE=${2:-1} ;; *) _VAR_TOGGLE=${3:-0} ;; esac eval $1=$_VAR_TOGGLE } die_xy() { d_num=$1 d_row=$(( $dy_inc * ($d_num - 1) + $dy_off )) d_col=$(( $dx_inc * ($d_num - 1) + $dx_off )) printat $d_row $d_col ##"$cu_save" } roll_dice() { case $c in ""|"$NL") roll_num=$(( 5 - ($k_1 + $k_2 + $k_3 + $k_4 + $k_5 + $k_6) )) random -n $roll_num -l1 -u6 set -- $_RANDOM case $verbose in [1-9]*) printat 1 1 "rolled $roll_num ($#)";; esac case $k_1 in 0) d_1=$1; shift ;; esac case $k_2 in 0) d_2=$1; shift ;; esac case $k_3 in 0) d_3=$1; shift ;; esac case $k_4 in 0) d_4=$1; shift ;; esac case $k_5 in 0) d_5=$1; shift ;; esac ;; esac } kd() { printat $(( $LINES -8 + $n )) 3 # printf "n: %d keep: %d fg:%d bg: %d loop: %d scores: %d %d %d %d %d %d %d %d %d %d %d %d %d %d " $n $keep $sd_fg $sd_bg $loop $_1 $_2 $_3 $_4 $_5 $_6 } show_die() { die_xy $1 eval _die=\$d_$n eval dice=\$dice$_die printf "%s" "$dice" } show_dice() { printf "$NA" n=1 while [ $n -le ${num_dice:-5} ] do eval "keep=\$k_$n" case $keep in 1) printf "$db_setup" ;; *) printf "$da_setup" ;; esac case $loop in 0) printf "$dc_setup" ;; esac die_xy $n eval _die=\$d_$n eval dice=\$dice$_die printf "%s" "$dice" printf "$dnum_attr %d " $n n=$(( $n + 1 )) done printf "$NA" } show_upper() { printf "$NA" [ $loop -eq 0 ] && printf "$set_attr $bold" printat $u_row $u_col "$cu_save" ## printf "$cu_save %10s %6s %s$crd1" "" Scored Rolled printf " %10s %6s %s$cu_NL" "" Scored Rolled score_fmt=" [%c]%7s: %3s %-7s$cu_NL" [ -n "$_a" ] && _s= || _s=${#ones} [ "$selection" = a ] && printf "$score_so" || printf "$score_attr" printf "$score_fmt" a Ones "${_a:-${mt}}" "${_s:+"(${#ones}=$_s)"}" [ -n "$_b" ] && _s= || _s=$(( 2 * ${#twos} )) [ "$selection" = b ] && printf "$score_so" || printf "$score_attr" printf "$score_fmt" b Twos "${_b:-${mt}}" "${_s:+"(${#twos}=$_s)"}" [ -n "$_c" ] && _s= || _s=$(( 3 * ${#threes} )) [ "$selection" = c ] && printf "$score_so" || printf "$score_attr" printf "$score_fmt" c Threes "${_c:-${mt}}" "${_s:+"(${#threes}=$_s)"}" [ -n "$_d" ] && _s= || _s=$(( 4 * ${#fours} )) [ "$selection" = d ] && printf "$score_so" || printf "$score_attr" printf "$score_fmt" d Fours "${_d:-${mt}}" "${_s:+"(${#fours}=$_s)"}" [ -n "$_e" ] && _s= || _s=$(( 5 * ${#fives} )) [ "$selection" = e ] && printf "$score_so" || printf "$score_attr" printf "$score_fmt" e Fives "${_e:-${mt}}" "${_s:+"(${#fives}=$_s)"}" [ -n "$_f" ] && _s= || _s=$(( 6 * ${#sixes} )) [ "$selection" = f ] && printf "$score_so" || printf "$score_attr" printf "$score_fmt" f Sixes "${_f:-${mt}}" "${_s:+"(${#sixes}=$_s)"}" if [ $utotal -ge 63 ] then printf "$cu_d1$cu_save $cu_NL" bonus=35 else printf "$cu_d1$cu_save [%d away from bonus] $cu_NL" $tobonus fi } xshow_upper() { printf "$NA" printat $u_row $u_col printf "$cu_save %10s %6s %s$cu_NL" "" Scored Rolled score_fmt=" [%c]%7s: %3s %-7s$cu_NL" for sc in a b c d e f do eval score=\$_$sc [ -n "$score" ] && _s= || _s=${#ones} [ "$selection" = $sc ] && printf "$score_so" || printf "$score_attr" printf "$score_fmt" a Ones "${_a:-${mt}}" "${_s:+"(${#ones}=$_s)"}" done if [ $utotal -ge 63 ] then printf "$cu_d1$cu_save $cu_NL" bonus=35 else printf "$cu_d1$cu_save [%d away from bonus] $cu_NL" $tobonus fi } show_lower() { printat $l_row $l_col "$NA" [ $loop -eq 0 ] && printf "$set_attr $bold" printf "$cu_save %18s %6s %s$cu_NL" "" Scored Rolled score_fmt=" [%c]%15s: %3s %-5s$cu_NL" [ -n "$_g" ] && _s= || _s=$k3 [ "$selection" = g ] && printf "$score_so" || printf "$score_attr" printf "$score_fmt" g "3 of a kind" "${_g:-${mt}}" ${_s:+"($_s)"} [ -n "$_h" ] && _s= || _s=$k4 [ "$selection" = h ] && printf "$score_so" || printf "$score_attr" printf "$score_fmt" h "4 of a kind" "${_h:-${mt}}" ${_s:+"($_s)"} [ -n "$_i" ] && _s= || _s=$full_house [ "$selection" = i ] && printf "$score_so" || printf "$score_attr" printf "$score_fmt" i "Full house" "${_i:-${mt}}" ${_s:+"($_s)"} [ -n "$_j" ] && _s= || _s=$sm_straight [ "$selection" = j ] && printf "$score_so" || printf "$score_attr" printf "$score_fmt" j "Small straight" "${_j:-${mt}}" ${_s:+"($_s)"} [ -n "$_k" ] && _s= || _s=$lg_straight [ "$selection" = k ] && printf "$score_so" || printf "$score_attr" printf "$score_fmt" k "Large straight" "${_k:-${mt}}" ${_s:+"($_s)"} [ -n "$_l" ] && _s= || _s=$yahtzee [ "$selection" = l ] && printf "$score_so" || printf "$score_attr" printf "$score_fmt" l "Yahtzee" "${_l:-${mt}}" ${_s:+"($_s)"} [ -n "$_m" ] && _s= || _s=$chance [ "$selection" = m ] && printf "$score_so" || printf "$score_attr" printf "$score_fmt" m "Chance" "${_m:-${mt}}" ${_s:+"($_s)"} } defaults() { table=$white dx_off=10 dy_off=3 dx_inc=10 dy_inc=0 ## rolled dice da_fg=$blue #$white da_bg= #$black da_attr=$reverse ## keepers db_fg=$white db_bg=$blue db_attr=$bold ## fixed (select box) dc_fg=$white dc_bg=$yellow dc_attr= da_setup=`set_da_attr` db_setup=`set_db_attr` dc_setup=`set_dc_attr` k_fg=$db_fg k_bg=$db_bg main_fg=$da_fg main_bg=$da_bg main_attr= u_col=10 ##$(( ( $COLUMNS - 60 ) / 2 )) u_row=10 u_score=$u_col l_col=$(( $u_col + 30 )) l_row=$u_row mt=[-] ## empty box bonus_row=2 bonus_col=2 score_so=`set_attr $bold $reverse` ##$BR score_attr=$NA score_line=$(( $u_row + 11 )) select_button=SELECT select_col=$(( $COLUMNS - ${#select_button} -5 )) select_row=1 select_attr=`printf "$B$RV$set_fgbg" $white $black` dnum_attr=$NA status_row=1 } show_scores() { show_upper show_lower printat $score_line $u_col printf "${cu_save}Upper total: %2s " "$utotal" printf " Bonus: %2s " "${bonus:-0}" printf " Lower total: %2s" "$ltotal" printf " $BOLD Grand total: %3s$NA\n" "$gtotal" } evaluate() { rolls=$* ones=`gsub "$rolls" '[!1]'` twos=`gsub "$rolls" '[!2]'` threes=`gsub "$rolls" '[!3]'` fours=`gsub "$rolls" '[!4]'` fives=`gsub "$rolls" '[!5]'` sixes=`gsub "$rolls" '[!6]'` list=${#ones}${#twos}${#threes}${#fours}${#fives}${#sixes} chance=$(( ${#ones} + 2 * ${#twos} + 3 * ${#threes} + 4 * ${#fours} + 5 * ${#fives} + 6 * ${#sixes} )) full_house=0 yahtzee=0 sm_straight=0 lg_straight=0 k3=0 k4=0 joker=0 case $list in *3*2* | *2*3*) full_house=25; k3=$chance ;; *3*) k3=$chance ;; *4*) k4=$chance; k3=$chance ;; *5*) yahtzee=50 k3=$chance k4=$chance [ -n "$y" ] && { set -- $rolls eval "[ -n \"\$_$1\" ]" && { full_house=25 sm_straight=30 lg_straight=40 } } if [ ${_l:-0} -ge 50 ] then _l=$(( $_l + 100 )) printat $bonus_row $bonus_col "Yahtzee bonus!" printf "\a" yahtzee=0 fi ;; *11111*) lg_straight=40; sm_straight=30 ;; *[1-2][1-2][1-2][1-2]*) sm_straight=30 ;; esac utotal=$(( ${_a:-0} + ${_b:-0} + ${_c:-0} + ${_d:-0} + ${_e:-0} + ${_f:-0} )) tobonus=$(( 63 - $utotal )) ltotal=$(( ${_h:-0} + ${_i:-0} + ${_j:-0} + ${_k:-0} + ${_l:-0} + ${_m:-0} + ${_g:-0} )) gtotal=$(( $utotal + $bonus + $ltotal )) } usage() { echo " $progname - $description USAGE: $progname [OPTIONS] OPTIONS: " [ $longusage -eq 1 ] && echo " -c FILE - use FILE for configuration instead of /home/chris/.config/mkscript.cfg -h - help: print shorter help message -H - help: print this message" || echo " -h - help: print this message -H - help: print more detailed message" echo " -v - verbose: -V - print version information " [ $longusage -eq 1 ] && version || echo " Copyright 2004, Chris F.A. Johnson " } reload() { . y2-funcs-sh } cleanup() { printat $LINES 1 "$cu_vis$NA" [ -t 0 ] && stty sane echo icanon exit } verify() { printat ${verify_line:-1} ${verify_col:-1} "$cle" printf "$B$RV Do you really want to quit [Y/n]? " get_key Q printat ${verify_line:-1} ${verify_col:-1} "$cle" case $Q in y|Y|"") return ;; *) return 1 ;; esac } config_key() { case $c in ""|"$NL") break ;; " ") reload ;; q) break 2;; C) var_toggle configure ;; a) d_attr=$(( (${d_attr:=0} + 1) % 8 )) [ $d_attr -ne 0 ] && printf "${CSI}0;${d_attr}m" ;; b|=|+) d_bg=$(( ($d_bg + 1) % 8 )); set_bg $d_bg ;; d) defaults ;; f|-) d_fg=$(( ($d_fg + 1) % 8 )); set_fg $d_fg ;; [1-5]) var_toggle k_$c ;; h) dx_inc=10; dy_inc=0 ;; v) dx_inc=0; dy_inc=6 ;; o) dx_inc=10; dy_inc=6 ;; x) dx_off=$(( $dx_off + 1 )) ;; X) dx_off=$(( $dx_off - 1 )) ;; y) dy_off=$(( $dy_off + 1 )) ;; Y) dy_off=$(( $dy_off - 1 )) ;; esac case $c in [hvoxXyY]) clear; show_scores ;; esac } unkeep() { k_1=0 k_2=0 k_3=0 k_4=0 k_5=0 } num2colour() { case $1 in "") colour=NONE ;; 0) colour=black ;; 1) colour=red ;; 2) colour=green ;; 3) colour=yellow ;; 4) colour=blue ;; 5) colour=magenta ;; 6) colour=cyan ;; 7) colour=white ;; esac } num2attr() { case $1 in 0|"") dx_attr=NONE ;; 1) dx_attr=bold ;; 2) dx_attr=half-bright ;; 3) dx_attr=3 ;; 4) dx_attr=underline ;; 5) dx_attr=blink ;; 6) dx_attr=6 ;; 7) dx_attr=reverse ;; *) dx_attr= ;; esac } value_box() { kk=${5%??} k1=${kk%?} k2=${kk#?} kk=${5#??} k3=${kk%?} k4=${kk#?} die_xy $(( $num_dice + 1 )) printf "${NA}${B}${cu_save}%s${cu_NL}${cu_d1}${cu_save}${cle}" "$1" printf "${NA}Background [%s]: %-10s${cu_NL}${cle}" "$k1" "$3" printf "${NA}Foreground [%s]: %-10s${cu_NL}${cle}" "$k2" "$2" printf "${NA} Attribute [%s]: %-10s${cu_NL}${cle}" "$k3" "$4" } set_up_dice() { roll_dice printf "$NA" case $1 in [qwerQWE]) d_attr=`set_attr $da_attr;set_fgbg $da_fg $da_bg` dtype="Roll of dice" num2colour $da_fg d_fg=$colour num2colour $da_bg d_bg=$colour num2attr $da_attr dy_off=3 keys=qwer da_setup=`set_fgbgattr "$da_fg" "$da_bg" $da_attr` printf "$da_setup" ;; [asdfASD]) d_attr=`set_attr $db_attr;set_fgbg $db_fg $db_bg` dtype="Keepers" num2colour $db_fg d_fg=$colour num2colour $db_bg d_bg=$colour num2attr $db_attr dy_off=13 keys=asdf db_setup=`set_fgbgattr "$db_fg" "$db_bg" $db_attr` printf "$db_setup" k_1=1 ;; [zxcvZXC]) d_attr=`set_attr $dc_attr;set_fgbg $dc_fg $dc_bg` dtype="Select box score" num2colour $dc_fg d_fg=$colour num2colour $dc_bg d_bg=$colour num2attr $dc_attr dy_off=23 keys=zxcv dc_setup=`set_fgbgattr "$dc_fg" "$dc_bg" $dc_attr` printf "$dc_setup" k_1=0 ;; esac }