From 333e1d9878ed731775babdacec77237141d96d1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20W=2E=20Urba=C5=84czyk?= Date: Sun, 16 Nov 2008 01:06:15 +0000 Subject: [PATCH] * started making support for save/load options --- CGameState.h | 13 +- CLua.h | 4 + CMT.cpp | 9 ++ CPreGame.cpp | 323 ++++++++++++++++++++++++++------------ CPreGame.h | 20 ++- client/Client.cpp | 5 + client/Client.h | 1 + lib/Connection.cpp | 27 ++-- lib/Connection.h | 334 +++++++++++++++++++++------------------- map.h | 4 + server/CGameHandler.cpp | 9 ++ server/CGameHandler.h | 14 ++ 12 files changed, 485 insertions(+), 278 deletions(-) diff --git a/CGameState.h b/CGameState.h index e46b84e92..0101f8d29 100644 --- a/CGameState.h +++ b/CGameState.h @@ -50,6 +50,11 @@ public: std::vector towns; std::vector availableHeroes; //heroes available in taverns PlayerState():color(-1),currentSelection(0xffffffff){}; + template void serialize(Handler &h, const int version) + { + h & color & serial & currentSelection & fogOfWarMap & resources; + //TODO: vectors of heroes/towns + } }; struct DLL_EXPORT BattleInfo @@ -156,7 +161,7 @@ private: BattleInfo *curB; //current battle ui32 day; //total number of days in game Mapa * map; - std::map players; //ID <-> playerstate + std::map players; //ID <-> player state std::map villages, forts, capitols; //def-info for town graphics std::vector resVals; @@ -191,6 +196,12 @@ private: std::set tilesToReveal(int3 pos, int radious, int player); //if player==-1 => adds all tiles in radious public: int getDate(int mode=0) const; //mode=0 - total days in game, mode=1 - day of week, mode=2 - current week, mode=3 - current month + template void serialize(Handler &h, const int version) + { + h & scenarioOps & seed & currentPlayer & day & map & players & resVals; + //TODO: villages, forts, capitols - will need reloading + //TODO: hero pool + } friend class CCallback; friend class CPathfinder;; diff --git a/CLua.h b/CLua.h index 4e5da0fc2..35dd9e709 100644 --- a/CLua.h +++ b/CLua.h @@ -99,6 +99,10 @@ public: CScriptCallback * cb; CCPPObjectScript(CScriptCallback * CB){cb=CB;}; virtual std::vector yourObjects()=0; //returns IDs of objects which are handled by script + template void serialize(Handler &h, const int version) + { + //TODO: write + } }; class CVisitableOPH : public CCPPObjectScript //once per hero { diff --git a/CMT.cpp b/CMT.cpp index 66c0c85af..5f91ccdf4 100644 --- a/CMT.cpp +++ b/CMT.cpp @@ -131,6 +131,9 @@ int main(int argc, char** argv) abilh->loadAbilities(); cgi->abilh = abilh; tlog0<<"\tAbility handler: "<preth = new CPreGameTextHandler; + CGI->preth->loadTexts(); + tlog0<<"\tCPreGameTextHandler: "<> fname; + client->save(fname); + } else if(message=="get txt") { boost::filesystem::create_directory("Extracted_txts"); diff --git a/CPreGame.cpp b/CPreGame.cpp index e41fa0d48..290857473 100644 --- a/CPreGame.cpp +++ b/CPreGame.cpp @@ -109,6 +109,11 @@ void HighButton::hover(bool on) SDL_BlitSurface(imgs->ourImages[i].bitmap,NULL,screen,&pos); updateRect(&pos); } + +HighButton::~HighButton() +{ + delete imgs; +} void Button::hover(bool on) { HighButton::hover(on); @@ -136,15 +141,15 @@ void Slider::updateSlid() { myh=perc*((float)pos.h-48)+pos.y+16; SDL_FillRect(screen,&genRect(pos.h-32,pos.w,pos.x,pos.y+16),0); - blitAt(slider.imgs->ourImages[0].bitmap,pos.x,(int)myh); - slider.pos.y=(int)myh; + blitAt(slider->imgs->ourImages[0].bitmap,pos.x,(int)myh); + slider->pos.y=(int)myh; } else { myh=perc*((float)pos.w-48)+pos.x+16; SDL_FillRect(screen,&genRect(pos.h,pos.w-32,pos.x+16,pos.y),0); - blitAt(slider.imgs->ourImages[0].bitmap,(int)myh,pos.y); - slider.pos.x=(int)myh; + blitAt(slider->imgs->ourImages[0].bitmap,(int)myh,pos.y); + slider->pos.x=(int)myh; } updateRect(&pos); } @@ -169,16 +174,16 @@ Slider::Slider(int x, int y, int h, int amnt, int cap, bool ver) if (ver) { pos = genRect(h,16,x,y); - down = Button(genRect(16,16,x,y+h-16),boost::bind(&Slider::moveDown,this),CDefHandler::giveDef("SCNRBDN.DEF"),false); - up = Button(genRect(16,16,x,y),boost::bind(&Slider::moveUp,this),CDefHandler::giveDef("SCNRBUP.DEF"),false); - slider = Button(genRect(16,16,x,y+16),boost::function(),CDefHandler::giveDef("SCNRBSL.DEF"),false); + down = new Button(genRect(16,16,x,y+h-16),boost::bind(&Slider::moveDown,this),CDefHandler::giveDef("SCNRBDN.DEF"),false); + up = new Button(genRect(16,16,x,y),boost::bind(&Slider::moveUp,this),CDefHandler::giveDef("SCNRBUP.DEF"),false); + slider = new Button(genRect(16,16,x,y+16),boost::function(),CDefHandler::giveDef("SCNRBSL.DEF"),false); } else { pos = genRect(16,h,x,y); - down = Button(genRect(16,16,x+h-16,y),boost::bind(&Slider::moveDown,this),CDefHandler::giveDef("SCNRBRT.DEF"),false); - up = Button(genRect(16,16,x,y),boost::bind(&Slider::moveUp,this),CDefHandler::giveDef("SCNRBLF.DEF"),false); - slider = Button(genRect(16,16,x+16,y),boost::function(),CDefHandler::giveDef("SCNRBSL.DEF"),false); + down = new Button(genRect(16,16,x+h-16,y),boost::bind(&Slider::moveDown,this),CDefHandler::giveDef("SCNRBRT.DEF"),false); + up = new Button(genRect(16,16,x,y),boost::bind(&Slider::moveUp,this),CDefHandler::giveDef("SCNRBLF.DEF"),false); + slider = new Button(genRect(16,16,x+16,y),boost::function(),CDefHandler::giveDef("SCNRBSL.DEF"),false); } moving = false; whereAreWe=0; @@ -191,9 +196,9 @@ void Slider::deactivate() void Slider::activate() { SDL_FillRect(screen,&pos,0); - up.show(); - down.show(); - slider.show(); + up->show(); + down->show(); + slider->show(); //SDL_Flip(screen); CSDL_Ext::update(screen); CPG->interested.push_back(this); @@ -203,17 +208,17 @@ void Slider::handleIt(SDL_Event sEvent) { if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_LEFT)) { - if (isItIn(&down.pos,sEvent.motion.x,sEvent.motion.y)) + if (isItIn(&down->pos,sEvent.motion.x,sEvent.motion.y)) { - down.press(); + down->press(); } - else if (isItIn(&up.pos,sEvent.motion.x,sEvent.motion.y)) + else if (isItIn(&up->pos,sEvent.motion.x,sEvent.motion.y)) { - up.press(); + up->press(); } - else if (isItIn(&slider.pos,sEvent.motion.x,sEvent.motion.y)) + else if (isItIn(&slider->pos,sEvent.motion.x,sEvent.motion.y)) { - //slider.press(); + //slider->press(); moving=true; } else if (isItIn(&pos,sEvent.motion.x,sEvent.motion.y)) @@ -243,19 +248,19 @@ void Slider::handleIt(SDL_Event sEvent) else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_LEFT)) { - if ((down.state==1) && isItIn(&down.pos,sEvent.motion.x,sEvent.motion.y)) + if ((down->state==1) && isItIn(&down->pos,sEvent.motion.x,sEvent.motion.y)) { - this->down.fun(); + this->down->fun(); } - if ((up.state==1) && isItIn(&up.pos,sEvent.motion.x,sEvent.motion.y)) + if ((up->state==1) && isItIn(&up->pos,sEvent.motion.x,sEvent.motion.y)) { - this->up.fun(); + this->up->fun(); } - if (down.state==1) down.press(false); - if (up.state==1) up.press(false); + if (down->state==1) down->press(false); + if (up->state==1) up->press(false); if (moving) { - //slider.press(); + //slider->press(); moving=false; } } @@ -324,21 +329,28 @@ void Slider::handleIt(SDL_Event sEvent) } - if (isItIn(&down.pos,sEvent.motion.x,sEvent.motion.y)) + if (isItIn(&down->pos,sEvent.motion.x,sEvent.motion.y)) { - (this->*down.fun)(); + (this->*down->fun)(); } - if (isItIn(&up.pos,sEvent.motion.x,sEvent.motion.y)) + if (isItIn(&up->pos,sEvent.motion.x,sEvent.motion.y)) { - (this->*up.fun)(); + (this->*up->fun)(); } - if (isItIn(&slider.pos,sEvent.motion.x,sEvent.motion.y)) + if (isItIn(&slider->pos,sEvent.motion.x,sEvent.motion.y)) { - (this->*slider.fun)(); + (this->*slider->fun)(); } }*/ } + +Slider::~Slider() +{ + delete up; + delete down; + delete slider; +} IntBut::IntBut() { type=2; @@ -982,6 +994,7 @@ void MapSel::processMaps(std::vector &pliczkiTemp, int &index) static boost::mutex mx; bool areMaps=true; int pom=-1; + unsigned char sss[1000]; while(areMaps) { mx.lock(); @@ -996,7 +1009,6 @@ void MapSel::processMaps(std::vector &pliczkiTemp, int &index) mx.unlock(); } gzFile tempf = gzopen(pliczkiTemp[pom].c_str(),"rb"); - unsigned char * sss = new unsigned char[1000]; int iii=0; while(true) { @@ -1009,13 +1021,22 @@ void MapSel::processMaps(std::vector &pliczkiTemp, int &index) else break; } gzclose(tempf); - if(iii<50) {tlog3<<"\t\tWarning: corrupted map file: "<20) temp=0; - std::string sss = CPG->preth->victoryConditions[temp]; - if (temp && ourMaps[selected].vicConDetails->allowNormalVictory) sss+= "/" + CPG->preth->victoryConditions[0]; + std::string sss = CGI->preth->victoryConditions[temp]; + if (temp && ourMaps[selected].vicConDetails->allowNormalVictory) sss+= "/" + CGI->preth->victoryConditions[0]; CSDL_Ext::printAt(sss,452,310,GEOR13,zwykly); temp = ourMaps[selected].lossCondition.typeOfLossCon+1; if (temp>20) temp=0; - sss = CPG->preth->lossCondtions[temp]; + sss = CGI->preth->lossCondtions[temp]; CSDL_Ext::printAt(sss,452,370,GEOR13,zwykly); //blit descrption @@ -1245,19 +1266,19 @@ void MapSel::printSelectedInfo() switch (ourMaps[selected].difficulty) { case 0: - diff=gdiff(CPG->preth->zelp[24].second); + diff=gdiff(CGI->preth->zelp[24].second); break; case 1: - diff=gdiff(CPG->preth->zelp[25].second); + diff=gdiff(CGI->preth->zelp[25].second); break; case 2: - diff=gdiff(CPG->preth->zelp[26].second); + diff=gdiff(CGI->preth->zelp[26].second); break; case 3: - diff=gdiff(CPG->preth->zelp[27].second); + diff=gdiff(CGI->preth->zelp[27].second); break; case 4: - diff=gdiff(CPG->preth->zelp[28].second); + diff=gdiff(CGI->preth->zelp[28].second); break; } temp=-1; @@ -1383,6 +1404,7 @@ void CPreGame::showScenList() } CPreGame::CPreGame() { + CPG=this; highlighted=NULL; currentTab=NULL; run=true; @@ -1390,23 +1412,20 @@ CPreGame::CPreGame() tytulowy.r=229;tytulowy.g=215;tytulowy.b=123;tytulowy.unused=0; zwykly.r=255;zwykly.g=255;zwykly.b=255;zwykly.unused=0; //gbr tlo.r=66;tlo.g=44;tlo.b=24;tlo.unused=0; - preth = new CPreGameTextHandler; - preth->loadTexts(); - CGI->preth=preth; - tlog0<<"\tCPreGame: loading txts: "<listShowed=false; - if (rand()%2) ourScenSel->background=BitmapHandler::loadBitmap("ZPIC1000.bmp"); - else ourScenSel->background=BitmapHandler::loadBitmap("ZPIC1001.bmp"); - - ourScenSel->pressed=NULL; - - ourScenSel->scenInf=BitmapHandler::loadBitmap("GSELPOP1.bmp");//SDL_LoadBMP("h3bitmap.lod\\GSELPOP1.bmp"); - ourScenSel->randMap=BitmapHandler::loadBitmap("RANMAPBK.bmp"); - ourScenSel->options=BitmapHandler::loadBitmap("ADVOPTBK.bmp"); - SDL_SetColorKey(ourScenSel->scenInf,SDL_SRCCOLORKEY,SDL_MapRGB(ourScenSel->scenInf->format,0,255,255)); - //SDL_SetColorKey(ourScenSel->scenList,SDL_SRCCOLORKEY,SDL_MapRGB(ourScenSel->scenList->format,0,255,255)); - SDL_SetColorKey(ourScenSel->randMap,SDL_SRCCOLORKEY,SDL_MapRGB(ourScenSel->randMap->format,0,255,255)); - SDL_SetColorKey(ourScenSel->options,SDL_SRCCOLORKEY,SDL_MapRGB(ourScenSel->options->format,0,255,255)); - - ourScenSel->difficulty = new CPoinGroup(); - ourScenSel->difficulty->type=1; - ourScenSel->selectedDiff=-77; - ourScenSel->difficulty->gdzie = &ourScenSel->selectedDiff; - ourScenSel->bEasy = IntSelBut(genRect(0,0,506,456),NULL,CDefHandler::giveDef("GSPBUT3.DEF"),true,ourScenSel->difficulty,0); - ourScenSel->bNormal = IntSelBut(genRect(0,0,538,456),NULL,CDefHandler::giveDef("GSPBUT4.DEF"),true,ourScenSel->difficulty,1); - ourScenSel->bHard = IntSelBut(genRect(0,0,570,456),NULL,CDefHandler::giveDef("GSPBUT5.DEF"),true,ourScenSel->difficulty,2); - ourScenSel->bExpert = IntSelBut(genRect(0,0,602,456),NULL,CDefHandler::giveDef("GSPBUT6.DEF"),true,ourScenSel->difficulty,3); - ourScenSel->bImpossible = IntSelBut(genRect(0,0,634,456),NULL,CDefHandler::giveDef("GSPBUT7.DEF"),true,ourScenSel->difficulty,4); - - ourScenSel->bBack = Button(genRect(0,0,584,535),boost::bind(&CPreGame::showNewMenu,this),CDefHandler::giveDef("SCNRBACK.DEF")); - ourScenSel->bBegin = Button(genRect(0,0,414,535),boost::bind(&CPreGame::begin,this),CDefHandler::giveDef("SCNRBEG.DEF")); - ourScenSel->bScens = Button(genRect(0,0,414,81),boost::bind(&CPreGame::showScenList,this),CDefHandler::giveDef("GSPBUTT.DEF")); - - for (int i=0; ibScens.imgs->ourImages.size(); i++) - CSDL_Ext::printAt(CGI->generaltexth->allTexts[500],25+i,2+i,GEOR13,zwykly,ourScenSel->bScens.imgs->ourImages[i].bitmap); //"Show Available Scenarios" - ourScenSel->bRandom = Button(genRect(0,0,414,105),boost::bind(&CPreGame::showScenList,this),CDefHandler::giveDef("GSPBUTT.DEF")); - for (int i=0; ibRandom.imgs->ourImages.size(); i++) - CSDL_Ext::printAt(CGI->generaltexth->allTexts[740],25+i,2+i,GEOR13,zwykly,ourScenSel->bRandom.imgs->ourImages[i].bitmap); - ourScenSel->bOptions = Button(genRect(0,0,414,509),boost::bind(&CPreGame::showOptions,this),CDefHandler::giveDef("GSPBUTT.DEF")); - for (int i=0; ibOptions.imgs->ourImages.size(); i++) - CSDL_Ext::printAt(CGI->generaltexth->allTexts[501],25+i,2+i,GEOR13,zwykly,ourScenSel->bOptions.imgs->ourImages[i].bitmap); //"Show Advanced Options" - - CPG=this; tlog5 << "\t\tLoaded graphics\n"; ourScenSel->mapsel.init(); tlog5 << "\t\tLoaded maps\n"; @@ -1554,8 +1535,6 @@ void CPreGame::initNewMenu() ourNewMenu->highScores = CDefHandler::giveDef("ZTCAMPN.DEF"); ourNewMenu->credits = CDefHandler::giveDef("ZTTUTOR.DEF"); ourNewMenu->quit = CDefHandler::giveDef("ZTBACK.DEF"); - ok = CDefHandler::giveDef("IOKAY.DEF"); - cancel = CDefHandler::giveDef("ICANCEL.DEF"); // single scenario ourNewMenu->lNewGame.h=ourNewMenu->newGame->ourImages[0].bitmap->h; ourNewMenu->lNewGame.w=ourNewMenu->newGame->ourImages[0].bitmap->w; @@ -1627,6 +1606,7 @@ void CPreGame::initMainMenu() ourMainMenu->lLoadGame.w=ourMainMenu->loadGame->ourImages[0].bitmap->w; ourMainMenu->lLoadGame.x=532; ourMainMenu->lLoadGame.y=132; + ourMainMenu->fLoadGame=&CPreGame::showLoadMenu; //high scores ourMainMenu->lHighScores.h=ourMainMenu->highScores->ourImages[0].bitmap->h; ourMainMenu->lHighScores.w=ourMainMenu->highScores->ourImages[0].bitmap->w; @@ -1696,7 +1676,7 @@ void CPreGame::highlightButton(int which, int on) void CPreGame::showCenBox (std::string data) { CMessage * cmh = new CMessage(); - SDL_Surface * infoBox = cmh->genMessage(preth->getTitle(data), preth->getDescr(data)); + SDL_Surface * infoBox = cmh->genMessage(CGI->preth->getTitle(data), CGI->preth->getDescr(data)); behindCurMes = CSDL_Ext::newSurface(infoBox->w,infoBox->h,screen); SDL_Rect pos = genRect(infoBox->h,infoBox->w, (screen->w/2)-(infoBox->w/2),(screen->h/2)-(infoBox->h/2)); @@ -1714,7 +1694,7 @@ void CPreGame::showAskBox (std::string data, void(*f1)(),void(*f2)()) std::vector * btnspos= new std::vector(0); przyciski->push_back(ok); przyciski->push_back(cancel); - SDL_Surface * infoBox = cmh->genMessage(preth->getTitle(data), preth->getDescr(data), yesOrNO, przyciski, btnspos); + SDL_Surface * infoBox = cmh->genMessage(CGI->preth->getTitle(data), CGI->preth->getDescr(data), yesOrNO, przyciski, btnspos); behindCurMes = CSDL_Ext::newSurface(infoBox->w,infoBox->h,screen); SDL_Rect pos = genRect(infoBox->h,infoBox->w, (screen->w/2)-(infoBox->w/2),(screen->h/2)-(infoBox->h/2)); @@ -1761,6 +1741,8 @@ CPreGame::menuItems * CPreGame::currentItems() return ourMainMenu; case newGame: return ourNewMenu; + case loadGame: + return ourLoadMenu; default: return NULL; } @@ -2065,6 +2047,7 @@ StartInfo CPreGame::runLoop() { highlightButton(2,2); current->highlighted=2; + (this->*(current->fLoadGame))(); } else if (isItIn(¤t->lHighScores,sEvent.motion.x,sEvent.motion.y)) { @@ -2129,31 +2112,31 @@ std::string CPreGame::buttonText(int which) switch (which) { case 0: - return CPG->preth->zelp[3].second; + return CGI->preth->zelp[3].second; case 1: - return CPG->preth->zelp[4].second; + return CGI->preth->zelp[4].second; case 2: - return CPG->preth->zelp[5].second; + return CGI->preth->zelp[5].second; case 3: - return CPG->preth->zelp[6].second; + return CGI->preth->zelp[6].second; case 4: - return CPG->preth->zelp[7].second; + return CGI->preth->zelp[7].second; } } - else if (state==newGame) + else if (state==newGame || state==loadGame) { switch (which) { case 0: - return CPG->preth->zelp[10].second; + return CGI->preth->zelp[10].second; case 1: - return CPG->preth->zelp[11].second; + return CGI->preth->zelp[11].second; case 2: - return CPG->preth->zelp[12].second; + return CGI->preth->zelp[12].second; case 3: - return CPG->preth->zelp[13].second; + return CGI->preth->zelp[13].second; case 4: - return CPG->preth->zelp[14].second; + return CGI->preth->zelp[14].second; } } return std::string(); @@ -2224,3 +2207,139 @@ void CPreGame::setTurnLength(int on) } else CSDL_Ext::printAtMiddle("Unlimited",323,563,GEOR13); } + +void CPreGame::showLoadMenu() +{ + if (currentTab/*==&ourScenSel->mapsel*/) + currentTab->hide(); + btns.clear(); + interested.clear(); + handleOther=NULL; + state = loadGame; + SDL_BlitSurface(ourLoadMenu->background,NULL,screen,NULL); + SDL_BlitSurface(ourLoadMenu->newGame->ourImages[0].bitmap,NULL,screen,&ourLoadMenu->lNewGame); + SDL_BlitSurface(ourLoadMenu->loadGame->ourImages[0].bitmap,NULL,screen,&ourLoadMenu->lLoadGame); + SDL_BlitSurface(ourLoadMenu->highScores->ourImages[0].bitmap,NULL,screen,&ourLoadMenu->lHighScores); + SDL_BlitSurface(ourLoadMenu->credits->ourImages[0].bitmap,NULL,screen,&ourLoadMenu->lCredits); + SDL_BlitSurface(ourLoadMenu->quit->ourImages[0].bitmap,NULL,screen,&ourLoadMenu->lQuit); + //SDL_Flip(screen); + CSDL_Ext::update(screen); + first = true; +} + +void CPreGame::initLoadMenu() +{ + ourLoadMenu = new menuItems(); + ourLoadMenu->bgAd = BitmapHandler::loadBitmap("ZLOADGAM.bmp"); + ourLoadMenu->background = BitmapHandler::loadBitmap("ZPIC1005.bmp"); + blitAt(ourLoadMenu->bgAd,114,312,ourLoadMenu->background); + //loading menu buttons + ourLoadMenu->newGame = CDefHandler::giveDef("ZTSINGL.DEF"); + ourLoadMenu->loadGame = CDefHandler::giveDef("ZTMULTI.DEF"); + ourLoadMenu->highScores = CDefHandler::giveDef("ZTCAMPN.DEF"); + ourLoadMenu->credits = CDefHandler::giveDef("ZTTUTOR.DEF"); + ourLoadMenu->quit = CDefHandler::giveDef("ZTBACK.DEF"); + // single scenario + ourLoadMenu->lNewGame.h=ourLoadMenu->newGame->ourImages[0].bitmap->h; + ourLoadMenu->lNewGame.w=ourLoadMenu->newGame->ourImages[0].bitmap->w; + ourLoadMenu->lNewGame.x=545; + ourLoadMenu->lNewGame.y=4; + ourLoadMenu->fNewGame=&CPreGame::showScenSel; + //multiplayer + ourLoadMenu->lLoadGame.h=ourLoadMenu->loadGame->ourImages[0].bitmap->h; + ourLoadMenu->lLoadGame.w=ourLoadMenu->loadGame->ourImages[0].bitmap->w; + ourLoadMenu->lLoadGame.x=568; + ourLoadMenu->lLoadGame.y=120; + //campaign + ourLoadMenu->lHighScores.h=ourLoadMenu->highScores->ourImages[0].bitmap->h; + ourLoadMenu->lHighScores.w=ourLoadMenu->highScores->ourImages[0].bitmap->w; + ourLoadMenu->lHighScores.x=541; + ourLoadMenu->lHighScores.y=233; + //tutorial + ourLoadMenu->lCredits.h=ourLoadMenu->credits->ourImages[0].bitmap->h; + ourLoadMenu->lCredits.w=ourLoadMenu->credits->ourImages[0].bitmap->w; + ourLoadMenu->lCredits.x=545; + ourLoadMenu->lCredits.y=358; + //back + ourLoadMenu->lQuit.h=ourLoadMenu->quit->ourImages[0].bitmap->h; + ourLoadMenu->lQuit.w=ourLoadMenu->quit->ourImages[0].bitmap->w; + ourLoadMenu->lQuit.x=582; + ourLoadMenu->lQuit.y=464; + ourLoadMenu->fQuit=&CPreGame::showMainMenu; + + ourLoadMenu->highlighted=0; +} + +CPreGame::~CPreGame() +{ + delete ourMainMenu; + delete ourNewMenu; + delete ourLoadMenu; + + delete ok; + delete cancel; +} + +CPreGame::menuItems::menuItems() +{ +} + +CPreGame::menuItems::~menuItems() +{ + delete this->newGame; + delete this->loadGame; + delete this->highScores; + delete this->credits; + delete this->quit; + SDL_FreeSurface(bgAd); + SDL_FreeSurface(background); +} + +ScenSel::ScenSel() +: + difficulty(new CPoinGroup()), + bEasy(genRect(0,0,506,456),NULL,CDefHandler::giveDef("GSPBUT3.DEF"),true,difficulty,0), + bNormal(genRect(0,0,538,456),NULL,CDefHandler::giveDef("GSPBUT4.DEF"),true,difficulty,1), + bHard(genRect(0,0,570,456),NULL,CDefHandler::giveDef("GSPBUT5.DEF"),true,difficulty,2), + bExpert(genRect(0,0,602,456),NULL,CDefHandler::giveDef("GSPBUT6.DEF"),true,difficulty,3), + bImpossible(genRect(0,0,634,456),NULL,CDefHandler::giveDef("GSPBUT7.DEF"),true,difficulty,4), + bBack(genRect(0,0,584,535),boost::bind(&CPreGame::showNewMenu,CPG),CDefHandler::giveDef("SCNRBACK.DEF")), + bBegin(genRect(0,0,414,535),boost::bind(&CPreGame::begin,CPG),CDefHandler::giveDef("SCNRBEG.DEF")), + bScens(genRect(0,0,414,81),boost::bind(&CPreGame::showScenList,CPG),CDefHandler::giveDef("GSPBUTT.DEF")), + bRandom(genRect(0,0,414,105),boost::bind(&CPreGame::showScenList,CPG),CDefHandler::giveDef("GSPBUTT.DEF")), + bOptions(genRect(0,0,414,509),boost::bind(&CPreGame::showOptions,CPG),CDefHandler::giveDef("GSPBUTT.DEF")) +{ + pressed=NULL; + listShowed=false; + if (rand()%2) + background = BitmapHandler::loadBitmap("ZPIC1000.bmp"); + else + background = BitmapHandler::loadBitmap("ZPIC1001.bmp"); + + scenInf = BitmapHandler::loadBitmap("GSELPOP1.bmp"); + randMap = BitmapHandler::loadBitmap("RANMAPBK.bmp"); + options = BitmapHandler::loadBitmap("ADVOPTBK.bmp"); + SDL_SetColorKey(scenInf,SDL_SRCCOLORKEY,SDL_MapRGB(scenInf->format,0,255,255)); + SDL_SetColorKey(randMap,SDL_SRCCOLORKEY,SDL_MapRGB(randMap->format,0,255,255)); + SDL_SetColorKey(options,SDL_SRCCOLORKEY,SDL_MapRGB(options->format,0,255,255)); + + difficulty->type=1; + selectedDiff=-77; + difficulty->gdzie = &selectedDiff; + + for (int i=0; iourImages.size(); i++) + CSDL_Ext::printAt(CGI->generaltexth->allTexts[500],25+i,2+i,GEOR13,zwykly,bScens.imgs->ourImages[i].bitmap); //"Show Available Scenarios" + for (int i=0; iourImages.size(); i++) + CSDL_Ext::printAt(CGI->generaltexth->allTexts[740],25+i,2+i,GEOR13,zwykly,bRandom.imgs->ourImages[i].bitmap); + for (int i=0; iourImages.size(); i++) + CSDL_Ext::printAt(CGI->generaltexth->allTexts[501],25+i,2+i,GEOR13,zwykly,bOptions.imgs->ourImages[i].bitmap); //"Show Advanced Options" +} + +ScenSel::~ScenSel() +{ + delete difficulty; + SDL_FreeSurface(scenInf); + SDL_FreeSurface(randMap); + SDL_FreeSurface(background); + SDL_FreeSurface(options); +} \ No newline at end of file diff --git a/CPreGame.h b/CPreGame.h index cabd9fc20..196faec83 100644 --- a/CPreGame.h +++ b/CPreGame.h @@ -28,6 +28,7 @@ struct HighButton int state; HighButton( SDL_Rect Pos, CDefHandler* Imgs, bool Sel=false, int id=-1); HighButton(); + ~HighButton(); bool selectable, selected; bool highlightable, highlighted; virtual void show(); @@ -56,8 +57,8 @@ class Slider public: bool vertical; // false means horizontal SDL_Rect pos; // position - Button up, down, //or left/right - slider; + Button *up, *down, //or left/right + *slider; int positionsAmnt, capacity;// capacity - amount of positions dispplayed at once int whereAreWe; // first displayed thing bool moving; @@ -70,6 +71,7 @@ public: void deactivate(); void activate(); Slider(int x, int y, int h, int amnt, int cap, bool ver); + ~Slider(); void updateSlid(); void handleIt(SDL_Event sev); }; @@ -188,6 +190,7 @@ public: std::vector scenImgs; //int current; std::vector ourMaps; + std::vector ourGames; IntBut small, medium, large, xlarge, all; SetrButton nrplayer, mapsize, type, name, viccon, loscon; Slider *slid, *descslid; @@ -210,6 +213,7 @@ public: class ScenSel { public: + CPoinGroup * difficulty; bool listShowed; //RanSel ransel; MapSel mapsel; @@ -217,14 +221,14 @@ public: Button bScens, bOptions, bRandom, bBegin, bBack; IntSelBut bEasy, bNormal, bHard, bExpert, bImpossible; Button * pressed; - CPoinGroup * difficulty; std::vector maps; int selectedDiff; void initRanSel(); void showRanSel(); void hideRanSel(); void genScenList(); - ~ScenSel(){delete difficulty;}; + ScenSel(); + ~ScenSel(); } ; class CPreGame { @@ -239,7 +243,6 @@ public: std::vector interested; CMusicHandler * mush; std::vector btns; - CPreGameTextHandler * preth ; SDL_Rect * currentMessage; SDL_Surface * behindCurMes; CDefHandler *ok, *cancel; @@ -247,16 +250,19 @@ public: mainMenu, newGame, loadGame, ScenarioList } state; struct menuItems { + menuItems(); + ~menuItems(); SDL_Surface * background, *bgAd; CDefHandler *newGame, *loadGame, *highScores,*credits, *quit; SDL_Rect lNewGame, lLoadGame, lHighScores, lCredits, lQuit; ttt fNewGame, fLoadGame, fHighScores, fCredits, fQuit; int highlighted;//0=none; 1=new game; 2=load game; 3=high score; 4=credits; 5=quit - } * ourMainMenu, * ourNewMenu; + } * ourMainMenu, * ourNewMenu, * ourLoadMenu; ScenSel * ourScenSel; Options * ourOptions; std::string map; //selected map CPreGame(); //c-tor + ~CPreGame();//d-tor std::string buttonText(int which); menuItems * currentItems(); void(CPreGame::*handleOther)(SDL_Event&); @@ -270,7 +276,9 @@ public: void initOptions(); void showOptions(); void initNewMenu(); + void initLoadMenu(); void showNewMenu(); + void showLoadMenu(); void showMainMenu(); StartInfo runLoop(); // runs mainloop of PreGame void initMainMenu(); //loads components for main menu diff --git a/client/Client.cpp b/client/Client.cpp index f31033daf..4bc0ee43d 100644 --- a/client/Client.cpp +++ b/client/Client.cpp @@ -698,3 +698,8 @@ void CClient::close() serv->close(); tlog3 << "Our socket has been closed.\n"; } + +void CClient::save(const std::string & fname) +{ + *serv << ui16(98) << fname; +} diff --git a/client/Client.h b/client/Client.h index 885b9c4e0..9052af776 100644 --- a/client/Client.h +++ b/client/Client.h @@ -48,6 +48,7 @@ public: ~CClient(void); void close(); + void save(const std::string & fname); void process(int what); void run(); diff --git a/lib/Connection.cpp b/lib/Connection.cpp index b721dd85d..f92603897 100644 --- a/lib/Connection.cpp +++ b/lib/Connection.cpp @@ -3,6 +3,7 @@ #include "Connection.h" #include #include +#include using namespace boost; using namespace boost::asio::ip; @@ -143,18 +144,24 @@ void CConnection::close() socket = NULL; } } -template <> -void CConnection::saveSerializable(const std::string &data) + +CSaveFile::CSaveFile( const std::string &fname ) + :sfile(new std::ofstream(fname.c_str())) { - *this << ui32(data.size()); - write(data.c_str(),data.size()); + if(!(*sfile)) + { + tlog1 << "Error: cannot open to write " << fname << std::endl; + sfile = NULL; + } } -template <> -void CConnection::loadSerializable(std::string &data) +CSaveFile::~CSaveFile() { - ui32 l; - *this >> l; - data.resize(l); - read((void*)data.c_str(),l); + delete sfile; } + +int CSaveFile::write( const void * data, unsigned size ) +{ + sfile->write((char *)data,size); + return size; +} \ No newline at end of file diff --git a/lib/Connection.h b/lib/Connection.h index ba117870c..11fc75f9e 100644 --- a/lib/Connection.h +++ b/lib/Connection.h @@ -103,6 +103,79 @@ public: template COSer & operator&(T & t){ return * this->This() << t; + } + + + + int write(const void * data, unsigned size); + template + void savePrimitive(const T &data) + { + this->This()->write(&data,sizeof(data)); + } + template + void savePointer(const T &data) + { + *this << *data; + } + template + void save(const T &data) + { + typedef + //if + typename mpl::eval_if< mpl::equal_to,mpl::int_ >, + mpl::identity >, + //else if + typename mpl::eval_if,mpl::int_ >, + mpl::identity >, + //else if + typename mpl::eval_if,mpl::int_ >, + mpl::identity >, + //else + mpl::identity > + > + > + >::type typex; + typex::invoke(* this->This(), data); + } + template + void saveSerializable(const T &data) + { + const_cast(data).serialize(*this,version); + } + template + void saveSerializable(const std::vector &data) + { + boost::uint32_t length = data.size(); + *this << length; + for(ui32 i=0;i + void saveSerializable(const std::set &data) + { + std::set &d = const_cast &>(data); + boost::uint32_t length = d.size(); + *this << length; + for(typename std::set::iterator i=d.begin();i!=d.end();i++) + *this << *i; + } + void saveSerializable(const std::string &data) + { + *this << ui32(data.size()); + this->This()->write(data.c_str(),data.size()); + } + template + void saveSerializable(const std::pair &data) + { + *this << data.first << data.second; + } + template + void saveSerializable(const std::map &data) + { + *this << ui32(data.size()); + for(typename std::map::const_iterator i=data.begin();i!=data.end();i++) + *this << i->first << i->second; } }; template class DLL_EXPORT CISer @@ -125,7 +198,94 @@ public: template CISer & operator&(T & t){ return * this->This() >> t; + } + + int write(const void * data, unsigned size); + template + void load(T &data) + { + typedef + //if + typename mpl::eval_if< mpl::equal_to,mpl::int_ >, + mpl::identity >, + //else if + typename mpl::eval_if,mpl::int_ >, + mpl::identity >, + //else if + typename mpl::eval_if,mpl::int_ >, + mpl::identity >, + //else + mpl::identity > + > + > + >::type typex; + typex::invoke(* this->This(), data); } + template + void loadPrimitive(T &data) + { + this->This()->read(&data,sizeof(data)); + } + template + void loadSerializable(T &data) + { + data.serialize(*this,version); + } + template + void loadPointer(T &data) + { + tlog5<<"Allocating memory for pointer!"<::type npT; + data = new npT; + *this >> *data; + } + template + void loadSerializable(std::vector &data) + { + boost::uint32_t length; + *this >> length; + data.resize(length); + for(ui32 i=0;i> data[i]; + } + template + void loadSerializable(std::set &data) + { + boost::uint32_t length; + *this >> length; + T ins; + for(ui32 i=0;i> ins; + data.insert(ins); + } + } + template + void loadSerializable(std::pair &data) + { + *this >> data.first >> data.second; + } + + template + void loadSerializable(std::map &data) + { + ui32 length; + *this >> length; + T1 t; + for(int i=0;i> t; + *this >> data[t]; + } + } + void loadSerializable(std::string &data) + { + ui32 l; + *this >> l; + data.resize(l); + this->This()->read((void*)data.c_str(),l); + } + }; template @@ -195,165 +355,28 @@ struct LoadWrong }; +class DLL_EXPORT CSaveFile + : public COSer +{ + void dummyMagicFunction() + { + *this << std::string("This function makes stuff working."); + } +public: + std::ofstream *sfile; + CSaveFile(const std::string &fname); + ~CSaveFile(); + int write(const void * data, unsigned size); +}; + class DLL_EXPORT CConnection :public CISer, public COSer { - - std::ostream &out; CConnection(void); void init(); public: boost::mutex *rmx, *wmx; // read/write mutexes - - template - void savePrimitive(const T &data) - { - write(&data,sizeof(data)); - } - template - void loadPrimitive(T &data) - { - read(&data,sizeof(data)); - } - - - - template - void saveSerializable(const T &data) - { - const_cast(data).serialize(*static_cast*>(this),version); - } - template - void loadSerializable(T &data) - { - data.serialize(*static_cast*>(this),version); - } - template - void savePointer(const T &data) - { - *this << *data; - } - template - void loadPointer(T &data) - { - tlog5<<"Allocating memory for pointer!"<::type npT; - data = new npT; - *this >> *data; - } - template - void saveSerializable(const std::vector &data) - { - boost::uint32_t length = data.size(); - *this << length; - for(ui32 i=0;i - void loadSerializable(std::vector &data) - { - boost::uint32_t length; - *this >> length; - data.resize(length); - for(ui32 i=0;i> data[i]; - } - - template - void saveSerializable(const std::set &data) - { - std::set &d = const_cast &>(data); - boost::uint32_t length = d.size(); - *this << length; - for(typename std::set::iterator i=d.begin();i!=d.end();i++) - *this << *i; - } - template - void loadSerializable(std::set &data) - { - boost::uint32_t length; - *this >> length; - T ins; - for(ui32 i=0;i> ins; - data.insert(ins); - } - } - - template - void saveSerializable(const std::pair &data) - { - *this << data.first << data.second; - } - template - void loadSerializable(std::pair &data) - { - *this >> data.first >> data.second; - } - - template - void saveSerializable(const std::map &data) - { - *this << ui32(data.size()); - for(typename std::map::const_iterator i=data.begin();i!=data.end();i++) - *this << i->first << i->second; - } - template - void loadSerializable(std::map &data) - { - ui32 length; - *this >> length; - T1 t; - for(int i=0;i> t; - *this >> data[t]; - } - } - template - void save(const T &data) - { - typedef - //if - typename mpl::eval_if< mpl::equal_to,mpl::int_ >, - mpl::identity >, - //else if - typename mpl::eval_if,mpl::int_ >, - mpl::identity >, - //else if - typename mpl::eval_if,mpl::int_ >, - mpl::identity >, - //else - mpl::identity > - > - > - >::type typex; - typex::invoke(*this, data); - } - - template - void load(T &data) - { - typedef - //if - typename mpl::eval_if< mpl::equal_to,mpl::int_ >, - mpl::identity >, - //else if - typename mpl::eval_if,mpl::int_ >, - mpl::identity >, - //else if - typename mpl::eval_if,mpl::int_ >, - mpl::identity >, - //else - mpl::identity > - > - > - >::type typex; - typex::invoke(*this, data); - } - boost::asio::basic_stream_socket < boost::asio::ip::tcp , boost::asio::stream_socket_service > * socket; bool logging; bool connected; @@ -374,11 +397,4 @@ public: int readLine(void * data, unsigned maxSize); void close(); ~CConnection(void); -}; - -template<> DLL_EXPORT -void CConnection::saveSerializable(const std::string &data); -template <>DLL_EXPORT -void CConnection::loadSerializable(std::string &data); - - +}; \ No newline at end of file diff --git a/map.h b/map.h index 1dff0bafa..ea29114a4 100644 --- a/map.h +++ b/map.h @@ -505,5 +505,9 @@ struct DLL_EXPORT Mapa Mapa(std::string filename); //creates map structure from .h3m file CGHeroInstance * getHero(int ID, int mode=0); bool isInTheMap(int3 pos); + template void serialize(Handler &h, const int version) + { + //TODO: write + } }; #endif //MAPD_H diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index f16ce69e2..a5b9a5283 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -407,6 +407,15 @@ void CGameHandler::handleConnection(std::set players, CConnection &c) bool blockvis = false; switch(pom) { + case 98: + { + std::string fname; + c >> fname; + CSaveFile save(fname); + save << this; + //save << this; + break; + } case 99: //end! { tlog0 << "We have been requested to close.\n"; diff --git a/server/CGameHandler.h b/server/CGameHandler.h index 01095fba5..5dd81cf0b 100644 --- a/server/CGameHandler.h +++ b/server/CGameHandler.h @@ -24,7 +24,12 @@ struct PlayerStatus { bool makingTurn, engagedIntoBattle; std::set queries; + PlayerStatus():makingTurn(false),engagedIntoBattle(false){}; + template void serialize(Handler &h, const int version) + { + h & makingTurn & engagedIntoBattle & queries; + } }; class PlayerStatuses { @@ -32,6 +37,7 @@ public: std::map players; boost::mutex mx; boost::condition_variable cv; //notifies when any changes are made + void addPlayer(ui8 player); PlayerStatus operator[](ui8 player); bool hasQueries(ui8 player); @@ -39,6 +45,10 @@ public: void setFlag(ui8 player, bool PlayerStatus::*flag, bool val); void addQuery(ui8 player, ui32 id); void removeQuery(ui8 player, ui32 id); + template void serialize(Handler &h, const int version) + { + h & players; + } }; class CGameHandler { @@ -69,6 +79,10 @@ public: ~CGameHandler(void); void init(StartInfo *si, int Seed); void handleConnection(std::set players, CConnection &c); + template void serialize(Handler &h, const int version) + { + h & QID & gs & cppscripts & states; + } template void applyAndAsk(Query * sel, ui8 player, boost::function &callback) { gsm.lock();