aboutsummaryrefslogtreecommitdiff
path: root/clientmods/buildbot/init.lua
blob: 3cb13999edb95731190d1366d53d1e92becaae07 (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
buildbot = {}

local function build_y(callback)
	buildbot.pos.y = buildbot.pos.y - buildbot.step.y
	local function step()
		buildbot.pos.y = buildbot.pos.y + buildbot.step.y
		minetest.after(0.25, (buildbot.pos.y == buildbot.goal.y) and callback or step)
		minetest.place_node(buildbot.pos)
		local player_pos = minetest.find_node_near(buildbot.pos, 2, "air")
		if player_pos then
			minetest.localplayer:set_pos(player_pos)
		end
	end
	minetest.after(0.25, step)
end

local function build_z(callback)
	buildbot.pos.z = buildbot.pos.z - buildbot.step.z
	local function step()
		buildbot.start.y, buildbot.goal.y = buildbot.goal.y, buildbot.start.y
		buildbot.step.y = (buildbot.goal.y > buildbot.pos.y) and 1 or -1
		buildbot.pos.z = buildbot.pos.z + buildbot.step.z
		build_y((buildbot.pos.z == buildbot.goal.z) and callback or step)
	end
	minetest.after(0.25, step)
end

local function build_x(callback)
	buildbot.pos.x = buildbot.pos.x - buildbot.step.x
	local function step()
		buildbot.start.z, buildbot.goal.z = buildbot.goal.z, buildbot.start.z
		buildbot.step.z = (buildbot.goal.z > buildbot.pos.z) and 1 or -1
		buildbot.pos.x = buildbot.pos.x + buildbot.step.x
		build_z((buildbot.pos.x == buildbot.goal.x) and callback or step)
	end
	minetest.after(0.25, step)
end

minetest.register_chatcommand("build", {
	func = function(param)
		local sucess
		buildbot.start = vector.round(minetest.localplayer:get_pos())
		buildbot.pos = vector.new(buildbot.start)
		success, buildbot.goal = minetest.parse_pos(param)
		if success then
			buildbot.step = {}
			buildbot.step.x = (buildbot.goal.x > buildbot.start.x) and 1 or -1
			buildbot.start.z, buildbot.goal.z = buildbot.goal.z, buildbot.start.z
			buildbot.start.y, buildbot.goal.y = buildbot.goal.y, buildbot.start.y
			build_x(function() minetest.display_chat_message("Done.") end)
		end
		return false, buildbot.goal
	end
})

local keep_digging = false

core.register_chatcommand("keepdigging", {
	params = "<X>,<Y>,<Z>",
	description = "Dig node again and again",
	func = function(param)
		local success, pos = core.parse_pos(param)
		if success then
			keep_digging = true
			local function loop()
				core.dig_node(pos)
				if keep_digging then
					core.after(0.1, loop)
				end
			end
			loop()
		end
	end,
})

core.register_chatcommand("stopdigging", {
	description = "Stop diggin",
	func = function()
		keep_digging = false
	end,
})


core.register_chatcommand("digaround", {
	description = "Automatically dig nodes around you",
	param = "<radius> node1[,node2...]",
	func = function(param)
		local radius = tonumber(param:split(" ")[1])
		local nodes = param:split(" ")[2]:split(",")
		local function loop()
			local fpos = core.find_node_near(core.localplayer:get_pos(), radius, nodes, true)
			if fpos then core.dig_node(fpos) end
			core.after(0, loop)
		end
		loop()
	end,
})