Game of Life MPP

John Conway’s Game of Life in MPP ¯\_(ツ)_/¯

目前为 2022-12-17 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Game of Life MPP
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.3
  5. // @description John Conway’s Game of Life in MPP ¯\_(ツ)_/¯
  6. // @author Hustandant#1917
  7. // @match *://mppclone.com/*
  8. // @include *://www.multiplayerpiano.com/*
  9. // @include *://multiplayerpiano.com/*
  10. // @include *://piano.ourworldofpixels.com/*
  11. // @include *://mppfork.netlify.app/*
  12. // @match *.mpp.hri7566.info/*
  13. // @match *://mpp.autoplayer.xyz/*
  14. // @license MIT
  15. // @icon https://github.com/Hustoroff/mpp/blob/main/icon.png?raw=true
  16. // @grant none
  17. // ==/UserScript==
  18.  
  19. var mashtab = 10, matr = [], indstrt = false, webBool = false, delay = 0.5, f = 0, LifeC = 0, setTime;
  20.  
  21. MPP.client.on("a", function(msg) {
  22. let message = msg.a.split(" ");
  23. if(message[0] == "delay" && msg.p.id == MPP.client.participantId && !isNaN(Number(message[1]))) {
  24. delay = (Number(message[1]) > 3) ? 3 : (Number(message[1]) <= 0) ? 0.001 : Number(message[1]);
  25. document.getElementById("delay").innerText = delay;
  26. }
  27. });
  28.  
  29. MPP.client.emit("notification", {
  30. title: "Game of Life script (by Hustandant#8787)",
  31. id:"Script_notification",
  32. duration:20000,
  33. target:"#piano",
  34. html:`<p><h3>F2 - start<br> F4 - clear canvas<br> Tab - grid</br></h3> ${delay} sec. - current delay (<span style="background-color: black"><font color="red">to chat "delay [min - 0.001 max - 3]"</font></span>)<br>ctrl + left mouse button - clear rect</p> Join our discord server: <a target="_blank" href="https://discord.gg/A3SDgxS2Q2">https://discord.gg/A3SDgxS2Q2<a>`
  35. });
  36.  
  37. const statGM = document.createElement("div");
  38. statGM.id = "statGM";
  39. statGM.style.opacity = "1";
  40. statGM.style.position = "fixed";
  41. statGM.style["z-index"] = 150;
  42. statGM.style.display = "block";
  43. statGM.style.float = "right";
  44. statGM.style.margin = "auto";
  45. statGM.style.top = `${document.getElementById("piano").height}px`;
  46. statGM.style["background-color"] = "rgba(137, 137, 137, 0.414)";
  47. statGM.style["backdrop-filter"] = "blur(1px)";
  48. statGM.style["font-size"] = "21px"
  49. statGM.innerHTML = `<span id="start">Start (F2)</span>, Delay: <span id="delay">${delay}</span>, Сells: <span id="LifeC">0</span>, FPS: <span id="fps">0</span>`;
  50. statGM.style.marginLeft = `${String(document.getElementById("piano").offsetLeft + document.getElementById("piano").getElementsByTagName("canvas")[0].offsetLeft)}px`;
  51.  
  52. const canvas = document.createElement("canvas");
  53. canvas.height = parseInt(document.getElementById("piano").style["margin-top"]);
  54. canvas.width = window.innerWidth;
  55. canvas.id = "canv";
  56. canvas.style.opacity = "1";
  57. canvas.style.top = "0";
  58. canvas.style.display = "block";
  59. canvas.style.float = "right";
  60. canvas.style.position = "fixed";
  61. canvas.style.margin = "auto";canvas.style["z-index"] = 200;
  62. canvas.style["background-color"] = "black";
  63.  
  64. const ctx = window.ctx = canvas.getContext("2d");
  65.  
  66. const canvas_web = document.createElement("canvas");
  67.  
  68. canvas_web.height = parseInt(document.getElementById("piano").style["margin-top"]),
  69. canvas_web.width = window.innerWidth;
  70. canvas_web.id = "canv_web";
  71. canvas_web.style.opacity = "1";
  72. canvas_web.style.top = "0";
  73. canvas_web.style.display = "none";
  74. canvas_web.style.float = "right";
  75. canvas_web.style.position = "fixed";
  76. canvas_web.style.margin = "auto";
  77. canvas_web.style["pointer-events"] = "none";
  78. canvas_web.style["z-index"] = 201;
  79.  
  80. const ctx_web = window.ctx = canvas_web.getContext("2d");
  81.  
  82. document.body.append(canvas);
  83. document.body.append(canvas_web);
  84. document.body.append(statGM);
  85.  
  86. window.addEventListener("keyup", function (key) {
  87. key.code == "F2" ? start() : key.code == "F4" ? clearSpace() : key.code == "Tab" ? web() : console.log("ничего");
  88. });
  89.  
  90. function start() {
  91. indstrt = !indstrt;
  92. document.getElementById("start").innerText = indstrt ? "Stop (F2)" : "Start (F2)";
  93. if (indstrt) scanCanvas();
  94. };
  95.  
  96. setInterval(() => {
  97. document.getElementById("fps").innerText = f;
  98. f = 0;
  99. }, 1e3);
  100. window.requestAnimationFrame(fps);
  101.  
  102. function fps() {
  103. f++;
  104. window.requestAnimationFrame(fps);
  105. };
  106.  
  107. function clearSpace() {
  108. ctx.clearRect(0, 0, canvas.width, canvas.height);
  109. for (var i = 0; i < Math.floor(canvas.height / mashtab); i++) {
  110. for (var j = 0; j < Math.floor(canvas.width / mashtab); j++) {
  111. matr[i][j] = 0;
  112. ctx.fillStyle = canvas.style["background-color"] == "white" ? "white" : "black";
  113. ctx.fillRect(j * mashtab, i * mashtab, 10, 10);
  114. }
  115. }
  116. drawRect();
  117. };
  118.  
  119. function web() {
  120. webBool = !webBool;
  121. canvas_web.style.display = webBool ? "block" : "none";
  122. };
  123.  
  124. function drawWeb() {
  125. for (let i = 0; i < Math.floor(canvas_web.height / mashtab); i ++) {
  126. for (let j = 0; j < Math.floor(canvas_web.width / mashtab); j ++) {
  127. ctx_web.strokeStyle = "white";
  128. ctx_web.lineWidth = "0.2";
  129. ctx_web.strokeRect(j * mashtab, i * mashtab, 10, 10);
  130. }
  131. }
  132. };
  133.  
  134. drawWeb();
  135.  
  136. function matrDraw() {
  137. for (var i = 0; i < Math.floor(canvas.height / mashtab); i++) {
  138. matr[i] = [];
  139. for (var j = 0; j < Math.floor(canvas.width / mashtab); j++) {
  140. matr[i][j] = 0;
  141. }
  142. }
  143. };
  144.  
  145. matrDraw();
  146.  
  147. function drawRect() {
  148. LifeC = 0;
  149. ctx.clearRect(0, 0, canvas.width, canvas.height);
  150. for (var i = 0; i < Math.floor(canvas.height / mashtab); i++) {
  151. for (var j = 0; j < Math.floor(canvas.width / mashtab); j++) {
  152. ctx.fillStyle = matr[i][j] == 1 ? "white" : "black";
  153. ctx.fillRect(j * mashtab, i * mashtab, 10, 10);
  154. if(matr[i][j] == 1) LifeC++;
  155. }
  156. }
  157. document.getElementById("LifeC").innerText = LifeC;
  158. };
  159.  
  160. canvas.onclick = function(event) {
  161. if (!indstrt && !event.ctrlKey) {
  162. matr[Math.floor(event.offsetY / mashtab)][Math.floor(event.offsetX / mashtab)] = 1;
  163. drawMouse(1, Math.floor(event.offsetY / mashtab), Math.floor(event.offsetX / mashtab));
  164. } else {
  165. if(!indstrt && event.ctrlKey) {
  166. matr[Math.floor(event.offsetY / mashtab)][Math.floor(event.offsetX / mashtab)] = 0;
  167. drawMouse(0, Math.floor(event.offsetY / mashtab), Math.floor(event.offsetX / mashtab));
  168. }
  169. }
  170. };
  171.  
  172. canvas.onmousemove = (event) => {
  173. if(!indstrt && event.which == 1 && !event.ctrlKey) {
  174. matr[Math.floor(event.offsetY / mashtab)][Math.floor(event.offsetX / mashtab)] = 1;
  175. drawMouse(1, Math.floor(event.offsetY / mashtab), Math.floor(event.offsetX / mashtab));
  176. } else {
  177. if(!indstrt && event.which == 1 && event.ctrlKey) {
  178. matr[Math.floor(event.offsetY / mashtab)][Math.floor(event.offsetX / mashtab)] = 0;
  179. drawMouse(0, Math.floor(event.offsetY / mashtab), Math.floor(event.offsetX / mashtab));
  180. }
  181. }
  182. };
  183.  
  184. function drawMouse(stat, y, x) {
  185. ctx.fillStyle = stat == 1 ? "white" : "black";
  186. ctx.fillRect(x * mashtab, y * mashtab, 10, 10);
  187. };
  188.  
  189. function scanCanvas(step) {
  190. if(indstrt){
  191. LifeC = 0;
  192. var arr = [];
  193. for (var i = 0; i < Math.floor(canvas.height / mashtab); i++) {
  194. arr[i] = [];
  195. for (var j = 0; j < Math.floor(canvas.width / mashtab); j++) {
  196. var c = 0;
  197. if(matr[i][j] == 1) LifeC++;
  198. if (matr[(i == 0 ? Math.floor(canvas.height / mashtab) : i) - 1][j] == 1) c++;
  199. if (matr[i][(j == Math.floor(canvas.width / mashtab) - 1 ? -1 : j) + 1] == 1) c++;
  200. if (matr[(i == Math.floor(canvas.height / mashtab) - 1 ? -1 : i) + 1][j] == 1) c++;
  201. if (matr[i][(j == 0 ? Math.floor(canvas.width / mashtab) : j) - 1] == 1) c++;
  202. if (matr[(i == 0 ? Math.floor(canvas.height / mashtab) : i) - 1][(j == Math.floor(canvas.width / mashtab) - 1 ? -1 : j) + 1] == 1) c++;
  203. if (matr[(i == Math.floor(canvas.height / mashtab) - 1 ? -1 : i) + 1][(j == Math.floor(canvas.width / mashtab) - 1 ? -1 : j) + 1] == 1) c++;
  204. if (matr[(i == Math.floor(canvas.height / mashtab) - 1 ? -1 : i) + 1][(j == 0 ? Math.floor(canvas.width / mashtab) : j) - 1] == 1) c++;
  205. if (matr[(i == 0 ? Math.floor(canvas.height / mashtab) : i) - 1][(j == 0 ? Math.floor(canvas.width / mashtab) : j) - 1] == 1) c++;
  206. arr[i][j] = matr[i][j] == 0 ? c == 3 ? 1 : 0 : c == 2 || c == 3 ? 1 : 0;
  207. }
  208. }
  209. matr = arr;
  210. document.getElementById("LifeC").innerText = LifeC;
  211. drawRect();
  212. setTime = setTimeout(scanCanvas, delay*1000);
  213. }
  214. };