9e04048cd0f5d0c66c33d892fd152aa875b5ec40
[oweals/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 = {"v6", "v7", "indev", "singlenode"}
20
21         local current_seed = core.setting_get("fixed_map_seed") or ""
22         local current_mg   = core.setting_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.setting_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         local retval =
48                 "size[12,6,true]" ..
49                 "label[2,0;" .. fgettext("World name") .. "]"..
50                 "field[4.5,0.4;6,0.5;te_world_name;;]" ..
51
52                 "label[2,1;" .. fgettext("Seed") .. "]"..
53                 "field[4.5,1.4;6,0.5;te_seed;;".. current_seed .. "]" ..
54
55                 "label[2,2;" .. fgettext("Mapgen") .. "]"..
56                 "dropdown[4.2,2;6.3;dd_mapgen;" .. mglist .. ";" .. selindex .. "]" ..
57
58                 "label[2,3;" .. fgettext("Game") .. "]"..
59                 "textlist[4.2,3;5.8,2.3;games;" .. gamemgr.gamelist() ..
60                 ";" .. gameidx .. ";true]" ..
61
62                 "button[5,5.5;2.6,0.5;world_create_confirm;" .. fgettext("Create") .. "]" ..
63                 "button[7.5,5.5;2.8,0.5;world_create_cancel;" .. fgettext("Cancel") .. "]"
64
65         return retval
66
67 end
68
69 local function create_world_buttonhandler(this, fields)
70
71         if fields["world_create_confirm"] or
72                 fields["key_enter"] then
73
74                 local worldname = fields["te_world_name"]
75                 local gameindex = core.get_textlist_index("games")
76
77                 if gameindex ~= nil and
78                         worldname ~= "" then
79
80                         local message = nil
81
82                         if not menudata.worldlist:uid_exists_raw(worldname) then
83                                 core.setting_set("mg_name",fields["dd_mapgen"])
84                                 message = core.create_world(worldname,gameindex)
85                         else
86                                 message = fgettext("A world named \"$1\" already exists", worldname)
87                         end
88
89                         core.setting_set("fixed_map_seed", fields["te_seed"])
90
91                         if message ~= nil then
92                                 gamedata.errormessage = message
93                         else
94                                 core.setting_set("menu_last_game",gamemgr.games[gameindex].id)
95                                 if this.data.update_worldlist_filter then
96                                         menudata.worldlist:set_filtercriteria(gamemgr.games[gameindex].id)
97                                         mm_texture.update("singleplayer", gamemgr.games[gameindex].id)
98                                 end
99                                 menudata.worldlist:refresh()
100                                 core.setting_set("mainmenu_last_selected_world",
101                                                                         menudata.worldlist:raw_index_by_uid(worldname))
102                         end
103                 else
104                         gamedata.errormessage =
105                                 fgettext("No worldname given or no game selected")
106                 end
107                 this:delete()
108                 return true
109         end
110
111         if fields["games"] then
112                 return true
113         end
114         
115         if fields["world_create_cancel"] then
116                 this:delete()
117                 return true
118         end
119
120         return false
121 end
122
123
124 function create_create_world_dlg(update_worldlistfilter)
125         local retval = dialog_create("sp_create_world",
126                                         create_world_formspec,
127                                         create_world_buttonhandler,
128                                         nil)
129         retval.update_worldlist_filter = update_worldlistfilter
130         
131         return retval
132 end