import random
import math
import util
import sys
from demoscript import *
sched = SchedulerSingleton_instance() # setup scheduler

joypad1=0
modplayer=32

# Create mappings
addMapping(modplayer,8,"schuif")
addMapping(modplayer,23,"bass")	 
addMapping(modplayer,3,"bass")
addMapping(modplayer,23,"revbass")
addMapping(modplayer,24,"revbass")
addMapping(modplayer,4,"bass")
addMapping(modplayer,1,"bass")
addMapping(modplayer,16,"bass")


addMapping(modplayer,19,"bright")
addMapping(modplayer,13,"chat")
addMapping(modplayer,14,"chat")


for i in [22]:  addMapping(modplayer,i,"strings")


# Start demo
# Background image
#back1=Bitmap("d:\\demos\\demo\\groupname.png")

# Flashes
flitsen=[]
for i in range(8):
	flitsen.append(util.RandomImage("d:\\demos\\demo\\overlay10.png",
									xpos=0,
									ypos=0,
									xamp=0.5,
									yamp=0.2))
	
	flitsen[-1].scaleY=FixedSlider(1,1)
	flitsen[-1].scaleY.setFixPointDamping(0)
	flitsen[-1].setAdditive(1)
	flitsen[-1].opacity.setFixPointDamping(0.99)


# Overlay thingies
imgs=[]
for i in [1,2,3,4,2,9,11]*8:
	imgs.append(util.RandomImage2("d:\\demos\\demo\\overlay"+str(i)+".png",xpos=0.7,ypos=0.5,xamp=0.2,yamp=1))
	if i==i:
 	  imgs[-1].setAdditive(1)

bass=[]
bass.append(util.RandomImage("d:\\demos\\demo\\bass.png",
							 xpos=0,
							 xamp=0,
							 yamp=0,
							 ypos=0,
							 orders=[27,28,29,30])) 

bass[-1].scaleY=FixedSlider(1,1)
bass[-1].scaleY.setFixPointDamping(0)
bass[-1].setAdditive(0)
bass[-1].opacity.setFixPointDamping(0.99)
bass[-1].preload()

scrollers = []

for i in ["creds"]:
	scrollers.append(util.ScrollImage("d:\\demos\\demo\\%s.png" %i))
	scrollers[-1].setAdditive(1)
#	scrollers[-1].opacity.setFixPoint

	scrollers[-1].preload()

scrollbars = []

for i in ["groupname","title"]:
	scrollbars.append(util.RandomImage("d:\\demos\\demo\\%s.png" % i,
									   xpos=0,
									   xamp=0,
									   yamp=0,
									   ypos=0,
									   scaling=1))

	scrollbars[-1].scaleY=FixedSlider(1,1)
	scrollbars[-1].scaleY.setFixPointDamping(0)
	scrollbars[-1].setAdditive(1)
	scrollbars[-1].opacity.setFixPointDamping(0.997)
#	scrollbars[-1].opacity.setNoiseAmount(0.2)


designbass = []

for i in [0,1,2,3]:
	designbass.append(util.RandomImage("d:\\demos\\demo\\sn0%s.png" % i,
									   xpos=0,
									   xamp=0,
									   yamp=0,
									   ypos=0,
									   orders=range(19,26),
									   scaling=0))

	designbass[-1].scaleY=FixedSlider(1,1)
	designbass[-1].scaleY.setFixPointDamping(0)
	designbass[-1].setAdditive(1)
	designbass[-1].opacity.setFixPointDamping(0.99)
	designbass[-1].preload()
	

lovetpb = []
for i in ["on","off"]:
	lovetpb.append(util.RandomImage("d:\\demos\\demo\\coat_%s.png" % i,
									   xpos=0,
									   xamp=0,
									   yamp=0,
									   ypos=-0.67,
									   scaling=0))

	lovetpb[-1].scaleY=FixedSlider(1,1)
	lovetpb[-1].scaleY.setFixPointDamping(0)
	lovetpb[-1].setAdditive(1)
	lovetpb[-1].opacity.setFixPointDamping(0.999)
	lovetpb[-1].preload()

imgSeq=util.HackImageSeq(images=imgs+flitsen+bass+designbass+scrollbars+lovetpb+scrollers,
						 autosize=len(imgs),
						 events=[["strings",5,len(imgs),len(imgs)+len(flitsen)],
								 ["bass",2,len(imgs)+len(flitsen),len(imgs)+len(flitsen)+1],
								 ["bass",1,len(imgs)+len(flitsen)+len(bass),len(imgs)+len(flitsen)+len(bass)+len(designbass)],
								 ["revbass",1,len(imgs)+len(flitsen)+len(bass)+len(designbass),len(imgs)+len(flitsen)+len(bass)+len(designbass)+len(scrollbars)]])


city_k=0
def cityImgCallBack(event):
	global city_k
	if getModuleOrder() in range(9,19):
		lovetpb[city_k%2].revive()
		lovetpb[(city_k+1)%2].opacity.setTargetValue(0)
		city_k+=1
		
imgSeq.callBacks.append(["bass",cityImgCallBack])		


# city scene
city=util.SimpleScene("d:\\demos\\demo\\city.lzma",
					  nextCamera=["bass","bass2"],
					  reset=["bass","bass2"],
					  colors=[[1,"bass"],
							  [6,"bass"],
							  [7,"bass"],
							  [5,"bass"],
							  [8,"bass"],
							  [9,"bass"],
							  # light up the pilars
							  [12,"strings",1,1,1],
							  [13,"strings",1,1,1],
							  [15,"strings",1,1,1],
							  [16,"strings",1,1,1],
							  [17,"strings",1,1,1],
							  # light up the pilars in red
  							  [12,"bright",1,0,0,0.5],
							  [13,"bright",1,0,0,0.5],
							  [15,"bright",1,0,0,0.5],
							  [16,"bright",1,0,0,0.5],
							  [17,"bright",1,0,0,0.5]],
					  # slowdown camera
					  speeds=[["bright",4000]],
					  p=0.9)

#set light pilars to invisible
for i in [12,13,15,16,17]:
	city.getScene().getObjects()[i].red=FixedSlider(0,0)
	city.getScene().getObjects()[i].green=FixedSlider(0,0)
	city.getScene().getObjects()[i].blue=FixedSlider(0,0)

def onCitySyncMiss(scene):
	objs=scene.getScene().getObjects()
	for i in [1,5,6,7,8,9]:
		objs[i].green.setTargetValue(0)
		objs[i].red.setTargetValue(0)
		objs[i].blue.setTargetValue(0)		

city.missCallBacks.append(["bass",onCitySyncMiss])

cityTransforms=[Scaling(),Scaling(),Scaling(),Scaling(),Scaling(),Scaling()]
k=0
for i in [1,5,6,7,8,9]:
	city.getScene().getObjects()[i].addTransform(cityTransforms[k])
	cityTransforms[k].z=FixedSlider(3,3)
	k+=1

def onCityBass(scene,event):

#	s=random.sample(cityTransforms,5)
	
	for s in xrange(5):
		s=random.choice(cityTransforms)
		s.z.setTargetValue(1)
		s.z.setDamping(0.95)



		
	
city.callBacks.append(["bass",onCityBass])




origblob=util.SimpleScene("d:\\demos\\demo\\origblob.lzma",
					  nextCamera=["bass","bass2"],
					  reset=["bass","bass2"],
					  colors=[[0,"bass",1,0,1],
							  # rings around blob
							  [5,"bass"],
							  [6,"bass"],
							  [7,"bass"],
							  # tunnel (6th parameter is variation)
							  [8,"bass",0,0,0],
							  [9,"bass",0,0,0],
							  [8,"bass",1,0,0,0.9],
							  [9,"bass",1,0,0,0.9],					  
							  [10,"bass",0,0,0]],
					  p=0.5)

scrollerStarted=0
def startScroller(scene,event):
	global scrollerStarted
	global scrollers
	if getModuleOrder() == 36 and scrollerStarted == 0:
		scrollers[0].revive()
		scrollerStarted=1





transforms=[Rotation(0,1,0),Rotation(0,1,0)]



blob=util.SimpleScene("d:\\demos\\demo\\blob.lzma",
					  nextCamera=["bass","bass2"],
					  reset=["bass","bass2"],
					  transforms=[[3,transforms[0],"bass"],
								  [4,transforms[1],"bass"]],
					  colors=[[0,"bass",0,0,0],
							  [7,"bass",0,0,0,0,0.6],
							  [7,"chat",2,2,2,1,0.7],
							  [10,"bass",2,2,2,0],							  
  							  [11,"bass",0.7,0.7,0.7,0,0.2]],

					  p=0.5)
for i in [7,10,9,8]:

	blob.getScene().getObjects()[i].red.setDamping(0.96)
	blob.getScene().getObjects()[i].green.setDamping(0.96)
	blob.getScene().getObjects()[i].blue.setDamping(0.96)
	blob.getScene().getObjects()[i].red.setFixPointDamping(0.96)
	blob.getScene().getObjects()[i].green.setFixPointDamping(0.96)
	blob.getScene().getObjects()[i].blue.setFixPointDamping(0.96)

blob.getScene().getObjects()[11].red=FixedSlider(0,0)
blob.getScene().getObjects()[11].green=FixedSlider(0,0)
blob.getScene().getObjects()[11].blue=FixedSlider(0,0)


origblob.callBacks.append(["bass",startScroller])

tunnel=util.SimpleScene("d:\\demos\\demo\\geval2.lzma",
						nextCamera=["bass","bass2"],
						reset=["bass","bass2"],
						colors=[[1,"bass"],
								[6,"bass"],
								[7,"bass"],
								[5,"bass"],
								[8,"bass"],
								[9,"bass"]],
						p=0.5)

scene1=util.SimpleScene("d:\\demos\\demo\\geval1.lzma",
						nextCamera=["bass","bass2"],
						reset=["bass","bass2"],
						curveEvents=[[10,"bass"],[11,"bass2"]],
						cameraOrder=[1,1,2,1,0,1,2,0,3,2,0,1,2,4,0,3,0,5,1,6,0,3]*4+[4,6,4,5,5,5,3,2,5,3,0,4,4,3,6,5,4,3,2,1,5,2]*4,p=0.5)
#back1.connectOutput(scene1,"in")
scene1.speed=FixedSlider(32000,32000)
tunnel.speed=FixedSlider(32000,32000)
city.speed=FixedSlider(32000,32000)
#blob.speed=FixedSlider(62000,62000)



sched.schedule( 7,        62-7, blob,SI_A,0)
sched.schedule(62,        67,   city,SI_A,0)
#              130        184
sched.schedule(60*2+10,   54,   scene1,SI_A,0)
sched.schedule(60*3+5,    55,   tunnel,SI_A,0)

# credits hier 
sched.schedule(60*4,      27    ,origblob,SI_A,0)
sched.schedule(60*4+27,      9+26+28,    blob,SI_A,0)


# Setup postprocessing and overlays
# Blur filter
blur=util.SimpleBlur(events=["bass","bass2"])
blur2=util.SimpleBlur(events=["bass","bass2"])


# setup distortion callback 
def distortCallback(self):
  if getTime() > 60 and getTime() < 190  and getEvent(EC_NOTE_ON,"bass"):
	  
    self.amplitude.setTargetValue(frand(-1,1))
    self.scrollx.setTargetValue(frand(-1,1))
    self.scrolly.setTargetValue(frand(-1,1))
dist=util.SimpleRipple()
dist.callbacks.append(distortCallback)

# chain effects
dist.connectOutput(blur,"in")

sched.schedule(0,32,blur,SI_POST,0)
sched.schedule(32,98,blur2,SI_POST,0)
sched.schedule(130,54,blur,SI_POST,0)
sched.schedule(184,328-184+2+2,blur2,SI_POST,0)


sched.schedule(7,5*60+21+2+2,imgSeq,SI_SUPER,0)

# Play music
playModule("d:\\demos\\demo\\_rf02701.xm")

