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

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


    int qUEUE2 ( register struct qUEUE2 * 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 ("Queue2");
       LinkSubComp ( &(pData->NameElem) );



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

          StNxt(NQUEUE) = 0;   Sta(NQUEUE) =  0L;
          StNxt(NSERVER1) = 0;   Sta(NSERVER1) =  0L;
          StNxt(NSERVER2) = 0;   Sta(NSERVER2) =  0L;
          StNxt(TARRIVE) = 0;   Sta(TARRIVE) =  0.E0;
          StNxt(TSERVICE1) = 0;   Sta(TSERVICE1) =  0.E0;
          StNxt(TSERVICE2) = 0;   Sta(TSERVICE2) =  0.E0;

       Random(IARRIVE).NxtRan =  HeadRan;     HeadRan = Adr(IARRIVE); 
       Random(IARRIVE).GenNo  =  GenCount;    GenCount ++; 
        Random(IARRIVE).Dist   =  4; 
        Random(IARRIVE).P[0]   =  0.11E2;
        Random(IARRIVE).P[1]   =  0.0;
        Random(IARRIVE).P[2]   =  - 1.E+35;
        Random(IARRIVE).P[3]   =    1.E+35;
        Random(IARRIVE).P[4]   =  0.0;
       Random(ISERVICE).NxtRan =  HeadRan;     HeadRan = Adr(ISERVICE); 
       Random(ISERVICE).GenNo  =  GenCount;    GenCount ++; 
        Random(ISERVICE).Dist   =  4; 
        Random(ISERVICE).P[0]   =  0.1E2;
        Random(ISERVICE).P[1]   =  0.0;
        Random(ISERVICE).P[2]   =  - 1.E+35;
        Random(ISERVICE).P[3]   =    1.E+35;
        Random(ISERVICE).P[4]   =  0.0;

       VarEff (NQUEUE,CompNo)   = BCount;   VarEff (NQUEUE,EffFlag) = 2;
       VarEff (NQUEUE,eff_next) = NULL;
       VarEff (NSERVER1,CompNo)   = BCount;   VarEff (NSERVER1,EffFlag) = 2;
       VarEff (NSERVER1,eff_next) = NULL;
       VarEff (NSERVER2,CompNo)   = BCount;   VarEff (NSERVER2,EffFlag) = 2;
       VarEff (NSERVER2,eff_next) = NULL;
       VarEff (TARRIVE,CompNo)   = BCount;   VarEff (TARRIVE,EffFlag) = 2;
       VarEff (TARRIVE,eff_next) = NULL;
       VarEff (TSERVICE1,CompNo)   = BCount;   VarEff (TSERVICE1,EffFlag) = 2;
       VarEff (TSERVICE1,eff_next) = NULL;
       VarEff (TSERVICE2,CompNo)   = BCount;   VarEff (TSERVICE2,EffFlag) = 2;
       VarEff (TSERVICE2,eff_next) = NULL;

       for (i = TCmpCount + 1L;  i <= TCmpCount + 3L;  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 += 3;

       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 (WHENEVER) */
/*  =================== */
    if (Mode == Events)
    {
      ActLine = 23;
      if ((TCompare(ge,Sta(TARRIVE),TCmpNo(1L),0L)) )
      {
        if (CroDetect)  ExpectEvt = Yes;
        else
        {
          ActLine = 25;
          StaAsgnI (ASta(NQUEUE), Sta(NQUEUE) +1L, NULL);
          ActLine = 26;
          StaAsgnD (ASta(TARRIVE), Tc +Ran(IARRIVE), NULL);
          ActLine = 27;
          sprintf (Message, "T= %f Customer arrives and enters queue\n",Tc);
          Display (Message);

          EvtCond = Yes;
        }
      }
    }

/*  Ereignis (WHENEVER) */
/*  =================== */
    if (Mode == Events)
    {
      ActLine = 32;
      if (((Sta(NSERVER1)==0L))&&((Sta(NQUEUE)>0L)) )
      {
        if (CroDetect)  ExpectEvt = Yes;
        else
        {
          ActLine = 34;
          StaAsgnI (ASta(NQUEUE), Sta(NQUEUE) -1L, NULL);
          ActLine = 35;
          StaAsgnI (ASta(NSERVER1), Sta(NSERVER1) +1L, NULL);
          ActLine = 36;
          StaAsgnD (ASta(TSERVICE1), Tc +Ran(ISERVICE), NULL);
          ActLine = 37;
          sprintf (Message, "T= %f Customer enters service 1\n",Tc);
          Display (Message);

          EvtCond = Yes;
        }
      }
    }

/*  Ereignis (WHENEVER) */
/*  =================== */
    if (Mode == Events)
    {
      ActLine = 40;
      if (((Sta(NSERVER2)==0L))&&((Sta(NQUEUE)>0L))&&((Sta(NSERVER1)>0L)) )
      {
        if (CroDetect)  ExpectEvt = Yes;
        else
        {
          ActLine = 42;
          StaAsgnI (ASta(NQUEUE), Sta(NQUEUE) -1L, NULL);
          ActLine = 43;
          StaAsgnI (ASta(NSERVER2), Sta(NSERVER2) +1L, NULL);
          ActLine = 44;
          StaAsgnD (ASta(TSERVICE2), Tc +Ran(ISERVICE), NULL);
          ActLine = 45;
          sprintf (Message, "T= %f Customer enters service 2\n",Tc);
          Display (Message);

          EvtCond = Yes;
        }
      }
    }

/*  Ereignis (WHENEVER) */
/*  =================== */
    if (Mode == Events)
    {
      ActLine = 50;
      if ((TCompare(ge,Sta(TSERVICE1),TCmpNo(2L),0L))&&((Sta(NSERVER1)==1L)) )
      {
        if (CroDetect)  ExpectEvt = Yes;
        else
        {
          ActLine = 52;
          StaAsgnI (ASta(NSERVER1), 0L, NULL);
          ActLine = 53;
          sprintf (Message, "T= %f Customer leaves service 1\n",Tc);
          Display (Message);

          EvtCond = Yes;
        }
      }
    }

/*  Ereignis (WHENEVER) */
/*  =================== */
    if (Mode == Events)
    {
      ActLine = 56;
      if ((TCompare(ge,Sta(TSERVICE2),TCmpNo(3L),0L))&&((Sta(NSERVER2)==1L)) )
      {
        if (CroDetect)  ExpectEvt = Yes;
        else
        {
          ActLine = 58;
          StaAsgnI (ASta(NSERVER2), 0L, NULL);
          ActLine = 59;
          sprintf (Message, "T= %f Customer leaves service 2\n",Tc);
          Display (Message);

          EvtCond = Yes;
        }
      }
    }

    ActComp =  NULL;

    return status;

    }

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