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