Greasy Fork

Greasy Fork is available in English.

diepAPI

https://github.com/Cazka/diepAPI

当前为 2021-10-10 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.icu/scripts/433681/977916/diepAPI.js

// ==UserScript==
// @name         diepAPI
// @description  https://github.com/Cazka/diepAPI
// @version      2.0.3
// @author       Cazka
// @match        https://diep.io/*
// @icon         https://www.google.com/s2/favicons?domain=diep.io
// @namespace    http://greasyfork.icu/users/541070
// @run-at       document-start
// @grant        none
// ==/UserScript==
(() => {
    if(window.diepAPI) return;

    var diepAPI;(()=>{"use strict";var e={d:(t,o)=>{for(var s in o)e.o(o,s)&&!e.o(t,s)&&Object.defineProperty(t,s,{enumerable:!0,get:o[s]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{CanvasKit:()=>s,Vector:()=>o,arena:()=>p,arenaScaling:()=>d,entityManager:()=>v,game:()=>a,gamepad:()=>i,minimap:()=>r,player:()=>w});class o{x;y;constructor(e,t){this.x=e,this.y=t}static add(e,t){return new o(e.x+t.x,e.y+t.y)}static subtract(e,t){return new o(e.x-t.x,e.y-t.y)}static multiply(e,t){return new o(e.x*t.x,e.y*t.y)}static divide(e,t){return new o(e.x/t.x,e.y/t.y)}static scale(e,t){return new o(e*t.x,e*t.y)}static round(e){return new o(Math.round(e.x),Math.round(e.y))}static len(e){return Math.sqrt(e.x**2+e.y**2)}static distance(e,t){return o.len(o.subtract(e,t))}static centroid(...e){const t=e.reduce(((e,t)=>o.add(e,t)),new o(0,0));return o.scale(1/e.length,t)}static radius(...e){const t=o.centroid(...e);return e.reduce(((e,s)=>e+o.distance(t,s)),0)/e.length}}class s{static hook(e,t){const o=window.CanvasRenderingContext2D.prototype;o[e]=new Proxy(o[e],{apply:(e,o,s)=>("CanvasKit-bypass"!==o.canvas.className&&t(e,o,s),Reflect.apply(e,o,s))})}static replace(e,t){const o=window.CanvasRenderingContext2D.prototype;o[e]=new Proxy(o[e],{apply:(e,o,s)=>"CanvasKit-bypass"!==o.canvas.className?t(e,o,s):Reflect.apply(e,o,s)})}static hookRAF(e){window.requestAnimationFrame=new Proxy(window.requestAnimationFrame,{apply:(t,o,s)=>(e(),Reflect.apply(t,o,s))})}static createCanvas(){const e=document.createElement("canvas");return e.className="CanvasKit-bypass",e.style.pointerEvents="none",e.style.position="fixed",e.style["z-index"]=1,e.style.top="0px",e.style.left="0px",e.style.right="0px",e.style.bottom="0px",e.style.width="100%",e.style.height="100%",e}}const i=new class{#axes;#buttons;connected;constructor(){this.#axes=[0,0,0,0],this.#buttons=[...Array(17)].map((e=>({pressed:!1}))),this.connected=!1,window.navigator.getGamepads=new Proxy(window.navigator.getGamepads,{apply:(e,t,o)=>this.connected?[this.#toGamepad()]:Reflect.apply(e,t,o)})}set x(e){this.#axes[0]=e}set y(e){this.#axes[1]=e}set mx(e){this.#axes[2]=e}set my(e){this.#axes[3]=e}set leftMouse(e){this.#buttons[7].pressed=e}set rightMouse(e){this.#buttons[6].pressed=e}get x(){return this.#axes[0]}get y(){return this.#axes[1]}get mx(){return this.#axes[2]}get my(){return this.#axes[3]}get leftMouse(){return this.#buttons[7].pressed}get rightMouse(){return this.#buttons[6].pressed}#toGamepad(){return{axes:this.#axes,buttons:this.#buttons,mapping:"standard"}}};class n extends EventTarget{emit(e,...t){this.dispatchEvent(new CustomEvent(e,{detail:t}))}on(e,t){this.addEventListener(e,(e=>Reflect.apply(t,this,e.detail)))}once(e,t){this.addEventListener(e,(e=>Reflect.apply(t,this,e.detail)),{once:!0})}off(e,t){this.removeEventListener(e,t)}}const a=new class extends n{#ready=!1;constructor(){super(),s.hookRAF((()=>this.#onframe()))}#onframe(){this.#ready||void 0===window.input||(this.#ready=!0,this.#onready()),super.emit("frame")}#onready(){super.emit("ready")}},r=new class{#minimapDim=new o(1,1);#minimapPos=new o(0,0);#viewportDim=new o(1,1);#viewportPos=new o(0,0);#arrowPos=new o(.5,.5);#drawViewport=!1;constructor(){a.once("ready",(()=>{window.input.set_convar("ren_minimap_viewport","true"),window.input.set_convar=new Proxy(window.input.set_convar,{apply:(e,t,o)=>{"ren_minimap_viewport"===o[0]?this.#drawViewport=o[1]:Reflect.apply(e,t,o)}})})),this._minimapHook(),this._viewportHook(),this._arrowHook()}get minimapDim(){return this.#minimapDim}get minimapPos(){return this.#minimapPos}get viewportDim(){return this.#viewportDim}get viewportPos(){return this.#viewportPos}get arrowPos(){return this.#arrowPos}drawViewport(e){this.#drawViewport=e}_minimapHook(){s.hook("strokeRect",((e,t,s)=>{const i=t.getTransform();this.#minimapDim=new o(i.a,i.d),this.#minimapPos=new o(i.e,i.f)}))}_viewportHook(){s.replace("fillRect",((e,t,s)=>{const i=t.getTransform();return Math.round(i.a/i.d*1e4)!==Math.round(window.innerWidth/window.innerHeight*1e4)||i.a>=window.innerWidth&&i.d>=window.innerHeight?Reflect.apply(e,t,s):(this.#viewportDim=new o(i.a,i.d),this.#viewportPos=new o(i.e,i.f),this.#drawViewport?Reflect.apply(e,t,s):void 0)}))}_arrowHook(){let e,t,i,n=0;const a=()=>{const s=Math.round(o.distance(e,t)),n=Math.round(o.distance(e,i)),a=Math.round(o.distance(t,i));if(s===n&&n===a)return;const r=o.centroid(e,t,i),c=o.subtract(r,this.#minimapPos),d=o.divide(c,this.#minimapDim);this.#arrowPos=d};s.hook("beginPath",((e,t,o)=>{n=1})),s.hook("moveTo",((t,s,i)=>{if(1===n)return n++,void(e=new o(i[0],i[1]));n=0})),s.hook("lineTo",((e,s,a)=>2===n?(n++,void(t=new o(a[0],a[1]))):3===n?(n++,void(i=new o(a[0],a[1]))):void(n=0))),s.hook("fill",((e,t,o)=>{if(4===n)return n++,void a();n=0}))}},c=new class{get position(){const e=o.add(r.viewportPos,o.scale(.5,r.viewportDim)),t=o.subtract(e,r.minimapPos),s=o.divide(t,r.minimapDim);return p.scale(s)}},d=new class{#scalingFactor=1;constructor(){s.hook("stroke",((e,t,o)=>{"#cdcdcd"===t.fillStyle&&0!==t.globalAlpha&&(this.#scalingFactor=10*t.globalAlpha)}))}get scalingFactor(){return this.#scalingFactor}get windowRatio(){return Math.max(window.innerWidth/1920,window.innerHeight/1080)}get fov(){return this.#scalingFactor/this.windowRatio}toArenaUnits(e){return o.scale(1/this.#scalingFactor,e)}toScreenUnits(e){return o.scale(this.#scalingFactor,e)}toArenaPos(e){const t=o.subtract(e,new o(window.innerWidth/2,window.innerHeight/2)),s=this.toArenaUnits(t);return o.add(s,c.position)}toScreenPos(e){const t=o.subtract(e,c.position),s=this.toScreenUnits(t);return o.add(s,new o(window.innerWidth/2,window.innerHeight/2))}},p=new class{#size=1;constructor(){a.on("frame",(()=>{const e=o.divide(r.minimapDim,r.viewportDim),t=o.multiply(e,new o(window.innerWidth,window.innerHeight)),s=o.round(d.toArenaUnits(t));this.#size=s.x}))}get size(){return this.#size}scale(e){const t=e=>Math.round(this.#size*(e-.5));return new o(t(e.x),t(e.y))}unscale(e){const t=e=>e/this.#size+.5;return new o(t(e.x),t(e.y))}};class h{#position=new o(0,0);#velocity=new o(0,0);#velocitySamplesSize=10;#velocitySamples=[];#velocitySamplesIndex=0;#velocityLastNow=performance.now();get position(){return this.#position}get velocity(){return this.#velocity}predictPos(e){const t=(e-this.#velocityLastNow)/1e3;return o.add(this.#position,o.scale(t,this.#velocity))}updatePos(e){this.#updateVelocity(e),this.#position=e}#updateVelocity(e){const t=performance.now(),s=(t-this.#velocityLastNow)/1e3;if(0===s)return;this.#velocityLastNow=t;const i=o.scale(1/s,o.subtract(e,this.#position));this.#velocitySamples[this.#velocitySamplesIndex++]=i,this.#velocitySamplesIndex%=this.#velocitySamplesSize,this.#velocity=o.scale(1/this.#velocitySamples.length,this.#velocitySamples.reduce(((e,t)=>o.add(e,t))))}}const l=new class extends h{constructor(){super(),a.on("frame",(()=>super.updatePos(p.scale(r.arrowPos))))}},u=e=>new Promise(((t,o)=>setTimeout(t,e))),w=new class extends n{#isDead=!0;#mouseLock=!1;#mouseScreenPos=new o(0,0);#mousePos=new o(0,0);#gamemode=window.localStorage.gamemode;constructor(){super(),a.once("ready",(()=>{a.on("frame",(()=>{const e=!window.input.should_prevent_unload();this.#isDead!=e&&(this.#isDead=e,this.#isDead?this.#ondead():this.#onspawn())})),a.on("frame",(()=>{this.#mousePos=d.toArenaPos(this.#mouseScreenPos)}));const e=document.getElementById("canvas");e.onmousemove=new Proxy(e.onmousemove,{apply:(e,t,o)=>{if(!this.#mouseLock)return this.#onmousemove(o[0]),Reflect.apply(e,t,o)}}),e.onmousedown=new Proxy(e.onmousedown,{apply:(e,t,o)=>{if(!this.#mouseLock)return this.#onmousedown(o[0]),Reflect.apply(e,t,o)}}),e.onmouseup=new Proxy(e.onmouseup,{apply:(e,t,o)=>{if(!this.#mouseLock)return this.#onmouseup(o[0]),Reflect.apply(e,t,o)}}),window.onkeydown=new Proxy(window.onkeydown,{apply:(e,t,o)=>(this.#onkeydown(o[0]),Reflect.apply(e,t,o))}),window.onkeyup=new Proxy(window.onkeyup,{apply:(e,t,o)=>(this.#onkeyup(o[0]),Reflect.apply(e,t,o))})}))}get position(){return l.position}get velocity(){return l.velocity}get mouse(){return this.#mousePos}get isDead(){return this.#isDead}get gamemode(){return this.#gamemode}predictPos(e){return l.predictPos(e)}async octoBuild(){this.keyDown("k"),await this.upgrade_stat(4,7),await this.upgrade_stat(5,7),await this.upgrade_stat(6,7),await this.upgrade_stat(7,7),await this.upgrade_stat(8,5),await this.upgrade_tank(1),await this.upgrade_tank(2),await this.upgrade_tank(1),this.keyUp("k")}async#ondead(){await u(50),super.emit("dead")}async#onspawn(){this.#gamemode=window.localStorage.gamemode,await u(50),super.emit("spawn")}useGamepad(e){i.connected=e}keyDown(e){if("string"==typeof e){if(1!=e.length)throw new Error(`diepAPI: Unsupported key: ${e}`);e=e.toUpperCase().charCodeAt(0)}window.input.keyDown(e),this.#onkeydown({keyCode:e})}keyUp(e){if("string"==typeof e){if(1!=e.length)throw new Error(`diepAPI: Unsupported key: ${e}`);e=e.toUpperCase().charCodeAt(0)}window.input.keyUp(e),this.#onkeyup({keyCode:e})}async keyPress(e){this.keyDown(e),await u(200),this.keyUp(e),await u(10)}async spawn(e,t=0){this.#isDead&&(void 0!==e&&(document.getElementById("textInput").value=e),await this.keyPress(13),await u(250),await this.spawn(e,t+1))}async upgrade_stat(e,t){if(e<1||e>8)throw`diepAPI: ${e} is not a supported stat`;this.keyDown(85);for(let o=0;o<t;o++)await this.keyPress(48+e);this.keyUp(85),await u(250)}async upgrade_tank(e){const t=(e-=1)%2,o=Math.floor(e/2),s=window.devicePixelRatio*d.windowRatio*(115*t+97.5),i=window.devicePixelRatio*d.windowRatio*(110*o+120);this.#mouseLock=!0,window.input.mouse(s,i),await this.keyPress(1),await u(200),this.#mouseLock=!1,await u(1500)}moveTo(e){if(i.connected){const t=o.subtract(e,this.position),s=o.len(t);if(0===s)return i.x=0,void(i.y=0);const n=o.scale(1/s,t);i.x=n.x,i.y=n.y}else{const t=o.subtract(e,this.position);t.x>0?(this.keyUp("a"),this.keyDown("d")):t.x<0?(this.keyUp("d"),this.keyDown("a")):(this.keyUp("a"),this.keyUp("d")),t.y>0?(this.keyUp("w"),this.keyDown("s")):t.y<0?(this.keyUp("s"),this.keyDown("w")):(this.keyUp("w"),this.keyUp("s"))}}lookAt(e){const t=d.toScreenPos(e);window.input.mouse(t.x,t.y),this.#onmousemove({clientX:t.x,clientY:t.y})}#onmousemove(e){if(this.#mouseScreenPos=new o(e.clientX,e.clientY),i.connected){const e=d.toArenaPos(this.#mouseScreenPos),t=o.subtract(e,this.position);let s=o.scale(d.fov/1200/1.1,t);const n=o.len(s);0!==n&&n<.15&&(s=o.scale(.15/n,s)),i.mx=s.x,i.my=s.y}}#onmousedown(e){i.connected&&this.#onkeydown({keyCode:e.which})}#onmouseup(e){i.connected&&this.#onkeyup({keyCode:e.which})}#onkeydown(e){if(super.emit("keydown",e.keyCode),i.connected)switch(e.keyCode){case 37:case 65:i.x=-1;break;case 40:case 83:i.y=1;break;case 38:case 87:i.y=-1;break;case 39:case 68:i.x=1;break;case 1:case 32:i.leftMouse=!0;break;case 3:case 16:i.rightMouse=!0}}#onkeyup(e){if(super.emit("keyup",e.keyCode),i.connected)switch(e.keyCode){case 37:case 65:i.x=0;break;case 40:case 83:i.y=0;break;case 38:case 87:i.y=0;break;case 39:case 68:i.x=0;break;case 1:case 32:i.leftMouse=!1;break;case 3:case 16:i.rightMouse=!1}}};var y,m;!function(e){e[e.Player=0]="Player",e[e.Bullet=1]="Bullet",e[e.Drone=2]="Drone",e[e.Trap=3]="Trap",e[e.Square=4]="Square",e[e.Triangle=5]="Triangle",e[e.Pentagon=6]="Pentagon",e[e.AlphaPentagon=7]="AlphaPentagon",e[e.Crasher=8]="Crasher",e[e.UNKNOWN=9]="UNKNOWN"}(y||(y={})),function(e){e.TeamBlue="#00b2e1",e.TeamRed="#f14e54",e.TeamPurple="#bf7ff5",e.TeamGreen="#00e16e",e.Square="#ffe869",e.Triangle="#fc7677",e.Pentagon="#768dfc",e.AlphaPentagon="#768dfc",e.Crasher="#f177dd",e.NecromancerDrone="#fcc376"}(m||(m={}));const g=[m.TeamBlue,m.TeamRed,m.TeamPurple,m.TeamGreen];class k extends h{type;extras;constructor(e,t={}){super(),this.type=e,this.extras=t}updatePos(e){super.updatePos(e)}}const v=new class{#entities=[];#entitiesUpdated=[];constructor(){this.#triangleHook(),this.#squareHook(),this.#pentagonHook(),this.#playerHook(),a.on("frame",(()=>{this.#entities=this.#entitiesUpdated,this.#entitiesUpdated=[]}))}get entities(){return this.#entities}#add(e,t,o={}){const s=this.#findEntity(e,t);let i;i=-1===s?new k(e,{id:Math.random().toString(36).slice(2,5),timestamp:performance.now(),...o}):this.#entities[s],i.updatePos(t),this.#entitiesUpdated.push(i)}#findEntity(e,t){let s=-1,i=Number.MAX_SAFE_INTEGER;return this.#entities.forEach(((e,n)=>{const a=o.distance(e.predictPos(performance.now()),t);a<i&&(i=a,s=n)})),i>28||y.UNKNOWN!==e&&this.#entities[s].type!==e?-1:s}#createPolygonHook(e,t){let i=0,n=[];s.hook("beginPath",((e,t,o)=>{i=1,n=[]})),s.hook("moveTo",((e,t,s)=>{if(1===i)return i++,void n.push(new o(s[0],s[1]));i=0})),s.hook("lineTo",((t,s,a)=>{if(i>=2&&i<=e)return i++,void n.push(new o(a[0],a[1]));i=0})),s.hook("fill",((o,s,a)=>{if(i===e+1)return i++,void t(n,s);i=0}))}#triangleHook(){this.#createPolygonHook(3,((e,t)=>{const s=Math.round(o.distance(e[0],e[1])),i=Math.round(o.distance(e[0],e[2])),n=Math.round(o.distance(e[1],e[2]));if(s!==i||i!==n)return;if("#000000"===t.fillStyle)return;e=e.map((e=>d.toArenaPos(e)));const a=o.centroid(...e),r=Math.round(o.radius(...e)),c=t.fillStyle;let p;switch(r){case 23:case 30:g.includes(c)&&(p=y.Drone);break;case 35:m.Crasher===c&&(p=y.Crasher);break;case 40:case 41:case 42:case 43:case 44:case 45:case 46:g.includes(c)&&(p=y.Drone);break;case 55:m.Crasher===c&&(p=y.Crasher),m.Triangle===c&&(p=y.Triangle)}void 0===p&&(p=y.UNKNOWN),this.#add(p,a,{color:c,radius:r})}))}#squareHook(){this.#createPolygonHook(4,((e,t)=>{e=e.map((e=>d.toArenaPos(e)));const s=o.centroid(...e),i=Math.round(o.radius(...e)),n=t.fillStyle;let a;switch(i){case 55:m.Square===n&&(a=y.Square),(g.includes(n)||m.NecromancerDrone===n)&&(a=y.Drone)}void 0===a&&(a=y.UNKNOWN),this.#add(a,s,{color:n,radius:i})}))}#pentagonHook(){this.#createPolygonHook(5,((e,t)=>{e=e.map((e=>d.toArenaPos(e)));const s=o.centroid(...e),i=Math.round(o.radius(...e)),n=t.fillStyle;let a;switch(i){case 75:m.Pentagon===n&&(a=y.Pentagon);break;case 200:m.AlphaPentagon===n&&(a=y.AlphaPentagon)}void 0===a&&(a=y.UNKNOWN),this.#add(a,s,{color:n,radius:i})}))}#playerHook(){let e,t,i,n=0;const a=()=>{e=d.toArenaPos(e),i=d.toArenaUnits(new o(i,i)).x;let s=y.UNKNOWN;s=i>53?y.Player:y.Bullet,this.#add(s,e,{color:t,radius:i})};s.hook("beginPath",((e,t,o)=>{3===n?3!==n?n=0:n++:n=1})),s.hook("arc",((s,r,c)=>{if(1===n){n++;const t=r.getTransform();return e=new o(t.e,t.f),void(i=t.a)}return 4===n?(n++,void(t=r.fillStyle)):6===n?(n++,void a()):void(n=0)})),s.hook("fill",((e,t,o)=>{2!==n&&5!==n?n=0:n++}))}};diepAPI=t})();

    window.diepAPI = diepAPI;
})();