//::///////////////////////////////////////////////
//:: Rod of Wonder - On activated script
//::///////////////////////////////////////////////
/*

	On activation script for the rod of wonder.
	i_2d2f_rod_of_wonder_ac.nss

*/
//::///////////////////////////////////////////////
//:: Created By: 2DruNk2Frag
//:: Created On: 06-15-2008
//:: Last Update: 06-15-2008
//::///////////////////////////////////////////////


#include "2d2f_inc_magic_wild"

int GetRandomSurge();

void doSurgeNoSpellWonder(object oCaster, object oTarget);

void main()
{
	object oCaster = GetItemActivator();
	object oTarget = GetItemActivatedTarget();
	
	if(GetHasFeat(FEAT_CHAOTIC_MIND,oCaster) == TRUE)
	{
		int nRand1 = GetRandomSurge();
    	int nRand2 = GetRandomSurge();
    
    	string sSurgeName1 = GetSurgeName(nRand1);
    	string sSurgeName2 = GetSurgeName(nRand2);
    
    	SetLocalInt(oCaster,"RodOfWonderOkButton",nRand1);
    	SetLocalInt(oCaster,"RodOfWonderCancelButton",nRand2);
		SetLocalObject(oCaster,"RodOfWonderTarget",oTarget);
		
		DisplayMessageBox(  oCaster, // oPC   	- The player object of the player to show this message box to
                    	0,// nMessageStrRef- The STRREF for the Message Box message.
                    	"<color=gold><b>Rod of Wonder</b></color>\n\n"
                        + "Choose an Effect.\n\n"
                        + "1. - " + sSurgeName1 + "\n\n"
                        + "2. - " + sSurgeName2,// sMessage      - The text to display in the message box. Overrides anything indicated by the nMessageStrRef
                    	"gui_2d2f_rod_wonder_ok",// sOkCB         - The callback script to call if the user clicks OK, defaults to none. The script name MUST start with 'gui'
                    	"gui_2d2f_rod_wonder_cancel",// sCancelCB     - The callback script to call if the user clicks Cancel, defaultsto none. The script name MUST start with 'gui'
                    	TRUE,// bShowCancel   - If TRUE, Cancel Button will appear on the message box.
                    	"SCREEN_MESSAGEBOX_DEFAULT",// sScreenName   - The GUI SCREEN NAME to use in place of the default message box. The default is SCREEN_STRINGINPUT_MESSAGEBOX
                    	0,// nOkStrRef     - The STRREF to display in the OK button, defaults to OK
                    	"1",// sOkString     - The string to show in the OK button. Overrides anything that nOkStrRef indicates if it is not an empty string
                    	0,// nCancelStrRef - The STRREF to dispaly in the Cancel button, defaults to Cancel.
                    	"2"// sCancelString - The string to display in the Cancel button. Overrides anything that nCancelStrRef indicates if it is anything besides empty string
                   	    );
	
	}
	else
	{
		int nSurgeIndex = GetRandomSurge();
		SetGlobalInt("next_surge_index",nSurgeIndex);
		doSurgeNoSpellWonder(oCaster,oTarget);
	
	}

}

int GetRandomSurge()
{
	int nSurgeIndex;
	int nRand = Random(78);

	switch(nRand)
	{
		case	0	: nSurgeIndex =	1	; break;  //	Wall of Force Appears in front of caster
		case	1	: nSurgeIndex =	2	; break;  //	Caster smells like a skunk for (caster level) rounds
		case	2	: nSurgeIndex =	3	; break;  //	Summons various animals
		case	3	: nSurgeIndex =	4	; break;  //	Caster's clothes itch for 4d6 rounds
		case	4	: nSurgeIndex =	5	; break;  //	Light on caster
		case	5	: nSurgeIndex =	9	; break;  //	Everyone turns around
		case	6	: nSurgeIndex =	11	; break;  //	Caster becomes allergic to magic items
		case	7	: nSurgeIndex =	12	; break;  //	Gold on caster is destroyed
		case	8	: nSurgeIndex =	13	; break;  //	Caster “reduced”
		case	9	: nSurgeIndex =	14	; break;  //	Caster falls madly in love with target until dispel magic or remove curse is cast
		case	10	: nSurgeIndex =	15	; break;  //	Everyone around you is knocked down
		case	11	: nSurgeIndex =	16	; break;  //	Caster polymorphed into a random form
		case	12	: nSurgeIndex =	17	; break;  //	Colorful bubbles come out of caster's mouth for one turn
		case	13	: nSurgeIndex =	18	; break;  //	A herd of cats appear
		case	14	: nSurgeIndex =	19	; break;  //	Caster surrounded by flames
		case	15	: nSurgeIndex =	20	; break;  //	Caster's feet enlarge
		case	16	: nSurgeIndex =	22	; break;  //	Caster levitates for 3d6 rounds
		case	17	: nSurgeIndex =	23	; break;  //	Fear on Caster
		case	18	: nSurgeIndex =	24	; break;  //	Caster becomes two-dimensional for 3-10 rounds
		case	19	: nSurgeIndex =	25	; break;  //	Caster gains a phobia of random creature type for 1d4 days
		case	20	: nSurgeIndex =	26	; break;  //	Spells in effect around caster explode violently
		case	21	: nSurgeIndex =	27	; break;  //	Silence 15' on caster
		case	22	: nSurgeIndex =	28	; break;  //	Time Stop, with turns
		case	23	: nSurgeIndex =	30	; break;  //	Fireworks erupt over caster's head
		case	24	: nSurgeIndex =	32	; break;  //	Caster becomes invisible
		case	25	: nSurgeIndex =	33	; break;  //	Color Spray
		case	26	: nSurgeIndex =	34	; break;  //	Butterflies pour from caster's mouth
		case	27	: nSurgeIndex =	35	; break;  //	Gold is created on the target
		case	28	: nSurgeIndex =	36	; break;  //	Gems shoot from caster's fingertips
		case	29	: nSurgeIndex =	37	; break;  //	Music fills the air
		case	30	: nSurgeIndex =	38	; break;  //	Create Food and Water
		case	31	: nSurgeIndex =	42	; break;  //	All nearby weapons temporarily enchanted
		case	32	: nSurgeIndex =	43	; break;  //	Smoke pours from the ears of nearby creatures
		case	33	: nSurgeIndex =	44	; break;  //	Shimmering Lights
		case	34	: nSurgeIndex =	45	; break;  //	Everyone within 30' of caster begins to hiccup
		case	35	: nSurgeIndex =	46	; break;  //	All normal, secret, and magic doors open
		case	36	: nSurgeIndex =	47	; break;  //	Caster and target exchange places
		case	37	: nSurgeIndex =	49	; break;  //	Spell fails but caster is refreshed
		case	38	: nSurgeIndex =	50	; break;  //	Random allies are summoned
		case	39	: nSurgeIndex =	51	; break;  //	Weather changes abruptly
		case	40	: nSurgeIndex =	52	; break;  //	Deafening bang affects all within 60'
		case	41	: nSurgeIndex =	53	; break;  //	Random spell is cast at random target
		case	42	: nSurgeIndex =	54	; break;  //	Gate opens and a creature emerges
		case	43	: nSurgeIndex =	59	; break;  //	All nearby weapons glow for 1d4 rounds
		case	44	: nSurgeIndex =	62	; break;  //	All nearby magic items begin to glow
		case	45	: nSurgeIndex =	64	; break;  //	Target slowed
		case	46	: nSurgeIndex =	65	; break;  //	All spells in effect around target explode violently
		case	47	: nSurgeIndex =	66	; break;  //	Lightning Bolt cast at target
		case	48	: nSurgeIndex =	67	; break;  //	Target Enlarged
		case	49	: nSurgeIndex =	68	; break;  //	Darkness on target
		case	50	: nSurgeIndex =	69	; break;  //	Plant Growth on target
		case	51	: nSurgeIndex =	70	; break;  //	1d4 clones of target are created for one turn
		case	52	: nSurgeIndex =	71	; break;  //	Fireball on target
		case	53	: nSurgeIndex =	72	; break;  //	Target turned to stone
		case	54	: nSurgeIndex =	74	; break;  //	Everyone within 10' of caster Healed
		case	55	: nSurgeIndex =	75	; break;  //	Target becomes dizzy for 2d4 rounds
		case	56	: nSurgeIndex =	76	; break;  //	Target surrounded by flames
		case	57	: nSurgeIndex =	77	; break;  //	Target levitates for 3d6 rounds
		case	58	: nSurgeIndex =	78	; break;  //	Target struck blind for one round per caster level
		case	59	: nSurgeIndex =	79	; break;  //	Charm Monster on target
		case	60	: nSurgeIndex =	80	; break;  //	Every creature within 60' of target becomes drunk for 4-10 rounds
		case	61	: nSurgeIndex =	81	; break;  //	Target's feet enlarge
		case	62	: nSurgeIndex =	82	; break;  //	Rust Monster appears
		case	63	: nSurgeIndex =	83	; break;  //	Target polymorphs into a random form
		case	64	: nSurgeIndex =	84	; break;  //	Target falls madly in love with caster until dispel magic or remove curse is cast
		case	65	: nSurgeIndex =	85	; break;  //	Target or caster changes sex
		case	66	: nSurgeIndex =	86	; break;  //	Small black raincloud forms over target
		case	67	: nSurgeIndex =	87	; break;  //	Stinking Cloud on target
		case	68	: nSurgeIndex =	88	; break;  //	Heavy object falls on target
		case	69	: nSurgeIndex =	89	; break;  //	Target begins sneezing
		case	70	: nSurgeIndex =	91	; break;  //	Target's clothes itch for 4d6 rounds
		case	71	: nSurgeIndex =	92	; break;  //	Target or caster's race randomly changes
		case	72	: nSurgeIndex =	93	; break;  //	Target becomes Ethereal
		case	73	: nSurgeIndex =	94	; break;  //	Target Hasted
		case	74	: nSurgeIndex =	95	; break;  //	All cloth on target crumbles to dust
		case	75	: nSurgeIndex =	96	; break;  //	Target sprouts leaves
		case	76	: nSurgeIndex =	97	; break;  //	Target grows a new but useless appendage
		case	77	: nSurgeIndex =	98	; break;  //	Target's skin changes color
	}

	return nSurgeIndex;
}

void doSurgeNoSpellWonder(object oCaster, object oTarget)
{
	object oPC = GetFirstPC(TRUE);
	
	int nSpellId;
	
	SetLocalInt(oCaster,"nDoSurgeNoSpell", 1);
	
	DelayCommand(1.0,SetLocalInt(oCaster,"nDoSurgeNoSpell", 0));
	DelayCommand(1.0,DeleteLocalInt(oCaster,"RodOfWonderOkButton"));
    DelayCommand(1.0,DeleteLocalInt(oCaster,"RodOfWonderCancelButton"));
	DelayCommand(1.0,DeleteLocalObject(oCaster,"RodOfWonderTarget"));
		
	// hostile										
	if(spellsIsTarget(oTarget,SPELL_TARGET_ALLALLIES,oCaster) == FALSE)
		{
		nSpellId = SPELL_RANDOM_SPELL_UTILITY_HOSTILE;
	}
	else // friendly
	{
		nSpellId = SPELL_RANDOM_SPELL_UTILITY_FRIENDLY;
	}
	
	//:debug
	//SendMessageToPC(oPC,"doSurge nospell spell id = " + IntToString(nSpellId));
	//SendMessageToPC(oPC,"doSurge nospell caster is = " + GetName(oCaster));
	//SendMessageToPC(oPC,"doSurge nospell target is = " + GetName(oTarget));
	
	// Shazbotian: added this here to fix a bug where glyph of wild magic would modifiy your dweomer selection without your knowledge
	//int iOriginalSpellId = GetLocalInt(oCaster,"nSpellId");

	SetLocalInt(oCaster,"nDoSurgeNoSpellSpellId",nSpellId);
	
	// Shazbotian: the idea here is to restore the original selected dweomer spell after the surge insanity is over
	//DelayCommand(2.0f, SetLocalInt(oCaster,"nSpellId",iOriginalSpellId));
		
	//:2d2f this code is to fix bug, apparently objects can't cast when walking... go fig
	// ... it took me 3 days to figure that out... ouch!
	effect eImmob = EffectCutsceneImmobilize();
	JXApplyEffectToObject(DURATION_TYPE_TEMPORARY,eImmob,oCaster,0.5);
	AssignCommand(oCaster, ClearAllActions());
			
	JXCastSpellFromObjectAtObject(oCaster,
								oTarget,
								nSpellId,
								METAMAGIC_NONE,
								JXGetCasterLevel(oCaster),
								JXGetSpellSaveDC(oCaster),
								TRUE);
}