Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 6001efcf2f
Fetching contributors…

Cannot retrieve contributors at this time

file 199 lines (166 sloc) 4.795 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
#include "switch.h"
#include "board.h"

void switchReact( playField* pf, int x, int y ); //Should be used only private


int switchSetTargets( playField* pf )
{
  //Figure out switch targets
  listItem* it = pf->levelInfo->switchList;
  while( (it=it->next) )
  {
    switch_t* sw = (switch_t*)it->data;
    //Sanity check
    if( !isSwitch( pf->board[sw->sx][sw->sy] ) )
    {
      printf("Switch error: List tells there is a switch at %i,%i but that is not the case.\n", sw->sx, sw->sy);
      free(sw);
      listRemoveItem(pf->levelInfo->switchList, it );
      continue;
    }

    //Check that the target is a supported type
    if( !switchIsValidTarget( pf, sw->dx, sw->dy ) )
    {
      printf("Switch error: Switch at %i,%i points at %i,%i but that's not a valid target brick.\n", sw->sx, sw->sy, sw->dx, sw->dy);
      free(sw);
      listRemoveItem(pf->levelInfo->switchList, it );
      continue;
    }

    switchAttachTarget( pf, sw );
    //We set the switch itself to have alive = -1 which causes it to be updated.
    pf->board[sw->sx][sw->sy]->isActive = -1;

  }

  return(1);
}

int switchFindTele( playField* pf, int x, int y )
{
  //When found, we set target = reservedbrick then use sx/dx hack for teleport destination.
  listItem* it = pf->levelInfo->teleList;
  telePort_t* tp;

  while( (it=it->next) )
  {
    tp = (telePort_t*)it->data;
    if( tp->sx == x && tp->sy == y )
    {
      return(1);
    }
  }

  return(0);
}

int switchIsValidTarget( playField* pf, int x, int y )
{

  if( isWall( pf, x, y ) || isMover(pf->board[x][y]) || switchFindTele(pf, x, y) )
  {
    return(1);
  }

 return(0);
}

void switchAttachTarget( playField* pf, switch_t* sw )
{
  //If it's a walltype or mover.
  if( isWall( pf, sw->dx, sw->dy ) || isMover(pf->board[sw->dx][sw->dy]) )
  {
    pf->board[sw->sx][sw->sy]->target = pf->board[sw->dx][sw->dy];
  }

  //If it's a teleport
  if( switchFindTele( pf, sw->dx, sw->dy ) )
  {
    //So, the teleport knows that if the teleport has a target brick thats a blocker, it should look at switches, riight...
    pf->board[sw->sx][sw->sy]->target = pf->blocker;
  }

}

//Tell if a switch is disabled and pointing to x,y
int switchAmIEnabled(playField* pf, int x, int y)
{
  listItem* it=pf->levelInfo->switchList;
  switch_t* sw;
  //Do any switch have this
  while( (it=it->next) )
  {
    sw = (switch_t*)it->data;
    if( sw->dx==x && sw->dy==y )
    {
      return( pf->board[sw->sx][sw->sy]->isActive );
    }
  }

  //If no switch points to this brick, it's active.
  return(1);
}

void switchReact( playField* pf, int x, int y )
{
  int newState;

  if( y>0 && pf->board[x][y-1] && isBrick(pf->board[x][y-1]) )
  {
    newState = (pf->board[x][y]->type==SWOFF)?0:1;
  } else {
    newState = (pf->board[x][y]->type==SWOFF)?1:0;
  }

  if( pf->board[x][y]->isActive != newState )
  {
    pf->board[x][y]->isActive=newState;
    switchAffectTarget(pf, x, y, newState );
    if(newState)
    {
      printf("Sound: Click! Switch is active!\n");
    } else {
      printf("Sound: Click! Switch is inactive!\n");
    }
  }
}

void switchAffectTarget( playField* pf, int x, int y, int newState )
{
  brickType* s = pf->board[x][y];

  switch( s->target->type )
  {
    // Walls are lifted off the board and placed in deactivated.
    case STDWALL:


      s->target->isActive = newState;

      //We turn off the brick. (if the brick is there, it might not be as we could have lifted it off, placed a brick at destination, and moved off the switch and now try to lift it again)
      if( !newState && (pf->board[ s->target->dx ][ s->target->dy ]==s->target) )
      {
        listAddData( pf->deactivated, (void*)s->target );
        pf->board[ s->target->dx ][ s->target->dy ]=NULL;
      }
      boardSetWalls( pf );
    break;

    //These types only have their active flag modified.
    case GLUE:
    case MOVERHORIZ:
    case MOVERVERT:
    case ONEWAYLEFT:
    case ONEWAYRIGHT:
      s->target->isActive = newState;
    break;

    //Teleports will watch for switches and need no modification.
    case RESERVED:

    break;

    default:
      printf( "Switch error: Type %i not handled.\n", s->target->type );
    break;
  }
}

void switchUpdateAll( playField* pf )
{
  listItem* it =pf->levelInfo->switchList;

  while( (it=it->next) )
  {
    telePort_t* sw = (telePort_t*)it->data;
    switchReact(pf, sw->sx, sw->sy );
  }
}

void switchPutBack(playField* pf)
{
  //Now we put down activated bricks from list:
  listItem* it = pf->deactivated;
  while( (it=it->next) )
  {
    brickType* b = (brickType*)it->data;
    if( b->isActive )
    {
      if(!pf->board[b->dx][b->dy])
      {
        pf->board[b->dx][b->dy]=b;
        boardSetWalls(pf);
        it=listRemoveItem(pf->deactivated,it);
      }
    }
  }
}
Something went wrong with that request. Please try again.