
/*******************************************************************************
*
* File blk_tasks.h
*
* Copyright (C) 2005 Martin Luescher
*
* This software is distributed under the terms of the GNU General Public
* License (GPL)
*
* Block task descriptions
*
*******************************************************************************/

#define BLK_TASKS_H

#if (defined FLAGS_C)

static int (*blk_task_fcts[TASKS])(int nb)={NULL};


static int GridTaskAssignU2ubgr(int nb)
{
   blk_flags_t *bf;

   bf=(*gf).bf;

   return ((lat.u>0)&&((*gf).shu==0)&&(bf[0].u>0));
}


static int GridTaskAssignUd2ubgr(int nb)
{
   blk_flags_t *bf;

   bf=(*gf).bf;

   return ((lat.ud>0)&&((*gf).shu==0)&&(bf[0].u>0));
}


static int GridTaskAssignUd2udbgr(int nb)
{
   blk_flags_t *bf;

   bf=(*gf).bf;

   if ((*gf).shud==0)
      return ((lat.ud>0)&&(bf[0].ud>0));
   else
      return ((lat.ud>0)&&(nb<(*gf).nb));
}


static int GridTaskSwapActive(int nb)
{
   int n,nbf,nt;
   blk_flags_t *bf;

   nbf=(*gf).nb;
   bf=(*gf).bf;
   nt=((*gf).shud==1);

   for (n=0;n<nbf;n++)
   {
      if ((bf[n].ud==0)||(bf[n].ubd!=(*gf).ubd)||(bf[n].bcd!=lat.bcd))
         nt=1;
   }

   return ((lat.ud>0)&&(nt==0));
}


static int GridTaskAssignSwd2swbgr(int nb)
{
   blk_flags_t *bf;

   bf=(*gf).bf;

   return ((lat.swd[0]>0)&&((*gf).shu==0)&&(bf[0].u>0));
}


static int GridTaskAssignSwd2swdbgr(int nb)
{
   blk_flags_t *bf;

   bf=(*gf).bf;

   if ((*gf).shud==0)
      return ((lat.swd[0]>0)&&(bf[0].ud>0));
   else
      return ((lat.swd[0]>0)&&(nb<(*gf).nb));
}


static int GridTaskUpdateSwdbgr2swd(int nb)
{
   int n,nbf,nt;
   blk_flags_t *bf;

   nbf=(*gf).nb;
   bf=(*gf).bf;
   nt=((lat.ud==0)||(lat.swd[0]==0)||((*gf).shud==1));

   for (n=0;n<nbf;n++)
   {
      if ((bf[n].ud==0)||(bf[n].ubd==0)||(bf[n].ubd!=(*gf).ubd)||
          (bf[n].swb!=bf[n].ubd))
         nt=1;
   }
   
   return (nt==0);
}


static int GridTaskSwTermInactive(int nb)
{
   int n,nbf,nt;
   blk_flags_t *bf;

   nbf=(*gf).nb;
   bf=(*gf).bf;
   nt=0;

   for (n=0;n<nbf;n++)
   {
      if ((bf[n].ud==0)||(bf[n].ubd==0)||(bf[n].ubd!=(*gf).ubd)||
          (bf[n].swd[0]!=bf[n].ud)||(bf[n].swd[1]!=0)||(bf[n].swd[2]!=0))
         nt=1;
   }

   return (nt==0);
}


static int BlkTaskAssignUd2u(int nb)
{
   int n,nbf,nt;
   blk_flags_t *bf;

   nbf=(*gf).nb;
   bf=(*gf).bf;
   nt=((nb==nbf)&&(((*gf).shu==1)||((*gf).shud==1)));

   for (n=0;n<nbf;n++)
   {
      if ((nb==nbf)||(nb==n))
      {
         if ((bf[n].ud==0)||((bf[n].u==0)&&((*gf).shu==0)))
            nt=1;
      }
   }

   return (nt==0);
}


static int BlkTaskAssignSwd2sw(int nb)
{
   int n,nbf,nt;
   blk_flags_t *bf;

   nbf=(*gf).nb;
   bf=(*gf).bf;
   nt=((nb==nbf)&&(((*gf).shu==1)||((*gf).shud==1)));

   for (n=0;n<nbf;n++)
   {
      if ((nb==nbf)||(nb==n))
      {
         if ((bf[n].swd[0]==0)||((bf[n].u==0)&&((*gf).shu==0)))
            nt=1;
      }
   }

   return (nt==0);
}


static int BlkTaskAction(int nb)
{
   int n,nbf,nt;
   blk_flags_t *bf;

   nbf=(*gf).nb;
   bf=(*gf).bf;
   nt=((nb==nbf)&&((*gf).shu==1));

   for (n=0;n<nbf;n++)
   {
      if ((nb==nbf)||(nb==n))
      {
         if ((bf[n].u==0)||(bf[n].ub==0))
            nt=1;
      }
   }

   return (nt==0);
}


static int BlkTaskActionDble(int nb)
{
   int n,nbf,nt;
   blk_flags_t *bf;

   nbf=(*gf).nb;
   bf=(*gf).bf;
   nt=((nb==nbf)&&((*gf).shud==1));

   for (n=0;n<nbf;n++)
   {
      if ((nb==nbf)||(nb==n))
      {
         if ((bf[n].ud==0)||(bf[n].ubd==0))
            nt=1;
      }
   }

   return (nt==0);
}


static int BlkTaskSwTerm(int nb)
{
   int n,nbf,nt;
   blk_flags_t *bf;

   nbf=(*gf).nb;
   bf=(*gf).bf;
   nt=((nb==nbf)&&((*gf).shud==1));

   for (n=0;n<nbf;n++)
   {
      if ((nb==nbf)||(nb==n))
      {
         if ((bf[n].ud==0)||(bf[n].ubd==0)||(bf[n].swb!=bf[n].ubd))
            nt=1;
      }
   }

   return (nt==0);
}


static int BlkTaskQhat(int nb)
{
   int n,nbf,nt;
   blk_flags_t *bf;

   nbf=(*gf).nb;
   bf=(*gf).bf;
   nt=((nb==nbf)&&((*gf).shu==1));

   for (n=0;n<nbf;n++)
   {
      if ((nb==nbf)||(nb==n))
      {
         if ((bf[n].u==0)||(bf[n].ub==0)||(bf[n].sw[0]!=bf[n].u)||
             (bf[n].sw[1]!=0)||(bf[n].sw[2]!=1))
            nt=1;
      }
   }

   return (nt==0);
}


static int BlkTaskQnohat(int nb)
{
   int n,nbf,nt;
   blk_flags_t *bf;

   nbf=(*gf).nb;
   bf=(*gf).bf;
   nt=((nb==nbf)&&((*gf).shu==1));

   for (n=0;n<nbf;n++)
   {
      if ((nb==nbf)||(nb==n))
      {
         if ((bf[n].u==0)||(bf[n].ub==0)||(bf[n].sw[0]!=bf[n].u)||
             (bf[n].sw[1]!=0)||(bf[n].sw[2]!=0))
            nt=1;
      }
   }

   return (nt==0);
}


static int BlkTaskQeo(int nb)
{
   int n,nbf,nt;
   blk_flags_t *bf;

   nbf=(*gf).nb;
   bf=(*gf).bf;
   nt=((nb==nbf)&&((*gf).shu==1));

   for (n=0;n<nbf;n++)
   {
      if ((nb==nbf)||(nb==n))
      {
         if ((bf[n].u==0)||(bf[n].ub==0))
            nt=1;
      }
   }

   return (nt==0);
}


static int BlkTaskQhatDble(int nb)
{
   int n,nbf,nt;
   blk_flags_t *bf;

   nbf=(*gf).nb;
   bf=(*gf).bf;
   nt=((nb==nbf)&&((*gf).shud==1));

   for (n=0;n<nbf;n++)
   {
      if ((nb==nbf)||(nb==n))
      {
         if ((bf[n].ud==0)||(bf[n].ubd==0)||(bf[n].swd[0]!=bf[n].ud)||
             (bf[n].swd[1]!=0)||(bf[n].swd[2]!=1))
            nt=1;
      }
   }

   return (nt==0);
}


static int BlkTaskQnohatDble(int nb)
{
   int n,nbf,nt;
   blk_flags_t *bf;

   nbf=(*gf).nb;
   bf=(*gf).bf;
   nt=((nb==nbf)&&((*gf).shud==1));

   for (n=0;n<nbf;n++)
   {
      if ((nb==nbf)||(nb==n))
      {
         if ((bf[n].ud==0)||(bf[n].ubd==0)||(bf[n].swd[0]!=bf[n].ud)||
             (bf[n].swd[1]!=0)||(bf[n].swd[2]!=0))
            nt=1;
      }
   }

   return (nt==0);
}


static int BlkTaskQeoDble(int nb)
{
   int n,nbf,nt;
   blk_flags_t *bf;

   nbf=(*gf).nb;
   bf=(*gf).bf;
   nt=((nb==nbf)&&((*gf).shud==1));

   for (n=0;n<nbf;n++)
   {
      if ((nb==nbf)||(nb==n))
      {
         if ((bf[n].ud==0)||(bf[n].ubd==0))
            nt=1;
      }
   }

   return (nt==0);
}


static void set_blk_tasks(void)
{
   blk_task_fcts[ASSIGN_U2UBGR]=GridTaskAssignU2ubgr;
   blk_task_fcts[ASSIGN_UD2UBGR]=GridTaskAssignUd2ubgr;
   blk_task_fcts[ASSIGN_UD2UDBGR]=GridTaskAssignUd2udbgr;
   blk_task_fcts[SWAP_ACTIVE]=GridTaskSwapActive;
   blk_task_fcts[ASSIGN_SWD2SWBGR]=GridTaskAssignSwd2swbgr;
   blk_task_fcts[ASSIGN_SWD2SWDBGR]=GridTaskAssignSwd2swdbgr;
   blk_task_fcts[UPDATE_SWDBGR2SWD]=GridTaskUpdateSwdbgr2swd;
   blk_task_fcts[CHMASS_SWDBGR2SWD]=GridTaskUpdateSwdbgr2swd;   
   blk_task_fcts[SW_TERM_INACTIVE]=GridTaskSwTermInactive;
   
   blk_task_fcts[ASSIGN_UD2U]=BlkTaskAssignUd2u;
   blk_task_fcts[ASSIGN_SWD2SW]=BlkTaskAssignSwd2sw;
   blk_task_fcts[ACTION]=BlkTaskAction;
   blk_task_fcts[ACTION_DBLE]=BlkTaskActionDble;
   blk_task_fcts[SW_TERM]=BlkTaskSwTerm;
   blk_task_fcts[QHAT]=BlkTaskQhat;
   blk_task_fcts[QNOHAT]=BlkTaskQnohat;
   blk_task_fcts[QEO]=BlkTaskQeo;
   blk_task_fcts[QOE]=BlkTaskQeo;
   blk_task_fcts[QHAT_DBLE]=BlkTaskQhatDble;
   blk_task_fcts[QNOHAT_DBLE]=BlkTaskQnohatDble;
   blk_task_fcts[QEO_DBLE]=BlkTaskQeoDble;
   blk_task_fcts[QOE_DBLE]=BlkTaskQeoDble;
}

#endif
