"""
tp3
have fun!
put the extention in this format:
"territories":[(x,y),(x,y),(x,y)]
"""
from pyspades.server import Territory

def apply_script(protocol, connection, config):
    class CPProtocol(protocol):
        """game_mode=1"""


        def get_cp_entities(self):
            terretories=[] #default
            positions=[(0,0,63)] #default

            #getting positions
            extensions=self.map_info.extensions
            if extensions.has_key("territories"):
                positions=extensions['territories']
            l=len(positions)+1

            #looping through positions
            for i in range(l-1):
                pos=positions[i]

                if len(pos)>2: #if given the x,y and z
                    x,y,z=pos

                else: #if given only x and y
                    x,y=pos
                    z=self.map.get_z(x,y)

                cp=Territory(i, self, x, y, z) #creates the territory

                #sets the team                
                if i<l/2:
                    cp.team=self.blue_team
                elif i<l/2:
                    cp.team=self.green_team
                else:
                    cp.team=None

                #add the teretory to the list
                terretories.append(cp)

            #the end!
            return terretories
    return CPProtocol,connection
                
        
