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

class Cowboy(Missions.SuperMission):
    "Haul supplies up to workers on construction platform."
    level_name = "Level 8"
    name = "Cowboy"
    preview = "Levels/Level8/cowboy_preview.bmp"
    equipcost = 350
    pay = 925
    perfectspeed = 780 # seconds
    worstspeed = 1260
    
    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 = 8
    accuracy_type = 6
    
    def __init__(self, levelIndex):
        Missions.SuperMission.__init__(self, levelIndex)
        
        plus.setBackColor(0, 0, 0)
        
        self.missionID = 8
        self.instructions = "<text font fg_mcn12>The guys need more supplies as they work on the upper level of this building. Use loader with straight fork to carry skids of materials from the storage area and set them near the crane. Hook the loads and set them on the second floor platform. We need about six of them up there.</text>"
        
        self.loader = plus.addVehicleWithRotation("FrontEndLoaderFork", self.level.startpoint, self.level.startrotation)
        plus.setPinned(self.loader, True)
        plus.enableVehicleControls(self.loader, False)
        
        self.cranePosition = (26, 37)
        self.craneRotation = (math.pi, 0, 0)
        self.crane = plus.addVehicleWithRotation("CraneHook", self.cranePosition, self.craneRotation)
        plus.enableVehicleControls(self.crane, False)
        
        plus.setAvailableVehicles((0, "Loader", 1, "Crane"))
        
        plus.addZone(0, (60, 6, 9), (71, 21.5, 41), self)
        
        self.cratesInZone = 0
        self.checkCraneReleased = False
        
        self.voiceovers['intro'] = "Sounds/Voiceovers/Missions/8/Description8-1.wav"
        self.voiceovers['hook'] = "Sounds/Voiceovers/Missions/8/Description8-2.wav"
        
        self.objectives = ("Raise supplies onto construction platform.",)

    def Introduction(self):
        plus.park(self.crane, True)

        # set initial camera & fade from black
        plus.animationCamera()
        plus.setCameraPosition(-8.3, 25.6, 28)
        plus.setCameraRotation(.155, 1.24)
        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)
        plus.animateCamera((-8.3, 25.6, 28), (.155, 1.24), math.pi / 4, (48.5, 20.4, 69.35), (.25, 1.13), math.pi / 4, 2, 9, False)
        yield 11.5
        
        plus.stopSound(self.curVoice)
        plus.removeSound(self.curVoice)
        self.curVoice = plus.createSound(self.voiceovers['hook'])
        plus.playSound(self.curVoice)
        plus.animateCamera((48.5, 20.4, 69.35), (.25, 1.13), math.pi / 4, (26.45, 27, 78.8), (.09, 2.6), math.pi / 4, 0, 4, False)
        yield 6
        
        # done
        yield 0
        
    def IntroComplete(self):
        Missions.SuperMission.IntroComplete(self)
        plus.enableVehicleControls(self.loader, True)
        plus.setPinned(self.loader, False)
        plus.resetClock()
        plus.enableSwitchVehicle(True)
        plus.park(self.crane, False)
	
    def Tick(self):
        if self.checkCraneReleased:
            hook = plus.getHookStatus(self.crane)
            if hook == -1:
                if self.cratesInZone == 4:
                    plus.showTip("Nice work.  Only five more to go.")
                    self.checkCraneReleased = False
                elif self.cratesInZone == 12:
                    plus.showTip("You're halfway there.")
                    self.checkCraneReleased = False
                elif self.cratesInZone == 20:
                    plus.showTip("One more crate up here and you're done.")
                    self.checkCraneReleased = False
                elif self.cratesInZone == 24:
                    plus.objectiveComplete(0, True)
                    self.checkCraneReleased = False
            
        return Missions.SuperMission.Tick(self)

    def ZoneEvent(self, direction, id, objType):
        if direction == 1: # entering the zone
            if objType == 4:
                self.cratesInZone = self.cratesInZone + 1
                
                 # each crate actually triggers the ZoneEvent 4 times, so when we have 32, we actually have 8
                if self.cratesInZone == 4 or self.cratesInZone == 12 or self.cratesInZone == 20 or self.cratesInZone == 24:
                    self.checkCraneReleased = True
        else: # exiting the zone
            if objType == 4:
                self.cratesInZone = self.cratesInZone - 1
                if self.cratesInZone != 4 and self.cratesInZone != 12 and self.cratesInZone != 20 and self.cratesInZone != 24:
                    self.checkCraneReleased = False
                
        return True
        
Missions.register(Cowboy)
