
// Project: height maps 
// Created: 2020-02-26

// show all errors

#include ".\..\Templates\ShaderPack\Includes\ShaderPack.agc"

SetErrorMode(2)

// set window properties
SetWindowTitle( "height maps" )
SetWindowSize( 1024, 1024, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window

// set display properties
SetVirtualResolution( 1024, 1024 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 60, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
// enable the sky box
SetSkyBoxVisible( 1 )

// load terrain detail texture with mipmapping enabled
SetGenerateMipmaps(1)

AddVirtualButton(1,950,50,100)
AddVirtualButton(2,850,50,100)
AddVirtualButton(3,750,50,100)
AddVirtualButton(4,650,50,100)
AddVirtualButton(5,550,50,100)


AddVirtualButton(6,450,50,100)
AddVirtualButton(7,350,50,100)


AddVirtualButton(8,50,50,100)
AddVirtualButton(9,150,50,100)


AddVirtualButton(10,50,150,100)
AddVirtualButton(11,150,150,100)

AddVirtualButton(12,50,250,100)
AddVirtualButton(13,150,250,100)

AddVirtualButton(14,250,150,100)
AddVirtualButton(15,350,150,100)

AddVirtualButton(16,250,250,100)
AddVirtualButton(17,350,250,100)
AddVirtualButton(18,250,50,100)

AddVirtualButton(19,150,350,100)
AddVirtualButton(20,50,350,100)





SetVirtualButtonActive(8,0)
SetVirtualButtonActive(9,0)
SetVirtualButtonActive(10,0)
SetVirtualButtonActive(11,0)
SetVirtualButtonActive(12,0)
SetVirtualButtonActive(13,0)

SetVirtualButtonActive(14,0)
SetVirtualButtonActive(15,0)
SetVirtualButtonActive(16,0)
SetVirtualButtonActive(17,0)


SetVirtualButtonActive(19,0)
SetVirtualButtonActive(20,0)

SetVirtualButtonVisible(8,0)
SetVirtualButtonVisible(9,0)
SetVirtualButtonVisible(10,0)
SetVirtualButtonVisible(11,0)
SetVirtualButtonVisible(12,0)
SetVirtualButtonVisible(13,0)

SetVirtualButtonVisible(14,0)
SetVirtualButtonVisible(15,0)
SetVirtualButtonVisible(16,0)
SetVirtualButtonVisible(17,0)



SetVirtualButtonVisible(19,0)
SetVirtualButtonVisible(20,0)



SetVirtualButtonText(1,"Show map")
SetVirtualButtonText(2,"Rase")
SetVirtualButtonText(3,"Water")
SetVirtualButtonText(4,"Flatten")

SetVirtualButtonText(6,"MAX +")
SetVirtualButtonText(7,"MAX -")


SetVirtualButtonText(8,"x -")
SetVirtualButtonText(9,"x +")

SetVirtualButtonText(10,"y -")
SetVirtualButtonText(11,"y +")

SetVirtualButtonText(12,"z -")
SetVirtualButtonText(13,"z +")

SetVirtualButtonText(14,"p_x -")
SetVirtualButtonText(15,"p_x +")

SetVirtualButtonText(16,"P-z -")
SetVirtualButtonText(17,"p_z +")

SetVirtualButtonText(19,"s - ")
SetVirtualButtonText(20,"s + ")

SetVirtualButtonText(5,"Tile")
SetVirtualButtonText(18,"import")


AddVirtualButton(21,950,150,100)
SetVirtualButtonText(21,"Builder")


AddVirtualButton(22,950,50,100)
SetVirtualButtonText(22,"Import")

AddVirtualButton(23,850,50,100)
SetVirtualButtonText(23,"Floor")


AddVirtualButton(24,750,50,100)
SetVirtualButtonText(24,"wall")


AddVirtualButton(25,650,50,100)
SetVirtualButtonText(25,"Rotate L")


AddVirtualButton(26,550,50,100)
SetVirtualButtonText(26,"Rotate R")


AddVirtualButton(27,450,50,100)
SetVirtualButtonText(27,"New site")


AddVirtualButton(28,350,50,100)
SetVirtualButtonText(28,"New floor")




AddVirtualButton(29,850,150,100)
SetVirtualButtonText(29,"Update")


AddVirtualButton(30,250,50,100)
SetVirtualButtonText(30,"stairs")

AddVirtualButton(31,750,150,100)
SetVirtualButtonText(31,"Texture")
AddVirtualButton(32,650,150,100)
SetVirtualButtonText(32,"Edit")


AddVirtualButton(33,550,150,100)
SetVirtualButtonText(33,"Floor")
AddVirtualButton(34,450,150,100)
SetVirtualButtonText(34,"walls")

AddVirtualButton(35,350,150,100)
SetVirtualButtonText(35,"Roof")

AddVirtualButton(36,250,150,100)
SetVirtualButtonText(36,"paint")








type models
	model as integer []
	x as float []
	y as float []
	z as float []
	ax as float []
	ay as float []
	az as float []
	sx as float []
	sy as float []
	sz as float []
	
	
	
	texture as integer []
	amount
endtype




global import as models


type builder
	
	import  as integer []
	model  as integer []
	
	pivit as float [100]
	
	walls_front1 as integer [100,100]
	walls_lr1 as integer [100,100]
	wall_texture1 as integer [100,100] 
	
	walls_front2 as integer [100,100]
	walls_lr2 as integer [100,100]
	wall_texture2 as integer [100,100]
	
	walls_front3 as integer [100,100]
	walls_lr3 as integer [100,100]
	wall_texture3 as integer [100,100]
	
	level1 as integer [100,100]
	level2 as integer [100,100]
	level3 as integer [100,100]
	roof as integer[100,100]
	has_roof as integer[100,100]
	amountas as integer[100,100] 
endtype

global build as builder


type stair_case
	
	
	
	steps as integer[100,13]
	pivit as float [100]
	
	
	
	amountas as integer[100,100] 
endtype

global stairs as stair_case

global dim temp_textures[100]
global dim loaded_textures[100]

SP_Init() // To use the AppShaderKit System you need to initialise it and use its Sync command.
global dim ROAD[1000]
dim holdx[1000]
dim holdz[1000]
dim holdy[1000]
DIM TILES[100]
dim sp_tiles[100]
DIM DOTS[100]
dim grid[10]
global dim trees[100]

global tile_image
global h#
global max

global make_build
global sites=0
global Count
global rod
global up
global grab
global Object

global pointer_x
global pointer_y

GLOBAL MAP
global ttex
global sp
global ste#
global box
global stairs_set
global yes

global x_builds_scale#=1
global y_builds_scale#=1
global z_builds_scale#=1
global temps=1
GLOBAL Edit_MODE
GLOBAL Edit_MODE2
MAX=130
TOP=255
ttex=1
ste#=0.01
make_build=0
up=1
draw=1
rod=1
count=0
grab=0
stairs_set=0
global paint_this
global temp_sprite
global paint_it							


box=CreateObjectBox(10,30,10)
SetObjectCollisionMode(box,0)


WaterNormalImageID=LoadImage("waterNormal.png")
SetImageWrapU(WaterNormalImageID,1)
SetImageWrapV(WaterNormalImageID,1)

WaterObjectID=CreateObjectPlane(1024,1024)
RotateObjectLocalX(WaterObjectID,90)
FixObjectPivot(WaterObjectID)
SetObjectPosition(WaterObjectID,0,-10,40)
SetObjectUVScale(WaterObjectID,0,8,8)
SP_Water_AddObject(WaterObjectID,WaterNormalImageID)
// SP_Water_SetSunColor(WaterObjectID,190,100,0)
 //SP_Water_SetDepthScale(WaterObjectID,0.4)
 //SP_Water_SetDepthColorScale(WaterObjectID,0.2)
 SP_Water_SetWaveStrength(WaterObjectID,0)
 SP_Water_SetRefractionHeightScale(1000.3)
// Setup the water object before any other object so they generate the clipping plane code for the water


LoadImage( 1, "detail.png" )
SetImageWrapU( 1, 1 )
SetImageWrapV( 1, 1 )

// load the terrain shader to give the terrain color based on height
LoadShader( 1, "Terrain.vs", "Terrain.ps" )

// create the terrain object from a height map
CreateObjectFromHeightMap( 1, "\media\height maps\2\05.png", 1024, 50, 1024, 4, 16 )
SetObjectImage( 1, 1, 0 )
SetObjectUVScale( 1, 0, 128, 128 ) // scale the detail texture so it repeats
SetObjectShader( 1, 1 )
SetObjectCollisionMode(1,1)
SetCameraPosition(1, 30, 30, 30)

SetCameraRotation(1,0,0,0)
SetAmbientColor( 128,128,128 )

x#=getcamerax(1)
y#=GetObjectHeightMapHeight(1,getcamerax(1),getcameraz(1))
z#=getcameraz(1)

SetCameraPosition(1,x#,y#+1,z#)
SetObjectPosition(WaterObjectID,x#+500,y#-1,z#+500)

tree=loadobject("\media\foliage\tree01.x")
SetObjectScale(tree,0.1,0.1,0.1)
tree_image=LoadImage("\media\foliage\tree01_d_result.png")
SetObjectImage(tree,tree_image,0)
SetObjectAlphaMask(tree,1)
 


for t=1 to 100
   trees[t]=InstanceObject(tree)
	
_labal:

	x#=random(1,1024)
	z#=random(1,1024)
	y#=GetObjectHeightMapHeight(1,x#,z#)
	hit=ObjectRAYCast(0,x#,y#,z#,x#,y#+200,z#)
	if hit=WaterObjectID then goto _labal:
	SetObjectPosition(trees[t],x#,y#,z#)
	SP_AddObject(trees[t])
	next
	
	

i=loadimage("\media\height maps\2\99.png")
sp=CreateSprite(i)
map=CreateRenderImage(1024,1024,0,0)
setRenderToImage(map,0)
	
		DrawSprite(sp)
		SetRenderToScreen()		
		SetSpriteImage(sp,map)
		SetSpriteSize(sp,1024,1024)



grid[1]=CreateRenderImage(1024,1024,0,0)
SetRenderToImage(grid[1],0)


for q=1 to 1024 step 1024 /50
	
	for t=1 to  1024 step 1024 /50
		
		DrawLine(T,Q,T+1024,Q,MakeColor(0,255,0),MakeColor(0,255,0))

			
		next

		next

for q=1 to 1024 step 1024 /50
	
	for t=1 to  1024 step 1024 /50		
		DrawLine(T,Q,T,Q+1024,MakeColor(0,255,0),MakeColor(0,255,0))		
		next
		next


	SetRenderToScreen()	

sp_grid=CreateSprite(grid[1])
SetSpriteTransparency(sp_grid,1)


SetPrintColor(255,0,0,255)



TILES[1]=loadimage("\media\TILES\TILE2.jpg")
TILES[2]=loadimage("\media\TILES\TILE3.jpg")
TILES[3]=loadimage("\media\TILES\TILE4.png")
TILES[4]=loadimage("\media\TILES\TILE5.jpg")

AddVirtualButton(91,990,250,50)
SetVirtualButtonImageUp(91,TILES[1])

AddVirtualButton(92,990,300,50)
SetVirtualButtonImageUp(92,TILES[2])

AddVirtualButton(93,990,350,50)
SetVirtualButtonImageUp(93,TILES[3])

AddVirtualButton(94,990,400,50)
SetVirtualButtonImageUp(94,TILES[4])











C#=MakeColor(200,200,55)
DrawEllipse(5,5,20,20,C#,C#,1)
DOT1=GetImage(0,0,10,10)

FOR T=1 TO 100
DOTS[T]=CreateSprite(DOT1)
NEXT





C#=MakeColor(0,80,80)
Drawbox(1,1,1+1024/50,1+1024/50,C#,C#,c#,c#,1)


tile_image=GetImage(1,1,1+1024/50,1+1024/50)
dim sp_tiles[1000]

red=MakeColor(255,0,0)
DrawEllipse(3,3,6,6,red,red,1)
im2=getimage(0,0,10,10)	
CreateSprite(100,im2)

SetSpriteDepth(100,0)





for t=22 to 28

						SetVirtualButtonVisible(t,0)
						SetVirtualButtonActive(t,0)
						next




SetCameraPosition(1,150,50,150)

SetObjectCollisionMode(WaterObjectID,0)
do
	
	if GetVirtualButtonPressed(21) then make_build=1-make_build
	if GetVirtualButtonPressed(29) then updata()
	
	if make_build=1
		for t=1 to 8
			SetVirtualButtonVisible(t,0)
			SetVirtualButtonActive(t,0)
			next
		
			SetVirtualButtonVisible(18,0)
			SetVirtualButtonActive(18,0)
		
		for t=21 to 28
			SetVirtualButtonVisible(t,1)
			SetVirtualButtonActive(t,1)
			next
		SetVirtualButtonVisible(30,1)
		SetVirtualButtonActive(30,1)
			sync()
		build_it()
		endif

		if GetVirtualButtonPressed(91) then ttex=1
		if GetVirtualButtonPressed(92) then ttex=2
		if GetVirtualButtonPressed(93) then ttex=3
		if GetVirtualButtonPressed(94) then ttex=4

		IF GetVirtualButtonPressed(31) THEN EDIT_MODE=2
		IF GetVirtualButtonPressed(32) THEN EDIT_MODE=1
		
		
		
	
		
		

		
		
		
		
		
		
		IF GetVirtualButtonPressed(33) 
			 EDIT_MODE2=2
			 if yes=1
				 for t=1 to 36
		SetObjectCollisionMode(build.walls_lr2[sites,t],0)
		NEXT
		
		
		for t=1 to 35
		SetObjectCollisionMode(build.walls_front2[sites,t],0)
		
		NEXT
		endif
		endif	 
			 
		IF GetVirtualButtonPressed(34) 
			
			 EDIT_MODE2=1
if yes=1
	for t=1 to 36
		SetObjectCollisionMode(build.walls_lr2[sites,t],1)
		NEXT
		
		
		for t=1 to 35
		SetObjectCollisionMode(build.walls_front2[sites,t],1)
		
		NEXT
		endif



endif

	if make_build=0
	SetVirtualButtonVisible(18,1)
	SetVirtualButtonActive(18,1)
	SetVirtualButtonVisible(30,0)
	SetVirtualButtonActive(30,0)
	SetVirtualButtonVisible(35,0)
	SetVirtualButtonVisible(36,0)
	SetVirtualButtonActive(35,0)
	SetVirtualButtonActive(36,0)
	
	
	
	
			if GetVirtualButtonState(18) and draw=1
				import_model()
				endif
			
			if GetVirtualButtonState(6) and MAX<TOP  then MAX=MAX+1
			if GetVirtualButtonState(7) and MAX>90   then MAX=MAX-1
	
			SetSpritePosition(100,GetCameraX(1),1024-getcameraZ(1))

			if GetVirtualButtonReleased(5) and draw=1  
				rod=1-rod
				for t=8 to 20
					SetVirtualButtonActive(t,1-rod)
					SetVirtualButtonVisible(t,1-rod)
					next
					endif

			if rod=1
				if GetVirtualButtonReleased(1)
					 H#=110 
					 draw=1-draw
					 MoveCameraLocalY(1,10)
	
					 updata()
					 endif
					 endif

			if draw=0 
				 drawmap()
				 if GetPointerState()=0
					 h#=110
					 endif
					 endif
				 				 
			if rod=0 then make_tiles()
			if draw=1 and rod=1 then picker()	
			 edit_models()
	
			
	
	
	
			if count>0 and rod=1
				for t=1 to Count					
					tx#=GetObjectWorldX(road[t]) 
					tz#=GetObjectWorldZ(road[t]) 
					SetSpritePosition( sp_tiles[t],TX#,1024-TZ#)	
					next			
					endif

				for t=1 to 100
					tx#=GetObjectWorldX(trees[t]) 
					tz#=GetObjectWorldZ(trees[t]) 
					SetSpritePosition(DOTS[T],TX#,1024-TZ#)
					NEXT
										
	PRINT("MAX=" +STR(MAX))
	sp_sync()
	if grab>0 and object=1 then move_trees()
if grab>0 and  object=3 then move_site()
	endif
  
loop


function move_trees()
	
      
		pointer_x1 = GetPointerX()		
		pointer_y1 = GetPointerY()
    
       if pointer_x1<pointer_x then MoveObjectLocalX( trees[grab],-1)
       if pointer_x1>pointer_x then MoveObjectLocalX( trees[grab],1)
        
       if pointer_y1<pointer_y then MoveObjectLocalz( trees[grab],1)
       if pointer_y1>pointer_y then MoveObjectLocalz( trees[grab],-1)
        
        
			x#=GetObjectWorldX(trees[grab])
			z#=GetObjectWorldz(trees[grab])
			y#=GetObjectHeightMapHeight(1,x#,z#)
	
			SetobjectPosition(trees[grab],x#,y#+1,z#)
    
        if  GetPointerState()=0 
			 grab=0
			 object=0
			 endif
			 
        
	endfunction
	

function move_site()
	
	pointer_x1 = GetPointerX()		
		pointer_y1 = GetPointerY()
    if GetRawKeyState(16)=0
       if pointer_x1<pointer_x then MoveObjectLocalX( build.pivit[grab],-1)
       if pointer_x1>pointer_x then MoveObjectLocalX( build.pivit[grab],1)
        
       if pointer_y1<pointer_y then MoveObjectLocalz(build.pivit[grab],1)
       if pointer_y1>pointer_y then MoveObjectLocalz( build.pivit[grab],-1)
        endif
        
        if GetRawKeyState(16)=1
       if pointer_x1<pointer_x then MoveObjectLocalX( build.pivit[grab],-0.1)
       if pointer_x1>pointer_x then MoveObjectLocalX( build.pivit[grab],0.1)
        
       if pointer_y1<pointer_y then MoveObjectLocalz(build.pivit[grab],0.1)
       if pointer_y1>pointer_y then MoveObjectLocalz( build.pivit[grab],-0.1)
        endif
		
    
        if  GetPointerState()=0 
			 grab=0
			 object=0
			 endif
	
	
	
	
endfunction



function edit_models()

	
	print("step#="+str(ste#))
	
	if GetVirtualButtonState(19) and ste#>0 then ste#=ste#-0.001
	if GetVirtualButtonState(20) and ste#<1 then ste#=ste#+0.001

		if GetVirtualButtonState(8) 
			
			import.sx[grab]=import.sx[grab]-ste#
		    SetObjectScale(import.model[grab],import.sx[grab],import.sy[grab],import.sz[grab])
			endif
	
		if GetVirtualButtonState(9) 
			
			import.sx[grab]=import.sx[grab]+ste#
		    SetObjectScale(import.model[grab],import.sx[grab],import.sy[grab],import.sz[grab])
			endif
	
		
		if GetVirtualButtonState(10) 
			
			import.sy[grab]=import.sy[grab]-ste#
		    SetObjectScale(import.model[grab],import.sx[grab],import.sy[grab],import.sz[grab])
			endif
	
		if GetVirtualButtonState(11) 
			import.sy[grab]=import.sy[grab]+ste#
		    SetObjectScale(import.model[grab],import.sx[grab],import.sy[grab],import.sz[grab])
			endif

		if GetVirtualButtonState(12) 
			import.sz[grab]=import.sz[grab]-ste#
		    SetObjectScale(import.model[grab],import.sx[grab],import.sy[grab],import.sz[grab])
			endif
	
		if GetVirtualButtonState(13) 
			import.sz[grab]=import.sz[grab]+ste#
		    SetObjectScale(import.model[grab],import.sx[grab],import.sy[grab],import.sz[grab])
			endif


		if GetVirtualButtonState(14) 
			MoveObjectLocalX(import.model[grab],-ste#)
			endif


		if GetVirtualButtonState(15) 
			MoveObjectLocalX(import.model[grab],ste#)
			endif

		if GetVirtualButtonState(16) 
			MoveObjectLocalz(import.model[grab],-ste#)
			endif


		if GetVirtualButtonState(17) 
			MoveObjectLocalz(import.model[grab],ste#)
			endif

		if  GetRawKeyPressed(32)
			 grab=0
			 object=0
			 
			 	SetVirtualButtonActive(8,0)
				SetVirtualButtonActive(9,0)
				SetVirtualButtonActive(10,0)
				SetVirtualButtonActive(11,0)
				SetVirtualButtonActive(12,0)
				SetVirtualButtonActive(13,0)
				SetVirtualButtonActive(14,0)
				SetVirtualButtonActive(15,0)
				SetVirtualButtonActive(16,0)
				SetVirtualButtonActive(17,0)
				
				SetVirtualButtonActive(19,0)
				SetVirtualButtonActive(20,0)
				
				
				
				SetVirtualButtonVisible(8,0)
				SetVirtualButtonVisible(9,0)
				SetVirtualButtonVisible(10,0)
				SetVirtualButtonVisible(11,0)
				SetVirtualButtonVisible(12,0)
				SetVirtualButtonVisible(13,0)
				SetVirtualButtonVisible(14,0)
				SetVirtualButtonVisible(15,0)
				SetVirtualButtonVisible(16,0)
				SetVirtualButtonVisible(17,0)
				SetVirtualButtonVisible(19,0)
				SetVirtualButtonVisible(20,0)
			 endif			 						 


	
	
	endfunction
	


function picker()
	
	
	SetVirtualButtonVisible(31,0)
	
	SetVirtualButtonVisible(32,0)
	SetVirtualButtonVisible(33,0)
	
	SetVirtualButtonVisible(34,0)
	
	
	SetSpriteVisible(sp,0)
	if GetRawKeyState(38)=1  then MoveCameraLocalZ(1,0.3)
	if GetRawKeyState(40)=1  then MoveCameraLocalZ(1,-0.3)
	if GetRawKeyState(37)=1  then RotateCameraLocalY(1,-2)
	if GetRawKeyState(39)=1  then RotateCameraLocalY(1,2)

	
	
	MoveCameraLocalY(1,-1)
	x#=getcamerax(1)
	y#=getcameray(1)
	z#=getcameraz(1)
	//
	
	hit=ObjectRAYCast(0,x#,y#,z#,x#,y#-2,z#)
	if hit<>0
	
		
		newy#=GetObjectRayCastY(0)
		SetCameraPosition(1,x#,newy#+2,z#)
		
		else
			x#=getcamerax(1)
			z#=getcameraz(1)
			y#=GetObjectHeightMapHeight(1,x#,z#)
		   SetCameraPosition(1,x#,y#+2,z#)
		
		
		
			ENDIF
			
	

		

	
	
	
		
	pointer_x = GetPointerX()		
    pointer_y = GetPointerY()
                
	if grab=0
      
        // get the x, y and z unit vectors based on the pointer position
        unit_x# = Get3DVectorXFromScreen(pointer_x,pointer_y)        
        unit_y# = Get3DVectorYFromScreen(pointer_x,pointer_y)      
        unit_z# = Get3DVectorZFromScreen(pointer_x,pointer_y)
       
        // calculate the start of the ray cast, which is the unit vector + the camera position
        start_x# = unit_x# + GetCameraX(1)       
        start_y# = unit_y# + GetCameraY(1)      
        start_z# = unit_z# + GetCameraZ(1)      
      
        // calculate the end of the vector, which is the unit vector multiplied by the length of the ray cast and then add the camera position to it
        end_x# = 800*unit_x# + GetCameraX(1)     
        end_y# = 800*unit_y# + GetCameraY(1)       
        end_z# = 800*unit_z# + GetCameraZ(1)
       
        // determine which object has been hit
        object_hit = ObjectrayCast(0,start_x#,start_y#,start_z#,end_x#,end_y#,end_z#)
        
        
       
			if object_hit <> 0
			
				for t=1 to 100
					if  object_hit=trees[t] 			
						x#=GetObjectWorldX(trees[t])			
						y#=GetObjectWorldy(trees[t])			
						z#=GetObjectWorldz(trees[t])						
					
						SetObjectPosition(box,x#,y#+10,z#)	
							
						if GetPointerState()=1 				
							grab=t	
							object=1				 
							SetObjectRotation(trees[grab],GetCameraAngleX(1),GetCameraAngley(1),GetCameraAnglez(1))						 
							endif
					endif			          
				next 
             
             
            if sites>0
           for t=1  to  sites
				if  object_hit=build.pivit[t] 
					if GetPointerState()=1
					grab=t	
					object=3
					endif
					
					x#=GetObjectWorldX(build.pivit[t])			
					y#=GetObjectWorldy(build.pivit[t])			
					z#=GetObjectWorldz(build.pivit[t])										
					SetObjectPosition(box,x#,y#+10,z#)	
					
					
					
			
					endif
             next
             endif
             
             
			
			if 	import.amount>0
				for t=0 to import.amount-1		
				if  object_hit=import.model[t] 
								
					x#=GetObjectWorldX(import.model[t])			
					y#=GetObjectWorldy(import.model[t])			
					z#=GetObjectWorldz(import.model[t])						
					
					SetObjectPosition(box,x#,y#+10,z#)	
							
					if GetPointerState()=1 				
						 grab=t	
						 object=2	
						 
					SetVirtualButtonActive(8,1)
					SetVirtualButtonActive(9,1)
					SetVirtualButtonActive(10,1)
					SetVirtualButtonActive(11,1)
					SetVirtualButtonActive(12,1)
					SetVirtualButtonActive(13,1)
					SetVirtualButtonActive(14,1)
					SetVirtualButtonActive(15,1)
					SetVirtualButtonActive(16,1)
					SetVirtualButtonActive(17,1)
					SetVirtualButtonActive(18,1)
					SetVirtualButtonActive(19,1)
					SetVirtualButtonActive(20,1)
					
					
					SetVirtualButtonVisible(8,1)
					SetVirtualButtonVisible(9,1)
					SetVirtualButtonVisible(10,1)
					SetVirtualButtonVisible(11,1)
					SetVirtualButtonVisible(12,1)
					SetVirtualButtonVisible(13,1)
					SetVirtualButtonVisible(14,1)
					SetVirtualButtonVisible(15,1)
					SetVirtualButtonVisible(16,1)
					SetVirtualButtonVisible(17,1)
					SetVirtualButtonVisible(18,1)
					SetVirtualButtonVisible(19,1)
					SetVirtualButtonVisible(20,1)	 	 
					endif
					
				endif			          
             next 
             endif
             
                              
        endif       
		endif

	
endfunction

function make_tiles()
		if  count>0
			
		if GetVirtualButtonState(8) 
		   SetObjectScalePermanent(road[count],0.95,1,1)
		endif

		if GetVirtualButtonState(9) 
			SetObjectScalePermanent(road[count],1.05,1,1)
			endif
			
		if GetVirtualButtonState(10) 
			SetObjectScalePermanent(road[count],1,0.95,1)
			endif
	
		if GetVirtualButtonState(11) 
			SetObjectScalePermanent(road[count],1,1.05,1)
			endif

		if GetVirtualButtonState(12) 
			SetObjectScalePermanent(road[count],1,1,0.95)
			endif
	
		if GetVirtualButtonState(13) 
			SetObjectScalePermanent(road[count],1,1,1.05)
			endif

		if GetVirtualButtonState(14) 
			MoveObjectLocalX(road[count],-1)
			endif

		if GetVirtualButtonState(15) 
			MoveObjectLocalX(road[count],1)
			endif

		if GetVirtualButtonState(16) 
			MoveObjectLocalz(road[count],-1)
			endif

		if GetVirtualButtonState(17) 
			MoveObjectLocalz(road[count],1)
			endif
			
		tx1#=GetObjectWorldX(road[count]) 
		tz1#=GetObjectWorldZ(road[count]) 
		SetSpritePosition( sp_tiles[count],TX1#,1024-TZ1#)
		
	endif


	
	a=1024/50

	for q=1 to 1024 step 1024 /50	
	for t=1 to  1024 step 1024 /50
		px = GetPointerX()		
		py = 1024- GetPointerY()+a	
			if GetPointerState()

			if px>t and px<t+a	
			if py>q and py<q+a
						
						if GetObjectExists(road[count+1])=0							
							
							c#=MakeColor(120,120,120)
							SetRenderToImage(map,0)
							Drawbox( t-25,1024- q-25, t+25,1024- q+25, c#,c#,c#,c#, 1 )
							
							SetRenderToScreen()			
							
							SaveImage(map,"image101.png")

								DeleteObject(1)
								CreateObjectFromHeightMap( 1, "image101.png", 1024, 50, 1024, 4, 16 )
								SetObjectImage( 1, 1, 0 )
								SetObjectUVScale( 1, 0, 128, 128 ) // scale the detail texture so it repeats
								SetObjectShader( 1, 1 )
									SetObjectCollisionMode(1,1)
							
							
							inc count
							road[count]=CreateObjectBox(1024 /50,2,1024 /50)
							
							holdx[count]=t
							
							holdz[count]=q
							
							holdy[count]=GetObjectHeightMapHeight(1,t,q)
							
							SetObjectPosition(road[count],holdx[count],holdy[count],holdz[count])
							
							 sp_tiles[count]=CreateSprite(tile_image)

							setobjectimage(road[count],TILES[ttex],0)
							sync()
							endif
					endif
					endif
			
			endif
	
			next	
		next
	
	
	
	

endfunction




function drawmap()
	
	if GetVirtualButtonReleased(2) then up=1
	if GetVirtualButtonReleased(3) then up=0
	if GetVirtualButtonReleased(4) then up=2
	
	
	
		SetSpriteVisible(sp,1)
		SetSpriteVisible(100,0)

		pointer_x = GetPointerX()		
		pointer_y = GetPointerY()
	
		if GetPointerState()=0
			start_x=pointer_x
			start_y=pointer_y
			endif
		
		
			if pointer_x>1 and pointer_x<1024
			if pointer_y>1 and pointer_y<1024
				dd=0
			if GetPointerState()=1
				dd=1
				y#=GetObjectHeightMapHeight(1,pointer_x,pointer_y)
						
				if up=1					
					IF H#<MAX then H#=H#+1
						c#=MakeColor(H#,H#,H#)
						endif
					
				if up=0
					c#=MakeColor(0,200,200)
					endif
					
				if up=2
					c#=MakeColor(120,120,120)
					endif
					
					
		
						SetRenderToImage(map,0)
						DrawEllipse( pointer_x, pointer_y, 10, 10, c#,c#, 1 )
						SetRenderToScreen()	
						sync()

					
						
					
					
					
						
									
				endif
				endif
				endif	
				
				
				SetSpriteVisible(100,1)					
				
endfunction


function updata()
	SaveImage(map,"image101.png")
	DeleteObject(1)
	CreateObjectFromHeightMap( 1, "image101.png", 1024, 50, 1024, 4, 16 )
	SetObjectImage( 1, 1, 0 )
	SetObjectUVScale( 1, 0, 128, 128 ) // scale the detail texture so it repeats
	SetObjectShader( 1, 1 )
	SetObjectCollisionMode(1,1)
endfunction



function import_model()
	
		load=0
        local spr

        ret$=ChooseRawFile("*.x;*.obj;*.fbx")
        if ret$=""
                no=0
        else

			
					import.model.insert(loadobject(ret$))
					load=1
					
				import.sx.insert(0.05)
				import.sy.insert(0.05)
				import.sz.insert(0.05) 

					 ret$=ChooseRawFile("*.png;*.jpg")
					 
					 if ret$=""
							no=0
						else

				     import.texture.insert(LoadImage(ret$))
					endif
        endif
  
if load=1
	repeat
	
			a=1024/50
				for q=1 to 1024 step 1024 /50	
				for t=1 to  1024 step 1024 /50
					px = GetPointerX()		
					py = 1024- GetPointerY()+a	
			
						if GetPointerState()

							if px>t and px<t+a	
							if py>q and py<q+a
								
								y#=GetObjectHeightMapHeight(1,t,q)
								SetObjectPosition(import.model[import.amount],t,y#,q)
								SetObjectScale(import.model[import.amount],0.05,0.05,0.05)
								SetObjectImage(import.model[import.amount],import.texture[import.amount],0)
								SetObjectAlphaMask(import.model[import.amount],1)

								load=0
								inc import.amount,1
								endif
								endif
						endif
	next
	next
	
	
	sync()

	until load=0

	endif

	endfunction



function build_it()
	
	
	
	
	
	
	if sites>0 and build.has_roof[sites,1]=0
	if GetVirtualButtonPressed(35)
		build.roof[sites,1]=LoadObject("\media\builder\roof.obj")
		FixObjectToObject(build.roof[sites,1],build.pivit[sites])
		build.has_roof[sites,1]=1
	   if yes=1 
		   MoveObjectLocalY(build.roof[sites,1],10)
		 
		    MoveObjectLocalx(build.roof[sites,1],10)
		    MoveObjectLocalz(build.roof[sites,1],11)
		    SetObjectImage(build.roof[sites,1],TILES[1],0)
			endif
	
	if yes=0 
		    MoveObjectLocalY(build.roof[sites,1],5)
		    MoveObjectLocalx(build.roof[sites,1],10)
		    MoveObjectLocalz(build.roof[sites,1],11)
		    SetObjectImage(build.roof[sites,1],TILES[1],0)
			endif
	
	endif
	
	endif
	
	
	
	
	if GetVirtualButtonPressed(36) and paint_it=0
	paint_it=1
	endif
	
	
	

	SetVirtualButtonVisible(31,1)	
	SetVirtualButtonVisible(32,1)
	SetVirtualButtonVisible(33,1)	
	SetVirtualButtonVisible(34,1)
	SetVirtualButtonVisible(35,1)
	SetVirtualButtonVisible(36,1)
	SetVirtualButtonActive(35,1)
	SetVirtualButtonActive(36,1)

	for t=1 to 255
		if GetRawKeyState(t)
			print(t)
			endif
			next

    if GetRawKeyState(107) 
		 
		 x_builds_scale#=x_builds_scale#+0.01
		 y_builds_scale#=y_builds_scale#+0.01
		 z_builds_scale#=z_builds_scale#+0.01
		 SetObjectScale(build.pivit[sites],x_builds_scale#,y_builds_scale#,z_builds_scale#)
		
		 endif
		 
		 
    if GetRawKeyState(109) 
		
		 x_builds_scale#=x_builds_scale#-0.01
		 y_builds_scale#=y_builds_scale#-0.01
		 z_builds_scale#=z_builds_scale#-0.01
		 SetObjectScale(build.pivit[sites],x_builds_scale#,y_builds_scale#,z_builds_scale#)
		 
		
	
		
		endif

	if stairs_set>0
	    if GetRawKeyState(asc("W")) THEN MoveObjectLocalZ(stairs.pivit[stairs_set],0.1)
		if GetRawKeyState(asc("S")) THEN MoveObjectLocalZ(stairs.pivit[stairs_set],-0.1)	
		if GetRawKeyState(asc("A")) THEN MoveObjectLocalX(stairs.pivit[stairs_set],-0.1)	
		if GetRawKeyState(asc("D")) THEN MoveObjectLocalX(stairs.pivit[stairs_set],0.1)
		if GetRawKeyPressed(asc("R")) THEN ROTATEObjectLocalY(stairs.pivit[stairs_set],90)
		endif

	if GetRawKeyState(38)=1  then MoveCameraLocalZ(1,0.3)
	if GetRawKeyState(40)=1  then MoveCameraLocalZ(1,-0.3)
	if GetRawKeyState(37)=1  then RotateCameraLocalY(1,-2)
	if GetRawKeyState(39)=1  then RotateCameraLocalY(1,2)

	x#=getcamerax(1)
	y#=GetObjectHeightMapHeight(1,getcamerax(1),getcameraz(1))
	z#=getcameraz(1)
	
	SetCameraPosition(1,x#,y#+13,z#)
	
	
	
	

if GetVirtualButtonPressed(30)
	if sites>0
		stairs_set=stairs_set+1
		stairs.pivit[stairs_set]=CreateObjectSphere(1,32,32)
		z#=0
		y#=0.3
			for p=1 to 8
		
				stairs.steps[stairs_set,p]=CreateObjectBox(2,y#*2,0.5)
				FixObjectToObject(stairs.steps[stairs_set,p],stairs.pivit[stairs_set])
				MoveObjectLocalZ(stairs.steps[stairs_set,p],z#)
				
				MoveObjectLocaly(stairs.steps[stairs_set,p],y#)
				y#=y#+0.3
				z#=z#+0.5
				
			next
			
	FixObjectToObject(stairs.pivit[stairs_set],build.pivit[sites])
	MoveObjectLocalZ(stairs.pivit[stairs_set],-1.5)
	MoveObjectLocalx(stairs.pivit[stairs_set],1.5)
	
	endif
	
endif
	

if GetVirtualButtonPressed(27)
	sites=sites+1
	
	build.pivit[sites]=CreateObjectCylinder(29,0.3,32)
    x=0
    z=0
    c=0
    for t=1 to 30
		c=c+1
		build.level1[sites,t]=createobjectplane(5,5)
		RotateObjectLocalX(build.level1[sites,t],90)
		FixObjectPivot(build.level1[sites,t])
		FixObjectToObject(build.level1[sites,t],build.pivit[sites])
		MoveObjectLocalX(build.level1[sites,t],x)
		MoveObjectLocalZ(build.level1[sites,t],Z)
		
		X=X+5
		
			IF C=5
				C=0
				X=0
				Z=Z+5
				ENDIF
		NEXT
		
		x=0
    z=0
    c=0
    for t=1 to 35
		c=c+1
		build.walls_front1[sites,t]=createobjectbox(5.3,5,0.5)
		
		
		FixObjectToObject(build.walls_front1[sites,t],build.pivit[sites])
		MoveObjectLocaly(build.walls_front1[sites,t],2.5)
		MoveObjectLocalX(build.walls_front1[sites,t],x)
		MoveObjectLocalZ(build.walls_front1[sites,t],Z-2.5)
		
		X=X+5
		
			IF C=5
				C=0
				X=0
				Z=Z+5
				ENDIF
		NEXT
		
			x=0
    z=0
    c=0
		
		for t=1 to 36
		c=c+1
		build.walls_lr1[sites,t]=createobjectbox(5.48,5,0.5)
		RotateObjectLocaly(build.walls_lr1[sites,t],90)
		FixObjectPivot(build.walls_lr1[sites,t])
		
		FixObjectToObject(build.walls_lr1[sites,t],build.pivit[sites])
		
		MoveObjectLocaly(build.walls_lr1[sites,t],2.5)
		
		
		MoveObjectLocalX(build.walls_lr1[sites,t],x-2.5)
		MoveObjectLocalZ(build.walls_lr1[sites,t],Z)
		
		X=X+5
		
			IF C=6
				C=0
				X=0
				Z=Z+5
				ENDIF
		NEXT
		
		
		
		
		
		
		
		SET=1	
			repeat
	
				a=1024/50
				for q=1 to 1024 step 1024 /50	
				for t=1 to  1024 step 1024 /50
					px = GetPointerX()		
					py = 1024- GetPointerY()+a	
			
						if GetPointerState()

							if px>t and px<t+a	
							if py>q and py<q+a
								
								c#=MakeColor(120,120,120)
								SetRenderToImage(map,0)
								Drawbox( t-35,1024- q-35, t+35,1024- q+35, c#,c#,c#,c#, 1 )
								
							SetRenderToScreen()			
							
							SaveImage(map,"image101.png")

								DeleteObject(1)
								CreateObjectFromHeightMap( 1, "image101.png", 1024, 50, 1024, 4, 16 )
								SetObjectImage( 1, 1, 0 )
								SetObjectUVScale( 1, 0, 128, 128 ) // scale the detail texture so it repeats
								SetObjectShader( 1, 1 )
								SetObjectCollisionMode(1,1)
								y#=GetObjectHeightMapHeight(1,t,q)
								SetObjectPosition(build.pivit[sites],t,y#+0.1,q-25)
							

								SET=0
							
								endif
								endif
						endif
					
				next
				next
	
	
	SYNC()

	until SET=0
    
    ENDIF
    
    if GetVirtualButtonPressed(28) and sites>0 and yes=0
	yes=1
    x=0
    z=0
    c=0
    for t=1 to 30
		c=c+1
		build.level2[sites,t]=createobjectbox(5,0.5,5)
	
		FixObjectToObject(build.level2[sites,t],build.pivit[sites])
		MoveObjectLocalX(build.level2[sites,t],x)
		MoveObjectLocalZ(build.level2[sites,t],Z)
		MoveObjectLocaly(build.level2[sites,t],5)
		X=X+5
		
			IF C=5
				C=0
				X=0
				Z=Z+5
				ENDIF
		NEXT
		
		x=0
    z=0
    c=0
    for t=1 to 35
		c=c+1
		build.walls_front2[sites,t]=createobjectbox(5.3,5,0.5)
		
		
		FixObjectToObject(build.walls_front2[sites,t],build.pivit[sites])
		MoveObjectLocaly(build.walls_front2[sites,t],7.5)
		
		MoveObjectLocalX(build.walls_front2[sites,t],x)
		MoveObjectLocalZ(build.walls_front2[sites,t],Z-2.5)
		
		X=X+5
		
			IF C=5
				C=0
				X=0
				Z=Z+5
				ENDIF
		NEXT
		
			x=0
    z=0
    c=0
		
		for t=1 to 36
		c=c+1
		build.walls_lr2[sites,t]=createobjectbox(5.48,5,0.5)
		RotateObjectLocaly(build.walls_lr2[sites,t],90)
		FixObjectPivot(build.walls_lr2[sites,t])
		
		FixObjectToObject(build.walls_lr2[sites,t],build.pivit[sites])
		
		MoveObjectLocaly(build.walls_lr2[sites,t],7.5)
		
		
		MoveObjectLocalX(build.walls_lr2[sites,t],x-2.5)
		MoveObjectLocalZ(build.walls_lr2[sites,t],Z)
		
		X=X+5
		
			IF C=6
				C=0
				X=0
				Z=Z+5
				ENDIF
		NEXT
		endif
   
	
	if GetVirtualButtonPressed(22)
		ret$=ChooseRawFile("*.x;*.obj;*.fbx")
			if ret$=""
				no=0
				else		
				build.model.insert(loadobject(ret$))
				endif
	ENDIF
	
	
	
	
   IF GetVirtualButtonPressed(21)
					for t=1 to 7

						SetVirtualButtonVisible(t,1)
						SetVirtualButtonActive(t,1)
						next
		
						SetVirtualButtonVisible(18,1)
						SetVirtualButtonActive(18,1)
	
					for t=22 to 28

						SetVirtualButtonVisible(t,0)
						SetVirtualButtonActive(t,0)
						next
				SP_SYNC()		
			make_build=0
			ENDIF	

			
		if sites>0 and paint_it=0 then 	site_picker()
		if  sites>0 and paint_it=1 and paint_this=0 then 	paint_picker()
					
		if  sites>0 and paint_it=1 and paint_this>0 then 	paint()	
	endfunction
	
	function paint()
		
		
		
		
	
			px=GetPointerX()
			py=GetPointery()
			if GetPointerState()=1
				SetRenderToImage(temp_textures[temps],0)	 
				DrawEllipse(px,py,10,10,MakeColor(255,2555,0),MakeColor(255,255,0),1)					
					SetRenderToScreen()
					SetImageTransparentColor(temp_textures[temps],0,0,0)
					setobjectimage(paint_this,temp_textures[temps],0)	
					SetObjectAlphaMask(paint_this,1)		 
			endif				
	
	
	
	
	
	if GetVirtualButtonPressed(36) and paint_it=1 
		paint_it=0
		temps=temps+1
	
	
			deletesprite(temp_sprite)
	
		 paint_this=0
		endif
								
									
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	endfunction
	
function site_picker()
	
	

	
		
	pointer_x = GetPointerX()		
    pointer_y = GetPointerY()
                
	
      
			// get the x, y and z unit vectors based on the pointer position
			unit_x# = Get3DVectorXFromScreen(pointer_x,pointer_y)        
			unit_y# = Get3DVectorYFromScreen(pointer_x,pointer_y)      
			unit_z# = Get3DVectorZFromScreen(pointer_x,pointer_y)
       
			// calculate the start of the ray cast, which is the unit vector + the camera position
			start_x# = unit_x# + GetCameraX(1)       
			start_y# = unit_y# + GetCameraY(1)      
			start_z# = unit_z# + GetCameraZ(1)      
      
			// calculate the end of the vector, which is the unit vector multiplied by the length of the ray cast and then add the camera position to it
			end_x# = 800*unit_x# + GetCameraX(1)     
			end_y# = 800*unit_y# + GetCameraY(1)       
			end_z# = 800*unit_z# + GetCameraZ(1)
       
			// determine which object has been hit
			object_hit = ObjectrayCast(0,start_x#,start_y#,start_z#,end_x#,end_y#,end_z#)
        
        
       
			if object_hit <> 0
			
			
					for t=1 to 36		
						setobjectcolor(build.walls_lr1[sites,t],250,250,250,255)		
							if  object_hit=build.walls_lr1[sites,t]														
								setobjectcolor(build.walls_lr1[sites,t],255,00,00,255)
								if  GetPointerPressed() AND EDIT_MODE=1 and EDIT_MODE2=1
									 SetObjectVisible(build.walls_lr1[sites,t],0)
									 
									 ENDIF
									 
									 
									 IF GetPointerPressed() AND EDIT_MODE=2 and EDIT_MODE2=1
								   
								    SETOBJECTIMAGE(build.walls_lr1[sites,t],TILES[ttex],0)
								    setobjectcolor(build.walls_lr1[sites,t],250,250,250,255)
								    ENDIF
									 																									
								endif			          
								next 
			     
			     
			     
			     
			       for t=1 to 35
						setobjectcolor(build.walls_front1[sites,t],250,250,250,255)		
							if  object_hit=build.walls_front1[sites,t]														
								setobjectcolor(build.walls_front1[sites,t],255,00,00,255)	
								if GetPointerPressed() AND EDIT_MODE=1 and EDIT_MODE2=1
									 SetObjectVisible(build.walls_front1[sites,t],0)
									 ENDIF
									 	
								
							   IF GetPointerPressed() AND EDIT_MODE=2 and EDIT_MODE2=1
								   
								    SETOBJECTIMAGE(build.walls_front1[sites,t],	TILES[ttex],0)
								    setobjectcolor(build.walls_front1[sites,t],250,250,250,255)
								    ENDIF
								
								
																																	
								endif			          
								next 
		
			     
			     
			     
			     if sites>0 and yes=1
			     for t=1 to 36		
						setobjectcolor(build.walls_lr2[sites,t],250,250,250,255)		
							if  object_hit=build.walls_lr2[sites,t]														
								setobjectcolor(build.walls_lr2[sites,t],255,00,00,255)
								if GetPointerPressed() AND EDIT_MODE=1 and EDIT_MODE2=1
									 SetObjectVisible(build.walls_lr2[sites,t],0)
									 
									 ENDIF
									 IF GetPointerPressed() AND EDIT_MODE=2  and EDIT_MODE2=1
								   
								    SETOBJECTIMAGE(build.walls_lr2[sites,t],TILES[ttex],0)
								    setobjectcolor(build.walls_lr2[sites,t],250,250,250,255)
								    ENDIF
									 																									
								endif			          
								next 
			     
			     
			     
			     
			       for t=1 to 35
						setobjectcolor(build.walls_front2[sites,t],250,250,250,255)		
							if  object_hit=build.walls_front2[sites,t]														
								setobjectcolor(build.walls_front2[sites,t],255,00,00,255)	
								if GetPointerPressed() AND EDIT_MODE=1 and EDIT_MODE2=1
									 SetObjectVisible(build.walls_front2[sites,t],0)
									 ENDIF
									 	
								
							   IF GetPointerPressed() AND EDIT_MODE=2  and EDIT_MODE2=1
								   
								    SETOBJECTIMAGE(build.walls_front2[sites,t],	TILES[ttex],0)
								    setobjectcolor(build.walls_front2[sites,t],250,250,250,255)
								    ENDIF
								
								
																																	
								endif			          
								next 
	
			     
			     
			   
			     
			     for t=1 to 30
						setobjectcolor(build.level2[sites,t],250,250,250,255)		
							if  object_hit=build.level2[sites,t]	
																		
								setobjectcolor(build.level2[sites,t],255,00,00,255)	
								if GetPointerPressed() AND EDIT_MODE=1 and EDIT_MODE2=2
									 SetObjectVisible(build.level2[sites,t],0)
									 ENDIF
									 	
								
							   IF GetPointerPressed() AND EDIT_MODE=2 and EDIT_MODE2=2 
								   
								    SETOBJECTIMAGE(build.level2[sites,t],TILES[ttex],0)
								    setobjectcolor(build.level2[sites,t],250,250,250,255)
								    
								    ENDIF
								
								
																																	
								endif			          
								next 
			     

			     
							endif 
			
             endif
            
	endfunction




function paint_picker()
	

	pointer_x = GetPointerX()		
    pointer_y = GetPointerY()
                
	
      
			// get the x, y and z unit vectors based on the pointer position
			unit_x# = Get3DVectorXFromScreen(pointer_x,pointer_y)        
			unit_y# = Get3DVectorYFromScreen(pointer_x,pointer_y)      
			unit_z# = Get3DVectorZFromScreen(pointer_x,pointer_y)
       
			// calculate the start of the ray cast, which is the unit vector + the camera position
			start_x# = unit_x# + GetCameraX(1)       
			start_y# = unit_y# + GetCameraY(1)      
			start_z# = unit_z# + GetCameraZ(1)      
      
			// calculate the end of the vector, which is the unit vector multiplied by the length of the ray cast and then add the camera position to it
			end_x# = 800*unit_x# + GetCameraX(1)     
			end_y# = 800*unit_y# + GetCameraY(1)       
			end_z# = 800*unit_z# + GetCameraZ(1)
       
			// determine which object has been hit
			object_hit = ObjectrayCast(0,start_x#,start_y#,start_z#,end_x#,end_y#,end_z#)
        
        
       
			if object_hit <> 0
			
			
					for t=1 to 36		
						setobjectcolor(build.walls_lr1[sites,t],250,250,250,255)		
							if  object_hit=build.walls_lr1[sites,t]	
																					
								setobjectcolor(build.walls_lr1[sites,t],255,00,00,255)
								if  GetPointerPressed()
									
									 paint_this=object_hit
									 temp_textures[temps]=CreateRenderImage(GetImageWidth(tiles[ttex]),GetImageHeight(tiles[ttex]),0,0)
										temp_sprite=CreateSprite (tiles[ttex])
										SetRenderToImage(temp_textures[temps],0)	 
										DrawSprite(temp_sprite) 
										SetRenderToScreen()
									    SetSpriteImage(temp_sprite,temp_textures[temps])
									
									 ENDIF
		          endif
								next 
			     
			     
			     
			     
			       for t=1 to 35
					   
						setobjectcolor(build.walls_front1[sites,t],250,250,250,255)
								
							if  object_hit=build.walls_front1[sites,t]														
								setobjectcolor(build.walls_front1[sites,t],255,00,00,255)	
								if GetPointerPressed() 
									
								paint_this=object_hit
								temp_textures[temps]=CreateRenderImage(GetImageWidth(tiles[ttex]),GetImageHeight(tiles[ttex]),0,0)
								
								temp_sprite=CreateSprite (tiles[ttex])
								
								SetRenderToImage(temp_textures[temps],0)	 
									DrawSprite(temp_sprite) 
									SetRenderToScreen()
									SetSpriteImage(temp_sprite,temp_textures[temps])
									 ENDIF
									 	
								
							  
								
								
																																	
								endif			          
								next 
		
			     
			     
			     
			    
			     
			     
			   
			     endif
			     
			   
			    
			     

			     
						
			
          
            
	endfunction
