Ticket #4085: sharing-v2.patch

File sharing-v2.patch, 9.3 KB (added by mimo, 8 years ago)

extended version, the first patch missed the hunting and fishing cases

  • binaries/data/mods/public/simulation/ai/common-api/entity.js

     
    623623    decaying: function() { return this._entity.decaying !== undefined ? this._entity.decaying : undefined; },
    624624    capturePoints: function() {return this._entity.capturePoints !== undefined ? this._entity.capturePoints : undefined; },
    625625
     626    "isSharedDropsite": function() { return this._entity.sharedDropsite === true; },
     627
    626628    /**
    627629     * Returns the current training queue state, of the form
    628630     * [ { "id": 0, "template": "...", "count": 1, "progress": 0.5, "metadata": ... }, ... ]
  • binaries/data/mods/public/simulation/ai/common-api/gamestate.js

     
    595595m.GameState.prototype.getOwnDropsites = function(resource)
    596596{
    597597    if (resource !== undefined)
    598         return this.updatingCollection("dropsite-" + resource, m.Filters.isDropsite(resource), this.getOwnEntities());
    599     return this.updatingCollection("dropsite-all", m.Filters.isDropsite(), this.getOwnEntities());
     598        return this.updatingCollection("ownDropsite-" + resource, m.Filters.isDropsite(resource), this.getOwnEntities());
     599    return this.updatingCollection("ownDropsite-all", m.Filters.isDropsite(), this.getOwnEntities());
    600600};
    601601
     602m.GameState.prototype.getAnyDropsites = function(resource)
     603{
     604    if (resource !== undefined)
     605        return this.updatingGlobalCollection("anyDropsite-" + resource, m.Filters.isDropsite(resource), this.getEntities());
     606    return this.updatingGlobalCollection("anyDropsite-all", m.Filters.isDropsite(), this.getEntities());
     607};
     608
    602609m.GameState.prototype.getResourceSupplies = function(resource)
    603610{
    604611    return this.updatingGlobalCollection("resource-" + resource, m.Filters.byResource(resource), this.getEntities());
  • binaries/data/mods/public/simulation/ai/petra/entityExtend.js

     
    9999    let closestDropsite;
    100100    let distmin = Math.min();
    101101    let access = gameState.ai.accessibility.getAccessValue(ent.position());
    102     gameState.getOwnDropsites(resource).forEach(function(dropsite) {
    103         if (!dropsite.position() || dropsite.getMetadata(PlayerID, "access") !== access)
    104             return;
     102    let hasSharedDropsites = gameState.playerData.hasSharedDropsites;
     103    let dropsiteCollection = hasSharedDropsites ? gameState.getAnyDropsites(resource) : gameState.getOwnDropsites(resource);
     104    for (let dropsite of dropsiteCollection.values())
     105    {
     106        if (!dropsite.position())
     107            continue;
     108        let owner = dropsite.owner();
     109        // owner !== PlayerID can only happen when hasSharedDropsites === true, so no need to test it again
     110        if (owner !== PlayerID && (!dropsite.isSharedDropsite() || !gameState.isPlayerMutualAlly(owner)))
     111            continue;
     112        let dropsiteAccess = dropsite.getMetadata(PlayerID, "access");
     113        if (!dropsiteAccess)
     114        {
     115            dropsiteAccess = gameState.ai.accessibility.getAccessValue(dropsite.position());
     116            dropsite.setMetadata(PlayerID, "access", dropsiteAccess);
     117        }
     118        if (dropsiteAccess !== access)
     119            continue;
    105120        let dist = API3.SquareVectorDistance(ent.position(), dropsite.position());
    106121        if (dist > distmin)
    107             return;
     122            continue;
    108123        distmin = dist;
    109124        closestDropsite = dropsite;
    110     });
     125    }
    111126
    112127    if (!closestDropsite)
    113128        return false;
  • binaries/data/mods/public/simulation/ai/petra/worker.js

     
    541541    let foodDropsites = gameState.getOwnDropsites("food");
    542542    let entPosition = position ? position : this.ent.position();
    543543    let access = gameState.ai.accessibility.getAccessValue(entPosition);
     544    let hasSharedDropsites = gameState.playerData.hasSharedDropsites;
     545    let foodDropsites = (hasSharedDropsites ? gameState.getAnyDropsites("food") : gameState.getOwnDropsites("food")).toEntityArray();
    544546
    545547    let nearestDropsiteDist = function(supply) {
    546548        let distMin = 1000000;
    547549        let pos = supply.position();
    548         foodDropsites.forEach(function (dropsite) {
    549             if (!dropsite.position() || dropsite.getMetadata(PlayerID, "access") !== access)
    550                 return;
    551             let dist = API3.SquareVectorDistance(pos, dropsite.position());
    552             if (dist < distMin)
    553                 distMin = dist;
    554         });
     550        for (let dropsite of foodDropsites)
     551        {
     552            if (!dropsite.position())
     553                continue;
     554            let owner = dropsite.owner();
     555            // owner !== PlayerID can only happen when hasSharedDropsites === true, so no need to test it again
     556            if (owner !== PlayerID && (!dropsite.isSharedDropsite() || !gameState.isPlayerMutualAlly(owner)))
     557                continue;
     558            let dropsiteAccess = dropsite.getMetadata(PlayerID, "access");
     559            if (!dropsiteAccess)
     560            {
     561                dropsiteAccess = gameState.ai.accessibility.getAccessValue(dropsite.position());
     562                dropsite.setMetadata(PlayerID, "access", dropsiteAccess);
     563            }
     564            if (dropsiteAccess !== access)
     565                continue;
     566            distMin = Math.min(distMin, API3.SquareVectorDistance(pos, dropsite.position()));
     567        }
    555568        return distMin;
    556569    };
    557570
     
    636649
    637650    let entPosition = this.ent.position();
    638651    let fisherSea = this.ent.getMetadata(PlayerID, "sea");
    639     let docks = gameState.getOwnEntitiesByClass("Dock", true).filter(API3.Filters.isBuilt());
     652    let hasSharedDropsites = gameState.playerData.hasSharedDropsites;
     653    let fishDropsites = (hasSharedDropsites ? gameState.getAnyDropsites("food") : gameState.getOwnDropsites("food")).filter(API3.Filters.byClass("Dock")).toEntityArray();
    640654
    641655    let nearestDropsiteDist = function(supply) {
    642656        let distMin = 1000000;
    643657        let pos = supply.position();
    644         docks.forEach(function (dock) {
    645             if (!dock.position() || dock.getMetadata(PlayerID, "sea") !== fisherSea)
    646                 return;
    647             let dist = API3.SquareVectorDistance(pos, dock.position());
    648             if (dist < distMin)
    649                 distMin = dist;
    650         });
     658        for (let dropsite of fishDropsites)
     659        {
     660            if (!dropsite.position())
     661                continue;
     662            let owner = dropsite.owner();
     663            // owner !== PlayerID can only happen when hasSharedDropsites === true, so no need to test it again
     664            if (owner !== PlayerID && (!dropsite.isSharedDropsite() || !gameState.isPlayerMutualAlly(owner)))
     665                continue;
     666            let dropsiteSea = dropsite.getMetadata(PlayerID, "sea");
     667            if (!dropsiteSea)
     668            {
     669                dropsiteSea = gameState.ai.accessibility.getAccessValue(dropsite.position(), true);
     670                dropsite.setMetadata(PlayerID, "sea", dropsiteSea);
     671            }
     672            if (dropsiteSea !== fisherSea)
     673                continue;
     674            distMin = Math.min(distMin, API3.SquareVectorDistance(pos, dropsite.position()));
     675        }
    651676        return distMin;
    652677    };
    653678
  • binaries/data/mods/public/simulation/components/AIProxy.js

     
    193193    this.changes.foundationBuilders = msg.to;
    194194};
    195195
     196AIProxy.prototype.OnDropsiteSharingChanged = function(msg)
     197{
     198    if (!this.NotifyChange())
     199        return;
     200    this.changes.sharedDropsite = msg.shared;
     201};
     202
    196203AIProxy.prototype.OnTerritoryDecayChanged = function(msg)
    197204{
    198205    if (!this.NotifyChange())
     
    286293        ret.resourceCarrying = cmpResourceGatherer.GetCarryingStatus();
    287294    }
    288295
     296    let cmpResourceDropsite = Engine.QueryInterface(this.entity, IID_ResourceDropsite);
     297    if (cmpResourceDropsite)
     298    {
     299        // Updated by OnDropsiteSharingChanged
     300        ret.sharedDropsite = cmpResourceDropsite.IsShared();
     301    }
     302
    289303    let cmpGarrisonHolder = Engine.QueryInterface(this.entity, IID_GarrisonHolder);
    290304    if (cmpGarrisonHolder)
    291305    {
  • binaries/data/mods/public/simulation/components/ResourceDropsite.js

     
    5252
    5353ResourceDropsite.prototype.SetSharing = function(value)
    5454{
    55     if (this.sharable)
    56         this.shared = value;
     55    if (!this.sharable)
     56        return;
     57    this.shared = value;
     58    Engine.PostMessage(this.entity, MT_DropsiteSharingChanged, { "shared": this.shared });
    5759};
    5860
    5961Engine.RegisterComponentType(IID_ResourceDropsite, "ResourceDropsite", ResourceDropsite);
  • binaries/data/mods/public/simulation/components/interfaces/ResourceDropsite.js

     
    11Engine.RegisterInterface("ResourceDropsite");
     2
     3// Message of the form { "shared": true },
     4Engine.RegisterMessageType("DropsiteSharingChanged");