LUADOC - Farming Simulator 19

DebugUtil

Functions

debugTableToString

Description
useless on large tables
Definition
debugTableToString()
Code
349function DebugUtil.debugTableToString(inputTable, inputIndent, depth, maxDepth)
350 if depth > maxDepth then
351 return nil
352 end
353 local string1 = ""
354 for i,j in pairs(inputTable) do
355 string1 = string1 .. string.format("\n%s %s :: %s", inputIndent, tostring(i), tostring(j))
356
357 if type(j) == "table" then
358 local string2 = DebugUtil.debugTableToString(j, inputIndent .. " ", depth+1, maxDepth)
359 if string2 ~= nil then
360 string1 = string1 .. string2
361 end
362 end
363 end
364 return string1
365end

drawArea

Description
Definition
drawArea()
Code
310function DebugUtil.drawArea(area, r, g, b, a)
311 local x0,_,z0 = getWorldTranslation(area.start);
312 local x1,_,z1 = getWorldTranslation(area.width);
313 local x2,_,z2 = getWorldTranslation(area.height);
314 local x,z, widthX,widthZ, heightX,heightZ = MathUtil.getXZWidthAndHeight(x0, z0, x1, z1, x2, z2)
315 DebugUtil.drawDebugParallelogram(x,z, widthX,widthZ, heightX,heightZ, r, g, b, a);
316end

drawDebugArea

Description
Definition
drawDebugArea()
Code
40function DebugUtil.drawDebugArea(start, width, height, r,g,b, alignToGround, drawNodes, drawCircle)
41 local x,y,z = getWorldTranslation(start)
42 local x1,y1,z1 = getWorldTranslation(width)
43 local x2,y2,z2 = getWorldTranslation(height)
44
45 DebugUtil.drawDebugAreaRectangle(x,y,z, x1,y1,z1, x2,y2,z2, alignToGround, r,g,b)
46
47 if drawNodes == nil or drawNodes then
48 DebugUtil.drawDebugNode(start, getName(start), alignToGround)
49 DebugUtil.drawDebugNode(width, getName(width), alignToGround)
50 DebugUtil.drawDebugNode(height, getName(height), alignToGround)
51 end
52
53 local lsx, lsy, lsz, lex, ley, lez, radius = DensityMapHeightUtil.getLineByArea(start, width, height, 0.5)
54 if alignToGround then
55 lsy = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, lsx,0,lsz)+0.1
56 ley = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, lex,0,lez)+0.1
57 end
58
59 if drawCircle == nil or drawCircle then
60 drawDebugLine(lsx, lsy, lsz, 1,1,1, lex, ley, lez, 1,1,1)
61 DebugUtil.drawDebugCircle((lsx+lex)*0.5, (lsy+ley)*0.5, (lsz+lez)*0.5, radius, 20, nil)
62 end
63end

drawDebugAreaRectangle

Description
Definition
drawDebugAreaRectangle()
Code
67function DebugUtil.drawDebugAreaRectangle(x,y,z, x1,y1,z1, x2,y2,z2, alignToGround, r,g,b)
68 if alignToGround then
69 y = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, x,0,z)+0.1
70 y1 = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, x1,0,z1)+0.1
71 y2 = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, x2,0,z2)+0.1
72 end
73
74 drawDebugLine(x,y,z, r,g,b, x1, y1, z1, r,g,b)
75 drawDebugLine(x,y,z, r,g,b, x2, y2, z2, r,g,b)
76
77 local dirX1, dirY1, dirZ1 = x1-x, y1-y, z1-z
78 local dirX2, dirY2, dirZ2 = x2-x, y2-y, z2-z
79
80 drawDebugLine(x2,y2,z2, r,g,b, x2+dirX1, y2+dirY1, z2+dirZ1, r,g,b)
81 drawDebugLine(x1,y1,z1, r,g,b, x1+dirX2, y1+dirY2, z1+dirZ2, r,g,b)
82end

drawDebugCircle

Description
Draw debug circle
Definition
drawDebugCircle(float x, float y, float z, float radius, integer steps)
Arguments
floatxworld x position
floatyworld y position
floatzworld z position
floatradiusradius
integerstepssteps
Code
106function DebugUtil.drawDebugCircle(x,y,z, radius, steps, color)
107 for i=1,steps do
108 local a1 = ((i-1)/steps)*2*math.pi
109 local a2 = ((i)/steps)*2*math.pi
110
111 local c = math.cos(a1) * radius
112 local s = math.sin(a1) * radius
113 local x1, y1, z1 = x+c, y, z+s
114
115 local c = math.cos(a2) * radius
116 local s = math.sin(a2) * radius
117 local x2, y2, z2 = x+c, y, z+s
118
119 if color == nil then
120 drawDebugLine(x1,y1,z1, 1,0,0, x2,y2,z2, 1,0,0)
121 else
122 drawDebugLine(x1,y1,z1, color[1],color[2],color[3], x2,y2,z2, color[1],color[2],color[3])
123 end
124
125 end
126end

drawDebugCube

Description
Draw debug cube at given node
Definition
drawDebugCube(integer id, float sizeX, float sizeY, float sizeZ, float r, float g, float b)
Arguments
integeridnode id
floatsizeXx size
floatsizeYy size
floatsizeZz size
floatrred value
floatggreen value
floatbblue value
Code
174function DebugUtil.drawDebugCube(node, sizeX, sizeY, sizeZ, r, g, b)
175 sizeX, sizeY, sizeZ = sizeX * 0.5, sizeY * 0.5, sizeZ * 0.5
176
177 local x, y, z = getWorldTranslation(node)
178 local up1X, up1Y, up1Z = localDirectionToWorld(node, 1, 0, 0)
179 local upX, upY, upZ = localDirectionToWorld(node, 0, 1, 0)
180 local dirX, dirY, dirZ = localDirectionToWorld(node, 0, 0, 1)
181
182 up1X, up1Y, up1Z = up1X*sizeX, up1Y*sizeX, up1Z*sizeX
183 upX, upY, upZ = upX*sizeY, upY*sizeY, upZ*sizeY
184 dirX, dirY, dirZ = dirX*sizeZ, dirY*sizeZ, dirZ*sizeZ
185
186 drawDebugLine(x + up1X - dirX - upX, y + up1Y - dirY - upY, z + up1Z - dirZ - upZ, r, g, b, x + up1X - dirX + upX, y + up1Y - dirY + upY, z + up1Z - dirZ + upZ, r, g, b)
187 drawDebugLine(x - up1X - dirX - upX, y - up1Y - dirY - upY, z - up1Z - dirZ - upZ, r, g, b, x - up1X - dirX + upX, y - up1Y - dirY + upY, z - up1Z - dirZ + upZ, r, g, b)
188 drawDebugLine(x + up1X + dirX - upX, y + up1Y + dirY - upY, z + up1Z + dirZ - upZ, r, g, b, x + up1X + dirX + upX, y + up1Y + dirY + upY, z + up1Z + dirZ + upZ, r, g, b)
189 drawDebugLine(x - up1X + dirX - upX, y - up1Y + dirY - upY, z - up1Z + dirZ - upZ, r, g, b, x - up1X + dirX + upX, y - up1Y + dirY + upY, z - up1Z + dirZ + upZ, r, g, b)
190
191 drawDebugLine(x + up1X - dirX + upX, y + up1Y - dirY + upY, z + up1Z - dirZ + upZ, r, g, b, x - up1X - dirX + upX, y - up1Y - dirY + upY, z - up1Z - dirZ + upZ, r, g, b)
192 drawDebugLine(x - up1X - dirX + upX, y - up1Y - dirY + upY, z - up1Z - dirZ + upZ, r, g, b, x - up1X + dirX + upX, y - up1Y + dirY + upY, z - up1Z + dirZ + upZ, r, g, b)
193 drawDebugLine(x - up1X + dirX + upX, y - up1Y + dirY + upY, z - up1Z + dirZ + upZ, r, g, b, x + up1X + dirX + upX, y + up1Y + dirY + upY, z + up1Z + dirZ + upZ, r, g, b)
194 drawDebugLine(x + up1X + dirX + upX, y + up1Y + dirY + upY, z + up1Z + dirZ + upZ, r, g, b, x + up1X - dirX + upX, y + up1Y - dirY + upY, z + up1Z - dirZ + upZ, r, g, b)
195
196 drawDebugLine(x + up1X - dirX - upX, y + up1Y - dirY - upY, z + up1Z - dirZ - upZ, r, g, b, x - up1X - dirX - upX, y - up1Y - dirY - upY, z - up1Z - dirZ - upZ, r, g, b)
197 drawDebugLine(x - up1X - dirX - upX, y - up1Y - dirY - upY, z - up1Z - dirZ - upZ, r, g, b, x - up1X + dirX - upX, y - up1Y + dirY - upY, z - up1Z + dirZ - upZ, r, g, b)
198 drawDebugLine(x - up1X + dirX - upX, y - up1Y + dirY - upY, z - up1Z + dirZ - upZ, r, g, b, x + up1X + dirX - upX, y + up1Y + dirY - upY, z + up1Z + dirZ - upZ, r, g, b)
199 drawDebugLine(x + up1X + dirX - upX, y + up1Y + dirY - upY, z + up1Z + dirZ - upZ, r, g, b, x + up1X - dirX - upX, y + up1Y - dirY - upY, z + up1Z - dirZ - upZ, r, g, b)
200end

drawDebugCubeAtWorldPos

Description
Draw debug cube at world position
Definition
drawDebugCubeAtWorldPos(float x, float y, float z, float dirX, float dirY, float dirZ, float upX, float upY, float upZ, float sizeX, float sizeY, float sizeZ, float r, float g, float b)
Arguments
floatxworld x center position
floatyworld y center position
floatzworld z center position
floatdirXx direction
floatdirYy direction
floatdirZz direction
floatupXx up of vector
floatupYy up of vector
floatupZz up of vector
floatsizeXx size
floatsizeYy size
floatsizeZz size
floatrred value
floatggreen value
floatbblue value
Code
145function DebugUtil.drawDebugCubeAtWorldPos(x, y, z, dirX, dirY, dirZ, upX, upY, upZ, sizeX, sizeY, sizeZ, r, g, b)
146 local temp = createTransformGroup("temp_drawDebugCubeAtWorldPos")
147 link(getRootNode(), temp)
148 setTranslation(temp, x, y, z)
149 setDirection(temp, dirX, dirY, dirZ, upX, upY, upZ)
150 DebugUtil.drawDebugCube(temp, sizeX, sizeY, sizeZ, r, g, b)
151 delete(temp)
152end

drawDebugGizmoAtWorldPos

Description
Definition
drawDebugGizmoAtWorldPos()
Code
22function DebugUtil.drawDebugGizmoAtWorldPos(x,y,z, dirX, dirY, dirZ, upX, upY, upZ, text, alignToGround)
23 local sideX, sideY, sideZ = MathUtil.crossProduct(upX, upY, upZ, dirX, dirY, dirZ)
24
25 if alignToGround then
26 y = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, x,0,z)+0.1
27 end
28
29 drawDebugLine(x,y,z,1,0,0, x+sideX*0.3,y+sideY*0.3,z+sideZ*0.3, 1,0,0)
30 drawDebugLine(x,y,z,0,1,0, x+upX*0.3, y+upY*0.3, z+upZ*0.3, 0,1,0)
31 drawDebugLine(x,y,z,0,0,1, x+dirX*0.3, y+dirY*0.3, z+dirZ*0.3, 0,0,1)
32
33 if text ~= nil then
34 Utils.renderTextAtWorldPosition(x,y,z, tostring(text), getCorrectTextSize(0.012), 0)
35 end
36end

drawDebugNode

Description
Draws a debug node and optional a text at world position of given node
Definition
drawDebugNode(integer id, string text)
Arguments
integeridnode id
stringtexttext
Code
13function DebugUtil.drawDebugNode(node, text, alignToGround)
14 local x, y, z = getWorldTranslation(node)
15 local upX, upY, upZ = localDirectionToWorld(node, 0, 1, 0)
16 local dirX, dirY, dirZ = localDirectionToWorld(node, 0, 0, 1)
17 DebugUtil.drawDebugGizmoAtWorldPos(x,y,z, dirX, dirY, dirZ, upX, upY, upZ, text, alignToGround)
18end

drawDebugParallelogram

Description
Draw debug parallelogram
Definition
drawDebugParallelogram(float x, float z, float widthX, float widthZ, float heightX, float heightZ, float heightOffset, float r, float g, float b, float a)
Arguments
floatxworld x center position
floatzworld z center position
floatwidthXwidthX
floatwidthZwidthZ
floatheightXheightX
floatheightZheightZ
floatheightOffsetheightOffset
floatrred
floatggreen
floatbblue
floataalpha
Code
276function DebugUtil.drawDebugParallelogram(x,z, widthX,widthZ, heightX,heightZ, heightOffset, r,g,b,a)
277
278 local x0, z0 = x, z
279 local y0 = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, x0,0,z0) + heightOffset
280
281 local x1 = x0 + widthX
282 local z1 = z0 + widthZ
283 local y1 = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, x1,0,z1) + heightOffset
284
285 local x2 = x0 + heightX
286 local z2 = z0 + heightZ
287 local y2 = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, x2,0,z2) + heightOffset
288
289 local x3 = x0 + widthX + heightX
290 local z3 = z0 + widthZ + heightZ
291 local y3 = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, x3,0,z3) + heightOffset
292
293 drawDebugTriangle(x0,y0,z0, x1,y1,z1, x2,y2,z2, r,g,b,a, false)
294 drawDebugTriangle(x1,y1,z1, x3,y3,z3, x2,y2,z2, r,g,b,a, false)
295 -- and reverse order
296 drawDebugTriangle(x0,y0,z0, x2,y2,z2, x1,y1,z1, r,g,b,a, false)
297 drawDebugTriangle(x2,y2,z2, x3,y3,z3, x1,y1,z1, r,g,b,a, false)
298
299 drawDebugLine(x0,y0,z0, r,g,b, x1,y1,z1, r,g,b)
300 drawDebugLine(x1,y1,z1, r,g,b, x2,y2,z2, r,g,b)
301 drawDebugLine(x2,y2,z2, r,g,b, x0,y0,z0, r,g,b)
302
303 drawDebugLine(x1,y1,z1, r,g,b, x3,y3,z3, r,g,b)
304 drawDebugLine(x3,y3,z3, r,g,b, x2,y2,z2, r,g,b)
305
306end

drawDebugRectangle

Description
Definition
drawDebugRectangle()
Code
86function DebugUtil.drawDebugRectangle(node, minX, maxX, minZ, maxZ, yOffset, r, g, b)
87 local leftFrontX, leftFrontY, leftFrontZ = localToWorld(node, minX, yOffset, maxZ)
88 local rightFrontX, rightFrontY, rightFrontZ = localToWorld(node, maxX, yOffset, maxZ)
89
90 local leftBackX, leftBackY, leftBackZ = localToWorld(node, minX, yOffset, minZ)
91 local rightBackX, rightBackY, rightBackZ = localToWorld(node, maxX, yOffset, minZ)
92
93 drawDebugLine(leftFrontX, leftFrontY, leftFrontZ, r,g,b, rightFrontX, rightFrontY, rightFrontZ, r,g,b)
94 drawDebugLine(rightFrontX, rightFrontY, rightFrontZ, r,g,b, rightBackX, rightBackY, rightBackZ, r,g,b)
95 drawDebugLine(rightBackX, rightBackY, rightBackZ, r,g,b, leftBackX, leftBackY, leftBackZ, r,g,b)
96 drawDebugLine(leftBackX, leftBackY, leftBackZ, r,g,b, leftFrontX, leftFrontY, leftFrontZ, r,g,b)
97end

drawDebugReferenceAxis

Description
Draw debug reference axis
Definition
drawDebugReferenceAxis(float x, float y, float z, float up, float up, float up, float direction, float direction, float direction)
Arguments
floatxworld x center position
floatyworld y center position
floatzworld z center position
floatupx
floatupy
floatupz
floatdirectionx
floatdirectiony
floatdirectionz
Code
254function DebugUtil.drawDebugReferenceAxis(posX, posY, posZ, upX, upY, upZ, dirX, dirY, dirZ )
255 local sideX, sideY, sideZ = MathUtil.crossProduct(upX, upY, upZ, dirX, dirY, dirZ)
256 local length = 0.2
257
258 drawDebugLine((posX - sideX * length), (posY - sideY * length), (posZ - sideZ * length), 1, 1, 1, (posX + sideX * length), (posY + sideY * length), (posZ + sideZ * length), 1, 0, 0)
259 drawDebugLine((posX - upX * length), (posY - upY * length), (posZ - upZ * length), 1, 1, 1, (posX + upX * length), (posY + upY * length), (posZ + upZ * length), 0, 1, 0)
260 drawDebugLine((posX - dirX * length), (posY - dirY * length), (posZ - dirZ * length), 1, 1, 1, (posX + dirX * length), (posY + dirY * length), (posZ + dirZ * length), 0, 0, 1)
261end

drawDebugReferenceAxisFromNode

Description
Draw debug reference axis fom Node
Definition
drawDebugReferenceAxisFromNode(node to)
Arguments
nodetodraw a reference axis from
Code
233function DebugUtil.drawDebugReferenceAxisFromNode(node)
234 if node ~= nil then
235 local x, y, z = getWorldTranslation(node)
236 local yx, yy, yz = localDirectionToWorld(node, 0,1,0) -- up
237 local zx, zy, zz = localDirectionToWorld(node, 0,0,1) -- direction
238
239 DebugUtil.drawDebugReferenceAxis(x, y, z, yx, yy, yz, zx, zy, zz )
240 end
241end

drawOverlapBox

Description
Definition
drawOverlapBox()
Code
156function DebugUtil.drawOverlapBox(x, y, z, rotX, rotY, rotZ, extendX, extendY, extendZ, r, g, b)
157 local temp = createTransformGroup("temp_drawDebugCubeAtWorldPos")
158 link(getRootNode(), temp)
159 setTranslation(temp, x, y, z)
160 setRotation(temp, rotX, rotY, rotZ)
161 DebugUtil.drawDebugCube(temp, extendX*2, extendY*2, extendZ*2, r, g, b)
162 delete(temp)
163end

drawSimpleDebugCube

Description
Draw debug cube
Definition
drawSimpleDebugCube(float x, float y, float z, float width, float red, float green, float blue)
Arguments
floatxworld x center position
floatyworld y center position
floatzworld z center position
floatwidth
floatred
floatgreen
floatblue
Code
211function DebugUtil.drawSimpleDebugCube(x, y, z, width, r, g, b)
212 local halfWidth = width * 0.5
213
214 drawDebugLine(x - halfWidth, y - halfWidth, z - halfWidth, r, g, b, x + halfWidth, y - halfWidth, z - halfWidth, r, g, b)
215 drawDebugLine(x - halfWidth, y - halfWidth, z - halfWidth, r, g, b, x - halfWidth, y + halfWidth, z - halfWidth, r, g, b)
216 drawDebugLine(x - halfWidth, y - halfWidth, z - halfWidth, r, g, b, x - halfWidth, y - halfWidth, z + halfWidth, r, g, b)
217 drawDebugLine(x + halfWidth, y + halfWidth, z + halfWidth, r, g, b, x - halfWidth, y + halfWidth, z + halfWidth, r, g, b)
218 drawDebugLine(x + halfWidth, y + halfWidth, z + halfWidth, r, g, b, x + halfWidth, y - halfWidth, z + halfWidth, r, g, b)
219 drawDebugLine(x + halfWidth, y + halfWidth, z + halfWidth, r, g, b, x + halfWidth, y + halfWidth, z - halfWidth, r, g, b)
220 drawDebugLine(x - halfWidth, y - halfWidth, z + halfWidth, r, g, b, x + halfWidth, y - halfWidth, z + halfWidth, r, g, b)
221 drawDebugLine(x - halfWidth, y - halfWidth, z + halfWidth, r, g, b, x - halfWidth, y + halfWidth, z + halfWidth, r, g, b)
222 drawDebugLine(x - halfWidth, y + halfWidth, z - halfWidth, r, g, b, x - halfWidth, y + halfWidth, z + halfWidth, r, g, b)
223 drawDebugLine(x - halfWidth, y + halfWidth, z - halfWidth, r, g, b, x + halfWidth, y + halfWidth, z - halfWidth, r, g, b)
224 drawDebugLine(x + halfWidth, y - halfWidth, z - halfWidth, r, g, b, x + halfWidth, y + halfWidth, z - halfWidth, r, g, b)
225 drawDebugLine(x + halfWidth, y - halfWidth, z - halfWidth, r, g, b, x + halfWidth, y - halfWidth, z + halfWidth, r, g, b)
226 drawDebugPoint(x, y, z, r, g, b, 1)
227end

printCallingFunctionLocation

Description
Print the script call location of a function call which uses this function.
Definition
printCallingFunctionLocation()
Code
408function DebugUtil.printCallingFunctionLocation()
409 local stackLevel = 3 -- = <this>() + <warning / debug code> + <calling function>
410 local location = debug.getinfo(stackLevel, "Sl")
411 local callerScript = location.source
412 local callerLine = location.currentline
413
414 print(string.format("%s, line %d", tostring(callerScript), callerLine))
415end

printNodeHierarchy

Description
Definition
printNodeHierarchy()
Code
398function DebugUtil.printNodeHierarchy(node, offset)
399 offset = offset or ""
400 log(offset..getName(node))
401 for i=0, getNumOfChildren(node)-1 do
402 DebugUtil.printNodeHierarchy(getChildAt(node, i), offset.." ")
403 end
404end

printTableRecursively

Description
Print a table recursively
Definition
printTableRecursively(table inputTable, string inputIndent, integer depth, integer maxDepth)
Arguments
tableinputTabletable to print
stringinputIndentinput indent
integerdepthcurrent depth
integermaxDepthmax depth
Code
324function DebugUtil.printTableRecursively(inputTable, inputIndent, depth, maxDepth)
325 inputIndent = inputIndent or " "
326 depth = depth or 0
327 maxDepth = maxDepth or 3
328 if depth > maxDepth then
329 return
330 end
331 local debugString = ""
332 for i,j in pairs(inputTable) do
333 print(inputIndent..tostring(i).." :: "..tostring(j))
334 --debugString = debugString .. string.format("%s %s :: %s\n", inputIndent, tostring(i), tostring(j))
335 if type(j) == "table" then
336 DebugUtil.printTableRecursively(j, inputIndent.." ", depth+1, maxDepth)
337 --local string2 = DebugUtil.printTableRecursively(j, inputIndent.." ", depth+1, maxDepth)
338 --if string2 ~= nil then
339 -- debugString = debugString .. string2
340 --end
341 end
342 end
343 return debugString
344end

renderTable

Description
Definition
renderTable()
Code
369function DebugUtil.renderTable(posX, posY, textSize, data, nextColumnOffset)
370 local i = 0
371 setTextColor(1,1,1,1)
372 setTextBold(false)
373 textSize = getCorrectTextSize(textSize)
374 for _, valuePair in ipairs(data) do
375 if valuePair.name ~= "" then
376 local offset = i*textSize*1.05
377 setTextAlignment(RenderText.ALIGN_RIGHT)
378 renderText(posX, posY-offset, textSize, tostring(valuePair.name) .. ":")
379 setTextAlignment(RenderText.ALIGN_LEFT)
380 if type(valuePair.value) == "number" then
381 renderText(posX, posY-offset, textSize, " " ..string.format("%.4f", valuePair.value))
382 else
383 renderText(posX, posY-offset, textSize, " " ..tostring(valuePair.value))
384 end
385 end
386
387 i = i + 1
388
389 if valuePair.newColumn or valuePair.columnOffset then
390 i = 0
391 posX = posX + (valuePair.columnOffset or nextColumnOffset)
392 end
393 end
394end