"""thepolm3converts the cps to towers to be defended and capturedSuggested by Jdrew"""from twisted.internet.task import LoopingCallfrom twisted.internet.reactor import callLaterfrom pyspades.constants import *from pyspades.server import block_action, set_color, block_linefrom commands import get_teamfrom pyspades.common import make_colordef apply_script(protocol, connection, config):    class TerritoryControlTowerConnection(connection):                def on_block_destroy(self, x, y, z, mode):            tower=self.protocol.is_tower(x,y,z)            if tower:                if tower.team==self.team:                    return False                if self.tool!=SPADE_TOOL:                    return False                tower.add_player(self)                callLater(0.1,self.capture_if_allowed,tower, x,y,z)            return connection.on_block_destroy(self, x, y, z, mode)        def capture_if_allowed(self, tower, x,y,z):            if self in tower.players:                callLater(0.1,self.protocol.convert_tower,tower,self)            else:                self.protocol.repair_tower(tower.id)    class TerritoryControlTowerProtocol(protocol):        game_mode=TC_MODE        towers=[]        capstop=None        def destroyBlock(self, x,y,z):            if 1<x<511 and 1<y<511 and 1<z<63:                block_action.value = DESTROY_BLOCK                block_action.x,block_action.y,block_action.z=x,y,z                self.map.remove_point(x,y,z)                self.send_contained(block_action)                def buildBlock(self, x,y,z,colour):            if 1<x<511 and 1<y<511 and 1<z<63:                block_action.value = BUILD_BLOCK                block_action.x,block_action.y,block_action.z=x,y,z                set_color.value = make_color(*colour)                self.map.set_point(x,y,z,colour)                self.send_contained(block_action)                self.send_contained(set_color)        def is_tower(self,x,y,z):            for i in range(len(self.towers)):                xa,ya,za=self.towers[i]                if xa+1>=x>=xa-2 and ya+1>=y>=ya-2:                    return self.entities[i]            return False        def convert_tower(self,tower,player):            i=tower.id            x,y,z=self.towers[i]            if not self.map.get_solid(x,y,z):                tower.rate=[0,1][tower.team==self.blue_team]                tower.finish()                self.repair_tower(i)                protocol.on_cp_capture(self,tower)        def remove_tower(self,ID):            xa,ya,zb=self.towers[ID]            height=zb+9            zb=height            for x in range(-1,2):                for y in range(-1,2):                    for z in range(0,height):                        self.destroyBlock(x+xa,y+ya,z)            self.buildBlock(xa,ya,62,(155,155,155))        def repair_tower(self,ID):            xa,ya,height=self.towers[ID]            if self.entities[ID].team:                colour=self.entities[ID].team.color            else:                colour=(255,255,255)            for x in range(-2,1):                for y in range(-2,1):                    for z in range(height-1,62):                        if self.map.get_solid(x+xa,y+ya,z):                            if self.map.get_point(x+xa,y+ya,z)[1]!=colour:                                self.destroyBlock(x+xa,y+ya,z)                                self.buildBlock(x+xa,y+ya,z,colour)                        else:                            self.buildBlock(x+xa,y+ya,z,colour)            for x in range(-2,1):                for y in range(-2,1):                    self.destroyBlock(x+xa,y+ya,height)                    self.buildBlock(x+xa,y+ya,height,(0,0,0))            self.destroyBlock(xa,ya,62)        def create_tower(self,ID):            xa,ya=self.entities[ID].x,self.entities[ID].y            height=self.map.get_z(xa,ya)-10            if self.entities[ID].team:                colour=self.entities[ID].team.color            else:                colour=(255,255,255)            for x in range(-1,2):                for y in range(-1,2):                    for z in range(height,64):                        self.destroyBlock(x+xa,y+ya,z)                        self.buildBlock(x+xa,y+ya,z,colour)            for x in range(-1,2):                for y in range(-1,2):                    self.destroyBlock(x+xa,y+ya,height+1)                    self.buildBlock(x+xa,y+ya,height+1,(0,0,0))            if len(self.towers)>ID:                self.towers[ID]=(x+xa,y+ya,height+1)            else:                self.towers.append((x+xa,y+ya,height+1))            self.destroyBlock(xa,ya,62) #puts the cp below the tower        def reset_tc(self):            for i in range(len(self.entities)):                self.remove_tower(i)            return protocol.reset_tc(self)                        def get_cp_entities(self):            self.entities=protocol.get_cp_entities(self)            for i in range(len(self.entities)):                self.create_tower(i)                self.entities[i].z=63                self.capstop=LoopingCall(self.stop_capture)                self.capstop.start(0.1)            return self.entities        def stop_capture(self):            #hate this workaround, buut otherwise this wouldn't be compatible with other scripts            for cp in self.entities:                                cp.progress = 0.0                cp.update()    return TerritoryControlTowerProtocol, TerritoryControlTowerConnection