
// schemes <- {}
// scheme_weights <- {}

foreach (name in split(@"
  utils
  universe
  save
  topology
  geometry
  listeners
  events
  worldElements
  generator
  test
  ", " \t\r\n")) include("gen/"+name+".nut")


in_main_menu <- false
cur_world <- null
universe <- null


function generate_world(reason)
{
  in_main_menu=(reason=="main_menu")

  reset_listeners()

  cur_world=null
  if (universe && universe.enteredWorld>=0
    && universe.enteredWorld<universe.worlds.len())
      cur_world=universe.worlds[universe.enteredWorld]

  if (!cur_world)
  {
    cur_world=make_world(0, 0, tocolor(hsv2rgba(frnd()*360, rndf(0.5, 1), rndf(0.4, 0.7), 1)))
    cur_world.id <- -1
    if (in_main_menu) cur_world.scheme="main_menu"
    // cur_world.seed=1470298524 //==
  }

  cur_world.visited=true

  print(format("world seed: %d\n", cur_world.seed))
  set_rnd_seed(cur_world.seed)
  reseed_noise()

  if (reason!="test_pal")
    cur_world_pal=get_world_pal(format("pal%02d", irnd()%30+1)) //== use pal from uni data

  if (reason!="test_pal")
  {
    local ha=frnd()*PI*2, va=rndf(-5, 30)*PI/180
    set_sun_dir(make4f(cos(ha)*cos(va), sin(va), sin(ha)*cos(va), 0))
  }

  local gen=_WELEM("!WorldGen", cur_world.scheme)()
  gen.worldTags.append(cur_world.scheme)
  gen.generate()

  wgen=null
  update_check_delay=0.0
}
