File: skyblock.levels.lua

package info (click to toggle)
minetest-mod-skyblock 0.2.4-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 384 kB
  • sloc: makefile: 14
file content (285 lines) | stat: -rw-r--r-- 7,858 bytes parent folder | download | duplicates (2)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
--[[

Skyblock for Minetest

Copyright (c) 2015 cornernote, Brett O'Donnell <cornernote@gmail.com>
Source Code: https://github.com/cornernote/minetest-skyblock
License: GPLv3

]]--


skyblock.levels = {}

--
-- CONFIG OPTIONS
--

-- true to restart from level1, false to restart from current level
skyblock.levels.restart_on_die = minetest.setting_getbool('skyblock.levels.restart_on_die')


--
-- Functions
--

-- empty inventory
function skyblock.levels.empty_inventory(player)
	local inv = player:get_inventory()
	if not inv:is_empty('rewards') then
		for i=1,inv:get_size('rewards') do
			inv:set_stack('rewards', i, nil)
		end
	end
	if skyblock.levels.lose_bags_on_death then
		local bags_inv = minetest.get_inventory({type='detached', name=player:get_player_name()..'_bags'})
		for bag=1,4 do
			if not bags_inv:is_empty('bag'..bag) then
				for i=1,bags_inv:get_size('bag'..bag) do
					inv:set_stack('bag'..bag, i, nil)
				end
				for i=1,bags_inv:get_size('bag'..bag) do
					bags_inv:set_stack('bag'..bag, i, nil)
					inv:set_stack('bag'..bag..'contents', i, nil)
				end
			end
		end
	end
end


--
-- Formspec
--

-- get_formspec
function skyblock.levels.get_formspec(player_name)
	local level = skyblock.feats.get_level(player_name)
	local level_info = skyblock.levels[level].get_info(player_name)
	return level_info.formspec
end

-- get_inventory_formspec
function skyblock.levels.get_inventory_formspec(level,player_name,inventory)
	local formspec = 'size[15,10;]'
	if inventory then
		if minetest.get_modpath('unified_inventory') then
			formspec = formspec..skyblock.get_unified_inventory_buttons()
		end
		formspec = formspec..'button_exit[13,0;2,0.5;close;Close]'
	else
		formspec = formspec
			..'button[11,0;2,0.5;restart;Restart]'
			..'button_exit[13,0;2,0.5;close;Close]'
	end
		
	formspec = formspec
		..'background[-0.1,-0.1;6.6,10.3;goals.png]'
		..'label[0,0; --== LEVEL '..level..' for '..player_name..' ==--]'

		..'label[7,1.5; Rewards]'
		..'background[6.9,1.4;2.2,2.8;rewards.png]'
		..'list[current_player;rewards;7,2;2,2;]'

		..'label[10,1.5; Craft]'
		..'label[14,2.5;Output]'
		..'background[9.9,1.4;5.2,3.8;craft.png]'
		..'list[current_player;craft;10,2;3,3;]'
		..'list[current_player;craftpreview;14,3;1,1;]'
		
		..'label[7,5.5; Inventory]'
		..'background[6.9,5.4;8.2,4.8;inventory.png]'
		..'list[current_player;main;7,6;8,4;]'
	return formspec
end

-- render an item image button for the formspec
local function image_button_link(stack_string)
	local stack = ItemStack(stack_string);
	local new_node_name = stack_string;
	if stack and stack:get_name() then
		new_node_name = stack:get_name()
	end
	return tostring(stack_string)..';item_button_nochange_'..unified_inventory.mangle_for_formspec(new_node_name)..';'
end

-- get_feat_formspec
function skyblock.levels.get_feat_formspec(info,i,feat,required,text,hint,inventory)
	local y = 2.9+(i*0.6)
	local count = skyblock.feats.get(info.level,info.player_name,feat)
	if count > required then
		count = required
	end
	local formspec = 'label[0.5,'..y..'; '..text..' ('..count..'/'..required..')]'
	if hint then
		if inventory and minetest.get_modpath('unified_inventory') then
			formspec = formspec..'item_image_button[5.8,'..y..';0.6,0.6;'..image_button_link(hint)..']'
		else
			formspec = formspec..'item_image_button[5.8,'..y..';0.6,0.6;'..skyblock.craft_guide.image_button_link(hint)..']'
		end
	end
	if count == required then
		formspec = formspec .. 'image[-0.2,'..(y-0.25)..';1,1;checkbox_checked.png]'
		info.count = info.count + 1
	else
		formspec = formspec .. 'image[-0.2,'..(y-0.25)..';1,1;checkbox_unchecked.png]'
	end
	return formspec
end

-- get_unified_inventory_buttons
function skyblock.get_unified_inventory_buttons()
	local formspec = ''
	local button_row = 0
	local button_col = 0
	local main_button_x = 7
	local main_button_y = 0
	for i, def in pairs(unified_inventory.buttons) do
		if unified_inventory.lite_mode and i > 4 then
			button_row = 1
			button_col = 1
		end
		local tooltip = def.tooltip or ''
		if def.type == 'image' then
			formspec = formspec..'image_button['
					..( main_button_x + 0.65 * (i - 1) - button_col * 0.65 * 4)
					..','..(main_button_y + button_row * 0.7)..';0.8,0.8;'
					..minetest.formspec_escape(def.image)..';'
					..minetest.formspec_escape(def.name)..';]'
					..'tooltip['..minetest.formspec_escape(def.name)
					..';'..tooltip..']'
		end
	end
	return formspec
end

-- get_restart_formspec
function skyblock.show_restart_formspec(player_name)
	local formspec = 'size[6,2.5;]'
		..'label[0,0.0;-= Are you sure you want to restart? =-]'
		..'label[0,0.5;You will lose all your Items and will have to redo]'
		..'label[0,1.0;all of your Quests starting from Level 1.]'
		..'button[0,2;3,0.5;restart;Confirm Restart]'
		..'button_exit[3,2;3,0.5;close;Cancel]'
	minetest.show_formspec(player_name, 'skyblock_restart', formspec)
end

--
-- Feat Checks
--

-- reward_feat
function skyblock.levels.reward_feat(level,player_name,feat)
	local count = skyblock.feats.get(level,player_name,feat)
	for _,v in ipairs(skyblock.levels[level].feats) do
		if v.feat == feat and v.count == count then
			skyblock.feats.give_reward(level,player_name,v.reward)
			return true
		end
	end
end

-- track digging feats
function skyblock.levels.on_dignode(level,pos,oldnode,digger)
	local player_name = digger:get_player_name()
	for _,v in ipairs(skyblock.levels[level].feats) do
		if v.dignode then
			for _,vv in ipairs(v.dignode) do
				if oldnode.name == vv then
					skyblock.feats.add(level,player_name,v.feat)
					return
				end
			end
		end
	end
end

-- track placing feats
function skyblock.levels.on_placenode(level,pos,newnode,placer,oldnode)
	local player_name = placer:get_player_name()
	for _,v in ipairs(skyblock.levels[level].feats) do
		if v.placenode then
			for _,vv in ipairs(v.placenode) do
				if newnode.name == vv then
					skyblock.feats.add(level,player_name,v.feat)
					return
				end
			end
		end
	end
end

-- track eating feats
function skyblock.levels.on_item_eat(level,player_name,itemstack)
	local item_name = itemstack:get_name()
	for _,v in ipairs(skyblock.levels[level].feats) do
		if v.item_eat then
			for _,vv in ipairs(v.item_eat) do
				if item_name==vv then
					skyblock.feats.add(level,player_name,v.feat)
					return
				end
			end
		end
	end
end

-- track crafting feats
function skyblock.levels.on_craft(level,player_name,itemstack)
	local item_name = itemstack:get_name()
	for _,v in ipairs(skyblock.levels[level].feats) do
		if v.craft then
			for _,vv in ipairs(v.craft) do
				if item_name==vv then
					skyblock.feats.add(level,player_name,v.feat)
					return
				end
			end
		end
	end
end

-- track bucket feats
function skyblock.levels.bucket_on_use(level,player_name,pointed_thing)
	local node = minetest.env:get_node(pointed_thing.under)
	for _,v in ipairs(skyblock.levels[level].feats) do
		if v.bucket then
			for _,vv in ipairs(v.bucket) do
				if node.name == vv then
					skyblock.feats.add(level,player_name,v.feat)
					return
				end
			end
		end
	end
end

-- track bucket water feats
function skyblock.levels.bucket_water_on_use(level,player_name,pointed_thing) 
	local node = minetest.env:get_node(pointed_thing.under)
	for _,v in ipairs(skyblock.levels[level].feats) do
		if v.bucket_water then
			for _,vv in ipairs(v.bucket_water) do
				if node.name == vv then
					skyblock.feats.add(level,player_name,v.feat)
					return
				end
			end
		end
	end
end

-- track bucket lava feats
function skyblock.levels.bucket_lava_on_use(level,player_name,pointed_thing)
	local node = minetest.env:get_node(pointed_thing.under)
	for _,v in ipairs(skyblock.levels[level].feats) do
		if v.bucket_lava then
			for _,vv in ipairs(v.bucket_lava) do
				if node.name == vv then
					skyblock.feats.add(level,player_name,v.feat)
					return
				end
			end
		end
	end
end