## AI : ant road map creator

General FreeBASIC programming questions.
bluatigro
Posts: 651
Joined: Apr 25, 2012 10:35
Location: netherlands

### AI : ant road map creator

i m trying to create a road map by ants

this is based on a presenation
by the ai hcc ig

see also :
www.ai.hcc.nl/nieuws.html [ dutch ]
[ wil be soon there ]

error :
my food is not on good place

Code: Select all

`'' bluatigro 5 feb 2019'' ant road map makerscreen 18 , 32dim shared as integer winx , winyscreeninfo winx , winypaint ( 1 , 1 ) , 0const as double pi = atn( 1 ) * 4type double2d  dim as double x , y  declare constructor ()  declare constructor ( a as double , b as double )end typeconstructor double2d()end constructorconstructor double2d( a as double , b as double )    x = a    y = bend constructoroperator - ( a as double2d , b as double2d ) as double2d    return double2d( a.x - b.x , a.x - b.y )end operatoroperator + ( a as double2d , b as double2d ) as double2d    return double2d( a.x + b.x , a.y + b.y )end operatorfunction length( a as double2d ) as double    return sqr( a.x ^ 2 + a.y ^ 2 )end function  function rad( deg as double ) as double    return deg * pi / 180end function dim shared as double2d food( 3 )food( 0 ) = double2d( winx / 4 , winy / 4 )food( 1 ) = double2d( winx / 4 , winy * 3 / 4 )food( 2 ) = double2d( winx * 3 / 4 , winy / 4 )food( 3 ) = double2d( winx * 3 / 4 , winy * 3 / 4 )sub rotate( byref k as double , byref l as double , deg as double )    dim as double s , c , hk , hl    s = sin( rad( deg ) )    c = cos( rad( deg ) )    hk = k * c - l * s    hl = k * s + l * c    k = hk    l = hlend subtype ant    dim as double2d center , sensor( 2 )    dim as double angle     declare sub fill( a as double , b as double )    declare sub move( a as double , b as double )end typesub ant.fill( a as double , b as double )    dim as double dx , dy     dx = b    dy = 0    sensor( 1 ) = double2d( dx , dy )    rotate dx , dy , a    sensor( 0 ) = double2d( dx , dy )    rotate dx , dy , -a * 2    sensor( 2 ) = double2d( dx , dy )end subfunction range( l as double , h as double ) as double    return rnd * ( h - l ) + lend functionsub ant.move( a as double , b as double )    dim as double2d s( 2 ) , d    dim as integer i , fl    dim as ulong kl( 2 )    dim as double dice    for i = 0 to 2      s( i ) = sensor( i )      rotate s( i ).x , s( i ).y , angle      kl( i ) = point( center.x + s( i ).x , center.y - s( i ).y )    next i    d.x = b    d.y = 0    rotate d.x , d.y , angle    if kl( 0 ) > kl( 1 ) and kl( 0 ) > kl( 2 ) then      dice = range( 0 , a )      rotate d.x , d.y , dice      angle += dice    end if    if kl( 2 ) > kl( 1 ) and kl( 2 ) > kl( 0 ) then      dice = range( -a , 0 )      rotate d.x , d.y , dice      angle += dice    end if    if kl( 1 ) > kl( 0 ) and kl( 1 ) > kl( 2 ) then      dice = range( -a / 2 , a / 2 )      rotate d.x , d.y , dice      angle += dice    end if    center.x = ( center.x + d.x ) mod winx    center.y = ( center.y + d.y ) mod winy    fl = 0    for i = 0 to ubound( food )        if length( food( i ) - center ) < 10 then fl = 1    next i    if fl then        pset( center.x , center.y ) _        , point( center.x , center.y ) + 200    else        pset( center.x , center.y ) _        , point( center.x , center.y ) + 50    end ifend subrandomize timerdim as integer i , x , ydim as ant ants( 2000 )for i = 0 to ubound( ants )    ants( i ).center.x = range( 0 , winx )    ants( i ).center.y = range( 0 , winy )    ants( i ).angle = range( 0 , 360 )    ants( i ).fill 45 , 5next ido''feramon disapering    for x = 0 to winx         for y =- 0 to winy             if point( x , y ) then                 pset( x , y ) , point( x , y ) - 1             end if         next y    next x ''ants move and drop feramon    for i = 0 to ubound( ants )        ants( i ).move 45 , 2    next iloop while inkey = ""`
Tourist Trap
Posts: 2901
Joined: Jun 02, 2015 16:24

### Re: AI : ant road map creator

Hi,

I just made this change:

Code: Select all

`do''feramon disapering    for x = 0 to winx         for y =- 0 to winy             if point( x , y ) then                 pset( x , y ) , (point( x , y ) - 1)*10000  ''value was too dark             end if         next y    next x ''ants move and drop feramon    for i = 0 to ubound( ants )        ants( i ).move 45 , 2    next iloop while inkey = ""`

the color is a ulong that in general will be rendered very dark for little values. So I multiply by 10000, it's better for my eyes :)

Return to “General”

### Who is online

Users browsing this forum: No registered users and 5 guests