Ticket #3176: livestock_attack_notification_improved_suppression_r16653.diff
File livestock_attack_notification_improved_suppression_r16653.diff, 5.6 KB (added by , 9 years ago) |
---|
-
binaries/data/mods/public/gui/session/messages.js
119 119 addChatMessage({ 120 120 "type": "attack", 121 121 "player": player, 122 "attacker": notification.attacker 122 "attacker": notification.attacker, 123 "targetIsDomesticAnimal": notification.targetIsDomesticAnimal 123 124 }); 124 125 }, 125 126 "dialog": function(notification, player) … … 511 512 return; 512 513 513 514 [username, playerColor] = getUsernameAndColor(msg.attacker); 514 formatted = sprintf(translate("You have been attacked by %(attacker)s!"), { attacker: "[color=\"" + playerColor + "\"]" + username + "[/color]" }); 515 // Since livestock can be attacked/gathered by allied players, 516 // we display a more specific notification in this case to not confuse the player 517 if (msg.targetIsDomesticAnimal) 518 var message = translate("Your livestock have been attacked by %(attacker)s!"); 519 else 520 var message = translate("You have been attacked by %(attacker)s!"); 521 formatted = sprintf(message, { attacker: "[color=\"" + playerColor + "\"]" + username + "[/color]" }); 515 522 break; 516 523 case "message": 517 524 // May have been hidden by the 'team' command. -
binaries/data/mods/public/simulation/components/AttackDetection.js
22 22 this.suppressedList = []; 23 23 }; 24 24 25 AttackDetection.prototype.ActivateTimer = function(event) 26 { 27 var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); 28 cmpTimer.SetTimeout(this.entity, IID_AttackDetection, "HandleTimeout", this.suppressionTime); 29 }; 30 25 31 AttackDetection.prototype.AddSuppression = function(event) 26 32 { 27 33 this.suppressedList.push(event); 34 this.ActivateTimer(); 35 }; 28 36 29 var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); 30 cmpTimer.SetTimeout(this.entity, IID_AttackDetection, "HandleTimeout", this.suppressionTime); 37 AttackDetection.prototype.UpdateSuppressionEvent = function(index, event) 38 { 39 this.suppressedList[index] = event; 40 this.ActivateTimer(); 31 41 }; 32 42 33 43 //// Message handlers //// … … 53 63 // Don't register attacks dealt by myself 54 64 if (cmpAttackerOwnership.GetOwner() == cmpPlayer.GetPlayerID()) 55 65 return; 66 67 // Since livestock can be attacked/gathered by allied players 68 // and generally not so valuable as other units/buildings, 69 // we have a lower priority notification for it, which can be 70 // later overriden by a regular one. 71 var cmpTargetIdentity = Engine.QueryInterface(target, IID_Identity); 72 var targetIsDomesticAnimal = cmpTargetIdentity && cmpTargetIdentity.HasClass("Animal") && cmpTargetIdentity.HasClass("Domestic"); 56 73 57 74 var cmpPosition = Engine.QueryInterface(target, IID_Position); 58 75 if (!cmpPosition || !cmpPosition.IsInWorld()) 59 76 return; 60 77 var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); 61 var event = {target: target, position: cmpPosition.GetPosition(), time: cmpTimer.GetTime()}; 78 var event = { 79 "target": target, 80 "position": cmpPosition.GetPosition(), 81 "time": cmpTimer.GetTime(), 82 "targetIsDomesticAnimal": targetIsDomesticAnimal 83 }; 62 84 63 for (var i = 0; i < this.suppressedList.length; i++) 85 // If we already have a low priority livestock event in suppressed list, 86 // and now a more important target is attacked, we want to upgrade the 87 // suppressed event and send the new notification 88 var isPriorityIncreased = false; 89 for (var i = 0; i < this.suppressedList.length; ++i) 64 90 { 65 91 var element = this.suppressedList[i]; 66 92 … … 69 95 var dist = SquaredDistance(element.position, event.position); 70 96 if (dist < this.suppressionRangeSquared) 71 97 { 72 if (dist < this.suppressionTransferRangeSquared) 73 element = event; 98 isPriorityIncreased = element.targetIsDomesticAnimal && !targetIsDomesticAnimal; 99 var isPriorityDescreased = !element.targetIsDomesticAnimal && targetIsDomesticAnimal; 100 101 if (isPriorityIncreased 102 || (!isPriorityDescreased && dist < this.suppressionTransferRangeSquared)) 103 this.UpdateSuppressionEvent(i, event); 104 105 // If priority has increased, exit the loop to send the upgraded notification below 106 if (isPriorityIncreased) 107 break; 108 74 109 return; 75 110 } 76 111 } 77 112 78 this.AddSuppression(event); 113 // If priority has increased for an existing event, then we already have it 114 // in the suppression list 115 if (!isPriorityIncreased) 116 this.AddSuppression(event); 79 117 Engine.PostMessage(this.entity, MT_AttackDetected, { "player": cmpPlayer.GetPlayerID(), "event": event }); 80 118 var cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface); 81 cmpGuiInterface.PushNotification({"type": "attack", "players": [cmpPlayer.GetPlayerID()], "attacker": cmpAttackerOwnership.GetOwner() }); 119 cmpGuiInterface.PushNotification({ 120 "type": "attack", 121 "players": [cmpPlayer.GetPlayerID()], 122 "attacker": cmpAttackerOwnership.GetOwner(), 123 "targetIsDomesticAnimal": targetIsDomesticAnimal 124 }); 82 125 PlaySound("attacked", target); 83 126 }; 84 127 … … 91 134 { 92 135 var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); 93 136 var now = cmpTimer.GetTime(); 94 for (var i = 0; i < this.suppressedList.length; i++)137 for (var i = 0; i < this.suppressedList.length; ++i) 95 138 { 96 139 var event = this.suppressedList[i]; 97 140 … … 99 142 if (now - event.time >= this.suppressionTime) 100 143 { 101 144 this.suppressedList.splice(i, 1); 102 i--;103 145 return; 104 146 } 105 147 }