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

class GearJammer(Missions.SuperMission):
    "Haul a load of busted concrete to the quarry and bring back some gravel."
    level_name = "Level 1 - Roadway"
    name = "Gear Jammer"
    preview = "Levels/Level1Roadway/roadway_preview.tga"
    equipcost = 400
    pay = 1100
    perfectspeed = 150 # seconds
    worstspeed = 540
    
    easy_speedfail = 20
    easy_safetyfail = 40
    easy_accuracyfail = 40
    easy_default = 55
    med_speedfail = 30
    med_safetyfail = 45
    med_accuracyfail = 45
    med_default = 62
    med_bonus = 80
    hard_speedfail = 55
    hard_safetyfail = 65
    hard_accuracyfail = 65
    hard_default = 70
    hard_bonus = 90
    
    missionNumber = 1
    accuracy_type = 2
    
    def __init__(self, levelIndex):
        Missions.SuperMission.__init__(self, levelIndex)
        plus.setBackColor(0, 0, 0)
        
        self.InitZones(0)
        
        plus.setActiveSubspace(1)
        self.quarry = None
        quarryIndex = Levels.createLevelByName("Level 1 - Quarry")
        if quarryIndex > 0:
            self.quarry = Levels.currentLevels[quarryIndex]
            self.InitZones(1)
            self.quarry.SetHelpArrowPosition(0, 50.2, 20.3, 87.7, 0.0) # Dump Zone
        plus.setActiveSubspace(0)
        
        self.missionID = 1
        self.instructions = "<text font fg_mcn12>You're the new guy on the site of a widened roadway embankment. Super said they need to take some busted concrete to the quarry to be crushed later. Park your dump truck inside the white rectangle and wait for the guys to load you up. The highway entrance will take you to the quarry. Dump your load in the fenced-in recycle zone. Then, while you're at the quarry, pick up a load of gravel. You can just use the hopper loader, but be careful backing in. Bring it back and dump it inside the white rectangle and a dozer will take it from there.</text>"
                        
        self.showQuarryIntro = True
        
        self.truck = plus.addVehicleWithRotation("DumpTruck", self.level.startpoint, self.level.startrotation)
        plus.enableVehicleControls(self.truck, False)
        self.loaderPos = (75, 67)
        self.loaderRot = (math.pi / 2, 0, 0)
        self.loader = plus.addVehicleWithRotation("FrontEndLoaderScoop", self.loaderPos, self.loaderRot)
        plus.enableVehicleControls(self.loader, False)
        plus.setPinned(self.loader, True)
        plus.setDirtAmount(self.loader, 50)
        
        self.objectives = ("Load dump truck with roadway debris.", "Drive to the quarry with load.", "Dump your load in the fenced-in recycle zone.", "Load dump truck with gravel from the hopper.", "Drive back to roadway.", "Dump gravel inside the white rectangle.")
        
        self.loadDebrisTime = 0
        self.animationStage = 0 # use this to step through stages of the loader animation
        self.armPos = 0
        self.tilted = 0
        self.stageTime = 0
        
        self.voiceovers['intro'] = "Sounds/Voiceovers/Missions/1/Description1-1.wav"
        self.voiceovers['highway'] = "Sounds/Voiceovers/Missions/1/Description1-2.wav"
        self.voiceovers['hopper'] = "Sounds/Voiceovers/Missions/1/Description1-3.wav"
        
        self.clockStarted = False
        self.hopperDumped = False
        self.terrainDumped = False
        self.roadwayDumped = False
        self.quarryDumped = False
        self.checkQuarryLoadDumped = False
        self.checkRoadwayLoadDumped = False
        
        self.level.SetHelpArrowPosition(0, 60.5, 13, 70.5, 0.0) # Load up area
    
    def __del__(self):
        Missions.SuperMission.__del__(self)
        
        plus.setActiveSubspace(0)
        self.level = None
        plus.setActiveSubspace(1)
        self.quarry = None
        plus.setActiveSubspace(0)
             
    def InitZones(self, iLevel):
        if iLevel == 0: # roadway - first time
            plus.addZone(0, (20, 10, 18), (120, 16, 35), self) # trigger time warp to quarry
            plus.addZone(4, (4, 15, 10), (61, 12.5, 71), self) # terrain debris dump / gravel dump site
        elif iLevel == 1: # quarry
            plus.addZone(1, (10, 10, 1), (123, 25, 50), self) # trigger time warp to roadway
            plus.addZone(2, (10, 10, 1), (12, 25, 50), self) # trigger time warp to roadway
            plus.addZone(3, (1, 1, 1), (98.63, 22.39, 81.34), self) # hopper dump
            plus.addZone(7, (10, 4, 4), (50.56, 21, 87.73), self) # terrain debris dump site

    def Introduction(self):
        # set initial camera & fade from black
        plus.animationCamera()
        plus.setCameraPosition(-.5, 18, 15)
        plus.setCameraRotation(.29, .9)
        plus.setCameraFOV(math.pi / 4)
        plus.fadeFromBlack(.5, .5)

        #start playing music loop
        yield .75
        
        #intro cam, welcome comment
        self.curVoice = plus.createSound(self.voiceovers['intro'])
        plus.playSound(self.curVoice)
        plus.animateCamera((-.5, 18, 15), (.29, .9), math.pi / 4, (26.19, 16.35, 31.25), (.31, .655), math.pi / 4, 0, 4, False) # zoom in on truck, pause
        plus.animateCamera((26.19, 16.35, 31.25), (.31, .655), math.pi / 4, (38.32, 16.25, 33.07), (.335, -.94), math.pi / 4, 4.5, 8, False)
        plus.animateCamera((38.32, 16.25, 33.07), (.335, -.94), math.pi / 4, (52.567, 17.92, 54.936), (.245, .67), math.pi / 4, 8, 13, False) # white rectangle and loader
        yield 16
        
        plus.stopSound(self.curVoice)
        plus.removeSound(self.curVoice)
        self.curVoice = plus.createSound(self.voiceovers['highway'])
        plus.playSound(self.curVoice)
        plus.animateCamera((52.567, 17.92, 54.936), (.245, .67), math.pi / 4, (60, 16.2, 36.6), (.19, 2.665), math.pi / 4, 0, 2.5, False) # road sign
        plus.animateCamera((60, 16.2, 36.6), (.19, 2.665), math.pi / 4, (60, 16.2, 36.6), (.135, 1.6), math.pi / 4, 3, 5, False) # highway
        
        yield 7
        
        # done
        yield 0
        
    def QuarryIntro(self):
        # set initial camera & fade from black
        plus.animationCamera()
        plus.setCameraPosition(29.9, 26.6, 105)
        plus.setCameraRotation(.26, 1.8)
        plus.setCameraFOV(math.pi / 4)
        plus.fadeFromBlack(1, .5)
        plus.park(self.truck, True)

        #start playing music loop
        yield 2.5
        
        #intro cam, welcome comment
        self.curVoice = plus.createSound(self.voiceovers['hopper'])
        plus.playSound(self.curVoice)
        plus.animateCamera((29.9, 26.6, 105), (.26, 1.8), math.pi / 4, (82.67, 27.53, 85), (.26, 1.8), math.pi / 4, 0, 4, False) # hopper
        plus.animateCamera((82.67, 27.53, 85), (.26, 1.8), math.pi / 4, (77.16, 28.73, 80.87), (.24, 4.43), math.pi / 4, 5, 8, False) # turn around
        plus.animateCamera((77.16, 28.73, 80.87), (.24, 4.43), math.pi / 4, (25.96, 26.46, 59.46), (.24, 4.43), 0.785, 8, 12, False) # truck
        
        yield 13
        
        # done
        yield 0
        
    def IntroComplete(self):
        Missions.SuperMission.IntroComplete(self)
        plus.park(self.truck, False)
	plus.enableVehicleControls(self.truck, True)
	
        if self.clockStarted == False:
            plus.resetClock()
            self.clockStarted = True
        
    def Tick(self):
        if self.loadDebrisTime > 0:
            self.loadDebrisTime -= .25
            if self.loadDebrisTime <= 0:
                self.loadDebrisTime = 0
                plus.removeVehicles()
                self.truck = plus.addVehicleWithRotation("DumpTruck", (61, 73.3), (math.pi, 0, 0))
                plus.enableVehicleControls(self.truck, False)
                plus.setPinned(self.truck, True)
                self.loader = plus.addVehicleWithRotation("FrontEndLoaderScoop", self.loaderPos, self.loaderRot)
                plus.enableVehicleControls(self.loader, False)
                plus.park(self.loader, True)
                plus.setDirtAmount(self.loader, 50)
                plus.animationCamera()
                plus.setCameraPosition(59, 25, 46)
                plus.setCameraRotation(.315, .435)
                plus.setCameraFOV(math.pi / 4)
                plus.disableEscape(False, .75)
                plus.fadeFromBlack(.5, 0)
                self.terrainDumped = True
                self.animationStage = 1
        
        # loader animation
        if self.animationStage == 1: # raise arm
            self.armPos = plus.getArmStatus(self.loader)
            if self.armPos < 1:
                plus.raiseArm(self.loader, True, 100)
            else:
                plus.raiseArm(self.loader, False, 100)
                plus.addZone(5, (1, 1, 5), (63.5, 15.5, 70), self) # this zone will stop the loader when it gets to the truck
                self.animationStage = 2
        elif self.animationStage == 2: # drive toward truck - ZoneEvent will stop the loader when it gets near the truck, zone 5
            plus.park(self.loader, False)
            plus.drive(self.loader, True, -100)
            self.animationStage = 3
        elif self.animationStage == 3: # zone 5 starts the scoop tilting, stop it when it's all the way down and wait a few seconds for the load to empty
            if self.tilted != -1:
                self.tilted = plus.getScoopStatus(self.loader)
                if self.tilted == -1: # all the way down, so stop tilting
                    plus.tiltScoop(self.loader, False, 0)
            if self.stageTime > 0:
                self.stageTime -= .25
                if self.stageTime <= 0:
                    #plus.setDirtAmount(self.truck, 50)
                    self.tilted = 0
                    plus.tiltScoop(self.loader, True, 100) # raise scoop back up
                    self.animationStage = 4
        elif self.animationStage == 4: # when scoop is back up, drive away
            if self.tilted != 1:
                self.tilted = plus.getScoopStatus(self.loader)
            else:
                plus.tiltScoop(self.loader, False, 0) # stop trying to tilt scoop up
                plus.addZone(6, (1, 1, 1), (80, 15.5, 70), self) # this zone will stop the loader when it is backed away
                plus.park(self.loader, False)
                plus.drive(self.loader, True, 100)
                self.animationStage = 5
        elif self.animationStage == 6: # 5 is handled by ZoneEvent -- all done
            if self.stageTime > 0:
                self.stageTime -= .25
                if self.stageTime <= 0:
                    plus.lastSelectedCamera()
                    plus.disableEscape(False, .75)
                    plus.fadeFromBlack(.5, 0)
                    plus.enableVehicleControls(self.truck, True)
                    plus.setPinned(self.truck, False)
                    plus.objectiveComplete(0, True)
                    self.animationStage = 0 # animation complete
                    
                    self.level.SetHelpArrowPosition(0, 128, 13, 37.4, 0.0) # Exit
                    
        if self.checkQuarryLoadDumped:
            dirt = plus.getDirtAmount(self.truck)
            if dirt <= 0:
                self.checkQuarryLoadDumped = False
                plus.objectiveComplete(2, True)
                self.quarry.SetHelpArrowPosition(0, 98.5, 30.0, 81.3, 0.0) # Hopper
                
        if self.checkRoadwayLoadDumped:
            dirt = plus.getDirtAmount(self.truck)
            if dirt <= 0:
                self.checkRoadwayLoadDumped = False
                plus.objectiveComplete(5, True)
                self.level.KillHelpArrow(0)
                    
        return Missions.SuperMission.Tick(self)

    def ZoneEvent(self, direction, id, objType):
        if id == 0 and objType == 1: # time warp to quarry
            if direction == 1:
                if plus.isObjectiveComplete(0):
                    plus.fadeToBlack(.5, 0)
                    self.iScoops = plus.getDirtAmount(self.truck)
                    plus.removeVehicles()
                    plus.hideLevel(self.level)
                    plus.setActiveSubspace(1)
                    Levels.setLevel(self.quarry, False)
                    
                    self.terrainDumped = True # the loader won't be there next time we go to the roadway, so don't try to dump terrain debris
                        
                    self.truck = plus.addVehicleWithRotation("DumpTruck", self.quarry.startpoint, self.quarry.startrotation)
                    plus.setDirtAmount(self.truck, self.iScoops)
                    
                    plus.showLevel(self.quarry)
                    
                    if self.showQuarryIntro == True:
                        plus.enableVehicleControls(self.truck, False)
                        self.Intro = self.QuarryIntro()
                        self.bDoingIntro = True
                        plus.setIntroPlaying(True)
                        self.showQuarryIntro = False
                        self.introPause = 0
                    else:
                        plus.lastSelectedCamera()
                        plus.fadeFromBlack(.5, .5)
                    
                    if not plus.isObjectiveComplete(1):
                        plus.objectiveComplete(1, True)
                        self.level.KillHelpArrow(0)
                else:
                    plus.endMission()
                
        elif id == 1 or id == 2 and objType == 1: # time warp to roadway
            if direction == 1:
                plus.fadeToBlack(.5, 0)
                self.iScoops = plus.getDirtAmount(self.truck)
                plus.removeVehicles()
                plus.hideLevel(self.quarry)
                plus.setActiveSubspace(0)
                Levels.setLevel(self.level, False)
                
                self.truck = plus.addVehicleWithRotation("DumpTruck", (90, 37.5), (math.pi / 2.0, 0, 0))
                plus.setDirtAmount(self.truck, self.iScoops)
                
                plus.showLevel(self.level)
                plus.lastSelectedCamera()
                plus.fadeFromBlack(.5, .5)
                
                plus.objectiveComplete(4, True)
                
                # destroy all help arrows
                self.quarry.KillHelpArrow(0)
                self.quarry.KillHelpArrow(1)
        
        elif id == 3 and objType == 1: # hopper dump
            if direction == 1 and self.hopperDumped == False:
                self.quarry.DumpHopperDirt()
                self.hopperDumped = True
                
                plus.objectiveComplete(3, True)
                # Exit help arrows
                self.quarry.SetHelpArrowPosition(0, 13.7, 22.8, 40.0, 0.0)
                self.quarry.SetHelpArrowPosition(1, 118.0, 24.5, 40.0, 0.0)
        
        elif id == 4: # front end loader dump
            if direction == 1 and self.loadDebrisTime == 0 and not self.terrainDumped and objType == 1:
                self.LoadDebris()
            elif direction == 1 and objType == 2 and plus.isObjectiveComplete(4) and not self.roadwayDumped:
                self.checkRoadwayLoadDumped = True
                self.roadwayDumped = True
                
        elif id == 5 and self.animationStage == 3 and objType == 1: # stop loader and start tilting scoop
            plus.drive(self.loader, False, 0)
            plus.park(self.loader, True)
            plus.tiltScoop(self.loader, True, -100)
            self.tilted = 0
            self.stageTime = 4
            self.animationStage = 3
        
        elif id == 6 and self.animationStage == 5 and objType == 1: # back away from truck
            plus.drive(self.loader, False, 0)
            plus.park(self.loader, True)
            plus.disableEscape(True, 0)
            plus.fadeToBlack(.5, 0)
            self.stageTime = .75
            self.animationStage = 6
            
        elif id == 7:
            if direction == 1 and objType == 2 and not self.quarryDumped:
                self.checkQuarryLoadDumped = True
                self.quarryDumped = True

        return True
        
    def LoadDebris(self):
        plus.disableEscape(True, 0)
        plus.fadeToBlack(.5, 0)
        self.loadDebrisTime = .75
        plus.enableVehicleControls(self.truck, False)

Missions.register(GearJammer)
