#Inclib "./perso3D_chung_dll"
Declare Function getperso Cdecl Alias "getperso" (iperso As uint) As Any Ptr
Declare Sub selectperso Alias "selectperso" (iperso As uint) 
Declare Sub resetanim Alias "resetanim" () 
Declare Sub newanim Alias "newanim" (nameframe As ZString Ptr,dt As Double,scale As Single=1)
Declare Sub addanim Alias "addanim" (nameframe As ZString Ptr,dt As Double,scale As Single=1)
Declare Function loadperso Cdecl Alias "loadperso" (filename As ZString Ptr,jpg As ZString Ptr=0) As uint
Declare Function loadpersosmooth Cdecl Alias "loadpersosmooth" (filename As ZString Ptr,jpg As ZString Ptr=0) As uint
Declare Sub deleteperso Alias "deleteperso" (iperso As uint) 
Declare Sub drawperso Alias "drawperso" (iperso As uInt)
Declare Sub drawpersotest Alias "drawpersotest" (iperso As uint) 
Declare Sub initweapons Alias "initweapons" () 
Declare Sub selectweapon Alias "selectweapon" (iweapon As integer) 
Declare Sub selectanim3D Alias "selectanim3D" (anim As Any Ptr) 
Declare Sub copyanim3D Alias "copyanim3D" (animsource As any Ptr,animdest As Any Ptr)
Declare Sub saveanim3D Alias "saveanim3D" (fic As String) 
Declare Sub loadanim3D Alias "loadanim3D" (fic As String)  
Declare Sub enablecustomanim3D Alias "enablecustomanim3D" ()  
Declare Sub disablecustomanim3D Alias "disablecustomanim3D" ()  

Type frame 
Dim as Single body1o1,body1o2,body1o3
Dim as Single body2o1,body2o2,body2o3
Dim as Single heado1,heado2,heado3
Dim as Single arm1o1,arm1o2,arm1o3
Dim as Single arm2o1,arm2o2,arm2o3
Dim as Single weapono1,weapono2,weapono3
Dim as Single arm1Lo1,arm1Lo2,arm1Lo3
Dim as Single arm2Lo1,arm2Lo2,arm2Lo3
Dim as Single leg1o1,leg1o2,leg1o3
Dim as Single leg2o1,leg2o2,leg2o3
Dim as Single leg1Lo1,leg1Lo2,leg1Lo3
Dim as Single leg2Lo1,leg2Lo2,leg2Lo3
Dim As Single height
Dim as Single jawo1,jawo2,jawo3
Dim as Single tail1o1,tail1o2,tail1o3
Dim as Single tail2o1,tail2o2,tail2o3
End Type
Const As Integer nframe=70
Type anim3D
Dim As String framename(nframe) 
Dim As frame  frames(nframe)
End Type
Const As Integer nanim=7
Type perso3D
Dim As String  personame
Dim  As Single x,y,z,o1,o2,o3,vie,scalex,scaley,scalez  
Dim  As Double tidle,tforward,trear,tjump,tleft,twalk,twalk2,tback,tback2
Dim  As Double trun,trun2,tidle2,tattack,tblood
Dim  As UInteger texture,body1list,body2list,headlist,leg1list,leg2list
Dim  As UInteger arm1list,arm2list,weaponlist,shieldlist,jawlist,tail1list
Dim  As UInteger tail2list
Dim  As Single body1scale=1,body2scale=1,headscale=1,leg1scale=1,leg2scale=1
Dim  As Single arm1scale=1,arm2scale=1,weaponscale=1,shieldscale=1
Dim  As Single jawscale=1,tail1scale=1,tail2scale=1
Dim  As Single height,leg1Lscale,leg2Lscale,arm1Lscale,arm2Lscale 
Dim  As Single body1x,body1y,body1z,body1o1,body1o2,body1o3
Dim  As Single body2x,body2y,body2z,body2o1,body2o2,body2o3
Dim  As Single headx,heady,headz,heado1,heado2,heado3
Dim  As Single leg1x,leg1y,leg1z,leg1o1,leg1o2,leg1o3
Dim  As Single leg2x,leg2y,leg2z,leg2o1,leg2o2,leg2o3
Dim  As Single arm1x,arm1y,arm1z,arm1o1,arm1o2,arm1o3
Dim  As Single arm2x,arm2y,arm2z,arm2o1,arm2o2,arm2o3
Dim  As Single leg1Lx,leg1Ly,leg1Lz,leg1Lo1,leg1Lo2,leg1Lo3
Dim  As Single leg2Lx,leg2Ly,leg2Lz,leg2Lo1,leg2Lo2,leg2Lo3
Dim  As Single arm1Lx,arm1Ly,arm1Lz,arm1Lo1,arm1Lo2,arm1Lo3
Dim  As Single arm2Lx,arm2Ly,arm2Lz,arm2Lo1,arm2Lo2,arm2Lo3
Dim  As Single weaponx,weapony,weaponz,weapono1,weapono2,weapono3
Dim  As Single shieldx,shieldy,shieldz,shieldo1,shieldo2,shieldo3
Dim  As Single jawx,jawy,jawz,jawo1,jawo2,jawo3
Dim  As Single tail1x,tail1y,tail1z,tail1o1,tail1o2,tail1o3
Dim  As Single tail2x,tail2y,tail2z,tail2o1,tail2o2,tail2o3
Dim  As Single weapondx,weapondy,weapondz,weaponx1,weapony1,weaponz1
Dim  As Single weapondx2,weapondy2,weapondz2,weaponx2,weapony2,weaponz2
Dim  As Single headx1,heady1,headz1,shieldx1,shieldy1,shieldz1
Dim  As Single jawx1,jawy1,jawz1
Dim  As Double  tanim(nanim,20),tfade=0.35
Dim  As integer animframe(nanim,20),ianimframe(nanim),ianim,weapon,iperso
Dim  As Single  kanim(nanim,20)
Dim As frame   currframe,frame0
End Type 
Dim Shared As anim3D Ptr curranim3D
Dim Shared As perso3D Ptr currperso
Dim Shared As Integer anim=1
Dim Shared As Single vie0=100
Sub walk()
With *currperso
		If Timer>(.twalk+1.6) Then
			.twalk=Timer:.twalk2=Timer+1.6
		   newanim("walk1",0.4)
	      addanim("walk2",0.4)
		ElseIf Timer>(.twalk+0.8) And Timer<(.twalk+1.6) And Timer<.twalk2 Then     
	      .twalk2=0
	      newanim("walk3",0.4) 
	      addanim("walk4",0.4) 
		EndIf
End With 	
End Sub
Sub back()
With *currperso
		If Timer>(.tback+1.6) Then
			.tback=Timer:.tback2=Timer+1.6
		   newanim("walk4",0.4)
	      addanim("walk3",0.4)
		ElseIf Timer>(.tback+0.8) And Timer<(.tback+1.6) And Timer<.tback2 Then     
	      .tback2=0
	      newanim("walk2",0.4) 
	      addanim("walk1",0.4) 
		EndIf	
End With
End Sub
Declare Sub jump()
Sub attack1()
With *currperso
		   newanim("attack1",0.22)
	      addanim("attack2",0.22) 
	      addanim("attack2",0.05)	
End With
If Rnd<0.27 Then jump()
End Sub
Sub attack2()
With *currperso
		   newanim("attack3",0.22)
	      addanim("attack4",0.22) 
	      addanim("attack4",0.05)	
End With
If Rnd<0.27 Then jump()
End Sub
Sub attack3()
With *currperso
		   newanim("attack5",0.22)
	      addanim("attack6",0.22) 
	      addanim("attack6",0.05)	
End With
If Rnd<0.27 Then jump()
End Sub
Sub animrun()
With *currperso
		If Timer>(.trun+0.8) Then
			.trun=Timer:.trun2=Timer+0.8
		   newanim("run1",0.2)
	      addanim("run2",0.2)
		ElseIf Timer>(.trun+0.4) And Timer<(.trun+0.8) And Timer<.trun2 Then     
	      .trun2=0
	      newanim("run3",0.2) 
	      addanim("run4",0.2) 
		EndIf	
End With
End Sub
Sub defend1()
With *currperso
Dim As Integer i
For i=1 To nanim
  If curranim3D->framename(.animframe(i,1))="defend1" Then
 	  .ianim=i-1:If .ianim<1 Then .ianim=nanim
     newanim("defend1",0.0001)
     addanim("defend1",0.3)
     addanim("stand1",0.1) 
     Exit Sub 	
  EndIf 	
Next
newanim("defend1",0.1)
addanim("defend1",0.3)
addanim("stand1",0.1) 	
End With
End Sub
Sub jump()
With *currperso
		   newanim("run5",0.32)
		   addanim("run5",0.07)
	      addanim("run6",0.32) 	
End With
End Sub
Sub turn()
With *currperso
		   newanim("walk5",0.25)
	      addanim("walk6",0.25) 	
End With
End Sub
Dim Shared As Integer tforward
Sub ride()
With *currperso
Dim As Integer i
For i=1 To nanim
  If curranim3D->framename(.animframe(i,1))="stand4" Then
 	  .ianim=i-1:If .ianim<1 Then .ianim=nanim
     If tforward=2 Then 
       newanim("stand4",0.0001)
       addanim("stand4",0.25) 
       addanim("stand5",0.5) 
       addanim("stand4",0.25) 	
     Else
       newanim("stand4",0.0001)
       addanim("stand4",1) 
       addanim("stand5",2) 
       addanim("stand4",1) 	
     EndIf 	
     Exit Sub 	
  EndIf 	
Next
If tforward=2 Then 
  newanim("stand4",0.01)
  addanim("stand4",0.25) 	
  addanim("stand5",0.5)
  addanim("stand4",0.25) 	
Else
  newanim("stand4",0.01)
  addanim("stand4",1) 	
  addanim("stand5",2)
  addanim("stand4",1) 	
EndIf     
End With
End Sub
Sub dead()
With *currperso
Dim As Integer i
For i=1 To nanim
  If curranim3D->framename(.animframe(i,1))="stand3" Then
     Exit Sub 	
  EndIf 	
Next
newanim("stand3",1)     
addanim("stand3",4000)
End With
End Sub
Dim Shared As Single degtorad=ASin(1)/90,radtodeg=90/ASin(1)
Function diro10(dx As Single,dy As Single)As Single 
Dim As Single do1
If Abs(dx)>max(0.001,0.001*Abs(dy)) Then
   If dx>0 Then
   	do1=Atn(dy/dx)*radtodeg
   Else
   	do1=180-Atn(dy/Abs(dx))*radtodeg
   EndIf
Else
	do1=Sgn(dy)*90
EndIf
Return do1
End Function 
Dim Shared As Double kfps=1
Dim Shared As Single x,y,z,o1
Sub animidle() 
	   With *currperso
	   If Timer>(.tidle2+5) And anim=1 Then
		  If .vie>0 Then
		    .tidle2=Timer 
		    newanim("stand1",2+Rnd)
		    addanim("stand2",2+Rnd)
		  Else
		  	 If Timer>(.tidle2+40) Then .vie=vie0:resetanim()
		  EndIf 	
	   EndIf
	   If .vie>0.01 then
	    If (guitestkey(vk_space) And Rnd<0.03*kfps) Or _  
	          (Rnd<0.02*kfps And max(Abs(.x-x),Abs(.y-y))<100) Then
	   	If Timer>(.tidle+0.4) Then
	   		.tidle=Timer
	   		.o1=diro10(x-.x,y-.y)'+90
	   		If Rnd<0.4 Then
	   			.tattack=Timer 
	   			If Rnd<0.4 Then
	   				attack1()
	   			ElseIf Rnd<0.7 Then
	   				attack2()
	   			Else
	   				attack3()
	   			EndIf
	   		EndIf
	   	EndIf
	    Else
	   	if max(Abs(.x-x),Abs(.y-y))>100 then .o1=o1-90-90
	    EndIf
	   EndIf  
	   End With 	
End Sub
