aboutsummaryrefslogtreecommitdiff
path: root/builtin/client/util.lua
blob: 440f99ebc7ecf7429616b176aff6c1f89a701519 (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
function core.parse_pos(param)
	local p = {}
	local playerpos = core.localplayer:get_pos()
	p.x, p.y, p.z = string.match(param, "^([~|%d.-]+)[, ] *([~|%d.-]+)[, ] *([~|%d.-]+)$")
	for k, v in pairs(p) do
		if p[k] == "~" then
			p[k] = playerpos[k]
		else
			p[k] = tonumber(v)
		end
	end
	if p.x and p.y and p.z then
		return true, vector.round(p)
	end
	return false, "Invalid position (" .. param .. ")"
end

function core.parse_relative_pos(param)
	local success, pos = core.parse_pos(param:gsub("~", "0"))
	if success then pos = vector.round(vector.add(core.localplayer:get_pos(), pos)) end
	return success, pos
end

function core.find_item(item, mini, maxi)
	for index, stack in ipairs(core.get_inventory("current_player").main) do
		if (not mini or index >= mini) and (not maxi or index <= maxi) and stack:get_name() == item then
			return index
		end
	end
end

function core.switch_to_item(item)
	local i = core.find_item(item)
	if i then
		core.localplayer:set_wield_index(i)
		return true
	else
		return false
	end
end

function core.get_pointed_thing()
	local pos = core.camera:get_pos()
	local pos2 = vector.add(pos, vector.multiply(core.camera:get_look_dir(), 7))
	local player = core.localplayer
	if not player then return end
	local item = player:get_wielded_item()
	if not item then return end
	local def = core.get_item_def(item:get_name())
	local ray = core.raycast(pos, pos2, true, core.settings:get_bool("point_liquids") or def and def.liquids_pointable)
	return ray and ray:next()
end

function core.close_formspec(formname)
	return core.show_formspec(formname, "")
end

function core.get_nearby_objects(radius)
	return core.get_objects_inside_radius(core.localplayer:get_pos(), radius)
end

-- HTTP callback interface

function core.http_add_fetch(httpenv)
	httpenv.fetch = function(req, callback)
		local handle = httpenv.fetch_async(req)

		local function update_http_status()
			local res = httpenv.fetch_async_get(handle)
			if res.completed then
				callback(res)
			else
				core.after(0, update_http_status)
			end
		end
		core.after(0, update_http_status)
	end

	return httpenv
end