
//plugin written by sheldon macdonald
//updated sept 1,2015
function initialize_objects(mm)
global max_objects
max_objects=mm
//do not change any values below at this time
dim edit_object1#[2,2]
dim edit_objectn1#[2,2]
dim edit_objectuv1#[2,2]
dim edit_object2[2,2]
global max_vertexcount
global max_normalscount
global max_uvcount
global obj_editcounter=0
max_vertexcount=0
max_normalscount=0
max_uvcount=0

global max_indexcount
max_indexcount=0
global max_limbs
max_limbs=20
global max_keyframes
max_keyframes=83
global limb_start_obj
limb_start_obj=10000
dim object_buffer#[max_objects,20,11,83]
dim object_scale#[max_objects,2]

dim transition#[max_objects,5]
dim object_filenames$[max_objects,max_limbs,1]
//0=filename for obj, 1 is texturename
dim refresh_object[20]
dim objects_playing#[max_objects,6]
dim count_data[20,4]
endfunction

function setobjectspeed(object1,value#)
objects_playing#[object1,4]=value#
endfunction

function moveobjectz(object1,value#)
moveobjectlocalz(object1,value#)
x#=getobjectx(object1)
y#=getobjecty(object1)
z#=getobjectz(object1)
positionobject(object1,0,x#,y#,z#)
endfunction

function moveobjectx(object1,value#)
moveobjectlocalx(object1,value#)
x#=getobjectx(object1)
y#=getobjecty(object1)
z#=getobjectz(object1)
positionobject(object1,0,x#,y#,z#)
endfunction

function moveobjecty(object1,value#)
moveobjectlocaly(object1,value#)
x#=getobjectx(object1)
y#=getobjecty(object1)
z#=getobjectz(object1)
positionobject(object1,0,x#,y#,z#)
endfunction

function setobjectmeshfilename(object1,limb1,file$)
if object1<1 then exitfunction
if object1>max_objects then exitfunction
if limb1<0 then exitfunction
if limb1>max_limbs then exitfunction
object_filenames$[object1,limb1,0]=file$
endfunction

function setindexdata2(index,v1)
if index<0 then exitfunction -1
if index>max_indexcount then exitfunction -1
if v1<0 then exitfunction -1
//if v1>max_vertexcount then exitfunction -1
for s=0 to 2 step 1
edit_object2[index,s]=v1
next s
endfunction 1

function loadobject2(object1,filename$,texture$)
//load it and store source filename
if object1<1 then exitfunction
if object1>max_objects then exitfunction
if getfileexists(filename$)=0 then exitfunction
if getobjectexists(object1)=1 then exitfunction
loadobject(object1,filename$)
if texture$="" then texture$="NULL"
if getfileexists(texture$)=0 then texture$="NULL"
object_filenames$[object1,0,0]=filename$
object_filenames$[object1,0,1]=texture$
if getfileexists(texture$)=1
//load it and apply it
//limb_start_obj+(object1*20)
    limb=0
    if getimageexists(limb_start_obj+(object1*20)+limb)=1 then deleteimage(limb_start_obj+(object1*20)+limb)
    loadimage(limb_start_obj+(object1*20)+limb,texture$,1)
    setobjectimage(object1,limb_start_obj+(object1*20)+limb,0)
endif
endfunction


function setlimbcolor(obj,lmb,r,g,b)
if lmb=0 and getobjectexists(obj)=1 then setobjectcolor(obj,r,g,b,255)
id=obj
//for t=1 to 20 step 1
    //apply shader test
if lmb>0
    obj=limb_start_obj+(id*20)+lmb
    if getobjectexists(obj)=1 
         setobjectcolor(obj,r,g,b,255)
        //setobjectshader(obj,1)
        //setobjectlightmode(obj,1)
    endif
endif

endfunction

function texturemodel(obj,tex)
if getobjectexists(obj)=1 and getimageexists(tex)=1 then setobjectimage(obj,tex,0)
id=obj
//for t=1 to 20 step 1
    //apply shader test
for lmb=1 to 20 step 1
    obj=limb_start_obj+(id*20)+lmb
    if getobjectexists(obj)=1 and getimageexists(tex)=1 
         setobjectimage(obj,tex,0)
    endif
next lmb

endfunction

function setmodelvisible(obj,value)
if getobjectexists(obj)=1 then setobjectvisible(obj,0)
id=obj
//for t=1 to 20 step 1
    //apply shader test
for lmb=1 to 20 step 1
    obj=limb_start_obj+(id*20)+lmb
    if getobjectexists(obj)=1
         setobjectvisible(obj,value)
    endif
next lmb	
	
	
endfunction


function setmodellightmode(obj,s)
if getobjectexists(obj)=1 then setobjectlightmode(obj,s)
id=obj
//for t=1 to 20 step 1
    //apply shader test
for lmb=1 to 20 step 1
    obj=limb_start_obj+(id*20)+lmb
    if getobjectexists(obj)=1
         setobjectlightmode(obj,s)
    endif
next lmb

endfunction


function setmodelcolor(obj,r,g,b)
if getobjectexists(obj)=1 then setobjectcolor(obj,r,g,b,255)
id=obj
//for t=1 to 20 step 1
//apply shader test
for lmb=1 to 20 step 1
    obj=limb_start_obj+(id*20)+lmb
    if getobjectexists(obj)=1 
         setobjectcolor(obj,r,g,b,255)
        //setobjectshader(obj,1)
        //setobjectlightmode(obj,1)
    endif
next lmb

endfunction
function setobjecttexturefilename(object1,limb1,file$)
if object1<1 then exitfunction
if object1>max_objects then exitfunction
if limb1<0 then exitfunction
if limb1>max_limbs then exitfunction
object_filenames$[object1,limb1,1]=file$
endfunction


function positionobject(object1,lmb,x#,y#,z#)
if object1<0 or object1>max_objects then exitfunction
if getobjectexists(object1)=0 then exitfunction
object_buffer#[object1,lmb,1,0]=x#
object_buffer#[object1,lmb,2,0]=y#
object_buffer#[object1,lmb,3,0]=z#
//update_limbs(object1,20)
endfunction



function linklimb(object1,child,parent)
if getobjectexists(object1)=0 then exitfunction
if getobjectexists(limb_start_obj+(object1*20)+parent)=0 then exitfunction
if getobjectexists(limb_start_obj+(object1*20)+child)=0 then exitfunction
object_buffer#[object1,child,0,0]=parent
endfunction

function setlimbparent(object1,limb1,parent)
object_buffer#[object1,limb1,0,0]=parent	
endfunction

//function getlimbexists(object1,limb1)
//if limb1=0 then exitfunction getobjectexists(object1)
//if limb1>0
//    sobj=limb_start_obj+(object1*20)+limb1
//    exitfunction getobjectexists(sobj)
//endif
//endfunction 0


function getlimbexists(object1,limb1)
if limb1=0 and getobjectexists(object1)=1 then exitfunction 1
if limb1>0
	if getobjectexists(limb_start_obj+(object1*20)+limb1)=1 then exitfunction 1
endif	
endfunction 0

function getlimbcount(object1)
cnt=0
for limb=1 to 20 step 1
if getobjectexists(limb_start_obj+(object1*20)+limb)=1 then cnt=cnt+1
next limb
endfunction cnt

function wrapvalue(a#)
while a#<0 or a#>360
if a#<0 then a#=a#+360
if a#>360 then a#=a#-360
endwhile
endfunction a#

//function wrapvaluex(a#)
////while a#<-90 or a#>90
//if a#<-88 then a#=-88
//if a#>88 then a#=88
////endwhile
////no actual wrap just limits since agk hangs at 90 deg
//endfunction a#

function rotateobject(object1,lmb,x#,y#,z#)
if object1<0 or object1>max_objects then exitfunction
if getobjectexists(object1)=0 then exitfunction
y#=wrapvalue(y#)
x#=wrapvalue(x#)
z#=wrapvalue(z#)
object_buffer#[object1,lmb,4,0]=x#
object_buffer#[object1,lmb,5,0]=y#
object_buffer#[object1,lmb,6,0]=z#
//update_limbs(object1,20)
endfunction

function getlimbangley(object1,limb)
if object1<1 then exitfunction 0.0
if object1>max_objects then exitfunction 0.0
if limb<0 then exitfunction 0.0
if limb>20 then exitfunction 0.0
ret#=object_buffer#[object1,limb,5,0]
endfunction ret#

function getlimbanglex(object1,limb)
if object1<1 then exitfunction 0.0
if object1>max_objects then exitfunction 0.0
if limb<0 then exitfunction 0.0
if limb>20 then exitfunction 0.0
ret#=object_buffer#[object1,limb,4,0]
endfunction ret#

function getlimbanglez(object1,limb)
if object1<1 then exitfunction 0.0
if object1>max_objects then exitfunction 0.0
if limb<0 then exitfunction 0.0
if limb>20 then exitfunction 0.0
ret#=object_buffer#[object1,limb,6,0]
endfunction ret#

function getlimbrotationy(object1,limb)
if object1<1 then exitfunction 0.0
if object1>max_objects then exitfunction 0.0
if limb<0 then exitfunction 0.0
if limb>20 then exitfunction 0.0
obj=limb_start_obj+(object1*20)+limb
ret#=getobjectangley(obj)
endfunction ret#

function getlimbrotationx(object1,limb)
if object1<1 then exitfunction 0.0
if object1>max_objects then exitfunction 0.0
if limb<0 then exitfunction 0.0
if limb>20 then exitfunction 0.0
obj=limb_start_obj+(object1*20)+limb
ret#=getobjectanglex(obj)
endfunction ret#

function getlimbrotationz(object1,limb)
if object1<1 then exitfunction 0.0
if object1>max_objects then exitfunction 0.0
if limb<0 then exitfunction 0.0
if limb>20 then exitfunction 0.0
obj=limb_start_obj+(object1*20)+limb
ret#=getobjectanglez(obj)
endfunction ret#

function getlimbpositionx(object1,limb)
obj=limb_start_obj+(object1*20)+limb
ret#=getobjectx(obj)
endfunction ret#

function getlimbpositiony(object1,limb)
obj=limb_start_obj+(object1*20)+limb
ret#=getobjecty(obj)
endfunction ret#

function getlimbpositionz(object1,limb)
obj=limb_start_obj+(object1*20)+limb
ret#=getobjectz(obj)
endfunction ret#



function getobjectscalex(object1)
ret#=object_scale#[object1,0]+1.0
endfunction ret#

function getobjectscaley(object1)
ret#=object_scale#[object1,1]+1.0
endfunction ret#

function getobjectscalez(object1)
ret#=object_scale#[object1,2]+1.0
endfunction ret#

function setmodelscale(object1,sx#,sy#,sz#)
object_scale#[object1,0]=(sx#-1.0)
object_scale#[object1,1]=(sy#-1.0)
object_scale#[object1,2]=(sz#-1.0)
endfunction


function setobjectscalex(object1,size#)
object_scale#[object1,0]=(size#-1.0)
endfunction

function setobjectscaley(object1,size#)
object_scale#[object1,1]=(size#-1.0)
endfunction

function setobjectscalez(object1,size#)
object_scale#[object1,2]=(size#-1.0)
endfunction


function getlimbpositionxf(object1,limb,frame)
ret#=object_buffer#[object1,limb,1,frame]
endfunction ret#

function getlimbpositionyf(object1,limb,frame)
ret#=object_buffer#[object1,limb,2,frame]
endfunction ret#

function getlimbpositionzf(object1,limb,frame)
ret#=object_buffer#[object1,limb,3,frame]
endfunction ret#

function getlimbrotationxf(object1,limb,frame)
ret#=object_buffer#[object1,limb,4,frame]
endfunction ret#

function getlimbrotationyf(object1,limb,frame)
ret#=object_buffer#[object1,limb,5,frame]
endfunction ret#

function getlimbrotationzf(object1,limb,frame)
ret#=object_buffer#[object1,limb,6,frame]
endfunction ret#


function updateobject(object1,limbcount)
if object1<1 or object1>max_objects then exitfunction
if getobjectexists(object1)=0 then exitfunction
key_frame#=transition#[object1,5]
scx#=object_scale#[object1,0]+1.0
scy#=object_scale#[object1,1]+1.0
scz#=object_scale#[object1,2]+1.0
setobjectposition(object1,object_buffer#[object1,0,1,0],object_buffer#[object1,0,2,0],object_buffer#[object1,0,3,0])
setobjectrotation(object1,object_buffer#[object1,0,4,0],object_buffer#[object1,0,5,0],object_buffer#[object1,0,6,0])
//limb 0 should have no anim data!!
refresh_object[0]=1
rfobject=0
runthrough=0
while rfobject<limbcount
for limb=1 to limbcount step 1
    if runthrough=0
        refresh_object[limb]=0
        if getobjectexists(limb_start_obj+(object1*20)+limb)=0
            refresh_object[limb]=1
            rfobject=rfobject+1
        endif
    endif
    if refresh_object[limb]=0
        parent=object_buffer#[object1,limb,0,0]
        p_updated=refresh_object[parent]
        if p_updated=1 or parent=0
            c_obj=limb_start_obj+(object1*20)+limb
            refresh_object[limb]=1
            p_obj=limb_start_obj+(object1*20)+parent
            if parent=0 then p_obj=object1
            setobjectposition(c_obj,getobjectx(p_obj),getobjecty(p_obj),getobjectz(p_obj))
            setobjectscale(c_obj,scx#,scy#,scz#)
            setobjectrotation(c_obj,getobjectanglex(p_obj),getobjectangley(p_obj),getobjectanglez(p_obj))
            //transition code here
            baseframe#=object_buffer#[object1,limb,1,0]
            frame1=trunc(key_frame#)
            frame2=trunc(key_frame#)+1
            transition1#=key_frame#-trunc(key_frame#)
            
            //color alpha
            if frame1=0 then alpha1#=object_buffer#[object1,limb,7,0]
            if frame1>0 then alpha1#=(object_buffer#[object1,limb,7,0])+(object_buffer#[object1,limb,7,frame1])
			alpha2#=(object_buffer#[object1,limb,7,0])+(object_buffer#[object1,limb,7,frame2])
			alength#=alpha2#-alpha1#
			na#=alpha1#+(alength#*transition1#)
			//r
			if frame1=0 then alpha1#=object_buffer#[object1,limb,8,0]
            if frame1>0 then alpha1#=(object_buffer#[object1,limb,8,0])+(object_buffer#[object1,limb,8,frame1])
			alpha2#=(object_buffer#[object1,limb,8,0])+(object_buffer#[object1,limb,8,frame2])
			alength#=alpha2#-alpha1#
			nr#=alpha1#+(alength#*transition1#)
			//g
			if frame1=0 then alpha1#=object_buffer#[object1,limb,9,0]
            if frame1>0 then alpha1#=(object_buffer#[object1,limb,9,0])+(object_buffer#[object1,limb,9,frame1])
			alpha2#=(object_buffer#[object1,limb,9,0])+(object_buffer#[object1,limb,9,frame2])
			alength#=alpha2#-alpha1#
			ng#=alpha1#+(alength#*transition1#)
			//b
			if frame1=0 then alpha1#=object_buffer#[object1,limb,10,0]
            if frame1>0 then alpha1#=(object_buffer#[object1,limb,10,0])+(object_buffer#[object1,limb,10,frame1])
			alpha2#=(object_buffer#[object1,limb,10,0])+(object_buffer#[object1,limb,10,frame2])
			alength#=alpha2#-alpha1#
			nb#=alpha1#+(alength#*transition1#)
			
			//set new colors + alpha
			if flash<>0 or mylimb<>limb or playing=1 then setobjectcolor(c_obj,nr#,ng#,nb#,na#)
			transp=object_buffer#[object1,limb,11,0]
			setobjecttransparency(c_obj,transp)
			
            if frame1=0 then x1#=object_buffer#[object1,limb,1,0]*scx#
            if frame1>0 then x1#=(object_buffer#[object1,limb,1,0]*scx#)+(object_buffer#[object1,limb,1,frame1]*scx#)
            x2#=(object_buffer#[object1,limb,1,0]*scx#)+(object_buffer#[object1,limb,1,frame2]*scx#)
            lengthx#=x2#-x1#
            xdraw#=x1#+(lengthx#*transition1#)
            if frame1=0 then y1#=object_buffer#[object1,limb,2,0]*scy#
            if frame1>0 then y1#=(object_buffer#[object1,limb,2,0]*scy#)+(object_buffer#[object1,limb,2,frame1]*scy#)
            y2#=(object_buffer#[object1,limb,2,0]*scy#)+(object_buffer#[object1,limb,2,frame2]*scy#)
            lengthy#=y2#-y1#
            ydraw#=y1#+(lengthy#*transition1#)
            if frame1=0 then z1#=object_buffer#[object1,limb,3,0]*scz#
            if frame1>0 then z1#=(object_buffer#[object1,limb,3,0]*scz#)+(object_buffer#[object1,limb,3,frame1]*scz#)
            z2#=(object_buffer#[object1,limb,3,0]*scz#)+(object_buffer#[object1,limb,3,frame2]*scz#)
            lengthz#=z2#-z1#
            zdraw#=z1#+(lengthz#*transition1#)
            moveobjectlocalx(c_obj,xdraw#)
            moveobjectlocaly(c_obj,ydraw#)
            moveobjectlocalz(c_obj,zdraw#)
            if frame1=0 then ax1#=object_buffer#[object1,limb,4,0]
            if frame1>0 then ax1#=(object_buffer#[object1,limb,4,0]+object_buffer#[object1,limb,4,frame1])
            ax2#=(object_buffer#[object1,limb,4,0]+object_buffer#[object1,limb,4,frame2])
            lengthax#=ax2#-ax1#
            axdraw#=(ax1#+(lengthax#*transition1#))
            if frame1=0 then ay1#=object_buffer#[object1,limb,5,0]
            if frame1>0 then ay1#=object_buffer#[object1,limb,5,0]+object_buffer#[object1,limb,5,frame1]
            ay2#=object_buffer#[object1,limb,5,0]+object_buffer#[object1,limb,5,frame2]
            lengthay#=ay2#-ay1#
            aydraw#=wrapvalue(ay1#+(lengthay#*transition1#))
            if frame1=0 then az1#=object_buffer#[object1,limb,6,0]
            if frame1>0 then az1#=object_buffer#[object1,limb,6,0]+object_buffer#[object1,limb,6,frame1]
            az2#=object_buffer#[object1,limb,6,0]+object_buffer#[object1,limb,6,frame2]
            lengthaz#=az2#-az1#
            azdraw#=wrapvalue(az1#+(lengthaz#*transition1#))
            rotateobjectlocalx(c_obj,axdraw#)
            rotateobjectlocaly(c_obj,aydraw#)
            rotateobjectlocalz(c_obj,azdraw#)
            
            
            rfobject=rfobject+1
        endif
    endif
next limb
runthrough=1
endwhile
endfunction

function addlimb(objtoadd,object1)
if getobjectexists(objtoadd)=0 then exitfunction
if getobjectexists(object1)=0 then exitfunction
if object1<1 then exitfunction
if objtoadd<1 then exitfunction
if object1>max_objects then exitfunction
//do a limb count and see if it can be added
for limb=1 to 20 step 1
    cp=limb_start_obj+(object1*20)+limb
    if getobjectexists(cp)=0
        if getlimbcount(object1)=0
            //mt out array completely for clean start
            for lmb=1 to 20 step 1
            for dat=0 to 11 step 1
            for f=1 to 83 step 1
            object_buffer#[object1,lmb,dat,f]=0.0
            next f
            next dat
            next lmb
        endif
        cloneobject(cp,objtoadd)
        //copy filename sources
        object_filenames$[object1,limb,0]=object_filenames$[objtoadd,0,0]
        object_filenames$[object1,limb,1]=object_filenames$[objtoadd,0,1]
        for d=0 to 11 step 1
        object_buffer#[object1,limb,d,0]=0
        next d
        exitfunction
    endif
next limb
endfunction

function setlimbkeyposition(object1,limb,x#,y#,z#,key_frame)
if key_frame<1 or key_frame>max_keyframes then exitfunction
if object1<1 or object1>max_objects then exitfunction
if limb<1 or limb>max_limbs then exitfunction
object_buffer#[object1,limb,1,key_frame]=x#
object_buffer#[object1,limb,2,key_frame]=y#
object_buffer#[object1,limb,3,key_frame]=z#
endfunction

function setlimbkeyrotation(object1,limb,x#,y#,z#,key_frame)
if key_frame<1 or key_frame>max_keyframes then exitfunction
if object1<1 or object1>max_objects then exitfunction
if limb<1 or limb>max_limbs then exitfunction
object_buffer#[object1,limb,4,key_frame]=x#
object_buffer#[object1,limb,5,key_frame]=y#
object_buffer#[object1,limb,6,key_frame]=z#
endfunction

function playobject(object1,startkey,endkey,continous,speed#,limbcount)
//objects_playing[max_objects,6]
objects_playing#[object1,0]=1 //is playing
objects_playing#[object1,1]=startkey
objects_playing#[object1,2]=endkey
objects_playing#[object1,3]=continous
objects_playing#[object1,4]=speed#
objects_playing#[object1,5]=limbcount
transition#[object1,5]=startkey
endfunction

function setobjectframe(object1,frame#)
transition#[object1,5]=frame#
endfunction

function runanimationtransition(object1,startframe,endframe,continous,speed#,limbcount)
//lets writeout to frame 21
//get values to make starting point
key_frame#=transition#[object1,5]

//if key_frame#<0 then key_frame#=0
//if key_frame#>22.0 then key_frame#=22.0
for limb=1 to 20 step 1
            //baseframe#=object_buffer#[object1,limb,1,0]
            frame1=trunc(key_frame#)
            frame2=trunc(key_frame#)+1
            transition1#=key_frame#-trunc(key_frame#)
            if frame1=0 then x1#=object_buffer#[object1,limb,1,0]
            if frame1>0 then x1#=object_buffer#[object1,limb,1,0]+object_buffer#[object1,limb,1,frame1]
            x2#=object_buffer#[object1,limb,1,0]+object_buffer#[object1,limb,1,frame2]
            lengthx#=x2#-x1#
            xdraw#=x1#+(lengthx#*transition1#)
            if frame1=0 then y1#=object_buffer#[object1,limb,2,0]
            if frame1>0 then y1#=object_buffer#[object1,limb,2,0]+object_buffer#[object1,limb,2,frame1]
            y2#=object_buffer#[object1,limb,2,0]+object_buffer#[object1,limb,2,frame2]
            lengthy#=y2#-y1#
            ydraw#=y1#+(lengthy#*transition1#)
            if frame1=0 then z1#=object_buffer#[object1,limb,3,0]
            if frame1>0 then z1#=object_buffer#[object1,limb,3,0]+object_buffer#[object1,limb,3,frame1]
            z2#=object_buffer#[object1,limb,3,0]+object_buffer#[object1,limb,3,frame2]
            lengthz#=z2#-z1#
            zdraw#=z1#+(lengthz#*transition1#)
            //moveobjectlocalx(c_obj,xdraw#)
            //moveobjectlocaly(c_obj,ydraw#)
            //moveobjectlocalz(c_obj,zdraw#)
            if frame1=0 then ax1#=object_buffer#[object1,limb,4,0]
            if frame1>0 then ax1#=object_buffer#[object1,limb,4,0]+object_buffer#[object1,limb,4,frame1]
            ax2#=object_buffer#[object1,limb,4,0]+object_buffer#[object1,limb,4,frame2]
            lengthax#=ax2#-ax1#
            axdraw#=ax1#+(lengthax#*transition1#)
            if frame1=0 then ay1#=object_buffer#[object1,limb,5,0]
            if frame1>0 then ay1#=object_buffer#[object1,limb,5,0]+object_buffer#[object1,limb,5,frame1]
            ay2#=object_buffer#[object1,limb,5,0]+object_buffer#[object1,limb,5,frame2]
            lengthay#=ay2#-ay1#
            aydraw#=ay1#+(lengthay#*transition1#)
            if frame1=0 then az1#=object_buffer#[object1,limb,6,0]
            if frame1>0 then az1#=object_buffer#[object1,limb,6,0]+object_buffer#[object1,limb,6,frame1]
            az2#=object_buffer#[object1,limb,6,0]+object_buffer#[object1,limb,6,frame2]
            lengthaz#=az2#-az1#
            azdraw#=az1#+(lengthaz#*transition1#)
            //rotateobjectlocalx(c_obj,axdraw#)
            //rotateobjectlocaly(c_obj,aydraw#)
            //rotateobjectlocalz(c_obj,azdraw#)
            //if startframe>0.00
                object_buffer#[object1,limb,1,81]=xdraw#-object_buffer#[object1,limb,1,0]
                object_buffer#[object1,limb,2,81]=ydraw#-object_buffer#[object1,limb,2,0]
                object_buffer#[object1,limb,3,81]=zdraw#-object_buffer#[object1,limb,3,0]
                object_buffer#[object1,limb,4,81]=axdraw#-object_buffer#[object1,limb,4,0]
                object_buffer#[object1,limb,5,81]=aydraw#-object_buffer#[object1,limb,5,0]
                object_buffer#[object1,limb,6,81]=azdraw#-object_buffer#[object1,limb,6,0]
            //endif
            //if startframe=0.0
            //    object_buffer#[object1,limb,1,31]=0
            //    object_buffer#[object1,limb,2,31]=0
            //    object_buffer#[object1,limb,3,31]=0
            //    object_buffer#[object1,limb,4,31]=0
            //    object_buffer#[object1,limb,5,31]=0
            //    object_buffer#[object1,limb,6,31]=0
            //endif

            //copy frame in
            //if startframe>0
                object_buffer#[object1,limb,1,82]=object_buffer#[object1,limb,1,startframe+1]
                object_buffer#[object1,limb,2,82]=object_buffer#[object1,limb,2,startframe+1]
                object_buffer#[object1,limb,3,82]=object_buffer#[object1,limb,3,startframe+1]
                object_buffer#[object1,limb,4,82]=object_buffer#[object1,limb,4,startframe+1]
                object_buffer#[object1,limb,5,82]=object_buffer#[object1,limb,5,startframe+1]
                object_buffer#[object1,limb,6,82]=object_buffer#[object1,limb,6,startframe+1]
            //endif
            //if startframe=0
                //object_buffer#[object1,limb,1,32]=0
                //object_buffer#[object1,limb,2,32]=0
                //object_buffer#[object1,limb,3,32]=0
                //object_buffer#[object1,limb,4,32]=0
                //object_buffer#[object1,limb,5,32]=0
                //object_buffer#[object1,limb,6,32]=0
            //endif
next limb
//then swap animation loop
//transition[max_objects,4]
//0 data
//1 keyframe
//2 transition?
//3 start frame
//4 end frame
transition#[object1,0]=1 //activated
transition#[object1,1]=continous
transition#[object1,3]=startframe
transition#[object1,4]=endframe
transition#[object1,5]=81.0
playobject(object1,81,82,0,speed#,limbcount)
endfunction



function getobjectplaying(object1)
if objects_playing#[object1,0]=1 then exitfunction 1
endfunction 0

function getobjectframe#(object1)
v#=transition#[object1,5]
endfunction v#

function getobjectspeed#(object1)
v#=objects_playing#[object1,4]
endfunction v#

function stopobject(object1)
objects_playing#[object1,0]=0
transition#[object1,0]=0
endfunction

function cloneanimatedobject(source,destination)
if source<1 then exitfunction
if source>max_objects then exitfunction
if getobjectexists(source)=0 then exitfunction
if destination<1 then exitfunction
if destination>max_objects then exitfunction
if getobjectexists(destination)=1 then deleteobject(destination)
//ok lets clone away
cloneobject(destination,source)
for limb=1 to 20 step 1
sobj=limb_start_obj+(source*20)+limb
dobj=limb_start_obj+(destination*20)+limb
if getobjectexists(dobj)=1 then deleteobject(dobj)
if getobjectexists(sobj)=1 then cloneobject(dobj,sobj)
next limb
//now copy animation data
for limb=0 to 20 step 1
    //copy file names
    object_filenames$[destination,limb,0]=object_filenames$[source,limb,0]
    object_filenames$[destination,limb,1]=object_filenames$[source,limb,1]
    for dat=0 to 11 step 1
        for key=0 to 80 step 1
            object_buffer#[destination,limb,dat,key]=object_buffer#[source,limb,dat,key]
        next key
    next dat
next limb
//frame 0 start
transition#[destination,5]=0.0
updateobject(destination,20)
endfunction

function deleteanimatedobject(object1)
if getobjectexists(object1)=1 then deleteobject(object1)
for limb=1 to 20 step 1
    sobj=limb_start_obj+(object1*20)+limb
    if getobjectexists(sobj)=1 then deleteobject(sobj)
next limb
//clear array
for limb=0 to 20 step 1
    object_filenames$[object1,limb,0]=""
    object_filenames$[object1,limb,1]=""
    for dat=0 to 11 step 1
        for key=0 to 80 step 1
            object_buffer#[object1,limb,dat,key]=0
        next key
    next dat
next limb
endfunction

function instanceanimatedobject(source,destination)
if source<1 then exitfunction
if source>max_objects then exitfunction
if getobjectexists(source)=0 then exitfunction
if destination<1 then exitfunction
if destination>max_objects then exitfunction
if getobjectexists(destination)=1 then deleteobject(destination)
//ok lets clone away
instanceobject(destination,source)
for limb=1 to 20 step 1
sobj=limb_start_obj+(source*20)+limb
dobj=limb_start_obj+(destination*20)+limb
if getobjectexists(dobj)=1 then deleteobject(dobj)
if getobjectexists(sobj)=1 then instanceobject(dobj,sobj)
next limb
//now copy animation data
for limb=0 to 20 step 1
    object_filenames$[destination,limb,0]=object_filenames$[source,limb,0]
    object_filenames$[destination,limb,1]=object_filenames$[source,limb,1]
    for dat=0 to 11 step 1
        for key=0 to 80 step 1
            object_buffer#[destination,limb,dat,key]=object_buffer#[source,limb,dat,key]
        next key
    next dat
next limb
//
transition#[destination,5]=0
updateobject(destination,20)
endfunction

function hideobject(object1)
setobjectvisible(object1,0)
for limb=1 to 20 step 1
    sobj=limb_start_obj+(object1*20)+limb
    if getobjectexists(sobj)=1 then setobjectvisible(sobj,0)
next limb
endfunction

function hidelimb(object1,limb)
if limb=0 then setobjectvisible(object1,0)
if limb>0
    sobj=limb_start_obj+(object1*20)+limb
    if getobjectexists(sobj)=1 then setobjectvisible(sobj,0)
endif
endfunction




function showlimb(object1,limb)
if limb=0 then setobjectvisible(object1,1)
if limb>0
    sobj=limb_start_obj+(object1*20)+limb
    if getobjectexists(sobj)=1 then setobjectvisible(sobj,1)
endif
endfunction

function showobject(object1)
setobjectvisible(object1,0)
for limb=1 to 20 step 1
sobj=limb_start_obj+(object1*20)+limb
if getobjectexists(sobj)=1 then setobjectvisible(sobj,1)
next limb
endfunction

function updateobjectanimation(object1)
//not based on timer yet!
atkey#=transition#[object1,5]
//skip=0
//test if its running transition
//print(str(transition#[object1,0]))

//check if playing
if objects_playing#[object1,0]=1

if transition#[object1,0]=1 and atkey#=82.0 //activated
    objects_playing#[object1,3]=transition#[object1,1] //countinous
    atkey#=transition#[object1,3]+1
    objects_playing#[object1,1]=transition#[object1,3]
    objects_playing#[object1,2]=transition#[object1,4]
    transition#[object1,0]=0
    //skip=1
endif

//test forward
if objects_playing#[object1,2]>objects_playing#[object1,1] //and skip=0
    atkey#=atkey#+objects_playing#[object1,4]
    if atkey#=>objects_playing#[object1,2] and objects_playing#[object1,3]=1 then atkey#=objects_playing#[object1,1]
    if atkey#=>objects_playing#[object1,2] and objects_playing#[object1,3]=0
        atkey#=objects_playing#[object1,2]
        if transition#[object1,0]=0 then objects_playing#[object1,0]=0
    endif
endif
//test backward
if objects_playing#[object1,1]>objects_playing#[object1,2] //and skip=0
    atkey#=atkey#-objects_playing#[object1,4]
    if atkey#=<objects_playing#[object1,2] and objects_playing#[object1,3]=1 then atkey#=objects_playing#[object1,1]
    if atkey#=<objects_playing#[object1,2] and objects_playing#[object1,3]=0
        atkey#=objects_playing#[object1,1]
        if transition#[object1,0]=0 then objects_playing#[object1,0]=0
    endif
endif


endif

transition#[object1,5]=atkey#
if objects_playing#[object1,5]=0 then objects_playing#[object1,5]=getlimbcount(object1)
updateobject(object1,objects_playing#[object1,5])
endfunction



function getdistance#(x1#,y1#,z1#,x2#,y2#,z2#)
    dx# = x2#-x1#
    dy# = y2#-y1#
    dz# = z2#-z1#
    d#=sqrt(dx#*dx# + dy#*dy# + dz#*dz#)
endfunction d#

function makenewobject(vertex_cnt,index_cnt,normal_cnt,uv_cnt)
if vertex_cnt-1<2 then exitfunction -1
if index_cnt-1<2 then exitfunction -1
    dim edit_object1#[vertex_cnt-1,2]
    dim edit_object2[index_cnt-1,2]
    max_vertexcount=vertex_cnt-1
    max_indexcount=index_cnt-1
//dim edit_object1#[2,2]
dim edit_objectn1#[normal_cnt-1,2]
dim edit_objectuv1#[uv_cnt-1,2]
//dim edit_object2[2,2]
//global max_vertexcount
max_normalscount=normal_cnt-1
max_uvcount=uv_cnt-1
endfunction 1

function setvertexdataposition(vertex,x#,y#,z#)
if vertex>max_vertexcount then exitfunction -1
if vertex<0 then exitfunction -1
edit_object1#[vertex,0]=x#
edit_object1#[vertex,1]=y#
edit_object1#[vertex,2]=z#
endfunction 1

function setvertexdatanormals(vertex,x#,y#,z#)
if vertex>max_normalscount then exitfunction -1
if vertex<0 then exitfunction -1
edit_objectn1#[vertex,0]=x#
edit_objectn1#[vertex,1]=y#
edit_objectn1#[vertex,2]=z#
endfunction 1

function setvertexdatauv(vertex,u#,v#,w#)
if vertex>max_uvcount then exitfunction -1
if vertex<0 then exitfunction -1
edit_objectuv1#[vertex,0]=u#
edit_objectuv1#[vertex,1]=v#
edit_objectuv1#[vertex,2]=w#
endfunction 1

function setindexdata(index,v1,s)
if index<0 then exitfunction -1
if index>max_indexcount then exitfunction -1
if v1<0 then exitfunction -1
//if v1>max_vertexcount then exitfunction -1
edit_object2[index,s]=v1
endfunction 1

function savenewobject(object_number,filename$,flag1,flag2)
//flag1 1 = overwrite existing file
//flag1 <>0 = fail if file exists
//flag2 load it in after saving it =1
if getfileexists(filename$)=1 and flag1<>1 then exitfunction -1
if max_vertexcount<2 then exitfunction -1
if max_indexcount<2 then exitfunction -1
//ok lets write the file
if getfileexists(filename$)=1 then deletefile(filename$)
    opentowrite(1,filename$)
        writeline(1,"# agk easy editable object format")
        writeline(1,"# "+str(max_vertexcount+1)+" vertex positions" )
        stp=-1
        for v=0 to max_vertexcount step 1
            writeline(1,"v "+str(edit_object1#[v,0])+" "+str(edit_object1#[v,1])+" "+str(edit_object1#[v,2]))
        next v

        writeline(1,"# "+str(max_uvcount+1)+" uv coordinates" )
        for v=0 to max_uvcount step 1
            writeline(1,"vt "+str(edit_objectuv1#[v,0])+" "+str(edit_objectuv1#[v,1])+" "+str(edit_objectuv1#[v,2]))
        next v
        writeline(1,"# "+str(max_normalscount+1)+" vertex normals" )
        for v=0 to max_normalscount step 1
            writeline(1,"vn "+str(edit_objectn1#[v,0])+" "+str(edit_objectn1#[v,1])+" "+str(edit_objectn1#[v,2]))
        next v
        ind_count=(max_indexcount+1)/3
        writeline(1,"# Mesh 'object_name' with "+str(ind_count)+" faces" )
        writeline(1,"# " ) // normally would specify material name but agk ignores this
        writeline(1,"g object_name")
        for i=0 to max_indexcount step 3
            index1=edit_object2[i,0]+1
            index2=edit_object2[i+1,0]+1
            index3=edit_object2[i+2,0]+1
            
            index4=edit_object2[i,1]+1
            index5=edit_object2[i+1,1]+1
            index6=edit_object2[i+2,1]+1
            
            index7=edit_object2[i,2]+1
            index8=edit_object2[i+1,2]+1
            index9=edit_object2[i+2,2]+1
            
            writeline(1,"f "+str(index1)+"/"+str(index4)+"/"+str(index7)+" "+str(index2)+"/"+str(index5)+"/"+str(index8)+" "+str(index3)+"/"+str(index6)+"/"+str(index9))
        next i
        writeline(1,"# thats it ")
        //message("saved something?")
    closefile(1)
if flag2=1
    if getobjectexists(object_number)=1 then deleteobject(object_number)
    loadobject(object_number,filename$)
endif
endfunction 1

function loadobj(object1,filename$)
//lets load the file 2 times to count data
opentoread(1,filename$)
vertex_counter=0
uv_counter=0
nm_counter=0
index_counter=0
while fileeof(1)=0
    cmd$=readline(1)
    if left(cmd$,2)="v "
        vertex_counter=vertex_counter+1
    endif
    if left(cmd$,2)="vn"
        nm_counter=nm_counter+1
    endif
    if left(cmd$,2)="vt"
        uv_counter=uv_counter+1
    endif
    if left(cmd$,2)="f "
        index_counter=index_counter+1
    endif
endwhile
closefile(1)
//ok so we have some general data to actualy load the file in
//if vertex_counter<3 then exitfunction
//if index_counter<3 then exitfunction
ic=(index_counter*3)
makenewobject(vertex_counter,ic,nm_counter,uv_counter)
//makenewobject(vertex_cnt,index_cnt,normal_cnt,uv_cnt)
//load it in and fill in the data
opentoread(1,filename$)
vertex_counter=0
uv_counter=0
nm_counter=0
index_counter=0
while fileeof(1)=0
    cmd$=readline(1)
    if left(cmd$,2)="v "
        vertex_counter=vertex_counter+1
        num=countstringtokens(cmd$," ")
        v1#=0.0
        v2#=0.0
        v3#=0.0
        v1#=valfloat(getstringtoken(cmd$," ",2))
        v2#=valfloat(getstringtoken(cmd$," ",3))
        v3#=valfloat(getstringtoken(cmd$," ",4))
        setvertexdataposition(vertex_counter-1,v1#,v2#,v3#)
    endif
    if left(cmd$,2)="vn"
        nm_counter=nm_counter+1
        num=countstringtokens(cmd$," ")
        v1#=0.0
        v2#=0.0
        v3#=0.0
        v1#=valfloat(getstringtoken(cmd$," ",2))
        v2#=valfloat(getstringtoken(cmd$," ",3))
        v3#=valfloat(getstringtoken(cmd$," ",4))
        setvertexdatanormals(nm_counter-1,v1#,v2#,v3#)
    endif
    if left(cmd$,2)="vt"
        uv_counter=uv_counter+1
        num=countstringtokens(cmd$," ")
        v1#=0.0
        v2#=0.0
        v3#=0.0
        v1#=valfloat(getstringtoken(cmd$," ",2))
        v2#=valfloat(getstringtoken(cmd$," ",3))
        v3#=valfloat(getstringtoken(cmd$," ",4))
        setvertexdatauv(uv_counter-1,v1#,v2#,v3#)
    endif
    if left(cmd$,2)="f "
        index_counter=index_counter+1
        //split into several strings
        num=countstringtokens(cmd$," ")
        st1$=getstringtoken(cmd$," ",2)
        st2$=getstringtoken(cmd$," ",3)
        st3$=getstringtoken(cmd$," ",4)
        
        ind1=val(getstringtoken(st1$,"/",1))
        indn1=val(getstringtoken(st1$,"/",2))
        induv1=val(getstringtoken(st1$,"/",3))
        
        ind2=val(getstringtoken(st2$,"/",1))
        indn2=val(getstringtoken(st2$,"/",2))
        induv2=val(getstringtoken(st2$,"/",3))
        
        ind3=val(getstringtoken(st3$,"/",1))
        indn3=val(getstringtoken(st3$,"/",2))
        induv3=val(getstringtoken(st3$,"/",3))
        
        ic=(index_counter*3.0)-3
        setindexdata(ic,ind1-1,0)
        setindexdata(ic+1,ind2-1,0)
        setindexdata(ic+2,ind3-1,0)
        
        setindexdata(ic,indn1-1,1)
        setindexdata(ic+1,indn2-1,1)
        setindexdata(ic+2,indn3-1,1)
        
        setindexdata(ic,induv1-1,2)
        setindexdata(ic+1,induv2-1,2)
        setindexdata(ic+2,induv3-1,2)
               
    endif
endwhile
closefile(1)
//ok lets save temp obj and loadit in.
setfolder("")
savenewobject(object1,"temporary_smd.obj",1,1)
if getfileexists("temporary_smd.obj")=1 then deletefile("temporary_smd.obj")
setfolder("media")
//benefits you ask?  you can edit it and save it again!
//automatically fills vertex and index data into memory
endfunction








function loadobjectarray(object1,limb)
filename3$=object_filenames$[object1,limb,0]
//ok so we have some general data to actualy load the file in
vertex_counter=count_data[limb,1]
index_counter=count_data[limb,4]
nm_counter=count_data[limb,2] //n
uv_counter=count_data[limb,3] //uv_counter //t
ic=(index_counter*3)

makenewobject(vertex_counter,ic,nm_counter,uv_counter)
//load it in and fill in the data
opentoread(1,filename3$)
vertex_counter=0
uv_counter=0
nm_counter=0
index_counter=0
while fileeof(1)=0
    cmd$=readline(1)
    if left(cmd$,2)="v "
        vertex_counter=vertex_counter+1
        num=countstringtokens(cmd$," ")
        v1#=0.0
        v2#=0.0
        v3#=0.0
        v1#=valfloat(getstringtoken(cmd$," ",2))
        v2#=valfloat(getstringtoken(cmd$," ",3))
        v3#=valfloat(getstringtoken(cmd$," ",4))
        setvertexdataposition(vertex_counter-1,v1#,v2#,v3#)
    endif
    if left(cmd$,2)="vn"
        nm_counter=nm_counter+1
        num=countstringtokens(cmd$," ")
        v1#=0.0
        v2#=0.0
        v3#=0.0
        v1#=valfloat(getstringtoken(cmd$," ",2))
        v2#=valfloat(getstringtoken(cmd$," ",3))
        v3#=valfloat(getstringtoken(cmd$," ",4))
        setvertexdatanormals(nm_counter-1,v1#,v2#,v3#)
    endif
    if left(cmd$,2)="vt"
        uv_counter=uv_counter+1
        num=countstringtokens(cmd$," ")
        v1#=0.0
        v2#=0.0
        v3#=0.0
        v1#=valfloat(getstringtoken(cmd$," ",2))
        v2#=valfloat(getstringtoken(cmd$," ",3))
        v3#=valfloat(getstringtoken(cmd$," ",4))
        setvertexdatauv(uv_counter-1,v1#,v2#,v3#)
    endif
    if left(cmd$,2)="f "
        index_counter=index_counter+1
        //split into several strings
        num=countstringtokens(cmd$," ")
        st1$=getstringtoken(cmd$," ",2)
        st2$=getstringtoken(cmd$," ",3)
        st3$=getstringtoken(cmd$," ",4)
        ind1=val(getstringtoken(st1$,"/",1))
        indv1=val(getstringtoken(st1$,"/",2))
        indn1=val(getstringtoken(st1$,"/",3))
        
        
        ind2=val(getstringtoken(st2$,"/",1))
        indv2=val(getstringtoken(st2$,"/",2))
        indn2=val(getstringtoken(st2$,"/",3))
        
        
        ind3=val(getstringtoken(st3$,"/",1))
        indv3=val(getstringtoken(st3$,"/",2))
        indn3=val(getstringtoken(st3$,"/",3))
        
        ic=(index_counter*3.0)-3
        setindexdata(ic,ind1-1,0)
        setindexdata(ic+1,ind2-1,0)
        setindexdata(ic+2,ind3-1,0)
        
        setindexdata(ic,indv1-1,1)
        setindexdata(ic+1,indv2-1,1)
        setindexdata(ic+2,indv3-1,1)
        
        setindexdata(ic,indn1-1,2)
        setindexdata(ic+1,indn2-1,2)
        setindexdata(ic+2,indn3-1,2)
        
    endif
endwhile
closefile(1)
//datas in
endfunction















function saveanimatedmodel(object1,filenameb$,flag)
counthowmany=0
obj_editcounter=0
rlen=len(filenameb$)
filenamec$=left(filenameb$,rlen-3)
filenamec$=filenamec$+"cr"

if object1<1 then exitfunction
if object1>max_objects then exitfunction
//flag 1 = overwrite if exists
if getfileexists(filenameb$)=1 and flag=0 then exitfunction
if getfileexists(filenameb$)=1 then deletefile(filenameb$)

if getfileexists(filenamec$)=1 
	opentoread(2,filenamec$)
	obj_editcounter=readinteger(2)
	closefile(2)
	deletefile(filenamec$)
endif
//count how many limbs
counthowmany=0
if getobjectexists(object1)=1 then counthowmany=counthowmany+1
for t=1 to 20 step 1
	if getlimbexists(object1,t)=1 then counthowmany=counthowmany+1
next t

opentowrite(2,filenameb$)
//new release 31
writeline(2,"[ANIMATED OBJ FORMAT FOR AGK r0.34]")

//new addition 31 addition
obj_editcounter=obj_editcounter+1
writeinteger(2,obj_editcounter)
//seek this out

writeinteger(2,counthowmany)
writestring2(2,"[ANIMDATA]")
//store anim data first
for limb=0 to 20 step 1
    for dat=0 to 11 step 1
        for key=0 to 80 step 1
            writefloat(2,object_buffer#[object1,limb,dat,key])
        next key
    next dat
next limb
writestring2(2,"[ENDANIMDATA]")	

//additional data
for d=0 to 20 step 1
writestring2(2,object_filenames$[object1,d,1])//texturename
writestring2(2,object_filenames$[object1,d,0])//objectname
next d

writestring2(2,"[OBJECTDATA]")
for limb=0 to 20 step 1
    count_objectdata(object1,limb)
    //get it into memory
    if count_data[limb,0]=1
        //load that data in
        loadobjectarray(object1,limb)
        writestring2(2,"[NEW OBJECT]")
 
        //writestring2(2,"[VTOTAL "+str(max_vertexcount+1)+" ]")
        //writestring2(2,"[UVTOTAL "+str(max_uvcount+1)+" ]" )
        //writestring2(2,"[NMTOTAL "+str(max_normalscount+1)+" ]" )
        ind_count=(max_indexcount+1)/3
        //writestring2(2,"[IFACES "+str(ind_count)+" ]" )
		
		writeinteger(2,max_vertexcount+1)
		writeinteger(2,max_uvcount+1)
		writeinteger(2,max_normalscount+1)
		//extra
		writeinteger(2,ind_count)
		
        for v=0 to max_vertexcount step 1
            //writestring2(2,"v "+str(edit_object1#[v,0])+" "+str(edit_object1#[v,1])+" "+str(edit_object1#[v,2]))
			writefloat(2,edit_object1#[v,0])
			writefloat(2,edit_object1#[v,1])
			writefloat(2,edit_object1#[v,2])
        next v

        for v=0 to max_uvcount step 1
            //writestring2(2,"vt "+str(edit_objectuv1#[v,0])+" "+str(edit_objectuv1#[v,1])+" "+str(edit_objectuv1#[v,2]))
			writefloat(2,edit_objectuv1#[v,0])
			writefloat(2,edit_objectuv1#[v,1])
			writefloat(2,edit_objectuv1#[v,2])
        next v

        for v=0 to max_normalscount step 1
            //writestring2(2,"vn "+str(edit_objectn1#[v,0])+" "+str(edit_objectn1#[v,1])+" "+str(edit_objectn1#[v,2]))
        	writefloat(2,edit_objectn1#[v,0])
			writefloat(2,edit_objectn1#[v,1])
			writefloat(2,edit_objectn1#[v,2])
        next v

        for i=0 to max_indexcount step 3
            index1=edit_object2[i,0]+1
            index2=edit_object2[i+1,0]+1
            index3=edit_object2[i+2,0]+1
            
            index4=edit_object2[i,1]+1
            index5=edit_object2[i+1,1]+1
            index6=edit_object2[i+2,1]+1
            
            index7=edit_object2[i,2]+1
            index8=edit_object2[i+1,2]+1
            index9=edit_object2[i+2,2]+1
            //writestring2(2,"f "+str(index1)+"/"+str(index4)+"/"+str(index7)+" "+str(index2)+"/"+str(index5)+"/"+str(index8)+" "+str(index3)+"/"+str(index6)+"/"+str(index9))
			writeinteger(2,index1)
			writeinteger(2,index4)
			writeinteger(2,index7)
			writeinteger(2,index2)
			writeinteger(2,index5)
			writeinteger(2,index8)
			writeinteger(2,index3)
			writeinteger(2,index6)
			writeinteger(2,index9)
        
        next i

       
    endif

    if count_data[limb,0]=0
        writestring2(2,"[MT OBJECT]")
    endif

next limb
//ok setup info complete

writestring2(2,"[ENDFILE]")

closefile(2)
	
endfunction





function loadanimatedmodel(object1,directory$,fname$,flag)
myversion=3
filename$=""
objectcounter=0
objectmaxcounted=0
new_objeditcounter=0
obj_editcounter=0
if right(directory$,1)<>"\" and directory$<>"" then directory$=directory$+"\"
rlen=len(fname$)
fname2$=left(fname$,rlen-3)+"cr"
//flag =0 no texturing
//flag =1 if attemp to load and apply textures
//flag =2 if load only first texture and apply to all limbs
filename$=directory$+fname$
filename2$=directory$+fname2$
if getfileexists(filename$)=0 then exitfunction
if object1<1 then exitfunction
if object1>max_objects then exitfunction

//ok load it here
//if getobjectexists(object1)=1 then deleteobject(object1)
deleteanimatedobject(object1)
opentoread(1,filename$)
a$=readline(1)

//is it .31 or is it .3?
//writeline(2,"[ANIMATED OBJ FORMAT FOR AGK r0.31]")
if a$="[ANIMATED OBJ FORMAT FOR AGK r0.31]"
	//new addition
	obj_editcounter=readinteger(1)
	//writeline(2,str(obj_editcounter))
	myversion=31
endif

//check for 80 framed animation
if a$="[ANIMATED OBJ FORMAT FOR AGK r0.32]"
	//new addition
	obj_editcounter=readinteger(1)
	//writeline(2,str(obj_editcounter))
	myversion=32
endif

if a$="[ANIMATED OBJ FORMAT FOR AGK r0.33]"
	//new addition
	obj_editcounter=readinteger(1)
	//writeline(2,str(obj_editcounter))
	myversion=33
endif

if a$="[ANIMATED OBJ FORMAT FOR AGK r0.34]" //newest version
	//new addition
	obj_editcounter=readinteger(1)
	//writeline(2,str(obj_editcounter))
	myversion=34
endif


if myversion<33
objectmaxcounted=readinteger(1)
a$=readline(1)
//now animation data
//store anim data first
for limb=0 to 20 step 1
    for dat=0 to 6 step 1
        
        if myversion=<31
			for key=0 to 30 step 1
				object_buffer#[object1,limb,dat,key]=readfloat(1)
		if key=0
		object_buffer#[object1,limb,7,0]=256
		object_buffer#[object1,limb,8,0]=256
		object_buffer#[object1,limb,9,0]=256
		object_buffer#[object1,limb,10,0]=256
		object_buffer#[object1,limb,11,0]=0
		endif
		if key>0
		object_buffer#[object1,limb,7,key]=0
		object_buffer#[object1,limb,8,key]=0
		object_buffer#[object1,limb,9,key]=0
		object_buffer#[object1,limb,10,key]=0
		object_buffer#[object1,limb,11,key]=0
		endif
			next key
		endif
		if myversion=32
			for key=0 to 80 step 1
				object_buffer#[object1,limb,dat,key]=readfloat(1)
		if key=0
		object_buffer#[object1,limb,7,0]=256
		object_buffer#[object1,limb,8,0]=256
		object_buffer#[object1,limb,9,0]=256
		object_buffer#[object1,limb,10,0]=256
		object_buffer#[object1,limb,11,0]=0
		endif
		if key>0
		object_buffer#[object1,limb,7,key]=0
		object_buffer#[object1,limb,8,key]=0
		object_buffer#[object1,limb,9,key]=0
		object_buffer#[object1,limb,10,key]=0
		object_buffer#[object1,limb,11,key]=0
		endif
			
			next key
		endif
		
	
		
    next dat
next limb
a$=readline(1)
objectcounter=0
for d=0 to 20 step 1
	limb=d
	object_filenames$[object1,d,1]=directory$+readline(1)//texturename
	object_filenames$[object1,d,0]=directory$+readline(1)//objectname
	//images
	if d>0 and getimageexists(limb_start_obj+(object1*20)+limb)=1 then deleteimage(limb_start_obj+(object1*20)+limb)
	if d>0 and getfileexists(object_filenames$[object1,d,1])=1 then loadimage(limb_start_obj+(object1*20)+limb,object_filenames$[object1,d,1],1)
	if d=0 and getimageexists(limb_start_obj+(object1*20)+limb)=1 then deleteimage(limb_start_obj+(object1*20)+limb)
	if d=0 and getfileexists(object_filenames$[object1,d,1])=1 then loadimage(limb_start_obj+(object1*20)+limb,object_filenames$[object1,d,1],1)
	
	if d=0
		if getobjectexists(object1)=1 then deleteobject(object1)
		if getfileexists(object_filenames$[object1,d,0])=1 
			loadobject(object1,object_filenames$[object1,d,0])
			objectcounter=objectcounter+1
			if getimageexists(limb_start_obj+(object1*20)+limb)=1 then setobjectimage(object1,limb_start_obj+(object1*20)+limb,0)
		endif
	endif
	if d>0
		if getobjectexists(limb_start_obj+(object1*20)+limb)=1 then deleteobject(limb_start_obj+(object1*20)+limb)
		if getfileexists(object_filenames$[object1,d,0])=1 
			loadobject(limb_start_obj+(object1*20)+limb,object_filenames$[object1,d,0]) 
			objectcounter=objectcounter+1
			if getimageexists(limb_start_obj+(object1*20)+limb)=1 then setobjectimage(limb_start_obj+(object1*20)+limb,limb_start_obj+(object1*20)+limb,0)
		endif
	endif
	 
	 	
next d


//EXTENDED
//do we need to extract objects??
if objectcounter=objectmaxcounted and myversion=3
	closefile(1)
	hidelimb(object1,0)
	exitfunction
endif


if myversion=>31 and objectcounter=objectmaxcounted
	new_objeditcounter=0
	if getfileexists(filename2$)=1
		opentoread(3,filename2$)
		new_objeditcounter=readinteger(3)
		closefile(3)
	endif
	if new_objeditcounter=obj_editcounter
		closefile(1)
		hidelimb(object1,0)
		exitfunction
	endif
endif

//darn we need to extract objects
//process objects / make them / save data / load in obj / apply texture
limb=-1
tex$="NULL"
while fileeof(1)=0
    cmd$=readline(1)
    //process comands
    if cmd$="[NEW OBJECT]" then limb=limb+1
    //if left(cmd$,8)="[TEXTURE" then tex$=getstringtoken(cmd$," ",2)
    if left(cmd$,7)="[VTOTAL" then vertex_counter=val(getstringtoken(cmd$," ",2))
    if left(cmd$,8)="[UVTOTAL" then uv_counter=val(getstringtoken(cmd$," ",2))
    if left(cmd$,8)="[NMTOTAL" then nm_counter=val(getstringtoken(cmd$," ",2))
    if left(cmd$,7)="[IFACES"
        index_counter=val(getstringtoken(cmd$," ",2))
        //ok so we have some general data to actualy load the file in
		//count_data[limb,1]=vertex_counter
		//count_data[limb,4]=index_counter
		//count_data[limb,2]=nm_counter //n
		//count_data[limb,3]=uv_counter //uv_counter //t
//ic=(index_counter*3)

        //start object
        ic=(index_counter*3.0)
        //makenewobject(vertex_counter,ic,)
        makenewobject(vertex_counter,ic,nm_counter,uv_counter)
        //0 the data
        vertex_counter=0
        uv_counter=0
        nm_counter=0
        index_counter=0
    endif

    if left(cmd$,2)="v "
        vertex_counter=vertex_counter+1
        num=countstringtokens(cmd$," ")
        v1#=0.0
        v2#=0.0
        v3#=0.0
        v1#=valfloat(getstringtoken(cmd$," ",2))
        v2#=valfloat(getstringtoken(cmd$," ",3))
        v3#=valfloat(getstringtoken(cmd$," ",4))
        setvertexdataposition(vertex_counter-1,v1#,v2#,v3#)
    endif
    if left(cmd$,2)="vn"
        nm_counter=nm_counter+1
        num=countstringtokens(cmd$," ")
        v1#=0.0
        v2#=0.0
        v3#=0.0
        v1#=valfloat(getstringtoken(cmd$," ",2))
        v2#=valfloat(getstringtoken(cmd$," ",3))
        v3#=valfloat(getstringtoken(cmd$," ",4))
        setvertexdatanormals(nm_counter-1,v1#,v2#,v3#)
    endif
    if left(cmd$,2)="vt"
        uv_counter=uv_counter+1
        num=countstringtokens(cmd$," ")
        v1#=0.0
        v2#=0.0
        v3#=0.0
        v1#=valfloat(getstringtoken(cmd$," ",2))
        v2#=valfloat(getstringtoken(cmd$," ",3))
        v3#=valfloat(getstringtoken(cmd$," ",4))
        setvertexdatauv(uv_counter-1,v1#,v2#,v3#)
    endif
    if left(cmd$,2)="f "
        index_counter=index_counter+1
        //split into several strings
        num=countstringtokens(cmd$," ")
        st1$=getstringtoken(cmd$," ",2)
        st2$=getstringtoken(cmd$," ",3)
        st3$=getstringtoken(cmd$," ",4)
        ind1=val(getstringtoken(st1$,"/",1))
        indn1=val(getstringtoken(st1$,"/",2))
        induv1=val(getstringtoken(st1$,"/",3))
        
        ind2=val(getstringtoken(st2$,"/",1))
        indn2=val(getstringtoken(st2$,"/",2))
        induv2=val(getstringtoken(st2$,"/",3))
        
        ind3=val(getstringtoken(st3$,"/",1))
        indn3=val(getstringtoken(st3$,"/",2))
        induv3=val(getstringtoken(st3$,"/",3))
        
        ic=((index_counter)*3.0)-3
        setindexdata(ic,ind1-1,0)
        setindexdata(ic+1,ind2-1,0)
        setindexdata(ic+2,ind3-1,0)
        
        setindexdata(ic,indn1-1,1)
        setindexdata(ic+1,indn2-1,1)
        setindexdata(ic+2,indn3-1,1)
        
        setindexdata(ic,induv1-1,2)
        setindexdata(ic+1,induv2-1,2)
        setindexdata(ic+2,induv3-1,2)
           
    endif

    if cmd$="[ENDOBJ]"
        //ok lets save it and load it in place
        if limb=0 then savenewobjectlimb(object1,object_filenames$[object1,limb,0])
        if limb>0 then savenewobjectlimb(limb_start_obj+(object1*20)+limb,object_filenames$[object1,limb,0])
        //apply texture if any
        tex$=object_filenames$[object1,limb,1]
        if flag=1 or flag=2 and limb=0
            if getimageexists(limb_start_obj+(object1*20)+limb)=1 then deleteimage(limb_start_obj+(object1*20)+limb)
            if tex$<>"NULL" and getfileexists(tex$)=1
                loadimage(limb_start_obj+(object1*20)+limb,tex$)
                if limb>0 then setobjectimage(limb_start_obj+(object1*20)+limb,limb_start_obj+(object1*20)+limb,0)
                if limb=0 then setobjectimage(object1,limb_start_obj+(object1*20)+limb,0)
            endif
        endif
        if flag=2
            if getimageexists(limb_start_obj+(object1*20))=1 then setobjectimage(limb_start_obj+(object1*20)+limb,limb_start_obj+(object1*20),0)
        endif
        //vertex_counter=0
        //uv_counter=0
        //nm_counter=0
        //index_counter=0
    endif

    if cmd$="[MT OBJECT]"
        limb=limb+1
        if getobjectexists(limb_start_obj+(object1*20)+limb)=1 then deleteobject(limb_start_obj+(object1*20)+limb)
    endif

endwhile
hidelimb(object1,0)
closefile(1)


endif //old formats





if myversion=33
objectmaxcounted=readinteger(1)
a$=readstring2(1)
//now animation data
//store anim data first
for limb=0 to 20 step 1
    for dat=0 to 6 step 1
        
		//if myversion=32
		for key=0 to 80 step 1
				object_buffer#[object1,limb,dat,key]=readfloat(1)
			
		if key=0
		object_buffer#[object1,limb,7,0]=256
		object_buffer#[object1,limb,8,0]=256
		object_buffer#[object1,limb,9,0]=256
		object_buffer#[object1,limb,10,0]=256
		object_buffer#[object1,limb,11,0]=0
		endif
		if key>0
		object_buffer#[object1,limb,7,key]=0
		object_buffer#[object1,limb,8,key]=0
		object_buffer#[object1,limb,9,key]=0
		object_buffer#[object1,limb,10,key]=0
		object_buffer#[object1,limb,11,key]=0
		endif
			
			next key
		//endif

    next dat
next limb
a$=readstring2(1)
objectcounter=0
for d=0 to 20 step 1
	limb=d
	object_filenames$[object1,d,1]=directory$+readstring2(1)//texturename
	object_filenames$[object1,d,0]=directory$+readstring2(1)//objectname
	//images
	if d>0 and getimageexists(limb_start_obj+(object1*20)+limb)=1 then deleteimage(limb_start_obj+(object1*20)+limb)
	if d>0 and getfileexists(object_filenames$[object1,d,1])=1 then loadimage(limb_start_obj+(object1*20)+limb,object_filenames$[object1,d,1],1)
	if d=0 and getimageexists(limb_start_obj+(object1*20)+limb)=1 then deleteimage(limb_start_obj+(object1*20)+limb)
	if d=0 and getfileexists(object_filenames$[object1,d,1])=1 then loadimage(limb_start_obj+(object1*20)+limb,object_filenames$[object1,d,1],1)
	
	if d=0
		if getobjectexists(object1)=1 then deleteobject(object1)
		if getfileexists(object_filenames$[object1,d,0])=1 
			loadobject(object1,object_filenames$[object1,d,0])
			objectcounter=objectcounter+1
			if getimageexists(limb_start_obj+(object1*20)+limb)=1 then setobjectimage(object1,limb_start_obj+(object1*20)+limb,0)
		endif
	endif
	if d>0
		if getobjectexists(limb_start_obj+(object1*20)+limb)=1 then deleteobject(limb_start_obj+(object1*20)+limb)
		if getfileexists(object_filenames$[object1,d,0])=1 
			loadobject(limb_start_obj+(object1*20)+limb,object_filenames$[object1,d,0]) 
			objectcounter=objectcounter+1
			if getimageexists(limb_start_obj+(object1*20)+limb)=1 then setobjectimage(limb_start_obj+(object1*20)+limb,limb_start_obj+(object1*20)+limb,0)
		endif
	endif
	 
	 	
next d


//EXTENDED
//do we need to extract objects??
if objectcounter=objectmaxcounted and myversion=3
	closefile(1)
	hidelimb(object1,0)
	exitfunction
endif


if myversion=>31 and objectcounter=objectmaxcounted
	new_objeditcounter=0
	if getfileexists(filename2$)=1
		opentoread(3,filename2$)
		new_objeditcounter=readinteger(3)
		closefile(3)
	endif
	if new_objeditcounter=obj_editcounter
		closefile(1)
		hidelimb(object1,0)
		exitfunction
	endif
endif



//darn we need to extract objects
//process objects / make them / save data / load in obj / apply texture
limb=-1
tex$="NULL"
while fileeof(1)=0
    cmd$=readstring2(1)
    //process comands
    if cmd$="[NEW OBJECT]" then limb=limb+1
    //if left(cmd$,8)="[TEXTURE" then tex$=getstringtoken(cmd$," ",2)
    if left(cmd$,7)="[VTOTAL" then vertex_counter=val(getstringtoken(cmd$," ",2))
    if left(cmd$,8)="[UVTOTAL" then uv_counter=val(getstringtoken(cmd$," ",2))
    if left(cmd$,8)="[NMTOTAL" then nm_counter=val(getstringtoken(cmd$," ",2))
    if left(cmd$,7)="[IFACES"
        index_counter=val(getstringtoken(cmd$," ",2))
        //ok so we have some general data to actualy load the file in
		//count_data[limb,1]=vertex_counter
		//count_data[limb,4]=index_counter
		//count_data[limb,2]=nm_counter //n
		//count_data[limb,3]=uv_counter //uv_counter //t
//ic=(index_counter*3)

        //start object
        ic=(index_counter*3.0)
        //makenewobject(vertex_counter,ic,)
        makenewobject(vertex_counter,ic,nm_counter,uv_counter)
        //0 the data
        vertex_counter=0
        uv_counter=0
        nm_counter=0
        index_counter=0
    endif

    if left(cmd$,2)="v "
        vertex_counter=vertex_counter+1
        num=countstringtokens(cmd$," ")
        v1#=0.0
        v2#=0.0
        v3#=0.0
        v1#=valfloat(getstringtoken(cmd$," ",2))
        v2#=valfloat(getstringtoken(cmd$," ",3))
        v3#=valfloat(getstringtoken(cmd$," ",4))
        setvertexdataposition(vertex_counter-1,v1#,v2#,v3#)
    endif
    if left(cmd$,2)="vn"
        nm_counter=nm_counter+1
        num=countstringtokens(cmd$," ")
        v1#=0.0
        v2#=0.0
        v3#=0.0
        v1#=valfloat(getstringtoken(cmd$," ",2))
        v2#=valfloat(getstringtoken(cmd$," ",3))
        v3#=valfloat(getstringtoken(cmd$," ",4))
        setvertexdatanormals(nm_counter-1,v1#,v2#,v3#)
    endif
    if left(cmd$,2)="vt"
        uv_counter=uv_counter+1
        num=countstringtokens(cmd$," ")
        v1#=0.0
        v2#=0.0
        v3#=0.0
        v1#=valfloat(getstringtoken(cmd$," ",2))
        v2#=valfloat(getstringtoken(cmd$," ",3))
        v3#=valfloat(getstringtoken(cmd$," ",4))
        setvertexdatauv(uv_counter-1,v1#,v2#,v3#)
    endif
    if left(cmd$,2)="f "
        index_counter=index_counter+1
        //split into several strings
        num=countstringtokens(cmd$," ")
        st1$=getstringtoken(cmd$," ",2)
        st2$=getstringtoken(cmd$," ",3)
        st3$=getstringtoken(cmd$," ",4)
        ind1=val(getstringtoken(st1$,"/",1))
        indn1=val(getstringtoken(st1$,"/",2))
        induv1=val(getstringtoken(st1$,"/",3))
        
        ind2=val(getstringtoken(st2$,"/",1))
        indn2=val(getstringtoken(st2$,"/",2))
        induv2=val(getstringtoken(st2$,"/",3))
        
        ind3=val(getstringtoken(st3$,"/",1))
        indn3=val(getstringtoken(st3$,"/",2))
        induv3=val(getstringtoken(st3$,"/",3))
        
        ic=((index_counter)*3.0)-3
        setindexdata(ic,ind1-1,0)
        setindexdata(ic+1,ind2-1,0)
        setindexdata(ic+2,ind3-1,0)
        
        setindexdata(ic,indn1-1,1)
        setindexdata(ic+1,indn2-1,1)
        setindexdata(ic+2,indn3-1,1)
        
        setindexdata(ic,induv1-1,2)
        setindexdata(ic+1,induv2-1,2)
        setindexdata(ic+2,induv3-1,2)
           
    endif

    if cmd$="[ENDOBJ]"
        //ok lets save it and load it in place
        if limb=0 then savenewobjectlimb(object1,object_filenames$[object1,limb,0])
        if limb>0 then savenewobjectlimb(limb_start_obj+(object1*20)+limb,object_filenames$[object1,limb,0])
        //apply texture if any
        tex$=object_filenames$[object1,limb,1]
        if flag=1 or flag=2 and limb=0
            if getimageexists(limb_start_obj+(object1*20)+limb)=1 then deleteimage(limb_start_obj+(object1*20)+limb)
            if tex$<>"NULL" and getfileexists(tex$)=1
                loadimage(limb_start_obj+(object1*20)+limb,tex$)
                if limb>0 then setobjectimage(limb_start_obj+(object1*20)+limb,limb_start_obj+(object1*20)+limb,0)
                if limb=0 then setobjectimage(object1,limb_start_obj+(object1*20)+limb,0)
            endif
        endif
        if flag=2
            if getimageexists(limb_start_obj+(object1*20))=1 then setobjectimage(limb_start_obj+(object1*20)+limb,limb_start_obj+(object1*20),0)
        endif
        //vertex_counter=0
        //uv_counter=0
        //nm_counter=0
        //index_counter=0
    endif

    if cmd$="[MT OBJECT]"
        limb=limb+1
        if getobjectexists(limb_start_obj+(object1*20)+limb)=1 then deleteobject(limb_start_obj+(object1*20)+limb)
    endif

endwhile
hidelimb(object1,0)
closefile(1)


endif //new format
















if myversion=34
	
objectmaxcounted=readinteger(1)
a$=readstring2(1)
//now animation data
//store anim data first
for limb=0 to 20 step 1
    for dat=0 to 11 step 1
        
		//if myversion=32
		for key=0 to 80 step 1
				object_buffer#[object1,limb,dat,key]=readfloat(1)	
			next key
		//endif

    next dat
next limb
a$=readstring2(1)
objectcounter=0
for d=0 to 20 step 1
	limb=d
	object_filenames$[object1,d,1]=directory$+readstring2(1)//texturename
	object_filenames$[object1,d,0]=directory$+readstring2(1)//objectname
	//images
	if d>0 and getimageexists(limb_start_obj+(object1*20)+limb)=1 then deleteimage(limb_start_obj+(object1*20)+limb)
	if d>0 and getfileexists(object_filenames$[object1,d,1])=1 then loadimage(limb_start_obj+(object1*20)+limb,object_filenames$[object1,d,1],1)
	if d=0 and getimageexists(limb_start_obj+(object1*20)+limb)=1 then deleteimage(limb_start_obj+(object1*20)+limb)
	if d=0 and getfileexists(object_filenames$[object1,d,1])=1 then loadimage(limb_start_obj+(object1*20)+limb,object_filenames$[object1,d,1],1)
	
	if d=0
		if getobjectexists(object1)=1 then deleteobject(object1)
		if getfileexists(object_filenames$[object1,d,0])=1 
			loadobject(object1,object_filenames$[object1,d,0])
			objectcounter=objectcounter+1
			if getimageexists(limb_start_obj+(object1*20)+limb)=1 then setobjectimage(object1,limb_start_obj+(object1*20)+limb,0)
		endif
	endif
	if d>0
		if getobjectexists(limb_start_obj+(object1*20)+limb)=1 then deleteobject(limb_start_obj+(object1*20)+limb)
		if getfileexists(object_filenames$[object1,d,0])=1 
			loadobject(limb_start_obj+(object1*20)+limb,object_filenames$[object1,d,0]) 
			objectcounter=objectcounter+1
			if getimageexists(limb_start_obj+(object1*20)+limb)=1 then setobjectimage(limb_start_obj+(object1*20)+limb,limb_start_obj+(object1*20)+limb,0)
		endif
	endif
	 
	 	
next d



if objectcounter=objectmaxcounted
	new_objeditcounter=0
	if getfileexists(filename2$)=1
		opentoread(3,filename2$)
		new_objeditcounter=readinteger(3)
		closefile(3)
	endif
	if new_objeditcounter=obj_editcounter
		closefile(1)
		hidelimb(object1,0)
		exitfunction
	endif
endif



//darn we need to extract objects
//process objects / make them / save data / load in obj / apply texture
limb=-1
tex$="NULL"

while fileeof(1)=0
    cmd$=readstring2(1)
    //process comands
    if cmd$="[NEW OBJECT]" 
		limb=limb+1
		vertex_counter=readinteger(1)
		uv_counter=readinteger(1)
		nm_counter=readinteger(1)
		//extra
		index_counter=readinteger(1)
        ic=(index_counter*3.0)
        makenewobject(vertex_counter,ic,nm_counter,uv_counter)     
        vertex_counter=0
        uv_counter=0
        nm_counter=0
        index_counter=0
			//rest of obj build here 
        for v=0 to max_vertexcount step 1
            //writestring2(2,"v "+str(edit_object1#[v,0])+" "+str(edit_object1#[v,1])+" "+str(edit_object1#[v,2]))
			edit_object1#[v,0]=readfloat(1)
			edit_object1#[v,1]=readfloat(1)
			edit_object1#[v,2]=readfloat(1)
        next v

        for v=0 to max_uvcount step 1
            //writestring2(2,"vt "+str(edit_objectuv1#[v,0])+" "+str(edit_objectuv1#[v,1])+" "+str(edit_objectuv1#[v,2]))
			edit_objectuv1#[v,0]=readfloat(1)
			edit_objectuv1#[v,1]=readfloat(1)
			edit_objectuv1#[v,2]=readfloat(1)
        next v

        for v=0 to max_normalscount step 1
            //writestring2(2,"vn "+str(edit_objectn1#[v,0])+" "+str(edit_objectn1#[v,1])+" "+str(edit_objectn1#[v,2]))
        	edit_objectn1#[v,0]=readfloat(1)
			edit_objectn1#[v,1]=readfloat(1)
			edit_objectn1#[v,2]=readfloat(1)
        next v

        for i=0 to max_indexcount step 3
           
            //writestring2(2,"f "+str(index1)+"/"+str(index4)+"/"+str(index7)+" "+str(index2)+"/"+str(index5)+"/"+str(index8)+" "+str(index3)+"/"+str(index6)+"/"+str(index9))
			index1=readinteger(1)
			index4=readinteger(1)
			index7=readinteger(1)
			index2=readinteger(1)
			index5=readinteger(1)
			index8=readinteger(1)
			index3=readinteger(1)
			index6=readinteger(1)
			index9=readinteger(1)
			edit_object2[i,0]=index1-1
            edit_object2[i+1,0]=index2-1
            edit_object2[i+2,0]=index3-1
            
            edit_object2[i,1]=index4-1
            edit_object2[i+1,1]=index5-1
            edit_object2[i+2,1]=index6-1
            
            edit_object2[i,2]=index7-1
            edit_object2[i+1,2]=index8-1
            edit_object2[i+2,2]=index9-1
        next i
//ok lets save it and load it in place
        if limb=0 then savenewobjectlimb(object1,object_filenames$[object1,limb,0])
        if limb>0 then savenewobjectlimb(limb_start_obj+(object1*20)+limb,object_filenames$[object1,limb,0])
        //apply texture if any
        tex$=object_filenames$[object1,limb,1]
        if flag=1 or flag=2 and limb=0
            if getimageexists(limb_start_obj+(object1*20)+limb)=1 then deleteimage(limb_start_obj+(object1*20)+limb)
            if tex$<>"NULL" and getfileexists(tex$)=1
                loadimage(limb_start_obj+(object1*20)+limb,tex$)
                if limb>0 then setobjectimage(limb_start_obj+(object1*20)+limb,limb_start_obj+(object1*20)+limb,0)
                if limb=0 then setobjectimage(object1,limb_start_obj+(object1*20)+limb,0)
            endif
        endif
        if flag=2
            if getimageexists(limb_start_obj+(object1*20))=1 then setobjectimage(limb_start_obj+(object1*20)+limb,limb_start_obj+(object1*20),0)
        endif
        //vertex_counter=0
        //uv_counter=0
        //nm_counter=0
        //index_counter=0
 
	endif
 


    if cmd$="[MT OBJECT]"
        limb=limb+1
        if getobjectexists(limb_start_obj+(object1*20)+limb)=1 then deleteobject(limb_start_obj+(object1*20)+limb)
    endif

endwhile

hidelimb(object1,0)
closefile(1)


endif //new format 34






















//lets recreate the cr file
if myversion=>31 
	if getfileexists(filename2$)=1 then deletefile(filename2$)
	opentowrite(1,filename2$)
	writeinteger(1,obj_editcounter)
	closefile(1)
endif

endfunction





function count_objectdata(objecta,limb)
//lets look for data
if objecta<1 then exitfunction
if objecta>max_objects then exitfunction
filename2$=object_filenames$[objecta,limb,0]
if getfileexists(filename2$)=0 then exitfunction
opentoread(1,filename2$)
vertex_counter=0
uv_counter=0
nm_counter=0
index_counter=0
while fileeof(1)=0
    cmd$=readline(1)
    if left(cmd$,2)="v "
        vertex_counter=vertex_counter+1
    endif
    if left(cmd$,2)="vn"
        nm_counter=nm_counter+1
    endif
    if left(cmd$,2)="vt"
        uv_counter=uv_counter+1
    endif
    if left(cmd$,2)="f "
        index_counter=index_counter+1
    endif
endwhile
closefile(1)
count_data[limb,0]=1 //exists
count_data[limb,1]=vertex_counter //v
count_data[limb,2]=nm_counter //n
count_data[limb,3]=uv_counter //t
count_data[limb,4]=index_counter //f
endfunction




function savenewobjectlimb(object,filename4$)
//flag1 1 = overwrite existing file
//flag1 <>0 = fail if file exists
//flag2 load it in after saving it =1
//filename4$="temporary_smd_"+str(object)+".obj"
if max_vertexcount<2 then exitfunction 0
if max_indexcount<2 then exitfunction 0
//ok lets write the file
if getfileexists(filename4$)=1 then deletefile(filename4$)
    opentowrite(3,filename4$)
        writeline(3,"# agk easy editable object format")
        
        writeline(3,"# "+str(max_vertexcount+1)+" vertex positions" )
        stp=-1
        for v=0 to max_vertexcount step 1
            writeline(3,"v "+str(edit_object1#[v,0])+" "+str(edit_object1#[v,1])+" "+str(edit_object1#[v,2]))
        next v
        writeline(3,"# "+str(max_uvcount+1)+" uv coordinates" )
        for v=0 to max_uvcount step 1
            writeline(3,"vt "+str(edit_objectuv1#[v,0])+" "+str(edit_objectuv1#[v,1])+" "+str(edit_objectuv1#[v,2]))
        next v
        writeline(3,"# "+str(max_normalscount+1)+" vertex normals" )
        for v=0 to max_normalscount step 1
            writeline(3,"vn "+str(edit_objectn1#[v,0])+" "+str(edit_objectn1#[v,1])+" "+str(edit_objectn1#[v,2]))
        next v
        ic=(max_indexcount)
        writeline(3,"# Mesh 'object_name' with "+str(max_indexcount)+" faces" )
        writeline(3,"# " ) // normally would specify material name but agk ignores this
        writeline(3,"g object_name")

        for i=0 to ic step 3
            index1=edit_object2[i,0]+1
            index2=edit_object2[i+1,0]+1
            index3=edit_object2[i+2,0]+1
            
            index4=edit_object2[i,1]+1
            index5=edit_object2[i+1,1]+1
            index6=edit_object2[i+2,1]+1
            
            index7=edit_object2[i,2]+1
            index8=edit_object2[i+1,2]+1
            index9=edit_object2[i+2,2]+1
            writeline(3,"f "+str(index1)+"/"+str(index4)+"/"+str(index7)+" "+str(index2)+"/"+str(index5)+"/"+str(index8)+" "+str(index3)+"/"+str(index6)+"/"+str(index9))
        next i
        writeline(3,"# thats it ")
        //message("saved something?")
    closefile(3)

    if getobjectexists(object)=1 then deleteobject(object)
    loadobject(object,filename4$)
    //deletefile(filename4$)
endfunction 1

function adjustpivotpoint(object1,limb,x#,y#,z#)
//first load it in
//loadobj(object1,filename$)
count_objectdata(object1,limb)
loadobjectarray(object1,limb)
//adjust all vertex data offsets
for v=0 to max_vertexcount step 1
    edit_object1#[v,0]=edit_object1#[v,0]+x#
    edit_object1#[v,1]=edit_object1#[v,1]+y#
    edit_object1#[v,2]=edit_object1#[v,2]-z#
next v
//save temporary object and reload it.
//savenewobject(object1,"temporary_smd.obj",1,1)
if limb=0 then savenewobjectlimb(object1,object_filenames$[object1,limb,0])
if limb>0 then savenewobjectlimb(limb_start_obj+(object1*20)+limb,object_filenames$[object1,limb,0])
endfunction


function curvevalue#(current#,dest#,speed#)
diff#=current#-dest#
value#=current#-(diff#/speed#)
endfunction value#


function greater#(a#,b#)
	
	if a#=>b# then exitfunction a#
endfunction b#

function opposite#(b#)
if b#>0.0
	b#=b#-b#-b#
	exitfunction b#
endif
b#=abs(b#)
endfunction b#



