1
0
forked from 0ad/0ad

Make the player state an enum.

So we don't need to hassle with strings all over the place.

Differential revision: D4504
Comments by: @Silier
This was SVN commit r27242.
This commit is contained in:
Freagarach 2022-11-22 07:54:11 +00:00
parent da2d002c78
commit 966727b52e
7 changed files with 70 additions and 35 deletions

View File

@ -120,14 +120,14 @@ Auras.prototype.CalculateAffectedPlayers = function(name)
if (!cmpPlayer)
cmpPlayer = QueryOwnerInterface(this.entity);
if (!cmpPlayer || cmpPlayer.GetState() == "defeated")
if (!cmpPlayer || cmpPlayer.IsDefeated())
return;
let cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
for (let i of cmpPlayerManager.GetAllPlayers())
{
let cmpAffectedPlayer = QueryPlayerIDInterface(i);
if (!cmpAffectedPlayer || cmpAffectedPlayer.GetState() == "defeated")
if (!cmpAffectedPlayer || cmpAffectedPlayer.IsDefeated())
continue;
if (affectedPlayers.some(p => p == "Player" ? cmpPlayer.GetPlayerID() == i : cmpPlayer["Is" + p](i)))

View File

@ -57,17 +57,17 @@ EndGameManager.prototype.SetGameSettings = function(newSettings = {})
*/
EndGameManager.prototype.MarkPlayerAndAlliesAsWon = function(playerID, victoryString, defeatString)
{
let state = QueryPlayerIDInterface(playerID).GetState();
if (state != "active")
const cmpPlayer = QueryPlayerIDInterface(playerID);
if (!cmpPlayer.IsActive())
{
warn("Can't mark player " + playerID + " as won, since the state is " + state);
warn("Can't mark player " + playerID + " as won, since the state is " + cmpPlayer.GetState());
return;
}
let winningPlayers = [playerID];
if (this.alliedVictory)
winningPlayers = QueryPlayerIDInterface(playerID).GetMutualAllies(playerID).filter(
player => QueryPlayerIDInterface(player).GetState() == "active");
winningPlayers = cmpPlayer.GetMutualAllies(playerID).filter(
player => QueryPlayerIDInterface(player).IsActive());
this.MarkPlayersAsWon(winningPlayers, victoryString, defeatString);
};
@ -88,20 +88,19 @@ EndGameManager.prototype.MarkPlayersAsWon = function(winningPlayers, victoryStri
for (let playerID of winningPlayers)
{
let cmpPlayer = QueryPlayerIDInterface(playerID);
let state = cmpPlayer.GetState();
if (state != "active")
if (!cmpPlayer.IsActive())
{
warn("Can't mark player " + playerID + " as won, since the state is " + state);
warn("Can't mark player " + playerID + " as won, since the state is " + cmpPlayer.GetState());
continue;
}
cmpPlayer.SetState("won", undefined);
cmpPlayer.Win(undefined);
}
let defeatedPlayers = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager).GetActivePlayers().filter(
playerID => winningPlayers.indexOf(playerID) == -1);
for (let playerID of defeatedPlayers)
QueryPlayerIDInterface(playerID).SetState("defeated", undefined);
QueryPlayerIDInterface(playerID).Defeat(undefined);
let cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
cmpGUIInterface.PushNotification({
@ -146,7 +145,7 @@ EndGameManager.prototype.AlliedVictoryCheck = function()
for (let playerID = 1; playerID < numPlayers; ++playerID)
{
let cmpPlayer = QueryPlayerIDInterface(playerID);
if (cmpPlayer.GetState() != "active")
if (!cmpPlayer.IsActive())
continue;
if (allies.length && !cmpPlayer.IsMutualAlly(allies[0]))
@ -160,12 +159,8 @@ EndGameManager.prototype.AlliedVictoryCheck = function()
if (this.alliedVictory || allies.length == 1)
{
for (let playerID of allies)
{
let cmpPlayer = QueryPlayerIDInterface(playerID);
if (cmpPlayer)
cmpPlayer.SetState("won", undefined);
}
for (const playerID of allies)
QueryPlayerIDInterface(playerID)?.Win(undefined);
cmpGuiInterface.PushNotification({
"type": "won",

View File

@ -27,6 +27,11 @@ Player.prototype.Schema =
"<ref name='nonNegativeDecimal'/>" +
"</element>";
// The GUI expects these strings.
Player.prototype.STATE_ACTIVE = "active";
Player.prototype.STATE_DEFEATED = "defeated";
Player.prototype.STATE_WON = "won";
/**
* Don't serialize diplomacyColor or displayDiplomacyColor since they're modified by the GUI.
*/
@ -68,7 +73,7 @@ Player.prototype.Init = function()
this.tradingGoods = []; // Goods for next trade-route and its probabilities * 100.
this.team = -1; // Team number of the player, players on the same team will always have ally diplomatic status. Also this is useful for team emblems, scoring, etc.
this.teamsLocked = false;
this.state = "active"; // Game state. One of "active", "defeated", "won".
this.state = this.STATE_ACTIVE;
this.diplomacy = []; // Array of diplomatic stances for this player with respect to other players (including gaia and self).
this.sharedDropsites = false;
this.formations = this.template.Formations._string.split(" ");
@ -427,6 +432,25 @@ Player.prototype.SetTradingGoods = function(tradingGoods)
});
};
/**
* @param {string} message - The message to send in the chat. May be undefined.
*/
Player.prototype.Win = function(message)
{
this.SetState(this.STATE_WON, message);
};
/**
* @param {string} message - The message to send in the chat. May be undefined.
*/
Player.prototype.Defeat = function(message)
{
this.SetState(this.STATE_DEFEATED, message);
};
/**
* @return {string} - The string identified with the current state.
*/
Player.prototype.GetState = function()
{
return this.state;
@ -437,7 +461,23 @@ Player.prototype.GetState = function()
*/
Player.prototype.IsActive = function()
{
return this.state === "active";
return this.state === this.STATE_ACTIVE;
};
/**
* @return {boolean} -
*/
Player.prototype.IsDefeated = function()
{
return this.state === this.STATE_DEFEATED;
};
/**
* @return {boolean} -
*/
Player.prototype.HasWon = function()
{
return this.state === this.STATE_WON;
};
/**
@ -448,12 +488,12 @@ Player.prototype.IsActive = function()
*/
Player.prototype.SetState = function(newState, message)
{
if (this.state != "active")
if (!this.IsActive())
return;
if (newState != "won" && newState != "defeated")
if (newState != this.STATE_WON && newState != this.STATE_DEFEATED)
{
warn("Can't change playerstate to " + this.state);
warn("Can't change playerstate to " + newState);
return;
}
@ -465,7 +505,7 @@ Player.prototype.SetState = function(newState, message)
this.state = newState;
let won = newState == "won";
const won = this.HasWon();
let cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
if (won)
cmpRangeManager.SetLosRevealAll(this.playerID, true);
@ -567,7 +607,7 @@ Player.prototype.SetDiplomacyIndex = function(idx, value)
if (!cmpPlayer)
return;
if (this.state != "active" || cmpPlayer.state != "active")
if (!this.IsActive() || !cmpPlayer.IsActive())
return;
this.diplomacy[idx] = value;
@ -846,7 +886,7 @@ Player.prototype.TributeResource = function(player, amounts)
if (!cmpPlayer)
return;
if (this.state != "active" || cmpPlayer.state != "active")
if (!this.IsActive() || !cmpPlayer.IsActive())
return;
let resTribCodes = Resources.GetTributableCodes();
@ -943,7 +983,7 @@ Player.prototype.OnGlobalPlayerDefeated = function(msg)
if (!cmpSound)
return;
let soundGroup = cmpSound.GetSoundGroup(this.playerID === msg.playerId ? "defeated" : this.IsAlly(msg.playerId) ? "defeated_ally" : this.state === "won" ? "won" : "defeated_enemy");
const soundGroup = cmpSound.GetSoundGroup(this.playerID === msg.playerId ? "defeated" : this.IsAlly(msg.playerId) ? "defeated_ally" : this.HasWon() ? "won" : "defeated_enemy");
if (soundGroup)
Engine.QueryInterface(SYSTEM_ENTITY, IID_SoundManager).PlaySoundGroupForPlayer(soundGroup, this.playerID);
};

View File

@ -9,7 +9,7 @@ Engine.LoadComponentScript("ModifiersManager.js");
var playerID = [0, 1, 2];
var playerEnt = [10, 11, 12];
var playerState = ["active", "active", "active"];
var playerDefeated = [false, false, false];
var sourceEnt = 20;
var targetEnt = 30;
var auraRange = 40;
@ -55,14 +55,14 @@ function testAuras(name, test_function)
"IsAlly": id => id == playerID[1] || id == playerID[2],
"IsEnemy": id => id != playerID[1] || id != playerID[2],
"GetPlayerID": () => playerID[1],
"GetState": () => playerState[1]
"IsDefeated": () => playerDefeated[1]
});
AddMock(playerEnt[2], IID_Player, {
"IsAlly": id => id == playerID[1] || id == playerID[2],
"IsEnemy": id => id != playerID[1] || id != playerID[2],
"GetPlayerID": () => playerID[2],
"GetState": () => playerState[2]
"IsDefeated": () => playerDefeated[2]
});
AddMock(targetEnt, IID_Identity, {
@ -154,7 +154,7 @@ testAuras("global", (name, cmpAuras) => {
TS_ASSERT_EQUALS(ApplyValueModificationsToTemplate("Component/Value", 5, playerID[2], template), 5);
});
playerState[1] = "defeated";
playerDefeated[1] = true;
testAuras("global", (name, cmpAuras) => {
cmpAuras.OnGlobalPlayerDefeated({ "playerId": playerID[1] });
TS_ASSERT_EQUALS(ApplyValueModificationsToTemplate("Component/Value", 5, playerID[2], template), 5);

View File

@ -17,7 +17,7 @@ AddMock(SYSTEM_ENTITY, IID_GuiInterface, {
AddMock(playerEnt1, IID_Player, {
"GetName": () => "Player 1",
"GetState": () => "active",
"IsActive": () => true,
});
TS_ASSERT_EQUALS(cmpEndGameManager.skipAlliedVictoryCheck, true);

View File

@ -53,7 +53,7 @@ function Cheat(input)
case "defeatplayer":
if (isNaN(input.parameter))
return;
QueryPlayerIDInterface(input.parameter)?.SetState("defeated",
QueryPlayerIDInterface(input.parameter)?.Defeat(
markForTranslation("%(player)s has been defeated (cheat).")
);
return;

View File

@ -470,7 +470,7 @@ var g_Commands = {
"resign": function(player, cmd, data)
{
data.cmpPlayer.SetState("defeated", markForTranslation("%(player)s has resigned."));
data.cmpPlayer.Defeat(markForTranslation("%(player)s has resigned."));
},
"occupy-turret": function(player, cmd, data)