---------------------------------------------------------------------------------------------------------
-- @Author: ViperGTS96-----------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
--------------------"The simplest design is the best design." -------------------------------------------
---------------------------------------------------------------------------------------------------------
-----The ability to add Masks/Hats/Helmets to player via this script is reserved by ViperGTS96-----------
---------------------------------------------------------------------------------------------------------

cowboyHat = {}
cowboyHat.path = g_currentModDirectory;
cowboyHat.dir = cowboyHat.path.."models/";
local modDescFile = loadXMLFile("modDesc", cowboyHat.path .. "modDesc.xml");
cowboyHat.title = getXMLString(modDescFile, "modDesc.title.en");
cowboyHat.author = getXMLString(modDescFile, "modDesc.author");
cowboyHat.version = getXMLString(modDescFile, "modDesc.version");
delete(modDescFile);
cowboyHat.hatFilename = {"Bullrider.i3d", "Cattleman.i3d", "WedgedBullrider.i3d", "WedgedCattleman.i3d"}
cowboyHat.styleCount = 4;
cowboyHat.glovesFilename = "";
cowboyHat.isHelperHat = false;
cowboyHat.initialized = false;
local cowboyHatXML = loadXMLFile("cowboyHatXML", cowboyHat.path .. "cowboyHat.xml");
local xmlKey = "cowboyHat."
--cowboyHat.plyrModel = Utils.getNoNil(getXMLInt(cowboyHatXML, xmlKey.."playerHat.model.type#value"), 1);
cowboyHat.rainbow = Utils.getNoNil(getXMLBool(cowboyHatXML, xmlKey.."playerHat.rainbowBand#value"), false);
cowboyHat.equipGloves = Utils.getNoNil(getXMLBool(cowboyHatXML, xmlKey.."playerHat.glovesWithHat#value"), false);
cowboyHat.aiGetHats = Utils.getNoNil(getXMLBool(cowboyHatXML, xmlKey.."helperHats.aiGetHats#value"), false);
cowboyHat.olDuration = Utils.getNoNil(getXMLInt(cowboyHatXML, xmlKey.."menu.intervals.toggled#duration"), 60);
cowboyHat.olDurationMnu = Utils.getNoNil(getXMLInt(cowboyHatXML, xmlKey.."menu.intervals.controls#duration"), 140);
cowboyHat.plyrModelIndex = 1;
cowboyHat.colors = {};
---------Player Hat Color----------
cowboyHat.colors.player = {};
cowboyHat.colors.player.mat = {};
xmlKey = "cowboyHat.playerHat.color.material."
cowboyHat.colors.player.mat.r = Utils.getNoNil(getXMLFloat(cowboyHatXML, xmlKey.."red#value"), 0.053);
cowboyHat.colors.player.mat.g = Utils.getNoNil(getXMLFloat(cowboyHatXML, xmlKey.."green#value"), 0.03);
cowboyHat.colors.player.mat.b = Utils.getNoNil(getXMLFloat(cowboyHatXML, xmlKey.."blue#value"), 0.013);
cowboyHat.colors.player.mat.var = 11.0;
local matValue = Utils.getNoNil(getXMLBool(cowboyHatXML, xmlKey.."leather#value"), false);
if matValue then cowboyHat.colors.player.mat.var = 11.5; end;
cowboyHat.colors.player.band = {};
xmlKey = "cowboyHat.playerHat.color.band."
cowboyHat.colors.player.band.r = Utils.getNoNil(getXMLFloat(cowboyHatXML, xmlKey.."red#value"), 0.05);
cowboyHat.colors.player.band.g = Utils.getNoNil(getXMLFloat(cowboyHatXML, xmlKey.."green#value"), 0.05);
cowboyHat.colors.player.band.b = Utils.getNoNil(getXMLFloat(cowboyHatXML, xmlKey.."blue#value"), 0.05);
cowboyHat.colors.player.band.var = 1.0;	
local bandValue = Utils.getNoNil(getXMLBool(cowboyHatXML, xmlKey.."plated#value"), false);
if bandValue then cowboyHat.colors.player.band.var = 2.0; end;
xmlKey = nil;
delete(cowboyHatXML);
----------Hat Colors--------------
cowboyHat.colors.mat = {};
cowboyHat.colors.mat.index = 8;
cowboyHat.colors.mat.black = {};
cowboyHat.colors.mat.black.r, cowboyHat.colors.mat.black.g, cowboyHat.colors.mat.black.b = 0.05, 0.05, 0.05;
cowboyHat.colors.mat.brown = {};--0.21, 0.12, 0.05;
cowboyHat.colors.mat.brown.r, cowboyHat.colors.mat.brown.g, cowboyHat.colors.mat.brown.b = 0.158, 0.09, 0.038;
cowboyHat.colors.mat.lightBrown = {};--0.42, 0.31, 0.23;
cowboyHat.colors.mat.lightBrown.r, cowboyHat.colors.mat.lightBrown.g, cowboyHat.colors.mat.lightBrown.b = 0.322, 0.176, 0.071;
cowboyHat.colors.mat.darkBrown = {};
cowboyHat.colors.mat.darkBrown.r, cowboyHat.colors.mat.darkBrown.g, cowboyHat.colors.mat.darkBrown.b = 0.053, 0.03, 0.013;
cowboyHat.colors.mat.beige = {};--0.66, 0.63, 0.57;
cowboyHat.colors.mat.beige.r, cowboyHat.colors.mat.beige.g, cowboyHat.colors.mat.beige.b = 0.58, 0.5, 0.4;
cowboyHat.colors.mat.tan = {};--0.58, 0.5, 0.4;
cowboyHat.colors.mat.tan.r, cowboyHat.colors.mat.tan.g, cowboyHat.colors.mat.tan.b = 0.42, 0.31, 0.23;
cowboyHat.colors.mat.white = {};
cowboyHat.colors.mat.white.r, cowboyHat.colors.mat.white.g, cowboyHat.colors.mat.white.b = 0.85, 0.85, 0.85;
cowboyHat.numberOfPrimaryColors = 7;
----------Band Colors-------------
cowboyHat.colors.band = {};
cowboyHat.colors.band.index = 14;
cowboyHat.colors.band.black = {};
cowboyHat.colors.band.black.r, cowboyHat.colors.band.black.g, cowboyHat.colors.band.black.b = 0.05, 0.05, 0.05;
cowboyHat.colors.band.brown = {};
cowboyHat.colors.band.brown.r, cowboyHat.colors.band.brown.g, cowboyHat.colors.band.brown.b = 0.158, 0.09, 0.038;
cowboyHat.colors.band.tan = {};--0.58, 0.5, 0.4;
cowboyHat.colors.band.tan.r, cowboyHat.colors.band.tan.g, cowboyHat.colors.band.tan.b = 0.42, 0.31, 0.23;
cowboyHat.colors.band.red = {};
cowboyHat.colors.band.red.r, cowboyHat.colors.band.red.g, cowboyHat.colors.band.red.b = 0.4, 0.0, 0.0;
cowboyHat.colors.band.green = {};
cowboyHat.colors.band.green.r, cowboyHat.colors.band.green.g, cowboyHat.colors.band.green.b = 0.0, 0.4, 0.0;
cowboyHat.colors.band.blue = {};
cowboyHat.colors.band.blue.r, cowboyHat.colors.band.blue.g, cowboyHat.colors.band.blue.b = 0.0, 0.0, 0.4;
cowboyHat.colors.band.yellow = {};
cowboyHat.colors.band.yellow.r, cowboyHat.colors.band.yellow.g, cowboyHat.colors.band.yellow.b = 0.4, 0.4, 0.0;
cowboyHat.colors.band.orange = {};
cowboyHat.colors.band.orange.r, cowboyHat.colors.band.orange.g, cowboyHat.colors.band.orange.b = 0.4, 0.2, 0.0;
cowboyHat.colors.band.teal = {};
cowboyHat.colors.band.teal.r, cowboyHat.colors.band.teal.g, cowboyHat.colors.band.teal.b = 0.0, 0.4, 0.4;
cowboyHat.colors.band.skyBlue = {};
cowboyHat.colors.band.skyBlue.r, cowboyHat.colors.band.skyBlue.g, cowboyHat.colors.band.skyBlue.b = 0.0, 0.2, 0.4;
cowboyHat.colors.band.pink = {};
cowboyHat.colors.band.pink.r, cowboyHat.colors.band.pink.g, cowboyHat.colors.band.pink.b = 0.4, 0.0, 0.4;
cowboyHat.colors.band.purple = {};
cowboyHat.colors.band.purple.r, cowboyHat.colors.band.purple.g, cowboyHat.colors.band.purple.b = 0.2, 0.0, 0.4;
cowboyHat.colors.band.white = {};
cowboyHat.colors.band.white.r, cowboyHat.colors.band.white.g, cowboyHat.colors.band.white.b = 0.85, 0.85, 0.85;
cowboyHat.numberOfSecondaryColors = 13;

-----------------------------------
print("Load mod: "..cowboyHat.title.." : v"..cowboyHat.version.." by "..cowboyHat.author);
-----------------------------------

function cowboyHat:setHatColors(self)

	local colorChanged = true;

	if self.hatNode ~= nil then
		local cboyHatShape = getChildAt(self.hatNode, 0);
		cboyHatShape = getChildAt(cboyHatShape, 0);
		if cowboyHat.isHelperHat then
			if cowboyHat.aiGetHats == false then
				self:removeHat();
			else
				if getHasShaderParameter(cboyHatShape, "colorMat0") then
					local materialType = math.random(0,1);
					if materialType > 0.5 then
						materialType = 11.5;
					else
						materialType = 11.0;
					end;
					local aiColorR, aiColorG, aiColorB = cowboyHat:getHatColor(true, true, false);
					setShaderParameter(cboyHatShape, "colorMat0", aiColorR, aiColorG, aiColorB, materialType, false);
					local aiBColorR, aiBColorG, aiBColorB = cowboyHat:getBandColor(true, true, false);--math.random(0.05, 0.5), math.random(0.05, 0.5), math.random(0.05, 0.5);
					setShaderParameter(cboyHatShape, "colorMat1", aiBColorR, aiBColorG, aiBColorB, 1, false);
				end;
			end;
			cowboyHat.isHelperHat = false;
		else --Player Hat
			if getHasShaderParameter(cboyHatShape, "colorMat0") then
				local colors = cowboyHat.colors.player;
				local colorR, colorG, colorB = cowboyHat:getHatColor(true, false, true);
				setShaderParameter(cboyHatShape, "colorMat0", colorR, colorG, colorB, colors.mat.var, false);
				local bColorR, bColorG, bColorB = cowboyHat:getBandColor(true, false, true);
				setShaderParameter(cboyHatShape, "colorMat1", bColorR, bColorG, bColorB, colors.band.var, false);
			end;
		end;
	else		
		colorChanged = false;
	end;

	return colorChanged;
	
end;

function cowboyHat:nextHatStyle()

	cowboyHat.plyrModelIndex = cowboyHat.plyrModelIndex + 1;

	if cowboyHat.plyrModelIndex > cowboyHat.styleCount then
		cowboyHat.plyrModelIndex = 1;
	end;
	return cowboyHat.plyrModelIndex;
end;

function cowboyHat:getHatColor(random, setNotGet, isPlayer)

	local colorR, colorG, colorB = 0,0,0;
	local colors = cowboyHat.colors.mat;	
	local colorIndex = 1;

	if setNotGet then
		if random then
			colorIndex = math.random(1,cowboyHat.numberOfPrimaryColors);
		else
			cowboyHat.colors.mat.index = cowboyHat.colors.mat.index + 1;
			if cowboyHat.colors.mat.index > cowboyHat.numberOfPrimaryColors then
				if isPlayer then
					if cowboyHat.colors.mat.index > (cowboyHat.numberOfPrimaryColors+1) then
						cowboyHat.colors.mat.index = 1;
					end;
				else
					cowboyHat.colors.mat.index = 1;
				end;
			end;
			colorIndex = cowboyHat.colors.mat.index;
		end;
	else
		colorIndex = cowboyHat.colors.mat.index;
	end;

	if colorIndex <= 1 then
		colorR, colorG, colorB = colors.black.r, colors.black.g, colors.black.b;
	elseif colorIndex <= 2 then
		colorR, colorG, colorB = colors.brown.r, colors.brown.g, colors.brown.b;
	elseif colorIndex <= 3 then
		colorR, colorG, colorB = colors.lightBrown.r, colors.lightBrown.g, colors.lightBrown.b;
	elseif colorIndex <= 4 then
		colorR, colorG, colorB = colors.darkBrown.r, colors.darkBrown.g, colors.darkBrown.b;
	elseif colorIndex <= 5 then
		colorR, colorG, colorB = colors.beige.r, colors.beige.g, colors.beige.b;
	elseif colorIndex <= 6 then
		colorR, colorG, colorB = colors.tan.r, colors.tan.g, colors.tan.b;
	elseif colorIndex <= 7 then
		colorR, colorG, colorB = colors.white.r, colors.white.g, colors.white.b;
	elseif colorIndex <= 8 then
		colors = cowboyHat.colors.player.mat;
		colorR, colorG, colorB = colors.r, colors.g, colors.b;
	end;

	return colorR, colorG, colorB;

end;

function cowboyHat:getBandColor(random, setNotGet, isPlayer)

	local colorR, colorG, colorB = 0,0,0;
	local colors = cowboyHat.colors.band;
	local colorIndex = 1;
	
	if setNotGet then
		if random then
			colorIndex = math.random(1,cowboyHat.numberOfSecondaryColors);
		else
			cowboyHat.colors.band.index = cowboyHat.colors.band.index + 1;
			if cowboyHat.colors.band.index > cowboyHat.numberOfSecondaryColors then
				if isPlayer then
					if cowboyHat.colors.band.index > (cowboyHat.numberOfSecondaryColors+1) then
						cowboyHat.colors.band.index = 1;
					end;
				else
					cowboyHat.colors.band.index = 1;
				end;
			end;
			colorIndex = cowboyHat.colors.band.index;
		end;
	else
		colorIndex = cowboyHat.colors.band.index;
	end;

	if colorIndex <= 1 then
		colorR, colorG, colorB = colors.black.r, colors.black.g, colors.black.b;
	elseif colorIndex <= 2 then
		colorR, colorG, colorB = colors.brown.r, colors.brown.g, colors.brown.b;
	elseif colorIndex <= 3 then
		colorR, colorG, colorB = colors.tan.r, colors.tan.g, colors.tan.b;
	elseif colorIndex <= 4 then
		colorR, colorG, colorB = colors.red.r, colors.red.g, colors.red.b;
	elseif colorIndex <= 5 then
		colorR, colorG, colorB = colors.green.r, colors.green.g, colors.green.b;
	elseif colorIndex <= 6 then
		colorR, colorG, colorB = colors.blue.r, colors.blue.g, colors.blue.b;
	elseif colorIndex <= 7 then
		colorR, colorG, colorB = colors.yellow.r, colors.yellow.g, colors.yellow.b;
	elseif colorIndex <= 8 then
		colorR, colorG, colorB = colors.orange.r, colors.orange.g, colors.orange.b;
	elseif colorIndex <= 9 then
		colorR, colorG, colorB = colors.teal.r, colors.teal.g, colors.teal.b;
	elseif colorIndex <= 10 then
		colorR, colorG, colorB = colors.skyBlue.r, colors.skyBlue.g, colors.skyBlue.b;
	elseif colorIndex <= 11 then
		colorR, colorG, colorB = colors.pink.r, colors.pink.g, colors.pink.b;
	elseif colorIndex <= 12 then
		colorR, colorG, colorB = colors.purple.r, colors.purple.g, colors.purple.b;
	elseif colorIndex <= 13 then
		colorR, colorG, colorB = colors.white.r, colors.white.g, colors.white.b;
	elseif colorIndex <= 14 then
		colors = cowboyHat.colors.player.band;
		colorR, colorG, colorB = colors.r, colors.g, colors.b;
	end;

	return colorR, colorG, colorB;

end;

PlayerStyle.setHat = function(self, hatIndex)

    local result = false;

	if self.player ~= nil and self.playerHatNode ~= nil then

		if self.hatNode ~= nil then self:removeHat(); end;

		if cowboyHat.isHelperHat then -- Load Helper Hat
			local rndm = math.random(1,6);
			if rndm > cowboyHat.styleCount then rndm = rndm - cowboyHat.styleCount; end; -- Half the chance for a wedged hat style.
			self.hatNode = loadI3DFile(cowboyHat.dir..cowboyHat.hatFilename[rndm], false, false, false);
		else -- Load Player Hat
			local index = cowboyHat.plyrModelIndex;
			--if index == 0 then index = cowboyHat.plyrModel; end;
			self.hatNode = loadI3DFile(cowboyHat.dir..cowboyHat.hatFilename[index], false, false, false);
		end;

		if self.hatNode ~= nil then
			self:setHair(0, true);
			link(self.playerHatNode, self.hatNode);
			cowboyHat:setHatColors(self);
			self.selectedHatIndex = hatIndex;
			result = true;
		end;
	else
		g_logManager:devError("-- [PlayerStyle:setHat] Player not set. Missing call to PlayerStyle:loadXML().")
	end;

	if cowboyHat.equipGloves then
		if self.protection.glovesNode ~= nil then
			local gloves = self.protection.glovesNode;
			--ConfigurationUtil.replaceMaterialRec(gloves, gloves, getMaterial(gloves, 0), getMaterial(cboyHatShape, 0))
			--gloves = loadI3DFile(cowboyHat.dir..cowboyHat.glovesFilename, false, false, false); --requires skin bindings
			setVisibility(gloves, true);
		end;
    end;

    return result;

end;

Enterable.setRandomVehicleCharacter = function(self)
    local spec = self.spec_enterable;
    if spec.vehicleCharacter ~= nil then
		if not cowboyHat.isHelperHat then
			cowboyHat.isHelperHat = true; -- Set Alternate Hat Coloring. (Sets back to false after color is set)
		end;
        local aiPlayerModelIndex = math.random(1, g_playerModelManager:getNumOfPlayerModels());
        local playerModel = g_playerModelManager:getPlayerModelByIndex(aiPlayerModelIndex);
        self:setVehicleCharacter(playerModel.xmlFilename, PlayerStyle:new());
        local aiStyle = self:getCurrentPlayerStyle();
        local oldStyle = self:getUserPlayerStyle();
        local numOfBodys = #aiStyle.bodies;
        local aiBodyIndex = math.random(1, numOfBodys);
        if oldStyle ~= nil then
            if aiStyle.selectedModelIndex == oldStyle.selectedModelIndex then
                local oldBodyIndex = oldStyle.selectedBodyIndex;
                if numOfBodys > 1 then
                    while aiBodyIndex == oldBodyIndex do
                        aiBodyIndex = math.random(1, numOfBodys);
                    end;
                end;
            end;
        end;
        aiStyle:setBody(aiBodyIndex);
        aiStyle:setColor(math.random(1, #g_playerColors));
		if not cowboyHat.aiGetHats then		
			aiStyle:setHair(math.random(1, #aiStyle.hairs.styles))
		end;
        aiStyle:setJacket(math.random(0, #aiStyle.jackets));
    end;
end;
------------------------------------------------------------------------------------------------------

function cowboyHat:loadMap(savegame)

	if cowboyHat.rainbow then
		VehicleCharacter.update = Utils.appendedFunction(cowboyHat.updatePlayer, VehicleCharacter.update)	
	end;		

	FSBaseMission.saveSavegame = Utils.appendedFunction(FSBaseMission.saveSavegame, cowboyHat.saveSettings);		
	Player.onLeaveVehicle = Utils.appendedFunction(cowboyHat.updateThirdPersonHat, Player.onLeaveVehicle);

	cowboyHat:loadSettings();

end;

cowboyHat.dt = 0;
function cowboyHat:updatePlayer(dt)

	local limit = 20;
	cowboyHat.dt = cowboyHat.dt + 1;
	if cowboyHat.dt == limit then
		if self.visualInformation ~= nil then
			if self.visualInformation.hatNode ~= nil then
				local cboyHatShape = getChildAt(self.visualInformation.hatNode, 0);
				cboyHatShape = getChildAt(cboyHatShape, 0);
				if not cowboyHat.isHelperHat then
					local colors = cowboyHat.colors.player;
					if getHasShaderParameter(cboyHatShape, "colorMat0") then
						local colorR, colorG, colorB = cowboyHat:getHatColor(true, false, true);
						setShaderParameter(cboyHatShape, "colorMat0", colorR, colorG, colorB, colors.mat.var, false);
						--setShaderParameter(cboyHatShape, "colorMat0", colors.mat.r, colors.mat.g, colors.mat.b, colors.mat.var, false);
						local bandColorR, bandColorG, bandColorB = cowboyHat:getBandColor(false,true,false);
						setShaderParameter(cboyHatShape, "colorMat1", bandColorR, bandColorG, bandColorB, colors.band.var, false);
					end;
				end;
			end;
		end;
	elseif cowboyHat.dt >= limit then
		cowboyHat.dt = 0;
	end;

end;

function cowboyHat:updateThirdPersonHat()
	if self.playerCamera2 ~= nil then
		if self.visualInformation ~= nil then
			if self.visualInformation.hatNode ~= nil then
				self.visualInformation.hatNode = loadI3DFile(cowboyHat.dir..cowboyHat.hatFilename[cowboyHat.plyrModelIndex], false, false, false);				
				link(self.visualInformation.playerHatNode, self.visualInformation.hatNode);
				cowboyHat:setHatColors(self.visualInformation);
			end;
		end;
	end;
end;

addModEventListener(cowboyHat);
------------------------------------------------------------------------------------------------------

function cowboyHat:saveSettings()

	local savegameFolderPath = g_currentMission.missionInfo.savegameDirectory.."/";
	if savegameFolderPath == nil then
		savegameFolderPath = ('%ssavegame%d'):format(getUserProfileAppPath(), g_currentMission.missionInfo.savegameIndex.."/");
	end;
	local key = "cowboyHat";
	local xmlFile = createXMLFile(key, savegameFolderPath.."cowboyHat.xml", key);

	setXMLBool(xmlFile, key..".helperHats.aiGetHats#value", cowboyHat.aiGetHats);
	 key = "cowboyHat.playerHat.";
	setXMLInt(xmlFile, key.."savegame.styleIndex#value", cowboyHat.plyrModelIndex);
	setXMLInt(xmlFile, key.."savegame.matIndex#value", cowboyHat.colors.mat.index);
	setXMLInt(xmlFile, key.."savegame.bandIndex#value", cowboyHat.colors.band.index);
	
	setXMLFloat(xmlFile, key.."material.red#value", cowboyHat.colors.player.mat.r);
	setXMLFloat(xmlFile, key.."material.green#value", cowboyHat.colors.player.mat.g);
	setXMLFloat(xmlFile, key.."material.blue#value", cowboyHat.colors.player.mat.b);
	setXMLFloat(xmlFile, key.."band.red#value", cowboyHat.colors.player.band.r);
	setXMLFloat(xmlFile, key.."band.green#value", cowboyHat.colors.player.band.g);
	setXMLFloat(xmlFile, key.."band.blue#value", cowboyHat.colors.player.band.b);

	local leatherValue = false;
	local platedValue = false;
	if cowboyHat.colors.player.mat.var == 11.5 then 
		leatherValue = true; 
	end;
	if cowboyHat.colors.player.band.var == 2.0 then 
		platedValue = true; 
	end;
	setXMLBool(xmlFile, key.."material.leather#value", leatherValue);
	setXMLBool(xmlFile, key.."band.plated#value", platedValue);

	saveXMLFile(xmlFile);

	delete(xmlFile);

end;

function cowboyHat:loadSettings()

	local savegameFolderPath = g_currentMission.missionInfo.savegameDirectory;
	--print(savegameFolderPath.." : Location");
	if savegameFolderPath == nil then
		savegameFolderPath = ('%ssavegame%d'):format(getUserProfileAppPath(), g_currentMission.missionInfo.savegameIndex);
		--print(savegameFolderPath.." : Location");
	end;
	savegameFolderPath = savegameFolderPath.."/"
	local key = "cowboyHat";

	if fileExists(savegameFolderPath.."cowboyHat.xml") then

		local xmlFile = loadXMLFile(key, savegameFolderPath.."cowboyHat.xml");

		cowboyHat.aiGetHats = Utils.getNoNil(getXMLBool(xmlFile, key.."helperHats.aiGetHats#value"), cowboyHat.aiGetHats);
		key = "cowboyHat.playerHat.";
		cowboyHat.plyrModelIndex = Utils.getNoNil(getXMLInt(xmlFile, key.."savegame.styleIndex#value"), cowboyHat.plyrModelIndex);
		cowboyHat.colors.mat.index = Utils.getNoNil(getXMLInt(xmlFile, key.."savegame.matIndex#value"), cowboyHat.colors.mat.index);
		cowboyHat.colors.band.index = Utils.getNoNil(getXMLInt(xmlFile, key.."savegame.bandIndex#value"), cowboyHat.colors.band.index);
		
		cowboyHat.colors.player.mat.r = Utils.getNoNil(getXMLFloat(xmlFile, key.."material.red#value"), cowboyHat.colors.player.mat.r);
		cowboyHat.colors.player.mat.g = Utils.getNoNil(getXMLFloat(xmlFile, key.."material.green#value"), cowboyHat.colors.player.mat.g);
		cowboyHat.colors.player.mat.b = Utils.getNoNil(getXMLFloat(xmlFile, key.."material.blue#value"), cowboyHat.colors.player.mat.b);
		cowboyHat.colors.player.band.r = Utils.getNoNil(getXMLFloat(xmlFile, key.."band.red#value"), cowboyHat.colors.player.band.r);
		cowboyHat.colors.player.band.g = Utils.getNoNil(getXMLFloat(xmlFile, key.."band.green#value"), cowboyHat.colors.player.band.g);
		cowboyHat.colors.player.band.b = Utils.getNoNil(getXMLFloat(xmlFile, key.."band.blue#value"), cowboyHat.colors.player.band.b);
		
		local leatherValue = Utils.getNoNil(getXMLBool(xmlFile, key.."material.leather#value"),cowboyHat.colors.player.mat.var);
		local platedValue = Utils.getNoNil(getXMLBool(xmlFile, key.."band.plated#value"), cowboyHat.colors.player.band.var);

		if leatherValue then
			cowboyHat.colors.player.mat.var = 11.5;
		else
			cowboyHat.colors.player.mat.var = 11.0;
		end;		
		if platedValue then
			cowboyHat.colors.player.band.var = 2.0;
		else
			cowboyHat.colors.player.band.var = 1.0;
		end;
		--print("loaded hat settings");
		delete(xmlFile);

	end;

end;
------------------------------------------------------------------------------------------------------