Ticket #2413: ResourceTrickle.patch

File ResourceTrickle.patch, 5.5 KB (added by O.Davoodi, 10 years ago)
  • components/interfaces/ResourceTrickle.js

     
     1Engine.RegisterInterface("ResourceTrickle");
  • components/ResourceTrickle.js

     
     1function ResourceTrickle() {}
     2
     3ResourceTrickle.prototype.Schema =
     4    "<a:help>Controls the resource trickle ability of the unit.</a:help>" +
     5    "<element name='FoodRate' a:help='Food given to the player every interval'>" +
     6        "<ref name='nonNegativeDecimal'/>" +
     7    "</element>" +
     8    "<element name='WoodRate' a:help='Wood given to the player every interval'>" +
     9        "<ref name='nonNegativeDecimal'/>" +
     10    "</element>" +
     11    "<element name='StoneRate' a:help='Stone given to the player every interval'>" +
     12        "<ref name='nonNegativeDecimal'/>" +
     13    "</element>" +
     14    "<element name='MetalRate' a:help='Metal given to the player every interval'>" +
     15        "<ref name='nonNegativeDecimal'/>" +
     16    "</element>" +
     17    "<element name='Interval' a:help='Number of miliseconds must pass for the player to gain the next trickle.'>" +
     18        "<ref name='nonNegativeDecimal'/>" +
     19    "</element>";
     20
     21ResourceTrickle.prototype.Init = function()
     22{
     23    // We can't add float values to player's resources. So we put the reminder of the float value here.
     24    // For example, if our food rate is 1.5, in the first interval it gives 1 point of food to the player
     25    // and puts the rest (0.5) here. In the next interval, the value here is added to the rate, which means
     26    // 0.5 + 1.5 = 2.0 . So 2 points of food is added to the player and the rest (0.0) is put here and so on.
     27    this.store = { "food": 0.0, "wood": 0.0, "stone": 0.0, "metal": 0.0 };
     28   
     29    // Call the timer
     30    var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
     31    cmpTimer.SetInterval(this.entity, IID_ResourceTrickle, "Trickle", this.GetTimer(), this.GetTimer(), undefined)
     32};
     33
     34ResourceTrickle.prototype.GetTimer = function()
     35{
     36    var interval = +this.template.Interval;
     37
     38    interval = ApplyValueModificationsToEntity("ResourceTrickle/Interval", interval, this.entity);
     39   
     40    return interval;
     41};
     42
     43ResourceTrickle.prototype.GetRates = function()
     44{
     45    var foodrate = +this.template.FoodRate;
     46    var woodrate = +this.template.WoodRate;
     47    var stonerate = +this.template.StoneRate;
     48    var metalrate = +this.template.MetalRate;
     49
     50    foodrate = ApplyValueModificationsToEntity("ResourceTrickle/FoodRate", foodrate, this.entity);
     51    woodrate = ApplyValueModificationsToEntity("ResourceTrickle/WoodRate", woodrate, this.entity);
     52    stonerate = ApplyValueModificationsToEntity("ResourceTrickle/StoneRate", stonerate, this.entity);
     53    metalrate = ApplyValueModificationsToEntity("ResourceTrickle/MetalRate", metalrate, this.entity);
     54   
     55    return [ { "type": "food", "rate": foodrate },
     56            { "type": "wood", "rate": woodrate },
     57            { "type": "stone", "rate": stonerate },
     58            { "type": "metal", "rate": metalrate } ];
     59};
     60
     61// Do the actual work here
     62ResourceTrickle.prototype.Trickle = function(data, lateness)
     63{
     64    // Get the rates
     65    var rates = this.GetRates();
     66   
     67    // Get the player
     68    var cmpPlayer = QueryOwnerInterface(this.entity, IID_Player);
     69   
     70    for each (var resource in rates)
     71    {
     72        // Always add an integer to the player's treasury. Explained fully in the init() function
     73        this.store[resource.type] += resource.rate;
     74        if (this.store[resource.type] >= 1.0)
     75        {
     76            var commit = Math.floor(this.store[resource.type]); // The ammount that is going to be committed to the player
     77            cmpPlayer.AddResource(resource.type, commit);
     78            this.store[resource.type] -= commit; // Store the reminder
     79        }
     80    }
     81   
     82};
     83
     84Engine.RegisterComponentType(IID_ResourceTrickle, "ResourceTrickle", ResourceTrickle);
  • templates/structures/pers_apadana.xml

     
    3131    </Classes>
    3232    <History>The term Apadana designates a large hypostyle palace found in Persia. The best known example, and by far the largest, was the great Apadana at Persepolis. Functioning as the empire's central audience hall, the palace is famous for the reliefs of the tribute-bearers and of the army, including the Immortals. The annual tribute that the Persians received from their satrapies and vassal states, as regularised by Darius the Great, accounted for incredible annual revenue.</History>
    3333    <Icon>structures/palace.png</Icon>
    34     <Tooltip>"Satrapy Tribute": Gain a trickle of food, wood, stone, and metal resources (not implemented yet). Train Persian heroes and their "Immortals" bodyguards. Build Limit: 1.</Tooltip>
     34    <Tooltip>"Satrapy Tribute": Gain a trickle of food, wood, stone, and metal resources. Train Persian heroes and their "Immortals" bodyguards. Build Limit: 1.</Tooltip>
    3535  </Identity>
    3636  <Obstruction>
    3737    <Static width="30.0" depth="30.0"/>
     
    5252      persians/immortals
    5353    </Technologies>
    5454  </ProductionQueue>
     55  <ResourceTrickle>
     56    <FoodRate>1.0</FoodRate>
     57    <WoodRate>1.0</WoodRate>
     58    <StoneRate>0.75</StoneRate>
     59    <MetalRate>0.75</MetalRate>
     60    <Interval>1000</Interval>
     61  </ResourceTrickle>
    5562  <VisualActor>
    5663    <Actor>structures/persians/sb1_new.xml</Actor>
    5764    <FoundationActor>structures/fndn_6x6.xml</FoundationActor>