remstart
Dark AI Tutorial
By Steve Vink for The Game Creators Ltd
remend

` The debug switch allows us to show waypoints manually.
` The automatic debug visibility is not appropriate,
` because of the different landscape heights
#constant cDEBUG 1

` To store the positions of objects and entities efficiently,
` we will use a user defined type to hold the 3 coordinates.
Type tPos
	x as float
	y as float
	z as float
endtype


` Waypoint positions will be stored in an array for future reference
dim gWaypoints() as tPos

	` Basic Setup
	SYNC ON: SYNC RATE 60
	autocam off
	AI start
	
	` *********************************************************************
	` Make the scene using advanced terrain and some scenery
	makeTerrain()
	
	
	` *********************************************************************
	` Add waypoints to the map, to mark out the relevant paths
	createWaypoints()
	
	
	` *********************************************************************
	` Add random waypoints across the map as intermediate points
	for x = 50 to 1500 step 100
		for z = 50 to 1500 step 100
			x# = x + (rnd(20) - 10) : z# = z + (rnd(20) - 10)
			AI add waypoint 0, x#, z#
			if cDEBUG
				obj = freeObj(100)
				make object cone obj, 5
				color object obj, rgb(128,255,255)
				position object obj, x#,get terrain ground height(1,x#,z#),z#
			endif
		next z
	next x
	
	AI update waypoint visibility 0, 150

	` Now use the memblock functionality to make the hilltop
	` waypoints less desirable
	updateWaypoints()


	` *********************************************************************
	` Make a character
	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
	AI add neutral cCHARACTER,cCHARACTER,0
	AI set entity speed cCHARACTER, cCHARSPEED
	AI set entity patrol time cCHARACTER, -1

	` Create a path from Start to End, and assign to our character
	AI make path between points 10, 0,gWaypoints(0).x,gWaypoints(0).z,gWaypoints(1).x,gWaypoints(1).z,300
	AI entity assign patrol path cCHARACTER, 10
	
	` *************************************************
	` Main Loop
	
	do
		
		AI Update
		` Position the character
		posChar(50)
		text 5,5, "x: " + str$(camera position x(),4)
		text 5,25, "Y: " + str$(camera position Y(),4)
		text 5,45, "Z: " + str$(camera position Z(),4)
		text 5,65, AI get entity state$(50)
	
		set camera to follow object position x(50), object position y(50), object position z(50), object angle y(50), 30, 15,50,0
		position camera camera position x(), get terrain ground height(1,camera position x(),camera position z()) + 5,camera position z()
		point camera object position x(50), object position y(50), object position z(50)
		update terrain
		sync
		
	loop
	
	end


function updateWaypoints()
remstart
	In this function we will iterate through the waypoints
	For any waypoint above a height of 60, add a larger cost
remend

	AI make memblock from waypoints 1, 0
	wp = memblock dword(1,0)
	edge = memblock dword(1,4)

	wpStart = 8 : wpEnd = wpStart + (wp * 12) - 1
	edgeStart = wpEnd + 1 : edgeEnd = get memblock size(1) - 1
	
	wpIndex = 0
	for n = wpstart to wpend step 12
		th# = get terrain ground height(1, memblock float(1,n),memblock float(1,n+4))
		if th# > 60
			write memblock float 1,n + 8,2000
			if cDEBUG
				make object cone 600 + start, 12
				position object 600 + start, memblock float(1,n),th#,memblock float(1,n+4)
				color object 600 + start, rgb(255,0,0)
			endif
		endif
		inc start, 1
		inc wpIndex, 1
	next n
	
	AI make waypoints from memblock 0,1
	delete memblock 1
		
endfunction

		 