Ooops
January 26, 2021, 12:06:54 PM

Author Topic: [bb] Radar Scan: Is that enemy within range? by Ghoula [ 1+ years ago ]  (Read 423 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Radar Scan: Is that enemy within range?
Author : Ghoula
Posted : 1+ years ago

Description : Will scan a given radius and return true/false if target is in range.

Code :
Code: BlitzBasic
  1. ;---------------------------------------------------------------
  2. ;------------------Radar Check 1.0------------------------------
  3. ;---------------------by  Ghoula--------------------------------
  4.  
  5. ;This example will show you how to check if an enemy unit
  6. ;is within a freindly unit's attack radius. This example
  7. ;is a visual representation of the logic involved, so about 95%
  8. ;of this code is for pulling off the visuals. Function: 'RealCheckRange'
  9. ;at the bottom of this code shows only the hardcore logic needed.
  10. ;It's actually quite small :)
  11.  
  12.  
  13. ;I use this function in my current project for satellites and
  14. ;performing air unit strikes (if enemy is in range) and also
  15. ;for rocket launcher units (again: if enemy is in range he can be shot)
  16. ;I also searched high and low for a routine or snipet of code
  17. ;that could do this and found nothing. So with a little patience i made
  18. ;my own Function. Maybe someone has a use for this if so feel free To
  19. ;use Or abuse code in anyway! :)
  20.  
  21. Graphics 640,480
  22.  
  23. Global unit_x,unit_y            ;Storage - Freindly unit X and Y map location
  24. Global target_x,target_y        ;Storage - Enemy unit X and Y map location
  25. Global range                            ;Storage - Scan range of unit
  26.  
  27. ;Setup a small 2d map for visuals
  28. Type tiledata
  29.         Field x_loc
  30.         Field y_loc
  31. End Type
  32. SeedRnd MilliSecs()                     ;Always seed the timer for randomness
  33. SetBuffer FrontBuffer()         ;We will draw straight to the frontbuffer nothing fancy
  34.  
  35. ;Set friendly unit location and his radar range
  36. unit_x=10
  37. unit_Y=7
  38. range=6 ;This value is from unit outward. Since we scan a radius the actual scan area is x2 or 12 if value is 6
  39.  
  40. ;Main loop for visuals, will keep scanning until ESC is hit
  41. Repeat
  42.  
  43.         ;Draw map tiles
  44.         DrawTiles()
  45.         ;Check our radar to see if enemy is within range. Will return true or false
  46.         InRange=Func_CheckRange(unit_x,unit_y,target_x,target_y,range)
  47.  
  48.         ;Check range returned true so enemy is within range
  49.         If InRange=True
  50.                 Color 0,255,0
  51.                 Text 0,0,"Target is in range!! hooah!"
  52.         ;Returned false, enemy not in range
  53.         Else
  54.                 Color 255,0,0
  55.                 Text 0,0,"Target not in range"
  56.         EndIf
  57.         Delay 1000 ;Delay fer visuals
  58.        
  59. Until KeyHit(1)
  60. End
  61.  
  62. ;----Func: Check range---------------------------------------------------
  63. Function Func_CheckRange(ux,uy,tx,ty,rng)
  64.  
  65.         ;For illustration purposes this function has been tailored to simulate
  66.         ;32x32 pixel sized tiles but is easily adjustable to work on different
  67.         ;size tiles. Also keep in mind mmost of this crap is for the visuals :)
  68.        
  69.         ;Draw radius circle
  70.         Color 255,0,0
  71.         Oval ((ux*32)-rng*32)+16,((uy*32)-rng*32)+16,rng*64,rng*64,0
  72.        
  73.         ;Scan logic, notice that only a 90 degree sweep is needed to effectively
  74.         ;scan a 360 degree radius. The visuals will show you the logic in action
  75.        
  76.         For degrees=0 To 90 Step 5 ;The smaller the scan area the higher the step can be
  77.                                                            ;if you are scanning large areas then lower the step value
  78.                                                            ;to aviod 'gaps'. Observe the blue line when program is running.
  79.                                                            ;In this example 5 is actually overkill could get away with
  80.                                                            ;a higher value since our scan radius is small.
  81.                                                        
  82.                 Delay 100 ;A delay simply for visuals, remove delay and it's lightning quick
  83.                 scanx=ux*32+16+Sin(degrees)*rng*32 ;<< The SIN logic
  84.                 scany=uy*32+16+Cos(degrees)*rng*32 ;<< The COS logic
  85.                 Color 0,50,255
  86.                 Line scanx,scany,ux*32+16,uy*32+16 ;Blue degree line (visual)
  87.                 scanx=Abs(scanx-ux*32) ;<< Some gnomish magic (using ABS to keep the number posistive)
  88.                 scany=Abs(scany-uy*32) ;<< "
  89.                
  90.                 ;Here we will color the tiles we have scanned yellow
  91.                 For tile.tiledata=Each tiledata
  92.                         If tilex_loc>(ux*32)-scanx And tilex_loc<(ux*32)+scanx And tiley_loc>(uy*32)-scany And tiley_loc<(uy*32)+scany
  93.                                 Color 220,220,0
  94.                                 Rect tilex_loc,tiley_loc,32,32,0
  95.                                 ;Target in range return true
  96.                                 If tilex_loc=tx*32 And tiley_loc=ty*32 Then Return True
  97.                         EndIf
  98.                 Next
  99.         Next
  100.        
  101.         ;Target not in range, return false
  102.         Return False
  103.  
  104. End Function
  105.  
  106. ;----Draw tiles (illustration purposes)-----------------------------------
  107. Function DrawTiles()
  108.  
  109.         ;Draw map grid
  110.         Cls
  111.         x=0
  112.         y=0
  113.         Color 111,111,111
  114.         For t=1 To 300
  115.                 Rect x,y,32,32,0
  116.                 x=x+32
  117.                 tile.tiledata=New tiledata
  118.                 tilex_loc=x
  119.                 tiley_loc=y
  120.                 If x=>640
  121.                         x=0
  122.                         y=y+32
  123.                 EndIf
  124.         Next
  125.         ;Draw unit
  126.         Color 0,220,0
  127.         Rect unit_x*32,unit_y*32,32,32,1
  128.         ;Draw target unit at random location
  129.         target_x=Rand(19) ;Remember Rand for integers and Rnd fer floating numbers! :)
  130.         target_y=Rand(14) ;                                             ""
  131.         Color 255,0,0
  132.         Rect target_x*32,target_y*32,32,32,1
  133.  
  134. End Function
  135.  
  136.  
  137.  
  138.  
  139. ;---------------------------------------------------------------------------
  140. ;----Here is the radar check function stripped down and ready fer action----
  141. ;----If your are using diffrent size tiles say 48x48 then change all 32s----
  142. ;----to 48 and all 16s to 24. You get the picture :P
  143. ;---------------------------------------------------------------------------
  144.  
  145. ;----Func: Check range------------------------------------------------------
  146. Function Func_RealCheckRange(ux,uy,tx,ty,rng)
  147.  
  148.         For degrees=0 To 90 Step 5
  149.                 scanx=ux*32+16+Sin(degrees)*rng*32
  150.                 scany=uy*32+16+Cos(degrees)*rng*32
  151.                 scanx=Abs(scanx-ux*32)
  152.                 scany=Abs(scany-uy*32)
  153.                 ;Scan tiles            
  154.                 For tile.tiledata=Each tiledata
  155.                         If tilex_loc>(ux*32)-scanx And tilex_loc<(ux*32)+scanx And tiley_loc>(uy*32)-scany And tiley_loc<(uy*32)+scany
  156.                                 ;In range
  157.                                 If tilex_loc=tx*32 And tiley_loc=ty*32 Then Return True
  158.                         EndIf
  159.                 Next
  160.         Next
  161.         ;Not in range
  162.         Return False
  163.  
  164. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal