#################################################################
#   )      (\_     # WOLFPACK 13.0.0 Scripts                    #
#  ((    _/{  "-;  # Created by: DarkStorm                      #
#   )).-' {{ ;'`   # Revised by:                                #
#  ( (  ;._ \\ ctr # Last Modification: Created                 #
#################################################################

# Script for bladed weapons

import wolfpack
import wolfpack.time
from wolfpack.consts import *
from wolfpack.utilities import hex2dec
import wolfpack.utilities
import whrandom
import weapons.blades

# List of Blood IDs
blood = [ "122a", "122b", "122d", "122f" ]
trees = []

def onLoad():
	# Load the list of valid trees just once
	weapons.blades.trees = wolfpack.list( "IDS_TREE" )

def onUse( char, item ):
	char.socket.clilocmessage( 1, 10, 18 ) # What do you want to use this on?
	char.socket.attachtarget( "weapons.blades.response", [ item.serial ] )
	return 1

def response( char, args, target ):
	item = wolfpack.finditem( args[0] )

	if not item or not item.container == char:
		char.message( "You have to have the item in your hand to use it!" )
		return

	# Corpse => Carve
	# Wood => Kindling/Logs
	model = 0

	if target.item:
		if target.item.id == 0x2006 and target.item.corpse:
			carve_corpse( char, target.item )
			return
		else:
			model = target.item.id
			
	elif target.char:
		if ( target.char.id == 0xcf ):
		    target.char.id = 0xdf
		    target.char.carve = "carve_sheep_sheered"
		    target.char.update()
		elif target.char.id == 0xdf:
			char.socket.sysmessage( "This sheep has no whool at the moment." )
			return
		else:
		    char.socket.clilocmessage( 0, 500, 494 )
		    return
			 
		wool = wolfpack.additem( "df8" )
		wool.amount = 2
		if not wolfpack.utilities.tocontainer( wool, char.getbackpack() ):
			wool.update()
		
		char.socket.sysmessage( "You cut the whool from the sheep." )
		return

	else:
		model = target.model

	# Check for Trees
	if not is_tree( model ):
		char.socket.clilocmessage( 0, 500, 494 ) # You can't use a bladed item on that
	else:
		if not check_wood( target.pos ):
			char.message( "There is no wood here to cut" )

		# This depends on the Weapon Type, 1001 (=Swords) and 1002 (=Axes) can hack logs
		if item.type == 1001 or item.type == 1002:
			hack_logs( char, target.pos )
		else:
			hack_kindling( char, target.pos )

# Check for Wood at a specific position
def check_wood( pos ):
	statics = wolfpack.statics( pos.x, pos.y, pos.map )

	for item in statics:
		id = item[ "id" ]

		if is_tree( id ):
			return 1

	# Check normal items
	dynamics = wolfpack.items( pos.x, pos.y, pos.map )

	for item in dynamics:
		if is_tree( item.id ):
			return 1

	return 0

# HACK LOGS
def hack_logs( char, pos ):
	# Try to find a resource item
	resource = None

	dynamics = wolfpack.items( pos.x, pos.y, pos.map )

	for item in dynamics:
		if item.id == 0x1ea7 and item.hastag( 'resourcecount' ) and item.hastag( 'resource' ) and item.gettag( 'resource' ) == 'logs':

			# Check if the resource item didn't expire already (could be a bug!)
			if item.decaytime <= wolfpack.time.servertime():
				item.delete()
			else:
				resource = item
				break

	# Default Range for each tree is 10 to 15 Logs
	if not resource:		
		amount = whrandom.randint( LUMBERJACKING_MIN_LOGS, LUMBERJACKING_MAX_LOGS )
	else:
		amount = int( resource.gettag( 'resourcecount' ) )

	# No resource left to harvest?
	if amount < 1:
		char.socket.clilocmessage( 0, 500, 493, "", 0x3b2, 3, char ) # There is no wood left to chop
		return

	# Turn to our lumberjacking position
	direction = char.directionto( pos )
	if char.direction != direction:
		char.direction = direction
		char.updateflags()

	# Let him hack
	char.action( 0x9 )
	char.soundeffect( 0x13e )

	# SkillCheck against LUMBERJACKING
	if not char.checkskill( LUMBERJACKING, LUMBERJACKING_MIN_SKILL, LUMBERJACKING_MAX_SKILL ):
		char.socket.clilocmessage( 0, 500, 495, "", 0x3b2, 3, char  ) # You hack at the tree for a while but fail to produce...
		return
	
	char.socket.clilocmessage( 0, 500, 498, "", 0x3b2, 3, char  ) # You put some logs into your backpack

	# Create an item in my pack (logs to be specific)
	item = wolfpack.additem( "1be0" )
	if not wolfpack.utilities.tobackpack( item, char ):
		item.update()

	# Create a resource item if neccesary
	if not resource:
		resource = wolfpack.additem( "1ea7" )
		resource.name = 'Resource Item: logs'
		resource.settag( 'resourcecount', amount - 1 )
		resource.settag( 'resource', 'logs' )
		resource.visible = 0 # GM Visible only
		resource.moveto( pos )
		resource.decay = 1
		resource.decaytime = wolfpack.time.servertime() + ( LUMBERJACKING_REFILLTIME * 1000 )
		resource.update() # Send to GMs
	else:
		resource.settag( 'resourcecount', amount - 1 )

# CARVE CORPSE
def carve_corpse( char, corpse ):
	if corpse.container:
		char.socket.sysmessage( "You can't carve corpses in a container" )
		return

	if char.distanceto( corpse ) > 3:
		char.socket.clilocmessage( 0, 500, 312, "", 0x3b2, 3, corpse ) # You cannot reach that
		return

	# Human Bodies can always be carved
	if corpse.bodyid == 0x190 or corpse.bodyid == 0x191:
		char.message( "You can't carve a human body right now" )
		return

	# Not carvable or already carved
	if corpse.carve == '':
		char.socket.clilocmessage( 0, 500, 485, "", 0x3b2, 3, corpse ) # You see nothing useful to carve..
		return
	
	# Create all items in the carve list
	carve = wolfpack.list( corpse.carve )

	for id in carve:
		amount = 1
		# Is amount contained in it ?
		if id.find( "," ) != -1:
			parts = id.split( "," )
			id = parts[0]
			amount = int( parts[1] )

		item = wolfpack.additem( id )
		item.amount = amount
		if not wolfpack.utilities.tocontainer( item, corpse ):
			item.update()

	# Create Random Blood
	bloodid = whrandom.choice( blood )
	blooditem = wolfpack.additem( bloodid )
	blooditem.moveto( corpse.pos )
	blooditem.decay = 1
	blooditem.decaytime = wolfpack.time.servertime() + ( 30 * 1000 ) # Decay after 30 Seconds
	blooditem.update()

	char.socket.clilocmessage( 0, 500, 467, "", 0x3b2, 3, corpse ) # You carve away some meat which remains on the corpse
	corpse.carve = ''

# HACK KINDLINGS
def hack_kindling( char, pos ):
	if pos.distance( char.pos ) > 3:
		char.socket.clilocmessage( 0, 500, 312 ) # You cannot reach that
		return 1

	direction = char.directionto( pos )
	if char.direction != direction:
		char.direction = direction
		char.updateflags()

	# N O T E: For anti-cheat prevention we have to check
	# if the statics REALLY contain the tree

	# Let's add some Kindling
	item = wolfpack.additem( "de1" )
	if not wolfpack.utilities.tobackpack( item, char ):
		item.update()

	# Let him hack
	char.action( 0x9 )
	char.soundeffect( 0x13e )
	char.socket.clilocmessage( 0, 500, 491 ) # You put some kindlings in your pack

# Is "model" a Tree art-tile?
def is_tree( model ):
	# List consists of hexadecimal integers
	for tree in trees:
		tree = hex2dec( tree )
		if tree == model:
			return 1

	return 0
