Last active
May 30, 2023 09:30
-
-
Save raidan00/aa341b647179af4ba7de370ce33de36c to your computer and use it in GitHub Desktop.
StarBreak-Bots: Combat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name StarBreak-Bots: Combat | |
// @version 1.0 | |
// @match https://*.starbreak.com/* | |
// @grant GM_setValue | |
// @grant GM_getValue | |
// @grant GM_addValueChangeListener | |
// ==/UserScript== | |
let goToLeadCode = "KeyX"; | |
let jumpToLeadCode = "KeyS"; | |
let jumpToLeadDistance = 120; | |
let ammoUseCode = "KeyA"; | |
let ammoUseNatveKeyCode = 81; | |
let doubleSideBlockCode = "KeyZ"; | |
let ignoreKeyKodes = { | |
16: true, | |
82: true, | |
9: true, | |
19: true, | |
} | |
let un = getAccountUsername(); | |
let botsList = JSON.parse(localStorage.botsList ?? "{}"); | |
let botsLead = localStorage.botsLead || un; | |
localStorage.botsLead = botsLead; | |
function sendKey(type, keyCode) { | |
let preventDefault = function dummyPreventDefault() {}; | |
let ev = { type, keyCode, preventDefault }; | |
XDL.onKey(ev); | |
} | |
GM_addValueChangeListener("pressThisKey-"+un, function(key, oldValue, newValue, remote) { | |
sendKey(newValue.type, newValue.keyCode); | |
}); | |
GM_addValueChangeListener("makeBotsCentred", function(key, oldValue, newValue, remote) { | |
if(botsLead == un) return; | |
let lastKeyCode = 0; | |
let interval = setInterval(()=>{ | |
let distance = leadPos.x - center.x; | |
if(Math.abs(distance) < 7){ | |
clearInterval(interval) | |
sendKey("keyup", 37); | |
sendKey("keyup", 39); | |
return; | |
} | |
if(distance > 0){ | |
var keyCode = 39; | |
}else{ | |
keyCode = 37; | |
}; | |
if(lastKeyCode != keyCode){ | |
sendKey("keyup", lastKeyCode); | |
lastKeyCode = keyCode; | |
sendKey("keydown", keyCode); | |
} | |
}, 0) | |
}); | |
let needKeyUp = {}; | |
function listener(e){ | |
if (XDL.textInputActive || e.keyCode == 13) return; | |
if(botsLead != un) return; | |
if(e.code == goToLeadCode && e.type == "keydown"){ | |
GM_setValue("makeBotsCentred", Math.random()); | |
} | |
if(e.code == jumpToLeadCode){ | |
if(e.type == "keydown"){ | |
for (const key in botsList) { | |
if(key == un) continue; | |
let distanceY = rope[key].posY - center.y; | |
if(distanceY < -jumpToLeadDistance){ | |
GM_setValue("pressThisKey-"+key, {type:"keydown", keyCode: 40}); | |
GM_setValue("pressThisKey-"+key, {type:"keydown", keyCode: 67}); | |
GM_setValue("pressThisKey-"+key, {type:"keyup", keyCode: 40}); | |
GM_setValue("pressThisKey-"+key, {type:"keyup", keyCode: 67}); | |
} | |
if(distanceY > jumpToLeadDistance){ | |
GM_setValue("pressThisKey-"+key, {type:"keydown", keyCode: 67}); | |
needKeyUp[key] = true; | |
} | |
} | |
}else{ | |
for(let key in needKeyUp){ | |
GM_setValue("pressThisKey-"+key, {type:"keyup", keyCode: 67}); | |
} | |
needKeyUp = {}; | |
} | |
} | |
if(ignoreKeyKodes[e.keyCode]) return; | |
e.stopImmediatePropagation(); | |
if(e.code == ammoUseCode && e.type == "keydown"){ | |
let max = {un, val: 0}; | |
for (const key in botsList){ | |
let ammoInfo = GM_getValue("ammoInfo-"+key); | |
if(ammoInfo.now > max.val){ | |
max.un = key; | |
max.val = ammoInfo.now; | |
} | |
} | |
GM_setValue("pressThisKey-"+max.un, {type:"keydown", keyCode: ammoUseNatveKeyCode}); | |
GM_setValue("pressThisKey-"+max.un, {type:"keyup", keyCode: ammoUseNatveKeyCode}); | |
return; | |
} | |
if(e.code == doubleSideBlockCode && e.type == "keydown"){ | |
let sideKeyCode = 37; | |
for (const key in botsList) { | |
if(sideKeyCode == 37)sideKeyCode = 39; | |
else sideKeyCode = 37 | |
GM_setValue("pressThisKey-"+key, {type:"keydown", keyCode:40}); | |
let innerSideKeyCode = sideKeyCode; | |
GM_setValue("pressThisKey-"+key, {type:"keydown", keyCode:innerSideKeyCode}); | |
setTimeout(()=>{ | |
GM_setValue("pressThisKey-"+key, {type:"keyup", keyCode:innerSideKeyCode}); | |
},20) | |
} | |
return; | |
} | |
for (const key in botsList) { | |
GM_setValue("pressThisKey-"+key, {type:e.type, keyCode:e.keyCode}); | |
} | |
} | |
document.addEventListener('keydown', listener, true); | |
document.addEventListener('keyup', listener, true); | |
let baseTid = 0; | |
let center = {}; | |
let leadTid = 0; | |
let leadPos = {}; | |
let rope = {}; | |
let missionCompletedTid = 0; | |
for (const key in botsList) { | |
rope[key] = {tid: 0, lastSeen: 0, posX: 0, posY: 0}; | |
} | |
function onLoaded(){ | |
(function (orig) { | |
RenderEngineWebGL._Z21XDL_CreateTextTextureiPKcijS0_ij = function (textureId, fontName, size, color, text, outlineSize, outlineColor) { | |
var textStr = Pointer_stringify(text); | |
var font = size + 'px ' + Pointer_stringify(fontName); | |
var csscolor = TextUtils.loadColorToCSSRGBA(color); | |
var tid = orig.apply(null, arguments); | |
if(textStr == "XP" && font == "22px HemiHeadBold" && csscolor == "rgba(255,255,255,1)"){ | |
missionCompletedTid = tid; | |
}; | |
if(font == "14px HemiHeadBold" && csscolor == "rgba(255,255,255,1)" && textStr.match(/\d+\/\d+/)){ | |
let match = textStr.match(/(\d+)\/(\d+)/); | |
if(+match[2] >= 50){ | |
GM_setValue("hpInfo-"+un, {int: +match[1], str: textStr}); | |
}else{ | |
GM_setValue("ammoInfo-"+un, {now: +match[1], max: +match[2]}); | |
} | |
} | |
let reg = new RegExp(`^\\[\\d{1,2}\\] ${ botsLead }`); | |
if(font == "14px TenbyFive" && csscolor == "rgba(255,255,255,1)" && textStr.match(reg)){ | |
leadTid = tid; | |
} | |
if(botsLead == un && font == "14px TenbyFive" && csscolor == "rgba(255,255,255,1)") { | |
for (const key in rope) { | |
let reg = new RegExp(`^\\[\\d{1,2}\\] ${ key }`); | |
if(textStr.match(reg)){ | |
rope[key].tid = tid | |
} | |
} | |
} | |
return tid; | |
}; | |
})(RenderEngineWebGL._Z21XDL_CreateTextTextureiPKcijS0_ij); | |
(function (orig) { | |
RenderEngineWebGL.addTexture = function (image) { | |
let tid = orig.apply(null, arguments); | |
if(image.userScriptTextureName){ | |
var name = image.userScriptTextureName; | |
}else{ | |
name = image.src.match(/\w+\.\d/)[0]; | |
} | |
if(name == "base.1") baseTid = tid; | |
return tid; | |
}; | |
})(RenderEngineWebGL.addTexture); | |
(function (orig) { | |
RenderEngineWebGL._Z15XDL_DrawTextureiiiiiiiiiiiiij9BlendMode = function(texture, sx, sy, sw, sh, dx, dy, dw, dh, rot, rpx, rpy, flip, cm, blendMode) { | |
if(texture == baseTid && sx == 1271 && sy == 1182){ | |
center.x = dx + dw/2; | |
center.y = dy + dh/2; | |
if(botsLead == un) { | |
canvas.setAttribute("width", Module.canvas.widthNative); | |
canvas.setAttribute("height", Module.canvas.heightNative); | |
let zoom = localStorage.userScriptZoom ?? 1; | |
let left = 250; | |
let top = 10; | |
let minHp = {int:1000, str: ""}; | |
for (const key in botsList){ | |
let hpInfo = GM_getValue("hpInfo-"+key); | |
if(hpInfo?.int < minHp.int) minHp = hpInfo; | |
} | |
ctx.textBaseline = "top" | |
ctx.font = 60*zoom+"px serif"; | |
let textSize = ctx.measureText(minHp.str); | |
ctx.strokeStyle = "#a5a5a5"; | |
ctx.fillStyle = "rgba(16, 16, 16, 0.9)"; | |
ctx.beginPath(); | |
ctx.roundRect(left-5, top-5, textSize.width+10, textSize.fontBoundingBoxDescent+10, 5); | |
ctx.stroke(); | |
ctx.fill(); | |
ctx.beginPath(); | |
ctx.fillStyle = "#f0f0f0"; | |
ctx.fillText(minHp.str, left, top); | |
ctx.fill(); | |
left+= textSize.width+20; | |
let nowAmmo = 0, maxAmmo = 0; | |
for (const key in botsList){ | |
let ammoInfo = GM_getValue("ammoInfo-"+key); | |
nowAmmo+= ammoInfo?.now; | |
maxAmmo+= ammoInfo?.max; | |
} | |
let ammoStr =nowAmmo+"/"+maxAmmo; | |
ctx.font = 50*zoom+"px serif"; | |
textSize = ctx.measureText(ammoStr); | |
ctx.strokeStyle = "#a5a5a5"; | |
ctx.fillStyle = "rgba(16, 16, 16, 0.9)"; | |
ctx.beginPath(); | |
ctx.roundRect(left-5, top-5, textSize.width+10, textSize.fontBoundingBoxDescent+10, 5); | |
ctx.stroke(); | |
ctx.fill(); | |
ctx.beginPath(); | |
ctx.fillStyle = "#f0f0f0"; | |
ctx.fillText(ammoStr, left, top); | |
ctx.fill(); | |
left+= textSize.width+20; | |
for (const key in rope) { | |
if(rope[key].lastSeen + 200 < Date.now() && key != botsLead){ | |
ctx.fillStyle = "#ff00ff"; | |
ctx.strokeStyle = "#000000"; | |
ctx.lineWidth = 0.7; | |
ctx.fillText(key, left, top); | |
ctx.strokeText(key, left, top); | |
ctx.fill(); | |
ctx.stroke(); | |
left+=ctx.measureText(key).width+10; | |
} | |
} | |
} | |
} | |
for (const key in rope) { | |
if(rope[key].tid == texture){ | |
let drawYdiff = 10; | |
rope[key].lastSeen = Date.now(); | |
let icHeight = 66.5; | |
let fabHeight = 63; | |
let shellHeight = icHeight; | |
rope[key].posX = dx + dw/2; | |
rope[key].posY = dy + dh/2 - shellHeight; | |
ctx.fillStyle = "#ff00ff"; | |
ctx.lineWidth = 3; | |
ctx.strokeStyle = "#000000"; | |
ctx.beginPath(); | |
ctx.arc(rope[key].posX, rope[key].posY+drawYdiff, 20, 0, 2 * Math.PI); | |
ctx.fill(); | |
ctx.stroke(); | |
ctx.beginPath(); | |
ctx.lineWidth = 5; | |
ctx.strokeStyle = "#000000"; | |
ctx.moveTo(center.x, center.y+drawYdiff); | |
ctx.lineTo(rope[key].posX, rope[key].posY+drawYdiff) | |
ctx.stroke(); | |
ctx.beginPath(); | |
ctx.lineWidth = 1; | |
ctx.strokeStyle = "#ff00ff"; | |
ctx.moveTo(center.x, center.y+drawYdiff); | |
ctx.lineTo(rope[key].posX, rope[key].posY+drawYdiff) | |
ctx.stroke(); | |
} | |
} | |
if(texture == leadTid){ | |
leadPos.x = dx + dw/2; | |
leadPos.y = dy + dh/2; | |
} | |
if(texture == missionCompletedTid){ | |
missionCompletedTid = 0; | |
setTimeout(()=>{ | |
Module.setCanvasSize(Module.canvas.widthNative, Module.canvas.heightNative); | |
Module.canvas.style.width = "100vw"; | |
Module.canvas.style.height = "100vh"; | |
},0); | |
} | |
if(document.hidden) return; | |
return orig.apply(null, arguments); | |
}; | |
})(RenderEngineWebGL._Z15XDL_DrawTextureiiiiiiiiiiiiij9BlendMode); | |
} | |
function waitUntilLoaded () { | |
if (typeof RenderEngineWebGL == 'undefined' || typeof XDL == 'undefined' || typeof Module === 'undefined') { | |
setTimeout(waitUntilLoaded, 0); | |
return; | |
} | |
onLoaded(); | |
} | |
waitUntilLoaded(); | |
function readString(b, fieldname) { | |
let i = b.indexOf(fieldname); | |
if (i === -1) return null; | |
return b.substr(i+fieldname.length+2, b.charCodeAt(i+fieldname.length+1)); | |
} | |
function getAccountUsername() { | |
let b = atob(localStorage.getItem('LocalStorage.pb')); | |
let n = readString(b, '\x08username'); | |
return n; | |
} | |
let canvas = document.createElement("canvas"); | |
canvas.id = "botsCanvas"; | |
const ctx = canvas.getContext("2d"); | |
document.body.appendChild(canvas); | |
const innerStyle = ` | |
#botsCanvas { | |
position: absolute; | |
left: 0px; | |
top: 0px; | |
pointer-events: none; | |
width: 100vw; | |
height: 100vh; | |
} | |
` | |
let css = document.createElement('style'); | |
css.innerHTML = innerStyle; | |
document.head.appendChild(css); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment