Ticket #4143: petra_lastmanstanding_v1.1.patch
File petra_lastmanstanding_v1.1.patch, 13.1 KB (added by , 7 years ago) |
---|
-
binaries/data/mods/public/simulation/ai/common-api/gamestate.js
21 21 this.playerData = SharedScript.playersData[this.player]; 22 22 this.barterPrices = SharedScript.barterPrices; 23 23 this.gameType = SharedScript.gameType; 24 this.alliedVictory = SharedScript.alliedVictory; 25 this.ceasefireActive = SharedScript.ceasefireActive; 24 26 25 27 // get the list of possible phases for this civ: 26 28 // we assume all of them are researchable from the civil centre … … 55 57 this.timeElapsed = SharedScript.timeElapsed; 56 58 this.playerData = SharedScript.playersData[this.player]; 57 59 this.barterPrices = SharedScript.barterPrices; 60 this.ceasefireActive = SharedScript.ceasefireActive; 58 61 }; 59 62 60 63 m.GameState.prototype.updatingCollection = function(id, filter, collection) … … 117 120 return this.gameType; 118 121 }; 119 122 123 m.GameState.prototype.getAlliedVictory = function() 124 { 125 return this.alliedVictory; 126 }; 127 128 m.GameState.prototype.isCeasefireActive = function() 129 { 130 return this.ceasefireActive; 131 }; 132 120 133 m.GameState.prototype.getTemplate = function(type) 121 134 { 122 135 if (this.techTemplates[type] !== undefined) … … 291 304 m.GameState.prototype.hasAllies = function() 292 305 { 293 306 for (let i in this.playerData.isAlly) 294 if (this.playerData.isAlly[i] && +i !== this.player) 307 if (this.playerData.isAlly[i] && +i !== this.player && 308 this.sharedScript.playersData[i].state !== "defeated") 295 309 return true; 296 310 return false; 297 311 }; 298 312 313 m.GameState.prototype.hasEnemies = function() 314 { 315 for (let i in this.playerData.isEnemy) 316 if (this.playerData.isEnemy[i] && +i !== 0 && 317 this.sharedScript.playersData[i].state !== "defeated") 318 return true; 319 return false; 320 }; 321 299 322 m.GameState.prototype.isPlayerAlly = function(id) 300 323 { 301 324 return this.playerData.isAlly[id]; … … 408 431 return this.updatingGlobalCollection("" + this.player + "-units", m.Filters.byClass("Unit"), this.getOwnEntities()); 409 432 }; 410 433 411 m.GameState.prototype.getAllyEntities = function( )434 m.GameState.prototype.getAllyEntities = function(allyID) 412 435 { 413 return this.entities.filter(m.Filters.byOwners(this.getAllies())); 436 if (allyID === undefined) 437 return this.entities.filter(m.Filters.byOwners(this.getAllies())); 438 439 return this.updatingGlobalCollection("" + allyID + "-entities", m.Filters.byOwner(allyID)); 414 440 }; 415 441 416 442 m.GameState.prototype.getExclusiveAllyEntities = function() -
binaries/data/mods/public/simulation/ai/common-api/shared.js
142 142 this.mapSize = state.mapSize; 143 143 this.gameType = state.gameType; 144 144 this.barterPrices = state.barterPrices; 145 this.alliedVictory = state.alliedVictory; 146 this.ceasefireActive = state.ceasefireActive; 145 147 146 148 this.passabilityMap = state.passabilityMap; 147 149 if (this.mapSize % this.passabilityMap.width !== 0) … … 241 243 this.playersData = state.players; 242 244 this.timeElapsed = state.timeElapsed; 243 245 this.barterPrices = state.barterPrices; 246 this.ceasefireActive = state.ceasefireActive; 244 247 245 248 this.passabilityMap = state.passabilityMap; 246 249 this.passabilityMap.cellSize = this.mapSize / this.passabilityMap.width; -
binaries/data/mods/public/simulation/ai/petra/chatHelper.js
130 130 }); 131 131 }; 132 132 133 m.chatNewDiplomacy = function(gameState, player, enemy) 134 { 135 let message; 136 if (enemy) 137 message = markForTranslation("%(_player_)s and I are now enemies."); 138 else 139 message = markForTranslation("%(_player_)s and I are now allies."); 140 141 Engine.PostCommand(PlayerID, { 142 "type": "aichat", 143 "message": message, 144 "translateMessage": true, 145 "translateParameters": ["_player_"], 146 "parameters": {"_player_": player} 147 }); 148 }; 149 133 150 return m; 134 151 }(PETRA); -
binaries/data/mods/public/simulation/ai/petra/diplomacyManager.js
13 13 this.nextTributeUpdate = -1; 14 14 this.nextTributeRequest = new Map(); 15 15 this.nextTributeRequest.set("all", 240); 16 this.allyBetrayLapseTime = -1; 17 this.waitingToBetray = false; 16 18 }; 17 19 18 20 /** … … 108 110 continue; 109 111 this.Config.personality.cooperative = Math.min(1, this.Config.personality.cooperative + 0.003); 110 112 } 113 114 if (events.DiplomacyChanged.length || events.PlayerDefeated.length || events.CeasefireEnded.length) 115 this.lastManStandingCheck(gameState); 111 116 }; 112 117 118 /** 119 * Check if the only remaining players are allies. If so, turn against the strongest first 120 * if the "Last Man Standing" option is enabled 121 */ 122 m.DiplomacyManager.prototype.lastManStandingCheck = function(gameState) 123 { 124 if (gameState.getAlliedVictory() || gameState.isCeasefireActive()) 125 return; 113 126 127 if (gameState.hasEnemies()) 128 { 129 this.waitingToBetray = false; 130 return; 131 } 132 133 if (!gameState.hasAllies()) 134 return; 135 136 // wait a bit before turning 137 if (!this.waitingToBetray) 138 { 139 this.allyBetrayLapseTime = gameState.ai.elapsedTime + Math.random() * 100 + 10; 140 this.waitingToBetray = true; 141 return; 142 } 143 144 // do not turn against an ally yet if we are not strong enough 145 if (gameState.getOwnUnits().length < 50) 146 { 147 this.allyBetrayLapseTime += 60; 148 return; 149 } 150 151 let allyToTurnAgainst; 152 let allyEntities = 0; 153 let max = 0; 154 155 // count the amount of entities our allies have 156 for (let i = 1; i < gameState.sharedScript.playersData.length; ++i) 157 { 158 if (i === PlayerID || gameState.ai.HQ.attackManager.defeated[i]) 159 continue; 160 161 allyEntities = gameState.getAllyEntities(i).length; 162 163 if (allyEntities < max) 164 continue; 165 166 max = allyEntities; 167 allyToTurnAgainst = i; 168 } 169 170 if (allyToTurnAgainst) 171 { 172 Engine.PostCommand(PlayerID, { "type": "diplomacy", "player": allyToTurnAgainst, "to": "enemy" }); 173 if (this.Config.debug > 1) 174 API3.warn("player " + allyToTurnAgainst + " is now an enemy"); 175 if (this.Config.chat) 176 m.chatNewDiplomacy(gameState, allyToTurnAgainst, true); 177 } 178 this.allyBetrayLapseTime = -1; 179 this.waitingToBetray = false; 180 }; 181 114 182 m.DiplomacyManager.prototype.update = function(gameState, events) 115 183 { 116 184 this.checkEvents(gameState, events); … … 117 185 118 186 if (!gameState.ai.HQ.saveResources && gameState.ai.elapsedTime > this.nextTributeUpdate) 119 187 this.tributes(gameState); 188 189 if (this.waitingToBetray && gameState.ai.elapsedTime > this.allyBetrayLapseTime) 190 this.lastManStandingCheck(gameState); 120 191 }; 121 192 122 193 m.DiplomacyManager.prototype.Serialize = function() 123 194 { 124 return { "nextTributeUpdate": this.nextTributeUpdate, "nextTributeRequest": this.nextTributeRequest }; 195 return { 196 "nextTributeUpdate": this.nextTributeUpdate, 197 "nextTributeRequest": this.nextTributeRequest, 198 "allyBetrayLapseTime": this.allyBetrayLapseTime, 199 "waitingToBetray": this.waitingToBetray 200 }; 125 201 }; 126 202 127 203 m.DiplomacyManager.prototype.Deserialize = function(data) 128 204 { 129 this.nextTributeUpdate = data.nextTributeUpdate;130 this.nextTributeRequest = data.nextTributeRequest;205 for (let key in data) 206 this[key] = data[key]; 131 207 }; 132 208 133 209 return m; -
binaries/data/mods/public/simulation/ai/petra/headquarters.js
82 82 this.treasures.registerUpdates(); 83 83 this.currentPhase = gameState.currentPhase(); 84 84 this.decayingStructures = new Set(); 85 this.diplomacyManager.lastManStandingCheck(gameState); 85 86 }; 86 87 87 88 /** -
binaries/data/mods/public/simulation/components/AIInterface.js
20 20 "TerritoriesChanged", 21 21 "TerritoryDecayChanged", 22 22 "TributeExchanged", 23 "AttackRequest" 23 "AttackRequest", 24 "CeasefireEnded" 24 25 ]; 25 26 26 27 AIInterface.prototype.Init = function() … … 197 198 this.events.TerritoriesChanged.push(msg); 198 199 }; 199 200 201 AIInterface.prototype.OnCeasefireEnded = function(msg) 202 { 203 this.events.CeasefireEnded.push(msg); 204 }; 205 200 206 /** 201 207 * When a new technology is researched, check which templates it affects, 202 208 * and send the updated values to the AI. -
binaries/data/mods/public/simulation/components/EndGameManager.js
76 76 this.alliedVictory = flag; 77 77 }; 78 78 79 EndGameManager.prototype.GetAlliedVictory = function() 80 { 81 return this.alliedVictory; 82 }; 83 79 84 EndGameManager.prototype.AlliedVictoryCheck = function() 80 85 { 81 86 if (this.skipAlliedVictoryCheck) -
binaries/data/mods/public/simulation/components/GuiInterface.js
144 144 ret.ceasefireTimeRemaining = ret.ceasefireActive ? cmpCeasefireManager.GetCeasefireStartedTime() + cmpCeasefireManager.GetCeasefireTime() - ret.timeElapsed : 0; 145 145 } 146 146 147 // Add the game type 147 // Add the game type and allied victory 148 148 let cmpEndGameManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_EndGameManager); 149 149 ret.gameType = cmpEndGameManager.GetGameType(); 150 ret.alliedVictory = cmpEndGameManager.GetAlliedVictory(); 150 151 151 152 // Add bartering prices 152 153 let cmpBarter = Engine.QueryInterface(SYSTEM_ENTITY, IID_Barter); -
binaries/data/mods/public/simulation/components/tests/test_EndGameManager.js
1 Engine.LoadComponentScript("interfaces/Player.js"); 2 Engine.LoadComponentScript("interfaces/EndGameManager.js"); 3 Engine.LoadComponentScript("Player.js"); 4 5 Engine.LoadComponentScript("PlayerManager.js"); 6 Engine.LoadComponentScript("EndGameManager.js"); 7 Engine.LoadComponentScript("GuiInterface.js"); 8 9 let cmpEndGameManager = ConstructComponent(SYSTEM_ENTITY, "EndGameManager"); 10 let playerEnt1 = 1; 11 let wonderDuration = 1; 12 13 let gameTypeSettings = {}; 14 gameTypeSettings.wonderDuration = wonderDuration * 60 * 1000; 15 16 AddMock(SYSTEM_ENTITY, IID_PlayerManager, { 17 "GetNumPlayers": () => 4 18 }); 19 20 AddMock(SYSTEM_ENTITY, IID_GuiInterface, { 21 "DeleteTimeNotification": () => null, 22 "AddTimeNotification": () => 1 23 }); 24 25 AddMock(playerEnt1, IID_Player, { 26 "GetName": () => "Player 1", 27 "GetState": () => "active", 28 }); 29 30 let cmpPlayerManager = ConstructComponent(SYSTEM_ENTITY, "PlayerManager"); 31 32 TS_ASSERT_EQUALS(cmpEndGameManager.skipAlliedVictoryCheck, true); 33 cmpEndGameManager.SetAlliedVictory(true); 34 TS_ASSERT_EQUALS(cmpEndGameManager.GetAlliedVictory(), true); 35 cmpEndGameManager.SetGameType("wonder", gameTypeSettings); 36 TS_ASSERT_EQUALS(cmpEndGameManager.CheckGameType("regicide"), false); 37 TS_ASSERT_EQUALS(cmpEndGameManager.skipAlliedVictoryCheck, false); 38 TS_ASSERT(cmpEndGameManager.GetGameType() == "wonder"); 39 TS_ASSERT_EQUALS(cmpEndGameManager.GetGameTypeSettings().wonderDuration, 60000); 40 cmpEndGameManager.AlliedVictoryCheck(); -
binaries/data/mods/public/simulation/components/tests/test_GuiInterface.js
Property changes on: binaries/data/mods/public/simulation/components/tests/test_EndGameManager.js ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property
47 47 }); 48 48 49 49 AddMock(SYSTEM_ENTITY, IID_EndGameManager, { 50 GetGameType: function() { return "conquest"; } 50 GetGameType: function() { return "conquest"; }, 51 GetAlliedVictory: function() { return false; } 51 52 }); 52 53 53 54 AddMock(SYSTEM_ENTITY, IID_PlayerManager, { … … 331 332 circularMap: false, 332 333 timeElapsed: 0, 333 334 gameType: "conquest", 335 alliedVictory: false, 334 336 barterPrices: {buy: {food: 150}, sell: {food: 25}} 335 337 }); 336 338 … … 450 452 circularMap: false, 451 453 timeElapsed: 0, 452 454 gameType: "conquest", 455 alliedVictory: false, 453 456 barterPrices: {buy: {food: 150}, sell: {food: 25}} 454 457 }); 455 458