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

class Swamper(Missions.SuperMission):
    "Dig trenches and put down drainage pipe."
    level_name = "Level 7"
    name = "Swamper"
    preview = "Levels/Level7/swamper_preview.bmp"
    equipcost = 200
    pay = 920
    perfectspeed = 630 # 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 = 7
    accuracy_type = 8
    
    def __init__(self, levelIndex):
        Missions.SuperMission.__init__(self, levelIndex)
        
        plus.setBackColor(0, 0, 0)
        
        self.missionID = 7
        self.instructions = "<text font fg_mcn12>We need some drainage pipes buried before we can proceed with this roadway. Dig a nice trench one meter deep with the excavator, then pick up and lower the cement pipe segments into it. Keep the segments close to each other. When you're done, fill in the trench with the excavator.</text>"
        
        self.excavator = plus.addVehicleWithRotation("Excavator", self.level.startpoint, self.level.startrotation)
        plus.setPinned(self.excavator, True)
        plus.enableVehicleControls(self.excavator, False)
        
        #self.dozerPosition = (85, 62)
        #self.dozerRotation = (math.pi / 2, 0, 0)
        #self.dozer = plus.addVehicleWithRotation("Bulldozer", self.dozerPosition, self.dozerRotation)
        #plus.enableVehicleControls(self.dozer, False)
        
        self.cranePosition = (85, 72)
        self.craneRotation = (math.pi / 2, 0, 0)
        self.crane = plus.addVehicleWithRotation("CraneHook", self.cranePosition, self.craneRotation)
        plus.enableVehicleControls(self.crane, False)
        
        #plus.setAvailableVehicles((0, "Excavator", 1, "Bulldozer", 2, "Crane"))
        plus.setAvailableVehicles((0, "Excavator", 1, "Crane"))
        
        self.voiceovers['intro'] = "Sounds/Voiceovers/Missions/7/Description7-1.wav"
        self.voiceovers['pipes'] = "Sounds/Voiceovers/Missions/7/Description7-2.wav"
        
        self.objectives = ("Dig a trench 1 meter deep with the excavator.", "Lay four pipes end to end in the trench with the crane.", "Bury pipes with the excavator.", "Radio in when ready to call it a day.")
        
	#initial hole digging params
	self.checkHoleDug = True
	self.holedug = 0
	self.bHoleFinished = False
		
	#pipe placement params
	self.checkPipesPlaced = False
	self.pipesinZone = 0
	self.bShowPipeMessage1 = True
	self.bShowPipeMessage2 = True
	self.bShowPipeMessage3 = True
	self.bShowPipeMessage4 = True
	
	self.pipeinZone0 = False
	self.pipeinZone1 = False
	self.pipeinZone2 = False
	self.pipeinZone3 = False
	self.justActivatedPipe = -1
	
	#hole refilling params
	self.checkHoleRefilled = False
	self.holerefilled = 50
	
        self.missionEndsWithRadioIn = True
        
        self.level.SetHelpArrowPosition(0, 63.0, 11.7, 70.9, 60.0) # Trench area
        self.level.SetHelpArrowPosition(1, 82.0, 14.0, 81.9, 60.0) # Pipe marker
	
	self.craneHookStatus = -1 # crane starts out not hooked to anything
	
	plus.addZone(0, (3.536, 10, 3.7), (56.768, 6.2, 70.85), self) # Zone to fit pipe segment in
	plus.addZone(1, (3.536, 10, 3.7), (60.304, 6.2, 70.85), self) 
	plus.addZone(2, (3.536, 10, 3.7), (63.84, 6.2, 70.85), self) 
	plus.addZone(3, (3.536, 10, 3.7), (67.376, 6.2, 70.85), self) 

    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.dozer, True)
        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 6
        
        plus.stopSound(self.curVoice)
        plus.removeSound(self.curVoice)
        self.curVoice = plus.createSound(self.voiceovers['pipes'])
        plus.playSound(self.curVoice)
        plus.animateCamera((89, 26, 123), (.28, 3.6), math.pi / 4, (69.5, 23.1, 87.3), (.66, 3.54), math.pi / 4, 0, 3, False)
        plus.animateCamera((69.5, 23.1, 87.3), (.66, 3.54), math.pi / 4, (54.1, 25.6, 73.4), (.5, 1.46), math.pi / 4, 4, 7, False)
        plus.animateCamera((54.1, 25.6, 73.4), (.5, 1.46), math.pi / 4, (72.4, 18.44, 52.8), (.465, 1.636), math.pi / 4, 8, 11, False)
        yield 13.5
        
        # 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()
        
    def Tick(self):
    
	#get the crane's hook status
    	self.craneHookStatus = plus.getHookStatus(self.crane)
    
	# check to see if the hole for the pipes is dug
	if self.checkHoleDug:
		if self.holedug < 70:
		    self.holedug = plus.getTerrainScore()  # [0-100] returned
		if self.holedug >= 70 and not self.bHoleFinished:
		    plus.objectiveComplete(0, True)  # objective complete is if we've dug 75% or better....accuracy improves the more you do, but it's not necessary
		    self.bHoleFinished = True
		    self.checkHoleDug = False
		    self.checkPipesPlaced = True
		if self.holedug > 1:
		    self.level.KillHelpArrow(0)

	if self.checkPipesPlaced:
		if self.justActivatedPipe > -1:
			if self.craneHookStatus != -1:  #unhook the item if it's hooked
				plus.unhook(self.crane)
				self.craneHookStatus = -1
			# now align the pipe into the zone, place it near the top, and pin it into position
			plus.lockdownTypeInZone(1.57,0,1,0,7,self.justActivatedPipe)
			self.justActivatedPipe = -1
    
		if self.pipesinZone == 1:
			self.level.KillHelpArrow(1)
			if self.bShowPipeMessage1:
				#plus.showTip("First pipe.")
				self.bShowPipeMessage1 = False
				self.level.RecordDepthAccuracy()
		elif self.pipesinZone == 2:
			if self.bShowPipeMessage2:
				#plus.showTip("Second pipe.")
				plus.showTip("Two more pipes to go.");
				self.bShowPipeMessage2 = False
		elif self.pipesinZone == 3:
			if self.bShowPipeMessage3:
				#plus.showTip("Third pipe.")
				plus.showTip("One pipe left.");
				self.bShowPipeMessage3 = False
		elif self.pipesinZone == 4:
			if self.bShowPipeMessage4:
				#plus.showTip("Fourth pipe.")
				plus.objectiveComplete(1, True)
				self.bShowPipeMessage4 = False
				self.checkPipesPlaced = False
				self.checkHoleRefilled = True
				
	if self.checkHoleRefilled:
		self.holerefilled = plus.getTerrainScore()
		if self.holerefilled <= 20:
			plus.objectiveComplete(2, True)
			self.checkHoleRefilled = False
	
        return Missions.SuperMission.Tick(self)

    def ZoneEvent(self, direction, id, objType):
	if self.checkPipesPlaced:
		if self.justActivatedPipe == -1: #prevent us from tagging multiple zones with a single pipe
			if direction == 1: # entering the zone
				if objType == 7: # 7 is pipe/flatbed models
					if id == 0: 
						if not self.pipeinZone0: # a pipe has entered zone 0 for the first time...lock it down
							self.pipeinZone0 = True
							self.justActivatedPipe = id
							self.pipesinZone = self.pipesinZone + 1
					elif id == 1:
						if not self.pipeinZone1:
							self.pipeinZone1 = True
							self.justActivatedPipe = id
							self.pipesinZone = self.pipesinZone + 1
					elif id == 2:
						if not self.pipeinZone2:
							self.pipeinZone2 = True
							self.justActivatedPipe = id
							self.pipesinZone = self.pipesinZone + 1
					elif id == 3:
						if not self.pipeinZone3:
							self.pipeinZone3 = True
							self.justActivatedPipe = id
							self.pipesinZone = self.pipesinZone + 1
        return True
        
Missions.register(Swamper)
