Now the generated terrain and dungeons are actually seeded.

This commit is contained in:
Relintai 2021-08-29 19:38:58 +02:00
parent 715e4f209f
commit fcf4e520e9
4 changed files with 33 additions and 17 deletions

View File

@ -61,20 +61,28 @@ func _generate_terra_chunk(chunk, spawn_mobs):
# entrance_position = d.next_level_teleporter_position_data_space
# entrance_position *= voxel_scale
var cx : int = chunk.get_position_x()
var cz : int = chunk.get_position_z()
var chunk_seed : int = 123 + (cx * 231) + (cz * 123)
var rng : RandomNumberGenerator = RandomNumberGenerator.new()
rng.seed = chunk_seed
#terrarin_gen.generate_simple_terrarin(chunk, spawn_mobs)
gen_terra_chunk(chunk)
gen_terra_chunk(chunk, rng)
if chunk.position_x == 0 && chunk.position_z == 0:
#test
spawn_dungeon(chunk)
spawn_dungeon(chunk, chunk_seed)
else:
if randi() % 10 == 0:
spawn_dungeon(chunk)
if rng.randi() % 10 == 0:
spawn_dungeon(chunk, chunk_seed)
for i in range(get_building_count()):
get_building(i).generate_terra_chunk(chunk, spawn_mobs)
if not Engine.editor_hint and spawn_mobs and randi() % 4 == 0:
if not Engine.editor_hint and spawn_mobs and rng.randi() % 4 == 0:
var level : int = 1
if chunk.get_voxel_world().has_method("get_mob_level"):
@ -85,7 +93,7 @@ func _generate_terra_chunk(chunk, spawn_mobs):
100, \
chunk.position_z * chunk.size_z * chunk.voxel_scale + chunk.size_z / 2))
func gen_terra_chunk(chunk: TerraChunk) -> void:
func gen_terra_chunk(chunk: TerraChunk, rng : RandomNumberGenerator) -> void:
chunk.channel_ensure_allocated(TerraChunkDefault.DEFAULT_CHANNEL_TYPE, 1)
chunk.channel_ensure_allocated(TerraChunkDefault.DEFAULT_CHANNEL_ISOLEVEL, 0)
@ -117,13 +125,13 @@ func gen_terra_chunk(chunk: TerraChunk) -> void:
if chunk.position_x == 0 && chunk.position_z == 0:
continue
if randf() > 0.992:
if rng.randf() > 0.992:
var tr : Transform = Transform()
tr = tr.rotated(Vector3(0, 1, 0), randf() * PI)
tr = tr.rotated(Vector3(1, 0, 0), randf() * 0.2 - 0.1)
tr = tr.rotated(Vector3(0, 0, 1), randf() * 0.2 - 0.1)
tr = tr.scaled(Vector3(0.9 + randf() * 0.2, 0.9 + randf() * 0.2, 0.9 + randf() * 0.2))
tr = tr.rotated(Vector3(0, 1, 0), rng.randf() * PI)
tr = tr.rotated(Vector3(1, 0, 0), rng.randf() * 0.2 - 0.1)
tr = tr.rotated(Vector3(0, 0, 1), rng.randf() * 0.2 - 0.1)
tr = tr.scaled(Vector3(0.9 + rng.randf() * 0.2, 0.9 + rng.randf() * 0.2, 0.9 + rng.randf() * 0.2))
tr.origin = Vector3((x + chunk.position_x * chunk.size_x), ((val - 2) / 255.0) * chunk.world_height, (z + chunk.position_z * chunk.size_z))
chunk.voxel_world.prop_add(tr, prop_tree)
@ -135,7 +143,7 @@ func gen_terra_chunk(chunk: TerraChunk) -> void:
# chunk.voxel_world.prop_add(tr, prop_tree2)
func spawn_dungeon(chunk: TerraChunk) -> void:
func spawn_dungeon(chunk: TerraChunk, dungeon_seed : int) -> void:
var x : float = chunk.position_x * chunk.voxel_scale * chunk.size_x
var z : float = chunk.position_z * chunk.voxel_scale * chunk.size_z
@ -153,5 +161,6 @@ func spawn_dungeon(chunk: TerraChunk) -> void:
dt.min_level = level - 1
dt.max_level = level + 1
dt.dungeon_seed = dungeon_seed
dt.transform = Transform(Basis().scaled(Vector3(chunk.voxel_scale, chunk.voxel_scale, chunk.voxel_scale)), Vector3(x, vwh, z))

View File

@ -9,6 +9,7 @@ export(PackedScene) var dungeon_back_teleporter : PackedScene
var min_level : int = 1
var max_level : int = 2
var dungeon_seed : int = 0
var owner_chunk : TerraChunk = null
var _dungeon : Spatial = null
@ -90,6 +91,7 @@ func teleport():
_dungeon.transform = t
_dungeon.min_level = min_level
_dungeon.max_level = max_level
_dungeon.dungeon_seed = dungeon_seed
get_parent().add_child(_dungeon)
teleport_to = t.xform(Vector3())

View File

@ -11,6 +11,7 @@ export(bool) var spawn_mobs : bool = true
export(int) var min_level : int = 1
export(int) var max_level : int = 2
export(int) var dungeon_seed : int = 0
#todo calc aabbs and store in PropData during prop conversion
var room_hulls : Dictionary
@ -21,6 +22,8 @@ var current_aabbs : Array
var debug : bool = true
var _rng : RandomNumberGenerator = RandomNumberGenerator.new()
func _enter_tree() -> void:
if not Engine.editor_hint && generate_on_ready:
call_deferred("generate")
@ -122,6 +125,8 @@ func clear_room_data() -> void:
current_aabbs.clear()
func generate() -> void:
_rng.seed = dungeon_seed
clear()
set_up_room_data()
@ -168,7 +173,7 @@ func spawn_room(room_lworld_transform : Transform, room : PropData, level : int
current_aabbs.push_back(ctfab)
if spawn_mobs && level > 0 && ctfab.size() > 0:
if randi() % 3 == 0:
if _rng.randi() % 3 == 0:
var v2 : Vector2 = ctfab[0]
for i in range(1, ctfab.size()):
@ -178,7 +183,7 @@ func spawn_room(room_lworld_transform : Transform, room : PropData, level : int
var scale : Vector3 = gt.basis.get_scale()
v2 *= Vector2(scale.x, scale.z)
ESS.entity_spawner.spawn_mob(0, min_level + (randi() % (max_level - min_level)), Vector3(v2.x, gt.origin.y, v2.y))
ESS.entity_spawner.spawn_mob(0, min_level + (_rng.randi() % (max_level - min_level)), Vector3(v2.x, gt.origin.y, v2.y))
#if Engine.editor_hint and debug:
# sr.owner = get_tree().edited_scene_root
@ -195,9 +200,7 @@ func spawn_room(room_lworld_transform : Transform, room : PropData, level : int
if d.size() == 0:
continue
randomize()
var new_room_data = d[randi() % d.size()]
var new_room_data = d[_rng.randi() % d.size()]
#[ croom, cportal, cj ]
var new_room : PropData = new_room_data[0]

View File

@ -348,6 +348,8 @@ func load_character(file_name : String) -> void:
mob_level = _player.clevel
set_player(_player.get_body())
#_player.sseed = 2
Server.sset_seed(_player.sseed)
if level_generator != null: