#*********************horsewalk.py*************************
#
#   Emily Echevarria
#
#   usage:   python horsefallen2.py 
#
#   Blobby horse is in ellipsoid form.
#   The blobby horse walks back and forth eventually making the 
#   rider fall off.  The horse resumes walking after the rider
#   has fallen.
#*********************************************************

from visual import *

mult = 2
deltax = .0002/mult
nframes = 500*mult
nframes2 = 5000*mult



# Initialize the frame system
# and create hierarchy (i.e. PushMatrix)
world_fr=frame()

#scene.mouse.camera.pos = (0, 0, -5)
floor = box(length=50, height=.1, width=20, color=color.green, pos = (0, -4.2, 0))


xaxis = (1, 0, 0)
yaxis = (0, 1, 0)
zaxis = (0, 0, 1)

M_PI = 3.15159


#HORSE

# Initialize the frame system for the horse
barrel = frame(frame = world_fr)
point_of_hip = frame(frame = barrel)
point_of_croup = frame(frame = barrel)
withers = frame(frame = point_of_croup)
point_of_tail = frame(frame = point_of_hip)
point_of_tail2 = frame(frame = point_of_tail)
offpoint_of_hip = frame(frame = point_of_hip)
nearpoint_of_hip = frame(frame = point_of_hip)
poll = frame(frame = withers)
right_earframe = frame(frame = poll)
left_earframe = frame(frame = poll)
offside_elbow = frame(frame = point_of_croup)
offside_knee = frame(frame = offside_elbow)
offsidefore_pasternjoint = frame(frame = offside_knee)
nearside_elbow = frame(frame = point_of_croup)
nearside_knee = frame(frame = nearside_elbow)
nearsidefore_pasternjoint = frame(frame = nearside_knee)
offside_hock = frame(frame = offpoint_of_hip)
offsideback_pasternjoint = frame(frame = offside_hock)
nearside_hock = frame(frame = nearpoint_of_hip)
nearsideback_pasternjoint = frame(frame = nearside_hock)
offsidefore_hoof = frame(frame = offsidefore_pasternjoint)
nearsidefore_hoof = frame(frame = nearsidefore_pasternjoint)
offsideback_hoof = frame(frame = offsideback_pasternjoint)
nearsideback_hoof = frame(frame = nearsideback_pasternjoint)


#Initialize joints for the horse in their correct frames
witherspoint = sphere(frame = withers, pos=(0,0,0), color = color.yellow, radius = .2)




point_of_crouppoint = sphere(frame = point_of_croup, pos=(0,0,0), color = color.orange, radius = .2)
point_of_tailpoint = sphere(frame = point_of_tail, pos=(0,0,0), color = color.red, radius = .2)
point_of_tail2point = sphere(frame = point_of_tail2, pos=(0,-.1,0), color = color.red, radius = .2)
pollpoint = sphere(frame = poll, pos=(-.5,0,0), color = color.red, radius = .2)
right_earpoint = sphere(frame = right_earframe, pos=(0,0,0), color = color.red, radius = .2)
left_earpoint = sphere(frame = left_earframe, pos=(0,0,0), color = color.red, radius = .2)
offside_elbowpoint = sphere(frame = offside_elbow, pos=(0,0,0), color = color.orange, radius = .2)
offside_kneepoint = sphere(frame = offside_knee, pos=(0,0,0), color = (color.yellow), radius = .4)
nearside_kneepoint = sphere(frame = nearside_knee, pos=(0,0,0), color = (color.yellow), radius = .4)
offside_hockpoint = sphere(frame = offside_hock, pos=(0,0,0), color = (color.yellow), radius = .4)
nearside_hockpoint = sphere(frame = nearside_hock, pos=(0,0,0), color = (color.yellow), radius = .4)
nearside_elbowpoint = sphere(frame = nearside_elbow, pos=(0,0,0), color = color.orange, radius = .2)
offsidefore_pasternjointpoint = sphere(frame = offsidefore_pasternjoint, pos=(0,0,0), color = (color.yellow), radius = .35)
nearsidefore_pasternjointpoint = sphere(frame = nearsidefore_pasternjoint, pos=(0,0,0), color = (color.yellow), radius = .35)
offsideback_pasternjointpoint = sphere(frame = offsideback_pasternjoint, pos=(0,0,0), color = (color.yellow), radius = .35)
nearsideback_pasternjointpoint = sphere(frame = nearsideback_pasternjoint, pos=(0,0,0), color = (color.yellow), radius = .35)
offsideback_pasternjointpoint = sphere(frame = offsideback_pasternjoint, pos=(0,0,0), color = (color.yellow), radius = .35)
nearsideback_pasternjointpoint = sphere(frame = nearsideback_pasternjoint, pos=(0,0,0), color = (color.yellow), radius = .35)
offsidefore_hoofpoint = ellipsoid(frame = offsidefore_hoof, pos=(0,0,0), size = (.9, .6, .8), color = color.orange)
nearsidefore_hoofpoint = ellipsoid(frame = nearsidefore_hoof, pos=(0,0,0), size = (.9, .6, .8), color = color.orange)
offsideback_hoofpoint = ellipsoid(frame = offsideback_hoof, pos=(0,0,0), size = (.9, .6, .8), color = color.orange)
                                
nearsideback_hoofpoint = ellipsoid(frame = nearsideback_hoof, pos=(0,0,0), size = (.9, .6, .8), color = color.orange)
offpoint_of_hippoint = sphere(frame = offpoint_of_hip, pos=(0,0,0), color = color.green, radius = .2)
nearpoint_of_hippoint = sphere(frame = nearpoint_of_hip, pos=(0,0,0), color = color.green, radius = .2)


#Initialize the horse's body parts
ribs = ellipsoid(frame = point_of_croup, pos=(2, 0, 0), size = (6, 2.8, 2.8), color = color.yellow)
sternum = ellipsoid(frame = point_of_croup, pos=(4, 0, 0), size = (3, 3, 3), color = color.yellow)
hindquarters = ellipsoid(frame = point_of_croup, pos=(0, 0, 0), size = (3, 3, 3), color = color.yellow)
cervicalvertebrae = ellipsoid (frame = withers, pos=(2, -.2, 0), size = (5, 1.7, 1), color = (color.yellow))
head1 = ellipsoid(frame = poll, pos=(0, -.5, 0), size = (1.8, 1.8, 1.7), color = (color.yellow))
head2 = ellipsoid(frame = poll, pos=(0, -1.6,0), size = (1.3, 1.3, 1.3), color = (color.yellow))

tail1 = ellipsoid(frame = point_of_tail, pos=(0, -.5, 0), size = (.4, 1.5, .4), color = color.red)
tail2 = ellipsoid(frame = point_of_tail2, pos=(0, -1.5,0), size = (.4, 3, .5), color = color.red)

mane1 = ellipsoid(frame = withers, pos=(1.75, .4, 0), length = 5.5, width = .1, depth = .1, color = color.red)
mane2 = ellipsoid(frame = poll, pos=(.37, -.05,0), length =1, width = .1, depth = 2, color = color.red)

offtibia = ellipsoid(frame = offpoint_of_hip, pos=(0, -1.75, 0), size = (1.8, 4, 1.8), color = (color.yellow))
neartibia = ellipsoid(frame = nearpoint_of_hip, pos=(0, -1.75, 0), size = (1.8, 4, 1.8), color = (color.yellow))
offradius = ellipsoid(frame = offside_elbow, pos=(0, -.7, 0), size = (1.2, 3, 1.2), color = (color.yellow))
nearradius = ellipsoid(frame = nearside_elbow, pos=(0, -.7, 0), size = (1.2, 3, 1.2), color = (color.yellow))
offcannon = ellipsoid(frame = offside_knee, pos=(0, -.75, 0), size = (.85, 2, .85), color = (color.yellow))
nearcannon = ellipsoid(frame = nearside_knee, pos=(0, -.75, 0), size = (.85, 2, .85), color = (color.yellow))
offpastern = ellipsoid(frame = offsidefore_pasternjoint, pos=(0, -.5, 0), size = (.7, 1, .7), color = (color.yellow))
nearpastern = ellipsoid(frame = nearsidefore_pasternjoint, pos=(0, -.5, 0), size = (.7, 1, .7), color = (color.yellow))

offbackpastern = ellipsoid(frame = offsideback_pasternjoint, pos=(0, -.5, 0), size = (.7, 1, .7), color = (color.yellow))
nearbackpastern = ellipsoid(frame = nearsideback_pasternjoint, pos=(0, -.5, 0), size = (.7, 1, .7), color = (color.yellow))

offbackcannon = ellipsoid(frame = offside_hock, pos=(0, -.75, 0), size = (.9, 2.5, .9), color = (color.yellow))
nearbackcannon = ellipsoid(frame = nearside_hock, pos=(0, -.75, 0), size = (.9, 2.5, .9), color = (color.yellow))

right_ear = ellipsoid(frame = right_earframe, pos=(.2, .1, 0), size = (1, .4, .6), color = (color.yellow))
left_ear = ellipsoid(frame = left_earframe, pos=(.2, .1, 0), size = (1, .4, .6), color = (color.yellow))





# Move the frames to their correct starting locations (i.e. glTranslate)
# All .pos are relative to the frame that they are in
world_fr.pos=(0,0,0)

#Move the horse's frame to their correct starting locations
barrel.pos=(0, 2.2, 0)
withers.pos=(3.3, 0,0)
point_of_hip.pos = (0, 0, 0)
point_of_croup.pos = (-2, -1, 0)
offpoint_of_hip.pos = (-2,0,.75)
nearpoint_of_hip.pos = (-2,0,-.75)
point_of_tail.pos = (-3, 0, 0)
point_of_tail2.pos = (0, -1, 0)
poll.pos = (4, 0, 0)
left_earframe.pos = (.5, -.2, -.4)
right_earframe.pos = (.5, -.2, .4)
offside_elbow.pos = (4.2, -1, .7)
offside_knee.pos = (0, -2, 0)
offsidefore_pasternjoint.pos = (0,-1.5, 0)
nearside_elbow.pos = (4.2, -1, -.7)
nearside_knee.pos = (0, -2, 0)
nearsidefore_pasternjoint.pos = (0,-1.5, 0)
offsidefore_hoof.pos = (0, -.8, 0)
nearsidefore_hoof.pos = (0, -.8, 0)
offsideback_hoof.pos = (0, -.8, 0)
nearsideback_hoof.pos = (0, -.8, 0)
offside_hock.pos = (0, -3.5, 0)
offsideback_pasternjoint.pos = (0,-2,0)
nearside_hock.pos = (0, -3.5, 0)
nearsideback_pasternjoint.pos = (0,-2,0)

# HUMAN
saddle_fr = frame(frame = point_of_croup)
human_fr = frame(frame=saddle_fr)
waist=frame(frame=human_fr)
neck=frame(frame=waist)
lshoulder=frame(frame=waist)
rshoulder=frame(frame=waist)
lelbow=frame(frame=lshoulder) 
relbow=frame(frame=rshoulder)
lwrist=frame(frame=lelbow)
rwrist=frame(frame=relbow)
lhip=frame(frame=human_fr)
rhip=frame(frame=human_fr)
lknee=frame(frame=lhip)
rknee=frame(frame=rhip) 
lankle=frame(frame=lknee) 
rankle=frame(frame=rknee)
#Initilize joints in their correct frames

saddlepoint = sphere(frame = saddle_fr, pos=(0,0,0), color = color.red, radius = .2)

neckpoint = sphere(frame=neck, pos=(0,0,0), color=color.cyan, radius = .2)
lshoulderpoint = sphere(frame=lshoulder, pos=(0,0,0), color =color.cyan, radius=.25)
rshoulderpoint = sphere(frame=rshoulder, pos=(0,0,0), color= color.cyan, radius=.25)
lelbowpoint = sphere(frame=lelbow, pos=(0,0,-.1), color=color.cyan, radius=.2)
relbowpoint = sphere(frame=relbow, pos=(0,0,-.1), color=color.cyan, radius=.2)
lwristpoint = sphere(frame=lwrist, pos=(0,0,-.1), color= color.cyan, radius=.1)
rwristpoint = sphere(frame=rwrist, pos=(0,0,-.1), color=color.cyan, radius=.1)
lhippoint = sphere(frame=lhip, pos=(0,0,0), color=color.cyan, radius=.3)
rhippoint = sphere(frame=rhip, pos=(0,0,0), color= color.cyan, radius=.3)
lkneepoint = sphere(frame=lknee, pos=(0,0,0), color=color.cyan, radius=.2)
rkneepoint = sphere(frame=rknee, pos=(0,0,0), color=color.cyan, radius=.2)
lanklepoint = sphere(frame=lankle, pos=(0,0,0), color= color.cyan, radius=.2)
ranklepoint = sphere(frame=rankle, pos=(0,0,0), color=color.cyan, radius=.2)
waistpoint = sphere(frame=waist, pos=(0,0,0), color=color.cyan, radius = .4)
# Initialize body parts in their correct frames
utorso = ellipsoid(frame=waist,pos=(0,.5,0), size = (1, 2, 1),
        color=(color.cyan))
ltorso = ellipsoid(frame=human_fr, pos=(0,-.5,0), size =(1, 1.5, 1), color = (color.cyan)) 
head = sphere(frame=neck,pos=(0,.5,0), radius=.5,color=(color.cyan))

lhumerous = ellipsoid(frame=lshoulder, pos=(.5,0,-.1), size=(1.5, .4, .4),
        color=(color.cyan))

rhumerous = ellipsoid(frame=rshoulder, pos=(-.5,0,-.1), size=(1.5, .4, .4),color=(color.cyan))

lradius = ellipsoid(frame=lelbow, pos=(.5,0,-.1), size=(1.5, .4, .3),
        color=(color.cyan))

rradius = ellipsoid(frame=relbow, pos=(-.5,0,-.1), size=(1.5, .3, .4), color=(color.cyan))

lhand = ellipsoid(frame=lwrist, pos=(.25,0,-.1), size=(.5, .3, .3),color=(color.cyan))

rhand = ellipsoid(frame=rwrist, pos=(-.25,0,-.1), size = (.5, .3, .3),color=(color.cyan))

lfemur = ellipsoid(frame=lhip, pos=(0,-.75,0), size=(.5, 2, .5), color=(color.cyan))

rfemur = ellipsoid(frame=rhip, pos=(0,-.75,0), size=(.5, 2, .5), color=(color.cyan))

ltibia = ellipsoid(frame=lknee, pos=(0,-.75,0), size=(.5, 2, .5), color=(color.cyan))

rtibia = ellipsoid(frame=rknee, pos=(0,-.75,0), size=(.5, 2, .5), color=(color.cyan))

lfoot = box(frame=lankle, pos=(0,0,.25), length=.3, height=.2, 
        width=.45,color=(color.cyan))

rfoot = box(frame=rankle, pos=(0,0,.25), length=.3, height=.2, 
        width=.45,color=(color.cyan))
# Move the frames to their correct starting locations (i.e. glTranslate)
# All .pos are relative to the frame that they are in
world_fr.pos=(0,0,0)
human_fr.pos.y = human_fr.pos.y + 2.5
#human_fr.pos.x = human_fr.pos.x + 1.6
saddle_fr.pos.x = saddle_fr.pos.x + 1.6
neck.pos=(0,1.5,0)
lshoulder.pos=(.55,1.3,0)
rshoulder.pos= (-.55,1.3,0)
lelbow.pos=(1.05,0,0)
relbow.pos=(-1.05,0,0)
lwrist.pos=(1.0,0,0)
rwrist.pos=(-1.0,0,0)
lhip.pos=(.35,-1.,0)
rhip.pos=(-.35,-1.,0)
lknee.pos=(0,-1.6,0)
rknee.pos=(0,-1.6,0)
lankle.pos= (0,-1.6,0)
rankle.pos=(0,-1.6,0)
waist.pos=(0, 0,0)

    
# resetcolors()
def resetcolors():
    """
    #reset human colors
    utorso.color=(.2,.2,.9)
    ltorso.color=(.2, .2, .9)
    head.color= (.2,.2,.9)
    lhumerous.color=(.2,.2,.9)
    rhumerous.color=(.2,.2,.9)
    lradius.color= (.2,.2,.9)
    rradius.color=(.2,.2,.9)
    lhand.color=(.2,.2,.9)
    rhand.color=(.2,.2,.9)
    lfemur.color= (.2,.2,.9)
    rfemur.color=(.2,.2,.9)
    ltibia.color=(.2,.2,.9)
    rtibia.color=(.2,.2,.9) 
    lfoot.color=(.2,.2,.9)
    rfoot.color=(.2,.2,.9)
    """
#reset horse's colors
    ribs.color =col
    sternum.color = col
    hindquarters.color = col
    cervicalvertebrae.color = col
    head1.color = col
    head2.color = col
    tail1.color =color.red
    tail2.color = color.red
    mane1.color = color.red
    mane2.color = color.red
    point_of_tailpoint.color = color.red
    point_of_tail2point.color = color.red
    offtibia.color = col
    neartibia.color = col
    offradius.color = col
    nearradius.color = col
    offcannon.color = col
    nearcannon.color = col
    offpastern.color = col
    nearpastern.color = col
    offbackpastern.color = col
    nearbackpastern.color = col
    offbackcannon.color = col
    nearbackcannon.color = col
    right_ear.color = col
    left_ear.color = col
    offside_kneepoint.color = col
    nearside_kneepoint.color = col
    offside_hockpoint.color = col
    nearside_hockpoint.color = col
    offsidefore_pasternjointpoint.color = col
    nearsidefore_pasternjointpoint.color = col
    offsideback_pasternjointpoint.color = col
    nearsideback_pasternjointpoint.color = col
    offsidefore_hoofpoint.color = color.orange
    nearsidefore_hoofpoint.color = color.orange
    offsideback_hoofpoint.color = color.orange
    nearsideback_hoofpoint.color = color.orange

#rotate parts of human
lshoulder.rotate(angle=-M_PI/2, axis=(0,1,0))
lshoulder.rotate(angle=M_PI/4, axis=(0,0,1))
lshoulder.rotate(angle=M_PI/4, axis=(1,0,0))
rshoulder.rotate(angle=M_PI/2, axis=(0,1,0))
rshoulder.rotate(angle=-M_PI/4, axis=(0,0,1))
rshoulder.rotate(angle= M_PI/4, axis=(1,0,0))
relbow.rotate(angle = -M_PI/4, axis=(0, 0, 1))
lelbow.rotate(angle =  M_PI/4, axis=(0,0,1))
relbow.rotate(angle = -M_PI/4, axis=(0, 1, 0))
lelbow.rotate(angle =  M_PI/4, axis=(0,1,0))
waist.rotate(angle = M_PI/8, axis =(1, 0, 0))

lhip.rotate(angle = -M_PI/2, axis = (1, 0, 0))
rhip.rotate(angle=-M_PI/2, axis = (1, 0, 0))
lhip.rotate(angle = M_PI/8, axis= (0, 1, 0))
rhip.rotate(angle=-M_PI/8, axis = (0, 1, 0))
lknee.rotate(angle = 3*M_PI/4, axis = (1, 0, 0))
rknee.rotate(angle=3*M_PI/4, axis = (1, 0, 0))
lknee.rotate(angle = -M_PI/4, axis = (0, 1, 0))
rknee.rotate(angle=M_PI/4, axis = (0, 1, 0))
human_fr.rotate(angle=M_PI/2, axis = (0, 1, 0))


#unbend parts arrays
parts = [lshoulder, lshoulder, lshoulder, rshoulder, rshoulder, rshoulder, relbow, lelbow, relbow, lelbow, waist, lhip, rhip, lhip, rhip, lknee, rknee, lknee, rknee, waist, human_fr]
partangles = [M_PI/2, -M_PI/4, -M_PI/4, -M_PI/2, M_PI/4, -M_PI/4, M_PI/4, -M_PI/4, M_PI/4, -M_PI/4, -M_PI/8, M_PI/2, M_PI/2, -M_PI/8, M_PI/8, 3*M_PI/4, -3*M_PI/4, -M_PI/4, M_PI/4, M_PI/2, M_PI/2]
partaxes = [yaxis, zaxis, xaxis, yaxis, zaxis, xaxis, xaxis, zaxis, yaxis, yaxis, xaxis, xaxis, xaxis, yaxis, yaxis, yaxis, yaxis, yaxis, yaxis, xaxis, xaxis]


  
#rotate parts of horse to begin in initial stance
nearsidefore_pasternjoint.rotate(angle=M_PI/8, axis=(0,0,1))
offsidefore_pasternjoint.rotate(angle=M_PI/8, axis=(0,0,1))
nearsideback_pasternjoint.rotate(angle=M_PI/16, axis=(0,0,1))
offsideback_pasternjoint.rotate(angle=M_PI/16, axis=(0,0,1))
nearsidefore_hoof.rotate(angle=-M_PI/8, axis=(0,0,1))
offsidefore_hoof.rotate(angle=-M_PI/8, axis=(0,0,1))
nearsideback_hoof.rotate(angle=-M_PI/8, axis=(0,0,1))
offsideback_hoof.rotate(angle=-M_PI/8, axis=(0,0,1))
withers.rotate(angle=M_PI/4, axis=(0,0,1))
offpoint_of_hip.rotate(angle =-M_PI/16, axis=(0,0,1))
nearpoint_of_hip.rotate(angle=-M_PI/16, axis=(0,0,1))
offside_hock.rotate(angle=M_PI/8, axis = (0,0,1))
nearside_hock.rotate(angle=M_PI/8, axis = (0,0,1))
point_of_tail.rotate(angle=-M_PI/6, axis = (0,0,1))
point_of_tail2.rotate(angle=M_PI/6, axis = (0,0,1))


picked=None             # What body part is picked
mousedown=false          # Is the mouse being dragged?

# Set the window title
scene.title="Blobby Horse for VPython, By Emily Echevarria, Math198 Spring 2006"

# Set up the scene, we want autoscaling off or VPython will resize
# the scene and it can be distracting
scene.center=(0,-1,0)
scene.autoscale=0



bendkneeangle = -(M_PI/180)*90
bendpasternangle = -(M_PI/180)*90
bendelbowangle = (M_PI/180)*90

bendhip = (M_PI/180)*10
bendhock = (M_PI/180)*10
bendhindpastern = -(M_PI/180)*15
bendtail = (M_PI/180)*30
bendhead = (M_PI/180)*10


nearforeoffhind = [nearside_elbow, offpoint_of_hip, offside_elbow, nearpoint_of_hip, nearside_knee, offside_hock, offside_knee, nearside_hock, nearsidefore_pasternjoint, offsideback_pasternjoint, offsidefore_pasternjoint, nearsideback_pasternjoint, withers, point_of_tail]
offforenearhind = [offside_elbow, nearpoint_of_hip, nearside_elbow, offpoint_of_hip, offside_knee, nearside_hock, nearside_knee, offside_hock, offsidefore_pasternjoint, nearsideback_pasternjoint, nearsidefore_pasternjoint, offsideback_pasternjoint, withers, point_of_tail]
angup = [bendelbowangle*.7, bendhip, -bendelbowangle/5, -bendhip*1, bendkneeangle/2, bendhock*2.5, 0*bendkneeangle/4, -bendhock*2, bendpasternangle*.7, bendhindpastern*3.5, -bendpasternangle*.2, bendhindpastern*1, -bendhead, -bendtail]
angdown = [-bendelbowangle*.7, -bendhip, bendelbowangle/5, bendhip*1, -bendkneeangle/2, -bendhock*2.5, 0*-bendkneeangle/4, bendhock*2, -bendpasternangle*.7, -bendhindpastern*3.5, bendpasternangle*.2, -bendhindpastern*1, bendhead, bendtail]

zaxes = [zaxis, zaxis, zaxis, zaxis, zaxis, zaxis, zaxis, zaxis, zaxis, zaxis, zaxis, zaxis, zaxis, zaxis]

#trotanglefactor = 1.2
#walkspeed = 600
#offsetwalk = .0002

countsteps = 0
off = deltax

stillgoing = true
mouseclicked = false
fallen = false
def bendjoint(frame, angle, axis, OFFSET = .005, SPEED = nframes, ANGLEFACTOR=1.2, MAX_FRAMES = 14, MOVELATERALLY = 1, ROTATE = true):
    count = 0
    rotationCounter = 0

    while (count < SPEED):
        i = 0
        
        while( i < MAX_FRAMES):
            if(ROTATE):
                rotation = ANGLEFACTOR*angle[i]/SPEED
                tempframe = frame[i]
                tempframe.rotate(angle = rotation, axis = axis[i])
            if(MOVELATERALLY == 1):
                barrel.pos.x = barrel.pos.x + OFFSET
            if(MOVELATERALLY == 2):
                human_fr.pos.x = human_fr.pos.x - .0001
                
                
            
            i = i + 1
            
        count = count + 1
        
def movelaterally(fr, offsetx):
    fr.pos.x = fr.pos.x + offsetx

def stall():
    stall = 0
    while(stall !=1000000):
            stall = stall + 1



def movefrontlegs(OFFSET = .000025):

        bendjoint(offforenearhind, angup, zaxes, OFFSET=OFFSET)
    

        

        bendjoint(offforenearhind, angdown, zaxes, OFFSET=OFFSET)

        

        
        bendjoint(nearforeoffhind, angup, zaxes, OFFSET=OFFSET)
        

   
        
        bendjoint(nearforeoffhind, angdown, zaxes, OFFSET=OFFSET)

mouseclicked = false
riderCounter = 0



def checkClick():
    if scene.mouse.events:  #rotating mode
        m1 = scene.mouse.getevent() # obtain drag or drop event

        if m1.click == 'left':
            mouseclicked = true
            return true

def throwRider():
    #animation goes here
    riderCounter = 0
    horseCounter = 0
    bendjoint(frame = [point_of_croup, offside_elbow, nearside_elbow, nearside_knee, offside_knee, nearsidefore_pasternjoint, offsidefore_pasternjoint], angle=[M_PI/6, M_PI/2, M_PI/3, -M_PI/3, -M_PI/2, -M_PI/3, -M_PI/3], axis=[zaxis, zaxis, zaxis, zaxis, zaxis, zaxis, zaxis], SPEED = nframes2, MAX_FRAMES = 7, MOVELATERALLY = 15)

    

    bendjoint(frame = parts, angle = partangles, axis = partaxes, MAX_FRAMES = 21, MOVELATERALLY = 2, SPEED = 6600)

    
    human_fr.frame = world_fr
    human_fr.rotate(angle = -M_PI/180*175, axis = yaxis)
    human_fr.pos = (23.9, -3.55, -.35)
    human_fr.rotate(angle = -M_PI/180*36.5, axis = zaxis)
        
    bendjoint(frame = [point_of_croup, offside_elbow, nearside_elbow, nearside_knee, offside_knee, nearsidefore_pasternjoint, offsidefore_pasternjoint], angle=[-M_PI/6, -M_PI/2, -M_PI/3, M_PI/3, M_PI/2, M_PI/3, M_PI/3], axis=[zaxis, zaxis, zaxis, zaxis, zaxis, zaxis, zaxis], SPEED = nframes2, MAX_FRAMES = 7, MOVELATERALLY = 0)
       
iterationcount =1
fifthiteration = false
i = 1
j = 1
k = 0
while(1):
    while (fifthiteration == false):
    
        if(countsteps == 4):
            barrel.rotate(angle = (M_PI/180)*180, axis = [0, 1, 0])
            off = -off
            countsteps = 0
            if(fallen == true):
                col = (i, j, k)
                oldi = i
                oldj = j
                oldk = k
                i = oldk
                j = oldi
                k = oldj
                resetcolors()
        movefrontlegs(OFFSET = off)

        countsteps = countsteps + 1
        iterationcount = iterationcount + 1
        if(iterationcount == 15 and fallen == false):
            fifthiteration = true

    throwRider()
    fallen = true
    countsteps = 0
    fifthiteration = false