'#################################################
' Project Name: Editor
' Owner: Fobsi
' Date: 16.03.2019
' Description: Editor for Age of Empires like Game
'#################################################
Strict

Include "../lib/astarlib.bmx"
AppTitle = "Age of Civilizations - Editor"

Const USERCONTROLLED = 1
Const CHASE = 2

Const TILESIZE = 36'108
Global TILESIZEW = 36'108
Global TILESIZEH = TILESIZEW/2
Global MAPWIDTH = 50
Global MAPHEIGHT= 50

' Variablen für Pixel/Tile Koordinaten
Global pixX:Int
Global pixY:Int

'Editor Variabeln
Global tiletoselect:Int = 0
Global activeLayer:Int = 1
Global showgrid = True

'Mapname
Global mapname:String

Global pathmap[][]
Global isomapL1[][]
Global isomapL2[][]

Global autotilemapWasser[][]	
Global autotilemapGrass[][]
Global autotilemapDreck[][]
Global autotilemapDarkGrass[][]		
Global autotilemapPlain[][]

Global tilewertmapWasser[]
Global tilewertmapGrass[]
Global tilewertmapDreck[]
Global tilewertmapDarkGrass[]
Global tilewertmapPlain[]

Global playerChat:String = ""
Global saveorload = 0

Global deleteszenerie = 2
Global tilenumberselector:String = Null
Global autotileingoff = False

Type FPS
	    Global Counter, Time, TFPS
	
	    Function Calc%()
	        Counter:+1
	        If Time < MilliSecs() Then
	            TFPS = Counter' <- Frames/Sec
	            Time = MilliSecs() + 1000'Update
	            Counter = 0
	        End If
	        Return TFPS
	    End Function
EndType

Type Tgame 
	Field cursor:TImage
	Field grid:TImage
	Field redBlock:TImage
	Field wallBlock:TImage
	Field smiley:TImage
	Field chaser:TImage
	Field tiles:TImage
	Field unitsprite:TImage
	
	Field Drawing, Erasing, Started
	Field unit:Tunit
 	Field Astar:Tastar
	
	Global pickedX:Int,pickedY:Int,pickedXTile:Int,pickedYTile:Int
	Global mapX:Float = 540, mapY:Float = 0
	Field mXs:Int
	Field mYs:Int
	
	Field collision = 2
	
	Function Create:tgame()
		Local game:tgame = New Tgame
		If Not GraphicsModeExists(1024,768) RuntimeError "Grafikkarte nicht unterstützt!"	
		Graphics 1024,768 ; SetMaskColor 255,255,255 ;	HideMouse() 
		
		game.cursor = LoadImage("../graphics/red_pointer.bmp",MASKEDIMAGE)
		game.grid = LoadImage("../graphics/grid.bmp",MASKEDIMAGE)		
		'game.redBlock = LoadImage("../graphics/end.bmp") 'target location
		'game.wallBlock = LoadImage("../graphics/wall.bmp") 'wall	
		'game.smiley = LoadAnimImage("../graphics/units/worker/worker.png",72,72,0,60)
		'MidHandleImage game.smiley	
		'game.chaser = LoadAnimImage("../graphics/units/warrior/warrior.png",72,72,0,60)
		game.tiles = LoadAnimImage("../graphics/TILEMAP_LARGE.png",108,108,0,288)
		game.unitsprite = LoadImage("../graphics/unit.bmp",MASKEDIMAGE)
		'tree1 = LoadImage("../graphics/tree1.png")
		'MidHandleImage tree1
		'MidHandleImage game.chaser
		SetBlend(ALPHABLEND)	
		Local tempunit:tunit
		pathmap=pathmap[..mapwidth*3+1]
		For Local z = 0 To mapwidth*3
			pathmap[z]=pathmap[z][..mapHeight*3]
		Next
		isomapL1=isomapL1[..mapwidth+1]
		For Local z = 0 To mapwidth
			isomapL1[z]=isomapL1[z][..mapHeight]
		Next
		isomapL2=isomapL2[..mapwidth*3+1]
		For Local z = 0 To mapwidth*3
			isomapL2[z]=isomapL2[z][..mapHeight*3]
		Next
		
		autotilemapWasser=autotilemapWasser[..mapwidth+1]
		For Local z = 0 To mapwidth
			autotilemapWasser[z]=autotilemapWasser[z][..mapHeight]
		Next
		
		autotilemapGrass=autotilemapGrass[..mapwidth+1]
		For Local z = 0 To mapwidth
			autotilemapGrass[z]=autotilemapGrass[z][..mapHeight]
		Next

		autotilemapDreck=autotilemapDreck[..mapwidth+1]
		For Local z = 0 To mapwidth
			autotilemapDreck[z]=autotilemapDreck[z][..mapHeight]
		Next

		autotilemapDarkGrass=autotilemapDarkGrass[..mapwidth+1]
		For Local z = 0 To mapwidth
			autotilemapDarkGrass[z]=autotilemapDarkGrass[z][..mapHeight]
		Next
		
		autotilemapPlain=autotilemapPlain[..mapwidth+1]
		For Local z = 0 To mapwidth
			autotilemapPlain[z]=autotilemapPlain[z][..mapHeight]
		Next

		tilewertmapWasser=tilewertmapWasser[..256]
		For Local c = 0 To 255
			Select c
				Case 11,15
					tilewertmapWasser[c] = 105
				Case 22,23,150,151
					tilewertmapWasser[c] = 87
				Case 31,63,159,253,30
					tilewertmapWasser[c] = 86
				Case 43
					tilewertmapWasser[c] = 84
				Case 104,105,110,232
					tilewertmapWasser[c] = 90
				Case 107,111,235
					tilewertmapWasser[c] = 89
				Case 127
					tilewertmapWasser[c] = 92
				Case 208,212,240
					tilewertmapWasser[c] = 81
				Case 214,215,246
					tilewertmapWasser[c] = 79
				Case 223
					tilewertmapWasser[c] = 93
				Case 248,249,216,252
					tilewertmapWasser[c] = 82
				Case 251
					tilewertmapWasser[c] = 95
				Case 254
					tilewertmapWasser[c] = 94
				Case 255,247
					tilewertmapWasser[c] = 72
			End Select 
		Next
		
		tilewertmapGrass=tilewertmapGrass[..256]
		For Local c = 0 To 255
			Select c
				Case 11,15
					tilewertmapGrass[c] = 14
				Case 22,23,150,151
					tilewertmapGrass[c] = 8
				Case 31,63,159,253,30
					tilewertmapGrass[c] = 21
				Case 43
					tilewertmapGrass[c] = 20
				Case 104,105,110,232
					tilewertmapGrass[c] = 12
				Case 107,111,235
					tilewertmapGrass[c] = 19
				Case 127
					tilewertmapGrass[c] = 29
				Case 208,212,240
					tilewertmapGrass[c] = 10
				Case 214,215,246
					tilewertmapGrass[c] = 23
				Case 223
					tilewertmapGrass[c] = 31
				Case 248,249,216,252
					tilewertmapGrass[c] = 17
				Case 251
					tilewertmapGrass[c] = 27
				Case 254
					tilewertmapGrass[c] = 33
				Case 255,247
					tilewertmapGrass[c] = 9
			End Select 
		Next

		tilewertmapDreck=tilewertmapDreck[..256]
		For Local c = 0 To 255
			Select c
				Case 11,15
					tilewertmapDreck[c] = 45
				Case 22,23,150,151
					tilewertmapDreck[c] = 46
				Case 31,63,159,253,30
					tilewertmapDreck[c] = 53
				Case 43
					tilewertmapDreck[c] = 45
				Case 104,105,110,232
					tilewertmapDreck[c] = 44
				Case 107,111,235
					tilewertmapDreck[c] = 51
				Case 127
					tilewertmapDreck[c] = 59
				Case 208,212,240
					tilewertmapDreck[c] = 43
				Case 214,215,246
					tilewertmapDreck[c] = 55
				Case 223
					tilewertmapDreck[c] = 61
				Case 248,249,216,252
					tilewertmapDreck[c] = 49
				Case 251
					tilewertmapDreck[c] = 65
				Case 254
					tilewertmapDreck[c] = 63
				Case 255,247
					tilewertmapDreck[c] = 40
				Case 126
					tilewertmapDreck[c] = 47
				Case 219
					tilewertmapDreck[c] = 57
			End Select 
		Next
		
		tilewertmapDarkGrass=tilewertmapDarkGrass[..256]
		For Local c = 0 To 255
			Select c
				Case 11,15
					tilewertmapDarkGrass[c] = 125
				Case 22,23,150,151
					tilewertmapDarkGrass[c] = 126
				Case 31,63,159,253,30
					tilewertmapDarkGrass[c] = 129
				Case 43
					tilewertmapDarkGrass[c] = 125
				Case 104,105,110,232
					tilewertmapDarkGrass[c] = 127
				Case 107,111,235
					tilewertmapDarkGrass[c] = 128
				Case 127
					tilewertmapDarkGrass[c] = 141
				Case 208,212,240
					tilewertmapDarkGrass[c] = 122
				Case 214,215,246
					tilewertmapDarkGrass[c] = 123
				Case 223
					tilewertmapDarkGrass[c] = 140
				Case 248,249,216,252
					tilewertmapDarkGrass[c] = 124
				Case 251
					tilewertmapDarkGrass[c] = 142
				Case 254
					tilewertmapDarkGrass[c] = 143
				Case 255,247
					tilewertmapDarkGrass[c] = 120
				Case 126
					tilewertmapDarkGrass[c] = 47
				Case 219
					tilewertmapDarkGrass[c] = 57
			End Select 
		Next
		
		tilewertmapPlain=tilewertmapPlain[..256]
		For Local c = 0 To 255
			Select c
				Case 11,15
					tilewertmapPlain[c] = 165
				Case 22,23,150,151
					tilewertmapPlain[c] = 166
				Case 31,63,159,253,30
					tilewertmapPlain[c] = 176
				Case 43
					tilewertmapPlain[c] = 186
				Case 104,105,110,232
					tilewertmapPlain[c] = 184
				Case 107,111,235
					tilewertmapPlain[c] = 175
				Case 127
					tilewertmapPlain[c] = 168
				Case 208,212,240
					tilewertmapPlain[c] = 182
				Case 214,215,246
					tilewertmapPlain[c] = 173
				Case 223
					tilewertmapPlain[c] = 169
				Case 248,249,216,252
					tilewertmapPlain[c] = 174
				Case 251
					tilewertmapPlain[c] = 171
				Case 254
					tilewertmapPlain[c] = 170
				Case 255,247
					tilewertmapPlain[c] = 160
				Case 126
					tilewertmapPlain[c] = 47
				Case 219
					tilewertmapPlain[c] = 57
			End Select 
		Next


		For Local x:Int = 0 To mapwidth-1
			For Local y:Int = 0 To mapheight-1
				isomapL1[x][y] = Rand(0,4)
				
			Next
		Next
		For Local x:Int = 0 To mapwidth*3-1
			For Local y:Int = 0 To mapheight*3-1
				isomapL2[x][y] = 287
			Next
		Next

		Return game
	End Function
	
	Field endprogramm = False
	
	Method RunProgram()'Main program loop	
		While Not endprogramm 'And Not Confirm("Beenden?")
			UserInput()
			'MoveUnits()
			RenderScreen()
			If AppTerminate() Or KeyHit(KEY_ESCAPE)=True Then
				If Confirm("Wirklich Beenden?") Then endprogramm =True 
			EndIf 
		Wend
	End Method
	Method UserInput()'This Function handles most user mouse And keyboard Input
		If Started = False	'If in map edit mode
			If KeyHit(KEY_1)=True Then 
				If activeLayer = 2 Then activeLayer = 1
			EndIf 
			If KeyHit(KEY_2)=True Then 
				If activeLayer = 1 Then activeLayer = 2
			EndIf
			If KeyHit(KEY_F3)=True Then
				FlushKeys() 
				If mapname = "" Then 
					playerChat = "map_"
					saveorload = 1
				Else
					playerChat = mapname
					saveorload = 1
				EndIf 
			EndIf 
			
			If KeyHit(KEY_F4)=True Then
				FlushKeys() 
				If mapname = "" Then 
					playerChat = "map_"
					saveorload = 2
				Else
					playerChat = mapname
					saveorload = 2
				EndIf 
			EndIf 

			
			If KeyHit(KEY_G)=True Then 
				If showgrid = True Then 
					showgrid = False
				Else
					showgrid = True 
				EndIf
			EndIf
			
			If KeyHit(KEY_S)=True Then
				If deleteszenerie < 2 Then
					deleteszenerie:+1
				Else
					deleteszenerie = 0
				EndIf
			EndIf
			
			If KeyHit(KEY_A)=True Then 
				If autotileingoff = False Then 
					autotileingoff =True 
				Else
					autotileingoff =False
				EndIf 
			EndIf
			
			
				Local c=GetChar()
				If c = 49 Or c = 50 Or c = 51 Or c = 52 Or c = 53 Or c= 54 Or c = 55 Or c = 56 Or c = 57 Or c = 48 Then 
				Select c
					Case 49,50,51,52,53,54,55,56,57,48
						tilenumberselector:+Chr(c)
				End Select
				If tilenumberselector.length <= 3 Then 
					tiletoselect = tilenumberselector.ToInt()
				Else
					tilenumberselector = Null
					tilenumberselector:+Chr(c)
				EndIf
				Else
					tiletoselect:+ MouseZSpeed()
				EndIf  
 
			'tiletoselect :
			
			If playerChat <> "" Then 
				Local c=GetChar()
				Select c
					Case 8
						If playerChat playerChat=playerChat[..playerChat.length-1]
					Case 13
						If playerChat <> "" Then
							If saveorload = 1 Then 
						 		SaveMap("maps",playerChat)
							ElseIf saveorload = 2 Then 
								LoadMap("maps",playerChat)
							EndIf 
						EndIf 
					Default
						If c>31 And c<127 playerChat:+Chr(c)
				End Select
			EndIf
							
			If (Not MouseDown(1)) Then Drawing = False; Erasing = False
			If MouseHit(1) Or MouseDown(1) Then							'Edit map by drawing Or erasing walls
				If pickedX >= 0 And pickedY >= 0 And pickedX  <= mapwidth*3-1 And pickedY <= mapheight*3-1 Then
					
					
					'If activeLayer = 1 And collision = False Then IsomapL1[pickedXTile][pickedYTile] = tiletoselect
					'If activeLayer = 2 And collision = False Then IsomapL2[pickedXTile][pickedYTile] = tiletoselect
					'192 197
					If collision = 2 Then 
						Select tiletoselect
							Case 72
							If autotileingoff = False Then 
								If pickedXTile>= 1 And pickedYTile>= 1 And pickedXTile<= mapwidth-1 And pickedYTile<= mapheight-1 Then
									autotilemapWasser[pickedXTile][pickedYTile] = 15
									autotilemapWasser[pickedXTile-1][pickedYTile] = 13
									autotilemapWasser[pickedXTile+1][pickedYTile] = 11
									autotilemapWasser[pickedXTile][pickedYTile-1] = 14
									autotilemapWasser[pickedXTile][pickedYTile+1] = 7
									autotilemapWasser[pickedXTile-1][pickedYTile-1] = 12
									autotilemapWasser[pickedXTile+1][pickedYTile+1] = 3
									autotilemapWasser[pickedXTile-1][pickedYTile+1] = 5
									autotilemapWasser[pickedXTile+1][pickedYTile-1] = 10
								
									For Local x = pickedXTile-1 To pickedXTile+1
										For Local y = pickedYTile-1 To pickedYTile+1
											autotilemapWasser[x][y]=TileIndex(autotilemapWasser,x,y)
											Local atw = autotilemapWasser[x][y]
											Local tw = tilewertmapWasser[atw]
											Select atw
												Case 11,15
													For Local ax = x*3 To x*3+1
														For Local ay = y*3 To y*3+1
															pathmap[ax][ay]=unwalkable
														Next
													Next
												Case 22,23,150,151
													For Local ax = x*3+1 To x*3+2
														For Local ay = y*3 To y*3+1
															pathmap[ax][ay]=unwalkable
														Next
													Next
												Case 31,63,159,253,30
													For Local ax = x*3 To x*3+2
														For Local ay = y*3 To y*3+1
															pathmap[ax][ay]=unwalkable
														Next
													Next
												Case 43
													For Local ax = x*3 To x*3+1
														For Local ay = y*3 To y*3+1
															pathmap[ax][ay]=unwalkable
														Next
													Next
												Case 104,105,110,232
													For Local ax = x*3 To x*3+1
														For Local ay = y*3+1 To y*3+2
															pathmap[ax][ay]=unwalkable
														Next
													Next
												Case 107,111,235
													For Local ax = x*3 To x*3+1
														For Local ay = y*3 To y*3+2
															pathmap[ax][ay]=unwalkable
														Next
													Next
												Case 127
													For Local ax = x*3 To x*3+2
														For Local ay = y*3 To y*3+2
															pathmap[ax][ay]=unwalkable
															If ax = x*3+2 And ay = y*3+2 Then pathmap[ax][ay]=walkable 
														Next
													Next
												Case 208,212,240
													For Local ax = x*3+1 To x*3+2
														For Local ay = y*3+1 To y*3+2
															pathmap[ax][ay]=unwalkable
														Next
													Next
												Case 214,215,246
													For Local ax = x*3+1 To x*3+2
														For Local ay = y*3 To y*3+2
															pathmap[ax][ay]=unwalkable
														Next
													Next
												Case 223
													For Local ax = x*3 To x*3+2
														For Local ay = y*3 To y*3+2
															pathmap[ax][ay]=unwalkable
															If ax = x*3 And ay = y*3+2 Then pathmap[ax][ay]=walkable 
														Next
													Next
												Case 248,249,216,252
													For Local ax = x*3 To x*3+2
														For Local ay = y*3+1 To y*3+2
															pathmap[ax][ay]=unwalkable
														Next
													Next
												Case 251
													For Local ax = x*3 To x*3+2
														For Local ay = y*3 To y*3+2
															pathmap[ax][ay]=unwalkable
															If ax = x*3+2 And ay = y*3 Then pathmap[ax][ay]=walkable 
														Next
													Next
												Case 254
													For Local ax = x*3 To x*3+2
														For Local ay = y*3 To y*3+2
															pathmap[ax][ay]=unwalkable
															If ax = x*3 And ay = y*3 Then pathmap[ax][ay]=walkable 
														Next
													Next
	
												Case 255,247
													For Local ax = x*3 To x*3+2
														For Local ay = y*3 To y*3+2
															pathmap[ax][ay]=unwalkable
														Next
													Next
											End Select 
	
											IsomapL1[x][y]=tw
											'pathmap[x][y]=unwalkable
										Next
									Next
								EndIf 
							Else
								IsomapL1[pickedxtile][pickedytile] = tiletoselect
							EndIf 
							Case 9
							If autotileingoff = False Then 
								If pickedXTile>= 1 And pickedYTile>= 1 And pickedXTile<= mapwidth-1 And pickedYTile<= mapheight-1 Then
									autotilemapGrass[pickedXTile][pickedYTile] = 15
									autotilemapGrass[pickedXTile-1][pickedYTile] = 13
									autotilemapGrass[pickedXTile+1][pickedYTile] = 11
									autotilemapGrass[pickedXTile][pickedYTile-1] = 14
									autotilemapGrass[pickedXTile][pickedYTile+1] = 7
									autotilemapGrass[pickedXTile-1][pickedYTile-1] = 12
									autotilemapGrass[pickedXTile+1][pickedYTile+1] = 3
									autotilemapGrass[pickedXTile-1][pickedYTile+1] = 5
									autotilemapGrass[pickedXTile+1][pickedYTile-1] = 10
								
									For Local x = pickedXTile-1 To pickedXTile+1
										For Local y = pickedYTile-1 To pickedYTile+1
											autotilemapGrass[x][y]=TileIndex(autotilemapGrass,x,y)
											IsomapL1[x][y]=tilewertmapGrass[autotilemapGrass[x][y]]
											'pathmap[x][y]=unwalkable
										Next
									Next
								EndIf 
							Else
								IsomapL1[pickedxtile][pickedytile] = tiletoselect
							EndIf
							Case 40
							If autotileingoff = False Then 
								If pickedXTile>= 1 And pickedYTile>= 1 And pickedXTile<= mapwidth-1 And pickedYTile<= mapheight-1 Then
									autotilemapDreck[pickedXTile][pickedYTile] = 15
									autotilemapDreck[pickedXTile-1][pickedYTile] = 13
									autotilemapDreck[pickedXTile+1][pickedYTile] = 11
									autotilemapDreck[pickedXTile][pickedYTile-1] = 14
									autotilemapDreck[pickedXTile][pickedYTile+1] = 7
									autotilemapDreck[pickedXTile-1][pickedYTile-1] = 12
									autotilemapDreck[pickedXTile+1][pickedYTile+1] = 3
									autotilemapDreck[pickedXTile-1][pickedYTile+1] = 5
									autotilemapDreck[pickedXTile+1][pickedYTile-1] = 10
								
									For Local x = pickedXTile-1 To pickedXTile+1
										For Local y = pickedYTile-1 To pickedYTile+1
											autotilemapDreck[x][y]=TileIndex(autotilemapDreck,x,y)
											IsomapL1[x][y]=tilewertmapDreck[autotilemapDreck[x][y]]
											'pathmap[x][y]=unwalkable
										Next
									Next
								EndIf 
							Else
								IsomapL1[pickedxtile][pickedytile] = tiletoselect
							EndIf
							Case 120
							If autotileingoff = False Then 
								If pickedXTile>= 1 And pickedYTile>= 1 And pickedXTile<= mapwidth-1 And pickedYTile<= mapheight-1 Then
									autotilemapDarkGrass[pickedXTile][pickedYTile] = 15
									autotilemapDarkGrass[pickedXTile-1][pickedYTile] = 13
									autotilemapDarkGrass[pickedXTile+1][pickedYTile] = 11
									autotilemapDarkGrass[pickedXTile][pickedYTile-1] = 14
									autotilemapDarkGrass[pickedXTile][pickedYTile+1] = 7
									autotilemapDarkGrass[pickedXTile-1][pickedYTile-1] = 12
									autotilemapDarkGrass[pickedXTile+1][pickedYTile+1] = 3
									autotilemapDarkGrass[pickedXTile-1][pickedYTile+1] = 5
									autotilemapDarkGrass[pickedXTile+1][pickedYTile-1] = 10
								
									For Local x = pickedXTile-1 To pickedXTile+1
										For Local y = pickedYTile-1 To pickedYTile+1
											autotilemapDarkGrass[x][y]=TileIndex(autotilemapDarkGrass,x,y)
											IsomapL1[x][y]=tilewertmapDarkGrass[autotilemapDarkGrass[x][y]]
											'pathmap[x][y]=unwalkable
										Next
									Next
								EndIf 
							Else
								IsomapL1[pickedxtile][pickedytile] = tiletoselect
							EndIf
							Case 160
							If autotileingoff = False Then 
								If pickedXTile>= 1 And pickedYTile>= 1 And pickedXTile<= mapwidth-1 And pickedYTile<= mapheight-1 Then
									autotilemapPlain[pickedXTile][pickedYTile] = 15
									autotilemapPlain[pickedXTile-1][pickedYTile] = 13
									autotilemapPlain[pickedXTile+1][pickedYTile] = 11
									autotilemapPlain[pickedXTile][pickedYTile-1] = 14
									autotilemapPlain[pickedXTile][pickedYTile+1] = 7
									autotilemapPlain[pickedXTile-1][pickedYTile-1] = 12
									autotilemapPlain[pickedXTile+1][pickedYTile+1] = 3
									autotilemapPlain[pickedXTile-1][pickedYTile+1] = 5
									autotilemapPlain[pickedXTile+1][pickedYTile-1] = 10
								
									For Local x = pickedXTile-1 To pickedXTile+1
										For Local y = pickedYTile-1 To pickedYTile+1
											autotilemapPlain[x][y]=TileIndex(autotilemapPlain,x,y)
											IsomapL1[x][y]=tilewertmapPlain[autotilemapPlain[x][y]]
											'pathmap[x][y]=unwalkable
										Next
									Next
								EndIf 
							Else
								IsomapL1[pickedxtile][pickedytile] = tiletoselect
							EndIf
							Case 192,193,194,195,196,197
								'Local r:tunit = New tUnit
								'Print r.typ					
								'r.xLoc =((pickedX*tilesize)/tilesize)*tilesize -3.5*tilesize
								'r.yLoc =((pickedY*tilesize)/tilesize)*tilesize -2*tilesize

								If deleteszenerie = 1 Then
								 	IsomapL2[pickedX][pickedY] = Null
								ElseIf deleteszenerie = 0 Then 
									IsomapL2[pickedX][pickedY] = 192
								EndIf
								'r.typ = "szenerie"
								'r.pathAI = Null 'userControlled
								'r.sprite = tree1
												
								'UnitList.addlast(r)
							Default 
								IsomapL1[pickedXtile][pickedYtile] = tiletoselect
						End Select
					EndIf 					
					If collision = 0 Then 
						'Print pickedX+" "+pickedY
						pathmap[pickedX][pickedY] = unwalkable
					ElseIf collision = 1 Then 
						pathmap[pickedX][pickedY] = walkable
					EndIf  
					 	
				EndIf 
			End If
		Else	'If in game/pathfinding mode Then reenter map edit mode by pressing enter/Return key. This stops the units.
		End If
	End Method
	Method RenderScreen() 'This Function draws stuff on the screen.
	
		'Tile Auswahl 1*1
		pickedX = TileX(MouseX(), MouseY(), tilesizeW, tilesizeH, mapX, mapY)
		pickedY = TileY(MouseX(), MouseY(), tilesizeW, tilesizeH, mapX, mapY)
		
		'Tile Auswahl 3*3
		pickedXTile = TileX(MouseX(), MouseY(), tilesizeW*3, tilesizeH*3, mapX, mapY)
		pickedYTile = TileY(MouseX(), MouseY(), tilesizeW*3, tilesizeH*3, mapX, mapY)
		
		'Mausrad
		
		mXs = MouseXSpeed()
		mYs = MouseYSpeed()	
		
		'Runtime Error protektion
		If tiletoselect < 0 Then tiletoselect = 287
		If tiletoselect > 287 Then tiletoselect = 0
		
		'Toggeln der Kollision
		If KeyHit(KEY_K)=True Then
			deleteszenerie = 2 
			If collision < 2 Then 
				collision:+1
			Else
				collision = 0
			EndIf 
		EndIf
		
		' Mousegeschwindigkeit aktualisieren
		If MouseDown(2)
			MapX = mapX + mXs
			MapY = mapY + mYs
		End If
		
		Cls	
		
		DrawMaps()
		DrawTextInfos()
		
		DrawImage cursor,MouseX(),MouseY() 'Draw the mouse

		Flip
	End Method
	Method DrawTextinfos()
	
		If KeyDown(KEY_F1) Then 
			SetColor 0,0,0
			DrawRect 49,50,240,103
			SetColor 255,128,0
			DrawBox(49,50,240,103)
			DrawText "Keybinds:",50,50 
			DrawText "F1 - Info",50,61 
			DrawText "F3 - Map speichern",50,72 
			DrawText "F4 - Map laden",50,83 
			DrawText " G - Grid anzeigen/verstecken",50,94
			DrawText " K - Kollision toggeln",50,105
			DrawText " S - Szenerie toggeln",50,116
			DrawText " A - Autotile toggeln",50,127
			DrawText "1-9- Tile auswählen per ID",50,138
		EndIf 
		
		SetColor 0,255,0
		DrawText "Mapname:"+mapname,5,5
		SetColor 255,255,255
		'Print "T:"+PlayerChat
		
		If playerChat <> "" Then 
			Local txt:String
			If saveorload = 1 Then 
				txt = "Speichern -> Dateiname:"
			ElseIf saveorload = 2 Then 
				txt = "Laden -> Dateiname:"
			EndIf 
			SetColor 0,0,0
			DrawRect 8,578,TextWidth(txt+playerChat),16
			SetColor 255,255,0
			DrawText txt+playerChat,8,578
			SetColor 0,200,0
			DrawRect TextWidth(txt+playerChat)+8,578,8,16
			SetColor 255,255,255
		EndIf 
	
		'mapX / mapY
		'DrawText "mapX:"+mapX+"|mapY:"+mapY,0,0
	
		' Die "gepickten" Tilekoordinaten neben dem Mauszeiger ausgeben
		SetColor 255,0,0
		DrawText "X:"+ pickedX + "|Y:" + pickedY + "|TX:" + pickedXTile  + "|TY:" + pickedYTile , MouseX()+8, MouseY()-8
		SetColor 255,255,255
		
		'Editor Tile ID und Anzeige des Aktuell ausgewählten Tiles
		SetColor 0,0,0
		DrawRect 914,36,110,110
		If tiletoselect = 9 Or tiletoselect = 40 Or tiletoselect = 72 Or tiletoselect = 120 Or tiletoselect = 160 Then
			If autotileingoff = False Then SetColor 0,255,0 Else SetColor 255,0,0
			DrawText "Autotile",915,37
			If tiletoselect = 72 Then DrawText "AutoCollision",915,49
		EndIf 
		SetColor 255,255,255
		DrawImage(tiles, 915,37 ,tiletoselect)
		'Print collision
		If collision = 0 Then 
			SetColor 0,255,0 
		ElseIf collision = 1
			SetColor 255,128,0
		ElseIf collision = 2 
			SetColor 255,0,0
		EndIf 
		DrawRect 991,2,32,32
		SetColor 255,0,0
		DrawText "Tile ID:"+tiletoselect,915,146
		SetColor 255,255,255
		
		' Kollision und Grid
		SetColor 0,0,0
		DrawText "K",1002,12
		If showgrid = True Then SetColor 0,255,0 Else SetColor 255,0,0
		DrawRect 957,2,32,32
		SetColor 0,0,0
		DrawText "G",968,12
		
		'Layer2 löschen
		If deleteszenerie = 0 Then 
			SetColor 0,255,0 
		ElseIf deleteszenerie = 1 Then 
			SetColor 255,128,0
		ElseIf deleteszenerie = 2 Then 
			SetColor 255,0,0
		EndIf
		DrawRect 923,2,32,32
		SetColor 0,0,0
		DrawText "Sz",930,12

		
		Rem
		' Aktiver Layer 1
		If activeLayer = 1 Then SetColor 0,255,0 Else SetColor 255,0,0
		DrawRect 905,2,50,15
		SetColor 0,0,0
		DrawText "Layer1",906,3
		
		' Aktiver Layer 2
		If activeLayer = 2 Then SetColor 0,255,0 Else SetColor 255,0,0
		DrawRect 905,18,50,15
		SetColor 0,0,0
		DrawText "Layer2",906,18
		EndRem
		' RAM
		If KeyDown(KEY_F1)=True Then 
			SetColor 0,128,0
			Local txt$="MemAllocd:"+GCMemAlloced()/1000+"mb"
			DrawText txt,1024-TextWidth(txt),768-TextHeight(txt)
			'FPS
			Local nfps = FPS.Calc()
			If nfps > 50 Then 
				SetColor 0,128,0
			Else
				SetColor 255,140,0
			EndIf 
				Local fpst$ = "FPS: "+nfps
				DrawText fpst ,1024-TextWidth(fpst ),748-TextHeight(fpst)	
 		EndIf
			SetColor 255,255,255
	End Method
	Method DrawMaps()
		'Iso Map Layer 1
		For Local y = 0 To mapwidth-1 'Draw the walls And the grid 
			For Local x = 0 To mapheight-1
				pixX = PixelX(x, y, tilesizeW*3)
				pixY = PixelY(x, y, tilesizeH*3)
						
				DrawImage(tiles, pixX-(tilesizeW*3)/2 + mapX, pixY + mapY-(tilesizeH*3),isomapL1[x][y])
				'If pathmap[x][y] = unwalkable Then 
				'	DrawImage(tiles, pixX-tilesizeW/2 + mapX, pixY + mapY-(tilesizeH),15)
				'EndIf 
			Next
		Next
		
		For Local y = 0 To mapwidth*3-1 'Draw the walls And the grid 
			For Local x = 0 To mapheight*3-1
				pixX = PixelX(x, y, tilesizeW)
				pixY = PixelY(x, y, tilesizeH)
				Select isomapL2[x][y]
				Case 192
					DrawImage(tiles,(pixX-tilesizeH) + mapX-36, (pixY-(tilesizeH*3)) + mapY-18 ,isomapL2[x][y])
				EndSelect
				
				'If pathmap[x][y] = unwalkable Then 
				'	DrawImage(tiles, pixX-tilesizeW/2 + mapX, pixY + mapY-(tilesizeH),15)
				'EndIf 
			Next
		Next

		
		'Einheiten
		'Local test:Int 
		'test = 0
		'For Local r:tunit = EachIn unitList
			'test = test + 1			
			'Local px = ((r.xLoc/(tilesize) - r.yLoc/(tilesize)) * (tilesizeW/2) ) + mapX
			'Local py = ((r.xLoc/(tilesize) + r.yLoc/(tilesize)) * (tilesizeH/2) ) + mapY -tilesizeH/2
			
		'	DrawText "px:"+ px+" py:"+py,500,500+test

			'DrawImage r.sprite,px,py,r.richtung
			
			'Print test
		'Next
		
		'Iso Map + Pathmap
		'For Local y = 0 To mapwidth-1  
			'For Local x = 0 To mapheight-1
				'pixX = PixelX(x, y, tilesizeW)
				'pixY = PixelY(x, y, tilesizeH)
						
				'DrawImage(tiles, pixX-tilesizeW/2 + mapX, pixY + mapY-(tilesizeH),isomapL2[x][y])
				'If pathmap[x][y] = unwalkable Then 
				'	DrawImage(tiles, pixX-tilesizeW/2 + mapX, pixY + mapY-(tilesizeH),15)
				'EndIf 
			'Next
		'Next
		
		'Pathmap - Kollisionmap
		For Local y = 0 To (mapwidth*3)-1  
			For Local x = 0 To (mapheight*3)-1
				pixX = PixelX(x, y, tilesizeW)
				pixY = PixelY(x, y, tilesizeH)
				SetAlpha .25
				If showgrid =True Then DrawImage(tiles, (pixX-tilesizeH) + mapX, (pixY-(tilesizeH*3)) + mapY,25)
				If pathmap[x][y] = unwalkable And showgrid =True Then 
					SetAlpha .75
					SetColor 255,0,0
					DrawImage(tiles, (pixX-tilesizeH) + mapX, (pixY-(tilesizeH*3)) + mapY,25)
					SetColor 255,255,255
				EndIf
				SetAlpha 1 
			Next
		Next

		'Minimap
		'For Local x = 0 To mapwidth*3-1 'Draw the walls And the grid 
		'	For Local y = 0 To mapheight*3-1
		'		DrawImage grid,x*3,y*3
		'		If pathmap[x][y] = unwalkable Then DrawImage wallBlock,x*3,y*3
		'		
		'	Next
		'Next
		
		'Units on Minimap
		'For Local r:tunit = EachIn unitList
		'	DrawImage unitsprite,((r.xLoc/tilesizeW)*3)-1,((r.yLoc/(tilesizeH*2))*3)-1
		'Next
		
		'Autotile MAP 
		'For Local y = 0 To mapwidth-1 'Draw the walls And the grid 
		'	For Local x = 0 To mapheight-1
		'		pixX = PixelX(x+1, y+1, tilesizeW*3)
		'		pixY = PixelY(x+1, y+1, tilesizeH*3)
		'		DrawText autotilemapWasser[x][y],pixX-tilesizeW/2 + mapX+10,pixY + mapY-(tilesizeH)	
		'	Next
		'Next
		
		'Tile Annzeige
		Local bigtile = False
		Local smalltile = False
		
		If collision = 2 Or deleteszenerie = 2 Then 
			bigtile = True
		EndIf 
		
		If collision = 1 Or deleteszenerie = 1 Or collision = 0 Or deleteszenerie =0  Then 
			bigtile = False
			smalltile = True
		EndIf 
		
		
		If bigtile = True Then 
			DrawImage(tiles, PixelX(pickedXTile, pickedYTile, tilesizeW*3)-tilesizeW*3/2 + mapX, PixelY(pickedXTile, pickedYTile, tilesizeH*3) + mapY-(tilesizeH*3),15)
		EndIf 
		If smalltile = True Then 
			SetColor 255,0,0
			DrawImage(tiles, PixelX(pickedX, pickedY, tilesizeW)-tilesizeW/2 + mapX, PixelY(pickedX, pickedY, tilesizeH) + mapY-(tilesizeH*3),25)
 			SetColor 255,255,255
		EndIf 
		'If collision = 1 Or collision = 0 And deleteszenerie = 1 Then 
			'SetColor 255,0,0
			'DrawImage(tiles, PixelX(pickedX, pickedY, tilesizeW)-tilesizeW/2 + mapX, PixelY(pickedX, pickedY, tilesizeH) + mapY-(tilesizeH*3),25)
 			'SetColor 255,255,255
		'EndIf 
		
	End Method
End Type


SeedRnd MilliSecs()

Local game:tgame = Tgame.Create()
	game.RunProgram()
End 

' Funktionen für das Umrechnen von normalen Pixel-Koordinaten in Tile-Koordinaten und umgekehrt

Function PixelX:Float(tileX:Int, tileY:Int, tileW:Int)
	Return ( tileX * (tileW/2) ) - ( tileY * (tileW/2) )
End Function

Function PixelY:Float(tileX:Int, tileY:Int, tileH:Int)
	Return ( tileX * (tileH/2) ) + ( tileY * (tileH/2) )
End Function

Function TileX:Int(PixelX:Float, PixelY:Float, tileW:Int, tileH:Int, offsetX:Float = 0, offsetY:Float = 0)
	pixelX = pixelX - offsetX
	pixelY = pixelY - offsetY
	Return ( pixelX / (tileW/2) + pixelY / (tileH/2) ) / 2
End Function

Function TileY:Int(PixelX:Float, PixelY:Float, tileW:Int, tileH:Int, offsetX:Float = 0, offsetY:Float = 0)
	pixelX = pixelX - offsetX
	pixelY = pixelY - offsetY
	Return ( pixelY / (tileH/2) - pixelX / (tileW/2) ) / 2
End Function

Function TileIndex:Int(map[][],x:Int,y:Int)
		Local le=0,ri=0 ,up=0 ,do=0 ,leup=0,ledo=0,riup=0,rido=0
		'Print "Check: "+x+""+y
		If x >= 1 And y >= 1 And x <= (mapwidth-2) And y <= (mapheight-2) Then 
			
			If map[x-1][y] > 0 Then  le = 1
			If map[x+1][y] > 0 Then  ri = 1
			If map[x][y-1] > 0 Then  up = 1
			If map[x][y+1] > 0 Then  do = 1
			If map[x-1][y-1] > 0 Then  leup = 1
			If map[x-1][y+1] > 0 Then  ledo = 1
			If map[x+1][y-1] > 0 Then  riup = 1
			If map[x+1][y+1] > 0 Then  rido = 1
			
			Local te = (leup + up*2 + riup*4 + le*8 + ri*16 + ledo*32 + do*64 + rido*128) 
			'Print te
			Return te
		EndIf 
		Return 			
End Function 

' Map Arrays Laden
Function LoadMap:Int(ordner:String,name:String)
	If Confirm("Mapname: "+ordner+"/"+name+".map Laden?")=True Then 
		Local file:TStream
		Local existent = FileType("../"+ordner+"/"+name+".map")
		If existent = True Then 
			file = OpenFile("../"+ordner+"/"+name+".map")
			mapwidth = ReadShort(file)
			mapheight = ReadShort(file)
			ClearAutoMap()
			For Local x:Int = 0 To mapwidth-1
				For Local y:Int = 0 To mapheight-1
					isomapL1[x][y] = ReadInt(file)
				Next
			Next
			
			For Local x:Int = 0 To mapwidth*3-1
				For Local y:Int = 0 To mapheight*3-1
					pathmap[x][y] = ReadInt(file)
				Next
			Next

			For Local x:Int = 0 To mapwidth*3-1
				For Local y:Int = 0 To mapheight*3-1
					isomapL2[x][y] = ReadInt(file)
					'Print isomapL2[x][y]
				Next
			Next
			
			'For Local x:Int = 0 To mapwidth*3-1
			'	For Local y:Int = 0 To mapheight*3-1
			'		pathmap[x][y] = ReadShort(file)
			'	Next
			'Next
			mapname = playerChat
			playerChat = ""
			saveorload = 0
			CloseFile(file)
		EndIf 
	EndIf 
	Return True 
End Function 

'Map Arrays Speichern
Function SaveMap:Int(ordner:String,name:String)
	If Confirm("Mapname: "+ordner+"/"+name+".map Speichern?")=True Then 
		Local file:TStream
		file = WriteFile("../"+ordner+"/"+name+".map")
		If FileType("../"+ordner+"/"+name+".map") Then 
			WriteShort file,mapwidth
			WriteShort file,mapheight
			
			For Local x:Int = 0 To mapwidth-1
				For Local y:Int = 0 To mapheight-1
					WriteInt file,isomapL1[x][y]
				Next
			Next
			
			For Local x:Int = 0 To mapwidth*3-1
				For Local y:Int = 0 To mapheight*3-1
					WriteInt file,pathmap[x][y]
				Next
			Next
			
			For Local x:Int = 0 To mapwidth*3-1
				For Local y:Int = 0 To mapheight*3-1
				 	If isomapL2[x][y] <> 287 Then Print x+"|"+y+"|"+ isomapL2[x][y] 
					WriteInt file,isomapL2[x][y] 
				Next
			Next

			'For Local x:Int = 0 To mapwidth*3-1
			'	For Local y:Int = 0 To mapheight*3-1
			'		WriteShort file,pathmap[x][y]
			'	Next
			'Next
			
						
		
		CloseFile(file)
		mapname = playerChat
		playerChat = ""
		saveorload = 0
		EndIf 
	EndIf 
	Return True 
End Function 
Function ClearAutoMap()
	For Local x:Int = 0 To mapwidth-1
		For Local y:Int = 0 To mapheight-1
			autotilemapWasser[x][y]=0
			autotilemapGrass[x][y]=0
			autotilemapDreck[x][y]=0
			autotilemapDarkGrass[x][y]=0		
			autotilemapPlain[x][y]=0

		Next
	Next
End Function
Function DrawBox(a,b,c,d)
  DrawLine a,b,a+c-1,b
  DrawLine a+c-1,b,a+c-1,b+d-1
  DrawLine a+c-1,b+d-1,a,b+d-1
  DrawLine a,b+d-1,a,b
End Function
Function RectsOverlap:Int(x0, y0, w0, h0, x2, y2, w2, h2)
    If x0 > (x2 + w2) Or (x0 + w0) < x2 Then Return False
    If y0 > (y2 + h2) Or (y0 + h0) < y2 Then Return False
    Return True
End Function

