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

class GreasePole(Missions.SuperMission):
    "Raise machinery onto roof of industrial building."
    level_name = "Level 6"
    name = "Grease Pole"
    preview = "Levels/Level6/grease_pole_preview.bmp"
    equipcost = 240
    pay = 700
    perfectspeed = 240 # seconds
    worstspeed = 720
    
    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 = 6
    accuracy_type = 1
    
    def __init__(self, levelIndex):
        Missions.SuperMission.__init__(self, levelIndex)
        
        plus.setBackColor(0, 0, 0)
        
        self.missionID = 6
        self.instructions = "<text font fg_mcn12>This industrial company is putting up some big new equipment. Use your crane and hook to carefully pick up the machinery and lift it into position up on the high supports of the factory structure. Don't bump it as you move it. It costs more than your house.</text>"
        
        self.crane = plus.addVehicleWithRotation("CraneHook", self.level.startpoint, self.level.startrotation)
        plus.enableVehicleControls(self.crane, False)
        
        plus.addZone(0, (10, 8, 10), (34.5, 21, 35), self)
        plus.addZone(1, (10, 8, 10), (58.5, 25, 38), self)
        plus.addZone(2, (10, 8, 10), (99.5, 25, 78.5), self)
        
        self.voiceovers['intro'] = "Sounds/Voiceovers/Missions/6/Description6-1.wav"
        self.voiceovers['machinery'] = "Sounds/Voiceovers/Missions/6/Description6-2.wav"
        self.objectives = ("Raise machinery onto roof of industrial building.",)
        
        self.checkCraneReleased = False
        self.zone0Complete = False
        self.zone1Complete = False
        self.zone2Complete = False
        self.numCratesComplete = 0
        self.cratesComplete = False
        self.cratesInZone0 = 0
        self.cratesInZone1 = 0
        self.cratesInZone2 = 0
        
        # Drop points on building
        self.level.SetHelpArrowPosition(0, 34.5, 20.0, 35.0, 0.0)
        self.level.SetHelpArrowPosition(1, 58.5, 23.0, 38.0, 0.0)
        self.level.SetHelpArrowPosition(2, 99.5, 23.5, 78.5, 0.0)

    def Introduction(self):
        # set initial camera & fade from black
        plus.animationCamera()
        plus.setCameraPosition(89, 26, 123)
        plus.setCameraRotation(.28, 3.6)
        plus.setCameraFOV(math.pi / 4)
        plus.fadeFromBlack(.5, .5)
        plus.park(self.crane, True)

        #start playing music loop
        yield .75
        
        #intro cam, welcom comment
        self.curVoice = plus.createSound(self.voiceovers['intro'])
        plus.playSound(self.curVoice)
        yield 7.5
        
        plus.stopSound(self.curVoice)
        plus.removeSound(self.curVoice)
        self.curVoice = plus.createSound(self.voiceovers['machinery'])
        plus.playSound(self.curVoice)
        plus.animateCamera((89, 26, 123), (.28, 3.6), math.pi / 4, (52.5, 19, 79.5), (.165, 3.635), math.pi / 4, 0, 4, False)
        plus.animateCamera((52.5, 19, 79.5), (.165, 3.635), math.pi / 4, (28.58, 34, 58.5), (.625, 3.1), math.pi / 4, 5, 7.5, False) # first building top
        plus.animateCamera((28.58, 34, 58.5), (.625, 3.1), math.pi / 4, (54.22, 34.9, 68), (.295, 3.24), math.pi / 4, 8, 10.5, False) # second building top
        plus.animateCamera((54.22, 34.9, 68), (.295, 3.24), math.pi / 4, (74, 30, 79.4), (.29, 1.5), math.pi / 4, 11, 13.5, False) # third building top
        yield 15
        
        # done
        yield 0
        
    def IntroComplete(self):
        Missions.SuperMission.IntroComplete(self)
        plus.enableVehicleControls(self.crane, True)
        plus.setPinned(self.crane, False)
        plus.resetClock()
        plus.park(self.crane, False)
    
    def Tick(self):
        if self.checkCraneReleased:
            hook = plus.getHookStatus(self.crane)
            if hook == -1:
                if self.cratesInZone0 > 0 and not self.zone0Complete:
                    self.zone0Complete = True
                    self.numCratesComplete = self.numCratesComplete + 1
                    if (self.numCratesComplete == 1):
                        plus.showTip("Nice work. That's one.")
                    elif (self.numCratesComplete == 2):
                        plus.showTip("Get one more and you're done.")
                    self.checkCraneReleased = False
                elif self.cratesInZone1 > 0 and not self.zone1Complete:
                    self.zone1Complete = True
                    self.numCratesComplete = self.numCratesComplete + 1
                    if (self.numCratesComplete == 1):
                        plus.showTip("Nice work. That's one.")
                    elif (self.numCratesComplete == 2):
                        plus.showTip("Get one more and you're done.")
                    self.checkCraneReleased = False
                    print "zone 1 complete", self.cratesInZone1
                elif self.cratesInZone2 > 0 and not self.zone2Complete:
                    self.zone2Complete = True
                    self.numCratesComplete = self.numCratesComplete + 1
                    if (self.numCratesComplete == 1):
                        plus.showTip("Nice work. That's one.")
                    elif (self.numCratesComplete == 2):
                        plus.showTip("Get one more and you're done.")
                    self.checkCraneReleased = False
                    print "zone 2 complete", self.cratesInZone2
                
                if self.zone0Complete and self.zone1Complete and self.zone2Complete and not self.cratesComplete:
                    plus.objectiveComplete(0, True)
                    self.cratesComplete = True

        return Missions.SuperMission.Tick(self)

    def ZoneEvent(self, direction, id, objType):
        if direction == 1: # entering zone
            if id == 0 and objType == 4:
                self.cratesInZone0 = self.cratesInZone0 + 1
                self.checkCraneReleased = True
            elif id == 1 and objType == 4:
                self.cratesInZone1 = self.cratesInZone1 + 1
                self.checkCraneReleased = True
            elif id == 2 and objType == 4:
                self.cratesInZone2 = self.cratesInZone2 + 1
                self.checkCraneReleased = True
                
        else: # exiting zone
            if id == 0 and objType == 4:
                self.cratesInZone0 = self.cratesInZone0 - 1
                if self.cratesInZone0 == 0:
                    self.checkCraneReleased = False
            elif id == 1 and objType == 4:
                self.cratesInZone1 = self.cratesInZone1 - 1
                if self.cratesInZone1 == 0:
                    self.checkCraneReleased = False
            elif id == 2 and objType == 4:
                self.cratesInZone2 = self.cratesInZone2 - 1
                if self.cratesInZone2 == 0:
                    self.checkCraneReleased = False
        
        return True
        
Missions.register(GreasePole)
