/*
Draw a card from the deck of many things
*/

// This library holds all of the functions for the
// individual cards.

// Updated 9_11_08 by 2d2f

#include "x0_i0_deckmany"

// Run the multiple-card draw
void DoDeck(object oCaster);

void main()
{
	object oCaster = GetPCSpeaker();
	int nDeckDraws = GetLocalInt(oCaster,"nDeckDraws");
	SetLocalInt(oCaster,"nDeckDraws",nDeckDraws + 1);
	
	// This is the person using the deck
    
    // This is the number of cards being drawn
	
	
	// 2d2f - I'm going to only allow 1 card for now
    int nCards = 1;//JXGetCasterLevel(OBJECT_SELF);
    //DBG_msg("Cards to draw: " + IntToString(nCards));

    // Create an invisible object to actually run the draws
    object oDeck = GetLocalObject(oCaster,"oDM");//CreateObject(OBJECT_TYPE_PLACEABLE,
                                //"plc_ipoint",
                                //GetLocation(oCaster));
			
	

    SetNumberDeckDraws(oCaster, nCards);
    AssignCommand(oDeck, DoDeck(oCaster));

    // Clean up the deck object
    DestroyObject(oDeck, 3.0);
}
	



void DoDeck(object oCaster)
{
    // Randomly start with a positive or negative card
    int bNegative = Random(2);

    // Keep track of how many we've drawn so far
    // so we can delay effects
    int nTurn = 0;

    // No interrupting the deck!
    int nDrawsLeft = GetNumberDeckDraws(oCaster);

    while (nDrawsLeft > 0) {
        SetNumberDeckDraws(oCaster, nDrawsLeft-1);
        if (bNegative) {			
            DoDeckDrawNegative(oCaster, nTurn);
        } else {
		    DoDeckDrawPositive(oCaster, nTurn);
        }

        // We use this just to space out the card draws
        nTurn++;
		
        // Flip the negative setting so the next card will
        // be the opposite type
        bNegative = (bNegative + 1) % 2;

        // We have to get this off the caster again because it may have
        // been modified by the card drawn
        nDrawsLeft = GetNumberDeckDraws(oCaster);
    }
}