alexthescott.memory probe 6

-- memory probe 6
-- alexthescott
-- 10/4/21 9:12pm

-- new seed every day of the year 
srand(31*stat(81)+stat(82)) 

p1={7,6,15,143,4,141,13,134,5,133,2,1,130,128,129,0}
p2={7,135,143,14,9,142,137,8,136,2,134,4,132,133,130,0}
p3={7,6,134,13,12,2,141,5,140,131,1,129,133,130,128,0}

p={p1,p2,p3}
c=p[1+rnd(#p)\1]

pal(c,1)

cnt=rnd(2)\1

function new_obj()
	obj={}
	obj.pos={x=rnd(128)\1,y=rnd(128)\1}
	obj.a=rnd()
	obj.vel={x=cos(obj.a)/2,y=sin(obj.a)/2}
	-- grid position 
	obj.grid_size=16
	obj.g_pos={}
	obj.g_pos.x=obj.pos.x\obj.grid_size
	obj.g_pos.y=obj.pos.y\obj.grid_size
	obj.pop={}
	
	obj.draw=function(self)
		for i=1,#self.pop do
			circ(self.pos.x,self.pos.y,self.pop[i],2)
		end
	end
	
	obj.update=function(self)
		self.pos.x+=self.vel.x
		self.pos.y+=self.vel.y
		
		if self.pos.x<0 or
			self.pos.x>128 then
				self.vel.x*=-1
		end
		
		if self.pos.y<0 or
			self.pos.y>128 then
				self.vel.y*=-1
		end
		
		for i=1,#self.pop do
				if self.pop[i]!=nil and
				 self.pop[i]>=14 then
						del(self.pop,self.pop[i])
				elseif self.pop[i]!=nil then
					self.pop[i]+=rnd()/4
				end
		end
	
		self.g_pos.x=self.pos.x\self.grid_size
		self.g_pos.y=self.pos.y\self.grid_size
	end
	
	obj.new_pop=function(self)
		add(self.pop,1)
	end
	
	obj.nearby=function(self,a)
			local sgp=self.g_pos
			local sp=self.pos
			for o in all(a) do
				local ogp=o.g_pos
				local op=o.pos
				-- draw above
				if sgp.y<128/self.grid_size
					and ogp.y-1==sgp.y
					and abs(ogp.x-sgp.x)<=1 then
						line(op.x,op.y,sp.x,sp.y,1)
				-- draw right
				elseif sgp.x<128/self.grid_size
					and ogp.x-1==sgp.x
					and abs(ogp.y-sgp.y)<=1 then
					line(op.x,op.y,sp.x,sp.y,1)
				-- draw below
				elseif sgp.y>0
					and ogp.y+1==sgp.y
					and abs(ogp.x-sgp.x)<=1 then
					line(op.x,op.y,sp.x,sp.y,1)
				-- draw left
				elseif sgp.x>0
					and ogp.x+1==sgp.x
					and abs(ogp.y-sgp.y)<=1 then
					line(op.x,op.y,sp.x,sp.y,1)
				end
		end
	end
	
	return obj
end

function new_set()
	set={}
	set.objs={}
	for i=1,32-8*(cnt) do
		add(set.objs,new_obj())
	end
	
	set.run=function(self)
		for o in all(self.objs) do
			o:update()
			o:nearby(self.objs)
			o:draw()
		end
	end
	
	set.new_pop=function(self)
		for o in all(self.objs) do
			if rnd(1)<=0.3 then
				o:new_pop()
			end
		end
	end
	
	return set
end

dir=((1+rnd(2)\1)*2-3)

function burn()
	for i=1,1024+1024*(cnt) do
		p=0x6000+rnd(8128)\1
		poke(p-dir,peek(p)-1)
	end
end

function forward()
	v=c[1]
	del(c,v)
	c[#c+1]=v
	pal(c,1)
end

function backward()
	v=c[#c]
	del(c,v)
	for i=#c+1,1,-1 do
		if i!=1 then
			c[i]=c[i-1]
		else
			c[i]=v
		end
	end
	pal(c,1)
end

s=new_set()

for i=1,rnd()*16 do
	forward()
end

month=stat(81)
day=stat(82)

cls()
_set_fps(60)
::♥::
if t()<2 then
	print("memory probe 6",36,59,1)
	print(month.."/"..day,55,65)
else
	burn()
	s:run()
	b=(t()*60)%128
	
	pset(b,127,rnd()*16)
	pset(b,126,rnd()*16)
	pset(128-b,0,rnd()*16)
	pset(127-b,1,rnd()*16)
	pset(0,b,rnd()*16)
	pset(1,b,rnd()*16)
	pset(127,128-b,rnd()*16)
	pset(126,128-b,rnd()*16)
	
	if btnp(❎) or btnp(🅾️) then
			s:new_pop()
	end
	
	if btnp(⬆️) or btnp(⬅️) then
		forward()
	end
		
	if btnp(⬇️) or btnp(➡️) then
		backward()
	end
end
flip()
goto ♥