dlg_create_world.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.setting_get("fixed_map_seed") or ""
  20. local current_mg = core.setting_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.setting_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[12,6,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[5,5.5;2.6,0.5;world_create_confirm;" .. fgettext("Create") .. "]" ..
  53. "button[7.5,5.5;2.8,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 and
  71. worldname ~= "" then
  72. local message = nil
  73. core.setting_set("fixed_map_seed", fields["te_seed"])
  74. if not menudata.worldlist:uid_exists_raw(worldname) then
  75. core.setting_set("mg_name",fields["dd_mapgen"])
  76. message = core.create_world(worldname,gameindex)
  77. else
  78. message = fgettext("A world named \"$1\" already exists", worldname)
  79. end
  80. if message ~= nil then
  81. gamedata.errormessage = message
  82. else
  83. core.setting_set("menu_last_game",gamemgr.games[gameindex].id)
  84. if this.data.update_worldlist_filter then
  85. menudata.worldlist:set_filtercriteria(gamemgr.games[gameindex].id)
  86. mm_texture.update("singleplayer", gamemgr.games[gameindex].id)
  87. end
  88. menudata.worldlist:refresh()
  89. core.setting_set("mainmenu_last_selected_world",
  90. menudata.worldlist:raw_index_by_uid(worldname))
  91. end
  92. else
  93. gamedata.errormessage =
  94. fgettext("No worldname given or no game selected")
  95. end
  96. this:delete()
  97. return true
  98. end
  99. if fields["games"] then
  100. return true
  101. end
  102. if fields["world_create_cancel"] then
  103. this:delete()
  104. return true
  105. end
  106. return false
  107. end
  108. function create_create_world_dlg(update_worldlistfilter)
  109. local retval = dialog_create("sp_create_world",
  110. create_world_formspec,
  111. create_world_buttonhandler,
  112. nil)
  113. retval.update_worldlist_filter = update_worldlistfilter
  114. return retval
  115. end