from __future__ import generators
import plus
import Missions
import Levels
import random
import math

class TrainingCrane(Missions.SuperMission):
    "Learn to drive the crane."
    level_name = "CraneLevel"
    name = "Crane Training Mission"
    training = 4 # crane is vehicle 4

    def __init__(self, levelIndex):
        Missions.SuperMission.__init__(self, levelIndex)

        self.instructions = "<text font fg_mcn16 color 0 0 0 1>Learn to drive the crane.</text>"
        
        self.crane = plus.addVehicleWithRotation("CraneWreckingBall", self.level.startpoint, self.level.startrotation)
        plus.enableVehicleControls(self.crane, False)
        
        #load all sounds now to decrease lag later
        self.voiceovers['intro'] = "Sounds/Voiceovers/TrainingMissions/5/Description5-1.wav"
        self.voiceovers['drive'] = "Sounds/Voiceovers/TrainingMissions/5/Description5-2.wav"
        self.voiceovers['cab'] = "Sounds/Voiceovers/TrainingMissions/5/Description5-3.wav"
        self.voiceovers['arm'] ="Sounds/Voiceovers/TrainingMissions/5/Description5-4.wav"
        self.voiceovers['extend'] ="Sounds/Voiceovers/TrainingMissions/5/Description5-5.wav"
        self.voiceovers['ball'] ="Sounds/Voiceovers/TrainingMissions/5/Description5-6.wav"
        self.voiceovers['stabilizers1'] ="Sounds/Voiceovers/TrainingMissions/5/Description5-7.wav"
        self.voiceovers['stabilizers2'] ="Sounds/Voiceovers/TrainingMissions/5/Description5-8.wav"
        self.voiceovers['garage'] ="Sounds/Voiceovers/TrainingMissions/5/Description5-9.wav"
        
        # some variables for checking the completion status of the various tasks
        self.task = 0
        self.endTime = 0
        self.redoTaskTime = 0
        self.nextTaskWhenStopped = False
        self.nextTaskWhenCabStopped = False
        self.cabstatus = 0
        self.nextTaskWhenArmStopped = False
        self.armstatus = 0
        self.nextTaskWhenExtenderStopped = False
        self.extenderstatus = 0
        self.nextTaskWhenCableStopped = False
        self.cablestatus = 0
        self.nextTaskWhenStabilizerActivated = False
        self.nextTaskWhenStabilizerDeactivated = False
        self.stabilizerstatus = 0
        self.tasksComplete = {0:False, 1:False, 2:False, 3:False, 4:False, 5:False, 6:False, 7:False}
        self.driveComplete = {0:False, 1:False}
        
        self.cameraSwitchTime = 0
        self.cameraPos = (0, 0, 0)
        self.cameraRot = (0, 0)
        self.cameraFOV = math.pi / 4
        self.bLastCam = False
        self.animationTime = 0
        
    def __del__(self):
        plus.stopSound(self.curVoice)
        plus.removeSound(self.curVoice)
        
        Missions.SuperMission.__del__(self)
        
    def Introduction(self):
        # set initial camera & fade from black
        plus.animationCamera()
        plus.setCameraPosition(14.83, 20.8, 109.85)
        plus.setCameraRotation(.265, -3.65)
        plus.setCameraFOV(math.pi / 4)
        plus.fadeFromBlack(.5, .5)

        yield .75
        
        #intro cam, welcom comment
        self.curVoice = plus.createSound(self.voiceovers['intro'])
        plus.playSound(self.curVoice)

        plus.animateCamera((14.83, 18.8, 109.85), (.265, -3.65), math.pi / 4, (25.5, 20.23, 86.87), (.265, -3.65), math.pi / 4, 0, 9.5, False) # zoom in on crane
        plus.animateCamera((25.5, 20.23, 86.87), (.265, -3.65), math.pi / 4, (61, 25.13, 87.96), (.31, -2.345), math.pi / 4, 9.5, 16, False) # circle around crane
        plus.animateCamera((61, 25.13, 87.96), (.31, -2.345), math.pi / 4, (49.3, 23.05, 48.05), (.32, -.39), math.pi / 4, 16, 22, False) # circle around crane
        
	plus.park(self.crane, True)
	
        yield 24.5
	
        # done
        yield 0
        
    def IntroComplete(self):
        Missions.SuperMission.IntroComplete(self)
	plus.park(self.crane, False)
        self.NextTask(self.task)
        
    def Tick(self):
        if self.nextTaskWhenStopped:
            stopped = plus.getVehicleStatus(self.crane)
            if stopped == 0:
                self.NextTask(self.task)
                self.nextTaskWhenStopped = False
                
        if self.nextTaskWhenCabStopped:
            if self.cabstatus >= .9 * math.pi:
                cab = plus.getCabRotation(self.crane)
                if cab < 0 and cab >= .1 * -math.pi:
                    self.nextTaskWhenCabStopped = False
                    self.cabstatus = 0
                    self.tasksComplete[2] = True
                    self.NextTask(self.task)
            elif self.cabstatus <= .9 * -math.pi:
                cab = plus.getCabRotation(self.crane)
                if cab > 0 and cab <= .1 * math.pi:
                    self.nextTaskWhenCabStopped = False
                    self.cabstatus = 0
                    self.tasksComplete[2] = True
                    self.NextTask(self.task)
            else:
                self.cabstatus = plus.getCabRotation(self.crane)
                
        if self.nextTaskWhenArmStopped:
            if self.armstatus == 1:
                arm = plus.getArmStatus(self.crane)
                if arm == -1:
                    self.nextTaskWhenArmStopped = False
                    self.armstatus = 0
                    self.tasksComplete[3] = True
                    self.NextTask(self.task)
            else:
                self.armstatus = plus.getArmStatus(self.crane)
                
        if self.nextTaskWhenExtenderStopped:
            if self.extenderstatus == 1:
                extender = plus.getExtenderStatus(self.crane)
                if extender == -1:
                    self.nextTaskWhenExtenderStopped = False
                    self.extenderstatus = 0
                    self.tasksComplete[4] = True
                    self.NextTask(self.task)
            else:
                self.extenderstatus = plus.getExtenderStatus(self.crane)
                
        if self.nextTaskWhenCableStopped:
            if self.cablestatus == -1:
                cable = plus.getCableStatus(self.crane)
                if cable == 1:
                    self.nextTaskWhenCableStopped = False
                    self.cablestatus = 0
                    self.tasksComplete[5] = True
                    self.NextTask(self.task)
            else:
                self.cablestatus = plus.getCableStatus(self.crane)
                
        if self.nextTaskWhenStabilizerActivated:
            if self.stabilizerstatus == -1:
                self.nextTaskWhenStabilizerActivated = False
                self.stabilizerstatus = 0
                self.tasksComplete[6] = True
                self.NextTask(self.task)
            else:
                self.stabilizerstatus = plus.getOutriggerStatus(self.crane)
                
        if self.nextTaskWhenStabilizerDeactivated:
            if self.stabilizerstatus == 1:
                self.nextTaskWhenStabilizerDeactivated = False
                self.stabilizerstatus = 0
                self.tasksComplete[7] = True
                self.NextTask(self.task)
            else:
                self.stabilizerstatus = plus.getOutriggerStatus(self.crane)
                
        if self.redoTaskTime > 0:
            self.redoTaskTime -= .25
            if self.redoTaskTime <= 0:
                self.NextTask(self.task - 1)
                
        if self.cameraSwitchTime > 0:
            self.cameraSwitchTime -= .25
            if self.cameraSwitchTime <= 0:
                self.SwitchCamera()
                
        if self.endTime > 0:
            self.endTime -= .25
            if self.endTime <= 0:
                plus.endMission()
            
        return Missions.SuperMission.Tick(self)

    def ZoneEvent(self, direction, id, objType): # objType -- 1=Vehicle, 2=Dirt, 3=Building
        if direction == 1:
            if id == 0 and objType == 1 and not self.tasksComplete[0] and not self.driveComplete[0]:
                self.driveComplete[0] = True
            elif id == 1 and objType == 1 and self.tasksComplete[1] == False:
                if self.driveComplete[0]:
                    self.nextTaskWhenStopped = True
                    self.driveComplete[1] = True
                    self.tasksComplete[1] = True
                elif self.screwupTime <= 0:
                    self.Screwup()
                    self.screwupTime = 5
                
        return True
        
    def Screwup(self):
        Missions.SuperMission.Screwup(self)
        
        if self.task == 0:
            self.driveComplete = {0:False, 1:False}
        
        plus.removeVehicles()
        self.crane = plus.addVehicleWithRotation("CraneWreckingBall", self.level.startpoint, self.level.startrotation)
        plus.lastSelectedCamera()
        self.redoTaskTime = 5
        
    def NextTask(self, thisTask):
        self.NextTaskVoiceover(thisTask)
        
        if thisTask == 0: # drive
            plus.fadeToBlack(.5, 0)
            self.cameraSwitchTime = .5
            self.bLastCam = False
            self.cameraPos = (49.3, 25.05, 48.05)
            self.cameraRot = (.32, -.39)
            self.cameraFOV = math.pi / 4
            self.animationTime = 12
            
            plus.addZone(0, (2, 4, 2), (32.42, 13, 48.46), self)
            plus.addZone(1, (2, 4, 2), (54.67, 13, 79.7), self)
            plus.animateCamera((49.3, 25.05, 48.05), (.32, -.39), math.pi / 4, (49.3, 25.05, 48.05), (.575, -1.38), math.pi / 4, 1, 5, True)
            plus.animateCamera((49.3, 25.05, 48.05), (.575, -1.38), math.pi / 4, (54.16, 19.69, 65.17), (.33, .015), math.pi / 4, 6, 10, True)
            self.task = 1
        elif thisTask == 1: # cab up left and right
            self.nextTaskWhenCabStopped = True
            self.task = 2
        elif thisTask == 2: # arm up and down
            self.nextTaskWhenArmStopped = True
            self.task = 3
        elif thisTask == 3: # extender out and in
            self.nextTaskWhenExtenderStopped = True
            self.task = 4
        elif thisTask == 4: # cable up and down
            self.nextTaskWhenCableStopped = True
            self.task = 5
        elif thisTask == 5:
            self.nextTaskWhenStabilizerActivated = True
            self.task = 6
        elif thisTask == 6:
            self.nextTaskWhenStabilizerDeactivated = True
            self.task = 7
            
            
    def NextTaskVoiceover(self, thisTask):
        if self.curVoice != None:
            plus.stopSound(self.curVoice)
            plus.removeSound(self.curVoice)
            
        if thisTask == 0:
            self.curVoice = plus.createSound(self.voiceovers['drive'])
            plus.playSound(self.curVoice)
        elif thisTask == 1:
            self.curVoice = plus.createSound(self.voiceovers['cab'])
            plus.playSound(self.curVoice)
        elif thisTask == 2:
            self.curVoice = plus.createSound(self.voiceovers['arm'])
            plus.playSound(self.curVoice)
        elif thisTask == 3:
            self.curVoice = plus.createSound(self.voiceovers['extend'])
            plus.playSound(self.curVoice)
        elif thisTask == 4:
            self.curVoice = plus.createSound(self.voiceovers['ball'])
            plus.playSound(self.curVoice)
        elif thisTask == 5:
            self.curVoice = plus.createSound(self.voiceovers['stabilizers1'])
            plus.playSound(self.curVoice)
        elif thisTask == 6:
            self.curVoice = plus.createSound(self.voiceovers['stabilizers2'])
            plus.playSound(self.curVoice)
        elif thisTask == 7:
            self.curVoice = plus.createSound(self.voiceovers['garage'])
            plus.playSound(self.curVoice)
        
    def SwitchCamera(self):
        if self.bLastCam:
            plus.lastSelectedCamera()
            plus.enableVehicleControls(self.crane, True)
            #plus.setPinned(self.crane, False)
	    plus.park(self.crane, False)
            plus.fadeFromBlack(.5, 0)
        else:
            plus.enableVehicleControls(self.crane, False)
            #plus.setPinned(self.crane, True)
	    plus.park(self.crane, True)
	    plus.stopAllSounds(self.crane)
	    plus.stopAllControls(self.crane)
            plus.setCameraPosition(self.cameraPos[0], self.cameraPos[1], self.cameraPos[2])
            plus.setCameraRotation(self.cameraRot[0], self.cameraRot[1])
            plus.setCameraFOV(self.cameraFOV)
            plus.fadeFromBlack(.5, 0)
            plus.fadeToBlack(.5, self.animationTime - .5)
            self.cameraSwitchTime = self.animationTime
            self.bLastCam = True

Missions.register(TrainingCrane)
