summaryrefslogtreecommitdiff
path: root/init.lua
blob: d8e344b82572893dfb55899c481926f6048375df (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
local globalstepLag = 0
local ABMLag = 0
local ABMLastRun = 0

minetest.register_chatcommand("globalsteplag",
	{
		params = "[time per globalstep, in ms]",
		description = "Add the specified amount of lag on each globalstep",
		privs = {server = true},
		func = function(name,param)
			if param ~= "" then
				if not tonumber(param) then
					return false,minetest.colorize("#FF0000","ERROR")..": Invalid value. Valid range is 0-5000."
				elseif tonumber(param) > 5000 then
					return false,minetest.colorize("#FF0000","ERROR")..": Invalid value. Valid range is 0-5000."
				elseif tonumber(param) < 0 then
					return false,minetest.colorize("#FF0000","ERROR")..": Invalid value. Only positive values are allowed."
				end
			else
				return true,"Current globalstep lag setting: "..minetest.colorize("#00FFFF",tostring(globalstepLag))
			end
			if tonumber(param) then
				globalstepLag = tonumber(param)
				return true,"Globalstep lag set to: "..minetest.colorize("#00FFFF",tostring(globalstepLag))
			end
		end
	}
)

minetest.register_chatcommand("abmlag",
	{
		params = "[time per second, in ms]",
		description = "Add the specified amount of lag to an ABM running (nominally) once per second",
		privs = {server = true},
		func = function(name,param)
			if param ~= "" then
				if not tonumber(param) then
					return false,minetest.colorize("#FF0000","ERROR")..": Invalid value. Valid range is 0-5000."
				elseif tonumber(param) > 5000 then
					return false,minetest.colorize("#FF0000","ERROR")..": Invalid value. Valid range is 0-5000."
				elseif tonumber(param) < 0 then
					return false,minetest.colorize("#FF0000","ERROR")..": Invalid value. Only positive values are allowed."
				end
			else
				return true,"Current ABM lag setting: "..minetest.colorize("#00FFFF",tostring(ABMLag))
			end
			if tonumber(param) then
				ABMLag = tonumber(param)
				return true,"ABM lag set to: "..minetest.colorize("#00FFFF",tostring(ABMLag))
			end
		end
	}
)

minetest.register_chatcommand("lag",
	{
		params = "[time, in ms]",
		description = "Freeze the server for the specified length of time",
		privs = {server = true},
		func = function(name,param)
			if param ~= "" then
				if not tonumber(param) then
					return false,minetest.colorize("#FF0000","ERROR")..": Invalid value. Valid range is 0-5000."
				elseif tonumber(param) > 5000 then
					return false,minetest.colorize("#FF0000","ERROR")..": Invalid value. Valid range is 0-5000."
				elseif tonumber(param) < 0 then
					return false,minetest.colorize("#FF0000","ERROR")..": Invalid value. Only positive values are allowed."
				end
			else
				return false,minetest.colorize("#FF0000","ERROR")..": No value specified."
			end
			if tonumber(param) then
				local endTime = os.clock() + (tonumber(param)/1000)
				while os.clock() < endTime do
					--Nothing, just busy-waiting here
				end
				return true,"Done!"
			end
		end
	}
)

minetest.register_globalstep(function()
	if globalstepLag == 0 then return end
	local endTime = os.clock() + (globalstepLag/1000)
	while os.clock() < endTime do
		--Nothing, just busy-waiting here
	end
end)

minetest.register_abm({
	label = "Generate Lag",
	nodenames = {"air"},
	interval = 1,
	chance = 1,
	action = function()
		if ABMLag == 0 or ABMLastRun < (os.time() + 1) then return end
		ABMLastRun = os.time()
		local endTime = os.clock() + (ABMLag/1000)
		while os.clock() < endTime do
			--Nothing, just busy-waiting here
		end
	end,
})