NWNWiki
Advertisement
NWNWiki
3,718
pages

Slot Machine v1.1[]

What it is[]

This is a major revision to my slot machine. It consists of two objects. One is a chest that allows you to deposit money into a "wagering account" and withdraw whatever money you have left when you are done. The other object is a lever which is the slot machine itself.


The script, part 1[]

If you want to make the objects yourself, first create a chest. On the OnOpen event of the chest put this script into it:

//::///////////////////////////////////////////////
//:: Withdraw Gold
//:://////////////////////////////////////////////
/*
     Withdraw gold from your slot machine "account". Put this on the "OnOpen"
     event of a chest near the lever.
*/
// Main
void main()
{
    object oPC = GetLastOpenedBy();
    if ( !GetIsPC( oPC ) )
        return;
    if ( GetLocalInt( oPC, "sm_SlotGold" ) > 0 )
    {
        CreateItemOnObject( "nw_it_gold001", OBJECT_SELF, GetLocalInt( oPC, "sm_SlotGold" ) );
        SetLocalInt( oPC, "sm_SlotGold", 0 );
    }
    else
    {
        SpeakString( "Place gold you wish to wager into the chest and close it." );
    }
}

The Script, part 2[]

On the OnClose event, put this script

//::///////////////////////////////////////////////
//:: Deposit Gold
//:://////////////////////////////////////////////
/*
     Deposit gold into your slot machine "account". Put this on the "OnClose"
     event of a chest near the lever.
*/
// Main
void main()
{
    object oPC = GetLastOpenedBy();
    int iGold = GetGold( OBJECT_SELF );
    if ( !GetIsPC( oPC ) )
        return;
    if ( iGold > 0 )
    {
        SetLocalInt( oPC, "sm_SlotGold", iGold );
        object oGold = GetFirstItemInInventory();
        while( oGold != OBJECT_INVALID )
        {
            if ( GetTag( oGold ) == "NW_IT_GOLD001" )
            {
                DestroyObject( oGold );
                return;
            }
            oGold = GetNextItemInInventory();
        }
    }
}

The Script, part 3[]

On the OnUsed event of the lever put this

//::///////////////////////////////////////////////
//:: Slot Machine Lever v1.1
//:://////////////////////////////////////////////
/*
     Put this script on the OnUsed event of a floor lever. On each pull of the
     lever, 1g is deducted from the PC's account. Each pull has a chance of
     winning up to 25,000g.
*/
//:://////////////////////////////////////////////
//:: Created By: August 6th, 2002
//:: Created On: Karthal <kar_of_albion@hotmail.com>
//:://////////////////////////////////////////////
// Set "constants"
int ROLL_GARNET  = 100;
int ROLL_TOPAZ   = 148;
int ROLL_SAPHIRE = 160;
int ROLL_DIAMOND = 166;
int ROLL_RUBY    = 170;
int ROLL_EMERALD = 173;
int ROLL_MAX     = 173;
int PAYOUT_GARNET  = 2;
int PAYOUT_TOPAZ   = 5;
int PAYOUT_SAPHIRE = 300;
int PAYOUT_DIAMOND = 2500;
int PAYOUT_RUBY    = 10000;
int PAYOUT_EMERALD = 30000;
int DEBUG = FALSE;
// Declare functions
string GetGem( int iGem );
int GetPayout( string sGem1, int iWager );
// Main
void main()
{
    // Determine the size of the wager
    int iWager = FindSubString( GetTag( OBJECT_SELF ), "_W");
    if ( iWager >= 0 )
        iWager = StringToInt( GetSubString( GetTag( OBJECT_SELF ), iWager + 2, 4 ) );
    else iWager = 1;
    // Run
    object oPC = GetLastUsedBy();
    if ( GetIsPC( oPC ) )
    {
        if ( GetLocalInt( oPC, "sm_SlotGold" ) == 0 )
        {
            SpeakString( "This is a " + IntToString( iWager ) + "g machine. If you wish to wager your gold, you must put it in the chest." );
        }
        else if ( iWager > GetLocalInt( oPC, "sm_SlotGold" ) )
        {
            SpeakString( "The minimum wager is " + IntToString( iWager ) + ". You must add more gold into the chest." );
        }
        else
        {
            // Animate the lever
            AssignCommand( OBJECT_SELF, ActionPlayAnimation( ANIMATION_PLACEABLE_DEACTIVATE ) );
            AssignCommand( OBJECT_SELF, DelayCommand( 1.0, ActionPlayAnimation( ANIMATION_PLACEABLE_ACTIVATE ) ) );
            // Decrement wager from the PC's account
            SetLocalInt( oPC, "sm_SlotGold", GetLocalInt( oPC, "sm_SlotGold" ) - iWager );
            // Spin the wheels
            string sGem1 = GetGem( Random( ROLL_MAX ) + 1 );
            string sGem2 = GetGem( Random( ROLL_MAX ) + 1 );
            string sGem3 = GetGem( Random( ROLL_MAX ) + 1 );
            FloatingTextStringOnCreature( sGem1 + " : " + sGem2 + " : " + sGem3, oPC );
            // Check for winner
            if ( sGem1 == sGem2 && sGem1 == sGem3 )
            {
                SetLocalInt( oPC, "sm_SlotGold", GetLocalInt( oPC, "sm_SlotGold" ) + GetPayout( sGem1, iWager ) );
                FloatingTextStringOnCreature( "WE HAVE A WINNER!", oPC );
                PlaySound( "as_cv_shopmetal1");
            }
            // Display gold in account
            FloatingTextStringOnCreature( IntToString( GetLocalInt( oPC, "sm_SlotGold" ) ) + " gold left.", oPC );
        }
    }
}
string GetGem( int iGem )
{
    if ( iGem <= ROLL_GARNET )
        return "Garnet";
    else if ( iGem <= ROLL_TOPAZ )
        return "Topaz";
    else if ( iGem <= ROLL_SAPHIRE )
        return "Saphire";
    else if ( iGem <= ROLL_DIAMOND )
        return "Diamond";
    else if ( iGem <= ROLL_RUBY )
        return "Ruby";
    else if ( iGem <= ROLL_EMERALD )
        return "Emerald";
    return "None";
}
int GetPayout( string sGem1, int iWager )
{
    if ( sGem1 == "Garnet" )
        return iWager * PAYOUT_GARNET;
    else if ( sGem1 == "Topaz" )
        return iWager * PAYOUT_TOPAZ;
    else if ( sGem1 == "Saphire" )
        return iWager * PAYOUT_SAPHIRE;
    else if ( sGem1 == "Diamond" )
        return iWager * PAYOUT_DIAMOND;
    else if ( sGem1 == "Ruby" )
        return iWager * PAYOUT_RUBY;
    else if ( sGem1 == "Emerald" )
        return iWager * PAYOUT_EMERALD;
    return 0;
}


How it works[]

When you put gold in the chest and close it, the gold is destroyed and a local variable is set on the PC.

When the PC pulls the lever, the amount of gold in the wagering account is decremented by 1. Then the script randomly chooses 3 gems. If all 3 match, the PC wins gold and the local variable on the PC is incremented.

When the PC goes back to open the chest, an amount of gold equal to what is in the PC's wagering account is created in the chest and the local variable is set to zero.

If you import the .erf, notice the tag on the slot lever. It contains the switch _W0001. If you want it to be a nickel slot change the switch to _W0005. If you want the wager to be 100g each time, change the switch to _W0100. Make sure to change the name of the lever as well.

Here is what the payout is if the wager is 1g:

Garnet : Garnet : Garnet = 2g
Topaz : Topaz : Topaz = 5g 
Saphire : Saphire : Saphire = 300g
Diamond : Diamond : Diamond = 2500g
Ruby : Ruby : Ruby = 10,000g
Emerald : Emerald : Emerald = 30,000g

The overal payout rate is 97.75%, so for every 100g wagered it would payout 97.75g.

If you have questions or features you would like to see added, post them in this thread Click Here

Advertisement