IsCrowd3D = 0

function CrowdUpdate()
	local as = gSportsRNA
	local state = as:GetTable("wvState")
	local crowd = as:GetTable("wvCrowd")
		
	db.crowd.lightID = as:GetInt(state, "wvAttribEnvLighting")
	db.crowd.homeTeamID = as:GetInt(crowd, "homeTeamID")
	db.crowd.awayTeamID = as:GetInt(crowd, "awayTeamID")
	
	db.crowd.homeKitTeamID = as:GetInt(crowd, "homeKitTeamID")
	db.crowd.homeKitTypeID = as:GetInt(crowd, "homeKitTypeID")
	db.crowd.awayKitTeamID = as:GetInt(crowd, "awayKitTeamID")
	db.crowd.awayKitTypeID = as:GetInt(crowd, "awayKitTypeID")
	
	db.crowd.homeTeamID = db.crowd.homeKitTeamID
  db.crowd.awayTeamID = db.crowd.awayKitTeamID

	local weather = as:GetInt(state, "wvAttribStadWeather" )
	if (weather == 1 or weather == 2) then
		db.crowd.wet = 1
	else
		db.crowd.wet = 0
	end
	
	
	local wipe3d = as:GetTable("wvWipe", 1)
	local leagueID = as:GetInt(wipe3d, "leagueID")
	leagueID = getTournamentGraphics(leagueID)
	

	local homecrowdsize = 4 - getHomeCrowdSize(db.crowd.homeKitTeamID,db.crowd.awayKitTeamID,leagueID)
	local awaycrowdsize = 4 - getAwayCrowdSize(db.crowd.homeKitTeamID,db.crowd.awayKitTeamID,leagueID)
	
		
	db.crowd.crowdvisable = {}
	
	local characters = 4
	local groupcount = 3
	
	for i=0,groupcount-1 do
	db.crowd.crowdvisable[i] = {}
		for j=0,characters-1 do
		
		if ( ((i == 0) and (j >= homecrowdsize)) or ((i == 1) and (j >= homecrowdsize)) or ((i == 2) and (j >= awaycrowdsize)) ) then
		db.crowd.crowdvisable[i][j] = "rx3"
		else
		db.crowd.crowdvisable[i][j] = "xxx"
		end
		
		end
	end
	
	
	db.crowd.seatvisable = "rx3"
	
	if (getRemoveSeats(db.crowd.homeKitTeamID)) then
	db.crowd.seatvisable = "xxx"
	end
	
	
end

function CrowdAssetBind(crowd)
	local gr = gRenderables
	
	local settingTable = gSportsRNA:GetTable("Settings")
	gSportsRNA:SetInt(settingTable, "IsCrowd3D", IsCrowd3D) 
	
	local lod = 0
	local priority = 1
	
	gr:AddCallback(crowd, lod, "CrowdUpdate()")
	
	gr:AddAsset(crowd, lod, "shader", "data/fifarna/shader.big", priority)
	
	-- Characters per group
	local characters = 4
	local groupcount = 3
	local path = "data/sceneassets/crowd/"
	local pathCrowd3d = "data/sceneassets/crowd13/"
	
	gr:AddAsset(crowd, lod, "crowd_common_textures", path.."crowd_common_textures.rx3", priority)
	
	for i=0,groupcount-1 do
		for j=0,characters-1 do
			local meshpart = "bodymesh_"..i.."_"..j
			local texpart = "bodytex_"..i.."_"..j
			-- if it's the 4th mesh in an away or neutral zone, create a jersey crowd character
			-- i == 0 -> neutral section
			-- j == 3 -> 4th mesh
			
			if (IsCrowd3D == 1) then
				-- todo: FS4 assets
				local ethnicity = 0
				local glod = 3
				gr:AddAsset(crowd, lod, meshpart, pathCrowd3d.."crowd_"..j.."_"..ethnicity.."_"..glod..".rx3", priority)
				gr:AddAsset(crowd, lod, texpart, pathCrowd3d.."crowd_"..j.."_"..ethnicity.."_0_textures.rx3", priority)
				
				gr:CreateMaterial(crowd, lod, meshpart, "crowd_body.fx")
				gr:SetTexture(crowd, lod, meshpart, "textures", "diffuseTexture", texpart, "") -- grabs first texture from the texlib
			else
				if (j == 3 and i > 0) then
					-- create mesh for body without jersey
					local bodymesh = meshpart.."_0"
					gr:AddAsset(crowd, lod, bodymesh, path.."crowd_0_4_${db.crowd.lightID}_${db.crowd.wet}.${db.crowd.crowdvisable["..i.."]["..j.."]}", priority)
					gr:AddAsset(crowd, lod, texpart, path.."crowd_0_4_${db.crowd.lightID}_${db.crowd.wet}_textures.rx3", priority)
					
					gr:CreateMaterial(crowd, lod, bodymesh, "crowd_body.fx")
					gr:SetSubMesh(crowd, lod, bodymesh, "crowd_body")
					gr:SetTexture(crowd, lod, bodymesh, "textures", "diffuseTexture", texpart, "") -- grabs first texture from the texlib
					
					-- create mesh for jersey
					local jerseymesh = meshpart.."_1"
					gr:AddAsset(crowd, lod, jerseymesh, path.."crowd_0_4_${db.crowd.lightID}_${db.crowd.wet}.${db.crowd.crowdvisable["..i.."]["..j.."]}", priority)
					
					gr:CreateMaterial(crowd, lod, jerseymesh, "crowd_jersey.fx")
					gr:SetSubMesh(crowd, lod, jerseymesh, "crowd_jersey")
					if i == 1 then
						-- home group
						gr:SetTextureFromRuntime(crowd, lod, jerseymesh, "textures", "diffuseTexture", "jersey_${db.crowd.homeKitTeamID}_${db.crowd.homeKitTypeID}_0_0_-1")
					else
						-- away group
						gr:SetTextureFromRuntime(crowd, lod, jerseymesh, "textures", "diffuseTexture", "jersey_${db.crowd.awayKitTeamID}_${db.crowd.awayKitTypeID}_0_1_-1")
					end
					gr:SetTexture(crowd, lod, jerseymesh, "textures", "detailTexture", "crowd_common_textures", "crowdjersey_am")
				else
					gr:AddAsset(crowd, lod, meshpart, path.."crowd_0_"..j.."_${db.crowd.lightID}_${db.crowd.wet}.${db.crowd.crowdvisable["..i.."]["..j.."]}", priority)
					gr:AddAsset(crowd, lod, texpart, path.."crowd_0_"..j.."_${db.crowd.lightID}_${db.crowd.wet}_textures.rx3", priority)
					
					gr:CreateMaterial(crowd, lod, meshpart, "crowd_body.fx")
					gr:SetTexture(crowd, lod, meshpart, "textures", "diffuseTexture", texpart, "") -- grabs first texture from the texlib
				end
			end
		end
	end
	
	
	-- Chairs
	gr:AddAsset(crowd, lod, "chairmesh_0", "data/sceneassets/crowdchair/crowdchair_0.${db.crowd.seatvisable}", priority)
	gr:AddAsset(crowd, lod, "chairtex_0", "data/sceneassets/crowdchair/chaircmn_textures.rx3", priority)
	gr:CreateMaterial(crowd, lod, "chairmesh_0", "crowd_chair.fx")
	gr:SetTexture(crowd, lod, "chairmesh_0", "textures", "diffuseTexture", "chairtex_0", "")

	-- Scarves
	local scarfcount = 4
	for i=0,groupcount-1 do
		for j=0,scarfcount-1 do
			local meshpart = "scarfmesh_"..i.."_"..j
			local texpart = "scarftex_"..i.."_"..j
			local flagindex = math.fmod(j, 2)
			gr:AddAsset(crowd, lod, meshpart, path.."crowd_scarf_0.${db.crowd.crowdvisable["..i.."]["..j.."]}", priority)
			gr:CreateMaterial(crowd, lod, meshpart, "crowd_accs.fx")
			
			if (i < 2) then
				gr:AddAsset(crowd, lod, texpart, "data/sceneassets/flag/scarf_${db.crowd.homeTeamID}.rx3;data/sceneassets/flag/flag_${db.crowd.homeTeamID}.rx3", priority)
				gr:SetTexture(crowd, lod, meshpart, "textures", "diffuseTexture", texpart, "flag_${db.crowd.homeTeamID}_"..flagindex)
			else
				gr:AddAsset(crowd, lod, texpart, "data/sceneassets/flag/scarf_${db.crowd.awayTeamID}.rx3;data/sceneassets/flag/flag_${db.crowd.awayTeamID}.rx3", priority)
				gr:SetTexture(crowd, lod, meshpart, "textures", "diffuseTexture", texpart, "flag_${db.crowd.awayTeamID}_"..flagindex)
			end
		end
	end

	-- banner textures
	-- names indexed as follows
	-- 0 - empty
	-- 1 - home
	-- 2 - away
	-- 3 - neutral
	gr:AddAsset(crowd, lod, "bannertex0", "data/sceneassets/banner/banner_0.rx3", priority)
	gr:AddAsset(crowd, lod, "bannertex1", "data/sceneassets/banner/banner_${db.crowd.homeTeamID}.rx3;data/sceneassets/banner/banner_0.rx3", priority)
	gr:AddAsset(crowd, lod, "bannertex2", "data/sceneassets/banner/banner_${db.crowd.awayTeamID}.rx3;data/sceneassets/banner/banner_0.rx3", priority)
	gr:AddAsset(crowd, lod, "bannertex3", "data/sceneassets/banner/banner_0.rx3", priority)

	return crowd
end





teamSeatsRemoved = {}
-- stadiumSeatsRemoved = {}

function removeSeatsTeam(team)
teamSeatsRemoved[team] = 1
end


function getRemoveSeats(team)
-- if (stadiumSeatsRemoved[stad] == 1) then
-- return true
-- end

if (teamSeatsRemoved[team] == 1) then
return true
end

return false
end



homeCrowdTeam = {}
awayCrowdTeam = {}

homeCrowdGame = {}
awayCrowdGame = {}


homeCrowdTournamentTeam = {}
awayCrowdTournamentTeam = {}

homeCrowdTournamentGame = {}
awayCrowdTournamentGame = {}

homeCrowdTournament = {}
awayCrowdTournament = {}


function setTeamCrowdSize(team,home,away)

if (type(home) == "number") then
homeCrowdTeam[team] = home
else
homeCrowdTeam[team] = home[math.random(#home)]
end

if (type(away) == "number") then
awayCrowdTeam[team] = away
else
awayCrowdTeam[team] = away[math.random(#away)]
end

end


function setTournamentCrowdSize(tourn,home,away)

if (type(home) == "number") then
homeCrowdTournament[tourn] = home
else
homeCrowdTournament[tourn] = home[math.random(#home)]
end

if (type(away) == "number") then
awayCrowdTournament[tourn] = away
else
awayCrowdTournament[tourn] = away[math.random(#away)]
end

end


function setGameCrowdSize(hometeam,awayteam,home,away)
if (homeCrowdGame[hometeam] == nil) then
homeCrowdGame[hometeam] = {}
end

if (awayCrowdGame[hometeam] == nil) then
awayCrowdGame[hometeam] = {}
end

if (type(home) == "number") then
homeCrowdGame[hometeam][awayteam] = home
else
homeCrowdGame[hometeam][awayteam] = home[math.random(#home)]
end

if (type(away) == "number") then
awayCrowdGame[hometeam][awayteam] = away
else
awayCrowdGame[hometeam][awayteam] = away[math.random(#away)]
end

end


function setTournamentTeamCrowdSize(team,tourn,home,away)
if (homeCrowdTournamentTeam[team] == nil) then
homeCrowdTournamentTeam[team] = {}
end

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

if (type(home) == "number") then
homeCrowdTournamentTeam[team][tourn] = home
else
homeCrowdTournamentTeam[team][tourn] = home[math.random(#home)]
end

if (type(away) == "number") then
awayCrowdTournamentTeam[team][tourn] = away
else
awayCrowdTournamentTeam[team][tourn] = away[math.random(#away)]
end

end


function setTournamentGameCrowdSize(hometeam,awayteam,tourn,home,away)
if (homeCrowdTournamentGame[hometeam] == nil) then
homeCrowdTournamentGame[hometeam] = {}
end

if (awayCrowdTournamentGame[hometeam] == nil) then
awayCrowdTournamentGame[hometeam] = {}
end

if (homeCrowdTournamentGame[hometeam][awayteam] == nil) then
homeCrowdTournamentGame[hometeam][awayteam] = {}
end

if (awayCrowdTournamentGame[hometeam][awayteam] == nil) then
awayCrowdTournamentGame[hometeam][awayteam] = {}
end

if (type(home) == "number") then
homeCrowdTournamentGame[hometeam][awayteam][tourn] = home
else
homeCrowdTournamentGame[hometeam][awayteam][tourn] = home[math.random(#home)]
end

if (type(away) == "number") then
awayCrowdTournamentGame[hometeam][awayteam][tourn] = away
else
awayCrowdTournamentGame[hometeam][awayteam][tourn] = away[math.random(#away)]
end

end





function getHomeCrowdSize(hometeam,awayteam,tourn)

--tournament match
if (homeCrowdTournamentGame[hometeam] ~= nil) then
if (homeCrowdTournamentGame[hometeam][awayteam] ~= nil) then
if (homeCrowdTournamentGame[hometeam][awayteam][tourn] ~= nil) then
return homeCrowdTournamentGame[hometeam][awayteam][tourn]
end
end
end

--tournament team
if (homeCrowdTournamentTeam[hometeam] ~= nil) then
if (homeCrowdTournamentTeam[hometeam][tourn] ~= nil) then
return homeCrowdTournamentTeam[hometeam][tourn]
end
end

--match
if (homeCrowdGame[hometeam] ~= nil) then
if (homeCrowdGame[hometeam][awayteam] ~= nil) then
return homeCrowdGame[hometeam][awayteam]
end
end

--team
if (homeCrowdTeam[hometeam] ~= nil) then
return homeCrowdTeam[hometeam]
end

--tournament
if (homeCrowdTournament[tourn] ~= nil) then
return homeCrowdTournament[tourn]
end

if (teamTournament[hometeam] ~= nil) then
if (homeCrowdTournament[teamTournament[hometeam]] ~= nil) then
return homeCrowdTournament[teamTournament[hometeam]]
end
end

return 4
end

function getAwayCrowdSize(hometeam,awayteam,tourn)

--tournament match
if (awayCrowdTournamentGame[hometeam] ~= nil) then
if (awayCrowdTournamentGame[hometeam][awayteam] ~= nil) then
if (awayCrowdTournamentGame[hometeam][awayteam][tourn] ~= nil) then
return awayCrowdTournamentGame[hometeam][awayteam][tourn]
end
end
end

--tournament team
if (awayCrowdTournamentTeam[awayteam] ~= nil) then
if (awayCrowdTournamentTeam[awayteam][tourn] ~= nil) then
return awayCrowdTournamentTeam[awayteam][tourn]
end
end

--match
if (awayCrowdGame[hometeam] ~= nil) then
if (awayCrowdGame[hometeam][awayteam] ~= nil) then
return awayCrowdGame[hometeam][awayteam]
end
end

--team
if (awayCrowdTeam[awayteam] ~= nil) then
return awayCrowdTeam[awayteam]
end

--tournament
if (awayCrowdTournament[tourn] ~= nil) then
return awayCrowdTournament[tourn]
end

if (teamTournament[awayteam] ~= nil) then
if (awayCrowdTournament[teamTournament[awayteam]] ~= nil) then
return awayCrowdTournament[teamTournament[awayteam]]
end
end

return 4
end

--Revolution Mod 14 1.0
--Edited by scouser09
