CnC Tiberium Alliances - Compass - CCE

Creates compass poiting to the currently selected base (compass points from the center of the screen). - EDIT by Chillchef - current position of center-screen added

// ==UserScript==
// @name        CnC Tiberium Alliances - Compass - CCE
// @namespace   http*://prodgame*.alliances.commandandconquer.com/*/index.aspx*
// @description Creates compass poiting to the currently selected base (compass points from the center of the screen). - EDIT by Chillchef - current position of center-screen added
// @version     1.0.1.3
// @author      Caine, Chillchef
// @include     http*://prodgame*.alliances.commandandconquer.com/*/index.aspx*
// @include     http*://cncapp*.alliances.commandandconquer.com/*/index.aspx*
// ==/UserScript==

(function () {
    var CompassMain = function () {
        try {
            function createCompass() {
                console.log('Compass loaded'); qx.Class.define('Compass', {
                    type: 'singleton',
                    extend: qx.core.Object,
                    members: {
                        needle: null,
                        ctx: null,
                        background: null,
                        size: 50,
                        initialize: function () {
                            try {
                                this.background = new Image();
                                this.background.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEYAAABaCAYAAAAFOiBkAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAACnNJREFUeNrsXV1sHFcVPnf+9sdrp3Ucp60QogHRSFFbVdCkrSKhSCBICVASGqdVSVIKUop45Y1H3pEQDzwArdKfUNM2pGkShERDgdQlrQQEKSUgNwgJUWo7tde7O7vzdznnzL27d9drtyDh2Os7yXh25s7cmfvdc77zMzN3xCN7dwBNjYV3wJjcg1/7dvLpLx2GLE0gyyQkSb4EELjMACT+FgL/43qaQSYzcF0X1x0sklQEvDeuUx2AS9riOA7XQfVxWZbwOtXDSyyXWZpXz/sCr9O+tI9Dx+A/R7i4T8Ln9nyPzwW0n+PyMWkS4zYB+VUAnteHQjHg48+cfBLOnXvO0YUfu/U2s+2C9wc79Z08/ePwt76DPe5AHKeIvEx2fuoBqC9W815BAYnjGHsZexKlxg8CiFotxpZ6K8scSLBcotT4fsCSlaUtcP0iS1aK27GvscwDV2TQaCUsEQ4en6QSuw1PgP89T0tTzF1JdZGUkHSRpHm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WVRaAXOji4VCkYtkq+l+dhhrzufMxGuK9E6jSnVIlf9NAYSJnyqyxsrmt5Pqam8jHhMvkY83nf9XIn0pSYsvZjdIpifFQlwTvZTI9el0kVSiIOQShxoHSEmYfJgVk/967N269S5VoyxTuZQULKt+V73cQzWSftAO3X5UZGxtr8UoaN87TDDHEMvfgFRtohwjCg2Qh5ZXZhlu9E6icdQuNph9TM5K2DDJ6jMniOoUqu8bRDWT3x0FRPO8zO16CCSI2Vi0ujawhKeeWyG/22yij1cQzpWBd+jOgOIsFQN7DRtY2ubXRto2sbXdvo2kbXNrq20bWNrm10baNrG13b6NpG1za6ttG1ja5tdG2jaxtd2+jaRtc2urbRtY2ubXRto+vVi65pxNMoinR8ACX2hLtfFh3U4d5KHEUrkREpvwIZ+Pkr6V6xWII4iuGWffvgHR4gcA7d4gwibFlzQAcIpON8GjIXLfJ7o2PwoQe+jBs9Hh329ItP58C8eeFkcteuL3q3PngQpuMYFief4yElqeVOH3O9nOQsAUasrsT810NKqlETCxOHYNvEwxDg+tSrL18+9ez3b9eW2X/jd6flnbs+C9seehiuYtPCk6dgbLHGUjNQg5CqbaQ+1yplqBya4EFIfc+H30+duYxFO8CIE7neSxd/Ie+65/Ow/eij8BZKy8ypl2DroA5bi6AMPXSIh60NkEqmXvnxZad40w6zbnM8X3HpjXM80PHHDx+FK0kCfxnggY5ZUoIAps6/0CUpt3/yvhyMDT40dnV8a2VT36Gx9fD7X/nq183B1CUNpp6m8UANpk7HE7gOXs/ZM89Xk0ZjU5/B1LtV6fgPvmtuL0RR3FrpuwQF6j3zuwTFD/5dgkL5+n+X4Ne/mrxRN7bnuwQOgP0uwbLTfwQYAIY7xh+Wjkp8AAAAAElFTkSuQmCC';
                                this.needle = new Image();
                                this.needle.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAACXBIWXMAAAsSAAALEgHS3X78AAAG6ElEQVRogd2aXYxdVRXHf+ucc8/QoU6mHWyfNEP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/oy4UuWNG/nvudME12yGYpA9oyoZCIRi+QrX7pxk+FNn+5UmjirMiNjktDJ3F+HwIsgbEA7wR4NalBEOb2Vl7i42jZ+tEioVYwTBZEMMcLqxAbKnMxKtUCH2qgL50FOdJfJB/AmlspfrAPlQT91H5v9a/wMJbneLG5+XVwAAAABJRU5ErkJggg==';
                                var ec = document.createElement('canvas');
                                document.body.appendChild(ec);

                                ec.width = 90;//70
                                ec.height = 110;//90
                                ec.style.position = 'absolute';
                                ec.style.top = '35px';//35
                                ec.style.left = '140px';
                                ec.style.zIndex = 999999;
                                this.ctx = ec.getContext('2d');

                                phe.cnc.Util.attachNetEvent(ClientLib.Vis.VisMain.GetInstance().get_Region(), "PositionChange", ClientLib.Vis.PositionChange, this, this.displayCompass);
                                this.displayCompass();
                            } catch (e) {
                                console.log("Compass.initialize: ", e);
                            }
                        },
                        displayCompass: function () {
                            try {
                                var currentCity = ClientLib.Data.MainData.GetInstance().get_Cities().get_CurrentOwnCity();
                                var cityCoordX = currentCity.get_PosX();
                                var cityCoordY = currentCity.get_PosY();
                                var region = ClientLib.Vis.VisMain.GetInstance().get_Region();
                                var zoom = region.get_ZoomFactor();
                                var targetCoordX = region.get_ViewWidth() / 2;
                                var targetCoordY = region.get_ViewHeight() / 2;
                                var gridW = region.get_GridWidth();
                                var gridH = region.get_GridHeight();
                                var viewCoordX = (region.get_PosX() + targetCoordX / zoom - zoom * gridW / 2) / gridW;
                                var viewCoordY = (region.get_PosY() + targetCoordY / zoom - zoom * gridH / 2) / gridH;
                                var dx = viewCoordX - cityCoordX;
                                var dy = cityCoordY - viewCoordY;
                                var distance = Math.sqrt(dx * dx + dy * dy);
                                var ctx = this.ctx;
                                ctx.clearRect(0, 0, 90, 110);    //70,90
                                ctx.save();
                                ctx.font = '18px Tahoma';

                                ctx.fillStyle = "rgba(200, 20, 20, 0.6)";
                                ctx.fillRect(0, 0, 90, 110);

                                var grd = ctx.createRadialGradient(50,50,10,45,-30,160);
                                grd.addColorStop(0,"rgba(0, 0, 0, 0.7)");
                                grd.addColorStop(1,"rgba(225, 0, 0, 0.7)");

                                ctx.fillStyle = grd;
                                ctx.fillRect(0,0,90,110);

                                ctx.fillStyle = "rgba(220, 0, 0, 1)";
                                ctx.lineWidth = 3;
                                ctx.strokeRect(0, 0, 90, 110);

                                ctx.translate(45, 35);//35
                                ctx.fillStyle = '#FCDE7E';
                                //ctx.drawImage(this.background, -30, -30);//-35,-35
                                var dtext = Math.round(10 * distance) / 10;
                                var dtext2 =  String (viewCoordX.toFixed(0)) + ":" + String(viewCoordY.toFixed(0));
                                ctx.fillText(dtext, -dtext.toString().length * 9 / 2, 40);
                                ctx.fillText(dtext2, -dtext2.toString().length * 9 / 2, 60);
                                ctx.rotate(dy > 0 ? Math.asin(dx / distance) + Math.PI : -Math.asin(dx / distance));
                                ctx.drawImage(this.needle, - this.size / 2, - this.size / 2);

                                ctx.restore();
                            } catch (e) {
                                console.log("displayCompass", e);
                            }
                        }
                    }
                });
            }
        } catch (e) {
            console.log('createCompass: ', e);
        }
        function CompassCheckLoaded() {
            try {
                if (typeof qx != 'undefined' && qx.core.Init.getApplication() && qx.core.Init.getApplication().getUIItem(ClientLib.Data.Missions.PATH.BAR_NAVIGATION) && qx.core.Init.getApplication().getUIItem(ClientLib.Data.Missions.PATH.BAR_NAVIGATION).isVisible()) {
                    createCompass(); window.Compass.getInstance().initialize();
                } else {
                    window.setTimeout(CompassCheckLoaded, 1000);
                }
            } catch (e) {
                console.log('CompassCheckLoaded: ', e);
            }
        }
        if (/commandandconquer\.com/i.test(document.domain)) {
            window.setTimeout(CompassCheckLoaded, 1000);
        }
    };
    try {
        var CompassScript = document.createElement('script');
        CompassScript.innerHTML = "(" + CompassMain.toString() + ')();';
        CompassScript.type = 'text/javascript';
        if (/commandandconquer\.com/i.test(document.domain)) {
            document.getElementsByTagName('head')[0].appendChild(CompassScript);
        }
    } catch (e) {
        console.log('Compass: init error: ', e);
    }
})();