| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573 |
- #include "stdafx.h"
- #include "CCallback.h"
- #include "CPathfinder.h"
- #include "hch/CHeroHandler.h"
- #include "hch/CTownHandler.h"
- #include "CGameInfo.h"
- #include "hch/CAmbarCendamo.h"
- #include "mapHandler.h"
- #include "CGameState.h"
- #include "CPlayerInterface.h"
- #include "hch/CGeneralTextHandler.h"
- #include "CAdvmapInterface.h"
- #include "CPlayerInterface.h"
- #include "hch/CBuildingHandler.h"
- #include "hch/CObjectHandler.h"
- #include "lib/Connection.h"
- #include "client/Client.h"
- #include <boost/thread.hpp>
- #include <boost/foreach.hpp>
- #include "lib/NetPacks.h"
- //LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...);
- extern CSharedCond<std::set<IPack*> > mess;
- HeroMoveDetails::HeroMoveDetails(int3 Src, int3 Dst, CGHeroInstance*Ho)
- :src(Src),dst(Dst),ho(Ho)
- {
- owner = ho->getOwner();
- };
- bool CCallback::moveHero(int ID, CPath * path, int idtype, int pathType)
- {
- CGHeroInstance * hero = NULL;
- if (idtype==0)
- {
- if (player==-1)
- hero=gs->players[player+1].heroes[ID];
- else
- hero=gs->players[player].heroes[ID];
- }
- else if (idtype==1 && player>=0) //looking for it in local area
- {
- for (int i=0; i<gs->players[player].heroes.size();i++)
- {
- if (gs->players[player].heroes[i]->type->ID == ID)
- hero = gs->players[player].heroes[i];
- }
- }
- else //idtype==1; player<0
- {
- for(std::map<ui8, PlayerState>::iterator j=CGI->state->players.begin(); j!=CGI->state->players.end(); ++j)
- {
- for (int i=0; i<(*j).second.heroes.size();i++)
- {
- if ((*j).second.heroes[i]->type->ID == ID)
- {
- hero = (*j).second.heroes[i];
- }
- }
- }
- }
- if (!hero)
- return false; //can't find hero
- if(!verifyPath(path,!hero->canWalkOnSea()))//TODO: not check sea, if hero has flying or walking on water
- return false; //invalid path
- //check path format
- if (pathType==0)
- CPathfinder::convertPath(path,pathType);
- if (pathType>1)
- throw std::exception("Unknown path format");
- CPath * ourPath = path;
- if(!ourPath)
- return false;
- for(int i=ourPath->nodes.size()-1; i>0; i--)
- {
- int3 stpos(ourPath->nodes[i].coord.x, ourPath->nodes[i].coord.y, hero->pos.z),
- endpos(ourPath->nodes[i-1].coord.x, ourPath->nodes[i-1].coord.y, hero->pos.z);
- HeroMoveDetails curd(stpos,endpos,hero);
- *cl->serv << ui16(501) << hero->id << stpos << endpos;
- {//wait till there is server answer
- boost::unique_lock<boost::mutex> lock(*mess.mx);
- while(std::find_if(mess.res->begin(),mess.res->end(),IPack::isType<501>) == mess.res->end())
- mess.cv->wait(lock);
- std::set<IPack*>::iterator itr = std::find_if(mess.res->begin(),mess.res->end(),IPack::isType<501>);
- TryMoveHero tmh = *static_cast<TryMoveHero*>(*itr);
- mess.res->erase(itr);
- if(!tmh.result)
- return false;
- }
- }
- return true;
- }
- void CCallback::selectionMade(int selection, int asker)
- {
- //todo - jak bedzie multiplayer po sieci, to moze wymagac przerobek zaleznych od obranego modelu
- //IChosen * ask = (IChosen *)asker;
- //ask->chosen(selection);
- }
- void CCallback::recruitCreatures(const CGObjectInstance *obj, int ID, int amount)
- {
- if(amount<=0) return;
- if(obj->ID==98) //recruiting from town
- {
- int ser=-1; //used dwelling level
- CGTownInstance *t = const_cast<CGTownInstance*>(static_cast<const CGTownInstance*>(obj));
- //verify
- bool found = false;
- typedef std::pair<const int,int> Parka;
- for(std::map<si32,ui32>::iterator av=t->strInfo.creatures.begin();av!=t->strInfo.creatures.end();av++)
- {
- if( ( found = (ID == t->town->basicCreatures[av->first]) ) //creature is available among basic cretures
- || (found = (ID == t->town->upgradedCreatures[av->first])) )//creature is available among upgraded cretures
- {
- amount = min(amount,av->second); //reduce recruited amount up to available amount
- ser = av->first;
- break;
- }
- }
- if(!found) //no such creature
- return;
- if(amount > CGI->creh->creatures[ID].maxAmount(gs->players[player].resources))
- return; //not enough resources
- //for(int i=0;i<RESOURCE_QUANTITY;i++)
- // if (gs->players[player].resources[i] < (CGI->creh->creatures[ID].cost[i] * amount))
- // return; //not enough resources
- if(amount<=0) return;
- //recruit
- int slot = -1; //slot ID
- std::pair<si32,std::pair<ui32,si32> > parb;
- for(int i=0;i<7;i++) //TODO: if there is already stack of same creatures it should be used always
- {
- if(((!t->army.slots[i].first) && (!t->army.slots[i].second)) || (t->army.slots[i].first == ID)) //slot is free or there is saem creature
- {
- slot = i;
- break;
- }
- }
- if(slot<0) //no free slot
- return;
- for(int i=0;i<RESOURCE_QUANTITY;i++)
- gs->players[player].resources[i] -= (CGI->creh->creatures[ID].cost[i] * amount);
- t->strInfo.creatures[ser] -= amount;
- if(t->army.slots[slot].first) //add new creatures to the existing stack
- {
- t->army.slots[slot].second += amount;
- }
- else //create new stack in the garrison
- {
- t->army.slots[slot].first = ID;
- t->army.slots[slot].second = amount;
- }
- cl->playerint[player]->garrisonChanged(obj);
- }
- //TODO: recruit from dwellings on the adventure map
- }
- bool CCallback::dismissCreature(const CArmedInstance *obj, int stackPos)
- {
- if(((player>=0) && obj->tempOwner != player) || obj->army.slots.size()<2)
- return false;
- *cl->serv << ui16(503) << obj->id << ui8(stackPos);
- return true;
- }
- bool CCallback::upgradeCreature(const CArmedInstance *obj, int stackPos, int newID)
- {
- //TODO: write
- return false;
- }
- void CCallback::endTurn()
- {
- std::cout << "Player "<<(unsigned)player<<" end his turn."<<std::endl;
- cl->serv->wmx->lock();
- *cl->serv << ui16(100); //report that we ended turn
- cl->serv->wmx->unlock();
- }
- UpgradeInfo CCallback::getUpgradeInfo(const CArmedInstance *obj, int stackPos)
- {
- UpgradeInfo ret;
- CCreature *base = &CGI->creh->creatures[((CArmedInstance *)obj)->army.slots[stackPos].first];
- if((obj->ID == 98) || ((obj->ID == 34) && static_cast<const CGHeroInstance*>(obj)->visitedTown))
- {
- CGTownInstance * t;
- if(obj->ID == 98)
- t = static_cast<CGTownInstance *>(const_cast<CArmedInstance *>(obj));
- else
- t = static_cast<const CGHeroInstance*>(obj)->visitedTown;
- for(std::set<si32>::iterator i=t->builtBuildings.begin(); i!=t->builtBuildings.end(); i++)
- {
- if( (*i) >= 37 && (*i) < 44 ) //upgraded creature dwelling
- {
- int nid = t->town->upgradedCreatures[(*i)-37]; //upgrade offered by that building
- if(base->upgrades.find(nid) != base->upgrades.end()) //possible upgrade
- {
- ret.newID.push_back(nid);
- ret.cost.push_back(std::set<std::pair<int,int> >());
- for(int j=0;j<RESOURCE_QUANTITY;j++)
- {
- int dif = CGI->creh->creatures[nid].cost[j] - base->cost[j];
- if(dif)
- ret.cost[ret.cost.size()-1].insert(std::make_pair(j,dif));
- }
- }
- }
- }//end for
- }
- //TODO: check if hero ability makes some upgrades possible
- if(ret.newID.size())
- ret.oldID = base->idNumber;
- return ret;
- }
- const StartInfo * CCallback::getStartInfo()
- {
- return gs->scenarioOps;
- }
- int CCallback::howManyTowns()
- {
- return gs->players[gs->currentPlayer].towns.size();
- }
- const CGTownInstance * CCallback::getTownInfo(int val, bool mode) //mode = 0 -> val = serial; mode = 1 -> val = ID
- {
- if (!mode)
- return gs->players[gs->currentPlayer].towns[val];
- else
- {
- //TODO: add some smart ID to the CTownInstance
- //for (int i=0; i<gs->players[gs->currentPlayer].towns.size();i++)
- //{
- // if (gs->players[gs->currentPlayer].towns[i]->someID==val)
- // return gs->players[gs->currentPlayer].towns[i];
- //}
- return NULL;
- }
- return NULL;
- }
- int CCallback::howManyHeroes()
- {
- return gs->players[player].heroes.size();
- }
- const CGHeroInstance * CCallback::getHeroInfo(int player, int val, bool mode) //mode = 0 -> val = serial; mode = 1 -> val = ID
- {
- if (gs->currentPlayer!=player) //TODO: checking if we are allowed to give that info
- return NULL;
- if (!mode)
- if(val<gs->players[player].heroes.size())
- return gs->players[player].heroes[val];
- else return NULL;
- else
- {
- for (int i=0; i<gs->players[player].heroes.size();i++)
- {
- if (gs->players[player].heroes[i]->type->ID==val)
- return gs->players[player].heroes[i];
- }
- }
- return NULL;
- }
- int CCallback::getResourceAmount(int type)
- {
- return gs->players[player].resources[type];
- }
- std::vector<si32> CCallback::getResourceAmount()
- {
- return gs->players[player].resources;
- }
- int CCallback::getDate(int mode)
- {
- return gs->getDate(mode);
- }
- std::vector < std::string > CCallback::getObjDescriptions(int3 pos)
- {
- std::vector<std::string> ret;
- BOOST_FOREACH(const CGObjectInstance * obj, gs->map->terrain[pos.x][pos.y][pos.z].blockingObjects)
- ret.push_back(obj->hoverName);
- return ret;
- }
- bool CCallback::verifyPath(CPath * path, bool blockSea)
- {
- for (int i=0;i<path->nodes.size();i++)
- {
- if ( CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tileInfo->blocked
- && (! (CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tileInfo->visitable)))
- return false; //path is wrong - one of the tiles is blocked
- if (blockSea)
- {
- if (i==0)
- continue;
- if (
- ((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tileInfo->tertype==EterrainType::water)
- &&
- (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tileInfo->tertype!=EterrainType::water))
- ||
- ((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tileInfo->tertype!=EterrainType::water)
- &&
- (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tileInfo->tertype==EterrainType::water))
- ||
- (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tileInfo->tertype==EterrainType::rock)
-
- )
- return false;
- }
- }
- return true;
- }
- std::vector< std::vector< std::vector<unsigned char> > > & CCallback::getVisibilityMap()
- {
- return gs->players[player].fogOfWarMap;
- }
- bool CCallback::isVisible(int3 pos, int Player)
- {
- return gs->players[Player].fogOfWarMap[pos.x][pos.y][pos.z];
- }
- std::vector < const CGTownInstance *> CCallback::getTownsInfo(bool onlyOur)
- {
- std::vector < const CGTownInstance *> ret = std::vector < const CGTownInstance *>();
- for ( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
- {
- for (int j=0;j<(*i).second.towns.size();j++)
- {
- if ( ( isVisible((*i).second.towns[j]->pos,player) ) || (*i).first==player)
- {
- ret.push_back((*i).second.towns[j]);
- }
- }
- } // for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
- return ret;
- }
- std::vector < const CGHeroInstance *> CCallback::getHeroesInfo(bool onlyOur)
- {
- std::vector < const CGHeroInstance *> ret = std::vector < const CGHeroInstance *>();
- for ( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
- {
- for (int j=0;j<(*i).second.heroes.size();j++)
- {
- if ( ( isVisible((*i).second.heroes[j]->getPosition(false),player) ) || (*i).first==player)
- {
- ret.push_back((*i).second.heroes[j]);
- }
- }
- } // for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
- return ret;
- }
- bool CCallback::isVisible(int3 pos)
- {
- return isVisible(pos,player);
- }
- int CCallback::getMyColor()
- {
- return player;
- }
- int CCallback::getHeroSerial(const CGHeroInstance * hero)
- {
- for (int i=0; i<gs->players[player].heroes.size();i++)
- {
- if (gs->players[player].heroes[i]==hero)
- return i;
- }
- return -1;
- }
- const CCreatureSet* CCallback::getGarrison(const CGObjectInstance *obj)
- {
- if(!obj)
- return NULL;
- if(obj->ID == 34)
- return &(dynamic_cast<const CGHeroInstance*>(obj))->army;
- else if(obj->ID == 98)
- return &(dynamic_cast<const CGTownInstance*>(obj)->army);
- else return NULL;
- }
- int CCallback::swapCreatures(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2)
- {
- if(s1->tempOwner != player || s2->tempOwner != player)
- return -1;
- *cl->serv << ui16(502) << ui8(1) << s1->id << ui8(p1) << s2->id << ui8(p2);
- return 0;
- }
- int CCallback::mergeStacks(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2)
- {
- if ((s1->tempOwner!= player || s2->tempOwner!=player))
- {
- return -1;
- }
- *cl->serv << ui16(502) << ui8(2) << s1->id << ui8(p1) << s2->id << ui8(p2);
- return 0;
- }
- int CCallback::splitStack(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2, int val)
- {
- if (s1->tempOwner!= player || s2->tempOwner!=player || (!val))
- {
- return -1;
- }
- *cl->serv << ui16(502) << ui8(3) << s1->id << ui8(p1) << s2->id << ui8(p2) << si32(val);
- return 0;
- }
- bool CCallback::dismissHero(const CGHeroInstance *hero)
- {
- CGHeroInstance * Vhero = const_cast<CGHeroInstance *>(hero);
- CGI->mh->removeObject(Vhero);
- std::vector<CGHeroInstance*>::iterator nitr = find(CGI->state->players[player].heroes.begin(), CGI->state->players[player].heroes.end(), Vhero);
- CGI->state->players[player].heroes.erase(nitr);
- LOCPLINT->adventureInt->heroList.updateHList();
- return false;
- }
- int CCallback::getMySerial()
- {
- return gs->players[player].serial;
- }
- bool CCallback::swapArifacts(const CGHeroInstance * hero1, bool worn1, int pos1, const CGHeroInstance * hero2, bool worn2, int pos2)
- {
- if(!hero1 || !hero2) //incorrect data
- return false;
- CGHeroInstance * Uhero1 = const_cast<CGHeroInstance *>(hero1);
- CGHeroInstance * Uhero2 = const_cast<CGHeroInstance *>(hero2);
- if(worn1 && worn2)
- {
- std::swap(Uhero1->artifWorn[pos1], Uhero2->artifWorn[pos2]);
- }
- else if(worn1 && !worn2)
- {
- std::swap(Uhero1->artifWorn[pos1], Uhero2->artifacts[pos2]);
- }
- else if(!worn1 && worn2)
- {
- std::swap(Uhero1->artifacts[pos1], Uhero2->artifWorn[pos2]);
- }
- else
- {
- std::swap(Uhero1->artifacts[pos1], Uhero2->artifacts[pos2]);
- }
-
- return true;
- }
- bool CCallback::buildBuilding(const CGTownInstance *town, si32 buildingID)
- {
- CGTownInstance * t = const_cast<CGTownInstance *>(town);
- if(town->tempOwner!=player)
- return false;
- CBuilding *b = CGI->buildh->buildings[t->subID][buildingID];
- for(int i=0;i<7;i++)
- if(b->resources[i] > gs->players[player].resources[i])
- return false; //lack of resources
- *cl->serv << ui16(504) << town->id << buildingID;
- //TODO: check if we are allowed to build
- return true;
- }
- int CCallback::battleGetBattlefieldType()
- {
- return CGI->mh->ttiles[CGI->state->curB->tile.x][CGI->state->curB->tile.y][CGI->state->curB->tile.z].tileInfo->tertype;
- }
- int CCallback::battleGetObstaclesAtTile(int tile) //returns bitfield
- {
- //TODO - write
- return -1;
- }
- int CCallback::battleGetStack(int pos)
- {
- for(int g=0; g<CGI->state->curB->stacks.size(); ++g)
- {
- if(CGI->state->curB->stacks[g]->position == pos ||
- ( CGI->state->curB->stacks[g]->creature->isDoubleWide() &&
- ( (CGI->state->curB->stacks[g]->attackerOwned && CGI->state->curB->stacks[g]->position-1 == pos) ||
- (!CGI->state->curB->stacks[g]->attackerOwned && CGI->state->curB->stacks[g]->position+1 == pos)
- )
- )
- )
- return CGI->state->curB->stacks[g]->ID;
- }
- return -1;
- }
- CStack* CCallback::battleGetStackByID(int ID)
- {
- for(int g=0; g<CGI->state->curB->stacks.size(); ++g)
- {
- if(CGI->state->curB->stacks[g]->ID == ID)
- return CGI->state->curB->stacks[g];
- }
- return NULL;
- }
- CStack* CCallback::battleGetStackByPos(int pos)
- {
- return battleGetStackByID(battleGetStack(pos));
- }
- int CCallback::battleGetPos(int stack)
- {
- for(int g=0; g<CGI->state->curB->stacks.size(); ++g)
- {
- if(CGI->state->curB->stacks[g]->ID == stack)
- return CGI->state->curB->stacks[g]->position;
- }
- return -1;
- }
- std::map<int, CStack> CCallback::battleGetStacks()
- {
- std::map<int, CStack> ret;
- for(int g=0; g<CGI->state->curB->stacks.size(); ++g)
- {
- ret[CGI->state->curB->stacks[g]->ID] = *(CGI->state->curB->stacks[g]);
- }
- return ret;
- }
- CCreature CCallback::battleGetCreature(int number)
- {
- for(int h=0; h<CGI->state->curB->stacks.size(); ++h)
- {
- if(CGI->state->curB->stacks[h]->ID == number) //creature found
- return *(CGI->state->curB->stacks[h]->creature);
- }
- throw new std::exception("Cannot find the creature");
- }
- std::vector<int> CCallback::battleGetAvailableHexes(int ID)
- {
- return CGI->state->battleGetRange(ID);
- }
- bool CCallback::battleIsStackMine(int ID)
- {
- for(int h=0; h<CGI->state->curB->stacks.size(); ++h)
- {
- if(CGI->state->curB->stacks[h]->ID == ID) //creature found
- return CGI->state->curB->stacks[h]->owner == player;
- }
- return false;
- }
|