//-------------------------------------------------------------------
// TAGHEX01.ACS
// Script file for TAGHEX.WAD
//-------------------------------------------------------------------

// The Gem team has the gem for it's flag.
// The Skull team has the skull for it's flag.

#include "specials.acs"
#include "defs.acs"
#include "wvars.acs"

int gemscore;   // Score for gem/pedestal team.
int skullscore; // Score for statue skull/team.
int gemid;      // TID of Big Gem puzzle item.
int skullid;	// TID of Skull puzzle item.
int skulloc;    // TID of Skull location.
int gemloc;     // TID of Gem location.
int nplayers;   // Number of players.
int monstdoor;  // Monster (boss) door sector ID.
int skulluse;   // 1=Skull sitting there; 2=Skull being carried.
int gemuse;     // 1=Gem sitting there; 2=Gem being carried.
int skulltime;  // Time when skull is picked up.
int gemtime;    // Time when gem is picked up.
int gentime;    // Use to time random generator.
int genloc;     // Random thing generator location.
int gswin;      // Someone won, don't open the other door.
int bossrel;    // Boss moster release flag.
int dscore;     // 0=Don't do anything; 1=Skull scored; 2=Gem scored.
int timeann;    // Time announcement for next random respawn.

//-------------------------------------------------------------------
// Initialization
//-------------------------------------------------------------------
script 0 OPEN
{
	gemscore   = 0;
	skullscore = 0;
        gemid = 4;
        skullid = 3;
        skulloc = 5;
        gemloc = 6;
        skulluse = 1;
        gemuse = 1;
        nplayers = PlayerCount();
        monstdoor = 1; // Sector tag. The doors have to have sequential tags.
        gentime = timer();
        genloc = 30;
        gswin = 0;
        bossrel = 0;
        dscore = 0;

        ACS_Execute(const:1,1,0,0,0); // Main processing loop.
}

//-------------------------------------------------------------------
// Called from: script 0
// Calls      : script 5,6,11,50
//-------------------------------------------------------------------
script 1 (void)
{
int mynoise;
while (1==1)
    {
//-----------------------------------------------
// Here is the way it's looking. When a player picks up the skull/gem,
// the thingcount will equal 0. You can't track what the player is carrying.
// So, how about this? Respawn a skull/gem in 5 minutes if it hasn't been
// used yet.
//-----------------------------------------------

// Extra important note!!! With this kind of loop (i.e. 1==1), there has to
// be a delay. Otherwise it hogs up all the processing time. Talk about non-
// multitasking. You can also use "Restart". Just watch variable initialization.

        delay(2*35); // Check once every 2 seconds.

        if (random(1,4) == 1) // Make noise in swamp room.
            {
            mynoise = random(1,4);
            Switch(mynoise) // Swamp room
                {
                Case 1:
                    {
                    thingsound(55,"Ambient1",64); // Insects1
                    break;
                    }
                Case 2:
                    {
                    thingsound(55,"Ambient2",64); // Crickets
                    break;
                    }
                Case 3:
                    {
                    thingsound(55,"Ambient3",64); // Crickets1
                    break;
                    }
                Case 4:
                    {
                    thingsound(55,"Ambient5",64); // Frogs
                    break;
                      }
                }
            }
        if (random(1,4) == 1)
            {
            mynoise = random(1,3);
            Switch(mynoise) // Steel rooms.
                {
                Case 1:
                    {
                    thingsound(56,"Ambient12",48); // Chains
                    break;
                    }
                Case 2:
                    {
                    thingsound(56,"Ambient14",48); // Steel1
                    break;
                    }
                Case 3:
                    {
                    thingsound(56,"Ambient15",48); // Steel2
                    break;
                    }
                }
            }

        // Skull has been picked AND it's been more than 5 minutes.
        if (skulluse == 2)
            {
            if (timer() > (skulltime + 10500))
                {
                ACS_Execute(const:5,1,0,0,0); // Run script 5 to respawn skull.
                }
            }
        // Gem has been picked AND it's been more than 5 minutes.
        if (gemuse == 2)
            {
            if (timer() > (gemtime + 10500))
                {
                ACS_Execute(const:6,1,0,0,0); // Run script 5 to respawn gem.
                }
            }

        // Somebody scored. Release the next mage/fighter/cleric boss.
        if (bossrel == 1)
            {
            bossrel = 0;
            ACS_Execute(const:11,1,0,0,0); // Run release boss script.
            }

    if (gswin != 0) // 0-Nothing; 1-Gem wins; 2-Skull wins; 3-All done.
        {
        if (gswin <=2)
            {
            Switch(gswin)
                {
                Case 1:
                    {
                    gswin = 3; // Lock them out! Only one team gets their door open.
                    Print(s:"GEM TEAM WINS!");
                    Delay(3*35);
                    Print(s:"SKULL TEAM, BE PREPARED TO DIE...");
                    Door_Raise(31,200,0);
                    Delay(4*35);
                    Break;
                    }
                Case 2:
                    {
                    gswin = 3; // Lock them out! Only one team gets their door open.
                    Print(s:"SKULL TEAM WINS!");
                    Delay(3*35);
                    Print(s:"GEM TEAM, BE PREPARED TO DIE..");
                    Door_Raise(30,200,0);
                    Delay(4*35);
                    Break;
                    }
                }
            }
        }

        Switch(dscore) // Display scores.
            {
            Case 1:
                {
                dscore = 0;
                Print(s:"GEM TEAM SCORES!");
                Delay(3*35);
                Print(s:"SCORE IS NOW, ");
                Delay(3*35);
                Print(s:"SKULL TEAM: ",d:skullscore,s:" GEM TEAM: ",d:gemscore);
                Break;
                }
            Case 2:
                {
                dscore = 0;
                Print(s:"SKULL TEAM SCORES!");
                Delay(3*35);
                Print(s:"SCORE IS NOW, ");
                Delay(3*35);
                Print(s:"SKULL TEAM: ",d:skullscore,s:" GEM TEAM: ",d:gemscore);
                Break;
                }
            }

    }
}


//-------------------------------------------------------------------
// Gem team scores using skull
// Called from: usepuzzleitem
// Calls      : script 20
//
//  It looks like we will need 5 statues. Oh, well. How about
//  showing the item as used? Then the statue disappears!
//  Statue IDs 20,21,22,23,24
//-------------------------------------------------------------------

Script 2 (int stid)
{
    Thing_Activate(stid);
    Thing_Remove(stid);
    gemscore += 1;
    bossrel = 1;
    dscore = 1;
    ACS_Execute(5,1,0,0,0);

    if ((gemscore >= 5) && (gswin == 0))
        {
        gswin = 1;
        }
}

//-------------------------------------------------------------------
// Skull team scores using gem
// Called from: usepuzzleitem (linedef)
// Calls      : script 20
// See note in script 2.
//
// Gem Pedestal IDs: 25,26,27,28,29
//-------------------------------------------------------------------
Script 3 (int gmid)
{
    Thing_Activate(gmid);
    Thing_Remove(gmid);
    skullscore += 1;
    bossrel = 1;
    dscore = 2;
    ACS_Execute(6,1,0,0,0);

    if ((skullscore >= 5) && (gswin == 0))
       {
       gswin = 2;
       }
}
//-------------------------------------------------------------------
// Respawn skull. Player died, or skull was used.
// Called from: script 1
// Calls      : none
//-------------------------------------------------------------------
Script 5 (void)
{
    skulluse = 1;
    // Only spawn if there are no skulls.
    if (ThingCount(T_PUZZSKULL,0) == 0)
        {
        Thing_SpawnNoFog(skulloc,T_PUZZSKULL,0);
        }
}

//-------------------------------------------------------------------
// Respawn big gem. Player died, or gem was used.
// Called from: script 1
// Calls      : none
//-------------------------------------------------------------------
Script 6 (void)
{
    gemuse = 1;
    // Only spawn if there are no gems.
    IF (ThingCount(T_PUZZGEMBIG,0) == 0)
        {
        Thing_SpawnNoFog(gemloc,T_PUZZGEMBIG,0);
        }
}

//-------------------------------------------------------------------
// Pick up Big Gem
// Called from: thing special (when gem is picked up)
// Calls      : none
//-------------------------------------------------------------------
Script 8 (void)
{
    gemtime = timer();
    gemuse = 2;
    Thing_Remove(gemid);
}

//-------------------------------------------------------------------
// Pick up Skull
// Called from: thing special (when skull is picked up)
// Calls      : none
//-------------------------------------------------------------------
Script 9 (void)
{
    skulltime = timer();
    skulluse = 2;
    Thing_Remove(skullid);
}

//-------------------------------------------------------------------
// Release the dogs! Will open the door and wake up the boss.
// Called from: 1
// Calls      : none
//-------------------------------------------------------------------

Script 11 (void)
{
    if (monstdoor <= 3) // If you want more bosses, YOU put them in!
        {
        // Even with thing_activate, I think the boss needs a sound to
        // actually start running around.
        Thing_Activate(6+monstdoor); // 6 is the base TID for boss monsters.
        Door_Open(monstdoor,200);
        monstdoor += 1;
        }
}

//-------------------------------------------------------------------
// Random item/monster generator.
// Will randomly generate things. Biased of course, towards monsters.
// Can only do this once every 2 minutes.
//
// Called from: Line special (switch)
// Calls      : none
//-------------------------------------------------------------------
Script 15 (void)
{
    int gentype, whichone;

    if (timer() >= gentime )
        {
        gentime = Timer() + 4200; // 2 minutes.
        // Monster or thing? 1-13 is monster.
        if (Random(1,20) <= 13)
            {
            // Big, mean, nasty, or just another monster.
            if (Random(1,20) <= 15)
                {
                // Normal.
                whichone = Random(1,6);
                Switch(whichone)
                    {
                    case 1:
                        gentype = T_CENTAUR;
                        break;
                    case 2:
                        gentype = T_DEMON;
                        break;
                    case 3:
                        gentype = T_ETTIN;
                        break;
                    case 4:
                        gentype = T_FIREGARGOYLE;
                        break;
                    case 5:
                        gentype = T_BISHOP;
                        break;
                    case 6:
                        gentype = T_ICEGOLEM;
                    }

                }
            else // Nasty monster.
                {
                whichone = Random(1,2);
                Switch (whichone)
                    {
                    case 1:
                        gentype = T_CENTAURLEADER;
                        break;
                    case 2:
                        gentype = T_WRAITH;
                    }
                }
            }
        else // Their luck held. Spawn item.
            {
            whichone = Random(1,20);
            Switch (whichone)
                {
                case 1:
                    gentype = T_MANA1;
                    break;
                case 2:
                    gentype = T_MANA2;
                    break;
                case 3:
                    gentype = T_ITEMBOOTS;
                    break;
                case 4:
                    gentype = T_ITEMEGG;
                    break;
                case 5:
                    gentype = T_ITEMFLIGHT;
                    break;
                case 6:
                    gentype = T_ITEMSUMMON;
                    break;
                case 7:
                    gentype = T_ITEMTPORTOTHER;
                    break;
                case 8:
                    gentype = T_ITEMTELEPORT;
                    break;
                case 9:
                    gentype = T_DRAGONSKINBRACERS;
                    break;
                case 10:
                    gentype = T_ITEMHEALTHPOTION;
                    break;
                case 11:
                    gentype = T_ITEMHEALTHFLASK;
                    break;
                case 12:
                    gentype = T_ITEMHEALTHFULL;
                    break;
                case 13:
                    gentype = T_ITEMBOOSTMANA;
                    break;
                case 14:
                    gentype = T_MESHARMOR;
                    break;
                case 15:
                    gentype = T_FALCONSHIELD;
                    break;
                case 16:
                    gentype = T_PLATINUMHELM;
                    break;
                case 15:
                    gentype = T_AMULETOFWARDING;
                    break;
                case 17:
                    gentype = T_ITEMFLECHETTE;
                    break;
                case 18:
                    gentype = T_ITEMTORCH;
                    break;
                case 19:
                    gentype = T_ITEMREPULSION;
                    break;
                case 20:
                    gentype = T_MANA3;
                }
            }
        Thing_Spawn(genloc,gentype,0);
        }
        else
            {
            timeann = (gentime - Timer())/35; // Should give seconds.
            Print(s:"YOU NEED TO WAIT ",d:timeann,s:" SECONDS");
            }
}

