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

class CosaNostra(Missions.SuperMission):
    "We have a job offer you can't refuse."
    level_name = "Level 10"
    name = "Cosa Nostra"
    preview = "Levels/Level10/cosa_nostra_preview.tga"
    equipcost = 180
    pay = 20000
    perfectspeed = 600 # seconds
    worstspeed = 1200
    
    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 = 10
    accuracy_type = 2
    
    def __init__(self, levelIndex):
        Missions.SuperMission.__init__(self, levelIndex)
        
        plus.setBackColor(0, 0, 0)
        
        self.missionID = 10
        self.instructions = "<text font fg_mcn12>Early morning. Really early. Got a place ready down by the river. Boss needs a few problems buried. Just something in the trunk of that black sedan. Dig a hole at least 2 meters deep, and then push the car into the hole with the bulldozer.  Then, put those old logs on top of it.  Once that's done, bury the sedan using the excavator.  Keep your mouth shut or you'll be sleeping with the fishes - ya got it?.</text>"
        
        self.excavator = plus.addVehicleWithRotation("Excavator", self.level.startpoint, self.level.startrotation)
        plus.setPinned(self.excavator, True)
        plus.enableVehicleControls(self.excavator, False)
        
        self.dozerPosition = (34, 111)
        self.dozerRotation = (-math.pi / 2, 0, 0)
        self.dozer = plus.addVehicleWithRotation("Bulldozer", self.dozerPosition, self.dozerRotation)
        plus.enableVehicleControls(self.dozer, False)
        
        self.carPosition = (65, 46)
        self.carRotation = (0, 0, 0)
        self.car = plus.addVehicleWithRotation("Sedan", self.carPosition, self.carRotation)
        plus.enableVehicleControls(self.car, False)
        plus.soundOn(self.car, False)
        
        plus.setAvailableVehicles((0, "Excavator", 1, "Bulldozer"))
        
	self.sedansinZone = 0
	self.logsinZone = 0
	
        self.voiceovers['intro'] = "Sounds/Voiceovers/Missions/10/Description10-1.wav"
	
        self.objectives = ("Dig hole in marked area 2 meters deep.", "Push sedan into hole with the bulldozer.", "Push logs over sedan with the bulldozer.", "Fill the hole using the excavator.", "Radio in when you're done hiding all the evidence.")
	
	self.checkHoleDug = True
	self.holedug = 0
	self.holefilled = 50
        self.bHoleFinished = False
	self.bHoleRefilled = False
	
	self.checkCarInHole = False
	self.checkLogsInHole = False
	self.checkHoleRefilled = False
	
	self.bShowLogMessageOne = True
	self.bShowLogMessageTwo = True
	
        self.missionEndsWithRadioIn = True

	self.level.SetHelpArrowPosition(0, 47.5, 14.5, 42.5, 0.0) # Dig area
	
	plus.addZone(0, (6.75, 20, 13), (47.3, 3.5, 42.5), self) # Zone to fit the sedan in

    def Introduction(self):
        plus.park(self.dozer, True)
	plus.park(self.car, True)
        
        # set initial camera & fade from black
        plus.animationCamera()
        plus.setCameraPosition(68.4, 16.8, 104.3)
        plus.setCameraRotation(-.1, 1.26)
        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((68.4, 16.8, 104.3), (-.1, 1.26), math.pi / 4, (68.58, 15.45, 49.7), (.075, 3.83), math.pi / 4, 1, 6, False)
        plus.animateCamera((68.58, 15.45, 49.7), (.075, 3.83), math.pi / 4, (58.16, 18.81, 49), (.36, 4.2), math.pi / 4, 7, 12, False)
        #plus.animateCamera((58.16, 18.81, 49), (.36, 4.2), math.pi / 4, (51.08, 20.65, 47.14), (.36, 4.2), math.pi / 4, 13, 16, False)
        yield 28
        
        # done
        yield 0
        
    def IntroComplete(self):
        Missions.SuperMission.IntroComplete(self)
        plus.enableVehicleControls(self.excavator, True)
        plus.setPinned(self.excavator, False)
        plus.resetClock()
        plus.enableSwitchVehicle(True)
        self.level.AddDepthGuage()
	
	plus.park(self.car, False)
        
    def Tick(self):
	    
	# check to see if the initial hole is dug
	if self.checkHoleDug:
		if self.holedug < 60:
		    self.holedug = plus.getTerrainScore()  # [0-100] returned
		if self.holedug >= 60 and not self.bHoleFinished:
		    plus.objectiveComplete(0, True)  # objective complete is if we've dug 60% or better....accuracy improves the more you do, but it's not necessary
		    self.bHoleFinished = True
		    self.checkHoleDug = False
		    self.checkCarInHole = True
		if self.holedug > 1:
		    self.level.KillHelpArrow(0)
	
	# check to see if there is one sedan in the hole dug
	if self.checkCarInHole:
		if self.sedansinZone > 0:
			plus.objectiveComplete(1, True)
			self.checkCarInHole = False
			self.checkLogsInHole = True
			
	#check to see if there are at least three logs in the hole
	if self.checkLogsInHole:
		if self.logsinZone == 1:
			if self.bShowLogMessageOne:
				plus.showTip("Drop on a couple more logs.")
				self.bShowLogMessageOne = False
		if self.logsinZone == 2:
			if self.bShowLogMessageTwo:
				plus.showTip("One more should do it.")
				self.bShowLogMessageTwo = False
		elif self.logsinZone > 2:
			plus.objectiveComplete(2, True)
			self.checkLogsInHole = False
			self.checkHoleRefilled = True
			plus.setPhantom(self.car, True)	  # phantom, pin, activate movement constraints on the car 
			plus.setPinned(self.car, True)	   
			plus.activateMovementConstraints(self.car)
			plus.phantomTypeInZone(True, 6, 0) # phantom all logs in zone 0 
			plus.pinTypeInZone(True, 6, 0)         # pin all logs in zone 0
			
	#check to see if the hole is refilled
	if self.checkHoleRefilled:
		self.holefilled = plus.getTerrainScore() # [0-100] returned
		if self.holefilled <= 20:  #objective complete 
			plus.objectiveComplete(3, True) 
			self.checkHoleRefilled = False
	
        return Missions.SuperMission.Tick(self)

    def ZoneEvent(self, direction, id, objType):
    
	if direction == 1: # entering the zone
            if objType == 5: # 5 is the sedan
		self.sedansinZone = self.sedansinZone + 1
	    elif objType == 6: # 6 are various log models
		self.logsinZone = self.logsinZone + 1
		
	else: #exiting the zone
	    if objType == 5:
		self.sedansinZone = self.sedansinZone - 1
	    elif objType == 6:
		self.logsinZone = self.logsinZone - 1
	
        return True
        
Missions.register(CosaNostra)
