Ooops
March 05, 2021, 06:39:21 AM

Author Topic: [bmx] Traffic by dw817 [ 1+ years ago ]  (Read 441 times)

Offline BlitzBot

[bmx] Traffic by dw817 [ 1+ years ago ]
« on: June 29, 2017, 12:28:40 AM »
Title : Traffic
Author : dw817
Posted : 1+ years ago

Description : What does this program do ?

It sends many cars in from 4-directions, interspaced so there can be no head-on collisions. Despite this safety measure traffic jams can and will occur.

If a car is about to collide with another, it will wait until the way is clear.

However, in the event of a more serious traffic jam, the drivers will recognize this and attempt to clear it by backing up if necessary. Ultimately this does free the way for other drivers to work their way through it.

Hold down SPACEBAR for TURBO. Turbo will only not be available when there is an actual traffic jam taking place so you can observe how the drivers clear it.

WHAT'S UP ?

Set it so there is more traffic and when it reaches the end, it does not stop but instead vanish - allowing more traffic to cross.

Developed a clever method of handling whether or not I am READING or WRITING to the map by using a single variable called NEG.

Handling 4-car pileup by backing up the car that's been waiting the longest. Now to my knowledge, this is a PERFECT solution.

When a pileup does occur and you are holding down SPACEBAR for TURBO, it will be disabled so you can see how everyone helps to clear out the traffic jam.

Once the jam is clear, you can enter TURBO mode again to try and find another problem.


Code :
Code: BlitzMax
  1. '     ________________________________________
  2. '    //                                     //
  3. '   // "T R A F F I C"                     //
  4. '  // Version: "Chivalry On The Road"     //
  5. ' // Written by David W (dw817) 03/11/16 //
  6. '//_____________________________________//
  7. '
  8. ' What's up ??
  9. ' Set it so there is more traffic and when it reaches the
  10. ' end, it does not stop but instead vanish - allowing more
  11. ' traffic to cross.
  12. '
  13. ' Developed a clever method of handling whether or not I
  14. ' am READING or WRITING to the map by using a single
  15. ' variable called NEG.
  16. '
  17. ' Handling 4-car pileup by backing up the car that's been
  18. ' waiting the longest. Now to my knowledge, this is a
  19. ' PERFECT solution.
  20.  
  21. ' When a pileup does occur and you are holding down
  22. ' SPACEBAR for TURBO, it will be disabled so you can see how
  23. ' everyone helps to clear out the traffic jam.
  24.  
  25. ' Once the jam is clear, you can enter TURBO mode again to
  26. ' try and find another problem.
  27.  
  28. Strict
  29. SeedRnd MilliSecs() ' ensure random results each time
  30.  
  31. SetMaskColor 0,0,0
  32. Global img_tile:TImage=LoadAnimImage(LoadBank("http::www.writerscafe.org/uploads/rte/0a3c15c2943abe552357bc7d46840e8c.png"),8,8,0,5,maskedimage)
  33. ' ^ load in 5 image tiles. Quicker than DefData.
  34.  
  35. Global scrn[31,23] ' full screen size of 31x23 tiles at 32x32 pixels, 8x8 pixels zoomed 4x
  36. Global scrncount[31,23] ' prevent traffic jams by taking out a car if stationary for 5 turns
  37.  
  38. Local i,j,k,m,c,x,y,s,r,jam,neg=1
  39.  
  40. SetGraphicsDriver GLMax2DDriver(),0
  41. Graphics 1024,768
  42. SetScale 4,4
  43.  
  44. Repeat ' {* MAIN *}
  45.  
  46. r=(r+1)Mod 4 ' everyone gets equal traffic time
  47. If jam<5 ' only add cars if there is no traffic jam
  48.   If r=0
  49.     setscrn fnr(16)*2,22,1*neg ' add a car IF the place is empty
  50.   ElseIf r=1
  51.     setscrn 30,fnr(11)*2,2*neg
  52.   ElseIf r=2
  53.     setscrn 0,fnr(11)*2+1,3*neg
  54.   Else ' no need to compare for 3
  55.     setscrn fnr(15)*2+1,0,4*neg
  56.   EndIf
  57. EndIf
  58.  
  59. update ' show our work
  60. If jam Then jam:-1
  61. For i=0 Until 23 ' scan entire playfield
  62.   For j=0 Until 31
  63.     c=getscrn(j,i) ' read in each tile of the playfield
  64.     x=0 ; y=0 ' if changed means we are moving a vehicle
  65.     If c=1*neg Then y=-1 ' Move arrow in designated direction IF area is free.
  66.     If c=2*neg Then x=-1 ' Negative is necessary so the tile is not reselected
  67.     If c=3*neg Then x=1 ' in this one pass or it won't work correctly.
  68.     If c=4*neg Then y=1
  69.     s=scrncount[j,i] ' how long has this vehicle been idle ?
  70.     If x<>0 Or y<>0 Or s>4 ' car can move or is stalled
  71.       If s>4 And getscrn(j-x,i-y)=0 ' qualifies as jammed
  72.         If s>4 Then jam=8 ' 8 cycles will pass where you cannot use TURBO
  73.         scrn[j,i]=0 ' erase position of car
  74.         setscrn j-x,i-y,-c ' back up vehicle one step if possible
  75.         setscrncount j-x,i-y,Rand(0,5) ' critical ! Car waits a specific time before backing up again
  76.         scrncount[j,i]=Rand(0,5)
  77.       ElseIf getscrn(j+x,i+y)=0 ' path is free
  78.         scrn[j,i]=0 ' erase position of car
  79.         scrn[j+x,i+y]=-c ' move car forward one step
  80.         scrncount[j,i]=0 ' reset any stalled status
  81.       EndIf
  82.     ElseIf c ' car is here but it can't move, there is an obstacle in the way
  83.       scrncount[j,i]:+1 ' increase stalled time
  84.     Else
  85.       scrncount[j,i]=0 ' always reset stalled time on empty space
  86.     EndIf
  87.     If i=0 And scrn[j,i]=1*neg Then scrn[j,i]=0 ' clear out any cars that have successfully
  88.     If j=0 And scrn[j,i]=2*neg Then scrn[j,i]=0 ' traveled the full length of the screen across
  89.     If j=30 And scrn[j,i]=3*neg Then scrn[j,i]=0
  90.     If i=22 And scrn[j,i]=4*neg Then scrn[j,i]=0
  91.   Next
  92. Next
  93.  
  94. neg=-neg ' BAM ! This resets all positions so they can be read again
  95.  
  96. If KeyDown(32)=0 Or jam Then Delay 150 ' hold down SPACEBAR for TURBO, disabled if there is a traffic jam
  97. If KeyDown(27) Then End ' exit on ESCAPE key
  98.  
  99. Forever ' {* END OF MAIN *}
  100.  
  101. ' >> UPDATE SCREEN WITH TILES
  102. Function update()
  103. Local i,j,n,x,y
  104.   Cls
  105.   For i=0 Until 23
  106.     For j=0 Until 31
  107.       x=j*32+16 ; y=i*32
  108.       DrawImage img_tile,x,y,0
  109.       n=scrn[j,i]
  110.       If n ' with no comparitive number it means <>0 (zero)
  111.         SetColor 255,255,255 ' always reset color when plotting any tiles
  112.         DrawImage img_tile,x,y,Abs(n) ' draw arrow images, NOTICE it is the absolute value !
  113.       EndIf
  114.     Next
  115.   Next
  116.   Flip 0 ' show our work
  117. EndFunction
  118.  
  119. ' >> RETRIEVE SCREEN TILE EVEN IF OFF EDGES
  120. Function getscrn(x,y)
  121. Local r=-1
  122.   If x>=0 And y>=0 And x<=30 And y<=22 Then r=scrn[x,y]
  123.   Return r
  124. EndFunction
  125.  
  126. ' >> PLOT A VEHICLE IF SPACE IS CLEAR
  127. Function setscrn(x,y,z)
  128.   If getscrn(x,y)=0 Then scrn[x,y]=z
  129. EndFunction
  130.  
  131. ' >> MARK A TILE WITH STALL TIME EVEN IF OFF EDGES
  132. Function setscrncount(x,y,z)
  133.   If x>=0 And y>=0 And x<=30 And y<=22 Then scrncount[x,y]=z
  134. EndFunction
  135.  
  136. ' >> RETURN COMMA DELIMITED TEXT FROM A$
  137. Function yank$(a$ Var)
  138. Local b=Instr(a$+",",","),c$=Left$(a$,b-1)
  139.   a$=Mid$(a$,b+1) ' also remove it from input string
  140.   Return c$
  141. EndFunction
  142.  
  143. ' >> QUICK RETRIEVE RANDOM 0 to A-1
  144. Function fnr(a)
  145.   Return Rand(a)-1
  146. EndFunction


Comments :


Derron(Posted 1+ years ago)

 As I did not have a closer look to the algorithm itself:- you could store one grayscaled sprite and use a "SetColor r,g,b;DrawImage(...);SetColor 255,255,255" tint-approach to skip creating multiple variations of a sprite. This way you could use even more colors or indicators of whether they are in a jam or not (darken, brighten up...).byeRon


Blitzplotter(Posted 1+ years ago)

 Nice piece of code - fired up my copy of BMax for the first time in a while (been immersed in Monkey and B3D)


dw817(Posted 1+ years ago)

 Hi Derron. That's not such a bad idea. Sometimes I forget you can color sprites in BlitzMAX, you can't do that in GFA and it's up to the author to color their own.... But they are also facing different directions. Ok, I could make all 4 of them WHITE and then tint accordingly.BP, I really must say I'm not impressed with Monkey. Blitz3D I have never used. I think the best GUI to use really is in BlitzMAX, is one of the main reasons I code in it.


Derron(Posted 1+ years ago)

 @facingMidHandle the image, and SetRotation(90/180/270/0) might helpbyeRon


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal