$taken updated with the new villages * @return list */ public function around(World $world, Village $village, array &$taken): array { $config = $world->gameConfig(); $frontier = new Frontier($config); $maxLevel = $config->get('world.map.barbarians.max_level', 3); $created = []; for ($n = $frontier->barbariansPerJoin(VillageFactory::random(...)); $n > 0; $n--) { $spot = $frontier->near($village->x, $village->y, $taken, VillageFactory::random(...)); if (! $spot) { break; } $taken["{$spot[0]}|{$spot[1]}"] = false; $levels = []; foreach (self::BUILDINGS as $type) { $levels[$type] = mt_rand(1, $maxLevel); } $barbarian = $this->villages->create($world, null, 'Barbarian village', $spot[0], $spot[1], $levels); Barbarians::of($barbarian)->fill(); $this->scheduleFirstGrowth($world, $barbarian); $created[] = $barbarian; } return $created; } /** * Give every barbarian village without one a pending growth event, spread over one * interval so they don't all grow at once. Safe to run on every deploy. * * @return int events scheduled */ public function scheduleGrowth(World $world): int { $waiting = GameEvent::where('type', GameEvent::BARBARIAN_GROWS)->whereNull('processed_at')->select('village_id'); $villages = $world->villages()->whereNull('player_id')->whereNotIn('id', $waiting)->get(); foreach ($villages as $village) { $this->scheduleFirstGrowth($world, $village); } return $villages->count(); } /** * Give every barbarian village its garrison: full strength at once for one that never * had any (from before garrisons), a regroup step for one that is short without one * waiting. Safe to run on every deploy. * * @return int villages that got troops or a regroup step */ public function scheduleGarrisons(World $world): int { $now = now()->toImmutable(); $changed = 0; foreach ($world->villages()->whereNull('player_id')->pluck('id') as $id) { $changed += DB::transaction(function () use ($id, $now) { $village = Village::whereKey($id)->lockForUpdate()->first(); if (! $village || $village->player_id !== null) { return 0; } $barbarians = Barbarians::of($village); $waiting = GameEvent::where('type', GameEvent::BARBARIAN_REGROUPS)->where('village_id', $id)->whereNull('processed_at')->exists(); if ($barbarians->current() === [] && ! $waiting) { $barbarians->fill(); return $barbarians->current() === [] ? 0 : 1; } if (! $waiting && ! $barbarians->rules()->isFull($barbarians->current(), $barbarians->full())) { $barbarians->scheduleRegroup($now); return 1; } return 0; }); } return $changed; } /** * Plan a raid for every player village without one, from now or the end of its * owner's beginner protection. Safe to run on every deploy. */ public function scheduleRaids(World $world): void { $now = now()->toImmutable(); foreach ($world->villages()->whereNotNull('player_id')->pluck('id') as $id) { DB::transaction(function () use ($id, $now) { $village = Village::whereKey($id)->lockForUpdate()->first(); if ($village?->player_id !== null) { Raiders::of($village)->ensurePlanned($now); } }); } } /** The first growth step lands 0.5–1.5 intervals out, so villages don't grow in step. */ private function scheduleFirstGrowth(World $world, Village $village): void { $interval = (new BarbarianGrowth($world->gameConfig()))->interval(); GameEvent::create([ 'world_id' => $world->id, 'village_id' => $village->id, 'type' => GameEvent::BARBARIAN_GROWS, 'fire_at' => now()->toImmutable()->addSeconds((int) round($interval * (0.5 + VillageFactory::random()))), ]); } }