Modernized code for better readability master
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Thu, 18 Apr 2024 21:51:23 +0000 (00:51 +0300)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Thu, 18 Apr 2024 21:51:23 +0000 (00:51 +0300)
graphics/3D/stars.bas

index 7feee8a..dbd0701 100755 (executable)
-' 3D starfield\r
-' made by Svjatoslav Agejenko\r
-' in 2003.03\r
-' H-Page: svjatoslav.eu\r
-' E-Mail: svjatoslav@svjatoslav.eu\r
+' 3D Starfield Simulation\r
+' Originally made by Svjatoslav Agejenko in 2003.03\r
+' In 2024 code was modernized using artificial intelligence\r
+' Homepage: svjatoslav.eu\r
+' Email: svjatoslav@svjatoslav.eu\r
 \r
 \r
-DECLARE SUB setstar (x2!, y2!, z2!)\r
-DECLARE SUB galaxy ()\r
-Dim Shared mitu\r
-Dim Shared mituv\r
+DECLARE SUB AddStar (xPosition AS SINGLE, yPosition AS SINGLE, zPosition AS SINGLE)\r
+DECLARE SUB CreateGalaxy ()\r
 \r
 \r
+Dim Shared totalStars As Integer\r
+Dim Shared maxStars As Integer\r
 \r
 Randomize Timer\r
 \r
 Randomize Timer\r
-mituv = 2000\r
-mitu = mituv\r
-rns = 500\r
-wl = 0\r
+maxStars = 2000\r
+totalStars = maxStars\r
+starFieldDepth = 500\r
+\r
+Dim Shared starXPositions(1 To maxStars + 1000) As Single\r
+Dim Shared starYPositions(1 To maxStars + 1000) As Single\r
+Dim Shared starZPositions(1 To maxStars + 1000) As Single\r
+\r
+' Initialize the positions of the stars\r
+For starIndex = 1 To totalStars\r
+    starZPositions(starIndex) = Rnd * starFieldDepth + 20\r
+    angle = Rnd * 100\r
+    starXPositions(starIndex) = Sin(angle) * 20\r
+    starYPositions(starIndex) = Cos(angle) * 20\r
+Next starIndex\r
 \r
 \r
-Dim Shared px(1 To mitu + 1000)\r
-Dim Shared py(1 To mitu + 1000)\r
-Dim Shared pz(1 To mitu + 1000)\r
+Screen 13\r
 \r
 \r
-For a = 1 To mitu\r
-    pz(a) = Rnd * 500 + 20\r
-    n = Rnd * 100\r
-    px(a) = Sin(n) * 20\r
-    py(a) = Cos(n) * 20\r
-Next a\r
 \r
 \r
+Do\r
+\r
+    ' Calculate the camera's rotation and position offsets\r
+    frameCount = frameCount + 1\r
+    cameraRotation = (3.1412 / 2) + Sin(frameCount / 35) / 100 + Sin(frameCount / 21) / 100\r
+    rs1 = Sin(cameraRotation)\r
+    rc1 = Cos(cameraRotation)\r
+\r
+    ' Update and draw each star\r
+    For starIndex = 1 To totalStars\r
+        x = starXPositions(starIndex)\r
+        y = starYPositions(starIndex)\r
+        z = starZPositions(starIndex)\r
+\r
+        ' Project the star's 3D position onto the 2D screen\r
+        projectedX = (x / z) * 160 + 160\r
+        projectedY = (y / z) * 100 + 100\r
+        PSet (projectedX, projectedY), 0 ' Erase the previous position\r
+\r
+        ' Rotate the star's position around the camera\r
+        x5 = x * rs1 - y * rc1\r
+        y5 = x * rc1 + y * rs1\r
+\r
+        ' Update the star's position with camera movement\r
+        x = x5 + Sin(frameCount / 21) * 3\r
+        y = y5 + Sin(frameCount / 18) * 3\r
+\r
+        ' Move the star closer to the viewer and wrap around if too close\r
+        z = z - 3\r
+        If z < 10 Then\r
+            z = Rnd * 300 + 400\r
+            x = Rnd * 800 - 400\r
+            y = Rnd * 800 - 400\r
+        End If\r
+\r
+        ' Project the new position and draw with perspective-based brightness\r
+        projectedX = (x / z) * 160 + 160\r
+        projectedY = (y / z) * 100 + 100\r
+        colorCode = 3000 / z + 15\r
+        If colorCode > 31 Then colorCode = 31\r
+        PSet (projectedX, projectedY), colorCode\r
+\r
+        ' Update the star's array positions\r
+        starXPositions(starIndex) = x\r
+        starYPositions(starIndex) = y\r
+        starZPositions(starIndex) = z\r
+    Next starIndex\r
+\r
+    ' Add new stars to the galaxy if needed\r
+    If maxStars - totalStars > Rnd * 800 + 100 Then CreateGalaxy: totalStars = totalStars + 1\r
+\r
+    ' Remove the two farthest stars and replace them with new ones\r
+    For a = 1 To 2\r
+        starIndex = Int(Rnd * (totalStars - 10)) + 1\r
+        Swap starXPositions(totalStars), starXPositions(starIndex)\r
+        Swap starYPositions(totalStars), starYPositions(starIndex)\r
+        Swap starZPositions(totalStars), starZPositions(starIndex)\r
+\r
+        x = starXPositions(totalStars)\r
+        y = starYPositions(totalStars)\r
+        z = starZPositions(totalStars)\r
+        projectedX = (x / z) * 160 + 160\r
+        projectedY = (y / z) * 100 + 100\r
+        PSet (projectedX, projectedY), 0 ' Erase the star\r
+        totalStars = totalStars - 1\r
+    Next a\r
 \r
 \r
-Screen 13\r
 \r
 \r
+    ' Check for user input to exit the program\r
+    If InKey$ <> "" Then System\r
 \r
 \r
-frm = 10\r
-1\r
-fps = fps + 1\r
-If tm$ <> Time$ Then\r
-    'LOCATE 1, 1\r
-    'PRINT fps\r
-    If fps > 20 Then wl = wl + 2 Else wl = wl - 1\r
-    If wl < 0 Then wl = 0\r
-    fps = 0\r
-    tm$ = Time$\r
-End If\r
-frm = frm + 1\r
-xp = Sin(frm / 21) * 3\r
-yp = Sin(frm / 18) * 3\r
-\r
-nrk = (3.1412) / 2 + Sin(frm / 35) / 100 + Sin(frm / 21) / 100\r
-rs1 = Sin(nrk)\r
-rc1 = Cos(nrk)\r
-\r
-For a = 1 To mitu\r
-    x = px(a)\r
-    y = py(a)\r
-    z = pz(a)\r
-    x1 = x / z * 160 + 160\r
-    y1 = y / z * 100 + 100\r
-    PSet (x1, y1), 0\r
-\r
-    x5 = x * rs1 - y * rc1\r
-    y5 = x * rc1 + y * rs1\r
-\r
-    x = x5\r
-    y = y5\r
-\r
-    z = z - 3\r
-    x = x + xp\r
-    y = y + yp\r
-    If z < 10 Then\r
-        z = Rnd * 300 + 400\r
-        x = Rnd * 800 - 400\r
-        y = Rnd * 800 - 400\r
-    End If\r
-\r
-    x1 = x / z * 160 + 160\r
-    y1 = y / z * 100 + 100\r
-    c = 3000 / z + 15\r
-    If c > 31 Then c = 31\r
-    PSet (x1, y1), c\r
-\r
-    px(a) = x\r
-    py(a) = y\r
-    pz(a) = z\r
-Next a\r
-\r
-\r
-If mituv - mitu > rns Then galaxy: rns = Rnd * 800 + 100\r
-\r
-For a = 1 To 2\r
-    b = Rnd * (mitu - 10) + 1\r
-    Swap px(mitu), px(b)\r
-    Swap py(mitu), py(b)\r
-    Swap pz(mitu), pz(b)\r
-\r
-    x = px(mitu)\r
-    y = py(mitu)\r
-    z = pz(mitu)\r
-    x1 = x / z * 160 + 160\r
-    y1 = y / z * 100 + 100\r
-    PSet (x1, y1), 0\r
-    mitu = mitu - 1\r
-Next a\r
-\r
-'LOCATE 2, 1\r
-'PRINT wl\r
-For a = 0 To wl\r
-    For b = 0 To 1000\r
-        c = c / 100\r
-    Next b\r
-Next a\r
-\r
-If InKey$ <> "" Then System\r
-sound 0,1\r
-GoTo 1\r
-\r
-Sub galaxy\r
-\r
-    xf = Rnd * 4 - 2\r
-    yf = Rnd * 4 - 2\r
-    xp = Rnd * 200 - 100\r
-    yp = Rnd * 200 - 100\r
-\r
-    For a = 1 To Rnd * 15 + 10 Step .04\r
-        x = Sin(a) * a * a / 10\r
-        y = Cos(a) * a * a / 10\r
-        setstar x + Rnd * a * a / 30 + xp, y + Rnd * a * a / 30 + yp, 700 + Rnd * a * a / 30 + (x * xf) + (y * yf)\r
-    Next a\r
+    ' sleep, to limit framerate\r
+    Sound 0, 1\r
+Loop\r
+\r
+' Subroutine to create a new galaxy of stars\r
+Sub CreateGalaxy\r
+    xForce = Rnd * 4 - 2\r
+    yForce = Rnd * 4 - 2\r
+    xPositionOffset = Rnd * 200 - 100\r
+    yPositionOffset = Rnd * 200 - 100\r
+\r
+    ' Add a new set of stars with varying positions and velocities\r
+    For starIndex = 1 To Int(Rnd * 15) + 10 Step .04\r
+        x = Sin(starIndex) * starIndex * starIndex / 10\r
+        y = Cos(starIndex) * starIndex * starIndex / 10\r
+        AddStar x + RND * starIndex * starIndex / 30 + xPositionOffset, _\r
+                   y + RND * starIndex * starIndex / 30 + yPositionOffset, _\r
+                   700 + RND * starIndex * starIndex / 30 + (x * xForce) + (y * yForce)\r
+    Next starIndex\r
 \r
 \r
-    'SOUND 1000, 1\r
+    ' Play a sound when creating new stars (commented out)\r
+    ' SOUND 1000, 1\r
 End Sub\r
 \r
 End Sub\r
 \r
-Sub setstar (x2, y2, z2)\r
-    mitu = mitu + 1\r
-    s = mitu\r
+' Subroutine to add a new star at the specified position\r
+Sub AddStar (xPosition As Single, yPosition As Single, zPosition As Single)\r
+    totalStars = totalStars + 1\r
+    starIndex = totalStars\r
 \r
 \r
-    px(s) = x2\r
-    py(s) = y2\r
-    pz(s) = z2\r
+    starXPositions(starIndex) = xPosition\r
+    starYPositions(starIndex) = yPosition\r
+    starZPositions(starIndex) = zPosition\r
 End Sub\r
 \r
 End Sub\r
 \r