Guest User

/r/morocco

a guest
Apr 2nd, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var colors = [
  2. 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,10,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,10,10,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,10,5,10,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,10,5,5,10,5,5,5,5,5,5,5,5,5,5,10,10,10,10,10,10,10,10,10,10,10,10,10,10,5,5,5,5,5,10,10,5,5,10,5,5,5,5,10,5,10,10,5,5,5,5,5,5,5,5,10,10,5,5,5,5,5,10,10,5,5,5,5,5,5,5,5,5,5,5,10,10,10,5,5,10,5,10,5,5,5,5,5,5,5,5,5,5,10,5,5,5,10,10,5,5,10,5,5,5,5,5,5,5,5,5,5,10,5,10,10,5,5,10,10,5,10,5,5,5,5,5,5,5,5,10,10,10,5,5,5,5,5,5,10,10,5,5,5,5,5,5,5,5,10,5,5,5,5,5,5,5,5,5,5,10,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5
  3. ];
  4. var colorsABGR = [];
  5.  
  6. var babaevski = {
  7.   x: 979,
  8.   y: 737,
  9.   width: 19,
  10.   height: 14
  11. };
  12.  
  13. var perfect_timeout = 60;
  14.  
  15. // hooks
  16. var client;
  17. var canvasse;
  18. var jQuery;
  19.  
  20. var currentDrawPosition = 0;
  21. var currentLoop = 0;
  22.  
  23. r.placeModule("babaevski", function(e){
  24.   client = e("client");
  25.   canvasse = e("canvasse");
  26.   jQuery = e("jQuery");
  27.  
  28.   for(var i=0; i<client.palette.length; i++){
  29.     colorsABGR[i] = client.getPaletteColorABGR(i);
  30.   }
  31.  
  32.   attempt();
  33.  
  34. });
  35.  
  36. function attempt(){
  37.   console.log("tried to draw");  
  38.   var toWait = client.getCooldownTimeRemaining();
  39.   if(toWait === 0)
  40.   {
  41.     tryDrawPixel();
  42.   }
  43.   else
  44.   {
  45.     setTimeout(attempt, toWait + Math.round(Math.random() * 1500));
  46.     console.log("set a timeout");
  47.   }
  48. }
  49.  
  50. function tryDrawPixel()
  51. {
  52.   var toWait = client.getCooldownTimeRemaining();
  53.   if(toWait === 0 && currentLoop < babaevski.width*babaevski.height)
  54.   {
  55.     console.log("checking pixel " + currentDrawPosition);
  56.     var targetPoint = getPoint(currentDrawPosition);
  57.    
  58.     $.get( "/api/place/pixel.json", { x:targetPoint.x, y:targetPoint.y }, function( data )
  59.     {
  60.       console.log("x" + targetPoint.x + " y"+ targetPoint.y);
  61.       if (data.color == undefined)
  62.         data.color = 0;
  63.       canvasse.drawTileAt(data.x, data.y, colorsABGR[data.color]);
  64.       if (colors[currentDrawPosition] != data.color && colors[currentDrawPosition] !== -1)
  65.       {
  66.         client.setColor(colors[currentDrawPosition]);
  67.         client.drawTile(targetPoint.x, targetPoint.y);
  68.         console.log("drew pixel at " + targetPoint.x + "/" + targetPoint.y);
  69.         var toWait = client.getCooldownTimeRemaining();
  70.         setTimeout(attempt, toWait + Math.round(Math.random() * 1500));
  71.         currentLoop = 0;
  72.         console.log("set a timeout");
  73.       }
  74.       else
  75.       {
  76.         currentDrawPosition++;
  77.         if (currentDrawPosition >= babaevski.width*babaevski.height)
  78.           currentDrawPosition = 0;
  79.         tryDrawPixel(currentDrawPosition);
  80.       }
  81.     });
  82.   }
  83.   else
  84.   {
  85.     var added_timeout = 0;
  86.     if (currentLoop == babaevski.width*babaevski.height)
  87.       added_timeout = perfect_timeout*1000;
  88.     setTimeout(attempt, toWait + Math.round(Math.random() * 1500) + added_timeout);
  89.     currentLoop = 0;
  90.     console.log("set a timeout");
  91.   }
  92.  
  93. }
  94.  
  95. function drawTest(){
  96.   for(var i=0; i < babaevski.width*babaevski.height; i++){
  97.     if(colors[i] === -1){
  98.       continue;
  99.     }
  100.     var targetPoint = getPoint(i);
  101.     canvasse.drawTileAt(targetPoint.x, targetPoint.y, colorsABGR[colors[i]]);
  102.   }
  103. }
  104.  
  105. function getPoint(i){
  106.   var x = i % babaevski.width;
  107.   return {
  108.     x: babaevski.x + x,
  109.     y: babaevski.y + (i - x) / babaevski.width - babaevski.height
  110.   };
  111. }
  112.  
  113. function getPixel(x, y){
  114.   return canvasse.writeBuffer[canvasse.getIndexFromCoords(x, y)];
  115. }
Advertisement
Add Comment
Please, Sign In to add comment