2010-10-01 22:51:21 +02:00
|
|
|
// Repetition interval (msecs) for checking end game conditions
|
|
|
|
var g_ProgressInterval = 1000;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* System component which regularly checks victory/defeat conditions
|
|
|
|
* and if they are satisfied then it marks the player as victorious/defeated.
|
|
|
|
*/
|
|
|
|
function EndGameManager() {}
|
|
|
|
|
|
|
|
EndGameManager.prototype.Schema =
|
|
|
|
"<a:component type='system'/><empty/>";
|
|
|
|
|
|
|
|
EndGameManager.prototype.Init = function()
|
|
|
|
{
|
|
|
|
// Game type, initialised from the map settings.
|
|
|
|
// One of: "conquest" (default) and "endless"
|
|
|
|
this.gameType = "conquest";
|
2010-11-21 01:35:08 +01:00
|
|
|
|
|
|
|
// Allied victory means allied players can win if victory conditions are met for each of them
|
|
|
|
// Would be false for a "last man standing" game (when diplomacy is fully implemented)
|
|
|
|
this.alliedVictory = true;
|
2010-10-01 22:51:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
EndGameManager.prototype.SetGameType = function(newGameType)
|
|
|
|
{
|
|
|
|
this.gameType = newGameType;
|
|
|
|
};
|
|
|
|
|
2010-11-21 01:35:08 +01:00
|
|
|
EndGameManager.prototype.SetAlliedVictory = function(flag)
|
|
|
|
{
|
|
|
|
this.alliedVictory = flag;
|
|
|
|
};
|
|
|
|
|
2010-10-01 22:51:21 +02:00
|
|
|
/**
|
|
|
|
* Begin checking the end-game conditions.
|
|
|
|
* Must be called once, after calling SetGameType.
|
|
|
|
*/
|
|
|
|
EndGameManager.prototype.Start = function()
|
|
|
|
{
|
|
|
|
if (this.gameType != "endless")
|
|
|
|
{
|
|
|
|
var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
|
|
|
|
this.timer = cmpTimer.SetTimeout(this.entity, IID_EndGameManager, "ProgressTimeout", g_ProgressInterval, {});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
EndGameManager.prototype.OnDestroy = function()
|
|
|
|
{
|
|
|
|
if (this.timer)
|
|
|
|
{
|
|
|
|
var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
|
|
|
|
cmpTimer.CancelTimer(this.timer);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
EndGameManager.prototype.ProgressTimeout = function(data)
|
|
|
|
{
|
|
|
|
this.UpdatePlayerStates();
|
|
|
|
|
|
|
|
// Repeat the timer
|
|
|
|
var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
|
|
|
|
this.timer = cmpTimer.SetTimeout(this.entity, IID_EndGameManager, "ProgressTimeout", g_ProgressInterval, data);
|
|
|
|
};
|
|
|
|
|
|
|
|
EndGameManager.prototype.UpdatePlayerStates = function()
|
|
|
|
{
|
|
|
|
var cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
|
|
|
|
switch (this.gameType)
|
|
|
|
{
|
|
|
|
case "conquest":
|
|
|
|
|
2010-11-21 01:35:08 +01:00
|
|
|
// Ignore gaia
|
|
|
|
var numPlayers = cmpPlayerManager.GetNumPlayers() - 1;
|
2011-07-02 02:06:39 +02:00
|
|
|
var cmpPlayers = new Array(numPlayers);
|
2010-11-21 01:35:08 +01:00
|
|
|
|
2010-10-01 22:51:21 +02:00
|
|
|
// If a player is currently active but has no suitable units left,
|
2011-07-02 02:06:39 +02:00
|
|
|
// mark that player as defeated
|
2010-11-21 01:35:08 +01:00
|
|
|
for (var i = 0; i < numPlayers; i++)
|
2010-10-01 22:51:21 +02:00
|
|
|
{
|
2010-11-21 01:35:08 +01:00
|
|
|
var playerEntityId = cmpPlayerManager.GetPlayerByID(i+1);
|
2011-07-02 02:06:39 +02:00
|
|
|
cmpPlayers[i] = Engine.QueryInterface(playerEntityId, IID_Player);
|
|
|
|
if (cmpPlayers[i].GetState() == "active")
|
2010-10-01 22:51:21 +02:00
|
|
|
{
|
2011-07-02 02:06:39 +02:00
|
|
|
if (cmpPlayers[i].GetConquestCriticalEntitiesCount() == 0)
|
|
|
|
{ // Defeated
|
2010-10-01 22:51:21 +02:00
|
|
|
Engine.PostMessage(playerEntityId, MT_PlayerDefeated, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-21 01:35:08 +01:00
|
|
|
var onlyAlliesLeft = true;
|
2011-07-02 02:06:39 +02:00
|
|
|
var allies = [];
|
2010-11-21 01:35:08 +01:00
|
|
|
for (var i = 0; i < numPlayers && onlyAlliesLeft; i++)
|
2010-10-01 22:51:21 +02:00
|
|
|
{
|
2011-07-02 02:06:39 +02:00
|
|
|
if (cmpPlayers[i].GetState() == "active")
|
2010-11-21 01:35:08 +01:00
|
|
|
{ //Active player
|
2011-07-02 02:06:39 +02:00
|
|
|
for (var j = 0; j < numPlayers && onlyAlliesLeft; j++)
|
2010-11-21 01:35:08 +01:00
|
|
|
{
|
2011-07-02 02:06:39 +02:00
|
|
|
if (cmpPlayers[j].GetState() == "active" && (cmpPlayers[i].IsEnemy(j+1) || cmpPlayers[j].IsEnemy(i+1)))
|
2010-11-21 01:35:08 +01:00
|
|
|
{ // Only need to find an active non-allied player
|
|
|
|
onlyAlliesLeft = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (onlyAlliesLeft)
|
2011-07-02 02:06:39 +02:00
|
|
|
allies.push(i);
|
2010-10-01 22:51:21 +02:00
|
|
|
}
|
|
|
|
}
|
2010-11-21 01:35:08 +01:00
|
|
|
|
|
|
|
// If only allies left and allied victory set (or only one player left)
|
2011-07-02 02:06:39 +02:00
|
|
|
if (onlyAlliesLeft && (this.alliedVictory || allies.length == 1))
|
2010-10-01 22:51:21 +02:00
|
|
|
{
|
2011-07-02 02:06:39 +02:00
|
|
|
for each (var p in allies)
|
2010-11-21 01:35:08 +01:00
|
|
|
{
|
2011-07-02 02:06:39 +02:00
|
|
|
cmpPlayers[p].SetState("won");
|
2010-11-21 01:35:08 +01:00
|
|
|
}
|
2011-06-29 01:24:42 +02:00
|
|
|
|
|
|
|
// Reveal the map to all players
|
2011-03-06 03:29:07 +01:00
|
|
|
var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
|
2011-06-29 01:24:42 +02:00
|
|
|
cmpRangeManager.SetLosRevealAll(-1, true);
|
2010-10-01 22:51:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error("Invalid game type "+this.gameType);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Engine.RegisterComponentType(IID_EndGameManager, "EndGameManager", EndGameManager);
|