function DetermineAvgRatingForTeam( teamId, numPlayers )
teamId = Engine.StackRankTeamByOverall( teamId )
local totalOverall = 0
for playerCount = 1, numPlayers do
local currPlayer = Engine.GetPlayer( teamId, playerCount )
totalOverall = totalOverall + Engine.GetPlayerOverall( currPlayer )
end --for playerCount = 1, numPlayers do
local average = totalOverall / numPlayers
return average
end
function ModifyTeamMorale( teamId, change )
local numPlayersOnTeam = Engine.GetNumPlayers( teamId )
for playerCount = 1, numPlayersOnTeam do
local currentPlayer = Engine.GetPlayer( teamId, playerCount )
Engine.ModifyPlayerMorale( teamId, currentPlayer, change )
end
end
function MakeCPUOfferForPlayer( teamId, playerId, numOffers, startRandom )
local inWindow = Engine.IsWithinTransferWindow()
if( inWindow == true ) then
local maxOffers = 5
if( numOffers > maxOffers ) then
numOffers = maxOffers
end --if( numOffers > maxOffers ) then
local offers = {}
local offer1 = 0
local offer2 = 0
local offer3 = 0
local offer4 = 0
local offer5 = 0
local actualOffers, offer1, offer2, offer3, offer4, offer5 = Engine.FindSuitableTeamsForPlayer( teamId, playerId, numOffers, true )
offers[ 1 ] = offer1
offers[ 2 ] = offer2
offers[ 3 ] = offer3
offers[ 4 ] = offer4
offers[ 5 ] = offer5
if( actualOffers < startRandom ) then
startRandom = actualOffers
end --if( actualOffers < startRandom ) then
if( actualOffers < numOffers ) then
numOffers = actualOffers
end --if( actualOffers < numOffers ) then
local teamChoice = Engine.GetRandomNumber( startRandom, numOffers )
Engine.CreateOfferForPlayer( teamId, playerId, offers[ teamChoice ] )
end --if( inWindow == true ) then
end
-- Player Upset With Club Functions
function GetReasonForPlayerUpset( teamId, playerId )
local match = 0
if( IsUnderachieving( teamId, playerId ) == true ) then
match = 1
elseif( IsTooBigForTeam( teamId, playerId ) == true ) then
match = 2
elseif( IsUnhappyWithSalary( teamId, playerId ) == true ) then
match = 3
end
return match
end
function IsTooBigForTeam( teamId, playerId )
local tooBig = false
teamId = Engine.StackRankTeamByOverall( teamId )
local playerRank = Engine.GetPlayerIndexById( teamId, playerId )
if( playerRank == 1 ) then -- top play
local playerOverall = Engine.GetPlayerOverall( playerId )
local secondBestPlayer = Engine.GetPlayer( teamId, 2 )
local secondBestOverall = Engine.GetPlayerOverall( secondBestPlayer )
local overallDiff = Tuning.MISC.ovrDiff_BestPlayer
if( playerOverall >= secondBestOverall + overallDiff ) then
tooBig = true
end
elseif( playerRank <= Tuning.MISC.topPlayerCount ) then -- top five player
local potentialDiff = Tuning.MISC.potDiff_TopPlayers
local playerOverall = Engine.GetPlayerOverall( playerId )
local playerPotential = Engine.GetPlayerPotential( teamId, playerId )
if( playerPotential >= playerOverall + potentialDiff ) then
tooBig = true
end
end
return tooBig
end
function GetTeamPlayerAvg( teamId, numPlayers )
local totalAvg = 0
teamId = Engine.StackRankTeamByOverall( teamId )
local loopToPlayers = numPlayers
local numPlayersOnTeam = Engine.GetNumPlayers( teamId )
if( numPlayersOnTeam < loopToPlayers ) then
loopToPlayers = numPlayersOnTeam
end
for playerCount = 1, loopToPlayers do
local currPlayer = Engine.GetPlayer( teamId, playerCount )
local playerOverall = Engine.GetPlayerOverall( currPlayer )
totalAvg = totalAvg + playerOverall
end
totalAvg = totalAvg / numPlayers
return totalAvg
end
function GetUserImportance()
local fixture = Engine.GetCurrentGameFixtureData()
local team1, team2 = Engine.GetTeamsFromFixtureData( fixture )
local teamMI1, teamMI2 = Engine.GetMatchImportance( fixture )
local matchImp = 0
if( team1 == Engine.GetUserTeam() ) then
matchImp = teamMI1
else
matchImp = teamMI2
end
return matchImp
end
function GetUserCpuTeamFromIds( team1, team2 )
local userTeam = team1
local cpuTeam = team2
if( team1 ~= Engine.GetUserTeam() ) then
cpuTeam = team1
userTeam = team2
end
return userTeam, cpuTeam
end
function GetTeamsFromFixture( fixture )
local team1, team2 = Engine.GetTeamsFromFixtureData( fixture )
local userTeam, cpuTeam = GetUserCpuTeamFromIds( team1, team2 )
return userTeam, cpuTeam
end
function GetAudioPositionId( teamId, playerId )
local posId = 14 -- Defaults to the striker positions
local position = Engine.GetPlayerPosition( teamId, playerId )
if( position == "GK" ) then
posId = 06
elseif( position == "SW" ) then
posId = 07
elseif( position == "RWB" ) then
posId = 08
elseif( position == "RB" ) then
posId = 08
elseif( position == "RCB" ) then
posId = 07
elseif( position == "CB" ) then
posId = 07
elseif( position == "LCB" ) then
posId = 07
elseif( position == "LB" ) then
posId = 09
elseif( position == "LWB" ) then
posId = 09
elseif( position == "RDM" ) then
posId = 10
elseif( position == "CDM" ) then
posId = 10
elseif( position == "LDM" ) then
posId = 10
elseif( position == "RM" ) then
posId = 11
elseif( position == "RCM" ) then
posId = 10
elseif( position == "CM" ) then
posId = 10
elseif( position == "LCM" ) then
posId = 10
elseif( position == "LM" ) then
posId = 12
elseif( position == "RAM" ) then
posId = 13
elseif( position == "CAM" ) then
posId = 13
elseif( position == "LAM" ) then
posId = 13
elseif( position == "RW" ) then
posId = 11
elseif( position == "LW" ) then
posId = 12
end
return posId
end
function WouldPlayerEvenLeaveClub( teamId, playerId )
local monthsJoined = Engine.GetNumberOfMonthsSincePlayerJoined( teamId, playerId )
local numYears = Engine.GetNumberOfYearsSincePlayerJoined( teamId, playerId )
local playerAge = Engine.GetPlayerAge( teamId, playerId )
local oneClubPlayer = Engine.HasPlayerTrait( teamId, playerId, "ONE_CLUB_PLAYER" )
local isListed = Engine.IsPlayerOnTransferList( playerId )
local loanListed = Engine.IsPlayerOnLoanList( playerId )
local inOnLoan = Engine.IsPlayerInOnLoan( teamId, playerId )
local isRetiring = Engine.IsRetiring( playerId )
local isPap = ( playerId == Engine.GetPlayAsPlayerID() )
local injuryDuration = Engine.GetInjuryDuration( teamId, playerId )
local numDaysOfInjury = Tuning.MISC.numDaysInjured
local playerFitEnough = ( injuryDuration < numDaysOfInjury )
local hasSignedPreContract = Engine.HasSignedPreContract( playerId )
local retireStory = Engine.IsPlayerInvolvedInStoryId( playerId, "POTRET" )
local wouldLeave = ( ( numYears <= Tuning.MISC.numYearsOnTeam or playerAge <= Tuning.MISC.playerAge ) and ( oneClubPlayer == false ) and ( inOnLoan == false ) and ( isRetiring == false ) and ( monthsJoined >= Tuning.MISC.monthsJoined ) and ( isPap == false ) and ( playerFitEnough == true ) and ( isListed == false ) and ( loanListed == false ) and ( hasSignedPreContract == false ) and ( retireStory == false ) )
return wouldLeave
end
function GetSuitableTeamsForPlayer( teamId, playerId, numOffers, ignoreBudget )
local offer = { -1, -1, -1 }
local actualOffers = 0
if( numOffers <= 3 ) then
local badOfferFound = false
actualOffers, offer[ 1 ], offer[ 2 ], offer[ 3 ] = Engine.FindSuitableTeamsForPlayer( teamId, playerId, numOffers, ignoreBudget )
for offerCount = 1, actualOffers do
if( offer[ offerCount ] == -1 ) then
badOfferFound = true
break
end
end
if( badOfferFound == true ) then
offer[ 1 ], offer[ 2 ], offer[ 3 ] = FindSuitablePlayersSafety( playerId, numOffers )
end
else
Engine.LuaAssert( "FindSuitablePlayers - Too many offers requested: " .. numOffers )
end
return actualOffers, offer[ 1 ], offer[ 2 ], offer[ 3 ]
end
function FindSuitablePlayersSafety( playerId, numOffers )
local highTeam = Tuning.MISC.highTeamIds
local numHighTeams = table.getn( highTeam )
local userTeam = Engine.GetUserTeam()
if( numOffers == nil ) then
numOffers = 3
end
for teamCount = 1, numHighTeams do
if( userTeam == highTeam[ teamCount ] ) then
table.remove( highTeam, teamCount )
numHighTeams = table.getn( highTeam )
break
end
end
randNum = Engine.GetRandomNumber( 1, numHighTeams - numOffers )
return highTeam[ randNum ], highTeam[ randNum + 1 ], highTeam[ randNum + 2 ]
end