Ticket #633: repair-v4.diff

File repair-v4.diff, 19.3 KB (added by mimo, 9 years ago)

some cleanings + add number of repairing units in gui

  • binaries/data/mods/public/gui/session/selection_details.js

     
    201201        else
    202202            Engine.GetGUIObjectByName("resourceCarryingIcon").tooltip = translate("Number of builders.");
    203203    }
     204    else if (entState.repairable && entState.repairable.numBuilders > 0 && entState.visibility == "visible")
     205    {
     206        Engine.GetGUIObjectByName("resourceCarryingIcon").hidden = false;
     207        Engine.GetGUIObjectByName("resourceCarryingText").hidden = false;
     208        Engine.GetGUIObjectByName("resourceCarryingIcon").sprite = "stretched:session/icons/repair.png";
     209        Engine.GetGUIObjectByName("resourceCarryingText").caption = entState.repairable.numBuilders + "    ";
     210        Engine.GetGUIObjectByName("resourceCarryingIcon").tooltip = translate("Number of builders.");
     211    }
    204212    else if (entState.resourceSupply && (!entState.resourceSupply.killBeforeGather || !entState.hitpoints) && entState.visibility == "visible")
    205213    {
    206214        Engine.GetGUIObjectByName("resourceCarryingIcon").hidden = false;
  • binaries/data/mods/public/simulation/components/Builder.js

     
    6464 */
    6565Builder.prototype.PerformBuilding = function(target)
    6666{
    67     var rate = ApplyValueModificationsToEntity("Builder/Rate", +this.template.Rate, this.entity);
     67    let rate = ApplyValueModificationsToEntity("Builder/Rate", +this.template.Rate, this.entity);
    6868
    6969    // If it's a foundation, then build it
    70     var cmpFoundation = Engine.QueryInterface(target, IID_Foundation);
     70    let cmpFoundation = Engine.QueryInterface(target, IID_Foundation);
    7171    if (cmpFoundation)
    7272    {
    7373        cmpFoundation.Build(this.entity, rate);
     
    7575    }
    7676
    7777    // Otherwise try to repair it
    78     var cmpHealth = Engine.QueryInterface(target, IID_Health);
    79     if (cmpHealth)
     78    let cmpRepairable = Engine.QueryInterface(target, IID_Repairable);
     79    if (cmpRepairable)
    8080    {
    81         cmpHealth.Repair(this.entity, rate);
     81        cmpRepairable.Repair(this.entity, rate);
    8282        return;
    8383    }
    8484};
  • binaries/data/mods/public/simulation/components/Foundation.js

     
    142142 */
    143143Foundation.prototype.SetBuildMultiplier = function()
    144144{
    145     var numBuilders = this.builders.length;
    146     if (numBuilders == 0)
     145    let numBuilders = this.builders.length;
     146    if (numBuilders < 2)
    147147        this.buildMultiplier = 1;
    148148    else
    149149        this.buildMultiplier = Math.pow(numBuilders, 0.7) / numBuilders;
  • binaries/data/mods/public/simulation/components/GuiInterface.js

     
    333333        };
    334334    }
    335335
     336    var cmpRepairable = QueryMiragedInterface(ent, IID_Repairable);
     337    if (cmpRepairable)
     338    {
     339        ret.repairable = {
     340            "numBuilders": cmpRepairable.GetNumBuilders()
     341        };
     342    }
     343   
    336344    var cmpOwnership = Engine.QueryInterface(ent, IID_Ownership);
    337345    if (cmpOwnership)
    338346    {
  • binaries/data/mods/public/simulation/components/Health.js

     
    3232    "</element>" +
    3333    "<element name='Unhealable' a:help='Indicates that the entity can not be healed by healer units'>" +
    3434        "<data type='boolean'/>" +
    35     "</element>" +
    36     "<element name='Repairable' a:help='Indicates that the entity can be repaired by builder units'>" +
    37         "<data type='boolean'/>" +
    3835    "</element>";
    3936
    4037Health.prototype.Init = function()
     
    9289
    9390Health.prototype.IsRepairable = function()
    9491{
    95     return (this.template.Repairable == "true");
     92    return (Engine.QueryInterface(this.entity, IID_Repairable) != null);
    9693};
    9794
    9895Health.prototype.IsUnhealable = function()
     
    315312    return spawnedEntity;
    316313};
    317314
    318 Health.prototype.Repair = function(builderEnt, work)
    319 {
    320     var damage = this.GetMaxHitpoints() - this.GetHitpoints();
    321 
    322     // Do nothing if we're already at full hitpoints
    323     if (damage <= 0)
    324         return;
    325 
    326     // Calculate the amount of hitpoints that will be added
    327     // TODO: what computation should we use?
    328     // TODO: should we do some diminishing returns thing? (see Foundation.Build)
    329     var amount = Math.min(damage, work);
    330 
    331     // TODO: resource costs?
    332 
    333     // Add hitpoints
    334     this.Increase(amount);
    335 
    336     // If we repaired all the damage, send a message to entities
    337     // to stop repairing this building
    338     if (amount >= damage)
    339     {
    340         Engine.PostMessage(this.entity, MT_ConstructionFinished,
    341             { "entity": this.entity, "newentity": this.entity });
    342     }
    343 };
    344 
    345315Health.prototype.OnValueModification = function(msg)
    346316{
    347317    if (msg.component != "Health")
  • binaries/data/mods/public/simulation/components/Repairable.js

     
     1function Repairable() {}
     2
     3Repairable.prototype.Schema =
     4    "<a:help>Deals with repairable structures and units.</a:help>" +
     5    "<a:example>" +
     6        "<RepairTimeRatio>2.0</RepairTimeRatio>" +
     7    "</a:example>" +
     8    "<element name='RepairTimeRatio' a:help='repair time ratio relative to building (or production) time.'>" +
     9        "<ref name='positiveDecimal'/>" +
     10    "</element>";
     11
     12Repairable.prototype.Init = function()
     13{
     14    this.builders = []; // builder entities
     15    this.buildMultiplier = 1; // Multiplier for the amount of work builders do.
     16    this.repairTimeRatio = +this.template.RepairTimeRatio;
     17};
     18
     19Repairable.prototype.GetNumBuilders = function()
     20{
     21    return this.builders.length;
     22};
     23
     24Repairable.prototype.AddBuilder = function(builderEnt)
     25{
     26    if (this.builders.indexOf(builderEnt) !== -1)
     27        return;
     28    this.builders.push(builderEnt);
     29    this.SetBuildMultiplier();
     30};
     31
     32Repairable.prototype.RemoveBuilder = function(builderEnt)
     33{
     34    if (this.builders.indexOf(builderEnt) === -1)
     35        return;
     36    this.builders.splice(this.builders.indexOf(builderEnt), 1);
     37    this.SetBuildMultiplier();
     38 };
     39
     40/**
     41 * Sets the build rate multiplier, which is applied to all builders.
     42 * Yields a total rate of construction equal to numBuilders^0.7
     43 */
     44Repairable.prototype.SetBuildMultiplier = function()
     45{
     46    let numBuilders = this.builders.length;
     47    if (numBuilders < 2)
     48        this.buildMultiplier = 1;
     49    else
     50        this.buildMultiplier = Math.pow(numBuilders, 0.7) / numBuilders;
     51};
     52
     53// TODO: should we have resource costs?
     54Repairable.prototype.Repair = function(builderEnt, rate)
     55{
     56    let cmpHealth = Engine.QueryInterface(this.entity, IID_Health);
     57    let cmpCost = Engine.QueryInterface(this.entity, IID_Cost);
     58    if (!cmpHealth || !cmpCost)
     59        return;
     60    let damage = cmpHealth.GetMaxHitpoints() - cmpHealth.GetHitpoints();
     61    if (damage <= 0)
     62        return;
     63
     64    // Calculate the amount of hitpoints that will be added (using diminushing rate when several builders)
     65    let repairTime = this.repairTimeRatio * cmpCost.GetBuildTime();
     66    let work = repairTime ? rate * this.buildMultiplier * cmpHealth.GetMaxHitpoints() / repairTime : rate * this.buildMultiplier;
     67    let amount = Math.min(damage, work);
     68    cmpHealth.Increase(amount);
     69
     70    // If we repaired all the damage, send a message to entities to stop repairing this building
     71    if (amount >= damage)
     72        Engine.PostMessage(this.entity, MT_ConstructionFinished, { "entity": this.entity, "newentity": this.entity });
     73};
     74
     75Engine.RegisterComponentType(IID_Repairable, "Repairable", Repairable);
  • binaries/data/mods/public/simulation/components/UnitAI.js

     
    26152615                        return true;
    26162616                    }
    26172617
    2618                     var cmpFoundation = Engine.QueryInterface(this.repairTarget, IID_Foundation);
    2619                     if (cmpFoundation)
    2620                         cmpFoundation.AddBuilder(this.entity);
     2618                    let cmpBuilderList = QueryBuilderListInterface(this.repairTarget);
     2619                    if (cmpBuilderList)
     2620                        cmpBuilderList.AddBuilder(this.entity);
    26212621
    26222622                    this.SelectAnimation("build", false, 1.0, "build");
    26232623                    this.StartTimer(1000, 1000);
     
    26252625                },
    26262626
    26272627                "leave": function() {
    2628                     var cmpFoundation = Engine.QueryInterface(this.repairTarget, IID_Foundation);
    2629                     if (cmpFoundation)
    2630                         cmpFoundation.RemoveBuilder(this.entity);
     2628                    let cmpBuilderList = QueryBuilderListInterface(this.repairTarget);
     2629                    if (cmpBuilderList)
     2630                        cmpBuilderList.RemoveBuilder(this.entity);
    26312631                    delete this.repairTarget;
    26322632                    this.StopTimer();
    26332633                },
  • binaries/data/mods/public/simulation/components/interfaces/Repairable.js

     
     1Engine.RegisterInterface("Repairable");
  • binaries/data/mods/public/simulation/helpers/Player.js

     
    243243}
    244244
    245245/**
     246 * Similar to Engine.QueryInterface, but checks for all interfaces
     247 * implementing a builder list (currently Foundation and Repairable)
     248 */
     249function QueryBuilderListInterface(ent)
     250{
     251    return Engine.QueryInterface(ent, IID_Foundation) || Engine.QueryInterface(ent, IID_Repairable);
     252}
     253
     254/**
    246255 * Returns true if the entity 'target' is owned by an ally of
    247256 * the owner of 'entity'.
    248257 */
     
    400409Engine.RegisterGlobal("QueryOwnerInterface", QueryOwnerInterface);
    401410Engine.RegisterGlobal("QueryPlayerIDInterface", QueryPlayerIDInterface);
    402411Engine.RegisterGlobal("QueryMiragedInterface", QueryMiragedInterface);
     412Engine.RegisterGlobal("QueryBuilderListInterface", QueryBuilderListInterface);
    403413Engine.RegisterGlobal("IsOwnedByAllyOfEntity", IsOwnedByAllyOfEntity);
    404414Engine.RegisterGlobal("IsOwnedByMutualAllyOfEntity", IsOwnedByMutualAllyOfEntity);
    405415Engine.RegisterGlobal("IsOwnedByPlayer", IsOwnedByPlayer);
  • binaries/data/mods/public/simulation/templates/gaia/fauna_shark.xml

     
    11<?xml version="1.0" encoding="utf-8"?>
    22<Entity parent="template_unit_fauna_wild_passive">
    3     <Footprint replace="">
    4         <Square width="5" depth="8"/>
    5         <Height>7.5</Height>
    6     </Footprint>
    7     <Health>
    8         <Max>150</Max>
    9         <Unhealable>true</Unhealable>
    10         <Repairable>false</Repairable>
    11     </Health>
    12     <Identity>
    13         <Civ>gaia</Civ>
    14         <SpecificName>Great White</SpecificName>
    15         <GenericName>Shark</GenericName>
    16         <Classes datatype="tokens">SeaCreature</Classes>
    17         <Icon>gaia/fauna_fish.png</Icon>
    18     </Identity>
    19     <Minimap disable=""/>
    20     <Position>
    21         <Altitude>-1</Altitude>
    22         <Floating>true</Floating>
    23         <TurnRate>5.0</TurnRate>
    24     </Position>
    25     <Selectable>
    26         <Overlay>
    27             <Texture>
    28                 <MainTexture>circle/128x128.png</MainTexture>
    29                 <MainTextureMask>circle/128x128_mask.png</MainTextureMask>
    30             </Texture>
    31         </Overlay>
    32     </Selectable>
    33     <Visibility>
    34         <RetainInFog>false</RetainInFog>
    35     </Visibility>
    36     <Vision>
    37         <Range>100</Range>
    38     </Vision>
    39     <VisualActor>
    40         <Actor>fauna/shark.xml</Actor>
    41     </VisualActor>
    42     <UnitAI>
    43         <NaturalBehaviour>passive</NaturalBehaviour>
    44         <RoamDistance>100.0</RoamDistance>
    45         <FleeDistance>60.0</FleeDistance>
    46         <RoamTimeMin>100000</RoamTimeMin>
    47         <RoamTimeMax>300000</RoamTimeMax>
    48         <FeedTimeMin>1</FeedTimeMin>
    49         <FeedTimeMax>2</FeedTimeMax>
    50     </UnitAI>
    51     <UnitMotion>
    52         <PassabilityClass>ship-small</PassabilityClass>
    53         <WalkSpeed>4.0</WalkSpeed>
    54         <Run>
    55             <Speed>35.0</Speed>
    56         </Run>
    57     </UnitMotion>
     3  <Footprint replace="">
     4    <Square width="5" depth="8"/>
     5    <Height>7.5</Height>
     6  </Footprint>
     7  <Health>
     8    <Max>150</Max>
     9    <Unhealable>true</Unhealable>
     10  </Health>
     11  <Identity>
     12    <Civ>gaia</Civ>
     13    <SpecificName>Great White</SpecificName>
     14    <GenericName>Shark</GenericName>
     15    <Classes datatype="tokens">SeaCreature</Classes>
     16    <Icon>gaia/fauna_fish.png</Icon>
     17  </Identity>
     18  <Minimap disable=""/>
     19  <Position>
     20    <Altitude>-1</Altitude>
     21    <Floating>true</Floating>
     22    <TurnRate>5.0</TurnRate>
     23  </Position>
     24  <Selectable>
     25    <Overlay>
     26      <Texture>
     27        <MainTexture>circle/128x128.png</MainTexture>
     28        <MainTextureMask>circle/128x128_mask.png</MainTextureMask>
     29      </Texture>
     30    </Overlay>
     31  </Selectable>
     32  <Visibility>
     33    <RetainInFog>false</RetainInFog>
     34  </Visibility>
     35  <Vision>
     36    <Range>100</Range>
     37  </Vision>
     38  <VisualActor>
     39    <Actor>fauna/shark.xml</Actor>
     40  </VisualActor>
     41  <UnitAI>
     42    <NaturalBehaviour>passive</NaturalBehaviour>
     43    <RoamDistance>100.0</RoamDistance>
     44    <FleeDistance>60.0</FleeDistance>
     45    <RoamTimeMin>100000</RoamTimeMin>
     46    <RoamTimeMax>300000</RoamTimeMax>
     47    <FeedTimeMin>1</FeedTimeMin>
     48    <FeedTimeMax>2</FeedTimeMax>
     49  </UnitAI>
     50  <UnitMotion>
     51    <PassabilityClass>ship-small</PassabilityClass>
     52    <WalkSpeed>4.0</WalkSpeed>
     53    <Run>
     54      <Speed>35.0</Speed>
     55    </Run>
     56  </UnitMotion>
    5857</Entity>
  • binaries/data/mods/public/simulation/templates/other/plane.xml

     
    4141    <Formations datatype="tokens" replace=""/>
    4242  </Identity>
    4343  <Health>
    44     <Max>100</Max>
     44    <Max>100</Max>
    4545    <Unhealable>true</Unhealable>
    46     <Repairable>true</Repairable>
    4746  </Health>
     47  <Repairable>
     48    <RepairTimeRatio>3.0</RepairTimeRatio>
     49  </Repairable>
    4850  <Obstruction disable=""/>
    4951  <Position>
    5052    <TurnRate>1.0</TurnRate>
  • binaries/data/mods/public/simulation/templates/structures/rome_army_camp.xml

     
    5151  </GarrisonHolder>
    5252  <Health>
    5353    <Max>2500</Max>
    54     <Repairable>true</Repairable>
    5554    <SpawnEntityOnDeath>rubble/rubble_rome_sb</SpawnEntityOnDeath>
    5655  </Health>
    5756  <Identity>
     
    5958    <GenericName>Entrenched Army Camp</GenericName>
    6059    <SpecificName>Castrum Vallum</SpecificName>
    6160    <Classes datatype="tokens">
    62       ArmyCamp
    63     </Classes>
     61      ArmyCamp
     62    </Classes>
    6463    <Icon>structures/roman_camp.png</Icon>
    6564    <Tooltip>Build anywhere on the map, even in enemy territory. Construct siege weapons and train citizen-soldiers. Heal garrisoned units slowly.</Tooltip>
    6665    <History>Sometimes it was a temporary camp built facing the route by which the army is to march, other times a defensive or offensive (for sieges) structure. Within this gate the tents of the first centuries or cohorts are pitched, and the dragons (ensigns of cohorts) and other ensigns planted. The Decumane gate is directly opposite to the Praetorian in the rear of the camp, and through this the soldiers are conducted to the place appointed for punishment or execution.</History>
  • binaries/data/mods/public/simulation/templates/template_structure.xml

     
    4848    <DeathType>corpse</DeathType>
    4949    <RegenRate>0</RegenRate>
    5050    <Unhealable>true</Unhealable>
    51     <Repairable>true</Repairable>
    5251  </Health>
     52  <Repairable>
     53    <RepairTimeRatio>2.0</RepairTimeRatio>
     54  </Repairable>
    5355  <Identity>
    5456    <GenericName>Structure</GenericName>
    5557    <Classes datatype="tokens">Structure ConquestCritical</Classes>
  • binaries/data/mods/public/simulation/templates/template_unit.xml

     
    3232    <Max>100</Max>
    3333    <RegenRate>0</RegenRate>
    3434    <Unhealable>false</Unhealable>
    35     <Repairable>false</Repairable>
    3635  </Health>
    3736  <Identity>
    3837    <GenericName>Unit</GenericName>
    3938    <Classes datatype="tokens">Unit ConquestCritical</Classes>
    40         <Formations datatype="tokens">
    41             formations/null
    42         </Formations>
     39    <Formations datatype="tokens">
     40      formations/null
     41    </Formations>
    4342  </Identity>
    4443  <Looter/>
    4544  <Minimap>
  • binaries/data/mods/public/simulation/templates/template_unit_fauna_hunt_whale.xml

     
    88    <Max>200</Max>
    99    <DeathType>remain</DeathType>
    1010    <Unhealable>true</Unhealable>
    11     <Repairable>false</Repairable>
    1211  </Health>
    1312  <Identity>
    1413    <Classes datatype="tokens">SeaCreature</Classes>
    1514    <Civ>gaia</Civ>
    1615    <GenericName>Cetacean</GenericName>
    17     <Tooltip>Kill, then collect food from this bountiful oceanic resource.</Tooltip>
     16    <Tooltip>Kill, then collect food from this bountiful oceanic resource.</Tooltip>
    1817    <Icon>gaia/fauna_fish.png</Icon>
    1918  </Identity>
    2019  <Position>
    21     <Floating>true</Floating>
     20    <Floating>true</Floating>
    2221  </Position>
    2322  <ResourceSupply>
    2423    <KillBeforeGather>true</KillBeforeGather>
    2524    <Amount>2000</Amount>
    2625    <Type>food.fish</Type>
    27     <MaxGatherers>5</MaxGatherers>
     26    <MaxGatherers>5</MaxGatherers>
    2827  </ResourceSupply>
    2928  <Selectable>
    3029    <Overlay>
  • binaries/data/mods/public/simulation/templates/template_unit_mechanical.xml

     
    77  </Decay>
    88  <Health>
    99    <Unhealable>true</Unhealable>
    10     <Repairable>true</Repairable>
    1110  </Health>
     11  <Repairable>
     12    <RepairTimeRatio>4.0</RepairTimeRatio>
     13  </Repairable>
    1214  <Identity>
    1315    <GenericName>Mechanical</GenericName>
    1416    <VisibleClasses datatype="tokens">Mechanical</VisibleClasses>