Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (async function() {
- const startX = -20;
- const startY = -10;
- const endX = 19;
- const endY = 9;
- const areaWidth = endX - startX + 1;
- const typingCooldown = 10;
- let isRunning = true;
- let safetyEnabled = true;
- let chatText = '';
- let chatX = 0;
- let chatY = 0;
- let chatTimeout = null;
- let cloudAnimationInterval = null;
- let dayNightInterval = null;
- let timeUpdateInterval = null;
- // Time phases
- const PHASE_DAY = 0;
- const PHASE_SUNSET = 1;
- const PHASE_NIGHT = 2;
- const PHASE_SUNRISE = 3;
- let currentPhase = PHASE_DAY;
- const fullCycleDuration = 150000; // 150 seconds full cycle
- const phaseDuration = fullCycleDuration / 4; // 37.5 seconds per phase
- let phaseStartTime = Date.now();
- // Colors for each phase
- const dayColor = 10;
- const sunsetColor = 5;
- const nightColor = 30;
- const sunriseColor = 26;
- const starColor = 0;
- // Characters for each phase
- const dayChar = '▒';
- const sunsetChar = '▒';
- const nightChar = '▒';
- const sunriseChar = '▒';
- const starChar = '•';
- const cloudChar = '~';
- // Star positions
- let starPositions = [];
- // Simulation time
- let simYear = 0;
- let simMonth = 0;
- let simDay = 1;
- let simHour = 6;
- let simMinute = 0;
- const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
- const daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
- // Date/Time display positions
- const dateDisplayX = 21;
- const dateDisplayY = -10;
- const timeDisplayX = 21;
- const timeDisplayY = -8;
- // Previous display lengths for clearing
- let prevDateTextLength = 0;
- let prevTimeTextLength = 0;
- // Correct chars list for version, coords, date, time
- let correctChars = [];
- // World position of square
- let squareWorldX = 0;
- let squareWorldY = 0;
- // Camera position
- let cameraWorldX = startX;
- // Terrain Y levels (base values)
- const baseGrassY = 1;
- const baseDirtStartY = 2;
- const baseDirtEndY = 3;
- const baseStoneStartY = 4;
- const cloudEndY = -7;
- // Characters
- const grassChar = ',';
- const dirtChar = '.';
- const stoneChar = '#';
- // Colors
- const cloudColor = 0;
- const grassColor = 9;
- const dirtColor = 22;
- const stoneColor = 1;
- const defaultColor = 25;
- // Cloud data structure
- let clouds = [];
- // Terrain height cache
- let terrainHeights = {};
- // World border
- const worldBorderMinX = -500000;
- const worldBorderMaxX = 500000;
- // Input values for movement
- const inputx = 1;
- const inputy = 1;
- // Coordinate display position
- const coordDisplayX = startX;
- const coordDisplayY = startY;
- const versionDisplayY = startY + 1;
- const versionText = 'Square Area Testing 1.1.1';
- // Previous coordinate text
- let prevCoordText = '';
- let prevCoordTextLength = 0;
- // Helper function for cooldown
- function wait(ms) {
- return new Promise(resolve => setTimeout(resolve, ms));
- }
- // Simple seeded random
- function seededRandom(seed) {
- let x = Math.sin(seed * 12.9898 + 78.233) * 43758.5453;
- return x - Math.floor(x);
- }
- // Weak perlin-like noise for terrain
- function perlinNoise(x) {
- let noise = 0;
- noise += Math.sin(x * 0.15) * 0.4;
- noise += Math.sin(x * 0.4 + 100) * 0.3;
- noise += Math.sin(x * 0.8 + 200) * 0.15;
- noise += (seededRandom(Math.floor(x)) - 0.5) * 0.3;
- return noise;
- }
- // Get grass height offset at world X (0 or 1)
- function getGrassHeightOffset(worldX) {
- let key = 'h' + worldX;
- if (terrainHeights[key] !== undefined) {
- return terrainHeights[key];
- }
- let noise = perlinNoise(worldX);
- let offset = noise > 0.1 ? 0 : 1;
- terrainHeights[key] = offset;
- return offset;
- }
- // Get actual grass Y at world X
- function getGrassY(worldX) {
- return baseGrassY - getGrassHeightOffset(worldX);
- }
- // Get current sky color based on phase
- function getSkyColor() {
- switch (currentPhase) {
- case PHASE_DAY: return dayColor;
- case PHASE_SUNSET: return sunsetColor;
- case PHASE_NIGHT: return nightColor;
- case PHASE_SUNRISE: return sunriseColor;
- default: return dayColor;
- }
- }
- // Get current sky char based on phase
- function getSkyChar() {
- switch (currentPhase) {
- case PHASE_DAY: return dayChar;
- case PHASE_SUNSET: return sunsetChar;
- case PHASE_NIGHT: return nightChar;
- case PHASE_SUNRISE: return sunriseChar;
- default: return dayChar;
- }
- }
- // Calculate time based on phase progress
- function calculateSimTime() {
- let now = Date.now();
- let elapsed = now - phaseStartTime;
- let progress = Math.min(elapsed / phaseDuration, 1);
- let startHour, hoursInPhase;
- switch (currentPhase) {
- case PHASE_DAY:
- startHour = 6;
- hoursInPhase = 12;
- break;
- case PHASE_SUNSET:
- startHour = 18;
- hoursInPhase = 3;
- break;
- case PHASE_NIGHT:
- startHour = 21;
- hoursInPhase = 7;
- break;
- case PHASE_SUNRISE:
- startHour = 4;
- hoursInPhase = 2;
- break;
- default:
- startHour = 6;
- hoursInPhase = 12;
- }
- let currentHourFloat = startHour + (progress * hoursInPhase);
- if (currentHourFloat >= 24) {
- currentHourFloat -= 24;
- }
- simHour = Math.floor(currentHourFloat);
- simMinute = Math.floor((currentHourFloat - simHour) * 60);
- }
- // Generate star positions for night sky
- function generateStars() {
- starPositions = [];
- let seed = simDay * 100 + simMonth * 10 + simYear;
- for (let y = startY; y <= cloudEndY; y++) {
- for (let x = startX; x <= endX; x++) {
- let rand = seededRandom(seed + x * 100 + y);
- if (rand < 0.08) {
- starPositions.push({ x: x, y: y });
- }
- }
- }
- for (let y = cloudEndY + 1; y < 0; y++) {
- for (let x = startX; x <= endX; x++) {
- let rand = seededRandom(seed + x * 100 + y + 500);
- if (rand < 0.05) {
- starPositions.push({ x: x, y: y });
- }
- }
- }
- }
- // Check if position has a star
- function hasStarAt(x, y) {
- if (currentPhase !== PHASE_NIGHT) return false;
- for (let i = 0; i < starPositions.length; i++) {
- if (starPositions[i].x === x && starPositions[i].y === y) {
- return true;
- }
- }
- return false;
- }
- // Format date string
- function getDateString() {
- return months[simMonth] + ' ' + simDay + ' ' + String(simYear).padStart(4, '0');
- }
- // Format time string
- function getTimeString() {
- let hourStr = String(simHour).padStart(2, '0');
- let minStr = String(simMinute).padStart(2, '0');
- return hourStr + ':' + minStr;
- }
- // Increment date (called after full day/night cycle)
- function incrementDate() {
- simDay += 1;
- if (simDay > daysInMonth[simMonth]) {
- simDay = 1;
- simMonth += 1;
- if (simMonth >= 12) {
- simMonth = 0;
- simYear += 1;
- }
- }
- generateStars();
- }
- // Build version correct list
- function buildVersionCorrectList() {
- for (let i = 0; i < versionText.length; i++) {
- let key = (coordDisplayX + i) + ',' + versionDisplayY;
- correctChars[key] = { char: versionText[i], color: defaultColor };
- }
- }
- // Build/update coord correct list
- function buildCoordCorrectList() {
- let coordText = 'square is at ' + squareWorldX + ', ' + (-squareWorldY);
- if (prevCoordTextLength > 0) {
- for (let i = 0; i < prevCoordTextLength; i++) {
- let key = (coordDisplayX + i) + ',' + coordDisplayY;
- delete correctChars[key];
- }
- }
- for (let i = 0; i < coordText.length; i++) {
- let key = (coordDisplayX + i) + ',' + coordDisplayY;
- correctChars[key] = { char: coordText[i], color: defaultColor };
- }
- prevCoordText = coordText;
- prevCoordTextLength = coordText.length;
- }
- // Build/update date correct list
- function buildDateCorrectList() {
- let dateText = getDateString();
- if (prevDateTextLength > 0) {
- for (let i = 0; i < prevDateTextLength; i++) {
- let key = (dateDisplayX + i) + ',' + dateDisplayY;
- delete correctChars[key];
- }
- }
- for (let i = 0; i < dateText.length; i++) {
- let key = (dateDisplayX + i) + ',' + dateDisplayY;
- correctChars[key] = { char: dateText[i], color: defaultColor };
- }
- prevDateTextLength = dateText.length;
- }
- // Build/update time correct list
- function buildTimeCorrectList() {
- let timeText = getTimeString();
- if (prevTimeTextLength > 0) {
- for (let i = 0; i < prevTimeTextLength; i++) {
- let key = (timeDisplayX + i) + ',' + timeDisplayY;
- delete correctChars[key];
- }
- }
- for (let i = 0; i < timeText.length; i++) {
- let key = (timeDisplayX + i) + ',' + timeDisplayY;
- correctChars[key] = { char: timeText[i], color: defaultColor };
- }
- prevTimeTextLength = timeText.length;
- }
- // Check if position is in correct chars list
- function getCorrectChar(x, y) {
- let key = x + ',' + y;
- return correctChars[key] || null;
- }
- // Generate random clouds
- function generateClouds() {
- clouds = [];
- for (let y = startY; y <= cloudEndY; y++) {
- let x = startX;
- let seed = y * 1000 + currentCloudPattern * 100;
- while (x <= endX) {
- let rand = seededRandom(seed + x);
- if (rand < 0.35) {
- let length = Math.floor(seededRandom(seed + x + 1) * 4) + 3;
- if (x + length > endX + 1) {
- length = endX - x + 1;
- }
- if (length >= 3) {
- clouds.push({ x: x, y: y, length: length });
- }
- x += length + Math.floor(seededRandom(seed + x + 2) * 4) + 2;
- } else {
- x += 1;
- }
- }
- }
- }
- let currentCloudPattern = 0;
- // Get screen X from world X
- function worldToScreenX(worldX) {
- return worldX - cameraWorldX + startX;
- }
- // Get world X from screen X
- function screenToWorldX(screenX) {
- return screenX - startX + cameraWorldX;
- }
- // Get square screen position
- function getSquareScreenX() {
- return worldToScreenX(squareWorldX);
- }
- // Update camera to follow square
- function updateCamera() {
- let screenX = getSquareScreenX();
- let margin = 8;
- if (screenX < startX + margin) {
- cameraWorldX = squareWorldX - margin;
- } else if (screenX > endX - margin - 1) {
- cameraWorldX = squareWorldX - (areaWidth - margin - 2);
- }
- }
- // Check if screen position has a cloud
- function hasCloudAt(screenX, screenY) {
- for (let i = 0; i < clouds.length; i++) {
- let cloud = clouds[i];
- if (cloud.y === screenY && screenX >= cloud.x && screenX < cloud.x + cloud.length) {
- return true;
- }
- }
- return false;
- }
- // Get terrain info for a screen position
- function getTerrainInfo(screenX, y) {
- let worldX = screenToWorldX(screenX);
- let grassY = getGrassY(worldX);
- let dirtStartY = grassY + 1;
- let dirtEndY = grassY + 2;
- let stoneStartY = grassY + 3;
- // Cloud area
- if (y <= cloudEndY) {
- if (hasCloudAt(screenX, y)) {
- return { char: cloudChar, color: cloudColor, isSky: false };
- } else if (hasStarAt(screenX, y)) {
- return { char: starChar, color: starColor, isSky: true };
- } else {
- return { char: getSkyChar(), color: getSkyColor(), isSky: true };
- }
- }
- // Sky area (between clouds and grass)
- if (y < grassY) {
- if (hasStarAt(screenX, y)) {
- return { char: starChar, color: starColor, isSky: true };
- } else {
- return { char: getSkyChar(), color: getSkyColor(), isSky: true };
- }
- }
- // Grass
- if (y === grassY) {
- return { char: grassChar, color: grassColor, isSky: false };
- }
- // Dirt
- if (y >= dirtStartY && y <= dirtEndY) {
- return { char: dirtChar, color: dirtColor, isSky: false };
- }
- // Stone
- if (y >= stoneStartY && y <= endY) {
- return { char: stoneChar, color: stoneColor, isSky: false };
- }
- // Default sky
- return { char: getSkyChar(), color: getSkyColor(), isSky: true };
- }
- // Function to draw text at position
- async function drawText(x, y, text) {
- await w.changeColor(defaultColor);
- for (let i = 0; i < text.length; i++) {
- await w.tp(x + i, y);
- await w.typeChar(text[i], 1);
- await wait(typingCooldown);
- }
- }
- // Function to clear text at position with terrain
- async function clearTextWithTerrain(x, y, length) {
- for (let i = 0; i < length; i++) {
- let terrain = getTerrainInfo(x + i, y);
- await w.changeColor(terrain.color);
- await w.tp(x + i, y);
- await w.typeChar(terrain.char, 1);
- await wait(typingCooldown);
- }
- }
- // Function to draw sky with day/night/stars
- async function drawSky() {
- let skyColor = getSkyColor();
- let skyChar = getSkyChar();
- let squareScreenX = getSquareScreenX();
- // Draw sky in cloud area
- for (let y = startY; y <= cloudEndY; y++) {
- for (let x = startX; x <= endX; x++) {
- if (getCorrectChar(x, y)) {
- continue;
- }
- if (y === squareWorldY && (x === squareScreenX || x === squareScreenX + 1)) {
- continue;
- }
- if (chatText.length > 0 && y === chatY && x >= chatX && x < chatX + chatText.length) {
- continue;
- }
- if (hasCloudAt(x, y)) {
- await w.changeColor(cloudColor);
- await w.tp(x, y);
- await w.typeChar(cloudChar, 1);
- } else if (hasStarAt(x, y)) {
- await w.changeColor(starColor);
- await w.tp(x, y);
- await w.typeChar(starChar, 1);
- } else {
- await w.changeColor(skyColor);
- await w.tp(x, y);
- await w.typeChar(skyChar, 1);
- }
- await wait(typingCooldown);
- }
- }
- // Draw air between clouds and lowest possible grass
- for (let y = cloudEndY + 1; y < baseGrassY; y++) {
- for (let x = startX; x <= endX; x++) {
- if (y === squareWorldY && (x === squareScreenX || x === squareScreenX + 1)) {
- continue;
- }
- if (chatText.length > 0 && y === chatY && x >= chatX && x < chatX + chatText.length) {
- continue;
- }
- let terrain = getTerrainInfo(x, y);
- if (terrain.isSky) {
- if (hasStarAt(x, y)) {
- await w.changeColor(starColor);
- await w.tp(x, y);
- await w.typeChar(starChar, 1);
- } else {
- await w.changeColor(skyColor);
- await w.tp(x, y);
- await w.typeChar(skyChar, 1);
- }
- } else {
- await w.changeColor(terrain.color);
- await w.tp(x, y);
- await w.typeChar(terrain.char, 1);
- }
- await wait(typingCooldown);
- }
- }
- await w.changeColor(defaultColor);
- }
- // Function to draw all clouds
- async function drawClouds() {
- generateClouds();
- await drawSky();
- }
- // Function to draw terrain (grass, dirt, stone with perlin height)
- async function drawTerrain() {
- let squareScreenX = getSquareScreenX();
- for (let x = startX; x <= endX; x++) {
- let worldX = screenToWorldX(x);
- let grassY = getGrassY(worldX);
- let dirtStartY = grassY + 1;
- let dirtEndY = grassY + 2;
- let stoneStartY = grassY + 3;
- // Draw grass
- if (!(squareWorldY === grassY && (x === squareScreenX || x === squareScreenX + 1))) {
- await w.changeColor(grassColor);
- await w.tp(x, grassY);
- await w.typeChar(grassChar, 1);
- await wait(typingCooldown);
- }
- // Draw dirt
- for (let y = dirtStartY; y <= dirtEndY && y <= endY; y++) {
- if (squareWorldY === y && (x === squareScreenX || x === squareScreenX + 1)) {
- continue;
- }
- await w.changeColor(dirtColor);
- await w.tp(x, y);
- await w.typeChar(dirtChar, 1);
- await wait(typingCooldown);
- }
- // Draw stone
- for (let y = stoneStartY; y <= endY; y++) {
- if (squareWorldY === y && (x === squareScreenX || x === squareScreenX + 1)) {
- continue;
- }
- await w.changeColor(stoneColor);
- await w.tp(x, y);
- await w.typeChar(stoneChar, 1);
- await wait(typingCooldown);
- }
- }
- await w.changeColor(defaultColor);
- }
- // Function to draw all terrain
- async function drawAllTerrain() {
- await drawClouds();
- await drawTerrain();
- }
- // Function to update coordinate display
- async function updateCoordDisplay() {
- let coordText = 'square is at ' + squareWorldX + ', ' + (-squareWorldY);
- if (prevCoordTextLength > coordText.length) {
- await clearTextWithTerrain(coordDisplayX + coordText.length, coordDisplayY, prevCoordTextLength - coordText.length);
- }
- buildCoordCorrectList();
- await drawText(coordDisplayX, coordDisplayY, coordText);
- }
- // Function to draw version text
- async function drawVersionText() {
- buildVersionCorrectList();
- await drawText(coordDisplayX, versionDisplayY, versionText);
- }
- // Function to draw date display
- async function drawDateDisplay() {
- let dateText = getDateString();
- if (prevDateTextLength > dateText.length) {
- await clearTextWithTerrain(dateDisplayX + dateText.length, dateDisplayY, prevDateTextLength - dateText.length);
- }
- buildDateCorrectList();
- await drawText(dateDisplayX, dateDisplayY, dateText);
- }
- // Function to draw time display
- async function drawTimeDisplay() {
- let timeText = getTimeString();
- if (prevTimeTextLength > timeText.length) {
- await clearTextWithTerrain(timeDisplayX + timeText.length, timeDisplayY, prevTimeTextLength - timeText.length);
- }
- buildTimeCorrectList();
- await drawText(timeDisplayX, timeDisplayY, timeText);
- }
- // Function to draw the square
- async function drawSquare() {
- let screenX = getSquareScreenX();
- await w.changeColor(defaultColor);
- await w.tp(screenX, squareWorldY);
- await w.typeChar('[', 1);
- await wait(typingCooldown);
- await w.typeChar(']', 1);
- await wait(typingCooldown);
- }
- // Function to clear the square
- async function clearSquare() {
- let screenX = getSquareScreenX();
- let y = squareWorldY;
- let terrain1 = getTerrainInfo(screenX, y);
- let terrain2 = getTerrainInfo(screenX + 1, y);
- await w.changeColor(terrain1.color);
- await w.tp(screenX, y);
- await w.typeChar(terrain1.char, 1);
- await wait(typingCooldown);
- await w.changeColor(terrain2.color);
- await w.tp(screenX + 1, y);
- await w.typeChar(terrain2.char, 1);
- await wait(typingCooldown);
- await w.changeColor(defaultColor);
- }
- // Function to clear chat text
- async function clearChatText() {
- if (chatText.length > 0) {
- let oldChatText = chatText;
- let oldChatX = chatX;
- let oldChatY = chatY;
- chatText = '';
- for (let i = 0; i < oldChatText.length; i++) {
- let currentX = oldChatX + i;
- let terrain = getTerrainInfo(currentX, oldChatY);
- await w.changeColor(terrain.color);
- await w.tp(currentX, oldChatY);
- await w.typeChar(terrain.char, 1);
- await wait(typingCooldown);
- }
- await w.changeColor(defaultColor);
- }
- }
- // Function to draw chat text
- async function drawChatText() {
- if (chatText.length > 0) {
- let screenX = getSquareScreenX();
- let squareMiddle = screenX + 0.5;
- let textHalfLength = Math.floor(chatText.length / 2);
- chatX = Math.round(squareMiddle - textHalfLength);
- chatY = squareWorldY - 1;
- await w.changeColor(defaultColor);
- for (let i = 0; i < chatText.length; i++) {
- await w.tp(chatX + i, chatY);
- await w.typeChar(chatText[i], 1);
- await wait(typingCooldown);
- }
- }
- }
- // Check if position is part of UI elements
- function isUIPosition(x, y) {
- let screenX = getSquareScreenX();
- if (y === squareWorldY && (x === screenX || x === screenX + 1)) {
- return true;
- }
- if (chatText.length > 0 && y === chatY && x >= chatX && x < chatX + chatText.length) {
- return true;
- }
- if (getCorrectChar(x, y)) {
- return true;
- }
- return false;
- }
- // Check if square brackets are missing or miscolored
- async function checkSquare() {
- if (!isRunning || !safetyEnabled) return;
- try {
- let screenX = getSquareScreenX();
- let leftBracket = await getCharInfoXY(screenX, squareWorldY);
- let rightBracket = await getCharInfoXY(screenX + 1, squareWorldY);
- let needRedraw = false;
- if (!leftBracket || leftBracket.char !== '[' || leftBracket.color !== defaultColor) {
- needRedraw = true;
- }
- if (!rightBracket || rightBracket.char !== ']' || rightBracket.color !== defaultColor) {
- needRedraw = true;
- }
- if (needRedraw) {
- await drawSquare();
- console.log('Square restored at (' + squareWorldX + ', ' + (-squareWorldY) + ')');
- }
- } catch (e) {
- console.log('checkSquare error:', e);
- }
- }
- // Check version, coord, date, time text using correct list
- async function checkCorrectChars() {
- if (!isRunning || !safetyEnabled) return;
- try {
- for (let key in correctChars) {
- if (!isRunning || !safetyEnabled) return;
- let parts = key.split(',');
- let x = parseInt(parts[0]);
- let y = parseInt(parts[1]);
- let expected = correctChars[key];
- let charInfo = await getCharInfoXY(x, y);
- if (!charInfo || charInfo.char !== expected.char || charInfo.color !== expected.color) {
- await w.changeColor(expected.color);
- await w.tp(x, y);
- await w.typeChar(expected.char, 1);
- await wait(typingCooldown);
- }
- }
- } catch (e) {
- console.log('checkCorrectChars error:', e);
- }
- }
- // Check terrain char and color
- async function checkTerrain() {
- if (!isRunning || !safetyEnabled) return;
- try {
- for (let y = startY; y <= endY; y++) {
- for (let x = startX; x <= endX; x++) {
- if (!isRunning || !safetyEnabled) return;
- if (isUIPosition(x, y)) {
- continue;
- }
- let terrain = getTerrainInfo(x, y);
- let charInfo = await getCharInfoXY(x, y);
- if (!charInfo) {
- await w.changeColor(terrain.color);
- await w.tp(x, y);
- await w.typeChar(terrain.char, 1);
- await wait(typingCooldown);
- continue;
- }
- if (charInfo.char !== terrain.char || charInfo.color !== terrain.color) {
- await w.changeColor(terrain.color);
- await w.tp(x, y);
- await w.typeChar(terrain.char, 1);
- await wait(typingCooldown);
- }
- }
- }
- await w.changeColor(defaultColor);
- } catch (e) {
- console.log('checkTerrain error:', e);
- }
- }
- // Check chat text
- async function checkChatText() {
- if (!isRunning || !safetyEnabled) return;
- if (chatText.length === 0) return;
- try {
- for (let i = 0; i < chatText.length; i++) {
- if (!isRunning || !safetyEnabled) return;
- let charInfo = await getCharInfoXY(chatX + i, chatY);
- if (!charInfo || charInfo.char !== chatText[i] || charInfo.color !== defaultColor) {
- await w.changeColor(defaultColor);
- await w.tp(chatX + i, chatY);
- await w.typeChar(chatText[i], 1);
- await wait(typingCooldown);
- }
- }
- } catch (e) {
- console.log('checkChatText error:', e);
- }
- }
- // Animate clouds
- async function animateClouds() {
- if (!isRunning) return;
- currentCloudPattern = (currentCloudPattern + 1) % 1000;
- generateClouds();
- await drawSky();
- // Redraw UI on top
- await drawVersionText();
- await updateCoordDisplay();
- await drawDateDisplay();
- await drawTimeDisplay();
- await drawSquare();
- if (chatText.length > 0) {
- await drawChatText();
- }
- console.log('Clouds animated');
- }
- // Phase transition
- async function transitionPhase() {
- if (!isRunning) return;
- let oldPhase = currentPhase;
- currentPhase = (currentPhase + 1) % 4;
- phaseStartTime = Date.now();
- // Increment date after full cycle (when returning to day from sunrise)
- if (oldPhase === PHASE_SUNRISE && currentPhase === PHASE_DAY) {
- incrementDate();
- }
- // Generate stars when entering night
- if (currentPhase === PHASE_NIGHT) {
- generateStars();
- }
- let phaseName = '';
- switch (currentPhase) {
- case PHASE_DAY: phaseName = 'Day'; break;
- case PHASE_SUNSET: phaseName = 'Sunset'; break;
- case PHASE_NIGHT: phaseName = 'Night'; break;
- case PHASE_SUNRISE: phaseName = 'Sunrise'; break;
- }
- console.log('Phase changed to: ' + phaseName);
- // Redraw sky with new color/char/stars
- await drawSky();
- // Redraw UI on top
- await drawVersionText();
- await updateCoordDisplay();
- await drawDateDisplay();
- await drawTimeDisplay();
- await drawSquare();
- if (chatText.length > 0) {
- await drawChatText();
- }
- }
- // Update time display periodically
- async function updateTime() {
- if (!isRunning) return;
- calculateSimTime();
- buildTimeCorrectList();
- buildDateCorrectList();
- await drawTimeDisplay();
- }
- // Clear entire area with space detection
- async function clearArea() {
- await w.changeColor(defaultColor);
- for (let y = startY; y <= endY; y++) {
- for (let x = startX; x <= endX; x++) {
- try {
- let charInfo = await getCharInfoXY(x, y);
- if (charInfo && charInfo.char === ' ') {
- continue;
- }
- } catch (e) {}
- await w.tp(x, y);
- await w.typeChar(' ', 1);
- await wait(typingCooldown);
- }
- }
- }
- // Redraw entire view
- async function redrawView() {
- await drawAllTerrain();
- await drawVersionText();
- await updateCoordDisplay();
- await drawDateDisplay();
- await drawTimeDisplay();
- await drawSquare();
- if (chatText.length > 0) {
- await drawChatText();
- }
- }
- // Initial area clear
- console.log('Clearing area...');
- await w.changeColor(defaultColor);
- for (let y = startY; y <= endY; y++) {
- for (let x = startX; x <= endX; x++) {
- try {
- let charInfo = await getCharInfoXY(x, y);
- if (charInfo && charInfo.char === ' ') {
- continue;
- }
- } catch (e) {}
- await w.tp(x, y);
- await w.typeChar(' ', 1);
- await wait(typingCooldown);
- }
- }
- console.log('Area cleared!');
- // Generate initial clouds and stars
- generateClouds();
- generateStars();
- // Initialize phase start time
- phaseStartTime = Date.now();
- // Draw all terrain
- console.log('Drawing terrain...');
- await drawAllTerrain();
- console.log('Terrain drawn!');
- // Draw version text
- await drawVersionText();
- console.log('Version text drawn!');
- // Initial coordinate display
- await updateCoordDisplay();
- console.log('Coord display drawn!');
- // Draw date display
- await drawDateDisplay();
- console.log('Date display drawn!');
- // Draw time display
- await drawTimeDisplay();
- console.log('Time display drawn!');
- // Initial draw
- await drawSquare();
- console.log('Square drawn!');
- // Start monitoring loop
- async function monitorLoop() {
- while (isRunning) {
- if (safetyEnabled) {
- await checkSquare();
- await checkCorrectChars();
- await checkChatText();
- await checkTerrain();
- }
- await wait(500);
- }
- }
- // Start monitor in background
- setTimeout(function() {
- monitorLoop();
- }, 100);
- // Start cloud animation (every 25 seconds)
- cloudAnimationInterval = setInterval(function() {
- if (isRunning) {
- animateClouds();
- }
- }, 25000);
- // Start phase transitions
- dayNightInterval = setInterval(function() {
- if (isRunning) {
- transitionPhase();
- }
- }, phaseDuration);
- // Start time updates (every 500ms for smooth time display)
- timeUpdateInterval = setInterval(function() {
- if (isRunning) {
- updateTime();
- }
- }, 500);
- // Move right (d)
- window.moved = async function(amount) {
- if (!isRunning) return;
- let moveAmount = (typeof amount === 'number') ? amount : inputx;
- let oldCameraX = cameraWorldX;
- await clearChatText();
- await clearSquare();
- squareWorldX += moveAmount;
- if (squareWorldX > worldBorderMaxX - 1) {
- squareWorldX = worldBorderMaxX - 1;
- console.log('Hit world border at X = ' + worldBorderMaxX);
- }
- updateCamera();
- buildCoordCorrectList();
- if (cameraWorldX !== oldCameraX) {
- await redrawView();
- } else {
- await drawSquare();
- await drawChatText();
- await updateCoordDisplay();
- }
- console.log('Square position: (' + squareWorldX + ', ' + (-squareWorldY) + ')');
- };
- // Move left (a)
- window.movea = async function(amount) {
- if (!isRunning) return;
- let moveAmount = (typeof amount === 'number') ? amount : inputx;
- let oldCameraX = cameraWorldX;
- await clearChatText();
- await clearSquare();
- squareWorldX -= moveAmount;
- if (squareWorldX < worldBorderMinX) {
- squareWorldX = worldBorderMinX;
- console.log('Hit world border at X = ' + worldBorderMinX);
- }
- updateCamera();
- buildCoordCorrectList();
- if (cameraWorldX !== oldCameraX) {
- await redrawView();
- } else {
- await drawSquare();
- await drawChatText();
- await updateCoordDisplay();
- }
- console.log('Square position: (' + squareWorldX + ', ' + (-squareWorldY) + ')');
- };
- // Move up (w)
- window.movew = async function(amount) {
- if (!isRunning) return;
- let moveAmount = (typeof amount === 'number') ? amount : inputy;
- await clearChatText();
- await clearSquare();
- squareWorldY -= moveAmount;
- if (squareWorldY < startY) squareWorldY = startY;
- buildCoordCorrectList();
- await drawSquare();
- await drawChatText();
- await updateCoordDisplay();
- console.log('Square position: (' + squareWorldX + ', ' + (-squareWorldY) + ')');
- };
- // Move down (s)
- window.moves = async function(amount) {
- if (!isRunning) return;
- let moveAmount = (typeof amount === 'number') ? amount : inputy;
- await clearChatText();
- await clearSquare();
- squareWorldY += moveAmount;
- if (squareWorldY > endY) squareWorldY = endY;
- buildCoordCorrectList();
- await drawSquare();
- await drawChatText();
- await updateCoordDisplay();
- console.log('Square position: (' + squareWorldX + ', ' + (-squareWorldY) + ')');
- };
- // Teleport square to specific world coordinates
- window.squaretake = async function(worldX, worldY) {
- if (!isRunning) return;
- if (typeof worldX !== 'number' || typeof worldY !== 'number') {
- console.log('Usage: squaretake(worldX, worldY)');
- return;
- }
- await clearChatText();
- await clearSquare();
- squareWorldX = worldX;
- squareWorldY = -worldY;
- if (squareWorldX < worldBorderMinX) {
- squareWorldX = worldBorderMinX;
- console.log('Clamped to world border at X = ' + worldBorderMinX);
- }
- if (squareWorldX > worldBorderMaxX - 1) {
- squareWorldX = worldBorderMaxX - 1;
- console.log('Clamped to world border at X = ' + worldBorderMaxX);
- }
- if (squareWorldY < startY) {
- squareWorldY = startY;
- console.log('Clamped to Y boundary');
- }
- if (squareWorldY > endY) {
- squareWorldY = endY;
- console.log('Clamped to Y boundary');
- }
- updateCamera();
- buildCoordCorrectList();
- await redrawView();
- console.log('Square teleported to (' + squareWorldX + ', ' + (-squareWorldY) + ')');
- };
- // Chat function
- window.chat = async function(text) {
- if (!isRunning) return;
- if (chatTimeout) {
- clearTimeout(chatTimeout);
- chatTimeout = null;
- }
- await clearChatText();
- chatText = text || '';
- if (chatText.length > 0) {
- await drawChatText();
- console.log('Chat: "' + chatText + '" at (' + chatX + ', ' + (-chatY) + ')');
- chatTimeout = setTimeout(async function() {
- if (isRunning) {
- await clearChatText();
- console.log('Chat cleared (timeout)');
- }
- }, 6000);
- } else {
- console.log('Chat cleared');
- }
- };
- // Disable safety function
- window.disablesafety = async function() {
- safetyEnabled = false;
- await w.chat.send('Safety DISABLED - char detection OFF');
- console.log('Safety disabled - char detection is now OFF');
- };
- // Enable safety function
- window.enablesafety = async function() {
- safetyEnabled = true;
- await w.chat.send('Safety ENABLED - char detection ON');
- console.log('Safety enabled - char detection is now ON');
- };
- // Quit function
- window.quit = async function() {
- isRunning = false;
- if (chatTimeout) {
- clearTimeout(chatTimeout);
- chatTimeout = null;
- }
- if (cloudAnimationInterval) {
- clearInterval(cloudAnimationInterval);
- cloudAnimationInterval = null;
- }
- if (dayNightInterval) {
- clearInterval(dayNightInterval);
- dayNightInterval = null;
- }
- if (timeUpdateInterval) {
- clearInterval(timeUpdateInterval);
- timeUpdateInterval = null;
- }
- console.log('Clearing area for shutdown...');
- await clearArea();
- let shutdownText = 'SCRIPT SHUTDOWN.';
- let centerX = Math.floor((startX + endX) / 2) - Math.floor(shutdownText.length / 2);
- let centerY = Math.floor((startY + endY) / 2);
- await w.changeColor(defaultColor);
- for (let i = 0; i < shutdownText.length; i++) {
- await w.tp(centerX + i, centerY);
- await w.typeChar(shutdownText[i], 1);
- await wait(typingCooldown);
- }
- await w.chat.send('Script Shutdown');
- console.log('Script Shutdown');
- delete window.movew;
- delete window.movea;
- delete window.moves;
- delete window.moved;
- delete window.chat;
- delete window.quit;
- delete window.disablesafety;
- delete window.enablesafety;
- delete window.squaretake;
- };
- console.log('Controls ready!');
- console.log('Use: movew(n), movea(n), moves(n), moved(n) - n is optional move amount');
- console.log('Use: squaretake(worldX, worldY) - teleport square to world coordinates');
- console.log('Use: chat("your message") - displays text above square for 6 seconds');
- console.log('Use: disablesafety() - disables char detection');
- console.log('Use: enablesafety() - enables char detection');
- console.log('Use: quit() - clears area and stops script');
- console.log('World border: X = ' + worldBorderMinX + ' to ' + worldBorderMaxX);
- console.log('Day/Night cycle: Day -> Sunset -> Night -> Sunrise (37.5s each, 150s full cycle)');
- console.log('Simulation starts at: ' + getDateString() + ' ' + getTimeString());
- console.log('Square starts at (' + squareWorldX + ', ' + (-squareWorldY) + ')');
- })();
Advertisement
Add Comment
Please, Sign In to add comment