Ticket #3176: livestock_attack_notification_improved_suppression_r16653.diff

File livestock_attack_notification_improved_suppression_r16653.diff, 5.6 KB (added by fcxSanya, 9 years ago)

New version of livestock_attack_notification_improved_suppression_r16572.diff​, updated according to leper's review in #comment:5

  • binaries/data/mods/public/gui/session/messages.js

     
    119119        addChatMessage({
    120120            "type": "attack",
    121121            "player": player,
    122             "attacker": notification.attacker
     122            "attacker": notification.attacker,
     123            "targetIsDomesticAnimal": notification.targetIsDomesticAnimal
    123124        });
    124125    },
    125126    "dialog": function(notification, player)
     
    511512            return;
    512513
    513514        [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]" });
    515522        break;
    516523    case "message":
    517524        // May have been hidden by the 'team' command.
  • binaries/data/mods/public/simulation/components/AttackDetection.js

     
    2222    this.suppressedList = [];
    2323};
    2424
     25AttackDetection.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
    2531AttackDetection.prototype.AddSuppression = function(event)
    2632{
    2733    this.suppressedList.push(event);
     34    this.ActivateTimer();
     35};
    2836
    29     var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
    30     cmpTimer.SetTimeout(this.entity, IID_AttackDetection, "HandleTimeout", this.suppressionTime);
     37AttackDetection.prototype.UpdateSuppressionEvent = function(index, event)
     38{
     39    this.suppressedList[index] = event;
     40    this.ActivateTimer();
    3141};
    3242
    3343//// Message handlers ////
     
    5363    // Don't register attacks dealt by myself
    5464    if (cmpAttackerOwnership.GetOwner() == cmpPlayer.GetPlayerID())
    5565        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");
    5673
    5774    var cmpPosition = Engine.QueryInterface(target, IID_Position);
    5875    if (!cmpPosition || !cmpPosition.IsInWorld())
    5976        return;
    6077    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    };
    6284
    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)
    6490    {
    6591        var element = this.suppressedList[i];
    6692
     
    6995        var dist = SquaredDistance(element.position, event.position);
    7096        if (dist < this.suppressionRangeSquared)
    7197        {
    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
    74109            return;
    75110        }
    76111    }
    77112
    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);
    79117    Engine.PostMessage(this.entity, MT_AttackDetected, { "player": cmpPlayer.GetPlayerID(), "event": event });
    80118    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    });
    82125    PlaySound("attacked", target);
    83126};
    84127
     
    91134{
    92135    var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
    93136    var now = cmpTimer.GetTime();
    94     for (var i = 0; i < this.suppressedList.length; i++)
     137    for (var i = 0; i < this.suppressedList.length; ++i)
    95138    {
    96139        var event = this.suppressedList[i];
    97140
     
    99142        if (now - event.time >= this.suppressionTime)
    100143        {
    101144            this.suppressedList.splice(i, 1);
    102             i--;
    103145            return;
    104146        }
    105147    }