/**************************************************************************************************
 * Program:		New Miracles for Black & White 2
 * Purpose:		Forest.txt contains all the scripts needed for a miracle which creates ore rocks
 * Author:		Bill
 * Version: 	0
 * Date: 		Feb 2 2020
**************************************************************************************************/
//-------------------------------------------Scripts-----------------------------------------------
define script NMsOre_createSymbolicBubble(idx, xPos, yPos, zPos, t_idx)
define script NMsOre_createMiracleInHand
define script NMsOre_hideSymbolicBubble(m_idx)
define script NMsOre_CastPour(pwr,idx)
define script NMsOre_CastThrown(pwr,idx)
define script NMsOre_deleteMiracleInHand(idx)
define script NMsOre_calculateMana(m_idx,pwr)
//------------------------------------------Constants----------------------------------------------
define NMsOre_MAXACTIVE			= 20
//-------------------------------------------Globals-----------------------------------------------
global NMsOre_Count 				= 0

global NMsOre_Hand[NMsOre_MAXACTIVE]
global NMsOre_HandV1[NMsOre_MAXACTIVE]
global NMsOre_HandV2[NMsOre_MAXACTIVE]
global NMsOre_HandActive[NMsOre_MAXACTIVE]

global NMsOre_HandLock				= NMs_FALSE		//Lock variable for NMsOre_createMiracleInHand
global NMsOre_HandResult			= 0				//Result variable for NMsOre_createMiracleInHand

global NMsOre_Casting 				= NMs_FALSE
global NMsOre_Pile 					= 0

//--------------------------------------------Code-------------------------------------------------
begin script NMsOre_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.75 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_ore_large"
	SCRIPT_OBJECT_PROPERTY_TYPE_YPOS of NMs_Miracles[calIdx + NMs_MIRSYMBOL] = yPos + 1
	//Bubble Creation
	NMs_Miracles[calIdx + NMs_MIROBJ1] = create visual effect VISUAL_MIRACLE_FIRE_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.6
	set NMs_Miracles[calIdx + NMs_MIROBJ1] colour red 255 green 255 blue 255
	set NMs_Miracles[calIdx + NMs_MIROBJ2] colour red 50 green 50 blue 50
	
	//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_ORE
	
end script NMsOre_createSymbolicBubble

//Create the miracle in the player's hand
begin script NMsOre_createMiracleInHand
	idx = -1
	cnt = 0
start
	while((cnt < NMsOre_MAXACTIVE) and (idx == -1))
		if(NMsOre_HandActive[cnt] == NMs_FALSE)
			idx = cnt
		end if
		cnt++
	end while
	
	NMsOre_HandResult = idx
	NMsOre_HandActive[idx] = NMs_TRUE
	
	//Symbol Creation
	NMsOre_Hand[idx] = create with angle 0 and scale 0.75 SCRIPT_OBJECT_TYPE_ROCK MOBILE_STATIC_INFO_SMALL_NORSE_ROCK at hand position
	override mesh for NMsOre_Hand[idx] with "..\models\m_ore_large"
	//Bubble Creation
	NMsOre_HandV1[idx] = create visual effect VISUAL_MIRACLE_FIRE_IN_HAND on NMsOre_Hand[idx] time -1	
	NMsOre_HandV2[idx] = create visual effect VISUAL_EFFECT_HAND on NMsOre_Hand[idx] time -1	
	set NMsOre_Hand[idx] in player 0 hand
	//Scaling and Colouring
	SCRIPT_OBJECT_PROPERTY_TYPE_SCALE of NMsOre_HandV2[idx] = 0.6
	set NMsOre_HandV1[idx] colour red 255 green 255 blue 255
	set NMsOre_HandV2[idx] colour red 50 green 50 blue 50
	
end script NMsOre_createMiracleInHand

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

start
	NMsOre_HandActive[idx] = NMs_FALSE
	delete NMsOre_Hand[idx]
	stop visual effect NMsOre_HandV1[idx]
	stop visual effect NMsOre_HandV2[idx]
end script NMsOre_deleteMiracleInHand

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

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

//Script for pouring the miracle, NMsOre_Casting is set false upon completion of the cast
//	Either by the amount of wheat is reached or the cancel is hit
begin script NMsOre_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 = 0
	p_z2 = 0
	
	t_obj = 0
	typ = 0
	
	ti_Tmr = create timer for 0 seconds
start
	NMsOre_Casting = NMs_TRUE
	if(NMsOre_HandActive[idx] == NMs_TRUE)
		set NMsOre_Hand[idx] in player 0 hand
		
		//Calculate
		t_Amount = 3 * pwr
		if(t_Amount < 3)
			t_Amount = 3
		end if
		t_PerTick = 1
		
		pos = marker at {NMsOre_Hand[idx]}
		v_Pour = create visual effect VISUAL_EFFECT_RESOURCE_ORE_DOWN at {pos} time 1

		set ti_Tmr time to 0.1 seconds
		
		begin loop		
			set NMsOre_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 {NMsOre_Hand[idx]}
				t_obj = create with angle 0 and scale (number from 0.8 to 1.1) SCRIPT_OBJECT_TYPE_ORE_ROCK constant 41 at {pos}+{number from 0 to 2,0,number from 0 to 2}
				
				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 NMsOre_deleteMiracleInHand(idx)
	end if
	
	NMsOre_Casting = NMs_FALSE
end script NMsOre_CastPour

//Script for throwing the miracle, NMsOre_Casting is set false upon completion of the cast
begin script NMsOre_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_x1 = 0
	p_z1 = 0
	pr = 0	//Random amount of piles
	ar = 0	//Random angle value in position calculations
	dr = 0	//Random distance value in position calculations
	ps = 0	//Pile Object
	pa = 0	//Amount per pile
	cnt = 0
	v_ex = 0
	typ = 0
	t_obj = 0
start
	NMsOre_Casting = NMs_TRUE
	if(NMsOre_HandActive[idx] == NMs_TRUE)
		//Calculate
		t_Amount = 3 * pwr
		if(t_Amount < 3)
			t_Amount = 3
		end if
		//Enable physics tracking
		enable NMsOre_Hand[idx] physics tracking
		
		//Wait until something happens, or the time limit is hit
		wait until ((NMsOre_Hand[idx] hit) or (NMsOre_Hand[idx] hit land) or (get ti_Tmr time remaining == 0) or (not NMsOre_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 {NMsOre_Hand[idx]}
		dr = create visual effect VISUAL_EFFECT_HEAL_EXPLOSION at {pos} time 3
		SCRIPT_OBJECT_PROPERTY_TYPE_SCALE of dr = 1
		SCRIPT_OBJECT_PROPERTY_TYPE_STRENGTH of dr = 2
		set dr colour red 128 green 128 blue 128
		
		//Remove the miracle in hand representation
		run script NMsOre_deleteMiracleInHand(idx)
		
		if(effect_go == NMs_TRUE)
			//Create effects
			v_ex = create visual effect VISUAL_EFFECT_NEPTUNE_FOUNTAIN at {pos} time -1
			
			SCRIPT_OBJECT_PROPERTY_TYPE_SCALE of v_ex = 3
			SCRIPT_OBJECT_PROPERTY_TYPE_STRENGTH of v_ex = 4
			set v_ex colour red 50 green 50 blue 50
			
			//Calculate positions
			while(cnt < t_Amount)
				
				//Calculate spot
				p_x1 = SCRIPT_OBJECT_PROPERTY_TYPE_XPOS of pos + ((1) / NMs_MATHTHING) * cos(ar)
				p_z1 = SCRIPT_OBJECT_PROPERTY_TYPE_ZPOS of pos + ((1) / NMs_MATHTHING) * sin(ar)
				
				t_obj = create with angle 0 and scale (number from 0.8 to 1.1) SCRIPT_OBJECT_TYPE_ORE_ROCK constant 41 at {p_x1,p_z1}
				
				run background script NMs_LaunchObject(t_obj)
				ar += 50
				cnt++
			end while
			
			stop visual effect dr
			stop visual effect v_ex
		end if
	end if
	NMsOre_Casting = NMs_FALSE
end script NMsOre_CastThrown
