File size: 8,273 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
if FirstLoad then
	s_ShowGossipMatch = false
	dbg_color = false
	dbg_objects = false
end

function DbgClearColors()
	for obj in pairs(dbg_color or empty_table) do
		if IsValid(obj) then
			ClearColorModifierReason(obj, "DbgColor")
		end
	end
	dbg_color = false
end

function DbgSetColor(obj, color, skip_attaches)
	if not IsValid(obj) then return end
	if not color then
		if dbg_color then dbg_color[obj] = nil end
		ClearColorModifierReason(obj, "DbgColor", nil, skip_attaches)
	else
		dbg_color = table.create_set(dbg_color, obj, true)
		SetColorModifierReason(obj, "DbgColor", color, 1000, nil, skip_attaches)
	end
end

function DbgClearObjs()
	DoneObjects(dbg_objects)
	dbg_objects = false
end

function DbgAddObj(obj)
	dbg_objects = table.create_add_unique(dbg_objects, obj)
end

function OnMsg.SaveGameStart()
	-- avoid saving temp changes
	DbgClearColors()
	DbgClearObjs()
end

if FirstLoad then
	DbgClearLast = false
end

function DbgClear(once)
	if once then
		local time = RealTime()
		if DbgClearLast == time then
			return
		end
		DbgClearLast = time
	end
	DbgClearVectors()
	DbgClearTexts()
	DbgClearColors()
	DbgClearObjs()
	Msg("DbgClear")
end
OnMsg.ChangeMap = DbgClear

function ShowGossip(gossip_match)
	s_ShowGossipMatch = gossip_match or false
	
	NetGossip = function(...)
		local params = table.pack(...)
		for idx, param in ipairs(params) do
			if type(param) == "string" and not utf8.IsValidString(param) then
				params[idx] = UnicodeEscapeCharactersToUtf8(param)
			end
		end
		if not s_ShowGossipMatch or string.match(s_ShowGossipMatch, params[1]) then
			print(table.unpack(params)) 
		end
	end
	
	netAllowGossip = true
end

function TracePos(o, color)
	local origSetPos = o.SetPos
	g_PosTraced[o] = color or RGB(255, 255, 255)
	o.SetPos = function(self, pos, ...)
		origSetPos(self, pos, ...)
		dbgDrawPosHistory(pos)
	end
end

function DrawRadiuses(o)
	CreateGameTimeThread(function()
		while IsValid(o) do
			MapForEach( o:GetPos(), 50 * guim, nil, const.efDestlock,
				function(obj)
					if not rawget(obj, "__radius_circle") then
						local circle = CreateCircleMesh(pf.GetDestlockRadius(obj), RGB(0, 255, 255))
						obj:SetEnumFlags(const.efVisible)
						obj:Attach(circle)
						rawset(obj, "__radius_circle", circle)
					end
				end )
			MapForEach(o:GetPos(), 50 * guim, "Unit",
				function(obj)
					if obj:GetEnumFlags(const.efResting) ~= 0 then
						if not rawget(obj, "__radius_circle") then
							local circle = CreateCircleMesh(pf.GetDestlockRadius(obj), RGB(0, 255, 0))
							obj:Attach(circle)
							rawset(obj, "__radius_circle", circle)
						end
					else
						if rawget(obj, "__radius_circle") then
							DoneObject(obj.__radius_circle)
							obj.__radius_circle = nil
						end
					end
				end)
			
			Sleep(100)
		end
	end)
end

g_PosTraced = rawget(_G, "g_PosTraced") or {}
function TraceCollisionOverstep(o, method)
	method = method or "GetCollisionRadius"
	local origSetPos = o.SetPos
	o.SetPos = function(self, pos, ...)
		local bad = {}
		MapForEach( pos, 5*guim, "Unit", 
				function(o)
					if o ~= self and not o:IsDead() and o:GetVisualDist2D(self) < self[method](self) + o[method](self) - 10 * guic then
						table.insert(bad, o)
					end
				end)
		if #bad > 0 then
			self:Trace("Overlaping", bad, GetStack(1))
			self:SetColorModifier(RGB(255, 0, 0))
			CreateGameTimeThread(function()
				Sleep(1000)
				if IsValid(self) then
					self:SetColorModifier(RGB(128, 128, 128))
				end
			end)
		end
		origSetPos(self, pos, ...)
	end
end

function dbgDrawPosHistory(pos)
	dbgOutputClear()
	for o, color in pairs(g_PosTraced) do
		local r, g, b = GetRGB(color)
		dbgDrawCircle(o:GetPos(), o:GetCollisionRadius(), RGB(r/2, g/2, b/2))
		dbgDrawArrow(o:GetVisualPos(), o:GetPos(), color)
		dbgInfo(o:GetPos(), RGB(255, 255, 255), false, o.class .. " " .. o:GetStateText())
	end
end

CameraDebugSegments = {}
CameraDebugPoints = {}

function dbgDrawCameraCollision()
	for i = 1, #CameraDebugPoints do
		DbgAddVector(CameraDebugSegments[i*2], CameraDebugSegments[i*2+1]-CameraDebugSegments[i*2], RGB(0, 255, 0))
		DbgAddVector(CameraDebugSegments[i*2], CameraDebugPoints[i]-CameraDebugSegments[i*2], RGB(0, 255, 0))
	end
end

function ShowRect(box, color, angle)
	CreateRealTimeThread(function()
		local rc = Polyline:new()
		local points = pstr("")
		points:AppendVertex(point(box:minx(), box:maxy(), 20 * guic), color)
		points:AppendVertex(point(box:maxx(), box:maxy(), 20 * guic))
		points:AppendVertex(point(box:maxx(), box:miny(), 20 * guic))
		points:AppendVertex(point(box:minx(), box:miny(), 20 * guic))
		points:AppendVertex(point(box:minx(), box:maxy(), 20 * guic))
		rc:SetMeshFlags(const.mfTerrainDistorted)
		rc:SetMesh(points)
		rc:SetPos(box:Center():SetTerrainZ())
		if angle then
			rc:SetAngle(angle)
		end
		
		Sleep(1000)
		rc:delete()
	end)
end


function ShowObj(obj)
	CreateRealTimeThread(function()
		local cm = obj:GetColorModifier()
		obj:SetColorModifier(blue)
		Sleep(1000)
		obj:SetColorModifier(cm)
	end)
end

if FirstLoad then
	showme_markers = {}
end

function ShowMe(o, color, time)
	if o == nil then
		return ClearShowMe()
	end
	if type(o) == "table" and #o == 2 then
		if IsPoint(o[1]) and terrain.IsPointInBounds(o[1]) and 
			IsPoint(o[2]) and terrain.IsPointInBounds(o[2]) then
			local m = Vector:new()
			m:Set(o[1], o[2], color)
			showme_markers[m] = "vector"
			o = m
		end
	elseif IsPoint(o) then
		if terrain.IsPointInBounds(o) then
			local m = CreateSphereMesh(50 * guic, color or RGB(0, 255, 0))
			m:SetPos(o)
			showme_markers[m] = "point"
			if not time then
				ViewPos(o)
			end
			o = m
		end
	elseif IsValid(o) then
		showme_markers[o] = showme_markers[o] or o:GetColorModifier()
		o:SetColorModifier(color or RGB(0, 255, 0))
		local pos = o:GetVisualPos()
		if not time and terrain.IsPointInBounds(pos) then
			ViewPos(pos)
		end
	else
		if not showme_markers[o] then
			AddTrackerText(false, o)
		end
	end
	if time then
		CreateGameTimeThread(function(o, time)
			Sleep(time)
			local v = showme_markers[o]
			if IsValid(o) then
				if v == "point" or v == "vector" then
					DoneObject(o)
				else
					o:SetColorModifier(v)
				end
			end
			if ClearTextTrackers then
				ClearTextTrackers(o)
			end
		end, o, time)
	end
end

function ClearShowMe()
	for k, v in pairs(showme_markers) do
		if IsValid(k) then
			if v == "point" then
				DoneObject(k)
			else
				k:SetColorModifier(v)
			end
		end
	end
	if ClearTextTrackers then
		ClearTextTrackers()
	end
	showme_markers = {}
end

function ShowCircle(pt, r, color)
	local c = CreateCircleMesh(r, color or RGB(255, 255, 255))
	c:SetPos(pt:SetTerrainZ(10*guic))
	CreateGameTimeThread(function()
		Sleep(7000)
		if IsValid(c) then
			c:delete()
		end
	end)
end

function DbgShowClassHierarchy(class, filter, unique_only)
	local html = "<!DOCTYPE html><html><head><meta http-equiv='refresh' content = '0; url = %s' /></head></html>"
	local url = "http://magjac.com/graphviz-visual-editor/?dot="
	local dot = {'strict digraph { rankdir=TB'}
	local node_style = '[shape="polygon" style="filled" fillcolor="#1f77b4" fontcolor="#ffffff"]'
	local edge_style = '[fillcolor="#a6cee3" color="#1f78b4"]'
	local ignored_node_style = '[shape="polygon" style="filled" fillcolor="#7d91a0" fontcolor="#ffffff"]'
	if type(filter) == "string" then
		local member = filter
		filter = function(cls) return rawget(cls, member) ~= nil end
	end
	local queue, seen, inherited = {class}, {}, {}
	while next(queue) do
		local curr = table.remove(queue, #queue)
		if not seen[curr] then
			local curr_cls = g_Classes[curr]
			local ignored = filter and not filter(curr_cls)
			local curr_style = ignored and ignored_node_style or node_style
			table.insert(dot, string.format('"%s" %s', curr, curr_style))
			local parents = curr_cls.__parents
			for i, parent in ipairs(parents) do
				if not unique_only or not inherited[parent] then
					inherited[parent] = true
					table.insert(dot, string.format('"%s" -> "%s" %s', curr, parent, edge_style))
					table.insert(queue, parent)
				end
			end
		end
	end
	table.insert(dot, "}")
	url = url .. EncodeURL(table.concat(dot, "\n"))
	html = string.format(html, url)
	local path = ConvertToOSPath("TmpData/ClassGraph.html")
	AsyncStringToFile(path, html)
	OpenUrl(path)
end