1 function CeasefireManager() {}
3 CeasefireManager.prototype.Schema = "<a:component type='system'/><empty/>";
5 CeasefireManager.prototype.Init = function()
7 // Weather or not ceasefire is active currently.
8 this.ceasefireIsActive = false;
10 // Ceasefire timeout in milliseconds
11 this.ceasefireTime = 0;
13 // Time elapsed when the ceasefire was started
14 this.ceasefireStartedTime = 0;
16 // diplomacy states before the ceasefire started
17 this.diplomacyBeforeCeasefire = [];
19 // Message duration for the countdown in milliseconds
20 this.countdownMessageDuration = 10000;
22 // Duration for the post ceasefire message in milliseconds
23 this.postCountdownMessageDuration = 5000;
26 CeasefireManager.prototype.IsCeasefireActive = function()
28 return this.ceasefireIsActive;
31 CeasefireManager.prototype.GetCeasefireStartedTime = function()
33 return this.ceasefireStartedTime;
36 CeasefireManager.prototype.GetCeasefireTime = function()
38 return this.ceasefireTime;
41 CeasefireManager.prototype.GetDiplomacyBeforeCeasefire = function()
43 return this.diplomacyBeforeCeasefire;
46 CeasefireManager.prototype.StartCeasefire = function(ceasefireTime)
48 // If invalid timeout given, return
49 if (ceasefireTime <= 0)
52 // Remove existing timers
53 let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
54 if (this.ceasefireCountdownMessageTimer)
55 cmpTimer.CancelTimer(this.ceasefireCountdownMessageTimer);
57 if (this.stopCeasefireTimer)
58 cmpTimer.CancelTimer(this.stopCeasefireTimer);
60 // Remove existing messages
61 let cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
62 if (this.ceasefireCountdownMessage)
63 cmpGuiInterface.DeleteTimeNotification(this.ceasefireCountdownMessage);
65 if (this.ceasefireEndedMessage)
66 cmpGuiInterface.DeleteTimeNotification(this.ceasefireEndedMessage);
68 // Save diplomacy and set everyone neutral
69 if (!this.ceasefireIsActive)
72 let numPlayers = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager).GetNumPlayers();
73 for (let i = 1; i < numPlayers; ++i)
74 this.diplomacyBeforeCeasefire.push(QueryPlayerIDInterface(i).GetDiplomacy());
76 // Set every enemy (except gaia) to neutral
77 for (let i = 1; i < numPlayers; ++i)
78 for (let j = 1; j < numPlayers; ++j)
79 if (this.diplomacyBeforeCeasefire[i-1][j] < 0)
80 QueryPlayerIDInterface(i).SetNeutral(j);
83 this.ceasefireIsActive = true;
84 this.ceasefireTime = ceasefireTime;
85 this.ceasefireStartedTime = cmpTimer.GetTime();
87 Engine.PostMessage(SYSTEM_ENTITY, MT_CeasefireStarted);
89 // Add timers for countdown message and resetting diplomacy
90 this.stopCeasefireTimer = cmpTimer.SetTimeout(SYSTEM_ENTITY, IID_CeasefireManager, "StopCeasefire", this.ceasefireTime);
91 this.ceasefireCountdownMessageTimer = cmpTimer.SetTimeout(SYSTEM_ENTITY, IID_CeasefireManager, "ShowCeasefireCountdownMessage",
92 this.ceasefireTime - this.countdownMessageDuration);
95 CeasefireManager.prototype.ShowCeasefireCountdownMessage = function()
97 let cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
98 this.ceasefireCountdownMessage = cmpGuiInterface.AddTimeNotification({
99 "message": markForTranslation("You can attack in %(time)s"),
100 "translateMessage": true
101 }, this.countdownMessageDuration);
104 CeasefireManager.prototype.StopCeasefire = function()
106 let cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
108 if (this.ceasefireCountdownMessage)
109 cmpGuiInterface.DeleteTimeNotification(this.ceasefireCountdownMessage);
111 this.ceasefireEndedMessage = cmpGuiInterface.AddTimeNotification({
112 "message": markForTranslation("You can attack now!"),
113 "translateMessage": true
114 }, this.postCountdownMessageDuration);
116 // Reset diplomacies to original settings
117 let numPlayers = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager).GetNumPlayers();
118 for (let i = 1; i < numPlayers; ++i)
119 QueryPlayerIDInterface(i).SetDiplomacy(this.diplomacyBeforeCeasefire[i-1]);
121 this.ceasefireIsActive = false;
122 this.ceasefireTime = 0;
123 this.ceasefireStartedTime = 0;
124 this.diplomacyBeforeCeasefire = [];
126 Engine.PostMessage(SYSTEM_ENTITY, MT_CeasefireEnded);
128 cmpGuiInterface.PushNotification({
129 "type": "ceasefire-ended",
130 "players": [-1] // processed globally
134 Engine.RegisterSystemComponentType(IID_CeasefireManager, "CeasefireManager", CeasefireManager);