from mapgen import *
from numpy import random as rn
import numpy as np

def encounter_map(w,h):
	mx = np.zeros((h,w),dtype=np.uint8)
	mx[rn.randint(2,h-2),0] = 1
	jumplist = []
	jumped = 0 
	for col in xrange(w-1):
		for row in xrange(2,h-2):
			try:
				if mx[row,col]:
					rnd = rn.rand()
					mx[row,col+1] = 1 if rnd < 0.8 else 0
					if rn.rand() < 0.05:
						if jumped : 
							jumped = 0
							continue
						nr = rn.randint(-6,7)
						if (h-2) <= (nr + row) <= 2 :
							nr *= -1
						newrow = nr + row
						low,high = (row,newrow) if row < newrow else (newrow,row)
						jumplist.append((col,low,high))
						jumped = 1
						mx[newrow,col+1]= 1
				else:
					if mx[row,col-1]:
						mx[row,col+1] = 1 if rnd < 0.4 else 0
						rnd = rn.rand()
						r = 2 if rnd <= 0.5 else -2
						nr = row + r
						if nr < 2:
							nr += 6
						elif nr > h-2:
							nr -= 6
						low,high = (row,nr) if row < nr else (nr,row)
						jumplist.append((col,low,high))
						mx[nr,col] = 1
			except IndexError, e:
				print e
	strel = np.ones((2,3),dtype=np.uint8)
	for col,low,high in jumplist:
		mx[low:high,col] = 1
	out = dilate(mx,strel)
	strel = np.ones((2,2),dtype=np.uint8)
	out = dilate(flip(out),strel)
	return out


map = encounter_map(120,40)
pp(map)
