diff options
author | elioat <elioat@tilde.institute> | 2024-12-04 19:41:35 -0500 |
---|---|---|
committer | elioat <elioat@tilde.institute> | 2024-12-04 19:41:35 -0500 |
commit | 977bb2ea686f36a7be47553a86f326e9a6f7aa21 (patch) | |
tree | 8392c7b9bc41e6dc0b74d70f2f9ad4670439e921 /html/broughlike | |
parent | c35d5ead81dc92621ff1d7d5104fda8346c74d73 (diff) | |
download | tour-977bb2ea686f36a7be47553a86f326e9a6f7aa21.tar.gz |
*'
Diffstat (limited to 'html/broughlike')
-rw-r--r-- | html/broughlike/broughlike.js | 204 |
1 files changed, 80 insertions, 124 deletions
diff --git a/html/broughlike/broughlike.js b/html/broughlike/broughlike.js index 5e82ebb..7efa67d 100644 --- a/html/broughlike/broughlike.js +++ b/html/broughlike/broughlike.js @@ -671,8 +671,6 @@ function resetGame() { generateEnemies(); generateItems(); render(); - if (AUTO_PLAY) - autoPlay(); } function checkPlayerAtExit() { @@ -695,8 +693,6 @@ function checkPlayerAtExit() { generateEnemies(); generateItems(); render(); - if (AUTO_PLAY) - autoPlay(); } } @@ -792,14 +788,14 @@ render(); - +let AUTO_PLAY = false; +let autoPlayInterval = null; function autoPlay() { let lastPosition = { x: player.x, y: player.y }; let stuckCounter = 0; const playerAtExit = () => player.x === exit.x && player.y === exit.y; - const playerCanMove = (dx, dy) => isValidMove(player.x + dx, player.y + dy); const checkIfStuck = () => { if (lastPosition.x === player.x && lastPosition.y === player.y) { @@ -808,160 +804,120 @@ function autoPlay() { stuckCounter = 0; lastPosition = { x: player.x, y: player.y }; } - return stuckCounter > 3; // Consider yourself stuck after 3 turns in the same position + return stuckCounter > 3; }; - const desperateEscape = () => { - const allDirections = [ - {dx: 1, dy: 0}, {dx: -1, dy: 0}, - {dx: 0, dy: 1}, {dx: 0, dy: -1}, - {dx: 1, dy: 1}, {dx: -1, dy: 1}, - {dx: 1, dy: -1}, {dx: -1, dy: -1} - ]; - - allDirections.sort(() => Math.random() - 0.5); - - for (const {dx, dy} of allDirections) { - if (playerCanMove(dx, dy)) { - movePlayer(dx, dy); - return true; + const findSafestPath = (target) => { + const path = findPath(player, target); + if (path.length <= 1) { + // If you can't find a path, find the nearest enemy + const nearestEnemy = enemies.reduce((closest, enemy) => { + const distToCurrent = Math.abs(enemy.x - player.x) + Math.abs(enemy.y - player.y); + const distToClosest = closest ? Math.abs(closest.x - player.x) + Math.abs(closest.y - player.y) : Infinity; + return distToCurrent < distToClosest ? enemy : closest; + }, null); + + if (nearestEnemy) { + return [{x: player.x, y: player.y}, {x: nearestEnemy.x, y: nearestEnemy.y}]; } + return null; } - return false; - }; - const hasAdjacentEnemy = (x, y) => { - return enemies.some(enemy => - Math.abs(enemy.x - x) + Math.abs(enemy.y - y) === 1 + const nextStep = path[1]; + const adjacentEnemies = enemies.filter(enemy => + Math.abs(enemy.x - nextStep.x) + Math.abs(enemy.y - nextStep.y) <= 1 ); - }; - - const findNearestItem = () => { - if (items.length === 0) return null; - return items.reduce((nearest, item) => { - const distToCurrent = Math.abs(player.x - item.x) + Math.abs(player.y - item.y); - const distToNearest = nearest ? Math.abs(player.x - nearest.x) + Math.abs(player.y - nearest.y) : Infinity; - return distToCurrent < distToNearest ? item : nearest; - }, null); - }; - const decideNextTarget = () => { - - const healingItem = items.find(item => - item.type === 'pentagon' && - player.health < CONFIG.PLAYER_HEALTH * 0.5 - ); - if (healingItem) return healingItem; + if (adjacentEnemies.length > 0 && player.health < 3) { + const alternativePaths = [ + {dx: 1, dy: 0}, {dx: -1, dy: 0}, + {dx: 0, dy: 1}, {dx: 0, dy: -1} + ].filter(({dx, dy}) => { + const newX = player.x + dx; + const newY = player.y + dy; + return isValidMove(newX, newY) && + !enemies.some(e => Math.abs(e.x - newX) + Math.abs(e.y - newY) <= 1); + }); - const nearestItem = findNearestItem(); - if (nearestItem && Math.abs(player.x - nearestItem.x) + Math.abs(player.y - nearestItem.y) < 5) { - return nearestItem; + if (alternativePaths.length > 0) { + const randomPath = alternativePaths[Math.floor(Math.random() * alternativePaths.length)]; + return [{x: player.x, y: player.y}, {x: player.x + randomPath.dx, y: player.y + randomPath.dy}]; + } } - return exit; + return path; }; const moveTowardsTarget = (target) => { - const path = findPath(player, target); - if (path.length > 1) { + const path = findSafestPath(target); + if (path && path.length > 1) { const nextStep = path[1]; - - if (Math.random() < 0.2) { - const alternateDirections = [ - {dx: 1, dy: 0}, {dx: -1, dy: 0}, - {dx: 0, dy: 1}, {dx: 0, dy: -1} - ].filter(({dx, dy}) => - playerCanMove(dx, dy) && - !hasAdjacentEnemy(player.x + dx, player.y + dy) - ); - - if (alternateDirections.length > 0) { - const randomDir = alternateDirections[Math.floor(Math.random() * alternateDirections.length)]; - movePlayer(randomDir.dx, randomDir.dy); - return true; - } - } - - if (!hasAdjacentEnemy(nextStep.x, nextStep.y)) { - const dx = nextStep.x - player.x; - const dy = nextStep.y - player.y; - if (playerCanMove(dx, dy)) { - movePlayer(dx, dy); - return true; - } - } + const dx = nextStep.x - player.x; + const dy = nextStep.y - player.y; + movePlayer(dx, dy); + return true; } return false; }; - const handleCombat = () => { - const adjacentEnemy = enemies.find(enemy => - Math.abs(enemy.x - player.x) + Math.abs(enemy.y - player.y) === 1 - ); - - if (adjacentEnemy) { - // Increase the chance of retreating when stuck - const retreatChance = checkIfStuck() ? 0.8 : 0.3; - - if (Math.random() < retreatChance) { - const retreatDirections = [ - {dx: 1, dy: 0}, {dx: -1, dy: 0}, - {dx: 0, dy: 1}, {dx: 0, dy: -1} - ]; - - retreatDirections.sort(() => Math.random() - 0.5); - - for (const {dx, dy} of retreatDirections) { - if (playerCanMove(dx, dy) && !hasAdjacentEnemy(player.x + dx, player.y + dy)) { - movePlayer(dx, dy); - return true; - } - } - } - - // If stuck, try desperate escape - if (checkIfStuck()) { - return desperateEscape(); + const findBestTarget = () => { + // If health is low, prioritize healing items + if (player.health < 3) { + const healingItem = items.find(item => item.type === 'pentagon'); + if (healingItem && findPath(player, healingItem).length > 0) { + return healingItem; } - - // Attack if can't retreat - const dx = adjacentEnemy.x - player.x; - const dy = adjacentEnemy.y - player.y; - movePlayer(dx, dy); - return true; } - return false; + + // If there's a nearby damage boost and we're healthy, grab it + const damageItem = items.find(item => + item.type === 'diamond' && + Math.abs(item.x - player.x) + Math.abs(item.y - player.y) < 5 + ); + if (damageItem && player.health > 2) { + return damageItem; + } + + // Default to exit + return exit; }; const play = () => { - if (playerAtExit()) { + + if (!AUTO_PLAY) { + clearTimeout(autoPlayInterval); + autoPlayInterval = null; return; } - // If stuck, try desperate escape - if (checkIfStuck() && desperateEscape()) { - // Successfully escaped - } else if (!handleCombat()) { - // If no combat, move towards the next target - const target = decideNextTarget(); + if (playerAtExit()) return; + + if (checkIfStuck()) { + const directions = [{dx: 1, dy: 0}, {dx: -1, dy: 0}, {dx: 0, dy: 1}, {dx: 0, dy: -1}]; + const validDirections = directions.filter(({dx, dy}) => isValidMove(player.x + dx, player.y + dy)); + if (validDirections.length > 0) { + const {dx, dy} = validDirections[Math.floor(Math.random() * validDirections.length)]; + movePlayer(dx, dy); + } + } else { + const target = findBestTarget(); moveTowardsTarget(target); } - setTimeout(play, 400); // 400ms is about 1.5 moves per second because 1000ms was terribly slow. + autoPlayInterval = setTimeout(play, 400); }; play(); } -let AUTO_PLAY = false; - document.addEventListener('keydown', (e) => { if (e.key === 'v') { - // alert to confirm that the user wants to toggle auto play - if (confirm("Are you sure you want to toggle auto play? Once auto play is turned on, the only way to turn it off is to reload the page.")) { - AUTO_PLAY = !AUTO_PLAY; - if (AUTO_PLAY) - autoPlay(); + AUTO_PLAY = !AUTO_PLAY; + if (AUTO_PLAY) { + console.log("Auto-play on"); + autoPlay(); + } else { + console.log("Auto-play off"); } } }); \ No newline at end of file |