Bash functions for Nourishment

This function library contains bash shell functions to feed
a character.
Though it had been tested with the busybox ash shell, some functions might only work correctly in bash ( or the other way around ).

The functions use code from

These client side scripting files are suggested to be installed inside the user's “HOME/.crossfire/s/” folder,
with a shortcut link cf → to the .crossfire directory,
to do have not much typing to do in the client's commandline.

Functions are usually named _function(){ ;} , functions named _ _function and _ _ _function are alternatives to the _function.

290 lines of code without much explanations…

#!/bin/ash
 
[ "$HAVE_FUNCS_FOOD" ] && return 0
 
# depends :
[ "$HAVE_FUNCS_COMMON"   ] || . cf_funcs_common.sh
[ "$HAVE_FUNCS_REQUESTS" ] || . cf_funcs_requests.sh
[ "$HAVE_FUNCS_ITEMS" ]    || . cf_funcs_items.sh
 
 
#Food
 
_check_mana_for_create_food(){
_debug "_check_mana_for_create_food:$*"
 
local lSP=${*:-$SP}
test "$lSP" || return 254
 
local REPLY
 
# This function forces drawing of
# all spells that start witch 'create'
# to the message panel and the reads the
# drawinfo lines.
# It needs the SP variable set by
# _check_food_level()
_watch $DRAWINFO
_is 1 0 cast create
 
while :;
do
 
read -t ${TMOUT:-1}
   _log "_check_mana_for_create_food:$REPLY"
 _msg 7 "_check_mana_for_create_food:$REPLY"
 
 case $REPLY in
 *ready*the*spell*create*food*) return 0;;
 *create*food*)
 MANA_NEEDED=`echo "$REPLY" | awk '{print $NF}'`
 _debug "MANA_NEEDED=$MANA_NEEDED"
 test "$lSP" -ge "$MANA_NEEDED" && return 0 || break 1
 ;;
 *'Something blocks your spellcasting.'*) _exit 1;;
 *scripttell*break*) break ${REPLY##*?break};;
 *scripttell*exit*)  _exit 1 $REPLY;;
 *'YOU HAVE DIED.'*) _just_exit;;
 '') break 1;;
 *) :;;
 esac
 
sleep 0.01
unset REPLY
done
 
_unwatch $DRAWINFO
return 1
}
 
_cast_create_food_and_eat(){
_debug "_cast_create_food_and_eat:$*"
 
local lEAT_FOOD BUNGLE
 
lEAT_FOOD="${*:-$EAT_FOOD}"
lEAT_FOOD=${lEAT_FOOD:-"$FOOD_DEF"}
lEAT_FOOD=${lEAT_FOOD:-food}
 
#while :;
#do
#_check_mana_for_create_food && break || { sleep 10; continue; }
#done
 
#_is 1 1 pickup 0
_set_pickup 0
 
_unwatch $DRAWINFO
_empty_message_stream
_watch $DRAWINFO
 
unset HAVE_NOT_SPELL
# TODO: Check MANA
_is 1 1 cast create food $lEAT_FOOD
while :;
 do
 unset REPLY
 read -t $TMOUT
 _log "_cast_create_food_and_eat:$REPLY"
 _msg 7 "$REPLY"
 case $REPLY in
 *Cast*what*spell*) HAVE_NOT_SPELL=1; break 1;; #Cast what spell?  Choose one of:
 *ready*the*spell*)  break 1;;                  #You ready the spell create food
 '')                 break 1;;
 *scripttell*break*) break ${REPLY##*?break};;
 *scripttell*exit*)  _exit 1 $REPLY;;
 *'YOU HAVE DIED.'*) _just_exit;;
 *) :;;
 esac
sleep 0.01
done
 
test "$HAVE_NOT_SPELL" && return 253
 
_empty_message_stream
 
while :;
do
_is 1 1 fire_stop # precaution
sleep 0.1
 
 while :;
 do
  _check_mana_for_create_food && break
  sleep 10
 done
 
# _check_mana_for_create_food returns early
_unwatch $DRAWINFO
_empty_message_stream
sleep 0.2
 
_watch $DRAWINFO
_is 1 1 fire center ## TODO: handle bungling the spell
_is 1 1 fire_stop
 
 while :; do
  unset BUNGLE
  read -t $TMOUT BUNGLE
  _log "_cast_create_food_and_eat:$BUNGLE"
  _msg 7 "BUNGLE=$BUNGLE"
  case $BUNGLE in
  *bungle*|*fumble*) break 1;;
  '') break 2;;
  *scripttell*break*) break ${BUNGLE##*?break};;
  *scripttell*exit*)  _exit 1 $BUNGLE;;
  *'YOU HAVE DIED.'*) _just_exit;;
  *) :;;
  esac
 sleep 0.01
 done
 
sleep 0.2
done
 
_unwatch $DRAWINFO
_is 1 1 fire_stop
_empty_message_stream
_sleep
 
_check_if_on_item -l ${lEAT_FOOD:-haggis}
case $? in 0) _is 1 1 apply;;
*) _is 1 1 get all
   _is 1 1 apply ${lEAT_FOOD:-haggis};;
esac
 
_empty_message_stream
}
 
_apply_horn_of_plenty_and_eat(){
_debug "_apply_horn_of_plenty_and_eat:$*"
 
_is 1 1 apply -u Horn of Plenty
_is 1 1 apply -a Horn of Plenty
_sleep
 
_is 1 1 fire center ## TODO: handle bungling
_is 1 1 fire_stop
_sleep
 
_check_if_on_item -lt ${FOOD_DEF:-haggis} && _is 1 1 apply
}
 
_eat_food_from_inventory(){
_debug "_eat_food_from_inventory:$*"
 
local lEAT_FOOD="${@:-$EAT_FOOD}"
lEAT_FOOD=${lEAT_FOOD:-"$FOOD_DEF"}
test "$lEAT_FOOD" || return 254
 
#_check_food_in_inventory ## Todo: check if food is in INV
 
if _check_have_item_in_inventory $lEAT_FOOD; then
     unset NROF_ITEM
  _is 1 1 apply $lEAT_FOOD
else unset NROF_ITEM
     false
fi
}
 
_eat_food_from_open_container(){
_debug "_eat_food_from_open_container:$*"
 
local lEAT_FOOD="${@:-$EAT_FOOD}"
lEAT_FOOD=${lEAT_FOOD:-"$FOOD_DEF"}
test "$lEAT_FOOD" || return 254
 
#_check_food_in_inventory ## Todo: check if food is in INV
#_check_have_item_in_open_container $lEAT_FOOD && _is 1 1 apply -b $lEAT_FOOD
_is 1 1 apply -b $lEAT_FOOD
}
 
__check_food_level(){
_debug "__check_food_level:$*"
 
test "$*" && MIN_FOOD_LEVEL="$@"
MIN_FOOD_LEVEL=${MIN_FOOD_LEVEL:-$MIN_FOOD_LEVEL_DEF}
MIN_FOOD_LEVEL=${MIN_FOOD_LEVEL:-300}
 
local FOOD_LVL=''
local REPLY
 
_empty_message_stream
_sleep
 
echo request stat hp   #hp,maxhp,sp,maxsp,grace,maxgrace,food
while :;
do
unset HP MHP SP MSP GR MGR FOOD_LVL
read -t ${TMOUT:-1} Re Stat Hp HP MHP SP MSP GR MGR FOOD_LVL
   _log "__check_food_level:HP=$HP $MHP $SP $MSP $GR $MGR FOOD_LVL=$FOOD_LVL"
 _msg 7 "HP=$HP $MHP $SP $MSP $GR $MGR FOOD_LVL=$FOOD_LVL" #DEBUG
 
test "$Re" = request || continue
test "$FOOD_LVL" || break
test "${FOOD_LVL//[[:digit:]]/}" && break
 
if test "$FOOD_LVL" -lt $MIN_FOOD_LEVEL; then
 #_eat_food_from_inventory
 _cast_create_food_and_eat $EAT_FOOD || _eat_food_from_inventory $EAT_FOOD
 
 _sleep
 _empty_message_stream
 _sleep
 echo request stat hp   #hp,maxhp,sp,maxsp,grace,maxgrace,food
 _sleep
 read -t ${TMOUT:-1} Re2 Stat2 Hp2 HP2 MHP2 SP2 MSP2 GR2 MGR2 FOOD_LVL
   _log "__check_food_level:HP=$HP2 $MHP2 $SP2 $MSP2 $GR2 $MGR2 FOOD_LVL=$FOOD_LVL"
 _msg 7 "HP=$HP2 $MHP2 $SP2 $MSP2 $GR2 $MGR2 FOOD_LVL=$FOOD_LVL" #DEBUG
 break
fi
 
test "${FOOD_LVL//[[:digit:]]/}" || break
test "$FOOD_LVL" && break
test "$oF" = "$FOOD_LVL" && break
 
oF="$FOOD_LVL"
sleep 0.1
done
}
 
_check_food_level(){
_debug "_check_food_level:$*"
 
test "$*" && MIN_FOOD_LEVEL="$@"
MIN_FOOD_LEVEL=${MIN_FOOD_LEVEL:-$MIN_FOOD_LEVEL_DEF}
MIN_FOOD_LEVEL=${MIN_FOOD_LEVEL:-300}
 
local FOOD_LVL=''
local REPLY
 
while :;
do
 
_request_stat_hp # FOOD_LVL
 
if test "$FOOD_LVL" -lt $MIN_FOOD_LEVEL; then
 #_eat_food_from_inventory
 _cast_create_food_and_eat $EAT_FOOD || _eat_food_from_inventory $EAT_FOOD
 _request_stat_hp
  break
else true
fi
 
test "$FOOD_LVL" && break
 
sleep 0.1
done
}
 
#Food
 
###END###
HAVE_FUNCS_FOOD=1