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

class RubbleTrouble(Missions.SuperMission):
    "Tear down a building and move the rubble out of the way."
    level_name = "Level 9"
    name = "Rubble Trouble"
    preview = "Levels/Level9/rubble_trouble_preview.bmp"
    equipcost = 870
    pay = 1240
    perfectspeed = 1680 # seconds
    worstspeed = 3300
    
    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 = 9
    accuracy_type = 7
    safety_type = 1
    
    def __init__(self, levelIndex):
        Missions.SuperMission.__init__(self, levelIndex)
        
        plus.setBackColor(0, 0, 0)
        
        self.missionID = 9
        self.instructions = "<text font fg_mcn12>This old department store has to come down. You won't be able to get to it all with your crane unless you knock part down and then use the bulldozer to get the rubble out of the way. Put it into the dump truck and take it to the dump. Then you can move your crane in to finish the job.</text>"

        self.crane = plus.addVehicleWithRotation("CraneHook", self.level.startpoint, self.level.startrotation)
        plus.enableVehicleControls(self.crane, False)
        
        self.dozerPosition = (78, 106)
        self.dozerRotation = (-math.pi / 2, 0, 0)
        self.dozer = plus.addVehicleWithRotation("Bulldozer", self.dozerPosition, self.dozerRotation)
        plus.enableVehicleControls(self.dozer, False)

        self.loaderPosition = (76.5, 88)
        self.loaderRotation = (-math.pi / 2, 0, 0)
        self.loader = plus.addVehicleWithRotation("FrontEndLoaderFork", self.loaderPosition, self.loaderRotation)
        plus.enableVehicleControls(self.loader, False)

        self.flatbedPosition = (65.5, 60.5)
        self.flatbedRotation = (0, 0, 0)
        self.flatbed = plus.addVehicleWithRotation("FlatbedTruck", self.flatbedPosition, self.flatbedRotation)
        plus.enableVehicleControls(self.flatbed, False)

        plus.setAvailableVehicles((0, "Crane", 1, "Bulldozer", 2, "Loader"))
        plus.setAvailableTools(("CraneHook", "Hook", "CraneWreckingBall", "Wrecking Ball"))

        plus.addZone(0, (12, 7, 50), (99, 20, 76), self) # crate moved from top of building
        plus.addZone(1, (3, 5, 7), (65.5, 15, 62.5), self) # crate on flatbed
        plus.addZone(2, (30, 7, 30), (65.85, 14.4, 61.86), self) # vehicle drive away from flatbed

        self.missionEndsWithRadioIn = True
        self.buildingDown = False
        self.cratesInZone = 0
        self.cratesOnTruck = 0
        self.checkCraneReleased = False
        self.checkCraneHooked = True
        self.truckGone = False
        self.animationStage = 0
        self.animationTimer = 0

        self.voiceovers['intro'] = "Sounds/Voiceovers/Missions/9/Description9-1.wav"
        self.voiceovers['crane'] = "Sounds/Voiceovers/Missions/9/Description9-2.wav"
        self.voiceovers['area'] = "Sounds/Voiceovers/Missions/9/Description9-3.wav"
        
        self.objectives = ("Move crate off of building.", "Load crate onto flatbed truck.", "Tear down building.", "Move building debris into marked area.")
        
    def Introduction(self):
        plus.park(self.dozer, True)
        plus.park(self.loader, True)
        plus.park(self.flatbed, True)
	plus.park(self.crane, True)

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

        #start playing music loop
        yield .75
        
        #intro cam, welcom comment
        self.curVoice = plus.createSound(self.voiceovers['intro'])
        plus.playSound(self.curVoice)
        plus.animateCamera((77, 18.6, 40.8), (.225, .69), math.pi / 4, (85.17, 22.75, 71.08), (.195, .89), math.pi / 4, 2, 7, False)
        plus.animateCamera((85.17, 22.75, 71.08), (.195, .89), math.pi / 4, (73.4, 18.9, 69.67), (.44, -2.29), math.pi / 4, 9, 14, False)
        yield 21
        
        plus.stopSound(self.curVoice)
        plus.removeSound(self.curVoice)
        self.curVoice = plus.createSound(self.voiceovers['crane'])
        plus.playSound(self.curVoice)
        plus.animateCamera((73.4, 18.9, 69.67), (.44, -2.29), math.pi / 4, (46.14, 24.6254, 60.197), (.385, -1.265), math.pi / 4, 0, 4, False)
        yield 7
        
        plus.stopSound(self.curVoice)
        plus.removeSound(self.curVoice)
        self.curVoice = plus.createSound(self.voiceovers['area'])
        plus.playSound(self.curVoice)
        plus.animateCamera((46.14, 24.6254, 60.197), (.385, -1.265), math.pi / 4, (66, 39.62, 60), (1.115, 1.56), math.pi / 4, 0, 4, False)
        yield 7
        
        # done
        yield 0
        
    def IntroComplete(self):
        Missions.SuperMission.IntroComplete(self)
        plus.enableVehicleControls(self.crane, True)
        plus.park(self.crane, False)
        plus.resetClock()
        plus.enableSwitchVehicle(True)
        plus.enableSwitchTool(True)
        self.level.SetHelpArrowPosition(0, 100.5, 23.3, 84.5, 30.0) # Crate
        
    def Tick(self):
        if not self.buildingDown and plus.isBuildingDown():
            plus.objectiveComplete(2, True)
            self.buildingDown = True
            self.level.SetHelpArrowPosition(0, 38.5, 12.0, 60.0, 40.0) # Square
        
        if self.checkCraneReleased:
            hook = plus.getHookStatus(self.crane)
            if hook == -1 and self.cratesInZone == 0:
                plus.objectiveComplete(0, True)
                self.checkCraneReleased = False
                self.level.SetHelpArrowPosition(0, 65.5, 15.5, 60.5, 30.0) # Truck


        if self.checkCraneHooked:
            hook = plus.getHookStatus(self.crane)
            if hook == 1:
                self.checkCraneReleased = True
                self.checkCraneHooked = False
                
        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(58.11, 22, 74.5)
                    plus.setCameraRotation(.4, 2.74)
                    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 truck_horn sound and drive away
            if self.animationTimer > 0:
                self.animationTimer -= .25
                if self.animationTimer <= 0:
                    plus.playSoundOnce("Sounds/truck_horn.wav")
                    plus.addZone(3, (10, 10, 1), (64, 17, 7), self)
                    plus.park(self.flatbed, False)
		    plus.unhook(self.crane)  #unhook the crane in case it somehow rehooked to the crate
                    plus.drive(self.flatbed, True, -100)
        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(3)
                    self.level.RemovePhysicalObjectByFilename("Resources/Levels/Models/wood_crate.gmf")
                    self.flatbed = None
                    plus.lastSelectedCamera()
                    #plus.park(plus.getVehicle(plus.getCurrentVehicle()), False)
                    plus.brake(self.loader, False, 100)
                    plus.brake(self.crane, False, 100)
                    plus.brake(self.dozer, False, 100)
                    plus.enableVehicleControls(plus.getVehicle(plus.getCurrentVehicle()), True)
                    plus.disableEscape(False, .75)
                    plus.fadeFromBlack(.5, 0)
                    self.animationStage = 0 # all done
                    plus.objectiveComplete(1, True)
                    self.level.SetHelpArrowPosition(0, 99.5, 20.5, 75.5, 30.0) # Building


        return Missions.SuperMission.Tick(self)

    def ZoneEvent(self, direction, id, objType):
        if direction == 1: # entering the zone
            # crate enters zone on building
            if id == 0 and objType == 4:
                self.cratesInZone = self.cratesInZone + 1
                
                 # each crate actually triggers the ZoneEvent 4 times, so when we have 4, we actually have 1
                if self.cratesInZone > 0:
                    self.checkCraneReleased = False
            
            # crate enters zone on truck
            elif id == 1 and objType == 4:
                self.cratesOnTruck = self.cratesOnTruck + 1
                
            # truck hits end of road
            if id == 3 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
                
        else: # exiting the zone
            # crate leaves zone on building
            if id == 0 and objType == 4:
                self.cratesInZone = self.cratesInZone - 1
                if self.cratesInZone == 0:
                    self.checkCraneReleased = True
            
            # crate leaves zone on truck
            elif id == 1 and objType == 4:
                self.cratesOnTruck = self.cratesOnTruck - 1
            
            # vehicle leaves zone surrounding truck - time to drive away
            elif id == 2 and objType == 1 and self.cratesOnTruck > 0 and not self.truckGone:
                self.truckGone = True
                plus.disableEscape(True, 0)
                plus.fadeToBlack(.5, 0)
                self.animationStage = 1
                self.animationTimer = .75
                plus.enableVehicleControls(self.loader, False)
                plus.brake(self.loader, True, 100)
                plus.enableVehicleControls(self.crane, False)
                plus.brake(self.crane, True, 100)
                plus.enableVehicleControls(self.dozer, False)
                plus.brake(self.dozer, True, 100)
                

        return True
        
    def SwitchedVehicles(self, iVehicle):
        if iVehicle == 0:
            plus.enableSwitchTool(True)
        else:
            plus.enableSwitchTool(False)
            
    def SwitchedTools(self, iVehicle):
        self.crane = plus.getVehicle(iVehicle)
        
Missions.register(RubbleTrouble)
