1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-06 09:09:40 +02:00

Rewrote hero moving code. Seems to be working.

This commit is contained in:
Michał W. Urbańczyk
2008-07-28 12:44:08 +00:00
parent 001868652a
commit 3247a9a4dd
15 changed files with 454 additions and 233 deletions

View File

@@ -11,6 +11,9 @@
#include "../lib/NetPacks.h"
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include "../hch/CObjectHandler.h"
CSharedCond<std::set<IPack*> > mess(new std::set<IPack*>);
CClient::CClient(void)
{
}
@@ -58,17 +61,18 @@ CClient::CClient(CConnection *con, StartInfo *si)
for (int i=0; i<CGI->state->scenarioOps->playerInfos.size();i++) //initializing interfaces
{
CCallback *cb = new CCallback(CGI->state,CGI->state->scenarioOps->playerInfos[i].color,this);
if(!CGI->state->scenarioOps->playerInfos[i].human)
CGI->playerint.push_back(static_cast<CGameInterface*>(CAIHandler::getNewAI(cb,"EmptyAI.dll")));
ui8 color = gs->scenarioOps->playerInfos[i].color;
CCallback *cb = new CCallback(gs,color,this);
if(!gs->scenarioOps->playerInfos[i].human)
playerint[color] = static_cast<CGameInterface*>(CAIHandler::getNewAI(cb,"EmptyAI.dll"));
else
{
CGI->state->currentPlayer=CGI->state->scenarioOps->playerInfos[i].color;
CGI->playerint.push_back(new CPlayerInterface(CGI->state->scenarioOps->playerInfos[i].color,i));
((CPlayerInterface*)(CGI->playerint[i]))->init(cb);
gs->currentPlayer = color;
playerint[color] = new CPlayerInterface(color,i);
playerint[color]->init(cb);
}
}
CGI->consoleh->cb = new CCallback(CGI->state,-1,this);
CGI->consoleh->cb = new CCallback(gs,-1,this);
}
CClient::~CClient(void)
{
@@ -82,7 +86,7 @@ void CClient::process(int what)
ui8 player;
*serv >> player;//who?
std::cout << "It's turn of "<<(unsigned)player<<" player."<<std::endl;
boost::thread(boost::bind(&CGameInterface::yourTurn,CGI->playerint[gs->players[player].serial]));
boost::thread(boost::bind(&CGameInterface::yourTurn,playerint[player]));
break;
}
case 101:
@@ -94,6 +98,42 @@ void CClient::process(int what)
std::cout << "done!"<<std::endl;
break;
}
case 501: //hero movement response - we have to notify interfaces and callback
{
TryMoveHero *th = new TryMoveHero;
*serv >> *th;
std::cout << "HeroMove: id="<<th->id<<"\tResult: "<<(unsigned)th->result<<"\tPosition "<<th->end<<std::endl;
gs->apply(th);
int player = gs->map->objects[th->id]->getOwner();
if(playerint[player])
{
for(std::set<int3>::iterator i=th->fowRevealed.begin(); i != th->fowRevealed.end(); i++)
playerint[player]->tileRevealed(*i);
//boost::function<void(int3)> tr = boost::bind(&CGameInterface::tileRevealed,playerint[player]);
//std::for_each(th->fowRevealed.begin(),th->fowRevealed.end(),tr);
}
//notify interfacesabout move
int nn=0; //number of interfece of currently browsed player
for(std::map<ui8, CGameInterface*>::iterator i=playerint.begin();i!=playerint.end();i++)
{
if(gs->players[i->first].fogOfWarMap[th->start.x-1][th->start.y][th->start.z] || gs->players[i->first].fogOfWarMap[th->end.x-1][th->end.y][th->end.z])
{
HeroMoveDetails hmd(th->start,th->end,static_cast<CGHeroInstance*>(gs->map->objects[th->id]));
hmd.successful = th->result;
i->second->heroMoved(hmd);
}
}
//add info for callback
mess.mx->lock();
mess.res->insert(th);
mess.mx->unlock();
mess.cv->notify_all();
break;
}
default:
throw std::exception("Not supported server message!");
break;

View File

@@ -5,10 +5,37 @@ class CGameState;
class CGameInterface;
class CConnection;
class CCallback;
namespace boost
{
class mutex;
class condition_variable;
}
template <typename T>
struct CSharedCond
{
boost::mutex *mx;
boost::condition_variable *cv;
T *res;
CSharedCond(T*R)
{
res = R;
mx = new boost::mutex;
cv = new boost::condition_variable;
}
~CSharedCond()
{
delete res;
delete mx;
delete cv;
}
};
class CClient
{
CGameState *gs;
std::map<int,CGameInterface *> playerint;
std::map<ui8,CGameInterface *> playerint;
CConnection *serv;
public:
CClient(void);
@@ -19,4 +46,5 @@ public:
void run();
friend class CCallback;
friend class CScriptCallback;
};

View File

@@ -35,7 +35,7 @@ SDL_Surface * Graphics::drawHeroInfoWin(const CGHeroInstance * curh)
SDL_SetColorKey(ret,SDL_SRCCOLORKEY,SDL_MapRGB(ret->format,0,255,255));
printAt(curh->name,75,15,GEOR13,zwykly,ret);
drawPrimarySkill(curh, ret);
for (std::map<int,std::pair<CCreature*,int> >::const_iterator i=curh->army.slots.begin(); i!=curh->army.slots.end();i++)
for (std::map<si32,std::pair<CCreature*,si32> >::const_iterator i=curh->army.slots.begin(); i!=curh->army.slots.end();i++)
{
blitAt(graphics->smallImgs[(*i).second.first->idNumber],slotsPos[(*i).first].first+1,slotsPos[(*i).first].second+1,ret);
itoa((*i).second.second,buf,10);
@@ -64,7 +64,7 @@ SDL_Surface * Graphics::drawTownInfoWin(const CGTownInstance * curh)
blitAt(halls->ourImages[pom].bitmap,77,42,ret);
itoa(curh->dailyIncome(),buf,10);
printAtMiddle(buf,167,70,GEORM,zwykly,ret);
for (std::map<int,std::pair<CCreature*,int> >::const_iterator i=curh->army.slots.begin(); i!=curh->army.slots.end();i++)
for (std::map<si32,std::pair<CCreature*,si32> >::const_iterator i=curh->army.slots.begin(); i!=curh->army.slots.end();i++)
{
if(!i->second.first)
continue;