| 338 | } |
| 339 | |
| 340 | void SystemWorldServer::spawnObjects() { |
| 341 | double diff = min(systemConfig().objectSpawnCycle, time() - m_lastSpawn); |
| 342 | m_lastSpawn = time() - diff; |
| 343 | while (diff > m_objectSpawnTime) { |
| 344 | m_lastSpawn += m_objectSpawnTime; |
| 345 | m_objectSpawnTime = Random::randf(systemConfig().objectSpawnInterval[0], systemConfig().objectSpawnInterval[1]); |
| 346 | diff = time() - m_lastSpawn; |
| 347 | |
| 348 | WeightedPool<String> spawnPool = jsonToWeightedPool<String>(Root::singleton().assets()->json("/systemworld.config:objectSpawnPool").toArray()); |
| 349 | String name = spawnPool.select(); |
| 350 | Uuid uuid = Uuid(); |
| 351 | auto objectConfig = systemObjectConfig(name, uuid); |
| 352 | |
| 353 | SystemObjectPtr object; |
| 354 | RandomSource rand = RandomSource(Random::randu64()); |
| 355 | Vec2F position = randomObjectSpawnPosition(rand); |
| 356 | if (time() > m_lastSpawn + m_objectSpawnTime && objectConfig.moving) { |
| 357 | // if this is not the last object we're spawning, and it's moving, immediately put it in orbit around a planet |
| 358 | auto targets = planets().filtered([this](CelestialCoordinate const& p) { |
| 359 | auto objectsAtPlanet = objects().filtered([p](SystemObjectPtr const& o) { return o->orbitTarget() == p; }); |
| 360 | return objectsAtPlanet.size() == 0; |
| 361 | }); |
| 362 | if (targets.size() > 0) { |
| 363 | auto target = Random::randFrom(targets); |
| 364 | |
| 365 | Vec2F targetPosition = planetPosition(target); |
| 366 | Vec2F relativeOrbit = (position - targetPosition).normalized() * (clusterSize(target) / 2.0 + objectConfig.orbitDistance); |
| 367 | object = make_shared<SystemObject>(objectConfig, uuid, targetPosition + relativeOrbit, m_lastSpawn); |
| 368 | |
| 369 | object->enterOrbit(target, planetPosition(target), m_lastSpawn); |
| 370 | } else { |
| 371 | object = make_shared<SystemObject>(objectConfig, uuid, position, m_lastSpawn); |
| 372 | } |
| 373 | } else { |
| 374 | object = make_shared<SystemObject>(objectConfig, uuid, position, m_lastSpawn); |
| 375 | } |
| 376 | addObject(object); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | Vec2F SystemWorldServer::randomObjectSpawnPosition(RandomSource& rand) const { |
| 381 | List<Vec2F> spawnRanges; |
nothing calls this directly
no test coverage detected