define TEST_MIN_ALTITUDE = 3
define TEST_MOVE_SPEED = 1.5
//define TEST_MOVE_SPEED = 5.0
define TEST_NO_TURN_WIDTH = 0.07
define TEST_MAX_TURN_SPEED = 10
define TEST_NO_PITCH_HEIGHT = 0.125
define TEST_MAX_PITCH_SPEED = 0.125
define TEST_MAX_PITCH_AMOUNT = 1
define TEST_GRAVITY = 0.5
define TEST_JUMP_VELOCITY = 3
define TEST_WALL_HEIGHT = 2
define TEST_FIREBALL_VELOCITY = 100

// Miracle cost
define HEAL_COST = 1000
define FIRE_COST = 2000
define METEOR_COST = 7500
define WIND_COST = 1250
define WATER_COST = 750
define LIGHTNING_COST = 5000


// Kalev's First Person Shooter Script.
// - Modified by Serdan.
//
// Features:
// # Obstacle Detection.
// # Casting miracles costs mana.
// # The Avatar can be killed.
// # FPS mode can only be activated inside the player's influence.
// # Miracles have to be bought before they can be cast in FPS mode.
//
// Key Map:
// # Switch between FPS and God mode: T
// # Fire weapon: LMB
// # Jump: RMB and Space
// # Move: WASD and Arrows.
// # Fireball: 1
// # Lightning: 2
// # Meteor: 3
// # Wind: 4
// # Water: 5
// # Heal: E
begin script KalevFPS
	CurrentFocusRotation = 0
	CurrentFocusRelativeHeight = 0

	TurnAmount = 0
	TurnHotArea = 0

	CameraLoc = 0
	CameraFoc = 0

	OnGround = 0
	CurrentYVelocity = 0

	CameraAltitude = 0
	PrevCameraHeight = 0
	CameraHeight = 0

	PrevCameraLoc = 0
	NewCameraLoc = 0

	WaterHeight = 0

	// Location of exit area.
	//ExitX = 2015.31
	//ExitZ = 678.56

	// The minimum distance the player has to be from the specified exit.
	//ExitDis = 20.0

	//Weapon Variables
	Weapon = 0
	CurrentWeapon = 1
	Fire = 1
	Wind = 2
	Lightning = 3
	Meteor = 4
	Water = 5
	Heal = 6
	Shield = 7

	Mana = 0

	// Avatar
	//Avatar = 0

start
	wait until key KB_T down and get player 0 influence at camera position > 0
	wait 0.2 seconds

	WaterHeight = get water height

	Avatar = create random villager of tribe TRIBE_TYPE_GREEK at camera position
	set Avatar player 0
	disable Avatar pickup
	disable Avatar can be fisted
	disable Avatar interactable

	enable objective window
	enable objective list in widescreen
	set player 0 objective TRIBUTE_OBJECTIVE_LAND_1 amount 1
	if get research ARTEFACT_MAGIC_TYPE_FIRE_FIRE available == 2
		CurrentWeapon = Fire
		set player 0 objective TRIBUTE_OBJECTIVE_LAND_1 text "BW2T_MIRACLE_FIRE"
	else
		CurrentWeapon = 0
	end if
	begin cinema
		begin loop
			CameraLoc = marker at camera position

			if OnGround == 0
				CameraAltitude = get SCRIPT_OBJECT_PROPERTY_TYPE_YPOS of CameraLoc
				CameraAltitude += CurrentYVelocity
				CurrentYVelocity -= TEST_GRAVITY
			elsif key KB_SPACE down or mouse right button down
				CurrentYVelocity = TEST_JUMP_VELOCITY
				OnGround = 0
			end if

			TurnAmount = mouse percentage across
			TurnHotArea = (1 - TEST_NO_TURN_WIDTH)/2
			if TurnAmount < TurnHotArea
				CurrentFocusRotation += (1 - (TurnAmount / TurnHotArea)) * TEST_MAX_TURN_SPEED
			elsif  TurnAmount > 1 - TurnHotArea
				CurrentFocusRotation -= (1 - ((1 - TurnAmount) / TurnHotArea)) * TEST_MAX_TURN_SPEED
			end if

			TurnAmount = mouse percentage down
			TurnHotArea = (1 - TEST_NO_PITCH_HEIGHT)/2
			if TurnAmount < TurnHotArea
				CurrentFocusRelativeHeight -= (1 - (TurnAmount / TurnHotArea)) * TEST_MAX_PITCH_SPEED
			elsif  TurnAmount > 1 - TurnHotArea
				CurrentFocusRelativeHeight += (1 - ((1 - TurnAmount) / TurnHotArea)) * TEST_MAX_PITCH_SPEED
			end if
			if (CurrentFocusRelativeHeight < -1 * TEST_MAX_PITCH_AMOUNT) CurrentFocusRelativeHeight = -1 * TEST_MAX_PITCH_AMOUNT end if
			if (CurrentFocusRelativeHeight > TEST_MAX_PITCH_AMOUNT) CurrentFocusRelativeHeight = TEST_MAX_PITCH_AMOUNT end if

			PrevCameraLoc = CameraLoc
			PrevCameraHeight = CameraAltitude + land height at {CameraLoc}
			if key KB_W down or key KB_UP down
				NewCameraLoc = marker at get target from {CameraLoc} + {1,0,0} to {CameraLoc} distance TEST_MOVE_SPEED angle CurrentFocusRotation
				if Avatar can navigate to {NewCameraLoc}
					CameraLoc = NewCameraLoc
				end if
			elsif key KB_S down or key KB_DOWN down
				NewCameraLoc = marker at get target from {CameraLoc} + {-1,0,0} to {CameraLoc} distance TEST_MOVE_SPEED angle CurrentFocusRotation
				if Avatar can navigate to {NewCameraLoc}
					CameraLoc = NewCameraLoc
				end if
			end if
			if key KB_A down or key KB_LEFT down
				NewCameraLoc = marker at get target from {CameraLoc} + {0,0,1} to {CameraLoc} distance TEST_MOVE_SPEED angle CurrentFocusRotation
				if Avatar can navigate to {NewCameraLoc}
					CameraLoc = NewCameraLoc
				end if
			elsif key KB_D down or key KB_RIGHT down
				NewCameraLoc = marker at get target from {CameraLoc} + {0,0,-1} to {CameraLoc} distance TEST_MOVE_SPEED angle CurrentFocusRotation
				if Avatar can navigate to {NewCameraLoc}
					CameraLoc = NewCameraLoc
				end if
			end if
			CameraHeight = CameraAltitude + land height at {CameraLoc}
			if CameraHeight < TEST_MIN_ALTITUDE + WaterHeight
				CameraAltitude = TEST_MIN_ALTITUDE + WaterHeight - land height at {CameraLoc}
				OnGround = 1
			elsif OnGround == 0
				CameraAltitude = PrevCameraHeight - land height at {CameraLoc}
				if CameraAltitude < TEST_MIN_ALTITUDE
					CameraAltitude = TEST_MIN_ALTITUDE
					if CurrentYVelocity < 0
						OnGround = 1
					end if
				end if
			elsif land height at {CameraLoc} < WaterHeight
				CameraAltitude = TEST_MIN_ALTITUDE + WaterHeight - land height at {CameraLoc}
			elsif (land height at {CameraLoc}) - (land height at {PrevCameraLoc}) > TEST_WALL_HEIGHT
				CameraLoc = PrevCameraLoc
			end if
			SCRIPT_OBJECT_PROPERTY_TYPE_YPOS of CameraLoc = CameraAltitude

			CameraFoc = marker at get target from {CameraLoc} + {1,0,0} to {CameraLoc} distance 1 angle CurrentFocusRotation

			SCRIPT_OBJECT_PROPERTY_TYPE_YPOS of CameraFoc = get SCRIPT_OBJECT_PROPERTY_TYPE_YPOS of CameraFoc - CurrentFocusRelativeHeight

			set camera position to {CameraLoc}
			set camera focus to {CameraFoc}

			set Avatar position to {CameraLoc}
			set Avatar focus to {CameraFoc}

			// Change weapon according to key pressed.
			if key KB_1 down and get research ARTEFACT_MAGIC_TYPE_FIRE_FIRE available == 2
				CurrentWeapon = Fire
				set player 0 objective TRIBUTE_OBJECTIVE_LAND_1 text "BW2T_MIRACLE_FIRE"
			elsif key KB_2 down and get research ARTEFACT_MAGIC_TYPE_WATER_STORM available == 2
				CurrentWeapon = Lightning
				set player 0 objective TRIBUTE_OBJECTIVE_LAND_1 text "BW2T_MIRACLE_LIGHTNING"
			elsif key KB_3 down and get research ARTEFACT_MAGIC_TYPE_EARTH_METEOR available == 2
				CurrentWeapon = Meteor
				set player 0 objective TRIBUTE_OBJECTIVE_LAND_1 text "BW2T_MIRACLE_METEOR"
			elsif key KB_4 down and get research ARTEFACT_MAGIC_TYPE_AIR_TEMPEST available == 2
				CurrentWeapon = Wind
				set player 0 objective TRIBUTE_OBJECTIVE_LAND_1 text "BW2T_MIRACLE_SHIELD"
			elsif key KB_5 down and get research ARTEFACT_MAGIC_TYPE_WATER_RAIN available == 2
				CurrentWeapon = Water
				set player 0 objective TRIBUTE_OBJECTIVE_LAND_1 text "BW2T_MIRACLE_WATER"
			end if

			Mana = get player 0 mana
			// Fire selected weapon
			if mouse left button down

				if CurrentWeapon == Fire and FIRE_COST <= Mana
					Weapon = make player 0 throw miracle MIRACLE_TYPE_FIRE from {CameraLoc} heading TEST_FIREBALL_VELOCITY * ({CameraFoc} - {CameraLoc})
					set player 0 mana Mana-FIRE_COST
				elsif CurrentWeapon == Lightning and LIGHTNING_COST <= Mana
					Weapon = make player 0 throw miracle MIRACLE_TYPE_STORM from {CameraLoc} heading TEST_FIREBALL_VELOCITY * ({CameraFoc} - {CameraLoc})
					set player 0 mana Mana-LIGHTNING_COST
				elsif CurrentWeapon == Meteor and METEOR_COST <= Mana
					Weapon = make player 0 throw miracle MIRACLE_TYPE_METEOR from {CameraLoc} heading TEST_FIREBALL_VELOCITY * ({CameraFoc} - {CameraLoc})
					set player 0 mana Mana-METEOR_COST
				elsif CurrentWeapon == Wind and WIND_COST <= Mana
					Weapon = make player 0 throw miracle MIRACLE_TYPE_TORNADO from {CameraLoc} heading TEST_FIREBALL_VELOCITY * ({CameraFoc} - {CameraLoc})
					set player 0 mana Mana-WIND_COST
				elsif CurrentWeapon == Water and WATER_COST <= Mana
					Weapon = make player 0 throw miracle MIRACLE_TYPE_WATER from {CameraLoc} heading TEST_FIREBALL_VELOCITY * ({CameraFoc} - {CameraLoc})
					set player 0 mana Mana-WATER_COST
				end if
			end if

			if key KB_E down and HEAL_COST <= Mana and get research ARTEFACT_MAGIC_TYPE_LIFE_HEAL available == 2
				Weapon = make player 0 pour miracle MIRACLE_TYPE_HEAL at {CameraLoc}
				set player 0 mana Mana-HEAL_COST
			end if			

			// Exit FPS mode when the "T" button is pressed.
			until key KB_T down or get SCRIPT_OBJECT_PROPERTY_TYPE_HEALTH of Avatar < 0.2
			//until get distance from camera position to {ExitX,ExitZ} < ExitDis

		end loop
	end cinema

	disable player 0 objective TRIBUTE_OBJECTIVE_LAND_1
	disable objective window
	disable objective list in widescreen
	//enable Avatar on fire 1.0
	delete Avatar
	wait 0.2 seconds
	run background script KalevFPS

end script KalevFPS