#include <allegro.h>
#include <stdio.h>
#include "objekt.h"
#include "nepriatelia.h"
#include "spravca.h"
#include <iostream>
#include <time.h>
#include <alfont.h>

#define TORPEDO_SPEED 3
#define CENA_TORPEDA 50
#define CENA_NAVNADY 10

#define DENNA_OBLOHA makecol(205,237,255)
#define NOCNA_OBLOHA makecol(9,0,26)
#define VPS 100 //Vypocty Per Second
#define KKF 3 //Kazdy kolkaty vypocet sa ma vykreslit
int kedy_vykresli = 1;

int width=1024;
int height=768;
int volatile ido = 0;
int volatile timer = 0;

void dalsia()
{
  ++ido;
  ++timer;
}

void pockaj(int x)
{
  timer = 0;
  while(timer < x);
}

double vzdialPokus(double x1, double y1, double x2, double y2)
{
  return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}

#define max(a, b)  (((a) > (b)) ? (a) : (b))

using namespace std;

/*
Nakresli ramcek

@param bmp Bitmapa
@param xlt, ylt Suradnice x a y pre lavy horny roh
@param xlr, ylr Suradnice x a y pre pravy dolny roh
@param col Farba (makecol)
*/

void drawBorder(BITMAP* bmp, int xtl, int ytl, int xlr, int ylr, int col) {
	
	line(bmp, xtl, ytl, xlr, ytl, col); // top
	line(bmp, xtl, ylr, xlr, ylr, col); // bottom
	line(bmp, xtl, ytl, xtl, ylr, col); // left
	line(bmp, xlr, ytl, xlr, ylr, col); // right
}

/*
Nakresli progress bar

@param bmp Bitmapa do ktorej kreslit (BITMAP*)
@param x,y Suradnice zaciatku
@param width,height Sirka a vyska
@param start_r, start_g, start_b RGB pre zaciatok (0 - 255)
@param end_r, end_g, end b RGB pre koniec (0 - 255)
@param progress Vyfarbena cast (0 - 100%)
@param border Urcuje ci sa nakresli ramcek (bool)
@param border_col Farba ramceku (makecol)
@param background_col Farba pozadia (makecol)
*/
void drawProgressBar(BITMAP* bmp, int x, int y, int width, int height, int start_r, int start_g, int start_b, int end_r, int end_g, int end_b, int progress, bool border, int border_col, int background_col) {
	
	// pocet "krokov" pri 100%
	
	int steps = width;
	
	if (steps > 255) steps = 255;
	if (steps < 1) steps = 1;
	
	// krok pre RGB
	
	double step_r = max(start_r, end_r) / steps;
	double step_g = max(start_g, end_g) / steps;
	double step_b = max(start_b, end_b) / steps;	
	
	// pricitat ci odcitat? 
	
	bool add_r = (max(start_r, end_r) == end_r) ? TRUE : FALSE;
	bool add_g = (max(start_g, end_g) == end_g) ? TRUE : FALSE;
	bool add_b = (max(start_b, end_b) == end_b) ? TRUE : FALSE;	
	
	// pozadie
	
	rectfill(bmp, x, y, x + width, y + height, background_col);
	
	// cyklusove premenne
	
	int i;
	int stop = (progress * width) / 100;
	
	// docasne pomocne premenne
	
	int sx, ex, sy, ey;
	int r,g,b;
	
	for (i = 0; i < stop; i++) {
		
		// nastavenie zaciatku a konca
		
		sx = i; // start x
		ex = i; // end x
		
		sy = 0; // start y
		ey = height; // end y
		
		// posun
		
		sx = sx + x;
		ex = ex + x;
		
		sy = sy + y;
		ey = ey + y;
		
		// spocitanie RGB
		
		r = (int) step_r * i;
		g = (int) step_g * i;
		b = (int) step_b * i;
		
		// pripociatat (alebo odpocitat) zaciatok 
		
		r = (add_r) ? (r + start_r) : (start_r - r);
		g = (add_g) ? (g + start_g) : (start_g - g);
		b = (add_b) ? (b + start_b) : (start_b - b);
		
		if (i > steps) {
			
			r = end_r;
			g = end_g;
			b = end_b;
			
		}
		
		// nakreslenie
		
		line (bmp, sx, sy, ex, ey, makecol(r, g, b));
		
	}
	
	if (border) {
		
		// ramcek
		
		drawBorder(bmp, x, y, x + width, y + height, border_col);
		
	}
	
}

/*
Nakresli cely status bar (sirka = SCREEN_W, vyska = SREEN_H / 10);

@param bmp Bitmapa kam kreslit
@param health zdravie (0 - 100)
@param reload_navnada, reload_torpedo1-4 Reloading navnady a torpedovych komor
@param pocet_torped, pocet_navnad
@param cas Cas od novej hry (s)
@param cash Peniaze ($)
@param border, border_col Ramcek, farba (makecol)
@param background_col Farba pozadia (makecol);
*/

void drawStatusBar(BITMAP* bmp,ALFONT_FONT *font , int health, int reload_navnada, int reload_torpedo1, int reload_torpedo2, int reload_torpedo3, int reload_torpedo4, int pocet_torped, int pocet_navnad, int cas, int cash, bool border, int border_col, int background_col) {
	
	int sirka = SCREEN_W;
	int vyska = SCREEN_H / 10;
	
	int y = 0;
	
	// sirka & vyska veci // za komentarom je ideal pre 1024*768
	// health
	int health_vyska = (int) (vyska / 3.3); // 23
	int health_hspace = (int) (vyska / 7.6); // 10
	int health_sirka  = (int) (sirka / 3.41); // 300
	int health_wspace = (int) (sirka / 51.2); // 20
	
	// reload torped
	int reloadt_vyska = (int) (vyska / 6.33); // 12
	int reloadt_hspace = (int) (vyska / 7.6); // 10
	int reloadt_sirka = (int) (sirka / 6.83); // 150
	int reloadt_wspace = (int) (sirka / 51.2); // 20
	
	// reload navnady
	int reloadn_vyska = (int) (vyska / 6.33); // 12
	int reloadn_hspace = (int) (vyska / 7.6); // 10
	int reloadn_sirka = (int) (sirka / 5.12); // 200
	int reloadn_wspace = (int) (sirka / 12.8); // 80
	
	// text dole pod health (tct - time cash text)
	int tct_vyska = (int) (vyska / 3.3); // 23
	int tct_hspace = (int) (vyska / 7.6); // 10
	int tct_sirka = (int) (sirka / 3.41); // 300
	int tct_wspace = (int) (sirka / 51.2); // 20
	
	// text v strede (pocet torped a navnad)
	int ptn_vyska = (int) (vyska / 3.16); // 24
	int ptn_hspace = (int) (vyska / 7.6); // 10
	int ptn_sirka = (int) (sirka / 8.26); // 124
	int ptn_wspace = (int) (sirka / 10.24); // 100
	
	// pozadie, ramcek
	
	rectfill(bmp, 0, y, sirka, y + vyska, background_col);
	line(bmp, 0, y, sirka, y, border_col);
	
	// progress bars ...
	
	// health progressbar

	drawProgressBar(bmp, health_wspace, y + health_hspace, health_sirka, health_vyska, 255, 0, 0, 0, 255, 0, health, 1, makecol(255,255,255), makecol(0,0,255));

	// torpedo reload progressbars
	
	drawProgressBar(bmp, sirka - 2*reloadt_sirka - 2*reloadt_wspace, y + reloadt_hspace, reloadt_sirka, reloadt_vyska, 255, 0, 0, 0, 255, 0, reload_torpedo1, 0, 0, makecol(0,0,0));
	drawProgressBar(bmp, sirka - reloadt_sirka - reloadt_wspace, y + reloadt_hspace, reloadt_sirka, reloadt_vyska, 255, 0, 0, 0, 255, 0, reload_torpedo2, 0, 0, makecol(0,0,0));
	drawProgressBar(bmp, sirka - 2*reloadt_sirka - 2*reloadt_wspace, y + 2*reloadt_hspace + reloadt_vyska, reloadt_sirka, reloadt_vyska, 255, 0, 0, 0, 255, 0, reload_torpedo3, 0, 0, makecol(0,0,0));
	drawProgressBar(bmp, sirka - reloadt_sirka - reloadt_wspace, y + 2*reloadt_hspace + reloadt_vyska, reloadt_sirka, reloadt_vyska, 255, 0, 0, 0, 255, 0, reload_torpedo4, 0, 0, makecol(0,0,0));
	
	// navnada reload progressbar
	
	drawProgressBar(bmp, sirka - reloadn_sirka - reloadn_wspace, y + 3*reloadn_hspace + 2*reloadn_vyska, reloadn_sirka, reloadn_vyska, 255, 0, 0, 0, 255, 0, reload_navnada, 0, 0, makecol(0,0,0));
	
	// text
	
	// ramcek na cas a chash
	drawBorder(bmp, tct_wspace, y + vyska - tct_hspace - tct_vyska, tct_wspace + tct_sirka, vyska + y - tct_hspace, makecol(255,255,255));
	
	/*
	Rasto - navod na pridanie textu
	
	* cas a peniaze
	
	suradnice zaciatku a koncia si vypocitas z sirky a vysky (premene tct_...)
	sposob najdes v drawBorder nad tymto komentarom
	udaje su ako parametre int cas (v sekundach) a int cash
	ak budes mat problem s prepoctom sekund na min a sec povedz, spravim ti to
	font, velkost, farba, ... je na tebe
	
	* pocet torped a navnad
	
	suradnice zase cez premenne ptn_...
	sposob pod tymto kometarom
	udaje su ako parametre (pocet_torped a pocet_navnad)
	farba a font zalezi od teba, ale font by sa hodil taky displey-ovy (LED)
	
	keby nieco, povedz
	
	*/
	
	// ramcek na pocet torped a navnad
	
	int sekundy=0;
	int minuty=0;
	sekundy=(cas % 60);
	minuty=((cas-sekundy)/60);
	
	drawBorder(bmp, health_sirka + 2*health_wspace + ptn_wspace, y + ptn_hspace, health_sirka + 2*health_wspace + ptn_wspace + ptn_sirka, y + ptn_hspace + ptn_vyska, makecol(255,255,255));
	drawBorder(bmp, health_sirka + 2*health_wspace + ptn_wspace, vyska + y - ptn_hspace - ptn_vyska, health_sirka + 2*health_wspace + ptn_wspace + ptn_sirka, vyska + y - ptn_hspace, makecol(255,255,255));
	
	alfont_text_mode(-1);
	floodfill(bmp,health_sirka + 2*health_wspace + ptn_wspace + ((ptn_sirka)/2),y + ptn_hspace+1,makecol(0,0,0));
	floodfill(bmp,tct_wspace + ((tct_sirka)/2),y + vyska - tct_hspace - tct_vyska + 1,makecol(0,0,0));
	floodfill(bmp,health_sirka + 2*health_wspace + ptn_wspace + ((ptn_sirka)/2),vyska + y - ptn_hspace - ptn_vyska + 1,makecol(0,0,0));
	
	alfont_textprintf_centre_aa(bmp,font,health_sirka + 2*health_wspace + ptn_wspace + ((ptn_sirka)/2),y + ptn_hspace,makecol(76,176,76),"%d",pocet_torped);
	alfont_textprintf_centre_aa(bmp,font,health_sirka + 2*health_wspace + ptn_wspace + ((ptn_sirka)/2),vyska + y - ptn_hspace - ptn_vyska,makecol(76,176,76),"%d",pocet_navnad);
	alfont_textprintf_centre_aa(bmp,font,tct_wspace + ((tct_sirka)/2),y + vyska - tct_hspace - tct_vyska,makecol(176,76,76),"$ %d \t\t %d:%d",cash,minuty,sekundy);
}
// Briefing
volatile int ms=0;
void briefing_timer() {ms++;}

void briefing(ALFONT_FONT *beznytext,int farba,char text[])
{
  int bezny_size=(int)(height/36.923);
  alfont_set_font_size(beznytext,bezny_size);
  int text_x=(int)(width/1.82),text_y=(int)(height/5.12);
  int font_size=(int)(height/21.333333);
  int pocet_vypisanych=0;
  int r=0,g=0,b=0;
  bool raise=TRUE;
  BITMAP *chuck;
  BITMAP *buffer;
  SAMPLE *beat;
  ALFONT_FONT *copper;
  copper=alfont_load_font("data/fonts/copperlate.ttf");
  alfont_set_font_size(copper,font_size);
  
  beat=load_sample("sfx/briefing/beat.wav");
  
  buffer=create_bitmap(width,height);
  chuck=load_bitmap("gfx/briefing/chuck.tga",NULL);
  
  clear_to_color(buffer,makecol(0,0,0));
  
  install_int(briefing_timer,8);
  play_sample(beat,128,128,1000,TRUE);
  while(!key[KEY_ENTER]&&!key[KEY_ENTER_PAD])
  {
    if(ms!=0)
    {
      if(raise)
      {
        r++;
        g++;
        b++;
      }
      if(!raise)
      {
        r--;
        g--;
        b--;
      }
      if(r>=128) raise=FALSE;
      if(r<=0) raise=TRUE;
      ms=0;
      if(pocet_vypisanych<strlen(text))
        pocet_vypisanych++;
    }
    stretch_sprite(buffer,chuck,(int)(width/12.8),(int)(height/6.4),(int)(width/2.31),(int)(height/1.47));
    for(int loop=0;loop<pocet_vypisanych;loop++)
    {
      char c=text[loop];
      if(c=='\n')
      {
        text_x=(int)(width/1.82);
        text_y+=bezny_size+2;
      }
      else
      {
        if(text_x>=width-(2*bezny_size))
        {
          text_x=(int)(width/1.82);
          text_y+=bezny_size+2;
        }
        alfont_textprintf_aa(buffer,beznytext,text_x,text_y,farba,"%c",c);
        char retazec[2];
        retazec[0]=c;
        retazec[1]='\0';
        int j=alfont_text_length(beznytext,retazec);
        text_x+=j;
      }
    }
    alfont_textout_centre_aa(buffer,copper,"Press ENTER to continue",width/2,(height-(height/10)),makecol(r,g,b));
    blit(buffer,screen,0,0,0,0,width,height);
    text_x=(int)(width/1.82);
    text_y=(int)(height/5.12);
  }
  remove_int(briefing_timer);
  alfont_destroy_font(copper);
  destroy_bitmap(buffer);
  destroy_bitmap(chuck);
  destroy_sample(beat);
}

volatile bool mozme_kupit=TRUE;
void shop_timer()
{
  mozme_kupit=TRUE;
}

// funkcia shop - parametre: 
//    smernik na peniaze ktore hrac ma
//    cena jedneho torpeda
//    cena jednej navnady
//    maximalny pocet torped
//    maximalny pocet navnad
//    smernik na pocet torped
//    smernik na pocet navnad
int shop(int *peniaze,int cena_torpeda,int cena_navnady,int max_torpeda,int max_navnady,int *p_torpeda,int *p_navnady)
{
  int farba_continue=makecol(128,128,128);
  int formal_font_size=(int)(height/36.923076);
  int money_font_size=(int)(height/30);
  bool tp_act,nv_act;
  ALFONT_FONT *formal,*money;
  formal=alfont_load_font("data/fonts/copperlate.ttf");
  money=alfont_load_font("data/fonts/formal.ttf");
  alfont_set_font_size(formal,formal_font_size);
  alfont_set_font_size(money,money_font_size);
  BITMAP *mouse;
  BITMAP *pozadie;
  BITMAP *buffer;
  BITMAP *tp_unsel,*nv_unsel;
  BITMAP *tp_unact,*nv_unact;
  BITMAP *tp_sel,*nv_sel;
  buffer=create_bitmap(width,height);
  pozadie=load_bitmap("gfx/shop/dock.tga",NULL);
  mouse=load_bitmap("gfx/shop/cursor.bmp",NULL);
  tp_unsel=load_bitmap("gfx/shop/torpedo_unselected.tga",NULL);
  tp_unact=load_bitmap("gfx/shop/torpedo_unactive.tga",NULL);
  tp_sel=load_bitmap("gfx/shop/torpedo_selected.tga",NULL);
  nv_unsel=load_bitmap("gfx/shop/navnada_unselected.tga",NULL);
  nv_unact=load_bitmap("gfx/shop/navnada_unactive.tga",NULL);
  nv_sel=load_bitmap("gfx/shop/navnada_selected.tga",NULL);
  set_mouse_sprite(mouse);
  
  stretch_blit(pozadie,buffer,0,0,1280,1024,0,0,width,height);
  rectfill(buffer,(int)(width/6.4),(int)(height/14.222222),(int)(width-(width/6.4)),(int)(height/1.333333),makecol(0,0,0));
  
  bool shop_done=FALSE;
  install_int(shop_timer,1000);
  
  while(!shop_done)
  {
    show_mouse(NULL);
    if(*peniaze<cena_torpeda)
    {
      tp_act=FALSE;
    }
    else
    {
      tp_act=TRUE;
    }
    if(*peniaze<cena_navnady)
    {
      nv_act=FALSE;
    }
    else
    {
      nv_act=TRUE;
    }
    
    if(mouse_b & 1)
    {
      if((mouse_x>=((int)(width/5.333333)) && mouse_x<=(((int)(width/5.333333))+((int)(width/3.2)))) && (mouse_y>=((int)(height/9.142857)) && mouse_y<=(((int)(height/9.142857))+((int)(height/2.56)))))
      {  
        if(mozme_kupit)
        {
          if(tp_act)
          {
            *peniaze-=cena_torpeda;
            *p_torpeda+=1;
            printf("Torpedo kupene\nPocet torped %d\nPeniaze %d\n",*p_torpeda,*peniaze);
            mozme_kupit=FALSE;
          }
        } 
      }
      if((mouse_x>=(width/2) && mouse_x<=((width/2)+((int)(width/3.2)))) && (mouse_y>=((int)(height/9.142857)) && mouse_y<=(((int)(height/9.142857))+((int)(height/2.56)))))
      {  
        if(mozme_kupit)
        {
          if(nv_act)
          {
            *peniaze-=cena_navnady; 
            *p_navnady+=1;
            printf("Navnada kupena\nPocet navnad %d\nPeniaze %d\n",*p_navnady,*peniaze);
            mozme_kupit=FALSE;
          }
        } 
      }
      if((mouse_x>=((int)((width/2)-(width/6.4))) && mouse_x<=((int)((width/2)+(width/6.4)))) && (mouse_y>=((int)((height/1.422222)-(height/34.133333))) && mouse_y<=((int)((height/1.422222)+(height/34.133333)))))
      {
        shop_done=TRUE;
      }
    }
    else
    {
      mozme_kupit=TRUE;
    }
    
    vsync();
    rectfill(buffer,(int)(width/6.4),(int)(height/14.222222),(int)(width-(width/6.4)),(int)(height/1.333333),makecol(0,0,0));
    rect(buffer,(int)((width/2)-(width/6.4)),(int)((height/1.422222)-(height/34.133333)),(int)((width/2)+(width/6.4)),(int)((height/1.422222)+(height/34.133333)),makecol(128,128,128));
    alfont_textout_centre_aa(buffer,formal,"CONTINUE",width/2,(int)(height/1.45),farba_continue);
    
    alfont_textprintf_centre_aa(buffer,money,(int)((width/2)-(width/6.4)),(int)(height/2),makecol(56,176,56),"$ %d",cena_torpeda);
    alfont_textprintf_centre_aa(buffer,money,(int)((width/2)-(width/6.4)),(int)((height/2)+(height/25.6)),makecol(176,128,56),"Current: %d",*p_torpeda);
    alfont_textprintf_centre_aa(buffer,money,(int)((width/2)+(width/6.4)),(int)(height/2),makecol(56,176,56),"$ %d",cena_navnady);
    alfont_textprintf_centre_aa(buffer,money,(int)((width/2)+(width/6.4)),(int)((height/2)+(height/25.6)),makecol(176,128,56),"Current: %d",*p_navnady);
    
    alfont_textprintf_centre_aa(buffer,money,(int)(width/2),(int)((height/2)+(height/25.6)),makecol(56,128,176),"Money: $ %d",*peniaze);
    
    if((mouse_x>=((int)((width/2)-(width/6.4))) && mouse_x<=((int)((width/2)+(width/6.4)))) && (mouse_y>=((int)((height/1.422222)-(height/34.133333))) && mouse_y<=((int)((height/1.422222)+(height/34.133333)))))
    {
      farba_continue=makecol(182,182,182);
    }
    else
    {
      farba_continue=makecol(128,128,128);
    }
    
    if(tp_act)
    {
      if((mouse_x>=((int)(width/5.333333)) && mouse_x<=(((int)(width/5.333333))+((int)(width/3.2)))) && (mouse_y>=((int)(height/9.142857)) && mouse_y<=(((int)(height/9.142857))+((int)(height/2.56)))))
      {  
        stretch_blit(tp_sel,buffer,0,0,400,400,(int)(width/5.333333),(int)(height/9.142857),(int)(width/3.2),(int)(height/2.56));
      }
      else
      {
        stretch_blit(tp_unsel,buffer,0,0,400,400,(int)(width/5.333333),(int)(height/9.142857),(int)(width/3.2),(int)(height/2.56));
      }
    }
    else
    {
      stretch_blit(tp_unact,buffer,0,0,400,400,(int)(width/5.333333),(int)(height/9.142857),(int)(width/3.2),(int)(height/2.56));
    }
  
  
    if(nv_act)
    {
      if((mouse_x>=(width/2) && mouse_x<=((width/2)+((int)(width/3.2)))) && (mouse_y>=((int)(height/9.142857)) && mouse_y<=(((int)(height/9.142857))+((int)(height/2.56)))))
      {  
        stretch_blit(nv_sel,buffer,0,0,400,400,width/2,(int)(height/9.142857),(int)(width/3.2),(int)(height/2.56));
      }
      else
      {
        stretch_blit(nv_unsel,buffer,0,0,400,400,width/2,(int)(height/9.142857),(int)(width/3.2),(int)(height/2.56));
      }
    }
    else
    {
      stretch_blit(nv_unact,buffer,0,0,400,400,width/2,(int)(height/9.142857),(int)(width/3.2),(int)(height/2.56));
    }
      
    show_mouse(buffer);
    blit(buffer,screen,0,0,0,0,width,height);
  }
  remove_int(shop_timer);
  destroy_bitmap(mouse);
  destroy_bitmap(pozadie);
  destroy_bitmap(tp_unsel);
  destroy_bitmap(nv_unsel);
  destroy_bitmap(tp_unact);
  destroy_bitmap(nv_unact);
  destroy_bitmap(tp_sel);
  destroy_bitmap(nv_sel);
  alfont_destroy_font(formal);
  alfont_destroy_font(money);
}

void create_more(BITMAP* more, int p,int q,int obloha)   // funkcia ktora vygeneruje more - treba jej zadat bitmapu, sirku, vysku a farbu oblohy
{
   clear_to_color(more,obloha);
   printf("Creating more....\n");
   int y;
   int sirka=p;
   int vyska=q;
   int scr10=1024/10;
   
   int x=3*scr10;
   
   float r=83;
   float g=144;
   float b=255;
   
   float ch_r=21/(3*((float)scr10));
   float ch_g=127/(3*((float)scr10));
   float ch_b=0;
   
   if(obloha==NOCNA_OBLOHA)
   {
     r=62;
     g=17;
     b=255;
     
     ch_r=62/(3*((float)scr10));
     ch_g=17/(3*((float)scr10));
     ch_b=92/(3*((float)scr10));
   }
   
   for(y=scr10;y<2*scr10;y++)
   {
     line(more,0,y,sirka-1,y,makecol((int)r,(int)g,(int)b));
   }
   printf("First part created\n");
   for(y=2*scr10;y<5*scr10;y++)
   {
     r-=ch_r;
     g-=ch_g;
     b-=ch_b;
     line(more,0,y,sirka-1,y,makecol((int)r,(int)g,(int)b));
   }
   printf("Second part created\n");
   floodfill(more,sirka/2,vyska-1,makecol((int)r,(int)g,(int)b));
   
   if(obloha==NOCNA_OBLOHA)
   {
     printf("Creating stars...\n");
     int i,j;
     for(i=0;i<sirka;i+=4)
     {
       j=rand() % scr10-5;
       putpixel(more,i,j,makecol(255,255,255));
     }
   }
   printf("More sucessfully created!\n");
}

int main()
{ 
  //strat allegra
  allegro_init();
	alfont_init();
  install_keyboard();
  install_mouse();
  install_sound(DIGI_AUTODETECT,MIDI_NONE,NULL);
  set_color_depth(16);
  if (set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, width, height, 0, 0) != 0)
  {
    set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
    allegro_message("Nemozem nastartovat grafiku\n%s\n",allegro_error);
    return 1;
  }
  
  //TOTO JE INICIALIZACIA
  
  srand(time(NULL));
  
  //IDO = CAS
  //VSETKO OHLADOM TIMEROV
  install_int(dalsia,1);
  LOCK_VARIABLE(ido);
  LOCK_VARIABLE(timer);
  LOCK_FUNCTION(dalsia);
  int vypocet = 0;
  ido = 0;
  
  Spravca BigBoss;  //priliehavy nazov toho, co to vsetko riadi
  BigBoss.Chuck.SetHealth(10000); //ZIVOT CHUCKA
  BigBoss.Chuck.SetMax_health(10000); //MAXIMALNY ZIVOT CHUCKA
  BITMAP *more1=create_bitmap(1024,768);   //BITMAPA PRE MORE
  create_more(more1,1024,768,NOCNA_OBLOHA); // vygenerovanie mora
  
  int j;
  int financie=BigBoss.Chuck.GetMoney();
  int torpeda=BigBoss.Chuck.GetTorpedo();
  int navnady=BigBoss.Chuck.GetNavnada();
  
  //TOTO SU DALSIE FONTY
  ALFONT_FONT *status_font;
  ALFONT_FONT *briefing_font;
	status_font=alfont_load_font("data/fonts/formal.ttf");
	briefing_font=alfont_load_font("data/fonts/trebuchet.ttf");
	alfont_set_font_size(status_font,(int)(SCREEN_H/36.923076));
	alfont_set_font_size(briefing_font,(int)(SCREEN_H/36.923076));
	BITMAP *bitmap_status_bar = create_bitmap(SCREEN_W,(int) ((double) SCREEN_H/10));
	
	
	// TU ZACINA 1.LEVEL
	briefing(briefing_font,makecol(176,128,128),"Vitaj na palube!!!\n\nVolam sa Chuck Norris, a ododneska som tvojim\nnadriadenym.Budes plnit moje rozkazy,\na odmenou ti bude skvely pocit\nze si sluzil pod mojim velenim.\n\nZverime ti do ruk najmodernejsiu\ndnesnu ponorku so skusenou posadkou,\na ty nam predvedies, co vies.\n\nTvojou prvou uloho bude znicit\nnepriatelsku ponorku.\n\nVela stastia!");
	
  //SEM MOZES HODIT NEPRIATELOV ATD, TO CO CHES ABY BOLO NA ZACIATKU, MENIT PARAMETRE SNAD VIES, IE VSETKY SU OBSIAHNUTE V PRIDAJ
  //SPRAVCA::PRIDAJ JE ASI 81 RIADKU V SPRAVCA.CPP TAM JE TO DOSTATOCNE OKOMENTOVANE
   
  BigBoss.Pridaj(1,1024,384,0.3,0.3,270,1500,1500,0.2,100);  
  
  //KONIEC INICIALIZACIE
  
  while(1==1)
  {  
    //veci s chuckom
    
    // SEM MOZTE PISAT
    //ako cas radsej pouzivaj vypocet, je to predsa len objektivnejsie, ked to niekomu seka/ide rychlejsie
    //vypocet je pocet vypoctov dosial urobenych
    //pokus si nakopci a men ho ostatne subory prosim ta radsej nie
    //mozesdavat rozne parametre, napriklad ked das nepriatelskemu torpedu maxspeed aj speed 0, tak mas z neho minu
    //este jedna vec, vecsina striel ma premenne prebijanie a reload
    //u ponoriek to je za ako casto strielaju, ale u striel je to vlastne palivo
    //reload je kolko paliva maju a prebijanie je za kolko ho minuly dostanes sa k nim ako objekt->GetPrebijanie() a objekt->GetReload
    //ked im dojde palivo, tak sa zmazu a znicia a zaroven vybuchnu
    //prajem prijemne programovanie, keby si nieco nevedel, tak som ICQ  
    
    
    if(BigBoss.GetZacNepr1()==NULL)
    {
      break;
    }    
    
    //ZATYMTO UZ RADSEJ NIE
    
    //prida jedno torpedo a urobi vestko okolo toho
    if(keypressed())
    {
      if(key[KEY_F] && BigBoss.Chuck.GetTorpedo()>0 && BigBoss.Chuck.GetPrebijanie()>=BigBoss.Chuck.GetReload())
      {
        bool ci_torpedo = FALSE;
        if(BigBoss.Chuck.GetNabijanie(0) == BigBoss.Chuck.GetPrebi(0) && !ci_torpedo)
        {
          BigBoss.Chuck.SetNabijanie(0,0); 
          ci_torpedo = TRUE;            
        }
        if(BigBoss.Chuck.GetNabijanie(1) == BigBoss.Chuck.GetPrebi(1) && !ci_torpedo)
        {
          BigBoss.Chuck.SetNabijanie(1,0); 
          ci_torpedo = TRUE;            
        }
        if(BigBoss.Chuck.GetNabijanie(2) == BigBoss.Chuck.GetPrebi(2) && !ci_torpedo)
        {
          BigBoss.Chuck.SetNabijanie(2,0); 
          ci_torpedo = TRUE;            
        }
        if(BigBoss.Chuck.GetNabijanie(3) == BigBoss.Chuck.GetPrebi(3) && !ci_torpedo)
        {
          BigBoss.Chuck.SetNabijanie(3,0); 
          ci_torpedo = TRUE;            
        }
        if(ci_torpedo)
        {
          BigBoss.Chuck.SetTorpedo(BigBoss.Chuck.GetTorpedo() - 1);
          BigBoss.Pridaj(50,BigBoss.Chuck.GetVx(),BigBoss.Chuck.GetVy(),BigBoss.Chuck.GetSpeed(),BigBoss.Chuck.GetMax_speed()*1.2,BigBoss.Chuck.GetUhol(),1000,1000,0.4,50);            
          BigBoss.Chuck.SetPrebijanie(0);
        }
      }
    //gulomet  
      if(key[KEY_C] && BigBoss.Chuck.GetNabijanie(7)>=BigBoss.Chuck.GetPrebi(7))
      {
        BigBoss.Pridaj(53,BigBoss.Chuck.GetVx(),BigBoss.Chuck.GetVy(),BigBoss.Chuck.GetSpeed(),BigBoss.Chuck.GetMax_speed(),BigBoss.Chuck.GetUhol(),300,300,0,20);            
        BigBoss.Chuck.SetNabijanie(7,0);
      }
    
    //prida jednu navnadu a urobi vsetko okolo toho
      if(key[KEY_X] && BigBoss.Chuck.GetNabijanie(4)>=BigBoss.Chuck.GetPrebi(4) && BigBoss.Chuck.GetNavnada()>0)
      {
        BigBoss.Chuck.SetNabijanie(4,0);
        BigBoss.Chuck.SetNavnada(BigBoss.Chuck.GetNavnada() - 1);
        BigBoss.Pridaj(51,2*BigBoss.Chuck.GetPx()-BigBoss.Chuck.GetVx()-100,2*BigBoss.Chuck.GetPy()-BigBoss.Chuck.GetVy(),BigBoss.Chuck.GetSpeed(),BigBoss.Chuck.GetSpeed(),BigBoss.Chuck.GetUhol(),1500,1500,0.0,1);
      } 
    //RoundHouseKick
      if(key[KEY_R] && BigBoss.Chuck.GetNabijanie(5)>=BigBoss.Chuck.GetPrebi(5))
      {
        BigBoss.Chuck.SetNabijanie(5,0);
        for(j=2;j<20;j++)
          BigBoss.Pridaj(200,BigBoss.Chuck.GetVx()+j*50*sin(BigBoss.Chuck.GetUhol()*pi180),BigBoss.Chuck.GetVy()+j*50*cos(BigBoss.Chuck.GetUhol()*pi180),0,0,0,75,75,0,0);            
      }
    //megabomb do stredu obrazovky  
      if(key[KEY_E] && BigBoss.Chuck.GetNabijanie(6)>=BigBoss.Chuck.GetPrebi(6))
      {
        BigBoss.Chuck.SetNabijanie(6,0);
        BigBoss.Pridaj(200,512,384,0,0,0,400,400,0,0);            
      }
    }
    //prebehne vsetky navnady
    Objekt *ps2 = BigBoss.GetZacStrela2();
    for(;ps2 != NULL;ps2 = ps2->GetDalsia())
    {
      if(ps2->GetHealth() <= 0 || ps2->GetPrebijanie() >= ps2->GetReload()) //vlastne ci zomrel
      {
        BigBoss.Uber(51,ps2);
        break; //takze za jedno kolo vymaze len jedneho !
      }
    }
    //prebehne vsetky nepriatelske torpeda
    ps2 = BigBoss.GetZacStrela3();
    Objekt *pspo = BigBoss.GetZacStrela2();
    for(;ps2 != NULL;ps2 = ps2->GetDalsia())
    {
      if(ps2->GetHealth() <= 0 || ps2->GetPrebijanie() >= ps2->GetReload() ) //vlastne ci zomrel
      {
        BigBoss.Uber(52,ps2);
        break; //takze za jedno kolo vymaze len jedneho !
      }
      for(pspo = BigBoss.GetZacStrela2();pspo!=NULL;pspo = pspo -> GetDalsia())
      {
        if(vzdialPokus(pspo->GetPx(),pspo->GetPy(),ps2->GetPx(),ps2->GetPy())<8)
        {
          Objekt *pomoc1 = ps2;
          Objekt *pomoc2 = pspo;
          BigBoss.Uber(52,pomoc1);
          BigBoss.Uber(51,pomoc2);
          pspo = NULL;
          ps2 = NULL;
        }
        if(pspo == NULL)
          break;
      }
      if(ps2 == NULL)
        break; 
      if(vzdialPokus(BigBoss.Chuck.GetPx()-50,BigBoss.Chuck.GetPy(),ps2->GetPx(),ps2->GetPy())<50)   
        if(vzdialPokus(BigBoss.Chuck.GetPx()-50,BigBoss.Chuck.GetPy(),ps2->GetPx(),ps2->GetPy())<15 || vzdialPokus((BigBoss.Chuck.GetPx()-50 + BigBoss.Chuck.GetVx())/2,(BigBoss.Chuck.GetPy()+ BigBoss.Chuck.GetVy())/2,ps2->GetPx(),ps2->GetPy())<15 || vzdialPokus((3*BigBoss.Chuck.GetPx()-BigBoss.Chuck.GetVx()-150)/2 ,(3*BigBoss.Chuck.GetPy()-BigBoss.Chuck.GetVy())/2,ps2->GetPx(),ps2->GetPy())<15)  //ked narazi nepriatelske torpedo do chucka
      {
          Objekt *pomoc1 = ps2;
          BigBoss.Uber(52,pomoc1);
          ps2 = NULL;
      }  
      if(ps2 == NULL)
        break;  
    }
    
    //prebehne vsetky vybuchy
    ps2 = BigBoss.GetZacVybuch1();
    
    for(;ps2 != NULL;ps2 = ps2->GetDalsia())
    {
      if(ps2->GetHealth() <= 0) //vlastne ci zomrel
      {
        BigBoss.Uber(200,ps2);
        break; //takze za jedno kolo vymaze len jedneho !
      }
    }
    //prebehne vsetkych neppriatelov
    Objekt *pn1 = BigBoss.GetZacNepr1();
    for(;pn1 != NULL;pn1 = pn1->GetDalsia())
    {
      if(pn1->GetHealth() <= 0) //vlastne ci zomrel
      {
        BigBoss.Uber(1,pn1);
        break; //takze za jedno kolo vymaze len jedneho !
      }
      if(pn1->GetPrebijanie() >= pn1->GetReload())
      {
        pn1->SetPrebijanie(0);
        BigBoss.Pridaj(52,pn1->GetPx() + 25*sin(pn1->GetUhol()*pi180), pn1->GetPy() + 25*cos(pn1->GetUhol()*pi180), 0.3,0.3,pn1->GetUhol(),1000,1000,0.5,40);
      }
    }
    //prebehne vsetky torpeda
    Objekt *ps1 = BigBoss.GetZacStrela1();
    pspo = BigBoss.GetZacNepr1();
    for(;ps1 != NULL;ps1 = ps1->GetDalsia())
    {
      if(ps1->GetHealth() <= 0 || ps1->GetPrebijanie() >= ps1->GetReload()) //vlastne ci zomrel
      {
        BigBoss.Uber(50,ps1);
        break; //takze za jedno kolo vymaze len jedneho !
      }
      for(pspo = BigBoss.GetZacNepr1();pspo != NULL;pspo = pspo->GetDalsia()) //prejde vsetkych nepriatelov
      {
        if(vzdialPokus(ps1->GetPx(),ps1->GetPy(),pspo->GetPx(),pspo->GetPy())<18) //ked sa zrazia, tak ich oboch zmazem
        {
          Objekt *pomoc1 = ps1;
          BigBoss.Uber(50,pomoc1);
          pspo = NULL;
          ps1=NULL;
        }
        if(pspo == NULL)
          break;
      }
      if(ps1 == NULL)
        break;            
    }
    
    //vykreslovanie
    if(kedy_vykresli == KKF)
    {
      blit(more1,BigBoss.screen_n,0,0,0,0,1024,768);
    }
    BigBoss.Krok(); //vykona jeden krok
    if(kedy_vykresli == KKF)
    {
      blit(bitmap_status_bar,screen,0,0,0,(int) ((double)(SCREEN_H*9/10)),SCREEN_W,(int) ((double)SCREEN_H/10));
    }
    if(kedy_vykresli == KKF) //vykresli to kazdy KKF-ty frame
    {
      double percento[5];
      for(j=0 ; j<5; j++)
        percento[j] = (double) BigBoss.Chuck.GetNabijanie(j)/BigBoss.Chuck.GetPrebi(j)*100;
      double perc_health = (double) BigBoss.Chuck.GetHealth()/BigBoss.Chuck.GetMax_health()*100;  
      drawStatusBar(bitmap_status_bar, status_font,(int) perc_health, (int) percento[4], (int) percento[0],(int) percento[1],(int) percento[2],(int) percento[3], BigBoss.Chuck.GetTorpedo(), BigBoss.Chuck.GetNavnada(), vypocet / 100, BigBoss.Chuck.GetMoney(), 1, makecol(255,255,0), makecol(128,128,128)); 
      kedy_vykresli = 0;
      switch(SCREEN_W)
      {
        case 1280:
        pockaj(12);
        break;
        case 1024:
        pockaj(15);
        break;
        case 640:
        pockaj(20);
        break;
      }
    }  
    
    kedy_vykresli++;
    vypocet++;
    //koniec cyklu
  }
  briefing(briefing_font,makecol(76,176,76),"VYNIKAJUCE!!!\n\nTeraz ta zavediem do obchodu,\nkde si za utrzene financie nakupis torpeda\na navnady, pretoze vedz, ze vlada ta \nzasobovat nebude.Od nej dostanes\nakurat 100$ za kazdu ponorku,\nktoru znicis.\n\nHor sa nakupovat!!!");
  
  financie=BigBoss.Chuck.GetMoney();
  torpeda=BigBoss.Chuck.GetTorpedo();
  navnady=BigBoss.Chuck.GetNavnada();
  //printf("%d %d %d\n",financie,torpeda,navnady);
  //printf("%d %d %d\n",BigBoss.Chuck.GetMoney(),BigBoss.Chuck.GetTorpedo(),BigBoss.Chuck.GetNavnada());
  shop(&financie,CENA_TORPEDA,CENA_NAVNADY,0,0,&torpeda,&navnady);  
  BigBoss.Chuck.SetMoney(financie);
  BigBoss.Chuck.SetTorpedo(torpeda);
  BigBoss.Chuck.SetNavnada(navnady);
  //printf("%d %d %d\n",financie,torpeda,navnady);
  //printf("%d %d %d\n",BigBoss.Chuck.GetMoney(),BigBoss.Chuck.GetTorpedo(),BigBoss.Chuck.GetNavnada());
  
  // LEVEL 2
  
  briefing(briefing_font,makecol(76,176,76),"Vitam ta na dalsej misii!!!\n\nTvoj minuly vykon bol tak ohromujuci,\nze sa nasi nepriatelia rozhodli\ndostat ta za kazdu cenu.\n\nZ nasich tajnych zdrojov vieme,\nze sa na teba plavi pat-clenna flotila\nroznych druhov ponoriek.\nNiekolko rychlych,slabsich,\nale aj zopar tazko ozbrojenych,\nnalozenych vybusninami.\n\nPozor na ne!!!");
  
  BigBoss.Pridaj(1,2048,100,1.0,1.0,270,1000,1000,0.2,50);
  BigBoss.Pridaj(1,2048,200,0.3,0.3,270,2000,2000,0.2,200);
  BigBoss.Pridaj(1,2048,300,0.5,0.5,270,1500,1500,0.2,100);
  BigBoss.Pridaj(1,2048,400,0.3,0.3,270,2000,2000,0.2,200);
  BigBoss.Pridaj(1,2048,500,1.0,1.0,270,1000,1000,0.2,50);
  
  while(1==1)
  {  
    //veci s chuckom
    
    // SEM MOZTE PISAT
    //ako cas radsej pouzivaj vypocet, je to predsa len objektivnejsie, ked to niekomu seka/ide rychlejsie
    //vypocet je pocet vypoctov dosial urobenych
    //pokus si nakopci a men ho ostatne subory prosim ta radsej nie
    //mozesdavat rozne parametre, napriklad ked das nepriatelskemu torpedu maxspeed aj speed 0, tak mas z neho minu
    //este jedna vec, vecsina striel ma premenne prebijanie a reload
    //u ponoriek to je za ako casto strielaju, ale u striel je to vlastne palivo
    //reload je kolko paliva maju a prebijanie je za kolko ho minuly dostanes sa k nim ako objekt->GetPrebijanie() a objekt->GetReload
    //ked im dojde palivo, tak sa zmazu a znicia a zaroven vybuchnu
    //prajem prijemne programovanie, keby si nieco nevedel, tak som ICQ  
    
    
    if(BigBoss.GetZacNepr1()==NULL)
    {
      break;
    }    
    
    //ZATYMTO UZ RADSEJ NIE
    
    //prida jedno torpedo a urobi vestko okolo toho
    if(keypressed())
    {
      if(key[KEY_F] && BigBoss.Chuck.GetTorpedo()>0 && BigBoss.Chuck.GetPrebijanie()>=BigBoss.Chuck.GetReload())
      {
        bool ci_torpedo = FALSE;
        if(BigBoss.Chuck.GetNabijanie(0) == BigBoss.Chuck.GetPrebi(0) && !ci_torpedo)
        {
          BigBoss.Chuck.SetNabijanie(0,0); 
          ci_torpedo = TRUE;            
        }
        if(BigBoss.Chuck.GetNabijanie(1) == BigBoss.Chuck.GetPrebi(1) && !ci_torpedo)
        {
          BigBoss.Chuck.SetNabijanie(1,0); 
          ci_torpedo = TRUE;            
        }
        if(BigBoss.Chuck.GetNabijanie(2) == BigBoss.Chuck.GetPrebi(2) && !ci_torpedo)
        {
          BigBoss.Chuck.SetNabijanie(2,0); 
          ci_torpedo = TRUE;            
        }
        if(BigBoss.Chuck.GetNabijanie(3) == BigBoss.Chuck.GetPrebi(3) && !ci_torpedo)
        {
          BigBoss.Chuck.SetNabijanie(3,0); 
          ci_torpedo = TRUE;            
        }
        if(ci_torpedo)
        {
          BigBoss.Chuck.SetTorpedo(BigBoss.Chuck.GetTorpedo() - 1);
          BigBoss.Pridaj(50,BigBoss.Chuck.GetVx(),BigBoss.Chuck.GetVy(),BigBoss.Chuck.GetSpeed(),BigBoss.Chuck.GetMax_speed()*1.2,BigBoss.Chuck.GetUhol(),1000,1000,0.4,50);            
          BigBoss.Chuck.SetPrebijanie(0);
        }
      }
    //gulomet  
      if(key[KEY_C] && BigBoss.Chuck.GetNabijanie(7)>=BigBoss.Chuck.GetPrebi(7))
      {
        BigBoss.Pridaj(53,BigBoss.Chuck.GetVx(),BigBoss.Chuck.GetVy(),BigBoss.Chuck.GetSpeed(),BigBoss.Chuck.GetMax_speed(),BigBoss.Chuck.GetUhol(),300,300,0,20);            
        BigBoss.Chuck.SetNabijanie(7,0);
      }
    
    //prida jednu navnadu a urobi vsetko okolo toho
      if(key[KEY_X] && BigBoss.Chuck.GetNabijanie(4)>=BigBoss.Chuck.GetPrebi(4) && BigBoss.Chuck.GetNavnada()>0)
      {
        BigBoss.Chuck.SetNabijanie(4,0);
        BigBoss.Chuck.SetNavnada(BigBoss.Chuck.GetNavnada() - 1);
        BigBoss.Pridaj(51,2*BigBoss.Chuck.GetPx()-BigBoss.Chuck.GetVx()-100,2*BigBoss.Chuck.GetPy()-BigBoss.Chuck.GetVy(),BigBoss.Chuck.GetSpeed(),BigBoss.Chuck.GetSpeed(),BigBoss.Chuck.GetUhol(),1500,1500,0.0,1);
      } 
    //RoundHouseKick
      if(key[KEY_R] && BigBoss.Chuck.GetNabijanie(5)>=BigBoss.Chuck.GetPrebi(5))
      {
        BigBoss.Chuck.SetNabijanie(5,0);
        for(j=2;j<20;j++)
          BigBoss.Pridaj(200,BigBoss.Chuck.GetVx()+j*50*sin(BigBoss.Chuck.GetUhol()*pi180),BigBoss.Chuck.GetVy()+j*50*cos(BigBoss.Chuck.GetUhol()*pi180),0,0,0,75,75,0,0);            
      }
    //megabomb do stredu obrazovky  
      if(key[KEY_E] && BigBoss.Chuck.GetNabijanie(6)>=BigBoss.Chuck.GetPrebi(6))
      {
        BigBoss.Chuck.SetNabijanie(6,0);
        BigBoss.Pridaj(200,512,384,0,0,0,400,400,0,0);            
      }
    }
    //prebehne vsetky navnady
    Objekt *ps2 = BigBoss.GetZacStrela2();
    for(;ps2 != NULL;ps2 = ps2->GetDalsia())
    {
      if(ps2->GetHealth() <= 0 || ps2->GetPrebijanie() >= ps2->GetReload()) //vlastne ci zomrel
      {
        BigBoss.Uber(51,ps2);
        break; //takze za jedno kolo vymaze len jedneho !
      }
    }
    //prebehne vsetky nepriatelske torpeda
    ps2 = BigBoss.GetZacStrela3();
    Objekt *pspo = BigBoss.GetZacStrela2();
    for(;ps2 != NULL;ps2 = ps2->GetDalsia())
    {
      if(ps2->GetHealth() <= 0 || ps2->GetPrebijanie() >= ps2->GetReload() ) //vlastne ci zomrel
      {
        BigBoss.Uber(52,ps2);
        break; //takze za jedno kolo vymaze len jedneho !
      }
      for(pspo = BigBoss.GetZacStrela2();pspo!=NULL;pspo = pspo -> GetDalsia())
      {
        if(vzdialPokus(pspo->GetPx(),pspo->GetPy(),ps2->GetPx(),ps2->GetPy())<8)
        {
          Objekt *pomoc1 = ps2;
          Objekt *pomoc2 = pspo;
          BigBoss.Uber(52,pomoc1);
          BigBoss.Uber(51,pomoc2);
          pspo = NULL;
          ps2 = NULL;
        }
        if(pspo == NULL)
          break;
      }
      if(ps2 == NULL)
        break; 
      if(vzdialPokus(BigBoss.Chuck.GetPx()-50,BigBoss.Chuck.GetPy(),ps2->GetPx(),ps2->GetPy())<50)   
        if(vzdialPokus(BigBoss.Chuck.GetPx()-50,BigBoss.Chuck.GetPy(),ps2->GetPx(),ps2->GetPy())<15 || vzdialPokus((BigBoss.Chuck.GetPx()-50 + BigBoss.Chuck.GetVx())/2,(BigBoss.Chuck.GetPy()+ BigBoss.Chuck.GetVy())/2,ps2->GetPx(),ps2->GetPy())<15 || vzdialPokus((3*BigBoss.Chuck.GetPx()-BigBoss.Chuck.GetVx()-150)/2 ,(3*BigBoss.Chuck.GetPy()-BigBoss.Chuck.GetVy())/2,ps2->GetPx(),ps2->GetPy())<15)  //ked narazi nepriatelske torpedo do chucka
      {
          Objekt *pomoc1 = ps2;
          BigBoss.Uber(52,pomoc1);
          ps2 = NULL;
      }  
      if(ps2 == NULL)
        break;  
    }
    
    //prebehne vsetky vybuchy
    ps2 = BigBoss.GetZacVybuch1();
    
    for(;ps2 != NULL;ps2 = ps2->GetDalsia())
    {
      if(ps2->GetHealth() <= 0) //vlastne ci zomrel
      {
        BigBoss.Uber(200,ps2);
        break; //takze za jedno kolo vymaze len jedneho !
      }
    }
    //prebehne vsetkych neppriatelov
    Objekt *pn1 = BigBoss.GetZacNepr1();
    for(;pn1 != NULL;pn1 = pn1->GetDalsia())
    {
      if(pn1->GetHealth() <= 0) //vlastne ci zomrel
      {
        BigBoss.Uber(1,pn1);
        break; //takze za jedno kolo vymaze len jedneho !
      }
      if(pn1->GetPrebijanie() >= pn1->GetReload())
      {
        pn1->SetPrebijanie(0);
        BigBoss.Pridaj(52,pn1->GetPx() + 25*sin(pn1->GetUhol()*pi180), pn1->GetPy() + 25*cos(pn1->GetUhol()*pi180), 0.3,0.3,pn1->GetUhol(),1000,1000,0.5,40);
      }
    }
    //prebehne vsetky torpeda
    Objekt *ps1 = BigBoss.GetZacStrela1();
    pspo = BigBoss.GetZacNepr1();
    for(;ps1 != NULL;ps1 = ps1->GetDalsia())
    {
      if(ps1->GetHealth() <= 0 || ps1->GetPrebijanie() >= ps1->GetReload()) //vlastne ci zomrel
      {
        BigBoss.Uber(50,ps1);
        break; //takze za jedno kolo vymaze len jedneho !
      }
      for(pspo = BigBoss.GetZacNepr1();pspo != NULL;pspo = pspo->GetDalsia()) //prejde vsetkych nepriatelov
      {
        if(vzdialPokus(ps1->GetPx(),ps1->GetPy(),pspo->GetPx(),pspo->GetPy())<18) //ked sa zrazia, tak ich oboch zmazem
        {
          Objekt *pomoc1 = ps1;
          BigBoss.Uber(50,pomoc1);
          pspo = NULL;
          ps1=NULL;
        }
        if(pspo == NULL)
          break;
      }
      if(ps1 == NULL)
        break;            
    }
    
    //vykreslovanie
    if(kedy_vykresli == KKF)
    {
      blit(more1,BigBoss.screen_n,0,0,0,0,1024,768);
    }
    BigBoss.Krok(); //vykona jeden krok
    if(kedy_vykresli == KKF)
    {
      blit(bitmap_status_bar,screen,0,0,0,(int) ((double)(SCREEN_H*9/10)),SCREEN_W,(int) ((double)SCREEN_H/10));
    }
    if(kedy_vykresli == KKF) //vykresli to kazdy KKF-ty frame
    {
      double percento[5];
      for(j=0 ; j<5; j++)
        percento[j] = (double) BigBoss.Chuck.GetNabijanie(j)/BigBoss.Chuck.GetPrebi(j)*100;
      double perc_health = (double) BigBoss.Chuck.GetHealth()/BigBoss.Chuck.GetMax_health()*100;  
      drawStatusBar(bitmap_status_bar, status_font,(int) perc_health, (int) percento[4], (int) percento[0],(int) percento[1],(int) percento[2],(int) percento[3], BigBoss.Chuck.GetTorpedo(), BigBoss.Chuck.GetNavnada(), vypocet / 100, BigBoss.Chuck.GetMoney(), 1, makecol(255,255,0), makecol(128,128,128)); 
      kedy_vykresli = 0;
      switch(SCREEN_W)
      {
        case 1280:
        pockaj(12);
        break;
        case 1024:
        pockaj(15);
        break;
        case 640:
        pockaj(20);
        break;
      }
    }  
    
    kedy_vykresli++;
    vypocet++;
    //koniec cyklu
  }
  briefing(briefing_font,makecol(76,128,176),"Neskutocne!!!\n\nSkvela praca!!!");
  
  financie=BigBoss.Chuck.GetMoney();
  torpeda=BigBoss.Chuck.GetTorpedo();
  navnady=BigBoss.Chuck.GetNavnada();
  //printf("%d %d %d\n",financie,torpeda,navnady);
  //printf("%d %d %d\n",BigBoss.Chuck.GetMoney(),BigBoss.Chuck.GetTorpedo(),BigBoss.Chuck.GetNavnada());
  shop(&financie,CENA_TORPEDA,CENA_NAVNADY,0,0,&torpeda,&navnady);  
  BigBoss.Chuck.SetMoney(financie);
  BigBoss.Chuck.SetTorpedo(torpeda);
  BigBoss.Chuck.SetNavnada(navnady);
  
  
  printf("P_VYPOCET = %d \nCAS = %d \nCAS_NA_VYPOCET %f \n VYPOCET/SEK = %f\nFPS = %f \nHealth = %d",vypocet,ido,(float) ido/vypocet, (float) vypocet/ido*1000, (float) (vypocet/KKF)/(ido/1000), BigBoss.Chuck.GetHealth());
  destroy_bitmap(more1);
  destroy_bitmap(bitmap_status_bar);
  return EXIT_SUCCESS;
} 
END_OF_MAIN();
 