#include <WScom.h>
#include <WSCfunctionList.h>
#include <WSCbase.h>
//----------------------------------------------------------
//Function for the event procedure
//----------------------------------------------------------
#include <WSCmessageDialog.h>
#include <newwin000.h>
extern WSClistData invader_list_x;
extern WSClistData invader_list_y;
extern WSClistData fence_list_x;
extern WSClistData fence_list_y;
extern WSClistData invader_missile_list_x;
extern WSClistData invader_missile_list_y;
extern WSClistData my_missile_list_x;
extern WSClistData my_missile_list_y;
extern long        my_base_x;
extern long        my_base_y;

#define RIGHT 0
#define LEFT  1
long direction = RIGHT;

void drawep(WSCbase* object){
  newvdra_001->setForeColor("#000000");
  newvdra_001->drawRect(0,0,400,400);
  newvdra_001->setForeColor("#ffffff");

  long most_right = -1;
  long most_left = -1;
  long diff_x = 0;
  long diff_y = 0;
  long num,i;
  num = invader_list_x.getNum();
  for(i=0; i<num; i++){
    long pt_x = (long)invader_list_x[i];
    long pt_y = (long)invader_list_y[i];
    if (most_right == -1){
      most_right = pt_x;
    }
    if (most_left == -1){
      most_left = pt_x;
    }
    if (pt_x < most_left){
      most_left = pt_x;
    }
    if (pt_x > most_right){
      most_right = pt_x;
    }
    if ( (int)(rand()*200.0 / (RAND_MAX+1)) == 0){
      invader_missile_list_x.add((void*)( pt_x+5 ));
      invader_missile_list_y.add((void*)( pt_y   ));
    }
  }
  if (direction == RIGHT){
    diff_x = 1;
    if (most_right > 380){
      direction = LEFT;
      diff_x = -1;
      diff_y = 1;
    }
  }else{
    diff_x = -1;
    if (most_left < 20){
      direction = RIGHT;
      diff_x = 1;
      diff_y = 1;
    }
  }
  for(i=0; i<num; i++){
    long pt_x = (long)invader_list_x[i];
    long pt_y = (long)invader_list_y[i];
    pt_x += diff_x;
    pt_y += diff_y;
    invader_list_x[i] = (void*)pt_x;
    invader_list_y[i] = (void*)pt_y;

    if ((pt_x/20 + pt_y/20)%2 != 0){
      newvdra_001->drawImage(pt_x,pt_y,10,10,"1.xpm",WS_CENTER);
    }else{
      newvdra_001->drawImage(pt_x,pt_y,10,10,"2.xpm",WS_CENTER);
    }
  }

  newvdra_001->setForeColor("#ff00ff");
  num = invader_missile_list_x.getNum();
  for(i=0; i<num; i++){
    long pt_x = (long)invader_missile_list_x[i];
    long pt_y = (long)invader_missile_list_y[i];
    pt_y = pt_y +3;
    invader_missile_list_y[i] = (void*)pt_y;
    newvdra_001->drawFillRect(pt_x-2,pt_y,3,6);
    if (pt_y > 400){
      invader_missile_list_x.delPos(i);
      invader_missile_list_y.delPos(i);
      num--;
      i--;
    }
  }

  newvdra_001->setForeColor("#00ff00");
  num = my_missile_list_x.getNum();
  for(i=0; i<num; i++){
    long pt_x = (long)my_missile_list_x[i];
    long pt_y = (long)my_missile_list_y[i];
    pt_y = pt_y -10;
    my_missile_list_y[i] = (void*)pt_y;
    newvdra_001->drawFillRect(pt_x-2,pt_y,3,6);
    if (pt_y < 0){
      my_missile_list_x.delPos(i);
      my_missile_list_y.delPos(i);
      i--;
      num--;
    }
  }

  newvdra_001->setForeColor("#ffff00");
  num = fence_list_x.getNum();
  for(i=0; i<num; i++){
    long pt_x = (long)fence_list_x[i];
    long pt_y = (long)fence_list_y[i];
    newvdra_001->drawFillRect(pt_x,pt_y,5,5);
  }

  newvdra_001->drawImage(my_base_x,my_base_y,10,10,"base.xpm",WS_CENTER);
}
static WSCfunctionRegister  op("drawep",(void*)drawep);
