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"
|
2007-08-30 13:11:53 +03:00
|
|
|
#include "hch\CHeroHandler.h"
|
2007-09-18 16:30:26 +03:00
|
|
|
#include "hch\CTownHandler.h"
|
2007-08-29 15:18:31 +03:00
|
|
|
#include "CGameInfo.h"
|
2007-08-30 13:11:53 +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"
|
2007-10-27 22:38:48 +03:00
|
|
|
#include "CLua.h"
|
2007-11-19 00:58:28 +02:00
|
|
|
#include "hch/CGeneralTextHandler.h"
|
|
|
|
#include "CAdvmapInterface.h"
|
|
|
|
#include "CPlayerInterface.h"
|
2008-03-14 20:24:37 +02:00
|
|
|
|
2007-11-19 00:58:28 +02:00
|
|
|
LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...);
|
2008-03-14 20:24:37 +02:00
|
|
|
|
2007-10-27 22:38:48 +03:00
|
|
|
int CCallback::lowestSpeed(CGHeroInstance * chi)
|
2007-10-03 21:53:49 +03:00
|
|
|
{
|
|
|
|
int min = 150;
|
|
|
|
for ( std::map<int,std::pair<CCreature*,int> >::iterator i = chi->army.slots.begin();
|
|
|
|
i!=chi->army.slots.end(); i++ )
|
|
|
|
{
|
|
|
|
if (min>(*i).second.first->speed)
|
|
|
|
min = (*i).second.first->speed;
|
|
|
|
}
|
|
|
|
return min;
|
|
|
|
}
|
2007-10-27 22:38:48 +03:00
|
|
|
int CCallback::valMovePoints(CGHeroInstance * chi)
|
2007-10-03 21:53:49 +03:00
|
|
|
{
|
|
|
|
int ret = 1270+70*lowestSpeed(chi);
|
|
|
|
if (ret>2000)
|
|
|
|
ret=2000;
|
|
|
|
|
|
|
|
//TODO: additional bonuses (but they aren't currently stored in chi)
|
2007-08-27 17:15:03 +03:00
|
|
|
|
2007-10-03 21:53:49 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
void CCallback::newTurn()
|
|
|
|
{
|
|
|
|
//std::map<int, PlayerState>::iterator i = gs->players.begin() ;
|
2007-10-21 19:45:13 +03:00
|
|
|
gs->day++;
|
2007-11-24 16:17:57 +02:00
|
|
|
for (std::set<CCPPObjectScript *>::iterator i=gs->cppscripts.begin();i!=gs->cppscripts.end();i++)
|
2007-11-24 00:33:55 +02:00
|
|
|
{
|
2007-11-24 16:17:57 +02:00
|
|
|
(*i)->newTurn();
|
2007-11-24 00:33:55 +02:00
|
|
|
}
|
2007-10-03 21:53:49 +03:00
|
|
|
for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
|
|
|
|
{
|
|
|
|
for (int j=0;j<(*i).second.heroes.size();j++)
|
|
|
|
{
|
|
|
|
(*i).second.heroes[j]->movement = valMovePoints((*i).second.heroes[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
|
|
|
for(std::map<int, 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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
throw std::exception("Unknown path format");
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
int3 stpos, endpos;
|
2007-10-05 21:16:22 +03:00
|
|
|
stpos = int3(ourPath->nodes[i].coord.x, ourPath->nodes[i].coord.y, hero->pos.z);
|
|
|
|
endpos = int3(ourPath->nodes[i-1].coord.x, ourPath->nodes[i-1].coord.y, hero->pos.z);
|
2007-08-30 13:11:53 +03:00
|
|
|
HeroMoveDetails curd;
|
|
|
|
curd.src = stpos;
|
|
|
|
curd.dst = endpos;
|
2007-10-27 23:14:25 +03:00
|
|
|
curd.ho = hero;
|
2007-11-19 00:58:28 +02:00
|
|
|
curd.owner = hero->getOwner();
|
2007-10-13 21:49:43 +03:00
|
|
|
/*if(player!=-1)
|
2007-10-04 19:40:40 +03:00
|
|
|
{
|
2007-10-05 21:16:22 +03:00
|
|
|
hero->pos = endpos;
|
2007-10-13 21:49:43 +03:00
|
|
|
}*/
|
2007-10-14 15:11:18 +03:00
|
|
|
if((hero->movement>=CGI->mh->getCost(stpos, endpos, hero)) || player==-1)
|
2007-08-30 13:11:53 +03:00
|
|
|
{ //performing move
|
2007-10-14 15:11:18 +03:00
|
|
|
hero->movement-=CGI->mh->getCost(stpos, endpos, hero);
|
2007-10-18 23:14:26 +03:00
|
|
|
|
2008-02-03 17:24:35 +02:00
|
|
|
std::vector< CGObjectInstance * > vis = CGI->mh->getVisitableObjs(CGHeroInstance::convertPosition(curd.dst,false));
|
2007-11-25 15:16:45 +02:00
|
|
|
bool blockvis = false;
|
|
|
|
for (int pit = 0; pit<vis.size();pit++)
|
|
|
|
if (vis[pit]->blockVisit)
|
|
|
|
blockvis = true;
|
2007-10-26 20:55:33 +03:00
|
|
|
|
2007-11-25 15:16:45 +02:00
|
|
|
if (!blockvis)
|
|
|
|
{
|
|
|
|
curd.successful = true;
|
2007-12-01 14:50:33 +02:00
|
|
|
hero->pos = curd.dst;
|
2007-10-26 20:55:33 +03:00
|
|
|
|
2008-01-28 16:01:09 +02:00
|
|
|
//inform leaved objects
|
2008-02-03 17:24:35 +02:00
|
|
|
std::vector< CGObjectInstance * > leave = CGI->mh->getVisitableObjs(CGHeroInstance::convertPosition(curd.src,false));
|
2008-01-28 16:01:09 +02:00
|
|
|
for (int iii=0; iii<leave.size(); iii++) //if object is visitable we call onHeroVisit
|
|
|
|
{
|
|
|
|
//TODO: allow to handle this in LUA
|
|
|
|
if(leave[iii]->state) //hard-coded function
|
|
|
|
leave[iii]->state->onHeroLeave(leave[iii],curd.ho->subID);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//reveal fog of war
|
|
|
|
int heroSight = hero->getSightDistance();
|
2007-11-25 15:16:45 +02:00
|
|
|
int xbeg = stpos.x - heroSight - 2;
|
|
|
|
if(xbeg < 0)
|
|
|
|
xbeg = 0;
|
|
|
|
int xend = stpos.x + heroSight + 2;
|
|
|
|
if(xend >= CGI->ac->map.width)
|
|
|
|
xend = CGI->ac->map.width;
|
|
|
|
int ybeg = stpos.y - heroSight - 2;
|
|
|
|
if(ybeg < 0)
|
|
|
|
ybeg = 0;
|
|
|
|
int yend = stpos.y + heroSight + 2;
|
|
|
|
if(yend >= CGI->ac->map.height)
|
|
|
|
yend = CGI->ac->map.height;
|
|
|
|
for(int xd=xbeg; xd<xend; ++xd) //revealing part of map around heroes
|
2007-10-18 23:14:26 +03:00
|
|
|
{
|
2007-11-25 15:16:45 +02:00
|
|
|
for(int yd=ybeg; yd<yend; ++yd)
|
|
|
|
{
|
|
|
|
int deltaX = (hero->getPosition(false).x-xd)*(hero->getPosition(false).x-xd);
|
|
|
|
int deltaY = (hero->getPosition(false).y-yd)*(hero->getPosition(false).y-yd);
|
|
|
|
if(deltaX+deltaY<hero->getSightDistance()*hero->getSightDistance())
|
2007-12-06 21:54:31 +02:00
|
|
|
{
|
|
|
|
if(gs->players[player].fogOfWarMap[xd][yd][hero->getPosition(false).z] == 0)
|
|
|
|
{
|
|
|
|
CGI->playerint[gs->players[player].serial]->tileRevealed(int3(xd, yd, hero->getPosition(false).z));
|
|
|
|
}
|
2007-11-25 15:16:45 +02:00
|
|
|
gs->players[player].fogOfWarMap[xd][yd][hero->getPosition(false).z] = 1;
|
2007-12-06 21:54:31 +02:00
|
|
|
}
|
2007-11-25 15:16:45 +02:00
|
|
|
}
|
2007-10-18 23:14:26 +03:00
|
|
|
}
|
2007-10-20 00:12:37 +03:00
|
|
|
|
2008-01-28 16:01:09 +02:00
|
|
|
|
|
|
|
//notify interfacesabout move
|
2007-11-25 15:16:45 +02:00
|
|
|
int nn=0; //number of interfece of currently browsed player
|
|
|
|
for(std::map<int, PlayerState>::iterator j=CGI->state->players.begin(); j!=CGI->state->players.end(); ++j)//CGI->state->players.size(); ++j) //for testing
|
|
|
|
{
|
|
|
|
if (j->first > PLAYER_LIMIT)
|
|
|
|
break;
|
|
|
|
if(j->second.fogOfWarMap[stpos.x-1][stpos.y][stpos.z] || j->second.fogOfWarMap[endpos.x-1][endpos.y][endpos.z])
|
|
|
|
{ //player should be notified
|
|
|
|
CGI->playerint[j->second.serial]->heroMoved(curd);
|
|
|
|
}
|
|
|
|
++nn;
|
|
|
|
}
|
2008-01-28 16:01:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
//call objects if they arevisited
|
2007-11-25 15:16:45 +02:00
|
|
|
for (int iii=0; iii<vis.size(); iii++) //if object is visitable we call onHeroVisit
|
|
|
|
{
|
|
|
|
if(gs->checkFunc(vis[iii]->ID,"heroVisit")) //script function
|
|
|
|
gs->objscr[vis[iii]->ID]["heroVisit"]->onHeroVisit(vis[iii],curd.ho->subID);
|
|
|
|
if(vis[iii]->state) //hard-coded function
|
|
|
|
vis[iii]->state->onHeroVisit(vis[iii],curd.ho->subID);
|
2007-10-20 00:12:37 +03:00
|
|
|
}
|
|
|
|
}
|
2007-12-01 14:50:33 +02:00
|
|
|
else //interaction with blocking object (like resources)
|
2007-11-19 00:58:28 +02:00
|
|
|
{
|
2007-11-25 15:16:45 +02:00
|
|
|
curd.successful = false;
|
|
|
|
CGI->playerint[gs->players[hero->getOwner()].serial]->heroMoved(curd);
|
|
|
|
for (int iii=0; iii<vis.size(); iii++) //if object is visitable we call onHeroVisit
|
|
|
|
{
|
|
|
|
if (vis[iii]->blockVisit)
|
|
|
|
{
|
|
|
|
if(gs->checkFunc(vis[iii]->ID,"heroVisit")) //script function
|
|
|
|
gs->objscr[vis[iii]->ID]["heroVisit"]->onHeroVisit(vis[iii],curd.ho->subID);
|
|
|
|
if(vis[iii]->state) //hard-coded function
|
|
|
|
vis[iii]->state->onHeroVisit(vis[iii],curd.ho->subID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2007-11-19 00:58:28 +02:00
|
|
|
}
|
2007-10-27 22:38:48 +03:00
|
|
|
|
2007-08-30 13:11:53 +03:00
|
|
|
}
|
2007-10-14 15:11:18 +03:00
|
|
|
else
|
|
|
|
return true; //move ended - no more movement points
|
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
|
|
|
|
IChosen * ask = (IChosen *)asker;
|
|
|
|
ask->chosen(selection);
|
|
|
|
}
|
2007-09-14 16:11:10 +03:00
|
|
|
|
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
|
|
|
{
|
|
|
|
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
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2007-12-01 14:50:33 +02:00
|
|
|
return gs->players[player].resources[type];
|
2007-09-18 16:30:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int CCallback::getDate(int mode)
|
|
|
|
{
|
|
|
|
int temp;
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return gs->day;
|
2007-10-21 21:41:58 +03:00
|
|
|
break;
|
2007-09-18 16:30:26 +03:00
|
|
|
case 1:
|
|
|
|
temp = (gs->day)%7;
|
|
|
|
if (temp)
|
|
|
|
return temp;
|
|
|
|
else return 7;
|
2007-10-21 21:41:58 +03:00
|
|
|
break;
|
2007-09-18 16:30:26 +03:00
|
|
|
case 2:
|
|
|
|
temp = ((gs->day-1)/7)+1;
|
2007-10-21 21:41:58 +03:00
|
|
|
if (!(temp%4))
|
|
|
|
return 4;
|
|
|
|
else
|
|
|
|
return (temp%4);
|
|
|
|
break;
|
2007-09-18 16:30:26 +03:00
|
|
|
case 3:
|
|
|
|
return ((gs->day-1)/28)+1;
|
2007-10-21 21:41:58 +03:00
|
|
|
break;
|
2007-09-18 16:30:26 +03:00
|
|
|
}
|
2007-10-25 19:14:43 +03:00
|
|
|
return 0;
|
2007-10-05 21:10:33 +03:00
|
|
|
}
|
2007-10-05 21:16:22 +03:00
|
|
|
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].blocked
|
|
|
|
&& (! (CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].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].terType==EterrainType::water)
|
|
|
|
&&
|
|
|
|
(CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].terType!=EterrainType::water))
|
|
|
|
||
|
|
|
|
((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].terType!=EterrainType::water)
|
|
|
|
&&
|
|
|
|
(CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].terType==EterrainType::water))
|
2007-10-05 23:38:14 +03:00
|
|
|
||
|
|
|
|
(CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].terType==EterrainType::rock)
|
2007-10-05 21:16:22 +03:00
|
|
|
|
|
|
|
)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2007-10-05 21:10:33 +03:00
|
|
|
|
|
|
|
std::vector < std::string > CCallback::getObjDescriptions(int3 pos)
|
|
|
|
{
|
|
|
|
if(gs->players[player].fogOfWarMap[pos.x][pos.y][pos.z])
|
|
|
|
return CGI->mh->getObjDescriptions(pos);
|
|
|
|
else return std::vector< std::string > ();
|
|
|
|
}
|
2007-10-16 20:46:01 +03:00
|
|
|
|
|
|
|
PseudoV< PseudoV< PseudoV<unsigned char> > > & CCallback::getVisibilityMap()
|
|
|
|
{
|
|
|
|
return gs->players[player].fogOfWarMap;
|
2007-10-20 00:12:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CCallback::isVisible(int3 pos, int Player)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
std::vector < const CGTownInstance *> ret = std::vector < const CGTownInstance *>();
|
|
|
|
for ( std::map<int, 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;
|
|
|
|
}
|
2007-12-06 20:55:58 +02:00
|
|
|
std::vector < const CGHeroInstance *> CCallback::getHeroesInfo(bool onlyOur)
|
2007-10-20 00:12:37 +03:00
|
|
|
{
|
2007-12-06 20:55:58 +02:00
|
|
|
std::vector < const CGHeroInstance *> ret = std::vector < const CGHeroInstance *>();
|
2007-10-20 00:12:37 +03:00
|
|
|
for ( std::map<int, 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)
|
|
|
|
{
|
2007-12-06 20:55:58 +02:00
|
|
|
ret.push_back((*i).second.heroes[j]);
|
2007-10-20 00:12:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // 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);
|
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)
|
|
|
|
{
|
|
|
|
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-01-28 16:01:09 +02:00
|
|
|
CCreatureSet *S1 = const_cast<CCreatureSet*>(getGarrison(s1)), *S2 = const_cast<CCreatureSet*>(getGarrison(s2));
|
2008-01-26 21:36:31 +02:00
|
|
|
if (false)
|
|
|
|
{
|
|
|
|
//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;
|
2008-01-26 23:50:51 +02:00
|
|
|
int pom2 = S2->slots[p2].second;
|
|
|
|
S2->slots[p2].second = S1->slots[p1].second;
|
|
|
|
S1->slots[p1].second = pom2;
|
2008-01-28 16:01:09 +02:00
|
|
|
|
|
|
|
if(!S1->slots[p1].first)
|
|
|
|
S1->slots.erase(p1);
|
|
|
|
if(!S2->slots[p2].first)
|
|
|
|
S2->slots.erase(p2);
|
|
|
|
if(s1->tempOwner<PLAYER_LIMIT)
|
2008-03-06 18:42:34 +02:00
|
|
|
{
|
|
|
|
for(int b=0; b<CGI->playerint.size(); ++b)
|
|
|
|
{
|
|
|
|
if(CGI->playerint[b]->playerID == s1->tempOwner)
|
|
|
|
{
|
|
|
|
CGI->playerint[b]->garrisonChanged(s1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-01-31 23:35:30 +02:00
|
|
|
if((s2->tempOwner<PLAYER_LIMIT) && (s2 != s1))
|
2008-03-06 18:42:34 +02:00
|
|
|
{
|
|
|
|
for(int b=0; b<CGI->playerint.size(); ++b)
|
|
|
|
{
|
|
|
|
if(CGI->playerint[b]->playerID == s2->tempOwner)
|
|
|
|
{
|
|
|
|
CGI->playerint[b]->garrisonChanged(s2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-01-30 00:47:43 +02:00
|
|
|
return 0;
|
2008-01-26 21:36:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int CCallback::splitStack(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2, int val)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-01-27 18:07:27 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-12-01 14:50:33 +02:00
|
|
|
int CCallback::getMySerial()
|
|
|
|
{
|
|
|
|
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-03-10 22:19:41 +02:00
|
|
|
int CCallback::battleGetBattlefieldType()
|
|
|
|
{
|
|
|
|
return CGI->mh->ttiles[CGI->state->curB->tile.x][CGI->state->curB->tile.y][CGI->state->curB->tile.z].terType;
|
|
|
|
}
|
|
|
|
|
2008-03-14 20:24:37 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-11-19 00:58:28 +02:00
|
|
|
int3 CScriptCallback::getPos(CGObjectInstance * ob)
|
|
|
|
{
|
|
|
|
return ob->pos;
|
|
|
|
}
|
|
|
|
void CScriptCallback::changePrimSkill(int ID, int which, int val)
|
|
|
|
{
|
|
|
|
CGHeroInstance * hero = CGI->state->getHero(ID,0);
|
2007-12-25 18:25:53 +02:00
|
|
|
if (which<PRIMARY_SKILLS)
|
2007-11-19 00:58:28 +02:00
|
|
|
{
|
2007-12-25 18:25:53 +02:00
|
|
|
hero->primSkills[which]+=val;
|
|
|
|
for (int i=0; i<CGI->playerint.size(); i++)
|
2007-11-19 00:58:28 +02:00
|
|
|
{
|
2007-12-25 18:25:53 +02:00
|
|
|
if (CGI->playerint[i]->playerID == hero->getOwner())
|
|
|
|
{
|
|
|
|
CGI->playerint[i]->heroPrimarySkillChanged(hero, which, val);
|
|
|
|
break;
|
|
|
|
}
|
2007-11-19 00:58:28 +02:00
|
|
|
}
|
|
|
|
}
|
2007-12-25 18:25:53 +02:00
|
|
|
else if (which==4)
|
|
|
|
{
|
|
|
|
hero->exp+=val;
|
2007-12-28 01:43:36 +02:00
|
|
|
std::cout << "Bohater o ID " << ID <<" (" <<CGI->heroh->heroes[ID]->name <<") dostaje "<<val<<" expa, ale nic z tym nie umiem zrobic :("<<std::endl;
|
2007-12-25 18:25:53 +02:00
|
|
|
//TODO - powiadomic interfejsy, sprawdzic czy nie ma awansu itp
|
|
|
|
}
|
2007-11-19 00:58:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int CScriptCallback::getHeroOwner(int heroID)
|
|
|
|
{
|
|
|
|
CGHeroInstance * hero = CGI->state->getHero(heroID,0);
|
|
|
|
return hero->getOwner();
|
|
|
|
}
|
|
|
|
void CScriptCallback::showInfoDialog(int player, std::string text, std::vector<SComponent*> * components)
|
|
|
|
{
|
|
|
|
//TODO: upewniac sie ze mozemy to zrzutowac (przy customowych interfejsach cos moze sie kopnac)
|
|
|
|
if (player>=0)
|
|
|
|
{
|
|
|
|
CGameInterface * temp = CGI->playerint[CGI->state->players[player].serial];
|
|
|
|
if (temp->human)
|
|
|
|
((CPlayerInterface*)(temp))->showInfoDialog(text,*components);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (int i=0; i<CGI->playerint.size();i++)
|
|
|
|
{
|
|
|
|
if (CGI->playerint[i]->human)
|
|
|
|
((CPlayerInterface*)(CGI->playerint[i]))->showInfoDialog(text,*components);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-12-25 18:25:53 +02:00
|
|
|
|
2007-12-27 02:11:46 +02:00
|
|
|
void CScriptCallback::showSelDialog(int player, std::string text, std::vector<CSelectableComponent*>*components, IChosen * asker)
|
2007-12-25 18:25:53 +02:00
|
|
|
{
|
|
|
|
CGameInterface * temp = CGI->playerint[CGI->state->players[player].serial];
|
|
|
|
if (temp->human)
|
|
|
|
((CPlayerInterface*)(temp))->showSelDialog(text,*components,(int)asker);
|
|
|
|
return;
|
|
|
|
}
|
2007-11-19 00:58:28 +02:00
|
|
|
int CScriptCallback::getSelectedHero()
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
if (LOCPLINT->adventureInt->selection.type == HEROI_TYPE)
|
|
|
|
ret = ((CGHeroInstance*)(LOCPLINT->adventureInt->selection.selected))->subID;
|
|
|
|
else
|
|
|
|
ret = -1;;
|
|
|
|
return ret;
|
|
|
|
}
|
2007-11-24 00:33:55 +02:00
|
|
|
int CScriptCallback::getDate(int mode)
|
|
|
|
{
|
|
|
|
int temp;
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return gs->day;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
temp = (gs->day)%7;
|
|
|
|
if (temp)
|
|
|
|
return temp;
|
|
|
|
else return 7;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
temp = ((gs->day-1)/7)+1;
|
|
|
|
if (!(temp%4))
|
|
|
|
return 4;
|
|
|
|
else
|
|
|
|
return (temp%4);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
return ((gs->day-1)/28)+1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
void CScriptCallback::giveResource(int player, int which, int val)
|
|
|
|
{
|
|
|
|
gs->players[player].resources[which]+=val;
|
2007-11-25 15:16:45 +02:00
|
|
|
CGI->playerint[gs->players[player].serial]->receivedResource(which,val);
|
2007-11-24 00:33:55 +02:00
|
|
|
}
|
2007-12-22 16:40:27 +02:00
|
|
|
void CScriptCallback::showCompInfo(int player, SComponent * comp)
|
|
|
|
{
|
|
|
|
CPlayerInterface * i = dynamic_cast<CPlayerInterface*>(CGI->playerint[gs->players[player].serial]);
|
|
|
|
if(i)
|
|
|
|
i->showComp(*comp);
|
|
|
|
}
|
2008-01-28 16:01:09 +02:00
|
|
|
void CScriptCallback::heroVisitCastle(CGObjectInstance * ob, int heroID)
|
|
|
|
{
|
|
|
|
CGTownInstance * n;
|
|
|
|
if(n = dynamic_cast<CGTownInstance*>(ob))
|
|
|
|
{
|
|
|
|
n->visitingHero = CGI->state->getHero(heroID,0);
|
2008-03-06 18:42:34 +02:00
|
|
|
for(int b=0; b<CGI->playerint.size(); ++b)
|
|
|
|
{
|
|
|
|
if(CGI->playerint[b]->playerID == getHeroOwner(heroID))
|
|
|
|
{
|
|
|
|
CGI->playerint[b]->heroVisitsTown(CGI->state->getHero(heroID,0),n);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-01-28 16:01:09 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
2007-12-22 16:40:27 +02:00
|
|
|
|
2008-01-28 16:01:09 +02:00
|
|
|
void CScriptCallback::stopHeroVisitCastle(CGObjectInstance * ob, int heroID)
|
|
|
|
{
|
|
|
|
CGTownInstance * n;
|
|
|
|
if(n = dynamic_cast<CGTownInstance*>(ob))
|
|
|
|
{
|
2008-02-23 21:20:41 +02:00
|
|
|
if(n->visitingHero && n->visitingHero->type->ID == heroID)
|
2008-01-28 16:01:09 +02:00
|
|
|
n->visitingHero = NULL;
|
2008-02-23 21:20:41 +02:00
|
|
|
return;
|
2008-01-28 16:01:09 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
2008-02-07 20:45:22 +02:00
|
|
|
void CScriptCallback::giveHeroArtifact(int artid, int hid, int position) //pos==-1 - first free slot in backpack
|
|
|
|
{
|
|
|
|
CGHeroInstance* h = gs->getHero(hid,0);
|
|
|
|
if(position<0)
|
|
|
|
{
|
|
|
|
for(int i=0;i<h->artifacts.size();i++)
|
|
|
|
{
|
|
|
|
if(!h->artifacts[i])
|
|
|
|
{
|
|
|
|
h->artifacts[i] = &CGI->arth->artifacts[artid];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
h->artifacts.push_back(&CGI->arth->artifacts[artid]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(h->artifWorn[position]) //slot is occupied
|
|
|
|
{
|
|
|
|
giveHeroArtifact(h->artifWorn[position]->id,hid,-1);
|
|
|
|
}
|
|
|
|
h->artifWorn[position] = &CGI->arth->artifacts[artid];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-25 01:06:27 +02:00
|
|
|
void CScriptCallback::startBattle(CCreatureSet * army1, CCreatureSet * army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2) //use hero=NULL for no hero
|
|
|
|
{
|
|
|
|
gs->battle(army1,army2,tile,hero1,hero2);
|
|
|
|
}
|
|
|
|
void CScriptCallback::startBattle(int heroID, CCreatureSet * army, int3 tile) //for hero<=>neutral army
|
|
|
|
{
|
|
|
|
CGHeroInstance* h = gs->getHero(heroID,0);
|
|
|
|
gs->battle(&h->army,army,tile,h,NULL);
|
|
|
|
}
|
2007-11-19 00:58:28 +02:00
|
|
|
void CLuaCallback::registerFuncs(lua_State * L)
|
|
|
|
{
|
|
|
|
lua_newtable(L);
|
|
|
|
|
|
|
|
#define REGISTER_C_FUNC(x) \
|
|
|
|
lua_pushstring(L, #x); \
|
|
|
|
lua_pushcfunction(L, x); \
|
|
|
|
lua_rawset(L, -3)
|
|
|
|
|
|
|
|
REGISTER_C_FUNC(getPos);
|
|
|
|
REGISTER_C_FUNC(changePrimSkill);
|
|
|
|
REGISTER_C_FUNC(getGnrlText);
|
|
|
|
REGISTER_C_FUNC(getSelectedHero);
|
|
|
|
|
|
|
|
/*
|
|
|
|
REGISTER_C_FUNC(changePrimSkill);
|
|
|
|
REGISTER_C_FUNC(getGnrlText);
|
|
|
|
REGISTER_C_FUNC(changePrimSkill);
|
|
|
|
REGISTER_C_FUNC(getGnrlText);
|
|
|
|
REGISTER_C_FUNC(changePrimSkill);
|
|
|
|
REGISTER_C_FUNC(getGnrlText);*/
|
|
|
|
|
|
|
|
|
|
|
|
lua_setglobal(L, "vcmi");
|
2008-01-28 16:01:09 +02:00
|
|
|
#undef REGISTER_C_FUNC
|
2007-11-19 00:58:28 +02:00
|
|
|
}
|
|
|
|
int CLuaCallback::getPos(lua_State * L)//(CGObjectInstance * object);
|
|
|
|
{
|
|
|
|
const int args = lua_gettop(L); // number of arguments
|
|
|
|
if ((args < 1) || !lua_isnumber(L, 1) )
|
|
|
|
luaL_error(L,
|
|
|
|
"Incorrect arguments to getPos([Object address])");
|
|
|
|
CGObjectInstance * object = (CGObjectInstance *)(lua_tointeger(L, 1));
|
|
|
|
lua_pushinteger(L,object->pos.x);
|
|
|
|
lua_pushinteger(L,object->pos.y);
|
|
|
|
lua_pushinteger(L,object->pos.z);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
int CLuaCallback::changePrimSkill(lua_State * L)//(int ID, int which, int val);
|
|
|
|
{
|
|
|
|
const int args = lua_gettop(L); // number of arguments
|
|
|
|
if ((args < 1) || !lua_isnumber(L, 1) ||
|
|
|
|
((args >= 2) && !lua_isnumber(L, 2)) ||
|
|
|
|
((args >= 3) && !lua_isnumber(L, 3)) )
|
|
|
|
{
|
|
|
|
luaL_error(L,
|
|
|
|
"Incorrect arguments to changePrimSkill([Hero ID], [Which Primary skill], [Change by])");
|
|
|
|
}
|
|
|
|
int ID = lua_tointeger(L, 1),
|
|
|
|
which = lua_tointeger(L, 2),
|
|
|
|
val = lua_tointeger(L, 3);
|
|
|
|
|
|
|
|
CScriptCallback::changePrimSkill(ID,which,val);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int CLuaCallback::getGnrlText(lua_State * L) //(int which),returns string
|
|
|
|
{
|
|
|
|
const int args = lua_gettop(L); // number of arguments
|
|
|
|
if ((args < 1) || !lua_isnumber(L, 1) )
|
|
|
|
luaL_error(L,
|
|
|
|
"Incorrect arguments to getGnrlText([Text ID])");
|
|
|
|
int which = lua_tointeger(L,1);
|
|
|
|
lua_pushstring(L,CGI->generaltexth->allTexts[which].c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int CLuaCallback::getSelectedHero(lua_State * L) //(),returns int (ID of hero, -1 if no hero is seleceted)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
if (LOCPLINT->adventureInt->selection.type == HEROI_TYPE)
|
|
|
|
ret = ((CGHeroInstance*)(LOCPLINT->adventureInt->selection.selected))->subID;
|
|
|
|
else
|
|
|
|
ret = -1;
|
|
|
|
lua_pushinteger(L,ret);
|
|
|
|
return 1;
|
|
|
|
}
|