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

class TrainingExcavator(Missions.SuperMission):
    "Learn to drive the excavator."
    level_name = "ExcavatorLevel"
    name = "Excavator Training Mission"
    training = 1 # excavator is vehicle 1

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

        self.instructions = "<text font fg_mcn16 color 0 0 0 1>Learn to drive the excavator.</text>"
        
        self.excavator = plus.addVehicleWithRotation("Excavator", self.level.startpoint, self.level.startrotation)
        plus.enableVehicleControls(self.excavator, False)
        plus.setPinned(self.excavator, True)
        
        #load all sounds now to decrease lag later
        self.voiceovers['intro'] = "Sounds/Voiceovers/TrainingMissions/4/Description4-1.wav"
        self.voiceovers['drive'] = "Sounds/Voiceovers/TrainingMissions/4/Description4-2.wav"
        self.voiceovers['cab'] = "Sounds/Voiceovers/TrainingMissions/4/Description4-3.wav"
        self.voiceovers['arm'] ="Sounds/Voiceovers/TrainingMissions/4/Description4-4.wav"
        self.voiceovers['extender'] ="Sounds/Voiceovers/TrainingMissions/4/Description4-5.wav"
        self.voiceovers['bucket'] ="Sounds/Voiceovers/TrainingMissions/4/Description4-6.wav"
        self.voiceovers['work'] ="Sounds/Voiceovers/TrainingMissions/4/Description4-7.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.nextTaskWhenArmStopped = False
        self.armstatus = 0
        self.nextTaskWhenBucketStopped = False
        self.bucketstatus = 0
        self.nextTaskWhenExtenderStopped = False
        self.extenderstatus = 0
        self.nextTaskWhenCabStopped = False
        self.cabstatus = 0
	self.nextTaskWhenDigging = False
	self.holedug = 0
        self.tasksComplete = {0:False, 1:False, 2:False, 3:False, 4:False, 5:False, 6: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(98.6, 21.1, 108)
        plus.setCameraRotation(.225, -2.21)
        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((98.6, 21.1, 108), (.225, -2.21), math.pi / 4, (51.28, 18.97, 79.4), (.225, -2.21), math.pi / 4, 0, 7.5, False) # zoom in on excavator
        plus.animateCamera((51.28, 18.97, 79.4), (.225, -2.21), math.pi / 4, (51.77, 19.9, 56.9), (.29, -.885), math.pi / 4, 7.5, 14, False) # circle around excavator
        plus.animateCamera((51.77, 19.9, 56.9), (.29, -.885), math.pi / 4, (28.33, 19.75, 55.34), (.275, .71), math.pi / 4, 14, 19.5, False) # circle around excavator
        
        yield 20.5
        
        # done
        yield 0
        
    def IntroComplete(self):
        Missions.SuperMission.IntroComplete(self)
        
        self.NextTask(self.task)
        
    def Tick(self):
        if self.nextTaskWhenStopped:
            stopped = plus.getVehicleStatus(self.excavator)
            if stopped == 0:
                self.NextTask(self.task)
                self.nextTaskWhenStopped = False
                
        if self.nextTaskWhenArmStopped:
            if self.armstatus == 1:
                arm = plus.getArmStatus(self.excavator)
                if arm == -1:
                    self.nextTaskWhenArmStopped = False
                    self.armstatus = 0
                    self.tasksComplete[3] = True
                    self.NextTask(self.task)
            else:
                self.armstatus = plus.getArmStatus(self.excavator)
                    
        if self.nextTaskWhenBucketStopped:
            if self.bucketstatus == 1:
                bucket = plus.getScoopStatus(self.excavator)
                if bucket == -1:
                    self.nextTaskWhenBucketStopped = False
                    self.bucketstatus = 0
                    self.tasksComplete[5] = True
                    self.NextTask(self.task)
            else:
                self.bucketstatus = plus.getScoopStatus(self.excavator)
                    
        if self.nextTaskWhenExtenderStopped:
            if self.extenderstatus == 1:
                extender = plus.getExtenderStatus(self.excavator)
                if extender == -1:
                    self.nextTaskWhenExtenderStopped = False
                    self.extenderstatus = 0
                    self.tasksComplete[4] = True
                    self.NextTask(self.task)
            else:
                self.extenderstatus = plus.getExtenderStatus(self.excavator)
                
        if self.nextTaskWhenCabStopped:
            if self.cabstatus >= .9 * math.pi:
                cab = plus.getCabRotation(self.excavator)
                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.excavator)
                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.excavator)
	
	if self.nextTaskWhenDigging:
		if self.holedug < 33:
		    self.holedug = plus.getTerrainScore()  # [0-100] returned
		else:
		    self.nextTaskWhenDigging = False
		    self.tasksComplete[6] = True
		    self.NextTask(self.task)
	
        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.excavator = plus.addVehicleWithRotation("Excavator", 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 = (27.2, 25.8, 50.4)
            self.cameraRot = (.38, .47)
            self.cameraFOV = math.pi / 4
            self.animationTime = 11.5
            
            plus.addZone(0, (2, 4, 2), (36.2, 13, 99.9), self)
            plus.addZone(1, (2, 4, 2), (97.8, 13, 98.91), self)
            plus.animateCamera((27.2, 25.8, 50.4), (.38, .47), math.pi / 4, (35.59, 17.6, 88.4), (.36, .03), math.pi / 4, 1, 5, True)
            plus.animateCamera((35.59, 17.6, 88.4), (.36, .03), math.pi / 4, (91, 15.5, 96), (.39, 1), math.pi / 4, 5.5, 11, True)
            self.task = 1
        elif thisTask == 1: # cab left to right
            self.nextTaskWhenCabStopped = True
            self.task = 2
        elif thisTask == 2: # arm up and down
            self.nextTaskWhenArmStopped = True
            self.task = 3
        elif thisTask == 3: # extender up and down
            self.nextTaskWhenExtenderStopped = True
            self.task = 4
        elif thisTask == 4: # bucket down and up
            self.nextTaskWhenBucketStopped = True
            self.task = 5
        elif thisTask == 5: # dig it out and radio in
            plus.fadeToBlack(.5, 0)
	    plus.stopAllSounds(self.excavator)
	    plus.stopAllControls(self.excavator)
            self.cameraSwitchTime = .5
            self.bLastCam = False
            self.cameraPos = (111, 22, 108)
            self.cameraRot = (.375, 4.015)
            self.cameraFOV = math.pi / 4
            self.animationTime = 11.5
            
            plus.animateCamera((111, 22, 108), (.375, 4.015), math.pi / 4, (53.73, 19.77, 67.73), (.26, 4.575), math.pi / 4, 1, 5, True)
            plus.animateCamera((53.73, 19.77, 67.73), (.26, 4.575), math.pi / 4, (111, 22, 108), (.375, 4.015), math.pi / 4, 7, 11, True)
            self.task = 6
            
    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['extender'])
            plus.playSound(self.curVoice)
        elif thisTask == 4:
            self.curVoice = plus.createSound(self.voiceovers['bucket'])
            plus.playSound(self.curVoice)
        elif thisTask == 5:
            self.curVoice = plus.createSound(self.voiceovers['work'])
            plus.playSound(self.curVoice)
            
    def SwitchCamera(self):
        if self.bLastCam:
            plus.lastSelectedCamera()
            plus.enableVehicleControls(self.excavator, True)
            plus.setPinned(self.excavator, False)
            plus.fadeFromBlack(.5, 0)
        else:
            plus.enableVehicleControls(self.excavator, False)
            plus.setPinned(self.excavator, True)
            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(TrainingExcavator)
