/**************************************************************************************************
 * Program:		New Miracles for Black & White 2
 * Purpose:		Wheat.txt contains all the scripts needed for a miracle which creates piles of grain
 * Author:		Bill
 * Version: 	0
 * Date: 		Feb 2 2020
**************************************************************************************************/
//-------------------------------------------Scripts-----------------------------------------------
define script NMsWheat_createSymbolicBubble(idx, xPos, yPos, zPos, t_idx)
define script NMsWheat_createMiracleInHand
define script NMsWheat_hideSymbolicBubble(m_idx)
define script NMsWheat_CastPour(pwr,idx)
define script NMsWheat_CastThrown(pwr,idx)
define script NMsWheat_deleteMiracleInHand(idx)
define script NMsWheat_calculateMana(m_idx,pwr)
//------------------------------------------Constants----------------------------------------------
define NMsWHEAT_MAXACTIVE			= 20
//-------------------------------------------Globals-----------------------------------------------
global NMsWheat_Count 				= 0

global NMsWheat_Hand[NMsWHEAT_MAXACTIVE]
global NMsWheat_HandV1[NMsWHEAT_MAXACTIVE]
global NMsWheat_HandV2[NMsWHEAT_MAXACTIVE]
global NMsWheat_HandActive[NMsWHEAT_MAXACTIVE]

global NMsWheat_HandLock			= NMs_FALSE		//Lock variable for NMsWheat_createMiracleInHand
global NMsWheat_HandResult			= 0				//Result variable for NMsWheat_createMiracleInHand

global NMsWheat_Casting 			= NMs_FALSE
global NMsWheat_Pile 				= 0

//--------------------------------------------Code-------------------------------------------------
begin script NMsWheat_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.5 SCRIPT_OBJECT_TYPE_FEATURE FEATURE_INFO_AZTC_SUNTEMPLE at {xPos,yPos,zPos}-{0,0,0}
	override mesh for NMs_Miracles[calIdx + NMs_MIRSYMBOL] with "..\models\m_generic_haybail"
	SCRIPT_OBJECT_PROPERTY_TYPE_YPOS of NMs_Miracles[calIdx + NMs_MIRSYMBOL] = yPos
	//Bubble Creation
	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_MIRACLE_TREMOR_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
	set NMs_Miracles[calIdx + NMs_MIROBJ1] colour red 255 green 229 blue 104
	set NMs_Miracles[calIdx + NMs_MIROBJ2] colour red 182 green 100 blue 3
	
	//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_WHEAT
	
end script NMsWheat_createSymbolicBubble

//Create the miracle in the player's hand
begin script NMsWheat_createMiracleInHand
	idx = -1
	cnt = 0
start
	while((cnt < NMsWHEAT_MAXACTIVE) and (idx == -1))
		if(NMsWheat_HandActive[cnt] == NMs_FALSE)
			idx = cnt
		end if
		cnt++
	end while
	
	NMsWheat_HandResult = idx
	NMsWheat_HandActive[idx] = NMs_TRUE
	
	//Symbol Creation
	NMsWheat_Hand[idx] = create with angle 0 and scale 0.5 SCRIPT_OBJECT_TYPE_MOBILE_STATIC MOBILE_STATC_HAY_BALE at hand position
	//Bubble Creation
	NMsWheat_HandV1[idx] = create visual effect VISUAL_MIRACLE_TORNADO_IN_AIR on NMsWheat_Hand[idx] time -1	
	NMsWheat_HandV2[idx] = create visual effect VISUAL_MIRACLE_TREMOR_IN_HAND on NMsWheat_Hand[idx] time -1	
	set NMsWheat_Hand[idx] in player 0 hand
	//Scaling and Colouring
	SCRIPT_OBJECT_PROPERTY_TYPE_SCALE of NMsWheat_HandV2[idx] = 1
	set NMsWheat_HandV1[idx] colour red 255 green 229 blue 104
	set NMsWheat_HandV2[idx] colour red 182 green 100 blue 3
	
	enable NMsWheat_Hand[idx] physics tracking
	
end script NMsWheat_createMiracleInHand

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

start
	NMsWheat_HandActive[idx] = NMs_FALSE
	delete NMsWheat_Hand[idx]
	stop visual effect NMsWheat_HandV1[idx]
	stop visual effect NMsWheat_HandV2[idx]
end script NMsWheat_deleteMiracleInHand

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

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

//Script for pouring the miracle, NMsWheat_Casting is set false upon completion of the cast
//	Either by the amount of wheat is reached or the cancel is hit
begin script NMsWheat_CastPour(pwr, idx)
	amt = 0
	v_Pour = 0
	t_Amount = 0
	currentAmt = 0
	t_PerTick = 0
	dist = 0
	obj = 0
	pile_Obj = 0
	ti_Tmr = create timer for 0 seconds
start
	//Set casting state to true
	NMsWheat_Casting = NMs_TRUE
	
	//Make sure the object exists
	if(NMsWheat_HandActive[idx] == NMs_TRUE)
		set NMsWheat_Hand[idx] in player 0 hand

		//Grain pour visual effect, includes sound effects
		v_Pour = create visual effect VISUAL_EFFECT_RESOURCE_GRAIN_DOWN on NMsWheat_Hand[idx] time -1
		
		//Calculate the amount of wheat based on the totem power
		t_Amount = 1000 * pwr
		if(t_Amount < 1000)
			t_Amount = 1000 //minimum amount of wheat
		end if
		//Timer loop is 0.1 seconds
		//Therefore to achieve a period of 5 seconds the amount must be divided by 50
		t_PerTick = t_Amount / 50
		
		//Check for store or pile near by
		obj = get object hand is over
		if (obj exists)
			pile_Obj = obj
		else
			pile_Obj = create SCRIPT_OBJECT_TYPE_STORE RESOURCE_TYPE_FOOD at {NMsWheat_Hand[idx]}
		end if

		set ti_Tmr time to 0.1 seconds
		
		begin loop
			//Keep object in player hand
			set NMsWheat_Hand[idx] in player 0 hand
			//wait for the timer to be 0, the action must be held down plus the hand must be within your 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))
				//Increment the amount produced
				amt += t_PerTick
				
				//Make sure we are still in range of the previous pile
				dist = get distance from {pile_Obj} to hand position
				if(dist >= 5) //Create new pile if the distance is > 5
					pile_Obj = create SCRIPT_OBJECT_TYPE_STORE RESOURCE_TYPE_FOOD at {NMsWheat_Hand[idx]}
				end if
				//Add wheat to the pile
				currentAmt = get resource RESOURCE_TYPE_FOOD in pile_Obj
				set pile_Obj resource RESOURCE_TYPE_FOOD to (currentAmt + t_PerTick)
				
				//Reset timer
				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
		
		//Clean up
		stop visual effect v_Pour
		run script NMsWheat_deleteMiracleInHand(idx)
	end if
	
	//End casting state
	NMsWheat_Casting = NMs_FALSE
end script NMsWheat_CastPour

//Script for throwing the miracle, NMsWheat_Casting is set false upon completion of the cast
begin script NMsWheat_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
	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
start
	//Set casting state
	NMsWheat_Casting = NMs_TRUE
	
	if(NMsWheat_HandActive[idx] == NMs_TRUE)
		//Calculate wheat amounts
		t_Amount = 1000 * pwr
		if(t_Amount < 1000)
			t_Amount = 1000
			pr = number from 2 to 5
		else
			pr = number from 5 to 10
		end if
		pa = t_Amount / pr
		//Enable physics tracking
		enable NMsWheat_Hand[idx] physics tracking
		
		//Wait until something happens, or the time limit is hit
		wait until ((NMsWheat_Hand[idx] hit) or (NMsWheat_Hand[idx] hit land) or (get ti_Tmr time remaining == 0) or (not NMsWheat_Hand[idx] is moving))
		
		//If the time limit is reached, exit, assuming the object is lost
		if(get ti_Tmr time remaining == 0)
			effect_go = NMs_FALSE
		end if
		
		//Read object position
		pos = marker at {NMsWheat_Hand[idx]}
		//Remove the miracle in hand representation
		run script NMsWheat_deleteMiracleInHand(idx)
		
		if(effect_go == NMs_TRUE)
			//Create effects
			v_ex = create visual effect VISUAL_MIRACLE_WATER_IMPACT at {pos} time -1
			//Visual effect properties
			SCRIPT_OBJECT_PROPERTY_TYPE_SCALE of v_ex = 1.5
			set v_ex colour red 255 green 229 blue 104
			
			//Calculate positions
			while(cnt < pr)
				ar = number from 0 to 360
				dr = number from 5 to 25
				p_x = SCRIPT_OBJECT_PROPERTY_TYPE_XPOS of pos + (dr / NMs_MATHTHING) * cos(ar)
				p_z = SCRIPT_OBJECT_PROPERTY_TYPE_ZPOS of pos + (dr / NMs_MATHTHING) * sin(ar)
				
				obj = get SCRIPT_OBJECT_TYPE_ABODE at {p_x,p_z} radius 20
				if((obj exists) and (variable get obj sub type == 7))
					add resource RESOURCE_TYPE_FOOD pa to obj
				else
					ps = create SCRIPT_OBJECT_TYPE_STORE RESOURCE_TYPE_FOOD at {p_x,p_z}
				end if
				set ps resource RESOURCE_TYPE_FOOD to pa
				
				cnt++
			end while
		end if
	end if
	
	//Clear casting state
	NMsWheat_Casting = NMs_FALSE
end script NMsWheat_CastThrown

