/**************************************************************************************************
 * Program:		New Miracles for Black & White 2
 * Purpose:		Forest.txt contains all the scripts needed for a miracle which creates fireworks to
 * 					impress near by villagers
 * Author:		Bill
 * Version: 	0
 * Date: 		Feb 2 2020
**************************************************************************************************/
//-------------------------------------------Scripts-----------------------------------------------
define script NMsLight_createSymbolicBubble(idx, xPos, yPos, zPos, t_idx)
define script NMsLight_createMiracleInHand
define script NMsLight_hideSymbolicBubble(m_idx)
define script NMsLight_CastPour(pwr,idx)
define script NMsLight_CastThrown(pwr,idx)
define script NMsLight_deleteMiracleInHand(idx)
define script NMsLight_calculateMana(m_idx,pwr)
define script NMsLight_Source(amt,pos,tmr)
//------------------------------------------Constants----------------------------------------------
define NMsLight_MAXACTIVE			= 20
//-------------------------------------------Globals-----------------------------------------------
global NMsLight_Count 				= 0

global NMsLight_Hand[NMsLight_MAXACTIVE]
global NMsLight_HandV1[NMsLight_MAXACTIVE]
global NMsLight_HandV2[NMsLight_MAXACTIVE]
global NMsLight_HandActive[NMsLight_MAXACTIVE]

global NMsLight_HandLock			= NMs_FALSE		//Lock variable for NMsLight_createMiracleInHand
global NMsLight_HandResult			= 0				//Result variable for NMsLight_createMiracleInHand

global NMsLight_Casting 			= NMs_FALSE
global NMsLight_Pile 				= 0

//--------------------------------------------Code-------------------------------------------------
begin script NMsLight_createSymbolicBubble(idx, xPos, yPos, zPos, t_idx)
	calIdx = idx * NMs_MIR2D
	visHeight = 0
start
	visHeight = yPos + land height at {xPos,zPos}
	NMs_Miracles[calIdx + NMs_MIRTOTEM] = t_idx
	//Symbol Creation
	NMs_Miracles[calIdx + NMs_MIRSYMBOL] = create with angle 0 and scale 0.8 SCRIPT_OBJECT_TYPE_FEATURE FEATURE_INFO_AZTC_SUNTEMPLE at {xPos,yPos,zPos}+{0,1,0}
	override mesh for NMs_Miracles[calIdx + NMs_MIRSYMBOL] with "..\models\m_decayskull" //m_greek_tf_urnshrapnel
	SCRIPT_OBJECT_PROPERTY_TYPE_YPOS of NMs_Miracles[calIdx + NMs_MIRSYMBOL] = yPos + 1
	set NMs_Miracles[calIdx + NMs_MIRSYMBOL] colour red 100 green 100 blue 10
	//Bubble Creation
	NMs_Miracles[calIdx + NMs_MIROBJ1] = create visual effect VISUAL_MIRACLE_HEAL_SEED at {xPos,visHeight,zPos}+{0,1,0} time -1	
	NMs_Miracles[calIdx + NMs_MIROBJ2] = create visual effect VISUAL_EFFECT_BEAUTY_GLOW at {xPos,visHeight,zPos}+{0,1,0} time -1	
	//Scaling and Colouring
	SCRIPT_OBJECT_PROPERTY_TYPE_SCALE of NMs_Miracles[calIdx + NMs_MIROBJ2] = 1.2
	//SCRIPT_OBJECT_PROPERTY_TYPE_STRENGTH of NMs_Miracles[calIdx + NMs_MIROBJ2] = 0.5
	set NMs_Miracles[calIdx + NMs_MIROBJ1] colour red 0 green 183 blue 18
	set NMs_Miracles[calIdx + NMs_MIROBJ2] colour red 164 green 7 blue 255
	
	//Interaction commands
	disable NMs_Miracles[calIdx + NMs_MIRSYMBOL] pickup
	disable NMs_Miracles[calIdx + NMs_MIRSYMBOL] set on fire
	disable NMs_Miracles[calIdx + NMs_MIRSYMBOL] moveable
	
	enable NMs_Miracles[calIdx + NMs_MIRSYMBOL] interactable
	enable NMs_Miracles[calIdx + NMs_MIRSYMBOL] cylindar override
	
	//Other initialization
	NMs_Miracles[calIdx + NMs_VISIBLE] = NMs_TRUE
	NMs_Miracles[calIdx + NMs_MIRTYPE] = NMs_LIGHT
	
end script NMsLight_createSymbolicBubble

//Create the miracle in the player's hand
begin script NMsLight_createMiracleInHand
	idx = -1
	cnt = 0
start
	while((cnt < NMsLight_MAXACTIVE) and (idx == -1))
		if(NMsLight_HandActive[cnt] == NMs_FALSE)
			idx = cnt
		end if
		cnt++
	end while
	
	NMsLight_HandResult = idx
	NMsLight_HandActive[idx] = NMs_TRUE
	
	//Symbol Creation
	NMsLight_Hand[idx] = create with angle 0 and scale 0.8 SCRIPT_OBJECT_TYPE_MOBILE_STATIC MOBILE_STATC_HAY_BALE at hand position
	override mesh for NMsLight_Hand[idx] with "..\models\m_decayskull"
	set NMsLight_Hand[idx] colour red 100 green 100 blue 10
	//Bubble Creation
	NMsLight_HandV1[idx] = create visual effect VISUAL_MIRACLE_HEAL_IN_HAND on NMsLight_Hand[idx] time -1	
	NMsLight_HandV2[idx] = create visual effect VISUAL_EFFECT_BEAUTY_GLOW on NMsLight_Hand[idx] time -1	
	set NMsLight_Hand[idx] in player 0 hand
	//Scaling and Colouring
	SCRIPT_OBJECT_PROPERTY_TYPE_SCALE of NMsLight_HandV2[idx] = 1.2
	set NMsLight_HandV1[idx] colour red 0 green 183 blue 18
	set NMsLight_HandV2[idx] colour red 164 green 7 blue 255
	
end script NMsLight_createMiracleInHand

//Deletes the miracle in the player's hand
begin script NMsLight_deleteMiracleInHand(idx)

start
	NMsLight_HandActive[idx] = NMs_FALSE
	delete NMsLight_Hand[idx]
	stop visual effect NMsLight_HandV1[idx]
	stop visual effect NMsLight_HandV2[idx]
end script NMsLight_deleteMiracleInHand

//Hides the symbolic representation from the totem
begin script NMsLight_hideSymbolicBubble(m_idx)
	m_ele = m_idx
	vis1 = 0
start
	delete NMs_Miracles[m_ele + NMs_MIRSYMBOL] with fade
	stop visual effect NMs_Miracles[m_ele + NMs_MIROBJ1]
	stop visual effect NMs_Miracles[m_ele + NMs_MIROBJ2]
end script NMsLight_hideSymbolicBubble

//Calculates the mana required for this miracle, depending on the power passed in
begin script NMsLight_calculateMana(m_idx,pwr)
	m_ele = m_idx * NMs_MIR2D
	m_mana = 0
start
	m_mana = 750 * pwr
	if(m_mana < 750) //Minimum
		m_mana = 750
	end if
	NMs_Miracles[m_ele + NMs_MANA] = m_mana
end script NMsLight_calculateMana

//Script for pouring the miracle, NMsLight_Casting is set false upon completion of the cast
//	Either by the amount of wheat is reached or the cancel is hit
begin script NMsLight_CastPour(pwr, idx)
	amt = 0
	v_Pour = 0
	t_Amount = 0
	currentAmt = 0
	t_PerTick = 0
	
	pos = 0
	typ = 0
	
	v_dr = 0
	v_f = 0
	gd = 0
	obj = 0
	ftyp = 0
	
	ti_Tmr = create timer for 0 seconds
start
	NMsLight_Casting = NMs_TRUE
	if(NMsLight_HandActive[idx] == NMs_TRUE)
		set NMsLight_Hand[idx] in player 0 hand
		
		//Calculate
		t_Amount = 5 * pwr
		if(t_Amount < 5)
			t_Amount = 5
		end if
		t_PerTick = 1
		
		pos = marker at {NMsLight_Hand[idx]}

		set ti_Tmr time to 0.3 seconds
		
		gd = get player 0 alignment
		
		begin loop		
			set NMsLight_Hand[idx] in player 0 hand
			if((get ti_Tmr time remaining == 0) and (bindable action BINDABLE_ACTION_TYPE_ACTION performed) and (get player 0 influence at hand position > 0.1))				
				amt += t_PerTick
				
				pos = marker at {NMsLight_Hand[idx]}
				
				obj = create with angle 0 and scale 0.01 SCRIPT_OBJECT_TYPE_MOBILE_STATIC MOBILE_STATC_HAY_BALE at {pos}+{0,100,0}
				override mesh for obj with "..\models\m_decayskull"
				set obj colour red 100 green 100 blue 10
				
				attach reaction obj REACTION_LOOK_AT_NICE_SPELL
				
				v_dr = create visual effect VISUAL_EFFECT_DRUM_BEAT at {pos} time 3
				SCRIPT_OBJECT_PROPERTY_TYPE_SCALE of v_dr = 2
				ftyp = number from 0 to 7
				if(ftyp == 0)
					v_f = create visual effect VISUAL_EFFECT_FIREWORK_1 at {pos} time 5
					SCRIPT_OBJECT_PROPERTY_TYPE_STRENGTH of v_f = 5
					play string sound "SCRIPT27_challengedrum4"
				elsif(ftyp == 1)
					v_f = create visual effect VISUAL_EFFECT_FIREWORK_2 at {pos} time 5
					SCRIPT_OBJECT_PROPERTY_TYPE_STRENGTH of v_f = 5
					play string sound "SCRIPT27_challengedrum5"
				elsif(ftyp == 2)
					v_f = create visual effect VISUAL_EFFECT_FIREWORK_3 at {pos} time 5
					SCRIPT_OBJECT_PROPERTY_TYPE_STRENGTH of v_f = 5
					play string sound "SCRIPT27_challengedrum2"
				elsif(ftyp == 3)
					v_f = create visual effect VISUAL_EFFECT_ICEDROPPER at {pos} time 5
					SCRIPT_OBJECT_PROPERTY_TYPE_SCALE of v_f = 1
					play string sound "SCRIPT27_challengedrum4"
				elsif(ftyp == 4)
					v_f = create visual effect VISUAL_EFFECT_LIGHTNING_VILLAGER_EXPLODE at {pos} time 5
					SCRIPT_OBJECT_PROPERTY_TYPE_SCALE of v_f = 2
					play string sound "SCRIPT27_challengedrum5"
				elsif(ftyp == 5)
					v_f = create visual effect VISUAL_EFFECT_CREATURE_KNOCKED_OUT at {pos} time 7
					SCRIPT_OBJECT_PROPERTY_TYPE_SCALE of v_f = 2
					play string sound "SCRIPT27_challengedrum4"
				elsif(ftyp == 6)
					v_f = create visual effect VISUAL_EFFECT_CREATURE_LOVE at {pos} time 7
					SCRIPT_OBJECT_PROPERTY_TYPE_SCALE of v_f = 2
					play string sound "SCRIPT27_challengedrum5"
				elsif(ftyp == 7)
					v_f = create visual effect VISUAL_EFFECT_FLAME_THROWER_HAND at {pos} time 5
					SCRIPT_OBJECT_PROPERTY_TYPE_SCALE of v_f = 8
					play string sound "SCRIPT27_challengedrum4"
				end if
				
				set player 0 alignment (gd + 0.0001)
				
				delete obj
				
				set ti_Tmr time to 0.1 seconds
			end if
			until ((amt >= t_Amount) or (bindable action BINDABLE_ACTION_TYPE_CANCEL_CURRENT_ACTION performed))
		end loop
		
		stop visual effect v_Pour
		run script NMsLight_deleteMiracleInHand(idx)
	end if
	
	NMsLight_Casting = NMs_FALSE
end script NMsLight_CastPour

//Script for throwing the miracle, NMsLight_Casting is set false upon completion of the cast
//TODO: Edit to split the source into multiple spots depending on the power level
begin script NMsLight_CastThrown(pwr, idx)
	t_Amount = 0
	currentAmt = 0
	ti_Tmr = create timer for 60 seconds
	effect_go = NMs_TRUE
	pos = 0	//Position of the miracle when it hits something
	obj = 0
	cnt = 0
	v_sp = 0
	v_gl = 0
	v_dr = 0
	v_f = 0
	typ = 0
	t_obj = 0
	rad = 0
	ang = 0
	mk = 0
	posX = 0
	posZ = 0
	rd = 0
start
	NMsLight_Casting = NMs_TRUE
	if(NMsLight_HandActive[idx] == NMs_TRUE)
		//Calculate
		t_Amount = pwr
		if(t_Amount < 1)
			t_Amount = 1
		end if
		//if(t_Amount > 10)
		//	t_Amount = 10
		//end if
		rad = (1.1 * t_Amount) + 10
		if(rad > 20)
			rad = 20
		end if
		rd = 1
		ang = 0
		
		//Enable physics tracking
		enable NMsLight_Hand[idx] physics tracking
		
		//Wait until something happens, or the time limit is hit
		wait until ((NMsLight_Hand[idx] hit) or (NMsLight_Hand[idx] hit land) or (get ti_Tmr time remaining == 0) or (not NMsLight_Hand[idx] is moving))
		
		//If the time limit is reached, then something has gone terribly wrong! Exit at once!
		if(get ti_Tmr time remaining == 0)
			effect_go = NMs_FALSE
		end if
		
		pos = marker at {NMsLight_Hand[idx]}
		
		//Remove the miracle in hand representation
		run script NMsLight_deleteMiracleInHand(idx)
		
		//TODO: We also need to get the villegers to react to the lights
		
		if(effect_go == NMs_TRUE)
			//Calculate positions	
			while(cnt < t_Amount)				
				posX = SCRIPT_OBJECT_PROPERTY_TYPE_XPOS of pos + ((rad) / NMs_MATHTHING) * cos(ang)
				posZ = SCRIPT_OBJECT_PROPERTY_TYPE_ZPOS of pos + ((rad) / NMs_MATHTHING) * sin(ang)
				ang += (36 / rd)
				if(ang > 360)
					ang = 0
					rd++
					rad += 10
				end if
				
				mk = marker at {posX, land height at {posX,posZ},posZ}
				run background script NMsLight_Source(t_Amount + 5,mk,0.5)
				wait 0.2 seconds
				cnt++
			end while
			
		end if
	end if
	NMsLight_Casting = NMs_FALSE
end script NMsLight_CastThrown

begin script NMsLight_Source(amt,pos,tmr)
	v_sp = 0 	//Sparkle
	v_gl = 0 	//Glow
	v_dr = 0	//Drum
	ftyp = 0 	//Firework type
	v_f = 0 	//firework
	v_g = 0
	cnt = 0
	obj = 0
	der = 0
	gd = 0
start
	v_sp = create visual effect VISUAL_EFFECT_ORE_SPARKLE at {pos} time -1
	v_gl = create visual effect VISUAL_EFFECT_BEAUTY_GLOW at {pos} time -1
	
	obj = create with angle 0 and scale 0.01 SCRIPT_OBJECT_TYPE_MOBILE_STATIC MOBILE_STATC_HAY_BALE at {pos}+{0,100,0}
	override mesh for obj with "..\models\m_decayskull"
	set obj colour red 100 green 100 blue 10
	
	attach reaction obj REACTION_LOOK_AT_NICE_SPELL
	
	SCRIPT_OBJECT_PROPERTY_TYPE_SCALE of v_sp = 3
	SCRIPT_OBJECT_PROPERTY_TYPE_SCALE of v_gl = 3
	
	gd = get player 0 alignment
	
	while(cnt < amt)
		
		v_dr = create visual effect VISUAL_EFFECT_DRUM_BEAT at {pos} time 3
		SCRIPT_OBJECT_PROPERTY_TYPE_SCALE of v_dr = 2
		ftyp = number from 0 to 7
		if(ftyp == 0)
			v_f = create visual effect VISUAL_EFFECT_FIREWORK_1 at {pos} time 5
			SCRIPT_OBJECT_PROPERTY_TYPE_STRENGTH of v_f = 5
			play string sound "SCRIPT27_challengedrum4"
		elsif(ftyp == 1)
			v_f = create visual effect VISUAL_EFFECT_FIREWORK_2 at {pos} time 5
			SCRIPT_OBJECT_PROPERTY_TYPE_STRENGTH of v_f = 5
			play string sound "SCRIPT27_challengedrum5"
		elsif(ftyp == 2)
			v_f = create visual effect VISUAL_EFFECT_FIREWORK_3 at {pos} time 5
			SCRIPT_OBJECT_PROPERTY_TYPE_STRENGTH of v_f = 5
			play string sound "SCRIPT27_challengedrum2"
		elsif(ftyp == 3)
			v_f = create visual effect VISUAL_EFFECT_ICEDROPPER at {pos} time 5
			SCRIPT_OBJECT_PROPERTY_TYPE_SCALE of v_f = 1
			play string sound "SCRIPT27_challengedrum4"
		elsif(ftyp == 4)
			v_f = create visual effect VISUAL_EFFECT_LIGHTNING_VILLAGER_EXPLODE at {pos} time 5
			SCRIPT_OBJECT_PROPERTY_TYPE_SCALE of v_f = 2
			play string sound "SCRIPT27_challengedrum5"
		elsif(ftyp == 5)
			v_f = create visual effect VISUAL_EFFECT_CREATURE_KNOCKED_OUT at {pos} time 7
			SCRIPT_OBJECT_PROPERTY_TYPE_SCALE of v_f = 2
			play string sound "SCRIPT27_challengedrum4"
		elsif(ftyp == 6)
			v_f = create visual effect VISUAL_EFFECT_CREATURE_LOVE at {pos} time 7
			SCRIPT_OBJECT_PROPERTY_TYPE_SCALE of v_f = 2
			play string sound "SCRIPT27_challengedrum5"
		elsif(ftyp == 7)
			v_f = create visual effect VISUAL_EFFECT_FLAME_THROWER_HAND at {pos} time 5
			SCRIPT_OBJECT_PROPERTY_TYPE_SCALE of v_f = 8
			play string sound "SCRIPT27_challengedrum4"
		end if
		
		
		
		set player 0 alignment (gd + 0.0001)
		
		wait tmr seconds
		cnt++
	end while

	stop visual effect v_sp
	stop visual effect v_gl
	
	wait 5 seconds
	delete obj
	
end script NMsLight_Source
