This shows you the differences between two versions of the page.
client_side_scripting:scripts:bash:cf_melt_icecube [2018/02/16 15:54] karl created |
client_side_scripting:scripts:bash:cf_melt_icecube [2018/04/16 06:21] (current) karl Code reworkings, no functional changes |
||
---|---|---|---|
Line 2: | Line 2: | ||
- | What a nasty situation : You used some icestorm spell to eliminate some monsters, | + | What for a nasty situation : You used some icestorm spell to eliminate some monsters, |
but now almost all items on the ground are boxed inside an one-time ice cube container. | but now almost all items on the ground are boxed inside an one-time ice cube container. | ||
Line 21: | Line 21: | ||
* _say_count() to finally say how many failures and successes were made. | * _say_count() to finally say how many failures and successes were made. | ||
+ | ==== Code == | ||
<code bash> | <code bash> | ||
#!/bin/bash | #!/bin/bash | ||
- | MARK_ITEM='icecube' | + | VERSION=0.0 |
- | ITEM='flint and steel' | + | VERSION=1.0 # posted on forum.metalforge.net |
+ | VERSION=2.0 # 2018-01-10 code overhaul now using functions, posted on wiki.cross-fire.org | ||
+ | VERSION=2.1 # 2018-02-17 more code overhauling | ||
+ | Version=2.2 # 2018-04-16 code overhaul | ||
- | DEBUG=1 | + | MARK_ITEM='icecube' # item to mark in function _mark_item() |
- | MSGLEVEL=7 | + | ITEM='flint and steel' # item to apply in function _apply_item() |
+ | |||
+ | DEBUG='' #1 # empty string or anything | ||
+ | MSGLEVEL=6 #7 # 0 - 7 | ||
+ | LOGGING='' #1 # empty string or anything | ||
MY_SELF=`realpath "$0"` | MY_SELF=`realpath "$0"` | ||
MY_BASE=${MY_SELF##*/} | MY_BASE=${MY_SELF##*/} | ||
+ | MY_DIR=${MY_SELF%/*} | ||
- | test -f "${MY_SELF%/*}"/cf_funcs_common.sh && . "${MY_SELF%/*}"/cf_funcs_common.sh | + | test -f "$MY_DIR"/cf_funcs_common.sh && . "$MY_DIR"/cf_funcs_common.sh |
_set_global_variables "$@" | _set_global_variables "$@" | ||
+ | |||
+ | cd "$MY_DIR" | ||
# *** Override any VARIABLES in cf_functions.sh *** # | # *** Override any VARIABLES in cf_functions.sh *** # | ||
- | test -f "${MY_SELF%/*}"/"${MY_BASE}".conf && . "${MY_SELF%/*}"/"${MY_BASE}".conf | + | test -f "$MY_DIR"/"${MY_BASE}".conf && . "$MY_DIR"/"${MY_BASE}".conf |
- | # *** Here begins program *** # | + | unset DIRB DIRF # for _exit() |
- | _say_start_msg "$@" | + | |
- | # *** Check for parameters *** # | + | # functions: |
- | while [ "$1" ] | + | _say_help(){ |
- | do | + | _draw 2 "$MY_BASE" |
- | PARAM_1="$1" | + | _draw 2 "Script to melt icecubes in inventory." |
- | + | _draw 4 "To be used in the crossfire roleplaying game client." | |
- | # *** implementing 'help' option *** # | + | _draw 2 "Syntax:" |
- | case "$PARAM_1" in -h|*"help"*) | + | _draw 2 "script $MY_SELF <<number>>" |
- | + | _draw 2 "For example: 'script $MY_SELF 5'" | |
- | _draw 5 "$MY_BASE" | + | _draw 2 "will issue mark icecube and apply flint and steel" |
- | _draw 5 "Script to melt icecubes in inventory." | + | _draw 2 " until 5 icecubes have been melt." |
- | _draw 2 "To be used in the crossfire roleplaying game client." | + | |
- | _draw 5 "Syntax:" | + | |
- | _draw 5 "script $0 <<number>>" | + | |
- | _draw 5 "For example: 'script $0 5'" | + | |
- | _draw 5 "will issue 5 times mark icecube and apply flint and steel." | + | |
exit 0 | exit 0 | ||
- | ;; | + | } |
- | -d) DEBUG=$((DEBUG+1));; | + | |
- | -V) _say_version;; | + | |
- | *) | + | |
- | # *** testing parameters for validity *** # | + | |
- | PARAM_1test="${PARAM_1//[[:digit:]]/}" | + | |
- | test "$PARAM_1test" && { | + | |
- | _draw 3 "Only :digit: numbers as first option allowed." | + | |
- | exit 1 #exit if other input than letters | + | |
- | } | + | |
- | NUMBER=$PARAM_1 | ||
- | ;; | ||
- | esac | ||
- | |||
- | shift | ||
- | sleep 0.1 | ||
- | done | ||
- | |||
- | # functions: | ||
_mark_item(){ | _mark_item(){ | ||
+ | # returns not 0, if message says | ||
+ | # no match | ||
_debug "_mark_item:$*" | _debug "_mark_item:$*" | ||
+ | _log "_mark_item:$*" | ||
+ | |||
local lITEM=${*:-"$MARK_ITEM"} | local lITEM=${*:-"$MARK_ITEM"} | ||
+ | |||
lITEM=${lITEM:-"$ITEM"} | lITEM=${lITEM:-"$ITEM"} | ||
lITEM=${lITEM:-icecube} | lITEM=${lITEM:-icecube} | ||
+ | |||
test "$lITEM" || return 254 | test "$lITEM" || return 254 | ||
Line 90: | Line 83: | ||
_is 1 1 mark "$lITEM" | _is 1 1 mark "$lITEM" | ||
- | while [ 1 ]; do | + | while :; do |
- | unset REPLY | + | unset REPLY |
read -t $TMOUT | read -t $TMOUT | ||
- | _log "_mark_item:$REPLY" | + | _log "_mark_item:$REPLY" |
- | _debug "$REPLY" | + | _debug "$REPLY" |
case $REPLY in | case $REPLY in | ||
- | *Could*not*find*an*object*that*matches*) lRV=1;break 1;; | + | *Could*not*find*an*object*that*matches*) lRV=1;break 1;; |
- | *scripttell*break*) break ${REPLY##*?break};; | + | *scripttell*break*) break ${REPLY##*?break};; |
- | *scripttell*exit*) _exit 1 $REPLY;; | + | *scripttell*exit*) _exit 1 $REPLY;; |
- | *'YOU HAVE DIED.'*) _just_exit;; | + | *'YOU HAVE DIED.'*) _just_exit;; |
- | '') break;; | + | '') break;; |
esac | esac | ||
- | #unset REPLY | + | sleep 0.1 |
- | sleep 0.1s | + | |
done | done | ||
Line 110: | Line 102: | ||
_apply_item(){ | _apply_item(){ | ||
+ | # returns 0 on success, | ||
+ | # -gt 0 otherwise | ||
+ | |||
_debug "_apply_item:$*" | _debug "_apply_item:$*" | ||
+ | _log "_apply_item:$*" | ||
+ | |||
local lITEM=${*:-"$ITEM"} | local lITEM=${*:-"$ITEM"} | ||
+ | |||
lITEM=${lITEM:-"flint and steel"} | lITEM=${lITEM:-"flint and steel"} | ||
+ | |||
test "$lITEM" || return 254 | test "$lITEM" || return 254 | ||
local lRV=0 | local lRV=0 | ||
+ | |||
_is 1 1 apply "$lITEM" | _is 1 1 apply "$lITEM" | ||
- | while [ 1 ]; do | + | while :; do |
- | unset REPLY | + | unset REPLY |
read -t $TMOUT | read -t $TMOUT | ||
- | _log "_apply_item:$REPLY" | + | _log "_apply_item:$REPLY" |
- | _debug "$REPLY" | + | _debug "$REPLY" |
case $REPLY in | case $REPLY in | ||
- | *used*up*flint*and*steel*) lRV=5; break 1;; | + | *You*light*the*icecube*with*the*flint*and*steel.*) lRV=0; SUCC=$((SUCC+1)); break 1;; |
- | *Could*not*find*any*match*to*the*flint*and*steel*) lRV=6; break 1;; | + | *fail*used*up*flint*and*steel*) lRV=5; break 1;; |
- | *You*need*to*mark*a*lightable*object.*) NO_FAIL=1; lRV=7; break 1;; | + | *fail*) lRV=1; FAIL=$((FAIL+1)); break 1;; |
- | '') lRV=1; break 1;; | + | *Could*not*find*any*match*to*the*flint*and*steel*) lRV=6; break 1;; |
- | *fail*) lRV=1; FAIL=$((FAIL+1)); break 1;; | + | *You*need*to*mark*a*lightable*object.*) lRV=7; break 1;; |
- | *You*light*the*icecube*with*the*flint*and*steel.*) lRV=0; SUCC=$((SUCC+1)); break 1;; | + | |
- | *scripttell*break*) break ${REPLY##*?break};; | + | *scripttell*break*) break ${REPLY##*?break};; |
- | *scripttell*exit*) _exit 1 $REPLY;; | + | *scripttell*exit*) _exit 1 $REPLY;; |
- | *'YOU HAVE DIED.'*) _just_exit;; | + | *'YOU HAVE DIED.'*) _just_exit;; |
- | *) :;; | + | |
+ | '') lRV=1; break 1;; | ||
+ | *) :;; | ||
esac | esac | ||
- | #unset REPLY | + | sleep 0.1 |
- | sleep 0.1s | + | |
done | done | ||
Line 143: | Line 144: | ||
_say_count(){ | _say_count(){ | ||
- | case $SUCC in [0-9]*) _draw 7 "You had smolten $SUCC icecube(s).";; esac | + | case $SUCC in [0-9]*) _draw 7 "You had melt $SUCC icecube(s).";; esac |
- | case $FAIL in [0-9]*) _draw 4 "You tried $FAIL time(s) and failed.";; esac | + | case $FAIL in [0-9]*) _draw 4 "You tried $FAIL time(s) with failure.";; esac |
} | } | ||
+ | |||
+ | # *** Here begins program *** # | ||
+ | |||
+ | # *** Check for parameters *** # | ||
+ | |||
+ | while [ "$1" ] | ||
+ | do | ||
+ | PARAM_1="$1" | ||
+ | |||
+ | # *** implementing 'help' option *** # | ||
+ | case "$PARAM_1" in | ||
+ | -h|*"help"*) _say_help;; | ||
+ | -d) DEBUG=$((DEBUG+1));; | ||
+ | -L) LOGGING=$((LOGGING+1));; | ||
+ | -V) _say_version;; | ||
+ | *) # *** testing parameters for validity *** # | ||
+ | PARAM_1test="${PARAM_1//[[:digit:]]/}" | ||
+ | test "$PARAM_1test" && _exit 1 "Only :digit: numbers as first option allowed." | ||
+ | #exit if other input than letters | ||
+ | NUMBER=$PARAM_1 | ||
+ | ;; | ||
+ | esac | ||
+ | |||
+ | shift | ||
+ | sleep 0.1 | ||
+ | done | ||
+ | |||
+ | # Prerequsites: DRAWINFO variable, | ||
+ | # prevent multiple scripts running, | ||
+ | # set sleep values according to player speed | ||
+ | _say_start_msg "$@" | ||
# *** Getting Player's Speed *** # | # *** Getting Player's Speed *** # | ||
Line 152: | Line 184: | ||
test "$PL_SPEED1" && __set_sync_sleep ${PL_SPEED1} || _set_sync_sleep "$PL_SPEED" | test "$PL_SPEED1" && __set_sync_sleep ${PL_SPEED1} || _set_sync_sleep "$PL_SPEED" | ||
- | # *** Actual script to pray multiple times *** # | + | test "$NUMBER" && { test $NUMBER -ge 1 || NUMBER=1; } #paranoid precaution |
- | # MAIN | + | _debug "NUMBER='$NUMBER'" |
SUCC=0 # count the successful attempts | SUCC=0 # count the successful attempts | ||
FAIL=0 # count the failure attempts | FAIL=0 # count the failure attempts | ||
- | test "$NUMBER" && { test $NUMBER -ge 1 || NUMBER=1; } #paranoid precaution | + | # *** Actual script to pray multiple times *** # |
- | _debug "NUMBER='$NUMBER'" | + | # MAIN |
- | _watch | + | _watch $DRAWINFO |
unset one | unset one | ||
Line 171: | Line 203: | ||
_sleep | _sleep | ||
- | NO_FAIL= | + | while :; |
- | until [ "$NO_FAIL" ] | + | |
do | do | ||
Line 182: | Line 213: | ||
3) :;; #unused | 3) :;; #unused | ||
4) :;; #unused | 4) :;; #unused | ||
- | 5) break 2;; #used up | + | 5) #used up |
+ | _warn "Your flint and steel is used up."; | ||
+ | break 2;; | ||
6) break 2;; #no item | 6) break 2;; #no item | ||
7) break 1;; #not marked | 7) break 1;; #not marked | ||
Line 189: | Line 222: | ||
esac | esac | ||
- | _say_count | + | #_say_count |
_sleep | _sleep | ||
- | done #NO_FAIL | + | done |
- | #test "$NUMBER" && { test "$NUMBER" = "$cnt" && break 1; } | ||
case $NUMBER in $one) break 1;; esac | case $NUMBER in $one) break 1;; esac | ||
+ | _say_count | ||
done #main while loop | done #main while loop | ||