gameConfig()))->interval(); $waiting = GameEvent::where('type', GameEvent::BARBARIAN_GROWS)->whereNull('processed_at')->select('village_id'); $villages = $world->villages()->whereNull('player_id')->whereNotIn('id', $waiting)->pluck('id'); $now = now()->toImmutable(); foreach ($villages as $id) { GameEvent::create([ 'world_id' => $world->id, 'village_id' => $id, 'type' => GameEvent::BARBARIAN_GROWS, 'fire_at' => $now->addSeconds((int) round($interval * (0.5 + mt_rand() / (mt_getrandmax() + 1)))), ]); } return $villages->count(); } /** @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(1, $config['max_level']); } $this->villages->create($world, null, 'Barbarian village', $x, $y, $levels); $created++; } return $created; }); } }