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,3,8,3,8,8,8,3,8,8,3,8,3,8,8,8,3,3,3,8,3,8,3,8,8,3,
  8. 3,8,3,8,8,3,8,8,8,3,8,8,8,3,8,3,8,3,3,3,8,3,3,3,8,8,3,8,8,8,8,3,8,8,8,3,3,3,8,3,3,3,8,3,8,3,8,3,3,3,8,3,8,8,3,8,3,3,3,8,3,8,8,3,8,3,3,3,8,3,8,3,8,3,3,8,8,8,3,
  9. 3,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,3,
  10. 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
  11. ];
  12. var colorsABGR = [];
  13.  
  14.  
  15. var image = {
  16. x: 281,
  17. y: 648,
  18. width: 79,
  19. height: 9
  20. };
  21.  
  22. var perfect_timeout = 60;
  23.  
  24. //update timeout for poor reddit servers (ms)
  25. var get_delay = 500;
  26.  
  27.  
  28. var use_skipping = false;
  29. var skipping_max = 20;
  30.  
  31.  
  32.  
  33. var client;
  34. var canvasse;
  35. var jQuery;
  36.  
  37.  
  38. var currentDrawPosition = 2;
  39.  
  40. var currentLoop = 0;
  41.  
  42. r.placeModule("image", function(e){
  43. client = e("client");
  44. canvasse = e("canvasse");
  45. jQuery = e("jQuery");
  46.  
  47. for(var i=0; i<client.palette.length; i++){
  48. colorsABGR[i] = client.getPaletteColorABGR(i);
  49. }
  50.  
  51. startup();
  52.  
  53. });
  54.  
  55.  
  56. function startup()
  57. {
  58. console.log("skipping to first wrong pixel");
  59. var toWait = client.getCooldownTimeRemaining();
  60. for(var i = currentDrawPosition; i < image.width*image.height; i++){
  61. if(colors[i] === -1){
  62. continue;
  63. }
  64. var targetPoint = getPoint(i);
  65. var pixelColor = getPixel(targetPoint.x, targetPoint.y);
  66. if(pixelColor !== colorsABGR[colors[i]]){
  67. currentDrawPosition = i;
  68. console.log("starting at " + currentDrawPosition);
  69. break;
  70. }
  71. }
  72. setTimeout(attempt, toWait + Math.round(Math.random() * 1500));
  73.  
  74. }
  75.  
  76.  
  77.  
  78. function attempt(){
  79. console.log("tried to draw");
  80. var toWait = client.getCooldownTimeRemaining();
  81. if(toWait === 0)
  82. {
  83. tryDrawPixel();
  84. }
  85. else
  86. {
  87. setTimeout(attempt, toWait + Math.round(Math.random() * 1500));
  88. console.log("set a timeout");
  89. }
  90. }
  91.  
  92. function tryDrawPixel()
  93. {
  94. var toWait = client.getCooldownTimeRemaining();
  95. if(toWait === 0 && currentLoop < image.width*image.height)
  96. {
  97. currentLoop++;
  98. console.log("checking pixel " + currentDrawPosition);
  99. var targetPoint = getPoint(currentDrawPosition);
  100.  
  101. $.get( "/api/place/pixel.json", { x:targetPoint.x, y:targetPoint.y }, function( data )
  102. {
  103. setTimeout(function() {
  104. //wait an extra half second for reddits servers
  105.  
  106.  
  107. console.log("x" + targetPoint.x + " y"+ targetPoint.y);
  108. if (data.color == undefined)
  109. data.color = 0;
  110. canvasse.drawTileAt(data.x, data.y, colorsABGR[data.color]);
  111. if (colors[currentDrawPosition] != data.color && colors[currentDrawPosition] !== -1)
  112. {
  113. client.setColor(colors[currentDrawPosition]);
  114. client.drawTile(targetPoint.x, targetPoint.y);
  115. console.log("drew pixel at " + targetPoint.x + "/" + targetPoint.y);
  116. var toWait = client.getCooldownTimeRemaining();
  117. setTimeout(attempt, toWait + Math.round(Math.random() * 1500));
  118. currentLoop = 0;
  119. console.log("set a timeout");
  120. }
  121. else
  122. {
  123. var positionIncrease = 1;
  124. if (use_skipping == true)
  125. {
  126. positionIncrease = Math.floor(Math.random() * (Math.min(0.2*currentLoop, skipping_max))) + 1;
  127. }
  128. currentDrawPosition += positionIncrease;
  129. if (currentDrawPosition >= image.width*image.height)
  130. currentDrawPosition = 0;
  131. tryDrawPixel(currentDrawPosition);
  132. }
  133. }, get_delay);
  134. });
  135. }
  136. else
  137. {
  138. var added_timeout = 0;
  139. if (currentLoop == image.width*image.height)
  140. added_timeout = perfect_timeout*1000;
  141. setTimeout(attempt, toWait + Math.round(Math.random() * 1500) + added_timeout);
  142. currentLoop = 0;
  143. console.log("set a timeout");
  144. }
  145.  
  146. }
  147.  
  148. function drawTestImage(){
  149. for(var i=0; i < image.width*image.height; i++){
  150. if(colors[i] === -1){
  151. continue;
  152. }
  153. var targetPoint = getPoint(i);
  154. canvasse.drawTileAt(targetPoint.x, targetPoint.y, colorsABGR[colors[i]]);
  155. }
  156. }
  157.  
  158. function getPoint(i){
  159. var x = i % image.width;
  160. return {
  161. x: image.x + x,
  162. y: image.y + (i - x) / image.width - image.height
  163. };
  164. }
  165.  
  166. function getPixel(x, y){
  167. return canvasse.writeBuffer[canvasse.getIndexFromCoords(x, y)];
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement