feat: map.view and barbarian villages seeded per world
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
34aa8acead
commit
e8c4382d78
|
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
namespace App\Actions;
|
||||
|
||||
use App\Engine\VillageFactory;
|
||||
use App\Models\World;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* Scatters barbarian villages around the map centre, once per world. The RNG is
|
||||
* seeded from the world's slug, so a world always gets the same layout.
|
||||
*/
|
||||
final class SeedBarbarians
|
||||
{
|
||||
private const BUILDINGS = ['headquarters', 'storage', 'farm', 'timber_camp', 'clay_pit', 'iron_mine'];
|
||||
|
||||
public function __construct(private readonly VillageFactory $villages) {}
|
||||
|
||||
/** @return int villages created */
|
||||
public function __invoke(World $world): int
|
||||
{
|
||||
if ($world->villages()->whereNull('player_id')->exists()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$config = $world->gameConfig()->get('world.map.barbarians');
|
||||
$centre = intdiv($world->gameConfig()->get('world.map.size', 1000), 2);
|
||||
mt_srand(crc32($world->slug));
|
||||
|
||||
return DB::transaction(function () use ($world, $config, $centre) {
|
||||
$taken = VillageFactory::takenSpots($world);
|
||||
$created = 0;
|
||||
for ($tries = 0; $created < $config['count'] && $tries < $config['count'] * 20; $tries++) {
|
||||
$x = $centre + mt_rand(-$config['radius'], $config['radius']);
|
||||
$y = $centre + mt_rand(-$config['radius'], $config['radius']);
|
||||
// Leave the centre ring free for player spawns (even coordinates near the middle).
|
||||
if (isset($taken["$x|$y"]) || (abs($x - $centre) < 6 && abs($y - $centre) < 6)) {
|
||||
continue;
|
||||
}
|
||||
$taken["$x|$y"] = true;
|
||||
$levels = [];
|
||||
foreach (self::BUILDINGS as $type) {
|
||||
$levels[$type] = mt_rand(0, $config['max_level']);
|
||||
}
|
||||
$this->villages->create($world, null, 'Barbarian village', $x, $y, $levels);
|
||||
$created++;
|
||||
}
|
||||
|
||||
return $created;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
|
||||
namespace App\Actions;
|
||||
|
||||
use App\Game\GameRuleViolation;
|
||||
use App\Game\Map\Continent;
|
||||
use App\Models\Player;
|
||||
use App\Models\User;
|
||||
use App\Models\Village;
|
||||
use App\Models\World;
|
||||
|
||||
/**
|
||||
* The villages in a w × h window of a world's map, centred on (x, y) the way the
|
||||
* Die Stämme map is. Without a centre it opens on the player's first village there.
|
||||
*/
|
||||
final class ShowMap
|
||||
{
|
||||
public function __invoke(User $user, int $worldId, ?int $x, ?int $y, int $w, int $h): array
|
||||
{
|
||||
$world = World::find($worldId) ?? throw new GameRuleViolation('not_found', 'World not found.');
|
||||
$config = $world->gameConfig();
|
||||
$size = $config->get('world.map.size', 1000);
|
||||
$max = $config->get('world.map.max_view', 41);
|
||||
$w = max(1, min($max, $w));
|
||||
$h = max(1, min($max, $h));
|
||||
|
||||
if ($x === null || $y === null) {
|
||||
$home = Village::whereIn('player_id', Player::where('user_id', $user->id)->where('world_id', $world->id)->select('id'))
|
||||
->orderBy('id')->first();
|
||||
$x = $home->x ?? intdiv($size, 2);
|
||||
$y = $home->y ?? intdiv($size, 2);
|
||||
}
|
||||
$x = max(0, min($size - 1, $x));
|
||||
$y = max(0, min($size - 1, $y));
|
||||
$left = max(0, min($size - $w, $x - intdiv($w, 2)));
|
||||
$top = max(0, min($size - $h, $y - intdiv($h, 2)));
|
||||
|
||||
$villages = Village::where('world_id', $world->id)
|
||||
->whereBetween('x', [$left, $left + $w - 1])
|
||||
->whereBetween('y', [$top, $top + $h - 1])
|
||||
->with('player:id,user_id,name')
|
||||
->get(['id', 'player_id', 'name', 'x', 'y', 'points'])
|
||||
->map(fn (Village $v) => [
|
||||
'id' => $v->id,
|
||||
'x' => $v->x,
|
||||
'y' => $v->y,
|
||||
'name' => $v->name,
|
||||
'points' => $v->points,
|
||||
'continent' => Continent::of($v->x, $v->y),
|
||||
'player' => $v->player ? ['id' => $v->player->id, 'name' => $v->player->name] : null,
|
||||
'own' => $v->player?->user_id === $user->id,
|
||||
])->values()->all();
|
||||
|
||||
return [
|
||||
'world_id' => $world->id,
|
||||
'size' => $size,
|
||||
'center' => ['x' => $x, 'y' => $y],
|
||||
'x' => $left,
|
||||
'y' => $top,
|
||||
'w' => $w,
|
||||
'h' => $h,
|
||||
'villages' => $villages,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
namespace App\Websocket\Controllers;
|
||||
|
||||
use App\Actions\ShowMap;
|
||||
use App\Websocket\GameController;
|
||||
|
||||
/** map.view {world_id, x?, y?, w, h}: villages around (x, y), or around the player's village. */
|
||||
class MapController extends GameController
|
||||
{
|
||||
public function view()
|
||||
{
|
||||
$x = request('x');
|
||||
$y = request('y');
|
||||
|
||||
return $this->play(fn () => app(ShowMap::class)(
|
||||
auth()->user(),
|
||||
(int) request('world_id'),
|
||||
is_numeric($x) ? (int) $x : null,
|
||||
is_numeric($y) ? (int) $y : null,
|
||||
(int) (request('w') ?: 15),
|
||||
(int) (request('h') ?: 15),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
@ -2,19 +2,25 @@
|
|||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Actions\SeedBarbarians;
|
||||
use App\Models\World;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
/** The default world and a fast dev world. Safe to run on every deploy. */
|
||||
/** The default world and a fast dev world, with their barbarians. Safe to run on every deploy. */
|
||||
class WorldSeeder extends Seeder
|
||||
{
|
||||
public function run(): void
|
||||
{
|
||||
World::firstOrCreate(['slug' => 'w1'], ['name' => 'World 1', 'started_at' => now()]);
|
||||
$worlds = [
|
||||
World::firstOrCreate(['slug' => 'w1'], ['name' => 'World 1', 'started_at' => now()]),
|
||||
World::firstOrCreate(['slug' => 'dev'], [
|
||||
'name' => 'Dev world',
|
||||
'config' => ['world' => ['speed' => 100]],
|
||||
'started_at' => now(),
|
||||
]);
|
||||
]),
|
||||
];
|
||||
foreach ($worlds as $world) {
|
||||
app(SeedBarbarians::class)($world);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
use App\Actions\SeedBarbarians;
|
||||
use App\Actions\ShowMap;
|
||||
use App\Models\User;
|
||||
use App\Models\Village;
|
||||
use App\Models\World;
|
||||
|
||||
it('opens on the player village and marks it as their own', function () {
|
||||
$village = startVillage();
|
||||
$other = startVillage(User::factory()->create(['name' => 'Bea']));
|
||||
|
||||
$map = app(ShowMap::class)(owner($village), $village->world_id, null, null, 15, 15);
|
||||
|
||||
expect($map)->toMatchArray(['center' => ['x' => 500, 'y' => 500], 'x' => 493, 'y' => 493, 'w' => 15, 'h' => 15, 'size' => 1000])
|
||||
->and(collect($map['villages'])->keyBy('id')->sortKeys()->map(fn ($v) => [$v['x'], $v['y'], $v['own'], $v['player']['name'] ?? null, $v['continent']])->all())
|
||||
->toBe([
|
||||
$village->id => [500, 500, true, owner($village)->name, 'K55'],
|
||||
$other->id => [498, 498, false, 'Bea', 'K44'],
|
||||
]);
|
||||
});
|
||||
|
||||
it('shows only the window around the given centre, kept inside the map', function () {
|
||||
$village = startVillage();
|
||||
|
||||
$far = app(ShowMap::class)(owner($village), $village->world_id, 100, 100, 15, 15);
|
||||
$edge = app(ShowMap::class)(owner($village), $village->world_id, 2, 998, 15, 999);
|
||||
|
||||
expect($far['villages'])->toBe([])
|
||||
->and($edge)->toMatchArray(['x' => 0, 'y' => 959, 'w' => 15, 'h' => 41]);
|
||||
});
|
||||
|
||||
it('seeds barbarian villages once per world, away from the spawn centre', function () {
|
||||
config(['game.world.map.barbarians' => ['count' => 20, 'radius' => 20, 'max_level' => 3]]);
|
||||
$village = startVillage();
|
||||
$world = World::find($village->world_id);
|
||||
|
||||
$barbarians = Village::where('world_id', $world->id)->whereNull('player_id')->get();
|
||||
expect($barbarians)->toHaveCount(20)
|
||||
->and($barbarians->every(fn ($v) => abs($v->x - 500) <= 20 && abs($v->y - 500) <= 20))->toBeTrue()
|
||||
->and($barbarians->every(fn ($v) => abs($v->x - 500) >= 6 || abs($v->y - 500) >= 6))->toBeTrue()
|
||||
->and($barbarians->every(fn ($v) => $v->points > 0 && $v->buildings()->count() === 6))->toBeTrue()
|
||||
->and(app(SeedBarbarians::class)($world))->toBe(0);
|
||||
|
||||
$map = app(ShowMap::class)(owner($village), $world->id, 500, 500, 41, 41);
|
||||
expect(collect($map['villages'])->whereNull('player'))->toHaveCount(20);
|
||||
});
|
||||
Loading…
Reference in New Issue