]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/gamemgr.lua
LuaVoxelManip: Allow liquid updates in non-mapgen VoxelManip objects
[dragonfireclient.git] / builtin / gamemgr.lua
1 --Minetest
2 --Copyright (C) 2013 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 gamemgr = {}
19
20 --------------------------------------------------------------------------------
21 function gamemgr.dialog_new_game()
22         local retval = 
23                 "label[2,2;Game Name]"..
24                 "field[4.5,2.4;6,0.5;te_game_name;;]" ..
25                 "button[5,4.2;2.6,0.5;new_game_confirm;Create]" ..
26                 "button[7.5,4.2;2.8,0.5;new_game_cancel;Cancel]"
27
28         return retval
29 end
30
31 --------------------------------------------------------------------------------
32 function gamemgr.handle_games_buttons(fields)
33         if fields["gamelist"] ~= nil then
34                 local event = explode_textlist_event(fields["gamelist"])
35                 gamemgr.selected_game = event.index
36         end
37         
38         if fields["btn_game_mgr_edit_game"] ~= nil then
39                 return {
40                         is_dialog = true,
41                         show_buttons = false,
42                         current_tab = "dialog_edit_game"
43                 }
44         end
45         
46         if fields["btn_game_mgr_new_game"] ~= nil then
47                 return {
48                         is_dialog = true,
49                         show_buttons = false,
50                         current_tab = "dialog_new_game"
51                 }
52         end
53         
54         return nil
55 end
56
57 --------------------------------------------------------------------------------
58 function gamemgr.handle_new_game_buttons(fields)
59
60         if fields["new_game_confirm"] and
61                 fields["te_game_name"] ~= nil and
62                 fields["te_game_name"] ~= "" then
63                 local gamepath = engine.get_gamepath()
64                 
65                 if gamepath ~= nil and
66                         gamepath ~= "" then
67                         local gamefolder = cleanup_path(fields["te_game_name"])
68                         
69                         --TODO check for already existing first
70                         engine.create_dir(gamepath .. DIR_DELIM .. gamefolder)
71                         engine.create_dir(gamepath .. DIR_DELIM .. gamefolder .. DIR_DELIM .. "mods")
72                         engine.create_dir(gamepath .. DIR_DELIM .. gamefolder .. DIR_DELIM .. "menu")
73                         
74                         local gameconf = 
75                                 io.open(gamepath .. DIR_DELIM .. gamefolder .. DIR_DELIM .. "game.conf","w")
76                         
77                         if gameconf then
78                                 gameconf:write("name = " .. fields["te_game_name"])
79                                 gameconf:close()
80                         end
81                 end
82         end
83
84         return {
85                 is_dialog = false,
86                 show_buttons = true,
87                 current_tab = engine.setting_get("main_menu_tab")
88                 }
89 end
90
91 --------------------------------------------------------------------------------
92 function gamemgr.handle_edit_game_buttons(fields)
93         local current_game = gamemgr.get_game(gamemgr.selected_game)
94         
95         if fields["btn_close_edit_game"] ~= nil or
96                 current_game == nil then
97                 return {
98                         is_dialog = false,
99                         show_buttons = true,
100                         current_tab = engine.setting_get("main_menu_tab")
101                         }
102         end
103
104         if fields["btn_remove_mod_from_game"] ~= nil then
105                 gamemgr.delete_mod(current_game,engine.get_textlist_index("mods_current"))
106         end
107         
108         if fields["btn_add_mod_to_game"] ~= nil then
109                 local modindex = engine.get_textlist_index("mods_available")
110                 
111                 if modindex > 0 and
112                         modindex <= #modmgr.global_mods then
113                         
114                         local sourcepath = 
115                                 engine.get_modpath() .. DIR_DELIM .. modmgr.global_mods[modindex]
116                         
117                         gamemgr.add_mod(current_game,sourcepath)
118                 end
119         end
120         
121         return nil
122 end
123
124 --------------------------------------------------------------------------------
125 function gamemgr.add_mod(gamespec,sourcepath)
126         if gamespec.gamemods_path ~= nil and
127                 gamespec.gamemods_path ~= "" then
128                 
129                 local modname = get_last_folder(sourcepath)
130                 
131                 engine.copy_dir(sourcepath,gamespec.gamemods_path .. DIR_DELIM .. modname);
132         end
133 end
134
135 --------------------------------------------------------------------------------
136 function gamemgr.delete_mod(gamespec,modindex)
137         if gamespec.gamemods_path ~= nil and
138                 gamespec.gamemods_path ~= "" then
139                 local game_mods = {}
140                 get_mods(gamespec.gamemods_path,game_mods)
141                 
142                 if modindex > 0 and
143                         #game_mods >= modindex then
144                         
145                         local modname = game_mods[modindex]
146         
147                         if modname:find("<MODPACK>") ~= nil then
148                                 modname = modname:sub(0,modname:find("<") -2)
149                         end
150
151                         local modpath = gamespec.gamemods_path .. DIR_DELIM .. modname
152                         if modpath:sub(0,gamespec.gamemods_path:len()) == gamespec.gamemods_path then
153                                 engine.delete_dir(modpath)
154                         end
155                 end
156         end
157 end
158
159 --------------------------------------------------------------------------------
160 function gamemgr.get_game_mods(gamespec)
161
162         local retval = ""
163         
164         if gamespec.gamemods_path ~= nil and
165                 gamespec.gamemods_path ~= "" then
166                 local game_mods = {}
167                 get_mods(gamespec.gamemods_path,game_mods)
168                 
169                 for i=1,#game_mods,1 do
170                         if retval ~= "" then
171                                 retval = retval..","
172                         end
173                         retval = retval .. game_mods[i]
174                 end 
175         end
176         return retval
177 end
178
179 --------------------------------------------------------------------------------
180 function gamemgr.gettab(name)
181         local retval = ""
182         
183         if name == "dialog_edit_game" then
184                 retval = retval .. gamemgr.dialog_edit_game()
185         end
186         
187         if name == "dialog_new_game" then
188                 retval = retval .. gamemgr.dialog_new_game()
189         end
190         
191         if name == "game_mgr" then
192                 retval = retval .. gamemgr.tab()
193         end
194         
195         return retval
196 end
197
198 --------------------------------------------------------------------------------
199 function gamemgr.tab()
200         if gamemgr.selected_game == nil then
201                 gamemgr.selected_game = 1
202         end
203         
204         local retval = 
205                 "vertlabel[0,-0.25;GAMES]" ..
206                 "label[1,-0.25;Games:]" ..
207                 "textlist[1,0.25;4.5,4.4;gamelist;" ..
208                 gamemgr.gamelist() ..
209                 ";" .. gamemgr.selected_game .. "]"
210         
211         local current_game = gamemgr.get_game(gamemgr.selected_game)
212         
213         if current_game ~= nil then
214                 if current_game.menuicon_path ~= nil and
215                         current_game.menuicon_path ~= "" then
216                         retval = retval .. 
217                                 "image[5.8,-0.25;2,2;" .. current_game.menuicon_path .. "]"
218                 end
219                 
220                 retval = retval ..
221                         "field[8,-0.25;6,2;;" .. current_game.name .. ";]"..
222                         "label[6,1.4;Mods:]" ..
223                         "button[9.7,1.5;2,0.2;btn_game_mgr_edit_game;edit game]" ..
224                         "textlist[6,2;5.5,3.3;game_mgr_modlist;"
225                         .. gamemgr.get_game_mods(current_game) ..";0]" ..
226                         "button[1,4.75;3.2,0.5;btn_game_mgr_new_game;new game]"
227         end
228         return retval
229 end
230
231 --------------------------------------------------------------------------------
232 function gamemgr.dialog_edit_game()
233         local current_game = gamemgr.get_game(gamemgr.selected_game)
234         if current_game ~= nil then
235                 local retval = 
236                         "vertlabel[0,-0.25;EDIT GAME]" ..
237                         "label[0,-0.25;" .. current_game.name .. "]" ..
238                         "button[11.55,-0.2;0.75,0.5;btn_close_edit_game;x]"
239                 
240                 if current_game.menuicon_path ~= nil and
241                         current_game.menuicon_path ~= "" then
242                         retval = retval .. 
243                                 "image[5.25,0;2,2;" .. current_game.menuicon_path .. "]"                        
244                 end
245                 
246                 retval = retval .. 
247                         "textlist[0.5,0.5;4.5,4.3;mods_current;"
248                         .. gamemgr.get_game_mods(current_game) ..";0]"
249                         
250                         
251                 retval = retval .. 
252                         "textlist[7,0.5;4.5,4.3;mods_available;"
253                         .. modmgr.get_mods_list() .. ";0]"
254
255                 retval = retval ..
256                         "button[0.55,4.95;4.7,0.5;btn_remove_mod_from_game;Remove selected mod]"
257                         
258                 retval = retval ..
259                         "button[7.05,4.95;4.7,0.5;btn_add_mod_to_game;<<-- Add mod]"
260                 
261                 return retval
262         end
263 end
264
265 --------------------------------------------------------------------------------
266 function gamemgr.handle_buttons(tab,fields)
267         local retval = nil
268         
269         if tab == "dialog_edit_game" then
270                 retval = gamemgr.handle_edit_game_buttons(fields)
271         end
272         
273         if tab == "dialog_new_game" then
274                 retval = gamemgr.handle_new_game_buttons(fields)
275         end
276         
277         if tab == "game_mgr" then
278                 retval = gamemgr.handle_games_buttons(fields)
279         end
280         
281         return retval
282 end
283
284 --------------------------------------------------------------------------------
285 function gamemgr.get_game(index) 
286         if index > 0 and index <= #gamemgr.games then
287                 return gamemgr.games[index]
288         end
289         
290         return nil
291 end
292
293 --------------------------------------------------------------------------------
294 function gamemgr.update_gamelist()
295         gamemgr.games = engine.get_games()
296 end
297
298 --------------------------------------------------------------------------------
299 function gamemgr.gamelist()
300         local retval = ""
301         if #gamemgr.games > 0 then
302                 retval = retval .. gamemgr.games[1].id
303                 
304                 for i=2,#gamemgr.games,1 do
305                         retval = retval .. "," .. gamemgr.games[i].name
306                 end
307         end
308         return retval
309 end