--track0:Timing,0,9999999,0,1
--track1:Period,1,100,1,1
--track2:Depth,1,30,5,1
--track3:Facing,-360,360,0
--check0:Setup Mode,1
--dialog:pos,pos={};Anchor counr,num=4;Slimness,masu=50;Mode,mode=0;Coeff,bi=5;Delay,tsub=0;Decay,att=10;Erect,at=10;PeriodDiff,tmpsub=10;Smoothing,nuru=0;Vertex per layer,push={0,0};TimeCtrl/chk,shu=1;NewAlgo/chk,newcalc=0;InvertDomain/chk,rever=0;
local shita=1
local k=1
for i=1,10 do
if obj.getoption("script_name",-i)=="BreastShake" then
k=k+1
end
end
if k==1 then
dd_OPkey=0
end
local num=math.floor(num)
local temp=obj.track1/obj.framerate*0.1*360
local yure=math.floor(obj.track2)
obj.setanchor("pos",num,"loop")
masu=math.floor(masu)
if masu<5 then
masu=5
end
if att<1 then
att=1
end
local f=obj.frame-tsub
if shu==0 then
f=math.floor(obj.track0)-tsub
end
if type(_OPax)~="table" then
_OPax={}
end
if type(_OPax[obj.layer])~="table" then
_OPax[obj.layer]={}
end
if type(_OPay)~="table" then
_OPay={}
end
if type(_OPay[obj.layer])~="table" then
_OPay[obj.layer]={}
end
if type(_OPfx)~="table" then
_OPfx={}
end
if type(_OPfx[obj.layer])~="table" then
_OPfx[obj.layer]={}
_OPfx[obj.layer][k]=f
end
if type(_OPfy)~="table" then
_OPfy={}
end
if type(_OPfy[obj.layer])~="table" then
_OPfy[obj.layer]={}
_OPfy[obj.layer][k]=f
end
local w,h=obj.getpixel()
local w1=w
local h1=h
if w1%masu~=0 then
w1=w1-w1%masu+masu
end
if h1%masu~=0 then
h1=h1-h1%masu+masu
end
if w1%2==1 then
w1=w1+1
end
if h1%2==1 then
h1=h1+1
end
local yo=math.floor(w1/masu)
local ta=math.floor(h1/masu)
local han={}
local yo1=yo+1
local ta1=ta+1
local yota=(yo+1)*(ta+1)
for i=1,yota do
han[i]=0
end
if dd_OPkey~=0 and mode~=5 then
for d=1,dd_OPkey do
ddpoint[d]={}
for i=1,yota do
ddpoint[d][i]=0
end
end
end
for n=1,num do
local n1=n*2-1
local n2=(n%num+1)*2-1
local n3=n*2
local n4=(n%num+1)*2
local n5=(pos[n4]-pos[n3])/(pos[n2]-pos[n1])
for i=1,yo1 do
for j=1,ta1 do
local nn=j+ta1*(i-1)
if math.min(pos[n1],pos[n2])<=-w1*0.5+masu*(i-1) and -w1*0.5+masu*(i-1)<math.max(pos[n1],pos[n2]) and n5*(-w1*0.5+masu*(i-1)-pos[n1])+pos[n3]<=-h1*0.5+masu*(j-1) then
han[nn]=han[nn]+1
end
end
end
end

if dd_OPkey~=0 and mode~=5 then
for d=1,dd_OPkey do
for n=1,ddnum[d] do
local n1=n*2-1
local n2=(n%ddnum[d]+1)*2-1
local n3=n*2
local n4=(n%ddnum[d]+1)*2
local n5=(ddpos[d][n4]-ddpos[d][n3])/(ddpos[d][n2]-ddpos[d][n1])
for i=1,yo1 do
for j=1,ta1 do
local nn=j+ta1*(i-1)
if math.min(ddpos[d][n1],ddpos[d][n2])<=-w1*0.5+masu*(i-1) and -w1*0.5+masu*(i-1)<math.max(ddpos[d][n1],ddpos[d][n2]) and n5*(-w1*0.5+masu*(i-1)-ddpos[d][n1])+ddpos[d][n3]<=-h1*0.5+masu*(j-1) then
ddpoint[d][nn]=ddpoint[d][nn]+1
end
end
end
end
end

for d=1,dd_OPkey do
for i=1,yota do
if ddpoint[d][i]%2==ddrev[d] and han[i]%2==1 then
han[i]=0
end
end
end

end

if rever==1 then
for i=1,yota do
if han[i]%2==0 then
han[i]=1
else
han[i]=0
end
end
end
local point={}
local ori_point={}
for i=1,yo1 do
for j=1,ta1 do
local nn=j+ta1*(i-1)
point[nn]={}
point[nn][1]=-w1*0.5+masu*(i-1)
point[nn][2]=-h1*0.5+masu*(j-1)
ori_point[nn]={}
ori_point[nn][1]=masu*(i-1)
ori_point[nn][2]=masu*(j-1)
end
end
if type(_OPax[obj.layer][k])~="table" then
_OPax[obj.layer][k]={}
for i=1,yota do
_OPax[obj.layer][k][i]=0
end
end
if type(_OPay[obj.layer][k])~="table" then
_OPay[obj.layer][k]={}
for i=1,yota do
_OPay[obj.layer][k][i]=0
end
end
if obj.frame==0 then
for i=1,yota do
_OPax[obj.layer][k][i]=0
_OPay[obj.layer][k][i]=0
end
_OPfx[obj.layer][k]=f
_OPfy[obj.layer][k]=f
end

function LURD(w,i,j)
local rep=0
local nn=j+ta1*(i-1)
if w=="l" then
while(0) do
rep=rep+1
if nn-ta*rep<=0 then
break
elseif han[nn-ta*rep]%2==0 then
break
end
end
elseif w=="u" then
while(0) do
rep=rep+1
if (nn-rep)%ta1==0 then
break
elseif han[nn-rep]%2==0 then
break
end
end
elseif w=="r" then
while(0) do
rep=rep+1
if yota<nn+rep*ta1 then
break
elseif han[nn+ta1*rep]%2==0 then
break
end
end
elseif w=="d" then
while(0) do
rep=rep+1
if (nn+rep)%ta1==1 then
break
elseif han[nn+rep]%2==0 then
break
end
end
end
return rep
end
function twop(s,t,i,table)
local atai
if t==1 then
if 0<=s then
atai=table[i][3]
else
atai=table[i][1]
end
else
if 0<=s then
atai=table[i][4]
else
atai=table[i][2]
end
end
return atai*s*0.8
end
local lurd={}
if newcalc==1 then
for i=1,yo1 do
for j=1,ta1 do
local mm=j+ta1*(i-1)
if han[mm]%2==1 then
lurd[mm]={}
lurd[mm][1]=LURD("l",i,j)
lurd[mm][2]=LURD("u",i,j)
lurd[mm][3]=LURD("r",i,j)
lurd[mm][4]=LURD("d",i,j)
if lurd[mm][1]<lurd[mm][3] then
lurd[mm][3]=math.floor((lurd[mm][1]+lurd[mm][1])*0.5)
elseif lurd[mm][3]<lurd[mm][1] then
lurd[mm][1]=math.floor((lurd[mm][1]+lurd[mm][1])*0.5)
end
if lurd[mm][2]<lurd[mm][4] then
lurd[mm][4]=math.floor((lurd[mm][2]+lurd[mm][4])*0.5)
elseif lurd[mm][4]<lurd[mm][2] then
lurd[mm][2]=math.floor((lurd[mm][2]+lurd[mm][4])*0.5)
end
end
end
end
end

local MY3=masu*yure/30
for i=1,yota do
if han[i]%2==1 then
if mode==0 then
local objdeg=math.rad(obj.track3)
if newcalc==0 then
point[i][1]=point[i][1]+MY3*math.sin(math.rad(temp*f))*math.sin(objdeg)*0.9
point[i][2]=point[i][2]+MY3*math.sin(math.rad(temp*f))*math.cos(objdeg)*0.9
else
point[i][1]=point[i][1]+MY3*twop(math.sin(math.rad(temp*f))*math.sin(objdeg),1,i,lurd)*0.9
point[i][2]=point[i][2]+MY3*twop(math.sin(math.rad(temp*f))*math.cos(objdeg),2,i,lurd)*0.9
end
elseif mode==1 then
local objdeg=math.floor(obj.track3)
attc=f/obj.rand(math.ceil(at*0.1),at,11*i,objdeg)
if attc<0 then
attc=0
elseif attc>1 or at<=0 then
attc=1
end
local ran1=obj.rand(100-tmpsub,100+tmpsub,i,objdeg)
local ran2=obj.rand(-tmpsub*10,tmpsub*10,7*i,objdeg)*3.6/obj.framerate
local TEMP=math.rad(temp*ran1*0.01*f+obj.rand(0,math.abs(objdeg),3*i,objdeg))
local POW=obj.rand(50,100,math.floor(TEMP/(2*math.pi)),objdeg)*0.01
if ran1==0 then
ran1=1
end
if ran2==0 then
ran2=1
end
if newcalc==0 then
point[i][1]=point[i][1]+MY3*math.sin(TEMP)*math.sin(math.rad(f*ran2+obj.rand(0,360,13*i,objdeg)))*attc*0.9*POW
point[i][2]=point[i][2]+MY3*math.sin(TEMP)*math.cos(math.rad(f*ran2+obj.rand(0,360,13*i,objdeg)))*attc*0.9*POW
else
point[i][1]=point[i][1]+MY3*twop(math.sin(TEMP)*math.sin(math.rad(f*ran2+obj.rand(0,360,13*i,objdeg))),1,i,lurd)*attc*0.9*POW
point[i][2]=point[i][2]+MY3*twop(math.sin(TEMP)*math.cos(math.rad(f*ran2+obj.rand(0,360,13*i,objdeg))),2,i,lurd)*attc*0.9*POW
end
elseif mode==2 then
local L=((push[1]-point[i][1])^2+(push[2]-point[i][2])^2)^0.5
local deg=math.atan((push[2]-point[i][2])/(push[1]-point[i][1]))
if(push[2]-point[i][2]==0 and push[1]-point[i][1]==0) then
deg=math.pi*0.5
end
if point[i][1]<=push[1] then
deg=deg+math.pi
end
hapuku=att^(-math.rad(temp*(f-L*bi*0.1))*0.01)
if 1<hapuku then
hapuku=1
end
if at==0 then
local deg2=math.rad(temp*f)
local deg3=deg+math.pi+math.rad(obj.track3)
if newcalc==0 then
point[i][1]=point[i][1]+MY3*math.sin(deg2)*math.cos(deg3)*hapuku*0.9
point[i][2]=point[i][2]+MY3*math.sin(deg2)*math.sin(deg3)*hapuku*0.9
else
point[i][1]=point[i][1]+MY3*twop(math.sin(deg2)*math.cos(deg3),1,i,lurd)*hapuku*0.9
point[i][2]=point[i][2]+MY3*twop(math.sin(deg2)*math.sin(deg3),2,i,lurd)*hapuku*0.9
end
elseif L*bi*0.1<=f then
attc=(f-L*bi*0.1)/at
if 1<attc then
attc=1
elseif attc<=0 then
attc=0
end
local deg2=math.rad(temp*(f-L*bi*0.1))
local deg3=deg+math.pi+math.rad(obj.track3)
if newcalc==0 then
point[i][1]=point[i][1]+MY3*math.sin(deg2)*math.cos(deg3)*hapuku*attc*0.9
point[i][2]=point[i][2]+MY3*math.sin(deg2)*math.sin(deg3)*hapuku*attc*0.9
else
point[i][1]=point[i][1]+MY3*twop(math.sin(deg2)*math.cos(deg3),1,i,lurd)*hapuku*attc*0.9
point[i][2]=point[i][2]+MY3*twop(math.sin(deg2)*math.sin(deg3),2,i,lurd)*hapuku*attc*0.9
end
else
point[i][1]=point[i][1]
point[i][2]=point[i][2]
end
elseif mode==3 or mode==4 then
local x=0
local y=0
local kai=0
local la=push[1]
if la~=0 then
for n=-math.ceil(nuru*0.5),nuru do
if 0<=obj.frame-(n+1)-tsub and obj.frame-n-tsub<=obj.totalframe then
local nurux=obj.getvalue("layer"..la..".X",(obj.frame-n-tsub)/obj.framerate)-obj.getvalue("layer"..la..".X",(obj.frame-(n+1)-tsub)/obj.framerate)
local nuruy=obj.getvalue("layer"..la..".Y",(obj.frame-n-tsub)/obj.framerate)-obj.getvalue("layer"..la..".Y",(obj.frame-(n+1)-tsub)/obj.framerate)
x=x+nurux
y=y+nuruy
kai=kai+1
end
end
else
for n=-math.ceil(nuru*0.5),nuru do
local nurux=obj.getvalue("X",(obj.frame-n-tsub)/obj.framerate)-obj.getvalue("X",(obj.frame-(n+1)-tsub)/obj.framerate)
local nuruy=obj.getvalue("Y",(obj.frame-n-tsub)/obj.framerate)-obj.getvalue("Y",(obj.frame-(n+1)-tsub)/obj.framerate)
x=x+nurux
y=y+nuruy
kai=kai+1
end
end
if kai~=0 then
x=x/(kai)
y=y/(kai)
end
local dx=0
local dy=0
--local z,L,deg
if mode==4 then
z=0
kai=0
if la~=0 then
for n=-math.ceil(nuru*0.5),nuru do
if 0<=obj.frame-(n+1)-tsub and obj.frame-n-tsub<=obj.totalframe then
local nuruz=obj.getvalue("layer"..la..".rz",(obj.frame-n-tsub)/obj.framerate)-obj.getvalue("layer"..la..".rz",(obj.frame-(n+1)-tsub)/obj.framerate)
z=z+nuruz
kai=kai+1
end
end
else
for n=-math.ceil(nuru*0.5),nuru do
local nuruz=obj.getvalue("rz",(obj.frame-n-tsub)/obj.framerate)-obj.getvalue("rz",(obj.frame-(n+1)-tsub)/obj.framerate)
z=z+nuruz
kai=kai+1
end
end
if kai~=0 then
z=z/(kai)
end
L=((obj.cx-point[i][1])^2+(obj.cy-point[i][2])^2)^0.5
deg=math.atan((obj.cy-point[i][2])/(obj.cx-point[i][1]))
if (obj.cy-point[i][2])==0 and (obj.cx-point[i][1])==0 then
deg=math.pi*0.5
end
if point[i][1]<=obj.cx then
deg=deg+math.pi
end
dx=-L*math.rad(z)*(math.sin(deg)+math.rad(z)*math.cos(deg))
dy=L*math.rad(z)*(math.cos(deg)-math.rad(z)*math.sin(deg))
end
local deg2=math.rad(obj.getvalue("rz"))
local delx=x*math.cos(deg2)+y*math.sin(deg2)
local dely=y*math.cos(deg2)+x*math.sin(deg2)
x=delx+dx
y=dely+dy
local ax=-x*bi*0.01
local ay=-y*bi*0.01
if -0.001<=x and x<=0.001 then
ax=_OPax[obj.layer][k][i]*math.cos(math.rad(temp*(f-_OPfx[obj.layer][k])))*att^(-math.rad(temp*(f-_OPfx[obj.layer][k]))*0.01)
end
if -0.001<=y and y<=0.001 then
ay=_OPay[obj.layer][k][i]*math.cos(math.rad(temp*(f-_OPfy[obj.layer][k])))*att^(-math.rad(temp*(f-_OPfy[obj.layer][k]))*0.01)
end
if 1<ax then
ax=1
elseif ax<-1 then
ax=-1
end
if 1<ay then
ay=1
elseif ay<-1 then
ay=-1
end
if newcalc==0 then
point[i][1]=point[i][1]+MY3*ax*0.9
point[i][2]=point[i][2]+MY3*ay*0.9
else
point[i][1]=point[i][1]+MY3*twop(ax,1,i,lurd)*0.9
point[i][2]=point[i][2]+MY3*twop(ay,2,i,lurd)*0.9
end
if not (-0.001<=x and x<=0.001) then
_OPax[obj.layer][k][i]=ax
_OPfx[obj.layer][k]=f
end
if not (-0.001<=y and y<=0.001) then
_OPay[obj.layer][k][i]=ay
_OPfy[obj.layer][k]=f
end
end
end
end

if mode~=5 then
dd_OPkey=0
ddpos=nil
ddpoint=nil
ddrev=nil
ddnum=nil
else
if dd_OPkey==0 then
ddpos={}
ddpoint={}
ddrev={}
ddnum={}
end
dd_OPkey=dd_OPkey+1
ddpos[dd_OPkey]={}
ddrev[dd_OPkey]=1-rever
ddnum[dd_OPkey]=num
for i=1,num do
ddpos[dd_OPkey][2*i-1]=pos[2*i-1]
ddpos[dd_OPkey][2*i]=pos[2*i]
end
end

obj.setoption("dst","tmp",w1,h1)
obj.draw()
obj.load("tempbuffer")
obj.setoption("dst","tmp",w,h)
if not obj.check0 then
obj.draw()
if shita==1 then
obj.setoption("blend","alpha_sub")
for i=1,yo*ta do
	local nn=math.ceil(i/ta)
	local v1=ori_point[i+nn-1][1]
	local u1=ori_point[i+nn-1][2]
	local v2=ori_point[i+nn+ta][1]
	local u2=ori_point[i+nn+ta][2]
	local v3=ori_point[i+nn+ta+1][1]
	local u3=ori_point[i+nn+ta+1][2]
	local v4=ori_point[i+nn][1]
	local u4=ori_point[i+nn][2]
if han[i+nn-1]%2==1 or han[i+nn+ta]%2==1 or han[i+nn+ta+1]%2==1 or han[i+nn]%2==1 then
	obj.drawpoly(v1-w1*0.5,u1-h1*0.5,0,v2-w1*0.5,u2-h1*0.5,0,v3-w1*0.5,u3-h1*0.5,0,v4-w1*0.5,u4-h1*0.5,0, v1,u1,v2,u2,v3,u3,v4,u4)
end
end
obj.setoption("blend","alpha_add")
end
for i=1,yo*ta do
	local nn=math.ceil(i/ta)
	local x1=point[i+nn-1][1]
	local y1=point[i+nn-1][2]
	local x2=point[i+nn+ta][1]
	local y2=point[i+nn+ta][2]
	local x3=point[i+nn+ta+1][1]
	local y3=point[i+nn+ta+1][2]
	local x4=point[i+nn][1]
	local y4=point[i+nn][2]
	local v1=ori_point[i+nn-1][1]
	local u1=ori_point[i+nn-1][2]
	local v2=ori_point[i+nn+ta][1]
	local u2=ori_point[i+nn+ta][2]
	local v3=ori_point[i+nn+ta+1][1]
	local u3=ori_point[i+nn+ta+1][2]
	local v4=ori_point[i+nn][1]
	local u4=ori_point[i+nn][2]
if han[i+nn-1]%2==1 or han[i+nn+ta]%2==1 or han[i+nn+ta+1]%2==1 or han[i+nn]%2==1 then
	obj.drawpoly(x1,y1,0,x2,y2,0,x3,y3,0,x4,y4,0, v1,u1,v2,u2,v3,u3,v4,u4)
end
end
obj.setoption("blend",0)
else
for i=1,yo*ta do
	local nn=math.ceil(i/ta)
	local v1=ori_point[i+nn-1][1]
	local u1=ori_point[i+nn-1][2]
	local v2=ori_point[i+nn+ta][1]
	local u2=ori_point[i+nn+ta][2]
	local v3=ori_point[i+nn+ta+1][1]
	local u3=ori_point[i+nn+ta+1][2]
	local v4=ori_point[i+nn][1]
	local u4=ori_point[i+nn][2]
if han[i+nn-1]%2==1 or han[i+nn+ta]%2==1 or han[i+nn+ta+1]%2==1 or han[i+nn]%2==1 then
	obj.drawpoly(v1-w1*0.5,u1-h1*0.5,0,v2-w1*0.5,u2-h1*0.5,0,v3-w1*0.5,u3-h1*0.5,0,v4-w1*0.5,u4-h1*0.5,0, v1,u1,v2,u2,v3,u3,v4,u4)
end
end
obj.copybuffer("cache:OP","tmp")
obj.setoption("dst","tmp",w,h)
obj.draw()
obj.copybuffer("obj","cache:OP")
if mode~=5 then
obj.effect("Monochromatic","color",0xff)
obj.effect("Color compensation","Hue",4*yure)
else
obj.effect("Monochromatic","color",0,"Keep luminance",0)
end
obj.draw()
for i=1,num do
obj.load("figure","Circle",0xff,10)
obj.effect("Color compensation","Hue",4*yure)
obj.draw(pos[i*2-1],pos[i*2])
obj.setfont("",30,0,0xff)
obj.load("text",i)
obj.effect("Color compensation","Hue",4*yure)
obj.effect("Reversal","Hue inversion",1)
obj.draw(pos[i*2-1],pos[i*2])
end
if mode==2 then
obj.load("figure","Circle",0,30)
obj.draw(push[1],push[2])
end
end
obj.load("tempbuffer")

if mode==6 then
obj.setoption("dst","frm")
obj.draw()
local h3=obj.screen_h
obj.setfont("",50,4,0xffffff,0)
obj.load("text","Usage")
obj.draw(0,-h3*0.3)
obj.setfont("",40,4,0xffffff,0)
obj.load("text","Mode 0: Normal shake")
obj.draw(0,-h3*0.2)
obj.setfont("",35,4,0xffffff,0)
obj.load("text","Parameters: Period, Depth, Facing")
obj.draw(0,-h3*0.2+35)
obj.setfont("",40,4,0xffffff,0)
obj.load("text","Mode 1: Irregular shake")
obj.draw(0,-h3*0.2+75)
obj.setfont("",35,4,0xffffff,0)
obj.load("text","Parameters: Period, Depth, Facing,Erect,PeriodDiff")
obj.draw(0,-h3*0.2+110)
obj.setfont("",40,4,0xffffff,0)
obj.load("text","Mode 2:Fixed point shaking")
obj.draw(0,-h3*0.2+150)
obj.setfont("",35,4,0xffffff,0)
obj.load("text","Parameters:Period, Depth, Facing, Coeff, Decay, Erect, Vertex per layer")
obj.draw(0,-h3*0.2+185)
obj.setfont("",40,4,0xffffff,0)
obj.load("text","Mode 3: Shake according to body movement (No effect from rotation)")
obj.draw(0,-h3*0.2+225)
obj.setfont("",35,4,0xffffff,0)
obj.load("text","Parameters: Period, Depth, Coeff, Decay, Vertex per layer, Smoothing")
obj.draw(0,-h3*0.2+260)
obj.setfont("",40,4,0xffffff,0)
obj.load("text","Mode 4: Shake according to body movement")
obj.draw(0,-h3*0.2+300)
obj.setfont("",35,4,0xffffff,0)
obj.load("text","Parameters: Period, Depth, Coeff, Decay, Vertex per layer, Smoothing")
obj.draw(0,-h3*0.2+335)
obj.setfont("",40,4,0xffffff,0)
obj.load("text","Mode 5: Define exclusion domain")
obj.draw(0,-h3*0.2+375)
obj.setfont("",35,4,0xffffff,0)
obj.load("text","Just set the domain and then add other modes below")
obj.draw(0,-h3*0.2+410)
end


num=nil
masu=nil
mode=nil
bi=nil
tsub=nil
att=nil
at=nil
tmpsub=nil
nuru=nil
push=nil
shu=nil
newcalc=nil
rever=nil
shita=nil
