aboutsummaryrefslogtreecommitdiff
path: root/games/devtest/mods/testpathfinder
diff options
context:
space:
mode:
authorWuzzy <wuzzy2@mail.ru>2020-05-26 00:17:52 +0200
committerGitHub <noreply@github.com>2020-05-26 00:17:52 +0200
commit083b285f4319c470f307f0b52f03a2fb68facd38 (patch)
treebdd02540ad58756a38606f03a995ab837a176709 /games/devtest/mods/testpathfinder
parentb546e8938d41aa9e3101fb9d4d5b02924ed73b60 (diff)
downloadminetest-083b285f4319c470f307f0b52f03a2fb68facd38.tar.gz
minetest-083b285f4319c470f307f0b52f03a2fb68facd38.tar.bz2
minetest-083b285f4319c470f307f0b52f03a2fb68facd38.zip
Rename “Minimal development test” to “Development Test” (#9928)
Diffstat (limited to 'games/devtest/mods/testpathfinder')
-rw-r--r--games/devtest/mods/testpathfinder/README.md15
-rw-r--r--games/devtest/mods/testpathfinder/init.lua132
-rw-r--r--games/devtest/mods/testpathfinder/mod.conf2
-rw-r--r--games/devtest/mods/testpathfinder/textures/testpathfinder_testpathfinder.pngbin0 -> 159 bytes
-rw-r--r--games/devtest/mods/testpathfinder/textures/testpathfinder_waypoint.pngbin0 -> 99 bytes
-rw-r--r--games/devtest/mods/testpathfinder/textures/testpathfinder_waypoint_end.pngbin0 -> 147 bytes
-rw-r--r--games/devtest/mods/testpathfinder/textures/testpathfinder_waypoint_start.pngbin0 -> 139 bytes
7 files changed, 149 insertions, 0 deletions
diff --git a/games/devtest/mods/testpathfinder/README.md b/games/devtest/mods/testpathfinder/README.md
new file mode 100644
index 000000000..2b9d46e70
--- /dev/null
+++ b/games/devtest/mods/testpathfinder/README.md
@@ -0,0 +1,15 @@
+# Pathfinder Tester
+
+Usage:
+
+Use the Pathfinder Tester tool (`testpathfinder:testpathfinder`).
+Here's how it works:
+
+* Place on node: Set destination position
+* Punch: Find path
+* Sneak+punch: Select pathfinding algorithm
+
+Information will be shown in chat. If a path was found, all waypoints
+will be shown for a few seconds.
+
+See `init.lua` for config variables.
diff --git a/games/devtest/mods/testpathfinder/init.lua b/games/devtest/mods/testpathfinder/init.lua
new file mode 100644
index 000000000..f94848236
--- /dev/null
+++ b/games/devtest/mods/testpathfinder/init.lua
@@ -0,0 +1,132 @@
+local S = minetest.get_translator("testpathfinder")
+
+-- Config parameters
+
+-- Maximum direct distance between start and end
+local MAX_DIRECT_DISTANCE = 64
+-- Maximum search distance
+local MAX_SEARCH_DISTANCE = 32
+-- Maximum permitted jump height
+local MAX_JUMP = 1
+-- Maximum permitted drop height
+local MAX_DROP = 5
+-- If true, mod won't refuse to run pathfinder even at long distances
+local IGNORE_MAX_DISTANCE_SAFEGUARD = false
+
+-- End of config parameters
+
+local timer = 0
+local algorithms = {
+ "A*_noprefetch",
+ "A*",
+ "Dijkstra",
+}
+
+local function find_path_for_player(player, itemstack)
+ local meta = itemstack:get_meta()
+ if not meta then
+ return
+ end
+ local x = meta:get_int("pos_x")
+ local y = meta:get_int("pos_y")
+ local z = meta:get_int("pos_z")
+ local algo = meta:get_int("algorithm")
+ if x and y and z then
+ local pos2 = {x=x, y=y, z=z}
+ algo = algorithms[algo+1]
+ local pos1 = vector.round(player:get_pos())
+ -- Don't bother calling pathfinder for high distance to avoid freezing
+ if (not IGNORE_MAX_DISTANCE_SAFEGUARD) and (vector.distance(pos1, pos2) > MAX_DIRECT_DISTANCE) then
+ minetest.chat_send_player(player:get_player_name(), S("Destination too far away! Set a destination (via placing) within a distance of @1 and try again!", MAX_DIRECT_DISTANCE))
+ return
+ end
+ local str = S("Path from @1 to @2:",
+ minetest.pos_to_string(pos1),
+ minetest.pos_to_string(pos2))
+
+ minetest.chat_send_player(player:get_player_name(), str)
+ local time_start = minetest.get_us_time()
+ local path = minetest.find_path(pos1, pos2, MAX_SEARCH_DISTANCE, MAX_JUMP, MAX_DROP, algo)
+ local time_end = minetest.get_us_time()
+ local time_diff = time_end - time_start
+ str = ""
+ if not path then
+ minetest.chat_send_player(player:get_player_name(), S("No path!"))
+ minetest.chat_send_player(player:get_player_name(), S("Time: @1 ms", time_diff/1000))
+ return
+ end
+ for s=1, #path do
+ str = str .. minetest.pos_to_string(path[s]) .. "\n"
+ local t
+ if s == #path then
+ t = "testpathfinder_waypoint_end.png"
+ elseif s == 1 then
+ t = "testpathfinder_waypoint_start.png"
+ else
+ local c = math.floor(((#path-s)/#path)*255)
+ t = string.format("testpathfinder_waypoint.png^[multiply:#%02x%02x00", 0xFF-c, c)
+ end
+ minetest.add_particle({
+ pos = path[s],
+ expirationtime = 5 + 0.2 * s,
+ playername = player:get_player_name(),
+ glow = minetest.LIGHT_MAX,
+ texture = t,
+ size = 3,
+ })
+ end
+ minetest.chat_send_player(player:get_player_name(), str)
+ minetest.chat_send_player(player:get_player_name(), S("Path length: @1", #path))
+ minetest.chat_send_player(player:get_player_name(), S("Time: @1 ms", time_diff/1000))
+ end
+end
+
+local function set_destination(itemstack, user, pointed_thing)
+ if not (user and user:is_player()) then
+ return
+ end
+ local name = user:get_player_name()
+ local obj
+ local meta = itemstack:get_meta()
+ if pointed_thing.type == "node" then
+ local pos = pointed_thing.above
+ meta:set_int("pos_x", pos.x)
+ meta:set_int("pos_y", pos.y)
+ meta:set_int("pos_z", pos.z)
+ minetest.chat_send_player(user:get_player_name(), S("Destination set to @1", minetest.pos_to_string(pos)))
+ return itemstack
+ end
+end
+
+local function find_path_or_set_algorithm(itemstack, user, pointed_thing)
+ if not (user and user:is_player()) then
+ return
+ end
+ local ctrl = user:get_player_control()
+ -- No sneak: Find path
+ if not ctrl.sneak then
+ find_path_for_player(user, itemstack)
+ else
+ -- Sneak: Set algorithm
+ local meta = itemstack:get_meta()
+ local algo = meta:get_int("algorithm")
+ algo = (algo + 1) % #algorithms
+ meta:set_int("algorithm", algo)
+ minetest.chat_send_player(user:get_player_name(), S("Algorithm: @1", algorithms[algo+1]))
+ return itemstack
+ end
+end
+
+-- Punch: Find path
+-- Sneak+punch: Select pathfinding algorithm
+-- Place: Select destination node
+minetest.register_tool("testpathfinder:testpathfinder", {
+ description = S("Pathfinder Tester"),
+ inventory_image = "testpathfinder_testpathfinder.png",
+ groups = { testtool = 1, disable_repair = 1 },
+ on_use = find_path_or_set_algorithm,
+ on_secondary_use = set_destination,
+ on_place = set_destination,
+})
+
+
diff --git a/games/devtest/mods/testpathfinder/mod.conf b/games/devtest/mods/testpathfinder/mod.conf
new file mode 100644
index 000000000..e6034ae8c
--- /dev/null
+++ b/games/devtest/mods/testpathfinder/mod.conf
@@ -0,0 +1,2 @@
+name = testpathfinder
+description = Tool to test Minetest's pathfinder function
diff --git a/games/devtest/mods/testpathfinder/textures/testpathfinder_testpathfinder.png b/games/devtest/mods/testpathfinder/textures/testpathfinder_testpathfinder.png
new file mode 100644
index 000000000..37eef0565
--- /dev/null
+++ b/games/devtest/mods/testpathfinder/textures/testpathfinder_testpathfinder.png
Binary files differ
diff --git a/games/devtest/mods/testpathfinder/textures/testpathfinder_waypoint.png b/games/devtest/mods/testpathfinder/textures/testpathfinder_waypoint.png
new file mode 100644
index 000000000..661dcf906
--- /dev/null
+++ b/games/devtest/mods/testpathfinder/textures/testpathfinder_waypoint.png
Binary files differ
diff --git a/games/devtest/mods/testpathfinder/textures/testpathfinder_waypoint_end.png b/games/devtest/mods/testpathfinder/textures/testpathfinder_waypoint_end.png
new file mode 100644
index 000000000..41a1cc549
--- /dev/null
+++ b/games/devtest/mods/testpathfinder/textures/testpathfinder_waypoint_end.png
Binary files differ
diff --git a/games/devtest/mods/testpathfinder/textures/testpathfinder_waypoint_start.png b/games/devtest/mods/testpathfinder/textures/testpathfinder_waypoint_start.png
new file mode 100644
index 000000000..a22e31c3b
--- /dev/null
+++ b/games/devtest/mods/testpathfinder/textures/testpathfinder_waypoint_start.png
Binary files differ