Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * $Id: ruine_explorer.user.js 75 2013-05-13 00:00:00Z jolan $
  3.  * Copyright (c) 2008-2010 JC Plessis
  4.  * Released under the GPL license
  5.  *
  6.  *
  7.  * @author  jcplessis <jcplessis@gmail.com>
  8.  * @license http://www.gnu.org/copyleft/gpl.html  GNU General Public License
  9.  * @charset UTF-8
  10.  */
  11. // ==UserScript==
  12. // @name           RuineExplorer
  13. // @namespace      http://jplessis.free.fr/
  14. // @description    Permet de tracer les cartes dans les ruines de hordes.fr
  15. // @include        http://www.hordes.fr/*
  16. // @include        http://jcplessis.alwaysdata.net/hordes/ruine_explorer/ruine_simulator.html
  17. // @version        0.0.8
  18. // @grant       GM_getValue
  19. // @grant       GM_setValue
  20. // @grant       GM_deleteValue
  21. // ==/UserScript==
  22.  
  23.  
  24. console.log('Hello RuineExplorer');
  25.  
  26. CELL_SIZE=16;
  27. CELL_NB=16;
  28.  
  29. current_position = [0, 0]
  30. cells = []
  31.  
  32. function init_cells(){
  33.     for(var i =0; i < CELL_NB; i++){
  34.         var line = []
  35.         for(var j = 0; j < CELL_NB; j++){
  36.             line.push({});  
  37.         }
  38.         cells.push(line)
  39.     }
  40.     get_cell(0, 0).visited=true;
  41.     get_cell(0, 0).has_north=true;
  42. }
  43.  
  44. init_cells();
  45.  
  46. CANVAS_WIDTH=CELL_SIZE*CELL_NB;
  47.  
  48. var root = document.createElement('div');
  49. root.setAttribute('id', 'ruine_explorer');
  50. root.setAttribute('style', 'position: absolute; bottom: 5;');
  51. root.innerHTML = '<style> button {  background-image : url("http://jcplessis.alwaysdata.net/hordes/ruine_explorer/button.gif"); border : 1px solid black;   color : #f0d79e;}</style>' +
  52.     '<div id="map" style="display:none; border:3px solid #f0d79e; background:#9C6845; padding : 5px; " >' +
  53.     '<button onclick="document.dispatchEvent(new CustomEvent(\'clear_map\'));">Effacer la carte</button>' +
  54.     '<button onclick="document.dispatchEvent(new CustomEvent(\'add_exit\', {\'detail\':\'north\'}));"><img src="http://jcplessis.alwaysdata.net/hordes/ruine_explorer/north.png" /></button>' +
  55.     '<button onclick="document.dispatchEvent(new CustomEvent(\'add_exit\', {\'detail\':\'south\'}));"><img src="http://jcplessis.alwaysdata.net/hordes/ruine_explorer/south.png" /></button>' +
  56.     '<button onclick="document.dispatchEvent(new CustomEvent(\'add_exit\', {\'detail\':\'east\'}));"><img src="http://jcplessis.alwaysdata.net/hordes/ruine_explorer/east.png" /></button>' +
  57.     '<button onclick="document.dispatchEvent(new CustomEvent(\'add_exit\', {\'detail\':\'west\'}));"><img src="http://jcplessis.alwaysdata.net/hordes/ruine_explorer/west.png" /></button><br/>' +
  58.     '<canvas id="ruine_canvas" width="' + CANVAS_WIDTH + 'px" height="' + CANVAS_WIDTH + 'px"></canvas>' +
  59.     '</div>' +
  60.     '<br/><a style="display:inline-block; margin:1px; border:3px solid #f0d79e; background:#9C6845; padding : 5px; color:f0d79e; text-decoration:  none; " href="#" onclick="document.dispatchEvent(new CustomEvent(\'hide_show_map\')); return false;">RUINE EXPLORER</a>';
  61. document.body.appendChild(root);
  62.  
  63. function createCookie(name,value) {
  64.     setTimeout(function() {
  65.         GM_setValue(name, JSON.stringify(value));
  66.     }, 0);
  67. }
  68.  
  69. function readCookie(name) {
  70.     return JSON.parse(GM_getValue(name));
  71. }
  72.  
  73. function eraseCookie(name) {
  74.     GM_deleteValue(name);
  75. }
  76.  
  77. context = null;
  78. function get_canvas(){
  79.     if (context == null){
  80.         console.log("Create canvas");
  81.         var c=document.getElementById("ruine_canvas");
  82.         context = c.getContext("2d");
  83.         context.globalAlpha=1;
  84.         context.translate(0.5, 0.5);
  85.     }
  86.     return context;
  87. }
  88.  
  89. function draw_line(p1, p2, color) {
  90.     var ctx = get_canvas();
  91.     get_canvas().strokeStyle = color;
  92.     ctx.beginPath();
  93.     ctx.moveTo(p1[0],p1[1]);
  94.     ctx.lineTo(p2[0],p2[1]);
  95.     ctx.stroke();  
  96. }
  97.  
  98. function draw_open(){
  99.     draw_line([2, 0], [2, 2]);
  100.     draw_line([CELL_SIZE-2, 0], [CELL_SIZE-2, 2]);  
  101. }
  102. function draw_wall(){
  103.     draw_line([2, 2], [CELL_SIZE-2, 2]);  
  104. }
  105.  
  106. function draw_cell(has_opening, rotation){
  107.     var canvas = get_canvas();
  108.     canvas.save();
  109.     canvas.translate(CELL_SIZE / 2, CELL_SIZE / 2);
  110.     canvas.rotate(rotation);
  111.     canvas.translate(CELL_SIZE / -2, CELL_SIZE / -2);
  112.     if(has_opening){
  113.         draw_open();
  114.     }else{
  115.         draw_wall();  
  116.     }
  117.     canvas.restore();
  118. }
  119.  
  120. function draw_img(src, x, y){
  121.     var img=new Image();
  122.     img.canvas_x = x;
  123.     img.canvas_y = y;
  124.     img.addEventListener('load', function(param) {
  125.         var canvas = get_canvas();
  126.         canvas.save();
  127.         canvas.translate(CANVAS_WIDTH/2 + this.canvas_x*CELL_SIZE, this.canvas_y*CELL_SIZE);
  128.         canvas.drawImage(this, 2, 2, CELL_SIZE - 4, CELL_SIZE - 4);
  129.         canvas.restore();
  130.         draw_current_location();
  131.     });
  132.     img.src=src
  133. }
  134.  
  135. function draw_square(x, y, cell){
  136.     var canvas = get_canvas();
  137.     canvas.save();
  138.     canvas.beginPath();
  139.     canvas.translate(CANVAS_WIDTH/2 + x*CELL_SIZE, y*CELL_SIZE);
  140.     canvas.strokeStyle = "Black";
  141.     if (cell.has_zombie){
  142.         canvas.beginPath();
  143.         canvas.fillStyle = "#E01B5D";
  144.         canvas.fillRect(2, 2, CELL_SIZE-4, CELL_SIZE-4);
  145.     }
  146.     if (cell.has_room){
  147.         draw_img('http://jcplessis.alwaysdata.net/hordes/ruine_explorer/small_enter.gif', x, y);
  148.     }else {
  149.         if (cell.has_locked){
  150.             draw_img('http://jcplessis.alwaysdata.net/hordes/ruine_explorer/item_lock.gif', x, y);
  151.         }
  152.     }
  153.    
  154.     draw_cell(cell.has_north, 0)
  155.     draw_cell(cell.has_south, Math.PI)
  156.     draw_cell(cell.has_east, Math.PI / 2)
  157.     draw_cell(cell.has_west, 3 * Math.PI / 2)
  158.     canvas.restore();
  159.    
  160. }
  161.  
  162. function draw_current_location(){
  163.     var x = current_position[0]
  164.     var y = current_position[1]
  165.     var canvas = get_canvas();
  166.     canvas.save();
  167.     canvas.fillStyle = "#F5D800";
  168.     canvas.translate(CANVAS_WIDTH/2 + (x+0.5)*CELL_SIZE, (y+0.25)*CELL_SIZE);
  169.     canvas.rotate(Math.PI / 4);
  170.     canvas.fillRect(0, 0, CELL_SIZE * 0.4, CELL_SIZE*0.4);
  171.     canvas.restore();
  172. }
  173.  
  174. function treat_events(){
  175.     var canvas = get_canvas();
  176.     canvas.clearRect(0, 0, 500, 500);
  177.     for(var i = CELL_NB / -2; i < CELL_NB / 2; i++){
  178.         for(var j = 0; j < CELL_NB; j++){
  179.             cell = get_cell(i, j);
  180.             if (cell.visited){
  181.                 draw_square(i, j, cell);  
  182.             }
  183.         }
  184.     }
  185.     draw_current_location();
  186.    
  187. }
  188.  
  189. function init() {
  190.     js.XmlHttp._ruine_onEnd = js.XmlHttp.onEnd;
  191.     js.XmlHttp.onEnd = function() {
  192.         var url = this.urlForBack;
  193.         this._ruine_onEnd();
  194.         console.log('Ruine explorer url = ' + url);
  195.         var event = undefined;
  196.         if( /explo\/move\?x=(-?[0-9]+);y=(-?[0-9]+)/.test(url) ) {
  197.             var x = parseInt(RegExp.$1);
  198.             var y = parseInt(RegExp.$2);
  199.             console.log("Move " + x + "/" + y);
  200.             event = ["MOVE", x, y];
  201.         }
  202.         if( /explo\/flee/.test(url) ) {
  203.             console.log("Fuite !");
  204.             event = ["FLEE"];
  205.         }
  206.         if( /explo\/enterRoom/.test(url) || /explo\/searchRoom/.test(url) || /explo\/leaveRoom/.test(url)) {
  207.             console.log("Piece !");
  208.             event =["ROOM"];
  209.         }
  210.         if( /explo\/quit/.test(url)) {
  211.             console.log("Sortie !");
  212.             event =["QUIT"];
  213.         }
  214.         if( /explo\/unlockDoor/.test(url)) {
  215.             console.log("Porte verouillée !");
  216.             event =["LOCKED"];
  217.         }
  218.         if (event != undefined) {
  219.             var evt = new CustomEvent('ruine_event', {"detail":event});
  220.             document.dispatchEvent(evt);
  221.         }
  222.        
  223.     };
  224. }
  225.  
  226. function get_cell(x, y){
  227.     return cells[y][x+CELL_NB/2]  
  228. }
  229.  
  230. function get_current_cell(){
  231.     var x = current_position[0];
  232.     var y = current_position[1];
  233.     return get_cell(x, y);
  234. }
  235.  
  236. function handle_move(x, y, dx, dy){
  237.     var current_cell = get_cell(x, y);
  238.     current_cell.visited = true;
  239.     console.log("Handle move");
  240.     if (dx == 1){
  241.         current_cell.has_east = true;  
  242.     }
  243.     if (dx == -1){
  244.         current_cell.has_west = true;  
  245.     }
  246.     if (dy == -1){
  247.         current_cell.has_north = true;  
  248.     }
  249.     if (dy == 1){
  250.         current_cell.has_south = true;  
  251.     }
  252.     return current_cell;
  253. }
  254.  
  255. function treat_event(event){
  256.     event = event.detail
  257.     var x = current_position[0];
  258.     var y = current_position[1];
  259.     var current_cell = get_cell(x, y);
  260.     current_cell.visited = true;
  261.     console.log(event[0]);
  262.     switch (event[0]){
  263.         case "ROOM":
  264.             current_cell.has_room=true;
  265.             break;
  266.         case "FLEE":
  267.             current_cell.has_zombie=true;
  268.             break;
  269.         case "LOCKED":
  270.             current_cell.has_locked=true;
  271.             break;
  272.         case "MOVE":
  273.             console.log("Move " + x + "/" + y);
  274.             handle_move(x, y, event[1], event[2]);
  275.             x += event[1];
  276.             y += event[2];
  277.             handle_move(x, y, event[1] * -1, event[2] * -1);
  278.             current_position = [x, y]
  279.             break;
  280.     }
  281.     createCookie("cells", cells);
  282.     createCookie("current_position", current_position);
  283.     treat_events();
  284. }
  285. document.addEventListener("ruine_event", treat_event);
  286.  
  287. function realign_map(){
  288.     var map = document.getElementById("ruine_explorer");
  289.     var doc = document.documentElement, body = document.body;
  290.     var top = (doc && doc.scrollTop  || body && body.scrollTop  || 0);
  291.     map.style.bottom = (window.innerHeight - document.documentElement.clientHeight - top) + "px";
  292. }
  293.  
  294. function clear_map(event){
  295.     console.log("Clear map");
  296.     eraseCookie("cells");
  297.     eraseCookie("current_position");
  298.     current_position = [0, 0];
  299.     cells = [];
  300.     init_cells();
  301.     createCookie("cells", cells);
  302.     createCookie("current_position", current_position);
  303.     treat_events();
  304. }
  305. document.addEventListener("clear_map", clear_map);
  306.  
  307. function hide_show_map(event){
  308.     var map = document.getElementById("map");
  309.     if (window.getComputedStyle(map).getPropertyValue("display") == "none"){
  310.         map.style.display = "inline-block";    
  311.         createCookie("map_visible", true);
  312.     } else {
  313.         map.style.display = "none";
  314.         createCookie("map_visible", false);
  315.     }
  316.    
  317. }
  318. document.addEventListener("hide_show_map", hide_show_map);
  319.  
  320. function add_exit(event){
  321.     var direction = event.detail;
  322.     var cell = get_current_cell();
  323.     cell["has_" + direction] = true;
  324.     var x = 0;
  325.     var y = 0;
  326.     var new_direction = "";
  327.     if (direction == "south"){
  328.         new_direction = "north";
  329.         y = 1;
  330.     }
  331.     if (direction == "north"){
  332.         new_direction = "south";
  333.         y = -1;
  334.     }
  335.     if (direction == "east"){
  336.         new_direction = "west";
  337.         x = 1;
  338.     }
  339.     if (direction == "west"){
  340.         new_direction = "east";
  341.         x = -1;
  342.     }
  343.     console.log("Rha ?" + x + " " + y + " " + new_direction);
  344.     get_cell(current_position[0] + x, current_position[1] + y)["has_" + new_direction] = true;
  345.     createCookie("cells", cells);
  346.     treat_events();
  347. }
  348. document.addEventListener("add_exit", add_exit);
  349.  
  350. realign_map();
  351. window.onscroll = realign_map;
  352. try{
  353.     cells = readCookie("cells") || [];
  354.     current_position = readCookie("current_position") || [0, 0]
  355.     treat_events();
  356. }catch(err){
  357.     cells = [];
  358.     init_cells();
  359.     current_position = [0, 0];
  360. }
  361.  
  362. var map_visible = readCookie("map_visible", false);
  363. if (map_visible) {
  364.     hide_show_map();
  365. }
  366.  
  367. var script = document.createElement('script');
  368. script.setAttribute('id',   'hmu:script:init');
  369. script.setAttribute('type', 'application/javascript');
  370. root.appendChild(script);
  371. script.textContent = '(' + init.toString() + ')();';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement