/***************************************************************************************************************************/
/*       C-Code der   Basiskomponente   "Maintenance                     " , Version "Version0                        "    */
/***************************************************************************************************************************/

#   include  "c_macros.h"
#   include  "sys_struct.h"
#   include  "sys_extern.h"
#   include  "rts_func.h"
#   include  "mod_struct.h"


    int mAINTENANCE ( register struct mAINTENANCE * pData )
    {
    int  status = 0;


/*  Strukturierung und Urinitialisierung  */
/*  ====================================  */
    if (Mode == Activ  &&  ActPhase == 1)
    {
       static    logic   Call1 = Yes;
       register  long    i, j;


/*     Uebergabe der Adressen  */
/*     ----------------------  */
       if ( !ReStart )
       {
#         include "offset.incl" 
       }

       SaveCompName ("Maintenance");
       LinkSubComp ( &(pData->NameElem) );



       BCount ++;   FBComp [BCount] = (FPtr) mAINTENANCE;
                    DBComp [BCount] = pData;

       Con(CPH) =  0.2E4;

          StNxt(STATUS) = 0;   Sta(STATUS) = 0;
          StNxt(TMAINT) = 0;   Sta(TMAINT) =  0.E0;
          StNxt(TFAIL) = 0;   Sta(TFAIL) =  0.E0;
          StNxt(TRESTART) = 0;   Sta(TRESTART) =  0.E0;
          StNxt(COST) = 0;   Sta(COST) =  0.E0;

       Random(IFAIL).NxtRan =  HeadRan;     HeadRan = Adr(IFAIL); 
       Random(IFAIL).GenNo  =  GenCount;    GenCount ++; 
        Random(IFAIL).Dist   =  3; 
        Random(IFAIL).P[0]   =  0.2E3;
        Random(IFAIL).P[1]   =  0.2E2;
        Random(IFAIL).P[2]   =  - 1.E+35;
        Random(IFAIL).P[3]   =    1.E+35;
        Random(IFAIL).P[4]   =  0.0;
       Random(IMAINT).NxtRan =  HeadRan;     HeadRan = Adr(IMAINT); 
       Random(IMAINT).GenNo  =  GenCount;    GenCount ++; 
        Random(IMAINT).Dist   =  3; 
        Random(IMAINT).P[0]   =  0.145E3;
        Random(IMAINT).P[1]   =  0.1E1;
        Random(IMAINT).P[2]   =  - 1.E+35;
        Random(IMAINT).P[3]   =    1.E+35;
        Random(IMAINT).P[4]   =  0.0;
       Random(IREPAIR).NxtRan =  HeadRan;     HeadRan = Adr(IREPAIR); 
       Random(IREPAIR).GenNo  =  GenCount;    GenCount ++; 
        Random(IREPAIR).Dist   =  3; 
        Random(IREPAIR).P[0]   =  0.4E1;
        Random(IREPAIR).P[1]   =  0.5E0;
        Random(IREPAIR).P[2]   =  - 1.E+35;
        Random(IREPAIR).P[3]   =    1.E+35;
        Random(IREPAIR).P[4]   =  0.0;
       Random(ICHECK).NxtRan =  HeadRan;     HeadRan = Adr(ICHECK); 
       Random(ICHECK).GenNo  =  GenCount;    GenCount ++; 
        Random(ICHECK).Dist   =  3; 
        Random(ICHECK).P[0]   =  0.1E1;
        Random(ICHECK).P[1]   =  0.1E0;
        Random(ICHECK).P[2]   =  - 1.E+35;
        Random(ICHECK).P[3]   =    1.E+35;
        Random(ICHECK).P[4]   =  0.0;

       VarEff (STATUS,CompNo)   = BCount;   VarEff (STATUS,EffFlag) = 2;
       VarEff (STATUS,eff_next) = NULL;
       VarEff (TMAINT,CompNo)   = BCount;   VarEff (TMAINT,EffFlag) = 2;
       VarEff (TMAINT,eff_next) = NULL;
       VarEff (TFAIL,CompNo)   = BCount;   VarEff (TFAIL,EffFlag) = 2;
       VarEff (TFAIL,eff_next) = NULL;
       VarEff (TRESTART,CompNo)   = BCount;   VarEff (TRESTART,EffFlag) = 2;
       VarEff (TRESTART,eff_next) = NULL;
       VarEff (COST,CompNo)   = BCount;   VarEff (COST,EffFlag) = 2;
       VarEff (COST,eff_next) = NULL;

       for (i = TCmpCount + 1L;  i <= TCmpCount + 4L;  i ++)
       {
          TimeCmp [i]. Effect. CompNo   =  BCount;
          TimeCmp [i]. Effect. EffFlag  =  2;
          TimeCmp [i]. Effect. eff_next =  NULL;
       }

       TimeEff [BCount]. CompNo   =  BCount;
       TimeEff [BCount]. EffFlag  =  0;
       TimeEff [BCount]. eff_next =  NULL;

       ExAlg [BCount] =  0;
       ExDtl [BCount] =  0;
       ExEvt [BCount] =  1;

       pData -> CroBase  = CroCount;   CroCount  += 0;
       pData -> IndBase  = IndCount;   IndCount  += 0;
       pData -> RegBase  = RegCount;   RegCount  += 0;
       pData -> TCmpBase = TCmpCount;  TCmpCount += 4;

       return status;
    }

/*  Belegungen der Arrayinfo-Strukturen  */
/*  ===================================  */
    if (Mode == Activ  &&  ActPhase == 2)
    {
      long i;


      return status;
    }


/*  Aktuellen Komponentennamen merken  */
/*  ---------------------------------  */
    ActComp =  & (pData->NameElem);



/*  Beschreibung der Modelldynamik  */
/*  ==============================  */


/*  Ereignis (ON) */
/*  ============= */
    if (Mode == Events)
    {
      ActLine = 28;
      if ( Start )
      {
        if (CroDetect)  ExpectEvt = Yes;
        else
        {
          ActLine = 30;
          StaAsgnE (ASta(STATUS), EVal(1), NULL);
          ActLine = 31;
          StaAsgnD (ASta(TFAIL), Ran(IFAIL), NULL);
          ActLine = 32;
          StaAsgnD (ASta(TMAINT), Ran(IMAINT), NULL);

          EvtCond = Yes;
        }
      }
    }

/*  Ereignis (WHENEVER) */
/*  =================== */
    if (Mode == Events)
    {
      ActLine = 35;
      if ((TCompare(ge,Sta(TFAIL),TCmpNo(1L),0L))&&((Sta(STATUS)==EVal(1))) )
      {
        if (CroDetect)  ExpectEvt = Yes;
        else
        {
          ActLine = 37;
          StaAsgnE (ASta(STATUS), EVal(3), NULL);
          ActLine = 38;
          StaAsgnD (ASta(COST), Sta(COST) +Con(CPH)*Ran(IREPAIR), NULL);
          ActLine = 39;
          StaAsgnD (ASta(TRESTART), Tc +Ran(IREPAIR), NULL);
          ActLine = 40;
          sprintf (Message, "T= %f Machine breaks down\n",Tc);
          Display (Message);

          EvtCond = Yes;
        }
      }
    }

/*  Ereignis (WHENEVER) */
/*  =================== */
    if (Mode == Events)
    {
      ActLine = 43;
      if ((TCompare(ge,Sta(TRESTART),TCmpNo(2L),0L))&&((Sta(STATUS)==EVal(3))) )
      {
        if (CroDetect)  ExpectEvt = Yes;
        else
        {
          ActLine = 45;
          StaAsgnE (ASta(STATUS), EVal(1), NULL);
          ActLine = 46;
          StaAsgnD (ASta(TFAIL), Tc +Ran(IFAIL), NULL);
          ActLine = 47;
          sprintf (Message, "T= %f Repair complete \n",Tc);
          Display (Message);

          EvtCond = Yes;
        }
      }
    }

/*  Ereignis (WHENEVER) */
/*  =================== */
    if (Mode == Events)
    {
      ActLine = 50;
      if ((TCompare(ge,Sta(TMAINT),TCmpNo(3L),0L))&&((Sta(STATUS)==EVal(1))) )
      {
        if (CroDetect)  ExpectEvt = Yes;
        else
        {
          ActLine = 52;
          StaAsgnE (ASta(STATUS), EVal(2), NULL);
          ActLine = 53;
          StaAsgnD (ASta(COST), Sta(COST) +Con(CPH)*Ran(ICHECK), NULL);
          ActLine = 54;
          StaAsgnD (ASta(TRESTART), Tc +Ran(ICHECK), NULL);
          ActLine = 55;
          sprintf (Message, "T= %f Begin maintenance \n",Tc);
          Display (Message);

          EvtCond = Yes;
        }
      }
    }

/*  Ereignis (WHENEVER) */
/*  =================== */
    if (Mode == Events)
    {
      ActLine = 58;
      if ((TCompare(ge,Sta(TRESTART),TCmpNo(4L),0L))&&((Sta(STATUS)==EVal(2))) )
      {
        if (CroDetect)  ExpectEvt = Yes;
        else
        {
          ActLine = 60;
          StaAsgnE (ASta(STATUS), EVal(1), NULL);
          ActLine = 61;
          StaAsgnD (ASta(TFAIL), Tc +Ran(IFAIL), NULL);
          ActLine = 62;
          StaAsgnD (ASta(TMAINT), Tc +Ran(IMAINT), NULL);
          ActLine = 63;
          sprintf (Message, "T= %f End maintenance \n",Tc);
          Display (Message);

          EvtCond = Yes;
        }
      }
    }

    ActComp =  NULL;

    return status;

    }

/*  ===================== Ende der Modellkomponente =====================  */
/*  =====================================================================  */
