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