summaryrefslogtreecommitdiff
path: root/builtin/mainmenu/init.lua
blob: d008ec8b0815b7f3c84be5aeac6d475c6d0bdd62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
--Minetest
--Copyright (C) 2014 sapier
--
--This program is free software; you can redistribute it and/or modify
--it under the terms of the GNU Lesser General Public License as published by
--the Free Software Foundation; either version 2.1 of the License, or
--(at your option) any later version.
--
--This program is distributed in the hope that it will be useful,
--but WITHOUT ANY WARRANTY; without even the implied warranty of
--MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--GNU Lesser General Public License for more details.
--
--You should have received a copy of the GNU Lesser General Public License along
--with this program; if not, write to the Free Software Foundation, Inc.,
--51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

mt_color_grey  = "#AAAAAA"
mt_color_blue  = "#0000DD"
mt_color_green = "#00DD00"
mt_color_dark_green = "#003300"

--for all other colors ask sfan5 to complete his work!

local menupath = core.get_mainmenu_path()
local basepath = core.get_builtin_path()
defaulttexturedir = core.get_texturepath_share() .. DIR_DELIM .. "base" ..
					DIR_DELIM .. "pack" .. DIR_DELIM

dofile(basepath .. DIR_DELIM .. "common" .. DIR_DELIM .. "async_event.lua")
dofile(basepath .. DIR_DELIM .. "common" .. DIR_DELIM .. "filterlist.lua")
dofile(basepath .. DIR_DELIM .. "fstk" .. DIR_DELIM .. "buttonbar.lua")
dofile(basepath .. DIR_DELIM .. "fstk" .. DIR_DELIM .. "dialog.lua")
dofile(basepath .. DIR_DELIM .. "fstk" .. DIR_DELIM .. "tabview.lua")
dofile(basepath .. DIR_DELIM .. "fstk" .. DIR_DELIM .. "ui.lua")
dofile(menupath .. DIR_DELIM .. "common.lua")
dofile(menupath .. DIR_DELIM .. "gamemgr.lua")
dofile(menupath .. DIR_DELIM .. "modmgr.lua")
dofile(menupath .. DIR_DELIM .. "store.lua")
dofile(menupath .. DIR_DELIM .. "dlg_config_world.lua")
dofile(menupath .. DIR_DELIM .. "tab_credits.lua")
dofile(menupath .. DIR_DELIM .. "tab_mods.lua")
dofile(menupath .. DIR_DELIM .. "tab_settings.lua")
if PLATFORM ~= "Android" then
	dofile(menupath .. DIR_DELIM .. "dlg_create_world.lua")
	dofile(menupath .. DIR_DELIM .. "dlg_delete_mod.lua")
	dofile(menupath .. DIR_DELIM .. "dlg_delete_world.lua")
	dofile(menupath .. DIR_DELIM .. "dlg_rename_modpack.lua")
	dofile(menupath .. DIR_DELIM .. "tab_multiplayer.lua")
	dofile(menupath .. DIR_DELIM .. "tab_server.lua")
	dofile(menupath .. DIR_DELIM .. "tab_singleplayer.lua")
	dofile(menupath .. DIR_DELIM .. "tab_texturepacks.lua")
	dofile(menupath .. DIR_DELIM .. "textures.lua")
else
	dofile(menupath .. DIR_DELIM .. "tab_simple_main.lua")
end

--------------------------------------------------------------------------------
local function main_event_handler(tabview, event)
	if event == "MenuQuit" then
		core.close()
	end
	return true
end

--------------------------------------------------------------------------------
local function init_globals()
	-- Init gamedata
	gamedata.worldindex = 0


	if PLATFORM ~= "Android" then
		menudata.worldlist = filterlist.create(
			core.get_worlds,
			compare_worlds,
			-- Unique id comparison function
			function(element, uid)
				return element.name == uid
			end,
			-- Filter function
			function(element, gameid)
				return element.gameid == gameid
			end
		)

		menudata.worldlist:add_sort_mechanism("alphabetic", sort_worlds_alphabetic)
		menudata.worldlist:set_sortmode("alphabetic")

		if not core.setting_get("menu_last_game") then
			local default_game = core.setting_get("default_game") or "minetest"
			core.setting_set("menu_last_game", default_game )
		end

		mm_texture.init()
	else
		local world_list = core.get_worlds()

		local found_singleplayerworld = false

		for i,world in pairs(world_list) do
			if world.name == "singleplayerworld" then
				found_singleplayerworld = true
				gamedata.worldindex = i
				break
			end
		end

		if not found_singleplayerworld then
			core.create_world("singleplayerworld", 1)

			local world_list = core.get_worlds()

			for i,world in pairs(world_list) do
				if world.name == "singleplayerworld" then
					gamedata.worldindex = i
					break
				end
			end
		end
	end

	-- Create main tabview
	local tv_main = tabview_create("maintab",{x=12,y=5.2},{x=0,y=0})
	if PLATFORM ~= "Android" then
		tv_main:set_autosave_tab(true)
	end
	if PLATFORM ~= "Android" then
		tv_main:add(tab_singleplayer)
		tv_main:add(tab_multiplayer)
		tv_main:add(tab_server)
	else
		tv_main:add(tab_simple_main)
	end
	tv_main:add(tab_settings)
	if PLATFORM ~= "Android" then
		tv_main:add(tab_texturepacks)
	end
	tv_main:add(tab_mods)
	tv_main:add(tab_credits)

	tv_main:set_global_event_handler(main_event_handler)
	if PLATFORM ~= "Android" then
		tv_main:set_fixed_size(true)
	else
		tv_main:set_fixed_size(false)
	end

	if not (PLATFORM == "Android") then
		tv_main:set_tab(core.setting_get("maintab_LAST"))
	end
	ui.set_default("maintab")
	tv_main:show()

	-- Create modstore ui
	if PLATFORM == "Android" then
		modstore.init({x=12, y=6}, 3, 2)
	else
		modstore.init({x=12, y=8}, 4, 3)
	end

	ui.update()

	core.sound_play("main_menu", true)
end

init_globals()

1 rail around -> choose and orient straight 3. there's no rail around -> set straight ]] local function istrackandbc(pos_p, conn) local tpos = pos_p local cnode=minetest.get_node(advtrains.dirCoordSet(tpos, conn.c)) if advtrains.is_track_and_drives_on(cnode.name, advtrains.all_tracktypes) then local cconns=advtrains.get_track_connections(cnode.name, cnode.param2) return advtrains.conn_matches_to(conn, cconns) end --try the same 1 node below tpos = {x=tpos.x, y=tpos.y-1, z=tpos.z} cnode=minetest.get_node(advtrains.dirCoordSet(tpos, conn.c)) if advtrains.is_track_and_drives_on(cnode.name, advtrains.all_tracktypes) then local cconns=advtrains.get_track_connections(cnode.name, cnode.param2) return advtrains.conn_matches_to(conn, cconns) end return false end function tp.find_already_connected(pos) local dnode=minetest.get_node(pos) local dconns=advtrains.get_track_connections(dnode.name, dnode.param2) local found_conn for connid, conn in ipairs(dconns) do if istrackandbc(pos, conn) then if found_conn then --we found one in previous iteration return true, true --signal that it's connected else found_conn = conn.c end end end return found_conn end function tp.rail_and_can_be_bent(originpos, conn) local pos=advtrains.dirCoordSet(originpos, conn) local newdir=(conn+8)%16 local node=minetest.get_node(pos) if not advtrains.is_track_and_drives_on(node.name, advtrains.all_tracktypes) then return false end local ndef=minetest.registered_nodes[node.name] local nnpref = ndef and ndef.at_nnpref if not nnpref then return false end local tr=tp.tracks[nnpref] if not tr then return false end if not tr.modify[node.name] then --we actually can use this rail, but only if it already points to the desired direction. if advtrains.is_track_and_drives_on(node.name, advtrains.all_tracktypes) then local cconns=advtrains.get_track_connections(node.name, node.param2) return advtrains.conn_matches_to(conn, cconns) end end -- If the rail is not allowed to be modified, also only use if already in desired direction if not advtrains.can_dig_or_modify_track(pos) then local cconns=advtrains.get_track_connections(node.name, node.param2) return advtrains.conn_matches_to(conn, cconns) end --rail at other end? local adj1, adj2=tp.find_already_connected(pos) if adj1 and adj2 then return false--dont destroy existing track elseif adj1 and not adj2 then if tr.double_conn[adj1.."_"..newdir] then return true--if exists, connect new rail and old end end return false else if tr.single_conn[newdir] then--just rotate old rail to right orientation return true end return false end end function tp.bend_rail(originpos, conn) local pos=advtrains.dirCoordSet(originpos, conn) local newdir=advtrains.oppd(conn) local node=minetest.get_node(pos) local ndef=minetest.registered_nodes[node.name] local nnpref = ndef and ndef.at_nnpref if not nnpref then return false end local tr=tp.tracks[nnpref] if not tr then return false end --is rail already connected? no need to bend. local conns=advtrains.get_track_connections(node.name, node.param2) if advtrains.conn_matches_to(conn, conns) then return end --rail at other end? local adj1, adj2=tp.find_already_connected(pos) if adj1 and adj2 then return false--dont destroy existing track elseif adj1 and not adj2 then if tr.double_conn[adj1.."_"..newdir] then advtrains.ndb.swap_node(pos, tr.double_conn[adj1.."_"..newdir]) return true--if exists, connect new rail and old end end return false else if tr.single_conn[newdir] then--just rotate old rail to right orientation advtrains.ndb.swap_node(pos, tr.single_conn[newdir]) return true end return false end end function tp.placetrack(pos, nnpref, placer, itemstack, pointed_thing, yaw) --1. find all rails that are likely to be connected local tr=tp.tracks[nnpref] local p_rails={} local p_railpos={} for i=0,15 do if tp.rail_and_can_be_bent(pos, i, nnpref) then p_rails[#p_rails+1]=i p_railpos[i] = pos else local upos = {x=pos.x, y=pos.y-1, z=pos.z} if tp.rail_and_can_be_bent(upos, i, nnpref) then p_rails[#p_rails+1]=i p_railpos[i] = upos end end end -- try double_conn if #p_rails > 1 then --iterate subsets for k1, conn1 in ipairs(p_rails) do for k2, conn2 in ipairs(p_rails) do if k1~=k2 then local dconn1 = tr.double_conn_1 local dconn2 = tr.double_conn_2 if not (advtrains.yawToDirection(yaw, conn1, conn2) == conn1) then dconn1 = tr.double_conn_2 dconn2 = tr.double_conn_1 end -- Checks are made this way round so that dconn1 has priority (this will make arrows of atc rails -- point in the right direction) local using if (dconn2[conn1.."_"..conn2]) then using = dconn2[conn1.."_"..conn2] end if (dconn1[conn1.."_"..conn2]) then using = dconn1[conn1.."_"..conn2] end if using then -- has found a fitting rail in either direction -- if not, continue loop tp.bend_rail(p_railpos[conn1], conn1, nnpref) tp.bend_rail(p_railpos[conn2], conn2, nnpref) advtrains.ndb.swap_node(pos, using) local nname=using.name if minetest.registered_nodes[nname] and minetest.registered_nodes[nname].after_place_node then minetest.registered_nodes[nname].after_place_node(pos, placer, itemstack, pointed_thing) end return end end end end end -- try single_conn if #p_rails > 0 then for ix, p_rail in ipairs(p_rails) do local sconn1 = tr.single_conn_1 local sconn2 = tr.single_conn_2 if not (advtrains.yawToDirection(yaw, p_rail, (p_rail+8)%16) == p_rail) then sconn1 = tr.single_conn_2 sconn2 = tr.single_conn_1 end if sconn1[p_rail] then local using = sconn1[p_rail] tp.bend_rail(p_railpos[p_rail], p_rail, nnpref) advtrains.ndb.swap_node(pos, using) local nname=using.name if minetest.registered_nodes[nname] and minetest.registered_nodes[nname].after_place_node then minetest.registered_nodes[nname].after_place_node(pos, placer, itemstack, pointed_thing) end return end if sconn2[p_rail] then local using = sconn2[p_rail] tp.bend_rail(p_railpos[p_rail], p_rail, nnpref) advtrains.ndb.swap_node(pos, using) local nname=using.name if minetest.registered_nodes[nname] and minetest.registered_nodes[nname].after_place_node then minetest.registered_nodes[nname].after_place_node(pos, placer, itemstack, pointed_thing) end return end end end --use default minetest.set_node(pos, {name=nnpref.."_"..tr.default}) if minetest.registered_nodes[nnpref.."_"..tr.default] and minetest.registered_nodes[nnpref.."_"..tr.default].after_place_node then minetest.registered_nodes[nnpref.."_"..tr.default].after_place_node(pos, placer, itemstack, pointed_thing) end end function tp.register_track_placer(nnprefix, imgprefix, dispname, def) minetest.register_craftitem(":"..nnprefix.."_placer",{ description = dispname, inventory_image = imgprefix.."_placer.png", wield_image = imgprefix.."_placer.png", groups={advtrains_trackplacer=1, digtron_on_place=1}, liquids_pointable = def.liquids_pointable, on_place = function(itemstack, placer, pointed_thing) local name = placer:get_player_name() if not name then return itemstack, false end if pointed_thing.type=="node" then local pos=pointed_thing.above local upos=vector.subtract(pointed_thing.above, {x=0, y=1, z=0}) if not advtrains.check_track_protection(pos, name) then return itemstack, false end if minetest.registered_nodes[minetest.get_node(pos).name] and minetest.registered_nodes[minetest.get_node(pos).name].buildable_to then local s if def.suitable_substrate then s = def.suitable_substrate(upos) else s = minetest.registered_nodes[minetest.get_node(upos).name] and minetest.registered_nodes[minetest.get_node(upos).name].walkable end if s then -- minetest.chat_send_all(nnprefix) local yaw = placer:get_look_horizontal()