function PlayerUpdate(idx)
	local as = gSportsRNA
	local player = as:GetTable("wvPlayer", idx)
	local state = as:GetTable("wvState")
	
	db.player[idx].envLighting = as:GetInt(state, "wvAttribEnvLighting")
	db.player[idx].stadiumLightID = as:GetInt(state, "wvAttribStadLightID")
	db.player[idx].stadiumID = as:GetInt(state, "wvAttribStadID")
	local weather = as:GetInt(state, "wvAttribStadWeather" )
	if (weather == 1 or weather == 2) then
		db.player[idx].wet = 1
	else
		db.player[idx].wet = 0
	end
	
	db.player[idx].playerassetid = as:GetInt(player, "playerassetid") 
	db.player[idx].teamid = as:GetInt(player, "teamid") 
	db.player[idx].teamside = as:GetInt(player, "teamside") 
	db.player[idx].isgoalie = as:GetInt(player, "goalie") 
	
	db.player[idx].kit = as:GetInt(player, "kit") 
	db.player[idx].kitType = as:GetInt(player, "kitType") 
	
	if ( db.player[idx].kitType > 59 ) then 
	db.player[idx].kitType = db.player[idx].kitType - 60
	end
	if ( db.player[idx].kitType > 39 ) then 
	db.player[idx].kitType = db.player[idx].kitType - 40
	end
	if ( db.player[idx].kitType > 19 ) then 
	db.player[idx].kitType = db.player[idx].kitType - 20
	end
	
	db.player[idx].kitYear = as:GetInt(player, "kitYear") 

	local kitNumber = as:GetInt(player, "kitNumber") 
	db.player[idx].kitNumberTens = math.floor ( kitNumber / 10 )
	db.player[idx].kitNumberUnits = math.floor ( math.fmod(kitNumber, 10) )

	db.player[idx].kitNameFont = as:GetInt(player, "kitNameFont")
	db.player[idx].kitNumberFont = as:GetInt(player, "kitNumberFont")
	db.player[idx].kitNumberColor = as:GetInt(player, "kitNumberColor")
	db.player[idx].kitNameColor = as:GetInt(player, "kitNameColor")
	
	db.player[idx].shortsNumberFont = as:GetInt(player, "shortsNumberFont")
	db.player[idx].shortsNumberColor = as:GetInt(player, "shortsNumberColor")
	db.player[idx].jerseyNameVisible = as:GetInt(player, "jerseyNameVisible")
	db.player[idx].jerseyNameLayout = as:GetInt(player, "jerseyNameLayout")
	
	db.player[idx].head = as:GetInt(player, "playerHead") 
	db.player[idx].headClass = as:GetInt(player, "playerHeadClass") 
	
	db.player[idx].jerseyCollarType = as:GetInt(player, "jerseyCollarType") 
	db.player[idx].jerseySleeveLength = as:GetInt(player, "jerseySleeveLength") 
	db.player[idx].jerseyArmBand = as:GetInt(player, "jerseyArmBand") 
	db.player[idx].jerseyTucked = as:GetInt(player, "jerseyTucked") 
	db.player[idx].jerseyfit = as:GetInt(player, "jerseyfit") 
	db.player[idx].socklength = as:GetInt(player, "socklength")
	db.player[idx].shortstyle = as:GetInt(player, "shortstyle")
	
	db.player[idx].bodySkinToneType = as:GetInt(player, "playerBodySkinToneType")
	db.player[idx].headSkinToneType = as:GetInt(player, "playerHeadSkinToneType")
	db.player[idx].headSkinType = as:GetInt(player, "playerHeadSkinType")
	db.player[idx].playerBodySkinColor = as:GetInt(player, "playerBodySkinColor")
	db.player[idx].faceGenSkinToneType = as:GetInt(player, "faceGenSkinToneType")
	
	db.player[idx].shoeType = as:GetInt(player, "shoeType")
	db.player[idx].shoeDesign = 0 --as:GetInt(player, "shoeDesign")
	--db.player[idx].shoeColorPri = as:GetInt(player, "shoeColorPrim")
	--db.player[idx].shoeColorSec = as:GetInt(player, "shoeColorSec")
	--db.player[idx].shoeColorTer = as:GetInt(player, "shoeColorTer")

	db.player[idx].faceType = as:GetInt(player, "faceType")
	db.player[idx].faceProxyHeadClass = as:GetInt(player, "faceProxyHeadClass")
	-- Faceproxyheadclass should never be greater than one including createplayer
	if (db.player[idx].faceProxyHeadClass == 2) then
		db.player[idx].faceProxyHeadClass = 1
	end
	
	db.player[idx].faceSideBurn = as:GetInt(player, "faceSideBurn")
	db.player[idx].facialHairColor = as:GetInt(player, "facialHairColor")
	db.player[idx].facialHairType = as:GetInt(player, "facialHairType")
	db.player[idx].eyebrow = as:GetInt(player, "eyebrow")
	
	db.player[idx].isVirtualPro = as:GetInt(player, "isVirtualPro")
	db.player[idx].isCreatePlayer = as:GetInt(player, "isCreatePlayer")
	db.player[idx].faceTexExtension = "rx3"
	
	db.player[idx].isCreationZone = as:GetInt(player, "isFCZ")
	db.player[idx].hasCzCrestImage = as:GetInt(player, "hasCzCrestImage")
	db.player[idx].crestAssetId = as:GetInt(player, "crestAssetId")
	db.player[idx].forceLowResBNM = as:GetInt(player, "forceLowResBNM")
	db.player[idx].forcePowerOfTwoBNM = as:GetInt(player, "forcePowerOfTwoBNM")
	db.player[idx].forceunderarm = as:GetInt(player, "forceunderarm")
	db.player[idx].forceundershorts = as:GetInt(player, "forceundershorts")
	db.player[idx].forceunderneck = as:GetInt(player, "forceunderneck")

	db.player[idx].sponsorAssetId = as:GetInt(player, "sponsorAssetId")
	db.player[idx].hotspotJerseySponsorL = as:GetFloat(player, "hotspotJerseySponsorL")
	db.player[idx].hotspotJerseySponsorT = as:GetFloat(player, "hotspotJerseySponsorT")
	db.player[idx].hotspotJerseySponsorR = as:GetFloat(player, "hotspotJerseySponsorR")
	db.player[idx].hotspotJerseySponsorB = as:GetFloat(player, "hotspotJerseySponsorB")
	
	db.player[idx].kitColourJerseyPri = as:GetInt(player, "kitColourJerseyPri")
	db.player[idx].kitColourJerseySec = as:GetInt(player, "kitColourJerseySec")
	db.player[idx].kitColourJerseyTer = as:GetInt(player, "kitColourJerseyTer")

	db.player[idx].kitColourShortPri = as:GetInt(player, "kitColourShortPri")
	db.player[idx].kitColourShortSec = as:GetInt(player, "kitColourShortSec")
	db.player[idx].kitColourShortTer = as:GetInt(player, "kitColourShortTer")

	db.player[idx].kitColourSocksPri = as:GetInt(player, "kitColourSocksPri")
	db.player[idx].kitColourSocksSec = as:GetInt(player, "kitColourSocksSec")
	db.player[idx].kitColourSocksTer = as:GetInt(player, "kitColourSocksTer")
	
	db.player[idx].kitColourMaskPri = 0x00FF0000
	db.player[idx].kitColourMaskSec = 0x0000FF00
	db.player[idx].kitColourMaskTer = 0x000000FF


	db.player[idx].sponsorcolour = as:GetInt(player, "sponsorcolour")

	db.player[idx].crestTexName = "crest_cm"
	
	if (db.player[idx].isCreationZone == 1 and db.player[idx].hasCzCrestImage > 0) then
		db.player[idx].crestTexName = "png";
	end

	if (db.player[idx].isVirtualPro == 1) then
		db.player[idx].faceTexExtension = "dds"
	end
	
	db.player[idx].faceTypeFallback = 0
	db.player[idx].headFallback = 0
	db.player[idx].faceSideBurnFallback = db.player[idx].faceSideBurn
	db.player[idx].facialHairColorFallback = db.player[idx].facialHairColor
	db.player[idx].facialHairTypeFallback = db.player[idx].facialHairType
	db.player[idx].headSkinTypeFallback = db.player[idx].headSkinType
	db.player[idx].headSkinToneTypeFallback = db.player[idx].headSkinToneType
	
	if(db.player[idx].headSkinToneTypeFallback == 1 or db.player[idx].headSkinToneTypeFallback == 3 or db.player[idx].headSkinToneTypeFallback == 7) then
		db.player[idx].headSkinToneTypeFallback = db.player[idx].headSkinToneTypeFallback + 1
	end
	
	-- If create player then we need the create player head
	-- head_{id}_{headClass}.rx2
	-- {id} == 0 is create player
	-- {id} == 1 is create player copy
	-- {id} == 2 is facegen head
	-- {id} == 3 is facegen head copy
 	if (db.player[idx].isCreatePlayer == 1) then
		db.player[idx].headClass = 2
		db.player[idx].faceProxyHeadClass = 1
		
		if (db.player[idx].isVirtualPro == 1) then
			db.player[idx].faceSideBurn = 0
			db.player[idx].facialHairColor = 0
			db.player[idx].facialHairType = 0
			db.player[idx].headSkinType = 0
			db.player[idx].headSkinToneType = 0
			db.player[idx].bodySkinToneType = 11 -- force body skin tone type to 11 (greyscale) for face gen
			
		end
		
	end
	
	
	db.player[idx].eyeColor = as:GetInt(player, "eyeColor")
	db.player[idx].hair = as:GetInt(player, "hair")
	db.player[idx].hairColor = as:GetInt(player, "hairColor") 
	

	--IF REFEREE
	if (db.player[idx].kitType == 5) then
	db.player[idx].playerassetid = db.player[idx].playerassetid + 500000
	end
	
	--PLAYER FILES
	if (oldidx ~= idx and db.player[idx].isCreatePlayer ~= 1 and db.player[idx].isVirtualPro ~= 1 and db.player[idx].isCreationZone ~= 1) then
	
	if (idx < 2)then
	LoadAsync("data/fifarna/lua/assignments/teams/team_"..db.player[idx].teamid..".lua")
	end
	
	LoadAsync("data/fifarna/lua/assignments/players/player_"..db.player[idx].playerassetid..".lua")
	LoadAsyncWait()
	end
	oldidx = idx
	
	--FACE
	db.player[idx].headClass = getPlayerFace(db.player[idx].playerassetid,db.player[idx].headClass)
	db.player[idx].faceProxyHeadClass = getPlayerFace(db.player[idx].playerassetid,db.player[idx].faceProxyHeadClass)
	

	if (db.player[idx].headClass == 0) then
	
		local playerassetid = db.player[idx].playerassetid
		
		db.player[idx].head = playerassetid 
		db.player[idx].eyeColor = playerassetid

		db.player[idx].eyebrow = 0
		db.player[idx].faceType = playerassetid
		db.player[idx].faceSideBurn = 0
		db.player[idx].facialHairColor = 0
		db.player[idx].facialHairType = 0
		db.player[idx].headSkinType = 0
		db.player[idx].headSkinToneType = 0

		db.player[idx].hair = playerassetid
		db.player[idx].hairColor = 0
	else
		if(db.player[idx].headSkinToneType == 1 or db.player[idx].headSkinToneType == 3 or db.player[idx].headSkinToneType == 7) then
			db.player[idx].headSkinToneType = db.player[idx].headSkinToneType + 1
		end
		--tmp
		-- db.player[idx].hairColorIdx = db.player[idx].hairColor
		-- db.player[idx].hairColor = 0	
	end
	
	
-- Seasonal Assets --
	-- These exist only here and not in the code:
	local seasonal = as:GetInt(player, "seasonaljersey")
	
	
	--GET ACCESSORY WEATHER
	local winterAcc = getWinterAccessoriesWeather(weather)
	
	--GET PLAYER WINTER ACCESSORIES
	if (db.player[idx].kitType == 5) then
	seasonal = getRefereeWinterAccessories(seasonal)
	end
	
	seasonal = getWinterAccessories(db.player[idx].playerassetid,seasonal)
	
	--SLEEVE LENGTH
	db.player[idx].jerseySleeveLength = getSleeveLength(db.player[idx].playerassetid,db.player[idx].jerseySleeveLength)
	
	
	
	if(db.player[idx].isVirtualPro == 1 or db.player[idx].isCreatePlayer == 1) then
		if(seasonal < 2) then
			seasonal = db.player[idx].jerseySleeveLength
		end
	--elseif(db.player[idx].wet == 0) then
	elseif (not winterAcc) then
		seasonal = db.player[idx].jerseySleeveLength
	end
	
	

	
	if (seasonal == 0) then
		-- Short Sleeves
		db.player[idx].jerseySleeveLength  = 0
		db.player[idx].underneck = -1
		db.player[idx].underarms = -1
		db.player[idx].armLength = 0
	elseif (seasonal == 1) then
		-- Long Sleeves
		db.player[idx].jerseySleeveLength  = 1
		db.player[idx].underneck = -1
		db.player[idx].underarms = -1
		db.player[idx].armLength = 1
	elseif (seasonal == 2) then
		-- Long Sleeves and just underarmor neck
		db.player[idx].jerseySleeveLength = 1
		db.player[idx].underneck = 0
		db.player[idx].underarms = -1
		db.player[idx].armLength = 1
	elseif (seasonal == 3) then
		-- Short sleeves with underarmor arms no neck
		db.player[idx].jerseySleeveLength  = 0
		db.player[idx].underneck = -1
		db.player[idx].underarms = 0
		db.player[idx].armLength = 1
	elseif (seasonal == 4) then
		-- Short sleeves with underarmor neck and arms
		db.player[idx].jerseySleeveLength  = 0
		db.player[idx].underneck = 0
		db.player[idx].underarms = 0
		db.player[idx].armLength = 1
	end
	
	if (db.player[idx].wet > 0 or db.player[idx].isVirtualPro == 1 or db.player[idx].isCreatePlayer == 1) then
		db.player[idx].undershorts = 0 
	else
		db.player[idx].undershorts = -1
	end

	-- end of seasonal assets
	
	-- check if we need to force underwear for injuries
	if ( db.player[idx].forceunderarm == 1 ) then
		db.player[idx].jerseySleeveLength  = 0
		db.player[idx].underarms = 0
		db.player[idx].armLength = 1
	end
	
	if ( db.player[idx].forceundershorts == 1 ) then
		db.player[idx].undershorts = 0 
	end
	
	if ( db.player[idx].forceunderneck == 1 ) then
		db.player[idx].underneck = 0 
	end

	-- Goalie long pants, turn off necessary assets.
	if( db.player[idx].shortstyle == 1 ) then
		db.player[idx].socklength = -1
		db.player[idx].undershorts = -1
	end
	
	db.player[idx].useTextureComposition = as:GetInt(player, "useTextureComposition")
	
	
	--RMCUSTOMSTART
	
	if (db.player[idx].shoeType == 0) then
	db.player[idx].shoeType = getGenericBootReplacement(db.player[idx].playerassetid,db.player[idx].shoeType)
	end
	
	
	
	
	
	
	
	
		
	local stadium = as:GetTable("wvStadium", 1)
		
	db.player[idx].homeKitTeamID = as:GetInt(stadium, "homeKitTeamID" )
	db.player[idx].awayKitTeamID = as:GetInt(stadium, "awayKitTeamID" )
	db.player[idx].homeKitTypeID = as:GetInt(stadium, "homeKitTypeID" )
	db.player[idx].awayKitTypeID = as:GetInt(stadium, "awayKitTypeID" )
	
	local outidx = 3
	if (db.player[idx].teamside == 1) then
	outidx = 14
	end
	
	local outplayer = as:GetTable("wvPlayer", outidx)
	
	local kitOutfield = as:GetInt(outplayer, "kit") 
	local kitTypeOutfield = as:GetInt(outplayer, "kitType") 
	local kitYearOutfield = as:GetInt(outplayer, "kitYear")
	
	local wipe3d = as:GetTable("wvWipe", 1)
	
	db.player[idx].tournid = as:GetInt(wipe3d, "leagueID")
	db.player[idx].tournid = getTournamentGraphics(db.player[idx].tournid)


	--IF GENERIC KIT
	db.player[idx].speckitType = -1
	if (db.player[idx].teamid == db.player[idx].kit) then
	db.player[idx].genkit = db.player[idx].teamid
	else
	db.player[idx].genkit = db.player[idx].kit
	if (db.player[idx].kitType ~= 5) then
	db.player[idx].kit = db.player[idx].teamid
	-- if (db.player[idx].isgoalie == 1) then
	-- db.player[idx].kitNameFont = as:GetInt(outplayer, "kitNameFont")
	-- db.player[idx].kitNumberFont = as:GetInt(outplayer, "kitNumberFont")
	-- db.player[idx].kitNumberColor = 1
	-- db.player[idx].kitNameColor = hex("EEEEEE")
	-- end
	end
	end
	
	--IF CLASSIC KIT
	if ((db.player[idx].kitYear > 0) and true) then
	db.player[idx].tournid = db.player[idx].kitYear
	end
	
	db.player[idx].defaulttournid = -1
	if (db.player[idx].kitType == 5) then
	if (teamTournament[db.player[idx].homeKitTeamID] ~= nil) then
	db.player[idx].defaulttournid = teamTournament[db.player[idx].homeKitTeamID]
	end
	else
	if (teamTournament[db.player[idx].teamid] ~= nil) then
	db.player[idx].defaulttournid = teamTournament[db.player[idx].teamid]
	end
	end
	
	
	--CLASSIC GK KIT
	if ((db.player[idx].isgoalie == 1) and (kitYearOutfield > 0)) then
	db.player[idx].tournid = kitYearOutfield
	end
	
	--ARENA PLAYER KIT
	if ((idx <  2) and (db.player[idx].isgoalie == 0)) then
	db.player[idx].speckitType = 91
	end

	--ARENA GK KIT
	if ((idx <  2) and (db.player[idx].isgoalie == 1)) then
	db.player[idx].speckitType = 92
	end
	
	--TRAINING KIT HOME
	if ((db.player[idx].genkit == 7000) and (db.player[idx].kitType == 6)) then
	db.player[idx].speckitType = 93
	end
	
	--TRAINING KIT AWAY
	if ((db.player[idx].genkit == 7000) and (db.player[idx].kitType == 7)) then
	db.player[idx].speckitType = 94
	end
	
	if ((db.player[idx].isgoalie == 1) and (idx > 1)) then
	
	--TRAINING KIT HOME GK
	if ((kitOutfield == 7000) and (kitTypeOutfield == 6)) then
	db.player[idx].speckitType = 95
	end
	
	--TRAINING KIT HOME GK
	if ((kitOutfield == 7000) and (kitTypeOutfield == 7)) then
	db.player[idx].speckitType = 96
	end
	
	end
	
	--GK KITS
	if ((db.player[idx].isgoalie == 1) and (idx > 1)) then
	
	--ALTERNATE GK
	if ((kitOutfield ~= 7000)) then
	db.player[idx].speckitType = kitTypeOutfield + 30
	end
	
	--
	if (db.player[idx].kitYear == 0) then
	if (db.player[idx].teamside == 0) then
	db.player[idx].speckitType = getGKKit(db.player[idx].kit,db.player[idx].homeKitTypeID,db.player[idx].homeKitTeamID,db.player[idx].awayKitTeamID,db.player[idx].kitType,db.player[idx].speckitType)
	else
	db.player[idx].speckitType = getGKKit(db.player[idx].kit,db.player[idx].awayKitTypeID,db.player[idx].homeKitTeamID,db.player[idx].awayKitTeamID,db.player[idx].kitType,db.player[idx].speckitType)
	end
	end
	
	end
	
	
	
	

	--NON GK KIT
	if ((db.player[idx].isgoalie == 0) and (idx > 1)) then
	
	--SPECIFIC MATCH KIT
	if (db.player[idx].kitYear == 0) then
	db.player[idx].speckitType = getGameKit(db.player[idx].kit,db.player[idx].homeKitTeamID,db.player[idx].awayKitTeamID,db.player[idx].kitType,db.player[idx].speckitType)
	end
	
	end

	
	--KIT DETAILS
	db.player[idx].kitNameFont = getKitNameFont(db.player[idx].kit,db.player[idx].kitType,db.player[idx].kitNameFont,db.player[idx].tournid,db.player[idx].teamid)
	db.player[idx].kitNumberFont = getKitNumberSet(db.player[idx].kit,db.player[idx].kitType,db.player[idx].kitNumberFont,db.player[idx].tournid,db.player[idx].teamid)
	db.player[idx].kitNumberColor = getKitNumberColourShirt(db.player[idx].kit,db.player[idx].kitType,db.player[idx].kitNumberColor,db.player[idx].tournid)
	db.player[idx].kitNameColor = getKitNameColour(db.player[idx].kit,db.player[idx].kitType,db.player[idx].kitNameColor,db.player[idx].tournid)
	db.player[idx].shortsNumberFont = getKitNumberSet(db.player[idx].kit,db.player[idx].kitType,db.player[idx].shortsNumberFont,db.player[idx].tournid,db.player[idx].teamid)
	db.player[idx].shortsNumberColor = getKitNumberColourShort(db.player[idx].kit,db.player[idx].kitType,db.player[idx].shortsNumberColor,db.player[idx].tournid)
	db.player[idx].jerseyNameLayout = getKitNameCurve(db.player[idx].kit,db.player[idx].kitType,db.player[idx].jerseyNameLayout,db.player[idx].tournid)
	db.player[idx].jerseyCollarType = getKitCollar(db.player[idx].kit,db.player[idx].kitType,db.player[idx].jerseyCollarType,db.player[idx].tournid)
	db.player[idx].jerseyfit = getKitFit(db.player[idx].kit,db.player[idx].kitType,db.player[idx].jerseyfit,db.player[idx].tournid,db.player[idx].playerassetid)
	--KIT DETAILS ARENA/TRAINING/ALT GK
	if (db.player[idx].speckitType ~= -1) then
	db.player[idx].kitNameFont = getKitNameFont(db.player[idx].kit,db.player[idx].speckitType,db.player[idx].kitNameFont,db.player[idx].tournid,db.player[idx].teamid)
	db.player[idx].kitNumberFont = getKitNumberSet(db.player[idx].kit,db.player[idx].speckitType,db.player[idx].kitNumberFont,db.player[idx].tournid,db.player[idx].teamid)
	db.player[idx].kitNumberColor = getKitNumberColourShirt(db.player[idx].kit,db.player[idx].speckitType,db.player[idx].kitNumberColor,db.player[idx].tournid)
	db.player[idx].kitNameColor = getKitNameColour(db.player[idx].kit,db.player[idx].speckitType,db.player[idx].kitNameColor,db.player[idx].tournid)
	db.player[idx].shortsNumberFont = getKitNumberSet(db.player[idx].kit,db.player[idx].speckitType,db.player[idx].shortsNumberFont,db.player[idx].tournid,db.player[idx].teamid)
	db.player[idx].shortsNumberColor = getKitNumberColourShort(db.player[idx].kit,db.player[idx].speckitType,db.player[idx].shortsNumberColor,db.player[idx].tournid)
	db.player[idx].jerseyNameLayout = getKitNameCurve(db.player[idx].kit,db.player[idx].speckitType,db.player[idx].jerseyNameLayout,db.player[idx].tournid)
	db.player[idx].jerseyCollarType = getKitCollar(db.player[idx].kit,db.player[idx].speckitType,db.player[idx].jerseyCollarType,db.player[idx].tournid)
	db.player[idx].jerseyfit = getKitFit(db.player[idx].kit,db.player[idx].speckitType,db.player[idx].jerseyfit,db.player[idx].tournid,db.player[idx].playerassetid)
	end

	--REF KIT COLLAR
	if (db.player[idx].kitType == 5) then
	db.player[idx].jerseyCollarType = getTournamentRefereeKitsCollar(db.player[idx].tournid,db.player[idx].jerseyCollarType,db.player[idx].homeKitTeamID)
	end
	
	--GK PANTS
	db.player[idx].shortstyle = getGKPants(db.player[idx].playerassetid,db.player[idx].shortstyle)
	
	--JERSEY TUCK
	db.player[idx].jerseyTucked = getJerseyTuck(db.player[idx].playerassetid,db.player[idx].jerseyTucked)
	
	--SOCK HEIGHT
	db.player[idx].socklength = getSockHeight(db.player[idx].playerassetid,db.player[idx].socklength)
	

	--RANDOM BOOT
	db.player[idx].bootrand = math.random(0,3)
	
	--CROWD FIX
	db.player[idx].crowdkitType = db.player[idx].kitType
	if ((db.player[idx].speckitType > -1) and (db.player[idx].speckitType < 91)) then
	ddb.player[idx].crowdkitType = db.player[idx].speckitType
	end
	
		
end

---------------------------------------------------------------------------------------------------
-- Get hair color for generic hair recoloring 
function GetHairColorARGB(idx)
	hairColourList = { 0x00edac57, 0x000e0e0d, 0x00a07741, 0x001f160e, 
                       0x00ffd286, 0x006e4d2b, 0x003b2816, 0x00782c08, 
                       0x00c8c9cf, 0x00525355, 0x00345a34, 0x00263f67,
                       0x00f62f0a }

	local clr = 0x00FF0000
	if (db.player[idx].headClass > 0) then
		clr = hairColourList[db.player[idx].hairColor + 1]
		if (clr == nil) then
			clr = 0x00FF0000
		end
	else
		clr = 0x00808080
	end
	return clr
end

---------------------------------------------------------------------------------------------------
function EASWMorphFile(idx)
	if (db.player[idx].isVirtualPro == 1) then
		return string.format("data/easw/deltas_%d.easwmorph", db.player[idx].faceType) --12366)
	else
		return "dummyasset.easwmorph"
	end
end

---------------------------------------------------------------------------------------------------
function GetHeadAndHairAssetType(idx)
	if (db.player[idx].isCreatePlayer == 1) then
		return "cloneasset"
	else
		return ""
	end
end

---------------------------------------------------------------------------------------------------
function MorphFile(idx)
	if (db.player[idx].isCreatePlayer == 1 and db.player[idx].isVirtualPro == 0) then
		return "data/sceneassets/createplayer/head_0_2_0_1_morphtargets.rx3"
	else
		return "dummyasset.rx3"
	end
end

---------------------------------------------------------------------------------------------------
function PlayerAssetShadow(player, lod)
	local gr = gRenderables
	gr:AddAsset(player, lod, "shadow", "data/sceneassets/body/playershadow_5_1_0.rx3")
	gr:CreateMaterial(player, lod, "shadow", "missingShader.fx")
end

---------------------------------------------------------------------------------------------------
function GetCrestAsset(idx)
	if (db.player[idx].isCreationZone == 1) then
		if (db.player[idx].hasCzCrestImage > 0) then
			-- // The "\\" is so that the mounted image can be found properly by Apt
			return "data\\ugc/cz_crest/${db.player[?].crestAssetId}.png;data/ugc/cz_crest/1.png"
		else
			return "data/sceneassets/crest/crest_${db.player[?].crestAssetId}.rx3"
		end
	else
		return GetRMKit(idx,1)
	end

end

---------------------------------------------------------------------------------------------------
function GetSkinTone(idx, lod)
	local useLight = false
	if (db.player[idx].isVirtualPro == 1) then
		-- // 0 is light, 1 is dark
		useLight = ( db.player[idx].faceGenSkinToneType == 0 )
	else
		useLight = ( db.player[idx].bodySkinToneType < 5 )
	end
	
	if ( useLight ) then
		return ( lod == 0 ) and "skin_light" or "skin_lod_light"
	else
		return ( lod == 0 ) and "skin_dark" or "skin_lod_dark"
	end
end

---------------------------------------------------------------------------------------------------
function GetFaceTone(idx, lod)
	local useLight = false
	if (db.player[idx].isVirtualPro == 1) then
		-- // 0 is light, 1 is dark
		useLight = ( db.player[idx].faceGenSkinToneType == 0 )
	else
		useLight = ( db.player[idx].bodySkinToneType < 5 )
	end
	
	if ( useLight ) then
		return ( lod == 0 ) and "face_light" or "face_lod_light"
	else
		return ( lod == 0 ) and "face_dark" or "face_lod_dark"
	end
end

---------------------------------------------------------------------------------------------------
function PlayerAssetHighLod(player, lod)
	local gr = gRenderables
	
	local as = gSportsRNA
	local settingTable = as:GetTable("Settings")
	
	local levelOfDetail = as:GetString(settingTable, "LevelOfDetail") 
	
    -- GEO's
	if (levelOfDetail == "low" or levelOfDetail == "superlow") then
		gr:AddAsset(player, lod, "jerseybnmlod", "data/sceneassets/kit/jersey_${db.player[?].jerseyTucked}_${db.player[?].jerseyfit}_bnm_lod.rx3")
		gr:AddAsset(player, lod, "shortsbnmlod", "data/sceneassets/kit/shorts_${db.player[?].shortstyle}_bnm_lod.rx3")		
	end
	gr:AddAsset(player, lod, "jersey", "data/sceneassets/body/jersey_1_${db.player[?].jerseyCollarType}_${db.player[?].jerseySleeveLength}_${db.player[?].jerseyArmBand}_${db.player[?].jerseyTucked}_${db.player[?].jerseyfit}.rx3")
	gr:AddAsset(player, lod, "arms", "data/sceneassets/body/arms_1_${db.player[?].armLength}.rx3")
	gr:AddAsset(player, lod, "shorts", "data/sceneassets/body/shorts_1_${db.player[?].shortstyle}.rx3")
	gr:AddAsset(player, lod, "shoes",  "${GetRMBoot(?,0)}data/sceneassets/shoe/shoe_${db.player[?].shoeType}.rx3")
	gr:AddAsset(player, lod, "socks", "data/sceneassets/body/sock_1_${db.player[?].socklength}.rx3")
	gr:AddAsset(player, lod, "legs",  "data/sceneassets/body/legs_1_${db.player[?].socklength}.rx3")
	gr:AddAssetEx(player, lod, "${GetHeadAndHairAssetType(?)}", "hair1", "data/sceneassets/hair/hair_${db.player[?].playerassetid}_0_0.rx3;data/sceneassets/hair/hair_${db.player[?].hair}_${db.player[?].faceProxyHeadClass}_0.rx3")
	gr:AddAssetEx(player, lod, "${GetHeadAndHairAssetType(?)}", "hair2", "data/sceneassets/hair/hair_${db.player[?].playerassetid}_0_0.rx3;data/sceneassets/hair/hair_${db.player[?].hair}_${db.player[?].faceProxyHeadClass}_0.rx3")
	gr:AddAssetEx(player, lod, "${GetHeadAndHairAssetType(?)}", "head",  "data/sceneassets/heads/head_${db.player[?].playerassetid}_0.rx3;data/sceneassets/heads/head_${db.player[?].head}_${db.player[?].headClass}.rx3")
    -- Point the eyes to the same asset the head uses, whether it being cloned or not.
	gr:AddAssetEx(player, lod, "fromasset", "eyes", "head")
	-- non volume texture normalMap for socks
	gr:AddAsset(player, lod, "sockbnmlod", "data/sceneassets/kit/shorts_${db.player[?].shortstyle}_bnm_lod.rx3")
	
	-- Add the morph files
	
	local priority = 0	-- default priority
	local temporary = 1	-- mark as temporary asset
	gr:AddAsset(player, lod, "morphhead", "${MorphFile(?)}", priority, temporary)
	gr:AddAsset(player, lod, "easwmorph", "${EASWMorphFile(?)}")
	
	local part = "jersey"
	if (levelOfDetail == "low" or levelOfDetail == "superlow") then
		gr:CreateMaterialFromAttribulator(player, lod, part, "jersey_material", "${AttribMaterial('jerseylod')}" )
		gr:SetTexture(player, lod, part, "textures", "wrinkleMap", "jerseybnmlod", "jersey_nm")
	else
		gr:CreateMaterialFromAttribulator(player, lod, part, "jersey_material", "${AttribMaterial('jersey')}" )
		gr:SetTexture(player, lod, part, "textures", "wrinkleMap", "jerseybnm", "jersey_nm")
	end
	gr:SetTexture(player, lod, part, "textures", "diffuseTexture", "jersey_tex", "jersey_cm")
	gr:SetTexture(player, lod, part, "textures", "specularMap", "kittex_common", "jersey_coeff")
	gr:SetTexture(player, lod, part, "textures", "occlusionMap", "jerseyam", "jersey_am")
	gr:SetTexture(player, lod, part, "textures", "envDiffuseTexture", "charcmn", "envd_")
	gr:SetTexture(player, lod, part, "textures", "envSpecTexture", "charcmn", "envs_")
	gr:SetTexture(player, lod, part, "textures", "weavePatternNormalMap", "cmnweave", "kit_weave")
	gr:SetTexture(player, lod, part, "textures", "jerseyNumberShirt_Tens", "kitnumbers", "${db.player[?].kitNumberTens}.Raster")
	gr:SetTexture(player, lod, part, "textures", "jerseyNumberShirt_Units", "kitnumbers", "${db.player[?].kitNumberUnits}.Raster")
	gr:SetTexture(player, lod, part, "textures", "tex_decalmap", "cresttex", "${db.player[?].crestTexName}")
	gr:SetTextureFromRuntime(player, lod, part, "textures", "jerseyName", "namestamp${?}")
	gr:SetTextureFromRuntime(player, lod, part, "textures", "coverageMap", "covmap_${db.player[?].stadiumID}_${db.player[?].stadiumLightID}")
	
	gr:SetHotSpot(player, lod, part, "global", "jersey_decalBounds", "kithotspots", "jersey", "team")
	gr:SetHotSpot(player, lod, part, "global", "jersey_nameBound", "kithotspots", "jersey", "name")
	gr:SetHotSpot(player, lod, part, "global", "jersey_nameArcCenter", "kithotspots", "jersey", "name_arccenter")
		
	gr:SetConstantInt(player, lod, part, "global", "jersey_nameLayout", "${db.player[?].jerseyNameLayout}")
	
	gr:SetConstantARGB(player, lod, part, "global", "jersey_nameColor", "${db.player[?].kitNameColor}")

	skinTone = "${GetSkinTone(?,"..lod..")}"
	faceTone = "${GetFaceTone(?,"..lod..")}"
	
	part = "arms"
	gr:CreateMaterialFromAttribulator(player, lod, part, "head_material",  skinTone)
	gr:SetTexture(player, lod, part, "textures", "diffuseTexture", "skintex", "body_")  
	gr:SetTexture(player, lod, part, "textures", "envDiffuseTexture", "charcmn", "envd_")
	gr:SetTexture(player, lod, part, "textures", "envSpecTexture", "charcmn", "envs_")
	gr:SetTexture(player, lod, part, "textures", "normalMap", "bodycmn", "body_nm")
	gr:SetTexture(player, lod, part, "textures", "coeffMap", "bodycmn", "body_coeff")
	gr:SetConstantARGB(player, lod, part, "global", "skincolor", "${db.player[?].playerBodySkinColor}")
	gr:SetTextureFromRuntime(player, lod, part, "textures", "coverageMap", "covmap_${db.player[?].stadiumID}_${db.player[?].stadiumLightID}")

	part = "legs"
	gr:CreateMaterialFromAttribulator(player, lod, part, "head_material", skinTone )
	gr:SetTexture(player, lod, part, "textures", "diffuseTexture", "skintex", "body_")	
	gr:SetTexture(player, lod, part, "textures", "envDiffuseTexture", "charcmn", "envd_")
	gr:SetTexture(player, lod, part, "textures", "envSpecTexture", "charcmn", "envs_")
	gr:SetTexture(player, lod, part, "textures", "normalMap", "bodycmn", "body_nm")
	gr:SetTexture(player, lod, part, "textures", "coeffMap", "bodycmn", "body_coeff")
	gr:SetConstantARGB(player, lod, part, "global", "skincolor", "${db.player[?].playerBodySkinColor}")
	gr:SetTextureFromRuntime(player, lod, part, "textures", "coverageMap", "covmap_${db.player[?].stadiumID}_${db.player[?].stadiumLightID}")

	part = "shorts"
	if (levelOfDetail == "low" or levelOfDetail == "superlow") then
		gr:CreateMaterialFromAttribulator(player, lod, part, "shorts_material", "${AttribMaterial('shortslod')}" )
		gr:SetTexture(player, lod, part, "textures", "wrinkleMap", "shortsbnmlod", "shorts_nm")
	else
		gr:CreateMaterialFromAttribulator(player, lod, part, "shorts_material", "${AttribMaterial('shorts')}") 
		gr:SetTexture(player, lod, part, "textures", "wrinkleMap", "shortsbnm", "shorts_nm")
	end
	gr:SetTexture(player, lod, part, "textures", "diffuseTexture", "shortstex", "shorts_cm")
	gr:SetTexture(player, lod, part, "textures", "specularMap", "shortstex", "shorts_coeff")
	gr:SetTexture(player, lod, part, "textures", "occlusionMap", "shortsam", "shorts_am")
	gr:SetTexture(player, lod, part, "textures", "envDiffuseTexture", "charcmn", "envd_")
	gr:SetTexture(player, lod, part, "textures", "envSpecTexture", "charcmn", "envs_")
	gr:SetTexture(player, lod, part, "textures", "weavePatternNormalMap", "cmnweave", "kit_weave")
	gr:SetTexture(player, lod, part, "textures", "tex_decalmap", "cresttex", "${db.player[?].crestTexName}")	
	gr:SetTexture(player, lod, part, "textures", "jerseyNumberShorts_Tens", "shortsnumbers", "${db.player[?].kitNumberTens}.Raster")
	gr:SetTexture(player, lod, part, "textures", "jerseyNumberShorts_Units", "shortsnumbers", "${db.player[?].kitNumberUnits}.Raster")
	gr:SetHotSpot(player, lod, part, "global", "shorts_decalBounds", "shortshotspots", "shorts", "team")
	gr:SetTextureFromRuntime(player, lod, part, "textures", "coverageMap", "covmap_${db.player[?].stadiumID}_${db.player[?].stadiumLightID}")

	gr:SetConstantARGB(player, lod, part, "global", "jerseyCustomColorPri", "${db.player[?].kitColourShortPri}")
	gr:SetConstantARGB(player, lod, part, "global", "jerseyCustomColorSec", "${db.player[?].kitColourShortSec}")
	gr:SetConstantARGB(player, lod, part, "global", "jerseyCustomColorTer", "${db.player[?].kitColourShortTer}")

	-- head
	part = "head"
	gr:CreateMaterialFromAttribulator(player, lod, part, "head_material", faceTone )
	gr:SetSubMesh(player, lod, part, "head")
	gr:SetTexture(player, lod, part, "textures", "diffuseTexture", "facetex", "head_")
	gr:SetTexture(player, lod, part, "textures", "normalMap", "headcmn", "head_0_nm")
	gr:SetTexture(player, lod, part, "textures", "coeffMap", "headcmn", "head_coeff")
	gr:SetTexture(player, lod, part, "textures", "envDiffuseTexture", "charcmn", "envd_")
	gr:SetTexture(player, lod, part, "textures", "envSpecTexture", "charcmn", "envs_")
	gr:SetTexture(player, lod, part, "textures", "wrinkleMap", "headcmn", "head_nm_wkl.Raster")
	gr:SetTexture(player, lod, part, "textures", "wrinkleMask", "headcmn", "head_nm_mask.Raster")
	gr:SetTextureFromRuntime(player, lod, part, "textures", "coverageMap", "covmap_${db.player[?].stadiumID}_${db.player[?].stadiumLightID}")

	-- Eyes
	part = "eyes"	 
	gr:CreateMaterialFromAttribulator(player, lod, part, "eye_material", "${AttribMaterial('eyes')}" )
	gr:SetSubMesh(player, lod, part, "eyes")
	gr:SetTexture(player, lod, part, "textures", "diffuseTexture", "eyetex", "eyes_")
	gr:SetTexture(player, lod, part, "textures", "coeffMap", "headcmn", "eye_coeff")
	gr:SetTexture(player, lod, part, "textures", "normalMap", "headcmn", "eyes_nm")
	gr:SetTexture(player, lod, part, "textures", "envDiffuseTexture", "charcmn", "envd_")
	gr:SetTexture(player, lod, part, "textures", "envSpecTexture", "charcmn", "env_")
	gr:SetTextureFromRuntime(player, lod, part, "textures", "coverageMap", "covmap_${db.player[?].stadiumID}_${db.player[?].stadiumLightID}")

	-- hair1
	part = "hair1"
	gr:CreateMaterialFromAttribulator(player, lod, part, "hair_material", "${AttribMaterial('player_hair_kk_alphaA')}")
	gr:SetSubMesh(player, lod, part, "alphaA")
	gr:SetPriority(player, lod, part, 1)             -- haircap render before hair 2
	gr:SetTexture(player, lod, part, "textures", "diffuseTexture", "hairtex", "hair_cm") 
	gr:SetTexture(player, lod, part, "textures", "specAlphaMap", "hairtex", "hair_coeff")
	gr:SetTexture(player, lod, part, "textures", "envDiffuseTexture", "charcmn", "envd_")
	gr:SetConstantARGB(player, lod, part, "global", "hairColor", "${GetHairColorARGB(?)}")
	gr:SetTextureFromRuntime(player, lod, part, "textures", "coverageMap", "covmap_${db.player[?].stadiumID}_${db.player[?].stadiumLightID}")

	-- hair2
	part = "hair2"
	gr:CreateMaterialFromAttribulator(player, lod, part, "hair_material2", "${AttribMaterial('player_hair_kk_alphaB')}")
	gr:SetSubMesh(player, lod, part, "alphaB")
	gr:SetPriority(player, lod, part, 2)             -- strands render after hair 1
	gr:SetSelfShadowAlpha(player, lod, part)          -- will use different self shadow shader that accounts for alpha
	--gr:DisableSelfShadow(player, lod, part)          -- Don't render the hairstrands into the selfshadow.
	gr:SetTexture(player, lod, part, "textures", "diffuseTexture", "hairtex", "hair_cm")
	gr:SetTexture(player, lod, part, "textures", "specAlphaMap", "hairtex", "hair_coeff")
	gr:SetTexture(player, lod, part, "textures", "envDiffuseTexture", "charcmn", "envd_")
	gr:SetConstantARGB(player, lod, part, "global", "hairColor", "${GetHairColorARGB(?)}")
	gr:SetTextureFromRuntime(player, lod, part, "textures", "coverageMap", "covmap_${db.player[?].stadiumID}_${db.player[?].stadiumLightID}")

	-- socks
	part = "socks"
	if (levelOfDetail == "low" or levelOfDetail == "superlow") then
		gr:CreateMaterialFromAttribulator(player, lod, part, "socks_material", "${AttribMaterial('socklod')}" )
	else
		gr:CreateMaterialFromAttribulator(player, lod, part, "socks_material", "${AttribMaterial('sock')}" )  
	end
	gr:SetTexture(player, lod, part, "textures", "diffuseTexture", "shortstex", "shorts_cm")
	gr:SetTexture(player, lod, part, "textures", "specularMap", "shortstex", "shorts_coeff")
	gr:SetTexture(player, lod, part, "textures", "occlusionMap", "shortsam", "shorts_am")
	gr:SetTexture(player, lod, part, "textures", "envDiffuseTexture", "charcmn", "envd_")
	gr:SetTexture(player, lod, part, "textures", "envSpecTexture", "charcmn", "envs_")
	gr:SetTexture(player, lod, part, "textures", "weavePatternNormalMap", "cmnweave", "kit_weave")
	gr:SetTexture(player, lod, part, "textures", "wrinkleMap", "sockbnmlod", "shorts_nm")
	gr:SetTextureFromRuntime(player, lod, part, "textures", "coverageMap", "covmap_${db.player[?].stadiumID}_${db.player[?].stadiumLightID}")

	gr:SetConstantARGB(player, lod, part, "global", "jerseyCustomColorPri", "${db.player[?].kitColourSocksPri}")
	gr:SetConstantARGB(player, lod, part, "global", "jerseyCustomColorSec", "${db.player[?].kitColourSocksSec}")
	gr:SetConstantARGB(player, lod, part, "global", "jerseyCustomColorTer", "${db.player[?].kitColourSocksTer}")

	-- shoes
	part = "shoes"
	if (levelOfDetail == "low" or levelOfDetail == "superlow") then
		gr:CreateMaterialFromAttribulator(player, lod, part, "body_material", "${AttribMaterial('shoelod')}" )
	else
		gr:CreateMaterialFromAttribulator(player, lod, part, "shoe_material", "${AttribMaterial('shoe')}" )
	end
	gr:SetTexture(player, lod, part, "textures", "diffuseTexture", "shoetex", "shoe_cm")	
	gr:SetTexture(player, lod, part, "textures", "normalMap", "shoetex", "shoe_nm")
	gr:SetTexture(player, lod, part, "textures", "coeffMap", "shoetex", "shoe_coeff")	
	gr:SetTexture(player, lod, part, "textures", "envDiffuseTexture", "charcmn", "envd_")
	gr:SetTexture(player, lod, part, "textures", "envSpecTexture", "charcmn", "envs_")
	gr:SetTextureFromRuntime(player, lod, part, "textures", "coverageMap", "covmap_${db.player[?].stadiumID}_${db.player[?].stadiumLightID}")

	--gr:SetConstantARGB(player, lod, part, "global", "customColorPri", "${db.player[?].shoeColorPri}")
	--gr:SetConstantARGB(player, lod, part, "global", "customColorSec", "${db.player[?].shoeColorSec}")
	--gr:SetConstantARGB(player, lod, part, "global", "customColorTer", "${db.player[?].shoeColorTer}")
	
	--underarmor neck
	local part = "underneck"
	if (levelOfDetail == "low" or levelOfDetail == "superlow") then
		gr:CreateMaterialFromAttribulator(player, lod, part, "underneck_material", "${AttribMaterial('socklod')}" )
	else
		gr:CreateMaterialFromAttribulator(player, lod, part, "underneck_material", "${AttribMaterial('undergear')}" )
	end
	gr:SetTexture(player, lod, part, "textures", "diffuseTexture", "jersey_tex", "jersey_cm")
	gr:SetTexture(player, lod, part, "textures", "specularMap", "kittex_common", "jersey_coeff")
	gr:SetTexture(player, lod, part, "textures", "occlusionMap", "jerseyam", "jersey_am")
	gr:SetTexture(player, lod, part, "textures", "envDiffuseTexture", "charcmn", "envd_")
	gr:SetTexture(player, lod, part, "textures", "envSpecTexture", "charcmn", "envs_")
	gr:SetTexture(player, lod, part, "textures", "weavePatternNormalMap", "cmnweave", "kit_weave")
	gr:SetTexture(player, lod, part, "textures", "wrinkleMap", "jerseybnm", "jersey_nm")
	gr:SetTextureFromRuntime(player, lod, part, "textures", "coverageMap", "covmap_${db.player[?].stadiumID}_${db.player[?].stadiumLightID}")

	gr:SetConstantARGB(player, lod, part, "global", "jerseyCustomColorPri", "${db.player[?].kitColourMaskPri}")
	gr:SetConstantARGB(player, lod, part, "global", "jerseyCustomColorSec", "${db.player[?].kitColourMaskSec}")
	gr:SetConstantARGB(player, lod, part, "global", "jerseyCustomColorTer", "${db.player[?].kitColourMaskTer}")
	
	--underarmor sleeves
	local part = "underarms"
	if (levelOfDetail == "low" or levelOfDetail == "superlow") then
		gr:CreateMaterialFromAttribulator(player, lod, part, "underarms_material", "${AttribMaterial('socklod')}" )
	else
		gr:CreateMaterialFromAttribulator(player, lod, part, "underarms_material", "${AttribMaterial('undergear')}" )
	end
	gr:SetTexture(player, lod, part, "textures", "diffuseTexture", "jersey_tex", "jersey_cm")
	gr:SetTexture(player, lod, part, "textures", "specularMap", "kittex_common", "jersey_coeff")
	gr:SetTexture(player, lod, part, "textures", "occlusionMap", "jerseyam", "jersey_am")
	gr:SetTexture(player, lod, part, "textures", "envDiffuseTexture", "charcmn", "envd_")
	gr:SetTexture(player, lod, part, "textures", "envSpecTexture", "charcmn", "envs_")
	gr:SetTexture(player, lod, part, "textures", "weavePatternNormalMap", "cmnweave", "kit_weave")
	gr:SetTexture(player, lod, part, "textures", "wrinkleMap", "jerseybnm", "jersey_nm")
	gr:SetTextureFromRuntime(player, lod, part, "textures", "coverageMap", "covmap_${db.player[?].stadiumID}_${db.player[?].stadiumLightID}")
	
	gr:SetConstantARGB(player, lod, part, "global", "jerseyCustomColorPri", "${db.player[?].kitColourMaskPri}")
	gr:SetConstantARGB(player, lod, part, "global", "jerseyCustomColorSec", "${db.player[?].kitColourMaskSec}")
	gr:SetConstantARGB(player, lod, part, "global", "jerseyCustomColorTer", "${db.player[?].kitColourMaskTer}")

	--undershorts
	local part = "undershorts"
	if (levelOfDetail == "low" or levelOfDetail == "superlow") then
		gr:CreateMaterialFromAttribulator(player, lod, part, "undershorts_material", "${AttribMaterial('socklod')}")
	else
		gr:CreateMaterialFromAttribulator(player, lod, part, "undershorts_material", "${AttribMaterial('undergear')}") 
	end
	gr:SetTexture(player, lod, part, "textures", "diffuseTexture", "shortstex", "shorts_cm")
	gr:SetTexture(player, lod, part, "textures", "specularMap", "shortstex", "shorts_coeff")
	gr:SetTexture(player, lod, part, "textures", "occlusionMap", "shortsam", "shorts_am")
	gr:SetTexture(player, lod, part, "textures", "wrinkleMap", "shortsbnm", "shorts_nm")
	gr:SetTexture(player, lod, part, "textures", "envDiffuseTexture", "charcmn", "envd_")
	gr:SetTexture(player, lod, part, "textures", "envSpecTexture", "charcmn", "envs_")
	gr:SetTexture(player, lod, part, "textures", "weavePatternNormalMap", "cmnweave", "kit_weave")
	gr:SetTextureFromRuntime(player, lod, part, "textures", "coverageMap", "covmap_${db.player[?].stadiumID}_${db.player[?].stadiumLightID}")

	gr:SetConstantARGB(player, lod, part, "global", "jerseyCustomColorPri", "${db.player[?].kitColourShortPri}")
	gr:SetConstantARGB(player, lod, part, "global", "jerseyCustomColorSec", "${db.player[?].kitColourShortSec}")
	gr:SetConstantARGB(player, lod, part, "global", "jerseyCustomColorTer", "${db.player[?].kitColourShortTer}")
	
end

---------------------------------------------------------------------------------------------------
function PlayerAssetLowLod(player, lod)
	local gr = gRenderables

	skinLodTone = "${GetSkinTone(?,"..lod..")}"
	faceLodTone = "${GetFaceTone(?,"..lod..")}"


	-- GEO's
	if (lod == 1) then
		gr:AddAsset(player, lod, "jersey", "data/sceneassets/body/jersey_2_${db.player[?].jerseyCollarType}_${db.player[?].jerseySleeveLength}_${db.player[?].jerseyArmBand}_${db.player[?].jerseyTucked}_${db.player[?].jerseyfit}.rx3")
		gr:AddAsset(player, lod, "shorts", "data/sceneassets/body/shorts_2_${db.player[?].shortstyle}.rx3")
		gr:AddAsset(player, lod, "bodyskinlod", "data/sceneassets/body/body_2_${db.player[?].armLength}_0.rx3")
		gr:AddAsset(player, lod, "bodyshoelod", "data/sceneassets/body/shoes_2_0.rx3")
		gr:AddAsset(player, lod, "bodysocklod", "data/sceneassets/body/socks_2_${db.player[?].socklength}.rx3")
		gr:AddAsset(player, lod, "headlod",  "data/sceneassets/body/head_2.rx3")
		gr:AddAsset(player, lod, "eyeslod",  "data/sceneassets/body/head_2.rx3")
	elseif (lod == 2) then
		gr:AddAsset(player, lod, "jersey", "data/sceneassets/body/jersey_3_${db.player[?].jerseySleeveLength}_${db.player[?].jerseyArmBand}_${db.player[?].jerseyTucked}.rx3")
		gr:AddAsset(player, lod, "shorts", "data/sceneassets/body/shorts_3_${db.player[?].shortstyle}.rx3")
		gr:AddAsset(player, lod, "bodyskinlod", "data/sceneassets/body/body_3_${db.player[?].armLength}_0.rx3")
		gr:AddAsset(player, lod, "bodyshoelod", "data/sceneassets/body/shoes_3_0.rx3")
		gr:AddAsset(player, lod, "bodysocklod", "data/sceneassets/body/socks_3_${db.player[?].socklength}.rx3")
		gr:AddAsset(player, lod, "headlod",  "data/sceneassets/body/head_3.rx3")
		gr:AddAsset(player, lod, "eyeslod",  "data/sceneassets/body/head_3.rx3")
	end
	
	gr:AddAsset(player, lod, "hairlod",  "data/sceneassets/hairlod/hairlod_${db.player[?].playerassetid}_0_0.rx3;data/sceneassets/hairlod/hairlod_${db.player[?].hair}_${db.player[idx].faceProxyHeadClass}_0.rx3")
	gr:AddAsset(player, lod, "jerseybnmlod", "data/sceneassets/kit/jersey_${db.player[?].jerseyTucked}_${db.player[?].jerseyfit}_bnm_lod.rx3")
	gr:AddAsset(player, lod, "shortsbnmlod", "data/sceneassets/kit/shorts_${db.player[?].shortstyle}_bnm_lod.rx3")

	local part = "jersey"
	gr:CreateMaterialFromAttribulator(player, lod, part, "jersey_material", "${AttribMaterial('jerseylod')}" )
	gr:SetTexture(player, lod, part, "textures", "diffuseTexture", "jersey_tex", "jersey_cm")
	gr:SetTexture(player, lod, part, "textures", "specularMap", "kittex_common", "jersey_coeff")
	gr:SetTexture(player, lod, part, "textures", "occlusionMap", "jerseyam", "jersey_am")
	gr:SetTexture(player, lod, part, "textures", "envDiffuseTexture", "charcmn", "envd_")
	gr:SetTexture(player, lod, part, "textures", "envSpecTexture", "charcmn", "envs_")
	gr:SetTexture(player, lod, part, "textures", "weavePatternNormalMap", "cmnweave", "kit_weave")
	gr:SetTexture(player, lod, part, "textures", "wrinkleMap", "jerseybnmlod", "jersey_nm")
	gr:SetTexture(player, lod, part, "textures", "jerseyNumberShirt_Tens", "kitnumbers", "${db.player[?].kitNumberTens}.Raster")
	gr:SetTexture(player, lod, part, "textures", "jerseyNumberShirt_Units", "kitnumbers", "${db.player[?].kitNumberUnits}.Raster")
	gr:SetTexture(player, lod, part, "textures", "tex_decalmap", "cresttex", "${db.player[?].crestTexName}")
	gr:SetTextureFromRuntime(player, lod, part, "textures", "jerseyName", "namestamp${?}")
	gr:SetTextureFromRuntime(player, lod, part, "textures", "coverageMap", "covmap_${db.player[?].stadiumID}_${db.player[?].stadiumLightID}")
	
	gr:SetHotSpot(player, lod, part, "global", "jersey_decalBounds", "kithotspots", "jersey", "team")
	gr:SetHotSpot(player, lod, part, "global", "jersey_nameBound", "kithotspots", "jersey", "name")
	gr:SetHotSpot(player, lod, part, "global", "jersey_nameArcCenter", "kithotspots", "jersey", "name_arccenter")
	gr:SetConstantInt(player, lod, part, "global", "jersey_nameLayout", "${db.player[?].jerseyNameLayout}")
	
	gr:SetConstantARGB(player, lod, part, "global", "jersey_nameColor", "${db.player[?].kitNameColor}")

	part = "shorts"
	gr:CreateMaterialFromAttribulator(player, lod, part, "body_material", "${AttribMaterial('shortslod')}" )
	gr:SetTexture(player, lod, part, "textures", "diffuseTexture", "shortstex", "shorts_cm")
	gr:SetTexture(player, lod, part, "textures", "specularMap", "shortstex", "shorts_coeff")
	gr:SetTexture(player, lod, part, "textures", "occlusionMap", "shortsam", "shorts_am")
	gr:SetTexture(player, lod, part, "textures", "wrinkleMap", "shortsbnmlod", "shorts_nm")
	gr:SetTexture(player, lod, part, "textures", "envDiffuseTexture", "charcmn", "envd_")
	gr:SetTexture(player, lod, part, "textures", "envSpecTexture", "charcmn", "envs_")
	gr:SetTexture(player, lod, part, "textures", "weavePatternNormalMap", "cmnweave", "kit_weave")
	gr:SetTexture(player, lod, part, "textures", "tex_decalmap", "cresttex", "${db.player[?].crestTexName}")
	gr:SetTexture(player, lod, part, "textures", "jerseyNumberShorts_Tens", "shortsnumbers", "${db.player[?].kitNumberTens}.Raster")
	gr:SetTexture(player, lod, part, "textures", "jerseyNumberShorts_Units", "shortsnumbers", "${db.player[?].kitNumberUnits}.Raster")
	gr:SetHotSpot(player, lod, part, "global", "shorts_decalBounds", "shortshotspots", "shorts", "team")
	gr:SetTextureFromRuntime(player, lod, part, "textures", "coverageMap", "covmap_${db.player[?].stadiumID}_${db.player[?].stadiumLightID}")

	gr:SetConstantARGB(player, lod, part, "global", "jerseyCustomColorPri", "${db.player[?].kitColourShortPri}")
	gr:SetConstantARGB(player, lod, part, "global", "jerseyCustomColorSec", "${db.player[?].kitColourShortSec}")
	gr:SetConstantARGB(player, lod, part, "global", "jerseyCustomColorTer", "${db.player[?].kitColourShortTer}")

	part = "bodyskinlod"
	gr:CreateMaterialFromAttribulator(player, lod, part, "body_material", skinLodTone )
	gr:SetTexture(player, lod, part, "textures", "diffuseTexture", "skintex", "body_")  
	gr:SetTexture(player, lod, part, "textures", "envDiffuseTexture", "charcmn", "envd_")
	gr:SetTexture(player, lod, part, "textures", "envSpecTexture", "charcmn", "envs_")
	gr:SetTexture(player, lod, part, "textures", "normalMap", "bodycmn", "body_nm")
	gr:SetTexture(player, lod, part, "textures", "coeffMap", "bodycmn", "body_coeff")
	gr:SetConstantARGB(player, lod, part, "global", "skincolor", "${db.player[?].playerBodySkinColor}")
	gr:SetTextureFromRuntime(player, lod, part, "textures", "coverageMap", "covmap_${db.player[?].stadiumID}_${db.player[?].stadiumLightID}")

	part = "bodyshoelod"
	gr:CreateMaterialFromAttribulator(player, lod, part, "body_material", "${AttribMaterial('shoelod')}" )
	gr:SetTexture(player, lod, part, "textures", "diffuseTexture", "shoetex", "shoe_cm")	
	gr:SetTexture(player, lod, part, "textures", "normalMap", "shoetex", "shoe_nm")
	gr:SetTexture(player, lod, part, "textures", "coeffMap", "shoetex", "shoe_coeff")
	gr:SetTexture(player, lod, part, "textures", "envDiffuseTexture", "charcmn", "envd_")	
	gr:SetTexture(player, lod, part, "textures", "envSpecTexture", "charcmn", "envs_")
	gr:SetTextureFromRuntime(player, lod, part, "textures", "coverageMap", "covmap_${db.player[?].stadiumID}_${db.player[?].stadiumLightID}")

	--gr:SetConstantARGB(player, lod, part, "global", "customColorPri", "${db.player[?].shoeColorPri}")
	--gr:SetConstantARGB(player, lod, part, "global", "customColorSec", "${db.player[?].shoeColorSec}")
	--gr:SetConstantARGB(player, lod, part, "global", "customColorTer", "${db.player[?].shoeColorTer}")

	part = "bodysocklod"
	gr:CreateMaterialFromAttribulator(player, lod, part, "body_material", "${AttribMaterial('socklod')}" )
	gr:SetTexture(player, lod, part, "textures", "diffuseTexture", "shortstex", "shorts_cm")
	gr:SetTexture(player, lod, part, "textures", "specularMap", "shortstex", "shorts_coeff")
	gr:SetTexture(player, lod, part, "textures", "occlusionMap", "shortsam", "shorts_am")
	gr:SetTexture(player, lod, part, "textures", "envDiffuseTexture", "charcmn", "envd_")
	gr:SetTexture(player, lod, part, "textures", "envSpecTexture", "charcmn", "envs_")
	gr:SetTexture(player, lod, part, "textures", "weavePatternNormalMap", "cmnweave", "kit_weave")
	gr:SetTexture(player, lod, part, "textures", "wrinkleMap", "shortsbnmlod", "shorts_nm")
	gr:SetTextureFromRuntime(player, lod, part, "textures", "coverageMap", "covmap_${db.player[?].stadiumID}_${db.player[?].stadiumLightID}")

	gr:SetConstantARGB(player, lod, part, "global", "jerseyCustomColorPri", "${db.player[?].kitColourSocksPri}")
	gr:SetConstantARGB(player, lod, part, "global", "jerseyCustomColorSec", "${db.player[?].kitColourSocksSec}")
	gr:SetConstantARGB(player, lod, part, "global", "jerseyCustomColorTer", "${db.player[?].kitColourSocksTer}")

	-- eyes
	part = "eyeslod"
	gr:CreateMaterialFromAttribulator(player, lod, part, "head_material", "${AttribMaterial('eyeslod')}" )
	gr:SetSubMesh(player, lod, part, "eye")
	gr:SetTexture(player, lod, part, "textures", "diffuseTexture", "eyetex", "eyes_")
	gr:SetTexture(player, lod, part, "textures", "coeffMap", "headcmn", "eye_coeff")
	gr:SetTexture(player, lod, part, "textures", "envDiffuseTexture", "charcmn", "envd_")
	gr:SetTexture(player, lod, part, "textures", "envSpecTexture", "charcmn", "env_")
	gr:SetTextureFromRuntime(player, lod, part, "textures", "coverageMap", "covmap_${db.player[?].stadiumID}_${db.player[?].stadiumLightID}")

	-- head
	part = "headlod"
	gr:CreateMaterialFromAttribulator(player, lod, part, "head_material", faceLodTone )
	gr:SetSubMesh(player, lod, part, "head")
	gr:SetTexture(player, lod, part, "textures", "diffuseTexture", "facetex", "head_")
	gr:SetTexture(player, lod, part, "textures", "normalMap", "headcmn", "head_0_nm")
	gr:SetTexture(player, lod, part, "textures", "coeffMap", "headcmn", "head_coeff")
	gr:SetTexture(player, lod, part, "textures", "envDiffuseTexture", "charcmn", "envd_")
	gr:SetTexture(player, lod, part, "textures", "envSpecTexture", "charcmn", "envs_")
	gr:SetTextureFromRuntime(player, lod, part, "textures", "coverageMap", "covmap_${db.player[?].stadiumID}_${db.player[?].stadiumLightID}")

	-- hair
	part = "hairlod"
	gr:CreateMaterialFromAttribulator(player, lod, part, "hair_material", "${AttribMaterial('player_hair_kk_alphaA_lod')}")
	gr:SetSubMesh(player, lod, part, "alphaA")
	gr:SetPriority(player, lod, part, 1)
	gr:SetTexture(player, lod, part, "textures", "diffuseTexture", "hairtex", "hair_cm")
	gr:SetTexture(player, lod, part, "textures", "specAlphaMap", "hairtex", "hair_coeff")
	gr:SetTexture(player, lod, part, "textures", "envDiffuseTexture", "charcmn", "envd_")
	gr:SetConstantARGB(player, lod, part, "global", "hairColor", "${GetHairColorARGB(?)}")
	
	--underarmor neck
	local part = "underneck"
	gr:CreateMaterialFromAttribulator(player, lod, part, "underneck_material", "${AttribMaterial('socklod')}" )
	gr:SetTexture(player, lod, part, "textures", "diffuseTexture", "jersey_tex", "jersey_cm")
	gr:SetTexture(player, lod, part, "textures", "specularMap", "kittex_common", "jersey_coeff")
	gr:SetTexture(player, lod, part, "textures", "occlusionMap", "jerseyam", "jersey_am")
	gr:SetTexture(player, lod, part, "textures", "envDiffuseTexture", "charcmn", "envd_")
	gr:SetTexture(player, lod, part, "textures", "envSpecTexture", "charcmn", "envs_")
	gr:SetTexture(player, lod, part, "textures", "weavePatternNormalMap", "cmnweave", "kit_weave")
	gr:SetTexture(player, lod, part, "textures", "wrinkleMap", "jerseybnmlod", "jersey_nm")
	gr:SetTextureFromRuntime(player, lod, part, "textures", "coverageMap", "covmap_${db.player[?].stadiumID}_${db.player[?].stadiumLightID}")

	gr:SetConstantARGB(player, lod, part, "global", "jerseyCustomColorPri", "${db.player[?].kitColourMaskPri}")
	gr:SetConstantARGB(player, lod, part, "global", "jerseyCustomColorSec", "${db.player[?].kitColourMaskSec}")
	gr:SetConstantARGB(player, lod, part, "global", "jerseyCustomColorTer", "${db.player[?].kitColourMaskTer}")

	--underarmor sleeves
	local part = "underarms"
	gr:CreateMaterialFromAttribulator(player, lod, part, "underarms_material", "${AttribMaterial('socklod')}" )
	gr:SetTexture(player, lod, part, "textures", "diffuseTexture", "jersey_tex", "jersey_cm")
	gr:SetTexture(player, lod, part, "textures", "specularMap", "kittex_common", "jersey_coeff")
	gr:SetTexture(player, lod, part, "textures", "occlusionMap", "jerseyam", "jersey_am")
	gr:SetTexture(player, lod, part, "textures", "envDiffuseTexture", "charcmn", "envd_")
	gr:SetTexture(player, lod, part, "textures", "envSpecTexture", "charcmn", "envs_")
	gr:SetTexture(player, lod, part, "textures", "weavePatternNormalMap", "cmnweave", "kit_weave")
	gr:SetTexture(player, lod, part, "textures", "wrinkleMap", "jerseybnm", "jersey_nm")
	gr:SetTextureFromRuntime(player, lod, part, "textures", "coverageMap", "covmap_${db.player[?].stadiumID}_${db.player[?].stadiumLightID}")

	gr:SetConstantARGB(player, lod, part, "global", "jerseyCustomColorPri", "${db.player[?].kitColourMaskPri}")
	gr:SetConstantARGB(player, lod, part, "global", "jerseyCustomColorSec", "${db.player[?].kitColourMaskSec}")
	gr:SetConstantARGB(player, lod, part, "global", "jerseyCustomColorTer", "${db.player[?].kitColourMaskTer}")

	
	--undershorts
	local part = "undershorts"
	gr:CreateMaterialFromAttribulator(player, lod, part, "undershorts_material", "${AttribMaterial('socklod')}") 
	gr:SetTexture(player, lod, part, "textures", "diffuseTexture", "shortstex", "shorts_cm")
	gr:SetTexture(player, lod, part, "textures", "specularMap", "shortstex", "shorts_coeff")
	gr:SetTexture(player, lod, part, "textures", "occlusionMap", "shortsam", "shorts_am")
	gr:SetTexture(player, lod, part, "textures", "wrinkleMap", "shortsbnmlod", "shorts_nm")
	gr:SetTexture(player, lod, part, "textures", "envDiffuseTexture", "charcmn", "envd_")
	gr:SetTexture(player, lod, part, "textures", "envSpecTexture", "charcmn", "envs_")
	gr:SetTexture(player, lod, part, "textures", "weavePatternNormalMap", "cmnweave", "kit_weave")
	gr:SetTextureFromRuntime(player, lod, part, "textures", "coverageMap", "covmap_${db.player[?].stadiumID}_${db.player[?].stadiumLightID}")

	gr:SetConstantARGB(player, lod, part, "global", "jerseyCustomColorPri", "${db.player[?].kitColourShortPri}")
	gr:SetConstantARGB(player, lod, part, "global", "jerseyCustomColorSec", "${db.player[?].kitColourShortSec}")
	gr:SetConstantARGB(player, lod, part, "global", "jerseyCustomColorTer", "${db.player[?].kitColourShortTer}")
	
end

---------------------------------------------------------------------------------------------------
function GetFaceTexTemplate(idx)
	if(db.player[idx].isVirtualPro == 1) then
		--return "data/sceneassets/faces/face_"..db.player[idx].faceType.."_"..db.player[idx].faceProxyHeadClass.."_0_"..db.player[idx].eyebrow.."_"..db.player[idx].faceSideBurn.."_"..db.player[idx].facialHairColor.."_"..db.player[idx].facialHairType.."_"..db.player[idx].headSkinType.."_"..db.player[idx].headSkinToneType.."_textures."..db.player[idx].faceTexExtension..";data/sceneassets/faces/face_"..db.player[idx].faceTypeFallback.."_"..db.player[idx].faceProxyHeadClass.."_0_"..db.player[idx].eyebrow.."_"..db.player[idx].faceSideBurnFallback.."_"..db.player[idx].facialHairColorFallback.."_"..db.player[idx].facialHairTypeFallback.."_"..db.player[idx].headSkinTypeFallback.."_"..db.player[idx].headSkinToneTypeFallback.."_textures.rx3"
		facetex = string.format("data/easw/head_%d.dds", db.player[idx].faceType)
		return facetex..";data/sceneassets/faces/face_"..db.player[idx].faceTypeFallback.."_"..db.player[idx].faceProxyHeadClass.."_0_"..db.player[idx].eyebrow.."_"..db.player[idx].faceSideBurnFallback.."_"..db.player[idx].facialHairColorFallback.."_"..db.player[idx].facialHairTypeFallback.."_"..db.player[idx].headSkinTypeFallback.."_"..db.player[idx].headSkinToneTypeFallback.."_textures.rx3"
	else
		return "data/sceneassets/faces/face_"..db.player[idx].playerassetid.."_0_0_0_0_0_0_0_0_textures.rx3;data/sceneassets/faces/face_"..db.player[idx].faceType.."_"..db.player[idx].faceProxyHeadClass.."_0_"..db.player[idx].eyebrow.."_"..db.player[idx].faceSideBurn.."_"..db.player[idx].facialHairColor.."_"..db.player[idx].facialHairType.."_"..db.player[idx].headSkinType.."_"..db.player[idx].headSkinToneType.."_textures."..db.player[idx].faceTexExtension
	end
end

---------------------------------------------------------------------------------------------------
function GetJerseyBMN(idx)

	if(db.player[idx].forceLowResBNM == 1 or db.player[idx].forcePowerOfTwoBNM == 1) then
		return "data/sceneassets/kit/jersey_${db.player[?].jerseyTucked}_${db.player[?].jerseyfit}_bnm_x2.rx3"
	end
		
	return "data/sceneassets/kit/jersey_${db.player[?].jerseyTucked}_${db.player[?].jerseyfit}_bnm.rx3"
end

---------------------------------------------------------------------------------------------------
function GetShortsBMN(idx)

	if(db.player[idx].forcePowerOfTwoBNM == 1 or db.player[idx].forceLowResBNM == 1) then
		return "data/sceneassets/kit/shorts_0_bnm_x2.rx3"
	end
	
	return "data/sceneassets/kit/shorts_${db.player[idx].shortstyle}_bnm.rx3"
end

---------------------------------------------------------------------------------------------------
-- returns kit texture or goalie pants kit 5200
function GetShortsTex(idx,pos)
	if(db.player[idx].shortstyle == 1) then
		return "data/sceneassets/kit/kit_${db.player[?].kit}_99_0.rx3;data/sceneassets/kit/kit_5200_0_0.rx3"
	else
		return GetRMKit(idx,pos)
	end
end

function GetKitTextureOperation(idx)
	if (db.player[idx].useTextureComposition == 1) then
		-- Creation Zone requires generated textures, describe the operation
		-- assetOperation generatetexture: "generatetexture texname width, height, mipCount, compression"
		return "generatetexture jersey_cm 1024,1024,9,eadxt1"
	else
		-- For others we do not need to generate the texture, so we just extract the source asset
		return "extracttexture jersey_cm"
	end
end

function GetKitTextureAssetName(idx)
	if (db.player[idx].useTextureComposition == 1) then
		-- Creation Zone requires generated textures, thus give the asset a unique name
		-- For CZ, kittype is always home, and year is always zero.
		return "gentex_kit_${db.player[?].kit}_${db.player[?].kitType}_${db.player[?].kitYear}_${db.player[?].teamside}_${db.player[?].kitColourJerseyPri}_${db.player[?].kitColourJerseySec}_${db.player[?].kitColourJerseyTer}"
	else
		-- Keep regular rx3 name as this is the source of the texture
		return GetRMKit(idx,0)
	end
end


---------------------------------------------------------------------------------------------------
function PlayerAssetLod(player, lod)
	local gr = gRenderables
	
	local defaultpriority = 0	-- default priority

	gr:AddCallback(player, lod, "PlayerUpdate(?)")
	
	--local kitTexAsset = "data/sceneassets/kit/kit_${db.player[?].kit}_${db.player[?].kitType}_${db.player[?].kitYear}.rx3"
	
	-- Generic files needed by all LODs
	gr:AddAsset(player, lod, "shader", "data/fifarna/shader.big")		
	gr:AddAsset(player, lod, "charcmn", "data/sceneassets/charactercmn/charactercmn_${db.player[?].envLighting}.rx3")	
	gr:AddAsset(player, lod, "bodycmn", "data/sceneassets/body/body_common_textures.rx3")	
	gr:AddAsset(player, lod, "headcmn", "data/sceneassets/heads/head_${db.player[?].playerassetid}_bump.rx3;data/sceneassets/heads/head_common_textures.rx3")	

	-- Sponsor image for stamping onto kit
	gr:AddAsset(player, lod, "sponsortex", "data/ugc/cz_sponsor/${db.player[?].sponsorAssetId}.png;data/ugc/cz_sponsor/1.png", defaultpriority, ALLOCTYPE_TEMP_AUTO_RELEASE)
	
	-- gr:AddAssetEx(asset, lod, assetOperation, assetPart, containerName)
	-- extracttexture will create an asset with just the texture you extract from the source asset, this way we can free the original rx3, freeing unused textures.
	gr:AddAssetEx(player, lod, "extracttexture jersey_coeff", "kittex_common" , "${GetRMKit(?,0)}")
	gr:AddAssetEx(player, lod, "extracttexture shorts_cm,shorts_coeff", "shortstex" , "${GetShortsTex(?,0)}")
	gr:AddAssetEx(player, lod, "extracthotspots", "kithotspots" , "${GetRMKit(?,1)}")
	gr:AddAssetEx(player, lod, "extracthotspots", "shortshotspots",  "${GetShortsTex(?,1)}")
	gr:AddAssetEx(player, lod, "extracttexture ${db.player[?].crestTexName}", "cresttex", "${GetCrestAsset(?)}")
	

	-- Texture composition code
	-- Add the template asset as a temporary asset, auto releasing it when it's not being used (after texture generation in this case)
	gr:AddAssetEx(player, lod, "extracttexture jersey_cm", "kittex_cm_template" , "${GetRMKit(?,0)}", defaultpriority, ALLOCTYPE_TEMP_AUTO_RELEASE)
	local part = "jersey_tex"
	-- GetKitTextureOperation will determine if texture composition is needed.
	gr:AddAssetEx(player, lod, "${GetKitTextureOperation(?)}", part , "${GetKitTextureAssetName(?)}")
	-- Describe texture composition in case we have to generate the texture, this material won't be created if no composition is needed.
	gr:CreateMaterial(player, lod, part, "texcomp.fx" )
	gr:SetTexture(player, lod, part, "textures", "texcomp_tex0", "kittex_cm_template", "jersey_cm")
	gr:SetTexture(player, lod, part, "textures", "texcomp_tex1", "sponsortex", "png")
	gr:SetConstantARGB(player, lod, part, "global", "recolorCustomColorPri", "${db.player[?].kitColourJerseyPri}")
	gr:SetConstantARGB(player, lod, part, "global", "recolorCustomColorSec", "${db.player[?].kitColourJerseySec}")
	gr:SetConstantARGB(player, lod, part, "global", "recolorCustomColorTer", "${db.player[?].kitColourJerseyTer}")
	gr:SetConstantARGB(player, lod, part, "global", "recolourMult", "${db.player[?].sponsorcolour}")
	gr:SetConstantHotSpot(player, lod, part, "global", "samplerBounds1", "${db.player[?].hotspotJerseySponsorL};${db.player[?].hotspotJerseySponsorT};${db.player[?].hotspotJerseySponsorR};${db.player[?].hotspotJerseySponsorB}")
	-- End of texture composition
	
	-- Set the jersey texture as a runtime texture so we can use it outside of player
	gr:SetRuntimeTexture(player, lod, "jersey_${db.player[?].kit}_${db.player[?].crowdkitType}_${db.player[?].isgoalie}_${db.player[?].teamside}_-1" , "jersey_tex", "jersey_cm")
	
	gr:AddAsset(player, lod, "shoetex", "${GetRMBoot(?,1)}data/sceneassets/shoe/shoe_${db.player[?].shoeType}_${db.player[?].shoeDesign}_textures.rx3")	
	gr:AddAsset(player, lod, "skintex", "data/sceneassets/body/playerskin_${db.player[?].playerassetid}_textures.rx3;data/sceneassets/body/skin_${db.player[?].bodySkinToneType}_textures.rx3")
	
	gr:AddAsset(player, lod, "hairtex", "data/sceneassets/hair/hair_${db.player[?].playerassetid}_0_textures.rx3;data/sceneassets/hair/hair_${db.player[?].hair}_${db.player[?].faceProxyHeadClass}_textures.rx3")
	gr:AddAsset(player, lod, "shortsam", "data/sceneassets/kit/shorts_${db.player[?].shortstyle}_${db.player[?].wet}_textures.rx3")
	gr:AddAsset(player, lod, "jerseyam", "data/sceneassets/kit/jersey_${db.player[?].jerseyTucked}_${db.player[?].wet}_textures.rx3")
	local jerseyBNM = "${GetJerseyBMN(?)}"
	gr:AddAsset(player, lod, "jerseybnm", jerseyBNM)

	gr:AddAsset(player, lod, "cmnweave", "data/sceneassets/kit/kitweave.rx3")	
	local shortsBNM = "${GetShortsBMN(?)}"
	gr:AddAsset(player, lod, "shortsbnm", shortsBNM)
	
	-- Dirt masks not used in FIFARNA There is no runtime logic to drive them besides the expensiveness
	-- gr:AddAsset(player, lod, "shortsdk", "data/sceneassets/kit/shorts_dk.rx3")
	-- gr:AddAsset(player, lod, "jerseydk", "data/sceneassets/kit/jersey_dk.rx3")

	gr:AddAsset(player, lod, "eyetex", "data/sceneassets/heads/eyes_${db.player[?].playerassetid}_0_textures.rx3;data/sceneassets/heads/eyes_${db.player[?].eyeColor}_${db.player[?].faceProxyHeadClass}_textures.rx3")

	
	-- Add font as a temporary asset, because the font is used after the load (bind time), we still have to manually release it from the renderable
	-- TODO: add font stamping support to composite instead of having the renderable do this at bind time
	gr:AddAsset(player, lod, "font", "${GetRMNameFont(?)}data/sceneassets/jerseyfonts/font_${db.player[?].kitNameFont}.ttf", defaultpriority, ALLOCTYPE_TEMP_MANUAL_RELEASE)
	gr:AddAsset(player, lod, "kitnumbers", "${GetRMNumberSet(?,0)}data/sceneassets/kitnumbers/kitnumbers_${db.player[?].kitNumberFont}_${db.player[?].kitNumberColor}.rx3;data/sceneassets/kitnumbers/kitnumbers_0_0.rx3")
	gr:AddAsset(player, lod, "shortsnumbers", "${GetRMNumberSet(?,1)}data/sceneassets/kitnumbers/kitnumbers_${db.player[?].shortsNumberFont}_${db.player[?].shortsNumberColor}.rx3;data/sceneassets/kitnumbers/kitnumbers_0_0.rx3")
	
	local faceGenTemplate = "${GetFaceTexTemplate(?)}"
	gr:AddAsset(player, lod, "facetex", faceGenTemplate)
	
	gr:AddAsset(player, lod, "underarms", "data/sceneassets/body/underarms_1_${db.player[?].underarms}.rx3")
	gr:AddAsset(player, lod, "underneck", "data/sceneassets/body/underneck_1_${db.player[?].underneck}.rx3")
	gr:AddAsset(player, lod, "undershorts", "data/sceneassets/body/undershorts_1_${db.player[?].undershorts}.rx3")


	if (lod == 0) then
		PlayerAssetHighLod(player, lod)
	elseif (lod == 1) then
		PlayerAssetLowLod(player, lod)	
	elseif (lod == 2) then
		PlayerAssetLowLod(player, lod)	
	elseif (lod == 3) then
		PlayerAssetShadow(player, lod)
	end	
	
end

---------------------------------------------------------------------------------------------------
function PlayerAssetBind(player)
	PlayerAssetLod(player, 0)
	PlayerAssetLod(player, 1)
	PlayerAssetLod(player, 2)
	PlayerAssetLod(player, 3)

end







function GetRMNameFont(idx)
	local kitnamestring = ""
	kitnamestring = kitnamestring.."data/sceneassets/jerseyfonts/specificfont_"..db.player[idx].teamid.."_1_"..db.player[idx].tournid.."_"..db.player[idx].speckitType..".ttf;"
	if (db.player[idx].kitYear == 0) then
	kitnamestring = kitnamestring.."data/sceneassets/jerseyfonts/specificfont_"..db.player[idx].teamid.."_1_"..db.player[idx].defaulttournid.."_"..db.player[idx].speckitType..".ttf;"
	kitnamestring = kitnamestring.."data/sceneassets/jerseyfonts/specificfont_"..db.player[idx].teamid.."_1_0_"..db.player[idx].speckitType..".ttf;"
	end
	kitnamestring = kitnamestring.."data/sceneassets/jerseyfonts/specificfont_"..db.player[idx].teamid.."_1_"..db.player[idx].tournid.."_"..db.player[idx].kitType..".ttf;"
	if (db.player[idx].kitYear == 0) then
	kitnamestring = kitnamestring.."data/sceneassets/jerseyfonts/specificfont_"..db.player[idx].teamid.."_1_"..db.player[idx].defaulttournid.."_"..db.player[idx].kitType..".ttf;"
	kitnamestring = kitnamestring.."data/sceneassets/jerseyfonts/specificfont_"..db.player[idx].teamid.."_1_0_"..db.player[idx].kitType..".ttf;"
	end
	kitnamestring = kitnamestring.."data/sceneassets/jerseyfonts/specificfont_"..db.player[idx].teamid.."_0_"..db.player[idx].tournid.."_0.ttf;"
	if (db.player[idx].kitYear == 0) then
    kitnamestring = kitnamestring.."data/sceneassets/jerseyfonts/specificfont_0_0_"..db.player[idx].tournid.."_0.ttf;"
    kitnamestring = kitnamestring.."data/sceneassets/jerseyfonts/specificfont_"..db.player[idx].teamid.."_0_0_0.ttf;"
	kitnamestring = kitnamestring.."data/sceneassets/jerseyfonts/specificfont_0_0_"..db.player[idx].defaulttournid.."_0.ttf;"
	kitnamestring = kitnamestring.."data/sceneassets/jerseyfonts/specificfont_0_0_0_0.ttf;"
	end
	return kitnamestring
end

function GetRMNumberSet(idx,short)
	local colour = 0
	local kitnumberstring = ""
	
	if (short == 0) then
	colour = db.player[idx].kitNumberColor
	else
	colour = db.player[idx].shortsNumberColor
	end
	
	kitnumberstring = kitnumberstring.."data/sceneassets/kitnumbers/specifickitnumbers_"..db.player[idx].teamid.."_"..(short+1).."_"..db.player[idx].tournid.."_"..db.player[idx].speckitType..".rx3;"
	if (db.player[idx].kitYear == 0) then
	kitnumberstring = kitnumberstring.."data/sceneassets/kitnumbers/specifickitnumbers_"..db.player[idx].teamid.."_"..(short+1).."_"..db.player[idx].defaulttournid.."_"..db.player[idx].speckitType..".rx3;"
	kitnumberstring = kitnumberstring.."data/sceneassets/kitnumbers/specifickitnumbers_"..db.player[idx].teamid.."_"..(short+1).."_0_"..db.player[idx].speckitType..".rx3;"
	end
	kitnumberstring = kitnumberstring.."data/sceneassets/kitnumbers/specifickitnumbers_"..db.player[idx].teamid.."_"..(short+1).."_"..db.player[idx].tournid.."_"..db.player[idx].kitType..".rx3;"
	if (db.player[idx].kitYear == 0) then
	kitnumberstring = kitnumberstring.."data/sceneassets/kitnumbers/specifickitnumbers_"..db.player[idx].teamid.."_"..(short+1).."_"..db.player[idx].defaulttournid.."_"..db.player[idx].kitType..".rx3;"
	kitnumberstring = kitnumberstring.."data/sceneassets/kitnumbers/specifickitnumbers_"..db.player[idx].teamid.."_"..(short+1).."_0_"..db.player[idx].kitType..".rx3;"
	end
	kitnumberstring = kitnumberstring.."data/sceneassets/kitnumbers/specifickitnumbers_"..db.player[idx].teamid.."_0_"..db.player[idx].tournid.."_"..colour..".rx3;"
	if (db.player[idx].kitYear == 0) then
	kitnumberstring = kitnumberstring.."data/sceneassets/kitnumbers/specifickitnumbers_0_0_"..db.player[idx].tournid.."_"..colour..".rx3;"
	kitnumberstring = kitnumberstring.."data/sceneassets/kitnumbers/specifickitnumbers_"..db.player[idx].teamid.."_0_"..db.player[idx].kitYear.."_"..colour..".rx3;"
	kitnumberstring = kitnumberstring.."data/sceneassets/kitnumbers/specifickitnumbers_0_0_"..db.player[idx].defaulttournid.."_"..colour..".rx3;"
	kitnumberstring = kitnumberstring.."data/sceneassets/kitnumbers/specifickitnumbers_0_0_"..db.player[idx].kitYear.."_"..colour..".rx3;"
	end
	return kitnumberstring 
end






function getGenericBootReplacement(id,boot)
--min 126 max 132 (total 105)
local rand = (id%107)+126

if (rand == 195) then
rand = rand + 1
end

return rand
-- genericBootReplace = {23,40,228}
-- return genericBootReplace[(id%#genericBootReplace)+1]
end


function GetRMBoot(idx,model)
local bootstring = ""
local tex = ""

if (model == 1) then
tex = "_textures"
end

bootstring = bootstring.."data/sceneassets/shoe/playershoe_"..db.player[idx].playerassetid.."_0_"..db.player[idx].bootrand..""..tex..".rx3;"
bootstring = bootstring.."data/sceneassets/shoe/playershoe_"..db.player[idx].playerassetid.."_0_0"..tex..".rx3;"
return bootstring
end







	

function GetRMKit(idx,isPos)
	local kitstring = ""
	--if (db.player[idx].teamside ~= -1) then
	kitstring = kitstring.."data/sceneassets/kit/kit_"..db.player[idx].kit.."_"..db.player[idx].speckitType.."_"..db.player[idx].tournid..".rx3;"
	kitstring = kitstring.."data/sceneassets/kit/kit_"..db.player[idx].kit.."_"..db.player[idx].speckitType.."_"..db.player[idx].defaulttournid..".rx3;"
	kitstring = kitstring.."data/sceneassets/kit/kit_"..db.player[idx].kit.."_"..db.player[idx].speckitType.."_0.rx3;"
	kitstring = kitstring.."data/sceneassets/kit/kit_0_"..db.player[idx].speckitType.."_0.rx3;"
	--end
	-- if (db.player[idx].teamside == -1) then
	-- kitstring = kitstring.."data/sceneassets/kit/specifickit_6004_5_"..db.player[idx].tournid.."_"..db.player[idx].homeKitTeamID.."_"..db.player[idx].awayKitTeamID..".rx3;"
	-- end
	kitstring = kitstring.."data/sceneassets/kit/kit_"..db.player[idx].kit.."_"..db.player[idx].kitType.."_"..db.player[idx].tournid..".rx3;"
	-- if (db.player[idx].teamside == -1) then
	-- kitstring = kitstring.."data/sceneassets/kit/specifickit_6004_5_"..db.player[idx].defaulttournid.."_"..db.player[idx].homeKitTeamID.."_"..db.player[idx].awayKitTeamID..".rx3;"
	-- end
	kitstring = kitstring.."data/sceneassets/kit/kit_"..db.player[idx].kit.."_"..db.player[idx].kitType.."_"..db.player[idx].defaulttournid..".rx3;"
	-- if (db.player[idx].teamside == -1) then
	-- kitstring = kitstring.."data/sceneassets/kit/specifickit_6004_5_0_"..db.player[idx].homeKitTeamID.."_"..db.player[idx].awayKitTeamID..".rx3;"
	-- end
	--kitstring = kitstring.."data/sceneassets/kit/kit_"..db.player[idx].kit.."_"..db.player[idx].kitType.."_"..db.player[idx].kitYear..".rx3;"
	kitstring = kitstring.."data/sceneassets/kit/kit_"..db.player[idx].kit.."_"..db.player[idx].kitType.."_0.rx3;"
	-- if ((db.player[idx].kitType == 2) and (isPos == 1)) then
	-- kitstring = kitstring.."data/sceneassets/kit/kit_"..db.player[idx].kit.."_0_"..db.player[idx].tournid..".rx3;"
	-- kitstring = kitstring.."data/sceneassets/kit/kit_"..db.player[idx].kit.."_0_0.rx3;"
	-- end
	kitstring = kitstring.."data/sceneassets/kit/kit_"..db.player[idx].genkit.."_"..db.player[idx].kitType.."_"..db.player[idx].tournid..".rx3;"
	kitstring = kitstring.."data/sceneassets/kit/kit_"..db.player[idx].genkit.."_"..db.player[idx].kitType.."_"..db.player[idx].defaulttournid..".rx3;"
	kitstring = kitstring.."data/sceneassets/kit/kit_"..db.player[idx].genkit.."_"..db.player[idx].kitType.."_0.rx3"
	return kitstring
end
















tournamentRefereeKitsCollar = {}

function assignTournamentRefereeKitCollar(tournament,collar)
tournamentRefereeKitsCollar[tournament] = collar
end



function getTournamentRefereeKitsCollar(tourn,id,home)

if (tournamentRefereeKitsCollar[tourn] ~= nil) then
return tournamentRefereeKitsCollar[tourn]
end

if (teamTournament[home] ~= nil) then
if (tournamentRefereeKitsCollar[teamTournament[home]] ~= nil) then
return tournamentRefereeKitsCollar[teamTournament[home]]
end
end

return id
end



gkPants = {}

function assignGKPants(id)
gkPants[id] = 1
end

function getGKPants(id,pant)

if (gkPants[id] ~= nil) then
return gkPants[id]
else
return pant
end

end


playerFace = {}

function assignPlayerFace(id)
playerFace[id] = 0
end

function getPlayerFace(id,headtype)

if (playerFace[id] ~= nil) then
return playerFace[id]
else
return headtype
end

end


gkKits = {}

function assignGKKit(team,player,gk)

if (gkKits[team] == nil) then
gkKits[team] = {}
end

if (type(gk) == "number") then
gkKits[team][player] = gk
else
gkKits[team][player] = gk[math.random(#gk)]
end

end

function getGKKit(team,player,home,away,gk,spec)

if (matchKit[team] ~= nil) then
if (matchKit[team][home] ~= nil) then
if (matchKit[team][home][away] ~= nil) then
if (matchKit[team][home][away][gk] ~= nil) then
return matchKit[team][home][away][gk]
end
end
end
end

if (gkKits[team] ~= nil) then
if (gkKits[team][player] ~= nil) then
return gkKits[team][player]
end
end

return spec
end


--KIT DETAILS START

kitCollar = {}
kitNumberSet = {}
kitNumberColourShirt = {}
kitNumberColourShort = {}
kitNameFont = {}
kitNameColour = {}
kitFit = {}
kitNameCurve = {}


function assignKitDetails(team,kit,namefont,namecolour,lay,numberset,numbercolourshirt,numbercolourshort,fit,collar)

if (kitCollar[team] == nil) then
kitCollar[team] = {}
end

if (kitNumberSet[team] == nil) then
kitNumberSet[team] = {}
end

if (kitNumberColourShirt[team] == nil) then
kitNumberColourShirt[team] = {}
end

if (kitNumberColourShort[team] == nil) then
kitNumberColourShort[team] = {}
end

if (kitNameFont[team] == nil) then
kitNameFont[team] = {}
end

if (kitNameColour[team] == nil) then
kitNameColour[team] = {}
end

if (kitFit[team] == nil) then
kitFit[team] = {}
end

if (kitNameCurve[team] == nil) then
kitNameCurve[team] = {}
end


kitCollar[team][kit] = collar
kitNumberSet[team][kit] = numberset
kitNumberColourShirt[team][kit] = numbercolourshirt
kitNumberColourShort[team][kit] = numbercolourshort
kitNameFont[team][kit] = namefont

if (namecolour ~= -1) then
kitNameColour[team][kit] = hex(namecolour)
end

kitFit[team][kit] = fit
kitNameCurve[team][kit] = lay
end


kitCollarTournament = {}
kitNumberSetTournament = {}
kitNumberColourShirtTournament = {}
kitNumberColourShortTournament = {}
kitNameFontTournament = {}
kitNameColourTournament = {}
kitFitTournament = {}
kitNameCurveTournament = {}


function assignTournamentKitDetails(team,kit,tournament,namefont,namecolour,lay,numberset,numbercolourshirt,numbercolourshort,fit,collar)

if (kitCollarTournament[team] == nil) then
kitCollarTournament[team] = {}
end

if (kitNumberSetTournament[team] == nil) then
kitNumberSetTournament[team] = {}
end

if (kitNumberColourShirtTournament[team] == nil) then
kitNumberColourShirtTournament[team] = {}
end

if (kitNumberColourShortTournament[team] == nil) then
kitNumberColourShortTournament[team] = {}
end

if (kitNameFontTournament[team] == nil) then
kitNameFontTournament[team] = {}
end

if (kitNameColourTournament[team] == nil) then
kitNameColourTournament[team] = {}
end

if (kitFitTournament[team] == nil) then
kitFitTournament[team] = {}
end

if (kitNameCurveTournament[team] == nil) then
kitNameCurveTournament[team] = {}
end



if (kitCollarTournament[team][kit] == nil) then
kitCollarTournament[team][kit] = {}
end

if (kitNumberSetTournament[team][kit] == nil) then
kitNumberSetTournament[team][kit] = {}
end

if (kitNumberColourShirtTournament[team][kit] == nil) then
kitNumberColourShirtTournament[team][kit] = {}
end

if (kitNumberColourShortTournament[team][kit] == nil) then
kitNumberColourShortTournament[team][kit] = {}
end

if (kitNameFontTournament[team][kit] == nil) then
kitNameFontTournament[team][kit] = {}
end

if (kitNameColourTournament[team][kit] == nil) then
kitNameColourTournament[team][kit] = {}
end

if (kitFitTournament[team][kit] == nil) then
kitFitTournament[team][kit] = {}
end

if (kitNameCurveTournament[team][kit] == nil) then
kitNameCurveTournament[team][kit] = {}
end



kitCollarTournament[team][kit][tournament] = collar
kitNumberSetTournament[team][kit][tournament] = numberset
kitNumberColourShirtTournament[team][kit][tournament] = numbercolourshirt
kitNumberColourShortTournament[team][kit][tournament] = numbercolourshort
kitNameFontTournament[team][kit][tournament] = namefont

if (namecolour ~= -1) then
kitNameColourTournament[team][kit][tournament] = hex(namecolour)
end

kitFitTournament[team][kit][tournament] = fit
kitNameCurveTournament[team][kit][tournament] = lay
end


function getKitCollar(team,kit,item,tournament)

if (kitCollarTournament[team] ~= nil) then
if (kitCollarTournament[team][kit] ~= nil) then
if ((kitCollarTournament[team][kit][tournament] ~= nil) and (kitCollarTournament[team][kit][tournament] ~= -1)) then
return kitCollarTournament[team][kit][tournament]
end
end
end

if (tournament < 1800) then

if (kitCollar[team] ~= nil) then
if ((kitCollar[team][kit] ~= nil) and (kitCollar[team][kit] ~= -1)) then
return kitCollar[team][kit]
end
end

end

return item
end

function getKitNumberSet(team,kit,item,tournament,teamdef)

if (kitNumberSetTournament[team] ~= nil) then
if (kitNumberSetTournament[team][kit] ~= nil) then
if ((kitNumberSetTournament[team][kit][tournament] ~= nil) and (kitNumberSetTournament[team][kit][tournament] ~= -1)) then
return kitNumberSetTournament[team][kit][tournament]
end
end
end


if (tournament < 1800) then

if (kitNumberSet[team] ~= nil) then
if ((kitNumberSet[team][kit] ~= nil) and (kitNumberSet[team][kit] ~= -1)) then
return kitNumberSet[team][kit]
end
end

end

return item
end

function getKitNumberColourShirt(team,kit,item,tournament)

if (kitNumberColourShirtTournament[team] ~= nil) then
if (kitNumberColourShirtTournament[team][kit] ~= nil) then
if ((kitNumberColourShirtTournament[team][kit][tournament] ~= nil) and (kitNumberColourShirtTournament[team][kit][tournament] ~= -1)) then
return kitNumberColourShirtTournament[team][kit][tournament]
end
end
end

if (tournament < 1800) then

if (kitNumberColourShirt[team] ~= nil) then
if ((kitNumberColourShirt[team][kit] ~= nil) and (kitNumberColourShirt[team][kit] ~= -1)) then
return kitNumberColourShirt[team][kit]
end
end

end

return item
end

function getKitNumberColourShort(team,kit,item,tournament)

if (kitNumberColourShortTournament[team] ~= nil) then
if (kitNumberColourShortTournament[team][kit] ~= nil) then
if ((kitNumberColourShortTournament[team][kit][tournament] ~= nil) and (kitNumberColourShortTournament[team][kit][tournament] ~= -1)) then
return kitNumberColourShortTournament[team][kit][tournament]
end
end
end

if (tournament < 1800) then

if (kitNumberColourShort[team] ~= nil) then
if ((kitNumberColourShort[team][kit] ~= nil) and (kitNumberColourShort[team][kit] ~= -1)) then
return kitNumberColourShort[team][kit]
end
end

end

return item
end

function getKitNameFont(team,kit,item,tournament,teamdef)

if (kitNameFontTournament[team] ~= nil) then
if (kitNameFontTournament[team][kit] ~= nil) then
if ((kitNameFontTournament[team][kit][tournament] ~= nil) and (kitNameFontTournament[team][kit][tournament] ~= -1)) then
return kitNameFontTournament[team][kit][tournament]
end
end
end


if (tournament < 1800) then

if (kitNameFont[team] ~= nil) then
if ((kitNameFont[team][kit] ~= nil) and (kitNameFont[team][kit] ~= -1)) then
return kitNameFont[team][kit]
end
end

end

return item
end

function getKitNameColour(team,kit,item,tournament)

if (kitNameColourTournament[team] ~= nil) then
if (kitNameColourTournament[team][kit] ~= nil) then
if ((kitNameColourTournament[team][kit][tournament] ~= nil) and (kitNameColourTournament[team][kit][tournament] ~= -1)) then
return kitNameColourTournament[team][kit][tournament]
end
end

end

if (tournament < 1800) then

if (kitNameColour[team] ~= nil) then
if ((kitNameColour[team][kit] ~= nil) and (kitNameColour[team][kit] ~= -1)) then
return kitNameColour[team][kit]
end
end

end

return item
end

function getKitFit(team,kit,item,tournament,player)

if (globalJerseyFit ~= nil) then
return globalJerseyFit
end

if (kitFitTournament[team] ~= nil) then
if (kitFitTournament[team][kit] ~= nil) then
if ((kitFitTournament[team][kit][tournament] ~= nil) and (kitFitTournament[team][kit][tournament] ~= -1)) then
return kitFitTournament[team][kit][tournament]
end
end
end

if (tournament < 1800) then

if (kitFit[team] ~= nil) then
if ((kitFit[team][kit] ~= nil) and (kitFit[team][kit] ~= -1)) then
return kitFit[team][kit]
end
end

end

if (kitFitPlayer[player] ~= nil) then
return kitFitPlayer[player]
end

return item
end


function getKitNameCurve(team,kit,lay,tournament)

if (kitNameCurveTournament[team] ~= nil) then
if (kitNameCurveTournament[team][kit] ~= nil) then
if ((kitNameCurveTournament[team][kit][tournament] ~= nil) and (kitNameCurveTournament[team][kit][tournament] ~= -1)) then
return kitNameCurveTournament[team][kit][tournament]
end
end
end

if (tournament < 1800) then

if (kitNameCurve[team] ~= nil) then
if ((kitNameCurve[team][kit] ~= nil) and (kitNameCurve[team][kit] ~= -1)) then
return kitNameCurve[team][kit]
end
end

end

return lay

end


--KIT DETAILS END


jerseyTuck = {}

function setJerseyTuck(player,tuck)
jerseyTuck[player] = tuck
end

function getJerseyTuck(player,tuck)

if (jerseyTuck[player] ~= nil) then
return jerseyTuck[player]
end

return tuck
end


sockHeight = {}

function setSockHeight(player,sock)
sockHeight[player] = sock
end

function getSockHeight(player,sock)

if (sockHeight[player] ~= nil) then
return sockHeight[player]
end

return sock
end



matchKit = {}

function assignGameKit(team,home,away,type1,type2)

if (matchKit[team] == nil) then
matchKit[team] = {}
end

if (matchKit[team][home] == nil) then
matchKit[team][home] = {}
end

if (matchKit[team][home][away] == nil) then
matchKit[team][home][away] = {}
end

if (type(type2) == "number") then
matchKit[team][home][away][type1] = type2
else
matchKit[team][home][away][type1] = type2[math.random(#type2)]

end

end

function getGameKit(team,home,away,type1,type2)

if (matchKit[team] ~= nil) then
if (matchKit[team][home] ~= nil) then
if (matchKit[team][home][away] ~= nil) then
if (matchKit[team][home][away][type1] ~= nil) then
return matchKit[team][home][away][type1]
end
end
end
end

if (swapKit[team] ~= nil) then
if (swapKit[team][type1] ~= nil) then
return swapKit[team][type1]
end
end

return type2
end


function hex(s)

return tonumber(s,16) + 4278190080

end




--nameColourCodes = {"d5d9d5", "0d0c0d", "0046b0", "a00005", "e8d628", "007a00", "f88500", "7a19b8", "553210", "ed80b0", "750e05", "7dc1f0", "001450", "a0a2a0", "c5c965", "98864d", "f8c932", "c53232", "003c00"}


kitFitPlayer = {}

function setJerseyFit(player,fit)
kitFitPlayer[player] = fit
end




winterAccessoriesRef = nil

function useGlobalRefereeWinterAccessories(option)
winterAccessoriesRef = option
end

function getRefereeWinterAccessories(option)

if (winterAccessoriesRef ~= nil) then
return winterAccessoriesRef
end

return option
end


globalJerseyFit = nil

function useGlobalJerseyFit(option)
globalJerseyFit = option
end


swapKit = {}

function swapTeamKit(team,type1,type2)

if (swapKit[team] == nil) then
swapKit[team] = {}
end

if (type(type2) == "number") then
swapKit[team][type1] = type2
else
swapKit[team][type1] = type2[math.random(#type2)]

end

end





oldidx = -1

--Revolution Mod 14 1.1
--Edited by scouser09