Closed gurumnyang closed 2 years ago
Hey,
entities in diepAPI.entitites have their absolute position in entity.position
.
With absolute position being the position in the arena.
Are you trying to convert the entity position to the position on your screen?
I wanna convert the entity position to the absolute position in the arena.
however, entity.position
looks like relative. Because it changed when I move.
It can't show position in the arena. Is there any way to get the absolute position of the entity in the arena likeplayer.position
?
two entity.position
are same!
that looks like a bug. which version of diepAPI are you using and did you disable all other scripts?
maybe 2.0.3 or 2.1.1 I used your example 'farmer.user.js'
ah... I think you didn't change project version in package.json
maybe my project is lastest version
i have just tested it and it works for me. I dont know why it doesn't work for you.
Im using:
My browser setting is +Naver Whale(Kind of Chrome Extension) (not problem) +Violentmonkey Chrome Extension (not problem) +diepAPI v2.1.1 (not problem)
How is the location of the entity displayed on your screen?
(tried in Window11 Brave browser Tampermonkey v2.1.1 userscript )
`// ==UserScript== // @name diepAPI // @description https://github.com/Cazka/diepAPI // @version 2.1.1 // @author Cazka // @match https://diep.io/* // @icon https://www.google.com/s2/favicons?domain=diep.io // @namespace https://greasyfork.org/users/541070 // @run-at document-start // @grant none // ==/UserScript== (() => { if(window.diepAPI) return;
var diepAPI;(()=>{"use strict";var e={d:(t,s)=>{for(var o in s)e.o(s,o)&&!e.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:s[o]})},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:()=>o,Vector:()=>s,arena:()=>l,arenaScaling:()=>d,entityManager:()=>k,game:()=>a,gamepad:()=>n,minimap:()=>r,player:()=>w});class s{x;y;constructor(e,t){this.x=e,this.y=t}static add(e,t){return new s(e.x+t.x,e.y+t.y)}static subtract(e,t){return new s(e.x-t.x,e.y-t.y)}static multiply(e,t){return new s(e.x*t.x,e.y*t.y)}static divide(e,t){return new s(e.x/t.x,e.y/t.y)}static scale(e,t){return new s(e*t.x,e*t.y)}static round(e){return new s(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 s.len(s.subtract(e,t))}static centroid(...e){const t=e.reduce(((e,t)=>s.add(e,t)),new s(0,0));return s.scale(1/e.length,t)}static radius(...e){const t=s.centroid(...e);return e.reduce(((e,o)=>e+s.distance(t,o)),0)/e.length}}class o{static hook(e,t){const s=window.CanvasRenderingContext2D.prototype;s[e]=new Proxy(s[e],{apply:(e,s,o)=>("CanvasKit-bypass"!==s.canvas.className&&t(e,s,o),Reflect.apply(e,s,o))})}static replace(e,t){const s=window.CanvasRenderingContext2D.prototype;s[e]=new Proxy(s[e],{apply:(e,s,o)=>"CanvasKit-bypass"!==s.canvas.className?t(e,s,o):Reflect.apply(e,s,o)})}static hookRAF(e){window.requestAnimationFrame=new Proxy(window.requestAnimationFrame,{apply:(t,s,o)=>(e(),Reflect.apply(t,s,o))})}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 n=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,s)=>this.connected?[this.#toGamepad()]:Reflect.apply(e,t,s)})}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 i 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 i{#ready=!1;constructor(){super(),o.hookRAF((()=>this.#onframe()))}#onframe(){this.#ready||void 0===window.input||(this.#ready=!0,this.#onready()),super.emit("frame")}#onready(){setTimeout((()=>super.emit("ready")),100)}},r=new class{#minimapDim=new s(1,1);#minimapPos=new s(0,0);#viewportDim=new s(1,1);#viewportPos=new s(0,0);#arrowPos=new s(.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,s)=>{"ren_minimap_viewport"===s[0]?this.#drawViewport=s[1]:Reflect.apply(e,t,s)}})})),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(){o.hook("strokeRect",((e,t,o)=>{const n=t.getTransform();this.#minimapDim=new s(n.a,n.d),this.#minimapPos=new s(n.e,n.f)}))}_viewportHook(){o.replace("fillRect",((e,t,o)=>{const n=t.getTransform();return Math.round(n.a/n.d*1e4)!==Math.round(window.innerWidth/window.innerHeight*1e4)||n.a>=window.innerWidth&&n.d>=window.innerHeight?Reflect.apply(e,t,o):(this.#viewportDim=new s(n.a,n.d),this.#viewportPos=new s(n.e,n.f),this.#drawViewport?Reflect.apply(e,t,o):void 0)}))}_arrowHook(){let e,t,n,i=0;const a=()=>{const o=Math.round(s.distance(e,t)),i=Math.round(s.distance(e,n)),a=Math.round(s.distance(t,n));if(o===i&&i===a)return;const r=s.centroid(e,t,n),c=s.subtract(r,this.#minimapPos),d=s.divide(c,this.#minimapDim);this.#arrowPos=d};o.hook("beginPath",((e,t,s)=>{i=1})),o.hook("moveTo",((t,o,n)=>{if(1===i)return i++,void(e=new s(n[0],n[1]));i=0})),o.hook("lineTo",((e,o,a)=>2===i?(i++,void(t=new s(a[0],a[1]))):3===i?(i++,void(n=new s(a[0],a[1]))):void(i=0))),o.hook("fill",((e,t,s)=>{if(4===i)return i++,void a();i=0}))}},c=new class{get position(){const e=s.add(r.viewportPos,s.scale(.5,r.viewportDim)),t=s.subtract(e,r.minimapPos),o=s.divide(t,r.minimapDim);return l.scale(o)}},d=new class{#scalingFactor=1;constructor(){o.hook("stroke",((e,t,s)=>{"#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 s.scale(1/this.#scalingFactor,e)}toCanvasUnits(e){return s.scale(this.#scalingFactor,e)}toArenaPos(e){const t=s.subtract(e,this.screenToCanvas(new s(window.innerWidth/2,window.innerHeight/2))),o=this.toArenaUnits(t);return s.add(o,c.position)}toCanvasPos(e){const t=s.subtract(e,c.position),o=this.toCanvasUnits(t);return s.add(o,this.screenToCanvas(new s(window.innerWidth/2,window.innerHeight/2)))}screenToCanvasUnits(e){return e*window.devicePixelRatio}canvasToScreenUnits(e){return e/window.devicePixelRatio}screenToCanvas(e){return s.scale(window.devicePixelRatio,e)}canvasToScreen(e){return s.scale(1/window.devicePixelRatio,e)}},l=new class{#size=1;constructor(){a.on("frame",(()=>{const e=s.divide(r.minimapDim,r.viewportDim),t=s.multiply(e,d.screenToCanvas(new s(window.innerWidth,window.innerHeight))),o=s.round(d.toArenaUnits(t));this.#size=o.x}))}get size(){return this.#size}scale(e){const t=e=>Math.round(this.#size*(e-.5));return new s(t(e.x),t(e.y))}unscale(e){const t=e=>e/this.#size+.5;return new s(t(e.x),t(e.y))}};class p{#position=new s(0,0);#velocity=new s(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+performance.now()-this.#velocityLastNow)/1e3;return s.add(this.#position,s.scale(t,this.#velocity))}updatePos(e){this.#updateVelocity(e),this.#position=e}#updateVelocity(e){const t=performance.now(),o=(t-this.#velocityLastNow)/1e3;if(0===o)return;this.#velocityLastNow=t;const n=s.scale(1/o,s.subtract(e,this.#position));this.#velocitySamples[this.#velocitySamplesIndex++]=n,this.#velocitySamplesIndex%=this.#velocitySamplesSize,this.#velocity=s.scale(1/this.#velocitySamples.length,this.#velocitySamples.reduce(((e,t)=>s.add(e,t))))}}const h=new class extends p{constructor(){super(),a.on("frame",(()=>super.updatePos(l.scale(r.arrowPos))))}},u=e=>new Promise(((t,s)=>setTimeout(t,e))),w=new class extends i{#isDead=!0;#mouseLock=!1;#mouseCanvasPos=new s(0,0);#mousePos=new s(0,0);#gamemode=window.localStorage.gamemode;#level=1;#tank="Tank";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.#mouseCanvasPos)}));const e=document.getElementById("canvas");e.onmousemove=new Proxy(e.onmousemove,{apply:(e,t,s)=>{if(!this.#mouseLock)return this.#onmousemove(s[0]),Reflect.apply(e,t,s)}}),e.onmousedown=new Proxy(e.onmousedown,{apply:(e,t,s)=>{if(!this.#mouseLock)return this.#onmousedown(s[0]),Reflect.apply(e,t,s)}}),e.onmouseup=new Proxy(e.onmouseup,{apply:(e,t,s)=>{if(!this.#mouseLock)return this.#onmouseup(s[0]),Reflect.apply(e,t,s)}}),window.onkeydown=new Proxy(window.onkeydown,{apply:(e,t,s)=>(this.#onkeydown(s[0]),Reflect.apply(e,t,s))}),window.onkeyup=new Proxy(window.onkeyup,{apply:(e,t,s)=>(this.#onkeyup(s[0]),Reflect.apply(e,t,s))}),o.hook("fillText",((e,t,s)=>{const o=s[0].match(/^Lvl (\d+) (\w*)$/);if(null==o)return;const n=Number(o[1]),i=o[2];for(;n>this.#level+1;)super.emit("level",++this.#level);n!==this.#level&&super.emit("level",n),i!==this.#tank&&super.emit("tank",i),this.#level=n,this.#tank=o[2]}))}))}get position(){return h.position}get velocity(){return h.velocity}get mouse(){return this.#mousePos}get isDead(){return this.#isDead}get gamemode(){return this.#gamemode}get level(){return this.#level}get tank(){return this.#tank}predictPos(e){return h.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){n.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 s=0;s<t;s++)await this.keyPress(48+e);this.keyUp(85),await u(250)}async upgrade_tank(e){const t=(e-=1)%2,s=Math.floor(e/2),o=d.screenToCanvasUnits(d.windowRatio*(115*t+97.5)),n=d.screenToCanvasUnits(d.windowRatio*(110*s+120));this.#mouseLock=!0,window.input.mouse(o,n),await this.keyPress(1),await u(200),this.#mouseLock=!1,await u(1500)}moveTo(e){if(n.connected){const t=s.subtract(e,this.position),o=s.len(t);if(0===o)return n.x=0,void(n.y=0);const i=s.scale(1/o,t);n.x=i.x,n.y=i.y}else{const t=s.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.toCanvasPos(e);window.input.mouse(t.x,t.y),this.#onmousemove({clientX:t.x,clientY:t.y})}#onmousemove(e){if(this.#mouseCanvasPos=d.screenToCanvas(new s(e.clientX,e.clientY)),n.connected){const e=d.toArenaPos(this.#mouseCanvasPos),t=s.subtract(e,this.position);let o=s.scale(d.fov/1200/1.1,t);const i=s.len(o);0!==i&&i<.15&&(o=s.scale(.15/i,o)),n.mx=o.x,n.my=o.y}}#onmousedown(e){n.connected&&this.#onkeydown({keyCode:e.which})}#onmouseup(e){n.connected&&this.#onkeyup({keyCode:e.which})}#onkeydown(e){if(super.emit("keydown",e.keyCode),n.connected)switch(e.keyCode){case 37:case 65:n.x=-1;break;case 40:case 83:n.y=1;break;case 38:case 87:n.y=-1;break;case 39:case 68:n.x=1;break;case 1:case 32:n.leftMouse=!0;break;case 3:case 16:n.rightMouse=!0}}#onkeyup(e){if(super.emit("keyup",e.keyCode),n.connected)switch(e.keyCode){case 37:case 65:n.x=0;break;case 40:case 83:n.y=0;break;case 38:case 87:n.y=0;break;case 39:case 68:n.x=0;break;case 1:case 32:n.leftMouse=!1;break;case 3:case 16:n.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 v=[m.TeamBlue,m.TeamRed,m.TeamPurple,m.TeamGreen];class g extends p{type;extras;constructor(e,t={}){super(),this.type=e,this.extras=t}updatePos(e){super.updatePos(e)}}const k=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,s={}){const o=this.#findEntity(e,t);let n;n=-1===o?new g(e,{id:Math.random().toString(36).slice(2,5),timestamp:performance.now(),...s}):this.#entities[o],n.updatePos(t),this.#entitiesUpdated.push(n)}#findEntity(e,t){let o=-1,n=Number.MAX_SAFE_INTEGER;return this.#entities.forEach(((e,i)=>{const a=s.distance(e.predictPos(0),t);a<n&&(n=a,o=i)})),n>28||y.UNKNOWN!==e&&this.#entities[o].type!==e?-1:o}#createPolygonHook(e,t){let n=0,i=[];o.hook("beginPath",((e,t,s)=>{n=1,i=[]})),o.hook("moveTo",((e,t,o)=>{if(1===n)return n++,void i.push(new s(o[0],o[1]));n=0})),o.hook("lineTo",((t,o,a)=>{if(n>=2&&n<=e)return n++,void i.push(new s(a[0],a[1]));n=0})),o.hook("fill",((s,o,a)=>{if(n===e+1)return n++,void t(i,o);n=0}))}#triangleHook(){this.#createPolygonHook(3,((e,t)=>{const o=Math.round(s.distance(e[0],e[1])),n=Math.round(s.distance(e[0],e[2])),i=Math.round(s.distance(e[1],e[2]));if(o!==n||n!==i)return;if("#000000"===t.fillStyle)return;e=e.map((e=>d.toArenaPos(e)));const a=s.centroid(...e),r=Math.round(s.radius(...e)),c=t.fillStyle;let l;switch(r){case 23:case 30:v.includes(c)&&(l=y.Drone);break;case 35:m.Crasher===c&&(l=y.Crasher);break;case 40:case 41:case 42:case 43:case 44:case 45:case 46:v.includes(c)&&(l=y.Drone);break;case 55:m.Crasher===c&&(l=y.Crasher),m.Triangle===c&&(l=y.Triangle)}void 0===l&&(l=y.UNKNOWN),this.#add(l,a,{color:c,radius:r})}))}#squareHook(){this.#createPolygonHook(4,((e,t)=>{e=e.map((e=>d.toArenaPos(e)));const o=s.centroid(...e),n=Math.round(s.radius(...e)),i=t.fillStyle;let a;switch(n){case 55:m.Square===i&&(a=y.Square),(v.includes(i)||m.NecromancerDrone===i)&&(a=y.Drone)}void 0===a&&(a=y.UNKNOWN),this.#add(a,o,{color:i,radius:n})}))}#pentagonHook(){this.#createPolygonHook(5,((e,t)=>{e=e.map((e=>d.toArenaPos(e)));const o=s.centroid(...e),n=Math.round(s.radius(...e)),i=t.fillStyle;let a;switch(n){case 75:m.Pentagon===i&&(a=y.Pentagon);break;case 200:m.AlphaPentagon===i&&(a=y.AlphaPentagon)}void 0===a&&(a=y.UNKNOWN),this.#add(a,o,{color:i,radius:n})}))}#playerHook(){let e,t,n,i=0;const a=()=>{e=d.toArenaPos(e),n=d.toArenaUnits(new s(n,n)).x;let o=y.UNKNOWN;o=n>53?y.Player:y.Bullet,this.#add(o,e,{color:t,radius:n})};o.hook("beginPath",((e,t,s)=>{3===i?3!==i?i=0:i++:i=1})),o.hook("arc",((o,r,c)=>{if(1===i){i++;const t=r.getTransform();return e=new s(t.e,t.f),void(n=t.a)}return 4===i?(i++,void(t=r.fillStyle)):6===i?(i++,void a()):void(i=0)})),o.hook("fill",((e,t,s)=>{2!==i&&5!==i?i=0:i++}))}};diepAPI=t})();
window.diepAPI = diepAPI;
})(); `
I made my function to calculate absolute position on my userscript.
position in diepAPI.entities.entity return relative position. I wanna absolute position, like diepAPI.player.position
I tried to find how to change relative position to absolute position, but I'm failed. Can you tell me how to calculate the absolute position of the entity?