-- JFD_Wales_Functions
-- Author: JFD
-- DateCreated: 5/5/2017 2:09:09 PM
--==========================================================================================================================
-- INCLUDES
--==========================================================================================================================
include("Civ6Common.lua")
--==========================================================================================================================
-- UTILITY FUNCTIONS
--==========================================================================================================================
--HasCivilizationTrait
function HasCivilizationTrait(civilizationType, traitType)
	for row in GameInfo.CivilizationTraits() do
		if (row.CivilizationType == civilizationType and row.TraitType == traitType) then return true end
	end
	return false
end

--HasLeaderTrait
function HasLeaderTrait(leaderType, traitType)
	for row in GameInfo.LeaderTraits() do
		if (row.LeaderType == leaderType and row.TraitType == traitType) then return true end
	end
	return false
end

--GetRandom
function GetRandom(lower, upper, string)
    return Game.GetRandNum((lower + 1) - lower, string) + lower
end
--==========================================================================================================================
-- CORE FUNCTIONS
--==========================================================================================================================
-- WALES: THE MABINOGION
----------------------------------------------------------------------------------------------------------------------------
local traitTheMabinogion = "TRAIT_CIVILIZATION_JFD_MABINOGION"

--JFD_Wales_Mabinogion_UnitKilledInCombat
local greatPersonWriterID  = GameInfo.GreatPersonClasses["GREAT_PERSON_CLASS_WRITER"].Index
function JFD_Wales_Mabinogion_UnitKilledInCombat(killedPlayerID, killedUnitID, playerID, unitID)
	if (not killedPlayerID) then return end
	if (not playerID) then return end
	if killedPlayerID == playerID then return end
	local player = Players[playerID]
	local playerKilled = Players[killedPlayerID]
	local playerConfig = PlayerConfigurations[playerID]
	local civType = playerConfig:GetCivilizationTypeName()
	if (not HasCivilizationTrait(civType, traitTheMabinogion)) then return end
	local unit = player:GetUnits():FindID(unitID)
	local unitKilled = playerKilled:GetUnits():FindID(killedUnitID)
	if (not unitKilled) then return end
	local numGWP = math.floor(GameInfo.Units[unitKilled:GetType()].Combat/4)
	local plotX, plotY = unit:GetX(), unit:GetY()
	local plot = Map.GetPlot(plotX, plotY)
	if plot:GetOwner() == playerID then numGWP = numGWP*2 end
	if numGWP <= 0 then return end
	player:GetGreatPeoplePoints():ChangePointsTotal(greatPersonWriterID,  numGWP)
	if player:IsHuman() then
		Game.AddWorldViewText(playerID, Locale.Lookup("[COLOR_FLOAT_FOOD]+{1_Num} Great Writer Points[ENDCOLOR]", numGWP), unit:GetX(), unit:GetY(), 0)
	end
end
Events.UnitKilledInCombat.Add(JFD_Wales_Mabinogion_UnitKilledInCombat)
--==========================================================================================================================
--==========================================================================================================================

