mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-07-26 12:31:28 +00:00
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:
@@ -120,14 +120,14 @@ Auras.prototype.CalculateAffectedPlayers = function(name)
|
|||||||
if (!cmpPlayer)
|
if (!cmpPlayer)
|
||||||
cmpPlayer = QueryOwnerInterface(this.entity);
|
cmpPlayer = QueryOwnerInterface(this.entity);
|
||||||
|
|
||||||
if (!cmpPlayer || cmpPlayer.GetState() == "defeated")
|
if (!cmpPlayer || cmpPlayer.IsDefeated())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
let cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
|
let cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
|
||||||
for (let i of cmpPlayerManager.GetAllPlayers())
|
for (let i of cmpPlayerManager.GetAllPlayers())
|
||||||
{
|
{
|
||||||
let cmpAffectedPlayer = QueryPlayerIDInterface(i);
|
let cmpAffectedPlayer = QueryPlayerIDInterface(i);
|
||||||
if (!cmpAffectedPlayer || cmpAffectedPlayer.GetState() == "defeated")
|
if (!cmpAffectedPlayer || cmpAffectedPlayer.IsDefeated())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (affectedPlayers.some(p => p == "Player" ? cmpPlayer.GetPlayerID() == i : cmpPlayer["Is" + p](i)))
|
if (affectedPlayers.some(p => p == "Player" ? cmpPlayer.GetPlayerID() == i : cmpPlayer["Is" + p](i)))
|
||||||
|
|||||||
@@ -57,17 +57,17 @@ EndGameManager.prototype.SetGameSettings = function(newSettings = {})
|
|||||||
*/
|
*/
|
||||||
EndGameManager.prototype.MarkPlayerAndAlliesAsWon = function(playerID, victoryString, defeatString)
|
EndGameManager.prototype.MarkPlayerAndAlliesAsWon = function(playerID, victoryString, defeatString)
|
||||||
{
|
{
|
||||||
let state = QueryPlayerIDInterface(playerID).GetState();
|
const cmpPlayer = QueryPlayerIDInterface(playerID);
|
||||||
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());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let winningPlayers = [playerID];
|
let winningPlayers = [playerID];
|
||||||
if (this.alliedVictory)
|
if (this.alliedVictory)
|
||||||
winningPlayers = QueryPlayerIDInterface(playerID).GetMutualAllies(playerID).filter(
|
winningPlayers = cmpPlayer.GetMutualAllies(playerID).filter(
|
||||||
player => QueryPlayerIDInterface(player).GetState() == "active");
|
player => QueryPlayerIDInterface(player).IsActive());
|
||||||
|
|
||||||
this.MarkPlayersAsWon(winningPlayers, victoryString, defeatString);
|
this.MarkPlayersAsWon(winningPlayers, victoryString, defeatString);
|
||||||
};
|
};
|
||||||
@@ -88,20 +88,19 @@ EndGameManager.prototype.MarkPlayersAsWon = function(winningPlayers, victoryStri
|
|||||||
for (let playerID of winningPlayers)
|
for (let playerID of winningPlayers)
|
||||||
{
|
{
|
||||||
let cmpPlayer = QueryPlayerIDInterface(playerID);
|
let cmpPlayer = QueryPlayerIDInterface(playerID);
|
||||||
let state = cmpPlayer.GetState();
|
if (!cmpPlayer.IsActive())
|
||||||
if (state != "active")
|
|
||||||
{
|
{
|
||||||
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;
|
continue;
|
||||||
}
|
}
|
||||||
cmpPlayer.SetState("won", undefined);
|
cmpPlayer.Win(undefined);
|
||||||
}
|
}
|
||||||
|
|
||||||
let defeatedPlayers = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager).GetActivePlayers().filter(
|
let defeatedPlayers = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager).GetActivePlayers().filter(
|
||||||
playerID => winningPlayers.indexOf(playerID) == -1);
|
playerID => winningPlayers.indexOf(playerID) == -1);
|
||||||
|
|
||||||
for (let playerID of defeatedPlayers)
|
for (let playerID of defeatedPlayers)
|
||||||
QueryPlayerIDInterface(playerID).SetState("defeated", undefined);
|
QueryPlayerIDInterface(playerID).Defeat(undefined);
|
||||||
|
|
||||||
let cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
|
let cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
|
||||||
cmpGUIInterface.PushNotification({
|
cmpGUIInterface.PushNotification({
|
||||||
@@ -146,7 +145,7 @@ EndGameManager.prototype.AlliedVictoryCheck = function()
|
|||||||
for (let playerID = 1; playerID < numPlayers; ++playerID)
|
for (let playerID = 1; playerID < numPlayers; ++playerID)
|
||||||
{
|
{
|
||||||
let cmpPlayer = QueryPlayerIDInterface(playerID);
|
let cmpPlayer = QueryPlayerIDInterface(playerID);
|
||||||
if (cmpPlayer.GetState() != "active")
|
if (!cmpPlayer.IsActive())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (allies.length && !cmpPlayer.IsMutualAlly(allies[0]))
|
if (allies.length && !cmpPlayer.IsMutualAlly(allies[0]))
|
||||||
@@ -160,12 +159,8 @@ EndGameManager.prototype.AlliedVictoryCheck = function()
|
|||||||
|
|
||||||
if (this.alliedVictory || allies.length == 1)
|
if (this.alliedVictory || allies.length == 1)
|
||||||
{
|
{
|
||||||
for (let playerID of allies)
|
for (const playerID of allies)
|
||||||
{
|
QueryPlayerIDInterface(playerID)?.Win(undefined);
|
||||||
let cmpPlayer = QueryPlayerIDInterface(playerID);
|
|
||||||
if (cmpPlayer)
|
|
||||||
cmpPlayer.SetState("won", undefined);
|
|
||||||
}
|
|
||||||
|
|
||||||
cmpGuiInterface.PushNotification({
|
cmpGuiInterface.PushNotification({
|
||||||
"type": "won",
|
"type": "won",
|
||||||
|
|||||||
@@ -27,6 +27,11 @@ Player.prototype.Schema =
|
|||||||
"<ref name='nonNegativeDecimal'/>" +
|
"<ref name='nonNegativeDecimal'/>" +
|
||||||
"</element>";
|
"</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.
|
* 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.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.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.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.diplomacy = []; // Array of diplomatic stances for this player with respect to other players (including gaia and self).
|
||||||
this.sharedDropsites = false;
|
this.sharedDropsites = false;
|
||||||
this.formations = this.template.Formations._string.split(" ");
|
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()
|
Player.prototype.GetState = function()
|
||||||
{
|
{
|
||||||
return this.state;
|
return this.state;
|
||||||
@@ -437,7 +461,23 @@ Player.prototype.GetState = function()
|
|||||||
*/
|
*/
|
||||||
Player.prototype.IsActive = 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)
|
Player.prototype.SetState = function(newState, message)
|
||||||
{
|
{
|
||||||
if (this.state != "active")
|
if (!this.IsActive())
|
||||||
return;
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -465,7 +505,7 @@ Player.prototype.SetState = function(newState, message)
|
|||||||
|
|
||||||
this.state = newState;
|
this.state = newState;
|
||||||
|
|
||||||
let won = newState == "won";
|
const won = this.HasWon();
|
||||||
let cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
|
let cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
|
||||||
if (won)
|
if (won)
|
||||||
cmpRangeManager.SetLosRevealAll(this.playerID, true);
|
cmpRangeManager.SetLosRevealAll(this.playerID, true);
|
||||||
@@ -567,7 +607,7 @@ Player.prototype.SetDiplomacyIndex = function(idx, value)
|
|||||||
if (!cmpPlayer)
|
if (!cmpPlayer)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (this.state != "active" || cmpPlayer.state != "active")
|
if (!this.IsActive() || !cmpPlayer.IsActive())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
this.diplomacy[idx] = value;
|
this.diplomacy[idx] = value;
|
||||||
@@ -846,7 +886,7 @@ Player.prototype.TributeResource = function(player, amounts)
|
|||||||
if (!cmpPlayer)
|
if (!cmpPlayer)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (this.state != "active" || cmpPlayer.state != "active")
|
if (!this.IsActive() || !cmpPlayer.IsActive())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
let resTribCodes = Resources.GetTributableCodes();
|
let resTribCodes = Resources.GetTributableCodes();
|
||||||
@@ -943,7 +983,7 @@ Player.prototype.OnGlobalPlayerDefeated = function(msg)
|
|||||||
if (!cmpSound)
|
if (!cmpSound)
|
||||||
return;
|
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)
|
if (soundGroup)
|
||||||
Engine.QueryInterface(SYSTEM_ENTITY, IID_SoundManager).PlaySoundGroupForPlayer(soundGroup, this.playerID);
|
Engine.QueryInterface(SYSTEM_ENTITY, IID_SoundManager).PlaySoundGroupForPlayer(soundGroup, this.playerID);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ Engine.LoadComponentScript("ModifiersManager.js");
|
|||||||
|
|
||||||
var playerID = [0, 1, 2];
|
var playerID = [0, 1, 2];
|
||||||
var playerEnt = [10, 11, 12];
|
var playerEnt = [10, 11, 12];
|
||||||
var playerState = ["active", "active", "active"];
|
var playerDefeated = [false, false, false];
|
||||||
var sourceEnt = 20;
|
var sourceEnt = 20;
|
||||||
var targetEnt = 30;
|
var targetEnt = 30;
|
||||||
var auraRange = 40;
|
var auraRange = 40;
|
||||||
@@ -55,14 +55,14 @@ function testAuras(name, test_function)
|
|||||||
"IsAlly": id => id == playerID[1] || id == playerID[2],
|
"IsAlly": id => id == playerID[1] || id == playerID[2],
|
||||||
"IsEnemy": id => id != playerID[1] || id != playerID[2],
|
"IsEnemy": id => id != playerID[1] || id != playerID[2],
|
||||||
"GetPlayerID": () => playerID[1],
|
"GetPlayerID": () => playerID[1],
|
||||||
"GetState": () => playerState[1]
|
"IsDefeated": () => playerDefeated[1]
|
||||||
});
|
});
|
||||||
|
|
||||||
AddMock(playerEnt[2], IID_Player, {
|
AddMock(playerEnt[2], IID_Player, {
|
||||||
"IsAlly": id => id == playerID[1] || id == playerID[2],
|
"IsAlly": id => id == playerID[1] || id == playerID[2],
|
||||||
"IsEnemy": id => id != playerID[1] || id != playerID[2],
|
"IsEnemy": id => id != playerID[1] || id != playerID[2],
|
||||||
"GetPlayerID": () => playerID[2],
|
"GetPlayerID": () => playerID[2],
|
||||||
"GetState": () => playerState[2]
|
"IsDefeated": () => playerDefeated[2]
|
||||||
});
|
});
|
||||||
|
|
||||||
AddMock(targetEnt, IID_Identity, {
|
AddMock(targetEnt, IID_Identity, {
|
||||||
@@ -154,7 +154,7 @@ testAuras("global", (name, cmpAuras) => {
|
|||||||
TS_ASSERT_EQUALS(ApplyValueModificationsToTemplate("Component/Value", 5, playerID[2], template), 5);
|
TS_ASSERT_EQUALS(ApplyValueModificationsToTemplate("Component/Value", 5, playerID[2], template), 5);
|
||||||
});
|
});
|
||||||
|
|
||||||
playerState[1] = "defeated";
|
playerDefeated[1] = true;
|
||||||
testAuras("global", (name, cmpAuras) => {
|
testAuras("global", (name, cmpAuras) => {
|
||||||
cmpAuras.OnGlobalPlayerDefeated({ "playerId": playerID[1] });
|
cmpAuras.OnGlobalPlayerDefeated({ "playerId": playerID[1] });
|
||||||
TS_ASSERT_EQUALS(ApplyValueModificationsToTemplate("Component/Value", 5, playerID[2], template), 5);
|
TS_ASSERT_EQUALS(ApplyValueModificationsToTemplate("Component/Value", 5, playerID[2], template), 5);
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ AddMock(SYSTEM_ENTITY, IID_GuiInterface, {
|
|||||||
|
|
||||||
AddMock(playerEnt1, IID_Player, {
|
AddMock(playerEnt1, IID_Player, {
|
||||||
"GetName": () => "Player 1",
|
"GetName": () => "Player 1",
|
||||||
"GetState": () => "active",
|
"IsActive": () => true,
|
||||||
});
|
});
|
||||||
|
|
||||||
TS_ASSERT_EQUALS(cmpEndGameManager.skipAlliedVictoryCheck, true);
|
TS_ASSERT_EQUALS(cmpEndGameManager.skipAlliedVictoryCheck, true);
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ function Cheat(input)
|
|||||||
case "defeatplayer":
|
case "defeatplayer":
|
||||||
if (isNaN(input.parameter))
|
if (isNaN(input.parameter))
|
||||||
return;
|
return;
|
||||||
QueryPlayerIDInterface(input.parameter)?.SetState("defeated",
|
QueryPlayerIDInterface(input.parameter)?.Defeat(
|
||||||
markForTranslation("%(player)s has been defeated (cheat).")
|
markForTranslation("%(player)s has been defeated (cheat).")
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -470,7 +470,7 @@ var g_Commands = {
|
|||||||
|
|
||||||
"resign": function(player, cmd, data)
|
"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)
|
"occupy-turret": function(player, cmd, data)
|
||||||
|
|||||||
Reference in New Issue
Block a user