Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // == Xbox Cloud Gaming aim assist ===
- (function(){
- // --- Flags & State ---
- let enabled = true,
- pointerLocked = false,
- gameArea = null,
- stateInt, dispatchInt;
- const TOGGLE_KEY = 'end', KILL_KEY = 'home';
- console.log(' KBM Aim Assist – [End]=Toggle, [Home]=Kill');
- // --- Config ---
- const POLL_MS = 16,
- DISPATCH_MS = 100,
- AXIS_DEADZONE = 0.05;
- const BASE_SENS_X = 0.025,
- BASE_SENS_Y = 0.025,
- ADS_MULT = 1.75,
- MOUSE_DECAY = 0.0;
- const ENABLE_SMOOTH = true,
- SMOOTH_BASE = 0.5,
- SMOOTH_PEAK = 0.9,
- SMOOTH_RAMP_MS = 300;
- const ENABLE_MICRO = true,
- MICRO_RAD = 0.02,
- MICRO_SPD = 0.15;
- const ENABLE_RECOIL = true,
- FIRE_IDX = 7;
- const RECOIL_PRESETS = {
- scar: 0.020,
- m16: 0.017,
- compact_smg: 0.030,
- pump_shotgun: 0.005,
- bolt_sniper: 0.008,
- tac_shotgun: 0.007,
- heavy_sniper: 0.010
- };
- let currentPreset = 'scar';
- // --- Controller State ---
- const state = {
- axes: [0,0,0,0],
- buttons: Array(17).fill().map((_,i)=>({
- pressed:false, touched:false, value:0, index:i
- })),
- axisKey: {0:{neg:false,pos:false},1:{neg:false,pos:false}},
- dx: 0, dy:0,
- ts: performance.now(),
- microAngle: 0,
- adsStart: 0
- };
- // --- Mappings ---
- const ADS_IDX = 6;
- const keyMap = {
- w:{t:'a',a:1,v:-1}, s:{t:'a',a:1,v:1},
- a:{t:'a',a:0,v:-1}, d:{t:'a',a:0,v:1},
- ' ':{t:'b',i:0}, shift:{t:'b',i:10}, control:{t:'b',i:11},
- r:{t:'b',i:2}, e:{t:'b',i:3}, f:{t:'b',i:5}, g:{t:'b',i:1},
- escape:{t:'b',i:9}, tab:{t:'b',i:8}, m:{t:'b',i:8},
- z:{t:'b',i:14}, x:{t:'b',i:15}, c:{t:'b',i:13}, v:{t:'b',i:12}
- };
- const mouseMap = {
- 0:{t:'b',i:FIRE_IDX},
- 2:{t:'b',i:ADS_IDX}
- };
- // --- Build a real Gamepad object ---
- function makeGamepad() {
- const axes = state.axes.map(v=>Math.abs(v)<AXIS_DEADZONE ? 0 : v);
- const btns = state.buttons.map(b=>({
- pressed:b.pressed, touched:b.touched, value:b.value, index:b.index
- }));
- return {
- axes, buttons: btns, connected: true,
- id: "KBM Mapper v10.7", index: 0,
- mapping: "standard", timestamp: state.ts
- };
- }
- // --- Core Update Loop ---
- function updateState() {
- if (!enabled || !pointerLocked) return;
- const now = performance.now();
- const isADS = state.buttons[ADS_IDX].pressed;
- const isFire = state.buttons[FIRE_IDX].pressed;
- // manage ADS ramp timer
- if (isADS && state.adsStart===0) state.adsStart = now;
- if (!isADS) state.adsStart = 0;
- // base mouse deltas
- let sx = BASE_SENS_X, sy = BASE_SENS_Y;
- if (isADS) { sx *= ADS_MULT; sy *= ADS_MULT; }
- let rx = state.axes[2]*MOUSE_DECAY + state.dx * sx;
- let ry = state.axes[3]*MOUSE_DECAY + state.dy * sy;
- // circular micro-movement
- if (ENABLE_MICRO && isADS) {
- rx += Math.cos(state.microAngle) * MICRO_RAD;
- ry += Math.sin(state.microAngle) * MICRO_RAD;
- state.microAngle = (state.microAngle + MICRO_SPD) % (2*Math.PI);
- }
- // manual anti-recoil
- if (ENABLE_RECOIL && isFire) {
- ry += RECOIL_PRESETS[currentPreset] || 0;
- }
- // smoothing ramp
- if (ENABLE_SMOOTH && isADS) {
- const held = now - state.adsStart;
- const t = Math.min(1, held / SMOOTH_RAMP_MS);
- const factor = SMOOTH_BASE * (1 - t) + SMOOTH_PEAK * t;
- rx = state.axes[2]*factor + rx*(1-factor);
- ry = state.axes[3]*factor + ry*(1-factor);
- }
- // clamp to [-1,1]
- state.axes[2] = Math.max(-1, Math.min(1, rx));
- state.axes[3] = Math.max(-1, Math.min(1, ry));
- // clear deltas & timestamp
- state.dx = state.dy = 0;
- state.ts = now;
- }
- // --- Dispatch a real GamepadEvent ---
- function dispatchPad() {
- if (!enabled) return;
- const gp = makeGamepad();
- try {
- const evt = new GamepadEvent('gamepadconnected', { gamepad: gp });
- window.dispatchEvent(evt);
- } catch (e) {
- console.error('Failed to dispatch GamepadEvent:', e);
- }
- }
- // --- Input Helpers ---
- function prevent(e) { e.preventDefault(); e.stopPropagation(); }
- function shouldCapture(key, code) {
- if ([TOGGLE_KEY, KILL_KEY].includes(key)) return false;
- if (/^[a-z0-9]$/.test(key)) return true;
- if ("`-=[]\\;',./~_+{}|:\"<>?".includes(key)) return true;
- return ['Tab','Enter','Escape','ArrowUp','ArrowDown','ArrowLeft','ArrowRight']
- .includes(code);
- }
- // --- Keyboard Handlers ---
- function onKeyDown(e) {
- const k = e.key.toLowerCase();
- if (k === KILL_KEY) { teardown(); return prevent(e); }
- if (k === TOGGLE_KEY) { toggle(); return prevent(e); }
- if (!enabled) return;
- // recoil preset switch (1–7)
- if (ENABLE_RECOIL && /[1-7]/.test(k)) {
- const arr = ['scar','m16','compact_smg','pump_shotgun','bolt_sniper','tac_shotgun','heavy_sniper'];
- currentPreset = arr[+k-1];
- console.log(` Recoil → ${currentPreset} (${RECOIL_PRESETS[currentPreset]})`);
- return prevent(e);
- }
- const m = keyMap[k];
- if (m) {
- prevent(e);
- if (m.t === 'b') {
- const btn = state.buttons[m.i];
- btn.pressed = btn.touched = true; btn.value = 1;
- } else {
- const ax = state.axisKey[m.a];
- m.v < 0 ? ax.neg = true : ax.pos = true;
- state.axes[m.a] = ax.neg && ax.pos ? 0 : (ax.neg ? -1 : 1);
- }
- } else if (shouldCapture(k, e.code)) {
- prevent(e);
- }
- }
- function onKeyUp(e) {
- const k = e.key.toLowerCase();
- if ([KILL_KEY, TOGGLE_KEY].includes(k)) return prevent(e);
- if (!enabled) return;
- if (ENABLE_RECOIL && /[1-7]/.test(k)) return prevent(e);
- const m = keyMap[k];
- if (m) {
- prevent(e);
- if (m.t === 'b') {
- const btn = state.buttons[m.i];
- btn.pressed = btn.touched = false; btn.value = 0;
- } else {
- const ax = state.axisKey[m.a];
- m.v < 0 ? ax.neg = false : ax.pos = false;
- state.axes[m.a] = ax.neg ? -1 : ax.pos ? 1 : 0;
- }
- } else if (shouldCapture(k, e.code)) {
- prevent(e);
- }
- }
- // --- Mouse Handlers ---
- function onMouseDown(e) {
- if (!enabled) return;
- const m = mouseMap[e.button]; if (!m) return;
- prevent(e);
- const btn = state.buttons[m.i];
- btn.pressed = btn.touched = true; btn.value = 1;
- }
- function onMouseUp(e) {
- if (!enabled) return;
- const m = mouseMap[e.button]; if (!m) return;
- prevent(e);
- const btn = state.buttons[m.i];
- btn.pressed = btn.touched = false; btn.value = 0;
- }
- function onMouseMove(e) {
- if (!enabled || !pointerLocked) return;
- prevent(e);
- state.dx += e.movementX || 0;
- state.dy += e.movementY || 0;
- }
- // --- Pointer Lock ---
- function onPointerLockChange() {
- pointerLocked = (document.pointerLockElement === gameArea);
- console.log(pointerLocked ? '🔒 Pointer Locked' : '🔓 Pointer Unlocked');
- if (!pointerLocked) {
- state.axes[2] = state.axes[3] = 0;
- state.microAngle = 0;
- }
- }
- function requestLock() {
- gameArea?.requestPointerLock?.();
- }
- // --- Toggle & Teardown ---
- function toggle() {
- enabled = !enabled;
- console.log(enabled ? '%cMapper ON' : '%cMapper OFF',
- 'font-weight:bold;color:' + (enabled ? 'lightgreen' : 'orange'));
- if (!enabled) {
- // reset controller state
- state.axes = [0,0,0,0];
- state.buttons.forEach(b=>{b.pressed=b.touched=false; b.value=0});
- state.axisKey = {0:{neg:false,pos:false},1:{neg:false,pos:false}};
- currentPreset = 'scar';
- if (pointerLocked) document.exitPointerLock();
- }
- }
- function teardown() {
- enabled = false;
- clearInterval(stateInt);
- clearInterval(dispatchInt);
- window.removeEventListener('keydown', onKeyDown, true);
- window.removeEventListener('keyup', onKeyUp, true);
- gameArea.removeEventListener('mousedown', onMouseDown, true);
- gameArea.removeEventListener('mouseup', onMouseUp, true);
- gameArea.removeEventListener('mousemove', onMouseMove, true);
- gameArea.removeEventListener('click', requestLock);
- document.removeEventListener('pointerlockchange', onPointerLockChange);
- navigator.getGamepads = () => [];
- console.warn('%cKBM Mapper v10.7 TERMINATED – refresh to reload','color:red;font-weight:bold;');
- }
- // --- Initialization ---
- (function init(){
- gameArea = document.getElementById('game-stream')
- || document.querySelector('video[playsinline]')
- || document.querySelector('video')
- || document.body;
- navigator.getGamepads = () => enabled ? [ makeGamepad() ] : [];
- window.addEventListener('keydown', onKeyDown, true);
- window.addEventListener('keyup', onKeyUp, true);
- gameArea.addEventListener('mousedown', onMouseDown, true);
- gameArea.addEventListener('mouseup', onMouseUp, true);
- gameArea.addEventListener('mousemove', onMouseMove, true);
- gameArea.addEventListener('click', requestLock,false);
- document.addEventListener('pointerlockchange', onPointerLockChange,false);
- stateInt = setInterval(updateState, POLL_MS);
- dispatchInt = setInterval(dispatchPad, DISPATCH_MS);
- console.log(' KBM Aim Assist ACTIVE – click to lock pointer');
- console.log(' Press 1–7 to select recoil preset:', Object.keys(RECOIL_PRESETS).join(', '));
- })();
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement