按程序生成一个巨大的宇宙
我正在创建一个基于文本的游戏,并尝试实现程序世界生成。
我最初的计划有点随意:每个宇宙都有一个 3D 星系阵列、太阳系阵列,然后依次用随机天体传播。我计划单独生成实际的局部区域,但我不确定目前是否可以完成我所拥有的任务。
Each universe is a Galaxy[10][10][10] (arbitrary number at the moment),
each galaxy is a randomly sized SolarSystem[50-150][50-150][50-150],
each SolarSystem is a randomly sized CelestialBody[5-20][5-20][5-20].
所有这些都将被写入数据文件以供稍后读取。
现在来看,如果我没记错的话,这将需要 (((ClassSize^3)^3)^3) 个字节,即使 ClassSize 只有 4 个字节也是不可能存储的。
我最初使用数组数组的目的是能够有效地将集群分组在一起,并更好地帮助识别玩家在宇宙中的位置。
我的问题是:如何更有效地生成如此规模的世界?
I'm creating a text-based game, and trying to implement procedural world generation.
My initial plan was a bit haphazard: each universe has a 3D array of galaxies, of solar systems, which then in turn would be propagated with randomized celestial bodies. I was planning on generating the actual local area separately, but I'm not sure it's currently possible to complete the task as I have it.
Each universe is a Galaxy[10][10][10] (arbitrary number at the moment),
each galaxy is a randomly sized SolarSystem[50-150][50-150][50-150],
each SolarSystem is a randomly sized CelestialBody[5-20][5-20][5-20].
All of which would then be written out to a data file to be read later.
Looking at it now, if I'm not mistaken, this would require (((ClassSize^3)^3)^3) bytes, which is impossible to store even if the ClassSize were only 4 bytes.
My intent with arrays of arrays initially was to be able to efficiently group clusters together and better help identify where the player was in the universe.
My question is: how can I generate a world of such scale more efficiently?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无需尝试存储生成的宇宙,只需为每个玩家创建一个独特的随机种子值,并在玩家玩游戏时使用它来动态生成世界。
当您每次使用相同的值为生成器播种时,每次的随机数都会相同。因此,如果我唯一的随机玩家 ID 是
654156475
,那么当我加载游戏时,将该 ID 放入宇宙生成器中,生成器每次都会生成相同的宇宙。不同的玩家会得到不同的宇宙,因为他们的种子与我的不同。请参阅本文的“视频游戏”部分,简要概述此技术的原理用于游戏中。
或者,不要将其写为宇宙生成器,而将其写为太阳系生成器(或玩家将占据的最小空间单位)。然后,为每个太阳系存储一个随机种子,并使用这些种子(这将是相对少量的数据)来生成(并稍后重新生成相同的东西)游戏场随着玩家的游戏而飞行。
这种方法的主要优点是您只需将种子值存储在磁盘上,这是非常少量的数据,并且根本不必存储宇宙数据。不仅如此,动态地重新生成宇宙的一小部分通常比从磁盘加载要快得多。
Rather than trying to store the resulting universe, simply create a unique, random seed value for each player, and use that to procedurally generate the world on the fly, as the player plays.
When you seed your generator with the same value each time, the random numbers will be the same every time. So, if my unique, random player ID is
654156475
, then throw that ID into the universe generator when I load up the game, and the generator will produce the same universe every time. A different player will get a different universe, because their seed is different than mine.See the "video games" section of this article for a brief overview of how this technique is used in games.
Or, instead of writing it as a universe generator, write it as a solar system generator (or whatever is the smallest unit of space that the player will occupy). Then, store a random seed for each solar system, and use those seeds (which will be a relatively small amount of data) to generate (and re-generate the same stuff later) the play field on the fly as the player plays.
The major advantage to this approach is that you only have to store the seed values on disk, which is a very small amount of data, and you don't have to store the universe data at all. Not only that, regenerating just small parts of the universe on the fly can often be much faster than loading it from disk anyway.
在很多很多年前做过像你所描述的事情(当时 PC 有两个 5.25 英寸软盘驱动器),我不会在内存中预先分配整个游戏。你应该将其分解,以便游戏加载玩家的宇宙部分位于,例如 10x10x10 的 3D 块中。当游戏跨越加载空间的边界时,将该空间写入磁盘并读入它们已移入的空间。
Having done something like you describe many, many years ago, (back when PCs had two 5.25" floppy drives) I wouldn't preallocate the entire game in memory. You should break it up so the game loads the portion of the universe the player is in, say in 3D blocks of 10x10x10. When the play wanders moves across the boundary of the loaded space, write that space out to disk and read in the space they've moved into.