dlg_create_world.lua 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 worldname = ""
  18. local function create_world_formspec(dialogdata)
  19. local mapgens = core.get_mapgen_names()
  20. local current_seed = core.settings:get("fixed_map_seed") or ""
  21. local current_mg = core.settings:get("mg_name")
  22. local gameid = core.settings:get("menu_last_game")
  23. local gameidx = 0
  24. if gameid ~= nil then
  25. _, gameidx = pkgmgr.find_by_gameid(gameid)
  26. if gameidx == nil then
  27. gameidx = 0
  28. end
  29. end
  30. local game_by_gameidx = core.get_game(gameidx)
  31. if game_by_gameidx ~= nil then
  32. local gamepath = game_by_gameidx.path
  33. local gameconfig = Settings(gamepath.."/game.conf")
  34. local disallowed_mapgens = (gameconfig:get("disallowed_mapgens") or ""):split()
  35. for key, value in pairs(disallowed_mapgens) do
  36. disallowed_mapgens[key] = value:trim()
  37. end
  38. if disallowed_mapgens then
  39. for i = #mapgens, 1, -1 do
  40. if table.indexof(disallowed_mapgens, mapgens[i]) > 0 then
  41. table.remove(mapgens, i)
  42. end
  43. end
  44. end
  45. end
  46. local mglist = ""
  47. local selindex = 1
  48. local i = 1
  49. for k,v in pairs(mapgens) do
  50. if current_mg == v then
  51. selindex = i
  52. end
  53. i = i + 1
  54. mglist = mglist .. v .. ","
  55. end
  56. mglist = mglist:sub(1, -2)
  57. current_seed = core.formspec_escape(current_seed)
  58. local retval =
  59. "size[11.5,6.5,true]" ..
  60. "label[2,0;" .. fgettext("World name") .. "]"..
  61. "field[4.5,0.4;6,0.5;te_world_name;;" .. minetest.formspec_escape(worldname) .. "]" ..
  62. "label[2,1;" .. fgettext("Seed") .. "]"..
  63. "field[4.5,1.4;6,0.5;te_seed;;".. current_seed .. "]" ..
  64. "label[2,2;" .. fgettext("Mapgen") .. "]"..
  65. "dropdown[4.2,2;6.3;dd_mapgen;" .. mglist .. ";" .. selindex .. "]" ..
  66. "label[2,3;" .. fgettext("Game") .. "]"..
  67. "textlist[4.2,3;5.8,2.3;games;" .. pkgmgr.gamelist() ..
  68. ";" .. gameidx .. ";true]" ..
  69. "button[3.25,6;2.5,0.5;world_create_confirm;" .. fgettext("Create") .. "]" ..
  70. "button[5.75,6;2.5,0.5;world_create_cancel;" .. fgettext("Cancel") .. "]"
  71. if #pkgmgr.games == 0 then
  72. retval = retval .. "box[2,4;8,1;#ff8800]label[2.25,4;" ..
  73. fgettext("You have no games installed.") .. "]label[2.25,4.4;" ..
  74. fgettext("Download one from minetest.net") .. "]"
  75. elseif #pkgmgr.games == 1 and pkgmgr.games[1].id == "minimal" then
  76. retval = retval .. "box[1.75,4;8.7,1;#ff8800]label[2,4;" ..
  77. fgettext("Warning: The minimal development test is meant for developers.") .. "]label[2,4.4;" ..
  78. fgettext("Download a game, such as Minetest Game, from minetest.net") .. "]"
  79. end
  80. return retval
  81. end
  82. local function create_world_buttonhandler(this, fields)
  83. if fields["world_create_confirm"] or
  84. fields["key_enter"] then
  85. local worldname = fields["te_world_name"]
  86. local gameindex = core.get_textlist_index("games")
  87. if gameindex ~= nil then
  88. if worldname == "" then
  89. local random_number = math.random(10000, 99999)
  90. local random_world_name = "Unnamed" .. random_number
  91. worldname = random_world_name
  92. end
  93. core.settings:set("fixed_map_seed", fields["te_seed"])
  94. local message
  95. if not menudata.worldlist:uid_exists_raw(worldname) then
  96. core.settings:set("mg_name",fields["dd_mapgen"])
  97. message = core.create_world(worldname,gameindex)
  98. else
  99. message = fgettext("A world named \"$1\" already exists", worldname)
  100. end
  101. if message ~= nil then
  102. gamedata.errormessage = message
  103. else
  104. core.settings:set("menu_last_game",pkgmgr.games[gameindex].id)
  105. if this.data.update_worldlist_filter then
  106. menudata.worldlist:set_filtercriteria(pkgmgr.games[gameindex].id)
  107. mm_texture.update("singleplayer", pkgmgr.games[gameindex].id)
  108. end
  109. menudata.worldlist:refresh()
  110. core.settings:set("mainmenu_last_selected_world",
  111. menudata.worldlist:raw_index_by_uid(worldname))
  112. end
  113. else
  114. gamedata.errormessage = fgettext("No game selected")
  115. end
  116. this:delete()
  117. return true
  118. end
  119. worldname = fields.te_world_name
  120. if fields["games"] then
  121. local gameindex = core.get_textlist_index("games")
  122. core.settings:set("menu_last_game", pkgmgr.games[gameindex].id)
  123. return true
  124. end
  125. if fields["world_create_cancel"] then
  126. this:delete()
  127. return true
  128. end
  129. return false
  130. end
  131. function create_create_world_dlg(update_worldlistfilter)
  132. worldname = ""
  133. local retval = dialog_create("sp_create_world",
  134. create_world_formspec,
  135. create_world_buttonhandler,
  136. nil)
  137. retval.update_worldlist_filter = update_worldlistfilter
  138. return retval
  139. end