123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481 |
- #include "StdInc.h"
- #include "CAdvmapInterface.h"
- #include "BattleInterface/CBattleInterface.h"
- #include "BattleInterface/CBattleInterfaceClasses.h"
- #include "../CCallback.h"
- #include "CCastleInterface.h"
- #include "UIFramework/CCursorHandler.h"
- #include "CKingdomInterface.h"
- #include "CGameInfo.h"
- #include "CHeroWindow.h"
- #include "CCreatureWindow.h"
- #include "CQuestLog.h"
- #include "CMessage.h"
- #include "CPlayerInterface.h"
- //#include "UIFramework/SDL_Extensions.h"
- #include "UIFramework/SDL_Extensions.h"
- #include "../lib/CConfigHandler.h"
- #include "BattleInterface/CCreatureAnimation.h"
- #include "Graphics.h"
- #include "../lib/CArtHandler.h"
- #include "../lib/CGeneralTextHandler.h"
- #include "../lib/CHeroHandler.h"
- #include "../lib/CObjectHandler.h"
- #include "../lib/Connection.h"
- #include "../lib/CSpellHandler.h"
- #include "../lib/CTownHandler.h"
- #include "../lib/BattleState.h"
- #include "../lib/JsonNode.h"
- #include "CMusicHandler.h"
- #include "../lib/CondSh.h"
- #include "../lib/NetPacks.h"
- #include "../lib/Mapping/CMap.h"
- #include "../lib/VCMIDirs.h"
- #include "mapHandler.h"
- #include "../lib/CStopWatch.h"
- #include "../lib/StartInfo.h"
- #include "../lib/CGameState.h"
- #include "../lib/GameConstants.h"
- #include "UIFramework/CGuiHandler.h"
- #include "../lib/UnlockGuard.h"
- #ifdef min
- #undef min
- #endif
- #ifdef max
- #undef max
- #endif
- /*
- * CPlayerInterface.cpp, part of VCMI engine
- *
- * Authors: listed in file AUTHORS in main folder
- *
- * License: GNU General Public License v2.0 or later
- * Full text of license available in license.txt file, in main folder
- *
- */
- // The macro below is used to mark functions that are called by client when game state changes.
- // They all assume that CPlayerInterface::pim mutex is locked.
- #define EVENT_HANDLER_CALLED_BY_CLIENT
- // The macro marks functions that are run on a new thread by client.
- // They do not own any mutexes intiially.
- #define THREAD_CREATED_BY_CLIENT
- using namespace boost::assign;
- using namespace CSDL_Ext;
- void processCommand(const std::string &message, CClient *&client);
- extern std::queue<SDL_Event> events;
- extern boost::mutex eventsM;
- boost::recursive_mutex * CPlayerInterface::pim = new boost::recursive_mutex;
- CondSh<bool> CPlayerInterface::terminate_cond;
- CPlayerInterface * LOCPLINT;
- CBattleInterface * CPlayerInterface::battleInt;
- enum EMoveState {STOP_MOVE, WAITING_MOVE, CONTINUE_MOVE, DURING_MOVE};
- CondSh<EMoveState> stillMoveHero; //used during hero movement
- int CPlayerInterface::howManyPeople = 0;
- struct OCM_HLP_CGIN
- {
- bool inline operator ()(const std::pair<const CGObjectInstance*,SDL_Rect> & a, const std::pair<const CGObjectInstance*,SDL_Rect> & b) const
- {
- return (*a.first)<(*b.first);
- }
- } ocmptwo_cgin ;
- CPlayerInterface::CPlayerInterface(int Player)
- {
- tlog5 << "\tHuman player interface for player " << Player << " being constructed\n";
- observerInDuelMode = false;
- howManyPeople++;
- GH.defActionsDef = 0;
- LOCPLINT = this;
- curAction = NULL;
- playerID=Player;
- human=true;
- castleInt = NULL;
- battleInt = NULL;
- //pim = new boost::recursive_mutex;
- makingTurn = false;
- showingDialog = new CondSh<bool>(false);
- cingconsole = new CInGameConsole;
- terminate_cond.set(false);
- firstCall = 1; //if loading will be overwritten in serialize
- autosaveCount = 0;
- }
- CPlayerInterface::~CPlayerInterface()
- {
- howManyPeople--;
- //delete pim;
- //vstd::clear_pointer(pim);
- delete showingDialog;
- if(adventureInt)
- {
- if(adventureInt->active & CIntObject::KEYBOARD)
- adventureInt->deactivateKeyboard();
- delete adventureInt;
- adventureInt = NULL;
- }
- delete cingconsole;
- LOCPLINT = NULL;
- }
- void CPlayerInterface::init(CCallback * CB)
- {
- cb = dynamic_cast<CCallback*>(CB);
- if(observerInDuelMode)
- return;
- if(!towns.size() && !wanderingHeroes.size())
- initializeHeroTownList();
- if(!adventureInt)
- adventureInt = new CAdvMapInt();
- }
- void CPlayerInterface::yourTurn()
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- {
- boost::unique_lock<boost::mutex> lock(eventsM); //block handling events until interface is ready
- LOCPLINT = this;
- GH.curInt = this;
- adventureInt->selection = NULL;
- if(firstCall)
- {
- if(howManyPeople == 1)
- adventureInt->setPlayer(playerID);
- autosaveCount = getLastIndex("Autosave_");
- if(firstCall > 0) //new game, not loaded
- {
- int index = getLastIndex("Newgame_Autosave_");
- index %= SAVES_COUNT;
- cb->save("Saves/Newgame_Autosave_" + boost::lexical_cast<std::string>(index + 1));
- }
- firstCall = 0;
- }
- else
- {
- LOCPLINT->cb->save("Saves/Autosave_" + boost::lexical_cast<std::string>(autosaveCount++ + 1));
- autosaveCount %= 5;
- }
- if(adventureInt->player != playerID)
- adventureInt->setPlayer(playerID);
- if(howManyPeople > 1) //hot seat message
- {
- adventureInt->startHotSeatWait(playerID);
- makingTurn = true;
- std::string msg = CGI->generaltexth->allTexts[13];
- boost::replace_first(msg, "%s", cb->getStartInfo()->playerInfos.find(playerID)->second.name);
- std::vector<CComponent*> cmp;
- cmp.push_back(new CComponent(CComponent::flag, playerID, 0));
- showInfoDialog(msg, cmp);
- }
- else
- {
- makingTurn = true;
- adventureInt->startTurn();
- }
- }
- acceptTurn();
- }
- STRONG_INLINE void subRect(const int & x, const int & y, const int & z, const SDL_Rect & r, const ObjectInstanceID & hid)
- {
- TerrainTile2 & hlp = CGI->mh->ttiles[x][y][z];
- for(int h=0; h<hlp.objects.size(); ++h)
- if(hlp.objects[h].first->id == hid)
- {
- hlp.objects[h].second = r;
- return;
- }
- }
- STRONG_INLINE void delObjRect(const int & x, const int & y, const int & z, const ObjectInstanceID & hid)
- {
- TerrainTile2 & hlp = CGI->mh->ttiles[x][y][z];
- for(int h=0; h<hlp.objects.size(); ++h)
- if(hlp.objects[h].first->id == hid)
- {
- hlp.objects.erase(hlp.objects.begin()+h);
- return;
- }
- }
- void CPlayerInterface::heroMoved(const TryMoveHero & details)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- waitWhileDialog();
- if(LOCPLINT != this)
- return;
- const CGHeroInstance * ho = cb->getHero(details.id); //object representing this hero
- int3 hp = details.start;
- if(!ho)
- {
- //AI hero left the visible area (we can't obtain info)
- //TODO very evil workaround -> retreive pointer to hero so we could animate it
- // TODO -> we should not need full CGHeroInstance structure to display animation or it should not be handled by playerint (but by the client itself)
- const TerrainTile2 &tile = CGI->mh->ttiles[hp.x-1][hp.y][hp.z];
- for(int i = 0; i < tile.objects.size(); i++)
- if(tile.objects[i].first->id == details.id)
- ho = dynamic_cast<const CGHeroInstance *>(tile.objects[i].first);
- if(!ho) //still nothing...
- return;
- }
- adventureInt->centerOn(ho); //actualizing screen pos
- adventureInt->minimap.redraw();
- adventureInt->heroList.redraw();
- bool directlyAttackingCreature =
- CGI->mh->map->isInTheMap(details.attackedFrom)
- && adventureInt->terrain.currentPath //in case if movement has been canceled in the meantime and path was already erased
- && adventureInt->terrain.currentPath->nodes.size() == 3;//FIXME should be 2 but works nevertheless...
- if(makingTurn && ho->tempOwner == playerID) //we are moving our hero - we may need to update assigned path
- {
- //We may need to change music - select new track, music handler will change it if needed
- CCS->musich->playMusicFromSet("terrain", LOCPLINT->cb->getTile(ho->visitablePos())->terType, true);
- if(details.result == TryMoveHero::TELEPORTATION)
- {
- if(adventureInt->terrain.currentPath)
- {
- assert(adventureInt->terrain.currentPath->nodes.size() >= 2);
- std::vector<CGPathNode>::const_iterator nodesIt = adventureInt->terrain.currentPath->nodes.end() - 1;
- if((nodesIt)->coord == CGHeroInstance::convertPosition(details.start, false)
- && (nodesIt-1)->coord == CGHeroInstance::convertPosition(details.end, false))
- {
- //path was between entrance and exit of teleport -> OK, erase node as usual
- removeLastNodeFromPath(ho);
- }
- else
- {
- //teleport was not along current path, it'll now be invalid (hero is somewhere else)
- eraseCurrentPathOf(ho);
- }
- }
- adventureInt->heroList.update(ho);
- return; //teleport - no fancy moving animation
- //TODO: smooth disappear / appear effect
- }
- if (ho->pos != details.end //hero didn't change tile but visit succeeded
- || directlyAttackingCreature) // or creature was attacked from endangering tile.
- {
- eraseCurrentPathOf(ho, false);
- }
- else if(adventureInt->terrain.currentPath && ho->pos == details.end) //&& hero is moving
- {
- if(details.start != details.end) //so we don't touch path when revisiting with spacebar
- removeLastNodeFromPath(ho);
- }
- }
- if (details.result != TryMoveHero::SUCCESS) //hero failed to move
- {
- ho->isStanding = true;
- stillMoveHero.setn(STOP_MOVE);
- GH.totalRedraw();
- adventureInt->heroList.update(ho);
- return;
- }
- initMovement(details, ho, hp);
- //first initializing done
- GH.mainFPSmng->framerateDelay(); // after first move
- ui32 speed = settings["adventure"]["heroSpeed"].Float();
- //main moving
- for(int i=1; i<32; i+=2*speed)
- {
- movementPxStep(details, i, hp, ho);
- adventureInt->updateScreen = true;
- adventureInt->show(screen);
- CSDL_Ext::update(screen);
- GH.mainFPSmng->framerateDelay(); //for animation purposes
- } //for(int i=1; i<32; i+=4)
- //main moving done
- //finishing move
- finishMovement(details, hp, ho);
- ho->isStanding = true;
- //move finished
- adventureInt->minimap.redraw();
- adventureInt->heroList.update(ho);
- //check if user cancelled movement
- {
- boost::unique_lock<boost::mutex> un(eventsM);
- while(events.size())
- {
- SDL_Event ev = events.front();
- events.pop();
- switch(ev.type)
- {
- case SDL_MOUSEBUTTONDOWN:
- stillMoveHero.setn(STOP_MOVE);
- break;
- case SDL_KEYDOWN:
- if(ev.key.keysym.sym < SDLK_F1 || ev.key.keysym.sym > SDLK_F15)
- stillMoveHero.setn(STOP_MOVE);
- break;
- }
- }
- }
- if(stillMoveHero.get() == WAITING_MOVE)
- stillMoveHero.setn(DURING_MOVE);
- // Hero attacked creature directly, set direction to face it.
- if (directlyAttackingCreature) {
- // Get direction to attacker.
- int3 posOffset = details.attackedFrom - details.end + int3(2, 1, 0);
- static const ui8 dirLookup[3][3] = {
- { 1, 2, 3 },
- { 8, 0, 4 },
- { 7, 6, 5 }
- };
- // FIXME: Avoid const_cast, make moveDir mutable in some other way?
- const_cast<CGHeroInstance *>(ho)->moveDir = dirLookup[posOffset.y][posOffset.x];
- }
- }
- void CPlayerInterface::heroKilled(const CGHeroInstance* hero)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- const CArmedInstance *newSelection = nullptr;
- if (makingTurn)
- {
- //find new object for selection: either hero
- int next = adventureInt->getNextHeroIndex(vstd::find_pos(wanderingHeroes, hero));
- if (next >= 0)
- newSelection = wanderingHeroes[next];
- //or town
- if (!newSelection || newSelection == hero)
- {
- if (towns.empty())
- newSelection = nullptr;
- else
- newSelection = towns.front();
- }
- }
- wanderingHeroes -= hero;
- if(vstd::contains(paths, hero))
- paths.erase(hero);
- adventureInt->heroList.update(hero);
- if (makingTurn)
- adventureInt->select(newSelection, true);
- else
- adventureInt->selection = nullptr;
- }
- void CPlayerInterface::heroCreated(const CGHeroInstance * hero)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- wanderingHeroes.push_back(hero);
- adventureInt->heroList.update(hero);
- }
- void CPlayerInterface::openTownWindow(const CGTownInstance * town)
- {
- if (castleInt)
- castleInt->close();
- castleInt = new CCastleInterface(town);
- GH.pushInt(castleInt);
- }
- int3 CPlayerInterface::repairScreenPos(int3 pos)
- {
- if(pos.x<-CGI->mh->frameW)
- pos.x = -CGI->mh->frameW;
- if(pos.y<-CGI->mh->frameH)
- pos.y = -CGI->mh->frameH;
- if(pos.x>CGI->mh->sizes.x - adventureInt->terrain.tilesw + CGI->mh->frameW)
- pos.x = CGI->mh->sizes.x - adventureInt->terrain.tilesw + CGI->mh->frameW;
- if(pos.y>CGI->mh->sizes.y - adventureInt->terrain.tilesh + CGI->mh->frameH)
- pos.y = CGI->mh->sizes.y - adventureInt->terrain.tilesh + CGI->mh->frameH;
- return pos;
- }
- void CPlayerInterface::heroPrimarySkillChanged(const CGHeroInstance * hero, int which, si64 val)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if(which == 4)
- {
- if(CAltarWindow *ctw = dynamic_cast<CAltarWindow *>(GH.topInt()))
- ctw->setExpToLevel();
- }
- else if(which < GameConstants::PRIMARY_SKILLS) //no need to redraw infowin if this is experience (exp is treated as prim skill with id==4)
- updateInfo(hero);
- }
- void CPlayerInterface::heroSecondarySkillChanged(const CGHeroInstance * hero, int which, int val)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- CUniversityWindow* cuw = dynamic_cast<CUniversityWindow*>(GH.topInt());
- if(cuw) //university window is open
- {
- GH.totalRedraw();
- }
- }
- void CPlayerInterface::heroManaPointsChanged(const CGHeroInstance * hero)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- updateInfo(hero);
- if(makingTurn && hero->tempOwner == playerID)
- adventureInt->heroList.update(hero);
- }
- void CPlayerInterface::heroMovePointsChanged(const CGHeroInstance * hero)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if(makingTurn && hero->tempOwner == playerID)
- adventureInt->heroList.update(hero);
- }
- void CPlayerInterface::receivedResource(int type, int val)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if(CMarketplaceWindow *mw = dynamic_cast<CMarketplaceWindow *>(GH.topInt()))
- mw->resourceChanged(type, val);
- GH.totalRedraw();
- }
- void CPlayerInterface::heroGotLevel(const CGHeroInstance *hero, PrimarySkill::PrimarySkill pskill, std::vector<SecondarySkill>& skills, int queryID)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- waitWhileDialog();
- CCS->soundh->playSound(soundBase::heroNewLevel);
- CLevelWindow *lw = new CLevelWindow(hero,pskill,skills,
- [=](ui32 selection){ cb->selectionMade(selection, queryID); });
- GH.pushInt(lw);
- }
- void CPlayerInterface::commanderGotLevel (const CCommanderInstance * commander, std::vector<ui32> skills, int queryID)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- waitWhileDialog();
- CCS->soundh->playSound(soundBase::heroNewLevel);
- CCreatureWindow * cw = new CCreatureWindow(skills, commander,
- [=](ui32 selection){ cb->selectionMade(selection, queryID); });
- GH.pushInt(cw);
- }
- void CPlayerInterface::heroInGarrisonChange(const CGTownInstance *town)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- updateInfo(town);
- if(town->garrisonHero && vstd::contains(wanderingHeroes,town->garrisonHero)) //wandering hero moved to the garrison
- {
- CGI->mh->hideObject(town->garrisonHero);
- if (town->garrisonHero->tempOwner == playerID) // our hero
- wanderingHeroes -= town->garrisonHero;
- }
- if(town->visitingHero && !vstd::contains(wanderingHeroes,town->visitingHero)) //hero leaves garrison
- {
- CGI->mh->printObject(town->visitingHero);
- if (town->visitingHero->tempOwner == playerID) // our hero
- wanderingHeroes.push_back(town->visitingHero);
- }
- adventureInt->heroList.update();
- adventureInt->updateNextHero(NULL);
- if(CCastleInterface *c = castleInt)
- {
- c->garr->selectSlot(nullptr);
- c->garr->setArmy(town->getUpperArmy(), 0);
- c->garr->setArmy(town->visitingHero, 1);
- c->garr->recreateSlots();
- c->heroes->update();
- }
- BOOST_FOREACH(IShowActivatable *isa, GH.listInt)
- {
- CKingdomInterface *ki = dynamic_cast<CKingdomInterface*>(isa);
- if (ki)
- {
- ki->townChanged(town);
- ki->updateGarrisons();
- }
- }
- GH.totalRedraw();
- }
- void CPlayerInterface::heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if(hero->tempOwner != playerID )
- return;
- waitWhileDialog();
- openTownWindow(town);
- }
- void CPlayerInterface::garrisonsChanged(std::vector<const CGObjectInstance *> objs)
- {
- boost::unique_lock<boost::recursive_mutex> un(*pim);
- BOOST_FOREACH(auto object, objs)
- updateInfo(object);
- for(std::list<IShowActivatable*>::iterator i = GH.listInt.begin(); i != GH.listInt.end(); i++)
- {
- CGarrisonHolder *cgh = dynamic_cast<CGarrisonHolder*>(*i);
- if (cgh)
- cgh->updateGarrisons();
- if(CTradeWindow *cmw = dynamic_cast<CTradeWindow*>(*i))
- {
- if(vstd::contains(objs, cmw->hero))
- cmw->garrisonChanged();
- }
- }
- GH.totalRedraw();
- }
- void CPlayerInterface::garrisonChanged( const CGObjectInstance * obj)
- {
- garrisonsChanged(std::vector<const CGObjectInstance *>(1, obj));
- }
- void CPlayerInterface::buildChanged(const CGTownInstance *town, BuildingID buildingID, int what) //what: 1 - built, 2 - demolished
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- switch (buildingID)
- {
- case BuildingID::FORT: case BuildingID::CITADEL: case BuildingID::CASTLE:
- case BuildingID::VILLAGE_HALL: case BuildingID::TOWN_HALL: case BuildingID::CITY_HALL: case BuildingID::CAPITOL:
- case BuildingID::RESOURCE_SILO:
- updateInfo(town);
- break;
- }
- if(!castleInt)
- return;
- if(castleInt->town!=town)
- return;
- switch(what)
- {
- case 1:
- CCS->soundh->playSound(soundBase::newBuilding);
- castleInt->addBuilding(buildingID);
- break;
- case 2:
- castleInt->removeBuilding(buildingID);
- break;
- }
- adventureInt->townList.update(town);
- castleInt->townlist->update(town);
- }
- void CPlayerInterface::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool side)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if(LOCPLINT != this)
- { //another local interface should do this
- return;
- }
- waitForAllDialogs();
- GH.pushInt(battleInt);
- }
- void CPlayerInterface::battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, bool tentHeal, si32 lifeDrainFrom)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if(LOCPLINT != this)
- { //another local interface should do this
- return;
- }
- for(int b=0; b<healedStacks.size(); ++b)
- {
- const CStack * healed = cb->battleGetStackByID(healedStacks[b].first);
- if(battleInt->creAnims[healed->ID]->getType() == CCreatureAnim::DEATH)
- {
- //stack has been resurrected
- battleInt->creAnims[healed->ID]->setType(CCreatureAnim::HOLDING);
- }
- }
- if (lifeDrain)
- {
- const CStack *attacker = cb->battleGetStackByID(healedStacks[0].first, false);
- const CStack *defender = cb->battleGetStackByID(lifeDrainFrom, false);
- int textOff = 0;
- if (attacker)
- {
- battleInt->displayEffect(52, attacker->position); //TODO: transparency
- if (attacker->count > 1)
- {
- textOff += 1;
- }
- CCS->soundh->playSound(soundBase::DRAINLIF);
- }
- //print info about life drain
- char textBuf[1000];
- sprintf(textBuf, CGI->generaltexth->allTexts[361 + textOff].c_str(), attacker->getCreature()->nameSing.c_str(),
- healedStacks[0].second, defender->getCreature()->namePl.c_str());
- battleInt->console->addText(textBuf);
- }
- if (tentHeal)
- {
- std::string text = CGI->generaltexth->allTexts[414];
- boost::algorithm::replace_first(text, "%s", cb->battleGetStackByID(lifeDrainFrom, false)->getCreature()->nameSing);
- boost::algorithm::replace_first(text, "%s", cb->battleGetStackByID(healedStacks[0].first, false)->getCreature()->nameSing);
- boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(healedStacks[0].second));
- battleInt->console->addText(text);
- }
- }
- void CPlayerInterface::battleNewStackAppeared(const CStack * stack)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if(LOCPLINT != this)
- { //another local interface should do this
- return;
- }
- battleInt->newStack(stack);
- }
- void CPlayerInterface::battleObstaclesRemoved(const std::set<si32> & removedObstacles)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if(LOCPLINT != this)
- { //another local interface should do this
- return;
- }
- // for(std::set<si32>::const_iterator it = removedObstacles.begin(); it != removedObstacles.end(); ++it)
- // {
- // for(std::map< int, CDefHandler * >::iterator itBat = battleInt->idToObstacle.begin(); itBat != battleInt->idToObstacle.end(); ++itBat)
- // {
- // if(itBat->first == *it) //remove this obstacle
- // {
- // battleInt->idToObstacle.erase(itBat);
- // break;
- // }
- // }
- // }
- //update accessible hexes
- battleInt->redrawBackgroundWithHexes(battleInt->activeStack);
- }
- void CPlayerInterface::battleCatapultAttacked(const CatapultAttack & ca)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if(LOCPLINT != this)
- { //another local interface should do this
- return;
- }
- battleInt->stackIsCatapulting(ca);
- }
- void CPlayerInterface::battleStacksRemoved(const BattleStacksRemoved & bsr)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if(LOCPLINT != this)
- { //another local interface should do this
- return;
- }
- for(std::set<ui32>::const_iterator it = bsr.stackIDs.begin(); it != bsr.stackIDs.end(); ++it) //for each removed stack
- {
- battleInt->stackRemoved(*it);
- //battleInt->stackRemoved(LOCPLINT->cb->battleGetStackByID(*it));
- }
- }
- void CPlayerInterface::battleNewRound(int round) //called at the beginning of each turn, round=-1 is the tactic phase, round=0 is the first "normal" turn
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if(LOCPLINT != this)
- { //another local interface should do this
- return;
- }
- battleInt->newRound(round);
- }
- void CPlayerInterface::actionStarted(const BattleAction* action)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if(LOCPLINT != this)
- { //another local interface should do this
- return;
- }
- curAction = new BattleAction(*action);
- battleInt->startAction(action);
- }
- void CPlayerInterface::actionFinished(const BattleAction* action)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if(LOCPLINT != this)
- { //another local interface should do this
- return;
- }
- delete curAction;
- curAction = NULL;
- battleInt->endAction(action);
- }
- BattleAction CPlayerInterface::activeStack(const CStack * stack) //called when it's turn of that stack
- {
- THREAD_CREATED_BY_CLIENT;
- tlog5 << "Awaiting command for " << stack->nodeName() << std::endl;
- CBattleInterface *b = battleInt;
- assert(!b->givenCommand->get()); //command buffer must be clean (we don't want to use old command)
- {
- boost::unique_lock<boost::recursive_mutex> un(*pim);
- b->stackActivated(stack);
- //Regeneration & mana drain go there
- }
- //wait till BattleInterface sets its command
- boost::unique_lock<boost::mutex> lock(b->givenCommand->mx);
- while(!b->givenCommand->data)
- {
- b->givenCommand->cond.wait(lock);
- if(!battleInt) //batle ended while we were waiting for movement (eg. because of spell)
- throw boost::thread_interrupted(); //will shut the thread peacefully
- }
- //tidy up
- BattleAction ret = *(b->givenCommand->data);
- delete b->givenCommand->data;
- b->givenCommand->data = NULL;
- //return command
- tlog5 << "Giving command for " << stack->nodeName() << std::endl;
- return ret;
- }
- void CPlayerInterface::battleEnd(const BattleResult *br)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if(LOCPLINT != this)
- { //another local interface should do this
- return;
- }
- battleInt->battleFinished(*br);
- }
- void CPlayerInterface::battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if(LOCPLINT != this)
- { //another local interface should do this
- return;
- }
- battleInt->stackMoved(stack, dest, distance);
- }
- void CPlayerInterface::battleSpellCast( const BattleSpellCast *sc )
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if(LOCPLINT != this)
- { //another local interface should do this
- return;
- }
- battleInt->spellCast(sc);
- }
- void CPlayerInterface::battleStacksEffectsSet( const SetStackEffect & sse )
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if(LOCPLINT != this)
- { //another local interface should do this
- return;
- }
- battleInt->battleStacksEffectsSet(sse);
- }
- void CPlayerInterface::battleTriggerEffect (const BattleTriggerEffect & bte)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- battleInt->battleTriggerEffect(bte);
- }
- void CPlayerInterface::battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if(LOCPLINT != this)
- { //another local interface should do this
- return;
- }
-
- std::vector<StackAttackedInfo> arg;
- for(std::vector<BattleStackAttacked>::const_iterator i = bsa.begin(); i != bsa.end(); i++)
- {
- const CStack *defender = cb->battleGetStackByID(i->stackAttacked, false);
- const CStack *attacker = cb->battleGetStackByID(i->attackerID, false);
- if(i->isEffect() && i->effect != 12) //and not armageddon
- {
- if (defender && !i->isSecondary())
- battleInt->displayEffect(i->effect, defender->position);
- }
- bool shooting = (LOCPLINT->curAction ? LOCPLINT->curAction->actionType == Battle::SHOOT : false); //FIXME: why action is deleted during enchanter cast?
- StackAttackedInfo to_put = {defender, i->damageAmount, i->killedAmount, attacker, shooting, i->killed(), i->willRebirth(), i->cloneKilled()};
- arg.push_back(to_put);
- }
- if(bsa.begin()->isEffect() && bsa.begin()->effect == 12) //for armageddon - I hope this condition is enough
- {
- battleInt->displayEffect(bsa.begin()->effect, -1);
- }
- battleInt->stacksAreAttacked(arg);
- }
- void CPlayerInterface::battleAttack(const BattleAttack *ba)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if(LOCPLINT != this)
- { //another local interface should do this
- return;
- }
- assert(curAction);
- if(ba->lucky()) //lucky hit
- {
- const CStack *stack = cb->battleGetStackByID(ba->stackAttacking);
- std::string hlp = CGI->generaltexth->allTexts[45];
- boost::algorithm::replace_first(hlp,"%s", (stack->count != 1) ? stack->getCreature()->namePl.c_str() : stack->getCreature()->nameSing.c_str());
- battleInt->console->addText(hlp);
- battleInt->displayEffect(18, stack->position);
- }
- //TODO: bad luck?
- if (ba->deathBlow())
- {
- const CStack *stack = cb->battleGetStackByID(ba->stackAttacking);
- std::string hlp = CGI->generaltexth->allTexts[(stack->count != 1) ? 366 : 365];
- boost::algorithm::replace_first(hlp,"%s", (stack->count != 1) ? stack->getCreature()->namePl.c_str() : stack->getCreature()->nameSing.c_str());
- battleInt->console->addText(hlp);
- for (std::vector<BattleStackAttacked>::const_iterator i = ba->bsa.begin(); i != ba->bsa.end(); i++)
- {
- const CStack * attacked = cb->battleGetStackByID(i->stackAttacked);
- battleInt->displayEffect(73, attacked->position);
- }
- }
- const CStack * attacker = cb->battleGetStackByID(ba->stackAttacking);
- if(ba->shot())
- {
- for(std::vector<BattleStackAttacked>::const_iterator i = ba->bsa.begin(); i != ba->bsa.end(); i++)
- {
- if (!i->isSecondary()) //display projectile only for primary target
- {
- const CStack * attacked = cb->battleGetStackByID(i->stackAttacked);
- battleInt->stackAttacking(attacker, attacked->position, attacked, true);
- }
- }
- }
- else
- {
- int shift = 0;
- if(ba->counter() && BattleHex::mutualPosition(curAction->destinationTile, attacker->position) < 0)
- {
- int distp = BattleHex::getDistance(curAction->destinationTile + 1, attacker->position);
- int distm = BattleHex::getDistance(curAction->destinationTile - 1, attacker->position);
- if( distp < distm )
- shift = 1;
- else
- shift = -1;
- }
- const CStack * attacked = cb->battleGetStackByID(ba->bsa.begin()->stackAttacked);
- battleInt->stackAttacking( attacker, ba->counter() ? curAction->destinationTile + shift : curAction->additionalInfo, attacked, false);
- }
- }
- void CPlayerInterface::battleObstaclePlaced(const CObstacleInstance &obstacle)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if(LOCPLINT != this)
- { //another local interface should do this
- return;
- }
- battleInt->obstaclePlaced(obstacle);
- }
- void CPlayerInterface::yourTacticPhase(int distance)
- {
- THREAD_CREATED_BY_CLIENT;
- while(battleInt && battleInt->tacticsMode)
- boost::this_thread::sleep(boost::posix_time::millisec(1));
- }
- void CPlayerInterface::showComp(const Component &comp, std::string message)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- waitWhileDialog(); //Fix for mantis #98
- CCS->soundh->playSoundFromSet(CCS->soundh->pickupSounds);
- adventureInt->infoBar.showComponent(comp, message);
- }
- void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<Component*> &components, int soundID)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if (settings["session"]["autoSkip"].Bool() && !LOCPLINT->shiftPressed())
- {
- return;
- }
- std::vector<CComponent*> intComps;
- for(int i=0;i<components.size();i++)
- intComps.push_back(new CComponent(*components[i]));
- showInfoDialog(text,intComps,soundID);
- }
- void CPlayerInterface::showInfoDialog(const std::string &text, CComponent * component)
- {
- std::vector<CComponent*> intComps;
- intComps.push_back(component);
- showInfoDialog(text, intComps, soundBase::sound_todo, true);
- }
- void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<CComponent*> & components, int soundID, bool delComps)
- {
- waitWhileDialog();
- if (settings["session"]["autoSkip"].Bool() && !LOCPLINT->shiftPressed())
- {
- return;
- }
- CInfoWindow *temp = CInfoWindow::create(text, playerID, &components);
- temp->setDelComps(delComps);
- if(makingTurn && GH.listInt.size() && LOCPLINT == this)
- {
- CCS->soundh->playSound(static_cast<soundBase::soundID>(soundID));
- showingDialog->set(true);
- stopMovement(); // interrupt movement to show dialog
- GH.pushInt(temp);
- }
- else
- {
- dialogs.push_back(temp);
- }
- }
- void CPlayerInterface::showYesNoDialog(const std::string &text, CFunctionList<void()> onYes, CFunctionList<void()> onNo, bool DelComps, const std::vector<CComponent*> & components)
- {
- boost::unique_lock<boost::recursive_mutex> un(*pim);
- stopMovement();
- LOCPLINT->showingDialog->setn(true);
- CInfoWindow::showYesNoDialog(text, &components, onYes, onNo, DelComps, playerID);
- }
- void CPlayerInterface::showBlockingDialog( const std::string &text, const std::vector<Component> &components, ui32 askID, int soundID, bool selection, bool cancel )
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- waitWhileDialog();
- stopMovement();
- CCS->soundh->playSound(static_cast<soundBase::soundID>(soundID));
- if(!selection && cancel) //simple yes/no dialog
- {
- std::vector<CComponent*> intComps;
- for(int i=0;i<components.size();i++)
- intComps.push_back(new CComponent(components[i])); //will be deleted by close in window
- showYesNoDialog(text, boost::bind(&CCallback::selectionMade,cb,1,askID),boost::bind(&CCallback::selectionMade,cb,0,askID),true, intComps);
- }
- else if(selection)
- {
- std::vector<CSelectableComponent*> intComps;
- for(int i=0;i<components.size();i++)
- intComps.push_back(new CSelectableComponent(components[i])); //will be deleted by CSelWindow::close
- std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
- pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
- if(cancel)
- {
- pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
- }
- int charperline = 35;
- if (pom.size() > 1)
- charperline = 50;
- CSelWindow * temp = new CSelWindow(text, playerID, charperline, intComps, pom, askID);
- GH.pushInt(temp);
- intComps[0]->clickLeft(true, false);
- }
- }
- void CPlayerInterface::tileRevealed(const boost::unordered_set<int3, ShashInt3> &pos)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- for(boost::unordered_set<int3, ShashInt3>::const_iterator i=pos.begin(); i!=pos.end();i++)
- adventureInt->minimap.showTile(*i);
- if(pos.size())
- GH.totalRedraw();
- }
- void CPlayerInterface::tileHidden(const boost::unordered_set<int3, ShashInt3> &pos)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- for(boost::unordered_set<int3, ShashInt3>::const_iterator i=pos.begin(); i!=pos.end();i++)
- adventureInt->minimap.hideTile(*i);
- if(pos.size())
- GH.totalRedraw();
- }
- void CPlayerInterface::openHeroWindow(const CGHeroInstance *hero)
- {
- boost::unique_lock<boost::recursive_mutex> un(*pim);
- GH.pushInt(new CHeroWindow(hero));
- }
- /*
- void CPlayerInterface::heroArtifactSetChanged(const CGHeroInstance*hero)
- {
- boost::unique_lock<boost::recursive_mutex> un(*pim);
- if(adventureInt->heroWindow->curHero && adventureInt->heroWindow->curHero->id == hero->id) //hero window is opened
- {
- adventureInt->heroWindow->deactivate();
- adventureInt->heroWindow->setHero(hero);
- adventureInt->heroWindow->activate();
- }
- else if(CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(GH.topInt())) //exchange window is open
- {
- cew->deactivate();
- for(int g=0; g<ARRAY_COUNT(cew->heroInst); ++g)
- {
- if(cew->heroInst[g]->id == hero->id)
- {
- cew->heroInst[g] = hero;
- cew->artifs[g]->updateState = true;
- cew->artifs[g]->setHero(hero);
- cew->artifs[g]->updateState = false;
- }
- }
- cew->prepareBackground();
- cew->activate();
- }
- else if(CTradeWindow *caw = dynamic_cast<CTradeWindow*>(GH.topInt()))
- {
- if(caw->arts)
- {
- caw->deactivate();
- caw->arts->updateState = true;
- caw->arts->setHero(hero);
- caw->arts->updateState = false;
- caw->activate();
- }
- }
- updateInfo(hero);
- }*/
- void CPlayerInterface::availableCreaturesChanged( const CGDwelling *town )
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if(const CGTownInstance * townObj = dynamic_cast<const CGTownInstance*>(town))
- {
- CFortScreen *fs = dynamic_cast<CFortScreen*>(GH.topInt());
- if(fs)
- fs->creaturesChanged();
- BOOST_FOREACH(IShowActivatable *isa, GH.listInt)
- {
- CKingdomInterface *ki = dynamic_cast<CKingdomInterface*>(isa);
- if (ki && townObj)
- ki->townChanged(townObj);
- }
- }
- else if(GH.listInt.size() && (town->ID == Obj::CREATURE_GENERATOR1
- || town->ID == Obj::CREATURE_GENERATOR4 || town->ID == Obj::WAR_MACHINE_FACTORY))
- {
- CRecruitmentWindow *crw = dynamic_cast<CRecruitmentWindow*>(GH.topInt());
- if(crw && crw->dwelling == town)
- crw->availableCreaturesChanged();
- }
- }
- void CPlayerInterface::heroBonusChanged( const CGHeroInstance *hero, const Bonus &bonus, bool gain )
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if(bonus.type == Bonus::NONE)
- return;
- updateInfo(hero);
- if ((bonus.type == Bonus::FLYING_MOVEMENT || bonus.type == Bonus::WATER_WALKING) && !gain)
- {
- //recalculate paths because hero has lost bonus influencing pathfinding
- eraseCurrentPathOf(hero, false);
- }
- }
- template <typename Handler> void CPlayerInterface::serializeTempl( Handler &h, const int version )
- {
- h & playerID;
- h & spellbookSettings;
- //sleeping heroes
- ui8 sleepingSize = 0; //fix for uninitialized warning
- if (h.saving)
- sleepingSize = sleepingHeroes.size();
- h & sleepingSize;
- for (int i = 0; i < sleepingSize; i++)
- {
- ObjectInstanceID hid;
- if (h.saving)
- hid = sleepingHeroes[i]->id;
- h & hid;
- if (!h.saving)
- {
- const CGHeroInstance *hero = cb->getHero(hid);
- sleepingHeroes += hero;
- }
- }
- //hero list order
- ui8 heroListSize = 0; //fix for uninitialized warning
- if (h.saving)
- heroListSize = wanderingHeroes.size();
- else
- wanderingHeroes.clear();
- h & heroListSize;
- for (int i = 0; i < heroListSize; i++)
- {
- ObjectInstanceID hid;
- if (h.saving)
- hid = wanderingHeroes[i]->id;
- h & hid;
- if (!h.saving)
- {
- const CGHeroInstance *hero = cb->getHero(hid);
- wanderingHeroes += hero;
- }
- }
- }
- void CPlayerInterface::serialize( COSer<CSaveFile> &h, const int version )
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- serializeTempl(h,version);
- }
- void CPlayerInterface::serialize( CISer<CLoadFile> &h, const int version )
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- serializeTempl(h,version);
- firstCall = -1;
- }
- bool CPlayerInterface::moveHero( const CGHeroInstance *h, CGPath path )
- {
- tlog5 << __FUNCTION__ << std::endl;
- if(!LOCPLINT->makingTurn)
- return false;
- if (!h)
- return false; //can't find hero
- //It shouldn't be possible to move hero with open dialog (or dialog waiting in bg)
- if(showingDialog->get() || dialogs.size())
- return false;
- if (adventureInt && adventureInt->isHeroSleeping(h))
- {
- adventureInt->sleepWake.clickLeft(true, false);
- adventureInt->sleepWake.clickLeft(false, true);
- //could've just called
- //adventureInt->fsleepWake();
- //but no authentic button click/sound ;-)
- }
- int i = 1;
- bool result = false; //TODO why not set to true anywhere?
- {
- //evil...
- tlog5 << "before [un]locks in " << __FUNCTION__ << std::endl;
- auto unlockEvents = vstd::makeUnlockGuard(eventsM);
- auto unlockGs = vstd::makeUnlockSharedGuard(cb->getGsMutex()); //GS mutex is above PIM because CClient::run thread first locks PIM and then GS -> so this way we avoid deadlocks
- auto unlockPim = vstd::makeUnlockGuard(*pim);
- tlog5 << "after [un]locks in " << __FUNCTION__ << std::endl;
- //TODO the above combination works... but it should all be atomic (unlock all three or none)
- {
- path.convert(0);
- boost::unique_lock<boost::mutex> un(stillMoveHero.mx);
- stillMoveHero.data = CONTINUE_MOVE;
- ETerrainType currentTerrain = ETerrainType::BORDER; // not init yet
- ETerrainType newTerrain;
- int sh = -1;
- const TerrainTile * curTile = cb->getTile(CGHeroInstance::convertPosition(h->pos, false));
- for(i=path.nodes.size()-1; i>0 && (stillMoveHero.data == CONTINUE_MOVE || curTile->blocked); i--)
- {
- //changing z coordinate means we're moving through subterranean gate -> it's done automatically upon the visit, so we don't have to request that move here
- if(path.nodes[i-1].coord.z != path.nodes[i].coord.z)
- continue;
- //stop sending move requests if the next node can't be reached at the current turn (hero exhausted his move points)
- if(path.nodes[i-1].turns)
- {
- stillMoveHero.data = STOP_MOVE;
- break;
- }
- // Start a new sound for the hero movement or let the existing one carry on.
- #if 0
- // TODO
- if (hero is flying && sh == -1)
- sh = CCS->soundh->playSound(soundBase::horseFlying, -1);
- #endif
- {
- newTerrain = cb->getTile(CGHeroInstance::convertPosition(path.nodes[i].coord, false))->terType;
- if (newTerrain != currentTerrain)
- {
- CCS->soundh->stopSound(sh);
- sh = CCS->soundh->playSound(CCS->soundh->horseSounds[newTerrain], -1);
- currentTerrain = newTerrain;
- }
- }
- stillMoveHero.data = WAITING_MOVE;
- int3 endpos(path.nodes[i-1].coord.x, path.nodes[i-1].coord.y, h->pos.z);
- bool guarded = CGI->mh->map->isInTheMap(cb->guardingCreaturePosition(endpos - int3(1, 0, 0)));
- tlog5 << "Requesting hero movement to " << endpos << std::endl;
- cb->moveHero(h,endpos);
- while(stillMoveHero.data != STOP_MOVE && stillMoveHero.data != CONTINUE_MOVE)
- stillMoveHero.cond.wait(un);
- tlog5 << "Resuming " << __FUNCTION__ << std::endl;
- if (guarded || showingDialog->get() == true) // Abort movement if a guard was fought or there is a dialog to display (Mantis #1136)
- break;
- }
- CCS->soundh->stopSound(sh);
- }
- //Update cursor so icon can change if needed when it reappears; doesn;'t apply if a dialog box pops up at the end of the movement
- if(!showingDialog->get())
- GH.fakeMouseMove();
- //RAII unlocks
- }
- if (adventureInt)
- {
- // (i == 0) means hero went through all the path
- adventureInt->updateMoveHero(h, (i != 0));
- adventureInt->updateNextHero(h);
- }
- return result;
- }
- bool CPlayerInterface::shiftPressed() const
- {
- return SDL_GetKeyState(NULL)[SDLK_LSHIFT] || SDL_GetKeyState(NULL)[SDLK_RSHIFT];
- }
- bool CPlayerInterface::altPressed() const
- {
- return SDL_GetKeyState(NULL)[SDLK_LALT] || SDL_GetKeyState(NULL)[SDLK_RALT];
- }
- void CPlayerInterface::showGarrisonDialog( const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, int queryID)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- auto onEnd = [=]{ cb->selectionMade(0, queryID); };
- if(stillMoveHero.get() == DURING_MOVE && adventureInt->terrain.currentPath && adventureInt->terrain.currentPath->nodes.size() > 1) //to ignore calls on passing through garrisons
- {
- onEnd();
- return;
- }
- waitForAllDialogs();
- CGarrisonWindow *cgw = new CGarrisonWindow(up,down,removableUnits);
- cgw->quit->callback += onEnd;
- GH.pushInt(cgw);
- }
- /**
- * Shows the dialog that appears when right-clicking an artifact that can be assembled
- * into a combinational one on an artifact screen. Does not require the combination of
- * artifacts to be legal.
- * @param artifactID ID of a constituent artifact.
- * @param assembleTo ID of artifact to assemble a constituent into, not used when assemble
- * is false.
- * @param assemble True if the artifact is to be assembled, false if it is to be disassembled.
- */
- void CPlayerInterface::showArtifactAssemblyDialog (ui32 artifactID, ui32 assembleTo, bool assemble, CFunctionList<void()> onYes, CFunctionList<void()> onNo)
- {
- const CArtifact &artifact = *CGI->arth->artifacts[artifactID];
- std::string text = artifact.Description();
- text += "\n\n";
- std::vector<CComponent*> scs;
- if (assemble) {
- const CArtifact &assembledArtifact = *CGI->arth->artifacts[assembleTo];
- // You possess all of the components to...
- text += boost::str(boost::format(CGI->generaltexth->allTexts[732]) % assembledArtifact.Name());
- // Picture of assembled artifact at bottom.
- CComponent* sc = new CComponent(CComponent::artifact, assembledArtifact.id, 0);
- //sc->description = assembledArtifact.Description();
- //sc->subtitle = assembledArtifact.Name();
- scs.push_back(sc);
- } else {
- // Do you wish to disassemble this artifact?
- text += CGI->generaltexth->allTexts[733];
- }
- showYesNoDialog(text, onYes, onNo, true, scs);
- }
- void CPlayerInterface::requestRealized( PackageApplied *pa )
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if(pa->packType == typeList.getTypeID<MoveHero>() && stillMoveHero.get() == DURING_MOVE)
- stillMoveHero.setn(CONTINUE_MOVE);
- }
- void CPlayerInterface::heroExchangeStarted(ObjectInstanceID hero1, ObjectInstanceID hero2)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- GH.pushInt(new CExchangeWindow(hero1, hero2));
- }
- void CPlayerInterface::objectPropertyChanged(const SetObjectProperty * sop)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- //redraw minimap if owner changed
- if(sop->what == ObjProperty::OWNER)
- {
- const CGObjectInstance * obj = cb->getObj(sop->id);
- std::set<int3> pos = obj->getBlockedPos();
- for(std::set<int3>::const_iterator it = pos.begin(); it != pos.end(); ++it)
- {
- if(cb->isVisible(*it))
- adventureInt->minimap.showTile(*it);
- }
- if(obj->ID == Obj::TOWN)
- {
- if(obj->tempOwner == playerID)
- towns.push_back(static_cast<const CGTownInstance *>(obj));
- else
- towns -= obj;
- adventureInt->townList.update();
- }
- assert(cb->getTownsInfo().size() == towns.size());
- }
- }
- void CPlayerInterface::initializeHeroTownList()
- {
- std::vector<const CGHeroInstance*> allHeroes = cb->getHeroesInfo();
- /*
- std::vector <const CGHeroInstance *> newWanderingHeroes;
- //applying current heroes order to new heroes info
- int j;
- for (int i = 0; i < wanderingHeroes.size(); i++)
- if ((j = vstd::find_pos(allHeroes, wanderingHeroes[i])) >= 0)
- if (!allHeroes[j]->inTownGarrison)
- {
- newWanderingHeroes += allHeroes[j];
- allHeroes -= allHeroes[j];
- }
- //all the rest of new heroes go the end of the list
- wanderingHeroes.clear();
- wanderingHeroes = newWanderingHeroes;
- newWanderingHeroes.clear();*/
- for (int i = 0; i < allHeroes.size(); i++)
- if (!allHeroes[i]->inTownGarrison)
- wanderingHeroes += allHeroes[i];
- std::vector<const CGTownInstance*> allTowns = cb->getTownsInfo();
- /*
- std::vector<const CGTownInstance*> newTowns;
- for (int i = 0; i < towns.size(); i++)
- if ((j = vstd::find_pos(allTowns, towns[i])) >= 0)
- {
- newTowns += allTowns[j];
- allTowns -= allTowns[j];
- }
- towns.clear();
- towns = newTowns;
- newTowns.clear();*/
- for(int i = 0; i < allTowns.size(); i++)
- towns.push_back(allTowns[i]);
- if (adventureInt)
- adventureInt->updateNextHero(NULL);
- }
- void CPlayerInterface::showRecruitmentDialog(const CGDwelling *dwelling, const CArmedInstance *dst, int level)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- waitWhileDialog();
- CRecruitmentWindow *cr = new CRecruitmentWindow(dwelling, level, dst, boost::bind(&CCallback::recruitCreatures, cb, dwelling, _1, _2, -1));
- GH.pushInt(cr);
- }
- void CPlayerInterface::waitWhileDialog(bool unlockPim /*= true*/)
- {
- if(GH.amIGuiThread())
- {
- tlog3 << "Cannot wait for dialogs in gui thread (deadlock risk)!\n";
- return;
- }
- auto unlock = vstd::makeUnlockGuardIf(*pim, unlockPim);
- boost::unique_lock<boost::mutex> un(showingDialog->mx);
- while(showingDialog->data)
- showingDialog->cond.wait(un);
- }
- void CPlayerInterface::showShipyardDialog(const IShipyard *obj)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- auto state = obj->state();
- std::vector<si32> cost;
- obj->getBoatCost(cost);
- CShipyardWindow *csw = new CShipyardWindow(cost, state, obj->getBoatType(), boost::bind(&CCallback::buildBoat, cb, obj));
- GH.pushInt(csw);
- }
- void CPlayerInterface::newObject( const CGObjectInstance * obj )
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- //we might have built a boat in shipyard in opened town screen
- if(obj->ID == Obj::BOAT
- && LOCPLINT->castleInt
- && obj->pos-obj->getVisitableOffset() == LOCPLINT->castleInt->town->bestLocation())
- {
- CCS->soundh->playSound(soundBase::newBuilding);
- LOCPLINT->castleInt->addBuilding(BuildingID::SHIP);
- }
- }
- void CPlayerInterface::centerView (int3 pos, int focusTime)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- waitWhileDialog();
- adventureInt->centerOn (pos);
- if(focusTime)
- {
- bool activeAdv = (GH.topInt() == adventureInt && adventureInt->isActive());
- if(activeAdv)
- adventureInt->deactivate();
- SDL_Delay(focusTime);
- if(activeAdv)
- adventureInt->activate();
- }
- }
- void CPlayerInterface::objectRemoved( const CGObjectInstance *obj )
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if(obj->ID == Obj::HERO && obj->tempOwner == playerID)
- {
- const CGHeroInstance *h = static_cast<const CGHeroInstance*>(obj);
- heroKilled(h);
- }
- }
- bool CPlayerInterface::ctrlPressed() const
- {
- return SDL_GetKeyState(NULL)[SDLK_LCTRL] || SDL_GetKeyState(NULL)[SDLK_RCTRL];
- }
- void CPlayerInterface::update()
- {
- while(!terminate_cond.get() && !pim->try_lock()) //try acquiring long until it succeeds or we are told to terminate
- boost::this_thread::sleep(boost::posix_time::milliseconds(15));
- boost::unique_lock<boost::recursive_mutex> un(*pim, boost::adopt_lock); //create lock from already owned mutex
- if(terminate_cond.get())
- return;
- //make sure that gamestate won't change when GUI objects may obtain its parts on event processing or drawing request
- boost::shared_lock<boost::shared_mutex> gsLock(cb->getGsMutex());
- //if there are any waiting dialogs, show them
- if((howManyPeople <= 1 || makingTurn) && dialogs.size() && !showingDialog->get())
- {
- showingDialog->set(true);
- GH.pushInt(dialogs.front());
- dialogs.pop_front();
- }
- //in some conditions we may receive calls before selection is initialized - we must ignore them
- if(adventureInt && !adventureInt->selection && GH.topInt() == adventureInt)
- {
- return;
- }
- // Handles mouse and key input
- GH.updateTime();
- GH.handleEvents();
- if(adventureInt && !adventureInt->isActive() && adventureInt->scrollingDir) //player forces map scrolling though interface is disabled
- GH.totalRedraw();
- else
- GH.simpleRedraw();
- if (settings["general"]["showfps"].Bool())
- GH.drawFPSCounter();
- // draw the mouse cursor and update the screen
- CCS->curh->drawWithScreenRestore();
- CSDL_Ext::update(screen);
- CCS->curh->drawRestored();
- }
- int CPlayerInterface::getLastIndex( std::string namePrefix)
- {
- using namespace boost::filesystem;
- using namespace boost::algorithm;
- path gamesDir = VCMIDirs::get().localPath() + "/Games";
- std::map<std::time_t, int> dates; //save number => datestamp
- directory_iterator enddir;
- if(!exists(gamesDir))
- create_directory(gamesDir);
- for (directory_iterator dir(gamesDir); dir!=enddir; dir++)
- {
- if(is_regular(dir->status()))
- {
- std::string name = dir->path().leaf().string();
- if(starts_with(name, namePrefix) && ends_with(name, ".vlgm1"))
- {
- char nr = name[namePrefix.size()];
- if(std::isdigit(nr))
- {
- dates[last_write_time(dir->path())] = boost::lexical_cast<int>(nr);
- }
- }
- }
- }
- if(dates.size())
- return (--dates.end())->second; //return latest file number
- return 0;
- }
- void CPlayerInterface::initMovement( const TryMoveHero &details, const CGHeroInstance * ho, const int3 &hp )
- {
- if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
- {
- //ho->moveDir = 1;
- ho->isStanding = false;
- CGI->mh->ttiles[hp.x-3][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -31)));
- CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, -31)));
- CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, -31)));
- CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, -31)));
- CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 1)));
- subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 1), ho->id);
- subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 1), ho->id);
- subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 1), ho->id);
- CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 33)));
- subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 33), ho->id);
- subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 33), ho->id);
- subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 33), ho->id);
- std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.end(), ocmptwo_cgin);
- }
- else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
- {
- //ho->moveDir = 2;
- ho->isStanding = false;
- CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, -31)));
- CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, -31)));
- CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, -31)));
- subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1), ho->id);
- subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1), ho->id);
- subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1), ho->id);
- subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33), ho->id);
- subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33), ho->id);
- subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33), ho->id);
- std::stable_sort(CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
- }
- else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
- {
- //ho->moveDir = 3;
- ho->isStanding = false;
- CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, -31)));
- CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, -31)));
- CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, -31)));
- CGI->mh->ttiles[hp.x+1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -31)));
- subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 1), ho->id);
- subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 1), ho->id);
- subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 1), ho->id);
- CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 1)));
- subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 33), ho->id);
- subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 33), ho->id);
- subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 33), ho->id);
- CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 33)));
- std::stable_sort(CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.end(), ocmptwo_cgin);
- }
- else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
- {
- //ho->moveDir = 4;
- ho->isStanding = false;
- subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 0), ho->id);
- subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 0), ho->id);
- subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 0), ho->id);
- CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 0)));
- subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 32), ho->id);
- subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 32), ho->id);
- subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 32), ho->id);
- CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 32)));
- std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.end(), ocmptwo_cgin);
- }
- else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
- {
- //ho->moveDir = 5;
- ho->isStanding = false;
- subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, -1), ho->id);
- subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, -1), ho->id);
- subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, -1), ho->id);
- CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -1)));
- subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 31), ho->id);
- subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 31), ho->id);
- subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 31), ho->id);
- CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 31)));
- CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, 63)));
- CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, 63)));
- CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, 63)));
- CGI->mh->ttiles[hp.x+1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 63)));
- std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
- }
- else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
- {
- //ho->moveDir = 6;
- ho->isStanding = false;
- subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1), ho->id);
- subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1), ho->id);
- subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1), ho->id);
- subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31), ho->id);
- subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31), ho->id);
- subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31), ho->id);
- CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, 63)));
- CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, 63)));
- CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, 63)));
- std::stable_sort(CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
- }
- else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
- {
- //ho->moveDir = 7;
- ho->isStanding = false;
- CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -1)));
- subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, -1), ho->id);
- subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, -1), ho->id);
- subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, -1), ho->id);
- CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 31)));
- subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 31), ho->id);
- subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 31), ho->id);
- subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 31), ho->id);
- CGI->mh->ttiles[hp.x-3][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 63)));
- CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, 63)));
- CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, 63)));
- CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, 63)));
- std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
- }
- else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
- {
- //ho->moveDir = 8;
- ho->isStanding = false;
- CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 0)));
- subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 0), ho->id);
- subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 0), ho->id);
- subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 0), ho->id);
- CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 32)));
- subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 32), ho->id);
- subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 32), ho->id);
- subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 32), ho->id);
- std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.end(), ocmptwo_cgin);
- }
- }
- void CPlayerInterface::movementPxStep( const TryMoveHero &details, int i, const int3 &hp, const CGHeroInstance * ho )
- {
- if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
- {
- //setting advmap shift
- adventureInt->terrain.moveX = i-32;
- adventureInt->terrain.moveY = i-32;
- subRect(hp.x-3, hp.y-2, hp.z, genRect(32, 32, -31+i, -31+i), ho->id);
- subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 1+i, -31+i), ho->id);
- subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 33+i, -31+i), ho->id);
- subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 65+i, -31+i), ho->id);
- subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 1+i), ho->id);
- subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 1+i), ho->id);
- subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 1+i), ho->id);
- subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 1+i), ho->id);
- subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 33+i), ho->id);
- subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 33+i), ho->id);
- subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 33+i), ho->id);
- subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 33+i), ho->id);
- }
- else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
- {
- //setting advmap shift
- adventureInt->terrain.moveY = i-32;
- subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 0, -31+i), ho->id);
- subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 32, -31+i), ho->id);
- subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 64, -31+i), ho->id);
- subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1+i), ho->id);
- subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1+i), ho->id);
- subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1+i), ho->id);
- subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33+i), ho->id);
- subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33+i), ho->id);
- subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33+i), ho->id);
- }
- else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
- {
- //setting advmap shift
- adventureInt->terrain.moveX = -i+32;
- adventureInt->terrain.moveY = i-32;
- subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, -1-i, -31+i), ho->id);
- subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 31-i, -31+i), ho->id);
- subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 63-i, -31+i), ho->id);
- subRect(hp.x+1, hp.y-2, hp.z, genRect(32, 32, 95-i, -31+i), ho->id);
- subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 1+i), ho->id);
- subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 1+i), ho->id);
- subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 1+i), ho->id);
- subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 1+i), ho->id);
- subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 33+i), ho->id);
- subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 33+i), ho->id);
- subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 33+i), ho->id);
- subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 33+i), ho->id);
- }
- else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
- {
- //setting advmap shift
- adventureInt->terrain.moveX = -i+32;
- subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 0), ho->id);
- subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 0), ho->id);
- subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 0), ho->id);
- subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 0), ho->id);
- subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 32), ho->id);
- subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 32), ho->id);
- subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 32), ho->id);
- subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 32), ho->id);
- }
- else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
- {
- //setting advmap shift
- adventureInt->terrain.moveX = -i+32;
- adventureInt->terrain.moveY = -i+32;
- subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, -1-i), ho->id);
- subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, -1-i), ho->id);
- subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, -1-i), ho->id);
- subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, -1-i), ho->id);
- subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 31-i), ho->id);
- subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 31-i), ho->id);
- subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 31-i), ho->id);
- subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 31-i), ho->id);
- subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, -1-i, 63-i), ho->id);
- subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 31-i, 63-i), ho->id);
- subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 63-i, 63-i), ho->id);
- subRect(hp.x+1, hp.y+1, hp.z, genRect(32, 32, 95-i, 63-i), ho->id);
- }
- else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
- {
- //setting advmap shift
- adventureInt->terrain.moveY = -i+32;
- subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1-i), ho->id);
- subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1-i), ho->id);
- subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1-i), ho->id);
- subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31-i), ho->id);
- subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31-i), ho->id);
- subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31-i), ho->id);
- subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 0, 63-i), ho->id);
- subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 32, 63-i), ho->id);
- subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 64, 63-i), ho->id);
- }
- else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
- {
- //setting advmap shift
- adventureInt->terrain.moveX = i-32;
- adventureInt->terrain.moveY = -i+32;
- subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, -1-i), ho->id);
- subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, -1-i), ho->id);
- subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, -1-i), ho->id);
- subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, -1-i), ho->id);
- subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 31-i), ho->id);
- subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 31-i), ho->id);
- subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 31-i), ho->id);
- subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 31-i), ho->id);
- subRect(hp.x-3, hp.y+1, hp.z, genRect(32, 32, -31+i, 63-i), ho->id);
- subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 1+i, 63-i), ho->id);
- subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 33+i, 63-i), ho->id);
- subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 65+i, 63-i), ho->id);
- }
- else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
- {
- //setting advmap shift
- adventureInt->terrain.moveX = i-32;
- subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 0), ho->id);
- subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 0), ho->id);
- subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 0), ho->id);
- subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 0), ho->id);
- subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 32), ho->id);
- subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 32), ho->id);
- subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 32), ho->id);
- subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 32), ho->id);
- }
- }
- void CPlayerInterface::finishMovement( const TryMoveHero &details, const int3 &hp, const CGHeroInstance * ho )
- {
- adventureInt->terrain.moveX = adventureInt->terrain.moveY = 0;
- if(details.end.x+1 == details.start.x && details.end.y+1 == details.start.y) //tl
- {
- delObjRect(hp.x, hp.y-2, hp.z, ho->id);
- delObjRect(hp.x, hp.y-1, hp.z, ho->id);
- delObjRect(hp.x, hp.y, hp.z, ho->id);
- delObjRect(hp.x-1, hp.y, hp.z, ho->id);
- delObjRect(hp.x-2, hp.y, hp.z, ho->id);
- delObjRect(hp.x-3, hp.y, hp.z, ho->id);
- }
- else if(details.end.x == details.start.x && details.end.y+1 == details.start.y) //t
- {
- delObjRect(hp.x, hp.y, hp.z, ho->id);
- delObjRect(hp.x-1, hp.y, hp.z, ho->id);
- delObjRect(hp.x-2, hp.y, hp.z, ho->id);
- }
- else if(details.end.x-1 == details.start.x && details.end.y+1 == details.start.y) //tr
- {
- delObjRect(hp.x-2, hp.y-2, hp.z, ho->id);
- delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
- delObjRect(hp.x+1, hp.y, hp.z, ho->id);
- delObjRect(hp.x, hp.y, hp.z, ho->id);
- delObjRect(hp.x-1, hp.y, hp.z, ho->id);
- delObjRect(hp.x-2, hp.y, hp.z, ho->id);
- }
- else if(details.end.x-1 == details.start.x && details.end.y == details.start.y) //r
- {
- delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
- delObjRect(hp.x-2, hp.y, hp.z, ho->id);
- }
- else if(details.end.x-1 == details.start.x && details.end.y-1 == details.start.y) //br
- {
- delObjRect(hp.x-2, hp.y+1, hp.z, ho->id);
- delObjRect(hp.x-2, hp.y, hp.z, ho->id);
- delObjRect(hp.x+1, hp.y-1, hp.z, ho->id);
- delObjRect(hp.x, hp.y-1, hp.z, ho->id);
- delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
- delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
- }
- else if(details.end.x == details.start.x && details.end.y-1 == details.start.y) //b
- {
- delObjRect(hp.x, hp.y-1, hp.z, ho->id);
- delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
- delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
- }
- else if(details.end.x+1 == details.start.x && details.end.y-1 == details.start.y) //bl
- {
- delObjRect(hp.x, hp.y-1, hp.z, ho->id);
- delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
- delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
- delObjRect(hp.x-3, hp.y-1, hp.z, ho->id);
- delObjRect(hp.x, hp.y, hp.z, ho->id);
- delObjRect(hp.x, hp.y+1, hp.z, ho->id);
- }
- else if(details.end.x+1 == details.start.x && details.end.y == details.start.y) //l
- {
- delObjRect(hp.x, hp.y-1, hp.z, ho->id);
- delObjRect(hp.x, hp.y, hp.z, ho->id);
- }
- //restoring good rects
- subRect(details.end.x-2, details.end.y-1, details.end.z, genRect(32, 32, 0, 0), ho->id);
- subRect(details.end.x-1, details.end.y-1, details.end.z, genRect(32, 32, 32, 0), ho->id);
- subRect(details.end.x, details.end.y-1, details.end.z, genRect(32, 32, 64, 0), ho->id);
- subRect(details.end.x-2, details.end.y, details.end.z, genRect(32, 32, 0, 32), ho->id);
- subRect(details.end.x-1, details.end.y, details.end.z, genRect(32, 32, 32, 32), ho->id);
- subRect(details.end.x, details.end.y, details.end.z, genRect(32, 32, 64, 32), ho->id);
- //restoring good order of objects
- std::stable_sort(CGI->mh->ttiles[details.end.x-2][details.end.y-1][details.end.z].objects.begin(), CGI->mh->ttiles[details.end.x-2][details.end.y-1][details.end.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[details.end.x-1][details.end.y-1][details.end.z].objects.begin(), CGI->mh->ttiles[details.end.x-1][details.end.y-1][details.end.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[details.end.x][details.end.y-1][details.end.z].objects.begin(), CGI->mh->ttiles[details.end.x][details.end.y-1][details.end.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[details.end.x-2][details.end.y][details.end.z].objects.begin(), CGI->mh->ttiles[details.end.x-2][details.end.y][details.end.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[details.end.x-1][details.end.y][details.end.z].objects.begin(), CGI->mh->ttiles[details.end.x-1][details.end.y][details.end.z].objects.end(), ocmptwo_cgin);
- std::stable_sort(CGI->mh->ttiles[details.end.x][details.end.y][details.end.z].objects.begin(), CGI->mh->ttiles[details.end.x][details.end.y][details.end.z].objects.end(), ocmptwo_cgin);
- }
- void CPlayerInterface::gameOver(ui8 player, bool victory )
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if(LOCPLINT != this)
- return;
- if(player == playerID)
- {
- if(!victory)
- showInfoDialog(CGI->generaltexth->allTexts[95]);
- // else
- // showInfoDialog("Placeholder message: you won!");
- makingTurn = true;
- waitForAllDialogs(); //wait till all dialogs are displayed and closed
- makingTurn = false;
- howManyPeople--;
- if(!howManyPeople //all human players eliminated
- && cb->getStartInfo()->mode != StartInfo::CAMPAIGN) //campaigns are handled in proposeNextMission
- {
- requestReturningToMainMenu();
- }
- cb->unregisterMyInterface(); //we already won/lost, nothing else matters
- }
- else
- {
- if(!victory && cb->getPlayerStatus(playerID) == EPlayerStatus::INGAME) //enemy has lost
- {
- std::string txt = CGI->generaltexth->allTexts[5]; //%s has been vanquished!
- boost::algorithm::replace_first(txt, "%s", CGI->generaltexth->capColors[player]);
- showInfoDialog(txt,std::vector<CComponent*>(1, new CComponent(CComponent::flag, player, 0)));
- }
- }
- }
- void CPlayerInterface::playerBonusChanged( const Bonus &bonus, bool gain )
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- }
- void CPlayerInterface::showPuzzleMap()
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- waitWhileDialog();
- //TODO: interface should not know the real position of Grail...
- double ratio = 0;
- int3 grailPos = cb->getGrailPos(ratio);
- GH.pushInt(new CPuzzleWindow(grailPos, ratio));
- }
- void CPlayerInterface::advmapSpellCast(const CGHeroInstance * caster, int spellID)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if (spellID == SpellID::FLY || spellID == SpellID::WATER_WALK)
- {
- eraseCurrentPathOf(caster, false);
- }
- const CSpell * spell = CGI->spellh->spells[spellID];
- if (vstd::contains(CCS->soundh->spellSounds, spell))
- CCS->soundh->playSound(CCS->soundh->spellSounds[spell]);
- }
- void CPlayerInterface::eraseCurrentPathOf( const CGHeroInstance * ho, bool checkForExistanceOfPath /*= true */ )
- {
- if(checkForExistanceOfPath)
- {
- assert(vstd::contains(paths, ho));
- }
- else if (!vstd::contains(paths, ho))
- {
- return;
- }
- assert(ho == adventureInt->selection);
- paths.erase(ho);
- adventureInt->terrain.currentPath = NULL;
- adventureInt->updateMoveHero(ho, false);
- }
- void CPlayerInterface::removeLastNodeFromPath(const CGHeroInstance *ho)
- {
- adventureInt->terrain.currentPath->nodes.erase(adventureInt->terrain.currentPath->nodes.end()-1);
- if(adventureInt->terrain.currentPath->nodes.size() < 2) //if it was the last one, remove entire path and path with only one tile is not a real path
- eraseCurrentPathOf(ho);
- }
- CGPath * CPlayerInterface::getAndVerifyPath(const CGHeroInstance * h)
- {
- if(vstd::contains(paths,h)) //hero has assigned path
- {
- CGPath &path = paths[h];
- if(!path.nodes.size())
- {
- tlog3 << "Warning: empty path found...\n";
- paths.erase(h);
- }
- else
- {
- assert(h->getPosition(false) == path.startPos());
- //update the hero path in case of something has changed on map
- if(LOCPLINT->cb->getPath2(path.endPos(), path))
- return &path;
- else
- paths.erase(h);
- }
- }
- return NULL;
- }
- void CPlayerInterface::acceptTurn()
- {
- if(settings["session"]["autoSkip"].Bool())
- {
- while(CInfoWindow *iw = dynamic_cast<CInfoWindow *>(GH.topInt()))
- iw->close();
- }
- waitWhileDialog();
- if(howManyPeople > 1)
- adventureInt->startTurn();
- //select first hero if available.
- //TODO: check if hero is slept
- adventureInt->heroList.update();
- adventureInt->townList.update();
- if(wanderingHeroes.size())
- adventureInt->select(wanderingHeroes.front());
- else
- adventureInt->select(towns.front());
- //show new day animation and sound on infobar
- adventureInt->infoBar.showDate();
- adventureInt->updateNextHero(NULL);
- adventureInt->showAll(screen);
- if(settings["session"]["autoSkip"].Bool() && !LOCPLINT->shiftPressed())
- {
- if(CInfoWindow *iw = dynamic_cast<CInfoWindow *>(GH.topInt()))
- iw->close();
- adventureInt->endTurn.callback();
- }
- }
- void CPlayerInterface::tryDiggging(const CGHeroInstance *h)
- {
- std::string hlp;
- CGI->mh->getTerrainDescr(h->getPosition(false), hlp, false);
- int msgToShow = -1;
- CGHeroInstance::ECanDig isDiggingPossible = h->diggingStatus();
- if(hlp.length())
- isDiggingPossible = CGHeroInstance::TILE_OCCUPIED; //TODO integrate with canDig
- switch(isDiggingPossible)
- {
- case CGHeroInstance::CAN_DIG:
- break;
- case CGHeroInstance::LACK_OF_MOVEMENT:
- msgToShow = 56; //"Digging for artifacts requires a whole day, try again tomorrow."
- break;
- case CGHeroInstance::TILE_OCCUPIED:
- msgToShow = 97; //Try searching on clear ground.
- break;
- case CGHeroInstance::WRONG_TERRAIN:
- msgToShow = 60; ////Try looking on land!
- break;
- default:
- assert(0);
- }
- if(msgToShow < 0)
- cb->dig(h);
- else
- showInfoDialog(CGI->generaltexth->allTexts[msgToShow]);
- }
- void CPlayerInterface::updateInfo(const CGObjectInstance * specific)
- {
- adventureInt->infoBar.showSelection();
- }
- void CPlayerInterface::battleNewRoundFirst( int round )
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if(LOCPLINT != this)
- { //another local interface should do this
- return;
- }
- battleInt->newRoundFirst(round);
- }
- void CPlayerInterface::stopMovement()
- {
- if(stillMoveHero.get() == DURING_MOVE)//if we are in the middle of hero movement
- stillMoveHero.setn(STOP_MOVE); //after showing dialog movement will be stopped
- }
- void CPlayerInterface::showMarketWindow(const IMarket *market, const CGHeroInstance *visitor)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if(market->o->ID == Obj::ALTAR_OF_SACRIFICE)
- {
- //EEMarketMode mode = market->availableModes().front();
- if(market->allowsTrade(EMarketMode::ARTIFACT_EXP) && visitor->getAlignment() != EAlignment::EVIL)
- GH.pushInt(new CAltarWindow(market, visitor, EMarketMode::ARTIFACT_EXP));
- else if(market->allowsTrade(EMarketMode::CREATURE_EXP) && visitor->getAlignment() != EAlignment::GOOD)
- GH.pushInt(new CAltarWindow(market, visitor, EMarketMode::CREATURE_EXP));
- }
- else
- GH.pushInt(new CMarketplaceWindow(market, visitor, market->availableModes().front()));
- }
- void CPlayerInterface::showUniversityWindow(const IMarket *market, const CGHeroInstance *visitor)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- CUniversityWindow *cuw = new CUniversityWindow(visitor, market);
- GH.pushInt(cuw);
- }
- void CPlayerInterface::showHillFortWindow(const CGObjectInstance *object, const CGHeroInstance *visitor)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- CHillFortWindow *chfw = new CHillFortWindow(visitor, object);
- GH.pushInt(chfw);
- }
- void CPlayerInterface::availableArtifactsChanged(const CGBlackMarket *bm /*= NULL*/)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if(CMarketplaceWindow *cmw = dynamic_cast<CMarketplaceWindow*>(GH.topInt()))
- cmw->artifactsChanged(false);
- }
- void CPlayerInterface::showTavernWindow(const CGObjectInstance *townOrTavern)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- CTavernWindow *tv = new CTavernWindow(townOrTavern);
- GH.pushInt(tv);
- }
- void CPlayerInterface::showThievesGuildWindow (const CGObjectInstance * obj)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- CThievesGuildWindow *tgw = new CThievesGuildWindow(obj);
- GH.pushInt(tgw);
- }
- void CPlayerInterface::showQuestLog()
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- CQuestLog * ql = new CQuestLog (LOCPLINT->cb->getMyQuests());
- GH.pushInt (ql);
- }
- void CPlayerInterface::showShipyardDialogOrProblemPopup(const IShipyard *obj)
- {
- if(obj->state() != IBoatGenerator::GOOD)
- {
- MetaString txt;
- obj->getProblemText(txt);
- showInfoDialog(txt.toString());
- }
- else
- showShipyardDialog(obj);
- }
- void CPlayerInterface::requestReturningToMainMenu()
- {
- sendCustomEvent(RETURN_TO_MAIN_MENU);
- }
- void CPlayerInterface::requestStoppingClient()
- {
- sendCustomEvent(STOP_CLIENT);
- }
- void CPlayerInterface::sendCustomEvent( int code )
- {
- CGuiHandler::pushSDLEvent(SDL_USEREVENT, code);
- }
- void CPlayerInterface::stackChagedCount(const StackLocation &location, const TQuantity &change, bool isAbsolute)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- garrisonChanged(location.army);
- }
- void CPlayerInterface::stackChangedType(const StackLocation &location, const CCreature &newType)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- garrisonChanged(location.army);
- }
- void CPlayerInterface::stacksErased(const StackLocation &location)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- garrisonChanged(location.army);
- }
- void CPlayerInterface::stacksSwapped(const StackLocation &loc1, const StackLocation &loc2)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- std::vector<const CGObjectInstance *> objects;
- objects.push_back(loc1.army);
- if(loc2.army != loc1.army)
- objects.push_back(loc2.army);
- garrisonsChanged(objects);
- }
- void CPlayerInterface::newStackInserted(const StackLocation &location, const CStackInstance &stack)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- garrisonChanged(location.army);
- }
- void CPlayerInterface::stacksRebalanced(const StackLocation &src, const StackLocation &dst, TQuantity count)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- std::vector<const CGObjectInstance *> objects;
- objects.push_back(src.army);
- if(src.army != dst.army)
- objects.push_back(dst.army);
- garrisonsChanged(objects);
- }
- void CPlayerInterface::artifactPut(const ArtifactLocation &al)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- }
- void CPlayerInterface::artifactRemoved(const ArtifactLocation &al)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- BOOST_FOREACH(IShowActivatable *isa, GH.listInt)
- {
- auto artWin = dynamic_cast<CArtifactHolder*>(isa);
- if(artWin)
- artWin->artifactRemoved(al);
- }
- }
- void CPlayerInterface::artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- BOOST_FOREACH(IShowActivatable *isa, GH.listInt)
- {
- auto artWin = dynamic_cast<CArtifactHolder*>(isa);
- if(artWin)
- artWin->artifactMoved(src, dst);
- }
- }
- void CPlayerInterface::artifactAssembled(const ArtifactLocation &al)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- BOOST_FOREACH(IShowActivatable *isa, GH.listInt)
- {
- auto artWin = dynamic_cast<CArtifactHolder*>(isa);
- if(artWin)
- artWin->artifactAssembled(al);
- }
- }
- void CPlayerInterface::artifactDisassembled(const ArtifactLocation &al)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- BOOST_FOREACH(IShowActivatable *isa, GH.listInt)
- {
- auto artWin = dynamic_cast<CArtifactHolder*>(isa);
- if(artWin)
- artWin->artifactDisassembled(al);
- }
- }
- void CPlayerInterface::playerStartsTurn(ui8 player)
- {
- EVENT_HANDLER_CALLED_BY_CLIENT;
- if (!vstd::contains (GH.listInt, adventureInt))
- {
- GH.popInts (GH.listInt.size()); //after map load - remove everything else
- GH.pushInt (adventureInt);
- }
- else
- {
- while (GH.listInt.front() != adventureInt && !dynamic_cast<CInfoWindow*>(GH.listInt.front())) //don't remove dialogs that expect query answer
- GH.popInts(1);
- }
- if(howManyPeople == 1)
- {
- GH.curInt = this;
- adventureInt->startTurn();
- }
- if(player != playerID && this == LOCPLINT)
- {
- waitWhileDialog();
- adventureInt->aiTurnStarted();
- }
- }
- void CPlayerInterface::waitForAllDialogs(bool unlockPim /*= true*/)
- {
- while(dialogs.size())
- {
- auto unlock = vstd::makeUnlockGuardIf(*pim, unlockPim);
- SDL_Delay(5);
- }
- waitWhileDialog(unlockPim);
- }
- void CPlayerInterface::proposeLoadingGame()
- {
- showYesNoDialog(CGI->generaltexth->allTexts[68], [this] { sendCustomEvent(RETURN_TO_MENU_LOAD); }, 0, false);
- }
- CPlayerInterface::SpellbookLastSetting::SpellbookLastSetting()
- {
- spellbookLastPageBattle = spellbokLastPageAdvmap = 0;
- spellbookLastTabBattle = spellbookLastTabAdvmap = 4;
- }
|