Ticket #4143: petra_lastmanstanding_v1.2.patch
File petra_lastmanstanding_v1.2.patch, 13.8 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 322 m.GameState.prototype.hasNeutrals = function() 323 { 324 for (let i in this.playerData.isNeutral) 325 if (this.playerData.isNeutral[i] && 326 this.sharedScript.playersData[i].state !== "defeated") 327 return true; 328 return false; 329 }; 330 331 m.GameState.prototype.isPlayerNeutral = function(id) 332 { 333 return this.playerData.isNeutral[id]; 334 }; 335 299 336 m.GameState.prototype.isPlayerAlly = function(id) 300 337 { 301 338 return this.playerData.isAlly[id]; … … 408 445 return this.updatingGlobalCollection("" + this.player + "-units", m.Filters.byClass("Unit"), this.getOwnEntities()); 409 446 }; 410 447 411 m.GameState.prototype.getAllyEntities = function( )448 m.GameState.prototype.getAllyEntities = function(allyID) 412 449 { 413 return this.entities.filter(m.Filters.byOwners(this.getAllies())); 450 if (allyID === undefined) 451 return this.entities.filter(m.Filters.byOwners(this.getAllies())); 452 453 return this.updatingGlobalCollection("" + allyID + "-entities", m.Filters.byOwner(allyID)); 414 454 }; 415 455 416 456 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 the diplomacy at the start of the game 120 */ 121 m.DiplomacyManager.prototype.diplomacyCheck = function(gameState) 122 { 123 if (!gameState.getAlliedVictory() && !gameState.isCeasefireActive()) 124 this.lastManStandingCheck(gameState); 125 }; 113 126 127 /** 128 * If the "Last Man Standing" option is enabled, check if the only remaining players are allies or neutral. 129 * If so, turn against the strongest first 130 */ 131 m.DiplomacyManager.prototype.lastManStandingCheck = function(gameState) 132 { 133 if (gameState.getAlliedVictory() || gameState.isCeasefireActive()) 134 return; 135 136 if (gameState.hasEnemies()) 137 { 138 this.waitingToBetray = false; 139 return; 140 } 141 142 if (!gameState.hasAllies() && !gameState.hasNeutrals()) 143 return; 144 145 // wait a bit before turning 146 if (!this.waitingToBetray) 147 { 148 this.allyBetrayLapseTime = gameState.ai.elapsedTime + Math.random() * 100 + 10; 149 this.waitingToBetray = true; 150 return; 151 } 152 153 // do not turn against a player yet if we are not strong enough 154 if (gameState.getOwnUnits().length < 50) 155 { 156 this.allyBetrayLapseTime += 60; 157 return; 158 } 159 160 let allyToTurnAgainst; 161 let turnFactor = 0; 162 let max = 0; 163 164 // count the amount of entities remaining players have 165 for (let i = 1; i < gameState.sharedScript.playersData.length; ++i) 166 { 167 if (i === PlayerID || gameState.ai.HQ.attackManager.defeated[i]) 168 continue; 169 170 turnFactor = gameState.getAllyEntities(i).length; 171 172 if (gameState.isPlayerNeutral(i)) // be more inclined to turn against neutral players 173 turnFactor += 150; 174 175 if (turnFactor < max) 176 continue; 177 178 max = turnFactor; 179 allyToTurnAgainst = i; 180 } 181 182 if (allyToTurnAgainst) 183 { 184 Engine.PostCommand(PlayerID, { "type": "diplomacy", "player": allyToTurnAgainst, "to": "enemy" }); 185 if (this.Config.debug > 1) 186 API3.warn("player " + allyToTurnAgainst + " is now an enemy"); 187 if (this.Config.chat) 188 m.chatNewDiplomacy(gameState, allyToTurnAgainst, true); 189 } 190 this.allyBetrayLapseTime = -1; 191 this.waitingToBetray = false; 192 }; 193 114 194 m.DiplomacyManager.prototype.update = function(gameState, events) 115 195 { 116 196 this.checkEvents(gameState, events); … … 117 197 118 198 if (!gameState.ai.HQ.saveResources && gameState.ai.elapsedTime > this.nextTributeUpdate) 119 199 this.tributes(gameState); 200 201 if (this.waitingToBetray && gameState.ai.elapsedTime > this.allyBetrayLapseTime) 202 this.lastManStandingCheck(gameState); 120 203 }; 121 204 122 205 m.DiplomacyManager.prototype.Serialize = function() 123 206 { 124 return { "nextTributeUpdate": this.nextTributeUpdate, "nextTributeRequest": this.nextTributeRequest }; 207 return { 208 "nextTributeUpdate": this.nextTributeUpdate, 209 "nextTributeRequest": this.nextTributeRequest, 210 "allyBetrayLapseTime": this.allyBetrayLapseTime, 211 "waitingToBetray": this.waitingToBetray 212 }; 125 213 }; 126 214 127 215 m.DiplomacyManager.prototype.Deserialize = function(data) 128 216 { 129 this.nextTributeUpdate = data.nextTributeUpdate;130 this.nextTributeRequest = data.nextTributeRequest;217 for (let key in data) 218 this[key] = data[key]; 131 219 }; 132 220 133 221 return m; -
binaries/data/mods/public/simulation/ai/petra/startingStrategy.js
54 54 // configure our first base strategy 55 55 if (this.baseManagers.length > 1) 56 56 this.configFirstBase(gameState); 57 this.diplomacyManager.diplomacyCheck(gameState); 57 58 }; 58 59 59 60 /** -
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); -
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