File size: 2,753 Bytes
b6a38d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
if not const.ConnectivitySupported then
	return
end

OnMsg.PostNewMapLoaded = ConnectivityResume
OnMsg.PostLoadGame = ConnectivityResume

if Platform.asserts then

function TestConnectivity(unit, target, count)
	count = count or 1
	unit = unit or SelectedObj
	target = target or terrain.FindPassable(GetCursorPos())
	DbgClear()
	if not IsKindOf(unit, "Movable") then return end
	target = target or MapFindNearest(unit, "map", unit.class, function(obj) return obj ~= unit end)
	if not target then return end
	ConnectivityClear() -- test the uncached connectivity speed, as the cached one is practically zero.
	local stA = GetPreciseTicks(1000000)
	local pathA = ConnectivityCheck(unit, target) or false
	local timeA = GetPreciseTicks(1000000) - stA
	local stB = GetPreciseTicks(1000000)
	local pfclass, range, min_range, path_owner, restrict_area_radius, restrict_area
	local path_flags = const.pfmImpassableSource
	local pathB = pf.HasPosPath(unit, target, pfclass, range, min_range, path_owner, restrict_area_radius, restrict_area, path_flags) or false
	local timeB = GetPreciseTicks(1000000) - stB
	DbgAddSegment(unit, target)
	print("1 | path:", pathA, "| time:", timeA / 1000.0, "| ConnectivityCheck")
	print("2 | path:", pathB, "| time:", timeB / 1000.0, "| pf.HasPosPath")
	print("Linear dist 2D:", unit:GetDist2D(target))
end

function TestConnectivityShowPatch(pos)
	hr.DbgAutoClearLimit = Max(20000, hr.DbgAutoClearLimit)
	hr.DbgAutoClearLimitTexts = Max(10000, hr.DbgAutoClearLimitTexts)
	pos = pos or SelectedObj or GetCursorPos()
	local pfclass = 0
	if IsKindOf(pos, "Movable") then
		pfclass = pos:GetPfClass()
	end
	pos = terrain.FindPassable(pos)
	print(ValueToStr(ConnectivityPatchInfo(ConnectivityGameToPatch(pos), pfclass)))
end

function TestConnectivityRecalcPatch(pos)
	pos = pos or SelectedObj or GetCursorPos()
	local grid = 0
	if IsKindOf(pos, "Movable") then
		grid = table.get(pos:GetPfClassData(), "pass_grid") or 0
	end
	pos = terrain.FindPassable(pos)
	ConnectivityRecalcPatch(ConnectivityGameToPatch(pos, grid))
end

function TestConnectivityPerformance(pos)
	pos = terrain.FindPassable(pos or SelectedObj or GetCursorPos())
	local minx, miny, maxx, maxy = GetPlayBox(guim):xyxy()
	local seed = 0
	local count = 100000
	local x, y
	local target = point()
	SuspendThreadDebugHook(1)
	local st = GetPreciseTicks(1000000)
	for i=1,count do
		x, seed = BraidRandom(seed, minx, maxx - 1)
		y, seed = BraidRandom(seed, miny, maxy - 1)
		if terrain.IsPassable(x, y) then
			target:InplaceSet(x, y)
			ConnectivityClear()
			ConnectivityCheck(pos, target)
		end
	end
	print("Avg Time:", (GetPreciseTicks(1000000) - st) / (1000.0 * count))
	print("Stats:", ConnectivityStats())
	ResumeThreadDebugHook(1)
end

end -- Platform.asserts