Improve code readability
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Thu, 27 Feb 2025 20:04:48 +0000 (22:04 +0200)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Thu, 27 Feb 2025 20:04:48 +0000 (22:04 +0200)
Graphics/Presentations/AI/AI demo.bas

index 4bc6711..16aa99c 100644 (file)
@@ -6,7 +6,7 @@ DECLARE SUB Scene3 ()
 DECLARE SUB Scene4 ()\r
 DECLARE SUB Scene5 ()\r
 DECLARE SUB Scene7 ()\r
-DECLARE SUB start ()\r
+DECLARE SUB Initialize ()           ' Renamed from "start" for clarity\r
 DECLARE SUB sc1 ()\r
 DECLARE SUB sc2 ()\r
 DECLARE SUB sc3 ()\r
@@ -17,8 +17,8 @@ DECLARE SUB sc7 ()
 DECLARE SUB sc8 ()\r
 DECLARE SUB sc10 ()\r
 DECLARE SUB sc9 ()\r
-DECLARE SUB box (x1!, y1!, x2!, y2!)\r
-DECLARE SUB calc (tx1!, ty1!, tz1!, tx2!, ty2!, tz2!, tx3!, ty3!, tz3!, c!)\r
+DECLARE SUB DrawRoundedBox (x1!, y1!, x2!, y2!)   ' Renamed from "box" for clarity\r
+DECLARE SUB ComputeShadeValue (tx1!, ty1!, tz1!, tx2!, ty2!, tz2!, tx3!, ty3!, tz3!, c!)   ' Renamed from "calc"\r
 DECLARE SUB GetAngle (x1!, y1!, x2!, y2!, N!)\r
 DECLARE SUB RotatePoint (zx!, zy!, x1!, y1!, N!)\r
 DECLARE SUB FillPolygon (xCoord1!, yCoord1!, xCoord2!, yCoord2!, xCoord3!, yCoord3!, colorVal!)\r
@@ -39,7 +39,7 @@ DIM SHARED pi
 DIM SHARED angl1, angl2\r
 DIM SHARED font(0 TO 7, 0 TO 7, 32 TO 150)\r
 \r
-start\r
+Initialize\r
 \r
 Scene1\r
 Scene2\r
@@ -50,32 +50,7 @@ Scene7
 Scene8\r
 Scene9\r
 \r
-SUB box (x1, y1, x2, y2)\r
-' Draws a soft-edged rectangular box by blending pixel colors around the edges.\r
-' The edges fade gently to create a smoothed border.\r
-\r
-    FOR y = y1 TO y2\r
-        s = 10\r
-        IF y - y1 <= 10 THEN\r
-            s = (SQR((20 - (y - y1)) * (y - y1)))\r
-        END IF\r
-\r
-        IF y2 - y <= 10 THEN\r
-            s = (SQR((20 - (y2 - y)) * (y2 - y)))\r
-        END IF\r
-\r
-        FOR x = x1 - s TO x2 + s\r
-            c = POINT(x, y)\r
-            IF c <= 127 THEN\r
-                c = c + 127\r
-                IF c > 245 THEN c = 245\r
-                PSET (x, y), c\r
-            END IF\r
-        NEXT x\r
-    NEXT y\r
-END SUB\r
-\r
-SUB calc (tx1, ty1, tz1, tx2, ty2, tz2, tx3, ty3, tz3, c)\r
+SUB ComputeShadeValue (tx1, ty1, tz1, tx2, ty2, tz2, tx3, ty3, tz3, c)\r
 ' Computes a value (c) based on the positions of three 3D points.\r
 ' It rotates these points around multiple axes, then calculates\r
 ' an offset 'a' used to determine a final computed result in 'c'.\r
@@ -124,6 +99,32 @@ SUB calc (tx1, ty1, tz1, tx2, ty2, tz2, tx3, ty3, tz3, c)
     c = 49 - a\r
     IF c < 0 THEN c = 0\r
 END SUB\r
+\r
+SUB DrawRoundedBox (x1, y1, x2, y2)\r
+' Draws a soft-edged rectangular box by blending pixel colors around the edges.\r
+' The edges fade gently to create a smoothed border.\r
+\r
+    FOR y = y1 TO y2\r
+        s = 10\r
+        IF y - y1 <= 10 THEN\r
+            s = (SQR((20 - (y - y1)) * (y - y1)))  ' fade calculation\r
+        END IF\r
+\r
+        IF y2 - y <= 10 THEN\r
+            s = (SQR((20 - (y2 - y)) * (y2 - y)))  ' Similar fade near the lower boundary\r
+        END IF\r
+\r
+        FOR x = x1 - s TO x2 + s\r
+            c = POINT(x, y)\r
+            IF c <= 127 THEN\r
+                c = c + 127\r
+                IF c > 245 THEN c = 245\r
+                PSET (x, y), c\r
+            END IF\r
+        NEXT x\r
+    NEXT y\r
+END SUB\r
+\r
 '-------------------------------------------------------------------------------\r
 SUB FillPolygon (xCoord1, yCoord1, xCoord2, yCoord2, xCoord3, yCoord3, colorVal)\r
 ' Fills a triangular area by connecting the edges.\r
@@ -173,8 +174,9 @@ mkl:
     NEXT y\r
     RETURN\r
 \r
-1:\r
+1 :\r
 END SUB\r
+\r
 '-------------------------------------------------------------------------------\r
 SUB GetAngle (x1, y1, x2, y2, N)\r
 ' Determines the angle (N) between two points (x1,y1) and (x2,y2).\r
@@ -207,8 +209,21 @@ SUB GetAngle (x1, y1, x2, y2, N)
         END IF\r
     END IF\r
 \r
-2:\r
+2 :\r
+END SUB\r
+\r
+'-------------------------------------------------------------------------------\r
+SUB Initialize\r
+' Initializes screen mode, sets up global constants pi and pii,\r
+' and captures system font data for later use in PrintText.\r
+\r
+    SCREEN 13\r
+    pi = 3.141592\r
+    pii = pi\r
+    fac = 360 / (pi * 2)\r
+    InitializeFont\r
 END SUB\r
+\r
 '-------------------------------------------------------------------------------\r
 SUB InitializeFont\r
 ' Captures the current text font into an array for later use\r
@@ -226,14 +241,9 @@ SUB InitializeFont
         NEXT y\r
     NEXT a\r
 END SUB\r
-'-------------------------------------------------------------------------------\r
-SUB WaitForInput\r
-' Reads exactly one character from the keyboard and stores it in a$.\r
 \r
-    a$ = INPUT$(1)\r
-END SUB\r
-'-------------------------------------------------------------------------------\r
 DEFINT A-Z\r
+'-------------------------------------------------------------------------------\r
 SUB MakeBackground\r
 ' Creates a fractal-like background by iteratively sampling and\r
 ' perturbing pixel values, producing a terrain effect.\r
@@ -265,7 +275,7 @@ SUB MakeBackground
     lm = 127\r
 \r
     s = 2 ^ 8\r
-5:\r
+5 :\r
     s = s \ 2\r
     x2 = (319 \ s) - 1\r
     y2 = (199 \ s) - 1\r
@@ -306,8 +316,9 @@ SUB MakeBackground
     IF s > 2 THEN GOTO 5\r
 \r
 END SUB\r
-'-------------------------------------------------------------------------------\r
+\r
 DEFSNG A-Z\r
+'-------------------------------------------------------------------------------\r
 SUB PrintText (x, y, s, c, a$)\r
 ' Prints text at a specified (x,y) position on the screen, by reading\r
 ' from the 'font' array captured in InitializeFont. Allows for scaling = 1.\r
@@ -323,11 +334,12 @@ SUB PrintText (x, y, s, c, a$)
                     IF c1 > 0 THEN PSET (x1 + x2, y1 + y), c\r
                 NEXT x1\r
             NEXT y1\r
-7:\r
+7 :\r
             x2 = x2 + 8\r
         NEXT a\r
     END IF\r
 END SUB\r
+\r
 '-------------------------------------------------------------------------------\r
 SUB RotatePoint (zx, zy, x1, y1, N)\r
 ' Rotates a point (x1,y1) around a center (zx,zy) by angle N (in radians).\r
@@ -340,6 +352,7 @@ SUB RotatePoint (zx, zy, x1, y1, N)
     x1 = x2 * s1 - y2 * c1 + zx\r
     y1 = x2 * c1 + y2 * s1 + zy\r
 END SUB\r
+\r
 '-------------------------------------------------------------------------------\r
 SUB Scene1\r
 ' Initializes palette and reads 3D data from "data.dat" for a rotating\r
@@ -380,7 +393,7 @@ SUB Scene1
     SetPalette 40, 40, 40, 254\r
     COLOR 254\r
     LOCATE 2, 11\r
-    PRINT "�ks hetk"\r
+    PRINT "One moment"\r
 \r
     OPEN "data.dat" FOR INPUT AS #1\r
     INPUT #1, a\r
@@ -409,15 +422,15 @@ SUB Scene1
         nlin2! = l3!\r
         GOSUB addlin\r
         LOCATE 4, 10\r
-        PRINT STR$(INT(a / (inpo - 1) * 100)) + "% valmis"\r
+        PRINT STR$(INT(a / (inpo - 1) * 100)) + "% ready"\r
     NEXT a\r
     CLOSE 1\r
     CLS\r
 \r
-3:\r
+3 :\r
     tim = tim + 1\r
-    sj$ = INKEY$\r
-    IF sj$ = "q" THEN END\r
+    quitKey$ = INKEY$\r
+    IF quitKey$ = "q" THEN END\r
 \r
     a = COS(tim / 25)\r
     an1 = COS(tim / 29) * a\r
@@ -485,10 +498,11 @@ addlin:
     lin2!(nl) = nlin2!\r
     RETURN\r
 \r
-4:\r
+4 :\r
     angl1 = an1\r
     angl2 = an2\r
 END SUB\r
+\r
 '-------------------------------------------------------------------------------\r
 SUB Scene2\r
 ' Loads 3D data from "data.dat" and projects triangular faces to 2D.\r
@@ -587,10 +601,11 @@ SUB Scene2
         pol3(sun) = pol3(e)\r
         e = e - 1\r
 \r
-        calc rpx(p1), rpy(p1), rpz(p1), rpx(p2), rpy(p2), rpz(p2), rpx(p3), rpy(p3), rpz(p3), d\r
+        ComputeShadeValue rpx(p1), rpy(p1), rpz(p1), rpx(p2), rpy(p2), rpz(p2), rpx(p3), rpy(p3), rpz(p3), d\r
         FillPolygon INT(rpx(p1)), INT(rpy(p1)), INT(rpx(p2)), INT(rpy(p2)), INT(rpx(p3)), INT(rpy(p3)), INT(d)\r
     NEXT a\r
 END SUB\r
+\r
 '-------------------------------------------------------------------------------\r
 SUB Scene3\r
 ' Demonstrates some simple raster effects and wave patterns,\r
@@ -662,6 +677,7 @@ SUB Scene3
 \r
     WaitForInput\r
 END SUB\r
+\r
 '-------------------------------------------------------------------------------\r
 SUB Scene4\r
 ' Uses MakeBackground to generate a fractal backdrop, then draws a box\r
@@ -670,7 +686,7 @@ SUB Scene4
     RANDOMIZE 1\r
     MakeBackground\r
 \r
-    box 30, 50, 290, 150\r
+    DrawRoundedBox 30, 50, 290, 150\r
 \r
     SetPalette 32, 64, 32, 250\r
     y = 0\r
@@ -684,6 +700,7 @@ SUB Scene4
 \r
     WaitForInput\r
 END SUB\r
+\r
 '-------------------------------------------------------------------------------\r
 SUB Scene5\r
 ' Again uses MakeBackground, draws a box, and prints text describing\r
@@ -692,7 +709,7 @@ SUB Scene5
     RANDOMIZE 4\r
     MakeBackground\r
 \r
-    box 30, 50, 290, 150\r
+    DrawRoundedBox 30, 50, 290, 150   ' Renamed call\r
 \r
     SetPalette 32, 64, 32, 250\r
     y = -8\r
@@ -710,6 +727,7 @@ SUB Scene5
 \r
     WaitForInput\r
 END SUB\r
+\r
 '-------------------------------------------------------------------------------\r
 SUB Scene7\r
 ' Simple transition effect: horizontal lines are drawn across the screen\r
@@ -723,6 +741,7 @@ SUB Scene7
         SOUND 0, .5\r
     NEXT a\r
 END SUB\r
+\r
 '-------------------------------------------------------------------------------\r
 SUB Scene8\r
 ' A more complex 3D-like demo. It generates fractal terrain in the background,\r
@@ -777,7 +796,7 @@ SUB Scene8
     RANDOMIZE 100\r
     s = 64\r
 \r
-14:\r
+14 :\r
     sp = s / 2\r
     FOR y = 0 TO 100 STEP s\r
         FOR x = 0 TO 100 STEP s\r
@@ -847,7 +866,7 @@ SUB Scene8
     b = 0\r
     hlkin = np + 1\r
 \r
-15:\r
+15 :\r
     INPUT #1, x, y, z\r
     IF x = 999 THEN GOTO 16\r
     a = a + 1\r
@@ -856,7 +875,7 @@ SUB Scene8
     hlkz(a) = z\r
     GOTO 15\r
 \r
-16:\r
+16 :\r
     INPUT #1, x, y\r
     IF x = 999 THEN GOTO 17\r
     nl = nl + 1\r
@@ -865,7 +884,7 @@ SUB Scene8
     linc(nl) = 2\r
     GOTO 16\r
 \r
-17:\r
+17 :\r
     CLOSE #1\r
     np = np + a\r
     hlknu = a\r
@@ -934,7 +953,7 @@ SUB Scene8
         nl = nl + 8\r
     NEXT a\r
 \r
-10:\r
+10 :\r
     SOUND 0, 1\r
     IF INKEY$ <> "" THEN miin = 1\r
     IF miin > 150 THEN GOTO 13\r
@@ -1043,7 +1062,7 @@ SUB Scene8
 \r
         px1(a) = x1 + 160\r
         py1(a) = y1 + 80\r
-11:\r
+11 :\r
     NEXT a\r
 \r
     FOR a = 1 TO nl\r
@@ -1060,12 +1079,13 @@ SUB Scene8
         lby1(a) = y1\r
         lbx2(a) = x2\r
         lby2(a) = y2\r
-12:\r
+12 :\r
     NEXT a\r
     IF kau > 200 THEN kau = kau - 10\r
     IF tim < 28000 THEN GOTO 10\r
-13:\r
+13 :\r
 END SUB\r
+\r
 '-------------------------------------------------------------------------------\r
 SUB Scene9\r
 ' Wrap-up scene that shows a background, draws a box, and prints\r
@@ -1074,13 +1094,14 @@ SUB Scene9
     RANDOMIZE 45\r
     MakeBackground\r
 \r
-    box 30, 50, 290, 80\r
+    DrawRoundedBox 30, 50, 290, 80\r
 \r
     SetPalette 32, 64, 32, 250\r
     y = -8\r
     PrintText 30, 70 + y, 1, 250, "     Thank you for attention!"\r
     WaitForInput\r
 END SUB\r
+\r
 '-------------------------------------------------------------------------------\r
 SUB SetPalette (r, g, b, c)\r
 ' Sets a palette entry (c) to the specified (r,g,b) values, each 0..63 range.\r
@@ -1097,15 +1118,11 @@ SUB SetPalette (r, g, b, c)
     OUT &H3C9, g\r
     OUT &H3C9, b\r
 END SUB\r
+\r
 '-------------------------------------------------------------------------------\r
-SUB start\r
-' Initializes screen mode, sets up global constants pi and pii,\r
-' and captures system font data for later use in PrintText.\r
+SUB WaitForInput\r
+' Reads exactly one character from the keyboard and stores it in inputKey$.\r
 \r
-    SCREEN 13\r
-    pi = 3.141592\r
-    pii = pi\r
-    fac = 360 / (pi * 2)\r
-    InitializeFont\r
+    inputKey$ = INPUT$(1)\r
 END SUB\r
-'-------------------------------------------------------------------------------\r
+\r