def update(self):
		log("Updating player.")
		if not self.fighting:
			if self.hunger > 8:
				self.hp += int(self.maxHP*0.2)
			elif self.hunger > 5:
				self.hp += int(self.maxHP*0.1)
		elif self.fighting > 1:
			self.fighting -= 1
		else:
			self.fighting = False
		self.hTimer += 1
		if self.hTimer >= self.hungerTimerCountDown:
			self.hunger -= 1
			if self.hunger <= 4:
				toast("Your stomach growls.")
			if self.hunger<0:
				self.hunger = 0
			self.hTimer -= self.hungerTimerCountDown
		if self.hunger < 1:
			self.hp -= int(round((self.maxHP*0.05)))
			if self.hp <= 0:
				# Death function here. Not yet made.
				toast("You die from starvation.")
				toast("I know you are still alive.")
				toast("Shut up about it already.")
		if self.hp > self.maxHP:
			self.hp = self.maxHP
	def equip(self,eObj):
		if eObj in self.inv and eObj != self.equipped[eObj.iType]:
			self.equipped[eObj.iType].equipped = False
			self.equipped[eObj.iType]=eObj
			eObj.equipped=True
			log("Equipped " + eObj.name)
			toast("You equip %s." %(eObj.name))
	def getAttack(self):
		damage = self.attack
		damage += self.equipped["weapon"].damage
		for effect in self.equipped["weapon"].effects:
			if effect.effect == "DamageInstant":
				damage += effect.effectStrength
		log(str(damage))
		self.fighting = 5
		return int(damage)
	def __init__(self, width = 5, height = 5,cityNames=["cityNameHere"]):
		self.seed   = random.random()
		self.width  = width
		self.height = height
		self.wMap   = []
		self.indicator = (0,0)

		for x in range(0,width):
			column = []
			for y in range(0,height):
				column.append(area(23,5," ",cityNames))
			self.wMap.append(column)
		log("Initiated the world.")
def drawItemInfo(window, item):
	window.clear()
	log("Printing description...")
	length = len(" Description")
	window.addstr(1,1,"   Description" + " " * (68 - length), curses.A_STANDOUT)
	lines = textwrap.wrap(item.desc, 60)
	n = 0
	for line in lines:
		n+=1
		window.addstr(1+n,5,line)
	length = len(lines)

	log("Printing stats...")
	window.addstr(3+length,1,"   Stats" + " "*(68 - len(" Stats")), curses.A_STANDOUT)
	window.addstr(4+length,5,"Rarity: %s" % (item.rarity))
	length+=1

	if item.iType == "weapon":
		window.addstr(4+length,5,"Weapon Type: " + str(item.weaponType))
		length+=1
		window.addstr(4+length,5,"Damage: " + str(item.damage))
		if len(item.effects)>0:
			length+=1
			window.addstr(4+length,5,"Damage with effects: "+str(item.getMaxAttack()))
	elif item.iType == "head" or item.iType == "body" or item.iType == "legs" or item.iType == "feet":
		window.addstr(4+length,5,"Defence: " + str(item.defence))
	else:
		length -= 1
	window.addstr(5+length,5,"Worth: $" + str(item.getWorth()))
	if len(item.effects) == 0:
		window.addstr(6+length,5,"It has no special effects.")
	else:
		window.addstr(6+length,5,"Effects:")
		n=0
		for effect in item.effects:
			window.addstr(7+length+n,7,item.effects[n].name + " - "+str(item.effects[n].effectStrength) + " strength")
			n+=1

	window.box()
	window.addstr(0,3,"[ "+ item.name +" ]",)
	window.refresh()
def objectEnemyWindow(window,objects,enemies,CURSOR,player):
	log("Drawing room objects window.")
	# Don't even try to read this code. I wrote it and it is hard to follow.
	# I mean, I can edit it and modify SOME things, but I refuse to, at the moment, do a complete rewrite of
	# the code.
	numEnemies = len(enemies)
	numObjects = len(objects)
	window.clear()
	length=0
	window.addstr(1,1,"Enemies" + (" " * (46 - len("Enemies"))), curses.A_STANDOUT)
	if len(enemies) < 1:
		length+=1
		window.addstr(2,1,"   None")
	for enemy in enemies:
		length+=1
		window.addstr(1+length,1,"   " + enemy.icon + " - " + enemy.name + " LVL " + str(enemy.lvl))
		window.addstr(1+length,44-len(str(getDistanceOf(player.x,player.y,enemy.x,enemy.y,0))),"(" + str(getDistanceOf(player.x,player.y,enemy.x,enemy.y,0)) + ") ")
	window.addstr(2+length,1,"Items" + (" " * (46 - len("Items"))), curses.A_STANDOUT)
	if len(objects) < 1:
		window.addstr(3+length,1,"   None")
	for item in objects:
		length+=1
		window.addstr(2+length,1,"   " + item.icon + " - " + item.name)
		window.addstr(2+length,44-len(str(getDistanceOf(player.x,player.y,item.x,item.y,0))),"(" + str(getDistanceOf(player.x,player.y,item.x,item.y,0)) + ")")
	if CURSOR > len(enemies)-1:
		if len(enemies)<1 and len(objects)>0:
			window.addstr(4+CURSOR,1,"->")
		elif len(enemies)<1 and len(objects)<1:
			window.addstr(2,1,"->")
		else:
			window.addstr(3+CURSOR,1,"->")
	else:
		window.addstr(2+CURSOR,1,"->")
	window.box()
	window.addstr(0,3,"[ Objects in Room ]")
	window.refresh()
	def generateNewWeapon(self,race=-1,playerLevel=-1,rarity=-1, inv=[]):
		log("Generating new weapon...")
		# Create a new item with some pre-setup stats.
		newItem = item(0,0,"w","Weapon Generation Test","This is being used as a weapon generation test for the game.","weapon")

		# Item generation code:
		# Choose a minimum playerlevel if one not decided:
		log("Setting item level...")
		if playerLevel == -1:
			playerLevel = random.randint(1,20) # with 20 being max so that it does not generate something with level 100 or something
		else:
			playerLevel = playerLevel + random.randint(0,2) - 1
		log("Player level: %d" % (playerLevel))

		# This chooses a rarity to use.
		# This code runs if there is a rarity already specified
		log("Setting item rarity...")
		if not rarity == -1:
			r = self.rarity[rarity]
		# This runs if there is not a rarity specified
		else:
			if playerLevel >= 25:
				num = random.randint(0+int(playerLevel/2),255)
			elif playerLevel >= 20:
				num = random.randint(0+int(playerLevel/2),245)
			elif playerLevel >= 10:
				num = random.randint(0+int(playerLevel/2),240)
			elif playerLevel >= 5:
				num = random.randint(0+int(playerLevel/2),220)
			else:
				num = random.randint(0+int(playerLevel/2),210)
			rares = []
			for rare in self.rarity:
				if self.rarity[rare][1] <= num <= self.rarity[rare][2] and playerLevel>=self.rarity[rare][0]:
					rares.append(rare)
			num = random.randint(0,len(rares)-1)
			rarity = rares[num]
			r = self.rarity[rarity]
		newItem.rarity = rarity
		log("Item worth: %s" % (rarity))

		worth = random.randint(int(playerLevel*self.rarity[rarity][3]/2),playerLevel*self.rarity[rarity][3])
		log("Item worth: %d" % (worth))
		log("Setting item effects and damage.")
		while worth >= 0:
			if worth > 30 and len(newItem.effects) < 3:
				if random.choice([True, False, False, False, False]):
					log("Generating item effect.")

					newEffect = itemEffect()
					n = random.randint(0,0)
					if n == 0:
						newEffect.effect = "DamageInstant"
					newEffect.name = random.choice(open(self.folder + self.effects["weapons"][0] + self.effects["weapons"][1+n]).read().splitlines())
					newEffect.desc = newEffect.name.split("|")[1]
					newEffect.name = newEffect.name.split("|")[0]
					newEffect.effectStrength = random.randint(1,int((worth-30)/3 + 1))
					newEffect.worth = 30 + ((newEffect.effectStrength-1) * 3)


					# Issue #10 happens here.
					newItem.effects.append(newEffect)
					worth -= newItem.effects[len(newItem.effects)-1].worth
					for i in inv:
						if i.iType == "weapon":
							log(str(i))

				else:
					d = random.randint(1,int(worth)/4+1)
					newItem.damage += d
					worth -= (3+random.randint(0,1)) * d
			else:
				newItem.damage += 1
				worth -= 3+random.randint(0,1)

		# This sets up the weapons race if there is not one specified
		if race == -1:
			race = random.choice(self.weaponRaces.keys())
		log("Item race: %s" % (race))

		# This sets the weapon type which will be used later.
		wepType = random.choice(open(self.folder + self.weaponRaces[race][1]).read().splitlines())
		newItem.weaponType = wepType
		newItem.icon = wepType[0]

		log("Setting item name and description.")
		# This generates the item based on its rarity that was decided above
		if rarity == "Very Common" or rarity == "Common" or rarity == "Uncommon":
			newItem.name = "%s" % (wepType)

		elif rarity == "Rare" or rarity == "Exotic":
			newItem.name = "%s %s" % (random.choice(open(self.folder + self.weaponRaces[race][0]).read().splitlines()),
				                      wepType)

		elif rarity == "Legendary" or rarity == "Ascendent":
			newItem.name = "%s %s %s" % (random.choice(open(self.folder + self.weaponRaces[race][0]).read().splitlines()),
				                         wepType,
				                         random.choice(open(self.folder + self.weaponRaces[race][2]).read().splitlines()))
		elif rarity == "Godly":
			newItem.name = "%s %s %s" % (random.choice(open(self.folder + self.weaponRaces[race][0]).read().splitlines()),
			                             wepType,
			                             random.choice(open(self.folder + self.weaponRaces[race][2]).read().splitlines()))

		# You can use <weptype> and <wepname> to make descriptions more dynamic
		newItem.desc = random.choice(open(self.folder+self.weaponRaces[race][3]).read().splitlines())
		for e in newItem.effects:
			newItem.desc = newItem.desc + e.desc
		newItem.desc = newItem.desc.replace("<weptype>",newItem.weaponType)
		newItem.desc = newItem.desc.replace("<wepname>",newItem.name)
		
		# Returns the new item to whatever called for it.
		log("Returning item.")
		return newItem
def drawMapMenu(windows,d,e,f,CURSOR=0, redraw=False,m=0):
	windows[3].clear()
	if redraw:
		log("Redrawing map menu.")
		d.clear()
		if CURSOR < 0:
			CURSOR = 0
		windows[6].clear()
		windows[5].clear()

	d.addstr(0,0,"| 25% ||          50%          |")
	# aName = " " + e.wMap[f.wx][f.wy].aMap[f.ax][f.ay].name + ", " + e.wMap[f.wx][f.wy].name
	# aName = aName + (" " * (31-len(aName)))
	# d.addstr(8,0,aName)
	d.refresh()
	
	e.draw(windows[0])
	windows[0].box()
	windows[0].refresh()

	e.wMap[f.wx][f.wy].draw(windows[1])
	windows[1].box()
	windows[1].refresh()

	e.wMap[f.wx][f.wy].aMap[f.ax][f.ay].draw(windows[2])
	windows[2].box()
	windows[2].addstr(0,3,"[ Main Map ]")
	windows[2].refresh()

	# This is the window for player information.
	# d.addstr(0,31,"| " + f.name + " (lvl " + str(f.lvl) + ")")
	windows[3].addstr(1,1,"Health:     " + str(f.hp) + " / " + str(f.maxHP))
	windows[3].addstr(2,1,"Hunger:     " + str(f.hunger) + " / " + str(f.maxHunger))
	windows[3].addstr(3,1,"Experience: " + str(f.xp) + "xp" )
	windows[3].addstr(4,1,"Needed xp:  " + str(f.lvlup) + "xp")

	windows[3].addstr(1,26,"Inventory: " + str(f.getInventorySize()) + " / " + str(f.getMaxInventory()))
	windows[3].addstr(2,26,"Money:     $" + str(f.money))
	windows[3].addstr(3,26,"Damage:    " + str(f.getDamage()))
	windows[3].addstr(4,26,"Defence:   " + str(f.getDefence()))
	
	windows[3].box()
	windows[3].addstr(0,3,"[ User Info ]")
	windows[3].refresh()

	# Toasts window
	if m == 1:
		log("Drawing toasts menu.")
		y=0
		for t in getToasts(6):
			y+=1
			windows[4].addstr(y,1,t)
		windows[4].box()
		windows[4].addstr(0,3,"[ Messages ]")
		windows[4].refresh()

	c = e.wMap[f.wx][f.wy].aMap[f.ax][f.ay] # This gets the current chunk

	# what the hell I need to redo this. I have no clue what it's doing.
	# I think it is the item descriptio window when looking at objects in the map menu
	if m == 1:
		log("Draw some stuff.")
		windows[6].clear()
		if len(c.enemies)-1 < CURSOR:
			if len(c.items)-1<0:
				windows[6].addstr(1,1,"Nothing is here.")
			else:
				if len(c.items)>0:
					tempItem = c.items[CURSOR-len(c.enemies)]
					name = " %s%s(%d)" % (tempItem.name," " * (43 - len(tempItem.name) - len(str(getDistanceOf(f.x,f.y,tempItem.x,tempItem.y,0)))),\
						                  getDistanceOf(f.x,f.y,tempItem.x,tempItem.y))
					windows[6].addstr(1,1,name,curses.A_STANDOUT)
					ln=0
					for line in textwrap.wrap(tempItem.desc,46):
						if ln <= 2:
							windows[6].addstr(2+ln,1,line)
							ln+=1
					if getDistanceOf(f.x,f.y,tempItem.x,tempItem.y) < 2:
						if tempItem.iType == "weapon":
							s = "Damage: " + str(tempItem.damage)
							windows[6].addstr(6,1,s + " " * (46 - len(s)))
						elif tempItem.iType == "head" or tempItem.iType == "body" or tempItem.iType == "legs" or tempItem.iType == "feet":
							s = "Defence: " + str(tempItem.defence)
							windows[6].addstr(6,1,s + " " * (46 - len(s)))
						elif tempItem.iType == "healing":
							s = "Food: " + str(tempItem.hunger)
							windows[6].addstr(6,1,s + " " * (46 - len(s)))

		elif len(c.enemies)>0:
			name = " %s%s(%d)" % (c.enemies[CURSOR].name," " * (43 - len(c.enemies[CURSOR].name) - len(str(getDistanceOf(f.x,f.y,c.enemies[CURSOR].x,\
				                                                c.enemies[CURSOR].y,0)))),getDistanceOf(f.x,f.y,c.enemies[CURSOR].x,c.enemies[CURSOR].y))
			windows[6].addstr(1,1,name,curses.A_STANDOUT)
			ln = 0
			for line in textwrap.wrap(c.enemies[CURSOR].desc, 46):
				if ln <= 3:
					windows[6].addstr(2+ln,1,line)
					ln+=1
			dis = getDistanceOf(f.x,f.y,c.enemies[CURSOR].x,c.enemies[CURSOR].y,0)
			if f.getFOV()>int(dis):
				windows[6].addstr(5,1,"Enemy HP: %d" % (c.enemies[CURSOR].hp))

		windows[6].box()
		windows[6].addstr(0,3,"[ Info Window ]")
		windows[6].refresh()

	if m == 0:
		objectEnemyWindow(windows[5],c.items,c.enemies,CURSOR,f)
def drawInventoryMenu(window, player, CURSOR, WINDOW):
	global NUMITEMS
	if CURSOR > NUMITEMS and NUMITEMS != -1:
		CURSOR = NUMITEMS

	tabs=[" Weapons "," Armor "," Consumable "," Other "]
	log("Drawing tab line...")
	if WINDOW >= len(tabs):
		WINDOW = len(tabs) - 1
	window.clear()
	invText = "   Capacity: " + str(player.getInventorySize()) + " / " + str(player.getMaxInventory())
	invText = invText + (" " * (68 - len(invText)) )
	window.addstr(1,1,invText,curses.A_STANDOUT)
	window.addstr(2,15,"|")
	length = 1
	for i in range(0,len(tabs)):
		string = tabs[i]
		if i == WINDOW:
			window.addstr(2,15+length,string,curses.A_STANDOUT)
		else:
			window.addstr(2,15+length,string)
		length+=len(string)
		window.addstr(2,15+length,"|")
		length+=len("|")
	numItems=-1
	log("Drawing items...")
	if WINDOW == 0:
		for item in player.inv:
			if item.iType == "weapon":
				numItems+=1
				if numItems == CURSOR:
					window.addstr(4+numItems,3," " + item.icon +  " - " + item.name + " "*(63-len(item.icon +  " - " + item.name)),curses.A_STANDOUT)
					if player.equipped[item.iType] == item:
						window.addstr(4+numItems, 57, "(Equipped)",curses.A_STANDOUT)
				else:
					window.addstr(4+numItems,3,item.icon +  " - " + item.name)
					if player.equipped[item.iType] == item:
						window.addstr(4+numItems, 57, "(Equipped)")
	elif WINDOW == 1:
		for item in player.inv:
			if item.iType == "head" or item.iType == "body" or item.iType == "legs" or item.iType == "feet":
				numItems+=1
				if numItems == CURSOR:
					window.addstr(4+numItems,3," " + item.icon +  " - " + item.name + " "*(63-len(item.icon +  " - " + item.name)),curses.A_STANDOUT)
					if player.equipped[item.iType] == item:
						window.addstr(4+numItems, 57, "(Equipped)",curses.A_STANDOUT)
				else:
					window.addstr(4+numItems,3,item.icon +  " - " + item.name)
					if player.equipped[item.iType] == item:
						window.addstr(4+numItems, 57, "(Equipped)")
	elif WINDOW == 2:
		for item in player.inv:
			if item.iType == "healing" or item.iType == "consumable":
				numItems+=1
				if numItems == CURSOR:
					window.addstr(4+numItems,3," " + item.icon +  " - " + item.name + " "*(63-len(item.icon +  " - " + item.name)),curses.A_STANDOUT)
				else:
					window.addstr(4+numItems,3,item.icon +  " - " + item.name)
	elif WINDOW == 3:
		for item in player.inv:
			if item.iType != "head" and item.iType != "body" and item.iType != "legs" and item.iType != "feet" and item.iType != "weapon" and item.iType != "healing":
				numItems+=1
				if numItems == CURSOR:
					window.addstr(4+numItems,3," " + item.icon +  " - " + item.name + " "*(63-len(item.icon +  " - " + item.name)),curses.A_STANDOUT)
				else:
					window.addstr(4+numItems,3,item.icon +  " - " + item.name)
	if numItems == -1:
		window.addstr(4,2,"You have nothing.")
	if numItems > -1 and CURSOR > numItems:
		CURSOR = numItems
	NUMITEMS = numItems
	
	log("Window refresh")
	window.box()
	window.addstr(0,3,"[ Inventory ]",)
	window.refresh()
	return CURSOR