var text_logger = (function() {
let texts = new Set();
let clanTags = new Set();
// regex filter anything between two square brackets
const clanTagRegex = /[(.*?)]/g;
function addClanTags(text) {
let matches = text.match(clanTagRegex);
if (matches) {
for (let match of matches) {
clanTags.add(match);
}
}
}
return {
// check if an activation sequence (string) matches the event's keydown
matchesActivationSeq: function(e, keybind) {
if (!keybind) { return false; }
const parts = keybind.split('+');
const key = parts.pop(); // The last part is the key
const modifiers = {
Shift: e.shiftKey,
Ctrl: e.ctrlKey,
Alt: e.altKey,
Meta: e.metaKey // Meta is the Command key on Mac and Windows key on Windows
};
code
// Check if all specified modifiers are pressed
for (const modifier of parts) {
if (!modifiers[modifier]) {
return false; // If any modifier is not pressed, return false
}
}
// Check if the actual key pressed matches the specified key
// Note: For digits and symbols, use e.key instead of e.code because e.code might not match the actual character depending on the keyboard layout
return e.code === keyCodeMap.get(key) || e.key === key;
},
keyCodeMap: keyCodeMap
const FUNC_INPUT_ID = 're_func_input';
let enabled = false;
let re_input = false;
let rot_offset = 0;
function update_rot_offset(xy) {
console.log(xy: ${JSON.stringify(xy)});
let x = xy.x - window.innerWidth / 2;
let y = -xy.y + window.innerHeight / 2;
code
if (x === 0 && y === 0) {
return;
}
rot_offset = Math.atan2(y, x);
}
// await delay
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// map mouse button names to mouse.button values
const mouseButtonMap = new Map([
['left', 0],
['middle', 1],
['right', 2]
]);
// store the elements that called EventTarget.prototype.addEventListener
let eventListenerElementMap = new Map();
// simulate a mouse down and up event at a given position
function simulateMouseDownUp(xy, button, delay = 0) {
let button_ = mouseButtonMap.get(button);
if (button_ === undefined) { return; }
code
let event = { clientX: xy.x, clientY: xy.y, button: button_ };
console.log(`dispatching mousedown, event: ${JSON.stringify(event)}`);
// these two should be the same el but we'll keep them separate just in case
let down_el = eventListenerElementMap.get('mousedown');
let up_el = eventListenerElementMap.get('mouseup');
down_el.dispatchEvent(new MouseEvent('mousedown', event));
if (delay === 0) {
console.log(`dispatching mouseup`);
up_el.dispatchEvent(new MouseEvent('mouseup', event));
return;
}
setTimeout(function() {
console.log(`dispatching mouseup`);
up_el.dispatchEvent(new MouseEvent('mouseup', event));
}, delay); // delay in milliseconds
}
// simulate a mouse move event at a given position
function simulateMouseMove(xy_, c = true) {
let xy = xy_;
if (c) {
xy = {x: xy_.x + window.innerWidth / 2, y: -xy_.y + window.innerHeight / 2};
}
let event = { clientX: xy.x, clientY: xy.y };
re_input = true;
code
let el = eventListenerElementMap.get('mousemove');
el.dispatchEvent(new MouseEvent('mousemove', event));
// el.dispatchEvent(event);
}
// press down key for delay milliseconds
function simulateKeyDownUp(key, delay = 0) {
let code = key_utils.keyCodeMap.get(key);
let event = { key: key, code: code };
if (!code) { return; }
window.dispatchEvent(new KeyboardEvent('keydown', event));
// modify keydown and keyup event listeners to trust all (including programmatic) events
const originalAddEventListener = window.addEventListener;
window.addEventListener = function(type, listener, options) {
if (type === 'keydown' || type === 'keyup') {
// get the element being focused rn
code
const originalListener = listener;
listener = function(event) {
const focusedEl = document.activeElement;
if (focusedEl.id === FUNC_INPUT_ID) {
// originalListener.call(this, event);
return;
}
// object.assign doesn't work
// -: -0.1 rot_offset
// +: +0.1 rot_offset
if (event.key === '-') {
rot_offset -= 0.1;
} else if (event.key === '+') {
rot_offset += 0.1;
}
let event_ = {
ctrlKey: event.ctrlKey, altKey: event.altKey, shiftKey: event.shiftKey, metaKey: event.metaKey,
key: event.key, code: event.code, repeat: event.repeat,
preventDefault: event.preventDefault.bind(event),
isTrusted: true
};
// if (runningSeq) { // push these events to some queue and execute them after the sequence is done
// queuedEvents.push({event: event_, listener: originalListener});
// }
originalListener.call(this, event_);
};
}
return originalAddEventListener.call(this, type, listener, options);
function simulate(mspt, f) {
const start = performance.now();
code
// call f every mspt ms
move_interval = setInterval(function() {
let t = performance.now() - start;
let rot = f(t, mspt);
rot += rot_offset;
let R = get_radius();
let x = R * Math.cos(rot);
let y = R * Math.sin(rot);
simulateMouseMove({x: x, y: y}, true);
}, mspt);
}
let mspt = 1;
let f = function(t, m) {
return 2 * Math.PI * (t / 1000);
};
// redefine start & the interval HERE
// simulate(mspt, f);
The userscript you provided is written to enhance the gameplay experience on Arras.io by creating custom rotation editing functionality. Here's an explanation of key aspects of the userscript:
General Overview
The script implements a custom "rotation editor" tool for manipulating cursor movement (rotations) programmatically based on a mathematical function. Players can define their own rotation logic as a function, enabling advanced control over cursor behavior in the game.
The script achieves this by:
Listening to mouse and keyboard events in-game.
Providing an interface for defining and applying custom cursor rotation logic.
Allowing users to enable or disable the custom rotation logic.
Key Elements in the Script
1. Customizable Rotation Function
Users can input a custom rotation function (f) that defines how the cursor rotates over time.
The rotation function accepts two parameters:
t: Elapsed time (in milliseconds) since the script started running.
m: Milliseconds per tick (related to game frames or rotations per second).
Example provided in the script:
javascript
return 0.83 * 2 * Math.PI * (t / 1000);
This produces a smooth circular rotation.
2. Circle Radius Customization
Two radius values (r_min and r_max) define the range of the circle in which the cursor moves.
get_radius() computes the effective radius for the rotation, which can be adjusted as needed.
3. Custom UI
A draggable UI container (#dd_re_container) overlay is added to the webpage. It contains:
A textarea for defining the custom rotation function.
A button to apply the function (Set Function), along with inputs for mspt (milliseconds per tick).
Enable/disable (toggle) and reset buttons for activating or deactivating the rotation logic in real time.
4. Simulated Mouse Movement
Uses simulateMouseMove() to emulate mouse events based on the current computed rotation (rot) and radius (R).
User
I want x^{\left(\cos x\right)\sin x^{2}} in textarea
Assistant
To display the mathematical expression ( x^{(\cos x)\sin(x^2)} ) correctly in a <textarea> in a way that it can accurately represent the intent, you need to use an ASCII or LaTeX-like representation since textareas don't support superscripts or formatting. Here's a representation for it:
Plain Text Representation
You can enter the expression in the <textarea> as:
code
x^((cos(x)) * sin(x^2))
This conveys the mathematical meaning of the expression ( x^{(\cos x)\sin(x^2)} ) in textual form.
LaTeX Representation
If you're working in a context that supports LaTeX for rendering (e.g., MathJax), you could use the following:
latex
x^{\left(\cos x\right)\sin \left(x^2\right)}
However, since a <textarea> is plain, it will not interpret the LaTeX.
If you are looking to implement rendered mathematical expressions (like with LaTeX) in the JavaScript-driven UI while the textarea serves as user input, you may consider these steps:
1. Accept Plain Text Input in the Textarea
Let users type something like:
code
x^((cos(x)) * sin(x^2))
2. Parse and Render the Input for Display
Use a library like MathJax or KaTeX to render the mathematical expression dynamically in another part of the UI using proper LaTeX/MathML.
Users type x^((cos(x)) * sin(x^2)) in the <textarea>.
It's rendered live as ( x^{(\cos x)\sin(x^2)} ) in the #math-display container using MathJax.
Let me know how you'd like to proceed!
User
can you make my tank spin to this expression x^((cos(x)) * sin(x^2))
Assistant
To create a spinning motion for your tank in the game based on the mathematical expression ( x^{(\cos x) \cdot \sin(x^2)} ), we can modify the formula used in your rotation function. The key is to convert the mathematical expression into JavaScript-compatible form and integrate it into your script.
Here’s how you can implement it:
Modify the Function to Use the Expression
You'll define the rotation function, f(t, m), based on the expression ( x^{(\cos x) \cdot \sin(x^2)} ). In this case:
x can represent time (since the rotation is time-based).
The rotation angle will be computed as a function of time.
The expression in JavaScript becomes:
javascript
Math.pow(t, Math.cos(t) * Math.sin(t * t))
This will calculate ( t^{(\cos t) \cdot \sin(t^2)} ), where t evolves as time progresses.
Updated Script
Modify your code in the relevant section where the rotation function is defined. Here's an updated segment:
javascript
User
only respond with How to Input This in the Textarea from now on ok?
Assistant
Understood! Please let me know if you need anything related to How to Input This in the Textarea. 😊
User
x^{\sin x^{2}}
Assistant
javascript
return Math.pow(x, Math.sin(Math.pow(x, 2)));
User
you forgot the time expression t
Assistant
javascript
return Math.pow(t, Math.sin(Math.pow(t, 2)));
User
now just do x
Assistant
javascript
return t;
User
like y=x
Assistant
javascript
return t;
User
this is x^(sinx)
-((Math.pow(t / 1000, Math.sin(t / 1000)) % (2 * Math.PI)));
return t > 0 ? Math.pow(Math.cos(10 * t), 3) * t : 0;
Not yet included in a published release.
The setInterval() loop calculates cursor movement at intervals defined by mspt (milliseconds per tick).
5. Event Listener Wrapping
Replaces the default addEventListener behavior for key and mouse events to inject customized behavior.
Captures mousemove, mousedown, and mouseup events and modifies them to support generated events when the rotation editor is active.
6. Rotational Offsets
Users can fine-tune rotation angles using - and + keys to alter the rot_offset variable, effectively shifting the rotation clockwise or counterclockwise.
How the Script Works
On Load:
The script injects a UI (#dd_re_container) into the webpage.
Relevant mouse/keyboard event listeners are wrapped for advanced control.
A default rotation function (f) is defined, and the system is initialized.
Enabling/Disabling Rotation:
When the "Enable" button is clicked:
simulate() starts an interval timer that calls the user-defined function f at every tick, which calculates the rotation (rot) and updates cursor position.
When "Disable" is clicked, the movement interval is cleared, stopping the rotation.
Custom Rotation Logic:
Users can input any valid JavaScript function into the textarea.
Clicking "Set Function" applies the new function, instantly updating the simulation logic.
Simulating Custom Events:
Custom mouse movements and key presses are simulated programmatically using MouseEvent and KeyboardEvent objects.
These events mimic real user interactions but are carefully crafted to include game-specific adjustments like rot_offset.
Key Variables and Functions
Variable/Function
Description
t
Time elapsed (in milliseconds) since the rotation simulation started.
mspt
Milliseconds per tick, defining how often the cursor position updates during rotation.
simulate(mspt, f)
Starts invoking the rotation function (f) every mspt milliseconds to calculate cursor position offsets.
rot_offset
A rotational offset (radians) applied to the calculated angle (rot) for fine-tuning cursor direction.
simulateMouseMove()
Simulates a custom mousemove event, generating new cursor positions based on the circle's radius and rotation.
f
A user-defined rotation function that specifies cursor behavior.
#re_func_input
The textarea where users input their custom rotation function.
toggle_btn
The toggle button that enables or disables the rotation simulation logic.
CanvasRenderingContext2D.prototype.fillText
Hook used to intercept and process text elements drawn on the game canvas (clan tags, etc.).
Adjustments for Compatibility
For proper functionality, ensure this script is only executed in a safe and legal manner. Avoid using it to violate the primary game's terms of service, as custom automation could result in penalties.
If you have specific questions or need modifications (e.g., additional functionality), feel free to ask!
(function() {
const FUNC_INPUT_ID = 're_func_input';
let enabled = false;
let re_input = false;
let rot_offset = 0;
// Await delay
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// Core simulation function
function simulate(mspt, f) {
const start = performance.now();
// Call the user-defined function `f` every `mspt` milliseconds
move_interval = setInterval(function() {
let t = (performance.now() - start) / 1000; // Convert time to seconds
// Use the rotation defined by the custom mathematical function
let rot = f(t, mspt);
// Add rotational offset (if enabled)
rot += rot_offset;
let R = get_radius(); // Radius of rotation
let x = R * Math.cos(rot); // X-coordinate of the cursor
let y = R * Math.sin(rot); // Y-coordinate of the cursor
simulateMouseMove({ x: x, y: y }, true); // Simulate mouse movement to rotate
}, mspt);
}
// Define your mathematical rotation function
let mspt = 1; // Milliseconds per tick
let f = function(t, m) {
return Math.pow(t, Math.cos(t) * Math.sin(t * t));
};
// Attach the function to the textarea's "Set Function" button
const func_btn = document.querySelector('#set_func_button');
func_btn.addEventListener('click', (e) => {
// Read function body from the user input
const f_body = document.querySelector(`#${FUNC_INPUT_ID}`).value;
try {
f = new Function('t', 'm', f_body);
if (enabled) {
clearInterval(move_interval);
simulate(mspt, f);
}
} catch (error) {
console.error('Invalid function body:', error);
}
});
// Example of how the function would look in the textarea:
const exampleFunctionText = `
return Math.pow(t, Math.cos(t) * Math.sin(t * t));
`;
})();
Explanation of Implementation
Rotation Function:
The core of the spinning motion is controlled by the function f(t, m). It accepts:
t: The elapsed time (in seconds).
m: Milliseconds per tick, though it's unused in this case.
The return value of f determines the rotation angle.
Expression ( x^{(\cos x) \cdot \sin(x^2)} ):
Written in JavaScript syntax as:
javascript
Math.pow(t, Math.cos(t) * Math.sin(t * t));
Here, Math.pow(t, ...) calculates t raised to a power.
Radius:
get_radius() remains the same as in your original script, calculating the radius for the spinning motion.
Simulating Mouse Movement:
The script uses simulateMouseMove() to programmatically move the cursor in a circular or custom pattern.
How to Input This in the Textarea
Paste the following expression for the function in the textarea: