Dragon-Knight/src/actions/explore.php

57 lines
1.5 KiB
PHP
Raw Normal View History

<?php
// explore.php :: Handles all map exploring, chances to fight, etc.
2024-12-17 22:10:49 -06:00
function move() {
global $controlrow;
// Early exit if fighting
if (user()->currentaction == 'Fighting') redirect('/fight');
// Validate direction
$form = validate($_POST, ['direction' => ['in:north,west,east,south']]);
if (!$form['valid']) return display(ul_from_validate_errors($form['errors']), 'Move Error');
2024-12-17 22:10:49 -06:00
// Current game state
$game_size = $controlrow['gamesize'];
$latitude = user()->latitude;
$longitude = user()->longitude;
2024-12-17 22:10:49 -06:00
$direction = $form['data']['direction'];
// Calculate new coordinates with boundary checks
switch ($direction) {
case 'north':
$latitude = min($latitude + 1, $game_size);
break;
case 'south':
$latitude = max($latitude - 1, -$game_size);
break;
case 'east':
$longitude = min($longitude + 1, $game_size);
break;
case 'west':
$longitude = max($longitude - 1, -$game_size);
break;
}
2024-12-17 22:10:49 -06:00
// Check for town
$town = get_town_by_xy($longitude, $latitude);
2024-12-12 10:20:49 -06:00
if ($town !== false) {
return Towns\travelto($town['id'], false);
}
2024-12-17 22:10:49 -06:00
// Determine action (1 in 5 chance of fighting)
if (rand(1, 5) === 1) {
user()->currentaction = 'Fighting';
user()->currentfight = 1;
} else {
user()->currentaction = 'Exploring';
}
user()->latitude = $latitude;
user()->longitude = $longitude;
user()->save();
redirect('/');
}