@name IT
@outputs E:entity Open Pos:vector P2:entity Ang:angle Ok Eggs:array Count
@persist Op Rot Up Egg:entity Ply:array Possesed Co Ang2:angle Co2 HapE:array Timer
@trigger all
@model models/hunter/blocks/cube025x025x025.mdl
runOnLast(1)
runOnTick(1)

if(first() | duped()){
E = entity()
propSpawnEffect(0)

E:setAlpha(0)


HapE[1,string] = "vo/ravenholm/madlaugh01.wav"
HapE[2,string] = "vo/ravenholm/madlaugh02.wav"
HapE[3,string] = "vo/ravenholm/madlaugh03.wav"
HapE[4,string] = "vo/ravenholm/madlaugh04.wav"
HapE[5,string] = "npc/zombie/zombie_pain1.wav"
HapE[6,string] = "npc/zombie/zombie_die3.wav"
HapE[7,string] = "npc/zombie/zombie_pain2.wav"
HapE[8,string] = "npc/zombie/zombie_die2.wav"
HapE[9,string] = "npc/zombie/zombie_voice_idle1.wav"
HapE[10,string] = "npc/zombie/moan_loop4.wav"

holoCreate(1)
holoModel(1,"cube")
holoScaleUnits(1,vec(10,10,1))
holoPos(1,E:toWorld(vec(0,0,-6)))
holoColor(1,vec(90,90,90))
holoParent(1,E)

## Rotate Cube 1
holoCreate(2)
holoModel(2,"cube")
holoScaleUnits(2,vec(2,2,2))
holoColor(2,vec(90,90,90))
holoPos(2,E:toWorld(vec(0,4.5,-6)))
holoParent(2,E) 

## Rotate Cube 2
holoCreate(3)
holoModel(3,"cube")
holoScaleUnits(3,vec(2,2,2))
holoColor(3,vec(90,90,90))
holoPos(3,E:toWorld(vec(0,-4.5,-6)))
holoParent(3,E) 

## Rotate Cube 3
holoCreate(4)
holoModel(4,"cube")
holoScaleUnits(4,vec(2,2,2))
holoColor(4,vec(90,90,90))
holoPos(4,E:toWorld(vec(4.5,0,-6)))
holoParent(4,E) 

## Rotate Cube 3
holoCreate(5)
holoModel(5,"cube")
holoScaleUnits(5,vec(2,2,2))
holoColor(5,vec(90,90,90))
holoPos(5,E:toWorld(vec(-4.5,0,-6)))
holoParent(5,E) 

## Cube 1
holoCreate(6)
holoModel(6,"cube")
holoScaleUnits(6,vec(1,8,8))
holoColor(6,vec(90,90,90))
holoPos(6,E:toWorld(vec(4.3,0,-2)))
holoParent(6,4) 

## Cube 2
holoCreate(7)
holoModel(7,"cube")
holoScaleUnits(7,vec(1,8,8))
holoColor(7,vec(90,90,90))
holoPos(7,E:toWorld(vec(-4.3,0,-2)))
holoParent(7,5)

## Cube 3
holoCreate(8)
holoModel(8,"cube")
holoScaleUnits(8,vec(8,1,8))
holoColor(8,vec(90,90,90))
holoPos(8,E:toWorld(vec(0,-4.3,-2)))
holoParent(8,3)

## Cube 4
holoCreate(9)
holoModel(9,"cube")
holoScaleUnits(9,vec(8,1,8))
holoColor(9,vec(90,90,90))
holoPos(9,E:toWorld(vec(0,4.3,-2)))
holoParent(9,2)  

## IT
holoCreate(12)
holoModel(12,"models/props_phx/misc/egg.mdl")
holoPos(12,E:toWorld(vec(0,0,-5)))
holoColor(12,vec(255,240,0))
holoMaterial(12,"models/debug/debugwhite")
holoParent(12,E)    

## Cube TOP
holoCreate(11)
holoModel(11,"cube")
holoScaleUnits(11,vec(8,8,1))
holoColor(11,vec(90,90,90))
holoPos(11,E:toWorld(vec(0,0,2)))
holoParent(11,8)  

Up = -5

for(I=6,11){
holoMaterial(I,"models/props_lab/xencrystal_sheet")    
}

}

if(owner():keyUse() & owner():aimEntity() == E & owner():pos():distance(E:pos()) <= 100 & Open == 0){
Open = 1  
E:soundPlay(1,0,"buttons/button6.wav")  
}


Rot+=5
holoAng(12,ang(0,Rot,0))

if(Open == 1){
    
if(Op == 1){
E:soundPlay(2,0,"music/stingers/hl1_stinger_song8.mp3")    
}

if(Op <= 90){
Op+=0.5
}else{
Open = 2
}

holoAng(2,ang(0,0,-Op))
holoAng(3,ang(0,0,Op))
holoAng(4,ang(Op,0,0))
holoAng(5,ang(-Op,0,0))  
}


if(Open == 2){
    
if(Up <= 15){
Up+=0.05
}else{
E:soundPlay(3,0,"vo/ravenholm/madlaugh03.wav")
soundPitch(3,80)
Open = 3
}

holoPos(12,E:toWorld(vec(0,0,Up)))    
}

if(Open == 3){

Egg = propSpawn("models/props_phx/misc/egg.mdl",holoEntity(12):pos(),ang(0,0,0),1)  
holoDelete(12)  
Egg:setColor(255,240,0)
Egg:setMaterial("models/debug/debugwhite")
Open = -1   
Ok = 1 
}


if(Ok == 1){

findByModel("models/props_phx/misc/egg.mdl")
Eggs = findToArray()
Count = Eggs:count()

if(Count <= 0){
P2 = propSpawn("models/props_c17/doll01.mdl",Pos+vec(0,0,10),Ang,0)     
Ok = 2
P2:soundPlay(4,0,"ambient/creatures/town_child_scream1.wav")
}else{
Pos = Egg:pos() 
Ang = Egg:angles()
}

}

if(Ok == 2){
Possesed = 1 
  
Ang2 = ang(cos(curtime()*10)*randint(1,5),0,0)

rangerHitWater(0)
rangerFilter(P2)
Rng = rangerOffset(P2:pos(),P2:pos()-vec(0,0,100))

Diff = 24-Rng:distance()
P2:applyForce((vec(0,0,Diff)*3-P2:vel()/30)*P2:mass())

TarQ = quat(ang(Co,Co2,0))
CurQ = quat(E)
Q = TarQ/CurQ
V = P2:toLocal(rotationVector(Q)+P2:pos())
P2:applyTorque((800*V - 12*P2:angVelVector())*P2:inertia()) 

    
}


if(Possesed){
R = randint(1,100)
Timer+=0.1

Co = cos(Timer*1)*R
Co2 = sin(Timer*1)*R

timer("Ha",1200)

if(clk("Ha")){
P = randint(1,10)
P23 = randint(70,255)

P2:soundPlay(100,100,HapE[P,string])
soundPitch(100,P23) 
}

}else{
Co = 0
Co2 = 0
}
