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

class TrainingBulldozer(Missions.SuperMission):
    "Learn to drive the bulldozer."
    level_name = "BulldozerLevel"
    name = "Bulldozer Training Mission"
    training = 2 # bulldozer is vehicle 2

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

        self.instructions = "<text font fg_mcn16 color 0 0 0 1>Learn to drive the bulldozer.</text>"
        
        self.dozer = plus.addVehicleWithRotation("Bulldozer", self.level.startpoint, self.level.startrotation)
        plus.enableVehicleControls(self.dozer, False)
        plus.setPinned(self.dozer, True)
        
        #load all sounds now to decrease lag later
        self.voiceovers['intro'] = "Sounds/Voiceovers/TrainingMissions/2/Description2-1.wav"
        self.voiceovers['drive'] = "Sounds/Voiceovers/TrainingMissions/2/Description2-2.wav"
        self.voiceovers['blade'] = "Sounds/Voiceovers/TrainingMissions/2/Description2-3.wav"
        self.voiceovers['level dirt'] ="Sounds/Voiceovers/TrainingMissions/2/Description2-4.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.nextTaskWhenBladeStopped = False
        self.bladestatus = 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(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, (40.5, 14.42, 74.37), (.26, -2.41), math.pi / 4, 0, 5.5, False) # zoom in on dozer
        plus.animateCamera((40.5, 14.42, 74.37), (.26, -2.41), math.pi / 4, (43.945, 14.42, 66.23), (.44, -1.29), math.pi / 4, 5.5, 10, False) # circle around dozer
        plus.animateCamera((43.945, 14.42, 66.23), (.44, -1.29), math.pi / 4, (34.93, 15.42, 60.98), (.405, .13), math.pi / 4, 10, 14, False) # circle around dozer
        
        yield 15
        
        # done
        yield 0
        
    def IntroComplete(self):
        Missions.SuperMission.IntroComplete(self)
        
        self.NextTask(self.task)
        
    def Tick(self):
        if self.nextTaskWhenStopped:
            stopped = plus.getVehicleStatus(self.dozer)
            if stopped == 0:
                self.NextTask(self.task)
                self.nextTaskWhenStopped = False
                
        if self.nextTaskWhenBladeStopped:
            if self.bladestatus == 1:
                blade = plus.getBladeStatus(self.dozer)
                if blade == -1:
                    self.nextTaskWhenBladeStopped = False
                    self.bladestatus = 0
                    self.tasksComplete[2] = True
                    self.NextTask(self.task)
            else:
                self.bladestatus = plus.getBladeStatus(self.dozer)
                
        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.dozer = plus.addVehicleWithRotation("Bulldozer", 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 = (26.2, 22.31, 53.78)
            self.cameraRot = (.27, .81)
            self.cameraFOV = math.pi / 4
            self.animationTime = 12.5
            
            plus.addZone(0, (2, 4, 2), (96.15, 13, 52.43), self)
            plus.addZone(1, (2, 4, 2), (35.47, 13, 75.44), self)
            plus.animateCamera((26.2, 22.31, 53.78), (.27, .81), math.pi / 4, (86.32, 16.17, 49.26), (.225, 1.2), math.pi / 4, 1, 5, True)
            plus.animateCamera((86.32, 16.17, 49.26), (.225, 1.2), math.pi / 4, (23.4, 16.4, 66.57), (.225, 1.2), math.pi / 4, 6, 11, True)
            self.task = 1
        elif thisTask == 1: # blade up and down
            self.nextTaskWhenBladeStopped = True
            self.task = 2
        elif thisTask == 2: # level dirt and radio in
            plus.fadeToBlack(.5, 0)
	    plus.stopAllSounds(self.dozer)
	    plus.stopAllControls(self.dozer)
            self.cameraSwitchTime = .5
            self.bLastCam = False
            self.cameraPos = (51, 21.36, 88.96)
            self.cameraRot = (.21, 3.45)
            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.task = 3
            
    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['blade'])
            plus.playSound(self.curVoice)
        elif thisTask == 2:
            self.curVoice = plus.createSound(self.voiceovers['level dirt'])
            plus.playSound(self.curVoice)
            
    def SwitchCamera(self):
        if self.bLastCam:
            plus.lastSelectedCamera()
            plus.enableVehicleControls(self.dozer, True)
            plus.setPinned(self.dozer, False)
            plus.fadeFromBlack(.5, 0)
        else:
            plus.enableVehicleControls(self.dozer, False)
            plus.setPinned(self.dozer, 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(TrainingBulldozer)
