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

class YardApe(Missions.SuperMission):
    "Unload decorative stone off a truck and clean up the construction yard."
    level_name = "Level 4"
    name = "Yard Ape"
    preview = "Levels/Level4/yardape_preview.bmp"
    equipcost = 120
    pay = 570
    perfectspeed = 270 # seconds
    worstspeed = 750
    
    easy_speedfail = 20
    easy_safetyfail = 20
    easy_accuracyfail = 20
    easy_default = 50
    med_speedfail = 20
    med_safetyfail = 20
    med_accuracyfail = 20
    med_default = 50
    med_bonus = 70
    hard_speedfail = 20
    hard_safetyfail = 20
    hard_accuracyfail = 20
    hard_default = 50
    hard_bonus = 70
    
    missionNumber = 4
    accuracy_type = 5
    
    def __init__(self, levelIndex):
        Missions.SuperMission.__init__(self, levelIndex)
        
        plus.setBackColor(0, 0, 0)
        
        self.missionID = 4
        self.instructions = "<text font fg_mcn12>A truckload of decorative stone just arrived. Unload it using the straight fork on your front-end loader. Pick up the skids of materials and put them next to the other materials. And hurry up; the driver has to pick up some other supplies before noon, so he's leaving in five minutes whether you got the stuff off or not. When you're done with that, there's a heap of landscaping rocks that spilled out of another truck. Get the dump truck and park it next to the heap. Then switch out the straight fork for the bucket on the loader and put the landscaping rocks into the dump truck. Take the landscaping rock to the fenced-in area and dump them.</text>"
        
        self.loader = plus.addVehicleWithRotation("FrontEndLoaderFork", self.level.startpoint, self.level.startrotation)
        plus.setPinned(self.loader, True)
        plus.enableVehicleControls(self.loader, False)
        
        self.flatbedPosition = (63, 14)
        self.flatbedRotation = (-math.pi / 2, 0, 0)
        self.flatbed = plus.addVehicleWithRotation("FlatbedTruck", self.flatbedPosition, self.flatbedRotation)
        plus.enableVehicleControls(self.flatbed, False)
        
        self.truckPosition = (47, 30)
        self.truckRotation = (math.pi, 0, 0)
        self.truck = plus.addVehicleWithRotation("DumpTruck", self.truckPosition, self.truckRotation)
        plus.enableVehicleControls(self.truck, False)
        
        plus.setAvailableVehicles((0, "Loader", 2, "Dump Truck"))
        plus.setAvailableTools(("FrontEndLoaderFork", "Fork", "FrontEndLoaderScoop", "Bucket"))
        
        self.voiceovers['intro'] = "Sounds/Voiceovers/Missions/4/Description4-1.wav"
        self.voiceovers['skids'] = "Sounds/Voiceovers/Missions/4/Description4-2.wav"
        self.voiceovers['heap'] = "Sounds/Voiceovers/Missions/4/Description4-3.wav"
        self.voiceovers['area'] = "Sounds/Voiceovers/Missions/4/Description4-4.wav"
        
        self.truckguy = { 0:"Sounds/truck_horn.wav", 1:"Sounds/Voiceovers/Missions/4/TruckGuy/TruckGuy1.wav", 2:"Sounds/Voiceovers/Missions/4/TruckGuy/TruckGuy5.wav", 3:"Sounds/Voiceovers/Missions/4/TruckGuy/TruckGuy10.wav", 4:"Sounds/Voiceovers/Missions/4/TruckGuy/TruckGuy30.wav" }
        self.truckguyPlayed = { 0:False, 1:False, 2:False, 3:False, 4:False }
        
        self.animationStage = 0
        self.animationTimer = 0
        self.bTimer = False
        self.checkTruckFull = False
        self.checkTruckEmpty = False
        self.flatbedUnloaded = False
        self.flatbedGone = False
        self.checkFlatbedExit = False
        self.checkFlatbedExitTime = 0
        self.timeToFail = 0
        
        self.skidPlaced = False
        
        plus.addZone(2, (30, 10, 30), (63, 18, 15), self)
        plus.addZone(3, (5, 5, 5), (33.6, 15.5, 61.5), self)
        
        self.objectives = ("Unload decorative stone from flatbed truck.", "Place decorative stone next to other materials.", "Load dump truck with landscaping rocks.", "Dump load in the fenced-in area.")
        
        self.level.SetHelpArrowPosition(0, 60.1, 20.0, 14.0, 0.0) # Truck

    def Introduction(self):
        plus.park(self.flatbed, True)
        plus.park(self.truck, True)

        # set initial camera & fade from black
        plus.animationCamera()
        plus.setCameraPosition(69, 19, 51)
        plus.setCameraRotation(.225, 3.245)
        plus.setCameraFOV(math.pi / 4)
        plus.fadeFromBlack(.5, .5)

        #start playing music loop
        yield .75
        
        #load all sounds now to decrease lag later
        
        #intro cam, welcom comment
        self.curVoice = plus.createSound(self.voiceovers['intro'])
        plus.playSound(self.curVoice)
        plus.animateCamera((69, 19, 51), (.225, 3.245), math.pi / 4, (63.55, 18.7, 27), (.225, 3.245), math.pi / 4, 0, 5, False) # flatbed
        yield 9
        
        plus.stopSound(self.curVoice)
        plus.removeSound(self.curVoice)
        self.curVoice = plus.createSound(self.voiceovers['skids'])
        plus.playSound(self.curVoice)
        plus.animateCamera((63.55, 18.7, 27), (.225, 3.245), math.pi / 4, (48.2, 19.8, 56.58), (.19, 5.42),math.pi / 4, 0, 7, False) #skids
        plus.animateCamera((48.2, 19.8, 56.58), (.19, 5.42),math.pi / 4, (42.7, 18.4, 54.8), (.235, 4.4), math.pi / 4, 9, 15, False) # loader

        yield 18
        
        plus.stopSound(self.curVoice)
        plus.removeSound(self.curVoice)
        self.curVoice = plus.createSound(self.voiceovers['heap'])
        plus.playSound(self.curVoice)
        plus.animateCamera((42.7, 18.4, 54.8), (.235, 4.4), math.pi / 4, (34.45, 16.9, 46.35), (.2, 3.92), math.pi / 4, 0, 5, False)
        plus.animateCamera((34.45, 16.9, 46.35), (.2, 3.92), math.pi / 4, (40.9, 17.7, 42.6), (.165, 2.82), math.pi / 4, 8, 12, False)
        yield 14
        
        plus.stopSound(self.curVoice)
        plus.removeSound(self.curVoice)
        self.curVoice = plus.createSound(self.voiceovers['area'])
        plus.playSound(self.curVoice)
        plus.animateCamera((40.9, 17.7, 42.6), (.165, 2.82), math.pi / 4, (71.7, 18.6, 60), (.065, 1.6), math.pi / 4, 0, 4, False) # zoom in on pool, pause
        
        yield 7
        
        # done
        yield 0
        
    def IntroComplete(self):
        Missions.SuperMission.IntroComplete(self)
        plus.enableVehicleControls(self.loader, True)
        plus.setPinned(self.loader, False)
        plus.resetClock()
        plus.setTimer(300)
        #plus.setTimer(15)
        plus.showTimer(True)
        self.bTimer = True
        plus.enableSwitchVehicle(True)
        plus.enableSwitchTool(True)
        
        plus.playSoundOnce(self.truckguy[4])
        self.truckguyPlayed[4] = True
        
    def Tick(self):
        if self.checkFlatbedExit:
            self.checkFlatbedExitTime += .25
            if self.checkFlatbedExitTime == 11:
                plus.playSoundOnce("Sounds/Voiceovers/Missions/4/TruckGuy/TruckGuyUpset.wav")
                self.timeToFail = 6
                self.checkFlatbedExit = False
                
        if self.timeToFail > 0:
            self.timeToFail -= .25
            if self.timeToFail <= 0:
                plus.failMission()
            
        if self.bTimer:
            time = plus.getTimeRemaining()
            if time <= 0 and self.truckguyPlayed[0] == False:
                self.TimerComplete()
            if time <= 60 and self.truckguyPlayed[1] == False:
                plus.playSoundOnce(self.truckguy[1])
                self.truckguyPlayed[1] = True
            elif time <= 120 and self.truckguyPlayed[2] == False:
                plus.playSoundOnce(self.truckguy[2])
                self.truckguyPlayed[2] = True
            elif time <= 180 and self.truckguyPlayed[3] == False:
                plus.playSoundOnce(self.truckguy[3])
                self.truckguyPlayed[3] = True
    
        if self.animationStage == 1: # switch to new camera and fade back in
            if self.animationTimer > 0:
                self.animationTimer -= .25
                if self.animationTimer <= 0:
                    plus.animationCamera()
                    plus.setCameraPosition(49.8, 15.8, 16.9)
                    plus.setCameraRotation(.15, 1.8)
                    plus.setCameraFOV(math.pi / 4)
                    plus.disableEscape(False, .75)
                    plus.fadeFromBlack(.5, 0)
                    self.animationTimer = .25
                    self.animationStage = 2
                    self.flatbedGone = True
        elif self.animationStage == 2: # play last truck guy sound and drive away
            if self.animationTimer > 0:
                self.animationTimer -= .25
                if self.animationTimer <= 0:
                    plus.playSoundOnce(self.truckguy[0])
                    self.truckguyPlayed[0] = True
                    plus.addZone(0, (1, 10, 10), (110, 20, 14), self)
                    plus.park(self.flatbed, False)
                    plus.drive(self.flatbed, True, -100)
                    self.checkFlatbedExit = True
        elif self.animationStage == 3: # end animation and switch back to controlling the vehicle
            if self.animationTimer > 0:
                self.animationTimer -= .25
                if self.animationTimer <= 0:
                    plus.removeVehicle(1)
                    plus.setAvailableVehicles((0, "Loader", 1, "Dump Truck")) # dump truck is now vehicle 1 since flatbed is gone
                    self.flatbed = None
                    plus.lastSelectedCamera()
                    #plus.park(plus.getVehicle(plus.getCurrentVehicle()), False)
                    plus.brake(self.loader, False, 15)
                    plus.brake(self.truck, False, 15)
                    plus.enableVehicleControls(plus.getVehicle(plus.getCurrentVehicle()), True)
                    plus.disableEscape(False, .75)
                    plus.fadeFromBlack(.5, 0)
                    self.animationStage = 0 # all done
                    self.checkFlatbedExit = False
                    
                    if self.flatbedUnloaded:
                        plus.objectiveComplete(0, True)
                        self.level.SetHelpArrowPosition(0, 33.4, 14.0, 61.5, 0.0) # Skid area
                        self.checkTruckFull = True
                        
        if self.checkTruckFull:
            full = plus.getDirtAmount(self.truck)
            if full > 40:
                self.checkTruckFull = False
                plus.objectiveComplete(2, True)
                self.level.SetHelpArrowPosition(0, 100.5, 14.0, 60.0, 0.0) # Dump area
                
                # now that the truck is done, add a zone in the fenced-in area for gravel
                plus.addZone(1, (26, 10, 20), (106, 18, 61), self)

        if self.checkTruckEmpty:
            empty = plus.getDirtAmount(self.truck)
            if empty == 0:
                self.checkTruckEmpty = False
                plus.objectiveComplete(3, True)
            
        return Missions.SuperMission.Tick(self)

    def ZoneEvent(self, direction, id, objType):
        if direction == 1: # entering the zone
            if id == 0 and self.animationStage == 2 and objType == 1:
                plus.drive(self.flatbed, False, 0)
                self.animationStage = 3
                plus.disableEscape(True, 0)
                plus.fadeToBlack(.5, 0)
                self.animationTimer = .75
                
            if id == 1 and objType == 2:
                self.checkTruckEmpty = True
                
            if id == 3 and objType == 8 and self.skidPlaced == False:
                self.skidPlaced = True
                plus.objectiveComplete(1, True)
                self.level.SetHelpArrowPosition(0, 26.0, 15.8, 38.0, 0.0) # Stone pile
                
        else: # leaving the zone
            if id == 2 and objType == 8 and self.flatbedGone == False and self.flatbedUnloaded == False:
                self.flatbedUnloaded = True
                self.TimerComplete()
    
        return True
        
    def SwitchedVehicles(self, iVehicle):
        if iVehicle == 0:
            plus.enableSwitchTool(True)
        else:
            plus.enableSwitchTool(False)
            
    def SwitchedTools(self, iVehicle):
        self.loader = plus.getVehicle(iVehicle)
        
    def TimerComplete(self):
        plus.disableEscape(True, 0)
        plus.fadeToBlack(.5, 0)

        self.animationStage = 1
        self.animationTimer = .75
        self.bTimer = False
        plus.showTimer(False)
        
        plus.enableVehicleControls(self.loader, False)
        plus.brake(self.loader, True, 15)
        plus.enableVehicleControls(self.truck, False)
        plus.brake(self.truck, True, 15)
        
Missions.register(YardApe)
