/*

EDITACE
-------
INS - vlozit udaje pro priseru
SHIFT+INF - precist udaje pro priser
R, F, V - editace udaju prisery (zmenit, +,-)
CTRL+mouse - zmena udaju prisery






to do
------
plasma ball - kdyz vidi zasuvku, tak se do ni zasune a zapne se vypinac (zprovozni se nejakej stroj, dvere, ..)
pri strelbe do neho se da posunovat - vybuch posune vic najednou
pri blizkem dotyku bude prskat blesky a aranovat okolni prisery
bude mit svuj monster_memo, aby ho vsichni obchazeli




stin...
---------
http://www.allegro.cc/forums/thread/586225/599763#target
float light_x,light_y;   // light position
float x1,y1;             // edge start
float x2,y2;             // edge end

float x3 = light_x+(x2-light_x)*10000;
float y3 = light_y+(y2-light_y)*10000;
float x4 = light_x+(x1-light_x)*10000;
float y4 = light_y+(y1-light_y)*10000;

After this, quad (x1,y1) (x2,y2) (x3,y3) (x4,y4) is the shadow volume.

*/

 
//kdyz umre, tak se vsichni co meli active=-1 zmeni na active=-2 
//(budou hledat novyho, kdo ho neuvidi, ten ho nebude poslouchat





//BOSS - pomoci cisla v active si muze boss cestou jak jde hledat koho bude k sobe lakat (kdyz nekoho uvidi s active=0, tak ho zavola at ho jde nasledovat)


//prisera 3 - nemusi se delat zadna logika, pokud neni videt, stejne dela prd (animace jak lita kolem modre prisery)

//osetrit v draw level aby klamera nepresahla povolenej limit - X a Y, to co dela ted kdy zje x<0 a y<0

//kresleni levelu udelat na 4 kroky ? kreslit jednotlivy patra priser, aby kdyz se prolinaji, ab do sebe "zapadali" ?

//in your case, make as few calls to glBegin()/glEnd() as possible
// - udelat pri pohybu WASD aby nemohl ke zdi na vzdalensot sirky (muze ve velke plose se pohybovat libovolne, jinde ale jen jak to dela ted
// - zkusit pohyb otacenim a dopredu/dozadu jako tank??

/*


udelat kazdymu objektu energii, kdyz je u generatora svetla, tak generator sviti a vysava energii z objektu
 - kulka doleti jen omezenou vzdalenost, nez ji to vysaje
 - hrac musi volit cestu ke generatorum, aby se dostal pres podobny mista a nesmi se zastavit


pridelat BOSS boolean a boss_pointer, ze kdyz prisera, ktera NENI boss uvidi svyho bosse, tak pujde k nemu
- hrac kdyz se zapne jako boss, tak si posbira k sobe svoje priserky
 (jak prisera jedou uvidi bosse, tak si ulozi jeho cvislo a bude ho nasledovat az do smrti bosse)
  - hracovy prisery budou chodit s hracem jako jeho parta, kdyz bude treba. (Muze takhle nabirat i novy hrace cestou)
  - protivnici se budou taky drzet ve skupinach kolem nejakyho bosse, ktery tak nejak vlastne budou chranit ..)



kdyz pathfinding najde cislo >200, tak ho bude ignorovat, pokud je prisera moc daleko - da se predpokladat,
ze tam ted nekdo jde, ale ze tam uz nebude, kdyz bude prisera blizko


udelat 4 druhy postav
-------------------------
1 vojaci
   - napadaji hrace a prisery
2 hracova banda
   - ovlada hrac, v zasade utoci automaticky na vojaky a prisery
3 vetrelci/zombici/prisery
   - utoci na vsechno
4 civilisti (TO JE ZATIM PROBLEM, JINAK TED UZ JDE NEOMEZENEJ POCET PROTIVNIKU)
   - neutoci na nic, meli by se i nejakym zpusobem schovavat


kdyz nekdo zemre, tak udela speed znacku o neco vetsi na tom miste, aby kdyz tam zemre vic panaku, aby se tomu priste vyhnuli
(to bude mit efekt jen kdyz bude hrac bojovat proti nejake VELKE presile, ktera poleze nejakyma dverma do mistnosti (a bude vic dveri dostupnych)
*/

#include <allegro.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>


#include "allegro.h"
#include "alleggl.h"
#include "winalleg.h"
#include "almp3.h"


#define random()     rand()
#define srandom(n)   srand(n)

#define level_matrix_x 200
#define level_matrix_y 170

#define steps_to_remember 100
#define max_priser 200
#define max_objects 600
#define max_particles 1000
#define max_nodes 500

#define walkable_ground 10
//#define walkable_ground_diagonal 4
#define walkable_water 20
//#define walkable_water_diagonal 7

#define time_to_lose_target 10

#define mymodel_x 11
#define mymodel_y 11




   



//----- NODE
typedef struct {
   int x,y;//pozice uzlu
   int F,G,H;//pro vyhledavani A*
   int prev;//node na kterej je navazanej
   int typ;//0 nic, 1-open, 2-close
   } data_node;
   data_node nodes[max_nodes+9];//pro jistotu davam vic, nez s cim pak pocitam
   int nodes_used=max_nodes;//kolik uzlu je pouzito (a kolik se ma mazat) - v prvnim pruchodu je to max

//----- PATH
typedef struct {
   int x,y;//pozice uzlu
   } data_path;
   
   
typedef struct {
   int height;// 0 = no height changed, other numbers are 
   int texture;//what texture to use
   unsigned char speed;//10 - normalni, jinak je pro ucel pathfinderu
   int typ;// 0 volno, 1- voda, 8 nejde projit, ale jde prestrelit, 9 a vice - zed
   int node;//------- -1 -neni zadny node, cislo - pozice nodu v poli nodes
   int monster_memo;
   int data1,data2,data3,data4,data5,monster;//kvuli kompilaci
   } data_level;
   data_level level[level_matrix_x+10][level_matrix_y+10];

int shadows[level_matrix_x+10][level_matrix_y+10];


typedef struct {
   int x,y;//pozice v levelu
   int sprite;
   int angle;//uhel kam se diva
   int loading_weapon;//pokud neni nula, tak se snizuje, pokud je nula tak muze strilet
   int typ;//1=start,2=cil,0=nic
   data_path path[steps_to_remember];//pamatuje si 10 kroku co ma udelat
   int cil_x,cil_y;//kam jde (neco slysel, vraci se neco hlidat apod.)
   int cil_vzdalenost;//jak daleko se ma od cile dostat - kace se provadi na cilli primo, utok je na dalku (jen ho musi videt, to obstara program)
   int guard_x,guard_y,guard_angle;//kde zacal - a kam se vrati, pokud uz nic nevidi
   int guard_wait;//pokud nic nevidi, zvetsuje se, jakmile nabyde hodnoty X, tak se vyda na cestu k pozici guard
   int radius;//velikost prisery
   int health;
   char is_monster;
//   int enemy_typ;
   int shield;
   int alerted;//je nula, pokud je prisera v klidu, je cislo pokud pronasleduje cil (prisera[cislo])
   int alerted_time;//jak dlouho pronasleduje - klesa k nule, pokud je nula, tak cil ztratil
   char active;//pokud je cislo, tak to vyjadruje koho ma nasledovat (cislo prisery), boss si takhle muze nachytat vsechny ktery uvidi a nemaji Bosse
   int speed;
   int shockwave_speed_x,shockwave_speed_y;//pro pohyb po narazu
   int shockwave_speed;// rychlost kterou udelil naraz - snizuje se automaticky k nule
   int shockwave_angle;// uhel pohybu po narazu
   
   int weapon;//jakou ma zbran
   int data1,data2,data3,data4;//for some monster-memory use 
   int fov;//(field of view) - how wide the monster is able to see (in angle)
   int fov_sprite;//kdyz je -1 > vypocita se, jinak vyjadruje odpovidajici uhel ale jako obrazek
   int fov_actual;//uhel kterej ma a na kterej se snizuje/zvysuje FOV (podle toho jestli je allerted nebo odpociva)
   int disable_bounce;//kdyz neni nula, ignoruje se odrazeni 
   float zoom;
   } data_monster;
   data_monster prisera[max_priser+10];//pro jistotu davam vic, nez s cim pak pocitam

data_monster prisera_edit;//prisera ktera se edituje
int prisera_edit_actual_variable=0;//polozka ktera se aktualne edituje

typedef struct {
   float x,y;//pozice v levelu
   float speed_x, speed_y;//pokud nejsou nulovy, tak se posunuej timto smerem
   float speed_length;
   int data1,data2,data3,data4,data5;
   int angle;//uhel kam se diva
   int radius;//velikost objektu   
   int typ;//kulka/kour/...
   int power;//u kulek kolik maji vzit hracum (vsem stranam)
   int rotating;//0 - neotaci se, 1 - otaci se +, 2 - otaci se -
   int rotating_angle;//pokud se otaci kolem osy, tak tady
   int sprite;
   int texture;//grafika brana z textur - napr dvere maji texturu ale ne sprite
   int alpha;//s jakou alphou bude kreslenej
   int angle_to_follow;//angle ke kterymu se priblizuje
   int is_on_floor;//je to flek na zemi
   int is_1st_floor;//je to nad zemi, ale pod ostatnima objektama (ikony co se hybou s hracem apod.)   
   int is_on_top;
   float zoom;  //drawing zoom factor
   int blender;
   int homing;//used only for homing bullects/objects - if set, the number is the angle for one step
   } data_object;
   data_object object[max_objects+10];//pro jistotu davam vic, nez s cim pak pocitam
   data_object particle[max_particles+10];//pro jistotu davam vic, nez s cim pak pocitam


/*const order_array_length=max_objects+max_priser+50;

typedef struct {
   int y;
   int typ;
   int number;
   } data_order_array;
   data_order_array order_array[max_objects+max_priser+50];//limit vic nez oba dohromady
/**/

//  BITMAP *sprite_test=NULL;
  GLuint gl_sprite[500],gl_floors[500],gl_textures[500],gl_walls[500],gl_wallmasks[500], gl_particle[500];//200 sprites should be enough
//  GLuint gl_shadow_texture,gl_shadow_texture_temp;
  
   FILE *logfile;
   FONT *myfont=NULL,*myfont_big=NULL;
   int objektu_total;//pocita kolik zobrazil objektu

//pamet klaves, ktera byla minule zmacknuta
char mykey[512];//bylo 128, ale davam vic kvuli momory leaks

//----------screen setting
  int my_screen_width,my_screen_height,my_screen_bpp,windowed,my_screen_rate,real_screen_width,real_screen_height;

//----------interrupt setting
  int intrpt_video,intrpt_game,keyboard_cycler;
//----------
  int enable_log_file,voices_allocated,log_message_no=1;

  int fps,fps_show,paths_in_a_second;
  FONT *agl_font;
  
  int tile_width,tile_width_2;
  int tile_height,tile_height_2;
  int kamera_x,kamera_y;
  int aktualni_hrac;//cislo hrace, ktery je prave k ovladani (vetsinou  je to 1)
  int move_button_enable;//pokud je nula, tak je funkcni, jinak nefunguje
  int mouse_cursor_texture,mouse_cursor_showing;//jakou ma texturu a cislo prisery na kterou ukazuje
  int mouse_cursor_dblclicked,mouse_cursor_dblclick;//prvni je odpocitavani ze se kliklo, druhej pozna ze je doubleclick a mel by se vyhodnotit
  int mouse_holding; //je bud 1 nebo 0, podle toho jestli je mys stisknuta nad hracem, kvuli dvojkliku

void intrpt_video_proc(){intrpt_video=1;keyboard_cycler=1;}
void intrpt_game_proc(){intrpt_game=1;}

int mymouse_x,mymouse_y,real_mouse_x,real_mouse_y;

int show_info;

//only for testing & info
int monster_counter,object_counter,particle_counter;

int actual_blood_splat; // splat texture counter (random is not random enough :)
int quitting;//pokud se nastavi, hra se vypne



int set_log_string(char log[500]) //ulozi text do cfg
{
char text[500];

if (log_message_no>1000){return;}
log_message_no++;//zvetsi cislo
if (enable_log_file)
 {
 sprintf(text,"mes%03i\0",log_message_no);
 push_config_state();
 set_config_file("log_file");
 set_config_string(NULL,text,log);
 pop_config_state();
 }
}


int set_log_int(int cislo) //ulozi text do cfg
{
char text[500];
char log[500];

if (log_message_no>1000){return;}
log_message_no++;//zvetsi cislo
if (enable_log_file)
 {
 sprintf(text,"mes%03i\0",log_message_no);
 push_config_state();
 set_config_file("log_file");
 sprintf(log,"number%i\0",cislo);
 set_config_string(NULL,text,log);
 pop_config_state();
 }
}


int set_log_ext(int cislo1, int cislo2, int cislo3) //ulozi text do cfg
{
char text[500];
char log[500];

if (log_message_no>2000){return;}
log_message_no++;//zvetsi cislo
if (enable_log_file)
 {
 sprintf(text,"mes%03i\0",log_message_no);
 push_config_state();
 set_config_file("log_file");
 sprintf(log,"monster texture %i, position %i,%i\0",cislo1,cislo2,cislo3);
 set_config_string(NULL,text,log);
 pop_config_state();
 }
}




void mydraw(GLuint textura, int x,int y,int r,int g,int b,int alpha,int angle,int blender)
{
objektu_total++;

//Enable texturing on all models for now on.
glLoadIdentity();//reset matrix
glBindTexture(GL_TEXTURE_2D, textura);
//globalni transformace
//glTranslatef(gl_global_x1, gl_global_y1, 0.0);
//glRotatef(gl_global_angle, 0, 0, 1.0);
//glTranslatef(gl_global_x2, gl_global_y2, 0.0);

//Define how alpha blending will work and enable alpha blending.
glEnable(GL_BLEND);
if (blender==0) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);//klasicky
if (blender==1) glBlendFunc(GL_ONE, GL_ONE);//pruhledny veci
if (blender==2) glBlendFunc(GL_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA);//pruhledny veci


glTranslatef(x, y, 0.0);
glTranslatef(256, 256, 0.0);
glRotatef(angle, 0, 0, 1.0);
glTranslatef(-256, -256, 0.0);

glBegin(GL_QUADS);
  //If we set the color to white here, then the textured quad won't be
  //tinted red or half-see-through or something when we draw it based on
  //the last call to glColor*().
  glColor4ub(r, g, b, alpha);
//  glColor4ub(100, 100, 255, 255);

  //Draw our four points, clockwise.
  glTexCoord2f(0, 0); glVertex3f(0,0, 0);
  glTexCoord2f(1, 0); glVertex3f(256, 0, 0); 

  glTexCoord2f(1, 1); glVertex3f(256, 256, 0);
  glTexCoord2f(0, 1); glVertex3f(0, 256 , 0);
glEnd();
glLoadIdentity();
}



void mydraw_model_sprite(GLuint textura, int x,int y,int r,int g,int b,int alpha,int angle,int blender,float zoom)
{
int size,size2;
size=128*zoom;
size2=size/2;
objektu_total++;

if (zoom!=1) {
            x=x-size2+64;
            y=y-size2+64;
            }

//Enable texturing on all models for now on.
glLoadIdentity();//reset matrix
glBindTexture(GL_TEXTURE_2D, textura);
//globalni transformace
//glTranslatef(gl_global_x1, gl_global_y1, 0.0);
//glRotatef(gl_global_angle, 0, 0, 1.0);
//glTranslatef(gl_global_x2, gl_global_y2, 0.0);

//Define how alpha blending will work and enable alpha blending.
glEnable(GL_BLEND);

if (blender==0) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);//klasicky
if (blender==1) glBlendFunc(GL_ONE, GL_ONE);//pruhledny veci
if (blender==2) glBlendFunc(GL_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA);//pruhledny veci
if (blender==3) glBlendFunc(GL_DST_COLOR, GL_ZERO);
if (blender==4) glBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_COLOR);


glTranslatef(x+size2, y+size2, 0.0);
//glScaled(1,1,1);
//glTranslatef(size2, size2, 0.0);
glRotatef(angle, 0, 0, 1.0);
glTranslatef(-size2, -size2, 0.0);

glBegin(GL_QUADS);
  //If we set the color to white here, then the textured quad won't be
  //tinted red or half-see-through or something when we draw it based on
  //the last call to glColor*().
  glColor4ub(r, g, b, alpha);

  //Draw our four points, clockwise.
  glTexCoord2f(0, 0); glVertex3f(0,0, 0);
  glTexCoord2f(1, 0); glVertex3f(size, 0, 0); 
  glTexCoord2f(1, 1); glVertex3f(size, size, 0);
  glTexCoord2f(0, 1); glVertex3f(0, size , 0);/**/
glEnd();
glLoadIdentity();
}


//x,y - kde ma zacit kreslit, x1,y1, ... jak vypada objekt
void mydraw_rect(int x,int y,int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4,int r,int g,int b,int alpha)
{

//Enable texturing on all models for now on.
glLoadIdentity();//reset matrix
glBindTexture(GL_TEXTURE_2D, gl_textures[1]);
//Define how alpha blending will work and enable alpha blending.
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glTranslatef(x, y, 0.0);

//glRotatef(angle, 0, 0, 1.0);

glBegin(GL_QUADS);
  //If we set the color to white here, then the textured quad won't be
  //tinted red or half-see-through or something when we draw it based on
  //the last call to glColor*().
  glColor4ub(r, g, b, alpha);

  //Draw our four points, clockwise.
  glTexCoord2f(0, 0); glVertex3f(x1,y1, 0);
  glTexCoord2f(1, 0); glVertex3f(x2,y2, 0); 
  glTexCoord2f(1, 1); glVertex3f(x3,y3, 0);
  glTexCoord2f(0, 1); glVertex3f(x4,y4, 0);/**/
  
glEnd();
glLoadIdentity();
}



void mydraw_floor(int x,int y,int texture,int r,int g,int b,int alpha)
{

//Enable texturing on all models for now on.
glLoadIdentity();//reset matrix
//glBindTexture(GL_TEXTURE_2D, gl_floors[texture]);
glBindTexture(GL_TEXTURE_2D, gl_floors[texture]);
//Define how alpha blending will work and enable alpha blending.
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glTranslatef(x, y, 0.0);

//glScaled(0.625,0.547,1);
//glRotatef(angle, 0, 0, 1.0);

glBegin(GL_QUADS);
  //If we set the color to white here, then the textured quad won't be
  //tinted red or half-see-through or something when we draw it based on
  //the last call to glColor*().
  glColor4ub(r, g, b, alpha);

  //Draw our four points, clockwise.
  glTexCoord2f(0, 0); glVertex3f(0,0, 0);
  glTexCoord2f(1, 0); glVertex3f(128,0, 0); 
  glTexCoord2f(1, 1); glVertex3f(128,128, 0);
  glTexCoord2f(0, 1); glVertex3f(0,128,0);/**/
  
glEnd();
glLoadIdentity();
}





void mydraw_wall(int x,int y,int texture,int r,int g,int b,int alpha,char is_mask)
{

//Enable texturing on all models for now on.
glLoadIdentity();//reset matrix
if (is_mask==1) 
  glBindTexture(GL_TEXTURE_2D, gl_walls[texture]);
  else
  glBindTexture(GL_TEXTURE_2D, gl_wallmasks[texture]);
  
//Define how alpha blending will work and enable alpha blending.
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glTranslatef(x, y, 0.0);
glTranslatef(-44, -44, 0.0);


//glScaled(0.625,0.547,1);
//glRotatef(angle, 0, 0, 1.0);

glBegin(GL_QUADS);
  //If we set the color to white here, then the textured quad won't be
  //tinted red or half-see-through or something when we draw it based on
  //the last call to glColor*().
  glColor4ub(r, g, b, alpha);

  //Draw our four points, clockwise.
  glTexCoord2f(0, 0); glVertex3f(0,0, 0);
  glTexCoord2f(1, 0); glVertex3f(128,0, 0); 
  glTexCoord2f(1, 1); glVertex3f(128,128, 0);
  glTexCoord2f(0, 1); glVertex3f(0,128,0);/**/
  
glEnd();
glLoadIdentity();
}


//temp jen pro testovani bez textur, neni treba pro hru ...
void mydraw_texture(int my_texture, int x,int y,int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4,int r,int g,int b,int alpha,int angle)
{

//Enable texturing on all models for now on.
glLoadIdentity();//reset matrix
glBindTexture(GL_TEXTURE_2D, gl_textures[my_texture]);
//Define how alpha blending will work and enable alpha blending.
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);


glEnable(GL_BLEND);

int blender=0;
if (blender==0) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);//klasicky
if (blender==1) glBlendFunc(GL_ONE, GL_ONE);//pruhledny veci
if (blender==2) glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR);//pruhledny veci
if (blender==3) glBlendFunc(GL_DST_COLOR, GL_ZERO);
if (blender==4) glBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_COLOR);


glTranslatef(x, y, 0.0);

glRotatef(angle, 0, 0, 1.0);

glBegin(GL_QUADS);
  //If we set the color to white here, then the textured quad won't be
  //tinted red or half-see-through or something when we draw it based on
  //the last call to glColor*().
  glColor4ub(r, g, b, alpha);

  //Draw our four points, clockwise.
  glTexCoord2f(0, 0); glVertex3f(x1,y1, 0);
  glTexCoord2f(1, 0); glVertex3f(x2,y2, 0); 
  glTexCoord2f(1, 1); glVertex3f(x3,y3, 0);
  glTexCoord2f(0, 1); glVertex3f(x4,y4, 0);
glEnd();
glLoadIdentity();
}




void mydraw_line(int x1,int y1,int x2,int y2,int r,int g,int b,int alpha,int linewidth)
{

//Enable texturing on all models for now on.
glLoadIdentity();//reset matrix
glBindTexture(GL_TEXTURE_2D, gl_textures[0]);
//Define how alpha blending will work and enable alpha blending.
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glEnable(GL_BLEND);

int blender=0;
if (blender==0) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);//klasicky
if (blender==1) glBlendFunc(GL_ONE, GL_ONE);//pruhledny veci
if (blender==2) glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR);//pruhledny veci
if (blender==3) glBlendFunc(GL_DST_COLOR, GL_ZERO);
if (blender==4) glBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_COLOR);

glColor4ub(r, g, b,alpha);

glRotatef(0, 0, 0, 1.0);

	glEnable(GL_LINE_SMOOTH);
	// Disable line stippling
	glEnable(GL_LINE_STIPPLE);
	// Set the line width
	glLineWidth(linewidth);

	glBegin(GL_LINE_STRIP);
		glVertex2f(x1, y1);
		glVertex2f(x2, y2);
	glEnd();
	
}




//vezme data z level[x][y]
void draw_tile(int offset_x,int offset_y,int x,int y)
{
int b_r,b_g,b_b,texture;

//je vubec v levelu ? - opkud ne, tak vypadni
if ((x<1)||(x>level_matrix_x)||(y<1)||(y>level_matrix_y)) return;

if (!(((x/3)*3)==x)) return;
if (!(((y/3)*3)==y)) return;

b_r=255;b_g=255;b_b=255;

texture=1;

if (level[x][y].typ==0) {texture=0;}//zeme
if (level[x][y].typ==1) {texture=3;}//voda
//if (level[x][y].typ==8) {b_r=155;b_g=155;b_b=155;texture=1;}//
//if (level[x][y].typ==7) {b_r=255;b_g=155;b_b=155;texture=1;}//

mydraw_floor(offset_x,offset_y,texture, b_r,b_g,b_b,255);

/*
if (level[x][y].monster_memo>0)
 {
  b_r=155;b_g=155;b_b=155;texture=1;
  mydraw_floor(offset_x,offset_y,texture, b_r,b_g,b_b,255);
 }/**/
}




void draw_monster(int f,int grafika)
{
int x,y,h,i,ofsetx,ofsety;
char r,g,b;
int tempx,tempy;


// 0 - nakresli jen stin
// 1 - nakresli jen telo
// 2 - nakresli telo s ocima otocenyma ve smeru

x=prisera[f].x-kamera_x-64;
y=prisera[f].y-kamera_y-64;
if (prisera[f].typ==3)
 {
 tempx=prisera[prisera[f].data1].x-kamera_x;
 tempy=prisera[prisera[f].data1].y-kamera_y;
 }
         
r=255;
g=255;
b=255;

// jen stin a FOV
if (grafika==0) {
                //udelej FOV sprite

                 
                 if (prisera[f].fov_sprite>-1)
                    {
                     //set_log_ext((14+prisera[f].fov_sprite),x,y);
                     mydraw_model_sprite(gl_sprite[(14+prisera[f].fov_sprite)], x, y , 255,255,255, 255,prisera[f].angle,0,prisera[f].zoom);
                     }
                 //set_log_ext((7*prisera[f].sprite)+1,x,y);
                 mydraw_model_sprite(gl_sprite[(7*prisera[f].sprite)+1], x, y , r, g,b,255,0,0,prisera[f].zoom);//stin
       //        if (prisera[f].typ==3)//je to cast modre prisery, udelej mu spojovaci caru
       //         {mydraw_line(x+64,y+64,tempx,tempy,0,0,0,255,24);}
                 }
 
// telo s ocima otacecima
if (grafika==2) {
                 /*
                 if (prisera[f].typ==3)//je to cast modre prisery, udelej mu spojovaci caru
                  {mydraw_line(x+64,y+64,tempx,tempy,18,89,162,255,5);}/**/
                 //set_log_ext((7*prisera[f].sprite),x,y);
                 if (prisera[f].typ<4)//prisery 1-3
                 {
                 mydraw_model_sprite(gl_sprite[(7*prisera[f].sprite)], x, y , r, g,b,255,0,0,prisera[f].zoom);//telo
                 ofsetx = 6 * cos((float)(M_PI*((float)prisera[f].angle-90))/180);
                 ofsety = (6 * sin((float)(M_PI*((float)prisera[f].angle-90))/180))/2;
                 //set_log_ext((7*prisera[f].sprite)+2,x+ofsetx,y+ofsety);
                 mydraw_model_sprite(gl_sprite[(7*prisera[f].sprite)+2], x+ofsetx, y+ofsety , r, g,b,255,0,0,prisera[f].zoom);//oci
                 }
                 
                 if (prisera[f].typ==4)//je to plasma ball
                 {
                 mydraw_model_sprite(gl_sprite[(7*prisera[f].sprite)], x, y , r, g,b,255,prisera[f].data2,0,prisera[f].zoom);//jedna koule
                 mydraw_model_sprite(gl_sprite[(7*prisera[f].sprite)+2], x, y , r, g,b,255,360-prisera[f].data2,0,prisera[f].zoom);//druha koule
                 } 
                }
}



void draw_object(int f)
{
int x,y,h,i,tempx,tempy,temp1;
char r,g,b;



x=object[f].x-kamera_x-64;
y=object[f].y-kamera_y-64;

r=255;
g=255;
b=255;

if (object[f].sprite>0) mydraw_model_sprite(gl_particle[object[f].sprite], x, y , r, g,b,object[f].alpha,object[f].angle+object[f].rotating_angle,object[f].blender,object[f].zoom);
if (object[f].texture>0) mydraw_model_sprite(gl_walls[object[f].texture], x, y , r, g,b,object[f].alpha,object[f].angle+object[f].rotating_angle,object[f].blender,object[f].zoom);
//(dvere maji texture)

if (object[f].typ==8) // je to blesk
 {
 if (object[f].data3>0) // navazuje na bod blesku
  {
   tempx=object[object[f].data3].x-kamera_x;//druhej bod, kam ma smerovat
   tempy=object[object[f].data3].y-kamera_y;//druhej bod, kam ma smerovat                     
  }
  else
  {//navazuje na priseru
   tempx=prisera[object[f].data2].x-kamera_x;//druhej bod, kam ma smerovat
   tempy=prisera[object[f].data2].y-kamera_y;//druhej bod, kam ma smerovat                     
  }
  
 mydraw_line(x+64,y+64,tempx,tempy,255,255,255,object[f].alpha,object[f].data5);
 mydraw_line(x+64,y+64,tempx,tempy,255,255,255,object[f].alpha/4,8);
 
 //udelej malej odnozek z toho blesku
 }

}




void draw_particle(int f)
{
int x,y,h,i,tempx,tempy,temp1;
char r,g,b;


x=particle[f].x-kamera_x-64;
y=particle[f].y-kamera_y-64;

r=255;
g=255;
b=255;

if (particle[f].sprite>0) mydraw_model_sprite(gl_particle[particle[f].sprite], x, y , r, g,b,particle[f].alpha,particle[f].angle+particle[f].rotating_angle,particle[f].blender,particle[f].zoom);

}
















void save_testing_memo(char jmeno[200],int memo)
{
char name[100],string[100];
PACKFILE *soubor;



sprintf(name,"%s.memo\0",jmeno);
if (memo>0) 
   {
   soubor = pack_fopen(name,F_WRITE);
   pack_fwrite(&memo,sizeof(memo),soubor);
   pack_fclose(soubor);     
   }
   else
   {
   delete_file(name);
   }
}/**/


void draw_level()
{
int f,g,h,i,j,pozice_x,pozice_y,kreslena_sirka,kreslena_vyska,mkamera_x_offset,mkamera_y_offset;
int draw_kamera_x,draw_kamera_y;

//save_testing_memo("draw_level error",1);

//----------
kreslena_sirka=my_screen_width/tile_width+3;
kreslena_vyska=my_screen_height/tile_height+2;

/*
if (kamera_x<tile_width_2) kamera_x=tile_width_2;
if (kamera_y<tile_height_2) kamera_y=tile_height_2;/**/

if (kamera_x<(tile_width*3)) kamera_x=tile_width*3;
if (kamera_y<(tile_height*3)) kamera_y=tile_height*3;/**/

draw_kamera_x=kamera_x;
draw_kamera_y=kamera_y;

/*
set_log_string("pos");
set_log_int(draw_kamera_x);
set_log_int(draw_kamera_y);
/**/

pozice_x=draw_kamera_x/tile_width;
pozice_y=draw_kamera_y/tile_height;
if (pozice_x<3) pozice_x=3;
if (pozice_y<3) pozice_y=3;


mkamera_x_offset=draw_kamera_x%tile_width;
mkamera_y_offset=draw_kamera_y%tile_height;

//save_testing_memo("draw_level1",1);

//###nakresli podlahy
for (g=-2;g<kreslena_vyska;g++)
for (f=-3;f<kreslena_sirka;f++)
{
//set_log_ext(0,pozice_x+f,pozice_y+g);
draw_tile(f*tile_width-mkamera_x_offset,(g*tile_height)-mkamera_y_offset,pozice_x+f,pozice_y+g);
}

//save_testing_memo("draw_level1",0);
//save_testing_memo("draw_level2",1);

//###nakresli prisery a objekty
 //-- kresleni objektu a particlu  - JEN TY CO SOU NA ZEMI
 for (g=0;g<max_particles;g++)
 {
 if ((particle[g].typ>0) && (particle[g].is_on_floor==1) 
     && ((particle[g].x-kamera_x+128)>0) && ((particle[g].x-kamera_x-128)<my_screen_width)
     && ((particle[g].y-kamera_y+128)>0) && ((particle[g].y-kamera_y-128)<my_screen_height))
   {
   draw_particle(g); 
   } 
 }
//save_testing_memo("draw_level2",0);

 //-- kresleni stinu priser
 for (g=0;g<max_priser;g++)
 {
 if ((prisera[g].typ>0) 
     && ((prisera[g].x-kamera_x+128)>0) && ((prisera[g].x-kamera_x-128)<my_screen_width)
     && ((prisera[g].y-kamera_y+128)>0) && ((prisera[g].y-kamera_y-128)<my_screen_height))
   {
   draw_monster(g,0); //stiny
   }
     
 }
 
 

 //-- kresleni objektu a particlu  - JEN TY CO SOU NAD ZEMI v prvni vrstve
 for (g=0;g<max_objects;g++)
 {
 if ((object[g].typ>0) && ((object[g].is_on_floor==1) || (object[g].is_1st_floor==1))
     && ((object[g].x-kamera_x+128)>0) && ((object[g].x-kamera_x-128)<my_screen_width)
     && ((object[g].y-kamera_y+128)>0) && ((object[g].y-kamera_y-128)<my_screen_height))
   {
   draw_object(g); 
   }
     
 }



 for (g=0;g<max_particles;g++)
 {
 if ((particle[g].typ>0) && (particle[g].is_1st_floor==1) 
     && ((particle[g].x-kamera_x+128)>0) && ((particle[g].x-kamera_x-128)<my_screen_width)
     && ((particle[g].y-kamera_y+128)>0) && ((particle[g].y-kamera_y-128)<my_screen_height))
   {
   draw_particle(g); 
   }
     
 } 


 //-- kresleni objektu a particlu  - JEN TY CO NEJSOU NA ZEMI
 for (g=0;g<max_objects;g++)
 {
 if ((object[g].typ>0) && (object[g].is_on_floor==0) && (object[g].is_1st_floor==0)
     && ((object[g].x-kamera_x+128)>0) && ((object[g].x-kamera_x-128)<my_screen_width)
     && ((object[g].y-kamera_y+128)>0) && ((object[g].y-kamera_y-128)<my_screen_height))
   {
   draw_object(g); 
   }
     
 }


//particle co jsou uplne nad

 for (g=0;g<max_particles;g++)
 {
 if ((particle[g].typ>0) && (particle[g].is_on_floor==0) && (particle[g].is_1st_floor==0)
     && ((particle[g].x-kamera_x+128)>0) && ((particle[g].x-kamera_x-128)<my_screen_width)
     && ((particle[g].y-kamera_y+128)>0) && ((particle[g].y-kamera_y-128)<my_screen_height))
   {
   draw_particle(g); 
   }
 }


//-- kresleni priser - nepouziti order_array

 for (g=0;g<max_priser;g++)
 {
//y=prisera[f].y-kamera_y-tile_height;
 if ((prisera[g].typ>0) 
     && ((prisera[g].x-kamera_x+128)>0) && ((prisera[g].x-kamera_x-128)<my_screen_width)
     && ((prisera[g].y-kamera_y+128)>0) && ((prisera[g].y-kamera_y-128)<my_screen_height))
   {
   draw_monster(g,2);//telo a oci

   //nakresli blbosti kolem - mozno nasledne zrusit
             if (show_info==1)
             {
               allegro_gl_printf(agl_font, prisera[g].x-draw_kamera_x,prisera[g].y-draw_kamera_y, 0, makecol(225, 255, 255), "%i",prisera[g].health);
               allegro_gl_printf(agl_font, prisera[g].x-draw_kamera_x,prisera[g].y-draw_kamera_y+14, 0, makecol(225, 255, 255), "%i",prisera[g].alerted);
               //ma nejaky nody ?
               i=1;
               
               if (prisera[g].path[i].x>0)
               {
                do{

                   mydraw_line(
                   prisera[g].path[i-1].x-kamera_x,prisera[g].path[i-1].y-kamera_y,
                   prisera[g].path[i].x-kamera_x,prisera[g].path[i].y-kamera_y, 255,255,255,15,20.0);/**/
                   
                   mydraw_line(
                   prisera[g].path[i-1].x-kamera_x,prisera[g].path[i-1].y-kamera_y,
                   prisera[g].path[i].x-kamera_x,prisera[g].path[i].y-kamera_y, 255,255,255,75,10.0);/**/                   

                   mydraw_line(
                   prisera[g].path[i-1].x-kamera_x,prisera[g].path[i-1].y-kamera_y,
                   prisera[g].path[i].x-kamera_x,prisera[g].path[i].y-kamera_y, 255,255,255,255,3.0);/**/
                   
                   
                   
                  // mydraw_rect(prisera[g].path[i].x-draw_kamera_x,prisera[g].path[i].y-draw_kamera_y,-5,-5,5,-5,5,5,-5,5,0,0,0,110);
                   i++;
                  }while(prisera[g].path[i].x>0);
               } 
             }
   }
     
 }


 for (g=0;g<max_particles;g++)
 {
 if ((particle[g].typ>0) && (particle[g].is_on_top==1) 
     && ((particle[g].x-kamera_x+128)>0) && ((particle[g].x-kamera_x-128)<my_screen_width)
     && ((particle[g].y-kamera_y+128)>0) && ((particle[g].y-kamera_y-128)<my_screen_height))
   {
   draw_particle(g); 
   }
 } 


 
//nakresli masky zdi

for (g=-1;g<kreslena_vyska;g++)
{ 
 for (f=-2;f<kreslena_sirka;f++)
 {
 if (level[pozice_x+f][pozice_y+g].typ==9)
  {

  if (((pozice_x+f)/2*2)==(pozice_x+f))
   {
  if (((pozice_y+g)/2*2)==(pozice_y+g))
  mydraw_wall(f*tile_width-mkamera_x_offset,(g*tile_height)-mkamera_y_offset,4, 255,255,255,255,0);
else
  mydraw_wall(f*tile_width-mkamera_x_offset,(g*tile_height)-mkamera_y_offset,6, 255,255,255,255,0);   
   }
   else
   {
  if (((pozice_y+g)/2*2)==(pozice_y+g))
  mydraw_wall(f*tile_width-mkamera_x_offset,(g*tile_height)-mkamera_y_offset,5, 255,255,255,255,0);
else
  mydraw_wall(f*tile_width-mkamera_x_offset,(g*tile_height)-mkamera_y_offset,7, 255,255,255,255,0);
   }/**/
 //  mydraw_wall(f*tile_width-mkamera_x_offset,(g*tile_height)-mkamera_y_offset,0, 255,255,255,255,0);
  }
 }
}
/**/

//nakresli zdi
for (g=-1;g<kreslena_vyska;g++)
{ 
 for (f=-2;f<kreslena_sirka;f++)
 {
 if (level[pozice_x+f][pozice_y+g].typ==9)
  {
  
  if (((pozice_x+f)/2*2)==(pozice_x+f))
   {
  if (((pozice_y+g)/2*2)==(pozice_y+g))
  mydraw_wall(f*tile_width-mkamera_x_offset,(g*tile_height)-mkamera_y_offset,4, 255,255,255,255,1);
else
  mydraw_wall(f*tile_width-mkamera_x_offset,(g*tile_height)-mkamera_y_offset,6, 255,255,255,255,1);   
   }
   else
   {
  if (((pozice_y+g)/2*2)==(pozice_y+g))
  mydraw_wall(f*tile_width-mkamera_x_offset,(g*tile_height)-mkamera_y_offset,5, 255,255,255,255,1);
else
  mydraw_wall(f*tile_width-mkamera_x_offset,(g*tile_height)-mkamera_y_offset,7, 255,255,255,255,1);        
   }
  /**/                                        
//  mydraw_wall(f*tile_width-mkamera_x_offset,(g*tile_height)-mkamera_y_offset,0, 255,255,255,255,1);
  }
 }
}



 //nakresli cisla priser v levelu pro kompilaci
if (show_info==1)
{
 for (g=0;g<kreslena_vyska;g++)
 { 
  for (f=0;f<kreslena_sirka;f++)
  if (level[pozice_x+f][pozice_y+g].monster>0)
  {
  allegro_gl_printf(agl_font, (f*tile_width-mkamera_x_offset)+10,(g*tile_height)-mkamera_y_offset, 0, makecol(225, 255, 255), "%i",level[pozice_x+f][pozice_y+g].monster);  

  allegro_gl_printf(agl_font, (f*tile_width-mkamera_x_offset)+10,(g*tile_height)-mkamera_y_offset+9, 0, makecol(195, 195, 195), "%i",level[pozice_x+f][pozice_y+g].data1);  
  allegro_gl_printf(agl_font, (f*tile_width-mkamera_x_offset)+30,(g*tile_height)-mkamera_y_offset+9, 0, makecol(175, 175, 175), "%i",level[pozice_x+f][pozice_y+g].data2);  

  allegro_gl_printf(agl_font, (f*tile_width-mkamera_x_offset)+10,(g*tile_height)-mkamera_y_offset+18, 0, makecol(150, 150, 150), "%i",level[pozice_x+f][pozice_y+g].data3);  
  allegro_gl_printf(agl_font, (f*tile_width-mkamera_x_offset)+30,(g*tile_height)-mkamera_y_offset+18, 0, makecol(130, 130, 130), "%i",level[pozice_x+f][pozice_y+g].data4);  

  allegro_gl_printf(agl_font, (f*tile_width-mkamera_x_offset)+10,(g*tile_height)-mkamera_y_offset+26, 0, makecol(100, 100, 100), "%i",level[pozice_x+f][pozice_y+g].data5);  
  }
 }
} 


//mydraw_texture(0, 0,0, 0,0 , 800,0, 800,600, 0,600, 255,255,255, 50, 0);

//stiny
/*
for (g=-1;g<kreslena_vyska;g++)
{ 
 for (f=-2;f<kreslena_sirka;f++)
 {
 h=255-shadows[pozice_x+f][pozice_y+g];

 if (h<128)
  {
 // mydraw_wall(f*tile_width-mkamera_x_offset,(g*tile_height)-mkamera_y_offset,28, 255,255,255,h/2,1);
 }
//else
  {
  mydraw_wall(f*tile_width-mkamera_x_offset,(g*tile_height)-mkamera_y_offset,29, 255,255,255,h,1);
  }
 }
}
/**/

//###kurzor mysi
mydraw_texture(mouse_cursor_texture, mymouse_x-64,mymouse_y-64, 0,0 , 128,0, 128,128, 0,128, 255,255,255, 255, 0);
//save_testing_memo("draw_level error",0);
}




void light_source(x,y)
{
int f,g,h,delka,angle,light_step;
float step_x,step_y,ff,gg;


delka=20;
light_step=255/delka;


 for (angle=0;angle<360;angle+=5)
 {
 step_x = tile_width_2 * cos((float)(M_PI*((float)angle))/180);
 step_y = tile_height_2 * sin((float)(M_PI*((float)angle))/180);

 gg=x;ff=y;

   for (f=0;f<delka;f++)
   {
   gg=gg+step_x;
   ff=ff+step_y;
   h=255-(light_step*f);
if (shadows[(int)gg/tile_width][(int)ff/tile_height]<h) shadows[(int)gg/tile_width][(int)ff/tile_height]++;
//   shadows[(int)gg/tile_width][(int)ff/tile_height]=;
   
   if (shadows[(int)gg/tile_width][(int)ff/tile_height]>255) {shadows[(int)gg/tile_width][(int)ff/tile_height]=255;}
   if (level[(int)gg/tile_width][(int)ff/tile_height].typ>7) {f=delka+1;}
   }
     
 }

}

void generate_shadows()
{
int f,g,h,i,j,pozice_x,pozice_y,kreslena_sirka,kreslena_vyska,mkamera_x_offset,mkamera_y_offset;
int draw_kamera_x,draw_kamera_y;


return;


//----------
kreslena_sirka=my_screen_width/tile_width+1;
kreslena_vyska=my_screen_height/tile_height+2;

if (kamera_x<tile_width_2) kamera_x=tile_width_2;
if (kamera_y<tile_height_2) kamera_y=tile_height_2;/**/

draw_kamera_x=kamera_x;
draw_kamera_y=kamera_y;
/*
if (kamera_x<0) draw_kamera_x=0;
if (kamera_y<0) draw_kamera_y=0;/**/


pozice_x=draw_kamera_x/tile_width;
pozice_y=draw_kamera_y/tile_height;
mkamera_x_offset=draw_kamera_x%tile_width;
mkamera_y_offset=draw_kamera_y%tile_height;
//###nakresli podlahy
for (g=0;g<kreslena_vyska;g++)
for (f=0;f<kreslena_sirka;f++)
{
if (shadows[pozice_x+f][pozice_y+g]>0) shadows[pozice_x+f][pozice_y+g]--;
}


 for (g=0;g<max_priser;g++)
 {
 if ((prisera[g].typ==1) // >0
     && ((prisera[g].x-kamera_x+128)>0) && ((prisera[g].x-kamera_x-128)<my_screen_width)
     && ((prisera[g].y-kamera_y+128)>0) && ((prisera[g].y-kamera_y-128)<my_screen_height))
   {
//shadows[(prisera[g].x-draw_kamera_x)/tile_width][(prisera[g].y-draw_kamera_y)/tile_height]=0;

//   light_source((prisera[g].x)/tile_width,(prisera[g].y)/tile_height);
   light_source((prisera[g].x),(prisera[g].y));
   }
     
 }     
     
}

void vykresli_video()
{
int f,g,h,i,j;
objektu_total=0;



//vymaz screen a nakresli zdi a prisery
mydraw_rect(0,0, 0,0, my_screen_width,0, my_screen_width,my_screen_height, 0,my_screen_height, 0,0,0,255);
draw_level();

//udelej cvicnej model
//do_test_model();

//mydraw_line(prisera[1].x-kamera_x,prisera[1].y-kamera_y,0,0, 155,155,255,155,10.0);

//### zobraz temp data a spus
if (show_info==1)
{
allegro_gl_printf(agl_font, 10, 10, 0, makecol(255, 255, 255), " %i fps - %i,%i - paths %i, angle %i",fps_show,kamera_x,kamera_y,paths_in_a_second,prisera[1].angle);

allegro_gl_printf(agl_font, 10, 40, 0, makecol(255, 255, 255), " M:%i O:%i P:%i",monster_counter,object_counter,particle_counter);

f=255;
if (prisera_edit_actual_variable==0) {f=255;} else {f=200;}
allegro_gl_printf(agl_font, 10, 80, 0, makecol(f, f, f), "typ:%i",prisera_edit.typ);
if (prisera_edit_actual_variable==1) {f=255;} else {f=200;}
allegro_gl_printf(agl_font, 10, 90, 0, makecol(f, f, f), "data1:%i",prisera_edit.data1);
if (prisera_edit_actual_variable==2) {f=255;} else {f=200;}
allegro_gl_printf(agl_font, 10, 100, 0, makecol(f, f, f), "data2:%i",prisera_edit.data2);
if (prisera_edit_actual_variable==3) {f=255;} else {f=200;}
allegro_gl_printf(agl_font, 10, 110, 0, makecol(f, f, f), "data3:%i",prisera_edit.data3);
if (prisera_edit_actual_variable==4) {f=255;} else {f=200;}
allegro_gl_printf(agl_font, 10, 120, 0, makecol(f, f, f), "data4:%i",prisera_edit.data4);
}



allegro_gl_flip();

}

//uprav pruchodnost kostek v levelu 
void generuj_pruchodnost()
{
int f,g;

for (f=0;f<level_matrix_x;f++)
 for (g=0;g<level_matrix_y;g++)
  {
  if (level[f][g].typ==0) level[f][g].speed=walkable_ground;
  if (level[f][g].typ==1) level[f][g].speed=walkable_water;
  }
}



void compile_level()
{
int f,g;
aktualni_hrac=-1;//neni definovanej

for (f=0;f<level_matrix_x;f++)
 for (g=0;g<level_matrix_y;g++)
  { 
   level[f][g].monster_memo=0;
  }
  
for (f=0;f<(max_particles+1);f++)
    {
    particle[f].sprite=0;  
    particle[f].typ=0;  
    }
for (f=0;f<(max_objects+1);f++)    
    {
    object[f].sprite=0;  
    object[f].typ=0;
    object[f].texture=0; 
    }
    
for (f=0;f<(max_priser+1);f++)
    {
    prisera[f].sprite=0;  
    prisera[f].typ=0;     
    prisera[f].x=0;
    prisera[f].y=0;
    prisera[f].angle=0;
    prisera[f].fov=180;
    prisera[f].fov_sprite=-1;    
    prisera[f].fov_actual=0;
    prisera[f].loading_weapon=0;
    prisera[f].cil_x=0;
    prisera[f].cil_y=0;
    prisera[f].path[0].x=0;
    prisera[f].path[0].y=0;   
    prisera[f].guard_x=0;
    prisera[f].guard_y=0;
    prisera[f].guard_angle=0;
    prisera[f].guard_wait=0;
    prisera[f].radius=0;
    prisera[f].health=0;
    prisera[f].alerted=0;
    prisera[f].alerted_time=0;
    prisera[f].active=0;
    prisera[f].speed=0;
    prisera[f].shockwave_angle=0;
    prisera[f].shockwave_speed=0;
    prisera[f].shockwave_speed_x=0;
    prisera[f].shockwave_speed_y=0;
    prisera[f].is_monster=0;
    prisera[f].path[0].x=0;
    prisera[f].path[0].y=0;    
    }
    
    
    
for (f=0;f<level_matrix_x;f++)
 for (g=0;g<level_matrix_y;g++)
  {

//1 prisera 1
if (level[f][g].monster==1)
 summon_monster(level[f][g].monster,f*tile_width,g*tile_height,0,level[f][g].data1,level[f][g].data2,level[f][g].data3,level[f][g].data4,level[f][g].data5);

//2 prisera 2
if (level[f][g].monster==2)
 summon_monster(level[f][g].monster,f*tile_width,g*tile_height,0,level[f][g].data1,level[f][g].data2,level[f][g].data3,level[f][g].data4,level[f][g].data5);


//3 dvere 
if (level[f][g].monster==3)
  {
  summon_object(2,f*tile_width,g*tile_height,0,level[f][g].data1,level[f][g].data2,level[f][g].data3,level[f][g].data4,level[f][g].data5);
  //xdata3;//textura
  //xdata4;//Styl - 1/0
  }

//4 plasma ball
if (level[f][g].monster==4)
 summon_monster(level[f][g].monster,f*tile_width,g*tile_height,0,level[f][g].data1,level[f][g].data2,level[f][g].data3,level[f][g].data4,level[f][g].data5);


  }
  
  
if (aktualni_hrac==-1)//jeste porad neni definovanej ?
 {
  //posli at vygeneruje hlavni hrace
  summon_monster(1,700,550,0,1,0,0,0,0,0);
 }

prisera[aktualni_hrac].shield=30;
}






unsigned char save_level(int cislo)
{
int f,g;
char name[100],string[100];
PACKFILE *soubor;

//vymaz vsechny dvere co sou pruchozi..
for (f=0;f<level_matrix_x;f++)
 for (g=0;g<level_matrix_y;g++)
  { 
   if (level[f][g].typ==7)level[f][g].typ=0;
   level[f][g].monster_memo=0;
  }


//uloz level do level.dat
sprintf(name,"levels/%i/level.dat",cislo);
soubor = pack_fopen(name,F_WRITE_PACKED);

pack_fwrite(&level,sizeof(level),soubor);

pack_fclose(soubor);
}



void load_level(int cislo)
{
int f,g;
char name[100],string[100];
PACKFILE *soubor;


sprintf(name,"levels/%i/level.dat",cislo);
if (exists(name)) {} else {return;} //kdyz ho nenajde, tak nic ... necha prazdnej level


set_log_string(".......naravam mapu z level.dat");

sprintf(name,"levels/%i/level.dat",cislo);
soubor = pack_fopen(name,F_READ_PACKED);

pack_fread(&level,sizeof(level),soubor);
pack_fclose(soubor);
generuj_pruchodnost();
}


void generate_level()
{    
int f,g;  


for (f=0;f<(max_particles+1);f++)
    {
    particle[f].sprite=0;  
    particle[f].typ=0;  
    }
for (f=0;f<(max_objects+1);f++)    
    {
    object[f].sprite=0;  
    object[f].typ=0;  
    }
    
for (f=0;f<(max_priser+1);f++)
    {
    prisera[f].sprite=0;  
    prisera[f].typ=0;     
    prisera[f].x=0;
    prisera[f].y=0;
    prisera[f].angle=0;
    prisera[f].fov=180;
    prisera[f].fov_sprite=-1;    
    prisera[f].fov_actual=0;
    prisera[f].loading_weapon=0;
    prisera[f].cil_x=0;
    prisera[f].cil_y=0;
    prisera[f].path[0].x=0;
    prisera[f].path[0].y=0;   
    prisera[f].guard_x=0;
    prisera[f].guard_y=0;
    prisera[f].guard_angle=0;
    prisera[f].guard_wait=0;
    prisera[f].radius=0;
    prisera[f].health=0;
    prisera[f].alerted=0;
    prisera[f].alerted_time=0;
    prisera[f].active=0;
    prisera[f].speed=0;
    prisera[f].is_monster=0;
    }
    
//vycisti celej level
for (f=0;f<level_matrix_x;f++)
 for (g=0;g<level_matrix_y;g++)
  {
  level[f][g].typ=0;
  level[f][g].monster_memo=0;
  level[f][g].speed=walkable_ground;
  level[f][g].monster_memo=0;
  level[f][g].monster=0;
  level[f][g].node=0;
  }

//pridej zdi kolem levelu

for (g=0;g<(12);g++)
for (f=0;f<(level_matrix_x-1);f++)
{level[f][g].typ=9;level[f][level_matrix_y-g].typ=9;}

for (g=0;g<(15);g++)
for (f=0;f<(level_matrix_y-1);f++)
{level[g][f].typ=9;level[level_matrix_y-g][f].typ=9;}



//summon_object(2,260,580,0,0,0,14,0);//dvere (textura je od 15)
//summon_object(2,260,300,0,0,0,21,1);//dvere (textura je od 15)


//zdi
/**/

generuj_pruchodnost();

}




//hleda mezi dvema body
int find_path(int kdo_jde, int x1, int y1,int x2, int y2)
{
int open_list_length,closed_list_length,node_current;
int temp_F,temp_G,temp_H,temp_prev,steps_done;
int roh,f,g,h;




paths_in_a_second++;

//pocet open a close nodu
open_list_length=0;
closed_list_length=0;

//vycisti level.node (poznaky kde se neco delalo)
for(f=0;f<(level_matrix_x-1);f++)
for(g=0;g<(level_matrix_y-1);g++)
level[f][g].node=-1;
/**/
//vycisti stary nody
for (f=0;f<nodes_used;f++) {nodes[f].typ=0;} //   typ  ----  0-nic, 1-open, 2-close


//### Put node_start on the open list 
nodes[0].typ=1;
nodes[0].x=x1;nodes[0].y=y1;
nodes[0].F=0;nodes[0].G=0;nodes[0].H=0;
open_list_length=1;
level[x1][y1].node=0;//nulty NODE

//### while the OPEN list is not empty 
steps_done=0;
do
{
//### Get the node off the open list with the lowest f and call it node_current
g=open_list_length+closed_list_length+1;//celkovy pocet vlozenych uzlu, bude hledat s nejmensim F
//pokud zacina, tak at zacne na nule, jinak od prvniho nodu (start node se neresi)
//najdi prvni kterej ma typ=1
//for(f=0;f<g;f++) {if (nodes[f].typ==1) {node_current=f;break;}}
f=-1;do{f++;}while(nodes[f].typ!=1);node_current=f;/**/

for(f=node_current;f<g;f++) {if ((nodes[f].F<nodes[node_current].F)&&(nodes[f].typ==1)) {node_current=f;}}

//### if node_current is the same state as node_goal we have found the solution; break from the while loop 
if ((nodes[node_current].x==x2)&&(nodes[node_current].y==y2))
    {
    nodes[node_current].typ=2;//dej do CLOSED - uz se nasel posledni
    
    //uloz cestu zpatky
    g=0;//bude pocitat kroky zpatky
    h=node_current;
    do {
        g++;
        h=nodes[h].prev;
       } while (h!=0);//delej dokud nenajde startovni pozici
    
    h=node_current;
    f=g;//pocet kolik udela kroku
    g=0;
    if (f<steps_to_remember){
                         prisera[kdo_jde].path[f].x=0;//nula je ze tam neni cesta - da to na posledni, aby nepresel cil (jen pro jistotu)
                         prisera[kdo_jde].path[f].y=0;
                         }
                         else
                         {
                         prisera[kdo_jde].path[steps_to_remember-1].x=0;
                         prisera[kdo_jde].path[steps_to_remember-1].y=0;
                         }
    do {
        g++;
        if ((f-g)<steps_to_remember) { //zapise jen tolik nodu, kolik se da zapsat
                             prisera[kdo_jde].path[f-g].x=nodes[h].x*tile_width+tile_width_2;
                             prisera[kdo_jde].path[f-g].y=nodes[h].y*tile_height+tile_height_2;
                             }
        h=nodes[h].prev;       
       } while (h!=0);//delej dokud nenajde startovni pozici
       /**/
//pro pristi mazani
//    nodes_used=steps_done+1;
    nodes_used=open_list_length+closed_list_length+2;

    return(g);}//NASEL CIL - nutno jeste zpetne projit
    
        
//### Generate each state node_successor that can come after node_current  
//### for each node_successor of node_current  

//are there nodes free to create ?
if ((open_list_length+closed_list_length+9)<max_nodes)
for (f=(nodes[node_current].x-1);f<(nodes[node_current].x+2);f++)
for (g=(nodes[node_current].y-1);g<(nodes[node_current].y+2);g++)
{
if ((level[f][g].typ<8)&&(!((f==nodes[node_current].x)&&(g==nodes[node_current].y)))) //neni to zed a neni to ten puvodni node
 {
                                                                                       
//test jestli to nejsou spatny rohy                                                                                       
roh=0;//0 = neni problem, pokracuj
if ((nodes[node_current].x-1)==f) // leva strana
    {
    //levej horni roh
    if ((nodes[node_current].y-1)==g) {if (!((level[f+1][g].typ<8)&&(level[f][g+1].typ<8))) {roh=1;}}
    //levej dolni roh
    if ((nodes[node_current].y+1)==g) {if (!((level[f+1][g].typ<8)&&(level[f][g-1].typ<8))) {roh=1;}}
    }
if ((nodes[node_current].x+1)==f) // prava strana
    {
    //pravej horni roh
    if ((nodes[node_current].y-1)==g) {if (!((level[f-1][g].typ<8)&&(level[f][g+1].typ<8))) {roh=1;}}
    //pravej doni roh
    if ((nodes[node_current].y+1)==g) {if (!((level[f-1][g].typ<8)&&(level[f][g-1].typ<8))) {roh=1;}}
    }                                 
                                                                                       
//roh=0;
if (roh==0)                                                                                       
{                                                                                       
                                                                                       
    //-------------------------------------------------------------------------
    //vypocitej F,G,H pro novy budouci node
//  temp_G=nodes[node_current].G+10;//vezmi predchozi hodnotu a pridej 10
/*
    if (level[nodes[node_current].x][nodes[node_current].y].typ==0)
        {temp_G=nodes[node_current].G+walkable_ground;}
        else
        {temp_G=nodes[node_current].G+walkable_water;}
/**/
    
    temp_G=nodes[node_current].G+level[nodes[node_current].x][nodes[node_current].y].speed;
    if (level[nodes[node_current].x][nodes[node_current].y].monster_memo!=0)
      {temp_G+=250;}

    //byl diagonalni posun ?
    if ((f!=nodes[node_current].x)&&(g!=nodes[node_current].y))
      {
       temp_G+=(level[nodes[node_current].x][nodes[node_current].y].speed/2)-1;//pridej 4, aby byl zisk 14 (diagonal)   
       //vyres kdy jde diagonalne nekam kde uz nekdo stoji
       }      
    temp_H=(abs(f - x2)+abs(g - y2))*10;//vzdalenost ctvercova
    temp_F=temp_G+temp_H;
    //-------------------------------------------------------------------------

//node je prazdnej, udelej ho uplne novej a pridej do OPEN listu
if (level[f][g].node==-1)
   {
    //udelej novej node
    open_list_length++;
    h=open_list_length+closed_list_length;// novy nodu
    nodes[h].typ=1;//dej do open
    nodes[h].x=f;nodes[h].y=g;//pozice x,y
    nodes[h].H=temp_H;
    nodes[h].G=temp_G;
    nodes[h].F=temp_F;
    nodes[h].prev=node_current;   
    level[f][g].node=h;
   }                                                         
    else
    {
//        ??? - kdyz uz je pouzitej, tak ?
//### if node_successor is on the OPEN list but the existing one is as good or better then discard this successor and continue  
if (nodes[level[f][g].node].typ==1)//je v open
     {
     //pokud je ten novej lepsi, tak tento node znic, jinak nic nedelej
     if (temp_F<nodes[level[f][g].node].F)
           {
           h=level[f][g].node;// novy nodu
           nodes[h].typ=1;//dej do open
           nodes[h].x=f;nodes[h].y=g;//pozice x,y
           nodes[h].H=temp_H;
           nodes[h].G=temp_G;
           nodes[h].F=temp_F;
           nodes[h].prev=node_current;
           level[f][g].node=h;
           }
     }
     else
//### if node_successor is on the CLOSED list but the existing one is as good or better then discard this successor and continue  
     {
     //pokud je ten novej lepsi, tak tento znic, jinak nic nedelej
     if (temp_F<nodes[level[f][g].node].F)
           {
           h=level[f][g].node;// novy nodu
           nodes[h].x=f;nodes[h].y=g;//pozice x,y
           nodes[h].H=temp_H;
           nodes[h].G=temp_G;
           nodes[h].F=temp_F;
           level[f][g].node=h;
           nodes[h].prev=node_current;           
           }     
     }
//### Remove occurences of node_successor from OPEN and CLOSED  
// ??? co udelat tady ?        
    }                                                      
  }//test rohu
 }
}
//### Add node_current to the CLOSED list 
if (nodes[node_current].typ==1)
  {
  nodes[node_current].typ=2;
  open_list_length--;closed_list_length++;
  }
  
steps_done++;  
} 
while(open_list_length>0);

//---- nevi kam ma jit ! :( --- 
prisera[kdo_jde].path[0].x=0;//nula je ze tam neni cesta
prisera[kdo_jde].path[0].y=0;

//---
nodes_used=open_list_length+closed_list_length+2;//krajni verze, nutno zmenit, aby to nemusel mazat pokazdy cely


return(0);//nenasel cestu
}



//rekne, jestli se objekty prekryvaji, vrati 1 pokud ano, 0 pokud ne
int konflikt_objekt_prisera(int cislo_prisera,int cislo_objekt)
{
int f,g,prisera_radius;


//jiny radius, pokud ma stit, pak kulka exploduje ve vetsi vzdalenosti od prisery
prisera_radius=prisera[cislo_prisera].radius;
if (prisera[cislo_prisera].shield>0) {prisera_radius=40;}


f=((abs(prisera[cislo_prisera].x-object[cislo_objekt].x))*(abs(prisera[cislo_prisera].x-object[cislo_objekt].x)))
  +
  ((abs(prisera[cislo_prisera].y-object[cislo_objekt].y))*(abs(prisera[cislo_prisera].y-object[cislo_objekt].y)));
g=(prisera_radius+object[cislo_objekt].radius)*(prisera_radius+object[cislo_objekt].radius);

if (f<g) {return(1);}
    
return(0);
}



int konflikt_prisera_prisera(int cislo_objekt,int cislo_prisera) // cislo_objekt je cislo prvni prisery
{
int f,g,prisera_radius;

//jiny radius, pokud ma stit, pak kulka exploduje ve vetsi vzdalenosti od prisery
prisera_radius=prisera[cislo_prisera].radius;
if (prisera[cislo_prisera].shield>0) {prisera_radius=40;}

f=((abs(prisera[cislo_prisera].x-prisera[cislo_objekt].x))*(abs(prisera[cislo_prisera].x-prisera[cislo_objekt].x)))
  +
  ((abs(prisera[cislo_prisera].y-prisera[cislo_objekt].y))*(abs(prisera[cislo_prisera].y-prisera[cislo_objekt].y)));
g=(prisera_radius+prisera[cislo_objekt].radius)*(prisera_radius+prisera[cislo_objekt].radius);

if (f<g) {return(1);}
    
return(0);
}



// vrati uhel ve stupnich, kolik stupnu je z x1,y1 do x2,y2
int prisera_uhel(x1,y1,x2,y2)
{
int x,y;
unsigned int angle;

 
 if ((y1-y2)<0) 
   {
    angle = (atan2(y2 - y1, x2 - x1)*180)/M_PI;
   }
   else
   {
    angle = ( (atan2(y1 - y2, x1 - x2)*180)/M_PI)+180;
   }   
angle=angle+90;

return(angle);
}




//test, jestli p1 vidi p2, tak vrati -1, jinak cislo uhlu, kterej sviraji
int prisera_vidi(p1,p2_x,p2_y)
{
return vidi(prisera[p1].x,prisera[p1].y,p2_x,p2_y,1); //jednicka je ze nevidi skrz dvere, nula je ze vidi)
}

int vidi(priserap1x,priserap1y,p2_x,p2_y,skrzdvere)
{
int f,g,h,a,b,dx,dy,temp;



//kdyz je moc daleko, tak to kasli
if ((abs(priserap1x-p2_x)>500)||(abs(priserap1y-p2_y)>500)) {return(-1);}



a=priserap1x;
b=priserap1y;

//pocet kroku, ktery udela
dx=abs(priserap1x-p2_x); // vzdalenost horizontalni
dy=abs(priserap1y-p2_y); // vzdalenost vertikalni

// udela x kroku smerem k cili a proveri, jestli tam jde projit
dx=dx/50;//krok x
dy=dy/50;//krok y
f=0;
do
{
//posun se smerem k cili
if (priserap1x<p2_x) {a+=dx;} else {a-=dx;}
if (priserap1y<p2_y) {b+=dy;} else {b-=dy;}
f++;
temp=level[a/tile_width][b/tile_height].typ;
if (((temp==7)&&(skrzdvere==1))||(temp>8)) {return(-1);} //nevidi skrz dvere ani skrz zed
}
while(f<50);


//vidi ho..
return(1);
}
















int prisera_najdi_viditelnej_cil_pro_kulku(int kdo,int allied)//allied = ten kdo to vystrelil, resp ten na koho to nefunguje
{
int f,temp,tempfov;
int vidi_priseru=-1;//nevidi nikoho
int cesta=0;//nejblizsi vzdalenost k prisere kterou nasel
int cesta_temp;


cesta=9999999;//pocatecni nejmensi cesta (kazda mensi je lepsi)

for (f=0;f<max_priser;f++)
 {
 if ((prisera[f].typ!=allied)&&(prisera[f].typ>0)&&(prisera[f].is_monster==1))//nasel priseru, kterou hledal
   {

   //zjisti jestli ho vidi (uhel)
   //uhel kterej sviraji pohled prisery a uhel pod kterou je prisera 

//   tempfov=abs(object[kdo].angle-prisera_uhel(object[kdo].x,object[kdo].y,prisera[f].x,prisera[f].y));
//   if (tempfov>180) {tempfov=360-180;}// temp je uhel pod kterym ho prisera vidi
//   if (360<tempfov) {continue;}//ignoruj ho
                    /**/                                               
  
   if (vidi(object[kdo].x,object[kdo].y,prisera[f].x,prisera[f].y,1)==1)
    {
    cesta_temp=(abs(prisera[f].x-object[kdo].x)*abs(prisera[f].x-object[kdo].x)) + (abs(prisera[f].y-object[kdo].y)*abs(prisera[f].y-object[kdo].y));
    if ((vidi_priseru==-1)||(cesta>cesta_temp))//zjisti, jestli nasel lepsi cestu (nebo zatim prvni prisera co vyhovuje)
        {
         cesta=cesta_temp;
         vidi_priseru=f;
         }
     }
   }
 }
 
if (vidi_priseru>0)
   {
   return(vidi_priseru);
   }

return(0);// vrti ze nic nenasel
}



//najdi nejblizsiho co vidi --- kdo= cislo v poli prisera (kdo hleda), koho = koho hleda (typ => prisera/hrac) , vrati cislo toho koho prisera vidi jako nejblizsiho
int prisera_najdi_viditelnej_cil(int kdo)
{
int f,temp,tempfov;
int vidi=-1;//nevidi nikoho
int cesta=0;//nejblizsi vzdalenost k prisere kterou nasel
int cesta_temp;


cesta=9999999;//pocatecni nejmensi cesta (kazda mensi je lepsi)

for (f=0;f<max_priser;f++)
 {
 if ((prisera[f].typ!=prisera[kdo].typ)&&(prisera[f].typ>0)&&(prisera[f].is_monster==1))//nasel priseru, kterou hledal
   {

   //zjisti jestli ho vidi (uhel)
   //uhel kterej sviraji pohled prisery a uhel pod kterou je prisera 

   tempfov=abs(prisera[kdo].angle-prisera_uhel(prisera[kdo].x,prisera[kdo].y,prisera[f].x,prisera[f].y));
   if (tempfov>180) {tempfov=360-180;}// temp je uhel pod kterym ho prisera vidi
   if ((prisera[kdo].fov/2)<tempfov) {continue;}//ignoruj ho
                    /**/                                               
   //zjisti vzdalenost
   if (prisera_vidi(kdo,prisera[f].x,prisera[f].y)==1)//vidi ho, tak spocitej vzdalenost
    {
    cesta_temp=(abs(prisera[f].x-prisera[kdo].x)*abs(prisera[f].x-prisera[kdo].x)) + (abs(prisera[f].y-prisera[kdo].y)*abs(prisera[f].y-prisera[kdo].y));
    if ((vidi==-1)||(cesta>cesta_temp))//zjisti, jestli nasel lepsi cestu (nebo zatim prvni prisera co vyhovuje)
        {
         cesta=cesta_temp;
         vidi=f;
         prisera[kdo].alerted=vidi;
         prisera[kdo].alerted_time=time_to_lose_target;
         }//zapise se, ze tohle je zatim nejlepsi volba 
     }
   }

// neni to stejna / je to stejnej typ              / ta druha nekoho sleduje / a tahle nesleduje nikoho / a zatim nic jinyho nenasel
 if ((f!=kdo)&&(prisera[f].typ==prisera[kdo].typ)&&(prisera[f].alerted_time>0)&&(prisera[kdo].alerted==0)&&(prisera[f].alerted>0)&&(vidi==-1))
   {
   if (prisera_vidi(kdo,prisera[f].x,prisera[f].y)==1)//vidi ho
      {
      if (vidi==-1)//pokud zatim nikoho nenasel, tak vezmi aspon tohodle
         {
         vidi=prisera[f].alerted;
         prisera[kdo].alerted=vidi;
         prisera[kdo].alerted_time=prisera[f].alerted_time;
         }//nastavi se na pronasledovani prisery, kterou pronasleduej ta co nasel
      }
   }/**/
 }
 
if (vidi>0)
   {
   prisera[kdo].alerted=vidi;
   return(vidi);
   }
//---- mrkni jestli nevidi aspon kulky od nekud ..   

for (f=0;f<max_objects;f++)
 {
   if (object[f].power>0) // je to kulka (ma nejakou silu)
     {
     if (prisera_vidi(kdo,object[f].x,object[f].y)==1)//vidi ten objekt
         {
         if ((prisera[object[f].data2].typ!=prisera[kdo].typ)&&(prisera[object[f].data2].typ>0)&&(prisera[object[f].data2].is_monster==1))//je to nepritel
                {
                prisera[kdo].alerted=object[f].data2;//kdo to vystrelil - pronasleduj
                prisera[kdo].alerted_time=time_to_lose_target;
                return(object[f].data2);
                }
         }
     }    
 }/**/
//konec hlidani viditelnosti kulek

return(vidi);// vrati cislo prisery, kterou nasel, ze je nejlepsi    
}



int summon_monster(int typ,int x,int y,int angle,int xdata1,int xdata2,int xdata3, int xdata4, int xdata5)
{
int f,g;
int tmp1,tmp2,tmp3,tmp4,tmp5;
//najdi volny misto a vloz tam novej objekt
f=1;
for (g=1;g<max_priser;g++)
 {
  if ((g>(max_priser-2))||(prisera[g].typ==0)) {f=g;break;} // zaroven aby nepretekl
 }
 
//pokud nenasel, zkus najit nejakej particle, kterej bys pouzil
if ((max_priser-1)==g)
  {
    f=max_priser-5;//proste tam nekoho premaz ..
  }
/// dej priseru na tuto pozici

//vymaz priseru ktera bude vznikat
  
 prisera[f].x=0;
 prisera[f].y=0;
 prisera[f].sprite=0;
 prisera[f].angle=0;
 prisera[f].loading_weapon=0;
 prisera[f].typ=0;
 prisera[f].cil_x=0;
 prisera[f].cil_y=0;
 prisera[f].cil_vzdalenost=0;
 prisera[f].guard_x=0;
 prisera[f].guard_y=0;
 prisera[f].guard_angle=0;
 prisera[f].guard_wait=0;
 prisera[f].radius=0;
 prisera[f].health=0;
 prisera[f].is_monster=0;
 prisera[f].shield=0;
 prisera[f].alerted=0;
 prisera[f].alerted_time=0;
 prisera[f].active=0;
 prisera[f].speed=0;
 prisera[f].weapon=0;
 prisera[f].data1=0;
 prisera[f].data2=0;
 prisera[f].data3=0;
 prisera[f].data4=0;
 prisera[f].fov=0;
 prisera[f].fov_sprite=-1;
 prisera[f].zoom=1;
 prisera[f].fov_actual=0;
 prisera[f].disable_bounce=0;

 
 
 if (typ==1)//je to pixelova prisera
  {
  prisera[f].typ=1;
  prisera[f].x=x/tile_width*tile_width+tile_width_2;
  prisera[f].y=y/tile_height*tile_height+tile_height_2;
  prisera[f].active=-1;
  prisera[f].health=4;
  prisera[f].radius=12;
  prisera[f].speed=3;
  prisera[f].sprite=0;
  prisera[f].weapon=1;
  prisera[f].is_monster=1;
  if (xdata1==1)//je to aktualni hrac ?
    {
     summon_particle(6,0,0,0,0,30,0,0);//kurzor pod aktualniho hrace
     aktualni_hrac=f;
    }
  }
  

  
 if (typ==2)//je to bublinova prisera
  {
  //udelej podruzny bublinky a navaz je na tuto

  tmp1=summon_monster(3,0,0,0,0,0,0,0,0);
  tmp2=summon_monster(3,0,0,0,0,0,0,0,0);
  tmp3=summon_monster(3,0,0,0,0,0,0,0,0);
  tmp4=summon_monster(3,0,0,0,0,0,0,0,0);
  tmp5=summon_monster(3,0,0,0,0,0,0,0,0);
  // konec bublinek
  f=tmp5;
  prisera[tmp1].data1=f;
  prisera[tmp2].data1=f;
  prisera[tmp3].data1=f;
  prisera[tmp4].data1=f;/**/
  prisera[f].typ=2;
  prisera[f].x=x/tile_width*tile_width+tile_width_2;
  prisera[f].y=y/tile_height*tile_height+tile_height_2;
  prisera[f].health=4;
  prisera[f].radius=12;
  prisera[f].speed=3;
  prisera[f].sprite=3;
  prisera[f].weapon=1;
  prisera[f].is_monster=1;
  if (xdata1==1)//je to aktualni hrac ?
    {
     summon_particle(6,0,0,0,0,30,0,0);//kurzor pod aktualniho hrace
     aktualni_hrac=f;
    }
  } 
    
if (typ==3)//kousek kulate prisery
  {
  prisera[f].x=x/tile_width*tile_width+tile_width_2;
  prisera[f].y=y/tile_height*tile_height+tile_height_2;
  prisera[f].typ=3;//
  prisera[f].health=9;
  prisera[f].radius=14;
  prisera[f].sprite=4;
  prisera[f].data1=0;
  prisera[f].fov_sprite=-1;           
  prisera[f].disable_bounce=1;
  prisera[f].is_monster=0;
  }
  
if (typ==4)//plasma ball
  {
  prisera[f].x=x/tile_width*tile_width+tile_width_2;
  prisera[f].y=y/tile_height*tile_height+tile_height_2;
  prisera[f].typ=4;
  prisera[f].health=9;
  prisera[f].radius=14;
  prisera[f].sprite=5;
  prisera[f].shockwave_angle=xdata1;
  prisera[f].shockwave_speed=xdata2;
  prisera[f].fov_sprite=-1;
  }

return (f);
}




int summon_object(int typ,int x,int y,int angle,int xdata1,int xdata2,int xdata3, int xdata4)
{
int f,g;
//najdi volny misto a vloz tam novej objekt
f=1;
for (g=0;g<max_objects;g++)
 {
  if ((g>(max_objects-2))||(object[g].typ==0)) {f=g;break;} // zaroven aby nepretekl
 }
 
//pokud nenasel, zkus najit nejakej particle, kterej bys pouzil
if ((max_objects-1)==g)
  {
    for (g=0;g<max_objects;g++)
     {
     if ((g>(max_objects-2))||(object[g].typ==3)||(object[g].typ==2)) {f=g;break;} // zaroven aby nepretekl
     }
   }
 
/// dej objekt na tuto pozici

//vymaz objekt, ktery bdue vznikat
 object[f].x=0; object[f].y=0; object[f].sprite=0; object[f].speed_x=0; object[f].speed_y=0;
 object[f].speed_length=0; object[f].angle=0;
 object[f].radius=0;object[f].alpha=255; object[f].rotating=0; object[f].rotating_angle=0;
 object[f].is_on_floor=0;object[f].blender=0;object[f].is_1st_floor=0;
 object[f].data1=0;object[f].data2=0;object[f].data3=0;object[f].data4=0;
 object[f].texture=0; object[f].zoom=1;object[f].homing=0;
 
if (typ==1)//je to kulka
  {
  object[f].sprite=2;
  object[f].typ=1;
  object[f].x=x;
  object[f].y=y;
  object[f].speed_x=0;
  object[f].speed_y=0;
  object[f].speed_length=8;//rychlost kulky
  object[f].angle=angle;  
  object[f].power=1;
  object[f].radius=8;
  object[f].data1=xdata1;//koho NEma kulka zabit
  object[f].data2=xdata2;//ktera prisera PRESNE ji vystrelila (aby se mohlo utocit na toho kdo strili)
  object[f].rotating=30;//otaci se automaticky
  }  
  
if (typ==2)//jsou to dvere
  {
  object[f].sprite=0;//nema sprite
  object[f].typ=2;
  object[f].x=x/tile_width*tile_width+tile_width_2;
  object[f].y=y/tile_height*tile_height+tile_height_2;
  object[f].speed_x=0;
  object[f].speed_y=0;
  object[f].angle=angle;
  object[f].data1=xdata1;//informace jeslti je otevreno nebo zavreno
  object[f].data2=xdata2;//pocitani pro animaci
  object[f].data3=xdata3;//textura
  object[f].data4=xdata4;//Styl - 1/0
  }  
  
  
if (typ==3)//je to letici sip
  {
  object[f].sprite=4;
  object[f].typ=3;
  object[f].x=x;
  object[f].y=y;
  object[f].speed_x=0;
  object[f].speed_y=0;
  object[f].speed_length=16;//rychlost kulky
  object[f].angle=angle;  
  object[f].power=1;
  object[f].radius=8;
  object[f].data1=xdata1;//koho NEma kulka zabit
  object[f].data2=xdata2;//ktera prisera PRESNE ji vystrelila (aby se mohlo utocit na toho kdo strili)
  } 

 if (typ==8)//je to kus blesku
  {
  object[f].sprite=0;
  object[f].typ=8;
  object[f].x=x;
  object[f].y=y;
  object[f].power=1;
  object[f].radius=8;
  object[f].angle=angle;   
  object[f].data1=xdata1;//koho NEma kulka zabit
  object[f].data2=xdata2;//ktera prisera PRESNE ji vystrelila (aby se mohlo utocit na toho kdo strili)
  object[f].data3=xdata3;//cislo objektu na kterej navazuje (pokud je nula, tak je navazanej na xdata2 priseru)
  object[f].data4=4;//jak dlouho bude videt
  object[f].data5=xdata4;//sirka toho blesku
  } 
  
    
 if (typ==9)//je to vybuch (spritovej)
  {
  object[f].sprite=36;
  object[f].typ=9;
  object[f].x=x;
  object[f].y=y;
  object[f].power=1;
  object[f].radius=30;
  object[f].angle=angle;
  object[f].alpha=255;
  object[f].data1=xdata1;//koho NEma kulka zabit - nula = zabije vsechny
  object[f].data2=xdata2;//ktera prisera PRESNE ji vystrelila - nevi se
  object[f].data3=2;//kolik kroku do zmeny textury
  object[f].data4=2;//aktualni pocitadlo zpomaleni
  object[f].data5=12;//kolik kroku do zruseni spritu
  } 
  
if (typ==10)//je to raketa
  {
  object[f].sprite=1;
  object[f].typ=10;
  object[f].x=x;
  object[f].y=y;
  object[f].speed_x=0;
  object[f].speed_y=0;
  object[f].speed_length=8;//rychlost kulky
  object[f].angle=angle;  
  object[f].power=1;
  object[f].radius=8;
  object[f].data1=xdata1;//koho NEma kulka zabit
  object[f].data2=xdata2;//ktera prisera PRESNE ji vystrelila (aby se mohlo utocit na toho kdo strili)
  object[f].rotating=30;//otaci se automaticky
  object[f].homing=4;
  }   

 
return(f);
}




int summon_particle(int typ,int x,int y,int angle,int xdata1,int xdata2,int xdata3, int xdata4)
{
int f,g;
//najdi volny misto a vloz tam novej objekt
f=1;
for (g=0;g<max_particles;g++)
 {
  if ((g>(max_particles-2))||(particle[g].typ==0)) {f=g;break;} // zaroven aby nepretekl
 }
 
//pokud nenasel, zkus najit nejakej particle, kterej bys pouzil
if ((max_particles-1)==g)
  {
    for (g=0;g<max_particles;g++)
     {
     if ((g>(max_particles-2))||(particle[g].typ==3)||(particle[g].typ==2)) {f=g;break;} // zaroven aby nepretekl
     }
   }
 
/// dej objekt na tuto pozici

//vymaz objekt, ktery bdue vznikat
 particle[f].x=0; particle[f].y=0; particle[f].sprite=0; particle[f].speed_x=0; particle[f].speed_y=0;
 particle[f].speed_length=0; particle[f].angle=0;
 particle[f].radius=0;particle[f].alpha=255; particle[f].rotating=0; particle[f].rotating_angle=0;
 particle[f].is_on_floor=0;particle[f].blender=0;particle[f].is_1st_floor=0;
 particle[f].data1=0;particle[f].data2=0;particle[f].data3=0;particle[f].data4=0;
 particle[f].is_on_top=0;particle[f].zoom=1;
 
 
  
if (typ==2)//je to particle z vybuchu
  {
  particle[f].sprite=2;
  particle[f].typ=2;
  particle[f].x=x;
  particle[f].y=y;
  particle[f].speed_x=0;
  particle[f].speed_y=0;
  particle[f].speed_length=2;//rychlost
  particle[f].angle=angle;  
  particle[f].power=0;
  particle[f].radius=0;
  particle[f].data1=xdata1;//jak daleko doleti
  particle[f].rotating=0;
  particle[f].is_1st_floor=1;
  }
  
  
 if (typ==3)//je to trail za strelou
  {
  particle[f].sprite=2;
  particle[f].typ=3;
  particle[f].x=x;
  particle[f].y=y;
  particle[f].speed_length=2;//rychlost
  particle[f].angle=angle;  
  particle[f].power=0;
  particle[f].radius=0;
  particle[f].data1=xdata1;//jak daleko doleti
  particle[f].rotating=0;
  particle[f].is_1st_floor=1;
  } 
  
   
 if (typ==4)//je to flek krve
  {
  particle[f].sprite=8+actual_blood_splat;
  particle[f].typ=4;
  particle[f].x=x;
  particle[f].y=y;
  particle[f].angle=angle;  
  particle[f].power=0;
  particle[f].radius=0;
  particle[f].is_on_floor=1;
  particle[f].blender=0;
  particle[f].alpha=220;
  actual_blood_splat++;
  if (actual_blood_splat>13) actual_blood_splat=0;
  }      
  
  
 if (typ==5)//je to stit
  {
  particle[f].sprite=29;
  particle[f].typ=5;
  particle[f].x=x;
  particle[f].y=y;
  particle[f].angle=angle+180;  
  particle[f].power=0;
  particle[f].radius=0;
  particle[f].is_on_floor=1;
  particle[f].blender=0;
  particle[f].alpha=255;
  particle[f].data1=xdata1;//ktera prisera ma stit
  particle[f].data2=15;//jak dlouho jeste bude trvat
  }
  
 if (typ==6)//je to kurzor - jede na prisere (muze ukazovat i text BOSS)
  {
  particle[f].sprite=xdata2;
  particle[f].typ=6;
  particle[f].x=x;
  particle[f].y=y;
  particle[f].is_1st_floor=1;
  particle[f].blender=0;
  particle[f].alpha=155;
  particle[f].data1=xdata1;//kterou prisera nasledovat, kdyz je -1, tak nasleduje actual hrace
  particle[f].rotating=2;
  } 
  
 if (typ==7)//je to vec na zemi, ktera pomalu mizi - vyuzije se na vyznaceni mista kam ma jit apod
  {
  particle[f].sprite=xdata2;
  particle[f].typ=7;
  particle[f].x=x;
  particle[f].y=y;
  particle[f].is_1st_floor=1;
  particle[f].blender=0;
  particle[f].alpha=155;
  particle[f].data1=xdata1;//jak dlouho nez zmizi
  particle[f].rotating=0;
  } 

if (typ==11)//je to kour
  {
  particle[f].sprite=64+(rand()&2);
  particle[f].typ=11;
  //particle[f].is_on_top=1;
  particle[f].blender=0;
  particle[f].x=x;
  particle[f].y=y;
  particle[f].speed_x=0;
  particle[f].speed_y=0;
  particle[f].speed_length=1;//rychlost kulky
  particle[f].angle=angle;  
  particle[f].rotating=5;//otaci se automaticky
  particle[f].is_on_floor=1;//is_1st_floor=1;
  particle[f].data1=255;//kolik kroku bude mize
  particle[f].data2=2;//krok mizeni
  particle[f].zoom=3;
  
  } 

  
  
return(f);
}


void summon_explode(int rozsah,int x,int y)
{
int pocet,delka,angle;
int f;     

pocet=10;
delka=50;


if (rozsah==1)//hodne
  {
   pocet=40;
   delka=30;
  }
if (rozsah==2) //malo
  {
   pocet=10;
   delka=8;
  } 
if (rozsah==3) //vybuch rozmazanej (bez kruhu)
  {
   pocet=30;
   delka=20+(rand()&10);
  } 
  
for (f=0;f<pocet;f++)
 {
 angle=(random()&120)+(random()&300)+(random()&300);
 summon_particle(2,x+(random()&2)-3,y+(random()&2)-3,angle,delka,0,0,0);
 }

}



void summon_lightning(int f,int angle,int lightning_length) //cislo prisery tkera udelala blesk
{
int temp,temp1,temp2,temp3,temp4,temp_angle,tempx,tempy;

 temp4=255/lightning_length;//pro alpha

 temp3=1+(rand()&3);
 temp_angle=angle;
 tempx= 10 * cos((float)(M_PI*((float)temp_angle))/180);
 tempy= 10 * sin((float)(M_PI*((float)temp_angle))/180);
 
 temp2=summon_object(8,prisera[f].x+tempx+(rand()&6)-3,prisera[f].y+tempy+(rand()&6)-3,temp_angle,prisera[f].typ,f,0,temp3);
 
// delej cyklus, kterej udela 10 kroku blesku
 temp1=lightning_length;//pocet kroku blesku

 do
 {
 temp1--;
 temp2=summon_object(8,object[temp2].x+tempx+(rand()&6)-3,object[temp2].y+tempy+(rand()&6)-3,temp_angle,prisera[f].typ,f,temp2,temp3);
 object[temp2].alpha=(temp1*temp4);
 if (((temp1/3)*3)==temp1) {object[temp2].power=1;}else{object[temp2].power=0;}

if (((rand()&7)>3)&&(temp1<15)){
                   temp_angle=object[temp2].angle+20-(rand()&40);
                   object[temp2].angle=temp_angle;
                   tempx= 10 * cos((float)(M_PI*((float)temp_angle))/180);
                   tempy= 10 * sin((float)(M_PI*((float)temp_angle))/180);
                   }/**/
temp=level[(int)(object[temp2].x)/tile_width][(int)(object[temp2].y)/tile_height].typ;
 }
 while ((temp1>0)&&(temp<9)&&(temp!=7));
 
 //je to ve zdi ? pak udelej efekt rozprsku pred zdi, jinak na konci blesku


 if (level[(int)(object[temp2].x)/tile_width][(int)(object[temp2].y)/tile_height].typ>8)
   summon_explode(2,object[object[temp2].data3].x,object[object[temp2].data3].y);
//   else
  // summon_explode(2,object[temp2].x,object[temp2].y);
        
}

void vystrel_kulku(int f)
{
int typ_kulky;//koho zabije
int tempx,tempy,temp1,temp2,temp3,temp_angle;
int loading_weapon;

//if (prisera[f].loading_weapon!=0) return;

prisera[f].loading_weapon+=4;
loading_weapon=prisera[f].loading_weapon;
if (f==aktualni_hrac) {loading_weapon=loading_weapon/2;}//aktualni hrac se nabiji dvakrat rychle - drzenim fire a automatickym narustanim zaroven

if ((prisera[f].weapon==1)&&(loading_weapon>60)) // klasicka KULKA
 {
 summon_object(1,prisera[f].x,prisera[f].y,prisera[f].angle-90,prisera[f].typ,f,0,0);
 prisera[f].loading_weapon=0;
 return;
 }
 
 
if ((prisera[f].weapon==2)&&(loading_weapon>8)) // strili BLESKY
 {
 summon_lightning(f,prisera[f].angle-95,30);
 summon_lightning(f,prisera[f].angle-85,30);
 summon_lightning(f,prisera[f].angle+(rand()&180),(rand()&3)+2);
 summon_lightning(f,prisera[f].angle-(rand()&180),(rand()&3)+2);
 prisera[f].loading_weapon=0;
 return;
 }
 
 
if ((prisera[f].weapon==3)&&(loading_weapon>90)) // klasicka RAKETA
 {
 summon_object(10,prisera[f].x,prisera[f].y,prisera[f].angle-90,prisera[f].typ,f,0,0);
 prisera[f].loading_weapon=0;
 return;
 }


if ((prisera[f].weapon==4)&&(loading_weapon>30)) // strili KOUR
 {
 summon_particle(11,prisera[f].x,prisera[f].y,prisera[f].angle-90,0,0,0,0);
 prisera[f].loading_weapon=0;
 return;
 }


if ((prisera[f].weapon==5)&&(loading_weapon>70)) // SIPY
 {
 summon_object(3,prisera[f].x,prisera[f].y,prisera[f].angle-90,prisera[f].typ,f,0,0);
 prisera[f].loading_weapon=0;
 return;
 }


}


void vystrel()
{
int f,g,h;
vystrel_kulku(aktualni_hrac);//udelej kulku   
}

//test jestli kulka narazila do nejake prisery
int hit_test(int h,int pocet)//pokud je pocet ==0 tak muze kulka zabit neomezene prise, pokud je jedna, tak jen jednu a zmizne
{
int f,g,uber,temp;
int return_value;

return_value=0;

for (f=0;f<max_priser;f++)
 {                         
 if ((prisera[f].typ!=object[h].data1)&&((prisera[f].is_monster==1)||(prisera[f].disable_bounce==0))&&(prisera[f].typ!=0))
  {
   if (konflikt_objekt_prisera(f,h)>0)
    {
     uber=object[h].power;              
    if (prisera[f].shield>0)
      {
      prisera[f].shield-=uber;//uber stit
      summon_particle(5,prisera[f].x,prisera[f].y,object[h].angle,f,0,0,0);//udelej stit
      if (prisera[f].shield<0)
             {
              uber+=prisera[f].shield;//sniz uber jen o ten kousek
              prisera[f].shield=0;//stit vynulvoat (byl zapornej)
              }
              else
              {
              uber=0;//zrusi se o ten stit    
              }
      }
    if (uber>0)//pokud ubral stit a sila byla vetsi, tak jeste i uber zivot, jinak ubira zivot jako by bylo bez stitu
      {
      prisera[f].health-=uber;//uber zivota kolik ma kulka
      if (prisera[f].is_monster==1)//jen prisery muzou krvacet, ostatni objekty si to musi poresit samy
      {
      if (prisera[f].health>0)
        {
         summon_particle(4,prisera[f].x,prisera[f].y,object[h].angle,0,0,0,0);//udelej krev
         }
         else
         {
         temp=summon_particle(4,prisera[f].x,prisera[f].y,object[h].angle,0,0,0,0);//udelej krev
         particle[temp].sprite=22+actual_blood_splat/2;
         }
       }
      }
    if (prisera[f].typ==4)//je to plasma ball, bude delat bounce
      {
prisera[f].shockwave_speed=70;
prisera[f].shockwave_angle=object[h].angle;
prisera[f].shockwave_speed_x=0;
prisera[f].shockwave_speed_y=0;
      }  
      
//    if (pocet==1) return(1);//rekni zpatky ze ma kulka zmiznout
//    return_value=1;
    if (pocet==1) return(f);//rekni zpatky ze ma kulka zmiznout - mela zabit jen jednoho nepritele
    return_value=f;//prisera kterou posledni zabila
    }
  }    
 }
return(return_value);
}



void do_objects()
{
int f,g,h,temp_x,temp_y,temp,temp1,temp2,temp_level;     



//testing only
object_counter=0;


for(f=0;f<max_objects;f++)
{
if (object[f].typ>0)
{
  object_counter++;
                        
if (object[f].rotating!=0) {object[f].rotating_angle+=object[f].rotating;
                            if (object[f].rotating_angle>360) {object[f].rotating_angle-=360;}
                            if (object[f].rotating_angle<(-360)) {object[f].rotating_angle=0;}}
                    
if (object[f].typ==1)//je to kulka
  {
   //ma urceny speed_x a speed_y ? pokud ne, tak ho urci, kulka nemuze jen tak lezet
   if ((object[f].speed_x==0)&&(object[f].speed_y==0))
     {
     object[f].speed_x = object[f].speed_length * cos((float)(M_PI*((float)object[f].angle))/180);
     object[f].speed_y = object[f].speed_length * sin((float)(M_PI*((float)object[f].angle))/180);
     }
   //posun ve smeru letu
   //je tam volno ? pokud ano, posun ho tam, pokud ne, znic kulku   
   temp_level=level[(int)(object[f].x+object[f].speed_x)/tile_width][(int)(object[f].y+object[f].speed_y)/tile_height].typ;
   if (//narazil do zdi ?
       (temp_level<9)&& //neni to zed (9)
       (temp_level!=7)//nejsou to dvere
        &&//narazil do prisery?
       (hit_test(f,1)==0)
      )  
     {//posun ho na novou pozici
     object[f].x+=object[f].speed_x;
     object[f].y+=object[f].speed_y;
     }
     else
     {
     object[f].typ=0;//znic kulku
     summon_explode(2,object[f].x,object[f].y);
     }
     

  }
  

if (object[f].typ==2)//jsou to dvere
  {
   //je nekdo blizko ?
   temp=0;
   temp1=0;//na konci, pokud je 0, tak schova dvere, pokud je 1 tak otevre
   for (g=0;g<max_priser;g++)
    {
   temp1=abs(prisera[g].x-object[f].x);
   temp2=abs(prisera[g].y-object[f].y);
   
    if ((prisera[g].typ>0)&&
        ((temp1*temp1+temp2*temp2)<(100*100))
        //&&(vidi(object[f].x,object[f].y,prisera[g].x,prisera[g].y,0)>0)
        )
          {temp=1;g=max_priser;}//nasel nekoho blizko
    }

   if (object[f].data1==1)
    {
   //jsou otevreny ?                         
   //--pokud je nekdo blizko, tak nic, jinak je zavri
   if (temp==0) {object[f].data1=0;temp1=0;} //zavri dvere
   else
    {temp1=1;} //nech otevreny
    }else
    {
         
   //jsou zavreny ?
   //-- pokud je nekdo blizko, tak je otevri, jinak nic
   if (temp==1)
        {object[f].data1=1;temp=1;} //otevri dvere
   else {temp1=0;} //nech zavreny
    }
    
    if (temp1==0){
                 object[f].data2--;if (object[f].data2<1) {object[f].data2=0;}//animace otevreni dveri (deli se dvema)
                 }else
                 {
                 object[f].data2++;if (object[f].data2>8) {object[f].data2=8;}//animace zavreni dveri (deli se dvema)
                 }
//dvere vertikalni                 
 if(object[f].data4==1)
   {
    if (object[f].data2<2){
                 level[(int)(object[f].x/tile_width)+1][(int)(object[f].y/tile_height)].typ=7;
                 level[(int)(object[f].x/tile_width)][(int)(object[f].y/tile_height)].typ=7;
                 level[(int)(object[f].x/tile_width)-1][(int)(object[f].y/tile_height)].typ=7;
                 }else
                 {
                 level[(int)(object[f].x/tile_width)+1][(int)(object[f].y/tile_height)].typ=0;
                 level[(int)(object[f].x/tile_width)][(int)(object[f].y/tile_height)].typ=0;
                 level[(int)(object[f].x/tile_width)-1][(int)(object[f].y/tile_height)].typ=0;
                 }
   }
   else
   {
    if (object[f].data2<2){
                 level[(int)(object[f].x/tile_width)][(int)(object[f].y/tile_height)+1].typ=7;
                 level[(int)(object[f].x/tile_width)][(int)(object[f].y/tile_height)].typ=7;
                 level[(int)(object[f].x/tile_width)][(int)(object[f].y/tile_height)-1].typ=7;
                 }else
                 {
                 level[(int)(object[f].x/tile_width)][(int)(object[f].y/tile_height)+1].typ=0;
                 level[(int)(object[f].x/tile_width)][(int)(object[f].y/tile_height)].typ=0;
                  level[(int)(object[f].x/tile_width)][(int)(object[f].y/tile_height)-1].typ=0;
                 }
   }
   
    object[f].texture=object[f].data3+(object[f].data2/2);
  }



if (object[f].typ==3)//je to SIP
  {
                          
  if (object[f].power>0)
   {
   //ma urceny speed_x a speed_y ? pokud ne, tak ho urci, kulka nemuze jen tak lezet
   if ((object[f].speed_x==0)&&(object[f].speed_y==0))
     {
     object[f].speed_x = object[f].speed_length * cos((float)(M_PI*((float)object[f].angle))/180);
     object[f].speed_y = object[f].speed_length * sin((float)(M_PI*((float)object[f].angle))/180);
     }
   //posun ve smeru letu
   //je tam volno ? pokud ano, posun ho tam, pokud ne, znic kulku   
   temp_level=level[(int)(object[f].x+object[f].speed_x)/tile_width][(int)(object[f].y+object[f].speed_y)/tile_height].typ;

temp=hit_test(f,1);
   if (//narazil do zdi ?
       (temp_level<9)&& //neni to zed (9)
       (temp_level!=7)//nejsou to dvere
        &&//narazil do prisery?
       (temp==0)
      )  
     {//posun ho na novou pozici
     object[f].x+=object[f].speed_x;
     object[f].y+=object[f].speed_y;
     }
     else
     {
     object[f].power=0;//znic sip - zasekne se kam dopadl a bude drzet pozici

    if (temp>0)
     {
      object[f].data3=temp;//data3 bude mit cislo prisery na kterou narazil
      object[f].data4=prisera[temp].x-object[f].x;
      object[f].data5=prisera[temp].y-object[f].y;  
      }
     summon_explode(2,object[f].x,object[f].y);
     }
   }
   else
  {//drzi se posledni pozice kam dopadl
  if (object[f].data3>0)
     {
     object[f].x=prisera[object[f].data3].x-object[f].data4;
     object[f].y=prisera[object[f].data3].y-object[f].data5;
     if (prisera[object[f].data3].typ==0) {object[f].data3=0;}//kdyz prisera chcipne, tak se zrus
     }
     else
     {
     if (object[f].alpha>0)
       {
       object[f].alpha-=5;                    
       }    
       else
       {
       object[f].typ=0;
       }
     }     
  } 

  }



if (object[f].typ==8)//je to kus blesku
  {
  if (((object[object[f].data3].typ!=8)&& // nenavazuje na blesk
      (object[f].data3!=0))&&(hit_test(f,1)==0))
     {object[f].typ=0;}
     else
     {
         //delej blesk
         object[f].data4--; //zmizni za chvilku
     if (object[f].data4<1) {object[f].typ=0;}
     }
  }

if (object[f].typ==9)//je to kus blesku
  {
  hit_test(f,0); //zkus nekoho zabit
  object[f].data4--;
  if (object[f].data4<1)
     {
      object[f].data4=object[f].data3;
      object[f].sprite++;//animuj
      object[f].data5--;
      if (object[f].data5<1)  {object[f].typ=0;}
      }
  }
  

if (object[f].typ==10)//je to raketa
  {
   //ma urceny speed_x a speed_y ? pokud ne, tak ho urci, kulka nemuze jen tak lezet
   if ((object[f].speed_x==0)&&(object[f].speed_y==0))
     {
     object[f].speed_x = object[f].speed_length * cos((float)(M_PI*((float)object[f].angle))/180);
     object[f].speed_y = object[f].speed_length * sin((float)(M_PI*((float)object[f].angle))/180);
     }
   //posun ve smeru letu
   //je tam volno ? pokud ano, posun ho tam, pokud ne, znic kulku   
   temp_level=level[(int)(object[f].x+object[f].speed_x)/tile_width][(int)(object[f].y+object[f].speed_y)/tile_height].typ;
   if (//narazil do zdi ?
       (temp_level<9)//neni to zed
       &&
       (temp_level!=7)//nejsou to dvere
        &&//narazil do prisery?
       (hit_test(f,1)==0)
      )  
     {//posun ho na novou pozici
     object[f].x+=object[f].speed_x;
     object[f].y+=object[f].speed_y;
     temp=summon_particle(3,object[f].x+(random()&2)-3,object[f].y+(random()&2)-3,object[f].angle+180,25,0,0,0);
     }
     else
     {
     summon_object(9,object[f].x,object[f].y,0,object[f].data1,object[f].data2,0,0);//exploze
     object[f].typ=0;//znic kulku
     summon_explode(1,object[f].x,object[f].y);
       temp=summon_particle(4,object[f].x,object[f].y,0,0,0,0,0);//exploze
     particle[temp].sprite=71;
     particle[temp].angle=0;
     }
           
             
  }


if (object[f].homing>0)
 {
   //jestli vidi nepritele, tak zahne za nim
   temp=prisera_najdi_viditelnej_cil_pro_kulku(f,object[f].data1);
     if (temp>0)
       {/*
        if (prisera_uhel(object[f].x,object[f].y,prisera[temp].x,prisera[temp].y)>object[f].angle)
           {object[f].angle=prisera_uhel(object[f].x,object[f].y,prisera[temp].x,prisera[temp].y);}
           else
           {object[f].angle=prisera_uhel(object[f].x,object[f].y,prisera[temp].x,prisera[temp].y);}/**/
        temp=prisera_uhel(object[f].x,object[f].y,prisera[temp].x,prisera[temp].y)-90;
        if (temp<0) {temp+=360;}
        if (temp>360) {temp-=360;}
        if (object[f].angle<0) {object[f].angle+=360;}
        if (object[f].angle>360) {object[f].angle-=360;}


if ((temp<180)&&(object[f].angle>180)) {object[f].angle-=360;} //kdyzby to bylo kolem nuly, kulka nad (360), prisera pod (0)

        if (abs(temp-object[f].angle)>180) {object[f].angle+=360;}
//        if (abs(temp-object[f].angle)<120)
         {
          if (temp>object[f].angle)
            {object[f].angle+=object[f].homing;}
           else
            {object[f].angle-=object[f].homing;}
          }
        object[f].speed_x=0;
        object[f].speed_y=0;
        }
        
   }    
 
}


}
}




void do_particles()
{
int f,g,h,temp_x,temp_y,temp,temp_level;     

//testing only
particle_counter=0;


for(f=0;f<max_particles;f++)
{
if (particle[f].typ>0)
{
  particle_counter++;
                        
if (particle[f].rotating!=0) {particle[f].rotating_angle+=particle[f].rotating;
                            if (particle[f].rotating_angle>360) {particle[f].rotating_angle-=360;}
                            if (particle[f].rotating_angle<(-360)) {particle[f].rotating_angle=0;}}
                    
if (particle[f].typ==2)//je to obycejna particle
  {
   //ma urceny speed_x a speed_y ? pokud ne, tak ho urci, kulka nemuze jen tak lezet
   if ((particle[f].speed_x==0)&&(particle[f].speed_y==0))
     {
     particle[f].speed_x = particle[f].speed_length * cos((float)(M_PI*((float)particle[f].angle))/180);
     particle[f].speed_y = particle[f].speed_length * sin((float)(M_PI*((float)particle[f].angle))/180);
     }
   //posun ve smeru letu
   //je tam volno ? pokud ano, posun ho tam, pokud ne, znic kulku   
   if ((level[(int)(particle[f].x+particle[f].speed_x)/tile_width][(int)(particle[f].y+particle[f].speed_y)/tile_height].typ<9))  
     {//posun ho na novou pozici
     particle[f].x+=particle[f].speed_x;
     particle[f].y+=particle[f].speed_y;
         
     }
     else
     {//znic kulku
     particle[f].typ=0;
     }
  particle[f].data1--; if (particle[f].data1<1) {particle[f].typ=0;}
  if (particle[f].data1<24) {particle[f].alpha=10*particle[f].data1;} else {particle[f].alpha=255;}
  if (particle[f].data1==3) {particle[f].speed_x=0;particle[f].speed_y=0;particle[f].speed_length=0;}
  }
  
  
  
if (particle[f].typ==3)//je to particle za kulkou
  {
  temp=level[(int)(particle[f].x/tile_width)][(int)(particle[f].y/tile_height)].typ;
   //je tam volno ? pokud ano, posun ho tam, pokud ne, znic kulku   
   if (!((temp<9)||(temp!=7)))  
     {//znic ji
     particle[f].typ=0;
     }

  particle[f].data1--; if (particle[f].data1<1) {particle[f].typ=0;}
  if (particle[f].data1<24) {particle[f].alpha=5*particle[f].data1;} else {particle[f].alpha=200;}
  }

  
  
  
if (particle[f].typ==5)//je to stit
  {
   if (prisera[particle[f].data1].typ>0)//prisera jeste zije
     {
      particle[f].x=prisera[particle[f].data1].x;
      particle[f].y=prisera[particle[f].data1].y;
     }
     else
     {//znic ho, prisera uz zdechla
      particle[f].typ=0;
      }
  particle[f].data2--; if (particle[f].data2<1) {particle[f].typ=0;}
  if (particle[f].data2<15) {particle[f].alpha=10*particle[f].data2;} else {particle[f].alpha=255;}
  }
  
  
  
  
if (particle[f].typ==6)//je to kurzor pod priserou
  {
   if ((particle[f].data1==0)||(prisera[particle[f].data1].typ>0))//prisera jeste zije
     {
      if (particle[f].data1==0)//neni urceno, chytne se aktualniho hrace
       {
       particle[f].x=prisera[aktualni_hrac].x;
       particle[f].y=prisera[aktualni_hrac].y;  
       if (aktualni_hrac==-1) {particle[f].typ=0;} //znic ho, uz neni zadny hrac           
       }
       else
       {
       particle[f].x=prisera[particle[f].data1].x;
       particle[f].y=prisera[particle[f].data1].y;
       }
     }
     else
     {//znic ho, prisera uz zdechla
      particle[f].typ=0;
      }
  }
  
if (particle[f].typ==7)//je to objekt, kterej jen zmizne (kurzor kam ma jit apod)
  {
  particle[f].data1--; if (particle[f].data1<1) {particle[f].typ=0;}
  if (particle[f].data1<24) {particle[f].alpha=3*particle[f].data1;} else {particle[f].alpha=72;}
  }

if (particle[f].typ==11)//je to stin
  {
   //ma urceny speed_x a speed_y ? pokud ne, tak ho urci, kulka nemuze jen tak lezet
   if ((particle[f].speed_x==0)&&(particle[f].speed_y==0))
     {
     particle[f].speed_x = particle[f].speed_length * cos((float)(M_PI*((float)particle[f].angle))/180);
     particle[f].speed_y = particle[f].speed_length * sin((float)(M_PI*((float)particle[f].angle))/180);
     }
   //posun ve smeru letu
   //je tam volno ? pokud ano, posun ho tam, pokud ne, znic kulku   
   temp_level=level[(int)(particle[f].x+object[f].speed_x)/tile_width][(int)(particle[f].y+object[f].speed_y)/tile_height].typ;
   if (//narazil do zdi ?
       (temp_level<9)&& //neni to zed (9)
       (temp_level!=7)//nejsou to dvere
      )  
     {//posun ho na novou pozici
//     particle[f].x+=particle[f].speed_x;
//     particle[f].y+=particle[f].speed_y;
     }

   if (particle[f].data1>0)
    {
    particle[f].data1-=particle[f].data2;
//    particle[f].alpha=255-particle[f].data1;
    if (particle[f].data1>128)
        particle[f].alpha=255-(particle[f].data1-128*2);
        else
        particle[f].alpha=particle[f].data1;

    if (particle[f].data1<1) particle[f].typ=0;//zmiz
    }
if (particle[f].zoom>0) {particle[f].zoom-=0.05;} else {particle[f].zoom=0.01;}
//particle[f].zoom=0.5;
  }
  
}
}
}










void do_ai()
{
int f,g,h,i,j,speed,steps_found,temp_x,temp_y,temp,force_angle,temp_level;
char temp_string[200];

//testing only
monster_counter=0;
mouse_cursor_texture=3;//klasicka sipka
mouse_cursor_showing=-1;//neukazuje na nikoho


if ((prisera[aktualni_hrac].health<1)&&(aktualni_hrac!=-1))//zemri, podle urovne kolik mas zivot, cim vic, tak tim vic se rozleti..
    {
    aktualni_hrac=-1;
    //najdi nejblizsiho
/*    f=aktualni_hrac;
    g=najdi_nejblizsiho_hrace(prisera[aktualni_hrac].x,prisera[aktualni_hrac].y);      
    if (g!=aktualni_hrac) {aktualni_hrac=g;} else {aktualni_hrac=-1;}
    prisera[f].typ=0;
    level[(prisera[f].x)/tile_width][(prisera[f].y)/tile_height].monster_memo=0;
    summon_explode(3,prisera[f].x,prisera[f].y);
  */  
     }
     
     
//if (prisera[aktualni_hrac].health>0) {prisera[aktualni_hrac].angle=prisera_uhel(prisera[aktualni_hrac].x,prisera[aktualni_hrac].y,kamera_x+mymouse_x,kamera_y+mymouse_y);}
if ((prisera[aktualni_hrac].health>0)&&(aktualni_hrac>-1))
    {
/*
     temp_x=prisera[aktualni_hrac].x-my_screen_width/2;
     temp_y=prisera[aktualni_hrac].y-my_screen_height/2;
     if (temp_x<0) temp_x=0;
     if (temp_y<0) temp_y=0;
     prisera[aktualni_hrac].angle=prisera_uhel(prisera[aktualni_hrac].x,prisera[aktualni_hrac].y,mymouse_x+temp_x,mymouse_y+temp_y);
/**/
  //   temp_x=real_mouse_x;
//     temp_y=real_mouse_y;
prisera[aktualni_hrac].angle=prisera_uhel(prisera[aktualni_hrac].x,prisera[aktualni_hrac].y,real_mouse_x,real_mouse_y);






//ovladani hlavni prisery  - hrace
f=aktualni_hrac;

if (prisera[f].loading_weapon<250) {prisera[f].loading_weapon+=4;}

prisera[f].fov_sprite=-1;//nezobrazuje ho u aktualniho hrace


    
if (prisera[f].health>0)
 {    
  if (prisera[f].loading_weapon>0) {prisera[f].loading_weapon--;}      
      
  if (prisera[f].path[0].x>0)//je vubec nalezena cesta ?
   {
   //zrus znacku kterou si mel v mape
   if (level[(prisera[f].x)/tile_width][(prisera[f].y)/tile_height].monster_memo==f) level[(prisera[f].x)/tile_width][(prisera[f].y)/tile_height].monster_memo=0;

   speed=prisera[f].speed-(level[(prisera[f].x)/tile_width][(prisera[f].y)/tile_height].speed/10);
   if (speed<1)speed=1;
   temp_x=prisera[f].x;temp_y=prisera[f].y;
   if (prisera[f].x>prisera[f].path[0].x) {prisera[f].x-=speed;}
   if (prisera[f].x<prisera[f].path[0].x) {prisera[f].x+=speed;}
   if (prisera[f].y>prisera[f].path[0].y) {prisera[f].y-=speed;}
   if (prisera[f].y<prisera[f].path[0].y) {prisera[f].y+=speed;}


     if 
      (
      (level[prisera[f].path[0].x/tile_width][prisera[f].path[0].y/tile_height].monster_memo!=0)&&
      (prisera[level[prisera[f].path[0].x/tile_width][prisera[f].path[0].y/tile_height].monster_memo].typ!=prisera[f].typ)
      )
      {
       prisera[f].x=temp_x;prisera[f].y=temp_y;
       if (level[(prisera[f].x)/tile_width][(prisera[f].y)/tile_height].monster_memo==0) level[(prisera[f].x)/tile_width][(prisera[f].y)/tile_height].monster_memo=f;
           prisera[f].path[0].x=(prisera[f].x)/tile_width*tile_width+tile_width_2;
           prisera[f].path[0].y=(prisera[f].y)/tile_height*tile_height+tile_height_2;           
       }

       
       
   if ((abs(prisera[f].y-prisera[f].path[0].y)<2)&&(abs(prisera[f].x-prisera[f].path[0].x)<2))
    {   
    steps_found=find_path(f,prisera[f].x/tile_width,prisera[f].y/tile_height,prisera[f].cil_x/tile_width,prisera[f].cil_y/tile_height);
    if (steps_found<1)
                      {
                       level[(prisera[f].x)/tile_width][(prisera[f].y)/tile_height].monster_memo=0;
                       prisera[f].path[0].x=0;
                       }
    }
   //udelej si znacku
   if (level[(prisera[f].x)/tile_width][(prisera[f].y)/tile_height].monster_memo==0) level[(prisera[f].x)/tile_width][(prisera[f].y)/tile_height].monster_memo=f;
   }
   else//nebyla cesta, najdi
   {
    if (prisera[f].cil_x>0)
    {
     if (level[(prisera[f].x)/tile_width][(prisera[f].y)/tile_height].monster_memo==f) level[(prisera[f].x)/tile_width][(prisera[f].y)/tile_height].monster_memo=0;
     steps_found=find_path(f,prisera[f].x/tile_width,prisera[f].y/tile_height,prisera[f].cil_x/tile_width,prisera[f].cil_y/tile_height);
     if (steps_found<2) {level[(prisera[f].x)/tile_width][(prisera[f].y)/tile_height].monster_memo=0;prisera[f].path[0].x=0;}
    if (level[(prisera[f].x)/tile_width][(prisera[f].y)/tile_height].monster_memo==0) level[(prisera[f].x)/tile_width][(prisera[f].y)/tile_height].monster_memo=f;
    }                
   }
}
}


/**/
//konec aktualniho hrace ------------------------------------------------ ostatni prisery

for(f=0;f<max_priser;f++)
//for(f=1;f<4;f++)
{
//hlavni prisery - typ 1 a 2
if (((prisera[f].typ==1)||(prisera[f].typ==2))&&(f!=aktualni_hrac))
 {
 
  monster_counter++;

if (prisera[f].health<1)//zemri, podle urovne kolik mas zivot, cim vic, tak tim vic se rozleti..
    {
    summon_explode(3,prisera[f].x,prisera[f].y);
    prisera[f].typ=0;
    level[(prisera[f].x)/tile_width][(prisera[f].y)/tile_height].monster_memo=0;
    continue;}

     
      
//udelej FOV sprite
if (prisera[f].alerted>0)
    {prisera[f].fov_actual=350;}
    else
    {prisera[f].fov_actual=180;}
    

//oprav FOV
if (prisera[f].fov<prisera[f].fov_actual)//fov narusta
     {
      prisera[f].fov+=10;
      prisera[f].fov_sprite=-1;
     }
     
if (prisera[f].fov>prisera[f].fov_actual)//fov se snizuje
     {
      prisera[f].fov-=10;
      prisera[f].fov_sprite=-1;
     }
     
if (prisera[f].fov_sprite==-1)
    {
    prisera[f].fov_sprite=6;
    if (prisera[f].fov>190) prisera[f].fov_sprite=5;
    if (prisera[f].fov>230) prisera[f].fov_sprite=4;
    if (prisera[f].fov>270) prisera[f].fov_sprite=3;
    if (prisera[f].fov>300) prisera[f].fov_sprite=2;
    if (prisera[f].fov>320) prisera[f].fov_sprite=1;
    if (prisera[f].fov>340) prisera[f].fov_sprite=0;
    }


//---zacatek vojaka
if ((prisera[f].typ==2)||(prisera[f].typ==1)) // 1 nebo 2
  {
  //pauza pro nabijeni zbrane  
  if (prisera[f].loading_weapon>0) {prisera[f].loading_weapon--;}
  //ztrata cile, pokud ho moc dlouho nevidel
  if (prisera[f].alerted_time==0) {prisera[f].alerted=0;} else {prisera[f].alerted_time--;}
  //vytvori si a pouziva guard, pokud ho nema (kam se ma vratit, jakmile skonci s pronasledovanim nepritele)
  if (prisera[f].guard_x==0) {prisera[f].guard_x=prisera[f].x;prisera[f].guard_y=prisera[f].y;}//inicializace pro prisery, ktery to nemaji definovany
  if (prisera[f].guard_wait>0) {prisera[f].guard_wait++;}//pokud se pocita, tak pocitej
  if (prisera[f].guard_wait>100) {prisera[f].cil_vzdalenost=80;prisera[f].cil_x=prisera[f].guard_x;prisera[f].cil_y=prisera[f].guard_y;prisera[f].guard_wait=0;}//vydej se na cestu zpatky


temp=0;
      
   if (prisera[f].alerted>0)
     {
     prisera[f].guard_wait=0;
     prisera[f].cil_x=prisera[prisera[f].alerted].x;
     prisera[f].cil_y=prisera[prisera[f].alerted].y;
     prisera[f].cil_vzdalenost=80;
     }
     else
     {
     //ma pocitat s cekanim na cestu zpatky ?
//       if ((prisera[f].guard_wait==0)&&((prisera[f].x/tile_width)==(prisera[f].cil_x/tile_width))&&((prisera[f].y/tile_width)==(prisera[f].cil_y/tile_width)))

       if ((prisera[f].guard_wait==0)&&((prisera[f].x/tile_width)==(prisera[f].cil_x/tile_width))&&((prisera[f].y/tile_height)==(prisera[f].cil_y/tile_height)))
        {prisera[f].guard_wait=1;}//zacni pocitat na cestu zpatky /**/
        
        
        if (prisera[f].active==-2)//nasleduje aktualniho hrace (-1 je hleda hrace, -2 je nasleduje aktualni_hrac)
        {
        prisera[f].guard_wait=0;
        prisera[f].cil_x=prisera[aktualni_hrac].x;
        prisera[f].cil_y=prisera[aktualni_hrac].y;
        prisera[f].cil_vzdalenost=(random()&40) + 40;
        //kdyz je moc blizko, mohl by chodit na ruzny mista kolem hrace, nestat jen jak trubka na miste :-/
        }
        if (prisera[f].active==-1)//je pripravenej si najit bosse
        { 
         //vidi aktualniho hrace ? pokud ano, tak se na neho nalep ..
         if (prisera_vidi(f,prisera[aktualni_hrac].x,prisera[aktualni_hrac].y)>0) prisera[f].active=-2;
        } /**/
        
      }
//pokud vidi priseru nejakou pobliz (protivnika), tak pujde za nim
force_angle=999;//-pokud najde nekoho koho ma sledovat, tak to zmeni a pak se na to prise bude divat
                           temp=prisera_najdi_viditelnej_cil(f);
                           if ((temp>0)&&(prisera[temp].typ!=prisera[f].typ))
                             {
                             force_angle=prisera_uhel(prisera[f].x,prisera[f].y,prisera[temp].x,prisera[temp].y);
                             prisera[f].angle=force_angle;
//                              if (prisera[f].path[0].x>0)//streli pred priseru
//                                    prisera[f].angle=prisera_uhel(prisera[f].x,prisera[f].y,prisera[temp].path[0].x,prisera[temp].path[0].y);

                              // vystreli jen kdyz vidi opravdu cil
                              if (prisera_vidi(f,prisera[temp].x,prisera[temp].y)==1) vystrel_kulku(f);
                              
                              prisera[f].cil_x=prisera[temp].x;
                              prisera[f].cil_y=prisera[temp].y; 
                              prisera[f].cil_vzdalenost=80;
                              if ((prisera[f].active>0)&&(prisera[prisera[f].active].typ>0))
                               {
                                prisera[f].cil_x=prisera[prisera[f].active].x;
                                prisera[f].cil_y=prisera[prisera[f].active].y; 
                               }            /**/                
                              }
                              else{temp=0;}
                           /*}/**/

   //------------------------- smer pohledu  
  if (force_angle==999)
   {
  //pokud vidi svuj cil, tak at se diva na cil
   if (prisera_vidi(f,prisera[f].cil_x,prisera[f].cil_y)>0)
     {
     if  ((abs(prisera[f].cil_x-prisera[f].x)*(abs(prisera[f].cil_x-prisera[f].x)))+
          (abs(prisera[f].cil_y-prisera[f].y)*(abs(prisera[f].cil_y-prisera[f].y)))
         >(tile_width*tile_width))
        {
         prisera[f].angle=prisera_uhel(prisera[f].x,prisera[f].y,prisera[f].cil_x,prisera[f].cil_y);
         }
     }/**/
    else
    {
    //dej mu uhel smerem cesty kam leze  
     if (prisera[f].path[0].x>0)
      {
       if (prisera[f].path[1].x>0) {prisera[f].angle=prisera_uhel(prisera[f].x,prisera[f].y,prisera[f].path[1].x,prisera[f].path[1].y);}
       else {prisera[f].angle=prisera_uhel(prisera[f].x,prisera[f].y,prisera[f].path[0].x,prisera[f].path[0].y);}
       }/**/
    }  
   }
   else
   {
     prisera[f].angle=force_angle;
   }
//   else //jestli je active, tak se divej na kurzor mysi

   //------------------------- smer pohledu KONEC


if ((prisera[f].guard_wait>20)&&(prisera[f].path[0].x>0)) {prisera[f].path[0].x=0;}//aby se zacal otacet



//jen pokus -- jeslit je blizko cile a vidi ho tak uz nejde bliz
  if (prisera[f].path[0].x>0)//je vubec nalezena cesta ?
    {
/*    if (
         (abs(prisera[f].x-prisera[f].cil_x)<prisera[f].cil_vzdalenost)&&
         (abs(prisera[f].y-prisera[f].cil_y)<prisera[f].cil_vzdalenost)&&
         (prisera_vidi(f,prisera[f].cil_x,prisera[f].cil_y)==1))
          {
           prisera[f].path[0].x=0;
           prisera[f].cil_x=prisera[f].x;
           prisera[f].cil_y=prisera[f].y;
           }/**/
    }


// konec pokusu


//posun se smerem na novou znacku v path 
  if (prisera[f].path[0].x>0)//je vubec nalezena cesta ?
   {
   //zrus znacku kterou si mel v mape
   if (level[(prisera[f].x)/tile_width][(prisera[f].y)/tile_height].monster_memo==f) level[(prisera[f].x)/tile_width][(prisera[f].y)/tile_height].monster_memo=0;

   speed=prisera[f].speed-(level[(prisera[f].x)/tile_width][(prisera[f].y)/tile_height].speed/10);
   temp_x=prisera[f].x;temp_y=prisera[f].y;
   if (prisera[f].x>prisera[f].path[0].x) {prisera[f].x-=speed;}
   if (prisera[f].x<prisera[f].path[0].x) {prisera[f].x+=speed;}
   if (prisera[f].y>prisera[f].path[0].y) {prisera[f].y-=speed;}
   if (prisera[f].y<prisera[f].path[0].y) {prisera[f].y+=speed;}


   if (level[prisera[f].path[0].x/tile_width][prisera[f].path[0].y/tile_height].monster_memo!=0)
      {
       prisera[f].x=temp_x;prisera[f].y=temp_y;
       if (level[(prisera[f].x)/tile_width][(prisera[f].y)/tile_height].monster_memo==0) level[(prisera[f].x)/tile_width][(prisera[f].y)/tile_height].monster_memo=f;
           prisera[f].path[0].x=(prisera[f].x)/tile_width*tile_width+tile_width_2;
           prisera[f].path[0].y=(prisera[f].y)/tile_height*tile_height+tile_height_2;           
       continue;
       }/**/
   if ((abs(prisera[f].y-prisera[f].path[0].y)<2)&&(abs(prisera[f].x-prisera[f].path[0].x)<2))
   {   
   steps_found=find_path(f,prisera[f].x/tile_width,prisera[f].y/tile_height,prisera[f].cil_x/tile_width,prisera[f].cil_y/tile_height);
    if (steps_found<1)
                      {
                       level[(prisera[f].x)/tile_width][(prisera[f].y)/tile_height].monster_memo=0;
                       prisera[f].path[0].x=0;
                       }
   }
   //udelej si znacku
if (level[(prisera[f].x)/tile_width][(prisera[f].y)/tile_height].monster_memo==0) level[(prisera[f].x)/tile_width][(prisera[f].y)/tile_height].monster_memo=f;
/*
if ((abs(prisera[f].cil_x-prisera[f].x)<10)
  &&(abs(prisera[f].cil_y-prisera[f].y)<10))
   {prisera[f].path[0].x=0;}//jestli uz nasels cos hledal, tak zrus cestu a delej ze uz tam jsi (nasledne v dalsim kroku bude delat rozhlizeni se atd)
/**/
   }
   else
   {
   
// stoji a ceka co se stane
//  -- ma danej aspon cil ? pokud ano, tak ho posli tim smerem

//otoc ho nahodne, pokud ceka uz moc dlouho
if (prisera[f].guard_wait>0) //pokud je v klidu, zacne se rozhlizet
  {
  if ((prisera[f].guard_angle==0)&&
    (abs(prisera[f].guard_x-prisera[f].x)<20)
    &&(abs(prisera[f].guard_y-prisera[f].y)<20)
    &&(prisera[f].guard_wait>60))
      {prisera[f].guard_angle=30-(rand()&60);prisera[f].guard_wait=0;}
   
  if (prisera[f].guard_angle>0) {prisera[f].guard_angle-=1;prisera[f].angle+=5;}
  if (prisera[f].guard_angle<0) {prisera[f].guard_angle+=1;prisera[f].angle-=5;}
  if (prisera[f].angle>360) {prisera[f].angle-=360;}
  if (prisera[f].angle<0) {prisera[f].angle+=360;}
}
else {prisera[f].guard_angle=0;}//nerozhlizej se
/**/

if (prisera[f].cil_x>0)
   {
    if (level[(prisera[f].x)/tile_width][(prisera[f].y)/tile_height].monster_memo==f) level[(prisera[f].x)/tile_width][(prisera[f].y)/tile_height].monster_memo=0;
    steps_found=find_path(f,prisera[f].x/tile_width,prisera[f].y/tile_height,prisera[f].cil_x/tile_width,prisera[f].cil_y/tile_height);
    if (steps_found<2)
                       {
                       level[(prisera[f].x)/tile_width][(prisera[f].y)/tile_height].monster_memo=0;
                       prisera[f].path[0].x=0;
                       }
   if (level[(prisera[f].x)/tile_width][(prisera[f].y)/tile_height].monster_memo==0) level[(prisera[f].x)/tile_width][(prisera[f].y)/tile_height].monster_memo=f;
   }
   else
   {
       //nema cil, ceka a dela co ma delat (cekat ? vratit se na puvodni misto kde hlidal ?)
/*
if (prisera[f].typ==2) {temp=prisera_najdi_viditelnej_cil(f,1);}
if (prisera[f].typ==1) {temp=prisera_najdi_viditelnej_cil(f,2);}
   if (temp>-1) {prisera[f].cil_x=prisera[temp].x;prisera[f].cil_y=prisera[temp].y;}
*/
   prisera[f].cil_x=prisera[f].guard_x;
   prisera[f].cil_y=prisera[f].guard_y;
   prisera[f].cil_vzdalenost=40;
   
//   if ((abs(prisera[f].guard_x-prisera[f].x)<10)&&(abs(prisera[f].guard_y-prisera[f].y)<10)&&(prisera[f].guard_wait>40)) {prisera[f].angle=rand()&360;prisera[f].guard_wait=0;}
   
// if ((prisera[f].guard_x==prisera[f].x)&&(prisera[f].guard_y==prisera[f].y)) {prisera[f].angle=prisera[f].guard_angle;}
   }

   }
  }

//---konec vojaka - 2
/*
 //uprav kurzor mysi
 if ((prisera[f].typ==prisera[aktualni_hrac].typ)&&(abs(prisera[f].x-real_mouse_x)<20)&&(abs(prisera[f].y-real_mouse_y)<20))
  {
  mouse_cursor_texture=4;mouse_cursor_showing=f;
  }/**/

 }
 
//-----------------------------------------

if (prisera[f].typ==3)//je to bublina co dela maso pro kulaty prisery
 {

  if ((prisera[f].data1>-1)&&(prisera[prisera[f].data1].typ>0))//ma koho hledat  
   {

 if (prisera[f].data2==0)
  {
   prisera[f].data2=(random()&3) + 4;prisera[f].cil_x=random()&120;prisera[f].cil_y=random()&100;
   if ((random()&100)>57) {prisera[f].guard_x=1;}else{prisera[f].guard_x=-1;}
   if ((random()&100)>57) {prisera[f].guard_y=1;}else{prisera[f].guard_y=-1;}
   }

 g=prisera[f].data2;//rychlost
 h=100;//oddaleni --puvodni nastaveni je 100

   if (prisera[f].guard_x==1)
    {prisera[f].cil_x=prisera[f].cil_x+g;if (prisera[f].cil_x>h) {prisera[f].cil_x=h;prisera[f].guard_x=-1;}}
    else
    {prisera[f].cil_x=prisera[f].cil_x-g;if (prisera[f].cil_x<(-h)) {prisera[f].cil_x=-h;prisera[f].guard_x=1;}}

   if (prisera[f].guard_y==1)
    {prisera[f].cil_y=prisera[f].cil_y+g;if (prisera[f].cil_y>h) {prisera[f].cil_y=h;prisera[f].guard_y=-1;}}
    else
    {prisera[f].cil_y=prisera[f].cil_y-g;if (prisera[f].cil_y<(-h)) {prisera[f].cil_y=-h;prisera[f].guard_y=1;}}

   prisera[f].x=prisera[prisera[f].data1].x+prisera[f].cil_x/10;
   prisera[f].y=prisera[prisera[f].data1].y+prisera[f].cil_y/10;
  // if ((abs(prisera[f].x-prisera[prisera[f].data1].x)<4)&&(abs(prisera[f].x-prisera[prisera[f].data1].x)>-4)) {prisera[f].cil_x=random()&h;}
   }
   else
   {
   if (prisera[prisera[f].data1].typ==0) {prisera[f].typ=0;}
   //nikoho nenasleduje, ceka a kdyz je nekdo blizko, posune se za nim    
   }
  }/**/
 
if (prisera[f].typ==4)//je to sud co se da strkat
  {//plasma ball
   level[(prisera[f].x)/tile_width][(prisera[f].y)/tile_height].monster_memo=f;
   prisera[f].data2+=10;
   if (prisera[f].data2>360) prisera[f].data2-=360;
  }
  
if ((prisera[f].shockwave_speed>0)&&(prisera[f].typ>0))//ma se hybat
{

level[(prisera[f].x)/tile_width][(prisera[f].y)/tile_height].monster_memo=0;

//zmensi a vynuluj krok, bude se muset spocitat znovu, kvuli nove rychlosti
prisera[f].shockwave_speed--;prisera[f].shockwave_speed_x=0;prisera[f].shockwave_speed_y=0;

  //ma urceny speed_x a speed_y ? pokud ne, tak ho urci
     if ((prisera[f].shockwave_speed_x==0)&&(prisera[f].shockwave_speed_y==0))
       {
       g=(prisera[f].shockwave_speed/10);
       if (g>10) {g=10;}
       prisera[f].shockwave_speed_x = g * cos((float)(M_PI*((float)prisera[f].shockwave_angle))/180);
       prisera[f].shockwave_speed_y = g * sin((float)(M_PI*((float)prisera[f].shockwave_angle))/180);
       }
   //posun ve smeru letu
   //je tam volno ? pokud ano, posun ho tam, pokud ne, znic kulku   
   temp_level=level[(int)(prisera[f].x+prisera[f].shockwave_speed_x)/tile_width][(int)(prisera[f].y+prisera[f].shockwave_speed_y)/tile_height].typ;
      if (//narazil do zdi ?
        (temp_level<9)&& //neni to zed (9)
        (temp_level!=7)//nejsou to dvere
         &&//narazil do prisery?
        (hit_test(f,1)==0)
        )  
      {//posun ho na novou pozici
      prisera[f].x+=prisera[f].shockwave_speed_x;
      prisera[f].y+=prisera[f].shockwave_speed_y;
      }
     else
     {
     // odraz se
     

   g=level[(int)(prisera[f].x+prisera[f].shockwave_speed_x)/tile_width][(int)(prisera[f].y)/tile_height].typ;
   h=level[(int)(prisera[f].x)/tile_width][(int)(prisera[f].y+prisera[f].shockwave_speed_y)/tile_height].typ;


//aby se od kazde zdi zkusil odrazit a nestalo se ze u ni zacne trcet, pak se blbe odrazi..

if ((g>8)||(g==7)) {g=1;prisera[f].shockwave_speed+=5;}
if ((h>8)||(h==7)) {h=1;prisera[f].shockwave_speed+=5;}


     //narazil dole 
     if ((prisera[f].shockwave_speed_y>0)&&(h==1))
        {
         if (prisera[f].shockwave_speed_x<0)
          {prisera[f].shockwave_angle=270-(prisera[f].shockwave_angle-90);}
          else
          {prisera[f].shockwave_angle=270+(90-prisera[f].shockwave_angle);}
         }
     //narazil nahore 
     if ((prisera[f].shockwave_speed_y<0)&&(h==1))
        {
         if (prisera[f].shockwave_speed_x<0)
          {prisera[f].shockwave_angle=90-(prisera[f].shockwave_angle-270);}
          else
          {prisera[f].shockwave_angle=90+(270-prisera[f].shockwave_angle);}
         }
     //narazil vlevo
     if ((prisera[f].shockwave_speed_x<0)&&(g==1))
        {
         if (prisera[f].shockwave_speed_y>0)
          {prisera[f].shockwave_angle=180-prisera[f].shockwave_angle;}
          else
          {prisera[f].shockwave_angle=(180-prisera[f].shockwave_angle);}
         }
     //narazil vpravo
     if ((prisera[f].shockwave_speed_x>0)&&(g==1))
        {
         if (prisera[f].shockwave_speed_y>0)
          {prisera[f].shockwave_angle=180-(prisera[f].shockwave_angle);}
          else
          {prisera[f].shockwave_angle=180+(360-prisera[f].shockwave_angle);}
         }

     
  if ((prisera[f].data3==prisera[f].x)&&(prisera[f].data4==prisera[f].y))
   {
   prisera[f].shockwave_angle-=180;
   }
     
prisera[f].data3=prisera[f].x;//uloz si pozici, pokud bude stejna i priste, tak trci v rohu :(
prisera[f].data4=prisera[f].y;
     
     prisera[f].shockwave_speed_x=0;prisera[f].shockwave_speed_y=0;
    // summon_explode(1,prisera[f].x,prisera[f].y);
     }

 //narazil pri pohybu ?
 for(g=0;g<max_priser;g++)
 {
  if ((prisera[g].typ!=0)&&(f!=g)&&(prisera[g].disable_bounce==0)&&((prisera[g].data1==0)||(prisera[g].typ!=4)))
   {//narazil ?
   if (konflikt_prisera_prisera(f,g)==1)
      {//narazil, postouchni ho kam patri (spocitej uhel a silu)
      //jakej uhel maji ty prisery
      j=360-prisera_uhel(prisera[f].x,prisera[f].y,prisera[g].x,prisera[g].y)-90;
    if (prisera[f].y>prisera[g].y)
     {
      if (prisera[f].x<prisera[g].x)
      {
       h=j;//osa uhlu podle kteryho se to zalomi
       h=prisera[f].shockwave_angle+h*2;
       i=prisera[f].shockwave_angle-h;
       prisera[f].shockwave_speed=prisera[f].shockwave_speed;
       prisera[g].shockwave_speed=prisera[f].shockwave_speed;
       prisera[f].shockwave_angle=h;
       prisera[g].shockwave_angle=i;
       prisera[f].x-=prisera[f].shockwave_speed_x;
       prisera[f].y-=prisera[f].shockwave_speed_y;/**/

       if (prisera[f].typ==4) prisera[f].data1=8;
       if (prisera[g].typ==4) prisera[g].data1=8;
       }
       else
       {
       h=j;//osa uhlu podle kteryho se to zalomi
       h=prisera[f].shockwave_angle-h/2;
       i=prisera[f].shockwave_angle+h;
       prisera[f].shockwave_speed=prisera[f].shockwave_speed;
       prisera[g].shockwave_speed=prisera[f].shockwave_speed;
       prisera[f].shockwave_angle=i;
       prisera[g].shockwave_angle=h;
       prisera[f].x-=prisera[f].shockwave_speed_x;
       prisera[f].y-=prisera[f].shockwave_speed_y;/**/
//prisera[f].typ=0;
       if (prisera[f].typ==4) prisera[f].data1=8;
       if (prisera[g].typ==4) prisera[g].data1=8;
       }
      }
      else
      {
          
      if (prisera[f].x<prisera[g].x)
      {
       h=j;//osa uhlu podle kteryho se to zalomi
       h=prisera[f].shockwave_angle+h;
       i=prisera[f].shockwave_angle-j/3;
       prisera[f].shockwave_speed=prisera[f].shockwave_speed;
       prisera[g].shockwave_speed=prisera[f].shockwave_speed;
       prisera[f].shockwave_angle=h;
       prisera[g].shockwave_angle=i;
       prisera[f].x-=prisera[f].shockwave_speed_x;
       prisera[f].y-=prisera[f].shockwave_speed_y;/**/

       if (prisera[f].typ==4) prisera[f].data1=8;
       if (prisera[g].typ==4) prisera[g].data1=8;
       }
       else
       {//jen zkopirovano z predchoziho IFu, je potreba otestovat a zmenit
       h=j;//osa uhlu podle kteryho se to zalomi
       h=prisera[f].shockwave_angle+h;
       i=prisera[f].shockwave_angle-j/3;
       prisera[f].shockwave_speed=prisera[f].shockwave_speed;
       prisera[g].shockwave_speed=prisera[f].shockwave_speed;
       prisera[f].shockwave_angle=h;
       prisera[g].shockwave_angle=i;
       prisera[f].x-=prisera[f].shockwave_speed_x;
       prisera[f].y-=prisera[f].shockwave_speed_y;/**/

       if (prisera[f].typ==4) prisera[f].data1=8;
       if (prisera[g].typ==4) prisera[g].data1=8;
       }
      }
       
      
      if (prisera[g].shockwave_angle<0) prisera[g].shockwave_angle+=360;
      if (prisera[g].shockwave_angle>360) prisera[g].shockwave_angle-=360;
      if (prisera[f].shockwave_angle<0) prisera[f].shockwave_angle+=360;
      if (prisera[f].shockwave_angle>360) prisera[f].shockwave_angle-=360;
      
      summon_explode(2,(prisera[f].x+prisera[g].x)/2,(prisera[f].y+prisera[g].y)/2);
      }
   }
 }
 if ((prisera[f].data1>0)&&(prisera[f].typ==4)) prisera[f].data1--;
}
 
}  
}

void do_ai_konec() //jen pro rychle nalezeni konce Do_AI
{
     
}

int najdi_nejblizsiho_hrace(int x, int y)
{
int cislo, delka, delka_temp,f;
cislo=-1;
delka=-1;

for (f=0;f<max_priser;f++)
{
if ((f!=aktualni_hrac)&&(prisera[f].typ==prisera[aktualni_hrac].typ)&&(prisera[f].health>0))
    {
    delka_temp=(((prisera[f].x-x)*(prisera[f].x-x))+((prisera[f].y-y)*(prisera[f].y-y)));
    if (delka>delka_temp) {delka=delka_temp;cislo=f;}
    if (delka==-1) {delka=delka_temp;cislo=f;}
    }
}
    
if (cislo==-1) {cislo=aktualni_hrac;}    
prisera[cislo].loading_weapon=0;
return(cislo);
}



void do_keyboard()
{
int temp_x,temp_y,speed,mickeyx, mickeyy,f,g;
int keyrepeat=40;
 

if (mouse_cursor_dblclicked>0) {mouse_cursor_dblclicked--;} else {mouse_cursor_showing=0;}
if (move_button_enable>0) {move_button_enable--;} 
    
if (prisera[aktualni_hrac].health>0)
 {
 kamera_x=prisera[aktualni_hrac].x-my_screen_width/2;
 kamera_y=prisera[aktualni_hrac].y-my_screen_height/2;
 
/*
  temp_x=kamera_x;
  temp_y=kamera_y;
 
 if (prisera[aktualni_hrac].x>mymouse_x)
    {kamera_x=prisera[aktualni_hrac].x - (my_screen_width/2) - ((prisera[aktualni_hrac].x-mymouse_x)/2);}
  else
    {kamera_x=prisera[aktualni_hrac].x - (my_screen_width/2) + ((mymouse_x-prisera[aktualni_hrac].x)/2);}

 if (prisera[aktualni_hrac].y>mymouse_y)
    {kamera_y=prisera[aktualni_hrac].y - (my_screen_height/2) - ((prisera[aktualni_hrac].y-mymouse_y)/2);}
  else
    {kamera_y=prisera[aktualni_hrac].y - (my_screen_height/2) + ((mymouse_y-prisera[aktualni_hrac].y)/2);}
 
 
  kamera_x=prisera[aktualni_hrac].x - my_screen_width/2;
  kamera_y=prisera[aktualni_hrac].y - my_screen_height/2;

  temp_x=temp_x-kamera_x;
  temp_y=temp_y-kamera_y;
 
  //----------
  */
  get_mouse_mickeys(&mickeyx, &mickeyy);
  mymouse_x=mickeyx+mymouse_x;
  mymouse_y=mickeyy+mymouse_y;
  
  
/*
  position_mouse(my_screen_width/2,my_screen_height/2);
  if (mymouse_x>(my_screen_width-5)) {mymouse_x=my_screen_width-5;}
  if (mymouse_x<5) {mymouse_x=5;}
  if (mymouse_y>(my_screen_height-5)) {mymouse_y=my_screen_height-5;}
  if (mymouse_y<5) {mymouse_y=5;}/**/
 }  

// udelej pozici v levelu podle myse a aktualni pozice
real_mouse_x=prisera[aktualni_hrac].x-my_screen_width/2;
real_mouse_y=prisera[aktualni_hrac].y-my_screen_height/2;
if (real_mouse_x<(tile_width*3)) real_mouse_x=tile_width*3;
if (real_mouse_y<(tile_height*3)) real_mouse_y=tile_height*3; 
real_mouse_x+=mymouse_x;
real_mouse_y+=mymouse_y;
/**/


  //ovladani klavesnice
  temp_x=0;temp_y=0;

  speed=prisera[aktualni_hrac].speed-(level[(prisera[aktualni_hrac].x)/tile_width][(prisera[aktualni_hrac].y)/tile_height].speed/10);
if (prisera[aktualni_hrac].health>0)
  {
  if ((key[KEY_A])&&(level[(prisera[aktualni_hrac].x-speed-prisera[aktualni_hrac].radius)/tile_width][(prisera[aktualni_hrac].y)/tile_height].typ<8)) {temp_x=-speed;}
  if ((key[KEY_D])&&(level[(prisera[aktualni_hrac].x+speed+prisera[aktualni_hrac].radius)/tile_width][(prisera[aktualni_hrac].y)/tile_height].typ<8)) {temp_x=speed;}
  if ((key[KEY_W])&&(level[(prisera[aktualni_hrac].x)/tile_width][(prisera[aktualni_hrac].y-speed-prisera[aktualni_hrac].radius)/tile_height].typ<8)) {temp_y=-speed;}
  if ((key[KEY_S])&&(level[(prisera[aktualni_hrac].x)/tile_width][(prisera[aktualni_hrac].y+speed+prisera[aktualni_hrac].radius)/tile_height].typ<8)) {temp_y=speed;}
  }
  if (key[KEY_LSHIFT]&&(mykey[KEY_LSHIFT]==0)) {if (show_info==0) {show_info=1;} else {show_info=0;}}
  if (key[KEY_R]&&(mykey[KEY_R]==0)) {prisera_edit_actual_variable++;if (prisera_edit_actual_variable>4) prisera_edit_actual_variable=0;}

  if (key[KEY_INSERT]&&(mykey[KEY_INSERT]==0)&&(!key[KEY_LSHIFT]))
     {
     level[(real_mouse_x)/tile_width][(real_mouse_y)/tile_height].monster=prisera_edit.typ;
     level[(real_mouse_x)/tile_width][(real_mouse_y)/tile_height].data1=prisera_edit.data1;
     level[(real_mouse_x)/tile_width][(real_mouse_y)/tile_height].data2=prisera_edit.data2;
     level[(real_mouse_x)/tile_width][(real_mouse_y)/tile_height].data3=prisera_edit.data3;
     level[(real_mouse_x)/tile_width][(real_mouse_y)/tile_height].data4=prisera_edit.data4;     
     }
  if (key[KEY_INSERT]&&(mykey[KEY_INSERT]==0)&&(key[KEY_LSHIFT]))
     {
     show_info=1;
     prisera_edit.typ=level[(real_mouse_x)/tile_width][(real_mouse_y)/tile_height].monster;
     prisera_edit.data1=level[(real_mouse_x)/tile_width][(real_mouse_y)/tile_height].data1;
     prisera_edit.data2=level[(real_mouse_x)/tile_width][(real_mouse_y)/tile_height].data2;
     prisera_edit.data3=level[(real_mouse_x)/tile_width][(real_mouse_y)/tile_height].data3;
     prisera_edit.data4=level[(real_mouse_x)/tile_width][(real_mouse_y)/tile_height].data4;
     }
  if (key[KEY_DEL]&&(mykey[KEY_DEL]==0))
     {level[(real_mouse_x)/tile_width][(real_mouse_y)/tile_height].monster=0;level[(real_mouse_x)/tile_width][(real_mouse_y)/tile_height].data1=0;level[(real_mouse_x)/tile_width][(real_mouse_y)/tile_height].data2=0;level[(real_mouse_x)/tile_width][(real_mouse_y)/tile_height].data3=0;level[(real_mouse_x)/tile_width][(real_mouse_y)/tile_height].data4=0;}



if ((show_info==1)&&(key_shifts & KB_CTRL_FLAG))
   {
    f=mymouse_x/3-20;
    if (prisera_edit_actual_variable==0) {prisera_edit.typ=mymouse_x/10-20;;}
    if (prisera_edit_actual_variable==1) {prisera_edit.data1=f;}
    if (prisera_edit_actual_variable==2) {prisera_edit.data2=f;}
    if (prisera_edit_actual_variable==3) {prisera_edit.data3=f;}
    if (prisera_edit_actual_variable==4) {prisera_edit.data4=f;}
   }
if ((show_info==1)&&key[KEY_F]&&(mykey[KEY_F]==0)) {if (prisera_edit_actual_variable==0) {prisera_edit.typ++;}if (prisera_edit_actual_variable==1) {prisera_edit.data1++;}if (prisera_edit_actual_variable==2) {prisera_edit.data2++;}if (prisera_edit_actual_variable==3) {prisera_edit.data3++;}if (prisera_edit_actual_variable==4) {prisera_edit.data4++;}}
if ((show_info==1)&&key[KEY_V]&&(mykey[KEY_V]==0)) {if (prisera_edit_actual_variable==0) {prisera_edit.typ--;}if (prisera_edit_actual_variable==1) {prisera_edit.data1--;}if (prisera_edit_actual_variable==2) {prisera_edit.data2--;}if (prisera_edit_actual_variable==3) {prisera_edit.data3--;}if (prisera_edit_actual_variable==4) {prisera_edit.data4--;}}
  
  if ((key[KEY_ENTER])&&(mykey[KEY_ENTER]==0)) {compile_level();}
  if (key[KEY_Z]) {level[(real_mouse_x)/tile_width][(real_mouse_y)/tile_height].typ=0;generuj_pruchodnost();}
  if (key[KEY_X]) {level[(real_mouse_x)/tile_width][(real_mouse_y)/tile_height].typ=9;generuj_pruchodnost();}
  if (key[KEY_C]) {level[(real_mouse_x)/tile_width][(real_mouse_y)/tile_height].typ=8;generuj_pruchodnost();}
//  if (key[KEY_V]) {level[(real_mouse_x)/tile_width][(real_mouse_y)/tile_height].typ=7;generuj_pruchodnost();}
  
  if (key[KEY_1]) {prisera[aktualni_hrac].weapon=1;}
  if (key[KEY_2]) {prisera[aktualni_hrac].weapon=2;}
  if (key[KEY_3]) {prisera[aktualni_hrac].weapon=3;}
  if (key[KEY_4]) {prisera[aktualni_hrac].weapon=4;}
  if (key[KEY_5]) {prisera[aktualni_hrac].weapon=5;}

  if ((key[KEY_F2])&&(mykey[KEY_F2]==0)) {save_level(1);}
  if ((key[KEY_F9])&&(mykey[KEY_F9]==0)) {load_level(1);}


  
  if ((key[KEY_E])&&(mykey[KEY_E]==0)) {aktualni_hrac=najdi_nejblizsiho_hrace(real_mouse_x,real_mouse_y);prisera[aktualni_hrac].cil_x=0;}
  
  
  
  if ((temp_x!=0)||(temp_y!=0))//ma novou pozici
   {
   prisera[aktualni_hrac].cil_x=0;//aby vyradil pathfinding
   prisera[aktualni_hrac].path[0].x=0;
   if (level[prisera[aktualni_hrac].x/tile_width][prisera[aktualni_hrac].y/tile_height].monster_memo==aktualni_hrac) {level[prisera[aktualni_hrac].x/tile_width][prisera[aktualni_hrac].y/tile_height].monster_memo=0;}
    
   if (
       (level[(prisera[aktualni_hrac].x+temp_x)/tile_width][(prisera[aktualni_hrac].y)/tile_height].typ<9)
       &&
       (
        (level[(prisera[aktualni_hrac].x+temp_x)/tile_width][(prisera[aktualni_hrac].y)/tile_height].monster_memo==0)
        ||
        (prisera[level[(prisera[aktualni_hrac].x+temp_x)/tile_width][(prisera[aktualni_hrac].y)/tile_height].monster_memo].typ==prisera[aktualni_hrac].typ)
       )
      )
     {prisera[aktualni_hrac].x+=(temp_x);}
      
   if (
       (level[(prisera[aktualni_hrac].x)/tile_width][(prisera[aktualni_hrac].y+temp_y)/tile_height].typ<9)
       &&
       (
        (level[(prisera[aktualni_hrac].x)/tile_width][(prisera[aktualni_hrac].y+temp_y)/tile_height].monster_memo==0)
        ||
        (prisera[level[(prisera[aktualni_hrac].x)/tile_width][(prisera[aktualni_hrac].y+temp_y)/tile_height].monster_memo].typ==prisera[aktualni_hrac].typ)
       )
      )
     {prisera[aktualni_hrac].y+=(temp_y);}

   if (level[prisera[aktualni_hrac].x/tile_width][prisera[aktualni_hrac].y/tile_height].monster_memo==0) {level[prisera[aktualni_hrac].x/tile_width][prisera[aktualni_hrac].y/tile_height].monster_memo=aktualni_hrac;}
   }
/**/   
   
   
//vycisti klavesy ktery nejsou zmacknuty
for (f=0;f<128;f++)
{
if (mykey[f]>0) {mykey[f]--;}
else
if (key[f]) {mykey[f]=keyrepeat;}
if (!key[f]) {mykey[f]=0;}
}
 
 
}




void load_graphics()
{
char radka[81],jmeno[81];
FILE *soubor;
BITMAP *temp_bitmap,*small_temp_bitmap;
int f,g,texture;/*pocitadlo textur*/


actual_blood_splat=0;


set_log_string("loading floors...");
strcpy(jmeno,"gfx/level_graphics/floor1.tga\0");
//jmeno[strlen(radka)-1]='\0';/*prida konec retezce (jinak by byla delka nahodna)*/
exists(jmeno);
if (errno>0){
             set_log_string("error reading level floor");
             }

 temp_bitmap = load_tga(jmeno,NULL);
//nakouskuj to na 64x64 a postupne to davej do textur
texture=0;
for (g=0;g<temp_bitmap->h/128;g++)
for (f=0;f<temp_bitmap->w/128;f++)
{
small_temp_bitmap=create_bitmap_ex(32,128,128);// vytvor odkladaci bitmapu
clear_to_color(small_temp_bitmap,makecol32(255,0,255));//vymaz odkladaci bitmapu
blit(temp_bitmap,small_temp_bitmap,f*128,g*128,0,0,256,512);
gl_floors[texture] = allegro_gl_make_texture_ex(AGL_TEXTURE_HAS_ALPHA, small_temp_bitmap, GL_RGBA); 
destroy_bitmap(small_temp_bitmap);//zrus odkladaci bitmapu
texture++;//zvetsi pocitadlo
}
destroy_bitmap(temp_bitmap);//zrus to co se nahravalo jako cela bitmapa
set_log_string("floors loaded...");

//load walls
set_log_string("loading walls...");
strcpy(jmeno,"gfx/level_graphics/wall1.tga\0");
//jmeno[strlen(radka)-1]='\0';/*prida konec retezce (jinak by byla delka nahodna)*/
exists(jmeno);
if (errno>0){
             set_log_string("error reading level walls");
             }

 temp_bitmap = load_tga(jmeno,NULL);
//nakouskuj to na 128x128 a postupne to davej do textur
texture=0;
for (g=0;g<temp_bitmap->h/128;g++)
for (f=0;f<temp_bitmap->w/128;f++)
{
small_temp_bitmap=create_bitmap_ex(32,128,128);// vytvor odkladaci bitmapu
clear_to_color(small_temp_bitmap,makecol32(255,0,255));//vymaz odkladaci bitmapu
blit(temp_bitmap,small_temp_bitmap,f*128,g*128,0,0,256,512);
gl_walls[texture] = allegro_gl_make_texture_ex(AGL_TEXTURE_HAS_ALPHA, small_temp_bitmap, GL_RGBA); 
destroy_bitmap(small_temp_bitmap);//zrus odkladaci bitmapu
texture++;//zvetsi pocitadlo
}
destroy_bitmap(temp_bitmap);//zrus to co se nahravalo jako cela bitmapa
set_log_string("walls loaded...");


//load wallmasks
set_log_string("loading wallmasks...");
strcpy(jmeno,"gfx/level_graphics/wall-mask1.tga\0");
//jmeno[strlen(radka)-1]='\0';/*prida konec retezce (jinak by byla delka nahodna)*/
exists(jmeno);
if (errno>0){
             set_log_string("error reading level wallmasks");
             }

 temp_bitmap = load_tga(jmeno,NULL);
//nakouskuj to na 128x128 a postupne to davej do textur
texture=0;
for (g=0;g<temp_bitmap->h/128;g++)
for (f=0;f<temp_bitmap->w/128;f++)
{
small_temp_bitmap=create_bitmap_ex(32,128,128);// vytvor odkladaci bitmapu
clear_to_color(small_temp_bitmap,makecol32(255,0,255));//vymaz odkladaci bitmapu
blit(temp_bitmap,small_temp_bitmap,f*128,g*128,0,0,256,512);
gl_wallmasks[texture] = allegro_gl_make_texture_ex(AGL_TEXTURE_HAS_ALPHA, small_temp_bitmap, GL_RGBA); 
destroy_bitmap(small_temp_bitmap);//zrus odkladaci bitmapu
texture++;//zvetsi pocitadlo
}
destroy_bitmap(temp_bitmap);//zrus to co se nahravalo jako cela bitmapa
set_log_string("wallmasks loaded...");




//load particles
set_log_string("loading particles...");
strcpy(jmeno,"gfx/particles.tga\0");
//jmeno[strlen(radka)-1]='\0';/*prida konec retezce (jinak by byla delka nahodna)*/
exists(jmeno);
if (errno>0){
             set_log_string("error reading level particles");
             }

 temp_bitmap = load_tga(jmeno,NULL);
//nakouskuj to na 128x128 a postupne to davej do textur
texture=1;
for (g=0;g<temp_bitmap->h/128;g++)
for (f=0;f<temp_bitmap->w/128;f++)
{
small_temp_bitmap=create_bitmap_ex(32,128,128);// vytvor odkladaci bitmapu
clear_to_color(small_temp_bitmap,makecol32(255,0,255));//vymaz odkladaci bitmapu
blit(temp_bitmap,small_temp_bitmap,f*128,g*128,0,0,256,512);
gl_particle[texture] = allegro_gl_make_texture_ex(AGL_TEXTURE_HAS_ALPHA, small_temp_bitmap, GL_RGBA); 
destroy_bitmap(small_temp_bitmap);//zrus odkladaci bitmapu
texture++;//zvetsi pocitadlo
}
destroy_bitmap(temp_bitmap);//zrus to co se nahravalo jako cela bitmapa
set_log_string("particles loaded...");



//load particles
set_log_string("loading sprites...");
strcpy(jmeno,"gfx/sprites.tga\0");
//jmeno[strlen(radka)-1]='\0';/*prida konec retezce (jinak by byla delka nahodna)*/
exists(jmeno);
if (errno>0){
             set_log_string("error reading level sprites");
             }

 temp_bitmap = load_tga(jmeno,NULL);
//nakouskuj to na 128x128 a postupne to davej do textur
texture=0;
for (g=0;g<temp_bitmap->h/128;g++)
for (f=0;f<temp_bitmap->w/128;f++)
{
small_temp_bitmap=create_bitmap_ex(32,128,128);// vytvor odkladaci bitmapu
clear_to_color(small_temp_bitmap,makecol32(255,0,255));//vymaz odkladaci bitmapu
blit(temp_bitmap,small_temp_bitmap,f*128,g*128,0,0,256,512);
gl_sprite[texture] = allegro_gl_make_texture_ex(AGL_TEXTURE_HAS_ALPHA, small_temp_bitmap, GL_RGBA); 
destroy_bitmap(small_temp_bitmap);//zrus odkladaci bitmapu
texture++;//zvetsi pocitadlo
}
destroy_bitmap(temp_bitmap);//zrus to co se nahravalo jako cela bitmapa
set_log_string("sprites loaded...");



//load textures
set_log_string("loading textures...");
strcpy(jmeno,"gfx/textures.tga\0");
//jmeno[strlen(radka)-1]='\0';/*prida konec retezce (jinak by byla delka nahodna)*/
exists(jmeno);
if (errno>0){
             set_log_string("error reading level textures");
             }

 temp_bitmap = load_tga(jmeno,NULL);
//nakouskuj to na 128x128 a postupne to davej do textur
texture=0;
for (g=0;g<temp_bitmap->h/128;g++)
for (f=0;f<temp_bitmap->w/128;f++)
{
small_temp_bitmap=create_bitmap_ex(32,128,128);// vytvor odkladaci bitmapu
clear_to_color(small_temp_bitmap,makecol32(255,0,255));//vymaz odkladaci bitmapu
blit(temp_bitmap,small_temp_bitmap,f*128,g*128,0,0,256,512);
gl_textures[texture] = allegro_gl_make_texture_ex(AGL_TEXTURE_HAS_ALPHA, small_temp_bitmap, GL_RGBA); 
destroy_bitmap(small_temp_bitmap);//zrus odkladaci bitmapu
texture++;//zvetsi pocitadlo
}
destroy_bitmap(temp_bitmap);//zrus to co se nahravalo jako cela bitmapa
set_log_string("textures loaded...");


}
 
 

void unload_graphics()
{
int f;
for (f=0;f<500;f++) if (gl_textures[f]) glDeleteTextures(1, &gl_textures[f]);
for (f=0;f<500;f++) if (gl_floors[f]) glDeleteTextures(1, &gl_floors[f]);
for (f=0;f<500;f++) if (gl_walls[f]) glDeleteTextures(1, &gl_walls[f]);
for (f=0;f<500;f++) if (gl_wallmasks[f]) glDeleteTextures(1, &gl_wallmasks[f]);
for (f=0;f<500;f++) if (gl_particle[f]) glDeleteTextures(1, &gl_particle[f]);
for (f=0;f<500;f++) if (gl_sprite[f]) glDeleteTextures(1, &gl_sprite[f]);

 }



int mygame(int level_cislo)
{
int f,g;
BITMAP *temp_bitmap=NULL;


// vycisti klavesnicovej buffer a nastav promenny pro mys
for (f=0;f<256;f++) {mykey[f]=0;}
mouse_cursor_showing=-1;
mouse_cursor_texture=3;
mouse_cursor_dblclick=0;
mouse_cursor_dblclicked=0;
mouse_holding=0;

show_info=0;
move_button_enable=0;
monster_counter=0;object_counter=0;particle_counter=0;

load_graphics();

mymouse_x=my_screen_width/2;
mymouse_y=my_screen_height/2;

//generuj level
generate_level();
set_log_string("level generated...");

load_level(1);
set_log_string("level loaded...");

compile_level();
set_log_string("level compiled...");


kamera_x=0;kamera_y=0;
fps=0;
fps_show=0;
paths_in_a_second=0;


int game_running=0; //dokud je nula, nevykresluje se nic na obrazovku (aby proslo aspon jednou vsechno logikou hry)
intrpt_game=1;//aby nezacilan od nuly s logikou, ale udelal ju hned v prnim pruchodu (a pak je vykresleni, ktery uz bude vedet co ma kreslit)

//nastav pocitadla, nez se zacne hrat (aby se proverilo ze kazdej interupt probehne)
intrpt_video=1;keyboard_cycler=1;intrpt_game=1;

do
{
rest(1);

fps++;
if (intrpt_video>0) {intrpt_video=0;fps_show=fps;fps=0;paths_in_a_second=0;}


if(intrpt_game>0)
 {
  generate_shadows();//vygeneruj pole shadows podle toho kde ted stoji a kdo tam sviti

  intrpt_game--;
  if ((mouse_b & 2)&&(move_button_enable==0))
  {
   move_button_enable=20;
   if ((prisera[aktualni_hrac].health>0)&&(level[(mymouse_x+kamera_x)/tile_width][(mymouse_y+kamera_y)/tile_height].typ<9)&&(level[(mymouse_x+kamera_x)/tile_width][(mymouse_y+kamera_y)/tile_height].monster_memo==0))
       prisera[aktualni_hrac].cil_x=((mymouse_x+kamera_x)/tile_width)*tile_width+tile_width_2;
       prisera[aktualni_hrac].cil_y=((mymouse_y+kamera_y)/tile_height)*tile_height+tile_height_2;
       prisera[aktualni_hrac].guard_x=prisera[aktualni_hrac].cil_x;
       prisera[aktualni_hrac].guard_y=prisera[aktualni_hrac].cil_y;
       summon_particle(7,mymouse_x+kamera_x,mymouse_y+kamera_y,0,31,31,0,0);
  }
  
  
  if (mouse_b & 1) //kliknul mysi
  {
  if (mouse_cursor_showing==-1) //neukazuje na nic, tak vystrel
   {if (prisera[aktualni_hrac].health>0) vystrel();}
   else
   {
   
   if (mouse_holding==0)
   {
    mouse_holding=1;
     //zjisti jeslti je doubleclick
     if (mouse_cursor_dblclicked>0)
        { // byl dvojklik
         mouse_cursor_dblclick=1;
         mouse_cursor_dblclicked=0;
         summon_explode(1,prisera[mouse_cursor_showing].x,prisera[mouse_cursor_showing].y);
         //vyhodnot doubleclick a pak
         mouse_cursor_dblclick=0;
         mouse_cursor_showing=0;
         }
          else
        { // nebyl dvojklik
         mouse_cursor_dblclicked=40;
         mouse_cursor_dblclick=0;
         //vyhodnot jeden klik
         }
//   do {} while (mouse_b & 1);
     }
    } 
  }else {mouse_holding=0;}
   
//  set_log_string("delej keyboard");
  do_keyboard();
//  set_log_string("delej objekty");
  do_objects();//kulky, vybuchy, ...
//  set_log_string("delej particly");
  do_particles();//particly
//  set_log_string("delej ai");
  do_ai();//jen postavy a jejich pohyb
//  set_log_string("hotovo cyklus");
  game_running=1;
 }

if (game_running>0) vykresli_video();  
 
}
while(!(key[KEY_ESC])&&(quitting==0));
  
unload_graphics();

}




//-------------------------------------------------
//--------------HLAVNI INICIALIZACE ---------------
//-------------------------------------------------


void nastav_opengl()
{
         
//Setting our.. well... settings.  =)  Read the AllegroGL documentation 
//for a more in-depth explanation for what these all do, but they are 
//pretty self explanatory.
//Note: Z_DEPTH isn't mandatory for 2D drawing.  It's only useful for 
//sorting 3D objects, so when you draw an object behind another, it 
//doesn't get drawn on top of it.  You CAN use it for 2D drawing, 
//but it's REALLY not necessary.
set_log_string("openGL:allegro_gl_set(AGL_Z_DEPTH, 8);");
  allegro_gl_set(AGL_Z_DEPTH, 8); 
set_log_string("openGL:allegro_gl_set(AGL_COLOR_DEPTH, my_screen_bpp);");
  allegro_gl_set(AGL_COLOR_DEPTH, my_screen_bpp);
set_log_string("openGL:allegro_gl_set(AGL_SUGGEST, AGL_Z_DEPTH | AGL_COLOR_DEPTH);");
  allegro_gl_set(AGL_SUGGEST, AGL_Z_DEPTH | AGL_COLOR_DEPTH);
set_log_string("openGL:allegro_gl_set(AGL_REQUIRE, AGL_DOUBLEBUFFER);");
  allegro_gl_set(AGL_REQUIRE, AGL_DOUBLEBUFFER);

//Setting the graphics mode:
//  set_gfx_mode(GFX_OPENGL_WINDOWED, 640, 480, 0, 0);
  //set_gfx_mode(GFX_OPENGL_WINDOWED, 800, 600, 0, 0);

//I am setting a state where I am editing the projection matrix...
  glMatrixMode(GL_PROJECTION);
//  glMatrixMode(GL_TEXTURE);// jak tohle pouzit ???
  

//Clearing the projection matrix...
  glLoadIdentity();

//Creating an orthoscopic view matrix going from -1 -> 1 in each
//dimension on the screen (x, y, z). 
//set_log_string("openGL:glOrtho(0, 640, 480, 0, -1, 1);");
set_log_string("openGL:glOrtho(0, my_screen_width, my_screen_height, 0, -1, 1);");
  glOrtho(0, my_screen_width, my_screen_height, 0, -1, 1);




/*
  set_color_depth(32);mysha = load_bitmap("mysha.pcx", NULL);
  texture = create_sub_bitmap(mysha, 84, 84, 64, 64);
  tex[0] = allegro_gl_make_texture_ex(AGL_TEXTURE_MASKED, texture, GL_RGBA);/**/
set_log_string("openGL:agl_font = allegro_gl_convert_allegro_font_ex(font,AGL_FONT_TYPE_TEXTURED, -1.0, GL_INTENSITY8);");

GLint format = allegro_gl_get_texture_format(NULL);
//format=GL_INTENSITY8
  agl_font = allegro_gl_convert_allegro_font_ex(font,AGL_FONT_TYPE_TEXTURED, -1.0, format);


    
//Now editing the model-view matrix.
set_log_string("openGL:glMatrixMode(GL_MODELVIEW);");
  glMatrixMode(GL_MODELVIEW);

//Clearing the model-view matrix.
set_log_string("openGL:glLoadIdentity();");
  glLoadIdentity();

//Disabling the depth test (z will not be used to tell what object 
//will be shown above another, only the order in which I draw them.)
set_log_string("openGL:glDisable(GL_DEPTH_TEST)");
  glDisable(GL_DEPTH_TEST);


//-----------------------------------
//Begin drawing quads
set_log_string("openGL:glEnable(GL_TEXTURE_2D);");
glEnable(GL_TEXTURE_2D);


// patri to sem ?
set_log_string("openGL:glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);");
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
set_log_string("openGL:glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);");
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);


}

void myexit()
{
 quitting=1;   
}

int main()
{
 quitting=0;
    int f;
    unsigned char temp_string[100];
  set_uformat(U_ASCII);//kvuli fontu    
  allegro_init();
  install_keyboard();
  install_allegro_gl(); 
  install_mouse();
  
  set_close_button_callback(*myexit);  
  set_window_title("MeinCannon v 0.3");

  set_config_file("setup.cfg");

  enable_log_file=get_config_int("program_data","enable_log_file",0);
  real_screen_width=get_config_int("program_data","screen_width",640);
  real_screen_height=get_config_int("program_data","screen_height",480);
  my_screen_width=800;
  my_screen_height=600;
  tile_width=40;
  tile_height=40;
  tile_width_2=tile_width/2;
  tile_height_2=tile_height/2;

  
  windowed=get_config_int("program_data","windowed",0);
  my_screen_bpp=get_config_int("program_data","video_bpp",16);
  my_screen_rate=get_config_int("program_data","refresh_rate",60);

  position_mouse(real_screen_width/2,real_screen_height/2);
  set_mouse_speed(120,120);
show_mouse(NULL);
  
if (enable_log_file)
        {
        //vymaz starej log soubor
        logfile=fopen("log_file","wb");
        fclose(logfile);
        }
   set_log_string("spusteni hry");

request_refresh_rate(my_screen_rate);

set_color_depth(my_screen_bpp);
set_log_string("color_depth_set.");
allegro_gl_set(AGL_COLOR_DEPTH, my_screen_bpp);
set_log_string("allegro_gl_set");



if (windowed==0) {windowed=GFX_OPENGL;} else {windowed=GFX_OPENGL_WINDOWED;} 
//if (set_gfx_mode(windowed,my_screen_width,my_screen_height,0,0)!=0)
if (set_gfx_mode(windowed,real_screen_width,real_screen_height,0,0)!=0)
 {
  set_log_string("ERROR:Nepodarilo se nastavit rozliseni.\n");
  exit(1);
  }
  
set_log_string("Nastav openGL");
nastav_opengl();
set_log_string("OpenGL nastaveno.");



set_log_string("Detekuji zvukovku.");
//install_sound(DIGI_WAVOUTID(0),MIDI_AUTODETECT,"a");
//install_sound(DIGI_DIRECTAMX(0),MIDI_AUTODETECT,"a");
voices_allocated=detect_digi_driver(DIGI_DIRECTX(0));
sprintf(temp_string,"Detekoval %i kanalu.",voices_allocated);
reserve_voices(16,1);
voices_allocated=16;
install_sound(DIGI_DIRECTX(0),MIDI_AUTODETECT,"a");

//nastavi jak nahravat a prevadet obrazky a sprity
set_color_conversion(COLORCONV_TOTAL);


set_log_string("Spoustime hru ...");


//nastav si timer na vsechnu grafiku
install_int_ex((*intrpt_video_proc), BPS_TO_TIMER(1));
install_int_ex((*intrpt_game_proc), BPS_TO_TIMER(50));

//menu_main();

mydraw_rect(0,0, 0,0, my_screen_width,0, my_screen_width,my_screen_height, 0,my_screen_height, 0,0,0,255);
allegro_gl_printf(agl_font, my_screen_width/2-30,my_screen_height/2 -10, 0, makecol(255, 255, 255), "LOADING...");
allegro_gl_flip();
mygame(1);

set_log_string("Konci hru ...");

set_gfx_mode(GFX_TEXT,80,25,0,0);
 return(0);
}
END_OF_MAIN();
