Ticket #1907: corral.diff

File corral.diff, 7.8 KB (added by mimo, 8 years ago)
  • binaries/data/mods/public/simulation/components/GarrisonHolder.js

     
    1010        "</attribute>" +
    1111        "<text/>" +
    1212    "</element>" +
    13     "<element name='EjectHealth' a:help='Percentage of maximum health below which this holder no longer allows garrisoning'>" +
     13    "<element name='EjectHealth' a:help='Fraction of maximum health below which this holder no longer allows garrisoning'>" +
    1414        "<ref name='nonNegativeDecimal'/>" +
    1515    "</element>" +
    1616    "<element name='BuffHeal' a:help='Number of hit points that will be restored to this holder&apos;s garrisoned units each second'>" +
     
    1818    "</element>" +
    1919    "<element name='LoadingRange' a:help='The maximum distance from this holder at which entities are allowed to garrison. Should be about 2.0 for land entities and preferably greater for ships'>" +
    2020        "<ref name='nonNegativeDecimal'/>" +
    21     "</element>";
     21    "</element>" +
     22    "<optional>" +
     23        "<element name='FoodProduction' a:help='Fraction of food that will be produced by this holder&apos;s garrisoned units each second'>" +
     24            "<ref name='nonNegativeDecimal'/>" +
     25        "</element>" +
     26    "</optional>";
    2227
    2328/**
    2429 * Initialize GarrisonHolder Component
     
    2833    // Garrisoned Units
    2934    this.entities = [];
    3035    this.spaceOccupied = 0;
    31     this.timer = undefined;
     36    this.healTimer = undefined;
     37    this.foodTimer = undefined;
    3238};
    3339
    3440/**
     
    7581};
    7682
    7783/**
     84 * Get the heal rate with which garrisoned units will be healed
     85 */
     86GarrisonHolder.prototype.GetFoodRate = function()
     87{
     88    if(this.template.FoodProduction)
     89    {
     90        var foodRate = +this.template.FoodProduction;
     91        return ApplyTechModificationsToEntity("GarrisonHolder/FoodProduction", foodRate, this.entity);
     92    }
     93    else
     94        return 0;
     95};
     96
     97/**
    7898 * Get number of garrisoned units capable of shooting arrows
    7999 * Not necessarily archers
    80100 */
     
    113133/**
    114134 * Garrison a unit inside.
    115135 * Returns true if successful, false if not
    116  * The timer for AutoHeal is started here
     136 * The timers for AutoHeal and food production are started here
    117137 */
    118138GarrisonHolder.prototype.Garrison = function(entity)
    119139{
     
    130150    if (this.GetCapacity() < this.spaceOccupied + 1)
    131151        return false;
    132152
    133     if (!this.timer && this.GetHealRate() > 0)
     153    if (!this.healTimer && this.GetHealRate() > 0)
    134154    {
    135155        var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
    136         this.timer = cmpTimer.SetTimeout(this.entity, IID_GarrisonHolder, "HealTimeout", 1000, {});
     156        this.healTimer = cmpTimer.SetTimeout(this.entity, IID_GarrisonHolder, "HealTimeout", 1000, {});
    137157    }
    138158
     159    if (!this.foodTimer && this.GetFoodRate() > 0)
     160    {
     161        this.foodProduced = 0;
     162        var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
     163        this.foodTimer = cmpTimer.SetTimeout(this.entity, IID_GarrisonHolder, "FoodTimeout", 1000, {});
     164    }
     165
    139166    var cmpPosition = Engine.QueryInterface(entity, IID_Position);
    140167    if (cmpPosition)
    141168    {
     
    383410    if (this.entities.length == 0)
    384411    {
    385412        var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
    386         cmpTimer.CancelTimer(this.timer);
    387         this.timer = undefined;
     413        cmpTimer.CancelTimer(this.healTimer);
     414        this.healTimer = undefined;
    388415    }
    389416    else
    390417    {
     
    399426            }
    400427        }
    401428        var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
    402         this.timer = cmpTimer.SetTimeout(this.entity, IID_GarrisonHolder, "HealTimeout", 1000, {});
     429        this.healTimer = cmpTimer.SetTimeout(this.entity, IID_GarrisonHolder, "HealTimeout", 1000, {});
    403430    }
    404431};
    405432
     433/**
     434 * Called every second. Produce food (in corrals)
     435 */
     436GarrisonHolder.prototype.FoodTimeout = function(data)
     437{
     438    if (this.entities.length == 0)
     439    {
     440        var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
     441        cmpTimer.CancelTimer(this.foodTimer);
     442        this.foodTimer = undefined;
     443    }
     444    else
     445    {
     446        var totalFood = 0;
     447        for each (var entity in this.entities)
     448        {
     449            var cmpResourceSupply = Engine.QueryInterface(entity, IID_ResourceSupply);
     450            if (cmpResourceSupply && cmpResourceSupply.GetType().generic == "food")
     451                totalFood += cmpResourceSupply.GetMaxAmount();
     452        }
     453        this.foodProduced += Math.round(totalFood*this.GetFoodRate()*100000);
     454        if (this.foodProduced >= 100000)
     455        {
     456            var newFood = parseInt(this.foodProduced/100000);
     457            var cmpPlayer = QueryOwnerInterface(this.entity, IID_Player);
     458            cmpPlayer.AddResource("food", newFood);
     459            var cmpStatisticsTracker = QueryOwnerInterface(this.entity, IID_StatisticsTracker);
     460            if (cmpStatisticsTracker)
     461                cmpStatisticsTracker.IncreaseResourceGatheredCounter("food", newFood, "meat");
     462            this.foodProduced -= newFood*100000;
     463        }
     464        var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
     465        this.foodTimer = cmpTimer.SetTimeout(this.entity, IID_GarrisonHolder, "FoodTimeout", 1000, {});
     466    }
     467};
     468
    406469GarrisonHolder.prototype.UpdateGarrisonFlag = function()
    407470{
    408471    var cmpVisual = Engine.QueryInterface(this.entity, IID_Visual);
     
    422485 */
    423486GarrisonHolder.prototype.OnDestroy = function()
    424487{
    425     if (this.timer)
     488    if (this.healTimer)
    426489    {
    427490        var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
    428         cmpTimer.CancelTimer(this.timer);
     491        cmpTimer.CancelTimer(this.healTimer);
    429492    }
     493    if (this.foodTimer)
     494    {
     495        var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
     496        cmpTimer.CancelTimer(this.foodTimer);
     497    }
    430498};
    431499
    432500/**
  • binaries/data/mods/public/simulation/components/UnitAI.js

     
    599599
    600600        if (this.MoveToTarget(this.order.data.target))
    601601        {
    602             this.SetNextState("INDIVIDUAL.GARRISON.APPROACHING");
     602            if (this.IsAnimal())
     603                this.SetNextState("ANIMAL.GARRISON.APPROACHING");
     604            else
     605                this.SetNextState("INDIVIDUAL.GARRISON.APPROACHING");
    603606        }
    604607        else
    605608        {
    606609            // We do a range check before actually garrisoning
    607610            this.StopMoving();
    608             this.SetNextState("INDIVIDUAL.GARRISON.GARRISONED");
     611            if (this.IsAnimal())
     612                this.SetNextState("ANIMAL.GARRISON.GARRISONED");
     613            else
     614                this.SetNextState("INDIVIDUAL.GARRISON.GARRISONED");
    609615        }
    610616    },
    611617
     
    24292435       
    24302436        "WALKING": "INDIVIDUAL.WALKING",    // reuse the same walking behaviour for animals
    24312437                            // only used for domestic animals
     2438
     2439        "GARRISON": "INDIVIDUAL.GARRISON",  // reuse the same garrison behaviour for animals
     2440                            // only used for domestic animals
     2441
    24322442    },
    24332443};
    24342444
     
    42174227    if (!cmpOwnership || !(IsOwnedByPlayer(cmpOwnership.GetOwner(), target) || IsOwnedByMutualAllyOfPlayer(cmpOwnership.GetOwner(), target)))
    42184228        return false;
    42194229
    4220     // Don't let animals garrison for now
    4221     // (If we want to support that, we'll need to change Order.Garrison so it
    4222     // doesn't move the animal into an INVIDIDUAL.* state)
    4223     if (this.IsAnimal())
     4230    // Don't let non domestic animals garrison for now
     4231    if (this.IsAnimal() && !this.IsDomestic())
    42244232        return false;
    42254233
    42264234    return true;
  • binaries/data/mods/public/simulation/templates/template_structure_resource_corral.xml

     
    1212  </Footprint>
    1313  <GarrisonHolder>
    1414    <Max>10</Max>
    15     <EjectHealth>0.1</EjectHealth>
     15    <EjectHealth>0.7</EjectHealth>
    1616    <List datatype="tokens">Animal</List>
    1717    <BuffHeal>1</BuffHeal>
    1818    <LoadingRange>2</LoadingRange>
     19    <FoodProduction>0.002</FoodProduction>
    1920  </GarrisonHolder>
    2021  <Health>
    2122    <Max>500</Max>