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