-- Watatsuki_GameplayScripts
-- Author: KCucumber
-- DateCreated: 1/24/2018 11:59:09 AM
--------------------------------------------------------------
print("God Summon loaded");
--------------------------------------------------------------
local SpeedMultiplier = GameInfo.GameSpeeds[GameConfiguration.GetGameSpeedType()].CostMultiplier / 100;

--------------------------------------------------------------
function YorihimeLeaderCheck(playerId)
	local pPlayer = Players[playerId];
	local playerConfig = PlayerConfigurations[playerId];

	if pPlayer:IsMajor() and pPlayer:IsAlive() then
		if playerConfig:GetLeaderTypeName() == "LEADER_YORIHIME" then
			if pPlayer:IsAlive() then
				local pCities = pPlayer:GetCities();
				if pCities:GetCapitalCity() ~= nil then
--					local EraVar = GetYorihimeEraVar(pPlayer);
					YorihimeSummonedGodCheck(pPlayer);
				end
			end
		end
	end
end
GameEvents.PlayerTurnStarted.Add(YorihimeLeaderCheck);
--------------------------------------------------------------
function YorihimeSummonedGodCheck(pPlayer)
	local pUnits = pPlayer:GetUnits();
	
	for i, pUnit in pUnits:Members() do
		if (pUnit ~= nil) then
			local typeName:string = GameInfo.Units[pUnit:GetType()].UnitType;
			local r = string.sub(typeName,0,14);
			print(r);
			if (r == "UNIT_WATATSUKI") then
				local EraVar = GetYorihimeEraVar(pPlayer);
				local unitExperience = pUnit:GetExperience();
				if ((unitExperience:GetExperienceForNextLevel() - unitExperience:GetExperiencePoints()) < (15 * (EraVar + 1) * SpeedMultiplier)) then
					unitExperience:ChangeExperience(15 * EraVar * SpeedMultiplier);
				end
				if unitExperience:HasPromotion(GameInfo.UnitPromotions["PROMOTION_RELIGION_L1"].Index) then
					local pPlayerTech = pPlayer:GetTechs();
					if pPlayerTech:GetResearchingTech() == GameInfo.Technologies["TECH_ASTROLOGY"].Index then
						local CurrentTech = pPlayerTech:GetScienceYield();
						pPlayerTech:ChangeCurrentResearchProgress(CurrentTech);
					end
				end
				if unitExperience:HasPromotion(GameInfo.UnitPromotions["PROMOTION_RELIGION_L3L"].Index) then
					local GPP = pPlayer:GetGreatPeoplePoints():CalculatePointsPerTurn(GameInfo.GreatPersonClasses["GREAT_PERSON_CLASS_PROPHET"].Index);
					pPlayer:GetReligion():ChangeFaithBalance(GPP * 2 * SpeedMultiplier);
				end
				return;
			end
		end
	end
	local capitalCity = pPlayer:GetCities():GetCapitalCity();
	pPlayer:GetUnits():Create(GameInfo.Units["UNIT_WATATSUKI_YORIHIME"].Index, capitalCity:GetX(), capitalCity:GetY());
	return;
end
--------------------------------------------------------------
function GetYorihimeEraVar(pPlayer)
	local playerEra = pPlayer:GetEra() + 1;
	--print("Era_",playerEra);
	if playerEra > 7 then
		playerEra = 7;
	end
	return playerEra;
end
--------------------------------------------------------------
function YorihimeSummonGod(playerId, unitId, IndexID)
	local pPlayer = Players[playerId];
	local playerConfig = PlayerConfigurations[playerId];

	if playerConfig:GetLeaderTypeName() == "LEADER_YORIHIME" then
		local pUnit = pPlayer:GetUnits():FindID(unitId);
		local typeName:string = GameInfo.Units[pUnit:GetType()].UnitType;
		if (typeName == "UNIT_WATATSUKI_YORIHIME") then
			if IndexID == GameInfo.UnitPromotions["PROMOTION_SCIENCE_GOD"].Index then
				pPlayer:GetUnits():Create(GameInfo.Units["UNIT_WATATSUKI_YORIHIME_SCIENCE"].Index, pUnit:GetX(), pUnit:GetY());
				UnitManager.Kill(pUnit, false);
				return;
			else
				if IndexID == GameInfo.UnitPromotions["PROMOTION_CULTURE_GOD"].Index then
					pPlayer:GetUnits():Create(GameInfo.Units["UNIT_WATATSUKI_YORIHIME_CULTURE"].Index, pUnit:GetX(), pUnit:GetY());
					UnitManager.Kill(pUnit, false);
					return;
				else
					if IndexID == GameInfo.UnitPromotions["PROMOTION_RELIGION_GOD"].Index then
						pPlayer:GetUnits():Create(GameInfo.Units["UNIT_WATATSUKI_YORIHIME_RELIGION"].Index, pUnit:GetX(), pUnit:GetY());
						UnitManager.Kill(pUnit, false);
						return;
					else
						if IndexID == GameInfo.UnitPromotions["PROMOTION_PRODUCTION_GOD"].Index then
							pPlayer:GetUnits():Create(GameInfo.Units["UNIT_WATATSUKI_YORIHIME_PRODUCTION"].Index, pUnit:GetX(), pUnit:GetY());
							UnitManager.Kill(pUnit, false);
							return;
						else
							if IndexID == GameInfo.UnitPromotions["PROMOTION_COMMERCIAL_GOD"].Index then
								pPlayer:GetUnits():Create(GameInfo.Units["UNIT_WATATSUKI_YORIHIME_COMMERCIAL"].Index, pUnit:GetX(), pUnit:GetY());
								UnitManager.Kill(pUnit, false);
								return;
							else
								if IndexID == GameInfo.UnitPromotions["PROMOTION_WAR_GOD"].Index then
									pPlayer:GetUnits():Create(GameInfo.Units["UNIT_WATATSUKI_YORIHIME_WAR"].Index, pUnit:GetX(), pUnit:GetY());
									UnitManager.Kill(pUnit, false);
									return;
								else
									if IndexID == GameInfo.UnitPromotions["PROMOTION_DIPLOMACY_GOD"].Index then
										pPlayer:GetUnits():Create(GameInfo.Units["UNIT_WATATSUKI_YORIHIME_DIPLOMACY"].Index, pUnit:GetX(), pUnit:GetY());
										UnitManager.Kill(pUnit, false);
										return;
--									else
--										if IndexID == GameInfo.UnitPromotions["PROMOTION_PEOPLE_GOD"].Index then
--											pPlayer:GetUnits():Create(GameInfo.Units["UNIT_WATATSUKI_YORIHIME_PEOPLE"].Index, pUnit:GetX(), pUnit:GetY());
--											UnitManager.Kill(pUnit, false);
--											return;
--										end
									end
								end
							end
						end
					end
				end
			end
		else 
			if (typeName == "UNIT_WATATSUKI_YORIHIME_WAR") then
				if IndexID == GameInfo.UnitPromotions["PROMOTION_WAR_L1"].Index then
					local capitalCity = pPlayer:GetCities():GetCapitalCity();
					pPlayer:GetUnits():Create(GameInfo.Units["UNIT_GOD_OF_WAR_TOKEN"].Index, capitalCity:GetX(), capitalCity:GetY());
					return;
				end
			end
		end
	end
end
Events.UnitPromoted.Add(YorihimeSummonGod);
--------------------------------------------------------------
function YorihimeGodSwitch(playerId)
	local pPlayer = Players[playerId];
	local playerConfig = PlayerConfigurations[playerId];

	if playerConfig:GetLeaderTypeName() == "LEADER_YORIHIME" then
		local pUnits = pPlayer:GetUnits();
		for i, pUnit in pUnits:Members() do
			if (pUnit ~= nil) then
				local typeName:string = GameInfo.Units[pUnit:GetType()].UnitType;
				local r = string.sub(typeName,0,14);
				if (r == "UNIT_WATATSUKI") and (typeName ~= "UNIT_WATATSUKI_YORIHIME") then
					if (typeName == "UNIT_WATATSUKI_YORIHIME_WAR") then
						WAR_GOD_REMOVE_TOKEN(pUnits);
					end
					pPlayer:GetUnits():Create(GameInfo.Units["UNIT_WATATSUKI_YORIHIME"].Index, pUnit:GetX(), pUnit:GetY());
					UnitManager.Kill(pUnit, false);
					return;
				end
			end
		end
	end
end
LuaEvents.SwitchGods.Add(YorihimeGodSwitch);
--------------------------------------------------------------
function PRODUCTION_L3R_WONDER(locX, locY, buildingIndex, playerIndex, iPercentComplete)
	local pPlayer	:table = Players[playerIndex];
	local playerConfig = PlayerConfigurations[playerIndex];

	if playerConfig:GetLeaderTypeName() == "LEADER_YORIHIME" then
		local pUnits = pPlayer:GetUnits();
		
		for i, pUnit in pUnits:Members() do
			if (pUnit ~= nil) then
				local typeName:string = GameInfo.Units[pUnit:GetType()].UnitType;
--				local r = string.sub(typeName,0,14);
--				print(r);
				if (typeName == "UNIT_WATATSUKI_YORIHIME_PRODUCTION") then
					local unitExperience = pUnit:GetExperience();
					if unitExperience:HasPromotion(GameInfo.UnitPromotions["PROMOTION_PRODUCTION_L3R"].Index) then
						local WonderProduction = GameInfo.Buildings[buildingIndex].Cost;
						local pCities = pPlayer:GetCities();
						for ii, pCity in pCities:Members() do
							if pCity:GetBuildings():HasBuilding(buildingIndex) then
								pCity:GetBuildQueue():AddProgress(WonderProduction * SpeedMultiplier * 0.2);
								return;
							end
						end
					end
				end
			end
		end
	end
end
Events.WonderCompleted.Add( PRODUCTION_L3R_WONDER );
--------------------------------------------------------------
function WAR_GOD_REMOVE_TOKEN(pUnits)
	for i, pUnit in pUnits:Members() do
		if (pUnit ~= nil) then
			local typeName:string = GameInfo.Units[pUnit:GetType()].UnitType;
			if (typeName == "UNIT_GOD_OF_WAR_TOKEN") then
				UnitManager.Kill(pUnit, false);
			end
		end
	end
end
--------------------------------------------------------------