/**************************************************************************************************
 * Program:		New Miracles for Black & White 2
 * Purpose:		TEMPLATE.txt contains all the basics for the scipts needed to create a miracle
 * Author:		Base code by Bill, Miracle effects and design by <Your Username>
 * Version: 	0
 * Date: 		--
**************************************************************************************************/
//-------------------------------------------Scripts-----------------------------------------------
define script NMsTemp_createSymbolicBubble(idx, xPos, yPos, zPos, t_idx)
define script NMsTemp_createMiracleInHand
define script NMsTemp_hideSymbolicBubble(m_idx)
define script NMsTemp_CastPour(pwr,idx)
define script NMsTemp_CastThrown(pwr,idx)
define script NMsTemp_deleteMiracleInHand(idx)
define script NMsTemp_calculateMana(m_idx,pwr)
define script NMsTemp_CreateTree(obj)
//------------------------------------------Constants----------------------------------------------
define NMsTEMP_MAXACTIVE			= 20
//-------------------------------------------Globals-----------------------------------------------
global NMsTemp_Count 				= 0

global NMsTemp_Hand[NMsTEMP_MAXACTIVE]
global NMsTemp_HandV1[NMsTEMP_MAXACTIVE]
global NMsTemp_HandV2[NMsTEMP_MAXACTIVE]
global NMsTemp_HandActive[NMsTEMP_MAXACTIVE]

global NMsTemp_HandLock				= NMs_FALSE		//Lock variable for NMsTemp_createMiracleInHand
global NMsTemp_HandResult			= 0				//Result variable for NMsTemp_createMiracleInHand

global NMsTemp_Casting 				= NMs_FALSE
global NMsTemp_Pile 				= 0

//--------------------------------------------Code-------------------------------------------------
begin script NMsTemp_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.25 SCRIPT_OBJECT_TYPE_FEATURE FEATURE_INFO_AZTC_SUNTEMPLE at {xPos,yPos,zPos}
	override mesh for NMs_Miracles[calIdx + NMs_MIRSYMBOL] with "..\models\m_tree_oak"
	SCRIPT_OBJECT_PROPERTY_TYPE_YPOS of NMs_Miracles[calIdx + NMs_MIRSYMBOL] = yPos
	//Bubble Visuals
	NMs_Miracles[calIdx + NMs_MIROBJ1] = create visual effect VISUAL_MIRACLE_TORNODO_SEED at {xPos,visHeight,zPos}+{0,1,0} time -1	
	NMs_Miracles[calIdx + NMs_MIROBJ2] = create visual effect VISUAL_EFFECT_HAND at {xPos,visHeight,zPos}+{0,1,0} time -1	
	//Scaling and Colouring
	SCRIPT_OBJECT_PROPERTY_TYPE_SCALE of NMs_Miracles[calIdx + NMs_MIROBJ2] = 0.5
	set NMs_Miracles[calIdx + NMs_MIROBJ1] colour red 0 green 127 blue 14
	set NMs_Miracles[calIdx + NMs_MIROBJ2] colour red 188 green 75 blue 0
	
	//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_FOREST
	
end script NMsTemp_createSymbolicBubble

//Create the miracle in the player's hand
begin script NMsTemp_createMiracleInHand
	idx = -1
	cnt = 0
start
	//Locate the most available index in the array 
	while((cnt < NMsTEMP_MAXACTIVE) and (idx == -1))
		if(NMsTemp_HandActive[cnt] == NMs_FALSE)
			idx = cnt
		end if
		cnt++
	end while
	
	//Set result
	NMsTemp_HandResult = idx
	NMsTemp_HandActive[idx] = NMs_TRUE
	
	//Symbol Creation
	NMsTemp_Hand[idx] = create with angle 0 and scale 0.25 SCRIPT_OBJECT_TYPE_ROCK MOBILE_STATIC_INFO_SMALL_NORSE_ROCK at hand position
	override mesh for NMsTemp_Hand[idx] with "..\models\m_tree_oak"
	//Bubble Visuals
	NMsTemp_HandV1[idx] = create visual effect VISUAL_MIRACLE_TORNADO_IN_HAND on NMsTemp_Hand[idx] time -1	
	NMsTemp_HandV2[idx] = create visual effect VISUAL_EFFECT_HAND on NMsTemp_Hand[idx] time -1	
	set NMsTemp_Hand[idx] in player 0 hand
	//Scaling and Colouring
	SCRIPT_OBJECT_PROPERTY_TYPE_SCALE of NMsTemp_HandV2[idx] = 0.5
	set NMsTemp_HandV1[idx] colour red 0 green 127 blue 14
	set NMsTemp_HandV2[idx] colour red 188 green 75 blue 0
	
end script NMsTemp_createMiracleInHand

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

start
	//Indicate the index of the array is open
	NMsTemp_HandActive[idx] = NMs_FALSE
	//Remove the miracle hand representation
	delete NMsTemp_Hand[idx]
	stop visual effect NMsTemp_HandV1[idx]
	stop visual effect NMsTemp_HandV2[idx]
end script NMsTemp_deleteMiracleInHand

//Hides the symbolic representation from the totem
//	This function only hides the symbolic bubble, it doesn't change its location or anything else.
begin script NMsTemp_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 NMsTemp_hideSymbolicBubble

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

//Script for pouring the miracle, NMsTemp_Casting is set false upon completion of the cast
begin script NMsTemp_CastPour(pwr, idx)
	amt = 0
	t_Amount = 0
	t_PerTick = 0
	pos = 0
	ti_Tmr = create timer for 0 seconds
start
	NMsTemp_Casting = NMs_TRUE
	if(NMsTemp_HandActive[idx] == NMs_TRUE)
		set NMsTemp_Hand[idx] in player 0 hand
		
		//Calculate
		/*
			Deal with Power calculations here
		*/
		t_Amount = pwr //t_Amount is used to determine how much of something happens
		//Incrementation per iteration
		t_PerTick = 1
		
		//Initialize tracker for hand position
		pos = marker at {NMsTemp_Hand[idx]}
		
		//Setup for timing each interation
		set ti_Tmr time to 0.3 seconds
		
		begin loop
			//Keep the miracle in the player's hand
			set NMsTemp_Hand[idx] in player 0 hand
			//Wait for timer to reach 0 and the action button is down and the hand is within its influence
			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))				
				//Amount tracker
				amt += t_PerTick
				//Update tracker for the hand position
				pos = marker at {NMsTemp_Hand[idx]}
				
				/*
					
					
					Code what occurs each iteration of the miracle here
					
					
				*/
				
				//Update Timer
				set ti_Tmr time to 0.3 seconds
			end if
			//Check if t_Amount as been reached or the cancel was hit
			until ((amt >= t_Amount) or (bindable action BINDABLE_ACTION_TYPE_CANCEL_CURRENT_ACTION performed))
		end loop
		
		//Clean up
		/*
		
			Remove any visuals, or objects that are not longer needed here
		
		*/
		//Delete the representation of the miracle from the player's hand
		run script NMsTemp_deleteMiracleInHand(idx)
	end if
	
	NMsTemp_Casting = NMs_FALSE
end script NMsTemp_CastPour


//Script for throwing the miracle, NMsTemp_Casting is set false upon completion of the cast
begin script NMsTemp_CastThrown(pwr, idx)
	t_Amount = 0
	ti_Tmr = create timer for 60 seconds
	effect_go = NMs_TRUE
	pos = 0	//Position of the miracle when it hits something
start
	NMsTemp_Casting = NMs_TRUE
	if(NMsTemp_HandActive[idx] == NMs_TRUE)
		//Calculate
		/*
			Deal with Power calculations here
		*/
		t_Amount = pwr //t_Amount is used to determine how much of something happens
		
		//Enable physics tracking
		enable NMsTemp_Hand[idx] physics tracking
		
		//Wait until something happens, or the time limit is hit
		wait until ((NMsTemp_Hand[idx] hit) or (NMsTemp_Hand[idx] hit land) or (get ti_Tmr time remaining == 0))
		
		//If the time limit is reached, this is more to ensure that in the event something goes wrong
		//	and the miracle never comes into contact with something. The timer will make sure the script
		//	ends rather than taking up processing time
		if(get ti_Tmr time remaining == 0)
			effect_go = NMs_FALSE
		end if
		
		//Record contact position
		pos = marker at {NMsTemp_Hand[idx]}
		
		//Remove the miracle hand representation
		run script NMsTemp_deleteMiracleInHand(idx)
		
		//Check if executing must be by-passed due to an error
		if(effect_go == NMs_TRUE)
			/*
			
				Code the miracle's effect here
				Use pos as the location to do whatever the miracle requires
			
			*/
			
			/*
			
				Clean up code here if needed
			
			*/
		end if
	end if
	NMsTemp_Casting = NMs_FALSE
end script NMsTemp_CastThrown
