1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-26 03:52:01 +02:00

Further code for objects (mines) - not finished.

This commit is contained in:
Michał W. Urbańczyk 2008-07-30 21:27:15 +00:00
parent b4f383f196
commit 03287540c7
11 changed files with 175 additions and 76 deletions

View File

@ -10,6 +10,7 @@ class CCallback;
class ICallback; class ICallback;
class CGlobalAI; class CGlobalAI;
class CGHeroInstance; class CGHeroInstance;
class Component;
class CSelectableComponent; class CSelectableComponent;
struct HeroMoveDetails; struct HeroMoveDetails;
class CGHeroInstance; class CGHeroInstance;
@ -59,6 +60,7 @@ public:
virtual void tileRevealed(int3 pos){}; virtual void tileRevealed(int3 pos){};
virtual void tileHidden(int3 pos){}; virtual void tileHidden(int3 pos){};
virtual void receivedResource(int type, int val){}; virtual void receivedResource(int type, int val){};
virtual void showInfoDialog(std::string text, std::vector<Component*> &components)=0{};
virtual void showSelDialog(std::string text, std::vector<CSelectableComponent*> & components, int askID)=0{}; virtual void showSelDialog(std::string text, std::vector<CSelectableComponent*> & components, int askID)=0{};
virtual void garrisonChanged(const CGObjectInstance * obj){}; virtual void garrisonChanged(const CGObjectInstance * obj){};
virtual void buildChanged(const CGTownInstance *town, int buildingID, int what){}; //what: 1 - built, 2 - demolished virtual void buildChanged(const CGTownInstance *town, int buildingID, int what){}; //what: 1 - built, 2 - demolished

View File

@ -104,7 +104,6 @@ public:
friend class CClient; friend class CClient;
friend void initGameState(Mapa * map, CGameInfo * cgi); friend void initGameState(Mapa * map, CGameInfo * cgi);
friend class CScriptCallback; friend class CScriptCallback;
friend void handleCPPObjS(std::map<int,CCPPObjectScript*> * mapa, CCPPObjectScript * script);
friend class CMapHandler; friend class CMapHandler;
friend class CGameHandler; friend class CGameHandler;
}; };

View File

@ -1034,10 +1034,10 @@ void LClickableArea::deactivate()
} }
void LClickableArea::clickLeft(boost::logic::tribool down) void LClickableArea::clickLeft(boost::logic::tribool down)
{ {
if(!down) //if(!down)
{ //{
LOCPLINT->showInfoDialog("TEST TEST AAA", std::vector<SComponent*>()); // LOCPLINT->showInfoDialog("TEST TEST AAA", std::vector<SComponent*>());
} //}
} }
void RClickableArea::activate() void RClickableArea::activate()
@ -1050,10 +1050,10 @@ void RClickableArea::deactivate()
} }
void RClickableArea::clickRight(boost::logic::tribool down) void RClickableArea::clickRight(boost::logic::tribool down)
{ {
if(!down) //if(!down)
{ //{
LOCPLINT->showInfoDialog("TEST TEST AAA", std::vector<SComponent*>()); // LOCPLINT->showInfoDialog("TEST TEST AAA", std::vector<SComponent*>());
} //}
} }
void LRClickableAreaWText::clickLeft(boost::logic::tribool down) void LRClickableAreaWText::clickLeft(boost::logic::tribool down)

View File

@ -291,7 +291,7 @@ void CVisitableOPH::onNAHeroVisit(int objid, int heroID, bool alreadyVisited)
else else
{ {
ot++; ot++;
cb->showInfoDialog(cb->getHeroOwner(heroID),VLC->objh->advobtxt[ot],&std::vector<SComponent*>()); //cb->showInfoDialog(cb->getHeroOwner(heroID),VLC->objh->advobtxt[ot],&std::vector<SComponent*>());
} }
} }
@ -415,36 +415,32 @@ void CMines::newObject(int objid)
} }
void CMines::onHeroVisit(int objid, int heroID) void CMines::onHeroVisit(int objid, int heroID)
{ {
//TODO: this is code for standard mines, no support for abandoned mine (subId==7)
DEFOS; DEFOS;
const CGHeroInstance *h = cb->getHero(heroID); const CGHeroInstance *h = cb->getHero(heroID);
cb->setOwner(objid,h->tempOwner); cb->setOwner(objid,h->tempOwner);
MetaString ms; MetaString ms;
ms << std::pair<ui8,ui32>(9,os->subID) << " " << std::pair<ui8,ui32>(6,23+h->tempOwner); ms << std::pair<ui8,ui32>(9,os->subID) << " " << std::pair<ui8,ui32>(6,23+h->tempOwner);
cb->setHoverName(objid,&ms); cb->setHoverName(objid,&ms);
//int vv = 1; ms.clear();
//if (os->subID==0 || os->subID==2)
// vv++; int vv=1; //amount of resource per turn
//else if (os->subID==6) if (os->subID==0 || os->subID==2)
// vv = 1000; vv++;
//if (os->tempOwner == cb->getHeroOwner(heroID)) else if (os->subID==6)
//{ vv = 1000;
// //TODO: garrison
//} InfoWindow iw;
//else iw.text << std::pair<ui8,ui32>(10,os->subID);
//{ iw.player = h->tempOwner;
// if (os->subID==7) iw.components.push_back(Component(2,os->subID,vv,-1));
// return; //TODO: support for abandoned mine cb->showInfoDialog(&iw);
// os->tempOwner = cb->getHeroOwner(heroID); //TODO: leaving garrison
// SComponent * com = new SComponent(SComponent::Etype::resource,os->subID,vv);
// com->subtitle+=VLC->generaltexth->allTexts[3].substr(2,VLC->generaltexth->allTexts[3].length()-2);
// std::vector<SComponent*> weko;
// weko.push_back(com);
// cb->showInfoDialog(cb->getHeroOwner(heroID),VLC->objh->mines[os->subID].second,&weko);
//}
} }
std::vector<int> CMines::yourObjects() std::vector<int> CMines::yourObjects()
{ {
std::vector<int> ret(1); std::vector<int> ret;
ret.push_back(53); ret.push_back(53);
return ret; return ret;
} }

View File

@ -33,6 +33,7 @@
#include "client/CCreatureAnimation.h" #include "client/CCreatureAnimation.h"
#include "client/Graphics.h" #include "client/Graphics.h"
#include "map.h" #include "map.h"
#include "lib/NetPacks.h"
using namespace CSDL_Ext; using namespace CSDL_Ext;
extern TTF_Font * GEOR16; extern TTF_Font * GEOR16;
@ -41,6 +42,14 @@ CPlayerInterface * LOCPLINT;
extern std::queue<SDL_Event> events; extern std::queue<SDL_Event> events;
extern boost::mutex eventsM; extern boost::mutex eventsM;
SComponent toSComponent(const Component *c)
{
SComponent ret((SComponent::Etype)c->type,c->subtype,c->val);
if(c->type == 2)
ret.subtitle+=CGI->generaltexth->allTexts[3].substr(2,CGI->generaltexth->allTexts[3].length()-2);
return ret;
}
class OCM_HLP_CGIN class OCM_HLP_CGIN
{ {
public: public:
@ -2014,6 +2023,24 @@ void CPlayerInterface::showComp(SComponent comp)
adventureInt->infoBar.showComp(&comp,4000); adventureInt->infoBar.showComp(&comp,4000);
} }
void CPlayerInterface::showInfoDialog(std::string text, std::vector<Component*> &components)
{
curint->deactivate(); //dezaktywacja starego interfejsu
std::vector<SComponent*> intComps;
CInfoWindow * temp = CMessage::genIWindow(text,LOCPLINT->playerID,32,intComps);
LOCPLINT->objsToBlit.push_back(temp);
temp->pos.x=300-(temp->pos.w/2);
temp->pos.y=300-(temp->pos.h/2);
temp->okb.pos.x = temp->okb.posr.x + temp->pos.x;
temp->okb.pos.y = temp->okb.posr.y + temp->pos.y;
temp->okb.activate();
for (int i=0;i<temp->components.size();i++)
{
temp->components[i]->activate();
temp->components[i]->pos.x += temp->pos.x;
temp->components[i]->pos.y += temp->pos.y;
}
}
void CPlayerInterface::showInfoDialog(std::string text, std::vector<SComponent*> & components) void CPlayerInterface::showInfoDialog(std::string text, std::vector<SComponent*> & components)
{ {
curint->deactivate(); //dezaktywacja starego interfejsu curint->deactivate(); //dezaktywacja starego interfejsu

View File

@ -154,7 +154,7 @@ public:
}; };
class CInfoWindow : public CSimpleWindow //text + comp. + ok button class CInfoWindow : public CSimpleWindow //text + comp. + ok button
{ //okno usuwa swoje komponenty w chwili zamkniecia { //window deletes its components when closed
public: public:
CSCButton<CInfoWindow> okb; CSCButton<CInfoWindow> okb;
std::vector<SComponent*> components; std::vector<SComponent*> components;
@ -320,6 +320,7 @@ public:
void heroCreated(const CGHeroInstance* hero); void heroCreated(const CGHeroInstance* hero);
void heroPrimarySkillChanged(const CGHeroInstance * hero, int which, int val); void heroPrimarySkillChanged(const CGHeroInstance * hero, int which, int val);
void receivedResource(int type, int val); void receivedResource(int type, int val);
void showInfoDialog(std::string text, std::vector<Component*> &components);
void showSelDialog(std::string text, std::vector<CSelectableComponent*> & components, int askID); void showSelDialog(std::string text, std::vector<CSelectableComponent*> & components, int askID);
void heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town); void heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town);
void garrisonChanged(const CGObjectInstance * obj); void garrisonChanged(const CGObjectInstance * obj);
@ -350,8 +351,8 @@ public:
void handleMouseMotion(SDL_Event *sEvent); void handleMouseMotion(SDL_Event *sEvent);
void init(ICallback * CB); void init(ICallback * CB);
int3 repairScreenPos(int3 pos); int3 repairScreenPos(int3 pos);
void showInfoDialog(std::string text, std::vector<SComponent*> & components);
void removeObjToBlit(IShowable* obj); void removeObjToBlit(IShowable* obj);
void showInfoDialog(std::string text, std::vector<SComponent*> & components);
CPlayerInterface(int Player, int serial);//c-tor CPlayerInterface(int Player, int serial);//c-tor
}; };

View File

@ -29,20 +29,26 @@ std::string toString(MetaString &ms)
else else
{ {
std::vector<std::string> *vec; std::vector<std::string> *vec;
int type = ms.texts[-ms.message[i]-1].first; int type = ms.texts[-ms.message[i]-1].first,
ser = ms.texts[-ms.message[i]-1].second;
if(type == 5) if(type == 5)
{ {
ret += CGI->arth->artifacts[ms.texts[-ms.message[i]-1].second].name; ret += CGI->arth->artifacts[ser].name;
continue; continue;
} }
else if(type == 7) else if(type == 7)
{ {
ret += CGI->creh->creatures[ms.texts[-ms.message[i]-1].second].namePl; ret += CGI->creh->creatures[ser].namePl;
continue; continue;
} }
else if(type == 9) else if(type == 9)
{ {
ret += CGI->objh->mines[ms.texts[-ms.message[i]-1].second].first; ret += CGI->objh->mines[ser].first;
continue;
}
else if(type == 10)
{
ret += CGI->objh->mines[ser].second;
continue; continue;
} }
else else
@ -68,7 +74,7 @@ std::string toString(MetaString &ms)
vec = &CGI->objh->creGens; vec = &CGI->objh->creGens;
break; break;
} }
ret += (*vec)[ms.texts[-ms.message[i]-1].second]; ret += (*vec)[ser];
} }
} }
} }
@ -159,6 +165,22 @@ void CClient::process(int what)
std::cout << "done!"<<std::endl; std::cout << "done!"<<std::endl;
break; break;
} }
case 102: //set resource amount
{
SetResource sr;
*serv >> sr;
std::cout << "Set amount of "<<CGI->objh->restypes[sr.resid]
<< " of player "<<(unsigned)sr.player <<" to "<<sr.val<<std::endl;
gs->apply(&sr);
playerint[sr.player]->receivedResource(sr.resid,sr.val);
break;
}
case 103: //show info dialog
{
InfoWindow iw;
*serv >> iw;
}
case 501: //hero movement response - we have to notify interfaces and callback case 501: //hero movement response - we have to notify interfaces and callback
{ {
TryMoveHero *th = new TryMoveHero; //will be deleted by callback after processing TryMoveHero *th = new TryMoveHero; //will be deleted by callback after processing

View File

@ -59,6 +59,18 @@ struct NewTurn : public CPack<NewTurn> //101
h & heroes & res & day & resetBuilded; h & heroes & res & day & resetBuilded;
} }
}; };
struct SetResource : public CPack<SetResource> //102
{
SetResource(){type = 102;};
ui8 player, resid;
si32 val;
template <typename Handler> void serialize(Handler &h, const int version)
{
h & player & resid & val;
}
};
struct TryMoveHero : public CPack<TryMoveHero> //501 struct TryMoveHero : public CPack<TryMoveHero> //501
{ {
TryMoveHero(){type = 501;}; TryMoveHero(){type = 501;};
@ -76,7 +88,7 @@ struct TryMoveHero : public CPack<TryMoveHero> //501
struct MetaString : public CPack<MetaString> //2001 helper for object scrips struct MetaString : public CPack<MetaString> //2001 helper for object scrips
{ {
std::vector<std::string> strings; std::vector<std::string> strings;
std::vector<std::pair<ui8,ui32> > texts; //pairs<text handler type, text number>; types: 1 - generaltexthandler->all; 2 - objh->xtrainfo; 3 - objh->names; 4 - objh->restypes; 5 - arth->artifacts[id].name; 6 - generaltexth->arraytxt; 7 - creh->creatures[os->subID].namePl; 8 - objh->creGens; 9 - objh->mines[ID].first std::vector<std::pair<ui8,ui32> > texts; //pairs<text handler type, text number>; types: 1 - generaltexthandler->all; 2 - objh->xtrainfo; 3 - objh->names; 4 - objh->restypes; 5 - arth->artifacts[id].name; 6 - generaltexth->arraytxt; 7 - creh->creatures[os->subID].namePl; 8 - objh->creGens; 9 - objh->mines[ID].first; 10 - objh->mines[ID].second
std::vector<si32> message; std::vector<si32> message;
template <typename Handler> void serialize(Handler &h, const int version) template <typename Handler> void serialize(Handler &h, const int version)
@ -96,9 +108,41 @@ struct MetaString : public CPack<MetaString> //2001 helper for object scrips
strings.push_back(txt); strings.push_back(txt);
return *this; return *this;
} }
void clear()
{
strings.clear();
texts.clear();
message.clear();
}
MetaString(){type = 2001;}; MetaString(){type = 2001;};
}; };
struct Component : public CPack<Component> //2002 helper for object scrips informations
{
ui16 type, subtype; //types: 0 - primskill; 1 - secskill; 2 - resource; 3 - creature; 4 - artifact; 5 - experience
si32 val; // + give; - take
si16 when; // 0 - now; +x - within x days; -x - per x days
template <typename Handler> void serialize(Handler &h, const int version)
{
h & type & subtype & val & when;
}
Component(){type = 2002;};
Component(ui16 Type, ui16 Subtype, si32 Val, si16 When):type(Type),subtype(Subtype),val(Val),when(When){type = 2002;};
};
struct InfoWindow : public CPack<InfoWindow> //103 - displays simple info window
{
MetaString text;
std::vector<Component> components;
ui8 player;
template <typename Handler> void serialize(Handler &h, const int version)
{
h & text & components & player;
}
InfoWindow(){type = 103;};
};
struct SetObjectProperty : public CPack<SetObjectProperty>//1001 struct SetObjectProperty : public CPack<SetObjectProperty>//1001
{ {

View File

@ -213,22 +213,6 @@ void CGameHandler::init(StartInfo *si, int Seed)
gs = new CGameState(); gs = new CGameState();
gs->init(si,map,Seed); gs->init(si,map,Seed);
/****************************SCRIPTS************************************************/
//std::map<int, std::map<std::string, CObjectScript*> > * skrypty = &objscr; //alias for easier access
/****************************C++ OBJECT SCRIPTS************************************************/
std::map<int,CCPPObjectScript*> scripts;
CScriptCallback * csc = new CScriptCallback();
csc->gh = this;
handleCPPObjS(&scripts,new CVisitableOPH(csc));
handleCPPObjS(&scripts,new CVisitableOPW(csc));
handleCPPObjS(&scripts,new CPickable(csc));
handleCPPObjS(&scripts,new CMines(csc));
handleCPPObjS(&scripts,new CTownScript(csc));
handleCPPObjS(&scripts,new CHeroScript(csc));
handleCPPObjS(&scripts,new CMonsterS(csc));
handleCPPObjS(&scripts,new CCreatureGen(csc));
//created map
/****************************LUA OBJECT SCRIPTS************************************************/ /****************************LUA OBJECT SCRIPTS************************************************/
//std::vector<std::string> * lf = CLuaHandler::searchForScripts("scripts/lua/objects"); //files //std::vector<std::string> * lf = CLuaHandler::searchForScripts("scripts/lua/objects"); //files
//for (int i=0; i<lf->size(); i++) //for (int i=0; i<lf->size(); i++)
@ -252,25 +236,6 @@ void CGameHandler::init(StartInfo *si, int Seed)
// delete temp; // delete temp;
// }HANDLE_EXCEPTION // }HANDLE_EXCEPTION
//} //}
/****************************INITIALIZING OBJECT SCRIPTS************************************************/
//std::string temps("newObject");
for (unsigned i=0; i<map->objects.size(); i++)
{
//c++ scripts
if (scripts.find(map->objects[i]->ID) != scripts.end())
{
map->objects[i]->state = scripts[map->objects[i]->ID];
map->objects[i]->state->newObject(map->objects[i]->id);
}
else
{
map->objects[i]->state = NULL;
}
//// lua scripts
//if(checkFunc(map->objects[i]->ID,temps))
// (*skrypty)[map->objects[i]->ID][temps]->newObject(map->objects[i]);
}
//delete lf; //delete lf;
} }
@ -363,6 +328,42 @@ void CGameHandler::run()
boost::thread(boost::bind(&CGameHandler::handleConnection,this,pom,boost::ref(**i))); boost::thread(boost::bind(&CGameHandler::handleConnection,this,pom,boost::ref(**i)));
} }
/****************************SCRIPTS************************************************/
//std::map<int, std::map<std::string, CObjectScript*> > * skrypty = &objscr; //alias for easier access
/****************************C++ OBJECT SCRIPTS************************************************/
std::map<int,CCPPObjectScript*> scripts;
CScriptCallback * csc = new CScriptCallback();
csc->gh = this;
handleCPPObjS(&scripts,new CVisitableOPH(csc));
handleCPPObjS(&scripts,new CVisitableOPW(csc));
handleCPPObjS(&scripts,new CPickable(csc));
handleCPPObjS(&scripts,new CMines(csc));
handleCPPObjS(&scripts,new CTownScript(csc));
handleCPPObjS(&scripts,new CHeroScript(csc));
handleCPPObjS(&scripts,new CMonsterS(csc));
handleCPPObjS(&scripts,new CCreatureGen(csc));
/****************************INITIALIZING OBJECT SCRIPTS************************************************/
//std::string temps("newObject");
for (unsigned i=0; i<gs->map->objects.size(); i++)
{
//c++ scripts
if (scripts.find(gs->map->objects[i]->ID) != scripts.end())
{
gs->map->objects[i]->state = scripts[gs->map->objects[i]->ID];
gs->map->objects[i]->state->newObject(gs->map->objects[i]->id);
}
else
{
gs->map->objects[i]->state = NULL;
}
//// lua scripts
//if(checkFunc(map->objects[i]->ID,temps))
// (*skrypty)[map->objects[i]->ID][temps]->newObject(map->objects[i]);
}
while (!end) while (!end)
{ {
newTurn(); newTurn();

View File

@ -87,8 +87,9 @@ int CScriptCallback::getHeroOwner(int heroID)
//return hero->getOwner(); //return hero->getOwner();
return -1; return -1;
} }
void CScriptCallback::showInfoDialog(int player, std::string text, std::vector<SComponent*> * components) void CScriptCallback::showInfoDialog(InfoWindow *iw)
{ {
gh->sendToAllClients(iw);
//TODO: upewniac sie ze mozemy to zrzutowac (przy customowych interfejsach cos moze sie kopnac) //TODO: upewniac sie ze mozemy to zrzutowac (przy customowych interfejsach cos moze sie kopnac)
//if (player>=0) //if (player>=0)
//{ //{
@ -153,6 +154,11 @@ int CScriptCallback::getDate(int mode)
} }
void CScriptCallback::giveResource(int player, int which, int val) void CScriptCallback::giveResource(int player, int which, int val)
{ {
SetResource sr;
sr.player = player;
sr.resid = which;
sr.val = (gh->gs->players[player].resources[which]+val);
gh->sendAndApply(&sr);
//gh->gs->players[player].resources[which]+=val; //gh->gs->players[player].resources[which]+=val;
//sv->playerint[player]->receivedResource(which,val); //sv->playerint[player]->receivedResource(which,val);
} }

View File

@ -14,6 +14,7 @@ class CGTownInstance;
class CGameState; class CGameState;
struct lua_State; struct lua_State;
struct MetaString; struct MetaString;
struct InfoWindow;
class CScriptCallback class CScriptCallback
{ {
CScriptCallback(void); CScriptCallback(void);
@ -35,7 +36,7 @@ public:
void setOwner(int objid, ui8 owner); void setOwner(int objid, ui8 owner);
void setHoverName(int objid, MetaString * name); void setHoverName(int objid, MetaString * name);
void changePrimSkill(int ID, int which, int val); void changePrimSkill(int ID, int which, int val);
void showInfoDialog(int player, std::string text, std::vector<SComponent*> * components); //TODO: obslugiwac nulle void showInfoDialog(InfoWindow *iw);
void showSelDialog(int player, std::string text, std::vector<CSelectableComponent*>*components, IChosen * asker); void showSelDialog(int player, std::string text, std::vector<CSelectableComponent*>*components, IChosen * asker);
void giveResource(int player, int which, int val); void giveResource(int player, int which, int val);
void showCompInfo(int player, SComponent * comp); void showCompInfo(int player, SComponent * comp);