Ticket #3176: livestock_attack_notification_improved_suppression_r16572.diff

File livestock_attack_notification_improved_suppression_r16572.diff, 4.0 KB (added by fcxSanya, 9 years ago)

In this patch a different "Your livestock have been attacked by %(attacker)s!" notification is displayed if the attacked entity has classes "Animal" and "Domestic"; if a non-livestock entity is attacked in the same area during the livestock notification suppression, the regular notification is still displayed

  • 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)
     
    509510            return;
    510511
    511512        [username, playerColor] = getUsernameAndColor(msg.attacker);
    512         formatted = sprintf(translate("You have been attacked by %(attacker)s!"), { attacker: "[color=\"" + playerColor + "\"]" + username + "[/color]" });
     513        // Since livestock can be attacked/gathered by allied players,
     514        // we display a more specific notification in this case to not confuse the player
     515        if (msg.targetIsDomesticAnimal)
     516            var message = translate("Your livestock have been attacked by %(attacker)s!");
     517        else
     518            var message = translate("You have been attacked by %(attacker)s!");
     519        formatted = sprintf(message, { attacker: "[color=\"" + playerColor + "\"]" + username + "[/color]" });
    513520        break;
    514521    case "message":
    515522        // May have been hidden by the 'team' command.
  • binaries/data/mods/public/simulation/components/AttackDetection.js

     
    5353    // Don't register attacks dealt by myself
    5454    if (cmpAttackerOwnership.GetOwner() == cmpPlayer.GetPlayerID())
    5555        return;
     56       
     57    // Since livestock can be attacked/gathered by allied players
     58    // and generally not so valuable as other units/buildings,
     59    // we have a lower priority notification for it, which can be
     60    // later overriden by a regular one.
     61    var cmpTargetIdentity = Engine.QueryInterface(target, IID_Identity);
     62    var targetIsDomesticAnimal = cmpTargetIdentity.HasClass("Animal") && cmpTargetIdentity.HasClass("Domestic");
    5663
    5764    var cmpPosition = Engine.QueryInterface(target, IID_Position);
    5865    if (!cmpPosition || !cmpPosition.IsInWorld())
    5966        return;
    6067    var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
    61     var event = {target: target, position: cmpPosition.GetPosition(), time: cmpTimer.GetTime()};
     68    var event = {
     69        target: target,
     70        position: cmpPosition.GetPosition(),
     71        time: cmpTimer.GetTime(),
     72        targetIsDomesticAnimal: targetIsDomesticAnimal
     73    };
    6274
     75    // If we already have a low priority livestock event in suppressed list,
     76    // and now a more important target is attacked, we want to upgrade the
     77    // suppressed event and send the new notification
     78    var isPriorityIncreased = false;
    6379    for (var i = 0; i < this.suppressedList.length; i++)
    6480    {
    6581        var element = this.suppressedList[i];
     
    6985        var dist = SquaredDistance(element.position, event.position);
    7086        if (dist < this.suppressionRangeSquared)
    7187        {
     88            isPriorityIncreased = element.targetIsDomesticAnimal && !targetIsDomesticAnimal;
     89
    7290            if (dist < this.suppressionTransferRangeSquared)
    7391                element = event;
     92
     93            if (isPriorityIncreased)
     94                break;
     95
    7496            return;
    7597        }
    7698    }
    7799
    78     this.AddSuppression(event);
     100    // If priority has increased for an existing event, then we already have it
     101    // in the suppression list
     102    if (!isPriorityIncreased)
     103        this.AddSuppression(event);
    79104    Engine.PostMessage(this.entity, MT_AttackDetected, { "player": cmpPlayer.GetPlayerID(), "event": event });
    80105    var cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
    81     cmpGuiInterface.PushNotification({"type": "attack", "players": [cmpPlayer.GetPlayerID()], "attacker": cmpAttackerOwnership.GetOwner() });
     106    cmpGuiInterface.PushNotification({
     107        "type": "attack",
     108        "players": [cmpPlayer.GetPlayerID()],
     109        "attacker": cmpAttackerOwnership.GetOwner(),
     110        "targetIsDomesticAnimal": targetIsDomesticAnimal
     111    });
    82112    PlaySound("attacked", target);
    83113};
    84114