JohnGilmore: This is a script I created. It's been pretty sophisticated for awhile, but it's only today (and some of yesterday) that I decided to pretty it up enough to share it. Note that the "Useage" function is called if you don't specify a ToME version (needed for finding savefiles) or if you specify --help.

JohnGilmore: Uhm, yeah. Now it actually works. It was late, I was tired, and I goofed. This actually works, instead of attempting to pass save/restore requests to ToME itself.

#!/bin/bash

#configuration section. this section actually properly include the
#functions lice "800x600" as those are the correct place to modify
#the window layout, should you choose to do so.
recordfile=.go-tome-rc.old
paramsfile=.go-tome-rc

#TODO: Add a "quiet" mode to suppress all those messages...

Useage()
{
        cat <<EOF
This is a shell script for automated savefile-scumming for ToME.
It also will automatically detect the resolution of your screeen,
and arrange the windows under accordingly. under X only, of course.
It will also remember the last thing you told it to do, and do that
if you don't specify (doesn't apply to save/restore stuff, of course)

It understands a few parameters that ToME does, and
will parse them in a attempt to locate your save file. These are:
-u<name>
-M<name>

In addition, there are some parameters which are unique to "go"
s       Make a backup of current savefile
r       Restore the most recent backup of savefile
rr      Restore the backup backup savefile (next-to-last save)
        This option is here in case you do "s" while dead, or some such.
        Been there, done that, this option is what I did about it.

And these options, which disable auto-checking for screen resolution
by forcing window arrangement as specified:
800x600
1024x768
1200x1600

And some misc. settings:
--dry-run       Don't actually run tome (for debugging "go")(not saved)
--help  Display this message and exit (not saved)
2.2/2.3/3.0     Set game version for finding savefile.
        ONE OF THESE VERSIONS IS REQUIRED FOR USING THIS SCRIPT

Any other option will be passed unchanged to ToME. Importantly, you
cannot pass display module options (the things after the --) 
I've never needed to, so I didn't bother parsing for a "--" and 
storing those differently.

The display module will be mx11 if \$DISPLAY is set, mgcu otherwise.

EOF
}


function 800x600() {
        echo "Arranging windows for 800x600"
# Main window
export ANGBAND_X11_FONT_0=9x15
export ANGBAND_X11_AT_X_0=0
export ANGBAND_X11_AT_Y_0=0

# inventory window - "Mirror"
export ANGBAND_X11_FONT_5=6x10
export ANGBAND_X11_AT_X_5=725
export ANGBAND_X11_AT_Y_5=0
#export ANGBAND_X11_ROWS_1 
export ANGBAND_X11_COLS_5=46

# Message window - "Recall"
export ANGBAND_X11_FONT_2=6x10
export ANGBAND_X11_AT_X_2=0
export ANGBAND_X11_AT_Y_2=390
export ANGBAND_X11_ROWS_2=30
export ANGBAND_X11_COLS_2=60

# Monster Recall window - "Choice"
export ANGBAND_X11_FONT_3=6x10
export ANGBAND_X11_AT_X_3=375
export ANGBAND_X11_AT_Y_3=390
export ANGBAND_X11_ROWS_3=30
export ANGBAND_X11_COLS_3=50

# Visible Monsters window - "Term-4"
export ANGBAND_X11_FONT_4=6x13
export ANGBAND_X11_AT_X_4=725
export ANGBAND_X11_AT_Y_4=500
#export ANGBAND_X11_ROWS_4 
export ANGBAND_X11_COLS_4=46

# Object recall window - "Term-5"
export ANGBAND_X11_FONT_1=6x13
export ANGBAND_X11_AT_X_1=725
export ANGBAND_X11_AT_Y_1=390
export ANGBAND_X11_COLS_1=30
export ANGBAND_X11_ROWS_1=10

# visible monsters - "Term-6"
export ANGBAND_X11_FONT_6=6x13
export ANGBAND_X11_AT_X_6=725
export ANGBAND_X11_AT_Y_6=345
export ANGBAND_X11_COLS_6=76
export ANGBAND_X11_ROWS_6=10
}


function 1024x768() {
        echo "Arranging windows for 1024x768"
# Main window
export ANGBAND_X11_FONT_0=9x15
export ANGBAND_X11_AT_X_0=0
export ANGBAND_X11_AT_Y_0=0

# inventory window - "Mirror"
export ANGBAND_X11_FONT_1=6x13
export ANGBAND_X11_AT_X_1=725
export ANGBAND_X11_AT_Y_1=270
export ANGBAND_X11_COLS_1=90
export ANGBAND_X11_ROWS_1=24


# Message window - "Recall"
export ANGBAND_X11_FONT_2=6x10
export ANGBAND_X11_AT_X_2=0
export ANGBAND_X11_AT_Y_2=390
export ANGBAND_X11_ROWS_2=50
export ANGBAND_X11_COLS_2=120

# Monster Recall window - "Choice"
export ANGBAND_X11_FONT_3=6x13
export ANGBAND_X11_AT_X_3=725
export ANGBAND_X11_AT_Y_3=600
export ANGBAND_X11_ROWS_3=12 
export ANGBAND_X11_COLS_3=90

# Visible Monsters window - "Term-4"
export ANGBAND_X11_FONT_4=6x10
export ANGBAND_X11_AT_X_4=725
export ANGBAND_X11_AT_Y_4=775
export ANGBAND_X11_ROWS_4=15
export ANGBAND_X11_COLS_4=90

# Object recall window - "Term-5"
export ANGBAND_X11_FONT_5=6x10
export ANGBAND_X11_AT_X_5=725
export ANGBAND_X11_AT_Y_5=0
#export ANGBAND_X11_ROWS_5 
export ANGBAND_X11_COLS_5=90

# visible monsters - "Term-6"
export ANGBAND_X11_FONT_6=6x13
export ANGBAND_X11_AT_X_6=725
export ANGBAND_X11_AT_Y_6=345
export ANGBAND_X11_COLS_6=76
export ANGBAND_X11_ROWS_6=10
}



function 1200x1600() {
        echo "Arranging windows for 1200x1600"
# Main window
export ANGBAND_X11_FONT_0=10x20
export ANGBAND_X11_AT_X_0=0
export ANGBAND_X11_AT_Y_0=0

# inventory window - "Mirror"
export ANGBAND_X11_FONT_5=8x13
export ANGBAND_X11_AT_X_5=806
export ANGBAND_X11_AT_Y_5=0
#export ANGBAND_X11_ROWS_1 

# Message window - "Recall"
export ANGBAND_X11_FONT_2=8x13
export ANGBAND_X11_AT_X_2=0
export ANGBAND_X11_AT_Y_2=490
export ANGBAND_X11_ROWS_2=22
export ANGBAND_X11_COLS_2=60

# Monster Recall window - "Choice"
export ANGBAND_X11_FONT_3=8x13
export ANGBAND_X11_AT_X_3=485
export ANGBAND_X11_AT_Y_3=500
export ANGBAND_X11_ROWS_3=22
export ANGBAND_X11_COLS_3=40

# Visible Monsters window - "Term-4"
export ANGBAND_X11_FONT_4=6x13
export ANGBAND_X11_AT_X_4=817
export ANGBAND_X11_AT_Y_4=500
#export ANGBAND_X11_ROWS_4 

# Object recall window - "Term-5"
export ANGBAND_X11_FONT_1=6x13
export ANGBAND_X11_AT_X_1=806
export ANGBAND_X11_AT_Y_1=500
export ANGBAND_X11_COLS_1=76
export ANGBAND_X11_ROWS_1=24

# visible monsters - "Term-6"
export ANGBAND_X11_FONT_5=6x13
export ANGBAND_X11_AT_X_5=808
export ANGBAND_X11_AT_Y_5=345
export ANGBAND_X11_COLS_5=76
export ANGBAND_X11_ROWS_5=10

}


#This function works by searching for a savefile of the name you've specified.
#If it doesn't exist, it may find the wrong location.
#This doesn't matter, as we'll only use this prefix for saving/restoring, which
#can't be done at all if the savefile doesn't exist in the firstplace.
setprefix(){
        [ "$VER" == "" ] && export VER=2.2
                [ ! -s "$PREFIX/$SAVENAME" ] && 
        export PREFIX='.'
        export SAVENAME=$NAME
                echo Trying w/ module name
                echo ver=$VER modname=$MODNAME savename=$SAVENAME
                [ ! -s "$PREFIX/$SAVENAME" ] && PREFIX=~/.tome/${VER}/$MODNAME      && echo $PREFIX/$SAVENAME
                [ ! -s "$PREFIX/$SAVENAME" ] && PREFIX=~/.tome/${VER}/$MODNAME/save && echo $PREFIX/$SAVENAME
                [ ! -s "$PREFIX/$SAVENAME" ] && PREFIX=~/.tome/${VER}/save/$MODNAME && echo $PREFIX/$SAVENAME
                [ ! -s "$PREFIX/$SAVENAME" ] && PREFIX=~/.tome/${VER}/$MODNAME/save && echo $PREFIX/$SAVENAME
                [ ! -s "$PREGIX/$SAVENAME" ] && echo Trying w/o module name
                [ ! -s "$PREFIX/$SAVENAME" ] && PREFIX=~/.tome/save                 && echo $PREFIX/$SAVENAME
                [ ! -s "$PREFIX/$SAVENAME" ] && PREFIX=~/.tome/${VER}/save          && echo $PREFIX/$SAVENAME
                [ ! -s "$PREFIX/$SAVENAME" ] && PREFIX=~/.tome/${VER}/tome/save     && echo $PREFIX/$SAVENAME
                [ ! -s "$PREFIX/$SAVENAME" ] && PREFIX=~/.tome/save                 && echo $PREFIX/$SAVENAME
                [ -s "./lib/save/${UID}.$NAME" ] && PREFIX=./lib/save
#       [ -s "$PREFIX/$NAMExxx.svg" ] && SAVENAME=$NAME.svg
        [ -s "$PREFIX/${UID}.$NAME" ] && SAVENAME=${UID}.$NAME
        
        if [ ! -d "$PREFIX/save" ] ; then 
                echo "Creating save directory..."
                mkdir "$PREFIX/save"
                mkdir "$PREFIX/save/save"
        fi
        if [ -s "$PREFIX/$SAVENAME" ]; then
                echo "Found SaveFile:$PREFIX/$SAVENAME"
        else
                echo "ERROR: cannot save/restore non-existant file!"
                exit
        fi
}
processparams(){
        export goodparams=''
        export PASSTOTOME=''
        while [ "$1" != "" ]; do
                # Test version type.. (changes savefile location..)
                case $1 in
                        2.2|3.0|2.3) export VER=$1; export goodparams=y ;;
                        -u*) export NAME=${1:2} ; goodparams=y ;;
                        -M*) export MOD=$1; MODNAME=${1:2}; goodparams=y ;;
                        --dry-run) export NORUNTOME=true; ;;
                        --help) Useage; exit;;
                        r);;
                        rr);;
                        s);;
                        -s*) export PREFIX=${1:2} ; goodparams=y ;;
                        800x600)  window_size=800x600;;
                        1024x768) window_size=1024x768;;
                        1200x1600)window_size=1200x1600;;
                        *)
                                #Save unknown params to pass to TOME
                                export PASSTOTOME="$PASSTOTOME $1"
                        ;;
                esac
                shift
        done
}
#important note here - this function DOES NOT print a trailing newline.
# If for some reason you want to save more parameters, list the corresponding
# variables here, and read them in the above function.
echoparams()
{
        echo -n "VER=$VER;"
        echo -n "MOD=$MOD;"
        echo -n "MODNAME=$MODNAME;"
        echo -n "NAME=$NAME;"
        echo -n "PASSTOTOME=$PASSTOTOME;"
        echo -n "window_size=$window_size;"
}


#This is the first real "action" line here.
        # load parameters from saved file!
        [ -s $paramsfile ] && echo saved parameters found, loading...
        [ -s $paramsfile ] && . $paramsfile
processparams $*
        echo -n params set to:
        echoparams
        echo ""

#Print useage if no "VER"
[ -z $VER ] && Useage && exit

# Gamma correction
#export ANGBAND_X11_GAMMA==42

# Find tome binary
if [ -e ./tome ]; then
        tome="./tome"
elif [ -e ./src/tome ]; then
        tome="./src/tome"
elif [ -e ./tome3 ]; then
        tome="./tome3"
elif [ -e ./src/tome3 ]; then
        tome="./src/tome3"
else
        echo "Can't find TOME executable!"
        exit
fi


#Save and restore parameters. 
# Note that save file is in the current directory, this is done
# so that running "go" from a particular source directory will
# always run the last game for that directory. A requirement
# for those of us who have multiple versions of tome.

# Good parameters, save them to file!
#First, check if they are the same.
if [ `echoparams` = `(. $paramsfile ; echoparams;)` ];then
        echo params are the same, not saved
else
        echo params are different, params saved.
        #Then save the old ones.
        grep "=" $paramsfile >>$recordfile
fi
#Finally, write the new ones.
(
        echo "#Tome \"go\" saved parameters file. Autogenerated."
        echoparams

        #Append a timestamp to the saved parameters.
        #Might be useful in looking over the things you've done
        echo -n '#';
        echo -n `date -R`;
        echo ""
) >$paramsfile



#Secondary processing
while [ "$1" != "" ]; do
        # Test version type.. (changes savefile location..)
        case $1 in
                s)
                        setprefix
                        echo "Saving $NAME's file"
                        set -vx
                        rm -f  "${PREFIX}/save/save/$SAVENAME"
                        mv "${PREFIX}/save/$SAVENAME" "${PREFIX}/save/save"
                        cp -r "${PREFIX}/$SAVENAME" "${PREFIX}/save"
                        set +vx
                ;;
                r)
                        setprefix
                        echo "Restoring $NAME's file backup."
                        set -vx
                        rm -f  "${PREFIX}/$SAVENAME"
                        cp "${PREFIX}/save/$SAVENAME" "${PREFIX}"
                        set +vx
                ;;
                rr)
                        setprefix
                        echo "Restoring $NAME's file backup of backup."
                        set -vx
                        rm -f  "${PREFIX}/$SAVENAME"
                        cp "${PREFIX}/save/save/$SAVENAME" "${PREFIX}"
                        set +vx
                ;;
        esac
        shift
done

echoparams
echo ""
[ -s "${PREFIX}/${NAME}" ] && echo "savefile found"
[ -s "${PREFIX}/${NAME}" ] || echo "savefile was not found"
if [ "" = "$NORUNTOME" ]; then

        if [ "$DISPLAY" != "" ]; then

                #Attempt determine display dimensions if not manually set.
                #Note that this automatic testing is done BELOW the param saving,
                #so the results of this will (properly) not be saved.
                if [ -z $window_size ]; then
                        window_size=`xdpyinfo | grep dimensions | cut -b 18-24`
                        echo "Windows size automatically determined. size detected=$window_size"
                fi

                #Arrange windows
                case $window_size in
                        800x600*) 800x600;;
                        1024x768*) 1024x768;;
                        1200x160*) 1200x1600;;
                        #This last is here because this is the closest match, it's "large", good enough.
                        1280x*) 1024x768;;
                        *)800x600;;
                esac

                #Call the appropriate ToME binary
                if [ -n "$NAME" ]; then
                        echo $tome $MOD -u$NAME -mx11 $PASSTOTOME -- -n6 -b
                        $tome $MOD -u$NAME -mx11 $PASSTOTOME  -- -n6 -b
                else
                        echo $tome $MOD -mx11 $PASSTOTOME  -- -n6 -b
                        $tome $MOD -mx11 $PASSTOTOME  -- -n6 -b
                fi
        else
                if [ -n "$NAME" ]; then
                        echo $tome $MOD -u$NAME -mgcu $PASSTOTOME 
                        $tome $MOD -u$NAME -mgcu $PASSTOTOME 
                else
                        echo $tome $MOD -mgcu $PASSTOTOME 
                        $tome $MOD -mgcu $PASSTOTOME 
                fi
        fi
fi

JohnGilmore/go (last edited 2006-09-23 14:26:58 by JohnGilmore)