Ticket #537: Repair.diff

File Repair.diff, 5.4 KB (added by Evans, 14 years ago)
  • binaries/data/mods/public/gui/session_new/input.js

     
    128128            {
    129129                return {"type": "build", "cursor": "action-build", "target": targets[0]};
    130130            }
     131            else if (entState.buildEntities && targetState.needsRepair && playerOwned)
     132            {
     133                return {"type": "build", "cursor": "action-build", "target": targets[0]};
     134            }
    131135            else if (entState.attack && enemyOwned)
    132136            {
    133137                return {"type": "attack", "cursor": "action-attack", "target": targets[0]};
  • binaries/data/mods/public/simulation/components/Health.js

     
    4242    // Default to <Initial>, but use <Max> if it's undefined or zero
    4343    // (Allowing 0 initial HP would break our death detection code)
    4444    this.hitpoints = +(this.template.Initial || this.GetMaxHitpoints());
     45    this.repairProgress = this.hitpoints / this.GetMaxHitpoints();
    4546};
    4647
    4748//// Interface functions ////
     
    6566    this.hitpoints = Math.max(1, Math.min(this.GetMaxHitpoints(), value));
    6667};
    6768
     69Health.prototype.IsRepairable = function()
     70{
     71    if (this.template.Repairable)
     72        return this.template.Repairable;
     73    else
     74        return false;
     75};
     76
    6877Health.prototype.Kill = function()
    6978{
    7079    this.Reduce(this.hitpoints);
     
    132141    cmpCorpseVisual.SelectAnimation("death", true, 1.0, "");
    133142};
    134143
     144Health.prototype.Repair = function(builderEnt, work)
     145{
     146    // Do nothing if we've already finished building
     147    this.repairProgress = this.GetHitpoints() / this.GetMaxHitpoints();
    135148
     149    if (this.repairProgress == 1.0)
     150        return;
     151
     152    // Calculate the amount of progress that will be added (where 1.0 = completion)
     153    var cmpCost = Engine.QueryInterface(this.entity, IID_Cost);
     154    var amount = work / cmpCost.GetBuildTime();
     155
     156    // TODO: implement some kind of diminishing returns for multiple repairmen.
     157    // e.g. record the set of entities that build this, then every ~2 seconds
     158    // count them (and reset the list), and apply some function to the count to get
     159    // a factor, and apply that factor here.
     160    this.repairProgress += amount;
     161    if (this.repairProgress > 1.0)
     162        this.repairProgress = 1.0;
     163
     164    // Add an appropriate proportion of hitpoints
     165    var targetHP = Math.max(0, Math.min(this.GetMaxHitpoints(), Math.floor(this.GetMaxHitpoints() * this.repairProgress)));
     166    var deltaHP = targetHP - this.hitpoints;
     167    if (deltaHP > 0)
     168    {
     169        var cmpHealth = Engine.QueryInterface(this.entity, IID_Health);
     170        cmpHealth.Increase(deltaHP);
     171    }
     172
     173    if (this.repairProgress >= 1.0)
     174    {
     175        Engine.PostMessage(this.entity, MT_ConstructionFinished,
     176                { "entity": this.entity, "newentity": this.entity });
     177    }
     178};
    136179Engine.RegisterComponentType(IID_Health, "Health", Health);
  • binaries/data/mods/public/simulation/components/GuiInterface.js

     
    7676    {
    7777        ret.hitpoints = cmpHealth.GetHitpoints();
    7878        ret.maxHitpoints = cmpHealth.GetMaxHitpoints();
     79        ret.needsRepair = cmpHealth.IsRepairable() && (cmpHealth.GetHitpoints() < cmpHealth.GetMaxHitpoints());
    7980    }
    8081
    8182    var cmpAttack = Engine.QueryInterface(ent, IID_Attack);
  • binaries/data/mods/public/simulation/components/Player.js

     
    1010    this.civ = "gaia";
    1111    this.colour = { "r": 0.0, "g": 0.0, "b": 0.0, "a": 1.0 };
    1212    this.popCount = 0;
    13     this.popLimit = 50;
     13    this.popLimit = 0;
     14    this.popReserve = 0;
    1415    this.resourceCount = {
    1516        "food": 2000,   
    1617        "wood": 1500,   
     
    5455    return this.colour;
    5556};
    5657
     58Player.prototype.ReservePopulationSlots = function(num)
     59{
     60    this.popReserve += num;
     61};
     62
     63Player.prototype.UnReservePopulationSlots = function(num)
     64{
     65    this.popReserve -= Math.min(num, this.popReserve);
     66}
     67
    5768Player.prototype.GetPopulationCount = function()
    5869{
    59     return this.popCount;
     70    return this.popCount + this.popReserve;
    6071};
    6172
    6273Player.prototype.GetPopulationLimit = function()
  • binaries/data/mods/public/simulation/components/Builder.js

     
    5151
    5252    // If it's a foundation, then build it
    5353    var cmpFoundation = Engine.QueryInterface(target, IID_Foundation);
     54    var cmpHealth = Engine.QueryInterface(target, IID_Health)
    5455    if (cmpFoundation)
    5556    {
    5657        cmpFoundation.Build(this.entity, rate);
    5758        return { "finished": false };
    5859    }
    59     else
     60    else if (cmpHealth)
    6061    {
    61         // TODO: do some kind of repairing
    62 
    63         return { "finished": true };
     62        return { "finished": cmpHealth.Repair(this.entity, rate) };
    6463    }
     64    return { "finished": false };
    6565};
    6666
    6767Engine.RegisterComponentType(IID_Builder, "Builder", Builder);