Ticket #3277: ticket3277.patch

File ticket3277.patch, 5.8 KB (added by mimo, 8 years ago)
  • binaries/data/mods/public/simulation/components/Market.js

     
     1function Market() {}
     2
     3Market.prototype.Schema =
     4    "<empty/>";
     5
     6Market.prototype.Init = function()
     7{
     8    this.traders = [];  // list of traders with a route on this market
     9};
     10
     11Market.prototype.GetTraders = function()
     12{
     13    return this.traders.slice();
     14};
     15
     16Market.prototype.AddTrader = function(ent)
     17{
     18    if (this.traders.indexOf(ent) != -1)
     19        return;
     20    this.traders.push(ent);
     21};
     22
     23Market.prototype.RemoveTrader = function(ent)
     24{
     25    let index = this.traders.indexOf(ent);
     26    if (index != -1)
     27        this.traders.splice(index, 1);
     28};
     29
     30/* Check if all traders with a route on this market can still trade */
     31Market.prototype.OnOwnershipChanged = function(msg)
     32{
     33    for (let ent of this.traders)
     34    {
     35        let cmpTrader = Engine.QueryInterface(ent, IID_Trader);
     36        if (!cmpTrader || (msg.to == -1 || !cmpTrader.CanTrade(this.entity)))
     37        {
     38            this.RemoveTrader(ent);
     39            if (cmpTrader)
     40                cmpTrader.RemoveMarket(this.entity);
     41        }
     42    }
     43};
     44
     45Engine.RegisterComponentType(IID_Market, "Market", Market);
  • binaries/data/mods/public/simulation/components/Trader.js

     
    1 // See helpers/TraderGain.js for the CalculateTaderGain() function which works out how many 
    2 // resources a trader gets 
     1// See helpers/TraderGain.js for the CalculateTaderGain() function which works out how many
     2// resources a trader gets
    33
    44// Additional gain for ships for each garrisoned trader, in percents
    55const GARRISONED_TRADER_ADDITION = 20;
     
    6161                gain.market2Gain = Math.round(garrisonMultiplier * gain.market2Gain);
    6262        }
    6363    }
    64    
     64
    6565    return gain;
    6666};
    6767
     
    6969// Return true if at least one of markets was changed.
    7070Trader.prototype.SetTargetMarket = function(target, source)
    7171{
    72     // Check that target is a market
    73     var cmpTargetIdentity = Engine.QueryInterface(target, IID_Identity);
    74     if (!cmpTargetIdentity)
     72    var cmpTargetMarket = Engine.QueryInterface(target, IID_Market);
     73    if (!cmpTargetMarket)
    7574        return false;
    76     if (!cmpTargetIdentity.HasClass("Market") && !cmpTargetIdentity.HasClass("NavalMarket"))
    77         return false;
    7875
    7976    if (source)
    8077    {
    8178        // Establish a trade route with both markets in one go.
    82         cmpTargetIdentity = Engine.QueryInterface(source, IID_Identity);
    83         if (!cmpTargetIdentity)
     79        let cmpSourceMarket = Engine.QueryInterface(source, IID_Market);
     80        if (!cmpSourceMarket)
    8481            return false;
    85         if (!cmpTargetIdentity.HasClass("Market") && !cmpTargetIdentity.HasClass("NavalMarket"))
    86             return false;
    8782        this.markets = [source];
     83        cmpSourceMarket.AddTrader(this.entity);
    8884    }
    8985    if (this.markets.length >= 2)
    9086    {
    9187        // If we already have both markets - drop them
    9288        // and use the target as first market
     89        for (let market of this.markets)
     90        {
     91            let cmpMarket = Engine.QueryInterface(market, IID_Market);
     92            if (cmpMarket)
     93                cmpMarket.RemoveTrader(this.entity);
     94        }
    9395        this.index = 0;
    9496        this.markets = [target];
     97        cmpTargetMarket.AddTrader(this.entity);
    9598    }
    9699    else if (this.markets.length == 1)
    97100    {
     
    103106        {
    104107            this.index = 0;
    105108            this.markets.push(target);
     109            cmpTargetMarket.AddTrader(this.entity);
    106110            this.goods.amount = this.CalculateGain(this.markets[0], this.markets[1]);
    107111        }
    108112    }
     
    112116        // set the target as first market
    113117        this.index = 0;
    114118        this.markets = [target];
     119        cmpTargetMarket.AddTrader(this.entity);
    115120    }
    116121    // Drop carried goods if markets were changed
    117122    this.goods.amount = null;
     
    242247    return this.goods;
    243248};
    244249
     250/* Called when this trader can no more trade with the market */
     251Trader.prototype.RemoveMarket = function(market)
     252{
     253    let index = this.markets.indexOf(market);
     254    if (index != -1)
     255        this.markets.splice(index, 1);
     256};
     257
    245258Trader.prototype.StopTrading = function()
    246259{
     260    for (let market of this.markets)
     261    {
     262        let cmpMarket = Engine.QueryInterface(market, IID_Market);
     263        if (cmpMarket)
     264            cmpMarket.RemoveTrader(this.entity);
     265    }
    247266    this.index = -1;
    248267    this.markets = [];
    249268    // Drop carried goods
  • binaries/data/mods/public/simulation/components/interfaces/Market.js

     
     1Engine.RegisterInterface("Market");
  • binaries/data/mods/public/simulation/templates/template_structure_economic_market.xml

     
    3232    <stone>25</stone>
    3333    <metal>25</metal>
    3434  </Loot>
     35  <Market/>
    3536  <Obstruction>
    3637    <Static width="30.0" depth="26.0"/>
    3738  </Obstruction>
  • binaries/data/mods/public/simulation/templates/template_structure_military_dock.xml

     
    3434    <stone>0</stone>
    3535    <metal>0</metal>
    3636  </Loot>
     37  <Market/>
    3738  <Obstruction>
    3839    <Static width="18.0" depth="18.0"/>
    3940  </Obstruction>