]> git.lizzy.rs Git - dragonfireclient.git/blob - games/devtest/mods/util_commands/init.lua
Enable shadows by default in devtest (#12157)
[dragonfireclient.git] / games / devtest / mods / util_commands / init.lua
1 minetest.register_chatcommand("hotbar", {
2         params = "<size>",
3         description = "Set hotbar size",
4         func = function(name, param)
5                 local player = minetest.get_player_by_name(name)
6                 if not player then
7                         return false, "No player."
8                 end
9                 local size = tonumber(param)
10                 if not size then
11                         return false, "Missing or incorrect size parameter!"
12                 end
13                 local ok = player:hud_set_hotbar_itemcount(size)
14                 if ok then
15                         return true
16                 else
17                         return false, "Invalid item count!"
18                 end
19         end,
20 })
21
22 minetest.register_chatcommand("hp", {
23         params = "<hp>",
24         description = "Set your health",
25         func = function(name, param)
26                 local player = minetest.get_player_by_name(name)
27                 if not player then
28                         return false, "No player."
29                 end
30                 local hp = tonumber(param)
31                 if not hp then
32                         return false, "Missing or incorrect hp parameter!"
33                 end
34                 player:set_hp(hp)
35                 return true
36         end,
37 })
38
39 minetest.register_on_joinplayer(function(player)
40         player:set_properties({zoom_fov = 15})
41 end)
42
43 minetest.register_chatcommand("zoomfov", {
44         params = "[<FOV>]",
45         description = "Set or display your zoom_fov",
46         func = function(name, param)
47                 local player = minetest.get_player_by_name(name)
48                 if not player then
49                         return false, "No player."
50                 end
51                 if param == "" then
52                         local fov = player:get_properties().zoom_fov
53                         return true, "zoom_fov = "..tostring(fov)
54                 end
55                 local fov = tonumber(param)
56                 if not fov then
57                         return false, "Missing or incorrect zoom_fov parameter!"
58                 end
59                 player:set_properties({zoom_fov = fov})
60                 fov = player:get_properties().zoom_fov
61                 return true, "zoom_fov = "..tostring(fov)
62         end,
63 })
64
65 local s_infplace = minetest.settings:get("devtest_infplace")
66 if s_infplace == "true" then
67         infplace = true
68 elseif s_infplace == "false" then
69         infplace = false
70 else
71         infplace = minetest.is_creative_enabled("")
72 end
73
74 minetest.register_chatcommand("infplace", {
75         params = "",
76         description = "Toggle infinite node placement",
77         func = function(name, param)
78                 infplace = not infplace
79                 if infplace then
80                         minetest.chat_send_all("Infinite node placement enabled!")
81                         minetest.log("action", "Infinite node placement enabled")
82                 else
83                         minetest.chat_send_all("Infinite node placement disabled!")
84                         minetest.log("action", "Infinite node placement disabled")
85                 end
86                 return true
87         end,
88 })
89
90 minetest.register_chatcommand("detach", {
91         params = "[<radius>]",
92         description = "Detach all objects nearby",
93         func = function(name, param)
94                 local radius = tonumber(param)
95                 if type(radius) ~= "number" then
96                         radius = 8
97                 end
98                 if radius < 1 then
99                         radius = 1
100                 end
101                 local player = minetest.get_player_by_name(name)
102                 if not player then
103                         return false, "No player."
104                 end
105                 local objs = minetest.get_objects_inside_radius(player:get_pos(), radius)
106                 local num = 0
107                 for o=1, #objs do
108                         if objs[o]:get_attach() then
109                                 objs[o]:set_detach()
110                                 num = num + 1
111                         end
112                 end
113                 return true, string.format("%d object(s) detached.", num)
114         end,
115 })
116
117 minetest.register_chatcommand("use_tool", {
118         params = "(dig <group> <leveldiff>) | (hit <damage_group> <time_from_last_punch>) [<uses>]",
119         description = "Apply tool wear a number of times, as if it were used for digging",
120         func = function(name, param)
121                 local player = minetest.get_player_by_name(name)
122                 if not player then
123                         return false, "No player."
124                 end
125                 local mode, group, level, uses = string.match(param, "([a-z]+) ([a-z0-9]+) (-?%d+) (%d+)")
126                 if not mode then
127                         mode, group, level = string.match(param, "([a-z]+) ([a-z0-9]+) (-?%d+)")
128                         uses = 1
129                 end
130                 if not mode or not group or not level then
131                         return false
132                 end
133                 if mode ~= "dig" and mode ~= "hit" then
134                         return false
135                 end
136                 local tool = player:get_wielded_item()
137                 local caps = tool:get_tool_capabilities()
138                 if not caps or tool:get_count() == 0 then
139                         return false, "No tool in hand."
140                 end
141                 local actual_uses = 0
142                 for u=1, uses do
143                         local wear = tool:get_wear()
144                         local dp
145                         if mode == "dig" then
146                                 dp = minetest.get_dig_params({[group]=3, level=level}, caps, wear)
147                         else
148                                 dp = minetest.get_hit_params({[group]=100}, caps, level, wear)
149                         end
150                         tool:add_wear(dp.wear)
151                         actual_uses = actual_uses + 1
152                         if tool:get_count() == 0 then
153                                 break
154                         end
155                 end
156                 player:set_wielded_item(tool)
157                 if tool:get_count() == 0 then
158                         return true, string.format("Tool used %d time(s). "..
159                                         "The tool broke after %d use(s).", uses, actual_uses)
160                 else
161                         local wear = tool:get_wear()
162                         return true, string.format("Tool used %d time(s). "..
163                                         "Final wear=%d", uses, wear)
164                 end
165         end,
166 })
167
168
169
170 -- Use this to test waypoint capabilities
171 minetest.register_chatcommand("test_waypoints", {
172         params = "[change_immediate]",
173         description = "tests waypoint capabilities",
174         func = function(name, params)
175                 local player = minetest.get_player_by_name(name)
176                 local regular = player:hud_add {
177                         hud_elem_type = "waypoint",
178                         name = "regular waypoint",
179                         text = "m",
180                         number = 0xFF0000,
181                         world_pos = vector.add(player:get_pos(), {x = 0, y = 1.5, z = 0})
182                 }
183                 local reduced_precision = player:hud_add {
184                         hud_elem_type = "waypoint",
185                         name = "better waypoint",
186                         text = "m (0.5 steps, precision = 2)",
187                         precision = 10,
188                         number = 0xFFFF00,
189                         world_pos = vector.add(player:get_pos(), {x = 0, y = 1, z = 0})
190                 }
191                 local function change()
192                         if regular then
193                                 player:hud_change(regular, "world_pos", vector.add(player:get_pos(), {x = 0, y = 3, z = 0}))
194                         end
195                         if reduced_precision then
196                                 player:hud_change(reduced_precision, "precision", 2)
197                         end
198                 end
199                 if params ~= "" then
200                         -- change immediate
201                         change()
202                 else
203                         minetest.after(0.5, change)
204                 end
205                 regular = regular or "error"
206                 reduced_precision = reduced_precision or "error"
207                 local hidden_distance = player:hud_add {
208                         hud_elem_type = "waypoint",
209                         name = "waypoint with hidden distance",
210                         text = "this text is hidden as well (precision = 0)",
211                         precision = 0,
212                         number = 0x0000FF,
213                         world_pos = vector.add(player:get_pos(), {x = 0, y = 0.5, z = 0})
214                 } or "error"
215                 local image_waypoint = player:hud_add {
216                         hud_elem_type = "image_waypoint",
217                         text = "wieldhand.png",
218                         world_pos = player:get_pos(),
219                         scale = {x = 10, y = 10},
220                         offset = {x = 0, y = -32}
221                 } or "error"
222                 minetest.chat_send_player(name, "Waypoint IDs: regular: " .. regular .. ", reduced precision: " .. reduced_precision ..
223                         ", hidden distance: " .. hidden_distance .. ", image waypoint: " .. image_waypoint)
224         end
225 })
226
227 -- Unlimited node placement
228 minetest.register_on_placenode(function(pos, newnode, placer, oldnode, itemstack)
229         if placer and placer:is_player() then
230                 return infplace
231         end
232 end)
233
234 -- Don't pick up if the item is already in the inventory
235 local old_handle_node_drops = minetest.handle_node_drops
236 function minetest.handle_node_drops(pos, drops, digger)
237         if not digger or not digger:is_player() or not infplace then
238                 return old_handle_node_drops(pos, drops, digger)
239         end
240         local inv = digger:get_inventory()
241         if inv then
242                 for _, item in ipairs(drops) do
243                         if not inv:contains_item("main", item, true) then
244                                 inv:add_item("main", item)
245                         end
246                 end
247         end
248 end
249
250 minetest.register_chatcommand("set_displayed_itemcount", {
251         params = "(-s \"<string>\" [-c <color>]) | -a <alignment_num>",
252         description = "Set the displayed itemcount of the wielded item",
253         func = function(name, param)
254                 local player = minetest.get_player_by_name(name)
255                 local item = player:get_wielded_item()
256                 local meta = item:get_meta()
257                 local flag1 = param:sub(1, 2)
258                 if flag1 == "-s" then
259                         if param:sub(3, 4) ~= " \"" then
260                                 return false, "Error: Space and string with \"s expected after -s."
261                         end
262                         local se = param:find("\"", 5, true)
263                         if not se then
264                                 return false, "Error: String with two \"s expected after -s."
265                         end
266                         local s = param:sub(5, se - 1)
267                         if param:sub(se + 1, se + 4) == " -c " then
268                                 s = minetest.colorize(param:sub(se + 5), s)
269                         end
270                         meta:set_string("count_meta", s)
271                 elseif flag1 == "-a" then
272                         local num = tonumber(param:sub(4))
273                         if not num then
274                                 return false, "Error: Invalid number: "..param:sub(4)
275                         end
276                         meta:set_int("count_alignment", num)
277                 else
278                         return false
279                 end
280                 player:set_wielded_item(item)
281                 return true, "Displayed itemcount set."
282         end,
283 })
284
285 minetest.register_chatcommand("dump_item", {
286         params = "",
287         description = "Prints a dump of the wielded item in table form",
288         func = function(name, param)
289                 local player = minetest.get_player_by_name(name)
290                 local item = player:get_wielded_item()
291                 local str = dump(item:to_table())
292                 print(str)
293                 return true, str
294         end,
295 })
296
297 -- shadow control
298 minetest.register_on_joinplayer(function (player)
299         player:set_lighting({shadows={intensity = 0.33}})
300 end)
301
302 core.register_chatcommand("set_shadow", {
303     params = "<shadow_intensity>",
304     description = "Set shadow parameters of current player.",
305     func = function(player_name, param)
306         local shadow_intensity = tonumber(param)
307         minetest.get_player_by_name(player_name):set_lighting({shadows = { intensity = shadow_intensity} })
308     end
309 })