Advertisement
Guest User

Spreadshot Script

a guest
Oct 14th, 2019
10,357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.06 KB | None | 0 0
  1.  
  2. (function(){//info
  3. if(window.updateInfo) return;
  4.  
  5.  
  6. var info = {};
  7. var info_container = document.createElement("div");
  8. info_container.style.position = "fixed";
  9. info_container.style.color = "white";
  10. info_container.style["pointer-events"] = "none";
  11. document.body.appendChild(info_container);
  12.  
  13. function toggle_info_container(e){
  14. if(e.key == "i"){
  15. info_container.style.display = info_container.style.display=="block" ? "none" : "block";
  16. }
  17. }
  18. window.addEventListener("keyup", toggle_info_container);
  19.  
  20. window.updateInfo = function(key, value){
  21. if(!value) delete info[key];
  22. else info[key] = value;
  23. var s = "";
  24. for(var _key in info){
  25. s += info[_key] + "\n";
  26. }
  27. info_container.innerText = s;
  28. };
  29. })();
  30.  
  31. function MeasureCycle(){
  32. var canvas = document.getElementById("canvas");
  33. var ctx = canvas.getContext("2d");
  34. var real_arc = ctx.arc;
  35. var real_setTransform = ctx.setTransform;
  36.  
  37. var a;
  38. var tx = 0, ty = 0;
  39. var a11 = 1;
  40.  
  41. var state = false;
  42. var found = false;
  43. var inA = null;
  44. var direction = 1;
  45.  
  46. var frameRequest;
  47. var intervalEMA = null; // ms
  48.  
  49. function arc(){
  50. real_arc.apply(ctx, arguments);
  51.  
  52. if(!found){
  53. var aimX = window.innerWidth / 2 + 50 * direction;
  54. var aimY = window.innerHeight / 2;
  55. found = (tx - a11 < aimX) && (tx + a11 > aimX) && (ty - a11 < aimY) && (ty + a11 > aimY);
  56. }
  57. }
  58.  
  59. function setTransform(b11, b12, b21, b22, bx, by){
  60. real_setTransform.apply(ctx, arguments);
  61. tx = bx, ty = by, a11 = b11;
  62. }
  63.  
  64. function onFrame(_a){
  65. frameRequest = window.requestAnimationFrame(onFrame);
  66. a = _a;
  67. if(!state && found){
  68. if(inA){
  69. var da = a - inA;
  70. inA = a;
  71. intervalEMA = intervalEMA ? 0.8 * intervalEMA + 0.2 * da : da;
  72. window.updateInfo && window.updateInfo(
  73. "intervalEMA",
  74. "平均発射間隔: " + intervalEMA.toString().substr(0, 5) + "ms"
  75. );
  76.  
  77. }else{
  78. inA = a;
  79. }
  80. }
  81. state = found;
  82. found = false;
  83. }
  84.  
  85. function onMouseEvent(e){
  86. e.stopPropagation();
  87. }
  88.  
  89. this.start = function(_direction){
  90. _direction = _direction || 1;
  91. direction = _direction > 0 ? 1 : -1;
  92. inA = null;
  93. intervalEMA = null;
  94. state = found = false;
  95.  
  96. ctx.setTransform = setTransform;
  97. ctx.arc = arc;
  98.  
  99. var aimX = window.innerWidth / 2 + 50 * direction;
  100. var aimY = window.innerHeight / 2;
  101. canvas.dispatchEvent(new MouseEvent("mousemove", {clientX: aimX, clientY: aimY}));
  102. canvas.dispatchEvent(new MouseEvent("mousedown", {clientX: aimX, clientY: aimY}));
  103.  
  104. window.addEventListener("mousemove", onMouseEvent, true);
  105. window.addEventListener("mouseup", onMouseEvent, true);
  106. window.addEventListener("mousedown", onMouseEvent, true);
  107. frameRequest = window.requestAnimationFrame(onFrame);
  108.  
  109. window.updateInfo && window.updateInfo("measuring", "連射速度測定モード");
  110. }
  111.  
  112. this.terminate = function(){
  113. ctx.setTransform = real_setTransform;
  114. ctx.arc = real_arc;
  115.  
  116. window.removeEventListener("mousemove", onMouseEvent, true);
  117. window.removeEventListener("mousedown", onMouseEvent, true);
  118. window.removeEventListener("mouseup", onMouseEvent, true);
  119. window.cancelAnimationFrame(frameRequest);
  120.  
  121. canvas.dispatchEvent(new MouseEvent("mouseup", {clientX: 10, clientY: 10}));
  122.  
  123. window.updateInfo && window.updateInfo("measuring", null);
  124. return intervalEMA;
  125. }
  126. };
  127.  
  128. (function(){
  129. var cycleRate = 0.003125; // ms^-1
  130. var maxAngle = Math.PI * 45 / 180;
  131. var NCANNON = 3;
  132. var angleUnit = maxAngle / (NCANNON - 1);
  133.  
  134. var tankData = [
  135. {name: "Penta", cycleRate: 0.003125, maxAngle: Math.PI * 45 / 180, NCANNON: 3},
  136. {name: "SpreadShot", cycleRate: 0.001555, maxAngle: Math.PI * 75 / 180, NCANNON: 6},
  137. {name: "Octo", cycleRate: 0.003125, maxAngle: Math.PI * 45 / 180, NCANNON: 2}
  138. ];
  139. var tankIndex = 0;
  140.  
  141. var measure = new MeasureCycle();
  142. var measuring = false;
  143.  
  144. var effective = false;
  145. var frameRequest;
  146.  
  147. var canvas = window.document.getElementById("canvas");
  148.  
  149. var mouseX;
  150. var mouseY;
  151. var a = 0;
  152. var startA = 0;
  153. var artificialMouseMove = false;
  154.  
  155. var disabled = false;
  156.  
  157. function onMouseDown(e){
  158. if(e.button == 2){
  159. if(!effective){
  160. startA = a - 50;
  161. mouseX = e.clientX;
  162. mouseY = e.clientY;
  163. canvas.dispatchEvent(new MouseEvent("mousedown", {clientX: mouseX, clientY: mouseY}));
  164. }
  165. effective = true;
  166. }
  167. }
  168.  
  169. function onMouseUp(e){
  170. if(e.button == 2){
  171. if(effective){
  172. canvas.dispatchEvent(new MouseEvent("mouseup", {clientX: mouseX, clientY: mouseY}));
  173. }
  174. effective = false;
  175. }
  176. }
  177.  
  178. function onMouseMove(e){
  179. if(effective){
  180. if(!artificialMouseMove){
  181. e.stopPropagation();
  182. mouseX = e.clientX;
  183. mouseY = e.clientY;
  184. }
  185. }else{
  186. mouseX = e.clientX;
  187. mouseY = e.clientY;
  188. }
  189. }
  190.  
  191. function update(_a){
  192. frameRequest = window.requestAnimationFrame(update);
  193. a = _a;
  194.  
  195. if(effective){
  196. var da = a - startA;
  197. var state = Math.floor(cycleRate * da * NCANNON) % (NCANNON * 2);
  198. var state1 = state % NCANNON;
  199. var state2 = Math.floor(state / NCANNON);
  200. var angle = angleUnit * state1 * (state1 % 2 == state2 ? 1 : -1);
  201.  
  202. var cx = window.innerWidth / 2;
  203. var cy = window.innerHeight / 2;
  204. var sin = Math.sin(angle);
  205. var cos = Math.cos(angle);
  206.  
  207. var x = mouseX - cx;
  208. var y = mouseY - cy;
  209. var _x = cos * x - sin * y;
  210. var _y = sin * x + cos * y;
  211. x = _x + cx;
  212. y = _y + cy;
  213.  
  214. artificialMouseMove = true;
  215. canvas.dispatchEvent(new MouseEvent("mousemove", {clientX: x, clientY: y}));
  216. artificialMouseMove = false;
  217. }
  218. }
  219.  
  220. function onKeyUp(e){
  221. if(e.key == "Q"){
  222. disabled = !disabled;
  223. if(disabled){
  224. if(measuring){
  225. cycleRate = 1 / measure.terminate();
  226. measuring = false;
  227. } else stop();
  228. }else start();
  229. window.updateInfo && window.updateInfo("off", disabled ? "機能オフ" : null);
  230. return;
  231. }
  232.  
  233. if(disabled) return;
  234.  
  235. if(e.key == "M"){
  236. if(measuring){
  237. cycleRate = 1 / measure.terminate();
  238. start();
  239. measuring = false;
  240. }else{
  241. stop();
  242. measure.start(mouseX - window.innerWidth / 2);
  243. measuring = true;
  244. }
  245. }else if(e.key == "T"){
  246. changeTank((tankIndex + 1) % tankData.length);
  247. }
  248. }
  249.  
  250. function changeTank(index){
  251. var data = tankData[index];
  252. tankIndex = index;
  253.  
  254. cycleRate = data.cycleRate; // ms^-1
  255. maxAngle = data.maxAngle;
  256. NCANNON = data.NCANNON;
  257. angleUnit = maxAngle / (NCANNON - 1);
  258. // window.updateInfo && window.updateInfo("changeTank", "Tank: " + data.name);
  259. }
  260.  
  261. function init(){
  262. window.addEventListener("keyup", onKeyUp);
  263. start();
  264. changeTank(0);
  265. }
  266.  
  267. function start(){
  268. canvas.addEventListener("mousedown", onMouseDown);
  269. canvas.addEventListener("mouseup", onMouseUp);
  270. window.addEventListener("mousemove", onMouseMove, true);
  271. frameRequest = window.requestAnimationFrame(update);
  272. }
  273.  
  274. function stop(){
  275. canvas.removeEventListener("mousedown", onMouseDown);
  276. canvas.removeEventListener("mouseup", onMouseUp);
  277. window.removeEventListener("mousemove", onMouseMove, true);
  278. window.cancelAnimationFrame(frameRequest);
  279. effective = false;
  280. }
  281.  
  282.  
  283. init();
  284.  
  285. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement