Reading the fabrication rate and energy consumptions of different unit types?

Discussion in 'Mod Discussions' started by ORFJackal, December 19, 2013.

  1. ORFJackal

    ORFJackal Active Member

    Messages:
    287
    Likes Received:
    248
    I've started creating a mod that will show the total fabrication rate and energy consumption of the selected units. This is to help me estimate whether my economy can handle it when I put some fabricators working, hopefully resulting in a more efficient economy :)

    Is there an easy way to get the Fabrication Rate and Energy Consumption as reported in http://planetaryannihilation.gamepedia.com/Commander and for the other units? I've figured out only two hacks for getting that information:

    (1) Monitor the reported consumption of the selected units (or was it reported only when hovering the mouse over them?) to find out the min/max values of each unit type's production and consumption. Complicated and the values will be known only after building something with the unit type.

    (2) Read the pages from the wiki with ajax and parse the information from the page. Slow, fragile and might not always be in sync with the real game.
    Last edited: December 19, 2013
  2. cola_colin

    cola_colin Moderator Alumni

    Messages:
    12,074
    Likes Received:
    16,221
    The information from pages like this is based on information you can find as json in PA/media/pa/units
    I think those files are not directly available to the UI however so you might want to write a program that automatically parses them and prints out a bit of json that you can include in your mod.

    ... or find out how to access them dunno
  3. proeleert

    proeleert Post Master General

    Messages:
    1,681
    Likes Received:
    1,656
    If you find out how to access them please let me know :)
    I want to read some basic data from units myself.
    stormingkiwi likes this.
  4. dfanz0r

    dfanz0r Active Member

    Messages:
    150
    Likes Received:
    47
    Refer to the json files directly with the coui:// address instead of using a relative path. It works flawlessly :)
    Last edited: December 21, 2013
  5. Clopse

    Clopse Post Master General

    Messages:
    2,535
    Likes Received:
    2,865
    If memory serves me correct Yarmond ( the person who made pa-db) gave us links to his work that makes these Jsons into easily readable format. Don't think he had posted much more so should be an easy find. Yarmond.database.python.github should do it.

    There are not so many units which fabricate and their numbers remain a constant so it shouldn't be too hard to compile this list.
  6. proeleert

    proeleert Post Master General

    Messages:
    1,681
    Likes Received:
    1,656
    Seriously oh man this I gotta check out :)
  7. proeleert

    proeleert Post Master General

    Messages:
    1,681
    Likes Received:
    1,656
    I can read those files fine.
    But has somebody figured out how to know if a Unit/Building is buildable or not ?
    Cause Megabot for example is not Avatar Factory is not. ...
  8. Raevn

    Raevn Moderator Alumni

    Messages:
    4,226
    Likes Received:
    4,324
    There's a file (I think it's in the units directory) which is a list of json files for buildable units.

    EDIT: unit_list.json in PA\media\pa\units
    Last edited: December 22, 2013
  9. proeleert

    proeleert Post Master General

    Messages:
    1,681
    Likes Received:
    1,656
    No megabot is in there
  10. Raevn

    Raevn Moderator Alumni

    Messages:
    4,226
    Likes Received:
    4,324
    It may be in there, but no factory/constructure unit has the appropriate "buildable_types" value to be able to build it.
  11. stuart98

    stuart98 Post Master General

    Messages:
    6,009
    Likes Received:
    3,888
    Out of curiosity raevn, do the unbuildable things have models? If so, would you post them?
  12. Raevn

    Raevn Moderator Alumni

    Messages:
    4,226
    Likes Received:
    4,324
    Teleporter:
    https://forums.uberent.com/threads/teleporters.54939/

    Unit Cannon:
    Unit Cannon.PNG

    Gunship:
    Gunship.PNG

    Tank Base Commander:
    Tank Commander.PNG

    Megabot:
    Megabot.PNG

    Plus some old models aren't available any more:

    Beast Commander:
    Beast Commander.PNG

    Imperial Commander:
    Imperial.PNG

    Raptor Commander:
    Raptor.PNG

    The remaining unbuildables do not have models.
  13. cola_colin

    cola_colin Moderator Alumni

    Messages:
    12,074
    Likes Received:
    16,221
    Based on this info I am about to use this in PA Stats for Alerts filtering, while doing so I've created these functions to parse in data from the unit json files, while tracking the base_specs as well:

    Code:
    // parses all units, following unit bases recursively
    // onComplete is given the finished map of spec => custom piece of data per spec
    // dataGetter gets the data from the unit json, it expects one parameter: the parsed unit json
    // datamerger expected two parameters, the data further up the definition tree of the unit and further down
    // examples see the next 2 functions after this
    function loadUnitData(onComplete, dataGetter, dataMerger) {
        var resultTypeMapping = {};
        var spawnedUnitCalls = 0;
        $.getJSON("coui://pa/units/unit_list.json", function(data) {
            var units = data.units;
            var finishedAll = false;
           
            function readUnitDataFromFile(file, callback) {
                $.getJSON(file, function(unit) {
                    var freshDataFromUnit = dataGetter(unit);
                    var baseSpec = unit.base_spec;
                   
                    if (baseSpec != undefined) {
                        readUnitDataFromFile("coui://"+baseSpec, function(unitData) {
                            callback(dataMerger(freshDataFromUnit, unitData));
                        });
                    } else {
                        if (freshDataFromUnit != undefined) {
                            callback(freshDataFromUnit);
                        }
                        spawnedUnitCalls--;
                        if (spawnedUnitCalls == 0) {
                            onComplete(resultTypeMapping);
                        }
                    }
                });
            }
           
            spawnedUnitCalls = units.length;
            for (var i = 0; i < units.length; i++) {
                function processUnitPath(unitPath) {
                    readUnitDataFromFile("coui://"+unitPath, function(unitData) {
                        resultTypeMapping[unitPath] = unitData;
                    });
                }
                processUnitPath(units[i]);
            }
        });
    }
    
    //creates a map of all unit specs to their display name
    function loadUnitNamesMapping(onComplete) {
        loadUnitData(onComplete, function(unit) {
            return unit.display_name;
        }, function (dataUpTheTree, dataDownTheTree) {
            return dataUpTheTree; // first name encountered is used
        });
    }
    
    //creates a map of all unit spec to an array of their type
    function loadUnitTypeMapping(onComplete) {
        loadUnitData(onComplete, function(unit) {
            var unitTypes = unit.unit_types;
            if (unitTypes != undefined) {
                for (var u = 0; u < unitTypes.length; u++) {
                    unitTypes[u] = unitTypes[u].replace("UNITTYPE_", "");
                }
            }
            return unitTypes;
        }, function(dataUpTheTree, dataDownTheTree) {
            if (dataUpTheTree == undefined) {
                dataUpTheTree = [];
            }
            if (dataDownTheTree == undefined) {
                dataDownTheTree = [];
            }
            return dataUpTheTree.concat(dataDownTheTree);
        });
    }
    
    Maybe somebody finds them helpful or wants to tell me how to do it better :p

    Calls look i.e. like this:


    Code:
    var pasUnitTypeMapping = {};
    loadUnitTypeMapping(function(mapping) {
        pasUnitTypeMapping = mapping;
        console.log(mapping);
    });
    
    loadUnitNamesMapping(function(data) {
        console.log(data);
    });
    
    Last edited: January 8, 2014

Share This Page