AI : ant road map creator

General FreeBASIC programming questions.
bluatigro
Posts: 597
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

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 maker

screen 18 , 32
dim shared as integer winx , winy
screeninfo winx , winy
paint ( 1 , 1 ) , 0
const as double pi = atn( 1 ) * 4

type double2d
dim as double x , y
declare constructor ()
declare constructor ( a as double , b as double )
end type
constructor double2d()
end constructor
constructor double2d( a as double , b as double )
x = a
y = b
end constructor
operator - ( a as double2d , b as double2d ) as double2d
return double2d( a.x - b.x , a.x - b.y )
end operator
operator + ( a as double2d , b as double2d ) as double2d
return double2d( a.x + b.x , a.y + b.y )
end operator
function 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 / 180
end 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 = hl
end sub

type 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 type
sub 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 sub
function range( l as double , h as double ) as double
return rnd * ( h - l ) + l
end function
sub 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 if
end sub
randomize timer
dim as integer i , x , y
dim 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 , 5
next i
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
end if
next y
next x
''ants move and drop feramon
for i = 0 to ubound( ants )
ants( i ).move 45 , 2
next i
loop while inkey = ""
Tourist Trap
Posts: 2762
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 i
loop 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 :)