CnC Tiberium Coord Box Shortcut

Adds "navigate" button which opens dialog in which you can enter map coordinates and scroll your screen to that location

  1. // ==UserScript==
  2. // @name CnC Tiberium Coord Box Shortcut
  3. // @namespace noobs
  4. // @description Adds "navigate" button which opens dialog in which you can enter map coordinates and scroll your screen to that location
  5. // @include https://prodgame*.alliances.commandandconquer.com/*/index.aspx*
  6. // @version 1.1
  7. // ==/UserScript==
  8.  
  9. // Fixed to work for chrome
  10. // All functions remain untouched
  11. // Original script here http://userscripts.org/scripts/show/137229
  12.  
  13. (function (){
  14. var MoveToCoords = function () {
  15. function getClib(){
  16. var clib;
  17. try{
  18. clib=ClientLib;
  19. }catch(e)
  20. {
  21. alert("can't get clientLib");
  22. return null;
  23. }
  24.  
  25. if(!clib)
  26. {
  27. alert("clib is undefined");
  28. }
  29. return clib;
  30. }
  31. function getMyCities(){
  32. var cities =getData().get_Cities().get_AllCities();
  33. return cities.d;
  34. }
  35.  
  36. function getWorld(){
  37. var world = getData().get_World();
  38. return world;
  39. }
  40. function getData(){
  41. return getClib().Data.MainData.GetInstance();
  42. }
  43.  
  44. function getNearestTargets(city){
  45. var world = getWorld();
  46. var x = city.get_X();
  47. var y = city.get_Y();
  48. var offset = 7;
  49. var msg = ""+city.get_Name()+"("+x+","+y+")\n";
  50. var types=["0","1","N","E","@"];
  51. for(var cy = y - offset; cy < y+ offset;cy++){
  52. for(var cx = x-offset; cx < x+offset;cx++){
  53.  
  54. var kind = types[world.GetTerritoryTypeByCoordinates(cx,cy)];
  55. var attack;
  56. try{
  57. attack = world.CheckAttackBase (cx , cy);
  58. }catch(e){
  59. //alert('err!');
  60. }
  61.  
  62. if(attack && (attack == attack.OK)){
  63. msg+="["+(x-cx)+","+(y-cy)+"]\n";
  64. }
  65. // var water = world.IsWater(cx,cy) ? 'W':'_';
  66. // var blocked = world.IsBlocked(cx,cy) ? '#':'_';
  67. //msg+=""+kind;//+water+blocked;
  68. }
  69. msg+="\n";
  70. }
  71. alert(msg);
  72. }
  73. function test(e){
  74. var clib=getClib();
  75. var citiesData = getMyCities();
  76. var counter = 0;
  77. var msg = "";
  78. for (var cityFname in citiesData){
  79. var city = citiesData[cityFname];
  80. var level = city.get_BaseLevel();
  81. var name = city.get_Name();
  82. counter++;
  83. msg+="'"+name+"':"+level+"\n";
  84. try{
  85. city.setResourcesToMax();
  86. }catch(e)
  87. {
  88. alert('cannot exec that func');
  89. }
  90. }
  91. msg = "CITIES:"+counter+"\n"+msg;
  92. alert(msg);
  93. }
  94.  
  95. function rangeTest(){
  96. var world = getWorld();
  97. world.SetRange(true,true,0,0,10,10);
  98. alert('finished');
  99. }
  100.  
  101.  
  102. function createButton(name, handler, par){
  103.  
  104.  
  105. var button = document.createElement("button");
  106. button.setAttribute("type","button");
  107. button.setAttribute("id", "myButton"+Math.random().toString().substr(2));
  108. try{
  109. // button.value=name;
  110. // button.setAttribute("value",name);
  111. // button.setAttribute("innerHTML",name);
  112. // button.setAttribute("label",name);
  113. // button.setAttribute("name",name);
  114. // button.setAttribute("text",name);
  115. button.innerHTML=name;
  116. }catch(e)
  117. {
  118. //do nothing, result is visible
  119. }
  120. button.addEventListener("click", handler);
  121. button.style.height="100%";
  122. button.style.margin = "2px";
  123. par.appendChild(button);
  124. }
  125. function tryBase(){
  126. var cities = getMyCities();
  127. for(var cityFname in cities){
  128. var city = cities[cityFname];
  129. getNearestTargets(city);
  130. }
  131. }
  132.  
  133. function navigate(){
  134. var dialog = document.getElementById('ta_navigation_container');
  135. if(dialog)
  136. dialog.style.display = (dialog.style.display == 'block') ? 'none' : 'block';
  137. else
  138. createDialog(["x","y"], processNavigateDialog);
  139. }
  140. function processNavigateDialog(){
  141.  
  142. }
  143. function createDialog(fields, handler){
  144. var dialog = document.createElement("div");
  145. dialog.id = 'ta_navigation_container';
  146. dialog.style.position="absolute";
  147. dialog.style.top="30px";
  148. dialog.style.left="127px";
  149. dialog.style.zIndex="100";
  150. dialog.style.display="block";
  151. dialog.style.borderColor="#DCDCDC"
  152. dialog.style.borderStyle="solid";
  153. dialog.style.borderWidth="5px";
  154. dialog.style.backgroundColor="#881515";
  155. document.body.appendChild(dialog);
  156. lastDialog=dialog;
  157. for(var i=0; i < fields.length;i++){
  158. var inputName = fields[i];
  159. //var element = document.createElement("div");
  160. var element = document.createElement("input");
  161. element.style.margin="2px";
  162. //var br = document.createElement("br");
  163. dialog.appendChild(element);
  164. //dialog.appendChild(br);
  165. }
  166. var commit = document.createElement("button");
  167. commit.innerHTML="Navigate!";
  168. commit.style.margin="2px";
  169. commit.addEventListener("click", doNavigate);
  170. dialog.appendChild(commit);
  171. }
  172. function getVis(){
  173. var clib = getClib();
  174. return clib.Vis.VisMain.GetInstance();
  175. }
  176. function doNavigate(){
  177. var dia = lastDialog;
  178.  
  179. var x = Number(dia.childNodes[0].value);
  180. var y = Number(dia.childNodes[1].value);
  181. var vis = getVis();
  182. vis.CenterGridPosition(x,y);
  183. document.body.removeChild(lastDialog);
  184. lastDialog = null;
  185. }
  186.  
  187. function createPanel(){
  188. var panel = document.createElement("div");
  189. panel.style.position="absolute";
  190. panel.style.top="0px";
  191. panel.style.left="127px";
  192. //panel.style.height="20px";
  193. panel.style.zIndex="100";
  194. panel.style.display="block";
  195. panel.style.borderColor="#DCDCDC";
  196. panel.style.borderStyle="solid";
  197. panel.style.borderWidth="5px";
  198. panel.style.backgroundColor="#881515";
  199. document.body.appendChild(panel);
  200. // createButton('Test', test,panel);
  201. // createButton('Targets',tryBase,panel);
  202. // createButton('Range',rangeTest, panel);
  203. createButton('Navigate', navigate,panel);
  204. }
  205.  
  206. createPanel();
  207. }
  208. var TASuiteScript = document.createElement("script");
  209. var txt = MoveToCoords.toString();
  210. TASuiteScript.innerHTML = "(" + txt + ")();";
  211. TASuiteScript.type = "text/javascript";
  212. if (/commandandconquer\.com/i.test(document.domain)) {
  213. document.getElementsByTagName("head")[0].appendChild(TASuiteScript);
  214. }
  215. })();