diff --git a/binaries/data/mods/public/globalscripts/random.js b/binaries/data/mods/public/globalscripts/random.js index 761d699f84..7c9af9bc3e 100644 --- a/binaries/data/mods/public/globalscripts/random.js +++ b/binaries/data/mods/public/globalscripts/random.js @@ -14,3 +14,11 @@ function randomNormal2D() s = Math.sqrt(-2 * Math.log(s) / s); return [a * s, b * s]; } + +/** + * Return a random element of the source array + */ +function pickRandom(source) +{ + return source.length ? source[Math.floor(source.length * Math.random())] : undefined; +} diff --git a/binaries/data/mods/public/simulation/ai/common-api/utils.js b/binaries/data/mods/public/simulation/ai/common-api/utils.js index af58efd44a..4eed4fd7eb 100644 --- a/binaries/data/mods/public/simulation/ai/common-api/utils.js +++ b/binaries/data/mods/public/simulation/ai/common-api/utils.js @@ -54,13 +54,6 @@ m.AssocArraytoArray = function(assocArray) return endArray; }; -/** Picks a random element from an array */ -m.PickRandom = function(list) -{ - return list.length ? list[Math.floor(Math.random()*list.length)] : undefined; -}; - - /** Utility functions for conversions of maps of different sizes */ /** diff --git a/binaries/data/mods/public/simulation/ai/petra/attackManager.js b/binaries/data/mods/public/simulation/ai/petra/attackManager.js index 673899a76f..803c22529a 100644 --- a/binaries/data/mods/public/simulation/ai/petra/attackManager.js +++ b/binaries/data/mods/public/simulation/ai/petra/attackManager.js @@ -51,7 +51,7 @@ m.AttackManager.prototype.checkEvents = function(gameState, events) for (let evt of events.PlayerDefeated) this.defeated[evt.playerId] = true; - let answer = false; + let answer = "decline"; let other; let targetPlayer; for (let evt of events.AttackRequest) @@ -94,8 +94,10 @@ m.AttackManager.prototype.checkEvents = function(gameState, events) attack.requested = true; } } - answer = true; + answer = "join"; } + else if (other !== undefined) + answer = "other"; break; // take only the first attack request into account } if (targetPlayer !== undefined) diff --git a/binaries/data/mods/public/simulation/ai/petra/chatHelper.js b/binaries/data/mods/public/simulation/ai/petra/chatHelper.js index 0a88215da7..fcbd0c829b 100644 --- a/binaries/data/mods/public/simulation/ai/petra/chatHelper.js +++ b/binaries/data/mods/public/simulation/ai/petra/chatHelper.js @@ -1,7 +1,57 @@ var PETRA = function(m) { -m.chatNewDiplomacyMessages = { +m.launchAttackMessages = { + "hugeAttack": [ + markForTranslation("I am starting a massive military campaign against %(_player_)s, come and join me."), + markForTranslation("I have set up an huge army to crush %(_player_)s. Join me and you will have your share of the loot.") + ], + "other": [ + markForTranslation("I am launching an attack against %(_player_)s."), + markForTranslation("I have just sent an army against %(_player_)s.") + ] +}; + +m.answerRequestAttackMessages = { + "join": [ + markForTranslation("Let me regroup my army and I am with you against %(_player_)s."), + markForTranslation("I am doing the final preparation and I will attack %(_player_)s.") + ], + "decline": [ + markForTranslation("Sorry, I do not have enough soldiers currently, but my next attack will target %(_player_)s.") + ], + "other": [ + markForTranslation("I cannot help you against %(_player_)s for the time being, as I have another attack foreseen against %(_player_2)s.") + ] +}; + +m.sentTributeMessages = [ + markForTranslation("Here is a gift for %(_player_)s, make a good use of it."), + markForTranslation("I see you are in a bad situation %(_player_)s, I hope this will help."), + markForTranslation("I can help you this time %(_player_)s, but try to assemble more resources in the future.") +]; + +m.requestTributeMessages = [ + markForTranslation("I am in need of %(resource)s, can you help? I will make it up to you."), + markForTranslation("I would participate more efficiently in our common war effort if you could provide me some %(resource)s."), + markForTranslation("If you have some %(resource)s excess, that would help me strengthen my army.") +]; + +m.newTradeRouteMessages = [ + markForTranslation("I have set up a new route with %(_player_)s. Trading will be profitable for all of us."), + markForTranslation("A new trade route is set up with %(_player_)s. Take your share of the profits.") +]; + +m.newPhaseMessages = { + "started": [ + markForTranslation("I am advancing to the %(phase)s.") + ], + "completed": [ + markForTranslation("I have reached the %(phase)s.") + ] +}; + +m.newDiplomacyMessages = { "ally": [ markForTranslation("%(_player_)s and I are now allies.") ], @@ -15,18 +65,9 @@ m.chatNewDiplomacyMessages = { m.chatLaunchAttack = function(gameState, player, type) { - let message; - let proba = Math.random(); - if (type === "HugeAttack" && proba > 0.25 && proba < 0.75) - message = markForTranslation("I am starting a massive military campaign against %(_player_)s, come and join me."); - else if (proba < 0.5) - message = markForTranslation("I am launching an attack against %(_player_)s."); - else - message = markForTranslation("I have just sent an army against %(_player_)s."); - Engine.PostCommand(PlayerID, { "type": "aichat", - "message": "/allies "+ message, + "message": "/allies " + pickRandom(this.launchAttackMessages[type === "HugeAttack" ? type : "other"]), "translateMessage": true, "translateParameters": ["_player_"], "parameters": { "_player_": player } @@ -35,52 +76,20 @@ m.chatLaunchAttack = function(gameState, player, type) m.chatAnswerRequestAttack = function(gameState, player, answer, other) { - let message; - if (answer) - { - let proba = Math.random(); - if (proba < 0.5) - message = markForTranslation("Let me regroup my army and I am with you against %(_player_)s."); - else - message = markForTranslation("I am doing the final preparation and I will attack %(_player_)s."); - } - else - { - if (other !== undefined) - message = markForTranslation("I cannot help you against %(_player_)s for the time being, as I have another attack foreseen against %(_player_2)s."); - else - message = markForTranslation("Sorry, I do not have enough soldiers currently, but my next attack will target %(_player_)s."); - } - - let chat = { + Engine.PostCommand(PlayerID, { "type": "aichat", - "message": "/allies " + message, + "message": "/allies " + pickRandom(this.answerRequestAttackMessages[answer]), "translateMessage": true, - "translateParameters": ["_player_"], - "parameters": { "_player_": player } - }; - if (other !== undefined) - { - chat.translateParameters.push("_player_2"); - chat.parameters._player_2 = other; - } - Engine.PostCommand(PlayerID, chat); + "translateParameters": answer != "other" ? ["_player_"] : ["_player_", "_player_2"], + "parameters": answer != "other" ? { "_player_": player } : { "_player_": player, "_player2_": other } + }); }; m.chatSentTribute = function(gameState, player) { - let message; - let proba = Math.random(); - if (proba < 0.33) - message = markForTranslation("Here is a gift for %(_player_)s, make a good use of it."); - else if (proba < 0.66) - message = markForTranslation("I see you are in a bad situation %(_player_)s, I hope this will help."); - else - message = markForTranslation("I can help you this time %(_player_)s, but try to assemble more resources in the future."); - Engine.PostCommand(PlayerID, { "type": "aichat", - "message": "/allies " + message, + "message": "/allies " + pickRandom(this.sentTributeMessages), "translateMessage": true, "translateParameters": ["_player_"], "parameters": { "_player_": player } @@ -89,18 +98,9 @@ m.chatSentTribute = function(gameState, player) m.chatRequestTribute = function(gameState, resource) { - let message; - let proba = Math.random(); - if (proba < 0.33) - message = markForTranslation("I am in need of %(resource)s, can you help? I will make it up to you."); - else if (proba < 0.66) - message = markForTranslation("I would participate more efficiently in our common war effort if you could provide me some %(resource)s."); - else - message = markForTranslation("If you have some %(resource)s excess, that would help me strengthen my army."); - Engine.PostCommand(PlayerID, { "type": "aichat", - "message": "/allies " + message, + "message": "/allies " + pickRandom(this.requestTributeMessages), "translateMessage": true, "translateParameters": { "resource": "withinSentence" }, "parameters": { "resource": gameState.sharedScript.resourceInfo.names[resource] } @@ -109,31 +109,20 @@ m.chatRequestTribute = function(gameState, resource) m.chatNewTradeRoute = function(gameState, player) { - let message; - let proba = Math.random(); - if (proba < 0.5) - message = markForTranslation("I have set up a new route with %(_player_)s. Trading will be profitable for all of us."); - else - message = markForTranslation("A new trade route is set up with %(_player_)s. Take your share of the profits."); - Engine.PostCommand(PlayerID, { "type": "aichat", - "message": "/allies " + message, + "message": "/allies " + pickRandom(this.newTradeRouteMessages), "translateMessage": true, "translateParameters": ["_player_"], "parameters": { "_player_": player } }); }; -m.chatNewPhase = function(gameState, phase, started) +m.chatNewPhase = function(gameState, phase, status) { - let message = started ? - markForTranslation("I am advancing to the %(phase)s.") : - markForTranslation("I have reached the %(phase)s."); - Engine.PostCommand(PlayerID, { "type": "aichat", - "message": "/allies " + message, + "message": "/allies " + pickRandom(this.newPhaseMessages[status]), "translateMessage": true, "translateParameters": ["phase"], "parameters": { "phase": phase } @@ -144,7 +133,7 @@ m.chatNewDiplomacy = function(gameState, player, newDiplomaticStance) { Engine.PostCommand(PlayerID, { "type": "aichat", - "message": API3.PickRandom(this.chatNewDiplomacyMessages[newDiplomaticStance]), + "message": pickRandom(this.newDiplomacyMessages[newDiplomaticStance]), "translateMessage": true, "translateParameters": ["_player_"], "parameters": { "_player_": player } diff --git a/binaries/data/mods/public/simulation/ai/petra/headquarters.js b/binaries/data/mods/public/simulation/ai/petra/headquarters.js index 3d78c9194c..828872231a 100644 --- a/binaries/data/mods/public/simulation/ai/petra/headquarters.js +++ b/binaries/data/mods/public/simulation/ai/petra/headquarters.js @@ -408,14 +408,14 @@ m.HQ.prototype.checkEvents = function (gameState, events, queues) m.HQ.prototype.OnTownPhase = function(gameState) { let phaseName = gameState.getTemplate(gameState.townPhase()).name(); - m.chatNewPhase(gameState, phaseName, true); + m.chatNewPhase(gameState, phaseName, "started"); }; /** Called by the "city phase" research plan once it's started */ m.HQ.prototype.OnCityPhase = function(gameState) { let phaseName = gameState.getTemplate(gameState.cityPhase()).name(); - m.chatNewPhase(gameState, phaseName, true); + m.chatNewPhase(gameState, phaseName, "started"); }; /** This code trains citizen workers, trying to keep close to a ratio of worker/soldiers */ @@ -2113,7 +2113,7 @@ m.HQ.prototype.update = function(gameState, queues, events) else if (this.currentPhase == 3) phaseName = gameState.getTemplate(gameState.cityPhase()).name(); - m.chatNewPhase(gameState, phaseName, false); + m.chatNewPhase(gameState, phaseName, "completed"); } if (this.numActiveBase() > 0)