Older Version
Newer Version
bluatigro
Dec 9, 2010
''version 9-12-2010 ''walk = ready ''follow hunt = ready ''intersept hunt = skipped ''flee = ready ''avoid = ready ''floking = in planing ''etc... = thinking about WindowWidth = DisplayWidth WindowHeight = DisplayHeight global winx , winy , anim$ , dotmax winx = WindowWidth winy = WindowHeight global pi , gohunt dim st( 5 ) , speed( 5 ) pi = atn( 1 ) * 4 dotmax = 10 for i = 0 to 31 loadbmp "green" ; i , DefaultDir$ _ + "\BMP\greenanimal" + nr$( i ) + ".bmp" greenanim$ = greenanim$ + " green" ; i loadbmp "red" ; i , DefaultDir$ _ + "\BMP\redanimal" + nr$( i ) + ".bmp" redanim$ = redanim$ + " red" ; i next i loadbmp "dot" , DefaultDir$ + "\BMP\wall.bmp" global hunter , prey hunter = 0 prey = 1 nomainwin menu #m , "senario" _ , "walk" , [mnuWalk] _ , "hunt" , [mnuHunt] _ , "flee" , [mnuFlee] _ , "avoid" , [mnuAvoid] _ , | , "exit" , [quit] global state , stwalk , sthunt , stflee , stavoid stwalk = 0 sthunt = 1 stflee = 2 stavoid = 3 open "ANIMALS" for graphics as #m #m "trapclose [quit]" #m "fill blue" #m "getbmp back 0 0 2 2" #m "background back" for i = 0 to dotmax #m "addsprite dot" ; i ; " dot" #m "spritexy dot"; i ; " " _ ; rnd( 0 ) * winx ; " " _ ; rnd( 0 ) * winy next i #m "addsprite animal" ; prey ; " " ; greenanim$ #m "spritexy animal" ; prey ; " " ; 0 _ ; " " ; 100 st( prey ) = 0 speed( prey ) = 3 #m "addsprite animal" ; hunter ; " " ; redanim$ #m "spritexy animal" ; hunter ; " " ; winx / 2 - 32 _ ; " " ; winy * 2 / 3 st( hunter ) = 0 speed( hunter ) = 0 #m "drawsprites" gosub [mnuWalk] timer 40 , [tmr] wait ''simulaion flow sub restart for i = 0 to dotmax #m "spritexy dot"; i ; " " _ ; rnd( 0 ) * winx ; " " _ ; rnd( 0 ) * winy next i #m "spritexy animal" ; prey ; " " ; 0 _ ; " " ; 100 st( prey ) = 8 speed( prey ) = 3 #m "spritexy animal" ; hunter ; " " ; winx / 2 - 32 _ ; " " ; winy * 2 / 3 st( hunter ) = 0 speed( hunter ) = 0 end sub [mnuWalk] state = stwalk timer 0 notice "Senario 0 :" ; chr$( 13 ) _ ; "The animals wil" ; chr$( 13 ) _ ; "walk randomly" ; chr$( 13 ) _ ; "whitout inteaction ." timer 40 , [tmr] call restart wait [mnuHunt] state = sthunt timer 0 notice "Senario 1 :" ; chr$( 13 ) _ ; "The green wil" ; chr$( 13 ) _ ; "walk in a line" ; chr$( 13 ) _ ; "while red wil" ; chr$( 13 ) _ ; "chase the green ." timer 40 , [tmr] call restart wait [mnuFlee] state = stflee timer 0 notice "Senario 2 :" ; chr$( 13 ) _ ; "The green wil" ; chr$( 13 ) _ ; "walk in a line" ; chr$( 13 ) _ ; "if red = colse 2 green" ; chr$( 13 ) _ ; "then green flees from red" ; chr$( 13 ) _ ; "while red wil" ; chr$( 13 ) _ ; "chase the green ." timer 40 , [tmr] call restart wait [mnuAvoid] state = stavoid timer 0 notice "Senario 3 :" ; chr$( 13 ) _ ; "The green wil" ; chr$( 13 ) _ ; "walk in a line" ; chr$( 13 ) _ ; "if red = close 2 green" ; chr$( 13 ) _ ; "then green flees from red" ; chr$( 13 ) _ ; "while red wil" ; chr$( 13 ) _ ; "chace the green" ; chr$( 13 ) _ ; "red and green wil" ; chr$( 13 ) _ ; "avoid the 'trees' ." timer 40 , [tmr] call restart wait [tmr] select case state case stavoid if distance( prey , hunter ) < 600 then fl = 0 for i = 0 to dotmax if avoid( hunter , "dot" ; i , 100 ) then fl = 1 next i if fl = 0 then call hunt 0 end if if distance( prey , hunter ) < 300 then fl = 0 for i = 0 to dotmax if avoid( prey , "dot" ; i , 100 ) then fl = 1 next i if fl = 0 then call flee 0 end if case stflee if distance( prey , hunter ) < 600 then call hunt 0 end if if distance( prey , hunter ) < 300 then call flee 0 end if case sthunt if distance( prey , hunter ) < 600 then call hunt 0 end if case else call walk prey call walk hunter end select if distance( prey , hunter ) < 50 then call restart end if ''update prey and hunter spot #m "spritexy? animal" ; prey ; " x y" x = x + sin( strad( st( prey ) ) ) * speed( prey ) y = y - cos( strad( st( prey ) ) ) * speed( prey ) x = x - sin( strad( st( hunter ) ) ) * speed( hunter ) y = y + cos( strad( st( hunter ) ) ) * speed( hunter ) #m "spritexy animal" ; prey ; " " ; x ; " " ; y ''this wil not change anymore til flocking #m "spriteimage animal" ; prey ; " green" _ ; ( st( prey ) + 16 ) mod 32 #m "spriteimage animal" ; hunter ; " red" _ ; ( st( huntor ) + 16 ) mod 32 #m "spritexy? animal" ; prey ; " x y" if x < -64 then x = winx if x > winx then x = -64 if y < -64 then y = winy if y > winy then y = -64 #m "spritexy animal" ; prey ; " " ; x ; " " ; y for i = 0 to dotmax #m "spritexy? dot" ; i ; " x y" x = x - sin( strad( st( hunter ) ) ) * speed( hunter ) y = y + cos( strad( st( hunter ) ) ) * speed( hunter ) if x < -64 then x = winx if x > winx then x = -64 if y < -64 then y = winy if y > winy then y = -64 #m "spritexy dot" ; i ; " " ; x ; " " ; y next i #m "drawsprites" wait [quit] for i = 0 to 31 unloadbmp "green" ; i unloadbmp "red" ; i next i close #m end ''movement control of anuimals sub walk nr move = int( rnd( 0 ) * 3 ) select case move case 0 st( nr ) = st( nr ) + 1 st( nr ) = st( nr ) mod 32 speed( nr ) = 2 case 1 st( nr ) = st( nr ) - 1 st( nr ) = st( nr ) mod 32 speed( nr ) = 2 case else speed( nr ) = 3 end select end sub sub hunt drunk #m "spritexy? animal" ; prey ; " px py" #m "spritexy? animal" ; hunter ; " hx hy" ha = strad( st( hunter ) ) dx = hx - px dy = hy - py if drunk then if abs( dx ) < 1e-10 then dx = 1e-10 da = atn( dy / dx ) if da < ha then st( hunter ) = st( hunter ) + 1 st( hunter ) = st( hunter ) and 31 else st( hunter ) = st( hunter ) - 1 st( hunter ) = st( hunter ) and 31 end if else call rotate dx , dy , 0-strad( st( hunter ) ) if dx < 0 then st( hunter ) = st( hunter ) + 1 st( hunter ) = st( hunter ) and 31 else st( hunter ) = st( hunter ) - 1 st( hunter ) = st( hunter ) and 31 end if end if speed( hunter ) = 5 end sub sub flee drunk #m "spritexy? animal" ; prey ; " px py" #m "spritexy? animal" ; hunter ; " hx hy" ha = strad( st( prey ) ) dx = hx - px dy = hy - py if drunk then if abs( dx ) < 1e-10 then dx = 1e-10 da = atn( dy / dx ) if da > ha then st( prey ) = st( prey ) + 1 st( prey ) = st( prey ) and 31 else st( prey ) = st( prey ) - 1 st( prey ) = st( prey ) and 31 end if else call rotate dx , dy , 0-strad( st( prey ) ) if dx < 0 then st( prey ) = st( prey ) + 1 st( prey ) = st( prey ) and 31 else st( prey ) = st( prey ) - 1 st( prey ) = st( prey ) and 31 end if end if speed( prey ) = 4 end sub function avoid( nr , obj$ , size ) uit = 0 #m "spritexy? animal" ; nr ; " ax ay" #m "spritexy? " ; obj$ ; " ox oy" seex = ax + sin( strad( st( nr ) ) ) * size seey = ay - cos( strad( st( nr ) ) ) * size if sqr( ( ox - seex ) ^ 2 + ( oy - seey ) ^ 2 ) < size then st( nr ) = st( nr ) + 1 speed( nr ) = 0 uit = not( 0 ) else if nr = hunter then speed( nr ) = 4 else speed( nr ) = 3 end if end if avoid = uit end function function atan2( y , x ) if x = 0 then if y < 0 then angle = pi * 2 - pi / 2 else angle = pi / 2 end if else angle = atn( y / x ) if x < 0 then angle = pi - angle end if if angle > pi * 2 then angle = angle - pi * 2 end if end if if angle < 0 - pi * 2 then angle = angle + pi * 2 atan2 = angle end function ''suport functions sub rotate byref k , byref l , r s = sin( r ) c = cos( r ) hk = k * c - l * s hl = k * s + l * c k = hk : l = hl end sub function nr$( no ) nr$ = right$( "00" + str$( no ) , 2 ) end function function strad( st ) strad = st * pi / 16 end function function distance( a , b ) #m "spritexy? animal" ; a ; " ax ay" #m "spritexy? animal" ; b ; " bx by" dx = abs( bx - ax ) dy = abs( by - ay ) distance = sqr( dx ^ 2 + dy ^ 2 ) end function