EMOTIONS CODE

 

 

#include <Mega32.h>

#include <Stdio.h>

#include  <Stdlib.h>

#include <Math.h>

#include <Delay.h>

//#include "keypad.c"

//#include "debuglcd.c"             

#include "evalcdfinal1.c"

#include "debuglcd_no.c"                 

#include "songfinal1.c"

#include "PWMTimer3.c"

 

                                                                   

 

void UpdateConditionTimers(unsigned char Action);

double random(void);

                                           

int Reload;

unsigned char emotion , ImprovedState , i ,  Playpoints , j, DisciplineNeeded;

int StateTransitions , TimeSinceLastHappy , TimeToNaughty , Timetobored , TotalTime , TimeToToilet;

int TimeToRelease , TotalUselessAction , TimeToSick , TimeToStarve,TimeToHungry;

int Timeinstate , CurrAttentionPoints , Seconds , Minutes , AttentionPointsNeeded;

unsigned char History[50] , CurrButtonPushes[10] , NewButtonPushed,PrevState;

int W_TimeToHungry , W_TimeToToilet , Time1 , Time2 , milliseconds , State_milliseconds;

int W_TimeToRelease , W_HungerLevel , W_TimeToStarve , W_TimeToSick , W_MedicationLevel , W_SickNotTreated , W_HungerNotFed, W_DisciplineNeeded;

unsigned char State_seconds , State_minutes;

float Tempfloat;

unsigned char NeedToPee , Sick , Hungerlevel , Hungry , Medicationlevel , Starve , Urgent , Spank;

unsigned char DisciplineCount, DisciplineDowngrade,DisciplineUpgrade;

int Time3;

unsigned char TimeMessNotClean , TimeHungerNotFed , TimeSickNotTreated , W_MessNotClean, TimeNotDiscipline, W_NotDiscipline;

unsigned char W_DisciplineUpgrade,W_TimeNotDiscipline,FirstTime, W_HealthUpgrade;

 

int TimeToDegenerate , TTDDueToUselessActions,counter1,counter2,counter3, Time4,counter5,counter6,counter7;

 // Condition Variables

unsigned char Health , Discipline , Happiness, IsNaughty,Disciplined;

unsigned char FedInTime, ToiletInTime, PrevPushed;

 

unsigned char SadEffects , HappyEffects , ExcitedEffects , BoredEffects , AngryEffects , NaughtyEffects;  

unsigned char r_HealthUpgrade, r_HealthDowngrade;                

int temp;

unsigned char barDisc,barH,barHealth;

        

 

#define play 7

#define tickle 1

#define feed 4

#define medicine 6

#define toilet 9

#define spank 3

 

#define t1 2

#define t2 2

#define t3 30 // Check Every 30ms

#define t4 30

 

#define Excited 1

#define Happy 2

#define Neutral 3

#define Bored 4

#define Sad 5

#define Angry 6

#define Naughty 7  

#define Death 8

 

#define Reset    0

#define Release 1

#define DebouncePressed 2

#define StillPressed 3

#define DebounceRelease 4    

 

#define maxkeys 17

 

unsigned char keycount,keyready, Maybe;

flash unsigned char keytbl[16]={0x7d, 0xee, 0xed,0xeb,0xde, 0xdd,0xdb,0xbe, 0xbd, 0xbb,0xe7,0xd7,0xb7,0x77}; //   6

unsigned char keypressed[10],key,butnum,tempnumber,State;

int tcounter=0;

 

 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

 // Timer 0 Overflow Isr

interrupt[TIM0_OVF] void timer0_overflow(void)

  {

   // Reload To force 1 Msec Overflow

  TCNT0 = Reload;

 

   // Decrement The Three Times if They Are Not Already Zero

 

  if(Time1 > 0) --Time1;

  if(Time2 > 0) --Time2;

if(Time3 > 0) --Time3;

if(Time4 > 0) --Time4;     

if(playsongtime>0) --playsongtime;           

if(servotimer>0) --servotimer; 

//output(servotimer);

 

  }

 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

 

void Initialize(void)

{

//Kelvin's code

  DDRB=0xff;    // PORT B - LCD Control Line

  DDRC=0xff;    // PORT C - LCD Command Line  

  LCDinit();

 

  // Setup Timer 0

 Reload = 256 -125 ; // Value for 1 Msec Must Set To 256 -125!!!

//Reload = 5;

 TCNT0 = Reload ; // Preload Timer 0 So That Is interrupts After 1 Msec.

 TCCR0 = 0x03 ; // Prescalar To 64

 

  // Set Up Timer 1

    TIMSK = 0x01 ; // Turn On Timer0 Overflow Isr

  

 InitSound();      

 ServoInit();

 

 UCSRB = 0x18;

 UBRRH = 0;

 UBRRL = 103;        

 

 StateTransitions = 0;

 emotion = Neutral;

 for(i = 0 ; i < 5 ; i++ )

            CurrButtonPushes[i] = 0;

 TimeSinceLastHappy = 0;

 TotalTime = 0;

 // Timetoboredduetofeeding = 0;

 TimeToNaughty = 0;

 TimeToToilet = 0;

 TimeToRelease = 0;

 TimeToHungry = 0;   

 FedInTime=0;

 ToiletInTime=0; 

 PrevState=8;

 

  // Weights

 W_TimeToHungry = 60;

W_TimeToToilet = 80;

 W_TimeToRelease = 40;

 W_HungerLevel = 20;

W_TimeToStarve = 40;

 W_TimeToSick = 100;

 W_MedicationLevel = 20;

 W_HungerNotFed = 10;

 W_SickNotTreated = 10;

 W_MessNotClean = 10;

 W_TimeNotDiscipline = 10;             //amount of time not disciplined to decrease discipline

 W_DisciplineNeeded=10;                                 //# of button pushes to implement discipline

 W_DisciplineUpgrade = 10;                  //# of disciplines to improve discipline of EVA 

 W_HealthUpgrade = 10;

 

 milliseconds = 0;

 Seconds = 0;

 Minutes = 0;

 State_milliseconds = 0;

 State_seconds = 0;

 State_minutes = 0;

 FirstTime=1;   

 counter1=0;

 counter2=0;       

 counter3=0;

 counter5=0;   

 counter6=0;

 

 

     TimeMessNotClean = 0 ; // Tracks How Long User Takes To Clean Up Toilet Mess

     TimeHungerNotFed = 0 ; // Tracks How Long Eva Has Been Starving

     TimeSickNotTreated = 0 ; // Tracks How Long Eva Has Been Sick And Not Treated

     TimeNotDiscipline = 0;

     DisciplineCount = 0;

     DisciplineDowngrade=random()*W_TimeNotDiscipline;

     DisciplineUpgrade=random()*W_DisciplineUpgrade;           

             r_HealthUpgrade=  1; //W_HealthUpgrade*random();  

             r_HealthDowngrade=   W_SickNotTreated *random();        

             //output(r_HealthDowngrade);

             

  // State Variables

 Sick = 0;

 Hungry = 0;

 NeedToPee = 0;

 Starve = 0;

 Urgent = 0;

 Disciplined = 0;

 IsNaughty=0;

 

 State = Reset ; // Reset State Of Keypad

 

 Health =45;

 Discipline = 45;

 Happiness =45;

                       

 AngryEffects=5;

 BoredEffects=12;

 SadEffects=8;

 HappyEffects=1000;

 ExcitedEffects=1000;

 

 UpdateConditionTimers(feed);

 UpdateConditionTimers(toilet);

 UpdateConditionTimers(medicine);       

 

 Time4=t4;

 Time1=t1;

 Time2=t2;

 Time3=t3;

     

            barDisc=0;

            barH=0;

            barHealth=0;

 

 

        // Crank Up The Isrs

  #asm

   sei

  #endasm

 

}

 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

 

void Reinitialize(void)

{                               

 ResetServo();                                       //reset servos         

 playsong(emotion);         

 for(i = 0 ; i <= 9 ; i++ )

            CurrButtonPushes[i] = 0;

 TTDDueToUselessActions = 0;

 TimeToNaughty = 0;

 State_milliseconds = 0;

 State_seconds = 0;

 TimeMessNotClean = 0 ; // Tracks How Long User Takes To Clean Up Toilet Mess

 TimeHungerNotFed = 0 ; // Tracks How Long Eva Has Been Starving

 TimeSickNotTreated = 0 ; // Tracks How Long Eva Has Been Sick And Not Treated

 FirstTime=1;     

  

 }                      

//*************************************************************

void servofunction(unsigned char feelings)

{           

 

            servotimer = t5;       

            switch(feelings)

            {

                        case Happy:

            tcounter++;

                                    HappyServo(); 

                                    break;              

                        case Excited:                

                                    ExcitedServo();

                                    break;                          

                        case Bored:

                                    BoredServo();

                                    break;                          

                        case Sad:

                                    SadServo();

                                    break;                          

                        case Angry:

                                    AngryServo();

                                    break;                          

                        case Naughty:

                                    NaughtyServo();

                                    break;                          

            }//switch(feelings)                                

 

}//void servofunction

 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

char keypad(void)         

{         

            //get lower nibble

             DDRA = 0x0f;

             PORTA = 0xf0;

             delay_us(5);

             key = PINA;

 

             //get upper nibble

             DDRA = 0xf0;

             PORTA = 0x0f;

             delay_us(5);

             key = key | PINA;  

            //find matching keycode in keytbl

             if (key != 0xff)

                        {            

                for (butnum=0; butnum<maxkeys; butnum++)

                        {                  

                        if (keytbl[butnum]==key)

                        {

                                      break;           

                        }

                                   

                        }  

                        } 

             else butnum=17;

            return butnum;  

 

 

}//keypad

//*****************************      

 

void KeypadStateMachine(void)

{     

 

                        switch (State)

            {

                        case Reset:

                                    State = Release;

                                    tempnumber=keypad();  

                                    break;

                        case Release:     

                                    if(tempnumber>=17)

                                    {

                                                tempnumber=keypad();  

                                    }

                                    else

                                    {

                                                State=DebouncePressed;

                                                Maybe=tempnumber;

                                                tempnumber=keypad();        

                                               

                                    }                                             

 

                                    break;  

                       

                        case DebouncePressed:                        

                                    if(tempnumber==Maybe)

                                                {                     

                                                                        keycount = 0;    //resets array of button pushes                                                   

                                                            State=StillPressed;        

                                                            keypressed[keycount++]=tempnumber;  

                                                            tempnumber=keypad();    

                                                              //Highlight(keypressed[0],1);           

                                                }

                                    else                             

                                    {

                                                State=Release;

                                    }

                                                tempnumber=keypad();             

                                    break;    

 

                        case StillPressed:

                                    if (tempnumber==keypressed[0])   //remains in stillpressed if true

                                    {

                                                tempnumber=keypad();

                                                keycount = 2;

                                    }                      

                       

                                    else

                                    {

                                                State=DebounceRelease;

                                                tempnumber=keypad();

                                    }

                        break;

 

                        case DebounceRelease:

                                    if (tempnumber==keypressed[keycount-1])

                                                State=StillPressed;

                                    else

                                    {

                                                State=Release;

                                                tempnumber=keypad();

                                    }

 

                        break;

 

 

     }//switch   

} //KeypadStateMachine

//*************************************

 

char ButtonPushed(void)

{ 

key=0;

keycount=0;    

 KeypadStateMachine();

if(keycount==1)      

 return keypressed[0];    

else

 return 17;

 

}

 

 

//*********************************************

void AddHistory(unsigned char CurrState)

{

            if(PrevState!=CurrState)

            {

                        PrevState=CurrState;   

                        History[StateTransitions++] = CurrState;

             }

}

 

 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

 

void UpdateButtonPushes() //(char * CurrButtonPushes)

{

 Time3 = t3;

 NewButtonPushed = ButtonPushed();

 

 if(NewButtonPushed != 17)

 {                        

            if(keycount == 1)

            {

                        Highlight(NewButtonPushed,1);        

                        CurrButtonPushes[NewButtonPushed] = CurrButtonPushes[NewButtonPushed] + 1;

                        print(CurrButtonPushes[0]);

                        PrevPushed=NewButtonPushed;

                        counter5=0;          

            }//if(keycount)  

 }// if(NewButtonPushed)

    if(counter5==10)

            {

                        Highlight(PrevPushed,0);

                        counter5=0;                  

            }

            else

                        counter5++;

 

}                                    

 

 

 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

 

 

double random(void)

{

 // randomize();

      return(double)rand() / 32727;

}

 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

 

int randomize_naughty(char CurrState)

{      

                        return (int)(random() * 10 +(8-CurrState) * 5)+Seconds; 

 

}

 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

void TrackTotalTime(void)

{

            Time2 = t2;

            milliseconds++ ;

            if(milliseconds == 1000)

            {

                        Seconds++ ;

                        milliseconds = 0;             

 printf("Time:%d Hungry:%d/%d Urgent:%d/%d Sick:%d Health:%d/%d Disc:%d/%d Happy:%d\r",Seconds,Hungry,Starve,NeedToPee,Urgent,Sick,Health,ToiletInTime+FedInTime,Discipline,DisciplineUpgrade-DisciplineCount,Happiness);   

 

            }

           

}

 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

void TrackTimeInState(void)

{

            State_milliseconds++ ;

            if(State_milliseconds == 1000)

            {

                        State_seconds++ ;

                        State_milliseconds = 0;

             }

}

//**************************************************************

void HealthUpgradePoints(unsigned char action)

{

 

           

            if(action==toilet)

            {

                        ToiletInTime++;

            }

            else

                        FedInTime++;                                  

           

            Health++;

            Discipline++;

                       

            if(ToiletInTime + FedInTime < 0)

            {         

                        if(Health>0) Health--;

                        r_HealthUpgrade=  W_HealthUpgrade*random();

                        ToiletInTime=0;

                        FedInTime=0;                                 

                        TimeSickNotTreated=0;

            }                                             

                       

}

//----------------------------------------------------

void UpdateLCD(void)

{                                   

            Time4=t4;

            DisplayEmotion(emotion);        

            UpdateStatus(Discipline, Happiness, Health,barDisc,barH,barHealth);

}

 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

 

void StateMachine(void)

{

 

            Time1 = t1;

            switch(emotion)

            {

            case Neutral:

 // - - - - - - - - - - - - - - - - -NEUTRAL- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 

                        PrintLCD_emotion(emotion);     

                TrackTimeInState() ; // Increment Time Spent In Current State

 

                        if(FirstTime == 1) // Don                            't repeat unnecessary calculations

                        {               

                       

                        FirstTime=0;

                                    TimeToNaughty = randomize_naughty(Neutral);  

                            AddHistory(Neutral);

                            if(random() > 0.5) // Determine Whether To Go Happy Or Excited

                               ImprovedState = Excited ; // When Given Enough Attention Points

                            else ImprovedState = Happy;

                                       Tempfloat = random() * 10;

 

                        AttentionPointsNeeded = Tempfloat + 5 ; // Determines Number Of Presses Of Tickling

                                                                                                                                    // And Playing To Improve State

                                    if(random() > 0.7) // Determine Whether Eva Prefers To Play Or Be

                                    Playpoints = 1 ; // Tickled

                                    else Playpoints = 2;

 

                                    j = StateTransitions;

                            TimeSinceLastHappy = 0;

                            for(j = StateTransitions ; j > 0 ; j--)

                                    {                                                                      // Check When Eva Was Last Happy Or Excited                                      

                                    TimeSinceLastHappy++ ;

                                    if(History[j]!= Happy || History[j]!= Excited)

                      break;

                                    } // while(history[j])

 

                        if(TimeSinceLastHappy == StateTransitions) // Was Never Happy B4

                                    {

                                    TimeSinceLastHappy = 0;

                                    TimeToDegenerate = 20+Seconds;

                                    } // if(TimeSinceLastHappy)

                                    else

                                    {

                        //TimeToDegenerate = 20 -TimeSinceLastHappy * 30 * 10+Seconds;

                        TimeToDegenerate = TimeSinceLastHappy * random()*30+Seconds;

                        if(TimeToDegenerate < 0) // Incase Was Happy Very Recently , Don't want to become negative

                                    TimeToDegenerate = 20+Seconds;

                        } // else

                                   

                                    TTDDueToUselessActions = random() * 5 + 80 ; // Ttd = Time To Degenerate

           debug(AttentionPointsNeeded,TimeToNaughty,TimeToDegenerate,emotion,TTDDueToUselessActions,Seconds,TimeToHungry,TimeToStarve,TimeToToilet,TimeToRelease,TimeToSick);

                        } // if(TimeToNaughty)

                        // Computes Current Attention Points

                        CurrAttentionPoints = CurrButtonPushes[play] * Playpoints + CurrButtonPushes[tickle];

 

                        TotalUselessAction = (!Hungry)*CurrButtonPushes[feed] + (!Sick)*CurrButtonPushes[medicine] + (!NeedToPee)*CurrButtonPushes[toilet];

         

            if(TimeToToilet == Seconds)

            {

                                    CurrButtonPushes[toilet] = 0 ; // Resets Number Of Toilet Pushes

                                    NeedToPee = 1;                                                   

                                    DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);                                    

                        }

                        if(TimeToHungry == Seconds)

                        {

                                    CurrButtonPushes[feed] = 0;

                                    Hungry = 1;         

                                                   //       DrawFood();

                                    DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);

                        }

 

                        if(CurrButtonPushes[spank] > 2)

            {

                        emotion = Angry;

                        Reinitialize();

                        Disciplined = 1;

                        ////PORTB=~0xcf;

            }

            else

                        if(TimeToSick < Seconds)

                        {

                                    Sick = 1;

                                    emotion = Sad;       

                                    DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);                                    

                                    Reinitialize();

                        } // if(TimeToSick)

                        else

                        if(NeedToPee == 1 && CurrButtonPushes[toilet] > 0)

                        {

                                    if(random() < 0.6)

                                    emotion = Neutral;

                                    else

                                    emotion = Happy;

                                    NeedToPee = 0;             

                                    HealthUpgradePoints(toilet);

                                    UpdateConditionTimers(toilet);

                                    ClearToilet();

                        } // if(NeedToPee)

                        else

                        if(Hungry == 1 && CurrButtonPushes[feed] > Hungerlevel)

                        {

                                    Hungry = 0;

                                    CurrButtonPushes[Hungry] = 0;

                                    if(random() < 0.6)

                                    emotion = Happy;

                                    else

                                    emotion = Neutral;      

                                    HealthUpgradePoints(feed);

                                    UpdateConditionTimers(feed);            

                                    ClearFood();

                                    Reinitialize();

                        } // if(Hungry)

                        else

                        if(CurrAttentionPoints > AttentionPointsNeeded)

                        {

                                    emotion = ImprovedState;

                                    Reinitialize();   

                        } // if(CurrAttentionPoints)

                        else

                        if(TimeToRelease < Seconds)

                        {

            emotion = Angry;

            Urgent = 1;       

                                    Reinitialize();

                                    UpdateConditionTimers(toilet);  

                                    DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);                                    

                        }//if(timetorelease)

        else

        if(TimeToStarve < Seconds && Hungry)

        {        

            emotion = Angry;

            Starve = 1;  

            DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);  

                                    Reinitialize();  

                        }

                        else

                        if(TimeToNaughty < Seconds)

                        {

                                    emotion = Naughty;

                                    Reinitialize();

                        }

                        else

        if(TimeToDegenerate < Seconds || TotalUselessAction > 3 * TTDDueToUselessActions)

                        {    

                                    emotion = Bored;

                                    Reinitialize();

                        } // if(Timetobored)

 

         break;

 // - -- - - - - - - - - - - -BORED - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 case Bored:

            PrintLCD_emotion(emotion);

      TrackTimeInState() ; // Increment Time Spent In Current State

 

                        if(TimeToNaughty == 0) // Don't repeat unnecessary calculations

                        {

                                    TimeToNaughty = randomize_naughty(Bored);

                                    AddHistory(Bored);

                        if(random() > 0.2) // Determine Whether To Go Happy Or Excited

                                    ImprovedState = Happy ; // When Given Enough Attention Points

                        else ImprovedState = Excited;

                                    Tempfloat = random() * 10;

 

                        AttentionPointsNeeded = Tempfloat + 5 ; // Determines Number Of Presses Of Tickling and Playing To Improve State

                                    if(random() > 0.7) // Determine Whether Eva Prefers To Play Or Be

                                    Playpoints = 1 ; // Tickled

                                    else Playpoints = 2;

 

                                    j = StateTransitions;

                                    TimeSinceLastHappy = 0;

                        for(j = StateTransitions ; j > 0 ; j--)

              // Check When Eva Was Last Happy Or Excited

                           {

                                       TimeSinceLastHappy++ ;

                                        if(History[j]!= Happy || History[j]!= Excited)

                      break;

                                    } // while(history[j])

 

                                    if(TimeSinceLastHappy == StateTransitions) // Was Never Happy B4 So Will Never Transition To Neutral By Itself

                                    {

                                    TimeSinceLastHappy = 0;

                                    TimeToDegenerate = 10+Seconds;

                                    } // if(TimeSinceLastHappy)

                                    else

                                    {

                        TimeToDegenerate = TimeSinceLastHappy * random()*50+Seconds;

 

                        } // else

                                    TTDDueToUselessActions = random() * 20 + 5 ; // Tts = Time To Sad         

                                           debug(AttentionPointsNeeded,TimeToNaughty,TimeToDegenerate,emotion,TTDDueToUselessActions,Seconds,TimeToHungry,TimeToStarve,TimeToToilet,TimeToRelease,TimeToSick);

                        } // if(TimeToNaughty)

 

                        CurrAttentionPoints = CurrButtonPushes[play] * Playpoints + CurrButtonPushes[tickle];

 

                        TotalUselessAction = (!Hungry)*CurrButtonPushes[feed] + (!Sick)*CurrButtonPushes[medicine] + (!NeedToPee)*CurrButtonPushes[toilet];

                        if(State_minutes>random()*BoredEffects)

                        {

                                    if(Happiness>0) Happiness--;

                                    State_minutes=0;

                        }//if(state_minutes)

 

            if(TimeToToilet == Seconds)

            {

                                    CurrButtonPushes[toilet] = 0 ; // Resets Number Of Toilet Pushes

                                    NeedToPee = 1;

                                    DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);  

                        }

                        if(TimeToHungry == Seconds)

                        {

                                    CurrButtonPushes[feed] = 0;

                                    Hungry = 1;        

                                    DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);

                        }

 

            if(CurrButtonPushes[spank] > 2)

            {

                        emotion = Angry;

                        Reinitialize();

                        Disciplined = 1;

            }

            else

                        if(TimeToSick < Seconds)

                        {

                                    Sick = 1;

                                    emotion = Sad;

                                    Reinitialize();                                      

                                    DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);                                    

                        } // if(TimeToSick)

                        else

                        if(NeedToPee == 1 && CurrButtonPushes[toilet] > 0)

                        {

                                    if(random() < 0.6)

                                    emotion = Neutral;

                                    else

                                                emotion = Happy;

                                    NeedToPee = 0;      

                                    HealthUpgradePoints(toilet);

                                    UpdateConditionTimers(toilet);

                                    ClearToilet();

                        } // if(NeedToPee)

                        else

                        if(Hungry == 1 && CurrButtonPushes[feed] > Hungerlevel)

                        {

                        Hungry = 0;     

                        HealthUpgradePoints(feed);

                        UpdateConditionTimers(feed);

                                    CurrButtonPushes[Hungry] = 0;

                                    if(random() < 0.6)

                                    emotion = Happy;

                                    else

                                    emotion = Neutral;

                                    ClearFood();

                        } // if(Hungry)

                        else

                        if(CurrAttentionPoints > AttentionPointsNeeded)

                        {

                                    emotion = ImprovedState;

                                    Reinitialize();

                        } // if(CurrAttentionPoints)

                        else

                        if(TimeToRelease < Seconds)

                        {

                        emotion = Angry;

                        Urgent = 1;

                                    UpdateConditionTimers(toilet);

                                    Reinitialize();              

                                    DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);                                    

                        }

        else

        if(TimeToStarve < Seconds && Hungry)

        {

            emotion = Angry;

            Starve = 1; 

           

                                    Reinitialize();   

                                    DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);                                    

                        }

                        else

                        if(TimeToNaughty < Seconds)

                        {

                                    emotion = Naughty;

                                    Reinitialize();

                        }

                        else

            if(TimeToDegenerate < Seconds || TotalUselessAction > 3 * TTDDueToUselessActions)

                        {      

                        emotion = Sad;

                        Reinitialize();

                        } // if(Timetobored)

 

                                    break;      

 // - - - - - - - - - - - - - - - - -SAD - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 case Sad:

            PrintLCD_emotion(emotion);

                        TrackTimeInState() ; // Increment Time Spent In Current State

                        if(TimeToNaughty == 0) // Don't repeat unnecessary calculations

                        {

                                    TimeToNaughty = randomize_naughty(Sad); 

                                    AddHistory(Sad);

                        if(random() > 0.2) // Determine Whether To Go Happy Or Excited

                                    ImprovedState = Neutral ; // When Given Enough Attention Points

                        else ImprovedState = Bored;

                                    Tempfloat = random() * 10;

 

                        AttentionPointsNeeded = Tempfloat + 5 ; // Determines Number Of Presses Of Tickling and Playing To Improve State

                                    if(random() > 0.7) // Determine Whether Eva Prefers To Play Or Be

                                    Playpoints = 1 ; // Tickled

                                    else Playpoints = 2;

 

                                    TimeSinceLastHappy = 0;

                        for(j = StateTransitions ; j > 0 ; j--)

              // Check When Eva Was Last Happy Or Excited

                                    {

                                    TimeSinceLastHappy++ ;

                                    if(History[j]!= Happy || History[j] != Excited)

                      break;

                                    } // while(history[j])

 

                                    if(TimeSinceLastHappy == StateTransitions) // Was Never Happy B4

                                    {

                                    TimeSinceLastHappy = 0;

                                    TimeToDegenerate = 20+Seconds;

                                    } // if(TimeSinceLastHappy)

                                    else

                                    {

                        TimeToDegenerate=TimeSinceLastHappy*random()*30+Seconds;

                        if(TimeToDegenerate < 0) // Incase Was Happy Very Recently , Don       't want to become negative

                                    TimeToDegenerate = 20+Seconds;

                        } // else

 

                                    TTDDueToUselessActions = random() * 5 + 5 ; // TTD = Time To Degenerate  

                                           debug(AttentionPointsNeeded,TimeToNaughty,TimeToDegenerate,emotion,TTDDueToUselessActions,Seconds,TimeToHungry,TimeToStarve,TimeToToilet,TimeToRelease,TimeToSick);

                        } // if(TimeToNaughty)

 

                        // Computes Current Attention Points

                        CurrAttentionPoints = CurrButtonPushes[play] * Playpoints + CurrButtonPushes[tickle];

 

                        TotalUselessAction = (!Hungry)*CurrButtonPushes[feed] + (!Sick)*CurrButtonPushes[medicine] + (!NeedToPee)*CurrButtonPushes[toilet];

            if(TimeToToilet == Seconds)

            {

                                    CurrButtonPushes[toilet] = 0 ; // Resets Number Of Toilet Pushes

                                    NeedToPee = 1;   

                                    DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);                                    

                        }

                        if(TimeToHungry == Seconds)

                        {

                                    CurrButtonPushes[feed] = 0;

                                    Hungry = 1;    

                                    DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);

                        }

                        // Decreased Happiness Due To Prolonged Sadness

                        if(State_minutes > random())

                        {

                                    if(Happiness>0) Happiness--;

                                    State_minutes=0;

                        }//if(state_minutes)

 

           // Tracks Sickness And Treatment And Length Of Sickness

                        if(Sick == 1)

                        {

                                    if(CurrButtonPushes[medicine] > Medicationlevel)

                                    {

                                    Sick = 0;

                                    UpdateConditionTimers(medicine); 

                                    ClearSick();

                                    {

                                                            HealthUpgradePoints(medicine);      

                                                    r_HealthDowngrade=W_SickNotTreated *random();                                                             

                                    emotion=Angry;

                                                Reinitialize();

                                    }//if(timesicknottreated)

                                    if(!Starve && !Urgent) // Goto Neutral Only if Not Starving And Not Urgent

                                    {

                                                emotion = Neutral;

                                                Reinitialize();

                                    } // if(!starve)

                                    TimeSickNotTreated = 0;

                                    } // if(CurrButtonPushes[medicine]

                                    else                                         

                                    {

                                                counter6++;

                                                if(counter6==1000)      

                                                {                

                                                            TimeSickNotTreated++;

                                                if(TimeSickNotTreated++ > r_HealthDowngrade) // Decrease Health Only if Starving

                                                {    

                                                                        if(Health>0) Health--;

                                                            TimeSickNotTreated=0;

                                                                r_HealthDowngrade=W_SickNotTreated *random();                                                             

                                                }//if(timehungernotfed)

 

                                                            counter6=0;

                                                }//if(counter)                            

    

                        }//else

                        } // if(sick == 1) 

                else

                        if(TimeToSick == Seconds)

                        {

                                    emotion = Angry;

                                    Sick = 1;

                                    CurrButtonPushes[medicine] = 0 ; // Reinitialize Number Of Medicine Given

                                    Reinitialize();  

                                    DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);                                    

                        } // if(TimeToSick)                   

       

                        if(Starve || Hungry)

                        {

                                    if(CurrButtonPushes[feed] > Hungerlevel)

                                    {

                                                if(!Starve)

                                                            HealthUpgradePoints(feed);

                                                if(!Sick && !Urgent)

                                                {

                                                emotion = Neutral;

                                                Reinitialize();

                                                } // if(!sick)

                                                if(TimeHungerNotFed > r_HealthDowngrade && Starve) // Decrease Health Only if Starving

                                                if(Health>0) Health--; ;

                                                Starve = 0;

                                                Hungry = 0;                       

                                                            ClearFood();   

                                                UpdateConditionTimers(feed);

                                                TimeHungerNotFed = 0;

                                    } // if(CurrButtonPushes[feed]

                                    else                                         

                                    {

                                    if(TimeToStarve < Seconds)

                                    {

                                    emotion = Angry;

                                    Starve = 1;

                                                            Reinitialize();

                                                            PrintLCD_emotion(Angry);

                                                            DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);                                    

                                   

                                                } // if(TimeToStarve)

                                                                       

                                                counter1++;

                                                if(counter1==1000)      

                                                {                                                                     

                                                if(TimeHungerNotFed++ > r_HealthDowngrade && Starve) // Decrease Health Only if Starving

                                                {                                                                                                         

                                                                        if(Health>0) Health--;

                                                            TimeHungerNotFed=0;

                                                                r_HealthDowngrade=W_SickNotTreated *random();     

                                                                //output(r_HealthDowngrade);                                                          

                                                }//if(timehungernotfed)

 

                                                            counter7=0;

                                                }//if(counter)                            

    

                        }//else

 

 

                        } // if(starve)

       

                        if(Urgent || NeedToPee)

                        {

                                    if(CurrButtonPushes[toilet] > 0)

                                    {    

                                                UpdateConditionTimers(toilet);

                                                if(!Urgent)

                                                            HealthUpgradePoints(toilet);

                                                if(!Sick && !Starve)

                                                {

                                                emotion = Neutral;

                                                Reinitialize();

                                                } // if(!sick)

                                                if(TimeMessNotClean > random() * W_MessNotClean && Urgent) // Decrease Discipline Only if Urgent

                                                            if(Discipline>0) Discipline--; ;

                                                Urgent = 0;

                                                NeedToPee = 0;

                                                TimeMessNotClean = 0;  

                                                ClearToilet();

                                               

                                    } // if(CurrButtonPushes[feed]

                                    else

                                    TimeMessNotClean++ ;

                        } // if(urgent)

        else

 

            if(CurrButtonPushes[spank] > 2)

            {

                        emotion = Angry;

                        Reinitialize();

                        Disciplined = 1;

            } // if(CurrButtonPushes)

 

                        else

                        if(CurrAttentionPoints > AttentionPointsNeeded)

                        {

                                    // ////PORTB = 0x00;

                        emotion = ImprovedState;

                                    Reinitialize();

                        } // if(CurrAttentionPoints)

                        else

                        if(TimeToRelease == Seconds)

                        {

            emotion = Angry;

            Urgent = 1;

                        UpdateConditionTimers(toilet);

                                    Reinitialize(); 

                                    DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);                                    

                        }

                        else

                        if(TimeToNaughty < Seconds && !TimeToNaughty)

                        {                     

                                    output(TimeToNaughty);

                                    if(random() < 0.4) // Go To Naughty randomly

                                    {                   

                                                output(emotion);

                                    emotion = Naughty;

                                    Reinitialize();

                                    } // if(random)

                        else

                        TimeToNaughty = randomize_naughty(Sad);

                        } // if(TimeToNaughty)

                        else

            if(TimeToDegenerate < Seconds || TotalUselessAction > 3 * TTDDueToUselessActions)

                        {

                        emotion = Angry;

                                    Reinitialize();

                        } // if(Timetobored)

 

         break;

 // - - - - - - - - - - - - - - - - - - - - - -ANGRY - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 case Angry:          

            PrintLCD_emotion(emotion);

            TrackTimeInState() ; // Increment Time Spent In Current State

             //                     UDR=TimeToNaughty;

                        if(TimeToNaughty == 0) // Don't repeat unnecessary calculations

                        {   

                        output(emotion);

                                    if(Starve==1)

                                                WriteHungry();

                                   

                                    if(Urgent==1)

                                                WriteLate();

                                               

                                    TimeToNaughty = randomize_naughty(emotion);

                                    AddHistory(emotion);

                        if(random() > 0.5) // Determine Whether To Go Happy Or Excited

                                    ImprovedState = Neutral ; // When Given Enough Attention Points

                        else ImprovedState = Bored;

               

                        Tempfloat = random() * 20;

                        AttentionPointsNeeded = Tempfloat + 5 ; // Determines Number Of Presses Of Tickling

               // And Playing To Improve State

                                    if(random() > 0.7) // Determine Whether Eva Prefers To Play Or Be

                                    Playpoints = 1 ; // Tickled

                                    else Playpoints = 2;

 

                                    j = StateTransitions;

                                    TimeSinceLastHappy = 0;

                        for(j = StateTransitions ; j > 0 ; j--)

              // Check When Eva Was Last Happy Or Excited

                                    {

                                    TimeSinceLastHappy++ ;

                                    if(History[j]!= Happy || History[j]!= Excited)

                      break;

                                    } // while(history[j])

 

                                    if(TimeSinceLastHappy == StateTransitions) // Was Never Happy B4

                                    {

                                    TimeSinceLastHappy = 0;

                                    } // if(TimeSinceLastHappy)

       debug(AttentionPointsNeeded,TimeToNaughty,TimeToDegenerate,emotion,TTDDueToUselessActions,Seconds,TimeToHungry,TimeToStarve,TimeToToilet,TimeToRelease,TimeToSick);

                        } // if(TimeToNaughty)

                                    // Computes Current Attention Points

                        CurrAttentionPoints = CurrButtonPushes[play] * Playpoints + CurrButtonPushes[tickle];

            if(TimeToToilet == Seconds)

            {

                                    CurrButtonPushes[toilet] = 0 ; // Resets Number Of Toilet Pushes

                                    NeedToPee = 1;      

                                    DrawToilet();

                                   

                        }

                        if(TimeToHungry == Seconds)

                        {

                                    CurrButtonPushes[feed] = 0;

                                    Hungry = 1;     

                                    DrawFood();

                        }

                        // Decreased Happiness Or Discipline Due To Prolonged Anger

                        if(State_seconds > random() * AngryEffects)

                        {

                                    State_seconds=0;

                                    if(random() > 0.4)                                                                                 

                                    if(Happiness>0) Happiness--;

                                    else

                                    if(Discipline>0) Discipline--;

                        } // if(State_minutes)                          

                       

       // print(Happiness);

           // Tracks Sickness And Treatment And Length Of Sickness

                        if(Sick == 1)

                        {

                                    if(CurrButtonPushes[medicine] > Medicationlevel)

                                    {

                                                Sick = 0;     

                                                ClearSick();

                                                if(Starve==1)

                                                            WriteHungry();

                                                else

                                                if(Urgent==1)

                                                            WriteLate();

                                                           

                                                UpdateConditionTimers(medicine);

                                                if(TimeSickNotTreated > r_HealthDowngrade)

                                                {

                                                            HealthUpgradePoints(medicine);   

                                                                r_HealthDowngrade=W_SickNotTreated *random();                                                             

                                                            TimeSickNotTreated=0;

                                                 }        

                                                if(!Starve && !Urgent && !Disciplined) // Goto Neutral Only if Not Starving And Not Urgent

                                                {

                                                emotion = Neutral;

                                                Reinitialize();

                                                } // if(!starve)

                                                TimeSickNotTreated = 0;

                                    } // if(CurrButtonPushes[medicine]

                                    else

                                    {

                                                counter3++;

                                                if(counter3==1000)

                                                {

                                                TimeSickNotTreated++ ;

                                                counter3=0;           

                                                if(TimeSickNotTreated>r_HealthDowngrade)

                                                {                                  

//                                                          output(r_HealthDowngrade);

                                                                        if(Health>0) Health--;

                                                                r_HealthDowngrade=W_SickNotTreated *random();                                                                                                                                     

                                                            TimeSickNotTreated=0;       

                                                }//if(timesicknottreated)

                                    }//if(counter3)

                        }//else

                        } // if(sick == 1)

                        else

                        if(TimeToSick == Seconds)

                        {

                                    Sick = 1;

                                    CurrButtonPushes[medicine] = 0 ; // Reinitialize Number Of Medicine Given

                                                DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);

                        } // if(TimeToSick)

 

                        if(Starve || Hungry)

                        {         

                                               

                                    if(CurrButtonPushes[feed] > Hungerlevel)

                                    {  

                                                if(!Starve)  HealthUpgradePoints(feed);                                    

                                                if(!Sick && !Urgent && !Disciplined)

                                                {

                                                emotion = Neutral;

                                                Reinitialize();  

                                    } // if(!sick)

                                    else

                                                if(Urgent==1)                                                          

                                                            WriteLate();

                                                else

                                                            WriteAngry();

                                               

                                                Starve = 0;

                                                Hungry = 0;    

                                                ClearFood();

                                                UpdateConditionTimers(feed);

                                    TimeHungerNotFed = 0;

                                    } // if(CurrButtonPushes[feed]

                                    else                        

                                    {

                                                counter1++;

                                                if(counter1==1000)      

                                                {                                                                     

                                                if(TimeHungerNotFed++ > r_HealthDowngrade && Starve) // Decrease Health Only if Starving

                                                {

                                                                        if(Health>0) Health--;

                                                            TimeHungerNotFed=0;

                                                                r_HealthDowngrade=W_SickNotTreated *random();     

                                                                //output(r_HealthDowngrade);                                                          

                                                }//if(timehungernotfed)

 

                                                            counter1=0;

                                                }//if(counter)                            

    

                        }//else

 

                        } // if(starve)             

 

                        if(Urgent)

                        {

                                    if(CurrButtonPushes[toilet] > 0)

                                    {

                                    if(!Sick && !Starve)

                                    {

                                                emotion = Neutral;

                                                Reinitialize();

                                    } // if(!sick)

                                    Urgent = 0;

                                    NeedToPee = 0;      

                                    ClearToilet();       

                                    if(Starve==1)

                                                WriteHungry();

                                    else

                                    if(Sick==1)

                                                WriteAngry();

                                               

                                    UpdateConditionTimers(toilet);

                                    TimeMessNotClean = 0;

                                    } // if(CurrButtonPushes[feed]

                                    else     

                                    {

                                    counter2++;

                                                if(counter2==1000)

                                                {

                                                            TimeMessNotClean++ ;

                                                            if(TimeMessNotClean > random() * W_MessNotClean && Urgent) // Decrease Discipline Only if Urgent

                                                            {

                                                            if(Discipline>0) Discipline--; ;

                                                            TimeMessNotClean=0;

                                                            }//if(timemessnotclean)  

                                                            counter2=0;

                                                }//if(counter) 

                                    }//else

                        } // if(urgent)

                        else

                        if(NeedToPee)

                        {

                                    if(CurrButtonPushes[toilet] > 0)

                                    {

                                                if(!Sick && !Starve)

                                                {

                                                emotion = Neutral;

                                                Reinitialize();

                                                } // if(!sick)

                                                Urgent = 0;

                                                NeedToPee = 0;     

                                                ClearToilet();

                                                UpdateConditionTimers(toilet);

                                                HealthUpgradePoints(toilet);

                                                TimeMessNotClean = 0;

                                    } // if(CurrButtonPushes[feed]              

                        }//if(needtopee)                                                                        

 

                        if(CurrButtonPushes[spank] > 0 )

                        {

                                    AttentionPointsNeeded = AttentionPointsNeeded + random() * 30;

                                    CurrButtonPushes[spank]=0;

            }

                        else

                        if(CurrAttentionPoints > AttentionPointsNeeded && !Sick && !Urgent && !Starve)

                        {  

            emotion = ImprovedState;

                                    Reinitialize();

                                    Disciplined = 0;

                        } // if(CurrAttentionPoints)

                        else

                        if(TimeToRelease == Seconds)

                        {

            Urgent = 1;

            UpdateConditionTimers(toilet);

                        WriteLate();

           

                        }

            else

            if(TimeToStarve < Seconds && !Starve && Hungry)

            {                 

                        Starve = 1; 

                                    WriteHungry();

 

                        } // if(TimeToStarve)

                        else

                        if(TimeToNaughty < Seconds && !Sick && !Urgent && !Starve)

                        {

                                    if(random() < 0.15) // Go To Naughty randomly

                                    {

                                    emotion = Naughty;

                                    Reinitialize();

                                    } // if(random)

                                    else

                                    TimeToNaughty = randomize_naughty(Angry)-20;

                        } // if(TimeToNaughty)

 

         break;

// - - - - - - - - - - - - - - - - - - - - - -NAUGHTY - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 case Naughty:        

                        PrintLCD_emotion(emotion);      

 

            TrackTimeInState() ; // Increment Time Spent In Current State

                        if(TTDDueToUselessActions == 0) // Don't repeat unnecessary calculations

                        {                 

                                    IsNaughty=1;

                            AddHistory(emotion);

                            if(random() > 0.2) // Determine Whether To Go Happy Or Excited

                               ImprovedState = Happy ; // When Given Enough Attention Points

                            else ImprovedState = Neutral;

                                                             

                                    Tempfloat = random() * 10;

 

                        AttentionPointsNeeded = Tempfloat + 5 ; // Determines Number Of Presses Of Tickling and Playing To Improve State

                                    if(random() > 0.7) // Determine Whether Eva Prefers To Play Or Be

                                    Playpoints = 1 ; // Tickled

                                    else Playpoints = 2;

                                       

                                    j = StateTransitions;

                            TimeSinceLastHappy = 0;

                            for(j = StateTransitions ; j > 0 ; j--)

                                    {                                 

                                    TimeSinceLastHappy++ ;                                              // Check When Eva Was Last Happy Or Excited  

                                    if(History[j]!= Happy || History[j]!= Excited)

                      break;

                                    } // for(history[j])

   

                                    if(TimeSinceLastHappy == StateTransitions) // Was Never Happy B4

                                    {

                                    TimeSinceLastHappy = 0;

                                    TimeToDegenerate = 20+Seconds;

                                    } // if(TimeSinceLastHappy)

                                    else

                                    {

                        TimeToDegenerate = 20 -TimeSinceLastHappy * 30 * 10+Seconds;

                        if(TimeToDegenerate < 0) // Incase Was Happy Very Recently , Don't want to become negative

                                    TimeToDegenerate = 20+Seconds;

                        } // else

 

                                    TTDDueToUselessActions = 10;//random() * 5 + 5 ; // Ttd = Time To Degenerate

                                    DisciplineNeeded = 1; //(TimeSinceLastHappy+1)*W_DisciplineNeeded;                       

            debug(AttentionPointsNeeded,TimeToNaughty,TimeToDegenerate,emotion,TTDDueToUselessActions,Seconds,TimeToHungry,TimeToStarve,TimeToToilet,TimeToRelease,TimeToSick);

                        } // if(TimeToNaughty)          

 

                        // Computes Current Attention Points

                        CurrAttentionPoints = CurrButtonPushes[play] * Playpoints + CurrButtonPushes[tickle];

 

                        TotalUselessAction = (!Hungry)*CurrButtonPushes[feed] + (!Sick)*CurrButtonPushes[medicine] + (!NeedToPee)*CurrButtonPushes[toilet];

 

                       

                        if(CurrButtonPushes[spank] >= DisciplineNeeded)

            {

                                    IsNaughty=0;   

                                    TimeNotDiscipline=0;

                        if(random()<0.5)                                   

                                                emotion = Neutral;

                                    else emotion = Bored; 

                                    DisciplineCount++;                    //counter for number of valid disciplines

                                    if(DisciplineCount>DisciplineUpgrade)    //check if discipline of EVA needs to decrease

                                    {

                                                if(Discipline<120) Discipline++;

                                                DisciplineUpgrade=random()*W_DisciplineUpgrade;

                                                DisciplineCount=0;                                           

                                    }

                        Reinitialize();                            

            }//if(currbuttonpushes[discipline]

                        else

                        if(State_minutes>random()*W_TimeNotDiscipline)         //check if discipline of EVA needs to decrease

                        {                                             

                                                State_minutes=0;

                                                if(Discipline>0) Discipline--;

                        }//if(timenotdiscipline)

                                                                       

                                                                       

            if(TimeToToilet == Seconds)

            {

                                    CurrButtonPushes[toilet] = 0 ; // Resets Number Of Toilet Pushes

                                    NeedToPee = 1; 

                                    DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);                                    

                        }

                        if(TimeToHungry == Seconds)

                        {

                                    CurrButtonPushes[feed] = 0;

                                    Hungry = 1;  

                                    DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);

                        // ////PORTB = 0x00;

                        }

            else

                        if(TimeToSick < Seconds)

                        {

                                    Sick = 1;

                                    emotion = Sad;

                                    DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);                        

                        } // if(TimeToSick)

                        else

                        if(NeedToPee == 1 && CurrButtonPushes[toilet] > 0)

                        {

                                    if(random() < 0.6)

                                    emotion = Neutral;

                                    else

                                    emotion = Happy;  

                                    HealthUpgradePoints(toilet);

                                    NeedToPee = 0;

                                    ClearToilet();

                                    UpdateConditionTimers(toilet);

                                    Reinitialize();

                        } // if(NeedToPee)

                        else

                        if(Hungry == 1 && CurrButtonPushes[feed] > Hungerlevel)

                        {

                                    Hungry = 0;  

                                    ClearFood();

                                    CurrButtonPushes[Hungry] = 0;

                                    if(random() < 0.6)

                                    emotion = Happy;

                                    else

                                    emotion = Neutral;

                                    HealthUpgradePoints(toilet);

                                    UpdateConditionTimers(feed);  

                                    DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);

                                    Reinitialize();                                                    

                        } // if(Hungry)

                        else

                        if(CurrAttentionPoints > AttentionPointsNeeded)

                        {       

            emotion = ImprovedState;

                                    Reinitialize();

                        } // if(CurrAttentionPoints)

                        else

                        if(TimeToRelease < Seconds)

                        {

            emotion = Angry;

            Urgent = 1;       

                                    Reinitialize();

                                    UpdateConditionTimers(toilet);

                                    DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);                                    

                        }//if(timetorelease)

        else

        if(TimeToStarve < Seconds && Hungry)

        {

            emotion = Sad;

 

            Starve = 1;       

                                    DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);                                    

           

                                    Reinitialize();   

                        }

                        else

        if(TimeToDegenerate < Seconds|| TotalUselessAction > 3 * TTDDueToUselessActions)

                        {                        

                                    emotion = Bored;

                                    Reinitialize();        

                        } // if(Timetobored)          

 

         break;          

//--------------------------------------------HAPPY------------------------------

            case Happy:

                        PrintLCD_emotion(emotion);

            TrackTimeInState() ; // Increment Time Spent In Current State 

 

                        if(FirstTime == 1) // Don                            't repeat unnecessary calculations

                        {               

                                    FirstTime=0;

                                    TimeToNaughty = randomize_naughty(Happy);  

                            AddHistory(Happy);          

                           

                            if(random() < 0.3) // Determine Whether To Go Happy Or Excited or Neutral

                               ImprovedState = Excited ; // When Given Enough Attention Points

                            else

                            if(random()<0.6)

                                    ImprovedState = Neutral;

                            else

                                    ImprovedState = Excited;

                           

                           

                                    Tempfloat = random() * 10;

                        AttentionPointsNeeded = Tempfloat + 5 ; // Determines Number Of Presses Of Tickling

                                                                                                                                    // And Playing To Improve State

                                    if(random() > 0.7) // Determine Whether Eva Prefers To Play Or Be

                                    Playpoints = 1 ; // Tickled

                                    else Playpoints = 2;

 

                                    j = StateTransitions;

                            TimeSinceLastHappy = 0;

                            for(j = StateTransitions ; j > 0 ; j--)

                                    {                                                                      // Check When Eva Was Last Happy Or Excited                                      

                                    TimeSinceLastHappy++ ;

                                    if(History[j]!= Happy || History[j]!= Excited)

                      break;

                                    } // while(history[j])

 

                                    if(TimeSinceLastHappy == StateTransitions) // Was Never Happy B4

                                    {

                                    TimeSinceLastHappy = 0;

                                    TimeToDegenerate = 20+Seconds;

                                    } // if(TimeSinceLastHappy)

                                    else

                                    {

                        TimeToDegenerate = 20 -TimeSinceLastHappy * 30 * 10+Seconds;

                        if(TimeToDegenerate < 0) // Incase Was Happy Very Recently , Don't want to become negative

                                    TimeToDegenerate = 20+Seconds;

                        } // else

                                   

                                    TTDDueToUselessActions = random() * 5 + 5 ; // Ttd = Time To Degenerate

                                   debug(AttentionPointsNeeded,TimeToNaughty,TimeToDegenerate,emotion,TTDDueToUselessActions,Seconds,TimeToHungry,TimeToStarve,TimeToToilet,TimeToRelease,TimeToSick);

                        } // if(TimeToNaughty)

 

                        // Computes Current Attention Points

                        CurrAttentionPoints = CurrButtonPushes[play] * Playpoints + CurrButtonPushes[tickle];

 

                        TotalUselessAction = (!Hungry)*CurrButtonPushes[feed] + (!Sick)*CurrButtonPushes[medicine] + (!NeedToPee)*CurrButtonPushes[toilet];

 

                        temp=rand();

                        if(State_seconds > 1)//temp/2)

                        {

                                    State_seconds=0;

                                    if(Happiness<120) Happiness++;

                        } // if(State_minutes)

 

            if(TimeToToilet == Seconds)

            {

                                    CurrButtonPushes[toilet] = 0 ; // Resets Number Of Toilet Pushes

                                    NeedToPee = 1;     

                                    DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);                                    

 

                        }

                        if(TimeToHungry == Seconds)

                        {

                                    CurrButtonPushes[feed] = 0;

                                    Hungry = 1;  

                                    DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);

                        }

 

                        if(CurrButtonPushes[spank] > 2)

            {

                        emotion = Angry;

                        Reinitialize();

                        Disciplined = 1;

            }

            else

                        if(TimeToSick < Seconds)

                        {

                                    Sick = 1;

                                    emotion = Sad;

                                    Reinitialize();

                                    DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);                                    

 

                        } // if(TimeToSick)

                        else

                        if(NeedToPee == 1 && CurrButtonPushes[toilet] > 0)

                        {

                                    if(random() < 0.4)

                                    emotion = Neutral;

                                    else

                                    emotion = Happy;

                                    NeedToPee = 0;      

                                    ClearToilet();

                                    HealthUpgradePoints(toilet);

                                    UpdateConditionTimers(toilet);

                        } // if(NeedToPee)

                        else

                        if(Hungry == 1 && CurrButtonPushes[feed] > Hungerlevel)

                        {

                                    Hungry = 0;  

                                    ClearFood();                     

                                    DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);

                                    CurrButtonPushes[Hungry] = 0;

                                    if(random() < 0.4)

                                    emotion = Happy;

                                    else

                                    emotion = Neutral;

                                    HealthUpgradePoints(feed);

                                    UpdateConditionTimers(feed);

                        } // if(Hungry)

                        else

                        if(CurrAttentionPoints > AttentionPointsNeeded)

                        {

            emotion = ImprovedState;

                                    if(emotion!=Happy)       Reinitialize();

                        } // if(CurrAttentionPoints)

                        else

                        if(TimeToRelease < Seconds)

                        {

            emotion = Angry;

            Urgent = 1;       

                                    Reinitialize();

                                    UpdateConditionTimers(toilet);    

                                    DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);                        

                        }//if(timetorelease)

        else

        if(TimeToStarve < Seconds && Hungry)

        {        

            emotion = Angry;

            Starve = 1;       

                        DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);                                    

 

                                    Reinitialize();   

                        }

                        else

                        if(TimeToNaughty < Seconds)

                        {

                                    emotion = Naughty;

                                    Reinitialize();

                        }

                        else

        if(TimeToDegenerate < Seconds || TotalUselessAction > 3 * TTDDueToUselessActions)

                        {

                                    emotion = Bored;

                                    Reinitialize();

                        } // if(Timetobored)

 

         break;

//----------------------------------EXCITED----------------------------------

            case Excited:

                        PrintLCD_emotion(emotion);

            TrackTimeInState() ; // Increment Time Spent In Current State

                        if(FirstTime == 1) // Don                            't repeat unnecessary calculations

                        {               

                                    FirstTime=0;

                                    TimeToNaughty = randomize_naughty(Excited);  

                            AddHistory(Excited);          

                           

                            if(random() < 0.3) // Determine Whether To Go Happy Or Excited or Neutral

                               ImprovedState = Happy ; // When Given Enough Attention Points

                            else

                            if(random()<0.7)

                                    ImprovedState = Happy;

                            else

                                    ImprovedState = Neutral;

                           

                           

                                    Tempfloat = random() * 10;

                        AttentionPointsNeeded = Tempfloat + 5 ; // Determines Number Of Presses Of Tickling and Playing To Improve State

                                    if(random() > 0.7) // Determine Whether Eva Prefers To Play Or Be

                                    Playpoints = 1 ; // Tickled

                                    else Playpoints = 2;

 

                                    j = StateTransitions;

                            TimeSinceLastHappy = 0;

                            for(j = StateTransitions ; j > 0 ; j--)

                                    {                                                                      // Check When Eva Was Last Happy Or Excited                                      

                                    TimeSinceLastHappy++ ;

                                    if(History[j]!= Excited)

                      break;

                                    } // while(history[j])

 

                                    if(TimeSinceLastHappy == StateTransitions) // Was Never Happy B4

                                    {

                                    TimeSinceLastHappy = 0;

                                    TimeToDegenerate = 10+Seconds;

                                    } // if(TimeSinceLastHappy)

                                    else

                                    {

                        TimeToDegenerate = 20 -TimeSinceLastHappy * 30+Seconds;

                        if(TimeToDegenerate < 0) // Incase Was Happy Very Recently , Don't want to become negative

                                    TimeToDegenerate = 20+Seconds;

                        } // else

                                   

                                    TTDDueToUselessActions = random() * 5 + 5 ; // Ttd = Time To Degenerate

                    debug(AttentionPointsNeeded,TimeToNaughty,TimeToDegenerate,emotion,TTDDueToUselessActions,Seconds,TimeToHungry,TimeToStarve,TimeToToilet,TimeToRelease,TimeToSick);

                        } // if(TimeToNaughty)

                        // Computes Current Attention Points

                        CurrAttentionPoints = CurrButtonPushes[play] * Playpoints + CurrButtonPushes[tickle];

 

                        TotalUselessAction = (!Hungry)*CurrButtonPushes[feed] + (!Sick)*CurrButtonPushes[medicine] + (!NeedToPee)*CurrButtonPushes[toilet]; 

                        temp=rand();

                        if(State_seconds > 0) //temp/2)

                        {       

                                    output(State_seconds);

                                    State_seconds=0;

                                    if(random()>0.6)                       

                                    {

                                                if(Happiness<120) Happiness++;

                                    }

                                    else               

                                    {

                                                if(Health<120) Health++;

                                    }

                        } // if(State_minutes)

 

            if(TimeToToilet == Seconds)

            {

                                    CurrButtonPushes[toilet] = 0 ; // Resets Number Of Toilet Pushes

                                    NeedToPee = 1;

                                    DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);                                    

                        }

                        if(TimeToHungry == Seconds)

                        {

                                    CurrButtonPushes[feed] = 0;

                                    Hungry = 1;  

                                    DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);

                        }

 

                        if(CurrButtonPushes[spank] > 2)

            {

                        emotion = Angry;

                        Reinitialize();

                        Disciplined = 1;

            }

            else

                        if(TimeToSick < Seconds)

                        {

                                    Sick = 1;                                          

                                    DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);                                                                        

                                    emotion = Sad;

                                    Reinitialize();

                        } // if(TimeToSick)

                        else

                        if(NeedToPee == 1 && CurrButtonPushes[toilet] > 0)

                        {

                                    if(random() < 0.4)

                                    emotion = Neutral;

                                    else

                                    emotion = Happy;

                                    NeedToPee = 0;

                                    ClearToilet();

                                    HealthUpgradePoints(toilet);

                                    UpdateConditionTimers(toilet);

                        } // if(NeedToPee)

                        else

                        if(Hungry == 1 && CurrButtonPushes[feed] > Hungerlevel)

                        {

                                    Hungry = 0;                            

                                    DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);

                                    CurrButtonPushes[Hungry] = 0;

                                    if(random() < 0.4)

                                    emotion = Happy;

                                    else

                                    emotion = Neutral;

                                    HealthUpgradePoints(feed); 

                                    ClearFood();

                                    UpdateConditionTimers(feed);

                        } // if(Hungry)

                        else

                        if(CurrAttentionPoints > AttentionPointsNeeded)

                        {

            emotion = ImprovedState;

                                    Reinitialize();

                        } // if(CurrAttentionPoints)

                        else

                        if(TimeToRelease < Seconds)

                        {

            emotion = Angry;

            Urgent = 1;       

                                    Reinitialize();

                                    UpdateConditionTimers(toilet);

                                    DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);                                    

                                   

                        }//if(timetorelease)

        else

        if(TimeToStarve < Seconds && Hungry)

        {        

            emotion = Angry;

            Starve = 1;       

                                    Reinitialize();   

                                    DrawCondition(Hungry,Starve,NeedToPee,Urgent,Sick);                        

                        }

                        else

                        if(TimeToNaughty < Seconds)

                        {

                                    emotion = Naughty;

                                    Reinitialize();

                                    //////PORTB=~0x2f;

                        }

                        else

        if(TimeToDegenerate < Seconds || TotalUselessAction > 3 * TTDDueToUselessActions)

                        {

                                    emotion = Bored;

                                    Reinitialize();

                        } // if(Timetobored)

 

         break;

//***************************************DEATH*****************************

            case Death:

                        DisplayEmotion(emotion);

                        debug(AttentionPointsNeeded,TimeToNaughty,TimeToDegenerate,emotion,TTDDueToUselessActions,Seconds,TimeToHungry,TimeToStarve,TimeToToilet,TimeToRelease,TimeToSick);                   

                        break;

           

} // Switch(emotion)

 

 

} // Statemachine

 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

 

void UpdateConditionTimers(unsigned char action)

{

            switch(action)

            {

            case feed:

            TimeToHungry =(int)(random() * W_TimeToHungry + Seconds);

                        TimeToStarve =(int)(random() * W_TimeToStarve + TimeToHungry);

                        Hungerlevel =(int)(random() * W_HungerLevel);

                        break;

            case toilet:                    

                        TimeToToilet =(int)(random() * W_TimeToToilet + Seconds);

                        TimeToRelease =(int)(random() * W_TimeToRelease + TimeToToilet);

                                    output(TimeToToilet);

            break;

            case medicine:

                        TimeToSick =(int)(random() * W_TimeToSick + Seconds);

                        Medicationlevel =(int)(random() * W_MedicationLevel);

                        output(TimeToSick);

                         break;

            } // Switch Action

}

 

 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

 

 

void main(void)

{

 

   Initialize();

 

  while(1)

  {

     if(Time2 == 0) TrackTotalTime() ; // Increment Total Game Time

            if(Time1 == 0)

            {

                        StateMachine();

                        if(Health==0)

                        {

                                    emotion=Death;          

                                    playsong(emotion);

                                    Health=1;

                         }

            }

            if(Time3 == 0) UpdateButtonPushes();

     if(Time4 == 0) UpdateLCD();              

     if(playsongtime==0) playsongfunction();

     if(servotimer==0) servofunction(emotion);

  }

 

} // Main