Add dynamic map refresh

This commit is contained in:
Gustas
2022-02-10 14:57:43 +02:00
committed by Matthias Mailänder
parent 61df7974b0
commit b254eb0f3d
6 changed files with 263 additions and 46 deletions

View File

@@ -1,6 +1,6 @@
#region Copyright & License Information
/*
* Copyright 2007-2021 The OpenRA Developers (see AUTHORS)
* Copyright 2007-2022 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
@@ -59,6 +59,9 @@ namespace OpenRA.Mods.Common.Widgets.Logic
MapPreview map;
Session.MapStatus mapStatus;
string oldMapUid;
string newMapUid;
string lastUpdatedUid;
bool chatEnabled;
bool addBotOnMapLoad;
@@ -129,6 +132,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
Game.LobbyInfoChanged += UpdateSpawnOccupants;
Game.BeforeGameStart += OnGameStart;
Game.ConnectionStateChanged += ConnectionStateChanged;
modData.MapCache.MapUpdated += TrackRelevantMapUpdates;
var name = lobby.GetOrNull<LabelWidget>("SERVER_NAME");
if (name != null)
@@ -182,20 +186,26 @@ namespace OpenRA.Mods.Common.Widgets.Logic
{
var onSelect = new Action<string>(uid =>
{
// Don't select the same map again
if (uid == map.Uid)
// Don't select the same map again, and handle map becoming unavailable
if (uid == map.Uid && modData.MapCache[uid].Status != MapStatus.Available)
return;
orderManager.IssueOrder(Order.Command("map " + uid));
Game.Settings.Server.Map = uid;
Game.Settings.Save();
newMapUid = null;
oldMapUid = null;
lastUpdatedUid = null;
});
// Check for updated maps, if the user has edited a map we'll preselect it for them
modData.MapCache.UpdateMaps();
Ui.OpenWindow("MAPCHOOSER_PANEL", new WidgetArgs()
{
{ "initialMap", map.Uid },
{ "initialMap", lastUpdatedUid ?? map.Uid },
{ "initialTab", MapClassification.System },
{ "onExit", DoNothing },
{ "onExit", Game.IsHost ? new Action(() => UpdateSelectedMap()) : null },
{ "onSelect", Game.IsHost ? onSelect : null },
{ "filter", MapVisibility.Lobby },
});
@@ -364,8 +374,14 @@ namespace OpenRA.Mods.Common.Widgets.Logic
// Force start panel
Action startGame = () =>
{
gameStarting = true;
orderManager.IssueOrder(Order.Command("startgame"));
// Refresh MapCache and check if the selected map is available before attempting to start the game
if (modData.MapCache[map.Uid].Status == MapStatus.Available)
{
gameStarting = true;
orderManager.IssueOrder(Order.Command("startgame"));
}
else
UpdateSelectedMap();
};
var startGameButton = lobby.GetOrNull<ButtonWidget>("START_GAME_BUTTON");
@@ -484,6 +500,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
Game.LobbyInfoChanged -= UpdateSpawnOccupants;
Game.BeforeGameStart -= OnGameStart;
Game.ConnectionStateChanged -= ConnectionStateChanged;
modData.MapCache.MapUpdated -= TrackRelevantMapUpdates;
}
base.Dispose(disposing);
@@ -822,6 +839,36 @@ namespace OpenRA.Mods.Common.Widgets.Logic
onStart();
}
void TrackRelevantMapUpdates(string oldUid, string newUid)
{
// We need to handle map being updated multiple times without a refresh
if (map.Uid == oldUid || oldUid == newMapUid)
{
if (oldMapUid == null)
oldMapUid = oldUid;
newMapUid = newUid;
}
if (newUid != null)
lastUpdatedUid = newUid;
}
void UpdateSelectedMap()
{
if (modData.MapCache[map.Uid].Status == MapStatus.Available)
return;
if (oldMapUid == map.Uid && modData.MapCache[newMapUid].Status == MapStatus.Available)
{
orderManager.IssueOrder(Order.Command("map " + newMapUid));
Game.Settings.Server.Map = newMapUid;
Game.Settings.Save();
newMapUid = null;
oldMapUid = null;
lastUpdatedUid = null;
}
}
}
public class LobbyFaction