2007-08-27 17:15:03 +03:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "CCallback.h"
|
2007-08-29 13:05:50 +03:00
|
|
|
#include "CPathfinder.h"
|
2008-07-01 11:01:02 +03:00
|
|
|
#include "hch/CHeroHandler.h"
|
|
|
|
#include "hch/CTownHandler.h"
|
2007-08-29 15:18:31 +03:00
|
|
|
#include "CGameInfo.h"
|
2008-07-01 11:01:02 +03:00
|
|
|
#include "hch/CAmbarCendamo.h"
|
2007-08-29 15:18:31 +03:00
|
|
|
#include "mapHandler.h"
|
2007-08-30 13:11:53 +03:00
|
|
|
#include "CGameState.h"
|
2007-11-19 00:58:28 +02:00
|
|
|
#include "CPlayerInterface.h"
|
|
|
|
#include "hch/CGeneralTextHandler.h"
|
|
|
|
#include "CAdvmapInterface.h"
|
|
|
|
#include "CPlayerInterface.h"
|
2008-03-21 02:03:31 +02:00
|
|
|
#include "hch/CBuildingHandler.h"
|
2008-07-02 11:39:56 +03:00
|
|
|
#include "hch/CObjectHandler.h"
|
2008-07-27 20:07:37 +03:00
|
|
|
#include "lib/Connection.h"
|
|
|
|
#include "client/Client.h"
|
|
|
|
#include <boost/thread.hpp>
|
2008-07-28 15:44:08 +03:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include "lib/NetPacks.h"
|
2008-08-05 02:04:15 +03:00
|
|
|
#include <boost/thread/shared_mutex.hpp>
|
2008-08-04 18:56:36 +03:00
|
|
|
#ifdef min
|
|
|
|
#undef min
|
|
|
|
#endif
|
|
|
|
#ifdef max
|
|
|
|
#undef max
|
|
|
|
#endif
|
2008-07-28 15:44:08 +03:00
|
|
|
extern CSharedCond<std::set<IPack*> > mess;
|
2008-03-14 20:24:37 +02:00
|
|
|
|
2008-07-28 15:44:08 +03:00
|
|
|
HeroMoveDetails::HeroMoveDetails(int3 Src, int3 Dst, CGHeroInstance*Ho)
|
|
|
|
:src(Src),dst(Dst),ho(Ho)
|
|
|
|
{
|
|
|
|
owner = ho->getOwner();
|
|
|
|
};
|
2007-10-05 21:16:22 +03:00
|
|
|
bool CCallback::moveHero(int ID, CPath * path, int idtype, int pathType)
|
2007-08-27 17:15:03 +03:00
|
|
|
{
|
2007-10-27 22:38:48 +03:00
|
|
|
CGHeroInstance * hero = NULL;
|
2007-10-05 21:16:22 +03:00
|
|
|
|
|
|
|
if (idtype==0)
|
|
|
|
{
|
|
|
|
if (player==-1)
|
|
|
|
hero=gs->players[player+1].heroes[ID];
|
|
|
|
else
|
|
|
|
hero=gs->players[player].heroes[ID];
|
|
|
|
}
|
2007-10-11 23:13:13 +03:00
|
|
|
else if (idtype==1 && player>=0) //looking for it in local area
|
2007-10-05 21:16:22 +03:00
|
|
|
{
|
|
|
|
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
|
2007-10-04 19:40:40 +03:00
|
|
|
{
|
2007-10-05 21:16:22 +03:00
|
|
|
|
2008-08-05 02:04:15 +03:00
|
|
|
for(std::map<ui8, PlayerState>::iterator j=gs->players.begin(); j!=gs->players.end(); ++j)
|
2007-10-05 21:16:22 +03:00
|
|
|
{
|
|
|
|
for (int i=0; i<(*j).second.heroes.size();i++)
|
|
|
|
{
|
|
|
|
if ((*j).second.heroes[i]->type->ID == ID)
|
|
|
|
{
|
|
|
|
hero = (*j).second.heroes[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-10-04 19:40:40 +03:00
|
|
|
}
|
2007-10-05 21:16:22 +03:00
|
|
|
|
|
|
|
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)
|
2008-08-04 12:05:52 +03:00
|
|
|
#ifndef __GNUC__
|
2007-10-05 21:16:22 +03:00
|
|
|
throw std::exception("Unknown path format");
|
2008-08-04 12:05:52 +03:00
|
|
|
#else
|
|
|
|
throw std::exception();
|
|
|
|
#endif
|
2007-10-05 21:16:22 +03:00
|
|
|
|
|
|
|
CPath * ourPath = path;
|
2007-08-29 15:18:31 +03:00
|
|
|
if(!ourPath)
|
|
|
|
return false;
|
2007-09-13 16:34:59 +03:00
|
|
|
for(int i=ourPath->nodes.size()-1; i>0; i--)
|
2007-08-30 13:11:53 +03:00
|
|
|
{
|
2008-07-28 15:44:08 +03:00
|
|
|
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)
|
2007-11-25 15:16:45 +02:00
|
|
|
return false;
|
2007-08-30 13:11:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2007-08-29 15:18:31 +03:00
|
|
|
}
|
2007-09-14 16:11:10 +03:00
|
|
|
|
2007-12-25 18:25:53 +02:00
|
|
|
void CCallback::selectionMade(int selection, int asker)
|
|
|
|
{
|
|
|
|
//todo - jak bedzie multiplayer po sieci, to moze wymagac przerobek zaleznych od obranego modelu
|
2008-07-30 20:51:19 +03:00
|
|
|
//IChosen * ask = (IChosen *)asker;
|
|
|
|
//ask->chosen(selection);
|
2007-12-25 18:25:53 +02:00
|
|
|
}
|
2008-04-11 20:41:02 +03:00
|
|
|
void CCallback::recruitCreatures(const CGObjectInstance *obj, int ID, int amount)
|
|
|
|
{
|
|
|
|
if(amount<=0) return;
|
2008-06-01 16:42:29 +03:00
|
|
|
if(obj->ID==98) //recruiting from town
|
2008-04-11 20:41:02 +03:00
|
|
|
{
|
2008-06-01 16:42:29 +03:00
|
|
|
int ser=-1; //used dwelling level
|
2008-04-11 20:41:02 +03:00
|
|
|
CGTownInstance *t = const_cast<CGTownInstance*>(static_cast<const CGTownInstance*>(obj));
|
|
|
|
|
|
|
|
//verify
|
|
|
|
bool found = false;
|
|
|
|
typedef std::pair<const int,int> Parka;
|
2008-08-01 14:21:15 +03:00
|
|
|
for(std::map<si32,ui32>::iterator av=t->strInfo.creatures.begin();av!=t->strInfo.creatures.end();av++)
|
2008-04-11 20:41:02 +03:00
|
|
|
{
|
2008-06-01 16:42:29 +03:00
|
|
|
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
|
2008-04-11 20:41:02 +03:00
|
|
|
{
|
2008-08-04 12:05:52 +03:00
|
|
|
amount = std::min(amount,(int)av->second); //reduce recruited amount up to available amount
|
2008-04-13 14:05:39 +03:00
|
|
|
ser = av->first;
|
2008-04-11 20:41:02 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!found) //no such creature
|
|
|
|
return;
|
|
|
|
|
2008-05-12 08:46:04 +03:00
|
|
|
if(amount > CGI->creh->creatures[ID].maxAmount(gs->players[player].resources))
|
|
|
|
return; //not enough resources
|
|
|
|
|
2008-06-01 16:42:29 +03:00
|
|
|
//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
|
2008-04-11 20:41:02 +03:00
|
|
|
|
|
|
|
if(amount<=0) return;
|
|
|
|
|
|
|
|
//recruit
|
2008-06-01 16:42:29 +03:00
|
|
|
int slot = -1; //slot ID
|
2008-07-31 16:21:42 +03:00
|
|
|
std::pair<si32,std::pair<ui32,si32> > parb;
|
2008-04-11 20:41:02 +03:00
|
|
|
|
2008-06-01 16:42:29 +03:00
|
|
|
for(int i=0;i<7;i++) //TODO: if there is already stack of same creatures it should be used always
|
2008-04-11 20:41:02 +03:00
|
|
|
{
|
2008-07-31 16:21:42 +03:00
|
|
|
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
|
2008-04-11 20:41:02 +03:00
|
|
|
{
|
|
|
|
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);
|
2008-04-13 14:05:39 +03:00
|
|
|
|
|
|
|
t->strInfo.creatures[ser] -= amount;
|
2008-05-31 23:37:54 +03:00
|
|
|
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
|
|
|
|
{
|
2008-07-31 16:21:42 +03:00
|
|
|
t->army.slots[slot].first = ID;
|
2008-05-31 23:37:54 +03:00
|
|
|
t->army.slots[slot].second = amount;
|
|
|
|
}
|
2008-07-28 15:44:08 +03:00
|
|
|
cl->playerint[player]->garrisonChanged(obj);
|
2008-04-11 20:41:02 +03:00
|
|
|
|
|
|
|
}
|
2008-06-01 16:42:29 +03:00
|
|
|
//TODO: recruit from dwellings on the adventure map
|
2008-04-11 20:41:02 +03:00
|
|
|
}
|
2007-09-14 16:11:10 +03:00
|
|
|
|
2008-05-23 22:50:11 +03:00
|
|
|
|
|
|
|
bool CCallback::dismissCreature(const CArmedInstance *obj, int stackPos)
|
|
|
|
{
|
2008-08-01 14:21:15 +03:00
|
|
|
if(((player>=0) && obj->tempOwner != player) || obj->army.slots.size()<2)
|
2008-05-31 23:37:54 +03:00
|
|
|
return false;
|
2008-08-01 14:21:15 +03:00
|
|
|
*cl->serv << ui16(503) << obj->id << ui8(stackPos);
|
2008-05-31 23:37:54 +03:00
|
|
|
return true;
|
2008-05-23 22:50:11 +03:00
|
|
|
}
|
2008-05-31 23:37:54 +03:00
|
|
|
bool CCallback::upgradeCreature(const CArmedInstance *obj, int stackPos, int newID)
|
2008-05-23 22:50:11 +03:00
|
|
|
{
|
2008-06-01 16:42:29 +03:00
|
|
|
//TODO: write
|
2008-05-23 22:50:11 +03:00
|
|
|
return false;
|
|
|
|
}
|
2008-07-27 20:07:37 +03:00
|
|
|
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();
|
|
|
|
}
|
2008-05-23 22:50:11 +03:00
|
|
|
UpgradeInfo CCallback::getUpgradeInfo(const CArmedInstance *obj, int stackPos)
|
|
|
|
{
|
2008-05-31 23:37:54 +03:00
|
|
|
UpgradeInfo ret;
|
2008-07-31 16:21:42 +03:00
|
|
|
CCreature *base = &CGI->creh->creatures[((CArmedInstance *)obj)->army.slots[stackPos].first];
|
2008-05-31 23:37:54 +03:00
|
|
|
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;
|
2008-08-01 14:21:15 +03:00
|
|
|
for(std::set<si32>::iterator i=t->builtBuildings.begin(); i!=t->builtBuildings.end(); i++)
|
2008-05-31 23:37:54 +03:00
|
|
|
{
|
|
|
|
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;
|
2008-05-23 22:50:11 +03:00
|
|
|
}
|
|
|
|
|
2008-06-30 03:06:41 +03:00
|
|
|
const StartInfo * CCallback::getStartInfo()
|
|
|
|
{
|
|
|
|
return gs->scenarioOps;
|
|
|
|
}
|
|
|
|
|
2007-09-18 16:30:26 +03:00
|
|
|
int CCallback::howManyTowns()
|
|
|
|
{
|
|
|
|
return gs->players[gs->currentPlayer].towns.size();
|
|
|
|
}
|
2007-10-27 22:38:48 +03:00
|
|
|
const CGTownInstance * CCallback::getTownInfo(int val, bool mode) //mode = 0 -> val = serial; mode = 1 -> val = ID
|
2007-09-18 16:30:26 +03:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2007-10-20 00:12:37 +03:00
|
|
|
int CCallback::howManyHeroes()
|
2007-09-14 16:11:10 +03:00
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2007-09-14 16:11:10 +03:00
|
|
|
return gs->players[player].heroes.size();
|
|
|
|
}
|
2007-10-27 22:38:48 +03:00
|
|
|
const CGHeroInstance * CCallback::getHeroInfo(int player, int val, bool mode) //mode = 0 -> val = serial; mode = 1 -> val = ID
|
2007-09-14 16:11:10 +03:00
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2007-09-14 16:11:10 +03:00
|
|
|
if (gs->currentPlayer!=player) //TODO: checking if we are allowed to give that info
|
|
|
|
return NULL;
|
|
|
|
if (!mode)
|
2008-01-20 18:24:03 +02:00
|
|
|
if(val<gs->players[player].heroes.size())
|
|
|
|
return gs->players[player].heroes[val];
|
|
|
|
else return NULL;
|
2007-09-14 16:11:10 +03:00
|
|
|
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;
|
2007-09-16 20:21:23 +03:00
|
|
|
}
|
2007-09-18 16:30:26 +03:00
|
|
|
|
2007-09-16 20:21:23 +03:00
|
|
|
int CCallback::getResourceAmount(int type)
|
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2007-12-01 14:50:33 +02:00
|
|
|
return gs->players[player].resources[type];
|
2007-09-18 16:30:26 +03:00
|
|
|
}
|
2008-08-01 14:21:15 +03:00
|
|
|
std::vector<si32> CCallback::getResourceAmount()
|
2008-05-12 08:46:04 +03:00
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2008-05-12 08:46:04 +03:00
|
|
|
return gs->players[player].resources;
|
|
|
|
}
|
2007-09-18 16:30:26 +03:00
|
|
|
int CCallback::getDate(int mode)
|
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2008-07-25 20:28:28 +03:00
|
|
|
return gs->getDate(mode);
|
2007-10-05 21:10:33 +03:00
|
|
|
}
|
2008-07-29 12:53:27 +03:00
|
|
|
std::vector < std::string > CCallback::getObjDescriptions(int3 pos)
|
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2008-07-29 12:53:27 +03:00
|
|
|
std::vector<std::string> ret;
|
2008-07-30 20:51:19 +03:00
|
|
|
BOOST_FOREACH(const CGObjectInstance * obj, gs->map->terrain[pos.x][pos.y][pos.z].blockingObjects)
|
|
|
|
ret.push_back(obj->hoverName);
|
2008-07-29 12:53:27 +03:00
|
|
|
return ret;
|
|
|
|
}
|
2007-10-05 21:16:22 +03:00
|
|
|
bool CCallback::verifyPath(CPath * path, bool blockSea)
|
|
|
|
{
|
|
|
|
for (int i=0;i<path->nodes.size();i++)
|
|
|
|
{
|
2008-07-29 12:53:27 +03:00
|
|
|
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)))
|
2007-10-05 21:16:22 +03:00
|
|
|
return false; //path is wrong - one of the tiles is blocked
|
|
|
|
|
|
|
|
if (blockSea)
|
|
|
|
{
|
|
|
|
if (i==0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (
|
2008-08-02 18:08:03 +03:00
|
|
|
((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tileInfo->tertype==water)
|
2007-10-05 21:16:22 +03:00
|
|
|
&&
|
2008-08-02 18:08:03 +03:00
|
|
|
(CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tileInfo->tertype!=water))
|
2007-10-05 21:16:22 +03:00
|
|
|
||
|
2008-08-02 18:08:03 +03:00
|
|
|
((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tileInfo->tertype!=water)
|
2007-10-05 21:16:22 +03:00
|
|
|
&&
|
2008-08-02 18:08:03 +03:00
|
|
|
(CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tileInfo->tertype==water))
|
2007-10-05 23:38:14 +03:00
|
|
|
||
|
2008-08-02 18:08:03 +03:00
|
|
|
(CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tileInfo->tertype==rock)
|
2007-10-05 21:16:22 +03:00
|
|
|
|
|
|
|
)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2007-10-05 21:10:33 +03:00
|
|
|
|
2008-06-30 03:06:41 +03:00
|
|
|
std::vector< std::vector< std::vector<unsigned char> > > & CCallback::getVisibilityMap()
|
2007-10-16 20:46:01 +03:00
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2007-10-16 20:46:01 +03:00
|
|
|
return gs->players[player].fogOfWarMap;
|
2007-10-20 00:12:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CCallback::isVisible(int3 pos, int Player)
|
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2007-10-20 00:12:37 +03:00
|
|
|
return gs->players[Player].fogOfWarMap[pos.x][pos.y][pos.z];
|
|
|
|
}
|
|
|
|
|
2008-02-18 23:14:28 +02:00
|
|
|
std::vector < const CGTownInstance *> CCallback::getTownsInfo(bool onlyOur)
|
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2008-02-18 23:14:28 +02:00
|
|
|
std::vector < const CGTownInstance *> ret = std::vector < const CGTownInstance *>();
|
2008-07-25 20:28:28 +03:00
|
|
|
for ( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
|
2008-02-18 23:14:28 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2007-12-06 20:55:58 +02:00
|
|
|
std::vector < const CGHeroInstance *> CCallback::getHeroesInfo(bool onlyOur)
|
2007-10-20 00:12:37 +03:00
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2008-08-02 00:41:38 +03:00
|
|
|
std::vector < const CGHeroInstance *> ret;
|
|
|
|
for(int i=0;i<gs->map->heroes.size();i++)
|
2007-10-20 00:12:37 +03:00
|
|
|
{
|
2008-08-02 00:41:38 +03:00
|
|
|
if( (gs->map->heroes[i]->tempOwner==player) ||
|
|
|
|
(isVisible(gs->map->heroes[i]->getPosition(false),player) && !onlyOur) )
|
2007-10-20 00:12:37 +03:00
|
|
|
{
|
2008-08-02 00:41:38 +03:00
|
|
|
ret.push_back(gs->map->heroes[i]);
|
2007-10-20 00:12:37 +03:00
|
|
|
}
|
2008-08-02 00:41:38 +03:00
|
|
|
}
|
2007-10-20 00:12:37 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCallback::isVisible(int3 pos)
|
|
|
|
{
|
|
|
|
return isVisible(pos,player);
|
2007-10-24 22:54:35 +03:00
|
|
|
}
|
2007-11-19 00:58:28 +02:00
|
|
|
|
2007-12-01 14:50:33 +02:00
|
|
|
int CCallback::getMyColor()
|
|
|
|
{
|
|
|
|
return player;
|
|
|
|
}
|
2007-12-06 21:06:07 +02:00
|
|
|
int CCallback::getHeroSerial(const CGHeroInstance * hero)
|
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2007-12-06 21:06:07 +02:00
|
|
|
for (int i=0; i<gs->players[player].heroes.size();i++)
|
|
|
|
{
|
|
|
|
if (gs->players[player].heroes[i]==hero)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2008-01-28 16:01:09 +02:00
|
|
|
const CCreatureSet* CCallback::getGarrison(const CGObjectInstance *obj)
|
|
|
|
{
|
2008-01-28 22:58:19 +02:00
|
|
|
if(!obj)
|
|
|
|
return NULL;
|
2008-01-28 16:01:09 +02:00
|
|
|
if(obj->ID == 34)
|
|
|
|
return &(dynamic_cast<const CGHeroInstance*>(obj))->army;
|
|
|
|
else if(obj->ID == 98)
|
2008-02-25 01:06:27 +02:00
|
|
|
return &(dynamic_cast<const CGTownInstance*>(obj)->army);
|
2008-01-28 16:01:09 +02:00
|
|
|
else return NULL;
|
|
|
|
}
|
2008-01-26 21:36:31 +02:00
|
|
|
|
2008-01-28 16:01:09 +02:00
|
|
|
int CCallback::swapCreatures(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2)
|
2008-01-26 21:36:31 +02:00
|
|
|
{
|
2008-07-31 16:21:42 +03:00
|
|
|
if(s1->tempOwner != player || s2->tempOwner != player)
|
2008-01-26 21:36:31 +02:00
|
|
|
return -1;
|
2008-04-13 14:05:39 +03:00
|
|
|
|
2008-07-31 16:21:42 +03:00
|
|
|
*cl->serv << ui16(502) << ui8(1) << s1->id << ui8(p1) << s2->id << ui8(p2);
|
2008-04-13 14:05:39 +03:00
|
|
|
return 0;
|
2008-01-26 21:36:31 +02:00
|
|
|
}
|
2008-01-27 18:07:27 +02:00
|
|
|
|
2008-01-30 00:47:43 +02:00
|
|
|
int CCallback::mergeStacks(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2)
|
2008-04-13 14:05:39 +03:00
|
|
|
{
|
2008-07-31 16:21:42 +03:00
|
|
|
if ((s1->tempOwner!= player || s2->tempOwner!=player))
|
2008-04-13 14:05:39 +03:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2008-07-31 16:21:42 +03:00
|
|
|
*cl->serv << ui16(502) << ui8(2) << s1->id << ui8(p1) << s2->id << ui8(p2);
|
2008-04-13 14:05:39 +03:00
|
|
|
return 0;
|
2008-01-30 00:47:43 +02:00
|
|
|
}
|
|
|
|
int CCallback::splitStack(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2, int val)
|
|
|
|
{
|
2008-07-31 16:21:42 +03:00
|
|
|
if (s1->tempOwner!= player || s2->tempOwner!=player || (!val))
|
2008-04-19 19:15:04 +03:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2008-07-31 16:21:42 +03:00
|
|
|
*cl->serv << ui16(502) << ui8(3) << s1->id << ui8(p1) << s2->id << ui8(p2) << si32(val);
|
2008-04-19 19:15:04 +03:00
|
|
|
return 0;
|
2008-01-30 00:47:43 +02:00
|
|
|
}
|
|
|
|
|
2008-01-27 18:07:27 +02:00
|
|
|
bool CCallback::dismissHero(const CGHeroInstance *hero)
|
|
|
|
{
|
2008-08-02 00:41:38 +03:00
|
|
|
if(player!=hero->tempOwner) return false;
|
|
|
|
*cl->serv << ui16(500) << hero->id;
|
|
|
|
return true;
|
2008-01-27 18:07:27 +02:00
|
|
|
}
|
|
|
|
|
2007-12-01 14:50:33 +02:00
|
|
|
int CCallback::getMySerial()
|
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2007-12-01 14:50:33 +02:00
|
|
|
return gs->players[player].serial;
|
|
|
|
}
|
2007-11-19 00:58:28 +02:00
|
|
|
|
2008-01-30 12:55:43 +02:00
|
|
|
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;
|
|
|
|
}
|
2007-11-19 00:58:28 +02:00
|
|
|
|
2008-08-01 14:21:15 +03:00
|
|
|
bool CCallback::buildBuilding(const CGTownInstance *town, si32 buildingID)
|
2008-03-21 02:03:31 +02:00
|
|
|
{
|
|
|
|
CGTownInstance * t = const_cast<CGTownInstance *>(town);
|
2008-05-03 18:30:11 +03:00
|
|
|
|
2008-08-01 14:21:15 +03:00
|
|
|
if(town->tempOwner!=player)
|
2008-03-23 03:01:17 +02:00
|
|
|
return false;
|
2008-08-01 14:21:15 +03:00
|
|
|
CBuilding *b = CGI->buildh->buildings[t->subID][buildingID];
|
2008-03-21 02:03:31 +02:00
|
|
|
for(int i=0;i<7;i++)
|
2008-08-01 14:21:15 +03:00
|
|
|
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
|
2008-03-21 02:03:31 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-03-10 22:19:41 +02:00
|
|
|
int CCallback::battleGetBattlefieldType()
|
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
|
|
|
return CGI->mh->ttiles[gs->curB->tile.x][gs->curB->tile.y][gs->curB->tile.z].tileInfo->tertype;
|
2008-03-10 22:19:41 +02:00
|
|
|
}
|
|
|
|
|
2008-04-13 14:05:39 +03:00
|
|
|
int CCallback::battleGetObstaclesAtTile(int tile) //returns bitfield
|
|
|
|
{
|
|
|
|
//TODO - write
|
|
|
|
return -1;
|
|
|
|
}
|
2008-04-07 20:51:46 +03:00
|
|
|
int CCallback::battleGetStack(int pos)
|
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
|
|
|
return gs->battleGetStack(pos);
|
2008-04-07 20:51:46 +03:00
|
|
|
}
|
|
|
|
|
2008-07-02 11:39:56 +03:00
|
|
|
CStack* CCallback::battleGetStackByID(int ID)
|
2008-04-07 20:51:46 +03:00
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
|
|
|
if(!gs->curB) return NULL;
|
|
|
|
return gs->curB->getStack(ID);
|
2008-04-07 20:51:46 +03:00
|
|
|
}
|
|
|
|
|
2008-07-02 11:39:56 +03:00
|
|
|
CStack* CCallback::battleGetStackByPos(int pos)
|
2008-06-11 04:58:18 +03:00
|
|
|
{
|
|
|
|
return battleGetStackByID(battleGetStack(pos));
|
|
|
|
}
|
|
|
|
|
2008-03-29 12:59:18 +02:00
|
|
|
int CCallback::battleGetPos(int stack)
|
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
|
|
|
for(int g=0; g<gs->curB->stacks.size(); ++g)
|
2008-03-29 12:59:18 +02:00
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
if(gs->curB->stacks[g]->ID == stack)
|
|
|
|
return gs->curB->stacks[g]->position;
|
2008-03-29 12:59:18 +02:00
|
|
|
}
|
2008-04-07 20:51:46 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-03-14 20:24:37 +02:00
|
|
|
std::map<int, CStack> CCallback::battleGetStacks()
|
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2008-03-14 20:24:37 +02:00
|
|
|
std::map<int, CStack> ret;
|
2008-08-05 02:04:15 +03:00
|
|
|
for(int g=0; g<gs->curB->stacks.size(); ++g)
|
2008-03-14 20:24:37 +02:00
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
ret[gs->curB->stacks[g]->ID] = *(gs->curB->stacks[g]);
|
2008-03-14 20:24:37 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-03-23 19:25:38 +02:00
|
|
|
CCreature CCallback::battleGetCreature(int number)
|
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
|
|
|
for(int h=0; h<gs->curB->stacks.size(); ++h)
|
2008-03-24 15:25:11 +02:00
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
if(gs->curB->stacks[h]->ID == number) //creature found
|
|
|
|
return *(gs->curB->stacks[h]->creature);
|
2008-03-24 15:25:11 +02:00
|
|
|
}
|
2008-08-04 12:05:52 +03:00
|
|
|
#ifndef __GNUC__
|
2008-04-11 20:41:02 +03:00
|
|
|
throw new std::exception("Cannot find the creature");
|
2008-08-04 12:05:52 +03:00
|
|
|
#else
|
|
|
|
throw new std::exception();
|
|
|
|
#endif
|
2008-03-23 19:25:38 +02:00
|
|
|
}
|
|
|
|
|
2008-03-29 12:59:18 +02:00
|
|
|
std::vector<int> CCallback::battleGetAvailableHexes(int ID)
|
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
|
|
|
return gs->battleGetRange(ID);
|
2008-03-29 12:59:18 +02:00
|
|
|
}
|
|
|
|
|
2008-04-07 20:51:46 +03:00
|
|
|
bool CCallback::battleIsStackMine(int ID)
|
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
|
|
|
for(int h=0; h<gs->curB->stacks.size(); ++h)
|
2008-04-07 20:51:46 +03:00
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
if(gs->curB->stacks[h]->ID == ID) //creature found
|
|
|
|
return gs->curB->stacks[h]->owner == player;
|
2008-04-07 20:51:46 +03:00
|
|
|
}
|
|
|
|
return false;
|
2008-08-02 18:08:03 +03:00
|
|
|
}
|
|
|
|
bool CCallback::battleCanShoot(int ID, int dest) //TODO: finish
|
|
|
|
{
|
2008-08-05 02:04:15 +03:00
|
|
|
boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
|
2008-08-02 18:08:03 +03:00
|
|
|
if(battleGetStackByID(ID)->creature->isShooting()
|
|
|
|
&& battleGetStack(dest) != -1
|
|
|
|
&& battleGetStackByPos(dest)->owner != battleGetStackByID(ID)->owner
|
|
|
|
&& battleGetStackByPos(dest)->alive)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|