void main()
{

        ///////////
        //Sometimes spawn monster in during rest v1.2
        ///////////
        //This script rolls a D6 and spawns a monster with a CR
        //of your level if a 3 is rolled, or spawns a monster with
        //a CR of 1 higher than your level if a 4 is rolled.
        ///////////
        //Script by Michael Ham (mindtakerr)
        //Date Created: 6/24/02
        //Filename: monster_rest
        ///////////

        // An object to hold the last player who rested
        object oPlayer = GetLastPCRested();

        // Check to see whether resting has started or is over
        int nRestTurn = GetLocalInt(oPlayer, "nRestTurns");
        SetLocalInt(oPlayer, "nRestTurns", nRestTurn + 1);
        nRestTurn = GetLocalInt(oPlayer, "nRestTurns");

        if (nRestTurn == 1) {

                // Determine whether rest will be peaceful or not
                int nRestType = d6();
                int nEnemyType = d3();

                object oEnemy;
                // A string to hold the creature's tag
                string sCreature;

                // Set a location for where the PC is
                location lHere = GetLocation(oPlayer);

                // Set the level of the monster to spawn
                int iLevel = 0;
                iLevel += GetLevelByPosition(1, oPlayer);
                iLevel += GetLevelByPosition(2, oPlayer);
                iLevel += GetLevelByPosition(3, oPlayer);

                // A roll of 4 causes the monster to be even harder
                if (nRestType == 4) {
                        iLevel++;
                        nRestType = 3;
                }

                // Script only has 15 places for monster levels
                if (iLevel > 15) {
                        iLevel = 15;
                }

                // Bad luck, a monster is coming
                if (nRestType == 3)  {

                        // Decide which monster to spawn, based on CR
                        switch (iLevel) {
                                case 1:
                                if (nEnemyType == 1)
                                sCreature = "NW_ORCA";
                                else if (nEnemyType == 2)
                                sCreature = "NW_WOLF";
                                else
                                sCreature = "NW_ZOMBIE01";
                                break;
                                case 2:
                                if (nEnemyType == 1)
                                sCreature = "NW_BEARBLCK";
                                else if (nEnemyType == 2)
                                sCreature = "NW_BUGBEARA";
                                else
                                sCreature = "NW_KRENSHAR";
                                break;
                                case 3:
                                if (nEnemyType == 1)
                                sCreature = "NW_MEPAIR";
                                else if (nEnemyType == 2)
                                sCreature = "NW_BTLBOMB";
                                else
                                sCreature = "NW_DIREBADG";
                                break;
                                case 4:
                                if (nEnemyType == 1)
                                sCreature = "NW_GHAST";
                                else if (nEnemyType == 2)
                                sCreature = "NW_AIR";
                                else
                                sCreature = "NW_MINOTAUR";
                                break;
                                case 5:
                                if (nEnemyType == 1)
                                sCreature = "NW_OGREMAGE01";
                                else if (nEnemyType == 2)
                                sCreature = "NW_BUGBEARA";
                                else
                                sCreature = "NW_KRENSHAR";
                                break;
                                case 6:
                                if (nEnemyType == 1)
                                sCreature = "NW_OGRECHIEF01";
                                else if (nEnemyType == 2)
                                sCreature = "NW_TROLL";
                                else
                                sCreature = "NW_DEVOUR";
                                break;
                                case 7:
                                if (nEnemyType == 1)
                                sCreature = "NW_SLAADBL";
                                else if (nEnemyType == 2)
                                sCreature = "NW_UMBERHULK";
                                else
                                sCreature = "NW_MUMMY";
                                break;
                                case 8:
                                if (nEnemyType == 1)
                                sCreature = "NW_GOLFLESH";
                                else if (nEnemyType == 2)
                                sCreature = "NW_ETTIN";
                                else
                                sCreature = "NW_TROLLCHIEF";
                                break;
                                case 9:
                                if (nEnemyType == 1)
                                sCreature = "NW_BEARDIRE";
                                else if (nEnemyType == 2)
                                sCreature = "NW_MINWIZ";
                                else
                                sCreature = "NW_RAKSHASA";
                                break;
                                case 10:
                                if (nEnemyType == 1)
                                sCreature = "NW_GNTFROST";
                                else if (nEnemyType == 2)
                                sCreature = "nw_bugbearboss";
                                else
                                sCreature = "NW_GNTHILL";
                                break;
                                case 11:
                                if (nEnemyType == 1)
                                sCreature = "NW_GOBLINBOSS";
                                else if (nEnemyType == 2)
                                sCreature = "NW_BATTDEVOUR";
                                else
                                sCreature = "NW_REVENANT001";
                                break;
                                case 12:
                                if (nEnemyType == 1)
                                sCreature = "NW_GolClay";
                                else if (nEnemyType == 2)
                                sCreature = "NW_SHGUARD";
                                else
                                sCreature = "NW_DRGBLACK001";
                                break;
                                case 13:
                                if (nEnemyType == 1)
                                sCreature = "NW_SPIDERBOSS";
                                else if (nEnemyType == 2)
                                sCreature = "NW_DRGBLACK001";
                                else
                                sCreature = "NW_GNTFIRE";
                                break;
                                case 14:
                                if (nEnemyType == 1)
                                sCreature = "NW_MUMMYBOSS";
                                else if (nEnemyType == 2)
                                sCreature = "drgblue002";
                                else
                                sCreature = "NW_GOLIRON";
                                break;
                                case 15:
                                if (nEnemyType == 1)
                                sCreature = "nw_beetleboss";
                                else if (nEnemyType == 2)
                                sCreature = "NW_FIREGREAT";
                                else
                                sCreature = "NW_DRGGREEN002";
                                break;
                        }

                        // Create the enemy at the PC's location
                        oEnemy = CreateObject(OBJECT_TYPE_CREATURE, sCreature, lHere, FALSE);

                        // The PC alerts of danger
                        AssignCommand(oPlayer, ActionSpeakString("I'm under attack!"));
                        // Wait a moment so that the PC might get ready
                        AssignCommand(oEnemy, ActionWait(3.0));
                        // Tell the enemy to attack the player
                        AssignCommand(oEnemy, ActionAttack(oPlayer));
                        SetLocalInt(oPlayer, "nRestTurns", -1);
                }
        }
        if(nRestTurn != 1)
        SetLocalInt(oPlayer, "nRestTurns", 0);
}

