dlg_create_world.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. --Minetest
  2. --Copyright (C) 2014 sapier
  3. --
  4. --This program is free software; you can redistribute it and/or modify
  5. --it under the terms of the GNU Lesser General Public License as published by
  6. --the Free Software Foundation; either version 2.1 of the License, or
  7. --(at your option) any later version.
  8. --
  9. --This program is distributed in the hope that it will be useful,
  10. --but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. --GNU Lesser General Public License for more details.
  13. --
  14. --You should have received a copy of the GNU Lesser General Public License along
  15. --with this program; if not, write to the Free Software Foundation, Inc.,
  16. --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. local function create_world_formspec(dialogdata)
  18. local mapgens = core.get_mapgen_names()
  19. local current_seed = core.settings:get("fixed_map_seed") or ""
  20. local current_mg = core.settings:get("mg_name")
  21. local mglist = ""
  22. local selindex = 1
  23. local i = 1
  24. for k,v in pairs(mapgens) do
  25. if current_mg == v then
  26. selindex = i
  27. end
  28. i = i + 1
  29. mglist = mglist .. v .. ","
  30. end
  31. mglist = mglist:sub(1, -2)
  32. local gameid = core.settings:get("menu_last_game")
  33. local game, gameidx = nil , 0
  34. if gameid ~= nil then
  35. game, gameidx = gamemgr.find_by_gameid(gameid)
  36. if gameidx == nil then
  37. gameidx = 0
  38. end
  39. end
  40. current_seed = core.formspec_escape(current_seed)
  41. local retval =
  42. "size[11.5,6.5,true]" ..
  43. "label[2,0;" .. fgettext("World name") .. "]"..
  44. "field[4.5,0.4;6,0.5;te_world_name;;]" ..
  45. "label[2,1;" .. fgettext("Seed") .. "]"..
  46. "field[4.5,1.4;6,0.5;te_seed;;".. current_seed .. "]" ..
  47. "label[2,2;" .. fgettext("Mapgen") .. "]"..
  48. "dropdown[4.2,2;6.3;dd_mapgen;" .. mglist .. ";" .. selindex .. "]" ..
  49. "label[2,3;" .. fgettext("Game") .. "]"..
  50. "textlist[4.2,3;5.8,2.3;games;" .. gamemgr.gamelist() ..
  51. ";" .. gameidx .. ";true]" ..
  52. "button[3.25,6;2.5,0.5;world_create_confirm;" .. fgettext("Create") .. "]" ..
  53. "button[5.75,6;2.5,0.5;world_create_cancel;" .. fgettext("Cancel") .. "]"
  54. if #gamemgr.games == 0 then
  55. retval = retval .. "box[2,4;8,1;#ff8800]label[2.25,4;" ..
  56. fgettext("You have no subgames installed.") .. "]label[2.25,4.4;" ..
  57. fgettext("Download one from minetest.net") .. "]"
  58. elseif #gamemgr.games == 1 and gamemgr.games[1].id == "minimal" then
  59. retval = retval .. "box[1.75,4;8.7,1;#ff8800]label[2,4;" ..
  60. fgettext("Warning: The minimal development test is meant for developers.") .. "]label[2,4.4;" ..
  61. fgettext("Download a subgame, such as minetest_game, from minetest.net") .. "]"
  62. end
  63. return retval
  64. end
  65. local function create_world_buttonhandler(this, fields)
  66. if fields["world_create_confirm"] or
  67. fields["key_enter"] then
  68. local worldname = fields["te_world_name"]
  69. local gameindex = core.get_textlist_index("games")
  70. if gameindex ~= nil then
  71. if worldname == "" then
  72. local random_number = math.random(10000, 99999)
  73. local random_world_name = "Unnamed" .. random_number
  74. worldname = random_world_name
  75. end
  76. local message = nil
  77. core.settings:set("fixed_map_seed", fields["te_seed"])
  78. if not menudata.worldlist:uid_exists_raw(worldname) then
  79. core.settings:set("mg_name",fields["dd_mapgen"])
  80. message = core.create_world(worldname,gameindex)
  81. else
  82. message = fgettext("A world named \"$1\" already exists", worldname)
  83. end
  84. if message ~= nil then
  85. gamedata.errormessage = message
  86. else
  87. core.settings:set("menu_last_game",gamemgr.games[gameindex].id)
  88. if this.data.update_worldlist_filter then
  89. menudata.worldlist:set_filtercriteria(gamemgr.games[gameindex].id)
  90. mm_texture.update("singleplayer", gamemgr.games[gameindex].id)
  91. end
  92. menudata.worldlist:refresh()
  93. core.settings:set("mainmenu_last_selected_world",
  94. menudata.worldlist:raw_index_by_uid(worldname))
  95. end
  96. else
  97. gamedata.errormessage = fgettext("No game selected")
  98. end
  99. this:delete()
  100. return true
  101. end
  102. if fields["games"] then
  103. return true
  104. end
  105. if fields["world_create_cancel"] then
  106. this:delete()
  107. return true
  108. end
  109. return false
  110. end
  111. function create_create_world_dlg(update_worldlistfilter)
  112. local retval = dialog_create("sp_create_world",
  113. create_world_formspec,
  114. create_world_buttonhandler,
  115. nil)
  116. retval.update_worldlist_filter = update_worldlistfilter
  117. return retval
  118. end