Source: mapgen/asteroids.js

  1. /**
  2. * Simulates an asteroid storm by creating a specified number of asteroid impacts on the world map.
  3. *
  4. * @param {number} num - The number of asteroids to create.
  5. */
  6. function asteroidStorm(num) {
  7. for (let i = 0; i < num; i++) {
  8. const rand = getRandomInt(1, 50);
  9. const randX = getRandomInt(0, world.width);
  10. const randY = getRandomInt(0, world.height);
  11. try {
  12. createAsteroid(rand, randX, randY);
  13. } catch (e) {
  14. console.error('Error creating asteroid:', e);
  15. }
  16. }
  17. }
  18. /**
  19. * Creates an asteroid impact at the specified coordinates, modifying the terrain to reflect the impact.
  20. *
  21. * @param {number} size - The size (diameter) of the asteroid.
  22. * @param {number} x - The x-coordinate of the impact point.
  23. * @param {number} y - The y-coordinate of the impact point.
  24. */
  25. function createAsteroid(size, x, y) {
  26. const impactPoint = xy(x, y);
  27. const diameter = size;
  28. const depth = Math.floor(1.5 * diameter);
  29. const backMod = Math.floor(diameter / 2) * -1;
  30. const forwardMod = diameter / 2;
  31. const asteroidName = "Placeholder";
  32. const asteroid = {
  33. impactPoint: impactPoint,
  34. name: asteroidName,
  35. cells: [impactPoint],
  36. size: size
  37. };
  38. impactPoint.impactPoint = true;
  39. impactPoint.asteroidCrater = true;
  40. impactPoint.asteroidNames = impactPoint.asteroidNames || [];
  41. impactPoint.asteroidNames.push(asteroidName);
  42. console.log(backMod);
  43. console.log(forwardMod);
  44. for (let n = backMod; n < forwardMod; n++) {
  45. for (let j = backMod; j < forwardMod; j++) {
  46. const newX = impactPoint.x + j;
  47. const newY = impactPoint.y + n;
  48. try {
  49. const nextCell = xy(newX, newY);
  50. console.log(nextCell);
  51. const dist = Math.floor(getDistance(nextCell.x, nextCell.y, impactPoint.x, impactPoint.y));
  52. console.log(dist);
  53. if (dist < forwardMod) {
  54. const sub = Math.max(depth - dist, 1);
  55. nextCell.elevation -= sub;
  56. nextCell.asteroidCrater = true;
  57. nextCell.asteroidNames = nextCell.asteroidNames || [];
  58. nextCell.asteroidNames.push(asteroidName);
  59. asteroid.cells.push(nextCell);
  60. }
  61. } catch (e) {
  62. console.error('Error processing cell:', e);
  63. }
  64. }
  65. }
  66. world.asteroids.push(asteroid);
  67. }