Thread Rating:
  • 1 Votes - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
API Example script
Author Message
Topper42 Offline
Developer
***

Posts: 248
Joined: Dec 2011
Post: #1
API Example script
Hi,

because of the many questions on how to use our api, i wrote a little example script for greasemonkey. It captures all functions from the API right now. It shows you how to build up your own script and how to use event listener.

So here is the code(https://userscripts.org/scripts/show/155079):

Code:
// ==UserScript==
// @name EA Command and Conquer: Tiberium Alliances API Example Script
// @description EA Command and Conquer: Tiberium Alliances API Example Script
// @namespace      http*://*.alliances.commandandconquer.com/*
// @include        http*://*.alliances.commandandconquer.com/*
// @version 1.0
// @author Topper42
// @grant none
// ==/UserScript==
(function(){
var injectFunction = function()
{
    function createClass()
    {
        qx.Class.define("EA_CNCTA_Example",
        {
            type: "singleton",
            extend: qx.core.Object,
            
            construct: function()
            {
                try
                {
                    // combat
                    
                    var app = qx.core.Init.getApplication();
                    var armySetupAttackBar = app.getArmySetupAttackBar();
                    var btnOpenCombatWindow = new qx.ui.form.Button("Sim");
                    btnOpenCombatWindow.set({ alignY: "middle", width: 60, maxHeight: 25, toolTipText: "open combat window" });
                    btnOpenCombatWindow.addListener("execute",this.__openCombatWindow, this);
                    var container = armySetupAttackBar.getChildren()[1];
                    var cntWave = container.getChildren()[4];
                    cntWave.removeAll();
                    cntWave.setLayout(new qx.ui.layout.HBox());
                    cntWave.add(new qx.ui.core.Spacer(), { flex: 1});
                    cntWave.add(btnOpenCombatWindow);
                    cntWave.add(new qx.ui.core.Spacer(), { flex: 1});
                    
                    // city
                    var optionsBar = app.getOptionsBar();
                    var parent = optionsBar.getLayoutParent();
                    var cntButtons = new qx.ui.container.Composite(new qx.ui.layout.HBox(5)).set({
                    allowGrowX: false, allowShrinkX: true });
                    var btnUpgradeAll = new qx.ui.form.Button(null, "FactionUI/icons/icon_building_detail_upgrade.png").set({
                    width: 44, height: 44, toolTipText: "open upgrade city window"});
                    btnUpgradeAll.addListener("execute",this.__openCityWindow, this);
                    cntButtons.add(new qx.ui.core.Spacer(), { flex: 1});
                    cntButtons.add(btnUpgradeAll);
                    parent.addAt(cntButtons,0);
                    
                }
                catch (e)
                {
                    console.log("Something is terribly wrong with this script. :(");
                    console.log(e.toString());
                }
                console.log("EA API Example script: Initialising finshed");
            },
            
            destruct: function()
            {
                
            },
                        
            members:
            {
                __openCombatWindow: function()
                {
                    EA_CNCTA_Example.CombatWindow.getInstance().open();
                },
                
                __openCityWindow: function()
                {
                    EA_CNCTA_Example.CityWindow.getInstance().open();
                },
                
                attachNetEvent: function()
                {
                    console.log("Need to assign correct function!");
                },
                
                formatNumbersCompact: function()
                {
                    console.log("Need to assign correct function!");
                },
                
                GetUnitMaxHealth: function()
                {
                    console.log("Need to assign correct function!");
                    return -1;                
                }
            }
        });
        
        qx.Class.define("EA_CNCTA_Example.CityWindow",
        {
            type: "singleton",
            extend: webfrontend.gui.CustomWindow,
            
            construct: function()
            {
                this.base(arguments);
                this.setLayout(new qx.ui.layout.VBox(2));
                
                this.set({
                    width: 200,
                    caption: "CityUpgradeWindow",
                    padding: 2,
                    allowMaximize: false,
                    showMaximize: false,
                    allowMinimize: false,
                    showMinimize: false,
                });
                
                var cntUpgradeAll = new qx.ui.container.Composite(new qx.ui.layout.VBox(5)).set({ padding: 5, decorator: "pane-light-opaque"});
                
                var lblTitleAll = new qx.ui.basic.Label("Upgrade all buildings").set({ alignX: "center", font: "font_size_14_bold"});
                cntUpgradeAll.add(lblTitleAll);
                
                var cntUpgradeAllHBox = new qx.ui.container.Composite(new qx.ui.layout.HBox(5));
                var lblPreLevelAll = new qx.ui.basic.Label("New Level:").set({ alignY: "middle"});
                this.__txtNewLevelAll = new qx.ui.form.TextField().set({ width: 100});
                this.__btnUpgradeAll = new qx.ui.form.Button("Upgrade", "FactionUI/icons/icon_building_detail_upgrade.png");
                this.__btnUpgradeAll.addListener("execute", this.__upgradeAllBuildings, this);
                
                cntUpgradeAllHBox.add(lblPreLevelAll);
                cntUpgradeAllHBox.add(this.__txtNewLevelAll);
                cntUpgradeAllHBox.add(this.__btnUpgradeAll);
                cntUpgradeAll.add(cntUpgradeAllHBox);
                
                this.add(cntUpgradeAll);
                
                var cntUpgradeCurrent = new qx.ui.container.Composite(new qx.ui.layout.VBox(5)).set({ padding: 5, decorator: "pane-light-opaque"});
                
                var lblTitleCurrent = new qx.ui.basic.Label("Upgrade current building").set({ alignX: "center", font: "font_size_14_bold"});
                cntUpgradeCurrent.add(lblTitleCurrent);
                
                this.__lblCurrentSelected = new qx.ui.basic.Label("Selected building: -");
                cntUpgradeCurrent.add(this.__lblCurrentSelected);
                
                var cntUpgradeCurrentHBox = new qx.ui.container.Composite(new qx.ui.layout.HBox(5));
                var lblPreLevelCurrent = new qx.ui.basic.Label("New Level:").set({ alignY: "middle"});
                this.__txtNewLevelCurrent = new qx.ui.form.TextField().set({ width: 100});
                this.__btnUpgradeBuilding = new qx.ui.form.Button("Upgrade", "FactionUI/icons/icon_building_detail_upgrade.png");
                this.__btnUpgradeBuilding.addListener("execute", this.__upgradeBuilding, this);
                
                cntUpgradeCurrentHBox.add(lblPreLevelCurrent);
                cntUpgradeCurrentHBox.add(this.__txtNewLevelCurrent);
                cntUpgradeCurrentHBox.add(this.__btnUpgradeBuilding);
                cntUpgradeCurrent.add(cntUpgradeCurrentHBox);
                
                this.add(cntUpgradeCurrent);
                
                EA_CNCTA_Example.getInstance().attachNetEvent(ClientLib.Vis.VisMain.GetInst​ance(), "SelectionChange", ClientLib.Vis.SelectionChange, this, this.__onSelectionChange);
            },
            
            destruct: function()
            {
                
            },
                        
            members:
            {
                __txtNewLevelAll: null,
                __btnUpgradeAll: null,
                
                __currentSelectedBuilding: null,
                __lblCurrentSelected: null,
                __btnUpgradeBuilding: null,
                __txtNewLevelCurrent: null,
                
                __upgradeAllBuildings: function()
                {
                    var newLevel = parseInt(this.__txtNewLevelAll.getValue());
                    if (newLevel > 0)
                        ClientLib.API.City.GetInstance().UpgradeAllBuildingsToLevel(newLevel);
                    
                    this.__txtNewLevelAll.setValue("");
                },
                
                __upgradeBuilding: function()
                {
                    var newLevel = parseInt(this.__txtNewLevelCurrent.getValue());
                    if (newLevel > 0)
                    {
                        var obj = this.__currentSelectedBuilding;
                        if (obj != null && obj.get_VisObjectType() == ClientLib.Vis.VisObject.EObjectType.CityBuildingType)
                            ClientLib.API.City.GetInstance().UpgradeBuildingToLevel(obj.get_BuildingDet​ails(), newLevel);
                    }
                    this.__txtNewLevelCurrent.setValue("");
                },
                
                __onSelectionChange: function(oldObj, newObj)
                {
                    if (newObj != null && newObj.get_VisObjectType() == ClientLib.Vis.VisObject.EObjectType.CityBuildingType)
                    {
                        this.__currentSelectedBuilding = newObj;
                        var name = newObj.get_BuildingName();
                        var level = newObj.get_BuildingLevel();
                        
                        this.__lblCurrentSelected.setValue("Selected building: " + name + "(" + level.toString() + ")");
                    }
                }
            }
        });
        
        qx.Class.define("EA_CNCTA_Example.CombatWindow",
        {
            type: "singleton",
            extend: webfrontend.gui.CustomWindow,
            
            construct: function()
            {
                this.base(arguments);
                this.setLayout(new qx.ui.layout.VBox());
                
                this.set({
                    width: 200,
                    caption: "CombatWindow",
                    padding: 2,
                    allowMaximize: false,
                    showMaximize: false,
                    allowMinimize: false,
                    showMinimize: false,
                    
                });
                
                this.setResizable(false, true, false, true);
                
                // Health
                
                var cntHealthTitle = new qx.ui.container.Composite(new qx.ui.layout.VBox()).set({ decorator: "pane-light-opaque"});
                var lblTitel = new qx.ui.basic.Label("Health").set({ alignX: "center", font: "font_size_14_bold"});
                cntHealthTitle.add(lblTitel);
                this.add(cntHealthTitle);
                
                var cntHealth = new qx.ui.container.Composite(new qx.ui.layout.HBox(2));
                var cntPreText = new qx.ui.container.Composite(new qx.ui.layout.VBox(5)).set({ padding: 5, decorator: "pane-light-opaque"});
                var lblOffenseInf = new qx.ui.basic.Label("Infantry:");
                var lblOffenseVeh = new qx.ui.basic.Label("Vehicle:");
                var lblOffenseAir = new qx.ui.basic.Label("Air:");
                var lblDefense = new qx.ui.basic.Label("Defense:");
                cntPreText.add(lblOffenseInf);
                cntPreText.add(lblOffenseVeh);
                cntPreText.add(lblOffenseAir);
                cntPreText.add(lblDefense);
                cntHealth.add(cntPreText);

                this.__lblUnitsHealthOffenseInf = new Array();
                this.__lblUnitsHealthOffenseVeh = new Array();
                this.__lblUnitsHealthOffenseAir = new Array();
                this.__lblUnitsHealthDefense = new Array();

                var cntValues = new qx.ui.container.Composite(new qx.ui.layout.VBox(5)).set({ alignX: "center", width: 50, padding: 5, decorator: "pane-light-opaque"});
                this.__lblUnitsHealthOffenseInf = new qx.ui.basic.Label("-");
                this.__lblUnitsHealthOffenseVeh = new qx.ui.basic.Label("-");
                this.__lblUnitsHealthOffenseAir = new qx.ui.basic.Label("-");
                this.__lblUnitsHealthDefense = new qx.ui.basic.Label("-");
                cntValues.add(this.__lblUnitsHealthOffenseInf);
                cntValues.add(this.__lblUnitsHealthOffenseVeh);
                cntValues.add(this.__lblUnitsHealthOffenseAir);
                cntValues.add(this.__lblUnitsHealthDefense);
                cntHealth.add(cntValues, { flex: 1 });            
                this.add(cntHealth);
                
                // Repair
                
                var cntRepairTitle = new qx.ui.container.Composite(new qx.ui.layout.VBox()).set({ decorator: "pane-light-opaque"});
                var lblTitel = new qx.ui.basic.Label("Repair").set({ alignX: "center", font: "font_size_14_bold"});
                cntRepairTitle.add(lblTitel);
                this.add(cntRepairTitle);
                
                var cntRepair = new qx.ui.container.Composite(new qx.ui.layout.HBox(2));
                var cntRepairPreText = new qx.ui.container.Composite(new qx.ui.layout.VBox(5)).set({ padding: 5, decorator: "pane-light-opaque"});
                var lblRepairOffenseInf = new qx.ui.basic.Label("Infantry:");
                var lblRepairOffenseVeh = new qx.ui.basic.Label("Vehicle:");
                var lblRepairOffenseAir = new qx.ui.basic.Label("Air:");
                cntRepairPreText.add(lblRepairOffenseInf);
                cntRepairPreText.add(lblRepairOffenseVeh);
                cntRepairPreText.add(lblRepairOffenseAir);
                cntRepair.add(cntRepairPreText);

                this.__lblUnitsRepairOffenseInf = new Array();
                this.__lblUnitsRepairOffenseVeh = new Array();
                this.__lblUnitsRepairOffenseAir = new Array();

                var cntRepairValues = new qx.ui.container.Composite(new qx.ui.layout.VBox(5)).set({ width: 50, padding: 5, decorator: "pane-light-opaque"});
                this.__lblUnitsRepairOffenseInf = new qx.ui.basic.Label("-");
                this.__lblUnitsRepairOffenseVeh = new qx.ui.basic.Label("-");
                this.__lblUnitsRepairOffenseAir = new qx.ui.basic.Label("-");
                cntRepairValues.add(this.__lblUnitsRepairOffenseInf);
                cntRepairValues.add(this.__lblUnitsRepairOffenseVeh);
                cntRepairValues.add(this.__lblUnitsRepairOffenseAir);
                cntRepair.add(cntRepairValues, { flex: 1 });            
                this.add(cntRepair);
                
                // Buttons
                
                var cntButtons = new qx.ui.container.Composite(new qx.ui.layout.VBox(5)).set({ padding: 5, decorator: "pane-light-opaque"});
                this.add(cntButtons);
                
                var btnSimulate = new qx.ui.form.Button("Simulate");
                btnSimulate.addListener("execute", this._SimulateWithPreview, this);
                cntButtons.add(btnSimulate);
        
                var btnSimulateStats = new qx.ui.form.Button("Simulate & Stats");
                btnSimulateStats.addListener("execute", this._SimulateWithPreviewAndStats, this);
                cntButtons.add(btnSimulateStats);        
                
                var btnStats = new qx.ui.form.Button("Stats");
                btnStats.addListener("execute", this._SimulateStats, this);
                cntButtons.add(btnStats);
                
                EA_CNCTA_Example.getInstance().attachNetEvent(ClientLib.API.Battleground.Ge​tInstance(), "OnSimulateBattleFinished", ClientLib.API.OnSimulateBattleFinished, this, this.__OnSimulateBattleFinished);
            },
            
            destruct: function()
            {
                
            },
                        
            members:
            {    
                __lblUnitsHealthOffenseInf: null,
                __lblUnitsHealthOffenseVeh: null,
                __lblUnitsHealthOffenseAir: null,
                __lblUnitsHealthDefense: null,
                
                __lblUnitsRepairOffenseInf: null,
                __lblUnitsRepairOffenseVeh: null,
                __lblUnitsRepairOffenseAir: null,

                __withoutStats: false,

                _SimulateWithPreview: function()
                {
                    this.__withoutStats = true;
                    
                    var city = ClientLib.Data.MainData.GetInstance().get_Cities().get_CurrentCity();
                    if (city != null)
                    {
                        var ownCity = ClientLib.Data.MainData.GetInstance().get_Cities().get_CurrentOwnCity();
                        ownCity.get_CityArmyFormationsManager().set_CurrentTargetBaseId(city.get_Id​());
                        ClientLib.API.Battleground.GetInstance().SimulateBattle();
                        var app = qx.core.Init.getApplication();
                        app.getPlayArea().setView(ClientLib.Data.PlayerAreaViewMode.pavmCombatRepla​y, city.get_Id(), 0, 0);
                        
                        this.__lblUnitsHealthOffense.setValue("-");
                        this.__lblUnitsHealthDefense.setValue("-");
                        this.__lblUnitsHealthAll.setValue("-");
                    }
                },
                
                _SimulateWithPreviewAndStats: function()
                {
                    this.__withoutStats = false;
                    
                    var city = ClientLib.Data.MainData.GetInstance().get_Cities().get_CurrentCity();
                    if (city != null)
                    {
                        var ownCity = ClientLib.Data.MainData.GetInstance().get_Cities().get_CurrentOwnCity();
                        ownCity.get_CityArmyFormationsManager().set_CurrentTargetBaseId(city.get_Id​());
                        ClientLib.API.Battleground.GetInstance().SimulateBattle();
                        var app = qx.core.Init.getApplication();
                        app.getPlayArea().setView(ClientLib.Data.PlayerAreaViewMode.pavmCombatRepla​y, city.get_Id(), 0, 0);
                    }
                },
                
                _SimulateStats: function()
                {
                    this.__withoutStats = false;
                    
                    var city = ClientLib.Data.MainData.GetInstance().get_Cities().get_CurrentCity();
                    if (city != null)
                    {
                        var ownCity = ClientLib.Data.MainData.GetInstance().get_Cities().get_CurrentOwnCity();
                        ownCity.get_CityArmyFormationsManager().set_CurrentTargetBaseId(city.get_Id​());
                        ClientLib.API.Battleground.GetInstance().GetSimulateBattleEndInfos();
                    }
                },
                
                __OnSimulateBattleFinished: function(data)
                {            
                    if (data == null || this.__withoutStats)
                        return;
                        
                    var defenseHealthBefore = 0;
                    var defenseHealthAfter = 0;
                    
                    var offenseHealthBefore = new Array(0,0,0);
                    var offenseHealthAfter = new Array(0,0,0);
                    
                    var offenseRepairCrystalCosts = new Array(0,0,0);
                    var offenseRepairTimeCosts = new Array(0,0,0);
                    
                    for (var i = 0; i < data.length; i++)
                    {
                        var combatId = data[i].Key;
                           var unitData = data[i].Value;
                      
                           var unitMDBID = unitData.t;
                           var unitLevel = unitData.l;
                           var unitStartHealth = unitData.sh;
                           var unitEndHealth = unitData.h;
                      
                          var unit = ClientLib.Res.ResMain.GetInstance().GetUnit_Obj(unitMDBID);
                        var placementType = unit.pt;
                        var movementType = unit.mt;
                        
                        var unitMaxHealth = EA_CNCTA_Example.getInstance().GetUnitMaxHealth(unitLevel, unit, false);
                                            
                        switch (placementType)
                        {
                            case ClientLib.Base.EPlacementType.Defense:
                                defenseHealthBefore += unitStartHealth;
                                defenseHealthAfter += unitEndHealth;
                                break;
                            case ClientLib.Base.EPlacementType.Offense:
                                var index = -1;
                                switch (movementType)
                                {
                                    case ClientLib.Base.EUnitMovementType.Feet:
                                        index = 0;
                                        break;
                                    case ClientLib.Base.EUnitMovementType.Wheel:
                                    case ClientLib.Base.EUnitMovementType.Track:
                                        index = 1;
                                        break;
                                    case ClientLib.Base.EUnitMovementType.Air:
                                    case ClientLib.Base.EUnitMovementType.Air2:
                                        index = 2;
                                        break;
                                }
                                if (index > -1)
                                {
                                    offenseHealthBefore[index] += unitStartHealth;
                                    offenseHealthAfter[index] += unitEndHealth;
                                    
                                    if (unitStartHealth != unitEndHealth)
                                    {    
                                        var damageRatio = 1;
                                        if (unitEndHealth > 0)
                                        {
                                            if (unitMaxHealth != -1)
                                            {
                                                damageRatio = (unitMaxHealth - (unitEndHealth/16)) / unitMaxHealth;
                                            }
                                        }
                                    
                                        var repairCosts = ClientLib.API.Util.GetUnitRepairCosts(unitLevel, unitMDBID, damageRatio);
                                        var crystal = 0;
                                        var repairTime = 0;
                                        for (var j = 0; j < repairCosts.length; j++)
                                        {
                                            var c = repairCosts[j];
                                            var type = parseInt(c.Type);
                                            switch (type)
                                            {
                                                case ClientLib.Base.EResourceType.Crystal:
                                                    crystal += c.Count;
                                                    break;
                                                case ClientLib.Base.EResourceType.RepairChargeBase:
                                                case ClientLib.Base.EResourceType.RepairChargeInf:
                                                case ClientLib.Base.EResourceType.RepairChargeVeh:
                                                case ClientLib.Base.EResourceType.RepairChargeAir:
                                                    repairTime += c.Count;
                                                    break;
                                            }
                                        }
                                        offenseRepairCrystalCosts[index] += crystal;
                                        offenseRepairTimeCosts[index] += repairTime;
                                    }
                                }
                                break;
                            case ClientLib.Base.EPlacementType.Structure:
                                defenseHealthBefore += unitStartHealth;
                                defenseHealthAfter += unitEndHealth;                        
                                break;
                        }
                    }
                    
                    this.__setHealthLabel(this.__lblUnitsHealthOffenseInf, offenseHealthBefore[0], offenseHealthAfter[0]);
                    this.__setHealthLabel(this.__lblUnitsHealthOffenseVeh, offenseHealthBefore[1], offenseHealthAfter[1]);
                    this.__setHealthLabel(this.__lblUnitsHealthOffenseAir, offenseHealthBefore[2], offenseHealthAfter[2]);
                    this.__setHealthLabel(this.__lblUnitsHealthDefense, defenseHealthBefore, defenseHealthAfter);

                    this.__setRepairLabel(this.__lblUnitsRepairOffenseInf, offenseRepairCrystalCosts[0], offenseRepairTimeCosts[0]);
                    this.__setRepairLabel(this.__lblUnitsRepairOffenseVeh, offenseRepairCrystalCosts[1], offenseRepairTimeCosts[1]);
                    this.__setRepairLabel(this.__lblUnitsRepairOffenseAir, offenseRepairCrystalCosts[2], offenseRepairTimeCosts[2]);
                },
                
                __setHealthLabel: function(lbl, before, after)
                {
                    if (before > 0)
                    {
                        var percent = Math.floor(((after * 100 )/ before));
                        lbl.setValue(percent.toString() + "%");
                        lbl.setToolTipText(before.toString() + " / " + after.toString());
                    }
                    else
                    {
                        lbl.setValue("-");    
                    }
                },
                
                __setRepairLabel: function(lbl, crystal, repairTime)
                {
                    var formattedRepairTime = "-";
                    if (repairTime == Number.POSITIVE_INFINITY)
                        formattedRepairTime = "inf";
                    else if (repairTime > 0)
                        formattedRepairTime = phe.cnc.Util.getTimespanString(ClientLib.Data.MainData.GetInstance().get_Time().​GetTimeSpan(repairTime));
                        
                    var formattedCrystal = "-";
                    if (crystal == Number.POSITIVE_INFINITY)
                        formattedCrystal = "inf";
                    else if (crystal > 0)
                        formattedCrystal = EA_CNCTA_Example.getInstance().formatNumbersCompact(crystal);

                    lbl.setValue(formattedCrystal + " / " + formattedRepairTime);
                }
                
            },
        });
    }
    
    function waitForGame()
    {
        try
        {
            if (typeof qx != 'undefined' && typeof qx.core != 'undfined' && typeof qx.core.Init != 'undefined')
            {
                var app = qx.core.Init.getApplication();
                if (app.initDone == true)
                {
                    try
                    {
                        createClass();
                        
                        console.log("Creating phe.cnc function wraps");
                        
                        if (typeof phe.cnc.Util.attachNetEvent == 'undefined')
                            EA_CNCTA_Example.getInstance().attachNetEvent = webfrontend.gui.Util.attachNetEvent;
                        else
                            EA_CNCTA_Example.getInstance().attachNetEvent = phe.cnc.Util.attachNetEvent;
                        
                        if (typeof phe.cnc.gui.util == 'undefined')
                            EA_CNCTA_Example.getInstance().formatNumbersCompact = webfrontend.gui.Util.formatNumbersCompact;    
                        else
                            EA_CNCTA_Example.getInstance().formatNumbersCompact = phe.cnc.gui.util.Numbers.formatNumbersCompact;        
                        
                        
                        // Strange Hacks
                        // don't try this at home ;)
                        if (typeof ClientLib.API.Util.GetUnitMaxHealth == 'undefined')
                            for (var key in ClientLib.Base.Util)
                            {
                                var strFunction = ClientLib.Base.Util[key].toString();
                                if ((strFunction.indexOf("function (a,b,c)") == 0 || strFunction.indexOf("function (a,b)") == 0) &&
                                     strFunction.indexOf("*=1.1") > -1)
                                {
                                        EA_CNCTA_Example.getInstance().GetUnitMaxHealth = ClientLib.Base.Util[key];
                                        break;
                                }
                            }
                        else
                            EA_CNCTA_Example.getInstance().GetUnitMaxHealth = ClientLib.API.Util.GetUnitMaxHealth;    
                        
                        EA_CNCTA_Example.getInstance();
                    }
                    catch(e)
                    {
                        console.log("EA API Eaxmple script init error:");
                        console.log(e);
                    }
                }
                else
                    window.setTimeout(waitForGame, 1000);
            }
            else
            {
                window.setTimeout(waitForGame, 1000);
            }
        }
        catch (e)
        {
            if (typeof console != 'undefined') console.log(e);
            else if (window.opera) opera.postError(e);
            else GM_log(e);
        }
    };
    
    window.setTimeout(waitForGame, 1000);    
}

    var script = document.createElement("script");
    var txt = injectFunction.toString();
        script.innerHTML = "(" + txt + ")();";
        script.type = "text/javascript";
    
    document.getElementsByTagName("head")[0].appendChild(script);
})();
(This post was last modified: 27-12-2012 06:06 PM by Topper42.)
23-12-2012 10:35 PM
 Search   Quote 
Deyhak Offline
Member
***

Posts: 53
Joined: Jul 2012
Post: #2
RE: API Example script
Amazing work.

The funniest thing is that nothing is here is anywhere near what we have been introduced to so far!

But I learnt already just by reviewing the script, so this is gonna get us somewhere.

Good job once again!
23-12-2012 11:26 PM
 Search   Quote 
Topper42 Offline
Developer
***

Posts: 248
Joined: Dec 2011
Post: #3
RE: API Example script
(23-12-2012 11:26 PM)Deyhak Wrote:  Amazing work.

The funniest thing is that nothing is here is anywhere near what we have been introduced to so far!

But I learnt already just by reviewing the script, so this is gonna get us somewhere.

Good job once again!

That's why i thought a script says more than thousand words ^^.
23-12-2012 11:40 PM
 Search   Quote 
Peluski17 Offline
Member
***

Posts: 133
Joined: Nov 2012
Post: #4
RE: API Example script
This is excellent! Thanks Topper!
24-12-2012 12:02 AM
 Search   Quote 
Peluski17 Offline
Member
***

Posts: 133
Joined: Nov 2012
Post: #5
RE: API Example script
I've tried running the code in Greasemonkey and not getting any errors, but nothing shows up either in game. Could be a difference of versions perhaps?
24-12-2012 01:18 AM
 Search   Quote 
Peluski17 Offline
Member
***

Posts: 133
Joined: Nov 2012
Post: #6
RE: API Example script
I'm guessing it was a copying problem. Recopied and tweaked by hand and it works. Very nice script btw. Although, I did notice that Simulate and Simulate with Stats does the same thing even though simulate is just supposed to simulate. I think it may be how the API is written.
24-12-2012 02:57 AM
 Search   Quote 
Topper42 Offline
Developer
***

Posts: 248
Joined: Dec 2011
Post: #7
RE: API Example script
(24-12-2012 02:57 AM)Peluski17 Wrote:  I'm guessing it was a copying problem. Recopied and tweaked by hand and it works. Very nice script btw. Although, I did notice that Simulate and Simulate with Stats does the same thing even though simulate is just supposed to simulate. I think it may be how the API is written.

Yes your right, it will always send down the additional information. I changed the script so that simulate will no longer calculate the stats.
24-12-2012 10:18 AM
 Search   Quote 
Topper42 Offline
Developer
***

Posts: 248
Joined: Dec 2011
Post: #8
RE: API Example script
I, also work on an advanced version but i'm not sure if i will release it.

Just a little peak, so the scripting competition may begin Wink

[Image: combatwindow.png]
26-12-2012 05:00 PM
 Search   Quote 
Peluski17 Offline
Member
***

Posts: 133
Joined: Nov 2012
Post: #9
RE: API Example script
Does that advanced script allow the player to change their unit formation each time?
26-12-2012 05:09 PM
 Search   Quote 
Topper42 Offline
Developer
***

Posts: 248
Joined: Dec 2011
Post: #10
RE: API Example script
(26-12-2012 05:09 PM)Peluski17 Wrote:  Does that advanced script allow the player to change their unit formation each time?

Yes it saves your formation when you click simulate and you can select a row and your formation will be set to that simulation Wink
26-12-2012 05:19 PM
 Search   Quote 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)