from twisted.internet.reactor import callLater
from commands import *
from pyspades.world import *
from pyspades.constants import *
from pyspades.server import *
from pyspades.common import *
@admin
def texture(connection, value = 5):
	value = int(value)
	irc_relay = connection.protocol.irc_relay 
	irc_relay.send("Texturing in progress...")
	connection.protocol.texture_in_progress = True
	for a in xrange(512):
		for b in xrange(512):
			for c in xrange (63):
				if connection.protocol.map.get_solid(a, b, c):
					connection.protocol.randomize_block(a, b, c, value)
	connection.protocol.texture_in_progress = False
	irc_relay.send("Texturing process complete!")
	return
add (texture)

@admin
def ocean(connection, value = 5, R = 28, G = 107, B = 160):
	value, R, G, B = int(value), int(R), int(G), int(B)
	irc_relay = connection.protocol.irc_relay 
	irc_relay.send("Texturing in progress...")
	connection.protocol.texture_in_progress = True
	for a in xrange(512):
		for b in xrange(512):
			c = 63
			if connection.protocol.map.get_solid(a, b, c):
				connection.protocol.ocean_block(a, b, c, value, R, G, B)
	connection.protocol.texture_in_progress = False
	irc_relay.send("Texturing process complete!")
	return
add (ocean)

def apply_script(protocol, connection, config):
	class TextureConnection(connection):
		def on_block_build_attempt(self, x, y, z):
			if self.protocol.texture_in_progress:
				return false
		def on_line_build_attempt(self, points):
			if self.protocol.texture_in_progress:
				return false
		def on_block_destroy(self, x, y, z, mode):
			if self.protocol.texture_in_progress:
				return false
	
	class TextureProtocol(protocol):
		texture_in_progress = False
		
		def randomize_block(self, a, b, c, value):
			R1, G1, B1 = self.map.get_color(a, b, c)
			R2, G2, B2 = R1 + random.randint(-value, value), G1 + random.randint(-value, value), B1 + random.randint(-value, value)
			if R2 < 0:
				R2 = 0
			if G2 < 0:
				G2 = 0
			if B2 < 0:
				B2 = 0
			if R2 > 255:
				R2 = 255
			if G2 > 255:
				G2 = 255
			if B2 > 255:
				B2 = 255
			RGB = (R2, G2, B2)
			self.map.set_point(a, b, c, RGB)
			set_color.value = make_color(*RGB)
			set_color.player_id = 32
			self.send_contained(set_color)
			block_action.x = a
			block_action.y = b
			block_action.z = c
			block_action.player_id = 32
			block_action.value = DESTROY_BLOCK
			self.send_contained(block_action, save = True)
			block_action.value = BUILD_BLOCK
			self.send_contained(block_action, save = True)
			return
			
		def ocean_block(self, a, b, c, value, R, G, B):
			R1, G1, B1 = R, G, B
			R2, G2, B2 = R1 + random.randint(-value, value), G1 + random.randint(-value, value), B1 + random.randint(-value, value)
			if R2 < 0:
				R2 = 0
			if G2 < 0:
				G2 = 0
			if B2 < 0:
				B2 = 0
			if R2 > 255:
				R2 = 255
			if G2 > 255:
				G2 = 255
			if B2 > 255:
				B2 = 255
			RGB = (R2, G2, B2)
			self.map.set_point(a, b, c, RGB)
			set_color.value = make_color(*RGB)
			set_color.player_id = 32
			self.send_contained(set_color)
			block_action.x = a
			block_action.y = b
			block_action.z = c
			block_action.player_id = 32
			block_action.value = DESTROY_BLOCK
			self.send_contained(block_action, save = True)
			block_action.value = BUILD_BLOCK
			self.send_contained(block_action, save = True)
			return
			
	return TextureProtocol, TextureConnection