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

?Win32
Framework BRL.D3D7Max2D
?MacOS
Framework BRL.GLMax2D
?

Import BRL.GNet
Import BRL.BASIC
Import BRL.PNGLoader

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

'Netcode
Const GAMEPORT=8888

Global NextUnitID = 0
Global MultiplayerPlayerID = 1
Global OldMultiplayerPlayerID = MultiplayerPlayerID 
Global PlayerColorID = MultiplayerPlayerID 

'Fonts
'Local font:TimageFont=LoadImageFont("graphics\fonts\arial.ttf",12)
'SetImageFont(font)
'SetImageFont(LoadImageFont(getenv_("windir") + "/fonts/" + "Arial.ttf", 22))

Global GWIDTH=1024
Global GHEIGHT=768
Global GDEPTH=0'32
Global GHERTZ=60

Const USERCONTROLLED = 1
Const CHASE = 2

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

Global mapX:Float = 540, mapY:Float = 0

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

'Map verschiebung und Fenster berechung
Global verschMapX
Global verschMapY

'Editor Variabeln
Global tiletoselect:Int = 72
Global activeLayer:Int = 1

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

Global buildmap[][]
Global buildidmap[][]

Global zmap[][]

Global unitList:TList
Global pickedX:Int,pickedY:Int,pickedXTile:Int,pickedYTile:Int,pickedpixelX,pickedpixelY 

Global menuid = 0
Global nextbuilding = 0

'Chat
Global lastChat$[6]
Global ChatList:TList = CreateList()

Global nextbuildingID = 0
 

Const SLOT_TYPE=0
Const SLOT_NAME=1
Const SLOT_CHAT=2		
Const SlOT_SCORE=3
Const SLOT_X=4
Const SLOT_Y=5
Const SLOT_VX=6
Const SLOT_VY=7
Const SLOT_RICHTUNG=8
Const SLOT_TIMEOUT=9
Const SLOT_HIT=10	
Const SLOT_ID=11	
Const SLOT_ANIM=12
Const SLOT_Z=13
Const SLOT_PLAYERID=14
Const SLOT_PLAYERCOLOR=15
Const SLOT_BUILDX=16
Const SLOT_BUILDY=17
Const SLOT_MODE=18

Const SLOT_HP = 19
Const SLOT_MAXHP = 20
Const SLOT_DMG = 21
Const SLOT_TARGETPID = 22
Const SLOT_TARGETID = 23
Const SLOT_LASTTILEX = 24
Const SLOT_LASTTILEY = 25

Const SLOT_EVENT = 26
Const SLOT_EVENTTIME = 27
Const SLOT_EVENTTYPE = 28

Type Tbotton
	Field menucontainer
	Field x,y,w,h
	Field hit,down
	Method OverBotton()
		If RectsOverLap(x,y,w,h,MouseX(),MouseY(),1,1) And menuid = menucontainer Then 
			Return True 
		Else
			Return False
		EndIf
	End Method 
	
	Method BottonHit()
		If OverBotton()=True And MouseHit(1)=True And menuid = menucontainer Then 
			Return True
		Else
			Return False
		EndIf  
	End Method
	
	Method BottonDown()
		If OverBotton()=True And MouseDown(1)=True And menuid = menucontainer Then 
			Return True
		Else
			Return False
		EndIf
	End Method  
End Type 

Type TChatMessage
	Field text$,PlayerName$,PlayerColor
	Field sendTime
End Type

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 workersprite:TImage
	Field warriorsprite:TImage
	Field tiles:TImage
	Field ui:TImage
	Field uiover:TImage
	Field slotsprite:TImage
	Field slotbuild:TImage
	Field unitspriteminimap:TImage
	Field tilesminimap:TImage
	Field buildingsprite:TImage
	Field buildingssprite:TImage
	Field Drawing, Erasing, Started = True
	Field unit:Tunit
 	Field Astar:Tastar
	Global buildingList:TList
	Field chatbackground:TImage
	
	'netcode
	Field host:TGNetHost=CreateGNetHost()

	Field playerName$="Player"
	Field playerChat$=""
	Field playerX#=Rnd(GWIDTH-64)+32
	Field playerY#=Rnd(GHEIGHT-64)+32
	Field playerVx#=0
	Field playerVy#=0
	Field playerRot#=0
	Field playerScore=0
	Field playerHit#=0
	Field playerShot=0
	
	'create local player
	Field localPlayer:TGNetObject=CreateGNetObject( host )
	
	Field mXs:Int
	Field mYs:Int
	Field recttmpx:Int=-1,recttmpy:Int=-1,recttmpwidth:Int,recttmpheight:Int,recttmpwidthX:Int,recttmpheightY:Int
	Field selectedUnitsN:Int
	Field selectedUnitsListN:Int
	
	Field t = MilliSecs()
	
	Field Mouse1Released
	Field collision = True 
	
	Field movingmap
	Field movingunits
	Field mh2,mh1,md1
	Field movetimer
	Field mh2check 
	
	Field buildmode = False
	Field buildingsiteunable = True 
	
Global game:tgame = New Tgame	
	Function Create:tgame()
		
		If Not GraphicsModeExists(GWIDTH,GHEIGHT,GDEPTH,GHERTZ) RuntimeError "Grafikkarte nicht unterstützt!"	
		Graphics GWIDTH,GHEIGHT,GDEPTH,GHERTZ ; SetMaskColor 255,255,255 ;	HideMouse() 
		
		game.cursor = LoadImage("graphics/ui/cursor.png")
		game.grid = LoadImage("graphics/grid.bmp",MASKEDIMAGE)		
		game.redBlock = LoadImage("graphics/end.bmp") 'target location
		game.wallBlock = LoadImage("graphics/wall.bmp") 'wall	
		game.workersprite = LoadAnimImage("graphics/units/worker/worker.png",72,72,0,104)
		MidHandleImage game.workersprite	
		game.warriorsprite = LoadAnimImage("graphics/units/warrior/warrior.png",72,72,0,96)
		MidHandleImage game.warriorsprite
		game.tiles = LoadAnimImage("graphics/TILEMAP_LARGE.png",108,108,0,288)
		game.tilesminimap = LoadAnimImage("graphics/minimaptiles.png",3,3,0,288)
		game.unitspriteminimap = LoadImage("graphics/unit.png")
		game.buildingsprite= LoadAnimImage("graphics/buildings/worker/buildings.png",108,64,0,2)
		game.ui = LoadImage("graphics/ui/ui.png")
		game.uiover = LoadImage("graphics/ui/uiover.png")
		game.slotsprite = LoadImage("graphics/ui/slot.png")
		game.slotbuild = LoadAnimImage("graphics/ui/slot_build.png",48,48,0,9)
		game.buildingssprite = LoadAnimImage("graphics/buildings/_buildings.png",65,69,0,10)
		game.chatbackground = LoadImage("graphics/ui/chat.png")
		SetBlend(ALPHABLEND)
		
		'netcode
		SetGNetString game.localPlayer,SLOT_TYPE,"player"
		SetGNetString game.localPlayer,SLOT_NAME,game.playerName
		SetGNetString game.localPlayer,SLOT_CHAT,"ready"
		'SetGNetFloat game.localPlayer,SLOT_X,game.playerX
		'SetGNetFloat game.localPlayer,SLOT_Y,game.playerY
		SetGNetInt game.localPlayer,SLOT_ID,MultiplayerPlayerID 
		SetGNetInt game.localPlayer,SLOT_PLAYERCOLOR,PlayerColorID
	'SetGNetFloat game.localPlayer,SLOT_ROT,game.playerRot
		'SetGNetFloat game.localPlayer,SLOT_HIT,game.playerHit
		'SetGNetInt game.localPlayer,SLOT_SCORE,game.playerScore

		'create Arrays	
		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
		
		buildmap=buildmap[..mapwidth*3+1]
		For Local z = 0 To mapwidth*3
			buildmap[z]=buildmap[z][..mapHeight*3]
		Next
		
		buildidmap=buildidmap[..mapwidth*3+1]
		For Local z = 0 To mapwidth*3
			buildidmap[z]=buildidmap[z][..mapHeight*3]
		Next
				
		For Local x:Int = 0 To mapwidth-1
			For Local y:Int = 0 To mapheight-1
				isomapL1[x][y] = Rand(0,4)
				'isomapL2[x][y] = 286
			Next
		Next
		
		zmap=zmap[..mapwidth*3+1]
		For Local z = 0 To mapwidth*3
			zmap[z]=zmap[z][..mapHeight*3]
		Next
		
		zmap[0][0] = 1
		
		Local xwert
		
		For Local x = 0 To mapwidth*3-1
			xwert:+1
			For Local y = 0 To mapheight*3-1
				zmap[x][y] = xwert+y
			Next
		Next


		game.Astar = Tastar.Create(TILESIZE,MAPWIDTH*3,MAPHEIGHT*3,pathmap)
		For Local x = 1 To 100 'Create And initialize 3 units
			game.unit = New Tunit ; game.unit.ID = x
			If Not UnitList Then unitList = CreateList()
			game.unit.pathBank = CreateBank(1) 'data bank that unit's path data is stored	in	
			unitList.addlast(game.unit)
			If x = 1 'smily
			
			
				'game.NewUnit(360+Rand(180),360+Rand(180),"worker",0,0,0,MultiplayerPlayerID,0,0)				
				
				
				'Local obj:TGnetObject=CreateGNetObject( game.host )
				'SetGNetString obj,SLOT_TYPE,"worker"
				'SetGNetFloat obj,SLOT_X,360+Rand(180)
				'SetGNetFloat obj,SLOT_Y,360+Rand(180)
				'SetGNetFloat obj,SLOT_VX,playerVx+Cos(playerRot)*10
				'SetGNetFloat obj,SLOT_VY,playerVy+Sin(playerRot)*10
				'SetGNetInt obj,SLOT_TIMEOUT,60

				'game.unit.xLoc = 360+Rand(180)
				'game.unit.yLoc = 360+Rand(180)
				'game.unit.speed = 5
				'game.unit.typ = "worker"
				'game.unit.mode = "ready"
				'game.unit.pathAI = userControlled
				'game.unit.sprite = game.workersprite
				'tempunit = game.unit	
			Else 'If x = 2 'chaser
			
			
				'game.NewUnit(360,360,"warrior",0,0,0,MultiplayerPlayerID,0,0)
				
				
				'game.unit.xLoc = 360+Rand(180)
				'game.unit.yLoc = 360+Rand(180)
				'game.unit.speed = 3
				'game.unit.typ = "warrior"
				'game.unit.mode = "ready"
				'game.unit.pathAI = Null'userControlled 'chase
				'game.unit.sprite = game.warriorsprite 'First unit 
			EndIf
			'ElseIf x > 10 Then 
			'game.unit.xLoc = 32+Rand(400)
			'game.unit.yLoc = 32+Rand(400)
			'r.typ = "szenerie"
			'r.anim = 192
			'r.pathAI = Null 'userControlled

		Next
		'game.NewUnit(414,630,"warrior",0,0,0,2,0,0)
		'game.NewUnit(414,666,"worker",0,0,0,2,0,0)
		
		Return game
	End Function
	
	Field checkTimer
	
	Method RunProgram()'Main program loop	
		While Not KeyHit(KEY_ESCAPE) And Not AppTerminate() 'And Not Confirm("Beenden?")
			'UserInput()
			Local t1 = MilliSecs()
			MoveUnits()
			RenderScreen()
			SelectUnits()
			
			If checkTimer+1000 <= MilliSecs() Then  
				'UnitCheck()
				checkTimer = MilliSecs()
			EndIf 
			UpdateBuildSites()		
			UserInput()
			BuildBuilding()
			CheckPlayerID()
			If MultiplayerPlayerID = 1 And hostUnitCreated =False Then CreateHostUnit()
			'CheckPlayerID()
			'game.NewUnit(360+Rand(180),360+Rand(180),"worker",0,0,0,MultiplayerPlayerID,0,0)
			Local t2 = MilliSecs() -t1
			'Print t2+"schleife"
		Wend
		CloseGNetHost host
	End Method
	
	Field hostUnitCreated = False
	
	Method CreateHostUnit()
		Local anz
		For obj:TGnetObject = EachIn GNetObjects( host ) 
			If GetGNetString(obj,SLOT_TYPE) = "player" Then 
				anz:+1
			EndIf
		Next
		If anz > 1 Then game.NewUnit(360+Rand(180),360+Rand(180),"worker",0,0,0,MultiplayerPlayerID,0,0,1)  hostUnitCreated =True
	End Method
	
	Field jointimer
	Field joined
	
	Method CheckPlayerID()
	Local newidset = False
	Local anz
	Local n
	If jointimer+000 <= MilliSecs() And joined = True Then
		For Local obj:TGNetObject=EachIn GNetObjects( host )
			If GNetObjectRemote( obj ) Then
				If GetGNetString(obj,SLOT_TYPE) = "player" Then 
					anz:+1
						Print anz						
					If anz = MultiplayerPlayerID Then
						MultiplayerPlayerID:+1
						oldMultiplayerPlayerID = MultiplayerPlayerID
						
					Else
						newidset = True
						Exit
					EndIf
				EndIf 
			EndIf   
		Next
		If newidset = True Then 
			'For Local obj:TGnetObject=EachIn GNetObjects( host )
				'If GNetObjectLocal( obj ) Then
					SetGNetInt localPlayer,SLOT_ID,MultiplayerPlayerID
					PlayerColorID = MultiplayerPlayerID
					SetGNetInt localPlayer,SLOT_PLAYERCOLOR,PlayerColorID
					UpdateUnitPids(OldMultiplayerPlayerID ,MultiplayerPlayerID)
					Local c:TChatMessage = New TChatMessage
					c.text$ = "Aendere eigene PlayerID auf: "+MultiplayerPlayerID
					c.PlayerName$ = "Client"
					c.PlayerColor = 5
					c.sendTime = MilliSecs()
					ChatList.addlast(c)
					game.NewUnit(360+Rand(180),360+Rand(180),"worker",0,0,0,MultiplayerPlayerID,0,0)
					joined = False
					newidset = False
					'Exit
				'EndIf 
			'Next
		EndIf 
	EndIf 
	End Method
	Method UpdateUnitPids(oldid:Int,newid:Int)
		For unit:Tunit = EachIn Unitlist
			If unit.playerID = oldid Then unit.playerID = newid 'Print unit.playerid
		Next
	End Method
	Method UserInput()'This Function handles most user mouse And keyboard Input	
	
		'If KeyHit(KEY_F9)=True Then 
		
		Local c=GetChar()
		Select c
			Case 8
				If playerChat playerChat=playerChat[..playerChat.length-1]
			Case 13
				If playerChat
					If playerChat[..1]="/"
						Local cmd$=playerChat[1..]
						Local i=cmd.Find(" "),arg$
						If i<>-1
							arg=cmd[i+1..]
							cmd=cmd[..i]
						EndIf
						Select cmd.ToLower()
						Case "nick"
							If arg
								playerName=arg
								SetGNetString localPlayer,SLOT_NAME,playerName
							EndIf
						Case "host"
							Local c:TChatMessage = New TChatMessage
							c.text$ = "Starte Hosting"
							c.PlayerName$ = "Server"
							c.PlayerColor = 5
							c.sendTime = MilliSecs()
							ChatList.addlast(c)
							If Not GNetListen( host,GAMEPORT ) Notify "Hosting Fehlgeschlagen"
							'game.NewUnit(360+Rand(180),360+Rand(180),"worker",0,0,0,MultiplayerPlayerID,0,0)
						Case "connect"
							If Not arg arg="localhost"
							Local c:TChatMessage = New TChatMessage
							c.text$ = "Connecte zu Server: "+arg
							c.PlayerName$ = "Client"
							c.PlayerColor = 5
							c.sendTime = MilliSecs()
							ChatList.addlast(c)
							
							'UpdateUnitPids(oldMultiplayerPlayerID,MultiplayerPlayerID)
							
							If Not GNetConnect( host,arg,GAMEPORT ) Then 
								Notify "Die Verbindung konnte nicht Hergstellt werden!"
							Else
								jointimer = MilliSecs()
								joined =True
								'MultiplayerPlayerID = 2							
							EndIf 
						End Select
					Else
						SetGNetString localPlayer,SLOT_CHAT,playerChat
					EndIf
					playerChat=""
				Else
					'playerChat=playerChat[..playerChat.length+1]
					playerChat=" "
				EndIf 
			Default
				If c>31 And c<127 playerChat:+Chr(c)
		End Select

	
		If KeyHit(KEY_F2)=True Then
			LoadMap("maps","map1")
		EndIf
		
		If KeyHit(KEY_F9)=True Then
			Local r:tunit = New tUnit
						
			r.xLoc = pickedX*36 
			r.yLoc = pickedY*36
			r.typ = "szenerie"
			r.pathAI = Null 'userControlled
			'r.sprite = tree1
			r.anim = 192
			                        							
			UnitList.addlast(r)

		EndIf
	
		If MouseHit(2)=True Then
			'Print "Hit"	

			movetimer = MilliSecs()+150
			'hitdetected = True
			'movingmap = True 
			'mh2 = 1
			'movingmap = False	
			mh2check = 1
			
		Else
			'mh2 = 0
		EndIf 	
		
		
		If movetimer <= MilliSecs() And mh2check = 1 And buildmode= False Then
			'Print MouseDown(2)
			If MouseDown(2) = True Then
				'Print "movemap"
				mh2 = 2
				mh2check = 0
			Else
				mh2 = 3 
				movingunits = True 
				mh2check = 0
			EndIf
			
			movetimer = MilliSecs()+150
		EndIf   
		
		Select mh2
			Case 1
				
			Case 2
				movingmap = True 
				mh2 = 0
			Case 3
				movingmap = False
				mh2 = 0
				'Print "Move Units!!" 
		EndSelect
		
		If movingmap = True 
			MapX = mapX + mXs
			MapY = mapY + mYs
			If MouseDown(2) = False Then
				mh2 = 0 
				mh2check = 0
				movingmap = False
			EndIf 
		EndIf 
		
		mh1 = MouseHit(1) 
		md1 = MouseDown(1)	
		  
	End Method
	Method RenderScreen() 'This Function draws stuff on the screen.
		'Tile Auswahl 1*1
		pickedX = TileX(MouseX(), MouseY(), tilesizeW, tilesizeH, mapX, mapY)
		pickedpixelX = PixelX(pickedX, pickedY, tilesizeW)
		pickedY = TileY(MouseX(), MouseY(), tilesizeW, tilesizeH, mapX, mapY)
		pickedpixelY = PixelY(pickedX, pickedY, tilesizeH)
		
		'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
		tiletoselect :+ MouseZSpeed()
		mXs = MouseXSpeed()
		mYs = MouseYSpeed()	
				
		'Aktiviere/Deaktiviere Kollisions zeichnen
		If MouseHit(3)  Then 
			If collision = True  Then 
				collision = False 
			Else 
				collision = True
			EndIf
		EndIf
		
		
		UnitCollision()
		
		GNetSync host
		
		Cls	
		
		'SortList(UnitList,True,Tunit.compare_x)
		'SortList(UnitList,True,Tunit.compare_y)
		
		DrawMaps()
		
		SelectedUnits()
		
		'DrawPlayerInfos()
		
		
		SetColor 255,255,255
		DrawBox(recttmpx,recttmpy,recttmpwidth,recttmpheight)
		
		DrawTextInfos()
		
		If KeyDown(KEY_F4) Then 
		
		
			
			DrawText "PX: "+pickedX+" PY:"+pickedY+" PXT:"+pickedXtile+" PYT:"+pickedYtile, MouseX()+10,MouseY()+10
			SetColor 255,255,255
		EndIf 
		DrawImage cursor,MouseX(),MouseY() 'Draw the mouse
		
		
		
		Flip
	End Method
	Method NewUnit(x:Float,y:Float,typ:String,anim:Int,richtung:Int,zindex:Int,playerid:Int,buildx:Int,buildy:Int,id:Int=-1) 
		'
		'Local x:BaseType=New BaseType
		'Local y:BaseType=New DerivedType
		'Local objj:TGNetObject = CreateGNetObject( host )
		Local unit:Tunit = New Tunit
		
		'data bank that unit's path data is stored	in	
		
		Select typ
			Case "worker","warrior"
				If id = -1 Then unit.ID = NextUnitID Else unit.ID = id 
				unit.pathBank = CreateBank(1) 
				unit.playerID = playerid
				unit.xLoc = x
				unit.yLoc = y
				unit.speed = 5
				unit.typ = typ
				unit.anim = anim
				unit.richtung = richtung
				unit.mode = "ready"
				unit.pathAI = Null 'userControlled
				unit.zindex = zindex
				If typ = "worker" Then 
				unit.hp = 20
				unit.maxhp = 20
				unit.dmg = 2.5
				unit.targetpid = 0
				unit.targetid = 0
				ElseIf typ = "warrior"
				unit.hp = 50
				unit.maxhp = 50
				unit.dmg = 5
				unit.targetpid = 0
				unit.targetid = 0
				EndIf 

			Case "building"
				If id = -1 Then unit.ID = NextUnitID Else unit.ID = id 
				unit.playerID = playerid
				If id -1 Then
				unit.xLoc =x*36+114
				unit.yLoc =y*36+114
				Else
				unit.xLoc =x
				unit.yLoc =y
				EndIf 
				'Print "SETCOLLISION X:"+x+" Y:"+y
				unit.typ = "building"
				unit.anim = 1 
				unit.richtung = richtung
				unit.wantstobe = nextbuilding+1
				unit.buildpointstoreach = 5
				unit.buildpoints = 0
				unit.pathAI = Null 'userControlled
				unit.zindex = 0
				unit.mode = "ready"
				If id = -1 Then 	
				unit.buildX = x
				unit.buildY = y
				Else
				unit.buildX = buildx
				unit.buildy = buildy
				EndIf
				unit.hp = 500
				unit.maxhp = 500
				unit.dmg = 5
				unit.targetpid = 0
				unit.targetid = 0

	
				'Print "SETCOLLISION X:"+x+" Y:"+y
				If id = -1 Then 
					pathmap[x][y] = unwalkable
					pathmap[x+1][y] = unwalkable
					pathmap[x+2][y] = unwalkable
					pathmap[x+2][y+1] = unwalkable
					pathmap[x][y+1] = unwalkable
					pathmap[x][y+2] = unwalkable
					pathmap[x+1][y+2] = unwalkable
					pathmap[x+1][y+1] = unwalkable
					pathmap[x+2][y+2] = unwalkable
						
					buildmap[x][y] = unit.ID
					buildmap[x+1][y] = unit.ID
					buildmap[x+2][y] = unit.ID
					buildmap[x+2][y+1] = unit.ID
					buildmap[x][y+1] = unit.ID
					buildmap[x][y+2] = unit.ID
					buildmap[x+1][y+2] = unit.ID
					buildmap[x+1][y+1] = unit.ID
					buildmap[x+2][y+2] = unit.ID
				Else
					pathmap[buildx][buildy] = unwalkable
					pathmap[buildx+1][buildy] = unwalkable
					pathmap[buildx+2][buildy] = unwalkable
					pathmap[buildx+2][buildy+1] = unwalkable
					pathmap[buildx][buildy+1] = unwalkable
					pathmap[buildx][buildy+2] = unwalkable
					pathmap[buildx+1][buildy+2] = unwalkable
					pathmap[buildx+1][buildy+1] = unwalkable
					pathmap[buildx+2][buildy+2] = unwalkable
						
					buildmap[buildx][buildy] = unit.ID
					buildmap[buildx+1][buildy] = unit.ID
					buildmap[buildx+2][buildy] = unit.ID
					buildmap[buildx+2][buildy+1] = unit.ID
					buildmap[buildx][buildy+1] = unit.ID
					buildmap[buildx][buildy+2] = unit.ID
					buildmap[buildx+1][buildy+2] = unit.ID
					buildmap[buildx+1][buildy+1] = unit.ID
					buildmap[buildx+2][buildy+2] = unit.ID
				EndIf 
			Case "szenerie"
				Print "Create Szenerie"
				If id = -1 Then unit.ID = NextUnitID  Else unit.ID = id 
				unit.playerID = playerid
				If id -1 Then
				unit.xLoc =x*36'+114
				unit.yLoc =y*36'+114
				Else
				unit.xLoc =x
				unit.yLoc =y
				EndIf 
				'Print "SETCOLLISION X:"+x+" Y:"+y
				unit.typ = "szenerie"
				unit.anim = anim
				unit.richtung = richtung
				unit.wantstobe = nextbuilding+1
				unit.buildpointstoreach = 5
				unit.buildpoints = 0
				unit.pathAI = Null 'userControlled
				unit.zindex = 0
				unit.mode = "ready"
				If id = -1 Then 	
				unit.buildX = buildx
				unit.buildY = buildy
				Else
				unit.buildX = buildx
				unit.buildy = buildy
				EndIf
				unit.hp = 500
				unit.maxhp = 500
				unit.dmg = 5
				unit.targetpid = 0
				unit.targetid = 0

	
				'Print "SETCOLLISION X:"+x+" Y:"+y
				If id = -1 Then 
					pathmap[x][y] = unwalkable
				Else
					pathmap[buildx][buildy] = unwalkable
				EndIf 

		EndSelect 
		If playerid = MultiplayerPlayerID Then
			Print "New Lokal Unit"
			Local Gnetobj:TGnetObject = CreateGNetObject( host )
			SetGNetString Gnetobj ,SLOT_TYPE,typ
			SetGNetInt Gnetobj ,SLOT_ID,unit.ID
			SetGNetFloat Gnetobj ,SLOT_X,unit.xLoc
			SetGNetFloat Gnetobj ,SLOT_Y,unit.yLoc
			SetGNetInt Gnetobj ,SLOT_RICHTUNG,unit.richtung
			SetGNetInt Gnetobj ,SLOT_ANIM,unit.anim
			SetGNetInt Gnetobj ,SLOT_Z,unit.Zindex
			SetGNetInt Gnetobj ,SLOT_BUILDX,unit.buildx
			SetGNetInt Gnetobj ,SLOT_BUILDY,unit.buildy
			SetGNetInt Gnetobj ,SLOT_PLAYERID,unit.playerId
			SetGNetFloat Gnetobj,SLOT_HP,unit.hp
			SetGNetFloat Gnetobj,SLOT_MAXHP,unit.maxhp
			SetGNetFloat Gnetobj,SLOT_DMG,unit.dmg
			SetGNetInt Gnetobj,SLOT_TARGETPID,unit.targetPid
			SetGNetInt Gnetobj,SLOT_TARGETID,unit.targetid
			SetGNetInt Gnetobj,SLOT_LASTTILEX,unit.lastTileX
			SetGNetInt Gnetobj,SLOT_LASTTILEY,unit.lastTileY

			SetGNetString Gnetobj ,SLOT_MODE,unit.mode
		Else
			Print "New Remote Unit ID:"+unit.id+" PID:"+unit.Playerid
		EndIf 
		NextUnitID :+1
			
		unitList.addlast(unit)	
	End Method
	Method DrawChat()
		
		Local ty
		For Local obj:TGNetObject=EachIn GNetObjects( host )
	
			If obj.State()=GNET_CLOSED Continue
			
			Local typ$=GetGNetString( obj,SLOT_TYPE )
			Select typ
			Case "player"
				Local name$=GetGNetString( obj,SLOT_NAME )
				Local chat$=GetGNetString( obj,SLOT_CHAT )
				Local id = GetGNetInt( obj,SLOT_ID)
				Local color = GetGNetInt(obj,SLOT_PLAYERCOLOR)
				If lastChat[id] <> chat$ Then  
					Local c:TChatMessage = New TChatMessage
					c.text$ = chat$
					c.PlayerName$ = name$
					c.PlayerColor = color
					c.sendTime = MilliSecs()
					ChatList.addlast(c)
					lastChat[id] = chat$
				EndIf 
				'Local score=GetGNetInt( obj,SLOT_SCORE )				
				'If obj=localPlayer SetColor 255,255,255 Else SetColor 0,128,255
				Select color 
					Case 1
						SetColor 255,0,0
					Case 2
						SetColor 0,255,0
					Case 3
						SetColor 0,0,255
					Case 4
						SetColor 128,128,0
				EndSelect 
				DrawText name+":"+id,0,ty
				SetColor 255,255,255
				ty:+16
			End Select
		Next
		
		'Eingabe
		If playerChat
			DrawImage chatbackground,0,569'GHEIGHT-16
			SetColor 255,255,0
			DrawText ">"+playerChat,8,578
			SetColor 0,200,0
			DrawRect TextWidth(">"+playerChat)+8,578,8,16
			SetColor 255,255,255
		EndIf
		
		Local cy
		Local nNachrichten:Int = CountList(ChatList)
		For Local c:TChatMessage = EachIn ChatList
			cy:+16
			Select c.PlayerColor 
					Case 1
						SetColor 255,0,0
					Case 2
						SetColor 0,255,0
					Case 3
						SetColor 0,0,255
					Case 4
						SetColor 128,128,0
					Case 5
						SetColor 179,0,89
				EndSelect 
			DrawText c.PlayerName$+":",5,cy+300
			SetColor 255,255,255
			DrawText c.text$,5+TextWidth(c.PlayerName$+":"),cy+300
			If c.sendTime+10000 <= MilliSecs() Then ListRemove(ChatList,c)
			If nNachrichten > 6 And c Then ListRemove(ChatList,c); nNachrichten = CountList(ChatList)
		Next
		
		
	End Method
	Method DrawTextinfos()
		If KeyDown(KEY_F4) Then 
			'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 + "|PPX:" + pickedpixelX + "|PPY:" + pickedpixelX + "MX:"+ MouseX() +"|MY:" +MouseY(), MouseX()-80, MouseY()-8
			SetColor 255,255,255
			
			'Editor Tile ID und Anzeige des Aktuell ausgewählten Tiles
			SetColor 0,0,0
			DrawRect 914,36,tilesizeW+2,tilesizeH*2+2
			SetColor 255,255,255
			DrawImage(tiles, 915,37 ,tiletoselect)
			If collision = True Then SetColor 0,255,0 Else SetColor 255,0,0
			DrawRect 991,2,32,32
			SetColor 255,0,0
			DrawText "Tile ID:"+tiletoselect,915,146
			SetColor 255,255,255
			
			' Edit Modus oder nicht
			SetColor 0,0,0
			DrawText "K",1002,12
			If Started = False Then SetColor 0,255,0 Else SetColor 255,0,0
			DrawRect 957,2,32,32
			SetColor 0,0,0
			DrawText "Edit",958,12
			
			' 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
		EndIf 
		
		'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)
		SetColor 255,255,255
		
		'GnetObjects
		SetColor 255,140,0
		Local nobj$="GnetObjects: "+CountGnetObjects()
		DrawText nobj,1024-TextWidth(nobj),758-TextHeight(nobj)
		SetColor 255,255,255
		If KeyDown(KEY_F6)=True Then 
			ListGnetObjects()
		EndIf
		
		' RAM
		SetColor 0,128,0
		Local txt$="MemAllocd: "+(GCMemAlloced()/1000)+"mb"
		DrawText txt,1024-TextWidth(txt),768-TextHeight(txt)
		SetColor 255,255,255
		
	End Method
	'Method UnitKi()
	'For unit = EachIn UnitList
		'If unit.mode = "building" And unit.pathStatus = 0 Then
			'unit.anim = 20
		'EndIf
	'Next	
	'EndMethod
	Method UpdateBuildSites()
		For Local r:tunit = EachIn UnitList
			If r.typ = "building" Then 
				If r.buildpoints >= r.buildpointstoreach And r.built = False Then 
					r.built = True 
					r.anim = r.wantstobe
					buildmap[r.buildX][r.buildY] = Null
					buildmap[r.buildX][r.buildY+1] = Null
					buildmap[r.buildX][r.buildY+2] = Null
					buildmap[r.buildX+1][r.buildY] = Null
					buildmap[r.buildX+2][r.buildY] = Null
					buildmap[r.buildX+1][r.buildY+1] = Null
					buildmap[r.buildX+2][r.buildY+2] = Null
					buildmap[r.buildX+1][r.buildY+2] = Null
					buildmap[r.buildX+2][r.buildY+1] = Null
					For Local w:tunit = EachIn Unitlist
					If w.typ = "worker" And w.mode = "building" Then 
						If r.ID = w.buildsbuildingid And r.built = True Then 
							w.mode = "ready"
							w.anim = 0
							w.richtung = 4
							w.buildsbuildingid = 0
						EndIf 
					EndIf  
				Next
				EndIf 
			EndIf
		Next
	End Method
	Method GetUnitPID(x:Int,y:Int)
		For Local u:Tunit = EachIn Unitlist
			If Floor(u.xLoc/36) = x And Floor(u.yLoc/36) = y Then Return u.playerid
		Next
	End Method
	Method GetUnitID(x:Int,y:Int)
		For Local u:Tunit = EachIn Unitlist
			If Floor(u.xLoc/36) = x And Floor(u.yLoc/36) = y Then Return u.id
		Next
	End Method
	Method GetTargetX(pid:Int,id:Int)
		For Local u:Tunit = EachIn Unitlist
			If u.playerid = pid And u.id = id Then Return u.xLoc
		Next
	End Method
	Method GetTargetY(pid:Int,id:Int)
		For Local u:Tunit = EachIn Unitlist
			If u.playerid = pid And u.id = id Then Return u.yLoc
		Next
	End Method
	Method GetTargetObject:Tunit(pid:Int,id:Int)
		For Local u:Tunit = EachIn Unitlist
			If u.playerid = pid And u.id = id Then Return u
		Next
	EndMethod 
	Method GetDistanceToObject:Float(xLoc:Float,yLoc:Float,pid:Int,id:Int)
		For Local u:Tunit = EachIn Unitlist
			If u.playerid = pid And u.id = id Then Return (Sqr(((xLoc-u.xLoc)^2)+((yLoc-u.yLoc)^2)))
		Next
	End Method
	Method CheckCollision(targetpid:Int, targetid:Int)
		For Local u:Tunit = EachIn Unitlist
			If u.playerid = targetpid And u.id = targetid Then 
				Local tilenorth = pathmap[u.xLoc/36][u.yLoc/36-1]
				Local tilesouth = pathmap[u.xLoc/36][u.yLoc/36+1]
				Local tilewest  = pathmap[u.xLoc/36-1][u.yLoc/36]
				Local tileost   = pathmap[u.xLoc/36+1][u.yLoc/36]
				
				If tilenorth <> 2 Or tilenorth <> 3 Then Return 1
				If tilesouth <> 2 Or tilenorth <> 3 Then Return 2
				If tilewest  <> 2 Or tilenorth <> 3 Then Return 3
				If tileost   <> 2 Or tilenorth <> 3 Then Return 4
			EndIf
		Next
	End Method 
	
	Method CreateEventObject(pid:Int,id:Int,event:String)
		Local Gnetobj:TGnetObject = CreateGNetObject( host )
		SetGNetString Gnetobj ,SLOT_TYPE,"event"
		SetGNetInt Gnetobj ,SLOT_PLAYERID,pid
		SetGNetInt Gnetobj ,SLOT_ID,id
		SetGNetString Gnetobj,SLOT_EVENTTYPE,event
		SetGNetFloat Gnetobj ,SLOT_EVENTTIME,MilliSecs()
	End Method
	Method DeleteEventObject(pid:Int,id:Int,event:String,time:Int)
		Local opid
		Local oid
		Local oevent$
		For Local obj:TGnetObject = EachIn GNetObjects( host )
			If GNetObjectLocal( obj ) And obj.State() <> GNET_CLOSED Then 
				If GetGNetString(obj,SLOT_TYPE) = "event"  Then 
					If GetGNetFloat(obj,SLOT_EVENTTIME)+500 <= time Then 
						opid = GetGNetInt(obj,SLOT_PLAYERID)
						oid = GetGNetInt(obj,SLOT_ID)
						oevent = GetGNetString(obj,SLOT_EVENTTYPE)
						
						If opid = pid And oid = id And oevent = event Then 
						 	For Local u:Tunit = EachIn UnitList
								If u.playerid = pid And u.id = id Then 
									ListRemove(UnitList,u)
									u = Null
									Local unittodeletepid
									Local unittodeleteid
									
									For Local de:TGnetObject = EachIn GNetObjects( host )
										If GetGNetString(de,SLOT_TYPE) <> "event" And GetGNetString(de,SLOT_TYPE) <> "player" Then 
											unittodeletepid = GetGNetInt(de,SLOT_PLAYERID)
											unittodeleteid = GetGNetInt(de,SLOT_ID)
											If opid = unittodeletepid And oid = unittodeleteid Then  
												CloseGNetObject(obj)
												CloseGNetObject(de)
												Exit
											EndIf 
										EndIf 
									Next
								EndIf 
							Next
						EndIf 
					EndIf 
				EndIf 
			EndIf 
		Next
	End Method
	Method DeleteRemoteUnitbyEvent(pid:Int,id:Int,event:String)
		Local opid
		Local oid
		Local oevent$
		For Local obj:TGnetObject = EachIn GNetObjects( host )
			If GNetObjectRemote( obj ) Then 
				If GetGNetString(obj,SLOT_TYPE) = "event" Then 
					opid = GetGNetInt(obj,SLOT_PLAYERID)
					oid = GetGNetInt(obj,SLOT_ID)
					oevent = GetGNetString(obj,SLOT_EVENTTYPE)
					If opid = pid And oid = id And oevent = event Then 
						'CloseGNetObject(obj)
						For Local u:Tunit = EachIn UnitList
							If u.playerid = pid And u.id = id Then 
								'Print "DELETED"
								ListRemove(UnitList,u)
								u = Null
								'CloseGNetObject obj
							EndIf 
						Next
					EndIf 
				EndIf 
			EndIf 
		Next

	EndMethod 
	Method CountGnetObjects:Int()
	Local anz
		For Local g:TGnetObject = EachIn GNetObjects( host )
			anz:+1 
		Next
		Return anz
	EndMethod
	Method ListGnetObjects:Int()
	Local anz
		For Local g:TGnetObject = EachIn GNetObjects( host )
			DrawText GetGNetString( g,SLOT_TYPE ),50,50+anz*12
			anz:+1
		Next
	EndMethod
	Method CheckForDeathOfTarget(pid:Int,id:Int)
		For Local u:tunit = EachIn Unitlist
			If u.playerid = pid And u.id = id Then
				If u.hp > 0 Then 
				Return False
				EndIf 
			EndIf
		Next
		Return True
	EndMethod

	Method UnitCheck()
	Local t1 = MilliSecs()
	For unit = EachIn UnitList
		'Local t3 = MilliSecs()
		
		If unit.typ = "warrior" Then
			If unit.mode = "ready" Or unit.mode = "walking" Then
				If unit.mode = "ready" Then unit.anim = 0
				If unit.hp <= 0 Then unit.mode = "dead"
				
				unit.targetpid = 0
				unit.targetid = 0
				
				Local targetpid
				Local targetid
				Local targetdist
				
				For Local t:tunit = EachIn UnitList
					targetpid = GetUnitPID(t.xLoc/36,t.yLoc/36) 
					targetid = GetUnitID(t.xLoc/36,t.yLoc/36)
					targetdist = GetDistanceToObject(unit.xloc,unit.yloc,targetpid,targetid)
					If targetpid <> MultiplayerPlayerID And targetpid > 0 And targetdist < 160 Then 
						unit.targetpid = targetpid
						unit.targetid = targetid
						unit.mode = "chase"
					EndIf
				Next
				
				
			ElseIf unit.mode = "chase" Then
				If unit.hp <= 0 Then unit.mode = "dead"
				Local dist = GetDistanceToObject(unit.xLoc,unit.yLoc,unit.targetpid,unit.targetid)
				'Print dist
				If dist > 160 Then 
					unit.mode = "ready"
					unit.anim = 0
				ElseIf dist > 40 And dist < 160 Then'And unit.pathAI <> Usercontrolled Then
					For Local t:tunit = EachIn UnitList
					
							Local targetpid
				Local targetid
				Local targetdist
					targetpid = GetUnitPID(t.xLoc/36,t.yLoc/36) 
					targetid = GetUnitID(t.xLoc/36,t.yLoc/36)
					targetdist = GetDistanceToObject(unit.xloc,unit.yloc,targetpid,targetid)
					If targetpid <> MultiplayerPlayerID And targetpid > 0 And targetdist < 40 Then 
						unit.targetpid = targetpid
						unit.targetid = targetid
						'unit.mode = "fight"
					EndIf
				Next
					Local x1
					Local y1
					Local checktiles = CheckCollision(unit.targetpid,unit.targetid)
					Select checktiles
						Case 1
							x1 = 0
							y1 = -1
						Case 2
							x1 = 0
							y1 = 1
						Case 3
							x1 = -1
							y1 = 0
						Case 4
							x1 = 1
							y1 = 0
					End Select
					unit.pathStatus = astar.FindPath(unit,GetTargetX(unit.targetpid,unit.targetid)+x1*36,GetTargetY(unit.targetpid,unit.targetid)+y1*36)
				ElseIf dist < 40 Then 
					If unit.mode <> "dead" Then unit.mode = "fight"
				EndIf
				
			ElseIf unit.mode = "fight" Then
				
				
												
				Local tilenorth = pathmap[unit.xLoc/36][unit.yLoc/36-1]
				Local tilesouth = pathmap[unit.xLoc/36][unit.yLoc/36+1]
				Local tilewest  = pathmap[unit.xLoc/36-1][unit.yLoc/36]
				Local tileost   = pathmap[unit.xLoc/36+1][unit.yLoc/36]
				
				Local check = tilenorth + tilesouth + tilewest + tileost
				
				If check < 1 Then 
					unit.mode = "ready"
					unit.anim = 0
				EndIf 
				
				
							
				Local dist = GetDistanceToObject(unit.xLoc,unit.yLoc,unit.targetpid,unit.targetid)
				'Print dist
				'Print dist
				If dist > 160 Then 
					unit.mode = "ready"
					unit.anim = 0
				ElseIf dist > 40 And dist < 160 Then 
					unit.mode = "chase"
				
					'unit.pathStatus = astar.FindPath(unit,GetTargetX(unit.targetpid,unit.targetid),GetTargetY(unit.targetpid,unit.targetid))
				ElseIf dist < 40 Then 
					unit.mode = "fight"
				EndIf

				If CheckForDeathOfTarget(unit.targetpid,unit.targetid) = True Then unit.mode = "ready"
				
				If tilenorth > 1 And GetUnitPid(unit.xLoc/36,unit.yLoc/36-1) <> MultiplayerPlayerId Then
					If unit.startzeit+100 <= MilliSecs() Then 
						
						If unit.anim < 4
							unit.startzeit = MilliSecs()
							unit.anim = unit.anim + 1
						Else
							unit.anim = 0
							unit.startzeit = MilliSecs()
							For Local j:tunit = EachIn unitlist
								If j.targetpid = MultiplayerPlayerID And j.targetid = unit.id Then
									unit.hp:- j.dmg
								EndIf 
							Next
						EndIf 
					EndIf
					unit.richtung = 41+(unit.anim*8)
				EndIf 
				If tilesouth > 1 And GetUnitPid(unit.xLoc/36,unit.yLoc/36+1) <> MultiplayerPlayerId   Then
					If unit.startzeit+100 <= MilliSecs() Then 
						
						If unit.anim < 4
							unit.startzeit = MilliSecs()
							unit.anim = unit.anim + 1
						Else
							unit.anim = 0
							unit.startzeit = MilliSecs()
							For Local j:tunit = EachIn unitlist
								If j.targetpid = MultiplayerPlayerID And j.targetid = unit.id Then
									unit.hp:- j.dmg
								EndIf 
							Next
						EndIf 
					EndIf
					unit.richtung = 46+(unit.anim*8)
					
				EndIf 
				If tilewest > 1 And GetUnitPid(unit.xLoc/36-1,unit.yLoc/36) <> MultiplayerPlayerId  Then
					If unit.startzeit+100 <= MilliSecs() Then 
						
						If unit.anim < 4
							unit.startzeit = MilliSecs()
							unit.anim = unit.anim + 1
						Else
							unit.startzeit = MilliSecs()
							unit.anim = 0
							For Local j:tunit = EachIn unitlist
								If j.targetpid = MultiplayerPlayerID And j.targetid = unit.id Then
									unit.hp:- j.dmg
								EndIf 
							Next
						EndIf 
					EndIf
					unit.richtung = 47+(unit.anim*8)  
				EndIf 
				If tileost > 1 And GetUnitPid(unit.xLoc/36+1,unit.yLoc/36) <> MultiplayerPlayerId Then
					 If unit.startzeit+100 <= MilliSecs() Then 
						
						If unit.anim < 4
							unit.startzeit = MilliSecs()
							unit.anim = unit.anim + 1
						Else
							unit.anim = 0
							unit.startzeit = MilliSecs()
							For Local j:tunit = EachIn unitlist
								If j.targetpid = MultiplayerPlayerID And j.targetid = unit.id Then
									unit.hp:- j.dmg
								EndIf 
							Next
						EndIf 
					EndIf
					unit.richtung = 43+(unit.anim*8)
				EndIf 
				If unit.hp <= 0 Then unit.mode = "dead"
			ElseIf unit.mode = "dead" Then 
				 If unit.startzeit+100 <= MilliSecs() Then 
						
					If unit.anim < 1
						unit.startzeit = MilliSecs()
						unit.anim = unit.anim + 1
					Else
						pathmap[unit.xLoc/36][unit.yLoc/36] = walkable
						
						If unit.playerID = MultiplayerPlayerID Then 
							CreateEventObject(unit.playerid,unit.id,"dead")
							unit.mode = "waitfordelete"
						EndIf 
						'New NetObject
					EndIf 
				EndIf
				If unit <> Null Then unit.richtung = 80+(unit.anim*8)
			ElseIf unit.mode = "waitfordelete" Then 
				unit.anim = 2
				If unit.playerID = MultiplayerPlayerID Then 
					DeleteEventObject(unit.playerid,unit.id,"dead",MilliSecs())
				Else
					DeleteRemoteUnitbyEvent(unit.playerid,unit.id,"dead")
				EndIf 
			EndIf 
		
		ElseIf unit.typ = "worker" Then
			If unit.mode = "ready" Or unit.mode = "building" Then
				If unit.hp <= 0 Then unit.mode = "dead"

				Local tilenorth = buildmap[unit.xLoc/36][unit.yLoc/36-1]
				Local tilesouth = buildmap[unit.xLoc/36][unit.yLoc/36+1]
				Local tilewest  = buildmap[unit.xLoc/36-1][unit.yLoc/36]
				Local tileost   = buildmap[unit.xLoc/36+1][unit.yLoc/36]

				Local check = tilenorth + tilesouth + tilewest + tileost
				
				If check > 0 Then 
					unit.mode = "building"
				EndIf 
				
				If tilenorth <> 0 Then
					If unit.startzeit+100 <= MilliSecs() Then 
						
						If unit.anim < 4
							unit.startzeit = MilliSecs()
							unit.anim = unit.anim + 1
						Else
							unit.anim = 0
							unit.startzeit = MilliSecs()
							unit.buildsbuildingid = tilenorth 
							For Local r:tunit = EachIn unitlist
								If r.iD = unit.buildsbuildingid Then 
									If r.pathStatus = 0 Then
										r.buildpoints:+1
									EndIf
								EndIf 
							Next
						EndIf 
					EndIf
					unit.richtung = 41+(unit.anim*8)
				EndIf 
				If tilesouth <> 0 Then
					If unit.startzeit+100 <= MilliSecs() Then 
						
						If unit.anim < 4
							unit.startzeit = MilliSecs()
							unit.anim = unit.anim + 1
						Else
							unit.anim = 0
							unit.startzeit = MilliSecs()
							unit.buildsbuildingid = tilesouth 
							For Local r:tunit = EachIn unitlist
								If r.iD = unit.buildsbuildingid Then 
									If r.pathStatus = 0 Then
									r.buildpoints:+1
									EndIf
								EndIf 
							Next
						EndIf 
					EndIf
					unit.richtung = 46+(unit.anim*8)
					
				EndIf 
				If tilewest <> 0 Then
					If unit.startzeit+100 <= MilliSecs() Then 
						
						If unit.anim < 4
							unit.startzeit = MilliSecs()
							unit.anim = unit.anim + 1
						Else
							unit.anim = 0
							unit.startzeit = MilliSecs()
							unit.buildsbuildingid = tilewest
							For Local r:tunit = EachIn unitlist
								If r.iD = unit.buildsbuildingid Then 
									If r.pathStatus = 0 Then
									'Print r.pathStatus
									r.buildpoints:+1
									EndIf 
								EndIf 
							Next
							EndIf 
					EndIf
					unit.richtung = 47+(unit.anim*8)  
				EndIf 
				If tileost <> 0 Then
					 If unit.startzeit+100 <= MilliSecs() Then 
						
						If unit.anim < 4
							unit.startzeit = MilliSecs()
							unit.anim = unit.anim + 1
						Else
							unit.anim = 0
							unit.startzeit = MilliSecs()
							unit.buildsbuildingid = tileost 
							For Local r:tunit = EachIn unitlist
								If r.iD = unit.buildsbuildingid Then 
									If r.pathStatus = 0 Then
									r.buildpoints:+1
									EndIf
								EndIf 
							Next
							EndIf 
					EndIf
					unit.richtung = 43+(unit.anim*8)
				EndIf
			Local targetpid
				Local targetid
				Local targetdist
				
				For Local t:tunit = EachIn UnitList
					targetpid = GetUnitPID(t.xLoc/36,t.yLoc/36) 
					targetid = GetUnitID(t.xLoc/36,t.yLoc/36)
					targetdist = GetDistanceToObject(unit.xloc,unit.yloc,targetpid,targetid)
					If targetpid <> MultiplayerPlayerID And targetpid > 0 And targetdist < 160 Then 
						unit.targetpid = targetpid
						unit.targetid = targetid
						unit.mode = "chase"
					EndIf
				Next
			ElseIf unit.mode = "chase" Then
				If unit.hp <= 0 Then unit.mode = "dead"
				Local dist = GetDistanceToObject(unit.xLoc,unit.yLoc,unit.targetpid,unit.targetid)
				'Print dist
				If dist > 160 Then 
					unit.mode = "ready"
					unit.anim = 0
				ElseIf dist > 40 And dist < 160 And unit.pathAI <> Usercontrolled Then 
					Local x1
					Local y1
					Local checktiles = CheckCollision(unit.targetpid,unit.targetid)
					Select checktiles
						Case 1
							x1 = 0
							y1 = -1
						Case 2
							x1 = 0
							y1 = 1
						Case 3
							x1 = -1
							y1 = 0
						Case 4
							x1 = 1
							y1 = 0
					End Select
					unit.pathStatus = astar.FindPath(unit,GetTargetX(unit.targetpid,unit.targetid)+x1*36,GetTargetY(unit.targetpid,unit.targetid)+y1*36)
				ElseIf dist < 40 Then 
					If unit.mode <> "dead" Then unit.mode = "fight"
				EndIf

			ElseIf unit.mode = "fight" Then
				
								
				Local tilenorth = pathmap[unit.xLoc/36][unit.yLoc/36-1]
				Local tilesouth = pathmap[unit.xLoc/36][unit.yLoc/36+1]
				Local tilewest  = pathmap[unit.xLoc/36-1][unit.yLoc/36]
				Local tileost   = pathmap[unit.xLoc/36+1][unit.yLoc/36]
				
				Local check = tilenorth + tilesouth + tilewest + tileost
				
				If check = 0 Then 
					unit.mode = "ready"
				EndIf 
				
				Local dist = GetDistanceToObject(unit.xLoc,unit.yLoc,unit.targetpid,unit.targetid)
				'Print dist
				If dist > 160 Then 
					unit.mode = "ready"
					unit.anim = 0
				ElseIf dist > 40 And dist < 160 Then 
					'unit.pathStatus = astar.FindPath(unit,GetTargetX(unit.targetpid,unit.targetid),GetTargetY(unit.targetpid,unit.targetid))
				ElseIf dist < 40 Then 
					unit.mode = "fight"
				EndIf

				
				If tilenorth <> 0 And GetUnitPid(unit.xLoc/36,unit.yLoc/36-1) <> MultiplayerPlayerId Then
					If unit.startzeit+100 <= MilliSecs() Then 
						
						If unit.anim < 4
							unit.startzeit = MilliSecs()
							unit.anim = unit.anim + 1
						Else
							unit.anim = 0
							unit.startzeit = MilliSecs()
							For Local j:tunit = EachIn unitlist
								If j.targetpid = MultiplayerPlayerID And j.targetid = unit.id Then
									unit.hp:- j.dmg
								EndIf 
							Next
						EndIf 
					EndIf
					unit.richtung = 41+(unit.anim*8)
				EndIf 
				If tilesouth <> 0 And GetUnitPid(unit.xLoc/36,unit.yLoc/36+1) <> MultiplayerPlayerId   Then
					If unit.startzeit+100 <= MilliSecs() Then 
						
						If unit.anim < 4
							unit.startzeit = MilliSecs()
							unit.anim = unit.anim + 1
						Else
							unit.anim = 0
							unit.startzeit = MilliSecs()
							For Local j:tunit = EachIn unitlist
								If j.targetpid = MultiplayerPlayerID And j.targetid = unit.id Then
									unit.hp:- j.dmg
								EndIf 
							Next
						EndIf 
					EndIf
					unit.richtung = 46+(unit.anim*8)
					
				EndIf 
				If tilewest <> 0 And GetUnitPid(unit.xLoc/36-1,unit.yLoc/36) <> MultiplayerPlayerId  Then
					If unit.startzeit+100 <= MilliSecs() Then 
						
						If unit.anim < 4
							unit.startzeit = MilliSecs()
							unit.anim = unit.anim + 1
						Else
							unit.startzeit = MilliSecs()
							unit.anim = 0
							For Local j:tunit = EachIn unitlist
								If j.targetpid = MultiplayerPlayerID And j.targetid = unit.id Then
									unit.hp:- j.dmg
								EndIf 
							Next
						EndIf 
					EndIf
					unit.richtung = 47+(unit.anim*8)  
				EndIf 
				If tileost <> 0 And GetUnitPid(unit.xLoc/36+1,unit.yLoc/36) <> MultiplayerPlayerId Then
					 If unit.startzeit+100 <= MilliSecs() Then 
						
						If unit.anim < 4
							unit.startzeit = MilliSecs()
							unit.anim = unit.anim + 1
						Else
							unit.anim = 0
							unit.startzeit = MilliSecs()
							For Local j:tunit = EachIn unitlist
								If j.targetpid = MultiplayerPlayerID And j.targetid = unit.id Then
									unit.hp:- j.dmg
								EndIf 
							Next
						EndIf 
					EndIf
					unit.richtung = 43+(unit.anim*8)
				EndIf 
				If unit.hp <= 0 Then unit.mode = "dead"

			ElseIf unit.mode = "dead" Then 
				 If unit.startzeit+100 <= MilliSecs() Then 
						
					If unit.anim < 1
						unit.startzeit = MilliSecs()
						unit.anim = unit.anim + 1
					Else
						pathmap[unit.xLoc/36][unit.yLoc/36] = walkable
						
						If unit.playerID = MultiplayerPlayerID Then 
							CreateEventObject(unit.playerid,unit.id,"dead")
							unit.mode = "waitfordelete"
						EndIf 
						'New NetObject
					EndIf 
				EndIf
				If unit <> Null Then unit.richtung = 80+(unit.anim*8)
			ElseIf unit.mode = "waitfordelete" Then 
				unit.anim = 2
				If unit.playerID = MultiplayerPlayerID Then 
					DeleteEventObject(unit.playerid,unit.id,"dead",MilliSecs())
				Else
					DeleteRemoteUnitbyEvent(unit.playerid,unit.id,"dead")
				EndIf 
			EndIf 
		EndIf
		'Local t4 = MilliSecs() - t3
		'Print t4+" Per Unit"
	Next
	 Local t2 = MilliSecs() - t1
	Print t2+" Check"
	End Method
	Method UnitCollision()
		For	Local x = 0 To mapwidth*3-1
			For Local y = 0 To mapheight*3-1
				pathmap[x][y] = 0
			Next
		Next
		For unit:tunit = EachIn unitlist
			
			'unit.Zindex = GetZ(unit.xLoc/36,unit.yLoc/36)
			'If unit.playerID = MultiplayerPlayerID Then 
			Select unit.typ
				Case "worker","warrior"
					'If buildmap[unit.lastTileX][unit.lastTileY] = Null Then 'pathmap[unit.lastTileX][unit.lastTileY] = walkable
					'pathmap[unit.lastTileX][unit.lastTileY] = walkable
					If unit.typ = "worker" Then pathmap[unit.xLoc/36][unit.yLoc/36] = 2
					If unit.typ = "warrior" Then pathmap[unit.xLoc/36][unit.yLoc/36] = 3
					
					unit.Zindex = GetZ(Floor(unit.xLoc/36),Floor(unit.yLoc/36))
				Case "building"				
					unit.Zindex = GetZ(unit.buildx,unit.buildy)
				Case "szenerie"
					Select unit.anim
						Case 192
						pathmap[unit.buildx][unit.buildy] = unwalkable
					End Select
					unit.Zindex = GetZ(unit.buildx,unit.buildy)
			End Select
			'EndIf 
		Next
	End Method
	Method BuildBuilding()
		
		If buildmode = True Then
			'Print "buildmode"
			If pickedx > -1 And pickedy > -1 And pickedx < mapwidth*3-4 And pickedy < mapheight*3-4 Then 
				'Print "buildd"
				'Print buildmode 
				Local checktiles = pathmap[pickedX][pickedY] + pathmap[pickedX+1][pickedY] 
				checktiles:+ pathmap[pickedX+2][pickedY] +	pathmap[pickedX+2][pickedY+1] 
				checktiles:+ pathmap[pickedX][pickedY+1] +buildmap[pickedX][pickedY+2] 
				checktiles:+ pathmap[pickedX+1][pickedY+2] +pathmap[pickedX+1][pickedY+1]+buildmap[pickedX+2][pickedY+2]

				If checktiles = 0 Then 
					buildingsiteunable = False	
					'Print "false"
				Else
					buildingsiteunable =True
					'Print "true"
				EndIf 
				If mh1 = True Then 
					If checktiles = 0 Then 	
						
						NewUnit(pickedx,pickedy, "building",1,0,0,MultiplayerPlayerID,pickedx,pickedy)				
						Local anz
						For unit = EachIn Unitlist
							If unit.pathAI = userControlled Then
								anz:+1
								Select anz
									Case 1
										unit.pathStatus = astar.FindPath(unit,pickedx*36+114-72,pickedy*36+114)
										'unit.mode = "building"
									Case 2
										unit.pathStatus = astar.FindPath(unit,pickedx*36+114-144,pickedy*36+114-72)
										'unit.mode = "building"
									Case 3
										unit.pathStatus = astar.FindPath(unit,pickedx*36+114,pickedy*36+114-72)
										'unit.mode = "building"
									Case 4
										unit.pathStatus = astar.FindPath(unit,pickedx*36+114-72,pickedy*36+114-144)
										'unit.mode = "building"
		
								EndSelect
								
							EndIf
						Next
						
						buildmode = False
					Else
					
					EndIf 
				EndIf 
			EndIf 
		EndIf
	End Method
	Method SelectUnits()
		Local wahl
		Local anzwarrior = 0
		Local anzworker = 0
		Local anzbuilding = 0
		Local anzszenerie = 0
		If Not Rectsoverlap(0,604,1024,500,MouseX(),MouseY(),1,1) Then 
			If mh1=True Then 
				selectedUnitsN = 0
				menuid = 0

				If recttmpx = -1 Then recttmpx = MouseX()
				If recttmpy = -1 Then recttmpy = MouseY()
				
				'For Local t:tunit = EachIn unitList
				'	t.pathAI = Null
				'Next
				
			Else 
				If buildmode = 0 Then 
					If md1=True  Then 
						recttmpwidth = MouseX()-recttmpx
						recttmpheight= MouseY()-recttmpy
						recttmpwidthX = MouseX() 
						recttmpheightY = MouseY()
						Mouse1Released =True
					ElseIf md1=False Then 
						
						If Mouse1Released = True Then 
						
							For Local r:tunit = EachIn unitList 
								r.pathAI = Null
							Next
							
							
							For Local r:tunit = EachIn unitList
			
								Local x = recttmpx 
								Local y = recttmpy
								Local w = recttmpwidth 
								Local h = recttmpheight  
														
								If w < 0 And h > 0 Then 
									'Print "F1"
									w = w *-1
									y = MouseY()
									x = MouseX()
									y = y - h
									'Print x+"-"+y+"-"+w+"-"+h
								ElseIf h < 0 And w > 0 Then 
									'Print "F2"
									h = h *-1
									y = MouseY()
									x = MouseX()
									x = x - w
									'Print x+"-"+y+"-"+w+"-"+h
								ElseIf w < 0 And h < 0 Then 
									'Print "F3"
									w = w *-1
									h = h *-1
									x = MouseX()
									y = MouseY()
									'Print x+"-"+y+"-"+w+"-"+h
								EndIf 
								
								Local px = ((r.xLoc/(tilesize) - r.yLoc/(tilesize)) * (tilesizeW/2) ) + mapX
								Local py = ((r.xLoc/(tilesize) + r.yLoc/(tilesize)) * (tilesizeH/2) ) + mapY -tilesizeH/2
								
								Local rwidth 
								Local rheight
								Local xlength
								Local ylength 
								Local verschy
								
								Select r.typ
									Case "warrior"
									rwidth = 15
									rheight= 15
									xlength= 30
									ylength= 30
									Case "worker"
									rwidth = 15
									rheight= 15
									xlength= 30
									ylength= 30
									Case "building"
									rwidth = 40
									rheight= 40
									xlength= 80
									ylength= 80
									verschy= 35
									Case "szenerie"
									rwidth = 15
									rheight= 15
									xlength= 30
									ylength= 30
								EndSelect 
								
								
			
								If RectsOverlap(x ,y+verschy ,w ,h ,px-rwidth ,py-rheight,xlength ,ylength )=True Then
										Local anz = 0
									'anz:+1
									'If anz < 9 Then 
									If r.playerID = MultiplayerPlayerID Then 
										Select r.typ
											Case "warrior"
												r.pathAI = userControlled
												'r.mode = "ready"
												anzwarrior:+1
											Case "worker"
												r.pathAI = userControlled
												anzworker:+1
											Case "building"
												r.pathAI = userControlled
												anzbuilding:+1
											Case "szenerie"
												r.pathAI = userControlled
												anzszenerie:+1
											
										EndSelect 
										
										'Print anzwarrior+" "+anzworker+" "+anzbuilding+" "+anzszenerie
										
										
										If anzwarrior > 0 Then wahl = 1
										
										If anzworker > 0 And anzwarrior = 0 Then wahl = 2
										
										If anzworker = 0 And anzwarrior = 0 And anzbuilding > 0 Then wahl = 3
										
										If anzworker = 0 And anzwarrior = 0 And anzbuilding = 0 And anzszenerie > 0 Then wahl = 4
	
										'Print "wahl:"+wahl
	
										
										For Local l:tunit = EachIn unitlist
											If l.pathAI = userControlled Then 
												Select wahl
													Case 1
														If l.typ = "warrior"  And anz < 9  Then
															l.pathAI = userControlled
															anz:+1
														Else
															l.pathAI = Null	
														EndIf
													Case 2
														If l.typ = "worker" And anz < 9  Then
															l.pathAI = userControlled	
															anz:+1
														Else
															l.pathAI = Null	
														EndIf
													Case 3
														If l.typ = "building" And anz < 9  Then
															l.pathAI = userControlled
															anz:+1
														Else
															l.pathAI = Null	
														EndIf
													Case 4
														If l.typ = "szenerie" And anz < 9 Then
															l.pathAI = userControlled
															anz:+1
														Else
															l.pathAI = Null	
														EndIf
												EndSelect
											EndIf 
										Next
										EndIf
									EndIf 
														
								'EndIf
							Next
							Mouse1Released = False
							recttmpx = -1
							recttmpy = -1
							recttmpwidth = 0
							recttmpheight = 0
						EndIf 
					EndIf 
				EndIf 
			EndIf
		EndIf 
	EndMethod 
	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)
						
				'If rectsoverlap(0,0,1024,768,pixX-(tilesizeW*3)/2 + mapX,pixY + mapY-(tilesizeH*3),12,12) Then 
				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),25)
				'EndIf 
			Next
		Next
		
		'Pathmap - Kollisionmap
		If KeyDown(KEY_F4)
			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
					'DrawImage(tiles, (pixX-tilesizeH) + mapX, (pixY-(tilesizeH*3)) + mapY,25)
					If pathmap[x][y] = unwalkable Then 
						SetAlpha .75
						SetColor 255,0,0
						DrawImage(tiles, (pixX-tilesizeH) + mapX, (pixY-(tilesizeH*3)) + mapY,25)
						SetColor 255,255,255
					ElseIf pathmap[x][y] = 2 Then 
						SetAlpha .75
						SetColor 255,128,0
						DrawImage(tiles, (pixX-tilesizeH) + mapX, (pixY-(tilesizeH*3)) + mapY,25)
						SetColor 255,255,255
					ElseIf pathmap[x][y] = 3 Then
						SetAlpha .75
						SetColor 255,0,128
						DrawImage(tiles, (pixX-tilesizeH) + mapX, (pixY-(tilesizeH*3)) + mapY,25)
						SetColor 255,255,255

					EndIf 
					SetAlpha 1 
				Next
			Next
		EndIf
		
		'buildmap Buildingsmap
		If KeyDown(KEY_F4)
			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
					'DrawImage(tiles, (pixX-tilesizeH) + mapX, (pixY-(tilesizeH*3)) + mapY,39)
					If buildmap [x][y] <> 0 Then 
						SetAlpha .75
						'SetColor 255,0,0
						DrawImage(tiles, (pixX-tilesizeH) + mapX, (pixY-(tilesizeH*3)) + mapY,39)
						SetColor 255,255,255
					EndIf
					SetAlpha 1 
				Next
			Next
		EndIf
		
		'Zmap
		If KeyDown(KEY_F5) Then 
			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)
					SetColor 255,0,0
					DrawText zmap[x][y],(pixX+(tilesizeH*8)) + mapX-148, (pixY-(tilesizeH*3)) + mapY +tilesizeH*4 -18
					SetColor 255,255,255
				Next
			Next
		EndIf 
		
		'For Local msg:TGNetObject=EachIn GNetMessages( host )
			'Local typ$=GetGNetString( msg,SLOT_TYPE )
				'If typ$.Contains("schaden") Then
					'Local obj:TGNetObject=GNetMessageObject(msg)
					'If obj.State()<>GNET_CLOSED
						'If typ="hurtme" 
						'	playerScore:+1
						'	SetGNetInt localPlayer,SLOT_SCORE,playerScore
						'EndIf
						'CloseGNetObject obj
					'EndIf
				'EndIf 
		'Next

		
	Local test
		For Local obj:TGNetObject=EachIn GNetObjects( host )
		
			If obj.State()=GNET_CLOSED Continue
			'Local id:Int = GetGNetInt(obj ,SLOT_ID)
			
			If GNetObjectRemote( obj ) Then
				Local typ:String = GetGNetString(obj,SLOT_TYPE)
				If typ <> "player" And typ <> "event" Then 
				 	Local id:Int 
					Local playerid
					id= GetGNetInt( obj,SLOT_ID)
					playerid=GetGNetInt( obj,SLOT_PLAYERID)
					Local CreateNewUnit = False

					For Local n:tunit = EachIn UnitList
						If n.ID = id And n.playerID = playerid Then
						'----------------------------------------------------------------
						'Print "Update Remote Unit"
						'----------------------------------------------------------------
							Local ntyp:String = GetGNetString( obj,SLOT_TYPE )
							Local nZIndex:Int = GetGNetInt( obj,SLOT_Z)
							Local nxLoc:Float = GetGNetFloat( obj,SLOT_X )
							Local nyLoc:Float = GetGNetFloat( obj,SLOT_Y )
							Local nrichtung:Int = GetGNetInt( obj,SLOT_RICHTUNG)
							Local nanim:Int = GetGNetInt( obj,SLOT_ANIM)
							Local nbuildx:Int = GetGNetInt( obj,SLOT_BUILDX)
							Local nbuildy:Int = GetGNetInt( obj,SLOT_BUILDY)
							Local nmode:String = GetGNetString( obj,SLOT_MODE)
							
							Local nhp:Float = GetGNetFloat(obj,SLOT_HP)
							Local nmaxhp:Float = GetGNetFloat(obj,SLOT_MAXHP)
							Local ndmg:Float = GetGNetFloat(obj,SLOT_DMG)
							Local ntargetPid:Int = GetGNetInt(obj,SLOT_TARGETPID)
							Local ntargetid:Int = GetGNetInt(obj,SLOT_TARGETID)
							
							Local nlasttilex:Int = GetGNetInt(obj,SLOT_LASTTILEX)
							Local nlasttiley:Int = GetGNetInt(obj,SLOT_LASTTILEY)
							
							If n.typ.Contains(ntyp) =False Then n.typ = ntyp 'Print "Update Typ"
							If n.ZIndex <> nZIndex Then n.ZIndex = nZIndex 'Print "Update Zindex von Pid:"+playerid+" ID:"+id
							If n.xLoc <> nxLoc Then n.xLoc = nxLoc 'Print "Update xLoc von Pid:"+playerid+" ID:"+id
							If n.yLoc <> nyLoc Then n.yLoc = nyLoc 'Print "Update yLoc von Pid:"+playerid+" ID:"+id
							If n.richtung <> nrichtung Then n.richtung = nrichtung ' Print "Update richtung von Pid:"+playerid+" ID:"+id
							If n.anim <> nanim Then n.anim = nanim 'Print "Update anim von Pid:"+playerid+" ID:"+id
							If n.buildx <> nbuildx Then n.buildx = nbuildx 'Print "Update buildx von Pid:"+playerid+" ID:"+id
							If n.buildy <> nbuildy Then n.buildy = nbuildy 'Print "Update buildy von Pid:"+playerid+" ID:"+id
							'Print n.mode.contains( nmode )
							'Print "'"+n.mode+"'"+"'"+nmode+"'"
							If n.mode.contains( nmode )=0 Then n.mode = nmode 'Print "Update mode von Pid:"+playerid+" ID:"+id+" von:"+nmode+" zu:"+n.mode
							If n.hp <> nhp Then n.hp = nhp 'Print "Update hp von Pid:"+playerid+" ID:"+id
							If n.maxhp <> nmaxhp Then n.maxhp = nmaxhp 'Print "Update maxhp von Pid:"+playerid+" ID:"+id
							If n.dmg <> ndmg Then n.dmg = ndmg  'Print "Update dmg  von Pid:"+playerid+" ID:"+id
							If n.targetPid <> ntargetPid And ntargetPid > 0 Then n.targetPid = ntargetPid 'Print "Update targetpid von Pid:"+playerid+" ID:"+id+" von"+n.targetPid+" zu"+ntargetPid
							If n.targetid <> ntargetid And ntargetid > 0 Then n.targetid = ntargetid 'Print "Update targetid von Pid:"+playerid+" ID:"+id
							If n.lastTileX <> nlasttilex Then n.lastTilex = nlasttilex 
							If n.lastTileY <> nlasttiley Then n.lastTiley = nlasttiley pathmap[n.lasttilex][n.lasttiley] = walkable
							
							CreateNewUnit = False
							Exit
						Else
							'Print "ID:"+id+" PID:"+playerid
							
							CreateNewUnit = True 
						EndIf
					Next
					
					
					If CreateNewUnit = True Then 
						'----------------------------------------------------------------
						'Print "New Unit PID:"
						'----------------------------------------------------------------
						If GetGNetString(obj,SLOT_MODE) <> "waitfordelete" Then
							Local xn:Float = GetGNetFloat( obj,SLOT_X )
							Local yn:Float=GetGNetFloat( obj,SLOT_Y )
							Local typn:String=GetGNetString( obj,SLOT_TYPE )
							Local animn:Int=GetGNetInt( obj,SLOT_ANIM)
							Local richtungn:Int=GetGNetInt( obj,SLOT_RICHTUNG)
							Local zn:Int=GetGNetInt( obj,SLOT_Z)
							Local pidn:Int=GetGNetInt(obj,SLOT_PLAYERID)
							Local bxn:Int=GetGNetInt(obj,SLOT_BUILDX)
							Local byn:Int=GetGNetInt(obj,SLOT_BUILDY)
							Local idn:Int=GetGNetInt( obj,SLOT_ID)
							'Local moden:String=GetGNetString( obj,SLOT_MODE)
						
							NewUnit(xn,yn,typn,animn,richtungn,zn,pidn,bxn,byn,idn)
						EndIf 
					EndIf 
				ElseIf typ = "event" Then 
					Local id:Int 
					Local playerid
					id= GetGNetInt( obj,SLOT_ID)
					playerid=GetGNetInt( obj,SLOT_PLAYERID)
					For Local e:tUnit = EachIn Unitlist
						If e.playerid = playerid And e.id = id Then 
							'DELETE
						EndIf 	
					Next
				EndIf 
			Else
				Local typ:String = GetGNetString(obj,SLOT_TYPE)
				If typ <> "player" And typ <> "event" Then
					Local id:Int
					id= GetGNetInt( obj,SLOT_ID)	
					For Local n:tunit = EachIn UnitList
						If n.ID = id And n.playerID = MultiplayerPlayerID Then
							'----------------------------------------------------------------
							'Print "Update Lokal Unit"
							'----------------------------------------------------------------
							
							SetGNetInt obj,SLOT_Z,n.Zindex
							SetGNetFloat obj,SLOT_X,n.xLoc
							SetGNetFloat obj,SLOT_Y,n.yLoc
							SetGNetInt obj,SLOT_RICHTUNG,n.richtung
							SetGNetInt obj,SLOT_ANIM,n.anim
							SetGNetInt obj,SLOT_PLAYERID,n.playerID
							SetGNetInt obj,SLOT_BUILDX,n.buildx
							SetGNetInt obj,SLOT_BUILDY,n.buildy
							SetGNetString obj,SLOT_MODE,n.mode
							SetGNetFloat obj,SLOT_HP,n.hp
							SetGNetFloat obj,SLOT_MAXHP,n.maxhp
							SetGNetFloat obj,SLOT_DMG,n.dmg
							SetGNetInt obj,SLOT_TARGETPID,n.targetpid
							SetGNetInt obj,SLOT_TARGETID,n.targetid
							SetGNetInt obj,SLOT_LASTTILEX,n.lastTileX
							SetGNetInt obj,SLOT_LASTTILEY,n.lastTileY
							
							
						EndIf
					Next
				ElseIf typ = "event" Then 
					
				EndIf 
			EndIf  
		Next
		
		'For Local hh:Object = EachIn Uliste
		'	ListRemove(Uliste, hh) 
		'Next

		SortList(UnitList,True,Tunit.compare_z)
		For Local unit:Tunit = EachIn Unitlist
			Local px = ((unit.xLoc/(tilesize) - unit.yLoc/(tilesize)) * (tilesizeW/2) ) + mapX
			Local py = ((unit.xLoc/(tilesize) + unit.yLoc/(tilesize)) * (tilesizeH/2) ) + mapY -tilesizeH/2
			If rectsoverlap(0,0,1024,768,px-6,py-6,12,12) Then 
									
				Select unit.typ
				Case "szenerie"
					DrawImage tiles,px-tilesizeh*2,py-tilesizeW*2,unit.anim'r.xLoc+mapX,r.yLoc+mapY,r.anim
					If KeyDown(KEY_F4) Then DrawText "UID:"+unit.ID+" PID:"+unit.PlayerID,px,py							
												'DrawText TileX(px, py, tilesizeW, tilesizeH, mapX, mapY)+"|"+TileY(px, py, tilesizeW, tilesizeH, mapX, mapY),px+108,py
				Case "building"
					DrawImage buildingssprite,px-tilesizeh*2,py-tilesizeW*2,unit.anim
					If KeyDown(KEY_F4) Then DrawText "UID:"+unit.ID+" PID:"+unit.PlayerID+"X:"+unit.xLoc+" Y:"+unit.yLoc,px,py							
												'DrawText "BP:"+r.buildpoints,px-tilesizeh*2,py-tilesizeW*2
				Case "worker"	
					unit.Update()

					DrawImage workersprite,px,py,unit.richtung
					'DrawText "HP"+unit.hp+" Mode:"+unit.mode	,px,py
					
				
					'DrawText r.mode+" "+r.pathStatus+" TX:"+r.TargetX+" TY:"+r.TargetY,5,test
					If KeyDown(KEY_F4) Then DrawText "UID:"+unit.ID+" PID:"+unit.PlayerID+" UPS:"+unit.pathStatus,px,py
				Case "warrior"
					unit.Update()
					Rem
					Select unit.PlayerId
					Case 1
						SetColor 255,0,0
					Case 2
						SetColor 0,255,0
					Case 3
						SetColor 0,0,255
					Case 4
						SetColor 128,128,0
					Case 5
						SetColor 179,0,89
					EndSelect
					EndRem
					DrawImage warriorsprite,px,py,unit.richtung
					'SetColor 255,255,255
					'DrawText "HP"+unit.hp+" Mode:"+unit.mode	,px,py
					'DrawText "LTX:"+unit.lastTilex+" LTY:"+unit.lastTiley,px,py	
					'DrawText "UID:"+unit.ID+" PID:"+unit.PlayerID+" Mode:"+unit.mode,px,py+12	
					'DrawText "tpid:"+unit.targetpid+" tid:"+unit.targetid+" HP:"+unit.hp+" Anim:"+unit.anim+" X:"+unit.xLoc+" Y:"+unit.yLoc,px,py+16						
				'DrawText TileX(px, py, tilesizeW, tilesizeH, mapX, mapY)+"|"+TileY(px, py, tilesizeW, tilesizeH, mapX, mapY),px,py
				'DrawText r.mode+" "+r.pathStatus+" TX:"+r.TargetX+" TY:"+r.TargetY,5,test
				End Select
			EndIf
		Next
		
		DrawChat()
		
		'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),isomapL2[x][y])
				'If pathmap[x][y] = unwalkable Then 
					'DrawImage(tiles, pixX-tilesizeW/2 + mapX, pixY + mapY-(tilesizeH),15)
				'EndIf 
			'Next
		'Next

		
		'Tile Annzeige
		If collision = False Then 
			DrawImage(tiles, PixelX(pickedXTile, pickedYTile, tilesizeW*3)-tilesizeW*3/2 + mapX, PixelY(pickedXTile, pickedYTile, tilesizeH*3) + mapY-(tilesizeH*3),15)
 		Else
			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 pickedX > 0 And pickedY > 0 And pickedX <= mapwidth*3-4 And pickedY <= mapheight*3-4 And buildmode =True Then 
			
			pixX = PixelX(pickedX, pickedY, tilesizeW)
			pixY = PixelY(pickedX, pickedY, tilesizeH)
			If buildingsiteunable = True Then SetColor 255,0,0 Else SetColor 0,255,0			
			DrawImage buildingssprite,pixX-(tilesizeW*3)/2 + mapX+tilesizeW/2, pixY + mapY-(tilesizeH*3)+tilesizeH*1.5,nextbuilding+1
			SetColor 255,255,255
		EndIf 
		
		'ui base
		DrawImage ui,0,0
		
		'Minimap
		For Local x = 0 To mapwidth*3-3 'Draw the walls And the grid 
			For Local y = 0 To mapheight*3-3
				
				DrawImage tilesminimap,x+8,y+610,isomapL1[x/3][y/3]
				'If pathmap[x][y] = unwalkable Then DrawImage wallBlock,x,y
				
			Next
		Next
		
		'Units on Minimap
		For Local r:tunit = EachIn unitList
			DrawImage unitspriteminimap,((r.xLoc/tilesizeW))+8,((r.yLoc/(tilesizeH*2)))+610
		Next
		
		'ui over
		SetAlpha .25
		DrawImage uiover,0,0
		SetAlpha 1
		
				
	End Method
	Method GetZ(x:Int,y:Int)
		Return zmap[x][y]
	End Method
	Method SelectedUnits()
		Local versch=0
		Local listx=140
		Local listy=637
		Local selectedtypeN
		
		For unit = EachIn unitList
			If unit.pathAI = userControlled Then 
				'Optionen
								
				'Menü rechts
				Select unit.typ
					Case "worker"
						Select menuid 
						Case 0
							DrawImage slotbuild,867,611,0
							
						Case 10
						
							DrawImage slotbuild,867,611,1
							DrawImage slotbuild,916,611,2
							DrawImage slotbuild,965,611,3
							
							DrawImage slotbuild,867,660,4
							DrawImage slotbuild,916,660,5
							DrawImage slotbuild,965,660,6
							
							DrawImage slotbuild,867,709,7
							DrawImage slotbuild,916,709,8


							If RectsOverLap(867,611,48,48,MouseX(),MouseY(),1,1) Then
								If mh1=True Then
									nextbuilding = 1
									buildmode = True 
								EndIf
							EndIf 
							If RectsOverLap(916,611,48,48,MouseX(),MouseY(),1,1)  Then
								If mh1=True Then
									nextbuilding = 2
									buildmode = True  
								EndIf 
							EndIf 
							If RectsOverLap(965,611,48,48,MouseX(),MouseY(),1,1) Then
								If mh1=True Then
									nextbuilding = 3
									buildmode = True  
								EndIf 
							EndIf 
							If RectsOverLap(867,660,48,48,MouseX(),MouseY(),1,1)  Then
								If mh1=True Then
									nextbuilding = 4
									buildmode = True 
								EndIf  
							EndIf 
							If RectsOverLap(916,660,48,48,MouseX(),MouseY(),1,1) Then
								If mh1=True Then
									nextbuilding = 5
									buildmode = True  
								EndIf 
							EndIf 
							If RectsOverLap(965,660,48,48,MouseX(),MouseY(),1,1) Then
								If mh1=True Then
									nextbuilding = 6
									buildmode = True  
								EndIf 
							EndIf 
							If RectsOverLap(867,709,48,48,MouseX(),MouseY(),1,1) Then
								If mh1=True Then
									nextbuilding = 7
									buildmode = True 
								EndIf 
							EndIf  
							If RectsOverLap(916,709,48,48,MouseX(),MouseY(),1,1) Then
								If mh1=True Then
									nextbuilding = 8
									buildmode = True 
								EndIf 
							EndIf
						
						End Select 
						
						
						
						If RectsOverLap(867,611,48,48,MouseX(),MouseY(),1,1) And menuid = 0 Then
							If mh1=True Then
							For Local r:tunit = EachIn unitList
								If r.pathAI = userControlled Then 
									If r.typ = "worker" Then 
										r.pathAI = userControlled 
										menuid = 10
									Else
										r.pathAI = Null
									EndIf 
								EndIf 
							Next
							EndIf 
						EndIf 
					Case "warrior"
					Case "building"
						Select unit.anim
							Case 2 'towncenter
								DrawImage slotbuild,867,611,0
								If RectsOverLap(867,611,48,48,MouseX(),MouseY(),1,1) Then
									If mh1 = True Then
										NewUnit(unit.xLoc-72,unit.yLoc,"worker",0,0,0,MultiplayerPlayerID,0,0)
									EndIf 
								EndIf 

							Case 6	'barracks
								DrawImage slotbuild,867,611,0
								If RectsOverLap(867,611,48,48,MouseX(),MouseY(),1,1) Then
									If mh1 = True Then
										NewUnit(unit.xLoc-72,unit.yLoc,"warrior",0,0,0,MultiplayerPlayerID,0,0)
									EndIf
								EndIf 
						End Select 
								
				End Select
				
				'Units anzeige
				versch:+52
				DrawImage slotsprite,listx+versch-18,listy-26
				SetColor 0,255,0
				DrawBox(listx+versch-14,listy-22,45,45)
				Local br:Float = (unit.maxhp/unit.hp)
				Local breite:Float = (41/br)
				If unit.hp > ((unit.maxhp/4)*3) Then SetColor 51,204,51
				'If unit.hp < (unit.maxhp/2) Then SetColor 128,128,0
				'If unit.hp < (unit.maxhp/4) Then SetColor 255,0,0
				DrawRect(listx+versch-12,listy-20,breite,5)
				SetColor 254,254,254
				DrawBox(listx+versch-13,listy-21,43,7)
				SetColor 255,255,255
				Select unit.typ
					Case "worker"
						DrawImage workersprite,listx+versch+8,listy+2,4
					Case "warrior"
						DrawImage warriorsprite,listx+versch+8,listy+2,4
					Case "szenerie"
						DrawImageRect tiles,listx+versch-9,listy-19,42,42,unit.anim
					Case "building"
						DrawImageRect buildingssprite,listx+versch-9,listy-16,36,36,unit.anim
				End Select
				
				SetColor 255,255,255
				'70 719
				'unit anwählen
				If RectsOverlap(listx+versch-14,listy-22,45,45,MouseX(),MouseY(),1,1) Then
					If mh1 = True Then
						For Local r:tunit = EachIn unitList
							r.pathAI = Null
						Next
						unit.pathAI = userControlled 
					EndIf 
				EndIf
			EndIf 
		Next
	End Method
	Method MoveUnits() 'This Function performs pathfinding And moves the units.
		If Started = True 	
			For unit = EachIn unitList
				UpdatePath(unit)
				If unit.pathStatus = found Then 
					unit.lastTileX = unit.xLoc/36
					unit.lastTileY = unit.yLoc/36
					MoveUnit(unit) 
					Select unit.mode
						Case "ready"
								unit.mode = "walking"
						Case "walking"
							If unit.pathStatus = 0 Then 
								unit.mode = "ready"
							EndIf 
						Case "building"
					End Select
				Else 
					Select unit.typ
						Case "worker"
							'unit.anim = 0
						Case "warrior"
						 	'unit.anim = 0
						Case "szenerie"
							unit.anim = unit.anim  
					End Select
				EndIf 
			Next
		EndIf
	End Method 
	Method UpdatePath(unit:Tunit) 'This Function checks For path updates And calls the FindPath() Function when needed.
		If unit.pathAI = userControlled Then'If the unit is the smiley, trigger New paths using the mouse
		
			If movingunits = True Then 
				'Print movingmap 
				movingunits = False
				If pickedX >= 1 And pickedY >= 1 And pickedX <= mapwidth*3-2 And pickedY <= mapheight*3-2 Then	
					selectedUnitsN =0
					For unit = EachIn unitList
						If unit.pathAI = userControlled Then 
							If unit.typ <> "szenerie" And unit.typ <> "building" Then 
								selectedUnitsN:+1	
								Local x = pickedX*tilesize
								Local y = pickedY*tilesize
								
								Select selectedUnitsN
									Case 1
										unit.pathStatus = astar.FindPath(unit,x,y)
									Case 2
										unit.pathStatus = astar.FindPath(unit,x+tilesize,y)
									Case 3
										unit.pathStatus = astar.FindPath(unit,x-tilesize,y)
									Case 4
										unit.pathStatus = astar.FindPath(unit,x,y+tilesize)
									Case 5
										unit.pathStatus = astar.FindPath(unit,x,y-tilesize)
									Case 6
										unit.pathStatus = astar.FindPath(unit,x+tilesize,y+tilesize)
									Case 7
										unit.pathStatus = astar.FindPath(unit,x-tilesize,y-tilesize)
									Case 8
										unit.pathStatus = astar.FindPath(unit,x+tilesize,y-tilesize)
									Case 9
										unit.pathStatus = astar.FindPath(unit,x-tilesize,y+tilesize)
								End Select
								
								Select unit.mode
									Case "building"
										unit.mode = "ready"
									Case "ready"
										If unit.typ = "worker" Then
											If pathmap[pickedX][pickedY] = unwalkable
												If buildmap[pickedX][pickedY] <> Null Then 
													Local no = buildmap[pickedX][pickedY-1]
													Local so= buildmap[pickedX][pickedY+1]
													Local we= buildmap[pickedX-1][pickedY]
													Local os= buildmap[pickedX+1][pickedY]
													
													Local center= buildmap[pickedX+1][pickedY]

													
													'Print no+" "+so+" "+we+" "+os
													
													If no = 0 Then 
													'Print "NO"
														unit.pathStatus = astar.FindPath(unit,x,y-tilesize*1)
													EndIf
													If so = 0 Then
													'Print "SO" 
														unit.pathStatus = astar.FindPath(unit,x,y+tilesize*1)
													EndIf
													If we = 0 Then
													'Print "WE" 
														unit.pathStatus = astar.FindPath(unit,x-tilesize*1,y)
													EndIf
													If os = 0 Then
													'Print "OS" 
														unit.pathStatus = astar.FindPath(unit,x+tilesize*1,y)
													EndIf
													
													If center <> 0 And os <> 0 And we <> 0 And os <> 0 Then
														unit.pathStatus = astar.FindPath(unit,x+tilesize*2,y)
													EndIf 

												EndIf 
											EndIf 
										EndIf
								EndSelect 
							EndIf
						EndIf 
					Next
				EndIf 
			EndIf 
		Else If unit.pathAI = chase	'Step Until it reaches the smiley. If smiley And chaser aren't at same loc. on screen And no active path, find a New path.
			If Not (unit.xLoc = unit.target.xLoc And unit.yLoc = unit.target.yLoc) And unit.target.xLoc > 0 And unit.target.yLoc > 0 'If not generating pad, generate one. Update it when 'the chaser reaches its third Step on path.	
			If unit.pathStatus <> found Or unit.pathlocation = 3  
				unit.pathStatus = astar.FindPath(unit,unit.target.xLoc,unit.target.yLoc)
			End If
			End If	
		EndIf  
	End Method
	Method UpdatePaths(unit:Tunit) 'This Function checks For path updates And calls the FindPath() Function when needed.
		If unit.pathAI = userControlled  Then'If the unit is the smiley, trigger New paths using the mouse
			If mh2 = True Then
				 
				If movingmap = False Then 
					If pickedX >= 0 And pickedY >= 0 And pickedX <= mapwidth*3-2 And pickedY <= mapheight*3-2 Then
						unit.pathStatus = astar.FindPath(unit,pickedX*tilesize,pickedY*tilesize)
					EndIf 					 
				EndIf 
			EndIf  
		Else If unit.pathAI = chase	'Step Until it reaches the smiley. If smiley And chaser aren't at same loc. on screen And no active path, find a New path.
			If Not (unit.xLoc = unit.target.xLoc And unit.yLoc = unit.target.yLoc) 'If not generating pad, generate one. Update it when 'the chaser reaches its third Step on path.	
			If unit.pathStatus <> found Or unit.pathlocation = 3 
 
				unit.pathStatus = astar.FindPath(unit,unit.target.xLoc,unit.target.yLoc)
			End If
			End If	
		End If 
	End Method

	Method  MoveUnit(unit:Tunit)'This Function moves the sprites around on the screen.
		astar.CheckPathStepAdvance(unit) 'Check For pathStep advances.
		Local xVector# = unit.xPath-unit.xLoc  
		Local yVector# = unit.yPath-unit.yLoc  
		Local angle# = ATan2(yVector#,xVector#)
		Local xSpeed#= Cos(angle)*unit.speed 
		Local ySpeed#= Sin(angle)*unit.speed
		'animation
		'Print xSpeed+"|"+ySpeed
		If unit.pathStatus = Started Then 
			If unit.startzeit+100 <= MilliSecs() Then 
				If unit.anim < 4
					unit.startzeit = MilliSecs()
					unit.anim = unit.anim + 1
				Else
					unit.anim = 0
				EndIf 
			EndIf
		Else
			unit.anim = 0
		EndIf 		
		If xSpeed > 0 And ySpeed <= 0 Then unit.richtung = 3+(unit.anim*8)
		
		If xSpeed > 0 And ySpeed > 0 Then unit.richtung = 6+(unit.anim*8)
		If xSpeed > 0 And ySpeed < 0 Then unit.richtung = 1+(unit.anim*8)
		If xSpeed < 0 And ySpeed > 0 Then unit.richtung = 7+(unit.anim*8)
		
		If xSpeed < 0 And ySpeed < 0 And xSpeed = ySpeed Then unit.richtung = 0+(unit.anim*8)
		If xSpeed > 0 And ySpeed > 0 And xSpeed = ySpeed Then unit.richtung = 4+(unit.anim*8)
		
		If xSpeed < 0 And ySpeed > 0 And xSpeed <> ySpeed Then unit.richtung = 5+(unit.anim*8)
		If xSpeed > 0 And ySpeed < 0 And xSpeed <> ySpeed Then unit.richtung = 2+(unit.anim*8)
		
		If Abs(unit.xLoc - unit.xPath) < Abs(xSpeed)  unit.xLoc = unit.xPath Else unit.xLoc = unit.xLoc + xSpeed 
		If Abs(unit.yLoc - unit.yPath) < Abs(ySpeed)  unit.yLoc = unit.yPath Else unit.yLoc = unit.yLoc + ySpeed
	End Method
	' 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)

			For Local x:Int = 0 To mapwidth-1
				For Local y:Int = 0 To mapheight-1
					isomapL1[x][y] = ReadInt(file)
					'Print isomapL1[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] = 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)
						If isomapL2[x][y] = 192 Then 
							Local r:tunit = New tUnit	
							
							game.NewUnit(x,y+1, "szenerie",192,0,0,0,x,y)	
							
						EndIf
					
				Next
			Next
			

			CloseFile(file)
		EndIf 
	EndIf 
	Return True 
End Function

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 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
