bash scripting

Technical and coding related questions.

Moderator: Board moderators

Post Reply
karl

bash scripting

Post by karl »

Here is a script to run in the command line of the client to
bulk mark icecube and light them :

Code: Select all

#!/bin/bash

# *** Here begins program *** #
echo draw 2 "$0 is started.."

# *** Check for parameters *** #
[ "$*" ] && {
PARAM_1="$1"

# *** implementing 'help' option *** #
test "$PARAM_1" = "help" && {

echo draw 5 "Script to melt icecube."
echo draw 5 "Syntax:"
echo draw 5 "script $0 [number]"
echo draw 5 "For example: 'script $0 5'"
echo draw 5 "will issue 5 times mark icecube and apply filint and steel."

        exit 0
        }

# *** testing parameters for validity *** #
PARAM_1test="${PARAM_1//[[:digit:]]/}"
test "$PARAM_1test" && {
echo draw 3 "Only :digit: numbers as first option allowed."
        exit 1 #exit if other input than letters
        }

NUMBER=$PARAM_1

}
#|| {
#echo draw 3 "Script needs number of praying attempts as argument."
#        exit 1
#}

#test "$1" || {
#echo draw 3 "Need <number> ie: script $0 50 ."
#        exit 1
#}


f_exit(){
echo draw 3 "Exiting $0."
echo unwatch
#echo unwatch drawinfo
exit $1
}

# *** Actual script to pray multiple times *** #
test "$NUMBER" && { test $NUMBER -ge 1 || NUMBER=1; } #paranoid precaution

    if test "$NUMBER"; then

for one in `seq 1 1 $NUMBER`
do

REPLY=
OLD_REPLY=

echo watch drawinfo
echo "issue 1 1 mark icecube"

 while [ 1 ]; do
 read -t 1 REPLY
 echo "$REPLY" >>/tmp/cf_script.rpl
 test "`echo "$REPLY" | grep 'Could not find an object that matches'`" && f_exit 1
 #test "`echo "$REPLY" | grep '.*There are only.*'`"  && f_exit 1
 #test "`echo "$REPLY" | grep '.*There is only.*'`"   && f_exit 1
 test "$REPLY" || break
 test "$REPLY" = "$OLD_REPLY" && break
 OLD_REPLY="$REPLY"
 sleep 0.1s
 done

echo unwatch drawinfo
sleep 1s

NO_FAIL=
until [ "$NO_FAIL" ]
do

REPLY=
OLD_REPLY=

echo watch drawinfo
echo "issue 1 1 apply flint and steel"

 while [ 1 ]; do
 read -t 1 REPLY
 echo "$REPLY" >>/tmp/cf_script.rpl
 test "`echo "$REPLY" | grep 'fail'`" || NO_FAIL=1
 #test "`echo "$REPLY" | grep '.*There are only.*'`"  && f_exit 1
 #test "`echo "$REPLY" | grep '.*There is only.*'`"   && f_exit 1
 test "$REPLY" || break
 test "$REPLY" = "$OLD_REPLY" && break
 OLD_REPLY="$REPLY"
 sleep 0.1s
 done

echo unwatch drawinfo
sleep 1s

done #NO_FAIL

done #NUMBER

    else #PARAM_1

until [ "$NO_ICECUBE" ];
do

REPLY=
OLD_REPLY=

echo watch drawinfo
echo "issue 1 1 mark icecube"
while [ 1 ]; do
 read -t 1 REPLY
 echo "$REPLY" >>/tmp/cf_script.rpl
 test "`echo "$REPLY" | grep 'Could not find an object that matches'`" && f_exit 1
 #test "`echo "$REPLY" | grep '.*There are only.*'`"  && f_exit 1
 #test "`echo "$REPLY" | grep '.*There is only.*'`"   && f_exit 1
 test "$REPLY" || break
 test "$REPLY" = "$OLD_REPLY" && break
 OLD_REPLY="$REPLY"
 sleep 0.1s
 done

echo unwatch drawinfo
sleep 1s

NO_FAIL=
until [ "$NO_FAIL" ]
do


REPLY=
OLD_REPLY=

echo watch drawinfo
echo "issue 1 1 apply flint and steel"
 while [ 1 ]; do
 read -t 1 REPLY
 echo "$REPLY" >>/tmp/cf_script.rpl
 test "`echo "$REPLY" | grep 'fail'`" || NO_FAIL=1
 #test "`echo "$REPLY" | grep '.*There are only.*'`"  && f_exit 1
 #test "`echo "$REPLY" | grep '.*There is only.*'`"   && f_exit 1
 test "$REPLY" || break
 test "$REPLY" = "$OLD_REPLY" && break
 OLD_REPLY="$REPLY"
 sleep 0.1s
 done

echo unwatch drawinfo
sleep 1s

done #NO_FAIL

done #true

    fi #^!PARAM_1


# *** Here ends program *** #
echo draw 2 "$0 is finished."
It is useful when using "cast icestorm" and "pickup 4" picking up huge amount of ice cubes .

It works as
#!/bin/ash
Almquist shell
provided by /bin/busybox
for me too .

Improvements welcome !
karl

shell script to pray multiple times

Post by karl »

Praying multiple times may be inconvenient pressing hundred times the p key .

A script can do it, too .

There is or are an or some example for it at the wiki of crossfire in the client side scripting section [ http://wiki.cross-fire.org/ ] .


The main problem for me is to sync the speed
the script issues the use_skill praying command send by client to the server
and the status messages back to the client with the player's speed .

I first started to use a simple sleep 1s for pausing 1 second .
This showed a huge async especially for lower character speeds like less than 0.6 .

I implemented a "simple" usleep algorhythm, that is somewhat OK, but not perfect
and likely could be improved.
Anyone that has a better clue, please share !

Using /bin/busybox ash shell,
but should work in /bin/bash also !

As usual,

Code: Select all

script /path/to/script
in the command line should run it.

I have named it

Code: Select all

$HOME/.crossfire/s/cf_PrayXtimes.sh
and made a link to it simply named as $HOME/.crossfire/s/p

It needs a parameter as a digit number for the amount of praying attempts to issue .
As

Code: Select all

$HOME/.crossfire/s/p 100

Code: Select all

#!/bin/ash

# *** Color numbers found in common/shared/newclient.h : *** #
#define NDI_BLACK       0
#define NDI_WHITE       1
#define NDI_NAVY        2
#define NDI_RED         3
#define NDI_ORANGE      4
#define NDI_BLUE        5       /**< Actually, it is Dodger Blue */
#define NDI_DK_ORANGE   6       /**< DarkOrange2 */
#define NDI_GREEN       7       /**< SeaGreen */
#define NDI_LT_GREEN    8       /**< DarkSeaGreen, which is actually paler
#                                 *   than seagreen - also background color. */
#define NDI_GREY        9
#define NDI_BROWN       10      /**< Sienna. */
#define NDI_GOLD        11
#define NDI_TAN         12      /**< Khaki. */
#define NDI_MAX_COLOR   12      /**< Last value in. */
#
#define NDI_COLOR_MASK  0xff    /**< Gives lots of room for expansion - we are
#                                 *   using an int anyways, so we have the
#                                 *   space to still do all the flags.
#                                 */
#define NDI_UNIQUE      0x100   /**< Print immediately, don't buffer. */
#define NDI_ALL         0x200   /**< Inform all players of this message. */
#define NDI_ALL_DMS     0x400   /**< Inform all logged in DMs. Used in case of
#                                 *   errors. Overrides NDI_ALL. */

export PATH=/bin:/usr/bin


# *** Here begins program *** #
echo draw 2 "$0 has started.."
echo draw 2 "PARAM:$* PID:$$ PPID :$PPID"

# *** Check for parameters *** #
[ "$*" ] && {
PARAM_1="$1"

# *** implementing 'help' option *** #
test "$PARAM_1" = "help" && {

echo draw 5 "Script to pray given number times."
echo draw 5 "Syntax:"
echo draw 5 "script $0 <number>"
echo draw 5 "For example: 'script $0 50'"
echo draw 5 "will issue 50 times the use_skill praying command."

        exit 0
        }

# *** testing parameters for validity *** #
PARAM_1test="${PARAM_1//[[:digit:]]/}"
test "$PARAM_1test" && {
echo draw 3 "Only :digit: numbers as first option allowed."
        exit 1 #exit if other input than letters
        }

NUMBER=$PARAM_1

} || {
echo draw 3 "Script needs number of praying attempts as argument."
        exit 1
}

test "$1" || {
echo draw 3 "Need <number> ie: script $0 50 ."
        exit 1
}

echo request stat cmbt
#read REQ_CMBT
#snprintf(buf, sizeof(buf), "request stat cmbt %d %d %d %d %d\n", cpl.stats.wc, cpl.stats.ac, cpl.stats.dam, cpl.stats.speed, cpl.stats.weapon_sp);
read Req Stat Cmbt WC AC DAM SPEED W_SPEED
echo draw 7 "wc=$WC:ac=$AC:dam=$DAM:speed=$SPEED:weaponspeed=$W_SPEED"
case $SPEED in
1[0-9][0-9][0-9][0-9]) USLEEP=1500000;;
2[0-9][0-9][0-9][0-9]) USLEEP=1400000;;
3[0-9][0-9][0-9][0-9]) USLEEP=1300000;;
4[0-9][0-9][0-9][0-9]) USLEEP=1200000;;
5[0-9][0-9][0-9][0-9]) USLEEP=1100000;;
6[0-9][0-9][0-9][0-9]) USLEEP=1000000;;
7[0-9][0-9][0-9][0-9]) USLEEP=900000;;
8[0-9][0-9][0-9][0-9]) USLEEP=800000;;
9[0-9][0-9][0-9][0-9]) USLEEP=700000;;
*) USLEEP=600000;;
esac
echo draw 10 "USLEEP=$USLEEP:SPEED=$SPEED"

USLEEP=$(( USLEEP- ((SPEED/10000)*1000) ))
echo draw 7 "Sleeping $USLEEP usleep micro-seconds between praying"


# *** Actual script to pray multiple times *** #
test $NUMBER -ge 1 || NUMBER=1 #paranoid precaution

for one in `seq 1 1 $NUMBER`
do

echo "issue 1 1 use_skill praying"
#sleep 1s
usleep $USLEEP

done

# *** Here ends program *** #
echo draw 2 "$0 is finished."
karl

Post by karl »

Script to train / level up fighting skills like clawing, flame touch, karate, punching.

Intention is to put the player inside a dungeon room with generators and let the player fire the skill
onto the tiles next to him that surround him.

The player could either turn around all the time attacking each of the spots 1-8 around him,
or only one direction.

The help shown by the '-h` option reads as

Code: Select all


""Script to fire killing monsters"
"by skill given as parameter."
"To be used in the crossfire roleplaying game client."
"Syntax:"
"$0 <<NUMBER>> <<Options>>"
"Options:"
"Simple number as first parameter:Just make NUMBER loop rounds."
"-C # :like above, just make NUMBER loop rounds."
"-A # :Number of attack attempts per spot."
"-B : brace character."
"-Z : turn anti-clockwise."
"-c : train skill clawing."
"-f : train skill flame touch."
"-k : train skill karate."
"-o : train skill one handed weapon."
"-p : train skill punching."
"-t : train skill two handed weapons."
"-D word :Direction to fire to,"
" as n, ne, e, se, s, sw, w, nw."
" If no direction, turns clockwise all around on the spot."
"-V   :Print version information."
"-d   :Print debugging to msgpane."-t : train skill two handed weapons."
I have posted the whole script on pastebin.com : https://pastebin.com/SQD7uhZw
as crossfire_train_melee.sh and have set the expiration time to one year.

All the functions have a _stdalone extension.
If you want, you can hack these functions into several function libraries as

. $HOME/cf/s/cf_funcs_common.sh || exit 4
. $HOME/cf/s/cf_funcs_food.sh || exit 5
. $HOME/cf/s/cf_funcs_move.sh || exit 7
. $HOME/cf/s/cf_funcs_skills.sh || exit 9
. $HOME/cf/s/cf_funcs_fight.sh || exit 10
. $HOME/cf/s/cf_funcs_requests.sh || exit 12

source-able functions.
The main code is run at the very end of the script as either

_main_melee_stdalone "$@"
#_main_melee_func "$@"

You need to comment the function call, that you do not want to use.

The script is 1400+ lines long.


The usual disclaimer : I am not a failure free man, who functions 45 years until getting pensioner
without any flaws to every one's satisfaction.
So there might be Bugs still, I am too lazy to fix now;
especially the error handling of wrongly entered positional parameters seems to be missing completely.

Suggestions and fixes are welcome !
Also reports, if somebody used it with whatever success.

The shell I prefer is the busybox almquist shell 'ash` .
Busybox could be kind of heard of due to the wave of rooting Android phones.
Most Linuxes should have busybox installed in /bin directory and ash enabled in it's
compile time configuration .

The usual 'bash` shell should work.
Just change the first line for the name of the interpreter to '#!/bin/bash` .


Good luck !
Post Reply