remstart
` *********************************************************************
` *********************************************************************
	This include file contains routines that are common to all
	stages of the tutorial that involve character movement.
` *********************************************************************
` *********************************************************************
remend


` Character Constants
#constant cCHARACTER 50
#constant cCHARSPEED 20


` Angle of ascent does not need checking every cycle.  In fact, it would
` cause some very jerky movements in certain circumstances.
` So we will calculate it every 5 cycles.  For this we need a counter.
global cycle as integer


function makeCharacter()
remstart
	This function loads our character, orientates him,
	positions and sets up the animation sequence
	Secondly, the character is added to the AI system, 
	and a few basic parametrs set.
remend

	load object "media\H-GI-Move.x", cCHARACTER
	yrotate object cCHARACTER, 180
	scale object cCHARACTER, 500,500,500
	set object speed cCHARACTER, 50
	loop object cCHARACTER,5,25
	fix object pivot cCHARACTER
	position object cCHARACTER, gWaypoints(0).x,get terrain ground height(1,gWaypoints(0).x,gWaypoints(0).z) ,gWaypoints(0).z
	
	` Add the character to the AI system
	AI add neutral cCHARACTER,cCHARACTER,0
	AI set entity speed cCHARACTER, cCHARSPEED
	AI set entity idle position cCHARACTER, gWaypoints(1).x, gWaypoints(1).z
	AI set entity patrol time cCHARACTER, -1

endfunction

function posChar(obj)
remstart
	This function moves characters to the correct ground height
	In addition, it also adjusts their speed to account for gradients
remend	
	
	` recalculate speed based on ascent every 10 cycles
	if cycle = 1
		yPos# = object position y(obj)
		xNew# = newXvalue(object position x(obj), object angle y(obj), 10)
		zNew# = newZvalue(object position z(obj), object angle y(obj), 10)
		yNew# = get terrain ground height(1,xNew#, zNew#)
		if yNew# > yPos#
			` This weird and wonderful calculation slows our character
			` down based on the angle of ascent
			angle# = (atanfull((yNew# - yPos#),10))
			speed# = cCHARSPEED * (angle#/25)
			if speed# > cCHARSPEED * 0.75 then speed# = cCHARSPEED * 0.75
			AI set entity speed obj, cCHARSPEED  - speed#
		else
			AI set entity speed obj, cCHARSPEED
		endif
	endif
	
	` Set the Y position of the character
	position object obj, object position x(obj), get terrain ground height(1, object position x(obj), object position z(obj)),object position z(obj)

	` Increment the cycle
	inc cycle, 1
	if cycle > 5 then cycle = 1
	
endfunction
