TGC Codebase Backup



Shoal! by napier

28th Nov 2006 7:08
Summary

Fish simulator (n is the number of fish, try 100)



Description

This game is more or less a practise at making fish brains. There are a whole bunch of fish that, if left to their own devices group into shoals and swim around. The player is a shark (or a circle with a line on it) which is avoided by the fish. The startup screen requires an input for the number of fish. Each fish can have its own personality, the level of which can be set by changing the freewill# variable. The distance from the fish that the shark must be to scare them is defined by fr#. l# controls how they respond to the other fish.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    `***************************************************************************
`										  SHOAL!
`***************************************************************************
hide mouse

input "n?    /",n

freewill#=10
fr#=50
l#=100

dim e#(n,10)

for t=1 to n
e#(t,1)=rnd(640)
e#(t,2)=rnd(480)
e#(t,5)=rnd(360)
e#(t,6)=rnd(freewill#)
next t

a=320
b=240


sync on
sync rate 0
do
cls




o=o+(leftkey()-rightkey())*4
if upkey() then a=a+5*sin(o):b=b+5*cos(o)
if downkey() then a=a-5*sin(o):b=b-5*cos(o)

if a=>640 then a=639
if a=<0 then a=1
if b=>480 then b=479
if b=<0 then b=1

for t=1 to n

if (e#(t,1)-a)=0
d=1
else
d=0
endif

` shark avoid function
`********************************************************************
if sqrt((a+10*sin(o)-e#(t,1))*(a+10*sin(o)-e#(t,1))+(b+10*cos(o)-e#(t,2))*(b+10*cos(o)-e#(t,2)))<fr#
ang#=atan(abs(b-e#(t,2))/abs(e#(t,1)-a-1))
if b>e#(t,2) and a<e#(t,1) then e#(t,5)=ang#-270
if b>e#(t,2) and a>e#(t,1) then e#(t,5)=ang#-90
if b<e#(t,2) and a<e#(t,1) then e#(t,5)=0-ang#-270
if b<e#(t,2) and a>e#(t,1) then e#(t,5)=ang#
goto sheep
endif


` Think of the others 

for k=1 to n

if k=t then goto dog

` SHOAL instinct 1 

if sqrt((e#(t,1)-e#(k,1))*(e#(t,1)-e#(k,1))+(e#(t,2)-e#(k,2))*(e#(t,2)-e#(k,2)))=0
fred#=1
else
fred#=0
endif
re#=100/sqrt((e#(t,1)-e#(k,1))*(e#(t,1)-e#(k,1))+(e#(t,2)-e#(k,2))*(e#(t,2)-e#(k,2))+fred#)
if e#(t,1)+e#(t,3)>e#(k,1)+l# and e#(t,2)+e#(t,4)>e#(k,2)+l# then e#(t,5)=e#(t,5)-re#+rnd(e#(t,6))-e#(t,6)/2
if e#(t,1)+e#(t,3)<e#(k,1)-l# and e#(t,2)+e#(t,4)<e#(k,2)-l# then e#(t,5)=e#(t,5)+re#+rnd(e#(t,6))-e#(t,6)/2
if e#(t,1)+e#(t,3)<e#(k,1)-l# and e#(t,2)+e#(t,4)>e#(k,2)+l# then e#(t,5)=e#(t,5)-re#+rnd(e#(t,6))-e#(t,6)/2
if e#(t,1)+e#(t,3)>e#(k,1)+l# and e#(t,2)+e#(t,4)<e#(k,2)-l# then e#(t,5)=e#(t,5)+re#+rnd(e#(t,6))-e#(t,6)/2

` SHOAL instinct 2 

x=e#(t,1)-e#(k,1)+e#(t,3)
y=e#(k,2)-e#(t,2)-e#(t,4)
if x=0
dork#=1
else
dork#=0
endif
e#(t,5)=(e#(t,5)*(n-re#)-re#*((x*90)/(abs(x)+1)-atan(y/(x+dork#))))/n


dog:
next k

sheep:

e#(t,3)=3*sin(e#(t,5))
e#(t,4)=3*cos(e#(t,5))

e#(t,1)=e#(t,1)+e#(t,3)
e#(t,2)=e#(t,2)+e#(t,4)

if e#(t,1)>640 then e#(t,1)=e#(t,1)-640
if e#(t,1)<0 then e#(t,1)=e#(t,1)+640

if e#(t,2)>480 then e#(t,2)=e#(t,2)-480
if e#(t,2)<0 then e#(t,2)=e#(t,2)+480

`draw fishes
ink 0,rgb(100,100,255)
line e#(t,1),e#(t,2),e#(t,1)-e#(t,3),e#(t,2)-e#(t,4)

next t

`draw shark

line a,b,a+12*sin(o),b+12*cos(o)
circle a,b,10

sync
loop