November 28, 2020, 02:23:13 AM

Author Topic: [bb] 2D turret firing algorhytms by Matt Merkulov [ 1+ years ago ]  (Read 580 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : 2D turret firing algorhytms
Author : Matt Merkulov
Posted : 1+ years ago

Description : From article:<a href="http://blitzetcetera.org/index.php/Стрельба_на_опережение" target="_blank"> Forestalling[/url] (rus)

Code :
Code: BlitzBasic
  1. 'Turret firing algorhytms including forestalling and self-training AI by Matt Merkulov
  2.  
  3. Graphics3D 800, 600
  4.  
  5. PositionEntity CreateCamera(), 0, 0, -20
  6. RotateEntity CreateLight(), 45, 45, 45
  7.  
  8. ; Type for a turret - coordinates, angle, mode of shooting, handles for 3D - objects, variables for AI
  9. Type turret
  10.         Field x#, y#, Mode, head, barrel, ang#, dir#, dx#, dy#
  11. End Type
  12.  
  13. ; Type for a shot - coordinates, increments, handle of 3D - object,
  14. ; Distance from a turret, auxiliary variables for AI
  15. Type shot
  16.         Field x#, y#, dx#, dy#, h, r#, tx#, ty#, dir#, t.turret
  17. End Type
  18.  
  19. Const aimax = 200, aistp# = .2
  20. Dim aiang# (aimax)
  21.  
  22. SetFont LoadFont("arial", 14)
  23. SetBuffer BackBuffer()
  24.  
  25. ; Constants - the period of shooting, speed of shells, speed of the player
  26. Const frq = 200, v0# = .01, vp# = .005
  27. ; Const frq = 200, v0# = .2, vp# = .1
  28. Const bs# = 2
  29.  
  30. Global x#, y#
  31.  
  32. ; createturret 1:createturret 2:createturret 3:createturret 4
  33. createturret 5:createturret 4:createturret 2
  34.  
  35.  
  36. ; Creation of the player
  37. p = CreateSphere(16)
  38. h = CreateSphere(8, p)
  39. PositionEntity h, 1, 0, 0
  40. ScaleEntity h, .4, .4, .4
  41.  
  42. Repeat
  43.         tim = MilliSecs()
  44.  
  45.         For t.turret = Each turret
  46.  
  47.                 ; Auxiliary variables + targetting on the player by default
  48.                 dx# = x# - tx
  49.                 dy# = y# - ty
  50.                 cang# = ATan2(dy#, dx)
  51.                 tdx# = dx#
  52.                 tdy# = dy#
  53.                 tdir# = cang#
  54.                 pr# = Sqr(dx# * dx# + dy# * dy#)
  55.  
  56.                 ; Modes of an aiming of turrets:
  57.                 Select tMode
  58.                         Case 0; shooting in the given direction
  59.                                 cang = 0
  60.                         Case 1; shooting with rotation
  61.                                 cang# = tang# + .05 * dt#
  62.                         Case 2; exact targetting on object
  63.  
  64.                         Case 3; shooting by a fan
  65.                                 cang# = cang# + Sin(dcang#) * 30
  66.                                 dcang# = dcang# + 2
  67.                         Case 4; shooting on an forestalling
  68.                                 ; Calculation of factors of a quadratic
  69.                                 a# = v# * v# - v0# * v0#
  70.                                 b# = 2.0 * v# * (dx# * Cos(ang#) + dy# * Sin(ang#) -bs#)
  71.                                 c# = dx# * dx# + dy# * dy# - bs# * bs#
  72.                                 ; Calculation of a discriminant
  73.                                 d# = b# * b# - 4.0 * a# * c#
  74.                                 If d# >= 0 Then
  75.                                         ; Calculation of the moment of a meeting of a shell and the player
  76.                                         t1# = (-b# + Sqr(d#)) / 2.0 / a#
  77.                                         t2# = (-b# - Sqr(d#)) / 2.0 / a#
  78.                                         If t2# > 0 Then t1# = t2#
  79.                                         If t1# >= 0 Then
  80.                                                 ; Calculation of a angle through coordinates of object during the found moment of time
  81.                                                 cang# = ATan2(y# + t1# * v# * Sin(ang#) - ty#, x# + t1# * v# * Cos(ang#) - tx#)
  82.                                         End If
  83.                                 End If
  84.                         Case 5; a self - training turret (the amendment to a angle undertakes from a file)
  85.                                 cang# = cang# + aiang(Int(pr# * aistp#))
  86.                 End Select
  87.  
  88.                 ; Turn of a turret
  89.                 rotateturret t, cang#
  90.                 ; Return of a barrel on former position
  91.                 If EntityX(tarrel) < .75 Then MoveEntity tarrel, 0, - .25 / frq * dt#, 0
  92.        
  93.                 ; A shot and displacement of a barrel inside of a turret
  94.                 If nextshot <= tim Then
  95.                         fire t
  96.                         PositionEntity tarrel, .5, 0, 0
  97.                 End If
  98.         Next
  99.  
  100.         ; Delay between shots
  101.         If nextshot <= tim Then nextshot = tim + frq
  102.  
  103.         For s.shot = Each shot
  104.                 ; Moving bullets
  105.                 sx# = sx# + sdx# * dt#
  106.                 sy# = sy# + sdy# * dt#
  107.                 s
  108. # = s
  109. # + v0# * dt#
  110.                 PositionEntity sh, sx#, sy#, 0
  111.                 ; If distances from a turret up to a bullet and up to the player are equal,
  112.                 If pr# <= s
  113. # And sdir# < 999 Then
  114.                         ; The adjusting angle for the given distance is calculated
  115.                         Newang# = s     dir# - sdir#
  116.                         ; Squeezes into borders (-180, 180)
  117.                         Newang# = Newang# - Floor((Newang# + 180.0) / 360.0) * 360.0
  118.                         ; Also storing it in a file,
  119.                         aiang(Int(pr# * aistp#)) = Newang#
  120.                         ;(A next line - for not to store this bullet in a file once again)
  121.                         sdir# = 999
  122.                 End If
  123.                 If(sx# - x#) ^ 2 + (sy# - y#) ^ 2 < 1 Then
  124.                         FreeEntity sh: Delete s
  125.                         red = 255
  126.                         hits# = hits# + 1
  127.                 Else
  128.                         ; Removing the bullets which have overstepped the bounds of the screen
  129.                         If Abs(sx#) + Abs(sy#) > 40 Then FreeEntity sh:Delete s
  130.                 End If
  131.         Next
  132.  
  133.         ; Indication of hit
  134.         If red > 0 Then
  135.                 red = red - Int(dt / 3)
  136.         Else
  137.                 red = 0
  138.         End If
  139.         EntityColor p, 255, 255 - red, 255 - red
  140.  
  141.         ; Moving and turn of the player (speed is calculated depending on last time)
  142.         PositionEntity p, x#, y#, 0
  143.         RotateEntity p, 0, 0, ang#
  144.         v# = (KeyDown(200) - KeyDown(208)) * vp#
  145.         ang# = ang# + .3 * dt# * (KeyDown(203) - KeyDown(205))
  146.         x# = x# + v# * Cos(ang#) * dt#
  147.         y# = y# + v# * Sin(ang#) * dt#
  148.        
  149.         RenderWorld
  150.         ; Counter
  151.         Text 0, 0, "Hits / sec:" + (hits# / sec#)
  152.         Flip
  153.         ; Time, spent for this cycle (fractional as it is multiplied by fractional values)
  154.         dt# = MilliSecs() - tim
  155.         sec# = sec# + 0.001 * dt#
  156. Until KeyHit(1)
  157.  
  158. Function createturret(Mode)
  159.         ; Creation of a head part
  160.         t.turret = New turret
  161.         thead = CreateCylinder(6)
  162.         ScaleMesh thead, 1, .5, 1
  163.         RotateMesh thead, 90, 0, 0
  164.         ; A barrel created separately, but to adhere to a head part
  165.         tarrel = CreateCylinder(16, False)
  166.         RotateEntity tarrel, 0, 0, 90
  167.         ScaleEntity tarrel, .2, 1, .2
  168.         PositionEntity tarrel, .75, 0, 0
  169.         h = CreateCylinder(16, True)
  170.         ScaleEntity h, .3, .3, .3
  171.         RotateEntity h, 0, 0, 90
  172.         PositionEntity h, 2, 0, 0
  173.         EntityParent tarrel, thead
  174.         EntityParent h, tarrel
  175.         ; Setting random coordinates of a turret
  176.         tx# = Rnd(-20, 20)
  177.         ty# = Rnd(-20, 20)
  178.         PositionEntity thead, tx#, ty#, 0
  179.         tMode = Mode
  180. End Function
  181.  
  182. Function rotateturret(t.turret, ang#)
  183.         tang# = ang#
  184.         RotateEntity thead, 0, 0, ang#
  185. End Function
  186.  
  187. Function fire(t.turret)
  188.         ang# = tang#
  189.         s.shot = New shot
  190.         sx# = tx# + Cos(ang#) * r
  191.         sy# = ty# + Sin(ang#) * r
  192.         s
  193. # = r
  194.         sdx# = Cos(ang#) * v0#
  195.         sdy# = Sin(ang#) * v0#
  196.         sh = CreateSphere(2)
  197.         ScaleEntity sh, .15, .15, .15
  198.         EntityColor sh, 255, 255, 0
  199.         ; Auxiliary variables(for AI)
  200.         s       x# = tx#
  201.         s       y# = ty#
  202.         sdir = tdir#
  203.         s        = t
  204. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal