From 078cf336c924f4835a1803522f5cec9aab3f2a68 Mon Sep 17 00:00:00 2001 From: Beu Date: Mon, 10 Oct 2022 15:40:23 +0200 Subject: [PATCH] Add CPKnockout Script --- TM_CPKnockout.Script.txt | 392 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 392 insertions(+) create mode 100644 TM_CPKnockout.Script.txt diff --git a/TM_CPKnockout.Script.txt b/TM_CPKnockout.Script.txt new file mode 100644 index 0000000..4f42f2d --- /dev/null +++ b/TM_CPKnockout.Script.txt @@ -0,0 +1,392 @@ +/** + * CP Knockout mode + */ +// #RequireContext CSmMode + +#Extends "Libs/Nadeo/TMNext/TrackMania/Modes/TMNextBase.Script.txt" + +#Const CompatibleMapTypes "TrackMania\\TM_Race,TM_Race" +#Const Version "2022-05-20" +#Const ScriptName "Modes/TM2020-Gamemodes/TM_CPKnockout.Script.txt" + +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // +// Libraries +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // +#Include "TextLib" as TL +#Include "MathLib" as ML +#Include "Libs/Nadeo/TMNext/TrackMania/Menu/Constants.Script.txt" as MenuConsts +#Include "Libs/Nadeo/TMNext/TrackMania/Modes/Rounds/StateManager.Script.txt" as StateMgr +#Include "ManiaApps/Nadeo/TMxSM/Race/UIModules/ScoresTable_Server.Script.txt" as UIModules_ScoresTable +#Include "ManiaApps/Nadeo/TMxSM/Race/UIModules/TimeGap_Server.Script.txt" as UIModules_TimeGap +#Include "ManiaApps/Nadeo/TMxSM/Race/UIModules/Checkpoint_Server.Script.txt" as UIModules_Checkpoint +#Include "ManiaApps/Nadeo/TMxSM/Race/UIModules/PauseMenuOnline_Server.Script.txt" as UIModules_PauseMenu_Online + +#Include "ManiaApps/Nadeo/TMxSM/Race/UIModules/BigMessage_Server.Script.txt" as UIModules_BigMessage + +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // +// Settings +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // +#Setting S_DisableGiveUp True as _("Disable give up") +#Setting S_NumberOfFinishers 10 +#Setting S_WarmUpNb 0 as _("Number of warm up") +#Setting S_WarmUpDuration 0 as _("Duration of one warm up") +#Setting S_WarmUpTimeout -1 as _("Warm up timeout") + +#Setting S_EliminatedPlayersNbRanks "10,0,0,0,0,0,0,0,0,0,0,10,0,0,0,0,0,0,0,0,0,10,0,0,0,0,0,0,0,0,0,10,0,0,0,0,0,0,0,0,0,10,0,0,0,0,0,0,0,0,0,10,0,0,0,0,0,0,0,0,0,10,0,0,0,0,0,0,0,0,0,10,0,0,0,0,0,0,0,0,0,10,0,0,0,0,0,0,0,0,0,10,0,0,0,0,0,0,0,0,0" as _("Nb of players above which one extra elim. /CP. Same setting of Knock") + + +#Struct K_MatchState { + Integer NbAliveAfter; + Integer NbEliminations; + Integer NbFinishers; +} + + + +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // +// Constants +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // +#Const C_ModeName "CP Knockout" +//L16N [Laps] Description of the mode rules +#Const Description _("$zIn $<$t$6F9Laps$> mode, the goal is to drive as far as possible by passing $<$t$6F9checkpoints$>.\n\nThe laps mode takes place on multilap (cyclical) maps, and is played in one go for every map.\n\nWhen the time is up, the $<$t$6F9winner$> is the player who passed the most $<$t$6F9checkpoints$>. In case of draws, the winner is the player who passed the last checkpoint first.") + +#Const C_HudModulePath "" //< Path to the hud module +#Const C_ManiaAppUrl "file://Media/ManiaApps/Nadeo/TMNext/TrackMania/Laps/Laps.Script.txt" //< Url of the mania app + +#Const C_UploadRecord True +#Const C_DisplayRecordGhost False +#Const C_DisplayRecordMedal False +#Const C_CelebrateRecordGhost True +#Const C_CelebrateRecordMedal True + +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // +// Extends +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // +***Match_LogVersions*** +*** +Log::RegisterScript(ScriptName, Version); +Log::RegisterScript(StateMgr::ScriptName, StateMgr::Version); +*** + +***Match_LoadLibraries*** +*** +StateMgr::Load(); +*** + +***Match_UnloadLibraries*** +*** +StateMgr::Unload(); +*** + +***Match_Settings*** +*** +MB_Settings_UseDefaultHud = (C_HudModulePath == ""); +MB_Settings_UseDefaultTimer = False; +*** + +***Match_Rules*** +*** +ModeInfo::SetName(C_ModeName); +ModeInfo::SetType(ModeInfo::C_Type_FreeForAll); +ModeInfo::SetRules(Description); +ModeInfo::SetStatusMessage(_("TYPE: Free for all\nOBJECTIVE: Set the best time on the track.")); +*** + +***Match_LoadHud*** +*** +if (C_HudModulePath != "") Hud_Load(C_HudModulePath); +*** + +***Match_AfterLoadHud*** +*** +ClientManiaAppUrl = C_ManiaAppUrl; +Race::SortScores(Race::C_Sort_BestRaceCheckpointsProgress); +UIModules_TimeGap::SetTimeGapMode(UIModules_TimeGap::C_TimeGapMode_BestRace); +UIModules_Checkpoint::SetRankMode(UIModules_Checkpoint::C_RankMode_BestRace); +UIModules_Checkpoint::SetVisibilityTimeDiff(False, True); +UIModules_PauseMenu_Online::SetHelp(Description); +UIModules_ScoresTable::SetScoreMode(UIModules_ScoresTable::C_Mode_Laps); +UIModules_ScoresTable::SetHideSpectators(True); +*** + +***Match_Yield*** +*** +foreach (Event in PendingEvents) { + switch (Event.Type) { + // Initialize players when they join the server + case CSmModeEvent::EType::OnPlayerAdded: { + StateMgr::InitializePlayer(Event.Player); + CarRank::InitializePlayer(Event.Player); + } + } +} + +StateMgr::Yield(); +*** + +***Match_StartServer*** +*** +// Initialize mode +Clans::SetClansNb(0); +StateMgr::ForcePlayersStates([StateMgr::C_State_Waiting]); +WarmUp::SetAvailability(True); +Race::SetRespawnBehaviour(Race::C_RespawnBehaviour_Normal); +Race::SetupRecord( + MenuConsts::C_ScopeType_Season, + MenuConsts::C_ScopeType_PersonalBest, + MenuConsts::C_GameMode_Laps, + "", + C_UploadRecord, + C_DisplayRecordGhost, + C_DisplayRecordMedal, + C_CelebrateRecordGhost, + C_CelebrateRecordMedal +); + +Race::UseAutomaticDossardColor(False); +*** + +***Match_InitMap*** +*** +declare Integer Last_NumberOfFinishers; +declare Text Last_EliminatedPlayersNbRanks; +declare K_MatchState[Integer] MatchState; + +declare Integer Last_CooldownDossardUpdate = -1; +declare Boolean Last_InCooldownDossardUpdate = False; +*** + +***Match_StartMap*** +*** +CarRank::Reset(); +UIModules_ScoresTable::SetCustomPoints([]); + +// Warm up +---Laps_Warmup--- + +StartTime = Now + Race::C_SpawnDuration; +if (S_DisableGiveUp) { + Race::SetRespawnBehaviour(Race::C_RespawnBehaviour_NeverGiveUp); +} else { + Race::SetRespawnBehaviour(Race::C_RespawnBehaviour_Normal); +} + +foreach (Player in Players) { + if (Player.Score != Null && Race::IsReadyToStart(Player)) { + Race::Start(Player, StartTime); + } +} + +CarRank::Update(CarRank::C_SortCriteria_BestRace); +StateMgr::ForcePlayersStates([StateMgr::C_State_Playing]); +*** + +***Match_StartRound*** +*** +Last_EliminatedPlayersNbRanks = S_EliminatedPlayersNbRanks; +Last_NumberOfFinishers = S_NumberOfFinishers; + + +MatchState = ComputeMatchState(); + +// Reset Dossard Color +foreach (Player in Players) { + Player.Dossard_Color = <1., 1., 1.>; +} + +Last_CooldownDossardUpdate = -1; +Last_InCooldownDossardUpdate = False; +*** + +***Match_PlayLoop*** +*** +// Manage race events +declare RacePendingEvents = Race::GetPendingEvents(); +foreach (Event in RacePendingEvents) { + Race::ValidEvent(Event); + + // Waypoint + if (Event.Type == Events::C_Type_Waypoint) { + if (Event.Player != Null) { + declare Integer NBOfCP = Event.Player.RaceWaypointTimes.count; + + if (MatchState.existskey(NBOfCP)) { + MatchState[NBOfCP].NbFinishers += 1; + + // Proceed kick + if (MatchState[NBOfCP].NbFinishers >= MatchState[NBOfCP].NbAliveAfter) { + foreach (Player in Players) { + if (Player.SpawnStatus != CSmPlayer::ESpawnStatus::Spawned) continue; + + if (Player.RaceWaypointTimes.count < NBOfCP) { + EliminatePlayer(Player); + } + } + + foreach (CPNb => State in MatchState) { + if (CPNb > Event.Player.RaceWaypointTimes.count) { + break; + } + } + } + } + + if (Event.IsEndRace) { + Scores::UpdatePlayerPrevRace(Event.Player); + declare BetterRace = Scores::UpdatePlayerBestRaceIfBetter(Event.Player); + declare BetterLap = Scores::UpdatePlayerBestLapIfBetter(Event.Player); + + if (MatchState.existskey(NBOfCP) && MatchState[NBOfCP].NbFinishers >= S_NumberOfFinishers) { + MB_StopMatch(); + } + } + + // Update best race at each checkpoint to sort scores with C_Sort_BestRaceCheckpointsProgress + Scores::UpdatePlayerBestRace(Event.Player); + + CarRank::ThrottleUpdate(CarRank::C_SortCriteria_BestRace); + + if (Last_CooldownDossardUpdate == -1) { + UpdateDossardColors(MatchState); + Last_CooldownDossardUpdate = Now + 1000; + } else { + Last_InCooldownDossardUpdate = True; + } + } + } else if (Event.Type == Events::C_Type_SkipOutro) { + if (Event.Player != Null) { + Race::StopSkipOutro(Event.Player); + } + } +} + +// Manage mode events +foreach (Event in PendingEvents) { + if (Event.HasBeenPassed || Event.HasBeenDiscarded) continue; + Events::Invalid(Event); +} + +if (Last_CooldownDossardUpdate != -1 && Now > Last_CooldownDossardUpdate) { + Last_CooldownDossardUpdate = -1; + if (Last_InCooldownDossardUpdate) { + Last_InCooldownDossardUpdate = False; + UpdateDossardColors(MatchState); + } +} + +if (Last_EliminatedPlayersNbRanks != S_EliminatedPlayersNbRanks || Last_NumberOfFinishers != S_NumberOfFinishers) { + Last_EliminatedPlayersNbRanks = S_EliminatedPlayersNbRanks; + Last_NumberOfFinishers = S_NumberOfFinishers; + + MatchState = ComputeMatchState(); +} + +if (Players.count > 0 && PlayersNbAlive <= 0) { + MB_StopMatch(); +} +*** + +***Match_EndMap*** +*** +// Ensure that we stop the match (after a vote for the next map, ...) +MB_StopMatch(); + +EndTime = -1; +StateMgr::ForcePlayersStates([StateMgr::C_State_Waiting]); + +CarRank::Update(CarRank::C_SortCriteria_BestRace); +Race::SortScores(Race::C_Sort_BestRaceCheckpointsProgress); +Scores::SetPlayerWinner(Scores::GetBestPlayer(Scores::C_Sort_BestRaceCheckpointsProgress)); +Race::StopSkipOutroAll(); +*** + +/* + * + * Functions + * +*/ + +/** Compute Match State based on S_EliminatedPlayersNbRanks and number of Players + * + * @return Integer[Integer] + */ +K_MatchState[Integer] ComputeMatchState() { + declare K_MatchState[Integer] MatchState; + + declare Text[] KORepartition = TL::Split(",", S_EliminatedPlayersNbRanks); + + declare Integer NBOfCP = Map::GetCheckpointsCount() + 1; + declare Integer NbAliveAfter = S_NumberOfFinishers; + + for (I, 0 , NBOfCP) { + declare Index = NBOfCP - I; + if (KORepartition.existskey(I)) { + MatchState[Index] = K_MatchState{ + NbAliveAfter = NbAliveAfter, + NbEliminations = TL::ToInteger(KORepartition[I]) + }; + } else { + MatchState[Index] = K_MatchState{ + NbAliveAfter = NbAliveAfter, + NbEliminations = TL::ToInteger(KORepartition[KORepartition.count - 1]) + }; + } + NbAliveAfter += MatchState[Index].NbEliminations; + } + + return MatchState; +} + +/** Update Dossard Color of Players depending of the CP and the Rank + * + * @return Void + */ +Void UpdateDossardColors(K_MatchState[Integer] _MatchState) { + Log::Log("UpdateDossardColors"); + declare Integer Rank = 1; + + foreach (Score in Scores) { + if (Score == Null) continue; + if (Score.User == Null) continue; + + declare CSmPlayer Player = GetPlayer(Score.User.Login); + if (Player == Null) continue; + if (Player.SpawnStatus == CSmPlayer::ESpawnStatus::NotSpawned) continue; + + declare Integer NbAlive; + + if (_MatchState.existskey(Player.CurrentLapNumber + 1)) { + NbAlive = _MatchState[Player.CurrentLapNumber + 1].NbAliveAfter; + } else { + // get first CP + foreach (Value in _MatchState) { + NbAlive = Value.NbAliveAfter; + break; + } + } + + if (Rank > NbAlive) { + Player.Dossard_Color = <1., 0., 0.>; + } else { + Player.Dossard_Color = <1., 1., 1.>; + } + + Rank += 1; + } +} + +/** Eliminate Player and send a message in a Chat + * + * @return Void + */ +Void EliminatePlayer(CSmPlayer _Player) { + if (_Player == Null) return; + if (_Player.SpawnStatus == CSmPlayer::ESpawnStatus::NotSpawned) return; + log("EliminatePlayer: " ^ _Player.User.Name ^ " (" ^ _Player.User.Login ^ ")"); + Race::StopSkipOutro(_Player); + UIManager.UIAll.SendChat("Player $<$ff6" ^ _Player.User.Name ^ "$> is $<$f00eliminated$>"); + + // waiting GetCustomPoints function in ScoresTable_Server + declare netwrite Text[][Text] Net_TMxSM_ScoresTable_CustomPoints for Teams[0]; + Net_TMxSM_ScoresTable_CustomPoints[_Player.User.WebServicesUserId] = [_("|Status|K.O."), "f00"]; +} \ No newline at end of file