Advertisement
Guest User

Untitled

a guest
May 25th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.46 KB | None | 0 0
  1. (function(w) {
  2. var modVersion = "v2.0.0",
  3. renderMode = 3, // 3 - Normal, 2 - Optimized, 1 - Simple
  4. normalMode = false,
  5. gameFPS = null,
  6. positionHUD = null,
  7. ipHUD = null,
  8. scoreHUD = null,
  9. fpsHUD = null,
  10. styleHUD = "color: #FFF; font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; font-size: 14px; position: fixed; opacity: 0.35; z-index: 7;",
  11. inpNick = null,
  12. currentIP = null,
  13. retry = 0,
  14. bgImage = null,
  15. rotate = false,
  16. highScore = 0;
  17. function init() {
  18. // Append DIVs
  19. appendDiv("position-hud", "nsi", styleHUD + "right: 30; bottom: 120px;");
  20. appendDiv("ip-hud", "nsi", styleHUD + "right: 30; bottom: 150px;");
  21. appendDiv("score-hud", "nsi", styleHUD + "right: 30; bottom: 170px;");
  22. appendDiv("fps-hud", "nsi", styleHUD + "right: 30; bottom: 190px;");
  23. positionHUD = document.getElementById("position-hud");
  24. ipHUD = document.getElementById("ip-hud");
  25. scoreHUD = document.getElementById("score-hud");
  26. fpsHUD = document.getElementById("fps-hud");
  27. // Add zoom
  28. if (/firefox/i.test(navigator.userAgent)) {
  29. document.addEventListener("DOMMouseScroll", zoom, false);
  30. } else {
  31. document.body.onmousewheel = zoom;
  32. }
  33. // Keys
  34. w.addEventListener("keydown", function(e) {
  35. switch(e.keyCode) {
  36. // ESC - quick resp
  37. case 27: quickResp();
  38. break;
  39. // A - Auto skin rotator
  40. case 65:
  41. rotate = !rotate;
  42. rotateSkin();
  43. break;
  44. // Q - Quit to menu
  45. case 81: quit();
  46. break;
  47. // S - Change skin
  48. case 83: changeSkin();
  49. break;
  50. // Z - Reset zoom
  51. case 90: resetZoom();
  52. break;
  53. }
  54. }, false);
  55. // Hijack console log
  56. /*
  57. if (w.console) {
  58. w.console.logOld = console.log;
  59. w.console.log = getConsoleLog;
  60. }
  61. */
  62. // Set menu
  63. setMenu();
  64. // Set leaderboard
  65. setLeaderboard();
  66. // Set graphics
  67. setGraphics();
  68. // Update loop
  69. updateLoop();
  70. // Show FPS
  71. showFPS();
  72. }
  73. // Append DIV
  74. function appendDiv(id, className, style) {
  75. var div = document.createElement("div");
  76. if (id) {
  77. div.id = id;
  78. }
  79. if (className) {
  80. div.className = className;
  81. }
  82. if (style) {
  83. div.style = style;
  84. }
  85. document.body.appendChild(div);
  86. }
  87. // Zoom
  88. function zoom(e) {
  89. if (!w.gsc || !w.playing) {
  90. return;
  91. }
  92. w.gsc *= Math.pow(0.9, e.wheelDelta / -120 || e.detail / 2 || 0);
  93. }
  94. // Reset zoom
  95. function resetZoom() {
  96. w.gsc = 0.9;
  97. }
  98. // Get console log
  99. function getConsoleLog(log) {
  100. //w.console.logOld(log);
  101. if (log.indexOf("FPS") != -1) {
  102. gameFPS = log;
  103. }
  104. }
  105. // Set menu
  106. function setMenu() {
  107. var login = document.getElementById("login");
  108. if (login) {
  109. // Unblock skins
  110. w.localStorage.setItem("edttsg", "1");
  111. // Load settings
  112. loadSettings();
  113. // Keys info
  114. var div = document.createElement("div");
  115. div.style.width = "300px";
  116. div.style.color = "#56ac81";
  117. div.style.fontFamily = "'Lucida Sans Unicode', 'Lucida Grande', sans-serif";
  118. div.style.fontSize = "12px";
  119. div.style.textAlign = "center";
  120. div.style.opacity = "0.5";
  121. div.style.margin = "0 auto";
  122. div.style.padding = "10px 0";
  123. div.innerHTML = "[ESC] - Quick resp | [Q] - Quit to menu<br/>[A] - Auto skin rotator | [S] - Change skin<br/>[Z] - Reset zoom | [Space] - Boost";
  124. login.appendChild(div);
  125. // Menu container
  126. var sltMenu = document.createElement("div");
  127. sltMenu.style.width = "260px";
  128. sltMenu.style.color = "#8058D0";
  129. sltMenu.style.backgroundColor = "#1e262e";
  130. sltMenu.style.borderRadius = "29px";
  131. sltMenu.style.fontFamily = "'Lucida Sans Unicode', 'Lucida Grande', sans-serif";
  132. sltMenu.style.fontSize = "14px";
  133. sltMenu.style.textAlign = "center";
  134. sltMenu.style.margin = "0 auto 100px auto";
  135. sltMenu.style.padding = "10px 14px";
  136. sltMenu.innerHTML = "<strong>SlitherEngine</strong> | <strong>" + modVersion + "</strong>";
  137. login.appendChild(sltMenu);
  138. // IP input container
  139. var div = document.createElement("div");
  140. div.style.color = "#8058D0";
  141. div.style.backgroundColor = "#4C447C";
  142. div.style.borderRadius = "29px";
  143. div.style.margin = "10 auto";
  144. div.style.padding = "8px";
  145. sltMenu.appendChild(div);
  146. // IP input
  147. var input = document.createElement("input");
  148. input.id = "server-ip";
  149. input.type = "text";
  150. input.placeholder = "Server Address - ip and port";
  151. input.style.height = "24px";
  152. input.style.display = "inline-block";
  153. input.style.background = "none";
  154. input.style.color = "#e0e0ff";
  155. input.style.border = "none";
  156. input.style.outline = "none";
  157. div.appendChild(input);
  158. // Connect (play) button
  159. var button = document.createElement("input");
  160. button.id = "connect-btn";
  161. button.type = "button";
  162. button.value = "Play";
  163. button.style.height = "24px";
  164. button.style.display = "inline-block";
  165. button.style.borderRadius = "12px";
  166. button.style.color = "#FFF";
  167. button.style.backgroundColor = "#56ac81";
  168. button.style.border = "none";
  169. button.style.outline = "none";
  170. button.style.cursor = "pointer";
  171. button.style.padding = "0 20px";
  172. div.appendChild(button);
  173. // Select server container
  174. var div = document.createElement("div");
  175. div.style.backgroundColor = "#919191";
  176. div.style.borderRadius = "29px";
  177. div.style.margin = "10 auto";
  178. div.style.padding = "8px";
  179. sltMenu.appendChild(div);
  180. // Select server
  181. var select = document.createElement("select");
  182. select.id = "select-srv";
  183. select.style.background = "none";
  184. select.style.border = "none";
  185. select.style.outline = "none";
  186. var option = document.createElement("option");
  187. option.value = "";
  188. option.text = "___Select a server___";
  189. select.appendChild(option);
  190. div.appendChild(select);
  191. // Select graph container
  192. var div = document.createElement("div");
  193. div.style.backgroundColor = "#A5A5A5";
  194. div.style.borderRadius = "29px";
  195. div.style.margin = "10 auto";
  196. div.style.padding = "8px";
  197. sltMenu.appendChild(div);
  198. // Select graph
  199. var select = document.createElement("select");
  200. select.id = "select-graph";
  201. select.style.background = "none";
  202. select.style.border = "none";
  203. select.style.outline = "none";
  204. div.appendChild(select);
  205. var option = document.createElement("option");
  206. option.value = "3";
  207. option.text = "Graphics: Normal";
  208. select.appendChild(option);
  209. var option = document.createElement("option");
  210. option.value = "2";
  211. option.text = "Graphics: Optimized";
  212. select.appendChild(option);
  213. var option = document.createElement("option");
  214. option.value = "1";
  215. option.text = "Graphics: Low";
  216. select.appendChild(option);
  217. // Menu footer
  218. sltMenu.innerHTML += '<a href="http://vk.com/xagar" target="_blank" style="color: #56ac81;">SlitherEngine</a> | ';
  219. sltMenu.innerHTML += '<a href="https://www.youtube.com/channel/UCCXtULxo8XIjdRISXonsJpg/videos?&ab_channel=SICKER" target="_blank" style="color: #56ac81;">YT Channel</a>';
  220. // Get IP input
  221. inpIP = document.getElementById("server-ip");
  222. // Get nick
  223. var nick = document.getElementById("nick");
  224. nick.addEventListener("input", getNick, false);
  225. // Force connect
  226. var connectBtn = document.getElementById("connect-btn");
  227. connectBtn.onclick = forceConnect;
  228. // Get servers list
  229. getServersList();
  230. // Set graphic mode
  231. var selectGraph = document.getElementById("select-graph");
  232. if (renderMode == 1) {
  233. selectGraph.selectedIndex = 2;
  234. } else if (renderMode == 2) {
  235. selectGraph.selectedIndex = 1;
  236. } else {
  237. selectGraph.selectedIndex = 0;
  238. normalMode = true;
  239. }
  240. selectGraph.onchange = function() {
  241. var mode = selectGraph.value;
  242. if (mode) {
  243. renderMode = mode;
  244. w.localStorage.setItem("rendermode", renderMode);
  245. }
  246. };
  247. resizeView();
  248. } else {
  249. setTimeout(setMenu, 100);
  250. }
  251. }
  252. // Load settings
  253. function loadSettings() {
  254. if (w.localStorage.getItem("nick") != null) {
  255. var nick = w.localStorage.getItem("nick");
  256. document.getElementById("nick").value = nick;
  257. }
  258. if (w.localStorage.getItem("rendermode") != null) {
  259. var mode = parseInt(w.localStorage.getItem("rendermode"));
  260. if (mode >= 1 && mode <= 3) {
  261. renderMode = mode;
  262. }
  263. }
  264. if (w.localStorage.getItem("highscore") != null) {
  265. var score = parseFloat(w.localStorage.getItem("highscore"));
  266. if (score > 0) {
  267. highScore = score;
  268. }
  269. }
  270. }
  271. // Get nick
  272. function getNick() {
  273. var nick = document.getElementById("nick").value;
  274. w.localStorage.setItem("nick", nick);
  275. }
  276. // Connection status
  277. function connectionStatus() {
  278. if (!w.connecting || retry == 10) {
  279. w.forcing = false;
  280. retry = 0;
  281. return;
  282. }
  283. retry++;
  284. setTimeout(connectionStatus, 1000);
  285. }
  286. // Force connect
  287. function forceConnect() {
  288. if (inpIP.value.length == 0 || !w.connect) {
  289. return;
  290. }
  291. w.forcing = true;
  292. if (!w.bso) {
  293. w.bso = {};
  294. }
  295. var srv = inpIP.value.trim().split(":");
  296. w.bso.ip = srv[0];
  297. w.bso.po = srv[1];
  298. w.connect();
  299. setTimeout(connectionStatus, 1000);
  300. }
  301. // Get servers list
  302. function getServersList() {
  303. if (w.sos && w.sos.length > 0) {
  304. var selectSrv = document.getElementById("select-srv");
  305. for (var i = 0; i < sos.length; i++) {
  306. var srv = sos[i];
  307. var option = document.createElement("option");
  308. option.value = srv.ip + ":" + srv.po;
  309. option.text = (i + 1) + ". " + option.value;
  310. selectSrv.appendChild(option);
  311. }
  312. selectSrv.onchange = function() {
  313. var srv = selectSrv.value;
  314. inpIP.value = srv;
  315. };
  316. } else {
  317. setTimeout(getServersList, 100);
  318. }
  319. }
  320. // Resize view
  321. function resizeView() {
  322. if (w.resize) {
  323. w.lww = 0; // Reset width (force resize)
  324. w.wsu = 0; // Clear ad space
  325. w.resize();
  326. var wh = Math.ceil(w.innerHeight);
  327. if (wh < 800) {
  328. var login = document.getElementById("login");
  329. w.lgbsc = wh / 800;
  330. login.style.top = - (Math.round(wh * (1 - w.lgbsc) * 1E5) / 1E5) + "px";
  331. if (w.trf) {
  332. w.trf(login, "scale(" + w.lgbsc + "," + w.lgbsc + ")");
  333. }
  334. }
  335. } else {
  336. setTimeout(resizeView, 100);
  337. }
  338. }
  339. // Set leaderboard
  340. function setLeaderboard() {
  341. if (w.lbh) {
  342. w.lbh.textContent = "Leaderboard";
  343. w.lbh.style.fontSize = "20px";
  344. } else {
  345. setTimeout(setLeaderboard, 100);
  346. }
  347. }
  348. // Set normal mode
  349. function setNormalMode() {
  350. normalMode = true;
  351. w.ggbg = true;
  352. if (!w.bgp2 && bgImage) {
  353. w.bgp2 = bgImage;
  354. }
  355. w.render_mode = 2;
  356. }
  357. // Set graphics
  358. function setGraphics() {
  359. if (renderMode == 3) {
  360. if (!normalMode) {
  361. setNormalMode();
  362. }
  363. return;
  364. }
  365. if (normalMode) {
  366. normalMode = false;
  367. }
  368. if (w.want_quality && w.want_quality != 0) {
  369. w.want_quality = 0;
  370. w.localStorage.setItem("qual", "0");
  371. w.grqi.src = "/s/lowquality.png";
  372. }
  373. if (w.ggbg && w.gbgmc) {
  374. w.ggbg = false;
  375. }
  376. if (w.bgp2) {
  377. bgImage = w.bgp2;
  378. w.bgp2 = null;
  379. }
  380. if (w.high_quality) {
  381. w.high_quality = false;
  382. }
  383. if (w.gla && w.gla != 0) {
  384. w.gla = 0;
  385. }
  386. if (w.render_mode && w.render_mode != renderMode) {
  387. w.render_mode = renderMode;
  388. }
  389. }
  390. // Quick resp
  391. function quickResp() {
  392. w.dead_mtm = 0;
  393. w.login_fr = 0;
  394. forceConnect();
  395. }
  396. // Quit to menu
  397. function quit() {
  398. if (w.playing && w.resetGame) {
  399. w.want_close_socket = true;
  400. w.dead_mtm = 0;
  401. if (w.play_btn) {
  402. w.play_btn.setEnabled(true);
  403. }
  404. w.resetGame();
  405. }
  406. }
  407. // Change skin
  408. function changeSkin() {
  409. if (w.playing && w.snake != null) {
  410. var skin = w.snake.rcv,
  411. max = w.max_skin_cv || 25;
  412. skin++;
  413. if (skin > max) {
  414. skin = 0;
  415. }
  416. w.setSkin(w.snake, skin);
  417. }
  418. }
  419. // Rotate skin
  420. function rotateSkin() {
  421. if (!rotate) {
  422. return;
  423. }
  424. changeSkin();
  425. setTimeout(rotateSkin, 500);
  426. }
  427. // Set high score
  428. function setHighScore() {
  429. if (!w.snake || !w.fpsls || !w.fmlts) {
  430. return;
  431. }
  432. var currentScore = Math.floor(150 * (w.fpsls[w.snake.sct] + w.snake.fam / w.fmlts[w.snake.sct] - 1) - 50) / 10;
  433. if (currentScore > highScore) {
  434. highScore = currentScore;
  435. w.localStorage.setItem("highscore", highScore);
  436. }
  437. if (scoreHUD && highScore > 0) {
  438. scoreHUD.textContent = "Best score: " + highScore;
  439. }
  440. }
  441. // Show FPS
  442. function showFPS() {
  443. if (w.playing && fpsHUD && w.fps && w.lrd_mtm) {
  444. if (Date.now() - w.lrd_mtm > 970) {
  445. fpsHUD.textContent = "FPS: " + w.fps;
  446. }
  447. }
  448. setTimeout(showFPS, 30);
  449. }
  450. // Update loop
  451. function updateLoop() {
  452. setGraphics();
  453. setHighScore();
  454. if (w.playing) {
  455. if (positionHUD) {
  456. positionHUD.textContent = "X: " + (~~w.view_xx || 0) + " Y: " + (~~w.view_yy || 0);
  457. }
  458. if (inpIP && w.bso && currentIP != w.bso.ip + ":" + w.bso.po) {
  459. currentIP = w.bso.ip + ":" + w.bso.po;
  460. inpIP.value = currentIP;
  461. if (ipHUD) {
  462. ipHUD.textContent = "Server: " + currentIP;
  463. }
  464. }
  465. } else {
  466. positionHUD.textContent = "";
  467. fpsHUD.textContent = "";
  468. }
  469. setTimeout(updateLoop, 1000);
  470. }
  471. // Init
  472. init();
  473. })(window);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement