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

class TrainingDumpTruck(Missions.SuperMission):
    "Learn to drive the dump truck."
    level_name = "DumpTruckLevel"
    name = "Dump Truck Training Mission"
    training = 0 # dump truck is vehicle 0

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

        self.instructions = "<text font fg_mcn16 color 0 0 0 1>Learn to drive the dump truck.</text>"
        
        self.truck = plus.addVehicleWithRotation("DumpTruck", self.level.startpoint, self.level.startrotation)
        plus.enableVehicleControls(self.truck, False)
        plus.setPinned(self.truck, True)
        
        self.level.AddHopper(87.44, 37.5, -90)
        
        #load all sounds now to decrease lag later
        self.voiceovers['intro'] = "Sounds/Voiceovers/TrainingMissions/1/Description1-1.wav"
        self.voiceovers['drive'] = "Sounds/Voiceovers/TrainingMissions/1/Description1-2.wav"
        self.voiceovers['bed lift'] = "Sounds/Voiceovers/TrainingMissions/1/Description1-3.wav"
        self.voiceovers['hopper'] = "Sounds/Voiceovers/TrainingMissions/1/Description1-4.wav"
        self.voiceovers['dump load'] = "Sounds/Voiceovers/TrainingMissions/1/Description1-5.wav"
        self.voiceovers['completed'] ="Sounds/Voiceovers/TrainingMissions/1/Description1-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.nextTaskWhenDumpStopped = False
        self.checkLoadFull = False
        self.checkLoadEmpty = False
        self.bedstatus = 0
        self.tasksComplete = {0:False, 1:False, 2:False, 3:False, 4:False}
        self.driveComplete = {0:False, 1:False, 2:False, 3:False}
        
        self.cameraSwitchTime = 0
        self.cameraPos = (0, 0, 0) # next animation cam position
        self.cameraRot = (0, 0) # next animation cam rotation
        self.cameraFOV = math.pi / 4 # next animation cam fov
        self.bLastCam = False # should we switch to last selected cam or next animation cam
        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(22, 20, .4)
        plus.setCameraRotation(.37, .145)
        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((22, 20, .4), (.37, .145), math.pi / 4, (22.26, 16.146, 11.8), (.375, .145), math.pi / 4, 0, 5.5, False) # zoom in on truck
        plus.animateCamera((22.26, 16.146, 11.8), (.375, .145), math.pi / 4, (29.76, 14.385, 17.086), (.325, -.97), math.pi / 4, 5.5, 10, False) # circle around truck
        plus.animateCamera((29.76, 14.385, 17.086), (.325, -.97), math.pi / 4, (29.22, 16.27, 28.34), (.435, -2.485), math.pi / 4, 10, 14, False) # circle around truck
        
        yield 17
        
        # done
        yield 0
        
    def IntroComplete(self):
        Missions.SuperMission.IntroComplete(self)
        self.NextTask(self.task)
        
    def Tick(self):
        if self.nextTaskWhenStopped:
            stopped = plus.getVehicleStatus(self.truck)
            if stopped == 0:
                self.NextTask(self.task)
                self.nextTaskWhenStopped = False
                
        if self.nextTaskWhenDumpStopped:
            if self.bedstatus == 1:
                bed = plus.getBedStatus(self.truck)
                if bed == -1:
                    self.nextTaskWhenDumpStopped = False
                    self.bedstatus = 0
                    self.tasksComplete[2] = True
                    self.NextTask(self.task)
            else:
                self.bedstatus = plus.getBedStatus(self.truck)
                
        if self.checkLoadFull:
            full = plus.getDirtAmount(self.truck)
            if full >= 20:
                self.tasksComplete[3] = True
                self.NextTask(self.task)
                self.checkLoadFull = False
        
        if self.checkLoadEmpty:
            empty = plus.getDirtAmount(self.truck)
            if empty <= 2:
                self.tasksComplete[4] = True
                self.NextTask(self.task)
                self.checkLoadEmpty = 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 self.tasksComplete[0] == False and self.driveComplete[0] == False: # drive zone 0
                self.driveComplete[0] = True
            if id == 1 and objType == 1 and self.tasksComplete[0] == False and self.driveComplete[1] == False: # drive zone 1
                if self.driveComplete[0]:
                    self.driveComplete[1] = True
                elif self.screwupTime <= 0:
                    self.Screwup()
                    self.screwupTime = 5
            if id == 2 and objType == 1 and self.tasksComplete[0] == False and self.driveComplete[2] == False: # drive zone 2
                if self.driveComplete[0] and self.driveComplete[1]:
                    self.driveComplete[2] = True
                elif self.screwupTime <= 0:
                    self.Screwup()
                    self.screwupTime = 5
            if id == 3 and objType == 1 and self.tasksComplete[0] == False and self.driveComplete[3] == False: # drive zone 3
                if self.driveComplete[0] and self.driveComplete[1] and self.driveComplete[2]:
                    self.driveComplete[3] = True
                    self.nextTaskWhenStopped = True
                    self.tasksComplete[0] = True
                elif self.screwupTime <= 0:
                    self.Screwup()
                    self.screwupTime = 5
            elif id == 4 and objType == 1 and self.tasksComplete[4] == False: # hopper zone
                if self.tasksComplete[3] == False:
                    self.checkLoadFull = True # go on to next task when truck is full
            elif id == 5 and objType == 2 and self.tasksComplete[4] == False and not self.checkLoadEmpty: # dump zone
                self.checkLoadEmpty = True
                
        return True
        
    def Screwup(self):
        Missions.SuperMission.Screwup(self)
        
        if self.task == 0:
            self.driveComplete = {0:False, 1:False, 2:False, 3:False}
            
        plus.removeVehicles()
        self.truck = plus.addVehicleWithRotation("DumpTruck", 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)
	    plus.stopAllSounds(self.truck)
            plus.stopAllControls(self.truck)
            self.cameraSwitchTime = .5
            self.bLastCam = False
            self.cameraPos = (14.85, 20.1, 14.7)
            self.cameraRot = (.38, .925)
            self.cameraFOV = math.pi / 4
            self.animationTime = 14.5
            
            plus.addZone(0, (.2, 5, 5), (97.5, 13, 21.15), self)
            plus.addZone(1, (5, 5, .2), (106.15, 13, 98.3), self)
            plus.addZone(2, (.2, 5, 5), (29.5, 13, 106.15), self)
            plus.addZone(3, (5, 5, .2), (22, 13, 35.9), self)
            plus.animateCamera((14.85, 20.1, 14.7), (.38, .925), math.pi / 4, (90, 18, 19.7), (.29, 1.275), math.pi / 4, 1, 4, False)
            plus.animateCamera((90, 18, 19.7), (.29, 1.275), math.pi / 4, (105.75, 17.8, 85.5), (.235, -.22), math.pi / 4, 4, 7, False)
            plus.animateCamera((105.75, 17.8, 85.5), (.235, -.22), math.pi / 4, (38.7, 17, 108.14), (.145, -1.945), math.pi / 4, 7, 11, False)
            plus.animateCamera((38.7, 17, 108.14), (.145, -1.945), math.pi / 4, (26.2, 16, 34), (.07, -3.015), math.pi / 4, 11, 14, False)
            self.task = 1
        elif thisTask== 1: # blade up and down
            self.nextTaskWhenDumpStopped = True
            self.task = 2
        elif self.task == 2: # load up from the hopper
            plus.fadeToBlack(.5, 0)
	    plus.stopAllSounds(self.truck)
            plus.stopAllControls(self.truck)
	    self.cameraSwitchTime = .5
            self.bLastCam = False
            self.cameraPos = (18.8, 22.62, 27.2)
            self.cameraRot = (.41, 1.07)
            self.cameraFOV = math.pi / 4
            self.animationTime = 9.5
        
            plus.addZone(4, (1, 4, 1), (88, 13, 37), self)
            plus.animateCamera((18.8, 22.62, 27.2), (.41, 1.07), math.pi / 4, (61, 19, 33.5), (.1, 1.425), math.pi / 4, 1, 5, True)
            #plus.animateCamera((61, 19, 33.5), (.1, 1.425), math.pi / 4, (18.8, 22.62, 27.2), (.41, 1.07), math.pi / 4, 5, 9, True)
            self.task = 3
        elif thisTask == 3: # dump hopper load
            plus.fadeToBlack(.5, 0)
	    plus.stopAllSounds(self.truck)
	    plus.stopAllControls(self.truck)
            self.cameraSwitchTime = .5
            self.bLastCam = False
            self.cameraPos = (61, 19, 33.5)
            self.cameraRot = (.1, 1.425)
            self.cameraFOV = math.pi / 4
            self.animationTime = 9.5
        
            plus.addZone(5, (7.5, 5, 6.5), (38.65, 13.5, 37), self)
            plus.animateCamera((61, 19, 33.5), (.1, 1.425), math.pi / 4, (18.8, 22.62, 27.2), (.41, 1.07), math.pi / 4, 1, 5, True)
            #plus.animateCamera((18.8, 22.62, 27.2), (.41, 1.07), math.pi / 4, (61, 19, 33.5), (.1, 1.425), math.pi / 4, 5, 9, True)
            self.task = 4
        elif thisTask == 4:
            self.endTime = 5
            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['bed lift'])
            plus.playSound(self.curVoice)
        elif thisTask == 2:
            self.curVoice = plus.createSound(self.voiceovers['hopper'])
            plus.playSound(self.curVoice)
        elif thisTask== 3:
            self.curVoice = plus.createSound(self.voiceovers['dump load'])
            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.truck, True)
            plus.setPinned(self.truck, False)
            plus.fadeFromBlack(.5, 0)
        else:
	    plus.stopAllSounds(self.truck)
            plus.stopAllControls(self.truck)
            plus.enableVehicleControls(self.truck, False)
            plus.setPinned(self.truck, 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(TrainingDumpTruck)
