from __future__ import generators
import plus
import Arenas
import random
import HazardsPhazer
    #To make a new Hazard file, just change the name after "import Hazards"(just above this line).
    # (I would leave the "Hazards" to indicate content(i.e."Hazards" to "Hazards_X").)
    # (I changed this one to "HazardsPhazer").
    #Then below, under the heading "def HazardsOn(self, on):" and its sub-heading "if on:",
    # is  "self.flame1a = Hazards.Flame((-1, -.19, 9)...." followed by its XYZ location and such.
    #Change only "Hazards" to the same new hazard file name "Hazard_XXXX". (Do not change the hazard itself[Flame]).
    #Summary:The "import" above and the word "Hazards' in "self.flame1a = Hazards.Flame((-1, -.19, 9)...." below,
    # are the only 2 places in an 'Arena' file that refer it to its hazards file. 
        #Notes:
        #"Hazard" is the .py file name, "Flame" is the particular hazard inside it that
        # you want to use (and maybe change its settings...).
        #There are several different hazards-'Flame', 'Spikes', 'Saws', 'Hammer',... .
        #Some arenas have multiple hazards.

import math

class ClawTophpr(Arenas.SuperArena):
    "This Magnetron, with 'Reversed Polarity' impulse field, will pick up and throw you out of the arena, and the Photon-Phazers will still disintegrate you from the inside out!."
    name = "Magnetic Photon-Phazer Arena-R"
    preview = "tabletop-magnetron/clawtop-magnetron-R_preview.bmp"
    game_types = ['TABLETOP']
    extent = (-13, 13, 14, -14)

    def __init__(self):
        Arenas.SuperArena.__init__(self, "Arenas/tabletop-magnetron/clawtop-magnetron-R.gmf")
        #plus.Arena.__init__(self, "")
        plus.setBackColor(0,0,0)
        
        fov = 0.015
        self.AddStaticCamera("Static Cam 1", (0, 19, -24), (0.8,0), 63*fov)
        self.AddStaticCamera("Static Cam 2", (9, 19, -24), (0.6196,-0.208), 63*fov)
        self.AddStaticCamera("Static Cam 3", (19, 19, -9), (0.8196,-1), 63*fov)
        #self.AddStaticCamera("Static Cam 4", (19, 13, 0), (0.8,-1.55), 75*fov)

        self.AddStaticCamera("Overhead Cam 5", (0, 24, 0), (1.5708,-3.14), 70*fov)

        self.AddWatchCamera("Watch Cam 1", (2.257, 6, -19.04), (10, 18, 55*fov, 20*fov))
        self.AddWatchCamera("Watch Cam 2", (0, 8.5, 0), (8, 15, 80*fov, 60*fov))

        self.SetSubMaterialSound("clawpipe", "rubber", .9, "Sounds\\magnetloud-Short-L.wav")######-material+sound for claw.
        self.SetSubMaterialSound("clawpipe2", "rubber", .9, "Sounds\\magnetloud-Short-R.wav")#####-material+sound for claw.

        self.players = ()
        
    def __del__(self):
        if self.bHazardsOn:
            plus.removeSound(self.ambience)
        Arenas.SuperArena.__del__(self)
        
    def AddShadowReceivers(self):
        self.SetShadowSource(5.897, 19.159, 5.899)
        
        #Main Arena Floor Shadow Triangles
        self.AddShadowTriangle((-10.2877,0.676244,1.83754), (-12.4182,0.676244,12.4283), (-12.4182,0.676244,1.83754))
        self.AddShadowTriangle((-8.17686,0.676244,9.91855), (-8.17686,0.676244,12.4283), (-12.4182,0.676244,12.4283))
        self.AddShadowTriangle((-8.17686,0.676244,9.91855), (-12.4182,0.676244,12.4283), (-10.2877,0.676244,1.83754))
        self.AddShadowTriangle((2.25999,0.676244,12.4283), (-2.49123,0.676244,12.4283), (-2.49123,0.676244,9.91855))
        self.AddShadowTriangle((2.25999,0.676244,9.91855), (2.25999,0.676244,12.4283), (-2.49123,0.676244,9.91855))
        self.AddShadowTriangle((12.413,0.676244,12.4283), (7.93297,0.676244,12.4283), (7.93297,0.676244,9.91855))
        self.AddShadowTriangle((10.2878,0.676244,1.91641), (12.413,0.676244,12.4283), (7.93297,0.676244,9.91855))
        self.AddShadowTriangle((10.2878,0.676244,1.91641), (12.413,0.676244,1.91641), (12.413,0.676244,12.4283))
        self.AddShadowTriangle((8.17711,0.676244,-9.92447), (12.413,0.676244,-12.4279), (10.2878,0.676244,-1.93329))
        self.AddShadowTriangle((10.2878,0.676244,-1.93329), (12.413,0.676244,-12.4279), (12.413,0.676244,-1.93329))
        self.AddShadowTriangle((8.17711,0.676244,-9.92447), (8.17711,0.676244,-12.4279), (12.413,0.676244,-12.4279))
        self.AddShadowTriangle((-2.26871,0.676244,-12.4279), (2.49519,0.676244,-12.4279), (2.49519,0.676244,-9.92447))
        self.AddShadowTriangle((-2.26871,0.676244,-9.92447), (-2.26871,0.676244,-12.4279), (2.49519,0.676244,-9.92447))
        self.AddShadowTriangle((-12.4182,0.676244,-12.4279), (-7.93688,0.676244,-12.4279), (-7.93688,0.676244,-9.92447))
        self.AddShadowTriangle((-10.2877,0.676244,-1.8361), (-12.4182,0.676244,-12.4279), (-7.93688,0.676244,-9.92447))
        self.AddShadowTriangle((-10.2877,0.676244,-1.8361), (-12.4182,0.676244,-1.8361), (-12.4182,0.676244,-12.4279))
        self.AddShadowTriangle((-2.49123,0.676244,9.91855), (-8.17686,0.676244,9.91855), (-10.2877,0.676244,1.83754))
        self.AddShadowTriangle((2.49519,0.676244,-9.92447), (8.17711,0.676244,-9.92447), (10.2878,0.676244,-1.93329))
        self.AddShadowTriangle((10.2878,0.676244,-1.93329), (-10.2877,0.676244,-1.8361), (-2.26871,0.676244,-9.92447))
        self.AddShadowTriangle((10.2878,0.676244,1.91641), (7.93297,0.676244,9.91855), (2.25999,0.676244,9.91855))
        self.AddShadowTriangle((2.25999,0.676244,9.91855), (-2.49123,0.676244,9.91855), (-10.2877,0.676244,1.83754))
        self.AddShadowTriangle((10.2878,0.676244,1.91641), (2.25999,0.676244,9.91855), (-10.2877,0.676244,1.83754))
        self.AddShadowTriangle((10.2878,0.676244,-1.93329), (10.2878,0.676244,1.91641), (-10.2877,0.676244,1.83754))
        self.AddShadowTriangle((-10.2877,0.676244,-1.8361), (10.2878,0.676244,-1.93329), (-10.2877,0.676244,1.83754))
        self.AddShadowTriangle((-2.26871,0.676244,-9.92447), (2.49519,0.676244,-9.92447), (10.2878,0.676244,-1.93329))
        self.AddShadowTriangle((-10.2877,0.676244,-1.8361), (-7.93688,0.676244,-9.92447), (-2.26871,0.676244,-9.92447))
        
        #Hazard Shadow Triangles
        self.AddShadowTriangle((-8.0217,0.407085,-12.2193), (-2.21868,0.407085,-12.2193), (-2.21868,0.407085,-9.90302))
        self.AddShadowTriangle((-2.21868,0.407085,-9.90302), (-8.0217,0.407085,-9.90302), (-8.0217,0.407085,-12.2193))
        self.AddShadowTriangle((2.42165,0.407085,-12.2193), (8.22466,0.407085,-12.2193), (8.22466,0.407085,-9.90302))
        self.AddShadowTriangle((8.22466,0.407085,-9.90302), (2.42165,0.407085,-9.90302), (2.42165,0.407085,-12.2193))
        self.AddShadowTriangle((-8.24321,0.407085,9.84521), (-2.4402,0.407085,9.84521), (-2.4402,0.407085,12.1615))
        self.AddShadowTriangle((-2.4402,0.407085,12.1615), (-8.24321,0.407085,12.1615), (-8.24321,0.407085,9.84521))
        self.AddShadowTriangle((2.20013,0.407085,9.84521), (8.00315,0.407085,9.84521), (8.00315,0.407085,12.1615))
        self.AddShadowTriangle((8.00315,0.407085,12.1615), (2.20013,0.407085,12.1615), (2.20013,0.407085,9.84521))
        
    def Activate(self, on):
        if on: self.players = plus.getPlayers()
        
        Arenas.SuperArena.Activate(self, on)
        
    def HazardsOn(self, on):
        if on:
            self.ambience = plus.createSound("Sounds/magnet.wav", False, (0, 0, 0))
            plus.loopSound(self.ambience)
            #self.firesound = plus.createSound("Sounds/magnetloud.wav", False, (0, 0, 0))############(added zonesound:part-1)

            self.flame1a = HazardsPhazer.Flame((-1, -.19, 9), (0, 25, 0), (.2, .4, .2), .8)
            self.AddHazard(self.flame1a)
            self.flame1b = HazardsPhazer.Flame((-2, -.19, 9), (0, 25, 0), (.2, .4, .2), .8)
            self.AddHazard(self.flame1b)
            self.flame2a = HazardsPhazer.Flame((1, -.19, 9), (0, 25, 0), (.2, .4, .2), .8)
            self.AddHazard(self.flame2a)
            self.flame2b = HazardsPhazer.Flame((2, -.19, 9), (0, 25, 0), (.2, .4, .2), .8)
            self.AddHazard(self.flame2b)
            self.flame3a = HazardsPhazer.Flame((-1, -.19, -9), (0, 25, 0), (.2, .4, .2), .8)
            self.AddHazard(self.flame3a)
            self.flame3b = HazardsPhazer.Flame((-2, -.19, -9), (0, 25, 0), (.2, .4, .2), .8)
            self.AddHazard(self.flame3b)
            self.flame4a = HazardsPhazer.Flame((1, -.19, -9), (0, 25, 0), (.2, .4, .2), .8)
            self.AddHazard(self.flame4a)
            self.flame4b = HazardsPhazer.Flame((2, -.19, -9), (0, 25, 0), (.2, .4, .2), .8)
            self.AddHazard(self.flame4b)

        
            self.RegisterZone("flamezone01",1)
            self.RegisterZone("flamezone02",2)
            self.RegisterZone("flamezone03",3)
            self.RegisterZone("flamezone04",4)
            #self.RegisterZone("magzone01",5)############(added zonesound:part-2)("magzone01"+"02"are geoobjects in the GMF)
            #self.RegisterZone("magzone02",6)############(added zonesound:part-2)
            
            self.claw1 = self.GetHinge("Hinge01")
            self.claw1.SetAutoLocks(False, True)
            self.claw1.SetPowerSettings(-20, 3000)
            self.claw1.Lock(False)
            self.claw1.SetDirection(100)
            
            self.claw2 = self.GetHinge("Hinge02")
            self.claw2.SetAutoLocks(False, True)
            self.claw2.SetPowerSettings(-20, 3000)
            self.claw2.Lock(False)
            self.claw2.SetDirection(100)
            
            self.AddCollisionLine((-8.2, 12.4), (-8.2, 9.9))
            self.AddCollisionLine((-8.2, 9.9), (-2.5, 9.9))
            self.AddCollisionLine((-2.5, 9.9), (-2.5, 12.4))
            
            self.AddCollisionLine((2.3, 12.4), (2.3, 9.9))
            self.AddCollisionLine((2.3, 9.9), (8, 9.9))
            self.AddCollisionLine((8, 9.9), (8, 12.4))
            
            self.AddCollisionLine((-7.9, -12.4), (-7.9, -9.9))
            self.AddCollisionLine((-7.9, -9.9), (-2.3, -9.9))
            self.AddCollisionLine((-2.3, -9.9), (-2.3, -12.4))
            
            self.AddCollisionLine((2.5, -12.4), (2.5, -9.9))
            self.AddCollisionLine((2.5, -9.9), (8.2, -9.9))
            self.AddCollisionLine((8.2, -9.9), (8.2, -12.4))
            
            self.AddCollisionLine((-12.4, 1.8), (-8.5, 1.8))
            self.AddCollisionLine((-8.5, 1.8), (-8.5, -1.8))
            self.AddCollisionLine((-8.5, -1.8), (-12.4, -1.8))
            
            self.AddCollisionLine((8.5, 1.8), (12.4, 1.8))
            self.AddCollisionLine((8.5, 1.8), (8.5, -1.8))
            self.AddCollisionLine((8.5, -1.8), (12.4, -1.8))            
        else:
            self.AddCollisionLine((-12.4, 1.8), (-10.3, 1.8))
            self.AddCollisionLine((-10.3, 1.8), (-10.3, -1.8))
            self.AddCollisionLine((-10.3, -1.8), (-12.4, -1.8))
            
            self.AddCollisionLine((10.3, 1.8), (12.4, 1.8))
            self.AddCollisionLine((10.3, 1.8), (10.3, -1.8))
            self.AddCollisionLine((10.3, -1.8), (12.4, -1.8))

        # walls
        self.AddCollisionLine((-11.4, 12.4), (-12.4, 11.4))
        self.AddCollisionLine((-12.4, 11.4), (-12.4, -11.4))
        self.AddCollisionLine((-12.4, -11.4), (-11.4, -12.4))
        self.AddCollisionLine((-11.4, -12.4), (11.4, -12.4))
        self.AddCollisionLine((11.4, -12.4), (12.4, -11.4))
        self.AddCollisionLine((12.4, -11.4), (12.4, 11.4))
        self.AddCollisionLine((12.4, 11.4), (11.4, 12.4))
        self.AddCollisionLine((11.4, 12.4), (-11.4, 12.4))
        
        self.AddPOV(0, (0, 0), (1, 2, 3, 4))
        self.AddPOV(1, (10.5, -7), (0,))
        self.AddPOV(2, (-10, -7), (0,))
        self.AddPOV(3, (10.5, 6.5), (0,))
        self.AddPOV(4, (-10.5, 6.5), (0,))
        
        return Arenas.SuperArena.HazardsOn(self, on)


            
    def Introduction(self):
        sounds = self.intro_sounds
        
        # set initial camera & fade from black
        plus.setCameraPosition(-11.6853,5.11775,-24.8297)
        plus.setCameraRotation(0.190675,0.481754)
        plus.setCameraFOV(0.675)
        plus.fadeFromBlack(.25)
        
        #start playing music loop
        self.intro_music = plus.createSound("Sounds/intro_music/aggression.wav", False, (0,0,0))
        plus.setVolume(self.intro_music, 0, 0)
        plus.loopSound(self.intro_music)
        yield .25
        
        #load all sounds now to decrease lag later
        sounds['crowd'] = plus.createSound("Sounds/crowd/LoudCheer_Loop.wav", False, (0,0,0))        

        arenaOpt = ("Sounds/announcers/Arena_Magnetron_Enter.wav", "Sounds/announcers/Arena_Magnetron_Welcome.wav")
        sounds['arena'] = plus.createSound(random.choice(arenaOpt), False, (0,0,0))

        genericOpt = ("Magnetron_Flames_Leave_Marks.wav", "Arena_NowhereToHide.wav", "Intro_TheFansAreReady.wav", "Intro_GreatMatchComingYourWay.wav", "Intro_FansAreRestless.wav", "Intro_Magnetron_ReachOutAndGrab.wav", "Intro_HoldOnToYourSeats.wav")
        sounds['generic'] = plus.createSound("Sounds/announcers/"+random.choice(genericOpt), False, (0,0,0))

        botOpt = ("Bots_YouCanFeelTension.wav", "Bots_ColdChill.wav", "Bots_FansLoveTheseBots.wav", "Bots_SeeingInterestingDesigns.wav", "Bots_CrowdPoisedBotsArmed.wav")
        sounds['bots'] = plus.createSound("Sounds/announcers/"+random.choice(botOpt), False, (0,0,0))

        hazardOpt = ("Sounds/announcers/Hazard_Magnetron_Huge_Set_Flames.wav", "Sounds/announcers/Hazard_Clawtop_PushingIntoFlames.wav", "Sounds/announcers/Hazard_Fire_GooseCooked.wav", "Sounds/announcers/Hazard_Fire_WatchFlames.wav")
        sounds['hazards'] = plus.createSound(random.choice(hazardOpt), False, (0,0,0))

        #intro cam, welcom comment
        plus.playSound(sounds['arena'])
        plus.fadeInToLoop(sounds['crowd'], -100, 800)
        plus.animateCamera((-12, 5, -25), (0.29, 0.48), 0.675,      (25, 14, 3), (0.53, -1.75), 0.825, 0, 12)
        yield 2
        plus.fadeOutLoop(sounds['crowd'], 8000)
        yield 2
        
        #play a generic (or specific) secondary comment
        plus.playSound(sounds['generic'])
        yield 5
        
        #hazard cams
        if self.bHazardsOn:
            plus.playSound(sounds['hazards'])
            yield 3

            plus.animateCamera((-9, 6, -9), (0.5, .875), 0.8,         (-9, 6, 9), (0.5, 2.35), 0.8, 0, 3)
            yield 3
            plus.animateCamera((-9, 6, 9), (0.5, 2.35), 0.8,          (9, 6, 9), (0.5, 3.92), 0.8, 0, 3)
            yield 3
            plus.playSound(sounds['bots'])
            plus.animateCamera((9, 6, 9), (0.5, 3.92), 0.8,            (9, 6, -9), (0.5, 5.49), 0.8, 0, 3)
            yield 3
            plus.animateCamera((9, 6, -9), (0.5, 5.49), 0.8,           (-9, 6, -9), (0.5, 7.06), 0.8, 0, 3)
            yield 3

        players = plus.getPlayers()
        pcount = len(players)
        delaytime = 6 - pcount
        
        
        #fade out music
        plus.fadeOutLoop(self.intro_music, 2000)
        yield 2
        
        # done
        yield 0



    def Tick(self):
        # check to see if anyone has been "eliminated" by falling off the tabletop
        for each in self.players:
            if plus.getLocation(each)[1] < -4:
                plus.eliminatePlayer(each)

        if self.bHazardsOn:
            self.flame1a.Tick()
            self.flame1b.Tick()
            self.flame2a.Tick()
            self.flame2b.Tick()
            self.flame3a.Tick()
            self.flame3b.Tick()
            self.flame4a.Tick()
            self.flame4b.Tick()
        
        return Arenas.SuperArena.Tick(self)

    def ZoneEvent(self, direction, id, robot, chassis):
        if id ==1:
            self.flame1a.ZoneEvent(direction)
            self.flame1b.ZoneEvent(direction)
        elif id == 2:
            self.flame2a.ZoneEvent(direction)
            self.flame2b.ZoneEvent(direction)
        elif id == 3:
            self.flame3a.ZoneEvent(direction)
            self.flame3b.ZoneEvent(direction)
        elif id == 4:
            self.flame4a.ZoneEvent(direction)
            self.flame4b.ZoneEvent(direction)
        #elif id == 5:
            #plus.playSound(self.firesound)############(added zonesound:part-3)
        #elif id == 6:
            #plus.playSound(self.firesound)############(added zonesound:part-3)


        return True

    def DistanceToEdge(self, location):
        min_dist = 999
        min_heading = 0
        
        dist = location[0] - -10.5
        if dist < min_dist:
            min_dist = dist
            min_heading = -math.pi / 2
        dist = 10.5 - location[0]
        if dist < min_dist:
            min_dist = dist
            min_heading = math.pi / 2
        dist = location[2] - -10.5
        if dist < min_dist:
            min_dist = dist
            min_heading = math.pi
        dist = 10.5 - location[2]
        if dist < min_dist:
            min_dist = dist
            min_heading = 0
        
        return (min_dist, min_dist <= 0, min_heading)

    def HeadingAwayFromEdge(self, location):
        dist, over, h = self.DistanceToEdge(location)
        
        return h + math.pi
        
Arenas.register(ClawTophpr)
