# ascii.py - ASCII Layout Manager
#---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
# 97-03-06 ct7 Original version.
#---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

# from pieces import *
from layout import Layout

#---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

HexWidth     = 8
HexHeight    = 4
HexWidthLap  = 3
HexHeightLap = 1

#---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

class ASCII( Layout ):

	#---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

	def __init__( self ):

		Layout.__init__( self )

		self.image = []

	#---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

	def draw( self ):

		self.init_image()
		self.draw_all_water()
		self.draw_all_ports()
		self.draw_all_lands()
		self.draw_all_spots()
		self.draw_all_roads()
		self.draw_image()

	#---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

	def hex_row_col( self, x, y ):

		tx,ty = self.T_Hex

		dx,dy = x,y
		dr,dc = 0,0

		while (dx != dy):
			if dx > dy:
				dx,dy = dx-2,dy+1
				dr,dc = dr+HexHeight/2, dc-HexWidth
			else:
				dx,dy = dx+1,dy-2
				dr,dc = dr+HexHeight/2, dc+HexWidth

		r = dr+(dx-tx)*HexHeight+(HexHeight/2)
		c = dc+(ty-1)*HexWidth+(HexWidth/2)+1

		return r,c

	#---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

	def spot_row_col( self, x, y ):

		tx,ty = self.T_Hex
		dx,dy = x,y
		dr,dc = 0,0

		s = dx + dy
		while (s > 3*tx-1):
			dx,dy = dx-1, dy-1
			dr,dc = dr + HexHeight, dc
			s = dx + dy

		while (s > 2*tx-1):
			if dx < dy:
				dx,dy = dx+1, dy-2
				dr,dc = dr+HexHeight/2, dc+HexWidth
			else:
				dx,dy = dx-2, dy+1
				dr,dc = dr+HexHeight/2, dc-HexWidth
			s = dx + dy

		r = dr + (s-(2*tx-1)) * (HexHeight/2)
		if dx > dy:
			c = dc + (ty-1)*HexWidth + 2
		else:
			c = dc + tx*HexWidth

		return r,c

	#---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

	def set_image( self, row, start, end, value ):

	    try:
		line = self.image[row]
		first = line[:start]
		if start == 0: first = ""
		self.image[row] = first+value+line[end+1:]
	    except:
		print "Failed to replace image[%d][%d:%d] with '%s'" % \
			( row, start, end, value )

	#---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

	def init_image( self ):

		self.image = []

		cols = HexWidth * self.SizeX + HexWidthLap
		rows = HexHeight * self.SizeY + HexHeightLap

		if self.SizeY % 2 == 0: rows = rows + HexHeight/2

		blank = " " * cols
		for y in range( rows ):
			self.image.append( blank )

	#---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

	def draw_image( self ): 

		for line in self.image:
			print line

	#---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

	def draw_hex( self, hex ):

		hr,hc = self.hex_row_col( hex.x, hex.y )

		hcs = hc - (HexWidth/2) - 1
		hce = hc + (HexWidth/2) + 1

		self.set_image(hr-2,hcs,hce,"  *-----*  ")
		self.set_image(hr-1,hcs,hce," /       \ ")
		self.set_image(hr  ,hcs,hce,"*         *")
		self.set_image(hr+1,hcs,hce," \       / ")
		self.set_image(hr+2,hcs,hce,"  *-----*  ")

		if hex.roll:
			x = "%-5s" % ("%s(%2d)" % (hex.land, hex.roll ))
		elif hex.land:
			x = "%-5s" % hex.land
		else:
			x = "     "

		self.set_image(hr,hc-2,hc+2,x)

	#---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

	def draw_spot( self, spot ):

		sr,sc = self.spot_row_col( spot.x, spot.y )

		if spot.settler and spot.port and spot.city:
			x = "<%d>" % spot.settler
		elif spot.settler and spot.port:
			x = "{%d}" % spot.settler
		elif spot.settler and spot.city:
			x = "[%d]" % spot.settler
		elif spot.settler:
			x = "(%d)" % spot.settler
		elif spot.port:
			x = "{ }"
		else:
			x = "( )"

		self.set_image(sr,sc-1,sc+1,x)

	#---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

	def draw_road( self, road ):

		fr,fc = self.spot_row_col( road.fm.x, road.fm.y )
		tr,tc = self.spot_row_col( road.to.x, road.to.y )
		rr,rc = (fr+tr)/2, (fc+tc)/2

		if road.fm.x == road.to.x:
			angle = -1
		elif road.fm.y == road.to.y:
			angle = 1
		else:
			angle = 0

		if road.settler:
			c = '%01d' % road.settler
		else:
			c = ' '

		if angle < 0:
			x = "\\"+c+"\\"
		elif angle == 0:
			x = "="+c+"="
		else:
			x = "/"+c+"/"

		self.set_image(rr,rc-1,rc+1,x)

#---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

