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