diff --git a/AI/EmptyAI/CEmptyAI.h b/AI/EmptyAI/CEmptyAI.h index d5ad45da7..f58656fd0 100644 --- a/AI/EmptyAI/CEmptyAI.h +++ b/AI/EmptyAI/CEmptyAI.h @@ -1,5 +1,5 @@ #include "../../AI_Base.h" - +#include "../../CCallback.h" class CEmptyAI : public CGlobalAI { ICallback * cb; diff --git a/CCallback.cpp b/CCallback.cpp index 12a76aed3..fe4fd591a 100644 --- a/CCallback.cpp +++ b/CCallback.cpp @@ -251,6 +251,7 @@ void CCallback::recruitCreatures(const CGObjectInstance *obj, int ID, int amount if(amount<=0) return; if(obj->ID==98) { + int ser=-1; CGTownInstance *t = const_cast(static_cast(obj)); //verify @@ -262,6 +263,7 @@ void CCallback::recruitCreatures(const CGObjectInstance *obj, int ID, int amount || (found = (ID == t->town->upgradedCreatures[av->first])) ) { amount = std::min(amount,av->second); + ser = av->first; break; } } @@ -292,6 +294,8 @@ void CCallback::recruitCreatures(const CGObjectInstance *obj, int ID, int amount for(int i=0;iplayers[player].resources[i] -= (CGI->creh->creatures[ID].cost[i] * amount); + + t->strInfo.creatures[ser] -= amount; t->army.slots[slot].first = &CGI->creh->creatures[ID]; t->army.slots[slot].second = amount; @@ -496,52 +500,82 @@ int CCallback::swapCreatures(const CGObjectInstance *s1, const CGObjectInstance //TODO: check if we are allowed to swap these creatures return -1; } - //if(S1->slots[p1].first) - { - //if(s2->slots[p2].first) - { - CCreature * pom = S2->slots[p2].first; - S2->slots[p2].first = S1->slots[p1].first; - S1->slots[p1].first = pom; - int pom2 = S2->slots[p2].second; - S2->slots[p2].second = S1->slots[p1].second; - S1->slots[p1].second = pom2; - if(!S1->slots[p1].first) - S1->slots.erase(p1); - if(!S2->slots[p2].first) - S2->slots.erase(p2); - if(s1->tempOwnerslots[p2].first; + S2->slots[p2].first = S1->slots[p1].first; + S1->slots[p1].first = pom; + int pom2 = S2->slots[p2].second; + S2->slots[p2].second = S1->slots[p1].second; + S1->slots[p1].second = pom2; + + if(!S1->slots[p1].first) + S1->slots.erase(p1); + if(!S2->slots[p2].first) + S2->slots.erase(p2); + + if(s1->tempOwnerplayerint.size(); ++b) + { + if(CGI->playerint[b]->playerID == s1->tempOwner) { - for(int b=0; bplayerint.size(); ++b) - { - if(CGI->playerint[b]->playerID == s1->tempOwner) - { - CGI->playerint[b]->garrisonChanged(s1); - break; - } - } + CGI->playerint[b]->garrisonChanged(s1); + break; } - if((s2->tempOwnerplayerint.size(); ++b) - { - if(CGI->playerint[b]->playerID == s2->tempOwner) - { - CGI->playerint[b]->garrisonChanged(s2); - break; - } - } - } - return 0; } } - return -1; + if((s2->tempOwnerplayerint.size(); ++b) + { + if(CGI->playerint[b]->playerID == s2->tempOwner) + { + CGI->playerint[b]->garrisonChanged(s2); + break; + } + } + } + return 0; } int CCallback::mergeStacks(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2) -{ - return -1; +{ + CCreatureSet *S1 = const_cast(getGarrison(s1)), *S2 = const_cast(getGarrison(s2)); + if ((S1->slots[p1].first != S2->slots[p2].first) && (true /*we are allowed to*/)) + { + return -1; + } + + + S2->slots[p2].second += S1->slots[p1].second; + S1->slots[p1].first = NULL; + S1->slots[p1].second = 0; + + S1->slots.erase(p1); + + if(s1->tempOwnerplayerint.size(); ++b) + { + if(CGI->playerint[b]->playerID == s1->tempOwner) + { + CGI->playerint[b]->garrisonChanged(s1); + break; + } + } + } + if((s2->tempOwnerplayerint.size(); ++b) + { + if(CGI->playerint[b]->playerID == s2->tempOwner) + { + CGI->playerint[b]->garrisonChanged(s2); + break; + } + } + } + return 0; } int CCallback::splitStack(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2, int val) { @@ -611,6 +645,11 @@ int CCallback::battleGetBattlefieldType() return CGI->mh->ttiles[CGI->state->curB->tile.x][CGI->state->curB->tile.y][CGI->state->curB->tile.z].terType; } +int CCallback::battleGetObstaclesAtTile(int tile) //returns bitfield +{ + //TODO - write + return -1; +} int CCallback::battleGetStack(int pos) { for(int g=0; gstate->curB->stacks.size(); ++g) diff --git a/CCallback.h b/CCallback.h index ba9d3519a..c004c81c8 100644 --- a/CCallback.h +++ b/CCallback.h @@ -42,6 +42,19 @@ public: virtual int getMySerial()=0; virtual int getHeroSerial(const CGHeroInstance * hero)=0; virtual const CCreatureSet* getGarrison(const CGObjectInstance *obj)=0; + +//battle + virtual int battleGetBattlefieldType()=0; // 1. sand/shore 2. sand/mesas 3. dirt/birches 4. dirt/hills 5. dirt/pines 6. grass/hills 7. grass/pines 8. lava 9. magic plains 10. snow/mountains 11. snow/trees 12. subterranean 13. swamp/trees 14. fiery fields 15. rock lands 16. magic clouds 17. lucid pools 18. holy ground 19. clover field 20. evil fog 21. "favourable winds" text on magic plains background 22. cursed ground 23. rough 24. ship to ship 25. ship + virtual int battleGetObstaclesAtTile(int tile)=0; //returns bitfield + virtual int battleGetStack(int pos)=0; //returns ID of stack on the tile + virtual CStack battleGetStackByID(int ID)=0; //returns stack info by given ID + virtual int battleGetPos(int stack)=0; //returns position (tile ID) of stack + virtual int battleMakeAction(BattleAction* action)=0;//perform action with an active stack (or custom action) + virtual std::map battleGetStacks()=0; //returns stacks on battlefield + virtual CCreature battleGetCreature(int number)=0; //returns type of creature by given number of stack + virtual bool battleMoveCreature(int ID, int dest)=0; //moves creature with id ID to dest if possible + virtual std::vector battleGetAvailableHexes(int ID)=0; //reutrns numbers of hexes reachable by creature with id ID + virtual bool battleIsStackMine(int ID)=0; //returns true if stack with id ID belongs to caller }; struct HeroMoveDetails @@ -90,7 +103,7 @@ public: int getHeroSerial(const CGHeroInstance * hero); int getMySerial(); int swapCreatures(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2); - int mergeStacks(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2); + int mergeStacks(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2); //first goes to the second int splitStack(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2, int val); bool dismissHero(const CGHeroInstance * hero); const CCreatureSet* getGarrison(const CGObjectInstance *obj); diff --git a/CMT.cpp b/CMT.cpp index 2f858922e..199bb3cb6 100644 --- a/CMT.cpp +++ b/CMT.cpp @@ -237,7 +237,22 @@ void initGameState(CGameInfo * cgi) } } } + + //init visiting heroes + for(int l=0; lsecond.heroes.size();l++) + { + for(int m=0; msecond.towns.size();m++) + { + int3 vistile = k->second.towns[m]->pos; vistile.x--; //tile next to the entrance + if(vistile == k->second.heroes[l]->pos) + { + k->second.towns[m]->visitingHero = k->second.heroes[l]; + break; + } + } + } } + /****************************SCRIPTS************************************************/ std::map > * skrypty = &cgi->state->objscr; //alias for easier access /****************************C++ OBJECT SCRIPTS************************************************/ diff --git a/CPlayerInterface.cpp b/CPlayerInterface.cpp index 6839cc2e9..da3e023cc 100644 --- a/CPlayerInterface.cpp +++ b/CPlayerInterface.cpp @@ -56,12 +56,18 @@ void CGarrisonSlot::hover (bool on) temp = CGI->townh->tcommands[2]; boost::algorithm::replace_first(temp,"%s",creature->nameSing); } - else + else if (owner->highlighted->creature) { temp = CGI->townh->tcommands[7]; boost::algorithm::replace_first(temp,"%s",owner->highlighted->creature->nameSing); boost::algorithm::replace_first(temp,"%s",creature->nameSing); } + else + { + std::cout << "Warning - shouldn't be - highlighted void slot "<highlighted<highlighted = NULL; + } } else { @@ -109,10 +115,26 @@ void CGarrisonSlot::clickLeft(tribool down) { if(owner->highlighted) { - LOCPLINT->cb->swapCreatures( - (!upg)?(owner->oup):(owner->odown), - (!owner->highlighted->upg)?(owner->oup):(owner->odown), - ID,owner->highlighted->ID); + if(owner->highlighted == this) + { + //TODO: view creature info + owner->highlighted = NULL; + show(); + } + else if(creature != owner->highlighted->creature) //swap + { + LOCPLINT->cb->swapCreatures( + (!upg)?(owner->oup):(owner->odown), + (!owner->highlighted->upg)?(owner->oup):(owner->odown), + ID,owner->highlighted->ID); + } + else + { + LOCPLINT->cb->mergeStacks( + (!owner->highlighted->upg)?(owner->oup):(owner->odown), + (!upg)?(owner->oup):(owner->odown), + owner->highlighted->ID,ID); + } } else { @@ -1861,6 +1883,11 @@ void CPlayerInterface::garrisonChanged(const CGObjectInstance * obj) SDL_FreeSurface(heroWins[hh->subID]); heroWins[hh->subID] = infoWin(hh); } + if(castleInt == curint) //opened town window - redraw town garrsion slots (change is within hero garr) + { + castleInt->garr->highlighted = NULL; + castleInt->garr->recreateSlots(); + } } else if (obj->ID == 98) //town {