aboutsummaryrefslogtreecommitdiff
path: root/games/devtest/mods/testnodes/textures/testnodes_normal3.png
blob: 0426ab216faf25f645c5b49d202c6f8f79f04318 (plain)
ofshex dumpascii
0000 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 00 10 00 00 00 10 08 02 00 00 00 90 91 68 .PNG........IHDR...............h
0020 36 00 00 00 39 49 44 41 54 78 01 63 f8 cf c0 f0 9f 14 c0 00 c2 44 03 84 86 33 96 ff 09 22 aa 69 6...9IDATx.c.........D...3...".i
0040 40 b8 90 28 0d 98 6c c2 36 d0 54 03 a6 1f 06 de 49 20 36 1d fd 80 a9 81 bc c4 47 22 00 00 c6 73 @..(..l.6.T.....I.6.......G"...s
0060 b1 5a b2 6f c5 25 00 00 00 00 49 45 4e 44 ae 42 60 82 .Z.o.%....IEND.B`.
44'>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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
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