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

class TrainingLoader(Missions.SuperMission):
    "Learn to drive the front end loader."
    level_name = "LoaderLevel"
    name = "Front End Loader Training Mission"
    training = 3 # loader is vehicle 3

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

        self.instructions = "<text font fg_mcn16 color 0 0 0 1>Learn to drive the front end loader.</text>"
        
        self.loader = plus.addVehicleWithRotation("FrontEndLoaderScoop", self.level.startpoint, self.level.startrotation)
        plus.enableVehicleControls(self.loader, False)
        plus.setPinned(self.loader, True)
        
        self.truckpos = (35, 50)
        self.truckrot = (-math.pi / 2.0, 0, 0)
        self.truck = plus.addVehicleWithRotation("DumpTruck", self.truckpos, self.truckrot)
        plus.enableVehicleControls(self.truck, False)
        plus.setPinned(self.truck, True)
        
        #load all sounds now to decrease lag later
        self.voiceovers['intro'] = "Sounds/Voiceovers/TrainingMissions/3/Description3-1.wav"
        self.voiceovers['drive'] = "Sounds/Voiceovers/TrainingMissions/3/Description3-2.wav"
        self.voiceovers['arm'] = "Sounds/Voiceovers/TrainingMissions/3/Description3-3.wav"
        self.voiceovers['scoop'] = "Sounds/Voiceovers/TrainingMissions/3/Description3-4.wav"
        self.voiceovers['dirt to truck'] = "Sounds/Voiceovers/TrainingMissions/3/Description3-5.wav"
        self.voiceovers['completed'] = "Sounds/Voiceovers/TrainingMissions/3/Description3-6.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.nextTaskWhenScoopStopped = False
        self.checkTruckFull = False
        self.armstatus = 0
        self.scoopstatus = 0
        self.tasksComplete = {0:False, 1:False, 2:False, 3:False, 4:False, 5: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(110.5, 16.3, 84.35)
        plus.setCameraRotation(-.02, -1.515)
        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((110.5, 14.3, 84.35), (-.02, -1.515), math.pi / 4, (50.55, 15.52, 87.7), (-.02, -1.515), math.pi / 4, 0, 9, False) # zoom in on loader
        plus.animateCamera((50.55, 15.52, 87.7), (-.02, -1.515), math.pi / 4, (37.33, 17.29, 71.64), (.12, -.23), math.pi / 4, 9, 17, False) # circle around loader
        plus.animateCamera((37.33, 17.29, 71.64), (.12, -.23), math.pi / 4, (19.33, 18, 79.22), (.235, 1.075), math.pi / 4, 17, 24, False) # circle around loader
        
        yield 26.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.loader)
            if stopped == 0:
                self.NextTask(self.task)
                self.nextTaskWhenStopped = False
                
        if self.nextTaskWhenArmStopped:
            if self.armstatus == 1:
                arm = plus.getArmStatus(self.loader)
                if arm == -1:
                    self.nextTaskWhenArmStopped = False
                    self.armstatus = 0
                    self.tasksComplete[2] = True
                    self.NextTask(self.task)
            else:
                self.armstatus = plus.getArmStatus(self.loader)
                
        if self.nextTaskWhenScoopStopped:
            if self.scoopstatus == 1:
                arm = plus.getScoopStatus(self.loader)
                if arm == -1:
                    self.nextTaskWhenScoopStopped = False
                    self.scoopstatus = 0
                    self.tasksComplete[3] = True
                    self.NextTask(self.task)
            else:
                self.scoopstatus = plus.getScoopStatus(self.loader)
                
        if self.checkTruckFull:
            full = plus.getDirtAmount(self.truck)
            if full >= 40:
                self.tasksComplete[4] = True
                self.NextTask(self.task)
                self.checkTruckFull = False
                
        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.loader = plus.addVehicleWithRotation("FrontEndLoaderScoop", self.level.startpoint, self.level.startrotation)
        plus.lastSelectedCamera()
        
        self.truck = plus.addVehicleWithRotation("DumpTruck", self.truckpos, self.truckrot)
        plus.enableVehicleControls(self.truck, False)
        plus.setPinned(self.truck, True)
        
        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 = (43.7, 25.5, 103)
            self.cameraRot = (.45, 3.32)
            self.cameraFOV = math.pi / 4
            self.animationTime = 11.5
            
            plus.addZone(0, (2, 4, 2), (95.1, 13, 58.28), self)
            plus.addZone(1, (2, 4, 2), (70, 13, 30.5), self)
            plus.animateCamera((43.7, 25.5, 103), (.45, 3.32), math.pi / 4, (87.2, 16.6, 66.87), (.295, 2.39), math.pi / 4, 1, 5, True)
            plus.animateCamera((87.2, 16.6, 66.87), (.295, 2.39), math.pi / 4, (70.83, 16.28, 39.2), (.32, 3.26), math.pi / 4, 6, 11, True)
            self.task = 1
        elif thisTask == 1: # arm up and down
            self.nextTaskWhenArmStopped = True
            self.task = 2
        elif thisTask == 2: # scoop up and down
            self.nextTaskWhenScoopStopped = True
            self.task = 3
        elif thisTask == 3: # scoop up dirt and put it in the truck
            plus.fadeToBlack(.5, 0)
	    plus.stopAllSounds(self.loader)
	    plus.stopAllControls(self.loader)
            self.cameraSwitchTime = .5
            self.bLastCam = False
            self.cameraPos = (60.83, 18.28, 15)
            self.cameraRot = (.32, .12)
            self.cameraFOV = math.pi / 4
            self.animationTime = 11.5
            
            plus.animateCamera((51, 21.36, 88.96), (.21, 3.45), math.pi / 4, (38.8, 18.2, 48.5), (.21, 3.45), math.pi / 4, 1, 5, True) # head toward pile of dirt
            plus.animateCamera((38.8, 18.2, 48.5), (.21, 3.45), math.pi / 4, (53.08, 20.69, 38.86), (.26, 4.59), math.pi / 4, 7, 11, True) # circle pile of dirt
            self.checkTruckFull = True
            self.task = 4
        elif thisTask == 4:
            self.endTime = 4
            self.task = 5
            
    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['arm'])
            plus.playSound(self.curVoice)
        elif thisTask == 2:
            self.curVoice = plus.createSound(self.voiceovers['scoop'])
            plus.playSound(self.curVoice)
        elif thisTask == 3:
            self.curVoice = plus.createSound(self.voiceovers['dirt to truck'])
            plus.playSound(self.curVoice)
        elif thisTask == 4:
            self.curVoice = plus.createSound(self.voiceovers['completed'])
            plus.playSound(self.curVoice)
            
    def SwitchCamera(self):
        if self.bLastCam:
            plus.lastSelectedCamera()
            plus.enableVehicleControls(self.loader, True)
            plus.setPinned(self.loader, False)
            plus.fadeFromBlack(.5, 0)
        else:
            plus.enableVehicleControls(self.loader, False)
            plus.setPinned(self.loader, 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(TrainingLoader)
