/**************************************************************************************************
 * Program:		New Miracles for Black & White 2
 * Purpose:		Forest.txt contains all the scripts needed for a miracle which creates animals
 * Author:		Bill
 * Version: 	0
 * Date: 		Feb 2 2020
**************************************************************************************************/
//-------------------------------------------Scripts-----------------------------------------------
define script NMsAmls_createSymbolicBubble(idx, xPos, yPos, zPos, t_idx)
define script NMsAmls_createMiracleInHand
define script NMsAmls_hideSymbolicBubble(m_idx)
define script NMsAmls_CastPour(pwr,idx)
define script NMsAmls_CastThrown(pwr,idx)
define script NMsAmls_deleteMiracleInHand(idx)
define script NMsAmls_calculateMana(m_idx,pwr)
//------------------------------------------Constants----------------------------------------------
define NMsAMLS_MAXACTIVE			= 20
//-------------------------------------------Globals-----------------------------------------------
global NMsAmls_Count 				= 0

global NMsAmls_Hand[NMsAMLS_MAXACTIVE]
global NMsAmls_HandV1[NMsAMLS_MAXACTIVE]
global NMsAmls_HandV2[NMsAMLS_MAXACTIVE]
global NMsAmls_HandActive[NMsAMLS_MAXACTIVE]

global NMsAmls_HandLock				= NMs_FALSE		//Lock variable for NMsAmls_createMiracleInHand
global NMsAmls_HandResult			= 0				//Result variable for NMsAmls_createMiracleInHand

global NMsAmls_Casting 				= NMs_FALSE
global NMsAmls_Pile 				= 0

//--------------------------------------------Code-------------------------------------------------
begin script NMsAmls_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 1.5 SCRIPT_OBJECT_TYPE_FEATURE FEATURE_INFO_AZTC_SUNTEMPLE at {xPos,yPos,zPos}
	override mesh for NMs_Miracles[calIdx + NMs_MIRSYMBOL] with "..\skins\s_cattle"
	SCRIPT_OBJECT_PROPERTY_TYPE_YPOS of NMs_Miracles[calIdx + NMs_MIRSYMBOL] = yPos
	//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_MIRACLE_WIND_SEED 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
	set NMs_Miracles[calIdx + NMs_MIROBJ1] colour red 0 green 127 blue 0
	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_ANIMALS
	
end script NMsAmls_createSymbolicBubble

//Create the miracle in the player's hand
begin script NMsAmls_createMiracleInHand
	idx = -1
	cnt = 0
start
	while((cnt < NMsAMLS_MAXACTIVE) and (idx == -1))
		if(NMsAmls_HandActive[cnt] == NMs_FALSE)
			idx = cnt
		end if
		cnt++
	end while
	
	NMsAmls_HandResult = idx
	NMsAmls_HandActive[idx] = NMs_TRUE
	
	//Symbol Creation
	NMsAmls_Hand[idx] = create with angle 0 and scale 1.5 SCRIPT_OBJECT_TYPE_ROCK MOBILE_STATIC_INFO_SMALL_NORSE_ROCK at hand position
	override mesh for NMsAmls_Hand[idx] with "..\skins\s_cattle"
	//Bubble Creation
	NMsAmls_HandV1[idx] = create visual effect VISUAL_MIRACLE_HEAL_IN_HAND on NMsAmls_Hand[idx] time -1	
	NMsAmls_HandV2[idx] = create visual effect VISUAL_MIRACLE_WIND_SEED on NMsAmls_Hand[idx] time -1	
	set NMsAmls_Hand[idx] in player 0 hand
	//Scaling and Colouring
	SCRIPT_OBJECT_PROPERTY_TYPE_SCALE of NMsAmls_HandV2[idx] = 1
	set NMsAmls_HandV1[idx] colour red 0 green 127 blue 0
	set NMsAmls_HandV2[idx] colour red 188 green 75 blue 0
	
end script NMsAmls_createMiracleInHand

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

start
	NMsAmls_HandActive[idx] = NMs_FALSE
	delete NMsAmls_Hand[idx]
	stop visual effect NMsAmls_HandV1[idx]
	stop visual effect NMsAmls_HandV2[idx]
end script NMsAmls_deleteMiracleInHand

//Hides the symbolic representation from the totem
begin script NMsAmls_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 NMsAmls_hideSymbolicBubble

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

//Script for pouring the miracle, NMsAmls_Casting is set false upon completion of the cast
//	Either by the amount of wheat is reached or the cancel is hit
begin script NMsAmls_CastPour(pwr, idx)
	amt = 0
	v_Pour = 0
	t_Amount = 0
	currentAmt = 0
	t_PerTick = 0
	
	pos = 0
	p_dist = 3
	p_x1 = 0
	p_z1 = 0
	p_x2 = 1
	p_z2 = 0
	
	t_obj = 0
	typ = 0
	
	ti_Tmr = create timer for 0 seconds
start
	NMsAmls_Casting = NMs_TRUE
	if(NMsAmls_HandActive[idx] == NMs_TRUE)
		set NMsAmls_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 {NMsAmls_Hand[idx]}

		set ti_Tmr time to 0.3 seconds
		v_Pour = create visual effect VISUAL_MIRACLE_HEAL_IMPACT at {pos} time -1
		set v_Pour colour red 0 green 127 blue 0
		begin loop		
			set NMsAmls_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))				
				if(p_x2 > 3)
					p_x2 = 3
				end if
				SCRIPT_OBJECT_PROPERTY_TYPE_SCALE of v_Pour = p_x2
				SCRIPT_OBJECT_PROPERTY_TYPE_STRENGTH of v_Pour = p_x2
				
				amt += t_PerTick
				
				pos = marker at {NMsAmls_Hand[idx]}
				
				//Calculate spot
				p_x1 = SCRIPT_OBJECT_PROPERTY_TYPE_XPOS of pos + ((p_dist) / NMs_MATHTHING) * cos(number from 0 to 360)
				p_z1 = SCRIPT_OBJECT_PROPERTY_TYPE_ZPOS of pos + ((p_dist) / NMs_MATHTHING) * sin(number from 0 to 360)
				
				//Pick a type of animal
				typ = number from 0 to 4
				if(typ == 0)
					typ = 8 //Cow
				elsif(typ == 1)
					typ = 9 //Chicken
				elsif(typ == 2)
					typ = 10 //Dog
				elsif(typ == 3)
					typ = 12 //Pig
				elsif(typ == 4)
					typ = 16 //Sheep
				end if
				t_obj = create with angle 0 and scale 1 SCRIPT_OBJECT_TYPE_ANIMAL constant typ at {p_x1,p_z1}
				SCRIPT_OBJECT_PROPERTY_TYPE_ANGLE of t_obj = number from 0 to 360
				
				p_dist += 0.5
				p_x2 += 0.1
				set ti_Tmr time to 0.3 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 NMsAmls_deleteMiracleInHand(idx)
	end if
	
	NMsAmls_Casting = NMs_FALSE
end script NMsAmls_CastPour

//Script for throwing the miracle, NMsAmls_Casting is set false upon completion of the cast
begin script NMsAmls_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
	p_x = 0
	p_z = 0
	cnt = 0
	v_ex = 0
	typ = 0
	p_dist = 3
	t_obj = 0
	t_PerTick = 0
start
	NMsAmls_Casting = NMs_TRUE
	if(NMsAmls_HandActive[idx] == NMs_TRUE)
		//Calculate
		t_Amount = 5 * pwr
		if(t_Amount < 5)
			t_Amount = 5
		end if
		t_PerTick = 1 //2 per tick
		
		//Enable physics tracking
		enable NMsAmls_Hand[idx] physics tracking
		
		//Wait until something happens, or the time limit is hit
		wait until ((NMsAmls_Hand[idx] hit) or (NMsAmls_Hand[idx] hit land) or (get ti_Tmr time remaining == 0) or (not NMsAmls_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 {NMsAmls_Hand[idx]}
		//Remove the miracle in hand representation
		run script NMsAmls_deleteMiracleInHand(idx)
		
		if(effect_go == NMs_TRUE)
			//Create effects
			v_ex = create visual effect VISUAL_MIRACLE_HEAL_IMPACT at {pos} time -1
			set v_ex colour red 0 green 127 blue 0
			
			//Calculate positions
			cnt = 0
			while(cnt < t_Amount)				
				//Calculate spot
				p_x = SCRIPT_OBJECT_PROPERTY_TYPE_XPOS of pos + ((p_dist) / NMs_MATHTHING) * cos(number from 0 to 360)
				p_z = SCRIPT_OBJECT_PROPERTY_TYPE_ZPOS of pos + ((p_dist) / NMs_MATHTHING) * sin(number from 0 to 360)
				
				//Pick a type of tree
				typ = number from 0 to 4
				if(typ == 0)
					typ = 8 //Cow
				elsif(typ == 1)
					typ = 9 //Chicken
				elsif(typ == 2)
					typ = 10 //Dog
				elsif(typ == 3)
					typ = 12 //Pig
				elsif(typ == 4)
					typ = 16 //Sheep
				end if
				t_obj = create with angle (number from 0 to 360) and scale 1 SCRIPT_OBJECT_TYPE_ANIMAL constant typ at {p_x,p_z}
				SCRIPT_OBJECT_PROPERTY_TYPE_ANGLE of t_obj = number from 0 to 360
				cnt += t_PerTick
				p_dist += 0.5
			end while
			stop visual effect v_ex
		end if
	end if
	NMsAmls_Casting = NMs_FALSE
end script NMsAmls_CastThrown
