#include "all.h"
#include "math.h"
#include <stdint.h>
#include <string>
hgeResourceManager *cGame::resman = NULL;
hgeSprite *grid_sel1 = NULL;
hgeSprite *grid_sel2 = NULL;
cGame *cGame::instance = NULL;
hgeFont *cGame::fnt1 = NULL;
cProjectile *cGame::proj = NULL;
int cGame::locklvl = 0;
st_global cGame::global;
int cGame::turn = 0;

const int c1 = 0;
const int c2 = 64;
const int c3 = 0;
const int c4 = 64;
const int INT32_MAX = 2147483647;

HTEXTURE cGame::drawgrid(){
    HTEXTURE tex = hge->Texture_Create(1024,768);
    DWORD *color = hge->Texture_Lock(tex);

    memset(color, 0, 1024*768*sizeof(DWORD));

    //ver
    for (int i = 0; i < 16; i++)
        for (int j = 64; j < 768-2*64; j++)
            color[j*1024+i*64] = ARGB(round(255.0*j/768.0),0,0,0);
        //hor
    for (int i = 64; i < 1024-64; i++)
        for (int j = 0; j < 11; j++)
            color[j*1024*64+i] = ARGB(round(255.0*j*64.0/768.0),0,0,0);

    hge->Texture_Unlock(tex);
    return tex;
}

/// Game
cGame::cGame(){
    doQuit = false;
    iface = NULL;
    instance = this;
    hge = hgeCreate(HGE_VERSION);
    resman = new hgeResourceManager("Res\\allres.txt");
    Init();
    maxlvl = 1;

    fnt1 = resman->GetFont("font2");
    fnt1->SetColor(0xFFFFFFFF);

    music = resman->GetStream("main_theme");
    muschan = hge->Stream_Play(music, true, musicvolume);


    mmenubg = resman->GetSprite("mmenu");
    chlvl = resman->GetSprite("chlvl");

    spr = resman->GetSprite("white");
    bgd = resman->GetSprite("map");
    cur = resman->GetSprite("cursor");
    aim = resman->GetSprite("cursor_attack");
    cellglow_rose = resman->GetSprite("cellglow_rose");
    cellglow_rose_2x = resman->GetSprite("cellglow_rose_2x");
    glowspr = resman->GetSprite("glow1");

    mmdistort = new hgeDistortionMesh(16,16);
    mmdistort->SetTexture(resman->GetTexture("mmenu"));
    mmdistort->SetTextureRect(0,0,1024,768);
    mmdistort->SetBlendMode(BLEND_COLORADD | BLEND_ALPHABLEND | BLEND_ZWRITE);
    mmdistort->Clear(0xFF000000);

    grid = new hgeSprite(drawgrid(), 0,0,1024,768);
    grid->SetHotSpot(1024/2, 768/2);

    grid_sel1 = resman->GetSprite("sel1");
    grid_sel2 = resman->GetSprite("sel2");

    gui = new hgeGUI();
    gui->AddCtrl(new guiButton(1, 428,500, 168,43,resman->GetSprite("button1"), resman->GetSprite("button1Over")));
    gui->AddCtrl(new guiButton(2, 428,550, 168,43,resman->GetSprite("button2"), resman->GetSprite("button2Over")));
    gui->AddCtrl(new guiButton(3, 428,600, 168,43,resman->GetSprite("button3"), resman->GetSprite("button3Over")));

    chlvlgui = new hgeGUI();
    //chlvlgui->AddCtrl(new guiButton(4, 470,255, 168,43,resman->GetSprite("chlvl1"), resman->GetSprite("chlvl1Over")));
    //chlvlgui->AddCtrl(new guiButton(5, 470,460, 168,43,resman->GetSprite("chlvl2"), resman->GetSprite("chlvl2Over")));
    //chlvlgui->AddCtrl(new guiButton(6, 470,650, 168,43,resman->GetSprite("chlvl3"), resman->GetSprite("chlvl3Over")));
    chlvlgui->AddCtrl(new guiButton(1, 137,83, 37,37,resman->GetSprite("chlvlbut"), resman->GetSprite("chlvlbutover")));
    chlvlgui->AddCtrl(new guiButton(2, 202,170,37,37,resman->GetSprite("chlvlbut"), resman->GetSprite("chlvlbutover")));
    chlvlgui->AddCtrl(new guiButton(3, 283,236, 37,37,resman->GetSprite("chlvlbut"), resman->GetSprite("chlvlbutover")));
    chlvlgui->AddCtrl(new guiButton(4, 405,258, 37,37,resman->GetSprite("chlvlbut"), resman->GetSprite("chlvlbutover")));
    chlvlgui->AddCtrl(new guiButton(5, 581,339, 37,37,resman->GetSprite("chlvlbut"), resman->GetSprite("chlvlbutover")));
    chlvlgui->AddCtrl(new guiButton(6, 659,449, 37,37,resman->GetSprite("chlvlbut"), resman->GetSprite("chlvlbutover")));
    chlvlgui->AddCtrl(new guiButton(7, 724,558, 37,37,resman->GetSprite("chlvlbut"), resman->GetSprite("chlvlbutover")));
    chlvlgui->AddCtrl(new guiButton(8, 848,618, 37,37,resman->GetSprite("chlvlbut"), resman->GetSprite("chlvlbutover")));

    chlvlgui->AddCtrl(new guiButton(10, 100,650, 168,43,resman->GetSprite("optionsback"), resman->GetSprite("optionsbackover")));

    guioptions = new hgeGUI();
    guioptions->AddCtrl(new guiButton(7, 428,600, 168,36,resman->GetSprite("optionsback"), resman->GetSprite("optionsbackover")));
    guioptions->AddCtrl(sound_sld= new hgeGUISlider(8, 178, 500, 160,39, resman->GetTexture("slider"), 0,0,19,19));
    guioptions->AddCtrl(music_sld= new hgeGUISlider(9, 428, 500, 160,39, resman->GetTexture("slider"), 0,0,19,19));
    guioptions->AddCtrl(speed_sld= new hgeGUISlider(10,678, 500, 160,39, resman->GetTexture("slider"), 0,0,19,19));
    music_sld->SetMode(0,100,HGESLIDER_SLIDER);
    music_sld->SetValue(musicvolume);
    sound_sld->SetMode(0,100,HGESLIDER_SLIDER);
    speed_sld->SetMode(0,100,HGESLIDER_SLIDER);
    sliderbg1 = resman->GetSprite("bgslider1");
    sliderbg2 = resman->GetSprite("bgslider2");
    sliderbg3 = resman->GetSprite("bgslider3");

    selcell= NULL;
    selunit= NULL;
    turn = 0;
    /*unit = new cAqua();
    unit->SetParent(GetCell(tcellpos(1,5)));
    unit->SetPosition(1,8);
    units.push_back(unit);
    unit = new cAqua();
    unit->SetParent(GetCell(tcellpos(1,6)));
    unit->SetPosition(2,8);
    units.push_back(unit);
    unit = new cAqua();
    unit->SetParent(GetCell(tcellpos(1,7)));
    unit->SetPosition(3,8);
    units.push_back(unit);
    unit = new cAqua();
    unit->SetParent(GetCell(tcellpos(1,8)));
    unit->SetPosition(4,8);
    units.push_back(unit);
    */
/*
    unit = new cBati();
    unit->SetParent(GetCell(tcellpos(1,1)));
    unit->SetPosition(5,8);
    units.push_back(unit);

    unit = new cTorp();
    unit->SetParent(GetCell(tcellpos(1,1)));
    unit->SetPosition(5,8);
    units.push_back(unit);*/

    /*unit = new cPaul();
    unit->SetParent(GetCell(tcellpos(12,5)));
    unit->SetPosition(6,8);
    units.push_back(unit);*/
/*
    unit = new cPaul();
    unit->SetParent(GetCell(tcellpos(4,1)));
    unit->SetPosition(7,8);
    units.push_back(unit);*/

    /*unit = new cMedu();
    unit->SetParent(GetCell(tcellpos(12,8)));
    unit->SetPosition(8,8);
    units.push_back(unit);*/

    //NextTurn();
}
void cGame::Init(){
    LoadData();
    gamestate = GS_MMENU;
    gridw = 16; gridh = 12;
    cellw = 64; cellh = 64;


    cells.clear();
    for(int j=0; j<gridh; j++)
        for (int i=0; i<gridw; i++) {
            cells.push_back(cCell(i,j));
            cells.back().SetScr(cellToScreen(i,j));
            }
}
void cGame::LoadData(){
    hge->System_SetState(HGE_INIFILE, "Res\\config.txt");
    musicvolume = hge->Ini_GetInt("options","music",    50);

    global.aqua.health  = hge->Ini_GetInt("aqua","health",    20);
    global.aqua.dmg     = hge->Ini_GetInt("aqua","dmg",       1);
    global.aqua.armor   = hge->Ini_GetInt("aqua","armor",     0);
    global.aqua.range   = hge->Ini_GetFloat("aqua","range",     100500);

    global.torp.health  = hge->Ini_GetInt("torp","health",    20);
    global.torp.dmg     = hge->Ini_GetInt("torp","dmg",       1);
    global.torp.armor   = hge->Ini_GetInt("torp","armor",     0);
    global.torp.range   = hge->Ini_GetFloat("torp","range",     100500);

    global.bati.health  = hge->Ini_GetInt("bati","health",    20);
    global.bati.dmg     = hge->Ini_GetInt("bati","dmg",       1);
    global.bati.armor   = hge->Ini_GetInt("bati","armor",     0);
    global.bati.range   = hge->Ini_GetFloat("bati","range",     100500);

    global.medu.health  = hge->Ini_GetInt("medu","health",    20);
    global.medu.dmg     = hge->Ini_GetInt("medu","dmg",       1);
    global.medu.armor   = hge->Ini_GetInt("medu","armor",     0);
    global.medu.range   = hge->Ini_GetFloat("medu","range",     100500);

    global.shark.health  = hge->Ini_GetInt("shark","health",    20);
    global.shark.dmg     = hge->Ini_GetInt("shark","dmg",       1);
    global.shark.armor   = hge->Ini_GetInt("shark","armor",     0);
    global.shark.range   = hge->Ini_GetFloat("shark","range",     100500);

    global.paul.health  = hge->Ini_GetInt("paul","health",    20);
    global.paul.dmg     = hge->Ini_GetInt("paul","dmg",       1);
    global.paul.armor   = hge->Ini_GetInt("paul","armor",     0);
    global.paul.range   = hge->Ini_GetFloat("paul","range",     100500);
}
void cGame::LoadLevel(int num){
    char _stnum[40];
    units.clear();
    for(int j=0; j<gridh; j++)
        for (int i=0; i<gridw; i++)
            cells[i+j*gridh].RemUnit();
    itoa(num, _stnum, 10);
    std::string stnum(_stnum);
    std::string path("res\\lvl");
    path = path + stnum + std::string(".txt");

    hge->System_SetState(HGE_INIFILE, path.c_str());
    //int _map = hge->Ini_GetInt("options","map", 3);

    std::string name, _unit;
    cUnit *u = NULL;
    for (UINT i = 1; i< 50; i++) //max 50 units on map
    {
        _unit = "unit";
        itoa(i, _stnum, 10);
        _unit = _unit + std::string(_stnum);
        name = hge->Ini_GetString(_unit.c_str(), "name", "empty");

        if (name.find("empty") == 0)
            break;

        u = NULL;
        if (name.find("Aqua") == 0)
        	u = new cAqua();
        if (name.find("Bati") == 0)
            u = new cBati();
        if (name.find("Torp") == 0)
            u = new cTorp();
        if (name.find("Medu") == 0)
            u = new cMedu();
        if (name.find("Shark") == 0)
            u = new cShark();
        if (name.find("Paul") == 0)
            u = new cPaul();

        if (u == NULL){
            hge->System_Log("unknown unit: %s", name.c_str());
            continue;
        } else {

        int px = hge->Ini_GetInt(_unit.c_str(), "x", 1);
        int py = hge->Ini_GetInt(_unit.c_str(), "y", 1);

        u->SetParent(GetCell(tcellpos(px,py)));
        u->SetPosition(px,py);
        units.push_back(u);
        }
    }
    turn = 0;
    NextTurn();
}
void cGame::render(){
    hge->Gfx_BeginScene();

    switch (gamestate){
    	case GS_MMENU:  render_mmenu();     break;
    	case GS_GAME:   render_game();      break;
    	case GS_OPTIONS:render_options();   break;
    	case GS_CHLVL:  render_chlvl();     break;
    	default:break;
    }
    if (gamestate != GS_GAME)
        cur->Render(mx, my);
    hge->Gfx_EndScene();
}
bool cGame::update(){
    dt = hge->Timer_GetDelta();
    hge->Input_GetMousePos(&mx, &my);

    switch (gamestate)    {
    	case GS_MMENU:  update_mmenu(dt);break;
    	case GS_GAME:   update_game(dt);break;
    	case GS_OPTIONS:update_options(dt);break;
    	case GS_CHLVL:  update_chlvl(dt);break;
    	default:break;
    }
    return doQuit;
}
void cGame::render_mmenu(){
    if (hge->Timer_GetTime()<2){
        hge->Gfx_Clear(0);
        resman->GetSprite("igdc_logo")->RenderEx(512,384, 0, 0.5);
    }
    else{
        mmdistort->Render(0,0);
        gui->Render();
    }
};
void cGame::render_game(){
    bgd->RenderStretch(0,0,1024,768);
    grid->Render(1024/2,768/2);

    iface->render();

    //do the cell glow
    overcell = screenToCell((int)mx, (int)my);
    tcellpos cellmid = cellToScreen(overcell.first, overcell.second);
    cCell* cell = GetCell(overcell);
    show_aim = false;
    if (selunit == NULL){
        if (cell->NoUnit())
            spr->Render(cellmid.first, cellmid.second);
        else
            if (cell->GetUnit()->GetSize() == 1)
                    spr->Render(cellmid.first, cellmid.second);
                else
                    spr->RenderEx(cell->GetUnit()->GetParent()->GetScrX() + 32, cell->GetUnit()->GetParent()->GetScrY() +  32, 0, 2.0);}
    else {
        if (!cell->NoUnit())
            if (!(cell->GetUnit()->IsPlayerControlled()))
                show_aim = true;

        if (!show_aim){
            if (selunit->GetSize()== 1)
                spr->Render(cellmid.first, cellmid.second);
            else {
                overcell = screenToCell((int)mx-32, (int)my-32);
                cellmid = cellToScreen(overcell.first, overcell.second);
                spr->RenderEx(cellmid.first+32, cellmid.second+32, 0, 2.0);}
            }
        else {
            if (cell->GetUnit()->GetSize()== 1)
                cellglow_rose->Render(cellmid.first, cellmid.second);
            else {
                //overcell = screenToCell((int)mx-32, (int)my-32);
                cellmid = tcellpos(cell->GetUnit()->GetParent()->GetScrX(),
                                   cell->GetUnit()->GetParent()->GetScrY());
                cellglow_rose_2x->Render(cellmid.first+32, cellmid.second+32);}
        }
    }


    for (UINT i=0; i< units.size(); i++)
        units[i]->render();

    if (proj != NULL)
        proj->render();

    /*fnt1->printf(5,  5, HGETEXT_MIDDLE, "Cell: %1.0f, %1.0f", (float)overcell.first, (float)overcell.second);
    fnt1->printf(5, 45, HGETEXT_MIDDLE, "Calc: %1.0f, %1.0f",  cellmid.first, cellmid.second);
    fnt1->printf(5, 25, HGETEXT_MIDDLE, "Pos: %1.0f, %1.0f",  mx, my);*/

/*    if (show_aim) {
        if (cell->GetUnit()->GetSize() == 1)
            aim->RenderEx(cellmid.first, cellmid.second, 0 , 0.2);
        else
            aim->RenderEx(cell->GetUnit()->GetParent()->GetScrX() + 32, cell->GetUnit()->GetParent()->GetScrY() +  32, 0, 0.2); }
*/
    if (show_aim)
        aim->Render(mx, my);
    else
        cur->Render(mx, my);
};
void cGame::render_options(){
    mmdistort->Render(0,0);

    sliderbg1->Render(262, 500);
    sliderbg2->Render(512, 500);
    sliderbg3->Render(762, 500);

    guioptions->Render();
};
void cGame::render_chlvl(){
    chlvl->Render(512,384);
    chlvlgui->Render();
    float x=0, y=0;
    switch (maxlvl)
    {
    	case 1:
            x = 137-37;
            y = 83+15;
            //chlvlgui->AddCtrl(new guiButton(1, 137,83, 37,37,resman->GetSprite("chlvlbut"), resman->GetSprite("chlvlbutover")));
            break;
        case 2:
            x = 202-37;
            y = 170+15;

        //chlvlgui->AddCtrl(new guiButton(2, 202,170,37,37,resman->GetSprite("chlvlbut"), resman->GetSprite("chlvlbutover")));
    		break;
        case 3:
            x = 283-37;
            y = 236+15;
        //chlvlgui->AddCtrl(new guiButton(3, 283,236, 37,37,resman->GetSprite("chlvlbut"), resman->GetSprite("chlvlbutover")));
    		break;
        case 4:
            x = 405-37;
            y = 258+15;
        //chlvlgui->AddCtrl(new guiButton(4, 405,258, 37,37,resman->GetSprite("chlvlbut"), resman->GetSprite("chlvlbutover")));
    		break;
        case 5:
            x = 581-37;
            y = 339+15;
        //chlvlgui->AddCtrl(new guiButton(5, 581,339, 37,37,resman->GetSprite("chlvlbut"), resman->GetSprite("chlvlbutover")));
    		break;
        case 6:
            x = 659-37;
            y = 449+15;
        //chlvlgui->AddCtrl(new guiButton(6, 659,449, 37,37,resman->GetSprite("chlvlbut"), resman->GetSprite("chlvlbutover")));
    		break;
        case 7:
            x = 724-37;
            y = 558+15;
        //chlvlgui->AddCtrl(new guiButton(7, 724,558, 37,37,resman->GetSprite("chlvlbut"), resman->GetSprite("chlvlbutover")));
    		break;
        case 8:
            x = 848-37;
            y = 618+15;
        //chlvlgui->AddCtrl(new guiButton(8, 848,618, 37,37,resman->GetSprite("chlvlbut"), resman->GetSprite("chlvlbutover")));
    		break;
    	default:
    		break;
    }

    fnt1->printf(x, y, HGETEXT_CENTER, "Level available");
};
void cGame::update_mmenu(float dt){
    static float t = 0.0f;

    if (hge->Timer_GetTime()<2)
        return;

    for(int i=1;i<16-1;i++)
        for(int j=1;j<16-1;j++)
            mmdistort->SetDisplacement(j,i,0.1*cosf(t*10+(i+j)/2)*5,0.1*sinf(t*10+(i+j)/2)*5,HGEDISP_NODE);
    t+=dt;

    int res = gui->Update(dt);
    switch (res)    {
    	case 1: gamestate = GS_CHLVL;    		break;
        case 2: gamestate = GS_OPTIONS;    		prevgamestate = GS_MMENU; break;
        case 3: doQuit = true;    		break;
    	default:    		break;
    }
};
void cGame::update_game(float dt){
    for (UINT i =0; i< units.size(); i++)
        if (units[i]->IsDead()) {
            cUnit *u = units[i];
            u->GetParent()->RemUnit();
            if (u->GetSize() == 2){
                int x = u->GetParent()->GetX();
                int y = u->GetParent()->GetY();

                GetCell(tcellpos(x+1, y))->RemUnit();
                GetCell(tcellpos(x  , y+1))->RemUnit();
                GetCell(tcellpos(x+1, y+1))->RemUnit();    }
            delete u;
            units.erase(units.begin()+i);
            if (i == step)
                NextStep();

            bool player_win = true, comp_win=true;
            for (UINT j=0; j< units.size(); j++){
                if (units[j]->IsPlayerControlled())
                    comp_win = false;
                else
                    player_win = false;
            }
            if (player_win){
                if (maxlvl < 8) maxlvl+= 1;
                gamestate = GS_CHLVL;
                }
            if (comp_win){
                gamestate = GS_CHLVL;
                }
        }
    NotLockableControlUpdate();
    if (!IsControlLocked())
        ControlUpdate();

    iface->update(dt);

    if (proj != NULL){
        proj->update(dt);
        if (proj->IsDead()) {
            delete proj;
            proj = NULL;}
    }

    for (UINT i =0; i< units.size(); i++)
        units[i]->update(dt);

};
void cGame::update_options(float dt){
    static float t = 0.0f;

    for(int i=1;i<16-1;i++)
        for(int j=1;j<16-1;j++)
            mmdistort->SetDisplacement(j,i,0.1*cosf(t*10+(i+j)/2)*5,0.1*sinf(t*10+(i+j)/2)*5,HGEDISP_NODE);
    t+=dt;

    int res = guioptions->Update(dt);
    switch (res){
    	case 7: gamestate = prevgamestate;
            hge->System_SetState(HGE_INIFILE, "Res\\config.txt");
            hge->Ini_SetInt("options", "music", music_sld->GetValue());
            hge->Ini_SetInt("options", "sound", sound_sld->GetValue());
            hge->Ini_SetInt("options", "speed", speed_sld->GetValue());
    	break;
    	case 8://sound
            hge->Channel_SetVolume(muschan, (int)music_sld->GetValue());
            muschan = hge->Music_Play(music,true, (int)music_sld->GetValue());
            hge->System_Log("change music volume: %i", (int)music_sld->GetValue());
            break;
    	case 9: //music
            musicvolume = (int)music_sld->GetValue();
            hge->Channel_SetVolume(muschan, musicvolume);
            hge->System_Log("change music volume: %i", musicvolume);
            break;
    	case 10: hge->Channel_SetVolume(muschan, (int)music_sld->GetValue());break;

    	default:
    		break;
    }
    //muschan = hge->Music_Play(music,true, (int)music_sld->GetValue());
};
void cGame::update_chlvl(float dt){
    int res = chlvlgui->Update(dt);
    if (res>0 and res<=8 and res<=maxlvl){
        LoadLevel(res);
        gamestate = GS_GAME;
        hge->Channel_Stop(muschan);
        music = resman->GetStream("game_music");
        muschan = hge->Stream_Play(music, true, musicvolume);
    }
    if (res == 10) {
        gamestate = GS_MMENU;
        hge->Channel_Stop(muschan);
        music = resman->GetStream("main_theme");
        muschan = hge->Stream_Play(music, true, musicvolume);
    }

};
void cGame::NotLockableControlUpdate(){
    /*if (hge->Input_KeyDown(HGEK_LBUTTON))    {
        selcell = NULL;
        if (selunit != NULL) {
            selunit->Unselect();
            selunit = NULL;
        }

        overcell = screenToCell((int)mx, (int)my);
        selcell = GetCell(overcell);

        if (! (selcell->NoUnit())){
            selunit = selcell->GetUnit();
            selcell->GetUnit()->Select();
            }
    }*/

    if(hge->Input_KeyDown(HGEK_ESCAPE)) {
        prevgamestate = gamestate;
        gamestate = GS_OPTIONS;
    }
}
void cGame::NextTurn(){
    ++turn;
    step = 0;
    //mixin
    hge->Random_Seed(0);
    std::vector<int> vct(units.size());
    bool nosequence;
    for (UINT i = 0; i<units.size(); i++)
        do {
            nosequence = true;
            vct[i] = hge->Random_Int(0, units.size()-1);
            for (UINT j = 0; j<i; j++)
                if (vct[i] == vct[j]) {
                    nosequence = false;
                    break;  }
        } while (nosequence != true);


    /*vct[0]= 6;
    vct[1]= 7;
    vct[2]= 0;
    vct[3]= 1;
    vct[4]= 2;
    vct[5]= 3;
    vct[6]= 4;
    vct[7]= 5;


    for(UINT i = 0; i<vct.size(); i++)
        hge->System_Log("Mixin. Num: %i. Value: %i", i, vct[i]);
    */

    std::vector<cUnit*> units1;
    for (UINT i = 0, sz = units.size(); i<sz; i++){
        cUnit *u = units[vct[i]];
        units1.push_back(u);
    }
    units.clear();
    for (UINT i = 0, sz = units1.size(); i<sz; i++){
        units.push_back(units1[i]);
        units[i]->SetPosition(i, units1.size());
    }

    //renew for action
    for (UINT i = 0; i<units.size(); i++)
        units[i]->NextTurn();

    SelectUnit(step);
    selunit = units[step];
}
void cGame::NextStep(){
    step++;
    if (step>=units.size())
        NextTurn();
    SelectUnit(step);
}
void cGame::ControlUpdate(){
    if (hge->Input_KeyDown(HGEK_LBUTTON) and selunit != NULL)
        if (selunit->actionpoint >0 and selunit->IsPlayerControlled()) {
        overcell = screenToCell((int)mx, (int)my);
        selcell = GetCell(overcell);
        if (!(selcell->NoUnit())){
            if (!selcell->GetUnit()->IsPlayerControlled()){
                float dist = UnitDistance(selunit, selcell->GetUnit());
                float range = selunit->GetRange();
                if (dist <= range)
                //if (UnitDistance(selunit, selcell->GetUnit()) < selunit->GetRange())
                    selunit->Shoot(selcell);
            }
                }
            else
                if (selcell->GetUnit() == selunit)
                    selunit->Move(selcell);
        else {
            if (selunit->GetSize() == 1)
                overcell = screenToCell((int)mx, (int)my);
            else
                overcell = screenToCell((int)mx-32, (int)my-32);
            selunit->Move(GetCell(overcell));}

        if (selunit->actionpoint == 0){
                NextStep();
        }
    }

    if (!selunit->IsPlayerControlled() and !IsControlLocked()){
        AIMove();
    }

}
void cGame::AIMove(){
    cUnit *u = AIFindPriorityTarget(selunit);
    if (u != NULL) {
        hge->System_Log("i can shoot!!!");
        selunit->Shoot(u->GetParent());
    }
    else{
        if (turn < 3 and selunit->IsMedu()){
            selunit->Stay();
            NextStep();
            return;}

        cCell *c = AIFindCellToMove(selunit);
        if (c!= NULL){
            if (turn < 3 and selunit->IsMedu())
                selunit->Stay();
            else
                selunit->Move(c);}
        else
            selunit->Stay();
    }
    NextStep();
}
cUnit*cGame::AIFindPriorityTarget(cUnit *u){
    int maxhp = INT32_MAX;
    cUnit *ret = NULL;
    for (UINT i =0, sz = units.size(); i<sz; i++)
        if (units[i]->IsPlayerControlled())
            if (u->GetRange() >= UnitDistance(u, units[i]))
                if (maxhp > units[i]->GetHealth()){
                    maxhp = units[i]->GetHealth();
                    ret = units[i];}
    return ret;
}
cCell *cGame::AIFindCellToMove(cUnit *u){
    cCell*ret = NULL;
    cUnit *nearestenemy = NULL;
    //find nearest enemy;
    float mindist = 1e20;
    for (UINT i =0; i<units.size(); i++)
        if (units[i]->IsPlayerControlled())
            if (UnitDistance(units[i], u) < mindist){
                nearestenemy = units[i];
                mindist = UnitDistance(units[i], u);
                }
    if (nearestenemy !=NULL){
        int dx, dy;
        dx = nearestenemy->GetParent()->GetX() - u->GetParent()->GetX();
        dy = nearestenemy->GetParent()->GetY() - u->GetParent()->GetY();
        if (dx != 0) dx = dx>0?1:-1;
        if (dy != 0) dy = dy>0?1:-1;

        if (!(dx == 0 and dy ==0))
            ret = GetCell(tcellpos(u->GetParent()->GetX()+dx,
                                   u->GetParent()->GetY()+dy));
    }
    return ret;
}
float cGame::CellDistance(cCell *c1, cCell *c2){
    if (c1 == NULL or c2 == NULL)
        return 0;
    return sqrt(pow(c1->GetX() - c2->GetX(), 2.0) +
                pow(c1->GetY() - c2->GetY(), 2.0));
}
float cGame::UnitDistance(cUnit *u1, cUnit *u2){
    // a little govnocode there
    if (u1 == u2) return 0;

    float mindist, dist;
    switch (u1->GetSize()+ u2->GetSize())
    {
    	case 2://1+1
            return CellDistance(u1->GetParent(), u2->GetParent());
    		break;
        case 3: //1+2 or 2+1
            {cUnit *u = u1->GetSize() ==  1 ? u1:u2; // u - cellsize 1
            cUnit *_u = u2->GetSize() == 1 ? u1:u2; //_u - cellsize 2

            mindist= sqrt(pow(u->GetParent()->GetX() - _u->GetParent()->GetX(), 2.0)
                        + pow(u->GetParent()->GetY() - _u->GetParent()->GetY(), 2.0));

            dist = sqrt(pow(u->GetParent()->GetX() - _u->GetParent()->GetX()-1, 2.0)
                      + pow(u->GetParent()->GetY() - _u->GetParent()->GetY(), 2.0));
            mindist = dist < mindist ? dist:mindist;

            dist = sqrt(pow(u->GetParent()->GetX() - _u->GetParent()->GetX(), 2.0)
                      + pow(u->GetParent()->GetY() - _u->GetParent()->GetY()-1, 2.0));
            mindist = dist < mindist ? dist:mindist;

            dist = sqrt(pow(u->GetParent()->GetX() - _u->GetParent()->GetX()-1, 2.0)
                      + pow(u->GetParent()->GetY() - _u->GetParent()->GetY()-1, 2.0));
            mindist = dist < mindist ? dist:mindist;

            return mindist;
            break;}
        case 4: //2+2
            {tcellpos _pos1(u1->GetParent()->GetX(), u1->GetParent()->GetY());
            tcellpos _pos2(u2->GetParent()->GetX(), u2->GetParent()->GetY());

            tcellpos pos1 = _pos1, pos2 = _pos2;

            mindist = sqrt(pow(pos1.first - pos2.first, 2.0)+     //1
                           pow(pos1.second - pos2.second, 2.0));

            pos1 = _pos1; pos2 = _pos2; //2
            pos1.first +=1;
            dist = sqrt(pow(pos1.first - pos2.first, 2.0)+ pow(pos1.second - pos2.second, 2.0));
            mindist = dist < mindist ? dist:mindist;

            pos1 = _pos1; pos2 = _pos2; //3
            pos1.second+=1;
            dist = sqrt(pow(pos1.first - pos2.first, 2.0)+ pow(pos1.second - pos2.second, 2.0));
            mindist = dist < mindist ? dist:mindist;

            pos1 = _pos1; pos2 = _pos2; //4
            pos1.first+=1;
            pos1.second+=1;
            dist = sqrt(pow(pos1.first - pos2.first, 2.0)+ pow(pos1.second - pos2.second, 2.0));
            mindist = dist < mindist ? dist:mindist;

            pos1 = _pos1; pos2 = _pos2; //5
            pos2.first +=1;
            dist = sqrt(pow(pos1.first - pos2.first, 2.0)+ pow(pos1.second - pos2.second, 2.0));
            mindist = dist < mindist ? dist:mindist;

            pos1 = _pos1; pos2 = _pos2; //6
            pos1.first+=1;
            pos2.first +=1;
            dist = sqrt(pow(pos1.first - pos2.first, 2.0)+ pow(pos1.second - pos2.second, 2.0));
            mindist = dist < mindist ? dist:mindist;

            pos1 = _pos1; pos2 = _pos2; //7
            pos1.second+=1;
            pos2.first +=1;
            dist = sqrt(pow(pos1.first - pos2.first, 2.0)+ pow(pos1.second - pos2.second, 2.0));
            mindist = dist < mindist ? dist:mindist;

            pos1 = _pos1; pos2 = _pos2; //8
            pos1.first+=1;
            pos1.second+=1;
            pos2.first +=1;
            dist = sqrt(pow(pos1.first - pos2.first, 2.0)+ pow(pos1.second - pos2.second, 2.0));
            mindist = dist < mindist ? dist:mindist;

            pos1 = _pos1; pos2 = _pos2; //9
            pos2.second +=1;
            dist = sqrt(pow(pos1.first - pos2.first, 2.0)+ pow(pos1.second - pos2.second, 2.0));
            mindist = dist < mindist ? dist:mindist;

            pos1 = _pos1; pos2 = _pos2; //10
            pos1.first+=1;
            pos2.second +=1;
            dist = sqrt(pow(pos1.first - pos2.first, 2.0)+ pow(pos1.second - pos2.second, 2.0));
            mindist = dist < mindist ? dist:mindist;

            pos1 = _pos1; pos2 = _pos2; //11
            pos1.second+=1;
            pos2.second +=1;
            dist = sqrt(pow(pos1.first - pos2.first, 2.0)+ pow(pos1.second - pos2.second, 2.0));
            mindist = dist < mindist ? dist:mindist;

            pos1 = _pos1; pos2 = _pos2; //12
            pos1.first+=1;
            pos1.second+=1;
            pos2.second +=1;
            dist = sqrt(pow(pos1.first - pos2.first, 2.0)+ pow(pos1.second - pos2.second, 2.0));
            mindist = dist < mindist ? dist:mindist;

            pos1 = _pos1; pos2 = _pos2; //13
            pos2.first +=1;
            pos2.second +=1;
            dist = sqrt(pow(pos1.first - pos2.first, 2.0)+ pow(pos1.second - pos2.second, 2.0));
            mindist = dist < mindist ? dist:mindist;

            pos1 = _pos1; pos2 = _pos2; //14
            pos1.first+=1;
            pos2.first +=1;
            pos2.second +=1;
            dist = sqrt(pow(pos1.first - pos2.first, 2.0)+ pow(pos1.second - pos2.second, 2.0));
            mindist = dist < mindist ? dist:mindist;

            pos1 = _pos1; pos2 = _pos2; //15
            pos1.second+=1;
            pos2.first +=1;
            pos2.second +=1;
            dist = sqrt(pow(pos1.first - pos2.first, 2.0)+ pow(pos1.second - pos2.second, 2.0));
            mindist = dist < mindist ? dist:mindist;

            pos1 = _pos1; pos2 = _pos2; //16
            pos1.first+=1;
            pos1.second+=1;
            pos2.first +=1;
            pos2.second +=1;
            dist = sqrt(pow(pos1.first - pos2.first, 2.0)+ pow(pos1.second - pos2.second, 2.0));
            mindist = dist < mindist ? dist:mindist;

            return mindist;
            break;}
    	default:
            return 0;
    		break;
    }
    return 0;
}
void cGame::SelectUnit(int num){
    if (selunit != NULL) {
            selunit->Unselect();
            selunit = NULL;
        }

    selunit = units[num];
    selunit->Select();
    selcell = selunit->GetParent();
}
cGame::~cGame(){
    delete iface;
    delete resman;
}

/// Interface
cInterface::cInterface(){
}
void cInterface::render(){
}
void cInterface::update(float dt){
}
cInterface::~cInterface(){
}

/// Grid
cCell *cGame::GetCell(tcellpos pos){
    return &(cells[pos.first+pos.second*gridw]);
}
tcellpos cGame::screenToCell(int x, int y){
    return tcellpos((x - c1)/c2,
                    (y - c3)/c4);

    /*гексы
    int retx = trunc(x / cellw);
    return tcellpos(retx, retx % 2 == 0 ? trunc(y / cellh) : trunc((y+cellh/2)/cellh));*/
}
tcellpos cGame::cellToScreen(int px, int py){
    return tcellpos(px*c2 + c1 + c2/2,
                    py*c4 + c3 + c4/2);

    //гексы
    //return tcellpos(px * cellw + cellw / 2, px % 2 == 0 ? (py * cellh + cellh / 2) : py*cellh);
}
bool cGame::UnitToCell(cUnit *u, int x, int y){
    if (cells[x+y*gridw].NoUnit())
        return cells[x+y*gridw].AddUnit(u);
    else return false;
}
bool cGame::MoveToCell(int x1, int y1, int x2, int y2){
    if (cells[x1+y1*gridw].NoUnit() == true)
        return false;
    else
        if (cells[x2+y2*gridw].NoUnit()){
            cUnit *u = cells[x1+y1*gridw].GetUnit();
            cells[x1+y1*gridw].RemUnit();
            cells[x2+y2*gridw].AddUnit(u);
            u->Move(&(cells[x2+y2*gridw]));
            return true;
        }else return false;
}

/// Cell
cCell::cCell(int px, int py){
    x = px; y = py;
    unit = NULL;
}
cUnit *cCell::GetUnit(){
    return unit;
}
bool cCell::NoUnit(){
    return unit == NULL ? true : false;
}
bool cCell::AddUnit(cUnit *u){
    if (NoUnit() and u!=NULL){
        unit = u;
        //unit->SetParent(this);
        return true;
    } else return false;
}
bool cCell::RemUnit(){
    if (unit != NULL) {
        unit = NULL;
        return true;
    } else return false;
}
cCell::~cCell(){
}

/// Unit
cUnit::cUnit(){
}
cUnit::~cUnit(){
    delete idle;
    delete move;
}
void cUnit::update(float dt){
    switch (state)  {
        case state_idle:
            idle->Update(dt);
            break;
        case state_move:
            move->Update(dt);
            time -= dt;
            if (time <= 0){
                state = state_idle;
                cGame::UnlockControl();}
            break;
        case state_attack:
            attanim->Update(dt);
            if (not attanim->IsPlaying()){
                state = state_idle;
                cGame::UnlockControl();
            }
    	default:
    		break;
    }
}
void cUnit::NextTurn(){
    actionpoint = 1;
}
bool cUnit::Move(cCell *dest){
    if (!(dest->NoUnit()))
        if (dest->GetUnit() != this)
            return false;
    if (cellsize == 2) {
        int x = dest->GetX();
        int y = dest->GetY();

        if (! (cGame::Instance()->GetCell(tcellpos(x+1, y))->NoUnit()))
            if (cGame::Instance()->GetCell(tcellpos(x+1, y))->GetUnit() != this)
                return false;
        if (! (cGame::Instance()->GetCell(tcellpos(x, y+1))->NoUnit()))
            if (cGame::Instance()->GetCell(tcellpos(x, y+1))->GetUnit() != this)
                return false;
        if (! (cGame::Instance()->GetCell(tcellpos(x+1, y+1))->NoUnit()))
            if (cGame::Instance()->GetCell(tcellpos(x+1, y+1))->GetUnit() != this)
                return false;
        }

    if (dest == parent)
        return false;
    if (abs(dest->GetX() - parent->GetX()) > 1)
        return false;
    if (abs(dest->GetY() - parent->GetY()) > 1)
        return false;

    if (dest->GetX()<1)
        return false;
    if ((dest->GetX()-cellsize)>15)
        return false;
    if (dest->GetY()<1)
        return false;
    if ((dest->GetY()-cellsize)>10)
        return false;
    state = state_move;
    time = move_time;
    start_point = parent;
    start_point->RemUnit();
    if (cellsize == 2){
        int x = start_point->GetX();
        int y = start_point->GetY();

        cGame::Instance()->GetCell(tcellpos(x+1, y))->RemUnit();
        cGame::Instance()->GetCell(tcellpos(x  , y+1))->RemUnit();
        cGame::Instance()->GetCell(tcellpos(x+1, y+1))->RemUnit();
    }
    SetParent(dest);
    cGame::LockControl();
    actionpoint = 0;
    hgeCreate(HGE_VERSION)->Effect_Play(snd_move);
    return true;
}
void cUnit::Stay(){
    actionpoint = 0;
}
void cUnit::Shoot(cCell *dest){
    cUnit* u = dest->GetUnit();
    dest = u->GetParent();

    tcellpos start = tcellpos(cellsize == 1? parent->GetScrX():parent->GetScrX()+32, cellsize == 1?parent->GetScrY():parent->GetScrY()+32);
    tcellpos _dest= tcellpos(u->GetSize()==1? dest->GetScrX():dest->GetScrX()+32, u->GetSize()==1?dest->GetScrY():dest->GetScrY()+32);

    if (not melee)
        cGame::SetProjectile(
            new cProjectile(ammotype, start, _dest, dest->GetUnit()));
    else{
        dest->GetUnit()->GetDamage(damage);
        attanim->SetFrame(0);
        attanim->Play();
        state = state_attack;
        }
    hgeCreate(HGE_VERSION)->Effect_Play(snd_attack);
    actionpoint = 0;
}
void cUnit::SetParam(st_unitparam par){
    health = par.health;
    armor = par.armor;
    damage = par.dmg;
    range = par.range;
}
void cUnit::GetDamage(int dmg){
    if (dmg>armor)
        health -=  dmg-armor;
}
void cUnit::SetParent(cCell *p){
    parent = p;
    parent->AddUnit(this);
    if (cellsize == 2){
        int x = parent->GetX();
        int y = parent->GetY();

        cGame::Instance()->GetCell(tcellpos(x+1, y))->AddUnit(this);
        cGame::Instance()->GetCell(tcellpos(x  , y+1))->AddUnit(this);
        cGame::Instance()->GetCell(tcellpos(x+1, y+1))->AddUnit(this);
    }
}
void cUnit::Select(){
    selected = true;
}
void cUnit::render(){
    int midx = cellsize ==1 ? parent->GetScrX():parent->GetScrX()+32,
                midy = cellsize ==1 ? parent->GetScrY():parent->GetScrY()+32,
                zzz = cellsize ==1 ? 1:2;

    switch (state)    {
    	case state_idle:
            if (selected)
                if (IsPlayerControlled())
                    grid_sel1->RenderEx(midx, midy,0,zzz);

            idle->Render(midx, midy);
            cGame::GetFont1()->printf(midx, midy, HGETEXT_CENTER, "%i", health );

    		break;
        case state_move:
            float zx, zy;
            zx =start_point->GetScrX() + (parent->GetScrX()-start_point->GetScrX()) * (move_time-time) / move_time;
            zy =start_point->GetScrY() + (parent->GetScrY()-start_point->GetScrY()) * (move_time-time) / move_time;
            move->Render(cellsize == 1 ? zx: zx+32, cellsize == 1 ? zy : zy+32);
            break;
    	case state_attack:
            attanim->Render(midx, midy);
            break;
    	default:
    		break;
    }

    (actionpoint == 0 ? turn_off : turn_on)->Render(512-25+position*50 - maxposition*50/2,700);
}

///Aqua
cAqua::cAqua(){
    Unselect();
    idle = new hgeAnimation(*cGame::resman->GetAnimation("aqua_idle"));
    move = new hgeAnimation(*cGame::resman->GetAnimation("aqua_move"));
    turn_on = cGame::resman->GetSprite("aqua_on");
    turn_off = cGame::resman->GetSprite("aqua_off");
    snd_move = cGame::resman->GetEffect("aquasnd_move");
    snd_attack = cGame::resman->GetEffect("aqua_attack");

    idle->Play();
    move->Play();
    state = state_idle;
    cellsize = 1;
    plcontrolled = true;
    ammotype = 0;
    melee = false;

    SetParam(cGame::global.aqua);
    actionpoint = 1;
}
cAqua::~cAqua(){
    delete idle;
    delete move;
}

///Torp
cTorp::cTorp(){
    Unselect();
    idle = new hgeAnimation(*cGame::resman->GetAnimation("torp_idle"));
    move = new hgeAnimation(*cGame::resman->GetAnimation("torp_move"));
    attanim = new hgeAnimation(*(cGame::resman->GetAnimation("torp_attack")));
    //attanim = cGame::resman->GetAnimation("torp_attack");
    turn_on = cGame::resman->GetSprite("torp_on");
    turn_off = cGame::resman->GetSprite("torp_off");
    snd_move = cGame::resman->GetEffect("aquasnd_move");
    snd_attack = cGame::resman->GetEffect("torpman_attack");
    idle->Play();
    move->Play();
    state = state_idle;
    cellsize = 1;
    plcontrolled = true;
    ammotype = 0;
    melee = true;

    SetParam(cGame::global.torp);
    actionpoint = 1;
}
cTorp::~cTorp(){
}

///Bati
cBati::cBati(){
    Unselect();
    idle = new hgeAnimation(*cGame::resman->GetAnimation("bati_idle"));
    move = new hgeAnimation(*cGame::resman->GetAnimation("bati_idle"));
    turn_on = cGame::resman->GetSprite("bati_on");
    turn_off = cGame::resman->GetSprite("bati_off");
    snd_move = cGame::resman->GetEffect("batisnd_move");
    snd_attack = cGame::resman->GetEffect("bati_attack");

    idle->Play();
    move->Play();
    state = state_idle;
    cellsize = 2;
    plcontrolled = true;
    ammotype = 1;
    melee = false;

    SetParam(cGame::global.bati);
    actionpoint = 1;
}
cBati::~cBati(){
    delete idle;
    delete move;
}

///Paul
cPaul::cPaul(){
    Unselect();
    idle = new hgeAnimation(*cGame::resman->GetAnimation("paul_idle"));
    move = new hgeAnimation(*cGame::resman->GetAnimation("paul_move"));
    attanim = new hgeAnimation(*cGame::resman->GetAnimation("paul_attack"));
    turn_on = cGame::resman->GetSprite("paul_on");
    turn_off = cGame::resman->GetSprite("paul_off");
    snd_move = cGame::resman->GetEffect("mobssnd_move");
    snd_attack = cGame::resman->GetEffect("paul_attack");

    idle->Play();
    move->Play();
    state = state_idle;
    cellsize = 2;
    plcontrolled = false;
    ammotype = 0;
    melee = true;

    SetParam(cGame::global.paul);
    actionpoint = 1;
}
cPaul::~cPaul(){
    delete idle;
    delete move;
}

///Shark
cShark::cShark(){
    Unselect();
    idle = new hgeAnimation(*cGame::resman->GetAnimation("akul_idle"));
    move = new hgeAnimation(*cGame::resman->GetAnimation("akul_move"));
    attanim = new hgeAnimation(*cGame::resman->GetAnimation("akul_attack"));
    turn_on = cGame::resman->GetSprite("shark_on");
    turn_off = cGame::resman->GetSprite("shark_off");
    snd_move = cGame::resman->GetEffect("mobssnd_move");
    snd_attack = cGame::resman->GetEffect("shark_attack");

    idle->Play();
    move->Play();
    state = state_idle;
    cellsize = 2;
    plcontrolled = false;
    ammotype = 0;
    melee = true;

    SetParam(cGame::global.shark);
    actionpoint = 1;
}
cShark::~cShark(){
    delete idle;
    delete move;
}

///Medusa
cMedu::cMedu(){
    Unselect();
    idle = new hgeAnimation(*cGame::resman->GetAnimation("medu_idle"));
    move = new hgeAnimation(*cGame::resman->GetAnimation("medu_move"));
    turn_on = cGame::resman->GetSprite("medu_on");
    turn_off = cGame::resman->GetSprite("medu_off");
    snd_move = cGame::resman->GetEffect("mobssnd_move");
    snd_attack = cGame::resman->GetEffect("medu_attack");

    idle->Play();
    move->Play();
    state = state_idle;
    cellsize = 1;
    plcontrolled = false;
    ammotype = 2;
    melee = false;

    SetParam(cGame::global.medu);
    actionpoint = 1;
}
cMedu::~cMedu(){
    delete idle;
    delete move;
}

///Projectile
cProjectile::cProjectile(int type, tcellpos _st, tcellpos _dest, cUnit *_target){
    dead = false;
    target = _target;
    start = _st;
    dest = _dest;
    time = 0;

    float speed = 500.0;
    hgeVector dist= hgeVector(dest.first, dest.second) - hgeVector(start.first, start.second);
    ftime = dist.Length()/speed;
    angle = dist.Angle();

    hgeCreate(HGE_VERSION)->System_Log("Shoot dest: %i %i", dest.first, dest.second);
    hgeCreate(HGE_VERSION)->System_Log("Shoot source: %i %i", start.first, start.second);

    cGame::LockControl();
    switch (type)    {
    	case 0:
            spr = cGame::resman->GetSprite("arrow");
            dmg = cGame::global.aqua.dmg;
            break;
        case 1:
            spr = cGame::resman->GetSprite("torpedo");
            dmg = cGame::global.bati.dmg;
    		break;
        case 2:
            spr = cGame::resman->GetSprite("spike");
            dmg = cGame::global.medu.dmg;
    		break;
    	default:
            spr = cGame::resman->GetSprite("arrow");
            dmg = 1;
    		break;}

};
cProjectile::~cProjectile(){
    cGame::UnlockControl();
};
void cProjectile::render() {
    float rx = start.first+(dest.first - start.first) * time / ftime;
    float ry = start.second + (dest.second - start.second) * time/ftime;

    /*spr->RenderEx(start.first+(dest.first - start.first) * time / ftime,
                start.second + (dest.second - start.second) * time/ftime,
                0, angle);*/
    spr->RenderEx(rx,
                ry,
                angle);
};
void cProjectile::update(float dt){
    time += dt;
    if (time > ftime and !dead){
        target->GetDamage(dmg);
        dead = true;
    }
};
bool cProjectile::IsDead(){
    return dead;
};

///GUIButton
guiButton::guiButton(int _id, float _x, float _y, float _w, float _h, hgeSprite *_sprNorm, hgeSprite *_sprOver){
    id = _id;
    bStatic = false;
    bVisible = true;
    bEnabled = true;
    rect = hgeRect(_x,_y, _x+_w, _y+_h);
    color = ARGB(255,255,255,255);
    sprNorm = _sprNorm;
    sprOver = _sprOver;
    overed = false;
};
guiButton::~guiButton(){
};
void guiButton::Render(){
    if (!overed)
        sprNorm->Render(rect.x1, rect.y1);
    else
        sprOver->Render(rect.x1, rect.y1);
}
void guiButton::MouseOver(bool bOver){
    if (overed ==false and bOver == true)
        hge->Effect_Play(cGame::resman->GetEffect("button_over"));


    overed = bOver;

    };
bool guiButton::MouseLButton(  bool bDown){
    return bDown? true:false;
}
