Ticket #3747: 3747_alternate_v1-1.patch

File 3747_alternate_v1-1.patch, 9.4 KB (added by s0600204, 8 years ago)

With style changes as suggested by elexis. Still requires #3993.

  • binaries/data/mods/public/globalscripts/Technologies.js

    diff --git a/binaries/data/mods/public/globalscripts/Technologies.js b/binaries/data/mods/public/globalscripts/Technologies.js
    index b6783e2..e00fa4e 100644
    a b function GetTechModifiedProperty(currentTechModifications, classes, propertyName  
    4444}
    4545
    4646/**
     47 * Derives modifications (to be applied to entities) from a given technology
     48 *
     49 * @param techTemplate The technology template to derive the modifications from.
     50 * @return Object containing the relevant modifications.
     51 */
     52function DeriveModificationsFromTech(techTemplate)
     53{
     54    if (!techTemplate.modifications)
     55        return {};
     56
     57    let techMods = {};
     58    let techAffects = [];
     59    if (techTemplate.affects && techTemplate.affects.length)
     60        for (let affected of techTemplate.affects)
     61            techAffects.push(affected.split(/\s+/));
     62    else
     63        techAffects.push([]);
     64
     65    for (let mod of techTemplate.modifications)
     66    {
     67        let affects = techAffects.slice();
     68        if (mod.affects)
     69        {
     70            let specAffects = mod.affects.split(/\s+/);
     71            for (let a in affects)
     72                affects[a] = affects[a].concat(specAffects);
     73        }
     74
     75        let newModifier = {
     76            "affects": affects
     77        };
     78        for (let idx in mod)
     79            if (idx !== "value" && idx !== "affects")
     80                newModifier[idx] = mod[idx];
     81
     82        if (!techMods[mod.value])
     83            techMods[mod.value] = [];
     84        techMods[mod.value].push(newModifier);
     85    }
     86    return techMods;
     87}
     88
     89/**
    4790 * Returns whether the given modification applies to the entity containing the given class list
    4891 */
    4992function DoesModificationApply(modification, classes)
  • binaries/data/mods/public/globalscripts/Templates.js

    diff --git a/binaries/data/mods/public/globalscripts/Templates.js b/binaries/data/mods/public/globalscripts/Templates.js
    index e50cbe4..da25a23 100644
    a b function GetTemplateDataHelper(template, player, auraTemplates)  
    7979    var func;
    8080    if (player)
    8181        func = ApplyValueModificationsToTemplate;
     82    else if (g_CurrentModifiers)
     83        func = function(property, val, c, template) {
     84            return GetTechModifiedProperty(g_CurrentModifiers, GetIdentityClasses(template.Identity), property, val);
     85        }
    8286    else
    8387        func = function(a, val, c, d) { return val; }
    8488
    function GetTemplateDataHelper(template, player, auraTemplates)  
    283287}
    284288
    285289/**
    286  * Get information about a technology template.
     290 * Get basic information about a technology template.
    287291 * @param template A valid template as obtained by loading the tech JSON file.
    288  * @param civ Civilization for which the specific name should be returned.
    289292 */
    290 function GetTechnologyDataHelper(template, civ)
     293function GetTechnologyBasicDataHelper(template)
    291294{
    292     var ret = {};
    293 
    294     // Get specific name for this civ or else the generic specific name
    295     var specific = undefined;
    296     if (template.specificName)
    297     {
    298         if (template.specificName[civ])
    299             specific = template.specificName[civ];
    300         else
    301             specific = template.specificName['generic'];
    302     }
     295    let ret = {};
    303296
    304297    ret.name = {
    305         "specific": specific,
    306298        "generic": template.genericName,
    307299    };
    308300
    function GetTechnologyDataHelper(template, civ)  
    310302        ret.icon = "technologies/" + template.icon;
    311303    else
    312304        ret.icon = null;
     305
     306    ret.description = template.description;
     307
     308    return ret;
     309}
     310
     311/**
     312 * Get information about a technology template.
     313 * @param template A valid template as obtained by loading the tech JSON file.
     314 * @param civ Civilization for which the specific name should be returned.
     315 */
     316function GetTechnologyDataHelper(template, civ)
     317{
     318    let ret = GetTechnologyBasicDataHelper(template);
     319
     320    if (template.specificName)
     321        ret.name.specific = template.specificName[civ] || template.specificName['generic'];
     322
    313323    ret.cost = {
    314324        "food": template.cost ? (+template.cost.food) : 0,
    315325        "wood": template.cost ? (+template.cost.wood) : 0,
    function GetTechnologyDataHelper(template, civ)  
    327337    if (template.requirements && template.requirements.class)
    328338        ret.classRequirements = {"class": template.requirements.class, "number": template.requirements.number};
    329339
    330     ret.description = template.description;
    331 
    332340    return ret;
    333341}
  • binaries/data/mods/public/gui/structree/helper.js

    diff --git a/binaries/data/mods/public/gui/structree/helper.js b/binaries/data/mods/public/gui/structree/helper.js
    index 8eb8bd3..e0edaa6 100644
    a b function loadTechData(templateName)  
    2424{
    2525    if (!(templateName in g_TechnologyData))
    2626    {
    27         var filename = "simulation/data/technologies/" + templateName + ".json";
    28         var data = Engine.ReadJSONFile(filename);
    29         translateObjectKeys(data, ["genericName", "tooltip"]);
    30        
     27        let filename = "simulation/data/technologies/" + templateName + ".json";
     28        let data = Engine.ReadJSONFile(filename);
     29        translateObjectKeys(data, ["genericName", "tooltip", "description"]);
     30
    3131        g_TechnologyData[templateName] = data;
    3232    }
    3333
    function loadAuraData(templateName)  
    4848    return g_AuraData[templateName];
    4949}
    5050
     51function loadAllAutoResearchedTechs()
     52{
     53    let path = "simulation/data/technologies/";
     54    let techFiles = Engine.BuildDirEntList(path, "*.json", true);
     55
     56    for (let filename of techFiles)
     57    {
     58        let templateName = filename.slice(path.length, -5);
     59        let data = loadTechData(templateName);
     60
     61        if (!data || !data.autoResearch)
     62            continue;
     63
     64        let tech = GetTechnologyBasicDataHelper(data);
     65
     66        tech.modifications = data.modifications;
     67        tech.affects = data.affects;
     68
     69        if (data.requirements !== undefined)
     70        {
     71            let op = Object.keys(data.requirements)[0];
     72            let val = data.requirements[op];
     73            tech.reqs = calcReqs(op, val);
     74        }
     75
     76        g_ParsedData.modifiers[templateName] = tech;
     77    }
     78}
     79
    5180/**
    5281 * Fetch a value from an entity's template
    5382 *
  • binaries/data/mods/public/gui/structree/structree.js

    diff --git a/binaries/data/mods/public/gui/structree/structree.js b/binaries/data/mods/public/gui/structree/structree.js
    index 1415022..509ffce 100644
    a b var g_ParsedData = {  
    22    "units": {},
    33    "structures": {},
    44    "techs": {},
    5     "phases": {}
     5    "phases": {},
     6    "modifiers": {}
    67};
    78
    89var g_Lists = {};
    910var g_CivData = {};
    1011var g_SelectedCiv = "";
     12var g_CurrentModifiers = {};
    1113var g_CallbackSet = false;
    1214
    1315/**
    function init(data = {})  
    2527    if (!civList.length)
    2628        return;
    2729
     30    loadAllAutoResearchedTechs();
     31
    2832    var civSelection = Engine.GetGUIObjectByName("civSelection");
    2933    civSelection.list = civList.map(c => c.name);
    3034    civSelection.list_data = civList.map(c => c.code);
    function selectCiv(civCode)  
    5559        return;
    5660    }
    5761
     62    // Derive modifications
     63    g_CurrentModifiers = {};
     64    for (let modifier in g_ParsedData.modifiers)
     65    {
     66        modifier = g_ParsedData.modifiers[modifier];
     67        if (modifier.reqs && !modifier.reqs[g_SelectedCiv])
     68            continue;
     69
     70        let derivedModifiers = DeriveModificationsFromTech(modifier);
     71        for (let modPath in derivedModifiers)
     72        {
     73            if (!g_CurrentModifiers[modPath])
     74                g_CurrentModifiers[modPath] = [];
     75            g_CurrentModifiers[modPath] = g_CurrentModifiers[modPath].concat(derivedModifiers[modPath]);
     76        }
     77    }
     78
    5879    g_Lists = {
    5980        "units": [],
    6081        "structures": [],
  • binaries/data/mods/public/simulation/components/TechnologyManager.js

    diff --git a/binaries/data/mods/public/simulation/components/TechnologyManager.js b/binaries/data/mods/public/simulation/components/TechnologyManager.js
    index f1f9cc7..ad8ff00 100644
    a b TechnologyManager.prototype.ResearchTechnology = function(tech)  
    267267    // store the modifications in an easy to access structure
    268268    if (template.modifications)
    269269    {
    270         var affects = [];
    271         if (template.affects && template.affects.length > 0)
     270        let derivedModifiers = DeriveModificationsFromTech(template);
     271        for (let modifierPath in derivedModifiers)
    272272        {
    273             for (let affect of template.affects)
    274             {
    275                 // Put the list of classes into an array for convenient access
    276                 affects.push(affect.split(/\s+/));
    277             }
    278         }
    279         else
    280         {
    281             affects.push([]);
    282         }
    283 
    284         // We add an item to this.modifications for every modification in the template.modifications array
    285         for (var i in template.modifications)
    286         {
    287             var modification = template.modifications[i];
    288             if (!this.modifications[modification.value])
    289                 this.modifications[modification.value] = [];
    290            
    291             var modAffects = affects.slice();
    292             if (modification.affects)
    293             {
    294                 var extraAffects = modification.affects.split(/\s+/);
    295                 for (var a in modAffects)
    296                     modAffects[a] = modAffects[a].concat(extraAffects);
    297             }
    298 
    299             var mod = {"affects": modAffects};
    300 
    301             // copy the modification data into our new data structure
    302             for (var j in modification)
    303                 if (j !== "value" && j !== "affects")
    304                     mod[j] = modification[j];
     273            if (!this.modifications[modifierPath])
     274                this.modifications[modifierPath] = [];
     275            this.modifications[modifierPath] = this.modifications[modifierPath].concat(derivedModifiers[modifierPath]);
    305276
    306             this.modifications[modification.value].push(mod);
    307             var component = modification.value.split("/")[0];
     277            let component = modifierPath.split("/")[0];
    308278            if (!modifiedComponents[component])
    309279                modifiedComponents[component] = [];
    310             modifiedComponents[component].push(modification.value);
    311             this.modificationCache[modification.value] = {};
     280            modifiedComponents[component].push(modifierPath);
     281            this.modificationCache[modifierPath] = {};
    312282        }
    313283    }
    314284