Example #1
0
void DosLifetimeSpline::calculate()
{
  if (!checkxsection()) return; //if a problem with the cross section we are pointing to, stop calculating
  //get the parameters
  const Parameter* Estartptr= getparameter(0);
  const double Estart=Estartptr->getvalue();
  const Parameter* Estopptr= getparameter(1);
  const double Estop=Estopptr->getvalue();
   const Parameter* aptr= getparameter(2);


  //calculate the DOS
  Parameter* pointptr=0; //pointer to a datapoint in the list
  bool changes=false;
  for (size_t index=0;index<degree;index++){
    pointptr=getparameter(index+3);
    changes=(changes||pointptr->changed()); //see if any of the data point parameters has changed
   }
  if ((Estartptr->changed()||Estopptr->changed())||aptr->changed()){
      //setup a new energy grid
    InitEnergy();
    changes=true;
  }
  if (changes){
    //if anything changed, we need to calculate, if not we leave without calculating
    #ifdef COMPONENT_DEBUG
    std::cout << "parameters changed calculating DOS \n degree: " << degree <<"\n";
    std::cout << "Estart: " << Estart <<"\n";
    std::cout << "Estop: " << Estop <<"\n";
    for (size_t index=0;index<degree;index++){
      std::cout<< " a" <<index<<" :"<<getparameter(index+3)->getvalue()<< "\n";
    }
    #endif


   //copy the parameter values in the spline vectors
    copyparameters();

    //calculate the spline parameters
    dospline();


    //do the evaluation
  for (size_t i=0; i<this->getnpoints();i++){
        const double en=this->getenergy(i);

        double cts=0.0;

        if ((en>Estart)&(en<Estop)){
          cts=seval(en);
        }
        if (en<=Estart){
            cts=0.0;
        }
        if (en>=Estop){
            cts=1.0;
        }
    this->setcounts(i,cts);
  }

  //set parameters as unchanged since last time we calculated
  this->setunchanged();

  }
  else{
#ifdef COMPONENT_DEBUG
    std::cout <<"parameters have not changed, i don't need to calculate again\n";
#endif
  }

}
Example #2
0
DosLifetimeSpline::DosLifetimeSpline(int n,double estart,double dispersion,std::vector<Parameter*>* parameterlistptr)
:Component(n,estart,dispersion),Evector(),Yvector(),b(),c(),d()
{
    broadeningtype=QUADRATIC;

    //offset=2;
    offset=2;
    //create spectrum with same energy scale as this
    dummy=new Spectrum(n,estart,dispersion);
Plotspec=0;
  compptr=0;
  if (parameterlistptr==0){
    //ask for the degree of the polynomial between 1 and 100
    const int min=1;
    const int max=1024;
    int d=10;
    Integerinput myinput(0,"","enter number of points",d,min,max);
    degree=size_t(d);
    //enter Estart and Estop
    Parameter* p1=new Parameter("Estart",estart,1);
    p1->interactivevalue("Enter Estart");
    p1->setchangeable(false);
    this->addparameter(p1);



    Parameter* p2=new Parameter("Estop",estart,1);
    p2->interactivevalue("Enter Estop");
    p2->setchangeable(false);
    this->addparameter(p2);


    //atomic to calculate the lifetime broadening
    Parameter* p3=new Parameter("a",0.4,1);

    //choose between different types of broadening
    int broadtype=0;
    Integerinput* myask=new Integerinput(0, "Lifetime broadening selector","1=Lin.,2=Quadr.,3=Egert.",broadtype,1,3);
    (void) myask;
    switch(broadtype){
        case 1:
            p3->setname("Linear coefficient");

            break;
        case 2:
            p3->setname("Quadratic coefficient");
            break;
        case 3:
            p3->setname("Egerton Broadening atomic distance [nm]");
            break;
        default:
            break;
    }
    p3->setboundaries(0,10.0);
    p3->interactivevalue("Enter broadening coefficient (0.0 for linear scale)");
    p3->setchangeable(false);
    this->addparameter(p3);

    //do we want linear or lifetime optimised energy points
    linear=false;
    if ((p3->getvalue())==0.0){
        //only for linear scale we need Estop
        //for lieftime broadening Estop is calculated by initenergy
        linear=true;
    }







    for (size_t i=0;i<degree;i++){
      std::string name;
      std::ostringstream s;
      if ((s << "a"<< i)){ //converting an int to a string in c++ style rather than unsafe c-style
	     // conversion worked
	     name=s.str();
      }
      //store parameters to hold the strengths of the basis set L
      Parameter* p=new Parameter(name,1.0,1);
      p->setboundaries(-1.0,10.0);
      //copy this parameter
      this->addparameter(p);
    }

    //now link this to a cross section which needs to be multiplied
    Model* mymodel=geteelsmodelptr()->getmodel_nonconst();
    int cnr=0;

    //create a componentselector here
    Componentselector myinput2(0,"","Select the component you want to multiply with",cnr);



    //get a pointer to this component and tell it that we are his multiplier
    compptr=mymodel->getcomponent(cnr);
    set_ismultiplier(true);//important do it here, otherwise multiplierptr is not accepted
    if (compptr!=0){
      compptr->setmultiplierptr(this);
    }
    else{
      //something went wrong
      Saysomething mysay(0,"Error","the component didn't appear to be valid");
      throw Componenterr::unable_to_create();
    }
    //save this number in a parameter
    Parameter* p5=new Parameter("compnr",cnr,1);
    p5->setchangeable(false);
    this->addparameter(p5);
  }
  else{
    //get parameters from a list
    for (size_t i=0;i<(parameterlistptr->size());i++){
      Parameter* p=(*parameterlistptr)[i];
      this->addparameter(p);
    }
    degree=(parameterlistptr->size())-4;//there are 4 other parameters

    //tell the component that we multiply that we are here
    Parameter* p5=(*parameterlistptr)[parameterlistptr->size()-1];
    Model* mymodel=geteelsmodelptr()->getmodel_nonconst();
    const int cnr=int(p5->getvalue());
    //get a pointer to this component and tell it that we are his multiplier
    compptr=mymodel->getcomponent(cnr);
    set_ismultiplier(true);//important do it here, otherwise multiplierptr is not accepted
    if (compptr!=0) {
      compptr->setmultiplierptr(this);
    }
#ifdef COMPONENT_DEBUG
      std::cout <<"After linking to the cross section\n";
#endif

} //end of else

  #ifdef COMPONENT_DEBUG
      std::cout <<"Setting the names etc\n";
#endif
  //give a name and description
  setname("Fine Structure (DOS) with lifetime (cubic  spline)");
  setdescription("Fine Structure used in combination with a normal cross-section using Lifetime broadening as an extra prior knowledge");
  setcanconvolute(true);
  setshifter(false);
  set_ismultiplier(true);


  /*
  for (size_t i=0;i<degree;i++){
    //tell that we have a gradient for each point a_i
    this->sethasgradient(i+3,true);
      }
*/


    InitEnergy(); //prepare the energy points that are linked to the paramters
    gslinit(); //setup the memory for the gsl fitter
    calculate();
    setvisible(true);



    Plotspec=new Spectrum(Evector.size());
    initplotspec();
    (this->getgraphptr())->addgraph(Plotspec);
    (this->getgraphptr())->setstyle(1,2); //set style of this plot to dots instead of lines


    //show the current DOS



    //make a new spectrum containing Evector,Yvector


    //show an equaliser
    this->showequalizer();
}
int main(void)
{
    bool done = false; // Variavel booleana para identificar se o programa terminou de ser executado
    bool redraw = true; // Enquanto essa variavel for verdadeira, ira ser desenhado algo na tela
    bool desenha = true;
    int draw[5]= {0, 0, 0, 0, 0};
    int pos_x = WIDTH / 2;
    int pos_y = HEIGHT / 2;
    int timer_tiros = 0;
    int timer_energy = 0;
    int timer_heats = 0;
    int timer_tamenho_heat = 0;
    int timer_componente = 0;
    int count = 0;


    Gamer(gamer);
    Zone zone[LINHAS][COLUNAS];
    Bullet bullets[NUM_BULLETS+1];
    Energy energy[NUM_ENERGYS+1];
    Heat heats[NUM_HEAT+1];
    Zombie zombie[NUM_ZOMBIES];
    Battery battery[NUM_BATTERY];


    ALLEGRO_DISPLAY *display = NULL;
    ALLEGRO_EVENT_QUEUE *event_queue = NULL;
    ALLEGRO_TIMER *timer = NULL;
    ALLEGRO_FONT *font18 = NULL;
    ALLEGRO_FONT *font_maior = NULL;
    ALLEGRO_BITMAP *resistor = NULL;
    ALLEGRO_BITMAP *capacitor = NULL;
    ALLEGRO_BITMAP *indutor = NULL;
    ALLEGRO_BITMAP *diodo = NULL;
    ALLEGRO_BITMAP *bateria = NULL;
    ALLEGRO_BITMAP *protoboard = NULL;
    ALLEGRO_BITMAP *fogo = NULL;
    ALLEGRO_BITMAP *zombie_bitmap = NULL;
    ALLEGRO_BITMAP *ataque_eletromagnetico = NULL;
    ALLEGRO_BITMAP *energia_capacitor = NULL;
    ALLEGRO_BITMAP *logo = NULL;


    if(!al_init())										//initialize Allegro
        return -1;

    display = al_create_display(WIDTH, HEIGHT);			//create our display object

    if(!display)										//test display object
        return -1;

    al_init_primitives_addon();
    al_install_keyboard();
    al_init_font_addon();
    al_init_ttf_addon();
    al_install_mouse();
    al_init_image_addon();

    resistor = al_load_bitmap("Resistor.png");
    capacitor = al_load_bitmap("Capacitor.png");
    indutor = al_load_bitmap("Indutor.png");
    diodo = al_load_bitmap("Diodo.png");
    bateria = al_load_bitmap("Bateria.png");
    protoboard = al_load_bitmap("Protoboard.png");
    fogo = al_load_bitmap("fogo_resistor/fire1.png");
    zombie_bitmap = al_load_bitmap("Zombie.png");
    ataque_eletromagnetico = al_load_bitmap("eletromagnetismo.jpg");
    energia_capacitor = al_load_bitmap("energia_capacitor.png");
    logo = al_load_bitmap("logo_EvsZ.jpg");

    al_convert_mask_to_alpha(resistor, al_map_rgb(255, 0, 255));
    al_convert_mask_to_alpha(capacitor, al_map_rgb(255, 0, 255));
    al_convert_mask_to_alpha(indutor, al_map_rgb(255, 0, 255));
    al_convert_mask_to_alpha(diodo, al_map_rgb(255, 0, 255));
    al_convert_mask_to_alpha(bateria, al_map_rgb(255, 0, 255));
    al_convert_mask_to_alpha(zombie_bitmap, al_map_rgb(255, 255, 255));
    al_convert_mask_to_alpha(ataque_eletromagnetico, al_map_rgb(255, 255, 255));


    event_queue = al_create_event_queue();
    timer = al_create_timer(1.0 / FPS);

    InitGamer(gamer);
    InitZone(zone, LINHAS, COLUNAS);
    InitBullet(bullets, NUM_BULLETS+1);
    InitHeat(heats, NUM_HEAT+1);
    InitZombie(zombie, NUM_ZOMBIES);
    InitEnergy(energy, NUM_ENERGYS+1);
    InitBattery(battery, NUM_BATTERY);

    font18 = al_load_font("arial.ttf", 18, 0);
    font_maior = al_load_font("arial.ttf", 24, 0);

    al_register_event_source(event_queue, al_get_mouse_event_source());
    al_register_event_source(event_queue, al_get_keyboard_event_source());
    al_register_event_source(event_queue, al_get_timer_event_source(timer));
    al_register_event_source(event_queue, al_get_display_event_source(display));

    al_hide_mouse_cursor(display);
    al_start_timer(timer);

    while(!done)
    {
        ALLEGRO_EVENT ev;
/*===============================================================================================================================================*/
        if(state == MENU)
        {
            al_wait_for_event(event_queue, &ev);

            al_draw_bitmap(logo, WIDTH / 2 - 145, HEIGHT - 500, 0);
            al_draw_text(font18, al_map_rgb(255, 255, 255), WIDTH / 2, HEIGHT / 2, ALLEGRO_ALIGN_CENTRE, "Os Zombies querem roubar seu diploma, proteja-o");
            al_draw_text(font18, al_map_rgb(0, 255, 255), WIDTH / 2, HEIGHT / 2 + 100, ALLEGRO_ALIGN_CENTRE, "Pressione Space para jogar");
            al_draw_text(font_maior, al_map_rgb(0, 255, 0), WIDTH / 2, HEIGHT / 2 + 150, ALLEGRO_ALIGN_CENTRE, "Tecla 1 = Resistor Tecla 2 = Capacitor  Tecla 3 = Indutor  Tecla 4 = Diodo");
            if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
                if(ev.keyboard.keycode == ALLEGRO_KEY_SPACE)
                    state = PLAYING;

            if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
                done = true;

            al_flip_display();

        }
/*===============================================================================================================================================*/
        if(state == PLAYING)
        {
            al_wait_for_event(event_queue, &ev);

            if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
                done = true;

            if(ev.type == ALLEGRO_EVENT_TIMER)
            {
                redraw = true;


                    for(int i = 0; i < LINHAS; i++)
                        for(int j=0; j<COLUNAS; j++)
                            if(zone[i][j].x < pos_x &&
                                    zone[i][j].x + ZONEX > pos_x &&
                                    zone[i][j].y < pos_y &&
                                    zone[i][j].y + ZONEY > pos_y)
                                if(desenha)
                                    if(zone[i][j].draw == 0)
                                    {
                                        if(keys[KEY_1] && gamer.energy >= 100)
                                        {
                                            zone[i][j].draw = draw[1];
                                            gamer.energy -=100;
                                            draw[1] = 0;
                                        }
                                        if(keys[KEY_2] && gamer.energy >= 50)
                                        {
                                            zone[i][j].draw = draw[2];
                                            gamer.energy -=50;
                                            draw[2] = 0;
                                        }
                                        if(keys[KEY_3] && gamer.energy >= 100)
                                        {
                                            zone[i][j].draw = draw[3];
                                            gamer.energy -=100;
                                            draw[3] = 0;
                                        }
                                        if(keys[KEY_4] && gamer.energy >= 100)
                                        {
                                            zone[i][j].draw = draw[4];
                                            gamer.energy -=100;
                                            draw[4] = 0;
                                        }
                                    }
                timer_battery_speed++;
                timer_battery_start++;
                if(timer_battery_start >= 360) // diminui a frequencia com que nasce uma bateria nova
                {
                    StartBattery(battery, NUM_BATTERY);
                    timer_battery_start = 0;
                }

                if(timer_battery_speed >= 2) // reduz um pouco a velocidade da bateria
                {
                    UpdateBattery(battery, NUM_BATTERY);
                    timer_battery_speed = 0;
                }


                timer_heats++;

                for(int i=0; i<LINHAS; i++)
                    for(int j=0; j<COLUNAS; j++)
                        if(zone[i][j].draw == 1)
                            if(timer_heats >= 200)
                            {
                                FireHeat(heats, NUM_HEAT+1, zone);
                                timer_heats = 0;
                            }

                timer_energy++;
                timer_energy_cap_death++;

                for(int i=0; i<LINHAS; i++)
                    for(int j=0; j<COLUNAS; j++)
                        if(zone[i][j].draw == 2)
                        {
                            if(timer_energy >= 420)
                            {
                                CreateEnergy(energy, NUM_ENERGYS+1, zone);
                                timer_energy = 0;
                            }

                        }

                timer_tiros++;

                for(int i=0; i<LINHAS; i++)
                    for(int j=0; j<COLUNAS; j++)
                        if(zone[i][j].draw == 3)
                            if(timer_tiros >= 200) // faz os Electronics atirarem numa velocidade constante
                            {
                                FireBullet(bullets, NUM_BULLETS+1, zone);
                                timer_tiros = 0;
                            }
                timer_zombie_start++;
                timer_zombie_speed++;
                timer_dificuldade++;
                if(timer_zombie_start >= 3)
                {
                    StartZombie(zombie, NUM_ZOMBIES);
                    timer_zombie_start = 0;
                }
                if(dificuldade >20 && dificuldade <=500)
                {
                if(timer_dificuldade >= 300)
                    {
                        dificuldade -= 30;
                        timer_dificuldade = 0;
                    }
                }
                if(timer_zombie_speed >= 3)
                {
                    UpdateZombie(zombie, NUM_ZOMBIES);
                    timer_zombie_speed = 0;
                }


                UpdateBullet(bullets, NUM_BULLETS+1);
                CollideBullet(bullets, NUM_BULLETS, zombie, NUM_ZOMBIES, gamer);
                CollideHeat(heats, NUM_HEAT, zombie, NUM_ZOMBIES, gamer, timer_tamenho_heat);
                CollideZone(zone, LINHAS, COLUNAS, zombie, NUM_ZOMBIES);
                CollideZoneDiodo(zone, LINHAS, COLUNAS, zombie, NUM_ZOMBIES);


                for(int i = 0; i < NUM_ENERGYS; i++)
                    if(energy[i].live)
                        if(energy[i].x - energy[i].boundx < pos_x &&
                                energy[i].x + energy[i].boundx > pos_x &&
                                energy[i].y - energy[i].boundy < pos_y &&
                                energy[i].y + energy[i].boundy > pos_y)
                        {
                            energy[i].live = false;
                            gamer.energy += 25;
                        }


            }
            else if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
            {
                switch(ev.keyboard.keycode)
                {
                case ALLEGRO_KEY_ESCAPE:
                    done = true;
                    break;
                case ALLEGRO_KEY_1:
                    keys[KEY_1] = true;
                    break;
                case ALLEGRO_KEY_2:
                    keys[KEY_2] = true;
                    break;
                case ALLEGRO_KEY_3:
                    keys[KEY_3] = true;
                    break;
                case ALLEGRO_KEY_4:
                    keys[KEY_4] = true;
                    break;
                }
            }
            else if(ev.type == ALLEGRO_EVENT_KEY_UP)
            {
                switch(ev.keyboard.keycode)
                {
                case ALLEGRO_KEY_ESCAPE:
                    done = true;
                    break;
                case ALLEGRO_KEY_1:
                    keys[KEY_1] = false;
                    break;
                case ALLEGRO_KEY_2:
                    keys[KEY_2] = false;
                    break;
                case ALLEGRO_KEY_3:
                    keys[KEY_3] = false;
                    break;
                case ALLEGRO_KEY_4:
                    keys[KEY_4] = false;
                    break;
                }
            }
            else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
            {
                if (ev.mouse.button & 2)
                    done = true;
            }
            else if(ev.type == ALLEGRO_EVENT_MOUSE_AXES)
            {
                pos_x = ev.mouse.x;
                pos_y = ev.mouse.y;
                CaptureBattery(battery, NUM_BATTERY, ev.mouse.x, ev.mouse.y, gamer);
            }

            timer_componente++;

            if(timer_componente >= 10)
            {
                if(keys[KEY_1])
                    draw[1] = 1;
                if(keys[KEY_2])
                    draw[2] = 2;
                if(keys[KEY_3])
                    draw[3] = 3;
                if(keys[KEY_4])
                    draw[4] = 4;
            }

            if(redraw && al_is_event_queue_empty(event_queue))
            {
                redraw = false;


                DrawZone(zone, LINHAS, COLUNAS, resistor, capacitor, indutor, diodo);
                DrawBullet(bullets, NUM_BULLETS+1, ataque_eletromagnetico);
                DrawEnergy(energy, NUM_ENERGYS+1, energia_capacitor);
                DrawZombie(zombie, NUM_ZOMBIES, zombie_bitmap);
                DrawBattery(battery, NUM_BATTERY, bateria);
                timer_tamenho_heat++;
                DrawHeat(heats, NUM_HEAT+1, timer_tamenho_heat, fogo);
                if(timer_tamenho_heat > 80)
                    for(int i=0; i<NUM_HEAT; i++)
                    {
                        heats[i].live = false;
                        timer_tamenho_heat = 0;
                    }


                al_draw_filled_rectangle(pos_x, pos_y, pos_x + 10, pos_y + 10, al_map_rgb(0, 0, 0));
                count++;
                al_draw_textf(font18, al_map_rgb(255, 0, 0), WIDTH*13/16, 85, 0, "Time: %i", count/60);
                al_draw_textf(font18, al_map_rgb(255, 0, 0), WIDTH*13/300, 85, 0, "Energy: %i", gamer.energy);
                al_draw_textf(font18, al_map_rgb(255, 0, 0), WIDTH*13/25, 85, 0, "Score: %i", gamer.score);
                al_flip_display();
                al_draw_bitmap(protoboard, 0, 0, 0);
            }

        }
    /*================================================================================================================================================*/
        if(state == GAMEOVER)
        {
            al_wait_for_event(event_queue, &ev);
            al_clear_to_color(al_map_rgb(0,0,0));
            al_draw_text(font18, al_map_rgb(255, 168, 255), WIDTH / 2, HEIGHT / 2, ALLEGRO_ALIGN_CENTRE, "Os Zombies roubaram seu diploma");
            al_draw_text(font18, al_map_rgb(255, 255, 255), WIDTH / 2, HEIGHT / 2 +40, ALLEGRO_ALIGN_CENTRE, "Pressione Space para sair");
            al_draw_textf(font_maior, al_map_rgb(255, 0, 0), WIDTH*13/28, 85, 0, "Score: %i", gamer.score);
            al_flip_display();

            if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
                done = true;

            if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
                if(ev.keyboard.keycode == ALLEGRO_KEY_SPACE)
            {
                al_destroy_event_queue(event_queue);
                al_destroy_timer(timer);
                al_destroy_font(font18);
                al_destroy_bitmap(resistor);
                al_destroy_bitmap(capacitor);
                al_destroy_bitmap(indutor);
                al_destroy_bitmap(diodo);
                al_destroy_bitmap(bateria);
                al_destroy_bitmap(protoboard);
                al_destroy_bitmap(zombie_bitmap);
                al_destroy_display(display);

            }




        }
    }


    return 0;
}