
remstart
` *********************************************************************
` *********************************************************************
	This include file contains routines that are common to all
	stages of the tutorial.  As previously discusses, modularising
	code is an easy way to share code between projects
` *********************************************************************
` *********************************************************************
remend

` Object numbers for the terrain
#constant cSKY 10
#constant cTREE 20


` *********************************************************************
function makeTerrain()
remstart
	This function sets up the scene
remend

` Load the Terrain
` (Uses DarkBASIC Professional Advanced Terrain Commands)
	load image "terrain.bmp", 1
	load image "detail.tga", 2
	
	make object terrain 1                              ` create the terrain object
	set terrain heightmap 1, "terrain_heightmap.bmp"                 ` set the heightmap
	set terrain scale 1, 3, 0.6, 3                     ` set the scale
	set terrain split 1, 16                            ` split value by 16 * 16
	set terrain tiling 1, 8                            ` detail map tiling
	set terrain light 1, 1, -0.25, 0, 1, 1, 0.78, 0.5  ` light - xdir, ydir, zdir, red, green, blue, intensity
	set terrain texture 1, 1, 2                        ` base and detail texture
	build terrain 1                                    ` finally build the terrain
	position camera 100,get terrain ground height(1,100,100), 100

	` Add some trees to the scene.
	` These trees were made with TreeMagik G3, and exported as dbo objects
	` Billboards can also be exported from TreeMagik for efficiency
	` At each point, we also make an AI obstacle so that the characters cannot bump into them.
	make object cube cTREE + 5, 20
	
	load object "media\lo_tree_a.dbo", cTREE
	set object transparency cTREE, 4
	yrotate object cTREE, rnd(360)
	x# = rnd(200) + 100: z# = rnd(200) + 100
	position object cTREE, x#, get terrain ground height(1, x#, z#),z#
	position object cTREE + 5, x#,0,z# : AI add static obstacle cTREE + 5

	` Tree 2	
	instance object cTREE + 1, cTREE
	yrotate object cTREE + 1, rnd(360)
	x# = rnd(200) + 700: z# = rnd(200) + 300
	position object cTREE + 1, x#, get terrain ground height(1, x#, z#),z#
	position object cTREE + 5, x#,0,z# : AI add static obstacle cTREE + 5
	
	` Tree 3	
	instance object cTREE + 2, cTREE
	yrotate object cTREE + 2, rnd(360)
	x# = rnd(200) + 400: z# = rnd(200) + 1000
	position object cTREE + 2, x#, get terrain ground height(1, x#, z#),z#
	position object cTREE + 5, x#,0,z# : AI add static obstacle cTREE + 5

	` Tree 4
	instance object cTREE + 3, cTREE
	yrotate object cTREE + 3, rnd(360)
	x# = rnd(200) + 1000: z# = rnd(200) + 1000
	position object cTREE + 3, x#, get terrain ground height(1, x#, z#),z#
	position object cTREE + 5, x#,0,z# : AI add static obstacle cTREE + 5

	delete object cTREE + 5
	
	` *********************************************************************
	` Load the Cover points for the character
	load object "media\rock1.dbo", cTREE + 4
	load image "media\rock1.png", cTREE + 4
	texture object cTREE + 4, cTREE + 4
	position object cTREE + 4, 750, get terrain ground height(1, 750, 200),300
	AI add static obstacle cTREE + 4

	instance object cTREE + 5, cTREE + 4
	yrotate object cTREE + 5, 180
	position object cTREE + 5, 750, get terrain ground height(1, 750, 1200),1200
	AI add static obstacle cTREE + 5



	` *********************************************************************
	` Load the Skybox
	tmpDir$ = get dir$()
	set dir "media\sky"
	load object "sky.x", cSKY
	set object light cSKY,0
	scale# = 2000.0 / object size(cSKY) * 100.0
	scale object cSKY, scale#,scale#,scale#
	xrotate object cSKY, 180
	position object cSKY, 750,0,750
	set dir tmpdir$
	set object fog cSKY,0
	
	fog on
	fog color rgb(64,64,92)
	fog distance 1000
	
endfunction


` *********************************************************************

function createWaypoints()
remstart
This function creates the waypoints
and adds markers if the cDEBUG flag is on
remend

	`Set a border around the scenery
	AI start new obstacle 1
	AI add obstacle vertex 0,0
	AI add obstacle vertex 1450, 0
	AI Add obstacle vertex 1450,1450
	AI add obstacle vertex 0, 1450
	AI end new obstacle 0,1, 0

	AI complete obstacles 0

	` Next, add all the waypoint data to the 
	` array for future reference
	` Starting Point
	add to queue gWaypoints()
	gWaypoints().x = 750 : gWaypoints().z = 100
	` Ending Point
	add to queue gWaypoints()
	gWaypoints().x = 750 : gWaypoints().z = 1400
	` Left hand side of hill
	add to queue gWaypoints()
	gWaypoints().x = 225 : gWaypoints().z = 435
	add to queue gWaypoints()
	gWaypoints().x = 120 : gWaypoints().z = 750
	add to queue gWaypoints()
	gWaypoints().x = 215 : gWaypoints().z = 970
	` Right hand side of hill
	add to queue gWaypoints()
	gWaypoints().x = 1300 : gWaypoints().z = 460
	add to queue gWaypoints()
	gWaypoints().x = 1385 : gWaypoints().z = 675
	add to queue gWaypoints()
	gWaypoints().x = 1215 : gWaypoints().z = 960
	` Patrol Points on the hill
	add to queue gWaypoints()
	gWaypoints().x = 475 : gWaypoints().z = 725
	add to queue gWaypoints()
	gWaypoints().x = 885 : gWaypoints().z = 775

	
	` Now take the waypoint data and create the actual AI waypoints
	` We are not using the automatically generated waypoints in this example
	`AI clear waypoints 0
	for n = 0 to array count(gWaypoints())
		x# = gWaypoints(n).x: z# = gWaypoints(n).z
		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 n
	
	AI update waypoint visibility 0, 200
	
endfunction

function freeObj(idx)
remstart
	Get the next free object from given index
remend	
	
	while object exist(idx)
		inc idx, 1
	endwhile
	
endfunction idx
	
	

