From bbc12b20588cfb42eb3dfb27872ccc7ccbe620e6 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Wed, 23 Oct 2024 20:11:36 +0300 Subject: [PATCH] Using AI to improve code readability --- .../Presentations/Intellektika/khkdemo5.BAS | 314 +++++------------- 1 file changed, 83 insertions(+), 231 deletions(-) diff --git a/Graphics/Presentations/Intellektika/khkdemo5.BAS b/Graphics/Presentations/Intellektika/khkdemo5.BAS index dd8de8c..a41826f 100755 --- a/Graphics/Presentations/Intellektika/khkdemo5.BAS +++ b/Graphics/Presentations/Intellektika/khkdemo5.BAS @@ -1,15 +1,24 @@ +' Render animated 3D maze. +' By Svjatoslav Agejenko. +' Email: svjatoslav@svjatoslav.eu +' Homepage: http://www.svjatoslav.eu + +' Changelog: +' 2002, Initial version +' 2024, Improved program readability using AI + DECLARE FUNCTION getWord& (addr!) DECLARE FUNCTION getByte! (addr!) + ' Render animated 3D maze. ' By Svjatoslav Agejenko. ' Email: svjatoslav@svjatoslav.eu ' Homepage: http://www.svjatoslav.eu -' + ' Changelog: ' 2002, Initial version ' 2024.09, Improved program readability using AI - DECLARE SUB startText () DECLARE SUB control () DECLARE SUB putByte (addr!, dat!) @@ -17,81 +26,80 @@ DECLARE SUB putWord (addr!, dat!) DECLARE SUB start () DECLARE SUB animate () -DIM SHARED px(1 TO 5000) -DIM SHARED py(1 TO 5000) -DIM SHARED pz(1 TO 5000) -DIM SHARED rpx(1 TO 5000) -DIM SHARED rpy(1 TO 5000) -DIM SHARED rpe(1 TO 5000) - -DIM SHARED l1(1 TO 5000) -DIM SHARED l2(1 TO 5000) -DIM SHARED lc(1 TO 5000) +DIM SHARED pointX(1 TO 5000) +DIM SHARED pointY(1 TO 5000) +DIM SHARED pointZ(1 TO 5000) +DIM SHARED rotatedPointX(1 TO 5000) +DIM SHARED rotatedPointY(1 TO 5000) +DIM SHARED rotatedPointE(1 TO 5000) -DIM SHARED nl, np +DIM SHARED lineStartIndex(1 TO 5000) +DIM SHARED lineEndIndex(1 TO 5000) +DIM SHARED lineColor(1 TO 5000) -DIM SHARED an1, an2, an3 +DIM SHARED numberOfLines, numberOfPoints -DIM SHARED tim +DIM SHARED angleXZ, angleYZ -DIM SHARED extSEG, extADDR +DIM SHARED externalSegment, externalAddress -DIM SHARED myx, myy, myz -DIM SHARED myxs, myys, myzs -DIM SHARED buttL, buttR -DIM SHARED maxmove +DIM SHARED viewerX, viewerY, viewerZ +DIM SHARED buttonLeft, buttonRight +DIM SHARED maxMovement -nl = 0 -np = 0 +numberOfLines = 0 +numberOfPoints = 0 start -cx = 0 -cy = 0 -cz = 0 +mazeX = 0 +mazeY = 0 +mazeZ = 0 -np = 1 -px(1) = 0 -py(1) = 0 -pz(1) = 0 +numberOfPoints = 1 +pointX(1) = 0 +pointY(1) = 0 +pointZ(1) = 0 ' Main loop 1 frm = frm + 1 -myx = SIN(frm / 30) * 100 -myz = COS(frm / 59) * 100 -myy = SIN(frm / 300) -an1 = SIN(frm / 60) -an2 = SIN(frm / 36) / 3 - -np = np + 1 -px(np) = cx -py(np) = cy -pz(np) = cz - -nl = nl + 1 -l1(nl) = np -l2(nl) = np - 1 -lc(nl) = INT(RND * 15) + 1 -' lc(nl) = ABS(cx / 20) - -va = INT(RND * 3) - -SELECT CASE va +viewerX = SIN(frm / 30) * 100 +viewerZ = COS(frm / 59) * 100 +viewerY = SIN(frm / 300) +angleXZ = SIN(frm / 60) +angleYZ = SIN(frm / 36) / 3 + +numberOfPoints = numberOfPoints + 1 +pointX(numberOfPoints) = mazeX +pointY(numberOfPoints) = mazeY +pointZ(numberOfPoints) = mazeZ + +numberOfLines = numberOfLines + 1 +lineStartIndex(numberOfLines) = numberOfPoints +lineEndIndex(numberOfLines) = numberOfPoints - 1 +lineColor(numberOfLines) = INT(RND * 15) + 1 + + +' Pick random axis (dimension) where next segment of the maze should appear. +mazeGrowthDirection = INT(RND * 3) +SELECT CASE mazeGrowthDirection CASE 0 - cx = RND * 500 - 250 + mazeX = RND * 500 - 250 CASE 1 - cy = RND * 100 - 50 + mazeY = RND * 100 - 50 CASE 2 - cz = RND * 500 - 250 + mazeZ = RND * 500 - 250 END SELECT -' control animate PCOPY 0, 1 CLS +' Limit animation speed by making 0 Hz sound with fixed duration. +sound 0,1 + IF frm > 1200 THEN GOTO 200 GOTO 1 @@ -101,208 +109,52 @@ CHAIN "khkdemo6.bas" SUB animate -s1 = SIN(an1) -s2 = SIN(an2) -s3 = SIN(an3) +sinValue1 = SIN(angleXZ) +sinValue2 = SIN(angleYZ) -c1 = COS(an1) -c2 = COS(an2) -c3 = COS(an3) +cosValue1 = COS(angleXZ) +cosValue2 = COS(angleYZ) ' Rotate and project points -FOR a = 1 TO np - x = px(a) - myx - y = py(a) - myy - z = pz(a) - myz +FOR a = 1 TO numberOfPoints + x = pointX(a) - viewerX + y = pointY(a) - viewerY + z = pointZ(a) - viewerZ ' First rotation around Y axis - x1 = x * c1 + z * s1 - z1 = z * c1 - x * s1 + rotatedX1 = x * cosValue1 + z * sinValue1 + rotatedZ1 = z * cosValue1 - x * sinValue1 ' Second rotation around X axis - y1 = y * c2 + z1 * s2 - z2 = z1 * c2 - y * s2 + rotatedY1 = y * cosValue2 + rotatedZ1 * sinValue2 + rotatedZ2 = rotatedZ1 * cosValue2 - y * sinValue2 ' Check if point is behind the viewer - IF z2 > 3 THEN - rpe(a) = 1 - rpx(a) = x1 / z2 * 130 + 160 - rpy(a) = y1 / z2 * 130 + 100 + IF rotatedZ2 > 3 THEN + rotatedPointE(a) = 1 + rotatedPointX(a) = rotatedX1 / rotatedZ2 * 130 + 160 + rotatedPointY(a) = rotatedY1 / rotatedZ2 * 130 + 100 ELSE - rpe(a) = 0 + rotatedPointE(a) = 0 END IF NEXT a ' Draw lines between visible points -FOR a = 1 TO nl +FOR a = 1 TO numberOfLines - p1 = l1(a) - p2 = l2(a) - IF (rpe(p1) = 1) AND (rpe(p2) = 1) THEN LINE (rpx(p1), rpy(p1))-(rpx(p2), rpy(p2)), lc(a) + startIndex = lineStartIndex(a) + endIndex = lineEndIndex(a) + IF (rotatedPointE(startIndex) = 1) AND (rotatedPointE(endIndex) = 1) THEN LINE (rotatedPointX(startIndex), rotatedPointY(startIndex))-(rotatedPointX(endIndex), rotatedPointY(endIndex)), lineColor(a) NEXT a END SUB -SUB control - -IF getByte(8) <> 0 THEN - putByte 8, 0 - xp = getWord(2) - putWord 2, 0 - yp = getWord(4) - putWord 4, 0 - butt = getWord(6) - putWord 6, 0 - buttL = 0 - buttR = 0 - IF butt = 1 THEN buttL = 1 - IF butt = 2 THEN buttR = 1 - IF butt = 3 THEN buttL = 1: buttR = 1 - - ' Handle mouse movement - IF buttR = 1 THEN - IF buttL = 1 THEN - myxs = myxs + SIN(an1) * yp / 4 - myzs = myzs - COS(an1) * yp / 4 - GOTO 3 - END IF - myys = myys + yp / 4 -3 - yp = 0 - END IF - -END IF - -' Limit movement to maxmove -IF xp < -maxmove THEN xp = -maxmove -IF xp > maxmove THEN xp = maxmove -an1 = an1 - xp / 150 - -IF yp < -maxmove THEN yp = -maxmove -IF yp > maxmove THEN yp = maxmove -an2 = an2 - yp / 150 - -a$ = INKEY$ - -' Handle keyboard input -IF a$ = "a" THEN myxs = myxs - COS(an1): myzs = myzs - SIN(an1) -IF a$ = "d" THEN myxs = myxs + COS(an1): myzs = myzs + SIN(an1) -IF a$ = "w" THEN myxs = myxs - SIN(an1): myzs = myzs + COS(an1) -IF a$ = "s" THEN myxs = myxs + SIN(an1): myzs = myzs - COS(an1) -IF a$ = "q" THEN SYSTEM - -myxs = myxs / 1.1 -myys = myys / 1.1 -myzs = myzs / 1.1 - -myx = myx + myxs -myz = myz + myzs -myy = myy + myys - -END SUB - -FUNCTION getByte (addr) - getByte = PEEK(extADDR + addr) -END FUNCTION - -FUNCTION getWord& (addr) - a = PEEK(extADDR + addr) - b = PEEK(extADDR + addr + 1) - - c$ = HEX$(a) - IF LEN(c$) = 1 THEN c$ = "0" + c$ - IF LEN(c$) = 0 THEN c$ = "00" - - c = VAL("&H" + HEX$(b) + c$) - - getWord = c -END FUNCTION - -SUB mouseDemo - -cx = 150 -cy = 100 -maxmove = 50 - -' Main loop -100 -frm = frm + 1 - -LOCATE 1, 1 -PRINT cx, cy -PRINT frm - -CIRCLE (cx, cy), 10, 0 -xp = getWord(2) -putWord 2, 0 -yp = getWord(4) -putWord 4, 0 - -IF xp < -maxmove THEN xp = -maxmove -IF xp > maxmove THEN xp = maxmove -cx = cx + xp - -IF yp < -maxmove THEN yp = -maxmove -IF yp > maxmove THEN yp = maxmove -cy = cy + yp - -CIRCLE (cx, cy), 10, 10 - -SOUND 0, .05 -GOTO 100 - -END SUB - -SUB putByte (addr, dat) - POKE (extADDR + addr), dat -END SUB - -SUB putWord (addr, dat) - -b$ = HEX$(dat) - -2 -IF LEN(b$) < 4 THEN b$ = "0" + b$: GOTO 2 - -n1 = VAL("&H" + LEFT$(b$, 2)) -n2 = VAL("&H" + RIGHT$(b$, 2)) - -POKE (extADDR + addr), n2 -POKE (extADDR + addr + 1), n1 - -END SUB SUB start - ' startext SCREEN 7, , , 1 -maxmove = 50 +maxMovement = 50 END SUB - -SUB startText - -DEF SEG = 0 ' read first from interrupt table - -extSEG = PEEK(&H79 * 4 + 3) * 256 -extSEG = extSEG + PEEK(&H79 * 4 + 2) - -PRINT "Segment is: " + HEX$(extSEG) - -extADDR = PEEK(&H79 * 4 + 1) * 256 -extADDR = extADDR + PEEK(&H79 * 4 + 0) - -PRINT "relative address is:"; extADDR - -DEF SEG = extSEG - -IF getWord(0) <> 1983 THEN - PRINT "FATAL ERROR: you must load" - PRINT "QBasic extension TSR first!" - SYSTEM -END IF - -END SUB - -- 2.20.1